#include "zbuild.h"
#include "zutil.h"
#include "inftrees.h"
#include "inflate.h"
#include "inflate_p.h"
#include "functable.h"
#ifdef ZLIB_COMPAT
# undef inflateBackInit
#endif
int32_t ZNG_CONDEXPORT PREFIX(inflateBackInit)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window) {
struct inflate_state *state;
if (strm == NULL || window == NULL || windowBits < MIN_WBITS || windowBits > MAX_WBITS)
return Z_STREAM_ERROR;
strm->msg = NULL;
if (strm->zalloc == NULL) {
strm->zalloc = PREFIX(zcalloc);
strm->opaque = NULL;
}
if (strm->zfree == NULL)
strm->zfree = PREFIX(zcfree);
inflate_allocs *alloc_bufs = alloc_inflate(strm);
if (alloc_bufs == NULL)
return Z_MEM_ERROR;
state = alloc_bufs->state;
state->alloc_bufs = alloc_bufs;
Tracev((stderr, "inflate: allocated\n"));
strm->state = (struct internal_state *)state;
state->wbits = (unsigned int)windowBits;
state->wsize = 1U << windowBits;
state->wbufsize = 1U << windowBits;
state->window = window;
state->wnext = 0;
state->whave = 0;
#ifdef INFLATE_STRICT
state->dmax = 32768U;
#endif
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
state->sane = 1;
#endif
return Z_OK;
}
int32_t Z_EXPORT PREFIX(inflateBackInit_)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window,
const char *version, int32_t stream_size) {
if (CHECK_VER_STSIZE(version, stream_size))
return Z_VERSION_ERROR;
return PREFIX(inflateBackInit)(strm, windowBits, window);
}
#define PULL() \
do { \
if (have == 0) { \
have = in(in_desc, &next); \
if (have == 0) { \
next = NULL; \
ret = Z_BUF_ERROR; \
goto inf_leave; \
} \
} \
} while (0)
#define PULLBYTE() \
do { \
PULL(); \
have--; \
hold += ((uint64_t)(*next++) << bits); \
bits += 8; \
} while (0)
#define ROOM() \
do { \
if (left == 0) { \
put = state->window; \
left = state->wsize; \
state->whave = left; \
if (out(out_desc, put, left)) { \
ret = Z_BUF_ERROR; \
goto inf_leave; \
} \
} \
} while (0)
int32_t Z_EXPORT PREFIX(inflateBack)(PREFIX3(stream) *strm, in_func in, void *in_desc, out_func out, void *out_desc) {
struct inflate_state *state;
z_const unsigned char *next;
unsigned char *put;
unsigned have, left;
uint64_t hold;
unsigned bits;
unsigned copy;
unsigned char *from;
code here;
code last;
unsigned len;
int32_t ret;
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 (strm == NULL || strm->state == NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
strm->msg = NULL;
state->mode = TYPE;
state->last = 0;
state->whave = 0;
next = strm->next_in;
have = next != NULL ? strm->avail_in : 0;
hold = 0;
bits = 0;
put = state->window;
left = state->wsize;
for (;;)
switch (state->mode) {
case TYPE:
if (state->last) {
BYTEBITS();
state->mode = DONE;
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:
PREFIX(fixedtables)(state);
Tracev((stderr, "inflate: fixed codes block%s\n", state->last ? " (last)" : ""));
state->mode = LEN;
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();
while (state->length != 0) {
copy = state->length;
PULL();
ROOM();
copy = MIN(copy, have);
copy = MIN(copy, left);
memcpy(put, next, copy);
have -= copy;
next += copy;
left -= copy;
put += copy;
state->length -= copy;
}
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;
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;
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 = 10;
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 = 9;
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;
Z_FALLTHROUGH;
case LEN:
if (have >= INFLATE_FAST_MIN_HAVE &&
left >= INFLATE_FAST_MIN_LEFT) {
RESTORE();
if (state->whave < state->wsize)
state->whave = state->wsize - left;
FUNCTABLE_CALL(inflate_fast)(strm, state->wsize);
LOAD();
break;
}
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);
}
DROPBITS(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));
ROOM();
*put++ = (unsigned char)(state->length);
left--;
state->mode = LEN;
break;
}
if (here.op & 32) {
Tracevv((stderr, "inflate: end of block\n"));
state->mode = TYPE;
break;
}
if (here.op & 64) {
SET_BAD("invalid literal/length code");
break;
}
state->extra = (here.op & MAX_BITS);
if (state->extra) {
NEEDBITS(state->extra);
state->length += BITS(state->extra);
DROPBITS(state->extra);
}
Tracevv((stderr, "inflate: length %u\n", state->length));
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);
}
DROPBITS(here.bits);
if (here.op & 64) {
SET_BAD("invalid distance code");
break;
}
state->offset = here.val;
state->extra = (here.op & MAX_BITS);
if (state->extra) {
NEEDBITS(state->extra);
state->offset += BITS(state->extra);
DROPBITS(state->extra);
}
#ifdef INFLATE_STRICT
if (state->offset > state->wsize - (state->whave < state->wsize ? left : 0)) {
SET_BAD("invalid distance too far back");
break;
}
#endif
Tracevv((stderr, "inflate: distance %u\n", state->offset));
do {
ROOM();
copy = state->wsize - state->offset;
if (copy < left) {
from = put + copy;
copy = left - copy;
} else {
from = put - state->offset;
copy = left;
}
copy = MIN(copy, state->length);
state->length -= copy;
left -= copy;
do {
*put++ = *from++;
} while (--copy);
} while (state->length != 0);
break;
case DONE:
ret = Z_STREAM_END;
goto inf_leave;
case BAD:
ret = Z_DATA_ERROR;
goto inf_leave;
default:
ret = Z_STREAM_ERROR;
goto inf_leave;
}
inf_leave:
if (left < state->wsize) {
if (out(out_desc, state->window, state->wsize - left) && (ret == Z_STREAM_END)) {
ret = Z_BUF_ERROR;
}
}
strm->next_in = next;
strm->avail_in = have;
return ret;
}
int32_t Z_EXPORT PREFIX(inflateBackEnd)(PREFIX3(stream) *strm) {
if (strm == NULL || strm->state == NULL || strm->zfree == NULL)
return Z_STREAM_ERROR;
free_inflate(strm);
Tracev((stderr, "inflate: end\n"));
return Z_OK;
}