Skip to content

Commit

Permalink
Simplify inflate window management now that there is no need to
Browse files Browse the repository at this point in the history
worry about failed allocs other than during init.
  • Loading branch information
Dead2 committed May 25, 2024
1 parent b7cb22e commit 4019a63
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 40 deletions.
14 changes: 6 additions & 8 deletions arch/s390/dfltcc_inflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ dfltcc_inflate_action Z_INTERNAL PREFIX(dfltcc_inflate)(PREFIX3(streamp) strm, i
if (strm->avail_in == 0 && !param->cf)
return DFLTCC_INFLATE_BREAK;

if (PREFIX(inflate_ensure_window)(state)) {
state->mode = MEM;
return DFLTCC_INFLATE_CONTINUE;
}
/* if window not in use yet, initialize */
if (state->wsize == 0)
state->wsize = 1U << state->wbits;

/* Translate stream to parameter block */
param->cvt = ((state->wrap & 4) && state->flags) ? CVT_CRC32 : CVT_ADLER32;
Expand Down Expand Up @@ -170,10 +169,9 @@ int Z_INTERNAL PREFIX(dfltcc_inflate_set_dictionary)(PREFIX3(streamp) strm,
struct inflate_state *state = (struct inflate_state *)strm->state;
struct dfltcc_param_v0 *param = &state->arch.common.param;

if (PREFIX(inflate_ensure_window)(state)) {
state->mode = MEM;
return Z_MEM_ERROR;
}
/* if window not in use yet, initialize */
if (state->wsize == 0)
state->wsize = 1U << state->wbits;

append_history(param, state->window, dictionary, dict_length);
state->havedict = 1;
Expand Down
37 changes: 8 additions & 29 deletions inflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

/* function prototypes */
static int inflateStateCheck(PREFIX3(stream) *strm);
static int updatewindow(PREFIX3(stream) *strm, const uint8_t *end, uint32_t len, int32_t cksum);
static void updatewindow(PREFIX3(stream) *strm, const uint8_t *end, uint32_t len, int32_t cksum);
static uint32_t syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len);

static inline void inf_chksum_cpy(PREFIX3(stream) *strm, uint8_t *dst,
Expand Down Expand Up @@ -303,17 +303,6 @@ void Z_INTERNAL PREFIX(fixedtables)(struct inflate_state *state) {
state->distbits = 5;
}

int Z_INTERNAL PREFIX(inflate_ensure_window)(struct inflate_state *state) {
/* if window not in use yet, initialize */
if (state->wsize == 0) {
state->wsize = 1U << state->wbits;
state->wnext = 0;
state->whave = 0;
}

return Z_OK;
}

/*
Update the window with the last wsize (normally 32K) bytes written before
returning. If window does not exist yet, create it. This is only called
Expand All @@ -328,13 +317,15 @@ int Z_INTERNAL PREFIX(inflate_ensure_window)(struct inflate_state *state) {
output will fall in the output data, making match copies simpler and faster.
The advantage may be dependent on the size of the processor's data caches.
*/
static int32_t updatewindow(PREFIX3(stream) *strm, const uint8_t *end, uint32_t len, int32_t cksum) {
static void updatewindow(PREFIX3(stream) *strm, const uint8_t *end, uint32_t len, int32_t cksum) {
struct inflate_state *state;
uint32_t dist;

state = (struct inflate_state *)strm->state;

if (PREFIX(inflate_ensure_window)(state)) return 1;
/* if window not in use yet, initialize */
if (state->wsize == 0)
state->wsize = 1U << state->wbits;

/* len state->wsize or less output bytes into the circular window */
if (len >= state->wsize) {
Expand Down Expand Up @@ -379,7 +370,6 @@ static int32_t updatewindow(PREFIX3(stream) *strm, const uint8_t *end, uint32_t
state->whave += dist;
}
}
return 0;
}

/*
Expand Down Expand Up @@ -1163,9 +1153,6 @@ int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
ret = Z_DATA_ERROR;
goto inf_leave;

case MEM:
return Z_MEM_ERROR;

case SYNC:

default: /* can't happen, but makes compilers happy */
Expand All @@ -1176,7 +1163,6 @@ int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
Return from inflate(), updating the total counts and the check value.
If there was no progress during the inflate() call, return a buffer
error. Call updatewindow() to create and/or update the window state.
Note: a memory error from inflate() is non-recoverable.
*/
inf_leave:
RESTORE();
Expand All @@ -1185,10 +1171,7 @@ int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
(state->wsize || (out != strm->avail_out && state->mode < BAD &&
(state->mode < CHECK || flush != Z_FINISH)))) {
/* update sliding window with respective checksum if not in "raw" mode */
if (updatewindow(strm, strm->next_out, check_bytes, state->wrap & 4)) {
state->mode = MEM;
return Z_MEM_ERROR;
}
updatewindow(strm, strm->next_out, check_bytes, state->wrap & 4);
}
in -= strm->avail_in;
out -= strm->avail_out;
Expand Down Expand Up @@ -1242,7 +1225,6 @@ int32_t Z_EXPORT PREFIX(inflateGetDictionary)(PREFIX3(stream) *strm, uint8_t *di
int32_t Z_EXPORT PREFIX(inflateSetDictionary)(PREFIX3(stream) *strm, const uint8_t *dictionary, uint32_t dictLength) {
struct inflate_state *state;
unsigned long dictid;
int32_t ret;

/* check state */
if (inflateStateCheck(strm))
Expand All @@ -1262,11 +1244,8 @@ int32_t Z_EXPORT PREFIX(inflateSetDictionary)(PREFIX3(stream) *strm, const uint8

/* copy dictionary to window using updatewindow(), which will amend the
existing dictionary if appropriate */
ret = updatewindow(strm, dictionary + dictLength, dictLength, 0);
if (ret) {
state->mode = MEM;
return Z_MEM_ERROR;
}
updatewindow(strm, dictionary + dictLength, dictLength, 0);

state->havedict = 1;
Tracev((stderr, "inflate: dictionary set\n"));
return Z_OK;
Expand Down
4 changes: 1 addition & 3 deletions inflate.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ typedef enum {
LENGTH, /* i: waiting for 32-bit length (gzip) */
DONE, /* finished check, done -- remain here until reset */
BAD, /* got a data error -- remain here until reset */
MEM, /* got an inflate() memory error -- remain here until reset */
SYNC /* looking for synchronization bytes to restart inflate() */
} inflate_mode;

/*
State transitions between above modes -
(most modes can go to BAD or MEM on error -- not shown for clarity)
(most modes can go to BAD on error -- not shown for clarity)
Process header:
HEAD -> (gzip) or (zlib) or (raw)
Expand Down Expand Up @@ -151,7 +150,6 @@ struct ALIGNED_(64) inflate_state {
#endif
};

int Z_INTERNAL PREFIX(inflate_ensure_window)(struct inflate_state *state);
void Z_INTERNAL PREFIX(fixedtables)(struct inflate_state *state);
Z_INTERNAL inflate_allocs* alloc_inflate(PREFIX3(stream) *strm);
Z_INTERNAL void free_inflate(PREFIX3(stream) *strm);
Expand Down

0 comments on commit 4019a63

Please sign in to comment.