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