bzip2: size reduction, to just below 9k.

This commit is contained in:
Denis Vlasenko
2007-10-14 00:43:01 +00:00
parent 77f1ec1b9b
commit ef3aabe906
7 changed files with 376 additions and 457 deletions

View File

@@ -40,25 +40,27 @@ in the file LICENSE.
/*---------------------------------------------------*/
/*---------------------------------------------------*/
#ifndef BZ_NO_STDIO
static void bz_assert_fail(int errcode)
#if BZ_LIGHT_DEBUG
static
void bz_assert_fail(int errcode)
{
/* if (errcode == 1007) bb_error_msg_and_die("probably bad RAM"); */
bb_error_msg_and_die("bzip2 internal error %d", errcode);
bb_error_msg_and_die("internal error %d", errcode);
}
#endif
/*---------------------------------------------------*/
static
void prepare_new_block(EState* s)
{
int32_t i;
int i;
s->nblock = 0;
s->numZ = 0;
s->state_out_pos = 0;
BZ_INITIALISE_CRC(s->blockCRC);
for (i = 0; i < 256; i++) s->inUse[i] = False;
/* inlined memset would be nice to have here */
for (i = 0; i < 256; i++)
s->inUse[i] = 0;
s->blockNo++;
}
@@ -97,9 +99,11 @@ void BZ2_bzCompressInit(bz_stream *strm, int blockSize100k)
s->mtfv = (uint16_t*)s->arr1;
s->ptr = (uint32_t*)s->arr1;
s->arr2 = xmalloc((n + BZ_N_OVERSHOOT) * sizeof(uint32_t));
s->block = (UChar*)s->arr2;
s->block = (uint8_t*)s->arr2;
s->ftab = xmalloc(65537 * sizeof(uint32_t));
s->crc32table = crc32_filltable(NULL, 1);
s->state = BZ_S_INPUT;
s->mode = BZ_M_RUNNING;
s->blockSize100k = blockSize100k;
@@ -107,7 +111,7 @@ void BZ2_bzCompressInit(bz_stream *strm, int blockSize100k)
strm->state = s;
/*strm->total_in = 0;*/
strm->total_out = 0;
strm->total_out = 0;
init_RL(s);
prepare_new_block(s);
}
@@ -118,31 +122,28 @@ static
void add_pair_to_block(EState* s)
{
int32_t i;
UChar ch = (UChar)(s->state_in_ch);
uint8_t ch = (uint8_t)(s->state_in_ch);
for (i = 0; i < s->state_in_len; i++) {
BZ_UPDATE_CRC(s->blockCRC, ch);
BZ_UPDATE_CRC(s, s->blockCRC, ch);
}
s->inUse[s->state_in_ch] = True;
s->inUse[s->state_in_ch] = 1;
switch (s->state_in_len) {
case 1:
s->block[s->nblock] = (UChar)ch; s->nblock++;
break;
case 2:
s->block[s->nblock] = (UChar)ch; s->nblock++;
s->block[s->nblock] = (UChar)ch; s->nblock++;
break;
case 3:
s->block[s->nblock] = (UChar)ch; s->nblock++;
s->block[s->nblock] = (UChar)ch; s->nblock++;
s->block[s->nblock] = (UChar)ch; s->nblock++;
s->block[s->nblock] = (uint8_t)ch; s->nblock++;
/* fall through */
case 2:
s->block[s->nblock] = (uint8_t)ch; s->nblock++;
/* fall through */
case 1:
s->block[s->nblock] = (uint8_t)ch; s->nblock++;
break;
default:
s->inUse[s->state_in_len-4] = True;
s->block[s->nblock] = (UChar)ch; s->nblock++;
s->block[s->nblock] = (UChar)ch; s->nblock++;
s->block[s->nblock] = (UChar)ch; s->nblock++;
s->block[s->nblock] = (UChar)ch; s->nblock++;
s->block[s->nblock] = ((UChar)(s->state_in_len-4));
s->inUse[s->state_in_len - 4] = 1;
s->block[s->nblock] = (uint8_t)ch; s->nblock++;
s->block[s->nblock] = (uint8_t)ch; s->nblock++;
s->block[s->nblock] = (uint8_t)ch; s->nblock++;
s->block[s->nblock] = (uint8_t)ch; s->nblock++;
s->block[s->nblock] = (uint8_t)(s->state_in_len - 4);
s->nblock++;
break;
}
@@ -164,17 +165,16 @@ void flush_RL(EState* s)
uint32_t zchh = (uint32_t)(zchh0); \
/*-- fast track the common case --*/ \
if (zchh != zs->state_in_ch && zs->state_in_len == 1) { \
UChar ch = (UChar)(zs->state_in_ch); \
BZ_UPDATE_CRC(zs->blockCRC, ch); \
zs->inUse[zs->state_in_ch] = True; \
zs->block[zs->nblock] = (UChar)ch; \
uint8_t ch = (uint8_t)(zs->state_in_ch); \
BZ_UPDATE_CRC(zs, zs->blockCRC, ch); \
zs->inUse[zs->state_in_ch] = 1; \
zs->block[zs->nblock] = (uint8_t)ch; \
zs->nblock++; \
zs->state_in_ch = zchh; \
} \
else \
/*-- general, uncommon cases --*/ \
if (zchh != zs->state_in_ch || \
zs->state_in_len == 255) { \
if (zchh != zs->state_in_ch || zs->state_in_len == 255) { \
if (zs->state_in_ch < 256) \
add_pair_to_block(zs); \
zs->state_in_ch = zchh; \
@@ -187,114 +187,117 @@ void flush_RL(EState* s)
/*---------------------------------------------------*/
static
Bool copy_input_until_stop(EState* s)
void /*Bool*/ copy_input_until_stop(EState* s)
{
Bool progress_in = False;
/*Bool progress_in = False;*/
//vda: cannot simplify this until avail_in_expect is removed
#ifdef SAME_CODE_AS_BELOW
if (s->mode == BZ_M_RUNNING) {
/*-- fast track the common case --*/
while (1) {
/*-- block full? --*/
if (s->nblock >= s->nblockMAX) break;
/*-- no input? --*/
if (s->strm->avail_in == 0) break;
progress_in = True;
ADD_CHAR_TO_BLOCK(s, (uint32_t)(*((UChar*)(s->strm->next_in))));
/*-- block full? --*/
if (s->nblock >= s->nblockMAX) break;
/*progress_in = True;*/
ADD_CHAR_TO_BLOCK(s, (uint32_t)(*(uint8_t*)(s->strm->next_in)));
s->strm->next_in++;
s->strm->avail_in--;
/*s->strm->total_in++;*/
}
} else {
} else
#endif
{
/*-- general, uncommon case --*/
while (1) {
/*-- block full? --*/
if (s->nblock >= s->nblockMAX) break;
/*-- no input? --*/
if (s->strm->avail_in == 0) break;
/*-- flush/finish end? --*/
if (s->avail_in_expect == 0) break;
progress_in = True;
ADD_CHAR_TO_BLOCK(s, (uint32_t)(*((UChar*)(s->strm->next_in))));
/*-- block full? --*/
if (s->nblock >= s->nblockMAX) break;
//# /*-- flush/finish end? --*/
//# if (s->avail_in_expect == 0) break;
/*progress_in = True;*/
ADD_CHAR_TO_BLOCK(s, *(uint8_t*)(s->strm->next_in));
s->strm->next_in++;
s->strm->avail_in--;
/*s->strm->total_in++;*/
s->avail_in_expect--;
//# s->avail_in_expect--;
}
}
return progress_in;
/*return progress_in;*/
}
/*---------------------------------------------------*/
static
Bool copy_output_until_stop(EState* s)
void /*Bool*/ copy_output_until_stop(EState* s)
{
Bool progress_out = False;
/*Bool progress_out = False;*/
while (1) {
/*-- no output space? --*/
if (s->strm->avail_out == 0) break;
/*-- block done? --*/
if (s->state_out_pos >= s->numZ) break;
progress_out = True;
/*progress_out = True;*/
*(s->strm->next_out) = s->zbits[s->state_out_pos];
s->state_out_pos++;
s->strm->avail_out--;
s->strm->next_out++;
s->strm->total_out++;
}
return progress_out;
/*return progress_out;*/
}
/*---------------------------------------------------*/
static
Bool handle_compress(bz_stream *strm)
void /*Bool*/ handle_compress(bz_stream *strm)
{
Bool progress_in = False;
Bool progress_out = False;
/*Bool progress_in = False;*/
/*Bool progress_out = False;*/
EState* s = strm->state;
while (1) {
if (s->state == BZ_S_OUTPUT) {
progress_out |= copy_output_until_stop(s);
/*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
//# && s->avail_in_expect == 0
&& s->strm->avail_in == 0
&& isempty_RL(s))
break;
prepare_new_block(s);
s->state = BZ_S_INPUT;
#ifdef FLUSH_IS_UNUSED
if (s->mode == BZ_M_FLUSHING
&& s->avail_in_expect == 0
&& isempty_RL(s))
break;
#endif
}
if (s->state == BZ_S_INPUT) {
progress_in |= copy_input_until_stop(s);
if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) {
/*progress_in |=*/ copy_input_until_stop(s);
//#if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) {
if (s->mode != BZ_M_RUNNING && s->strm->avail_in == 0) {
flush_RL(s);
BZ2_compressBlock(s, (Bool)(s->mode == BZ_M_FINISHING));
BZ2_compressBlock(s, (s->mode == BZ_M_FINISHING));
s->state = BZ_S_OUTPUT;
} else
if (s->nblock >= s->nblockMAX) {
BZ2_compressBlock(s, False);
BZ2_compressBlock(s, 0);
s->state = BZ_S_OUTPUT;
} else
if (s->strm->avail_in == 0) {
break;
}
}
}
return progress_in || progress_out;
/*return progress_in || progress_out;*/
}
@@ -302,82 +305,75 @@ Bool handle_compress(bz_stream *strm)
static
int BZ2_bzCompress(bz_stream *strm, int action)
{
Bool progress;
/*Bool progress;*/
EState* s;
if (strm == NULL) return BZ_PARAM_ERROR;
s = strm->state;
if (s == NULL) return BZ_PARAM_ERROR;
if (s->strm != strm) return BZ_PARAM_ERROR;
preswitch:
switch (s->mode) {
case BZ_M_IDLE:
return BZ_SEQUENCE_ERROR;
case BZ_M_RUNNING:
if (action == BZ_RUN) {
progress = handle_compress(strm);
return progress ? BZ_RUN_OK : BZ_PARAM_ERROR;
/*progress =*/ handle_compress(strm);
/*return progress ? BZ_RUN_OK : BZ_PARAM_ERROR;*/
return BZ_RUN_OK;
}
#ifdef FLUSH_IS_UNUSED
else
if (action == BZ_FLUSH) {
s->avail_in_expect = strm->avail_in;
//#s->avail_in_expect = strm->avail_in;
s->mode = BZ_M_FLUSHING;
goto preswitch;
goto case_BZ_M_FLUSHING;
}
#endif
else
if (action == BZ_FINISH) {
s->avail_in_expect = strm->avail_in;
/*if (action == BZ_FINISH)*/ {
//#s->avail_in_expect = strm->avail_in;
s->mode = BZ_M_FINISHING;
goto preswitch;
goto case_BZ_M_FINISHING;
}
else
return BZ_PARAM_ERROR;
#ifdef FLUSH_IS_UNUSED
case_BZ_M_FLUSHING:
case BZ_M_FLUSHING:
if (action != BZ_FLUSH) return BZ_SEQUENCE_ERROR;
if (s->avail_in_expect != s->strm->avail_in)
return BZ_SEQUENCE_ERROR;
progress = handle_compress(strm);
/*if (s->avail_in_expect != s->strm->avail_in)
return BZ_SEQUENCE_ERROR;*/
/*progress =*/ handle_compress(strm);
if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->numZ)
return BZ_FLUSH_OK;
s->mode = BZ_M_RUNNING;
return BZ_RUN_OK;
#endif
case BZ_M_FINISHING:
if (action != BZ_FINISH) return BZ_SEQUENCE_ERROR;
if (s->avail_in_expect != s->strm->avail_in)
return BZ_SEQUENCE_ERROR;
progress = handle_compress(strm);
if (!progress) return BZ_SEQUENCE_ERROR;
if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->numZ)
case_BZ_M_FINISHING:
/*case BZ_M_FINISHING:*/
default:
/*if (s->avail_in_expect != s->strm->avail_in)
return BZ_SEQUENCE_ERROR;*/
/*progress =*/ handle_compress(strm);
/*if (!progress) return BZ_SEQUENCE_ERROR;*/
//#if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->numZ)
//# return BZ_FINISH_OK;
if (s->strm->avail_in > 0 || !isempty_RL(s) || s->state_out_pos < s->numZ)
return BZ_FINISH_OK;
s->mode = BZ_M_IDLE;
/*s->mode = BZ_M_IDLE;*/
return BZ_STREAM_END;
}
return BZ_OK; /*--not reached--*/
/* return BZ_OK; --not reached--*/
}
/*---------------------------------------------------*/
static
int BZ2_bzCompressEnd(bz_stream *strm)
void BZ2_bzCompressEnd(bz_stream *strm)
{
EState* s;
if (strm == NULL) return BZ_PARAM_ERROR;
s = strm->state;
if (s == NULL) return BZ_PARAM_ERROR;
if (s->strm != strm) return BZ_PARAM_ERROR;
if (s->arr1 != NULL) free(s->arr1);
if (s->arr2 != NULL) free(s->arr2);
if (s->ftab != NULL) free(s->ftab);
free(s->arr1);
free(s->arr2);
free(s->ftab);
free(s->crc32table);
free(strm->state);
strm->state = NULL;
return BZ_OK;
}
@@ -418,11 +414,11 @@ int BZ2_bzBuffToBuffCompress(char* dest,
BZ2_bzCompressEnd(&strm);
return BZ_OK;
output_overflow:
output_overflow:
BZ2_bzCompressEnd(&strm);
return BZ_OUTBUFF_FULL;
errhandler:
errhandler:
BZ2_bzCompressEnd(&strm);
return ret;
}