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