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