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