[PATCH 07/21] lib/zlib: update deflate to zlib 1.2.0.4 release
From: Greg Kroah-Hartman
Date: Thu Dec 03 2026 - 02:31:13 EST
Sync the kernel deflate code with the zlib 1.2.0.4 release.
This involves:
- generalizing the noheader flag into a wrap mode (0 = raw, 1 = zlib,
2 = gzip)
- rename deflate_state.noheader to wrap and convert all users. A
finished stream now stores the negated wrap value instead of -1 so
deflateReset() can restore it.
- restructure the header and trailer emission in zlib_deflate() along
the new wrap values; a raw stream finishing with Z_FINISH returns
Z_STREAM_END directly as upstream does (any pending output has
already been flushed on the finish path or deflate would have
returned Z_OK earlier).
- carry the upstream gzip wrapper support as preprocessed-out "#ifdef
GZIP" sections. The kernel copy never defines GZIP (that would need
crc32() wired up and no kernel user produces gzip through
zlib_deflate); a note in defutil.h documents this as the upstream
NO_GZIP configuration
- comment wording updates
The kernel is now in sync with zlib commit 086e982175da ("zlib 1.2.0.4").
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
include/linux/zlib.h | 2 +-
lib/zlib_deflate/deflate.c | 133 ++++++++++++++++++++++++++++++---------------
lib/zlib_deflate/defutil.h | 9 ++-
3 files changed, 97 insertions(+), 47 deletions(-)
diff --git a/include/linux/zlib.h b/include/linux/zlib.h
index dce6ec967576..fd34fd3ee715 100644
--- a/include/linux/zlib.h
+++ b/include/linux/zlib.h
@@ -32,7 +32,7 @@
#include <linux/zconf.h>
-/* zlib deflate based on ZLIB_VERSION "1.2.0.3" */
+/* zlib deflate based on ZLIB_VERSION "1.2.0.4" */
/* zlib inflate based on ZLIB_VERSION "1.2.3" */
/*
diff --git a/lib/zlib_deflate/deflate.c b/lib/zlib_deflate/deflate.c
index 66b4bfd260a8..7cc54a3551d7 100644
--- a/lib/zlib_deflate/deflate.c
+++ b/lib/zlib_deflate/deflate.c
@@ -136,7 +136,7 @@ typedef struct config_s {
static const config configuration_table[10] = {
/* good lazy nice chain */
/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
-/* 1 */ {4, 4, 8, 4, deflate_fast}, /* maximum speed, no lazy matches */
+/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
/* 2 */ {4, 5, 16, 8, deflate_fast},
/* 3 */ {4, 6, 32, 32, deflate_fast},
@@ -145,7 +145,7 @@ static const config configuration_table[10] = {
/* 6 */ {8, 16, 128, 128, deflate_slow},
/* 7 */ {8, 32, 128, 256, deflate_slow},
/* 8 */ {32, 128, 258, 1024, deflate_slow},
-/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
+/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
* For deflate_fast() (levels <= 3) good is ignored and lazy has a different
@@ -193,7 +193,7 @@ int zlib_deflateInit2(
)
{
deflate_state *s;
- int noheader = 0;
+ int wrap = 1;
deflate_workspace *mem;
char *next;
@@ -210,10 +210,16 @@ int zlib_deflateInit2(
mem = (deflate_workspace *) strm->workspace;
- if (windowBits < 0) { /* undocumented feature: suppress zlib header */
- noheader = 1;
+ if (windowBits < 0) { /* suppress zlib wrapper */
+ wrap = 0;
windowBits = -windowBits;
}
+#ifdef GZIP
+ else if (windowBits > 15) {
+ wrap = 2; /* write gzip wrapper instead */
+ windowBits -= 16;
+ }
+#endif
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
strategy < 0 || strategy > Z_RLE) {
@@ -247,7 +253,7 @@ int zlib_deflateInit2(
strm->state = (struct internal_state *)s;
s->strm = strm;
- s->noheader = noheader;
+ s->wrap = wrap;
s->w_bits = windowBits;
s->w_size = 1 << s->w_bits;
s->w_mask = s->w_size - 1;
@@ -295,11 +301,15 @@ int zlib_deflateReset(
s->pending = 0;
s->pending_out = s->pending_buf;
- if (s->noheader < 0) {
- s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
+ if (s->wrap < 0) {
+ s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
}
- s->status = s->noheader ? BUSY_STATE : INIT_STATE;
- strm->adler = 1;
+ s->status = s->wrap ? INIT_STATE : BUSY_STATE;
+ strm->adler =
+#ifdef GZIP
+ s->wrap == 2 ? crc32(0L, NULL, 0) :
+#endif
+ zlib_adler32(0L, NULL, 0);
s->last_flush = Z_NO_FLUSH;
zlib_tr_init(s);
@@ -349,33 +359,53 @@ int zlib_deflate(
old_flush = s->last_flush;
s->last_flush = flush;
- /* Write the zlib header */
+ /* Write the header */
if (s->status == INIT_STATE) {
-
- uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
- uInt level_flags;
+#ifdef GZIP
+ if (s->wrap == 2) {
+ put_byte(s, 31);
+ put_byte(s, 139);
+ put_byte(s, 8);
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, s->level == 9 ? 2 :
+ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
+ 4 : 0));
+ put_byte(s, 255);
+ s->status = BUSY_STATE;
+ strm->adler = crc32(0L, NULL, 0);
+ }
+ else
+#endif
+ {
+ uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
+ uInt level_flags;
- if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
- level_flags = 0;
- else if (s->level < 6)
- level_flags = 1;
- else if (s->level == 6)
- level_flags = 2;
- else
- level_flags = 3;
- header |= (level_flags << 6);
- if (s->strstart != 0) header |= PRESET_DICT;
- header += 31 - (header % 31);
+ if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
+ level_flags = 0;
+ else if (s->level < 6)
+ level_flags = 1;
+ else if (s->level == 6)
+ level_flags = 2;
+ else
+ level_flags = 3;
+ header |= (level_flags << 6);
+ if (s->strstart != 0) header |= PRESET_DICT;
+ header += 31 - (header % 31);
- s->status = BUSY_STATE;
- putShortMSB(s, header);
+ s->status = BUSY_STATE;
+ putShortMSB(s, header);
- /* Save the adler32 of the preset dictionary: */
- if (s->strstart != 0) {
- putShortMSB(s, (uInt)(strm->adler >> 16));
- putShortMSB(s, (uInt)(strm->adler & 0xffff));
+ /* Save the adler32 of the preset dictionary: */
+ if (s->strstart != 0) {
+ putShortMSB(s, (uInt)(strm->adler >> 16));
+ putShortMSB(s, (uInt)(strm->adler & 0xffff));
+ }
+ strm->adler = zlib_adler32(0L, NULL, 0);
}
- strm->adler = 1L;
}
/* Flush as much pending output as possible */
@@ -457,9 +487,23 @@ int zlib_deflate(
Assert(strm->avail_out > 0, "bug2");
if (flush != Z_FINISH) return Z_OK;
+ if (s->wrap <= 0) return Z_STREAM_END;
- if (!s->noheader) {
- /* Write zlib trailer (adler32) */
+ /* Write the trailer */
+#ifdef GZIP
+ if (s->wrap == 2) {
+ put_byte(s, (Byte)(strm->adler & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
+ put_byte(s, (Byte)(strm->total_in & 0xff));
+ put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
+ put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
+ put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
+ }
+ else
+#endif
+ {
putShortMSB(s, (uInt)(strm->adler >> 16));
putShortMSB(s, (uInt)(strm->adler & 0xffff));
}
@@ -467,14 +511,8 @@ int zlib_deflate(
/* If avail_out is zero, the application will call deflate again
* to flush the rest.
*/
- if (!s->noheader) {
- s->noheader = -1; /* write the trailer only once! */
- }
- if (s->pending == 0) {
- Assert(s->bi_valid == 0, "bi_buf not flushed");
- return Z_STREAM_END;
- }
- return Z_OK;
+ if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
+ return s->pending != 0 ? Z_OK : Z_STREAM_END;
}
/* ========================================================================= */
@@ -520,9 +558,14 @@ static int read_buf(
strm->avail_in -= len;
if (!DEFLATE_NEED_CHECKSUM(strm)) {}
- else if (!((deflate_state *)(strm->state))->noheader) {
+ else if (((deflate_state *)(strm->state))->wrap == 1) {
strm->adler = zlib_adler32(strm->adler, strm->next_in, len);
}
+#ifdef GZIP
+ else if (((deflate_state *)(strm->state))->wrap == 2) {
+ strm->adler = crc32(strm->adler, strm->next_in, len);
+ }
+#endif
memcpy(buf, strm->next_in, len);
strm->next_in += len;
strm->total_in += len;
@@ -822,7 +865,7 @@ static void fill_window(
} else if (more == (unsigned)(-1)) {
/* Very unlikely, but possible on 16 bit machine if
- * strstart == 0 && lookahead == 1 (input done one byte at time)
+ * strstart == 0 && lookahead == 1 (input done a byte at time)
*/
more--;
}
@@ -1042,7 +1085,7 @@ static block_state deflate_fast(
*/
if (s->match_length <= s->max_insert_length &&
s->lookahead >= MIN_MATCH) {
- s->match_length--; /* string at strstart already in hash table */
+ s->match_length--; /* string at strstart already in table */
do {
s->strstart++;
INSERT_STRING(s, s->strstart, hash_head);
diff --git a/lib/zlib_deflate/defutil.h b/lib/zlib_deflate/defutil.h
index 0614141d4cda..dec377b7d25c 100644
--- a/lib/zlib_deflate/defutil.h
+++ b/lib/zlib_deflate/defutil.h
@@ -3,6 +3,13 @@
#include <linux/zutil.h>
+/* define NO_GZIP when compiling if you want to disable gzip header and
+ trailer creation by deflate(). NO_GZIP would be used to avoid linking in
+ the crc code when it is not needed. For shared libraries, gzip encoding
+ should be left enabled.
+ The kernel copy does not provide crc32() to this code, so GZIP is never
+ defined here: the kernel always builds the NO_GZIP variant. */
+
#define Assert(err, str)
#define Trace(dummy)
#define Tracev(dummy)
@@ -77,7 +84,7 @@ typedef struct deflate_state {
ulg pending_buf_size; /* size of pending_buf */
Byte *pending_out; /* next pending byte to output to the stream */
int pending; /* nb of bytes in the pending buffer */
- int noheader; /* suppress zlib header and adler32 */
+ int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
Byte data_type; /* UNKNOWN, BINARY or ASCII */
Byte method; /* STORED (for zip only) or DEFLATED */
int last_flush; /* value of flush param for previous deflate call */
--
2.56.1