author | Merge Script |
Wed, 31 Aug 2016 06:46:12 +0200 | |
branch | jv |
changeset 4055 | a3ec99ac3663 |
parent 3971 | 21bb0786f068 |
parent 4054 | 54f989be66a1 |
child 4276 | 956420daf17d |
permissions | -rw-r--r-- |
1308 | 1 |
" |
2 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
3916 | 3 |
All Rights Reserved |
1308 | 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 |
||
1838 | 30 |
#include <stdio.h> |
3971
21bb0786f068
BZip2Stream: #include <stdlib.h> to get a function prototype for of `free()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3916
diff
changeset
|
31 |
#include <stdlib.h> |
3915 | 32 |
|
33 |
#ifdef WIN32 |
|
34 |
// sigh - bzlib.h includes windows.h, which does not like ST/X definitions |
|
3916 | 35 |
# undef Process |
36 |
# undef Processor |
|
37 |
# undef Context |
|
38 |
# undef Array |
|
39 |
# undef Date |
|
40 |
# undef Method |
|
41 |
# undef Delay |
|
42 |
# undef Character |
|
43 |
# undef String |
|
44 |
# undef Time |
|
45 |
# undef Signal |
|
46 |
# undef Set |
|
47 |
# undef Message |
|
48 |
# undef Rectangle |
|
49 |
# undef Block |
|
50 |
# undef Object |
|
51 |
# undef Point |
|
52 |
# undef INT |
|
53 |
# undef UINT |
|
54 |
# define NO_STDIO |
|
55 |
# if defined(__i386__) || defined(__x86__) |
|
56 |
# define _X86_ |
|
57 |
# endif |
|
3915 | 58 |
#endif |
59 |
||
1838 | 60 |
#include "bzlib.h" |
1059 | 61 |
|
3915 | 62 |
#ifdef WIN32 |
3916 | 63 |
# define INT STX_INT |
64 |
# define UINT STX_UINT |
|
65 |
# ifdef STX_Process |
|
66 |
# define Process STX_Process |
|
67 |
# endif |
|
68 |
# ifdef STX_Processor |
|
69 |
# define Processor STX_Processor |
|
70 |
# endif |
|
71 |
# ifdef STX_Context |
|
72 |
# define Context STX_Context |
|
73 |
# endif |
|
74 |
# ifdef STX_Array |
|
75 |
# define Array STX_Array |
|
76 |
# endif |
|
77 |
# ifdef STX_Date |
|
78 |
# define Date STX_Date |
|
79 |
# endif |
|
80 |
# ifdef STX_Method |
|
81 |
# define Method STX_Method |
|
82 |
# endif |
|
83 |
# ifdef STX_Delay |
|
84 |
# define Delay STX_Delay |
|
85 |
# endif |
|
86 |
# ifdef STX_Character |
|
87 |
# define Character STX_Character |
|
88 |
# endif |
|
89 |
# ifdef STX_String |
|
90 |
# define String STX_String |
|
91 |
# endif |
|
92 |
# ifdef STX_Time |
|
93 |
# define Time STX_Time |
|
94 |
# endif |
|
95 |
# ifdef STX_Signal |
|
96 |
# define Signal STX_Signal |
|
97 |
# endif |
|
98 |
# ifdef STX_Set |
|
99 |
# define Set STX_Set |
|
100 |
# endif |
|
101 |
# ifdef STX_Message |
|
102 |
# define Message STX_Message |
|
103 |
# endif |
|
104 |
# ifdef STX_Rectangle |
|
105 |
# define Rectangle STX_Rectangle |
|
106 |
# endif |
|
107 |
# ifdef STX_Block |
|
108 |
# define Block STX_Block |
|
109 |
# endif |
|
110 |
# ifdef STX_Object |
|
111 |
# define Object STX_Object |
|
112 |
# endif |
|
113 |
# ifdef STX_Point |
|
114 |
# define Point STXPoint |
|
115 |
# endif |
|
3915 | 116 |
#endif |
117 |
||
1059 | 118 |
typedef enum { |
1195 | 119 |
e_opmode_unspecified /* processing done */ |
120 |
, e_opmode_deflate /* running deflate */ |
|
121 |
, e_opmode_inflate /* running inflate */ |
|
1059 | 122 |
} e_opmode; |
123 |
||
124 |
typedef struct { |
|
1195 | 125 |
bz_stream stream; /* pointer to the external in -stream */ |
126 |
char * in_ref; /* size of the external in -stream */ |
|
127 |
char * out_ref; /* pointer to the external out-stream */ |
|
128 |
unsigned int out_total; /* size of the external out-stream */ |
|
1059 | 129 |
|
1195 | 130 |
e_opmode op_mode; /* current operational mode */ |
1059 | 131 |
} zstream_s; |
132 |
||
133 |
%} |
|
134 |
! ! |
|
135 |
||
1308 | 136 |
!BZip2Stream class methodsFor:'documentation'! |
137 |
||
138 |
copyright |
|
139 |
" |
|
140 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
3916 | 141 |
All Rights Reserved |
1308 | 142 |
|
143 |
This software is furnished under a license and may be used |
|
144 |
only in accordance with the terms of that license and with the |
|
145 |
inclusion of the above copyright notice. This software may not |
|
146 |
be provided or otherwise made available to, or used by, any |
|
147 |
other person. No title to or ownership of the software is |
|
148 |
hereby transferred. |
|
149 |
" |
|
150 |
! ! |
|
1193 | 151 |
|
1196 | 152 |
!BZip2Stream methodsFor:'low level'! |
1059 | 153 |
|
154 |
zclose |
|
155 |
"low level close of the zip stream |
|
156 |
" |
|
157 |
%{ |
|
158 |
OBJ _zstreamObj = __INST( zstream ); |
|
159 |
||
160 |
if( _zstreamObj != nil ) |
|
161 |
{ |
|
1195 | 162 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj ); |
1059 | 163 |
|
1195 | 164 |
__INST(zstream) = nil; |
1059 | 165 |
|
1195 | 166 |
if( _zstream->stream.state != NULL ) |
167 |
{ |
|
168 |
if( _zstream->op_mode == e_opmode_inflate ) |
|
169 |
BZ2_bzDecompressEnd( & _zstream->stream ); |
|
170 |
else |
|
171 |
BZ2_bzCompressEnd( & _zstream->stream ); |
|
172 |
} |
|
173 |
free( _zstream ); |
|
1059 | 174 |
} |
175 |
%}. |
|
176 |
! |
|
177 |
||
178 |
zdeflate |
|
1570 | 179 |
"low level - deflate" |
180 |
||
1059 | 181 |
|errorNo| |
182 |
errorNo := nil. |
|
183 |
%{ |
|
184 |
OBJ _zstreamObj = __INST( zstream ); |
|
185 |
||
186 |
if( _zstreamObj != nil ) |
|
187 |
{ |
|
3916 | 188 |
int _errorNo, _action; |
189 |
unsigned int _bfsize; |
|
190 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj ); |
|
1059 | 191 |
|
3916 | 192 |
if( _zstream->op_mode != e_opmode_deflate ) |
193 |
RETURN( nil ); |
|
1059 | 194 |
|
3916 | 195 |
_bfsize = _zstream->out_total; |
1059 | 196 |
|
3916 | 197 |
if( _zstream->stream.state == NULL ) |
198 |
{ |
|
199 |
_zstream->op_mode = e_opmode_unspecified; |
|
200 |
RETURN( nil ); |
|
201 |
} |
|
202 |
_action = (__INST(hitEOF) == true) ? BZ_FINISH : BZ_RUN; |
|
1059 | 203 |
|
3916 | 204 |
_zstream->stream.avail_out = _bfsize; |
205 |
_zstream->stream.next_out = _zstream->out_ref; |
|
206 |
||
207 |
_errorNo = BZ2_bzCompress( & _zstream->stream, _action ); |
|
1059 | 208 |
|
3916 | 209 |
if( _errorNo == BZ_STREAM_END ) |
210 |
{ |
|
211 |
_zstream->stream.avail_in = 0; |
|
212 |
_zstream->stream.next_in = NULL; |
|
213 |
_errorNo = BZ2_bzCompressEnd( & _zstream->stream ); |
|
214 |
} |
|
1059 | 215 |
|
3916 | 216 |
if( (_errorNo == BZ_OK) |
217 |
|| (_errorNo == BZ_RUN_OK) |
|
218 |
|| (_errorNo == BZ_FINISH_OK) |
|
219 |
) |
|
220 |
{ |
|
221 |
if( (_zstream->stream.avail_out != _bfsize) |
|
222 |
|| (_zstream->stream.avail_in != 0) |
|
223 |
) |
|
224 |
RETURN( true ); |
|
1059 | 225 |
|
3916 | 226 |
RETURN( false ); |
227 |
} |
|
228 |
errorNo = __MKSMALLINT( _errorNo ); |
|
1059 | 229 |
} |
230 |
%}. |
|
4054 | 231 |
errorNo isNil ifTrue:[ |
232 |
zstream isNil ifTrue:[self errorNotOpen]. |
|
3916 | 233 |
self invalidArgument. |
1059 | 234 |
]. |
235 |
self zerror:errorNo. |
|
236 |
! |
|
237 |
||
238 |
zdeflateInit |
|
1570 | 239 |
"low level - open for reading" |
240 |
||
1059 | 241 |
|errorNo blockSize100k workFactor| |
242 |
||
243 |
errorNo := nil. |
|
244 |
blockSize100k := BlockSize. |
|
245 |
workFactor := 30. |
|
246 |
%{ |
|
247 |
OBJ _zstreamObj = __INST( zstream ); |
|
248 |
||
249 |
if( (_zstreamObj != nil) && __bothSmallInteger(blockSize100k, workFactor) ) |
|
250 |
{ |
|
3916 | 251 |
int _errorNo; |
252 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj ); |
|
1059 | 253 |
|
3916 | 254 |
_zstream->op_mode = e_opmode_deflate; |
1059 | 255 |
|
3916 | 256 |
_errorNo = BZ2_bzCompressInit( & _zstream->stream |
257 |
, __intVal( blockSize100k ), 0, __intVal( workFactor ) ); |
|
1059 | 258 |
|
3916 | 259 |
if( _errorNo == BZ_OK ) |
260 |
RETURN( self ); |
|
1059 | 261 |
|
3916 | 262 |
errorNo = __MKSMALLINT( _errorNo ); |
1059 | 263 |
} |
264 |
%}. |
|
4054 | 265 |
errorNo isNil ifTrue:[ |
266 |
zstream isNil ifTrue:[ self errorNotOpen ]. |
|
3916 | 267 |
self invalidArgument. |
1059 | 268 |
]. |
269 |
self zerror:errorNo. |
|
270 |
! |
|
271 |
||
272 |
zget_avail_out |
|
273 |
"low level - get the number of available out bytes |
|
274 |
" |
|
275 |
%{ |
|
276 |
OBJ _zstreamObj = __INST( zstream ); |
|
277 |
||
278 |
if( _zstreamObj != nil ) |
|
279 |
{ |
|
1195 | 280 |
unsigned int _count; |
281 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj ); |
|
1059 | 282 |
|
1195 | 283 |
_count = _zstream->out_total - _zstream->stream.avail_out; |
1059 | 284 |
|
1195 | 285 |
RETURN( __MKSMALLINT (_count) ); |
1059 | 286 |
} |
287 |
%}. |
|
288 |
self errorNotOpen. |
|
289 |
! |
|
290 |
||
291 |
zinflate |
|
292 |
"low level - inflate |
|
293 |
" |
|
294 |
|errorNo| |
|
295 |
||
296 |
errorNo := nil. |
|
297 |
%{ |
|
298 |
OBJ _zstreamObj = __INST( zstream ); |
|
299 |
||
300 |
if( _zstreamObj != nil ) |
|
301 |
{ |
|
1195 | 302 |
int _errorNo, _count; |
303 |
zstream_s * _zstream; |
|
1059 | 304 |
|
1195 | 305 |
_zstream = (zstream_s *) __externalBytesAddress( _zstreamObj ); |
1059 | 306 |
|
1195 | 307 |
if( _zstream->op_mode != e_opmode_inflate ) |
308 |
RETURN( nil ); |
|
1059 | 309 |
|
1195 | 310 |
if( _zstream->stream.state == NULL ) |
311 |
{ |
|
312 |
_zstream->op_mode = e_opmode_unspecified; |
|
313 |
RETURN( nil ); |
|
314 |
} |
|
315 |
if( _zstream->stream.avail_in == 0 ) |
|
316 |
RETURN( __MKSMALLINT (0) ); |
|
1059 | 317 |
|
1195 | 318 |
_zstream->stream.avail_out = _zstream->out_total; |
319 |
_zstream->stream.next_out = _zstream->out_ref; |
|
1059 | 320 |
|
1195 | 321 |
_errorNo = BZ2_bzDecompress( & _zstream->stream ); |
1059 | 322 |
|
1195 | 323 |
if( _errorNo == BZ_STREAM_END ) |
324 |
_errorNo = BZ2_bzDecompressEnd( & _zstream->stream ); |
|
1059 | 325 |
|
1195 | 326 |
if( _errorNo == BZ_OK ) |
327 |
{ |
|
328 |
_count = _zstream->out_total - _zstream->stream.avail_out; |
|
1059 | 329 |
|
1195 | 330 |
RETURN( __MKSMALLINT (_count) ); |
331 |
} |
|
332 |
errorNo = __MKSMALLINT( _errorNo ); |
|
1059 | 333 |
} |
334 |
%}. |
|
4054 | 335 |
errorNo isNil ifTrue:[ self errorNotOpen ]. |
1059 | 336 |
self zerror:errorNo. |
337 |
! |
|
338 |
||
339 |
zinflateInit |
|
340 |
"low level - inflateInit |
|
341 |
" |
|
342 |
|errorNo| |
|
343 |
||
344 |
errorNo := nil. |
|
345 |
||
346 |
%{ |
|
347 |
OBJ _zstreamObj = __INST( zstream ); |
|
348 |
||
349 |
if( _zstreamObj != nil ) |
|
350 |
{ |
|
1195 | 351 |
int _errorNo; |
352 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj ); |
|
1059 | 353 |
|
1195 | 354 |
_zstream->op_mode = e_opmode_inflate; |
355 |
_errorNo = BZ2_bzDecompressInit( & _zstream->stream, 0, 1 ); |
|
1059 | 356 |
|
1195 | 357 |
if( _errorNo == BZ_OK ) |
358 |
RETURN( self ); |
|
1059 | 359 |
|
1195 | 360 |
_zstream->stream.avail_in = 0; |
361 |
errorNo = __MKSMALLINT( _errorNo ); |
|
1059 | 362 |
} |
363 |
%}. |
|
4054 | 364 |
errorNo isNil ifTrue:[ self errorNotOpen ]. |
1059 | 365 |
self zerror:errorNo. |
366 |
! |
|
367 |
||
368 |
zopen |
|
369 |
"low level - opens the zip stream |
|
370 |
" |
|
371 |
|outTotal| |
|
372 |
||
373 |
outTotal := outputBytes size. |
|
374 |
%{ |
|
1750 | 375 |
extern void *malloc(); |
1564 | 376 |
|
1059 | 377 |
zstream_s * _zstream = (zstream_s *) malloc( sizeof(zstream_s) ); |
378 |
||
379 |
if( _zstream ) |
|
380 |
{ |
|
1195 | 381 |
OBJ _zobj = __MKEXTERNALADDRESS( _zstream ); |
382 |
OBJ _outObj = __INST( outputBytes ); |
|
383 |
OBJ _inpObj = __INST( inputBytes ); |
|
1059 | 384 |
|
1195 | 385 |
_zstream->in_ref = (char *) __externalBytesAddress( _inpObj ); |
386 |
_zstream->stream.next_in = NULL; |
|
387 |
_zstream->stream.avail_in = 0; |
|
388 |
_zstream->stream.total_in_lo32 = 0; |
|
389 |
_zstream->stream.total_in_hi32 = 0; |
|
1059 | 390 |
|
1195 | 391 |
_zstream->out_total = __intVal( outTotal ); |
392 |
_zstream->out_ref = (char *) __externalBytesAddress( _outObj ); |
|
393 |
_zstream->stream.next_out = _zstream->out_ref; |
|
394 |
_zstream->stream.avail_out = _zstream->out_total; |
|
1059 | 395 |
|
1195 | 396 |
_zstream->stream.total_out_lo32 = 0; |
397 |
_zstream->stream.total_out_hi32 = 0; |
|
1059 | 398 |
|
1195 | 399 |
_zstream->stream.bzalloc = 0; |
400 |
_zstream->stream.bzfree = 0; |
|
401 |
_zstream->stream.opaque = 0; |
|
1059 | 402 |
|
1195 | 403 |
_zstream->op_mode = e_opmode_unspecified; |
1059 | 404 |
|
1195 | 405 |
__INST (zstream) = _zobj; |
406 |
__STORE(self, _zobj); |
|
1059 | 407 |
} |
408 |
%}. |
|
409 |
! |
|
410 |
||
411 |
zset_avail_in:count |
|
1570 | 412 |
"set the 'avail_in'" |
1059 | 413 |
%{ |
414 |
OBJ _zstreamObj = __INST( zstream ); |
|
415 |
||
416 |
if( (_zstreamObj != nil) && __isSmallInteger(count) ) |
|
417 |
{ |
|
3916 | 418 |
int _count; |
419 |
zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj ); |
|
1059 | 420 |
|
3916 | 421 |
if( (_count = __intVal( count )) > 0 ) |
422 |
{ |
|
423 |
char * _in_ref = _zstream->in_ref; |
|
1059 | 424 |
|
3916 | 425 |
_zstream->stream.avail_in = _count; |
426 |
_zstream->stream.next_in = _in_ref; |
|
427 |
} else { |
|
428 |
_zstream->stream.avail_in = 0; |
|
429 |
_zstream->stream.next_in = NULL; |
|
430 |
} |
|
431 |
RETURN( self ); |
|
1059 | 432 |
} |
433 |
%}. |
|
4054 | 434 |
zstream isNil ifTrue:[ self errorNotOpen ]. |
1570 | 435 |
self invalidArgument. |
1059 | 436 |
! ! |
437 |
||
1196 | 438 |
!BZip2Stream class methodsFor:'documentation'! |
1193 | 439 |
|
440 |
version |
|
3915 | 441 |
^ '$Header$' |
1193 | 442 |
! ! |