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