inflate.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach/* inflate.c -- zlib decompression
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder * Copyright (C) 1995-2005 Mark Adler
bb83db66bd9b3b4ce67be66419daf29886175276Andy Gimblett * For conditions of distribution and use, see copyright notice in zlib.h
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach * Change history:
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach * 1.2.beta0 24 Nov 2002
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach * - First version -- complete rewrite of inflate to simplify code, avoid
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach * creation of window when not needed, minimize use of window when it is
bb83db66bd9b3b4ce67be66419daf29886175276Andy Gimblett * needed, make inffast.c even faster, implement gzip decoding, and to
bb83db66bd9b3b4ce67be66419daf29886175276Andy Gimblett * improve code readability and style over the previous zlib inflate code
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach * 1.2.beta1 25 Nov 2002
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach * - Use pointers for available input and output checking in inffast.c
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach * - Remove input and output counters in inffast.c
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach * - Remove unnecessary second byte pull from length extra in inffast.c
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder * - Unroll direct copy to three copies per loop in inffast.c
5cc369fbceee1b13bd0f06e43620c46541d1d4f8Christian Maeder * 1.2.beta2 4 Dec 2002
dfc58f5ec6492d1a9b9babd9cdcdbb15baa6e657Christian Maeder * - Change external routine names to reduce potential conflicts
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach * - Correct filename to inffixed.h for fixed tables in inflate.c
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * - Make hbuf[] unsigned char to match parameter type in inflate.c
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder * to avoid negation problem on Alphas (64 bit) in inflate.c
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach * 1.2.beta3 22 Dec 2002
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach * - Add comments on state->bits assertion in inffast.c
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder * - Add comments on op field in inftrees.h
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach * - Fix bug in reuse of allocated window after inflateReset()
792df0347edab377785d98c63e2be8e2ce0a8bdeChristian Maeder * - Remove bit fields--back to byte structure for speed
2a5b885d9350ec6dd8bc4992ee91d2f68aa592f4Christian Maeder * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
0ea916d1e6aea10fd7b84f802fb5148a79d8c20aAndy Gimblett * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
04ceed96d1528b939f2e592d0656290d81d1c045Andy Gimblett * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
d9e78002fb0bf01a9b72d3d3415fdf9790bdfee8Andy Gimblett * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly * - Use local copies of stream next and avail values, as well as local bit
9f93b2a8b552789cd939d599504d39732672dc84Christian Maeder * buffer and bit count in inflate()--for speed when inflate_fast() not used
3b48e17c1da54ee669e70b626d9fbc32ce495b2cChristian Maeder * 1.2.beta4 1 Jan 2003
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly * - Move a comment on output buffer sizes from inffast.c to inflate.c
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder * - Add comments in inffast.c to introduce the inflate_fast() routine
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder * - Rearrange window copies in inflate_fast() for speed and simplification
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder * - Unroll last copy for window match in inflate_fast()
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder * - Use local copies of window variables in inflate_fast() for speed
c4b2418421546a337f83332fe0db04742dcd735dAndy Gimblett * - Pull out common write == 0 case for speed in inflate_fast()
bcd914850de931848b86d7728192a149f9c0108bChristian Maeder * - Make op and len in inflate_fast() unsigned for consistency
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * - Add FAR to lcode and dcode declarations in inflate_fast()
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder * - Simplified bad distance check in inflate_fast()
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder * source file infback.c to provide a call-back interface to inflate for
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder * programs like gzip and unzip -- uses window as output buffer to avoid
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder * window copying
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder * 1.2.beta5 1 Jan 2003
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * - Improved inflateBack() interface to allow the caller to provide initial
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * input in strm.
a78bb62cd6f0beb2dab862db865357fc9d3c25feChristian Maeder * - Fixed stored blocks bug in inflateBack()
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * 1.2.beta6 4 Jan 2003
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * - Added comments in inffast.c on effectiveness of POSTINC
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * - Typecasting all around to reduce compiler warnings
7e7d791d2f643ffd82843b78e424b6f9f68c24eeChristian Maeder * - Changed loops from while (1) or do {} while (1) to for (;;), again to
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * make compilers happy
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * - Changed type of window in inflateBackInit() to unsigned char *
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * 1.2.beta7 27 Jan 2003
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * - Changed many types to unsigned or unsigned short to avoid warnings
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * - Added inflateCopy() function
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder * 1.2.0 9 Mar 2003
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder * - Changed inflateBack() interface to provide separate opaque descriptors
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder * for the in() and out() functions
12b2ae689353ecbaad720a9af9f9be01c1a3fe2dChristian Maeder * - Changed inflateBack() argument and in_func typedef to swap the length
8db2221917c1bc569614f3481bcdb3b988facaedChristian Maeder * and buffer address return values for the input function
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder * - Check next_in and next_out for Z_NULL on entry to inflate()
e771539425f4a0abef9f94cf4b63690f3603f682Andy Gimblett * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
bb83db66bd9b3b4ce67be66419daf29886175276Andy Gimblett/* function prototypes */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maederlocal void fixedtables OF((struct inflate_state FAR *state));
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maederlocal int updatewindow OF((z_streamp strm, unsigned out));
842ae753ab848a8508c4832ab64296b929167a97Christian Maederlocal unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
d297a45fc73aa6c4a1f9d073c3170611415f324bAndy Gimblett if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
bb83db66bd9b3b4ce67be66419daf29886175276Andy Gimblett state = (struct inflate_state FAR *)strm->state;
a79fe3aad8743ea57e473ea5f66a723244cb9c0eMarkus Roggenbach strm->total_in = strm->total_out = state->total = 0;
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder strm->adler = 1; /* to support ill-conceived Java test suite */
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder state->lencode = state->distcode = state->next = state->codes;
d297a45fc73aa6c4a1f9d073c3170611415f324bAndy Gimblett if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly state = (struct inflate_state FAR *)strm->state;
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reillyint ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
d297a45fc73aa6c4a1f9d073c3170611415f324bAndy Gimblett if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder strm->msg = Z_NULL; /* in case we return an error */
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly if (strm->zfree == (free_func)0) strm->zfree = zcfree;
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly ZALLOC(strm, 1, sizeof(struct inflate_state));
2a5b885d9350ec6dd8bc4992ee91d2f68aa592f4Christian Maeder strm->state = (struct internal_state FAR *)state;
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reillyint ZEXPORT inflateInit_(strm, version, stream_size)
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly return inflateInit2_(strm, DEF_WBITS, version, stream_size);
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly Return state with length and distance decoding tables and index sizes set to
bb83db66bd9b3b4ce67be66419daf29886175276Andy Gimblett fixed code decoding. Normally this returns fixed tables from inffixed.h.
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder If BUILDFIXED is defined, then instead this routine builds the tables the
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder first time it's called, and returns those tables the first time and
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder thereafter. This reduces the size of the code by about 2K bytes, in
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder exchange for a little execution time. However, BUILDFIXED should not be
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder used for threaded applications, since the rewriting of the tables and virgin
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder may not be thread-safe.
05cc55892e6c93bdd7b9c3f100ab1bb65fe6a21eLiam O'Reilly /* build fixed huffman tables if first call (may not be thread safe) */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder /* distance table */
bcd914850de931848b86d7728192a149f9c0108bChristian Maeder inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
d326dac41dadbe2b84bb7021cbfd91f4dd4a19bcAndy Gimblett /* do this just once */
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly#else /* !BUILDFIXED */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder#endif /* BUILDFIXED */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder those tables to stdout, which would be piped to inffixed.h. A small program
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder can simply call makefixed to do this:
bb83db66bd9b3b4ce67be66419daf29886175276Andy Gimblett void makefixed(void);
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder int main(void)
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder Then that can be linked with zlib built with MAKEFIXED defined and run:
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder puts(" /* inffixed.h -- table for decoding fixed codes");
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly puts(" * Generated automatically by makefixed().");
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder puts(" /* WARNING: this file should *not* be used by applications.");
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly puts(" It is part of the implementation of this library and is");
d04c328b10f17ec78001a94d694f7188ebd8c03cAndy Gimblett puts(" subject to change. Applications should only use zlib.h.");
d04c328b10f17ec78001a94d694f7188ebd8c03cAndy Gimblett printf(" static const code lenfix[%u] = {", size);
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder printf("\n static const code distfix[%u] = {", size);
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder#endif /* MAKEFIXED */
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly Update the window with the last wsize (normally 32K) bytes written before
d04c328b10f17ec78001a94d694f7188ebd8c03cAndy Gimblett returning. If window does not exist yet, create it. This is only called
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder when a window is already in use, or when output has been written during this
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder inflate call, but the end of the deflate stream has not been reached yet.
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder It is also called to create a window for dictionary data when a dictionary
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly Providing output buffers larger than 32K to inflate() should provide a speed
d04c328b10f17ec78001a94d694f7188ebd8c03cAndy Gimblett advantage, since only the last 32K of output is copied to the sliding window
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder upon return from inflate(), and since all distances after the first 32K of
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder output will fall in the output data, making match copies simpler and faster.
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder The advantage may be dependent on the size of the processor's data caches.
816c50f9135a598dfdcfb2af8a80390bc42a9b24Liam O'Reilly state = (struct inflate_state FAR *)strm->state;
d04c328b10f17ec78001a94d694f7188ebd8c03cAndy Gimblett /* if it hasn't been done already, allocate space for the window */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder sizeof(unsigned char));
d04c328b10f17ec78001a94d694f7188ebd8c03cAndy Gimblett /* if window not in use yet, initialize */
816c50f9135a598dfdcfb2af8a80390bc42a9b24Liam O'Reilly /* copy state->wsize or less output bytes into the circular window */
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder zmemcpy(state->window + state->write, strm->next_out - copy, dist);
816c50f9135a598dfdcfb2af8a80390bc42a9b24Liam O'Reilly zmemcpy(state->window, strm->next_out - copy, copy);
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder if (state->write == state->wsize) state->write = 0;
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder if (state->whave < state->wsize) state->whave += dist;
5a859092c242b0e37183a44c3c79479125b2920aChristian Maeder/* Macros for inflate(): */
816c50f9135a598dfdcfb2af8a80390bc42a9b24Liam O'Reilly/* check function to use adler32() for zlib or crc32() for gzip */
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
2f35e5f6757968746dbab385be21fcae52378a3fLiam O'Reilly# define UPDATE(check, buf, len) adler32(check, buf, len)
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder/* check macros for header crc */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder/* Load registers with state in inflate() for speed */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder/* Restore state from registers in inflate() */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder/* Clear the input bit accumulator */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder/* Get a byte of input into the bit accumulator, or return from inflate()
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder if there is no input available. */
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder hold += (unsigned long)(*next++) << bits; \
c56c0630e0299eca5dd603cdac49aab4463c0671Liam O'Reilly/* Assure that there are at least n bits in the bit accumulator. If there is
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder not enough available input to do that, then return from inflate(). */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder while (bits < (unsigned)(n)) \
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder/* Return the low n bits of the bit accumulator (n < 16) */
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reilly/* Remove n bits from the bit accumulator */
0b8146e4f675518993a34eb2255ad7ddd7bf82a4Christian Maeder bits -= (unsigned)(n); \
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reilly/* Remove zero to seven bits as needed to go to a byte boundary */
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly/* Reverse the bytes in a 32-bit value */
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder inflate() uses a state machine to process as much input data and generate as
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder much output data as possible before returning. The state machine is
096d1f4ecffdbaa9e8543b712f24a636ba5accffLiam O'Reilly structured roughly as follows:
9aeda2b3ae8ce0b018955521e4ca835a8ba8a27bLiam O'Reilly for (;;) switch (state) {
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder if (not enough input data or output space to make progress)
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder ... make progress ...
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder state = STATEm;
0b8146e4f675518993a34eb2255ad7ddd7bf82a4Christian Maeder so when inflate() is called again, the same case is attempted again, and
0b8146e4f675518993a34eb2255ad7ddd7bf82a4Christian Maeder if the appropriate resources are provided, the machine proceeds to the
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder next state. The NEEDBITS() macro is usually the way the state evaluates
0b8146e4f675518993a34eb2255ad7ddd7bf82a4Christian Maeder whether it can proceed or should return. NEEDBITS() does the return if
0b8146e4f675518993a34eb2255ad7ddd7bf82a4Christian Maeder the requested bits are not available. The typical use of the BITS macros
0b8146e4f675518993a34eb2255ad7ddd7bf82a4Christian Maeder ... do something with BITS(n) ...
0b8146e4f675518993a34eb2255ad7ddd7bf82a4Christian Maeder where NEEDBITS(n) either returns from inflate() if there isn't enough
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder input left to load n bits into the accumulator, or it continues. BITS(n)
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder gives the low n bits in the accumulator. When done, DROPBITS(n) drops
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder the low n bits off the accumulator. INITBITS() clears the accumulator
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder and sets the number of available bits to zero. BYTEBITS() discards just
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder enough bits to put the accumulator on a byte boundary. After BYTEBITS()
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder if there is no input available. The decoding of variable length codes uses
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder PULLBYTE() directly in order to pull just enough bytes to decode the next
0b8146e4f675518993a34eb2255ad7ddd7bf82a4Christian Maeder code, and no more.
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder Some states loop until they get enough input, making sure that enough
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder state information is maintained to continue the loop where it left off
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder if NEEDBITS() returns in the loop. For example, want, need, and keep
70a691ea12f53381209a3709cdd325df5fc0a0c8Christian Maeder would all have to actually be part of the saved state in case NEEDBITS()
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder while (want < need) {
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder keep[want++] = BITS(n);
2bb060537a37352251aa04d8dc09aa53aad5d4bfLiam O'Reilly state = STATEx;
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder As shown above, if the next state is also the next case, then the break
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly A state may also return if there is not enough output space available to
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly complete that state. Those states are copying stored data, writing a
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly literal byte, and copying a matching string.
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly When returning, a "goto inf_leave" is used to update the total counters,
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly update the check value, and determine whether any progress has been made
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly during that inflate() call in order to return the proper return code.
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly Progress is defined as a change in either strm->avail_in or strm->avail_out.
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly When there is a window, goto inf_leave will update the window with the last
2bb060537a37352251aa04d8dc09aa53aad5d4bfLiam O'Reilly output written. If a goto inf_leave occurs in the middle of decompression
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly and there is no window currently, goto inf_leave will create one and copy
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly output to the window for the next call of inflate().
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder In this implementation, the flush parameter of inflate() only affects the
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder return code (per zlib.h). inflate() always writes as much as possible to
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder strm->next_out, given the space available and the provided input--the effect
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly the allocation of and copying into a sliding window until necessary, which
2bb060537a37352251aa04d8dc09aa53aad5d4bfLiam O'Reilly provides the effect documented in zlib.h for Z_FINISH when the entire input
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly stream available. So the only thing the flush parameter actually does is:
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder will return Z_BUF_ERROR if it has not reached the end of the stream.
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly unsigned have, left; /* available input and output */
fd4ad12563262ebe380d810df8f7755cfab5fb42Liam O'Reilly unsigned in, out; /* save starting available input and output */
7cbbd12f559c5c700f521a52424b098db198f1b4Liam O'Reilly unsigned copy; /* number of stored or match bytes to copy */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder unsigned char FAR *from; /* where to copy match bytes from */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder code this; /* current decoding table entry */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder unsigned len; /* length to copy for repeats, bits to drop */
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
7cbbd12f559c5c700f521a52424b098db198f1b4Liam O'Reilly static const unsigned short order[19] = /* permutation of code lengths */
7cbbd12f559c5c700f521a52424b098db198f1b4Liam O'Reilly {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
7cbbd12f559c5c700f521a52424b098db198f1b4Liam O'Reilly if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
7cbbd12f559c5c700f521a52424b098db198f1b4Liam O'Reilly (strm->next_in == Z_NULL && strm->avail_in != 0))
9f31535736c3d43a98f0157efaa7f87ea73c9be0Liam O'Reilly state = (struct inflate_state FAR *)strm->state;
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder if (!(state->wrap & 1) || /* check if zlib header allowed */
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder strm->msg = (char *)"incorrect header check";
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder strm->msg = (char *)"unknown compression method";
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder Tracev((stderr, "inflate: zlib header ok\n"));
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder strm->adler = state->check = adler32(0L, Z_NULL, 0);
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder state->mode = hold & 0x200 ? DICTID : TYPE;
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder strm->msg = (char *)"unknown compression method";
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder strm->msg = (char *)"unknown header flags set";
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder state->head->text = (int)((hold >> 8) & 1);
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder if (state->flags & 0x0200) CRC2(state->check, hold);
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder if (state->flags & 0x0200) CRC4(state->check, hold);
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder if (state->flags & 0x0200) CRC2(state->check, hold);
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder if (state->flags & 0x0200) CRC2(state->check, hold);
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder state->check = crc32(state->check, next, copy);
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder state->check = crc32(state->check, next, copy);
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder state->head->comment[state->length++] = len;
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder state->check = crc32(state->check, next, copy);
dd7da1b5fedc05b92ba023ebd803e6f4a662503bChristian Maeder state->head->hcrc = (int)((state->flags >> 9) & 1);
5b5db1d788d5240070930175f1322dab56279f99Andy Gimblett strm->adler = state->check = crc32(0L, Z_NULL, 0);
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder strm->adler = state->check = REVERSE(hold);
5b5db1d788d5240070930175f1322dab56279f99Andy Gimblett strm->adler = state->check = adler32(0L, Z_NULL, 0);
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder case 0: /* stored block */
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder Tracev((stderr, "inflate: stored block%s\n",
842ae753ab848a8508c4832ab64296b929167a97Christian Maeder Tracev((stderr, "inflate: fixed codes block%s\n",
7f24d24e63854a9a2539c2dac55198f746ad57dbChristian Maeder Tracev((stderr, "inflate: dynamic codes block%s\n",
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
5b5db1d788d5240070930175f1322dab56279f99Andy Gimblett strm->msg = (char *)"invalid stored block lengths";
ad872d5e07383c8fec42bddf02a13d1fbcac52b2Christian Maeder Tracev((stderr, "inflate: stored length %u\n",
case TABLE:
#ifndef PKZIP_BUG_WORKAROUND
case LENLENS:
if (ret) {
case CODELENS:
PULLBYTE();
len = 0;
len = 0;
while (copy--)
if (ret) {
if (ret) {
case LEN:
RESTORE();
LOAD();
PULLBYTE();
PULLBYTE();
case LENEXT:
case DIST:
PULLBYTE();
PULLBYTE();
case DISTEXT:
#ifdef INFLATE_STRICT
case MATCH:
} while (--copy);
case LIT:
left--;
case CHECK:
if (out)
#ifdef GUNZIP
INITBITS();
#ifdef GUNZIP
case LENGTH:
INITBITS();
case DONE:
goto inf_leave;
case BAD:
goto inf_leave;
case MEM:
return Z_MEM_ERROR;
case SYNC:
return Z_STREAM_ERROR;
RESTORE();
return Z_MEM_ERROR;
return ret;
return Z_STREAM_ERROR;
return Z_OK;
unsigned long id;
return Z_STREAM_ERROR;
return Z_DATA_ERROR;
return Z_MEM_ERROR;
return Z_OK;
return Z_OK;
unsigned len;
unsigned got;
unsigned next;
next = 0;
got++;
got = 0;
next++;
return next;
len = 0;
return Z_OK;
unsigned wsize;
return Z_STREAM_ERROR;
return Z_MEM_ERROR;
return Z_OK;