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