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