From cd1dc96d27e54d676d854c3691ceeadc85cf02c2 Mon Sep 17 00:00:00 2001 From: Randy Date: Tue, 8 Feb 2022 23:24:02 +0100 Subject: [PATCH 1/2] deflate: write zlib FLEVEL based on compression level --- miniz_tdef.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/miniz_tdef.c b/miniz_tdef.c index 313a03c..5b287a8 100644 --- a/miniz_tdef.c +++ b/miniz_tdef.c @@ -602,6 +602,8 @@ static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block) return tdefl_compress_lz_codes(d); } +static const mz_uint s_tdefl_num_probes[11]; + static int tdefl_flush_block(tdefl_compressor *d, int flush) { mz_uint saved_bit_buf, saved_bits_in; @@ -622,8 +624,27 @@ static int tdefl_flush_block(tdefl_compressor *d, int flush) if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index)) { - TDEFL_PUT_BITS(0x78, 8); - TDEFL_PUT_BITS(0x01, 8); + const mz_uint8 cmf = 0x78; + mz_uint8 flg, flevel = 3; + mz_uint header, i, n = sizeof(s_tdefl_num_probes) / sizeof(mz_uint); + + /* Determine compression level by reversing the process in tdefl_create_comp_flags_from_zip_params() */ + for (i=0; i < n; i++) + if (s_tdefl_num_probes[i] == (d->m_flags & 0xFFF)) break; + + if (i < 2) + flevel = 0; + else if (i < 6) + flevel = 1; + else if (i == 6) + flevel = 2; + + header = cmf << 8 | (flevel << 6); + header += 31 - (header % 31); + flg = header & 0xFF; + + TDEFL_PUT_BITS(cmf, 8); + TDEFL_PUT_BITS(flg, 8); } TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1); From 7c459c8a057fd6a6de75f4a084f35a893270a5bf Mon Sep 17 00:00:00 2001 From: Randy Date: Fri, 6 May 2022 05:01:09 +0200 Subject: [PATCH 2/2] Update miniz_tdef.c Co-authored-by: Alexander Borsuk <170263+biodranik@users.noreply.github.com> --- miniz_tdef.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniz_tdef.c b/miniz_tdef.c index 5b287a8..3e6f5d0 100644 --- a/miniz_tdef.c +++ b/miniz_tdef.c @@ -629,7 +629,7 @@ static int tdefl_flush_block(tdefl_compressor *d, int flush) mz_uint header, i, n = sizeof(s_tdefl_num_probes) / sizeof(mz_uint); /* Determine compression level by reversing the process in tdefl_create_comp_flags_from_zip_params() */ - for (i=0; i < n; i++) + for (i = 0; i < n; i++) if (s_tdefl_num_probes[i] == (d->m_flags & 0xFFF)) break; if (i < 2)