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