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