ยปCore Development>Code coverage>Modules/zlib/inflate.c

Python code coverage for Modules/zlib/inflate.c

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