Blame zlib-src/inflate.c

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