6 * This file is a modified version of bzlib.c from the bzip2-1.0.2
7 * distribution which can be found at http://sources.redhat.com/bzip2/
10 /*-------------------------------------------------------------*/
11 /*--- Library top-level functions. ---*/
13 /*-------------------------------------------------------------*/
16 This file is a part of bzip2 and/or libbzip2, a program and
17 library for lossless, block-sorting data compression.
19 Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions
25 1. Redistributions of source code must retain the above copyright
26 notice, this list of conditions and the following disclaimer.
28 2. The origin of this software must not be misrepresented; you must
29 not claim that you wrote the original software. If you use this
30 software in a product, an acknowledgment in the product
31 documentation would be appreciated but is not required.
33 3. Altered source versions must be plainly marked as such, and must
34 not be misrepresented as being the original software.
36 4. The name of the author may not be used to endorse or promote
37 products derived from this software without specific prior written
40 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
41 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
42 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
44 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
46 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
48 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 Julian Seward, Cambridge, UK.
54 bzip2/libbzip2 version 1.0 of 21 March 2000
56 This program is based on (at least) the work of:
66 For more information on these sources, see the manual.
72 0.9.0 -- original version.
74 0.9.0a/b -- no changes in this file.
77 * made zero-length BZ_FLUSH work correctly in bzCompress().
78 * fixed bzWrite/bzRead to ignore zero-length requests.
79 * fixed bzread to correctly handle read requests after EOF.
80 * wrong parameter order in call to bzDecompressInit in
81 bzBuffToBuffDecompress. Fixed.
84 #include "bzlib_private.h"
86 /*---------------------------------------------------*/
87 /*--- Compression stuff ---*/
88 /*---------------------------------------------------*/
91 /*---------------------------------------------------*/
93 void BZ2_bz__AssertH__fail ( int errcode )
96 "\n\nbzip2/libbzip2: internal error number %d.\n"
97 "This is a bug in bzip2/libbzip2, %s.\n"
98 "Please report it to me at: jseward@acm.org. If this happened\n"
99 "when you were using some program which uses libbzip2 as a\n"
100 "component, you should also report this bug to the author(s)\n"
101 "of that program. Please make an effort to report this bug;\n"
102 "timely and accurate bug reports eventually lead to higher\n"
103 "quality software. Thanks. Julian Seward, 30 December 2001.\n\n",
108 if (errcode == 1007) {
110 "\n*** A special note about internal error number 1007 ***\n"
112 "Experience suggests that a common cause of i.e. 1007\n"
113 "is unreliable memory or other hardware. The 1007 assertion\n"
114 "just happens to cross-check the results of huge numbers of\n"
115 "memory reads/writes, and so acts (unintendedly) as a stress\n"
116 "test of your memory system.\n"
118 "I suggest the following: try compressing the file again,\n"
119 "possibly monitoring progress in detail with the -vv flag.\n"
121 "* If the error cannot be reproduced, and/or happens at different\n"
122 " points in compression, you may have a flaky memory system.\n"
123 " Try a memory-test program. I have used Memtest86\n"
124 " (www.memtest86.com). At the time of writing it is free (GPLd).\n"
125 " Memtest86 tests memory much more thorougly than your BIOSs\n"
126 " power-on test, and may find failures that the BIOS doesn't.\n"
128 "* If the error can be repeatably reproduced, this is a bug in\n"
129 " bzip2, and I would very much like to hear about it. Please\n"
130 " let me know, and, ideally, save a copy of the file causing the\n"
131 " problem -- without which I will be unable to investigate it.\n"
141 /*---------------------------------------------------*/
143 int bz_config_ok ( void )
145 if (sizeof(int) != 4) return 0;
146 if (sizeof(short) != 2) return 0;
147 if (sizeof(char) != 1) return 0;
152 /*---------------------------------------------------*/
154 void* default_bzalloc ( void* opaque, Int32 items, Int32 size )
156 void* v = malloc ( items * size );
161 void default_bzfree ( void* opaque, void* addr )
163 if (addr != NULL) free ( addr );
166 #ifndef BZ_NO_COMPRESS
167 /*---------------------------------------------------*/
169 void prepare_new_block ( EState* s )
174 s->state_out_pos = 0;
175 BZ_INITIALISE_CRC ( s->blockCRC );
176 for (i = 0; i < 256; i++) s->inUse[i] = False;
181 /*---------------------------------------------------*/
183 void init_RL ( EState* s )
185 s->state_in_ch = 256;
191 Bool isempty_RL ( EState* s )
193 if (s->state_in_ch < 256 && s->state_in_len > 0)
198 /*---------------------------------------------------*/
199 int BZ_API(BZ2_bzCompressInit)
208 if (!bz_config_ok()) return BZ_CONFIG_ERROR;
211 blockSize100k < 1 || blockSize100k > 9 ||
212 workFactor < 0 || workFactor > 250)
213 return BZ_PARAM_ERROR;
215 if (workFactor == 0) workFactor = 30;
216 if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
217 if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
219 s = BZALLOC( sizeof(EState) );
220 if (s == NULL) return BZ_MEM_ERROR;
227 n = 100000 * blockSize100k;
228 s->arr1 = BZALLOC( n * sizeof(UInt32) );
229 s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) );
230 s->ftab = BZALLOC( 65537 * sizeof(UInt32) );
232 if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) {
233 if (s->arr1 != NULL) BZFREE(s->arr1);
234 if (s->arr2 != NULL) BZFREE(s->arr2);
235 if (s->ftab != NULL) BZFREE(s->ftab);
236 if (s != NULL) BZFREE(s);
241 s->state = BZ_S_INPUT;
242 s->mode = BZ_M_RUNNING;
244 s->blockSize100k = blockSize100k;
245 s->nblockMAX = 100000 * blockSize100k - 19;
246 s->verbosity = verbosity;
247 s->workFactor = workFactor;
249 s->block = (UChar*)s->arr2;
250 s->mtfv = (UInt16*)s->arr1;
252 s->ptr = (UInt32*)s->arr1;
255 strm->total_in_lo32 = 0;
256 strm->total_in_hi32 = 0;
257 strm->total_out_lo32 = 0;
258 strm->total_out_hi32 = 0;
260 prepare_new_block ( s );
265 /*---------------------------------------------------*/
267 void add_pair_to_block ( EState* s )
270 UChar ch = (UChar)(s->state_in_ch);
271 for (i = 0; i < s->state_in_len; i++) {
272 BZ_UPDATE_CRC( s->blockCRC, ch );
274 s->inUse[s->state_in_ch] = True;
275 switch (s->state_in_len) {
277 s->block[s->nblock] = (UChar)ch; s->nblock++;
280 s->block[s->nblock] = (UChar)ch; s->nblock++;
281 s->block[s->nblock] = (UChar)ch; s->nblock++;
284 s->block[s->nblock] = (UChar)ch; s->nblock++;
285 s->block[s->nblock] = (UChar)ch; s->nblock++;
286 s->block[s->nblock] = (UChar)ch; s->nblock++;
289 s->inUse[s->state_in_len-4] = True;
290 s->block[s->nblock] = (UChar)ch; s->nblock++;
291 s->block[s->nblock] = (UChar)ch; s->nblock++;
292 s->block[s->nblock] = (UChar)ch; s->nblock++;
293 s->block[s->nblock] = (UChar)ch; s->nblock++;
294 s->block[s->nblock] = ((UChar)(s->state_in_len-4));
301 /*---------------------------------------------------*/
303 void flush_RL ( EState* s )
305 if (s->state_in_ch < 256) add_pair_to_block ( s );
310 /*---------------------------------------------------*/
311 #define ADD_CHAR_TO_BLOCK(zs,zchh0) \
313 UInt32 zchh = (UInt32)(zchh0); \
314 /*-- fast track the common case --*/ \
315 if (zchh != zs->state_in_ch && \
316 zs->state_in_len == 1) { \
317 UChar ch = (UChar)(zs->state_in_ch); \
318 BZ_UPDATE_CRC( zs->blockCRC, ch ); \
319 zs->inUse[zs->state_in_ch] = True; \
320 zs->block[zs->nblock] = (UChar)ch; \
322 zs->state_in_ch = zchh; \
325 /*-- general, uncommon cases --*/ \
326 if (zchh != zs->state_in_ch || \
327 zs->state_in_len == 255) { \
328 if (zs->state_in_ch < 256) \
329 add_pair_to_block ( zs ); \
330 zs->state_in_ch = zchh; \
331 zs->state_in_len = 1; \
333 zs->state_in_len++; \
338 /*---------------------------------------------------*/
340 Bool copy_input_until_stop ( EState* s )
342 Bool progress_in = False;
344 if (s->mode == BZ_M_RUNNING) {
346 /*-- fast track the common case --*/
348 /*-- block full? --*/
349 if (s->nblock >= s->nblockMAX) break;
351 if (s->strm->avail_in == 0) break;
353 ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
356 s->strm->total_in_lo32++;
357 if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
362 /*-- general, uncommon case --*/
364 /*-- block full? --*/
365 if (s->nblock >= s->nblockMAX) break;
367 if (s->strm->avail_in == 0) break;
368 /*-- flush/finish end? --*/
369 if (s->avail_in_expect == 0) break;
371 ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
374 s->strm->total_in_lo32++;
375 if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
376 s->avail_in_expect--;
383 /*---------------------------------------------------*/
385 Bool copy_output_until_stop ( EState* s )
387 Bool progress_out = False;
391 /*-- no output space? --*/
392 if (s->strm->avail_out == 0) break;
394 /*-- block done? --*/
395 if (s->state_out_pos >= s->numZ) break;
398 *(s->strm->next_out) = s->zbits[s->state_out_pos];
400 s->strm->avail_out--;
402 s->strm->total_out_lo32++;
403 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
410 /*---------------------------------------------------*/
412 Bool handle_compress ( bz_stream* strm )
414 Bool progress_in = False;
415 Bool progress_out = False;
416 EState* s = strm->state;
420 if (s->state == BZ_S_OUTPUT) {
421 progress_out |= copy_output_until_stop ( s );
422 if (s->state_out_pos < s->numZ) break;
423 if (s->mode == BZ_M_FINISHING &&
424 s->avail_in_expect == 0 &&
425 isempty_RL(s)) break;
426 prepare_new_block ( s );
427 s->state = BZ_S_INPUT;
428 if (s->mode == BZ_M_FLUSHING &&
429 s->avail_in_expect == 0 &&
430 isempty_RL(s)) break;
433 if (s->state == BZ_S_INPUT) {
434 progress_in |= copy_input_until_stop ( s );
435 if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) {
437 BZ2_compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) );
438 s->state = BZ_S_OUTPUT;
441 if (s->nblock >= s->nblockMAX) {
442 BZ2_compressBlock ( s, False );
443 s->state = BZ_S_OUTPUT;
446 if (s->strm->avail_in == 0) {
453 return progress_in || progress_out;
457 /*---------------------------------------------------*/
458 int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action )
462 if (strm == NULL) return BZ_PARAM_ERROR;
464 if (s == NULL) return BZ_PARAM_ERROR;
465 if (s->strm != strm) return BZ_PARAM_ERROR;
471 return BZ_SEQUENCE_ERROR;
474 if (action == BZ_RUN) {
475 progress = handle_compress ( strm );
476 return progress ? BZ_RUN_OK : BZ_PARAM_ERROR;
479 if (action == BZ_FLUSH) {
480 s->avail_in_expect = strm->avail_in;
481 s->mode = BZ_M_FLUSHING;
485 if (action == BZ_FINISH) {
486 s->avail_in_expect = strm->avail_in;
487 s->mode = BZ_M_FINISHING;
491 return BZ_PARAM_ERROR;
494 if (action != BZ_FLUSH) return BZ_SEQUENCE_ERROR;
495 if (s->avail_in_expect != s->strm->avail_in)
496 return BZ_SEQUENCE_ERROR;
497 progress = handle_compress ( strm );
498 if (s->avail_in_expect > 0 || !isempty_RL(s) ||
499 s->state_out_pos < s->numZ) return BZ_FLUSH_OK;
500 s->mode = BZ_M_RUNNING;
504 if (action != BZ_FINISH) return BZ_SEQUENCE_ERROR;
505 if (s->avail_in_expect != s->strm->avail_in)
506 return BZ_SEQUENCE_ERROR;
507 progress = handle_compress ( strm );
508 if (!progress) return BZ_SEQUENCE_ERROR;
509 if (s->avail_in_expect > 0 || !isempty_RL(s) ||
510 s->state_out_pos < s->numZ) return BZ_FINISH_OK;
512 return BZ_STREAM_END;
514 return BZ_OK; /*--not reached--*/
518 /*---------------------------------------------------*/
519 int BZ_API(BZ2_bzCompressEnd) ( bz_stream *strm )
522 if (strm == NULL) return BZ_PARAM_ERROR;
524 if (s == NULL) return BZ_PARAM_ERROR;
525 if (s->strm != strm) return BZ_PARAM_ERROR;
527 if (s->arr1 != NULL) BZFREE(s->arr1);
528 if (s->arr2 != NULL) BZFREE(s->arr2);
529 if (s->ftab != NULL) BZFREE(s->ftab);
536 #endif /* BZ_NO_COMPRESS */
538 /*---------------------------------------------------*/
539 /*--- Decompression stuff ---*/
540 /*---------------------------------------------------*/
542 /*---------------------------------------------------*/
543 int BZ_API(BZ2_bzDecompressInit)
550 if (!bz_config_ok()) return BZ_CONFIG_ERROR;
552 if (strm == NULL) return BZ_PARAM_ERROR;
553 if (small != 0 && small != 1) return BZ_PARAM_ERROR;
554 if (verbosity < 0 || verbosity > 4) return BZ_PARAM_ERROR;
556 if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
557 if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
559 s = BZALLOC( sizeof(DState) );
560 if (s == NULL) return BZ_MEM_ERROR;
563 s->state = BZ_X_MAGIC_1;
566 s->calculatedCombinedCRC = 0;
567 strm->total_in_lo32 = 0;
568 strm->total_in_hi32 = 0;
569 strm->total_out_lo32 = 0;
570 strm->total_out_hi32 = 0;
571 s->smallDecompress = (Bool)small;
576 s->verbosity = verbosity;
582 /*---------------------------------------------------*/
584 void unRLE_obuf_to_output_FAST ( DState* s )
588 if (s->blockRandomised) {
591 /* try to finish existing run */
593 if (s->strm->avail_out == 0) return;
594 if (s->state_out_len == 0) break;
595 *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
596 BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
599 s->strm->avail_out--;
600 s->strm->total_out_lo32++;
601 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
604 /* can a new run be started? */
605 if (s->nblock_used == s->save_nblock+1) return;
608 s->state_out_len = 1;
609 s->state_out_ch = s->k0;
610 BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
611 k1 ^= BZ_RAND_MASK; s->nblock_used++;
612 if (s->nblock_used == s->save_nblock+1) continue;
613 if (k1 != s->k0) { s->k0 = k1; continue; };
615 s->state_out_len = 2;
616 BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
617 k1 ^= BZ_RAND_MASK; s->nblock_used++;
618 if (s->nblock_used == s->save_nblock+1) continue;
619 if (k1 != s->k0) { s->k0 = k1; continue; };
621 s->state_out_len = 3;
622 BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
623 k1 ^= BZ_RAND_MASK; s->nblock_used++;
624 if (s->nblock_used == s->save_nblock+1) continue;
625 if (k1 != s->k0) { s->k0 = k1; continue; };
627 BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
628 k1 ^= BZ_RAND_MASK; s->nblock_used++;
629 s->state_out_len = ((Int32)k1) + 4;
630 BZ_GET_FAST(s->k0); BZ_RAND_UPD_MASK;
631 s->k0 ^= BZ_RAND_MASK; s->nblock_used++;
637 UInt32 c_calculatedBlockCRC = s->calculatedBlockCRC;
638 UChar c_state_out_ch = s->state_out_ch;
639 Int32 c_state_out_len = s->state_out_len;
640 Int32 c_nblock_used = s->nblock_used;
642 UInt32* c_tt = s->tt;
643 UInt32 c_tPos = s->tPos;
644 char* cs_next_out = s->strm->next_out;
645 unsigned int cs_avail_out = s->strm->avail_out;
648 UInt32 avail_out_INIT = cs_avail_out;
649 Int32 s_save_nblockPP = s->save_nblock+1;
650 unsigned int total_out_lo32_old;
654 /* try to finish existing run */
655 if (c_state_out_len > 0) {
657 if (cs_avail_out == 0) goto return_notr;
658 if (c_state_out_len == 1) break;
659 *( (UChar*)(cs_next_out) ) = c_state_out_ch;
660 BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch );
665 s_state_out_len_eq_one:
667 if (cs_avail_out == 0) {
668 c_state_out_len = 1; goto return_notr;
670 *( (UChar*)(cs_next_out) ) = c_state_out_ch;
671 BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch );
676 /* can a new run be started? */
677 if (c_nblock_used == s_save_nblockPP) {
678 c_state_out_len = 0; goto return_notr;
680 c_state_out_ch = c_k0;
681 BZ_GET_FAST_C(k1); c_nblock_used++;
683 c_k0 = k1; goto s_state_out_len_eq_one;
685 if (c_nblock_used == s_save_nblockPP)
686 goto s_state_out_len_eq_one;
689 BZ_GET_FAST_C(k1); c_nblock_used++;
690 if (c_nblock_used == s_save_nblockPP) continue;
691 if (k1 != c_k0) { c_k0 = k1; continue; };
694 BZ_GET_FAST_C(k1); c_nblock_used++;
695 if (c_nblock_used == s_save_nblockPP) continue;
696 if (k1 != c_k0) { c_k0 = k1; continue; };
698 BZ_GET_FAST_C(k1); c_nblock_used++;
699 c_state_out_len = ((Int32)k1) + 4;
700 BZ_GET_FAST_C(c_k0); c_nblock_used++;
704 total_out_lo32_old = s->strm->total_out_lo32;
705 s->strm->total_out_lo32 += (avail_out_INIT - cs_avail_out);
706 if (s->strm->total_out_lo32 < total_out_lo32_old)
707 s->strm->total_out_hi32++;
710 s->calculatedBlockCRC = c_calculatedBlockCRC;
711 s->state_out_ch = c_state_out_ch;
712 s->state_out_len = c_state_out_len;
713 s->nblock_used = c_nblock_used;
717 s->strm->next_out = cs_next_out;
718 s->strm->avail_out = cs_avail_out;
724 /*---------------------------------------------------*/
725 __inline__ Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab )
731 mid = (nb + na) >> 1;
732 if (indx >= cftab[mid]) nb = mid; else na = mid;
734 while (na - nb != 1);
739 /*---------------------------------------------------*/
741 void unRLE_obuf_to_output_SMALL ( DState* s )
745 if (s->blockRandomised) {
748 /* try to finish existing run */
750 if (s->strm->avail_out == 0) return;
751 if (s->state_out_len == 0) break;
752 *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
753 BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
756 s->strm->avail_out--;
757 s->strm->total_out_lo32++;
758 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
761 /* can a new run be started? */
762 if (s->nblock_used == s->save_nblock+1) return;
765 s->state_out_len = 1;
766 s->state_out_ch = s->k0;
767 BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
768 k1 ^= BZ_RAND_MASK; s->nblock_used++;
769 if (s->nblock_used == s->save_nblock+1) continue;
770 if (k1 != s->k0) { s->k0 = k1; continue; };
772 s->state_out_len = 2;
773 BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
774 k1 ^= BZ_RAND_MASK; s->nblock_used++;
775 if (s->nblock_used == s->save_nblock+1) continue;
776 if (k1 != s->k0) { s->k0 = k1; continue; };
778 s->state_out_len = 3;
779 BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
780 k1 ^= BZ_RAND_MASK; s->nblock_used++;
781 if (s->nblock_used == s->save_nblock+1) continue;
782 if (k1 != s->k0) { s->k0 = k1; continue; };
784 BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
785 k1 ^= BZ_RAND_MASK; s->nblock_used++;
786 s->state_out_len = ((Int32)k1) + 4;
787 BZ_GET_SMALL(s->k0); BZ_RAND_UPD_MASK;
788 s->k0 ^= BZ_RAND_MASK; s->nblock_used++;
794 /* try to finish existing run */
796 if (s->strm->avail_out == 0) return;
797 if (s->state_out_len == 0) break;
798 *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
799 BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
802 s->strm->avail_out--;
803 s->strm->total_out_lo32++;
804 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
807 /* can a new run be started? */
808 if (s->nblock_used == s->save_nblock+1) return;
810 s->state_out_len = 1;
811 s->state_out_ch = s->k0;
812 BZ_GET_SMALL(k1); s->nblock_used++;
813 if (s->nblock_used == s->save_nblock+1) continue;
814 if (k1 != s->k0) { s->k0 = k1; continue; };
816 s->state_out_len = 2;
817 BZ_GET_SMALL(k1); s->nblock_used++;
818 if (s->nblock_used == s->save_nblock+1) continue;
819 if (k1 != s->k0) { s->k0 = k1; continue; };
821 s->state_out_len = 3;
822 BZ_GET_SMALL(k1); s->nblock_used++;
823 if (s->nblock_used == s->save_nblock+1) continue;
824 if (k1 != s->k0) { s->k0 = k1; continue; };
826 BZ_GET_SMALL(k1); s->nblock_used++;
827 s->state_out_len = ((Int32)k1) + 4;
828 BZ_GET_SMALL(s->k0); s->nblock_used++;
835 /*---------------------------------------------------*/
836 int BZ_API(BZ2_bzDecompress) ( bz_stream *strm )
839 if (strm == NULL) return BZ_PARAM_ERROR;
841 if (s == NULL) return BZ_PARAM_ERROR;
842 if (s->strm != strm) return BZ_PARAM_ERROR;
845 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
848 if (s->state == BZ_X_IDLE) return BZ_SEQUENCE_ERROR;
849 if (s->state == BZ_X_OUTPUT) {
850 if (s->smallDecompress)
851 unRLE_obuf_to_output_SMALL ( s ); else
852 unRLE_obuf_to_output_FAST ( s );
853 if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) {
854 BZ_FINALISE_CRC ( s->calculatedBlockCRC );
855 if (s->verbosity >= 3)
856 VPrintf2 ( " {0x%x, 0x%x}", s->storedBlockCRC,
857 s->calculatedBlockCRC );
858 if (s->verbosity >= 2) VPrintf0 ( "]" );
859 if (s->calculatedBlockCRC != s->storedBlockCRC)
860 return BZ_DATA_ERROR;
861 s->calculatedCombinedCRC
862 = (s->calculatedCombinedCRC << 1) |
863 (s->calculatedCombinedCRC >> 31);
864 s->calculatedCombinedCRC ^= s->calculatedBlockCRC;
865 s->state = BZ_X_BLKHDR_1;
870 if (s->state >= BZ_X_MAGIC_1) {
871 Int32 r = BZ2_decompress ( s );
872 if (r == BZ_STREAM_END) {
873 if (s->verbosity >= 3)
874 VPrintf2 ( "\n combined CRCs: stored = 0x%x, computed = 0x%x",
875 s->storedCombinedCRC, s->calculatedCombinedCRC );
876 if (s->calculatedCombinedCRC != s->storedCombinedCRC)
877 return BZ_DATA_ERROR;
880 if (s->state != BZ_X_OUTPUT) return r;
886 return 0; /*NOTREACHED*/
890 /*---------------------------------------------------*/
891 int BZ_API(BZ2_bzDecompressEnd) ( bz_stream *strm )
894 if (strm == NULL) return BZ_PARAM_ERROR;
896 if (s == NULL) return BZ_PARAM_ERROR;
897 if (s->strm != strm) return BZ_PARAM_ERROR;
899 if (s->tt != NULL) BZFREE(s->tt);
900 if (s->ll16 != NULL) BZFREE(s->ll16);
901 if (s->ll4 != NULL) BZFREE(s->ll4);
911 /*---------------------------------------------------*/
912 /*--- File I/O stuff ---*/
913 /*---------------------------------------------------*/
915 #define BZ_SETERR(eee) \
917 if (bzerror != NULL) *bzerror = eee; \
918 if (bzf != NULL) bzf->lastErr = eee; \
924 Char buf[BZ_MAX_UNUSED];
934 /*---------------------------------------------*/
935 static Bool myfeof ( FILE* f )
937 Int32 c = fgetc ( f );
938 if (c == EOF) return True;
944 /*---------------------------------------------------*/
945 BZFILE* BZ_API(BZ2_bzWriteOpen)
958 (blockSize100k < 1 || blockSize100k > 9) ||
959 (workFactor < 0 || workFactor > 250) ||
960 (verbosity < 0 || verbosity > 4))
961 { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
964 { BZ_SETERR(BZ_IO_ERROR); return NULL; };
966 bzf = malloc ( sizeof(bzFile) );
968 { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
971 bzf->initialisedOk = False;
975 bzf->strm.bzalloc = NULL;
976 bzf->strm.bzfree = NULL;
977 bzf->strm.opaque = NULL;
979 if (workFactor == 0) workFactor = 30;
980 ret = BZ2_bzCompressInit ( &(bzf->strm), blockSize100k,
981 verbosity, workFactor );
983 { BZ_SETERR(ret); free(bzf); return NULL; };
985 bzf->strm.avail_in = 0;
986 bzf->initialisedOk = True;
991 /*---------------------------------------------------*/
992 void BZ_API(BZ2_bzWrite)
999 bzFile* bzf = (bzFile*)b;
1002 if (bzf == NULL || buf == NULL || len < 0)
1003 { BZ_SETERR(BZ_PARAM_ERROR); return; };
1004 if (!(bzf->writing))
1005 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
1006 if (ferror(bzf->handle))
1007 { BZ_SETERR(BZ_IO_ERROR); return; };
1010 { BZ_SETERR(BZ_OK); return; };
1012 bzf->strm.avail_in = len;
1013 bzf->strm.next_in = buf;
1016 bzf->strm.avail_out = BZ_MAX_UNUSED;
1017 bzf->strm.next_out = bzf->buf;
1018 ret = BZ2_bzCompress ( &(bzf->strm), BZ_RUN );
1019 if (ret != BZ_RUN_OK)
1020 { BZ_SETERR(ret); return; };
1022 if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
1023 n = BZ_MAX_UNUSED - bzf->strm.avail_out;
1024 n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
1026 if (n != n2 || ferror(bzf->handle))
1027 { BZ_SETERR(BZ_IO_ERROR); return; };
1030 if (bzf->strm.avail_in == 0)
1031 { BZ_SETERR(BZ_OK); return; };
1036 /*---------------------------------------------------*/
1037 void BZ_API(BZ2_bzWriteClose)
1041 unsigned int* nbytes_in,
1042 unsigned int* nbytes_out )
1044 BZ2_bzWriteClose64 ( bzerror, b, abandon,
1045 nbytes_in, NULL, nbytes_out, NULL );
1049 void BZ_API(BZ2_bzWriteClose64)
1053 unsigned int* nbytes_in_lo32,
1054 unsigned int* nbytes_in_hi32,
1055 unsigned int* nbytes_out_lo32,
1056 unsigned int* nbytes_out_hi32 )
1059 bzFile* bzf = (bzFile*)b;
1062 { BZ_SETERR(BZ_OK); return; };
1063 if (!(bzf->writing))
1064 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
1065 if (ferror(bzf->handle))
1066 { BZ_SETERR(BZ_IO_ERROR); return; };
1068 if (nbytes_in_lo32 != NULL) *nbytes_in_lo32 = 0;
1069 if (nbytes_in_hi32 != NULL) *nbytes_in_hi32 = 0;
1070 if (nbytes_out_lo32 != NULL) *nbytes_out_lo32 = 0;
1071 if (nbytes_out_hi32 != NULL) *nbytes_out_hi32 = 0;
1073 if ((!abandon) && bzf->lastErr == BZ_OK) {
1075 bzf->strm.avail_out = BZ_MAX_UNUSED;
1076 bzf->strm.next_out = bzf->buf;
1077 ret = BZ2_bzCompress ( &(bzf->strm), BZ_FINISH );
1078 if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END)
1079 { BZ_SETERR(ret); return; };
1081 if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
1082 n = BZ_MAX_UNUSED - bzf->strm.avail_out;
1083 n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
1085 if (n != n2 || ferror(bzf->handle))
1086 { BZ_SETERR(BZ_IO_ERROR); return; };
1089 if (ret == BZ_STREAM_END) break;
1093 if ( !abandon && !ferror ( bzf->handle ) ) {
1094 fflush ( bzf->handle );
1095 if (ferror(bzf->handle))
1096 { BZ_SETERR(BZ_IO_ERROR); return; };
1099 if (nbytes_in_lo32 != NULL)
1100 *nbytes_in_lo32 = bzf->strm.total_in_lo32;
1101 if (nbytes_in_hi32 != NULL)
1102 *nbytes_in_hi32 = bzf->strm.total_in_hi32;
1103 if (nbytes_out_lo32 != NULL)
1104 *nbytes_out_lo32 = bzf->strm.total_out_lo32;
1105 if (nbytes_out_hi32 != NULL)
1106 *nbytes_out_hi32 = bzf->strm.total_out_hi32;
1109 BZ2_bzCompressEnd ( &(bzf->strm) );
1114 /*---------------------------------------------------*/
1115 BZFILE* BZ_API(BZ2_bzReadOpen)
1129 (small != 0 && small != 1) ||
1130 (verbosity < 0 || verbosity > 4) ||
1131 (unused == NULL && nUnused != 0) ||
1132 (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)))
1133 { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
1136 { BZ_SETERR(BZ_IO_ERROR); return NULL; };
1138 bzf = malloc ( sizeof(bzFile) );
1140 { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
1144 bzf->initialisedOk = False;
1147 bzf->writing = False;
1148 bzf->strm.bzalloc = NULL;
1149 bzf->strm.bzfree = NULL;
1150 bzf->strm.opaque = NULL;
1152 while (nUnused > 0) {
1153 bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
1154 unused = ((void*)( 1 + ((UChar*)(unused)) ));
1158 ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
1160 { BZ_SETERR(ret); free(bzf); return NULL; };
1162 bzf->strm.avail_in = bzf->bufN;
1163 bzf->strm.next_in = bzf->buf;
1165 bzf->initialisedOk = True;
1170 /*---------------------------------------------------*/
1171 void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b )
1173 bzFile* bzf = (bzFile*)b;
1177 { BZ_SETERR(BZ_OK); return; };
1180 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
1182 if (bzf->initialisedOk)
1183 (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
1188 /*---------------------------------------------------*/
1189 int BZ_API(BZ2_bzRead)
1196 bzFile* bzf = (bzFile*)b;
1200 if (bzf == NULL || buf == NULL || len < 0)
1201 { BZ_SETERR(BZ_PARAM_ERROR); return 0; };
1204 { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
1207 { BZ_SETERR(BZ_OK); return 0; };
1209 bzf->strm.avail_out = len;
1210 bzf->strm.next_out = buf;
1214 if (ferror(bzf->handle))
1215 { BZ_SETERR(BZ_IO_ERROR); return 0; };
1217 if (bzf->strm.avail_in == 0 && !myfeof(bzf->handle)) {
1218 n = fread ( bzf->buf, sizeof(UChar),
1219 BZ_MAX_UNUSED, bzf->handle );
1220 if (ferror(bzf->handle))
1221 { BZ_SETERR(BZ_IO_ERROR); return 0; };
1223 bzf->strm.avail_in = bzf->bufN;
1224 bzf->strm.next_in = bzf->buf;
1227 ret = BZ2_bzDecompress ( &(bzf->strm) );
1229 if (ret != BZ_OK && ret != BZ_STREAM_END)
1230 { BZ_SETERR(ret); return 0; };
1232 if (ret == BZ_OK && myfeof(bzf->handle) &&
1233 bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0)
1234 { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; };
1236 if (ret == BZ_STREAM_END)
1237 { BZ_SETERR(BZ_STREAM_END);
1238 return len - bzf->strm.avail_out; };
1239 if (bzf->strm.avail_out == 0)
1240 { BZ_SETERR(BZ_OK); return len; };
1244 return 0; /*not reached*/
1248 /*---------------------------------------------------*/
1249 void BZ_API(BZ2_bzReadGetUnused)
1255 bzFile* bzf = (bzFile*)b;
1257 { BZ_SETERR(BZ_PARAM_ERROR); return; };
1258 if (bzf->lastErr != BZ_STREAM_END)
1259 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
1260 if (unused == NULL || nUnused == NULL)
1261 { BZ_SETERR(BZ_PARAM_ERROR); return; };
1264 *nUnused = bzf->strm.avail_in;
1265 *unused = bzf->strm.next_in;
1270 /*---------------------------------------------------*/
1271 /*--- Misc convenience stuff ---*/
1272 /*---------------------------------------------------*/
1273 #ifndef BZ_NO_COMPRESS
1274 /*---------------------------------------------------*/
1275 int BZ_API(BZ2_bzBuffToBuffCompress)
1277 unsigned int* destLen,
1279 unsigned int sourceLen,
1287 if (dest == NULL || destLen == NULL ||
1289 blockSize100k < 1 || blockSize100k > 9 ||
1290 verbosity < 0 || verbosity > 4 ||
1291 workFactor < 0 || workFactor > 250)
1292 return BZ_PARAM_ERROR;
1294 if (workFactor == 0) workFactor = 30;
1295 strm.bzalloc = NULL;
1298 ret = BZ2_bzCompressInit ( &strm, blockSize100k,
1299 verbosity, workFactor );
1300 if (ret != BZ_OK) return ret;
1302 strm.next_in = source;
1303 strm.next_out = dest;
1304 strm.avail_in = sourceLen;
1305 strm.avail_out = *destLen;
1307 ret = BZ2_bzCompress ( &strm, BZ_FINISH );
1308 if (ret == BZ_FINISH_OK) goto output_overflow;
1309 if (ret != BZ_STREAM_END) goto errhandler;
1311 /* normal termination */
1312 *destLen -= strm.avail_out;
1313 BZ2_bzCompressEnd ( &strm );
1317 BZ2_bzCompressEnd ( &strm );
1318 return BZ_OUTBUFF_FULL;
1321 BZ2_bzCompressEnd ( &strm );
1324 #endif /* BZ_NO_COMPRESS */
1326 /*---------------------------------------------------*/
1327 int BZ_API(BZ2_bzBuffToBuffDecompress)
1329 unsigned int* destLen,
1331 unsigned int sourceLen,
1338 if (destLen == NULL || source == NULL)
1339 return BZ_PARAM_ERROR;
1341 strm.bzalloc = NULL;
1344 ret = BZ2_bzDecompressInit ( &strm, verbosity, small );
1345 if (ret != BZ_OK) return ret;
1347 strm.next_in = source;
1348 strm.next_out = dest;
1349 strm.avail_in = sourceLen;
1350 strm.avail_out = *destLen;
1352 ret = BZ2_bzDecompress ( &strm );
1353 *destLen -= strm.avail_out;
1354 if (ret == BZ_OK) goto output_overflow_or_eof;
1355 if (ret != BZ_STREAM_END) goto errhandler;
1357 /* normal termination */
1358 BZ2_bzDecompressEnd ( &strm );
1361 output_overflow_or_eof:
1362 if (strm.avail_out > 0) {
1363 BZ2_bzDecompressEnd ( &strm );
1364 return BZ_UNEXPECTED_EOF;
1366 BZ2_bzDecompressEnd ( &strm );
1367 return BZ_OUTBUFF_FULL;
1371 BZ2_bzDecompressEnd ( &strm );
1376 /*---------------------------------------------------*/
1378 Code contributed by Yoshioka Tsuneo
1379 (QWF00133@niftyserve.or.jp/tsuneo-y@is.aist-nara.ac.jp),
1380 to support better zlib compatibility.
1381 This code is not _officially_ part of libbzip2 (yet);
1382 I haven't tested it, documented it, or considered the
1383 threading-safeness of it.
1384 If this code breaks, please contact both Yoshioka and me.
1386 /*---------------------------------------------------*/
1388 /*---------------------------------------------------*/
1390 return version like "0.9.0c".
1392 const char * BZ_API(BZ2_bzlibVersion)(void)
1399 /*---------------------------------------------------*/
1401 #if defined(_WIN32) || defined(OS2) || defined(MSDOS)
1404 # define SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
1406 # define SET_BINARY_MODE(file)
1409 BZFILE * bzopen_or_bzdopen
1410 ( const char *path, /* no use when bzdopen */
1411 int fd, /* no use when bzdopen */
1413 int open_mode) /* bzopen: 0, bzdopen:1 */
1416 char unused[BZ_MAX_UNUSED];
1417 int blockSize100k = 9;
1419 char mode2[10] = "";
1421 BZFILE *bzfp = NULL;
1423 int workFactor = 30;
1427 if (mode == NULL) return NULL;
1435 smallMode = 1; break;
1437 if (isdigit((int)(*mode))) {
1438 blockSize100k = *mode-BZ_HDR_0;
1443 strcat(mode2, writing ? "w" : "r" );
1444 strcat(mode2,"b"); /* binary mode */
1447 if (path==NULL || strcmp(path,"")==0) {
1448 fp = (writing ? stdout : stdin);
1449 SET_BINARY_MODE(fp);
1451 fp = fopen(path,mode2);
1454 #ifdef BZ_STRICT_ANSI
1457 fp = fdopen(fd,mode2);
1460 if (fp == NULL) return NULL;
1463 /* Guard against total chaos and anarchy -- JRS */
1464 if (blockSize100k < 1) blockSize100k = 1;
1465 if (blockSize100k > 9) blockSize100k = 9;
1466 bzfp = BZ2_bzWriteOpen(&bzerr,fp,blockSize100k,
1467 verbosity,workFactor);
1469 bzfp = BZ2_bzReadOpen(&bzerr,fp,verbosity,smallMode,
1473 if (fp != stdin && fp != stdout) fclose(fp);
1480 /*---------------------------------------------------*/
1482 open file for read or write.
1483 ex) bzopen("file","w9")
1484 case path="" or NULL => use stdin or stdout.
1486 BZFILE * BZ_API(BZ2_bzopen)
1490 return bzopen_or_bzdopen(path,-1,mode,/*bzopen*/0);
1494 /*---------------------------------------------------*/
1495 BZFILE * BZ_API(BZ2_bzdopen)
1499 return bzopen_or_bzdopen(NULL,fd,mode,/*bzdopen*/1);
1503 /*---------------------------------------------------*/
1504 int BZ_API(BZ2_bzread) (BZFILE* b, void* buf, int len )
1507 if (((bzFile*)b)->lastErr == BZ_STREAM_END) return 0;
1508 nread = BZ2_bzRead(&bzerr,b,buf,len);
1509 if (bzerr == BZ_OK || bzerr == BZ_STREAM_END) {
1517 /*---------------------------------------------------*/
1518 int BZ_API(BZ2_bzwrite) (BZFILE* b, void* buf, int len )
1522 BZ2_bzWrite(&bzerr,b,buf,len);
1531 /*---------------------------------------------------*/
1532 int BZ_API(BZ2_bzflush) (BZFILE *b)
1534 /* do nothing now... */
1539 /*---------------------------------------------------*/
1540 void BZ_API(BZ2_bzclose) (BZFILE* b)
1543 FILE *fp = ((bzFile *)b)->handle;
1545 if (b==NULL) {return;}
1546 if(((bzFile*)b)->writing){
1547 BZ2_bzWriteClose(&bzerr,b,0,NULL,NULL);
1549 BZ2_bzWriteClose(NULL,b,1,NULL,NULL);
1552 BZ2_bzReadClose(&bzerr,b);
1554 if(fp!=stdin && fp!=stdout){
1560 /*---------------------------------------------------*/
1562 return last error code
1564 static char *bzerrorstrings[] = {
1575 ,"???" /* for future */
1576 ,"???" /* for future */
1577 ,"???" /* for future */
1578 ,"???" /* for future */
1579 ,"???" /* for future */
1580 ,"???" /* for future */
1584 const char * BZ_API(BZ2_bzerror) (BZFILE *b, int *errnum)
1586 int err = ((bzFile *)b)->lastErr;
1590 return bzerrorstrings[err*-1];
1594 void bz_internal_error(int errcode)
1596 printf ("BZIP2 internal error %d\n", errcode);
1599 /*-------------------------------------------------------------*/
1600 /*--- end bzlib.c ---*/
1601 /*-------------------------------------------------------------*/