#pragma prototyped
/*-------------------------------------------------------------*/
/*--- Library top-level functions. ---*/
/*--- bzlib.c ---*/
/*-------------------------------------------------------------*/
/*--
library for lossless, block-sorting data compression.
Copyright (C) 1996-1998 Julian R Seward. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
3. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
4. The name of the author may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Julian Seward, Guildford, Surrey, UK.
jseward@acm.org
This program is based on (at least) the work of:
Mike Burrows
David Wheeler
Peter Fenwick
Alistair Moffat
Radford Neal
Ian H. Witten
Robert Sedgewick
Jon L. Bentley
For more information on these sources, see the manual.
--*/
/*--
CHANGES
~~~~~~~
0.9.0 -- original version.
0.9.0a/b -- no changes in this file.
0.9.0c
* made zero-length BZ_FLUSH work correctly in bzCompress().
* fixed bzread to correctly handle read requests after EOF.
* wrong parameter order in call to bzDecompressInit in
bzBuffToBuffDecompress. Fixed.
--*/
#include "bzhdr.h"
/*---------------------------------------------------*/
/*--- Compression stuff ---*/
/*---------------------------------------------------*/
/*---------------------------------------------------*/
#ifndef BZ_NO_STDIO
{
"it to me at: jseward@acm.org. If this happened when\n"
"you were using some program which uses libbzip2 as a\n"
"component, you should also report this bug to the author(s)\n"
"of that program. Please make an effort to report this bug;\n"
"timely and accurate bug reports eventually lead to higher\n"
"quality software. Thx. Julian Seward, 18 October 1998.\n\n",
);
exit(3);
}
#endif
/*---------------------------------------------------*/
static
{
return v;
}
static
{
}
/*---------------------------------------------------*/
static
{
Int32 i;
s->nblock = 0;
s->numZ = 0;
s->state_out_pos = 0;
BZ_INITIALISE_CRC ( s->blockCRC );
s->blockNo++;
}
/*---------------------------------------------------*/
static
{
s->state_in_ch = 256;
s->state_in_len = 0;
}
static
{
return False; else
return True;
}
/*---------------------------------------------------*/
int blockSize100k,
int verbosity,
int workFactor )
{
Int32 n;
EState* s;
return BZ_PARAM_ERROR;
if (s == NULL) return BZ_MEM_ERROR;
n = 100000 * blockSize100k;
return BZ_MEM_ERROR;
}
s->blockNo = 0;
s->state = BZ_S_INPUT;
s->mode = BZ_M_RUNNING;
s->combinedCRC = 0;
s->blockSize100k = blockSize100k;
s->workFactor = workFactor;
s->nBlocksRandomised = 0;
init_RL ( s );
prepare_new_block ( s );
return BZ_OK;
}
/*---------------------------------------------------*/
static
{
Int32 i;
for (i = 0; i < s->state_in_len; i++) {
}
switch (s->state_in_len) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
s->nblock++;
break;
}
}
/*---------------------------------------------------*/
static
{
init_RL ( s );
}
/*---------------------------------------------------*/
{ \
/*-- fast track the common case --*/ \
} \
else \
/*-- general, uncommon cases --*/ \
add_pair_to_block ( zs ); \
} else { \
zs->state_in_len++; \
} \
}
/*---------------------------------------------------*/
static
{
if (s->mode == BZ_M_RUNNING) {
/*-- fast track the common case --*/
while (True) {
/*-- block full? --*/
/*-- no input? --*/
progress_in = True;
}
} else {
/*-- general, uncommon case --*/
while (True) {
/*-- block full? --*/
/*-- no input? --*/
if (s->avail_in_expect == 0) break;
progress_in = True;
s->avail_in_expect--;
}
}
return progress_in;
}
/*---------------------------------------------------*/
static
{
while (True) {
/*-- no output space? --*/
/*-- block done? --*/
if (s->state_out_pos >= s->numZ) break;
progress_out = True;
s->state_out_pos++;
}
return progress_out;
}
/*---------------------------------------------------*/
static
{
while (True) {
if (s->state == BZ_S_OUTPUT) {
progress_out |= copy_output_until_stop ( s );
if (s->state_out_pos < s->numZ) break;
if (s->mode == BZ_M_FINISHING &&
s->avail_in_expect == 0 &&
isempty_RL(s)) break;
prepare_new_block ( s );
s->state = BZ_S_INPUT;
if (s->mode == BZ_M_FLUSHING &&
s->avail_in_expect == 0 &&
isempty_RL(s)) break;
}
if (s->state == BZ_S_INPUT) {
progress_in |= copy_input_until_stop ( s );
flush_RL ( s );
s->state = BZ_S_OUTPUT;
}
else
compressBlock ( s, False );
s->state = BZ_S_OUTPUT;
}
else
break;
}
}
}
return progress_in || progress_out;
}
/*---------------------------------------------------*/
{
EState* s;
if (s == NULL) return BZ_PARAM_ERROR;
switch (s->mode) {
case BZ_M_IDLE:
return BZ_SEQUENCE_ERROR;
case BZ_M_RUNNING:
}
else
s->mode = BZ_M_FLUSHING;
goto preswitch;
}
else
s->mode = BZ_M_FINISHING;
goto preswitch;
}
else
return BZ_PARAM_ERROR;
case BZ_M_FLUSHING:
if (s->avail_in_expect > 0 || !isempty_RL(s) ||
s->mode = BZ_M_RUNNING;
return BZ_RUN_OK;
case BZ_M_FINISHING:
if (!progress) return BZ_SEQUENCE_ERROR;
if (s->avail_in_expect > 0 || !isempty_RL(s) ||
return BZ_STREAM_END;
}
return BZ_OK; /*--not reached--*/
}
/*---------------------------------------------------*/
{
EState* s;
if (s == NULL) return BZ_PARAM_ERROR;
return BZ_OK;
}
/*---------------------------------------------------*/
/*--- Decompression stuff ---*/
/*---------------------------------------------------*/
/*---------------------------------------------------*/
int verbosity,
int small )
{
DState* s;
if (s == NULL) return BZ_MEM_ERROR;
s->state = BZ_X_MAGIC_1;
s->bsLive = 0;
s->bsBuff = 0;
s->calculatedCombinedCRC = 0;
s->currBlockNo = 0;
return BZ_OK;
}
/*---------------------------------------------------*/
static
{
if (s->blockRandomised) {
while (True) {
/* try to finish existing run */
while (True) {
if (s->state_out_len == 0) break;
s->state_out_len--;
}
/* can a new run be started? */
s->state_out_len = 1;
s->state_out_ch = s->k0;
s->state_out_len = 2;
s->state_out_len = 3;
}
} else {
/* restore */
/* end restore */
while (True) {
/* try to finish existing run */
if (c_state_out_len > 0) {
while (True) {
if (cs_avail_out == 0) goto return_notr;
if (c_state_out_len == 1) break;
cs_next_out++;
cs_avail_out--;
}
{
if (cs_avail_out == 0) {
};
cs_next_out++;
cs_avail_out--;
}
}
/* can a new run be started? */
if (c_nblock_used == s_save_nblockPP) {
c_state_out_len = 0; goto return_notr;
};
};
if (c_nblock_used == s_save_nblockPP)
goto s_state_out_len_eq_one;
c_state_out_len = 2;
if (c_nblock_used == s_save_nblockPP) continue;
c_state_out_len = 3;
if (c_nblock_used == s_save_nblockPP) continue;
}
/* save */
s->state_out_ch = c_state_out_ch;
s->state_out_len = c_state_out_len;
s->nblock_used = c_nblock_used;
/* end save */
}
}
/*---------------------------------------------------*/
{
nb = 0;
na = 256;
do {
}
return nb;
}
/*---------------------------------------------------*/
static
{
if (s->blockRandomised) {
while (True) {
/* try to finish existing run */
while (True) {
if (s->state_out_len == 0) break;
s->state_out_len--;
}
/* can a new run be started? */
s->state_out_len = 1;
s->state_out_ch = s->k0;
s->state_out_len = 2;
s->state_out_len = 3;
}
} else {
while (True) {
/* try to finish existing run */
while (True) {
if (s->state_out_len == 0) break;
s->state_out_len--;
}
/* can a new run be started? */
s->state_out_len = 1;
s->state_out_ch = s->k0;
s->state_out_len = 2;
s->state_out_len = 3;
}
}
}
/*---------------------------------------------------*/
{
DState* s;
if (s == NULL) return BZ_PARAM_ERROR;
while (True) {
if (s->state == BZ_X_OUTPUT) {
if (s->smallDecompress)
unRLE_obuf_to_output_SMALL ( s ); else
unRLE_obuf_to_output_FAST ( s );
BZ_FINALISE_CRC ( s->calculatedBlockCRC );
if (s->verbosity >= 3)
s->calculatedBlockCRC );
if (s->calculatedBlockCRC != s->storedBlockCRC)
return BZ_DATA_ERROR;
= (s->calculatedCombinedCRC << 1) |
(s->calculatedCombinedCRC >> 31);
s->calculatedCombinedCRC ^= s->calculatedBlockCRC;
s->state = BZ_X_BLKHDR_1;
} else {
return BZ_OK;
}
}
if (s->state >= BZ_X_MAGIC_1) {
Int32 r = decompress ( s );
if (r == BZ_STREAM_END) {
if (s->verbosity >= 3)
VPrintf2 ( "\n combined CRCs: stored = 0x%x, computed = 0x%x",
s->storedCombinedCRC, s->calculatedCombinedCRC );
if (s->calculatedCombinedCRC != s->storedCombinedCRC)
return BZ_DATA_ERROR;
return r;
}
if (s->state != BZ_X_OUTPUT) return r;
}
}
AssertH ( 0, 6001 );
/*notreached*/
}
/*---------------------------------------------------*/
{
DState* s;
if (s == NULL) return BZ_PARAM_ERROR;
return BZ_OK;
}
#ifndef BZ_NO_STDIO
/*---------------------------------------------------*/
/*--- File I/O stuff ---*/
/*---------------------------------------------------*/
{ \
}
typedef
struct {
}
/*---------------------------------------------*/
{
ungetc ( c, f );
return False;
}
/*---------------------------------------------------*/
( int* bzerror,
FILE* f,
int blockSize100k,
int verbosity,
int workFactor )
{
if (f == NULL ||
if (ferror(f))
verbosity, workFactor );
return bzf;
}
/*---------------------------------------------------*/
( int* bzerror,
BZFILE* b,
void* buf,
int len )
{
{ BZ_SETERR(BZ_PARAM_ERROR); return; };
{ BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
{ BZ_SETERR(BZ_IO_ERROR); return; };
if (len == 0)
while (True) {
{ BZ_SETERR(BZ_IO_ERROR); return; };
}
}
}
/*---------------------------------------------------*/
( int* bzerror,
BZFILE* b,
int abandon,
unsigned int* nbytes_in,
unsigned int* nbytes_out )
{
{ BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
{ BZ_SETERR(BZ_IO_ERROR); return; };
while (True) {
{ BZ_SETERR(BZ_IO_ERROR); return; };
}
if (ret == BZ_STREAM_END) break;
}
}
{ BZ_SETERR(BZ_IO_ERROR); return; };
}
}
/*---------------------------------------------------*/
( int* bzerror,
FILE* f,
int verbosity,
int small,
void* unused,
int nUnused )
{
int ret;
if (f == NULL ||
if (ferror(f))
while (nUnused > 0) {
nUnused--;
}
return bzf;
}
/*---------------------------------------------------*/
{
{ BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
if (bzf->initialisedOk)
}
/*---------------------------------------------------*/
( int* bzerror,
BZFILE* b,
void* buf,
int len )
{
{ BZ_SETERR(BZ_PARAM_ERROR); return 0; };
{ BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
if (len == 0)
while (True) {
{ BZ_SETERR(BZ_IO_ERROR); return 0; };
{ BZ_SETERR(BZ_IO_ERROR); return 0; };
}
{ BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; };
if (ret == BZ_STREAM_END)
}
return 0; /*not reached*/
}
/*---------------------------------------------------*/
( int* bzerror,
BZFILE* b,
void* vUnused,
int* nUnused )
{
{ BZ_SETERR(BZ_PARAM_ERROR); return; };
{ BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
{ BZ_SETERR(BZ_PARAM_ERROR); return; };
}
#endif
/*---------------------------------------------------*/
/*--- Misc convenience stuff ---*/
/*---------------------------------------------------*/
/*---------------------------------------------------*/
( char* dest,
unsigned int* destLen,
char* source,
unsigned int sourceLen,
int blockSize100k,
int verbosity,
int workFactor )
{
int ret;
return BZ_PARAM_ERROR;
verbosity, workFactor );
/* normal termination */
bzCompressEnd ( &strm );
return BZ_OK;
bzCompressEnd ( &strm );
return BZ_OUTBUFF_FULL;
bzCompressEnd ( &strm );
return ret;
}
/*---------------------------------------------------*/
( char* dest,
unsigned int* destLen,
char* source,
unsigned int sourceLen,
int small,
int verbosity )
{
int ret;
return BZ_PARAM_ERROR;
/* normal termination */
bzDecompressEnd ( &strm );
return BZ_OK;
bzDecompressEnd ( &strm );
return BZ_UNEXPECTED_EOF;
} else {
bzDecompressEnd ( &strm );
return BZ_OUTBUFF_FULL;
};
bzDecompressEnd ( &strm );
return BZ_SEQUENCE_ERROR;
}
/*---------------------------------------------------*/
/*--
Code contributed by Yoshioka Tsuneo
(QWF00133@niftyserve.or.jp/tsuneo-y@is.aist-nara.ac.jp),
to support better zlib compatibility.
This code is not _officially_ part of libbzip2 (yet);
I haven't tested it, documented it, or considered the
threading-safeness of it.
If this code breaks, please contact both Yoshioka and me.
--*/
/*---------------------------------------------------*/
/*---------------------------------------------------*/
/*--
return version like "0.9.0c".
--*/
{
return BZ_VERSION;
}
#ifndef BZ_NO_STDIO
/*---------------------------------------------------*/
# include <fcntl.h>
# include <io.h>
# ifdef _MSC_VER
# else
# endif
#else
#endif
static
( const char *path, /* if !=0 */
int fd, /* if path==0 && fp==0 */
const char *mode,
const void* buf,
unsigned int len)
{
int bzerr;
int writing = 0;
int verbosity = 0;
int smallMode = 0;
int nUnused = 0;
int noClose = 0;
while(*mode){
switch(*mode){
case 'r':
writing = 0;break;
case 'w':
writing = 1;break;
case 's':
smallMode = 1;break;
case 'o':
noClose = 1;break;
default:
blockSize100k = 0;
mode++;
}
}else{
/* ignore */
}
}
mode++;
}
if(path) {
if(path[0]==0){
noClose = 1;
}else{
}
}else if (fp==0) {
#ifdef BZ_STRICT_ANSI
#else
#endif
}
if(writing){
}else{
}
return NULL;
}
return bzfp;
}
/*---------------------------------------------------*/
/*--
open file for read or write.
ex) bzopen("file","w9")
case path="" or NULL => use stdin or stdout.
--*/
( const char *path,
const char *mode )
{
}
/*---------------------------------------------------*/
const char *mode )
{
}
/*---------------------------------------------------*/
( int fd,
const char *mode )
{
}
/*---------------------------------------------------*/
( int fd,
const char* mode,
const void* buf,
unsigned int len )
{
}
/*---------------------------------------------------*/
{
return nread;
} else {
return -1;
}
}
/*---------------------------------------------------*/
{
int bzerr;
return len;
}else{
return -1;
}
}
/*---------------------------------------------------*/
{
return -1;
return 0;
return -1;
do {
return -1;
return -1;
}
} while (ret == BZ_FLUSH_OK);
return 0;
}
/*---------------------------------------------------*/
{
int bzerr;
if(b==NULL){return -1;}
return -1;
}
}else{
bzReadClose(&bzerr,b);
return -1;
}
}
return 0;
}
/*---------------------------------------------------*/
/*--
return last error code
--*/
static char *bzerrorstrings[] = {
"OK"
,"SEQUENCE_ERROR"
,"PARAM_ERROR"
,"MEM_ERROR"
,"DATA_ERROR"
,"DATA_ERROR_MAGIC"
,"IO_ERROR"
,"UNEXPECTED_EOF"
,"OUTBUFF_FULL"
,"???" /* for future */
,"???" /* for future */
,"???" /* for future */
,"???" /* for future */
,"???" /* for future */
,"???" /* for future */
};
{
}
#endif
/*-------------------------------------------------------------*/
/*--- end bzlib.c ---*/
/*-------------------------------------------------------------*/