From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796283044; b=XiUPD20rjA9b9Ji4qckiNPaqrRXK73IKOKHs4cl4n4mT6osGG6llH/JYFU37V+S1scsl M5JuGrteuqABH+pRfjnIDGN3RAmIy552Jh2RBswPXizki1YLDvua/LcQWL5cxwxDHRvPv 2Wh4CdWEghvwnR3qkIRuLNJUHL3y8sDi64hmRGUJmD8UAUVp1vDAsiltzs388lx35C5zK xSFZa/n/slg4mVkb7AxNB4F5iM4drVRwU4MZSG/nqIneph2byxp3s3+BGkBHyQz7JUh2N yEGSBuDaEh8n+CBzdAAg26/NZXtCnAhQV+lWpBkrDONPYy3UJyhRcviK2RUGmIV9fCw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796283044; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=wghKAVMQrCjCgC4X3segemq9eUTf15JPhQ/7J+iCdX4=; b=2rwag2R+YlxjPl1ie6zjIthz3Yj3CsIc9gf1TBsxsboYliiXkM7Pky5n3EVkCawMYjpQ FC8Avu78kkBsHlWbgse72sd+2lO9UjURw8VqO2qZBY4Y1gPRJxQKKPg03msdzoepFjYYT Dc/HXpZJcgq0/5n5S/jr12OfAOld0kqRsVVpFL6suHrTPAKTVeIG4CIJwy7aGoOWhSlHE NnP6O3FimhspxDXgS6RK89t6iIG2Z2xAh8NMrb7zAXHti6UxbYthaeLwH8Hk1oUMwYgdG kbGkMJef/LchAPJtHC5OQ2YWKSWs2cHoYLllSHb9XGJQa3AKAkw76YhFyrtfUfRtTTw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Xt7GqcBf; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3F30606A32C for ; Thu, 3 Dec 2026 07:30:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Xt7GqcBf Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA4151F00893; Thu, 3 Dec 2026 07:30:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796283044; bh=wghKAVMQrCjCgC4X3segemq9eUTf15JPhQ/7J+iCdX4=; h=From:Date:Subject:References:In-Reply-To:To; b=Xt7GqcBf4SK7Lz2r+YHiDHYIAckHplMyiZDlrauVnZbM16nwMYAQxfDnfQRkZvgHH trqULT8mMtDl7YzDxWB6Zh0c9qeRjvsUtvN3XADp6E1/aJf+CfsXlRv056NfjVx2lM 0Ag2LFfHEXHqsWqdFyGgHU6Im/F/I1ZPGM70OnaE= From: Greg Kroah-Hartman Date: Thu, 03 Dec 2026 08:29:47 +0100 Subject: [PATCH 07/21] lib/zlib: update deflate to zlib 1.2.0.4 release X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-zlib-sync-v1-7-0d4ec56f1afb@linuxfoundation.org> References: <20261203-zlib-sync-v1-0-0d4ec56f1afb@linuxfoundation.org> In-Reply-To: <20261203-zlib-sync-v1-0-0d4ec56f1afb@linuxfoundation.org> To: linux-kernel@vger.kernel.org, Greg Kroah-Hartman X-Mailer: b4 0.17-dev-73bef X-Developer-Signature: v=1; a=openpgp-sha256; l=11547; i=gregkh@linuxfoundation.org; h=from:subject:message-id; bh=UXXSrRQhMwFInBbZXA2yxNmfJl5wdvpaFRcl3ROpHpE=; b=owGbwMvMwCRo6H6F97bub03G02pJDNmCUlm3L56YbKaZYHLw+NllvaI9mnleibK1136uVWx9c 94h6vXkjlgWBkEmBlkxRZYv23iO7q84pOhlaHsaZg4rE8gQBi5OAZjIrGSG2ewsD0/svO9xf/X+ R1xJmQ98FLcruzEs2Hvzz/Op/quPJsnMs3i2eJvVwrcPXQA= X-Developer-Key: i=gregkh@linuxfoundation.org; a=openpgp; fpr=F4B60CC5BF78C2214A313DCB3147D40DDB2DFB29 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 --- 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 -/* 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 +/* 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