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