1044
|
1 |
"{ Package: 'ca:Compress' }"
|
|
2 |
|
|
3 |
Object subclass:#ZipStream
|
|
4 |
instanceVariableNames:'onStream hitEOF binary isReadOpen inputBytes outputBytes zstream'
|
|
5 |
classVariableNames:'Z_FINISH Z_NO_FLUSH Z_SYNC_FLUSH Z_FULL_FLUSH Z_DEFLATED
|
|
6 |
Z_DEFAULT_COMPRESSION Z_DEFAULT_LEVEL Z_BEST_COMPRESSION
|
|
7 |
Z_DEF_MEM_LEVEL Z_DEFAULT_STRATEGY Z_FILTERED Z_HUFFMAN_ONLY
|
|
8 |
HEAD_OS_CODE HEAD_RESERVED HEAD_EXTRA_FIELD HEAD_ORIG_NAME
|
|
9 |
HEAD_COMMENT HEAD_CRC GZ_MAGIC_ID'
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'A-Compress'
|
|
12 |
!
|
|
13 |
|
1045
|
14 |
!ZipStream primitiveDefinitions!
|
|
15 |
%{
|
|
16 |
|
|
17 |
/*
|
|
18 |
* includes, defines, structure definitions
|
|
19 |
* and typedefs come here.
|
|
20 |
*/
|
|
21 |
|
|
22 |
#include "compress/zlib.h"
|
|
23 |
#include "compress/zutil.h"
|
|
24 |
|
|
25 |
typedef struct {
|
1046
|
26 |
z_stream stream;
|
|
27 |
Bytef * in_ref;
|
|
28 |
uLong in_total;
|
1045
|
29 |
|
1046
|
30 |
Bytef * out_ref;
|
|
31 |
uLong out_used;
|
|
32 |
uLong out_total;
|
1045
|
33 |
|
1046
|
34 |
uLong crc_32;
|
1045
|
35 |
} zstream_s;
|
|
36 |
|
|
37 |
%}
|
|
38 |
! !
|
|
39 |
|
|
40 |
|
|
41 |
!ZipStream class methodsFor:'initialization'!
|
|
42 |
|
|
43 |
initialize
|
|
44 |
|varZ_NO_FLUSH varZ_SYNC_FLUSH varZ_FULL_FLUSH varZ_FINISH varZ_DEFLATED
|
|
45 |
varZ_DEFAULT_COMPRESSION varZ_BEST_COMPRESSION varZ_DEF_MEM_LEVEL
|
|
46 |
varZ_DEFAULT_STRATEGY varZ_FILTERED varZ_HUFFMAN_ONLY
|
|
47 |
varHEAD_OS_CODE
|
|
48 |
|
|
|
49 |
%{
|
|
50 |
varZ_NO_FLUSH = __MKSMALLINT( Z_NO_FLUSH );
|
|
51 |
varZ_SYNC_FLUSH = __MKSMALLINT( Z_SYNC_FLUSH );
|
|
52 |
varZ_FULL_FLUSH = __MKSMALLINT( Z_FULL_FLUSH );
|
|
53 |
varZ_FINISH = __MKSMALLINT( Z_FINISH );
|
|
54 |
|
|
55 |
varZ_DEFLATED = __MKSMALLINT( Z_DEFLATED );
|
|
56 |
|
|
57 |
varZ_DEFAULT_COMPRESSION = __MKSMALLINT( Z_DEFAULT_COMPRESSION );
|
|
58 |
varZ_BEST_COMPRESSION = __MKSMALLINT( Z_BEST_COMPRESSION );
|
|
59 |
|
|
60 |
varZ_DEF_MEM_LEVEL = __MKSMALLINT( DEF_MEM_LEVEL );
|
|
61 |
|
|
62 |
varZ_DEFAULT_STRATEGY = __MKSMALLINT( Z_DEFAULT_STRATEGY );
|
|
63 |
varZ_FILTERED = __MKSMALLINT( Z_FILTERED );
|
|
64 |
varZ_HUFFMAN_ONLY = __MKSMALLINT( Z_HUFFMAN_ONLY );
|
|
65 |
varHEAD_OS_CODE = __MKSMALLINT( OS_CODE );
|
|
66 |
%}.
|
|
67 |
|
|
68 |
Z_NO_FLUSH := varZ_NO_FLUSH.
|
|
69 |
Z_SYNC_FLUSH := varZ_SYNC_FLUSH.
|
|
70 |
Z_FULL_FLUSH := varZ_FULL_FLUSH.
|
|
71 |
Z_FINISH := varZ_FINISH.
|
|
72 |
|
|
73 |
Z_DEFLATED := varZ_DEFLATED.
|
|
74 |
|
|
75 |
Z_DEFAULT_COMPRESSION := varZ_DEFAULT_COMPRESSION.
|
|
76 |
Z_DEFAULT_LEVEL := 6.
|
|
77 |
Z_BEST_COMPRESSION := varZ_BEST_COMPRESSION.
|
|
78 |
|
|
79 |
Z_DEF_MEM_LEVEL := varZ_DEF_MEM_LEVEL.
|
|
80 |
|
|
81 |
Z_DEFAULT_STRATEGY := varZ_DEFAULT_STRATEGY.
|
|
82 |
Z_FILTERED := varZ_FILTERED.
|
|
83 |
Z_HUFFMAN_ONLY := varZ_HUFFMAN_ONLY.
|
|
84 |
|
|
85 |
HEAD_OS_CODE := varHEAD_OS_CODE.
|
|
86 |
|
|
87 |
HEAD_RESERVED := 16rE0. " bits 5..7: reserved "
|
|
88 |
HEAD_EXTRA_FIELD := 16r04. " bit 2 set: extra field present "
|
|
89 |
HEAD_ORIG_NAME := 16r08. " bit 3 set: original file name present "
|
|
90 |
HEAD_COMMENT := 16r10. " bit 4 set: file comment present "
|
|
91 |
HEAD_CRC := 16r02. " bit 1 set: header CRC present "
|
|
92 |
|
|
93 |
GZ_MAGIC_ID := #[ 16r1f 16r8b ]
|
|
94 |
|
|
95 |
! !
|
|
96 |
|
|
97 |
|
|
98 |
!ZipStream class methodsFor:'instance creation'!
|
|
99 |
|
1046
|
100 |
readOpenOn:aStream
|
|
101 |
^ self new readOpenOn:aStream
|
|
102 |
!
|
|
103 |
|
1045
|
104 |
writeOpenOn:aStream
|
|
105 |
^ self writeOpenOn:aStream level:Z_DEFAULT_LEVEL
|
1046
|
106 |
memLevel:Z_DEF_MEM_LEVEL
|
|
107 |
strategy:Z_DEFAULT_STRATEGY.
|
1045
|
108 |
!
|
|
109 |
|
|
110 |
writeOpenOn:aStream level:level memLevel:memLevel strategy:strategy
|
|
111 |
|z|
|
|
112 |
|
|
113 |
z := self new.
|
|
114 |
z writeOpenOn:aStream level:level memLevel:memLevel strategy:strategy.
|
|
115 |
^ z
|
|
116 |
! !
|
|
117 |
|
|
118 |
|
|
119 |
!ZipStream class methodsFor:'test'!
|
|
120 |
|
|
121 |
test
|
|
122 |
"
|
|
123 |
ZipStream test
|
|
124 |
"
|
1046
|
125 |
|fileContents compContents uncompContents in zip out|
|
1045
|
126 |
|
1046
|
127 |
fileContents := 'smalltalk.rc' asFilename contentsOfEntireFile.
|
1045
|
128 |
|
1046
|
129 |
in := fileContents readStream.
|
|
130 |
out := #[] writeStream.
|
1045
|
131 |
|
|
132 |
[ zip := self writeOpenOn:out.
|
|
133 |
|
1046
|
134 |
[in atEnd] whileFalse:[ |buf|
|
|
135 |
buf := in nextAvailable:512.
|
|
136 |
zip nextPutAll:buf.
|
1045
|
137 |
].
|
1046
|
138 |
] valueNowOrOnUnwindDo:[ zip ifNotNil:[ zip close ] ].
|
1045
|
139 |
|
1046
|
140 |
compContents := out contents.
|
|
141 |
in := compContents readStream.
|
1045
|
142 |
|
1046
|
143 |
[ zip := self readOpenOn:in.
|
|
144 |
uncompContents := zip contents.
|
|
145 |
] valueNowOrOnUnwindDo:[ zip ifNotNil:[ zip close ] ].
|
1045
|
146 |
|
1046
|
147 |
fileContents = uncompContents ifFalse:[
|
|
148 |
self halt:'contents differs'.
|
|
149 |
^ self
|
|
150 |
].
|
|
151 |
Transcript showCR:'uncompressed size: ', fileContents size printString.
|
|
152 |
Transcript showCR:'compressed size: ', compContents size printString.
|
1045
|
153 |
|
1046
|
154 |
Transcript showCR:'OK'.
|
1045
|
155 |
|
|
156 |
! !
|
|
157 |
|
|
158 |
!ZipStream methodsFor:'accessing'!
|
|
159 |
|
|
160 |
binary
|
|
161 |
"switch to binary mode - default is text
|
|
162 |
"
|
|
163 |
binary := true.
|
|
164 |
!
|
|
165 |
|
|
166 |
text
|
|
167 |
"switch to text mode - default is text
|
|
168 |
"
|
|
169 |
binary := false.
|
|
170 |
! !
|
|
171 |
|
|
172 |
!ZipStream methodsFor:'error handling'!
|
|
173 |
|
|
174 |
errorNotOpen
|
|
175 |
self zerror:( self class name , ' not open' ).
|
|
176 |
!
|
|
177 |
|
|
178 |
invalidArgument
|
|
179 |
self zerror:( 'invalid argument' ).
|
|
180 |
!
|
|
181 |
|
|
182 |
pastEnd
|
|
183 |
self zerror:( 'end of stream' ).
|
|
184 |
!
|
|
185 |
|
|
186 |
zerror:anErrorOrNumber
|
|
187 |
|
|
188 |
onStream := nil.
|
|
189 |
|
|
190 |
self zclose.
|
|
191 |
self error:( 'error: ', anErrorOrNumber printString ).
|
|
192 |
! !
|
|
193 |
|
|
194 |
!ZipStream methodsFor:'low level'!
|
|
195 |
|
|
196 |
zclose
|
|
197 |
|
|
198 |
onStream := nil.
|
|
199 |
hitEOF := true.
|
|
200 |
zstream ifNil:[^ self].
|
|
201 |
|
|
202 |
%{
|
|
203 |
OBJ _zstreamObj = __INST( zstream );
|
|
204 |
|
|
205 |
if( _zstreamObj != nil )
|
|
206 |
{
|
1046
|
207 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
208 |
|
1046
|
209 |
__INST(zstream) = nil;
|
1045
|
210 |
|
1046
|
211 |
if( _zstream->stream.state != NULL )
|
|
212 |
{
|
|
213 |
if( __INST(isReadOpen) == true )
|
|
214 |
inflateEnd( & _zstream->stream );
|
|
215 |
else
|
|
216 |
deflateEnd( & _zstream->stream );
|
|
217 |
}
|
|
218 |
free( _zstream );
|
1045
|
219 |
}
|
|
220 |
%}.
|
|
221 |
zstream := nil.
|
|
222 |
!
|
|
223 |
|
|
224 |
zdeflate:aLevel
|
|
225 |
|errorNo|
|
|
226 |
|
|
227 |
errorNo := nil.
|
|
228 |
|
|
229 |
%{
|
|
230 |
OBJ _zstreamObj = __INST( zstream );
|
|
231 |
|
|
232 |
if( (_zstreamObj != nil) && (__isSmallInteger(aLevel)) )
|
|
233 |
{
|
1046
|
234 |
int _errorNo, _level;
|
|
235 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
236 |
|
1046
|
237 |
_level = __intVal( aLevel );
|
|
238 |
_errorNo = deflate( & _zstream->stream, __intVal(aLevel) );
|
1045
|
239 |
|
1046
|
240 |
if( _errorNo == Z_STREAM_END ) RETURN( true );
|
|
241 |
if( _errorNo == Z_OK ) RETURN( false );
|
1045
|
242 |
|
1046
|
243 |
errorNo = __MKSMALLINT( _errorNo );
|
1045
|
244 |
}
|
|
245 |
%}.
|
|
246 |
errorNo ifNil:[
|
1046
|
247 |
zstream ifNil:[self errorNotOpen].
|
|
248 |
self invalidArguments.
|
1045
|
249 |
].
|
|
250 |
self zerror:errorNo.
|
|
251 |
!
|
|
252 |
|
|
253 |
zdeflateEnd
|
|
254 |
|errorNo|
|
|
255 |
|
|
256 |
errorNo := nil.
|
|
257 |
|
|
258 |
%{
|
|
259 |
OBJ _zstreamObj = __INST( zstream );
|
|
260 |
|
|
261 |
if( _zstreamObj != nil )
|
|
262 |
{
|
1046
|
263 |
int _errorNo;
|
|
264 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
265 |
|
1046
|
266 |
_errorNo = deflateEnd( & _zstream->stream );
|
1045
|
267 |
|
1046
|
268 |
if( _errorNo == Z_OK )
|
|
269 |
RETURN( self );
|
1045
|
270 |
|
1046
|
271 |
errorNo = __MKSMALLINT( _errorNo );
|
1045
|
272 |
}
|
|
273 |
%}.
|
|
274 |
errorNo ifNil:[ self errorNotOpen ].
|
|
275 |
self zerror:errorNo.
|
|
276 |
!
|
|
277 |
|
|
278 |
zdeflateInit:aLevel memLevel:memLevel strategy:strategy
|
|
279 |
|errorNo|
|
|
280 |
|
|
281 |
errorNo := nil.
|
|
282 |
|
|
283 |
%{
|
|
284 |
OBJ _zstreamObj = __INST( zstream );
|
|
285 |
|
|
286 |
if( (_zstreamObj != nil)
|
|
287 |
&& __isSmallInteger(aLevel)
|
|
288 |
&& __isSmallInteger(memLevel)
|
|
289 |
&& __isSmallInteger(strategy)
|
|
290 |
)
|
|
291 |
{
|
1046
|
292 |
int _errorNo;
|
|
293 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
294 |
|
1046
|
295 |
_errorNo = deflateInit2( & _zstream->stream
|
|
296 |
, __intVal( aLevel )
|
|
297 |
, Z_DEFLATED
|
|
298 |
, -MAX_WBITS
|
|
299 |
, __intVal( memLevel )
|
|
300 |
, __intVal( strategy )
|
|
301 |
);
|
1045
|
302 |
|
1046
|
303 |
if( _errorNo == Z_OK )
|
|
304 |
RETURN( self );
|
1045
|
305 |
|
1046
|
306 |
errorNo = __MKSMALLINT( _errorNo );
|
1045
|
307 |
}
|
|
308 |
%}.
|
|
309 |
errorNo ifNil:[
|
1046
|
310 |
zstream ifNil:[ self errorNotOpen ].
|
|
311 |
self invalidArguments .
|
1045
|
312 |
].
|
|
313 |
self zerror:errorNo.
|
|
314 |
!
|
|
315 |
|
|
316 |
zflush:forced
|
|
317 |
|len|
|
|
318 |
|
|
319 |
len := 0.
|
|
320 |
|
|
321 |
%{
|
|
322 |
OBJ _zstreamObj = __INST( zstream );
|
|
323 |
|
|
324 |
if( _zstreamObj != nil )
|
|
325 |
{
|
1046
|
326 |
uInt _max, _len;
|
|
327 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
328 |
|
1046
|
329 |
_max = _zstream->out_total;
|
|
330 |
_len = _max - _zstream->stream.avail_out;
|
1045
|
331 |
|
1046
|
332 |
if( (_len == 0) || ((forced == false) && (_len != _max)) )
|
|
333 |
RETURN( __MKSMALLINT(0) );
|
1045
|
334 |
|
1046
|
335 |
_zstream->stream.avail_out = _max;
|
|
336 |
_zstream->stream.next_out = _zstream->out_ref;
|
1045
|
337 |
|
1046
|
338 |
len = __MKSMALLINT( _len );
|
1045
|
339 |
}
|
|
340 |
%}.
|
|
341 |
zstream ifNil:[ self errorNotOpen ].
|
|
342 |
self onStreamPutBytes:len from:outputBytes.
|
|
343 |
!
|
|
344 |
|
|
345 |
zgetAvailableOutBufferInto:inBytes startingAt:start
|
|
346 |
|
|
347 |
%{
|
|
348 |
OBJ _zstreamObj = __INST( zstream );
|
|
349 |
|
|
350 |
if( (_zstreamObj != nil) && __isSmallInteger(start) )
|
|
351 |
{
|
1046
|
352 |
char * _bytes;
|
|
353 |
zstream_s * _zstream;
|
|
354 |
int _count, _offs, _bsize;
|
1045
|
355 |
|
1046
|
356 |
if (__isBytes (inBytes)) {
|
|
357 |
_bytes = __ByteArrayInstPtr(inBytes)->ba_element;
|
|
358 |
_bsize = __byteArraySize( inBytes );
|
|
359 |
} else if ( __isString(inBytes) ) {
|
|
360 |
_bytes = __stringVal( inBytes );
|
|
361 |
_bsize = __stringSize( inBytes );
|
|
362 |
} else
|
|
363 |
goto bad;
|
1045
|
364 |
|
1046
|
365 |
_zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
366 |
_count = _zstream->out_used - _zstream->stream.avail_out;
|
1045
|
367 |
|
1046
|
368 |
_zstream->stream.avail_out = _zstream->out_used = 0;
|
|
369 |
_zstream->stream.next_out = Z_NULL;
|
1045
|
370 |
|
1046
|
371 |
if( _count < 1 ) { RETURN( __MKSMALLINT(0) ); }
|
1045
|
372 |
|
1046
|
373 |
_offs = __intVal( start ) - 1;
|
1045
|
374 |
|
1046
|
375 |
if( (_count + _offs) <= _bsize )
|
|
376 |
{
|
|
377 |
zmemcpy( (Bytef *) & _bytes[_offs], _zstream->out_ref, _count );
|
|
378 |
RETURN ( __MKSMALLINT(_count) );
|
|
379 |
}
|
1045
|
380 |
}
|
|
381 |
bad:;
|
|
382 |
%}.
|
|
383 |
zstream ifNil:[ self errorNotOpen ].
|
|
384 |
self invalidArguments .
|
|
385 |
!
|
|
386 |
|
|
387 |
zgetCrc32
|
|
388 |
|
|
389 |
%{
|
|
390 |
OBJ _zstreamObj = __INST( zstream );
|
|
391 |
|
|
392 |
if( _zstreamObj != nil )
|
|
393 |
{
|
1046
|
394 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
395 |
RETURN ( __MKUINT(_zstream->crc_32) );
|
1045
|
396 |
}
|
|
397 |
%}.
|
|
398 |
self errorNotOpen
|
|
399 |
!
|
|
400 |
|
|
401 |
zgetTotalIn
|
|
402 |
%{
|
|
403 |
OBJ _zstreamObj = __INST( zstream );
|
|
404 |
|
|
405 |
if( _zstreamObj != nil )
|
|
406 |
{
|
1046
|
407 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
408 |
RETURN ( __MKUINT(_zstream->stream.total_in) );
|
1045
|
409 |
}
|
|
410 |
%}.
|
|
411 |
self errorNotOpen
|
|
412 |
!
|
|
413 |
|
|
414 |
zinflate
|
|
415 |
|errorNo|
|
|
416 |
|
|
417 |
errorNo := nil.
|
|
418 |
|
|
419 |
%{
|
|
420 |
OBJ _zstreamObj = __INST( zstream );
|
|
421 |
|
|
422 |
if( _zstreamObj != nil )
|
|
423 |
{
|
1046
|
424 |
int _errorNo;
|
|
425 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
426 |
|
1046
|
427 |
_errorNo = inflate( & _zstream->stream, Z_NO_FLUSH );
|
1045
|
428 |
|
1046
|
429 |
if( _errorNo == Z_STREAM_END )
|
|
430 |
{
|
|
431 |
__INST(hitEOF) = true;
|
|
432 |
_errorNo = inflateEnd( & _zstream->stream );
|
|
433 |
}
|
|
434 |
if( _errorNo == Z_OK )
|
|
435 |
RETURN( self );
|
1045
|
436 |
|
1046
|
437 |
errorNo = __MKSMALLINT( _errorNo );
|
1045
|
438 |
}
|
|
439 |
%}.
|
|
440 |
errorNo ifNil:[ self errorNotOpen ].
|
|
441 |
self zerror:errorNo.
|
|
442 |
!
|
|
443 |
|
|
444 |
zinflateInit
|
|
445 |
|errorNo|
|
|
446 |
|
|
447 |
errorNo := nil.
|
|
448 |
|
|
449 |
%{
|
|
450 |
OBJ _zstreamObj = __INST( zstream );
|
|
451 |
|
|
452 |
if( _zstreamObj != nil )
|
|
453 |
{
|
1046
|
454 |
int _errorNo;
|
|
455 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
456 |
|
1046
|
457 |
_errorNo = inflateInit2( & _zstream->stream, -MAX_WBITS );
|
1045
|
458 |
|
1046
|
459 |
if( _errorNo == Z_OK )
|
|
460 |
RETURN( self );
|
1045
|
461 |
|
1046
|
462 |
errorNo = __MKSMALLINT( _errorNo );
|
1045
|
463 |
}
|
|
464 |
%}.
|
|
465 |
errorNo ifNil:[ self errorNotOpen ].
|
|
466 |
self zerror:errorNo.
|
|
467 |
!
|
|
468 |
zinputAvailable
|
|
469 |
%{
|
|
470 |
OBJ _zstreamObj = __INST( zstream );
|
|
471 |
|
|
472 |
if( _zstreamObj != nil )
|
|
473 |
{
|
1046
|
474 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
475 |
|
1046
|
476 |
if( _zstream->stream.avail_in != 0 )
|
|
477 |
RETURN( true );
|
1045
|
478 |
|
1046
|
479 |
RETURN( false );
|
1045
|
480 |
}
|
|
481 |
%}.
|
|
482 |
self errorNotOpen
|
|
483 |
!
|
|
484 |
|
|
485 |
zinputPosition:count
|
|
486 |
%{
|
|
487 |
OBJ _zstreamObj = __INST( zstream );
|
|
488 |
|
|
489 |
if( (_zstreamObj != nil) && __isSmallInteger(count) )
|
|
490 |
{
|
1046
|
491 |
int _count;
|
|
492 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
493 |
|
1046
|
494 |
_count = __intVal( count );
|
1045
|
495 |
|
1046
|
496 |
if( _count < 0 )
|
|
497 |
{
|
|
498 |
_zstream->stream.next_in = Z_NULL;
|
|
499 |
_count = 0;
|
|
500 |
}
|
|
501 |
else
|
|
502 |
_zstream->stream.next_in = _zstream->in_ref;
|
1045
|
503 |
|
1046
|
504 |
_zstream->stream.avail_in = _count;
|
|
505 |
RETURN( __MKSMALLINT (_count) );
|
1045
|
506 |
}
|
|
507 |
%}.
|
|
508 |
zstream ifNil:[ self errorNotOpen ].
|
|
509 |
self invalidArguments .
|
|
510 |
!
|
|
511 |
|
|
512 |
znextPutBytes:count from:inBytes startingAt:start
|
|
513 |
%{
|
|
514 |
OBJ _zstreamObj = __INST( zstream );
|
|
515 |
|
|
516 |
if( (_zstreamObj != nil) && __bothSmallInteger(count, start) )
|
|
517 |
{
|
1046
|
518 |
int _count, _offs, _bsize;
|
|
519 |
char * _bytes;
|
|
520 |
Bytef * _in_ref;
|
|
521 |
zstream_s * _zstream;
|
1045
|
522 |
|
1046
|
523 |
_zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
524 |
_in_ref = _zstream->in_ref;
|
1045
|
525 |
|
1046
|
526 |
if( (_count = __intVal (count)) <= 0 )
|
|
527 |
{
|
|
528 |
uInt _b;
|
1045
|
529 |
|
1046
|
530 |
if( _count < 0 ) goto bad;
|
1045
|
531 |
|
1046
|
532 |
if( __isCharacter(inBytes) )
|
|
533 |
_b = __intVal(__characterVal(inBytes)) & 0xFF;
|
|
534 |
else if( __isSmallInteger(inBytes) )
|
|
535 |
{
|
|
536 |
_b = __intVal( inBytes );
|
1045
|
537 |
|
1046
|
538 |
if( (_b < 0) || (_b > 255) ) goto bad;
|
|
539 |
}
|
|
540 |
else
|
|
541 |
{ RETURN(__MKSMALLINT (0) ); }
|
1045
|
542 |
|
1046
|
543 |
_in_ref[0] = (Byte) _b;
|
|
544 |
_zstream->stream.avail_in = 1;
|
|
545 |
_zstream->stream.next_in = _in_ref;
|
|
546 |
_zstream->crc_32 = crc32( _zstream->crc_32, _in_ref, 1 );
|
1045
|
547 |
|
1046
|
548 |
RETURN(__MKSMALLINT (1) );
|
|
549 |
}
|
1045
|
550 |
|
1046
|
551 |
if( (_offs = __intVal (start) - 1) < 0 )
|
|
552 |
goto bad;
|
1045
|
553 |
|
1046
|
554 |
if (__isBytes (inBytes)) {
|
|
555 |
_bytes = __ByteArrayInstPtr(inBytes)->ba_element;
|
|
556 |
_bsize = __byteArraySize( inBytes );
|
|
557 |
}
|
|
558 |
else if (__isString(inBytes)) {
|
|
559 |
_bytes = __stringVal ( inBytes );
|
|
560 |
_bsize = __stringSize( inBytes );
|
|
561 |
} else
|
|
562 |
goto bad;
|
1045
|
563 |
|
1046
|
564 |
if( (_count + _offs) < _bsize )
|
|
565 |
{
|
|
566 |
if(_count > _zstream->in_total )
|
|
567 |
_count = _zstream->in_total;
|
1045
|
568 |
|
1046
|
569 |
_zstream->stream.avail_in = _count;
|
|
570 |
_zstream->stream.next_in = _in_ref;
|
1045
|
571 |
|
1046
|
572 |
zmemcpy( _in_ref, (Bytef *) & _bytes[_offs], _count );
|
|
573 |
_zstream->crc_32 = crc32( _zstream->crc_32, _in_ref, _count );
|
1045
|
574 |
|
1046
|
575 |
RETURN( __MKSMALLINT (_count) );
|
|
576 |
}
|
1045
|
577 |
}
|
|
578 |
bad:;
|
|
579 |
%}.
|
|
580 |
zstream ifNil:[ self errorNotOpen ].
|
|
581 |
self invalidArguments.
|
|
582 |
!
|
|
583 |
|
|
584 |
zopen:aStream
|
|
585 |
|outTotal inTotal|
|
|
586 |
|
|
587 |
(onStream := aStream) ifNil:[ self pastEnd ].
|
|
588 |
|
|
589 |
outputBytes := ExternalBytes unprotectedNew:8192.
|
|
590 |
outTotal := outputBytes size.
|
|
591 |
|
|
592 |
inputBytes := ExternalBytes unprotectedNew:8192.
|
|
593 |
inTotal := outputBytes size.
|
|
594 |
binary := false.
|
|
595 |
%{
|
|
596 |
zstream_s * _zstream = (zstream_s *) malloc( sizeof(zstream_s) );
|
|
597 |
|
|
598 |
if( _zstream )
|
|
599 |
{
|
1046
|
600 |
OBJ _zobj = __MKEXTERNALADDRESS( _zstream );
|
|
601 |
OBJ _outObj = __INST( outputBytes );
|
|
602 |
OBJ _inpObj = __INST( inputBytes );
|
1045
|
603 |
|
1046
|
604 |
zmemzero( _zstream, sizeof(zstream_s) );
|
1045
|
605 |
|
1046
|
606 |
_zstream->in_total = __intVal( inTotal );
|
|
607 |
_zstream->in_ref = (Bytef *) __externalBytesAddress( _inpObj );
|
|
608 |
_zstream->stream.next_in = Z_NULL;
|
|
609 |
_zstream->stream.avail_in = 0;
|
|
610 |
_zstream->stream.total_in = 0;
|
1045
|
611 |
|
1046
|
612 |
_zstream->out_used = 0;
|
|
613 |
_zstream->out_total = __intVal( outTotal );
|
|
614 |
_zstream->out_ref = (Bytef *) __externalBytesAddress( _outObj );
|
|
615 |
_zstream->stream.next_out = _zstream->out_ref;
|
|
616 |
_zstream->stream.avail_out = _zstream->out_total;
|
|
617 |
_zstream->stream.total_out = 0;
|
1045
|
618 |
|
1046
|
619 |
_zstream->stream.zalloc = (alloc_func)0;
|
|
620 |
_zstream->stream.zfree = (free_func) 0;
|
|
621 |
_zstream->stream.opaque = (voidpf) 0;
|
1045
|
622 |
|
1046
|
623 |
_zstream->crc_32 = crc32( 0L, Z_NULL, 0 );
|
1045
|
624 |
|
1046
|
625 |
__INST (zstream) = _zobj;
|
|
626 |
__STORE(self, _zobj);
|
1045
|
627 |
}
|
|
628 |
%}.
|
|
629 |
zstream ifNil:[ self zerror:'cannot allocate zbuffer' ].
|
|
630 |
hitEOF := false.
|
|
631 |
!
|
|
632 |
|
|
633 |
zoutputLimit:count
|
|
634 |
%{
|
|
635 |
OBJ _zstreamObj = __INST( zstream );
|
|
636 |
|
|
637 |
if( (_zstreamObj != nil) && __isSmallInteger(count) )
|
|
638 |
{
|
1046
|
639 |
int _usedSz;
|
|
640 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
641 |
|
1046
|
642 |
_usedSz = __intVal (count);
|
1045
|
643 |
|
1046
|
644 |
if( _usedSz > 0 )
|
|
645 |
{
|
|
646 |
if( _usedSz > _zstream->out_total )
|
|
647 |
_usedSz = _zstream->out_total;
|
|
648 |
}
|
|
649 |
else
|
|
650 |
_usedSz = 0;
|
1045
|
651 |
|
1046
|
652 |
_zstream->stream.next_out = _zstream->out_ref;
|
|
653 |
_zstream->stream.avail_out = _zstream->out_used = _usedSz;
|
1045
|
654 |
|
1046
|
655 |
RETURN( __MKSMALLINT(_usedSz) );
|
1045
|
656 |
}
|
|
657 |
%}.
|
|
658 |
zstream ifNil:[ self errorNotOpen ].
|
|
659 |
self invalidArguments .
|
|
660 |
!
|
|
661 |
|
|
662 |
zoutputLimitReached
|
|
663 |
"returns true if the write limit is reached; must flush the buffer
|
|
664 |
"
|
|
665 |
%{
|
|
666 |
OBJ _zstreamObj = __INST( zstream );
|
|
667 |
|
|
668 |
if( _zstreamObj != nil )
|
|
669 |
{
|
1046
|
670 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
671 |
|
1046
|
672 |
if( _zstream->stream.avail_out == 0 )
|
|
673 |
RETURN( true );
|
1045
|
674 |
|
1046
|
675 |
RETURN( false );
|
1045
|
676 |
}
|
|
677 |
%}.
|
|
678 |
self errorNotOpen
|
|
679 |
! !
|
|
680 |
|
|
681 |
!ZipStream methodsFor:'private'!
|
|
682 |
|
|
683 |
onStreamPutBytes:count from:data
|
|
684 |
|
|
685 |
onStream ifNil:[ self errorNotOpen ].
|
|
686 |
onStream nextPutBytes:count from:data startingAt:1
|
|
687 |
!
|
|
688 |
|
|
689 |
onStreamPutLong:aLong
|
|
690 |
|bytes value|
|
|
691 |
|
|
692 |
bytes := ByteArray new:4.
|
|
693 |
value := aLong.
|
|
694 |
|
|
695 |
1 to:4 do:[:i|
|
1046
|
696 |
bytes at:i put:(value bitAnd:16rff).
|
|
697 |
value := value bitShift:-8.
|
1045
|
698 |
].
|
|
699 |
self onStreamPutBytes:(bytes size) from:bytes
|
|
700 |
!
|
|
701 |
|
|
702 |
onStreamReadBytesInto:data
|
|
703 |
|
|
704 |
onStream ifNil:[ self pastEnd ].
|
|
705 |
^ onStream nextBytes:(data size) into:data startingAt:1
|
|
706 |
! !
|
|
707 |
|
|
708 |
!ZipStream methodsFor:'private todo'!
|
|
709 |
|
|
710 |
readHeader
|
|
711 |
|method flags|
|
|
712 |
|
|
713 |
self isReadOpen ifFalse:[ self pastEnd ].
|
|
714 |
|
|
715 |
"Check the gzip magic id
|
|
716 |
"
|
|
717 |
GZ_MAGIC_ID do:[:b|
|
1046
|
718 |
onStream nextByte ~~ b ifTrue:[ self zerror:'version error' ]
|
1045
|
719 |
].
|
|
720 |
|
|
721 |
method := onStream nextByte.
|
|
722 |
method ~~ Z_DEFLATED ifTrue:[
|
1046
|
723 |
self zerror:'invalid method (not deflated)'
|
1045
|
724 |
].
|
|
725 |
|
|
726 |
flags := onStream nextByte.
|
|
727 |
(flags bitAnd:HEAD_RESERVED) ~~ 0 ifTrue:[
|
1046
|
728 |
self zerror:'wrong data format'
|
1045
|
729 |
].
|
|
730 |
|
|
731 |
"discard time, xflags and OS code
|
|
732 |
"
|
|
733 |
onStream skip:6.
|
|
734 |
|
|
735 |
(flags bitAnd:HEAD_EXTRA_FIELD) ~~ 0 ifTrue:[|len|
|
1046
|
736 |
"skip the extra field"
|
|
737 |
len := onStream nextByte + (onStream nextByte bitShift:8).
|
|
738 |
len timesRepeat:[ onStream nextByte ].
|
1045
|
739 |
].
|
|
740 |
|
|
741 |
(flags bitAnd:HEAD_ORIG_NAME) ~~ 0 ifTrue:[|b|
|
1046
|
742 |
"skip the original file name"
|
|
743 |
Transcript show:'header name: '.
|
|
744 |
[ (b := onStream nextByte) ~~ 0 ] whileTrue:[
|
|
745 |
Transcript show:(Character value:b)
|
|
746 |
].
|
|
747 |
Transcript cr.
|
1045
|
748 |
].
|
|
749 |
|
|
750 |
(flags bitAnd:HEAD_CRC) ~~ 0 ifTrue:[
|
1046
|
751 |
"skip the header crc"
|
|
752 |
onStream skip:2.
|
1045
|
753 |
].
|
|
754 |
|
|
755 |
!
|
|
756 |
|
|
757 |
writeHeader
|
|
758 |
|
|
759 |
self isWriteOpen ifFalse:[ self pastEnd ].
|
|
760 |
|
|
761 |
"write the gzip magic id
|
|
762 |
"
|
|
763 |
GZ_MAGIC_ID do:[:b| onStream nextPutByte:b ].
|
|
764 |
|
|
765 |
"write the method
|
|
766 |
"
|
|
767 |
onStream nextPutByte:Z_DEFLATED.
|
|
768 |
|
|
769 |
"write the flags
|
|
770 |
"
|
|
771 |
onStream nextPutByte:0.
|
|
772 |
|
|
773 |
"write time
|
|
774 |
"
|
|
775 |
4 timesRepeat:[ onStream nextPutByte:0 ].
|
|
776 |
|
|
777 |
"write xflags
|
|
778 |
"
|
|
779 |
onStream nextPutByte:0.
|
|
780 |
"write OS code
|
|
781 |
"
|
|
782 |
onStream nextPutByte:HEAD_OS_CODE.
|
|
783 |
|
|
784 |
! !
|
|
785 |
|
|
786 |
|
|
787 |
!ZipStream methodsFor:'queries'!
|
|
788 |
|
|
789 |
atEnd
|
|
790 |
^ hitEOF ~~ false
|
|
791 |
!
|
|
792 |
|
|
793 |
isBinary
|
|
794 |
"return true, if the stream is in binary (as opposed to text-) mode.
|
|
795 |
The default when created is false.
|
|
796 |
"
|
|
797 |
^ binary
|
|
798 |
!
|
|
799 |
|
|
800 |
isOpen
|
|
801 |
^ onStream notNil
|
|
802 |
!
|
|
803 |
|
|
804 |
isReadOpen
|
|
805 |
^ onStream notNil and:[isReadOpen == true]
|
|
806 |
!
|
|
807 |
|
|
808 |
isWriteOpen
|
|
809 |
^ onStream notNil and:[isReadOpen == false]
|
|
810 |
! !
|
|
811 |
|
|
812 |
!ZipStream methodsFor:'reading'!
|
|
813 |
|
|
814 |
contents
|
|
815 |
|out buf bsz|
|
|
816 |
|
|
817 |
self isReadOpen ifFalse:[self pastEnd].
|
|
818 |
|
|
819 |
bsz := inputBytes size.
|
|
820 |
|
|
821 |
binary ifFalse:[
|
1046
|
822 |
out := (String new:bsz) writeStream.
|
|
823 |
buf := String new:bsz.
|
1045
|
824 |
] ifTrue:[
|
1046
|
825 |
out := (ByteArray new:bsz) writeStream.
|
|
826 |
buf := ByteArray new:bsz.
|
1045
|
827 |
].
|
|
828 |
|
|
829 |
[self atEnd ] whileFalse:[ |n|
|
1046
|
830 |
n := self nextAvailableBytes:bsz into:buf startingAt:1.
|
1045
|
831 |
|
1046
|
832 |
n ~~ 0 ifTrue:[
|
|
833 |
out nextPutAll:buf startingAt:1 to:n
|
|
834 |
]
|
1045
|
835 |
].
|
|
836 |
^ out contents
|
|
837 |
|
|
838 |
"
|
|
839 |
|gzFile zip contents|
|
|
840 |
|
|
841 |
gzFile := FileStream readonlyFileNamed:'/home/ca/C/Compress/zlib-1.1.4/Test/YYY.gz'.
|
|
842 |
[
|
|
843 |
zip := self new readOpenOn:gzFile.
|
|
844 |
contents := zip contents.
|
|
845 |
] valueNowOrOnUnwindDo:[
|
1046
|
846 |
zip ifNotNil:[ zip close ].
|
|
847 |
gzFile close.
|
1045
|
848 |
].
|
|
849 |
^ contents
|
|
850 |
"
|
|
851 |
|
|
852 |
!
|
|
853 |
|
|
854 |
next
|
|
855 |
|b|
|
|
856 |
|
|
857 |
b := self nextByte.
|
|
858 |
|
|
859 |
b ifNotNil:[
|
1046
|
860 |
binary ifTrue:[^ b ].
|
1045
|
861 |
^ Character value:b
|
|
862 |
].
|
|
863 |
^ nil
|
|
864 |
!
|
|
865 |
|
|
866 |
next:count
|
|
867 |
^ self nextAvailable:count
|
|
868 |
!
|
|
869 |
nextAvailable:count
|
|
870 |
|n buffer|
|
|
871 |
|
|
872 |
self isReadOpen ifFalse:[ self pastEnd ].
|
|
873 |
|
|
874 |
binary ifTrue:[
|
1046
|
875 |
buffer := ByteArray uninitializedNew:count
|
1045
|
876 |
] ifFalse:[
|
1046
|
877 |
buffer := String new:count
|
1045
|
878 |
].
|
|
879 |
|
|
880 |
n := self nextAvailableBytes:count into:buffer startingAt:1.
|
|
881 |
|
|
882 |
n == 0 ifTrue:[
|
1046
|
883 |
binary ifTrue:[
|
|
884 |
^ #[]
|
|
885 |
].
|
|
886 |
^ ''
|
1045
|
887 |
].
|
|
888 |
n ~~ count ifTrue:[ ^ buffer copyTo:n ].
|
|
889 |
^ buffer.
|
|
890 |
!
|
|
891 |
|
|
892 |
nextAvailableBytes:count into:buffer startingAt:start
|
|
893 |
|eoStream|
|
|
894 |
|
|
895 |
self isReadOpen ifFalse:[ self pastEnd ].
|
|
896 |
|
|
897 |
(self zoutputLimit:count) < 0 ifTrue:[^ 0].
|
|
898 |
|
|
899 |
eoStream := false.
|
|
900 |
|
|
901 |
[ self atEnd or:[self zoutputLimitReached] ] whileFalse:[
|
1046
|
902 |
self zinputAvailable ifFalse:[ |n|
|
|
903 |
n := self onStreamReadBytesInto:inputBytes.
|
1045
|
904 |
|
1046
|
905 |
n == 0 ifTrue:[
|
|
906 |
eoStream ifTrue:[ self pastEnd ].
|
|
907 |
eoStream := true.
|
|
908 |
].
|
|
909 |
self zinputPosition:n.
|
|
910 |
].
|
|
911 |
self zinflate.
|
1045
|
912 |
].
|
|
913 |
^ self zgetAvailableOutBufferInto:buffer startingAt:start.
|
|
914 |
!
|
|
915 |
|
|
916 |
nextByte
|
|
917 |
|buff size|
|
|
918 |
|
|
919 |
buff := ByteArray new:1.
|
|
920 |
size := self nextAvailableBytes:1 into:buff startingAt:1.
|
|
921 |
|
|
922 |
size ~~ 0 ifTrue:[
|
1046
|
923 |
^ buff at:1
|
1045
|
924 |
].
|
|
925 |
^ nil
|
|
926 |
! !
|
|
927 |
|
|
928 |
!ZipStream methodsFor:'startup & release'!
|
|
929 |
|
|
930 |
close
|
|
931 |
zstream ifNil:[^ self].
|
|
932 |
|
|
933 |
self isWriteOpen ifTrue:[
|
1046
|
934 |
[ self zflush:true.
|
|
935 |
self zdeflate:Z_FINISH
|
|
936 |
] whileFalse.
|
1045
|
937 |
|
1046
|
938 |
self zflush:true.
|
|
939 |
self zdeflateEnd.
|
1045
|
940 |
|
1046
|
941 |
self onStreamPutLong:(self zgetCrc32 ). " write crc "
|
|
942 |
self onStreamPutLong:(self zgetTotalIn). " write total size "
|
1045
|
943 |
].
|
|
944 |
self zclose.
|
|
945 |
!
|
|
946 |
|
|
947 |
readOpenOn:aStream
|
|
948 |
|
|
949 |
isReadOpen := true.
|
|
950 |
|
|
951 |
self zopen:aStream.
|
|
952 |
self zinflateInit.
|
|
953 |
self readHeader.
|
|
954 |
!
|
|
955 |
|
|
956 |
writeOpenOn:aStream level:level memLevel:memLevel strategy:strategy
|
|
957 |
|
|
958 |
isReadOpen := false.
|
|
959 |
|
|
960 |
self zopen:aStream.
|
|
961 |
self zdeflateInit:level memLevel:memLevel strategy:strategy.
|
|
962 |
self writeHeader.
|
|
963 |
! !
|
|
964 |
|
|
965 |
!ZipStream methodsFor:'writing'!
|
|
966 |
|
|
967 |
flush
|
|
968 |
"force flush output
|
|
969 |
"
|
|
970 |
self flush:true
|
|
971 |
!
|
|
972 |
|
|
973 |
flush:forced
|
|
974 |
|
|
975 |
self isWriteOpen ifTrue:[
|
1046
|
976 |
self zflush:forced
|
1045
|
977 |
] ifFalse:[
|
1046
|
978 |
onStream ifNil:[self pastEnd]
|
1045
|
979 |
].
|
|
980 |
!
|
|
981 |
|
|
982 |
nextPut:aCharacter
|
|
983 |
|count|
|
|
984 |
|
|
985 |
self isWriteOpen ifFalse:[ self pastEnd ].
|
|
986 |
|
|
987 |
count := self znextPutBytes:0 from:aCharacter startingAt:0.
|
|
988 |
|
|
989 |
[
|
1046
|
990 |
self zflush:false.
|
|
991 |
self zdeflate:Z_NO_FLUSH.
|
|
992 |
self zinputAvailable
|
1045
|
993 |
] whileTrue.
|
|
994 |
!
|
|
995 |
|
|
996 |
nextPutByte:aByte
|
|
997 |
self nextPut:aByte.
|
|
998 |
!
|
|
999 |
|
|
1000 |
nextPutAll:buffer
|
|
1001 |
|count offset|
|
|
1002 |
|
|
1003 |
count := buffer size.
|
|
1004 |
offset := 1.
|
|
1005 |
|
|
1006 |
[ count > 0 ] whileTrue:[ |n|
|
1046
|
1007 |
n := self nextPutBytes:count from:buffer startingAt:offset.
|
|
1008 |
offset := offset + n.
|
|
1009 |
count := count - n.
|
1045
|
1010 |
].
|
|
1011 |
!
|
|
1012 |
|
|
1013 |
nextPutBytes:count from:inBytes startingAt:start
|
|
1014 |
|n|
|
|
1015 |
|
|
1016 |
self isWriteOpen ifFalse:[ self pastEnd ].
|
|
1017 |
|
|
1018 |
n := self znextPutBytes:count from:inBytes startingAt:start.
|
|
1019 |
|
|
1020 |
n ~~ 0 ifTrue:[
|
1046
|
1021 |
[
|
|
1022 |
self zflush:false.
|
|
1023 |
self zdeflate:Z_NO_FLUSH.
|
|
1024 |
self zinputAvailable
|
|
1025 |
] whileTrue.
|
1045
|
1026 |
].
|
|
1027 |
^ n
|
|
1028 |
! !
|
1044
|
1029 |
|
|
1030 |
!ZipStream class methodsFor:'documentation'!
|
|
1031 |
|
|
1032 |
version
|
1046
|
1033 |
^ '$Header: /cvs/stx/stx/libbasic2/ZipStream.st,v 1.3 2002-06-19 15:04:58 ca Exp $'
|
1044
|
1034 |
! !
|