1131377Stjr/* inflate.c -- zlib decompression
2311285Sdelphij * Copyright (C) 1995-2016 Mark Adler
3131377Stjr * For conditions of distribution and use, see copyright notice in zlib.h
417651Speter */
517651Speter
6131377Stjr/*
7131377Stjr * Change history:
8131377Stjr *
9131377Stjr * 1.2.beta0    24 Nov 2002
10131377Stjr * - First version -- complete rewrite of inflate to simplify code, avoid
11131377Stjr *   creation of window when not needed, minimize use of window when it is
12131377Stjr *   needed, make inffast.c even faster, implement gzip decoding, and to
13131377Stjr *   improve code readability and style over the previous zlib inflate code
14131377Stjr *
15131377Stjr * 1.2.beta1    25 Nov 2002
16131377Stjr * - Use pointers for available input and output checking in inffast.c
17131377Stjr * - Remove input and output counters in inffast.c
18131377Stjr * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
19131377Stjr * - Remove unnecessary second byte pull from length extra in inffast.c
20131377Stjr * - Unroll direct copy to three copies per loop in inffast.c
21131377Stjr *
22131377Stjr * 1.2.beta2    4 Dec 2002
23131377Stjr * - Change external routine names to reduce potential conflicts
24131377Stjr * - Correct filename to inffixed.h for fixed tables in inflate.c
25131377Stjr * - Make hbuf[] unsigned char to match parameter type in inflate.c
26131377Stjr * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
27131377Stjr *   to avoid negation problem on Alphas (64 bit) in inflate.c
28131377Stjr *
29131377Stjr * 1.2.beta3    22 Dec 2002
30131377Stjr * - Add comments on state->bits assertion in inffast.c
31131377Stjr * - Add comments on op field in inftrees.h
32131377Stjr * - Fix bug in reuse of allocated window after inflateReset()
33131377Stjr * - Remove bit fields--back to byte structure for speed
34131377Stjr * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
35131377Stjr * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
36131377Stjr * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
37131377Stjr * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
38131377Stjr * - Use local copies of stream next and avail values, as well as local bit
39131377Stjr *   buffer and bit count in inflate()--for speed when inflate_fast() not used
40131377Stjr *
41131377Stjr * 1.2.beta4    1 Jan 2003
42131377Stjr * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
43131377Stjr * - Move a comment on output buffer sizes from inffast.c to inflate.c
44131377Stjr * - Add comments in inffast.c to introduce the inflate_fast() routine
45131377Stjr * - Rearrange window copies in inflate_fast() for speed and simplification
46131377Stjr * - Unroll last copy for window match in inflate_fast()
47131377Stjr * - Use local copies of window variables in inflate_fast() for speed
48205194Sdelphij * - Pull out common wnext == 0 case for speed in inflate_fast()
49131377Stjr * - Make op and len in inflate_fast() unsigned for consistency
50131377Stjr * - Add FAR to lcode and dcode declarations in inflate_fast()
51131377Stjr * - Simplified bad distance check in inflate_fast()
52131377Stjr * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
53131377Stjr *   source file infback.c to provide a call-back interface to inflate for
54131377Stjr *   programs like gzip and unzip -- uses window as output buffer to avoid
55131377Stjr *   window copying
56131377Stjr *
57131377Stjr * 1.2.beta5    1 Jan 2003
58131377Stjr * - Improved inflateBack() interface to allow the caller to provide initial
59131377Stjr *   input in strm.
60131377Stjr * - Fixed stored blocks bug in inflateBack()
61131377Stjr *
62131377Stjr * 1.2.beta6    4 Jan 2003
63131377Stjr * - Added comments in inffast.c on effectiveness of POSTINC
64131377Stjr * - Typecasting all around to reduce compiler warnings
65131377Stjr * - Changed loops from while (1) or do {} while (1) to for (;;), again to
66131377Stjr *   make compilers happy
67131377Stjr * - Changed type of window in inflateBackInit() to unsigned char *
68131377Stjr *
69131377Stjr * 1.2.beta7    27 Jan 2003
70131377Stjr * - Changed many types to unsigned or unsigned short to avoid warnings
71131377Stjr * - Added inflateCopy() function
72131377Stjr *
73131377Stjr * 1.2.0        9 Mar 2003
74131377Stjr * - Changed inflateBack() interface to provide separate opaque descriptors
75131377Stjr *   for the in() and out() functions
76131377Stjr * - Changed inflateBack() argument and in_func typedef to swap the length
77131377Stjr *   and buffer address return values for the input function
78131377Stjr * - Check next_in and next_out for Z_NULL on entry to inflate()
79131377Stjr *
80131377Stjr * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
81131377Stjr */
82131377Stjr
8317651Speter#include "zutil.h"
84131377Stjr#include "inftrees.h"
85131377Stjr#include "inflate.h"
86131377Stjr#include "inffast.h"
8717651Speter
88131377Stjr#ifdef MAKEFIXED
89131377Stjr#  ifndef BUILDFIXED
90131377Stjr#    define BUILDFIXED
91131377Stjr#  endif
92131377Stjr#endif
9317651Speter
94131377Stjr/* function prototypes */
95311285Sdelphijlocal int inflateStateCheck OF((z_streamp strm));
96131377Stjrlocal void fixedtables OF((struct inflate_state FAR *state));
97250224Sdelphijlocal int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
98250224Sdelphij                           unsigned copy));
99131377Stjr#ifdef BUILDFIXED
100131377Stjr   void makefixed OF((void));
101131377Stjr#endif
102250224Sdelphijlocal unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
103131377Stjr                              unsigned len));
10417651Speter
105311285Sdelphijlocal int inflateStateCheck(strm)
106311285Sdelphijz_streamp strm;
107311285Sdelphij{
108311285Sdelphij    struct inflate_state FAR *state;
109311285Sdelphij    if (strm == Z_NULL ||
110311285Sdelphij        strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
111311285Sdelphij        return 1;
112311285Sdelphij    state = (struct inflate_state FAR *)strm->state;
113311285Sdelphij    if (state == Z_NULL || state->strm != strm ||
114311285Sdelphij        state->mode < HEAD || state->mode > SYNC)
115311285Sdelphij        return 1;
116311285Sdelphij    return 0;
117311285Sdelphij}
118311285Sdelphij
119230837Sdelphijint ZEXPORT inflateResetKeep(strm)
120131377Stjrz_streamp strm;
121131377Stjr{
122131377Stjr    struct inflate_state FAR *state;
12333904Ssteve
124311285Sdelphij    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
125131377Stjr    state = (struct inflate_state FAR *)strm->state;
126131377Stjr    strm->total_in = strm->total_out = state->total = 0;
127131377Stjr    strm->msg = Z_NULL;
128230837Sdelphij    if (state->wrap)        /* to support ill-conceived Java test suite */
129230837Sdelphij        strm->adler = state->wrap & 1;
130131377Stjr    state->mode = HEAD;
131131377Stjr    state->last = 0;
132131377Stjr    state->havedict = 0;
133157043Sdes    state->dmax = 32768U;
134157043Sdes    state->head = Z_NULL;
135131377Stjr    state->hold = 0;
136131377Stjr    state->bits = 0;
137131377Stjr    state->lencode = state->distcode = state->next = state->codes;
138205194Sdelphij    state->sane = 1;
139205194Sdelphij    state->back = -1;
140131377Stjr    Tracev((stderr, "inflate: reset\n"));
141131377Stjr    return Z_OK;
142131377Stjr}
14333904Ssteve
144230837Sdelphijint ZEXPORT inflateReset(strm)
145230837Sdelphijz_streamp strm;
146230837Sdelphij{
147230837Sdelphij    struct inflate_state FAR *state;
148230837Sdelphij
149311285Sdelphij    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
150230837Sdelphij    state = (struct inflate_state FAR *)strm->state;
151230837Sdelphij    state->wsize = 0;
152230837Sdelphij    state->whave = 0;
153230837Sdelphij    state->wnext = 0;
154230837Sdelphij    return inflateResetKeep(strm);
155230837Sdelphij}
156230837Sdelphij
157205194Sdelphijint ZEXPORT inflateReset2(strm, windowBits)
158157043Sdesz_streamp strm;
159205194Sdelphijint windowBits;
160157043Sdes{
161205194Sdelphij    int wrap;
162157043Sdes    struct inflate_state FAR *state;
163157043Sdes
164205194Sdelphij    /* get the state */
165311285Sdelphij    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
166157043Sdes    state = (struct inflate_state FAR *)strm->state;
167205194Sdelphij
168205194Sdelphij    /* extract wrap request from windowBits parameter */
169205194Sdelphij    if (windowBits < 0) {
170205194Sdelphij        wrap = 0;
171205194Sdelphij        windowBits = -windowBits;
172205194Sdelphij    }
173205194Sdelphij    else {
174311285Sdelphij        wrap = (windowBits >> 4) + 5;
175205194Sdelphij#ifdef GUNZIP
176205194Sdelphij        if (windowBits < 48)
177205194Sdelphij            windowBits &= 15;
178205194Sdelphij#endif
179205194Sdelphij    }
180205194Sdelphij
181205194Sdelphij    /* set number of window bits, free window if different */
182205194Sdelphij    if (windowBits && (windowBits < 8 || windowBits > 15))
183205194Sdelphij        return Z_STREAM_ERROR;
184205194Sdelphij    if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
185205194Sdelphij        ZFREE(strm, state->window);
186205194Sdelphij        state->window = Z_NULL;
187205194Sdelphij    }
188205194Sdelphij
189205194Sdelphij    /* update state and reset the rest of it */
190205194Sdelphij    state->wrap = wrap;
191205194Sdelphij    state->wbits = (unsigned)windowBits;
192205194Sdelphij    return inflateReset(strm);
193157043Sdes}
194157043Sdes
195131377Stjrint ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
196131377Stjrz_streamp strm;
197131377Stjrint windowBits;
198131377Stjrconst char *version;
199131377Stjrint stream_size;
200131377Stjr{
201205194Sdelphij    int ret;
202131377Stjr    struct inflate_state FAR *state;
20317651Speter
204131377Stjr    if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
205131377Stjr        stream_size != (int)(sizeof(z_stream)))
206131377Stjr        return Z_VERSION_ERROR;
207131377Stjr    if (strm == Z_NULL) return Z_STREAM_ERROR;
208131377Stjr    strm->msg = Z_NULL;                 /* in case we return an error */
209131377Stjr    if (strm->zalloc == (alloc_func)0) {
210230837Sdelphij#ifdef Z_SOLO
211230837Sdelphij        return Z_STREAM_ERROR;
212230837Sdelphij#else
213131377Stjr        strm->zalloc = zcalloc;
214131377Stjr        strm->opaque = (voidpf)0;
215230837Sdelphij#endif
216131377Stjr    }
217230837Sdelphij    if (strm->zfree == (free_func)0)
218230837Sdelphij#ifdef Z_SOLO
219230837Sdelphij        return Z_STREAM_ERROR;
220230837Sdelphij#else
221230837Sdelphij        strm->zfree = zcfree;
222230837Sdelphij#endif
223131377Stjr    state = (struct inflate_state FAR *)
224131377Stjr            ZALLOC(strm, 1, sizeof(struct inflate_state));
225131377Stjr    if (state == Z_NULL) return Z_MEM_ERROR;
226131377Stjr    Tracev((stderr, "inflate: allocated\n"));
227157043Sdes    strm->state = (struct internal_state FAR *)state;
228311285Sdelphij    state->strm = strm;
229205194Sdelphij    state->window = Z_NULL;
230311285Sdelphij    state->mode = HEAD;     /* to pass state test in inflateReset2() */
231205194Sdelphij    ret = inflateReset2(strm, windowBits);
232205194Sdelphij    if (ret != Z_OK) {
233131377Stjr        ZFREE(strm, state);
234131377Stjr        strm->state = Z_NULL;
235131377Stjr    }
236205194Sdelphij    return ret;
237131377Stjr}
23817651Speter
239131377Stjrint ZEXPORT inflateInit_(strm, version, stream_size)
240131377Stjrz_streamp strm;
241131377Stjrconst char *version;
242131377Stjrint stream_size;
24317651Speter{
244131377Stjr    return inflateInit2_(strm, DEF_WBITS, version, stream_size);
24517651Speter}
24617651Speter
247205194Sdelphijint ZEXPORT inflatePrime(strm, bits, value)
248205194Sdelphijz_streamp strm;
249205194Sdelphijint bits;
250205194Sdelphijint value;
251205194Sdelphij{
252205194Sdelphij    struct inflate_state FAR *state;
253205194Sdelphij
254311285Sdelphij    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
255205194Sdelphij    state = (struct inflate_state FAR *)strm->state;
256205194Sdelphij    if (bits < 0) {
257205194Sdelphij        state->hold = 0;
258205194Sdelphij        state->bits = 0;
259205194Sdelphij        return Z_OK;
260205194Sdelphij    }
261311285Sdelphij    if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
262205194Sdelphij    value &= (1L << bits) - 1;
263311285Sdelphij    state->hold += (unsigned)value << state->bits;
264311285Sdelphij    state->bits += (uInt)bits;
265205194Sdelphij    return Z_OK;
266205194Sdelphij}
267205194Sdelphij
268131377Stjr/*
269131377Stjr   Return state with length and distance decoding tables and index sizes set to
270131377Stjr   fixed code decoding.  Normally this returns fixed tables from inffixed.h.
271131377Stjr   If BUILDFIXED is defined, then instead this routine builds the tables the
272131377Stjr   first time it's called, and returns those tables the first time and
273131377Stjr   thereafter.  This reduces the size of the code by about 2K bytes, in
274131377Stjr   exchange for a little execution time.  However, BUILDFIXED should not be
275131377Stjr   used for threaded applications, since the rewriting of the tables and virgin
276131377Stjr   may not be thread-safe.
277131377Stjr */
278131377Stjrlocal void fixedtables(state)
279131377Stjrstruct inflate_state FAR *state;
280131377Stjr{
281131377Stjr#ifdef BUILDFIXED
282131377Stjr    static int virgin = 1;
283131377Stjr    static code *lenfix, *distfix;
284131377Stjr    static code fixed[544];
28517651Speter
286131377Stjr    /* build fixed huffman tables if first call (may not be thread safe) */
287131377Stjr    if (virgin) {
288131377Stjr        unsigned sym, bits;
289131377Stjr        static code *next;
290131377Stjr
291131377Stjr        /* literal/length table */
292131377Stjr        sym = 0;
293131377Stjr        while (sym < 144) state->lens[sym++] = 8;
294131377Stjr        while (sym < 256) state->lens[sym++] = 9;
295131377Stjr        while (sym < 280) state->lens[sym++] = 7;
296131377Stjr        while (sym < 288) state->lens[sym++] = 8;
297131377Stjr        next = fixed;
298131377Stjr        lenfix = next;
299131377Stjr        bits = 9;
300131377Stjr        inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
301131377Stjr
302131377Stjr        /* distance table */
303131377Stjr        sym = 0;
304131377Stjr        while (sym < 32) state->lens[sym++] = 5;
305131377Stjr        distfix = next;
306131377Stjr        bits = 5;
307131377Stjr        inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
308131377Stjr
309131377Stjr        /* do this just once */
310131377Stjr        virgin = 0;
311131377Stjr    }
312131377Stjr#else /* !BUILDFIXED */
313131377Stjr#   include "inffixed.h"
314131377Stjr#endif /* BUILDFIXED */
315131377Stjr    state->lencode = lenfix;
316131377Stjr    state->lenbits = 9;
317131377Stjr    state->distcode = distfix;
318131377Stjr    state->distbits = 5;
31917651Speter}
32017651Speter
321131377Stjr#ifdef MAKEFIXED
322131377Stjr#include <stdio.h>
32317651Speter
324131377Stjr/*
325131377Stjr   Write out the inffixed.h that is #include'd above.  Defining MAKEFIXED also
326131377Stjr   defines BUILDFIXED, so the tables are built on the fly.  makefixed() writes
327131377Stjr   those tables to stdout, which would be piped to inffixed.h.  A small program
328131377Stjr   can simply call makefixed to do this:
32917651Speter
330131377Stjr    void makefixed(void);
33117651Speter
332131377Stjr    int main(void)
333131377Stjr    {
334131377Stjr        makefixed();
335131377Stjr        return 0;
336131377Stjr    }
33717651Speter
338131377Stjr   Then that can be linked with zlib built with MAKEFIXED defined and run:
33917651Speter
340131377Stjr    a.out > inffixed.h
341131377Stjr */
342131377Stjrvoid makefixed()
343131377Stjr{
344131377Stjr    unsigned low, size;
345131377Stjr    struct inflate_state state;
34617651Speter
347131377Stjr    fixedtables(&state);
348131377Stjr    puts("    /* inffixed.h -- table for decoding fixed codes");
349131377Stjr    puts("     * Generated automatically by makefixed().");
350131377Stjr    puts("     */");
351131377Stjr    puts("");
352131377Stjr    puts("    /* WARNING: this file should *not* be used by applications.");
353131377Stjr    puts("       It is part of the implementation of this library and is");
354131377Stjr    puts("       subject to change. Applications should only use zlib.h.");
355131377Stjr    puts("     */");
356131377Stjr    puts("");
357131377Stjr    size = 1U << 9;
358131377Stjr    printf("    static const code lenfix[%u] = {", size);
359131377Stjr    low = 0;
360131377Stjr    for (;;) {
361131377Stjr        if ((low % 7) == 0) printf("\n        ");
362230837Sdelphij        printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
363230837Sdelphij               state.lencode[low].bits, state.lencode[low].val);
364131377Stjr        if (++low == size) break;
365131377Stjr        putchar(',');
366131377Stjr    }
367131377Stjr    puts("\n    };");
368131377Stjr    size = 1U << 5;
369131377Stjr    printf("\n    static const code distfix[%u] = {", size);
370131377Stjr    low = 0;
371131377Stjr    for (;;) {
372131377Stjr        if ((low % 6) == 0) printf("\n        ");
373131377Stjr        printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
374131377Stjr               state.distcode[low].val);
375131377Stjr        if (++low == size) break;
376131377Stjr        putchar(',');
377131377Stjr    }
378131377Stjr    puts("\n    };");
37917651Speter}
380131377Stjr#endif /* MAKEFIXED */
38117651Speter
382131377Stjr/*
383131377Stjr   Update the window with the last wsize (normally 32K) bytes written before
384131377Stjr   returning.  If window does not exist yet, create it.  This is only called
385131377Stjr   when a window is already in use, or when output has been written during this
386131377Stjr   inflate call, but the end of the deflate stream has not been reached yet.
387131377Stjr   It is also called to create a window for dictionary data when a dictionary
388131377Stjr   is loaded.
38917651Speter
390131377Stjr   Providing output buffers larger than 32K to inflate() should provide a speed
391131377Stjr   advantage, since only the last 32K of output is copied to the sliding window
392131377Stjr   upon return from inflate(), and since all distances after the first 32K of
393131377Stjr   output will fall in the output data, making match copies simpler and faster.
394131377Stjr   The advantage may be dependent on the size of the processor's data caches.
395131377Stjr */
396250224Sdelphijlocal int updatewindow(strm, end, copy)
397131377Stjrz_streamp strm;
398250224Sdelphijconst Bytef *end;
399250224Sdelphijunsigned copy;
40017651Speter{
401131377Stjr    struct inflate_state FAR *state;
402250224Sdelphij    unsigned dist;
403131377Stjr
404131377Stjr    state = (struct inflate_state FAR *)strm->state;
405131377Stjr
406131377Stjr    /* if it hasn't been done already, allocate space for the window */
407131377Stjr    if (state->window == Z_NULL) {
408131377Stjr        state->window = (unsigned char FAR *)
409131377Stjr                        ZALLOC(strm, 1U << state->wbits,
410131377Stjr                               sizeof(unsigned char));
411131377Stjr        if (state->window == Z_NULL) return 1;
412131377Stjr    }
413131377Stjr
414131377Stjr    /* if window not in use yet, initialize */
415131377Stjr    if (state->wsize == 0) {
416131377Stjr        state->wsize = 1U << state->wbits;
417205194Sdelphij        state->wnext = 0;
418131377Stjr        state->whave = 0;
419131377Stjr    }
420131377Stjr
421131377Stjr    /* copy state->wsize or less output bytes into the circular window */
422131377Stjr    if (copy >= state->wsize) {
423250224Sdelphij        zmemcpy(state->window, end - state->wsize, state->wsize);
424205194Sdelphij        state->wnext = 0;
425131377Stjr        state->whave = state->wsize;
426131377Stjr    }
427131377Stjr    else {
428205194Sdelphij        dist = state->wsize - state->wnext;
429131377Stjr        if (dist > copy) dist = copy;
430250224Sdelphij        zmemcpy(state->window + state->wnext, end - copy, dist);
431131377Stjr        copy -= dist;
432131377Stjr        if (copy) {
433250224Sdelphij            zmemcpy(state->window, end - copy, copy);
434205194Sdelphij            state->wnext = copy;
435131377Stjr            state->whave = state->wsize;
436131377Stjr        }
437131377Stjr        else {
438205194Sdelphij            state->wnext += dist;
439205194Sdelphij            if (state->wnext == state->wsize) state->wnext = 0;
440131377Stjr            if (state->whave < state->wsize) state->whave += dist;
441131377Stjr        }
442131377Stjr    }
443131377Stjr    return 0;
44417651Speter}
44517651Speter
446131377Stjr/* Macros for inflate(): */
44717651Speter
448131377Stjr/* check function to use adler32() for zlib or crc32() for gzip */
449131377Stjr#ifdef GUNZIP
450131377Stjr#  define UPDATE(check, buf, len) \
451131377Stjr    (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
452131377Stjr#else
453131377Stjr#  define UPDATE(check, buf, len) adler32(check, buf, len)
454131377Stjr#endif
45517651Speter
456131377Stjr/* check macros for header crc */
457131377Stjr#ifdef GUNZIP
458131377Stjr#  define CRC2(check, word) \
459131377Stjr    do { \
460131377Stjr        hbuf[0] = (unsigned char)(word); \
461131377Stjr        hbuf[1] = (unsigned char)((word) >> 8); \
462131377Stjr        check = crc32(check, hbuf, 2); \
463131377Stjr    } while (0)
46417651Speter
465131377Stjr#  define CRC4(check, word) \
466131377Stjr    do { \
467131377Stjr        hbuf[0] = (unsigned char)(word); \
468131377Stjr        hbuf[1] = (unsigned char)((word) >> 8); \
469131377Stjr        hbuf[2] = (unsigned char)((word) >> 16); \
470131377Stjr        hbuf[3] = (unsigned char)((word) >> 24); \
471131377Stjr        check = crc32(check, hbuf, 4); \
472131377Stjr    } while (0)
473131377Stjr#endif
474131377Stjr
475131377Stjr/* Load registers with state in inflate() for speed */
476131377Stjr#define LOAD() \
477131377Stjr    do { \
478131377Stjr        put = strm->next_out; \
479131377Stjr        left = strm->avail_out; \
480131377Stjr        next = strm->next_in; \
481131377Stjr        have = strm->avail_in; \
482131377Stjr        hold = state->hold; \
483131377Stjr        bits = state->bits; \
484131377Stjr    } while (0)
485131377Stjr
486131377Stjr/* Restore state from registers in inflate() */
487131377Stjr#define RESTORE() \
488131377Stjr    do { \
489131377Stjr        strm->next_out = put; \
490131377Stjr        strm->avail_out = left; \
491131377Stjr        strm->next_in = next; \
492131377Stjr        strm->avail_in = have; \
493131377Stjr        state->hold = hold; \
494131377Stjr        state->bits = bits; \
495131377Stjr    } while (0)
496131377Stjr
497131377Stjr/* Clear the input bit accumulator */
498131377Stjr#define INITBITS() \
499131377Stjr    do { \
500131377Stjr        hold = 0; \
501131377Stjr        bits = 0; \
502131377Stjr    } while (0)
503131377Stjr
504131377Stjr/* Get a byte of input into the bit accumulator, or return from inflate()
505131377Stjr   if there is no input available. */
506131377Stjr#define PULLBYTE() \
507131377Stjr    do { \
508131377Stjr        if (have == 0) goto inf_leave; \
509131377Stjr        have--; \
510131377Stjr        hold += (unsigned long)(*next++) << bits; \
511131377Stjr        bits += 8; \
512131377Stjr    } while (0)
513131377Stjr
514131377Stjr/* Assure that there are at least n bits in the bit accumulator.  If there is
515131377Stjr   not enough available input to do that, then return from inflate(). */
516131377Stjr#define NEEDBITS(n) \
517131377Stjr    do { \
518131377Stjr        while (bits < (unsigned)(n)) \
519131377Stjr            PULLBYTE(); \
520131377Stjr    } while (0)
521131377Stjr
522131377Stjr/* Return the low n bits of the bit accumulator (n < 16) */
523131377Stjr#define BITS(n) \
524131377Stjr    ((unsigned)hold & ((1U << (n)) - 1))
525131377Stjr
526131377Stjr/* Remove n bits from the bit accumulator */
527131377Stjr#define DROPBITS(n) \
528131377Stjr    do { \
529131377Stjr        hold >>= (n); \
530131377Stjr        bits -= (unsigned)(n); \
531131377Stjr    } while (0)
532131377Stjr
533131377Stjr/* Remove zero to seven bits as needed to go to a byte boundary */
534131377Stjr#define BYTEBITS() \
535131377Stjr    do { \
536131377Stjr        hold >>= bits & 7; \
537131377Stjr        bits -= bits & 7; \
538131377Stjr    } while (0)
539131377Stjr
540131377Stjr/*
541131377Stjr   inflate() uses a state machine to process as much input data and generate as
542131377Stjr   much output data as possible before returning.  The state machine is
543131377Stjr   structured roughly as follows:
544131377Stjr
545131377Stjr    for (;;) switch (state) {
546131377Stjr    ...
547131377Stjr    case STATEn:
548131377Stjr        if (not enough input data or output space to make progress)
549131377Stjr            return;
550131377Stjr        ... make progress ...
551131377Stjr        state = STATEm;
55217651Speter        break;
553131377Stjr    ...
554131377Stjr    }
55517651Speter
556131377Stjr   so when inflate() is called again, the same case is attempted again, and
557131377Stjr   if the appropriate resources are provided, the machine proceeds to the
558131377Stjr   next state.  The NEEDBITS() macro is usually the way the state evaluates
559131377Stjr   whether it can proceed or should return.  NEEDBITS() does the return if
560131377Stjr   the requested bits are not available.  The typical use of the BITS macros
561131377Stjr   is:
562131377Stjr
563131377Stjr        NEEDBITS(n);
564131377Stjr        ... do something with BITS(n) ...
565131377Stjr        DROPBITS(n);
566131377Stjr
567131377Stjr   where NEEDBITS(n) either returns from inflate() if there isn't enough
568131377Stjr   input left to load n bits into the accumulator, or it continues.  BITS(n)
569131377Stjr   gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
570131377Stjr   the low n bits off the accumulator.  INITBITS() clears the accumulator
571131377Stjr   and sets the number of available bits to zero.  BYTEBITS() discards just
572131377Stjr   enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
573131377Stjr   and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
574131377Stjr
575131377Stjr   NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
576131377Stjr   if there is no input available.  The decoding of variable length codes uses
577131377Stjr   PULLBYTE() directly in order to pull just enough bytes to decode the next
578131377Stjr   code, and no more.
579131377Stjr
580131377Stjr   Some states loop until they get enough input, making sure that enough
581131377Stjr   state information is maintained to continue the loop where it left off
582131377Stjr   if NEEDBITS() returns in the loop.  For example, want, need, and keep
583131377Stjr   would all have to actually be part of the saved state in case NEEDBITS()
584131377Stjr   returns:
585131377Stjr
586131377Stjr    case STATEw:
587131377Stjr        while (want < need) {
588131377Stjr            NEEDBITS(n);
589131377Stjr            keep[want++] = BITS(n);
590131377Stjr            DROPBITS(n);
591131377Stjr        }
592131377Stjr        state = STATEx;
593131377Stjr    case STATEx:
594131377Stjr
595131377Stjr   As shown above, if the next state is also the next case, then the break
596131377Stjr   is omitted.
597131377Stjr
598131377Stjr   A state may also return if there is not enough output space available to
599131377Stjr   complete that state.  Those states are copying stored data, writing a
600131377Stjr   literal byte, and copying a matching string.
601131377Stjr
602131377Stjr   When returning, a "goto inf_leave" is used to update the total counters,
603131377Stjr   update the check value, and determine whether any progress has been made
604131377Stjr   during that inflate() call in order to return the proper return code.
605131377Stjr   Progress is defined as a change in either strm->avail_in or strm->avail_out.
606131377Stjr   When there is a window, goto inf_leave will update the window with the last
607131377Stjr   output written.  If a goto inf_leave occurs in the middle of decompression
608131377Stjr   and there is no window currently, goto inf_leave will create one and copy
609131377Stjr   output to the window for the next call of inflate().
610131377Stjr
611131377Stjr   In this implementation, the flush parameter of inflate() only affects the
612131377Stjr   return code (per zlib.h).  inflate() always writes as much as possible to
613131377Stjr   strm->next_out, given the space available and the provided input--the effect
614131377Stjr   documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
615131377Stjr   the allocation of and copying into a sliding window until necessary, which
616131377Stjr   provides the effect documented in zlib.h for Z_FINISH when the entire input
617131377Stjr   stream available.  So the only thing the flush parameter actually does is:
618131377Stjr   when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
619131377Stjr   will return Z_BUF_ERROR if it has not reached the end of the stream.
620131377Stjr */
621131377Stjr
622131377Stjrint ZEXPORT inflate(strm, flush)
623131377Stjrz_streamp strm;
624131377Stjrint flush;
625131377Stjr{
626131377Stjr    struct inflate_state FAR *state;
627250224Sdelphij    z_const unsigned char FAR *next;    /* next input */
628131377Stjr    unsigned char FAR *put;     /* next output */
629131377Stjr    unsigned have, left;        /* available input and output */
630131377Stjr    unsigned long hold;         /* bit buffer */
631131377Stjr    unsigned bits;              /* bits in bit buffer */
632131377Stjr    unsigned in, out;           /* save starting available input and output */
633131377Stjr    unsigned copy;              /* number of stored or match bytes to copy */
634131377Stjr    unsigned char FAR *from;    /* where to copy match bytes from */
635205194Sdelphij    code here;                  /* current decoding table entry */
636131377Stjr    code last;                  /* parent table entry */
637131377Stjr    unsigned len;               /* length to copy for repeats, bits to drop */
638131377Stjr    int ret;                    /* return code */
639131377Stjr#ifdef GUNZIP
640131377Stjr    unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
64133904Ssteve#endif
642131377Stjr    static const unsigned short order[19] = /* permutation of code lengths */
643131377Stjr        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
644131377Stjr
645311285Sdelphij    if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
646131377Stjr        (strm->next_in == Z_NULL && strm->avail_in != 0))
647131377Stjr        return Z_STREAM_ERROR;
648131377Stjr
649131377Stjr    state = (struct inflate_state FAR *)strm->state;
650131377Stjr    if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
651131377Stjr    LOAD();
652131377Stjr    in = have;
653131377Stjr    out = left;
654131377Stjr    ret = Z_OK;
655131377Stjr    for (;;)
656131377Stjr        switch (state->mode) {
657131377Stjr        case HEAD:
658131377Stjr            if (state->wrap == 0) {
659131377Stjr                state->mode = TYPEDO;
660131377Stjr                break;
661131377Stjr            }
662131377Stjr            NEEDBITS(16);
663131377Stjr#ifdef GUNZIP
664131377Stjr            if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
665311285Sdelphij                if (state->wbits == 0)
666311285Sdelphij                    state->wbits = 15;
667131377Stjr                state->check = crc32(0L, Z_NULL, 0);
668131377Stjr                CRC2(state->check, hold);
669131377Stjr                INITBITS();
670131377Stjr                state->mode = FLAGS;
671131377Stjr                break;
672131377Stjr            }
673131377Stjr            state->flags = 0;           /* expect zlib header */
674157043Sdes            if (state->head != Z_NULL)
675157043Sdes                state->head->done = -1;
676131377Stjr            if (!(state->wrap & 1) ||   /* check if zlib header allowed */
677131377Stjr#else
678131377Stjr            if (
679131377Stjr#endif
680131377Stjr                ((BITS(8) << 8) + (hold >> 8)) % 31) {
681131377Stjr                strm->msg = (char *)"incorrect header check";
682131377Stjr                state->mode = BAD;
683131377Stjr                break;
684131377Stjr            }
685131377Stjr            if (BITS(4) != Z_DEFLATED) {
686131377Stjr                strm->msg = (char *)"unknown compression method";
687131377Stjr                state->mode = BAD;
688131377Stjr                break;
689131377Stjr            }
690131377Stjr            DROPBITS(4);
691157043Sdes            len = BITS(4) + 8;
692205194Sdelphij            if (state->wbits == 0)
693205194Sdelphij                state->wbits = len;
694311285Sdelphij            if (len > 15 || len > state->wbits) {
695131377Stjr                strm->msg = (char *)"invalid window size";
696131377Stjr                state->mode = BAD;
697131377Stjr                break;
698131377Stjr            }
699157043Sdes            state->dmax = 1U << len;
700131377Stjr            Tracev((stderr, "inflate:   zlib header ok\n"));
701131377Stjr            strm->adler = state->check = adler32(0L, Z_NULL, 0);
702131377Stjr            state->mode = hold & 0x200 ? DICTID : TYPE;
703131377Stjr            INITBITS();
704131377Stjr            break;
705131377Stjr#ifdef GUNZIP
706131377Stjr        case FLAGS:
707131377Stjr            NEEDBITS(16);
708131377Stjr            state->flags = (int)(hold);
709131377Stjr            if ((state->flags & 0xff) != Z_DEFLATED) {
710131377Stjr                strm->msg = (char *)"unknown compression method";
711131377Stjr                state->mode = BAD;
712131377Stjr                break;
713131377Stjr            }
714131377Stjr            if (state->flags & 0xe000) {
715131377Stjr                strm->msg = (char *)"unknown header flags set";
716131377Stjr                state->mode = BAD;
717131377Stjr                break;
718131377Stjr            }
719157043Sdes            if (state->head != Z_NULL)
720157043Sdes                state->head->text = (int)((hold >> 8) & 1);
721311285Sdelphij            if ((state->flags & 0x0200) && (state->wrap & 4))
722311285Sdelphij                CRC2(state->check, hold);
723131377Stjr            INITBITS();
724131377Stjr            state->mode = TIME;
725131377Stjr        case TIME:
726131377Stjr            NEEDBITS(32);
727157043Sdes            if (state->head != Z_NULL)
728157043Sdes                state->head->time = hold;
729311285Sdelphij            if ((state->flags & 0x0200) && (state->wrap & 4))
730311285Sdelphij                CRC4(state->check, hold);
731131377Stjr            INITBITS();
732131377Stjr            state->mode = OS;
733131377Stjr        case OS:
734131377Stjr            NEEDBITS(16);
735157043Sdes            if (state->head != Z_NULL) {
736157043Sdes                state->head->xflags = (int)(hold & 0xff);
737157043Sdes                state->head->os = (int)(hold >> 8);
738157043Sdes            }
739311285Sdelphij            if ((state->flags & 0x0200) && (state->wrap & 4))
740311285Sdelphij                CRC2(state->check, hold);
741131377Stjr            INITBITS();
742131377Stjr            state->mode = EXLEN;
743131377Stjr        case EXLEN:
744131377Stjr            if (state->flags & 0x0400) {
745131377Stjr                NEEDBITS(16);
746131377Stjr                state->length = (unsigned)(hold);
747157043Sdes                if (state->head != Z_NULL)
748157043Sdes                    state->head->extra_len = (unsigned)hold;
749311285Sdelphij                if ((state->flags & 0x0200) && (state->wrap & 4))
750311285Sdelphij                    CRC2(state->check, hold);
751131377Stjr                INITBITS();
752131377Stjr            }
753157043Sdes            else if (state->head != Z_NULL)
754157043Sdes                state->head->extra = Z_NULL;
755131377Stjr            state->mode = EXTRA;
756131377Stjr        case EXTRA:
757131377Stjr            if (state->flags & 0x0400) {
758131377Stjr                copy = state->length;
759131377Stjr                if (copy > have) copy = have;
760131377Stjr                if (copy) {
761157043Sdes                    if (state->head != Z_NULL &&
762157043Sdes                        state->head->extra != Z_NULL) {
763157043Sdes                        len = state->head->extra_len - state->length;
764157043Sdes                        zmemcpy(state->head->extra + len, next,
765157043Sdes                                len + copy > state->head->extra_max ?
766157043Sdes                                state->head->extra_max - len : copy);
767157043Sdes                    }
768311285Sdelphij                    if ((state->flags & 0x0200) && (state->wrap & 4))
769131377Stjr                        state->check = crc32(state->check, next, copy);
770131377Stjr                    have -= copy;
771131377Stjr                    next += copy;
772131377Stjr                    state->length -= copy;
773131377Stjr                }
774131377Stjr                if (state->length) goto inf_leave;
775131377Stjr            }
776157043Sdes            state->length = 0;
777131377Stjr            state->mode = NAME;
778131377Stjr        case NAME:
779131377Stjr            if (state->flags & 0x0800) {
780131377Stjr                if (have == 0) goto inf_leave;
781131377Stjr                copy = 0;
782131377Stjr                do {
783131377Stjr                    len = (unsigned)(next[copy++]);
784157043Sdes                    if (state->head != Z_NULL &&
785157043Sdes                            state->head->name != Z_NULL &&
786157043Sdes                            state->length < state->head->name_max)
787311285Sdelphij                        state->head->name[state->length++] = (Bytef)len;
788131377Stjr                } while (len && copy < have);
789311285Sdelphij                if ((state->flags & 0x0200) && (state->wrap & 4))
790131377Stjr                    state->check = crc32(state->check, next, copy);
791131377Stjr                have -= copy;
792131377Stjr                next += copy;
793131377Stjr                if (len) goto inf_leave;
794131377Stjr            }
795157043Sdes            else if (state->head != Z_NULL)
796157043Sdes                state->head->name = Z_NULL;
797157043Sdes            state->length = 0;
798131377Stjr            state->mode = COMMENT;
799131377Stjr        case COMMENT:
800131377Stjr            if (state->flags & 0x1000) {
801131377Stjr                if (have == 0) goto inf_leave;
802131377Stjr                copy = 0;
803131377Stjr                do {
804131377Stjr                    len = (unsigned)(next[copy++]);
805157043Sdes                    if (state->head != Z_NULL &&
806157043Sdes                            state->head->comment != Z_NULL &&
807157043Sdes                            state->length < state->head->comm_max)
808311285Sdelphij                        state->head->comment[state->length++] = (Bytef)len;
809131377Stjr                } while (len && copy < have);
810311285Sdelphij                if ((state->flags & 0x0200) && (state->wrap & 4))
811131377Stjr                    state->check = crc32(state->check, next, copy);
812131377Stjr                have -= copy;
813131377Stjr                next += copy;
814131377Stjr                if (len) goto inf_leave;
815131377Stjr            }
816157043Sdes            else if (state->head != Z_NULL)
817157043Sdes                state->head->comment = Z_NULL;
818131377Stjr            state->mode = HCRC;
819131377Stjr        case HCRC:
820131377Stjr            if (state->flags & 0x0200) {
821131377Stjr                NEEDBITS(16);
822311285Sdelphij                if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
823131377Stjr                    strm->msg = (char *)"header crc mismatch";
824131377Stjr                    state->mode = BAD;
825131377Stjr                    break;
826131377Stjr                }
827131377Stjr                INITBITS();
828131377Stjr            }
829157043Sdes            if (state->head != Z_NULL) {
830157043Sdes                state->head->hcrc = (int)((state->flags >> 9) & 1);
831157043Sdes                state->head->done = 1;
832157043Sdes            }
833131377Stjr            strm->adler = state->check = crc32(0L, Z_NULL, 0);
834131377Stjr            state->mode = TYPE;
835131377Stjr            break;
836131377Stjr#endif
837131377Stjr        case DICTID:
838131377Stjr            NEEDBITS(32);
839237248Sdelphij            strm->adler = state->check = ZSWAP32(hold);
840131377Stjr            INITBITS();
841131377Stjr            state->mode = DICT;
842131377Stjr        case DICT:
843131377Stjr            if (state->havedict == 0) {
844131377Stjr                RESTORE();
845131377Stjr                return Z_NEED_DICT;
846131377Stjr            }
847131377Stjr            strm->adler = state->check = adler32(0L, Z_NULL, 0);
848131377Stjr            state->mode = TYPE;
849131377Stjr        case TYPE:
850205194Sdelphij            if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
851131377Stjr        case TYPEDO:
852131377Stjr            if (state->last) {
853131377Stjr                BYTEBITS();
854131377Stjr                state->mode = CHECK;
855131377Stjr                break;
856131377Stjr            }
857131377Stjr            NEEDBITS(3);
858131377Stjr            state->last = BITS(1);
859131377Stjr            DROPBITS(1);
860131377Stjr            switch (BITS(2)) {
861131377Stjr            case 0:                             /* stored block */
862131377Stjr                Tracev((stderr, "inflate:     stored block%s\n",
863131377Stjr                        state->last ? " (last)" : ""));
864131377Stjr                state->mode = STORED;
865131377Stjr                break;
866131377Stjr            case 1:                             /* fixed block */
867131377Stjr                fixedtables(state);
868131377Stjr                Tracev((stderr, "inflate:     fixed codes block%s\n",
869131377Stjr                        state->last ? " (last)" : ""));
870205194Sdelphij                state->mode = LEN_;             /* decode codes */
871205194Sdelphij                if (flush == Z_TREES) {
872205194Sdelphij                    DROPBITS(2);
873205194Sdelphij                    goto inf_leave;
874205194Sdelphij                }
875131377Stjr                break;
876131377Stjr            case 2:                             /* dynamic block */
877131377Stjr                Tracev((stderr, "inflate:     dynamic codes block%s\n",
878131377Stjr                        state->last ? " (last)" : ""));
879131377Stjr                state->mode = TABLE;
880131377Stjr                break;
881131377Stjr            case 3:
882131377Stjr                strm->msg = (char *)"invalid block type";
883131377Stjr                state->mode = BAD;
884131377Stjr            }
885131377Stjr            DROPBITS(2);
886131377Stjr            break;
887131377Stjr        case STORED:
888131377Stjr            BYTEBITS();                         /* go to byte boundary */
889131377Stjr            NEEDBITS(32);
890131377Stjr            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
891131377Stjr                strm->msg = (char *)"invalid stored block lengths";
892131377Stjr                state->mode = BAD;
893131377Stjr                break;
894131377Stjr            }
895131377Stjr            state->length = (unsigned)hold & 0xffff;
896131377Stjr            Tracev((stderr, "inflate:       stored length %u\n",
897131377Stjr                    state->length));
898131377Stjr            INITBITS();
899205194Sdelphij            state->mode = COPY_;
900205194Sdelphij            if (flush == Z_TREES) goto inf_leave;
901205194Sdelphij        case COPY_:
902131377Stjr            state->mode = COPY;
903131377Stjr        case COPY:
904131377Stjr            copy = state->length;
905131377Stjr            if (copy) {
906131377Stjr                if (copy > have) copy = have;
907131377Stjr                if (copy > left) copy = left;
908131377Stjr                if (copy == 0) goto inf_leave;
909131377Stjr                zmemcpy(put, next, copy);
910131377Stjr                have -= copy;
911131377Stjr                next += copy;
912131377Stjr                left -= copy;
913131377Stjr                put += copy;
914131377Stjr                state->length -= copy;
915131377Stjr                break;
916131377Stjr            }
917131377Stjr            Tracev((stderr, "inflate:       stored end\n"));
918131377Stjr            state->mode = TYPE;
919131377Stjr            break;
920131377Stjr        case TABLE:
921131377Stjr            NEEDBITS(14);
922131377Stjr            state->nlen = BITS(5) + 257;
923131377Stjr            DROPBITS(5);
924131377Stjr            state->ndist = BITS(5) + 1;
925131377Stjr            DROPBITS(5);
926131377Stjr            state->ncode = BITS(4) + 4;
927131377Stjr            DROPBITS(4);
928131377Stjr#ifndef PKZIP_BUG_WORKAROUND
929131377Stjr            if (state->nlen > 286 || state->ndist > 30) {
930131377Stjr                strm->msg = (char *)"too many length or distance symbols";
931131377Stjr                state->mode = BAD;
932131377Stjr                break;
933131377Stjr            }
934131377Stjr#endif
935131377Stjr            Tracev((stderr, "inflate:       table sizes ok\n"));
936131377Stjr            state->have = 0;
937131377Stjr            state->mode = LENLENS;
938131377Stjr        case LENLENS:
939131377Stjr            while (state->have < state->ncode) {
940131377Stjr                NEEDBITS(3);
941131377Stjr                state->lens[order[state->have++]] = (unsigned short)BITS(3);
942131377Stjr                DROPBITS(3);
943131377Stjr            }
944131377Stjr            while (state->have < 19)
945131377Stjr                state->lens[order[state->have++]] = 0;
946131377Stjr            state->next = state->codes;
947250224Sdelphij            state->lencode = (const code FAR *)(state->next);
948131377Stjr            state->lenbits = 7;
949131377Stjr            ret = inflate_table(CODES, state->lens, 19, &(state->next),
950131377Stjr                                &(state->lenbits), state->work);
951131377Stjr            if (ret) {
952131377Stjr                strm->msg = (char *)"invalid code lengths set";
953131377Stjr                state->mode = BAD;
954131377Stjr                break;
955131377Stjr            }
956131377Stjr            Tracev((stderr, "inflate:       code lengths ok\n"));
957131377Stjr            state->have = 0;
958131377Stjr            state->mode = CODELENS;
959131377Stjr        case CODELENS:
960131377Stjr            while (state->have < state->nlen + state->ndist) {
961131377Stjr                for (;;) {
962205194Sdelphij                    here = state->lencode[BITS(state->lenbits)];
963205194Sdelphij                    if ((unsigned)(here.bits) <= bits) break;
964131377Stjr                    PULLBYTE();
965131377Stjr                }
966205194Sdelphij                if (here.val < 16) {
967205194Sdelphij                    DROPBITS(here.bits);
968205194Sdelphij                    state->lens[state->have++] = here.val;
969131377Stjr                }
970131377Stjr                else {
971205194Sdelphij                    if (here.val == 16) {
972205194Sdelphij                        NEEDBITS(here.bits + 2);
973205194Sdelphij                        DROPBITS(here.bits);
974131377Stjr                        if (state->have == 0) {
975131377Stjr                            strm->msg = (char *)"invalid bit length repeat";
976131377Stjr                            state->mode = BAD;
977131377Stjr                            break;
978131377Stjr                        }
979131377Stjr                        len = state->lens[state->have - 1];
980131377Stjr                        copy = 3 + BITS(2);
981131377Stjr                        DROPBITS(2);
982131377Stjr                    }
983205194Sdelphij                    else if (here.val == 17) {
984205194Sdelphij                        NEEDBITS(here.bits + 3);
985205194Sdelphij                        DROPBITS(here.bits);
986131377Stjr                        len = 0;
987131377Stjr                        copy = 3 + BITS(3);
988131377Stjr                        DROPBITS(3);
989131377Stjr                    }
990131377Stjr                    else {
991205194Sdelphij                        NEEDBITS(here.bits + 7);
992205194Sdelphij                        DROPBITS(here.bits);
993131377Stjr                        len = 0;
994131377Stjr                        copy = 11 + BITS(7);
995131377Stjr                        DROPBITS(7);
996131377Stjr                    }
997131377Stjr                    if (state->have + copy > state->nlen + state->ndist) {
998131377Stjr                        strm->msg = (char *)"invalid bit length repeat";
999131377Stjr                        state->mode = BAD;
1000131377Stjr                        break;
1001131377Stjr                    }
1002131377Stjr                    while (copy--)
1003131377Stjr                        state->lens[state->have++] = (unsigned short)len;
1004131377Stjr                }
1005131377Stjr            }
1006131377Stjr
1007145474Skientzle            /* handle error breaks in while */
1008145474Skientzle            if (state->mode == BAD) break;
1009145474Skientzle
1010205194Sdelphij            /* check for end-of-block code (better have one) */
1011205194Sdelphij            if (state->lens[256] == 0) {
1012205194Sdelphij                strm->msg = (char *)"invalid code -- missing end-of-block";
1013205194Sdelphij                state->mode = BAD;
1014205194Sdelphij                break;
1015205194Sdelphij            }
1016205194Sdelphij
1017205194Sdelphij            /* build code tables -- note: do not change the lenbits or distbits
1018205194Sdelphij               values here (9 and 6) without reading the comments in inftrees.h
1019205194Sdelphij               concerning the ENOUGH constants, which depend on those values */
1020131377Stjr            state->next = state->codes;
1021250224Sdelphij            state->lencode = (const code FAR *)(state->next);
1022131377Stjr            state->lenbits = 9;
1023131377Stjr            ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1024131377Stjr                                &(state->lenbits), state->work);
1025131377Stjr            if (ret) {
1026131377Stjr                strm->msg = (char *)"invalid literal/lengths set";
1027131377Stjr                state->mode = BAD;
1028131377Stjr                break;
1029131377Stjr            }
1030250224Sdelphij            state->distcode = (const code FAR *)(state->next);
1031131377Stjr            state->distbits = 6;
1032131377Stjr            ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1033131377Stjr                            &(state->next), &(state->distbits), state->work);
1034131377Stjr            if (ret) {
1035131377Stjr                strm->msg = (char *)"invalid distances set";
1036131377Stjr                state->mode = BAD;
1037131377Stjr                break;
1038131377Stjr            }
1039131377Stjr            Tracev((stderr, "inflate:       codes ok\n"));
1040205194Sdelphij            state->mode = LEN_;
1041205194Sdelphij            if (flush == Z_TREES) goto inf_leave;
1042205194Sdelphij        case LEN_:
1043131377Stjr            state->mode = LEN;
1044131377Stjr        case LEN:
1045131377Stjr            if (have >= 6 && left >= 258) {
1046131377Stjr                RESTORE();
1047131377Stjr                inflate_fast(strm, out);
1048131377Stjr                LOAD();
1049205194Sdelphij                if (state->mode == TYPE)
1050205194Sdelphij                    state->back = -1;
1051131377Stjr                break;
1052131377Stjr            }
1053205194Sdelphij            state->back = 0;
1054131377Stjr            for (;;) {
1055205194Sdelphij                here = state->lencode[BITS(state->lenbits)];
1056205194Sdelphij                if ((unsigned)(here.bits) <= bits) break;
1057131377Stjr                PULLBYTE();
1058131377Stjr            }
1059205194Sdelphij            if (here.op && (here.op & 0xf0) == 0) {
1060205194Sdelphij                last = here;
1061131377Stjr                for (;;) {
1062205194Sdelphij                    here = state->lencode[last.val +
1063131377Stjr                            (BITS(last.bits + last.op) >> last.bits)];
1064205194Sdelphij                    if ((unsigned)(last.bits + here.bits) <= bits) break;
1065131377Stjr                    PULLBYTE();
1066131377Stjr                }
1067131377Stjr                DROPBITS(last.bits);
1068205194Sdelphij                state->back += last.bits;
1069131377Stjr            }
1070205194Sdelphij            DROPBITS(here.bits);
1071205194Sdelphij            state->back += here.bits;
1072205194Sdelphij            state->length = (unsigned)here.val;
1073205194Sdelphij            if ((int)(here.op) == 0) {
1074205194Sdelphij                Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1075131377Stjr                        "inflate:         literal '%c'\n" :
1076205194Sdelphij                        "inflate:         literal 0x%02x\n", here.val));
1077131377Stjr                state->mode = LIT;
1078131377Stjr                break;
1079131377Stjr            }
1080205194Sdelphij            if (here.op & 32) {
1081131377Stjr                Tracevv((stderr, "inflate:         end of block\n"));
1082205194Sdelphij                state->back = -1;
1083131377Stjr                state->mode = TYPE;
1084131377Stjr                break;
1085131377Stjr            }
1086205194Sdelphij            if (here.op & 64) {
1087131377Stjr                strm->msg = (char *)"invalid literal/length code";
1088131377Stjr                state->mode = BAD;
1089131377Stjr                break;
1090131377Stjr            }
1091205194Sdelphij            state->extra = (unsigned)(here.op) & 15;
1092131377Stjr            state->mode = LENEXT;
1093131377Stjr        case LENEXT:
1094131377Stjr            if (state->extra) {
1095131377Stjr                NEEDBITS(state->extra);
1096131377Stjr                state->length += BITS(state->extra);
1097131377Stjr                DROPBITS(state->extra);
1098205194Sdelphij                state->back += state->extra;
1099131377Stjr            }
1100131377Stjr            Tracevv((stderr, "inflate:         length %u\n", state->length));
1101205194Sdelphij            state->was = state->length;
1102131377Stjr            state->mode = DIST;
1103131377Stjr        case DIST:
1104131377Stjr            for (;;) {
1105205194Sdelphij                here = state->distcode[BITS(state->distbits)];
1106205194Sdelphij                if ((unsigned)(here.bits) <= bits) break;
1107131377Stjr                PULLBYTE();
1108131377Stjr            }
1109205194Sdelphij            if ((here.op & 0xf0) == 0) {
1110205194Sdelphij                last = here;
1111131377Stjr                for (;;) {
1112205194Sdelphij                    here = state->distcode[last.val +
1113131377Stjr                            (BITS(last.bits + last.op) >> last.bits)];
1114205194Sdelphij                    if ((unsigned)(last.bits + here.bits) <= bits) break;
1115131377Stjr                    PULLBYTE();
1116131377Stjr                }
1117131377Stjr                DROPBITS(last.bits);
1118205194Sdelphij                state->back += last.bits;
1119131377Stjr            }
1120205194Sdelphij            DROPBITS(here.bits);
1121205194Sdelphij            state->back += here.bits;
1122205194Sdelphij            if (here.op & 64) {
1123131377Stjr                strm->msg = (char *)"invalid distance code";
1124131377Stjr                state->mode = BAD;
1125131377Stjr                break;
1126131377Stjr            }
1127205194Sdelphij            state->offset = (unsigned)here.val;
1128205194Sdelphij            state->extra = (unsigned)(here.op) & 15;
1129131377Stjr            state->mode = DISTEXT;
1130131377Stjr        case DISTEXT:
1131131377Stjr            if (state->extra) {
1132131377Stjr                NEEDBITS(state->extra);
1133131377Stjr                state->offset += BITS(state->extra);
1134131377Stjr                DROPBITS(state->extra);
1135205194Sdelphij                state->back += state->extra;
1136131377Stjr            }
1137157043Sdes#ifdef INFLATE_STRICT
1138157043Sdes            if (state->offset > state->dmax) {
1139157043Sdes                strm->msg = (char *)"invalid distance too far back";
1140157043Sdes                state->mode = BAD;
1141157043Sdes                break;
1142157043Sdes            }
1143157043Sdes#endif
1144131377Stjr            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
1145131377Stjr            state->mode = MATCH;
1146131377Stjr        case MATCH:
1147131377Stjr            if (left == 0) goto inf_leave;
1148131377Stjr            copy = out - left;
1149131377Stjr            if (state->offset > copy) {         /* copy from window */
1150131377Stjr                copy = state->offset - copy;
1151205194Sdelphij                if (copy > state->whave) {
1152205194Sdelphij                    if (state->sane) {
1153205194Sdelphij                        strm->msg = (char *)"invalid distance too far back";
1154205194Sdelphij                        state->mode = BAD;
1155205194Sdelphij                        break;
1156205194Sdelphij                    }
1157205194Sdelphij#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1158205194Sdelphij                    Trace((stderr, "inflate.c too far\n"));
1159205194Sdelphij                    copy -= state->whave;
1160205194Sdelphij                    if (copy > state->length) copy = state->length;
1161205194Sdelphij                    if (copy > left) copy = left;
1162205194Sdelphij                    left -= copy;
1163205194Sdelphij                    state->length -= copy;
1164205194Sdelphij                    do {
1165205194Sdelphij                        *put++ = 0;
1166205194Sdelphij                    } while (--copy);
1167205194Sdelphij                    if (state->length == 0) state->mode = LEN;
1168205194Sdelphij                    break;
1169205194Sdelphij#endif
1170205194Sdelphij                }
1171205194Sdelphij                if (copy > state->wnext) {
1172205194Sdelphij                    copy -= state->wnext;
1173131377Stjr                    from = state->window + (state->wsize - copy);
1174131377Stjr                }
1175131377Stjr                else
1176205194Sdelphij                    from = state->window + (state->wnext - copy);
1177131377Stjr                if (copy > state->length) copy = state->length;
1178131377Stjr            }
1179131377Stjr            else {                              /* copy from output */
1180131377Stjr                from = put - state->offset;
1181131377Stjr                copy = state->length;
1182131377Stjr            }
1183131377Stjr            if (copy > left) copy = left;
1184131377Stjr            left -= copy;
1185131377Stjr            state->length -= copy;
1186131377Stjr            do {
1187131377Stjr                *put++ = *from++;
1188131377Stjr            } while (--copy);
1189131377Stjr            if (state->length == 0) state->mode = LEN;
1190131377Stjr            break;
1191131377Stjr        case LIT:
1192131377Stjr            if (left == 0) goto inf_leave;
1193131377Stjr            *put++ = (unsigned char)(state->length);
1194131377Stjr            left--;
1195131377Stjr            state->mode = LEN;
1196131377Stjr            break;
1197131377Stjr        case CHECK:
1198131377Stjr            if (state->wrap) {
1199131377Stjr                NEEDBITS(32);
1200131377Stjr                out -= left;
1201131377Stjr                strm->total_out += out;
1202131377Stjr                state->total += out;
1203311285Sdelphij                if ((state->wrap & 4) && out)
1204131377Stjr                    strm->adler = state->check =
1205131377Stjr                        UPDATE(state->check, put - out, out);
1206131377Stjr                out = left;
1207311285Sdelphij                if ((state->wrap & 4) && (
1208131377Stjr#ifdef GUNZIP
1209131377Stjr                     state->flags ? hold :
1210131377Stjr#endif
1211237248Sdelphij                     ZSWAP32(hold)) != state->check) {
1212131377Stjr                    strm->msg = (char *)"incorrect data check";
1213131377Stjr                    state->mode = BAD;
1214131377Stjr                    break;
1215131377Stjr                }
1216131377Stjr                INITBITS();
1217131377Stjr                Tracev((stderr, "inflate:   check matches trailer\n"));
1218131377Stjr            }
1219131377Stjr#ifdef GUNZIP
1220131377Stjr            state->mode = LENGTH;
1221131377Stjr        case LENGTH:
1222131377Stjr            if (state->wrap && state->flags) {
1223131377Stjr                NEEDBITS(32);
1224131377Stjr                if (hold != (state->total & 0xffffffffUL)) {
1225131377Stjr                    strm->msg = (char *)"incorrect length check";
1226131377Stjr                    state->mode = BAD;
1227131377Stjr                    break;
1228131377Stjr                }
1229131377Stjr                INITBITS();
1230131377Stjr                Tracev((stderr, "inflate:   length matches trailer\n"));
1231131377Stjr            }
1232131377Stjr#endif
1233131377Stjr            state->mode = DONE;
1234131377Stjr        case DONE:
1235131377Stjr            ret = Z_STREAM_END;
1236131377Stjr            goto inf_leave;
1237131377Stjr        case BAD:
1238131377Stjr            ret = Z_DATA_ERROR;
1239131377Stjr            goto inf_leave;
1240131377Stjr        case MEM:
1241131377Stjr            return Z_MEM_ERROR;
1242131377Stjr        case SYNC:
1243131377Stjr        default:
1244131377Stjr            return Z_STREAM_ERROR;
1245131377Stjr        }
1246131377Stjr
1247131377Stjr    /*
1248131377Stjr       Return from inflate(), updating the total counts and the check value.
1249131377Stjr       If there was no progress during the inflate() call, return a buffer
1250131377Stjr       error.  Call updatewindow() to create and/or update the window state.
1251131377Stjr       Note: a memory error from inflate() is non-recoverable.
1252131377Stjr     */
1253131377Stjr  inf_leave:
1254131377Stjr    RESTORE();
1255230837Sdelphij    if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1256230837Sdelphij            (state->mode < CHECK || flush != Z_FINISH)))
1257250224Sdelphij        if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1258131377Stjr            state->mode = MEM;
1259131377Stjr            return Z_MEM_ERROR;
1260131377Stjr        }
1261131377Stjr    in -= strm->avail_in;
1262131377Stjr    out -= strm->avail_out;
1263131377Stjr    strm->total_in += in;
1264131377Stjr    strm->total_out += out;
1265131377Stjr    state->total += out;
1266311285Sdelphij    if ((state->wrap & 4) && out)
1267131377Stjr        strm->adler = state->check =
1268131377Stjr            UPDATE(state->check, strm->next_out - out, out);
1269311285Sdelphij    strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1270205194Sdelphij                      (state->mode == TYPE ? 128 : 0) +
1271205194Sdelphij                      (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1272131377Stjr    if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1273131377Stjr        ret = Z_BUF_ERROR;
1274131377Stjr    return ret;
127517651Speter}
127617651Speter
1277131377Stjrint ZEXPORT inflateEnd(strm)
1278131377Stjrz_streamp strm;
1279131377Stjr{
1280131377Stjr    struct inflate_state FAR *state;
1281311285Sdelphij    if (inflateStateCheck(strm))
1282131377Stjr        return Z_STREAM_ERROR;
1283131377Stjr    state = (struct inflate_state FAR *)strm->state;
1284131377Stjr    if (state->window != Z_NULL) ZFREE(strm, state->window);
1285131377Stjr    ZFREE(strm, strm->state);
1286131377Stjr    strm->state = Z_NULL;
1287131377Stjr    Tracev((stderr, "inflate: end\n"));
1288131377Stjr    return Z_OK;
1289131377Stjr}
129017651Speter
1291250224Sdelphijint ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
1292250224Sdelphijz_streamp strm;
1293250224SdelphijBytef *dictionary;
1294250224SdelphijuInt *dictLength;
1295250224Sdelphij{
1296250224Sdelphij    struct inflate_state FAR *state;
1297250224Sdelphij
1298250224Sdelphij    /* check state */
1299311285Sdelphij    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1300250224Sdelphij    state = (struct inflate_state FAR *)strm->state;
1301250224Sdelphij
1302250224Sdelphij    /* copy dictionary */
1303250224Sdelphij    if (state->whave && dictionary != Z_NULL) {
1304250224Sdelphij        zmemcpy(dictionary, state->window + state->wnext,
1305250224Sdelphij                state->whave - state->wnext);
1306250224Sdelphij        zmemcpy(dictionary + state->whave - state->wnext,
1307250224Sdelphij                state->window, state->wnext);
1308250224Sdelphij    }
1309250224Sdelphij    if (dictLength != Z_NULL)
1310250224Sdelphij        *dictLength = state->whave;
1311250224Sdelphij    return Z_OK;
1312250224Sdelphij}
1313250224Sdelphij
1314131377Stjrint ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1315131377Stjrz_streamp strm;
131617651Speterconst Bytef *dictionary;
1317131377StjruInt dictLength;
131817651Speter{
1319131377Stjr    struct inflate_state FAR *state;
1320237248Sdelphij    unsigned long dictid;
1321230837Sdelphij    int ret;
132217651Speter
1323131377Stjr    /* check state */
1324311285Sdelphij    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1325131377Stjr    state = (struct inflate_state FAR *)strm->state;
1326157043Sdes    if (state->wrap != 0 && state->mode != DICT)
1327157043Sdes        return Z_STREAM_ERROR;
132817651Speter
1329237248Sdelphij    /* check for correct dictionary identifier */
1330157043Sdes    if (state->mode == DICT) {
1331237248Sdelphij        dictid = adler32(0L, Z_NULL, 0);
1332237248Sdelphij        dictid = adler32(dictid, dictionary, dictLength);
1333237248Sdelphij        if (dictid != state->check)
1334157043Sdes            return Z_DATA_ERROR;
1335157043Sdes    }
133617651Speter
1337230837Sdelphij    /* copy dictionary to window using updatewindow(), which will amend the
1338230837Sdelphij       existing dictionary if appropriate */
1339250224Sdelphij    ret = updatewindow(strm, dictionary + dictLength, dictLength);
1340230837Sdelphij    if (ret) {
1341131377Stjr        state->mode = MEM;
1342131377Stjr        return Z_MEM_ERROR;
1343131377Stjr    }
1344131377Stjr    state->havedict = 1;
1345131377Stjr    Tracev((stderr, "inflate:   dictionary set\n"));
1346131377Stjr    return Z_OK;
134717651Speter}
134817651Speter
1349157043Sdesint ZEXPORT inflateGetHeader(strm, head)
1350157043Sdesz_streamp strm;
1351157043Sdesgz_headerp head;
1352157043Sdes{
1353157043Sdes    struct inflate_state FAR *state;
1354157043Sdes
1355157043Sdes    /* check state */
1356311285Sdelphij    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1357157043Sdes    state = (struct inflate_state FAR *)strm->state;
1358157043Sdes    if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1359157043Sdes
1360157043Sdes    /* save header structure */
1361157043Sdes    state->head = head;
1362157043Sdes    head->done = 0;
1363157043Sdes    return Z_OK;
1364157043Sdes}
1365157043Sdes
1366131377Stjr/*
1367131377Stjr   Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
1368131377Stjr   or when out of input.  When called, *have is the number of pattern bytes
1369131377Stjr   found in order so far, in 0..3.  On return *have is updated to the new
1370131377Stjr   state.  If on return *have equals four, then the pattern was found and the
1371131377Stjr   return value is how many bytes were read including the last byte of the
1372131377Stjr   pattern.  If *have is less than four, then the pattern has not been found
1373131377Stjr   yet and the return value is len.  In the latter case, syncsearch() can be
1374131377Stjr   called again with more data and the *have state.  *have is initialized to
1375131377Stjr   zero for the first call.
1376131377Stjr */
1377131377Stjrlocal unsigned syncsearch(have, buf, len)
1378131377Stjrunsigned FAR *have;
1379250224Sdelphijconst unsigned char FAR *buf;
1380131377Stjrunsigned len;
1381131377Stjr{
1382131377Stjr    unsigned got;
1383131377Stjr    unsigned next;
138417651Speter
1385131377Stjr    got = *have;
1386131377Stjr    next = 0;
1387131377Stjr    while (next < len && got < 4) {
1388131377Stjr        if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1389131377Stjr            got++;
1390131377Stjr        else if (buf[next])
1391131377Stjr            got = 0;
1392131377Stjr        else
1393131377Stjr            got = 4 - got;
1394131377Stjr        next++;
1395131377Stjr    }
1396131377Stjr    *have = got;
1397131377Stjr    return next;
1398131377Stjr}
1399131377Stjr
1400131377Stjrint ZEXPORT inflateSync(strm)
1401131377Stjrz_streamp strm;
140217651Speter{
1403131377Stjr    unsigned len;               /* number of bytes to look at or looked at */
1404131377Stjr    unsigned long in, out;      /* temporary to save total_in and total_out */
1405131377Stjr    unsigned char buf[4];       /* to restore bit buffer to byte string */
1406131377Stjr    struct inflate_state FAR *state;
140717651Speter
1408131377Stjr    /* check parameters */
1409311285Sdelphij    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1410131377Stjr    state = (struct inflate_state FAR *)strm->state;
1411131377Stjr    if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
141217651Speter
1413131377Stjr    /* if first time, start search in bit buffer */
1414131377Stjr    if (state->mode != SYNC) {
1415131377Stjr        state->mode = SYNC;
1416131377Stjr        state->hold <<= state->bits & 7;
1417131377Stjr        state->bits -= state->bits & 7;
1418131377Stjr        len = 0;
1419131377Stjr        while (state->bits >= 8) {
1420131377Stjr            buf[len++] = (unsigned char)(state->hold);
1421131377Stjr            state->hold >>= 8;
1422131377Stjr            state->bits -= 8;
1423131377Stjr        }
1424131377Stjr        state->have = 0;
1425131377Stjr        syncsearch(&(state->have), buf, len);
1426131377Stjr    }
142717651Speter
1428131377Stjr    /* search available input */
1429131377Stjr    len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1430131377Stjr    strm->avail_in -= len;
1431131377Stjr    strm->next_in += len;
1432131377Stjr    strm->total_in += len;
143317651Speter
1434131377Stjr    /* return no joy or set up to restart inflate() on a new block */
1435131377Stjr    if (state->have != 4) return Z_DATA_ERROR;
1436131377Stjr    in = strm->total_in;  out = strm->total_out;
1437131377Stjr    inflateReset(strm);
1438131377Stjr    strm->total_in = in;  strm->total_out = out;
1439131377Stjr    state->mode = TYPE;
1440131377Stjr    return Z_OK;
144117651Speter}
144233904Ssteve
1443131377Stjr/*
1444131377Stjr   Returns true if inflate is currently at the end of a block generated by
1445131377Stjr   Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1446131377Stjr   implementation to provide an additional safety check. PPP uses
1447131377Stjr   Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1448131377Stjr   block. When decompressing, PPP checks that at the end of input packet,
1449131377Stjr   inflate is waiting for these length bytes.
145033904Ssteve */
1451131377Stjrint ZEXPORT inflateSyncPoint(strm)
1452131377Stjrz_streamp strm;
145333904Ssteve{
1454131377Stjr    struct inflate_state FAR *state;
1455131377Stjr
1456311285Sdelphij    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1457131377Stjr    state = (struct inflate_state FAR *)strm->state;
1458131377Stjr    return state->mode == STORED && state->bits == 0;
145933904Ssteve}
1460131377Stjr
1461131377Stjrint ZEXPORT inflateCopy(dest, source)
1462131377Stjrz_streamp dest;
1463131377Stjrz_streamp source;
1464131377Stjr{
1465131377Stjr    struct inflate_state FAR *state;
1466131377Stjr    struct inflate_state FAR *copy;
1467131377Stjr    unsigned char FAR *window;
1468157043Sdes    unsigned wsize;
1469131377Stjr
1470131377Stjr    /* check input */
1471311285Sdelphij    if (inflateStateCheck(source) || dest == Z_NULL)
1472131377Stjr        return Z_STREAM_ERROR;
1473131377Stjr    state = (struct inflate_state FAR *)source->state;
1474131377Stjr
1475131377Stjr    /* allocate space */
1476131377Stjr    copy = (struct inflate_state FAR *)
1477131377Stjr           ZALLOC(source, 1, sizeof(struct inflate_state));
1478131377Stjr    if (copy == Z_NULL) return Z_MEM_ERROR;
1479131377Stjr    window = Z_NULL;
1480131377Stjr    if (state->window != Z_NULL) {
1481131377Stjr        window = (unsigned char FAR *)
1482131377Stjr                 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1483131377Stjr        if (window == Z_NULL) {
1484131377Stjr            ZFREE(source, copy);
1485131377Stjr            return Z_MEM_ERROR;
1486131377Stjr        }
1487131377Stjr    }
1488131377Stjr
1489131377Stjr    /* copy state */
1490230837Sdelphij    zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1491230837Sdelphij    zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1492311285Sdelphij    copy->strm = dest;
1493157043Sdes    if (state->lencode >= state->codes &&
1494157043Sdes        state->lencode <= state->codes + ENOUGH - 1) {
1495157043Sdes        copy->lencode = copy->codes + (state->lencode - state->codes);
1496157043Sdes        copy->distcode = copy->codes + (state->distcode - state->codes);
1497157043Sdes    }
1498131377Stjr    copy->next = copy->codes + (state->next - state->codes);
1499157043Sdes    if (window != Z_NULL) {
1500157043Sdes        wsize = 1U << state->wbits;
1501157043Sdes        zmemcpy(window, state->window, wsize);
1502157043Sdes    }
1503131377Stjr    copy->window = window;
1504157043Sdes    dest->state = (struct internal_state FAR *)copy;
1505131377Stjr    return Z_OK;
1506131377Stjr}
1507205194Sdelphij
1508205194Sdelphijint ZEXPORT inflateUndermine(strm, subvert)
1509205194Sdelphijz_streamp strm;
1510205194Sdelphijint subvert;
1511205194Sdelphij{
1512205194Sdelphij    struct inflate_state FAR *state;
1513205194Sdelphij
1514311285Sdelphij    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1515205194Sdelphij    state = (struct inflate_state FAR *)strm->state;
1516311285Sdelphij#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1517205194Sdelphij    state->sane = !subvert;
1518205194Sdelphij    return Z_OK;
1519205194Sdelphij#else
1520311285Sdelphij    (void)subvert;
1521205194Sdelphij    state->sane = 1;
1522205194Sdelphij    return Z_DATA_ERROR;
1523205194Sdelphij#endif
1524205194Sdelphij}
1525205194Sdelphij
1526311285Sdelphijint ZEXPORT inflateValidate(strm, check)
1527311285Sdelphijz_streamp strm;
1528311285Sdelphijint check;
1529311285Sdelphij{
1530311285Sdelphij    struct inflate_state FAR *state;
1531311285Sdelphij
1532311285Sdelphij    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1533311285Sdelphij    state = (struct inflate_state FAR *)strm->state;
1534311285Sdelphij    if (check)
1535311285Sdelphij        state->wrap |= 4;
1536311285Sdelphij    else
1537311285Sdelphij        state->wrap &= ~4;
1538311285Sdelphij    return Z_OK;
1539311285Sdelphij}
1540311285Sdelphij
1541205194Sdelphijlong ZEXPORT inflateMark(strm)
1542205194Sdelphijz_streamp strm;
1543205194Sdelphij{
1544205194Sdelphij    struct inflate_state FAR *state;
1545205194Sdelphij
1546311285Sdelphij    if (inflateStateCheck(strm))
1547311285Sdelphij        return -(1L << 16);
1548205194Sdelphij    state = (struct inflate_state FAR *)strm->state;
1549311285Sdelphij    return (long)(((unsigned long)((long)state->back)) << 16) +
1550205194Sdelphij        (state->mode == COPY ? state->length :
1551205194Sdelphij            (state->mode == MATCH ? state->was - state->length : 0));
1552205194Sdelphij}
1553311285Sdelphij
1554311285Sdelphijunsigned long ZEXPORT inflateCodesUsed(strm)
1555311285Sdelphijz_streamp strm;
1556311285Sdelphij{
1557311285Sdelphij    struct inflate_state FAR *state;
1558311285Sdelphij    if (inflateStateCheck(strm)) return (unsigned long)-1;
1559311285Sdelphij    state = (struct inflate_state FAR *)strm->state;
1560311285Sdelphij    return (unsigned long)(state->next - state->codes);
1561311285Sdelphij}
1562