[PATCH 20/21] lib/zlib: update deflate to zlib 1.2.2.4 release

From: Greg Kroah-Hartman

Date: Thu Dec 03 2026 - 02:32:25 EST


Bring the kernel deflate code up to zlib 1.2.2.4:

This involves:
- make deflate_state.pending unsigned (uInt), it's a byte count that
is compared against unsigned sizes everywhere.
- cast tree[m].Len explicitly in the gen_bitlen() comparison
- copy upstream's #if 0 draft of deflate_rle() (enabled in a later
release) with kernel-style tally calls
- fill_window() %%% marker comment

Changes that were not relevant for our copy were the gzip header
put_byte casts and gzindex type change as they are in code that the
kernel didn't ever copy.

The kernel is now in sync with zlib commit 9c3a5830218c ("zlib 1.2.2.4").

Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
include/linux/zlib.h | 2 +-
lib/zlib_deflate/deflate.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++
lib/zlib_deflate/deftree.c | 4 +--
lib/zlib_deflate/defutil.h | 2 +-
4 files changed, 67 insertions(+), 4 deletions(-)

diff --git a/include/linux/zlib.h b/include/linux/zlib.h
index 2220e8f4bc12..5ceb476f959c 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.2.3" */
+/* zlib deflate based on ZLIB_VERSION "1.2.2.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 b2c8860d6591..26b934eb84d3 100644
--- a/lib/zlib_deflate/deflate.c
+++ b/lib/zlib_deflate/deflate.c
@@ -893,6 +893,7 @@ static void fill_window(
later. (Using level 0 permanently is not an optimal usage of
zlib, so we don't care about this pathological case.)
*/
+ /* %%% avoid this when Z_RLE */
n = s->hash_size;
p = &s->head[n];
do {
@@ -1251,6 +1252,68 @@ static block_state deflate_slow(
return flush == Z_FINISH ? finish_done : block_done;
}

+#if 0
+/* ===========================================================================
+ * For Z_RLE, simply look for runs of bytes, generate matches only of distance
+ * one. Do not maintain a hash table. (It will be regenerated if this run of
+ * deflate switches away from Z_RLE.)
+ */
+static block_state deflate_rle(
+ deflate_state *s,
+ int flush
+)
+{
+ int bflush; /* set if current block must be flushed */
+ uInt run; /* length of run */
+ uInt max; /* maximum length of run */
+ uInt prev; /* byte at distance one to match */
+ Byte *scan; /* scan for end of run */
+
+ for (;;) {
+ /* Make sure that we always have enough lookahead, except
+ * at the end of the input file. We need MAX_MATCH bytes
+ * for the longest encodable run.
+ */
+ if (s->lookahead < MAX_MATCH) {
+ fill_window(s);
+ if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
+ return need_more;
+ }
+ if (s->lookahead == 0) break; /* flush the current block */
+ }
+
+ /* See how many times the previous byte repeats */
+ run = 0;
+ if (s->strstart > 0) { /* if there is a previous byte, that is */
+ max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
+ scan = s->window + s->strstart - 1;
+ prev = *scan++;
+ do {
+ if (*scan++ != prev)
+ break;
+ } while (++run < max);
+ }
+
+ /* Emit match if have run of MIN_MATCH or longer, else emit literal */
+ if (run >= MIN_MATCH) {
+ check_match(s, s->strstart, s->strstart - 1, run);
+ bflush = zlib_tr_tally(s, 1, run - MIN_MATCH);
+ s->lookahead -= run;
+ s->strstart += run;
+ } else {
+ /* No match, output a literal byte */
+ Tracevv((stderr,"%c", s->window[s->strstart]));
+ bflush = zlib_tr_tally (s, 0, s->window[s->strstart]);
+ s->lookahead--;
+ s->strstart++;
+ }
+ if (bflush) FLUSH_BLOCK(s, 0);
+ }
+ FLUSH_BLOCK(s, flush == Z_FINISH);
+ return flush == Z_FINISH ? finish_done : block_done;
+}
+#endif
+
int zlib_deflate_workspacesize(int windowBits, int memLevel)
{
if (windowBits < 0) /* undocumented feature: suppress zlib header */
diff --git a/lib/zlib_deflate/deftree.c b/lib/zlib_deflate/deftree.c
index b98feac2d498..2ae58d219155 100644
--- a/lib/zlib_deflate/deftree.c
+++ b/lib/zlib_deflate/deftree.c
@@ -1,6 +1,6 @@
/* +++ trees.c */
/* trees.c -- output deflated data using Huffman coding
- * Copyright (C) 1995-2004 Jean-loup Gailly
+ * Copyright (C) 1995-2005 Jean-loup Gailly
* For conditions of distribution and use, see copyright notice in zlib.h
*/

@@ -418,7 +418,7 @@ static void gen_bitlen(
while (n != 0) {
m = s->heap[--h];
if (m > max_code) continue;
- if (tree[m].Len != (unsigned) bits) {
+ if ((unsigned) tree[m].Len != (unsigned) bits) {
Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
s->opt_len += ((long)bits - (long)tree[m].Len)
*(long)tree[m].Freq;
diff --git a/lib/zlib_deflate/defutil.h b/lib/zlib_deflate/defutil.h
index 29fa51e32da1..98713d1b3f58 100644
--- a/lib/zlib_deflate/defutil.h
+++ b/lib/zlib_deflate/defutil.h
@@ -83,7 +83,7 @@ typedef struct deflate_state {
Byte *pending_buf; /* output still pending */
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 */
+ uInt pending; /* nb of bytes in the pending buffer */
int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
Byte method; /* STORED (for zip only) or DEFLATED */
int last_flush; /* value of flush param for previous deflate call */

--
2.56.1