author | Claus Gittinger <cg@exept.de> |
Wed, 20 Mar 2019 21:23:46 +0100 | |
changeset 4891 | d8c52483ab4c |
parent 4754 | 95158fac7fde |
child 4894 | 206d7a1d926b |
permissions | -rw-r--r-- |
4743 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1308 | 3 |
" |
4 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
1027 | 14 |
"{ Package: 'stx:libbasic2' }" |
15 |
||
3478 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1964 | 18 |
BaseNCoder subclass:#Base64Coder |
19 |
instanceVariableNames:'' |
|
1027 | 20 |
classVariableNames:'Base64Mapping Base64ReverseMapping' |
21 |
poolDictionaries:'' |
|
22 |
category:'System-Storage' |
|
23 |
! |
|
24 |
||
25 |
!Base64Coder class methodsFor:'documentation'! |
|
26 |
||
1308 | 27 |
copyright |
28 |
" |
|
29 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
1027 | 41 |
documentation |
42 |
" |
|
43 |
Instances of this class perform Base64 en- and decoding as defined in RFC 2045 |
|
44 |
3 bytes are mapped to 4 characters, representing 6 bits each. |
|
2068 | 45 |
The encoded string consists only of characters from the set: |
46 |
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' |
|
1027 | 47 |
|
4520 | 48 |
The main entry point API is |
4279 | 49 |
Base64Coder encode:aStringOrBytes |
50 |
and |
|
51 |
Base64Coder decode:aString |
|
52 |
||
53 |
If the decoder should return a string, use |
|
54 |
Base64Coder decodeAsString:aString. |
|
55 |
||
1027 | 56 |
[author:] |
57 |
Stefan Vogel |
|
58 |
||
59 |
[see also:] |
|
4743 | 60 |
RFC https://tools.ietf.org/html/rfc4648 |
61 |
||
1027 | 62 |
[instance variables:] |
63 |
||
64 |
[class variables:] |
|
4520 | 65 |
Base64Mapping String Mapping from bytes (with 6 valid bits) |
1027 | 66 |
to Base64 characters |
67 |
Base64ReverseMapping Array Mapping from Base64 characters to 6-bit-Bytes |
|
68 |
" |
|
69 |
! |
|
70 |
||
71 |
examples |
|
72 |
" |
|
1122 | 73 |
[exBegin] |
3478 | 74 |
(Base64Coder encode:'queen%27s%20gambit') asString = 'cXVlZW4lMjdzJTIwZ2FtYml0' |
75 |
[exEnd] |
|
76 |
||
77 |
[exBegin] |
|
78 |
(Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString = 'queen%27s%20gambit' |
|
2224 | 79 |
[exEnd] |
80 |
||
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. |
|
4520 | 87 |
data2 |
1122 | 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] |
4743 | 124 |
[exBegin] |
125 |
|bytes encoded decoded| |
|
126 |
||
127 |
bytes := #[0 0 0] copy. |
|
128 |
0 to:255 do:[:b1 | |
|
129 |
Transcript showCR:b1. |
|
130 |
bytes at:1 put:b1. |
|
131 |
0 to:255 do:[:b2 | |
|
132 |
bytes at:2 put:b2. |
|
133 |
0 to:255 do:[:b3 | |
|
134 |
bytes at:3 put:b3. |
|
135 |
encoded := Base64Coder encode:bytes. |
|
136 |
decoded := Base64Coder decode:encoded. |
|
137 |
self assert:(decoded = bytes). |
|
138 |
] |
|
139 |
] |
|
140 |
]. |
|
141 |
[exEnd] |
|
1027 | 142 |
" |
143 |
! ! |
|
144 |
||
145 |
!Base64Coder class methodsFor:'initialization'! |
|
146 |
||
3547
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
147 |
initializeMappings |
1027 | 148 |
"initialize class variables" |
149 |
||
3547
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
150 |
Base64Mapping isNil ifTrue:[ |
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
151 |
"65 characters representing the 6-bit values from 0-63 and one pad character" |
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
152 |
Base64Mapping := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='. |
4743 | 153 |
Base64ReverseMapping := self reverseMappingFor:Base64Mapping. |
1027 | 154 |
]. |
155 |
||
156 |
" |
|
4743 | 157 |
Base64Mapping := nil. |
3547
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
158 |
self initializeMappings |
1027 | 159 |
" |
4743 | 160 |
|
161 |
"Modified (comment): / 30-09-2018 / 15:39:44 / Claus Gittinger" |
|
162 |
! |
|
163 |
||
164 |
mapping |
|
165 |
^ Base64Mapping |
|
166 |
||
167 |
"Created: / 30-09-2018 / 15:30:33 / Claus Gittinger" |
|
168 |
! |
|
169 |
||
170 |
reverseMapping |
|
171 |
^ Base64ReverseMapping |
|
172 |
||
173 |
"Created: / 30-09-2018 / 15:30:40 / Claus Gittinger" |
|
174 |
! ! |
|
175 |
||
176 |
!Base64Coder class methodsFor:'decoding'! |
|
177 |
||
178 |
decode:aStringOrStream |
|
179 |
"because base64 decoding is used heavily in some protocols, |
|
180 |
a specially tuned version is provided here for the common case of decoding a string" |
|
181 |
||
4753 | 182 |
aStringOrStream isString ifTrue:[ |
183 |
^ self fastDecodeString:aStringOrStream |
|
184 |
]. |
|
4743 | 185 |
^ super decode:aStringOrStream. |
186 |
||
187 |
"Created: / 30-09-2018 / 14:14:51 / Claus Gittinger" |
|
4753 | 188 |
"Modified: / 30-09-2018 / 16:58:21 / Claus Gittinger" |
4743 | 189 |
! |
190 |
||
191 |
fastDecodeString:aString |
|
192 |
"because base64 decoding is used heavily in some protocols, |
|
193 |
a specially tuned version is provided here for the common case of decoding a string" |
|
194 |
||
195 |
^ self fastDecodeString:aString asString:false |
|
196 |
||
197 |
" |
|
198 |
(Base64Coder encode:'queen%27s%20gambit') asString => 'cXVlZW4lMjdzJTIwZ2FtYml0' |
|
199 |
||
200 |
(Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit' |
|
201 |
" |
|
202 |
||
203 |
"Created: / 30-09-2018 / 14:36:58 / Claus Gittinger" |
|
204 |
! |
|
205 |
||
206 |
fastDecodeString:aString asString:asStringBoolean |
|
207 |
"because base64 decoding is used heavily in some protocols, |
|
208 |
a specially tuned version is provided here for the common case of decoding a string" |
|
209 |
||
4753 | 210 |
|decoding revMapping| |
211 |
||
212 |
revMapping := self reverseMapping. |
|
213 |
revMapping isNil ifTrue:[ |
|
214 |
self initializeMappings. |
|
215 |
revMapping := self reverseMapping. |
|
216 |
]. |
|
4743 | 217 |
%{ |
4753 | 218 |
// overallocate by 3 |
219 |
# define N_QUICKBUFFER 512 |
|
220 |
if (__isString(aString) |
|
221 |
&& __isByteArray(revMapping)) { |
|
4754 | 222 |
unsigned char *_revMapping = __stringVal(revMapping); |
4753 | 223 |
int numInChars = __stringSize(aString); |
224 |
char *in = __stringVal(aString); |
|
225 |
unsigned char quickBuffer[N_QUICKBUFFER+3]; |
|
226 |
unsigned char *buffer = quickBuffer; |
|
227 |
int bufferSize = N_QUICKBUFFER; |
|
228 |
int outLen = 0; |
|
229 |
int charBuffer = 0; |
|
230 |
int nBitsOut = 0; |
|
231 |
int i; |
|
232 |
||
233 |
for (i=0; i<numInChars; i++) { |
|
234 |
char ch = in[i]; |
|
235 |
int bits = -1; |
|
236 |
||
237 |
if (ch <= 127) { |
|
238 |
bits = _revMapping[(ch-1) & 0x7F]; |
|
239 |
} |
|
240 |
||
241 |
if ((unsigned)bits <= 0x3F) { |
|
242 |
charBuffer = (charBuffer << 6) | bits; |
|
243 |
nBitsOut += 6; |
|
244 |
if (nBitsOut == 24) { |
|
245 |
if ((outLen + 3) > bufferSize) { |
|
246 |
if (buffer == quickBuffer) { |
|
247 |
// overallocate by 3 |
|
248 |
buffer = (unsigned char *)malloc(bufferSize*2+3); |
|
249 |
memcpy(buffer, quickBuffer, bufferSize); |
|
250 |
} else { |
|
251 |
buffer = (unsigned char *)realloc(buffer, bufferSize*2+3); |
|
252 |
} |
|
253 |
bufferSize = bufferSize * 2; |
|
4743 | 254 |
} |
4753 | 255 |
buffer[outLen] = (charBuffer >> 16) & 0xFF; |
256 |
buffer[outLen+1] = (charBuffer >> 8) & 0xFF; |
|
257 |
buffer[outLen+2] = (charBuffer) & 0xFF; |
|
258 |
outLen += 3; |
|
259 |
charBuffer = nBitsOut = 0; |
|
4743 | 260 |
} |
261 |
} else { |
|
4753 | 262 |
if ((unsigned)bits == 0x40) { |
263 |
// end mark |
|
264 |
// because of overallocation, there is no need to check for buffer-full condition here |
|
265 |
if (nBitsOut == 12) { |
|
266 |
// data has been padded to 12, skip 4 bits |
|
267 |
// one more byte coming |
|
268 |
charBuffer >>= 4; |
|
269 |
nBitsOut -= 4; |
|
270 |
buffer[outLen] = (charBuffer) & 0xFF; |
|
271 |
outLen += 1; |
|
272 |
} else if (nBitsOut == 18) { |
|
273 |
// data has been padded to 18, skip 2 bits |
|
274 |
charBuffer >>= 2; |
|
275 |
nBitsOut -= 2; |
|
276 |
buffer[outLen] = (charBuffer >> 8) & 0xFF; |
|
277 |
buffer[outLen+1] = (charBuffer) & 0xFF; |
|
278 |
outLen += 2; |
|
279 |
} |
|
280 |
} else { |
|
281 |
// ignore |
|
282 |
} |
|
283 |
} |
|
4743 | 284 |
} |
4753 | 285 |
|
286 |
if (asStringBoolean == true) { |
|
287 |
decoding = __MKSTRING_L(buffer, outLen); |
|
288 |
} else { |
|
289 |
decoding = __MKBYTEARRAY(buffer, outLen); |
|
290 |
} |
|
291 |
if (buffer != quickBuffer) { |
|
292 |
free(buffer); |
|
293 |
} |
|
294 |
RETURN(decoding); |
|
4743 | 295 |
} |
296 |
%}. |
|
4753 | 297 |
decoding := super decode:aString. |
298 |
asStringBoolean ifTrue:[ |
|
299 |
^ decoding asString |
|
300 |
]. |
|
301 |
^ decoding |
|
302 |
||
4743 | 303 |
" |
4753 | 304 |
(Base64Coder encode:'queen%27s%20gambit') => 'cXVlZW4lMjdzJTIwZ2FtYml0' |
4743 | 305 |
|
306 |
(Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit' |
|
307 |
(Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit' |
|
308 |
(Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0' asString:true) => 'queen%27s%20gambit' |
|
4753 | 309 |
|
310 |
(Base64Coder encode:'a') => 'YQ==' |
|
311 |
(Base64Coder fastDecodeString:'YQ==' asString:true) => 'a' |
|
312 |
||
313 |
(Base64Coder encode:'aa') => 'YWE=' |
|
314 |
(Base64Coder fastDecodeString:'YWE=' asString:true) => 'aa' |
|
315 |
||
316 |
|data encoded| |
|
317 |
data := ByteArray new:100000. |
|
318 |
encoded := Base64Coder encode:data. |
|
319 |
Time millisecondsToRun:[ |
|
320 |
10 timesRepeat:[ |
|
321 |
Base64Coder decode:encoded. |
|
322 |
] |
|
323 |
] |
|
324 |
||
325 |
|data encoded| |
|
326 |
data := ByteArray new:100000. |
|
327 |
encoded := Base64Coder encode:data. |
|
328 |
Time millisecondsToRun:[ |
|
329 |
10 timesRepeat:[ |
|
330 |
Base64Coder fastDecodeString:encoded. |
|
331 |
] |
|
332 |
] |
|
333 |
||
4743 | 334 |
" |
335 |
||
336 |
"Created: / 30-09-2018 / 14:35:05 / Claus Gittinger" |
|
4753 | 337 |
"Modified (comment): / 30-09-2018 / 16:57:52 / Claus Gittinger" |
1027 | 338 |
! ! |
339 |
||
1063 | 340 |
!Base64Coder methodsFor:'encoding'! |
341 |
||
3763 | 342 |
nextPutByte:aByte |
1027 | 343 |
"encode aByte on the output stream" |
344 |
||
4507 | 345 |
|b1 "{ Class: SmallInteger }" |
346 |
b2 "{ Class: SmallInteger }" |
|
347 |
b3 "{ Class: SmallInteger }" |
|
348 |
b4 "{ Class: SmallInteger }" |
|
349 |
bufferedBytes "{ Class: SmallInteger }" | |
|
1027 | 350 |
|
3763 | 351 |
buffer := (buffer bitShift:8) bitOr:aByte. |
1027 | 352 |
bits := bits + 8. |
353 |
bits == 24 ifTrue:[ |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
354 |
"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
|
355 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
356 |
stream cr. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
357 |
charCount := 0. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
358 |
]. |
4507 | 359 |
bufferedBytes := buffer. |
360 |
||
361 |
b4 := bufferedBytes bitAnd:16r3F. |
|
362 |
b3 := (bufferedBytes bitShift:-6) bitAnd:16r3F. |
|
363 |
b2 := (bufferedBytes bitShift:-12) bitAnd:16r3F. |
|
364 |
b1 := (bufferedBytes bitShift:-18) bitAnd:16r3F. |
|
1027 | 365 |
buffer := bits := 0. |
4507 | 366 |
|
4743 | 367 |
stream nextPut:(mapping at:b1+1); |
368 |
nextPut:(mapping at:b2+1); |
|
369 |
nextPut:(mapping at:b3+1); |
|
370 |
nextPut:(mapping at:b4+1). |
|
1027 | 371 |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
372 |
charCount := charCount + 4. |
1027 | 373 |
]. |
4507 | 374 |
|
375 |
"Modified: / 26-08-2017 / 12:35:17 / cg" |
|
4743 | 376 |
"Modified: / 30-09-2018 / 15:15:14 / Claus Gittinger" |
1027 | 377 |
! ! |
378 |
||
379 |
!Base64Coder methodsFor:'misc'! |
|
380 |
||
381 |
flush |
|
1390 | 382 |
"flush the remaining bits of buffer. |
383 |
The number of bits in buffer is not a multiple of 6, so we pad |
|
384 |
the buffer and signal that padding has been done via $= characters." |
|
1027 | 385 |
|
4891 | 386 |
|tempBuffer "{Class: SmallInteger}" |
387 |
b1 b2 b3 b4| |
|
1390 | 388 |
|
389 |
bits == 0 ifTrue:[ |
|
1027 | 390 |
"buffer is empty, nothing to do" |
391 |
^ self. |
|
1390 | 392 |
]. |
393 |
||
4891 | 394 |
tempBuffer := buffer. |
1390 | 395 |
bits == 8 ifTrue:[ |
4891 | 396 |
tempBuffer := tempBuffer bitShift:4. |
1027 | 397 |
b4 := b3 := 64. "pad with '=='" |
4891 | 398 |
b1 := (tempBuffer bitShift:-6) bitAnd:16r3F. |
399 |
b2 := tempBuffer bitAnd:16r3F. |
|
1390 | 400 |
] ifFalse:[ |
401 |
bits = 16 ifTrue:[ |
|
4891 | 402 |
tempBuffer := tempBuffer bitShift:2. |
1390 | 403 |
b4 := 64. "pad with '='" |
4891 | 404 |
b3 := tempBuffer bitAnd:16r3F. |
405 |
b2 := (tempBuffer bitShift:-6) bitAnd:16r3F. |
|
406 |
b1 := (tempBuffer bitShift:-12) bitAnd:16r3F. |
|
1390 | 407 |
] |
408 |
]. |
|
409 |
bits := buffer := 0. |
|
1027 | 410 |
|
1391 | 411 |
"RFC 2045 says: max 76 characters in one line" |
412 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
|
413 |
stream cr. |
|
414 |
charCount := 0. |
|
415 |
]. |
|
416 |
||
4743 | 417 |
stream nextPut:(mapping at:b1+1); |
418 |
nextPut:(mapping at:b2+1); |
|
419 |
nextPut:(mapping at:b3+1); |
|
420 |
nextPut:(mapping at:b4+1). |
|
1391 | 421 |
charCount := charCount + 4. |
4743 | 422 |
|
4891 | 423 |
"Modified: / 20-03-2019 / 21:22:39 / Claus Gittinger" |
1484 | 424 |
! ! |
425 |
||
426 |
!Base64Coder methodsFor:'private'! |
|
427 |
||
428 |
fillBuffer |
|
4743 | 429 |
"fill buffer with next 4 characters each representing 6 bits. |
430 |
Used when decoding." |
|
1484 | 431 |
|
4743 | 432 |
|b |
433 |
tempBuffer "{Class: SmallInteger}" |
|
434 |
_bits "{Class: SmallInteger}" | |
|
1484 | 435 |
|
436 |
tempBuffer := 0. |
|
4743 | 437 |
_bits := 0. |
438 |
||
3210 | 439 |
[ |
1484 | 440 |
"read next valid Base64 character, skip invalid characters" |
2137 | 441 |
b := 255. |
442 |
[b == 255] whileTrue:[ |
|
1484 | 443 |
b := stream next. |
444 |
b isNil ifTrue:[ "end of stream" |
|
1964 | 445 |
b := 64. "simulate end-mark" |
1484 | 446 |
] ifFalse:[ |
4743 | 447 |
b := reverseMapping at:b codePoint ifAbsent:255. |
1484 | 448 |
] |
2137 | 449 |
]. |
1964 | 450 |
|
1484 | 451 |
b == 64 ifTrue:[ |
452 |
"got $=, end of Base64 string has been reached" |
|
453 |
atEnd := true. |
|
4743 | 454 |
_bits == 12 ifTrue:[ |
1484 | 455 |
"data has been padded to 12, skip 4 bits" |
4743 | 456 |
tempBuffer := tempBuffer bitShift:-4. |
457 |
_bits := _bits - 4. |
|
458 |
] ifFalse:[_bits == 18 ifTrue:[ |
|
1484 | 459 |
"data has been padded to 18, skip 2 bits" |
4743 | 460 |
tempBuffer := tempBuffer bitShift:-2. |
461 |
_bits := _bits - 2. |
|
1484 | 462 |
]]. |
463 |
] ifFalse:[ |
|
464 |
"got valid Base64 character, append to buffer" |
|
465 |
tempBuffer := (tempBuffer bitShift:6) bitOr:b. |
|
4743 | 466 |
_bits := _bits + 6. |
1484 | 467 |
]. |
4743 | 468 |
(_bits == 24 or:[atEnd]) ifTrue:[ |
469 |
bits := _bits. |
|
2137 | 470 |
buffer := tempBuffer. |
471 |
^ self. |
|
472 |
]. |
|
3210 | 473 |
] loop. |
4743 | 474 |
|
475 |
"Modified: / 30-09-2018 / 15:16:19 / Claus Gittinger" |
|
1027 | 476 |
! ! |
477 |
||
478 |
!Base64Coder class methodsFor:'documentation'! |
|
479 |
||
480 |
version |
|
3763 | 481 |
^ '$Header$' |
2325 | 482 |
! |
483 |
||
484 |
version_CVS |
|
3763 | 485 |
^ '$Header$' |
1027 | 486 |
! ! |
1111 | 487 |