56 #define INT_MAX 0x7fffffff
62 #define MAX_HUFCODE_BITS 20
63 #define MAX_SYMBOLS 258
69 #define RETVAL_LAST_BLOCK (-1)
70 #define RETVAL_NOT_BZIP_DATA (-2)
71 #define RETVAL_UNEXPECTED_INPUT_EOF (-3)
72 #define RETVAL_UNEXPECTED_OUTPUT_EOF (-4)
73 #define RETVAL_DATA_ERROR (-5)
74 #define RETVAL_OUT_OF_MEMORY (-6)
75 #define RETVAL_OBSOLETE_INPUT (-7)
78 #define BZIP2_IOBUF_SIZE 4096
115 static unsigned int INIT get_bits(
struct bunzip_data *bd,
char bits_wanted)
117 unsigned int bits = 0;
139 bits <<= bits_wanted;
160 int dbufCount, nextSym, dbufSize, groupCount,
selector,
161 i,
j,
k,
t, runPos, symCount, symTotal, nSelectors, *byteCount;
162 unsigned char uc, *symToByte, *mtfSymbol, *selectors;
163 unsigned int *dbuf, origPtr;
174 i = get_bits(bd, 24);
175 j = get_bits(bd, 24);
177 if ((i == 0x177245) && (j == 0x385090))
179 if ((i != 0x314159) || (j != 0x265359))
186 origPtr = get_bits(bd, 24);
187 if (origPtr > dbufSize)
194 t = get_bits(bd, 16);
196 for (i = 0; i < 16; i++) {
197 if (t&(1 << (15-i))) {
198 k = get_bits(bd, 16);
199 for (j = 0; j < 16; j++)
201 symToByte[symTotal++] = (16*
i)+j;
205 groupCount = get_bits(bd, 3);
206 if (groupCount < 2 || groupCount >
MAX_GROUPS)
213 nSelectors = get_bits(bd, 15);
216 for (i = 0; i < groupCount; i++)
218 for (i = 0; i < nSelectors; i++) {
220 for (j = 0; get_bits(bd, 1); j++)
226 mtfSymbol[j] = mtfSymbol[j-1];
227 mtfSymbol[0] = selectors[
i] =
uc;
232 symCount = symTotal+2;
233 for (j = 0; j < groupCount; j++) {
245 t = get_bits(bd, 5)-1;
246 for (i = 0; i < symCount; i++) {
271 minLen = maxLen = length[0];
273 for (i = 1; i < symCount; i++) {
274 if (length[i] > maxLen)
276 else if (length[i] < minLen)
302 base = hufGroup->
base-1;
303 limit = hufGroup->
limit-1;
307 for (i = minLen; i <=
maxLen; i++) {
308 temp[
i] = limit[
i] = 0;
309 for (t = 0; t < symCount; t++)
314 for (i = 0; i < symCount; i++)
323 for (i = minLen; i <
maxLen; i++) {
335 limit[
i] = (pp << (maxLen -
i)) - 1;
337 base[i+1] = pp-(t += temp[
i]);
351 for (i = 0; i < 256; i++) {
353 mtfSymbol[
i] = (
unsigned char)i;
356 runPos = dbufCount = symCount = selector = 0;
361 if (selector >= nSelectors)
363 hufGroup = bd->
groups+selectors[selector++];
364 base = hufGroup->
base-1;
365 limit = hufGroup->
limit-1;
381 j = get_bits(bd, hufGroup->
maxLen);
390 ((1 << hufGroup->
maxLen)-1);
399 if ((i > hufGroup->
maxLen)
400 || (((unsigned)(j = (j>>(hufGroup->
maxLen-i))-base[i]))
426 t += (runPos << nextSym);
440 if (dbufCount+t >= dbufSize)
443 uc = symToByte[mtfSymbol[0]];
446 dbuf[dbufCount++] =
uc;
449 if (nextSym > symTotal)
460 if (dbufCount >= dbufSize)
470 mtfSymbol[
i] = mtfSymbol[i-1];
476 dbuf[dbufCount++] = (
unsigned int)uc;
487 for (i = 0; i < 256; i++) {
493 for (i = 0; i < dbufCount; i++) {
494 uc = (
unsigned char)(dbuf[i] & 0xff);
495 dbuf[byteCount[
uc]] |= (i << 8);
503 if (origPtr >= dbufCount)
522 static int INIT read_bunzip(
struct bunzip_data *bd,
char *outbuf,
int len)
524 const unsigned int *dbuf;
525 int pos, xcurrent, previous, gotcount;
547 if (gotcount >= len) {
554 outbuf[gotcount++] = xcurrent;
578 if (xcurrent != previous)
589 goto decode_next_byte;
609 previous = get_next_block(bd);
617 goto decode_next_byte;
620 static int INIT nofill(
void *
buf,
unsigned int len)
629 int (*
fill)(
void*,
unsigned int))
632 unsigned int i,
j,
c;
633 const unsigned int BZh0 =
634 (((
unsigned int)
'B') << 24)+(((
unsigned int)
'Z') << 16)
635 +(((
unsigned int)
'h') << 8)+(
unsigned int)
'0';
654 for (i = 0; i < 256; i++) {
657 c = c&0x80000000 ? (c << 1)^0x04c11db7 : (c << 1);
662 i = get_bits(bd, 32);
663 if (((
unsigned int)(i-BZh0-1)) >= 9)
679 int(*
fill)(
void*,
unsigned int),
680 int(*flush)(
void*,
unsigned int),
681 unsigned char *outbuf,
687 unsigned char *
inbuf;
693 error(
"Could not allocate output buffer");
701 error(
"Could not allocate input buffer");
705 i = start_bunzip(&bd, inbuf, len,
fill);
714 if (i != flush(outbuf, i)) {
723 error(
"Data integrity error when decompressing.");
727 error(
"Compressed file ends unexpectedly");
747 int(*
fill)(
void*,
unsigned int),
748 int(*flush)(
void*,
unsigned int),
749 unsigned char *outbuf,