1047
|
1 |
'From Smalltalk/X, Version:4.1.4 on 20-jun-2002 at 06:09:31 am' !
|
|
2 |
|
1044
|
3 |
"{ Package: 'ca:Compress' }"
|
|
4 |
|
1059
|
5 |
CompressionStream subclass:#ZipStream
|
1060
|
6 |
instanceVariableNames:''
|
|
7 |
classVariableNames:'Z_DEFLATED HEAD_OS_CODE HEAD_RESERVED HEAD_EXTRA_FIELD
|
|
8 |
HEAD_ORIG_NAME HEAD_COMMENT HEAD_CRC GZ_MAGIC_ID'
|
|
9 |
poolDictionaries:''
|
|
10 |
category:'A-Compress'
|
1044
|
11 |
!
|
|
12 |
|
1045
|
13 |
!ZipStream primitiveDefinitions!
|
|
14 |
%{
|
|
15 |
|
|
16 |
/*
|
|
17 |
* includes, defines, structure definitions
|
|
18 |
* and typedefs come here.
|
|
19 |
*/
|
|
20 |
|
1060
|
21 |
#include "zlib/zlib.h"
|
|
22 |
#include "zlib/zutil.h"
|
1045
|
23 |
|
1051
|
24 |
typedef enum {
|
1061
|
25 |
e_opmode_unspecified /* processing done */
|
|
26 |
, e_opmode_deflate /* running deflate */
|
|
27 |
, e_opmode_inflate /* running inflate */
|
1051
|
28 |
} e_opmode;
|
|
29 |
|
1045
|
30 |
typedef struct {
|
1061
|
31 |
z_stream stream; /* pointer to the external in -stream */
|
|
32 |
Bytef * in_ref; /* size of the external in -stream */
|
|
33 |
Bytef * out_ref; /* pointer to the external out-stream */
|
|
34 |
uLong out_total; /* size of the external out-stream */
|
1045
|
35 |
|
1061
|
36 |
e_opmode op_mode; /* current operational mode */
|
|
37 |
uLong crc_32; /* keeps the current crc */
|
|
38 |
|
|
39 |
Bytef tail_buff[ 8 ]; /* store the tail in the read modus */
|
|
40 |
uLong tail_size; /* to check the crc and data length */
|
1045
|
41 |
} zstream_s;
|
|
42 |
|
|
43 |
%}
|
|
44 |
! !
|
|
45 |
|
|
46 |
|
|
47 |
!ZipStream class methodsFor:'initialization'!
|
|
48 |
|
|
49 |
initialize
|
1061
|
50 |
"setup class attributes derived from the library
|
|
51 |
"
|
1059
|
52 |
|z_deflated os_code|
|
1045
|
53 |
%{
|
1059
|
54 |
z_deflated = __MKSMALLINT( Z_DEFLATED );
|
|
55 |
os_code = __MKSMALLINT( OS_CODE );
|
1045
|
56 |
%}.
|
|
57 |
|
1059
|
58 |
Z_DEFLATED := z_deflated.
|
1045
|
59 |
|
1059
|
60 |
HEAD_OS_CODE := os_code.
|
1045
|
61 |
HEAD_RESERVED := 16rE0. " bits 5..7: reserved "
|
|
62 |
HEAD_EXTRA_FIELD := 16r04. " bit 2 set: extra field present "
|
|
63 |
HEAD_ORIG_NAME := 16r08. " bit 3 set: original file name present "
|
|
64 |
HEAD_COMMENT := 16r10. " bit 4 set: file comment present "
|
|
65 |
HEAD_CRC := 16r02. " bit 1 set: header CRC present "
|
|
66 |
|
|
67 |
GZ_MAGIC_ID := #[ 16r1f 16r8b ]
|
|
68 |
|
|
69 |
! !
|
|
70 |
|
|
71 |
!ZipStream methodsFor:'low level'!
|
|
72 |
|
|
73 |
zclose
|
1052
|
74 |
"low level close of the zip stream
|
|
75 |
"
|
1045
|
76 |
%{
|
|
77 |
OBJ _zstreamObj = __INST( zstream );
|
|
78 |
|
|
79 |
if( _zstreamObj != nil )
|
|
80 |
{
|
1047
|
81 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
82 |
|
1047
|
83 |
__INST(zstream) = nil;
|
1045
|
84 |
|
1047
|
85 |
if( _zstream->stream.state != NULL )
|
|
86 |
{
|
1051
|
87 |
if( _zstream->op_mode == e_opmode_inflate )
|
1047
|
88 |
inflateEnd( & _zstream->stream );
|
|
89 |
else
|
|
90 |
deflateEnd( & _zstream->stream );
|
|
91 |
}
|
|
92 |
free( _zstream );
|
1045
|
93 |
}
|
|
94 |
%}.
|
|
95 |
!
|
|
96 |
|
1057
|
97 |
zdeflate
|
1052
|
98 |
"low level - deflate
|
|
99 |
"
|
1045
|
100 |
|errorNo|
|
|
101 |
|
|
102 |
errorNo := nil.
|
|
103 |
|
|
104 |
%{
|
|
105 |
OBJ _zstreamObj = __INST( zstream );
|
|
106 |
|
1056
|
107 |
if( _zstreamObj != nil )
|
1045
|
108 |
{
|
1056
|
109 |
int _errorNo, _action;
|
1051
|
110 |
uLong _bfsize;
|
1047
|
111 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
112 |
|
1051
|
113 |
if( _zstream->op_mode != e_opmode_deflate )
|
1057
|
114 |
RETURN( false );
|
1051
|
115 |
|
|
116 |
_bfsize = _zstream->out_total;
|
|
117 |
|
1050
|
118 |
if( _zstream->stream.state == NULL )
|
|
119 |
{
|
1051
|
120 |
/* processing finished; write crc_32 and the total size
|
|
121 |
*/
|
|
122 |
uLong v, i;
|
|
123 |
Bytef * p = _zstream->out_ref;
|
|
124 |
|
|
125 |
v = _zstream->crc_32;
|
|
126 |
for( i = 0; i < 4; ++i ) { p[i] = v & 0xff; v >>= 8; }
|
|
127 |
|
|
128 |
v = _zstream->stream.total_in;
|
|
129 |
for( i = 4; i < 8; ++i ) { p[i] = v & 0xff; v >>= 8; }
|
|
130 |
|
|
131 |
_zstream->op_mode = e_opmode_unspecified;
|
|
132 |
_zstream->stream.avail_in = 0;
|
|
133 |
_zstream->stream.next_in = Z_NULL;
|
|
134 |
_zstream->stream.avail_out = _bfsize - 8;
|
|
135 |
RETURN( true );
|
1050
|
136 |
}
|
1051
|
137 |
_zstream->stream.avail_out = _bfsize;
|
1050
|
138 |
_zstream->stream.next_out = _zstream->out_ref;
|
1056
|
139 |
|
1057
|
140 |
_action = (__INST(hitEOF) == true) ? Z_FINISH : Z_NO_FLUSH;
|
1056
|
141 |
_errorNo = deflate( & _zstream->stream, _action );
|
1045
|
142 |
|
1050
|
143 |
if( _errorNo == Z_STREAM_END )
|
1047
|
144 |
{
|
1050
|
145 |
_zstream->stream.avail_in = 0;
|
|
146 |
_zstream->stream.next_in = Z_NULL;
|
|
147 |
_errorNo = deflateEnd( & _zstream->stream );
|
1047
|
148 |
}
|
1045
|
149 |
|
1050
|
150 |
if( _errorNo == Z_OK )
|
|
151 |
{
|
1051
|
152 |
if( (_zstream->stream.avail_out != _bfsize)
|
1050
|
153 |
|| (_zstream->stream.avail_in != 0)
|
|
154 |
)
|
|
155 |
RETURN( true );
|
|
156 |
|
|
157 |
RETURN( false );
|
|
158 |
}
|
1047
|
159 |
errorNo = __MKSMALLINT( _errorNo );
|
1045
|
160 |
}
|
|
161 |
%}.
|
|
162 |
errorNo ifNil:[
|
1047
|
163 |
zstream ifNil:[self errorNotOpen].
|
|
164 |
self invalidArguments.
|
1045
|
165 |
].
|
|
166 |
self zerror:errorNo.
|
|
167 |
!
|
|
168 |
|
1059
|
169 |
zdeflateInit
|
1052
|
170 |
"low level - deflateInit
|
|
171 |
"
|
1059
|
172 |
|errorNo level|
|
1045
|
173 |
|
|
174 |
errorNo := nil.
|
1059
|
175 |
level := BlockSize.
|
1045
|
176 |
%{
|
|
177 |
OBJ _zstreamObj = __INST( zstream );
|
|
178 |
|
1059
|
179 |
if( (_zstreamObj != nil) && __isSmallInteger(level) )
|
1045
|
180 |
{
|
1047
|
181 |
int _errorNo;
|
|
182 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
183 |
|
1051
|
184 |
_zstream->op_mode = e_opmode_deflate;
|
|
185 |
|
1047
|
186 |
_errorNo = deflateInit2( & _zstream->stream
|
1059
|
187 |
, __intVal( level )
|
1047
|
188 |
, Z_DEFLATED
|
|
189 |
, -MAX_WBITS
|
1059
|
190 |
, DEF_MEM_LEVEL
|
|
191 |
, Z_DEFAULT_STRATEGY
|
1047
|
192 |
);
|
1045
|
193 |
|
1047
|
194 |
if( _errorNo == Z_OK )
|
|
195 |
RETURN( self );
|
1045
|
196 |
|
1047
|
197 |
errorNo = __MKSMALLINT( _errorNo );
|
1045
|
198 |
}
|
|
199 |
%}.
|
|
200 |
errorNo ifNil:[
|
1047
|
201 |
zstream ifNil:[ self errorNotOpen ].
|
|
202 |
self invalidArguments .
|
1045
|
203 |
].
|
|
204 |
self zerror:errorNo.
|
|
205 |
!
|
|
206 |
|
1059
|
207 |
zget_avail_out
|
|
208 |
"low level - get the number of available out bytes
|
|
209 |
"
|
|
210 |
%{
|
|
211 |
OBJ _zstreamObj = __INST( zstream );
|
|
212 |
|
|
213 |
if( _zstreamObj != nil )
|
|
214 |
{
|
|
215 |
uInt _count;
|
|
216 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
217 |
|
|
218 |
_count = _zstream->out_total - _zstream->stream.avail_out;
|
|
219 |
|
|
220 |
RETURN( __MKSMALLINT (_count) );
|
|
221 |
}
|
|
222 |
%}.
|
|
223 |
self errorNotOpen.
|
|
224 |
!
|
|
225 |
|
1050
|
226 |
zinflate
|
1052
|
227 |
"low level - inflate
|
|
228 |
"
|
1061
|
229 |
|errorNo tailError|
|
1050
|
230 |
|
1061
|
231 |
tailError := nil.
|
|
232 |
errorNo := nil.
|
1050
|
233 |
%{
|
|
234 |
OBJ _zstreamObj = __INST( zstream );
|
|
235 |
|
|
236 |
if( _zstreamObj != nil )
|
|
237 |
{
|
|
238 |
int _errorNo, _count;
|
|
239 |
zstream_s * _zstream;
|
1061
|
240 |
int _avail_in;
|
1050
|
241 |
|
|
242 |
_zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
243 |
|
1051
|
244 |
if( _zstream->op_mode != e_opmode_inflate )
|
|
245 |
RETURN( nil );
|
|
246 |
|
1061
|
247 |
_avail_in = _zstream->stream.avail_in;
|
|
248 |
|
1050
|
249 |
if( _zstream->stream.state == NULL )
|
|
250 |
{
|
1061
|
251 |
/* processing finished : check crc and data length */
|
|
252 |
Bytef * _next_in;
|
|
253 |
Bytef * _buff;
|
|
254 |
uLong _tnum;
|
|
255 |
int _i;
|
|
256 |
|
|
257 |
_next_in = _zstream->stream.next_in;
|
|
258 |
_buff = _zstream->tail_buff;
|
|
259 |
_tnum = _zstream->tail_size;
|
|
260 |
|
|
261 |
while( (_avail_in > 0) && (_tnum < 8) )
|
|
262 |
{
|
|
263 |
_buff[_tnum] = * _next_in;
|
|
264 |
|
|
265 |
++_next_in;
|
|
266 |
++_tnum;
|
|
267 |
--_avail_in;
|
|
268 |
}
|
|
269 |
_zstream->tail_size = _tnum;
|
|
270 |
|
|
271 |
if( _tnum < 8 ) /* test whether tail is read */
|
|
272 |
RETURN( __MKSMALLINT (0) ); /* need more data */
|
|
273 |
|
|
274 |
/* compute and check crc */
|
|
275 |
for( _tnum = 0, _i = 4; --_i >= 0; _tnum = (_tnum << 8) + _buff[_i] );
|
|
276 |
|
|
277 |
if( _tnum != _zstream->crc_32 )
|
|
278 |
{ tailError = __MKSMALLINT( 1 ); goto badTail; }
|
|
279 |
|
|
280 |
/* compute and check data length */
|
|
281 |
for( _tnum = 0, _i = 8; --_i >= 4; _tnum = (_tnum << 8) + _buff[_i] );
|
|
282 |
|
|
283 |
if( _zstream->stream.total_out != _tnum )
|
|
284 |
{ tailError = __MKSMALLINT( 2 ); goto badTail; }
|
1051
|
285 |
|
|
286 |
_zstream->op_mode = e_opmode_unspecified;
|
1050
|
287 |
RETURN( nil );
|
|
288 |
}
|
1061
|
289 |
if( _avail_in == 0 )
|
1050
|
290 |
RETURN( __MKSMALLINT (0) );
|
|
291 |
|
|
292 |
_zstream->stream.avail_out = _zstream->out_total;
|
|
293 |
_zstream->stream.next_out = _zstream->out_ref;
|
|
294 |
|
|
295 |
_errorNo = inflate( & _zstream->stream, Z_NO_FLUSH );
|
|
296 |
|
|
297 |
if( _errorNo == Z_STREAM_END )
|
|
298 |
_errorNo = inflateEnd( & _zstream->stream );
|
|
299 |
|
|
300 |
if( _errorNo == Z_OK )
|
|
301 |
{
|
|
302 |
_count = _zstream->out_total - _zstream->stream.avail_out;
|
1051
|
303 |
|
|
304 |
if( _count > 0 )
|
|
305 |
_zstream->crc_32 = crc32( _zstream->crc_32, _zstream->out_ref, _count );
|
|
306 |
|
1050
|
307 |
RETURN( __MKSMALLINT (_count) );
|
|
308 |
}
|
|
309 |
errorNo = __MKSMALLINT( _errorNo );
|
|
310 |
}
|
1061
|
311 |
badTail:;
|
1050
|
312 |
%}.
|
1061
|
313 |
errorNo ifNil:[
|
|
314 |
tailError ifNotNil:[
|
|
315 |
tailError == 1 ifTrue:[
|
|
316 |
self zerror:'invalid compressed data--crc error'
|
|
317 |
].
|
|
318 |
self zerror:'invalid compressed data--length error'
|
|
319 |
].
|
|
320 |
self errorNotOpen
|
|
321 |
].
|
1050
|
322 |
self zerror:errorNo.
|
|
323 |
!
|
|
324 |
|
1045
|
325 |
zinflateInit
|
1052
|
326 |
"low level - inflateInit
|
|
327 |
"
|
1045
|
328 |
|errorNo|
|
|
329 |
|
|
330 |
errorNo := nil.
|
|
331 |
|
|
332 |
%{
|
|
333 |
OBJ _zstreamObj = __INST( zstream );
|
|
334 |
|
|
335 |
if( _zstreamObj != nil )
|
|
336 |
{
|
1047
|
337 |
int _errorNo;
|
|
338 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
339 |
|
1051
|
340 |
_zstream->op_mode = e_opmode_inflate;
|
1047
|
341 |
_errorNo = inflateInit2( & _zstream->stream, -MAX_WBITS );
|
1045
|
342 |
|
1047
|
343 |
if( _errorNo == Z_OK )
|
|
344 |
RETURN( self );
|
1045
|
345 |
|
1047
|
346 |
_zstream->stream.avail_in = 0;
|
|
347 |
errorNo = __MKSMALLINT( _errorNo );
|
1045
|
348 |
}
|
|
349 |
%}.
|
|
350 |
errorNo ifNil:[ self errorNotOpen ].
|
|
351 |
self zerror:errorNo.
|
|
352 |
!
|
1047
|
353 |
|
1059
|
354 |
zopen
|
1052
|
355 |
"low level - opens the zip stream
|
|
356 |
"
|
1051
|
357 |
|outTotal|
|
1045
|
358 |
|
1059
|
359 |
outTotal := outputBytes size.
|
1045
|
360 |
%{
|
|
361 |
zstream_s * _zstream = (zstream_s *) malloc( sizeof(zstream_s) );
|
|
362 |
|
|
363 |
if( _zstream )
|
|
364 |
{
|
1047
|
365 |
OBJ _zobj = __MKEXTERNALADDRESS( _zstream );
|
|
366 |
OBJ _outObj = __INST( outputBytes );
|
|
367 |
OBJ _inpObj = __INST( inputBytes );
|
1045
|
368 |
|
1047
|
369 |
zmemzero( _zstream, sizeof(zstream_s) );
|
1045
|
370 |
|
1047
|
371 |
_zstream->in_ref = (Bytef *) __externalBytesAddress( _inpObj );
|
|
372 |
_zstream->stream.next_in = Z_NULL;
|
|
373 |
_zstream->stream.avail_in = 0;
|
|
374 |
_zstream->stream.total_in = 0;
|
1045
|
375 |
|
1047
|
376 |
_zstream->out_total = __intVal( outTotal );
|
|
377 |
_zstream->out_ref = (Bytef *) __externalBytesAddress( _outObj );
|
|
378 |
_zstream->stream.next_out = _zstream->out_ref;
|
|
379 |
_zstream->stream.avail_out = _zstream->out_total;
|
|
380 |
_zstream->stream.total_out = 0;
|
1045
|
381 |
|
1047
|
382 |
_zstream->stream.zalloc = (alloc_func)0;
|
|
383 |
_zstream->stream.zfree = (free_func) 0;
|
|
384 |
_zstream->stream.opaque = (voidpf) 0;
|
1045
|
385 |
|
1047
|
386 |
_zstream->crc_32 = crc32( 0L, Z_NULL, 0 );
|
1051
|
387 |
_zstream->op_mode = e_opmode_unspecified;
|
1061
|
388 |
_zstream->tail_size = 0;
|
1045
|
389 |
|
1047
|
390 |
__INST (zstream) = _zobj;
|
|
391 |
__STORE(self, _zobj);
|
1045
|
392 |
}
|
|
393 |
%}.
|
|
394 |
!
|
|
395 |
|
1059
|
396 |
zset_avail_in:count
|
|
397 |
"set the 'avail_in' and compute the crc
|
1052
|
398 |
"
|
1059
|
399 |
%{
|
|
400 |
OBJ _zstreamObj = __INST( zstream );
|
1045
|
401 |
|
1059
|
402 |
if( (_zstreamObj != nil) && __isSmallInteger(count) )
|
|
403 |
{
|
|
404 |
int _count;
|
|
405 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1045
|
406 |
|
1059
|
407 |
if( (_count = __intVal( count )) > 0 )
|
|
408 |
{
|
|
409 |
Bytef * _in_ref = _zstream->in_ref;
|
1045
|
410 |
|
1059
|
411 |
_zstream->stream.avail_in = _count;
|
|
412 |
_zstream->stream.next_in = _in_ref;
|
1045
|
413 |
|
1059
|
414 |
if( _zstream->op_mode == e_opmode_deflate )
|
|
415 |
_zstream->crc_32 = crc32( _zstream->crc_32, _in_ref, _count );
|
|
416 |
} else {
|
|
417 |
_zstream->stream.avail_in = 0;
|
|
418 |
_zstream->stream.next_in = Z_NULL;
|
|
419 |
}
|
|
420 |
RETURN( self );
|
|
421 |
}
|
|
422 |
%}.
|
|
423 |
zstream ifNil:[ self errorNotOpen ].
|
|
424 |
self invalidArguments.
|
1045
|
425 |
! !
|
|
426 |
|
|
427 |
!ZipStream methodsFor:'startup & release'!
|
|
428 |
|
1059
|
429 |
openWithMode:aMode on:aStream
|
|
430 |
super openWithMode:aMode on:aStream.
|
1045
|
431 |
|
1059
|
432 |
self isReadOpen ifTrue:[
|
|
433 |
"Check the gzip magic id
|
|
434 |
"
|
|
435 |
|flags|
|
1045
|
436 |
|
1059
|
437 |
GZ_MAGIC_ID do:[:b|
|
|
438 |
onStream nextByte ~~ b ifTrue:[ self zerror:'version error' ]
|
|
439 |
].
|
1045
|
440 |
|
1059
|
441 |
onStream nextByte ~~ Z_DEFLATED ifTrue:[
|
|
442 |
self zerror:'invalid method (not deflated)'
|
|
443 |
].
|
1045
|
444 |
|
1059
|
445 |
flags := onStream nextByte.
|
|
446 |
(flags bitAnd:HEAD_RESERVED) ~~ 0 ifTrue:[
|
|
447 |
self zerror:'wrong data format'
|
|
448 |
].
|
1045
|
449 |
|
1059
|
450 |
"discard time, xflags and OS code"
|
|
451 |
onStream skip:6.
|
1045
|
452 |
|
1059
|
453 |
(flags bitAnd:HEAD_EXTRA_FIELD) ~~ 0 ifTrue:[|len|
|
|
454 |
"skip the extra field"
|
|
455 |
len := onStream nextByte + (onStream nextByte bitShift:8).
|
|
456 |
len timesRepeat:[ onStream nextByte ].
|
1057
|
457 |
].
|
1045
|
458 |
|
1059
|
459 |
(flags bitAnd:HEAD_ORIG_NAME) ~~ 0 ifTrue:[|b|
|
|
460 |
"skip the original file name"
|
|
461 |
[ (b := onStream nextByte) ~~ 0 ] whileTrue.
|
|
462 |
].
|
1050
|
463 |
|
1059
|
464 |
(flags bitAnd:HEAD_CRC) ~~ 0 ifTrue:[
|
|
465 |
"skip the header crc"
|
|
466 |
onStream skip:2.
|
|
467 |
].
|
|
468 |
] ifFalse:[
|
|
469 |
"write the gzip magic id
|
|
470 |
"
|
|
471 |
GZ_MAGIC_ID do:[:b| onStream nextPutByte:b ].
|
1050
|
472 |
|
1059
|
473 |
"write the method"
|
|
474 |
onStream nextPutByte:Z_DEFLATED.
|
|
475 |
|
|
476 |
"write the flags"
|
|
477 |
onStream nextPutByte:0.
|
1050
|
478 |
|
1059
|
479 |
"write time"
|
|
480 |
4 timesRepeat:[ onStream nextPutByte:0 ].
|
1045
|
481 |
|
1059
|
482 |
"write xflags"
|
|
483 |
onStream nextPutByte:0.
|
|
484 |
|
|
485 |
"write OS code"
|
|
486 |
onStream nextPutByte:HEAD_OS_CODE.
|
|
487 |
].
|
1045
|
488 |
! !
|
1044
|
489 |
|
|
490 |
!ZipStream class methodsFor:'documentation'!
|
|
491 |
|
|
492 |
version
|
1061
|
493 |
^ '$Header: /cvs/stx/stx/libbasic2/ZipStream.st,v 1.17 2002-06-26 06:44:39 ca Exp $'
|
1044
|
494 |
! !
|
1047
|
495 |
ZipStream initialize!
|