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