2 * THIS FILE IS NOT IDENTICAL TO THE ORIGINAL
3 * FROM THE BZIP2 DISTRIBUTION.
5 * It has been modified, mainly to break the library
9 * rsc@plan9.bell-labs.com
13 /*-------------------------------------------------------------*/
14 /*--- Library top-level functions. ---*/
16 /*-------------------------------------------------------------*/
19 This file is a part of bzip2 and/or libbzip2, a program and
20 library for lossless, block-sorting data compression.
22 Copyright (C) 1996-2000 Julian R Seward. All rights reserved.
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions
28 1. Redistributions of source code must retain the above copyright
29 notice, this list of conditions and the following disclaimer.
31 2. The origin of this software must not be misrepresented; you must
32 not claim that you wrote the original software. If you use this
33 software in a product, an acknowledgment in the product
34 documentation would be appreciated but is not required.
36 3. Altered source versions must be plainly marked as such, and must
37 not be misrepresented as being the original software.
39 4. The name of the author may not be used to endorse or promote
40 products derived from this software without specific prior written
43 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
44 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
47 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
49 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
52 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
53 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 Julian Seward, Cambridge, UK.
57 bzip2/libbzip2 version 1.0 of 21 March 2000
59 This program is based on (at least) the work of:
69 For more information on these sources, see the manual.
75 0.9.0 -- original version.
77 0.9.0a/b -- no changes in this file.
80 * made zero-length BZ_FLUSH work correctly in bzCompress().
81 * fixed bzWrite/bzRead to ignore zero-length requests.
82 * fixed bzread to correctly handle read requests after EOF.
83 * wrong parameter order in call to bzDecompressInit in
84 bzBuffToBuffDecompress. Fixed.
89 #include "bzlib_private.h"
91 /*---------------------------------------------------*/
92 /*--- Compression stuff ---*/
93 /*---------------------------------------------------*/
95 /*---------------------------------------------------*/
97 void prepare_new_block ( EState* s )
102 s->state_out_pos = 0;
103 BZ_INITIALISE_CRC ( s->blockCRC );
104 for (i = 0; i < 256; i++) s->inUse[i] = False;
109 /*---------------------------------------------------*/
111 void init_RL ( EState* s )
113 s->state_in_ch = 256;
119 Bool isempty_RL ( EState* s )
121 if (s->state_in_ch < 256 && s->state_in_len > 0)
127 /*---------------------------------------------------*/
128 int BZ_API(BZ2_bzCompressInit)
137 if (!bz_config_ok()) return BZ_CONFIG_ERROR;
140 blockSize100k < 1 || blockSize100k > 9 ||
141 workFactor < 0 || workFactor > 250)
142 return BZ_PARAM_ERROR;
144 if (workFactor == 0) workFactor = 30;
145 if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
146 if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
148 s = BZALLOC( sizeof(EState) );
149 if (s == NULL) return BZ_MEM_ERROR;
156 n = 100000 * blockSize100k;
157 s->arr1 = BZALLOC( n * sizeof(UInt32) );
158 s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) );
159 s->ftab = BZALLOC( 65537 * sizeof(UInt32) );
161 if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) {
162 if (s->arr1 != NULL) BZFREE(s->arr1);
163 if (s->arr2 != NULL) BZFREE(s->arr2);
164 if (s->ftab != NULL) BZFREE(s->ftab);
165 if (s != NULL) BZFREE(s);
170 s->state = BZ_S_INPUT;
171 s->mode = BZ_M_RUNNING;
173 s->blockSize100k = blockSize100k;
174 s->nblockMAX = 100000 * blockSize100k - 19;
175 s->verbosity = verbosity;
176 s->workFactor = workFactor;
178 s->block = (UChar*)s->arr2;
179 s->mtfv = (UInt16*)s->arr1;
181 s->ptr = (UInt32*)s->arr1;
184 strm->total_in_lo32 = 0;
185 strm->total_in_hi32 = 0;
186 strm->total_out_lo32 = 0;
187 strm->total_out_hi32 = 0;
189 prepare_new_block ( s );
194 /*---------------------------------------------------*/
196 void add_pair_to_block ( EState* s )
199 UChar ch = (UChar)(s->state_in_ch);
200 for (i = 0; i < s->state_in_len; i++) {
201 BZ_UPDATE_CRC( s->blockCRC, ch );
203 s->inUse[s->state_in_ch] = True;
204 switch (s->state_in_len) {
206 s->block[s->nblock] = (UChar)ch; s->nblock++;
209 s->block[s->nblock] = (UChar)ch; s->nblock++;
210 s->block[s->nblock] = (UChar)ch; s->nblock++;
213 s->block[s->nblock] = (UChar)ch; s->nblock++;
214 s->block[s->nblock] = (UChar)ch; s->nblock++;
215 s->block[s->nblock] = (UChar)ch; s->nblock++;
218 s->inUse[s->state_in_len-4] = True;
219 s->block[s->nblock] = (UChar)ch; s->nblock++;
220 s->block[s->nblock] = (UChar)ch; s->nblock++;
221 s->block[s->nblock] = (UChar)ch; s->nblock++;
222 s->block[s->nblock] = (UChar)ch; s->nblock++;
223 s->block[s->nblock] = ((UChar)(s->state_in_len-4));
230 /*---------------------------------------------------*/
232 void flush_RL ( EState* s )
234 if (s->state_in_ch < 256) add_pair_to_block ( s );
239 /*---------------------------------------------------*/
240 #define ADD_CHAR_TO_BLOCK(zs,zchh0) \
242 UInt32 zchh = (UInt32)(zchh0); \
243 /*-- fast track the common case --*/ \
244 if (zchh != zs->state_in_ch && \
245 zs->state_in_len == 1) { \
246 UChar ch = (UChar)(zs->state_in_ch); \
247 BZ_UPDATE_CRC( zs->blockCRC, ch ); \
248 zs->inUse[zs->state_in_ch] = True; \
249 zs->block[zs->nblock] = (UChar)ch; \
251 zs->state_in_ch = zchh; \
254 /*-- general, uncommon cases --*/ \
255 if (zchh != zs->state_in_ch || \
256 zs->state_in_len == 255) { \
257 if (zs->state_in_ch < 256) \
258 add_pair_to_block ( zs ); \
259 zs->state_in_ch = zchh; \
260 zs->state_in_len = 1; \
262 zs->state_in_len++; \
267 /*---------------------------------------------------*/
269 Bool copy_input_until_stop ( EState* s )
271 Bool progress_in = False;
273 if (s->mode == BZ_M_RUNNING) {
275 /*-- fast track the common case --*/
277 /*-- block full? --*/
278 if (s->nblock >= s->nblockMAX) break;
280 if (s->strm->avail_in == 0) break;
282 ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
285 s->strm->total_in_lo32++;
286 if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
291 /*-- general, uncommon case --*/
293 /*-- block full? --*/
294 if (s->nblock >= s->nblockMAX) break;
296 if (s->strm->avail_in == 0) break;
297 /*-- flush/finish end? --*/
298 if (s->avail_in_expect == 0) break;
300 ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
303 s->strm->total_in_lo32++;
304 if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
305 s->avail_in_expect--;
312 /*---------------------------------------------------*/
314 Bool copy_output_until_stop ( EState* s )
316 Bool progress_out = False;
320 /*-- no output space? --*/
321 if (s->strm->avail_out == 0) break;
323 /*-- block done? --*/
324 if (s->state_out_pos >= s->numZ) break;
327 *(s->strm->next_out) = s->zbits[s->state_out_pos];
329 s->strm->avail_out--;
331 s->strm->total_out_lo32++;
332 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
339 /*---------------------------------------------------*/
341 Bool handle_compress ( bz_stream* strm )
343 Bool progress_in = False;
344 Bool progress_out = False;
345 EState* s = strm->state;
349 if (s->state == BZ_S_OUTPUT) {
350 progress_out |= copy_output_until_stop ( s );
351 if (s->state_out_pos < s->numZ) break;
352 if (s->mode == BZ_M_FINISHING &&
353 s->avail_in_expect == 0 &&
354 isempty_RL(s)) break;
355 prepare_new_block ( s );
356 s->state = BZ_S_INPUT;
357 if (s->mode == BZ_M_FLUSHING &&
358 s->avail_in_expect == 0 &&
359 isempty_RL(s)) break;
362 if (s->state == BZ_S_INPUT) {
363 progress_in |= copy_input_until_stop ( s );
364 if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) {
366 BZ2_compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) );
367 s->state = BZ_S_OUTPUT;
370 if (s->nblock >= s->nblockMAX) {
371 BZ2_compressBlock ( s, False );
372 s->state = BZ_S_OUTPUT;
375 if (s->strm->avail_in == 0) {
382 return progress_in || progress_out;
385 /*---------------------------------------------------*/
386 int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action )
390 if (strm == NULL) return BZ_PARAM_ERROR;
392 if (s == NULL) return BZ_PARAM_ERROR;
393 if (s->strm != strm) return BZ_PARAM_ERROR;
399 return BZ_SEQUENCE_ERROR;
402 if (action == BZ_RUN) {
403 progress = handle_compress ( strm );
404 return progress ? BZ_RUN_OK : BZ_PARAM_ERROR;
407 if (action == BZ_FLUSH) {
408 s->avail_in_expect = strm->avail_in;
409 s->mode = BZ_M_FLUSHING;
413 if (action == BZ_FINISH) {
414 s->avail_in_expect = strm->avail_in;
415 s->mode = BZ_M_FINISHING;
419 return BZ_PARAM_ERROR;
422 if (action != BZ_FLUSH) return BZ_SEQUENCE_ERROR;
423 if (s->avail_in_expect != s->strm->avail_in)
424 return BZ_SEQUENCE_ERROR;
425 progress = handle_compress ( strm );
426 if (!progress) return BZ_SEQUENCE_ERROR; //rsc added
427 if (s->avail_in_expect > 0 || !isempty_RL(s) ||
428 s->state_out_pos < s->numZ) return BZ_FLUSH_OK;
429 s->mode = BZ_M_RUNNING;
433 if (action != BZ_FINISH) return BZ_SEQUENCE_ERROR;
434 if (s->avail_in_expect != s->strm->avail_in)
435 return BZ_SEQUENCE_ERROR;
436 progress = handle_compress ( strm );
437 if (!progress) return BZ_SEQUENCE_ERROR;
438 if (s->avail_in_expect > 0 || !isempty_RL(s) ||
439 s->state_out_pos < s->numZ) return BZ_FINISH_OK;
441 return BZ_STREAM_END;
443 return BZ_OK; /*--not reached--*/
447 /*---------------------------------------------------*/
448 int BZ_API(BZ2_bzCompressEnd) ( bz_stream *strm )
451 if (strm == NULL) return BZ_PARAM_ERROR;
453 if (s == NULL) return BZ_PARAM_ERROR;
454 if (s->strm != strm) return BZ_PARAM_ERROR;
456 if (s->arr1 != NULL) BZFREE(s->arr1);
457 if (s->arr2 != NULL) BZFREE(s->arr2);
458 if (s->ftab != NULL) BZFREE(s->ftab);