mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org,
	 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: [PATCH 20/21] lib/zlib: update deflate to zlib 1.2.2.4 release
Date: Thu, 03 Dec 2026 08:30:00 +0100	[thread overview]
Message-ID: <20261203-zlib-sync-v1-20-0d4ec56f1afb@linuxfoundation.org> (raw)
In-Reply-To: <20261203-zlib-sync-v1-0-0d4ec56f1afb@linuxfoundation.org>

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@linuxfoundation.org>
---
 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


  parent reply	other threads:[~2026-12-03  7:32 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  7:29 [PATCH 00/21] lib/zlib: start to bring the in-kernel version in sync with upstream Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 01/21] MAINTAINERS: add myself as the zlib maintainer Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 02/21] lib/zlib: update deflate to zlib 1.1.4 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 03/21] lib/zlib: update deflate to zlib 1.2.0 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 04/21] lib/zlib: update deflate to zlib 1.2.0.1 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 05/21] lib/zlib: update deflate to zlib 1.2.0.2 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 06/21] lib/zlib: update deflate to zlib 1.2.0.3 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 07/21] lib/zlib: update deflate to zlib 1.2.0.4 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 08/21] lib/zlib: update deflate to zlib 1.2.0.5 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 09/21] lib/zlib: update deflate to zlib 1.2.0.6 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 10/21] lib/zlib: update deflate to zlib 1.2.0.7 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 11/21] lib/zlib: update deflate to zlib 1.2.0.8 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 12/21] lib/zlib: update deflate to zlib 1.2.1 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 13/21] lib/zlib: update deflate to zlib 1.2.1.1 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 14/21] lib/zlib: update deflate to zlib 1.2.1.2 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 15/21] lib/zlib: update deflate to zlib 1.2.2 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 16/21] lib/zlib: update deflate to zlib 1.2.2.1 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 17/21] lib/zlib: catch code up to the real zlib 1.1.3 Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 18/21] lib/zlib: update deflate to zlib 1.2.2.2 release Greg Kroah-Hartman
2026-12-03  7:29 ` [PATCH 19/21] lib/zlib: update deflate to zlib 1.2.2.3 release Greg Kroah-Hartman
2026-12-03  7:30 ` Greg Kroah-Hartman [this message]
2026-12-03  7:30 ` [PATCH 21/21] lib/zlib: update deflate to zlib 1.2.3 release Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203-zlib-sync-v1-20-0d4ec56f1afb@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®