Blame zlib-src/infback.c

Packit Service 4a2782
/* infback.c -- inflate using a call-back interface
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
   This code is largely copied from inflate.c.  Normally either infback.o or
Packit Service 4a2782
   inflate.o would be linked into an application--not both.  The interface
Packit Service 4a2782
   with inffast.c is retained so that optimized assembler-coded versions of
Packit Service 4a2782
   inflate_fast() can be used with either inflate.c or infback.c.
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
/* function prototypes */
Packit Service 4a2782
local void fixedtables OF((struct inflate_state FAR *state));
Packit Service 4a2782
Packit Service 4a2782
/*
Packit Service 4a2782
   strm provides memory allocation functions in zalloc and zfree, or
Packit Service 4a2782
   Z_NULL to use the library memory allocation functions.
Packit Service 4a2782
Packit Service 4a2782
   windowBits is in the range 8..15, and window is a user-supplied
Packit Service 4a2782
   window and output buffer that is 2**windowBits bytes.
Packit Service 4a2782
 */
Packit Service 4a2782
int ZEXPORT inflateBackInit_(
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    int windowBits,
Packit Service 4a2782
    unsigned char FAR *window,
Packit Service 4a2782
    const char *version,
Packit Service 4a2782
    int stream_size)
Packit Service 4a2782
{
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 || window == Z_NULL ||
Packit Service 4a2782
        windowBits < 8 || windowBits > 15)
Packit Service 4a2782
        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 *)ZALLOC(strm, 1,
Packit Service 4a2782
                                               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->dmax = 32768U;
Packit Service 4a2782
    state->wbits = (uInt)windowBits;
Packit Service 4a2782
    state->wsize = 1U << windowBits;
Packit Service 4a2782
    state->window = window;
Packit Service 4a2782
    state->wnext = 0;
Packit Service 4a2782
    state->whave = 0;
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
/* Macros for inflateBack(): */
Packit Service 4a2782
Packit Service 4a2782
/* Load returned state from inflate_fast() */
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
/* Set state from registers for inflate_fast() */
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
/* Assure that some input is available.  If input is requested, but denied,
Packit Service 4a2782
   then return a Z_BUF_ERROR from inflateBack(). */
Packit Service 4a2782
#define PULL() \
Packit Service 4a2782
    do { \
Packit Service 4a2782
        if (have == 0) { \
Packit Service 4a2782
            have = in(in_desc, &next;; \
Packit Service 4a2782
            if (have == 0) { \
Packit Service 4a2782
                next = Z_NULL; \
Packit Service 4a2782
                ret = Z_BUF_ERROR; \
Packit Service 4a2782
                goto inf_leave; \
Packit Service 4a2782
            } \
Packit Service 4a2782
        } \
Packit Service 4a2782
    } while (0)
Packit Service 4a2782
Packit Service 4a2782
/* Get a byte of input into the bit accumulator, or return from inflateBack()
Packit Service 4a2782
   with an error if there is no input available. */
Packit Service 4a2782
#define PULLBYTE() \
Packit Service 4a2782
    do { \
Packit Service 4a2782
        PULL(); \
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 inflateBack() with
Packit Service 4a2782
   an error. */
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
/* Assure that some output space is available, by writing out the window
Packit Service 4a2782
   if it's full.  If the write fails, return from inflateBack() with a
Packit Service 4a2782
   Z_BUF_ERROR. */
Packit Service 4a2782
#define ROOM() \
Packit Service 4a2782
    do { \
Packit Service 4a2782
        if (left == 0) { \
Packit Service 4a2782
            put = state->window; \
Packit Service 4a2782
            left = state->wsize; \
Packit Service 4a2782
            state->whave = left; \
Packit Service 4a2782
            if (out(out_desc, put, left)) { \
Packit Service 4a2782
                ret = Z_BUF_ERROR; \
Packit Service 4a2782
                goto inf_leave; \
Packit Service 4a2782
            } \
Packit Service 4a2782
        } \
Packit Service 4a2782
    } while (0)
Packit Service 4a2782
Packit Service 4a2782
/*
Packit Service 4a2782
   strm provides the memory allocation functions and window buffer on input,
Packit Service 4a2782
   and provides information on the unused input on return.  For Z_DATA_ERROR
Packit Service 4a2782
   returns, strm will also provide an error message.
Packit Service 4a2782
Packit Service 4a2782
   in() and out() are the call-back input and output functions.  When
Packit Service 4a2782
   inflateBack() needs more input, it calls in().  When inflateBack() has
Packit Service 4a2782
   filled the window with output, or when it completes with data in the
Packit Service 4a2782
   window, it calls out() to write out the data.  The application must not
Packit Service 4a2782
   change the provided input until in() is called again or inflateBack()
Packit Service 4a2782
   returns.  The application must not change the window/output buffer until
Packit Service 4a2782
   inflateBack() returns.
Packit Service 4a2782
Packit Service 4a2782
   in() and out() are called with a descriptor parameter provided in the
Packit Service 4a2782
   inflateBack() call.  This parameter can be a structure that provides the
Packit Service 4a2782
   information required to do the read or write, as well as accumulated
Packit Service 4a2782
   information on the input and output such as totals and check values.
Packit Service 4a2782
Packit Service 4a2782
   in() should return zero on failure.  out() should return non-zero on
Packit Service 4a2782
   failure.  If either in() or out() fails, than inflateBack() returns a
Packit Service 4a2782
   Z_BUF_ERROR.  strm->next_in can be checked for Z_NULL to see whether it
Packit Service 4a2782
   was in() or out() that caused in the error.  Otherwise,  inflateBack()
Packit Service 4a2782
   returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
Packit Service 4a2782
   error, or Z_MEM_ERROR if it could not allocate memory for the state.
Packit Service 4a2782
   inflateBack() can also return Z_STREAM_ERROR if the input parameters
Packit Service 4a2782
   are not correct, i.e. strm is Z_NULL or the state was not initialized.
Packit Service 4a2782
 */
Packit Service 4a2782
int ZEXPORT inflateBack(
Packit Service 4a2782
    z_streamp strm,
Packit Service 4a2782
    in_func in,
Packit Service 4a2782
    void FAR *in_desc,
Packit Service 4a2782
    out_func out,
Packit Service 4a2782
    void FAR *out_desc)
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 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
    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
    /* Check that the strm exists and that the state was initialized */
Packit Service 4a2782
    if (strm == Z_NULL || strm->state == Z_NULL)
Packit Service 4a2782
        return Z_STREAM_ERROR;
Packit Service 4a2782
    state = (struct inflate_state FAR *)strm->state;
Packit Service 4a2782
Packit Service 4a2782
    /* Reset the state */
Packit Service 4a2782
    strm->msg = Z_NULL;
Packit Service 4a2782
    state->mode = TYPE;
Packit Service 4a2782
    state->last = 0;
Packit Service 4a2782
    state->whave = 0;
Packit Service 4a2782
    next = strm->next_in;
Packit Service 4a2782
    have = next != Z_NULL ? strm->avail_in : 0;
Packit Service 4a2782
    hold = 0;
Packit Service 4a2782
    bits = 0;
Packit Service 4a2782
    put = state->window;
Packit Service 4a2782
    left = state->wsize;
Packit Service 4a2782
Packit Service 4a2782
    /* Inflate until end of block marked as last */
Packit Service 4a2782
    for (;;)
Packit Service 4a2782
        switch (state->mode) {
Packit Service 4a2782
        case TYPE:
Packit Service 4a2782
            /* determine and dispatch block type */
Packit Service 4a2782
            if (state->last) {
Packit Service 4a2782
                BYTEBITS();
Packit Service 4a2782
                state->mode = DONE;
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
                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
Packit Service 4a2782
        case STORED:
Packit Service 4a2782
            /* get and verify stored block length */
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
Packit Service 4a2782
            /* copy stored block from input to output */
Packit Service 4a2782
            while (state->length != 0) {
Packit Service 4a2782
                copy = state->length;
Packit Service 4a2782
                PULL();
Packit Service 4a2782
                ROOM();
Packit Service 4a2782
                if (copy > have) copy = have;
Packit Service 4a2782
                if (copy > left) copy = left;
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
            }
Packit Service 4a2782
            Tracev((stderr, "inflate:       stored end\n"));
Packit Service 4a2782
            state->mode = TYPE;
Packit Service 4a2782
            break;
Packit Service 4a2782
Packit Service 4a2782
        case TABLE:
Packit Service 4a2782
            /* get dynamic table entries descriptor */
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
Packit Service 4a2782
            /* get code length code lengths (not a typo) */
Packit Service 4a2782
            state->have = 0;
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 = (code const 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
Packit Service 4a2782
            /* get length and distance code code lengths */
Packit Service 4a2782
            state->have = 0;
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 = (unsigned)(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 = (code const 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 = (code const 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
            /* FALLTHROUGH */
Packit Service 4a2782
Packit Service 4a2782
        case LEN:
Packit Service 4a2782
            /* use inflate_fast() if we have enough input and output */
Packit Service 4a2782
            if (have >= 6 && left >= 258) {
Packit Service 4a2782
                RESTORE();
Packit Service 4a2782
                if (state->whave < state->wsize)
Packit Service 4a2782
                    state->whave = state->wsize - left;
Packit Service 4a2782
                inflate_fast(strm, state->wsize);
Packit Service 4a2782
                LOAD();
Packit Service 4a2782
                break;
Packit Service 4a2782
            }
Packit Service 4a2782
Packit Service 4a2782
            /* get a literal, length, or end-of-block code */
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
            }
Packit Service 4a2782
            DROPBITS(here.bits);
Packit Service 4a2782
            state->length = (unsigned)here.val;
Packit Service 4a2782
Packit Service 4a2782
            /* process literal */
Packit Service 4a2782
            if (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
                ROOM();
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
            }
Packit Service 4a2782
Packit Service 4a2782
            /* process end of block */
Packit Service 4a2782
            if (here.op & 32) {
Packit Service 4a2782
                Tracevv((stderr, "inflate:         end of block\n"));
Packit Service 4a2782
                state->mode = TYPE;
Packit Service 4a2782
                break;
Packit Service 4a2782
            }
Packit Service 4a2782
Packit Service 4a2782
            /* invalid code */
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
Packit Service 4a2782
            /* length code -- get extra bits, if any */
Packit Service 4a2782
            state->extra = (unsigned)(here.op) & 15;
Packit Service 4a2782
            if (state->extra != 0) {
Packit Service 4a2782
                NEEDBITS(state->extra);
Packit Service 4a2782
                state->length += BITS(state->extra);
Packit Service 4a2782
                DROPBITS(state->extra);
Packit Service 4a2782
            }
Packit Service 4a2782
            Tracevv((stderr, "inflate:         length %u\n", state->length));
Packit Service 4a2782
Packit Service 4a2782
            /* get distance code */
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
            }
Packit Service 4a2782
            DROPBITS(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
Packit Service 4a2782
            /* get distance extra bits, if any */
Packit Service 4a2782
            state->extra = (unsigned)(here.op) & 15;
Packit Service 4a2782
            if (state->extra != 0) {
Packit Service 4a2782
                NEEDBITS(state->extra);
Packit Service 4a2782
                state->offset += BITS(state->extra);
Packit Service 4a2782
                DROPBITS(state->extra);
Packit Service 4a2782
            }
Packit Service 4a2782
            if (state->offset > state->wsize - (state->whave < state->wsize ?
Packit Service 4a2782
                                                left : 0)) {
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
            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
Packit Service 4a2782
Packit Service 4a2782
            /* copy match from window to output */
Packit Service 4a2782
            do {
Packit Service 4a2782
                ROOM();
Packit Service 4a2782
                copy = state->wsize - state->offset;
Packit Service 4a2782
                if (copy < left) {
Packit Service 4a2782
                    from = put + copy;
Packit Service 4a2782
                    copy = left - copy;
Packit Service 4a2782
                }
Packit Service 4a2782
                else {
Packit Service 4a2782
                    from = put - state->offset;
Packit Service 4a2782
                    copy = left;
Packit Service 4a2782
                }
Packit Service 4a2782
                if (copy > state->length) copy = state->length;
Packit Service 4a2782
                state->length -= copy;
Packit Service 4a2782
                left -= copy;
Packit Service 4a2782
                do {
Packit Service 4a2782
                    *put++ = *from++;
Packit Service 4a2782
                } while (--copy);
Packit Service 4a2782
            } while (state->length != 0);
Packit Service 4a2782
            break;
Packit Service 4a2782
Packit Service 4a2782
        case DONE:
Packit Service 4a2782
            /* inflate stream terminated properly -- write leftover output */
Packit Service 4a2782
            ret = Z_STREAM_END;
Packit Service 4a2782
            if (left < state->wsize) {
Packit Service 4a2782
                if (out(out_desc, state->window, state->wsize - left))
Packit Service 4a2782
                    ret = Z_BUF_ERROR;
Packit Service 4a2782
            }
Packit Service 4a2782
            goto inf_leave;
Packit Service 4a2782
Packit Service 4a2782
        case BAD:
Packit Service 4a2782
            ret = Z_DATA_ERROR;
Packit Service 4a2782
            goto inf_leave;
Packit Service 4a2782
Packit Service 4a2782
        default:                /* can't happen, but makes compilers happy */
Packit Service 4a2782
            ret = Z_STREAM_ERROR;
Packit Service 4a2782
            goto inf_leave;
Packit Service 4a2782
        }
Packit Service 4a2782
Packit Service 4a2782
    /* Return unused input */
Packit Service 4a2782
  inf_leave:
Packit Service 4a2782
    strm->next_in = next;
Packit Service 4a2782
    strm->avail_in = have;
Packit Service 4a2782
    return ret;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
int ZEXPORT inflateBackEnd(
Packit Service 4a2782
    z_streamp strm)
Packit Service 4a2782
{
Packit Service 4a2782
    if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
Packit Service 4a2782
        return Z_STREAM_ERROR;
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
}