/* infback.c -- inflate using a call-back interface
* Copyright (C) 1995-2011 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/*
This code is largely copied from inflate.c. Normally either infback.o or
inflate.o would be linked into an application--not both. The interface
with inffast.c is retained so that optimized assembler-coded versions of
*/
#include "zutil.h"
#include "inftrees.h"
#include "inflate.h"
#include "inffast.h"
/* function prototypes */
/*
strm provides memory allocation functions in zalloc and zfree, or
Z_NULL to use the library memory allocation functions.
windowBits is in the range 8..15, and window is a user-supplied
window and output buffer that is 2**windowBits bytes.
*/
int windowBits;
const char *version;
int stream_size;
{
stream_size != (int)(sizeof(z_stream)))
return Z_VERSION_ERROR;
return Z_STREAM_ERROR;
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
#endif
}
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
#endif
sizeof(struct inflate_state));
return Z_OK;
}
/*
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 */
}
/* Macros for inflateBack(): */
/* Load returned state from inflate_fast() */
#define LOAD() \
do { \
} while (0)
/* Set state from registers for inflate_fast() */
#define RESTORE() \
do { \
} while (0)
/* Clear the input bit accumulator */
#define INITBITS() \
do { \
hold = 0; \
bits = 0; \
} while (0)
/* Assure that some input is available. If input is requested, but denied,
then return a Z_BUF_ERROR from inflateBack(). */
#define PULL() \
do { \
if (have == 0) { \
if (have == 0) { \
ret = Z_BUF_ERROR; \
goto inf_leave; \
} \
} \
} while (0)
/* Get a byte of input into the bit accumulator, or return from inflateBack()
with an error if there is no input available. */
#define PULLBYTE() \
do { \
PULL(); \
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 inflateBack() with
an error. */
#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)
/* Assure that some output space is available, by writing out the window
if it's full. If the write fails, return from inflateBack() with a
Z_BUF_ERROR. */
#define ROOM() \
do { \
if (left == 0) { \
ret = Z_BUF_ERROR; \
goto inf_leave; \
} \
} \
} while (0)
/*
strm provides the memory allocation functions and window buffer on input,
and provides information on the unused input on return. For Z_DATA_ERROR
returns, strm will also provide an error message.
in() and out() are the call-back input and output functions. When
inflateBack() needs more input, it calls in(). When inflateBack() has
filled the window with output, or when it completes with data in the
window, it calls out() to write out the data. The application must not
change the provided input until in() is called again or inflateBack()
inflateBack() returns.
in() and out() are called with a descriptor parameter provided in the
inflateBack() call. This parameter can be a structure that provides the
information required to do the read or write, as well as accumulated
information on the input and output such as totals and check values.
in() should return zero on failure. out() should return non-zero on
failure. If either in() or out() fails, than inflateBack() returns a
Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
was in() or out() that caused in the error. Otherwise, inflateBack()
returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
error, or Z_MEM_ERROR if it could not allocate memory for the state.
inflateBack() can also return Z_STREAM_ERROR if the input parameters
are not correct, i.e. strm is Z_NULL or the state was not initialized.
*/
{
{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
/* Check that the strm exists and that the state was initialized */
return Z_STREAM_ERROR;
/* Reset the state */
hold = 0;
bits = 0;
/* Inflate until end of block marked as last */
for (;;)
case TYPE:
/* determine and dispatch block type */
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:
/* get and verify stored block length */
BYTEBITS(); /* go to byte boundary */
NEEDBITS(32);
break;
}
INITBITS();
/* copy stored block from input to output */
PULL();
ROOM();
}
break;
case TABLE:
/* get dynamic table entries descriptor */
NEEDBITS(14);
DROPBITS(5);
DROPBITS(5);
DROPBITS(4);
#ifndef PKZIP_BUG_WORKAROUND
break;
}
#endif
/* get code length code lengths (not a typo) */
NEEDBITS(3);
DROPBITS(3);
}
if (ret) {
break;
}
/* get length and distance code code lengths */
for (;;) {
PULLBYTE();
}
}
else {
break;
}
DROPBITS(2);
}
len = 0;
DROPBITS(3);
}
else {
len = 0;
DROPBITS(7);
}
break;
}
while (copy--)
}
}
/* handle error breaks in while */
/* check for end-of-block code (better have one) */
break;
}
/* build code tables -- note: do not change the lenbits or distbits
values here (9 and 6) without reading the comments in inftrees.h
concerning the ENOUGH constants, which depend on those values */
if (ret) {
break;
}
if (ret) {
break;
}
case LEN:
/* use inflate_fast() if we have enough input and output */
RESTORE();
LOAD();
break;
}
/* get a literal, length, or end-of-block code */
for (;;) {
PULLBYTE();
}
for (;;) {
PULLBYTE();
}
}
/* process literal */
"inflate: literal '%c'\n" :
ROOM();
left--;
break;
}
/* process end of block */
break;
}
/* invalid code */
break;
}
/* length code -- get extra bits, if any */
}
/* get distance code */
for (;;) {
PULLBYTE();
}
for (;;) {
PULLBYTE();
}
}
break;
}
/* get distance extra bits, if any */
}
left : 0)) {
break;
}
/* copy match from window to output */
do {
ROOM();
}
else {
}
do {
} while (--copy);
break;
case DONE:
/* inflate stream terminated properly -- write leftover output */
ret = Z_STREAM_END;
ret = Z_BUF_ERROR;
}
goto inf_leave;
case BAD:
ret = Z_DATA_ERROR;
goto inf_leave;
default: /* can't happen, but makes compilers happy */
goto inf_leave;
}
/* Return unused input */
return ret;
}
{
return Z_STREAM_ERROR;
return Z_OK;
}