34#pragma warning(disable : 4996)
42#define setmode _setmode
55 "\n\nbzip2/libbzip2: internal error number %d.\n"
56 "This is a bug in bzip2/libbzip2, %s.\n"
57 "Please report it to me at: jseward@bzip.org. If this happened\n"
58 "when you were using some program which uses libbzip2 as a\n"
59 "component, you should also report this bug to the author(s)\n"
60 "of that program. Please make an effort to report this bug;\n"
61 "timely and accurate bug reports eventually lead to higher\n"
62 "quality software. Thanks. Julian Seward, 10 December 2007.\n\n",
67 if (errcode == 1007) {
69 "\n*** A special note about internal error number 1007 ***\n"
71 "Experience suggests that a common cause of i.e. 1007\n"
72 "is unreliable memory or other hardware. The 1007 assertion\n"
73 "just happens to cross-check the results of huge numbers of\n"
74 "memory reads/writes, and so acts (unintendedly) as a stress\n"
75 "test of your memory system.\n"
77 "I suggest the following: try compressing the file again,\n"
78 "possibly monitoring progress in detail with the -vv flag.\n"
80 "* If the error cannot be reproduced, and/or happens at different\n"
81 " points in compression, you may have a flaky memory system.\n"
82 " Try a memory-test program. I have used Memtest86\n"
83 " (www.memtest86.com). At the time of writing it is free (GPLd).\n"
84 " Memtest86 tests memory much more thorougly than your BIOSs\n"
85 " power-on test, and may find failures that the BIOS doesn't.\n"
87 "* If the error can be repeatably reproduced, this is a bug in\n"
88 " bzip2, and I would very much like to hear about it. Please\n"
89 " let me know, and, ideally, save a copy of the file causing the\n"
90 " problem -- without which I will be unable to investigate it.\n"
102int bz_config_ok (
void )
104 if (
sizeof(
int) != 4)
return 0;
105 if (
sizeof(
short) != 2)
return 0;
106 if (
sizeof(
char) != 1)
return 0;
113void* default_bzalloc (
void* opaque,
Int32 items,
Int32 size )
115 void* v =
malloc ( items * size );
120void default_bzfree (
void* opaque,
void* addr )
122 if (addr != NULL)
free ( addr );
128void prepare_new_block (
EState* s )
135 for (i = 0; i < 256; i++) s->
inUse[i] =
False;
171 blockSize100k < 1 || blockSize100k > 9 ||
172 workFactor < 0 || workFactor > 250)
175 if (workFactor == 0) workFactor = 30;
176 if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
177 if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
187 n = 100000 * blockSize100k;
192 if (s->
arr1 == NULL || s->
arr2 == NULL || s->
ftab == NULL) {
205 s->
nblockMAX = 100000 * blockSize100k - 19;
215 strm->total_in_lo32 = 0;
216 strm->total_in_hi32 = 0;
217 strm->total_out_lo32 = 0;
218 strm->total_out_hi32 = 0;
220 prepare_new_block ( s );
227void add_pair_to_block (
EState* s )
263void flush_RL (
EState* s )
271#define ADD_CHAR_TO_BLOCK(zs,zchh0) \
273 UInt32 zchh = (UInt32)(zchh0); \
275 if (zchh != zs->state_in_ch && \
276 zs->state_in_len == 1) { \
277 UChar ch = (UChar)(zs->state_in_ch); \
278 BZ_UPDATE_CRC( zs->blockCRC, ch ); \
279 zs->inUse[zs->state_in_ch] = True; \
280 zs->block[zs->nblock] = (UChar)ch; \
282 zs->state_in_ch = zchh; \
286 if (zchh != zs->state_in_ch || \
287 zs->state_in_len == 255) { \
288 if (zs->state_in_ch < 256) \
289 add_pair_to_block ( zs ); \
290 zs->state_in_ch = zchh; \
291 zs->state_in_len = 1; \
293 zs->state_in_len++; \
381 progress_out |= copy_output_until_stop ( s );
385 isempty_RL(s))
break;
386 prepare_new_block ( s );
390 isempty_RL(s))
break;
394 progress_in |= copy_input_until_stop ( s );
413 return progress_in || progress_out;
435 progress = handle_compress ( strm );
457 progress = handle_compress ( strm );
467 progress = handle_compress ( strm );
516 if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
517 if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
527 strm->total_in_lo32 = 0;
528 strm->total_in_hi32 = 0;
529 strm->total_out_lo32 = 0;
530 strm->total_out_hi32 = 0;
579 if (k1 != s->
k0) { s->
k0 = k1;
continue; };
585 if (k1 != s->
k0) { s->
k0 = k1;
continue; };
591 if (k1 != s->
k0) { s->
k0 = k1;
continue; };
615 UInt32 avail_out_INIT = cs_avail_out;
617 unsigned int total_out_lo32_old;
622 if (c_state_out_len > 0) {
624 if (cs_avail_out == 0)
goto return_notr;
625 if (c_state_out_len == 1)
break;
626 *( (
UChar*)(cs_next_out) ) = c_state_out_ch;
632 s_state_out_len_eq_one:
634 if (cs_avail_out == 0) {
635 c_state_out_len = 1;
goto return_notr;
637 *( (
UChar*)(cs_next_out) ) = c_state_out_ch;
644 if (c_nblock_used > s_save_nblockPP)
648 if (c_nblock_used == s_save_nblockPP) {
649 c_state_out_len = 0;
goto return_notr;
651 c_state_out_ch = c_k0;
654 c_k0 = k1;
goto s_state_out_len_eq_one;
656 if (c_nblock_used == s_save_nblockPP)
657 goto s_state_out_len_eq_one;
661 if (c_nblock_used == s_save_nblockPP)
continue;
662 if (k1 != c_k0) { c_k0 = k1;
continue; };
666 if (c_nblock_used == s_save_nblockPP)
continue;
667 if (k1 != c_k0) { c_k0 = k1;
continue; };
670 c_state_out_len = ((
Int32)k1) + 4;
704 mid = (nb + na) >> 1;
705 if (indx >= cftab[mid]) nb = mid;
else na = mid;
707 while (na - nb != 1);
749 if (k1 != s->
k0) { s->
k0 = k1;
continue; };
755 if (k1 != s->
k0) { s->
k0 = k1;
continue; };
761 if (k1 != s->
k0) { s->
k0 = k1;
continue; };
797 if (k1 != s->
k0) { s->
k0 = k1;
continue; };
802 if (k1 != s->
k0) { s->
k0 = k1;
continue; };
807 if (k1 != s->
k0) { s->
k0 = k1;
continue; };
832 corrupt = unRLE_obuf_to_output_SMALL ( s );
else
833 corrupt = unRLE_obuf_to_output_FAST ( s );
856 VPrintf2 (
"\n combined CRCs: stored = 0x%08x, computed = 0x%08x",
897#define BZ_SETERR(eee) \
899 if (bzerror != NULL) *bzerror = eee; \
900 if (bzf != NULL) bzf->lastErr = eee; \
917static Bool myfeof ( FILE* f )
919 Int32 c = fgetc ( f );
920 if (c == EOF)
return True;
940 (blockSize100k < 1 || blockSize100k > 9) ||
941 (workFactor < 0 || workFactor > 250) ||
942 (verbosity < 0 || verbosity > 4))
961 if (workFactor == 0) workFactor = 30;
963 verbosity, workFactor );
985 if (bzf == NULL || buf == NULL || len < 0)
1007 n2 = fwrite ( (
void*)(bzf->
buf),
sizeof(
UChar),
1009 if (n != n2 || ferror(bzf->
handle))
1024 unsigned int* nbytes_in,
1025 unsigned int* nbytes_out )
1028 nbytes_in, NULL, nbytes_out, NULL );
1036 unsigned int* nbytes_in_lo32,
1037 unsigned int* nbytes_in_hi32,
1038 unsigned int* nbytes_out_lo32,
1039 unsigned int* nbytes_out_hi32 )
1051 if (nbytes_in_lo32 != NULL) *nbytes_in_lo32 = 0;
1052 if (nbytes_in_hi32 != NULL) *nbytes_in_hi32 = 0;
1053 if (nbytes_out_lo32 != NULL) *nbytes_out_lo32 = 0;
1054 if (nbytes_out_hi32 != NULL) *nbytes_out_hi32 = 0;
1066 n2 = fwrite ( (
void*)(bzf->
buf),
sizeof(
UChar),
1068 if (n != n2 || ferror(bzf->
handle))
1076 if ( !abandon && !ferror ( bzf->
handle ) ) {
1082 if (nbytes_in_lo32 != NULL)
1084 if (nbytes_in_hi32 != NULL)
1086 if (nbytes_out_lo32 != NULL)
1088 if (nbytes_out_hi32 != NULL)
1112 (small != 0 && small != 1) ||
1113 (verbosity < 0 || verbosity > 4) ||
1114 (unused == NULL && nUnused != 0) ||
1115 (unused != NULL && (nUnused < 0 || nUnused >
BZ_MAX_UNUSED)))
1135 while (nUnused > 0) {
1137 unused = ((
void*)( 1 + ((
UChar*)(unused)) ));
1183 if (bzf == NULL || buf == NULL || len < 0)
1201 n = fread ( bzf->
buf,
sizeof(
UChar),
1243 if (unused == NULL || nUnused == NULL)
1260 unsigned int* destLen,
1262 unsigned int sourceLen,
1270 if (dest == NULL || destLen == NULL ||
1272 blockSize100k < 1 || blockSize100k > 9 ||
1273 verbosity < 0 || verbosity > 4 ||
1274 workFactor < 0 || workFactor > 250)
1277 if (workFactor == 0) workFactor = 30;
1282 verbosity, workFactor );
1283 if (ret !=
BZ_OK)
return ret;
1312 unsigned int* destLen,
1314 unsigned int sourceLen,
1321 if (dest == NULL || destLen == NULL ||
1323 (small != 0 && small != 1) ||
1324 verbosity < 0 || verbosity > 4)
1331 if (ret !=
BZ_OK)
return ret;
1339 if (ret ==
BZ_OK)
goto output_overflow_or_eof;
1347 output_overflow_or_eof:
1386#if defined(_WIN32) || defined(OS2) || defined(MSDOS)
1389# define SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
1391# define SET_BINARY_MODE(file)
1394BZFILE * bzopen_or_bzdopen
1402 int blockSize100k = 9;
1404 char mode2[10] =
"";
1408 int workFactor = 30;
1412 if (
mode == NULL)
return NULL;
1420 smallMode = 1;
break;
1422 if (isdigit((
int)(*
mode))) {
1428 strcat(mode2, writing ?
"w" :
"r" );
1432 if (path==NULL ||
strcmp(path,
"")==0) {
1433 fp = (writing ? stdout : stdin);
1436 fp = fopen(path,mode2);
1439#ifdef BZ_STRICT_ANSI
1442 fp = (FILE *)fdopen(fd,mode2);
1445 if (fp == NULL)
return NULL;
1449 if (blockSize100k < 1) blockSize100k = 1;
1450 if (blockSize100k > 9) blockSize100k = 9;
1452 verbosity,workFactor);
1458 if (fp != stdin && fp != stdout) fclose(fp);
1475 return bzopen_or_bzdopen(path,-1,
mode,0);
1484 return bzopen_or_bzdopen(NULL,fd,
mode,1);
1530 if (b==NULL) {
return;}
1531 fp = ((
bzFile *)b)->handle;
1532 if(((
bzFile*)b)->writing){
1540 if(fp!=stdin && fp!=stdout){
1550static const char *bzerrorstrings[] = {
1572 int err = ((
bzFile *)b)->lastErr;
1576 return bzerrorstrings[err*-1];
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
void BZ_API BZ2_bzWriteClose64(int *bzerror, BZFILE *b, int abandon, unsigned int *nbytes_in_lo32, unsigned int *nbytes_in_hi32, unsigned int *nbytes_out_lo32, unsigned int *nbytes_out_hi32)
BZFILE *BZ_API BZ2_bzWriteOpen(int *bzerror, FILE *f, int blockSize100k, int verbosity, int workFactor)
void BZ_API BZ2_bzReadGetUnused(int *bzerror, BZFILE *b, void **unused, int *nUnused)
BZFILE *BZ_API BZ2_bzdopen(int fd, const char *mode)
const char *BZ_API BZ2_bzlibVersion(void)
int BZ_API BZ2_bzwrite(BZFILE *b, void *buf, int len)
int BZ_API BZ2_bzCompressInit(bz_stream *strm, int blockSize100k, int verbosity, int workFactor)
int BZ_API BZ2_bzDecompressInit(bz_stream *strm, int verbosity, int small)
void BZ_API BZ2_bzReadClose(int *bzerror, BZFILE *b)
BZFILE *BZ_API BZ2_bzReadOpen(int *bzerror, FILE *f, int verbosity, int small, void *unused, int nUnused)
int BZ_API BZ2_bzflush(BZFILE *b)
Int32 BZ2_indexIntoF(Int32 indx, Int32 *cftab)
int BZ_API BZ2_bzBuffToBuffDecompress(char *dest, unsigned int *destLen, char *source, unsigned int sourceLen, int small, int verbosity)
ABC_NAMESPACE_IMPL_START void BZ2_bz__AssertH__fail(int errcode)
#define SET_BINARY_MODE(file)
void BZ_API BZ2_bzWrite(int *bzerror, BZFILE *b, void *buf, int len)
int BZ_API BZ2_bzCompressEnd(bz_stream *strm)
int BZ_API BZ2_bzDecompress(bz_stream *strm)
int BZ_API BZ2_bzCompress(bz_stream *strm, int action)
void BZ_API BZ2_bzWriteClose(int *bzerror, BZFILE *b, int abandon, unsigned int *nbytes_in, unsigned int *nbytes_out)
#define ADD_CHAR_TO_BLOCK(zs, zchh0)
int BZ_API BZ2_bzread(BZFILE *b, void *buf, int len)
int BZ_API BZ2_bzBuffToBuffCompress(char *dest, unsigned int *destLen, char *source, unsigned int sourceLen, int blockSize100k, int verbosity, int workFactor)
BZFILE *BZ_API BZ2_bzopen(const char *path, const char *mode)
int BZ_API BZ2_bzDecompressEnd(bz_stream *strm)
int BZ_API BZ2_bzRead(int *bzerror, BZFILE *b, void *buf, int len)
const char *BZ_API BZ2_bzerror(BZFILE *b, int *errnum)
void BZ_API BZ2_bzclose(BZFILE *b)
#define BZ_UNEXPECTED_EOF
#define BZ_SEQUENCE_ERROR
#define BZ_GET_FAST(cccc)
void BZ2_compressBlock(EState *, Bool)
#define BZ_UPDATE_CRC(crcVar, cha)
#define BZ_GET_SMALL(cccc)
#define VPrintf2(zf, za1, za2)
#define BZ_GET_FAST_C(cccc)
#define AssertH(cond, errcode)
#define BZ_INITIALISE_CRC(crcVar)
Int32 BZ2_decompress(DState *)
#define BZ_FINALISE_CRC(crcVar)
UInt32 calculatedBlockCRC
UInt32 calculatedCombinedCRC
void(* bzfree)(void *, void *)
void *(* bzalloc)(void *, int, int)
unsigned int total_out_lo32
unsigned int total_out_hi32
unsigned int total_in_lo32
unsigned int total_in_hi32