nuclear@2: /* inflate.c -- zlib decompression nuclear@2: * Copyright (C) 1995-2005 Mark Adler nuclear@2: * For conditions of distribution and use, see copyright notice in zlib.h nuclear@2: */ nuclear@2: nuclear@2: /* nuclear@2: * Change history: nuclear@2: * nuclear@2: * 1.2.beta0 24 Nov 2002 nuclear@2: * - First version -- complete rewrite of inflate to simplify code, avoid nuclear@2: * creation of window when not needed, minimize use of window when it is nuclear@2: * needed, make inffast.c even faster, implement gzip decoding, and to nuclear@2: * improve code readability and style over the previous zlib inflate code nuclear@2: * nuclear@2: * 1.2.beta1 25 Nov 2002 nuclear@2: * - Use pointers for available input and output checking in inffast.c nuclear@2: * - Remove input and output counters in inffast.c nuclear@2: * - Change inffast.c entry and loop from avail_in >= 7 to >= 6 nuclear@2: * - Remove unnecessary second byte pull from length extra in inffast.c nuclear@2: * - Unroll direct copy to three copies per loop in inffast.c nuclear@2: * nuclear@2: * 1.2.beta2 4 Dec 2002 nuclear@2: * - Change external routine names to reduce potential conflicts nuclear@2: * - Correct filename to inffixed.h for fixed tables in inflate.c nuclear@2: * - Make hbuf[] unsigned char to match parameter type in inflate.c nuclear@2: * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset) nuclear@2: * to avoid negation problem on Alphas (64 bit) in inflate.c nuclear@2: * nuclear@2: * 1.2.beta3 22 Dec 2002 nuclear@2: * - Add comments on state->bits assertion in inffast.c nuclear@2: * - Add comments on op field in inftrees.h nuclear@2: * - Fix bug in reuse of allocated window after inflateReset() nuclear@2: * - Remove bit fields--back to byte structure for speed nuclear@2: * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths nuclear@2: * - Change post-increments to pre-increments in inflate_fast(), PPC biased? nuclear@2: * - Add compile time option, POSTINC, to use post-increments instead (Intel?) nuclear@2: * - Make MATCH copy in inflate() much faster for when inflate_fast() not used nuclear@2: * - Use local copies of stream next and avail values, as well as local bit nuclear@2: * buffer and bit count in inflate()--for speed when inflate_fast() not used nuclear@2: * nuclear@2: * 1.2.beta4 1 Jan 2003 nuclear@2: * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings nuclear@2: * - Move a comment on output buffer sizes from inffast.c to inflate.c nuclear@2: * - Add comments in inffast.c to introduce the inflate_fast() routine nuclear@2: * - Rearrange window copies in inflate_fast() for speed and simplification nuclear@2: * - Unroll last copy for window match in inflate_fast() nuclear@2: * - Use local copies of window variables in inflate_fast() for speed nuclear@2: * - Pull out common write == 0 case for speed in inflate_fast() nuclear@2: * - Make op and len in inflate_fast() unsigned for consistency nuclear@2: * - Add FAR to lcode and dcode declarations in inflate_fast() nuclear@2: * - Simplified bad distance check in inflate_fast() nuclear@2: * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new nuclear@2: * source file infback.c to provide a call-back interface to inflate for nuclear@2: * programs like gzip and unzip -- uses window as output buffer to avoid nuclear@2: * window copying nuclear@2: * nuclear@2: * 1.2.beta5 1 Jan 2003 nuclear@2: * - Improved inflateBack() interface to allow the caller to provide initial nuclear@2: * input in strm. nuclear@2: * - Fixed stored blocks bug in inflateBack() nuclear@2: * nuclear@2: * 1.2.beta6 4 Jan 2003 nuclear@2: * - Added comments in inffast.c on effectiveness of POSTINC nuclear@2: * - Typecasting all around to reduce compiler warnings nuclear@2: * - Changed loops from while (1) or do {} while (1) to for (;;), again to nuclear@2: * make compilers happy nuclear@2: * - Changed type of window in inflateBackInit() to unsigned char * nuclear@2: * nuclear@2: * 1.2.beta7 27 Jan 2003 nuclear@2: * - Changed many types to unsigned or unsigned short to avoid warnings nuclear@2: * - Added inflateCopy() function nuclear@2: * nuclear@2: * 1.2.0 9 Mar 2003 nuclear@2: * - Changed inflateBack() interface to provide separate opaque descriptors nuclear@2: * for the in() and out() functions nuclear@2: * - Changed inflateBack() argument and in_func typedef to swap the length nuclear@2: * and buffer address return values for the input function nuclear@2: * - Check next_in and next_out for Z_NULL on entry to inflate() nuclear@2: * nuclear@2: * The history for versions after 1.2.0 are in ChangeLog in zlib distribution. nuclear@2: */ nuclear@2: nuclear@2: #include "zutil.h" nuclear@2: #include "inftrees.h" nuclear@2: #include "inflate.h" nuclear@2: #include "inffast.h" nuclear@2: nuclear@2: #ifdef MAKEFIXED nuclear@2: # ifndef BUILDFIXED nuclear@2: # define BUILDFIXED nuclear@2: # endif nuclear@2: #endif nuclear@2: nuclear@2: /* function prototypes */ nuclear@2: local void fixedtables OF((struct inflate_state FAR *state)); nuclear@2: local int updatewindow OF((z_streamp strm, unsigned out)); nuclear@2: #ifdef BUILDFIXED nuclear@2: void makefixed OF((void)); nuclear@2: #endif nuclear@2: local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf, nuclear@2: unsigned len)); nuclear@2: nuclear@2: int ZEXPORT inflateReset(strm) nuclear@2: z_streamp strm; nuclear@2: { nuclear@2: struct inflate_state FAR *state; nuclear@2: nuclear@2: if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; nuclear@2: state = (struct inflate_state FAR *)strm->state; nuclear@2: strm->total_in = strm->total_out = state->total = 0; nuclear@2: strm->msg = Z_NULL; nuclear@2: strm->adler = 1; /* to support ill-conceived Java test suite */ nuclear@2: state->mode = HEAD; nuclear@2: state->last = 0; nuclear@2: state->havedict = 0; nuclear@2: state->dmax = 32768U; nuclear@2: state->head = Z_NULL; nuclear@2: state->wsize = 0; nuclear@2: state->whave = 0; nuclear@2: state->write = 0; nuclear@2: state->hold = 0; nuclear@2: state->bits = 0; nuclear@2: state->lencode = state->distcode = state->next = state->codes; nuclear@2: Tracev((stderr, "inflate: reset\n")); nuclear@2: return Z_OK; nuclear@2: } nuclear@2: nuclear@2: int ZEXPORT inflatePrime(strm, bits, value) nuclear@2: z_streamp strm; nuclear@2: int bits; nuclear@2: int value; nuclear@2: { nuclear@2: struct inflate_state FAR *state; nuclear@2: nuclear@2: if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; nuclear@2: state = (struct inflate_state FAR *)strm->state; nuclear@2: if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR; nuclear@2: value &= (1L << bits) - 1; nuclear@2: state->hold += value << state->bits; nuclear@2: state->bits += bits; nuclear@2: return Z_OK; nuclear@2: } nuclear@2: nuclear@2: int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size) nuclear@2: z_streamp strm; nuclear@2: int windowBits; nuclear@2: const char *version; nuclear@2: int stream_size; nuclear@2: { nuclear@2: struct inflate_state FAR *state; nuclear@2: nuclear@2: if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || nuclear@2: stream_size != (int)(sizeof(z_stream))) nuclear@2: return Z_VERSION_ERROR; nuclear@2: if (strm == Z_NULL) return Z_STREAM_ERROR; nuclear@2: strm->msg = Z_NULL; /* in case we return an error */ nuclear@2: if (strm->zalloc == (alloc_func)0) { nuclear@2: strm->zalloc = zcalloc; nuclear@2: strm->opaque = (voidpf)0; nuclear@2: } nuclear@2: if (strm->zfree == (free_func)0) strm->zfree = zcfree; nuclear@2: state = (struct inflate_state FAR *) nuclear@2: ZALLOC(strm, 1, sizeof(struct inflate_state)); nuclear@2: if (state == Z_NULL) return Z_MEM_ERROR; nuclear@2: Tracev((stderr, "inflate: allocated\n")); nuclear@2: strm->state = (struct internal_state FAR *)state; nuclear@2: if (windowBits < 0) { nuclear@2: state->wrap = 0; nuclear@2: windowBits = -windowBits; nuclear@2: } nuclear@2: else { nuclear@2: state->wrap = (windowBits >> 4) + 1; nuclear@2: #ifdef GUNZIP nuclear@2: if (windowBits < 48) windowBits &= 15; nuclear@2: #endif nuclear@2: } nuclear@2: if (windowBits < 8 || windowBits > 15) { nuclear@2: ZFREE(strm, state); nuclear@2: strm->state = Z_NULL; nuclear@2: return Z_STREAM_ERROR; nuclear@2: } nuclear@2: state->wbits = (unsigned)windowBits; nuclear@2: state->window = Z_NULL; nuclear@2: return inflateReset(strm); nuclear@2: } nuclear@2: nuclear@2: int ZEXPORT inflateInit_(strm, version, stream_size) nuclear@2: z_streamp strm; nuclear@2: const char *version; nuclear@2: int stream_size; nuclear@2: { nuclear@2: return inflateInit2_(strm, DEF_WBITS, version, stream_size); nuclear@2: } nuclear@2: nuclear@2: /* nuclear@2: Return state with length and distance decoding tables and index sizes set to nuclear@2: fixed code decoding. Normally this returns fixed tables from inffixed.h. nuclear@2: If BUILDFIXED is defined, then instead this routine builds the tables the nuclear@2: first time it's called, and returns those tables the first time and nuclear@2: thereafter. This reduces the size of the code by about 2K bytes, in nuclear@2: exchange for a little execution time. However, BUILDFIXED should not be nuclear@2: used for threaded applications, since the rewriting of the tables and virgin nuclear@2: may not be thread-safe. nuclear@2: */ nuclear@2: local void fixedtables(state) nuclear@2: struct inflate_state FAR *state; nuclear@2: { nuclear@2: #ifdef BUILDFIXED nuclear@2: static int virgin = 1; nuclear@2: static code *lenfix, *distfix; nuclear@2: static code fixed[544]; nuclear@2: nuclear@2: /* build fixed huffman tables if first call (may not be thread safe) */ nuclear@2: if (virgin) { nuclear@2: unsigned sym, bits; nuclear@2: static code *next; nuclear@2: nuclear@2: /* literal/length table */ nuclear@2: sym = 0; nuclear@2: while (sym < 144) state->lens[sym++] = 8; nuclear@2: while (sym < 256) state->lens[sym++] = 9; nuclear@2: while (sym < 280) state->lens[sym++] = 7; nuclear@2: while (sym < 288) state->lens[sym++] = 8; nuclear@2: next = fixed; nuclear@2: lenfix = next; nuclear@2: bits = 9; nuclear@2: inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); nuclear@2: nuclear@2: /* distance table */ nuclear@2: sym = 0; nuclear@2: while (sym < 32) state->lens[sym++] = 5; nuclear@2: distfix = next; nuclear@2: bits = 5; nuclear@2: inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); nuclear@2: nuclear@2: /* do this just once */ nuclear@2: virgin = 0; nuclear@2: } nuclear@2: #else /* !BUILDFIXED */ nuclear@2: # include "inffixed.h" nuclear@2: #endif /* BUILDFIXED */ nuclear@2: state->lencode = lenfix; nuclear@2: state->lenbits = 9; nuclear@2: state->distcode = distfix; nuclear@2: state->distbits = 5; nuclear@2: } nuclear@2: nuclear@2: #ifdef MAKEFIXED nuclear@2: #include nuclear@2: nuclear@2: /* nuclear@2: Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also nuclear@2: defines BUILDFIXED, so the tables are built on the fly. makefixed() writes nuclear@2: those tables to stdout, which would be piped to inffixed.h. A small program nuclear@2: can simply call makefixed to do this: nuclear@2: nuclear@2: void makefixed(void); nuclear@2: nuclear@2: int main(void) nuclear@2: { nuclear@2: makefixed(); nuclear@2: return 0; nuclear@2: } nuclear@2: nuclear@2: Then that can be linked with zlib built with MAKEFIXED defined and run: nuclear@2: nuclear@2: a.out > inffixed.h nuclear@2: */ nuclear@2: void makefixed() nuclear@2: { nuclear@2: unsigned low, size; nuclear@2: struct inflate_state state; nuclear@2: nuclear@2: fixedtables(&state); nuclear@2: puts(" /* inffixed.h -- table for decoding fixed codes"); nuclear@2: puts(" * Generated automatically by makefixed()."); nuclear@2: puts(" */"); nuclear@2: puts(""); nuclear@2: puts(" /* WARNING: this file should *not* be used by applications."); nuclear@2: puts(" It is part of the implementation of this library and is"); nuclear@2: puts(" subject to change. Applications should only use zlib.h."); nuclear@2: puts(" */"); nuclear@2: puts(""); nuclear@2: size = 1U << 9; nuclear@2: printf(" static const code lenfix[%u] = {", size); nuclear@2: low = 0; nuclear@2: for (;;) { nuclear@2: if ((low % 7) == 0) printf("\n "); nuclear@2: printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits, nuclear@2: state.lencode[low].val); nuclear@2: if (++low == size) break; nuclear@2: putchar(','); nuclear@2: } nuclear@2: puts("\n };"); nuclear@2: size = 1U << 5; nuclear@2: printf("\n static const code distfix[%u] = {", size); nuclear@2: low = 0; nuclear@2: for (;;) { nuclear@2: if ((low % 6) == 0) printf("\n "); nuclear@2: printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, nuclear@2: state.distcode[low].val); nuclear@2: if (++low == size) break; nuclear@2: putchar(','); nuclear@2: } nuclear@2: puts("\n };"); nuclear@2: } nuclear@2: #endif /* MAKEFIXED */ nuclear@2: nuclear@2: /* nuclear@2: Update the window with the last wsize (normally 32K) bytes written before nuclear@2: returning. If window does not exist yet, create it. This is only called nuclear@2: when a window is already in use, or when output has been written during this nuclear@2: inflate call, but the end of the deflate stream has not been reached yet. nuclear@2: It is also called to create a window for dictionary data when a dictionary nuclear@2: is loaded. nuclear@2: nuclear@2: Providing output buffers larger than 32K to inflate() should provide a speed nuclear@2: advantage, since only the last 32K of output is copied to the sliding window nuclear@2: upon return from inflate(), and since all distances after the first 32K of nuclear@2: output will fall in the output data, making match copies simpler and faster. nuclear@2: The advantage may be dependent on the size of the processor's data caches. nuclear@2: */ nuclear@2: local int updatewindow(strm, out) nuclear@2: z_streamp strm; nuclear@2: unsigned out; nuclear@2: { nuclear@2: struct inflate_state FAR *state; nuclear@2: unsigned copy, dist; nuclear@2: nuclear@2: state = (struct inflate_state FAR *)strm->state; nuclear@2: nuclear@2: /* if it hasn't been done already, allocate space for the window */ nuclear@2: if (state->window == Z_NULL) { nuclear@2: state->window = (unsigned char FAR *) nuclear@2: ZALLOC(strm, 1U << state->wbits, nuclear@2: sizeof(unsigned char)); nuclear@2: if (state->window == Z_NULL) return 1; nuclear@2: } nuclear@2: nuclear@2: /* if window not in use yet, initialize */ nuclear@2: if (state->wsize == 0) { nuclear@2: state->wsize = 1U << state->wbits; nuclear@2: state->write = 0; nuclear@2: state->whave = 0; nuclear@2: } nuclear@2: nuclear@2: /* copy state->wsize or less output bytes into the circular window */ nuclear@2: copy = out - strm->avail_out; nuclear@2: if (copy >= state->wsize) { nuclear@2: zmemcpy(state->window, strm->next_out - state->wsize, state->wsize); nuclear@2: state->write = 0; nuclear@2: state->whave = state->wsize; nuclear@2: } nuclear@2: else { nuclear@2: dist = state->wsize - state->write; nuclear@2: if (dist > copy) dist = copy; nuclear@2: zmemcpy(state->window + state->write, strm->next_out - copy, dist); nuclear@2: copy -= dist; nuclear@2: if (copy) { nuclear@2: zmemcpy(state->window, strm->next_out - copy, copy); nuclear@2: state->write = copy; nuclear@2: state->whave = state->wsize; nuclear@2: } nuclear@2: else { nuclear@2: state->write += dist; nuclear@2: if (state->write == state->wsize) state->write = 0; nuclear@2: if (state->whave < state->wsize) state->whave += dist; nuclear@2: } nuclear@2: } nuclear@2: return 0; nuclear@2: } nuclear@2: nuclear@2: /* Macros for inflate(): */ nuclear@2: nuclear@2: /* check function to use adler32() for zlib or crc32() for gzip */ nuclear@2: #ifdef GUNZIP nuclear@2: # define UPDATE(check, buf, len) \ nuclear@2: (state->flags ? crc32(check, buf, len) : adler32(check, buf, len)) nuclear@2: #else nuclear@2: # define UPDATE(check, buf, len) adler32(check, buf, len) nuclear@2: #endif nuclear@2: nuclear@2: /* check macros for header crc */ nuclear@2: #ifdef GUNZIP nuclear@2: # define CRC2(check, word) \ nuclear@2: do { \ nuclear@2: hbuf[0] = (unsigned char)(word); \ nuclear@2: hbuf[1] = (unsigned char)((word) >> 8); \ nuclear@2: check = crc32(check, hbuf, 2); \ nuclear@2: } while (0) nuclear@2: nuclear@2: # define CRC4(check, word) \ nuclear@2: do { \ nuclear@2: hbuf[0] = (unsigned char)(word); \ nuclear@2: hbuf[1] = (unsigned char)((word) >> 8); \ nuclear@2: hbuf[2] = (unsigned char)((word) >> 16); \ nuclear@2: hbuf[3] = (unsigned char)((word) >> 24); \ nuclear@2: check = crc32(check, hbuf, 4); \ nuclear@2: } while (0) nuclear@2: #endif nuclear@2: nuclear@2: /* Load registers with state in inflate() for speed */ nuclear@2: #define LOAD() \ nuclear@2: do { \ nuclear@2: put = strm->next_out; \ nuclear@2: left = strm->avail_out; \ nuclear@2: next = strm->next_in; \ nuclear@2: have = strm->avail_in; \ nuclear@2: hold = state->hold; \ nuclear@2: bits = state->bits; \ nuclear@2: } while (0) nuclear@2: nuclear@2: /* Restore state from registers in inflate() */ nuclear@2: #define RESTORE() \ nuclear@2: do { \ nuclear@2: strm->next_out = put; \ nuclear@2: strm->avail_out = left; \ nuclear@2: strm->next_in = next; \ nuclear@2: strm->avail_in = have; \ nuclear@2: state->hold = hold; \ nuclear@2: state->bits = bits; \ nuclear@2: } while (0) nuclear@2: nuclear@2: /* Clear the input bit accumulator */ nuclear@2: #define INITBITS() \ nuclear@2: do { \ nuclear@2: hold = 0; \ nuclear@2: bits = 0; \ nuclear@2: } while (0) nuclear@2: nuclear@2: /* Get a byte of input into the bit accumulator, or return from inflate() nuclear@2: if there is no input available. */ nuclear@2: #define PULLBYTE() \ nuclear@2: do { \ nuclear@2: if (have == 0) goto inf_leave; \ nuclear@2: have--; \ nuclear@2: hold += (unsigned long)(*next++) << bits; \ nuclear@2: bits += 8; \ nuclear@2: } while (0) nuclear@2: nuclear@2: /* Assure that there are at least n bits in the bit accumulator. If there is nuclear@2: not enough available input to do that, then return from inflate(). */ nuclear@2: #define NEEDBITS(n) \ nuclear@2: do { \ nuclear@2: while (bits < (unsigned)(n)) \ nuclear@2: PULLBYTE(); \ nuclear@2: } while (0) nuclear@2: nuclear@2: /* Return the low n bits of the bit accumulator (n < 16) */ nuclear@2: #define BITS(n) \ nuclear@2: ((unsigned)hold & ((1U << (n)) - 1)) nuclear@2: nuclear@2: /* Remove n bits from the bit accumulator */ nuclear@2: #define DROPBITS(n) \ nuclear@2: do { \ nuclear@2: hold >>= (n); \ nuclear@2: bits -= (unsigned)(n); \ nuclear@2: } while (0) nuclear@2: nuclear@2: /* Remove zero to seven bits as needed to go to a byte boundary */ nuclear@2: #define BYTEBITS() \ nuclear@2: do { \ nuclear@2: hold >>= bits & 7; \ nuclear@2: bits -= bits & 7; \ nuclear@2: } while (0) nuclear@2: nuclear@2: /* Reverse the bytes in a 32-bit value */ nuclear@2: #define REVERSE(q) \ nuclear@2: ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ nuclear@2: (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) nuclear@2: nuclear@2: /* nuclear@2: inflate() uses a state machine to process as much input data and generate as nuclear@2: much output data as possible before returning. The state machine is nuclear@2: structured roughly as follows: nuclear@2: nuclear@2: for (;;) switch (state) { nuclear@2: ... nuclear@2: case STATEn: nuclear@2: if (not enough input data or output space to make progress) nuclear@2: return; nuclear@2: ... make progress ... nuclear@2: state = STATEm; nuclear@2: break; nuclear@2: ... nuclear@2: } nuclear@2: nuclear@2: so when inflate() is called again, the same case is attempted again, and nuclear@2: if the appropriate resources are provided, the machine proceeds to the nuclear@2: next state. The NEEDBITS() macro is usually the way the state evaluates nuclear@2: whether it can proceed or should return. NEEDBITS() does the return if nuclear@2: the requested bits are not available. The typical use of the BITS macros nuclear@2: is: nuclear@2: nuclear@2: NEEDBITS(n); nuclear@2: ... do something with BITS(n) ... nuclear@2: DROPBITS(n); nuclear@2: nuclear@2: where NEEDBITS(n) either returns from inflate() if there isn't enough nuclear@2: input left to load n bits into the accumulator, or it continues. BITS(n) nuclear@2: gives the low n bits in the accumulator. When done, DROPBITS(n) drops nuclear@2: the low n bits off the accumulator. INITBITS() clears the accumulator nuclear@2: and sets the number of available bits to zero. BYTEBITS() discards just nuclear@2: enough bits to put the accumulator on a byte boundary. After BYTEBITS() nuclear@2: and a NEEDBITS(8), then BITS(8) would return the next byte in the stream. nuclear@2: nuclear@2: NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return nuclear@2: if there is no input available. The decoding of variable length codes uses nuclear@2: PULLBYTE() directly in order to pull just enough bytes to decode the next nuclear@2: code, and no more. nuclear@2: nuclear@2: Some states loop until they get enough input, making sure that enough nuclear@2: state information is maintained to continue the loop where it left off nuclear@2: if NEEDBITS() returns in the loop. For example, want, need, and keep nuclear@2: would all have to actually be part of the saved state in case NEEDBITS() nuclear@2: returns: nuclear@2: nuclear@2: case STATEw: nuclear@2: while (want < need) { nuclear@2: NEEDBITS(n); nuclear@2: keep[want++] = BITS(n); nuclear@2: DROPBITS(n); nuclear@2: } nuclear@2: state = STATEx; nuclear@2: case STATEx: nuclear@2: nuclear@2: As shown above, if the next state is also the next case, then the break nuclear@2: is omitted. nuclear@2: nuclear@2: A state may also return if there is not enough output space available to nuclear@2: complete that state. Those states are copying stored data, writing a nuclear@2: literal byte, and copying a matching string. nuclear@2: nuclear@2: When returning, a "goto inf_leave" is used to update the total counters, nuclear@2: update the check value, and determine whether any progress has been made nuclear@2: during that inflate() call in order to return the proper return code. nuclear@2: Progress is defined as a change in either strm->avail_in or strm->avail_out. nuclear@2: When there is a window, goto inf_leave will update the window with the last nuclear@2: output written. If a goto inf_leave occurs in the middle of decompression nuclear@2: and there is no window currently, goto inf_leave will create one and copy nuclear@2: output to the window for the next call of inflate(). nuclear@2: nuclear@2: In this implementation, the flush parameter of inflate() only affects the nuclear@2: return code (per zlib.h). inflate() always writes as much as possible to nuclear@2: strm->next_out, given the space available and the provided input--the effect nuclear@2: documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers nuclear@2: the allocation of and copying into a sliding window until necessary, which nuclear@2: provides the effect documented in zlib.h for Z_FINISH when the entire input nuclear@2: stream available. So the only thing the flush parameter actually does is: nuclear@2: when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it nuclear@2: will return Z_BUF_ERROR if it has not reached the end of the stream. nuclear@2: */ nuclear@2: nuclear@2: int ZEXPORT inflate(strm, flush) nuclear@2: z_streamp strm; nuclear@2: int flush; nuclear@2: { nuclear@2: struct inflate_state FAR *state; nuclear@2: unsigned char FAR *next; /* next input */ nuclear@2: unsigned char FAR *put; /* next output */ nuclear@2: unsigned have, left; /* available input and output */ nuclear@2: unsigned long hold; /* bit buffer */ nuclear@2: unsigned bits; /* bits in bit buffer */ nuclear@2: unsigned in, out; /* save starting available input and output */ nuclear@2: unsigned copy; /* number of stored or match bytes to copy */ nuclear@2: unsigned char FAR *from; /* where to copy match bytes from */ nuclear@2: code this; /* current decoding table entry */ nuclear@2: code last; /* parent table entry */ nuclear@2: unsigned len; /* length to copy for repeats, bits to drop */ nuclear@2: int ret; /* return code */ nuclear@2: #ifdef GUNZIP nuclear@2: unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ nuclear@2: #endif nuclear@2: static const unsigned short order[19] = /* permutation of code lengths */ nuclear@2: {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; nuclear@2: nuclear@2: if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL || nuclear@2: (strm->next_in == Z_NULL && strm->avail_in != 0)) nuclear@2: return Z_STREAM_ERROR; nuclear@2: nuclear@2: state = (struct inflate_state FAR *)strm->state; nuclear@2: if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ nuclear@2: LOAD(); nuclear@2: in = have; nuclear@2: out = left; nuclear@2: ret = Z_OK; nuclear@2: for (;;) nuclear@2: switch (state->mode) { nuclear@2: case HEAD: nuclear@2: if (state->wrap == 0) { nuclear@2: state->mode = TYPEDO; nuclear@2: break; nuclear@2: } nuclear@2: NEEDBITS(16); nuclear@2: #ifdef GUNZIP nuclear@2: if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ nuclear@2: state->check = crc32(0L, Z_NULL, 0); nuclear@2: CRC2(state->check, hold); nuclear@2: INITBITS(); nuclear@2: state->mode = FLAGS; nuclear@2: break; nuclear@2: } nuclear@2: state->flags = 0; /* expect zlib header */ nuclear@2: if (state->head != Z_NULL) nuclear@2: state->head->done = -1; nuclear@2: if (!(state->wrap & 1) || /* check if zlib header allowed */ nuclear@2: #else nuclear@2: if ( nuclear@2: #endif nuclear@2: ((BITS(8) << 8) + (hold >> 8)) % 31) { nuclear@2: strm->msg = (char *)"incorrect header check"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: if (BITS(4) != Z_DEFLATED) { nuclear@2: strm->msg = (char *)"unknown compression method"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: DROPBITS(4); nuclear@2: len = BITS(4) + 8; nuclear@2: if (len > state->wbits) { nuclear@2: strm->msg = (char *)"invalid window size"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: state->dmax = 1U << len; nuclear@2: Tracev((stderr, "inflate: zlib header ok\n")); nuclear@2: strm->adler = state->check = adler32(0L, Z_NULL, 0); nuclear@2: state->mode = hold & 0x200 ? DICTID : TYPE; nuclear@2: INITBITS(); nuclear@2: break; nuclear@2: #ifdef GUNZIP nuclear@2: case FLAGS: nuclear@2: NEEDBITS(16); nuclear@2: state->flags = (int)(hold); nuclear@2: if ((state->flags & 0xff) != Z_DEFLATED) { nuclear@2: strm->msg = (char *)"unknown compression method"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: if (state->flags & 0xe000) { nuclear@2: strm->msg = (char *)"unknown header flags set"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: if (state->head != Z_NULL) nuclear@2: state->head->text = (int)((hold >> 8) & 1); nuclear@2: if (state->flags & 0x0200) CRC2(state->check, hold); nuclear@2: INITBITS(); nuclear@2: state->mode = TIME; nuclear@2: case TIME: nuclear@2: NEEDBITS(32); nuclear@2: if (state->head != Z_NULL) nuclear@2: state->head->time = hold; nuclear@2: if (state->flags & 0x0200) CRC4(state->check, hold); nuclear@2: INITBITS(); nuclear@2: state->mode = OS; nuclear@2: case OS: nuclear@2: NEEDBITS(16); nuclear@2: if (state->head != Z_NULL) { nuclear@2: state->head->xflags = (int)(hold & 0xff); nuclear@2: state->head->os = (int)(hold >> 8); nuclear@2: } nuclear@2: if (state->flags & 0x0200) CRC2(state->check, hold); nuclear@2: INITBITS(); nuclear@2: state->mode = EXLEN; nuclear@2: case EXLEN: nuclear@2: if (state->flags & 0x0400) { nuclear@2: NEEDBITS(16); nuclear@2: state->length = (unsigned)(hold); nuclear@2: if (state->head != Z_NULL) nuclear@2: state->head->extra_len = (unsigned)hold; nuclear@2: if (state->flags & 0x0200) CRC2(state->check, hold); nuclear@2: INITBITS(); nuclear@2: } nuclear@2: else if (state->head != Z_NULL) nuclear@2: state->head->extra = Z_NULL; nuclear@2: state->mode = EXTRA; nuclear@2: case EXTRA: nuclear@2: if (state->flags & 0x0400) { nuclear@2: copy = state->length; nuclear@2: if (copy > have) copy = have; nuclear@2: if (copy) { nuclear@2: if (state->head != Z_NULL && nuclear@2: state->head->extra != Z_NULL) { nuclear@2: len = state->head->extra_len - state->length; nuclear@2: zmemcpy(state->head->extra + len, next, nuclear@2: len + copy > state->head->extra_max ? nuclear@2: state->head->extra_max - len : copy); nuclear@2: } nuclear@2: if (state->flags & 0x0200) nuclear@2: state->check = crc32(state->check, next, copy); nuclear@2: have -= copy; nuclear@2: next += copy; nuclear@2: state->length -= copy; nuclear@2: } nuclear@2: if (state->length) goto inf_leave; nuclear@2: } nuclear@2: state->length = 0; nuclear@2: state->mode = NAME; nuclear@2: case NAME: nuclear@2: if (state->flags & 0x0800) { nuclear@2: if (have == 0) goto inf_leave; nuclear@2: copy = 0; nuclear@2: do { nuclear@2: len = (unsigned)(next[copy++]); nuclear@2: if (state->head != Z_NULL && nuclear@2: state->head->name != Z_NULL && nuclear@2: state->length < state->head->name_max) nuclear@2: state->head->name[state->length++] = len; nuclear@2: } while (len && copy < have); nuclear@2: if (state->flags & 0x0200) nuclear@2: state->check = crc32(state->check, next, copy); nuclear@2: have -= copy; nuclear@2: next += copy; nuclear@2: if (len) goto inf_leave; nuclear@2: } nuclear@2: else if (state->head != Z_NULL) nuclear@2: state->head->name = Z_NULL; nuclear@2: state->length = 0; nuclear@2: state->mode = COMMENT; nuclear@2: case COMMENT: nuclear@2: if (state->flags & 0x1000) { nuclear@2: if (have == 0) goto inf_leave; nuclear@2: copy = 0; nuclear@2: do { nuclear@2: len = (unsigned)(next[copy++]); nuclear@2: if (state->head != Z_NULL && nuclear@2: state->head->comment != Z_NULL && nuclear@2: state->length < state->head->comm_max) nuclear@2: state->head->comment[state->length++] = len; nuclear@2: } while (len && copy < have); nuclear@2: if (state->flags & 0x0200) nuclear@2: state->check = crc32(state->check, next, copy); nuclear@2: have -= copy; nuclear@2: next += copy; nuclear@2: if (len) goto inf_leave; nuclear@2: } nuclear@2: else if (state->head != Z_NULL) nuclear@2: state->head->comment = Z_NULL; nuclear@2: state->mode = HCRC; nuclear@2: case HCRC: nuclear@2: if (state->flags & 0x0200) { nuclear@2: NEEDBITS(16); nuclear@2: if (hold != (state->check & 0xffff)) { nuclear@2: strm->msg = (char *)"header crc mismatch"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: INITBITS(); nuclear@2: } nuclear@2: if (state->head != Z_NULL) { nuclear@2: state->head->hcrc = (int)((state->flags >> 9) & 1); nuclear@2: state->head->done = 1; nuclear@2: } nuclear@2: strm->adler = state->check = crc32(0L, Z_NULL, 0); nuclear@2: state->mode = TYPE; nuclear@2: break; nuclear@2: #endif nuclear@2: case DICTID: nuclear@2: NEEDBITS(32); nuclear@2: strm->adler = state->check = REVERSE(hold); nuclear@2: INITBITS(); nuclear@2: state->mode = DICT; nuclear@2: case DICT: nuclear@2: if (state->havedict == 0) { nuclear@2: RESTORE(); nuclear@2: return Z_NEED_DICT; nuclear@2: } nuclear@2: strm->adler = state->check = adler32(0L, Z_NULL, 0); nuclear@2: state->mode = TYPE; nuclear@2: case TYPE: nuclear@2: if (flush == Z_BLOCK) goto inf_leave; nuclear@2: case TYPEDO: nuclear@2: if (state->last) { nuclear@2: BYTEBITS(); nuclear@2: state->mode = CHECK; nuclear@2: break; nuclear@2: } nuclear@2: NEEDBITS(3); nuclear@2: state->last = BITS(1); nuclear@2: DROPBITS(1); nuclear@2: switch (BITS(2)) { nuclear@2: case 0: /* stored block */ nuclear@2: Tracev((stderr, "inflate: stored block%s\n", nuclear@2: state->last ? " (last)" : "")); nuclear@2: state->mode = STORED; nuclear@2: break; nuclear@2: case 1: /* fixed block */ nuclear@2: fixedtables(state); nuclear@2: Tracev((stderr, "inflate: fixed codes block%s\n", nuclear@2: state->last ? " (last)" : "")); nuclear@2: state->mode = LEN; /* decode codes */ nuclear@2: break; nuclear@2: case 2: /* dynamic block */ nuclear@2: Tracev((stderr, "inflate: dynamic codes block%s\n", nuclear@2: state->last ? " (last)" : "")); nuclear@2: state->mode = TABLE; nuclear@2: break; nuclear@2: case 3: nuclear@2: strm->msg = (char *)"invalid block type"; nuclear@2: state->mode = BAD; nuclear@2: } nuclear@2: DROPBITS(2); nuclear@2: break; nuclear@2: case STORED: nuclear@2: BYTEBITS(); /* go to byte boundary */ nuclear@2: NEEDBITS(32); nuclear@2: if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { nuclear@2: strm->msg = (char *)"invalid stored block lengths"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: state->length = (unsigned)hold & 0xffff; nuclear@2: Tracev((stderr, "inflate: stored length %u\n", nuclear@2: state->length)); nuclear@2: INITBITS(); nuclear@2: state->mode = COPY; nuclear@2: case COPY: nuclear@2: copy = state->length; nuclear@2: if (copy) { nuclear@2: if (copy > have) copy = have; nuclear@2: if (copy > left) copy = left; nuclear@2: if (copy == 0) goto inf_leave; nuclear@2: zmemcpy(put, next, copy); nuclear@2: have -= copy; nuclear@2: next += copy; nuclear@2: left -= copy; nuclear@2: put += copy; nuclear@2: state->length -= copy; nuclear@2: break; nuclear@2: } nuclear@2: Tracev((stderr, "inflate: stored end\n")); nuclear@2: state->mode = TYPE; nuclear@2: break; nuclear@2: case TABLE: nuclear@2: NEEDBITS(14); nuclear@2: state->nlen = BITS(5) + 257; nuclear@2: DROPBITS(5); nuclear@2: state->ndist = BITS(5) + 1; nuclear@2: DROPBITS(5); nuclear@2: state->ncode = BITS(4) + 4; nuclear@2: DROPBITS(4); nuclear@2: #ifndef PKZIP_BUG_WORKAROUND nuclear@2: if (state->nlen > 286 || state->ndist > 30) { nuclear@2: strm->msg = (char *)"too many length or distance symbols"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: #endif nuclear@2: Tracev((stderr, "inflate: table sizes ok\n")); nuclear@2: state->have = 0; nuclear@2: state->mode = LENLENS; nuclear@2: case LENLENS: nuclear@2: while (state->have < state->ncode) { nuclear@2: NEEDBITS(3); nuclear@2: state->lens[order[state->have++]] = (unsigned short)BITS(3); nuclear@2: DROPBITS(3); nuclear@2: } nuclear@2: while (state->have < 19) nuclear@2: state->lens[order[state->have++]] = 0; nuclear@2: state->next = state->codes; nuclear@2: state->lencode = (code const FAR *)(state->next); nuclear@2: state->lenbits = 7; nuclear@2: ret = inflate_table(CODES, state->lens, 19, &(state->next), nuclear@2: &(state->lenbits), state->work); nuclear@2: if (ret) { nuclear@2: strm->msg = (char *)"invalid code lengths set"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: Tracev((stderr, "inflate: code lengths ok\n")); nuclear@2: state->have = 0; nuclear@2: state->mode = CODELENS; nuclear@2: case CODELENS: nuclear@2: while (state->have < state->nlen + state->ndist) { nuclear@2: for (;;) { nuclear@2: this = state->lencode[BITS(state->lenbits)]; nuclear@2: if ((unsigned)(this.bits) <= bits) break; nuclear@2: PULLBYTE(); nuclear@2: } nuclear@2: if (this.val < 16) { nuclear@2: NEEDBITS(this.bits); nuclear@2: DROPBITS(this.bits); nuclear@2: state->lens[state->have++] = this.val; nuclear@2: } nuclear@2: else { nuclear@2: if (this.val == 16) { nuclear@2: NEEDBITS(this.bits + 2); nuclear@2: DROPBITS(this.bits); nuclear@2: if (state->have == 0) { nuclear@2: strm->msg = (char *)"invalid bit length repeat"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: len = state->lens[state->have - 1]; nuclear@2: copy = 3 + BITS(2); nuclear@2: DROPBITS(2); nuclear@2: } nuclear@2: else if (this.val == 17) { nuclear@2: NEEDBITS(this.bits + 3); nuclear@2: DROPBITS(this.bits); nuclear@2: len = 0; nuclear@2: copy = 3 + BITS(3); nuclear@2: DROPBITS(3); nuclear@2: } nuclear@2: else { nuclear@2: NEEDBITS(this.bits + 7); nuclear@2: DROPBITS(this.bits); nuclear@2: len = 0; nuclear@2: copy = 11 + BITS(7); nuclear@2: DROPBITS(7); nuclear@2: } nuclear@2: if (state->have + copy > state->nlen + state->ndist) { nuclear@2: strm->msg = (char *)"invalid bit length repeat"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: while (copy--) nuclear@2: state->lens[state->have++] = (unsigned short)len; nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: /* handle error breaks in while */ nuclear@2: if (state->mode == BAD) break; nuclear@2: nuclear@2: /* build code tables */ nuclear@2: state->next = state->codes; nuclear@2: state->lencode = (code const FAR *)(state->next); nuclear@2: state->lenbits = 9; nuclear@2: ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), nuclear@2: &(state->lenbits), state->work); nuclear@2: if (ret) { nuclear@2: strm->msg = (char *)"invalid literal/lengths set"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: state->distcode = (code const FAR *)(state->next); nuclear@2: state->distbits = 6; nuclear@2: ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, nuclear@2: &(state->next), &(state->distbits), state->work); nuclear@2: if (ret) { nuclear@2: strm->msg = (char *)"invalid distances set"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: Tracev((stderr, "inflate: codes ok\n")); nuclear@2: state->mode = LEN; nuclear@2: case LEN: nuclear@2: if (have >= 6 && left >= 258) { nuclear@2: RESTORE(); nuclear@2: inflate_fast(strm, out); nuclear@2: LOAD(); nuclear@2: break; nuclear@2: } nuclear@2: for (;;) { nuclear@2: this = state->lencode[BITS(state->lenbits)]; nuclear@2: if ((unsigned)(this.bits) <= bits) break; nuclear@2: PULLBYTE(); nuclear@2: } nuclear@2: if (this.op && (this.op & 0xf0) == 0) { nuclear@2: last = this; nuclear@2: for (;;) { nuclear@2: this = state->lencode[last.val + nuclear@2: (BITS(last.bits + last.op) >> last.bits)]; nuclear@2: if ((unsigned)(last.bits + this.bits) <= bits) break; nuclear@2: PULLBYTE(); nuclear@2: } nuclear@2: DROPBITS(last.bits); nuclear@2: } nuclear@2: DROPBITS(this.bits); nuclear@2: state->length = (unsigned)this.val; nuclear@2: if ((int)(this.op) == 0) { nuclear@2: Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ? nuclear@2: "inflate: literal '%c'\n" : nuclear@2: "inflate: literal 0x%02x\n", this.val)); nuclear@2: state->mode = LIT; nuclear@2: break; nuclear@2: } nuclear@2: if (this.op & 32) { nuclear@2: Tracevv((stderr, "inflate: end of block\n")); nuclear@2: state->mode = TYPE; nuclear@2: break; nuclear@2: } nuclear@2: if (this.op & 64) { nuclear@2: strm->msg = (char *)"invalid literal/length code"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: state->extra = (unsigned)(this.op) & 15; nuclear@2: state->mode = LENEXT; nuclear@2: case LENEXT: nuclear@2: if (state->extra) { nuclear@2: NEEDBITS(state->extra); nuclear@2: state->length += BITS(state->extra); nuclear@2: DROPBITS(state->extra); nuclear@2: } nuclear@2: Tracevv((stderr, "inflate: length %u\n", state->length)); nuclear@2: state->mode = DIST; nuclear@2: case DIST: nuclear@2: for (;;) { nuclear@2: this = state->distcode[BITS(state->distbits)]; nuclear@2: if ((unsigned)(this.bits) <= bits) break; nuclear@2: PULLBYTE(); nuclear@2: } nuclear@2: if ((this.op & 0xf0) == 0) { nuclear@2: last = this; nuclear@2: for (;;) { nuclear@2: this = state->distcode[last.val + nuclear@2: (BITS(last.bits + last.op) >> last.bits)]; nuclear@2: if ((unsigned)(last.bits + this.bits) <= bits) break; nuclear@2: PULLBYTE(); nuclear@2: } nuclear@2: DROPBITS(last.bits); nuclear@2: } nuclear@2: DROPBITS(this.bits); nuclear@2: if (this.op & 64) { nuclear@2: strm->msg = (char *)"invalid distance code"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: state->offset = (unsigned)this.val; nuclear@2: state->extra = (unsigned)(this.op) & 15; nuclear@2: state->mode = DISTEXT; nuclear@2: case DISTEXT: nuclear@2: if (state->extra) { nuclear@2: NEEDBITS(state->extra); nuclear@2: state->offset += BITS(state->extra); nuclear@2: DROPBITS(state->extra); nuclear@2: } nuclear@2: #ifdef INFLATE_STRICT nuclear@2: if (state->offset > state->dmax) { nuclear@2: strm->msg = (char *)"invalid distance too far back"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: #endif nuclear@2: if (state->offset > state->whave + out - left) { nuclear@2: strm->msg = (char *)"invalid distance too far back"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: Tracevv((stderr, "inflate: distance %u\n", state->offset)); nuclear@2: state->mode = MATCH; nuclear@2: case MATCH: nuclear@2: if (left == 0) goto inf_leave; nuclear@2: copy = out - left; nuclear@2: if (state->offset > copy) { /* copy from window */ nuclear@2: copy = state->offset - copy; nuclear@2: if (copy > state->write) { nuclear@2: copy -= state->write; nuclear@2: from = state->window + (state->wsize - copy); nuclear@2: } nuclear@2: else nuclear@2: from = state->window + (state->write - copy); nuclear@2: if (copy > state->length) copy = state->length; nuclear@2: } nuclear@2: else { /* copy from output */ nuclear@2: from = put - state->offset; nuclear@2: copy = state->length; nuclear@2: } nuclear@2: if (copy > left) copy = left; nuclear@2: left -= copy; nuclear@2: state->length -= copy; nuclear@2: do { nuclear@2: *put++ = *from++; nuclear@2: } while (--copy); nuclear@2: if (state->length == 0) state->mode = LEN; nuclear@2: break; nuclear@2: case LIT: nuclear@2: if (left == 0) goto inf_leave; nuclear@2: *put++ = (unsigned char)(state->length); nuclear@2: left--; nuclear@2: state->mode = LEN; nuclear@2: break; nuclear@2: case CHECK: nuclear@2: if (state->wrap) { nuclear@2: NEEDBITS(32); nuclear@2: out -= left; nuclear@2: strm->total_out += out; nuclear@2: state->total += out; nuclear@2: if (out) nuclear@2: strm->adler = state->check = nuclear@2: UPDATE(state->check, put - out, out); nuclear@2: out = left; nuclear@2: if (( nuclear@2: #ifdef GUNZIP nuclear@2: state->flags ? hold : nuclear@2: #endif nuclear@2: REVERSE(hold)) != state->check) { nuclear@2: strm->msg = (char *)"incorrect data check"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: INITBITS(); nuclear@2: Tracev((stderr, "inflate: check matches trailer\n")); nuclear@2: } nuclear@2: #ifdef GUNZIP nuclear@2: state->mode = LENGTH; nuclear@2: case LENGTH: nuclear@2: if (state->wrap && state->flags) { nuclear@2: NEEDBITS(32); nuclear@2: if (hold != (state->total & 0xffffffffUL)) { nuclear@2: strm->msg = (char *)"incorrect length check"; nuclear@2: state->mode = BAD; nuclear@2: break; nuclear@2: } nuclear@2: INITBITS(); nuclear@2: Tracev((stderr, "inflate: length matches trailer\n")); nuclear@2: } nuclear@2: #endif nuclear@2: state->mode = DONE; nuclear@2: case DONE: nuclear@2: ret = Z_STREAM_END; nuclear@2: goto inf_leave; nuclear@2: case BAD: nuclear@2: ret = Z_DATA_ERROR; nuclear@2: goto inf_leave; nuclear@2: case MEM: nuclear@2: return Z_MEM_ERROR; nuclear@2: case SYNC: nuclear@2: default: nuclear@2: return Z_STREAM_ERROR; nuclear@2: } nuclear@2: nuclear@2: /* nuclear@2: Return from inflate(), updating the total counts and the check value. nuclear@2: If there was no progress during the inflate() call, return a buffer nuclear@2: error. Call updatewindow() to create and/or update the window state. nuclear@2: Note: a memory error from inflate() is non-recoverable. nuclear@2: */ nuclear@2: inf_leave: nuclear@2: RESTORE(); nuclear@2: if (state->wsize || (state->mode < CHECK && out != strm->avail_out)) nuclear@2: if (updatewindow(strm, out)) { nuclear@2: state->mode = MEM; nuclear@2: return Z_MEM_ERROR; nuclear@2: } nuclear@2: in -= strm->avail_in; nuclear@2: out -= strm->avail_out; nuclear@2: strm->total_in += in; nuclear@2: strm->total_out += out; nuclear@2: state->total += out; nuclear@2: if (state->wrap && out) nuclear@2: strm->adler = state->check = nuclear@2: UPDATE(state->check, strm->next_out - out, out); nuclear@2: strm->data_type = state->bits + (state->last ? 64 : 0) + nuclear@2: (state->mode == TYPE ? 128 : 0); nuclear@2: if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) nuclear@2: ret = Z_BUF_ERROR; nuclear@2: return ret; nuclear@2: } nuclear@2: nuclear@2: int ZEXPORT inflateEnd(strm) nuclear@2: z_streamp strm; nuclear@2: { nuclear@2: struct inflate_state FAR *state; nuclear@2: if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) nuclear@2: return Z_STREAM_ERROR; nuclear@2: state = (struct inflate_state FAR *)strm->state; nuclear@2: if (state->window != Z_NULL) ZFREE(strm, state->window); nuclear@2: ZFREE(strm, strm->state); nuclear@2: strm->state = Z_NULL; nuclear@2: Tracev((stderr, "inflate: end\n")); nuclear@2: return Z_OK; nuclear@2: } nuclear@2: nuclear@2: int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) nuclear@2: z_streamp strm; nuclear@2: const Bytef *dictionary; nuclear@2: uInt dictLength; nuclear@2: { nuclear@2: struct inflate_state FAR *state; nuclear@2: unsigned long id; nuclear@2: nuclear@2: /* check state */ nuclear@2: if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; nuclear@2: state = (struct inflate_state FAR *)strm->state; nuclear@2: if (state->wrap != 0 && state->mode != DICT) nuclear@2: return Z_STREAM_ERROR; nuclear@2: nuclear@2: /* check for correct dictionary id */ nuclear@2: if (state->mode == DICT) { nuclear@2: id = adler32(0L, Z_NULL, 0); nuclear@2: id = adler32(id, dictionary, dictLength); nuclear@2: if (id != state->check) nuclear@2: return Z_DATA_ERROR; nuclear@2: } nuclear@2: nuclear@2: /* copy dictionary to window */ nuclear@2: if (updatewindow(strm, strm->avail_out)) { nuclear@2: state->mode = MEM; nuclear@2: return Z_MEM_ERROR; nuclear@2: } nuclear@2: if (dictLength > state->wsize) { nuclear@2: zmemcpy(state->window, dictionary + dictLength - state->wsize, nuclear@2: state->wsize); nuclear@2: state->whave = state->wsize; nuclear@2: } nuclear@2: else { nuclear@2: zmemcpy(state->window + state->wsize - dictLength, dictionary, nuclear@2: dictLength); nuclear@2: state->whave = dictLength; nuclear@2: } nuclear@2: state->havedict = 1; nuclear@2: Tracev((stderr, "inflate: dictionary set\n")); nuclear@2: return Z_OK; nuclear@2: } nuclear@2: nuclear@2: int ZEXPORT inflateGetHeader(strm, head) nuclear@2: z_streamp strm; nuclear@2: gz_headerp head; nuclear@2: { nuclear@2: struct inflate_state FAR *state; nuclear@2: nuclear@2: /* check state */ nuclear@2: if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; nuclear@2: state = (struct inflate_state FAR *)strm->state; nuclear@2: if ((state->wrap & 2) == 0) return Z_STREAM_ERROR; nuclear@2: nuclear@2: /* save header structure */ nuclear@2: state->head = head; nuclear@2: head->done = 0; nuclear@2: return Z_OK; nuclear@2: } nuclear@2: nuclear@2: /* nuclear@2: Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found nuclear@2: or when out of input. When called, *have is the number of pattern bytes nuclear@2: found in order so far, in 0..3. On return *have is updated to the new nuclear@2: state. If on return *have equals four, then the pattern was found and the nuclear@2: return value is how many bytes were read including the last byte of the nuclear@2: pattern. If *have is less than four, then the pattern has not been found nuclear@2: yet and the return value is len. In the latter case, syncsearch() can be nuclear@2: called again with more data and the *have state. *have is initialized to nuclear@2: zero for the first call. nuclear@2: */ nuclear@2: local unsigned syncsearch(have, buf, len) nuclear@2: unsigned FAR *have; nuclear@2: unsigned char FAR *buf; nuclear@2: unsigned len; nuclear@2: { nuclear@2: unsigned got; nuclear@2: unsigned next; nuclear@2: nuclear@2: got = *have; nuclear@2: next = 0; nuclear@2: while (next < len && got < 4) { nuclear@2: if ((int)(buf[next]) == (got < 2 ? 0 : 0xff)) nuclear@2: got++; nuclear@2: else if (buf[next]) nuclear@2: got = 0; nuclear@2: else nuclear@2: got = 4 - got; nuclear@2: next++; nuclear@2: } nuclear@2: *have = got; nuclear@2: return next; nuclear@2: } nuclear@2: nuclear@2: int ZEXPORT inflateSync(strm) nuclear@2: z_streamp strm; nuclear@2: { nuclear@2: unsigned len; /* number of bytes to look at or looked at */ nuclear@2: unsigned long in, out; /* temporary to save total_in and total_out */ nuclear@2: unsigned char buf[4]; /* to restore bit buffer to byte string */ nuclear@2: struct inflate_state FAR *state; nuclear@2: nuclear@2: /* check parameters */ nuclear@2: if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; nuclear@2: state = (struct inflate_state FAR *)strm->state; nuclear@2: if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; nuclear@2: nuclear@2: /* if first time, start search in bit buffer */ nuclear@2: if (state->mode != SYNC) { nuclear@2: state->mode = SYNC; nuclear@2: state->hold <<= state->bits & 7; nuclear@2: state->bits -= state->bits & 7; nuclear@2: len = 0; nuclear@2: while (state->bits >= 8) { nuclear@2: buf[len++] = (unsigned char)(state->hold); nuclear@2: state->hold >>= 8; nuclear@2: state->bits -= 8; nuclear@2: } nuclear@2: state->have = 0; nuclear@2: syncsearch(&(state->have), buf, len); nuclear@2: } nuclear@2: nuclear@2: /* search available input */ nuclear@2: len = syncsearch(&(state->have), strm->next_in, strm->avail_in); nuclear@2: strm->avail_in -= len; nuclear@2: strm->next_in += len; nuclear@2: strm->total_in += len; nuclear@2: nuclear@2: /* return no joy or set up to restart inflate() on a new block */ nuclear@2: if (state->have != 4) return Z_DATA_ERROR; nuclear@2: in = strm->total_in; out = strm->total_out; nuclear@2: inflateReset(strm); nuclear@2: strm->total_in = in; strm->total_out = out; nuclear@2: state->mode = TYPE; nuclear@2: return Z_OK; nuclear@2: } nuclear@2: nuclear@2: /* nuclear@2: Returns true if inflate is currently at the end of a block generated by nuclear@2: Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP nuclear@2: implementation to provide an additional safety check. PPP uses nuclear@2: Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored nuclear@2: block. When decompressing, PPP checks that at the end of input packet, nuclear@2: inflate is waiting for these length bytes. nuclear@2: */ nuclear@2: int ZEXPORT inflateSyncPoint(strm) nuclear@2: z_streamp strm; nuclear@2: { nuclear@2: struct inflate_state FAR *state; nuclear@2: nuclear@2: if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; nuclear@2: state = (struct inflate_state FAR *)strm->state; nuclear@2: return state->mode == STORED && state->bits == 0; nuclear@2: } nuclear@2: nuclear@2: int ZEXPORT inflateCopy(dest, source) nuclear@2: z_streamp dest; nuclear@2: z_streamp source; nuclear@2: { nuclear@2: struct inflate_state FAR *state; nuclear@2: struct inflate_state FAR *copy; nuclear@2: unsigned char FAR *window; nuclear@2: unsigned wsize; nuclear@2: nuclear@2: /* check input */ nuclear@2: if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL || nuclear@2: source->zalloc == (alloc_func)0 || source->zfree == (free_func)0) nuclear@2: return Z_STREAM_ERROR; nuclear@2: state = (struct inflate_state FAR *)source->state; nuclear@2: nuclear@2: /* allocate space */ nuclear@2: copy = (struct inflate_state FAR *) nuclear@2: ZALLOC(source, 1, sizeof(struct inflate_state)); nuclear@2: if (copy == Z_NULL) return Z_MEM_ERROR; nuclear@2: window = Z_NULL; nuclear@2: if (state->window != Z_NULL) { nuclear@2: window = (unsigned char FAR *) nuclear@2: ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); nuclear@2: if (window == Z_NULL) { nuclear@2: ZFREE(source, copy); nuclear@2: return Z_MEM_ERROR; nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: /* copy state */ nuclear@2: zmemcpy(dest, source, sizeof(z_stream)); nuclear@2: zmemcpy(copy, state, sizeof(struct inflate_state)); nuclear@2: if (state->lencode >= state->codes && nuclear@2: state->lencode <= state->codes + ENOUGH - 1) { nuclear@2: copy->lencode = copy->codes + (state->lencode - state->codes); nuclear@2: copy->distcode = copy->codes + (state->distcode - state->codes); nuclear@2: } nuclear@2: copy->next = copy->codes + (state->next - state->codes); nuclear@2: if (window != Z_NULL) { nuclear@2: wsize = 1U << state->wbits; nuclear@2: zmemcpy(window, state->window, wsize); nuclear@2: } nuclear@2: copy->window = window; nuclear@2: dest->state = (struct internal_state FAR *)copy; nuclear@2: return Z_OK; nuclear@2: }