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