Blame deps/zlib/inflate.c

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