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