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=1796283019; b=gqs1NX/Fi1XTN/7gtSq5wGeaxlF58u0VpKq5HqpNVQ7Uw6s4lBRYtA6R22GLqK+F73X4 bzNdPYwAwVxucIcCBDAplvGBr7X2f7zZ9CPMkq3tLKIAmiMiTJ45+VuMkAQ/LXIzH/fcd UxgA9WN9flGOiIMwBSN1126QTsE300z1XWyHY8H1rxRqa29Bp+jYMcZJtg4KTEdHD4pv4 OqML+aGtyNjJUqXevZeokgLOyz/Ae7Ho4q8FzklJVcVSBrBS587948QZ31VjdR4O2f5RS Lgh9h1PqRK1qMqdPb3nX4BIe9lbaex5yKiF8nchh6exzHcH6Cswiy0LqNkUqtV5K5Lg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796283019; 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=J7/GeNBjAg295PTtNZS/mfNt2RfJKk7b9MinVcOa3tQ=; b=tEGkT/m24KAsd+GLNiUBwXElyyho7HRYy5vLmSv1iJ0uPrOSaFCTFcC3okt+sq9TZHRX 4nHln0tiMuo3PEj4wduNJ9XSz1lNh9/+9zsGRdArujksE0BDYNe2LpkceEWv42UWEdK5u +4o7Rm8404jkjSrHGyPeY3dphwjRvAWYwbPiDuTCwUrAA8saxX+UdHz8dZBIaf72Slz2e RRMQcDL4y6vKWvTA08ZHiLWC184h+ckNHSg3ytz6gl5kXdrdG8bnP1cj5KgwLie74ERuf SLzNGB2WiV9nInmUPeLVX47ZrqytEYrBO/1X1xjJuCGbeD8HXZykNz3H7llaWfIJAQA== 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=YzBfEc1a; 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 B8E9A606A32D for ; Thu, 3 Dec 2026 07:30:19 +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=YzBfEc1a Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31CAC1F00896; Thu, 3 Dec 2026 07:30:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796283019; bh=J7/GeNBjAg295PTtNZS/mfNt2RfJKk7b9MinVcOa3tQ=; h=From:Date:Subject:References:In-Reply-To:To; b=YzBfEc1aS8odctmwF4XV4UARhKtWYONrasQZlTMcZf34bxAOPVDfuEkiMqJ/LPXW1 aTXcPifbcLBRjJB2xVegvRUk/Z++yTf7YEzDKuWeOBDSptyV8BElb0Pneci5DjRLy+ v9Sq8GvPpHvQ9avUBu+zRn7R14O6ipWLsyu9fxbw= From: Greg Kroah-Hartman Date: Thu, 03 Dec 2026 08:29:44 +0100 Subject: [PATCH 04/21] lib/zlib: update deflate to zlib 1.2.0.1 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-4-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=37493; i=gregkh@linuxfoundation.org; h=from:subject:message-id; bh=aZwE5Qu+k01/bYTXkKXtk+gPNG/WmbLLvirNSgdzsGY=; b=owGbwMvMwCRo6H6F97bub03G02pJDNmCUlkCXqtWKx+/pbv4066OCIXfxr5vi6bGiTw4PX91i 9C2ThbWjlgWBkEmBlkxRZYv23iO7q84pOhlaHsaZg4rE8gQBi5OAZhI6zKGeXZVGVP8j4onVV9V qj14aL/a2d1K2xnmO91T2Nj276JihG1SVcvb95F+AjvTAQ== X-Developer-Key: i=gregkh@linuxfoundation.org; a=openpgp; fpr=F4B60CC5BF78C2214A313DCB3147D40DDB2DFB29 Sync the kernel deflate code with the zlib 1.2.0.1 release. This involves: - add the Z_RLE compression strategy which limits match distances to one (run-length encoding) makeing it almost as fast as Z_HUFFMAN_ONLY but with better compression for PNG-style data. - convert the deflate sources from tabs to spaces and strip trailing whitespace. - fix the Z_BUFF_ERROR typo in a comment. There were some 1.2.0.1 changes that were not relevant to the kernel copy: - FASTEST build variant. - deflateInit_/deflateParams/deflateSetDictionary/deflateCopy and gen_trees_header changes as these functions are not in the kernel copy. - vsnprintf detection in zutil.h. The kernel is now in sync with zlib commit 13a294f044ef ("zlib 1.2.0.1"). Signed-off-by: Greg Kroah-Hartman --- include/linux/zlib.h | 21 ++-- lib/zlib_deflate/deflate.c | 282 +++++++++++++++++++++++++++------------------ lib/zlib_deflate/deftree.c | 138 +++++++++++----------- lib/zlib_deflate/defutil.h | 30 ++--- 4 files changed, 268 insertions(+), 203 deletions(-) diff --git a/include/linux/zlib.h b/include/linux/zlib.h index 2e04200282d5..fe5f7a68fc1a 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" */ +/* zlib deflate based on ZLIB_VERSION "1.2.0.1" */ /* zlib inflate based on ZLIB_VERSION "1.2.3" */ /* @@ -166,6 +166,7 @@ typedef z_stream *z_streamp; #define Z_FILTERED 1 #define Z_HUFFMAN_ONLY 2 +#define Z_RLE 3 #define Z_DEFAULT_STRATEGY 0 /* compression strategy; see deflateInit2() below for details */ @@ -485,14 +486,16 @@ extern int deflateInit2 (z_streamp strm, The strategy parameter is used to tune the compression algorithm. Use the value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a - filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no - string match). Filtered data consists mostly of small values with a - somewhat random distribution. In this case, the compression algorithm is - tuned to compress them better. The effect of Z_FILTERED is to force more - Huffman coding and less string matching; it is somewhat intermediate - between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects - the compression ratio but not the correctness of the compressed output even - if it is not set appropriately. + filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no + string match), or Z_RLE to limit match distances to one (run-length + encoding). Filtered data consists mostly of small values with a somewhat + random distribution. In this case, the compression algorithm is tuned to + compress them better. The effect of Z_FILTERED is to force more Huffman + coding and less string matching; it is somewhat intermediate between + Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as + Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy + parameter only affects the compression ratio but not the correctness of the + compressed output even if it is not set appropriately. deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid diff --git a/lib/zlib_deflate/deflate.c b/lib/zlib_deflate/deflate.c index 2cf1e3500104..a703adf97820 100644 --- a/lib/zlib_deflate/deflate.c +++ b/lib/zlib_deflate/deflate.c @@ -77,6 +77,7 @@ static void lm_init (deflate_state *s); static void putShortMSB (deflate_state *s, uInt b); static int read_buf (z_streamp strm, Byte *buf, unsigned size); static uInt longest_match (deflate_state *s, IPos cur_match); +static uInt longest_match_fast (deflate_state *s, IPos cur_match); #ifdef DEBUG_ZLIB static void check_match (deflate_state *s, IPos start, IPos match, @@ -183,12 +184,12 @@ static const config configuration_table[10] = { /* ========================================================================= */ int zlib_deflateInit2( - z_streamp strm, - int level, - int method, - int windowBits, - int memLevel, - int strategy + z_streamp strm, + int level, + int method, + int windowBits, + int memLevel, + int strategy ) { deflate_state *s; @@ -215,7 +216,7 @@ int zlib_deflateInit2( } if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || windowBits < 9 || windowBits > 15 || level < 0 || level > 9 || - strategy < 0 || strategy > Z_HUFFMAN_ONLY) { + strategy < 0 || strategy > Z_RLE) { return Z_STREAM_ERROR; } @@ -277,11 +278,11 @@ int zlib_deflateInit2( /* ========================================================================= */ int zlib_deflateReset( - z_streamp strm + z_streamp strm ) { deflate_state *s; - + if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR; @@ -314,31 +315,31 @@ int zlib_deflateReset( * pending_buf. */ static void putShortMSB( - deflate_state *s, - uInt b + deflate_state *s, + uInt b ) { put_byte(s, (Byte)(b >> 8)); put_byte(s, (Byte)(b & 0xff)); -} +} /* ========================================================================= */ int zlib_deflate( - z_streamp strm, - int flush + z_streamp strm, + int flush ) { int old_flush; /* value of flush param for previous deflate call */ deflate_state *s; if (strm == NULL || strm->state == NULL || - flush > Z_FINISH || flush < 0) { + flush > Z_FINISH || flush < 0) { return Z_STREAM_ERROR; } s = (deflate_state *) strm->state; if ((strm->next_in == NULL && strm->avail_in != 0) || - (s->status == FINISH_STATE && flush != Z_FINISH)) { + (s->status == FINISH_STATE && flush != Z_FINISH)) { return Z_STREAM_ERROR; } if (strm->avail_out == 0) return Z_BUF_ERROR; @@ -355,40 +356,40 @@ int zlib_deflate( if (level_flags > 3) level_flags = 3; header |= (level_flags << 6); - if (s->strstart != 0) header |= PRESET_DICT; + if (s->strstart != 0) header |= PRESET_DICT; header += 31 - (header % 31); 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)); - } - strm->adler = 1L; + /* 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 = 1L; } /* Flush as much pending output as possible */ if (s->pending != 0) { flush_pending(strm); if (strm->avail_out == 0) { - /* Since avail_out is 0, deflate will be called again with - * more output space, but possibly with both pending and - * avail_in equal to zero. There won't be anything to do, - * but this is not an error situation so make sure we - * return OK instead of BUF_ERROR at next call of deflate: + /* Since avail_out is 0, deflate will be called again with + * more output space, but possibly with both pending and + * avail_in equal to zero. There won't be anything to do, + * but this is not an error situation so make sure we + * return OK instead of BUF_ERROR at next call of deflate: */ - s->last_flush = -1; - return Z_OK; - } + s->last_flush = -1; + return Z_OK; + } /* Make sure there is something to do and avoid duplicate consecutive * flushes. For repeated and useless calls with Z_FINISH, we keep - * returning Z_STREAM_END instead of Z_BUFF_ERROR. + * returning Z_STREAM_END instead of Z_BUF_ERROR. */ } else if (strm->avail_in == 0 && flush <= old_flush && - flush != Z_FINISH) { + flush != Z_FINISH) { return Z_BUF_ERROR; } @@ -403,32 +404,32 @@ int zlib_deflate( (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { block_state bstate; - bstate = DEFLATE_HOOK(strm, flush, &bstate) ? bstate : - (*(configuration_table[s->level].func))(s, flush); + bstate = DEFLATE_HOOK(strm, flush, &bstate) ? bstate : + (*(configuration_table[s->level].func))(s, flush); if (bstate == finish_started || bstate == finish_done) { s->status = FINISH_STATE; } if (bstate == need_more || bstate == finish_started) { - if (strm->avail_out == 0) { - s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ - } - return Z_OK; - /* If flush != Z_NO_FLUSH && avail_out == 0, the next call - * of deflate should use the same flush parameter to make sure - * that the flush is complete. So we don't have to output an - * empty block here, this will be done at next call. This also - * ensures that for a very small output buffer, we emit at most - * one empty block. - */ - } + if (strm->avail_out == 0) { + s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ + } + return Z_OK; + /* If flush != Z_NO_FLUSH && avail_out == 0, the next call + * of deflate should use the same flush parameter to make sure + * that the flush is complete. So we don't have to output an + * empty block here, this will be done at next call. This also + * ensures that for a very small output buffer, we emit at most + * one empty block. + */ + } if (bstate == block_done) { if (flush == Z_PARTIAL_FLUSH) { zlib_tr_align(s); - } else if (flush == Z_PACKET_FLUSH) { - /* Output just the 3-bit `stored' block type value, - but not a zero length. */ - zlib_tr_stored_type_only(s); + } else if (flush == Z_PACKET_FLUSH) { + /* Output just the 3-bit `stored' block type value, + but not a zero length. */ + zlib_tr_stored_type_only(s); } else { /* FULL_FLUSH or SYNC_FLUSH */ zlib_tr_stored_block(s, (char*)0, 0L, 0); /* For a full flush, this empty block will be recognized @@ -439,10 +440,10 @@ int zlib_deflate( } } flush_pending(strm); - if (strm->avail_out == 0) { - s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ - return Z_OK; - } + if (strm->avail_out == 0) { + s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ + return Z_OK; + } } } Assert(strm->avail_out > 0, "bug2"); @@ -450,27 +451,27 @@ int zlib_deflate( if (flush != Z_FINISH) return Z_OK; if (!s->noheader) { - /* Write zlib trailer (adler32) */ - putShortMSB(s, (uInt)(strm->adler >> 16)); - putShortMSB(s, (uInt)(strm->adler & 0xffff)); + /* Write zlib trailer (adler32) */ + putShortMSB(s, (uInt)(strm->adler >> 16)); + putShortMSB(s, (uInt)(strm->adler & 0xffff)); } flush_pending(strm); /* 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! */ + 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; + Assert(s->bi_valid == 0, "bi_buf not flushed"); + return Z_STREAM_END; } return Z_OK; } /* ========================================================================= */ int zlib_deflateEnd( - z_streamp strm + z_streamp strm ) { int status; @@ -481,7 +482,7 @@ int zlib_deflateEnd( status = s->status; if (status != INIT_STATE && status != BUSY_STATE && - status != FINISH_STATE) { + status != FINISH_STATE) { return Z_STREAM_ERROR; } @@ -498,9 +499,9 @@ int zlib_deflateEnd( * (See also flush_pending()). */ static int read_buf( - z_streamp strm, - Byte *buf, - unsigned size + z_streamp strm, + Byte *buf, + unsigned size ) { unsigned len = strm->avail_in; @@ -525,7 +526,7 @@ static int read_buf( * Initialize the "longest match" routines for a new zlib stream */ static void lm_init( - deflate_state *s + deflate_state *s ) { s->window_size = (ulg)2L*s->w_size; @@ -560,8 +561,8 @@ static void lm_init( * match.S. The code will be functionally equivalent. */ static uInt longest_match( - deflate_state *s, - IPos cur_match /* current match */ + deflate_state *s, + IPos cur_match /* current match */ ) { unsigned chain_length = s->max_chain_length;/* max hash chain length */ @@ -698,24 +699,81 @@ static uInt longest_match( return s->lookahead; } +/* --------------------------------------------------------------------------- + * Optimized version for level == 1 or strategy == Z_RLE only + */ +static uInt longest_match_fast( + deflate_state *s, + IPos cur_match /* current match */ +) +{ + register Byte *scan = s->window + s->strstart; /* current string */ + register Byte *match; /* matched string */ + register int len; /* length of current match */ + register Byte *strend = s->window + s->strstart + MAX_MATCH; + + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. + * It is easy to get rid of this optimization if necessary. + */ + Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); + + Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); + + Assert(cur_match < s->strstart, "no future"); + + match = s->window + cur_match; + + /* Return failure if the match length is less than 2: + */ + if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; + + /* The check at best_len-1 can be removed because it will be made + * again later. (This heuristic is not always a win.) + * It is not necessary to compare scan[2] and match[2] since they + * are always equal when the other bytes match, given that + * the hash keys are equal and that HASH_BITS >= 8. + */ + scan += 2, match += 2; + Assert(*scan == *match, "match[2]?"); + + /* We check for insufficient lookahead only every 8th comparison; + * the 256th check will be made at strstart+258. + */ + do { + } while (*++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + scan < strend); + + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + + len = MAX_MATCH - (int)(strend - scan); + + if (len < MIN_MATCH) return MIN_MATCH - 1; + + s->match_start = cur_match; + return len <= s->lookahead ? len : s->lookahead; +} + #ifdef DEBUG_ZLIB /* =========================================================================== * Check that the match at match_start is indeed a match. */ static void check_match( - deflate_state *s, - IPos start, - IPos match, - int length + deflate_state *s, + IPos start, + IPos match, + int length ) { /* check that the match is indeed a match */ if (memcmp((char *)s->window + match, (char *)s->window + start, length)) { fprintf(stderr, " start %u, match %u, length %d\n", - start, match, length); + start, match, length); do { - fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); - } while (--length != 0); + fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); + } while (--length != 0); z_error("invalid match"); } if (z_verbose > 1) { @@ -738,7 +796,7 @@ static void check_match( * option -- not supported here). */ static void fill_window( - deflate_state *s + deflate_state *s ) { register unsigned n, m; @@ -835,8 +893,8 @@ static void fill_window( zlib_tr_flush_block(s, (s->block_start >= 0L ? \ (char *)&s->window[(unsigned)s->block_start] : \ NULL), \ - (ulg)((long)s->strstart - s->block_start), \ - (eof)); \ + (ulg)((long)s->strstart - s->block_start), \ + (eof)); \ s->block_start = s->strstart; \ flush_pending(s->strm); \ Tracev((stderr,"[FLUSH]")); \ @@ -858,8 +916,8 @@ static void fill_window( * window to pending_buf. */ static block_state deflate_stored( - deflate_state *s, - int flush + deflate_state *s, + int flush ) { /* Stored blocks are limited to 0xffff bytes, pending_buf is limited @@ -878,32 +936,32 @@ static block_state deflate_stored( if (s->lookahead <= 1) { Assert(s->strstart < s->w_size+MAX_DIST(s) || - s->block_start >= (long)s->w_size, "slide too late"); + s->block_start >= (long)s->w_size, "slide too late"); fill_window(s); if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more; if (s->lookahead == 0) break; /* flush the current block */ } - Assert(s->block_start >= 0L, "block gone"); + Assert(s->block_start >= 0L, "block gone"); - s->strstart += s->lookahead; - s->lookahead = 0; + s->strstart += s->lookahead; + s->lookahead = 0; - /* Emit a stored block if pending_buf will be full: */ - max_start = s->block_start + max_block_size; + /* Emit a stored block if pending_buf will be full: */ + max_start = s->block_start + max_block_size; if (s->strstart == 0 || (ulg)s->strstart >= max_start) { - /* strstart == 0 is possible when wraparound on 16-bit machine */ - s->lookahead = (uInt)(s->strstart - max_start); - s->strstart = (uInt)max_start; + /* strstart == 0 is possible when wraparound on 16-bit machine */ + s->lookahead = (uInt)(s->strstart - max_start); + s->strstart = (uInt)max_start; FLUSH_BLOCK(s, 0); - } - /* Flush if we may have to slide, otherwise block_start may become + } + /* Flush if we may have to slide, otherwise block_start may become * negative and the data will be gone: */ if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) { FLUSH_BLOCK(s, 0); - } + } } FLUSH_BLOCK(s, flush == Z_FINISH); return flush == Z_FINISH ? finish_done : block_done; @@ -917,8 +975,8 @@ static block_state deflate_stored( * matches. It is used only for the fast compression options. */ static block_state deflate_fast( - deflate_state *s, - int flush + deflate_state *s, + int flush ) { IPos hash_head = NIL; /* head of the hash chain */ @@ -933,8 +991,8 @@ static block_state deflate_fast( if (s->lookahead < MIN_LOOKAHEAD) { fill_window(s); if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { - return need_more; - } + return need_more; + } if (s->lookahead == 0) break; /* flush the current block */ } @@ -953,10 +1011,12 @@ static block_state deflate_fast( * of window index 0 (in particular we have to avoid a match * of the string with itself at the start of the input file). */ - if (s->strategy != Z_HUFFMAN_ONLY) { + if (s->strategy < Z_HUFFMAN_ONLY) { s->match_length = longest_match (s, hash_head); + } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) { + s->match_length = longest_match_fast (s, hash_head); } - /* longest_match() sets match_start */ + /* longest_match() or longest_match_fast() sets match_start */ } if (s->match_length >= MIN_MATCH) { check_match(s, s->strstart, s->match_start, s->match_length); @@ -979,7 +1039,7 @@ static block_state deflate_fast( * always MIN_MATCH bytes ahead. */ } while (--s->match_length != 0); - s->strstart++; + s->strstart++; } else { s->strstart += s->match_length; s->match_length = 0; @@ -997,7 +1057,7 @@ static block_state deflate_fast( Tracevv((stderr,"%c", s->window[s->strstart])); bflush = zlib_tr_tally (s, 0, s->window[s->strstart]); s->lookahead--; - s->strstart++; + s->strstart++; } if (bflush) FLUSH_BLOCK(s, 0); } @@ -1011,8 +1071,8 @@ static block_state deflate_fast( * no better match at the next window position. */ static block_state deflate_slow( - deflate_state *s, - int flush + deflate_state *s, + int flush ) { IPos hash_head = NIL; /* head of hash chain */ @@ -1028,8 +1088,8 @@ static block_state deflate_slow( if (s->lookahead < MIN_LOOKAHEAD) { fill_window(s); if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { - return need_more; - } + return need_more; + } if (s->lookahead == 0) break; /* flush the current block */ } @@ -1051,10 +1111,12 @@ static block_state deflate_slow( * of window index 0 (in particular we have to avoid a match * of the string with itself at the start of the input file). */ - if (s->strategy != Z_HUFFMAN_ONLY) { + if (s->strategy < Z_HUFFMAN_ONLY) { s->match_length = longest_match (s, hash_head); + } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) { + s->match_length = longest_match_fast (s, hash_head); } - /* longest_match() sets match_start */ + /* longest_match() or longest_match_fast() sets match_start */ if (s->match_length <= 5 && (s->strategy == Z_FILTERED || (s->match_length == MIN_MATCH && @@ -1076,7 +1138,7 @@ static block_state deflate_slow( check_match(s, s->strstart-1, s->prev_match, s->prev_length); bflush = zlib_tr_tally(s, s->strstart -1 - s->prev_match, - s->prev_length - MIN_MATCH); + s->prev_length - MIN_MATCH); /* Insert in hash table all strings up to the end of the match. * strstart-1 and strstart are already inserted. If there is not @@ -1134,7 +1196,7 @@ int zlib_deflate_workspacesize(int windowBits, int memLevel) /* Since the return value is typically passed to vmalloc() unchecked... */ BUG_ON(memLevel < 1 || memLevel > MAX_MEM_LEVEL || windowBits < 9 || - windowBits > 15); + windowBits > 15); return sizeof(deflate_workspace) + zlib_deflate_window_memsize(windowBits) @@ -1145,5 +1207,5 @@ int zlib_deflate_workspacesize(int windowBits, int memLevel) int zlib_deflate_dfltcc_enabled(void) { - return DEFLATE_DFLTCC_ENABLED(); + return DEFLATE_DFLTCC_ENABLED(); } diff --git a/lib/zlib_deflate/deftree.c b/lib/zlib_deflate/deftree.c index 2b98e56a713f..a5bcfbfe9013 100644 --- a/lib/zlib_deflate/deftree.c +++ b/lib/zlib_deflate/deftree.c @@ -239,7 +239,7 @@ static void tr_static_init(void) * Initialize the tree data structures for a new zlib stream. */ void zlib_tr_init( - deflate_state *s + deflate_state *s ) { tr_static_init(); @@ -270,7 +270,7 @@ void zlib_tr_init( * Initialize a new block. */ static void init_block( - deflate_state *s + deflate_state *s ) { int n; /* iterates over tree elements */ @@ -315,9 +315,9 @@ static void init_block( * two sons). */ static void pqdownheap( - deflate_state *s, - ct_data *tree, /* the tree to restore */ - int k /* node to move down */ + deflate_state *s, + ct_data *tree, /* the tree to restore */ + int k /* node to move down */ ) { int v = s->heap[k]; @@ -351,8 +351,8 @@ static void pqdownheap( * not null. */ static void gen_bitlen( - deflate_state *s, - tree_desc *desc /* the tree descriptor */ + deflate_state *s, + tree_desc *desc /* the tree descriptor */ ) { ct_data *tree = desc->dyn_tree; @@ -439,9 +439,9 @@ static void gen_bitlen( * zero code length. */ static void gen_codes( - ct_data *tree, /* the tree to decorate */ - int max_code, /* largest code with non zero frequency */ - ush *bl_count /* number of codes at each bit length */ + ct_data *tree, /* the tree to decorate */ + int max_code, /* largest code with non zero frequency */ + ush *bl_count /* number of codes at each bit length */ ) { ush next_code[MAX_BITS+1]; /* next code value for each bit length */ @@ -482,8 +482,8 @@ static void gen_codes( * also updated if stree is not null. The field max_code is set. */ static void build_tree( - deflate_state *s, - tree_desc *desc /* the tree descriptor */ + deflate_state *s, + tree_desc *desc /* the tree descriptor */ ) { ct_data *tree = desc->dyn_tree; @@ -571,9 +571,9 @@ static void build_tree( * in the bit length tree. */ static void scan_tree( - deflate_state *s, - ct_data *tree, /* the tree to be scanned */ - int max_code /* and its largest code of non zero frequency */ + deflate_state *s, + ct_data *tree, /* the tree to be scanned */ + int max_code /* and its largest code of non zero frequency */ ) { int n; /* iterates over all tree elements */ @@ -617,9 +617,9 @@ static void scan_tree( * bl_tree. */ static void send_tree( - deflate_state *s, - ct_data *tree, /* the tree to be scanned */ - int max_code /* and its largest code of non zero frequency */ + deflate_state *s, + ct_data *tree, /* the tree to be scanned */ + int max_code /* and its largest code of non zero frequency */ ) { int n; /* iterates over all tree elements */ @@ -669,7 +669,7 @@ static void send_tree( * bl_order of the last bit length code to send. */ static int build_bl_tree( - deflate_state *s + deflate_state *s ) { int max_blindex; /* index of last bit length code of non zero freq */ @@ -705,10 +705,10 @@ static int build_bl_tree( * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. */ static void send_all_trees( - deflate_state *s, - int lcodes, /* number of codes for each tree */ - int dcodes, /* number of codes for each tree */ - int blcodes /* number of codes for each tree */ + deflate_state *s, + int lcodes, /* number of codes for each tree */ + int dcodes, /* number of codes for each tree */ + int blcodes /* number of codes for each tree */ ) { int rank; /* index in bl_order */ @@ -737,10 +737,10 @@ static void send_all_trees( * Send a stored block */ void zlib_tr_stored_block( - deflate_state *s, - char *buf, /* input block */ - ulg stored_len, /* length of input block */ - int eof /* true if this is the last block for a file */ + deflate_state *s, + char *buf, /* input block */ + ulg stored_len, /* length of input block */ + int eof /* true if this is the last block for a file */ ) { send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */ @@ -753,7 +753,7 @@ void zlib_tr_stored_block( /* Send just the `stored block' type code without any length bytes or data. */ void zlib_tr_stored_type_only( - deflate_state *s + deflate_state *s ) { send_bits(s, (STORED_BLOCK << 1), 3); @@ -774,7 +774,7 @@ void zlib_tr_stored_type_only( * on one bit only. */ void zlib_tr_align( - deflate_state *s + deflate_state *s ) { send_bits(s, STATIC_TREES<<1, 3); @@ -801,10 +801,10 @@ void zlib_tr_align( * returns the total compressed length for the file so far. */ ulg zlib_tr_flush_block( - deflate_state *s, - char *buf, /* input block, or NULL if too old */ - ulg stored_len, /* length of input block */ - int eof /* true if this is the last block for a file */ + deflate_state *s, + char *buf, /* input block, or NULL if too old */ + ulg stored_len, /* length of input block */ + int eof /* true if this is the last block for a file */ ) { ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ @@ -813,39 +813,39 @@ ulg zlib_tr_flush_block( /* Build the Huffman trees unless a stored block is forced */ if (s->level > 0) { - /* Check if the file is ascii or binary */ - if (s->data_type == Z_UNKNOWN) set_data_type(s); + /* Check if the file is ascii or binary */ + if (s->data_type == Z_UNKNOWN) set_data_type(s); - /* Construct the literal and distance trees */ - build_tree(s, (tree_desc *)(&(s->l_desc))); - Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, - s->static_len)); + /* Construct the literal and distance trees */ + build_tree(s, (tree_desc *)(&(s->l_desc))); + Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, + s->static_len)); - build_tree(s, (tree_desc *)(&(s->d_desc))); - Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, - s->static_len)); - /* At this point, opt_len and static_len are the total bit lengths of - * the compressed block data, excluding the tree representations. - */ + build_tree(s, (tree_desc *)(&(s->d_desc))); + Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, + s->static_len)); + /* At this point, opt_len and static_len are the total bit lengths of + * the compressed block data, excluding the tree representations. + */ - /* Build the bit length tree for the above two trees, and get the index - * in bl_order of the last bit length code to send. - */ - max_blindex = build_bl_tree(s); + /* Build the bit length tree for the above two trees, and get the index + * in bl_order of the last bit length code to send. + */ + max_blindex = build_bl_tree(s); - /* Determine the best encoding. Compute the block lengths in bytes. */ - opt_lenb = (s->opt_len+3+7)>>3; - static_lenb = (s->static_len+3+7)>>3; + /* Determine the best encoding. Compute the block lengths in bytes. */ + opt_lenb = (s->opt_len+3+7)>>3; + static_lenb = (s->static_len+3+7)>>3; - Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", - opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, - s->last_lit)); + Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", + opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, + s->last_lit)); - if (static_lenb <= opt_lenb) opt_lenb = static_lenb; + if (static_lenb <= opt_lenb) opt_lenb = static_lenb; } else { Assert(buf != (char*)0, "lost buf"); - opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ + opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ } /* If compression failed and this is the first and last block, @@ -914,9 +914,9 @@ ulg zlib_tr_flush_block( * the current block must be flushed. */ int zlib_tr_tally( - deflate_state *s, - unsigned dist, /* distance of matched string */ - unsigned lc /* match length-MIN_MATCH or unmatched char (if dist==0) */ + deflate_state *s, + unsigned dist, /* distance of matched string */ + unsigned lc /* match length-MIN_MATCH or unmatched char (if dist==0) */ ) { s->d_buf[s->last_lit] = (ush)dist; @@ -963,9 +963,9 @@ int zlib_tr_tally( * Send the block data compressed using the given Huffman trees */ static void compress_block( - deflate_state *s, - ct_data *ltree, /* literal tree */ - ct_data *dtree /* distance tree */ + deflate_state *s, + ct_data *ltree, /* literal tree */ + ct_data *dtree /* distance tree */ ) { unsigned dist; /* distance of matched string */ @@ -1017,7 +1017,7 @@ static void compress_block( * frequencies does not exceed 64K (to fit in an int on 16 bit machines). */ static void set_data_type( - deflate_state *s + deflate_state *s ) { int n = 0; @@ -1034,17 +1034,17 @@ static void set_data_type( * one's complement if requested. */ static void copy_block( - deflate_state *s, - char *buf, /* the input data */ - unsigned len, /* its length */ - int header /* true if block header must be written */ + deflate_state *s, + char *buf, /* the input data */ + unsigned len, /* its length */ + int header /* true if block header must be written */ ) { bi_windup(s); /* align on byte boundary */ s->last_eob_len = 8; /* enough lookahead for inflate */ if (header) { - put_short(s, (ush)len); + put_short(s, (ush)len); put_short(s, (ush)~len); #ifdef DEBUG_ZLIB s->bits_sent += 2*16; diff --git a/lib/zlib_deflate/defutil.h b/lib/zlib_deflate/defutil.h index 4ea40f5a279f..0614141d4cda 100644 --- a/lib/zlib_deflate/defutil.h +++ b/lib/zlib_deflate/defutil.h @@ -3,11 +3,11 @@ #include -#define Assert(err, str) -#define Trace(dummy) -#define Tracev(dummy) -#define Tracecv(err, dummy) -#define Tracevv(dummy) +#define Assert(err, str) +#define Trace(dummy) +#define Tracev(dummy) +#define Tracecv(err, dummy) +#define Tracevv(dummy) @@ -242,17 +242,17 @@ typedef struct deflate_state { #ifdef CONFIG_ZLIB_DFLTCC #define zlib_deflate_window_memsize(windowBits) \ - (2 * (1 << (windowBits)) * sizeof(Byte) + PAGE_SIZE) + (2 * (1 << (windowBits)) * sizeof(Byte) + PAGE_SIZE) #else #define zlib_deflate_window_memsize(windowBits) \ - (2 * (1 << (windowBits)) * sizeof(Byte)) + (2 * (1 << (windowBits)) * sizeof(Byte)) #endif #define zlib_deflate_prev_memsize(windowBits) \ - ((1 << (windowBits)) * sizeof(Pos)) + ((1 << (windowBits)) * sizeof(Pos)) #define zlib_deflate_head_memsize(memLevel) \ - ((1 << ((memLevel)+7)) * sizeof(Pos)) + ((1 << ((memLevel)+7)) * sizeof(Pos)) #define zlib_deflate_overlay_memsize(memLevel) \ - ((1 << ((memLevel)+6)) * (sizeof(ush)+2)) + ((1 << ((memLevel)+6)) * (sizeof(ush)+2)) /* Output a byte on the stream. * IN assertion: there is enough room in pending_buf. @@ -274,10 +274,10 @@ typedef struct deflate_state { void zlib_tr_init (deflate_state *s); int zlib_tr_tally (deflate_state *s, unsigned dist, unsigned lc); ulg zlib_tr_flush_block (deflate_state *s, char *buf, ulg stored_len, - int eof); + int eof); void zlib_tr_align (deflate_state *s); void zlib_tr_stored_block (deflate_state *s, char *buf, ulg stored_len, - int eof); + int eof); void zlib_tr_stored_type_only (deflate_state *); @@ -417,7 +417,7 @@ static inline void zlib_tr_send_bits( * (See also read_buf()). */ static inline void flush_pending( - z_streamp strm + z_streamp strm ) { unsigned len; @@ -429,8 +429,8 @@ static inline void flush_pending( if (len == 0) return; if (strm->next_out != NULL) { - memcpy(strm->next_out, s->pending_out, len); - strm->next_out += len; + memcpy(strm->next_out, s->pending_out, len); + strm->next_out += len; } s->pending_out += len; strm->total_out += len; -- 2.56.1