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