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