Use named defines instead of hard coded numbers.

This commit is contained in:
Nathan Moinvaziri 2023-02-11 17:24:54 -08:00 committed by Hans Kristian Rosbach
parent 9db6a98894
commit fa9bfeddcf
13 changed files with 38 additions and 34 deletions

View File

@ -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 */

View File

@ -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 */

View 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;

View File

@ -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) {

View 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;

View File

@ -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;

View 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 */

View 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;

View 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;

View 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;

View 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

View 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

View File

@ -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