Commit 2db6cdcd by Edward Thomson

zlib: updated bundled zlib to v1.2.12

parent d1001fd0
...@@ -177,3 +177,10 @@ uLong ZEXPORT adler32_combine(adler1, adler2, len2) ...@@ -177,3 +177,10 @@ uLong ZEXPORT adler32_combine(adler1, adler2, len2)
return adler32_combine_(adler1, adler2, len2); return adler32_combine_(adler1, adler2, len2);
} }
uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
uLong adler1;
uLong adler2;
z_off64_t len2;
{
return adler32_combine_(adler1, adler2, len2);
}
/* crc32.c -- compute the CRC-32 of a data stream /* crc32.c -- compute the CRC-32 of a data stream
* Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 Mark Adler * Copyright (C) 1995-2022 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h * For conditions of distribution and use, see copyright notice in zlib.h
* *
* Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster * This interleaved implementation of a CRC makes use of pipelined multiple
* CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing * arithmetic-logic units, commonly found in modern CPU cores. It is due to
* tables for updating the shift register in one step with three exclusive-ors * Kadatch and Jenkins (2010). See doc/crc-doc.1.0.pdf in this distribution.
* instead of four steps with four exclusive-ors. This results in about a
* factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
*/ */
/* @(#) $Id$ */ /* @(#) $Id$ */
...@@ -18,7 +16,8 @@ ...@@ -18,7 +16,8 @@
first call get_crc_table() to initialize the tables before allowing more than first call get_crc_table() to initialize the tables before allowing more than
one thread to use crc32(). one thread to use crc32().
DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h. MAKECRCH can be #defined to write out crc32.h. A main() routine is also
produced, so that this one source file can be compiled to an executable.
*/ */
#ifdef MAKECRCH #ifdef MAKECRCH
...@@ -28,37 +27,224 @@ ...@@ -28,37 +27,224 @@
# endif /* !DYNAMIC_CRC_TABLE */ # endif /* !DYNAMIC_CRC_TABLE */
#endif /* MAKECRCH */ #endif /* MAKECRCH */
#include "zutil.h" /* for STDC and FAR definitions */ #include "zutil.h" /* for Z_U4, Z_U8, z_crc_t, and FAR definitions */
/* Definitions for doing the crc four data bytes at a time. */ /*
#if !defined(NOBYFOUR) && defined(Z_U4) A CRC of a message is computed on N braids of words in the message, where
# define BYFOUR each word consists of W bytes (4 or 8). If N is 3, for example, then three
running sparse CRCs are calculated respectively on each braid, at these
indices in the array of words: 0, 3, 6, ..., 1, 4, 7, ..., and 2, 5, 8, ...
This is done starting at a word boundary, and continues until as many blocks
of N * W bytes as are available have been processed. The results are combined
into a single CRC at the end. For this code, N must be in the range 1..6 and
W must be 4 or 8. The upper limit on N can be increased if desired by adding
more #if blocks, extending the patterns apparent in the code. In addition,
crc32.h would need to be regenerated, if the maximum N value is increased.
N and W are chosen empirically by benchmarking the execution time on a given
processor. The choices for N and W below were based on testing on Intel Kaby
Lake i7, AMD Ryzen 7, ARM Cortex-A57, Sparc64-VII, PowerPC POWER9, and MIPS64
Octeon II processors. The Intel, AMD, and ARM processors were all fastest
with N=5, W=8. The Sparc, PowerPC, and MIPS64 were all fastest at N=5, W=4.
They were all tested with either gcc or clang, all using the -O3 optimization
level. Your mileage may vary.
*/
/* Define N */
#ifdef Z_TESTN
# define N Z_TESTN
#else
# define N 5
#endif
#if N < 1 || N > 6
# error N must be in 1..6
#endif #endif
#ifdef BYFOUR
local unsigned long crc32_little OF((unsigned long, /*
const unsigned char FAR *, z_size_t)); z_crc_t must be at least 32 bits. z_word_t must be at least as long as
local unsigned long crc32_big OF((unsigned long, z_crc_t. It is assumed here that z_word_t is either 32 bits or 64 bits, and
const unsigned char FAR *, z_size_t)); that bytes are eight bits.
# define TBLS 8 */
/*
Define W and the associated z_word_t type. If W is not defined, then a
braided calculation is not used, and the associated tables and code are not
compiled.
*/
#ifdef Z_TESTW
# if Z_TESTW-1 != -1
# define W Z_TESTW
# endif
#else #else
# define TBLS 1 # ifdef MAKECRCH
#endif /* BYFOUR */ # define W 8 /* required for MAKECRCH */
# else
# if defined(__x86_64__) || defined(__aarch64__)
# define W 8
# else
# define W 4
# endif
# endif
#endif
#ifdef W
# if W == 8 && defined(Z_U8)
typedef Z_U8 z_word_t;
# elif defined(Z_U4)
# undef W
# define W 4
typedef Z_U4 z_word_t;
# else
# undef W
# endif
#endif
/* Local functions for crc concatenation */ /* Local functions. */
local unsigned long gf2_matrix_times OF((unsigned long *mat, local z_crc_t multmodp OF((z_crc_t a, z_crc_t b));
unsigned long vec)); local z_crc_t x2nmodp OF((z_off64_t n, unsigned k));
local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); /* If available, use the ARM processor CRC32 instruction. */
#if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32) && W == 8
# define ARMCRC32
#endif
#if defined(W) && (!defined(ARMCRC32) || defined(DYNAMIC_CRC_TABLE))
/*
Swap the bytes in a z_word_t to convert between little and big endian. Any
self-respecting compiler will optimize this to a single machine byte-swap
instruction, if one is available. This assumes that word_t is either 32 bits
or 64 bits.
*/
local z_word_t byte_swap(word)
z_word_t word;
{
# if W == 8
return
(word & 0xff00000000000000) >> 56 |
(word & 0xff000000000000) >> 40 |
(word & 0xff0000000000) >> 24 |
(word & 0xff00000000) >> 8 |
(word & 0xff000000) << 8 |
(word & 0xff0000) << 24 |
(word & 0xff00) << 40 |
(word & 0xff) << 56;
# else /* W == 4 */
return
(word & 0xff000000) >> 24 |
(word & 0xff0000) >> 8 |
(word & 0xff00) << 8 |
(word & 0xff) << 24;
# endif
}
#endif
/* CRC polynomial. */
#define POLY 0xedb88320 /* p(x) reflected, with x^32 implied */
#ifdef DYNAMIC_CRC_TABLE #ifdef DYNAMIC_CRC_TABLE
local volatile int crc_table_empty = 1; local z_crc_t FAR crc_table[256];
local z_crc_t FAR crc_table[TBLS][256]; local z_crc_t FAR x2n_table[32];
local void make_crc_table OF((void)); local void make_crc_table OF((void));
#ifdef W
local z_word_t FAR crc_big_table[256];
local z_crc_t FAR crc_braid_table[W][256];
local z_word_t FAR crc_braid_big_table[W][256];
local void braid OF((z_crc_t [][256], z_word_t [][256], int, int));
#endif
#ifdef MAKECRCH #ifdef MAKECRCH
local void write_table OF((FILE *, const z_crc_t FAR *)); local void write_table OF((FILE *, const z_crc_t FAR *, int));
local void write_table32hi OF((FILE *, const z_word_t FAR *, int));
local void write_table64 OF((FILE *, const z_word_t FAR *, int));
#endif /* MAKECRCH */ #endif /* MAKECRCH */
/*
Define a once() function depending on the availability of atomics. If this is
compiled with DYNAMIC_CRC_TABLE defined, and if CRCs will be computed in
multiple threads, and if atomics are not available, then get_crc_table() must
be called to initialize the tables and must return before any threads are
allowed to compute or combine CRCs.
*/
/* Definition of once functionality. */
typedef struct once_s once_t;
local void once OF((once_t *, void (*)(void)));
/* Check for the availability of atomics. */
#if defined(__STDC__) && __STDC_VERSION__ >= 201112L && \
!defined(__STDC_NO_ATOMICS__)
#include <stdatomic.h>
/* Structure for once(), which must be initialized with ONCE_INIT. */
struct once_s {
atomic_flag begun;
atomic_int done;
};
#define ONCE_INIT {ATOMIC_FLAG_INIT, 0}
/*
Run the provided init() function exactly once, even if multiple threads
invoke once() at the same time. The state must be a once_t initialized with
ONCE_INIT.
*/
local void once(state, init)
once_t *state;
void (*init)(void);
{
if (!atomic_load(&state->done)) {
if (atomic_flag_test_and_set(&state->begun))
while (!atomic_load(&state->done))
;
else {
init();
atomic_store(&state->done, 1);
}
}
}
#else /* no atomics */
/* Structure for once(), which must be initialized with ONCE_INIT. */
struct once_s {
volatile int begun;
volatile int done;
};
#define ONCE_INIT {0, 0}
/* Test and set. Alas, not atomic, but tries to minimize the period of
vulnerability. */
local int test_and_set OF((int volatile *));
local int test_and_set(flag)
int volatile *flag;
{
int was;
was = *flag;
*flag = 1;
return was;
}
/* Run the provided init() function once. This is not thread-safe. */
local void once(state, init)
once_t *state;
void (*init)(void);
{
if (!state->done) {
if (test_and_set(&state->begun))
while (!state->done)
;
else {
init();
state->done = 1;
}
}
}
#endif
/* State for once(). */
local once_t made = ONCE_INIT;
/* /*
Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
...@@ -68,360 +254,830 @@ local void make_crc_table OF((void)); ...@@ -68,360 +254,830 @@ local void make_crc_table OF((void));
is just exclusive-or, and multiplying a polynomial by x is a right shift by is just exclusive-or, and multiplying a polynomial by x is a right shift by
one. If we call the above polynomial p, and represent a byte as the one. If we call the above polynomial p, and represent a byte as the
polynomial q, also with the lowest power in the most significant bit (so the polynomial q, also with the lowest power in the most significant bit (so the
byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, byte 0xb1 is the polynomial x^7+x^3+x^2+1), then the CRC is (q*x^32) mod p,
where a mod b means the remainder after dividing a by b. where a mod b means the remainder after dividing a by b.
This calculation is done using the shift-register method of multiplying and This calculation is done using the shift-register method of multiplying and
taking the remainder. The register is initialized to zero, and for each taking the remainder. The register is initialized to zero, and for each
incoming bit, x^32 is added mod p to the register if the bit is a one (where incoming bit, x^32 is added mod p to the register if the bit is a one (where
x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by x
x (which is shifting right by one and adding x^32 mod p if the bit shifted (which is shifting right by one and adding x^32 mod p if the bit shifted out
out is a one). We start with the highest power (least significant bit) of is a one). We start with the highest power (least significant bit) of q and
q and repeat for all eight bits of q. repeat for all eight bits of q.
The first table is simply the CRC of all possible eight bit values. This is The table is simply the CRC of all possible eight bit values. This is all the
all the information needed to generate CRCs on data a byte at a time for all information needed to generate CRCs on data a byte at a time for all
combinations of CRC register values and incoming bytes. The remaining tables combinations of CRC register values and incoming bytes.
allow for word-at-a-time CRC calculation for both big-endian and little- */
endian machines, where a word is four bytes.
*/
local void make_crc_table() local void make_crc_table()
{ {
z_crc_t c; unsigned i, j, n;
int n, k; z_crc_t p;
z_crc_t poly; /* polynomial exclusive-or pattern */
/* terms of polynomial defining this crc (except x^32): */ /* initialize the CRC of bytes tables */
static volatile int first = 1; /* flag to limit concurrent making */ for (i = 0; i < 256; i++) {
static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; p = i;
for (j = 0; j < 8; j++)
/* See if another task is already doing this (not thread-safe, but better p = p & 1 ? (p >> 1) ^ POLY : p >> 1;
than nothing -- significantly reduces duration of vulnerability in crc_table[i] = p;
case the advice about DYNAMIC_CRC_TABLE is ignored) */ #ifdef W
if (first) { crc_big_table[i] = byte_swap(p);
first = 0; #endif
/* make exclusive-or pattern from polynomial (0xedb88320UL) */
poly = 0;
for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)
poly |= (z_crc_t)1 << (31 - p[n]);
/* generate a crc for every 8-bit value */
for (n = 0; n < 256; n++) {
c = (z_crc_t)n;
for (k = 0; k < 8; k++)
c = c & 1 ? poly ^ (c >> 1) : c >> 1;
crc_table[0][n] = c;
}
#ifdef BYFOUR
/* generate crc for each value followed by one, two, and three zeros,
and then the byte reversal of those as well as the first table */
for (n = 0; n < 256; n++) {
c = crc_table[0][n];
crc_table[4][n] = ZSWAP32(c);
for (k = 1; k < 4; k++) {
c = crc_table[0][c & 0xff] ^ (c >> 8);
crc_table[k][n] = c;
crc_table[k + 4][n] = ZSWAP32(c);
}
}
#endif /* BYFOUR */
crc_table_empty = 0;
}
else { /* not first */
/* wait for the other guy to finish (not efficient, but rare) */
while (crc_table_empty)
;
} }
/* initialize the x^2^n mod p(x) table */
p = (z_crc_t)1 << 30; /* x^1 */
x2n_table[0] = p;
for (n = 1; n < 32; n++)
x2n_table[n] = p = multmodp(p, p);
#ifdef W
/* initialize the braiding tables -- needs x2n_table[] */
braid(crc_braid_table, crc_braid_big_table, N, W);
#endif
#ifdef MAKECRCH #ifdef MAKECRCH
/* write out CRC tables to crc32.h */
{ {
/*
The crc32.h header file contains tables for both 32-bit and 64-bit
z_word_t's, and so requires a 64-bit type be available. In that case,
z_word_t must be defined to be 64-bits. This code then also generates
and writes out the tables for the case that z_word_t is 32 bits.
*/
#if !defined(W) || W != 8
# error Need a 64-bit integer type in order to generate crc32.h.
#endif
FILE *out; FILE *out;
int k, n;
z_crc_t ltl[8][256];
z_word_t big[8][256];
out = fopen("crc32.h", "w"); out = fopen("crc32.h", "w");
if (out == NULL) return; if (out == NULL) return;
fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); /* write out little-endian CRC table to crc32.h */
fprintf(out, "local const z_crc_t FAR "); fprintf(out,
fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); "/* crc32.h -- tables for rapid CRC calculation\n"
write_table(out, crc_table[0]); " * Generated automatically by crc32.c\n */\n"
# ifdef BYFOUR "\n"
fprintf(out, "#ifdef BYFOUR\n"); "local const z_crc_t FAR crc_table[] = {\n"
for (k = 1; k < 8; k++) { " ");
fprintf(out, " },\n {\n"); write_table(out, crc_table, 256);
write_table(out, crc_table[k]); fprintf(out,
} "};\n");
fprintf(out, "#endif\n");
# endif /* BYFOUR */ /* write out big-endian CRC table for 64-bit z_word_t to crc32.h */
fprintf(out, " }\n};\n"); fprintf(out,
"\n"
"#ifdef W\n"
"\n"
"#if W == 8\n"
"\n"
"local const z_word_t FAR crc_big_table[] = {\n"
" ");
write_table64(out, crc_big_table, 256);
fprintf(out,
"};\n");
/* write out big-endian CRC table for 32-bit z_word_t to crc32.h */
fprintf(out,
"\n"
"#else /* W == 4 */\n"
"\n"
"local const z_word_t FAR crc_big_table[] = {\n"
" ");
write_table32hi(out, crc_big_table, 256);
fprintf(out,
"};\n"
"\n"
"#endif\n");
/* write out braid tables for each value of N */
for (n = 1; n <= 6; n++) {
fprintf(out,
"\n"
"#if N == %d\n", n);
/* compute braid tables for this N and 64-bit word_t */
braid(ltl, big, n, 8);
/* write out braid tables for 64-bit z_word_t to crc32.h */
fprintf(out,
"\n"
"#if W == 8\n"
"\n"
"local const z_crc_t FAR crc_braid_table[][256] = {\n");
for (k = 0; k < 8; k++) {
fprintf(out, " {");
write_table(out, ltl[k], 256);
fprintf(out, "}%s", k < 7 ? ",\n" : "");
}
fprintf(out,
"};\n"
"\n"
"local const z_word_t FAR crc_braid_big_table[][256] = {\n");
for (k = 0; k < 8; k++) {
fprintf(out, " {");
write_table64(out, big[k], 256);
fprintf(out, "}%s", k < 7 ? ",\n" : "");
}
fprintf(out,
"};\n");
/* compute braid tables for this N and 32-bit word_t */
braid(ltl, big, n, 4);
/* write out braid tables for 32-bit z_word_t to crc32.h */
fprintf(out,
"\n"
"#else /* W == 4 */\n"
"\n"
"local const z_crc_t FAR crc_braid_table[][256] = {\n");
for (k = 0; k < 4; k++) {
fprintf(out, " {");
write_table(out, ltl[k], 256);
fprintf(out, "}%s", k < 3 ? ",\n" : "");
}
fprintf(out,
"};\n"
"\n"
"local const z_word_t FAR crc_braid_big_table[][256] = {\n");
for (k = 0; k < 4; k++) {
fprintf(out, " {");
write_table32hi(out, big[k], 256);
fprintf(out, "}%s", k < 3 ? ",\n" : "");
}
fprintf(out,
"};\n"
"\n"
"#endif\n"
"\n"
"#endif\n");
}
fprintf(out,
"\n"
"#endif\n");
/* write out zeros operator table to crc32.h */
fprintf(out,
"\n"
"local const z_crc_t FAR x2n_table[] = {\n"
" ");
write_table(out, x2n_table, 32);
fprintf(out,
"};\n");
fclose(out); fclose(out);
} }
#endif /* MAKECRCH */ #endif /* MAKECRCH */
} }
#ifdef MAKECRCH #ifdef MAKECRCH
local void write_table(out, table)
/*
Write the 32-bit values in table[0..k-1] to out, five per line in
hexadecimal separated by commas.
*/
local void write_table(out, table, k)
FILE *out; FILE *out;
const z_crc_t FAR *table; const z_crc_t FAR *table;
int k;
{ {
int n; int n;
for (n = 0; n < 256; n++) for (n = 0; n < k; n++)
fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ",
(unsigned long)(table[n]), (unsigned long)(table[n]),
n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", "));
}
/*
Write the high 32-bits of each value in table[0..k-1] to out, five per line
in hexadecimal separated by commas.
*/
local void write_table32hi(out, table, k)
FILE *out;
const z_word_t FAR *table;
int k;
{
int n;
for (n = 0; n < k; n++)
fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ",
(unsigned long)(table[n] >> 32),
n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", "));
}
/*
Write the 64-bit values in table[0..k-1] to out, three per line in
hexadecimal separated by commas. This assumes that if there is a 64-bit
type, then there is also a long long integer type, and it is at least 64
bits. If not, then the type cast and format string can be adjusted
accordingly.
*/
local void write_table64(out, table, k)
FILE *out;
const z_word_t FAR *table;
int k;
{
int n;
for (n = 0; n < k; n++)
fprintf(out, "%s0x%016llx%s", n == 0 || n % 3 ? "" : " ",
(unsigned long long)(table[n]),
n == k - 1 ? "" : (n % 3 == 2 ? ",\n" : ", "));
}
/* Actually do the deed. */
int main()
{
make_crc_table();
return 0;
} }
#endif /* MAKECRCH */ #endif /* MAKECRCH */
#ifdef W
/*
Generate the little and big-endian braid tables for the given n and z_word_t
size w. Each array must have room for w blocks of 256 elements.
*/
local void braid(ltl, big, n, w)
z_crc_t ltl[][256];
z_word_t big[][256];
int n;
int w;
{
int k;
z_crc_t i, p, q;
for (k = 0; k < w; k++) {
p = x2nmodp((n * w + 3 - k) << 3, 0);
ltl[k][0] = 0;
big[w - 1 - k][0] = 0;
for (i = 1; i < 256; i++) {
ltl[k][i] = q = multmodp(i << 24, p);
big[w - 1 - k][i] = byte_swap(q);
}
}
}
#endif
#else /* !DYNAMIC_CRC_TABLE */ #else /* !DYNAMIC_CRC_TABLE */
/* ======================================================================== /* ========================================================================
* Tables of CRC-32s of all single-byte values, made by make_crc_table(). * Tables for byte-wise and braided CRC-32 calculations, and a table of powers
* of x for combining CRC-32s, all made by make_crc_table().
*/ */
#include "crc32.h" #include "crc32.h"
#endif /* DYNAMIC_CRC_TABLE */ #endif /* DYNAMIC_CRC_TABLE */
/* ========================================================================
* Routines used for CRC calculation. Some are also required for the table
* generation above.
*/
/*
Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC polynomial,
reflected. For speed, this requires that a not be zero.
*/
local z_crc_t multmodp(a, b)
z_crc_t a;
z_crc_t b;
{
z_crc_t m, p;
m = (z_crc_t)1 << 31;
p = 0;
for (;;) {
if (a & m) {
p ^= b;
if ((a & (m - 1)) == 0)
break;
}
m >>= 1;
b = b & 1 ? (b >> 1) ^ POLY : b >> 1;
}
return p;
}
/*
Return x^(n * 2^k) modulo p(x). Requires that x2n_table[] has been
initialized.
*/
local z_crc_t x2nmodp(n, k)
z_off64_t n;
unsigned k;
{
z_crc_t p;
p = (z_crc_t)1 << 31; /* x^0 == 1 */
while (n) {
if (n & 1)
p = multmodp(x2n_table[k & 31], p);
n >>= 1;
k++;
}
return p;
}
/* ========================================================================= /* =========================================================================
* This function can be used by asm versions of crc32() * This function can be used by asm versions of crc32(), and to force the
* generation of the CRC tables in a threaded application.
*/ */
const z_crc_t FAR * ZEXPORT get_crc_table() const z_crc_t FAR * ZEXPORT get_crc_table()
{ {
#ifdef DYNAMIC_CRC_TABLE #ifdef DYNAMIC_CRC_TABLE
if (crc_table_empty) once(&made, make_crc_table);
make_crc_table();
#endif /* DYNAMIC_CRC_TABLE */ #endif /* DYNAMIC_CRC_TABLE */
return (const z_crc_t FAR *)crc_table; return (const z_crc_t FAR *)crc_table;
} }
/* ========================================================================= */ /* =========================================================================
#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) * Use ARM machine instructions if available. This will compute the CRC about
#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 * ten times faster than the braided calculation. This code does not check for
* the presence of the CRC instruction at run time. __ARM_FEATURE_CRC32 will
* only be defined if the compilation specifies an ARM processor architecture
* that has the instructions. For example, compiling with -march=armv8.1-a or
* -march=armv8-a+crc, or -march=native if the compile machine has the crc32
* instructions.
*/
#ifdef ARMCRC32
/*
Constants empirically determined to maximize speed. These values are from
measurements on a Cortex-A57. Your mileage may vary.
*/
#define Z_BATCH 3990 /* number of words in a batch */
#define Z_BATCH_ZEROS 0xa10d3d0c /* computed from Z_BATCH = 3990 */
#define Z_BATCH_MIN 800 /* fewest words in a final batch */
/* ========================================================================= */
unsigned long ZEXPORT crc32_z(crc, buf, len) unsigned long ZEXPORT crc32_z(crc, buf, len)
unsigned long crc; unsigned long crc;
const unsigned char FAR *buf; const unsigned char FAR *buf;
z_size_t len; z_size_t len;
{ {
if (buf == Z_NULL) return 0UL; z_crc_t val;
z_word_t crc1, crc2;
const z_word_t *word;
z_word_t val0, val1, val2;
z_size_t last, last2, i;
z_size_t num;
/* Return initial CRC, if requested. */
if (buf == Z_NULL) return 0;
#ifdef DYNAMIC_CRC_TABLE #ifdef DYNAMIC_CRC_TABLE
if (crc_table_empty) once(&made, make_crc_table);
make_crc_table();
#endif /* DYNAMIC_CRC_TABLE */ #endif /* DYNAMIC_CRC_TABLE */
#ifdef BYFOUR /* Pre-condition the CRC */
if (sizeof(void *) == sizeof(ptrdiff_t)) { crc ^= 0xffffffff;
z_crc_t endian;
endian = 1; /* Compute the CRC up to a word boundary. */
if (*((unsigned char *)(&endian))) while (len && ((z_size_t)buf & 7) != 0) {
return crc32_little(crc, buf, len); len--;
else val = *buf++;
return crc32_big(crc, buf, len); __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val));
} }
#endif /* BYFOUR */
crc = crc ^ 0xffffffffUL; /* Prepare to compute the CRC on full 64-bit words word[0..num-1]. */
while (len >= 8) { word = (z_word_t const *)buf;
DO8; num = len >> 3;
len -= 8; len &= 7;
/* Do three interleaved CRCs to realize the throughput of one crc32x
instruction per cycle. Each CRC is calcuated on Z_BATCH words. The three
CRCs are combined into a single CRC after each set of batches. */
while (num >= 3 * Z_BATCH) {
crc1 = 0;
crc2 = 0;
for (i = 0; i < Z_BATCH; i++) {
val0 = word[i];
val1 = word[i + Z_BATCH];
val2 = word[i + 2 * Z_BATCH];
__asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
__asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1));
__asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2));
}
word += 3 * Z_BATCH;
num -= 3 * Z_BATCH;
crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc1;
crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc2;
} }
if (len) do {
DO1;
} while (--len);
return crc ^ 0xffffffffUL;
}
/* ========================================================================= */ /* Do one last smaller batch with the remaining words, if there are enough
unsigned long ZEXPORT crc32(crc, buf, len) to pay for the combination of CRCs. */
unsigned long crc; last = num / 3;
const unsigned char FAR *buf; if (last >= Z_BATCH_MIN) {
uInt len; last2 = last << 1;
{ crc1 = 0;
return crc32_z(crc, buf, len); crc2 = 0;
for (i = 0; i < last; i++) {
val0 = word[i];
val1 = word[i + last];
val2 = word[i + last2];
__asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
__asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1));
__asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2));
}
word += 3 * last;
num -= 3 * last;
val = x2nmodp(last, 6);
crc = multmodp(val, crc) ^ crc1;
crc = multmodp(val, crc) ^ crc2;
}
/* Compute the CRC on any remaining words. */
for (i = 0; i < num; i++) {
val0 = word[i];
__asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
}
word += num;
/* Complete the CRC on any remaining bytes. */
buf = (const unsigned char FAR *)word;
while (len) {
len--;
val = *buf++;
__asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val));
}
/* Return the CRC, post-conditioned. */
return crc ^ 0xffffffff;
} }
#ifdef BYFOUR #else
#ifdef W
/* /*
This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit Return the CRC of the W bytes in the word_t data, taking the
integer pointer type. This violates the strict aliasing rule, where a least-significant byte of the word as the first byte of data, without any pre
compiler can assume, for optimization purposes, that two pointers to or post conditioning. This is used to combine the CRCs of each braid.
fundamentally different types won't ever point to the same memory. This can
manifest as a problem only if one of the pointers is written to. This code
only reads from those pointers. So long as this code remains isolated in
this compilation unit, there won't be a problem. For this reason, this code
should not be copied and pasted into a compilation unit in which other code
writes to the buffer that is passed to these routines.
*/ */
local z_crc_t crc_word(data)
z_word_t data;
{
int k;
for (k = 0; k < W; k++)
data = (data >> 8) ^ crc_table[data & 0xff];
return (z_crc_t)data;
}
/* ========================================================================= */ local z_word_t crc_word_big(data)
#define DOLIT4 c ^= *buf4++; \ z_word_t data;
c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ {
crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] int k;
#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 for (k = 0; k < W; k++)
data = (data << 8) ^
crc_big_table[(data >> ((W - 1) << 3)) & 0xff];
return data;
}
#endif
/* ========================================================================= */ /* ========================================================================= */
local unsigned long crc32_little(crc, buf, len) unsigned long ZEXPORT crc32_z(crc, buf, len)
unsigned long crc; unsigned long crc;
const unsigned char FAR *buf; const unsigned char FAR *buf;
z_size_t len; z_size_t len;
{ {
register z_crc_t c; /* Return initial CRC, if requested. */
register const z_crc_t FAR *buf4; if (buf == Z_NULL) return 0;
#ifdef DYNAMIC_CRC_TABLE
once(&made, make_crc_table);
#endif /* DYNAMIC_CRC_TABLE */
/* Pre-condition the CRC */
crc ^= 0xffffffff;
c = (z_crc_t)crc; #ifdef W
c = ~c;
while (len && ((ptrdiff_t)buf & 3)) { /* If provided enough bytes, do a braided CRC calculation. */
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); if (len >= N * W + W - 1) {
z_size_t blks;
z_word_t const *words;
unsigned endian;
int k;
/* Compute the CRC up to a z_word_t boundary. */
while (len && ((z_size_t)buf & (W - 1)) != 0) {
len--; len--;
crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
} }
buf4 = (const z_crc_t FAR *)(const void FAR *)buf; /* Compute the CRC on as many N z_word_t blocks as are available. */
while (len >= 32) { blks = len / (N * W);
DOLIT32; len -= blks * N * W;
len -= 32; words = (z_word_t const *)buf;
/* Do endian check at execution time instead of compile time, since ARM
processors can change the endianess at execution time. If the
compiler knows what the endianess will be, it can optimize out the
check and the unused branch. */
endian = 1;
if (*(unsigned char *)&endian) {
/* Little endian. */
z_crc_t crc0;
z_word_t word0;
#if N > 1
z_crc_t crc1;
z_word_t word1;
#if N > 2
z_crc_t crc2;
z_word_t word2;
#if N > 3
z_crc_t crc3;
z_word_t word3;
#if N > 4
z_crc_t crc4;
z_word_t word4;
#if N > 5
z_crc_t crc5;
z_word_t word5;
#endif
#endif
#endif
#endif
#endif
/* Initialize the CRC for each braid. */
crc0 = crc;
#if N > 1
crc1 = 0;
#if N > 2
crc2 = 0;
#if N > 3
crc3 = 0;
#if N > 4
crc4 = 0;
#if N > 5
crc5 = 0;
#endif
#endif
#endif
#endif
#endif
/*
Process the first blks-1 blocks, computing the CRCs on each braid
independently.
*/
while (--blks) {
/* Load the word for each braid into registers. */
word0 = crc0 ^ words[0];
#if N > 1
word1 = crc1 ^ words[1];
#if N > 2
word2 = crc2 ^ words[2];
#if N > 3
word3 = crc3 ^ words[3];
#if N > 4
word4 = crc4 ^ words[4];
#if N > 5
word5 = crc5 ^ words[5];
#endif
#endif
#endif
#endif
#endif
words += N;
/* Compute and update the CRC for each word. The loop should
get unrolled. */
crc0 = crc_braid_table[0][word0 & 0xff];
#if N > 1
crc1 = crc_braid_table[0][word1 & 0xff];
#if N > 2
crc2 = crc_braid_table[0][word2 & 0xff];
#if N > 3
crc3 = crc_braid_table[0][word3 & 0xff];
#if N > 4
crc4 = crc_braid_table[0][word4 & 0xff];
#if N > 5
crc5 = crc_braid_table[0][word5 & 0xff];
#endif
#endif
#endif
#endif
#endif
for (k = 1; k < W; k++) {
crc0 ^= crc_braid_table[k][(word0 >> (k << 3)) & 0xff];
#if N > 1
crc1 ^= crc_braid_table[k][(word1 >> (k << 3)) & 0xff];
#if N > 2
crc2 ^= crc_braid_table[k][(word2 >> (k << 3)) & 0xff];
#if N > 3
crc3 ^= crc_braid_table[k][(word3 >> (k << 3)) & 0xff];
#if N > 4
crc4 ^= crc_braid_table[k][(word4 >> (k << 3)) & 0xff];
#if N > 5
crc5 ^= crc_braid_table[k][(word5 >> (k << 3)) & 0xff];
#endif
#endif
#endif
#endif
#endif
} }
while (len >= 4) {
DOLIT4;
len -= 4;
} }
buf = (const unsigned char FAR *)buf4;
if (len) do { /*
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); Process the last block, combining the CRCs of the N braids at the
} while (--len); same time.
c = ~c; */
return (unsigned long)c; crc = crc_word(crc0 ^ words[0]);
} #if N > 1
crc = crc_word(crc1 ^ words[1] ^ crc);
#if N > 2
crc = crc_word(crc2 ^ words[2] ^ crc);
#if N > 3
crc = crc_word(crc3 ^ words[3] ^ crc);
#if N > 4
crc = crc_word(crc4 ^ words[4] ^ crc);
#if N > 5
crc = crc_word(crc5 ^ words[5] ^ crc);
#endif
#endif
#endif
#endif
#endif
words += N;
}
else {
/* Big endian. */
/* ========================================================================= */ z_word_t crc0, word0, comb;
#define DOBIG4 c ^= *buf4++; \ #if N > 1
c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ z_word_t crc1, word1;
crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] #if N > 2
#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 z_word_t crc2, word2;
#if N > 3
z_word_t crc3, word3;
#if N > 4
z_word_t crc4, word4;
#if N > 5
z_word_t crc5, word5;
#endif
#endif
#endif
#endif
#endif
/* ========================================================================= */ /* Initialize the CRC for each braid. */
local unsigned long crc32_big(crc, buf, len) crc0 = byte_swap(crc);
unsigned long crc; #if N > 1
const unsigned char FAR *buf; crc1 = 0;
z_size_t len; #if N > 2
{ crc2 = 0;
register z_crc_t c; #if N > 3
register const z_crc_t FAR *buf4; crc3 = 0;
#if N > 4
crc4 = 0;
#if N > 5
crc5 = 0;
#endif
#endif
#endif
#endif
#endif
c = ZSWAP32((z_crc_t)crc); /*
c = ~c; Process the first blks-1 blocks, computing the CRCs on each braid
while (len && ((ptrdiff_t)buf & 3)) { independently.
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); */
len--; while (--blks) {
} /* Load the word for each braid into registers. */
word0 = crc0 ^ words[0];
#if N > 1
word1 = crc1 ^ words[1];
#if N > 2
word2 = crc2 ^ words[2];
#if N > 3
word3 = crc3 ^ words[3];
#if N > 4
word4 = crc4 ^ words[4];
#if N > 5
word5 = crc5 ^ words[5];
#endif
#endif
#endif
#endif
#endif
words += N;
buf4 = (const z_crc_t FAR *)(const void FAR *)buf; /* Compute and update the CRC for each word. The loop should
while (len >= 32) { get unrolled. */
DOBIG32; crc0 = crc_braid_big_table[0][word0 & 0xff];
len -= 32; #if N > 1
crc1 = crc_braid_big_table[0][word1 & 0xff];
#if N > 2
crc2 = crc_braid_big_table[0][word2 & 0xff];
#if N > 3
crc3 = crc_braid_big_table[0][word3 & 0xff];
#if N > 4
crc4 = crc_braid_big_table[0][word4 & 0xff];
#if N > 5
crc5 = crc_braid_big_table[0][word5 & 0xff];
#endif
#endif
#endif
#endif
#endif
for (k = 1; k < W; k++) {
crc0 ^= crc_braid_big_table[k][(word0 >> (k << 3)) & 0xff];
#if N > 1
crc1 ^= crc_braid_big_table[k][(word1 >> (k << 3)) & 0xff];
#if N > 2
crc2 ^= crc_braid_big_table[k][(word2 >> (k << 3)) & 0xff];
#if N > 3
crc3 ^= crc_braid_big_table[k][(word3 >> (k << 3)) & 0xff];
#if N > 4
crc4 ^= crc_braid_big_table[k][(word4 >> (k << 3)) & 0xff];
#if N > 5
crc5 ^= crc_braid_big_table[k][(word5 >> (k << 3)) & 0xff];
#endif
#endif
#endif
#endif
#endif
} }
while (len >= 4) {
DOBIG4;
len -= 4;
} }
buf = (const unsigned char FAR *)buf4;
if (len) do { /*
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); Process the last block, combining the CRCs of the N braids at the
} while (--len); same time.
c = ~c; */
return (unsigned long)(ZSWAP32(c)); comb = crc_word_big(crc0 ^ words[0]);
} #if N > 1
comb = crc_word_big(crc1 ^ words[1] ^ comb);
#endif /* BYFOUR */ #if N > 2
comb = crc_word_big(crc2 ^ words[2] ^ comb);
#if N > 3
comb = crc_word_big(crc3 ^ words[3] ^ comb);
#if N > 4
comb = crc_word_big(crc4 ^ words[4] ^ comb);
#if N > 5
comb = crc_word_big(crc5 ^ words[5] ^ comb);
#endif
#endif
#endif
#endif
#endif
words += N;
crc = byte_swap(comb);
}
#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ /*
Update the pointer to the remaining bytes to process.
*/
buf = (unsigned char const *)words;
}
/* ========================================================================= */ #endif /* W */
local unsigned long gf2_matrix_times(mat, vec)
unsigned long *mat;
unsigned long vec;
{
unsigned long sum;
sum = 0; /* Complete the computation of the CRC on any remaining bytes. */
while (vec) { while (len >= 8) {
if (vec & 1) len -= 8;
sum ^= *mat; crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
vec >>= 1; crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
mat++; crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
}
while (len) {
len--;
crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
} }
return sum;
/* Return the CRC, post-conditioned. */
return crc ^ 0xffffffff;
} }
#endif
/* ========================================================================= */ /* ========================================================================= */
local void gf2_matrix_square(square, mat) unsigned long ZEXPORT crc32(crc, buf, len)
unsigned long *square; unsigned long crc;
unsigned long *mat; const unsigned char FAR *buf;
uInt len;
{ {
int n; return crc32_z(crc, buf, len);
for (n = 0; n < GF2_DIM; n++)
square[n] = gf2_matrix_times(mat, mat[n]);
} }
/* ========================================================================= */ /* ========================================================================= */
local uLong crc32_combine_(crc1, crc2, len2) uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
uLong crc1; uLong crc1;
uLong crc2; uLong crc2;
z_off64_t len2; z_off64_t len2;
{ {
int n; #ifdef DYNAMIC_CRC_TABLE
unsigned long row; once(&made, make_crc_table);
unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ #endif /* DYNAMIC_CRC_TABLE */
unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ return multmodp(x2nmodp(len2, 3), crc1) ^ crc2;
/* degenerate case (also disallow negative lengths) */
if (len2 <= 0)
return crc1;
/* put operator for one zero bit in odd */
odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
row = 1;
for (n = 1; n < GF2_DIM; n++) {
odd[n] = row;
row <<= 1;
}
/* put operator for two zero bits in even */
gf2_matrix_square(even, odd);
/* put operator for four zero bits in odd */
gf2_matrix_square(odd, even);
/* apply len2 zeros to crc1 (first square will put the operator for one
zero byte, eight zero bits, in even) */
do {
/* apply zeros operator for this bit of len2 */
gf2_matrix_square(even, odd);
if (len2 & 1)
crc1 = gf2_matrix_times(even, crc1);
len2 >>= 1;
/* if no more bits set, then done */
if (len2 == 0)
break;
/* another iteration of the loop with odd and even swapped */
gf2_matrix_square(odd, even);
if (len2 & 1)
crc1 = gf2_matrix_times(odd, crc1);
len2 >>= 1;
/* if no more bits set, then done */
} while (len2 != 0);
/* return combined crc */
crc1 ^= crc2;
return crc1;
} }
/* ========================================================================= */ /* ========================================================================= */
...@@ -430,6 +1086,31 @@ uLong ZEXPORT crc32_combine(crc1, crc2, len2) ...@@ -430,6 +1086,31 @@ uLong ZEXPORT crc32_combine(crc1, crc2, len2)
uLong crc2; uLong crc2;
z_off_t len2; z_off_t len2;
{ {
return crc32_combine_(crc1, crc2, len2); return crc32_combine64(crc1, crc2, len2);
}
/* ========================================================================= */
uLong ZEXPORT crc32_combine_gen64(len2)
z_off64_t len2;
{
#ifdef DYNAMIC_CRC_TABLE
once(&made, make_crc_table);
#endif /* DYNAMIC_CRC_TABLE */
return x2nmodp(len2, 3);
}
/* ========================================================================= */
uLong ZEXPORT crc32_combine_gen(len2)
z_off_t len2;
{
return crc32_combine_gen64(len2);
} }
/* ========================================================================= */
uLong crc32_combine_op(crc1, crc2, op)
uLong crc1;
uLong crc2;
uLong op;
{
return multmodp(op, crc1) ^ crc2;
}
This source diff could not be displayed because it is too large. You can view the blob instead.
/* deflate.c -- compress data using the deflation algorithm /* deflate.c -- compress data using the deflation algorithm
* Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler * Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h * For conditions of distribution and use, see copyright notice in zlib.h
*/ */
...@@ -52,7 +52,7 @@ ...@@ -52,7 +52,7 @@
#include "deflate.h" #include "deflate.h"
const char deflate_copyright[] = const char deflate_copyright[] =
" deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler "; " deflate 1.2.12 Copyright 1995-2022 Jean-loup Gailly and 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
in the documentation of your product. If for some reason you cannot in the documentation of your product. If for some reason you cannot
...@@ -190,8 +190,11 @@ local const config configuration_table[10] = { ...@@ -190,8 +190,11 @@ local const config configuration_table[10] = {
* prev[] will be initialized on the fly. * prev[] will be initialized on the fly.
*/ */
#define CLEAR_HASH(s) \ #define CLEAR_HASH(s) \
do { \
s->head[s->hash_size-1] = NIL; \ s->head[s->hash_size-1] = NIL; \
zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); zmemzero((Bytef *)s->head, \
(unsigned)(s->hash_size-1)*sizeof(*s->head)); \
} while (0)
/* =========================================================================== /* ===========================================================================
* Slide the hash table when sliding the window down (could be avoided with 32 * Slide the hash table when sliding the window down (could be avoided with 32
...@@ -252,11 +255,6 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, ...@@ -252,11 +255,6 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
int wrap = 1; int wrap = 1;
static const char my_version[] = ZLIB_VERSION; static const char my_version[] = ZLIB_VERSION;
ushf *overlay;
/* We overlay pending_buf and d_buf+l_buf. This works since the average
* output size for (length,distance) codes is <= 24 bits.
*/
if (version == Z_NULL || version[0] != my_version[0] || if (version == Z_NULL || version[0] != my_version[0] ||
stream_size != sizeof(z_stream)) { stream_size != sizeof(z_stream)) {
return Z_VERSION_ERROR; return Z_VERSION_ERROR;
...@@ -320,16 +318,53 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, ...@@ -320,16 +318,53 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
memset(s->prev, 0, s->w_size * sizeof(Pos));
s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
s->high_water = 0; /* nothing written to s->window yet */ s->high_water = 0; /* nothing written to s->window yet */
s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); /* We overlay pending_buf and sym_buf. This works since the average size
s->pending_buf = (uchf *) overlay; * for length/distance pairs over any compressed block is assured to be 31
s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); * bits or less.
*
* Analysis: The longest fixed codes are a length code of 8 bits plus 5
* extra bits, for lengths 131 to 257. The longest fixed distance codes are
* 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
* possible fixed-codes length/distance pair is then 31 bits total.
*
* sym_buf starts one-fourth of the way into pending_buf. So there are
* three bytes in sym_buf for every four bytes in pending_buf. Each symbol
* in sym_buf is three bytes -- two for the distance and one for the
* literal/length. As each symbol is consumed, the pointer to the next
* sym_buf value to read moves forward three bytes. From that symbol, up to
* 31 bits are written to pending_buf. The closest the written pending_buf
* bits gets to the next sym_buf symbol to read is just before the last
* code is written. At that time, 31*(n-2) bits have been written, just
* after 24*(n-2) bits have been consumed from sym_buf. sym_buf starts at
* 8*n bits into pending_buf. (Note that the symbol buffer fills when n-1
* symbols are written.) The closest the writing gets to what is unread is
* then n+14 bits. Here n is lit_bufsize, which is 16384 by default, and
* can range from 128 to 32768.
*
* Therefore, at a minimum, there are 142 bits of space between what is
* written and what is read in the overlain buffers, so the symbols cannot
* be overwritten by the compressed data. That space is actually 139 bits,
* due to the three-bit fixed-code block header.
*
* That covers the case where either Z_FIXED is specified, forcing fixed
* codes, or when the use of fixed codes is chosen, because that choice
* results in a smaller compressed block than dynamic codes. That latter
* condition then assures that the above analysis also covers all dynamic
* blocks. A dynamic-code block will only be chosen to be emitted if it has
* fewer bits than a fixed-code block would for the same set of symbols.
* Therefore its average symbol length is assured to be less than 31. So
* the compressed data for a dynamic block also cannot overwrite the
* symbols from which it is being constructed.
*/
s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4);
s->pending_buf_size = (ulg)s->lit_bufsize * 4;
if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
s->pending_buf == Z_NULL) { s->pending_buf == Z_NULL) {
...@@ -338,8 +373,12 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, ...@@ -338,8 +373,12 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
deflateEnd (strm); deflateEnd (strm);
return Z_MEM_ERROR; return Z_MEM_ERROR;
} }
s->d_buf = overlay + s->lit_bufsize/sizeof(ush); s->sym_buf = s->pending_buf + s->lit_bufsize;
s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; s->sym_end = (s->lit_bufsize - 1) * 3;
/* We avoid equality with lit_bufsize*3 because of wraparound at 64K
* on 16 bit machines and because stored blocks are restricted to
* 64K-1 bytes.
*/
s->level = level; s->level = level;
s->strategy = strategy; s->strategy = strategy;
...@@ -489,13 +528,13 @@ int ZEXPORT deflateResetKeep (strm) ...@@ -489,13 +528,13 @@ int ZEXPORT deflateResetKeep (strm)
#ifdef GZIP #ifdef GZIP
s->wrap == 2 ? GZIP_STATE : s->wrap == 2 ? GZIP_STATE :
#endif #endif
s->wrap ? INIT_STATE : BUSY_STATE; INIT_STATE;
strm->adler = strm->adler =
#ifdef GZIP #ifdef GZIP
s->wrap == 2 ? crc32(0L, Z_NULL, 0) : s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
#endif #endif
adler32(0L, Z_NULL, 0); adler32(0L, Z_NULL, 0);
s->last_flush = Z_NO_FLUSH; s->last_flush = -2;
_tr_init(s); _tr_init(s);
...@@ -550,7 +589,8 @@ int ZEXPORT deflatePrime (strm, bits, value) ...@@ -550,7 +589,8 @@ int ZEXPORT deflatePrime (strm, bits, value)
if (deflateStateCheck(strm)) return Z_STREAM_ERROR; if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
s = strm->state; s = strm->state;
if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3)) if (bits < 0 || bits > 16 ||
s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
return Z_BUF_ERROR; return Z_BUF_ERROR;
do { do {
put = Buf_size - s->bi_valid; put = Buf_size - s->bi_valid;
...@@ -588,12 +628,12 @@ int ZEXPORT deflateParams(strm, level, strategy) ...@@ -588,12 +628,12 @@ int ZEXPORT deflateParams(strm, level, strategy)
func = configuration_table[s->level].func; func = configuration_table[s->level].func;
if ((strategy != s->strategy || func != configuration_table[level].func) && if ((strategy != s->strategy || func != configuration_table[level].func) &&
s->high_water) { s->last_flush != -2) {
/* Flush the last buffer: */ /* Flush the last buffer: */
int err = deflate(strm, Z_BLOCK); int err = deflate(strm, Z_BLOCK);
if (err == Z_STREAM_ERROR) if (err == Z_STREAM_ERROR)
return err; return err;
if (strm->avail_out == 0) if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead)
return Z_BUF_ERROR; return Z_BUF_ERROR;
} }
if (s->level != level) { if (s->level != level) {
...@@ -812,6 +852,8 @@ int ZEXPORT deflate (strm, flush) ...@@ -812,6 +852,8 @@ int ZEXPORT deflate (strm, flush)
} }
/* Write the header */ /* Write the header */
if (s->status == INIT_STATE && s->wrap == 0)
s->status = BUSY_STATE;
if (s->status == INIT_STATE) { if (s->status == INIT_STATE) {
/* zlib header */ /* zlib header */
uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
...@@ -1109,7 +1151,6 @@ int ZEXPORT deflateCopy (dest, source) ...@@ -1109,7 +1151,6 @@ int ZEXPORT deflateCopy (dest, source)
#else #else
deflate_state *ds; deflate_state *ds;
deflate_state *ss; deflate_state *ss;
ushf *overlay;
if (deflateStateCheck(source) || dest == Z_NULL) { if (deflateStateCheck(source) || dest == Z_NULL) {
...@@ -1129,8 +1170,7 @@ int ZEXPORT deflateCopy (dest, source) ...@@ -1129,8 +1170,7 @@ int ZEXPORT deflateCopy (dest, source)
ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, 4);
ds->pending_buf = (uchf *) overlay;
if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
ds->pending_buf == Z_NULL) { ds->pending_buf == Z_NULL) {
...@@ -1144,8 +1184,7 @@ int ZEXPORT deflateCopy (dest, source) ...@@ -1144,8 +1184,7 @@ int ZEXPORT deflateCopy (dest, source)
zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
ds->l_desc.dyn_tree = ds->dyn_ltree; ds->l_desc.dyn_tree = ds->dyn_ltree;
ds->d_desc.dyn_tree = ds->dyn_dtree; ds->d_desc.dyn_tree = ds->dyn_dtree;
...@@ -1514,6 +1553,8 @@ local void fill_window(s) ...@@ -1514,6 +1553,8 @@ local void fill_window(s)
s->match_start -= wsize; s->match_start -= wsize;
s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
s->block_start -= (long) wsize; s->block_start -= (long) wsize;
if (s->insert > s->strstart)
s->insert = s->strstart;
slide_hash(s); slide_hash(s);
more += wsize; more += wsize;
} }
...@@ -1743,6 +1784,7 @@ local block_state deflate_stored(s, flush) ...@@ -1743,6 +1784,7 @@ local block_state deflate_stored(s, flush)
s->matches = 2; /* clear hash */ s->matches = 2; /* clear hash */
zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size); zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
s->strstart = s->w_size; s->strstart = s->w_size;
s->insert = s->strstart;
} }
else { else {
if (s->window_size - s->strstart <= used) { if (s->window_size - s->strstart <= used) {
...@@ -1751,12 +1793,14 @@ local block_state deflate_stored(s, flush) ...@@ -1751,12 +1793,14 @@ local block_state deflate_stored(s, flush)
zmemcpy(s->window, s->window + s->w_size, s->strstart); zmemcpy(s->window, s->window + s->w_size, s->strstart);
if (s->matches < 2) if (s->matches < 2)
s->matches++; /* add a pending slide_hash() */ s->matches++; /* add a pending slide_hash() */
if (s->insert > s->strstart)
s->insert = s->strstart;
} }
zmemcpy(s->window + s->strstart, s->strm->next_in - used, used); zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
s->strstart += used; s->strstart += used;
s->insert += MIN(used, s->w_size - s->insert);
} }
s->block_start = s->strstart; s->block_start = s->strstart;
s->insert += MIN(used, s->w_size - s->insert);
} }
if (s->high_water < s->strstart) if (s->high_water < s->strstart)
s->high_water = s->strstart; s->high_water = s->strstart;
...@@ -1771,7 +1815,7 @@ local block_state deflate_stored(s, flush) ...@@ -1771,7 +1815,7 @@ local block_state deflate_stored(s, flush)
return block_done; return block_done;
/* Fill the window with any remaining input. */ /* Fill the window with any remaining input. */
have = s->window_size - s->strstart - 1; have = s->window_size - s->strstart;
if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) { if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) {
/* Slide the window down. */ /* Slide the window down. */
s->block_start -= s->w_size; s->block_start -= s->w_size;
...@@ -1780,12 +1824,15 @@ local block_state deflate_stored(s, flush) ...@@ -1780,12 +1824,15 @@ local block_state deflate_stored(s, flush)
if (s->matches < 2) if (s->matches < 2)
s->matches++; /* add a pending slide_hash() */ s->matches++; /* add a pending slide_hash() */
have += s->w_size; /* more space now */ have += s->w_size; /* more space now */
if (s->insert > s->strstart)
s->insert = s->strstart;
} }
if (have > s->strm->avail_in) if (have > s->strm->avail_in)
have = s->strm->avail_in; have = s->strm->avail_in;
if (have) { if (have) {
read_buf(s->strm, s->window + s->strstart, have); read_buf(s->strm, s->window + s->strstart, have);
s->strstart += have; s->strstart += have;
s->insert += MIN(have, s->w_size - s->insert);
} }
if (s->high_water < s->strstart) if (s->high_water < s->strstart)
s->high_water = s->strstart; s->high_water = s->strstart;
...@@ -1913,7 +1960,7 @@ local block_state deflate_fast(s, flush) ...@@ -1913,7 +1960,7 @@ local block_state deflate_fast(s, flush)
FLUSH_BLOCK(s, 1); FLUSH_BLOCK(s, 1);
return finish_done; return finish_done;
} }
if (s->last_lit) if (s->sym_next)
FLUSH_BLOCK(s, 0); FLUSH_BLOCK(s, 0);
return block_done; return block_done;
} }
...@@ -2044,7 +2091,7 @@ local block_state deflate_slow(s, flush) ...@@ -2044,7 +2091,7 @@ local block_state deflate_slow(s, flush)
FLUSH_BLOCK(s, 1); FLUSH_BLOCK(s, 1);
return finish_done; return finish_done;
} }
if (s->last_lit) if (s->sym_next)
FLUSH_BLOCK(s, 0); FLUSH_BLOCK(s, 0);
return block_done; return block_done;
} }
...@@ -2119,7 +2166,7 @@ local block_state deflate_rle(s, flush) ...@@ -2119,7 +2166,7 @@ local block_state deflate_rle(s, flush)
FLUSH_BLOCK(s, 1); FLUSH_BLOCK(s, 1);
return finish_done; return finish_done;
} }
if (s->last_lit) if (s->sym_next)
FLUSH_BLOCK(s, 0); FLUSH_BLOCK(s, 0);
return block_done; return block_done;
} }
...@@ -2158,7 +2205,7 @@ local block_state deflate_huff(s, flush) ...@@ -2158,7 +2205,7 @@ local block_state deflate_huff(s, flush)
FLUSH_BLOCK(s, 1); FLUSH_BLOCK(s, 1);
return finish_done; return finish_done;
} }
if (s->last_lit) if (s->sym_next)
FLUSH_BLOCK(s, 0); FLUSH_BLOCK(s, 0);
return block_done; return block_done;
} }
/* deflate.h -- internal compression state /* deflate.h -- internal compression state
* Copyright (C) 1995-2016 Jean-loup Gailly * Copyright (C) 1995-2018 Jean-loup Gailly
* For conditions of distribution and use, see copyright notice in zlib.h * For conditions of distribution and use, see copyright notice in zlib.h
*/ */
...@@ -217,7 +217,7 @@ typedef struct internal_state { ...@@ -217,7 +217,7 @@ typedef struct internal_state {
/* Depth of each subtree used as tie breaker for trees of equal frequency /* Depth of each subtree used as tie breaker for trees of equal frequency
*/ */
uchf *l_buf; /* buffer for literals or lengths */ uchf *sym_buf; /* buffer for distances and literals/lengths */
uInt lit_bufsize; uInt lit_bufsize;
/* Size of match buffer for literals/lengths. There are 4 reasons for /* Size of match buffer for literals/lengths. There are 4 reasons for
...@@ -239,13 +239,8 @@ typedef struct internal_state { ...@@ -239,13 +239,8 @@ typedef struct internal_state {
* - I can't count above 4 * - I can't count above 4
*/ */
uInt last_lit; /* running index in l_buf */ uInt sym_next; /* running index in sym_buf */
uInt sym_end; /* symbol table full when sym_next reaches this */
ushf *d_buf;
/* Buffer for distances. To simplify the code, d_buf and l_buf have
* the same number of elements. To use different lengths, an extra flag
* array would be necessary.
*/
ulg opt_len; /* bit length of current block with optimal trees */ ulg opt_len; /* bit length of current block with optimal trees */
ulg static_len; /* bit length of current block with static trees */ ulg static_len; /* bit length of current block with static trees */
...@@ -325,20 +320,22 @@ void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, ...@@ -325,20 +320,22 @@ void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
# define _tr_tally_lit(s, c, flush) \ # define _tr_tally_lit(s, c, flush) \
{ uch cc = (c); \ { uch cc = (c); \
s->d_buf[s->last_lit] = 0; \ s->sym_buf[s->sym_next++] = 0; \
s->l_buf[s->last_lit++] = cc; \ s->sym_buf[s->sym_next++] = 0; \
s->sym_buf[s->sym_next++] = cc; \
s->dyn_ltree[cc].Freq++; \ s->dyn_ltree[cc].Freq++; \
flush = (s->last_lit == s->lit_bufsize-1); \ flush = (s->sym_next == s->sym_end); \
} }
# define _tr_tally_dist(s, distance, length, flush) \ # define _tr_tally_dist(s, distance, length, flush) \
{ uch len = (uch)(length); \ { uch len = (uch)(length); \
ush dist = (ush)(distance); \ ush dist = (ush)(distance); \
s->d_buf[s->last_lit] = dist; \ s->sym_buf[s->sym_next++] = dist; \
s->l_buf[s->last_lit++] = len; \ s->sym_buf[s->sym_next++] = dist >> 8; \
s->sym_buf[s->sym_next++] = len; \
dist--; \ dist--; \
s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
s->dyn_dtree[d_code(dist)].Freq++; \ s->dyn_dtree[d_code(dist)].Freq++; \
flush = (s->last_lit == s->lit_bufsize-1); \ flush = (s->sym_next == s->sym_end); \
} }
#else #else
# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
......
/* gzguts.h -- zlib internal header definitions for gz* operations /* gzguts.h -- zlib internal header definitions for gz* operations
* Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013, 2016 Mark Adler * Copyright (C) 2004-2019 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h * For conditions of distribution and use, see copyright notice in zlib.h
*/ */
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
# include <io.h> # include <io.h>
#endif #endif
#if defined(_WIN32) || defined(__CYGWIN__) #if defined(_WIN32)
# define WIDECHAR # define WIDECHAR
#endif #endif
...@@ -190,6 +190,7 @@ typedef struct { ...@@ -190,6 +190,7 @@ typedef struct {
/* just for writing */ /* just for writing */
int level; /* compression level */ int level; /* compression level */
int strategy; /* compression strategy */ int strategy; /* compression strategy */
int reset; /* true if a reset is pending after a Z_FINISH */
/* seek request */ /* seek request */
z_off64_t skip; /* amount to skip (already rewound if backwards) */ z_off64_t skip; /* amount to skip (already rewound if backwards) */
int seek; /* true if seek request pending */ int seek; /* true if seek request pending */
......
/* infback.c -- inflate using a call-back interface /* infback.c -- inflate using a call-back interface
* Copyright (C) 1995-2016 Mark Adler * Copyright (C) 1995-2022 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h * For conditions of distribution and use, see copyright notice in zlib.h
*/ */
...@@ -477,6 +477,7 @@ void FAR *out_desc; ...@@ -477,6 +477,7 @@ void FAR *out_desc;
} }
Tracev((stderr, "inflate: codes ok\n")); Tracev((stderr, "inflate: codes ok\n"));
state->mode = LEN; state->mode = LEN;
/* fallthrough */
case LEN: case LEN:
/* use inflate_fast() if we have enough input and output */ /* use inflate_fast() if we have enough input and output */
......
...@@ -70,7 +70,7 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ ...@@ -70,7 +70,7 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */
code const FAR *dcode; /* local strm->distcode */ code const FAR *dcode; /* local strm->distcode */
unsigned lmask; /* mask for first level of length codes */ unsigned lmask; /* mask for first level of length codes */
unsigned dmask; /* mask for first level of distance codes */ unsigned dmask; /* mask for first level of distance codes */
code here; /* retrieved table entry */ code const *here; /* retrieved table entry */
unsigned op; /* code bits, operation, extra bits, or */ unsigned op; /* code bits, operation, extra bits, or */
/* window position, window bytes to copy */ /* window position, window bytes to copy */
unsigned len; /* match length, unused bytes */ unsigned len; /* match length, unused bytes */
...@@ -107,20 +107,20 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ ...@@ -107,20 +107,20 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */
hold += (unsigned long)(*in++) << bits; hold += (unsigned long)(*in++) << bits;
bits += 8; bits += 8;
} }
here = lcode[hold & lmask]; here = lcode + (hold & lmask);
dolen: dolen:
op = (unsigned)(here.bits); op = (unsigned)(here->bits);
hold >>= op; hold >>= op;
bits -= op; bits -= op;
op = (unsigned)(here.op); op = (unsigned)(here->op);
if (op == 0) { /* literal */ if (op == 0) { /* literal */
Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ?
"inflate: literal '%c'\n" : "inflate: literal '%c'\n" :
"inflate: literal 0x%02x\n", here.val)); "inflate: literal 0x%02x\n", here->val));
*out++ = (unsigned char)(here.val); *out++ = (unsigned char)(here->val);
} }
else if (op & 16) { /* length base */ else if (op & 16) { /* length base */
len = (unsigned)(here.val); len = (unsigned)(here->val);
op &= 15; /* number of extra bits */ op &= 15; /* number of extra bits */
if (op) { if (op) {
if (bits < op) { if (bits < op) {
...@@ -138,14 +138,14 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ ...@@ -138,14 +138,14 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */
hold += (unsigned long)(*in++) << bits; hold += (unsigned long)(*in++) << bits;
bits += 8; bits += 8;
} }
here = dcode[hold & dmask]; here = dcode + (hold & dmask);
dodist: dodist:
op = (unsigned)(here.bits); op = (unsigned)(here->bits);
hold >>= op; hold >>= op;
bits -= op; bits -= op;
op = (unsigned)(here.op); op = (unsigned)(here->op);
if (op & 16) { /* distance base */ if (op & 16) { /* distance base */
dist = (unsigned)(here.val); dist = (unsigned)(here->val);
op &= 15; /* number of extra bits */ op &= 15; /* number of extra bits */
if (bits < op) { if (bits < op) {
hold += (unsigned long)(*in++) << bits; hold += (unsigned long)(*in++) << bits;
...@@ -264,7 +264,7 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ ...@@ -264,7 +264,7 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */
} }
} }
else if ((op & 64) == 0) { /* 2nd level distance code */ else if ((op & 64) == 0) { /* 2nd level distance code */
here = dcode[here.val + (hold & ((1U << op) - 1))]; here = dcode + here->val + (hold & ((1U << op) - 1));
goto dodist; goto dodist;
} }
else { else {
...@@ -274,7 +274,7 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ ...@@ -274,7 +274,7 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */
} }
} }
else if ((op & 64) == 0) { /* 2nd level length code */ else if ((op & 64) == 0) { /* 2nd level length code */
here = lcode[here.val + (hold & ((1U << op) - 1))]; here = lcode + here->val + (hold & ((1U << op) - 1));
goto dolen; goto dolen;
} }
else if (op & 32) { /* end-of-block */ else if (op & 32) { /* end-of-block */
......
/* inflate.c -- zlib decompression /* inflate.c -- zlib decompression
* Copyright (C) 1995-2016 Mark Adler * Copyright (C) 1995-2022 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h * For conditions of distribution and use, see copyright notice in zlib.h
*/ */
...@@ -130,6 +130,7 @@ z_streamp strm; ...@@ -130,6 +130,7 @@ z_streamp strm;
state->mode = HEAD; state->mode = HEAD;
state->last = 0; state->last = 0;
state->havedict = 0; state->havedict = 0;
state->flags = -1;
state->dmax = 32768U; state->dmax = 32768U;
state->head = Z_NULL; state->head = Z_NULL;
state->hold = 0; state->hold = 0;
...@@ -447,10 +448,10 @@ unsigned copy; ...@@ -447,10 +448,10 @@ unsigned copy;
/* check function to use adler32() for zlib or crc32() for gzip */ /* check function to use adler32() for zlib or crc32() for gzip */
#ifdef GUNZIP #ifdef GUNZIP
# define UPDATE(check, buf, len) \ # define UPDATE_CHECK(check, buf, len) \
(state->flags ? crc32(check, buf, len) : adler32(check, buf, len)) (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
#else #else
# define UPDATE(check, buf, len) adler32(check, buf, len) # define UPDATE_CHECK(check, buf, len) adler32(check, buf, len)
#endif #endif
/* check macros for header crc */ /* check macros for header crc */
...@@ -670,7 +671,6 @@ int flush; ...@@ -670,7 +671,6 @@ int flush;
state->mode = FLAGS; state->mode = FLAGS;
break; break;
} }
state->flags = 0; /* expect zlib header */
if (state->head != Z_NULL) if (state->head != Z_NULL)
state->head->done = -1; state->head->done = -1;
if (!(state->wrap & 1) || /* check if zlib header allowed */ if (!(state->wrap & 1) || /* check if zlib header allowed */
...@@ -697,6 +697,7 @@ int flush; ...@@ -697,6 +697,7 @@ int flush;
break; break;
} }
state->dmax = 1U << len; state->dmax = 1U << len;
state->flags = 0; /* indicate zlib header */
Tracev((stderr, "inflate: zlib header ok\n")); Tracev((stderr, "inflate: zlib header ok\n"));
strm->adler = state->check = adler32(0L, Z_NULL, 0); strm->adler = state->check = adler32(0L, Z_NULL, 0);
state->mode = hold & 0x200 ? DICTID : TYPE; state->mode = hold & 0x200 ? DICTID : TYPE;
...@@ -722,6 +723,7 @@ int flush; ...@@ -722,6 +723,7 @@ int flush;
CRC2(state->check, hold); CRC2(state->check, hold);
INITBITS(); INITBITS();
state->mode = TIME; state->mode = TIME;
/* fallthrough */
case TIME: case TIME:
NEEDBITS(32); NEEDBITS(32);
if (state->head != Z_NULL) if (state->head != Z_NULL)
...@@ -730,6 +732,7 @@ int flush; ...@@ -730,6 +732,7 @@ int flush;
CRC4(state->check, hold); CRC4(state->check, hold);
INITBITS(); INITBITS();
state->mode = OS; state->mode = OS;
/* fallthrough */
case OS: case OS:
NEEDBITS(16); NEEDBITS(16);
if (state->head != Z_NULL) { if (state->head != Z_NULL) {
...@@ -740,6 +743,7 @@ int flush; ...@@ -740,6 +743,7 @@ int flush;
CRC2(state->check, hold); CRC2(state->check, hold);
INITBITS(); INITBITS();
state->mode = EXLEN; state->mode = EXLEN;
/* fallthrough */
case EXLEN: case EXLEN:
if (state->flags & 0x0400) { if (state->flags & 0x0400) {
NEEDBITS(16); NEEDBITS(16);
...@@ -753,6 +757,7 @@ int flush; ...@@ -753,6 +757,7 @@ int flush;
else if (state->head != Z_NULL) else if (state->head != Z_NULL)
state->head->extra = Z_NULL; state->head->extra = Z_NULL;
state->mode = EXTRA; state->mode = EXTRA;
/* fallthrough */
case EXTRA: case EXTRA:
if (state->flags & 0x0400) { if (state->flags & 0x0400) {
copy = state->length; copy = state->length;
...@@ -775,6 +780,7 @@ int flush; ...@@ -775,6 +780,7 @@ int flush;
} }
state->length = 0; state->length = 0;
state->mode = NAME; state->mode = NAME;
/* fallthrough */
case NAME: case NAME:
if (state->flags & 0x0800) { if (state->flags & 0x0800) {
if (have == 0) goto inf_leave; if (have == 0) goto inf_leave;
...@@ -796,6 +802,7 @@ int flush; ...@@ -796,6 +802,7 @@ int flush;
state->head->name = Z_NULL; state->head->name = Z_NULL;
state->length = 0; state->length = 0;
state->mode = COMMENT; state->mode = COMMENT;
/* fallthrough */
case COMMENT: case COMMENT:
if (state->flags & 0x1000) { if (state->flags & 0x1000) {
if (have == 0) goto inf_leave; if (have == 0) goto inf_leave;
...@@ -816,6 +823,7 @@ int flush; ...@@ -816,6 +823,7 @@ int flush;
else if (state->head != Z_NULL) else if (state->head != Z_NULL)
state->head->comment = Z_NULL; state->head->comment = Z_NULL;
state->mode = HCRC; state->mode = HCRC;
/* fallthrough */
case HCRC: case HCRC:
if (state->flags & 0x0200) { if (state->flags & 0x0200) {
NEEDBITS(16); NEEDBITS(16);
...@@ -839,6 +847,7 @@ int flush; ...@@ -839,6 +847,7 @@ int flush;
strm->adler = state->check = ZSWAP32(hold); strm->adler = state->check = ZSWAP32(hold);
INITBITS(); INITBITS();
state->mode = DICT; state->mode = DICT;
/* fallthrough */
case DICT: case DICT:
if (state->havedict == 0) { if (state->havedict == 0) {
RESTORE(); RESTORE();
...@@ -846,8 +855,10 @@ int flush; ...@@ -846,8 +855,10 @@ int flush;
} }
strm->adler = state->check = adler32(0L, Z_NULL, 0); strm->adler = state->check = adler32(0L, Z_NULL, 0);
state->mode = TYPE; state->mode = TYPE;
/* fallthrough */
case TYPE: case TYPE:
if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave; if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
/* fallthrough */
case TYPEDO: case TYPEDO:
if (state->last) { if (state->last) {
BYTEBITS(); BYTEBITS();
...@@ -898,8 +909,10 @@ int flush; ...@@ -898,8 +909,10 @@ int flush;
INITBITS(); INITBITS();
state->mode = COPY_; state->mode = COPY_;
if (flush == Z_TREES) goto inf_leave; if (flush == Z_TREES) goto inf_leave;
/* fallthrough */
case COPY_: case COPY_:
state->mode = COPY; state->mode = COPY;
/* fallthrough */
case COPY: case COPY:
copy = state->length; copy = state->length;
if (copy) { if (copy) {
...@@ -935,6 +948,7 @@ int flush; ...@@ -935,6 +948,7 @@ int flush;
Tracev((stderr, "inflate: table sizes ok\n")); Tracev((stderr, "inflate: table sizes ok\n"));
state->have = 0; state->have = 0;
state->mode = LENLENS; state->mode = LENLENS;
/* fallthrough */
case LENLENS: case LENLENS:
while (state->have < state->ncode) { while (state->have < state->ncode) {
NEEDBITS(3); NEEDBITS(3);
...@@ -956,6 +970,7 @@ int flush; ...@@ -956,6 +970,7 @@ int flush;
Tracev((stderr, "inflate: code lengths ok\n")); Tracev((stderr, "inflate: code lengths ok\n"));
state->have = 0; state->have = 0;
state->mode = CODELENS; state->mode = CODELENS;
/* fallthrough */
case CODELENS: case CODELENS:
while (state->have < state->nlen + state->ndist) { while (state->have < state->nlen + state->ndist) {
for (;;) { for (;;) {
...@@ -1039,8 +1054,10 @@ int flush; ...@@ -1039,8 +1054,10 @@ int flush;
Tracev((stderr, "inflate: codes ok\n")); Tracev((stderr, "inflate: codes ok\n"));
state->mode = LEN_; state->mode = LEN_;
if (flush == Z_TREES) goto inf_leave; if (flush == Z_TREES) goto inf_leave;
/* fallthrough */
case LEN_: case LEN_:
state->mode = LEN; state->mode = LEN;
/* fallthrough */
case LEN: case LEN:
if (have >= 6 && left >= 258) { if (have >= 6 && left >= 258) {
RESTORE(); RESTORE();
...@@ -1090,6 +1107,7 @@ int flush; ...@@ -1090,6 +1107,7 @@ int flush;
} }
state->extra = (unsigned)(here.op) & 15; state->extra = (unsigned)(here.op) & 15;
state->mode = LENEXT; state->mode = LENEXT;
/* fallthrough */
case LENEXT: case LENEXT:
if (state->extra) { if (state->extra) {
NEEDBITS(state->extra); NEEDBITS(state->extra);
...@@ -1100,6 +1118,7 @@ int flush; ...@@ -1100,6 +1118,7 @@ int flush;
Tracevv((stderr, "inflate: length %u\n", state->length)); Tracevv((stderr, "inflate: length %u\n", state->length));
state->was = state->length; state->was = state->length;
state->mode = DIST; state->mode = DIST;
/* fallthrough */
case DIST: case DIST:
for (;;) { for (;;) {
here = state->distcode[BITS(state->distbits)]; here = state->distcode[BITS(state->distbits)];
...@@ -1127,6 +1146,7 @@ int flush; ...@@ -1127,6 +1146,7 @@ int flush;
state->offset = (unsigned)here.val; state->offset = (unsigned)here.val;
state->extra = (unsigned)(here.op) & 15; state->extra = (unsigned)(here.op) & 15;
state->mode = DISTEXT; state->mode = DISTEXT;
/* fallthrough */
case DISTEXT: case DISTEXT:
if (state->extra) { if (state->extra) {
NEEDBITS(state->extra); NEEDBITS(state->extra);
...@@ -1143,6 +1163,7 @@ int flush; ...@@ -1143,6 +1163,7 @@ int flush;
#endif #endif
Tracevv((stderr, "inflate: distance %u\n", state->offset)); Tracevv((stderr, "inflate: distance %u\n", state->offset));
state->mode = MATCH; state->mode = MATCH;
/* fallthrough */
case MATCH: case MATCH:
if (left == 0) goto inf_leave; if (left == 0) goto inf_leave;
copy = out - left; copy = out - left;
...@@ -1202,7 +1223,7 @@ int flush; ...@@ -1202,7 +1223,7 @@ int flush;
state->total += out; state->total += out;
if ((state->wrap & 4) && out) if ((state->wrap & 4) && out)
strm->adler = state->check = strm->adler = state->check =
UPDATE(state->check, put - out, out); UPDATE_CHECK(state->check, put - out, out);
out = left; out = left;
if ((state->wrap & 4) && ( if ((state->wrap & 4) && (
#ifdef GUNZIP #ifdef GUNZIP
...@@ -1218,10 +1239,11 @@ int flush; ...@@ -1218,10 +1239,11 @@ int flush;
} }
#ifdef GUNZIP #ifdef GUNZIP
state->mode = LENGTH; state->mode = LENGTH;
/* fallthrough */
case LENGTH: case LENGTH:
if (state->wrap && state->flags) { if (state->wrap && state->flags) {
NEEDBITS(32); NEEDBITS(32);
if (hold != (state->total & 0xffffffffUL)) { if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
strm->msg = (char *)"incorrect length check"; strm->msg = (char *)"incorrect length check";
state->mode = BAD; state->mode = BAD;
break; break;
...@@ -1231,6 +1253,7 @@ int flush; ...@@ -1231,6 +1253,7 @@ int flush;
} }
#endif #endif
state->mode = DONE; state->mode = DONE;
/* fallthrough */
case DONE: case DONE:
ret = Z_STREAM_END; ret = Z_STREAM_END;
goto inf_leave; goto inf_leave;
...@@ -1240,6 +1263,7 @@ int flush; ...@@ -1240,6 +1263,7 @@ int flush;
case MEM: case MEM:
return Z_MEM_ERROR; return Z_MEM_ERROR;
case SYNC: case SYNC:
/* fallthrough */
default: default:
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
} }
...@@ -1265,7 +1289,7 @@ int flush; ...@@ -1265,7 +1289,7 @@ int flush;
state->total += out; state->total += out;
if ((state->wrap & 4) && out) if ((state->wrap & 4) && out)
strm->adler = state->check = strm->adler = state->check =
UPDATE(state->check, strm->next_out - out, out); UPDATE_CHECK(state->check, strm->next_out - out, out);
strm->data_type = (int)state->bits + (state->last ? 64 : 0) + strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
(state->mode == TYPE ? 128 : 0) + (state->mode == TYPE ? 128 : 0) +
(state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
...@@ -1401,6 +1425,7 @@ int ZEXPORT inflateSync(strm) ...@@ -1401,6 +1425,7 @@ int ZEXPORT inflateSync(strm)
z_streamp strm; z_streamp strm;
{ {
unsigned len; /* number of bytes to look at or looked at */ unsigned len; /* number of bytes to look at or looked at */
int flags; /* temporary to save header status */
unsigned long in, out; /* temporary to save total_in and total_out */ unsigned long in, out; /* temporary to save total_in and total_out */
unsigned char buf[4]; /* to restore bit buffer to byte string */ unsigned char buf[4]; /* to restore bit buffer to byte string */
struct inflate_state FAR *state; struct inflate_state FAR *state;
...@@ -1433,9 +1458,15 @@ z_streamp strm; ...@@ -1433,9 +1458,15 @@ z_streamp strm;
/* return no joy or set up to restart inflate() on a new block */ /* return no joy or set up to restart inflate() on a new block */
if (state->have != 4) return Z_DATA_ERROR; if (state->have != 4) return Z_DATA_ERROR;
if (state->flags == -1)
state->wrap = 0; /* if no header yet, treat as raw */
else
state->wrap &= ~4; /* no point in computing a check value now */
flags = state->flags;
in = strm->total_in; out = strm->total_out; in = strm->total_in; out = strm->total_out;
inflateReset(strm); inflateReset(strm);
strm->total_in = in; strm->total_out = out; strm->total_in = in; strm->total_out = out;
state->flags = flags;
state->mode = TYPE; state->mode = TYPE;
return Z_OK; return Z_OK;
} }
...@@ -1531,7 +1562,7 @@ int check; ...@@ -1531,7 +1562,7 @@ int check;
if (inflateStateCheck(strm)) return Z_STREAM_ERROR; if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state; state = (struct inflate_state FAR *)strm->state;
if (check) if (check && state->wrap)
state->wrap |= 4; state->wrap |= 4;
else else
state->wrap &= ~4; state->wrap &= ~4;
......
/* inflate.h -- internal inflate state definition /* inflate.h -- internal inflate state definition
* Copyright (C) 1995-2016 Mark Adler * Copyright (C) 1995-2019 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h * For conditions of distribution and use, see copyright notice in zlib.h
*/ */
...@@ -86,7 +86,8 @@ struct inflate_state { ...@@ -86,7 +86,8 @@ struct inflate_state {
int wrap; /* bit 0 true for zlib, bit 1 true for gzip, int wrap; /* bit 0 true for zlib, bit 1 true for gzip,
bit 2 true to validate check value */ bit 2 true to validate check value */
int havedict; /* true if dictionary provided */ int havedict; /* true if dictionary provided */
int flags; /* gzip header method and flags (0 if zlib) */ int flags; /* gzip header method and flags, 0 if zlib, or
-1 if raw or no header yet */
unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
unsigned long check; /* protected copy of check value */ unsigned long check; /* protected copy of check value */
unsigned long total; /* protected copy of output count */ unsigned long total; /* protected copy of output count */
......
/* inftrees.c -- generate Huffman trees for efficient decoding /* inftrees.c -- generate Huffman trees for efficient decoding
* Copyright (C) 1995-2017 Mark Adler * Copyright (C) 1995-2022 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h * For conditions of distribution and use, see copyright notice in zlib.h
*/ */
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#define MAXBITS 15 #define MAXBITS 15
const char inflate_copyright[] = const char inflate_copyright[] =
" inflate 1.2.11 Copyright 1995-2017 Mark Adler "; " inflate 1.2.12 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
in the documentation of your product. If for some reason you cannot in the documentation of your product. If for some reason you cannot
...@@ -62,7 +62,7 @@ unsigned short FAR *work; ...@@ -62,7 +62,7 @@ unsigned short FAR *work;
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};
static const unsigned short lext[31] = { /* Length codes 257..285 extra */ static const unsigned short lext[31] = { /* Length codes 257..285 extra */
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 77, 202}; 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 199, 202};
static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
......
/* trees.c -- output deflated data using Huffman coding /* trees.c -- output deflated data using Huffman coding
* Copyright (C) 1995-2017 Jean-loup Gailly * Copyright (C) 1995-2021 Jean-loup Gailly
* detect_data_type() function provided freely by Cosmin Truta, 2006 * detect_data_type() function provided freely by Cosmin Truta, 2006
* For conditions of distribution and use, see copyright notice in zlib.h * For conditions of distribution and use, see copyright notice in zlib.h
*/ */
...@@ -149,7 +149,7 @@ local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes, ...@@ -149,7 +149,7 @@ local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
local void compress_block OF((deflate_state *s, const ct_data *ltree, local void compress_block OF((deflate_state *s, const ct_data *ltree,
const ct_data *dtree)); const ct_data *dtree));
local int detect_data_type OF((deflate_state *s)); local int detect_data_type OF((deflate_state *s));
local unsigned bi_reverse OF((unsigned value, int length)); local unsigned bi_reverse OF((unsigned code, int len));
local void bi_windup OF((deflate_state *s)); local void bi_windup OF((deflate_state *s));
local void bi_flush OF((deflate_state *s)); local void bi_flush OF((deflate_state *s));
...@@ -416,7 +416,7 @@ local void init_block(s) ...@@ -416,7 +416,7 @@ local void init_block(s)
s->dyn_ltree[END_BLOCK].Freq = 1; s->dyn_ltree[END_BLOCK].Freq = 1;
s->opt_len = s->static_len = 0L; s->opt_len = s->static_len = 0L;
s->last_lit = s->matches = 0; s->sym_next = s->matches = 0;
} }
#define SMALLEST 1 #define SMALLEST 1
...@@ -870,6 +870,7 @@ void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last) ...@@ -870,6 +870,7 @@ void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
bi_windup(s); /* align on byte boundary */ bi_windup(s); /* align on byte boundary */
put_short(s, (ush)stored_len); put_short(s, (ush)stored_len);
put_short(s, (ush)~stored_len); put_short(s, (ush)~stored_len);
if (stored_len)
zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len); zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len);
s->pending += stored_len; s->pending += stored_len;
#ifdef ZLIB_DEBUG #ifdef ZLIB_DEBUG
...@@ -947,7 +948,7 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) ...@@ -947,7 +948,7 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
s->last_lit)); s->sym_next / 3));
if (static_lenb <= opt_lenb) opt_lenb = static_lenb; if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
...@@ -1016,8 +1017,9 @@ int ZLIB_INTERNAL _tr_tally (s, dist, lc) ...@@ -1016,8 +1017,9 @@ int ZLIB_INTERNAL _tr_tally (s, dist, lc)
unsigned dist; /* distance of matched string */ unsigned dist; /* distance of matched string */
unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
{ {
s->d_buf[s->last_lit] = (ush)dist; s->sym_buf[s->sym_next++] = dist;
s->l_buf[s->last_lit++] = (uch)lc; s->sym_buf[s->sym_next++] = dist >> 8;
s->sym_buf[s->sym_next++] = lc;
if (dist == 0) { if (dist == 0) {
/* lc is the unmatched char */ /* lc is the unmatched char */
s->dyn_ltree[lc].Freq++; s->dyn_ltree[lc].Freq++;
...@@ -1032,30 +1034,7 @@ int ZLIB_INTERNAL _tr_tally (s, dist, lc) ...@@ -1032,30 +1034,7 @@ int ZLIB_INTERNAL _tr_tally (s, dist, lc)
s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++; s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
s->dyn_dtree[d_code(dist)].Freq++; s->dyn_dtree[d_code(dist)].Freq++;
} }
return (s->sym_next == s->sym_end);
#ifdef TRUNCATE_BLOCK
/* Try to guess if it is profitable to stop the current block here */
if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
/* Compute an upper bound for the compressed length */
ulg out_length = (ulg)s->last_lit*8L;
ulg in_length = (ulg)((long)s->strstart - s->block_start);
int dcode;
for (dcode = 0; dcode < D_CODES; dcode++) {
out_length += (ulg)s->dyn_dtree[dcode].Freq *
(5L+extra_dbits[dcode]);
}
out_length >>= 3;
Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
s->last_lit, in_length, out_length,
100L - out_length*100L/in_length));
if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
}
#endif
return (s->last_lit == s->lit_bufsize-1);
/* We avoid equality with lit_bufsize because of wraparound at 64K
* on 16 bit machines and because stored blocks are restricted to
* 64K-1 bytes.
*/
} }
/* =========================================================================== /* ===========================================================================
...@@ -1068,13 +1047,14 @@ local void compress_block(s, ltree, dtree) ...@@ -1068,13 +1047,14 @@ local void compress_block(s, ltree, dtree)
{ {
unsigned dist; /* distance of matched string */ unsigned dist; /* distance of matched string */
int lc; /* match length or unmatched char (if dist == 0) */ int lc; /* match length or unmatched char (if dist == 0) */
unsigned lx = 0; /* running index in l_buf */ unsigned sx = 0; /* running index in sym_buf */
unsigned code; /* the code to send */ unsigned code; /* the code to send */
int extra; /* number of extra bits to send */ int extra; /* number of extra bits to send */
if (s->last_lit != 0) do { if (s->sym_next != 0) do {
dist = s->d_buf[lx]; dist = s->sym_buf[sx++] & 0xff;
lc = s->l_buf[lx++]; dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8;
lc = s->sym_buf[sx++];
if (dist == 0) { if (dist == 0) {
send_code(s, lc, ltree); /* send a literal byte */ send_code(s, lc, ltree); /* send a literal byte */
Tracecv(isgraph(lc), (stderr," '%c' ", lc)); Tracecv(isgraph(lc), (stderr," '%c' ", lc));
...@@ -1099,11 +1079,10 @@ local void compress_block(s, ltree, dtree) ...@@ -1099,11 +1079,10 @@ local void compress_block(s, ltree, dtree)
} }
} /* literal or match pair ? */ } /* literal or match pair ? */
/* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ /* Check that the overlay between pending_buf and sym_buf is ok: */
Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow");
"pendingBuf overflow");
} while (lx < s->last_lit); } while (sx < s->sym_next);
send_code(s, END_BLOCK, ltree); send_code(s, END_BLOCK, ltree);
} }
...@@ -1112,9 +1091,9 @@ local void compress_block(s, ltree, dtree) ...@@ -1112,9 +1091,9 @@ local void compress_block(s, ltree, dtree)
* Check if the data type is TEXT or BINARY, using the following algorithm: * Check if the data type is TEXT or BINARY, using the following algorithm:
* - TEXT if the two conditions below are satisfied: * - TEXT if the two conditions below are satisfied:
* a) There are no non-portable control characters belonging to the * a) There are no non-portable control characters belonging to the
* "black list" (0..6, 14..25, 28..31). * "block list" (0..6, 14..25, 28..31).
* b) There is at least one printable character belonging to the * b) There is at least one printable character belonging to the
* "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). * "allow list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
* - BINARY otherwise. * - BINARY otherwise.
* - The following partially-portable control characters form a * - The following partially-portable control characters form a
* "gray list" that is ignored in this detection algorithm: * "gray list" that is ignored in this detection algorithm:
...@@ -1124,19 +1103,19 @@ local void compress_block(s, ltree, dtree) ...@@ -1124,19 +1103,19 @@ local void compress_block(s, ltree, dtree)
local int detect_data_type(s) local int detect_data_type(s)
deflate_state *s; deflate_state *s;
{ {
/* black_mask is the bit mask of black-listed bytes /* block_mask is the bit mask of block-listed bytes
* set bits 0..6, 14..25, and 28..31 * set bits 0..6, 14..25, and 28..31
* 0xf3ffc07f = binary 11110011111111111100000001111111 * 0xf3ffc07f = binary 11110011111111111100000001111111
*/ */
unsigned long black_mask = 0xf3ffc07fUL; unsigned long block_mask = 0xf3ffc07fUL;
int n; int n;
/* Check for non-textual ("black-listed") bytes. */ /* Check for non-textual ("block-listed") bytes. */
for (n = 0; n <= 31; n++, black_mask >>= 1) for (n = 0; n <= 31; n++, block_mask >>= 1)
if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0)) if ((block_mask & 1) && (s->dyn_ltree[n].Freq != 0))
return Z_BINARY; return Z_BINARY;
/* Check for textual ("white-listed") bytes. */ /* Check for textual ("allow-listed") bytes. */
if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
|| s->dyn_ltree[13].Freq != 0) || s->dyn_ltree[13].Freq != 0)
return Z_TEXT; return Z_TEXT;
...@@ -1144,7 +1123,7 @@ local int detect_data_type(s) ...@@ -1144,7 +1123,7 @@ local int detect_data_type(s)
if (s->dyn_ltree[n].Freq != 0) if (s->dyn_ltree[n].Freq != 0)
return Z_TEXT; return Z_TEXT;
/* There are no "black-listed" or "white-listed" bytes: /* There are no "block-listed" or "allow-listed" bytes:
* this stream either is empty or has tolerated ("gray-listed") bytes only. * this stream either is empty or has tolerated ("gray-listed") bytes only.
*/ */
return Z_BINARY; return Z_BINARY;
......
/* zlib.h -- interface of the 'zlib' general purpose compression library /* zlib.h -- interface of the 'zlib' general purpose compression library
version 1.2.11, January 15th, 2017 version 1.2.12, March 11th, 2022
Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
...@@ -37,11 +37,11 @@ ...@@ -37,11 +37,11 @@
extern "C" { extern "C" {
#endif #endif
#define ZLIB_VERSION "1.2.11" #define ZLIB_VERSION "1.2.12"
#define ZLIB_VERNUM 0x12b0 #define ZLIB_VERNUM 0x12c0
#define ZLIB_VER_MAJOR 1 #define ZLIB_VER_MAJOR 1
#define ZLIB_VER_MINOR 2 #define ZLIB_VER_MINOR 2
#define ZLIB_VER_REVISION 11 #define ZLIB_VER_REVISION 12
#define ZLIB_VER_SUBREVISION 0 #define ZLIB_VER_SUBREVISION 0
/* /*
...@@ -543,8 +543,7 @@ ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, ...@@ -543,8 +543,7 @@ ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
int strategy)); int strategy));
This is another version of deflateInit with more compression options. The This is another version of deflateInit with more compression options. The
fields next_in, zalloc, zfree and opaque must be initialized before by the fields zalloc, zfree and opaque must be initialized before by the caller.
caller.
The method parameter is the compression method. It must be Z_DEFLATED in The method parameter is the compression method. It must be Z_DEFLATED in
this version of the library. this version of the library.
...@@ -712,11 +711,12 @@ ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, ...@@ -712,11 +711,12 @@ ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
used to switch between compression and straight copy of the input data, or used to switch between compression and straight copy of the input data, or
to switch to a different kind of input data requiring a different strategy. to switch to a different kind of input data requiring a different strategy.
If the compression approach (which is a function of the level) or the If the compression approach (which is a function of the level) or the
strategy is changed, and if any input has been consumed in a previous strategy is changed, and if there have been any deflate() calls since the
deflate() call, then the input available so far is compressed with the old state was initialized or reset, then the input available so far is
level and strategy using deflate(strm, Z_BLOCK). There are three approaches compressed with the old level and strategy using deflate(strm, Z_BLOCK).
for the compression levels 0, 1..3, and 4..9 respectively. The new level There are three approaches for the compression levels 0, 1..3, and 4..9
and strategy will take effect at the next call of deflate(). respectively. The new level and strategy will take effect at the next call
of deflate().
If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does
not have enough output space to complete, then the parameter change will not not have enough output space to complete, then the parameter change will not
...@@ -865,9 +865,11 @@ ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, ...@@ -865,9 +865,11 @@ ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
detection, or add 16 to decode only the gzip format (the zlib format will detection, or add 16 to decode only the gzip format (the zlib format will
return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a
CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see
below), inflate() will not automatically decode concatenated gzip streams. below), inflate() will *not* automatically decode concatenated gzip members.
inflate() will return Z_STREAM_END at the end of the gzip stream. The state inflate() will return Z_STREAM_END at the end of the gzip member. The state
would need to be reset to continue decoding a subsequent gzip stream. would need to be reset to continue decoding a subsequent gzip member. This
*must* be done if there is more data after a gzip member, in order for the
decompression to be compliant with the gzip standard (RFC 1952).
inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_VERSION_ERROR if the zlib library version is incompatible with the memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
...@@ -1302,14 +1304,14 @@ typedef struct gzFile_s *gzFile; /* semi-opaque gzip file descriptor */ ...@@ -1302,14 +1304,14 @@ typedef struct gzFile_s *gzFile; /* semi-opaque gzip file descriptor */
/* /*
ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
Opens a gzip (.gz) file for reading or writing. The mode parameter is as Open the gzip (.gz) file at path for reading and decompressing, or
in fopen ("rb" or "wb") but can also include a compression level ("wb9") or compressing and writing. The mode parameter is as in fopen ("rb" or "wb")
a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman-only but can also include a compression level ("wb9") or a strategy: 'f' for
compression as in "wb1h", 'R' for run-length encoding as in "wb1R", or 'F' filtered data as in "wb6f", 'h' for Huffman-only compression as in "wb1h",
for fixed code compression as in "wb9F". (See the description of 'R' for run-length encoding as in "wb1R", or 'F' for fixed code compression
deflateInit2 for more information about the strategy parameter.) 'T' will as in "wb9F". (See the description of deflateInit2 for more information
request transparent writing or appending with no compression and not using about the strategy parameter.) 'T' will request transparent writing or
the gzip format. appending with no compression and not using the gzip format.
"a" can be used instead of "w" to request that the gzip stream that will "a" can be used instead of "w" to request that the gzip stream that will
be written be appended to the file. "+" will result in an error, since be written be appended to the file. "+" will result in an error, since
...@@ -1339,9 +1341,9 @@ ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); ...@@ -1339,9 +1341,9 @@ ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
/* /*
gzdopen associates a gzFile with the file descriptor fd. File descriptors Associate a gzFile with the file descriptor fd. File descriptors are
are obtained from calls like open, dup, creat, pipe or fileno (if the file obtained from calls like open, dup, creat, pipe or fileno (if the file has
has been previously opened with fopen). The mode parameter is as in gzopen. been previously opened with fopen). The mode parameter is as in gzopen.
The next call of gzclose on the returned gzFile will also close the file The next call of gzclose on the returned gzFile will also close the file
descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor
...@@ -1362,13 +1364,13 @@ ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); ...@@ -1362,13 +1364,13 @@ ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size)); ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size));
/* /*
Set the internal buffer size used by this library's functions. The Set the internal buffer size used by this library's functions for file to
default buffer size is 8192 bytes. This function must be called after size. The default buffer size is 8192 bytes. This function must be called
gzopen() or gzdopen(), and before any other calls that read or write the after gzopen() or gzdopen(), and before any other calls that read or write
file. The buffer memory allocation is always deferred to the first read or the file. The buffer memory allocation is always deferred to the first read
write. Three times that size in buffer space is allocated. A larger buffer or write. Three times that size in buffer space is allocated. A larger
size of, for example, 64K or 128K bytes will noticeably increase the speed buffer size of, for example, 64K or 128K bytes will noticeably increase the
of decompression (reading). speed of decompression (reading).
The new buffer size also affects the maximum length for gzprintf(). The new buffer size also affects the maximum length for gzprintf().
...@@ -1378,9 +1380,9 @@ ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size)); ...@@ -1378,9 +1380,9 @@ ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size));
ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
/* /*
Dynamically update the compression level or strategy. See the description Dynamically update the compression level and strategy for file. See the
of deflateInit2 for the meaning of these parameters. Previously provided description of deflateInit2 for the meaning of these parameters. Previously
data is flushed before the parameter change. provided data is flushed before applying the parameter changes.
gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not
opened for writing, Z_ERRNO if there is an error writing the flushed data, opened for writing, Z_ERRNO if there is an error writing the flushed data,
...@@ -1389,7 +1391,7 @@ ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); ...@@ -1389,7 +1391,7 @@ ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
/* /*
Reads the given number of uncompressed bytes from the compressed file. If Read and decompress up to len uncompressed bytes from file into buf. If
the input file is not in gzip format, gzread copies the given number of the input file is not in gzip format, gzread copies the given number of
bytes into the buffer directly from the file. bytes into the buffer directly from the file.
...@@ -1420,11 +1422,11 @@ ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); ...@@ -1420,11 +1422,11 @@ ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
ZEXTERN z_size_t ZEXPORT gzfread OF((voidp buf, z_size_t size, z_size_t nitems, ZEXTERN z_size_t ZEXPORT gzfread OF((voidp buf, z_size_t size, z_size_t nitems,
gzFile file)); gzFile file));
/* /*
Read up to nitems items of size size from file to buf, otherwise operating Read and decompress up to nitems items of size size from file into buf,
as gzread() does. This duplicates the interface of stdio's fread(), with otherwise operating as gzread() does. This duplicates the interface of
size_t request and return types. If the library defines size_t, then stdio's fread(), with size_t request and return types. If the library
z_size_t is identical to size_t. If not, then z_size_t is an unsigned defines size_t, then z_size_t is identical to size_t. If not, then z_size_t
integer type that can contain a pointer. is an unsigned integer type that can contain a pointer.
gzfread() returns the number of full items read of size size, or zero if gzfread() returns the number of full items read of size size, or zero if
the end of the file was reached and a full item could not be read, or if the end of the file was reached and a full item could not be read, or if
...@@ -1443,18 +1445,16 @@ ZEXTERN z_size_t ZEXPORT gzfread OF((voidp buf, z_size_t size, z_size_t nitems, ...@@ -1443,18 +1445,16 @@ ZEXTERN z_size_t ZEXPORT gzfread OF((voidp buf, z_size_t size, z_size_t nitems,
file, reseting and retrying on end-of-file, when size is not 1. file, reseting and retrying on end-of-file, when size is not 1.
*/ */
ZEXTERN int ZEXPORT gzwrite OF((gzFile file, ZEXTERN int ZEXPORT gzwrite OF((gzFile file, voidpc buf, unsigned len));
voidpc buf, unsigned len));
/* /*
Writes the given number of uncompressed bytes into the compressed file. Compress and write the len uncompressed bytes at buf to file. gzwrite
gzwrite returns the number of uncompressed bytes written or 0 in case of returns the number of uncompressed bytes written or 0 in case of error.
error.
*/ */
ZEXTERN z_size_t ZEXPORT gzfwrite OF((voidpc buf, z_size_t size, ZEXTERN z_size_t ZEXPORT gzfwrite OF((voidpc buf, z_size_t size,
z_size_t nitems, gzFile file)); z_size_t nitems, gzFile file));
/* /*
gzfwrite() writes nitems items of size size from buf to file, duplicating Compress and write nitems items of size size from buf to file, duplicating
the interface of stdio's fwrite(), with size_t request and return types. If the interface of stdio's fwrite(), with size_t request and return types. If
the library defines size_t, then z_size_t is identical to size_t. If not, the library defines size_t, then z_size_t is identical to size_t. If not,
then z_size_t is an unsigned integer type that can contain a pointer. then z_size_t is an unsigned integer type that can contain a pointer.
...@@ -1467,22 +1467,22 @@ ZEXTERN z_size_t ZEXPORT gzfwrite OF((voidpc buf, z_size_t size, ...@@ -1467,22 +1467,22 @@ ZEXTERN z_size_t ZEXPORT gzfwrite OF((voidpc buf, z_size_t size,
ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...)); ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...));
/* /*
Converts, formats, and writes the arguments to the compressed file under Convert, format, compress, and write the arguments (...) to file under
control of the format string, as in fprintf. gzprintf returns the number of control of the string format, as in fprintf. gzprintf returns the number of
uncompressed bytes actually written, or a negative zlib error code in case uncompressed bytes actually written, or a negative zlib error code in case
of error. The number of uncompressed bytes written is limited to 8191, or of error. The number of uncompressed bytes written is limited to 8191, or
one less than the buffer size given to gzbuffer(). The caller should assure one less than the buffer size given to gzbuffer(). The caller should assure
that this limit is not exceeded. If it is exceeded, then gzprintf() will that this limit is not exceeded. If it is exceeded, then gzprintf() will
return an error (0) with nothing written. In this case, there may also be a return an error (0) with nothing written. In this case, there may also be a
buffer overflow with unpredictable consequences, which is possible only if buffer overflow with unpredictable consequences, which is possible only if
zlib was compiled with the insecure functions sprintf() or vsprintf() zlib was compiled with the insecure functions sprintf() or vsprintf(),
because the secure snprintf() or vsnprintf() functions were not available. because the secure snprintf() or vsnprintf() functions were not available.
This can be determined using zlibCompileFlags(). This can be determined using zlibCompileFlags().
*/ */
ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
/* /*
Writes the given null-terminated string to the compressed file, excluding Compress and write the given null-terminated string s to file, excluding
the terminating null character. the terminating null character.
gzputs returns the number of characters written, or -1 in case of error. gzputs returns the number of characters written, or -1 in case of error.
...@@ -1490,11 +1490,12 @@ ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); ...@@ -1490,11 +1490,12 @@ ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
/* /*
Reads bytes from the compressed file until len-1 characters are read, or a Read and decompress bytes from file into buf, until len-1 characters are
newline character is read and transferred to buf, or an end-of-file read, or until a newline character is read and transferred to buf, or an
condition is encountered. If any characters are read or if len == 1, the end-of-file condition is encountered. If any characters are read or if len
string is terminated with a null character. If no characters are read due is one, the string is terminated with a null character. If no characters
to an end-of-file or len < 1, then the buffer is left untouched. are read due to an end-of-file or len is less than one, then the buffer is
left untouched.
gzgets returns buf which is a null-terminated string, or it returns NULL gzgets returns buf which is a null-terminated string, or it returns NULL
for end-of-file or in case of error. If there was an error, the contents at for end-of-file or in case of error. If there was an error, the contents at
...@@ -1503,13 +1504,13 @@ ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); ...@@ -1503,13 +1504,13 @@ ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
/* /*
Writes c, converted to an unsigned char, into the compressed file. gzputc Compress and write c, converted to an unsigned char, into file. gzputc
returns the value that was written, or -1 in case of error. returns the value that was written, or -1 in case of error.
*/ */
ZEXTERN int ZEXPORT gzgetc OF((gzFile file)); ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
/* /*
Reads one byte from the compressed file. gzgetc returns this byte or -1 Read and decompress one byte from file. gzgetc returns this byte or -1
in case of end of file or error. This is implemented as a macro for speed. in case of end of file or error. This is implemented as a macro for speed.
As such, it does not do all of the checking the other functions do. I.e. As such, it does not do all of the checking the other functions do. I.e.
it does not check to see if file is NULL, nor whether the structure file it does not check to see if file is NULL, nor whether the structure file
...@@ -1518,8 +1519,8 @@ ZEXTERN int ZEXPORT gzgetc OF((gzFile file)); ...@@ -1518,8 +1519,8 @@ ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file));
/* /*
Push one character back onto the stream to be read as the first character Push c back onto the stream for file to be read as the first character on
on the next read. At least one character of push-back is allowed. the next read. At least one character of push-back is always allowed.
gzungetc() returns the character pushed, or -1 on failure. gzungetc() will gzungetc() returns the character pushed, or -1 on failure. gzungetc() will
fail if c is -1, and may fail if a character has been pushed but not read fail if c is -1, and may fail if a character has been pushed but not read
yet. If gzungetc is used immediately after gzopen or gzdopen, at least the yet. If gzungetc is used immediately after gzopen or gzdopen, at least the
...@@ -1530,9 +1531,9 @@ ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); ...@@ -1530,9 +1531,9 @@ ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file));
ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
/* /*
Flushes all pending output into the compressed file. The parameter flush Flush all pending output to file. The parameter flush is as in the
is as in the deflate() function. The return value is the zlib error number deflate() function. The return value is the zlib error number (see function
(see function gzerror below). gzflush is only permitted when writing. gzerror below). gzflush is only permitted when writing.
If the flush parameter is Z_FINISH, the remaining data is written and the If the flush parameter is Z_FINISH, the remaining data is written and the
gzip stream is completed in the output. If gzwrite() is called again, a new gzip stream is completed in the output. If gzwrite() is called again, a new
...@@ -1547,8 +1548,8 @@ ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); ...@@ -1547,8 +1548,8 @@ ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
z_off_t offset, int whence)); z_off_t offset, int whence));
Sets the starting position for the next gzread or gzwrite on the given Set the starting position to offset relative to whence for the next gzread
compressed file. The offset represents a number of bytes in the or gzwrite on file. The offset represents a number of bytes in the
uncompressed data stream. The whence parameter is defined as in lseek(2); uncompressed data stream. The whence parameter is defined as in lseek(2);
the value SEEK_END is not supported. the value SEEK_END is not supported.
...@@ -1565,18 +1566,18 @@ ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, ...@@ -1565,18 +1566,18 @@ ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
ZEXTERN int ZEXPORT gzrewind OF((gzFile file)); ZEXTERN int ZEXPORT gzrewind OF((gzFile file));
/* /*
Rewinds the given file. This function is supported only for reading. Rewind file. This function is supported only for reading.
gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET).
*/ */
/* /*
ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file));
Returns the starting position for the next gzread or gzwrite on the given Return the starting position for the next gzread or gzwrite on file.
compressed file. This position represents a number of bytes in the This position represents a number of bytes in the uncompressed data stream,
uncompressed data stream, and is zero when starting, even if appending or and is zero when starting, even if appending or reading a gzip stream from
reading a gzip stream from the middle of a file using gzdopen(). the middle of a file using gzdopen().
gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
*/ */
...@@ -1584,22 +1585,22 @@ ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); ...@@ -1584,22 +1585,22 @@ ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file));
/* /*
ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file)); ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file));
Returns the current offset in the file being read or written. This offset Return the current compressed (actual) read or write offset of file. This
includes the count of bytes that precede the gzip stream, for example when offset includes the count of bytes that precede the gzip stream, for example
appending or when using gzdopen() for reading. When reading, the offset when appending or when using gzdopen() for reading. When reading, the
does not include as yet unused buffered input. This information can be used offset does not include as yet unused buffered input. This information can
for a progress indicator. On error, gzoffset() returns -1. be used for a progress indicator. On error, gzoffset() returns -1.
*/ */
ZEXTERN int ZEXPORT gzeof OF((gzFile file)); ZEXTERN int ZEXPORT gzeof OF((gzFile file));
/* /*
Returns true (1) if the end-of-file indicator has been set while reading, Return true (1) if the end-of-file indicator for file has been set while
false (0) otherwise. Note that the end-of-file indicator is set only if the reading, false (0) otherwise. Note that the end-of-file indicator is set
read tried to go past the end of the input, but came up short. Therefore, only if the read tried to go past the end of the input, but came up short.
just like feof(), gzeof() may return false even if there is no more data to Therefore, just like feof(), gzeof() may return false even if there is no
read, in the event that the last read request was for the exact number of more data to read, in the event that the last read request was for the exact
bytes remaining in the input file. This will happen if the input file size number of bytes remaining in the input file. This will happen if the input
is an exact multiple of the buffer size. file size is an exact multiple of the buffer size.
If gzeof() returns true, then the read functions will return no more data, If gzeof() returns true, then the read functions will return no more data,
unless the end-of-file indicator is reset by gzclearerr() and the input file unless the end-of-file indicator is reset by gzclearerr() and the input file
...@@ -1608,7 +1609,7 @@ ZEXTERN int ZEXPORT gzeof OF((gzFile file)); ...@@ -1608,7 +1609,7 @@ ZEXTERN int ZEXPORT gzeof OF((gzFile file));
ZEXTERN int ZEXPORT gzdirect OF((gzFile file)); ZEXTERN int ZEXPORT gzdirect OF((gzFile file));
/* /*
Returns true (1) if file is being copied directly while reading, or false Return true (1) if file is being copied directly while reading, or false
(0) if file is a gzip stream being decompressed. (0) if file is a gzip stream being decompressed.
If the input file is empty, gzdirect() will return true, since the input If the input file is empty, gzdirect() will return true, since the input
...@@ -1629,8 +1630,8 @@ ZEXTERN int ZEXPORT gzdirect OF((gzFile file)); ...@@ -1629,8 +1630,8 @@ ZEXTERN int ZEXPORT gzdirect OF((gzFile file));
ZEXTERN int ZEXPORT gzclose OF((gzFile file)); ZEXTERN int ZEXPORT gzclose OF((gzFile file));
/* /*
Flushes all pending output if necessary, closes the compressed file and Flush all pending output for file, if necessary, close file and
deallocates the (de)compression state. Note that once file is closed, you deallocate the (de)compression state. Note that once file is closed, you
cannot call gzerror with file, since its structures have been deallocated. cannot call gzerror with file, since its structures have been deallocated.
gzclose must not be called more than once on the same file, just as free gzclose must not be called more than once on the same file, just as free
must not be called more than once on the same allocation. must not be called more than once on the same allocation.
...@@ -1654,10 +1655,10 @@ ZEXTERN int ZEXPORT gzclose_w OF((gzFile file)); ...@@ -1654,10 +1655,10 @@ ZEXTERN int ZEXPORT gzclose_w OF((gzFile file));
ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
/* /*
Returns the error message for the last error which occurred on the given Return the error message for the last error which occurred on file.
compressed file. errnum is set to zlib error number. If an error occurred errnum is set to zlib error number. If an error occurred in the file system
in the file system and not in the compression library, errnum is set to and not in the compression library, errnum is set to Z_ERRNO and the
Z_ERRNO and the application may consult errno to get the exact error code. application may consult errno to get the exact error code.
The application must not modify the returned string. Future calls to The application must not modify the returned string. Future calls to
this function may invalidate the previously returned string. If file is this function may invalidate the previously returned string. If file is
...@@ -1670,7 +1671,7 @@ ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); ...@@ -1670,7 +1671,7 @@ ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
ZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));
/* /*
Clears the error and end-of-file flags for file. This is analogous to the Clear the error and end-of-file flags for file. This is analogous to the
clearerr() function in stdio. This is useful for continuing to read a gzip clearerr() function in stdio. This is useful for continuing to read a gzip
file that is being written concurrently. file that is being written concurrently.
*/ */
...@@ -1688,8 +1689,9 @@ ZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); ...@@ -1688,8 +1689,9 @@ ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));
ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
/* /*
Update a running Adler-32 checksum with the bytes buf[0..len-1] and Update a running Adler-32 checksum with the bytes buf[0..len-1] and
return the updated checksum. If buf is Z_NULL, this function returns the return the updated checksum. An Adler-32 value is in the range of a 32-bit
required initial value for the checksum. unsigned integer. If buf is Z_NULL, this function returns the required
initial value for the checksum.
An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed
much faster. much faster.
...@@ -1725,9 +1727,10 @@ ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2, ...@@ -1725,9 +1727,10 @@ ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2,
ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
/* /*
Update a running CRC-32 with the bytes buf[0..len-1] and return the Update a running CRC-32 with the bytes buf[0..len-1] and return the
updated CRC-32. If buf is Z_NULL, this function returns the required updated CRC-32. A CRC-32 value is in the range of a 32-bit unsigned integer.
initial value for the crc. Pre- and post-conditioning (one's complement) is If buf is Z_NULL, this function returns the required initial value for the
performed within this function so it shouldn't be done by the application. crc. Pre- and post-conditioning (one's complement) is performed within this
function so it shouldn't be done by the application.
Usage example: Usage example:
...@@ -1739,7 +1742,7 @@ ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); ...@@ -1739,7 +1742,7 @@ ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
if (crc != original_crc) error(); if (crc != original_crc) error();
*/ */
ZEXTERN uLong ZEXPORT crc32_z OF((uLong adler, const Bytef *buf, ZEXTERN uLong ZEXPORT crc32_z OF((uLong crc, const Bytef *buf,
z_size_t len)); z_size_t len));
/* /*
Same as crc32(), but with a size_t length. Same as crc32(), but with a size_t length.
...@@ -1755,6 +1758,20 @@ ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2)); ...@@ -1755,6 +1758,20 @@ ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));
len2. len2.
*/ */
/*
ZEXTERN uLong ZEXPORT crc32_combine_gen OF((z_off_t len2));
Return the operator corresponding to length len2, to be used with
crc32_combine_op().
*/
ZEXTERN uLong ZEXPORT crc32_combine_op OF((uLong crc1, uLong crc2, uLong op));
/*
Give the same result as crc32_combine(), using op in place of len2. op is
is generated from len2 by crc32_combine_gen(). This will be faster than
crc32_combine() if the generated op is used more than once.
*/
/* various hacks, don't look :) */ /* various hacks, don't look :) */
...@@ -1842,6 +1859,7 @@ ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */ ...@@ -1842,6 +1859,7 @@ ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */
ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t)); ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t));
ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t)); ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t));
ZEXTERN uLong ZEXPORT crc32_combine_gen64 OF((z_off64_t));
#endif #endif
#if !defined(ZLIB_INTERNAL) && defined(Z_WANT64) #if !defined(ZLIB_INTERNAL) && defined(Z_WANT64)
...@@ -1852,6 +1870,7 @@ ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */ ...@@ -1852,6 +1870,7 @@ ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */
# define z_gzoffset z_gzoffset64 # define z_gzoffset z_gzoffset64
# define z_adler32_combine z_adler32_combine64 # define z_adler32_combine z_adler32_combine64
# define z_crc32_combine z_crc32_combine64 # define z_crc32_combine z_crc32_combine64
# define z_crc32_combine_gen z_crc32_combine_gen64
# else # else
# define gzopen gzopen64 # define gzopen gzopen64
# define gzseek gzseek64 # define gzseek gzseek64
...@@ -1859,6 +1878,7 @@ ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */ ...@@ -1859,6 +1878,7 @@ ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */
# define gzoffset gzoffset64 # define gzoffset gzoffset64
# define adler32_combine adler32_combine64 # define adler32_combine adler32_combine64
# define crc32_combine crc32_combine64 # define crc32_combine crc32_combine64
# define crc32_combine_gen crc32_combine_gen64
# endif # endif
# ifndef Z_LARGE64 # ifndef Z_LARGE64
ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
...@@ -1867,6 +1887,7 @@ ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */ ...@@ -1867,6 +1887,7 @@ ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */
ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile)); ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile));
ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
ZEXTERN uLong ZEXPORT crc32_combine_gen64 OF((z_off_t));
# endif # endif
#else #else
ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *)); ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *));
...@@ -1875,12 +1896,14 @@ ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */ ...@@ -1875,12 +1896,14 @@ ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */
ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile)); ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile));
ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
ZEXTERN uLong ZEXPORT crc32_combine_gen OF((z_off_t));
#endif #endif
#else /* Z_SOLO */ #else /* Z_SOLO */
ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
ZEXTERN uLong ZEXPORT crc32_combine_gen OF((z_off_t));
#endif /* !Z_SOLO */ #endif /* !Z_SOLO */
...@@ -1893,7 +1916,7 @@ ZEXTERN int ZEXPORT inflateValidate OF((z_streamp, int)); ...@@ -1893,7 +1916,7 @@ ZEXTERN int ZEXPORT inflateValidate OF((z_streamp, int));
ZEXTERN unsigned long ZEXPORT inflateCodesUsed OF ((z_streamp)); ZEXTERN unsigned long ZEXPORT inflateCodesUsed OF ((z_streamp));
ZEXTERN int ZEXPORT inflateResetKeep OF((z_streamp)); ZEXTERN int ZEXPORT inflateResetKeep OF((z_streamp));
ZEXTERN int ZEXPORT deflateResetKeep OF((z_streamp)); ZEXTERN int ZEXPORT deflateResetKeep OF((z_streamp));
#if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(Z_SOLO) #if defined(_WIN32) && !defined(Z_SOLO)
ZEXTERN gzFile ZEXPORT gzopen_w OF((const wchar_t *path, ZEXTERN gzFile ZEXPORT gzopen_w OF((const wchar_t *path,
const char *mode)); const char *mode));
#endif #endif
......
...@@ -136,8 +136,8 @@ const char * ZEXPORT zError(err) ...@@ -136,8 +136,8 @@ const char * ZEXPORT zError(err)
return ERR_MSG(err); return ERR_MSG(err);
} }
#if defined(_WIN32_WCE) #if defined(_WIN32_WCE) && _WIN32_WCE < 0x800
/* The Microsoft C Run-Time Library for Windows CE doesn't have /* The older Microsoft C Run-Time Library for Windows CE doesn't have
* errno. We define it as a global variable to simplify porting. * errno. We define it as a global variable to simplify porting.
* Its value is always 0 and should not be used. * Its value is always 0 and should not be used.
*/ */
......
/* zutil.h -- internal interface and configuration of the compression library /* zutil.h -- internal interface and configuration of the compression library
* Copyright (C) 1995-2016 Jean-loup Gailly, Mark Adler * Copyright (C) 1995-2022 Jean-loup Gailly, Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h * For conditions of distribution and use, see copyright notice in zlib.h
*/ */
...@@ -29,10 +29,6 @@ ...@@ -29,10 +29,6 @@
# include <stdlib.h> # include <stdlib.h>
#endif #endif
#ifdef Z_SOLO
typedef long ptrdiff_t; /* guess -- will be caught if guess is wrong */
#endif
#ifndef local #ifndef local
# define local static # define local static
#endif #endif
...@@ -46,6 +42,17 @@ typedef unsigned short ush; ...@@ -46,6 +42,17 @@ typedef unsigned short ush;
typedef ush FAR ushf; typedef ush FAR ushf;
typedef unsigned long ulg; typedef unsigned long ulg;
#if !defined(Z_U8) && !defined(Z_SOLO) && defined(STDC)
# include <limits.h>
# if (ULONG_MAX == 0xffffffffffffffff)
# define Z_U8 unsigned long
# elif (ULLONG_MAX == 0xffffffffffffffff)
# define Z_U8 unsigned long long
# elif (UINT_MAX == 0xffffffffffffffff)
# define Z_U8 unsigned
# endif
#endif
extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
/* (size given to avoid silly warnings with Visual C++) */ /* (size given to avoid silly warnings with Visual C++) */
...@@ -170,10 +177,6 @@ extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ ...@@ -170,10 +177,6 @@ extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
#if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX #if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX
# if defined(_WIN32_WCE) # if defined(_WIN32_WCE)
# define fdopen(fd,mode) NULL /* No fdopen() */ # define fdopen(fd,mode) NULL /* No fdopen() */
# ifndef _PTRDIFF_T_DEFINED
typedef int ptrdiff_t;
# define _PTRDIFF_T_DEFINED
# endif
# else # else
# define fdopen(fd,type) _fdopen(fd,type) # define fdopen(fd,type) _fdopen(fd,type)
# endif # endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment