author | chzeiher |
Tue, 27 Nov 2018 16:31:23 +0100 | |
changeset 4770 | ff398f221e71 |
parent 4752 | 5392065bde5a |
child 4839 | dfb7f7db7b54 |
permissions | -rw-r--r-- |
4741 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1965 | 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 |
||
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
16 |
"{ NameSpace: Smalltalk }" |
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
17 |
|
1965 | 18 |
ObjectCoder subclass:#BaseNCoder |
4741 | 19 |
instanceVariableNames:'buffer bits charCount peekByte atEnd lineLimit mapping |
20 |
reverseMapping' |
|
1965 | 21 |
classVariableNames:'' |
22 |
poolDictionaries:'' |
|
23 |
category:'System-Storage' |
|
24 |
! |
|
25 |
||
26 |
!BaseNCoder class methodsFor:'documentation'! |
|
27 |
||
28 |
copyright |
|
29 |
" |
|
30 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
31 |
All Rights Reserved |
|
32 |
||
33 |
This software is furnished under a license and may be used |
|
34 |
only in accordance with the terms of that license and with the |
|
35 |
inclusion of the above copyright notice. This software may not |
|
36 |
be provided or otherwise made available to, or used by, any |
|
37 |
other person. No title to or ownership of the software is |
|
38 |
hereby transferred. |
|
39 |
" |
|
40 |
! |
|
41 |
||
42 |
documentation |
|
43 |
" |
|
44 |
Abstract superclass of Base64Coder and Base32Coder |
|
4278 | 45 |
Their main entry point API is |
46 |
<BaseNCoder> encode:aStringOrBytes |
|
47 |
and |
|
48 |
<BaseNCoder> decode:aString |
|
1965 | 49 |
|
4278 | 50 |
If the decoder should return a string, use |
51 |
<BaseNCoder> decodeAsString:aString. |
|
52 |
||
53 |
[examples:] |
|
54 |
Base64Coder encode:'helloWorld' |
|
55 |
||
56 |
Base64Coder decode:'aGVsbG9Xb3JsZA==' |
|
57 |
||
58 |
Base64Coder decodeAsString:'aGVsbG9Xb3JsZA==' |
|
59 |
||
1965 | 60 |
[author:] |
61 |
Stefan Vogel (stefan@zwerg) |
|
62 |
||
63 |
[instance variables:] |
|
64 |
buffer SmallInteger buffered data |
|
65 |
bits SmallInteger Number of valid bits in buffer |
|
66 |
charCount SmallInteger Number of characters since last cr |
|
67 |
atEnd Boolean true if end of Base64 string reached |
|
68 |
||
69 |
[class variables:] |
|
70 |
||
71 |
[see also:] |
|
72 |
||
73 |
" |
|
74 |
! ! |
|
75 |
||
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
76 |
!BaseNCoder class methodsFor:'initialization'! |
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
77 |
|
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
78 |
initializeMappings |
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
79 |
self subclassResponsibility |
4741 | 80 |
|
81 |
" |
|
82 |
self withAllSubclassesDo:#initialize |
|
83 |
" |
|
84 |
||
85 |
"Modified (comment): / 30-09-2018 / 15:39:16 / Claus Gittinger" |
|
86 |
! |
|
87 |
||
88 |
mapping |
|
89 |
self subclassResponsibility |
|
90 |
||
91 |
"Created: / 30-09-2018 / 15:30:08 / Claus Gittinger" |
|
92 |
! |
|
93 |
||
94 |
reverseMapping |
|
95 |
self subclassResponsibility |
|
96 |
||
97 |
"Created: / 30-09-2018 / 15:30:11 / Claus Gittinger" |
|
98 |
! |
|
99 |
||
100 |
reverseMappingFor:mapping |
|
101 |
"initialize class variables" |
|
102 |
||
103 |
|revMapping| |
|
104 |
||
105 |
revMapping := ByteArray new:128 withAll:255. |
|
106 |
mapping keysAndValuesDo:[:idx :char| |
|
107 |
revMapping at:char codePoint put:idx-1. |
|
108 |
]. |
|
109 |
^ revMapping |
|
110 |
||
111 |
"Created: / 30-09-2018 / 15:34:37 / Claus Gittinger" |
|
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
112 |
! ! |
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
113 |
|
1965 | 114 |
!BaseNCoder class methodsFor:'instance creation'! |
115 |
||
116 |
new |
|
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
117 |
self initializeMappings. |
1965 | 118 |
^ self basicNew initialize |
119 |
! ! |
|
120 |
||
2090 | 121 |
!BaseNCoder class methodsFor:'decoding'! |
122 |
||
123 |
decodeAsString:encodedString |
|
124 |
"decode a base-n encoded string. |
|
125 |
We already expect a string instead of a ByteArray" |
|
126 |
||
127 |
^ (self on:encodedString readStream) stringUpToEnd |
|
128 |
! ! |
|
129 |
||
4310 | 130 |
!BaseNCoder class methodsFor:'queries'! |
131 |
||
132 |
isAbstract |
|
133 |
"Return if this class is an abstract class. |
|
134 |
True is returned here for myself only; false for subclasses. |
|
135 |
Abstract subclasses must redefine this again." |
|
136 |
||
137 |
^ self == BaseNCoder. |
|
138 |
! ! |
|
139 |
||
1965 | 140 |
!BaseNCoder methodsFor:'accessing'! |
141 |
||
142 |
lineLimit:something |
|
143 |
"set the line length of the encoded output. |
|
144 |
Default is a line length of 76 characters. |
|
145 |
||
146 |
If nil, no line breaks will be done." |
|
147 |
||
148 |
lineLimit := something. |
|
149 |
! ! |
|
150 |
||
151 |
!BaseNCoder methodsFor:'decoding'! |
|
152 |
||
153 |
next |
|
154 |
"answer the next decoded byte" |
|
155 |
||
156 |
|b| |
|
157 |
||
158 |
peekByte notNil ifTrue:[ |
|
159 |
b := peekByte. |
|
160 |
peekByte := nil. |
|
161 |
^ b |
|
162 |
]. |
|
163 |
^ self basicNext. |
|
164 |
! |
|
165 |
||
2638 | 166 |
next:count |
167 |
"return the next count bytes of the stream as ByteArray" |
|
168 |
||
169 |
|answerStream |
|
170 |
cnt "{ Class: SmallInteger }" | |
|
171 |
||
172 |
cnt := count. |
|
173 |
answerStream := WriteStream on:(ByteArray new:cnt). |
|
174 |
answerStream signalAtEnd:true. |
|
175 |
1 to:cnt do:[:index | |
|
176 |
|next| |
|
177 |
||
178 |
next := self next. |
|
179 |
next isNil ifTrue:[ |
|
180 |
"if next did not raise EndOfStreamError, we have to do it" |
|
181 |
EndOfStreamError raiseRequestFrom:self. |
|
182 |
"if you proceed, you get what we have already collected" |
|
183 |
^ answerStream contents |
|
184 |
]. |
|
185 |
answerStream nextPut:next. |
|
186 |
]. |
|
187 |
^ answerStream contents |
|
188 |
! |
|
189 |
||
1965 | 190 |
peek |
191 |
"answer the next decoded byte. Do not consume this byte" |
|
192 |
||
193 |
peekByte isNil ifTrue:[ |
|
194 |
peekByte := self basicNext. |
|
195 |
]. |
|
196 |
^ peekByte |
|
197 |
! ! |
|
198 |
||
199 |
!BaseNCoder methodsFor:'encoding'! |
|
200 |
||
201 |
visitByteArray:aByteArray with:aParameter |
|
202 |
^ self |
|
3764 | 203 |
nextPutBytes:aByteArray; |
1965 | 204 |
flush. |
205 |
||
206 |
" |
|
3764 | 207 |
Base64Coder encodingOf:#[1 2 3 4 5 6 255] |
1965 | 208 |
" |
209 |
! |
|
210 |
||
211 |
visitObject:anObject with:aParameter |
|
212 |
"not defined. Use nextPut or nextPutAll:. |
|
213 |
Could encode the printString here" |
|
214 |
||
215 |
^ self shouldNotImplement |
|
216 |
! |
|
217 |
||
2160 | 218 |
visitStream:aStream with:aParameter |
3764 | 219 |
aStream copyToEndInto:self. |
2160 | 220 |
self flush. |
221 |
||
222 |
" |
|
223 |
Base64Coder encodingOf:#[1 2 3 4 5 6 255] |
|
224 |
Base64Coder encodingOf:#[1 2 3 4 5 6 255] readStream |
|
225 |
" |
|
226 |
! |
|
227 |
||
1965 | 228 |
visitString:aString with:aParameter |
229 |
^ self |
|
3764 | 230 |
nextPutAll:aString; |
1965 | 231 |
flush. |
232 |
||
233 |
" |
|
234 |
|encoded decoded decoder| |
|
235 |
||
3764 | 236 |
encoded := Base64Coder encode:'hello world'. |
1965 | 237 |
decoded := #[] writeStream. |
238 |
decoder := Base64Coder on:encoded readStream. |
|
239 |
[decoder atEnd] whileFalse:[ |
|
240 |
decoded nextPut:(decoder next). |
|
241 |
]. |
|
242 |
decoded := decoded contents. |
|
243 |
decoded asString. |
|
244 |
" |
|
245 |
! ! |
|
246 |
||
247 |
!BaseNCoder methodsFor:'initialization'! |
|
248 |
||
249 |
emptyWriteStream |
|
250 |
"answer an empty stream. We encode as string" |
|
251 |
||
252 |
^ WriteStream on:(String new:64) |
|
253 |
! |
|
254 |
||
255 |
initialize |
|
256 |
||
4305 | 257 |
<modifier: #super> "must be called if redefined" |
258 |
||
1965 | 259 |
buffer := bits := charCount := 0. |
260 |
lineLimit := 76. "RFC 2045 says: max 76 characters in one line" |
|
261 |
atEnd := false. |
|
4305 | 262 |
|
4741 | 263 |
mapping := self class mapping. |
264 |
reverseMapping := self class reverseMapping. |
|
265 |
||
4305 | 266 |
"Modified: / 08-02-2017 / 00:33:07 / cg" |
4741 | 267 |
"Modified: / 30-09-2018 / 15:29:41 / Claus Gittinger" |
1965 | 268 |
! ! |
269 |
||
270 |
!BaseNCoder methodsFor:'misc'! |
|
271 |
||
272 |
reset |
|
273 |
"reset to initial state" |
|
274 |
||
275 |
super reset. |
|
276 |
buffer := bits := charCount := 0. |
|
277 |
atEnd := false. |
|
278 |
peekByte := nil. |
|
279 |
! ! |
|
280 |
||
281 |
!BaseNCoder methodsFor:'private'! |
|
282 |
||
283 |
basicNext |
|
284 |
"answer the next decoded byte. |
|
285 |
No peekByte handling is done here." |
|
286 |
||
287 |
|b| |
|
288 |
||
289 |
bits == 0 ifTrue:[ |
|
290 |
self fillBuffer. |
|
291 |
bits == 0 ifTrue:[ |
|
292 |
^ stream pastEndRead. |
|
293 |
] |
|
294 |
]. |
|
295 |
b := (buffer bitShift:(8 - bits)) bitAnd:16rFF. |
|
296 |
bits := bits - 8. |
|
297 |
||
298 |
^ b. |
|
299 |
! ! |
|
300 |
||
301 |
!BaseNCoder methodsFor:'queries'! |
|
302 |
||
303 |
atEnd |
|
304 |
"answer true, if no more bytes can be read" |
|
305 |
||
306 |
bits == 0 ifTrue:[ |
|
307 |
atEnd ifTrue:[^ true]. |
|
308 |
self fillBuffer. |
|
309 |
bits == 0 ifTrue:[^ true]. |
|
310 |
]. |
|
311 |
^ false. |
|
312 |
! |
|
313 |
||
314 |
binary |
|
315 |
"switch to binary mode - nothing is done here. |
|
316 |
Defined for compatibility with ExternalStream." |
|
317 |
||
318 |
^ self |
|
319 |
! |
|
320 |
||
321 |
isStream |
|
322 |
"we simulate a stream" |
|
323 |
||
324 |
^ true |
|
325 |
! ! |
|
326 |
||
327 |
!BaseNCoder methodsFor:'stream compatibility'! |
|
328 |
||
329 |
nextBytesInto:anObject startingAt:offset |
|
330 |
"copy bytes into anObject starting at offset" |
|
331 |
||
332 |
|off| |
|
333 |
||
334 |
off := offset. |
|
335 |
[self atEnd] whileFalse:[ |
|
336 |
anObject at:off put:self next. |
|
337 |
off := off + 1. |
|
338 |
]. |
|
339 |
^ off - offset |
|
340 |
! |
|
341 |
||
3764 | 342 |
nextPut:aByte |
343 |
"encode aByte on the output stream" |
|
344 |
||
345 |
^ self nextPutByte:aByte asInteger. |
|
346 |
! |
|
347 |
||
3760 | 348 |
nextPutAll:aCollection startingAt:first to:last |
349 |
"append the elements with index from first to last |
|
3761 | 350 |
of the argument, aCollection onto the receiver." |
3760 | 351 |
|
352 |
aCollection from:first to:last do:[:element | |
|
3764 | 353 |
self nextPutByte:element |
3760 | 354 |
]. |
355 |
^ aCollection |
|
356 |
! |
|
357 |
||
3764 | 358 |
nextPutBytes:aCollectionOfBytes |
359 |
"encode all objects from the argument" |
|
360 |
||
361 |
aCollectionOfBytes do:[:o | |
|
362 |
self nextPutByte:o |
|
363 |
] |
|
364 |
! |
|
365 |
||
2090 | 366 |
stringUpToEnd |
367 |
"return a collection of the elements up-to the end" |
|
368 |
||
369 |
|answerStream| |
|
370 |
||
371 |
answerStream := WriteStream on:(String new:128). |
|
372 |
peekByte notNil ifTrue:[ |
|
373 |
answerStream nextPut:(Character codePoint:peekByte). |
|
374 |
peekByte := nil. |
|
375 |
]. |
|
376 |
[ |
|
377 |
[bits >= 8] whileTrue:[ |
|
378 |
answerStream nextPut:(Character codePoint:((buffer bitShift:(8 - bits)) bitAnd:16rFF)). |
|
379 |
bits := bits - 8. |
|
380 |
]. |
|
381 |
atEnd ifTrue:[ |
|
382 |
bits ~~ 0 ifTrue:[ |
|
383 |
answerStream nextPut:(Character codePoint:(buffer bitAnd:16rFF)). |
|
384 |
bits := 0. |
|
385 |
] |
|
386 |
] ifFalse:[ |
|
387 |
self fillBuffer. |
|
388 |
]. |
|
389 |
] doWhile:[bits > 0]. |
|
390 |
||
391 |
^ answerStream contents |
|
392 |
! |
|
393 |
||
1965 | 394 |
upToEnd |
395 |
"return a collection of the elements up-to the end" |
|
396 |
||
397 |
|answerStream| |
|
398 |
||
399 |
answerStream := WriteStream on:(ByteArray new:128). |
|
400 |
peekByte notNil ifTrue:[ |
|
401 |
answerStream nextPut:peekByte. |
|
402 |
peekByte := nil. |
|
403 |
]. |
|
404 |
[ |
|
405 |
[bits >= 8] whileTrue:[ |
|
406 |
answerStream nextPut:((buffer bitShift:(8 - bits)) bitAnd:16rFF). |
|
407 |
bits := bits - 8. |
|
408 |
]. |
|
4752 | 409 |
atEnd ifFalse:[ |
1965 | 410 |
self fillBuffer. |
411 |
]. |
|
412 |
] doWhile:[bits > 0]. |
|
413 |
||
414 |
^ answerStream contents |
|
4752 | 415 |
|
416 |
"Modified: / 30-09-2018 / 16:37:03 / Claus Gittinger" |
|
1965 | 417 |
! ! |
418 |
||
419 |
!BaseNCoder methodsFor:'subclass responsibility'! |
|
420 |
||
421 |
fillBuffer |
|
422 |
"fill buffer with next n characters each representing m bits" |
|
423 |
||
424 |
^ self subclassResponsibility. |
|
425 |
! |
|
426 |
||
427 |
flush |
|
428 |
"flush the remaining bits of buffer. |
|
429 |
The number of bits in buffer is not a multiple of m, so we pad |
|
430 |
the buffer and signal that padding has been done via $= characters." |
|
431 |
||
432 |
^ self subclassResponsibility. |
|
433 |
! |
|
434 |
||
3764 | 435 |
nextPutByte:aByte |
1965 | 436 |
"encode aByte on the output stream" |
437 |
||
438 |
^ self subclassResponsibility. |
|
439 |
! ! |
|
440 |
||
441 |
!BaseNCoder class methodsFor:'documentation'! |
|
442 |
||
443 |
version |
|
3760 | 444 |
^ '$Header$' |
3761 | 445 |
! |
446 |
||
447 |
version_CVS |
|
448 |
^ '$Header$' |
|
1965 | 449 |
! ! |
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
450 |