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