1308
|
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 |
|
1195
|
13 |
"{ Package: 'stx:libbasic2' }"
|
1059
|
14 |
|
1196
|
15 |
CompressionStream subclass:#BZip2Stream
|
1193
|
16 |
instanceVariableNames:''
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'System-Compress'
|
1059
|
20 |
!
|
|
21 |
|
1196
|
22 |
!BZip2Stream primitiveDefinitions!
|
1059
|
23 |
%{
|
|
24 |
|
|
25 |
/*
|
|
26 |
* includes, defines, structure definitions
|
|
27 |
* and typedefs come here.
|
|
28 |
*/
|
|
29 |
|
1196
|
30 |
#include "bz2lib/bzlib.h"
|
1059
|
31 |
|
|
32 |
typedef enum {
|
1195
|
33 |
e_opmode_unspecified /* processing done */
|
|
34 |
, e_opmode_deflate /* running deflate */
|
|
35 |
, e_opmode_inflate /* running inflate */
|
1059
|
36 |
} e_opmode;
|
|
37 |
|
|
38 |
typedef struct {
|
1195
|
39 |
bz_stream stream; /* pointer to the external in -stream */
|
|
40 |
char * in_ref; /* size of the external in -stream */
|
|
41 |
char * out_ref; /* pointer to the external out-stream */
|
|
42 |
unsigned int out_total; /* size of the external out-stream */
|
1059
|
43 |
|
1195
|
44 |
e_opmode op_mode; /* current operational mode */
|
1059
|
45 |
} zstream_s;
|
|
46 |
|
|
47 |
%}
|
|
48 |
! !
|
|
49 |
|
1308
|
50 |
!BZip2Stream class methodsFor:'documentation'!
|
|
51 |
|
|
52 |
copyright
|
|
53 |
"
|
|
54 |
COPYRIGHT (c) 2002 by eXept Software AG
|
|
55 |
All Rights Reserved
|
|
56 |
|
|
57 |
This software is furnished under a license and may be used
|
|
58 |
only in accordance with the terms of that license and with the
|
|
59 |
inclusion of the above copyright notice. This software may not
|
|
60 |
be provided or otherwise made available to, or used by, any
|
|
61 |
other person. No title to or ownership of the software is
|
|
62 |
hereby transferred.
|
|
63 |
"
|
|
64 |
! !
|
1193
|
65 |
|
1196
|
66 |
!BZip2Stream methodsFor:'low level'!
|
1059
|
67 |
|
|
68 |
zclose
|
|
69 |
"low level close of the zip stream
|
|
70 |
"
|
|
71 |
%{
|
|
72 |
OBJ _zstreamObj = __INST( zstream );
|
|
73 |
|
|
74 |
if( _zstreamObj != nil )
|
|
75 |
{
|
1195
|
76 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1059
|
77 |
|
1195
|
78 |
__INST(zstream) = nil;
|
1059
|
79 |
|
1195
|
80 |
if( _zstream->stream.state != NULL )
|
|
81 |
{
|
|
82 |
if( _zstream->op_mode == e_opmode_inflate )
|
|
83 |
BZ2_bzDecompressEnd( & _zstream->stream );
|
|
84 |
else
|
|
85 |
BZ2_bzCompressEnd( & _zstream->stream );
|
|
86 |
}
|
|
87 |
free( _zstream );
|
1059
|
88 |
}
|
|
89 |
%}.
|
|
90 |
!
|
|
91 |
|
|
92 |
zdeflate
|
|
93 |
"low level - deflate
|
|
94 |
"
|
|
95 |
|errorNo|
|
|
96 |
|
|
97 |
errorNo := nil.
|
|
98 |
|
|
99 |
%{
|
|
100 |
OBJ _zstreamObj = __INST( zstream );
|
|
101 |
|
|
102 |
if( _zstreamObj != nil )
|
|
103 |
{
|
1195
|
104 |
int _errorNo, _action;
|
|
105 |
unsigned int _bfsize;
|
|
106 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1059
|
107 |
|
1195
|
108 |
if( _zstream->op_mode != e_opmode_deflate )
|
|
109 |
RETURN( nil );
|
1059
|
110 |
|
1195
|
111 |
_bfsize = _zstream->out_total;
|
1059
|
112 |
|
1195
|
113 |
if( _zstream->stream.state == NULL )
|
|
114 |
{
|
|
115 |
_zstream->op_mode = e_opmode_unspecified;
|
|
116 |
RETURN( nil );
|
|
117 |
}
|
|
118 |
_action = (__INST(hitEOF) == true) ? BZ_FINISH : BZ_RUN;
|
1059
|
119 |
|
1195
|
120 |
_zstream->stream.avail_out = _bfsize;
|
|
121 |
_zstream->stream.next_out = _zstream->out_ref;
|
1059
|
122 |
|
1195
|
123 |
_errorNo = BZ2_bzCompress( & _zstream->stream, _action );
|
1059
|
124 |
|
1195
|
125 |
if( _errorNo == BZ_STREAM_END )
|
|
126 |
{
|
|
127 |
_zstream->stream.avail_in = 0;
|
|
128 |
_zstream->stream.next_in = NULL;
|
|
129 |
_errorNo = BZ2_bzCompressEnd( & _zstream->stream );
|
|
130 |
}
|
1059
|
131 |
|
1195
|
132 |
if( (_errorNo == BZ_OK)
|
|
133 |
|| (_errorNo == BZ_RUN_OK)
|
|
134 |
|| (_errorNo == BZ_FINISH_OK)
|
|
135 |
)
|
|
136 |
{
|
|
137 |
if( (_zstream->stream.avail_out != _bfsize)
|
|
138 |
|| (_zstream->stream.avail_in != 0)
|
|
139 |
)
|
|
140 |
RETURN( true );
|
1059
|
141 |
|
1195
|
142 |
RETURN( false );
|
|
143 |
}
|
|
144 |
errorNo = __MKSMALLINT( _errorNo );
|
1059
|
145 |
}
|
|
146 |
%}.
|
|
147 |
errorNo ifNil:[
|
1195
|
148 |
zstream ifNil:[self errorNotOpen].
|
|
149 |
self invalidArguments.
|
1059
|
150 |
].
|
|
151 |
self zerror:errorNo.
|
|
152 |
!
|
|
153 |
|
|
154 |
zdeflateInit
|
|
155 |
"low level - open for reading
|
|
156 |
"
|
|
157 |
|errorNo blockSize100k workFactor|
|
|
158 |
|
|
159 |
errorNo := nil.
|
|
160 |
blockSize100k := BlockSize.
|
|
161 |
workFactor := 30.
|
|
162 |
%{
|
|
163 |
OBJ _zstreamObj = __INST( zstream );
|
|
164 |
|
|
165 |
if( (_zstreamObj != nil) && __bothSmallInteger(blockSize100k, workFactor) )
|
|
166 |
{
|
1195
|
167 |
int _errorNo;
|
|
168 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1059
|
169 |
|
1195
|
170 |
_zstream->op_mode = e_opmode_deflate;
|
1059
|
171 |
|
1195
|
172 |
_errorNo = BZ2_bzCompressInit( & _zstream->stream
|
|
173 |
, __intVal( blockSize100k ), 0, __intVal( workFactor ) );
|
1059
|
174 |
|
1195
|
175 |
if( _errorNo == BZ_OK )
|
|
176 |
RETURN( self );
|
1059
|
177 |
|
1195
|
178 |
errorNo = __MKSMALLINT( _errorNo );
|
1059
|
179 |
}
|
|
180 |
%}.
|
|
181 |
errorNo ifNil:[
|
1195
|
182 |
zstream ifNil:[ self errorNotOpen ].
|
|
183 |
self invalidArguments .
|
1059
|
184 |
].
|
|
185 |
self zerror:errorNo.
|
|
186 |
!
|
|
187 |
|
|
188 |
zget_avail_out
|
|
189 |
"low level - get the number of available out bytes
|
|
190 |
"
|
|
191 |
%{
|
|
192 |
OBJ _zstreamObj = __INST( zstream );
|
|
193 |
|
|
194 |
if( _zstreamObj != nil )
|
|
195 |
{
|
1195
|
196 |
unsigned int _count;
|
|
197 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1059
|
198 |
|
1195
|
199 |
_count = _zstream->out_total - _zstream->stream.avail_out;
|
1059
|
200 |
|
1195
|
201 |
RETURN( __MKSMALLINT (_count) );
|
1059
|
202 |
}
|
|
203 |
%}.
|
|
204 |
self errorNotOpen.
|
|
205 |
!
|
|
206 |
|
|
207 |
zinflate
|
|
208 |
"low level - inflate
|
|
209 |
"
|
|
210 |
|errorNo|
|
|
211 |
|
|
212 |
errorNo := nil.
|
|
213 |
%{
|
|
214 |
OBJ _zstreamObj = __INST( zstream );
|
|
215 |
|
|
216 |
if( _zstreamObj != nil )
|
|
217 |
{
|
1195
|
218 |
int _errorNo, _count;
|
|
219 |
zstream_s * _zstream;
|
1059
|
220 |
|
1195
|
221 |
_zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1059
|
222 |
|
1195
|
223 |
if( _zstream->op_mode != e_opmode_inflate )
|
|
224 |
RETURN( nil );
|
1059
|
225 |
|
1195
|
226 |
if( _zstream->stream.state == NULL )
|
|
227 |
{
|
|
228 |
_zstream->op_mode = e_opmode_unspecified;
|
|
229 |
RETURN( nil );
|
|
230 |
}
|
|
231 |
if( _zstream->stream.avail_in == 0 )
|
|
232 |
RETURN( __MKSMALLINT (0) );
|
1059
|
233 |
|
1195
|
234 |
_zstream->stream.avail_out = _zstream->out_total;
|
|
235 |
_zstream->stream.next_out = _zstream->out_ref;
|
1059
|
236 |
|
1195
|
237 |
_errorNo = BZ2_bzDecompress( & _zstream->stream );
|
1059
|
238 |
|
1195
|
239 |
if( _errorNo == BZ_STREAM_END )
|
|
240 |
_errorNo = BZ2_bzDecompressEnd( & _zstream->stream );
|
1059
|
241 |
|
1195
|
242 |
if( _errorNo == BZ_OK )
|
|
243 |
{
|
|
244 |
_count = _zstream->out_total - _zstream->stream.avail_out;
|
1059
|
245 |
|
1195
|
246 |
RETURN( __MKSMALLINT (_count) );
|
|
247 |
}
|
|
248 |
errorNo = __MKSMALLINT( _errorNo );
|
1059
|
249 |
}
|
|
250 |
%}.
|
|
251 |
errorNo ifNil:[ self errorNotOpen ].
|
|
252 |
self zerror:errorNo.
|
|
253 |
!
|
|
254 |
|
|
255 |
zinflateInit
|
|
256 |
"low level - inflateInit
|
|
257 |
"
|
|
258 |
|errorNo|
|
|
259 |
|
|
260 |
errorNo := nil.
|
|
261 |
|
|
262 |
%{
|
|
263 |
OBJ _zstreamObj = __INST( zstream );
|
|
264 |
|
|
265 |
if( _zstreamObj != nil )
|
|
266 |
{
|
1195
|
267 |
int _errorNo;
|
|
268 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1059
|
269 |
|
1195
|
270 |
_zstream->op_mode = e_opmode_inflate;
|
|
271 |
_errorNo = BZ2_bzDecompressInit( & _zstream->stream, 0, 1 );
|
1059
|
272 |
|
1195
|
273 |
if( _errorNo == BZ_OK )
|
|
274 |
RETURN( self );
|
1059
|
275 |
|
1195
|
276 |
_zstream->stream.avail_in = 0;
|
|
277 |
errorNo = __MKSMALLINT( _errorNo );
|
1059
|
278 |
}
|
|
279 |
%}.
|
|
280 |
errorNo ifNil:[ self errorNotOpen ].
|
|
281 |
self zerror:errorNo.
|
|
282 |
!
|
|
283 |
|
|
284 |
zopen
|
|
285 |
"low level - opens the zip stream
|
|
286 |
"
|
|
287 |
|outTotal|
|
|
288 |
|
|
289 |
outTotal := outputBytes size.
|
|
290 |
%{
|
|
291 |
zstream_s * _zstream = (zstream_s *) malloc( sizeof(zstream_s) );
|
|
292 |
|
|
293 |
if( _zstream )
|
|
294 |
{
|
1195
|
295 |
OBJ _zobj = __MKEXTERNALADDRESS( _zstream );
|
|
296 |
OBJ _outObj = __INST( outputBytes );
|
|
297 |
OBJ _inpObj = __INST( inputBytes );
|
1059
|
298 |
|
1195
|
299 |
_zstream->in_ref = (char *) __externalBytesAddress( _inpObj );
|
|
300 |
_zstream->stream.next_in = NULL;
|
|
301 |
_zstream->stream.avail_in = 0;
|
|
302 |
_zstream->stream.total_in_lo32 = 0;
|
|
303 |
_zstream->stream.total_in_hi32 = 0;
|
1059
|
304 |
|
1195
|
305 |
_zstream->out_total = __intVal( outTotal );
|
|
306 |
_zstream->out_ref = (char *) __externalBytesAddress( _outObj );
|
|
307 |
_zstream->stream.next_out = _zstream->out_ref;
|
|
308 |
_zstream->stream.avail_out = _zstream->out_total;
|
1059
|
309 |
|
1195
|
310 |
_zstream->stream.total_out_lo32 = 0;
|
|
311 |
_zstream->stream.total_out_hi32 = 0;
|
1059
|
312 |
|
1195
|
313 |
_zstream->stream.bzalloc = 0;
|
|
314 |
_zstream->stream.bzfree = 0;
|
|
315 |
_zstream->stream.opaque = 0;
|
1059
|
316 |
|
1195
|
317 |
_zstream->op_mode = e_opmode_unspecified;
|
1059
|
318 |
|
1195
|
319 |
__INST (zstream) = _zobj;
|
|
320 |
__STORE(self, _zobj);
|
1059
|
321 |
}
|
|
322 |
%}.
|
|
323 |
!
|
|
324 |
|
|
325 |
zset_avail_in:count
|
1061
|
326 |
"set the 'avail_in'
|
1059
|
327 |
"
|
|
328 |
%{
|
|
329 |
OBJ _zstreamObj = __INST( zstream );
|
|
330 |
|
|
331 |
if( (_zstreamObj != nil) && __isSmallInteger(count) )
|
|
332 |
{
|
1195
|
333 |
int _count;
|
|
334 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
|
1059
|
335 |
|
1195
|
336 |
if( (_count = __intVal( count )) > 0 )
|
|
337 |
{
|
|
338 |
char * _in_ref = _zstream->in_ref;
|
1059
|
339 |
|
1195
|
340 |
_zstream->stream.avail_in = _count;
|
|
341 |
_zstream->stream.next_in = _in_ref;
|
|
342 |
} else {
|
|
343 |
_zstream->stream.avail_in = 0;
|
|
344 |
_zstream->stream.next_in = NULL;
|
|
345 |
}
|
|
346 |
RETURN( self );
|
1059
|
347 |
}
|
|
348 |
%}.
|
|
349 |
zstream ifNil:[ self errorNotOpen ].
|
|
350 |
self invalidArguments.
|
|
351 |
! !
|
|
352 |
|
1196
|
353 |
!BZip2Stream class methodsFor:'documentation'!
|
1193
|
354 |
|
|
355 |
version
|
1308
|
356 |
^ '$Header: /cvs/stx/stx/libbasic2/BZip2Stream.st,v 1.7 2003-08-29 19:31:11 cg Exp $'
|
1193
|
357 |
! !
|