typedef block_state (*compress_func) (deflate_state *s, int flush);
/* Compression function. Returns the block state after the call. */
+static int deflateStateCheck (z_stream *strm);
static block_state deflate_stored (deflate_state *s, int flush);
block_state deflate_fast (deflate_state *s, int flush);
block_state deflate_quick (deflate_state *s, int flush);
return Z_MEM_ERROR;
strm->state = (struct internal_state *)s;
s->strm = strm;
+ s->status = INIT_STATE; /* to pass state test in deflateReset() */
s->wrap = wrap;
s->gzhead = NULL;
return deflateReset(strm);
}
+/* =========================================================================
+ * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
+ */
+static int deflateStateCheck (z_stream *strm) {
+ deflate_state *s;
+ if (strm == NULL ||
+ strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
+ return 1;
+ s = strm->state;
+ if (s == NULL || s->strm != strm || (s->status != INIT_STATE &&
+ s->status != EXTRA_STATE &&
+ s->status != NAME_STATE &&
+ s->status != COMMENT_STATE &&
+ s->status != HCRC_STATE &&
+ s->status != BUSY_STATE &&
+ s->status != FINISH_STATE))
+ return 1;
+ return 0;
+}
+
/* ========================================================================= */
int ZEXPORT deflateSetDictionary(z_stream *strm, const unsigned char *dictionary, unsigned int dictLength) {
deflate_state *s;
uint32_t avail;
const unsigned char *next;
- if (strm == NULL || strm->state == NULL || dictionary == NULL)
+ if (deflateStateCheck(strm) || dictionary == NULL)
return Z_STREAM_ERROR;
s = strm->state;
wrap = s->wrap;
int ZEXPORT deflateResetKeep(z_stream *strm) {
deflate_state *s;
- if (strm == NULL || strm->state == NULL || strm->zalloc == NULL || strm->zfree == NULL) {
+ if (deflateStateCheck(strm)) {
return Z_STREAM_ERROR;
}
/* ========================================================================= */
int ZEXPORT deflateSetHeader(z_stream *strm, gz_headerp head) {
- if (strm == NULL || strm->state == NULL)
- return Z_STREAM_ERROR;
- if (strm->state->wrap != 2)
+ if (deflateStateCheck(strm) || strm->state->wrap != 2)
return Z_STREAM_ERROR;
strm->state->gzhead = head;
return Z_OK;
/* ========================================================================= */
int ZEXPORT deflatePending(z_stream *strm, uint32_t *pending, int *bits) {
- if (strm == NULL || strm->state == NULL)
+ if (deflateStateCheck(strm))
return Z_STREAM_ERROR;
if (pending != NULL)
*pending = strm->state->pending;
deflate_state *s;
int put;
- if (strm == NULL || strm->state == NULL)
+ if (deflateStateCheck(strm))
return Z_STREAM_ERROR;
s = strm->state;
if ((unsigned char *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
compress_func func;
int err = Z_OK;
- if (strm == NULL || strm->state == NULL)
+ if (deflateStateCheck(strm))
return Z_STREAM_ERROR;
s = strm->state;
int ZEXPORT deflateTune(z_stream *strm, int good_length, int max_lazy, int nice_length, int max_chain) {
deflate_state *s;
- if (strm == NULL || strm->state == NULL)
+ if (deflateStateCheck(strm))
return Z_STREAM_ERROR;
s = strm->state;
s->good_match = (unsigned int)good_length;
complen = sourceLen + ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
/* if can't get parameters, return conservative bound plus zlib wrapper */
- if (strm == NULL || strm->state == NULL)
+ if (deflateStateCheck(strm))
return complen + 6;
/* compute wrapper length */
int old_flush; /* value of flush param for previous deflate call */
deflate_state *s;
- if (strm == NULL || strm->state == NULL || flush > Z_BLOCK || flush < 0) {
+ if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
return Z_STREAM_ERROR;
}
s = strm->state;
int ZEXPORT deflateEnd(z_stream *strm) {
int status;
- if (strm == NULL || strm->state == NULL)
+ if (deflateStateCheck(strm))
return Z_STREAM_ERROR;
status = strm->state->status;
- if (status != INIT_STATE &&
- status != EXTRA_STATE &&
- status != NAME_STATE &&
- status != COMMENT_STATE &&
- status != HCRC_STATE &&
- status != BUSY_STATE &&
- status != FINISH_STATE) {
- return Z_STREAM_ERROR;
- }
/* Deallocate in reverse order of allocations: */
TRY_FREE(strm, strm->state->pending_buf);
deflate_state *ss;
uint16_t *overlay;
- if (source == NULL || dest == NULL || source->state == NULL) {
+ if (deflateStateCheck(source) || dest == NULL) {
return Z_STREAM_ERROR;
}
#endif
/* function prototypes */
+static int inflateStateCheck(z_stream *strm);
static void fixedtables(struct inflate_state *state);
static int updatewindow(z_stream *strm, const unsigned char *end, uint32_t copy);
#ifdef BUILDFIXED
#endif
static uint32_t syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len);
+static int inflateStateCheck(z_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;
+}
+
int ZEXPORT inflateResetKeep(z_stream *strm) {
struct inflate_state *state;
- if (strm == NULL || strm->state == NULL)
+ if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
strm->total_in = strm->total_out = state->total = 0;
int ZEXPORT inflateReset(z_stream *strm) {
struct inflate_state *state;
- if (strm == NULL || strm->state == NULL)
+ if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
state->wsize = 0;
struct inflate_state *state;
/* get the state */
- if (strm == NULL || strm->state == NULL)
+ if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
return Z_MEM_ERROR;
Tracev((stderr, "inflate: allocated\n"));
strm->state = (struct internal_state *)state;
+ state->strm = strm;
state->window = NULL;
+ state->mode = HEAD; /* to pass state test in inflateReset2() */
ret = inflateReset2(strm, windowBits);
if (ret != Z_OK) {
ZFREE(strm, state);
int ZEXPORT inflatePrime(z_stream *strm, int bits, int value) {
struct inflate_state *state;
- if (strm == NULL || strm->state == NULL)
+ if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if (bits < 0) {
static const uint16_t order[19] = /* permutation of code lengths */
{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 || strm->next_out == NULL ||
+ if (inflateStateCheck(strm) || strm->next_out == NULL ||
(strm->next_in == NULL && strm->avail_in != 0))
return Z_STREAM_ERROR;
int ZEXPORT inflateEnd(z_stream *strm) {
struct inflate_state *state;
- if (strm == NULL || strm->state == NULL || strm->zfree == NULL)
+ if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if (state->window != NULL)
struct inflate_state *state;
/* check state */
- if (strm == NULL || strm->state == NULL)
+ if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
int ret;
/* check state */
- if (strm == NULL || strm->state == NULL)
+ if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if (state->wrap != 0 && state->mode != DICT)
struct inflate_state *state;
/* check state */
- if (strm == NULL || strm->state == NULL)
+ if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if ((state->wrap & 2) == 0)
struct inflate_state *state;
/* check parameters */
- if (strm == NULL || strm->state == NULL)
+ if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if (strm->avail_in == 0 && state->bits < 8)
int ZEXPORT inflateSyncPoint(z_stream *strm) {
struct inflate_state *state;
- if (strm == NULL || strm->state == NULL)
+ if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
return state->mode == STORED && state->bits == 0;
unsigned wsize;
/* check input */
- if (dest == NULL || source == NULL || source->state == NULL ||
- source->zalloc == NULL || source->zfree == NULL)
+ if (inflateStateCheck(source) || dest == NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state *)source->state;
/* copy state */
memcpy((void *)dest, (void *)source, sizeof(z_stream));
memcpy((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);
int ZEXPORT inflateUndermine(z_stream *strm, int subvert) {
struct inflate_state *state;
- if (strm == NULL || strm->state == NULL)
+ if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
int ZEXPORT inflateValidate(z_stream *strm, int check) {
struct inflate_state *state;
- if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
+ if (inflateStateCheck(strm))
+ return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if (check)
state->wrap |= 4;
long ZEXPORT inflateMark(z_stream *strm) {
struct inflate_state *state;
- if (strm == NULL || strm->state == NULL)
+ if (inflateStateCheck(strm))
return -65536;
state = (struct inflate_state *)strm->state;
return ((long)(state->back) << 16) + (state->mode == COPY ? state->length :