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