Home | History | Annotate | Line # | Download | only in zlib
infback.c revision 1.1
      1  1.1  christos /*	$NetBSD: infback.c,v 1.1 2006/01/14 20:10:29 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /* infback.c -- inflate using a call-back interface
      4  1.1  christos  * Copyright (C) 1995-2005 Mark Adler
      5  1.1  christos  * For conditions of distribution and use, see copyright notice in zlib.h
      6  1.1  christos  */
      7  1.1  christos 
      8  1.1  christos /*
      9  1.1  christos    This code is largely copied from inflate.c.  Normally either infback.o or
     10  1.1  christos    inflate.o would be linked into an application--not both.  The interface
     11  1.1  christos    with inffast.c is retained so that optimized assembler-coded versions of
     12  1.1  christos    inflate_fast() can be used with either inflate.c or infback.c.
     13  1.1  christos  */
     14  1.1  christos 
     15  1.1  christos #include "zutil.h"
     16  1.1  christos #include "inftrees.h"
     17  1.1  christos #include "inflate.h"
     18  1.1  christos #include "inffast.h"
     19  1.1  christos 
     20  1.1  christos /* function prototypes */
     21  1.1  christos local void fixedtables OF((struct inflate_state FAR *state));
     22  1.1  christos 
     23  1.1  christos /*
     24  1.1  christos    strm provides memory allocation functions in zalloc and zfree, or
     25  1.1  christos    Z_NULL to use the library memory allocation functions.
     26  1.1  christos 
     27  1.1  christos    windowBits is in the range 8..15, and window is a user-supplied
     28  1.1  christos    window and output buffer that is 2**windowBits bytes.
     29  1.1  christos  */
     30  1.1  christos int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
     31  1.1  christos z_streamp strm;
     32  1.1  christos int windowBits;
     33  1.1  christos unsigned char FAR *window;
     34  1.1  christos const char *version;
     35  1.1  christos int stream_size;
     36  1.1  christos {
     37  1.1  christos     struct inflate_state FAR *state;
     38  1.1  christos 
     39  1.1  christos     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
     40  1.1  christos         stream_size != (int)(sizeof(z_stream)))
     41  1.1  christos         return Z_VERSION_ERROR;
     42  1.1  christos     if (strm == Z_NULL || window == Z_NULL ||
     43  1.1  christos         windowBits < 8 || windowBits > 15)
     44  1.1  christos         return Z_STREAM_ERROR;
     45  1.1  christos     strm->msg = Z_NULL;                 /* in case we return an error */
     46  1.1  christos     if (strm->zalloc == (alloc_func)0) {
     47  1.1  christos         strm->zalloc = zcalloc;
     48  1.1  christos         strm->opaque = (voidpf)0;
     49  1.1  christos     }
     50  1.1  christos     if (strm->zfree == (free_func)0) strm->zfree = zcfree;
     51  1.1  christos     state = (struct inflate_state FAR *)ZALLOC(strm, 1,
     52  1.1  christos                                                sizeof(struct inflate_state));
     53  1.1  christos     if (state == Z_NULL) return Z_MEM_ERROR;
     54  1.1  christos     Tracev((stderr, "inflate: allocated\n"));
     55  1.1  christos     strm->state = (struct internal_state FAR *)state;
     56  1.1  christos     state->dmax = 32768U;
     57  1.1  christos     state->wbits = windowBits;
     58  1.1  christos     state->wsize = 1U << windowBits;
     59  1.1  christos     state->window = window;
     60  1.1  christos     state->write = 0;
     61  1.1  christos     state->whave = 0;
     62  1.1  christos     return Z_OK;
     63  1.1  christos }
     64  1.1  christos 
     65  1.1  christos /*
     66  1.1  christos    Return state with length and distance decoding tables and index sizes set to
     67  1.1  christos    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
     68  1.1  christos    If BUILDFIXED is defined, then instead this routine builds the tables the
     69  1.1  christos    first time it's called, and returns those tables the first time and
     70  1.1  christos    thereafter.  This reduces the size of the code by about 2K bytes, in
     71  1.1  christos    exchange for a little execution time.  However, BUILDFIXED should not be
     72  1.1  christos    used for threaded applications, since the rewriting of the tables and virgin
     73  1.1  christos    may not be thread-safe.
     74  1.1  christos  */
     75  1.1  christos local void fixedtables(state)
     76  1.1  christos struct inflate_state FAR *state;
     77  1.1  christos {
     78  1.1  christos #ifdef BUILDFIXED
     79  1.1  christos     static int virgin = 1;
     80  1.1  christos     static code *lenfix, *distfix;
     81  1.1  christos     static code fixed[544];
     82  1.1  christos 
     83  1.1  christos     /* build fixed huffman tables if first call (may not be thread safe) */
     84  1.1  christos     if (virgin) {
     85  1.1  christos         unsigned sym, bits;
     86  1.1  christos         static code *next;
     87  1.1  christos 
     88  1.1  christos         /* literal/length table */
     89  1.1  christos         sym = 0;
     90  1.1  christos         while (sym < 144) state->lens[sym++] = 8;
     91  1.1  christos         while (sym < 256) state->lens[sym++] = 9;
     92  1.1  christos         while (sym < 280) state->lens[sym++] = 7;
     93  1.1  christos         while (sym < 288) state->lens[sym++] = 8;
     94  1.1  christos         next = fixed;
     95  1.1  christos         lenfix = next;
     96  1.1  christos         bits = 9;
     97  1.1  christos         inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
     98  1.1  christos 
     99  1.1  christos         /* distance table */
    100  1.1  christos         sym = 0;
    101  1.1  christos         while (sym < 32) state->lens[sym++] = 5;
    102  1.1  christos         distfix = next;
    103  1.1  christos         bits = 5;
    104  1.1  christos         inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
    105  1.1  christos 
    106  1.1  christos         /* do this just once */
    107  1.1  christos         virgin = 0;
    108  1.1  christos     }
    109  1.1  christos #else /* !BUILDFIXED */
    110  1.1  christos #   include "inffixed.h"
    111  1.1  christos #endif /* BUILDFIXED */
    112  1.1  christos     state->lencode = lenfix;
    113  1.1  christos     state->lenbits = 9;
    114  1.1  christos     state->distcode = distfix;
    115  1.1  christos     state->distbits = 5;
    116  1.1  christos }
    117  1.1  christos 
    118  1.1  christos /* Macros for inflateBack(): */
    119  1.1  christos 
    120  1.1  christos /* Load returned state from inflate_fast() */
    121  1.1  christos #define LOAD() \
    122  1.1  christos     do { \
    123  1.1  christos         put = strm->next_out; \
    124  1.1  christos         left = strm->avail_out; \
    125  1.1  christos         next = strm->next_in; \
    126  1.1  christos         have = strm->avail_in; \
    127  1.1  christos         hold = state->hold; \
    128  1.1  christos         bits = state->bits; \
    129  1.1  christos     } while (0)
    130  1.1  christos 
    131  1.1  christos /* Set state from registers for inflate_fast() */
    132  1.1  christos #define RESTORE() \
    133  1.1  christos     do { \
    134  1.1  christos         strm->next_out = put; \
    135  1.1  christos         strm->avail_out = left; \
    136  1.1  christos         strm->next_in = next; \
    137  1.1  christos         strm->avail_in = have; \
    138  1.1  christos         state->hold = hold; \
    139  1.1  christos         state->bits = bits; \
    140  1.1  christos     } while (0)
    141  1.1  christos 
    142  1.1  christos /* Clear the input bit accumulator */
    143  1.1  christos #define INITBITS() \
    144  1.1  christos     do { \
    145  1.1  christos         hold = 0; \
    146  1.1  christos         bits = 0; \
    147  1.1  christos     } while (0)
    148  1.1  christos 
    149  1.1  christos /* Assure that some input is available.  If input is requested, but denied,
    150  1.1  christos    then return a Z_BUF_ERROR from inflateBack(). */
    151  1.1  christos #define PULL() \
    152  1.1  christos     do { \
    153  1.1  christos         if (have == 0) { \
    154  1.1  christos             have = in(in_desc, &next); \
    155  1.1  christos             if (have == 0) { \
    156  1.1  christos                 next = Z_NULL; \
    157  1.1  christos                 ret = Z_BUF_ERROR; \
    158  1.1  christos                 goto inf_leave; \
    159  1.1  christos             } \
    160  1.1  christos         } \
    161  1.1  christos     } while (0)
    162  1.1  christos 
    163  1.1  christos /* Get a byte of input into the bit accumulator, or return from inflateBack()
    164  1.1  christos    with an error if there is no input available. */
    165  1.1  christos #define PULLBYTE() \
    166  1.1  christos     do { \
    167  1.1  christos         PULL(); \
    168  1.1  christos         have--; \
    169  1.1  christos         hold += (unsigned long)(*next++) << bits; \
    170  1.1  christos         bits += 8; \
    171  1.1  christos     } while (0)
    172  1.1  christos 
    173  1.1  christos /* Assure that there are at least n bits in the bit accumulator.  If there is
    174  1.1  christos    not enough available input to do that, then return from inflateBack() with
    175  1.1  christos    an error. */
    176  1.1  christos #define NEEDBITS(n) \
    177  1.1  christos     do { \
    178  1.1  christos         while (bits < (unsigned)(n)) \
    179  1.1  christos             PULLBYTE(); \
    180  1.1  christos     } while (0)
    181  1.1  christos 
    182  1.1  christos /* Return the low n bits of the bit accumulator (n < 16) */
    183  1.1  christos #define BITS(n) \
    184  1.1  christos     ((unsigned)hold & ((1U << (n)) - 1))
    185  1.1  christos 
    186  1.1  christos /* Remove n bits from the bit accumulator */
    187  1.1  christos #define DROPBITS(n) \
    188  1.1  christos     do { \
    189  1.1  christos         hold >>= (n); \
    190  1.1  christos         bits -= (unsigned)(n); \
    191  1.1  christos     } while (0)
    192  1.1  christos 
    193  1.1  christos /* Remove zero to seven bits as needed to go to a byte boundary */
    194  1.1  christos #define BYTEBITS() \
    195  1.1  christos     do { \
    196  1.1  christos         hold >>= bits & 7; \
    197  1.1  christos         bits -= bits & 7; \
    198  1.1  christos     } while (0)
    199  1.1  christos 
    200  1.1  christos /* Assure that some output space is available, by writing out the window
    201  1.1  christos    if it's full.  If the write fails, return from inflateBack() with a
    202  1.1  christos    Z_BUF_ERROR. */
    203  1.1  christos #define ROOM() \
    204  1.1  christos     do { \
    205  1.1  christos         if (left == 0) { \
    206  1.1  christos             put = state->window; \
    207  1.1  christos             left = state->wsize; \
    208  1.1  christos             state->whave = left; \
    209  1.1  christos             if (out(out_desc, put, left)) { \
    210  1.1  christos                 ret = Z_BUF_ERROR; \
    211  1.1  christos                 goto inf_leave; \
    212  1.1  christos             } \
    213  1.1  christos         } \
    214  1.1  christos     } while (0)
    215  1.1  christos 
    216  1.1  christos /*
    217  1.1  christos    strm provides the memory allocation functions and window buffer on input,
    218  1.1  christos    and provides information on the unused input on return.  For Z_DATA_ERROR
    219  1.1  christos    returns, strm will also provide an error message.
    220  1.1  christos 
    221  1.1  christos    in() and out() are the call-back input and output functions.  When
    222  1.1  christos    inflateBack() needs more input, it calls in().  When inflateBack() has
    223  1.1  christos    filled the window with output, or when it completes with data in the
    224  1.1  christos    window, it calls out() to write out the data.  The application must not
    225  1.1  christos    change the provided input until in() is called again or inflateBack()
    226  1.1  christos    returns.  The application must not change the window/output buffer until
    227  1.1  christos    inflateBack() returns.
    228  1.1  christos 
    229  1.1  christos    in() and out() are called with a descriptor parameter provided in the
    230  1.1  christos    inflateBack() call.  This parameter can be a structure that provides the
    231  1.1  christos    information required to do the read or write, as well as accumulated
    232  1.1  christos    information on the input and output such as totals and check values.
    233  1.1  christos 
    234  1.1  christos    in() should return zero on failure.  out() should return non-zero on
    235  1.1  christos    failure.  If either in() or out() fails, than inflateBack() returns a
    236  1.1  christos    Z_BUF_ERROR.  strm->next_in can be checked for Z_NULL to see whether it
    237  1.1  christos    was in() or out() that caused in the error.  Otherwise,  inflateBack()
    238  1.1  christos    returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
    239  1.1  christos    error, or Z_MEM_ERROR if it could not allocate memory for the state.
    240  1.1  christos    inflateBack() can also return Z_STREAM_ERROR if the input parameters
    241  1.1  christos    are not correct, i.e. strm is Z_NULL or the state was not initialized.
    242  1.1  christos  */
    243  1.1  christos int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
    244  1.1  christos z_streamp strm;
    245  1.1  christos in_func in;
    246  1.1  christos void FAR *in_desc;
    247  1.1  christos out_func out;
    248  1.1  christos void FAR *out_desc;
    249  1.1  christos {
    250  1.1  christos     struct inflate_state FAR *state;
    251  1.1  christos     unsigned char FAR *next;    /* next input */
    252  1.1  christos     unsigned char FAR *put;     /* next output */
    253  1.1  christos     unsigned have, left;        /* available input and output */
    254  1.1  christos     unsigned long hold;         /* bit buffer */
    255  1.1  christos     unsigned bits;              /* bits in bit buffer */
    256  1.1  christos     unsigned copy;              /* number of stored or match bytes to copy */
    257  1.1  christos     unsigned char FAR *from;    /* where to copy match bytes from */
    258  1.1  christos     code this;                  /* current decoding table entry */
    259  1.1  christos     code last;                  /* parent table entry */
    260  1.1  christos     unsigned len;               /* length to copy for repeats, bits to drop */
    261  1.1  christos     int ret;                    /* return code */
    262  1.1  christos     static const unsigned short order[19] = /* permutation of code lengths */
    263  1.1  christos         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
    264  1.1  christos 
    265  1.1  christos     /* Check that the strm exists and that the state was initialized */
    266  1.1  christos     if (strm == Z_NULL || strm->state == Z_NULL)
    267  1.1  christos         return Z_STREAM_ERROR;
    268  1.1  christos     state = (struct inflate_state FAR *)strm->state;
    269  1.1  christos 
    270  1.1  christos     /* Reset the state */
    271  1.1  christos     strm->msg = Z_NULL;
    272  1.1  christos     state->mode = TYPE;
    273  1.1  christos     state->last = 0;
    274  1.1  christos     state->whave = 0;
    275  1.1  christos     next = strm->next_in;
    276  1.1  christos     have = next != Z_NULL ? strm->avail_in : 0;
    277  1.1  christos     hold = 0;
    278  1.1  christos     bits = 0;
    279  1.1  christos     put = state->window;
    280  1.1  christos     left = state->wsize;
    281  1.1  christos 
    282  1.1  christos     /* Inflate until end of block marked as last */
    283  1.1  christos     for (;;)
    284  1.1  christos         switch (state->mode) {
    285  1.1  christos         case TYPE:
    286  1.1  christos             /* determine and dispatch block type */
    287  1.1  christos             if (state->last) {
    288  1.1  christos                 BYTEBITS();
    289  1.1  christos                 state->mode = DONE;
    290  1.1  christos                 break;
    291  1.1  christos             }
    292  1.1  christos             NEEDBITS(3);
    293  1.1  christos             state->last = BITS(1);
    294  1.1  christos             DROPBITS(1);
    295  1.1  christos             switch (BITS(2)) {
    296  1.1  christos             case 0:                             /* stored block */
    297  1.1  christos                 Tracev((stderr, "inflate:     stored block%s\n",
    298  1.1  christos                         state->last ? " (last)" : ""));
    299  1.1  christos                 state->mode = STORED;
    300  1.1  christos                 break;
    301  1.1  christos             case 1:                             /* fixed block */
    302  1.1  christos                 fixedtables(state);
    303  1.1  christos                 Tracev((stderr, "inflate:     fixed codes block%s\n",
    304  1.1  christos                         state->last ? " (last)" : ""));
    305  1.1  christos                 state->mode = LEN;              /* decode codes */
    306  1.1  christos                 break;
    307  1.1  christos             case 2:                             /* dynamic block */
    308  1.1  christos                 Tracev((stderr, "inflate:     dynamic codes block%s\n",
    309  1.1  christos                         state->last ? " (last)" : ""));
    310  1.1  christos                 state->mode = TABLE;
    311  1.1  christos                 break;
    312  1.1  christos             case 3:
    313  1.1  christos                 strm->msg = (char *)"invalid block type";
    314  1.1  christos                 state->mode = BAD;
    315  1.1  christos             }
    316  1.1  christos             DROPBITS(2);
    317  1.1  christos             break;
    318  1.1  christos 
    319  1.1  christos         case STORED:
    320  1.1  christos             /* get and verify stored block length */
    321  1.1  christos             BYTEBITS();                         /* go to byte boundary */
    322  1.1  christos             NEEDBITS(32);
    323  1.1  christos             if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
    324  1.1  christos                 strm->msg = (char *)"invalid stored block lengths";
    325  1.1  christos                 state->mode = BAD;
    326  1.1  christos                 break;
    327  1.1  christos             }
    328  1.1  christos             state->length = (unsigned)hold & 0xffff;
    329  1.1  christos             Tracev((stderr, "inflate:       stored length %u\n",
    330  1.1  christos                     state->length));
    331  1.1  christos             INITBITS();
    332  1.1  christos 
    333  1.1  christos             /* copy stored block from input to output */
    334  1.1  christos             while (state->length != 0) {
    335  1.1  christos                 copy = state->length;
    336  1.1  christos                 PULL();
    337  1.1  christos                 ROOM();
    338  1.1  christos                 if (copy > have) copy = have;
    339  1.1  christos                 if (copy > left) copy = left;
    340  1.1  christos                 zmemcpy(put, next, copy);
    341  1.1  christos                 have -= copy;
    342  1.1  christos                 next += copy;
    343  1.1  christos                 left -= copy;
    344  1.1  christos                 put += copy;
    345  1.1  christos                 state->length -= copy;
    346  1.1  christos             }
    347  1.1  christos             Tracev((stderr, "inflate:       stored end\n"));
    348  1.1  christos             state->mode = TYPE;
    349  1.1  christos             break;
    350  1.1  christos 
    351  1.1  christos         case TABLE:
    352  1.1  christos             /* get dynamic table entries descriptor */
    353  1.1  christos             NEEDBITS(14);
    354  1.1  christos             state->nlen = BITS(5) + 257;
    355  1.1  christos             DROPBITS(5);
    356  1.1  christos             state->ndist = BITS(5) + 1;
    357  1.1  christos             DROPBITS(5);
    358  1.1  christos             state->ncode = BITS(4) + 4;
    359  1.1  christos             DROPBITS(4);
    360  1.1  christos #ifndef PKZIP_BUG_WORKAROUND
    361  1.1  christos             if (state->nlen > 286 || state->ndist > 30) {
    362  1.1  christos                 strm->msg = (char *)"too many length or distance symbols";
    363  1.1  christos                 state->mode = BAD;
    364  1.1  christos                 break;
    365  1.1  christos             }
    366  1.1  christos #endif
    367  1.1  christos             Tracev((stderr, "inflate:       table sizes ok\n"));
    368  1.1  christos 
    369  1.1  christos             /* get code length code lengths (not a typo) */
    370  1.1  christos             state->have = 0;
    371  1.1  christos             while (state->have < state->ncode) {
    372  1.1  christos                 NEEDBITS(3);
    373  1.1  christos                 state->lens[order[state->have++]] = (unsigned short)BITS(3);
    374  1.1  christos                 DROPBITS(3);
    375  1.1  christos             }
    376  1.1  christos             while (state->have < 19)
    377  1.1  christos                 state->lens[order[state->have++]] = 0;
    378  1.1  christos             state->next = state->codes;
    379  1.1  christos             state->lencode = (code const FAR *)(state->next);
    380  1.1  christos             state->lenbits = 7;
    381  1.1  christos             ret = inflate_table(CODES, state->lens, 19, &(state->next),
    382  1.1  christos                                 &(state->lenbits), state->work);
    383  1.1  christos             if (ret) {
    384  1.1  christos                 strm->msg = (char *)"invalid code lengths set";
    385  1.1  christos                 state->mode = BAD;
    386  1.1  christos                 break;
    387  1.1  christos             }
    388  1.1  christos             Tracev((stderr, "inflate:       code lengths ok\n"));
    389  1.1  christos 
    390  1.1  christos             /* get length and distance code code lengths */
    391  1.1  christos             state->have = 0;
    392  1.1  christos             while (state->have < state->nlen + state->ndist) {
    393  1.1  christos                 for (;;) {
    394  1.1  christos                     this = state->lencode[BITS(state->lenbits)];
    395  1.1  christos                     if ((unsigned)(this.bits) <= bits) break;
    396  1.1  christos                     PULLBYTE();
    397  1.1  christos                 }
    398  1.1  christos                 if (this.val < 16) {
    399  1.1  christos                     NEEDBITS(this.bits);
    400  1.1  christos                     DROPBITS(this.bits);
    401  1.1  christos                     state->lens[state->have++] = this.val;
    402  1.1  christos                 }
    403  1.1  christos                 else {
    404  1.1  christos                     if (this.val == 16) {
    405  1.1  christos                         NEEDBITS(this.bits + 2);
    406  1.1  christos                         DROPBITS(this.bits);
    407  1.1  christos                         if (state->have == 0) {
    408  1.1  christos                             strm->msg = (char *)"invalid bit length repeat";
    409  1.1  christos                             state->mode = BAD;
    410  1.1  christos                             break;
    411  1.1  christos                         }
    412  1.1  christos                         len = (unsigned)(state->lens[state->have - 1]);
    413  1.1  christos                         copy = 3 + BITS(2);
    414  1.1  christos                         DROPBITS(2);
    415  1.1  christos                     }
    416  1.1  christos                     else if (this.val == 17) {
    417  1.1  christos                         NEEDBITS(this.bits + 3);
    418  1.1  christos                         DROPBITS(this.bits);
    419  1.1  christos                         len = 0;
    420  1.1  christos                         copy = 3 + BITS(3);
    421  1.1  christos                         DROPBITS(3);
    422  1.1  christos                     }
    423  1.1  christos                     else {
    424  1.1  christos                         NEEDBITS(this.bits + 7);
    425  1.1  christos                         DROPBITS(this.bits);
    426  1.1  christos                         len = 0;
    427  1.1  christos                         copy = 11 + BITS(7);
    428  1.1  christos                         DROPBITS(7);
    429  1.1  christos                     }
    430  1.1  christos                     if (state->have + copy > state->nlen + state->ndist) {
    431  1.1  christos                         strm->msg = (char *)"invalid bit length repeat";
    432  1.1  christos                         state->mode = BAD;
    433  1.1  christos                         break;
    434  1.1  christos                     }
    435  1.1  christos                     while (copy--)
    436  1.1  christos                         state->lens[state->have++] = (unsigned short)len;
    437  1.1  christos                 }
    438  1.1  christos             }
    439  1.1  christos 
    440  1.1  christos             /* handle error breaks in while */
    441  1.1  christos             if (state->mode == BAD) break;
    442  1.1  christos 
    443  1.1  christos             /* build code tables */
    444  1.1  christos             state->next = state->codes;
    445  1.1  christos             state->lencode = (code const FAR *)(state->next);
    446  1.1  christos             state->lenbits = 9;
    447  1.1  christos             ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
    448  1.1  christos                                 &(state->lenbits), state->work);
    449  1.1  christos             if (ret) {
    450  1.1  christos                 strm->msg = (char *)"invalid literal/lengths set";
    451  1.1  christos                 state->mode = BAD;
    452  1.1  christos                 break;
    453  1.1  christos             }
    454  1.1  christos             state->distcode = (code const FAR *)(state->next);
    455  1.1  christos             state->distbits = 6;
    456  1.1  christos             ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
    457  1.1  christos                             &(state->next), &(state->distbits), state->work);
    458  1.1  christos             if (ret) {
    459  1.1  christos                 strm->msg = (char *)"invalid distances set";
    460  1.1  christos                 state->mode = BAD;
    461  1.1  christos                 break;
    462  1.1  christos             }
    463  1.1  christos             Tracev((stderr, "inflate:       codes ok\n"));
    464  1.1  christos             state->mode = LEN;
    465  1.1  christos 
    466  1.1  christos         case LEN:
    467  1.1  christos             /* use inflate_fast() if we have enough input and output */
    468  1.1  christos             if (have >= 6 && left >= 258) {
    469  1.1  christos                 RESTORE();
    470  1.1  christos                 if (state->whave < state->wsize)
    471  1.1  christos                     state->whave = state->wsize - left;
    472  1.1  christos                 inflate_fast(strm, state->wsize);
    473  1.1  christos                 LOAD();
    474  1.1  christos                 break;
    475  1.1  christos             }
    476  1.1  christos 
    477  1.1  christos             /* get a literal, length, or end-of-block code */
    478  1.1  christos             for (;;) {
    479  1.1  christos                 this = state->lencode[BITS(state->lenbits)];
    480  1.1  christos                 if ((unsigned)(this.bits) <= bits) break;
    481  1.1  christos                 PULLBYTE();
    482  1.1  christos             }
    483  1.1  christos             if (this.op && (this.op & 0xf0) == 0) {
    484  1.1  christos                 last = this;
    485  1.1  christos                 for (;;) {
    486  1.1  christos                     this = state->lencode[last.val +
    487  1.1  christos                             (BITS(last.bits + last.op) >> last.bits)];
    488  1.1  christos                     if ((unsigned)(last.bits + this.bits) <= bits) break;
    489  1.1  christos                     PULLBYTE();
    490  1.1  christos                 }
    491  1.1  christos                 DROPBITS(last.bits);
    492  1.1  christos             }
    493  1.1  christos             DROPBITS(this.bits);
    494  1.1  christos             state->length = (unsigned)this.val;
    495  1.1  christos 
    496  1.1  christos             /* process literal */
    497  1.1  christos             if (this.op == 0) {
    498  1.1  christos                 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
    499  1.1  christos                         "inflate:         literal '%c'\n" :
    500  1.1  christos                         "inflate:         literal 0x%02x\n", this.val));
    501  1.1  christos                 ROOM();
    502  1.1  christos                 *put++ = (unsigned char)(state->length);
    503  1.1  christos                 left--;
    504  1.1  christos                 state->mode = LEN;
    505  1.1  christos                 break;
    506  1.1  christos             }
    507  1.1  christos 
    508  1.1  christos             /* process end of block */
    509  1.1  christos             if (this.op & 32) {
    510  1.1  christos                 Tracevv((stderr, "inflate:         end of block\n"));
    511  1.1  christos                 state->mode = TYPE;
    512  1.1  christos                 break;
    513  1.1  christos             }
    514  1.1  christos 
    515  1.1  christos             /* invalid code */
    516  1.1  christos             if (this.op & 64) {
    517  1.1  christos                 strm->msg = (char *)"invalid literal/length code";
    518  1.1  christos                 state->mode = BAD;
    519  1.1  christos                 break;
    520  1.1  christos             }
    521  1.1  christos 
    522  1.1  christos             /* length code -- get extra bits, if any */
    523  1.1  christos             state->extra = (unsigned)(this.op) & 15;
    524  1.1  christos             if (state->extra != 0) {
    525  1.1  christos                 NEEDBITS(state->extra);
    526  1.1  christos                 state->length += BITS(state->extra);
    527  1.1  christos                 DROPBITS(state->extra);
    528  1.1  christos             }
    529  1.1  christos             Tracevv((stderr, "inflate:         length %u\n", state->length));
    530  1.1  christos 
    531  1.1  christos             /* get distance code */
    532  1.1  christos             for (;;) {
    533  1.1  christos                 this = state->distcode[BITS(state->distbits)];
    534  1.1  christos                 if ((unsigned)(this.bits) <= bits) break;
    535  1.1  christos                 PULLBYTE();
    536  1.1  christos             }
    537  1.1  christos             if ((this.op & 0xf0) == 0) {
    538  1.1  christos                 last = this;
    539  1.1  christos                 for (;;) {
    540  1.1  christos                     this = state->distcode[last.val +
    541  1.1  christos                             (BITS(last.bits + last.op) >> last.bits)];
    542  1.1  christos                     if ((unsigned)(last.bits + this.bits) <= bits) break;
    543  1.1  christos                     PULLBYTE();
    544  1.1  christos                 }
    545  1.1  christos                 DROPBITS(last.bits);
    546  1.1  christos             }
    547  1.1  christos             DROPBITS(this.bits);
    548  1.1  christos             if (this.op & 64) {
    549  1.1  christos                 strm->msg = (char *)"invalid distance code";
    550  1.1  christos                 state->mode = BAD;
    551  1.1  christos                 break;
    552  1.1  christos             }
    553  1.1  christos             state->offset = (unsigned)this.val;
    554  1.1  christos 
    555  1.1  christos             /* get distance extra bits, if any */
    556  1.1  christos             state->extra = (unsigned)(this.op) & 15;
    557  1.1  christos             if (state->extra != 0) {
    558  1.1  christos                 NEEDBITS(state->extra);
    559  1.1  christos                 state->offset += BITS(state->extra);
    560  1.1  christos                 DROPBITS(state->extra);
    561  1.1  christos             }
    562  1.1  christos             if (state->offset > state->wsize - (state->whave < state->wsize ?
    563  1.1  christos                                                 left : 0)) {
    564  1.1  christos                 strm->msg = (char *)"invalid distance too far back";
    565  1.1  christos                 state->mode = BAD;
    566  1.1  christos                 break;
    567  1.1  christos             }
    568  1.1  christos             Tracevv((stderr, "inflate:         distance %u\n", state->offset));
    569  1.1  christos 
    570  1.1  christos             /* copy match from window to output */
    571  1.1  christos             do {
    572  1.1  christos                 ROOM();
    573  1.1  christos                 copy = state->wsize - state->offset;
    574  1.1  christos                 if (copy < left) {
    575  1.1  christos                     from = put + copy;
    576  1.1  christos                     copy = left - copy;
    577  1.1  christos                 }
    578  1.1  christos                 else {
    579  1.1  christos                     from = put - state->offset;
    580  1.1  christos                     copy = left;
    581  1.1  christos                 }
    582  1.1  christos                 if (copy > state->length) copy = state->length;
    583  1.1  christos                 state->length -= copy;
    584  1.1  christos                 left -= copy;
    585  1.1  christos                 do {
    586  1.1  christos                     *put++ = *from++;
    587  1.1  christos                 } while (--copy);
    588  1.1  christos             } while (state->length != 0);
    589  1.1  christos             break;
    590  1.1  christos 
    591  1.1  christos         case DONE:
    592  1.1  christos             /* inflate stream terminated properly -- write leftover output */
    593  1.1  christos             ret = Z_STREAM_END;
    594  1.1  christos             if (left < state->wsize) {
    595  1.1  christos                 if (out(out_desc, state->window, state->wsize - left))
    596  1.1  christos                     ret = Z_BUF_ERROR;
    597  1.1  christos             }
    598  1.1  christos             goto inf_leave;
    599  1.1  christos 
    600  1.1  christos         case BAD:
    601  1.1  christos             ret = Z_DATA_ERROR;
    602  1.1  christos             goto inf_leave;
    603  1.1  christos 
    604  1.1  christos         default:                /* can't happen, but makes compilers happy */
    605  1.1  christos             ret = Z_STREAM_ERROR;
    606  1.1  christos             goto inf_leave;
    607  1.1  christos         }
    608  1.1  christos 
    609  1.1  christos     /* Return unused input */
    610  1.1  christos   inf_leave:
    611  1.1  christos     strm->next_in = next;
    612  1.1  christos     strm->avail_in = have;
    613  1.1  christos     return ret;
    614  1.1  christos }
    615  1.1  christos 
    616  1.1  christos int ZEXPORT inflateBackEnd(strm)
    617  1.1  christos z_streamp strm;
    618  1.1  christos {
    619  1.1  christos     if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
    620  1.1  christos         return Z_STREAM_ERROR;
    621  1.1  christos     ZFREE(strm, strm->state);
    622  1.1  christos     strm->state = Z_NULL;
    623  1.1  christos     Tracev((stderr, "inflate: end\n"));
    624  1.1  christos     return Z_OK;
    625  1.1  christos }
    626