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 07/21] lib/zlib: update deflate to zlib 1.2.0.4 release
Date: Thu, 03 Dec 2026 08:29:47 +0100	[thread overview]
Message-ID: <20261203-zlib-sync-v1-7-0d4ec56f1afb@linuxfoundation.org> (raw)
In-Reply-To: <20261203-zlib-sync-v1-0-0d4ec56f1afb@linuxfoundation.org>

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


  parent reply	other threads:[~2026-12-03  7:30 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 ` Greg Kroah-Hartman [this message]
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 ` [PATCH 20/21] lib/zlib: update deflate to zlib 1.2.2.4 release Greg Kroah-Hartman
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-7-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®