michael@0: michael@0: /*-------------------------------------------------------------*/ michael@0: /*--- Library top-level functions. ---*/ michael@0: /*--- bzlib.c ---*/ michael@0: /*-------------------------------------------------------------*/ michael@0: michael@0: /* ------------------------------------------------------------------ michael@0: This file is part of bzip2/libbzip2, a program and library for michael@0: lossless, block-sorting data compression. michael@0: michael@0: bzip2/libbzip2 version 1.0.4 of 20 December 2006 michael@0: Copyright (C) 1996-2006 Julian Seward michael@0: michael@0: Please read the WARNING, DISCLAIMER and PATENTS sections in the michael@0: README file. michael@0: michael@0: This program is released under the terms of the license contained michael@0: in the file LICENSE. michael@0: ------------------------------------------------------------------ */ michael@0: michael@0: /* CHANGES michael@0: 0.9.0 -- original version. michael@0: 0.9.0a/b -- no changes in this file. michael@0: 0.9.0c -- made zero-length BZ_FLUSH work correctly in bzCompress(). michael@0: fixed bzWrite/bzRead to ignore zero-length requests. michael@0: fixed bzread to correctly handle read requests after EOF. michael@0: wrong parameter order in call to bzDecompressInit in michael@0: bzBuffToBuffDecompress. Fixed. michael@0: */ michael@0: michael@0: #include "bzlib_private.h" michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: /*--- Compression stuff ---*/ michael@0: /*---------------------------------------------------*/ michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: #ifndef BZ_NO_STDIO michael@0: void BZ2_bz__AssertH__fail ( int errcode ) michael@0: { michael@0: fprintf(stderr, michael@0: "\n\nbzip2/libbzip2: internal error number %d.\n" michael@0: "This is a bug in bzip2/libbzip2, %s.\n" michael@0: "Please report it to me at: jseward@bzip.org. If this happened\n" michael@0: "when you were using some program which uses libbzip2 as a\n" michael@0: "component, you should also report this bug to the author(s)\n" michael@0: "of that program. Please make an effort to report this bug;\n" michael@0: "timely and accurate bug reports eventually lead to higher\n" michael@0: "quality software. Thanks. Julian Seward, 15 February 2005.\n\n", michael@0: errcode, michael@0: BZ2_bzlibVersion() michael@0: ); michael@0: michael@0: if (errcode == 1007) { michael@0: fprintf(stderr, michael@0: "\n*** A special note about internal error number 1007 ***\n" michael@0: "\n" michael@0: "Experience suggests that a common cause of i.e. 1007\n" michael@0: "is unreliable memory or other hardware. The 1007 assertion\n" michael@0: "just happens to cross-check the results of huge numbers of\n" michael@0: "memory reads/writes, and so acts (unintendedly) as a stress\n" michael@0: "test of your memory system.\n" michael@0: "\n" michael@0: "I suggest the following: try compressing the file again,\n" michael@0: "possibly monitoring progress in detail with the -vv flag.\n" michael@0: "\n" michael@0: "* If the error cannot be reproduced, and/or happens at different\n" michael@0: " points in compression, you may have a flaky memory system.\n" michael@0: " Try a memory-test program. I have used Memtest86\n" michael@0: " (www.memtest86.com). At the time of writing it is free (GPLd).\n" michael@0: " Memtest86 tests memory much more thorougly than your BIOSs\n" michael@0: " power-on test, and may find failures that the BIOS doesn't.\n" michael@0: "\n" michael@0: "* If the error can be repeatably reproduced, this is a bug in\n" michael@0: " bzip2, and I would very much like to hear about it. Please\n" michael@0: " let me know, and, ideally, save a copy of the file causing the\n" michael@0: " problem -- without which I will be unable to investigate it.\n" michael@0: "\n" michael@0: ); michael@0: } michael@0: michael@0: exit(3); michael@0: } michael@0: #endif michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: static michael@0: int bz_config_ok ( void ) michael@0: { michael@0: if (sizeof(int) != 4) return 0; michael@0: if (sizeof(short) != 2) return 0; michael@0: if (sizeof(char) != 1) return 0; michael@0: return 1; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: static michael@0: void* default_bzalloc ( void* opaque, Int32 items, Int32 size ) michael@0: { michael@0: void* v = malloc ( items * size ); michael@0: return v; michael@0: } michael@0: michael@0: static michael@0: void default_bzfree ( void* opaque, void* addr ) michael@0: { michael@0: if (addr != NULL) free ( addr ); michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: static michael@0: void prepare_new_block ( EState* s ) michael@0: { michael@0: Int32 i; michael@0: s->nblock = 0; michael@0: s->numZ = 0; michael@0: s->state_out_pos = 0; michael@0: BZ_INITIALISE_CRC ( s->blockCRC ); michael@0: for (i = 0; i < 256; i++) s->inUse[i] = False; michael@0: s->blockNo++; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: static michael@0: void init_RL ( EState* s ) michael@0: { michael@0: s->state_in_ch = 256; michael@0: s->state_in_len = 0; michael@0: } michael@0: michael@0: michael@0: static michael@0: Bool isempty_RL ( EState* s ) michael@0: { michael@0: if (s->state_in_ch < 256 && s->state_in_len > 0) michael@0: return False; else michael@0: return True; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: int BZ_API(BZ2_bzCompressInit) michael@0: ( bz_stream* strm, michael@0: int blockSize100k, michael@0: int verbosity, michael@0: int workFactor ) michael@0: { michael@0: Int32 n; michael@0: EState* s; michael@0: michael@0: if (!bz_config_ok()) return BZ_CONFIG_ERROR; michael@0: michael@0: if (strm == NULL || michael@0: blockSize100k < 1 || blockSize100k > 9 || michael@0: workFactor < 0 || workFactor > 250) michael@0: return BZ_PARAM_ERROR; michael@0: michael@0: if (workFactor == 0) workFactor = 30; michael@0: if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc; michael@0: if (strm->bzfree == NULL) strm->bzfree = default_bzfree; michael@0: michael@0: s = BZALLOC( sizeof(EState) ); michael@0: if (s == NULL) return BZ_MEM_ERROR; michael@0: s->strm = strm; michael@0: michael@0: s->arr1 = NULL; michael@0: s->arr2 = NULL; michael@0: s->ftab = NULL; michael@0: michael@0: n = 100000 * blockSize100k; michael@0: s->arr1 = BZALLOC( n * sizeof(UInt32) ); michael@0: s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) ); michael@0: s->ftab = BZALLOC( 65537 * sizeof(UInt32) ); michael@0: michael@0: if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) { michael@0: if (s->arr1 != NULL) BZFREE(s->arr1); michael@0: if (s->arr2 != NULL) BZFREE(s->arr2); michael@0: if (s->ftab != NULL) BZFREE(s->ftab); michael@0: if (s != NULL) BZFREE(s); michael@0: return BZ_MEM_ERROR; michael@0: } michael@0: michael@0: s->blockNo = 0; michael@0: s->state = BZ_S_INPUT; michael@0: s->mode = BZ_M_RUNNING; michael@0: s->combinedCRC = 0; michael@0: s->blockSize100k = blockSize100k; michael@0: s->nblockMAX = 100000 * blockSize100k - 19; michael@0: s->verbosity = verbosity; michael@0: s->workFactor = workFactor; michael@0: michael@0: s->block = (UChar*)s->arr2; michael@0: s->mtfv = (UInt16*)s->arr1; michael@0: s->zbits = NULL; michael@0: s->ptr = (UInt32*)s->arr1; michael@0: michael@0: strm->state = s; michael@0: strm->total_in_lo32 = 0; michael@0: strm->total_in_hi32 = 0; michael@0: strm->total_out_lo32 = 0; michael@0: strm->total_out_hi32 = 0; michael@0: init_RL ( s ); michael@0: prepare_new_block ( s ); michael@0: return BZ_OK; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: static michael@0: void add_pair_to_block ( EState* s ) michael@0: { michael@0: Int32 i; michael@0: UChar ch = (UChar)(s->state_in_ch); michael@0: for (i = 0; i < s->state_in_len; i++) { michael@0: BZ_UPDATE_CRC( s->blockCRC, ch ); michael@0: } michael@0: s->inUse[s->state_in_ch] = True; michael@0: switch (s->state_in_len) { michael@0: case 1: michael@0: s->block[s->nblock] = (UChar)ch; s->nblock++; michael@0: break; michael@0: case 2: michael@0: s->block[s->nblock] = (UChar)ch; s->nblock++; michael@0: s->block[s->nblock] = (UChar)ch; s->nblock++; michael@0: break; michael@0: case 3: michael@0: s->block[s->nblock] = (UChar)ch; s->nblock++; michael@0: s->block[s->nblock] = (UChar)ch; s->nblock++; michael@0: s->block[s->nblock] = (UChar)ch; s->nblock++; michael@0: break; michael@0: default: michael@0: s->inUse[s->state_in_len-4] = True; michael@0: s->block[s->nblock] = (UChar)ch; s->nblock++; michael@0: s->block[s->nblock] = (UChar)ch; s->nblock++; michael@0: s->block[s->nblock] = (UChar)ch; s->nblock++; michael@0: s->block[s->nblock] = (UChar)ch; s->nblock++; michael@0: s->block[s->nblock] = ((UChar)(s->state_in_len-4)); michael@0: s->nblock++; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: static michael@0: void flush_RL ( EState* s ) michael@0: { michael@0: if (s->state_in_ch < 256) add_pair_to_block ( s ); michael@0: init_RL ( s ); michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: #define ADD_CHAR_TO_BLOCK(zs,zchh0) \ michael@0: { \ michael@0: UInt32 zchh = (UInt32)(zchh0); \ michael@0: /*-- fast track the common case --*/ \ michael@0: if (zchh != zs->state_in_ch && \ michael@0: zs->state_in_len == 1) { \ michael@0: UChar ch = (UChar)(zs->state_in_ch); \ michael@0: BZ_UPDATE_CRC( zs->blockCRC, ch ); \ michael@0: zs->inUse[zs->state_in_ch] = True; \ michael@0: zs->block[zs->nblock] = (UChar)ch; \ michael@0: zs->nblock++; \ michael@0: zs->state_in_ch = zchh; \ michael@0: } \ michael@0: else \ michael@0: /*-- general, uncommon cases --*/ \ michael@0: if (zchh != zs->state_in_ch || \ michael@0: zs->state_in_len == 255) { \ michael@0: if (zs->state_in_ch < 256) \ michael@0: add_pair_to_block ( zs ); \ michael@0: zs->state_in_ch = zchh; \ michael@0: zs->state_in_len = 1; \ michael@0: } else { \ michael@0: zs->state_in_len++; \ michael@0: } \ michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: static michael@0: Bool copy_input_until_stop ( EState* s ) michael@0: { michael@0: Bool progress_in = False; michael@0: michael@0: if (s->mode == BZ_M_RUNNING) { michael@0: michael@0: /*-- fast track the common case --*/ michael@0: while (True) { michael@0: /*-- block full? --*/ michael@0: if (s->nblock >= s->nblockMAX) break; michael@0: /*-- no input? --*/ michael@0: if (s->strm->avail_in == 0) break; michael@0: progress_in = True; michael@0: ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) ); michael@0: s->strm->next_in++; michael@0: s->strm->avail_in--; michael@0: s->strm->total_in_lo32++; michael@0: if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++; michael@0: } michael@0: michael@0: } else { michael@0: michael@0: /*-- general, uncommon case --*/ michael@0: while (True) { michael@0: /*-- block full? --*/ michael@0: if (s->nblock >= s->nblockMAX) break; michael@0: /*-- no input? --*/ michael@0: if (s->strm->avail_in == 0) break; michael@0: /*-- flush/finish end? --*/ michael@0: if (s->avail_in_expect == 0) break; michael@0: progress_in = True; michael@0: ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) ); michael@0: s->strm->next_in++; michael@0: s->strm->avail_in--; michael@0: s->strm->total_in_lo32++; michael@0: if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++; michael@0: s->avail_in_expect--; michael@0: } michael@0: } michael@0: return progress_in; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: static michael@0: Bool copy_output_until_stop ( EState* s ) michael@0: { michael@0: Bool progress_out = False; michael@0: michael@0: while (True) { michael@0: michael@0: /*-- no output space? --*/ michael@0: if (s->strm->avail_out == 0) break; michael@0: michael@0: /*-- block done? --*/ michael@0: if (s->state_out_pos >= s->numZ) break; michael@0: michael@0: progress_out = True; michael@0: *(s->strm->next_out) = s->zbits[s->state_out_pos]; michael@0: s->state_out_pos++; michael@0: s->strm->avail_out--; michael@0: s->strm->next_out++; michael@0: s->strm->total_out_lo32++; michael@0: if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; michael@0: } michael@0: michael@0: return progress_out; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: static michael@0: Bool handle_compress ( bz_stream* strm ) michael@0: { michael@0: Bool progress_in = False; michael@0: Bool progress_out = False; michael@0: EState* s = strm->state; michael@0: michael@0: while (True) { michael@0: michael@0: if (s->state == BZ_S_OUTPUT) { michael@0: progress_out |= copy_output_until_stop ( s ); michael@0: if (s->state_out_pos < s->numZ) break; michael@0: if (s->mode == BZ_M_FINISHING && michael@0: s->avail_in_expect == 0 && michael@0: isempty_RL(s)) break; michael@0: prepare_new_block ( s ); michael@0: s->state = BZ_S_INPUT; michael@0: if (s->mode == BZ_M_FLUSHING && michael@0: s->avail_in_expect == 0 && michael@0: isempty_RL(s)) break; michael@0: } michael@0: michael@0: if (s->state == BZ_S_INPUT) { michael@0: progress_in |= copy_input_until_stop ( s ); michael@0: if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) { michael@0: flush_RL ( s ); michael@0: BZ2_compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) ); michael@0: s->state = BZ_S_OUTPUT; michael@0: } michael@0: else michael@0: if (s->nblock >= s->nblockMAX) { michael@0: BZ2_compressBlock ( s, False ); michael@0: s->state = BZ_S_OUTPUT; michael@0: } michael@0: else michael@0: if (s->strm->avail_in == 0) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: } michael@0: michael@0: return progress_in || progress_out; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action ) michael@0: { michael@0: Bool progress; michael@0: EState* s; michael@0: if (strm == NULL) return BZ_PARAM_ERROR; michael@0: s = strm->state; michael@0: if (s == NULL) return BZ_PARAM_ERROR; michael@0: if (s->strm != strm) return BZ_PARAM_ERROR; michael@0: michael@0: preswitch: michael@0: switch (s->mode) { michael@0: michael@0: case BZ_M_IDLE: michael@0: return BZ_SEQUENCE_ERROR; michael@0: michael@0: case BZ_M_RUNNING: michael@0: if (action == BZ_RUN) { michael@0: progress = handle_compress ( strm ); michael@0: return progress ? BZ_RUN_OK : BZ_PARAM_ERROR; michael@0: } michael@0: else michael@0: if (action == BZ_FLUSH) { michael@0: s->avail_in_expect = strm->avail_in; michael@0: s->mode = BZ_M_FLUSHING; michael@0: goto preswitch; michael@0: } michael@0: else michael@0: if (action == BZ_FINISH) { michael@0: s->avail_in_expect = strm->avail_in; michael@0: s->mode = BZ_M_FINISHING; michael@0: goto preswitch; michael@0: } michael@0: else michael@0: return BZ_PARAM_ERROR; michael@0: michael@0: case BZ_M_FLUSHING: michael@0: if (action != BZ_FLUSH) return BZ_SEQUENCE_ERROR; michael@0: if (s->avail_in_expect != s->strm->avail_in) michael@0: return BZ_SEQUENCE_ERROR; michael@0: progress = handle_compress ( strm ); michael@0: if (s->avail_in_expect > 0 || !isempty_RL(s) || michael@0: s->state_out_pos < s->numZ) return BZ_FLUSH_OK; michael@0: s->mode = BZ_M_RUNNING; michael@0: return BZ_RUN_OK; michael@0: michael@0: case BZ_M_FINISHING: michael@0: if (action != BZ_FINISH) return BZ_SEQUENCE_ERROR; michael@0: if (s->avail_in_expect != s->strm->avail_in) michael@0: return BZ_SEQUENCE_ERROR; michael@0: progress = handle_compress ( strm ); michael@0: if (!progress) return BZ_SEQUENCE_ERROR; michael@0: if (s->avail_in_expect > 0 || !isempty_RL(s) || michael@0: s->state_out_pos < s->numZ) return BZ_FINISH_OK; michael@0: s->mode = BZ_M_IDLE; michael@0: return BZ_STREAM_END; michael@0: } michael@0: return BZ_OK; /*--not reached--*/ michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: int BZ_API(BZ2_bzCompressEnd) ( bz_stream *strm ) michael@0: { michael@0: EState* s; michael@0: if (strm == NULL) return BZ_PARAM_ERROR; michael@0: s = strm->state; michael@0: if (s == NULL) return BZ_PARAM_ERROR; michael@0: if (s->strm != strm) return BZ_PARAM_ERROR; michael@0: michael@0: if (s->arr1 != NULL) BZFREE(s->arr1); michael@0: if (s->arr2 != NULL) BZFREE(s->arr2); michael@0: if (s->ftab != NULL) BZFREE(s->ftab); michael@0: BZFREE(strm->state); michael@0: michael@0: strm->state = NULL; michael@0: michael@0: return BZ_OK; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: /*--- Decompression stuff ---*/ michael@0: /*---------------------------------------------------*/ michael@0: michael@0: /*---------------------------------------------------*/ michael@0: int BZ_API(BZ2_bzDecompressInit) michael@0: ( bz_stream* strm, michael@0: int verbosity, michael@0: int small ) michael@0: { michael@0: DState* s; michael@0: michael@0: if (!bz_config_ok()) return BZ_CONFIG_ERROR; michael@0: michael@0: if (strm == NULL) return BZ_PARAM_ERROR; michael@0: if (small != 0 && small != 1) return BZ_PARAM_ERROR; michael@0: if (verbosity < 0 || verbosity > 4) return BZ_PARAM_ERROR; michael@0: michael@0: if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc; michael@0: if (strm->bzfree == NULL) strm->bzfree = default_bzfree; michael@0: michael@0: s = BZALLOC( sizeof(DState) ); michael@0: if (s == NULL) return BZ_MEM_ERROR; michael@0: s->strm = strm; michael@0: strm->state = s; michael@0: s->state = BZ_X_MAGIC_1; michael@0: s->bsLive = 0; michael@0: s->bsBuff = 0; michael@0: s->calculatedCombinedCRC = 0; michael@0: strm->total_in_lo32 = 0; michael@0: strm->total_in_hi32 = 0; michael@0: strm->total_out_lo32 = 0; michael@0: strm->total_out_hi32 = 0; michael@0: s->smallDecompress = (Bool)small; michael@0: s->ll4 = NULL; michael@0: s->ll16 = NULL; michael@0: s->tt = NULL; michael@0: s->currBlockNo = 0; michael@0: s->verbosity = verbosity; michael@0: michael@0: return BZ_OK; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: /* Return True iff data corruption is discovered. michael@0: Returns False if there is no problem. michael@0: */ michael@0: static michael@0: Bool unRLE_obuf_to_output_FAST ( DState* s ) michael@0: { michael@0: UChar k1; michael@0: michael@0: if (s->blockRandomised) { michael@0: michael@0: while (True) { michael@0: /* try to finish existing run */ michael@0: while (True) { michael@0: if (s->strm->avail_out == 0) return False; michael@0: if (s->state_out_len == 0) break; michael@0: *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; michael@0: BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); michael@0: s->state_out_len--; michael@0: s->strm->next_out++; michael@0: s->strm->avail_out--; michael@0: s->strm->total_out_lo32++; michael@0: if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; michael@0: } michael@0: michael@0: /* can a new run be started? */ michael@0: if (s->nblock_used == s->save_nblock+1) return False; michael@0: michael@0: /* Only caused by corrupt data stream? */ michael@0: if (s->nblock_used > s->save_nblock+1) michael@0: return True; michael@0: michael@0: s->state_out_len = 1; michael@0: s->state_out_ch = s->k0; michael@0: BZ_GET_FAST(k1); BZ_RAND_UPD_MASK; michael@0: k1 ^= BZ_RAND_MASK; s->nblock_used++; michael@0: if (s->nblock_used == s->save_nblock+1) continue; michael@0: if (k1 != s->k0) { s->k0 = k1; continue; }; michael@0: michael@0: s->state_out_len = 2; michael@0: BZ_GET_FAST(k1); BZ_RAND_UPD_MASK; michael@0: k1 ^= BZ_RAND_MASK; s->nblock_used++; michael@0: if (s->nblock_used == s->save_nblock+1) continue; michael@0: if (k1 != s->k0) { s->k0 = k1; continue; }; michael@0: michael@0: s->state_out_len = 3; michael@0: BZ_GET_FAST(k1); BZ_RAND_UPD_MASK; michael@0: k1 ^= BZ_RAND_MASK; s->nblock_used++; michael@0: if (s->nblock_used == s->save_nblock+1) continue; michael@0: if (k1 != s->k0) { s->k0 = k1; continue; }; michael@0: michael@0: BZ_GET_FAST(k1); BZ_RAND_UPD_MASK; michael@0: k1 ^= BZ_RAND_MASK; s->nblock_used++; michael@0: s->state_out_len = ((Int32)k1) + 4; michael@0: BZ_GET_FAST(s->k0); BZ_RAND_UPD_MASK; michael@0: s->k0 ^= BZ_RAND_MASK; s->nblock_used++; michael@0: } michael@0: michael@0: } else { michael@0: michael@0: /* restore */ michael@0: UInt32 c_calculatedBlockCRC = s->calculatedBlockCRC; michael@0: UChar c_state_out_ch = s->state_out_ch; michael@0: Int32 c_state_out_len = s->state_out_len; michael@0: Int32 c_nblock_used = s->nblock_used; michael@0: Int32 c_k0 = s->k0; michael@0: UInt32* c_tt = s->tt; michael@0: UInt32 c_tPos = s->tPos; michael@0: char* cs_next_out = s->strm->next_out; michael@0: unsigned int cs_avail_out = s->strm->avail_out; michael@0: /* end restore */ michael@0: michael@0: UInt32 avail_out_INIT = cs_avail_out; michael@0: Int32 s_save_nblockPP = s->save_nblock+1; michael@0: unsigned int total_out_lo32_old; michael@0: michael@0: while (True) { michael@0: michael@0: /* try to finish existing run */ michael@0: if (c_state_out_len > 0) { michael@0: while (True) { michael@0: if (cs_avail_out == 0) goto return_notr; michael@0: if (c_state_out_len == 1) break; michael@0: *( (UChar*)(cs_next_out) ) = c_state_out_ch; michael@0: BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch ); michael@0: c_state_out_len--; michael@0: cs_next_out++; michael@0: cs_avail_out--; michael@0: } michael@0: s_state_out_len_eq_one: michael@0: { michael@0: if (cs_avail_out == 0) { michael@0: c_state_out_len = 1; goto return_notr; michael@0: }; michael@0: *( (UChar*)(cs_next_out) ) = c_state_out_ch; michael@0: BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch ); michael@0: cs_next_out++; michael@0: cs_avail_out--; michael@0: } michael@0: } michael@0: /* Only caused by corrupt data stream? */ michael@0: if (c_nblock_used > s_save_nblockPP) michael@0: return True; michael@0: michael@0: /* can a new run be started? */ michael@0: if (c_nblock_used == s_save_nblockPP) { michael@0: c_state_out_len = 0; goto return_notr; michael@0: }; michael@0: c_state_out_ch = c_k0; michael@0: BZ_GET_FAST_C(k1); c_nblock_used++; michael@0: if (k1 != c_k0) { michael@0: c_k0 = k1; goto s_state_out_len_eq_one; michael@0: }; michael@0: if (c_nblock_used == s_save_nblockPP) michael@0: goto s_state_out_len_eq_one; michael@0: michael@0: c_state_out_len = 2; michael@0: BZ_GET_FAST_C(k1); c_nblock_used++; michael@0: if (c_nblock_used == s_save_nblockPP) continue; michael@0: if (k1 != c_k0) { c_k0 = k1; continue; }; michael@0: michael@0: c_state_out_len = 3; michael@0: BZ_GET_FAST_C(k1); c_nblock_used++; michael@0: if (c_nblock_used == s_save_nblockPP) continue; michael@0: if (k1 != c_k0) { c_k0 = k1; continue; }; michael@0: michael@0: BZ_GET_FAST_C(k1); c_nblock_used++; michael@0: c_state_out_len = ((Int32)k1) + 4; michael@0: BZ_GET_FAST_C(c_k0); c_nblock_used++; michael@0: } michael@0: michael@0: return_notr: michael@0: total_out_lo32_old = s->strm->total_out_lo32; michael@0: s->strm->total_out_lo32 += (avail_out_INIT - cs_avail_out); michael@0: if (s->strm->total_out_lo32 < total_out_lo32_old) michael@0: s->strm->total_out_hi32++; michael@0: michael@0: /* save */ michael@0: s->calculatedBlockCRC = c_calculatedBlockCRC; michael@0: s->state_out_ch = c_state_out_ch; michael@0: s->state_out_len = c_state_out_len; michael@0: s->nblock_used = c_nblock_used; michael@0: s->k0 = c_k0; michael@0: s->tt = c_tt; michael@0: s->tPos = c_tPos; michael@0: s->strm->next_out = cs_next_out; michael@0: s->strm->avail_out = cs_avail_out; michael@0: /* end save */ michael@0: } michael@0: return False; michael@0: } michael@0: michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: __inline__ Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab ) michael@0: { michael@0: Int32 nb, na, mid; michael@0: nb = 0; michael@0: na = 256; michael@0: do { michael@0: mid = (nb + na) >> 1; michael@0: if (indx >= cftab[mid]) nb = mid; else na = mid; michael@0: } michael@0: while (na - nb != 1); michael@0: return nb; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: /* Return True iff data corruption is discovered. michael@0: Returns False if there is no problem. michael@0: */ michael@0: static michael@0: Bool unRLE_obuf_to_output_SMALL ( DState* s ) michael@0: { michael@0: UChar k1; michael@0: michael@0: if (s->blockRandomised) { michael@0: michael@0: while (True) { michael@0: /* try to finish existing run */ michael@0: while (True) { michael@0: if (s->strm->avail_out == 0) return False; michael@0: if (s->state_out_len == 0) break; michael@0: *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; michael@0: BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); michael@0: s->state_out_len--; michael@0: s->strm->next_out++; michael@0: s->strm->avail_out--; michael@0: s->strm->total_out_lo32++; michael@0: if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; michael@0: } michael@0: michael@0: /* can a new run be started? */ michael@0: if (s->nblock_used == s->save_nblock+1) return False; michael@0: michael@0: /* Only caused by corrupt data stream? */ michael@0: if (s->nblock_used > s->save_nblock+1) michael@0: return True; michael@0: michael@0: s->state_out_len = 1; michael@0: s->state_out_ch = s->k0; michael@0: BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK; michael@0: k1 ^= BZ_RAND_MASK; s->nblock_used++; michael@0: if (s->nblock_used == s->save_nblock+1) continue; michael@0: if (k1 != s->k0) { s->k0 = k1; continue; }; michael@0: michael@0: s->state_out_len = 2; michael@0: BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK; michael@0: k1 ^= BZ_RAND_MASK; s->nblock_used++; michael@0: if (s->nblock_used == s->save_nblock+1) continue; michael@0: if (k1 != s->k0) { s->k0 = k1; continue; }; michael@0: michael@0: s->state_out_len = 3; michael@0: BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK; michael@0: k1 ^= BZ_RAND_MASK; s->nblock_used++; michael@0: if (s->nblock_used == s->save_nblock+1) continue; michael@0: if (k1 != s->k0) { s->k0 = k1; continue; }; michael@0: michael@0: BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK; michael@0: k1 ^= BZ_RAND_MASK; s->nblock_used++; michael@0: s->state_out_len = ((Int32)k1) + 4; michael@0: BZ_GET_SMALL(s->k0); BZ_RAND_UPD_MASK; michael@0: s->k0 ^= BZ_RAND_MASK; s->nblock_used++; michael@0: } michael@0: michael@0: } else { michael@0: michael@0: while (True) { michael@0: /* try to finish existing run */ michael@0: while (True) { michael@0: if (s->strm->avail_out == 0) return False; michael@0: if (s->state_out_len == 0) break; michael@0: *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; michael@0: BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); michael@0: s->state_out_len--; michael@0: s->strm->next_out++; michael@0: s->strm->avail_out--; michael@0: s->strm->total_out_lo32++; michael@0: if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; michael@0: } michael@0: michael@0: /* can a new run be started? */ michael@0: if (s->nblock_used == s->save_nblock+1) return False; michael@0: michael@0: /* Only caused by corrupt data stream? */ michael@0: if (s->nblock_used > s->save_nblock+1) michael@0: return True; michael@0: michael@0: s->state_out_len = 1; michael@0: s->state_out_ch = s->k0; michael@0: BZ_GET_SMALL(k1); s->nblock_used++; michael@0: if (s->nblock_used == s->save_nblock+1) continue; michael@0: if (k1 != s->k0) { s->k0 = k1; continue; }; michael@0: michael@0: s->state_out_len = 2; michael@0: BZ_GET_SMALL(k1); s->nblock_used++; michael@0: if (s->nblock_used == s->save_nblock+1) continue; michael@0: if (k1 != s->k0) { s->k0 = k1; continue; }; michael@0: michael@0: s->state_out_len = 3; michael@0: BZ_GET_SMALL(k1); s->nblock_used++; michael@0: if (s->nblock_used == s->save_nblock+1) continue; michael@0: if (k1 != s->k0) { s->k0 = k1; continue; }; michael@0: michael@0: BZ_GET_SMALL(k1); s->nblock_used++; michael@0: s->state_out_len = ((Int32)k1) + 4; michael@0: BZ_GET_SMALL(s->k0); s->nblock_used++; michael@0: } michael@0: michael@0: } michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: int BZ_API(BZ2_bzDecompress) ( bz_stream *strm ) michael@0: { michael@0: Bool corrupt; michael@0: DState* s; michael@0: if (strm == NULL) return BZ_PARAM_ERROR; michael@0: s = strm->state; michael@0: if (s == NULL) return BZ_PARAM_ERROR; michael@0: if (s->strm != strm) return BZ_PARAM_ERROR; michael@0: michael@0: while (True) { michael@0: if (s->state == BZ_X_IDLE) return BZ_SEQUENCE_ERROR; michael@0: if (s->state == BZ_X_OUTPUT) { michael@0: if (s->smallDecompress) michael@0: corrupt = unRLE_obuf_to_output_SMALL ( s ); else michael@0: corrupt = unRLE_obuf_to_output_FAST ( s ); michael@0: if (corrupt) return BZ_DATA_ERROR; michael@0: if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) { michael@0: BZ_FINALISE_CRC ( s->calculatedBlockCRC ); michael@0: if (s->verbosity >= 3) michael@0: VPrintf2 ( " {0x%08x, 0x%08x}", s->storedBlockCRC, michael@0: s->calculatedBlockCRC ); michael@0: if (s->verbosity >= 2) VPrintf0 ( "]" ); michael@0: if (s->calculatedBlockCRC != s->storedBlockCRC) michael@0: return BZ_DATA_ERROR; michael@0: s->calculatedCombinedCRC michael@0: = (s->calculatedCombinedCRC << 1) | michael@0: (s->calculatedCombinedCRC >> 31); michael@0: s->calculatedCombinedCRC ^= s->calculatedBlockCRC; michael@0: s->state = BZ_X_BLKHDR_1; michael@0: } else { michael@0: return BZ_OK; michael@0: } michael@0: } michael@0: if (s->state >= BZ_X_MAGIC_1) { michael@0: Int32 r = BZ2_decompress ( s ); michael@0: if (r == BZ_STREAM_END) { michael@0: if (s->verbosity >= 3) michael@0: VPrintf2 ( "\n combined CRCs: stored = 0x%08x, computed = 0x%08x", michael@0: s->storedCombinedCRC, s->calculatedCombinedCRC ); michael@0: if (s->calculatedCombinedCRC != s->storedCombinedCRC) michael@0: return BZ_DATA_ERROR; michael@0: return r; michael@0: } michael@0: if (s->state != BZ_X_OUTPUT) return r; michael@0: } michael@0: } michael@0: michael@0: AssertH ( 0, 6001 ); michael@0: michael@0: return 0; /*NOTREACHED*/ michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: int BZ_API(BZ2_bzDecompressEnd) ( bz_stream *strm ) michael@0: { michael@0: DState* s; michael@0: if (strm == NULL) return BZ_PARAM_ERROR; michael@0: s = strm->state; michael@0: if (s == NULL) return BZ_PARAM_ERROR; michael@0: if (s->strm != strm) return BZ_PARAM_ERROR; michael@0: michael@0: if (s->tt != NULL) BZFREE(s->tt); michael@0: if (s->ll16 != NULL) BZFREE(s->ll16); michael@0: if (s->ll4 != NULL) BZFREE(s->ll4); michael@0: michael@0: BZFREE(strm->state); michael@0: strm->state = NULL; michael@0: michael@0: return BZ_OK; michael@0: } michael@0: michael@0: michael@0: #ifndef BZ_NO_STDIO michael@0: /*---------------------------------------------------*/ michael@0: /*--- File I/O stuff ---*/ michael@0: /*---------------------------------------------------*/ michael@0: michael@0: #define BZ_SETERR(eee) \ michael@0: { \ michael@0: if (bzerror != NULL) *bzerror = eee; \ michael@0: if (bzf != NULL) bzf->lastErr = eee; \ michael@0: } michael@0: michael@0: typedef michael@0: struct { michael@0: FILE* handle; michael@0: Char buf[BZ_MAX_UNUSED]; michael@0: Int32 bufN; michael@0: Bool writing; michael@0: bz_stream strm; michael@0: Int32 lastErr; michael@0: Bool initialisedOk; michael@0: } michael@0: bzFile; michael@0: michael@0: michael@0: /*---------------------------------------------*/ michael@0: static Bool myfeof ( FILE* f ) michael@0: { michael@0: Int32 c = fgetc ( f ); michael@0: if (c == EOF) return True; michael@0: ungetc ( c, f ); michael@0: return False; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: BZFILE* BZ_API(BZ2_bzWriteOpen) michael@0: ( int* bzerror, michael@0: FILE* f, michael@0: int blockSize100k, michael@0: int verbosity, michael@0: int workFactor ) michael@0: { michael@0: Int32 ret; michael@0: bzFile* bzf = NULL; michael@0: michael@0: BZ_SETERR(BZ_OK); michael@0: michael@0: if (f == NULL || michael@0: (blockSize100k < 1 || blockSize100k > 9) || michael@0: (workFactor < 0 || workFactor > 250) || michael@0: (verbosity < 0 || verbosity > 4)) michael@0: { BZ_SETERR(BZ_PARAM_ERROR); return NULL; }; michael@0: michael@0: if (ferror(f)) michael@0: { BZ_SETERR(BZ_IO_ERROR); return NULL; }; michael@0: michael@0: bzf = malloc ( sizeof(bzFile) ); michael@0: if (bzf == NULL) michael@0: { BZ_SETERR(BZ_MEM_ERROR); return NULL; }; michael@0: michael@0: BZ_SETERR(BZ_OK); michael@0: bzf->initialisedOk = False; michael@0: bzf->bufN = 0; michael@0: bzf->handle = f; michael@0: bzf->writing = True; michael@0: bzf->strm.bzalloc = NULL; michael@0: bzf->strm.bzfree = NULL; michael@0: bzf->strm.opaque = NULL; michael@0: michael@0: if (workFactor == 0) workFactor = 30; michael@0: ret = BZ2_bzCompressInit ( &(bzf->strm), blockSize100k, michael@0: verbosity, workFactor ); michael@0: if (ret != BZ_OK) michael@0: { BZ_SETERR(ret); free(bzf); return NULL; }; michael@0: michael@0: bzf->strm.avail_in = 0; michael@0: bzf->initialisedOk = True; michael@0: return bzf; michael@0: } michael@0: michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: void BZ_API(BZ2_bzWrite) michael@0: ( int* bzerror, michael@0: BZFILE* b, michael@0: void* buf, michael@0: int len ) michael@0: { michael@0: Int32 n, n2, ret; michael@0: bzFile* bzf = (bzFile*)b; michael@0: michael@0: BZ_SETERR(BZ_OK); michael@0: if (bzf == NULL || buf == NULL || len < 0) michael@0: { BZ_SETERR(BZ_PARAM_ERROR); return; }; michael@0: if (!(bzf->writing)) michael@0: { BZ_SETERR(BZ_SEQUENCE_ERROR); return; }; michael@0: if (ferror(bzf->handle)) michael@0: { BZ_SETERR(BZ_IO_ERROR); return; }; michael@0: michael@0: if (len == 0) michael@0: { BZ_SETERR(BZ_OK); return; }; michael@0: michael@0: bzf->strm.avail_in = len; michael@0: bzf->strm.next_in = buf; michael@0: michael@0: while (True) { michael@0: bzf->strm.avail_out = BZ_MAX_UNUSED; michael@0: bzf->strm.next_out = bzf->buf; michael@0: ret = BZ2_bzCompress ( &(bzf->strm), BZ_RUN ); michael@0: if (ret != BZ_RUN_OK) michael@0: { BZ_SETERR(ret); return; }; michael@0: michael@0: if (bzf->strm.avail_out < BZ_MAX_UNUSED) { michael@0: n = BZ_MAX_UNUSED - bzf->strm.avail_out; michael@0: n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar), michael@0: n, bzf->handle ); michael@0: if (n != n2 || ferror(bzf->handle)) michael@0: { BZ_SETERR(BZ_IO_ERROR); return; }; michael@0: } michael@0: michael@0: if (bzf->strm.avail_in == 0) michael@0: { BZ_SETERR(BZ_OK); return; }; michael@0: } michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: void BZ_API(BZ2_bzWriteClose) michael@0: ( int* bzerror, michael@0: BZFILE* b, michael@0: int abandon, michael@0: unsigned int* nbytes_in, michael@0: unsigned int* nbytes_out ) michael@0: { michael@0: BZ2_bzWriteClose64 ( bzerror, b, abandon, michael@0: nbytes_in, NULL, nbytes_out, NULL ); michael@0: } michael@0: michael@0: michael@0: void BZ_API(BZ2_bzWriteClose64) michael@0: ( int* bzerror, michael@0: BZFILE* b, michael@0: int abandon, michael@0: unsigned int* nbytes_in_lo32, michael@0: unsigned int* nbytes_in_hi32, michael@0: unsigned int* nbytes_out_lo32, michael@0: unsigned int* nbytes_out_hi32 ) michael@0: { michael@0: Int32 n, n2, ret; michael@0: bzFile* bzf = (bzFile*)b; michael@0: michael@0: if (bzf == NULL) michael@0: { BZ_SETERR(BZ_OK); return; }; michael@0: if (!(bzf->writing)) michael@0: { BZ_SETERR(BZ_SEQUENCE_ERROR); return; }; michael@0: if (ferror(bzf->handle)) michael@0: { BZ_SETERR(BZ_IO_ERROR); return; }; michael@0: michael@0: if (nbytes_in_lo32 != NULL) *nbytes_in_lo32 = 0; michael@0: if (nbytes_in_hi32 != NULL) *nbytes_in_hi32 = 0; michael@0: if (nbytes_out_lo32 != NULL) *nbytes_out_lo32 = 0; michael@0: if (nbytes_out_hi32 != NULL) *nbytes_out_hi32 = 0; michael@0: michael@0: if ((!abandon) && bzf->lastErr == BZ_OK) { michael@0: while (True) { michael@0: bzf->strm.avail_out = BZ_MAX_UNUSED; michael@0: bzf->strm.next_out = bzf->buf; michael@0: ret = BZ2_bzCompress ( &(bzf->strm), BZ_FINISH ); michael@0: if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END) michael@0: { BZ_SETERR(ret); return; }; michael@0: michael@0: if (bzf->strm.avail_out < BZ_MAX_UNUSED) { michael@0: n = BZ_MAX_UNUSED - bzf->strm.avail_out; michael@0: n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar), michael@0: n, bzf->handle ); michael@0: if (n != n2 || ferror(bzf->handle)) michael@0: { BZ_SETERR(BZ_IO_ERROR); return; }; michael@0: } michael@0: michael@0: if (ret == BZ_STREAM_END) break; michael@0: } michael@0: } michael@0: michael@0: if ( !abandon && !ferror ( bzf->handle ) ) { michael@0: fflush ( bzf->handle ); michael@0: if (ferror(bzf->handle)) michael@0: { BZ_SETERR(BZ_IO_ERROR); return; }; michael@0: } michael@0: michael@0: if (nbytes_in_lo32 != NULL) michael@0: *nbytes_in_lo32 = bzf->strm.total_in_lo32; michael@0: if (nbytes_in_hi32 != NULL) michael@0: *nbytes_in_hi32 = bzf->strm.total_in_hi32; michael@0: if (nbytes_out_lo32 != NULL) michael@0: *nbytes_out_lo32 = bzf->strm.total_out_lo32; michael@0: if (nbytes_out_hi32 != NULL) michael@0: *nbytes_out_hi32 = bzf->strm.total_out_hi32; michael@0: michael@0: BZ_SETERR(BZ_OK); michael@0: BZ2_bzCompressEnd ( &(bzf->strm) ); michael@0: free ( bzf ); michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: BZFILE* BZ_API(BZ2_bzReadOpen) michael@0: ( int* bzerror, michael@0: FILE* f, michael@0: int verbosity, michael@0: int small, michael@0: void* unused, michael@0: int nUnused ) michael@0: { michael@0: bzFile* bzf = NULL; michael@0: int ret; michael@0: michael@0: BZ_SETERR(BZ_OK); michael@0: michael@0: if (f == NULL || michael@0: (small != 0 && small != 1) || michael@0: (verbosity < 0 || verbosity > 4) || michael@0: (unused == NULL && nUnused != 0) || michael@0: (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED))) michael@0: { BZ_SETERR(BZ_PARAM_ERROR); return NULL; }; michael@0: michael@0: if (ferror(f)) michael@0: { BZ_SETERR(BZ_IO_ERROR); return NULL; }; michael@0: michael@0: bzf = malloc ( sizeof(bzFile) ); michael@0: if (bzf == NULL) michael@0: { BZ_SETERR(BZ_MEM_ERROR); return NULL; }; michael@0: michael@0: BZ_SETERR(BZ_OK); michael@0: michael@0: bzf->initialisedOk = False; michael@0: bzf->handle = f; michael@0: bzf->bufN = 0; michael@0: bzf->writing = False; michael@0: bzf->strm.bzalloc = NULL; michael@0: bzf->strm.bzfree = NULL; michael@0: bzf->strm.opaque = NULL; michael@0: michael@0: while (nUnused > 0) { michael@0: bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++; michael@0: unused = ((void*)( 1 + ((UChar*)(unused)) )); michael@0: nUnused--; michael@0: } michael@0: michael@0: ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small ); michael@0: if (ret != BZ_OK) michael@0: { BZ_SETERR(ret); free(bzf); return NULL; }; michael@0: michael@0: bzf->strm.avail_in = bzf->bufN; michael@0: bzf->strm.next_in = bzf->buf; michael@0: michael@0: bzf->initialisedOk = True; michael@0: return bzf; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b ) michael@0: { michael@0: bzFile* bzf = (bzFile*)b; michael@0: michael@0: BZ_SETERR(BZ_OK); michael@0: if (bzf == NULL) michael@0: { BZ_SETERR(BZ_OK); return; }; michael@0: michael@0: if (bzf->writing) michael@0: { BZ_SETERR(BZ_SEQUENCE_ERROR); return; }; michael@0: michael@0: if (bzf->initialisedOk) michael@0: (void)BZ2_bzDecompressEnd ( &(bzf->strm) ); michael@0: free ( bzf ); michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: int BZ_API(BZ2_bzRead) michael@0: ( int* bzerror, michael@0: BZFILE* b, michael@0: void* buf, michael@0: int len ) michael@0: { michael@0: Int32 n, ret; michael@0: bzFile* bzf = (bzFile*)b; michael@0: michael@0: BZ_SETERR(BZ_OK); michael@0: michael@0: if (bzf == NULL || buf == NULL || len < 0) michael@0: { BZ_SETERR(BZ_PARAM_ERROR); return 0; }; michael@0: michael@0: if (bzf->writing) michael@0: { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; }; michael@0: michael@0: if (len == 0) michael@0: { BZ_SETERR(BZ_OK); return 0; }; michael@0: michael@0: bzf->strm.avail_out = len; michael@0: bzf->strm.next_out = buf; michael@0: michael@0: while (True) { michael@0: michael@0: if (ferror(bzf->handle)) michael@0: { BZ_SETERR(BZ_IO_ERROR); return 0; }; michael@0: michael@0: if (bzf->strm.avail_in == 0 && !myfeof(bzf->handle)) { michael@0: n = fread ( bzf->buf, sizeof(UChar), michael@0: BZ_MAX_UNUSED, bzf->handle ); michael@0: if (ferror(bzf->handle)) michael@0: { BZ_SETERR(BZ_IO_ERROR); return 0; }; michael@0: bzf->bufN = n; michael@0: bzf->strm.avail_in = bzf->bufN; michael@0: bzf->strm.next_in = bzf->buf; michael@0: } michael@0: michael@0: ret = BZ2_bzDecompress ( &(bzf->strm) ); michael@0: michael@0: if (ret != BZ_OK && ret != BZ_STREAM_END) michael@0: { BZ_SETERR(ret); return 0; }; michael@0: michael@0: if (ret == BZ_OK && myfeof(bzf->handle) && michael@0: bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0) michael@0: { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; }; michael@0: michael@0: if (ret == BZ_STREAM_END) michael@0: { BZ_SETERR(BZ_STREAM_END); michael@0: return len - bzf->strm.avail_out; }; michael@0: if (bzf->strm.avail_out == 0) michael@0: { BZ_SETERR(BZ_OK); return len; }; michael@0: michael@0: } michael@0: michael@0: return 0; /*not reached*/ michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: void BZ_API(BZ2_bzReadGetUnused) michael@0: ( int* bzerror, michael@0: BZFILE* b, michael@0: void** unused, michael@0: int* nUnused ) michael@0: { michael@0: bzFile* bzf = (bzFile*)b; michael@0: if (bzf == NULL) michael@0: { BZ_SETERR(BZ_PARAM_ERROR); return; }; michael@0: if (bzf->lastErr != BZ_STREAM_END) michael@0: { BZ_SETERR(BZ_SEQUENCE_ERROR); return; }; michael@0: if (unused == NULL || nUnused == NULL) michael@0: { BZ_SETERR(BZ_PARAM_ERROR); return; }; michael@0: michael@0: BZ_SETERR(BZ_OK); michael@0: *nUnused = bzf->strm.avail_in; michael@0: *unused = bzf->strm.next_in; michael@0: } michael@0: #endif michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: /*--- Misc convenience stuff ---*/ michael@0: /*---------------------------------------------------*/ michael@0: michael@0: /*---------------------------------------------------*/ michael@0: int BZ_API(BZ2_bzBuffToBuffCompress) michael@0: ( char* dest, michael@0: unsigned int* destLen, michael@0: char* source, michael@0: unsigned int sourceLen, michael@0: int blockSize100k, michael@0: int verbosity, michael@0: int workFactor ) michael@0: { michael@0: bz_stream strm; michael@0: int ret; michael@0: michael@0: if (dest == NULL || destLen == NULL || michael@0: source == NULL || michael@0: blockSize100k < 1 || blockSize100k > 9 || michael@0: verbosity < 0 || verbosity > 4 || michael@0: workFactor < 0 || workFactor > 250) michael@0: return BZ_PARAM_ERROR; michael@0: michael@0: if (workFactor == 0) workFactor = 30; michael@0: strm.bzalloc = NULL; michael@0: strm.bzfree = NULL; michael@0: strm.opaque = NULL; michael@0: ret = BZ2_bzCompressInit ( &strm, blockSize100k, michael@0: verbosity, workFactor ); michael@0: if (ret != BZ_OK) return ret; michael@0: michael@0: strm.next_in = source; michael@0: strm.next_out = dest; michael@0: strm.avail_in = sourceLen; michael@0: strm.avail_out = *destLen; michael@0: michael@0: ret = BZ2_bzCompress ( &strm, BZ_FINISH ); michael@0: if (ret == BZ_FINISH_OK) goto output_overflow; michael@0: if (ret != BZ_STREAM_END) goto errhandler; michael@0: michael@0: /* normal termination */ michael@0: *destLen -= strm.avail_out; michael@0: BZ2_bzCompressEnd ( &strm ); michael@0: return BZ_OK; michael@0: michael@0: output_overflow: michael@0: BZ2_bzCompressEnd ( &strm ); michael@0: return BZ_OUTBUFF_FULL; michael@0: michael@0: errhandler: michael@0: BZ2_bzCompressEnd ( &strm ); michael@0: return ret; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: int BZ_API(BZ2_bzBuffToBuffDecompress) michael@0: ( char* dest, michael@0: unsigned int* destLen, michael@0: char* source, michael@0: unsigned int sourceLen, michael@0: int small, michael@0: int verbosity ) michael@0: { michael@0: bz_stream strm; michael@0: int ret; michael@0: michael@0: if (dest == NULL || destLen == NULL || michael@0: source == NULL || michael@0: (small != 0 && small != 1) || michael@0: verbosity < 0 || verbosity > 4) michael@0: return BZ_PARAM_ERROR; michael@0: michael@0: strm.bzalloc = NULL; michael@0: strm.bzfree = NULL; michael@0: strm.opaque = NULL; michael@0: ret = BZ2_bzDecompressInit ( &strm, verbosity, small ); michael@0: if (ret != BZ_OK) return ret; michael@0: michael@0: strm.next_in = source; michael@0: strm.next_out = dest; michael@0: strm.avail_in = sourceLen; michael@0: strm.avail_out = *destLen; michael@0: michael@0: ret = BZ2_bzDecompress ( &strm ); michael@0: if (ret == BZ_OK) goto output_overflow_or_eof; michael@0: if (ret != BZ_STREAM_END) goto errhandler; michael@0: michael@0: /* normal termination */ michael@0: *destLen -= strm.avail_out; michael@0: BZ2_bzDecompressEnd ( &strm ); michael@0: return BZ_OK; michael@0: michael@0: output_overflow_or_eof: michael@0: if (strm.avail_out > 0) { michael@0: BZ2_bzDecompressEnd ( &strm ); michael@0: return BZ_UNEXPECTED_EOF; michael@0: } else { michael@0: BZ2_bzDecompressEnd ( &strm ); michael@0: return BZ_OUTBUFF_FULL; michael@0: }; michael@0: michael@0: errhandler: michael@0: BZ2_bzDecompressEnd ( &strm ); michael@0: return ret; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: /*-- michael@0: Code contributed by Yoshioka Tsuneo (tsuneo@rr.iij4u.or.jp) michael@0: to support better zlib compatibility. michael@0: This code is not _officially_ part of libbzip2 (yet); michael@0: I haven't tested it, documented it, or considered the michael@0: threading-safeness of it. michael@0: If this code breaks, please contact both Yoshioka and me. michael@0: --*/ michael@0: /*---------------------------------------------------*/ michael@0: michael@0: /*---------------------------------------------------*/ michael@0: /*-- michael@0: return version like "0.9.5d, 4-Sept-1999". michael@0: --*/ michael@0: const char * BZ_API(BZ2_bzlibVersion)(void) michael@0: { michael@0: return BZ_VERSION; michael@0: } michael@0: michael@0: michael@0: #ifndef BZ_NO_STDIO michael@0: /*---------------------------------------------------*/ michael@0: michael@0: #ifdef WINCE michael@0: #ifndef setmode michael@0: #define setmode _setmode michael@0: #endif michael@0: #ifndef O_BINARY michael@0: #define O_BINARY _O_BINARY michael@0: #endif michael@0: static michael@0: FILE * fdopen(int fd, const char *mode) michael@0: { michael@0: wchar_t wMode[10]; michael@0: MultiByteToWideChar(CP_ACP, 0, mode, -1, wMode, 10); michael@0: return _wfdopen((void*)fd, wMode); michael@0: } michael@0: #endif michael@0: michael@0: #if (defined(_WIN32) || defined(OS2) || defined(MSDOS)) michael@0: # include michael@0: # include michael@0: # define SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY) michael@0: #else michael@0: # define SET_BINARY_MODE(file) michael@0: #endif michael@0: static michael@0: BZFILE * bzopen_or_bzdopen michael@0: ( const char *path, /* no use when bzdopen */ michael@0: int fd, /* no use when bzdopen */ michael@0: const char *mode, michael@0: int open_mode) /* bzopen: 0, bzdopen:1 */ michael@0: { michael@0: int bzerr; michael@0: char unused[BZ_MAX_UNUSED]; michael@0: int blockSize100k = 9; michael@0: int writing = 0; michael@0: char mode2[10] = ""; michael@0: FILE *fp = NULL; michael@0: BZFILE *bzfp = NULL; michael@0: int verbosity = 0; michael@0: int workFactor = 30; michael@0: int smallMode = 0; michael@0: int nUnused = 0; michael@0: michael@0: if (mode == NULL) return NULL; michael@0: while (*mode) { michael@0: switch (*mode) { michael@0: case 'r': michael@0: writing = 0; break; michael@0: case 'w': michael@0: writing = 1; break; michael@0: case 's': michael@0: smallMode = 1; break; michael@0: default: michael@0: if (isdigit((int)(*mode))) { michael@0: blockSize100k = *mode-BZ_HDR_0; michael@0: } michael@0: } michael@0: mode++; michael@0: } michael@0: strcat(mode2, writing ? "w" : "r" ); michael@0: strcat(mode2,"b"); /* binary mode */ michael@0: michael@0: if (open_mode==0) { michael@0: if (path==NULL || strcmp(path,"")==0) { michael@0: fp = (writing ? stdout : stdin); michael@0: SET_BINARY_MODE(fp); michael@0: } else { michael@0: fp = fopen(path,mode2); michael@0: } michael@0: } else { michael@0: #ifdef BZ_STRICT_ANSI michael@0: fp = NULL; michael@0: #else michael@0: fp = fdopen(fd,mode2); michael@0: #endif michael@0: } michael@0: if (fp == NULL) return NULL; michael@0: michael@0: if (writing) { michael@0: /* Guard against total chaos and anarchy -- JRS */ michael@0: if (blockSize100k < 1) blockSize100k = 1; michael@0: if (blockSize100k > 9) blockSize100k = 9; michael@0: bzfp = BZ2_bzWriteOpen(&bzerr,fp,blockSize100k, michael@0: verbosity,workFactor); michael@0: } else { michael@0: bzfp = BZ2_bzReadOpen(&bzerr,fp,verbosity,smallMode, michael@0: unused,nUnused); michael@0: } michael@0: if (bzfp == NULL) { michael@0: if (fp != stdin && fp != stdout) fclose(fp); michael@0: return NULL; michael@0: } michael@0: return bzfp; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: /*-- michael@0: open file for read or write. michael@0: ex) bzopen("file","w9") michael@0: case path="" or NULL => use stdin or stdout. michael@0: --*/ michael@0: BZFILE * BZ_API(BZ2_bzopen) michael@0: ( const char *path, michael@0: const char *mode ) michael@0: { michael@0: return bzopen_or_bzdopen(path,-1,mode,/*bzopen*/0); michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: BZFILE * BZ_API(BZ2_bzdopen) michael@0: ( int fd, michael@0: const char *mode ) michael@0: { michael@0: return bzopen_or_bzdopen(NULL,fd,mode,/*bzdopen*/1); michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: int BZ_API(BZ2_bzread) (BZFILE* b, void* buf, int len ) michael@0: { michael@0: int bzerr, nread; michael@0: if (((bzFile*)b)->lastErr == BZ_STREAM_END) return 0; michael@0: nread = BZ2_bzRead(&bzerr,b,buf,len); michael@0: if (bzerr == BZ_OK || bzerr == BZ_STREAM_END) { michael@0: return nread; michael@0: } else { michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: int BZ_API(BZ2_bzwrite) (BZFILE* b, void* buf, int len ) michael@0: { michael@0: int bzerr; michael@0: michael@0: BZ2_bzWrite(&bzerr,b,buf,len); michael@0: if(bzerr == BZ_OK){ michael@0: return len; michael@0: }else{ michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: int BZ_API(BZ2_bzflush) (BZFILE *b) michael@0: { michael@0: /* do nothing now... */ michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: void BZ_API(BZ2_bzclose) (BZFILE* b) michael@0: { michael@0: int bzerr; michael@0: FILE *fp; michael@0: michael@0: if (b==NULL) {return;} michael@0: fp = ((bzFile *)b)->handle; michael@0: if(((bzFile*)b)->writing){ michael@0: BZ2_bzWriteClose(&bzerr,b,0,NULL,NULL); michael@0: if(bzerr != BZ_OK){ michael@0: BZ2_bzWriteClose(NULL,b,1,NULL,NULL); michael@0: } michael@0: }else{ michael@0: BZ2_bzReadClose(&bzerr,b); michael@0: } michael@0: if(fp!=stdin && fp!=stdout){ michael@0: fclose(fp); michael@0: } michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------*/ michael@0: /*-- michael@0: return last error code michael@0: --*/ michael@0: static const char *bzerrorstrings[] = { michael@0: "OK" michael@0: ,"SEQUENCE_ERROR" michael@0: ,"PARAM_ERROR" michael@0: ,"MEM_ERROR" michael@0: ,"DATA_ERROR" michael@0: ,"DATA_ERROR_MAGIC" michael@0: ,"IO_ERROR" michael@0: ,"UNEXPECTED_EOF" michael@0: ,"OUTBUFF_FULL" michael@0: ,"CONFIG_ERROR" michael@0: ,"???" /* for future */ michael@0: ,"???" /* for future */ michael@0: ,"???" /* for future */ michael@0: ,"???" /* for future */ michael@0: ,"???" /* for future */ michael@0: ,"???" /* for future */ michael@0: }; michael@0: michael@0: michael@0: const char * BZ_API(BZ2_bzerror) (BZFILE *b, int *errnum) michael@0: { michael@0: int err = ((bzFile *)b)->lastErr; michael@0: michael@0: if(err>0) err = 0; michael@0: *errnum = err; michael@0: return bzerrorstrings[err*-1]; michael@0: } michael@0: #endif michael@0: michael@0: michael@0: /*-------------------------------------------------------------*/ michael@0: /*--- end bzlib.c ---*/ michael@0: /*-------------------------------------------------------------*/