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