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