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