author | Claus Gittinger <cg@exept.de> |
Wed, 18 Mar 2020 10:11:21 +0100 | |
changeset 5468 | e9da15c1a36c |
parent 4900 | 0e400da67727 |
permissions | -rw-r--r-- |
4743 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1308 | 3 |
" |
4 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
4900 | 5 |
All Rights Reserved |
1308 | 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 |
|
4900 | 30 |
All Rights Reserved |
1308 | 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: |
4900 | 46 |
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' |
1027 | 47 |
|
4900 | 48 |
Notice: for URLs, a slightly different encoding is used, |
4894 | 49 |
where instead of plus and slash, minus and underline are generated (see Base64UrlCoder). |
50 |
||
51 |
The main entry point API is: |
|
4900 | 52 |
Base64Coder encode:aStringOrBytes |
4279 | 53 |
and |
4900 | 54 |
Base64Coder decode:aString |
4279 | 55 |
|
4900 | 56 |
Typically, binary data is encoded as base64, |
4894 | 57 |
so the natural return value is a byte array. |
58 |
||
4279 | 59 |
If the decoder should return a string, use |
4900 | 60 |
Base64Coder decodeAsString:aString. |
4894 | 61 |
otherwise, a bytearray is returned from the decode: method. |
4900 | 62 |
|
1027 | 63 |
[author:] |
4900 | 64 |
Stefan Vogel |
1027 | 65 |
|
66 |
[see also:] |
|
4900 | 67 |
RFC https://tools.ietf.org/html/rfc4648 |
68 |
||
1027 | 69 |
[instance variables:] |
70 |
||
71 |
[class variables:] |
|
4900 | 72 |
Base64Mapping String Mapping from bytes (with 6 valid bits) |
73 |
to Base64 characters |
|
74 |
Base64ReverseMapping Array Mapping from Base64 characters to 6-bit-Bytes |
|
1027 | 75 |
" |
76 |
! |
|
77 |
||
78 |
examples |
|
79 |
" |
|
4900 | 80 |
[exBegin] |
3478 | 81 |
(Base64Coder encode:'queen%27s%20gambit') asString = 'cXVlZW4lMjdzJTIwZ2FtYml0' |
4900 | 82 |
[exEnd] |
3478 | 83 |
|
4900 | 84 |
[exBegin] |
3478 | 85 |
(Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString = 'queen%27s%20gambit' |
4900 | 86 |
[exEnd] |
2224 | 87 |
|
4900 | 88 |
[exBegin] |
1122 | 89 |
|data1 text data2| |
90 |
||
91 |
data1 := #[0 1 16r7F 16r80 16r81 16rFE 16rFF]. |
|
1639 | 92 |
text := Base64Coder encode:data1. |
93 |
data2 := Base64Coder decode:text. |
|
4520 | 94 |
data2 |
4900 | 95 |
[exEnd] |
1027 | 96 |
|
4900 | 97 |
[exBegin] |
1027 | 98 |
|coder| |
99 |
||
100 |
coder := Base64Coder on:'' writeStream. |
|
101 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]. |
|
102 |
coder flush. |
|
103 |
coder contents inspect. |
|
104 |
coder reset. |
|
105 |
coder nextPut:254. |
|
106 |
coder contents inspect. |
|
4900 | 107 |
[exEnd] |
1027 | 108 |
|
4900 | 109 |
[exBegin] |
1391 | 110 |
|coder decoder| |
1027 | 111 |
|
112 |
coder := Base64Coder on:'' writeStream. |
|
113 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]. |
|
114 |
coder flush. |
|
115 |
||
1391 | 116 |
decoder := Base64Coder on:(coder contents readStream). |
117 |
[decoder atEnd] whileFalse:[ |
|
118 |
Transcript show:decoder next |
|
1027 | 119 |
]. |
120 |
Transcript cr. |
|
4900 | 121 |
[exEnd] |
122 |
[exBegin] |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
123 |
|coder| |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
124 |
|
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
125 |
coder := Base64Coder on:'' writeStream. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
126 |
coder nextPutAll:(0 to:200) asByteArray. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
127 |
coder flush. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
128 |
|
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
129 |
Transcript showCR:(coder contents). |
4900 | 130 |
[exEnd] |
131 |
[exBegin] |
|
4894 | 132 |
|bytes| |
133 |
||
134 |
bytes := ByteArray new:100000. |
|
4900 | 135 |
Time millisecondsToRun:[ |
4894 | 136 |
100 timesRepeat:[ |
4900 | 137 |
Base64Coder encode:bytes. |
138 |
]. |
|
139 |
]. |
|
140 |
[exEnd] |
|
141 |
[exBegin] |
|
4743 | 142 |
|bytes encoded decoded| |
143 |
||
144 |
bytes := #[0 0 0] copy. |
|
145 |
0 to:255 do:[:b1 | |
|
4900 | 146 |
Transcript showCR:b1. |
147 |
bytes at:1 put:b1. |
|
4743 | 148 |
0 to:255 do:[:b2 | |
4900 | 149 |
bytes at:2 put:b2. |
150 |
0 to:255 do:[:b3 | |
|
151 |
bytes at:3 put:b3. |
|
152 |
encoded := Base64Coder encode:bytes. |
|
153 |
decoded := Base64Coder decode:encoded. |
|
154 |
self assert:(decoded = bytes). |
|
155 |
] |
|
4743 | 156 |
] |
157 |
]. |
|
4900 | 158 |
[exEnd] |
1027 | 159 |
" |
160 |
! ! |
|
161 |
||
162 |
!Base64Coder class methodsFor:'initialization'! |
|
163 |
||
3547
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
164 |
initializeMappings |
1027 | 165 |
"initialize class variables" |
166 |
||
3547
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
167 |
Base64Mapping isNil ifTrue:[ |
4900 | 168 |
"65 characters representing the 6-bit values from 0-63 and one pad character" |
169 |
Base64Mapping := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='. |
|
170 |
Base64ReverseMapping := self reverseMappingFor:Base64Mapping. |
|
1027 | 171 |
]. |
172 |
||
173 |
" |
|
4743 | 174 |
Base64Mapping := nil. |
3547
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
175 |
self initializeMappings |
1027 | 176 |
" |
4743 | 177 |
|
178 |
"Modified (comment): / 30-09-2018 / 15:39:44 / Claus Gittinger" |
|
179 |
! |
|
180 |
||
181 |
mapping |
|
182 |
^ Base64Mapping |
|
183 |
||
184 |
"Created: / 30-09-2018 / 15:30:33 / Claus Gittinger" |
|
185 |
! |
|
186 |
||
187 |
reverseMapping |
|
188 |
^ Base64ReverseMapping |
|
189 |
||
190 |
"Created: / 30-09-2018 / 15:30:40 / Claus Gittinger" |
|
191 |
! ! |
|
192 |
||
193 |
!Base64Coder class methodsFor:'decoding'! |
|
194 |
||
195 |
decode:aStringOrStream |
|
196 |
"because base64 decoding is used heavily in some protocols, |
|
4894 | 197 |
a specially tuned version is provided here |
198 |
for the common case of decoding a string. |
|
199 |
This returns a byteArray." |
|
4743 | 200 |
|
4753 | 201 |
aStringOrStream isString ifTrue:[ |
4900 | 202 |
^ self fastDecodeString:aStringOrStream asString:false |
203 |
]. |
|
4743 | 204 |
^ super decode:aStringOrStream. |
205 |
||
206 |
"Created: / 30-09-2018 / 14:14:51 / Claus Gittinger" |
|
4894 | 207 |
"Modified: / 21-03-2019 / 22:37:27 / Claus Gittinger" |
208 |
! |
|
209 |
||
210 |
decodeAsString:encodedString |
|
211 |
"because base64 decoding is used heavily in some protocols, |
|
212 |
a specially tuned version is provided here |
|
213 |
for the common case of decoding a string. |
|
214 |
This returns a string." |
|
215 |
||
216 |
encodedString isString ifTrue:[ |
|
4900 | 217 |
^ self fastDecodeString:encodedString asString:true |
218 |
]. |
|
4894 | 219 |
^ super decodeAsString:encodedString. |
220 |
||
221 |
"Created: / 21-03-2019 / 20:43:47 / Claus Gittinger" |
|
222 |
"Modified: / 21-03-2019 / 22:10:24 / Claus Gittinger" |
|
223 |
! |
|
224 |
||
225 |
encode:aStringOrStream |
|
226 |
"because base64 encoding is used heavily in some protocols, |
|
4900 | 227 |
a specially tuned version is provided here |
4894 | 228 |
for the common case of encoding a string. |
229 |
A string is generated with an inserted |
|
230 |
newline after every 76 characters (see RFC 2045)" |
|
231 |
||
232 |
(aStringOrStream isString or:[aStringOrStream isByteArray]) ifTrue:[ |
|
4900 | 233 |
^ self fastEncode:aStringOrStream asString:true lineLimit:(self lineLimit) |
234 |
]. |
|
4894 | 235 |
^ super encode:aStringOrStream. |
236 |
||
237 |
"Created: / 21-03-2019 / 20:44:35 / Claus Gittinger" |
|
238 |
"Modified: / 21-03-2019 / 22:33:37 / Claus Gittinger" |
|
4743 | 239 |
! |
240 |
||
241 |
fastDecodeString:aString |
|
242 |
"because base64 decoding is used heavily in some protocols, |
|
4894 | 243 |
a specially tuned version is provided here |
244 |
for the common case of decoding a string. |
|
245 |
This returns a byteArray" |
|
4743 | 246 |
|
247 |
^ self fastDecodeString:aString asString:false |
|
248 |
||
249 |
" |
|
4894 | 250 |
(Base64Coder encode:'queen%27s%20gambit') => 'cXVlZW4lMjdzJTIwZ2FtYml0' |
251 |
||
252 |
(Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') => #[113 117 101 101 110 37 50 55 115 37 50 48 103 97 109 98 105 116] |
|
253 |
(Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit' |
|
4900 | 254 |
(Base64Coder decodeAsString:'cXVlZW4lMjdzJTIwZ2FtYml0') => 'queen%27s%20gambit' |
4743 | 255 |
|
256 |
(Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit' |
|
257 |
" |
|
258 |
||
259 |
"Created: / 30-09-2018 / 14:36:58 / Claus Gittinger" |
|
4894 | 260 |
"Modified (comment): / 21-03-2019 / 22:12:07 / Claus Gittinger" |
4743 | 261 |
! |
262 |
||
263 |
fastDecodeString:aString asString:asStringBoolean |
|
264 |
"because base64 decoding is used heavily in some protocols, |
|
4900 | 265 |
a specially tuned version is provided here |
4894 | 266 |
for the common case of decoding a string. |
267 |
If the argument is true, a string is returned; |
|
268 |
otherwise, a bytearray" |
|
4743 | 269 |
|
4753 | 270 |
|decoding revMapping| |
271 |
||
272 |
revMapping := self reverseMapping. |
|
273 |
revMapping isNil ifTrue:[ |
|
4900 | 274 |
self initializeMappings. |
275 |
revMapping := self reverseMapping. |
|
276 |
]. |
|
4743 | 277 |
%{ |
4753 | 278 |
// overallocate by 3 |
279 |
# define N_QUICKBUFFER 512 |
|
4894 | 280 |
if (__isStringLike(aString) |
4753 | 281 |
&& __isByteArray(revMapping)) { |
4900 | 282 |
unsigned char *_revMapping = __stringVal(revMapping); |
283 |
int numInChars = __stringSize(aString); |
|
284 |
char *in = __stringVal(aString); |
|
285 |
unsigned char quickBuffer[N_QUICKBUFFER+3]; |
|
286 |
unsigned char *buffer = quickBuffer; |
|
287 |
int bufferSize = N_QUICKBUFFER; |
|
288 |
int outLen = 0; |
|
289 |
int charBuffer = 0; |
|
290 |
int nBitsOut = 0; |
|
291 |
int i; |
|
4753 | 292 |
|
4900 | 293 |
for (i=0; i<numInChars; i++) { |
294 |
char ch = in[i]; |
|
295 |
int bits = -1; |
|
4753 | 296 |
|
4900 | 297 |
if (ch <= 127) { |
298 |
bits = _revMapping[(ch-1) & 0x7F]; |
|
299 |
} |
|
4753 | 300 |
|
4900 | 301 |
if ((unsigned)bits <= 0x3F) { |
302 |
charBuffer = (charBuffer << 6) | bits; |
|
303 |
nBitsOut += 6; |
|
304 |
if (nBitsOut == 24) { |
|
305 |
if ((outLen + 3) > bufferSize) { |
|
306 |
if (buffer == quickBuffer) { |
|
307 |
// overallocate by 3 |
|
308 |
buffer = (unsigned char *)malloc(bufferSize*2+3); |
|
309 |
memcpy(buffer, quickBuffer, bufferSize); |
|
310 |
} else { |
|
311 |
buffer = (unsigned char *)realloc(buffer, bufferSize*2+3); |
|
312 |
} |
|
313 |
bufferSize = bufferSize * 2; |
|
314 |
} |
|
315 |
buffer[outLen] = (charBuffer >> 16) & 0xFF; |
|
316 |
buffer[outLen+1] = (charBuffer >> 8) & 0xFF; |
|
317 |
buffer[outLen+2] = (charBuffer) & 0xFF; |
|
318 |
outLen += 3; |
|
319 |
charBuffer = nBitsOut = 0; |
|
320 |
} |
|
321 |
} else { |
|
322 |
if ((unsigned)bits == 0x40) { |
|
323 |
// end mark |
|
324 |
// because of overallocation, there is no need to check for buffer-full condition here |
|
325 |
if (nBitsOut == 12) { |
|
326 |
// data has been padded to 12, skip 4 bits |
|
327 |
// one more byte coming |
|
328 |
charBuffer >>= 4; |
|
329 |
nBitsOut -= 4; |
|
330 |
buffer[outLen] = (charBuffer) & 0xFF; |
|
331 |
outLen += 1; |
|
332 |
} else if (nBitsOut == 18) { |
|
333 |
// data has been padded to 18, skip 2 bits |
|
334 |
charBuffer >>= 2; |
|
335 |
nBitsOut -= 2; |
|
336 |
buffer[outLen] = (charBuffer >> 8) & 0xFF; |
|
337 |
buffer[outLen+1] = (charBuffer) & 0xFF; |
|
338 |
outLen += 2; |
|
339 |
} |
|
340 |
} else { |
|
341 |
// ignore |
|
342 |
} |
|
343 |
} |
|
344 |
} |
|
4753 | 345 |
|
4900 | 346 |
if (asStringBoolean == true) { |
347 |
decoding = __MKSTRING_L(buffer, outLen); |
|
348 |
} else { |
|
349 |
decoding = __MKBYTEARRAY(buffer, outLen); |
|
350 |
} |
|
351 |
if (buffer != quickBuffer) { |
|
352 |
free(buffer); |
|
353 |
} |
|
354 |
RETURN(decoding); |
|
355 |
} |
|
4743 | 356 |
%}. |
4753 | 357 |
decoding := super decode:aString. |
358 |
asStringBoolean ifTrue:[ |
|
4900 | 359 |
^ decoding asString |
360 |
]. |
|
4753 | 361 |
^ decoding |
4900 | 362 |
|
4743 | 363 |
" |
4753 | 364 |
(Base64Coder encode:'queen%27s%20gambit') => 'cXVlZW4lMjdzJTIwZ2FtYml0' |
4743 | 365 |
|
366 |
(Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit' |
|
367 |
(Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit' |
|
368 |
(Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0' asString:true) => 'queen%27s%20gambit' |
|
4753 | 369 |
|
370 |
(Base64Coder encode:'a') => 'YQ==' |
|
371 |
(Base64Coder fastDecodeString:'YQ==' asString:true) => 'a' |
|
372 |
||
373 |
(Base64Coder encode:'aa') => 'YWE=' |
|
374 |
(Base64Coder fastDecodeString:'YWE=' asString:true) => 'aa' |
|
375 |
||
376 |
|data encoded| |
|
377 |
data := ByteArray new:100000. |
|
378 |
encoded := Base64Coder encode:data. |
|
379 |
Time millisecondsToRun:[ |
|
4900 | 380 |
10 timesRepeat:[ |
381 |
Base64Coder decode:encoded. |
|
382 |
] |
|
383 |
] |
|
384 |
||
4753 | 385 |
|data encoded| |
386 |
data := ByteArray new:100000. |
|
387 |
encoded := Base64Coder encode:data. |
|
388 |
Time millisecondsToRun:[ |
|
4900 | 389 |
10 timesRepeat:[ |
390 |
Base64Coder fastDecodeString:encoded. |
|
391 |
] |
|
4753 | 392 |
] |
393 |
||
4743 | 394 |
" |
395 |
||
396 |
"Created: / 30-09-2018 / 14:35:05 / Claus Gittinger" |
|
4894 | 397 |
"Modified: / 21-03-2019 / 22:34:49 / Claus Gittinger" |
398 |
! |
|
399 |
||
4900 | 400 |
fastEncode:aStringOrByteArray |
4894 | 401 |
"because base64 encoding is used heavily in some protocols, |
402 |
a specially tuned version is provided here |
|
403 |
for the common case of encoding a string or bytearray. |
|
404 |
A string is generated with an inserted |
|
405 |
newline after every 76 characters (see RFC 2045)" |
|
406 |
||
407 |
^ self fastEncode:aStringOrByteArray asString:true lineLimit:(self lineLimit) |
|
408 |
||
409 |
"Created: / 21-03-2019 / 20:43:07 / Claus Gittinger" |
|
410 |
"Modified (comment): / 21-03-2019 / 22:14:45 / Claus Gittinger" |
|
411 |
! |
|
412 |
||
413 |
fastEncode:aStringOrByteArray asString:asStringBoolean |
|
414 |
"because base64 encoding is used heavily in some protocols, |
|
415 |
a specially tuned version is provided here, |
|
416 |
for the common case of encoding a string or bytearray. |
|
417 |
If asStringBoolean is true, a string is generated; otherwise, a bytearray is returned. |
|
418 |
A newline is inserted after every 76 characters (see RFC 2045)" |
|
419 |
||
420 |
^ self fastEncode:aStringOrByteArray asString:asStringBoolean lineLimit:(self lineLimit) |
|
421 |
||
422 |
"Created: / 01-10-2018 / 09:19:35 / Claus Gittinger" |
|
423 |
"Modified (comment): / 21-03-2019 / 22:14:17 / Claus Gittinger" |
|
424 |
! |
|
425 |
||
426 |
fastEncode:aStringOrByteArray asString:asStringBoolean lineLimit:lineLimitOrNil |
|
427 |
"because base64 encoding is used heavily in some protocols, |
|
4900 | 428 |
a specially tuned version is provided here |
4894 | 429 |
for the common case of encoding a string. |
430 |
If asStringBoolean is true, a string is generated; otherwise, a bytearray is returned. |
|
431 |
If lineLimitOrNil is non-nil, a newline is inserted after every such number of characters" |
|
432 |
||
433 |
|encoding mapping| |
|
434 |
||
435 |
mapping := self mapping. |
|
436 |
mapping isNil ifTrue:[ |
|
4900 | 437 |
self initializeMappings. |
438 |
mapping := self mapping. |
|
439 |
]. |
|
4894 | 440 |
%{ |
441 |
// overallocate by 5 |
|
442 |
# define N_QUICKBUFFER 512 |
|
443 |
int argIsString = __isStringLike(aStringOrByteArray); |
|
4900 | 444 |
|
4894 | 445 |
if ((argIsString || __isByteArray(aStringOrByteArray)) |
446 |
&& __isStringLike(mapping)) { |
|
4900 | 447 |
unsigned char *__mapping = __stringVal(mapping); |
448 |
int numInChars; |
|
449 |
unsigned char *in; |
|
450 |
unsigned char quickBuffer[N_QUICKBUFFER+5]; |
|
451 |
unsigned char *buffer = quickBuffer; |
|
452 |
int bufferSize = N_QUICKBUFFER; |
|
453 |
int outLen = 0; |
|
454 |
int nBitsOut = 0; |
|
455 |
int i; |
|
456 |
int numInCharsMinus3; |
|
457 |
unsigned int lineLimit = ~0; |
|
458 |
int lineLength = 0; |
|
459 |
int restLength = 0; |
|
4894 | 460 |
|
4900 | 461 |
if (__isSmallInteger(lineLimitOrNil)) { |
462 |
lineLimit = __intVal(lineLimitOrNil); |
|
463 |
} |
|
4894 | 464 |
|
4900 | 465 |
if (argIsString) { |
466 |
numInChars = __stringSize(aStringOrByteArray); |
|
467 |
in = __stringVal(aStringOrByteArray); |
|
468 |
} else { |
|
469 |
numInChars = __byteArraySize(aStringOrByteArray); |
|
470 |
in = __byteArrayVal(aStringOrByteArray); |
|
471 |
} |
|
472 |
// fprintf(stderr, "%d\n", numInChars); |
|
4894 | 473 |
|
4900 | 474 |
lineLength = 0; |
475 |
numInCharsMinus3 = numInChars-3; |
|
476 |
||
477 |
for (i=0; i<=numInCharsMinus3; i+=3) { |
|
478 |
int charBuffer; |
|
4894 | 479 |
|
4900 | 480 |
if (lineLength >= lineLimit) { |
481 |
buffer[outLen++] = '\n'; |
|
482 |
lineLength = 0; |
|
483 |
} |
|
484 |
||
485 |
charBuffer = (in[i]) << 16; |
|
486 |
charBuffer |= ((in[i+1]) << 8); |
|
487 |
charBuffer |= (in[i+2]); |
|
4894 | 488 |
|
4900 | 489 |
if ((outLen + 5) > bufferSize) { |
490 |
if (buffer == quickBuffer) { |
|
491 |
// overallocate by 5 |
|
492 |
buffer = (unsigned char *)malloc(bufferSize*2+5); |
|
493 |
memcpy(buffer, quickBuffer, bufferSize); |
|
494 |
} else { |
|
495 |
buffer = (unsigned char *)realloc(buffer, bufferSize*2+5); |
|
496 |
} |
|
497 |
bufferSize = bufferSize * 2; |
|
498 |
} |
|
4894 | 499 |
#ifdef __LSBFIRST__ |
4900 | 500 |
{ |
501 |
unsigned int out = __mapping[(charBuffer >> 18) & 0x3F]; |
|
502 |
out |= (__mapping[(charBuffer >> 12) & 0x3F]) << 8; |
|
503 |
out |= (__mapping[(charBuffer >> 6) & 0x3F]) << 16; |
|
504 |
out |= (__mapping[(charBuffer) & 0x3F]) << 24; |
|
505 |
((unsigned int*)(&buffer[outLen]))[0] = out; |
|
506 |
} |
|
4894 | 507 |
#else |
4900 | 508 |
buffer[outLen] = __mapping[(charBuffer >> 18) & 0x3F]; |
509 |
buffer[outLen+1] = __mapping[(charBuffer >> 12) & 0x3F]; |
|
510 |
buffer[outLen+2] = __mapping[(charBuffer >> 6) & 0x3F]; |
|
511 |
buffer[outLen+3] = __mapping[(charBuffer) & 0x3F]; |
|
4894 | 512 |
#endif |
4900 | 513 |
outLen += 4; |
514 |
lineLength += 4; |
|
515 |
} |
|
516 |
||
517 |
restLength = numInChars-i; |
|
518 |
// fprintf(stderr, "rest: %d\n", restLength); |
|
519 |
if (restLength) { |
|
520 |
if (lineLength >= lineLimit) { |
|
521 |
buffer[outLen++] = '\n'; |
|
522 |
lineLength = 0; |
|
523 |
} |
|
524 |
if (restLength == 1) { |
|
525 |
unsigned int charBuffer; |
|
4894 | 526 |
|
4900 | 527 |
// pad with '==' |
528 |
charBuffer = (in[i]) << 4; |
|
529 |
buffer[outLen] = __mapping[(charBuffer >> 6) & 0x3F]; |
|
530 |
buffer[outLen+1] = __mapping[(charBuffer) & 0x3F]; |
|
531 |
buffer[outLen+2] = __mapping[64]; |
|
532 |
buffer[outLen+3] = __mapping[64]; |
|
533 |
outLen += 4; |
|
534 |
} else { |
|
535 |
unsigned int charBuffer; |
|
4894 | 536 |
|
4900 | 537 |
// restLength == 2 |
538 |
// pad with '=' |
|
539 |
charBuffer = (in[i]) << 8; |
|
540 |
charBuffer |= (in[i+1]); |
|
541 |
charBuffer <<= 2; |
|
542 |
buffer[outLen] = __mapping[(charBuffer >> 12) & 0x3F]; |
|
543 |
buffer[outLen+1] = __mapping[(charBuffer >> 6) & 0x3F]; |
|
544 |
buffer[outLen+2] = __mapping[(charBuffer) & 0x3F]; |
|
545 |
buffer[outLen+3] = __mapping[64]; |
|
546 |
outLen += 4; |
|
547 |
} |
|
548 |
} |
|
4894 | 549 |
|
4900 | 550 |
if (asStringBoolean == true) { |
551 |
encoding = __MKSTRING_L(buffer, outLen); |
|
552 |
} else { |
|
553 |
encoding = __MKBYTEARRAY(buffer, outLen); |
|
554 |
} |
|
555 |
if (buffer != quickBuffer) { |
|
556 |
free(buffer); |
|
557 |
} |
|
558 |
RETURN(encoding); |
|
559 |
} |
|
4894 | 560 |
%}. |
561 |
encoding := super encode:aStringOrByteArray with:lineLimitOrNil. |
|
562 |
asStringBoolean ifTrue:[ |
|
4900 | 563 |
^ encoding asString |
564 |
]. |
|
4894 | 565 |
^ encoding |
4900 | 566 |
|
4894 | 567 |
" |
568 |
(Base64Coder encode:'queen%27s%20gambit') => 'cXVlZW4lMjdzJTIwZ2FtYml0' |
|
569 |
(Base64Coder fastEncode:'queen%27s%20gambit' asString:true) => 'cXVlZW4lMjdzJTIwZ2FtYml0' |
|
570 |
||
571 |
(Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit' |
|
572 |
(Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit' |
|
573 |
(Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0' asString:true) => 'queen%27s%20gambit' |
|
574 |
||
575 |
(Base64Coder encode:'a') => 'YQ==' |
|
576 |
(Base64Coder fastEncode:'a' asString:true) => 'YQ==' |
|
577 |
(Base64Coder fastDecodeString:'YQ==' asString:true) => 'a' |
|
578 |
||
579 |
(Base64Coder encode:'aa') => 'YWE=' |
|
580 |
(Base64Coder fastEncode:'aa' asString:true) => 'YWE=' |
|
581 |
(Base64Coder fastDecodeString:'YWE=' asString:true) => 'aa' |
|
582 |
||
583 |
|data| |
|
584 |
data := ByteArray new:1000. |
|
585 |
Time millisecondsToRun:[ |
|
4900 | 586 |
10000 timesRepeat:[self halt. |
587 |
Base64Coder encode:data. |
|
588 |
] |
|
4894 | 589 |
] |
4900 | 590 |
|
4894 | 591 |
|data| |
592 |
data := ByteArray new:1000. |
|
593 |
Base64Coder fastEncode:data asString:true lineLimit:20. |
|
594 |
||
595 |
|data| |
|
596 |
data := ByteArray new:1000. |
|
597 |
Base64Coder fastEncode:data asString:true lineLimit:nil. |
|
598 |
||
599 |
|data| |
|
600 |
data := ByteArray new:1000. |
|
601 |
Time millisecondsToRun:[ |
|
4900 | 602 |
10000 timesRepeat:[ |
603 |
Base64Coder fastEncode:data. |
|
604 |
] |
|
4894 | 605 |
] |
606 |
||
607 |
self assert:((Base64Coder fastEncode:'abc' asString:true) |
|
4900 | 608 |
= 'abc' base64Encoded). |
4894 | 609 |
self assert:((Base64Coder fastEncode:'a' asString:true) |
4900 | 610 |
= 'a' base64Encoded). |
4894 | 611 |
self assert:((Base64Coder fastEncode:'ab' asString:true) |
4900 | 612 |
= 'ab' base64Encoded). |
4894 | 613 |
self assert:((Base64Coder fastEncode:'abcd' asString:true) |
4900 | 614 |
= 'abcd' base64Encoded). |
4894 | 615 |
self assert:((Base64Coder fastEncode:'abcde' asString:true) |
4900 | 616 |
= 'abcde' base64Encoded). |
4894 | 617 |
self assert:((Base64Coder fastEncode:'abcdef' asString:true) |
4900 | 618 |
= 'abcdef' base64Encoded). |
4894 | 619 |
|
620 |
self assert:((Base64Coder fastEncode:#'parseMethod:onError:rememberNodes:nodeGenerationCallback:' asString:true) |
|
4900 | 621 |
= #'parseMethod:onError:rememberNodes:nodeGenerationCallback:' base64Encoded). |
4894 | 622 |
|
623 |
self assert:((Base64Coder fastEncode:'_INVOKESTATIC_R:' asString:true) |
|
4900 | 624 |
= '_INVOKESTATIC_R:' base64Encoded). |
4894 | 625 |
|
626 |
self assert:((Base64Coder fastEncode:#'_INVOKESTATIC_R:' asString:true) |
|
4900 | 627 |
= #'_INVOKESTATIC_R:' base64Encoded) |
4894 | 628 |
|
629 |
self assert:((Base64Coder fastEncode:'_INVOKESTATIC_R:' asString:true) |
|
4900 | 630 |
= (Base64Coder fastEncode:#'_INVOKESTATIC_R:' asString:true)). |
4894 | 631 |
|
632 |
self assert:((#'_INVOKESTATIC_R:' base64Encoded) |
|
4900 | 633 |
= ('_INVOKESTATIC_R:' base64Encoded)). |
4894 | 634 |
|
635 |
||
636 |
||
637 |
self assert:((Base64Coder fastEncode:'_INVOKESTATIC_R:_:' asString:true) |
|
4900 | 638 |
= '_INVOKESTATIC_R:_:' base64Encoded). |
4894 | 639 |
|
640 |
self assert:((Base64Coder fastEncode:#'_INVOKESTATIC_R:_:' asString:true) |
|
4900 | 641 |
= #'_INVOKESTATIC_R:_:' base64Encoded) |
4894 | 642 |
|
643 |
self assert:((Base64Coder fastEncode:'_INVOKESTATIC_R:_:' asString:true) |
|
4900 | 644 |
= (Base64Coder fastEncode:#'_INVOKESTATIC_R:_:' asString:true)). |
4894 | 645 |
|
646 |
self assert:((#'_INVOKESTATIC_R:_:' base64Encoded) |
|
4900 | 647 |
= ('_INVOKESTATIC_R:_:' base64Encoded)). |
648 |
||
4894 | 649 |
Symbol allInstancesDo:[:each | |
4900 | 650 |
self assert:((Base64Coder fastEncode:each asString:true) |
651 |
= (Base64Coder encode:each with:nil)) |
|
652 |
] |
|
4894 | 653 |
" |
654 |
||
655 |
"Created: / 21-03-2019 / 21:58:59 / Claus Gittinger" |
|
656 |
"Modified (comment): / 21-03-2019 / 23:11:50 / Claus Gittinger" |
|
1027 | 657 |
! ! |
658 |
||
1063 | 659 |
!Base64Coder methodsFor:'encoding'! |
660 |
||
3763 | 661 |
nextPutByte:aByte |
1027 | 662 |
"encode aByte on the output stream" |
663 |
||
4507 | 664 |
|b1 "{ Class: SmallInteger }" |
4900 | 665 |
b2 "{ Class: SmallInteger }" |
4507 | 666 |
b3 "{ Class: SmallInteger }" |
4900 | 667 |
b4 "{ Class: SmallInteger }" |
4507 | 668 |
bufferedBytes "{ Class: SmallInteger }" | |
1027 | 669 |
|
3763 | 670 |
buffer := (buffer bitShift:8) bitOr:aByte. |
1027 | 671 |
bits := bits + 8. |
672 |
bits == 24 ifTrue:[ |
|
4900 | 673 |
"RFC 2045 says: max 76 characters in one line" |
674 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
|
675 |
stream cr. |
|
676 |
charCount := 0. |
|
677 |
]. |
|
678 |
bufferedBytes := buffer. |
|
1027 | 679 |
|
4900 | 680 |
b4 := bufferedBytes bitAnd:16r3F. |
681 |
b3 := (bufferedBytes bitShift:-6) bitAnd:16r3F. |
|
682 |
b2 := (bufferedBytes bitShift:-12) bitAnd:16r3F. |
|
683 |
b1 := (bufferedBytes bitShift:-18) bitAnd:16r3F. |
|
684 |
buffer := bits := 0. |
|
685 |
||
686 |
stream nextPut:(mapping at:b1+1); |
|
687 |
nextPut:(mapping at:b2+1); |
|
688 |
nextPut:(mapping at:b3+1); |
|
689 |
nextPut:(mapping at:b4+1). |
|
690 |
||
691 |
charCount := charCount + 4. |
|
1027 | 692 |
]. |
4507 | 693 |
|
694 |
"Modified: / 26-08-2017 / 12:35:17 / cg" |
|
4743 | 695 |
"Modified: / 30-09-2018 / 15:15:14 / Claus Gittinger" |
1027 | 696 |
! ! |
697 |
||
698 |
!Base64Coder methodsFor:'misc'! |
|
699 |
||
700 |
flush |
|
4900 | 701 |
"flush the remaining bits of buffer. |
1390 | 702 |
The number of bits in buffer is not a multiple of 6, so we pad |
703 |
the buffer and signal that padding has been done via $= characters." |
|
1027 | 704 |
|
4891 | 705 |
|tempBuffer "{Class: SmallInteger}" |
706 |
b1 b2 b3 b4| |
|
4900 | 707 |
|
1390 | 708 |
bits == 0 ifTrue:[ |
4900 | 709 |
"buffer is empty, nothing to do" |
710 |
^ self. |
|
1390 | 711 |
]. |
4900 | 712 |
|
4891 | 713 |
tempBuffer := buffer. |
1390 | 714 |
bits == 8 ifTrue:[ |
4900 | 715 |
tempBuffer := tempBuffer bitShift:4. |
716 |
b4 := b3 := 64. "pad with '=='" |
|
717 |
b1 := (tempBuffer bitShift:-6) bitAnd:16r3F. |
|
718 |
b2 := tempBuffer bitAnd:16r3F. |
|
1390 | 719 |
] ifFalse:[ |
4900 | 720 |
bits = 16 ifTrue:[ |
721 |
tempBuffer := tempBuffer bitShift:2. |
|
722 |
b4 := 64. "pad with '='" |
|
723 |
b3 := tempBuffer bitAnd:16r3F. |
|
724 |
b2 := (tempBuffer bitShift:-6) bitAnd:16r3F. |
|
725 |
b1 := (tempBuffer bitShift:-12) bitAnd:16r3F. |
|
726 |
] |
|
1390 | 727 |
]. |
728 |
bits := buffer := 0. |
|
1027 | 729 |
|
1391 | 730 |
"RFC 2045 says: max 76 characters in one line" |
731 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
|
4900 | 732 |
stream cr. |
733 |
charCount := 0. |
|
1391 | 734 |
]. |
735 |
||
4743 | 736 |
stream nextPut:(mapping at:b1+1); |
4900 | 737 |
nextPut:(mapping at:b2+1); |
738 |
nextPut:(mapping at:b3+1); |
|
739 |
nextPut:(mapping at:b4+1). |
|
1391 | 740 |
charCount := charCount + 4. |
4743 | 741 |
|
4891 | 742 |
"Modified: / 20-03-2019 / 21:22:39 / Claus Gittinger" |
1484 | 743 |
! ! |
744 |
||
745 |
!Base64Coder methodsFor:'private'! |
|
746 |
||
747 |
fillBuffer |
|
4743 | 748 |
"fill buffer with next 4 characters each representing 6 bits. |
749 |
Used when decoding." |
|
1484 | 750 |
|
4900 | 751 |
|b |
4743 | 752 |
tempBuffer "{Class: SmallInteger}" |
753 |
_bits "{Class: SmallInteger}" | |
|
1484 | 754 |
|
755 |
tempBuffer := 0. |
|
4743 | 756 |
_bits := 0. |
757 |
||
3210 | 758 |
[ |
4900 | 759 |
"read next valid Base64 character, skip invalid characters" |
760 |
b := 255. |
|
761 |
[b == 255] whileTrue:[ |
|
762 |
b := stream next. |
|
763 |
b isNil ifTrue:[ "end of stream" |
|
764 |
b := 64. "simulate end-mark" |
|
765 |
] ifFalse:[ |
|
766 |
b := reverseMapping at:b codePoint ifAbsent:255. |
|
767 |
] |
|
768 |
]. |
|
1964 | 769 |
|
4900 | 770 |
b == 64 ifTrue:[ |
771 |
"got $=, end of Base64 string has been reached" |
|
772 |
atEnd := true. |
|
773 |
_bits == 12 ifTrue:[ |
|
774 |
"data has been padded to 12, skip 4 bits" |
|
775 |
tempBuffer := tempBuffer bitShift:-4. |
|
776 |
_bits := _bits - 4. |
|
777 |
] ifFalse:[_bits == 18 ifTrue:[ |
|
778 |
"data has been padded to 18, skip 2 bits" |
|
779 |
tempBuffer := tempBuffer bitShift:-2. |
|
780 |
_bits := _bits - 2. |
|
781 |
]]. |
|
782 |
] ifFalse:[ |
|
783 |
"got valid Base64 character, append to buffer" |
|
784 |
tempBuffer := (tempBuffer bitShift:6) bitOr:b. |
|
785 |
_bits := _bits + 6. |
|
786 |
]. |
|
787 |
(_bits == 24 or:[atEnd]) ifTrue:[ |
|
788 |
bits := _bits. |
|
789 |
buffer := tempBuffer. |
|
790 |
^ self. |
|
791 |
]. |
|
3210 | 792 |
] loop. |
4743 | 793 |
|
794 |
"Modified: / 30-09-2018 / 15:16:19 / Claus Gittinger" |
|
1027 | 795 |
! ! |
796 |
||
797 |
!Base64Coder class methodsFor:'documentation'! |
|
798 |
||
799 |
version |
|
3763 | 800 |
^ '$Header$' |
2325 | 801 |
! |
802 |
||
803 |
version_CVS |
|
3763 | 804 |
^ '$Header$' |
1027 | 805 |
! ! |