author | Claus Gittinger <cg@exept.de> |
Sat, 02 May 2020 21:40:13 +0200 | |
changeset 5476 | 7355a4b11cb6 |
parent 4742 | 476666bdc4a0 |
permissions | -rw-r--r-- |
4742 | 1 |
"{ Encoding: utf8 }" |
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 |
|data1 text data2| |
|
64 |
||
65 |
data1 := #[0 1 16r7F 16r80 16r81 16rFE 16rFF]. |
|
66 |
text := Base32Coder encode:data1. |
|
67 |
data2 := Base32Coder decode:text. |
|
4742 | 68 |
self assert:(data2 = data1) |
1963 | 69 |
[exEnd] |
70 |
||
71 |
[exBegin] |
|
72 |
|coder| |
|
73 |
||
74 |
coder := Base32Coder on:'' writeStream. |
|
75 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]. |
|
76 |
coder flush. |
|
77 |
coder contents inspect. |
|
78 |
coder reset. |
|
79 |
coder nextPut:254. |
|
80 |
coder contents inspect. |
|
81 |
[exEnd] |
|
82 |
||
83 |
[exBegin] |
|
84 |
|coder decoder| |
|
85 |
||
86 |
coder := Base32Coder on:'' writeStream. |
|
87 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]. |
|
88 |
coder flush. |
|
89 |
||
90 |
decoder := Base32Coder on:(coder contents readStream). |
|
91 |
[decoder atEnd] whileFalse:[ |
|
92 |
Transcript show:decoder next |
|
93 |
]. |
|
94 |
Transcript cr. |
|
95 |
[exEnd] |
|
96 |
[exBegin] |
|
97 |
|coder| |
|
98 |
||
99 |
coder := Base32Coder on:'' writeStream. |
|
100 |
coder nextPutAll:(0 to:200) asByteArray. |
|
101 |
coder flush. |
|
102 |
||
103 |
Transcript showCR:(coder contents). |
|
104 |
[exEnd] |
|
4742 | 105 |
[exBegin] |
106 |
|bytes encoded decoded| |
|
107 |
||
108 |
bytes := #[0 0 0] copy. |
|
109 |
0 to:255 do:[:b1 | |
|
110 |
Transcript showCR:b1. |
|
111 |
bytes at:1 put:b1. |
|
112 |
0 to:255 do:[:b2 | |
|
113 |
bytes at:2 put:b2. |
|
114 |
0 to:255 do:[:b3 | |
|
115 |
bytes at:3 put:b3. |
|
116 |
encoded := Base32Coder encode:bytes. |
|
117 |
decoded := Base32Coder decode:encoded. |
|
118 |
self assert:(decoded = bytes). |
|
119 |
] |
|
120 |
] |
|
121 |
]. |
|
122 |
[exEnd] |
|
1963 | 123 |
" |
124 |
! ! |
|
125 |
||
126 |
!Base32Coder class methodsFor:'initialization'! |
|
127 |
||
3546
85cad7a4c32b
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2326
diff
changeset
|
128 |
initializeMappings |
1963 | 129 |
"initialize class variables" |
3546
85cad7a4c32b
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2326
diff
changeset
|
130 |
|
85cad7a4c32b
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2326
diff
changeset
|
131 |
Base32Mapping isNil ifTrue:[ |
85cad7a4c32b
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2326
diff
changeset
|
132 |
"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
|
133 |
Base32Mapping := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567='. |
4742 | 134 |
Base32ReverseMapping := self reverseMappingFor:Base32Mapping. |
1963 | 135 |
]. |
136 |
||
137 |
" |
|
4742 | 138 |
Base32Mapping := nil. |
3546
85cad7a4c32b
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2326
diff
changeset
|
139 |
self initializeMappings |
1963 | 140 |
" |
4742 | 141 |
|
142 |
"Modified (comment): / 30-09-2018 / 15:39:55 / Claus Gittinger" |
|
143 |
! |
|
144 |
||
145 |
mapping |
|
146 |
^ Base32Mapping |
|
147 |
||
148 |
"Created: / 30-09-2018 / 15:31:00 / Claus Gittinger" |
|
149 |
! |
|
150 |
||
151 |
reverseMapping |
|
152 |
^ Base32ReverseMapping |
|
153 |
||
154 |
"Created: / 30-09-2018 / 15:30:54 / Claus Gittinger" |
|
1963 | 155 |
! ! |
156 |
||
157 |
!Base32Coder methodsFor:'encoding'! |
|
158 |
||
3762 | 159 |
nextPutByte:aByte |
1963 | 160 |
"encode aByte on the output stream" |
161 |
||
162 |
"RFC 2045 says: max 76 characters in one line" |
|
163 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
|
164 |
stream cr. |
|
165 |
charCount := 0. |
|
166 |
]. |
|
167 |
||
3762 | 168 |
buffer := (buffer bitShift:8) bitOr:aByte. |
1963 | 169 |
bits := bits + 8 - 5. "max value of bits = 4 + 8 - 5" |
170 |
||
171 |
bits >= 5 ifTrue:[ |
|
172 |
"enough bits to write two chars: write the first 5 bits" |
|
4742 | 173 |
stream nextPut:(mapping at:(buffer rightShift:bits)+1). |
1963 | 174 |
"clear the first 5 bits" |
175 |
buffer := buffer bitAnd:(1 bitShift:bits)-1. |
|
176 |
charCount := charCount + 1. |
|
177 |
bits := bits - 5. |
|
178 |
]. |
|
179 |
||
180 |
"write 5 bits" |
|
4742 | 181 |
stream nextPut:(mapping at:(buffer rightShift:bits)+1). |
1963 | 182 |
"clear the first 5 bits" |
183 |
buffer := buffer bitAnd:(1 bitShift:bits)-1. |
|
184 |
charCount := charCount + 1. |
|
4742 | 185 |
|
186 |
"Modified: / 30-09-2018 / 15:32:16 / Claus Gittinger" |
|
1963 | 187 |
! ! |
188 |
||
189 |
!Base32Coder methodsFor:'misc'! |
|
190 |
||
191 |
flush |
|
192 |
"flush the remaining bits of buffer. |
|
193 |
The number of bits in buffer is not a multiple of 6, so we pad |
|
194 |
the buffer and signal that padding has been done via $= characters." |
|
195 |
||
196 |
|shift| |
|
197 |
||
198 |
bits == 0 ifTrue:[ |
|
199 |
"buffer is empty, nothing to do" |
|
200 |
^ self. |
|
201 |
]. |
|
202 |
||
203 |
"RFC 2045 says: max 76 characters in one line" |
|
204 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
|
205 |
stream cr. |
|
206 |
charCount := 0. |
|
207 |
]. |
|
208 |
||
209 |
shift := 5 - bits. |
|
210 |
buffer := buffer bitShift:shift. |
|
211 |
||
4742 | 212 |
stream nextPut:(mapping at:buffer+1). |
1963 | 213 |
bits := bits + 3. |
214 |
[ |
|
215 |
"simulate adding 8 bits and removing 5 bits, until we get a multiple of 8" |
|
216 |
stream nextPut:$=. |
|
217 |
bits := bits + 8 - 5. |
|
218 |
] doUntil:[bits \\ 8 == 0]. |
|
219 |
||
220 |
buffer := bits := 0. |
|
4742 | 221 |
|
222 |
"Modified: / 30-09-2018 / 15:32:08 / Claus Gittinger" |
|
1963 | 223 |
! ! |
224 |
||
225 |
!Base32Coder methodsFor:'private'! |
|
226 |
||
227 |
fillBuffer |
|
228 |
"fill buffer with next 8 characters each representing 5 bits" |
|
229 |
||
1974
40ac9e5f036c
changed #fillBuffer - remove explicit type SmallInteger
Stefan Vogel <sv@exept.de>
parents:
1963
diff
changeset
|
230 |
|b shift tempBuffer| |
1963 | 231 |
|
232 |
tempBuffer := 0. |
|
233 |
bits := 0. |
|
234 |
[ |
|
235 |
"read next valid Base64 character, skip invalid characters" |
|
236 |
[ |
|
237 |
b := stream next. |
|
238 |
b isNil ifTrue:[ "end of stream" |
|
239 |
b := 32. "simulate end-mark" |
|
240 |
] ifFalse:[ |
|
4742 | 241 |
b := reverseMapping at:b codePoint ifAbsent:255. |
1963 | 242 |
] |
243 |
] doWhile:[b == 255]. |
|
244 |
||
245 |
b == 32 ifTrue:[ |
|
246 |
"got $=, end of Base32 string has been reached. |
|
247 |
Strip fill bits" |
|
248 |
atEnd := true. |
|
249 |
bits > 8 ifTrue:[ |
|
250 |
shift := bits \\ 8. |
|
251 |
tempBuffer := tempBuffer rightShift:shift. |
|
252 |
bits := bits - shift. |
|
253 |
]. |
|
254 |
] ifFalse:[ |
|
255 |
"got valid Base64 character, append to buffer" |
|
256 |
tempBuffer := (tempBuffer bitShift:5) bitOr:b. |
|
257 |
bits := bits + 5. |
|
258 |
]. |
|
259 |
] doUntil:[bits == 40 or:[atEnd]]. |
|
260 |
||
261 |
buffer := tempBuffer. |
|
4742 | 262 |
|
263 |
"Modified: / 30-09-2018 / 15:43:20 / Claus Gittinger" |
|
1963 | 264 |
! ! |
265 |
||
266 |
!Base32Coder class methodsFor:'documentation'! |
|
267 |
||
268 |
version |
|
3762 | 269 |
^ '$Header$' |
2326 | 270 |
! |
271 |
||
272 |
version_CVS |
|
3762 | 273 |
^ '$Header$' |
1963 | 274 |
! ! |
275 |