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