]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Use named defines instead of hard coded numbers.
authorNathan Moinvaziri <nathan@nathanm.com>
Sun, 12 Feb 2023 01:24:54 +0000 (17:24 -0800)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Sat, 18 Feb 2023 19:30:55 +0000 (20:30 +0100)
13 files changed:
deflate.c
deflate.h
gzread.c.in
infback.c
inffast_tpl.h
inflate.c
inftrees.c
test/test_deflate_hash_head_0.cc
test/test_deflate_quick_block_open.cc
test/test_raw.cc
zconf-ng.h.in
zconf.h.in
zutil.h

index 78b19e26a3284510083b91e9c606b18202602ff9..273967b4263223e26e478cc023e56f50775a51c2 100644 (file)
--- a/deflate.c
+++ b/deflate.c
@@ -213,17 +213,17 @@ int32_t ZNG_CONDEXPORT PREFIX(deflateInit2)(PREFIX3(stream) *strm, int32_t level
 
     if (windowBits < 0) { /* suppress zlib wrapper */
         wrap = 0;
-        if (windowBits < -15)
+        if (windowBits < -MAX_WBITS)
             return Z_STREAM_ERROR;
         windowBits = -windowBits;
 #ifdef GZIP
-    } else if (windowBits > 15) {
+    } else if (windowBits > MAX_WBITS) {
         wrap = 2;       /* write gzip wrapper instead */
         windowBits -= 16;
 #endif
     }
-    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || windowBits < 8 ||
-        windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED ||
+    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || windowBits < MIN_WBITS ||
+        windowBits > MAX_WBITS || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED ||
         (windowBits == 8 && wrap != 1)) {
         return Z_STREAM_ERROR;
     }
@@ -662,7 +662,7 @@ unsigned long Z_EXPORT PREFIX(deflateBound)(PREFIX3(stream) *strm, unsigned long
 
     /* if not default parameters, return conservative bound */
     if (DEFLATE_NEED_CONSERVATIVE_BOUND(strm) ||  /* hook for IBM Z DFLTCC */
-            s->w_bits != 15 || HASH_BITS < 15) {
+            s->w_bits != MAX_WBITS || HASH_BITS < 15) {
         if (s->level == 0) {
             /* upper bound for stored blocks with length 127 (memLevel == 1) --
                ~4% overhead plus a small constant */
index 2d2ee3da11f9bd35fe9fa538d8b11377950ce50d..e4b971f88ab90e2d1180da528089e6205a07b229 100644 (file)
--- a/deflate.h
+++ b/deflate.h
@@ -45,9 +45,6 @@
 #define HEAP_SIZE (2*L_CODES+1)
 /* maximum heap size */
 
-#define MAX_BITS 15
-/* All codes must not exceed MAX_BITS bits */
-
 #define BIT_BUF_SIZE 64
 /* size of bit buffer in bi_buf */
 
index 262517e4fec437ddc839dff09c5e4cffd34ebd72..67a21a3e4daca4e8a743c0430d654ea901b7284b 100644 (file)
@@ -100,7 +100,7 @@ static int gz_look(gz_state *state) {
         state->strm.opaque = NULL;
         state->strm.avail_in = 0;
         state->strm.next_in = NULL;
-        if (PREFIX(inflateInit2)(&(state->strm), 15 + 16) != Z_OK) {    /* gunzip */
+        if (PREFIX(inflateInit2)(&(state->strm), MAX_WBITS + 16) != Z_OK) {    /* gunzip */
             zng_free(state->out);
             zng_free(state->in);
             state->size = 0;
index 179cea737be41ca5bb02e9e828a82617b70d0467..9f5042b4d3dcb8fce8f900c99725c4a9f3f5eddd 100644 (file)
--- a/infback.c
+++ b/infback.c
@@ -34,7 +34,7 @@
 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 < 8 || windowBits > 15)
+    if (strm == NULL || window == NULL || windowBits < MIN_WBITS || windowBits > MAX_WBITS)
         return Z_STREAM_ERROR;
     strm->msg = NULL;                   /* in case we return an error */
     if (strm->zalloc == NULL) {
@@ -408,7 +408,7 @@ int32_t Z_EXPORT PREFIX(inflateBack)(PREFIX3(stream) *strm, in_func in, void *in
             }
 
             /* length code -- get extra bits, if any */
-            state->extra = (here.op & 15);
+            state->extra = (here.op & MAX_BITS);
             if (state->extra) {
                 NEEDBITS(state->extra);
                 state->length += BITS(state->extra);
@@ -439,7 +439,7 @@ int32_t Z_EXPORT PREFIX(inflateBack)(PREFIX3(stream) *strm, in_func in, void *in
                 break;
             }
             state->offset = here.val;
-            state->extra = (here.op & 15);
+            state->extra = (here.op & MAX_BITS);
 
             /* get distance extra bits, if any */
             if (state->extra) {
index 6977a560a225310701ea1984e1d2938d1b0d3d89..e17e59241ec39f30b9d31ee66885d8a0261dea71 100644 (file)
@@ -148,7 +148,7 @@ void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
     /* decode literals and length/distances until end-of-block or not enough
        input data or output space */
     do {
-        if (bits < 15) {
+        if (bits < MAX_BITS) {
             hold |= load_64_bits(in, bits);
             in += 6;
             bits += 48;
@@ -173,7 +173,7 @@ void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
             len += BITS(op);
             DROPBITS(op);
             Tracevv((stderr, "inflate:         length %u\n", len));
-            if (bits < 15) {
+            if (bits < MAX_BITS) {
                 hold |= load_64_bits(in, bits);
                 in += 6;
                 bits += 48;
@@ -184,7 +184,7 @@ void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
             op = here->op;
             if (op & 16) {                      /* distance base */
                 dist = here->val;
-                op &= 15;                       /* number of extra bits */
+                op &= MAX_BITS;                 /* number of extra bits */
                 if (bits < op) {
                     hold |= load_64_bits(in, bits);
                     in += 6;
index 497b12d6672b43a506a12b98a2a926b6997e47a4..26e358efe8cc4d2188a32c7e65918261cf6c58a5 100644 (file)
--- a/inflate.c
+++ b/inflate.c
@@ -110,19 +110,19 @@ int32_t Z_EXPORT PREFIX(inflateReset2)(PREFIX3(stream) *strm, int32_t windowBits
     /* extract wrap request from windowBits parameter */
     if (windowBits < 0) {
         wrap = 0;
-        if (windowBits < -15)
+        if (windowBits < -MAX_WBITS)
             return Z_STREAM_ERROR;
         windowBits = -windowBits;
     } else {
         wrap = (windowBits >> 4) + 5;
 #ifdef GUNZIP
         if (windowBits < 48)
-            windowBits &= 15;
+            windowBits &= MAX_WBITS;
 #endif
     }
 
     /* set number of window bits, free window if different */
-    if (windowBits && (windowBits < 8 || windowBits > 15))
+    if (windowBits && (windowBits < MIN_WBITS || windowBits > MAX_WBITS))
         return Z_STREAM_ERROR;
     if (state->window != NULL && state->wbits != (unsigned)windowBits) {
         ZFREE_WINDOW(strm, state->window);
@@ -453,7 +453,7 @@ int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
 #ifdef GUNZIP
             if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
                 if (state->wbits == 0)
-                    state->wbits = 15;
+                    state->wbits = MAX_WBITS;
                 state->check = CRC32_INITIAL_VALUE;
                 CRC2(state->check, hold);
                 INITBITS();
@@ -478,7 +478,7 @@ int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
             len = BITS(4) + 8;
             if (state->wbits == 0)
                 state->wbits = len;
-            if (len > 15 || len > state->wbits) {
+            if (len > MAX_WBITS || len > state->wbits) {
                 SET_BAD("invalid window size");
                 break;
             }
@@ -919,7 +919,7 @@ int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
             }
 
             /* length code */
-            state->extra = (here.op & 15);
+            state->extra = (here.op & MAX_BITS);
             state->mode = LENEXT;
             Z_FALLTHROUGH;
 
@@ -962,7 +962,7 @@ int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
                 break;
             }
             state->offset = here.val;
-            state->extra = (here.op & 15);
+            state->extra = (here.op & MAX_BITS);
             state->mode = DISTEXT;
             Z_FALLTHROUGH;
 
index 78f6cf174b4ffa5f19fca0f31ce7d71709d9f2ab..f04d65f8653559288a4620e5f02970c5d7e74331 100644 (file)
@@ -7,8 +7,6 @@
 #include "zutil.h"
 #include "inftrees.h"
 
-#define MAXBITS 15
-
 const char PREFIX(inflate_copyright)[] = " inflate 1.2.13 Copyright 1995-2022 Mark Adler ";
 /*
   If you use the zlib library in a product, an acknowledgment is welcome
@@ -49,8 +47,8 @@ int Z_INTERNAL zng_inflate_table(codetype type, uint16_t *lens, unsigned codes,
     const uint16_t *base;       /* base value table to use */
     const uint16_t *extra;      /* extra bits table to use */
     unsigned match;             /* use base and extra for symbol >= match */
-    uint16_t count[MAXBITS+1];  /* number of codes of each length */
-    uint16_t offs[MAXBITS+1];   /* offsets in table for each length */
+    uint16_t count[MAX_BITS+1];  /* number of codes of each length */
+    uint16_t offs[MAX_BITS+1];   /* offsets in table for each length */
     static const uint16_t lbase[31] = { /* Length codes 257..285 base */
         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
@@ -98,14 +96,14 @@ int Z_INTERNAL zng_inflate_table(codetype type, uint16_t *lens, unsigned codes,
      */
 
     /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
-    for (len = 0; len <= MAXBITS; len++)
+    for (len = 0; len <= MAX_BITS; len++)
         count[len] = 0;
     for (sym = 0; sym < codes; sym++)
         count[lens[sym]]++;
 
     /* bound code lengths, force root to be within code lengths */
     root = *bits;
-    for (max = MAXBITS; max >= 1; max--)
+    for (max = MAX_BITS; max >= 1; max--)
         if (count[max] != 0) break;
     root = MIN(root, max);
     if (UNLIKELY(max == 0)) {           /* no symbols to code at all */
@@ -123,7 +121,7 @@ int Z_INTERNAL zng_inflate_table(codetype type, uint16_t *lens, unsigned codes,
 
     /* check for an over-subscribed or incomplete set of lengths */
     left = 1;
-    for (len = 1; len <= MAXBITS; len++) {
+    for (len = 1; len <= MAX_BITS; len++) {
         left <<= 1;
         left -= count[len];
         if (left < 0) return -1;        /* over-subscribed */
@@ -133,7 +131,7 @@ int Z_INTERNAL zng_inflate_table(codetype type, uint16_t *lens, unsigned codes,
 
     /* generate offsets into symbol table for each length for sorting */
     offs[1] = 0;
-    for (len = 1; len < MAXBITS; len++)
+    for (len = 1; len < MAX_BITS; len++)
         offs[len + 1] = offs[len] + count[len];
 
     /* sort symbols by length, by symbol order within each length */
index 4a4e4b64abeb06ab9806cc934d99c5547d1d914b..cbf601038cfe66c08ea227c799c73bd468044f7b 100644 (file)
@@ -64,7 +64,7 @@ TEST(deflate, hash_head_0) {
     EXPECT_EQ(err, Z_OK);
 
     memset(&strm, 0, sizeof(strm));
-    err = PREFIX(inflateInit2)(&strm, -15);
+    err = PREFIX(inflateInit2)(&strm, -MAX_WBITS);
     EXPECT_EQ(err, Z_OK);
 
     strm.next_in = next_out;
index b8703ae97735cb01fcb2c1346774137aeb2ac0f1..84a1ac8bbfba63662f5442a30cd2e11fb459e8e2 100644 (file)
@@ -20,7 +20,7 @@ TEST(deflate_quick, block_open) {
     int err;
 
     memset(&strm, 0, sizeof(strm));
-    err = PREFIX(deflateInit2)(&strm, 1, Z_DEFLATED, -15, 1, Z_FILTERED);
+    err = PREFIX(deflateInit2)(&strm, 1, Z_DEFLATED, -MAX_WBITS, 1, Z_FILTERED);
     EXPECT_EQ(err, Z_OK);
 
     z_const unsigned char next_in[495] =
@@ -75,7 +75,7 @@ TEST(deflate_quick, block_open) {
     EXPECT_EQ(err, Z_OK);
 
     memset(&strm, 0, sizeof(strm));
-    err = PREFIX(inflateInit2)(&strm, -15);
+    err = PREFIX(inflateInit2)(&strm, -MAX_WBITS);
     EXPECT_EQ(err, Z_OK);
 
     strm.next_in = next_out;
index 69959d9e03ea30b1b47b221399d7ef40560e76cd..a013d4bb47e4ad29cf118388ec53cf98d4bb5c63 100644 (file)
@@ -38,7 +38,7 @@ TEST(raw, basic) {
     EXPECT_EQ(err, Z_OK);
 
     memset(&stream, 0, sizeof(stream));
-    err = PREFIX(inflateInit2)(&stream, -15);
+    err = PREFIX(inflateInit2)(&stream, -MAX_WBITS);
     EXPECT_EQ(err, Z_OK);
 
     stream.adler = 0x87654321;
index 7f8983bf461d49e2bb9172693f350874f334cd6c..226f06a037792ca104538899d003d0217af2b688 100644 (file)
@@ -32,6 +32,9 @@
  * created by gzip. (Files created by minigzip can still be extracted by
  * gzip.)
  */
+#ifndef MIN_WBITS
+#  define MIN_WBITS   8  /* 256 LZ77 window */
+#endif
 #ifndef MAX_WBITS
 #  define MAX_WBITS   15 /* 32K LZ77 window */
 #endif
index 0611fac68b26131f0f57f965ea286e92ef841505..765e9c4e47308a6e2cf6fa340bbf9e5e1a1ec946 100644 (file)
@@ -35,6 +35,9 @@
  * created by gzip. (Files created by minigzip can still be extracted by
  * gzip.)
  */
+#ifndef MIN_WBITS
+#  define MIN_WBITS   8  /* 256 LZ77 window */
+#endif
 #ifndef MAX_WBITS
 #  define MAX_WBITS   15 /* 32K LZ77 window */
 #endif
diff --git a/zutil.h b/zutil.h
index 3dd548d38b7b7f2eb491230bd6f459eada4da5aa..7b9a8b09424df8f5f1c75795331821878dc7a7ce 100644 (file)
--- a/zutil.h
+++ b/zutil.h
@@ -36,6 +36,9 @@ extern z_const char * const PREFIX(z_errmsg)[10]; /* indexed by 2-zlib_error */
 #endif
 /* default windowBits for decompression. MAX_WBITS is for compression only */
 
+#define MAX_BITS 15
+/* all codes must not exceed MAX_BITS bits */
+
 #if MAX_MEM_LEVEL >= 8
 #  define DEF_MEM_LEVEL 8
 #else