mirror of
https://github.com/GerbilSoft/zlib-ng.git
synced 2025-06-18 19:45:37 -04:00
Use named defines instead of hard coded numbers.
This commit is contained in:
parent
9db6a98894
commit
fa9bfeddcf
10
deflate.c
10
deflate.c
@ -213,17 +213,17 @@ int32_t ZNG_CONDEXPORT PREFIX(deflateInit2)(PREFIX3(stream) *strm, int32_t level
|
|||||||
|
|
||||||
if (windowBits < 0) { /* suppress zlib wrapper */
|
if (windowBits < 0) { /* suppress zlib wrapper */
|
||||||
wrap = 0;
|
wrap = 0;
|
||||||
if (windowBits < -15)
|
if (windowBits < -MAX_WBITS)
|
||||||
return Z_STREAM_ERROR;
|
return Z_STREAM_ERROR;
|
||||||
windowBits = -windowBits;
|
windowBits = -windowBits;
|
||||||
#ifdef GZIP
|
#ifdef GZIP
|
||||||
} else if (windowBits > 15) {
|
} else if (windowBits > MAX_WBITS) {
|
||||||
wrap = 2; /* write gzip wrapper instead */
|
wrap = 2; /* write gzip wrapper instead */
|
||||||
windowBits -= 16;
|
windowBits -= 16;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || windowBits < 8 ||
|
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || windowBits < MIN_WBITS ||
|
||||||
windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED ||
|
windowBits > MAX_WBITS || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED ||
|
||||||
(windowBits == 8 && wrap != 1)) {
|
(windowBits == 8 && wrap != 1)) {
|
||||||
return Z_STREAM_ERROR;
|
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 not default parameters, return conservative bound */
|
||||||
if (DEFLATE_NEED_CONSERVATIVE_BOUND(strm) || /* hook for IBM Z DFLTCC */
|
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) {
|
if (s->level == 0) {
|
||||||
/* upper bound for stored blocks with length 127 (memLevel == 1) --
|
/* upper bound for stored blocks with length 127 (memLevel == 1) --
|
||||||
~4% overhead plus a small constant */
|
~4% overhead plus a small constant */
|
||||||
|
@ -45,9 +45,6 @@
|
|||||||
#define HEAP_SIZE (2*L_CODES+1)
|
#define HEAP_SIZE (2*L_CODES+1)
|
||||||
/* maximum heap size */
|
/* maximum heap size */
|
||||||
|
|
||||||
#define MAX_BITS 15
|
|
||||||
/* All codes must not exceed MAX_BITS bits */
|
|
||||||
|
|
||||||
#define BIT_BUF_SIZE 64
|
#define BIT_BUF_SIZE 64
|
||||||
/* size of bit buffer in bi_buf */
|
/* size of bit buffer in bi_buf */
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ static int gz_look(gz_state *state) {
|
|||||||
state->strm.opaque = NULL;
|
state->strm.opaque = NULL;
|
||||||
state->strm.avail_in = 0;
|
state->strm.avail_in = 0;
|
||||||
state->strm.next_in = NULL;
|
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->out);
|
||||||
zng_free(state->in);
|
zng_free(state->in);
|
||||||
state->size = 0;
|
state->size = 0;
|
||||||
|
@ -34,7 +34,7 @@
|
|||||||
int32_t ZNG_CONDEXPORT PREFIX(inflateBackInit)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window) {
|
int32_t ZNG_CONDEXPORT PREFIX(inflateBackInit)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window) {
|
||||||
struct inflate_state *state;
|
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;
|
return Z_STREAM_ERROR;
|
||||||
strm->msg = NULL; /* in case we return an error */
|
strm->msg = NULL; /* in case we return an error */
|
||||||
if (strm->zalloc == NULL) {
|
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 */
|
/* length code -- get extra bits, if any */
|
||||||
state->extra = (here.op & 15);
|
state->extra = (here.op & MAX_BITS);
|
||||||
if (state->extra) {
|
if (state->extra) {
|
||||||
NEEDBITS(state->extra);
|
NEEDBITS(state->extra);
|
||||||
state->length += BITS(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;
|
break;
|
||||||
}
|
}
|
||||||
state->offset = here.val;
|
state->offset = here.val;
|
||||||
state->extra = (here.op & 15);
|
state->extra = (here.op & MAX_BITS);
|
||||||
|
|
||||||
/* get distance extra bits, if any */
|
/* get distance extra bits, if any */
|
||||||
if (state->extra) {
|
if (state->extra) {
|
||||||
|
@ -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
|
/* decode literals and length/distances until end-of-block or not enough
|
||||||
input data or output space */
|
input data or output space */
|
||||||
do {
|
do {
|
||||||
if (bits < 15) {
|
if (bits < MAX_BITS) {
|
||||||
hold |= load_64_bits(in, bits);
|
hold |= load_64_bits(in, bits);
|
||||||
in += 6;
|
in += 6;
|
||||||
bits += 48;
|
bits += 48;
|
||||||
@ -173,7 +173,7 @@ void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
|
|||||||
len += BITS(op);
|
len += BITS(op);
|
||||||
DROPBITS(op);
|
DROPBITS(op);
|
||||||
Tracevv((stderr, "inflate: length %u\n", len));
|
Tracevv((stderr, "inflate: length %u\n", len));
|
||||||
if (bits < 15) {
|
if (bits < MAX_BITS) {
|
||||||
hold |= load_64_bits(in, bits);
|
hold |= load_64_bits(in, bits);
|
||||||
in += 6;
|
in += 6;
|
||||||
bits += 48;
|
bits += 48;
|
||||||
@ -184,7 +184,7 @@ void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
|
|||||||
op = here->op;
|
op = here->op;
|
||||||
if (op & 16) { /* distance base */
|
if (op & 16) { /* distance base */
|
||||||
dist = here->val;
|
dist = here->val;
|
||||||
op &= 15; /* number of extra bits */
|
op &= MAX_BITS; /* number of extra bits */
|
||||||
if (bits < op) {
|
if (bits < op) {
|
||||||
hold |= load_64_bits(in, bits);
|
hold |= load_64_bits(in, bits);
|
||||||
in += 6;
|
in += 6;
|
||||||
|
14
inflate.c
14
inflate.c
@ -110,19 +110,19 @@ int32_t Z_EXPORT PREFIX(inflateReset2)(PREFIX3(stream) *strm, int32_t windowBits
|
|||||||
/* extract wrap request from windowBits parameter */
|
/* extract wrap request from windowBits parameter */
|
||||||
if (windowBits < 0) {
|
if (windowBits < 0) {
|
||||||
wrap = 0;
|
wrap = 0;
|
||||||
if (windowBits < -15)
|
if (windowBits < -MAX_WBITS)
|
||||||
return Z_STREAM_ERROR;
|
return Z_STREAM_ERROR;
|
||||||
windowBits = -windowBits;
|
windowBits = -windowBits;
|
||||||
} else {
|
} else {
|
||||||
wrap = (windowBits >> 4) + 5;
|
wrap = (windowBits >> 4) + 5;
|
||||||
#ifdef GUNZIP
|
#ifdef GUNZIP
|
||||||
if (windowBits < 48)
|
if (windowBits < 48)
|
||||||
windowBits &= 15;
|
windowBits &= MAX_WBITS;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set number of window bits, free window if different */
|
/* 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;
|
return Z_STREAM_ERROR;
|
||||||
if (state->window != NULL && state->wbits != (unsigned)windowBits) {
|
if (state->window != NULL && state->wbits != (unsigned)windowBits) {
|
||||||
ZFREE_WINDOW(strm, state->window);
|
ZFREE_WINDOW(strm, state->window);
|
||||||
@ -453,7 +453,7 @@ int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
|
|||||||
#ifdef GUNZIP
|
#ifdef GUNZIP
|
||||||
if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
|
if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
|
||||||
if (state->wbits == 0)
|
if (state->wbits == 0)
|
||||||
state->wbits = 15;
|
state->wbits = MAX_WBITS;
|
||||||
state->check = CRC32_INITIAL_VALUE;
|
state->check = CRC32_INITIAL_VALUE;
|
||||||
CRC2(state->check, hold);
|
CRC2(state->check, hold);
|
||||||
INITBITS();
|
INITBITS();
|
||||||
@ -478,7 +478,7 @@ int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
|
|||||||
len = BITS(4) + 8;
|
len = BITS(4) + 8;
|
||||||
if (state->wbits == 0)
|
if (state->wbits == 0)
|
||||||
state->wbits = len;
|
state->wbits = len;
|
||||||
if (len > 15 || len > state->wbits) {
|
if (len > MAX_WBITS || len > state->wbits) {
|
||||||
SET_BAD("invalid window size");
|
SET_BAD("invalid window size");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -919,7 +919,7 @@ int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* length code */
|
/* length code */
|
||||||
state->extra = (here.op & 15);
|
state->extra = (here.op & MAX_BITS);
|
||||||
state->mode = LENEXT;
|
state->mode = LENEXT;
|
||||||
Z_FALLTHROUGH;
|
Z_FALLTHROUGH;
|
||||||
|
|
||||||
@ -962,7 +962,7 @@ int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
state->offset = here.val;
|
state->offset = here.val;
|
||||||
state->extra = (here.op & 15);
|
state->extra = (here.op & MAX_BITS);
|
||||||
state->mode = DISTEXT;
|
state->mode = DISTEXT;
|
||||||
Z_FALLTHROUGH;
|
Z_FALLTHROUGH;
|
||||||
|
|
||||||
|
14
inftrees.c
14
inftrees.c
@ -7,8 +7,6 @@
|
|||||||
#include "zutil.h"
|
#include "zutil.h"
|
||||||
#include "inftrees.h"
|
#include "inftrees.h"
|
||||||
|
|
||||||
#define MAXBITS 15
|
|
||||||
|
|
||||||
const char PREFIX(inflate_copyright)[] = " inflate 1.2.13 Copyright 1995-2022 Mark Adler ";
|
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
|
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 *base; /* base value table to use */
|
||||||
const uint16_t *extra; /* extra bits table to use */
|
const uint16_t *extra; /* extra bits table to use */
|
||||||
unsigned match; /* use base and extra for symbol >= match */
|
unsigned match; /* use base and extra for symbol >= match */
|
||||||
uint16_t count[MAXBITS+1]; /* number of codes of each length */
|
uint16_t count[MAX_BITS+1]; /* number of codes of each length */
|
||||||
uint16_t offs[MAXBITS+1]; /* offsets in table for 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 */
|
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,
|
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};
|
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) */
|
/* 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;
|
count[len] = 0;
|
||||||
for (sym = 0; sym < codes; sym++)
|
for (sym = 0; sym < codes; sym++)
|
||||||
count[lens[sym]]++;
|
count[lens[sym]]++;
|
||||||
|
|
||||||
/* bound code lengths, force root to be within code lengths */
|
/* bound code lengths, force root to be within code lengths */
|
||||||
root = *bits;
|
root = *bits;
|
||||||
for (max = MAXBITS; max >= 1; max--)
|
for (max = MAX_BITS; max >= 1; max--)
|
||||||
if (count[max] != 0) break;
|
if (count[max] != 0) break;
|
||||||
root = MIN(root, max);
|
root = MIN(root, max);
|
||||||
if (UNLIKELY(max == 0)) { /* no symbols to code at all */
|
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 */
|
/* check for an over-subscribed or incomplete set of lengths */
|
||||||
left = 1;
|
left = 1;
|
||||||
for (len = 1; len <= MAXBITS; len++) {
|
for (len = 1; len <= MAX_BITS; len++) {
|
||||||
left <<= 1;
|
left <<= 1;
|
||||||
left -= count[len];
|
left -= count[len];
|
||||||
if (left < 0) return -1; /* over-subscribed */
|
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 */
|
/* generate offsets into symbol table for each length for sorting */
|
||||||
offs[1] = 0;
|
offs[1] = 0;
|
||||||
for (len = 1; len < MAXBITS; len++)
|
for (len = 1; len < MAX_BITS; len++)
|
||||||
offs[len + 1] = offs[len] + count[len];
|
offs[len + 1] = offs[len] + count[len];
|
||||||
|
|
||||||
/* sort symbols by length, by symbol order within each length */
|
/* sort symbols by length, by symbol order within each length */
|
||||||
|
@ -64,7 +64,7 @@ TEST(deflate, hash_head_0) {
|
|||||||
EXPECT_EQ(err, Z_OK);
|
EXPECT_EQ(err, Z_OK);
|
||||||
|
|
||||||
memset(&strm, 0, sizeof(strm));
|
memset(&strm, 0, sizeof(strm));
|
||||||
err = PREFIX(inflateInit2)(&strm, -15);
|
err = PREFIX(inflateInit2)(&strm, -MAX_WBITS);
|
||||||
EXPECT_EQ(err, Z_OK);
|
EXPECT_EQ(err, Z_OK);
|
||||||
|
|
||||||
strm.next_in = next_out;
|
strm.next_in = next_out;
|
||||||
|
@ -20,7 +20,7 @@ TEST(deflate_quick, block_open) {
|
|||||||
int err;
|
int err;
|
||||||
|
|
||||||
memset(&strm, 0, sizeof(strm));
|
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);
|
EXPECT_EQ(err, Z_OK);
|
||||||
|
|
||||||
z_const unsigned char next_in[495] =
|
z_const unsigned char next_in[495] =
|
||||||
@ -75,7 +75,7 @@ TEST(deflate_quick, block_open) {
|
|||||||
EXPECT_EQ(err, Z_OK);
|
EXPECT_EQ(err, Z_OK);
|
||||||
|
|
||||||
memset(&strm, 0, sizeof(strm));
|
memset(&strm, 0, sizeof(strm));
|
||||||
err = PREFIX(inflateInit2)(&strm, -15);
|
err = PREFIX(inflateInit2)(&strm, -MAX_WBITS);
|
||||||
EXPECT_EQ(err, Z_OK);
|
EXPECT_EQ(err, Z_OK);
|
||||||
|
|
||||||
strm.next_in = next_out;
|
strm.next_in = next_out;
|
||||||
|
@ -38,7 +38,7 @@ TEST(raw, basic) {
|
|||||||
EXPECT_EQ(err, Z_OK);
|
EXPECT_EQ(err, Z_OK);
|
||||||
|
|
||||||
memset(&stream, 0, sizeof(stream));
|
memset(&stream, 0, sizeof(stream));
|
||||||
err = PREFIX(inflateInit2)(&stream, -15);
|
err = PREFIX(inflateInit2)(&stream, -MAX_WBITS);
|
||||||
EXPECT_EQ(err, Z_OK);
|
EXPECT_EQ(err, Z_OK);
|
||||||
|
|
||||||
stream.adler = 0x87654321;
|
stream.adler = 0x87654321;
|
||||||
|
@ -32,6 +32,9 @@
|
|||||||
* created by gzip. (Files created by minigzip can still be extracted by
|
* created by gzip. (Files created by minigzip can still be extracted by
|
||||||
* gzip.)
|
* gzip.)
|
||||||
*/
|
*/
|
||||||
|
#ifndef MIN_WBITS
|
||||||
|
# define MIN_WBITS 8 /* 256 LZ77 window */
|
||||||
|
#endif
|
||||||
#ifndef MAX_WBITS
|
#ifndef MAX_WBITS
|
||||||
# define MAX_WBITS 15 /* 32K LZ77 window */
|
# define MAX_WBITS 15 /* 32K LZ77 window */
|
||||||
#endif
|
#endif
|
||||||
|
@ -35,6 +35,9 @@
|
|||||||
* created by gzip. (Files created by minigzip can still be extracted by
|
* created by gzip. (Files created by minigzip can still be extracted by
|
||||||
* gzip.)
|
* gzip.)
|
||||||
*/
|
*/
|
||||||
|
#ifndef MIN_WBITS
|
||||||
|
# define MIN_WBITS 8 /* 256 LZ77 window */
|
||||||
|
#endif
|
||||||
#ifndef MAX_WBITS
|
#ifndef MAX_WBITS
|
||||||
# define MAX_WBITS 15 /* 32K LZ77 window */
|
# define MAX_WBITS 15 /* 32K LZ77 window */
|
||||||
#endif
|
#endif
|
||||||
|
3
zutil.h
3
zutil.h
@ -36,6 +36,9 @@ extern z_const char * const PREFIX(z_errmsg)[10]; /* indexed by 2-zlib_error */
|
|||||||
#endif
|
#endif
|
||||||
/* default windowBits for decompression. MAX_WBITS is for compression only */
|
/* 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
|
#if MAX_MEM_LEVEL >= 8
|
||||||
# define DEF_MEM_LEVEL 8
|
# define DEF_MEM_LEVEL 8
|
||||||
#else
|
#else
|
||||||
|
Loading…
Reference in New Issue
Block a user