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