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