1c9083b85SXin LI /* inflate.c -- zlib decompression
2cd882207SXin LI * Copyright (C) 1995-2022 Mark Adler
3c9083b85SXin LI * For conditions of distribution and use, see copyright notice in zlib.h
4c9083b85SXin LI */
5c9083b85SXin LI
6c9083b85SXin LI /*
7c9083b85SXin LI * Change history:
8c9083b85SXin LI *
9c9083b85SXin LI * 1.2.beta0 24 Nov 2002
10c9083b85SXin LI * - First version -- complete rewrite of inflate to simplify code, avoid
11c9083b85SXin LI * creation of window when not needed, minimize use of window when it is
12c9083b85SXin LI * needed, make inffast.c even faster, implement gzip decoding, and to
13c9083b85SXin LI * improve code readability and style over the previous zlib inflate code
14c9083b85SXin LI *
15c9083b85SXin LI * 1.2.beta1 25 Nov 2002
16c9083b85SXin LI * - Use pointers for available input and output checking in inffast.c
17c9083b85SXin LI * - Remove input and output counters in inffast.c
18c9083b85SXin LI * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
19c9083b85SXin LI * - Remove unnecessary second byte pull from length extra in inffast.c
20c9083b85SXin LI * - Unroll direct copy to three copies per loop in inffast.c
21c9083b85SXin LI *
22c9083b85SXin LI * 1.2.beta2 4 Dec 2002
23c9083b85SXin LI * - Change external routine names to reduce potential conflicts
24c9083b85SXin LI * - Correct filename to inffixed.h for fixed tables in inflate.c
25c9083b85SXin LI * - Make hbuf[] unsigned char to match parameter type in inflate.c
26c9083b85SXin LI * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
27c9083b85SXin LI * to avoid negation problem on Alphas (64 bit) in inflate.c
28c9083b85SXin LI *
29c9083b85SXin LI * 1.2.beta3 22 Dec 2002
30c9083b85SXin LI * - Add comments on state->bits assertion in inffast.c
31c9083b85SXin LI * - Add comments on op field in inftrees.h
32c9083b85SXin LI * - Fix bug in reuse of allocated window after inflateReset()
33c9083b85SXin LI * - Remove bit fields--back to byte structure for speed
34c9083b85SXin LI * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
35c9083b85SXin LI * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
36c9083b85SXin LI * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
37c9083b85SXin LI * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
38c9083b85SXin LI * - Use local copies of stream next and avail values, as well as local bit
39c9083b85SXin LI * buffer and bit count in inflate()--for speed when inflate_fast() not used
40c9083b85SXin LI *
41c9083b85SXin LI * 1.2.beta4 1 Jan 2003
42c9083b85SXin LI * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
43c9083b85SXin LI * - Move a comment on output buffer sizes from inffast.c to inflate.c
44c9083b85SXin LI * - Add comments in inffast.c to introduce the inflate_fast() routine
45c9083b85SXin LI * - Rearrange window copies in inflate_fast() for speed and simplification
46c9083b85SXin LI * - Unroll last copy for window match in inflate_fast()
47c9083b85SXin LI * - Use local copies of window variables in inflate_fast() for speed
48c9083b85SXin LI * - Pull out common wnext == 0 case for speed in inflate_fast()
49c9083b85SXin LI * - Make op and len in inflate_fast() unsigned for consistency
50c9083b85SXin LI * - Add FAR to lcode and dcode declarations in inflate_fast()
51c9083b85SXin LI * - Simplified bad distance check in inflate_fast()
52c9083b85SXin LI * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
53c9083b85SXin LI * source file infback.c to provide a call-back interface to inflate for
54c9083b85SXin LI * programs like gzip and unzip -- uses window as output buffer to avoid
55c9083b85SXin LI * window copying
56c9083b85SXin LI *
57c9083b85SXin LI * 1.2.beta5 1 Jan 2003
58c9083b85SXin LI * - Improved inflateBack() interface to allow the caller to provide initial
59c9083b85SXin LI * input in strm.
60c9083b85SXin LI * - Fixed stored blocks bug in inflateBack()
61c9083b85SXin LI *
62c9083b85SXin LI * 1.2.beta6 4 Jan 2003
63c9083b85SXin LI * - Added comments in inffast.c on effectiveness of POSTINC
64c9083b85SXin LI * - Typecasting all around to reduce compiler warnings
65c9083b85SXin LI * - Changed loops from while (1) or do {} while (1) to for (;;), again to
66c9083b85SXin LI * make compilers happy
67c9083b85SXin LI * - Changed type of window in inflateBackInit() to unsigned char *
68c9083b85SXin LI *
69c9083b85SXin LI * 1.2.beta7 27 Jan 2003
70c9083b85SXin LI * - Changed many types to unsigned or unsigned short to avoid warnings
71c9083b85SXin LI * - Added inflateCopy() function
72c9083b85SXin LI *
73c9083b85SXin LI * 1.2.0 9 Mar 2003
74c9083b85SXin LI * - Changed inflateBack() interface to provide separate opaque descriptors
75c9083b85SXin LI * for the in() and out() functions
76c9083b85SXin LI * - Changed inflateBack() argument and in_func typedef to swap the length
77c9083b85SXin LI * and buffer address return values for the input function
78c9083b85SXin LI * - Check next_in and next_out for Z_NULL on entry to inflate()
79c9083b85SXin LI *
80c9083b85SXin LI * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
81c9083b85SXin LI */
82c9083b85SXin LI
83c9083b85SXin LI #include "zutil.h"
84c9083b85SXin LI #include "inftrees.h"
85c9083b85SXin LI #include "inflate.h"
86c9083b85SXin LI #include "inffast.h"
87c9083b85SXin LI
88c9083b85SXin LI #ifdef MAKEFIXED
89c9083b85SXin LI # ifndef BUILDFIXED
90c9083b85SXin LI # define BUILDFIXED
91c9083b85SXin LI # endif
92c9083b85SXin LI #endif
93c9083b85SXin LI
inflateStateCheck(z_streamp strm)944717628eSXin LI local int inflateStateCheck(z_streamp strm) {
95c9083b85SXin LI struct inflate_state FAR *state;
96c9083b85SXin LI if (strm == Z_NULL ||
97c9083b85SXin LI strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
98c9083b85SXin LI return 1;
99c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
100c9083b85SXin LI if (state == Z_NULL || state->strm != strm ||
101c9083b85SXin LI state->mode < HEAD || state->mode > SYNC)
102c9083b85SXin LI return 1;
103c9083b85SXin LI return 0;
104c9083b85SXin LI }
105c9083b85SXin LI
inflateResetKeep(z_streamp strm)1064717628eSXin LI int ZEXPORT inflateResetKeep(z_streamp strm) {
107c9083b85SXin LI struct inflate_state FAR *state;
108c9083b85SXin LI
109c9083b85SXin LI if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
110c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
111c9083b85SXin LI strm->total_in = strm->total_out = state->total = 0;
112c9083b85SXin LI strm->msg = Z_NULL;
113c9083b85SXin LI if (state->wrap) /* to support ill-conceived Java test suite */
114c9083b85SXin LI strm->adler = state->wrap & 1;
115c9083b85SXin LI state->mode = HEAD;
116c9083b85SXin LI state->last = 0;
117c9083b85SXin LI state->havedict = 0;
118cd882207SXin LI state->flags = -1;
119c9083b85SXin LI state->dmax = 32768U;
120c9083b85SXin LI state->head = Z_NULL;
121c9083b85SXin LI state->hold = 0;
122c9083b85SXin LI state->bits = 0;
123c9083b85SXin LI state->lencode = state->distcode = state->next = state->codes;
124c9083b85SXin LI state->sane = 1;
125c9083b85SXin LI state->back = -1;
126c9083b85SXin LI Tracev((stderr, "inflate: reset\n"));
127c9083b85SXin LI return Z_OK;
128c9083b85SXin LI }
129c9083b85SXin LI
inflateReset(z_streamp strm)1304717628eSXin LI int ZEXPORT inflateReset(z_streamp strm) {
131c9083b85SXin LI struct inflate_state FAR *state;
132c9083b85SXin LI
133c9083b85SXin LI if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
134c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
135c9083b85SXin LI state->wsize = 0;
136c9083b85SXin LI state->whave = 0;
137c9083b85SXin LI state->wnext = 0;
138c9083b85SXin LI return inflateResetKeep(strm);
139c9083b85SXin LI }
140c9083b85SXin LI
inflateReset2(z_streamp strm,int windowBits)1414717628eSXin LI int ZEXPORT inflateReset2(z_streamp strm, int windowBits) {
142c9083b85SXin LI int wrap;
143c9083b85SXin LI struct inflate_state FAR *state;
144c9083b85SXin LI
145c9083b85SXin LI /* get the state */
146c9083b85SXin LI if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
147c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
148c9083b85SXin LI
149c9083b85SXin LI /* extract wrap request from windowBits parameter */
150c9083b85SXin LI if (windowBits < 0) {
151e37bb444SXin LI if (windowBits < -15)
152e37bb444SXin LI return Z_STREAM_ERROR;
153c9083b85SXin LI wrap = 0;
154c9083b85SXin LI windowBits = -windowBits;
155c9083b85SXin LI }
156c9083b85SXin LI else {
157c9083b85SXin LI wrap = (windowBits >> 4) + 5;
158c9083b85SXin LI #ifdef GUNZIP
159c9083b85SXin LI if (windowBits < 48)
160c9083b85SXin LI windowBits &= 15;
161c9083b85SXin LI #endif
162c9083b85SXin LI }
163c9083b85SXin LI
164c9083b85SXin LI /* set number of window bits, free window if different */
165c9083b85SXin LI if (windowBits && (windowBits < 8 || windowBits > 15))
166c9083b85SXin LI return Z_STREAM_ERROR;
167c9083b85SXin LI if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
168c9083b85SXin LI ZFREE(strm, state->window);
169c9083b85SXin LI state->window = Z_NULL;
170c9083b85SXin LI }
171c9083b85SXin LI
172c9083b85SXin LI /* update state and reset the rest of it */
173c9083b85SXin LI state->wrap = wrap;
174c9083b85SXin LI state->wbits = (unsigned)windowBits;
175c9083b85SXin LI return inflateReset(strm);
176c9083b85SXin LI }
177c9083b85SXin LI
inflateInit2_(z_streamp strm,int windowBits,const char * version,int stream_size)1784717628eSXin LI int ZEXPORT inflateInit2_(z_streamp strm, int windowBits,
1794717628eSXin LI const char *version, int stream_size) {
180c9083b85SXin LI int ret;
181c9083b85SXin LI struct inflate_state FAR *state;
182c9083b85SXin LI
183c9083b85SXin LI if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
184c9083b85SXin LI stream_size != (int)(sizeof(z_stream)))
185c9083b85SXin LI return Z_VERSION_ERROR;
186c9083b85SXin LI if (strm == Z_NULL) return Z_STREAM_ERROR;
187c9083b85SXin LI strm->msg = Z_NULL; /* in case we return an error */
188c9083b85SXin LI if (strm->zalloc == (alloc_func)0) {
189a15cb219SXin LI #if defined(Z_SOLO) && !defined(_KERNEL)
190c9083b85SXin LI return Z_STREAM_ERROR;
191c9083b85SXin LI #else
192c9083b85SXin LI strm->zalloc = zcalloc;
193c9083b85SXin LI strm->opaque = (voidpf)0;
194c9083b85SXin LI #endif
195c9083b85SXin LI }
196c9083b85SXin LI if (strm->zfree == (free_func)0)
197a15cb219SXin LI #if defined(Z_SOLO) && !defined(_KERNEL)
198c9083b85SXin LI return Z_STREAM_ERROR;
199c9083b85SXin LI #else
200c9083b85SXin LI strm->zfree = zcfree;
201c9083b85SXin LI #endif
202c9083b85SXin LI state = (struct inflate_state FAR *)
203c9083b85SXin LI ZALLOC(strm, 1, sizeof(struct inflate_state));
204c9083b85SXin LI if (state == Z_NULL) return Z_MEM_ERROR;
205c9083b85SXin LI Tracev((stderr, "inflate: allocated\n"));
206c9083b85SXin LI strm->state = (struct internal_state FAR *)state;
207c9083b85SXin LI state->strm = strm;
208c9083b85SXin LI state->window = Z_NULL;
209c9083b85SXin LI state->mode = HEAD; /* to pass state test in inflateReset2() */
210c9083b85SXin LI ret = inflateReset2(strm, windowBits);
211c9083b85SXin LI if (ret != Z_OK) {
212c9083b85SXin LI ZFREE(strm, state);
213c9083b85SXin LI strm->state = Z_NULL;
214c9083b85SXin LI }
215c9083b85SXin LI return ret;
216c9083b85SXin LI }
217c9083b85SXin LI
inflateInit_(z_streamp strm,const char * version,int stream_size)2184717628eSXin LI int ZEXPORT inflateInit_(z_streamp strm, const char *version,
2194717628eSXin LI int stream_size) {
220c9083b85SXin LI return inflateInit2_(strm, DEF_WBITS, version, stream_size);
221c9083b85SXin LI }
222c9083b85SXin LI
inflatePrime(z_streamp strm,int bits,int value)2234717628eSXin LI int ZEXPORT inflatePrime(z_streamp strm, int bits, int value) {
224c9083b85SXin LI struct inflate_state FAR *state;
225c9083b85SXin LI
226c9083b85SXin LI if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
2274717628eSXin LI if (bits == 0)
2284717628eSXin LI return Z_OK;
229c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
230c9083b85SXin LI if (bits < 0) {
231c9083b85SXin LI state->hold = 0;
232c9083b85SXin LI state->bits = 0;
233c9083b85SXin LI return Z_OK;
234c9083b85SXin LI }
235c9083b85SXin LI if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
236c9083b85SXin LI value &= (1L << bits) - 1;
237c9083b85SXin LI state->hold += (unsigned)value << state->bits;
238c9083b85SXin LI state->bits += (uInt)bits;
239c9083b85SXin LI return Z_OK;
240c9083b85SXin LI }
241c9083b85SXin LI
242c9083b85SXin LI /*
243c9083b85SXin LI Return state with length and distance decoding tables and index sizes set to
244c9083b85SXin LI fixed code decoding. Normally this returns fixed tables from inffixed.h.
245c9083b85SXin LI If BUILDFIXED is defined, then instead this routine builds the tables the
246c9083b85SXin LI first time it's called, and returns those tables the first time and
247c9083b85SXin LI thereafter. This reduces the size of the code by about 2K bytes, in
248c9083b85SXin LI exchange for a little execution time. However, BUILDFIXED should not be
249c9083b85SXin LI used for threaded applications, since the rewriting of the tables and virgin
250c9083b85SXin LI may not be thread-safe.
251c9083b85SXin LI */
fixedtables(struct inflate_state FAR * state)2524717628eSXin LI local void fixedtables(struct inflate_state FAR *state) {
253c9083b85SXin LI #ifdef BUILDFIXED
254c9083b85SXin LI static int virgin = 1;
255c9083b85SXin LI static code *lenfix, *distfix;
256c9083b85SXin LI static code fixed[544];
257c9083b85SXin LI
258c9083b85SXin LI /* build fixed huffman tables if first call (may not be thread safe) */
259c9083b85SXin LI if (virgin) {
260c9083b85SXin LI unsigned sym, bits;
261c9083b85SXin LI static code *next;
262c9083b85SXin LI
263c9083b85SXin LI /* literal/length table */
264c9083b85SXin LI sym = 0;
265c9083b85SXin LI while (sym < 144) state->lens[sym++] = 8;
266c9083b85SXin LI while (sym < 256) state->lens[sym++] = 9;
267c9083b85SXin LI while (sym < 280) state->lens[sym++] = 7;
268c9083b85SXin LI while (sym < 288) state->lens[sym++] = 8;
269c9083b85SXin LI next = fixed;
270c9083b85SXin LI lenfix = next;
271c9083b85SXin LI bits = 9;
272c9083b85SXin LI inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
273c9083b85SXin LI
274c9083b85SXin LI /* distance table */
275c9083b85SXin LI sym = 0;
276c9083b85SXin LI while (sym < 32) state->lens[sym++] = 5;
277c9083b85SXin LI distfix = next;
278c9083b85SXin LI bits = 5;
279c9083b85SXin LI inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
280c9083b85SXin LI
281c9083b85SXin LI /* do this just once */
282c9083b85SXin LI virgin = 0;
283c9083b85SXin LI }
284c9083b85SXin LI #else /* !BUILDFIXED */
285c9083b85SXin LI # include "inffixed.h"
286c9083b85SXin LI #endif /* BUILDFIXED */
287c9083b85SXin LI state->lencode = lenfix;
288c9083b85SXin LI state->lenbits = 9;
289c9083b85SXin LI state->distcode = distfix;
290c9083b85SXin LI state->distbits = 5;
291c9083b85SXin LI }
292c9083b85SXin LI
293c9083b85SXin LI #ifdef MAKEFIXED
294c9083b85SXin LI #include <stdio.h>
295c9083b85SXin LI
296c9083b85SXin LI /*
297c9083b85SXin LI Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
298c9083b85SXin LI defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
299c9083b85SXin LI those tables to stdout, which would be piped to inffixed.h. A small program
300c9083b85SXin LI can simply call makefixed to do this:
301c9083b85SXin LI
302c9083b85SXin LI void makefixed(void);
303c9083b85SXin LI
304c9083b85SXin LI int main(void)
305c9083b85SXin LI {
306c9083b85SXin LI makefixed();
307c9083b85SXin LI return 0;
308c9083b85SXin LI }
309c9083b85SXin LI
310c9083b85SXin LI Then that can be linked with zlib built with MAKEFIXED defined and run:
311c9083b85SXin LI
312c9083b85SXin LI a.out > inffixed.h
313c9083b85SXin LI */
makefixed(void)3144717628eSXin LI void makefixed(void)
315c9083b85SXin LI {
316c9083b85SXin LI unsigned low, size;
317c9083b85SXin LI struct inflate_state state;
318c9083b85SXin LI
319c9083b85SXin LI fixedtables(&state);
320c9083b85SXin LI puts(" /* inffixed.h -- table for decoding fixed codes");
321c9083b85SXin LI puts(" * Generated automatically by makefixed().");
322c9083b85SXin LI puts(" */");
323c9083b85SXin LI puts("");
324c9083b85SXin LI puts(" /* WARNING: this file should *not* be used by applications.");
325c9083b85SXin LI puts(" It is part of the implementation of this library and is");
326c9083b85SXin LI puts(" subject to change. Applications should only use zlib.h.");
327c9083b85SXin LI puts(" */");
328c9083b85SXin LI puts("");
329c9083b85SXin LI size = 1U << 9;
330c9083b85SXin LI printf(" static const code lenfix[%u] = {", size);
331c9083b85SXin LI low = 0;
332c9083b85SXin LI for (;;) {
333c9083b85SXin LI if ((low % 7) == 0) printf("\n ");
334c9083b85SXin LI printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
335c9083b85SXin LI state.lencode[low].bits, state.lencode[low].val);
336c9083b85SXin LI if (++low == size) break;
337c9083b85SXin LI putchar(',');
338c9083b85SXin LI }
339c9083b85SXin LI puts("\n };");
340c9083b85SXin LI size = 1U << 5;
341c9083b85SXin LI printf("\n static const code distfix[%u] = {", size);
342c9083b85SXin LI low = 0;
343c9083b85SXin LI for (;;) {
344c9083b85SXin LI if ((low % 6) == 0) printf("\n ");
345c9083b85SXin LI printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
346c9083b85SXin LI state.distcode[low].val);
347c9083b85SXin LI if (++low == size) break;
348c9083b85SXin LI putchar(',');
349c9083b85SXin LI }
350c9083b85SXin LI puts("\n };");
351c9083b85SXin LI }
352c9083b85SXin LI #endif /* MAKEFIXED */
353c9083b85SXin LI
354c9083b85SXin LI /*
355c9083b85SXin LI Update the window with the last wsize (normally 32K) bytes written before
356c9083b85SXin LI returning. If window does not exist yet, create it. This is only called
357c9083b85SXin LI when a window is already in use, or when output has been written during this
358c9083b85SXin LI inflate call, but the end of the deflate stream has not been reached yet.
359c9083b85SXin LI It is also called to create a window for dictionary data when a dictionary
360c9083b85SXin LI is loaded.
361c9083b85SXin LI
362c9083b85SXin LI Providing output buffers larger than 32K to inflate() should provide a speed
363c9083b85SXin LI advantage, since only the last 32K of output is copied to the sliding window
364c9083b85SXin LI upon return from inflate(), and since all distances after the first 32K of
365c9083b85SXin LI output will fall in the output data, making match copies simpler and faster.
366c9083b85SXin LI The advantage may be dependent on the size of the processor's data caches.
367c9083b85SXin LI */
updatewindow(z_streamp strm,const Bytef * end,unsigned copy)3684717628eSXin LI local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) {
369c9083b85SXin LI struct inflate_state FAR *state;
370c9083b85SXin LI unsigned dist;
371c9083b85SXin LI
372c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
373c9083b85SXin LI
374c9083b85SXin LI /* if it hasn't been done already, allocate space for the window */
375c9083b85SXin LI if (state->window == Z_NULL) {
376c9083b85SXin LI state->window = (unsigned char FAR *)
377c9083b85SXin LI ZALLOC(strm, 1U << state->wbits,
378c9083b85SXin LI sizeof(unsigned char));
379c9083b85SXin LI if (state->window == Z_NULL) return 1;
380c9083b85SXin LI }
381c9083b85SXin LI
382c9083b85SXin LI /* if window not in use yet, initialize */
383c9083b85SXin LI if (state->wsize == 0) {
384c9083b85SXin LI state->wsize = 1U << state->wbits;
385c9083b85SXin LI state->wnext = 0;
386c9083b85SXin LI state->whave = 0;
387c9083b85SXin LI }
388c9083b85SXin LI
389c9083b85SXin LI /* copy state->wsize or less output bytes into the circular window */
390c9083b85SXin LI if (copy >= state->wsize) {
391c9083b85SXin LI zmemcpy(state->window, end - state->wsize, state->wsize);
392c9083b85SXin LI state->wnext = 0;
393c9083b85SXin LI state->whave = state->wsize;
394c9083b85SXin LI }
395c9083b85SXin LI else {
396c9083b85SXin LI dist = state->wsize - state->wnext;
397c9083b85SXin LI if (dist > copy) dist = copy;
398c9083b85SXin LI zmemcpy(state->window + state->wnext, end - copy, dist);
399c9083b85SXin LI copy -= dist;
400c9083b85SXin LI if (copy) {
401c9083b85SXin LI zmemcpy(state->window, end - copy, copy);
402c9083b85SXin LI state->wnext = copy;
403c9083b85SXin LI state->whave = state->wsize;
404c9083b85SXin LI }
405c9083b85SXin LI else {
406c9083b85SXin LI state->wnext += dist;
407c9083b85SXin LI if (state->wnext == state->wsize) state->wnext = 0;
408c9083b85SXin LI if (state->whave < state->wsize) state->whave += dist;
409c9083b85SXin LI }
410c9083b85SXin LI }
411c9083b85SXin LI return 0;
412c9083b85SXin LI }
413c9083b85SXin LI
414c9083b85SXin LI /* Macros for inflate(): */
415c9083b85SXin LI
416c9083b85SXin LI /* check function to use adler32() for zlib or crc32() for gzip */
417c9083b85SXin LI #ifdef GUNZIP
418cd882207SXin LI # define UPDATE_CHECK(check, buf, len) \
419c9083b85SXin LI (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
420c9083b85SXin LI #else
421cd882207SXin LI # define UPDATE_CHECK(check, buf, len) adler32(check, buf, len)
422c9083b85SXin LI #endif
423c9083b85SXin LI
424c9083b85SXin LI /* check macros for header crc */
425c9083b85SXin LI #ifdef GUNZIP
426c9083b85SXin LI # define CRC2(check, word) \
427c9083b85SXin LI do { \
428c9083b85SXin LI hbuf[0] = (unsigned char)(word); \
429c9083b85SXin LI hbuf[1] = (unsigned char)((word) >> 8); \
430c9083b85SXin LI check = crc32(check, hbuf, 2); \
431c9083b85SXin LI } while (0)
432c9083b85SXin LI
433c9083b85SXin LI # define CRC4(check, word) \
434c9083b85SXin LI do { \
435c9083b85SXin LI hbuf[0] = (unsigned char)(word); \
436c9083b85SXin LI hbuf[1] = (unsigned char)((word) >> 8); \
437c9083b85SXin LI hbuf[2] = (unsigned char)((word) >> 16); \
438c9083b85SXin LI hbuf[3] = (unsigned char)((word) >> 24); \
439c9083b85SXin LI check = crc32(check, hbuf, 4); \
440c9083b85SXin LI } while (0)
441c9083b85SXin LI #endif
442c9083b85SXin LI
443c9083b85SXin LI /* Load registers with state in inflate() for speed */
444c9083b85SXin LI #define LOAD() \
445c9083b85SXin LI do { \
446c9083b85SXin LI put = strm->next_out; \
447c9083b85SXin LI left = strm->avail_out; \
448c9083b85SXin LI next = strm->next_in; \
449c9083b85SXin LI have = strm->avail_in; \
450c9083b85SXin LI hold = state->hold; \
451c9083b85SXin LI bits = state->bits; \
452c9083b85SXin LI } while (0)
453c9083b85SXin LI
454c9083b85SXin LI /* Restore state from registers in inflate() */
455c9083b85SXin LI #define RESTORE() \
456c9083b85SXin LI do { \
457c9083b85SXin LI strm->next_out = put; \
458c9083b85SXin LI strm->avail_out = left; \
459c9083b85SXin LI strm->next_in = next; \
460c9083b85SXin LI strm->avail_in = have; \
461c9083b85SXin LI state->hold = hold; \
462c9083b85SXin LI state->bits = bits; \
463c9083b85SXin LI } while (0)
464c9083b85SXin LI
465c9083b85SXin LI /* Clear the input bit accumulator */
466c9083b85SXin LI #define INITBITS() \
467c9083b85SXin LI do { \
468c9083b85SXin LI hold = 0; \
469c9083b85SXin LI bits = 0; \
470c9083b85SXin LI } while (0)
471c9083b85SXin LI
472c9083b85SXin LI /* Get a byte of input into the bit accumulator, or return from inflate()
473c9083b85SXin LI if there is no input available. */
474c9083b85SXin LI #define PULLBYTE() \
475c9083b85SXin LI do { \
476c9083b85SXin LI if (have == 0) goto inf_leave; \
477c9083b85SXin LI have--; \
478c9083b85SXin LI hold += (unsigned long)(*next++) << bits; \
479c9083b85SXin LI bits += 8; \
480c9083b85SXin LI } while (0)
481c9083b85SXin LI
482c9083b85SXin LI /* Assure that there are at least n bits in the bit accumulator. If there is
483c9083b85SXin LI not enough available input to do that, then return from inflate(). */
484c9083b85SXin LI #define NEEDBITS(n) \
485c9083b85SXin LI do { \
486c9083b85SXin LI while (bits < (unsigned)(n)) \
487c9083b85SXin LI PULLBYTE(); \
488c9083b85SXin LI } while (0)
489c9083b85SXin LI
490c9083b85SXin LI /* Return the low n bits of the bit accumulator (n < 16) */
491c9083b85SXin LI #define BITS(n) \
492c9083b85SXin LI ((unsigned)hold & ((1U << (n)) - 1))
493c9083b85SXin LI
494c9083b85SXin LI /* Remove n bits from the bit accumulator */
495c9083b85SXin LI #define DROPBITS(n) \
496c9083b85SXin LI do { \
497c9083b85SXin LI hold >>= (n); \
498c9083b85SXin LI bits -= (unsigned)(n); \
499c9083b85SXin LI } while (0)
500c9083b85SXin LI
501c9083b85SXin LI /* Remove zero to seven bits as needed to go to a byte boundary */
502c9083b85SXin LI #define BYTEBITS() \
503c9083b85SXin LI do { \
504c9083b85SXin LI hold >>= bits & 7; \
505c9083b85SXin LI bits -= bits & 7; \
506c9083b85SXin LI } while (0)
507c9083b85SXin LI
508c9083b85SXin LI /*
509c9083b85SXin LI inflate() uses a state machine to process as much input data and generate as
510c9083b85SXin LI much output data as possible before returning. The state machine is
511c9083b85SXin LI structured roughly as follows:
512c9083b85SXin LI
513c9083b85SXin LI for (;;) switch (state) {
514c9083b85SXin LI ...
515c9083b85SXin LI case STATEn:
516c9083b85SXin LI if (not enough input data or output space to make progress)
517c9083b85SXin LI return;
518c9083b85SXin LI ... make progress ...
519c9083b85SXin LI state = STATEm;
520c9083b85SXin LI break;
521c9083b85SXin LI ...
522c9083b85SXin LI }
523c9083b85SXin LI
524c9083b85SXin LI so when inflate() is called again, the same case is attempted again, and
525c9083b85SXin LI if the appropriate resources are provided, the machine proceeds to the
526c9083b85SXin LI next state. The NEEDBITS() macro is usually the way the state evaluates
527c9083b85SXin LI whether it can proceed or should return. NEEDBITS() does the return if
528c9083b85SXin LI the requested bits are not available. The typical use of the BITS macros
529c9083b85SXin LI is:
530c9083b85SXin LI
531c9083b85SXin LI NEEDBITS(n);
532c9083b85SXin LI ... do something with BITS(n) ...
533c9083b85SXin LI DROPBITS(n);
534c9083b85SXin LI
535c9083b85SXin LI where NEEDBITS(n) either returns from inflate() if there isn't enough
536c9083b85SXin LI input left to load n bits into the accumulator, or it continues. BITS(n)
537c9083b85SXin LI gives the low n bits in the accumulator. When done, DROPBITS(n) drops
538c9083b85SXin LI the low n bits off the accumulator. INITBITS() clears the accumulator
539c9083b85SXin LI and sets the number of available bits to zero. BYTEBITS() discards just
540c9083b85SXin LI enough bits to put the accumulator on a byte boundary. After BYTEBITS()
541c9083b85SXin LI and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
542c9083b85SXin LI
543c9083b85SXin LI NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
544c9083b85SXin LI if there is no input available. The decoding of variable length codes uses
545c9083b85SXin LI PULLBYTE() directly in order to pull just enough bytes to decode the next
546c9083b85SXin LI code, and no more.
547c9083b85SXin LI
548c9083b85SXin LI Some states loop until they get enough input, making sure that enough
549c9083b85SXin LI state information is maintained to continue the loop where it left off
550c9083b85SXin LI if NEEDBITS() returns in the loop. For example, want, need, and keep
551c9083b85SXin LI would all have to actually be part of the saved state in case NEEDBITS()
552c9083b85SXin LI returns:
553c9083b85SXin LI
554c9083b85SXin LI case STATEw:
555c9083b85SXin LI while (want < need) {
556c9083b85SXin LI NEEDBITS(n);
557c9083b85SXin LI keep[want++] = BITS(n);
558c9083b85SXin LI DROPBITS(n);
559c9083b85SXin LI }
560c9083b85SXin LI state = STATEx;
561c9083b85SXin LI case STATEx:
562c9083b85SXin LI
563c9083b85SXin LI As shown above, if the next state is also the next case, then the break
564c9083b85SXin LI is omitted.
565c9083b85SXin LI
566c9083b85SXin LI A state may also return if there is not enough output space available to
567c9083b85SXin LI complete that state. Those states are copying stored data, writing a
568c9083b85SXin LI literal byte, and copying a matching string.
569c9083b85SXin LI
570c9083b85SXin LI When returning, a "goto inf_leave" is used to update the total counters,
571c9083b85SXin LI update the check value, and determine whether any progress has been made
572c9083b85SXin LI during that inflate() call in order to return the proper return code.
573c9083b85SXin LI Progress is defined as a change in either strm->avail_in or strm->avail_out.
574c9083b85SXin LI When there is a window, goto inf_leave will update the window with the last
575c9083b85SXin LI output written. If a goto inf_leave occurs in the middle of decompression
576c9083b85SXin LI and there is no window currently, goto inf_leave will create one and copy
577c9083b85SXin LI output to the window for the next call of inflate().
578c9083b85SXin LI
579c9083b85SXin LI In this implementation, the flush parameter of inflate() only affects the
580c9083b85SXin LI return code (per zlib.h). inflate() always writes as much as possible to
581c9083b85SXin LI strm->next_out, given the space available and the provided input--the effect
582c9083b85SXin LI documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
583c9083b85SXin LI the allocation of and copying into a sliding window until necessary, which
584c9083b85SXin LI provides the effect documented in zlib.h for Z_FINISH when the entire input
585c9083b85SXin LI stream available. So the only thing the flush parameter actually does is:
586c9083b85SXin LI when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
587c9083b85SXin LI will return Z_BUF_ERROR if it has not reached the end of the stream.
588c9083b85SXin LI */
589c9083b85SXin LI
inflate(z_streamp strm,int flush)5904717628eSXin LI int ZEXPORT inflate(z_streamp strm, int flush) {
591c9083b85SXin LI struct inflate_state FAR *state;
592c9083b85SXin LI z_const unsigned char FAR *next; /* next input */
593c9083b85SXin LI unsigned char FAR *put; /* next output */
594c9083b85SXin LI unsigned have, left; /* available input and output */
595c9083b85SXin LI unsigned long hold; /* bit buffer */
596c9083b85SXin LI unsigned bits; /* bits in bit buffer */
597c9083b85SXin LI unsigned in, out; /* save starting available input and output */
598c9083b85SXin LI unsigned copy; /* number of stored or match bytes to copy */
599c9083b85SXin LI unsigned char FAR *from; /* where to copy match bytes from */
600c9083b85SXin LI code here; /* current decoding table entry */
601c9083b85SXin LI code last; /* parent table entry */
602c9083b85SXin LI unsigned len; /* length to copy for repeats, bits to drop */
603c9083b85SXin LI int ret; /* return code */
604c9083b85SXin LI #ifdef GUNZIP
605c9083b85SXin LI unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
606c9083b85SXin LI #endif
607c9083b85SXin LI static const unsigned short order[19] = /* permutation of code lengths */
608c9083b85SXin LI {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
609c9083b85SXin LI
610c9083b85SXin LI if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
611c9083b85SXin LI (strm->next_in == Z_NULL && strm->avail_in != 0))
612c9083b85SXin LI return Z_STREAM_ERROR;
613c9083b85SXin LI
614c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
615c9083b85SXin LI if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
616c9083b85SXin LI LOAD();
617c9083b85SXin LI in = have;
618c9083b85SXin LI out = left;
619c9083b85SXin LI ret = Z_OK;
620c9083b85SXin LI for (;;)
621c9083b85SXin LI switch (state->mode) {
622c9083b85SXin LI case HEAD:
623c9083b85SXin LI if (state->wrap == 0) {
624c9083b85SXin LI state->mode = TYPEDO;
625c9083b85SXin LI break;
626c9083b85SXin LI }
627c9083b85SXin LI NEEDBITS(16);
628c9083b85SXin LI #ifdef GUNZIP
629c9083b85SXin LI if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
630c9083b85SXin LI if (state->wbits == 0)
631c9083b85SXin LI state->wbits = 15;
632c9083b85SXin LI state->check = crc32(0L, Z_NULL, 0);
633c9083b85SXin LI CRC2(state->check, hold);
634c9083b85SXin LI INITBITS();
635c9083b85SXin LI state->mode = FLAGS;
636c9083b85SXin LI break;
637c9083b85SXin LI }
638c9083b85SXin LI if (state->head != Z_NULL)
639c9083b85SXin LI state->head->done = -1;
640c9083b85SXin LI if (!(state->wrap & 1) || /* check if zlib header allowed */
641c9083b85SXin LI #else
642c9083b85SXin LI if (
643c9083b85SXin LI #endif
644c9083b85SXin LI ((BITS(8) << 8) + (hold >> 8)) % 31) {
645c9083b85SXin LI strm->msg = (char *)"incorrect header check";
646c9083b85SXin LI state->mode = BAD;
647c9083b85SXin LI break;
648c9083b85SXin LI }
649c9083b85SXin LI if (BITS(4) != Z_DEFLATED) {
650c9083b85SXin LI strm->msg = (char *)"unknown compression method";
651c9083b85SXin LI state->mode = BAD;
652c9083b85SXin LI break;
653c9083b85SXin LI }
654c9083b85SXin LI DROPBITS(4);
655c9083b85SXin LI len = BITS(4) + 8;
656c9083b85SXin LI if (state->wbits == 0)
657c9083b85SXin LI state->wbits = len;
658c9083b85SXin LI if (len > 15 || len > state->wbits) {
659c9083b85SXin LI strm->msg = (char *)"invalid window size";
660c9083b85SXin LI state->mode = BAD;
661c9083b85SXin LI break;
662c9083b85SXin LI }
663c9083b85SXin LI state->dmax = 1U << len;
664cd882207SXin LI state->flags = 0; /* indicate zlib header */
665c9083b85SXin LI Tracev((stderr, "inflate: zlib header ok\n"));
666c9083b85SXin LI strm->adler = state->check = adler32(0L, Z_NULL, 0);
667c9083b85SXin LI state->mode = hold & 0x200 ? DICTID : TYPE;
668c9083b85SXin LI INITBITS();
669c9083b85SXin LI break;
670c9083b85SXin LI #ifdef GUNZIP
671c9083b85SXin LI case FLAGS:
672c9083b85SXin LI NEEDBITS(16);
673c9083b85SXin LI state->flags = (int)(hold);
674c9083b85SXin LI if ((state->flags & 0xff) != Z_DEFLATED) {
675c9083b85SXin LI strm->msg = (char *)"unknown compression method";
676c9083b85SXin LI state->mode = BAD;
677c9083b85SXin LI break;
678c9083b85SXin LI }
679c9083b85SXin LI if (state->flags & 0xe000) {
680c9083b85SXin LI strm->msg = (char *)"unknown header flags set";
681c9083b85SXin LI state->mode = BAD;
682c9083b85SXin LI break;
683c9083b85SXin LI }
684c9083b85SXin LI if (state->head != Z_NULL)
685c9083b85SXin LI state->head->text = (int)((hold >> 8) & 1);
686c9083b85SXin LI if ((state->flags & 0x0200) && (state->wrap & 4))
687c9083b85SXin LI CRC2(state->check, hold);
688c9083b85SXin LI INITBITS();
689c9083b85SXin LI state->mode = TIME;
690cd882207SXin LI /* fallthrough */
691c9083b85SXin LI case TIME:
692c9083b85SXin LI NEEDBITS(32);
693c9083b85SXin LI if (state->head != Z_NULL)
694c9083b85SXin LI state->head->time = hold;
695c9083b85SXin LI if ((state->flags & 0x0200) && (state->wrap & 4))
696c9083b85SXin LI CRC4(state->check, hold);
697c9083b85SXin LI INITBITS();
698c9083b85SXin LI state->mode = OS;
699cd882207SXin LI /* fallthrough */
700c9083b85SXin LI case OS:
701c9083b85SXin LI NEEDBITS(16);
702c9083b85SXin LI if (state->head != Z_NULL) {
703c9083b85SXin LI state->head->xflags = (int)(hold & 0xff);
704c9083b85SXin LI state->head->os = (int)(hold >> 8);
705c9083b85SXin LI }
706c9083b85SXin LI if ((state->flags & 0x0200) && (state->wrap & 4))
707c9083b85SXin LI CRC2(state->check, hold);
708c9083b85SXin LI INITBITS();
709c9083b85SXin LI state->mode = EXLEN;
710cd882207SXin LI /* fallthrough */
711c9083b85SXin LI case EXLEN:
712c9083b85SXin LI if (state->flags & 0x0400) {
713c9083b85SXin LI NEEDBITS(16);
714c9083b85SXin LI state->length = (unsigned)(hold);
715c9083b85SXin LI if (state->head != Z_NULL)
716c9083b85SXin LI state->head->extra_len = (unsigned)hold;
717c9083b85SXin LI if ((state->flags & 0x0200) && (state->wrap & 4))
718c9083b85SXin LI CRC2(state->check, hold);
719c9083b85SXin LI INITBITS();
720c9083b85SXin LI }
721c9083b85SXin LI else if (state->head != Z_NULL)
722c9083b85SXin LI state->head->extra = Z_NULL;
723c9083b85SXin LI state->mode = EXTRA;
724cd882207SXin LI /* fallthrough */
725c9083b85SXin LI case EXTRA:
726c9083b85SXin LI if (state->flags & 0x0400) {
727c9083b85SXin LI copy = state->length;
728c9083b85SXin LI if (copy > have) copy = have;
729c9083b85SXin LI if (copy) {
730dc3509f1SMark Adler if (state->head != Z_NULL &&
731dc3509f1SMark Adler state->head->extra != Z_NULL &&
7322969066fSMark Adler (len = state->head->extra_len - state->length) <
7332969066fSMark Adler state->head->extra_max) {
734c9083b85SXin LI zmemcpy(state->head->extra + len, next,
735c9083b85SXin LI len + copy > state->head->extra_max ?
736c9083b85SXin LI state->head->extra_max - len : copy);
737c9083b85SXin LI }
738c9083b85SXin LI if ((state->flags & 0x0200) && (state->wrap & 4))
739c9083b85SXin LI state->check = crc32(state->check, next, copy);
740c9083b85SXin LI have -= copy;
741c9083b85SXin LI next += copy;
742c9083b85SXin LI state->length -= copy;
743c9083b85SXin LI }
744c9083b85SXin LI if (state->length) goto inf_leave;
745c9083b85SXin LI }
746c9083b85SXin LI state->length = 0;
747c9083b85SXin LI state->mode = NAME;
748cd882207SXin LI /* fallthrough */
749c9083b85SXin LI case NAME:
750c9083b85SXin LI if (state->flags & 0x0800) {
751c9083b85SXin LI if (have == 0) goto inf_leave;
752c9083b85SXin LI copy = 0;
753c9083b85SXin LI do {
754c9083b85SXin LI len = (unsigned)(next[copy++]);
755c9083b85SXin LI if (state->head != Z_NULL &&
756c9083b85SXin LI state->head->name != Z_NULL &&
757c9083b85SXin LI state->length < state->head->name_max)
758c9083b85SXin LI state->head->name[state->length++] = (Bytef)len;
759c9083b85SXin LI } while (len && copy < have);
760c9083b85SXin LI if ((state->flags & 0x0200) && (state->wrap & 4))
761c9083b85SXin LI state->check = crc32(state->check, next, copy);
762c9083b85SXin LI have -= copy;
763c9083b85SXin LI next += copy;
764c9083b85SXin LI if (len) goto inf_leave;
765c9083b85SXin LI }
766c9083b85SXin LI else if (state->head != Z_NULL)
767c9083b85SXin LI state->head->name = Z_NULL;
768c9083b85SXin LI state->length = 0;
769c9083b85SXin LI state->mode = COMMENT;
770cd882207SXin LI /* fallthrough */
771c9083b85SXin LI case COMMENT:
772c9083b85SXin LI if (state->flags & 0x1000) {
773c9083b85SXin LI if (have == 0) goto inf_leave;
774c9083b85SXin LI copy = 0;
775c9083b85SXin LI do {
776c9083b85SXin LI len = (unsigned)(next[copy++]);
777c9083b85SXin LI if (state->head != Z_NULL &&
778c9083b85SXin LI state->head->comment != Z_NULL &&
779c9083b85SXin LI state->length < state->head->comm_max)
780c9083b85SXin LI state->head->comment[state->length++] = (Bytef)len;
781c9083b85SXin LI } while (len && copy < have);
782c9083b85SXin LI if ((state->flags & 0x0200) && (state->wrap & 4))
783c9083b85SXin LI state->check = crc32(state->check, next, copy);
784c9083b85SXin LI have -= copy;
785c9083b85SXin LI next += copy;
786c9083b85SXin LI if (len) goto inf_leave;
787c9083b85SXin LI }
788c9083b85SXin LI else if (state->head != Z_NULL)
789c9083b85SXin LI state->head->comment = Z_NULL;
790c9083b85SXin LI state->mode = HCRC;
791cd882207SXin LI /* fallthrough */
792c9083b85SXin LI case HCRC:
793c9083b85SXin LI if (state->flags & 0x0200) {
794c9083b85SXin LI NEEDBITS(16);
795c9083b85SXin LI if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
796c9083b85SXin LI strm->msg = (char *)"header crc mismatch";
797c9083b85SXin LI state->mode = BAD;
798c9083b85SXin LI break;
799c9083b85SXin LI }
800c9083b85SXin LI INITBITS();
801c9083b85SXin LI }
802c9083b85SXin LI if (state->head != Z_NULL) {
803c9083b85SXin LI state->head->hcrc = (int)((state->flags >> 9) & 1);
804c9083b85SXin LI state->head->done = 1;
805c9083b85SXin LI }
806c9083b85SXin LI strm->adler = state->check = crc32(0L, Z_NULL, 0);
807c9083b85SXin LI state->mode = TYPE;
808c9083b85SXin LI break;
809c9083b85SXin LI #endif
810c9083b85SXin LI case DICTID:
811c9083b85SXin LI NEEDBITS(32);
812c9083b85SXin LI strm->adler = state->check = ZSWAP32(hold);
813c9083b85SXin LI INITBITS();
814c9083b85SXin LI state->mode = DICT;
815cd882207SXin LI /* fallthrough */
816c9083b85SXin LI case DICT:
817c9083b85SXin LI if (state->havedict == 0) {
818c9083b85SXin LI RESTORE();
819c9083b85SXin LI return Z_NEED_DICT;
820c9083b85SXin LI }
821c9083b85SXin LI strm->adler = state->check = adler32(0L, Z_NULL, 0);
822c9083b85SXin LI state->mode = TYPE;
823cd882207SXin LI /* fallthrough */
824c9083b85SXin LI case TYPE:
825c9083b85SXin LI if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
826cd882207SXin LI /* fallthrough */
827c9083b85SXin LI case TYPEDO:
828c9083b85SXin LI if (state->last) {
829c9083b85SXin LI BYTEBITS();
830c9083b85SXin LI state->mode = CHECK;
831c9083b85SXin LI break;
832c9083b85SXin LI }
833c9083b85SXin LI NEEDBITS(3);
834c9083b85SXin LI state->last = BITS(1);
835c9083b85SXin LI DROPBITS(1);
836c9083b85SXin LI switch (BITS(2)) {
837c9083b85SXin LI case 0: /* stored block */
838c9083b85SXin LI Tracev((stderr, "inflate: stored block%s\n",
839c9083b85SXin LI state->last ? " (last)" : ""));
840c9083b85SXin LI state->mode = STORED;
841c9083b85SXin LI break;
842c9083b85SXin LI case 1: /* fixed block */
843c9083b85SXin LI fixedtables(state);
844c9083b85SXin LI Tracev((stderr, "inflate: fixed codes block%s\n",
845c9083b85SXin LI state->last ? " (last)" : ""));
846c9083b85SXin LI state->mode = LEN_; /* decode codes */
847c9083b85SXin LI if (flush == Z_TREES) {
848c9083b85SXin LI DROPBITS(2);
849c9083b85SXin LI goto inf_leave;
850c9083b85SXin LI }
851c9083b85SXin LI break;
852c9083b85SXin LI case 2: /* dynamic block */
853c9083b85SXin LI Tracev((stderr, "inflate: dynamic codes block%s\n",
854c9083b85SXin LI state->last ? " (last)" : ""));
855c9083b85SXin LI state->mode = TABLE;
856c9083b85SXin LI break;
857c9083b85SXin LI case 3:
858c9083b85SXin LI strm->msg = (char *)"invalid block type";
859c9083b85SXin LI state->mode = BAD;
860c9083b85SXin LI }
861c9083b85SXin LI DROPBITS(2);
862c9083b85SXin LI break;
863c9083b85SXin LI case STORED:
864c9083b85SXin LI BYTEBITS(); /* go to byte boundary */
865c9083b85SXin LI NEEDBITS(32);
866c9083b85SXin LI if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
867c9083b85SXin LI strm->msg = (char *)"invalid stored block lengths";
868c9083b85SXin LI state->mode = BAD;
869c9083b85SXin LI break;
870c9083b85SXin LI }
871c9083b85SXin LI state->length = (unsigned)hold & 0xffff;
872c9083b85SXin LI Tracev((stderr, "inflate: stored length %u\n",
873c9083b85SXin LI state->length));
874c9083b85SXin LI INITBITS();
875c9083b85SXin LI state->mode = COPY_;
876c9083b85SXin LI if (flush == Z_TREES) goto inf_leave;
877cd882207SXin LI /* fallthrough */
878c9083b85SXin LI case COPY_:
879c9083b85SXin LI state->mode = COPY;
880cd882207SXin LI /* fallthrough */
881c9083b85SXin LI case COPY:
882c9083b85SXin LI copy = state->length;
883c9083b85SXin LI if (copy) {
884c9083b85SXin LI if (copy > have) copy = have;
885c9083b85SXin LI if (copy > left) copy = left;
886c9083b85SXin LI if (copy == 0) goto inf_leave;
887c9083b85SXin LI zmemcpy(put, next, copy);
888c9083b85SXin LI have -= copy;
889c9083b85SXin LI next += copy;
890c9083b85SXin LI left -= copy;
891c9083b85SXin LI put += copy;
892c9083b85SXin LI state->length -= copy;
893c9083b85SXin LI break;
894c9083b85SXin LI }
895c9083b85SXin LI Tracev((stderr, "inflate: stored end\n"));
896c9083b85SXin LI state->mode = TYPE;
897c9083b85SXin LI break;
898c9083b85SXin LI case TABLE:
899c9083b85SXin LI NEEDBITS(14);
900c9083b85SXin LI state->nlen = BITS(5) + 257;
901c9083b85SXin LI DROPBITS(5);
902c9083b85SXin LI state->ndist = BITS(5) + 1;
903c9083b85SXin LI DROPBITS(5);
904c9083b85SXin LI state->ncode = BITS(4) + 4;
905c9083b85SXin LI DROPBITS(4);
906c9083b85SXin LI #ifndef PKZIP_BUG_WORKAROUND
907c9083b85SXin LI if (state->nlen > 286 || state->ndist > 30) {
908c9083b85SXin LI strm->msg = (char *)"too many length or distance symbols";
909c9083b85SXin LI state->mode = BAD;
910c9083b85SXin LI break;
911c9083b85SXin LI }
912c9083b85SXin LI #endif
913c9083b85SXin LI Tracev((stderr, "inflate: table sizes ok\n"));
914c9083b85SXin LI state->have = 0;
915c9083b85SXin LI state->mode = LENLENS;
916cd882207SXin LI /* fallthrough */
917c9083b85SXin LI case LENLENS:
918c9083b85SXin LI while (state->have < state->ncode) {
919c9083b85SXin LI NEEDBITS(3);
920c9083b85SXin LI state->lens[order[state->have++]] = (unsigned short)BITS(3);
921c9083b85SXin LI DROPBITS(3);
922c9083b85SXin LI }
923c9083b85SXin LI while (state->have < 19)
924c9083b85SXin LI state->lens[order[state->have++]] = 0;
925c9083b85SXin LI state->next = state->codes;
926c9083b85SXin LI state->lencode = (const code FAR *)(state->next);
927c9083b85SXin LI state->lenbits = 7;
928c9083b85SXin LI ret = inflate_table(CODES, state->lens, 19, &(state->next),
929c9083b85SXin LI &(state->lenbits), state->work);
930c9083b85SXin LI if (ret) {
931c9083b85SXin LI strm->msg = (char *)"invalid code lengths set";
932c9083b85SXin LI state->mode = BAD;
933c9083b85SXin LI break;
934c9083b85SXin LI }
935c9083b85SXin LI Tracev((stderr, "inflate: code lengths ok\n"));
936c9083b85SXin LI state->have = 0;
937c9083b85SXin LI state->mode = CODELENS;
938cd882207SXin LI /* fallthrough */
939c9083b85SXin LI case CODELENS:
940c9083b85SXin LI while (state->have < state->nlen + state->ndist) {
941c9083b85SXin LI for (;;) {
942c9083b85SXin LI here = state->lencode[BITS(state->lenbits)];
943c9083b85SXin LI if ((unsigned)(here.bits) <= bits) break;
944c9083b85SXin LI PULLBYTE();
945c9083b85SXin LI }
946c9083b85SXin LI if (here.val < 16) {
947c9083b85SXin LI DROPBITS(here.bits);
948c9083b85SXin LI state->lens[state->have++] = here.val;
949c9083b85SXin LI }
950c9083b85SXin LI else {
951c9083b85SXin LI if (here.val == 16) {
952c9083b85SXin LI NEEDBITS(here.bits + 2);
953c9083b85SXin LI DROPBITS(here.bits);
954c9083b85SXin LI if (state->have == 0) {
955c9083b85SXin LI strm->msg = (char *)"invalid bit length repeat";
956c9083b85SXin LI state->mode = BAD;
957c9083b85SXin LI break;
958c9083b85SXin LI }
959c9083b85SXin LI len = state->lens[state->have - 1];
960c9083b85SXin LI copy = 3 + BITS(2);
961c9083b85SXin LI DROPBITS(2);
962c9083b85SXin LI }
963c9083b85SXin LI else if (here.val == 17) {
964c9083b85SXin LI NEEDBITS(here.bits + 3);
965c9083b85SXin LI DROPBITS(here.bits);
966c9083b85SXin LI len = 0;
967c9083b85SXin LI copy = 3 + BITS(3);
968c9083b85SXin LI DROPBITS(3);
969c9083b85SXin LI }
970c9083b85SXin LI else {
971c9083b85SXin LI NEEDBITS(here.bits + 7);
972c9083b85SXin LI DROPBITS(here.bits);
973c9083b85SXin LI len = 0;
974c9083b85SXin LI copy = 11 + BITS(7);
975c9083b85SXin LI DROPBITS(7);
976c9083b85SXin LI }
977c9083b85SXin LI if (state->have + copy > state->nlen + state->ndist) {
978c9083b85SXin LI strm->msg = (char *)"invalid bit length repeat";
979c9083b85SXin LI state->mode = BAD;
980c9083b85SXin LI break;
981c9083b85SXin LI }
982c9083b85SXin LI while (copy--)
983c9083b85SXin LI state->lens[state->have++] = (unsigned short)len;
984c9083b85SXin LI }
985c9083b85SXin LI }
986c9083b85SXin LI
987c9083b85SXin LI /* handle error breaks in while */
988c9083b85SXin LI if (state->mode == BAD) break;
989c9083b85SXin LI
990c9083b85SXin LI /* check for end-of-block code (better have one) */
991c9083b85SXin LI if (state->lens[256] == 0) {
992c9083b85SXin LI strm->msg = (char *)"invalid code -- missing end-of-block";
993c9083b85SXin LI state->mode = BAD;
994c9083b85SXin LI break;
995c9083b85SXin LI }
996c9083b85SXin LI
997c9083b85SXin LI /* build code tables -- note: do not change the lenbits or distbits
998c9083b85SXin LI values here (9 and 6) without reading the comments in inftrees.h
999c9083b85SXin LI concerning the ENOUGH constants, which depend on those values */
1000c9083b85SXin LI state->next = state->codes;
1001c9083b85SXin LI state->lencode = (const code FAR *)(state->next);
1002c9083b85SXin LI state->lenbits = 9;
1003c9083b85SXin LI ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1004c9083b85SXin LI &(state->lenbits), state->work);
1005c9083b85SXin LI if (ret) {
1006c9083b85SXin LI strm->msg = (char *)"invalid literal/lengths set";
1007c9083b85SXin LI state->mode = BAD;
1008c9083b85SXin LI break;
1009c9083b85SXin LI }
1010c9083b85SXin LI state->distcode = (const code FAR *)(state->next);
1011c9083b85SXin LI state->distbits = 6;
1012c9083b85SXin LI ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1013c9083b85SXin LI &(state->next), &(state->distbits), state->work);
1014c9083b85SXin LI if (ret) {
1015c9083b85SXin LI strm->msg = (char *)"invalid distances set";
1016c9083b85SXin LI state->mode = BAD;
1017c9083b85SXin LI break;
1018c9083b85SXin LI }
1019c9083b85SXin LI Tracev((stderr, "inflate: codes ok\n"));
1020c9083b85SXin LI state->mode = LEN_;
1021c9083b85SXin LI if (flush == Z_TREES) goto inf_leave;
1022cd882207SXin LI /* fallthrough */
1023c9083b85SXin LI case LEN_:
1024c9083b85SXin LI state->mode = LEN;
1025cd882207SXin LI /* fallthrough */
1026c9083b85SXin LI case LEN:
1027c9083b85SXin LI if (have >= 6 && left >= 258) {
1028c9083b85SXin LI RESTORE();
1029c9083b85SXin LI inflate_fast(strm, out);
1030c9083b85SXin LI LOAD();
1031c9083b85SXin LI if (state->mode == TYPE)
1032c9083b85SXin LI state->back = -1;
1033c9083b85SXin LI break;
1034c9083b85SXin LI }
1035c9083b85SXin LI state->back = 0;
1036c9083b85SXin LI for (;;) {
1037c9083b85SXin LI here = state->lencode[BITS(state->lenbits)];
1038c9083b85SXin LI if ((unsigned)(here.bits) <= bits) break;
1039c9083b85SXin LI PULLBYTE();
1040c9083b85SXin LI }
1041c9083b85SXin LI if (here.op && (here.op & 0xf0) == 0) {
1042c9083b85SXin LI last = here;
1043c9083b85SXin LI for (;;) {
1044c9083b85SXin LI here = state->lencode[last.val +
1045c9083b85SXin LI (BITS(last.bits + last.op) >> last.bits)];
1046c9083b85SXin LI if ((unsigned)(last.bits + here.bits) <= bits) break;
1047c9083b85SXin LI PULLBYTE();
1048c9083b85SXin LI }
1049c9083b85SXin LI DROPBITS(last.bits);
1050c9083b85SXin LI state->back += last.bits;
1051c9083b85SXin LI }
1052c9083b85SXin LI DROPBITS(here.bits);
1053c9083b85SXin LI state->back += here.bits;
1054c9083b85SXin LI state->length = (unsigned)here.val;
1055c9083b85SXin LI if ((int)(here.op) == 0) {
1056c9083b85SXin LI Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1057c9083b85SXin LI "inflate: literal '%c'\n" :
1058c9083b85SXin LI "inflate: literal 0x%02x\n", here.val));
1059c9083b85SXin LI state->mode = LIT;
1060c9083b85SXin LI break;
1061c9083b85SXin LI }
1062c9083b85SXin LI if (here.op & 32) {
1063c9083b85SXin LI Tracevv((stderr, "inflate: end of block\n"));
1064c9083b85SXin LI state->back = -1;
1065c9083b85SXin LI state->mode = TYPE;
1066c9083b85SXin LI break;
1067c9083b85SXin LI }
1068c9083b85SXin LI if (here.op & 64) {
1069c9083b85SXin LI strm->msg = (char *)"invalid literal/length code";
1070c9083b85SXin LI state->mode = BAD;
1071c9083b85SXin LI break;
1072c9083b85SXin LI }
1073c9083b85SXin LI state->extra = (unsigned)(here.op) & 15;
1074c9083b85SXin LI state->mode = LENEXT;
1075cd882207SXin LI /* fallthrough */
1076c9083b85SXin LI case LENEXT:
1077c9083b85SXin LI if (state->extra) {
1078c9083b85SXin LI NEEDBITS(state->extra);
1079c9083b85SXin LI state->length += BITS(state->extra);
1080c9083b85SXin LI DROPBITS(state->extra);
1081c9083b85SXin LI state->back += state->extra;
1082c9083b85SXin LI }
1083c9083b85SXin LI Tracevv((stderr, "inflate: length %u\n", state->length));
1084c9083b85SXin LI state->was = state->length;
1085c9083b85SXin LI state->mode = DIST;
1086cd882207SXin LI /* fallthrough */
1087c9083b85SXin LI case DIST:
1088c9083b85SXin LI for (;;) {
1089c9083b85SXin LI here = state->distcode[BITS(state->distbits)];
1090c9083b85SXin LI if ((unsigned)(here.bits) <= bits) break;
1091c9083b85SXin LI PULLBYTE();
1092c9083b85SXin LI }
1093c9083b85SXin LI if ((here.op & 0xf0) == 0) {
1094c9083b85SXin LI last = here;
1095c9083b85SXin LI for (;;) {
1096c9083b85SXin LI here = state->distcode[last.val +
1097c9083b85SXin LI (BITS(last.bits + last.op) >> last.bits)];
1098c9083b85SXin LI if ((unsigned)(last.bits + here.bits) <= bits) break;
1099c9083b85SXin LI PULLBYTE();
1100c9083b85SXin LI }
1101c9083b85SXin LI DROPBITS(last.bits);
1102c9083b85SXin LI state->back += last.bits;
1103c9083b85SXin LI }
1104c9083b85SXin LI DROPBITS(here.bits);
1105c9083b85SXin LI state->back += here.bits;
1106c9083b85SXin LI if (here.op & 64) {
1107c9083b85SXin LI strm->msg = (char *)"invalid distance code";
1108c9083b85SXin LI state->mode = BAD;
1109c9083b85SXin LI break;
1110c9083b85SXin LI }
1111c9083b85SXin LI state->offset = (unsigned)here.val;
1112c9083b85SXin LI state->extra = (unsigned)(here.op) & 15;
1113c9083b85SXin LI state->mode = DISTEXT;
1114cd882207SXin LI /* fallthrough */
1115c9083b85SXin LI case DISTEXT:
1116c9083b85SXin LI if (state->extra) {
1117c9083b85SXin LI NEEDBITS(state->extra);
1118c9083b85SXin LI state->offset += BITS(state->extra);
1119c9083b85SXin LI DROPBITS(state->extra);
1120c9083b85SXin LI state->back += state->extra;
1121c9083b85SXin LI }
1122c9083b85SXin LI #ifdef INFLATE_STRICT
1123c9083b85SXin LI if (state->offset > state->dmax) {
1124c9083b85SXin LI strm->msg = (char *)"invalid distance too far back";
1125c9083b85SXin LI state->mode = BAD;
1126c9083b85SXin LI break;
1127c9083b85SXin LI }
1128c9083b85SXin LI #endif
1129c9083b85SXin LI Tracevv((stderr, "inflate: distance %u\n", state->offset));
1130c9083b85SXin LI state->mode = MATCH;
1131cd882207SXin LI /* fallthrough */
1132c9083b85SXin LI case MATCH:
1133c9083b85SXin LI if (left == 0) goto inf_leave;
1134c9083b85SXin LI copy = out - left;
1135c9083b85SXin LI if (state->offset > copy) { /* copy from window */
1136c9083b85SXin LI copy = state->offset - copy;
1137c9083b85SXin LI if (copy > state->whave) {
1138c9083b85SXin LI if (state->sane) {
1139c9083b85SXin LI strm->msg = (char *)"invalid distance too far back";
1140c9083b85SXin LI state->mode = BAD;
1141c9083b85SXin LI break;
1142c9083b85SXin LI }
1143c9083b85SXin LI #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1144c9083b85SXin LI Trace((stderr, "inflate.c too far\n"));
1145c9083b85SXin LI copy -= state->whave;
1146c9083b85SXin LI if (copy > state->length) copy = state->length;
1147c9083b85SXin LI if (copy > left) copy = left;
1148c9083b85SXin LI left -= copy;
1149c9083b85SXin LI state->length -= copy;
1150c9083b85SXin LI do {
1151c9083b85SXin LI *put++ = 0;
1152c9083b85SXin LI } while (--copy);
1153c9083b85SXin LI if (state->length == 0) state->mode = LEN;
1154c9083b85SXin LI break;
1155c9083b85SXin LI #endif
1156c9083b85SXin LI }
1157c9083b85SXin LI if (copy > state->wnext) {
1158c9083b85SXin LI copy -= state->wnext;
1159c9083b85SXin LI from = state->window + (state->wsize - copy);
1160c9083b85SXin LI }
1161c9083b85SXin LI else
1162c9083b85SXin LI from = state->window + (state->wnext - copy);
1163c9083b85SXin LI if (copy > state->length) copy = state->length;
1164c9083b85SXin LI }
1165c9083b85SXin LI else { /* copy from output */
1166c9083b85SXin LI from = put - state->offset;
1167c9083b85SXin LI copy = state->length;
1168c9083b85SXin LI }
1169c9083b85SXin LI if (copy > left) copy = left;
1170c9083b85SXin LI left -= copy;
1171c9083b85SXin LI state->length -= copy;
1172c9083b85SXin LI do {
1173c9083b85SXin LI *put++ = *from++;
1174c9083b85SXin LI } while (--copy);
1175c9083b85SXin LI if (state->length == 0) state->mode = LEN;
1176c9083b85SXin LI break;
1177c9083b85SXin LI case LIT:
1178c9083b85SXin LI if (left == 0) goto inf_leave;
1179c9083b85SXin LI *put++ = (unsigned char)(state->length);
1180c9083b85SXin LI left--;
1181c9083b85SXin LI state->mode = LEN;
1182c9083b85SXin LI break;
1183c9083b85SXin LI case CHECK:
1184c9083b85SXin LI if (state->wrap) {
1185c9083b85SXin LI NEEDBITS(32);
1186c9083b85SXin LI out -= left;
1187c9083b85SXin LI strm->total_out += out;
1188c9083b85SXin LI state->total += out;
1189c9083b85SXin LI if ((state->wrap & 4) && out)
1190c9083b85SXin LI strm->adler = state->check =
1191cd882207SXin LI UPDATE_CHECK(state->check, put - out, out);
1192c9083b85SXin LI out = left;
1193c9083b85SXin LI if ((state->wrap & 4) && (
1194c9083b85SXin LI #ifdef GUNZIP
1195c9083b85SXin LI state->flags ? hold :
1196c9083b85SXin LI #endif
1197c9083b85SXin LI ZSWAP32(hold)) != state->check) {
1198c9083b85SXin LI strm->msg = (char *)"incorrect data check";
1199c9083b85SXin LI state->mode = BAD;
1200c9083b85SXin LI break;
1201c9083b85SXin LI }
1202c9083b85SXin LI INITBITS();
1203c9083b85SXin LI Tracev((stderr, "inflate: check matches trailer\n"));
1204c9083b85SXin LI }
1205c9083b85SXin LI #ifdef GUNZIP
1206c9083b85SXin LI state->mode = LENGTH;
1207cd882207SXin LI /* fallthrough */
1208c9083b85SXin LI case LENGTH:
1209c9083b85SXin LI if (state->wrap && state->flags) {
1210c9083b85SXin LI NEEDBITS(32);
1211cd882207SXin LI if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
1212c9083b85SXin LI strm->msg = (char *)"incorrect length check";
1213c9083b85SXin LI state->mode = BAD;
1214c9083b85SXin LI break;
1215c9083b85SXin LI }
1216c9083b85SXin LI INITBITS();
1217c9083b85SXin LI Tracev((stderr, "inflate: length matches trailer\n"));
1218c9083b85SXin LI }
1219c9083b85SXin LI #endif
1220c9083b85SXin LI state->mode = DONE;
1221cd882207SXin LI /* fallthrough */
1222c9083b85SXin LI case DONE:
1223c9083b85SXin LI ret = Z_STREAM_END;
1224c9083b85SXin LI goto inf_leave;
1225c9083b85SXin LI case BAD:
1226c9083b85SXin LI ret = Z_DATA_ERROR;
1227c9083b85SXin LI goto inf_leave;
1228c9083b85SXin LI case MEM:
1229c9083b85SXin LI return Z_MEM_ERROR;
1230c9083b85SXin LI case SYNC:
1231cd882207SXin LI /* fallthrough */
1232c9083b85SXin LI default:
1233c9083b85SXin LI return Z_STREAM_ERROR;
1234c9083b85SXin LI }
1235c9083b85SXin LI
1236c9083b85SXin LI /*
1237c9083b85SXin LI Return from inflate(), updating the total counts and the check value.
1238c9083b85SXin LI If there was no progress during the inflate() call, return a buffer
1239c9083b85SXin LI error. Call updatewindow() to create and/or update the window state.
1240c9083b85SXin LI Note: a memory error from inflate() is non-recoverable.
1241c9083b85SXin LI */
1242c9083b85SXin LI inf_leave:
1243c9083b85SXin LI RESTORE();
1244c9083b85SXin LI if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1245c9083b85SXin LI (state->mode < CHECK || flush != Z_FINISH)))
1246c9083b85SXin LI if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1247c9083b85SXin LI state->mode = MEM;
1248c9083b85SXin LI return Z_MEM_ERROR;
1249c9083b85SXin LI }
1250c9083b85SXin LI in -= strm->avail_in;
1251c9083b85SXin LI out -= strm->avail_out;
1252c9083b85SXin LI strm->total_in += in;
1253c9083b85SXin LI strm->total_out += out;
1254c9083b85SXin LI state->total += out;
1255c9083b85SXin LI if ((state->wrap & 4) && out)
1256c9083b85SXin LI strm->adler = state->check =
1257cd882207SXin LI UPDATE_CHECK(state->check, strm->next_out - out, out);
1258c9083b85SXin LI strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1259c9083b85SXin LI (state->mode == TYPE ? 128 : 0) +
1260c9083b85SXin LI (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1261c9083b85SXin LI if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1262c9083b85SXin LI ret = Z_BUF_ERROR;
1263c9083b85SXin LI return ret;
1264c9083b85SXin LI }
1265c9083b85SXin LI
inflateEnd(z_streamp strm)12664717628eSXin LI int ZEXPORT inflateEnd(z_streamp strm) {
1267c9083b85SXin LI struct inflate_state FAR *state;
1268c9083b85SXin LI if (inflateStateCheck(strm))
1269c9083b85SXin LI return Z_STREAM_ERROR;
1270c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
1271c9083b85SXin LI if (state->window != Z_NULL) ZFREE(strm, state->window);
1272c9083b85SXin LI ZFREE(strm, strm->state);
1273c9083b85SXin LI strm->state = Z_NULL;
1274c9083b85SXin LI Tracev((stderr, "inflate: end\n"));
1275c9083b85SXin LI return Z_OK;
1276c9083b85SXin LI }
1277c9083b85SXin LI
inflateGetDictionary(z_streamp strm,Bytef * dictionary,uInt * dictLength)12784717628eSXin LI int ZEXPORT inflateGetDictionary(z_streamp strm, Bytef *dictionary,
12794717628eSXin LI uInt *dictLength) {
1280c9083b85SXin LI struct inflate_state FAR *state;
1281c9083b85SXin LI
1282c9083b85SXin LI /* check state */
1283c9083b85SXin LI if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1284c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
1285c9083b85SXin LI
1286c9083b85SXin LI /* copy dictionary */
1287c9083b85SXin LI if (state->whave && dictionary != Z_NULL) {
1288c9083b85SXin LI zmemcpy(dictionary, state->window + state->wnext,
1289c9083b85SXin LI state->whave - state->wnext);
1290c9083b85SXin LI zmemcpy(dictionary + state->whave - state->wnext,
1291c9083b85SXin LI state->window, state->wnext);
1292c9083b85SXin LI }
1293c9083b85SXin LI if (dictLength != Z_NULL)
1294c9083b85SXin LI *dictLength = state->whave;
1295c9083b85SXin LI return Z_OK;
1296c9083b85SXin LI }
1297c9083b85SXin LI
inflateSetDictionary(z_streamp strm,const Bytef * dictionary,uInt dictLength)12984717628eSXin LI int ZEXPORT inflateSetDictionary(z_streamp strm, const Bytef *dictionary,
12994717628eSXin LI uInt dictLength) {
1300c9083b85SXin LI struct inflate_state FAR *state;
1301c9083b85SXin LI unsigned long dictid;
1302c9083b85SXin LI int ret;
1303c9083b85SXin LI
1304c9083b85SXin LI /* check state */
1305c9083b85SXin LI if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1306c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
1307c9083b85SXin LI if (state->wrap != 0 && state->mode != DICT)
1308c9083b85SXin LI return Z_STREAM_ERROR;
1309c9083b85SXin LI
1310c9083b85SXin LI /* check for correct dictionary identifier */
1311c9083b85SXin LI if (state->mode == DICT) {
1312c9083b85SXin LI dictid = adler32(0L, Z_NULL, 0);
1313c9083b85SXin LI dictid = adler32(dictid, dictionary, dictLength);
1314c9083b85SXin LI if (dictid != state->check)
1315c9083b85SXin LI return Z_DATA_ERROR;
1316c9083b85SXin LI }
1317c9083b85SXin LI
1318c9083b85SXin LI /* copy dictionary to window using updatewindow(), which will amend the
1319c9083b85SXin LI existing dictionary if appropriate */
1320c9083b85SXin LI ret = updatewindow(strm, dictionary + dictLength, dictLength);
1321c9083b85SXin LI if (ret) {
1322c9083b85SXin LI state->mode = MEM;
1323c9083b85SXin LI return Z_MEM_ERROR;
1324c9083b85SXin LI }
1325c9083b85SXin LI state->havedict = 1;
1326c9083b85SXin LI Tracev((stderr, "inflate: dictionary set\n"));
1327c9083b85SXin LI return Z_OK;
1328c9083b85SXin LI }
1329c9083b85SXin LI
inflateGetHeader(z_streamp strm,gz_headerp head)13304717628eSXin LI int ZEXPORT inflateGetHeader(z_streamp strm, gz_headerp head) {
1331c9083b85SXin LI struct inflate_state FAR *state;
1332c9083b85SXin LI
1333c9083b85SXin LI /* check state */
1334c9083b85SXin LI if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1335c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
1336c9083b85SXin LI if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1337c9083b85SXin LI
1338c9083b85SXin LI /* save header structure */
1339c9083b85SXin LI state->head = head;
1340c9083b85SXin LI head->done = 0;
1341c9083b85SXin LI return Z_OK;
1342c9083b85SXin LI }
1343c9083b85SXin LI
1344c9083b85SXin LI /*
1345c9083b85SXin LI Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1346c9083b85SXin LI or when out of input. When called, *have is the number of pattern bytes
1347c9083b85SXin LI found in order so far, in 0..3. On return *have is updated to the new
1348c9083b85SXin LI state. If on return *have equals four, then the pattern was found and the
1349c9083b85SXin LI return value is how many bytes were read including the last byte of the
1350c9083b85SXin LI pattern. If *have is less than four, then the pattern has not been found
1351c9083b85SXin LI yet and the return value is len. In the latter case, syncsearch() can be
1352c9083b85SXin LI called again with more data and the *have state. *have is initialized to
1353c9083b85SXin LI zero for the first call.
1354c9083b85SXin LI */
syncsearch(unsigned FAR * have,const unsigned char FAR * buf,unsigned len)13554717628eSXin LI local unsigned syncsearch(unsigned FAR *have, const unsigned char FAR *buf,
13564717628eSXin LI unsigned len) {
1357c9083b85SXin LI unsigned got;
1358c9083b85SXin LI unsigned next;
1359c9083b85SXin LI
1360c9083b85SXin LI got = *have;
1361c9083b85SXin LI next = 0;
1362c9083b85SXin LI while (next < len && got < 4) {
1363c9083b85SXin LI if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1364c9083b85SXin LI got++;
1365c9083b85SXin LI else if (buf[next])
1366c9083b85SXin LI got = 0;
1367c9083b85SXin LI else
1368c9083b85SXin LI got = 4 - got;
1369c9083b85SXin LI next++;
1370c9083b85SXin LI }
1371c9083b85SXin LI *have = got;
1372c9083b85SXin LI return next;
1373c9083b85SXin LI }
1374c9083b85SXin LI
inflateSync(z_streamp strm)13754717628eSXin LI int ZEXPORT inflateSync(z_streamp strm) {
1376c9083b85SXin LI unsigned len; /* number of bytes to look at or looked at */
1377cd882207SXin LI int flags; /* temporary to save header status */
1378c9083b85SXin LI unsigned long in, out; /* temporary to save total_in and total_out */
1379c9083b85SXin LI unsigned char buf[4]; /* to restore bit buffer to byte string */
1380c9083b85SXin LI struct inflate_state FAR *state;
1381c9083b85SXin LI
1382c9083b85SXin LI /* check parameters */
1383c9083b85SXin LI if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1384c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
1385c9083b85SXin LI if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1386c9083b85SXin LI
1387c9083b85SXin LI /* if first time, start search in bit buffer */
1388c9083b85SXin LI if (state->mode != SYNC) {
1389c9083b85SXin LI state->mode = SYNC;
1390*6255c67cSXin LI state->hold >>= state->bits & 7;
1391c9083b85SXin LI state->bits -= state->bits & 7;
1392c9083b85SXin LI len = 0;
1393c9083b85SXin LI while (state->bits >= 8) {
1394c9083b85SXin LI buf[len++] = (unsigned char)(state->hold);
1395c9083b85SXin LI state->hold >>= 8;
1396c9083b85SXin LI state->bits -= 8;
1397c9083b85SXin LI }
1398c9083b85SXin LI state->have = 0;
1399c9083b85SXin LI syncsearch(&(state->have), buf, len);
1400c9083b85SXin LI }
1401c9083b85SXin LI
1402c9083b85SXin LI /* search available input */
1403c9083b85SXin LI len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1404c9083b85SXin LI strm->avail_in -= len;
1405c9083b85SXin LI strm->next_in += len;
1406c9083b85SXin LI strm->total_in += len;
1407c9083b85SXin LI
1408c9083b85SXin LI /* return no joy or set up to restart inflate() on a new block */
1409c9083b85SXin LI if (state->have != 4) return Z_DATA_ERROR;
1410cd882207SXin LI if (state->flags == -1)
1411cd882207SXin LI state->wrap = 0; /* if no header yet, treat as raw */
1412cd882207SXin LI else
1413cd882207SXin LI state->wrap &= ~4; /* no point in computing a check value now */
1414cd882207SXin LI flags = state->flags;
1415c9083b85SXin LI in = strm->total_in; out = strm->total_out;
1416c9083b85SXin LI inflateReset(strm);
1417c9083b85SXin LI strm->total_in = in; strm->total_out = out;
1418cd882207SXin LI state->flags = flags;
1419c9083b85SXin LI state->mode = TYPE;
1420c9083b85SXin LI return Z_OK;
1421c9083b85SXin LI }
1422c9083b85SXin LI
1423c9083b85SXin LI /*
1424c9083b85SXin LI Returns true if inflate is currently at the end of a block generated by
1425c9083b85SXin LI Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1426c9083b85SXin LI implementation to provide an additional safety check. PPP uses
1427c9083b85SXin LI Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1428c9083b85SXin LI block. When decompressing, PPP checks that at the end of input packet,
1429c9083b85SXin LI inflate is waiting for these length bytes.
1430c9083b85SXin LI */
inflateSyncPoint(z_streamp strm)14314717628eSXin LI int ZEXPORT inflateSyncPoint(z_streamp strm) {
1432c9083b85SXin LI struct inflate_state FAR *state;
1433c9083b85SXin LI
1434c9083b85SXin LI if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1435c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
1436c9083b85SXin LI return state->mode == STORED && state->bits == 0;
1437c9083b85SXin LI }
1438c9083b85SXin LI
inflateCopy(z_streamp dest,z_streamp source)14394717628eSXin LI int ZEXPORT inflateCopy(z_streamp dest, z_streamp source) {
1440c9083b85SXin LI struct inflate_state FAR *state;
1441c9083b85SXin LI struct inflate_state FAR *copy;
1442c9083b85SXin LI unsigned char FAR *window;
1443c9083b85SXin LI unsigned wsize;
1444c9083b85SXin LI
1445c9083b85SXin LI /* check input */
1446c9083b85SXin LI if (inflateStateCheck(source) || dest == Z_NULL)
1447c9083b85SXin LI return Z_STREAM_ERROR;
1448c9083b85SXin LI state = (struct inflate_state FAR *)source->state;
1449c9083b85SXin LI
1450c9083b85SXin LI /* allocate space */
1451c9083b85SXin LI copy = (struct inflate_state FAR *)
1452c9083b85SXin LI ZALLOC(source, 1, sizeof(struct inflate_state));
1453c9083b85SXin LI if (copy == Z_NULL) return Z_MEM_ERROR;
1454c9083b85SXin LI window = Z_NULL;
1455c9083b85SXin LI if (state->window != Z_NULL) {
1456c9083b85SXin LI window = (unsigned char FAR *)
1457c9083b85SXin LI ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1458c9083b85SXin LI if (window == Z_NULL) {
1459c9083b85SXin LI ZFREE(source, copy);
1460c9083b85SXin LI return Z_MEM_ERROR;
1461c9083b85SXin LI }
1462c9083b85SXin LI }
1463c9083b85SXin LI
1464c9083b85SXin LI /* copy state */
1465c9083b85SXin LI zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1466c9083b85SXin LI zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1467c9083b85SXin LI copy->strm = dest;
1468c9083b85SXin LI if (state->lencode >= state->codes &&
1469c9083b85SXin LI state->lencode <= state->codes + ENOUGH - 1) {
1470c9083b85SXin LI copy->lencode = copy->codes + (state->lencode - state->codes);
1471c9083b85SXin LI copy->distcode = copy->codes + (state->distcode - state->codes);
1472c9083b85SXin LI }
1473c9083b85SXin LI copy->next = copy->codes + (state->next - state->codes);
1474c9083b85SXin LI if (window != Z_NULL) {
1475c9083b85SXin LI wsize = 1U << state->wbits;
1476c9083b85SXin LI zmemcpy(window, state->window, wsize);
1477c9083b85SXin LI }
1478c9083b85SXin LI copy->window = window;
1479c9083b85SXin LI dest->state = (struct internal_state FAR *)copy;
1480c9083b85SXin LI return Z_OK;
1481c9083b85SXin LI }
1482c9083b85SXin LI
inflateUndermine(z_streamp strm,int subvert)14834717628eSXin LI int ZEXPORT inflateUndermine(z_streamp strm, int subvert) {
1484c9083b85SXin LI struct inflate_state FAR *state;
1485c9083b85SXin LI
1486c9083b85SXin LI if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1487c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
1488c9083b85SXin LI #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1489c9083b85SXin LI state->sane = !subvert;
1490c9083b85SXin LI return Z_OK;
1491c9083b85SXin LI #else
1492c9083b85SXin LI (void)subvert;
1493c9083b85SXin LI state->sane = 1;
1494c9083b85SXin LI return Z_DATA_ERROR;
1495c9083b85SXin LI #endif
1496c9083b85SXin LI }
1497c9083b85SXin LI
inflateValidate(z_streamp strm,int check)14984717628eSXin LI int ZEXPORT inflateValidate(z_streamp strm, int check) {
1499c9083b85SXin LI struct inflate_state FAR *state;
1500c9083b85SXin LI
1501c9083b85SXin LI if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1502c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
1503cd882207SXin LI if (check && state->wrap)
1504c9083b85SXin LI state->wrap |= 4;
1505c9083b85SXin LI else
1506c9083b85SXin LI state->wrap &= ~4;
1507c9083b85SXin LI return Z_OK;
1508c9083b85SXin LI }
1509c9083b85SXin LI
inflateMark(z_streamp strm)15104717628eSXin LI long ZEXPORT inflateMark(z_streamp strm) {
1511c9083b85SXin LI struct inflate_state FAR *state;
1512c9083b85SXin LI
1513c9083b85SXin LI if (inflateStateCheck(strm))
1514c9083b85SXin LI return -(1L << 16);
1515c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
1516c9083b85SXin LI return (long)(((unsigned long)((long)state->back)) << 16) +
1517c9083b85SXin LI (state->mode == COPY ? state->length :
1518c9083b85SXin LI (state->mode == MATCH ? state->was - state->length : 0));
1519c9083b85SXin LI }
1520c9083b85SXin LI
inflateCodesUsed(z_streamp strm)15214717628eSXin LI unsigned long ZEXPORT inflateCodesUsed(z_streamp strm) {
1522c9083b85SXin LI struct inflate_state FAR *state;
1523c9083b85SXin LI if (inflateStateCheck(strm)) return (unsigned long)-1;
1524c9083b85SXin LI state = (struct inflate_state FAR *)strm->state;
1525c9083b85SXin LI return (unsigned long)(state->next - state->codes);
1526c9083b85SXin LI }
1527