]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Simplify inflate window management now that there is no need to
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Thu, 23 May 2024 18:10:00 +0000 (20:10 +0200)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Thu, 30 May 2024 11:59:40 +0000 (13:59 +0200)
worry about failed allocs other than during init.

arch/s390/dfltcc_inflate.c
inflate.c
inflate.h

index 1c1e8929dd89b36bbfc4517b18eca81325ccfa72..cc3cb39781c8a5dbf7217e0d8734e190d46697e4 100644 (file)
@@ -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;
@@ -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;
index bf25fffc9f18a36d31a72517f35c6190f0dbda16..956f37db7dfb4d5774af192d73f662fc6e004a0f 100644 (file)
--- a/inflate.c
+++ b/inflate.c
@@ -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,
@@ -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
@@ -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) {
@@ -379,7 +370,6 @@ static int32_t updatewindow(PREFIX3(stream) *strm, const uint8_t *end, uint32_t
                 state->whave += dist;
         }
     }
-    return 0;
 }
 
 /*
@@ -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 */
@@ -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();
@@ -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;
@@ -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))
@@ -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;
index cf2a1011094f8dc847f92670e28c641c536644c3..536da7d1f8fce0317100ddd7aacfc539c6672a07 100644 (file)
--- a/inflate.h
+++ b/inflate.h
@@ -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)
@@ -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);