author | Claus Gittinger <cg@exept.de> |
Sun, 30 Sep 2018 15:55:25 +0200 | |
changeset 4743 | 6bdaef8ec48a |
parent 4520 | d2ea4e714e1f |
child 4753 | 5b849ae3a1a1 |
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 |
||
182 |
^ super decode:aStringOrStream. |
|
183 |
||
184 |
"Created: / 30-09-2018 / 14:14:51 / Claus Gittinger" |
|
185 |
! |
|
186 |
||
187 |
fastDecodeString:aString |
|
188 |
"because base64 decoding is used heavily in some protocols, |
|
189 |
a specially tuned version is provided here for the common case of decoding a string" |
|
190 |
||
191 |
^ self fastDecodeString:aString asString:false |
|
192 |
||
193 |
" |
|
194 |
(Base64Coder encode:'queen%27s%20gambit') asString => 'cXVlZW4lMjdzJTIwZ2FtYml0' |
|
195 |
||
196 |
(Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit' |
|
197 |
" |
|
198 |
||
199 |
"Created: / 30-09-2018 / 14:36:58 / Claus Gittinger" |
|
200 |
! |
|
201 |
||
202 |
fastDecodeString:aString asString:asStringBoolean |
|
203 |
"because base64 decoding is used heavily in some protocols, |
|
204 |
a specially tuned version is provided here for the common case of decoding a string" |
|
205 |
||
206 |
|decoding| |
|
207 |
||
208 |
%{ |
|
209 |
char quickBuffer[512]; |
|
210 |
char *buffer = quickBuffer; |
|
211 |
int bufferSize = sizeof(quickBuffer); |
|
212 |
int outLen = 0; |
|
213 |
int charBuffer = 0; |
|
214 |
int _bits = 0; |
|
215 |
int numChars = __stringSize(aString); |
|
216 |
char *in = __stringVal(aString); |
|
217 |
int i; |
|
218 |
||
219 |
for (i=0; i<numChars; i++) { |
|
220 |
char ch = in[i]; |
|
221 |
int bits = -1; |
|
222 |
||
223 |
if (((unsigned)(ch - 'A')) <= 25) { |
|
224 |
bits = ((unsigned)(ch - 'A')); goto ok; |
|
225 |
} else if (((unsigned)(ch - 'a')) <= 25) { |
|
226 |
bits = ((unsigned)(ch - 'a')) + 26; goto ok; |
|
227 |
} else if (((unsigned)(ch - '0')) <= 9) { |
|
228 |
bits = ((unsigned)(ch - '0')) + 34; goto ok; |
|
229 |
} else if (ch == '+') { |
|
230 |
bits = 0x3E; goto ok; |
|
231 |
} else if (ch == '/') { |
|
232 |
bits = 0x3F; goto ok; |
|
233 |
} |
|
234 |
if (bits >= 0) { |
|
235 |
ok: |
|
236 |
charBuffer = (charBuffer << 6) | bits; |
|
237 |
_bits += 6; |
|
238 |
if (_bits == 24) { |
|
239 |
if ((outLen + 3) > bufferSize) { |
|
240 |
if (buffer == quickBuffer) { |
|
241 |
buffer = (char *)malloc(bufferSize*2); |
|
242 |
memcpy(buffer, quickBuffer, bufferSize); |
|
243 |
} else { |
|
244 |
buffer = (char *)realloc(buffer, bufferSize*2); |
|
245 |
} |
|
246 |
bufferSize = bufferSize * 2; |
|
247 |
} |
|
248 |
buffer[outLen] = (charBuffer >> 16) & 0xFF; |
|
249 |
buffer[outLen+1] = (charBuffer >> 8) & 0xFF; |
|
250 |
buffer[outLen+2] = (charBuffer) & 0xFF; |
|
251 |
charBuffer = 0; |
|
252 |
outLen += 3; |
|
253 |
_bits = 0; |
|
254 |
} |
|
255 |
} else { |
|
256 |
if (ch == '=') { |
|
257 |
// end mark |
|
258 |
if (_bits == 12) { |
|
259 |
// data has been padded to 12, skip 4 bits |
|
260 |
charBuffer >>= 4; |
|
261 |
_bits -= 4; |
|
262 |
} else if (_bits == 18) { |
|
263 |
// data has been padded to 18, skip 2 bits |
|
264 |
charBuffer >>= 2; |
|
265 |
_bits -= 2; |
|
266 |
} |
|
267 |
} else { |
|
268 |
// ignore |
|
269 |
} |
|
270 |
} |
|
271 |
} |
|
272 |
||
273 |
if (_bits != 0) { |
|
274 |
} |
|
275 |
||
276 |
if (asStringBoolean == true) { |
|
277 |
decoding = __MKSTRING_L(buffer, outLen); |
|
278 |
} else { |
|
279 |
decoding = __MKBYTEARRAY(buffer, outLen); |
|
280 |
} |
|
281 |
if (buffer != quickBuffer) { |
|
282 |
free(buffer); |
|
283 |
} |
|
284 |
%}. |
|
285 |
^ decoding. |
|
286 |
||
287 |
" |
|
288 |
(Base64Coder encode:'queen%27s%20gambit') asString => 'cXVlZW4lMjdzJTIwZ2FtYml0' |
|
289 |
||
290 |
(Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit' |
|
291 |
(Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit' |
|
292 |
(Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0' asString:true) => 'queen%27s%20gambit' |
|
293 |
" |
|
294 |
||
295 |
"Created: / 30-09-2018 / 14:35:05 / Claus Gittinger" |
|
1027 | 296 |
! ! |
297 |
||
1063 | 298 |
!Base64Coder methodsFor:'encoding'! |
299 |
||
3763 | 300 |
nextPutByte:aByte |
1027 | 301 |
"encode aByte on the output stream" |
302 |
||
4507 | 303 |
|b1 "{ Class: SmallInteger }" |
304 |
b2 "{ Class: SmallInteger }" |
|
305 |
b3 "{ Class: SmallInteger }" |
|
306 |
b4 "{ Class: SmallInteger }" |
|
307 |
bufferedBytes "{ Class: SmallInteger }" | |
|
1027 | 308 |
|
3763 | 309 |
buffer := (buffer bitShift:8) bitOr:aByte. |
1027 | 310 |
bits := bits + 8. |
311 |
bits == 24 ifTrue:[ |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
312 |
"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
|
313 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
314 |
stream cr. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
315 |
charCount := 0. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
316 |
]. |
4507 | 317 |
bufferedBytes := buffer. |
318 |
||
319 |
b4 := bufferedBytes bitAnd:16r3F. |
|
320 |
b3 := (bufferedBytes bitShift:-6) bitAnd:16r3F. |
|
321 |
b2 := (bufferedBytes bitShift:-12) bitAnd:16r3F. |
|
322 |
b1 := (bufferedBytes bitShift:-18) bitAnd:16r3F. |
|
1027 | 323 |
buffer := bits := 0. |
4507 | 324 |
|
4743 | 325 |
stream nextPut:(mapping at:b1+1); |
326 |
nextPut:(mapping at:b2+1); |
|
327 |
nextPut:(mapping at:b3+1); |
|
328 |
nextPut:(mapping at:b4+1). |
|
1027 | 329 |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
330 |
charCount := charCount + 4. |
1027 | 331 |
]. |
4507 | 332 |
|
333 |
"Modified: / 26-08-2017 / 12:35:17 / cg" |
|
4743 | 334 |
"Modified: / 30-09-2018 / 15:15:14 / Claus Gittinger" |
1027 | 335 |
! ! |
336 |
||
337 |
!Base64Coder methodsFor:'misc'! |
|
338 |
||
339 |
flush |
|
1390 | 340 |
"flush the remaining bits of buffer. |
341 |
The number of bits in buffer is not a multiple of 6, so we pad |
|
342 |
the buffer and signal that padding has been done via $= characters." |
|
1027 | 343 |
|
1390 | 344 |
|b1 b2 b3 b4| |
345 |
||
346 |
bits == 0 ifTrue:[ |
|
1027 | 347 |
"buffer is empty, nothing to do" |
348 |
^ self. |
|
1390 | 349 |
]. |
350 |
||
351 |
bits == 8 ifTrue:[ |
|
1027 | 352 |
buffer := buffer bitShift:4. |
353 |
b4 := b3 := 64. "pad with '=='" |
|
354 |
b1 := (buffer bitShift:-6) bitAnd:16r3F. |
|
355 |
b2 := buffer bitAnd:16r3F. |
|
1390 | 356 |
] ifFalse:[ |
357 |
bits = 16 ifTrue:[ |
|
358 |
buffer := buffer bitShift:2. |
|
359 |
b4 := 64. "pad with '='" |
|
360 |
b3 := buffer bitAnd:16r3F. |
|
361 |
b2 := (buffer bitShift:-6) bitAnd:16r3F. |
|
362 |
b1 := (buffer bitShift:-12) bitAnd:16r3F. |
|
363 |
] |
|
364 |
]. |
|
365 |
bits := buffer := 0. |
|
1027 | 366 |
|
1391 | 367 |
"RFC 2045 says: max 76 characters in one line" |
368 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
|
369 |
stream cr. |
|
370 |
charCount := 0. |
|
371 |
]. |
|
372 |
||
4743 | 373 |
stream nextPut:(mapping at:b1+1); |
374 |
nextPut:(mapping at:b2+1); |
|
375 |
nextPut:(mapping at:b3+1); |
|
376 |
nextPut:(mapping at:b4+1). |
|
1391 | 377 |
charCount := charCount + 4. |
4743 | 378 |
|
379 |
"Modified: / 30-09-2018 / 15:15:52 / Claus Gittinger" |
|
1484 | 380 |
! ! |
381 |
||
382 |
!Base64Coder methodsFor:'private'! |
|
383 |
||
384 |
fillBuffer |
|
4743 | 385 |
"fill buffer with next 4 characters each representing 6 bits. |
386 |
Used when decoding." |
|
1484 | 387 |
|
4743 | 388 |
|b |
389 |
tempBuffer "{Class: SmallInteger}" |
|
390 |
_bits "{Class: SmallInteger}" | |
|
1484 | 391 |
|
392 |
tempBuffer := 0. |
|
4743 | 393 |
_bits := 0. |
394 |
||
3210 | 395 |
[ |
1484 | 396 |
"read next valid Base64 character, skip invalid characters" |
2137 | 397 |
b := 255. |
398 |
[b == 255] whileTrue:[ |
|
1484 | 399 |
b := stream next. |
400 |
b isNil ifTrue:[ "end of stream" |
|
1964 | 401 |
b := 64. "simulate end-mark" |
1484 | 402 |
] ifFalse:[ |
4743 | 403 |
b := reverseMapping at:b codePoint ifAbsent:255. |
1484 | 404 |
] |
2137 | 405 |
]. |
1964 | 406 |
|
1484 | 407 |
b == 64 ifTrue:[ |
408 |
"got $=, end of Base64 string has been reached" |
|
409 |
atEnd := true. |
|
4743 | 410 |
_bits == 12 ifTrue:[ |
1484 | 411 |
"data has been padded to 12, skip 4 bits" |
4743 | 412 |
tempBuffer := tempBuffer bitShift:-4. |
413 |
_bits := _bits - 4. |
|
414 |
] ifFalse:[_bits == 18 ifTrue:[ |
|
1484 | 415 |
"data has been padded to 18, skip 2 bits" |
4743 | 416 |
tempBuffer := tempBuffer bitShift:-2. |
417 |
_bits := _bits - 2. |
|
1484 | 418 |
]]. |
419 |
] ifFalse:[ |
|
420 |
"got valid Base64 character, append to buffer" |
|
421 |
tempBuffer := (tempBuffer bitShift:6) bitOr:b. |
|
4743 | 422 |
_bits := _bits + 6. |
1484 | 423 |
]. |
4743 | 424 |
(_bits == 24 or:[atEnd]) ifTrue:[ |
425 |
bits := _bits. |
|
2137 | 426 |
buffer := tempBuffer. |
427 |
^ self. |
|
428 |
]. |
|
3210 | 429 |
] loop. |
4743 | 430 |
|
431 |
"Modified: / 30-09-2018 / 15:16:19 / Claus Gittinger" |
|
1027 | 432 |
! ! |
433 |
||
434 |
!Base64Coder class methodsFor:'documentation'! |
|
435 |
||
436 |
version |
|
3763 | 437 |
^ '$Header$' |
2325 | 438 |
! |
439 |
||
440 |
version_CVS |
|
3763 | 441 |
^ '$Header$' |
1027 | 442 |
! ! |
1111 | 443 |