#include "zbuild.h"
#include "zutil.h"
#include "cpu_features.h"
#include "inftrees.h"
#include "inflate.h"
#include "inffast.h"
#include "inflate_p.h"
#include "inffixed_tbl.h"
#include "functable.h"
static int inflateStateCheck(PREFIX3(stream) *strm);
static uint32_t syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len);
static int inflateStateCheck(PREFIX3(stream) *strm) {
struct inflate_state *state;
if (strm == NULL || strm->zalloc == NULL || strm->zfree == NULL)
return 1;
state = (struct inflate_state *)strm->state;
if (state == NULL || state->strm != strm || state->mode < HEAD || state->mode > SYNC)
return 1;
return 0;
}
int32_t Z_EXPORT PREFIX(inflateResetKeep)(PREFIX3(stream) *strm) {
struct inflate_state *state;
if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
strm->total_in = strm->total_out = state->total = 0;
strm->msg = NULL;
if (state->wrap)
strm->adler = state->wrap & 1;
state->mode = HEAD;
state->check = ADLER32_INITIAL_VALUE;
state->last = 0;
state->havedict = 0;
state->flags = -1;
state->dmax = 32768U;
state->head = NULL;
state->hold = 0;
state->bits = 0;
state->lencode = state->distcode = state->next = state->codes;
state->sane = 1;
state->back = -1;
INFLATE_RESET_KEEP_HOOK(strm);
Tracev((stderr, "inflate: reset\n"));
return Z_OK;
}
int32_t Z_EXPORT PREFIX(inflateReset)(PREFIX3(stream) *strm) {
struct inflate_state *state;
if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
state->whave = 0;
state->wnext = 0;
return PREFIX(inflateResetKeep)(strm);
}
int32_t Z_EXPORT PREFIX(inflateReset2)(PREFIX3(stream) *strm, int32_t windowBits) {
int wrap;
struct inflate_state *state;
if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if (windowBits < 0) {
wrap = 0;
windowBits = -windowBits;
} else {
wrap = (windowBits >> 4) + 5;
#ifdef GUNZIP
if (windowBits < 48)
windowBits &= 15;
#endif
}
if (windowBits && (windowBits < 8 || windowBits > 15))
return Z_STREAM_ERROR;
if (state->window != NULL && state->wbits != (unsigned)windowBits) {
ZFREE_WINDOW(strm, state->window);
state->window = NULL;
state->wsize = 0;
}
state->wrap = wrap;
state->wbits = (unsigned)windowBits;
return PREFIX(inflateReset)(strm);
}
int32_t Z_EXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int32_t windowBits, const char *version, int32_t stream_size) {
int32_t ret;
struct inflate_state *state;
cpu_check_features();
if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream))))
return Z_VERSION_ERROR;
if (strm == NULL)
return Z_STREAM_ERROR;
strm->msg = NULL;
if (strm->zalloc == NULL) {
strm->zalloc = zng_calloc;
strm->opaque = NULL;
}
if (strm->zfree == NULL)
strm->zfree = zng_cfree;
state = (struct inflate_state *) ZALLOC_STATE(strm, 1, sizeof(struct inflate_state));
if (state == NULL)
return Z_MEM_ERROR;
Tracev((stderr, "inflate: allocated\n"));
strm->state = (struct internal_state *)state;
state->strm = strm;
state->window = NULL;
state->wsize = 0;
state->mode = HEAD;
state->chunksize = functable.chunksize();
ret = PREFIX(inflateReset2)(strm, windowBits);
if (ret != Z_OK) {
ZFREE_STATE(strm, state);
strm->state = NULL;
return ret;
}
return ret;
}
int32_t Z_EXPORT PREFIX(inflateInit_)(PREFIX3(stream) *strm, const char *version, int32_t stream_size) {
return PREFIX(inflateInit2_)(strm, DEF_WBITS, version, stream_size);
}
int32_t Z_EXPORT PREFIX(inflatePrime)(PREFIX3(stream) *strm, int32_t bits, int32_t value) {
struct inflate_state *state;
if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
INFLATE_PRIME_HOOK(strm, bits, value);
state = (struct inflate_state *)strm->state;
if (bits < 0) {
state->hold = 0;
state->bits = 0;
return Z_OK;
}
if (bits > 16 || state->bits + (unsigned int)bits > 32)
return Z_STREAM_ERROR;
value &= (1L << bits) - 1;
state->hold += (unsigned)value << state->bits;
state->bits += (unsigned int)bits;
return Z_OK;
}
void Z_INTERNAL fixedtables(struct inflate_state *state) {
state->lencode = lenfix;
state->lenbits = 9;
state->distcode = distfix;
state->distbits = 5;
}
int Z_INTERNAL inflate_ensure_window(struct inflate_state *state) {
if (state->window == NULL) {
unsigned wsize = 1U << state->wbits;
state->window = (unsigned char *)ZALLOC_WINDOW(state->strm, (wsize * 2) + state->chunksize, sizeof(unsigned char));
if (state->window == NULL)
return Z_MEM_ERROR;
memset(state->window + wsize, 0, state->chunksize);
}
if (state->wsize == 0) {
state->wsize = 1U << state->wbits;
state->wnext = 0;
state->whave = 0;
}
return Z_OK;
}
#define PULLBYTE() \
do { \
if (have == 0) goto inf_leave; \
have--; \
hold += ((unsigned)(*next++) << bits); \
bits += 8; \
} while (0)
int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
struct inflate_state *state;
const unsigned char *next;
unsigned char *put;
unsigned have, left;
uint32_t hold;
unsigned bits;
uint32_t in, out;
unsigned copy;
code here;
code last;
unsigned len;
int32_t ret;
#ifdef GUNZIP
unsigned char hbuf[4];
#endif
static const uint16_t order[19] =
{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
if (inflateStateCheck(strm) || strm->next_out == NULL ||
(strm->next_in == NULL && strm->avail_in != 0))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if (state->mode == TYPE)
state->mode = TYPEDO;
LOAD();
in = have;
out = left;
ret = Z_OK;
for (;;)
switch (state->mode) {
case HEAD:
if (state->wrap == 0) {
state->mode = TYPEDO;
break;
}
NEEDBITS(16);
#ifdef GUNZIP
if ((state->wrap & 2) && hold == 0x8b1f) {
if (state->wbits == 0)
state->wbits = 15;
state->check = CRC32_INITIAL_VALUE;
CRC2(state->check, hold);
INITBITS();
state->mode = FLAGS;
break;
}
if (state->head != NULL)
state->head->done = -1;
if (!(state->wrap & 1) ||
#else
if (
#endif
((BITS(8) << 8) + (hold >> 8)) % 31) {
SET_BAD("incorrect header check");
break;
}
if (BITS(4) != Z_DEFLATED) {
SET_BAD("unknown compression method");
break;
}
DROPBITS(4);
len = BITS(4) + 8;
if (state->wbits == 0)
state->wbits = len;
if (len > 15 || len > state->wbits) {
SET_BAD("invalid window size");
break;
}
state->dmax = 1U << len;
state->flags = 0;
Tracev((stderr, "inflate: zlib header ok\n"));
strm->adler = state->check = ADLER32_INITIAL_VALUE;
state->mode = hold & 0x200 ? DICTID : TYPE;
INITBITS();
break;
#ifdef GUNZIP
case FLAGS:
NEEDBITS(16);
state->flags = (int)(hold);
if ((state->flags & 0xff) != Z_DEFLATED) {
SET_BAD("unknown compression method");
break;
}
if (state->flags & 0xe000) {
SET_BAD("unknown header flags set");
break;
}
if (state->head != NULL)
state->head->text = (int)((hold >> 8) & 1);
if ((state->flags & 0x0200) && (state->wrap & 4))
CRC2(state->check, hold);
INITBITS();
state->mode = TIME;
case TIME:
NEEDBITS(32);
if (state->head != NULL)
state->head->time = hold;
if ((state->flags & 0x0200) && (state->wrap & 4))
CRC4(state->check, hold);
INITBITS();
state->mode = OS;
case OS:
NEEDBITS(16);
if (state->head != NULL) {
state->head->xflags = (int)(hold & 0xff);
state->head->os = (int)(hold >> 8);
}
if ((state->flags & 0x0200) && (state->wrap & 4))
CRC2(state->check, hold);
INITBITS();
state->mode = EXLEN;
case EXLEN:
if (state->flags & 0x0400) {
NEEDBITS(16);
state->length = (uint16_t)hold;
if (state->head != NULL)
state->head->extra_len = (uint16_t)hold;
if ((state->flags & 0x0200) && (state->wrap & 4))
CRC2(state->check, hold);
INITBITS();
} else if (state->head != NULL) {
state->head->extra = NULL;
}
state->mode = EXTRA;
case EXTRA:
if (state->flags & 0x0400) {
copy = state->length;
if (copy > have)
copy = have;
if (copy) {
if (state->head != NULL && state->head->extra != NULL) {
len = state->head->extra_len - state->length;
memcpy(state->head->extra + len, next,
len + copy > state->head->extra_max ?
state->head->extra_max - len : copy);
}
if ((state->flags & 0x0200) && (state->wrap & 4))
state->check = PREFIX(crc32)(state->check, next, copy);
have -= copy;
next += copy;
state->length -= copy;
}
if (state->length)
goto inf_leave;
}
state->length = 0;
state->mode = NAME;
case NAME:
if (state->flags & 0x0800) {
if (have == 0) goto inf_leave;
copy = 0;
do {
len = (unsigned)(next[copy++]);
if (state->head != NULL && state->head->name != NULL && state->length < state->head->name_max)
state->head->name[state->length++] = (unsigned char)len;
} while (len && copy < have);
if ((state->flags & 0x0200) && (state->wrap & 4))
state->check = PREFIX(crc32)(state->check, next, copy);
have -= copy;
next += copy;
if (len)
goto inf_leave;
} else if (state->head != NULL) {
state->head->name = NULL;
}
state->length = 0;
state->mode = COMMENT;
case COMMENT:
if (state->flags & 0x1000) {
if (have == 0) goto inf_leave;
copy = 0;
do {
len = (unsigned)(next[copy++]);
if (state->head != NULL && state->head->comment != NULL
&& state->length < state->head->comm_max)
state->head->comment[state->length++] = (unsigned char)len;
} while (len && copy < have);
if ((state->flags & 0x0200) && (state->wrap & 4))
state->check = PREFIX(crc32)(state->check, next, copy);
have -= copy;
next += copy;
if (len)
goto inf_leave;
} else if (state->head != NULL) {
state->head->comment = NULL;
}
state->mode = HCRC;
case HCRC:
if (state->flags & 0x0200) {
NEEDBITS(16);
if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
SET_BAD("header crc mismatch");
break;
}
INITBITS();
}
if (state->head != NULL) {
state->head->hcrc = (int)((state->flags >> 9) & 1);
state->head->done = 1;
}
if ((state->wrap & 4) && state->flags)
strm->adler = state->check = functable.crc32_fold_reset(&state->crc_fold);
state->mode = TYPE;
break;
#endif
case DICTID:
NEEDBITS(32);
strm->adler = state->check = ZSWAP32(hold);
INITBITS();
state->mode = DICT;
case DICT:
if (state->havedict == 0) {
RESTORE();
return Z_NEED_DICT;
}
strm->adler = state->check = ADLER32_INITIAL_VALUE;
state->mode = TYPE;
case TYPE:
if (flush == Z_BLOCK || flush == Z_TREES)
goto inf_leave;
case TYPEDO:
if (state->window == NULL) {
RESTORE();
ret = inflate_ensure_window(state);
if (ret != Z_OK) {
ZFREE_STATE(strm, state);
strm->state = NULL;
return ret;
}
LOAD();
}
INFLATE_TYPEDO_HOOK(strm, flush);
if (state->last) {
BYTEBITS();
state->mode = CHECK;
break;
}
NEEDBITS(3);
state->last = BITS(1);
DROPBITS(1);
switch (BITS(2)) {
case 0:
Tracev((stderr, "inflate: stored block%s\n", state->last ? " (last)" : ""));
state->mode = STORED;
break;
case 1:
fixedtables(state);
Tracev((stderr, "inflate: fixed codes block%s\n", state->last ? " (last)" : ""));
state->mode = LEN_;
if (flush == Z_TREES) {
DROPBITS(2);
goto inf_leave;
}
break;
case 2:
Tracev((stderr, "inflate: dynamic codes block%s\n", state->last ? " (last)" : ""));
state->mode = TABLE;
break;
case 3:
SET_BAD("invalid block type");
}
DROPBITS(2);
break;
case STORED:
BYTEBITS();
NEEDBITS(32);
if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
SET_BAD("invalid stored block lengths");
break;
}
state->length = (uint16_t)hold;
Tracev((stderr, "inflate: stored length %u\n", state->length));
INITBITS();
state->mode = COPY_;
if (flush == Z_TREES)
goto inf_leave;
case COPY_:
state->mode = COPY;
case COPY:
copy = state->length;
if (copy) {
unsigned char *end = state->window + (state->wsize * 2);
int64_t diff = end - put;
copy = MIN(copy, have);
if (copy > diff) {
if (left > 0) {
RESTORE();
window_output_flush(strm);
LOAD();
diff = end - put;
}
copy = MIN(copy, (uint32_t)diff);
}
if (copy == 0)
goto inf_leave;
memcpy(put, next, copy);
have -= copy;
next += copy;
put += copy;
state->length -= copy;
break;
}
Tracev((stderr, "inflate: stored end\n"));
state->mode = TYPE;
break;
case TABLE:
NEEDBITS(14);
state->nlen = BITS(5) + 257;
DROPBITS(5);
state->ndist = BITS(5) + 1;
DROPBITS(5);
state->ncode = BITS(4) + 4;
DROPBITS(4);
#ifndef PKZIP_BUG_WORKAROUND
if (state->nlen > 286 || state->ndist > 30) {
SET_BAD("too many length or distance symbols");
break;
}
#endif
Tracev((stderr, "inflate: table sizes ok\n"));
state->have = 0;
state->mode = LENLENS;
case LENLENS:
while (state->have < state->ncode) {
NEEDBITS(3);
state->lens[order[state->have++]] = (uint16_t)BITS(3);
DROPBITS(3);
}
while (state->have < 19)
state->lens[order[state->have++]] = 0;
state->next = state->codes;
state->lencode = (const code *)(state->next);
state->lenbits = 7;
ret = zng_inflate_table(CODES, state->lens, 19, &(state->next), &(state->lenbits), state->work);
if (ret) {
SET_BAD("invalid code lengths set");
break;
}
Tracev((stderr, "inflate: code lengths ok\n"));
state->have = 0;
state->mode = CODELENS;
case CODELENS:
while (state->have < state->nlen + state->ndist) {
for (;;) {
here = state->lencode[BITS(state->lenbits)];
if (here.bits <= bits) break;
PULLBYTE();
}
if (here.val < 16) {
DROPBITS(here.bits);
state->lens[state->have++] = here.val;
} else {
if (here.val == 16) {
NEEDBITS(here.bits + 2);
DROPBITS(here.bits);
if (state->have == 0) {
SET_BAD("invalid bit length repeat");
break;
}
len = state->lens[state->have - 1];
copy = 3 + BITS(2);
DROPBITS(2);
} else if (here.val == 17) {
NEEDBITS(here.bits + 3);
DROPBITS(here.bits);
len = 0;
copy = 3 + BITS(3);
DROPBITS(3);
} else {
NEEDBITS(here.bits + 7);
DROPBITS(here.bits);
len = 0;
copy = 11 + BITS(7);
DROPBITS(7);
}
if (state->have + copy > state->nlen + state->ndist) {
SET_BAD("invalid bit length repeat");
break;
}
while (copy) {
--copy;
state->lens[state->have++] = (uint16_t)len;
}
}
}
if (state->mode == BAD)
break;
if (state->lens[256] == 0) {
SET_BAD("invalid code -- missing end-of-block");
break;
}
state->next = state->codes;
state->lencode = (const code *)(state->next);
state->lenbits = 9;
ret = zng_inflate_table(LENS, state->lens, state->nlen, &(state->next), &(state->lenbits), state->work);
if (ret) {
SET_BAD("invalid literal/lengths set");
break;
}
state->distcode = (const code *)(state->next);
state->distbits = 6;
ret = zng_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
&(state->next), &(state->distbits), state->work);
if (ret) {
SET_BAD("invalid distances set");
break;
}
Tracev((stderr, "inflate: codes ok\n"));
state->mode = LEN_;
if (flush == Z_TREES)
goto inf_leave;
case LEN_:
state->mode = LEN;
case LEN:
if (have >= INFLATE_FAST_MIN_HAVE && left >= INFLATE_FAST_MIN_LEFT) {
RESTORE();
zng_inflate_fast(strm);
LOAD();
if (state->mode == TYPE)
state->back = -1;
break;
}
state->back = 0;
for (;;) {
here = state->lencode[BITS(state->lenbits)];
if (here.bits <= bits)
break;
PULLBYTE();
}
if (here.op && (here.op & 0xf0) == 0) {
last = here;
for (;;) {
here = state->lencode[last.val + (BITS(last.bits + last.op) >> last.bits)];
if ((unsigned)last.bits + (unsigned)here.bits <= bits)
break;
PULLBYTE();
}
DROPBITS(last.bits);
state->back += last.bits;
}
DROPBITS(here.bits);
state->back += here.bits;
state->length = here.val;
if ((int)(here.op) == 0) {
Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
"inflate: literal '%c'\n" :
"inflate: literal 0x%02x\n", here.val));
state->mode = LIT;
break;
}
if (here.op & 32) {
Tracevv((stderr, "inflate: end of block\n"));
state->back = -1;
state->mode = TYPE;
break;
}
if (here.op & 64) {
SET_BAD("invalid literal/length code");
break;
}
state->extra = (here.op & 15);
state->mode = LENEXT;
case LENEXT:
if (state->extra) {
NEEDBITS(state->extra);
state->length += BITS(state->extra);
DROPBITS(state->extra);
state->back += state->extra;
}
Tracevv((stderr, "inflate: length %u\n", state->length));
state->was = state->length;
state->mode = DIST;
case DIST:
for (;;) {
here = state->distcode[BITS(state->distbits)];
if (here.bits <= bits)
break;
PULLBYTE();
}
if ((here.op & 0xf0) == 0) {
last = here;
for (;;) {
here = state->distcode[last.val + (BITS(last.bits + last.op) >> last.bits)];
if ((unsigned)last.bits + (unsigned)here.bits <= bits)
break;
PULLBYTE();
}
DROPBITS(last.bits);
state->back += last.bits;
}
DROPBITS(here.bits);
state->back += here.bits;
if (here.op & 64) {
SET_BAD("invalid distance code");
break;
}
state->offset = here.val;
state->extra = (here.op & 15);
state->mode = DISTEXT;
case DISTEXT:
if (state->extra) {
NEEDBITS(state->extra);
state->offset += BITS(state->extra);
DROPBITS(state->extra);
state->back += state->extra;
}
#ifdef INFLATE_STRICT
if (state->offset > state->dmax) {
SET_BAD("invalid distance too far back");
break;
}
#endif
Tracevv((stderr, "inflate: distance %u\n", state->offset));
state->mode = MATCH;
case MATCH: {
if (left == 0)
goto inf_leave;
unsigned char *end = state->window + (state->wsize * 2);
int64_t buf_left = end - put;
copy = state->length;
RESTORE();
if (copy > buf_left) {
if (strm->avail_out > 0) {
window_output_flush(strm);
LOAD();
buf_left = end - put;
}
copy = MIN(copy, (uint32_t)buf_left);
}
if (copy == 0)
goto inf_leave;
if (state->offset > state->wnext + state->whave) {
if (state->sane) {
SET_BAD("invalid distance too far back");
break;
}
}
unsigned char *next_out = state->window + state->wsize + state->wnext;
if (copy <= state->offset) {
functable.chunkcopy_safe(next_out, next_out - state->offset, copy, put + buf_left);
} else {
functable.chunkmemset_safe(next_out, state->offset, copy, (uint32_t)buf_left);
}
state->wnext += copy;
state->length -= copy;
LOAD();
if (state->length == 0)
state->mode = LEN;
break;
}
case LIT:
if (put >= state->window + (state->wsize * 2)) {
RESTORE();
window_output_flush(strm);
LOAD();
}
if (left == 0)
goto inf_leave;
*put++ = (unsigned char)(state->length);
state->mode = LEN;
break;
case CHECK:
if (INFLATE_NEED_WINDOW_OUTPUT_FLUSH(strm)) {
RESTORE();
window_output_flush(strm);
LOAD();
if (strm->avail_out == 0 && state->wnext)
goto inf_leave;
}
if (state->wrap) {
NEEDBITS(32);
out -= left;
strm->total_out += out;
state->total += out;
if (INFLATE_NEED_CHECKSUM(strm) && strm->total_out) {
if ((state->wrap & 4) && state->flags)
strm->adler = state->check = functable.crc32_fold_final(&state->crc_fold);
}
out = left;
if ((state->wrap & 4) && (
#ifdef GUNZIP
state->flags ? hold :
#endif
ZSWAP32(hold)) != state->check) {
SET_BAD("incorrect data check");
break;
}
INITBITS();
Tracev((stderr, "inflate: check matches trailer\n"));
}
#ifdef GUNZIP
state->mode = LENGTH;
case LENGTH:
if (state->wrap && state->flags) {
NEEDBITS(32);
if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
SET_BAD("incorrect length check");
break;
}
INITBITS();
Tracev((stderr, "inflate: length matches trailer\n"));
}
#endif
state->mode = DONE;
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;
}
inf_leave:
RESTORE();
if (INFLATE_NEED_WINDOW_OUTPUT_FLUSH(strm) && strm->avail_out && state->wnext)
window_output_flush(strm);
in -= strm->avail_in;
out -= strm->avail_out;
strm->total_in += in;
strm->total_out += out;
state->total += out;
strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
(state->mode == TYPE ? 128 : 0) + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
ret = Z_BUF_ERROR;
return ret;
}
int32_t Z_EXPORT PREFIX(inflateEnd)(PREFIX3(stream) *strm) {
struct inflate_state *state;
if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if (state->window != NULL)
ZFREE_WINDOW(strm, state->window);
ZFREE_STATE(strm, strm->state);
strm->state = NULL;
Tracev((stderr, "inflate: end\n"));
return Z_OK;
}
int32_t Z_EXPORT PREFIX(inflateGetDictionary)(PREFIX3(stream) *strm, uint8_t *dictionary, uint32_t *dictLength) {
struct inflate_state *state;
if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
INFLATE_GET_DICTIONARY_HOOK(strm, dictionary, dictLength);
state = (struct inflate_state *)strm->state;
if (state->whave && dictionary != NULL)
memcpy(dictionary, state->window + state->wsize - state->whave, state->whave);
if (dictLength != NULL)
*dictLength = state->whave;
return Z_OK;
}
int32_t Z_EXPORT PREFIX(inflateSetDictionary)(PREFIX3(stream) *strm, const uint8_t *dictionary, uint32_t dictLength) {
struct inflate_state *state;
unsigned long dictid, dict_copy, hist_copy;
const unsigned char *dict_from;
unsigned char *dict_to, *hist_to;
int ret;
if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if (state->wrap != 0 && state->mode != DICT)
return Z_STREAM_ERROR;
if (state->mode == DICT) {
dictid = functable.adler32(ADLER32_INITIAL_VALUE, dictionary, dictLength);
if (dictid != state->check)
return Z_DATA_ERROR;
}
ret = inflate_ensure_window(state);
if (ret != Z_OK)
return Z_MEM_ERROR;
Tracec(state->wnext != 0, (stderr, "Setting dictionary with unflushed output"));
INFLATE_SET_DICTIONARY_HOOK(strm, dictionary, dictLength);
dict_from = dictionary;
dict_copy = dictLength;
if (dict_copy > state->wsize) {
dict_copy = state->wsize;
dict_from += (dictLength - dict_copy);
}
dict_to = state->window + state->wsize - dict_copy;
hist_copy = state->wsize - dict_copy;
if (hist_copy > state->whave)
hist_copy = state->whave;
hist_to = dict_to - hist_copy;
if (hist_copy)
memcpy(hist_to, state->window + state->wsize - hist_copy, hist_copy);
if (dict_copy)
memcpy(dict_to, dict_from, dict_copy);
state->whave = hist_copy + dict_copy;
state->havedict = 1;
Tracev((stderr, "inflate: dictionary set\n"));
return Z_OK;
}
int32_t Z_EXPORT PREFIX(inflateGetHeader)(PREFIX3(stream) *strm, PREFIX(gz_headerp) head) {
struct inflate_state *state;
if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if ((state->wrap & 2) == 0)
return Z_STREAM_ERROR;
state->head = head;
head->done = 0;
return Z_OK;
}
static uint32_t syncsearch(uint32_t *have, const uint8_t *buf, uint32_t len) {
uint32_t got, next;
got = *have;
next = 0;
while (next < len && got < 4) {
if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
got++;
else if (buf[next])
got = 0;
else
got = 4 - got;
next++;
}
*have = got;
return next;
}
int32_t Z_EXPORT PREFIX(inflateSync)(PREFIX3(stream) *strm) {
unsigned len;
int flags;
size_t in, out;
unsigned char buf[4];
struct inflate_state *state;
if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if (strm->avail_in == 0 && state->bits < 8)
return Z_BUF_ERROR;
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->hold >>= 8;
state->bits -= 8;
}
state->have = 0;
syncsearch(&(state->have), buf, len);
}
len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
strm->avail_in -= len;
strm->next_in += len;
strm->total_in += len;
if (state->have != 4)
return Z_DATA_ERROR;
if (state->flags == -1)
state->wrap = 0;
else
state->wrap &= ~4;
flags = state->flags;
in = strm->total_in;
out = strm->total_out;
PREFIX(inflateReset)(strm);
strm->total_in = (z_size_t)in;
strm->total_out = (z_size_t)out;
state->flags = flags;
state->mode = TYPE;
return Z_OK;
}
int32_t Z_EXPORT PREFIX(inflateSyncPoint)(PREFIX3(stream) *strm) {
struct inflate_state *state;
if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
INFLATE_SYNC_POINT_HOOK(strm);
state = (struct inflate_state *)strm->state;
return state->mode == STORED && state->bits == 0;
}
int32_t Z_EXPORT PREFIX(inflateCopy)(PREFIX3(stream) *dest, PREFIX3(stream) *source) {
struct inflate_state *state;
struct inflate_state *copy;
unsigned char *window;
unsigned wsize;
if (inflateStateCheck(source) || dest == NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state *)source->state;
copy = (struct inflate_state *)ZALLOC_STATE(source, 1, sizeof(struct inflate_state));
if (copy == NULL)
return Z_MEM_ERROR;
window = NULL;
if (state->window != NULL) {
wsize = 1U << state->wbits;
window = (unsigned char *)ZALLOC_WINDOW(state->strm, (wsize * 2) + state->chunksize, sizeof(unsigned char));
if (window == NULL) {
ZFREE_STATE(source, copy);
return Z_MEM_ERROR;
}
}
memcpy((void *)dest, (void *)source, sizeof(PREFIX3(stream)));
ZCOPY_STATE((void *)copy, (void *)state, sizeof(struct inflate_state));
copy->strm = dest;
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 != NULL) {
wsize = 1U << state->wbits;
memcpy(window, state->window, (wsize * 2) + state->chunksize);
}
copy->window = window;
dest->state = (struct internal_state *)copy;
return Z_OK;
}
int32_t Z_EXPORT PREFIX(inflateUndermine)(PREFIX3(stream) *strm, int32_t subvert) {
struct inflate_state *state;
if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
state->sane = !subvert;
return Z_OK;
#else
Z_UNUSED(subvert);
state->sane = 1;
return Z_DATA_ERROR;
#endif
}
int32_t Z_EXPORT PREFIX(inflateValidate)(PREFIX3(stream) *strm, int32_t check) {
struct inflate_state *state;
if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if (check && state->wrap)
state->wrap |= 4;
else
state->wrap &= ~4;
return Z_OK;
}
long Z_EXPORT PREFIX(inflateMark)(PREFIX3(stream) *strm) {
struct inflate_state *state;
if (inflateStateCheck(strm))
return -65536;
INFLATE_MARK_HOOK(strm);
state = (struct inflate_state *)strm->state;
return (long)(((unsigned long)((long)state->back)) << 16) +
(state->mode == COPY ? state->length :
(state->mode == MATCH ? state->was - state->length : 0));
}
unsigned long Z_EXPORT PREFIX(inflateCodesUsed)(PREFIX3(stream) *strm) {
struct inflate_state *state;
if (strm == NULL || strm->state == NULL)
return (unsigned long)-1;
state = (struct inflate_state *)strm->state;
return (unsigned long)(state->next - state->codes);
}