author | Stefan Vogel <sv@exept.de> |
Wed, 23 Apr 2008 14:59:44 +0200 | |
changeset 1974 | 40ac9e5f036c |
parent 1963 | 7b74621a9541 |
child 2326 | 497afb98bdcf |
permissions | -rw-r--r-- |
1963 | 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 |
" |
|
12 |
"{ Package: 'stx:libbasic2' }" |
|
13 |
||
14 |
BaseNCoder subclass:#Base32Coder |
|
15 |
instanceVariableNames:'' |
|
16 |
classVariableNames:'Base32Mapping Base32ReverseMapping' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'System-Storage' |
|
19 |
! |
|
20 |
||
21 |
!Base32Coder class methodsFor:'documentation'! |
|
22 |
||
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 |
||
37 |
documentation |
|
38 |
" |
|
39 |
Instances of this class perform Base32 en- and decoding as defined in RFC 3548 |
|
40 |
5 bytes are mapped to 8 characters, representing 5 bits each. |
|
41 |
||
42 |
[author:] |
|
43 |
Stefan Vogel |
|
44 |
||
45 |
[see also:] |
|
46 |
||
47 |
[instance variables:] |
|
48 |
||
49 |
[class variables:] |
|
50 |
Base32Mapping String Mapping from bytes (with 5 valid bits) |
|
51 |
to Base32 characters |
|
52 |
Base32ReverseMapping Array Mapping from Base32 characters to 5-bit-Bytes |
|
53 |
" |
|
54 |
! |
|
55 |
||
56 |
examples |
|
57 |
" |
|
58 |
[exBegin] |
|
59 |
0 to:16 do:[:l | |
|
60 |
|coder decoder data encoding decoded| |
|
61 |
||
62 |
data := (0 to:l) asByteArray copyTo:l. |
|
63 |
coder := Base32Coder on:'' writeStream. |
|
64 |
coder nextPutAll:data. |
|
65 |
coder flush. |
|
66 |
||
67 |
encoding := coder contents. |
|
68 |
||
69 |
decoder := Base32Coder on:encoding readStream. |
|
70 |
decoded := decoder upToEnd. |
|
71 |
Transcript showCR:(data printString). |
|
72 |
Transcript show:' -> '; showCR:encoding. |
|
73 |
Transcript show:' ---> '; showCR:(decoded printString). |
|
74 |
self assert:(data = decoded). |
|
75 |
]. |
|
76 |
[exEnd] |
|
77 |
[exBegin] |
|
78 |
|data1 text data2| |
|
79 |
||
80 |
data1 := #[0 1 16r7F 16r80 16r81 16rFE 16rFF]. |
|
81 |
text := Base32Coder encode:data1. |
|
82 |
data2 := Base32Coder decode:text. |
|
83 |
data2 |
|
84 |
[exEnd] |
|
85 |
||
86 |
[exBegin] |
|
87 |
|coder| |
|
88 |
||
89 |
coder := Base32Coder on:'' writeStream. |
|
90 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]. |
|
91 |
coder flush. |
|
92 |
coder contents inspect. |
|
93 |
coder reset. |
|
94 |
coder nextPut:254. |
|
95 |
coder contents inspect. |
|
96 |
[exEnd] |
|
97 |
||
98 |
[exBegin] |
|
99 |
|coder decoder| |
|
100 |
||
101 |
coder := Base32Coder 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 20]. |
|
103 |
coder flush. |
|
104 |
||
105 |
decoder := Base32Coder on:(coder contents readStream). |
|
106 |
[decoder atEnd] whileFalse:[ |
|
107 |
Transcript show:decoder next |
|
108 |
]. |
|
109 |
Transcript cr. |
|
110 |
[exEnd] |
|
111 |
[exBegin] |
|
112 |
|coder| |
|
113 |
||
114 |
coder := Base32Coder on:'' writeStream. |
|
115 |
coder nextPutAll:(0 to:200) asByteArray. |
|
116 |
coder flush. |
|
117 |
||
118 |
Transcript showCR:(coder contents). |
|
119 |
[exEnd] |
|
120 |
" |
|
121 |
! ! |
|
122 |
||
123 |
!Base32Coder class methodsFor:'initialization'! |
|
124 |
||
125 |
initialize |
|
126 |
"initialize class variables" |
|
127 |
||
128 |
"33 characters representing the 5-bit values from 0-31 and one pad character" |
|
129 |
Base32Mapping := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567='. |
|
130 |
Base32ReverseMapping := ByteArray new:96 withAll:255. |
|
131 |
Base32Mapping keysAndValuesDo:[:idx :char| |
|
132 |
Base32ReverseMapping at:char codePoint put:idx-1. |
|
133 |
]. |
|
134 |
||
135 |
" |
|
136 |
self initialize |
|
137 |
" |
|
138 |
! ! |
|
139 |
||
140 |
!Base32Coder methodsFor:'encoding'! |
|
141 |
||
142 |
nextPut:aByte |
|
143 |
"encode aByte on the output stream" |
|
144 |
||
145 |
"RFC 2045 says: max 76 characters in one line" |
|
146 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
|
147 |
stream cr. |
|
148 |
charCount := 0. |
|
149 |
]. |
|
150 |
||
151 |
buffer := (buffer bitShift:8) bitOr:aByte. |
|
152 |
bits := bits + 8 - 5. "max value of bits = 4 + 8 - 5" |
|
153 |
||
154 |
bits >= 5 ifTrue:[ |
|
155 |
"enough bits to write two chars: write the first 5 bits" |
|
156 |
stream nextPut:(Base32Mapping at:(buffer rightShift:bits)+1). |
|
157 |
"clear the first 5 bits" |
|
158 |
buffer := buffer bitAnd:(1 bitShift:bits)-1. |
|
159 |
charCount := charCount + 1. |
|
160 |
bits := bits - 5. |
|
161 |
]. |
|
162 |
||
163 |
"write 5 bits" |
|
164 |
stream nextPut:(Base32Mapping at:(buffer rightShift:bits)+1). |
|
165 |
"clear the first 5 bits" |
|
166 |
buffer := buffer bitAnd:(1 bitShift:bits)-1. |
|
167 |
charCount := charCount + 1. |
|
168 |
! ! |
|
169 |
||
170 |
!Base32Coder methodsFor:'misc'! |
|
171 |
||
172 |
flush |
|
173 |
"flush the remaining bits of buffer. |
|
174 |
The number of bits in buffer is not a multiple of 6, so we pad |
|
175 |
the buffer and signal that padding has been done via $= characters." |
|
176 |
||
177 |
|shift| |
|
178 |
||
179 |
bits == 0 ifTrue:[ |
|
180 |
"buffer is empty, nothing to do" |
|
181 |
^ self. |
|
182 |
]. |
|
183 |
||
184 |
"RFC 2045 says: max 76 characters in one line" |
|
185 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
|
186 |
stream cr. |
|
187 |
charCount := 0. |
|
188 |
]. |
|
189 |
||
190 |
shift := 5 - bits. |
|
191 |
buffer := buffer bitShift:shift. |
|
192 |
||
193 |
stream nextPut:(Base32Mapping at:buffer+1). |
|
194 |
bits := bits + 3. |
|
195 |
[ |
|
196 |
"simulate adding 8 bits and removing 5 bits, until we get a multiple of 8" |
|
197 |
stream nextPut:$=. |
|
198 |
bits := bits + 8 - 5. |
|
199 |
] doUntil:[bits \\ 8 == 0]. |
|
200 |
||
201 |
buffer := bits := 0. |
|
202 |
! ! |
|
203 |
||
204 |
!Base32Coder methodsFor:'private'! |
|
205 |
||
206 |
fillBuffer |
|
207 |
"fill buffer with next 8 characters each representing 5 bits" |
|
208 |
||
1974
40ac9e5f036c
changed #fillBuffer - remove explicit type SmallInteger
Stefan Vogel <sv@exept.de>
parents:
1963
diff
changeset
|
209 |
|b shift tempBuffer| |
1963 | 210 |
|
211 |
tempBuffer := 0. |
|
212 |
bits := 0. |
|
213 |
[ |
|
214 |
"read next valid Base64 character, skip invalid characters" |
|
215 |
[ |
|
216 |
b := stream next. |
|
217 |
b isNil ifTrue:[ "end of stream" |
|
218 |
b := 32. "simulate end-mark" |
|
219 |
] ifFalse:[ |
|
220 |
b := Base32ReverseMapping at:b codePoint ifAbsent:255. |
|
221 |
] |
|
222 |
] doWhile:[b == 255]. |
|
223 |
||
224 |
b == 32 ifTrue:[ |
|
225 |
"got $=, end of Base32 string has been reached. |
|
226 |
Strip fill bits" |
|
227 |
atEnd := true. |
|
228 |
bits > 8 ifTrue:[ |
|
229 |
shift := bits \\ 8. |
|
230 |
tempBuffer := tempBuffer rightShift:shift. |
|
231 |
bits := bits - shift. |
|
232 |
]. |
|
233 |
] ifFalse:[ |
|
234 |
"got valid Base64 character, append to buffer" |
|
235 |
tempBuffer := (tempBuffer bitShift:5) bitOr:b. |
|
236 |
bits := bits + 5. |
|
237 |
]. |
|
238 |
] doUntil:[bits == 40 or:[atEnd]]. |
|
239 |
||
240 |
buffer := tempBuffer. |
|
241 |
! ! |
|
242 |
||
243 |
!Base32Coder class methodsFor:'documentation'! |
|
244 |
||
245 |
version |
|
1974
40ac9e5f036c
changed #fillBuffer - remove explicit type SmallInteger
Stefan Vogel <sv@exept.de>
parents:
1963
diff
changeset
|
246 |
^ '$Header: /cvs/stx/stx/libbasic2/Base32Coder.st,v 1.2 2008-04-23 12:59:44 stefan Exp $' |
1963 | 247 |
! ! |
248 |
||
249 |
Base32Coder initialize! |