Commit Graph

44 Commits

Author SHA1 Message Date
Carlos Sánchez López
3da40c259e Fixes build issues C4242, C4244 and C4334 caused by loss of data bugs due to data type mismatch in various files. 2024-08-16 11:52:50 +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
Mark Adler
4fe59efbe0 zlib 1.3.1
madler/zlib#51b7f2abdade71cd9bb0e7a373ef2610ec6f9daf
2024-02-07 19:15:56 +01:00
Mark Adler
a3fb271c6e Add LIT_MEM define to use more memory for a small deflate speedup.
A bug fix in zlib 1.2.12 resulted in a slight slowdown (1-2%) of
deflate. This commit provides the option to #define LIT_MEM, which
uses more memory to reverse most of that slowdown. The memory for
the pending buffer and symbol buffers is increased by 25%, which
increases the total memory usage with the default parameters by
about 6%.

madler/zlib#ac8f12c97d1afd9bafa9c710f827d40a407d3266
2024-02-07 19:15:56 +01:00
Nathan Moinvaziri
760f78496a Remove unneeded pointer for functable.longest_match in deflate_slow 2024-01-15 13:00:04 +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
Adam Stylinski
321028cde9 Prevent stale stub functions from being called in deflate_slow
Just in case this is the very first call to longest match, we should
instead assign the function pointer instead of the function itself. This
way, by the time it leaves the stub, the function pointer gets
reassigned. This was found incidentally while debugging something else.
2022-02-23 14:47:49 +01:00
Nathan Moinvaziri
e4c622371d Switch longest_match in deflate_slow based on whether or not rolling hash is being used.
Co-authored-by: Hans Kristian Rosbach <hk-git@circlestorm.org>
2021-06-25 20:09:14 +02:00
Nathan Moinvaziri
a0fe6c590d Use longest_match_slow in deflate_slow. 2021-06-25 20:09:14 +02:00
Nathan Moinvaziri
a8543ecaa4 Minor prev_length calculation improvement in deflate_slow. 2021-06-25 20:09:14 +02:00
Nathan Moinvaziri
823a61bc97 Use STD_MIN_MATCH instead of WANT_MIN_MATCH in deflate_slow for fast-zlib. 2021-06-25 20:09:14 +02:00
Nathan Moinvaziri
1c766dbf67 Setup hash functions to be switched based on compression level. 2021-06-25 20:09:14 +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
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
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
Nathan Moinvaziri
1c58894c67 Remove NIL preprocessor macro which isn't consistently enforced. 2020-09-23 17:00:11 +02:00
Nathan Moinvaziri
7cffba4dd6 Rename ZLIB_INTERNAL to Z_INTERNAL for consistency. 2020-08-31 12:33:16 +02:00
Hans Kristian Rosbach
3a26093baf Fix some of the old and new conversion warnings in deflate* 2020-08-27 19:20:38 +02:00
Hans Kristian Rosbach
47b1208c44 Add likely/unlikely hinting to all deflate algorithms. 2020-07-03 15:33:43 +02:00
Nathan Moinvaziri
9609cb56a8 Use local match_len variable in deflate_slow.
Remove match_len from deflate internal_state.
2020-06-30 18:21:45 +02:00
Nathan Moinvaziri
b5bc498315 Don't set bflush to zero since it is assigned before used. 2020-06-27 16:24:14 +02:00
Dan Kegel
ae0fcc0add deflate_slow: Suppress a static analysis warning triggered by recent refactoring c459b4f5. 2020-06-18 22:13:53 +02:00
Nathan Moinvaziri
36e0e0bdf0 Move Tracevv statements when emitting literal to zng_tr_tally_lit. 2020-06-08 21:16:31 +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
c97a965f18 Converted compare258 to static and convert longest_match to template. 2020-05-24 13:53:25 +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
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
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
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
2071b9349e fix bug #207: avoid undefined integer overflow
zlib-ng used to fail when compiled with UBSan with this error:
deflate_slow.c:112:21: runtime error: unsigned integer overflow: 45871 - 45872 cannot be represented in type 'unsigned int'

The bug occurs in code added to zlib-ng under `#ifndef NOT_TWEAK_COMPILER`.
The original code of zlib contains a loop with two induction variables:

  s->prev_length -= 2;
  do {
      if (++s->strstart <= max_insert) {
          functable.insert_string(s, s->strstart, 1);
      }
  } while (--s->prev_length != 0);

The function insert_string is not executed when
  !(++s->strstart <= max_insert)
i.e., when
  !(s->strstart + 1 <= max_insert)
  !(s->strstart < max_insert)
  max_insert <= s->strstart

The function insert_string is executed when
  ++s->strstart <= max_insert
i.e., when
  s->strstart + 1 <= max_insert
  s->strstart < max_insert

The function is executed at most `max_insert - s->strstart` times, following the
exit condition of the do-while `(--s->prev_length != 0)`.  If the loop exits
after evaluating the exit condition once, the function is executed once
independently of `max_insert - s->strstart`.  The number of times the function
executes is the minimum between the number of iterations in the do-while loop
and `max_insert - s->strstart`.

The number of iterations of the loop is `mov_fwd = s->prev_length - 2`, and we
know that this is at least one as otherwise `--s->prev_length` would overflow.

The number of times the function insert_string is called is
  `min(mov_fwd, max_insert - s->strstart)`
2018-10-16 13:53:02 +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
bcab6edb16 Type cleanup...
* uInt -> unsigned int
* ulg -> unsigned long
2017-02-16 11:20:27 +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
Hans Kristian Rosbach
785a1ec85f No need to update hash_head.
Plus a single style change to match the other deflate algorithms.
2015-06-25 22:34:06 +02:00
Mika Lindqvist
fc1d0be4bb Split deflate.c
* Separate common inlines and macros to deflate_p.h
* Separate deflate_fast related code to deflate_fast.c
* Separate deflate_medium related code to deflate_medium.c
* Separate deflate_slow related code to deflate_slow.c
2015-06-24 20:34:55 +02:00