author | Claus Gittinger <cg@exept.de> |
Thu, 06 Sep 2007 17:31:09 +0200 | |
changeset 1894 | 8bf137acc445 |
parent 1888 | 7113378d5a37 |
child 1964 | 671b01812775 |
permissions | -rw-r--r-- |
1308 | 1 |
" |
2 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
1027 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
14 |
ObjectCoder subclass:#Base64Coder |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
15 |
instanceVariableNames:'buffer bits charCount peekByte atEnd lineLimit' |
1027 | 16 |
classVariableNames:'Base64Mapping Base64ReverseMapping' |
17 |
poolDictionaries:'' |
|
18 |
category:'System-Storage' |
|
19 |
! |
|
20 |
||
21 |
!Base64Coder class methodsFor:'documentation'! |
|
22 |
||
1308 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
1027 | 37 |
documentation |
38 |
" |
|
39 |
Instances of this class perform Base64 en- and decoding as defined in RFC 2045 |
|
40 |
3 bytes are mapped to 4 characters, representing 6 bits each. |
|
41 |
||
42 |
[author:] |
|
43 |
Stefan Vogel |
|
44 |
||
45 |
[see also:] |
|
46 |
||
47 |
[instance variables:] |
|
48 |
buffer SmallInteger Up to 24 bits of data |
|
49 |
bits SmallInteger Number of valid bits in buffer |
|
50 |
charCount SmallInteger Number of characters since last cr |
|
51 |
atEnd Boolean true if end of Base64 string reached |
|
52 |
||
53 |
[class variables:] |
|
54 |
Base64Mapping String Mapping from bytes (with 6 valid bits) |
|
55 |
to Base64 characters |
|
56 |
Base64ReverseMapping Array Mapping from Base64 characters to 6-bit-Bytes |
|
57 |
" |
|
58 |
! |
|
59 |
||
60 |
examples |
|
61 |
" |
|
1122 | 62 |
[exBegin] |
1484 | 63 |
0 to:16 do:[:l | |
64 |
|coder decoder data encoding decoded| |
|
1391 | 65 |
|
66 |
data := (0 to:l) asByteArray copyTo:l. |
|
67 |
coder := Base64Coder on:'' writeStream. |
|
68 |
coder nextPutAll:data. |
|
69 |
coder flush. |
|
70 |
||
71 |
encoding := coder contents. |
|
72 |
||
73 |
decoder := Base64Coder on:encoding readStream. |
|
74 |
decoded := decoder upToEnd. |
|
75 |
Transcript showCR:(data printString). |
|
76 |
Transcript show:' -> '; showCR:encoding. |
|
77 |
Transcript show:' ---> '; showCR:(decoded printString). |
|
78 |
self assert:(data = decoded). |
|
79 |
]. |
|
80 |
[exEnd] |
|
81 |
[exBegin] |
|
1122 | 82 |
|data1 text data2| |
83 |
||
84 |
data1 := #[0 1 16r7F 16r80 16r81 16rFE 16rFF]. |
|
1639 | 85 |
text := Base64Coder encode:data1. |
86 |
data2 := Base64Coder decode:text. |
|
1122 | 87 |
data2 |
88 |
[exEnd] |
|
1027 | 89 |
|
90 |
[exBegin] |
|
91 |
|coder| |
|
92 |
||
93 |
coder := Base64Coder on:'' writeStream. |
|
94 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]. |
|
95 |
coder flush. |
|
96 |
coder contents inspect. |
|
97 |
coder reset. |
|
98 |
coder nextPut:254. |
|
99 |
coder contents inspect. |
|
100 |
[exEnd] |
|
101 |
||
102 |
[exBegin] |
|
1391 | 103 |
|coder decoder| |
1027 | 104 |
|
105 |
coder := Base64Coder on:'' writeStream. |
|
106 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]. |
|
107 |
coder flush. |
|
108 |
||
1391 | 109 |
decoder := Base64Coder on:(coder contents readStream). |
110 |
[decoder atEnd] whileFalse:[ |
|
111 |
Transcript show:decoder next |
|
1027 | 112 |
]. |
113 |
Transcript cr. |
|
114 |
[exEnd] |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
115 |
[exBegin] |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
116 |
|coder| |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
117 |
|
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
118 |
coder := Base64Coder on:'' writeStream. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
119 |
coder nextPutAll:(0 to:200) asByteArray. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
120 |
coder flush. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
121 |
|
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
122 |
Transcript showCR:(coder contents). |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
123 |
[exEnd] |
1027 | 124 |
" |
125 |
! ! |
|
126 |
||
127 |
!Base64Coder class methodsFor:'initialization'! |
|
128 |
||
129 |
initialize |
|
130 |
"initialize class variables" |
|
131 |
||
132 |
"64 characters representing the 6-bit values from 0-63 and one pad character" |
|
133 |
Base64Mapping := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='. |
|
134 |
Base64ReverseMapping := ByteArray new:256 withAll:255. |
|
135 |
Base64Mapping keysAndValuesDo:[:idx :char| |
|
1415
3ef6a2c42611
Use #codePoint instead of deprecated #asciiValue
Stefan Vogel <sv@exept.de>
parents:
1391
diff
changeset
|
136 |
Base64ReverseMapping at:char codePoint put:idx-1. |
1027 | 137 |
]. |
138 |
||
139 |
" |
|
140 |
self initialize |
|
141 |
" |
|
142 |
! ! |
|
143 |
||
144 |
!Base64Coder class methodsFor:'instance creation'! |
|
145 |
||
146 |
new |
|
147 |
^ self basicNew initialize |
|
148 |
! ! |
|
149 |
||
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
150 |
!Base64Coder methodsFor:'accessing'! |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
151 |
|
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
152 |
lineLimit:something |
1484 | 153 |
"set the line length of the encoded output. |
154 |
Default is a line length of 76 characters. |
|
155 |
||
156 |
If nil, no line breaks will be done." |
|
157 |
||
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
158 |
lineLimit := something. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
159 |
! ! |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
160 |
|
1063 | 161 |
!Base64Coder methodsFor:'decoding'! |
1027 | 162 |
|
1084
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
163 |
next |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
164 |
"answer the next decoded byte" |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
165 |
|
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
166 |
|b| |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
167 |
|
1111 | 168 |
peekByte notNil ifTrue:[ |
169 |
b := peekByte. |
|
170 |
peekByte := nil. |
|
171 |
^ b |
|
1027 | 172 |
]. |
1111 | 173 |
^ self basicNext. |
174 |
! |
|
1027 | 175 |
|
1111 | 176 |
peek |
1484 | 177 |
"answer the next decoded byte. Do not consume this byte" |
1027 | 178 |
|
1111 | 179 |
peekByte isNil ifTrue:[ |
180 |
peekByte := self basicNext. |
|
181 |
]. |
|
182 |
^ peekByte |
|
1063 | 183 |
! ! |
184 |
||
185 |
!Base64Coder methodsFor:'encoding'! |
|
186 |
||
1027 | 187 |
nextPut:aByte |
188 |
"encode aByte on the output stream" |
|
189 |
||
190 |
|b1 b2 b3 b4| |
|
191 |
||
192 |
buffer := (buffer bitShift:8) bitOr:aByte. |
|
193 |
bits := bits + 8. |
|
194 |
bits == 24 ifTrue:[ |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
195 |
"RFC 2045 says: max 76 characters in one line" |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
196 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
197 |
stream cr. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
198 |
charCount := 0. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
199 |
]. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
200 |
|
1027 | 201 |
b4 := buffer bitAnd:16r3F. |
202 |
b3 := (buffer bitShift:-6) bitAnd:16r3F. |
|
203 |
b2 := (buffer bitShift:-12) bitAnd:16r3F. |
|
204 |
b1 := (buffer bitShift:-18) bitAnd:16r3F. |
|
205 |
buffer := bits := 0. |
|
206 |
stream nextPut:(Base64Mapping at:b1+1); |
|
207 |
nextPut:(Base64Mapping at:b2+1); |
|
208 |
nextPut:(Base64Mapping at:b3+1); |
|
209 |
nextPut:(Base64Mapping at:b4+1). |
|
210 |
||
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
211 |
charCount := charCount + 4. |
1027 | 212 |
]. |
1458
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
213 |
! |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
214 |
|
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
215 |
visitByteArray:aByteArray with:aParameter |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
216 |
^ self |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
217 |
nextPutAll:aByteArray; |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
218 |
flush. |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
219 |
|
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
220 |
" |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
221 |
self encodingOf:#[1 2 3 4 5 6 255] |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
222 |
" |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
223 |
! |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
224 |
|
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
225 |
visitObject:anObject with:aParameter |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
226 |
"not defined. Use nextPut or nextPutAll:. |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
227 |
Could encode the printString here" |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
228 |
|
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
229 |
^ self shouldNotImplement |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
230 |
! |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
231 |
|
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
232 |
visitString:aString with:aParameter |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
233 |
^ self |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
234 |
nextPutAll:aString asByteArray; |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
235 |
flush. |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
236 |
|
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
237 |
" |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
238 |
|encoded decoded decoder| |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
239 |
|
1641 | 240 |
encoded := self encode:'hello world'. |
1458
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
241 |
decoded := #[] writeStream. |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
242 |
decoder := Base64Coder on:encoded readStream. |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
243 |
[decoder atEnd] whileFalse:[ |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
244 |
decoded nextPut:(decoder next). |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
245 |
]. |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
246 |
decoded := decoded contents. |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
247 |
decoded asString. |
0c76da4e4ef5
Now inherit from Visitor and use #visit...:with: methods instead
Stefan Vogel <sv@exept.de>
parents:
1442
diff
changeset
|
248 |
" |
1027 | 249 |
! ! |
250 |
||
251 |
!Base64Coder methodsFor:'initialization'! |
|
252 |
||
1078 | 253 |
emptyWriteStream |
1390 | 254 |
"answer an empty stream. We encode as string" |
255 |
||
256 |
^ WriteStream on:(String new:64) |
|
1078 | 257 |
! |
258 |
||
1027 | 259 |
initialize |
260 |
||
261 |
buffer := bits := charCount := 0. |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
262 |
lineLimit := 76. "RFC 2045 says: max 76 characters in one line" |
1027 | 263 |
atEnd := false. |
264 |
! ! |
|
265 |
||
266 |
!Base64Coder methodsFor:'misc'! |
|
267 |
||
268 |
flush |
|
1390 | 269 |
"flush the remaining bits of buffer. |
270 |
The number of bits in buffer is not a multiple of 6, so we pad |
|
271 |
the buffer and signal that padding has been done via $= characters." |
|
1027 | 272 |
|
1390 | 273 |
|b1 b2 b3 b4| |
274 |
||
275 |
bits == 0 ifTrue:[ |
|
1027 | 276 |
"buffer is empty, nothing to do" |
277 |
^ self. |
|
1390 | 278 |
]. |
279 |
||
280 |
bits == 8 ifTrue:[ |
|
1027 | 281 |
buffer := buffer bitShift:4. |
282 |
b4 := b3 := 64. "pad with '=='" |
|
283 |
b1 := (buffer bitShift:-6) bitAnd:16r3F. |
|
284 |
b2 := buffer bitAnd:16r3F. |
|
1390 | 285 |
] ifFalse:[ |
286 |
bits = 16 ifTrue:[ |
|
287 |
buffer := buffer bitShift:2. |
|
288 |
b4 := 64. "pad with '='" |
|
289 |
b3 := buffer bitAnd:16r3F. |
|
290 |
b2 := (buffer bitShift:-6) bitAnd:16r3F. |
|
291 |
b1 := (buffer bitShift:-12) bitAnd:16r3F. |
|
292 |
] |
|
293 |
]. |
|
294 |
bits := buffer := 0. |
|
1027 | 295 |
|
1391 | 296 |
"RFC 2045 says: max 76 characters in one line" |
297 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
|
298 |
stream cr. |
|
299 |
charCount := 0. |
|
300 |
]. |
|
301 |
||
1390 | 302 |
stream nextPut:(Base64Mapping at:b1+1); |
303 |
nextPut:(Base64Mapping at:b2+1); |
|
304 |
nextPut:(Base64Mapping at:b3+1); |
|
305 |
nextPut:(Base64Mapping at:b4+1). |
|
1391 | 306 |
charCount := charCount + 4. |
1027 | 307 |
! |
308 |
||
309 |
reset |
|
310 |
"reset to initial state" |
|
311 |
||
312 |
super reset. |
|
313 |
buffer := bits := charCount := 0. |
|
314 |
atEnd := false. |
|
1484 | 315 |
peekByte := nil. |
316 |
! ! |
|
317 |
||
318 |
!Base64Coder methodsFor:'private'! |
|
319 |
||
320 |
basicNext |
|
321 |
"answer the next decoded byte. |
|
322 |
Not peekByte handling is done here." |
|
323 |
||
324 |
|b| |
|
325 |
||
326 |
bits == 0 ifTrue:[ |
|
327 |
self fillBuffer. |
|
328 |
bits == 0 ifTrue:[ |
|
329 |
^ stream pastEndRead. |
|
330 |
] |
|
331 |
]. |
|
332 |
b := (buffer bitShift:(8 - bits)) bitAnd:16rFF. |
|
333 |
bits := bits - 8. |
|
334 |
||
335 |
^ b. |
|
336 |
! |
|
337 |
||
338 |
fillBuffer |
|
339 |
"fill buffer with next 4 characters each representing 6 bits" |
|
340 |
||
341 |
|b shift tempBuffer "{Class: SmallInteger}"| |
|
342 |
||
343 |
tempBuffer := 0. |
|
344 |
bits := 0. |
|
345 |
[ |
|
346 |
"read next valid Base64 character, skip invalid characters" |
|
347 |
[ |
|
348 |
b := stream next. |
|
349 |
b isNil ifTrue:[ "end of stream" |
|
350 |
b := 64. "/ end-mark |
|
351 |
] ifFalse:[ |
|
352 |
b := Base64ReverseMapping at:b codePoint. |
|
353 |
] |
|
354 |
] doWhile:[b == 255]. |
|
355 |
b == 64 ifTrue:[ |
|
356 |
"got $=, end of Base64 string has been reached" |
|
357 |
atEnd := true. |
|
358 |
bits == 12 ifTrue:[ |
|
359 |
"data has been padded to 12, skip 4 bits" |
|
360 |
shift := -4. |
|
361 |
] ifFalse:[bits == 18 ifTrue:[ |
|
362 |
"data has been padded to 18, skip 2 bits" |
|
363 |
shift := -2. |
|
364 |
] ifFalse:[ |
|
365 |
shift := 0. |
|
366 |
]]. |
|
367 |
tempBuffer := tempBuffer bitShift:shift. |
|
368 |
bits := bits + shift. |
|
369 |
] ifFalse:[ |
|
370 |
"got valid Base64 character, append to buffer" |
|
371 |
tempBuffer := (tempBuffer bitShift:6) bitOr:b. |
|
372 |
bits := bits + 6. |
|
373 |
]. |
|
374 |
] doWhile:[bits ~~ 24 and:[atEnd not]]. |
|
375 |
||
376 |
buffer := tempBuffer. |
|
1027 | 377 |
! ! |
378 |
||
379 |
!Base64Coder methodsFor:'queries'! |
|
380 |
||
381 |
atEnd |
|
382 |
"answer true, if no more bytes can be read" |
|
383 |
||
1484 | 384 |
bits == 0 ifTrue:[ |
1084
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
385 |
atEnd ifTrue:[^ true]. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
386 |
self fillBuffer. |
1484 | 387 |
bits == 0 ifTrue:[^ true]. |
1084
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
388 |
]. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
389 |
^ false. |
1111 | 390 |
! |
391 |
||
392 |
binary |
|
1484 | 393 |
"switch to binary mode - nothing is done here. |
394 |
Defined for compatibility with ExternalStream." |
|
395 |
||
1111 | 396 |
^ self |
397 |
! |
|
398 |
||
399 |
isStream |
|
1484 | 400 |
"we simulate a stream" |
401 |
||
1111 | 402 |
^ true |
1063 | 403 |
! ! |
404 |
||
405 |
!Base64Coder methodsFor:'stream compatibility'! |
|
406 |
||
1315 | 407 |
nextBytesInto:anObject startingAt:offset |
408 |
"copy bytes into anObject starting at offset" |
|
409 |
||
410 |
|off| |
|
411 |
||
412 |
off := offset. |
|
413 |
[self atEnd] whileFalse:[ |
|
414 |
anObject at:off put:self next. |
|
415 |
off := off + 1. |
|
416 |
]. |
|
417 |
^ off - offset |
|
418 |
! |
|
419 |
||
1063 | 420 |
upToEnd |
1484 | 421 |
"return a collection of the elements up-to the end" |
1063 | 422 |
|
423 |
|answerStream| |
|
424 |
||
425 |
answerStream := WriteStream on:(ByteArray new:128). |
|
1484 | 426 |
peekByte notNil ifTrue:[ |
427 |
answerStream nextPut:peekByte. |
|
428 |
peekByte := nil. |
|
1063 | 429 |
]. |
1484 | 430 |
[ |
1888 | 431 |
[bits >= 8] whileTrue:[ |
1484 | 432 |
answerStream nextPut:((buffer bitShift:(8 - bits)) bitAnd:16rFF). |
433 |
bits := bits - 8. |
|
434 |
]. |
|
435 |
self fillBuffer. |
|
1888 | 436 |
] doWhile:[bits > 0]. |
1063 | 437 |
|
438 |
^ answerStream contents |
|
1027 | 439 |
! ! |
440 |
||
441 |
!Base64Coder class methodsFor:'documentation'! |
|
442 |
||
443 |
version |
|
1888 | 444 |
^ '$Header: /cvs/stx/stx/libbasic2/Base64Coder.st,v 1.18 2007-07-11 14:05:31 cg Exp $' |
1027 | 445 |
! ! |
1111 | 446 |
|
1027 | 447 |
Base64Coder initialize! |