xref: /minix3/crypto/external/bsd/netpgp/dist/src/netpgpverify/zlib.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: zlib.c,v 1.2 2015/02/05 01:26:54 agc Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /* inflate.c -- zlib decompression
4*0a6a1f1dSLionel Sambuc  * Copyright (C) 1995-2005 Mark Adler
5*0a6a1f1dSLionel Sambuc  * For conditions of distribution and use, see copyright notice in zlib.h
6*0a6a1f1dSLionel Sambuc  */
7*0a6a1f1dSLionel Sambuc 
8*0a6a1f1dSLionel Sambuc /*
9*0a6a1f1dSLionel Sambuc  * Change history:
10*0a6a1f1dSLionel Sambuc  *
11*0a6a1f1dSLionel Sambuc  * 1.2.beta0    24 Nov 2002
12*0a6a1f1dSLionel Sambuc  * - First version -- complete rewrite of inflate to simplify code, avoid
13*0a6a1f1dSLionel Sambuc  *   creation of window when not needed, minimize use of window when it is
14*0a6a1f1dSLionel Sambuc  *   needed, make inffast.c even faster, implement gzip decoding, and to
15*0a6a1f1dSLionel Sambuc  *   improve code readability and style over the previous zlib inflate code
16*0a6a1f1dSLionel Sambuc  *
17*0a6a1f1dSLionel Sambuc  * 1.2.beta1    25 Nov 2002
18*0a6a1f1dSLionel Sambuc  * - Use pointers for available input and output checking in inffast.c
19*0a6a1f1dSLionel Sambuc  * - Remove input and output counters in inffast.c
20*0a6a1f1dSLionel Sambuc  * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
21*0a6a1f1dSLionel Sambuc  * - Remove unnecessary second byte pull from length extra in inffast.c
22*0a6a1f1dSLionel Sambuc  * - Unroll direct copy to three copies per loop in inffast.c
23*0a6a1f1dSLionel Sambuc  *
24*0a6a1f1dSLionel Sambuc  * 1.2.beta2    4 Dec 2002
25*0a6a1f1dSLionel Sambuc  * - Change external routine names to reduce potential conflicts
26*0a6a1f1dSLionel Sambuc  * - Correct filename to inffixed.h for fixed tables in inflate.c
27*0a6a1f1dSLionel Sambuc  * - Make hbuf[] unsigned char to match parameter type in inflate.c
28*0a6a1f1dSLionel Sambuc  * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
29*0a6a1f1dSLionel Sambuc  *   to avoid negation problem on Alphas (64 bit) in inflate.c
30*0a6a1f1dSLionel Sambuc  *
31*0a6a1f1dSLionel Sambuc  * 1.2.beta3    22 Dec 2002
32*0a6a1f1dSLionel Sambuc  * - Add comments on state->bits assertion in inffast.c
33*0a6a1f1dSLionel Sambuc  * - Add comments on op field in inftrees.h
34*0a6a1f1dSLionel Sambuc  * - Fix bug in reuse of allocated window after inflateReset()
35*0a6a1f1dSLionel Sambuc  * - Remove bit fields--back to byte structure for speed
36*0a6a1f1dSLionel Sambuc  * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
37*0a6a1f1dSLionel Sambuc  * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
38*0a6a1f1dSLionel Sambuc  * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
39*0a6a1f1dSLionel Sambuc  * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
40*0a6a1f1dSLionel Sambuc  * - Use local copies of stream next and avail values, as well as local bit
41*0a6a1f1dSLionel Sambuc  *   buffer and bit count in inflate()--for speed when inflate_fast() not used
42*0a6a1f1dSLionel Sambuc  *
43*0a6a1f1dSLionel Sambuc  * 1.2.beta4    1 Jan 2003
44*0a6a1f1dSLionel Sambuc  * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
45*0a6a1f1dSLionel Sambuc  * - Move a comment on output buffer sizes from inffast.c to inflate.c
46*0a6a1f1dSLionel Sambuc  * - Add comments in inffast.c to introduce the inflate_fast() routine
47*0a6a1f1dSLionel Sambuc  * - Rearrange window copies in inflate_fast() for speed and simplification
48*0a6a1f1dSLionel Sambuc  * - Unroll last copy for window match in inflate_fast()
49*0a6a1f1dSLionel Sambuc  * - Use local copies of window variables in inflate_fast() for speed
50*0a6a1f1dSLionel Sambuc  * - Pull out common write == 0 case for speed in inflate_fast()
51*0a6a1f1dSLionel Sambuc  * - Make op and len in inflate_fast() unsigned for consistency
52*0a6a1f1dSLionel Sambuc  * - Add FAR to lcode and dcode declarations in inflate_fast()
53*0a6a1f1dSLionel Sambuc  * - Simplified bad distance check in inflate_fast()
54*0a6a1f1dSLionel Sambuc  * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
55*0a6a1f1dSLionel Sambuc  *   source file infback.c to provide a call-back interface to inflate for
56*0a6a1f1dSLionel Sambuc  *   programs like gzip and unzip -- uses window as output buffer to avoid
57*0a6a1f1dSLionel Sambuc  *   window copying
58*0a6a1f1dSLionel Sambuc  *
59*0a6a1f1dSLionel Sambuc  * 1.2.beta5    1 Jan 2003
60*0a6a1f1dSLionel Sambuc  * - Improved inflateBack() interface to allow the caller to provide initial
61*0a6a1f1dSLionel Sambuc  *   input in strm.
62*0a6a1f1dSLionel Sambuc  * - Fixed stored blocks bug in inflateBack()
63*0a6a1f1dSLionel Sambuc  *
64*0a6a1f1dSLionel Sambuc  * 1.2.beta6    4 Jan 2003
65*0a6a1f1dSLionel Sambuc  * - Added comments in inffast.c on effectiveness of POSTINC
66*0a6a1f1dSLionel Sambuc  * - Typecasting all around to reduce compiler warnings
67*0a6a1f1dSLionel Sambuc  * - Changed loops from while (1) or do {} while (1) to for (;;), again to
68*0a6a1f1dSLionel Sambuc  *   make compilers happy
69*0a6a1f1dSLionel Sambuc  * - Changed type of window in inflateBackInit() to unsigned char *
70*0a6a1f1dSLionel Sambuc  *
71*0a6a1f1dSLionel Sambuc  * 1.2.beta7    27 Jan 2003
72*0a6a1f1dSLionel Sambuc  * - Changed many types to unsigned or unsigned short to avoid warnings
73*0a6a1f1dSLionel Sambuc  * - Added inflateCopy() function
74*0a6a1f1dSLionel Sambuc  *
75*0a6a1f1dSLionel Sambuc  * 1.2.0        9 Mar 2003
76*0a6a1f1dSLionel Sambuc  * - Changed inflateBack() interface to provide separate opaque descriptors
77*0a6a1f1dSLionel Sambuc  *   for the in() and out() functions
78*0a6a1f1dSLionel Sambuc  * - Changed inflateBack() argument and in_func typedef to swap the length
79*0a6a1f1dSLionel Sambuc  *   and buffer address return values for the input function
80*0a6a1f1dSLionel Sambuc  * - Check next_in and next_out for Z_NULL on entry to inflate()
81*0a6a1f1dSLionel Sambuc  *
82*0a6a1f1dSLionel Sambuc  * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
83*0a6a1f1dSLionel Sambuc  */
84*0a6a1f1dSLionel Sambuc #include "config.h"
85*0a6a1f1dSLionel Sambuc 
86*0a6a1f1dSLionel Sambuc #include <stdio.h>
87*0a6a1f1dSLionel Sambuc #include <stdlib.h>
88*0a6a1f1dSLionel Sambuc #include <string.h>
89*0a6a1f1dSLionel Sambuc 
90*0a6a1f1dSLionel Sambuc #include "zlib.h"
91*0a6a1f1dSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc #ifdef MAKEFIXED
93*0a6a1f1dSLionel Sambuc #  ifndef BUILDFIXED
94*0a6a1f1dSLionel Sambuc #    define BUILDFIXED
95*0a6a1f1dSLionel Sambuc #  endif
96*0a6a1f1dSLionel Sambuc #endif
97*0a6a1f1dSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc #ifndef local
99*0a6a1f1dSLionel Sambuc #define local static
100*0a6a1f1dSLionel Sambuc #endif
101*0a6a1f1dSLionel Sambuc 
102*0a6a1f1dSLionel Sambuc #define ZALLOC(strm, items, size) \
103*0a6a1f1dSLionel Sambuc             (*((strm)->zalloc))((strm)->opaque, (items), (size))
104*0a6a1f1dSLionel Sambuc #define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
105*0a6a1f1dSLionel Sambuc #define Tracev(x)
106*0a6a1f1dSLionel Sambuc #define Tracevv(x)
107*0a6a1f1dSLionel Sambuc 
108*0a6a1f1dSLionel Sambuc /* Possible inflate modes between inflate() calls */
109*0a6a1f1dSLionel Sambuc typedef enum {
110*0a6a1f1dSLionel Sambuc     HEAD,       /* i: waiting for magic header */
111*0a6a1f1dSLionel Sambuc     FLAGS,      /* i: waiting for method and flags (gzip) */
112*0a6a1f1dSLionel Sambuc     TIME,       /* i: waiting for modification time (gzip) */
113*0a6a1f1dSLionel Sambuc     OS,         /* i: waiting for extra flags and operating system (gzip) */
114*0a6a1f1dSLionel Sambuc     EXLEN,      /* i: waiting for extra length (gzip) */
115*0a6a1f1dSLionel Sambuc     EXTRA,      /* i: waiting for extra bytes (gzip) */
116*0a6a1f1dSLionel Sambuc     NAME,       /* i: waiting for end of file name (gzip) */
117*0a6a1f1dSLionel Sambuc     COMMENT,    /* i: waiting for end of comment (gzip) */
118*0a6a1f1dSLionel Sambuc     HCRC,       /* i: waiting for header crc (gzip) */
119*0a6a1f1dSLionel Sambuc     DICTID,     /* i: waiting for dictionary check value */
120*0a6a1f1dSLionel Sambuc     DICT,       /* waiting for inflateSetDictionary() call */
121*0a6a1f1dSLionel Sambuc         TYPE,       /* i: waiting for type bits, including last-flag bit */
122*0a6a1f1dSLionel Sambuc         TYPEDO,     /* i: same, but skip check to exit inflate on new block */
123*0a6a1f1dSLionel Sambuc         STORED,     /* i: waiting for stored size (length and complement) */
124*0a6a1f1dSLionel Sambuc         COPY,       /* i/o: waiting for input or output to copy stored block */
125*0a6a1f1dSLionel Sambuc         TABLE,      /* i: waiting for dynamic block table lengths */
126*0a6a1f1dSLionel Sambuc         LENLENS,    /* i: waiting for code length code lengths */
127*0a6a1f1dSLionel Sambuc         CODELENS,   /* i: waiting for length/lit and distance code lengths */
128*0a6a1f1dSLionel Sambuc             LEN,        /* i: waiting for length/lit code */
129*0a6a1f1dSLionel Sambuc             LENEXT,     /* i: waiting for length extra bits */
130*0a6a1f1dSLionel Sambuc             DIST,       /* i: waiting for distance code */
131*0a6a1f1dSLionel Sambuc             DISTEXT,    /* i: waiting for distance extra bits */
132*0a6a1f1dSLionel Sambuc             MATCH,      /* o: waiting for output space to copy string */
133*0a6a1f1dSLionel Sambuc             LIT,        /* o: waiting for output space to write literal */
134*0a6a1f1dSLionel Sambuc     CHECK,      /* i: waiting for 32-bit check value */
135*0a6a1f1dSLionel Sambuc     LENGTH,     /* i: waiting for 32-bit length (gzip) */
136*0a6a1f1dSLionel Sambuc     DONE,       /* finished check, done -- remain here until reset */
137*0a6a1f1dSLionel Sambuc     BAD,        /* got a data error -- remain here until reset */
138*0a6a1f1dSLionel Sambuc     MEM,        /* got an inflate() memory error -- remain here until reset */
139*0a6a1f1dSLionel Sambuc     SYNC        /* looking for synchronization bytes to restart inflate() */
140*0a6a1f1dSLionel Sambuc } inflate_mode;
141*0a6a1f1dSLionel Sambuc 
142*0a6a1f1dSLionel Sambuc /* Structure for decoding tables.  Each entry provides either the
143*0a6a1f1dSLionel Sambuc    information needed to do the operation requested by the code that
144*0a6a1f1dSLionel Sambuc    indexed that table entry, or it provides a pointer to another
145*0a6a1f1dSLionel Sambuc    table that indexes more bits of the code.  op indicates whether
146*0a6a1f1dSLionel Sambuc    the entry is a pointer to another table, a literal, a length or
147*0a6a1f1dSLionel Sambuc    distance, an end-of-block, or an invalid code.  For a table
148*0a6a1f1dSLionel Sambuc    pointer, the low four bits of op is the number of index bits of
149*0a6a1f1dSLionel Sambuc    that table.  For a length or distance, the low four bits of op
150*0a6a1f1dSLionel Sambuc    is the number of extra bits to get after the code.  bits is
151*0a6a1f1dSLionel Sambuc    the number of bits in this code or part of the code to drop off
152*0a6a1f1dSLionel Sambuc    of the bit buffer.  val is the actual byte to output in the case
153*0a6a1f1dSLionel Sambuc    of a literal, the base length or distance, or the offset from
154*0a6a1f1dSLionel Sambuc    the current table to the next table.  Each entry is four bytes. */
155*0a6a1f1dSLionel Sambuc typedef struct {
156*0a6a1f1dSLionel Sambuc     unsigned char op;           /* operation, extra bits, table bits */
157*0a6a1f1dSLionel Sambuc     unsigned char bits;         /* bits in this part of the code */
158*0a6a1f1dSLionel Sambuc     unsigned short val;         /* offset in table or code value */
159*0a6a1f1dSLionel Sambuc } code;
160*0a6a1f1dSLionel Sambuc 
161*0a6a1f1dSLionel Sambuc #define ENOUGH 2048
162*0a6a1f1dSLionel Sambuc 
163*0a6a1f1dSLionel Sambuc /* Type of code to build for inftable() */
164*0a6a1f1dSLionel Sambuc typedef enum {
165*0a6a1f1dSLionel Sambuc     CODES,
166*0a6a1f1dSLionel Sambuc     LENS,
167*0a6a1f1dSLionel Sambuc     DISTS
168*0a6a1f1dSLionel Sambuc } codetype;
169*0a6a1f1dSLionel Sambuc 
170*0a6a1f1dSLionel Sambuc #ifndef DEF_WBITS
171*0a6a1f1dSLionel Sambuc #  define DEF_WBITS MAX_WBITS
172*0a6a1f1dSLionel Sambuc #endif
173*0a6a1f1dSLionel Sambuc 
174*0a6a1f1dSLionel Sambuc #define zmemcpy memcpy
175*0a6a1f1dSLionel Sambuc 
176*0a6a1f1dSLionel Sambuc /*
177*0a6a1f1dSLionel Sambuc     State transitions between above modes -
178*0a6a1f1dSLionel Sambuc 
179*0a6a1f1dSLionel Sambuc     (most modes can go to the BAD or MEM mode -- not shown for clarity)
180*0a6a1f1dSLionel Sambuc 
181*0a6a1f1dSLionel Sambuc     Process header:
182*0a6a1f1dSLionel Sambuc         HEAD -> (gzip) or (zlib)
183*0a6a1f1dSLionel Sambuc         (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME
184*0a6a1f1dSLionel Sambuc         NAME -> COMMENT -> HCRC -> TYPE
185*0a6a1f1dSLionel Sambuc         (zlib) -> DICTID or TYPE
186*0a6a1f1dSLionel Sambuc         DICTID -> DICT -> TYPE
187*0a6a1f1dSLionel Sambuc     Read deflate blocks:
188*0a6a1f1dSLionel Sambuc             TYPE -> STORED or TABLE or LEN or CHECK
189*0a6a1f1dSLionel Sambuc             STORED -> COPY -> TYPE
190*0a6a1f1dSLionel Sambuc             TABLE -> LENLENS -> CODELENS -> LEN
191*0a6a1f1dSLionel Sambuc     Read deflate codes:
192*0a6a1f1dSLionel Sambuc                 LEN -> LENEXT or LIT or TYPE
193*0a6a1f1dSLionel Sambuc                 LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
194*0a6a1f1dSLionel Sambuc                 LIT -> LEN
195*0a6a1f1dSLionel Sambuc     Process trailer:
196*0a6a1f1dSLionel Sambuc         CHECK -> LENGTH -> DONE
197*0a6a1f1dSLionel Sambuc  */
198*0a6a1f1dSLionel Sambuc 
199*0a6a1f1dSLionel Sambuc /* state maintained between inflate() calls.  Approximately 7K bytes. */
200*0a6a1f1dSLionel Sambuc struct inflate_state {
201*0a6a1f1dSLionel Sambuc     inflate_mode mode;          /* current inflate mode */
202*0a6a1f1dSLionel Sambuc     int last;                   /* true if processing last block */
203*0a6a1f1dSLionel Sambuc     int wrap;                   /* bit 0 true for zlib, bit 1 true for gzip */
204*0a6a1f1dSLionel Sambuc     int havedict;               /* true if dictionary provided */
205*0a6a1f1dSLionel Sambuc     int flags;                  /* gzip header method and flags (0 if zlib) */
206*0a6a1f1dSLionel Sambuc     unsigned dmax;              /* zlib header max distance (INFLATE_STRICT) */
207*0a6a1f1dSLionel Sambuc     unsigned long check;        /* protected copy of check value */
208*0a6a1f1dSLionel Sambuc     unsigned long total;        /* protected copy of output count */
209*0a6a1f1dSLionel Sambuc     gz_headerp head;            /* where to save gzip header information */
210*0a6a1f1dSLionel Sambuc         /* sliding window */
211*0a6a1f1dSLionel Sambuc     unsigned wbits;             /* log base 2 of requested window size */
212*0a6a1f1dSLionel Sambuc     unsigned wsize;             /* window size or zero if not using window */
213*0a6a1f1dSLionel Sambuc     unsigned whave;             /* valid bytes in the window */
214*0a6a1f1dSLionel Sambuc     unsigned write;             /* window write index */
215*0a6a1f1dSLionel Sambuc     unsigned char FAR *window;  /* allocated sliding window, if needed */
216*0a6a1f1dSLionel Sambuc         /* bit accumulator */
217*0a6a1f1dSLionel Sambuc     unsigned long hold;         /* input bit accumulator */
218*0a6a1f1dSLionel Sambuc     unsigned bits;              /* number of bits in "in" */
219*0a6a1f1dSLionel Sambuc         /* for string and stored block copying */
220*0a6a1f1dSLionel Sambuc     unsigned length;            /* literal or length of data to copy */
221*0a6a1f1dSLionel Sambuc     unsigned offset;            /* distance back to copy string from */
222*0a6a1f1dSLionel Sambuc         /* for table and code decoding */
223*0a6a1f1dSLionel Sambuc     unsigned extra;             /* extra bits needed */
224*0a6a1f1dSLionel Sambuc         /* fixed and dynamic code tables */
225*0a6a1f1dSLionel Sambuc     code const FAR *lencode;    /* starting table for length/literal codes */
226*0a6a1f1dSLionel Sambuc     code const FAR *distcode;   /* starting table for distance codes */
227*0a6a1f1dSLionel Sambuc     unsigned lenbits;           /* index bits for lencode */
228*0a6a1f1dSLionel Sambuc     unsigned distbits;          /* index bits for distcode */
229*0a6a1f1dSLionel Sambuc         /* dynamic table building */
230*0a6a1f1dSLionel Sambuc     unsigned ncode;             /* number of code length code lengths */
231*0a6a1f1dSLionel Sambuc     unsigned nlen;              /* number of length code lengths */
232*0a6a1f1dSLionel Sambuc     unsigned ndist;             /* number of distance code lengths */
233*0a6a1f1dSLionel Sambuc     unsigned have;              /* number of code lengths in lens[] */
234*0a6a1f1dSLionel Sambuc     code FAR *next;             /* next available space in codes[] */
235*0a6a1f1dSLionel Sambuc     unsigned short lens[320];   /* temporary storage for code lengths */
236*0a6a1f1dSLionel Sambuc     unsigned short work[288];   /* work area for code table building */
237*0a6a1f1dSLionel Sambuc     code codes[ENOUGH];         /* space for code tables */
238*0a6a1f1dSLionel Sambuc };
239*0a6a1f1dSLionel Sambuc 
240*0a6a1f1dSLionel Sambuc voidpf zcalloc(voidpf /*opaque*/, unsigned /*items*/, unsigned /*size*/);
241*0a6a1f1dSLionel Sambuc void zcfree (voidpf /*opaque*/, voidpf /*ptr*/);
242*0a6a1f1dSLionel Sambuc int inflate_table(codetype /*type*/, unsigned short FAR */*lens*/, unsigned /*codes*/,
243*0a6a1f1dSLionel Sambuc 	code FAR * FAR */*table*/, unsigned FAR */*bits*/, unsigned short FAR */*work*/);
244*0a6a1f1dSLionel Sambuc void inflate_fast(z_streamp /*strm*/, unsigned /*start*/);
245*0a6a1f1dSLionel Sambuc 
246*0a6a1f1dSLionel Sambuc voidpf
zcalloc(voidpf opaque,unsigned items,unsigned size)247*0a6a1f1dSLionel Sambuc zcalloc (voidpf opaque, unsigned items, unsigned size)
248*0a6a1f1dSLionel Sambuc {
249*0a6a1f1dSLionel Sambuc     if (opaque) items += size - size; /* make compiler happy */
250*0a6a1f1dSLionel Sambuc     return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
251*0a6a1f1dSLionel Sambuc                               (voidpf)calloc(items, size);
252*0a6a1f1dSLionel Sambuc }
253*0a6a1f1dSLionel Sambuc 
254*0a6a1f1dSLionel Sambuc void
zcfree(voidpf opaque,voidpf ptr)255*0a6a1f1dSLionel Sambuc zcfree (voidpf opaque, voidpf ptr)
256*0a6a1f1dSLionel Sambuc {
257*0a6a1f1dSLionel Sambuc     free(ptr);
258*0a6a1f1dSLionel Sambuc     if (opaque) return; /* make compiler happy */
259*0a6a1f1dSLionel Sambuc }
260*0a6a1f1dSLionel Sambuc 
261*0a6a1f1dSLionel Sambuc /* function prototypes */
262*0a6a1f1dSLionel Sambuc local void fixedtables OF((struct inflate_state FAR *state));
263*0a6a1f1dSLionel Sambuc local int updatewindow OF((z_streamp strm, unsigned out));
264*0a6a1f1dSLionel Sambuc #ifdef BUILDFIXED
265*0a6a1f1dSLionel Sambuc    void makefixed OF((void));
266*0a6a1f1dSLionel Sambuc #endif
267*0a6a1f1dSLionel Sambuc local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
268*0a6a1f1dSLionel Sambuc                               unsigned len));
269*0a6a1f1dSLionel Sambuc 
inflateReset(z_streamp strm)270*0a6a1f1dSLionel Sambuc int ZEXPORT inflateReset(z_streamp strm)
271*0a6a1f1dSLionel Sambuc {
272*0a6a1f1dSLionel Sambuc     struct inflate_state FAR *state;
273*0a6a1f1dSLionel Sambuc 
274*0a6a1f1dSLionel Sambuc     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
275*0a6a1f1dSLionel Sambuc     state = (struct inflate_state FAR *)(void *)strm->state;
276*0a6a1f1dSLionel Sambuc     strm->total_in = strm->total_out = state->total = 0;
277*0a6a1f1dSLionel Sambuc     strm->msg = Z_NULL;
278*0a6a1f1dSLionel Sambuc     strm->adler = 1;        /* to support ill-conceived Java test suite */
279*0a6a1f1dSLionel Sambuc     state->mode = HEAD;
280*0a6a1f1dSLionel Sambuc     state->last = 0;
281*0a6a1f1dSLionel Sambuc     state->havedict = 0;
282*0a6a1f1dSLionel Sambuc     state->dmax = 32768U;
283*0a6a1f1dSLionel Sambuc     state->head = Z_NULL;
284*0a6a1f1dSLionel Sambuc     state->wsize = 0;
285*0a6a1f1dSLionel Sambuc     state->whave = 0;
286*0a6a1f1dSLionel Sambuc     state->write = 0;
287*0a6a1f1dSLionel Sambuc     state->hold = 0;
288*0a6a1f1dSLionel Sambuc     state->bits = 0;
289*0a6a1f1dSLionel Sambuc     state->lencode = state->distcode = state->next = state->codes;
290*0a6a1f1dSLionel Sambuc     Tracev((stderr, "inflate: reset\n"));
291*0a6a1f1dSLionel Sambuc     return Z_OK;
292*0a6a1f1dSLionel Sambuc }
293*0a6a1f1dSLionel Sambuc 
inflatePrime(z_streamp strm,int bits,int value)294*0a6a1f1dSLionel Sambuc int ZEXPORT inflatePrime(z_streamp strm, int bits, int value)
295*0a6a1f1dSLionel Sambuc {
296*0a6a1f1dSLionel Sambuc     struct inflate_state FAR *state;
297*0a6a1f1dSLionel Sambuc 
298*0a6a1f1dSLionel Sambuc     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
299*0a6a1f1dSLionel Sambuc     state = (struct inflate_state FAR *)(void *)strm->state;
300*0a6a1f1dSLionel Sambuc     if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
301*0a6a1f1dSLionel Sambuc     value &= (1L << bits) - 1;
302*0a6a1f1dSLionel Sambuc     state->hold += value << state->bits;
303*0a6a1f1dSLionel Sambuc     state->bits += bits;
304*0a6a1f1dSLionel Sambuc     return Z_OK;
305*0a6a1f1dSLionel Sambuc }
306*0a6a1f1dSLionel Sambuc 
inflateInit2_(z_streamp strm,int windowBits,const char * version,int stream_size)307*0a6a1f1dSLionel Sambuc int ZEXPORT inflateInit2_(z_streamp strm, int windowBits, const char *version, int stream_size)
308*0a6a1f1dSLionel Sambuc {
309*0a6a1f1dSLionel Sambuc     struct inflate_state FAR *state;
310*0a6a1f1dSLionel Sambuc 
311*0a6a1f1dSLionel Sambuc     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
312*0a6a1f1dSLionel Sambuc         stream_size != (int)(sizeof(z_stream)))
313*0a6a1f1dSLionel Sambuc         return Z_VERSION_ERROR;
314*0a6a1f1dSLionel Sambuc     if (strm == Z_NULL) return Z_STREAM_ERROR;
315*0a6a1f1dSLionel Sambuc     strm->msg = Z_NULL;                 /* in case we return an error */
316*0a6a1f1dSLionel Sambuc     if (strm->zalloc == (alloc_func)0) {
317*0a6a1f1dSLionel Sambuc         strm->zalloc = zcalloc;
318*0a6a1f1dSLionel Sambuc         strm->opaque = (voidpf)0;
319*0a6a1f1dSLionel Sambuc     }
320*0a6a1f1dSLionel Sambuc     if (strm->zfree == (free_func)0) strm->zfree = zcfree;
321*0a6a1f1dSLionel Sambuc     state = (struct inflate_state FAR *)
322*0a6a1f1dSLionel Sambuc             ZALLOC(strm, 1, sizeof(struct inflate_state));
323*0a6a1f1dSLionel Sambuc     if (state == Z_NULL) return Z_MEM_ERROR;
324*0a6a1f1dSLionel Sambuc     Tracev((stderr, "inflate: allocated\n"));
325*0a6a1f1dSLionel Sambuc     strm->state = (struct internal_state FAR *)(void *)state;
326*0a6a1f1dSLionel Sambuc     if (windowBits < 0) {
327*0a6a1f1dSLionel Sambuc         state->wrap = 0;
328*0a6a1f1dSLionel Sambuc         windowBits = -windowBits;
329*0a6a1f1dSLionel Sambuc     }
330*0a6a1f1dSLionel Sambuc     else {
331*0a6a1f1dSLionel Sambuc         state->wrap = (windowBits >> 4) + 1;
332*0a6a1f1dSLionel Sambuc #ifdef GUNZIP
333*0a6a1f1dSLionel Sambuc         if (windowBits < 48) windowBits &= 15;
334*0a6a1f1dSLionel Sambuc #endif
335*0a6a1f1dSLionel Sambuc     }
336*0a6a1f1dSLionel Sambuc     if (windowBits < 8 || windowBits > 15) {
337*0a6a1f1dSLionel Sambuc         ZFREE(strm, state);
338*0a6a1f1dSLionel Sambuc         strm->state = Z_NULL;
339*0a6a1f1dSLionel Sambuc         return Z_STREAM_ERROR;
340*0a6a1f1dSLionel Sambuc     }
341*0a6a1f1dSLionel Sambuc     state->wbits = (unsigned)windowBits;
342*0a6a1f1dSLionel Sambuc     state->window = Z_NULL;
343*0a6a1f1dSLionel Sambuc     return inflateReset(strm);
344*0a6a1f1dSLionel Sambuc }
345*0a6a1f1dSLionel Sambuc 
inflateInit_(z_streamp strm,const char * version,int stream_size)346*0a6a1f1dSLionel Sambuc int ZEXPORT inflateInit_( z_streamp strm, const char *version, int stream_size)
347*0a6a1f1dSLionel Sambuc {
348*0a6a1f1dSLionel Sambuc     return inflateInit2_(strm, DEF_WBITS, version, stream_size);
349*0a6a1f1dSLionel Sambuc }
350*0a6a1f1dSLionel Sambuc 
351*0a6a1f1dSLionel Sambuc /*
352*0a6a1f1dSLionel Sambuc    Return state with length and distance decoding tables and index sizes set to
353*0a6a1f1dSLionel Sambuc    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
354*0a6a1f1dSLionel Sambuc    If BUILDFIXED is defined, then instead this routine builds the tables the
355*0a6a1f1dSLionel Sambuc    first time it's called, and returns those tables the first time and
356*0a6a1f1dSLionel Sambuc    thereafter.  This reduces the size of the code by about 2K bytes, in
357*0a6a1f1dSLionel Sambuc    exchange for a little execution time.  However, BUILDFIXED should not be
358*0a6a1f1dSLionel Sambuc    used for threaded applications, since the rewriting of the tables and virgin
359*0a6a1f1dSLionel Sambuc    may not be thread-safe.
360*0a6a1f1dSLionel Sambuc  */
361*0a6a1f1dSLionel Sambuc local void
fixedtables(struct inflate_state FAR * state)362*0a6a1f1dSLionel Sambuc fixedtables(struct inflate_state FAR *state)
363*0a6a1f1dSLionel Sambuc {
364*0a6a1f1dSLionel Sambuc /*	$NetBSD: zlib.c,v 1.2 2015/02/05 01:26:54 agc Exp $	*/
365*0a6a1f1dSLionel Sambuc 
366*0a6a1f1dSLionel Sambuc     /* inffixed.h -- table for decoding fixed codes
367*0a6a1f1dSLionel Sambuc      * Generated automatically by makefixed().
368*0a6a1f1dSLionel Sambuc      */
369*0a6a1f1dSLionel Sambuc 
370*0a6a1f1dSLionel Sambuc     /* WARNING: this file should *not* be used by applications. It
371*0a6a1f1dSLionel Sambuc        is part of the implementation of the compression library and
372*0a6a1f1dSLionel Sambuc        is subject to change. Applications should only use zlib.h.
373*0a6a1f1dSLionel Sambuc      */
374*0a6a1f1dSLionel Sambuc 
375*0a6a1f1dSLionel Sambuc     static const code lenfix[512] = {
376*0a6a1f1dSLionel Sambuc         {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
377*0a6a1f1dSLionel Sambuc         {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
378*0a6a1f1dSLionel Sambuc         {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
379*0a6a1f1dSLionel Sambuc         {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
380*0a6a1f1dSLionel Sambuc         {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
381*0a6a1f1dSLionel Sambuc         {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
382*0a6a1f1dSLionel Sambuc         {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
383*0a6a1f1dSLionel Sambuc         {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
384*0a6a1f1dSLionel Sambuc         {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
385*0a6a1f1dSLionel Sambuc         {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
386*0a6a1f1dSLionel Sambuc         {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
387*0a6a1f1dSLionel Sambuc         {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
388*0a6a1f1dSLionel Sambuc         {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
389*0a6a1f1dSLionel Sambuc         {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
390*0a6a1f1dSLionel Sambuc         {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
391*0a6a1f1dSLionel Sambuc         {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
392*0a6a1f1dSLionel Sambuc         {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
393*0a6a1f1dSLionel Sambuc         {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
394*0a6a1f1dSLionel Sambuc         {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
395*0a6a1f1dSLionel Sambuc         {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
396*0a6a1f1dSLionel Sambuc         {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
397*0a6a1f1dSLionel Sambuc         {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
398*0a6a1f1dSLionel Sambuc         {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
399*0a6a1f1dSLionel Sambuc         {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
400*0a6a1f1dSLionel Sambuc         {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
401*0a6a1f1dSLionel Sambuc         {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
402*0a6a1f1dSLionel Sambuc         {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
403*0a6a1f1dSLionel Sambuc         {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
404*0a6a1f1dSLionel Sambuc         {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
405*0a6a1f1dSLionel Sambuc         {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
406*0a6a1f1dSLionel Sambuc         {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
407*0a6a1f1dSLionel Sambuc         {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
408*0a6a1f1dSLionel Sambuc         {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
409*0a6a1f1dSLionel Sambuc         {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
410*0a6a1f1dSLionel Sambuc         {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
411*0a6a1f1dSLionel Sambuc         {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
412*0a6a1f1dSLionel Sambuc         {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
413*0a6a1f1dSLionel Sambuc         {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
414*0a6a1f1dSLionel Sambuc         {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
415*0a6a1f1dSLionel Sambuc         {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
416*0a6a1f1dSLionel Sambuc         {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
417*0a6a1f1dSLionel Sambuc         {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
418*0a6a1f1dSLionel Sambuc         {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
419*0a6a1f1dSLionel Sambuc         {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
420*0a6a1f1dSLionel Sambuc         {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
421*0a6a1f1dSLionel Sambuc         {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
422*0a6a1f1dSLionel Sambuc         {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
423*0a6a1f1dSLionel Sambuc         {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
424*0a6a1f1dSLionel Sambuc         {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
425*0a6a1f1dSLionel Sambuc         {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
426*0a6a1f1dSLionel Sambuc         {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
427*0a6a1f1dSLionel Sambuc         {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
428*0a6a1f1dSLionel Sambuc         {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
429*0a6a1f1dSLionel Sambuc         {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
430*0a6a1f1dSLionel Sambuc         {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
431*0a6a1f1dSLionel Sambuc         {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
432*0a6a1f1dSLionel Sambuc         {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
433*0a6a1f1dSLionel Sambuc         {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
434*0a6a1f1dSLionel Sambuc         {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
435*0a6a1f1dSLionel Sambuc         {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
436*0a6a1f1dSLionel Sambuc         {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
437*0a6a1f1dSLionel Sambuc         {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
438*0a6a1f1dSLionel Sambuc         {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
439*0a6a1f1dSLionel Sambuc         {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
440*0a6a1f1dSLionel Sambuc         {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
441*0a6a1f1dSLionel Sambuc         {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
442*0a6a1f1dSLionel Sambuc         {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
443*0a6a1f1dSLionel Sambuc         {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
444*0a6a1f1dSLionel Sambuc         {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
445*0a6a1f1dSLionel Sambuc         {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
446*0a6a1f1dSLionel Sambuc         {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
447*0a6a1f1dSLionel Sambuc         {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
448*0a6a1f1dSLionel Sambuc         {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
449*0a6a1f1dSLionel Sambuc         {0,9,255}
450*0a6a1f1dSLionel Sambuc     };
451*0a6a1f1dSLionel Sambuc 
452*0a6a1f1dSLionel Sambuc     static const code distfix[32] = {
453*0a6a1f1dSLionel Sambuc         {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
454*0a6a1f1dSLionel Sambuc         {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
455*0a6a1f1dSLionel Sambuc         {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
456*0a6a1f1dSLionel Sambuc         {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
457*0a6a1f1dSLionel Sambuc         {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
458*0a6a1f1dSLionel Sambuc         {22,5,193},{64,5,0}
459*0a6a1f1dSLionel Sambuc     };
460*0a6a1f1dSLionel Sambuc     state->lencode = lenfix;
461*0a6a1f1dSLionel Sambuc     state->lenbits = 9;
462*0a6a1f1dSLionel Sambuc     state->distcode = distfix;
463*0a6a1f1dSLionel Sambuc     state->distbits = 5;
464*0a6a1f1dSLionel Sambuc }
465*0a6a1f1dSLionel Sambuc 
466*0a6a1f1dSLionel Sambuc /*
467*0a6a1f1dSLionel Sambuc    Update the window with the last wsize (normally 32K) bytes written before
468*0a6a1f1dSLionel Sambuc    returning.  If window does not exist yet, create it.  This is only called
469*0a6a1f1dSLionel Sambuc    when a window is already in use, or when output has been written during this
470*0a6a1f1dSLionel Sambuc    inflate call, but the end of the deflate stream has not been reached yet.
471*0a6a1f1dSLionel Sambuc    It is also called to create a window for dictionary data when a dictionary
472*0a6a1f1dSLionel Sambuc    is loaded.
473*0a6a1f1dSLionel Sambuc 
474*0a6a1f1dSLionel Sambuc    Providing output buffers larger than 32K to inflate() should provide a speed
475*0a6a1f1dSLionel Sambuc    advantage, since only the last 32K of output is copied to the sliding window
476*0a6a1f1dSLionel Sambuc    upon return from inflate(), and since all distances after the first 32K of
477*0a6a1f1dSLionel Sambuc    output will fall in the output data, making match copies simpler and faster.
478*0a6a1f1dSLionel Sambuc    The advantage may be dependent on the size of the processor's data caches.
479*0a6a1f1dSLionel Sambuc  */
480*0a6a1f1dSLionel Sambuc local int
updatewindow(z_streamp strm,unsigned out)481*0a6a1f1dSLionel Sambuc updatewindow(z_streamp strm, unsigned out)
482*0a6a1f1dSLionel Sambuc {
483*0a6a1f1dSLionel Sambuc     struct inflate_state FAR *state;
484*0a6a1f1dSLionel Sambuc     unsigned copy, dist;
485*0a6a1f1dSLionel Sambuc 
486*0a6a1f1dSLionel Sambuc     state = (struct inflate_state FAR *)(void *)strm->state;
487*0a6a1f1dSLionel Sambuc 
488*0a6a1f1dSLionel Sambuc     /* if it hasn't been done already, allocate space for the window */
489*0a6a1f1dSLionel Sambuc     if (state->window == Z_NULL) {
490*0a6a1f1dSLionel Sambuc         state->window = (unsigned char FAR *)
491*0a6a1f1dSLionel Sambuc                         ZALLOC(strm, 1U << state->wbits,
492*0a6a1f1dSLionel Sambuc                                sizeof(unsigned char));
493*0a6a1f1dSLionel Sambuc         if (state->window == Z_NULL) return 1;
494*0a6a1f1dSLionel Sambuc     }
495*0a6a1f1dSLionel Sambuc 
496*0a6a1f1dSLionel Sambuc     /* if window not in use yet, initialize */
497*0a6a1f1dSLionel Sambuc     if (state->wsize == 0) {
498*0a6a1f1dSLionel Sambuc         state->wsize = 1U << state->wbits;
499*0a6a1f1dSLionel Sambuc         state->write = 0;
500*0a6a1f1dSLionel Sambuc         state->whave = 0;
501*0a6a1f1dSLionel Sambuc     }
502*0a6a1f1dSLionel Sambuc 
503*0a6a1f1dSLionel Sambuc     /* copy state->wsize or less output bytes into the circular window */
504*0a6a1f1dSLionel Sambuc     copy = out - strm->avail_out;
505*0a6a1f1dSLionel Sambuc     if (copy >= state->wsize) {
506*0a6a1f1dSLionel Sambuc         zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
507*0a6a1f1dSLionel Sambuc         state->write = 0;
508*0a6a1f1dSLionel Sambuc         state->whave = state->wsize;
509*0a6a1f1dSLionel Sambuc     }
510*0a6a1f1dSLionel Sambuc     else {
511*0a6a1f1dSLionel Sambuc         dist = state->wsize - state->write;
512*0a6a1f1dSLionel Sambuc         if (dist > copy) dist = copy;
513*0a6a1f1dSLionel Sambuc         zmemcpy(state->window + state->write, strm->next_out - copy, dist);
514*0a6a1f1dSLionel Sambuc         copy -= dist;
515*0a6a1f1dSLionel Sambuc         if (copy) {
516*0a6a1f1dSLionel Sambuc             zmemcpy(state->window, strm->next_out - copy, copy);
517*0a6a1f1dSLionel Sambuc             state->write = copy;
518*0a6a1f1dSLionel Sambuc             state->whave = state->wsize;
519*0a6a1f1dSLionel Sambuc         }
520*0a6a1f1dSLionel Sambuc         else {
521*0a6a1f1dSLionel Sambuc             state->write += dist;
522*0a6a1f1dSLionel Sambuc             if (state->write == state->wsize) state->write = 0;
523*0a6a1f1dSLionel Sambuc             if (state->whave < state->wsize) state->whave += dist;
524*0a6a1f1dSLionel Sambuc         }
525*0a6a1f1dSLionel Sambuc     }
526*0a6a1f1dSLionel Sambuc     return 0;
527*0a6a1f1dSLionel Sambuc }
528*0a6a1f1dSLionel Sambuc 
529*0a6a1f1dSLionel Sambuc #define BASE 65521UL    /* largest prime smaller than 65536 */
530*0a6a1f1dSLionel Sambuc #define NMAX 5552
531*0a6a1f1dSLionel Sambuc /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
532*0a6a1f1dSLionel Sambuc 
533*0a6a1f1dSLionel Sambuc #define DO1(buf,i)  {adler += (buf)[i]; sum2 += adler;}
534*0a6a1f1dSLionel Sambuc #define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
535*0a6a1f1dSLionel Sambuc #define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
536*0a6a1f1dSLionel Sambuc #define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
537*0a6a1f1dSLionel Sambuc #define DO16(buf)   DO8(buf,0); DO8(buf,8);
538*0a6a1f1dSLionel Sambuc 
539*0a6a1f1dSLionel Sambuc /* use NO_DIVIDE if your processor does not do division in hardware */
540*0a6a1f1dSLionel Sambuc #ifdef NO_DIVIDE
541*0a6a1f1dSLionel Sambuc #  define MOD(a) \
542*0a6a1f1dSLionel Sambuc     do { \
543*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 16)) a -= (BASE << 16); \
544*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 15)) a -= (BASE << 15); \
545*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 14)) a -= (BASE << 14); \
546*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 13)) a -= (BASE << 13); \
547*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 12)) a -= (BASE << 12); \
548*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 11)) a -= (BASE << 11); \
549*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 10)) a -= (BASE << 10); \
550*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 9)) a -= (BASE << 9); \
551*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 8)) a -= (BASE << 8); \
552*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 7)) a -= (BASE << 7); \
553*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 6)) a -= (BASE << 6); \
554*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 5)) a -= (BASE << 5); \
555*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 4)) a -= (BASE << 4); \
556*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 3)) a -= (BASE << 3); \
557*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 2)) a -= (BASE << 2); \
558*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 1)) a -= (BASE << 1); \
559*0a6a1f1dSLionel Sambuc         if (a >= BASE) a -= BASE; \
560*0a6a1f1dSLionel Sambuc     } while (0)
561*0a6a1f1dSLionel Sambuc #  define MOD4(a) \
562*0a6a1f1dSLionel Sambuc     do { \
563*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 4)) a -= (BASE << 4); \
564*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 3)) a -= (BASE << 3); \
565*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 2)) a -= (BASE << 2); \
566*0a6a1f1dSLionel Sambuc         if (a >= (BASE << 1)) a -= (BASE << 1); \
567*0a6a1f1dSLionel Sambuc         if (a >= BASE) a -= BASE; \
568*0a6a1f1dSLionel Sambuc     } while (0)
569*0a6a1f1dSLionel Sambuc #else
570*0a6a1f1dSLionel Sambuc #  define MOD(a) a %= BASE
571*0a6a1f1dSLionel Sambuc #  define MOD4(a) a %= BASE
572*0a6a1f1dSLionel Sambuc #endif
573*0a6a1f1dSLionel Sambuc 
574*0a6a1f1dSLionel Sambuc /* ========================================================================= */
575*0a6a1f1dSLionel Sambuc uLong ZEXPORT
adler32(uLong adler,const Bytef * buf,uInt len)576*0a6a1f1dSLionel Sambuc adler32(uLong adler, const Bytef *buf, uInt len)
577*0a6a1f1dSLionel Sambuc {
578*0a6a1f1dSLionel Sambuc     unsigned long sum2;
579*0a6a1f1dSLionel Sambuc     unsigned n;
580*0a6a1f1dSLionel Sambuc 
581*0a6a1f1dSLionel Sambuc     /* split Adler-32 into component sums */
582*0a6a1f1dSLionel Sambuc     sum2 = (adler >> 16) & 0xffff;
583*0a6a1f1dSLionel Sambuc     adler &= 0xffff;
584*0a6a1f1dSLionel Sambuc 
585*0a6a1f1dSLionel Sambuc     /* in case user likes doing a byte at a time, keep it fast */
586*0a6a1f1dSLionel Sambuc     if (len == 1) {
587*0a6a1f1dSLionel Sambuc         adler += buf[0];
588*0a6a1f1dSLionel Sambuc         if (adler >= BASE)
589*0a6a1f1dSLionel Sambuc             adler -= BASE;
590*0a6a1f1dSLionel Sambuc         sum2 += adler;
591*0a6a1f1dSLionel Sambuc         if (sum2 >= BASE)
592*0a6a1f1dSLionel Sambuc             sum2 -= BASE;
593*0a6a1f1dSLionel Sambuc         return adler | (sum2 << 16);
594*0a6a1f1dSLionel Sambuc     }
595*0a6a1f1dSLionel Sambuc 
596*0a6a1f1dSLionel Sambuc     /* initial Adler-32 value (deferred check for len == 1 speed) */
597*0a6a1f1dSLionel Sambuc     if (buf == Z_NULL)
598*0a6a1f1dSLionel Sambuc         return 1L;
599*0a6a1f1dSLionel Sambuc 
600*0a6a1f1dSLionel Sambuc     /* in case short lengths are provided, keep it somewhat fast */
601*0a6a1f1dSLionel Sambuc     if (len < 16) {
602*0a6a1f1dSLionel Sambuc         while (len--) {
603*0a6a1f1dSLionel Sambuc             adler += *buf++;
604*0a6a1f1dSLionel Sambuc             sum2 += adler;
605*0a6a1f1dSLionel Sambuc         }
606*0a6a1f1dSLionel Sambuc         if (adler >= BASE)
607*0a6a1f1dSLionel Sambuc             adler -= BASE;
608*0a6a1f1dSLionel Sambuc         MOD4(sum2);             /* only added so many BASE's */
609*0a6a1f1dSLionel Sambuc         return adler | (sum2 << 16);
610*0a6a1f1dSLionel Sambuc     }
611*0a6a1f1dSLionel Sambuc 
612*0a6a1f1dSLionel Sambuc     /* do length NMAX blocks -- requires just one modulo operation */
613*0a6a1f1dSLionel Sambuc     while (len >= NMAX) {
614*0a6a1f1dSLionel Sambuc         len -= NMAX;
615*0a6a1f1dSLionel Sambuc         n = NMAX / 16;          /* NMAX is divisible by 16 */
616*0a6a1f1dSLionel Sambuc         do {
617*0a6a1f1dSLionel Sambuc             DO16(buf);          /* 16 sums unrolled */
618*0a6a1f1dSLionel Sambuc             buf += 16;
619*0a6a1f1dSLionel Sambuc         } while (--n);
620*0a6a1f1dSLionel Sambuc         MOD(adler);
621*0a6a1f1dSLionel Sambuc         MOD(sum2);
622*0a6a1f1dSLionel Sambuc     }
623*0a6a1f1dSLionel Sambuc 
624*0a6a1f1dSLionel Sambuc     /* do remaining bytes (less than NMAX, still just one modulo) */
625*0a6a1f1dSLionel Sambuc     if (len) {                  /* avoid modulos if none remaining */
626*0a6a1f1dSLionel Sambuc         while (len >= 16) {
627*0a6a1f1dSLionel Sambuc             len -= 16;
628*0a6a1f1dSLionel Sambuc             DO16(buf);
629*0a6a1f1dSLionel Sambuc             buf += 16;
630*0a6a1f1dSLionel Sambuc         }
631*0a6a1f1dSLionel Sambuc         while (len--) {
632*0a6a1f1dSLionel Sambuc             adler += *buf++;
633*0a6a1f1dSLionel Sambuc             sum2 += adler;
634*0a6a1f1dSLionel Sambuc         }
635*0a6a1f1dSLionel Sambuc         MOD(adler);
636*0a6a1f1dSLionel Sambuc         MOD(sum2);
637*0a6a1f1dSLionel Sambuc     }
638*0a6a1f1dSLionel Sambuc 
639*0a6a1f1dSLionel Sambuc     /* return recombined sums */
640*0a6a1f1dSLionel Sambuc     return adler | (sum2 << 16);
641*0a6a1f1dSLionel Sambuc }
642*0a6a1f1dSLionel Sambuc 
643*0a6a1f1dSLionel Sambuc /* Macros for inflate(): */
644*0a6a1f1dSLionel Sambuc 
645*0a6a1f1dSLionel Sambuc /* check function to use adler32() for zlib or crc32() for gzip */
646*0a6a1f1dSLionel Sambuc #ifdef GUNZIP
647*0a6a1f1dSLionel Sambuc #  define UPDATE(check, buf, len) \
648*0a6a1f1dSLionel Sambuc     (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
649*0a6a1f1dSLionel Sambuc #else
650*0a6a1f1dSLionel Sambuc #  define UPDATE(check, buf, len) adler32(check, buf, len)
651*0a6a1f1dSLionel Sambuc #endif
652*0a6a1f1dSLionel Sambuc 
653*0a6a1f1dSLionel Sambuc /* check macros for header crc */
654*0a6a1f1dSLionel Sambuc #ifdef GUNZIP
655*0a6a1f1dSLionel Sambuc #  define CRC2(check, word) \
656*0a6a1f1dSLionel Sambuc     do { \
657*0a6a1f1dSLionel Sambuc         hbuf[0] = (unsigned char)(word); \
658*0a6a1f1dSLionel Sambuc         hbuf[1] = (unsigned char)((word) >> 8); \
659*0a6a1f1dSLionel Sambuc         check = crc32(check, hbuf, 2); \
660*0a6a1f1dSLionel Sambuc     } while (0)
661*0a6a1f1dSLionel Sambuc 
662*0a6a1f1dSLionel Sambuc #  define CRC4(check, word) \
663*0a6a1f1dSLionel Sambuc     do { \
664*0a6a1f1dSLionel Sambuc         hbuf[0] = (unsigned char)(word); \
665*0a6a1f1dSLionel Sambuc         hbuf[1] = (unsigned char)((word) >> 8); \
666*0a6a1f1dSLionel Sambuc         hbuf[2] = (unsigned char)((word) >> 16); \
667*0a6a1f1dSLionel Sambuc         hbuf[3] = (unsigned char)((word) >> 24); \
668*0a6a1f1dSLionel Sambuc         check = crc32(check, hbuf, 4); \
669*0a6a1f1dSLionel Sambuc     } while (0)
670*0a6a1f1dSLionel Sambuc #endif
671*0a6a1f1dSLionel Sambuc 
672*0a6a1f1dSLionel Sambuc /* Load registers with state in inflate() for speed */
673*0a6a1f1dSLionel Sambuc #define LOAD() \
674*0a6a1f1dSLionel Sambuc     do { \
675*0a6a1f1dSLionel Sambuc         put = strm->next_out; \
676*0a6a1f1dSLionel Sambuc         left = strm->avail_out; \
677*0a6a1f1dSLionel Sambuc         next = strm->next_in; \
678*0a6a1f1dSLionel Sambuc         have = strm->avail_in; \
679*0a6a1f1dSLionel Sambuc         hold = state->hold; \
680*0a6a1f1dSLionel Sambuc         bits = state->bits; \
681*0a6a1f1dSLionel Sambuc     } while (0)
682*0a6a1f1dSLionel Sambuc 
683*0a6a1f1dSLionel Sambuc /* Restore state from registers in inflate() */
684*0a6a1f1dSLionel Sambuc #define RESTORE() \
685*0a6a1f1dSLionel Sambuc     do { \
686*0a6a1f1dSLionel Sambuc         strm->next_out = put; \
687*0a6a1f1dSLionel Sambuc         strm->avail_out = left; \
688*0a6a1f1dSLionel Sambuc         strm->next_in = next; \
689*0a6a1f1dSLionel Sambuc         strm->avail_in = have; \
690*0a6a1f1dSLionel Sambuc         state->hold = hold; \
691*0a6a1f1dSLionel Sambuc         state->bits = bits; \
692*0a6a1f1dSLionel Sambuc     } while (0)
693*0a6a1f1dSLionel Sambuc 
694*0a6a1f1dSLionel Sambuc /* Clear the input bit accumulator */
695*0a6a1f1dSLionel Sambuc #define INITBITS() \
696*0a6a1f1dSLionel Sambuc     do { \
697*0a6a1f1dSLionel Sambuc         hold = 0; \
698*0a6a1f1dSLionel Sambuc         bits = 0; \
699*0a6a1f1dSLionel Sambuc     } while (0)
700*0a6a1f1dSLionel Sambuc 
701*0a6a1f1dSLionel Sambuc /* Get a byte of input into the bit accumulator, or return from inflate()
702*0a6a1f1dSLionel Sambuc    if there is no input available. */
703*0a6a1f1dSLionel Sambuc #define PULLBYTE() \
704*0a6a1f1dSLionel Sambuc     do { \
705*0a6a1f1dSLionel Sambuc         if (have == 0) goto inf_leave; \
706*0a6a1f1dSLionel Sambuc         have--; \
707*0a6a1f1dSLionel Sambuc         hold += (unsigned long)(*next++) << bits; \
708*0a6a1f1dSLionel Sambuc         bits += 8; \
709*0a6a1f1dSLionel Sambuc     } while (0)
710*0a6a1f1dSLionel Sambuc 
711*0a6a1f1dSLionel Sambuc /* Assure that there are at least n bits in the bit accumulator.  If there is
712*0a6a1f1dSLionel Sambuc    not enough available input to do that, then return from inflate(). */
713*0a6a1f1dSLionel Sambuc #define NEEDBITS(n) \
714*0a6a1f1dSLionel Sambuc     do { \
715*0a6a1f1dSLionel Sambuc         while (bits < (unsigned)(n)) \
716*0a6a1f1dSLionel Sambuc             PULLBYTE(); \
717*0a6a1f1dSLionel Sambuc     } while (0)
718*0a6a1f1dSLionel Sambuc 
719*0a6a1f1dSLionel Sambuc /* Return the low n bits of the bit accumulator (n < 16) */
720*0a6a1f1dSLionel Sambuc #define BITS(n) \
721*0a6a1f1dSLionel Sambuc     ((unsigned)hold & ((1U << (n)) - 1))
722*0a6a1f1dSLionel Sambuc 
723*0a6a1f1dSLionel Sambuc /* Remove n bits from the bit accumulator */
724*0a6a1f1dSLionel Sambuc #define DROPBITS(n) \
725*0a6a1f1dSLionel Sambuc     do { \
726*0a6a1f1dSLionel Sambuc         hold >>= (n); \
727*0a6a1f1dSLionel Sambuc         bits -= (unsigned)(n); \
728*0a6a1f1dSLionel Sambuc     } while (0)
729*0a6a1f1dSLionel Sambuc 
730*0a6a1f1dSLionel Sambuc /* Remove zero to seven bits as needed to go to a byte boundary */
731*0a6a1f1dSLionel Sambuc #define BYTEBITS() \
732*0a6a1f1dSLionel Sambuc     do { \
733*0a6a1f1dSLionel Sambuc         hold >>= bits & 7; \
734*0a6a1f1dSLionel Sambuc         bits -= bits & 7; \
735*0a6a1f1dSLionel Sambuc     } while (0)
736*0a6a1f1dSLionel Sambuc 
737*0a6a1f1dSLionel Sambuc /* Reverse the bytes in a 32-bit value */
738*0a6a1f1dSLionel Sambuc #define REVERSE(q) \
739*0a6a1f1dSLionel Sambuc     ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
740*0a6a1f1dSLionel Sambuc      (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
741*0a6a1f1dSLionel Sambuc 
742*0a6a1f1dSLionel Sambuc #define MAXBITS 15
743*0a6a1f1dSLionel Sambuc #define MAXD 592
744*0a6a1f1dSLionel Sambuc 
745*0a6a1f1dSLionel Sambuc /*
746*0a6a1f1dSLionel Sambuc   If you use the zlib library in a product, an acknowledgment is welcome
747*0a6a1f1dSLionel Sambuc   in the documentation of your product. If for some reason you cannot
748*0a6a1f1dSLionel Sambuc   include such an acknowledgment, I would appreciate that you keep this
749*0a6a1f1dSLionel Sambuc   copyright string in the executable of your product.
750*0a6a1f1dSLionel Sambuc  */
751*0a6a1f1dSLionel Sambuc 
752*0a6a1f1dSLionel Sambuc /*
753*0a6a1f1dSLionel Sambuc    Build a set of tables to decode the provided canonical Huffman code.
754*0a6a1f1dSLionel Sambuc    The code lengths are lens[0..codes-1].  The result starts at *table,
755*0a6a1f1dSLionel Sambuc    whose indices are 0..2^bits-1.  work is a writable array of at least
756*0a6a1f1dSLionel Sambuc    lens shorts, which is used as a work area.  type is the type of code
757*0a6a1f1dSLionel Sambuc    to be generated, CODES, LENS, or DISTS.  On return, zero is success,
758*0a6a1f1dSLionel Sambuc    -1 is an invalid code, and +1 means that ENOUGH isn't enough.  table
759*0a6a1f1dSLionel Sambuc    on return points to the next available entry's address.  bits is the
760*0a6a1f1dSLionel Sambuc    requested root table index bits, and on return it is the actual root
761*0a6a1f1dSLionel Sambuc    table index bits.  It will differ if the request is greater than the
762*0a6a1f1dSLionel Sambuc    longest code or if it is less than the shortest code.
763*0a6a1f1dSLionel Sambuc  */
764*0a6a1f1dSLionel Sambuc int
inflate_table(codetype type,unsigned short FAR * lens,unsigned codes,code FAR * FAR * table,unsigned FAR * bits,unsigned short FAR * work)765*0a6a1f1dSLionel Sambuc inflate_table(codetype type, unsigned short FAR *lens, unsigned codes,
766*0a6a1f1dSLionel Sambuc 	code FAR * FAR *table, unsigned FAR *bits, unsigned short FAR *work)
767*0a6a1f1dSLionel Sambuc {
768*0a6a1f1dSLionel Sambuc     unsigned len;               /* a code's length in bits */
769*0a6a1f1dSLionel Sambuc     unsigned sym;               /* index of code symbols */
770*0a6a1f1dSLionel Sambuc     unsigned mmin, mmax;        /* minimum and maximum code lengths */
771*0a6a1f1dSLionel Sambuc     unsigned root;              /* number of index bits for root table */
772*0a6a1f1dSLionel Sambuc     unsigned curr;              /* number of index bits for current table */
773*0a6a1f1dSLionel Sambuc     unsigned drop;              /* code bits to drop for sub-table */
774*0a6a1f1dSLionel Sambuc     int left;                   /* number of prefix codes available */
775*0a6a1f1dSLionel Sambuc     unsigned used;              /* code entries in table used */
776*0a6a1f1dSLionel Sambuc     unsigned huff;              /* Huffman code */
777*0a6a1f1dSLionel Sambuc     unsigned incr;              /* for incrementing code, index */
778*0a6a1f1dSLionel Sambuc     unsigned fill;              /* index for replicating entries */
779*0a6a1f1dSLionel Sambuc     unsigned low;               /* low bits for current root entry */
780*0a6a1f1dSLionel Sambuc     unsigned mask;              /* mask for low root bits */
781*0a6a1f1dSLionel Sambuc     code this;                  /* table entry for duplication */
782*0a6a1f1dSLionel Sambuc     code FAR *next;             /* next available space in table */
783*0a6a1f1dSLionel Sambuc     const unsigned short FAR *base;     /* base value table to use */
784*0a6a1f1dSLionel Sambuc     const unsigned short FAR *extra;    /* extra bits table to use */
785*0a6a1f1dSLionel Sambuc     int end;                    /* use base and extra for symbol > end */
786*0a6a1f1dSLionel Sambuc     unsigned short count[MAXBITS+1];    /* number of codes of each length */
787*0a6a1f1dSLionel Sambuc     unsigned short offs[MAXBITS+1];     /* offsets in table for each length */
788*0a6a1f1dSLionel Sambuc     static const unsigned short lbase[31] = { /* Length codes 257..285 base */
789*0a6a1f1dSLionel Sambuc         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
790*0a6a1f1dSLionel Sambuc         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
791*0a6a1f1dSLionel Sambuc     static const unsigned short lext[31] = { /* Length codes 257..285 extra */
792*0a6a1f1dSLionel Sambuc         16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
793*0a6a1f1dSLionel Sambuc         19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
794*0a6a1f1dSLionel Sambuc     static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
795*0a6a1f1dSLionel Sambuc         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
796*0a6a1f1dSLionel Sambuc         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
797*0a6a1f1dSLionel Sambuc         8193, 12289, 16385, 24577, 0, 0};
798*0a6a1f1dSLionel Sambuc     static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
799*0a6a1f1dSLionel Sambuc         16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
800*0a6a1f1dSLionel Sambuc         23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
801*0a6a1f1dSLionel Sambuc         28, 28, 29, 29, 64, 64};
802*0a6a1f1dSLionel Sambuc 
803*0a6a1f1dSLionel Sambuc     /*
804*0a6a1f1dSLionel Sambuc        Process a set of code lengths to create a canonical Huffman code.  The
805*0a6a1f1dSLionel Sambuc        code lengths are lens[0..codes-1].  Each length corresponds to the
806*0a6a1f1dSLionel Sambuc        symbols 0..codes-1.  The Huffman code is generated by first sorting the
807*0a6a1f1dSLionel Sambuc        symbols by length from short to long, and retaining the symbol order
808*0a6a1f1dSLionel Sambuc        for codes with equal lengths.  Then the code starts with all zero bits
809*0a6a1f1dSLionel Sambuc        for the first code of the shortest length, and the codes are integer
810*0a6a1f1dSLionel Sambuc        increments for the same length, and zeros are appended as the length
811*0a6a1f1dSLionel Sambuc        increases.  For the deflate format, these bits are stored backwards
812*0a6a1f1dSLionel Sambuc        from their more natural integer increment ordering, and so when the
813*0a6a1f1dSLionel Sambuc        decoding tables are built in the large loop below, the integer codes
814*0a6a1f1dSLionel Sambuc        are incremented backwards.
815*0a6a1f1dSLionel Sambuc 
816*0a6a1f1dSLionel Sambuc        This routine assumes, but does not check, that all of the entries in
817*0a6a1f1dSLionel Sambuc        lens[] are in the range 0..MAXBITS.  The caller must assure this.
818*0a6a1f1dSLionel Sambuc        1..MAXBITS is interpreted as that code length.  zero means that that
819*0a6a1f1dSLionel Sambuc        symbol does not occur in this code.
820*0a6a1f1dSLionel Sambuc 
821*0a6a1f1dSLionel Sambuc        The codes are sorted by computing a count of codes for each length,
822*0a6a1f1dSLionel Sambuc        creating from that a table of starting indices for each length in the
823*0a6a1f1dSLionel Sambuc        sorted table, and then entering the symbols in order in the sorted
824*0a6a1f1dSLionel Sambuc        table.  The sorted table is work[], with that space being provided by
825*0a6a1f1dSLionel Sambuc        the caller.
826*0a6a1f1dSLionel Sambuc 
827*0a6a1f1dSLionel Sambuc        The length counts are used for other purposes as well, i.e. finding
828*0a6a1f1dSLionel Sambuc        the minimum and maximum length codes, determining if there are any
829*0a6a1f1dSLionel Sambuc        codes at all, checking for a valid set of lengths, and looking ahead
830*0a6a1f1dSLionel Sambuc        at length counts to determine sub-table sizes when building the
831*0a6a1f1dSLionel Sambuc        decoding tables.
832*0a6a1f1dSLionel Sambuc      */
833*0a6a1f1dSLionel Sambuc 
834*0a6a1f1dSLionel Sambuc     /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
835*0a6a1f1dSLionel Sambuc     for (len = 0; len <= MAXBITS; len++)
836*0a6a1f1dSLionel Sambuc         count[len] = 0;
837*0a6a1f1dSLionel Sambuc     for (sym = 0; sym < codes; sym++)
838*0a6a1f1dSLionel Sambuc         count[lens[sym]]++;
839*0a6a1f1dSLionel Sambuc 
840*0a6a1f1dSLionel Sambuc     /* bound code lengths, force root to be within code lengths */
841*0a6a1f1dSLionel Sambuc     root = *bits;
842*0a6a1f1dSLionel Sambuc     for (mmax = MAXBITS; mmax >= 1; mmax--)
843*0a6a1f1dSLionel Sambuc         if (count[mmax] != 0) break;
844*0a6a1f1dSLionel Sambuc     if (root > mmax) root = mmax;
845*0a6a1f1dSLionel Sambuc     if (mmax == 0) {                     /* no symbols to code at all */
846*0a6a1f1dSLionel Sambuc         this.op = (unsigned char)64;    /* invalid code marker */
847*0a6a1f1dSLionel Sambuc         this.bits = (unsigned char)1;
848*0a6a1f1dSLionel Sambuc         this.val = (unsigned short)0;
849*0a6a1f1dSLionel Sambuc         *(*table)++ = this;             /* make a table to force an error */
850*0a6a1f1dSLionel Sambuc         *(*table)++ = this;
851*0a6a1f1dSLionel Sambuc         *bits = 1;
852*0a6a1f1dSLionel Sambuc         return 0;     /* no symbols, but wait for decoding to report error */
853*0a6a1f1dSLionel Sambuc     }
854*0a6a1f1dSLionel Sambuc     for (mmin = 1; mmin <= MAXBITS; mmin++)
855*0a6a1f1dSLionel Sambuc         if (count[mmin] != 0) break;
856*0a6a1f1dSLionel Sambuc     if (root < mmin) root = mmin;
857*0a6a1f1dSLionel Sambuc 
858*0a6a1f1dSLionel Sambuc     /* check for an over-subscribed or incomplete set of lengths */
859*0a6a1f1dSLionel Sambuc     left = 1;
860*0a6a1f1dSLionel Sambuc     for (len = 1; len <= MAXBITS; len++) {
861*0a6a1f1dSLionel Sambuc         left <<= 1;
862*0a6a1f1dSLionel Sambuc         left -= count[len];
863*0a6a1f1dSLionel Sambuc         if (left < 0) return -1;        /* over-subscribed */
864*0a6a1f1dSLionel Sambuc     }
865*0a6a1f1dSLionel Sambuc     if (left > 0 && (type == CODES || mmax != 1))
866*0a6a1f1dSLionel Sambuc         return -1;                      /* incomplete set */
867*0a6a1f1dSLionel Sambuc 
868*0a6a1f1dSLionel Sambuc     /* generate offsets into symbol table for each length for sorting */
869*0a6a1f1dSLionel Sambuc     offs[1] = 0;
870*0a6a1f1dSLionel Sambuc     for (len = 1; len < MAXBITS; len++)
871*0a6a1f1dSLionel Sambuc         offs[len + 1] = offs[len] + count[len];
872*0a6a1f1dSLionel Sambuc 
873*0a6a1f1dSLionel Sambuc     /* sort symbols by length, by symbol order within each length */
874*0a6a1f1dSLionel Sambuc     for (sym = 0; sym < codes; sym++)
875*0a6a1f1dSLionel Sambuc         if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
876*0a6a1f1dSLionel Sambuc 
877*0a6a1f1dSLionel Sambuc     /*
878*0a6a1f1dSLionel Sambuc        Create and fill in decoding tables.  In this loop, the table being
879*0a6a1f1dSLionel Sambuc        filled is at next and has curr index bits.  The code being used is huff
880*0a6a1f1dSLionel Sambuc        with length len.  That code is converted to an index by dropping drop
881*0a6a1f1dSLionel Sambuc        bits off of the bottom.  For codes where len is less than drop + curr,
882*0a6a1f1dSLionel Sambuc        those top drop + curr - len bits are incremented through all values to
883*0a6a1f1dSLionel Sambuc        fill the table with replicated entries.
884*0a6a1f1dSLionel Sambuc 
885*0a6a1f1dSLionel Sambuc        root is the number of index bits for the root table.  When len exceeds
886*0a6a1f1dSLionel Sambuc        root, sub-tables are created pointed to by the root entry with an index
887*0a6a1f1dSLionel Sambuc        of the low root bits of huff.  This is saved in low to check for when a
888*0a6a1f1dSLionel Sambuc        new sub-table should be started.  drop is zero when the root table is
889*0a6a1f1dSLionel Sambuc        being filled, and drop is root when sub-tables are being filled.
890*0a6a1f1dSLionel Sambuc 
891*0a6a1f1dSLionel Sambuc        When a new sub-table is needed, it is necessary to look ahead in the
892*0a6a1f1dSLionel Sambuc        code lengths to determine what size sub-table is needed.  The length
893*0a6a1f1dSLionel Sambuc        counts are used for this, and so count[] is decremented as codes are
894*0a6a1f1dSLionel Sambuc        entered in the tables.
895*0a6a1f1dSLionel Sambuc 
896*0a6a1f1dSLionel Sambuc        used keeps track of how many table entries have been allocated from the
897*0a6a1f1dSLionel Sambuc        provided *table space.  It is checked when a LENS table is being made
898*0a6a1f1dSLionel Sambuc        against the space in *table, ENOUGH, minus the maximum space needed by
899*0a6a1f1dSLionel Sambuc        the worst case distance code, MAXD.  This should never happen, but the
900*0a6a1f1dSLionel Sambuc        sufficiency of ENOUGH has not been proven exhaustively, hence the check.
901*0a6a1f1dSLionel Sambuc        This assumes that when type == LENS, bits == 9.
902*0a6a1f1dSLionel Sambuc 
903*0a6a1f1dSLionel Sambuc        sym increments through all symbols, and the loop terminates when
904*0a6a1f1dSLionel Sambuc        all codes of length mmax, i.e. all codes, have been processed.  This
905*0a6a1f1dSLionel Sambuc        routine permits incomplete codes, so another loop after this one fills
906*0a6a1f1dSLionel Sambuc        in the rest of the decoding tables with invalid code markers.
907*0a6a1f1dSLionel Sambuc      */
908*0a6a1f1dSLionel Sambuc 
909*0a6a1f1dSLionel Sambuc     /* set up for code type */
910*0a6a1f1dSLionel Sambuc     switch (type) {
911*0a6a1f1dSLionel Sambuc     case CODES:
912*0a6a1f1dSLionel Sambuc         base = extra = work;    /* dummy value--not used */
913*0a6a1f1dSLionel Sambuc         end = 19;
914*0a6a1f1dSLionel Sambuc         break;
915*0a6a1f1dSLionel Sambuc     case LENS:
916*0a6a1f1dSLionel Sambuc         base = lbase;
917*0a6a1f1dSLionel Sambuc         base -= 257;
918*0a6a1f1dSLionel Sambuc         extra = lext;
919*0a6a1f1dSLionel Sambuc         extra -= 257;
920*0a6a1f1dSLionel Sambuc         end = 256;
921*0a6a1f1dSLionel Sambuc         break;
922*0a6a1f1dSLionel Sambuc     default:            /* DISTS */
923*0a6a1f1dSLionel Sambuc         base = dbase;
924*0a6a1f1dSLionel Sambuc         extra = dext;
925*0a6a1f1dSLionel Sambuc         end = -1;
926*0a6a1f1dSLionel Sambuc     }
927*0a6a1f1dSLionel Sambuc 
928*0a6a1f1dSLionel Sambuc     /* initialize state for loop */
929*0a6a1f1dSLionel Sambuc     huff = 0;                   /* starting code */
930*0a6a1f1dSLionel Sambuc     sym = 0;                    /* starting code symbol */
931*0a6a1f1dSLionel Sambuc     len = mmin;                  /* starting code length */
932*0a6a1f1dSLionel Sambuc     next = *table;              /* current table to fill in */
933*0a6a1f1dSLionel Sambuc     curr = root;                /* current table index bits */
934*0a6a1f1dSLionel Sambuc     drop = 0;                   /* current bits to drop from code for index */
935*0a6a1f1dSLionel Sambuc     low = (unsigned)(-1);       /* trigger new sub-table when len > root */
936*0a6a1f1dSLionel Sambuc     used = 1U << root;          /* use root table entries */
937*0a6a1f1dSLionel Sambuc     mask = used - 1;            /* mask for comparing low */
938*0a6a1f1dSLionel Sambuc 
939*0a6a1f1dSLionel Sambuc     /* check available table space */
940*0a6a1f1dSLionel Sambuc     if (type == LENS && used >= ENOUGH - MAXD)
941*0a6a1f1dSLionel Sambuc         return 1;
942*0a6a1f1dSLionel Sambuc 
943*0a6a1f1dSLionel Sambuc     /* process all codes and make table entries */
944*0a6a1f1dSLionel Sambuc     for (;;) {
945*0a6a1f1dSLionel Sambuc         /* create table entry */
946*0a6a1f1dSLionel Sambuc         this.bits = (unsigned char)(len - drop);
947*0a6a1f1dSLionel Sambuc         if ((int)(work[sym]) < end) {
948*0a6a1f1dSLionel Sambuc             this.op = (unsigned char)0;
949*0a6a1f1dSLionel Sambuc             this.val = work[sym];
950*0a6a1f1dSLionel Sambuc         }
951*0a6a1f1dSLionel Sambuc         else if ((int)(work[sym]) > end) {
952*0a6a1f1dSLionel Sambuc             this.op = (unsigned char)(extra[work[sym]]);
953*0a6a1f1dSLionel Sambuc             this.val = base[work[sym]];
954*0a6a1f1dSLionel Sambuc         }
955*0a6a1f1dSLionel Sambuc         else {
956*0a6a1f1dSLionel Sambuc             this.op = (unsigned char)(32 + 64);         /* end of block */
957*0a6a1f1dSLionel Sambuc             this.val = 0;
958*0a6a1f1dSLionel Sambuc         }
959*0a6a1f1dSLionel Sambuc 
960*0a6a1f1dSLionel Sambuc         /* replicate for those indices with low len bits equal to huff */
961*0a6a1f1dSLionel Sambuc         incr = 1U << (len - drop);
962*0a6a1f1dSLionel Sambuc         fill = 1U << curr;
963*0a6a1f1dSLionel Sambuc         mmin = fill;                 /* save offset to next table */
964*0a6a1f1dSLionel Sambuc         do {
965*0a6a1f1dSLionel Sambuc             fill -= incr;
966*0a6a1f1dSLionel Sambuc             next[(huff >> drop) + fill] = this;
967*0a6a1f1dSLionel Sambuc         } while (fill != 0);
968*0a6a1f1dSLionel Sambuc 
969*0a6a1f1dSLionel Sambuc         /* backwards increment the len-bit code huff */
970*0a6a1f1dSLionel Sambuc         incr = 1U << (len - 1);
971*0a6a1f1dSLionel Sambuc         while (huff & incr)
972*0a6a1f1dSLionel Sambuc             incr >>= 1;
973*0a6a1f1dSLionel Sambuc         if (incr != 0) {
974*0a6a1f1dSLionel Sambuc             huff &= incr - 1;
975*0a6a1f1dSLionel Sambuc             huff += incr;
976*0a6a1f1dSLionel Sambuc         }
977*0a6a1f1dSLionel Sambuc         else
978*0a6a1f1dSLionel Sambuc             huff = 0;
979*0a6a1f1dSLionel Sambuc 
980*0a6a1f1dSLionel Sambuc         /* go to next symbol, update count, len */
981*0a6a1f1dSLionel Sambuc         sym++;
982*0a6a1f1dSLionel Sambuc         if (--(count[len]) == 0) {
983*0a6a1f1dSLionel Sambuc             if (len == mmax) break;
984*0a6a1f1dSLionel Sambuc             len = lens[work[sym]];
985*0a6a1f1dSLionel Sambuc         }
986*0a6a1f1dSLionel Sambuc 
987*0a6a1f1dSLionel Sambuc         /* create new sub-table if needed */
988*0a6a1f1dSLionel Sambuc         if (len > root && (huff & mask) != low) {
989*0a6a1f1dSLionel Sambuc             /* if first time, transition to sub-tables */
990*0a6a1f1dSLionel Sambuc             if (drop == 0)
991*0a6a1f1dSLionel Sambuc                 drop = root;
992*0a6a1f1dSLionel Sambuc 
993*0a6a1f1dSLionel Sambuc             /* increment past last table */
994*0a6a1f1dSLionel Sambuc             next += mmin;            /* here mmin is 1 << curr */
995*0a6a1f1dSLionel Sambuc 
996*0a6a1f1dSLionel Sambuc             /* determine length of next table */
997*0a6a1f1dSLionel Sambuc             curr = len - drop;
998*0a6a1f1dSLionel Sambuc             left = (int)(1 << curr);
999*0a6a1f1dSLionel Sambuc             while (curr + drop < mmax) {
1000*0a6a1f1dSLionel Sambuc                 left -= count[curr + drop];
1001*0a6a1f1dSLionel Sambuc                 if (left <= 0) break;
1002*0a6a1f1dSLionel Sambuc                 curr++;
1003*0a6a1f1dSLionel Sambuc                 left <<= 1;
1004*0a6a1f1dSLionel Sambuc             }
1005*0a6a1f1dSLionel Sambuc 
1006*0a6a1f1dSLionel Sambuc             /* check for enough space */
1007*0a6a1f1dSLionel Sambuc             used += 1U << curr;
1008*0a6a1f1dSLionel Sambuc             if (type == LENS && used >= ENOUGH - MAXD)
1009*0a6a1f1dSLionel Sambuc                 return 1;
1010*0a6a1f1dSLionel Sambuc 
1011*0a6a1f1dSLionel Sambuc             /* point entry in root table to sub-table */
1012*0a6a1f1dSLionel Sambuc             low = huff & mask;
1013*0a6a1f1dSLionel Sambuc             (*table)[low].op = (unsigned char)curr;
1014*0a6a1f1dSLionel Sambuc             (*table)[low].bits = (unsigned char)root;
1015*0a6a1f1dSLionel Sambuc             (*table)[low].val = (unsigned short)(next - *table);
1016*0a6a1f1dSLionel Sambuc         }
1017*0a6a1f1dSLionel Sambuc     }
1018*0a6a1f1dSLionel Sambuc 
1019*0a6a1f1dSLionel Sambuc     /*
1020*0a6a1f1dSLionel Sambuc        Fill in rest of table for incomplete codes.  This loop is similar to the
1021*0a6a1f1dSLionel Sambuc        loop above in incrementing huff for table indices.  It is assumed that
1022*0a6a1f1dSLionel Sambuc        len is equal to curr + drop, so there is no loop needed to increment
1023*0a6a1f1dSLionel Sambuc        through high index bits.  When the current sub-table is filled, the loop
1024*0a6a1f1dSLionel Sambuc        drops back to the root table to fill in any remaining entries there.
1025*0a6a1f1dSLionel Sambuc      */
1026*0a6a1f1dSLionel Sambuc     this.op = (unsigned char)64;                /* invalid code marker */
1027*0a6a1f1dSLionel Sambuc     this.bits = (unsigned char)(len - drop);
1028*0a6a1f1dSLionel Sambuc     this.val = (unsigned short)0;
1029*0a6a1f1dSLionel Sambuc     while (huff != 0) {
1030*0a6a1f1dSLionel Sambuc         /* when done with sub-table, drop back to root table */
1031*0a6a1f1dSLionel Sambuc         if (drop != 0 && (huff & mask) != low) {
1032*0a6a1f1dSLionel Sambuc             drop = 0;
1033*0a6a1f1dSLionel Sambuc             len = root;
1034*0a6a1f1dSLionel Sambuc             next = *table;
1035*0a6a1f1dSLionel Sambuc             this.bits = (unsigned char)len;
1036*0a6a1f1dSLionel Sambuc         }
1037*0a6a1f1dSLionel Sambuc 
1038*0a6a1f1dSLionel Sambuc         /* put invalid code marker in table */
1039*0a6a1f1dSLionel Sambuc         next[huff >> drop] = this;
1040*0a6a1f1dSLionel Sambuc 
1041*0a6a1f1dSLionel Sambuc         /* backwards increment the len-bit code huff */
1042*0a6a1f1dSLionel Sambuc         incr = 1U << (len - 1);
1043*0a6a1f1dSLionel Sambuc         while (huff & incr)
1044*0a6a1f1dSLionel Sambuc             incr >>= 1;
1045*0a6a1f1dSLionel Sambuc         if (incr != 0) {
1046*0a6a1f1dSLionel Sambuc             huff &= incr - 1;
1047*0a6a1f1dSLionel Sambuc             huff += incr;
1048*0a6a1f1dSLionel Sambuc         }
1049*0a6a1f1dSLionel Sambuc         else
1050*0a6a1f1dSLionel Sambuc             huff = 0;
1051*0a6a1f1dSLionel Sambuc     }
1052*0a6a1f1dSLionel Sambuc 
1053*0a6a1f1dSLionel Sambuc     /* set return parameters */
1054*0a6a1f1dSLionel Sambuc     *table += used;
1055*0a6a1f1dSLionel Sambuc     *bits = root;
1056*0a6a1f1dSLionel Sambuc     return 0;
1057*0a6a1f1dSLionel Sambuc }
1058*0a6a1f1dSLionel Sambuc 
1059*0a6a1f1dSLionel Sambuc /* Allow machine dependent optimization for post-increment or pre-increment.
1060*0a6a1f1dSLionel Sambuc    Based on testing to date,
1061*0a6a1f1dSLionel Sambuc    Pre-increment preferred for:
1062*0a6a1f1dSLionel Sambuc    - PowerPC G3 (Adler)
1063*0a6a1f1dSLionel Sambuc    - MIPS R5000 (Randers-Pehrson)
1064*0a6a1f1dSLionel Sambuc    Post-increment preferred for:
1065*0a6a1f1dSLionel Sambuc    - none
1066*0a6a1f1dSLionel Sambuc    No measurable difference:
1067*0a6a1f1dSLionel Sambuc    - Pentium III (Anderson)
1068*0a6a1f1dSLionel Sambuc    - M68060 (Nikl)
1069*0a6a1f1dSLionel Sambuc  */
1070*0a6a1f1dSLionel Sambuc #ifdef POSTINC
1071*0a6a1f1dSLionel Sambuc #  define OFF 0
1072*0a6a1f1dSLionel Sambuc #  define PUP(a) *(a)++
1073*0a6a1f1dSLionel Sambuc #else
1074*0a6a1f1dSLionel Sambuc #  define OFF 1
1075*0a6a1f1dSLionel Sambuc #  define PUP(a) *++(a)
1076*0a6a1f1dSLionel Sambuc #endif
1077*0a6a1f1dSLionel Sambuc 
1078*0a6a1f1dSLionel Sambuc /*
1079*0a6a1f1dSLionel Sambuc    Decode literal, length, and distance codes and write out the resulting
1080*0a6a1f1dSLionel Sambuc    literal and match bytes until either not enough input or output is
1081*0a6a1f1dSLionel Sambuc    available, an end-of-block is encountered, or a data error is encountered.
1082*0a6a1f1dSLionel Sambuc    When large enough input and output buffers are supplied to inflate(), for
1083*0a6a1f1dSLionel Sambuc    example, a 16K input buffer and a 64K output buffer, more than 95% of the
1084*0a6a1f1dSLionel Sambuc    inflate execution time is spent in this routine.
1085*0a6a1f1dSLionel Sambuc 
1086*0a6a1f1dSLionel Sambuc    Entry assumptions:
1087*0a6a1f1dSLionel Sambuc 
1088*0a6a1f1dSLionel Sambuc         state->mode == LEN
1089*0a6a1f1dSLionel Sambuc         strm->avail_in >= 6
1090*0a6a1f1dSLionel Sambuc         strm->avail_out >= 258
1091*0a6a1f1dSLionel Sambuc         start >= strm->avail_out
1092*0a6a1f1dSLionel Sambuc         state->bits < 8
1093*0a6a1f1dSLionel Sambuc 
1094*0a6a1f1dSLionel Sambuc    On return, state->mode is one of:
1095*0a6a1f1dSLionel Sambuc 
1096*0a6a1f1dSLionel Sambuc         LEN -- ran out of enough output space or enough available input
1097*0a6a1f1dSLionel Sambuc         TYPE -- reached end of block code, inflate() to interpret next block
1098*0a6a1f1dSLionel Sambuc         BAD -- error in block data
1099*0a6a1f1dSLionel Sambuc 
1100*0a6a1f1dSLionel Sambuc    Notes:
1101*0a6a1f1dSLionel Sambuc 
1102*0a6a1f1dSLionel Sambuc     - The maximum input bits used by a length/distance pair is 15 bits for the
1103*0a6a1f1dSLionel Sambuc       length code, 5 bits for the length extra, 15 bits for the distance code,
1104*0a6a1f1dSLionel Sambuc       and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
1105*0a6a1f1dSLionel Sambuc       Therefore if strm->avail_in >= 6, then there is enough input to avoid
1106*0a6a1f1dSLionel Sambuc       checking for available input while decoding.
1107*0a6a1f1dSLionel Sambuc 
1108*0a6a1f1dSLionel Sambuc     - The maximum bytes that a single length/distance pair can output is 258
1109*0a6a1f1dSLionel Sambuc       bytes, which is the maximum length that can be coded.  inflate_fast()
1110*0a6a1f1dSLionel Sambuc       requires strm->avail_out >= 258 for each loop to avoid checking for
1111*0a6a1f1dSLionel Sambuc       output space.
1112*0a6a1f1dSLionel Sambuc  */
1113*0a6a1f1dSLionel Sambuc void
inflate_fast(z_streamp strm,unsigned start)1114*0a6a1f1dSLionel Sambuc inflate_fast(z_streamp strm, unsigned start)
1115*0a6a1f1dSLionel Sambuc /* inflate()'s starting value for strm->avail_out */
1116*0a6a1f1dSLionel Sambuc {
1117*0a6a1f1dSLionel Sambuc     struct inflate_state FAR *state;
1118*0a6a1f1dSLionel Sambuc     unsigned char FAR *in;      /* local strm->next_in */
1119*0a6a1f1dSLionel Sambuc     unsigned char FAR *last;    /* while in < last, enough input available */
1120*0a6a1f1dSLionel Sambuc     unsigned char FAR *out;     /* local strm->next_out */
1121*0a6a1f1dSLionel Sambuc     unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
1122*0a6a1f1dSLionel Sambuc     unsigned char FAR *end;     /* while out < end, enough space available */
1123*0a6a1f1dSLionel Sambuc #ifdef INFLATE_STRICT
1124*0a6a1f1dSLionel Sambuc     unsigned dmax;              /* maximum distance from zlib header */
1125*0a6a1f1dSLionel Sambuc #endif
1126*0a6a1f1dSLionel Sambuc     unsigned wsize;             /* window size or zero if not using window */
1127*0a6a1f1dSLionel Sambuc     unsigned whave;             /* valid bytes in the window */
1128*0a6a1f1dSLionel Sambuc     unsigned wwrite;            /* window write index */
1129*0a6a1f1dSLionel Sambuc     unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
1130*0a6a1f1dSLionel Sambuc     unsigned long hold;         /* local strm->hold */
1131*0a6a1f1dSLionel Sambuc     unsigned bits;              /* local strm->bits */
1132*0a6a1f1dSLionel Sambuc     code const FAR *lcode;      /* local strm->lencode */
1133*0a6a1f1dSLionel Sambuc     code const FAR *dcode;      /* local strm->distcode */
1134*0a6a1f1dSLionel Sambuc     unsigned lmask;             /* mask for first level of length codes */
1135*0a6a1f1dSLionel Sambuc     unsigned dmask;             /* mask for first level of distance codes */
1136*0a6a1f1dSLionel Sambuc     code this;                  /* retrieved table entry */
1137*0a6a1f1dSLionel Sambuc     unsigned op;                /* code bits, operation, extra bits, or */
1138*0a6a1f1dSLionel Sambuc                                 /*  window position, window bytes to copy */
1139*0a6a1f1dSLionel Sambuc     unsigned len;               /* match length, unused bytes */
1140*0a6a1f1dSLionel Sambuc     unsigned dist;              /* match distance */
1141*0a6a1f1dSLionel Sambuc     unsigned char FAR *from;    /* where to copy match from */
1142*0a6a1f1dSLionel Sambuc 
1143*0a6a1f1dSLionel Sambuc     /* copy state to local variables */
1144*0a6a1f1dSLionel Sambuc     state = (struct inflate_state FAR *)(void *)strm->state;
1145*0a6a1f1dSLionel Sambuc     in = strm->next_in - OFF;
1146*0a6a1f1dSLionel Sambuc     last = in + (strm->avail_in - 5);
1147*0a6a1f1dSLionel Sambuc     out = strm->next_out - OFF;
1148*0a6a1f1dSLionel Sambuc     beg = out - (start - strm->avail_out);
1149*0a6a1f1dSLionel Sambuc     end = out + (strm->avail_out - 257);
1150*0a6a1f1dSLionel Sambuc #ifdef INFLATE_STRICT
1151*0a6a1f1dSLionel Sambuc     dmax = state->dmax;
1152*0a6a1f1dSLionel Sambuc #endif
1153*0a6a1f1dSLionel Sambuc     wsize = state->wsize;
1154*0a6a1f1dSLionel Sambuc     whave = state->whave;
1155*0a6a1f1dSLionel Sambuc     wwrite = state->write;
1156*0a6a1f1dSLionel Sambuc     window = state->window;
1157*0a6a1f1dSLionel Sambuc     hold = state->hold;
1158*0a6a1f1dSLionel Sambuc     bits = state->bits;
1159*0a6a1f1dSLionel Sambuc     lcode = state->lencode;
1160*0a6a1f1dSLionel Sambuc     dcode = state->distcode;
1161*0a6a1f1dSLionel Sambuc     lmask = (1U << state->lenbits) - 1;
1162*0a6a1f1dSLionel Sambuc     dmask = (1U << state->distbits) - 1;
1163*0a6a1f1dSLionel Sambuc 
1164*0a6a1f1dSLionel Sambuc     /* decode literals and length/distances until end-of-block or not enough
1165*0a6a1f1dSLionel Sambuc        input data or output space */
1166*0a6a1f1dSLionel Sambuc     do {
1167*0a6a1f1dSLionel Sambuc         if (bits < 15) {
1168*0a6a1f1dSLionel Sambuc             hold += (unsigned long)(PUP(in)) << bits;
1169*0a6a1f1dSLionel Sambuc             bits += 8;
1170*0a6a1f1dSLionel Sambuc             hold += (unsigned long)(PUP(in)) << bits;
1171*0a6a1f1dSLionel Sambuc             bits += 8;
1172*0a6a1f1dSLionel Sambuc         }
1173*0a6a1f1dSLionel Sambuc         this = lcode[hold & lmask];
1174*0a6a1f1dSLionel Sambuc       dolen:
1175*0a6a1f1dSLionel Sambuc         op = (unsigned)(this.bits);
1176*0a6a1f1dSLionel Sambuc         hold >>= op;
1177*0a6a1f1dSLionel Sambuc         bits -= op;
1178*0a6a1f1dSLionel Sambuc         op = (unsigned)(this.op);
1179*0a6a1f1dSLionel Sambuc         if (op == 0) {                          /* literal */
1180*0a6a1f1dSLionel Sambuc             Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
1181*0a6a1f1dSLionel Sambuc                     "inflate:         literal '%c'\n" :
1182*0a6a1f1dSLionel Sambuc                     "inflate:         literal 0x%02x\n", this.val));
1183*0a6a1f1dSLionel Sambuc             PUP(out) = (unsigned char)(this.val);
1184*0a6a1f1dSLionel Sambuc         }
1185*0a6a1f1dSLionel Sambuc         else if (op & 16) {                     /* length base */
1186*0a6a1f1dSLionel Sambuc             len = (unsigned)(this.val);
1187*0a6a1f1dSLionel Sambuc             op &= 15;                           /* number of extra bits */
1188*0a6a1f1dSLionel Sambuc             if (op) {
1189*0a6a1f1dSLionel Sambuc                 if (bits < op) {
1190*0a6a1f1dSLionel Sambuc                     hold += (unsigned long)(PUP(in)) << bits;
1191*0a6a1f1dSLionel Sambuc                     bits += 8;
1192*0a6a1f1dSLionel Sambuc                 }
1193*0a6a1f1dSLionel Sambuc                 len += (unsigned)hold & ((1U << op) - 1);
1194*0a6a1f1dSLionel Sambuc                 hold >>= op;
1195*0a6a1f1dSLionel Sambuc                 bits -= op;
1196*0a6a1f1dSLionel Sambuc             }
1197*0a6a1f1dSLionel Sambuc             Tracevv((stderr, "inflate:         length %u\n", len));
1198*0a6a1f1dSLionel Sambuc             if (bits < 15) {
1199*0a6a1f1dSLionel Sambuc                 hold += (unsigned long)(PUP(in)) << bits;
1200*0a6a1f1dSLionel Sambuc                 bits += 8;
1201*0a6a1f1dSLionel Sambuc                 hold += (unsigned long)(PUP(in)) << bits;
1202*0a6a1f1dSLionel Sambuc                 bits += 8;
1203*0a6a1f1dSLionel Sambuc             }
1204*0a6a1f1dSLionel Sambuc             this = dcode[hold & dmask];
1205*0a6a1f1dSLionel Sambuc           dodist:
1206*0a6a1f1dSLionel Sambuc             op = (unsigned)(this.bits);
1207*0a6a1f1dSLionel Sambuc             hold >>= op;
1208*0a6a1f1dSLionel Sambuc             bits -= op;
1209*0a6a1f1dSLionel Sambuc             op = (unsigned)(this.op);
1210*0a6a1f1dSLionel Sambuc             if (op & 16) {                      /* distance base */
1211*0a6a1f1dSLionel Sambuc                 dist = (unsigned)(this.val);
1212*0a6a1f1dSLionel Sambuc                 op &= 15;                       /* number of extra bits */
1213*0a6a1f1dSLionel Sambuc                 if (bits < op) {
1214*0a6a1f1dSLionel Sambuc                     hold += (unsigned long)(PUP(in)) << bits;
1215*0a6a1f1dSLionel Sambuc                     bits += 8;
1216*0a6a1f1dSLionel Sambuc                     if (bits < op) {
1217*0a6a1f1dSLionel Sambuc                         hold += (unsigned long)(PUP(in)) << bits;
1218*0a6a1f1dSLionel Sambuc                         bits += 8;
1219*0a6a1f1dSLionel Sambuc                     }
1220*0a6a1f1dSLionel Sambuc                 }
1221*0a6a1f1dSLionel Sambuc                 dist += (unsigned)hold & ((1U << op) - 1);
1222*0a6a1f1dSLionel Sambuc #ifdef INFLATE_STRICT
1223*0a6a1f1dSLionel Sambuc                 if (dist > dmax) {
1224*0a6a1f1dSLionel Sambuc                     strm->msg = (char *)"invalid distance too far back";
1225*0a6a1f1dSLionel Sambuc                     state->mode = BAD;
1226*0a6a1f1dSLionel Sambuc                     break;
1227*0a6a1f1dSLionel Sambuc                 }
1228*0a6a1f1dSLionel Sambuc #endif
1229*0a6a1f1dSLionel Sambuc                 hold >>= op;
1230*0a6a1f1dSLionel Sambuc                 bits -= op;
1231*0a6a1f1dSLionel Sambuc                 Tracevv((stderr, "inflate:         distance %u\n", dist));
1232*0a6a1f1dSLionel Sambuc                 op = (unsigned)(out - beg);     /* max distance in output */
1233*0a6a1f1dSLionel Sambuc                 if (dist > op) {                /* see if copy from window */
1234*0a6a1f1dSLionel Sambuc                     op = dist - op;             /* distance back in window */
1235*0a6a1f1dSLionel Sambuc                     if (op > whave) {
1236*0a6a1f1dSLionel Sambuc                         strm->msg = __UNCONST("invalid distance too far back");
1237*0a6a1f1dSLionel Sambuc                         state->mode = BAD;
1238*0a6a1f1dSLionel Sambuc                         break;
1239*0a6a1f1dSLionel Sambuc                     }
1240*0a6a1f1dSLionel Sambuc                     from = window - OFF;
1241*0a6a1f1dSLionel Sambuc                     if (wwrite == 0) {           /* very common case */
1242*0a6a1f1dSLionel Sambuc                         from += wsize - op;
1243*0a6a1f1dSLionel Sambuc                         if (op < len) {         /* some from window */
1244*0a6a1f1dSLionel Sambuc                             len -= op;
1245*0a6a1f1dSLionel Sambuc                             do {
1246*0a6a1f1dSLionel Sambuc                                 PUP(out) = PUP(from);
1247*0a6a1f1dSLionel Sambuc                             } while (--op);
1248*0a6a1f1dSLionel Sambuc                             from = out - dist;  /* rest from output */
1249*0a6a1f1dSLionel Sambuc                         }
1250*0a6a1f1dSLionel Sambuc                     }
1251*0a6a1f1dSLionel Sambuc                     else if (wwrite < op) {      /* wrap around window */
1252*0a6a1f1dSLionel Sambuc                         from += wsize + wwrite - op;
1253*0a6a1f1dSLionel Sambuc                         op -= wwrite;
1254*0a6a1f1dSLionel Sambuc                         if (op < len) {         /* some from end of window */
1255*0a6a1f1dSLionel Sambuc                             len -= op;
1256*0a6a1f1dSLionel Sambuc                             do {
1257*0a6a1f1dSLionel Sambuc                                 PUP(out) = PUP(from);
1258*0a6a1f1dSLionel Sambuc                             } while (--op);
1259*0a6a1f1dSLionel Sambuc                             from = window - OFF;
1260*0a6a1f1dSLionel Sambuc                             if (wwrite < len) {  /* some from start of window */
1261*0a6a1f1dSLionel Sambuc                                 op = wwrite;
1262*0a6a1f1dSLionel Sambuc                                 len -= op;
1263*0a6a1f1dSLionel Sambuc                                 do {
1264*0a6a1f1dSLionel Sambuc                                     PUP(out) = PUP(from);
1265*0a6a1f1dSLionel Sambuc                                 } while (--op);
1266*0a6a1f1dSLionel Sambuc                                 from = out - dist;      /* rest from output */
1267*0a6a1f1dSLionel Sambuc                             }
1268*0a6a1f1dSLionel Sambuc                         }
1269*0a6a1f1dSLionel Sambuc                     }
1270*0a6a1f1dSLionel Sambuc                     else {                      /* contiguous in window */
1271*0a6a1f1dSLionel Sambuc                         from += wwrite - op;
1272*0a6a1f1dSLionel Sambuc                         if (op < len) {         /* some from window */
1273*0a6a1f1dSLionel Sambuc                             len -= op;
1274*0a6a1f1dSLionel Sambuc                             do {
1275*0a6a1f1dSLionel Sambuc                                 PUP(out) = PUP(from);
1276*0a6a1f1dSLionel Sambuc                             } while (--op);
1277*0a6a1f1dSLionel Sambuc                             from = out - dist;  /* rest from output */
1278*0a6a1f1dSLionel Sambuc                         }
1279*0a6a1f1dSLionel Sambuc                     }
1280*0a6a1f1dSLionel Sambuc                     while (len > 2) {
1281*0a6a1f1dSLionel Sambuc                         PUP(out) = PUP(from);
1282*0a6a1f1dSLionel Sambuc                         PUP(out) = PUP(from);
1283*0a6a1f1dSLionel Sambuc                         PUP(out) = PUP(from);
1284*0a6a1f1dSLionel Sambuc                         len -= 3;
1285*0a6a1f1dSLionel Sambuc                     }
1286*0a6a1f1dSLionel Sambuc                     if (len) {
1287*0a6a1f1dSLionel Sambuc                         PUP(out) = PUP(from);
1288*0a6a1f1dSLionel Sambuc                         if (len > 1)
1289*0a6a1f1dSLionel Sambuc                             PUP(out) = PUP(from);
1290*0a6a1f1dSLionel Sambuc                     }
1291*0a6a1f1dSLionel Sambuc                 }
1292*0a6a1f1dSLionel Sambuc                 else {
1293*0a6a1f1dSLionel Sambuc                     from = out - dist;          /* copy direct from output */
1294*0a6a1f1dSLionel Sambuc                     do {                        /* minimum length is three */
1295*0a6a1f1dSLionel Sambuc                         PUP(out) = PUP(from);
1296*0a6a1f1dSLionel Sambuc                         PUP(out) = PUP(from);
1297*0a6a1f1dSLionel Sambuc                         PUP(out) = PUP(from);
1298*0a6a1f1dSLionel Sambuc                         len -= 3;
1299*0a6a1f1dSLionel Sambuc                     } while (len > 2);
1300*0a6a1f1dSLionel Sambuc                     if (len) {
1301*0a6a1f1dSLionel Sambuc                         PUP(out) = PUP(from);
1302*0a6a1f1dSLionel Sambuc                         if (len > 1)
1303*0a6a1f1dSLionel Sambuc                             PUP(out) = PUP(from);
1304*0a6a1f1dSLionel Sambuc                     }
1305*0a6a1f1dSLionel Sambuc                 }
1306*0a6a1f1dSLionel Sambuc             }
1307*0a6a1f1dSLionel Sambuc             else if ((op & 64) == 0) {          /* 2nd level distance code */
1308*0a6a1f1dSLionel Sambuc                 this = dcode[this.val + (hold & ((1U << op) - 1))];
1309*0a6a1f1dSLionel Sambuc                 goto dodist;
1310*0a6a1f1dSLionel Sambuc             }
1311*0a6a1f1dSLionel Sambuc             else {
1312*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("invalid distance code");
1313*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1314*0a6a1f1dSLionel Sambuc                 break;
1315*0a6a1f1dSLionel Sambuc             }
1316*0a6a1f1dSLionel Sambuc         }
1317*0a6a1f1dSLionel Sambuc         else if ((op & 64) == 0) {              /* 2nd level length code */
1318*0a6a1f1dSLionel Sambuc             this = lcode[this.val + (hold & ((1U << op) - 1))];
1319*0a6a1f1dSLionel Sambuc             goto dolen;
1320*0a6a1f1dSLionel Sambuc         }
1321*0a6a1f1dSLionel Sambuc         else if (op & 32) {                     /* end-of-block */
1322*0a6a1f1dSLionel Sambuc             Tracevv((stderr, "inflate:         end of block\n"));
1323*0a6a1f1dSLionel Sambuc             state->mode = TYPE;
1324*0a6a1f1dSLionel Sambuc             break;
1325*0a6a1f1dSLionel Sambuc         }
1326*0a6a1f1dSLionel Sambuc         else {
1327*0a6a1f1dSLionel Sambuc             strm->msg = __UNCONST("invalid literal/length code");
1328*0a6a1f1dSLionel Sambuc             state->mode = BAD;
1329*0a6a1f1dSLionel Sambuc             break;
1330*0a6a1f1dSLionel Sambuc         }
1331*0a6a1f1dSLionel Sambuc     } while (in < last && out < end);
1332*0a6a1f1dSLionel Sambuc 
1333*0a6a1f1dSLionel Sambuc     /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
1334*0a6a1f1dSLionel Sambuc     len = bits >> 3;
1335*0a6a1f1dSLionel Sambuc     in -= len;
1336*0a6a1f1dSLionel Sambuc     bits -= len << 3;
1337*0a6a1f1dSLionel Sambuc     hold &= (1U << bits) - 1;
1338*0a6a1f1dSLionel Sambuc 
1339*0a6a1f1dSLionel Sambuc     /* update state and return */
1340*0a6a1f1dSLionel Sambuc     strm->next_in = in + OFF;
1341*0a6a1f1dSLionel Sambuc     strm->next_out = out + OFF;
1342*0a6a1f1dSLionel Sambuc     strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
1343*0a6a1f1dSLionel Sambuc     strm->avail_out = (unsigned)(out < end ?
1344*0a6a1f1dSLionel Sambuc                                  257 + (end - out) : 257 - (out - end));
1345*0a6a1f1dSLionel Sambuc     state->hold = hold;
1346*0a6a1f1dSLionel Sambuc     state->bits = bits;
1347*0a6a1f1dSLionel Sambuc     return;
1348*0a6a1f1dSLionel Sambuc }
1349*0a6a1f1dSLionel Sambuc 
1350*0a6a1f1dSLionel Sambuc /*
1351*0a6a1f1dSLionel Sambuc    inflate() uses a state machine to process as much input data and generate as
1352*0a6a1f1dSLionel Sambuc    much output data as possible before returning.  The state machine is
1353*0a6a1f1dSLionel Sambuc    structured roughly as follows:
1354*0a6a1f1dSLionel Sambuc 
1355*0a6a1f1dSLionel Sambuc     for (;;) switch (state) {
1356*0a6a1f1dSLionel Sambuc     ...
1357*0a6a1f1dSLionel Sambuc     case STATEn:
1358*0a6a1f1dSLionel Sambuc         if (not enough input data or output space to make progress)
1359*0a6a1f1dSLionel Sambuc             return;
1360*0a6a1f1dSLionel Sambuc         ... make progress ...
1361*0a6a1f1dSLionel Sambuc         state = STATEm;
1362*0a6a1f1dSLionel Sambuc         break;
1363*0a6a1f1dSLionel Sambuc     ...
1364*0a6a1f1dSLionel Sambuc     }
1365*0a6a1f1dSLionel Sambuc 
1366*0a6a1f1dSLionel Sambuc    so when inflate() is called again, the same case is attempted again, and
1367*0a6a1f1dSLionel Sambuc    if the appropriate resources are provided, the machine proceeds to the
1368*0a6a1f1dSLionel Sambuc    next state.  The NEEDBITS() macro is usually the way the state evaluates
1369*0a6a1f1dSLionel Sambuc    whether it can proceed or should return.  NEEDBITS() does the return if
1370*0a6a1f1dSLionel Sambuc    the requested bits are not available.  The typical use of the BITS macros
1371*0a6a1f1dSLionel Sambuc    is:
1372*0a6a1f1dSLionel Sambuc 
1373*0a6a1f1dSLionel Sambuc         NEEDBITS(n);
1374*0a6a1f1dSLionel Sambuc         ... do something with BITS(n) ...
1375*0a6a1f1dSLionel Sambuc         DROPBITS(n);
1376*0a6a1f1dSLionel Sambuc 
1377*0a6a1f1dSLionel Sambuc    where NEEDBITS(n) either returns from inflate() if there isn't enough
1378*0a6a1f1dSLionel Sambuc    input left to load n bits into the accumulator, or it continues.  BITS(n)
1379*0a6a1f1dSLionel Sambuc    gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
1380*0a6a1f1dSLionel Sambuc    the low n bits off the accumulator.  INITBITS() clears the accumulator
1381*0a6a1f1dSLionel Sambuc    and sets the number of available bits to zero.  BYTEBITS() discards just
1382*0a6a1f1dSLionel Sambuc    enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
1383*0a6a1f1dSLionel Sambuc    and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
1384*0a6a1f1dSLionel Sambuc 
1385*0a6a1f1dSLionel Sambuc    NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
1386*0a6a1f1dSLionel Sambuc    if there is no input available.  The decoding of variable length codes uses
1387*0a6a1f1dSLionel Sambuc    PULLBYTE() directly in order to pull just enough bytes to decode the next
1388*0a6a1f1dSLionel Sambuc    code, and no more.
1389*0a6a1f1dSLionel Sambuc 
1390*0a6a1f1dSLionel Sambuc    Some states loop until they get enough input, making sure that enough
1391*0a6a1f1dSLionel Sambuc    state information is maintained to continue the loop where it left off
1392*0a6a1f1dSLionel Sambuc    if NEEDBITS() returns in the loop.  For example, want, need, and keep
1393*0a6a1f1dSLionel Sambuc    would all have to actually be part of the saved state in case NEEDBITS()
1394*0a6a1f1dSLionel Sambuc    returns:
1395*0a6a1f1dSLionel Sambuc 
1396*0a6a1f1dSLionel Sambuc     case STATEw:
1397*0a6a1f1dSLionel Sambuc         while (want < need) {
1398*0a6a1f1dSLionel Sambuc             NEEDBITS(n);
1399*0a6a1f1dSLionel Sambuc             keep[want++] = BITS(n);
1400*0a6a1f1dSLionel Sambuc             DROPBITS(n);
1401*0a6a1f1dSLionel Sambuc         }
1402*0a6a1f1dSLionel Sambuc         state = STATEx;
1403*0a6a1f1dSLionel Sambuc     case STATEx:
1404*0a6a1f1dSLionel Sambuc 
1405*0a6a1f1dSLionel Sambuc    As shown above, if the next state is also the next case, then the break
1406*0a6a1f1dSLionel Sambuc    is omitted.
1407*0a6a1f1dSLionel Sambuc 
1408*0a6a1f1dSLionel Sambuc    A state may also return if there is not enough output space available to
1409*0a6a1f1dSLionel Sambuc    complete that state.  Those states are copying stored data, writing a
1410*0a6a1f1dSLionel Sambuc    literal byte, and copying a matching string.
1411*0a6a1f1dSLionel Sambuc 
1412*0a6a1f1dSLionel Sambuc    When returning, a "goto inf_leave" is used to update the total counters,
1413*0a6a1f1dSLionel Sambuc    update the check value, and determine whether any progress has been made
1414*0a6a1f1dSLionel Sambuc    during that inflate() call in order to return the proper return code.
1415*0a6a1f1dSLionel Sambuc    Progress is defined as a change in either strm->avail_in or strm->avail_out.
1416*0a6a1f1dSLionel Sambuc    When there is a window, goto inf_leave will update the window with the last
1417*0a6a1f1dSLionel Sambuc    output written.  If a goto inf_leave occurs in the middle of decompression
1418*0a6a1f1dSLionel Sambuc    and there is no window currently, goto inf_leave will create one and copy
1419*0a6a1f1dSLionel Sambuc    output to the window for the next call of inflate().
1420*0a6a1f1dSLionel Sambuc 
1421*0a6a1f1dSLionel Sambuc    In this implementation, the flush parameter of inflate() only affects the
1422*0a6a1f1dSLionel Sambuc    return code (per zlib.h).  inflate() always writes as much as possible to
1423*0a6a1f1dSLionel Sambuc    strm->next_out, given the space available and the provided input--the effect
1424*0a6a1f1dSLionel Sambuc    documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
1425*0a6a1f1dSLionel Sambuc    the allocation of and copying into a sliding window until necessary, which
1426*0a6a1f1dSLionel Sambuc    provides the effect documented in zlib.h for Z_FINISH when the entire input
1427*0a6a1f1dSLionel Sambuc    stream available.  So the only thing the flush parameter actually does is:
1428*0a6a1f1dSLionel Sambuc    when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
1429*0a6a1f1dSLionel Sambuc    will return Z_BUF_ERROR if it has not reached the end of the stream.
1430*0a6a1f1dSLionel Sambuc  */
1431*0a6a1f1dSLionel Sambuc 
1432*0a6a1f1dSLionel Sambuc int ZEXPORT
inflate(z_streamp strm,int flush)1433*0a6a1f1dSLionel Sambuc inflate(z_streamp strm, int flush)
1434*0a6a1f1dSLionel Sambuc {
1435*0a6a1f1dSLionel Sambuc     struct inflate_state FAR *state;
1436*0a6a1f1dSLionel Sambuc     unsigned char FAR *next;    /* next input */
1437*0a6a1f1dSLionel Sambuc     unsigned char FAR *put;     /* next output */
1438*0a6a1f1dSLionel Sambuc     unsigned have, left;        /* available input and output */
1439*0a6a1f1dSLionel Sambuc     unsigned long hold;         /* bit buffer */
1440*0a6a1f1dSLionel Sambuc     unsigned bits;              /* bits in bit buffer */
1441*0a6a1f1dSLionel Sambuc     unsigned in, out;           /* save starting available input and output */
1442*0a6a1f1dSLionel Sambuc     unsigned copy;              /* number of stored or match bytes to copy */
1443*0a6a1f1dSLionel Sambuc     unsigned char FAR *from;    /* where to copy match bytes from */
1444*0a6a1f1dSLionel Sambuc     code this;                  /* current decoding table entry */
1445*0a6a1f1dSLionel Sambuc     code last;                  /* parent table entry */
1446*0a6a1f1dSLionel Sambuc     unsigned len;               /* length to copy for repeats, bits to drop */
1447*0a6a1f1dSLionel Sambuc     int ret;                    /* return code */
1448*0a6a1f1dSLionel Sambuc #ifdef GUNZIP
1449*0a6a1f1dSLionel Sambuc     unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
1450*0a6a1f1dSLionel Sambuc #endif
1451*0a6a1f1dSLionel Sambuc     static const unsigned short order[19] = /* permutation of code lengths */
1452*0a6a1f1dSLionel Sambuc         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
1453*0a6a1f1dSLionel Sambuc 
1454*0a6a1f1dSLionel Sambuc #if defined(__NetBSD__) && defined(_STANDALONE)
1455*0a6a1f1dSLionel Sambuc     /* Some kernels are loaded at address 0x0 so strm->next_out could be NULL */
1456*0a6a1f1dSLionel Sambuc     if (strm == Z_NULL || strm->state == Z_NULL ||
1457*0a6a1f1dSLionel Sambuc         (strm->next_in == Z_NULL && strm->avail_in != 0))
1458*0a6a1f1dSLionel Sambuc         return Z_STREAM_ERROR;
1459*0a6a1f1dSLionel Sambuc #else
1460*0a6a1f1dSLionel Sambuc     if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
1461*0a6a1f1dSLionel Sambuc         (strm->next_in == Z_NULL && strm->avail_in != 0))
1462*0a6a1f1dSLionel Sambuc         return Z_STREAM_ERROR;
1463*0a6a1f1dSLionel Sambuc #endif
1464*0a6a1f1dSLionel Sambuc 
1465*0a6a1f1dSLionel Sambuc     state = (struct inflate_state FAR *)(void *)strm->state;
1466*0a6a1f1dSLionel Sambuc     if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
1467*0a6a1f1dSLionel Sambuc     LOAD();
1468*0a6a1f1dSLionel Sambuc     in = have;
1469*0a6a1f1dSLionel Sambuc     out = left;
1470*0a6a1f1dSLionel Sambuc     ret = Z_OK;
1471*0a6a1f1dSLionel Sambuc     for (;;)
1472*0a6a1f1dSLionel Sambuc         switch (state->mode) {
1473*0a6a1f1dSLionel Sambuc         case HEAD:
1474*0a6a1f1dSLionel Sambuc             if (state->wrap == 0) {
1475*0a6a1f1dSLionel Sambuc                 state->mode = TYPEDO;
1476*0a6a1f1dSLionel Sambuc                 break;
1477*0a6a1f1dSLionel Sambuc             }
1478*0a6a1f1dSLionel Sambuc             NEEDBITS(16);
1479*0a6a1f1dSLionel Sambuc #ifdef GUNZIP
1480*0a6a1f1dSLionel Sambuc             if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
1481*0a6a1f1dSLionel Sambuc                 state->check = crc32(0L, Z_NULL, 0);
1482*0a6a1f1dSLionel Sambuc                 CRC2(state->check, hold);
1483*0a6a1f1dSLionel Sambuc                 INITBITS();
1484*0a6a1f1dSLionel Sambuc                 state->mode = FLAGS;
1485*0a6a1f1dSLionel Sambuc                 break;
1486*0a6a1f1dSLionel Sambuc             }
1487*0a6a1f1dSLionel Sambuc             state->flags = 0;           /* expect zlib header */
1488*0a6a1f1dSLionel Sambuc             if (state->head != Z_NULL)
1489*0a6a1f1dSLionel Sambuc                 state->head->done = -1;
1490*0a6a1f1dSLionel Sambuc             if (!(state->wrap & 1) ||   /* check if zlib header allowed */
1491*0a6a1f1dSLionel Sambuc #else
1492*0a6a1f1dSLionel Sambuc             if (
1493*0a6a1f1dSLionel Sambuc #endif
1494*0a6a1f1dSLionel Sambuc                 ((BITS(8) << 8) + (hold >> 8)) % 31) {
1495*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("incorrect header check");
1496*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1497*0a6a1f1dSLionel Sambuc                 break;
1498*0a6a1f1dSLionel Sambuc             }
1499*0a6a1f1dSLionel Sambuc             if (BITS(4) != Z_DEFLATED) {
1500*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("unknown compression method");
1501*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1502*0a6a1f1dSLionel Sambuc                 break;
1503*0a6a1f1dSLionel Sambuc             }
1504*0a6a1f1dSLionel Sambuc             DROPBITS(4);
1505*0a6a1f1dSLionel Sambuc             len = BITS(4) + 8;
1506*0a6a1f1dSLionel Sambuc             if (len > state->wbits) {
1507*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("invalid window size");
1508*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1509*0a6a1f1dSLionel Sambuc                 break;
1510*0a6a1f1dSLionel Sambuc             }
1511*0a6a1f1dSLionel Sambuc             state->dmax = 1U << len;
1512*0a6a1f1dSLionel Sambuc             Tracev((stderr, "inflate:   zlib header ok\n"));
1513*0a6a1f1dSLionel Sambuc             strm->adler = state->check = adler32(0L, Z_NULL, 0);
1514*0a6a1f1dSLionel Sambuc             state->mode = hold & 0x200 ? DICTID : TYPE;
1515*0a6a1f1dSLionel Sambuc             INITBITS();
1516*0a6a1f1dSLionel Sambuc             break;
1517*0a6a1f1dSLionel Sambuc #ifdef GUNZIP
1518*0a6a1f1dSLionel Sambuc         case FLAGS:
1519*0a6a1f1dSLionel Sambuc             NEEDBITS(16);
1520*0a6a1f1dSLionel Sambuc             state->flags = (int)(hold);
1521*0a6a1f1dSLionel Sambuc             if ((state->flags & 0xff) != Z_DEFLATED) {
1522*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("unknown compression method");
1523*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1524*0a6a1f1dSLionel Sambuc                 break;
1525*0a6a1f1dSLionel Sambuc             }
1526*0a6a1f1dSLionel Sambuc             if (state->flags & 0xe000) {
1527*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("unknown header flags set");
1528*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1529*0a6a1f1dSLionel Sambuc                 break;
1530*0a6a1f1dSLionel Sambuc             }
1531*0a6a1f1dSLionel Sambuc             if (state->head != Z_NULL)
1532*0a6a1f1dSLionel Sambuc                 state->head->text = (int)((hold >> 8) & 1);
1533*0a6a1f1dSLionel Sambuc             if (state->flags & 0x0200) CRC2(state->check, hold);
1534*0a6a1f1dSLionel Sambuc             INITBITS();
1535*0a6a1f1dSLionel Sambuc             state->mode = TIME;
1536*0a6a1f1dSLionel Sambuc         case TIME:
1537*0a6a1f1dSLionel Sambuc             NEEDBITS(32);
1538*0a6a1f1dSLionel Sambuc             if (state->head != Z_NULL)
1539*0a6a1f1dSLionel Sambuc                 state->head->time = hold;
1540*0a6a1f1dSLionel Sambuc             if (state->flags & 0x0200) CRC4(state->check, hold);
1541*0a6a1f1dSLionel Sambuc             INITBITS();
1542*0a6a1f1dSLionel Sambuc             state->mode = OS;
1543*0a6a1f1dSLionel Sambuc         case OS:
1544*0a6a1f1dSLionel Sambuc             NEEDBITS(16);
1545*0a6a1f1dSLionel Sambuc             if (state->head != Z_NULL) {
1546*0a6a1f1dSLionel Sambuc                 state->head->xflags = (int)(hold & 0xff);
1547*0a6a1f1dSLionel Sambuc                 state->head->os = (int)(hold >> 8);
1548*0a6a1f1dSLionel Sambuc             }
1549*0a6a1f1dSLionel Sambuc             if (state->flags & 0x0200) CRC2(state->check, hold);
1550*0a6a1f1dSLionel Sambuc             INITBITS();
1551*0a6a1f1dSLionel Sambuc             state->mode = EXLEN;
1552*0a6a1f1dSLionel Sambuc         case EXLEN:
1553*0a6a1f1dSLionel Sambuc             if (state->flags & 0x0400) {
1554*0a6a1f1dSLionel Sambuc                 NEEDBITS(16);
1555*0a6a1f1dSLionel Sambuc                 state->length = (unsigned)(hold);
1556*0a6a1f1dSLionel Sambuc                 if (state->head != Z_NULL)
1557*0a6a1f1dSLionel Sambuc                     state->head->extra_len = (unsigned)hold;
1558*0a6a1f1dSLionel Sambuc                 if (state->flags & 0x0200) CRC2(state->check, hold);
1559*0a6a1f1dSLionel Sambuc                 INITBITS();
1560*0a6a1f1dSLionel Sambuc             }
1561*0a6a1f1dSLionel Sambuc             else if (state->head != Z_NULL)
1562*0a6a1f1dSLionel Sambuc                 state->head->extra = Z_NULL;
1563*0a6a1f1dSLionel Sambuc             state->mode = EXTRA;
1564*0a6a1f1dSLionel Sambuc         case EXTRA:
1565*0a6a1f1dSLionel Sambuc             if (state->flags & 0x0400) {
1566*0a6a1f1dSLionel Sambuc                 copy = state->length;
1567*0a6a1f1dSLionel Sambuc                 if (copy > have) copy = have;
1568*0a6a1f1dSLionel Sambuc                 if (copy) {
1569*0a6a1f1dSLionel Sambuc                     if (state->head != Z_NULL &&
1570*0a6a1f1dSLionel Sambuc                         state->head->extra != Z_NULL) {
1571*0a6a1f1dSLionel Sambuc                         len = state->head->extra_len - state->length;
1572*0a6a1f1dSLionel Sambuc 			Assert (next != NULL, "next is null");
1573*0a6a1f1dSLionel Sambuc                         zmemcpy(state->head->extra + len, next,
1574*0a6a1f1dSLionel Sambuc                                 len + copy > state->head->extra_max ?
1575*0a6a1f1dSLionel Sambuc                                 state->head->extra_max - len : copy);
1576*0a6a1f1dSLionel Sambuc                     }
1577*0a6a1f1dSLionel Sambuc                     if (state->flags & 0x0200)
1578*0a6a1f1dSLionel Sambuc                         state->check = crc32(state->check, next, copy);
1579*0a6a1f1dSLionel Sambuc                     have -= copy;
1580*0a6a1f1dSLionel Sambuc                     next += copy;
1581*0a6a1f1dSLionel Sambuc                     state->length -= copy;
1582*0a6a1f1dSLionel Sambuc                 }
1583*0a6a1f1dSLionel Sambuc                 if (state->length) goto inf_leave;
1584*0a6a1f1dSLionel Sambuc             }
1585*0a6a1f1dSLionel Sambuc             state->length = 0;
1586*0a6a1f1dSLionel Sambuc             state->mode = NAME;
1587*0a6a1f1dSLionel Sambuc         case NAME:
1588*0a6a1f1dSLionel Sambuc             if (state->flags & 0x0800) {
1589*0a6a1f1dSLionel Sambuc                 if (have == 0) goto inf_leave;
1590*0a6a1f1dSLionel Sambuc                 copy = 0;
1591*0a6a1f1dSLionel Sambuc                 do {
1592*0a6a1f1dSLionel Sambuc                     len = (unsigned)(next[copy++]);
1593*0a6a1f1dSLionel Sambuc                     if (state->head != Z_NULL &&
1594*0a6a1f1dSLionel Sambuc                             state->head->name != Z_NULL &&
1595*0a6a1f1dSLionel Sambuc                             state->length < state->head->name_max)
1596*0a6a1f1dSLionel Sambuc                         state->head->name[state->length++] = len;
1597*0a6a1f1dSLionel Sambuc                 } while (len && copy < have);
1598*0a6a1f1dSLionel Sambuc                 if (state->flags & 0x0200)
1599*0a6a1f1dSLionel Sambuc                     state->check = crc32(state->check, next, copy);
1600*0a6a1f1dSLionel Sambuc                 have -= copy;
1601*0a6a1f1dSLionel Sambuc                 next += copy;
1602*0a6a1f1dSLionel Sambuc                 if (len) goto inf_leave;
1603*0a6a1f1dSLionel Sambuc             }
1604*0a6a1f1dSLionel Sambuc             else if (state->head != Z_NULL)
1605*0a6a1f1dSLionel Sambuc                 state->head->name = Z_NULL;
1606*0a6a1f1dSLionel Sambuc             state->length = 0;
1607*0a6a1f1dSLionel Sambuc             state->mode = COMMENT;
1608*0a6a1f1dSLionel Sambuc         case COMMENT:
1609*0a6a1f1dSLionel Sambuc             if (state->flags & 0x1000) {
1610*0a6a1f1dSLionel Sambuc                 if (have == 0) goto inf_leave;
1611*0a6a1f1dSLionel Sambuc                 copy = 0;
1612*0a6a1f1dSLionel Sambuc                 do {
1613*0a6a1f1dSLionel Sambuc                     len = (unsigned)(next[copy++]);
1614*0a6a1f1dSLionel Sambuc                     if (state->head != Z_NULL &&
1615*0a6a1f1dSLionel Sambuc                             state->head->comment != Z_NULL &&
1616*0a6a1f1dSLionel Sambuc                             state->length < state->head->comm_max)
1617*0a6a1f1dSLionel Sambuc                         state->head->comment[state->length++] = len;
1618*0a6a1f1dSLionel Sambuc                 } while (len && copy < have);
1619*0a6a1f1dSLionel Sambuc                 if (state->flags & 0x0200)
1620*0a6a1f1dSLionel Sambuc                     state->check = crc32(state->check, next, copy);
1621*0a6a1f1dSLionel Sambuc                 have -= copy;
1622*0a6a1f1dSLionel Sambuc                 next += copy;
1623*0a6a1f1dSLionel Sambuc                 if (len) goto inf_leave;
1624*0a6a1f1dSLionel Sambuc             }
1625*0a6a1f1dSLionel Sambuc             else if (state->head != Z_NULL)
1626*0a6a1f1dSLionel Sambuc                 state->head->comment = Z_NULL;
1627*0a6a1f1dSLionel Sambuc             state->mode = HCRC;
1628*0a6a1f1dSLionel Sambuc         case HCRC:
1629*0a6a1f1dSLionel Sambuc             if (state->flags & 0x0200) {
1630*0a6a1f1dSLionel Sambuc                 NEEDBITS(16);
1631*0a6a1f1dSLionel Sambuc                 if (hold != (state->check & 0xffff)) {
1632*0a6a1f1dSLionel Sambuc                     strm->msg = __UNCONST("header crc mismatch");
1633*0a6a1f1dSLionel Sambuc                     state->mode = BAD;
1634*0a6a1f1dSLionel Sambuc                     break;
1635*0a6a1f1dSLionel Sambuc                 }
1636*0a6a1f1dSLionel Sambuc                 INITBITS();
1637*0a6a1f1dSLionel Sambuc             }
1638*0a6a1f1dSLionel Sambuc             if (state->head != Z_NULL) {
1639*0a6a1f1dSLionel Sambuc                 state->head->hcrc = (int)((state->flags >> 9) & 1);
1640*0a6a1f1dSLionel Sambuc                 state->head->done = 1;
1641*0a6a1f1dSLionel Sambuc             }
1642*0a6a1f1dSLionel Sambuc             strm->adler = state->check = crc32(0L, Z_NULL, 0);
1643*0a6a1f1dSLionel Sambuc             state->mode = TYPE;
1644*0a6a1f1dSLionel Sambuc             break;
1645*0a6a1f1dSLionel Sambuc #endif
1646*0a6a1f1dSLionel Sambuc         case DICTID:
1647*0a6a1f1dSLionel Sambuc             NEEDBITS(32);
1648*0a6a1f1dSLionel Sambuc             strm->adler = state->check = REVERSE(hold);
1649*0a6a1f1dSLionel Sambuc             INITBITS();
1650*0a6a1f1dSLionel Sambuc             state->mode = DICT;
1651*0a6a1f1dSLionel Sambuc         case DICT:
1652*0a6a1f1dSLionel Sambuc             if (state->havedict == 0) {
1653*0a6a1f1dSLionel Sambuc                 RESTORE();
1654*0a6a1f1dSLionel Sambuc                 return Z_NEED_DICT;
1655*0a6a1f1dSLionel Sambuc             }
1656*0a6a1f1dSLionel Sambuc             strm->adler = state->check = adler32(0L, Z_NULL, 0);
1657*0a6a1f1dSLionel Sambuc             state->mode = TYPE;
1658*0a6a1f1dSLionel Sambuc         case TYPE:
1659*0a6a1f1dSLionel Sambuc             if (flush == Z_BLOCK) goto inf_leave;
1660*0a6a1f1dSLionel Sambuc         case TYPEDO:
1661*0a6a1f1dSLionel Sambuc             if (state->last) {
1662*0a6a1f1dSLionel Sambuc                 BYTEBITS();
1663*0a6a1f1dSLionel Sambuc                 state->mode = CHECK;
1664*0a6a1f1dSLionel Sambuc                 break;
1665*0a6a1f1dSLionel Sambuc             }
1666*0a6a1f1dSLionel Sambuc             NEEDBITS(3);
1667*0a6a1f1dSLionel Sambuc             state->last = BITS(1);
1668*0a6a1f1dSLionel Sambuc             DROPBITS(1);
1669*0a6a1f1dSLionel Sambuc             switch (BITS(2)) {
1670*0a6a1f1dSLionel Sambuc             case 0:                             /* stored block */
1671*0a6a1f1dSLionel Sambuc                 Tracev((stderr, "inflate:     stored block%s\n",
1672*0a6a1f1dSLionel Sambuc                         state->last ? " (last)" : ""));
1673*0a6a1f1dSLionel Sambuc                 state->mode = STORED;
1674*0a6a1f1dSLionel Sambuc                 break;
1675*0a6a1f1dSLionel Sambuc             case 1:                             /* fixed block */
1676*0a6a1f1dSLionel Sambuc                 fixedtables(state);
1677*0a6a1f1dSLionel Sambuc                 Tracev((stderr, "inflate:     fixed codes block%s\n",
1678*0a6a1f1dSLionel Sambuc                         state->last ? " (last)" : ""));
1679*0a6a1f1dSLionel Sambuc                 state->mode = LEN;              /* decode codes */
1680*0a6a1f1dSLionel Sambuc                 break;
1681*0a6a1f1dSLionel Sambuc             case 2:                             /* dynamic block */
1682*0a6a1f1dSLionel Sambuc                 Tracev((stderr, "inflate:     dynamic codes block%s\n",
1683*0a6a1f1dSLionel Sambuc                         state->last ? " (last)" : ""));
1684*0a6a1f1dSLionel Sambuc                 state->mode = TABLE;
1685*0a6a1f1dSLionel Sambuc                 break;
1686*0a6a1f1dSLionel Sambuc             case 3:
1687*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("invalid block type");
1688*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1689*0a6a1f1dSLionel Sambuc             }
1690*0a6a1f1dSLionel Sambuc             DROPBITS(2);
1691*0a6a1f1dSLionel Sambuc             break;
1692*0a6a1f1dSLionel Sambuc         case STORED:
1693*0a6a1f1dSLionel Sambuc             BYTEBITS();                         /* go to byte boundary */
1694*0a6a1f1dSLionel Sambuc             NEEDBITS(32);
1695*0a6a1f1dSLionel Sambuc             if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
1696*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("invalid stored block lengths");
1697*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1698*0a6a1f1dSLionel Sambuc                 break;
1699*0a6a1f1dSLionel Sambuc             }
1700*0a6a1f1dSLionel Sambuc             state->length = (unsigned)hold & 0xffff;
1701*0a6a1f1dSLionel Sambuc             Tracev((stderr, "inflate:       stored length %u\n",
1702*0a6a1f1dSLionel Sambuc                     state->length));
1703*0a6a1f1dSLionel Sambuc             INITBITS();
1704*0a6a1f1dSLionel Sambuc             state->mode = COPY;
1705*0a6a1f1dSLionel Sambuc         case COPY:
1706*0a6a1f1dSLionel Sambuc             copy = state->length;
1707*0a6a1f1dSLionel Sambuc             if (copy) {
1708*0a6a1f1dSLionel Sambuc                 if (copy > have) copy = have;
1709*0a6a1f1dSLionel Sambuc                 if (copy > left) copy = left;
1710*0a6a1f1dSLionel Sambuc                 if (copy == 0) goto inf_leave;
1711*0a6a1f1dSLionel Sambuc                 zmemcpy(put, next, copy);
1712*0a6a1f1dSLionel Sambuc                 have -= copy;
1713*0a6a1f1dSLionel Sambuc                 next += copy;
1714*0a6a1f1dSLionel Sambuc                 left -= copy;
1715*0a6a1f1dSLionel Sambuc                 put += copy;
1716*0a6a1f1dSLionel Sambuc                 state->length -= copy;
1717*0a6a1f1dSLionel Sambuc                 break;
1718*0a6a1f1dSLionel Sambuc             }
1719*0a6a1f1dSLionel Sambuc             Tracev((stderr, "inflate:       stored end\n"));
1720*0a6a1f1dSLionel Sambuc             state->mode = TYPE;
1721*0a6a1f1dSLionel Sambuc             break;
1722*0a6a1f1dSLionel Sambuc         case TABLE:
1723*0a6a1f1dSLionel Sambuc             NEEDBITS(14);
1724*0a6a1f1dSLionel Sambuc             state->nlen = BITS(5) + 257;
1725*0a6a1f1dSLionel Sambuc             DROPBITS(5);
1726*0a6a1f1dSLionel Sambuc             state->ndist = BITS(5) + 1;
1727*0a6a1f1dSLionel Sambuc             DROPBITS(5);
1728*0a6a1f1dSLionel Sambuc             state->ncode = BITS(4) + 4;
1729*0a6a1f1dSLionel Sambuc             DROPBITS(4);
1730*0a6a1f1dSLionel Sambuc #ifndef PKZIP_BUG_WORKAROUND
1731*0a6a1f1dSLionel Sambuc             if (state->nlen > 286 || state->ndist > 30) {
1732*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("too many length or distance symbols");
1733*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1734*0a6a1f1dSLionel Sambuc                 break;
1735*0a6a1f1dSLionel Sambuc             }
1736*0a6a1f1dSLionel Sambuc #endif
1737*0a6a1f1dSLionel Sambuc             Tracev((stderr, "inflate:       table sizes ok\n"));
1738*0a6a1f1dSLionel Sambuc             state->have = 0;
1739*0a6a1f1dSLionel Sambuc             state->mode = LENLENS;
1740*0a6a1f1dSLionel Sambuc         case LENLENS:
1741*0a6a1f1dSLionel Sambuc             while (state->have < state->ncode) {
1742*0a6a1f1dSLionel Sambuc                 NEEDBITS(3);
1743*0a6a1f1dSLionel Sambuc                 state->lens[order[state->have++]] = (unsigned short)BITS(3);
1744*0a6a1f1dSLionel Sambuc                 DROPBITS(3);
1745*0a6a1f1dSLionel Sambuc             }
1746*0a6a1f1dSLionel Sambuc             while (state->have < 19)
1747*0a6a1f1dSLionel Sambuc                 state->lens[order[state->have++]] = 0;
1748*0a6a1f1dSLionel Sambuc             state->next = state->codes;
1749*0a6a1f1dSLionel Sambuc             state->lencode = (code const FAR *)(state->next);
1750*0a6a1f1dSLionel Sambuc             state->lenbits = 7;
1751*0a6a1f1dSLionel Sambuc             ret = inflate_table(CODES, state->lens, 19, &(state->next),
1752*0a6a1f1dSLionel Sambuc                                 &(state->lenbits), state->work);
1753*0a6a1f1dSLionel Sambuc             if (ret) {
1754*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("invalid code lengths set");
1755*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1756*0a6a1f1dSLionel Sambuc                 break;
1757*0a6a1f1dSLionel Sambuc             }
1758*0a6a1f1dSLionel Sambuc             Tracev((stderr, "inflate:       code lengths ok\n"));
1759*0a6a1f1dSLionel Sambuc             state->have = 0;
1760*0a6a1f1dSLionel Sambuc             state->mode = CODELENS;
1761*0a6a1f1dSLionel Sambuc         case CODELENS:
1762*0a6a1f1dSLionel Sambuc             while (state->have < state->nlen + state->ndist) {
1763*0a6a1f1dSLionel Sambuc                 for (;;) {
1764*0a6a1f1dSLionel Sambuc                     this = state->lencode[BITS(state->lenbits)];
1765*0a6a1f1dSLionel Sambuc                     if ((unsigned)(this.bits) <= bits) break;
1766*0a6a1f1dSLionel Sambuc                     PULLBYTE();
1767*0a6a1f1dSLionel Sambuc                 }
1768*0a6a1f1dSLionel Sambuc                 if (this.val < 16) {
1769*0a6a1f1dSLionel Sambuc                     NEEDBITS(this.bits);
1770*0a6a1f1dSLionel Sambuc                     DROPBITS(this.bits);
1771*0a6a1f1dSLionel Sambuc                     state->lens[state->have++] = this.val;
1772*0a6a1f1dSLionel Sambuc                 }
1773*0a6a1f1dSLionel Sambuc                 else {
1774*0a6a1f1dSLionel Sambuc                     if (this.val == 16) {
1775*0a6a1f1dSLionel Sambuc                         NEEDBITS(this.bits + 2);
1776*0a6a1f1dSLionel Sambuc                         DROPBITS(this.bits);
1777*0a6a1f1dSLionel Sambuc                         if (state->have == 0) {
1778*0a6a1f1dSLionel Sambuc                             strm->msg = __UNCONST("invalid bit length repeat");
1779*0a6a1f1dSLionel Sambuc                             state->mode = BAD;
1780*0a6a1f1dSLionel Sambuc                             break;
1781*0a6a1f1dSLionel Sambuc                         }
1782*0a6a1f1dSLionel Sambuc                         len = state->lens[state->have - 1];
1783*0a6a1f1dSLionel Sambuc                         copy = 3 + BITS(2);
1784*0a6a1f1dSLionel Sambuc                         DROPBITS(2);
1785*0a6a1f1dSLionel Sambuc                     }
1786*0a6a1f1dSLionel Sambuc                     else if (this.val == 17) {
1787*0a6a1f1dSLionel Sambuc                         NEEDBITS(this.bits + 3);
1788*0a6a1f1dSLionel Sambuc                         DROPBITS(this.bits);
1789*0a6a1f1dSLionel Sambuc                         len = 0;
1790*0a6a1f1dSLionel Sambuc                         copy = 3 + BITS(3);
1791*0a6a1f1dSLionel Sambuc                         DROPBITS(3);
1792*0a6a1f1dSLionel Sambuc                     }
1793*0a6a1f1dSLionel Sambuc                     else {
1794*0a6a1f1dSLionel Sambuc                         NEEDBITS(this.bits + 7);
1795*0a6a1f1dSLionel Sambuc                         DROPBITS(this.bits);
1796*0a6a1f1dSLionel Sambuc                         len = 0;
1797*0a6a1f1dSLionel Sambuc                         copy = 11 + BITS(7);
1798*0a6a1f1dSLionel Sambuc                         DROPBITS(7);
1799*0a6a1f1dSLionel Sambuc                     }
1800*0a6a1f1dSLionel Sambuc                     if (state->have + copy > state->nlen + state->ndist) {
1801*0a6a1f1dSLionel Sambuc                         strm->msg = __UNCONST("invalid bit length repeat");
1802*0a6a1f1dSLionel Sambuc                         state->mode = BAD;
1803*0a6a1f1dSLionel Sambuc                         break;
1804*0a6a1f1dSLionel Sambuc                     }
1805*0a6a1f1dSLionel Sambuc                     while (copy--)
1806*0a6a1f1dSLionel Sambuc                         state->lens[state->have++] = (unsigned short)len;
1807*0a6a1f1dSLionel Sambuc                 }
1808*0a6a1f1dSLionel Sambuc             }
1809*0a6a1f1dSLionel Sambuc 
1810*0a6a1f1dSLionel Sambuc             /* handle error breaks in while */
1811*0a6a1f1dSLionel Sambuc             if (state->mode == BAD) break;
1812*0a6a1f1dSLionel Sambuc 
1813*0a6a1f1dSLionel Sambuc             /* build code tables */
1814*0a6a1f1dSLionel Sambuc             state->next = state->codes;
1815*0a6a1f1dSLionel Sambuc             state->lencode = (code const FAR *)(state->next);
1816*0a6a1f1dSLionel Sambuc             state->lenbits = 9;
1817*0a6a1f1dSLionel Sambuc             ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1818*0a6a1f1dSLionel Sambuc                                 &(state->lenbits), state->work);
1819*0a6a1f1dSLionel Sambuc             if (ret) {
1820*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("invalid literal/lengths set");
1821*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1822*0a6a1f1dSLionel Sambuc                 break;
1823*0a6a1f1dSLionel Sambuc             }
1824*0a6a1f1dSLionel Sambuc             state->distcode = (code const FAR *)(state->next);
1825*0a6a1f1dSLionel Sambuc             state->distbits = 6;
1826*0a6a1f1dSLionel Sambuc             ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1827*0a6a1f1dSLionel Sambuc                             &(state->next), &(state->distbits), state->work);
1828*0a6a1f1dSLionel Sambuc             if (ret) {
1829*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("invalid distances set");
1830*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1831*0a6a1f1dSLionel Sambuc                 break;
1832*0a6a1f1dSLionel Sambuc             }
1833*0a6a1f1dSLionel Sambuc             Tracev((stderr, "inflate:       codes ok\n"));
1834*0a6a1f1dSLionel Sambuc             state->mode = LEN;
1835*0a6a1f1dSLionel Sambuc         case LEN:
1836*0a6a1f1dSLionel Sambuc             if (have >= 6 && left >= 258) {
1837*0a6a1f1dSLionel Sambuc                 RESTORE();
1838*0a6a1f1dSLionel Sambuc                 inflate_fast(strm, out);
1839*0a6a1f1dSLionel Sambuc                 LOAD();
1840*0a6a1f1dSLionel Sambuc                 break;
1841*0a6a1f1dSLionel Sambuc             }
1842*0a6a1f1dSLionel Sambuc             for (;;) {
1843*0a6a1f1dSLionel Sambuc                 this = state->lencode[BITS(state->lenbits)];
1844*0a6a1f1dSLionel Sambuc                 if ((unsigned)(this.bits) <= bits) break;
1845*0a6a1f1dSLionel Sambuc                 PULLBYTE();
1846*0a6a1f1dSLionel Sambuc             }
1847*0a6a1f1dSLionel Sambuc             if (this.op && (this.op & 0xf0) == 0) {
1848*0a6a1f1dSLionel Sambuc                 last = this;
1849*0a6a1f1dSLionel Sambuc                 for (;;) {
1850*0a6a1f1dSLionel Sambuc                     this = state->lencode[last.val +
1851*0a6a1f1dSLionel Sambuc                             (BITS(last.bits + last.op) >> last.bits)];
1852*0a6a1f1dSLionel Sambuc                     if ((unsigned)(last.bits + this.bits) <= bits) break;
1853*0a6a1f1dSLionel Sambuc                     PULLBYTE();
1854*0a6a1f1dSLionel Sambuc                 }
1855*0a6a1f1dSLionel Sambuc                 DROPBITS(last.bits);
1856*0a6a1f1dSLionel Sambuc             }
1857*0a6a1f1dSLionel Sambuc             DROPBITS(this.bits);
1858*0a6a1f1dSLionel Sambuc             state->length = (unsigned)this.val;
1859*0a6a1f1dSLionel Sambuc             if ((int)(this.op) == 0) {
1860*0a6a1f1dSLionel Sambuc                 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
1861*0a6a1f1dSLionel Sambuc                         "inflate:         literal '%c'\n" :
1862*0a6a1f1dSLionel Sambuc                         "inflate:         literal 0x%02x\n", this.val));
1863*0a6a1f1dSLionel Sambuc                 state->mode = LIT;
1864*0a6a1f1dSLionel Sambuc                 break;
1865*0a6a1f1dSLionel Sambuc             }
1866*0a6a1f1dSLionel Sambuc             if (this.op & 32) {
1867*0a6a1f1dSLionel Sambuc                 Tracevv((stderr, "inflate:         end of block\n"));
1868*0a6a1f1dSLionel Sambuc                 state->mode = TYPE;
1869*0a6a1f1dSLionel Sambuc                 break;
1870*0a6a1f1dSLionel Sambuc             }
1871*0a6a1f1dSLionel Sambuc             if (this.op & 64) {
1872*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("invalid literal/length code");
1873*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1874*0a6a1f1dSLionel Sambuc                 break;
1875*0a6a1f1dSLionel Sambuc             }
1876*0a6a1f1dSLionel Sambuc             state->extra = (unsigned)(this.op) & 15;
1877*0a6a1f1dSLionel Sambuc             state->mode = LENEXT;
1878*0a6a1f1dSLionel Sambuc         case LENEXT:
1879*0a6a1f1dSLionel Sambuc             if (state->extra) {
1880*0a6a1f1dSLionel Sambuc                 NEEDBITS(state->extra);
1881*0a6a1f1dSLionel Sambuc                 state->length += BITS(state->extra);
1882*0a6a1f1dSLionel Sambuc                 DROPBITS(state->extra);
1883*0a6a1f1dSLionel Sambuc             }
1884*0a6a1f1dSLionel Sambuc             Tracevv((stderr, "inflate:         length %u\n", state->length));
1885*0a6a1f1dSLionel Sambuc             state->mode = DIST;
1886*0a6a1f1dSLionel Sambuc         case DIST:
1887*0a6a1f1dSLionel Sambuc             for (;;) {
1888*0a6a1f1dSLionel Sambuc                 this = state->distcode[BITS(state->distbits)];
1889*0a6a1f1dSLionel Sambuc                 if ((unsigned)(this.bits) <= bits) break;
1890*0a6a1f1dSLionel Sambuc                 PULLBYTE();
1891*0a6a1f1dSLionel Sambuc             }
1892*0a6a1f1dSLionel Sambuc             if ((this.op & 0xf0) == 0) {
1893*0a6a1f1dSLionel Sambuc                 last = this;
1894*0a6a1f1dSLionel Sambuc                 for (;;) {
1895*0a6a1f1dSLionel Sambuc                     this = state->distcode[last.val +
1896*0a6a1f1dSLionel Sambuc                             (BITS(last.bits + last.op) >> last.bits)];
1897*0a6a1f1dSLionel Sambuc                     if ((unsigned)(last.bits + this.bits) <= bits) break;
1898*0a6a1f1dSLionel Sambuc                     PULLBYTE();
1899*0a6a1f1dSLionel Sambuc                 }
1900*0a6a1f1dSLionel Sambuc                 DROPBITS(last.bits);
1901*0a6a1f1dSLionel Sambuc             }
1902*0a6a1f1dSLionel Sambuc             DROPBITS(this.bits);
1903*0a6a1f1dSLionel Sambuc             if (this.op & 64) {
1904*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("invalid distance code");
1905*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1906*0a6a1f1dSLionel Sambuc                 break;
1907*0a6a1f1dSLionel Sambuc             }
1908*0a6a1f1dSLionel Sambuc             state->offset = (unsigned)this.val;
1909*0a6a1f1dSLionel Sambuc             state->extra = (unsigned)(this.op) & 15;
1910*0a6a1f1dSLionel Sambuc             state->mode = DISTEXT;
1911*0a6a1f1dSLionel Sambuc         case DISTEXT:
1912*0a6a1f1dSLionel Sambuc             if (state->extra) {
1913*0a6a1f1dSLionel Sambuc                 NEEDBITS(state->extra);
1914*0a6a1f1dSLionel Sambuc                 state->offset += BITS(state->extra);
1915*0a6a1f1dSLionel Sambuc                 DROPBITS(state->extra);
1916*0a6a1f1dSLionel Sambuc             }
1917*0a6a1f1dSLionel Sambuc #ifdef INFLATE_STRICT
1918*0a6a1f1dSLionel Sambuc             if (state->offset > state->dmax) {
1919*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("invalid distance too far back");
1920*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1921*0a6a1f1dSLionel Sambuc                 break;
1922*0a6a1f1dSLionel Sambuc             }
1923*0a6a1f1dSLionel Sambuc #endif
1924*0a6a1f1dSLionel Sambuc             if (state->offset > state->whave + out - left) {
1925*0a6a1f1dSLionel Sambuc                 strm->msg = __UNCONST("invalid distance too far back");
1926*0a6a1f1dSLionel Sambuc                 state->mode = BAD;
1927*0a6a1f1dSLionel Sambuc                 break;
1928*0a6a1f1dSLionel Sambuc             }
1929*0a6a1f1dSLionel Sambuc             Tracevv((stderr, "inflate:         distance %u\n", state->offset));
1930*0a6a1f1dSLionel Sambuc             state->mode = MATCH;
1931*0a6a1f1dSLionel Sambuc         case MATCH:
1932*0a6a1f1dSLionel Sambuc             if (left == 0) goto inf_leave;
1933*0a6a1f1dSLionel Sambuc             copy = out - left;
1934*0a6a1f1dSLionel Sambuc             if (state->offset > copy) {         /* copy from window */
1935*0a6a1f1dSLionel Sambuc                 copy = state->offset - copy;
1936*0a6a1f1dSLionel Sambuc                 if (copy > state->write) {
1937*0a6a1f1dSLionel Sambuc                     copy -= state->write;
1938*0a6a1f1dSLionel Sambuc                     from = state->window + (state->wsize - copy);
1939*0a6a1f1dSLionel Sambuc                 }
1940*0a6a1f1dSLionel Sambuc                 else
1941*0a6a1f1dSLionel Sambuc                     from = state->window + (state->write - copy);
1942*0a6a1f1dSLionel Sambuc                 if (copy > state->length) copy = state->length;
1943*0a6a1f1dSLionel Sambuc             }
1944*0a6a1f1dSLionel Sambuc             else {                              /* copy from output */
1945*0a6a1f1dSLionel Sambuc                 from = put - state->offset;
1946*0a6a1f1dSLionel Sambuc                 copy = state->length;
1947*0a6a1f1dSLionel Sambuc             }
1948*0a6a1f1dSLionel Sambuc             if (copy > left) copy = left;
1949*0a6a1f1dSLionel Sambuc             left -= copy;
1950*0a6a1f1dSLionel Sambuc             state->length -= copy;
1951*0a6a1f1dSLionel Sambuc             do {
1952*0a6a1f1dSLionel Sambuc                 *put++ = *from++;
1953*0a6a1f1dSLionel Sambuc             } while (--copy);
1954*0a6a1f1dSLionel Sambuc             if (state->length == 0) state->mode = LEN;
1955*0a6a1f1dSLionel Sambuc             break;
1956*0a6a1f1dSLionel Sambuc         case LIT:
1957*0a6a1f1dSLionel Sambuc             if (left == 0) goto inf_leave;
1958*0a6a1f1dSLionel Sambuc             *put++ = (unsigned char)(state->length);
1959*0a6a1f1dSLionel Sambuc             left--;
1960*0a6a1f1dSLionel Sambuc             state->mode = LEN;
1961*0a6a1f1dSLionel Sambuc             break;
1962*0a6a1f1dSLionel Sambuc         case CHECK:
1963*0a6a1f1dSLionel Sambuc             if (state->wrap) {
1964*0a6a1f1dSLionel Sambuc                 NEEDBITS(32);
1965*0a6a1f1dSLionel Sambuc                 out -= left;
1966*0a6a1f1dSLionel Sambuc                 strm->total_out += out;
1967*0a6a1f1dSLionel Sambuc                 state->total += out;
1968*0a6a1f1dSLionel Sambuc                 if (out)
1969*0a6a1f1dSLionel Sambuc                     strm->adler = state->check =
1970*0a6a1f1dSLionel Sambuc                         UPDATE(state->check, put - out, out);
1971*0a6a1f1dSLionel Sambuc                 out = left;
1972*0a6a1f1dSLionel Sambuc                 if ((
1973*0a6a1f1dSLionel Sambuc #ifdef GUNZIP
1974*0a6a1f1dSLionel Sambuc                      state->flags ? hold :
1975*0a6a1f1dSLionel Sambuc #endif
1976*0a6a1f1dSLionel Sambuc                      REVERSE(hold)) != state->check) {
1977*0a6a1f1dSLionel Sambuc                     strm->msg = __UNCONST("incorrect data check");
1978*0a6a1f1dSLionel Sambuc                     state->mode = BAD;
1979*0a6a1f1dSLionel Sambuc                     break;
1980*0a6a1f1dSLionel Sambuc                 }
1981*0a6a1f1dSLionel Sambuc                 INITBITS();
1982*0a6a1f1dSLionel Sambuc                 Tracev((stderr, "inflate:   check matches trailer\n"));
1983*0a6a1f1dSLionel Sambuc             }
1984*0a6a1f1dSLionel Sambuc #ifdef GUNZIP
1985*0a6a1f1dSLionel Sambuc             state->mode = LENGTH;
1986*0a6a1f1dSLionel Sambuc         case LENGTH:
1987*0a6a1f1dSLionel Sambuc             if (state->wrap && state->flags) {
1988*0a6a1f1dSLionel Sambuc                 NEEDBITS(32);
1989*0a6a1f1dSLionel Sambuc                 if (hold != (state->total & 0xffffffffUL)) {
1990*0a6a1f1dSLionel Sambuc                     strm->msg = __UNCONST("incorrect length check");
1991*0a6a1f1dSLionel Sambuc                     state->mode = BAD;
1992*0a6a1f1dSLionel Sambuc                     break;
1993*0a6a1f1dSLionel Sambuc                 }
1994*0a6a1f1dSLionel Sambuc                 INITBITS();
1995*0a6a1f1dSLionel Sambuc                 Tracev((stderr, "inflate:   length matches trailer\n"));
1996*0a6a1f1dSLionel Sambuc             }
1997*0a6a1f1dSLionel Sambuc #endif
1998*0a6a1f1dSLionel Sambuc             state->mode = DONE;
1999*0a6a1f1dSLionel Sambuc         case DONE:
2000*0a6a1f1dSLionel Sambuc             ret = Z_STREAM_END;
2001*0a6a1f1dSLionel Sambuc             goto inf_leave;
2002*0a6a1f1dSLionel Sambuc         case BAD:
2003*0a6a1f1dSLionel Sambuc             ret = Z_DATA_ERROR;
2004*0a6a1f1dSLionel Sambuc             goto inf_leave;
2005*0a6a1f1dSLionel Sambuc         case MEM:
2006*0a6a1f1dSLionel Sambuc             return Z_MEM_ERROR;
2007*0a6a1f1dSLionel Sambuc         case SYNC:
2008*0a6a1f1dSLionel Sambuc         default:
2009*0a6a1f1dSLionel Sambuc             return Z_STREAM_ERROR;
2010*0a6a1f1dSLionel Sambuc         }
2011*0a6a1f1dSLionel Sambuc 
2012*0a6a1f1dSLionel Sambuc     /*
2013*0a6a1f1dSLionel Sambuc        Return from inflate(), updating the total counts and the check value.
2014*0a6a1f1dSLionel Sambuc        If there was no progress during the inflate() call, return a buffer
2015*0a6a1f1dSLionel Sambuc        error.  Call updatewindow() to create and/or update the window state.
2016*0a6a1f1dSLionel Sambuc        Note: a memory error from inflate() is non-recoverable.
2017*0a6a1f1dSLionel Sambuc      */
2018*0a6a1f1dSLionel Sambuc   inf_leave:
2019*0a6a1f1dSLionel Sambuc     RESTORE();
2020*0a6a1f1dSLionel Sambuc     if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
2021*0a6a1f1dSLionel Sambuc         if (updatewindow(strm, out)) {
2022*0a6a1f1dSLionel Sambuc             state->mode = MEM;
2023*0a6a1f1dSLionel Sambuc             return Z_MEM_ERROR;
2024*0a6a1f1dSLionel Sambuc         }
2025*0a6a1f1dSLionel Sambuc     in -= strm->avail_in;
2026*0a6a1f1dSLionel Sambuc     out -= strm->avail_out;
2027*0a6a1f1dSLionel Sambuc     strm->total_in += in;
2028*0a6a1f1dSLionel Sambuc     strm->total_out += out;
2029*0a6a1f1dSLionel Sambuc     state->total += out;
2030*0a6a1f1dSLionel Sambuc     if (state->wrap && out)
2031*0a6a1f1dSLionel Sambuc         strm->adler = state->check =
2032*0a6a1f1dSLionel Sambuc             UPDATE(state->check, strm->next_out - out, out);
2033*0a6a1f1dSLionel Sambuc     strm->data_type = state->bits + (state->last ? 64 : 0) +
2034*0a6a1f1dSLionel Sambuc                       (state->mode == TYPE ? 128 : 0);
2035*0a6a1f1dSLionel Sambuc     if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
2036*0a6a1f1dSLionel Sambuc         ret = Z_BUF_ERROR;
2037*0a6a1f1dSLionel Sambuc     return ret;
2038*0a6a1f1dSLionel Sambuc }
2039*0a6a1f1dSLionel Sambuc 
2040*0a6a1f1dSLionel Sambuc int ZEXPORT
inflateEnd(z_streamp strm)2041*0a6a1f1dSLionel Sambuc inflateEnd(z_streamp strm)
2042*0a6a1f1dSLionel Sambuc {
2043*0a6a1f1dSLionel Sambuc     struct inflate_state FAR *state;
2044*0a6a1f1dSLionel Sambuc     if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
2045*0a6a1f1dSLionel Sambuc         return Z_STREAM_ERROR;
2046*0a6a1f1dSLionel Sambuc     state = (struct inflate_state FAR *)(void *)strm->state;
2047*0a6a1f1dSLionel Sambuc     if (state->window != Z_NULL) ZFREE(strm, state->window);
2048*0a6a1f1dSLionel Sambuc     ZFREE(strm, strm->state);
2049*0a6a1f1dSLionel Sambuc     strm->state = Z_NULL;
2050*0a6a1f1dSLionel Sambuc     Tracev((stderr, "inflate: end\n"));
2051*0a6a1f1dSLionel Sambuc     return Z_OK;
2052*0a6a1f1dSLionel Sambuc }
2053*0a6a1f1dSLionel Sambuc 
2054*0a6a1f1dSLionel Sambuc int ZEXPORT
inflateSetDictionary(z_streamp strm,const Bytef * dictionary,uInt dictLength)2055*0a6a1f1dSLionel Sambuc inflateSetDictionary(z_streamp strm, const Bytef *dictionary, uInt dictLength)
2056*0a6a1f1dSLionel Sambuc {
2057*0a6a1f1dSLionel Sambuc     struct inflate_state FAR *state;
2058*0a6a1f1dSLionel Sambuc     unsigned long id;
2059*0a6a1f1dSLionel Sambuc 
2060*0a6a1f1dSLionel Sambuc     /* check state */
2061*0a6a1f1dSLionel Sambuc     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
2062*0a6a1f1dSLionel Sambuc     state = (struct inflate_state FAR *)(void *)strm->state;
2063*0a6a1f1dSLionel Sambuc     if (state->wrap != 0 && state->mode != DICT)
2064*0a6a1f1dSLionel Sambuc         return Z_STREAM_ERROR;
2065*0a6a1f1dSLionel Sambuc 
2066*0a6a1f1dSLionel Sambuc     /* check for correct dictionary id */
2067*0a6a1f1dSLionel Sambuc     if (state->mode == DICT) {
2068*0a6a1f1dSLionel Sambuc         id = adler32(0L, Z_NULL, 0);
2069*0a6a1f1dSLionel Sambuc         id = adler32(id, dictionary, dictLength);
2070*0a6a1f1dSLionel Sambuc         if (id != state->check)
2071*0a6a1f1dSLionel Sambuc             return Z_DATA_ERROR;
2072*0a6a1f1dSLionel Sambuc     }
2073*0a6a1f1dSLionel Sambuc 
2074*0a6a1f1dSLionel Sambuc     /* copy dictionary to window */
2075*0a6a1f1dSLionel Sambuc     if (updatewindow(strm, strm->avail_out)) {
2076*0a6a1f1dSLionel Sambuc         state->mode = MEM;
2077*0a6a1f1dSLionel Sambuc         return Z_MEM_ERROR;
2078*0a6a1f1dSLionel Sambuc     }
2079*0a6a1f1dSLionel Sambuc     if (dictLength > state->wsize) {
2080*0a6a1f1dSLionel Sambuc         zmemcpy(state->window, dictionary + dictLength - state->wsize,
2081*0a6a1f1dSLionel Sambuc                 state->wsize);
2082*0a6a1f1dSLionel Sambuc         state->whave = state->wsize;
2083*0a6a1f1dSLionel Sambuc     }
2084*0a6a1f1dSLionel Sambuc     else {
2085*0a6a1f1dSLionel Sambuc         zmemcpy(state->window + state->wsize - dictLength, dictionary,
2086*0a6a1f1dSLionel Sambuc                 dictLength);
2087*0a6a1f1dSLionel Sambuc         state->whave = dictLength;
2088*0a6a1f1dSLionel Sambuc     }
2089*0a6a1f1dSLionel Sambuc     state->havedict = 1;
2090*0a6a1f1dSLionel Sambuc     Tracev((stderr, "inflate:   dictionary set\n"));
2091*0a6a1f1dSLionel Sambuc     return Z_OK;
2092*0a6a1f1dSLionel Sambuc }
2093*0a6a1f1dSLionel Sambuc 
2094*0a6a1f1dSLionel Sambuc int ZEXPORT
inflateGetHeader(z_streamp strm,gz_headerp head)2095*0a6a1f1dSLionel Sambuc inflateGetHeader(z_streamp strm, gz_headerp head)
2096*0a6a1f1dSLionel Sambuc {
2097*0a6a1f1dSLionel Sambuc     struct inflate_state FAR *state;
2098*0a6a1f1dSLionel Sambuc 
2099*0a6a1f1dSLionel Sambuc     /* check state */
2100*0a6a1f1dSLionel Sambuc     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
2101*0a6a1f1dSLionel Sambuc     state = (struct inflate_state FAR *)(void *)strm->state;
2102*0a6a1f1dSLionel Sambuc     if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
2103*0a6a1f1dSLionel Sambuc 
2104*0a6a1f1dSLionel Sambuc     /* save header structure */
2105*0a6a1f1dSLionel Sambuc     state->head = head;
2106*0a6a1f1dSLionel Sambuc     head->done = 0;
2107*0a6a1f1dSLionel Sambuc     return Z_OK;
2108*0a6a1f1dSLionel Sambuc }
2109*0a6a1f1dSLionel Sambuc 
2110*0a6a1f1dSLionel Sambuc /*
2111*0a6a1f1dSLionel Sambuc    Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
2112*0a6a1f1dSLionel Sambuc    or when out of input.  When called, *have is the number of pattern bytes
2113*0a6a1f1dSLionel Sambuc    found in order so far, in 0..3.  On return *have is updated to the new
2114*0a6a1f1dSLionel Sambuc    state.  If on return *have equals four, then the pattern was found and the
2115*0a6a1f1dSLionel Sambuc    return value is how many bytes were read including the last byte of the
2116*0a6a1f1dSLionel Sambuc    pattern.  If *have is less than four, then the pattern has not been found
2117*0a6a1f1dSLionel Sambuc    yet and the return value is len.  In the latter case, syncsearch() can be
2118*0a6a1f1dSLionel Sambuc    called again with more data and the *have state.  *have is initialized to
2119*0a6a1f1dSLionel Sambuc    zero for the first call.
2120*0a6a1f1dSLionel Sambuc  */
2121*0a6a1f1dSLionel Sambuc local unsigned
syncsearch(unsigned FAR * have,unsigned char FAR * buf,unsigned len)2122*0a6a1f1dSLionel Sambuc syncsearch(unsigned FAR *have, unsigned char FAR *buf, unsigned len)
2123*0a6a1f1dSLionel Sambuc {
2124*0a6a1f1dSLionel Sambuc     unsigned got;
2125*0a6a1f1dSLionel Sambuc     unsigned next;
2126*0a6a1f1dSLionel Sambuc 
2127*0a6a1f1dSLionel Sambuc     got = *have;
2128*0a6a1f1dSLionel Sambuc     next = 0;
2129*0a6a1f1dSLionel Sambuc     while (next < len && got < 4) {
2130*0a6a1f1dSLionel Sambuc         if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
2131*0a6a1f1dSLionel Sambuc             got++;
2132*0a6a1f1dSLionel Sambuc         else if (buf[next])
2133*0a6a1f1dSLionel Sambuc             got = 0;
2134*0a6a1f1dSLionel Sambuc         else
2135*0a6a1f1dSLionel Sambuc             got = 4 - got;
2136*0a6a1f1dSLionel Sambuc         next++;
2137*0a6a1f1dSLionel Sambuc     }
2138*0a6a1f1dSLionel Sambuc     *have = got;
2139*0a6a1f1dSLionel Sambuc     return next;
2140*0a6a1f1dSLionel Sambuc }
2141*0a6a1f1dSLionel Sambuc 
2142*0a6a1f1dSLionel Sambuc int ZEXPORT
inflateSync(z_streamp strm)2143*0a6a1f1dSLionel Sambuc inflateSync(z_streamp strm)
2144*0a6a1f1dSLionel Sambuc {
2145*0a6a1f1dSLionel Sambuc     unsigned len;               /* number of bytes to look at or looked at */
2146*0a6a1f1dSLionel Sambuc     unsigned long in, out;      /* temporary to save total_in and total_out */
2147*0a6a1f1dSLionel Sambuc     unsigned char buf[4];       /* to restore bit buffer to byte string */
2148*0a6a1f1dSLionel Sambuc     struct inflate_state FAR *state;
2149*0a6a1f1dSLionel Sambuc 
2150*0a6a1f1dSLionel Sambuc     /* check parameters */
2151*0a6a1f1dSLionel Sambuc     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
2152*0a6a1f1dSLionel Sambuc     state = (struct inflate_state FAR *)(void *)strm->state;
2153*0a6a1f1dSLionel Sambuc     if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
2154*0a6a1f1dSLionel Sambuc 
2155*0a6a1f1dSLionel Sambuc     /* if first time, start search in bit buffer */
2156*0a6a1f1dSLionel Sambuc     if (state->mode != SYNC) {
2157*0a6a1f1dSLionel Sambuc         state->mode = SYNC;
2158*0a6a1f1dSLionel Sambuc         state->hold <<= state->bits & 7;
2159*0a6a1f1dSLionel Sambuc         state->bits -= state->bits & 7;
2160*0a6a1f1dSLionel Sambuc         len = 0;
2161*0a6a1f1dSLionel Sambuc         while (state->bits >= 8) {
2162*0a6a1f1dSLionel Sambuc             buf[len++] = (unsigned char)(state->hold);
2163*0a6a1f1dSLionel Sambuc             state->hold >>= 8;
2164*0a6a1f1dSLionel Sambuc             state->bits -= 8;
2165*0a6a1f1dSLionel Sambuc         }
2166*0a6a1f1dSLionel Sambuc         state->have = 0;
2167*0a6a1f1dSLionel Sambuc         syncsearch(&(state->have), buf, len);
2168*0a6a1f1dSLionel Sambuc     }
2169*0a6a1f1dSLionel Sambuc 
2170*0a6a1f1dSLionel Sambuc     /* search available input */
2171*0a6a1f1dSLionel Sambuc     len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
2172*0a6a1f1dSLionel Sambuc     strm->avail_in -= len;
2173*0a6a1f1dSLionel Sambuc     strm->next_in += len;
2174*0a6a1f1dSLionel Sambuc     strm->total_in += len;
2175*0a6a1f1dSLionel Sambuc 
2176*0a6a1f1dSLionel Sambuc     /* return no joy or set up to restart inflate() on a new block */
2177*0a6a1f1dSLionel Sambuc     if (state->have != 4) return Z_DATA_ERROR;
2178*0a6a1f1dSLionel Sambuc     in = strm->total_in;  out = strm->total_out;
2179*0a6a1f1dSLionel Sambuc     inflateReset(strm);
2180*0a6a1f1dSLionel Sambuc     strm->total_in = in;  strm->total_out = out;
2181*0a6a1f1dSLionel Sambuc     state->mode = TYPE;
2182*0a6a1f1dSLionel Sambuc     return Z_OK;
2183*0a6a1f1dSLionel Sambuc }
2184*0a6a1f1dSLionel Sambuc 
2185*0a6a1f1dSLionel Sambuc /*
2186*0a6a1f1dSLionel Sambuc    Returns true if inflate is currently at the end of a block generated by
2187*0a6a1f1dSLionel Sambuc    Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
2188*0a6a1f1dSLionel Sambuc    implementation to provide an additional safety check. PPP uses
2189*0a6a1f1dSLionel Sambuc    Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
2190*0a6a1f1dSLionel Sambuc    block. When decompressing, PPP checks that at the end of input packet,
2191*0a6a1f1dSLionel Sambuc    inflate is waiting for these length bytes.
2192*0a6a1f1dSLionel Sambuc  */
2193*0a6a1f1dSLionel Sambuc int ZEXPORT
inflateSyncPoint(z_streamp strm)2194*0a6a1f1dSLionel Sambuc inflateSyncPoint(z_streamp strm)
2195*0a6a1f1dSLionel Sambuc {
2196*0a6a1f1dSLionel Sambuc     struct inflate_state FAR *state;
2197*0a6a1f1dSLionel Sambuc 
2198*0a6a1f1dSLionel Sambuc     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
2199*0a6a1f1dSLionel Sambuc     state = (struct inflate_state FAR *)(void *)strm->state;
2200*0a6a1f1dSLionel Sambuc     return state->mode == STORED && state->bits == 0;
2201*0a6a1f1dSLionel Sambuc }
2202*0a6a1f1dSLionel Sambuc 
2203*0a6a1f1dSLionel Sambuc int ZEXPORT
inflateCopy(z_streamp dest,z_streamp source)2204*0a6a1f1dSLionel Sambuc inflateCopy(z_streamp dest, z_streamp source)
2205*0a6a1f1dSLionel Sambuc {
2206*0a6a1f1dSLionel Sambuc     struct inflate_state FAR *state;
2207*0a6a1f1dSLionel Sambuc     struct inflate_state FAR *copy;
2208*0a6a1f1dSLionel Sambuc     unsigned char FAR *window;
2209*0a6a1f1dSLionel Sambuc     unsigned wsize;
2210*0a6a1f1dSLionel Sambuc 
2211*0a6a1f1dSLionel Sambuc     /* check input */
2212*0a6a1f1dSLionel Sambuc     if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
2213*0a6a1f1dSLionel Sambuc         source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
2214*0a6a1f1dSLionel Sambuc         return Z_STREAM_ERROR;
2215*0a6a1f1dSLionel Sambuc     state = (struct inflate_state FAR *)(void *)source->state;
2216*0a6a1f1dSLionel Sambuc 
2217*0a6a1f1dSLionel Sambuc     /* allocate space */
2218*0a6a1f1dSLionel Sambuc     copy = (struct inflate_state FAR *)
2219*0a6a1f1dSLionel Sambuc            ZALLOC(source, 1, sizeof(struct inflate_state));
2220*0a6a1f1dSLionel Sambuc     if (copy == Z_NULL) return Z_MEM_ERROR;
2221*0a6a1f1dSLionel Sambuc     window = Z_NULL;
2222*0a6a1f1dSLionel Sambuc     if (state->window != Z_NULL) {
2223*0a6a1f1dSLionel Sambuc         window = (unsigned char FAR *)
2224*0a6a1f1dSLionel Sambuc                  ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
2225*0a6a1f1dSLionel Sambuc         if (window == Z_NULL) {
2226*0a6a1f1dSLionel Sambuc             ZFREE(source, copy);
2227*0a6a1f1dSLionel Sambuc             return Z_MEM_ERROR;
2228*0a6a1f1dSLionel Sambuc         }
2229*0a6a1f1dSLionel Sambuc     }
2230*0a6a1f1dSLionel Sambuc 
2231*0a6a1f1dSLionel Sambuc     /* copy state */
2232*0a6a1f1dSLionel Sambuc     zmemcpy(dest, source, sizeof(z_stream));
2233*0a6a1f1dSLionel Sambuc     zmemcpy(copy, state, sizeof(struct inflate_state));
2234*0a6a1f1dSLionel Sambuc     if (state->lencode >= state->codes &&
2235*0a6a1f1dSLionel Sambuc         state->lencode <= state->codes + ENOUGH - 1) {
2236*0a6a1f1dSLionel Sambuc         copy->lencode = copy->codes + (state->lencode - state->codes);
2237*0a6a1f1dSLionel Sambuc         copy->distcode = copy->codes + (state->distcode - state->codes);
2238*0a6a1f1dSLionel Sambuc     }
2239*0a6a1f1dSLionel Sambuc     copy->next = copy->codes + (state->next - state->codes);
2240*0a6a1f1dSLionel Sambuc     if (window != Z_NULL) {
2241*0a6a1f1dSLionel Sambuc         wsize = 1U << state->wbits;
2242*0a6a1f1dSLionel Sambuc         zmemcpy(window, state->window, wsize);
2243*0a6a1f1dSLionel Sambuc     }
2244*0a6a1f1dSLionel Sambuc     copy->window = window;
2245*0a6a1f1dSLionel Sambuc     dest->state = (struct internal_state FAR *)copy;
2246*0a6a1f1dSLionel Sambuc     return Z_OK;
2247*0a6a1f1dSLionel Sambuc }
2248