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