(deps/libz) Cleanups

This commit is contained in:
twinaphex 2020-07-02 18:48:23 +02:00
parent f14f418eb7
commit c86ba43c73

434
deps/libz/inflate.c vendored
View File

@ -95,44 +95,31 @@
#define Z_TREES 6
#endif
/* function prototypes */
int inflateReset2(z_streamp strm, int windowBits);
static void fixedtables (struct inflate_state FAR *state);
static int updatewindow (z_streamp strm, const unsigned char FAR *end,
unsigned copy);
#ifdef BUILDFIXED
void makefixed (void);
#endif
static unsigned syncsearch (unsigned FAR *have, const unsigned char FAR *buf,
unsigned len);
long inflateMark(z_streamp strm);
int inflateResetKeep(z_streamp strm);
int inflateUndermine(z_streamp strm, int subvert);
int inflateGetDictionary(z_streamp strm, Bytef *dictionary, uInt *dictLength);
int inflateResetKeep(z_streamp strm)
{
struct inflate_state FAR *state;
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
strm->total_in = strm->total_out = state->total = 0;
strm->msg = Z_NULL;
if (state->wrap) /* to support ill-conceived Java test suite */
strm->adler = state->wrap & 1;
state->mode = HEAD;
state->last = 0;
if ( strm == Z_NULL ||
strm->state == Z_NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
strm->total_in = strm->total_out = state->total = 0;
strm->msg = Z_NULL;
/* to support ill-conceived Java test suite */
if (state->wrap)
strm->adler = state->wrap & 1;
state->mode = HEAD;
state->last = 0;
state->havedict = 0;
state->head = Z_NULL;
state->hold = 0;
state->bits = 0;
state->lencode = state->distcode = state->next = state->codes;
state->sane = 1;
state->back = -1;
state->head = Z_NULL;
state->hold = 0;
state->bits = 0;
state->lencode = state->distcode = state->next = state->codes;
state->sane = 1;
state->back = -1;
return Z_OK;
}
@ -140,11 +127,14 @@ int inflateReset(z_streamp strm)
{
struct inflate_state FAR *state;
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
state->wsize = 0;
state->whave = 0;
state->wnext = 0;
return inflateResetKeep(strm);
}
@ -154,17 +144,20 @@ int inflateReset2(z_streamp strm, int windowBits)
struct inflate_state FAR *state = NULL;
/* get the state */
if (strm == Z_NULL || strm->state == Z_NULL)
if ( strm == Z_NULL ||
strm->state == Z_NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
state = (struct inflate_state FAR *)strm->state;
/* extract wrap request from windowBits parameter */
if (windowBits < 0) {
wrap = 0;
windowBits = -windowBits;
if (windowBits < 0)
{
wrap = 0;
windowBits = -windowBits;
}
else {
wrap = (windowBits >> 4) + 1;
else
{
wrap = (windowBits >> 4) + 1;
#ifdef GUNZIP
if (windowBits < 48)
windowBits &= 15;
@ -174,28 +167,35 @@ int inflateReset2(z_streamp strm, int windowBits)
/* set number of window bits, free window if different */
if (windowBits && (windowBits < 8 || windowBits > 15))
return Z_STREAM_ERROR;
if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
if ( state->window != Z_NULL &&
state->wbits != (unsigned)windowBits)
{
ZFREE(strm, state->window);
state->window = Z_NULL;
}
/* update state and reset the rest of it */
state->wrap = wrap;
state->wrap = wrap;
state->wbits = (unsigned)windowBits;
return inflateReset(strm);
}
int inflateInit2_(z_streamp strm, int windowBits, const char *version, int stream_size)
int inflateInit2_(z_streamp strm, int windowBits,
const char *version, int stream_size)
{
int ret;
struct inflate_state FAR *state;
if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
if ( version == Z_NULL ||
version[0] != ZLIB_VERSION[0] ||
stream_size != (int)(sizeof(z_stream)))
return Z_VERSION_ERROR;
if (strm == Z_NULL) return Z_STREAM_ERROR;
strm->msg = Z_NULL; /* in case we return an error */
if (strm->zalloc == (alloc_func)0) {
if (strm == Z_NULL)
return Z_STREAM_ERROR;
strm->msg = Z_NULL; /* in case we return an error */
if (strm->zalloc == (alloc_func)0)
{
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
@ -207,15 +207,17 @@ int inflateInit2_(z_streamp strm, int windowBits, const char *version, int strea
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
strm->zfree = zcfree;
strm->zfree = zcfree;
#endif
state = (struct inflate_state FAR *)
state = (struct inflate_state FAR *)
ZALLOC(strm, 1, sizeof(struct inflate_state));
if (state == Z_NULL) return Z_MEM_ERROR;
strm->state = (struct internal_state FAR *)state;
state->window = Z_NULL;
ret = inflateReset2(strm, windowBits);
if (ret != Z_OK) {
if (state == Z_NULL)
return Z_MEM_ERROR;
strm->state = (struct internal_state FAR *)state;
state->window = Z_NULL;
ret = inflateReset2(strm, windowBits);
if (ret != Z_OK)
{
ZFREE(strm, state);
strm->state = Z_NULL;
}
@ -231,17 +233,24 @@ int inflatePrime(z_streamp strm, int bits, int value)
{
struct inflate_state FAR *state;
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
if (bits < 0) {
if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
if (bits < 0)
{
state->hold = 0;
state->bits = 0;
return Z_OK;
}
if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
value &= (1L << bits) - 1;
state->hold += value << state->bits;
state->bits += bits;
if (bits > 16 || state->bits + bits > 32)
return Z_STREAM_ERROR;
value &= (1L << bits) - 1;
state->hold += value << state->bits;
state->bits += bits;
return Z_OK;
}
@ -263,24 +272,29 @@ static void fixedtables(struct inflate_state FAR *state)
static code fixed[544];
/* build fixed huffman tables if first call (may not be thread safe) */
if (virgin) {
unsigned sym, bits;
if (virgin)
{
unsigned bits;
static code *next;
/* literal/length table */
sym = 0;
while (sym < 144) state->lens[sym++] = 8;
while (sym < 256) state->lens[sym++] = 9;
while (sym < 280) state->lens[sym++] = 7;
while (sym < 288) state->lens[sym++] = 8;
next = fixed;
lenfix = next;
bits = 9;
unsigned sym = 0;
while (sym < 144)
state->lens[sym++] = 8;
while (sym < 256)
state->lens[sym++] = 9;
while (sym < 280)
state->lens[sym++] = 7;
while (sym < 288)
state->lens[sym++] = 8;
next = fixed;
lenfix = next;
bits = 9;
inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
/* distance table */
sym = 0;
while (sym < 32) state->lens[sym++] = 5;
sym = 0;
while (sym < 32)
state->lens[sym++] = 5;
distfix = next;
bits = 5;
inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
@ -291,8 +305,8 @@ static void fixedtables(struct inflate_state FAR *state)
#else /* !BUILDFIXED */
# include "inffixed.h"
#endif /* BUILDFIXED */
state->lencode = lenfix;
state->lenbits = 9;
state->lencode = lenfix;
state->lenbits = 9;
state->distcode = distfix;
state->distbits = 5;
}
@ -374,46 +388,53 @@ void makefixed(void)
*/
static int updatewindow(z_streamp strm, const Bytef *end, unsigned copy)
{
struct inflate_state FAR *state;
unsigned dist;
state = (struct inflate_state FAR *)strm->state;
struct inflate_state FAR *state = (struct inflate_state FAR *)strm->state;
/* if it hasn't been done already, allocate space for the window */
if (state->window == Z_NULL) {
if (state->window == Z_NULL)
{
state->window = (unsigned char FAR *)
ZALLOC(strm, 1U << state->wbits,
sizeof(unsigned char));
if (state->window == Z_NULL) return 1;
if (state->window == Z_NULL)
return 1;
}
/* if window not in use yet, initialize */
if (state->wsize == 0) {
if (state->wsize == 0)
{
state->wsize = 1U << state->wbits;
state->wnext = 0;
state->whave = 0;
}
/* copy state->wsize or less output bytes into the circular window */
if (copy >= state->wsize) {
if (copy >= state->wsize)
{
zmemcpy(state->window, end - state->wsize, state->wsize);
state->wnext = 0;
state->whave = state->wsize;
}
else {
else
{
dist = state->wsize - state->wnext;
if (dist > copy) dist = copy;
zmemcpy(state->window + state->wnext, end - copy, dist);
copy -= dist;
if (copy) {
if (copy)
{
zmemcpy(state->window, end - copy, copy);
state->wnext = copy;
state->whave = state->wsize;
}
else {
else
{
state->wnext += dist;
if (state->wnext == state->wsize) state->wnext = 0;
if (state->whave < state->wsize) state->whave += dist;
if (state->wnext == state->wsize)
state->wnext = 0;
if (state->whave < state->wsize)
state->whave += dist;
}
}
return 0;
@ -513,88 +534,6 @@ static int updatewindow(z_streamp strm, const Bytef *end, unsigned copy)
bits -= bits & 7; \
} while (0)
/*
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 inflate(z_streamp strm, int flush)
{
struct inflate_state FAR *state;
@ -1203,10 +1142,14 @@ inf_leave:
int inflateEnd(z_streamp strm)
{
struct inflate_state FAR *state;
if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == Z_NULL)
if (
strm == Z_NULL ||
strm->state == Z_NULL ||
strm->zfree == Z_NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
if (state->window != Z_NULL) ZFREE(strm, state->window);
if (state->window != Z_NULL)
ZFREE(strm, state->window);
ZFREE(strm, strm->state);
strm->state = Z_NULL;
return Z_OK;
@ -1217,36 +1160,47 @@ int inflateGetDictionary(z_streamp strm, Bytef *dictionary, uInt *dictLength)
struct inflate_state FAR *state;
/* check state */
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
if (
strm == Z_NULL ||
strm->state == Z_NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
/* copy dictionary */
if (state->whave && dictionary != Z_NULL) {
if (state->whave && dictionary != Z_NULL)
{
zmemcpy(dictionary, state->window + state->wnext,
state->whave - state->wnext);
zmemcpy(dictionary + state->whave - state->wnext,
state->window, state->wnext);
}
if (dictLength != Z_NULL)
*dictLength = state->whave;
return Z_OK;
}
int inflateSetDictionary(z_streamp strm, const Bytef *dictionary, uInt dictLength)
int inflateSetDictionary(z_streamp strm,
const Bytef *dictionary, uInt dictLength)
{
struct inflate_state FAR *state;
unsigned long dictid;
int ret;
struct inflate_state FAR *state;
/* check state */
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
if ( strm == Z_NULL ||
strm->state == Z_NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
if (state->wrap != 0 && state->mode != DICT)
if ( state->wrap != 0 &&
state->mode != DICT)
return Z_STREAM_ERROR;
/* check for correct dictionary identifier */
if (state->mode == DICT) {
dictid = adler32(0L, Z_NULL, 0);
if (state->mode == DICT)
{
unsigned long dictid = adler32(0L, Z_NULL, 0);
dictid = adler32(dictid, dictionary, dictLength);
if (dictid != state->check)
return Z_DATA_ERROR;
@ -1255,10 +1209,13 @@ int inflateSetDictionary(z_streamp strm, const Bytef *dictionary, uInt dictLengt
/* copy dictionary to window using updatewindow(), which will amend the
existing dictionary if appropriate */
ret = updatewindow(strm, dictionary + dictLength, dictLength);
if (ret) {
if (ret)
{
state->mode = MEM;
return Z_MEM_ERROR;
}
state->havedict = 1;
return Z_OK;
}
@ -1268,13 +1225,18 @@ int inflateGetHeader(z_streamp strm, gz_headerp head)
struct inflate_state FAR *state;
/* check state */
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
if ( strm == Z_NULL ||
strm->state == Z_NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
if ((state->wrap & 2) == 0)
return Z_STREAM_ERROR;
/* save header structure */
state->head = head;
head->done = 0;
head->done = 0;
return Z_OK;
}
@ -1289,14 +1251,14 @@ int inflateGetHeader(z_streamp strm, gz_headerp head)
called again with more data and the *have state. *have is initialized to
zero for the first call.
*/
static unsigned syncsearch(unsigned FAR *have, const unsigned char FAR *buf, unsigned len)
static unsigned syncsearch(unsigned FAR *have,
const unsigned char FAR *buf, unsigned len)
{
unsigned got;
unsigned next;
unsigned got = *have;
unsigned next = 0;
got = *have;
next = 0;
while (next < len && got < 4) {
while (next < len && got < 4)
{
if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
got++;
else if (buf[next])
@ -1317,37 +1279,46 @@ int inflateSync(z_streamp strm)
struct inflate_state FAR *state;
/* check parameters */
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
if ( strm == Z_NULL ||
strm->state == Z_NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
if (strm->avail_in == 0 && state->bits < 8)
return Z_BUF_ERROR;
/* if first time, start search in bit buffer */
if (state->mode != SYNC) {
state->mode = SYNC;
if (state->mode != SYNC)
{
state->mode = SYNC;
state->hold <<= state->bits & 7;
state->bits -= state->bits & 7;
len = 0;
while (state->bits >= 8) {
buf[len++] = (unsigned char)(state->hold);
state->bits -= state->bits & 7;
len = 0;
while (state->bits >= 8)
{
buf[len++] = (unsigned char)(state->hold);
state->hold >>= 8;
state->bits -= 8;
state->bits -= 8;
}
state->have = 0;
state->have = 0;
syncsearch(&(state->have), buf, len);
}
/* search available input */
len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
strm->avail_in -= len;
strm->next_in += len;
strm->next_in += len;
strm->total_in += len;
/* return no joy or set up to restart inflate() on a new block */
if (state->have != 4) return Z_DATA_ERROR;
in = strm->total_in; out = strm->total_out;
if (state->have != 4)
return Z_DATA_ERROR;
in = strm->total_in;
out = strm->total_out;
inflateReset(strm);
strm->total_in = in; strm->total_out = out;
state->mode = TYPE;
strm->total_in = in;
strm->total_out = out;
state->mode = TYPE;
return Z_OK;
}
@ -1363,7 +1334,9 @@ int inflateSyncPoint(z_streamp strm)
{
struct inflate_state FAR *state;
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
if ( strm == Z_NULL ||
strm->state == Z_NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
return state->mode == STORED && state->bits == 0;
}
@ -1376,20 +1349,27 @@ int inflateCopy(z_streamp dest, z_streamp source)
unsigned wsize;
/* check input */
if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
source->zalloc == Z_NULL || source->zfree == Z_NULL)
if ( dest == Z_NULL ||
source == Z_NULL ||
source->state == Z_NULL ||
source->zalloc == Z_NULL ||
source->zfree == Z_NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)source->state;
/* allocate space */
copy = (struct inflate_state FAR *)
ZALLOC(source, 1, sizeof(struct inflate_state));
if (copy == Z_NULL) return Z_MEM_ERROR;
if (copy == Z_NULL)
return Z_MEM_ERROR;
window = Z_NULL;
if (state->window != Z_NULL) {
if (state->window != Z_NULL)
{
window = (unsigned char FAR *)
ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
if (window == Z_NULL) {
if (window == Z_NULL)
{
ZFREE(source, copy);
return Z_MEM_ERROR;
}
@ -1398,18 +1378,22 @@ int inflateCopy(z_streamp dest, z_streamp source)
/* copy state */
zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
if (state->lencode >= state->codes &&
state->lencode <= state->codes + ENOUGH - 1) {
copy->lencode = copy->codes + (state->lencode - state->codes);
if ( state->lencode >= state->codes &&
state->lencode <= state->codes + ENOUGH - 1)
{
copy->lencode = copy->codes + (state->lencode - state->codes);
copy->distcode = copy->codes + (state->distcode - state->codes);
}
copy->next = copy->codes + (state->next - state->codes);
if (window != Z_NULL) {
if (window != Z_NULL)
{
wsize = 1U << state->wbits;
zmemcpy(window, state->window, wsize);
}
copy->window = window;
dest->state = (struct internal_state FAR *)copy;
dest->state = (struct internal_state FAR *)copy;
return Z_OK;
}
@ -1419,8 +1403,10 @@ int inflateUndermine(z_streamp strm, int subvert)
if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
state = (struct inflate_state FAR *)strm->state;
state->sane = 1;
return Z_DATA_ERROR;
}
@ -1428,10 +1414,12 @@ long inflateMark(z_streamp strm)
{
struct inflate_state FAR *state = NULL;
if (strm == Z_NULL || strm->state == Z_NULL)
if ( strm == Z_NULL ||
strm->state == Z_NULL)
return -1L << 16;
state = (struct inflate_state FAR *)strm->state;
return ((long)(state->back) << 16) +
(state->mode == COPY ? state->length :
(state->mode == COPY ? state->length :
(state->mode == MATCH ? state->was - state->length : 0));
}