author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 12:36:55 +0200 | |
changeset 3898 | c90424dba938 |
parent 3763 | 0d73959c77f3 |
child 4279 | bb907803ee15 |
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 |
|
46 |
[author:] |
|
47 |
Stefan Vogel |
|
48 |
||
49 |
[see also:] |
|
50 |
||
51 |
[instance variables:] |
|
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] |
3478 | 63 |
(Base64Coder encode:'queen%27s%20gambit') asString = 'cXVlZW4lMjdzJTIwZ2FtYml0' |
64 |
[exEnd] |
|
65 |
||
66 |
[exBegin] |
|
67 |
(Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString = 'queen%27s%20gambit' |
|
2224 | 68 |
[exEnd] |
69 |
||
70 |
[exBegin] |
|
1484 | 71 |
0 to:16 do:[:l | |
72 |
|coder decoder data encoding decoded| |
|
1391 | 73 |
|
74 |
data := (0 to:l) asByteArray copyTo:l. |
|
75 |
coder := Base64Coder on:'' writeStream. |
|
76 |
coder nextPutAll:data. |
|
77 |
coder flush. |
|
78 |
||
79 |
encoding := coder contents. |
|
80 |
||
81 |
decoder := Base64Coder on:encoding readStream. |
|
82 |
decoded := decoder upToEnd. |
|
83 |
Transcript showCR:(data printString). |
|
84 |
Transcript show:' -> '; showCR:encoding. |
|
85 |
Transcript show:' ---> '; showCR:(decoded printString). |
|
86 |
self assert:(data = decoded). |
|
87 |
]. |
|
88 |
[exEnd] |
|
89 |
[exBegin] |
|
1122 | 90 |
|data1 text data2| |
91 |
||
92 |
data1 := #[0 1 16r7F 16r80 16r81 16rFE 16rFF]. |
|
1639 | 93 |
text := Base64Coder encode:data1. |
94 |
data2 := Base64Coder decode:text. |
|
1122 | 95 |
data2 |
96 |
[exEnd] |
|
1027 | 97 |
|
98 |
[exBegin] |
|
99 |
|coder| |
|
100 |
||
101 |
coder := Base64Coder on:'' writeStream. |
|
102 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]. |
|
103 |
coder flush. |
|
104 |
coder contents inspect. |
|
105 |
coder reset. |
|
106 |
coder nextPut:254. |
|
107 |
coder contents inspect. |
|
108 |
[exEnd] |
|
109 |
||
110 |
[exBegin] |
|
1391 | 111 |
|coder decoder| |
1027 | 112 |
|
113 |
coder := Base64Coder on:'' writeStream. |
|
114 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]. |
|
115 |
coder flush. |
|
116 |
||
1391 | 117 |
decoder := Base64Coder on:(coder contents readStream). |
118 |
[decoder atEnd] whileFalse:[ |
|
119 |
Transcript show:decoder next |
|
1027 | 120 |
]. |
121 |
Transcript cr. |
|
122 |
[exEnd] |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
123 |
[exBegin] |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
124 |
|coder| |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
125 |
|
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
126 |
coder := Base64Coder on:'' writeStream. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
127 |
coder nextPutAll:(0 to:200) asByteArray. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
128 |
coder flush. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
129 |
|
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
130 |
Transcript showCR:(coder contents). |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
131 |
[exEnd] |
1027 | 132 |
" |
133 |
! ! |
|
134 |
||
135 |
!Base64Coder class methodsFor:'initialization'! |
|
136 |
||
3547
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
137 |
initializeMappings |
1027 | 138 |
"initialize class variables" |
139 |
||
3547
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
140 |
Base64Mapping isNil ifTrue:[ |
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
141 |
"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
|
142 |
Base64Mapping := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='. |
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
143 |
Base64ReverseMapping := ByteArray new:128 withAll:255. |
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
144 |
Base64Mapping keysAndValuesDo:[:idx :char| |
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
145 |
Base64ReverseMapping at:char codePoint put:idx-1. |
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
146 |
]. |
1027 | 147 |
]. |
148 |
||
149 |
" |
|
3547
399b59b67fda
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
3478
diff
changeset
|
150 |
self initializeMappings |
1027 | 151 |
" |
152 |
! ! |
|
153 |
||
1063 | 154 |
!Base64Coder methodsFor:'encoding'! |
155 |
||
3763 | 156 |
nextPutByte:aByte |
1027 | 157 |
"encode aByte on the output stream" |
158 |
||
159 |
|b1 b2 b3 b4| |
|
160 |
||
3763 | 161 |
buffer := (buffer bitShift:8) bitOr:aByte. |
1027 | 162 |
bits := bits + 8. |
163 |
bits == 24 ifTrue:[ |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
164 |
"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
|
165 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
166 |
stream cr. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
167 |
charCount := 0. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
168 |
]. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
169 |
|
1027 | 170 |
b4 := buffer bitAnd:16r3F. |
171 |
b3 := (buffer bitShift:-6) bitAnd:16r3F. |
|
172 |
b2 := (buffer bitShift:-12) bitAnd:16r3F. |
|
173 |
b1 := (buffer bitShift:-18) bitAnd:16r3F. |
|
174 |
buffer := bits := 0. |
|
175 |
stream nextPut:(Base64Mapping at:b1+1); |
|
176 |
nextPut:(Base64Mapping at:b2+1); |
|
177 |
nextPut:(Base64Mapping at:b3+1); |
|
178 |
nextPut:(Base64Mapping at:b4+1). |
|
179 |
||
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
180 |
charCount := charCount + 4. |
1027 | 181 |
]. |
182 |
! ! |
|
183 |
||
184 |
!Base64Coder methodsFor:'misc'! |
|
185 |
||
186 |
flush |
|
1390 | 187 |
"flush the remaining bits of buffer. |
188 |
The number of bits in buffer is not a multiple of 6, so we pad |
|
189 |
the buffer and signal that padding has been done via $= characters." |
|
1027 | 190 |
|
1390 | 191 |
|b1 b2 b3 b4| |
192 |
||
193 |
bits == 0 ifTrue:[ |
|
1027 | 194 |
"buffer is empty, nothing to do" |
195 |
^ self. |
|
1390 | 196 |
]. |
197 |
||
198 |
bits == 8 ifTrue:[ |
|
1027 | 199 |
buffer := buffer bitShift:4. |
200 |
b4 := b3 := 64. "pad with '=='" |
|
201 |
b1 := (buffer bitShift:-6) bitAnd:16r3F. |
|
202 |
b2 := buffer bitAnd:16r3F. |
|
1390 | 203 |
] ifFalse:[ |
204 |
bits = 16 ifTrue:[ |
|
205 |
buffer := buffer bitShift:2. |
|
206 |
b4 := 64. "pad with '='" |
|
207 |
b3 := buffer bitAnd:16r3F. |
|
208 |
b2 := (buffer bitShift:-6) bitAnd:16r3F. |
|
209 |
b1 := (buffer bitShift:-12) bitAnd:16r3F. |
|
210 |
] |
|
211 |
]. |
|
212 |
bits := buffer := 0. |
|
1027 | 213 |
|
1391 | 214 |
"RFC 2045 says: max 76 characters in one line" |
215 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
|
216 |
stream cr. |
|
217 |
charCount := 0. |
|
218 |
]. |
|
219 |
||
1390 | 220 |
stream nextPut:(Base64Mapping at:b1+1); |
221 |
nextPut:(Base64Mapping at:b2+1); |
|
222 |
nextPut:(Base64Mapping at:b3+1); |
|
223 |
nextPut:(Base64Mapping at:b4+1). |
|
1391 | 224 |
charCount := charCount + 4. |
1484 | 225 |
! ! |
226 |
||
227 |
!Base64Coder methodsFor:'private'! |
|
228 |
||
229 |
fillBuffer |
|
230 |
"fill buffer with next 4 characters each representing 6 bits" |
|
231 |
||
232 |
|b shift tempBuffer "{Class: SmallInteger}"| |
|
233 |
||
234 |
tempBuffer := 0. |
|
235 |
bits := 0. |
|
3210 | 236 |
[ |
1484 | 237 |
"read next valid Base64 character, skip invalid characters" |
2137 | 238 |
b := 255. |
239 |
[b == 255] whileTrue:[ |
|
1484 | 240 |
b := stream next. |
241 |
b isNil ifTrue:[ "end of stream" |
|
1964 | 242 |
b := 64. "simulate end-mark" |
1484 | 243 |
] ifFalse:[ |
1964 | 244 |
b := Base64ReverseMapping at:b codePoint ifAbsent:255. |
1484 | 245 |
] |
2137 | 246 |
]. |
1964 | 247 |
|
1484 | 248 |
b == 64 ifTrue:[ |
249 |
"got $=, end of Base64 string has been reached" |
|
250 |
atEnd := true. |
|
251 |
bits == 12 ifTrue:[ |
|
252 |
"data has been padded to 12, skip 4 bits" |
|
253 |
shift := -4. |
|
254 |
] ifFalse:[bits == 18 ifTrue:[ |
|
255 |
"data has been padded to 18, skip 2 bits" |
|
256 |
shift := -2. |
|
257 |
] ifFalse:[ |
|
258 |
shift := 0. |
|
259 |
]]. |
|
260 |
tempBuffer := tempBuffer bitShift:shift. |
|
261 |
bits := bits + shift. |
|
262 |
] ifFalse:[ |
|
263 |
"got valid Base64 character, append to buffer" |
|
264 |
tempBuffer := (tempBuffer bitShift:6) bitOr:b. |
|
265 |
bits := bits + 6. |
|
266 |
]. |
|
2137 | 267 |
(bits == 24 or:[atEnd]) ifTrue:[ |
268 |
buffer := tempBuffer. |
|
269 |
^ self. |
|
270 |
]. |
|
3210 | 271 |
] loop. |
1027 | 272 |
! ! |
273 |
||
274 |
!Base64Coder class methodsFor:'documentation'! |
|
275 |
||
276 |
version |
|
3763 | 277 |
^ '$Header$' |
2325 | 278 |
! |
279 |
||
280 |
version_CVS |
|
3763 | 281 |
^ '$Header$' |
1027 | 282 |
! ! |
1111 | 283 |