[PATCH 05/21] lib/zlib: update deflate to zlib 1.2.0.2 release
From: Greg Kroah-Hartman
Date: Thu Dec 03 2026 - 02:30:31 EST
Sync the kernel deflate code with the zlib 1.2.0.2 release.
This involves:
- accept windowBits == 8 in zlib_deflateInit2() again, silently
forcing it to 9 until the 256-byte-window bug is fixed (matching
upstream). The kernel's zlib_deflate_workspacesize() applies the
same change so a caller passing 8 sizes the workspace for the 9 that
init will actually use.
- compute the zlib header compression-level flags from the strategy
and level directly: Z_HUFFMAN_ONLY/Z_RLE and levels < 2 now mark the
stream as "fastest", matching what the data actually contains.
- restructure the 64K-limit handling in fill_window() under a
sizeof(int) <= 2 test so the window-slide check is no longer chained
behind the 16-bit-only cases.
- compile out the TOO_FAR heuristic in deflate_slow() when TOO_FAR is
configured >= the window size, but this results in no real change
with the default value of 4096.
- add missing (uInt) casts in longest_match_fast()'s return.
- rename the header guards _ZLIB_H/_Z_UTIL_H to ZLIB_H/ZUTIL_H and fix
an RFC URL in a comment.
There were some 1.2.0.2 changes that were not relevant to the kernel
copy:
- ZLIB_VERNUM/zlibCompileFlags
- _WIN32_WCE fdopen and vsnprintf detection
- the NO_DUMMY_DECL guard for the dummy struct in deflate.c
- inflateInit2() documentation updates were already present from the
kernel's 1.2.3-based inflate code.
The kernel is now in sync with zlib commit 8e34b3a8024c ("zlib 1.2.0.2").
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
include/linux/zlib.h | 10 +++++-----
include/linux/zutil.h | 6 +++---
lib/zlib_deflate/deflate.c | 47 ++++++++++++++++++++++++++++++----------------
3 files changed, 39 insertions(+), 24 deletions(-)
diff --git a/include/linux/zlib.h b/include/linux/zlib.h
index fe5f7a68fc1a..64a0ceafbb2c 100644
--- a/include/linux/zlib.h
+++ b/include/linux/zlib.h
@@ -27,12 +27,12 @@
(zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
*/
-#ifndef _ZLIB_H
-#define _ZLIB_H
+#ifndef ZLIB_H
+#define ZLIB_H
#include <linux/zconf.h>
-/* zlib deflate based on ZLIB_VERSION "1.2.0.1" */
+/* zlib deflate based on ZLIB_VERSION "1.2.0.2" */
/* zlib inflate based on ZLIB_VERSION "1.2.3" */
/*
@@ -591,7 +591,7 @@ extern int zlib_deflateInit2(z_streamp strm, int level, int method,
int strategy);
extern int zlib_inflateInit2(z_streamp strm, int windowBits);
-#if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)
+#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL)
struct internal_state {int dummy;}; /* hack for buggy compilers */
#endif
@@ -599,4 +599,4 @@ extern int zlib_inflateInit2(z_streamp strm, int windowBits);
* return len or negative error code. */
extern int zlib_inflate_blob(void *dst, unsigned dst_sz, const void *src, unsigned src_sz);
-#endif /* _ZLIB_H */
+#endif /* ZLIB_H */
diff --git a/include/linux/zutil.h b/include/linux/zutil.h
index c77a8d43d12f..2197ac6cff70 100644
--- a/include/linux/zutil.h
+++ b/include/linux/zutil.h
@@ -10,8 +10,8 @@
/* @(#) $Id: zutil.h,v 1.1 2000/01/01 03:32:23 davem Exp $ */
-#ifndef _Z_UTIL_H
-#define _Z_UTIL_H
+#ifndef ZUTIL_H
+#define ZUTIL_H
#include <linux/zlib.h>
#include <linux/string.h>
@@ -97,4 +97,4 @@ static inline uLong zlib_adler32(uLong adler,
return (s2 << 16) | s1;
}
-#endif /* _Z_UTIL_H */
+#endif /* ZUTIL_H */
diff --git a/lib/zlib_deflate/deflate.c b/lib/zlib_deflate/deflate.c
index a703adf97820..66b4bfd260a8 100644
--- a/lib/zlib_deflate/deflate.c
+++ b/lib/zlib_deflate/deflate.c
@@ -38,7 +38,7 @@
* REFERENCES
*
* Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
- * Available in ftp://ds.internic.net/rfc/rfc1951.txt
+ * Available in http://www.ietf.org/rfc/rfc1951.txt
*
* A description of the Rabin and Karp algorithm is given in the book
* "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
@@ -215,10 +215,11 @@ int zlib_deflateInit2(
windowBits = -windowBits;
}
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
- windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
+ windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
strategy < 0 || strategy > Z_RLE) {
return Z_STREAM_ERROR;
}
+ if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
/*
* Direct the workspace's pointers to the chunks that were allocated
@@ -352,9 +353,16 @@ int zlib_deflate(
if (s->status == INIT_STATE) {
uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
- uInt level_flags = (s->level-1) >> 1;
+ uInt level_flags;
- if (level_flags > 3) level_flags = 3;
+ 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);
@@ -753,7 +761,7 @@ static uInt longest_match_fast(
if (len < MIN_MATCH) return MIN_MATCH - 1;
s->match_start = cur_match;
- return len <= s->lookahead ? len : s->lookahead;
+ return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
}
#ifdef DEBUG_ZLIB
@@ -808,19 +816,22 @@ static void fill_window(
more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
/* Deal with !@#$% 64K limit: */
- if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
- more = wsize;
+ if (sizeof(int) <= 2) {
+ if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
+ more = wsize;
- } else if (more == (unsigned)(-1)) {
- /* Very unlikely, but possible on 16 bit machine if strstart == 0
- * and lookahead == 1 (input done one byte at time)
- */
- more--;
+ } else if (more == (unsigned)(-1)) {
+ /* Very unlikely, but possible on 16 bit machine if
+ * strstart == 0 && lookahead == 1 (input done one byte at time)
+ */
+ more--;
+ }
+ }
/* If the window is almost full and there is insufficient lookahead,
* move the upper half to the lower one to make room in the upper half.
*/
- } else if (s->strstart >= wsize+MAX_DIST(s)) {
+ if (s->strstart >= wsize+MAX_DIST(s)) {
memcpy((char *)s->window, (char *)s->window+wsize,
(unsigned)wsize);
@@ -1118,9 +1129,12 @@ static block_state deflate_slow(
}
/* longest_match() or longest_match_fast() sets match_start */
- if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
- (s->match_length == MIN_MATCH &&
- s->strstart - s->match_start > TOO_FAR))) {
+ if (s->match_length <= 5 && (s->strategy == Z_FILTERED
+#if TOO_FAR < 32768
+ || (s->match_length == MIN_MATCH &&
+ s->strstart - s->match_start > TOO_FAR)
+#endif
+ )) {
/* If prev_match is also MIN_MATCH, match_start is garbage
* but we will ignore the current match anyway.
@@ -1193,6 +1207,7 @@ int zlib_deflate_workspacesize(int windowBits, int memLevel)
{
if (windowBits < 0) /* undocumented feature: suppress zlib header */
windowBits = -windowBits;
+ if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
/* Since the return value is typically passed to vmalloc() unchecked... */
BUG_ON(memLevel < 1 || memLevel > MAX_MEM_LEVEL || windowBits < 9 ||
--
2.56.1