Blame nss/lib/zlib/inflate.c

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