Commit Graph

63 Commits

Author SHA1 Message Date
Hans Kristian Rosbach
56d3d9851a Clean up insert_match() in deflate_medium 2024-04-11 12:43:06 +02:00
Hans Kristian Rosbach
322753f36e Inserting strings is not slow any longer, remove bypass in deflate_medium(). 2024-04-11 12:43:06 +02:00
Vladislav Shchapov
fe0a6407da Explicitly indicate functions are conditionally dispatched
Signed-off-by: Vladislav Shchapov <vladislav@shchapov.ru>
2024-03-06 23:32:15 +01:00
Hans Kristian Rosbach
9953f12e21 Move update_hash(), insert_string() and quick_insert_string() out of functable
and remove SSE4.2 and ACLE optimizations. The functable overhead is higher
than the benefit from using optimized functions.
2024-02-23 13:34:10 +01:00
Nathan Moinvaziri
b047c7247f Prefix shared functions to prevent symbol conflict when linking native api against compat api. 2023-01-09 15:10:11 +01:00
Nathan Moinvaziri
405f11bfbf Remove duplicate header includes. 2022-02-06 16:52:24 +01:00
Hans Kristian Rosbach
94fd14ede7 Remove support for STD_MIN_MATCH != 3.
It has always been broken and untested anyways.
2021-06-13 20:55:01 +02:00
Hans Kristian Rosbach
6bea850887 Minor formatting changes related to MIN_MATCH/MAX_MATCH 2021-06-13 20:55:01 +02:00
Hans Kristian Rosbach
cf9127a231 Separate MIN_MATCH into STD_MIN_MATCH and WANT_MIN_MATCH
Rename MAX_MATCH to STD_MAX_MATCH
2021-06-13 20:55:01 +02:00
Hans Kristian Rosbach
ce6789c7e0 Rebalance levels 1-4.
- Deflate_quick (level 1), no longer limit window, improves compression.
- Deflate_medium, don't check next position for levels below 5.
- Use deflate_medium instead of deflate_fast for level 3.
- Tweak level 4 to give a more predictable speed/compression tradeoff curve.
2021-06-12 20:35:34 +02:00
Ilya Leoshkevich
f426ac9db3 Restore hash_head != 0 checks
Commit bc5915e2de ("Fixed unsigned integer overflow ASAN error when
hash_head > s->strstart.") removed hash_head != 0 checks in fast,
medium and slow deflate, because it improved performance [1].

Unfortunately, the attached test started failing after that.
Apparently, as the comments suggest, the code implicitly relies on
matches with the beginning of the window being skipped. So restore the
check.

[1] https://github.com/zlib-ng/zlib-ng/pull/772#issuecomment-710760300
2021-03-20 23:35:48 +01:00
Nathan Moinvaziri
a659d7f071 Fixed casting warnings when comparing MAX_DIST.
deflate_medium.c(127,76): warning C4244: '=': conversion from 'unsigned int' to 'Pos', possible loss of data
2020-11-02 17:01:58 +01:00
Nathan Moinvaziri
8d33736b03 Fixed match_start uint32_t to uint16_t casting warnings in deflate_medium.c
deflate_medium.c(204,49): warning C4244: '=': conversion from 'unsigned int' to 'uint16_t', possible loss of data
  deflate_medium.c(217,59): warning C4244: '=': conversion from 'unsigned int' to 'uint16_t', possible loss of data
  deflate_medium.c(238,46): warning C4244: '=': conversion from 'unsigned int' to 'uint16_t', possible loss of data
  deflate_medium.c(250,56): warning C4244: '=': conversion from 'unsigned int' to 'uint16_t', possible loss of data
2020-11-02 17:01:58 +01:00
Nathan Moinvaziri
bc5915e2de Fixed unsigned integer overflow ASAN error when hash_head > s->strstart.
zlib-ng/deflate_medium.c:244:47: runtime error: unsigned integer overflow: 58442 - 58452 cannot be represented in type 'unsigned int'

Co-authored-by: Mika Lindqvist <postmaster@raasu.org>
Co-authored-by: Hans Kristian Rosbach <hk-git@circlestorm.org>
2020-10-18 14:35:55 +02:00
Hans Kristian Rosbach
93ae5483d8 Fix numerous sign-conversion warnings in compare256/compare258 and
longest_match related code.
2020-08-31 13:22:54 +02:00
Nathan Moinvaziri
7cffba4dd6 Rename ZLIB_INTERNAL to Z_INTERNAL for consistency. 2020-08-31 12:33:16 +02:00
Hans Kristian Rosbach
47b1208c44 Add likely/unlikely hinting to all deflate algorithms. 2020-07-03 15:33:43 +02:00
Nathan Moinvaziri
4b1e9ea618 Do not set s->prev_length in deflate_medium, it is previously set in lm_init already. 2020-06-27 16:24:14 +02:00
Nathan Moinvaziri
712dd22704 Only set current_match to literal if hash_head == 0. 2020-06-27 16:24:14 +02:00
Hans Kristian Rosbach
f690af1ed0 Remove toofar checks now that we always hash 4 bytes, and the minimum
match length is thus also 4. (MIN_MATCH is still 3 for other reasons)
2020-05-30 21:31:38 +02:00
Nathan Moinvaziri
a0fa24f92f Remove IPos typedef which also helps to reduce casting warnings. 2020-05-30 21:29:44 +02:00
Nathan Moinvaziri
07207681ed Simplify generic hash function using knuth's multiplicative hash. 2020-05-24 14:32:26 +02:00
Nathan Moinvaziri
c97a965f18 Converted compare258 to static and convert longest_match to template. 2020-05-24 13:53:25 +02:00
Hans Kristian Rosbach
06173b6a49 Change two NOT_TWEAK_COMPILER to check for MIN-MATCH!=3 instead. 2020-05-06 10:00:11 +02:00
Hans Kristian Rosbach
6884f3715c Remove several NOT_TWEAK_COMPILER checks and their legacy code. 2020-05-06 10:00:11 +02:00
Nathan Moinvaziri
e09d131b5a Standardize fill_window implementations and abstract out slide_hash_neon for ARM. 2020-05-01 00:21:18 +02:00
Nathan Moinvaziri
51e695092d Replaced insert_string with quick_insert_string if length is 1. 2020-04-30 10:01:46 +02:00
Nathan Moinvaziri
c3c9ab51cb Fixed signed/unsigned mismatch in deflate_medium in MSVC. 2020-04-16 13:05:23 +02:00
Hans Kristian Rosbach
3f21e5071f Add comment regarding long match distance bit costs. 2020-03-18 11:04:05 +01:00
Hans Kristian Rosbach
4c1cf4f104 Reduce size of 'match' struct to 8 bytes, this allows us to fit two
structs into a single cacheline, resulting in a measurable speedup
in deflate_medium.
2020-03-18 11:04:05 +01:00
Hans Kristian Rosbach
30ad518826 Clean up usage of bflush make more similar for each deflate strategy. 2020-03-18 11:04:05 +01:00
Nathan Moinvaziri
c459b4f5e1 Clean up zng_tr_tally code. 2020-03-13 13:04:37 +01:00
Nathan Moinvaziri
5c4eb71b1d Removed MAX_DIST2 which is incompatible with zlib when compiled with INFLATE_STRICT. It also fixes the check for proper bounds in zng_tr_tally. 2020-02-07 17:48:05 +01:00
Nathan Moinvaziri
e0a711cdde Fixed formatting, 4 spaces for code intent, 2 spaces for preprocessor indent, initial function brace on the same line as definition, removed extraneous spaces and new lines. 2020-02-07 10:44:20 +01:00
Hans Kristian Rosbach
cf5959e119 Clean up LIKELY/UNLIKELY definitions, making them upper-case to improve visibility.
Add LIKELY_NULL hint.
Add PREFETCH_L1, PREFETCH_L2 and PREFETCH_RW for GCC, Clang, ICC and MSVC.
2019-10-22 09:56:34 +02:00
Nathan Moinvaziri
f06c71f981 Add zng_ prefix to internal functions to avoid linking conflicts with zlib. (#363) 2019-07-18 13:21:13 +02:00
Ilya Leoshkevich
d4b927b346 deflate_medium: fix handling of overlapping matches
Fixes #361
2019-07-18 10:50:06 +02:00
Hans Kristian Rosbach
24772ef137 Let deflate_medium be enabled by default. 2019-01-21 10:27:08 +01:00
Sebastian Pop
2576c4a710 move match.c to match_p.h and remove match.h 2019-01-16 10:49:40 +01:00
Mark Adler
ccbb3fef5a Fix a bug that can crash deflate on some input when using Z_FIXED.
This bug was reported by Danilo Ramos of Eideticom, Inc. It has
lain in wait 13 years before being found! The bug was introduced
in zlib 1.2.2.2, with the addition of the Z_FIXED option. That
option forces the use of fixed Huffman codes. For rare inputs with
a large number of distant matches, the pending buffer into which
the compressed data is written can overwrite the distance symbol
table which it overlays. That results in corrupted output due to
invalid distances, and can result in out-of-bound accesses,
crashing the application.

The fix here combines the distance buffer and literal/length
buffers into a single symbol buffer. Now three bytes of pending
buffer space are opened up for each literal or length/distance
pair consumed, instead of the previous two bytes. This assures
that the pending buffer cannot overwrite the symbol table, since
the maximum fixed code compressed length/distance is 31 bits, and
since there are four bytes of pending space for every three bytes
of symbol space.
2018-11-14 09:21:26 +01:00
Sebastian Pop
29b6ab42ca fix bug #192, oss-fuzz/9827 : MemorySanitizer:DEADLYSIGNAL
==4908==ERROR: MemorySanitizer: SEGV on unknown address 0x730fffffffff (pc 0x0000004b1b97 bp 0x7ffd4bf59a00 sp 0x7ffd4bf598a0 T4908)
==4908==The signal is caused by a READ memory access.
  #0 0x5a0599 in fizzle_matches zlib-ng/deflate_medium.c:168:12
  #1 0x59ea27 in deflate_medium zlib-ng/deflate_medium.c:296:21
  #2 0x5901c5 in zng_deflate zlib-ng/deflate.c:951:18
  #3 0x586955 in zng_compress2 zlib-ng/compress.c:59:15
  #4 0x5861eb in LLVMFuzzerTestOneInput zlib-ng/test/fuzz/compress_fuzzer.c:18:3
  #5 0x4e9b48 in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) /src/libfuzzer/FuzzerLoop.cpp:575:15
  #6 0x4a2f66 in fuzzer::RunOneTest(fuzzer::Fuzzer*, char const*, unsigned long) /src/libfuzzer/FuzzerDriver.cpp:280:6
  #7 0x4b3adb in fuzzer::FuzzerDriver(int*, char***, int (*)(unsigned char const*, unsigned long)) /src/libfuzzer/FuzzerDriver.cpp:715:9
  #8 0x4a2091 in main /src/libfuzzer/FuzzerMain.cpp:20:10
  #9 0x7fa3d7ff582f in __libc_start_main /build/glibc-Cl5G7W/glibc-2.23/csu/libc-start.c:291
  #10 0x41ec68 in _start
2018-09-17 11:16:10 +02:00
Mika Lindqvist
df1fd1567f Move private defines from zconf.h and zconf-ng.h to zbuild.h
* move definition of z_size_t to zbuild.h
2018-03-22 10:18:27 +01:00
Hans Kristian Rosbach
eb7fd8a1b0 Make sure we don't export internal functions 2017-08-17 11:24:46 +02:00
Hans Kristian Rosbach
da51338488 Add a struct func_table and function functableInit.
The struct contains pointers to select functions to be used by the
rest of zlib, and the init function selects what functions will be
used depending on what optimizations has been compiled in and what
instruction-sets are available at runtime.

Tests done on a haswell cpu running minigzip -6 compression of a
40M file shows a 2.5% decrease in branches, and a 25-30% reduction
in iTLB-loads. The reduction i iTLB-loads is likely mostly due to
the inability to inline functions. This also causes a slight
performance regression of around 1%, this might still be worth it
to make it much easier to implement new optimized functions for
various architectures and instruction sets.

The performance penalty will get smaller for functions that get more
alternative implementations to choose from, since there is no need
to add more branches to every call of the function.
Today insert_string has 1 branch to choose insert_string_sse
or insert_string_c, but if we also add for example insert_string_sse4
then that would have needed another branch, and it would probably
at some point hinder effective inlining too.
2017-04-24 11:02:56 +02:00
Mika Lindqvist
75e76eebeb deflate_medium: Make sure we have enough lookahead before trying to scan for matches.
* longest_match: Abort if match is in future
2017-03-24 21:56:23 +01:00
Mika Lindqvist
9fdda220c6 deflate_medium: Remove broken overlap test. 2017-03-24 21:56:15 +01:00
Mika Lindqvist
bcca02c8d4 Fix typos. 2017-03-24 21:55:38 +01:00
Mika Lindqvist
53204685c0 Avoid hashing same memory location twice by truncating overlapping byte ranges,
it's speed optimization as the inner code also checks that previous hash value
is not same as new hash value. Essentially those two checks together makes the
compression a little more efficient as it can remember matches further apart.
As far as I remember from my tests, the secondary path was triggered only twice
in very long uncompressed file, but the gain in compression rate was still noticeable.
2017-02-16 11:19:57 +01:00
Hans Kristian Rosbach
317e8824c9 Fix missing insert_string change from merge conflict 2017-01-30 13:18:07 +01:00
Mika Lindqvist
f111d5cb42 Merge insert_string and bulk_insert_str.
** Partial merge of this commit, based on a8c94e9f5a3b9d3c62182bcf84e72304a3c1a6e5
Excludes changes to fill_window_sse.c, changes to fill_window_c() in deflate.c
and several unrelated changes in the commit.
2017-01-30 12:07:06 +01:00