1059
|
1 |
"{ Package: 'ca:Compress' }"
|
|
2 |
|
|
3 |
CompressionStream subclass:#BZipStream
|
1060
|
4 |
instanceVariableNames:''
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'A-Compress'
|
1059
|
8 |
!
|
|
9 |
|
|
10 |
!BZipStream primitiveDefinitions!
|
|
11 |
%{
|
|
12 |
|
|
13 |
/*
|
|
14 |
* includes, defines, structure definitions
|
|
15 |
* and typedefs come here.
|
|
16 |
*/
|
|
17 |
|
1060
|
18 |
#include "bzlib/bzlib.h"
|
1059
|
19 |
|
|
20 |
typedef enum {
|
|
21 |
e_opmode_unspecified
|
|
22 |
, e_opmode_deflate
|
|
23 |
, e_opmode_inflate
|
|
24 |
} e_opmode;
|
|
25 |
|
|
26 |
typedef struct {
|
|
27 |
bz_stream stream;
|
|
28 |
char * in_ref;
|
|
29 |
char * out_ref;
|
|
30 |
unsigned int out_total;
|
|
31 |
|
|
32 |
e_opmode op_mode;
|
|
33 |
} zstream_s;
|
|
34 |
|
|
35 |
%}
|
|
36 |
! !
|
|
37 |
|
|
38 |
!BZipStream methodsFor:'low level'!
|
|
39 |
|
|
40 |
zclose
|
|
41 |
"low level close of the zip stream
|
|
42 |
"
|
|
43 |
onStream := mode := nil.
|
|
44 |
hitEOF := true.
|
|
45 |
|
|
46 |
zstream ifNil:[^ self].
|
|
47 |
self unregisterForFinalization.
|
|
48 |
%{
|
|
49 |
OBJ _zstreamObj = __INST( zstream );
|
|
50 |
|
|
51 |
if( _zstreamObj != nil )
|
|
52 |
{
|
|
53 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
54 |
|
|
55 |
__INST(zstream) = nil;
|
|
56 |
|
|
57 |
if( _zstream->stream.state != NULL )
|
|
58 |
{
|
|
59 |
if( _zstream->op_mode == e_opmode_inflate )
|
|
60 |
BZ2_bzDecompressEnd( & _zstream->stream );
|
|
61 |
else
|
|
62 |
BZ2_bzCompressEnd( & _zstream->stream );
|
|
63 |
}
|
|
64 |
free( _zstream );
|
|
65 |
}
|
|
66 |
%}.
|
|
67 |
zstream := nil.
|
|
68 |
!
|
|
69 |
|
|
70 |
zdeflate
|
|
71 |
"low level - deflate
|
|
72 |
"
|
|
73 |
|errorNo|
|
|
74 |
|
|
75 |
errorNo := nil.
|
|
76 |
|
|
77 |
%{
|
|
78 |
OBJ _zstreamObj = __INST( zstream );
|
|
79 |
|
|
80 |
if( _zstreamObj != nil )
|
|
81 |
{
|
|
82 |
int _errorNo, _action;
|
|
83 |
unsigned int _bfsize;
|
|
84 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
85 |
|
|
86 |
if( _zstream->op_mode != e_opmode_deflate )
|
|
87 |
RETURN( nil );
|
|
88 |
|
|
89 |
_bfsize = _zstream->out_total;
|
|
90 |
|
|
91 |
if( _zstream->stream.state == NULL )
|
|
92 |
{
|
|
93 |
_zstream->op_mode = e_opmode_unspecified;
|
|
94 |
RETURN( nil );
|
|
95 |
}
|
|
96 |
_action = (__INST(hitEOF) == true) ? BZ_FINISH : BZ_RUN;
|
|
97 |
|
|
98 |
_zstream->stream.avail_out = _bfsize;
|
|
99 |
_zstream->stream.next_out = _zstream->out_ref;
|
|
100 |
|
|
101 |
_errorNo = BZ2_bzCompress( & _zstream->stream, _action );
|
|
102 |
|
|
103 |
if( _errorNo == BZ_STREAM_END )
|
|
104 |
{
|
|
105 |
_zstream->stream.avail_in = 0;
|
|
106 |
_zstream->stream.next_in = NULL;
|
|
107 |
_errorNo = BZ2_bzCompressEnd( & _zstream->stream );
|
|
108 |
}
|
|
109 |
|
|
110 |
if( (_errorNo == BZ_OK)
|
|
111 |
|| (_errorNo == BZ_RUN_OK)
|
|
112 |
|| (_errorNo == BZ_FINISH_OK)
|
|
113 |
)
|
|
114 |
{
|
|
115 |
if( (_zstream->stream.avail_out != _bfsize)
|
|
116 |
|| (_zstream->stream.avail_in != 0)
|
|
117 |
)
|
|
118 |
RETURN( true );
|
|
119 |
|
|
120 |
RETURN( false );
|
|
121 |
}
|
|
122 |
errorNo = __MKSMALLINT( _errorNo );
|
|
123 |
}
|
|
124 |
%}.
|
|
125 |
errorNo ifNil:[
|
|
126 |
zstream ifNil:[self errorNotOpen].
|
|
127 |
self invalidArguments.
|
|
128 |
].
|
|
129 |
self zerror:errorNo.
|
|
130 |
!
|
|
131 |
|
|
132 |
zdeflateInit
|
|
133 |
"low level - open for reading
|
|
134 |
"
|
|
135 |
|errorNo blockSize100k workFactor|
|
|
136 |
|
|
137 |
errorNo := nil.
|
|
138 |
blockSize100k := BlockSize.
|
|
139 |
workFactor := 30.
|
|
140 |
%{
|
|
141 |
OBJ _zstreamObj = __INST( zstream );
|
|
142 |
|
|
143 |
if( (_zstreamObj != nil) && __bothSmallInteger(blockSize100k, workFactor) )
|
|
144 |
{
|
|
145 |
int _errorNo;
|
|
146 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
147 |
|
|
148 |
_zstream->op_mode = e_opmode_deflate;
|
|
149 |
|
|
150 |
_errorNo = BZ2_bzCompressInit( & _zstream->stream
|
|
151 |
, __intVal( blockSize100k ), 0, __intVal( workFactor ) );
|
|
152 |
|
|
153 |
if( _errorNo == BZ_OK )
|
|
154 |
RETURN( self );
|
|
155 |
|
|
156 |
errorNo = __MKSMALLINT( _errorNo );
|
|
157 |
}
|
|
158 |
%}.
|
|
159 |
errorNo ifNil:[
|
|
160 |
zstream ifNil:[ self errorNotOpen ].
|
|
161 |
self invalidArguments .
|
|
162 |
].
|
|
163 |
self zerror:errorNo.
|
|
164 |
!
|
|
165 |
|
|
166 |
zget_avail_out
|
|
167 |
"low level - get the number of available out bytes
|
|
168 |
"
|
|
169 |
%{
|
|
170 |
OBJ _zstreamObj = __INST( zstream );
|
|
171 |
|
|
172 |
if( _zstreamObj != nil )
|
|
173 |
{
|
|
174 |
unsigned int _count;
|
|
175 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
176 |
|
|
177 |
_count = _zstream->out_total - _zstream->stream.avail_out;
|
|
178 |
|
|
179 |
RETURN( __MKSMALLINT (_count) );
|
|
180 |
}
|
|
181 |
%}.
|
|
182 |
self errorNotOpen.
|
|
183 |
!
|
|
184 |
|
|
185 |
zinflate
|
|
186 |
"low level - inflate
|
|
187 |
"
|
|
188 |
|errorNo|
|
|
189 |
|
|
190 |
errorNo := nil.
|
|
191 |
%{
|
|
192 |
OBJ _zstreamObj = __INST( zstream );
|
|
193 |
|
|
194 |
if( _zstreamObj != nil )
|
|
195 |
{
|
|
196 |
int _errorNo, _count;
|
|
197 |
zstream_s * _zstream;
|
|
198 |
|
|
199 |
_zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
200 |
|
|
201 |
if( _zstream->op_mode != e_opmode_inflate )
|
|
202 |
RETURN( nil );
|
|
203 |
|
|
204 |
if( _zstream->stream.state == NULL )
|
|
205 |
{
|
|
206 |
_zstream->op_mode = e_opmode_unspecified;
|
|
207 |
RETURN( nil );
|
|
208 |
}
|
|
209 |
if( _zstream->stream.avail_in == 0 )
|
|
210 |
RETURN( __MKSMALLINT (0) );
|
|
211 |
|
|
212 |
_zstream->stream.avail_out = _zstream->out_total;
|
|
213 |
_zstream->stream.next_out = _zstream->out_ref;
|
|
214 |
|
|
215 |
_errorNo = BZ2_bzDecompress( & _zstream->stream );
|
|
216 |
|
|
217 |
if( _errorNo == BZ_STREAM_END )
|
|
218 |
_errorNo = BZ2_bzDecompressEnd( & _zstream->stream );
|
|
219 |
|
|
220 |
if( _errorNo == BZ_OK )
|
|
221 |
{
|
|
222 |
_count = _zstream->out_total - _zstream->stream.avail_out;
|
|
223 |
|
|
224 |
RETURN( __MKSMALLINT (_count) );
|
|
225 |
}
|
|
226 |
errorNo = __MKSMALLINT( _errorNo );
|
|
227 |
}
|
|
228 |
%}.
|
|
229 |
errorNo ifNil:[ self errorNotOpen ].
|
|
230 |
self zerror:errorNo.
|
|
231 |
!
|
|
232 |
|
|
233 |
zinflateInit
|
|
234 |
"low level - inflateInit
|
|
235 |
"
|
|
236 |
|errorNo|
|
|
237 |
|
|
238 |
errorNo := nil.
|
|
239 |
|
|
240 |
%{
|
|
241 |
OBJ _zstreamObj = __INST( zstream );
|
|
242 |
|
|
243 |
if( _zstreamObj != nil )
|
|
244 |
{
|
|
245 |
int _errorNo;
|
|
246 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
247 |
|
|
248 |
_zstream->op_mode = e_opmode_inflate;
|
|
249 |
_errorNo = BZ2_bzDecompressInit( & _zstream->stream, 0, 1 );
|
|
250 |
|
|
251 |
if( _errorNo == BZ_OK )
|
|
252 |
RETURN( self );
|
|
253 |
|
|
254 |
_zstream->stream.avail_in = 0;
|
|
255 |
errorNo = __MKSMALLINT( _errorNo );
|
|
256 |
}
|
|
257 |
%}.
|
|
258 |
errorNo ifNil:[ self errorNotOpen ].
|
|
259 |
self zerror:errorNo.
|
|
260 |
!
|
|
261 |
|
|
262 |
zopen
|
|
263 |
"low level - opens the zip stream
|
|
264 |
"
|
|
265 |
|outTotal|
|
|
266 |
|
|
267 |
outTotal := outputBytes size.
|
|
268 |
%{
|
|
269 |
zstream_s * _zstream = (zstream_s *) malloc( sizeof(zstream_s) );
|
|
270 |
|
|
271 |
if( _zstream )
|
|
272 |
{
|
|
273 |
OBJ _zobj = __MKEXTERNALADDRESS( _zstream );
|
|
274 |
OBJ _outObj = __INST( outputBytes );
|
|
275 |
OBJ _inpObj = __INST( inputBytes );
|
|
276 |
|
|
277 |
_zstream->in_ref = (char *) __externalBytesAddress( _inpObj );
|
|
278 |
_zstream->stream.next_in = NULL;
|
|
279 |
_zstream->stream.avail_in = 0;
|
|
280 |
_zstream->stream.total_in_lo32 = 0;
|
|
281 |
_zstream->stream.total_in_hi32 = 0;
|
|
282 |
|
|
283 |
_zstream->out_total = __intVal( outTotal );
|
|
284 |
_zstream->out_ref = (char *) __externalBytesAddress( _outObj );
|
|
285 |
_zstream->stream.next_out = _zstream->out_ref;
|
|
286 |
_zstream->stream.avail_out = _zstream->out_total;
|
|
287 |
|
|
288 |
_zstream->stream.total_out_lo32 = 0;
|
|
289 |
_zstream->stream.total_out_hi32 = 0;
|
|
290 |
|
|
291 |
_zstream->stream.bzalloc = 0;
|
|
292 |
_zstream->stream.bzfree = 0;
|
|
293 |
_zstream->stream.opaque = 0;
|
|
294 |
|
|
295 |
_zstream->op_mode = e_opmode_unspecified;
|
|
296 |
|
|
297 |
__INST (zstream) = _zobj;
|
|
298 |
__STORE(self, _zobj);
|
|
299 |
}
|
|
300 |
%}.
|
|
301 |
!
|
|
302 |
|
|
303 |
zset_avail_in:count
|
|
304 |
"set the 'avail_in' and compute the crc
|
|
305 |
"
|
|
306 |
%{
|
|
307 |
OBJ _zstreamObj = __INST( zstream );
|
|
308 |
|
|
309 |
if( (_zstreamObj != nil) && __isSmallInteger(count) )
|
|
310 |
{
|
|
311 |
int _count;
|
|
312 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
|
313 |
|
|
314 |
if( (_count = __intVal( count )) > 0 )
|
|
315 |
{
|
|
316 |
char * _in_ref = _zstream->in_ref;
|
|
317 |
|
|
318 |
_zstream->stream.avail_in = _count;
|
|
319 |
_zstream->stream.next_in = _in_ref;
|
|
320 |
} else {
|
|
321 |
_zstream->stream.avail_in = 0;
|
|
322 |
_zstream->stream.next_in = NULL;
|
|
323 |
}
|
|
324 |
RETURN( self );
|
|
325 |
}
|
|
326 |
%}.
|
|
327 |
zstream ifNil:[ self errorNotOpen ].
|
|
328 |
self invalidArguments.
|
|
329 |
! !
|
|
330 |
|