1027
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
ObjectCoder subclass:#Base64Coder
|
|
4 |
instanceVariableNames:'buffer bits charCount atEnd'
|
|
5 |
classVariableNames:'Base64Mapping Base64ReverseMapping'
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'System-Storage'
|
|
8 |
!
|
|
9 |
|
|
10 |
!Base64Coder class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
documentation
|
|
13 |
"
|
|
14 |
Instances of this class perform Base64 en- and decoding as defined in RFC 2045
|
|
15 |
3 bytes are mapped to 4 characters, representing 6 bits each.
|
|
16 |
|
|
17 |
[author:]
|
|
18 |
Stefan Vogel
|
|
19 |
|
|
20 |
[see also:]
|
|
21 |
|
|
22 |
[instance variables:]
|
|
23 |
buffer SmallInteger Up to 24 bits of data
|
|
24 |
bits SmallInteger Number of valid bits in buffer
|
|
25 |
charCount SmallInteger Number of characters since last cr
|
|
26 |
atEnd Boolean true if end of Base64 string reached
|
|
27 |
|
|
28 |
[class variables:]
|
|
29 |
Base64Mapping String Mapping from bytes (with 6 valid bits)
|
|
30 |
to Base64 characters
|
|
31 |
Base64ReverseMapping Array Mapping from Base64 characters to 6-bit-Bytes
|
|
32 |
"
|
|
33 |
!
|
|
34 |
|
|
35 |
examples
|
|
36 |
"
|
|
37 |
|
|
38 |
[exBegin]
|
|
39 |
|coder|
|
|
40 |
|
|
41 |
coder := Base64Coder on:'' writeStream.
|
|
42 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19].
|
|
43 |
coder flush.
|
|
44 |
coder contents inspect.
|
|
45 |
coder reset.
|
|
46 |
coder nextPut:254.
|
|
47 |
coder contents inspect.
|
|
48 |
[exEnd]
|
|
49 |
|
|
50 |
[exBegin]
|
|
51 |
|coder coder1|
|
|
52 |
|
|
53 |
coder := Base64Coder on:'' writeStream.
|
|
54 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20].
|
|
55 |
coder flush.
|
|
56 |
|
|
57 |
coder1 := Base64Coder on:coder contents readStream.
|
|
58 |
[coder1 atEnd] whileFalse:[
|
|
59 |
Transcript show:coder1 next
|
|
60 |
].
|
|
61 |
Transcript cr.
|
|
62 |
[exEnd]
|
|
63 |
"
|
|
64 |
! !
|
|
65 |
|
|
66 |
!Base64Coder class methodsFor:'initialization'!
|
|
67 |
|
|
68 |
initialize
|
|
69 |
"initialize class variables"
|
|
70 |
|
|
71 |
"64 characters representing the 6-bit values from 0-63 and one pad character"
|
|
72 |
Base64Mapping := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.
|
|
73 |
Base64ReverseMapping := ByteArray new:256 withAll:255.
|
|
74 |
Base64Mapping keysAndValuesDo:[:idx :char|
|
|
75 |
Base64ReverseMapping at:char asciiValue put:idx-1.
|
|
76 |
].
|
|
77 |
|
|
78 |
"
|
|
79 |
self initialize
|
|
80 |
"
|
|
81 |
! !
|
|
82 |
|
|
83 |
!Base64Coder class methodsFor:'instance creation'!
|
|
84 |
|
|
85 |
new
|
|
86 |
^ self basicNew initialize
|
|
87 |
! !
|
|
88 |
|
1063
|
89 |
!Base64Coder methodsFor:'decoding'!
|
1027
|
90 |
|
|
91 |
next
|
|
92 |
"answer the next decoded byte"
|
|
93 |
|
|
94 |
|b shift|
|
|
95 |
|
|
96 |
bits == 0 ifTrue:[
|
|
97 |
"no more bits in buffer
|
|
98 |
if end has been reached, raise signal"
|
|
99 |
atEnd ifTrue:[
|
1063
|
100 |
^ stream class endOfStreamSignal raiseRequest.
|
1027
|
101 |
].
|
|
102 |
|
|
103 |
"read next 4 characters each representing 6 bits"
|
|
104 |
buffer := 0.
|
|
105 |
[
|
|
106 |
"read next valid Base64 character, skip invalid characters"
|
|
107 |
[
|
|
108 |
b := Base64ReverseMapping at:stream next asciiValue.
|
|
109 |
] doWhile:[b == 255].
|
|
110 |
|
|
111 |
b == 64 ifTrue:[
|
|
112 |
"got #=, end of Base64 string has been reached"
|
|
113 |
atEnd := true.
|
|
114 |
bits == 12 ifTrue:[
|
|
115 |
"data has been padded to 12, skip 4 bits"
|
|
116 |
shift := -4.
|
|
117 |
] ifFalse:[
|
|
118 |
"data has been padded to 18, skip 2 bits"
|
|
119 |
shift := -2.
|
|
120 |
].
|
|
121 |
bits := bits + shift.
|
|
122 |
buffer := buffer bitShift:shift.
|
|
123 |
] ifFalse:[
|
|
124 |
"got valid Base64 character, append to buffer"
|
|
125 |
buffer := (buffer bitShift:6) bitOr:b.
|
|
126 |
bits := bits + 6.
|
|
127 |
].
|
|
128 |
] doWhile:[bits ~~ 24 and:[atEnd not]].
|
|
129 |
].
|
|
130 |
|
|
131 |
b := (buffer bitShift:(8 - bits)) bitAnd:16rFF.
|
|
132 |
bits := bits - 8.
|
|
133 |
|
|
134 |
^ b.
|
1063
|
135 |
! !
|
|
136 |
|
|
137 |
!Base64Coder methodsFor:'encoding'!
|
|
138 |
|
|
139 |
encodeByteArray:aByteArray with:aParameter
|
|
140 |
|
|
141 |
^ self nextPutAll:aByteArray; flush.
|
|
142 |
|
|
143 |
"
|
|
144 |
self encodingOf:#[1 2 3 4 5 6 255]
|
|
145 |
"
|
|
146 |
!
|
|
147 |
|
|
148 |
encodeObject:anObject with:aParameter
|
|
149 |
"not defined. Use nextPut or nextPutAll:.
|
|
150 |
Could encode the printString here"
|
|
151 |
|
|
152 |
^ self shouldNotImplement
|
|
153 |
!
|
|
154 |
|
|
155 |
encodeString:aString with:aParameter
|
|
156 |
|
|
157 |
^ self nextPutAll:aString asByteArray; flush.
|
|
158 |
|
|
159 |
"
|
|
160 |
self encodingOf:'hello world'
|
|
161 |
"
|
1027
|
162 |
!
|
|
163 |
|
|
164 |
nextPut:aByte
|
|
165 |
"encode aByte on the output stream"
|
|
166 |
|
|
167 |
|b1 b2 b3 b4|
|
|
168 |
|
|
169 |
buffer := (buffer bitShift:8) bitOr:aByte.
|
|
170 |
bits := bits + 8.
|
|
171 |
bits == 24 ifTrue:[
|
|
172 |
b4 := buffer bitAnd:16r3F.
|
|
173 |
b3 := (buffer bitShift:-6) bitAnd:16r3F.
|
|
174 |
b2 := (buffer bitShift:-12) bitAnd:16r3F.
|
|
175 |
b1 := (buffer bitShift:-18) bitAnd:16r3F.
|
|
176 |
buffer := bits := 0.
|
|
177 |
stream nextPut:(Base64Mapping at:b1+1);
|
|
178 |
nextPut:(Base64Mapping at:b2+1);
|
|
179 |
nextPut:(Base64Mapping at:b3+1);
|
|
180 |
nextPut:(Base64Mapping at:b4+1).
|
|
181 |
|
|
182 |
"RFC 2045 says: max 76 characters in one line"
|
|
183 |
charCount >= 68 ifTrue:[
|
|
184 |
stream cr.
|
|
185 |
charCount := 0.
|
|
186 |
] ifFalse:[
|
|
187 |
charCount := charCount + 4.
|
|
188 |
]
|
|
189 |
].
|
|
190 |
! !
|
|
191 |
|
|
192 |
!Base64Coder methodsFor:'initialization'!
|
|
193 |
|
|
194 |
initialize
|
|
195 |
|
|
196 |
buffer := bits := charCount := 0.
|
|
197 |
atEnd := false.
|
|
198 |
! !
|
|
199 |
|
|
200 |
!Base64Coder methodsFor:'misc'!
|
|
201 |
|
1063
|
202 |
emptyWriteStream
|
|
203 |
|
|
204 |
^ WriteStream on:(String new:64)
|
|
205 |
!
|
|
206 |
|
1027
|
207 |
flush
|
|
208 |
"flush the remaining bits of buffer.
|
|
209 |
The number of bits in buffer is not a multiple of 6, so we pad
|
|
210 |
the buffer and signal that padding has been done via $= characters."
|
|
211 |
|
|
212 |
|b1 b2 b3 b4|
|
|
213 |
|
|
214 |
bits == 0 ifTrue:[
|
|
215 |
"buffer is empty, nothing to do"
|
|
216 |
^ self.
|
|
217 |
].
|
|
218 |
|
|
219 |
bits == 8 ifTrue:[
|
|
220 |
buffer := buffer bitShift:4.
|
|
221 |
b4 := b3 := 64. "pad with '=='"
|
|
222 |
b1 := (buffer bitShift:-6) bitAnd:16r3F.
|
|
223 |
b2 := buffer bitAnd:16r3F.
|
|
224 |
] ifFalse:[bits = 16 ifTrue:[
|
|
225 |
buffer := buffer bitShift:2.
|
|
226 |
b4 := 64. "pad with '='"
|
|
227 |
b3 := buffer bitAnd:16r3F.
|
|
228 |
b2 := (buffer bitShift:-6) bitAnd:16r3F.
|
|
229 |
b1 := (buffer bitShift:-12) bitAnd:16r3F.
|
|
230 |
]].
|
|
231 |
bits := buffer := 0.
|
|
232 |
|
|
233 |
stream nextPut:(Base64Mapping at:b1+1);
|
|
234 |
nextPut:(Base64Mapping at:b2+1);
|
|
235 |
nextPut:(Base64Mapping at:b3+1);
|
|
236 |
nextPut:(Base64Mapping at:b4+1).
|
|
237 |
!
|
|
238 |
|
|
239 |
reset
|
|
240 |
"reset to initial state"
|
|
241 |
|
|
242 |
super reset.
|
|
243 |
buffer := bits := charCount := 0.
|
|
244 |
atEnd := false.
|
|
245 |
! !
|
|
246 |
|
|
247 |
!Base64Coder methodsFor:'queries'!
|
|
248 |
|
|
249 |
atEnd
|
|
250 |
"answer true, if no more bytes can be read"
|
|
251 |
|
1063
|
252 |
^ bits == 0 and:[atEnd or:[stream atEnd]]
|
|
253 |
! !
|
|
254 |
|
|
255 |
!Base64Coder methodsFor:'stream compatibility'!
|
|
256 |
|
|
257 |
upToEnd
|
|
258 |
"return a collection of the elements up-to the end.
|
|
259 |
Return nil if the stream-end is reached before."
|
|
260 |
|
|
261 |
|answerStream|
|
|
262 |
|
|
263 |
answerStream := WriteStream on:(ByteArray new:128).
|
|
264 |
[self atEnd] whileFalse:[
|
|
265 |
answerStream nextPut:self next
|
|
266 |
].
|
|
267 |
|
|
268 |
^ answerStream contents
|
1027
|
269 |
! !
|
|
270 |
|
|
271 |
!Base64Coder class methodsFor:'documentation'!
|
|
272 |
|
|
273 |
version
|
1063
|
274 |
^ '$Header: /cvs/stx/stx/libbasic2/Base64Coder.st,v 1.2 2002-07-02 12:33:35 stefan Exp $'
|
1027
|
275 |
! !
|
|
276 |
Base64Coder initialize!
|