xref: /freebsd/sys/contrib/zlib/infback.c (revision c9083b850aed19fff781fd82084e8308c9ae6df6)
1*c9083b85SXin LI /* infback.c -- inflate using a call-back interface
2*c9083b85SXin LI  * Copyright (C) 1995-2016 Mark Adler
3*c9083b85SXin LI  * For conditions of distribution and use, see copyright notice in zlib.h
4*c9083b85SXin LI  */
5*c9083b85SXin LI 
6*c9083b85SXin LI /*
7*c9083b85SXin LI    This code is largely copied from inflate.c.  Normally either infback.o or
8*c9083b85SXin LI    inflate.o would be linked into an application--not both.  The interface
9*c9083b85SXin LI    with inffast.c is retained so that optimized assembler-coded versions of
10*c9083b85SXin LI    inflate_fast() can be used with either inflate.c or infback.c.
11*c9083b85SXin LI  */
12*c9083b85SXin LI 
13*c9083b85SXin LI #include "zutil.h"
14*c9083b85SXin LI #include "inftrees.h"
15*c9083b85SXin LI #include "inflate.h"
16*c9083b85SXin LI #include "inffast.h"
17*c9083b85SXin LI 
18*c9083b85SXin LI /* function prototypes */
19*c9083b85SXin LI local void fixedtables OF((struct inflate_state FAR *state));
20*c9083b85SXin LI 
21*c9083b85SXin LI /*
22*c9083b85SXin LI    strm provides memory allocation functions in zalloc and zfree, or
23*c9083b85SXin LI    Z_NULL to use the library memory allocation functions.
24*c9083b85SXin LI 
25*c9083b85SXin LI    windowBits is in the range 8..15, and window is a user-supplied
26*c9083b85SXin LI    window and output buffer that is 2**windowBits bytes.
27*c9083b85SXin LI  */
28*c9083b85SXin LI int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
29*c9083b85SXin LI z_streamp strm;
30*c9083b85SXin LI int windowBits;
31*c9083b85SXin LI unsigned char FAR *window;
32*c9083b85SXin LI const char *version;
33*c9083b85SXin LI int stream_size;
34*c9083b85SXin LI {
35*c9083b85SXin LI     struct inflate_state FAR *state;
36*c9083b85SXin LI 
37*c9083b85SXin LI     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
38*c9083b85SXin LI         stream_size != (int)(sizeof(z_stream)))
39*c9083b85SXin LI         return Z_VERSION_ERROR;
40*c9083b85SXin LI     if (strm == Z_NULL || window == Z_NULL ||
41*c9083b85SXin LI         windowBits < 8 || windowBits > 15)
42*c9083b85SXin LI         return Z_STREAM_ERROR;
43*c9083b85SXin LI     strm->msg = Z_NULL;                 /* in case we return an error */
44*c9083b85SXin LI     if (strm->zalloc == (alloc_func)0) {
45*c9083b85SXin LI #ifdef Z_SOLO
46*c9083b85SXin LI         return Z_STREAM_ERROR;
47*c9083b85SXin LI #else
48*c9083b85SXin LI         strm->zalloc = zcalloc;
49*c9083b85SXin LI         strm->opaque = (voidpf)0;
50*c9083b85SXin LI #endif
51*c9083b85SXin LI     }
52*c9083b85SXin LI     if (strm->zfree == (free_func)0)
53*c9083b85SXin LI #ifdef Z_SOLO
54*c9083b85SXin LI         return Z_STREAM_ERROR;
55*c9083b85SXin LI #else
56*c9083b85SXin LI     strm->zfree = zcfree;
57*c9083b85SXin LI #endif
58*c9083b85SXin LI     state = (struct inflate_state FAR *)ZALLOC(strm, 1,
59*c9083b85SXin LI                                                sizeof(struct inflate_state));
60*c9083b85SXin LI     if (state == Z_NULL) return Z_MEM_ERROR;
61*c9083b85SXin LI     Tracev((stderr, "inflate: allocated\n"));
62*c9083b85SXin LI     strm->state = (struct internal_state FAR *)state;
63*c9083b85SXin LI     state->dmax = 32768U;
64*c9083b85SXin LI     state->wbits = (uInt)windowBits;
65*c9083b85SXin LI     state->wsize = 1U << windowBits;
66*c9083b85SXin LI     state->window = window;
67*c9083b85SXin LI     state->wnext = 0;
68*c9083b85SXin LI     state->whave = 0;
69*c9083b85SXin LI     return Z_OK;
70*c9083b85SXin LI }
71*c9083b85SXin LI 
72*c9083b85SXin LI /*
73*c9083b85SXin LI    Return state with length and distance decoding tables and index sizes set to
74*c9083b85SXin LI    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
75*c9083b85SXin LI    If BUILDFIXED is defined, then instead this routine builds the tables the
76*c9083b85SXin LI    first time it's called, and returns those tables the first time and
77*c9083b85SXin LI    thereafter.  This reduces the size of the code by about 2K bytes, in
78*c9083b85SXin LI    exchange for a little execution time.  However, BUILDFIXED should not be
79*c9083b85SXin LI    used for threaded applications, since the rewriting of the tables and virgin
80*c9083b85SXin LI    may not be thread-safe.
81*c9083b85SXin LI  */
82*c9083b85SXin LI local void fixedtables(state)
83*c9083b85SXin LI struct inflate_state FAR *state;
84*c9083b85SXin LI {
85*c9083b85SXin LI #ifdef BUILDFIXED
86*c9083b85SXin LI     static int virgin = 1;
87*c9083b85SXin LI     static code *lenfix, *distfix;
88*c9083b85SXin LI     static code fixed[544];
89*c9083b85SXin LI 
90*c9083b85SXin LI     /* build fixed huffman tables if first call (may not be thread safe) */
91*c9083b85SXin LI     if (virgin) {
92*c9083b85SXin LI         unsigned sym, bits;
93*c9083b85SXin LI         static code *next;
94*c9083b85SXin LI 
95*c9083b85SXin LI         /* literal/length table */
96*c9083b85SXin LI         sym = 0;
97*c9083b85SXin LI         while (sym < 144) state->lens[sym++] = 8;
98*c9083b85SXin LI         while (sym < 256) state->lens[sym++] = 9;
99*c9083b85SXin LI         while (sym < 280) state->lens[sym++] = 7;
100*c9083b85SXin LI         while (sym < 288) state->lens[sym++] = 8;
101*c9083b85SXin LI         next = fixed;
102*c9083b85SXin LI         lenfix = next;
103*c9083b85SXin LI         bits = 9;
104*c9083b85SXin LI         inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
105*c9083b85SXin LI 
106*c9083b85SXin LI         /* distance table */
107*c9083b85SXin LI         sym = 0;
108*c9083b85SXin LI         while (sym < 32) state->lens[sym++] = 5;
109*c9083b85SXin LI         distfix = next;
110*c9083b85SXin LI         bits = 5;
111*c9083b85SXin LI         inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
112*c9083b85SXin LI 
113*c9083b85SXin LI         /* do this just once */
114*c9083b85SXin LI         virgin = 0;
115*c9083b85SXin LI     }
116*c9083b85SXin LI #else /* !BUILDFIXED */
117*c9083b85SXin LI #   include "inffixed.h"
118*c9083b85SXin LI #endif /* BUILDFIXED */
119*c9083b85SXin LI     state->lencode = lenfix;
120*c9083b85SXin LI     state->lenbits = 9;
121*c9083b85SXin LI     state->distcode = distfix;
122*c9083b85SXin LI     state->distbits = 5;
123*c9083b85SXin LI }
124*c9083b85SXin LI 
125*c9083b85SXin LI /* Macros for inflateBack(): */
126*c9083b85SXin LI 
127*c9083b85SXin LI /* Load returned state from inflate_fast() */
128*c9083b85SXin LI #define LOAD() \
129*c9083b85SXin LI     do { \
130*c9083b85SXin LI         put = strm->next_out; \
131*c9083b85SXin LI         left = strm->avail_out; \
132*c9083b85SXin LI         next = strm->next_in; \
133*c9083b85SXin LI         have = strm->avail_in; \
134*c9083b85SXin LI         hold = state->hold; \
135*c9083b85SXin LI         bits = state->bits; \
136*c9083b85SXin LI     } while (0)
137*c9083b85SXin LI 
138*c9083b85SXin LI /* Set state from registers for inflate_fast() */
139*c9083b85SXin LI #define RESTORE() \
140*c9083b85SXin LI     do { \
141*c9083b85SXin LI         strm->next_out = put; \
142*c9083b85SXin LI         strm->avail_out = left; \
143*c9083b85SXin LI         strm->next_in = next; \
144*c9083b85SXin LI         strm->avail_in = have; \
145*c9083b85SXin LI         state->hold = hold; \
146*c9083b85SXin LI         state->bits = bits; \
147*c9083b85SXin LI     } while (0)
148*c9083b85SXin LI 
149*c9083b85SXin LI /* Clear the input bit accumulator */
150*c9083b85SXin LI #define INITBITS() \
151*c9083b85SXin LI     do { \
152*c9083b85SXin LI         hold = 0; \
153*c9083b85SXin LI         bits = 0; \
154*c9083b85SXin LI     } while (0)
155*c9083b85SXin LI 
156*c9083b85SXin LI /* Assure that some input is available.  If input is requested, but denied,
157*c9083b85SXin LI    then return a Z_BUF_ERROR from inflateBack(). */
158*c9083b85SXin LI #define PULL() \
159*c9083b85SXin LI     do { \
160*c9083b85SXin LI         if (have == 0) { \
161*c9083b85SXin LI             have = in(in_desc, &next); \
162*c9083b85SXin LI             if (have == 0) { \
163*c9083b85SXin LI                 next = Z_NULL; \
164*c9083b85SXin LI                 ret = Z_BUF_ERROR; \
165*c9083b85SXin LI                 goto inf_leave; \
166*c9083b85SXin LI             } \
167*c9083b85SXin LI         } \
168*c9083b85SXin LI     } while (0)
169*c9083b85SXin LI 
170*c9083b85SXin LI /* Get a byte of input into the bit accumulator, or return from inflateBack()
171*c9083b85SXin LI    with an error if there is no input available. */
172*c9083b85SXin LI #define PULLBYTE() \
173*c9083b85SXin LI     do { \
174*c9083b85SXin LI         PULL(); \
175*c9083b85SXin LI         have--; \
176*c9083b85SXin LI         hold += (unsigned long)(*next++) << bits; \
177*c9083b85SXin LI         bits += 8; \
178*c9083b85SXin LI     } while (0)
179*c9083b85SXin LI 
180*c9083b85SXin LI /* Assure that there are at least n bits in the bit accumulator.  If there is
181*c9083b85SXin LI    not enough available input to do that, then return from inflateBack() with
182*c9083b85SXin LI    an error. */
183*c9083b85SXin LI #define NEEDBITS(n) \
184*c9083b85SXin LI     do { \
185*c9083b85SXin LI         while (bits < (unsigned)(n)) \
186*c9083b85SXin LI             PULLBYTE(); \
187*c9083b85SXin LI     } while (0)
188*c9083b85SXin LI 
189*c9083b85SXin LI /* Return the low n bits of the bit accumulator (n < 16) */
190*c9083b85SXin LI #define BITS(n) \
191*c9083b85SXin LI     ((unsigned)hold & ((1U << (n)) - 1))
192*c9083b85SXin LI 
193*c9083b85SXin LI /* Remove n bits from the bit accumulator */
194*c9083b85SXin LI #define DROPBITS(n) \
195*c9083b85SXin LI     do { \
196*c9083b85SXin LI         hold >>= (n); \
197*c9083b85SXin LI         bits -= (unsigned)(n); \
198*c9083b85SXin LI     } while (0)
199*c9083b85SXin LI 
200*c9083b85SXin LI /* Remove zero to seven bits as needed to go to a byte boundary */
201*c9083b85SXin LI #define BYTEBITS() \
202*c9083b85SXin LI     do { \
203*c9083b85SXin LI         hold >>= bits & 7; \
204*c9083b85SXin LI         bits -= bits & 7; \
205*c9083b85SXin LI     } while (0)
206*c9083b85SXin LI 
207*c9083b85SXin LI /* Assure that some output space is available, by writing out the window
208*c9083b85SXin LI    if it's full.  If the write fails, return from inflateBack() with a
209*c9083b85SXin LI    Z_BUF_ERROR. */
210*c9083b85SXin LI #define ROOM() \
211*c9083b85SXin LI     do { \
212*c9083b85SXin LI         if (left == 0) { \
213*c9083b85SXin LI             put = state->window; \
214*c9083b85SXin LI             left = state->wsize; \
215*c9083b85SXin LI             state->whave = left; \
216*c9083b85SXin LI             if (out(out_desc, put, left)) { \
217*c9083b85SXin LI                 ret = Z_BUF_ERROR; \
218*c9083b85SXin LI                 goto inf_leave; \
219*c9083b85SXin LI             } \
220*c9083b85SXin LI         } \
221*c9083b85SXin LI     } while (0)
222*c9083b85SXin LI 
223*c9083b85SXin LI /*
224*c9083b85SXin LI    strm provides the memory allocation functions and window buffer on input,
225*c9083b85SXin LI    and provides information on the unused input on return.  For Z_DATA_ERROR
226*c9083b85SXin LI    returns, strm will also provide an error message.
227*c9083b85SXin LI 
228*c9083b85SXin LI    in() and out() are the call-back input and output functions.  When
229*c9083b85SXin LI    inflateBack() needs more input, it calls in().  When inflateBack() has
230*c9083b85SXin LI    filled the window with output, or when it completes with data in the
231*c9083b85SXin LI    window, it calls out() to write out the data.  The application must not
232*c9083b85SXin LI    change the provided input until in() is called again or inflateBack()
233*c9083b85SXin LI    returns.  The application must not change the window/output buffer until
234*c9083b85SXin LI    inflateBack() returns.
235*c9083b85SXin LI 
236*c9083b85SXin LI    in() and out() are called with a descriptor parameter provided in the
237*c9083b85SXin LI    inflateBack() call.  This parameter can be a structure that provides the
238*c9083b85SXin LI    information required to do the read or write, as well as accumulated
239*c9083b85SXin LI    information on the input and output such as totals and check values.
240*c9083b85SXin LI 
241*c9083b85SXin LI    in() should return zero on failure.  out() should return non-zero on
242*c9083b85SXin LI    failure.  If either in() or out() fails, than inflateBack() returns a
243*c9083b85SXin LI    Z_BUF_ERROR.  strm->next_in can be checked for Z_NULL to see whether it
244*c9083b85SXin LI    was in() or out() that caused in the error.  Otherwise,  inflateBack()
245*c9083b85SXin LI    returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
246*c9083b85SXin LI    error, or Z_MEM_ERROR if it could not allocate memory for the state.
247*c9083b85SXin LI    inflateBack() can also return Z_STREAM_ERROR if the input parameters
248*c9083b85SXin LI    are not correct, i.e. strm is Z_NULL or the state was not initialized.
249*c9083b85SXin LI  */
250*c9083b85SXin LI int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
251*c9083b85SXin LI z_streamp strm;
252*c9083b85SXin LI in_func in;
253*c9083b85SXin LI void FAR *in_desc;
254*c9083b85SXin LI out_func out;
255*c9083b85SXin LI void FAR *out_desc;
256*c9083b85SXin LI {
257*c9083b85SXin LI     struct inflate_state FAR *state;
258*c9083b85SXin LI     z_const unsigned char FAR *next;    /* next input */
259*c9083b85SXin LI     unsigned char FAR *put;     /* next output */
260*c9083b85SXin LI     unsigned have, left;        /* available input and output */
261*c9083b85SXin LI     unsigned long hold;         /* bit buffer */
262*c9083b85SXin LI     unsigned bits;              /* bits in bit buffer */
263*c9083b85SXin LI     unsigned copy;              /* number of stored or match bytes to copy */
264*c9083b85SXin LI     unsigned char FAR *from;    /* where to copy match bytes from */
265*c9083b85SXin LI     code here;                  /* current decoding table entry */
266*c9083b85SXin LI     code last;                  /* parent table entry */
267*c9083b85SXin LI     unsigned len;               /* length to copy for repeats, bits to drop */
268*c9083b85SXin LI     int ret;                    /* return code */
269*c9083b85SXin LI     static const unsigned short order[19] = /* permutation of code lengths */
270*c9083b85SXin LI         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
271*c9083b85SXin LI 
272*c9083b85SXin LI     /* Check that the strm exists and that the state was initialized */
273*c9083b85SXin LI     if (strm == Z_NULL || strm->state == Z_NULL)
274*c9083b85SXin LI         return Z_STREAM_ERROR;
275*c9083b85SXin LI     state = (struct inflate_state FAR *)strm->state;
276*c9083b85SXin LI 
277*c9083b85SXin LI     /* Reset the state */
278*c9083b85SXin LI     strm->msg = Z_NULL;
279*c9083b85SXin LI     state->mode = TYPE;
280*c9083b85SXin LI     state->last = 0;
281*c9083b85SXin LI     state->whave = 0;
282*c9083b85SXin LI     next = strm->next_in;
283*c9083b85SXin LI     have = next != Z_NULL ? strm->avail_in : 0;
284*c9083b85SXin LI     hold = 0;
285*c9083b85SXin LI     bits = 0;
286*c9083b85SXin LI     put = state->window;
287*c9083b85SXin LI     left = state->wsize;
288*c9083b85SXin LI 
289*c9083b85SXin LI     /* Inflate until end of block marked as last */
290*c9083b85SXin LI     for (;;)
291*c9083b85SXin LI         switch (state->mode) {
292*c9083b85SXin LI         case TYPE:
293*c9083b85SXin LI             /* determine and dispatch block type */
294*c9083b85SXin LI             if (state->last) {
295*c9083b85SXin LI                 BYTEBITS();
296*c9083b85SXin LI                 state->mode = DONE;
297*c9083b85SXin LI                 break;
298*c9083b85SXin LI             }
299*c9083b85SXin LI             NEEDBITS(3);
300*c9083b85SXin LI             state->last = BITS(1);
301*c9083b85SXin LI             DROPBITS(1);
302*c9083b85SXin LI             switch (BITS(2)) {
303*c9083b85SXin LI             case 0:                             /* stored block */
304*c9083b85SXin LI                 Tracev((stderr, "inflate:     stored block%s\n",
305*c9083b85SXin LI                         state->last ? " (last)" : ""));
306*c9083b85SXin LI                 state->mode = STORED;
307*c9083b85SXin LI                 break;
308*c9083b85SXin LI             case 1:                             /* fixed block */
309*c9083b85SXin LI                 fixedtables(state);
310*c9083b85SXin LI                 Tracev((stderr, "inflate:     fixed codes block%s\n",
311*c9083b85SXin LI                         state->last ? " (last)" : ""));
312*c9083b85SXin LI                 state->mode = LEN;              /* decode codes */
313*c9083b85SXin LI                 break;
314*c9083b85SXin LI             case 2:                             /* dynamic block */
315*c9083b85SXin LI                 Tracev((stderr, "inflate:     dynamic codes block%s\n",
316*c9083b85SXin LI                         state->last ? " (last)" : ""));
317*c9083b85SXin LI                 state->mode = TABLE;
318*c9083b85SXin LI                 break;
319*c9083b85SXin LI             case 3:
320*c9083b85SXin LI                 strm->msg = (char *)"invalid block type";
321*c9083b85SXin LI                 state->mode = BAD;
322*c9083b85SXin LI             }
323*c9083b85SXin LI             DROPBITS(2);
324*c9083b85SXin LI             break;
325*c9083b85SXin LI 
326*c9083b85SXin LI         case STORED:
327*c9083b85SXin LI             /* get and verify stored block length */
328*c9083b85SXin LI             BYTEBITS();                         /* go to byte boundary */
329*c9083b85SXin LI             NEEDBITS(32);
330*c9083b85SXin LI             if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
331*c9083b85SXin LI                 strm->msg = (char *)"invalid stored block lengths";
332*c9083b85SXin LI                 state->mode = BAD;
333*c9083b85SXin LI                 break;
334*c9083b85SXin LI             }
335*c9083b85SXin LI             state->length = (unsigned)hold & 0xffff;
336*c9083b85SXin LI             Tracev((stderr, "inflate:       stored length %u\n",
337*c9083b85SXin LI                     state->length));
338*c9083b85SXin LI             INITBITS();
339*c9083b85SXin LI 
340*c9083b85SXin LI             /* copy stored block from input to output */
341*c9083b85SXin LI             while (state->length != 0) {
342*c9083b85SXin LI                 copy = state->length;
343*c9083b85SXin LI                 PULL();
344*c9083b85SXin LI                 ROOM();
345*c9083b85SXin LI                 if (copy > have) copy = have;
346*c9083b85SXin LI                 if (copy > left) copy = left;
347*c9083b85SXin LI                 zmemcpy(put, next, copy);
348*c9083b85SXin LI                 have -= copy;
349*c9083b85SXin LI                 next += copy;
350*c9083b85SXin LI                 left -= copy;
351*c9083b85SXin LI                 put += copy;
352*c9083b85SXin LI                 state->length -= copy;
353*c9083b85SXin LI             }
354*c9083b85SXin LI             Tracev((stderr, "inflate:       stored end\n"));
355*c9083b85SXin LI             state->mode = TYPE;
356*c9083b85SXin LI             break;
357*c9083b85SXin LI 
358*c9083b85SXin LI         case TABLE:
359*c9083b85SXin LI             /* get dynamic table entries descriptor */
360*c9083b85SXin LI             NEEDBITS(14);
361*c9083b85SXin LI             state->nlen = BITS(5) + 257;
362*c9083b85SXin LI             DROPBITS(5);
363*c9083b85SXin LI             state->ndist = BITS(5) + 1;
364*c9083b85SXin LI             DROPBITS(5);
365*c9083b85SXin LI             state->ncode = BITS(4) + 4;
366*c9083b85SXin LI             DROPBITS(4);
367*c9083b85SXin LI #ifndef PKZIP_BUG_WORKAROUND
368*c9083b85SXin LI             if (state->nlen > 286 || state->ndist > 30) {
369*c9083b85SXin LI                 strm->msg = (char *)"too many length or distance symbols";
370*c9083b85SXin LI                 state->mode = BAD;
371*c9083b85SXin LI                 break;
372*c9083b85SXin LI             }
373*c9083b85SXin LI #endif
374*c9083b85SXin LI             Tracev((stderr, "inflate:       table sizes ok\n"));
375*c9083b85SXin LI 
376*c9083b85SXin LI             /* get code length code lengths (not a typo) */
377*c9083b85SXin LI             state->have = 0;
378*c9083b85SXin LI             while (state->have < state->ncode) {
379*c9083b85SXin LI                 NEEDBITS(3);
380*c9083b85SXin LI                 state->lens[order[state->have++]] = (unsigned short)BITS(3);
381*c9083b85SXin LI                 DROPBITS(3);
382*c9083b85SXin LI             }
383*c9083b85SXin LI             while (state->have < 19)
384*c9083b85SXin LI                 state->lens[order[state->have++]] = 0;
385*c9083b85SXin LI             state->next = state->codes;
386*c9083b85SXin LI             state->lencode = (code const FAR *)(state->next);
387*c9083b85SXin LI             state->lenbits = 7;
388*c9083b85SXin LI             ret = inflate_table(CODES, state->lens, 19, &(state->next),
389*c9083b85SXin LI                                 &(state->lenbits), state->work);
390*c9083b85SXin LI             if (ret) {
391*c9083b85SXin LI                 strm->msg = (char *)"invalid code lengths set";
392*c9083b85SXin LI                 state->mode = BAD;
393*c9083b85SXin LI                 break;
394*c9083b85SXin LI             }
395*c9083b85SXin LI             Tracev((stderr, "inflate:       code lengths ok\n"));
396*c9083b85SXin LI 
397*c9083b85SXin LI             /* get length and distance code code lengths */
398*c9083b85SXin LI             state->have = 0;
399*c9083b85SXin LI             while (state->have < state->nlen + state->ndist) {
400*c9083b85SXin LI                 for (;;) {
401*c9083b85SXin LI                     here = state->lencode[BITS(state->lenbits)];
402*c9083b85SXin LI                     if ((unsigned)(here.bits) <= bits) break;
403*c9083b85SXin LI                     PULLBYTE();
404*c9083b85SXin LI                 }
405*c9083b85SXin LI                 if (here.val < 16) {
406*c9083b85SXin LI                     DROPBITS(here.bits);
407*c9083b85SXin LI                     state->lens[state->have++] = here.val;
408*c9083b85SXin LI                 }
409*c9083b85SXin LI                 else {
410*c9083b85SXin LI                     if (here.val == 16) {
411*c9083b85SXin LI                         NEEDBITS(here.bits + 2);
412*c9083b85SXin LI                         DROPBITS(here.bits);
413*c9083b85SXin LI                         if (state->have == 0) {
414*c9083b85SXin LI                             strm->msg = (char *)"invalid bit length repeat";
415*c9083b85SXin LI                             state->mode = BAD;
416*c9083b85SXin LI                             break;
417*c9083b85SXin LI                         }
418*c9083b85SXin LI                         len = (unsigned)(state->lens[state->have - 1]);
419*c9083b85SXin LI                         copy = 3 + BITS(2);
420*c9083b85SXin LI                         DROPBITS(2);
421*c9083b85SXin LI                     }
422*c9083b85SXin LI                     else if (here.val == 17) {
423*c9083b85SXin LI                         NEEDBITS(here.bits + 3);
424*c9083b85SXin LI                         DROPBITS(here.bits);
425*c9083b85SXin LI                         len = 0;
426*c9083b85SXin LI                         copy = 3 + BITS(3);
427*c9083b85SXin LI                         DROPBITS(3);
428*c9083b85SXin LI                     }
429*c9083b85SXin LI                     else {
430*c9083b85SXin LI                         NEEDBITS(here.bits + 7);
431*c9083b85SXin LI                         DROPBITS(here.bits);
432*c9083b85SXin LI                         len = 0;
433*c9083b85SXin LI                         copy = 11 + BITS(7);
434*c9083b85SXin LI                         DROPBITS(7);
435*c9083b85SXin LI                     }
436*c9083b85SXin LI                     if (state->have + copy > state->nlen + state->ndist) {
437*c9083b85SXin LI                         strm->msg = (char *)"invalid bit length repeat";
438*c9083b85SXin LI                         state->mode = BAD;
439*c9083b85SXin LI                         break;
440*c9083b85SXin LI                     }
441*c9083b85SXin LI                     while (copy--)
442*c9083b85SXin LI                         state->lens[state->have++] = (unsigned short)len;
443*c9083b85SXin LI                 }
444*c9083b85SXin LI             }
445*c9083b85SXin LI 
446*c9083b85SXin LI             /* handle error breaks in while */
447*c9083b85SXin LI             if (state->mode == BAD) break;
448*c9083b85SXin LI 
449*c9083b85SXin LI             /* check for end-of-block code (better have one) */
450*c9083b85SXin LI             if (state->lens[256] == 0) {
451*c9083b85SXin LI                 strm->msg = (char *)"invalid code -- missing end-of-block";
452*c9083b85SXin LI                 state->mode = BAD;
453*c9083b85SXin LI                 break;
454*c9083b85SXin LI             }
455*c9083b85SXin LI 
456*c9083b85SXin LI             /* build code tables -- note: do not change the lenbits or distbits
457*c9083b85SXin LI                values here (9 and 6) without reading the comments in inftrees.h
458*c9083b85SXin LI                concerning the ENOUGH constants, which depend on those values */
459*c9083b85SXin LI             state->next = state->codes;
460*c9083b85SXin LI             state->lencode = (code const FAR *)(state->next);
461*c9083b85SXin LI             state->lenbits = 9;
462*c9083b85SXin LI             ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
463*c9083b85SXin LI                                 &(state->lenbits), state->work);
464*c9083b85SXin LI             if (ret) {
465*c9083b85SXin LI                 strm->msg = (char *)"invalid literal/lengths set";
466*c9083b85SXin LI                 state->mode = BAD;
467*c9083b85SXin LI                 break;
468*c9083b85SXin LI             }
469*c9083b85SXin LI             state->distcode = (code const FAR *)(state->next);
470*c9083b85SXin LI             state->distbits = 6;
471*c9083b85SXin LI             ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
472*c9083b85SXin LI                             &(state->next), &(state->distbits), state->work);
473*c9083b85SXin LI             if (ret) {
474*c9083b85SXin LI                 strm->msg = (char *)"invalid distances set";
475*c9083b85SXin LI                 state->mode = BAD;
476*c9083b85SXin LI                 break;
477*c9083b85SXin LI             }
478*c9083b85SXin LI             Tracev((stderr, "inflate:       codes ok\n"));
479*c9083b85SXin LI             state->mode = LEN;
480*c9083b85SXin LI 
481*c9083b85SXin LI         case LEN:
482*c9083b85SXin LI             /* use inflate_fast() if we have enough input and output */
483*c9083b85SXin LI             if (have >= 6 && left >= 258) {
484*c9083b85SXin LI                 RESTORE();
485*c9083b85SXin LI                 if (state->whave < state->wsize)
486*c9083b85SXin LI                     state->whave = state->wsize - left;
487*c9083b85SXin LI                 inflate_fast(strm, state->wsize);
488*c9083b85SXin LI                 LOAD();
489*c9083b85SXin LI                 break;
490*c9083b85SXin LI             }
491*c9083b85SXin LI 
492*c9083b85SXin LI             /* get a literal, length, or end-of-block code */
493*c9083b85SXin LI             for (;;) {
494*c9083b85SXin LI                 here = state->lencode[BITS(state->lenbits)];
495*c9083b85SXin LI                 if ((unsigned)(here.bits) <= bits) break;
496*c9083b85SXin LI                 PULLBYTE();
497*c9083b85SXin LI             }
498*c9083b85SXin LI             if (here.op && (here.op & 0xf0) == 0) {
499*c9083b85SXin LI                 last = here;
500*c9083b85SXin LI                 for (;;) {
501*c9083b85SXin LI                     here = state->lencode[last.val +
502*c9083b85SXin LI                             (BITS(last.bits + last.op) >> last.bits)];
503*c9083b85SXin LI                     if ((unsigned)(last.bits + here.bits) <= bits) break;
504*c9083b85SXin LI                     PULLBYTE();
505*c9083b85SXin LI                 }
506*c9083b85SXin LI                 DROPBITS(last.bits);
507*c9083b85SXin LI             }
508*c9083b85SXin LI             DROPBITS(here.bits);
509*c9083b85SXin LI             state->length = (unsigned)here.val;
510*c9083b85SXin LI 
511*c9083b85SXin LI             /* process literal */
512*c9083b85SXin LI             if (here.op == 0) {
513*c9083b85SXin LI                 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
514*c9083b85SXin LI                         "inflate:         literal '%c'\n" :
515*c9083b85SXin LI                         "inflate:         literal 0x%02x\n", here.val));
516*c9083b85SXin LI                 ROOM();
517*c9083b85SXin LI                 *put++ = (unsigned char)(state->length);
518*c9083b85SXin LI                 left--;
519*c9083b85SXin LI                 state->mode = LEN;
520*c9083b85SXin LI                 break;
521*c9083b85SXin LI             }
522*c9083b85SXin LI 
523*c9083b85SXin LI             /* process end of block */
524*c9083b85SXin LI             if (here.op & 32) {
525*c9083b85SXin LI                 Tracevv((stderr, "inflate:         end of block\n"));
526*c9083b85SXin LI                 state->mode = TYPE;
527*c9083b85SXin LI                 break;
528*c9083b85SXin LI             }
529*c9083b85SXin LI 
530*c9083b85SXin LI             /* invalid code */
531*c9083b85SXin LI             if (here.op & 64) {
532*c9083b85SXin LI                 strm->msg = (char *)"invalid literal/length code";
533*c9083b85SXin LI                 state->mode = BAD;
534*c9083b85SXin LI                 break;
535*c9083b85SXin LI             }
536*c9083b85SXin LI 
537*c9083b85SXin LI             /* length code -- get extra bits, if any */
538*c9083b85SXin LI             state->extra = (unsigned)(here.op) & 15;
539*c9083b85SXin LI             if (state->extra != 0) {
540*c9083b85SXin LI                 NEEDBITS(state->extra);
541*c9083b85SXin LI                 state->length += BITS(state->extra);
542*c9083b85SXin LI                 DROPBITS(state->extra);
543*c9083b85SXin LI             }
544*c9083b85SXin LI             Tracevv((stderr, "inflate:         length %u\n", state->length));
545*c9083b85SXin LI 
546*c9083b85SXin LI             /* get distance code */
547*c9083b85SXin LI             for (;;) {
548*c9083b85SXin LI                 here = state->distcode[BITS(state->distbits)];
549*c9083b85SXin LI                 if ((unsigned)(here.bits) <= bits) break;
550*c9083b85SXin LI                 PULLBYTE();
551*c9083b85SXin LI             }
552*c9083b85SXin LI             if ((here.op & 0xf0) == 0) {
553*c9083b85SXin LI                 last = here;
554*c9083b85SXin LI                 for (;;) {
555*c9083b85SXin LI                     here = state->distcode[last.val +
556*c9083b85SXin LI                             (BITS(last.bits + last.op) >> last.bits)];
557*c9083b85SXin LI                     if ((unsigned)(last.bits + here.bits) <= bits) break;
558*c9083b85SXin LI                     PULLBYTE();
559*c9083b85SXin LI                 }
560*c9083b85SXin LI                 DROPBITS(last.bits);
561*c9083b85SXin LI             }
562*c9083b85SXin LI             DROPBITS(here.bits);
563*c9083b85SXin LI             if (here.op & 64) {
564*c9083b85SXin LI                 strm->msg = (char *)"invalid distance code";
565*c9083b85SXin LI                 state->mode = BAD;
566*c9083b85SXin LI                 break;
567*c9083b85SXin LI             }
568*c9083b85SXin LI             state->offset = (unsigned)here.val;
569*c9083b85SXin LI 
570*c9083b85SXin LI             /* get distance extra bits, if any */
571*c9083b85SXin LI             state->extra = (unsigned)(here.op) & 15;
572*c9083b85SXin LI             if (state->extra != 0) {
573*c9083b85SXin LI                 NEEDBITS(state->extra);
574*c9083b85SXin LI                 state->offset += BITS(state->extra);
575*c9083b85SXin LI                 DROPBITS(state->extra);
576*c9083b85SXin LI             }
577*c9083b85SXin LI             if (state->offset > state->wsize - (state->whave < state->wsize ?
578*c9083b85SXin LI                                                 left : 0)) {
579*c9083b85SXin LI                 strm->msg = (char *)"invalid distance too far back";
580*c9083b85SXin LI                 state->mode = BAD;
581*c9083b85SXin LI                 break;
582*c9083b85SXin LI             }
583*c9083b85SXin LI             Tracevv((stderr, "inflate:         distance %u\n", state->offset));
584*c9083b85SXin LI 
585*c9083b85SXin LI             /* copy match from window to output */
586*c9083b85SXin LI             do {
587*c9083b85SXin LI                 ROOM();
588*c9083b85SXin LI                 copy = state->wsize - state->offset;
589*c9083b85SXin LI                 if (copy < left) {
590*c9083b85SXin LI                     from = put + copy;
591*c9083b85SXin LI                     copy = left - copy;
592*c9083b85SXin LI                 }
593*c9083b85SXin LI                 else {
594*c9083b85SXin LI                     from = put - state->offset;
595*c9083b85SXin LI                     copy = left;
596*c9083b85SXin LI                 }
597*c9083b85SXin LI                 if (copy > state->length) copy = state->length;
598*c9083b85SXin LI                 state->length -= copy;
599*c9083b85SXin LI                 left -= copy;
600*c9083b85SXin LI                 do {
601*c9083b85SXin LI                     *put++ = *from++;
602*c9083b85SXin LI                 } while (--copy);
603*c9083b85SXin LI             } while (state->length != 0);
604*c9083b85SXin LI             break;
605*c9083b85SXin LI 
606*c9083b85SXin LI         case DONE:
607*c9083b85SXin LI             /* inflate stream terminated properly -- write leftover output */
608*c9083b85SXin LI             ret = Z_STREAM_END;
609*c9083b85SXin LI             if (left < state->wsize) {
610*c9083b85SXin LI                 if (out(out_desc, state->window, state->wsize - left))
611*c9083b85SXin LI                     ret = Z_BUF_ERROR;
612*c9083b85SXin LI             }
613*c9083b85SXin LI             goto inf_leave;
614*c9083b85SXin LI 
615*c9083b85SXin LI         case BAD:
616*c9083b85SXin LI             ret = Z_DATA_ERROR;
617*c9083b85SXin LI             goto inf_leave;
618*c9083b85SXin LI 
619*c9083b85SXin LI         default:                /* can't happen, but makes compilers happy */
620*c9083b85SXin LI             ret = Z_STREAM_ERROR;
621*c9083b85SXin LI             goto inf_leave;
622*c9083b85SXin LI         }
623*c9083b85SXin LI 
624*c9083b85SXin LI     /* Return unused input */
625*c9083b85SXin LI   inf_leave:
626*c9083b85SXin LI     strm->next_in = next;
627*c9083b85SXin LI     strm->avail_in = have;
628*c9083b85SXin LI     return ret;
629*c9083b85SXin LI }
630*c9083b85SXin LI 
631*c9083b85SXin LI int ZEXPORT inflateBackEnd(strm)
632*c9083b85SXin LI z_streamp strm;
633*c9083b85SXin LI {
634*c9083b85SXin LI     if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
635*c9083b85SXin LI         return Z_STREAM_ERROR;
636*c9083b85SXin LI     ZFREE(strm, strm->state);
637*c9083b85SXin LI     strm->state = Z_NULL;
638*c9083b85SXin LI     Tracev((stderr, "inflate: end\n"));
639*c9083b85SXin LI     return Z_OK;
640*c9083b85SXin LI }
641