dbf-halloween2015

diff libs/zlib/inflate.c @ 1:c3f5c32cb210

barfed all the libraries in the source tree to make porting easier
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:36:56 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/zlib/inflate.c	Sun Nov 01 00:36:56 2015 +0200
     1.3 @@ -0,0 +1,1368 @@
     1.4 +/* inflate.c -- zlib decompression
     1.5 + * Copyright (C) 1995-2005 Mark Adler
     1.6 + * For conditions of distribution and use, see copyright notice in zlib.h
     1.7 + */
     1.8 +
     1.9 +/*
    1.10 + * Change history:
    1.11 + *
    1.12 + * 1.2.beta0    24 Nov 2002
    1.13 + * - First version -- complete rewrite of inflate to simplify code, avoid
    1.14 + *   creation of window when not needed, minimize use of window when it is
    1.15 + *   needed, make inffast.c even faster, implement gzip decoding, and to
    1.16 + *   improve code readability and style over the previous zlib inflate code
    1.17 + *
    1.18 + * 1.2.beta1    25 Nov 2002
    1.19 + * - Use pointers for available input and output checking in inffast.c
    1.20 + * - Remove input and output counters in inffast.c
    1.21 + * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
    1.22 + * - Remove unnecessary second byte pull from length extra in inffast.c
    1.23 + * - Unroll direct copy to three copies per loop in inffast.c
    1.24 + *
    1.25 + * 1.2.beta2    4 Dec 2002
    1.26 + * - Change external routine names to reduce potential conflicts
    1.27 + * - Correct filename to inffixed.h for fixed tables in inflate.c
    1.28 + * - Make hbuf[] unsigned char to match parameter type in inflate.c
    1.29 + * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
    1.30 + *   to avoid negation problem on Alphas (64 bit) in inflate.c
    1.31 + *
    1.32 + * 1.2.beta3    22 Dec 2002
    1.33 + * - Add comments on state->bits assertion in inffast.c
    1.34 + * - Add comments on op field in inftrees.h
    1.35 + * - Fix bug in reuse of allocated window after inflateReset()
    1.36 + * - Remove bit fields--back to byte structure for speed
    1.37 + * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
    1.38 + * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
    1.39 + * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
    1.40 + * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
    1.41 + * - Use local copies of stream next and avail values, as well as local bit
    1.42 + *   buffer and bit count in inflate()--for speed when inflate_fast() not used
    1.43 + *
    1.44 + * 1.2.beta4    1 Jan 2003
    1.45 + * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
    1.46 + * - Move a comment on output buffer sizes from inffast.c to inflate.c
    1.47 + * - Add comments in inffast.c to introduce the inflate_fast() routine
    1.48 + * - Rearrange window copies in inflate_fast() for speed and simplification
    1.49 + * - Unroll last copy for window match in inflate_fast()
    1.50 + * - Use local copies of window variables in inflate_fast() for speed
    1.51 + * - Pull out common write == 0 case for speed in inflate_fast()
    1.52 + * - Make op and len in inflate_fast() unsigned for consistency
    1.53 + * - Add FAR to lcode and dcode declarations in inflate_fast()
    1.54 + * - Simplified bad distance check in inflate_fast()
    1.55 + * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
    1.56 + *   source file infback.c to provide a call-back interface to inflate for
    1.57 + *   programs like gzip and unzip -- uses window as output buffer to avoid
    1.58 + *   window copying
    1.59 + *
    1.60 + * 1.2.beta5    1 Jan 2003
    1.61 + * - Improved inflateBack() interface to allow the caller to provide initial
    1.62 + *   input in strm.
    1.63 + * - Fixed stored blocks bug in inflateBack()
    1.64 + *
    1.65 + * 1.2.beta6    4 Jan 2003
    1.66 + * - Added comments in inffast.c on effectiveness of POSTINC
    1.67 + * - Typecasting all around to reduce compiler warnings
    1.68 + * - Changed loops from while (1) or do {} while (1) to for (;;), again to
    1.69 + *   make compilers happy
    1.70 + * - Changed type of window in inflateBackInit() to unsigned char *
    1.71 + *
    1.72 + * 1.2.beta7    27 Jan 2003
    1.73 + * - Changed many types to unsigned or unsigned short to avoid warnings
    1.74 + * - Added inflateCopy() function
    1.75 + *
    1.76 + * 1.2.0        9 Mar 2003
    1.77 + * - Changed inflateBack() interface to provide separate opaque descriptors
    1.78 + *   for the in() and out() functions
    1.79 + * - Changed inflateBack() argument and in_func typedef to swap the length
    1.80 + *   and buffer address return values for the input function
    1.81 + * - Check next_in and next_out for Z_NULL on entry to inflate()
    1.82 + *
    1.83 + * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
    1.84 + */
    1.85 +
    1.86 +#include "zutil.h"
    1.87 +#include "inftrees.h"
    1.88 +#include "inflate.h"
    1.89 +#include "inffast.h"
    1.90 +
    1.91 +#ifdef MAKEFIXED
    1.92 +#  ifndef BUILDFIXED
    1.93 +#    define BUILDFIXED
    1.94 +#  endif
    1.95 +#endif
    1.96 +
    1.97 +/* function prototypes */
    1.98 +local void fixedtables OF((struct inflate_state FAR *state));
    1.99 +local int updatewindow OF((z_streamp strm, unsigned out));
   1.100 +#ifdef BUILDFIXED
   1.101 +   void makefixed OF((void));
   1.102 +#endif
   1.103 +local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
   1.104 +                              unsigned len));
   1.105 +
   1.106 +int ZEXPORT inflateReset(strm)
   1.107 +z_streamp strm;
   1.108 +{
   1.109 +    struct inflate_state FAR *state;
   1.110 +
   1.111 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
   1.112 +    state = (struct inflate_state FAR *)strm->state;
   1.113 +    strm->total_in = strm->total_out = state->total = 0;
   1.114 +    strm->msg = Z_NULL;
   1.115 +    strm->adler = 1;        /* to support ill-conceived Java test suite */
   1.116 +    state->mode = HEAD;
   1.117 +    state->last = 0;
   1.118 +    state->havedict = 0;
   1.119 +    state->dmax = 32768U;
   1.120 +    state->head = Z_NULL;
   1.121 +    state->wsize = 0;
   1.122 +    state->whave = 0;
   1.123 +    state->write = 0;
   1.124 +    state->hold = 0;
   1.125 +    state->bits = 0;
   1.126 +    state->lencode = state->distcode = state->next = state->codes;
   1.127 +    Tracev((stderr, "inflate: reset\n"));
   1.128 +    return Z_OK;
   1.129 +}
   1.130 +
   1.131 +int ZEXPORT inflatePrime(strm, bits, value)
   1.132 +z_streamp strm;
   1.133 +int bits;
   1.134 +int value;
   1.135 +{
   1.136 +    struct inflate_state FAR *state;
   1.137 +
   1.138 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
   1.139 +    state = (struct inflate_state FAR *)strm->state;
   1.140 +    if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
   1.141 +    value &= (1L << bits) - 1;
   1.142 +    state->hold += value << state->bits;
   1.143 +    state->bits += bits;
   1.144 +    return Z_OK;
   1.145 +}
   1.146 +
   1.147 +int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
   1.148 +z_streamp strm;
   1.149 +int windowBits;
   1.150 +const char *version;
   1.151 +int stream_size;
   1.152 +{
   1.153 +    struct inflate_state FAR *state;
   1.154 +
   1.155 +    if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
   1.156 +        stream_size != (int)(sizeof(z_stream)))
   1.157 +        return Z_VERSION_ERROR;
   1.158 +    if (strm == Z_NULL) return Z_STREAM_ERROR;
   1.159 +    strm->msg = Z_NULL;                 /* in case we return an error */
   1.160 +    if (strm->zalloc == (alloc_func)0) {
   1.161 +        strm->zalloc = zcalloc;
   1.162 +        strm->opaque = (voidpf)0;
   1.163 +    }
   1.164 +    if (strm->zfree == (free_func)0) strm->zfree = zcfree;
   1.165 +    state = (struct inflate_state FAR *)
   1.166 +            ZALLOC(strm, 1, sizeof(struct inflate_state));
   1.167 +    if (state == Z_NULL) return Z_MEM_ERROR;
   1.168 +    Tracev((stderr, "inflate: allocated\n"));
   1.169 +    strm->state = (struct internal_state FAR *)state;
   1.170 +    if (windowBits < 0) {
   1.171 +        state->wrap = 0;
   1.172 +        windowBits = -windowBits;
   1.173 +    }
   1.174 +    else {
   1.175 +        state->wrap = (windowBits >> 4) + 1;
   1.176 +#ifdef GUNZIP
   1.177 +        if (windowBits < 48) windowBits &= 15;
   1.178 +#endif
   1.179 +    }
   1.180 +    if (windowBits < 8 || windowBits > 15) {
   1.181 +        ZFREE(strm, state);
   1.182 +        strm->state = Z_NULL;
   1.183 +        return Z_STREAM_ERROR;
   1.184 +    }
   1.185 +    state->wbits = (unsigned)windowBits;
   1.186 +    state->window = Z_NULL;
   1.187 +    return inflateReset(strm);
   1.188 +}
   1.189 +
   1.190 +int ZEXPORT inflateInit_(strm, version, stream_size)
   1.191 +z_streamp strm;
   1.192 +const char *version;
   1.193 +int stream_size;
   1.194 +{
   1.195 +    return inflateInit2_(strm, DEF_WBITS, version, stream_size);
   1.196 +}
   1.197 +
   1.198 +/*
   1.199 +   Return state with length and distance decoding tables and index sizes set to
   1.200 +   fixed code decoding.  Normally this returns fixed tables from inffixed.h.
   1.201 +   If BUILDFIXED is defined, then instead this routine builds the tables the
   1.202 +   first time it's called, and returns those tables the first time and
   1.203 +   thereafter.  This reduces the size of the code by about 2K bytes, in
   1.204 +   exchange for a little execution time.  However, BUILDFIXED should not be
   1.205 +   used for threaded applications, since the rewriting of the tables and virgin
   1.206 +   may not be thread-safe.
   1.207 + */
   1.208 +local void fixedtables(state)
   1.209 +struct inflate_state FAR *state;
   1.210 +{
   1.211 +#ifdef BUILDFIXED
   1.212 +    static int virgin = 1;
   1.213 +    static code *lenfix, *distfix;
   1.214 +    static code fixed[544];
   1.215 +
   1.216 +    /* build fixed huffman tables if first call (may not be thread safe) */
   1.217 +    if (virgin) {
   1.218 +        unsigned sym, bits;
   1.219 +        static code *next;
   1.220 +
   1.221 +        /* literal/length table */
   1.222 +        sym = 0;
   1.223 +        while (sym < 144) state->lens[sym++] = 8;
   1.224 +        while (sym < 256) state->lens[sym++] = 9;
   1.225 +        while (sym < 280) state->lens[sym++] = 7;
   1.226 +        while (sym < 288) state->lens[sym++] = 8;
   1.227 +        next = fixed;
   1.228 +        lenfix = next;
   1.229 +        bits = 9;
   1.230 +        inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
   1.231 +
   1.232 +        /* distance table */
   1.233 +        sym = 0;
   1.234 +        while (sym < 32) state->lens[sym++] = 5;
   1.235 +        distfix = next;
   1.236 +        bits = 5;
   1.237 +        inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
   1.238 +
   1.239 +        /* do this just once */
   1.240 +        virgin = 0;
   1.241 +    }
   1.242 +#else /* !BUILDFIXED */
   1.243 +#   include "inffixed.h"
   1.244 +#endif /* BUILDFIXED */
   1.245 +    state->lencode = lenfix;
   1.246 +    state->lenbits = 9;
   1.247 +    state->distcode = distfix;
   1.248 +    state->distbits = 5;
   1.249 +}
   1.250 +
   1.251 +#ifdef MAKEFIXED
   1.252 +#include <stdio.h>
   1.253 +
   1.254 +/*
   1.255 +   Write out the inffixed.h that is #include'd above.  Defining MAKEFIXED also
   1.256 +   defines BUILDFIXED, so the tables are built on the fly.  makefixed() writes
   1.257 +   those tables to stdout, which would be piped to inffixed.h.  A small program
   1.258 +   can simply call makefixed to do this:
   1.259 +
   1.260 +    void makefixed(void);
   1.261 +
   1.262 +    int main(void)
   1.263 +    {
   1.264 +        makefixed();
   1.265 +        return 0;
   1.266 +    }
   1.267 +
   1.268 +   Then that can be linked with zlib built with MAKEFIXED defined and run:
   1.269 +
   1.270 +    a.out > inffixed.h
   1.271 + */
   1.272 +void makefixed()
   1.273 +{
   1.274 +    unsigned low, size;
   1.275 +    struct inflate_state state;
   1.276 +
   1.277 +    fixedtables(&state);
   1.278 +    puts("    /* inffixed.h -- table for decoding fixed codes");
   1.279 +    puts("     * Generated automatically by makefixed().");
   1.280 +    puts("     */");
   1.281 +    puts("");
   1.282 +    puts("    /* WARNING: this file should *not* be used by applications.");
   1.283 +    puts("       It is part of the implementation of this library and is");
   1.284 +    puts("       subject to change. Applications should only use zlib.h.");
   1.285 +    puts("     */");
   1.286 +    puts("");
   1.287 +    size = 1U << 9;
   1.288 +    printf("    static const code lenfix[%u] = {", size);
   1.289 +    low = 0;
   1.290 +    for (;;) {
   1.291 +        if ((low % 7) == 0) printf("\n        ");
   1.292 +        printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
   1.293 +               state.lencode[low].val);
   1.294 +        if (++low == size) break;
   1.295 +        putchar(',');
   1.296 +    }
   1.297 +    puts("\n    };");
   1.298 +    size = 1U << 5;
   1.299 +    printf("\n    static const code distfix[%u] = {", size);
   1.300 +    low = 0;
   1.301 +    for (;;) {
   1.302 +        if ((low % 6) == 0) printf("\n        ");
   1.303 +        printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
   1.304 +               state.distcode[low].val);
   1.305 +        if (++low == size) break;
   1.306 +        putchar(',');
   1.307 +    }
   1.308 +    puts("\n    };");
   1.309 +}
   1.310 +#endif /* MAKEFIXED */
   1.311 +
   1.312 +/*
   1.313 +   Update the window with the last wsize (normally 32K) bytes written before
   1.314 +   returning.  If window does not exist yet, create it.  This is only called
   1.315 +   when a window is already in use, or when output has been written during this
   1.316 +   inflate call, but the end of the deflate stream has not been reached yet.
   1.317 +   It is also called to create a window for dictionary data when a dictionary
   1.318 +   is loaded.
   1.319 +
   1.320 +   Providing output buffers larger than 32K to inflate() should provide a speed
   1.321 +   advantage, since only the last 32K of output is copied to the sliding window
   1.322 +   upon return from inflate(), and since all distances after the first 32K of
   1.323 +   output will fall in the output data, making match copies simpler and faster.
   1.324 +   The advantage may be dependent on the size of the processor's data caches.
   1.325 + */
   1.326 +local int updatewindow(strm, out)
   1.327 +z_streamp strm;
   1.328 +unsigned out;
   1.329 +{
   1.330 +    struct inflate_state FAR *state;
   1.331 +    unsigned copy, dist;
   1.332 +
   1.333 +    state = (struct inflate_state FAR *)strm->state;
   1.334 +
   1.335 +    /* if it hasn't been done already, allocate space for the window */
   1.336 +    if (state->window == Z_NULL) {
   1.337 +        state->window = (unsigned char FAR *)
   1.338 +                        ZALLOC(strm, 1U << state->wbits,
   1.339 +                               sizeof(unsigned char));
   1.340 +        if (state->window == Z_NULL) return 1;
   1.341 +    }
   1.342 +
   1.343 +    /* if window not in use yet, initialize */
   1.344 +    if (state->wsize == 0) {
   1.345 +        state->wsize = 1U << state->wbits;
   1.346 +        state->write = 0;
   1.347 +        state->whave = 0;
   1.348 +    }
   1.349 +
   1.350 +    /* copy state->wsize or less output bytes into the circular window */
   1.351 +    copy = out - strm->avail_out;
   1.352 +    if (copy >= state->wsize) {
   1.353 +        zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
   1.354 +        state->write = 0;
   1.355 +        state->whave = state->wsize;
   1.356 +    }
   1.357 +    else {
   1.358 +        dist = state->wsize - state->write;
   1.359 +        if (dist > copy) dist = copy;
   1.360 +        zmemcpy(state->window + state->write, strm->next_out - copy, dist);
   1.361 +        copy -= dist;
   1.362 +        if (copy) {
   1.363 +            zmemcpy(state->window, strm->next_out - copy, copy);
   1.364 +            state->write = copy;
   1.365 +            state->whave = state->wsize;
   1.366 +        }
   1.367 +        else {
   1.368 +            state->write += dist;
   1.369 +            if (state->write == state->wsize) state->write = 0;
   1.370 +            if (state->whave < state->wsize) state->whave += dist;
   1.371 +        }
   1.372 +    }
   1.373 +    return 0;
   1.374 +}
   1.375 +
   1.376 +/* Macros for inflate(): */
   1.377 +
   1.378 +/* check function to use adler32() for zlib or crc32() for gzip */
   1.379 +#ifdef GUNZIP
   1.380 +#  define UPDATE(check, buf, len) \
   1.381 +    (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
   1.382 +#else
   1.383 +#  define UPDATE(check, buf, len) adler32(check, buf, len)
   1.384 +#endif
   1.385 +
   1.386 +/* check macros for header crc */
   1.387 +#ifdef GUNZIP
   1.388 +#  define CRC2(check, word) \
   1.389 +    do { \
   1.390 +        hbuf[0] = (unsigned char)(word); \
   1.391 +        hbuf[1] = (unsigned char)((word) >> 8); \
   1.392 +        check = crc32(check, hbuf, 2); \
   1.393 +    } while (0)
   1.394 +
   1.395 +#  define CRC4(check, word) \
   1.396 +    do { \
   1.397 +        hbuf[0] = (unsigned char)(word); \
   1.398 +        hbuf[1] = (unsigned char)((word) >> 8); \
   1.399 +        hbuf[2] = (unsigned char)((word) >> 16); \
   1.400 +        hbuf[3] = (unsigned char)((word) >> 24); \
   1.401 +        check = crc32(check, hbuf, 4); \
   1.402 +    } while (0)
   1.403 +#endif
   1.404 +
   1.405 +/* Load registers with state in inflate() for speed */
   1.406 +#define LOAD() \
   1.407 +    do { \
   1.408 +        put = strm->next_out; \
   1.409 +        left = strm->avail_out; \
   1.410 +        next = strm->next_in; \
   1.411 +        have = strm->avail_in; \
   1.412 +        hold = state->hold; \
   1.413 +        bits = state->bits; \
   1.414 +    } while (0)
   1.415 +
   1.416 +/* Restore state from registers in inflate() */
   1.417 +#define RESTORE() \
   1.418 +    do { \
   1.419 +        strm->next_out = put; \
   1.420 +        strm->avail_out = left; \
   1.421 +        strm->next_in = next; \
   1.422 +        strm->avail_in = have; \
   1.423 +        state->hold = hold; \
   1.424 +        state->bits = bits; \
   1.425 +    } while (0)
   1.426 +
   1.427 +/* Clear the input bit accumulator */
   1.428 +#define INITBITS() \
   1.429 +    do { \
   1.430 +        hold = 0; \
   1.431 +        bits = 0; \
   1.432 +    } while (0)
   1.433 +
   1.434 +/* Get a byte of input into the bit accumulator, or return from inflate()
   1.435 +   if there is no input available. */
   1.436 +#define PULLBYTE() \
   1.437 +    do { \
   1.438 +        if (have == 0) goto inf_leave; \
   1.439 +        have--; \
   1.440 +        hold += (unsigned long)(*next++) << bits; \
   1.441 +        bits += 8; \
   1.442 +    } while (0)
   1.443 +
   1.444 +/* Assure that there are at least n bits in the bit accumulator.  If there is
   1.445 +   not enough available input to do that, then return from inflate(). */
   1.446 +#define NEEDBITS(n) \
   1.447 +    do { \
   1.448 +        while (bits < (unsigned)(n)) \
   1.449 +            PULLBYTE(); \
   1.450 +    } while (0)
   1.451 +
   1.452 +/* Return the low n bits of the bit accumulator (n < 16) */
   1.453 +#define BITS(n) \
   1.454 +    ((unsigned)hold & ((1U << (n)) - 1))
   1.455 +
   1.456 +/* Remove n bits from the bit accumulator */
   1.457 +#define DROPBITS(n) \
   1.458 +    do { \
   1.459 +        hold >>= (n); \
   1.460 +        bits -= (unsigned)(n); \
   1.461 +    } while (0)
   1.462 +
   1.463 +/* Remove zero to seven bits as needed to go to a byte boundary */
   1.464 +#define BYTEBITS() \
   1.465 +    do { \
   1.466 +        hold >>= bits & 7; \
   1.467 +        bits -= bits & 7; \
   1.468 +    } while (0)
   1.469 +
   1.470 +/* Reverse the bytes in a 32-bit value */
   1.471 +#define REVERSE(q) \
   1.472 +    ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
   1.473 +     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
   1.474 +
   1.475 +/*
   1.476 +   inflate() uses a state machine to process as much input data and generate as
   1.477 +   much output data as possible before returning.  The state machine is
   1.478 +   structured roughly as follows:
   1.479 +
   1.480 +    for (;;) switch (state) {
   1.481 +    ...
   1.482 +    case STATEn:
   1.483 +        if (not enough input data or output space to make progress)
   1.484 +            return;
   1.485 +        ... make progress ...
   1.486 +        state = STATEm;
   1.487 +        break;
   1.488 +    ...
   1.489 +    }
   1.490 +
   1.491 +   so when inflate() is called again, the same case is attempted again, and
   1.492 +   if the appropriate resources are provided, the machine proceeds to the
   1.493 +   next state.  The NEEDBITS() macro is usually the way the state evaluates
   1.494 +   whether it can proceed or should return.  NEEDBITS() does the return if
   1.495 +   the requested bits are not available.  The typical use of the BITS macros
   1.496 +   is:
   1.497 +
   1.498 +        NEEDBITS(n);
   1.499 +        ... do something with BITS(n) ...
   1.500 +        DROPBITS(n);
   1.501 +
   1.502 +   where NEEDBITS(n) either returns from inflate() if there isn't enough
   1.503 +   input left to load n bits into the accumulator, or it continues.  BITS(n)
   1.504 +   gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
   1.505 +   the low n bits off the accumulator.  INITBITS() clears the accumulator
   1.506 +   and sets the number of available bits to zero.  BYTEBITS() discards just
   1.507 +   enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
   1.508 +   and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
   1.509 +
   1.510 +   NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
   1.511 +   if there is no input available.  The decoding of variable length codes uses
   1.512 +   PULLBYTE() directly in order to pull just enough bytes to decode the next
   1.513 +   code, and no more.
   1.514 +
   1.515 +   Some states loop until they get enough input, making sure that enough
   1.516 +   state information is maintained to continue the loop where it left off
   1.517 +   if NEEDBITS() returns in the loop.  For example, want, need, and keep
   1.518 +   would all have to actually be part of the saved state in case NEEDBITS()
   1.519 +   returns:
   1.520 +
   1.521 +    case STATEw:
   1.522 +        while (want < need) {
   1.523 +            NEEDBITS(n);
   1.524 +            keep[want++] = BITS(n);
   1.525 +            DROPBITS(n);
   1.526 +        }
   1.527 +        state = STATEx;
   1.528 +    case STATEx:
   1.529 +
   1.530 +   As shown above, if the next state is also the next case, then the break
   1.531 +   is omitted.
   1.532 +
   1.533 +   A state may also return if there is not enough output space available to
   1.534 +   complete that state.  Those states are copying stored data, writing a
   1.535 +   literal byte, and copying a matching string.
   1.536 +
   1.537 +   When returning, a "goto inf_leave" is used to update the total counters,
   1.538 +   update the check value, and determine whether any progress has been made
   1.539 +   during that inflate() call in order to return the proper return code.
   1.540 +   Progress is defined as a change in either strm->avail_in or strm->avail_out.
   1.541 +   When there is a window, goto inf_leave will update the window with the last
   1.542 +   output written.  If a goto inf_leave occurs in the middle of decompression
   1.543 +   and there is no window currently, goto inf_leave will create one and copy
   1.544 +   output to the window for the next call of inflate().
   1.545 +
   1.546 +   In this implementation, the flush parameter of inflate() only affects the
   1.547 +   return code (per zlib.h).  inflate() always writes as much as possible to
   1.548 +   strm->next_out, given the space available and the provided input--the effect
   1.549 +   documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
   1.550 +   the allocation of and copying into a sliding window until necessary, which
   1.551 +   provides the effect documented in zlib.h for Z_FINISH when the entire input
   1.552 +   stream available.  So the only thing the flush parameter actually does is:
   1.553 +   when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
   1.554 +   will return Z_BUF_ERROR if it has not reached the end of the stream.
   1.555 + */
   1.556 +
   1.557 +int ZEXPORT inflate(strm, flush)
   1.558 +z_streamp strm;
   1.559 +int flush;
   1.560 +{
   1.561 +    struct inflate_state FAR *state;
   1.562 +    unsigned char FAR *next;    /* next input */
   1.563 +    unsigned char FAR *put;     /* next output */
   1.564 +    unsigned have, left;        /* available input and output */
   1.565 +    unsigned long hold;         /* bit buffer */
   1.566 +    unsigned bits;              /* bits in bit buffer */
   1.567 +    unsigned in, out;           /* save starting available input and output */
   1.568 +    unsigned copy;              /* number of stored or match bytes to copy */
   1.569 +    unsigned char FAR *from;    /* where to copy match bytes from */
   1.570 +    code this;                  /* current decoding table entry */
   1.571 +    code last;                  /* parent table entry */
   1.572 +    unsigned len;               /* length to copy for repeats, bits to drop */
   1.573 +    int ret;                    /* return code */
   1.574 +#ifdef GUNZIP
   1.575 +    unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
   1.576 +#endif
   1.577 +    static const unsigned short order[19] = /* permutation of code lengths */
   1.578 +        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
   1.579 +
   1.580 +    if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
   1.581 +        (strm->next_in == Z_NULL && strm->avail_in != 0))
   1.582 +        return Z_STREAM_ERROR;
   1.583 +
   1.584 +    state = (struct inflate_state FAR *)strm->state;
   1.585 +    if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
   1.586 +    LOAD();
   1.587 +    in = have;
   1.588 +    out = left;
   1.589 +    ret = Z_OK;
   1.590 +    for (;;)
   1.591 +        switch (state->mode) {
   1.592 +        case HEAD:
   1.593 +            if (state->wrap == 0) {
   1.594 +                state->mode = TYPEDO;
   1.595 +                break;
   1.596 +            }
   1.597 +            NEEDBITS(16);
   1.598 +#ifdef GUNZIP
   1.599 +            if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
   1.600 +                state->check = crc32(0L, Z_NULL, 0);
   1.601 +                CRC2(state->check, hold);
   1.602 +                INITBITS();
   1.603 +                state->mode = FLAGS;
   1.604 +                break;
   1.605 +            }
   1.606 +            state->flags = 0;           /* expect zlib header */
   1.607 +            if (state->head != Z_NULL)
   1.608 +                state->head->done = -1;
   1.609 +            if (!(state->wrap & 1) ||   /* check if zlib header allowed */
   1.610 +#else
   1.611 +            if (
   1.612 +#endif
   1.613 +                ((BITS(8) << 8) + (hold >> 8)) % 31) {
   1.614 +                strm->msg = (char *)"incorrect header check";
   1.615 +                state->mode = BAD;
   1.616 +                break;
   1.617 +            }
   1.618 +            if (BITS(4) != Z_DEFLATED) {
   1.619 +                strm->msg = (char *)"unknown compression method";
   1.620 +                state->mode = BAD;
   1.621 +                break;
   1.622 +            }
   1.623 +            DROPBITS(4);
   1.624 +            len = BITS(4) + 8;
   1.625 +            if (len > state->wbits) {
   1.626 +                strm->msg = (char *)"invalid window size";
   1.627 +                state->mode = BAD;
   1.628 +                break;
   1.629 +            }
   1.630 +            state->dmax = 1U << len;
   1.631 +            Tracev((stderr, "inflate:   zlib header ok\n"));
   1.632 +            strm->adler = state->check = adler32(0L, Z_NULL, 0);
   1.633 +            state->mode = hold & 0x200 ? DICTID : TYPE;
   1.634 +            INITBITS();
   1.635 +            break;
   1.636 +#ifdef GUNZIP
   1.637 +        case FLAGS:
   1.638 +            NEEDBITS(16);
   1.639 +            state->flags = (int)(hold);
   1.640 +            if ((state->flags & 0xff) != Z_DEFLATED) {
   1.641 +                strm->msg = (char *)"unknown compression method";
   1.642 +                state->mode = BAD;
   1.643 +                break;
   1.644 +            }
   1.645 +            if (state->flags & 0xe000) {
   1.646 +                strm->msg = (char *)"unknown header flags set";
   1.647 +                state->mode = BAD;
   1.648 +                break;
   1.649 +            }
   1.650 +            if (state->head != Z_NULL)
   1.651 +                state->head->text = (int)((hold >> 8) & 1);
   1.652 +            if (state->flags & 0x0200) CRC2(state->check, hold);
   1.653 +            INITBITS();
   1.654 +            state->mode = TIME;
   1.655 +        case TIME:
   1.656 +            NEEDBITS(32);
   1.657 +            if (state->head != Z_NULL)
   1.658 +                state->head->time = hold;
   1.659 +            if (state->flags & 0x0200) CRC4(state->check, hold);
   1.660 +            INITBITS();
   1.661 +            state->mode = OS;
   1.662 +        case OS:
   1.663 +            NEEDBITS(16);
   1.664 +            if (state->head != Z_NULL) {
   1.665 +                state->head->xflags = (int)(hold & 0xff);
   1.666 +                state->head->os = (int)(hold >> 8);
   1.667 +            }
   1.668 +            if (state->flags & 0x0200) CRC2(state->check, hold);
   1.669 +            INITBITS();
   1.670 +            state->mode = EXLEN;
   1.671 +        case EXLEN:
   1.672 +            if (state->flags & 0x0400) {
   1.673 +                NEEDBITS(16);
   1.674 +                state->length = (unsigned)(hold);
   1.675 +                if (state->head != Z_NULL)
   1.676 +                    state->head->extra_len = (unsigned)hold;
   1.677 +                if (state->flags & 0x0200) CRC2(state->check, hold);
   1.678 +                INITBITS();
   1.679 +            }
   1.680 +            else if (state->head != Z_NULL)
   1.681 +                state->head->extra = Z_NULL;
   1.682 +            state->mode = EXTRA;
   1.683 +        case EXTRA:
   1.684 +            if (state->flags & 0x0400) {
   1.685 +                copy = state->length;
   1.686 +                if (copy > have) copy = have;
   1.687 +                if (copy) {
   1.688 +                    if (state->head != Z_NULL &&
   1.689 +                        state->head->extra != Z_NULL) {
   1.690 +                        len = state->head->extra_len - state->length;
   1.691 +                        zmemcpy(state->head->extra + len, next,
   1.692 +                                len + copy > state->head->extra_max ?
   1.693 +                                state->head->extra_max - len : copy);
   1.694 +                    }
   1.695 +                    if (state->flags & 0x0200)
   1.696 +                        state->check = crc32(state->check, next, copy);
   1.697 +                    have -= copy;
   1.698 +                    next += copy;
   1.699 +                    state->length -= copy;
   1.700 +                }
   1.701 +                if (state->length) goto inf_leave;
   1.702 +            }
   1.703 +            state->length = 0;
   1.704 +            state->mode = NAME;
   1.705 +        case NAME:
   1.706 +            if (state->flags & 0x0800) {
   1.707 +                if (have == 0) goto inf_leave;
   1.708 +                copy = 0;
   1.709 +                do {
   1.710 +                    len = (unsigned)(next[copy++]);
   1.711 +                    if (state->head != Z_NULL &&
   1.712 +                            state->head->name != Z_NULL &&
   1.713 +                            state->length < state->head->name_max)
   1.714 +                        state->head->name[state->length++] = len;
   1.715 +                } while (len && copy < have);
   1.716 +                if (state->flags & 0x0200)
   1.717 +                    state->check = crc32(state->check, next, copy);
   1.718 +                have -= copy;
   1.719 +                next += copy;
   1.720 +                if (len) goto inf_leave;
   1.721 +            }
   1.722 +            else if (state->head != Z_NULL)
   1.723 +                state->head->name = Z_NULL;
   1.724 +            state->length = 0;
   1.725 +            state->mode = COMMENT;
   1.726 +        case COMMENT:
   1.727 +            if (state->flags & 0x1000) {
   1.728 +                if (have == 0) goto inf_leave;
   1.729 +                copy = 0;
   1.730 +                do {
   1.731 +                    len = (unsigned)(next[copy++]);
   1.732 +                    if (state->head != Z_NULL &&
   1.733 +                            state->head->comment != Z_NULL &&
   1.734 +                            state->length < state->head->comm_max)
   1.735 +                        state->head->comment[state->length++] = len;
   1.736 +                } while (len && copy < have);
   1.737 +                if (state->flags & 0x0200)
   1.738 +                    state->check = crc32(state->check, next, copy);
   1.739 +                have -= copy;
   1.740 +                next += copy;
   1.741 +                if (len) goto inf_leave;
   1.742 +            }
   1.743 +            else if (state->head != Z_NULL)
   1.744 +                state->head->comment = Z_NULL;
   1.745 +            state->mode = HCRC;
   1.746 +        case HCRC:
   1.747 +            if (state->flags & 0x0200) {
   1.748 +                NEEDBITS(16);
   1.749 +                if (hold != (state->check & 0xffff)) {
   1.750 +                    strm->msg = (char *)"header crc mismatch";
   1.751 +                    state->mode = BAD;
   1.752 +                    break;
   1.753 +                }
   1.754 +                INITBITS();
   1.755 +            }
   1.756 +            if (state->head != Z_NULL) {
   1.757 +                state->head->hcrc = (int)((state->flags >> 9) & 1);
   1.758 +                state->head->done = 1;
   1.759 +            }
   1.760 +            strm->adler = state->check = crc32(0L, Z_NULL, 0);
   1.761 +            state->mode = TYPE;
   1.762 +            break;
   1.763 +#endif
   1.764 +        case DICTID:
   1.765 +            NEEDBITS(32);
   1.766 +            strm->adler = state->check = REVERSE(hold);
   1.767 +            INITBITS();
   1.768 +            state->mode = DICT;
   1.769 +        case DICT:
   1.770 +            if (state->havedict == 0) {
   1.771 +                RESTORE();
   1.772 +                return Z_NEED_DICT;
   1.773 +            }
   1.774 +            strm->adler = state->check = adler32(0L, Z_NULL, 0);
   1.775 +            state->mode = TYPE;
   1.776 +        case TYPE:
   1.777 +            if (flush == Z_BLOCK) goto inf_leave;
   1.778 +        case TYPEDO:
   1.779 +            if (state->last) {
   1.780 +                BYTEBITS();
   1.781 +                state->mode = CHECK;
   1.782 +                break;
   1.783 +            }
   1.784 +            NEEDBITS(3);
   1.785 +            state->last = BITS(1);
   1.786 +            DROPBITS(1);
   1.787 +            switch (BITS(2)) {
   1.788 +            case 0:                             /* stored block */
   1.789 +                Tracev((stderr, "inflate:     stored block%s\n",
   1.790 +                        state->last ? " (last)" : ""));
   1.791 +                state->mode = STORED;
   1.792 +                break;
   1.793 +            case 1:                             /* fixed block */
   1.794 +                fixedtables(state);
   1.795 +                Tracev((stderr, "inflate:     fixed codes block%s\n",
   1.796 +                        state->last ? " (last)" : ""));
   1.797 +                state->mode = LEN;              /* decode codes */
   1.798 +                break;
   1.799 +            case 2:                             /* dynamic block */
   1.800 +                Tracev((stderr, "inflate:     dynamic codes block%s\n",
   1.801 +                        state->last ? " (last)" : ""));
   1.802 +                state->mode = TABLE;
   1.803 +                break;
   1.804 +            case 3:
   1.805 +                strm->msg = (char *)"invalid block type";
   1.806 +                state->mode = BAD;
   1.807 +            }
   1.808 +            DROPBITS(2);
   1.809 +            break;
   1.810 +        case STORED:
   1.811 +            BYTEBITS();                         /* go to byte boundary */
   1.812 +            NEEDBITS(32);
   1.813 +            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
   1.814 +                strm->msg = (char *)"invalid stored block lengths";
   1.815 +                state->mode = BAD;
   1.816 +                break;
   1.817 +            }
   1.818 +            state->length = (unsigned)hold & 0xffff;
   1.819 +            Tracev((stderr, "inflate:       stored length %u\n",
   1.820 +                    state->length));
   1.821 +            INITBITS();
   1.822 +            state->mode = COPY;
   1.823 +        case COPY:
   1.824 +            copy = state->length;
   1.825 +            if (copy) {
   1.826 +                if (copy > have) copy = have;
   1.827 +                if (copy > left) copy = left;
   1.828 +                if (copy == 0) goto inf_leave;
   1.829 +                zmemcpy(put, next, copy);
   1.830 +                have -= copy;
   1.831 +                next += copy;
   1.832 +                left -= copy;
   1.833 +                put += copy;
   1.834 +                state->length -= copy;
   1.835 +                break;
   1.836 +            }
   1.837 +            Tracev((stderr, "inflate:       stored end\n"));
   1.838 +            state->mode = TYPE;
   1.839 +            break;
   1.840 +        case TABLE:
   1.841 +            NEEDBITS(14);
   1.842 +            state->nlen = BITS(5) + 257;
   1.843 +            DROPBITS(5);
   1.844 +            state->ndist = BITS(5) + 1;
   1.845 +            DROPBITS(5);
   1.846 +            state->ncode = BITS(4) + 4;
   1.847 +            DROPBITS(4);
   1.848 +#ifndef PKZIP_BUG_WORKAROUND
   1.849 +            if (state->nlen > 286 || state->ndist > 30) {
   1.850 +                strm->msg = (char *)"too many length or distance symbols";
   1.851 +                state->mode = BAD;
   1.852 +                break;
   1.853 +            }
   1.854 +#endif
   1.855 +            Tracev((stderr, "inflate:       table sizes ok\n"));
   1.856 +            state->have = 0;
   1.857 +            state->mode = LENLENS;
   1.858 +        case LENLENS:
   1.859 +            while (state->have < state->ncode) {
   1.860 +                NEEDBITS(3);
   1.861 +                state->lens[order[state->have++]] = (unsigned short)BITS(3);
   1.862 +                DROPBITS(3);
   1.863 +            }
   1.864 +            while (state->have < 19)
   1.865 +                state->lens[order[state->have++]] = 0;
   1.866 +            state->next = state->codes;
   1.867 +            state->lencode = (code const FAR *)(state->next);
   1.868 +            state->lenbits = 7;
   1.869 +            ret = inflate_table(CODES, state->lens, 19, &(state->next),
   1.870 +                                &(state->lenbits), state->work);
   1.871 +            if (ret) {
   1.872 +                strm->msg = (char *)"invalid code lengths set";
   1.873 +                state->mode = BAD;
   1.874 +                break;
   1.875 +            }
   1.876 +            Tracev((stderr, "inflate:       code lengths ok\n"));
   1.877 +            state->have = 0;
   1.878 +            state->mode = CODELENS;
   1.879 +        case CODELENS:
   1.880 +            while (state->have < state->nlen + state->ndist) {
   1.881 +                for (;;) {
   1.882 +                    this = state->lencode[BITS(state->lenbits)];
   1.883 +                    if ((unsigned)(this.bits) <= bits) break;
   1.884 +                    PULLBYTE();
   1.885 +                }
   1.886 +                if (this.val < 16) {
   1.887 +                    NEEDBITS(this.bits);
   1.888 +                    DROPBITS(this.bits);
   1.889 +                    state->lens[state->have++] = this.val;
   1.890 +                }
   1.891 +                else {
   1.892 +                    if (this.val == 16) {
   1.893 +                        NEEDBITS(this.bits + 2);
   1.894 +                        DROPBITS(this.bits);
   1.895 +                        if (state->have == 0) {
   1.896 +                            strm->msg = (char *)"invalid bit length repeat";
   1.897 +                            state->mode = BAD;
   1.898 +                            break;
   1.899 +                        }
   1.900 +                        len = state->lens[state->have - 1];
   1.901 +                        copy = 3 + BITS(2);
   1.902 +                        DROPBITS(2);
   1.903 +                    }
   1.904 +                    else if (this.val == 17) {
   1.905 +                        NEEDBITS(this.bits + 3);
   1.906 +                        DROPBITS(this.bits);
   1.907 +                        len = 0;
   1.908 +                        copy = 3 + BITS(3);
   1.909 +                        DROPBITS(3);
   1.910 +                    }
   1.911 +                    else {
   1.912 +                        NEEDBITS(this.bits + 7);
   1.913 +                        DROPBITS(this.bits);
   1.914 +                        len = 0;
   1.915 +                        copy = 11 + BITS(7);
   1.916 +                        DROPBITS(7);
   1.917 +                    }
   1.918 +                    if (state->have + copy > state->nlen + state->ndist) {
   1.919 +                        strm->msg = (char *)"invalid bit length repeat";
   1.920 +                        state->mode = BAD;
   1.921 +                        break;
   1.922 +                    }
   1.923 +                    while (copy--)
   1.924 +                        state->lens[state->have++] = (unsigned short)len;
   1.925 +                }
   1.926 +            }
   1.927 +
   1.928 +            /* handle error breaks in while */
   1.929 +            if (state->mode == BAD) break;
   1.930 +
   1.931 +            /* build code tables */
   1.932 +            state->next = state->codes;
   1.933 +            state->lencode = (code const FAR *)(state->next);
   1.934 +            state->lenbits = 9;
   1.935 +            ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
   1.936 +                                &(state->lenbits), state->work);
   1.937 +            if (ret) {
   1.938 +                strm->msg = (char *)"invalid literal/lengths set";
   1.939 +                state->mode = BAD;
   1.940 +                break;
   1.941 +            }
   1.942 +            state->distcode = (code const FAR *)(state->next);
   1.943 +            state->distbits = 6;
   1.944 +            ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
   1.945 +                            &(state->next), &(state->distbits), state->work);
   1.946 +            if (ret) {
   1.947 +                strm->msg = (char *)"invalid distances set";
   1.948 +                state->mode = BAD;
   1.949 +                break;
   1.950 +            }
   1.951 +            Tracev((stderr, "inflate:       codes ok\n"));
   1.952 +            state->mode = LEN;
   1.953 +        case LEN:
   1.954 +            if (have >= 6 && left >= 258) {
   1.955 +                RESTORE();
   1.956 +                inflate_fast(strm, out);
   1.957 +                LOAD();
   1.958 +                break;
   1.959 +            }
   1.960 +            for (;;) {
   1.961 +                this = state->lencode[BITS(state->lenbits)];
   1.962 +                if ((unsigned)(this.bits) <= bits) break;
   1.963 +                PULLBYTE();
   1.964 +            }
   1.965 +            if (this.op && (this.op & 0xf0) == 0) {
   1.966 +                last = this;
   1.967 +                for (;;) {
   1.968 +                    this = state->lencode[last.val +
   1.969 +                            (BITS(last.bits + last.op) >> last.bits)];
   1.970 +                    if ((unsigned)(last.bits + this.bits) <= bits) break;
   1.971 +                    PULLBYTE();
   1.972 +                }
   1.973 +                DROPBITS(last.bits);
   1.974 +            }
   1.975 +            DROPBITS(this.bits);
   1.976 +            state->length = (unsigned)this.val;
   1.977 +            if ((int)(this.op) == 0) {
   1.978 +                Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
   1.979 +                        "inflate:         literal '%c'\n" :
   1.980 +                        "inflate:         literal 0x%02x\n", this.val));
   1.981 +                state->mode = LIT;
   1.982 +                break;
   1.983 +            }
   1.984 +            if (this.op & 32) {
   1.985 +                Tracevv((stderr, "inflate:         end of block\n"));
   1.986 +                state->mode = TYPE;
   1.987 +                break;
   1.988 +            }
   1.989 +            if (this.op & 64) {
   1.990 +                strm->msg = (char *)"invalid literal/length code";
   1.991 +                state->mode = BAD;
   1.992 +                break;
   1.993 +            }
   1.994 +            state->extra = (unsigned)(this.op) & 15;
   1.995 +            state->mode = LENEXT;
   1.996 +        case LENEXT:
   1.997 +            if (state->extra) {
   1.998 +                NEEDBITS(state->extra);
   1.999 +                state->length += BITS(state->extra);
  1.1000 +                DROPBITS(state->extra);
  1.1001 +            }
  1.1002 +            Tracevv((stderr, "inflate:         length %u\n", state->length));
  1.1003 +            state->mode = DIST;
  1.1004 +        case DIST:
  1.1005 +            for (;;) {
  1.1006 +                this = state->distcode[BITS(state->distbits)];
  1.1007 +                if ((unsigned)(this.bits) <= bits) break;
  1.1008 +                PULLBYTE();
  1.1009 +            }
  1.1010 +            if ((this.op & 0xf0) == 0) {
  1.1011 +                last = this;
  1.1012 +                for (;;) {
  1.1013 +                    this = state->distcode[last.val +
  1.1014 +                            (BITS(last.bits + last.op) >> last.bits)];
  1.1015 +                    if ((unsigned)(last.bits + this.bits) <= bits) break;
  1.1016 +                    PULLBYTE();
  1.1017 +                }
  1.1018 +                DROPBITS(last.bits);
  1.1019 +            }
  1.1020 +            DROPBITS(this.bits);
  1.1021 +            if (this.op & 64) {
  1.1022 +                strm->msg = (char *)"invalid distance code";
  1.1023 +                state->mode = BAD;
  1.1024 +                break;
  1.1025 +            }
  1.1026 +            state->offset = (unsigned)this.val;
  1.1027 +            state->extra = (unsigned)(this.op) & 15;
  1.1028 +            state->mode = DISTEXT;
  1.1029 +        case DISTEXT:
  1.1030 +            if (state->extra) {
  1.1031 +                NEEDBITS(state->extra);
  1.1032 +                state->offset += BITS(state->extra);
  1.1033 +                DROPBITS(state->extra);
  1.1034 +            }
  1.1035 +#ifdef INFLATE_STRICT
  1.1036 +            if (state->offset > state->dmax) {
  1.1037 +                strm->msg = (char *)"invalid distance too far back";
  1.1038 +                state->mode = BAD;
  1.1039 +                break;
  1.1040 +            }
  1.1041 +#endif
  1.1042 +            if (state->offset > state->whave + out - left) {
  1.1043 +                strm->msg = (char *)"invalid distance too far back";
  1.1044 +                state->mode = BAD;
  1.1045 +                break;
  1.1046 +            }
  1.1047 +            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
  1.1048 +            state->mode = MATCH;
  1.1049 +        case MATCH:
  1.1050 +            if (left == 0) goto inf_leave;
  1.1051 +            copy = out - left;
  1.1052 +            if (state->offset > copy) {         /* copy from window */
  1.1053 +                copy = state->offset - copy;
  1.1054 +                if (copy > state->write) {
  1.1055 +                    copy -= state->write;
  1.1056 +                    from = state->window + (state->wsize - copy);
  1.1057 +                }
  1.1058 +                else
  1.1059 +                    from = state->window + (state->write - copy);
  1.1060 +                if (copy > state->length) copy = state->length;
  1.1061 +            }
  1.1062 +            else {                              /* copy from output */
  1.1063 +                from = put - state->offset;
  1.1064 +                copy = state->length;
  1.1065 +            }
  1.1066 +            if (copy > left) copy = left;
  1.1067 +            left -= copy;
  1.1068 +            state->length -= copy;
  1.1069 +            do {
  1.1070 +                *put++ = *from++;
  1.1071 +            } while (--copy);
  1.1072 +            if (state->length == 0) state->mode = LEN;
  1.1073 +            break;
  1.1074 +        case LIT:
  1.1075 +            if (left == 0) goto inf_leave;
  1.1076 +            *put++ = (unsigned char)(state->length);
  1.1077 +            left--;
  1.1078 +            state->mode = LEN;
  1.1079 +            break;
  1.1080 +        case CHECK:
  1.1081 +            if (state->wrap) {
  1.1082 +                NEEDBITS(32);
  1.1083 +                out -= left;
  1.1084 +                strm->total_out += out;
  1.1085 +                state->total += out;
  1.1086 +                if (out)
  1.1087 +                    strm->adler = state->check =
  1.1088 +                        UPDATE(state->check, put - out, out);
  1.1089 +                out = left;
  1.1090 +                if ((
  1.1091 +#ifdef GUNZIP
  1.1092 +                     state->flags ? hold :
  1.1093 +#endif
  1.1094 +                     REVERSE(hold)) != state->check) {
  1.1095 +                    strm->msg = (char *)"incorrect data check";
  1.1096 +                    state->mode = BAD;
  1.1097 +                    break;
  1.1098 +                }
  1.1099 +                INITBITS();
  1.1100 +                Tracev((stderr, "inflate:   check matches trailer\n"));
  1.1101 +            }
  1.1102 +#ifdef GUNZIP
  1.1103 +            state->mode = LENGTH;
  1.1104 +        case LENGTH:
  1.1105 +            if (state->wrap && state->flags) {
  1.1106 +                NEEDBITS(32);
  1.1107 +                if (hold != (state->total & 0xffffffffUL)) {
  1.1108 +                    strm->msg = (char *)"incorrect length check";
  1.1109 +                    state->mode = BAD;
  1.1110 +                    break;
  1.1111 +                }
  1.1112 +                INITBITS();
  1.1113 +                Tracev((stderr, "inflate:   length matches trailer\n"));
  1.1114 +            }
  1.1115 +#endif
  1.1116 +            state->mode = DONE;
  1.1117 +        case DONE:
  1.1118 +            ret = Z_STREAM_END;
  1.1119 +            goto inf_leave;
  1.1120 +        case BAD:
  1.1121 +            ret = Z_DATA_ERROR;
  1.1122 +            goto inf_leave;
  1.1123 +        case MEM:
  1.1124 +            return Z_MEM_ERROR;
  1.1125 +        case SYNC:
  1.1126 +        default:
  1.1127 +            return Z_STREAM_ERROR;
  1.1128 +        }
  1.1129 +
  1.1130 +    /*
  1.1131 +       Return from inflate(), updating the total counts and the check value.
  1.1132 +       If there was no progress during the inflate() call, return a buffer
  1.1133 +       error.  Call updatewindow() to create and/or update the window state.
  1.1134 +       Note: a memory error from inflate() is non-recoverable.
  1.1135 +     */
  1.1136 +  inf_leave:
  1.1137 +    RESTORE();
  1.1138 +    if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
  1.1139 +        if (updatewindow(strm, out)) {
  1.1140 +            state->mode = MEM;
  1.1141 +            return Z_MEM_ERROR;
  1.1142 +        }
  1.1143 +    in -= strm->avail_in;
  1.1144 +    out -= strm->avail_out;
  1.1145 +    strm->total_in += in;
  1.1146 +    strm->total_out += out;
  1.1147 +    state->total += out;
  1.1148 +    if (state->wrap && out)
  1.1149 +        strm->adler = state->check =
  1.1150 +            UPDATE(state->check, strm->next_out - out, out);
  1.1151 +    strm->data_type = state->bits + (state->last ? 64 : 0) +
  1.1152 +                      (state->mode == TYPE ? 128 : 0);
  1.1153 +    if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
  1.1154 +        ret = Z_BUF_ERROR;
  1.1155 +    return ret;
  1.1156 +}
  1.1157 +
  1.1158 +int ZEXPORT inflateEnd(strm)
  1.1159 +z_streamp strm;
  1.1160 +{
  1.1161 +    struct inflate_state FAR *state;
  1.1162 +    if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
  1.1163 +        return Z_STREAM_ERROR;
  1.1164 +    state = (struct inflate_state FAR *)strm->state;
  1.1165 +    if (state->window != Z_NULL) ZFREE(strm, state->window);
  1.1166 +    ZFREE(strm, strm->state);
  1.1167 +    strm->state = Z_NULL;
  1.1168 +    Tracev((stderr, "inflate: end\n"));
  1.1169 +    return Z_OK;
  1.1170 +}
  1.1171 +
  1.1172 +int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
  1.1173 +z_streamp strm;
  1.1174 +const Bytef *dictionary;
  1.1175 +uInt dictLength;
  1.1176 +{
  1.1177 +    struct inflate_state FAR *state;
  1.1178 +    unsigned long id;
  1.1179 +
  1.1180 +    /* check state */
  1.1181 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1.1182 +    state = (struct inflate_state FAR *)strm->state;
  1.1183 +    if (state->wrap != 0 && state->mode != DICT)
  1.1184 +        return Z_STREAM_ERROR;
  1.1185 +
  1.1186 +    /* check for correct dictionary id */
  1.1187 +    if (state->mode == DICT) {
  1.1188 +        id = adler32(0L, Z_NULL, 0);
  1.1189 +        id = adler32(id, dictionary, dictLength);
  1.1190 +        if (id != state->check)
  1.1191 +            return Z_DATA_ERROR;
  1.1192 +    }
  1.1193 +
  1.1194 +    /* copy dictionary to window */
  1.1195 +    if (updatewindow(strm, strm->avail_out)) {
  1.1196 +        state->mode = MEM;
  1.1197 +        return Z_MEM_ERROR;
  1.1198 +    }
  1.1199 +    if (dictLength > state->wsize) {
  1.1200 +        zmemcpy(state->window, dictionary + dictLength - state->wsize,
  1.1201 +                state->wsize);
  1.1202 +        state->whave = state->wsize;
  1.1203 +    }
  1.1204 +    else {
  1.1205 +        zmemcpy(state->window + state->wsize - dictLength, dictionary,
  1.1206 +                dictLength);
  1.1207 +        state->whave = dictLength;
  1.1208 +    }
  1.1209 +    state->havedict = 1;
  1.1210 +    Tracev((stderr, "inflate:   dictionary set\n"));
  1.1211 +    return Z_OK;
  1.1212 +}
  1.1213 +
  1.1214 +int ZEXPORT inflateGetHeader(strm, head)
  1.1215 +z_streamp strm;
  1.1216 +gz_headerp head;
  1.1217 +{
  1.1218 +    struct inflate_state FAR *state;
  1.1219 +
  1.1220 +    /* check state */
  1.1221 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1.1222 +    state = (struct inflate_state FAR *)strm->state;
  1.1223 +    if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
  1.1224 +
  1.1225 +    /* save header structure */
  1.1226 +    state->head = head;
  1.1227 +    head->done = 0;
  1.1228 +    return Z_OK;
  1.1229 +}
  1.1230 +
  1.1231 +/*
  1.1232 +   Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
  1.1233 +   or when out of input.  When called, *have is the number of pattern bytes
  1.1234 +   found in order so far, in 0..3.  On return *have is updated to the new
  1.1235 +   state.  If on return *have equals four, then the pattern was found and the
  1.1236 +   return value is how many bytes were read including the last byte of the
  1.1237 +   pattern.  If *have is less than four, then the pattern has not been found
  1.1238 +   yet and the return value is len.  In the latter case, syncsearch() can be
  1.1239 +   called again with more data and the *have state.  *have is initialized to
  1.1240 +   zero for the first call.
  1.1241 + */
  1.1242 +local unsigned syncsearch(have, buf, len)
  1.1243 +unsigned FAR *have;
  1.1244 +unsigned char FAR *buf;
  1.1245 +unsigned len;
  1.1246 +{
  1.1247 +    unsigned got;
  1.1248 +    unsigned next;
  1.1249 +
  1.1250 +    got = *have;
  1.1251 +    next = 0;
  1.1252 +    while (next < len && got < 4) {
  1.1253 +        if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
  1.1254 +            got++;
  1.1255 +        else if (buf[next])
  1.1256 +            got = 0;
  1.1257 +        else
  1.1258 +            got = 4 - got;
  1.1259 +        next++;
  1.1260 +    }
  1.1261 +    *have = got;
  1.1262 +    return next;
  1.1263 +}
  1.1264 +
  1.1265 +int ZEXPORT inflateSync(strm)
  1.1266 +z_streamp strm;
  1.1267 +{
  1.1268 +    unsigned len;               /* number of bytes to look at or looked at */
  1.1269 +    unsigned long in, out;      /* temporary to save total_in and total_out */
  1.1270 +    unsigned char buf[4];       /* to restore bit buffer to byte string */
  1.1271 +    struct inflate_state FAR *state;
  1.1272 +
  1.1273 +    /* check parameters */
  1.1274 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1.1275 +    state = (struct inflate_state FAR *)strm->state;
  1.1276 +    if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
  1.1277 +
  1.1278 +    /* if first time, start search in bit buffer */
  1.1279 +    if (state->mode != SYNC) {
  1.1280 +        state->mode = SYNC;
  1.1281 +        state->hold <<= state->bits & 7;
  1.1282 +        state->bits -= state->bits & 7;
  1.1283 +        len = 0;
  1.1284 +        while (state->bits >= 8) {
  1.1285 +            buf[len++] = (unsigned char)(state->hold);
  1.1286 +            state->hold >>= 8;
  1.1287 +            state->bits -= 8;
  1.1288 +        }
  1.1289 +        state->have = 0;
  1.1290 +        syncsearch(&(state->have), buf, len);
  1.1291 +    }
  1.1292 +
  1.1293 +    /* search available input */
  1.1294 +    len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
  1.1295 +    strm->avail_in -= len;
  1.1296 +    strm->next_in += len;
  1.1297 +    strm->total_in += len;
  1.1298 +
  1.1299 +    /* return no joy or set up to restart inflate() on a new block */
  1.1300 +    if (state->have != 4) return Z_DATA_ERROR;
  1.1301 +    in = strm->total_in;  out = strm->total_out;
  1.1302 +    inflateReset(strm);
  1.1303 +    strm->total_in = in;  strm->total_out = out;
  1.1304 +    state->mode = TYPE;
  1.1305 +    return Z_OK;
  1.1306 +}
  1.1307 +
  1.1308 +/*
  1.1309 +   Returns true if inflate is currently at the end of a block generated by
  1.1310 +   Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  1.1311 +   implementation to provide an additional safety check. PPP uses
  1.1312 +   Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
  1.1313 +   block. When decompressing, PPP checks that at the end of input packet,
  1.1314 +   inflate is waiting for these length bytes.
  1.1315 + */
  1.1316 +int ZEXPORT inflateSyncPoint(strm)
  1.1317 +z_streamp strm;
  1.1318 +{
  1.1319 +    struct inflate_state FAR *state;
  1.1320 +
  1.1321 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1.1322 +    state = (struct inflate_state FAR *)strm->state;
  1.1323 +    return state->mode == STORED && state->bits == 0;
  1.1324 +}
  1.1325 +
  1.1326 +int ZEXPORT inflateCopy(dest, source)
  1.1327 +z_streamp dest;
  1.1328 +z_streamp source;
  1.1329 +{
  1.1330 +    struct inflate_state FAR *state;
  1.1331 +    struct inflate_state FAR *copy;
  1.1332 +    unsigned char FAR *window;
  1.1333 +    unsigned wsize;
  1.1334 +
  1.1335 +    /* check input */
  1.1336 +    if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
  1.1337 +        source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
  1.1338 +        return Z_STREAM_ERROR;
  1.1339 +    state = (struct inflate_state FAR *)source->state;
  1.1340 +
  1.1341 +    /* allocate space */
  1.1342 +    copy = (struct inflate_state FAR *)
  1.1343 +           ZALLOC(source, 1, sizeof(struct inflate_state));
  1.1344 +    if (copy == Z_NULL) return Z_MEM_ERROR;
  1.1345 +    window = Z_NULL;
  1.1346 +    if (state->window != Z_NULL) {
  1.1347 +        window = (unsigned char FAR *)
  1.1348 +                 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
  1.1349 +        if (window == Z_NULL) {
  1.1350 +            ZFREE(source, copy);
  1.1351 +            return Z_MEM_ERROR;
  1.1352 +        }
  1.1353 +    }
  1.1354 +
  1.1355 +    /* copy state */
  1.1356 +    zmemcpy(dest, source, sizeof(z_stream));
  1.1357 +    zmemcpy(copy, state, sizeof(struct inflate_state));
  1.1358 +    if (state->lencode >= state->codes &&
  1.1359 +        state->lencode <= state->codes + ENOUGH - 1) {
  1.1360 +        copy->lencode = copy->codes + (state->lencode - state->codes);
  1.1361 +        copy->distcode = copy->codes + (state->distcode - state->codes);
  1.1362 +    }
  1.1363 +    copy->next = copy->codes + (state->next - state->codes);
  1.1364 +    if (window != Z_NULL) {
  1.1365 +        wsize = 1U << state->wbits;
  1.1366 +        zmemcpy(window, state->window, wsize);
  1.1367 +    }
  1.1368 +    copy->window = window;
  1.1369 +    dest->state = (struct internal_state FAR *)copy;
  1.1370 +    return Z_OK;
  1.1371 +}