/* inflate.c -- zlib decompression
* Copyright (C) 1995-2005 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/*
* Change history:
*
* 1.2.beta0 24 Nov 2002
* - First version -- complete rewrite of inflate to simplify code, avoid
* creation of window when not needed, minimize use of window when it is
* needed, make inffast.c even faster, implement gzip decoding, and to
* improve code readability and style over the previous zlib inflate code
*
* 1.2.beta1 25 Nov 2002
* - Use pointers for available input and output checking in inffast.c
* - Remove input and output counters in inffast.c
* - Change inffast.c entry and loop from avail_in >= 7 to >= 6
* - Remove unnecessary second byte pull from length extra in inffast.c
* - Unroll direct copy to three copies per loop in inffast.c
*
* 1.2.beta2 4 Dec 2002
* - Change external routine names to reduce potential conflicts
* - Correct filename to inffixed.h for fixed tables in inflate.c
* - Make hbuf[] unsigned char to match parameter type in inflate.c
* - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
* to avoid negation problem on Alphas (64 bit) in inflate.c
*
* 1.2.beta3 22 Dec 2002
* - Add comments on state->bits assertion in inffast.c
* - Add comments on op field in inftrees.h
* - Fix bug in reuse of allocated window after inflateReset()
* - Remove bit fields--back to byte structure for speed
* - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
* - Change post-increments to pre-increments in inflate_fast(), PPC biased?
* - Add compile time option, POSTINC, to use post-increments instead (Intel?)
* - Make MATCH copy in inflate() much faster for when inflate_fast() not used
* - Use local copies of stream next and avail values, as well as local bit
* buffer and bit count in inflate()--for speed when inflate_fast() not used
*
* 1.2.beta4 1 Jan 2003
* - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
* - Add comments in inffast.c to introduce the inflate_fast() routine
* - Rearrange window copies in inflate_fast() for speed and simplification
* - Unroll last copy for window match in inflate_fast()
* - Use local copies of window variables in inflate_fast() for speed
* - Pull out common write == 0 case for speed in inflate_fast()
* - Make op and len in inflate_fast() unsigned for consistency
* - Add FAR to lcode and dcode declarations in inflate_fast()
* - Simplified bad distance check in inflate_fast()
* - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
* source file infback.c to provide a call-back interface to inflate for
* programs like gzip and unzip -- uses window as output buffer to avoid
* window copying
*
* 1.2.beta5 1 Jan 2003
* - Improved inflateBack() interface to allow the caller to provide initial
* input in strm.
* - Fixed stored blocks bug in inflateBack()
*
* 1.2.beta6 4 Jan 2003
* - Added comments in inffast.c on effectiveness of POSTINC
* - Typecasting all around to reduce compiler warnings
* - Changed loops from while (1) or do {} while (1) to for (;;), again to
* make compilers happy
* - Changed type of window in inflateBackInit() to unsigned char *
*
* 1.2.beta7 27 Jan 2003
* - Changed many types to unsigned or unsigned short to avoid warnings
* - Added inflateCopy() function
*
* 1.2.0 9 Mar 2003
* - Changed inflateBack() interface to provide separate opaque descriptors
* for the in() and out() functions
* - Changed inflateBack() argument and in_func typedef to swap the length
* and buffer address return values for the input function
* - Check next_in and next_out for Z_NULL on entry to inflate()
*
* The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
*/
#include "zutil.h"
#include "inftrees.h"
#include "inflate.h"
#include "inffast.h"
#ifdef MAKEFIXED
# ifndef BUILDFIXED
# define BUILDFIXED
# endif
#endif
/* function prototypes */
#ifdef BUILDFIXED
#endif
unsigned len));
{
return Z_OK;
}
int bits;
int value;
{
return Z_OK;
}
int windowBits;
const char *version;
int stream_size;
{
stream_size != (int)(sizeof(z_stream)))
return Z_VERSION_ERROR;
}
if (windowBits < 0) {
windowBits = -windowBits;
}
else {
#ifdef GUNZIP
#endif
}
return Z_STREAM_ERROR;
}
return inflateReset(strm);
}
const char *version;
int stream_size;
{
}
/*
Return state with length and distance decoding tables and index sizes set to
fixed code decoding. Normally this returns fixed tables from inffixed.h.
If BUILDFIXED is defined, then instead this routine builds the tables the
first time it's called, and returns those tables the first time and
thereafter. This reduces the size of the code by about 2K bytes, in
exchange for a little execution time. However, BUILDFIXED should not be
used for threaded applications, since the rewriting of the tables and virgin
may not be thread-safe.
*/
{
#ifdef BUILDFIXED
/* build fixed huffman tables if first call (may not be thread safe) */
if (virgin) {
sym = 0;
bits = 9;
/* distance table */
sym = 0;
bits = 5;
/* do this just once */
virgin = 0;
}
#else /* !BUILDFIXED */
# include "inffixed.h"
#endif /* BUILDFIXED */
}
#ifdef MAKEFIXED
#include <stdio.h>
/*
Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
those tables to stdout, which would be piped to inffixed.h. A small program
can simply call makefixed to do this:
void makefixed(void);
int main(void)
{
makefixed();
return 0;
}
Then that can be linked with zlib built with MAKEFIXED defined and run:
a.out > inffixed.h
*/
void makefixed()
{
fixedtables(&state);
puts(" /* inffixed.h -- table for decoding fixed codes");
puts(" * Generated automatically by makefixed().");
puts(" */");
puts("");
puts(" /* WARNING: this file should *not* be used by applications.");
puts(" It is part of the implementation of this library and is");
puts(" */");
puts("");
low = 0;
for (;;) {
putchar(',');
}
puts("\n };");
low = 0;
for (;;) {
putchar(',');
}
puts("\n };");
}
#endif /* MAKEFIXED */
/*
Update the window with the last wsize (normally 32K) bytes written before
returning. If window does not exist yet, create it. This is only called
when a window is already in use, or when output has been written during this
inflate call, but the end of the deflate stream has not been reached yet.
It is also called to create a window for dictionary data when a dictionary
is loaded.
Providing output buffers larger than 32K to inflate() should provide a speed
advantage, since only the last 32K of output is copied to the sliding window
upon return from inflate(), and since all distances after the first 32K of
output will fall in the output data, making match copies simpler and faster.
The advantage may be dependent on the size of the processor's data caches.
*/
unsigned out;
{
/* if it hasn't been done already, allocate space for the window */
sizeof(unsigned char));
}
/* if window not in use yet, initialize */
}
/* copy state->wsize or less output bytes into the circular window */
}
else {
if (copy) {
}
else {
}
}
return 0;
}
/* Macros for inflate(): */
/* check function to use adler32() for zlib or crc32() for gzip */
#ifdef GUNZIP
#else
#endif
/* check macros for header crc */
#ifdef GUNZIP
do { \
} while (0)
do { \
} while (0)
#endif
/* Load registers with state in inflate() for speed */
#define LOAD() \
do { \
} while (0)
/* Restore state from registers in inflate() */
#define RESTORE() \
do { \
} while (0)
/* Clear the input bit accumulator */
#define INITBITS() \
do { \
hold = 0; \
bits = 0; \
} while (0)
/* Get a byte of input into the bit accumulator, or return from inflate()
if there is no input available. */
#define PULLBYTE() \
do { \
have--; \
bits += 8; \
} while (0)
/* Assure that there are at least n bits in the bit accumulator. If there is
not enough available input to do that, then return from inflate(). */
#define NEEDBITS(n) \
do { \
while (bits < (unsigned)(n)) \
PULLBYTE(); \
} while (0)
/* Return the low n bits of the bit accumulator (n < 16) */
#define BITS(n) \
/* Remove n bits from the bit accumulator */
#define DROPBITS(n) \
do { \
hold >>= (n); \
bits -= (unsigned)(n); \
} while (0)
/* Remove zero to seven bits as needed to go to a byte boundary */
#define BYTEBITS() \
do { \
} while (0)
/* Reverse the bytes in a 32-bit value */
#define REVERSE(q) \
((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
(((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
/*
inflate() uses a state machine to process as much input data and generate as
much output data as possible before returning. The state machine is
structured roughly as follows:
for (;;) switch (state) {
...
case STATEn:
if (not enough input data or output space to make progress)
return;
... make progress ...
state = STATEm;
break;
...
}
so when inflate() is called again, the same case is attempted again, and
if the appropriate resources are provided, the machine proceeds to the
next state. The NEEDBITS() macro is usually the way the state evaluates
whether it can proceed or should return. NEEDBITS() does the return if
the requested bits are not available. The typical use of the BITS macros
is:
NEEDBITS(n);
... do something with BITS(n) ...
DROPBITS(n);
where NEEDBITS(n) either returns from inflate() if there isn't enough
input left to load n bits into the accumulator, or it continues. BITS(n)
gives the low n bits in the accumulator. When done, DROPBITS(n) drops
the low n bits off the accumulator. INITBITS() clears the accumulator
and sets the number of available bits to zero. BYTEBITS() discards just
enough bits to put the accumulator on a byte boundary. After BYTEBITS()
and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
if there is no input available. The decoding of variable length codes uses
PULLBYTE() directly in order to pull just enough bytes to decode the next
code, and no more.
Some states loop until they get enough input, making sure that enough
state information is maintained to continue the loop where it left off
if NEEDBITS() returns in the loop. For example, want, need, and keep
would all have to actually be part of the saved state in case NEEDBITS()
returns:
case STATEw:
while (want < need) {
NEEDBITS(n);
keep[want++] = BITS(n);
DROPBITS(n);
}
state = STATEx;
case STATEx:
As shown above, if the next state is also the next case, then the break
is omitted.
A state may also return if there is not enough output space available to
complete that state. Those states are copying stored data, writing a
literal byte, and copying a matching string.
When returning, a "goto inf_leave" is used to update the total counters,
update the check value, and determine whether any progress has been made
during that inflate() call in order to return the proper return code.
Progress is defined as a change in either strm->avail_in or strm->avail_out.
When there is a window, goto inf_leave will update the window with the last
output written. If a goto inf_leave occurs in the middle of decompression
and there is no window currently, goto inf_leave will create one and copy
output to the window for the next call of inflate().
In this implementation, the flush parameter of inflate() only affects the
return code (per zlib.h). inflate() always writes as much as possible to
strm->next_out, given the space available and the provided input--the effect
documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
the allocation of and copying into a sliding window until necessary, which
provides the effect documented in zlib.h for Z_FINISH when the entire input
stream available. So the only thing the flush parameter actually does is:
when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
will return Z_BUF_ERROR if it has not reached the end of the stream.
*/
int flush;
{
#ifdef GUNZIP
#endif
{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
return Z_STREAM_ERROR;
LOAD();
for (;;)
case HEAD:
break;
}
NEEDBITS(16);
#ifdef GUNZIP
INITBITS();
break;
}
#else
if (
#endif
break;
}
break;
}
DROPBITS(4);
break;
}
INITBITS();
break;
#ifdef GUNZIP
case FLAGS:
NEEDBITS(16);
break;
}
break;
}
INITBITS();
case TIME:
NEEDBITS(32);
INITBITS();
case OS:
NEEDBITS(16);
}
INITBITS();
case EXLEN:
NEEDBITS(16);
INITBITS();
}
case EXTRA:
if (copy) {
}
}
}
case NAME:
copy = 0;
do {
}
case COMMENT:
copy = 0;
do {
}
case HCRC:
NEEDBITS(16);
break;
}
INITBITS();
}
}
break;
#endif
case DICTID:
NEEDBITS(32);
INITBITS();
case DICT:
RESTORE();
return Z_NEED_DICT;
}
case TYPE:
case TYPEDO:
BYTEBITS();
break;
}
NEEDBITS(3);
DROPBITS(1);
switch (BITS(2)) {
case 0: /* stored block */
break;
case 1: /* fixed block */
break;
case 2: /* dynamic block */
break;
case 3:
}
DROPBITS(2);
break;
case STORED:
BYTEBITS(); /* go to byte boundary */
NEEDBITS(32);
break;
}
INITBITS();
case COPY:
if (copy) {
break;
}
break;
case TABLE:
NEEDBITS(14);
DROPBITS(5);
DROPBITS(5);
DROPBITS(4);
#ifndef PKZIP_BUG_WORKAROUND
break;
}
#endif
case LENLENS:
NEEDBITS(3);
DROPBITS(3);
}
if (ret) {
break;
}
case CODELENS:
for (;;) {
PULLBYTE();
}
}
else {
break;
}
DROPBITS(2);
}
len = 0;
DROPBITS(3);
}
else {
len = 0;
DROPBITS(7);
}
break;
}
while (copy--)
}
}
/* handle error breaks in while */
/* build code tables */
if (ret) {
break;
}
if (ret) {
break;
}
case LEN:
RESTORE();
LOAD();
break;
}
for (;;) {
PULLBYTE();
}
for (;;) {
PULLBYTE();
}
}
"inflate: literal '%c'\n" :
break;
}
break;
}
break;
}
case LENEXT:
}
case DIST:
for (;;) {
PULLBYTE();
}
for (;;) {
PULLBYTE();
}
}
break;
}
case DISTEXT:
}
#ifdef INFLATE_STRICT
break;
}
#endif
break;
}
case MATCH:
}
else
}
else { /* copy from output */
}
do {
} while (--copy);
break;
case LIT:
left--;
break;
case CHECK:
NEEDBITS(32);
if (out)
if ((
#ifdef GUNZIP
#endif
break;
}
INITBITS();
}
#ifdef GUNZIP
case LENGTH:
NEEDBITS(32);
break;
}
INITBITS();
}
#endif
case DONE:
ret = Z_STREAM_END;
goto inf_leave;
case BAD:
ret = Z_DATA_ERROR;
goto inf_leave;
case MEM:
return Z_MEM_ERROR;
case SYNC:
default:
return Z_STREAM_ERROR;
}
/*
Return from inflate(), updating the total counts and the check value.
If there was no progress during the inflate() call, return a buffer
Note: a memory error from inflate() is non-recoverable.
*/
RESTORE();
return Z_MEM_ERROR;
}
ret = Z_BUF_ERROR;
return ret;
}
{
return Z_STREAM_ERROR;
return Z_OK;
}
const Bytef *dictionary;
{
unsigned long id;
/* check state */
return Z_STREAM_ERROR;
/* check for correct dictionary id */
return Z_DATA_ERROR;
}
/* copy dictionary to window */
return Z_MEM_ERROR;
}
}
else {
}
return Z_OK;
}
{
/* check state */
/* save header structure */
return Z_OK;
}
/*
Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
or when out of input. When called, *have is the number of pattern bytes
found in order so far, in 0..3. On return *have is updated to the new
state. If on return *have equals four, then the pattern was found and the
return value is how many bytes were read including the last byte of the
pattern. If *have is less than four, then the pattern has not been found
yet and the return value is len. In the latter case, syncsearch() can be
called again with more data and the *have state. *have is initialized to
zero for the first call.
*/
unsigned len;
{
unsigned got;
unsigned next;
next = 0;
got++;
got = 0;
else
next++;
}
return next;
}
{
unsigned len; /* number of bytes to look at or looked at */
/* check parameters */
/* if first time, start search in bit buffer */
len = 0;
}
}
/* search available input */
/* return no joy or set up to restart inflate() on a new block */
return Z_OK;
}
/*
Returns true if inflate is currently at the end of a block generated by
Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
implementation to provide an additional safety check. PPP uses
Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
block. When decompressing, PPP checks that at the end of input packet,
inflate is waiting for these length bytes.
*/
{
}
{
unsigned wsize;
/* check input */
return Z_STREAM_ERROR;
/* allocate space */
return Z_MEM_ERROR;
}
}
/* copy state */
}
}
return Z_OK;
}