Commit Graph

76 Commits

Author SHA1 Message Date
Mika Lindqvist
43b2703435 Fix shift overflow in inflate and send_code. 2025-02-08 21:43:51 +01:00
Hans Kristian Rosbach
a5c20ed67e Add variable 'wbufsize' to track window buffer including padding, to allow
the chunkset code to spill garbage data into the padding area if available.
2024-10-08 15:51:12 +02:00
Hans Kristian Rosbach
39e9c86ec0 Don't use 'dmax' and 'sane' variables unless their checks have been compiled in. 2024-10-08 15:51:12 +02:00
Hans Kristian Rosbach
63e1d460aa Rewrite inflate memory allocation.
Inflate used to allocate state during init, but window would be allocated
when/if needed and could be resized and that required a new free/alloc round.

- Now, we allocate state and a 32K window during init, allowing the latency cost
  of allocs to be done during init instead of at one or more times later.
- Total memory allocation is about the same when requesting a 32K window, but
  if now window or a smaller window was requested, then it is an increase.
- While doing alloc(), we now store pointer to corresponding free(), avoiding crashes
  with applications that incorrectly set alloc/free pointers after running init function.
- After init has succeeded, inflate will no longer possibly fail due to a failing malloc.

Co-authored-by: Ilya Leoshkevich <iii@linux.ibm.com>
2024-05-28 16:35:13 +02:00
Ilya Leoshkevich
05ef29eda5 IBM zSystems DFLTCC: Inline DLFTCC states into zlib states
Currently DFLTCC states are allocated using hook macros, complicating
memory management. Inline them into zlib states and remove the hooks.
2024-05-15 11:28:10 +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
Nathan Moinvaziri
fa9bfeddcf Use named defines instead of hard coded numbers. 2023-02-18 20:30:55 +01:00
Hans Kristian Rosbach
cf5bb01da9 Fix prefixing for internal functions calloc/cfree 2023-02-09 01:54:19 +01:00
Nathan Moinvaziri
aa1109bb2e Use arch-specific versions of inflate_fast.
This should reduce the cost of indirection that occurs when calling functable
chunk copying functions inside inflate_fast. It should also allow the compiler
to optimize the inflate fast path for the specific architecture.
2023-02-05 17:51:46 +01:00
Mark Adler
6581fb0efc Have infback() deliver all of the available output up to any error. 2023-02-03 15:49:02 +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
Cameron Cawley
baf0fd1234 Enable and fix -Wimplicit-fallthrough warnings 2022-10-23 15:00:46 +02:00
Dougall Johnson
2d110b17b8 Inflate: Increase max root table sizes to 10 and 9
This increases the size of the `codes` array by 1920 bytes (33%), but
improves performance a little. Root table size is still limited by the
maximum code length in use, so tiny files typically see no change to
table-building time, as they don't use longer codes.
2022-09-25 17:45:00 +02:00
Mark Adler
998fc886d6 Fix inflateBack to detect invalid input with distances too far. 2022-08-17 14:41:41 +02:00
Nathan Moinvaziri
d43822b9a7 zlib 1.2.12 2022-06-13 15:58:03 +02:00
Hans Kristian Rosbach
28b029c726 Simplify version and struct size checking, and ensure we do it the same way everywhere. 2022-06-03 10:21:01 +02:00
Hans Kristian Rosbach
2f4e2372a2 Simplify zlib-ng native API by removing version and struct size checks.
This should be backwards compatible with applications compiled for 2.0.x.
2022-06-03 10:21:01 +02:00
Ilya Leoshkevich
c592b1b332 IBM Z DFLTCC: Split deflate and inflate states
Currently deflate and inflate both use a common state struct. There are
several variables in this struct that we don't need for inflate, and
more may be coming in the future. Therefore split them in two separate
structs. This in turn requires splitting ZALLOC_STATE and ZCOPY_STATE
macros.
2022-04-28 12:01:57 +02:00
Nathan Moinvaziri
6c4beb611d Revert "Reorganize inflate window layout"
This reverts commit dc3b60841d.
2022-03-23 11:30:35 +01:00
Nathan Moinvaziri
b5af5013d9 Revert "Add back original version of inflate_fast for use with inflateBack."
This reverts commit 2d2dde43b1.
2022-03-23 11:30:35 +01:00
Nathan Moinvaziri
2d2dde43b1 Add back original version of inflate_fast for use with inflateBack. 2021-12-02 09:26:32 +01:00
Jim Kukunas
dc3b60841d Reorganize inflate window layout
This commit significantly improves inflate performance by reorganizing the window buffer into a contiguous window and pending output buffer. The goal of this layout is to reduce branching, improve cache locality, and enable for the use of crc folding with gzip input.

The window buffer is allocated as a multiple of the user-selected window size. In this commit, a factor of 2 is utilized.

The layout of the window buffer is divided into two sections. The first section, window offset [0, wsize), is reserved for history that has already been output. The second section, window offset [wsize, 2 * wsize), is reserved for buffering pending output that hasn't been flushed to the user's output buffer yet.

The history section grows downwards, towards the window offset of 0. The pending output section grows upwards, towards the end of the buffer. As a result, all of the possible distance/length data that may need to be copied is contiguous. This removes the need to stitch together output from 2 separate buffers.

In the case of gzip input, crc folding is used to copy the pending output to the user's buffers.

Co-authored-by: Nathan Moinvaziri <nathan@nathanm.com>
2021-12-02 09:26:32 +01:00
Nathan Moinvaziri
3f5801f151 Use MIN and MAX macros. 2021-06-13 20:56:06 +02:00
Nathan Moinvaziri
9ee1c5a9ed Use SET_BAD macro in inflateBack. 2021-06-04 20:53:46 +02:00
Nathan Moinvaziri
05d4a0857a Fixed left shift ubsan warning in PULLBYTE.
infback.c:200:13: runtime error: left shift of 255 by 24 places cannot be represented in type 'int'
  624: SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /Users/runner/work/zlib-ng/zlib-ng/infback.c:200:13 in
2020-10-09 11:31:03 +02:00
Nathan Moinvaziri
1f10aa4e29 Rename ZEXPORT and ZEXTERN for consistency. 2020-08-31 12:33:16 +02:00
Mika Lindqvist
28e5e73f34
Reintroduce support for ZLIB_CONST in compat mode. (#704)
* Reintroduce support for ZLIB_CONST in compat mode.
2020-08-23 09:58:57 +02:00
Nathan Moinvaziri
7241384112 Fixed chunksize not being initialized when using inflateBack. 2020-07-10 15:44:56 +02:00
Nathan Moinvaziri
fe69810c26 Use standard int types in zlib-ng api. 2020-06-09 22:40:36 +02: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
340f2f6ef6 Remove syntax, whitespace and comment differences between inflate.c
and infback.c, making actual differences much easier to spot, easing maintenance.
2019-08-08 10:21:36 +02:00
Hans Kristian Rosbach
4bc6ffa41a Deduplicate inflate's fixedtables(), and no longer inline the inffixed tables.
This also reduces the library size by 4120bytes or ~2.9%.
2019-08-06 09:39:26 +02:00
Hans Kristian Rosbach
d8eedcfa3e Deduplicate common inflate/inflatefast/inflateBack macros into inflate_p.h 2019-08-06 09:39:26 +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
dd70d6c467 bug #117: speed up inflate_fast
Based on a patch by Nigel Tao:
e0ff1f330c

This patch makes unzipping of files up to 1.2x faster on x86_64.  The other part
(1.3x speedup) of the patch by Nigel Tao is unsafe as discussed in the review of
that pull request.  zlib-ng already has a different way to optimize the memcpy
for that missing part.

The original patch was enabled only on little-endian machines. This patch adapts
the loading of 64 bits at a time to big endian machines.

Benchmarking notes from Hans Kristian Rosbach:
https://github.com/zlib-ng/zlib-ng/pull/224#issuecomment-444837182

Benchmark runs: 7, tested levels: 0-7, testfile 100M

develop at 796ad10 with -O3:
Level  Comp   Comptime min/avg/max  Decomptime min/avg/max
0     100.02%       0.01/0.01/0.02          0.08/0.09/0.11
1      47.08%       0.49/0.50/0.51          0.37/0.39/0.40
2      36.02%       1.10/1.12/1.13          0.39/0.39/0.40
3      34.77%       1.32/1.34/1.37          0.38/0.38/0.38
4      33.41%       1.50/1.53/1.56          0.37/0.37/0.38
5      33.07%       1.85/1.87/1.90          0.36/0.37/0.38
6      32.83%       2.54/2.57/2.61          0.36/0.37/0.38
avg    45.31%                 1.28                   0.34
tot                          62.60                  16.58

PR224 with -O3:
Level  Comp   Comptime min/avg/max  Decomptime min/avg/max
0     100.02%       0.01/0.01/0.02          0.09/0.09/0.10
1      47.08%       0.49/0.50/0.51          0.37/0.37/0.38
2      36.02%       1.09/1.11/1.13          0.38/0.38/0.39
3      34.77%       1.32/1.34/1.38          0.35/0.36/0.38
4      33.41%       1.49/1.52/1.54          0.36/0.36/0.37
5      33.07%       1.85/1.88/1.93          0.35/0.36/0.37
6      32.83%       2.55/2.58/2.65          0.35/0.35/0.36
avg    45.31%                 1.28                   0.33
tot                          62.48                  16.02

So I see about a 5.4% speedup on my x86_64 machine, not quite the 1.2x speedup
but a nice speedup nevertheless. This benchmark measures the total execution
time of minigzip, so that might have caused some inefficiencies.

At -O2, I only see a 2.7% speedup.
2018-12-18 13:40:06 +01: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
Mika Lindqvist
aff0fc6e3c Adapt code to support PREFIX macros and update build scripts 2018-01-31 10:45:29 +01:00
Hans Kristian Rosbach
f5e888a6a6 Add function prefix (zng_) to all exported functions to allow zlib-ng
to co-exist in an application that has been linked to something that
depends on stock zlib. Previously, that would cause random problems
since there is no way to guarantee what zlib version is being used
for each dynamically linked function.

Add the corresponding zlib-ng.h.

Tests, example and minigzip will not compile before they have been
adapted to use the correct functions as well.
Either duplicate them, so we have minigzip-ng.c for example, or add
compile-time detection in the source code.
2018-01-31 10:45:29 +01:00
Mika Lindqvist
bcab6edb16 Type cleanup...
* uInt -> unsigned int
* ulg -> unsigned long
2017-02-16 11:20:27 +01:00
Mark Adler
2a51c84f6c zlib 1.2.9 2017-02-09 11:39:40 +01:00
Mark Adler
f9bb580bcb Clean up type conversions.
Based on upstream 7096424f23df1b1813237fb5f8bc8f34cfcedd0c, but
modified heavily to match zlib-ng.
2017-01-31 12:24:20 +01:00
Mika Lindqvist
602531cf3d Replace Z_NULL with NULL. Fix incorrect uses of NULL/Z_NULL. 2017-01-31 10:53:22 +01:00
Mika Lindqvist
631817cce8 local -> static
* local -> static
* Normalize and cleanup line-endings
* Fix warnings under Visual Studio.
* Whitespace cleanup

***
This patch has been edited to merge cleanly and to exclude type changes.
Based on 8d7a7c3b82c6e38734bd504dac800b148ab410d0 "Type Cleanup"
2017-01-30 10:35:05 +01:00
Mika Lindqvist
9c3a280877 Type cleanup. 2015-12-14 11:00:22 +02:00
Hans Kristian Rosbach
8145354217 Revert "Replace 'unsigned long' with most suitable fixed-size type."
This commit was cherry-picked and was not done, resulting in a few
problems with gcc on 64bit windows.

This reverts commit edd7a72e05.

Conflicts:
	arch/x86/crc_folding.c
	arch/x86/fill_window_sse.c
	deflate.c
	deflate.h
	match.c
	trees.c
2015-06-05 18:35:25 +02:00
Hans Kristian Rosbach
2c22452ff2 Style cleanup for inflate code 2015-05-25 23:00:54 +02:00
Mika Lindqvist
edd7a72e05 Replace 'unsigned long' with most suitable fixed-size type. 2015-05-23 22:06:25 +02:00
Mika Lindqvist
d6523bc5d5 Replace (void *)0 with NULL. 2015-05-22 23:17:30 +03:00
Hans Kristian Rosbach
62c6d5ec70 Replace unsigned short with uint16_t
Conflicts:
	inflate.h
	inftrees.c
	inftrees.h
	match.c
2015-05-22 22:15:29 +03:00
Daniel Axtens
48f9b2a0e9 z_const -> const
Signed-off-by: Daniel Axtens <dja@axtens.net>

Conflicts:
	arch/x86/crc_folding.c
	crc32.c
2015-05-13 14:18:18 +02:00