author | Stefan Vogel <sv@exept.de> |
Fri, 17 Feb 2017 11:08:34 +0100 | |
changeset 4334 | 95bb043e3339 |
parent 4279 | bb907803ee15 |
child 4507 | e85ee316a1b2 |
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 |
||
3478 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1964 | 16 |
BaseNCoder subclass:#Base64Coder |
17 |
instanceVariableNames:'' |
|
1027 | 18 |
classVariableNames:'Base64Mapping Base64ReverseMapping' |
19 |
poolDictionaries:'' |
|
20 |
category:'System-Storage' |
|
21 |
! |
|
22 |
||
23 |
!Base64Coder class methodsFor:'documentation'! |
|
24 |
||
1308 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
1027 | 39 |
documentation |
40 |
" |
|
41 |
Instances of this class perform Base64 en- and decoding as defined in RFC 2045 |
|
42 |
3 bytes are mapped to 4 characters, representing 6 bits each. |
|
2068 | 43 |
The encoded string consists only of characters from the set: |
44 |
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' |
|
1027 | 45 |
|
4279 | 46 |
Thei main entry point API is |
47 |
Base64Coder encode:aStringOrBytes |
|
48 |
and |
|
49 |
Base64Coder decode:aString |
|
50 |
||
51 |
If the decoder should return a string, use |
|
52 |
Base64Coder decodeAsString:aString. |
|
53 |
||
1027 | 54 |
[author:] |
55 |
Stefan Vogel |
|
56 |
||
57 |
[see also:] |
|
58 |
||
59 |
[instance variables:] |
|
60 |
||
61 |
[class variables:] |
|
62 |
Base64Mapping String Mapping from bytes (with 6 valid bits) |
|
63 |
to Base64 characters |
|
64 |
Base64ReverseMapping Array Mapping from Base64 characters to 6-bit-Bytes |
|
65 |
" |
|
66 |
! |
|
67 |
||
68 |
examples |
|
69 |
" |
|
1122 | 70 |
[exBegin] |
3478 | 71 |
(Base64Coder encode:'queen%27s%20gambit') asString = 'cXVlZW4lMjdzJTIwZ2FtYml0' |
72 |
[exEnd] |
|
73 |
||
74 |
[exBegin] |
|
75 |
(Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString = 'queen%27s%20gambit' |
|
2224 | 76 |
[exEnd] |
77 |
||
78 |
[exBegin] |
|
1484 | 79 |
0 to:16 do:[:l | |
80 |
|coder decoder data encoding decoded| |
|
1391 | 81 |
|
82 |
data := (0 to:l) asByteArray copyTo:l. |
|
83 |
coder := Base64Coder on:'' writeStream. |
|
84 |
coder nextPutAll:data. |
|
85 |
coder flush. |
|
86 |
||
87 |
encoding := coder contents. |
|
88 |
||
89 |
decoder := Base64Coder on:encoding readStream. |
|
90 |
decoded := decoder upToEnd. |
|
91 |
Transcript showCR:(data printString). |
|
92 |
Transcript show:' -> '; showCR:encoding. |
|
93 |
Transcript show:' ---> '; showCR:(decoded printString). |
|
94 |
self assert:(data = decoded). |
|
95 |
]. |
|
96 |
[exEnd] |
|
97 |
[exBegin] |
|
1122 | 98 |
|data1 text data2| |
99 |
||
100 |
data1 := #[0 1 16r7F 16r80 16r81 16rFE 16rFF]. |
|
1639 | 101 |
text := Base64Coder encode:data1. |
102 |
data2 := Base64Coder decode:text. |
|
1122 | 103 |
data2 |
104 |
[exEnd] |
|
1027 | 105 |
|
106 |
[exBegin] |
|
107 |
|coder| |
|
108 |
||
109 |
coder := Base64Coder on:'' writeStream. |
|
110 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]. |
|
111 |
coder flush. |
|
112 |
coder contents inspect. |
|
113 |
coder reset. |
|
114 |
coder nextPut:254. |
|
115 |
coder contents inspect. |
|
116 |
[exEnd] |
|
117 |
||
118 |
[exBegin] |
|
1391 | 119 |
|coder decoder| |
1027 | 120 |
|
121 |
coder := Base64Coder on:'' writeStream. |
|
122 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]. |
|
123 |
coder flush. |
|
124 |
||
1391 | 125 |
decoder := Base64Coder on:(coder contents readStream). |
126 |
[decoder atEnd] whileFalse:[ |
|
127 |
Transcript show:decoder next |
|
1027 | 128 |
]. |
129 |
Transcript cr. |
|
130 |
[exEnd] |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
131 |
[exBegin] |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
132 |
|coder| |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
133 |
|
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
134 |
coder := Base64Coder on:'' writeStream. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
135 |
coder nextPutAll:(0 to:200) asByteArray. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
136 |
coder flush. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
137 |
|
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
138 |
Transcript showCR:(coder contents). |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
139 |
[exEnd] |
1027 | 140 |
" |
141 |
! ! |
|
142 |
||
143 |
!Base64Coder class methodsFor:'initialization'! |
|
144 |
||
3547
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
145 |
initializeMappings |
1027 | 146 |
"initialize class variables" |
147 |
||
3547
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
148 |
Base64Mapping isNil ifTrue:[ |
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
149 |
"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
|
150 |
Base64Mapping := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='. |
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
151 |
Base64ReverseMapping := ByteArray new:128 withAll:255. |
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
152 |
Base64Mapping keysAndValuesDo:[:idx :char| |
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
153 |
Base64ReverseMapping at:char codePoint put:idx-1. |
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
154 |
]. |
1027 | 155 |
]. |
156 |
||
157 |
" |
|
3547
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
158 |
self initializeMappings |
1027 | 159 |
" |
160 |
! ! |
|
161 |
||
1063 | 162 |
!Base64Coder methodsFor:'encoding'! |
163 |
||
3763 | 164 |
nextPutByte:aByte |
1027 | 165 |
"encode aByte on the output stream" |
166 |
||
167 |
|b1 b2 b3 b4| |
|
168 |
||
3763 | 169 |
buffer := (buffer bitShift:8) bitOr:aByte. |
1027 | 170 |
bits := bits + 8. |
171 |
bits == 24 ifTrue:[ |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
172 |
"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
|
173 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
174 |
stream cr. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
175 |
charCount := 0. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
176 |
]. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
177 |
|
1027 | 178 |
b4 := buffer bitAnd:16r3F. |
179 |
b3 := (buffer bitShift:-6) bitAnd:16r3F. |
|
180 |
b2 := (buffer bitShift:-12) bitAnd:16r3F. |
|
181 |
b1 := (buffer bitShift:-18) bitAnd:16r3F. |
|
182 |
buffer := bits := 0. |
|
183 |
stream nextPut:(Base64Mapping at:b1+1); |
|
184 |
nextPut:(Base64Mapping at:b2+1); |
|
185 |
nextPut:(Base64Mapping at:b3+1); |
|
186 |
nextPut:(Base64Mapping at:b4+1). |
|
187 |
||
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
188 |
charCount := charCount + 4. |
1027 | 189 |
]. |
190 |
! ! |
|
191 |
||
192 |
!Base64Coder methodsFor:'misc'! |
|
193 |
||
194 |
flush |
|
1390 | 195 |
"flush the remaining bits of buffer. |
196 |
The number of bits in buffer is not a multiple of 6, so we pad |
|
197 |
the buffer and signal that padding has been done via $= characters." |
|
1027 | 198 |
|
1390 | 199 |
|b1 b2 b3 b4| |
200 |
||
201 |
bits == 0 ifTrue:[ |
|
1027 | 202 |
"buffer is empty, nothing to do" |
203 |
^ self. |
|
1390 | 204 |
]. |
205 |
||
206 |
bits == 8 ifTrue:[ |
|
1027 | 207 |
buffer := buffer bitShift:4. |
208 |
b4 := b3 := 64. "pad with '=='" |
|
209 |
b1 := (buffer bitShift:-6) bitAnd:16r3F. |
|
210 |
b2 := buffer bitAnd:16r3F. |
|
1390 | 211 |
] ifFalse:[ |
212 |
bits = 16 ifTrue:[ |
|
213 |
buffer := buffer bitShift:2. |
|
214 |
b4 := 64. "pad with '='" |
|
215 |
b3 := buffer bitAnd:16r3F. |
|
216 |
b2 := (buffer bitShift:-6) bitAnd:16r3F. |
|
217 |
b1 := (buffer bitShift:-12) bitAnd:16r3F. |
|
218 |
] |
|
219 |
]. |
|
220 |
bits := buffer := 0. |
|
1027 | 221 |
|
1391 | 222 |
"RFC 2045 says: max 76 characters in one line" |
223 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
|
224 |
stream cr. |
|
225 |
charCount := 0. |
|
226 |
]. |
|
227 |
||
1390 | 228 |
stream nextPut:(Base64Mapping at:b1+1); |
229 |
nextPut:(Base64Mapping at:b2+1); |
|
230 |
nextPut:(Base64Mapping at:b3+1); |
|
231 |
nextPut:(Base64Mapping at:b4+1). |
|
1391 | 232 |
charCount := charCount + 4. |
1484 | 233 |
! ! |
234 |
||
235 |
!Base64Coder methodsFor:'private'! |
|
236 |
||
237 |
fillBuffer |
|
238 |
"fill buffer with next 4 characters each representing 6 bits" |
|
239 |
||
240 |
|b shift tempBuffer "{Class: SmallInteger}"| |
|
241 |
||
242 |
tempBuffer := 0. |
|
243 |
bits := 0. |
|
3210 | 244 |
[ |
1484 | 245 |
"read next valid Base64 character, skip invalid characters" |
2137 | 246 |
b := 255. |
247 |
[b == 255] whileTrue:[ |
|
1484 | 248 |
b := stream next. |
249 |
b isNil ifTrue:[ "end of stream" |
|
1964 | 250 |
b := 64. "simulate end-mark" |
1484 | 251 |
] ifFalse:[ |
1964 | 252 |
b := Base64ReverseMapping at:b codePoint ifAbsent:255. |
1484 | 253 |
] |
2137 | 254 |
]. |
1964 | 255 |
|
1484 | 256 |
b == 64 ifTrue:[ |
257 |
"got $=, end of Base64 string has been reached" |
|
258 |
atEnd := true. |
|
259 |
bits == 12 ifTrue:[ |
|
260 |
"data has been padded to 12, skip 4 bits" |
|
261 |
shift := -4. |
|
262 |
] ifFalse:[bits == 18 ifTrue:[ |
|
263 |
"data has been padded to 18, skip 2 bits" |
|
264 |
shift := -2. |
|
265 |
] ifFalse:[ |
|
266 |
shift := 0. |
|
267 |
]]. |
|
268 |
tempBuffer := tempBuffer bitShift:shift. |
|
269 |
bits := bits + shift. |
|
270 |
] ifFalse:[ |
|
271 |
"got valid Base64 character, append to buffer" |
|
272 |
tempBuffer := (tempBuffer bitShift:6) bitOr:b. |
|
273 |
bits := bits + 6. |
|
274 |
]. |
|
2137 | 275 |
(bits == 24 or:[atEnd]) ifTrue:[ |
276 |
buffer := tempBuffer. |
|
277 |
^ self. |
|
278 |
]. |
|
3210 | 279 |
] loop. |
1027 | 280 |
! ! |
281 |
||
282 |
!Base64Coder class methodsFor:'documentation'! |
|
283 |
||
284 |
version |
|
3763 | 285 |
^ '$Header$' |
2325 | 286 |
! |
287 |
||
288 |
version_CVS |
|
3763 | 289 |
^ '$Header$' |
1027 | 290 |
! ! |
1111 | 291 |