Home | History | Annotate | Line # | Download | only in zlib
inflate.c revision 1.1.1.4
      1      1.1  christos /* inflate.c -- zlib decompression
      2  1.1.1.3  christos  * Copyright (C) 1995-2022 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 /*
      7      1.1  christos  * Change history:
      8      1.1  christos  *
      9      1.1  christos  * 1.2.beta0    24 Nov 2002
     10      1.1  christos  * - First version -- complete rewrite of inflate to simplify code, avoid
     11      1.1  christos  *   creation of window when not needed, minimize use of window when it is
     12      1.1  christos  *   needed, make inffast.c even faster, implement gzip decoding, and to
     13      1.1  christos  *   improve code readability and style over the previous zlib inflate code
     14      1.1  christos  *
     15      1.1  christos  * 1.2.beta1    25 Nov 2002
     16      1.1  christos  * - Use pointers for available input and output checking in inffast.c
     17      1.1  christos  * - Remove input and output counters in inffast.c
     18      1.1  christos  * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
     19      1.1  christos  * - Remove unnecessary second byte pull from length extra in inffast.c
     20      1.1  christos  * - Unroll direct copy to three copies per loop in inffast.c
     21      1.1  christos  *
     22      1.1  christos  * 1.2.beta2    4 Dec 2002
     23      1.1  christos  * - Change external routine names to reduce potential conflicts
     24      1.1  christos  * - Correct filename to inffixed.h for fixed tables in inflate.c
     25      1.1  christos  * - Make hbuf[] unsigned char to match parameter type in inflate.c
     26      1.1  christos  * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
     27      1.1  christos  *   to avoid negation problem on Alphas (64 bit) in inflate.c
     28      1.1  christos  *
     29      1.1  christos  * 1.2.beta3    22 Dec 2002
     30      1.1  christos  * - Add comments on state->bits assertion in inffast.c
     31      1.1  christos  * - Add comments on op field in inftrees.h
     32      1.1  christos  * - Fix bug in reuse of allocated window after inflateReset()
     33      1.1  christos  * - Remove bit fields--back to byte structure for speed
     34      1.1  christos  * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
     35      1.1  christos  * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
     36      1.1  christos  * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
     37      1.1  christos  * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
     38      1.1  christos  * - Use local copies of stream next and avail values, as well as local bit
     39      1.1  christos  *   buffer and bit count in inflate()--for speed when inflate_fast() not used
     40      1.1  christos  *
     41      1.1  christos  * 1.2.beta4    1 Jan 2003
     42      1.1  christos  * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
     43      1.1  christos  * - Move a comment on output buffer sizes from inffast.c to inflate.c
     44      1.1  christos  * - Add comments in inffast.c to introduce the inflate_fast() routine
     45      1.1  christos  * - Rearrange window copies in inflate_fast() for speed and simplification
     46      1.1  christos  * - Unroll last copy for window match in inflate_fast()
     47      1.1  christos  * - Use local copies of window variables in inflate_fast() for speed
     48  1.1.1.2  christos  * - Pull out common wnext == 0 case for speed in inflate_fast()
     49      1.1  christos  * - Make op and len in inflate_fast() unsigned for consistency
     50      1.1  christos  * - Add FAR to lcode and dcode declarations in inflate_fast()
     51      1.1  christos  * - Simplified bad distance check in inflate_fast()
     52      1.1  christos  * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
     53      1.1  christos  *   source file infback.c to provide a call-back interface to inflate for
     54      1.1  christos  *   programs like gzip and unzip -- uses window as output buffer to avoid
     55      1.1  christos  *   window copying
     56      1.1  christos  *
     57      1.1  christos  * 1.2.beta5    1 Jan 2003
     58      1.1  christos  * - Improved inflateBack() interface to allow the caller to provide initial
     59      1.1  christos  *   input in strm.
     60      1.1  christos  * - Fixed stored blocks bug in inflateBack()
     61      1.1  christos  *
     62      1.1  christos  * 1.2.beta6    4 Jan 2003
     63      1.1  christos  * - Added comments in inffast.c on effectiveness of POSTINC
     64      1.1  christos  * - Typecasting all around to reduce compiler warnings
     65      1.1  christos  * - Changed loops from while (1) or do {} while (1) to for (;;), again to
     66      1.1  christos  *   make compilers happy
     67      1.1  christos  * - Changed type of window in inflateBackInit() to unsigned char *
     68      1.1  christos  *
     69      1.1  christos  * 1.2.beta7    27 Jan 2003
     70      1.1  christos  * - Changed many types to unsigned or unsigned short to avoid warnings
     71      1.1  christos  * - Added inflateCopy() function
     72      1.1  christos  *
     73      1.1  christos  * 1.2.0        9 Mar 2003
     74      1.1  christos  * - Changed inflateBack() interface to provide separate opaque descriptors
     75      1.1  christos  *   for the in() and out() functions
     76      1.1  christos  * - Changed inflateBack() argument and in_func typedef to swap the length
     77      1.1  christos  *   and buffer address return values for the input function
     78      1.1  christos  * - Check next_in and next_out for Z_NULL on entry to inflate()
     79      1.1  christos  *
     80      1.1  christos  * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
     81      1.1  christos  */
     82      1.1  christos 
     83      1.1  christos #include "zutil.h"
     84      1.1  christos #include "inftrees.h"
     85      1.1  christos #include "inflate.h"
     86      1.1  christos #include "inffast.h"
     87      1.1  christos 
     88      1.1  christos #ifdef MAKEFIXED
     89      1.1  christos #  ifndef BUILDFIXED
     90      1.1  christos #    define BUILDFIXED
     91      1.1  christos #  endif
     92      1.1  christos #endif
     93      1.1  christos 
     94  1.1.1.4  christos local int inflateStateCheck(z_streamp strm) {
     95  1.1.1.2  christos     struct inflate_state FAR *state;
     96  1.1.1.2  christos     if (strm == Z_NULL ||
     97  1.1.1.2  christos         strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
     98  1.1.1.2  christos         return 1;
     99  1.1.1.2  christos     state = (struct inflate_state FAR *)strm->state;
    100  1.1.1.2  christos     if (state == Z_NULL || state->strm != strm ||
    101  1.1.1.2  christos         state->mode < HEAD || state->mode > SYNC)
    102  1.1.1.2  christos         return 1;
    103  1.1.1.2  christos     return 0;
    104  1.1.1.2  christos }
    105  1.1.1.2  christos 
    106  1.1.1.4  christos int ZEXPORT inflateResetKeep(z_streamp strm) {
    107      1.1  christos     struct inflate_state FAR *state;
    108      1.1  christos 
    109  1.1.1.2  christos     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
    110      1.1  christos     state = (struct inflate_state FAR *)strm->state;
    111      1.1  christos     strm->total_in = strm->total_out = state->total = 0;
    112      1.1  christos     strm->msg = Z_NULL;
    113  1.1.1.2  christos     if (state->wrap)        /* to support ill-conceived Java test suite */
    114  1.1.1.2  christos         strm->adler = state->wrap & 1;
    115      1.1  christos     state->mode = HEAD;
    116      1.1  christos     state->last = 0;
    117      1.1  christos     state->havedict = 0;
    118  1.1.1.3  christos     state->flags = -1;
    119      1.1  christos     state->dmax = 32768U;
    120      1.1  christos     state->head = Z_NULL;
    121      1.1  christos     state->hold = 0;
    122      1.1  christos     state->bits = 0;
    123      1.1  christos     state->lencode = state->distcode = state->next = state->codes;
    124  1.1.1.2  christos     state->sane = 1;
    125  1.1.1.2  christos     state->back = -1;
    126      1.1  christos     Tracev((stderr, "inflate: reset\n"));
    127      1.1  christos     return Z_OK;
    128      1.1  christos }
    129      1.1  christos 
    130  1.1.1.4  christos int ZEXPORT inflateReset(z_streamp strm) {
    131      1.1  christos     struct inflate_state FAR *state;
    132      1.1  christos 
    133  1.1.1.2  christos     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
    134      1.1  christos     state = (struct inflate_state FAR *)strm->state;
    135  1.1.1.2  christos     state->wsize = 0;
    136  1.1.1.2  christos     state->whave = 0;
    137  1.1.1.2  christos     state->wnext = 0;
    138  1.1.1.2  christos     return inflateResetKeep(strm);
    139  1.1.1.2  christos }
    140  1.1.1.2  christos 
    141  1.1.1.4  christos int ZEXPORT inflateReset2(z_streamp strm, int windowBits) {
    142  1.1.1.2  christos     int wrap;
    143  1.1.1.2  christos     struct inflate_state FAR *state;
    144  1.1.1.2  christos 
    145  1.1.1.2  christos     /* get the state */
    146  1.1.1.2  christos     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
    147  1.1.1.2  christos     state = (struct inflate_state FAR *)strm->state;
    148  1.1.1.2  christos 
    149  1.1.1.2  christos     /* extract wrap request from windowBits parameter */
    150  1.1.1.2  christos     if (windowBits < 0) {
    151  1.1.1.3  christos         if (windowBits < -15)
    152  1.1.1.3  christos             return Z_STREAM_ERROR;
    153  1.1.1.2  christos         wrap = 0;
    154  1.1.1.2  christos         windowBits = -windowBits;
    155  1.1.1.2  christos     }
    156  1.1.1.2  christos     else {
    157  1.1.1.2  christos         wrap = (windowBits >> 4) + 5;
    158  1.1.1.2  christos #ifdef GUNZIP
    159  1.1.1.2  christos         if (windowBits < 48)
    160  1.1.1.2  christos             windowBits &= 15;
    161  1.1.1.2  christos #endif
    162  1.1.1.2  christos     }
    163  1.1.1.2  christos 
    164  1.1.1.2  christos     /* set number of window bits, free window if different */
    165  1.1.1.2  christos     if (windowBits && (windowBits < 8 || windowBits > 15))
    166  1.1.1.2  christos         return Z_STREAM_ERROR;
    167  1.1.1.2  christos     if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
    168  1.1.1.2  christos         ZFREE(strm, state->window);
    169  1.1.1.2  christos         state->window = Z_NULL;
    170  1.1.1.2  christos     }
    171  1.1.1.2  christos 
    172  1.1.1.2  christos     /* update state and reset the rest of it */
    173  1.1.1.2  christos     state->wrap = wrap;
    174  1.1.1.2  christos     state->wbits = (unsigned)windowBits;
    175  1.1.1.2  christos     return inflateReset(strm);
    176      1.1  christos }
    177      1.1  christos 
    178  1.1.1.4  christos int ZEXPORT inflateInit2_(z_streamp strm, int windowBits,
    179  1.1.1.4  christos                           const char *version, int stream_size) {
    180  1.1.1.2  christos     int ret;
    181      1.1  christos     struct inflate_state FAR *state;
    182      1.1  christos 
    183      1.1  christos     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
    184      1.1  christos         stream_size != (int)(sizeof(z_stream)))
    185      1.1  christos         return Z_VERSION_ERROR;
    186      1.1  christos     if (strm == Z_NULL) return Z_STREAM_ERROR;
    187      1.1  christos     strm->msg = Z_NULL;                 /* in case we return an error */
    188      1.1  christos     if (strm->zalloc == (alloc_func)0) {
    189  1.1.1.2  christos #ifdef Z_SOLO
    190  1.1.1.2  christos         return Z_STREAM_ERROR;
    191  1.1.1.2  christos #else
    192      1.1  christos         strm->zalloc = zcalloc;
    193      1.1  christos         strm->opaque = (voidpf)0;
    194  1.1.1.2  christos #endif
    195      1.1  christos     }
    196  1.1.1.2  christos     if (strm->zfree == (free_func)0)
    197  1.1.1.2  christos #ifdef Z_SOLO
    198  1.1.1.2  christos         return Z_STREAM_ERROR;
    199  1.1.1.2  christos #else
    200  1.1.1.2  christos         strm->zfree = zcfree;
    201  1.1.1.2  christos #endif
    202      1.1  christos     state = (struct inflate_state FAR *)
    203      1.1  christos             ZALLOC(strm, 1, sizeof(struct inflate_state));
    204      1.1  christos     if (state == Z_NULL) return Z_MEM_ERROR;
    205      1.1  christos     Tracev((stderr, "inflate: allocated\n"));
    206      1.1  christos     strm->state = (struct internal_state FAR *)state;
    207  1.1.1.2  christos     state->strm = strm;
    208  1.1.1.2  christos     state->window = Z_NULL;
    209  1.1.1.2  christos     state->mode = HEAD;     /* to pass state test in inflateReset2() */
    210  1.1.1.2  christos     ret = inflateReset2(strm, windowBits);
    211  1.1.1.2  christos     if (ret != Z_OK) {
    212      1.1  christos         ZFREE(strm, state);
    213      1.1  christos         strm->state = Z_NULL;
    214      1.1  christos     }
    215  1.1.1.2  christos     return ret;
    216      1.1  christos }
    217      1.1  christos 
    218  1.1.1.4  christos int ZEXPORT inflateInit_(z_streamp strm, const char *version,
    219  1.1.1.4  christos                          int stream_size) {
    220      1.1  christos     return inflateInit2_(strm, DEF_WBITS, version, stream_size);
    221      1.1  christos }
    222      1.1  christos 
    223  1.1.1.4  christos int ZEXPORT inflatePrime(z_streamp strm, int bits, int value) {
    224  1.1.1.2  christos     struct inflate_state FAR *state;
    225  1.1.1.2  christos 
    226  1.1.1.2  christos     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
    227  1.1.1.4  christos     if (bits == 0)
    228  1.1.1.4  christos         return Z_OK;
    229  1.1.1.2  christos     state = (struct inflate_state FAR *)strm->state;
    230  1.1.1.2  christos     if (bits < 0) {
    231  1.1.1.2  christos         state->hold = 0;
    232  1.1.1.2  christos         state->bits = 0;
    233  1.1.1.2  christos         return Z_OK;
    234  1.1.1.2  christos     }
    235  1.1.1.2  christos     if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
    236  1.1.1.2  christos     value &= (1L << bits) - 1;
    237  1.1.1.2  christos     state->hold += (unsigned)value << state->bits;
    238  1.1.1.2  christos     state->bits += (uInt)bits;
    239  1.1.1.2  christos     return Z_OK;
    240  1.1.1.2  christos }
    241  1.1.1.2  christos 
    242      1.1  christos /*
    243      1.1  christos    Return state with length and distance decoding tables and index sizes set to
    244      1.1  christos    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
    245      1.1  christos    If BUILDFIXED is defined, then instead this routine builds the tables the
    246      1.1  christos    first time it's called, and returns those tables the first time and
    247      1.1  christos    thereafter.  This reduces the size of the code by about 2K bytes, in
    248      1.1  christos    exchange for a little execution time.  However, BUILDFIXED should not be
    249      1.1  christos    used for threaded applications, since the rewriting of the tables and virgin
    250      1.1  christos    may not be thread-safe.
    251      1.1  christos  */
    252  1.1.1.4  christos local void fixedtables(struct inflate_state FAR *state) {
    253      1.1  christos #ifdef BUILDFIXED
    254      1.1  christos     static int virgin = 1;
    255      1.1  christos     static code *lenfix, *distfix;
    256      1.1  christos     static code fixed[544];
    257      1.1  christos 
    258      1.1  christos     /* build fixed huffman tables if first call (may not be thread safe) */
    259      1.1  christos     if (virgin) {
    260      1.1  christos         unsigned sym, bits;
    261      1.1  christos         static code *next;
    262      1.1  christos 
    263      1.1  christos         /* literal/length table */
    264      1.1  christos         sym = 0;
    265      1.1  christos         while (sym < 144) state->lens[sym++] = 8;
    266      1.1  christos         while (sym < 256) state->lens[sym++] = 9;
    267      1.1  christos         while (sym < 280) state->lens[sym++] = 7;
    268      1.1  christos         while (sym < 288) state->lens[sym++] = 8;
    269      1.1  christos         next = fixed;
    270      1.1  christos         lenfix = next;
    271      1.1  christos         bits = 9;
    272      1.1  christos         inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
    273      1.1  christos 
    274      1.1  christos         /* distance table */
    275      1.1  christos         sym = 0;
    276      1.1  christos         while (sym < 32) state->lens[sym++] = 5;
    277      1.1  christos         distfix = next;
    278      1.1  christos         bits = 5;
    279      1.1  christos         inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
    280      1.1  christos 
    281      1.1  christos         /* do this just once */
    282      1.1  christos         virgin = 0;
    283      1.1  christos     }
    284      1.1  christos #else /* !BUILDFIXED */
    285      1.1  christos #   include "inffixed.h"
    286      1.1  christos #endif /* BUILDFIXED */
    287      1.1  christos     state->lencode = lenfix;
    288      1.1  christos     state->lenbits = 9;
    289      1.1  christos     state->distcode = distfix;
    290      1.1  christos     state->distbits = 5;
    291      1.1  christos }
    292      1.1  christos 
    293      1.1  christos #ifdef MAKEFIXED
    294      1.1  christos #include <stdio.h>
    295      1.1  christos 
    296      1.1  christos /*
    297      1.1  christos    Write out the inffixed.h that is #include'd above.  Defining MAKEFIXED also
    298      1.1  christos    defines BUILDFIXED, so the tables are built on the fly.  makefixed() writes
    299      1.1  christos    those tables to stdout, which would be piped to inffixed.h.  A small program
    300      1.1  christos    can simply call makefixed to do this:
    301      1.1  christos 
    302      1.1  christos     void makefixed(void);
    303      1.1  christos 
    304      1.1  christos     int main(void)
    305      1.1  christos     {
    306      1.1  christos         makefixed();
    307      1.1  christos         return 0;
    308      1.1  christos     }
    309      1.1  christos 
    310      1.1  christos    Then that can be linked with zlib built with MAKEFIXED defined and run:
    311      1.1  christos 
    312      1.1  christos     a.out > inffixed.h
    313      1.1  christos  */
    314  1.1.1.4  christos void makefixed(void)
    315      1.1  christos {
    316      1.1  christos     unsigned low, size;
    317      1.1  christos     struct inflate_state state;
    318      1.1  christos 
    319      1.1  christos     fixedtables(&state);
    320      1.1  christos     puts("    /* inffixed.h -- table for decoding fixed codes");
    321      1.1  christos     puts("     * Generated automatically by makefixed().");
    322      1.1  christos     puts("     */");
    323      1.1  christos     puts("");
    324      1.1  christos     puts("    /* WARNING: this file should *not* be used by applications.");
    325      1.1  christos     puts("       It is part of the implementation of this library and is");
    326      1.1  christos     puts("       subject to change. Applications should only use zlib.h.");
    327      1.1  christos     puts("     */");
    328      1.1  christos     puts("");
    329      1.1  christos     size = 1U << 9;
    330      1.1  christos     printf("    static const code lenfix[%u] = {", size);
    331      1.1  christos     low = 0;
    332      1.1  christos     for (;;) {
    333      1.1  christos         if ((low % 7) == 0) printf("\n        ");
    334  1.1.1.2  christos         printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
    335  1.1.1.2  christos                state.lencode[low].bits, state.lencode[low].val);
    336      1.1  christos         if (++low == size) break;
    337      1.1  christos         putchar(',');
    338      1.1  christos     }
    339      1.1  christos     puts("\n    };");
    340      1.1  christos     size = 1U << 5;
    341      1.1  christos     printf("\n    static const code distfix[%u] = {", size);
    342      1.1  christos     low = 0;
    343      1.1  christos     for (;;) {
    344      1.1  christos         if ((low % 6) == 0) printf("\n        ");
    345      1.1  christos         printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
    346      1.1  christos                state.distcode[low].val);
    347      1.1  christos         if (++low == size) break;
    348      1.1  christos         putchar(',');
    349      1.1  christos     }
    350      1.1  christos     puts("\n    };");
    351      1.1  christos }
    352      1.1  christos #endif /* MAKEFIXED */
    353      1.1  christos 
    354      1.1  christos /*
    355      1.1  christos    Update the window with the last wsize (normally 32K) bytes written before
    356      1.1  christos    returning.  If window does not exist yet, create it.  This is only called
    357      1.1  christos    when a window is already in use, or when output has been written during this
    358      1.1  christos    inflate call, but the end of the deflate stream has not been reached yet.
    359      1.1  christos    It is also called to create a window for dictionary data when a dictionary
    360      1.1  christos    is loaded.
    361      1.1  christos 
    362      1.1  christos    Providing output buffers larger than 32K to inflate() should provide a speed
    363      1.1  christos    advantage, since only the last 32K of output is copied to the sliding window
    364      1.1  christos    upon return from inflate(), and since all distances after the first 32K of
    365      1.1  christos    output will fall in the output data, making match copies simpler and faster.
    366      1.1  christos    The advantage may be dependent on the size of the processor's data caches.
    367      1.1  christos  */
    368  1.1.1.4  christos local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) {
    369      1.1  christos     struct inflate_state FAR *state;
    370  1.1.1.2  christos     unsigned dist;
    371      1.1  christos 
    372      1.1  christos     state = (struct inflate_state FAR *)strm->state;
    373      1.1  christos 
    374      1.1  christos     /* if it hasn't been done already, allocate space for the window */
    375      1.1  christos     if (state->window == Z_NULL) {
    376      1.1  christos         state->window = (unsigned char FAR *)
    377      1.1  christos                         ZALLOC(strm, 1U << state->wbits,
    378      1.1  christos                                sizeof(unsigned char));
    379      1.1  christos         if (state->window == Z_NULL) return 1;
    380      1.1  christos     }
    381      1.1  christos 
    382      1.1  christos     /* if window not in use yet, initialize */
    383      1.1  christos     if (state->wsize == 0) {
    384      1.1  christos         state->wsize = 1U << state->wbits;
    385  1.1.1.2  christos         state->wnext = 0;
    386      1.1  christos         state->whave = 0;
    387      1.1  christos     }
    388      1.1  christos 
    389      1.1  christos     /* copy state->wsize or less output bytes into the circular window */
    390      1.1  christos     if (copy >= state->wsize) {
    391  1.1.1.2  christos         zmemcpy(state->window, end - state->wsize, state->wsize);
    392  1.1.1.2  christos         state->wnext = 0;
    393      1.1  christos         state->whave = state->wsize;
    394      1.1  christos     }
    395      1.1  christos     else {
    396  1.1.1.2  christos         dist = state->wsize - state->wnext;
    397      1.1  christos         if (dist > copy) dist = copy;
    398  1.1.1.2  christos         zmemcpy(state->window + state->wnext, end - copy, dist);
    399      1.1  christos         copy -= dist;
    400      1.1  christos         if (copy) {
    401  1.1.1.2  christos             zmemcpy(state->window, end - copy, copy);
    402  1.1.1.2  christos             state->wnext = copy;
    403      1.1  christos             state->whave = state->wsize;
    404      1.1  christos         }
    405      1.1  christos         else {
    406  1.1.1.2  christos             state->wnext += dist;
    407  1.1.1.2  christos             if (state->wnext == state->wsize) state->wnext = 0;
    408      1.1  christos             if (state->whave < state->wsize) state->whave += dist;
    409      1.1  christos         }
    410      1.1  christos     }
    411      1.1  christos     return 0;
    412      1.1  christos }
    413      1.1  christos 
    414      1.1  christos /* Macros for inflate(): */
    415      1.1  christos 
    416      1.1  christos /* check function to use adler32() for zlib or crc32() for gzip */
    417      1.1  christos #ifdef GUNZIP
    418  1.1.1.3  christos #  define UPDATE_CHECK(check, buf, len) \
    419      1.1  christos     (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
    420      1.1  christos #else
    421  1.1.1.3  christos #  define UPDATE_CHECK(check, buf, len) adler32(check, buf, len)
    422      1.1  christos #endif
    423      1.1  christos 
    424      1.1  christos /* check macros for header crc */
    425      1.1  christos #ifdef GUNZIP
    426      1.1  christos #  define CRC2(check, word) \
    427      1.1  christos     do { \
    428      1.1  christos         hbuf[0] = (unsigned char)(word); \
    429      1.1  christos         hbuf[1] = (unsigned char)((word) >> 8); \
    430      1.1  christos         check = crc32(check, hbuf, 2); \
    431      1.1  christos     } while (0)
    432      1.1  christos 
    433      1.1  christos #  define CRC4(check, word) \
    434      1.1  christos     do { \
    435      1.1  christos         hbuf[0] = (unsigned char)(word); \
    436      1.1  christos         hbuf[1] = (unsigned char)((word) >> 8); \
    437      1.1  christos         hbuf[2] = (unsigned char)((word) >> 16); \
    438      1.1  christos         hbuf[3] = (unsigned char)((word) >> 24); \
    439      1.1  christos         check = crc32(check, hbuf, 4); \
    440      1.1  christos     } while (0)
    441      1.1  christos #endif
    442      1.1  christos 
    443      1.1  christos /* Load registers with state in inflate() for speed */
    444      1.1  christos #define LOAD() \
    445      1.1  christos     do { \
    446      1.1  christos         put = strm->next_out; \
    447      1.1  christos         left = strm->avail_out; \
    448      1.1  christos         next = strm->next_in; \
    449      1.1  christos         have = strm->avail_in; \
    450      1.1  christos         hold = state->hold; \
    451      1.1  christos         bits = state->bits; \
    452      1.1  christos     } while (0)
    453      1.1  christos 
    454      1.1  christos /* Restore state from registers in inflate() */
    455      1.1  christos #define RESTORE() \
    456      1.1  christos     do { \
    457      1.1  christos         strm->next_out = put; \
    458      1.1  christos         strm->avail_out = left; \
    459      1.1  christos         strm->next_in = next; \
    460      1.1  christos         strm->avail_in = have; \
    461      1.1  christos         state->hold = hold; \
    462      1.1  christos         state->bits = bits; \
    463      1.1  christos     } while (0)
    464      1.1  christos 
    465      1.1  christos /* Clear the input bit accumulator */
    466      1.1  christos #define INITBITS() \
    467      1.1  christos     do { \
    468      1.1  christos         hold = 0; \
    469      1.1  christos         bits = 0; \
    470      1.1  christos     } while (0)
    471      1.1  christos 
    472      1.1  christos /* Get a byte of input into the bit accumulator, or return from inflate()
    473      1.1  christos    if there is no input available. */
    474      1.1  christos #define PULLBYTE() \
    475      1.1  christos     do { \
    476      1.1  christos         if (have == 0) goto inf_leave; \
    477      1.1  christos         have--; \
    478      1.1  christos         hold += (unsigned long)(*next++) << bits; \
    479      1.1  christos         bits += 8; \
    480      1.1  christos     } while (0)
    481      1.1  christos 
    482      1.1  christos /* Assure that there are at least n bits in the bit accumulator.  If there is
    483      1.1  christos    not enough available input to do that, then return from inflate(). */
    484      1.1  christos #define NEEDBITS(n) \
    485      1.1  christos     do { \
    486      1.1  christos         while (bits < (unsigned)(n)) \
    487      1.1  christos             PULLBYTE(); \
    488      1.1  christos     } while (0)
    489      1.1  christos 
    490      1.1  christos /* Return the low n bits of the bit accumulator (n < 16) */
    491      1.1  christos #define BITS(n) \
    492      1.1  christos     ((unsigned)hold & ((1U << (n)) - 1))
    493      1.1  christos 
    494      1.1  christos /* Remove n bits from the bit accumulator */
    495      1.1  christos #define DROPBITS(n) \
    496      1.1  christos     do { \
    497      1.1  christos         hold >>= (n); \
    498      1.1  christos         bits -= (unsigned)(n); \
    499      1.1  christos     } while (0)
    500      1.1  christos 
    501      1.1  christos /* Remove zero to seven bits as needed to go to a byte boundary */
    502      1.1  christos #define BYTEBITS() \
    503      1.1  christos     do { \
    504      1.1  christos         hold >>= bits & 7; \
    505      1.1  christos         bits -= bits & 7; \
    506      1.1  christos     } while (0)
    507      1.1  christos 
    508      1.1  christos /*
    509      1.1  christos    inflate() uses a state machine to process as much input data and generate as
    510      1.1  christos    much output data as possible before returning.  The state machine is
    511      1.1  christos    structured roughly as follows:
    512      1.1  christos 
    513      1.1  christos     for (;;) switch (state) {
    514      1.1  christos     ...
    515      1.1  christos     case STATEn:
    516      1.1  christos         if (not enough input data or output space to make progress)
    517      1.1  christos             return;
    518      1.1  christos         ... make progress ...
    519      1.1  christos         state = STATEm;
    520      1.1  christos         break;
    521      1.1  christos     ...
    522      1.1  christos     }
    523      1.1  christos 
    524      1.1  christos    so when inflate() is called again, the same case is attempted again, and
    525      1.1  christos    if the appropriate resources are provided, the machine proceeds to the
    526      1.1  christos    next state.  The NEEDBITS() macro is usually the way the state evaluates
    527      1.1  christos    whether it can proceed or should return.  NEEDBITS() does the return if
    528      1.1  christos    the requested bits are not available.  The typical use of the BITS macros
    529      1.1  christos    is:
    530      1.1  christos 
    531      1.1  christos         NEEDBITS(n);
    532      1.1  christos         ... do something with BITS(n) ...
    533      1.1  christos         DROPBITS(n);
    534      1.1  christos 
    535      1.1  christos    where NEEDBITS(n) either returns from inflate() if there isn't enough
    536      1.1  christos    input left to load n bits into the accumulator, or it continues.  BITS(n)
    537      1.1  christos    gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
    538      1.1  christos    the low n bits off the accumulator.  INITBITS() clears the accumulator
    539      1.1  christos    and sets the number of available bits to zero.  BYTEBITS() discards just
    540      1.1  christos    enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
    541      1.1  christos    and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
    542      1.1  christos 
    543      1.1  christos    NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
    544      1.1  christos    if there is no input available.  The decoding of variable length codes uses
    545      1.1  christos    PULLBYTE() directly in order to pull just enough bytes to decode the next
    546      1.1  christos    code, and no more.
    547      1.1  christos 
    548      1.1  christos    Some states loop until they get enough input, making sure that enough
    549      1.1  christos    state information is maintained to continue the loop where it left off
    550      1.1  christos    if NEEDBITS() returns in the loop.  For example, want, need, and keep
    551      1.1  christos    would all have to actually be part of the saved state in case NEEDBITS()
    552      1.1  christos    returns:
    553      1.1  christos 
    554      1.1  christos     case STATEw:
    555      1.1  christos         while (want < need) {
    556      1.1  christos             NEEDBITS(n);
    557      1.1  christos             keep[want++] = BITS(n);
    558      1.1  christos             DROPBITS(n);
    559      1.1  christos         }
    560      1.1  christos         state = STATEx;
    561      1.1  christos     case STATEx:
    562      1.1  christos 
    563      1.1  christos    As shown above, if the next state is also the next case, then the break
    564      1.1  christos    is omitted.
    565      1.1  christos 
    566      1.1  christos    A state may also return if there is not enough output space available to
    567      1.1  christos    complete that state.  Those states are copying stored data, writing a
    568      1.1  christos    literal byte, and copying a matching string.
    569      1.1  christos 
    570      1.1  christos    When returning, a "goto inf_leave" is used to update the total counters,
    571      1.1  christos    update the check value, and determine whether any progress has been made
    572      1.1  christos    during that inflate() call in order to return the proper return code.
    573      1.1  christos    Progress is defined as a change in either strm->avail_in or strm->avail_out.
    574      1.1  christos    When there is a window, goto inf_leave will update the window with the last
    575      1.1  christos    output written.  If a goto inf_leave occurs in the middle of decompression
    576      1.1  christos    and there is no window currently, goto inf_leave will create one and copy
    577      1.1  christos    output to the window for the next call of inflate().
    578      1.1  christos 
    579      1.1  christos    In this implementation, the flush parameter of inflate() only affects the
    580      1.1  christos    return code (per zlib.h).  inflate() always writes as much as possible to
    581      1.1  christos    strm->next_out, given the space available and the provided input--the effect
    582      1.1  christos    documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
    583      1.1  christos    the allocation of and copying into a sliding window until necessary, which
    584      1.1  christos    provides the effect documented in zlib.h for Z_FINISH when the entire input
    585      1.1  christos    stream available.  So the only thing the flush parameter actually does is:
    586      1.1  christos    when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
    587      1.1  christos    will return Z_BUF_ERROR if it has not reached the end of the stream.
    588      1.1  christos  */
    589      1.1  christos 
    590  1.1.1.4  christos int ZEXPORT inflate(z_streamp strm, int flush) {
    591      1.1  christos     struct inflate_state FAR *state;
    592  1.1.1.2  christos     z_const unsigned char FAR *next;    /* next input */
    593      1.1  christos     unsigned char FAR *put;     /* next output */
    594      1.1  christos     unsigned have, left;        /* available input and output */
    595      1.1  christos     unsigned long hold;         /* bit buffer */
    596      1.1  christos     unsigned bits;              /* bits in bit buffer */
    597      1.1  christos     unsigned in, out;           /* save starting available input and output */
    598      1.1  christos     unsigned copy;              /* number of stored or match bytes to copy */
    599      1.1  christos     unsigned char FAR *from;    /* where to copy match bytes from */
    600  1.1.1.2  christos     code here;                  /* current decoding table entry */
    601      1.1  christos     code last;                  /* parent table entry */
    602      1.1  christos     unsigned len;               /* length to copy for repeats, bits to drop */
    603      1.1  christos     int ret;                    /* return code */
    604      1.1  christos #ifdef GUNZIP
    605      1.1  christos     unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
    606      1.1  christos #endif
    607      1.1  christos     static const unsigned short order[19] = /* permutation of code lengths */
    608      1.1  christos         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
    609      1.1  christos 
    610  1.1.1.2  christos     if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
    611      1.1  christos         (strm->next_in == Z_NULL && strm->avail_in != 0))
    612      1.1  christos         return Z_STREAM_ERROR;
    613      1.1  christos 
    614      1.1  christos     state = (struct inflate_state FAR *)strm->state;
    615      1.1  christos     if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
    616      1.1  christos     LOAD();
    617      1.1  christos     in = have;
    618      1.1  christos     out = left;
    619      1.1  christos     ret = Z_OK;
    620      1.1  christos     for (;;)
    621      1.1  christos         switch (state->mode) {
    622      1.1  christos         case HEAD:
    623      1.1  christos             if (state->wrap == 0) {
    624      1.1  christos                 state->mode = TYPEDO;
    625      1.1  christos                 break;
    626      1.1  christos             }
    627      1.1  christos             NEEDBITS(16);
    628      1.1  christos #ifdef GUNZIP
    629      1.1  christos             if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
    630  1.1.1.2  christos                 if (state->wbits == 0)
    631  1.1.1.2  christos                     state->wbits = 15;
    632      1.1  christos                 state->check = crc32(0L, Z_NULL, 0);
    633      1.1  christos                 CRC2(state->check, hold);
    634      1.1  christos                 INITBITS();
    635      1.1  christos                 state->mode = FLAGS;
    636      1.1  christos                 break;
    637      1.1  christos             }
    638      1.1  christos             if (state->head != Z_NULL)
    639      1.1  christos                 state->head->done = -1;
    640      1.1  christos             if (!(state->wrap & 1) ||   /* check if zlib header allowed */
    641      1.1  christos #else
    642      1.1  christos             if (
    643      1.1  christos #endif
    644      1.1  christos                 ((BITS(8) << 8) + (hold >> 8)) % 31) {
    645      1.1  christos                 strm->msg = (char *)"incorrect header check";
    646      1.1  christos                 state->mode = BAD;
    647      1.1  christos                 break;
    648      1.1  christos             }
    649      1.1  christos             if (BITS(4) != Z_DEFLATED) {
    650      1.1  christos                 strm->msg = (char *)"unknown compression method";
    651      1.1  christos                 state->mode = BAD;
    652      1.1  christos                 break;
    653      1.1  christos             }
    654      1.1  christos             DROPBITS(4);
    655      1.1  christos             len = BITS(4) + 8;
    656  1.1.1.2  christos             if (state->wbits == 0)
    657  1.1.1.2  christos                 state->wbits = len;
    658  1.1.1.2  christos             if (len > 15 || len > state->wbits) {
    659      1.1  christos                 strm->msg = (char *)"invalid window size";
    660      1.1  christos                 state->mode = BAD;
    661      1.1  christos                 break;
    662      1.1  christos             }
    663      1.1  christos             state->dmax = 1U << len;
    664  1.1.1.3  christos             state->flags = 0;               /* indicate zlib header */
    665      1.1  christos             Tracev((stderr, "inflate:   zlib header ok\n"));
    666      1.1  christos             strm->adler = state->check = adler32(0L, Z_NULL, 0);
    667      1.1  christos             state->mode = hold & 0x200 ? DICTID : TYPE;
    668      1.1  christos             INITBITS();
    669      1.1  christos             break;
    670      1.1  christos #ifdef GUNZIP
    671      1.1  christos         case FLAGS:
    672      1.1  christos             NEEDBITS(16);
    673      1.1  christos             state->flags = (int)(hold);
    674      1.1  christos             if ((state->flags & 0xff) != Z_DEFLATED) {
    675      1.1  christos                 strm->msg = (char *)"unknown compression method";
    676      1.1  christos                 state->mode = BAD;
    677      1.1  christos                 break;
    678      1.1  christos             }
    679      1.1  christos             if (state->flags & 0xe000) {
    680      1.1  christos                 strm->msg = (char *)"unknown header flags set";
    681      1.1  christos                 state->mode = BAD;
    682      1.1  christos                 break;
    683      1.1  christos             }
    684      1.1  christos             if (state->head != Z_NULL)
    685      1.1  christos                 state->head->text = (int)((hold >> 8) & 1);
    686  1.1.1.2  christos             if ((state->flags & 0x0200) && (state->wrap & 4))
    687  1.1.1.2  christos                 CRC2(state->check, hold);
    688      1.1  christos             INITBITS();
    689      1.1  christos             state->mode = TIME;
    690  1.1.1.3  christos                 /* fallthrough */
    691      1.1  christos         case TIME:
    692      1.1  christos             NEEDBITS(32);
    693      1.1  christos             if (state->head != Z_NULL)
    694      1.1  christos                 state->head->time = hold;
    695  1.1.1.2  christos             if ((state->flags & 0x0200) && (state->wrap & 4))
    696  1.1.1.2  christos                 CRC4(state->check, hold);
    697      1.1  christos             INITBITS();
    698      1.1  christos             state->mode = OS;
    699  1.1.1.3  christos                 /* fallthrough */
    700      1.1  christos         case OS:
    701      1.1  christos             NEEDBITS(16);
    702      1.1  christos             if (state->head != Z_NULL) {
    703      1.1  christos                 state->head->xflags = (int)(hold & 0xff);
    704      1.1  christos                 state->head->os = (int)(hold >> 8);
    705      1.1  christos             }
    706  1.1.1.2  christos             if ((state->flags & 0x0200) && (state->wrap & 4))
    707  1.1.1.2  christos                 CRC2(state->check, hold);
    708      1.1  christos             INITBITS();
    709      1.1  christos             state->mode = EXLEN;
    710  1.1.1.3  christos                 /* fallthrough */
    711      1.1  christos         case EXLEN:
    712      1.1  christos             if (state->flags & 0x0400) {
    713      1.1  christos                 NEEDBITS(16);
    714      1.1  christos                 state->length = (unsigned)(hold);
    715      1.1  christos                 if (state->head != Z_NULL)
    716      1.1  christos                     state->head->extra_len = (unsigned)hold;
    717  1.1.1.2  christos                 if ((state->flags & 0x0200) && (state->wrap & 4))
    718  1.1.1.2  christos                     CRC2(state->check, hold);
    719      1.1  christos                 INITBITS();
    720      1.1  christos             }
    721      1.1  christos             else if (state->head != Z_NULL)
    722      1.1  christos                 state->head->extra = Z_NULL;
    723      1.1  christos             state->mode = EXTRA;
    724  1.1.1.3  christos                 /* fallthrough */
    725      1.1  christos         case EXTRA:
    726      1.1  christos             if (state->flags & 0x0400) {
    727      1.1  christos                 copy = state->length;
    728      1.1  christos                 if (copy > have) copy = have;
    729      1.1  christos                 if (copy) {
    730      1.1  christos                     if (state->head != Z_NULL &&
    731  1.1.1.3  christos                         state->head->extra != Z_NULL &&
    732  1.1.1.3  christos                         (len = state->head->extra_len - state->length) <
    733  1.1.1.3  christos                             state->head->extra_max) {
    734      1.1  christos                         zmemcpy(state->head->extra + len, next,
    735      1.1  christos                                 len + copy > state->head->extra_max ?
    736      1.1  christos                                 state->head->extra_max - len : copy);
    737      1.1  christos                     }
    738  1.1.1.2  christos                     if ((state->flags & 0x0200) && (state->wrap & 4))
    739      1.1  christos                         state->check = crc32(state->check, next, copy);
    740      1.1  christos                     have -= copy;
    741      1.1  christos                     next += copy;
    742      1.1  christos                     state->length -= copy;
    743      1.1  christos                 }
    744      1.1  christos                 if (state->length) goto inf_leave;
    745      1.1  christos             }
    746      1.1  christos             state->length = 0;
    747      1.1  christos             state->mode = NAME;
    748  1.1.1.3  christos                 /* fallthrough */
    749      1.1  christos         case NAME:
    750      1.1  christos             if (state->flags & 0x0800) {
    751      1.1  christos                 if (have == 0) goto inf_leave;
    752      1.1  christos                 copy = 0;
    753      1.1  christos                 do {
    754      1.1  christos                     len = (unsigned)(next[copy++]);
    755      1.1  christos                     if (state->head != Z_NULL &&
    756      1.1  christos                             state->head->name != Z_NULL &&
    757      1.1  christos                             state->length < state->head->name_max)
    758  1.1.1.2  christos                         state->head->name[state->length++] = (Bytef)len;
    759      1.1  christos                 } while (len && copy < have);
    760  1.1.1.2  christos                 if ((state->flags & 0x0200) && (state->wrap & 4))
    761      1.1  christos                     state->check = crc32(state->check, next, copy);
    762      1.1  christos                 have -= copy;
    763      1.1  christos                 next += copy;
    764      1.1  christos                 if (len) goto inf_leave;
    765      1.1  christos             }
    766      1.1  christos             else if (state->head != Z_NULL)
    767      1.1  christos                 state->head->name = Z_NULL;
    768      1.1  christos             state->length = 0;
    769      1.1  christos             state->mode = COMMENT;
    770  1.1.1.3  christos                 /* fallthrough */
    771      1.1  christos         case COMMENT:
    772      1.1  christos             if (state->flags & 0x1000) {
    773      1.1  christos                 if (have == 0) goto inf_leave;
    774      1.1  christos                 copy = 0;
    775      1.1  christos                 do {
    776      1.1  christos                     len = (unsigned)(next[copy++]);
    777      1.1  christos                     if (state->head != Z_NULL &&
    778      1.1  christos                             state->head->comment != Z_NULL &&
    779      1.1  christos                             state->length < state->head->comm_max)
    780  1.1.1.2  christos                         state->head->comment[state->length++] = (Bytef)len;
    781      1.1  christos                 } while (len && copy < have);
    782  1.1.1.2  christos                 if ((state->flags & 0x0200) && (state->wrap & 4))
    783      1.1  christos                     state->check = crc32(state->check, next, copy);
    784      1.1  christos                 have -= copy;
    785      1.1  christos                 next += copy;
    786      1.1  christos                 if (len) goto inf_leave;
    787      1.1  christos             }
    788      1.1  christos             else if (state->head != Z_NULL)
    789      1.1  christos                 state->head->comment = Z_NULL;
    790      1.1  christos             state->mode = HCRC;
    791  1.1.1.3  christos                 /* fallthrough */
    792      1.1  christos         case HCRC:
    793      1.1  christos             if (state->flags & 0x0200) {
    794      1.1  christos                 NEEDBITS(16);
    795  1.1.1.2  christos                 if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
    796      1.1  christos                     strm->msg = (char *)"header crc mismatch";
    797      1.1  christos                     state->mode = BAD;
    798      1.1  christos                     break;
    799      1.1  christos                 }
    800      1.1  christos                 INITBITS();
    801      1.1  christos             }
    802      1.1  christos             if (state->head != Z_NULL) {
    803      1.1  christos                 state->head->hcrc = (int)((state->flags >> 9) & 1);
    804      1.1  christos                 state->head->done = 1;
    805      1.1  christos             }
    806      1.1  christos             strm->adler = state->check = crc32(0L, Z_NULL, 0);
    807      1.1  christos             state->mode = TYPE;
    808      1.1  christos             break;
    809      1.1  christos #endif
    810      1.1  christos         case DICTID:
    811      1.1  christos             NEEDBITS(32);
    812  1.1.1.2  christos             strm->adler = state->check = ZSWAP32(hold);
    813      1.1  christos             INITBITS();
    814      1.1  christos             state->mode = DICT;
    815  1.1.1.3  christos                 /* fallthrough */
    816      1.1  christos         case DICT:
    817      1.1  christos             if (state->havedict == 0) {
    818      1.1  christos                 RESTORE();
    819      1.1  christos                 return Z_NEED_DICT;
    820      1.1  christos             }
    821      1.1  christos             strm->adler = state->check = adler32(0L, Z_NULL, 0);
    822      1.1  christos             state->mode = TYPE;
    823  1.1.1.3  christos                 /* fallthrough */
    824      1.1  christos         case TYPE:
    825  1.1.1.2  christos             if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
    826  1.1.1.3  christos                 /* fallthrough */
    827      1.1  christos         case TYPEDO:
    828      1.1  christos             if (state->last) {
    829      1.1  christos                 BYTEBITS();
    830      1.1  christos                 state->mode = CHECK;
    831      1.1  christos                 break;
    832      1.1  christos             }
    833      1.1  christos             NEEDBITS(3);
    834      1.1  christos             state->last = BITS(1);
    835      1.1  christos             DROPBITS(1);
    836      1.1  christos             switch (BITS(2)) {
    837      1.1  christos             case 0:                             /* stored block */
    838      1.1  christos                 Tracev((stderr, "inflate:     stored block%s\n",
    839      1.1  christos                         state->last ? " (last)" : ""));
    840      1.1  christos                 state->mode = STORED;
    841      1.1  christos                 break;
    842      1.1  christos             case 1:                             /* fixed block */
    843      1.1  christos                 fixedtables(state);
    844      1.1  christos                 Tracev((stderr, "inflate:     fixed codes block%s\n",
    845      1.1  christos                         state->last ? " (last)" : ""));
    846  1.1.1.2  christos                 state->mode = LEN_;             /* decode codes */
    847  1.1.1.2  christos                 if (flush == Z_TREES) {
    848  1.1.1.2  christos                     DROPBITS(2);
    849  1.1.1.2  christos                     goto inf_leave;
    850  1.1.1.2  christos                 }
    851      1.1  christos                 break;
    852      1.1  christos             case 2:                             /* dynamic block */
    853      1.1  christos                 Tracev((stderr, "inflate:     dynamic codes block%s\n",
    854      1.1  christos                         state->last ? " (last)" : ""));
    855      1.1  christos                 state->mode = TABLE;
    856      1.1  christos                 break;
    857      1.1  christos             case 3:
    858      1.1  christos                 strm->msg = (char *)"invalid block type";
    859      1.1  christos                 state->mode = BAD;
    860      1.1  christos             }
    861      1.1  christos             DROPBITS(2);
    862      1.1  christos             break;
    863      1.1  christos         case STORED:
    864      1.1  christos             BYTEBITS();                         /* go to byte boundary */
    865      1.1  christos             NEEDBITS(32);
    866      1.1  christos             if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
    867      1.1  christos                 strm->msg = (char *)"invalid stored block lengths";
    868      1.1  christos                 state->mode = BAD;
    869      1.1  christos                 break;
    870      1.1  christos             }
    871      1.1  christos             state->length = (unsigned)hold & 0xffff;
    872      1.1  christos             Tracev((stderr, "inflate:       stored length %u\n",
    873      1.1  christos                     state->length));
    874      1.1  christos             INITBITS();
    875  1.1.1.2  christos             state->mode = COPY_;
    876  1.1.1.2  christos             if (flush == Z_TREES) goto inf_leave;
    877  1.1.1.3  christos                 /* fallthrough */
    878  1.1.1.2  christos         case COPY_:
    879      1.1  christos             state->mode = COPY;
    880  1.1.1.3  christos                 /* fallthrough */
    881      1.1  christos         case COPY:
    882      1.1  christos             copy = state->length;
    883      1.1  christos             if (copy) {
    884      1.1  christos                 if (copy > have) copy = have;
    885      1.1  christos                 if (copy > left) copy = left;
    886      1.1  christos                 if (copy == 0) goto inf_leave;
    887      1.1  christos                 zmemcpy(put, next, copy);
    888      1.1  christos                 have -= copy;
    889      1.1  christos                 next += copy;
    890      1.1  christos                 left -= copy;
    891      1.1  christos                 put += copy;
    892      1.1  christos                 state->length -= copy;
    893      1.1  christos                 break;
    894      1.1  christos             }
    895      1.1  christos             Tracev((stderr, "inflate:       stored end\n"));
    896      1.1  christos             state->mode = TYPE;
    897      1.1  christos             break;
    898      1.1  christos         case TABLE:
    899      1.1  christos             NEEDBITS(14);
    900      1.1  christos             state->nlen = BITS(5) + 257;
    901      1.1  christos             DROPBITS(5);
    902      1.1  christos             state->ndist = BITS(5) + 1;
    903      1.1  christos             DROPBITS(5);
    904      1.1  christos             state->ncode = BITS(4) + 4;
    905      1.1  christos             DROPBITS(4);
    906      1.1  christos #ifndef PKZIP_BUG_WORKAROUND
    907      1.1  christos             if (state->nlen > 286 || state->ndist > 30) {
    908      1.1  christos                 strm->msg = (char *)"too many length or distance symbols";
    909      1.1  christos                 state->mode = BAD;
    910      1.1  christos                 break;
    911      1.1  christos             }
    912      1.1  christos #endif
    913      1.1  christos             Tracev((stderr, "inflate:       table sizes ok\n"));
    914      1.1  christos             state->have = 0;
    915      1.1  christos             state->mode = LENLENS;
    916  1.1.1.3  christos                 /* fallthrough */
    917      1.1  christos         case LENLENS:
    918      1.1  christos             while (state->have < state->ncode) {
    919      1.1  christos                 NEEDBITS(3);
    920      1.1  christos                 state->lens[order[state->have++]] = (unsigned short)BITS(3);
    921      1.1  christos                 DROPBITS(3);
    922      1.1  christos             }
    923      1.1  christos             while (state->have < 19)
    924      1.1  christos                 state->lens[order[state->have++]] = 0;
    925      1.1  christos             state->next = state->codes;
    926  1.1.1.2  christos             state->lencode = (const code FAR *)(state->next);
    927      1.1  christos             state->lenbits = 7;
    928      1.1  christos             ret = inflate_table(CODES, state->lens, 19, &(state->next),
    929      1.1  christos                                 &(state->lenbits), state->work);
    930      1.1  christos             if (ret) {
    931      1.1  christos                 strm->msg = (char *)"invalid code lengths set";
    932      1.1  christos                 state->mode = BAD;
    933      1.1  christos                 break;
    934      1.1  christos             }
    935      1.1  christos             Tracev((stderr, "inflate:       code lengths ok\n"));
    936      1.1  christos             state->have = 0;
    937      1.1  christos             state->mode = CODELENS;
    938  1.1.1.3  christos                 /* fallthrough */
    939      1.1  christos         case CODELENS:
    940      1.1  christos             while (state->have < state->nlen + state->ndist) {
    941      1.1  christos                 for (;;) {
    942  1.1.1.2  christos                     here = state->lencode[BITS(state->lenbits)];
    943  1.1.1.2  christos                     if ((unsigned)(here.bits) <= bits) break;
    944      1.1  christos                     PULLBYTE();
    945      1.1  christos                 }
    946  1.1.1.2  christos                 if (here.val < 16) {
    947  1.1.1.2  christos                     DROPBITS(here.bits);
    948  1.1.1.2  christos                     state->lens[state->have++] = here.val;
    949      1.1  christos                 }
    950      1.1  christos                 else {
    951  1.1.1.2  christos                     if (here.val == 16) {
    952  1.1.1.2  christos                         NEEDBITS(here.bits + 2);
    953  1.1.1.2  christos                         DROPBITS(here.bits);
    954      1.1  christos                         if (state->have == 0) {
    955      1.1  christos                             strm->msg = (char *)"invalid bit length repeat";
    956      1.1  christos                             state->mode = BAD;
    957      1.1  christos                             break;
    958      1.1  christos                         }
    959      1.1  christos                         len = state->lens[state->have - 1];
    960      1.1  christos                         copy = 3 + BITS(2);
    961      1.1  christos                         DROPBITS(2);
    962      1.1  christos                     }
    963  1.1.1.2  christos                     else if (here.val == 17) {
    964  1.1.1.2  christos                         NEEDBITS(here.bits + 3);
    965  1.1.1.2  christos                         DROPBITS(here.bits);
    966      1.1  christos                         len = 0;
    967      1.1  christos                         copy = 3 + BITS(3);
    968      1.1  christos                         DROPBITS(3);
    969      1.1  christos                     }
    970      1.1  christos                     else {
    971  1.1.1.2  christos                         NEEDBITS(here.bits + 7);
    972  1.1.1.2  christos                         DROPBITS(here.bits);
    973      1.1  christos                         len = 0;
    974      1.1  christos                         copy = 11 + BITS(7);
    975      1.1  christos                         DROPBITS(7);
    976      1.1  christos                     }
    977      1.1  christos                     if (state->have + copy > state->nlen + state->ndist) {
    978      1.1  christos                         strm->msg = (char *)"invalid bit length repeat";
    979      1.1  christos                         state->mode = BAD;
    980      1.1  christos                         break;
    981      1.1  christos                     }
    982      1.1  christos                     while (copy--)
    983      1.1  christos                         state->lens[state->have++] = (unsigned short)len;
    984      1.1  christos                 }
    985      1.1  christos             }
    986      1.1  christos 
    987      1.1  christos             /* handle error breaks in while */
    988      1.1  christos             if (state->mode == BAD) break;
    989      1.1  christos 
    990  1.1.1.2  christos             /* check for end-of-block code (better have one) */
    991  1.1.1.2  christos             if (state->lens[256] == 0) {
    992  1.1.1.2  christos                 strm->msg = (char *)"invalid code -- missing end-of-block";
    993  1.1.1.2  christos                 state->mode = BAD;
    994  1.1.1.2  christos                 break;
    995  1.1.1.2  christos             }
    996  1.1.1.2  christos 
    997  1.1.1.2  christos             /* build code tables -- note: do not change the lenbits or distbits
    998  1.1.1.2  christos                values here (9 and 6) without reading the comments in inftrees.h
    999  1.1.1.2  christos                concerning the ENOUGH constants, which depend on those values */
   1000      1.1  christos             state->next = state->codes;
   1001  1.1.1.2  christos             state->lencode = (const code FAR *)(state->next);
   1002      1.1  christos             state->lenbits = 9;
   1003      1.1  christos             ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
   1004      1.1  christos                                 &(state->lenbits), state->work);
   1005      1.1  christos             if (ret) {
   1006      1.1  christos                 strm->msg = (char *)"invalid literal/lengths set";
   1007      1.1  christos                 state->mode = BAD;
   1008      1.1  christos                 break;
   1009      1.1  christos             }
   1010  1.1.1.2  christos             state->distcode = (const code FAR *)(state->next);
   1011      1.1  christos             state->distbits = 6;
   1012      1.1  christos             ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
   1013      1.1  christos                             &(state->next), &(state->distbits), state->work);
   1014      1.1  christos             if (ret) {
   1015      1.1  christos                 strm->msg = (char *)"invalid distances set";
   1016      1.1  christos                 state->mode = BAD;
   1017      1.1  christos                 break;
   1018      1.1  christos             }
   1019      1.1  christos             Tracev((stderr, "inflate:       codes ok\n"));
   1020  1.1.1.2  christos             state->mode = LEN_;
   1021  1.1.1.2  christos             if (flush == Z_TREES) goto inf_leave;
   1022  1.1.1.3  christos                 /* fallthrough */
   1023  1.1.1.2  christos         case LEN_:
   1024      1.1  christos             state->mode = LEN;
   1025  1.1.1.3  christos                 /* fallthrough */
   1026      1.1  christos         case LEN:
   1027      1.1  christos             if (have >= 6 && left >= 258) {
   1028      1.1  christos                 RESTORE();
   1029      1.1  christos                 inflate_fast(strm, out);
   1030      1.1  christos                 LOAD();
   1031  1.1.1.2  christos                 if (state->mode == TYPE)
   1032  1.1.1.2  christos                     state->back = -1;
   1033      1.1  christos                 break;
   1034      1.1  christos             }
   1035  1.1.1.2  christos             state->back = 0;
   1036      1.1  christos             for (;;) {
   1037  1.1.1.2  christos                 here = state->lencode[BITS(state->lenbits)];
   1038  1.1.1.2  christos                 if ((unsigned)(here.bits) <= bits) break;
   1039      1.1  christos                 PULLBYTE();
   1040      1.1  christos             }
   1041  1.1.1.2  christos             if (here.op && (here.op & 0xf0) == 0) {
   1042  1.1.1.2  christos                 last = here;
   1043      1.1  christos                 for (;;) {
   1044  1.1.1.2  christos                     here = state->lencode[last.val +
   1045      1.1  christos                             (BITS(last.bits + last.op) >> last.bits)];
   1046  1.1.1.2  christos                     if ((unsigned)(last.bits + here.bits) <= bits) break;
   1047      1.1  christos                     PULLBYTE();
   1048      1.1  christos                 }
   1049      1.1  christos                 DROPBITS(last.bits);
   1050  1.1.1.2  christos                 state->back += last.bits;
   1051      1.1  christos             }
   1052  1.1.1.2  christos             DROPBITS(here.bits);
   1053  1.1.1.2  christos             state->back += here.bits;
   1054  1.1.1.2  christos             state->length = (unsigned)here.val;
   1055  1.1.1.2  christos             if ((int)(here.op) == 0) {
   1056  1.1.1.2  christos                 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
   1057      1.1  christos                         "inflate:         literal '%c'\n" :
   1058  1.1.1.2  christos                         "inflate:         literal 0x%02x\n", here.val));
   1059      1.1  christos                 state->mode = LIT;
   1060      1.1  christos                 break;
   1061      1.1  christos             }
   1062  1.1.1.2  christos             if (here.op & 32) {
   1063      1.1  christos                 Tracevv((stderr, "inflate:         end of block\n"));
   1064  1.1.1.2  christos                 state->back = -1;
   1065      1.1  christos                 state->mode = TYPE;
   1066      1.1  christos                 break;
   1067      1.1  christos             }
   1068  1.1.1.2  christos             if (here.op & 64) {
   1069      1.1  christos                 strm->msg = (char *)"invalid literal/length code";
   1070      1.1  christos                 state->mode = BAD;
   1071      1.1  christos                 break;
   1072      1.1  christos             }
   1073  1.1.1.2  christos             state->extra = (unsigned)(here.op) & 15;
   1074      1.1  christos             state->mode = LENEXT;
   1075  1.1.1.3  christos                 /* fallthrough */
   1076      1.1  christos         case LENEXT:
   1077      1.1  christos             if (state->extra) {
   1078      1.1  christos                 NEEDBITS(state->extra);
   1079      1.1  christos                 state->length += BITS(state->extra);
   1080      1.1  christos                 DROPBITS(state->extra);
   1081  1.1.1.2  christos                 state->back += state->extra;
   1082      1.1  christos             }
   1083      1.1  christos             Tracevv((stderr, "inflate:         length %u\n", state->length));
   1084  1.1.1.2  christos             state->was = state->length;
   1085      1.1  christos             state->mode = DIST;
   1086  1.1.1.3  christos                 /* fallthrough */
   1087      1.1  christos         case DIST:
   1088      1.1  christos             for (;;) {
   1089  1.1.1.2  christos                 here = state->distcode[BITS(state->distbits)];
   1090  1.1.1.2  christos                 if ((unsigned)(here.bits) <= bits) break;
   1091      1.1  christos                 PULLBYTE();
   1092      1.1  christos             }
   1093  1.1.1.2  christos             if ((here.op & 0xf0) == 0) {
   1094  1.1.1.2  christos                 last = here;
   1095      1.1  christos                 for (;;) {
   1096  1.1.1.2  christos                     here = state->distcode[last.val +
   1097      1.1  christos                             (BITS(last.bits + last.op) >> last.bits)];
   1098  1.1.1.2  christos                     if ((unsigned)(last.bits + here.bits) <= bits) break;
   1099      1.1  christos                     PULLBYTE();
   1100      1.1  christos                 }
   1101      1.1  christos                 DROPBITS(last.bits);
   1102  1.1.1.2  christos                 state->back += last.bits;
   1103      1.1  christos             }
   1104  1.1.1.2  christos             DROPBITS(here.bits);
   1105  1.1.1.2  christos             state->back += here.bits;
   1106  1.1.1.2  christos             if (here.op & 64) {
   1107      1.1  christos                 strm->msg = (char *)"invalid distance code";
   1108      1.1  christos                 state->mode = BAD;
   1109      1.1  christos                 break;
   1110      1.1  christos             }
   1111  1.1.1.2  christos             state->offset = (unsigned)here.val;
   1112  1.1.1.2  christos             state->extra = (unsigned)(here.op) & 15;
   1113      1.1  christos             state->mode = DISTEXT;
   1114  1.1.1.3  christos                 /* fallthrough */
   1115      1.1  christos         case DISTEXT:
   1116      1.1  christos             if (state->extra) {
   1117      1.1  christos                 NEEDBITS(state->extra);
   1118      1.1  christos                 state->offset += BITS(state->extra);
   1119      1.1  christos                 DROPBITS(state->extra);
   1120  1.1.1.2  christos                 state->back += state->extra;
   1121      1.1  christos             }
   1122      1.1  christos #ifdef INFLATE_STRICT
   1123      1.1  christos             if (state->offset > state->dmax) {
   1124      1.1  christos                 strm->msg = (char *)"invalid distance too far back";
   1125      1.1  christos                 state->mode = BAD;
   1126      1.1  christos                 break;
   1127      1.1  christos             }
   1128      1.1  christos #endif
   1129      1.1  christos             Tracevv((stderr, "inflate:         distance %u\n", state->offset));
   1130      1.1  christos             state->mode = MATCH;
   1131  1.1.1.3  christos                 /* fallthrough */
   1132      1.1  christos         case MATCH:
   1133      1.1  christos             if (left == 0) goto inf_leave;
   1134      1.1  christos             copy = out - left;
   1135      1.1  christos             if (state->offset > copy) {         /* copy from window */
   1136      1.1  christos                 copy = state->offset - copy;
   1137  1.1.1.2  christos                 if (copy > state->whave) {
   1138  1.1.1.2  christos                     if (state->sane) {
   1139  1.1.1.2  christos                         strm->msg = (char *)"invalid distance too far back";
   1140  1.1.1.2  christos                         state->mode = BAD;
   1141  1.1.1.2  christos                         break;
   1142  1.1.1.2  christos                     }
   1143  1.1.1.2  christos #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
   1144  1.1.1.2  christos                     Trace((stderr, "inflate.c too far\n"));
   1145  1.1.1.2  christos                     copy -= state->whave;
   1146  1.1.1.2  christos                     if (copy > state->length) copy = state->length;
   1147  1.1.1.2  christos                     if (copy > left) copy = left;
   1148  1.1.1.2  christos                     left -= copy;
   1149  1.1.1.2  christos                     state->length -= copy;
   1150  1.1.1.2  christos                     do {
   1151  1.1.1.2  christos                         *put++ = 0;
   1152  1.1.1.2  christos                     } while (--copy);
   1153  1.1.1.2  christos                     if (state->length == 0) state->mode = LEN;
   1154  1.1.1.2  christos                     break;
   1155  1.1.1.2  christos #endif
   1156  1.1.1.2  christos                 }
   1157  1.1.1.2  christos                 if (copy > state->wnext) {
   1158  1.1.1.2  christos                     copy -= state->wnext;
   1159      1.1  christos                     from = state->window + (state->wsize - copy);
   1160      1.1  christos                 }
   1161      1.1  christos                 else
   1162  1.1.1.2  christos                     from = state->window + (state->wnext - copy);
   1163      1.1  christos                 if (copy > state->length) copy = state->length;
   1164      1.1  christos             }
   1165      1.1  christos             else {                              /* copy from output */
   1166      1.1  christos                 from = put - state->offset;
   1167      1.1  christos                 copy = state->length;
   1168      1.1  christos             }
   1169      1.1  christos             if (copy > left) copy = left;
   1170      1.1  christos             left -= copy;
   1171      1.1  christos             state->length -= copy;
   1172      1.1  christos             do {
   1173      1.1  christos                 *put++ = *from++;
   1174      1.1  christos             } while (--copy);
   1175      1.1  christos             if (state->length == 0) state->mode = LEN;
   1176      1.1  christos             break;
   1177      1.1  christos         case LIT:
   1178      1.1  christos             if (left == 0) goto inf_leave;
   1179      1.1  christos             *put++ = (unsigned char)(state->length);
   1180      1.1  christos             left--;
   1181      1.1  christos             state->mode = LEN;
   1182      1.1  christos             break;
   1183      1.1  christos         case CHECK:
   1184      1.1  christos             if (state->wrap) {
   1185      1.1  christos                 NEEDBITS(32);
   1186      1.1  christos                 out -= left;
   1187      1.1  christos                 strm->total_out += out;
   1188      1.1  christos                 state->total += out;
   1189  1.1.1.2  christos                 if ((state->wrap & 4) && out)
   1190      1.1  christos                     strm->adler = state->check =
   1191  1.1.1.3  christos                         UPDATE_CHECK(state->check, put - out, out);
   1192      1.1  christos                 out = left;
   1193  1.1.1.2  christos                 if ((state->wrap & 4) && (
   1194      1.1  christos #ifdef GUNZIP
   1195      1.1  christos                      state->flags ? hold :
   1196      1.1  christos #endif
   1197  1.1.1.2  christos                      ZSWAP32(hold)) != state->check) {
   1198      1.1  christos                     strm->msg = (char *)"incorrect data check";
   1199      1.1  christos                     state->mode = BAD;
   1200      1.1  christos                     break;
   1201      1.1  christos                 }
   1202      1.1  christos                 INITBITS();
   1203      1.1  christos                 Tracev((stderr, "inflate:   check matches trailer\n"));
   1204      1.1  christos             }
   1205      1.1  christos #ifdef GUNZIP
   1206      1.1  christos             state->mode = LENGTH;
   1207  1.1.1.3  christos                 /* fallthrough */
   1208      1.1  christos         case LENGTH:
   1209      1.1  christos             if (state->wrap && state->flags) {
   1210      1.1  christos                 NEEDBITS(32);
   1211  1.1.1.3  christos                 if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
   1212      1.1  christos                     strm->msg = (char *)"incorrect length check";
   1213      1.1  christos                     state->mode = BAD;
   1214      1.1  christos                     break;
   1215      1.1  christos                 }
   1216      1.1  christos                 INITBITS();
   1217      1.1  christos                 Tracev((stderr, "inflate:   length matches trailer\n"));
   1218      1.1  christos             }
   1219      1.1  christos #endif
   1220      1.1  christos             state->mode = DONE;
   1221  1.1.1.3  christos                 /* fallthrough */
   1222      1.1  christos         case DONE:
   1223      1.1  christos             ret = Z_STREAM_END;
   1224      1.1  christos             goto inf_leave;
   1225      1.1  christos         case BAD:
   1226      1.1  christos             ret = Z_DATA_ERROR;
   1227      1.1  christos             goto inf_leave;
   1228      1.1  christos         case MEM:
   1229      1.1  christos             return Z_MEM_ERROR;
   1230      1.1  christos         case SYNC:
   1231  1.1.1.3  christos                 /* fallthrough */
   1232      1.1  christos         default:
   1233      1.1  christos             return Z_STREAM_ERROR;
   1234      1.1  christos         }
   1235      1.1  christos 
   1236      1.1  christos     /*
   1237      1.1  christos        Return from inflate(), updating the total counts and the check value.
   1238      1.1  christos        If there was no progress during the inflate() call, return a buffer
   1239      1.1  christos        error.  Call updatewindow() to create and/or update the window state.
   1240      1.1  christos        Note: a memory error from inflate() is non-recoverable.
   1241      1.1  christos      */
   1242      1.1  christos   inf_leave:
   1243      1.1  christos     RESTORE();
   1244  1.1.1.2  christos     if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
   1245  1.1.1.2  christos             (state->mode < CHECK || flush != Z_FINISH)))
   1246  1.1.1.2  christos         if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
   1247      1.1  christos             state->mode = MEM;
   1248      1.1  christos             return Z_MEM_ERROR;
   1249      1.1  christos         }
   1250      1.1  christos     in -= strm->avail_in;
   1251      1.1  christos     out -= strm->avail_out;
   1252      1.1  christos     strm->total_in += in;
   1253      1.1  christos     strm->total_out += out;
   1254      1.1  christos     state->total += out;
   1255  1.1.1.2  christos     if ((state->wrap & 4) && out)
   1256      1.1  christos         strm->adler = state->check =
   1257  1.1.1.3  christos             UPDATE_CHECK(state->check, strm->next_out - out, out);
   1258  1.1.1.2  christos     strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
   1259  1.1.1.2  christos                       (state->mode == TYPE ? 128 : 0) +
   1260  1.1.1.2  christos                       (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
   1261      1.1  christos     if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
   1262      1.1  christos         ret = Z_BUF_ERROR;
   1263      1.1  christos     return ret;
   1264      1.1  christos }
   1265      1.1  christos 
   1266  1.1.1.4  christos int ZEXPORT inflateEnd(z_streamp strm) {
   1267      1.1  christos     struct inflate_state FAR *state;
   1268  1.1.1.2  christos     if (inflateStateCheck(strm))
   1269      1.1  christos         return Z_STREAM_ERROR;
   1270      1.1  christos     state = (struct inflate_state FAR *)strm->state;
   1271      1.1  christos     if (state->window != Z_NULL) ZFREE(strm, state->window);
   1272      1.1  christos     ZFREE(strm, strm->state);
   1273      1.1  christos     strm->state = Z_NULL;
   1274      1.1  christos     Tracev((stderr, "inflate: end\n"));
   1275      1.1  christos     return Z_OK;
   1276      1.1  christos }
   1277      1.1  christos 
   1278  1.1.1.4  christos int ZEXPORT inflateGetDictionary(z_streamp strm, Bytef *dictionary,
   1279  1.1.1.4  christos                                  uInt *dictLength) {
   1280  1.1.1.2  christos     struct inflate_state FAR *state;
   1281  1.1.1.2  christos 
   1282  1.1.1.2  christos     /* check state */
   1283  1.1.1.2  christos     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
   1284  1.1.1.2  christos     state = (struct inflate_state FAR *)strm->state;
   1285  1.1.1.2  christos 
   1286  1.1.1.2  christos     /* copy dictionary */
   1287  1.1.1.2  christos     if (state->whave && dictionary != Z_NULL) {
   1288  1.1.1.2  christos         zmemcpy(dictionary, state->window + state->wnext,
   1289  1.1.1.2  christos                 state->whave - state->wnext);
   1290  1.1.1.2  christos         zmemcpy(dictionary + state->whave - state->wnext,
   1291  1.1.1.2  christos                 state->window, state->wnext);
   1292  1.1.1.2  christos     }
   1293  1.1.1.2  christos     if (dictLength != Z_NULL)
   1294  1.1.1.2  christos         *dictLength = state->whave;
   1295  1.1.1.2  christos     return Z_OK;
   1296  1.1.1.2  christos }
   1297  1.1.1.2  christos 
   1298  1.1.1.4  christos int ZEXPORT inflateSetDictionary(z_streamp strm, const Bytef *dictionary,
   1299  1.1.1.4  christos                                  uInt dictLength) {
   1300      1.1  christos     struct inflate_state FAR *state;
   1301  1.1.1.2  christos     unsigned long dictid;
   1302  1.1.1.2  christos     int ret;
   1303      1.1  christos 
   1304      1.1  christos     /* check state */
   1305  1.1.1.2  christos     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
   1306      1.1  christos     state = (struct inflate_state FAR *)strm->state;
   1307      1.1  christos     if (state->wrap != 0 && state->mode != DICT)
   1308      1.1  christos         return Z_STREAM_ERROR;
   1309      1.1  christos 
   1310  1.1.1.2  christos     /* check for correct dictionary identifier */
   1311      1.1  christos     if (state->mode == DICT) {
   1312  1.1.1.2  christos         dictid = adler32(0L, Z_NULL, 0);
   1313  1.1.1.2  christos         dictid = adler32(dictid, dictionary, dictLength);
   1314  1.1.1.2  christos         if (dictid != state->check)
   1315      1.1  christos             return Z_DATA_ERROR;
   1316      1.1  christos     }
   1317      1.1  christos 
   1318  1.1.1.2  christos     /* copy dictionary to window using updatewindow(), which will amend the
   1319  1.1.1.2  christos        existing dictionary if appropriate */
   1320  1.1.1.2  christos     ret = updatewindow(strm, dictionary + dictLength, dictLength);
   1321  1.1.1.2  christos     if (ret) {
   1322      1.1  christos         state->mode = MEM;
   1323      1.1  christos         return Z_MEM_ERROR;
   1324      1.1  christos     }
   1325      1.1  christos     state->havedict = 1;
   1326      1.1  christos     Tracev((stderr, "inflate:   dictionary set\n"));
   1327      1.1  christos     return Z_OK;
   1328      1.1  christos }
   1329      1.1  christos 
   1330  1.1.1.4  christos int ZEXPORT inflateGetHeader(z_streamp strm, gz_headerp head) {
   1331      1.1  christos     struct inflate_state FAR *state;
   1332      1.1  christos 
   1333      1.1  christos     /* check state */
   1334  1.1.1.2  christos     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
   1335      1.1  christos     state = (struct inflate_state FAR *)strm->state;
   1336      1.1  christos     if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
   1337      1.1  christos 
   1338      1.1  christos     /* save header structure */
   1339      1.1  christos     state->head = head;
   1340      1.1  christos     head->done = 0;
   1341      1.1  christos     return Z_OK;
   1342      1.1  christos }
   1343      1.1  christos 
   1344      1.1  christos /*
   1345      1.1  christos    Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
   1346      1.1  christos    or when out of input.  When called, *have is the number of pattern bytes
   1347      1.1  christos    found in order so far, in 0..3.  On return *have is updated to the new
   1348      1.1  christos    state.  If on return *have equals four, then the pattern was found and the
   1349      1.1  christos    return value is how many bytes were read including the last byte of the
   1350      1.1  christos    pattern.  If *have is less than four, then the pattern has not been found
   1351      1.1  christos    yet and the return value is len.  In the latter case, syncsearch() can be
   1352      1.1  christos    called again with more data and the *have state.  *have is initialized to
   1353      1.1  christos    zero for the first call.
   1354      1.1  christos  */
   1355  1.1.1.4  christos local unsigned syncsearch(unsigned FAR *have, const unsigned char FAR *buf,
   1356  1.1.1.4  christos                           unsigned len) {
   1357      1.1  christos     unsigned got;
   1358      1.1  christos     unsigned next;
   1359      1.1  christos 
   1360      1.1  christos     got = *have;
   1361      1.1  christos     next = 0;
   1362      1.1  christos     while (next < len && got < 4) {
   1363      1.1  christos         if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
   1364      1.1  christos             got++;
   1365      1.1  christos         else if (buf[next])
   1366      1.1  christos             got = 0;
   1367      1.1  christos         else
   1368      1.1  christos             got = 4 - got;
   1369      1.1  christos         next++;
   1370      1.1  christos     }
   1371      1.1  christos     *have = got;
   1372      1.1  christos     return next;
   1373      1.1  christos }
   1374      1.1  christos 
   1375  1.1.1.4  christos int ZEXPORT inflateSync(z_streamp strm) {
   1376      1.1  christos     unsigned len;               /* number of bytes to look at or looked at */
   1377  1.1.1.3  christos     int flags;                  /* temporary to save header status */
   1378      1.1  christos     unsigned long in, out;      /* temporary to save total_in and total_out */
   1379      1.1  christos     unsigned char buf[4];       /* to restore bit buffer to byte string */
   1380      1.1  christos     struct inflate_state FAR *state;
   1381      1.1  christos 
   1382      1.1  christos     /* check parameters */
   1383  1.1.1.2  christos     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
   1384      1.1  christos     state = (struct inflate_state FAR *)strm->state;
   1385      1.1  christos     if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
   1386      1.1  christos 
   1387      1.1  christos     /* if first time, start search in bit buffer */
   1388      1.1  christos     if (state->mode != SYNC) {
   1389      1.1  christos         state->mode = SYNC;
   1390  1.1.1.4  christos         state->hold >>= state->bits & 7;
   1391      1.1  christos         state->bits -= state->bits & 7;
   1392      1.1  christos         len = 0;
   1393      1.1  christos         while (state->bits >= 8) {
   1394      1.1  christos             buf[len++] = (unsigned char)(state->hold);
   1395      1.1  christos             state->hold >>= 8;
   1396      1.1  christos             state->bits -= 8;
   1397      1.1  christos         }
   1398      1.1  christos         state->have = 0;
   1399      1.1  christos         syncsearch(&(state->have), buf, len);
   1400      1.1  christos     }
   1401      1.1  christos 
   1402      1.1  christos     /* search available input */
   1403      1.1  christos     len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
   1404      1.1  christos     strm->avail_in -= len;
   1405      1.1  christos     strm->next_in += len;
   1406      1.1  christos     strm->total_in += len;
   1407      1.1  christos 
   1408      1.1  christos     /* return no joy or set up to restart inflate() on a new block */
   1409      1.1  christos     if (state->have != 4) return Z_DATA_ERROR;
   1410  1.1.1.3  christos     if (state->flags == -1)
   1411  1.1.1.3  christos         state->wrap = 0;    /* if no header yet, treat as raw */
   1412  1.1.1.3  christos     else
   1413  1.1.1.3  christos         state->wrap &= ~4;  /* no point in computing a check value now */
   1414  1.1.1.3  christos     flags = state->flags;
   1415      1.1  christos     in = strm->total_in;  out = strm->total_out;
   1416      1.1  christos     inflateReset(strm);
   1417      1.1  christos     strm->total_in = in;  strm->total_out = out;
   1418  1.1.1.3  christos     state->flags = flags;
   1419      1.1  christos     state->mode = TYPE;
   1420      1.1  christos     return Z_OK;
   1421      1.1  christos }
   1422      1.1  christos 
   1423      1.1  christos /*
   1424      1.1  christos    Returns true if inflate is currently at the end of a block generated by
   1425      1.1  christos    Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
   1426      1.1  christos    implementation to provide an additional safety check. PPP uses
   1427      1.1  christos    Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
   1428      1.1  christos    block. When decompressing, PPP checks that at the end of input packet,
   1429      1.1  christos    inflate is waiting for these length bytes.
   1430      1.1  christos  */
   1431  1.1.1.4  christos int ZEXPORT inflateSyncPoint(z_streamp strm) {
   1432      1.1  christos     struct inflate_state FAR *state;
   1433      1.1  christos 
   1434  1.1.1.2  christos     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
   1435      1.1  christos     state = (struct inflate_state FAR *)strm->state;
   1436      1.1  christos     return state->mode == STORED && state->bits == 0;
   1437      1.1  christos }
   1438      1.1  christos 
   1439  1.1.1.4  christos int ZEXPORT inflateCopy(z_streamp dest, z_streamp source) {
   1440      1.1  christos     struct inflate_state FAR *state;
   1441      1.1  christos     struct inflate_state FAR *copy;
   1442      1.1  christos     unsigned char FAR *window;
   1443      1.1  christos     unsigned wsize;
   1444      1.1  christos 
   1445      1.1  christos     /* check input */
   1446  1.1.1.2  christos     if (inflateStateCheck(source) || dest == Z_NULL)
   1447      1.1  christos         return Z_STREAM_ERROR;
   1448      1.1  christos     state = (struct inflate_state FAR *)source->state;
   1449      1.1  christos 
   1450      1.1  christos     /* allocate space */
   1451      1.1  christos     copy = (struct inflate_state FAR *)
   1452      1.1  christos            ZALLOC(source, 1, sizeof(struct inflate_state));
   1453      1.1  christos     if (copy == Z_NULL) return Z_MEM_ERROR;
   1454      1.1  christos     window = Z_NULL;
   1455      1.1  christos     if (state->window != Z_NULL) {
   1456      1.1  christos         window = (unsigned char FAR *)
   1457      1.1  christos                  ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
   1458      1.1  christos         if (window == Z_NULL) {
   1459      1.1  christos             ZFREE(source, copy);
   1460      1.1  christos             return Z_MEM_ERROR;
   1461      1.1  christos         }
   1462      1.1  christos     }
   1463      1.1  christos 
   1464      1.1  christos     /* copy state */
   1465  1.1.1.2  christos     zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
   1466  1.1.1.2  christos     zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
   1467  1.1.1.2  christos     copy->strm = dest;
   1468      1.1  christos     if (state->lencode >= state->codes &&
   1469      1.1  christos         state->lencode <= state->codes + ENOUGH - 1) {
   1470      1.1  christos         copy->lencode = copy->codes + (state->lencode - state->codes);
   1471      1.1  christos         copy->distcode = copy->codes + (state->distcode - state->codes);
   1472      1.1  christos     }
   1473      1.1  christos     copy->next = copy->codes + (state->next - state->codes);
   1474      1.1  christos     if (window != Z_NULL) {
   1475      1.1  christos         wsize = 1U << state->wbits;
   1476      1.1  christos         zmemcpy(window, state->window, wsize);
   1477      1.1  christos     }
   1478      1.1  christos     copy->window = window;
   1479      1.1  christos     dest->state = (struct internal_state FAR *)copy;
   1480      1.1  christos     return Z_OK;
   1481      1.1  christos }
   1482  1.1.1.2  christos 
   1483  1.1.1.4  christos int ZEXPORT inflateUndermine(z_streamp strm, int subvert) {
   1484  1.1.1.2  christos     struct inflate_state FAR *state;
   1485  1.1.1.2  christos 
   1486  1.1.1.2  christos     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
   1487  1.1.1.2  christos     state = (struct inflate_state FAR *)strm->state;
   1488  1.1.1.2  christos #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
   1489  1.1.1.2  christos     state->sane = !subvert;
   1490  1.1.1.2  christos     return Z_OK;
   1491  1.1.1.2  christos #else
   1492  1.1.1.2  christos     (void)subvert;
   1493  1.1.1.2  christos     state->sane = 1;
   1494  1.1.1.2  christos     return Z_DATA_ERROR;
   1495  1.1.1.2  christos #endif
   1496  1.1.1.2  christos }
   1497  1.1.1.2  christos 
   1498  1.1.1.4  christos int ZEXPORT inflateValidate(z_streamp strm, int check) {
   1499  1.1.1.2  christos     struct inflate_state FAR *state;
   1500  1.1.1.2  christos 
   1501  1.1.1.2  christos     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
   1502  1.1.1.2  christos     state = (struct inflate_state FAR *)strm->state;
   1503  1.1.1.3  christos     if (check && state->wrap)
   1504  1.1.1.2  christos         state->wrap |= 4;
   1505  1.1.1.2  christos     else
   1506  1.1.1.2  christos         state->wrap &= ~4;
   1507  1.1.1.2  christos     return Z_OK;
   1508  1.1.1.2  christos }
   1509  1.1.1.2  christos 
   1510  1.1.1.4  christos long ZEXPORT inflateMark(z_streamp strm) {
   1511  1.1.1.2  christos     struct inflate_state FAR *state;
   1512  1.1.1.2  christos 
   1513  1.1.1.2  christos     if (inflateStateCheck(strm))
   1514  1.1.1.2  christos         return -(1L << 16);
   1515  1.1.1.2  christos     state = (struct inflate_state FAR *)strm->state;
   1516  1.1.1.2  christos     return (long)(((unsigned long)((long)state->back)) << 16) +
   1517  1.1.1.2  christos         (state->mode == COPY ? state->length :
   1518  1.1.1.2  christos             (state->mode == MATCH ? state->was - state->length : 0));
   1519  1.1.1.2  christos }
   1520  1.1.1.2  christos 
   1521  1.1.1.4  christos unsigned long ZEXPORT inflateCodesUsed(z_streamp strm) {
   1522  1.1.1.2  christos     struct inflate_state FAR *state;
   1523  1.1.1.2  christos     if (inflateStateCheck(strm)) return (unsigned long)-1;
   1524  1.1.1.2  christos     state = (struct inflate_state FAR *)strm->state;
   1525  1.1.1.2  christos     return (unsigned long)(state->next - state->codes);
   1526  1.1.1.2  christos }
   1527