import zlib-ng in compat mode

This commit is contained in:
Alug committed 2025-03-20 14:31:02 +01:00
1 parent 78780799ea
commit 405c566038
96 files changed
+34709

No files matched your search

Binary file not shown.
Binary file not shown.
+27
View File
@@ -0,0 +1,27 @@
codecov:
max_report_age: off
notify:
wait_for_ci: false
require_ci_to_pass: false
comment:
require_base: false
require_head: false
coverage:
status:
project:
default:
threshold: 0.07
fixes:
- '/home/actions-runner/_work/zlib-ng/zlib-ng::'
- '/home/actions-runner/_work/zlib-ng/zlib-ng/build/::'
ignore:
- usr/include/.*
- /usr/include/.*
- /build/usr/include/.*
- usr/lib/.*
- /usr/lib/.*
- /build/usr/lib/.*
- usr/lib64/.*
- /usr/lib64/.*
- /build/usr/lib64/.*
- _deps/**/*
+1
View File
@@ -0,0 +1 @@
disable=SC2140,SC2086,SC2046,SC2015,SC1097,SC1035,SC1036,SC1007,SC2154,SC2155,SC2000,SC2034,SC2016,SC1091,SC1090,SC2212,SC2143,SC2129,SC2102,SC2069,SC1041,SC1042,SC1044,SC1046,SC1119,SC1110,SC1111,SC1112,SC1102,SC1105,SC1101,SC1004,SC1003,SC1012,SC2068,SC2065,SC2064,SC2063,SC2059,SC2053,SC2048,SC2044,SC2032,SC2031,SC2030,SC2029,SC2025,SC2024,SC2022,SC2018,SC2019,SC2017,SC2014,SC2013,SC2012,SC2009,SC2001,SC2098,SC2096,SC2094,SC2091,SC2092,SC2088,SC2087,SC2076,SC2072,SC2071,SC2223,SC2221,SC2222,SC2217,SC2207,SC2206,SC2205,SC2190,SC2188,SC2187,SC2185,SC2179,SC2178,SC2174,SC2168,SC2167,SC2163,SC2161,SC2160,SC2153,SC2150,SC2148,SC2147,SC2146,SC2142,SC2139,SC2126,SC2123,SC2120,SC2119,SC2117,SC2114,SC1117,SC2164,SC1083,SC2004,SC2125,SC2128,SC2011,SC1008,SC1019,SC2093,SC1132,SC1129,SC2236,SC2237,SC2231,SC2230,SC2229,SC2106,SC2102,SC2243,SC2244,SC2245,SC2247,SC2248,SC2249,SC2250,SC2251,SC2252,SC2181
File diff suppressed because it is too large. Load diff
+373
View File
@@ -0,0 +1,373 @@
##
# THIS IS AN UNMAINTAINED COPY OF THE ORIGINAL FILE DISTRIBUTED WITH ZLIB 1.2.11
##
Frequently Asked Questions about zlib
If your question is not there, please check the zlib home page
https://zlib.net/ which may have more recent information.
The latest zlib FAQ is at https://zlib.net/zlib_faq.html
1. Is zlib Y2K-compliant?
Yes. zlib doesn't handle dates.
2. Where can I get a Windows DLL version?
The zlib sources can be compiled without change to produce a DLL. See the
file win32/DLL_FAQ.txt in the zlib distribution.
3. Where can I get a Visual Basic interface to zlib?
See
* https://marknelson.us/1997/01/01/zlib-engine/
* win32/DLL_FAQ.txt in the zlib distribution
4. compress() returns Z_BUF_ERROR.
Make sure that before the call of compress(), the length of the compressed
buffer is equal to the available size of the compressed buffer and not
zero. For Visual Basic, check that this parameter is passed by reference
("as any"), not by value ("as long").
5. deflate() or inflate() returns Z_BUF_ERROR.
Before making the call, make sure that avail_in and avail_out are not zero.
When setting the parameter flush equal to Z_FINISH, also make sure that
avail_out is big enough to allow processing all pending input. Note that a
Z_BUF_ERROR is not fatal--another call to deflate() or inflate() can be
made with more input or output space. A Z_BUF_ERROR may in fact be
unavoidable depending on how the functions are used, since it is not
possible to tell whether or not there is more output pending when
strm.avail_out returns with zero. See https://zlib.net/zlib_how.html for a
heavily annotated example.
6. Where's the zlib documentation (man pages, etc.)?
It's in zlib.h . Examples of zlib usage are in the files test/example.c
and test/minigzip.c, with more in examples/ .
7. Why don't you use GNU autoconf or libtool or ...?
Because we would like to keep zlib as a very small and simple package.
zlib is rather portable and doesn't need much configuration.
8. I found a bug in zlib.
Most of the time, such problems are due to an incorrect usage of zlib.
Please try to reproduce the problem with a small program and send the
corresponding source to us at zlib@gzip.org . Do not send multi-megabyte
data files without prior agreement.
9. Why do I get "undefined reference to gzputc"?
If "make test" produces something like
example.o(.text+0x154): undefined reference to `gzputc'
check that you don't have old files libz.* in /usr/lib, /usr/local/lib or
/usr/X11R6/lib. Remove any old versions, then do "make install".
10. I need a Delphi interface to zlib.
See the contrib/delphi directory in the zlib distribution.
11. Can zlib handle .zip archives?
Not by itself, no. See the directory contrib/minizip in the zlib
distribution.
12. Can zlib handle .Z files?
No, sorry. You have to spawn an uncompress or gunzip subprocess, or adapt
the code of uncompress on your own.
13. How can I make a Unix shared library?
By default a shared (and a static) library is built for Unix. So:
make distclean
./configure
make
14. How do I install a shared zlib library on Unix?
After the above, then:
make install
However, many flavors of Unix come with a shared zlib already installed.
Before going to the trouble of compiling a shared version of zlib and
trying to install it, you may want to check if it's already there! If you
can #include <zlib.h>, it's there. The -lz option will probably link to
it. You can check the version at the top of zlib.h or with the
ZLIB_VERSION symbol defined in zlib.h .
15. I have a question about OttoPDF.
We are not the authors of OttoPDF. The real author is on the OttoPDF web
site: Joel Hainley, jhainley@myndkryme.com.
16. Can zlib decode Flate data in an Adobe PDF file?
Yes. See https://www.pdflib.com/ . To modify PDF forms, see
https://sourceforge.net/projects/acroformtool/ .
17. Why am I getting this "register_frame_info not found" error on Solaris?
After installing zlib 1.1.4 on Solaris 2.6, running applications using zlib
generates an error such as:
ld.so.1: rpm: fatal: relocation error: file /usr/local/lib/libz.so:
symbol __register_frame_info: referenced symbol not found
The symbol __register_frame_info is not part of zlib, it is generated by
the C compiler (cc or gcc). You must recompile applications using zlib
which have this problem. This problem is specific to Solaris. See
http://www.sunfreeware.com/ for Solaris versions of zlib and applications
using zlib.
18. Why does gzip give an error on a file I make with compress/deflate?
The compress and deflate functions produce data in the zlib format, which
is different and incompatible with the gzip format. The gz* functions in
zlib on the other hand use the gzip format. Both the zlib and gzip formats
use the same compressed data format internally, but have different headers
and trailers around the compressed data.
19. Ok, so why are there two different formats?
The gzip format was designed to retain the directory information about a
single file, such as the name and last modification date. The zlib format
on the other hand was designed for in-memory and communication channel
applications, and has a much more compact header and trailer and uses a
faster integrity check than gzip.
20. Well that's nice, but how do I make a gzip file in memory?
You can request that deflate write the gzip format instead of the zlib
format using deflateInit2(). You can also request that inflate decode the
gzip format using inflateInit2(). Read zlib.h for more details.
21. Is zlib thread-safe?
Yes. However any library routines that zlib uses and any application-
provided memory allocation routines must also be thread-safe. zlib's gz*
functions use stdio library routines, and most of zlib's functions use the
library memory allocation routines by default. zlib's *Init* functions
allow for the application to provide custom memory allocation routines.
Of course, you should only operate on any given zlib or gzip stream from a
single thread at a time.
22. Can I use zlib in my commercial application?
Yes. Please read the license in zlib.h.
23. Is zlib under the GNU license?
No. Please read the license in zlib.h.
24. The license says that altered source versions must be "plainly marked". So
what exactly do I need to do to meet that requirement?
You need to change the ZLIB_VERSION and ZLIB_VERNUM #defines in zlib.h. In
particular, the final version number needs to be changed to "f", and an
identification string should be appended to ZLIB_VERSION. Version numbers
x.x.x.f are reserved for modifications to zlib by others than the zlib
maintainers. For example, if the version of the base zlib you are altering
is "1.2.3.4", then in zlib.h you should change ZLIB_VERNUM to 0x123f, and
ZLIB_VERSION to something like "1.2.3.f-zachary-mods-v3". You can also
update the version strings in deflate.c and inftrees.c.
For altered source distributions, you should also note the origin and
nature of the changes in zlib.h, as well as in ChangeLog and README, along
with the dates of the alterations. The origin should include at least your
name (or your company's name), and an email address to contact for help or
issues with the library.
Note that distributing a compiled zlib library along with zlib.h and
zconf.h is also a source distribution, and so you should change
ZLIB_VERSION and ZLIB_VERNUM and note the origin and nature of the changes
in zlib.h as you would for a full source distribution.
25. Will zlib work on a big-endian or little-endian architecture, and can I
exchange compressed data between them?
Yes and yes.
26. Will zlib work on a 64-bit machine?
Yes. It has been tested on 64-bit machines, and has no dependence on any
data types being limited to 32-bits in length. If you have any
difficulties, please provide a complete problem report to zlib@gzip.org
27. Will zlib decompress data from the PKWare Data Compression Library?
No. The PKWare DCL uses a completely different compressed data format than
does PKZIP and zlib. However, you can look in zlib's contrib/blast
directory for a possible solution to your problem.
28. Can I access data randomly in a compressed stream?
No, not without some preparation. If when compressing you periodically use
Z_FULL_FLUSH, carefully write all the pending data at those points, and
keep an index of those locations, then you can start decompression at those
points. You have to be careful to not use Z_FULL_FLUSH too often, since it
can significantly degrade compression. Alternatively, you can scan a
deflate stream once to generate an index, and then use that index for
random access. See examples/zran.c .
29. Does zlib work on MVS, OS/390, CICS, etc.?
It has in the past, but we have not heard of any recent evidence. There
were working ports of zlib 1.1.4 to MVS, but those links no longer work.
If you know of recent, successful applications of zlib on these operating
systems, please let us know. Thanks.
30. Is there some simpler, easier to read version of inflate I can look at to
understand the deflate format?
First off, you should read RFC 1951. Second, yes. Look in zlib's
contrib/puff directory.
31. Does zlib infringe on any patents?
As far as we know, no. In fact, that was originally the whole point behind
zlib. Look here for some more information:
https://www.gzip.org/#faq11
32. Can zlib work with greater than 4 GB of data?
Yes. inflate() and deflate() will process any amount of data correctly.
Each call of inflate() or deflate() is limited to input and output chunks
of the maximum value that can be stored in the compiler's "unsigned int"
type, but there is no limit to the number of chunks. Note however that the
strm.total_in and strm_total_out counters may be limited to 4 GB. These
counters are provided as a convenience and are not used internally by
inflate() or deflate(). The application can easily set up its own counters
updated after each call of inflate() or deflate() to count beyond 4 GB.
compress() and uncompress() may be limited to 4 GB, since they operate in a
single call. gzseek() and gztell() may be limited to 4 GB depending on how
zlib is compiled. See the zlibCompileFlags() function in zlib.h.
The word "may" appears several times above since there is a 4 GB limit only
if the compiler's "long" type is 32 bits. If the compiler's "long" type is
64 bits, then the limit is 16 exabytes.
33. Does zlib have any security vulnerabilities?
The only one that we are aware of is potentially in gzprintf(). If zlib is
compiled to use sprintf() or vsprintf(), then there is no protection
against a buffer overflow of an 8K string space (or other value as set by
gzbuffer()), other than the caller of gzprintf() assuring that the output
will not exceed 8K. On the other hand, if zlib is compiled to use
snprintf() or vsnprintf(), which should normally be the case, then there is
no vulnerability. The ./configure script will display warnings if an
insecure variation of sprintf() will be used by gzprintf(). Also the
zlibCompileFlags() function will return information on what variant of
sprintf() is used by gzprintf().
If you don't have snprintf() or vsnprintf() and would like one, you can
find a portable implementation here:
https://www.ijs.si/software/snprintf/
Note that you should be using the most recent version of zlib. Versions
1.1.3 and before were subject to a double-free vulnerability, and versions
1.2.1 and 1.2.2 were subject to an access exception when decompressing
invalid compressed data.
34. Is there a Java version of zlib?
Probably what you want is to use zlib in Java. zlib is already included
as part of the Java SDK in the java.util.zip package. If you really want
a version of zlib written in the Java language, look on the zlib home
page for links: https://zlib.net/ .
35. I get this or that compiler or source-code scanner warning when I crank it
up to maximally-pedantic. Can't you guys write proper code?
Many years ago, we gave up attempting to avoid warnings on every compiler
in the universe. It just got to be a waste of time, and some compilers
were downright silly as well as contradicted each other. So now, we simply
make sure that the code always works.
36. Valgrind (or some similar memory access checker) says that deflate is
performing a conditional jump that depends on an uninitialized value.
Isn't that a bug?
No. That is intentional for performance reasons, and the output of deflate
is not affected. This only started showing up recently since zlib 1.2.x
uses malloc() by default for allocations, whereas earlier versions used
calloc(), which zeros out the allocated memory. Even though the code was
correct, versions 1.2.4 and later was changed to not stimulate these
checkers.
37. Will zlib read the (insert any ancient or arcane format here) compressed
data format?
Probably not. Look in the comp.compression FAQ for pointers to various
formats and associated software.
38. How can I encrypt/decrypt zip files with zlib?
zlib doesn't support encryption. The original PKZIP encryption is very
weak and can be broken with freely available programs. To get strong
encryption, use GnuPG, https://www.gnupg.org/ , which already includes zlib
compression. For PKZIP compatible "encryption", look at
http://infozip.sourceforge.net/
39. What's the difference between the "gzip" and "deflate" HTTP 1.1 encodings?
"gzip" is the gzip format, and "deflate" is the zlib format. They should
probably have called the second one "zlib" instead to avoid confusion with
the raw deflate compressed data format. While the HTTP 1.1 RFC 2616
correctly points to the zlib specification in RFC 1950 for the "deflate"
transfer encoding, there have been reports of servers and browsers that
incorrectly produce or expect raw deflate data per the deflate
specification in RFC 1951, most notably Microsoft. So even though the
"deflate" transfer encoding using the zlib format would be the more
efficient approach (and in fact exactly what the zlib format was designed
for), using the "gzip" transfer encoding is probably more reliable due to
an unfortunate choice of name on the part of the HTTP 1.1 authors.
Bottom line: use the gzip format for HTTP 1.1 encoding.
40. Does zlib support the new "Deflate64" format introduced by PKWare?
No. PKWare has apparently decided to keep that format proprietary, since
they have not documented it as they have previous compression formats. In
any case, the compression improvements are so modest compared to other more
modern approaches, that it's not worth the effort to implement.
41. I'm having a problem with the zip functions in zlib, can you help?
There are no zip functions in zlib. You are probably using minizip by
Giles Vollant, which is found in the contrib directory of zlib. It is not
part of zlib. In fact none of the stuff in contrib is part of zlib. The
files in there are not supported by the zlib authors. You need to contact
the authors of the respective contribution for help.
42. The match.asm code in contrib is under the GNU General Public License.
Since it's part of zlib, doesn't that mean that all of zlib falls under the
GNU GPL?
No. The files in contrib are not part of zlib. They were contributed by
other authors and are provided as a convenience to the user within the zlib
distribution. Each item in contrib has its own license.
43. Is zlib subject to export controls? What is its ECCN?
zlib is not subject to export controls, and so is classified as EAR99.
44. Can you please sign these lengthy legal documents and fax them back to us
so that we can use your software in our product?
No. Go away. Shoo.
+36
View File
@@ -0,0 +1,36 @@
Contents
--------
| Name | Description |
|:-----------------|:---------------------------------------------------------------|
| arch/ | Architecture-specific code |
| doc/ | Documentation for formats and algorithms |
| test/example.c | Zlib usages examples for build testing |
| test/minigzip.c | Minimal gzip-like functionality for build testing |
| test/infcover.c | Inflate code coverage for build testing |
| win32/ | Shared library version resources for Windows |
| CMakeLists.txt | Cmake build script |
| configure | Bash configure/build script |
| adler32.c | Compute the Adler-32 checksum of a data stream |
| chunkset.* | Inline functions to copy small data chunks |
| compress.c | Compress a memory buffer |
| deflate.* | Compress data using the deflate algorithm |
| deflate_fast.c | Compress data using the deflate algorithm with fast strategy |
| deflate_medium.c | Compress data using the deflate algorithm with medium strategy |
| deflate_slow.c | Compress data using the deflate algorithm with slow strategy |
| functable.* | Struct containing function pointers to optimized functions |
| gzguts.h | Internal definitions for gzip operations |
| gzlib.c | Functions common to reading and writing gzip files |
| gzread.c | Read gzip files |
| gzwrite.c | Write gzip files |
| infback.* | Inflate using a callback interface |
| inflate.* | Decompress data |
| inffast.* | Decompress data with speed optimizations |
| inffixed_tbl.h | Table for decoding fixed codes |
| inftrees.h | Generate Huffman trees for efficient decoding |
| trees.* | Output deflated data using Huffman coding |
| uncompr.c | Decompress a memory buffer |
| zconf.h.cmakein | zconf.h template for cmake |
| zendian.h | BYTE_ORDER for endian tests |
| zlib.map | Linux symbol information |
| zlib.pc.in | Pkg-config template |
+19
View File
@@ -0,0 +1,19 @@
(C) 1995-2024 Jean-loup Gailly and Mark Adler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
+411
View File
@@ -0,0 +1,411 @@
# Makefile for zlib
# Copyright (C) 1995-2024 Jean-loup Gailly, Mark Adler
# For conditions of distribution and use, see copyright notice in zlib.h
# To compile and test, type:
# ./configure; make test
# Normally configure builds both a static and a shared library.
# If you want to build just a static library, use: ./configure --static
# To install /usr/local/lib/libz.* and /usr/local/include/zlib.h, type:
# make install
# To install in $HOME instead of /usr/local, use:
# make install prefix=$HOME
CC=cc
CFLAGS=-O
#CFLAGS=-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7
#CFLAGS=-g -DZLIB_DEBUG
#CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \
# -Wstrict-prototypes -Wmissing-prototypes
SFLAGS=-O
LDFLAGS=-L.
LIBNAME1=libz-ng
LIBNAME2=zlib-ng
SUFFIX=-ng
TEST_LIBS=$(LIBNAME1).a
LDSHARED=$(CC)
LDSHAREDFLAGS=-shared
LDVERSIONSCRIPT=
VER=2.2.4
VER1=2
STATICLIB=$(LIBNAME1).a
SHAREDLIB=$(LIBNAME1).so
SHAREDLIBV=$(LIBNAME1).so.$(VER)
SHAREDLIBM=$(LIBNAME1).so.$(VER1)
IMPORTLIB=
SHAREDTARGET=$(LIBNAME1).so.$(VER)
PKGFILE=$(LIBNAME2).pc
LIBS=$(STATICLIB) $(SHAREDTARGET)
AR=ar
ARFLAGS=rc
DEFFILE=
RC=
RCFLAGS=
RCOBJS=
STRIP=
RANLIB=ranlib
LDCONFIG=ldconfig
LDSHAREDLIBC=
EXE=
SRCDIR=.
INCLUDES=-I$(SRCDIR)
BUILDDIR=.
ARCHDIR=arch/generic
ARCH_STATIC_OBJS=
ARCH_SHARED_OBJS=
prefix = /usr/local
exec_prefix = ${prefix}
bindir = ${exec_prefix}/bin
libdir = ${exec_prefix}/lib
sharedlibdir = ${libdir}
includedir = ${prefix}/include
mandir = ${prefix}/share/man
man3dir = ${mandir}/man3
pkgconfigdir = ${libdir}/pkgconfig
OBJZ = \
arch/generic/adler32_c.o \
arch/generic/adler32_fold_c.o \
arch/generic/chunkset_c.o \
arch/generic/compare256_c.o \
arch/generic/crc32_braid_c.o \
arch/generic/crc32_c.o \
arch/generic/crc32_fold_c.o \
arch/generic/slide_hash_c.o \
adler32.o \
compress.o \
crc32.o \
crc32_braid_comb.o \
deflate.o \
deflate_fast.o \
deflate_huff.o \
deflate_medium.o \
deflate_quick.o \
deflate_rle.o \
deflate_slow.o \
deflate_stored.o \
functable.o \
infback.o \
inflate.o \
inftrees.o \
insert_string.o \
insert_string_roll.o \
trees.o \
uncompr.o \
zutil.o \
arch/generic/crc32_chorba_c.o \
cpu_features.o \
$(ARCH_STATIC_OBJS)
OBJG = \
gzlib.o \
gzread.o \
gzwrite.o
TESTOBJG =
OBJC = $(OBJZ) $(OBJG)
PIC_OBJZ = \
arch/generic/adler32_c.lo \
arch/generic/adler32_fold_c.lo \
arch/generic/chunkset_c.lo \
arch/generic/compare256_c.lo \
arch/generic/crc32_braid_c.lo \
arch/generic/crc32_c.lo \
arch/generic/crc32_fold_c.lo \
arch/generic/slide_hash_c.lo \
adler32.lo \
compress.lo \
crc32.lo \
crc32_braid_comb.lo \
deflate.lo \
deflate_fast.lo \
deflate_huff.lo \
deflate_medium.lo \
deflate_quick.lo \
deflate_rle.lo \
deflate_slow.lo \
deflate_stored.lo \
functable.lo \
infback.lo \
inflate.lo \
inftrees.lo \
insert_string.lo \
insert_string_roll.lo \
trees.lo \
uncompr.lo \
zutil.lo \
arch/generic/crc32_chorba_c.lo \
cpu_features.lo \
$(ARCH_SHARED_OBJS)
PIC_OBJG = \
gzlib.lo \
gzread.lo \
gzwrite.lo
PIC_TESTOBJG =
PIC_OBJC = $(PIC_OBJZ) $(PIC_OBJG)
OBJS = $(OBJC)
PIC_OBJS = $(PIC_OBJC)
all: static shared
static: example$(EXE) minigzip$(EXE) makefixed$(EXE) maketrees$(EXE) makecrct$(EXE)
shared: examplesh$(EXE) minigzipsh$(EXE)
check: test
.SECONDARY:
$(ARCHDIR)/%.o: $(SRCDIR)/$(ARCHDIR)/%.c
$(MAKE) -C $(ARCHDIR) $(notdir $@)
$(ARCHDIR)/%.lo: $(SRCDIR)/$(ARCHDIR)/%.c
$(MAKE) -C $(ARCHDIR) $(notdir $@)
arch/generic/%.o: $(SRCDIR)/arch/generic/%.c
$(MAKE) -C arch/generic $(notdir $@)
arch/generic/%.lo: $(SRCDIR)/arch/generic/%.c
$(MAKE) -C arch/generic $(notdir $@)
%.o: $(ARCHDIR)/%.o
-cp $< $@
%.lo: $(ARCHDIR)/%.lo
-cp $< $@
test: all
$(MAKE) -C test
infcover.o: $(SRCDIR)/test/infcover.c zlib$(SUFFIX).h zconf$(SUFFIX).h zlib_name_mangling$(SUFFIX).h
$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/test/infcover.c
infcover$(EXE): infcover.o $(STATICLIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ infcover.o $(STATICLIB)
ifneq ($(STRIP),)
$(STRIP) $@
endif
cover: infcover$(EXE)
rm -f *.gcda
./infcover
gcov inf*.c
$(STATICLIB): $(OBJS)
$(AR) $(ARFLAGS) $@ $(OBJS)
-@ ($(RANLIB) $@ || true) >/dev/null 2>&1
example.o:
$(CC) $(CFLAGS) -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $(SRCDIR)/test/example.c
minigzip.o:
$(CC) $(CFLAGS) -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $(SRCDIR)/test/minigzip.c
makefixed.o:
$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/tools/makefixed.c
maketrees.o:
$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/tools/maketrees.c
makecrct.o:
$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/tools/makecrct.c
zlibrc.o: $(SRCDIR)/win32/zlib$(SUFFIX)1.rc
$(RC) $(RCFLAGS) -o $@ $(SRCDIR)/win32/zlib$(SUFFIX)1.rc
.SUFFIXES: .lo
%.o: $(SRCDIR)/%.c
$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
%.lo: $(SRCDIR)/%.c
$(CC) $(SFLAGS) -DPIC $(INCLUDES) -c -o $@ $<
gzlib.o: $(SRCDIR)/gzlib.c
$(CC) $(CFLAGS) -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $<
gzlib.lo: $(SRCDIR)/gzlib.c
$(CC) $(SFLAGS) -DPIC -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $<
gzread.o: gzread.c
$(CC) $(CFLAGS) -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $<
gzread.lo: gzread.c
$(CC) $(SFLAGS) -DPIC -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $<
gzwrite.o: $(SRCDIR)/gzwrite.c
$(CC) $(CFLAGS) -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $<
gzwrite.lo: $(SRCDIR)/gzwrite.c
$(CC) $(SFLAGS) -DPIC -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $<
$(SHAREDTARGET): $(PIC_OBJS) $(DEFFILE) $(RCOBJS)
ifneq ($(SHAREDTARGET),)
$(LDSHARED) $(CFLAGS) $(LDSHAREDFLAGS) $(LDVERSIONSCRIPT) $(LDFLAGS) -o $@ $(DEFFILE) $(PIC_OBJS) $(RCOBJS) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
$(STRIP) $@
endif
ifneq ($(SHAREDLIB),$(SHAREDTARGET))
rm -f $(SHAREDLIB) $(SHAREDLIBM)
ln -s $@ $(SHAREDLIB)
ln -s $@ $(SHAREDLIBM)
endif
endif
example$(EXE): example.o $(TESTOBJG) $(STATICLIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ example.o $(TESTOBJG) $(TEST_LIBS) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
$(STRIP) $@
endif
minigzip$(EXE): minigzip.o $(TESTOBJG) $(STATICLIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ minigzip.o $(TESTOBJG) $(TEST_LIBS) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
$(STRIP) $@
endif
minigzipsh$(EXE): minigzip.o $(PIC_TESTOBJG) $(SHAREDTARGET)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ minigzip.o $(PIC_TESTOBJG) $(SHAREDLIB) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
$(STRIP) $@
endif
examplesh$(EXE): example.o $(PIC_TESTOBJG) $(SHAREDTARGET)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ example.o $(PIC_TESTOBJG) $(SHAREDLIB) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
$(STRIP) $@
endif
makefixed$(EXE): makefixed.o $(STATICLIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ makefixed.o $(TEST_LIBS) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
$(STRIP) $@
endif
maketrees$(EXE): maketrees.o $(STATICLIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ maketrees.o $(TEST_LIBS) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
$(STRIP) $@
endif
makecrct$(EXE): makecrct.o $(STATICLIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ makecrct.o $(TEST_LIBS) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
$(STRIP) $@
endif
install-shared: $(SHAREDTARGET)
ifneq ($(SHAREDTARGET),)
-@if [ ! -d $(DESTDIR)$(sharedlibdir) ]; then mkdir -p $(DESTDIR)$(sharedlibdir); fi
rm -f $(DESTDIR)$(sharedlibdir)/$(SHAREDTARGET)
cp $(SHAREDTARGET) $(DESTDIR)$(sharedlibdir)
chmod 755 $(DESTDIR)$(sharedlibdir)/$(SHAREDTARGET)
ifneq ($(SHAREDLIB),$(SHAREDTARGET))
rm -f $(DESTDIR)$(sharedlibdir)/$(SHAREDLIB) $(DESTDIR)$(sharedlibdir)/$(SHAREDLIBM)
ln -s $(SHAREDLIBV) $(DESTDIR)$(sharedlibdir)/$(SHAREDLIB)
ln -s $(SHAREDLIBV) $(DESTDIR)$(sharedlibdir)/$(SHAREDLIBM)
($(LDCONFIG) || true) >/dev/null 2>&1
# ldconfig is for Linux
endif
ifneq ($(IMPORTLIB),)
cp $(IMPORTLIB) $(DESTDIR)$(sharedlibdir)
chmod 644 $(DESTDIR)$(sharedlibdir)/$(IMPORTLIB)
endif
endif
install-static: $(STATICLIB)
-@if [ ! -d $(DESTDIR)$(libdir) ]; then mkdir -p $(DESTDIR)$(libdir); fi
rm -f $(DESTDIR)$(libdir)/$(STATICLIB)
cp $(STATICLIB) $(DESTDIR)$(libdir)
chmod 644 $(DESTDIR)$(libdir)/$(STATICLIB)
-@($(RANLIB) $(DESTDIR)$(libdir)/$(STATICLIB) || true) >/dev/null 2>&1
# The ranlib in install-static is needed on NeXTSTEP which checks file times
install-libs: install-shared install-static
-@if [ ! -d $(DESTDIR)$(man3dir) ]; then mkdir -p $(DESTDIR)$(man3dir); fi
-@if [ ! -d $(DESTDIR)$(pkgconfigdir) ]; then mkdir -p $(DESTDIR)$(pkgconfigdir); fi
rm -f $(DESTDIR)$(pkgconfigdir)/$(PKGFILE)
cp $(PKGFILE) $(DESTDIR)$(pkgconfigdir)
chmod 644 $(DESTDIR)$(pkgconfigdir)/$(PKGFILE)
install: install-libs
-@if [ ! -d $(DESTDIR)$(includedir) ]; then mkdir -p $(DESTDIR)$(includedir); fi
rm -f $(DESTDIR)$(includedir)/zlib$(SUFFIX).h $(DESTDIR)$(includedir)/zconf$(SUFFIX).h $(DESTDIR)$(includedir)/zlib_name_mangling$(SUFFIX).h
cp zlib$(SUFFIX).h $(DESTDIR)$(includedir)/zlib$(SUFFIX).h
cp zconf$(SUFFIX).h $(DESTDIR)$(includedir)/zconf$(SUFFIX).h
cp zlib_name_mangling$(SUFFIX).h $(DESTDIR)$(includedir)/zlib_name_mangling$(SUFFIX).h
chmod 644 $(DESTDIR)$(includedir)/zlib$(SUFFIX).h $(DESTDIR)$(includedir)/zconf$(SUFFIX).h $(DESTDIR)$(includedir)/zlib_name_mangling$(SUFFIX).h
uninstall-static:
cd $(DESTDIR)$(libdir) && rm -f $(STATICLIB)
uninstall-shared:
ifneq ($(SHAREDLIB),)
cd $(DESTDIR)$(sharedlibdir) && rm -f $(SHAREDLIBV) $(SHAREDLIB) $(SHAREDLIBM)
endif
ifneq ($(IMPORTLIB),)
cd $(DESTDIR)$(sharedlibdir) && rm -f $(IMPORTLIB)
endif
uninstall: uninstall-static uninstall-shared
cd $(DESTDIR)$(includedir) && rm -f zlib$(SUFFIX).h zconf$(SUFFIX).h zlib_name_mangling$(SUFFIX).h
cd $(DESTDIR)$(pkgconfigdir) && rm -f $(PKGFILE)
mostlyclean: clean
clean:
@if [ -f $(ARCHDIR)/Makefile ]; then $(MAKE) -C $(ARCHDIR) clean; fi
@if [ -f arch/generic/Makefile ]; then $(MAKE) -C arch/generic clean; fi
@if [ -f test/Makefile ]; then $(MAKE) -C test clean; fi
rm -f *.o *.lo *~ \
example$(EXE) minigzip$(EXE) minigzipsh$(EXE) \
infcover makefixed$(EXE) maketrees$(EXE) makecrct$(EXE) \
$(STATICLIB) $(IMPORTLIB) $(SHAREDLIB) $(SHAREDLIBV) $(SHAREDLIBM) \
foo.gz so_locations \
_match.s maketree
rm -rf objs
rm -f *.gcda *.gcno *.gcov
rm -f a.out a.exe
rm -f *._h
rm -rf btmp1 btmp2 pkgtmp1 pkgtmp2
maintainer-clean: distclean
distclean: clean
@if [ -f $(ARCHDIR)/Makefile ]; then $(MAKE) -C $(ARCHDIR) distclean; fi
@if [ -f test/Makefile ]; then $(MAKE) -C test distclean; fi
rm -f $(PKGFILE) configure.log zconf.h zconf.h.cmakein zlib$(SUFFIX).h zlib_name_mangling$(SUFFIX).h *.pc gzread.c
-@rm -f .DS_Store
# Reset Makefile if building inside source tree
@if [ -f Makefile.in ]; then \
printf 'all:\n\t-@echo "Please use ./configure first. Thank you."\n' > Makefile ; \
printf '\ndistclean:\n\t$(MAKE) -f Makefile.in distclean\n' >> Makefile ; \
touch -r $(SRCDIR)/Makefile.in Makefile ; fi
# Reset zconf.h and zconf.h.cmakein if building inside source tree
@if [ -f zconf.h.in ]; then \
cp -p $(SRCDIR)/zconf.h.in zconf.h ; \
grep -v '^#cmakedefine' $(SRCDIR)/zconf.h.in > zconf.h.cmakein &&\
touch -r $(SRCDIR)/zconf.h.in zconf.h.cmakein ; fi
# Cleanup these files if building outside source tree
@if [ ! -f README.md ]; then rm -f Makefile; fi
# Remove arch and test directory if building outside source tree
@if [ ! -f $(ARCHDIR)/Makefile.in ]; then rm -rf arch; fi
@if [ ! -f test/Makefile.in ]; then rm -rf test; fi
tags:
etags $(SRCDIR)/*.[ch]
+111
View File
@@ -0,0 +1,111 @@
Porting applications to use zlib-ng
===================================
Zlib-ng can be used/compiled in two different modes, that require some
consideration by the application developer.
Changes from zlib affecting native and compat modes
---------------------------------------------------
Zlib-ng is not as conservative with memory allocation as Zlib is.
Where Zlib's inflate will allocate a lower amount of memory depending on
compression level and window size, zlib-ng will always allocate the maximum
amount of memory and possibly leave parts of it unused.
Zlib-ng's deflate will however allocate a lower amount of memory depending
on compression level and window size.
Zlib-ng also allocates one "big" buffer instead of doing multiple smaller
allocations. This is faster, can lead to better cache locality and reduces
space lost to alignment padding.
At the time of writing, by default zlib-ng allocates the following amounts
of memory on a 64-bit system (except on S390x that requires ~4KiB more):
- Deflate: 350.272 Bytes
- Inflate: 42.112 Bytes
**Advantages:**
- All memory is allocated during DeflateInit or InflateInit functions,
leaving the actual deflate/inflate functions free from allocations.
- Zlib-ng can only fail from memory allocation errors during init.
- Time spent doing memory allocation systemcalls is all done during init,
allowing applications to do prepare this before doing latency-sensitive
deflate/inflate later.
- Can reduce wasted memory due to buffer alignment padding both by OS and zlib-ng.
- Potentially improved memory locality.
**Disadvantages:**
- Zlib-ng allocates a little more memory than zlib does.
zlib-compat mode
----------------
Zlib-ng can be compiled in zlib-compat mode, suitable for zlib-replacement
in a single application or system-wide.
Please note that zlib-ng in zlib-compat mode tries to maintain both API and
ABI compatibility with the original zlib. Any issues regarding compatibility
can be reported as bugs.
In certain instances you may not be able to simply replace the zlib library/dll
files and expect the application to work. The application may need to be
recompiled against the zlib-ng headers and libs to ensure full compatibility.
It is also possible for the deflate output stream to differ from the original
zlib due to algorithmic differences between the two libraries. Any tests or
applications that depend on the exact length of the deflate stream being a
certain value will need to be updated.
**Advantages:**
- Easy to port to, since it only requires a recompile of the application and
no changes to the application code.
**Disadvantages:**
- Can conflict with a system-installed zlib, as that can often be linked in
by another library you are linking into your application. This can cause
crashes or incorrect output.
- If your application is pre-allocating a memory buffer and you are providing
deflate/inflate init with your own allocator that allocates from that buffer
(looking at you nginx), you should be aware that zlib-ng needs to allocate
more memory than stock zlib needs. The same problem exists with Intel’s and
Cloudflare’s zlib forks. Doing this is not recommended since it makes it
very hard to maintain compatibility over time.
**Build Considerations:**
- Compile against the *zlib.h* provided by zlib-ng
- Configuration header is named *zconf.h*
- Static library is *libz.a* on Unix and macOS, or *zlib.lib* on Windows
- Shared library is *libz.so* on Unix, *libz.dylib* on macOS, or *zlib1.dll*
on Windows
- Type `z_size_t` is *unsigned __int64* on 64-bit Windows, and *unsigned long* on 32-bit Windows, Unix and macOS
- Type `z_uintmax_t` is *unsigned long* in zlib-compat mode, and *size_t* with zlib-ng API
zlib-ng native mode
-------------------
Zlib-ng in native mode is suitable for co-existing with the standard zlib
library, allowing applications to implement support and testing separately.
The zlib-ng native has implemented some modernization and simplifications
in its API, intended to make life easier for application developers.
**Advantages:**
- Does not conflict with other zlib implementations, and can co-exist as a
system library along with zlib.
- In certain places zlib-ng native uses more appropriate data types, removing
the need for some workarounds in the API compared to zlib.
**Disadvantages:**
- Requires minor changes to applications to use the prefixed zlib-ng
function calls and structs. Usually this means a small prefix `zng_` has to be added.
**Build Considerations:**
- Compile against *zlib-ng.h*
- Configuration header is named *zconf-ng.h*
- Static library is *libz-ng.a* on Unix and macOS, or *zlib-ng.lib* on Windows
- Shared library is *libz-ng.so* on Unix, *libz-ng.dylib* on macOS, or
*zlib-ng2.dll* on Windows
- Type `z_size_t` is *size_t*
zlib-ng compile-time detection
------------------------------
To distinguish zlib-ng from other zlib implementations at compile-time check for the
existence of `ZLIBNG_VERSION` defined in the zlib header.
+228
View File
@@ -0,0 +1,228 @@
| CI | Stable | Develop |
|:---|:-------|:--------|
| GitHub Actions | [![Stable CMake](https://github.com/zlib-ng/zlib-ng/actions/workflows/cmake.yml/badge.svg?branch=stable)](https://github.com/zlib-ng/zlib-ng/actions/workflows/cmake.yml?query=branch%3Astable) <br> [![Stable Configure](https://github.com/zlib-ng/zlib-ng/actions/workflows/configure.yml/badge.svg?branch=stable)](https://github.com/zlib-ng/zlib-ng/actions/workflows/configure.yml?query=branch%3Astable) <br> [![Stable NMake](https://github.com/zlib-ng/zlib-ng/actions/workflows/nmake.yml/badge.svg?branch=stable)](https://github.com/zlib-ng/zlib-ng/actions/workflows/nmake.yml?query=branch%3Astable) | [![Develop CMake](https://github.com/zlib-ng/zlib-ng/actions/workflows/cmake.yml/badge.svg?branch=develop)](https://github.com/zlib-ng/zlib-ng/actions/workflows/cmake.yml?query=branch%3Adevelop) <br> [![Develop Configure](https://github.com/zlib-ng/zlib-ng/actions/workflows/configure.yml/badge.svg?branch=develop)](https://github.com/zlib-ng/zlib-ng/actions/workflows/configure.yml?query=branch%3Adevelop) <br> [![Develop NMake](https://github.com/zlib-ng/zlib-ng/actions/workflows/nmake.yml/badge.svg?branch=develop)](https://github.com/zlib-ng/zlib-ng/actions/workflows/nmake.yml?query=branch%3Adevelop) |
| CodeFactor | [![CodeFactor](https://www.codefactor.io/repository/github/zlib-ng/zlib-ng/badge/stable)](https://www.codefactor.io/repository/github/zlib-ng/zlib-ng/overview/stable) | [![CodeFactor](https://www.codefactor.io/repository/github/zlib-ng/zlib-ng/badge/develop)](https://www.codefactor.io/repository/github/zlib-ng/zlib-ng/overview/develop) |
| OSS-Fuzz | [![Fuzzing Status](https://oss-fuzz-build-logs.storage.googleapis.com/badges/zlib-ng.svg)](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:zlib-ng) | [![Fuzzing Status](https://oss-fuzz-build-logs.storage.googleapis.com/badges/zlib-ng.svg)](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:zlib-ng) |
| Codecov | [![codecov](https://codecov.io/github/zlib-ng/zlib-ng/branch/stable/graph/badge.svg?token=uKsgK9LIuC)](https://codecov.io/github/zlib-ng/zlib-ng/tree/stable) | [![codecov](https://codecov.io/github/zlib-ng/zlib-ng/branch/develop/graph/badge.svg?token=uKsgK9LIuC)](https://codecov.io/github/zlib-ng/zlib-ng/tree/develop) |
## zlib-ng
*zlib data compression library for the next generation systems*
Maintained by Hans Kristian Rosbach
aka Dead2 (zlib-ng àt circlestorm dót org)
Features
--------
* Zlib compatible API with support for dual-linking
* Modernized native API based on zlib API for ease of porting
* Modern C11 syntax and a clean code layout
* Deflate medium and quick algorithms based on Intel’s zlib fork
* Support for CPU intrinsics when available
* Adler32 implementation using SSSE3, AVX2, AVX512, AVX512-VNNI, Neon, VMX & VSX
* CRC32-B implementation using PCLMULQDQ, VPCLMULQDQ, ARMv8, & IBM Z
* Slide hash implementations using SSE2, AVX2, ARMv6, Neon, VMX & VSX
* Compare256 implementations using SSE2, AVX2, Neon, POWER9 & RVV
* Inflate chunk copying using SSE2, SSSE3, AVX, Neon & VSX
* Support for hardware-accelerated deflate using IBM Z DFLTCC
* Safe unaligned memory read/writes and large bit buffer improvements
* Includes improvements from Cloudflare and Intel forks
* Configure, CMake, and NMake build system support
* Comprehensive set of CMake unit tests
* Code sanitizers, fuzzing, and coverage
* GitHub Actions continuous integration on Windows, macOS, and Linux
* Emulated CI for ARM, AARCH64, PPC, PPC64, RISCV, SPARC64, S390x using qemu
History
-------
The motivation for this fork was seeing several 3rd party contributions with new optimizations not getting
implemented into the official zlib repository.
Mark Adler has been maintaining zlib for a very long time, and he has done a great job and hopefully he will continue
for a long time yet. The idea of zlib-ng is not to replace zlib, but to co-exist as a drop-in replacement with a
lower threshold for code change.
zlib has a long history and is incredibly portable, even supporting many systems that predate the Internet.<br>
That is great, but it can complicate further development and maintainability. The zlib code contains many workarounds
for really old compilers or to accommodate systems with limitations such as operating in a 16-bit environment.
Many of these workarounds are only maintenance burdens, some of them are pretty huge code-wise. With many workarounds
cluttered throughout the code, it makes it harder for new programmers with an idea/interest for zlib to contribute.
I decided to make a fork, merge all the Intel optimizations, some of the Cloudflare optimizations, plus a couple other
smaller patches. Then started cleaning out workarounds, various dead code, all contrib and example code.<br>
The result is a better performing and easier to maintain zlib-ng.
A lot of improvements have gone into zlib-ng since its start, and numerous people and companies have contributed both
small and big improvements, or valuable testing.
Build
-----
<sup>Please read LICENSE.md, it is very simple and very liberal.</sup>
There are two ways to build zlib-ng:
### Cmake
To build zlib-ng using the cross-platform makefile generator cmake.
```
cmake .
cmake --build . --config Release
ctest --verbose -C Release
```
Alternatively, you can use the cmake configuration GUI tool ccmake:
```
ccmake .
```
### Configure
To build zlib-ng using the bash configure script:
```
./configure
make
make test
```
Build Options
-------------
| CMake | configure | Description | Default |
|:---------------------------|:-------------------------|:------------------------------------------------------------------------------------|---------|
| ZLIB_COMPAT | --zlib-compat | Compile with zlib compatible API | OFF |
| ZLIB_ENABLE_TESTS | | Build test binaries | ON |
| WITH_GZFILEOP | --without-gzfileops | Compile with support for gzFile related functions | ON |
| WITH_OPTIM | --without-optimizations | Build with optimisations | ON |
| WITH_NEW_STRATEGIES | --without-new-strategies | Use new strategies | ON |
| WITH_NATIVE_INSTRUCTIONS | | Compiles with full instruction set supported on this host (gcc/clang -march=native) | OFF |
| WITH_RUNTIME_CPU_DETECTION | | Compiles with runtime CPU detection | ON |
| WITH_SANITIZER | | Build with sanitizer (memory, address, undefined) | OFF |
| WITH_GTEST | | Build gtest_zlib | ON |
| WITH_FUZZERS | | Build test/fuzz | OFF |
| WITH_BENCHMARKS | | Build test/benchmarks | OFF |
| WITH_MAINTAINER_WARNINGS | | Build with project maintainer warnings | OFF |
| WITH_CODE_COVERAGE | | Enable code coverage reporting | OFF |
Install
-------
WARNING: We do not recommend manually installing unless you really know what you are doing, because this can
potentially override the system default zlib library, and any incompatibility or wrong configuration of zlib-ng
can make the whole system unusable, requiring recovery or reinstall.
If you still want a manual install, we recommend using the /opt/ path prefix.
For Linux distros, an alternative way to use zlib-ng (if compiled in zlib-compat mode) instead of zlib, is through
the use of the _LD_PRELOAD_ environment variable. If the program is dynamically linked with zlib, then the program
will temporarily attempt to use zlib-ng instead, without risking system-wide instability.
```
LD_PRELOAD=/opt/zlib-ng/libz.so.1.2.13.zlib-ng /usr/bin/program
```
### Cmake
To install zlib-ng system-wide using cmake:
```sh or powershell
cmake --build . --target install
```
### Configure
To install zlib-ng system-wide using the configure script:
```sh
make install
```
### CPack
After building with cmake, an installation package can be created using cpack. By default a tgz package is created,
but you can append `-G <format>` to each command to generate alternative packages types (TGZ, ZIP, RPM, DEB). To easily
create a rpm or deb package, you would use `-G RPM` or `-G DEB` respectively.
```sh or powershell
cd build
cpack --config CPackConfig.cmake
cpack --config CPackSourceConfig.cmake
```
### Vcpkg
Alternatively, you can build and install zlib-ng using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager:
```sh or powershell
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh # "./bootstrap-vcpkg.bat" for powershell
./vcpkg integrate install
./vcpkg install zlib-ng
```
The zlib-ng port in vcpkg is kept up to date by Microsoft team members and community contributors.
If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
Contributing
------------
Zlib-ng is aiming to be open to contributions, and we would be delighted to receive pull requests on github.
Help with testing and reviewing pull requests etc is also very much appreciated.
Please check the Wiki for more info: [Contributing](https://github.com/zlib-ng/zlib-ng/wiki/Contributing)
Acknowledgments
----------------
Thanks go out to all the people and companies who have taken the time to contribute
code reviews, testing and/or patches. Zlib-ng would not have been nearly as good without you.
The deflate format used by zlib was defined by Phil Katz.<br>
The deflate and zlib specifications were written by L. Peter Deutsch.
zlib was originally created by Jean-loup Gailly (compression) and Mark Adler (decompression).
Advanced Build Options
----------------------
| CMake | configure | Description | Default |
|:--------------------------------|:----------------------|:--------------------------------------------------------------------|------------------------|
| FORCE_SSE2 | --force-sse2 | Skip runtime check for SSE2 instructions (Always on for x86_64) | OFF (x86) |
| WITH_AVX2 | | Build with AVX2 intrinsics | ON |
| WITH_AVX512 | | Build with AVX512 intrinsics | ON |
| WITH_AVX512VNNI | | Build with AVX512VNNI intrinsics | ON |
| WITH_SSE2 | | Build with SSE2 intrinsics | ON |
| WITH_SSSE3 | | Build with SSSE3 intrinsics | ON |
| WITH_SSE42 | | Build with SSE42 intrinsics | ON |
| WITH_PCLMULQDQ | | Build with PCLMULQDQ intrinsics | ON |
| WITH_VPCLMULQDQ | --without-vpclmulqdq | Build with VPCLMULQDQ intrinsics | ON |
| WITH_ARMV8 | --without-armv8 | Build with ARMv8 intrinsics | ON |
| WITH_NEON | --without-neon | Build with NEON intrinsics | ON |
| WITH_ARMV6 | --without-armv6 | Build with ARMv6 intrinsics | ON |
| WITH_ALTIVEC | --without-altivec | Build with AltiVec (VMX) intrinsics | ON |
| WITH_POWER8 | --without-power8 | Build with POWER8 optimisations | ON |
| WITH_RVV | | Build with RVV intrinsics | ON |
| WITH_CRC32_VX | --without-crc32-vx | Build with vectorized CRC32 on IBM Z | ON |
| WITH_DFLTCC_DEFLATE | --with-dfltcc-deflate | Build with DFLTCC intrinsics for compression on IBM Z | OFF |
| WITH_DFLTCC_INFLATE | --with-dfltcc-inflate | Build with DFLTCC intrinsics for decompression on IBM Z | OFF |
| WITH_INFLATE_STRICT | | Build with strict inflate distance checking | OFF |
| WITH_INFLATE_ALLOW_INVALID_DIST | | Build with zero fill for inflate invalid distances | OFF |
| INSTALL_UTILS | | Copy minigzip and minideflate during install | OFF |
| ZLIBNG_ENABLE_TESTS | | Test zlib-ng specific API | ON |
Related Projects
----------------
* Fork of the popular minizip https://github.com/zlib-ng/minizip-ng
* Python tool to benchmark minigzip/minideflate https://github.com/zlib-ng/deflatebench
* Python tool to benchmark pigz https://github.com/zlib-ng/pigzbench
* 3rd party patches for zlib-ng compatibility https://github.com/zlib-ng/patches
+69
View File
@@ -0,0 +1,69 @@
/* adler32.c -- compute the Adler-32 checksum of a data stream
* Copyright (C) 1995-2011, 2016 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "functable.h"
#include "adler32_p.h"
#ifdef ZLIB_COMPAT
unsigned long Z_EXPORT PREFIX(adler32_z)(unsigned long adler, const unsigned char *buf, size_t len) {
return (unsigned long)FUNCTABLE_CALL(adler32)((uint32_t)adler, buf, len);
}
#else
uint32_t Z_EXPORT PREFIX(adler32_z)(uint32_t adler, const unsigned char *buf, size_t len) {
return FUNCTABLE_CALL(adler32)(adler, buf, len);
}
#endif
/* ========================================================================= */
#ifdef ZLIB_COMPAT
unsigned long Z_EXPORT PREFIX(adler32)(unsigned long adler, const unsigned char *buf, unsigned int len) {
return (unsigned long)FUNCTABLE_CALL(adler32)((uint32_t)adler, buf, len);
}
#else
uint32_t Z_EXPORT PREFIX(adler32)(uint32_t adler, const unsigned char *buf, uint32_t len) {
return FUNCTABLE_CALL(adler32)(adler, buf, len);
}
#endif
/* ========================================================================= */
static uint32_t adler32_combine_(uint32_t adler1, uint32_t adler2, z_off64_t len2) {
uint32_t sum1;
uint32_t sum2;
unsigned rem;
/* for negative len, return invalid adler32 as a clue for debugging */
if (len2 < 0)
return 0xffffffff;
/* the derivation of this formula is left as an exercise for the reader */
len2 %= BASE; /* assumes len2 >= 0 */
rem = (unsigned)len2;
sum1 = adler1 & 0xffff;
sum2 = rem * sum1;
sum2 %= BASE;
sum1 += (adler2 & 0xffff) + BASE - 1;
sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
if (sum1 >= BASE) sum1 -= BASE;
if (sum1 >= BASE) sum1 -= BASE;
if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1);
if (sum2 >= BASE) sum2 -= BASE;
return sum1 | (sum2 << 16);
}
/* ========================================================================= */
#ifdef ZLIB_COMPAT
unsigned long Z_EXPORT PREFIX(adler32_combine)(unsigned long adler1, unsigned long adler2, z_off_t len2) {
return (unsigned long)adler32_combine_((uint32_t)adler1, (uint32_t)adler2, len2);
}
unsigned long Z_EXPORT PREFIX4(adler32_combine)(unsigned long adler1, unsigned long adler2, z_off64_t len2) {
return (unsigned long)adler32_combine_((uint32_t)adler1, (uint32_t)adler2, len2);
}
#else
uint32_t Z_EXPORT PREFIX4(adler32_combine)(uint32_t adler1, uint32_t adler2, z_off64_t len2) {
return adler32_combine_(adler1, adler2, len2);
}
#endif
+70
View File
@@ -0,0 +1,70 @@
/* adler32_p.h -- Private inline functions and macros shared with
* different computation of the Adler-32 checksum
* of a data stream.
* Copyright (C) 1995-2011, 2016 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef ADLER32_P_H
#define ADLER32_P_H
#define BASE 65521U /* largest prime smaller than 65536 */
#define NMAX 5552
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
#define ADLER_DO1(sum1, sum2, buf, i) {(sum1) += buf[(i)]; (sum2) += (sum1);}
#define ADLER_DO2(sum1, sum2, buf, i) {ADLER_DO1(sum1, sum2, buf, i); ADLER_DO1(sum1, sum2, buf, i+1);}
#define ADLER_DO4(sum1, sum2, buf, i) {ADLER_DO2(sum1, sum2, buf, i); ADLER_DO2(sum1, sum2, buf, i+2);}
#define ADLER_DO8(sum1, sum2, buf, i) {ADLER_DO4(sum1, sum2, buf, i); ADLER_DO4(sum1, sum2, buf, i+4);}
#define ADLER_DO16(sum1, sum2, buf) {ADLER_DO8(sum1, sum2, buf, 0); ADLER_DO8(sum1, sum2, buf, 8);}
static inline uint32_t adler32_len_1(uint32_t adler, const uint8_t *buf, uint32_t sum2) {
adler += buf[0];
adler %= BASE;
sum2 += adler;
sum2 %= BASE;
return adler | (sum2 << 16);
}
static inline uint32_t adler32_len_16(uint32_t adler, const uint8_t *buf, size_t len, uint32_t sum2) {
while (len) {
--len;
adler += *buf++;
sum2 += adler;
}
adler %= BASE;
sum2 %= BASE; /* only added so many BASE's */
/* return recombined sums */
return adler | (sum2 << 16);
}
static inline uint32_t adler32_copy_len_16(uint32_t adler, const uint8_t *buf, uint8_t *dst, size_t len, uint32_t sum2) {
while (len--) {
*dst = *buf++;
adler += *dst++;
sum2 += adler;
}
adler %= BASE;
sum2 %= BASE; /* only added so many BASE's */
/* return recombined sums */
return adler | (sum2 << 16);
}
static inline uint32_t adler32_len_64(uint32_t adler, const uint8_t *buf, size_t len, uint32_t sum2) {
#ifdef UNROLL_MORE
while (len >= 16) {
len -= 16;
ADLER_DO16(adler, sum2, buf);
buf += 16;
#else
while (len >= 8) {
len -= 8;
ADLER_DO8(adler, sum2, buf, 0);
buf += 8;
#endif
}
/* Process tail (len < 16). */
return adler32_len_16(adler, buf, len, sum2);
}
#endif /* ADLER32_P_H */
+29
View File
@@ -0,0 +1,29 @@
/* arch_functions.h -- Arch-specific function prototypes.
* Copyright (C) 2017 Hans Kristian Rosbach
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef CPU_FUNCTIONS_H_
#define CPU_FUNCTIONS_H_
#include "zbuild.h"
#include "zutil.h"
#include "crc32.h"
#include "deflate.h"
#include "fallback_builtins.h"
#include "arch/generic/generic_functions.h"
#if defined(X86_FEATURES)
# include "arch/x86/x86_functions.h"
#elif defined(ARM_FEATURES)
# include "arch/arm/arm_functions.h"
#elif defined(PPC_FEATURES) || defined(POWER_FEATURES)
# include "arch/power/power_functions.h"
#elif defined(S390_FEATURES)
# include "arch/s390/s390_functions.h"
#elif defined(RISCV_FEATURES)
# include "arch/riscv/riscv_functions.h"
#endif
#endif
+283
View File
@@ -0,0 +1,283 @@
/* chunkset_tpl.h -- inline functions to copy small data chunks.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include <stdlib.h>
/* Returns the chunk size */
Z_INTERNAL uint32_t CHUNKSIZE(void) {
return sizeof(chunk_t);
}
/* Behave like memcpy, but assume that it's OK to overwrite at least
chunk_t bytes of output even if the length is shorter than this,
that the length is non-zero, and that `from` lags `out` by at least
sizeof chunk_t bytes (or that they don't overlap at all or simply that
the distance is less than the length of the copy).
Aside from better memory bus utilisation, this means that short copies
(chunk_t bytes or fewer) will fall straight through the loop
without iteration, which will hopefully make the branch prediction more
reliable. */
#ifndef HAVE_CHUNKCOPY
static inline uint8_t* CHUNKCOPY(uint8_t *out, uint8_t const *from, unsigned len) {
Assert(len > 0, "chunkcopy should never have a length 0");
chunk_t chunk;
int32_t align = ((len - 1) % sizeof(chunk_t)) + 1;
loadchunk(from, &chunk);
storechunk(out, &chunk);
out += align;
from += align;
len -= align;
while (len > 0) {
loadchunk(from, &chunk);
storechunk(out, &chunk);
out += sizeof(chunk_t);
from += sizeof(chunk_t);
len -= sizeof(chunk_t);
}
return out;
}
#endif
/* Perform short copies until distance can be rewritten as being at least
sizeof chunk_t.
This assumes that it's OK to overwrite at least the first
2*sizeof(chunk_t) bytes of output even if the copy is shorter than this.
This assumption holds because inflate_fast() starts every iteration with at
least 258 bytes of output space available (258 being the maximum length
output from a single token; see inflate_fast()'s assumptions below). */
#ifndef HAVE_CHUNKUNROLL
static inline uint8_t* CHUNKUNROLL(uint8_t *out, unsigned *dist, unsigned *len) {
unsigned char const *from = out - *dist;
chunk_t chunk;
while (*dist < *len && *dist < sizeof(chunk_t)) {
loadchunk(from, &chunk);
storechunk(out, &chunk);
out += *dist;
*len -= *dist;
*dist += *dist;
}
return out;
}
#endif
#ifndef HAVE_CHUNK_MAG
/* Loads a magazine to feed into memory of the pattern */
static inline chunk_t GET_CHUNK_MAG(uint8_t *buf, uint32_t *chunk_rem, uint32_t dist) {
/* This code takes string of length dist from "from" and repeats
* it for as many times as can fit in a chunk_t (vector register) */
uint64_t cpy_dist;
uint64_t bytes_remaining = sizeof(chunk_t);
chunk_t chunk_load;
uint8_t *cur_chunk = (uint8_t *)&chunk_load;
while (bytes_remaining) {
cpy_dist = MIN(dist, bytes_remaining);
memcpy(cur_chunk, buf, (size_t)cpy_dist);
bytes_remaining -= cpy_dist;
cur_chunk += cpy_dist;
/* This allows us to bypass an expensive integer division since we're effectively
* counting in this loop, anyway */
*chunk_rem = (uint32_t)cpy_dist;
}
return chunk_load;
}
#endif
#if defined(HAVE_HALF_CHUNK) && !defined(HAVE_HALFCHUNKCOPY)
static inline uint8_t* HALFCHUNKCOPY(uint8_t *out, uint8_t const *from, unsigned len) {
halfchunk_t chunk;
int32_t align = ((len - 1) % sizeof(halfchunk_t)) + 1;
loadhalfchunk(from, &chunk);
storehalfchunk(out, &chunk);
out += align;
from += align;
len -= align;
while (len > 0) {
loadhalfchunk(from, &chunk);
storehalfchunk(out, &chunk);
out += sizeof(halfchunk_t);
from += sizeof(halfchunk_t);
len -= sizeof(halfchunk_t);
}
return out;
}
#endif
/* Copy DIST bytes from OUT - DIST into OUT + DIST * k, for 0 <= k < LEN/DIST.
Return OUT + LEN. */
static inline uint8_t* CHUNKMEMSET(uint8_t *out, uint8_t *from, unsigned len) {
/* Debug performance related issues when len < sizeof(uint64_t):
Assert(len >= sizeof(uint64_t), "chunkmemset should be called on larger chunks"); */
Assert(from != out, "chunkmemset cannot have a distance 0");
chunk_t chunk_load;
uint32_t chunk_mod = 0;
uint32_t adv_amount;
int64_t sdist = out - from;
uint64_t dist = llabs(sdist);
/* We are supporting the case for when we are reading bytes from ahead in the buffer.
* We now have to handle this, though it wasn't _quite_ clear if this rare circumstance
* always needed to be handled here or if we're just now seeing it because we are
* dispatching to this function, more */
if (sdist < 0 && dist < len) {
#ifdef HAVE_MASKED_READWRITE
/* We can still handle this case if we can mitigate over writing _and_ we
* fit the entirety of the copy length with one load */
if (len <= sizeof(chunk_t)) {
/* Tempting to add a goto to the block below but hopefully most compilers
* collapse these identical code segments as one label to jump to */
return CHUNKCOPY(out, from, len);
}
#endif
/* Here the memmove semantics match perfectly, as when this happens we are
* effectively sliding down the contents of memory by dist bytes */
memmove(out, from, len);
return out + len;
}
if (dist == 1) {
memset(out, *from, len);
return out + len;
} else if (dist >= sizeof(chunk_t)) {
return CHUNKCOPY(out, from, len);
}
/* Only AVX2+ as there's 128 bit vectors and 256 bit. We allow for shorter vector
* lengths because they serve to allow more cases to fall into chunkcopy, as the
* distance of the shorter length is still deemed a safe distance. We rewrite this
* here rather than calling the ssse3 variant directly now because doing so required
* dispatching to another function and broke inlining for this function entirely. We
* also can merge an assert and some remainder peeling behavior into the same code blocks,
* making the code a little smaller. */
#ifdef HAVE_HALF_CHUNK
if (len <= sizeof(halfchunk_t)) {
if (dist >= sizeof(halfchunk_t))
return HALFCHUNKCOPY(out, from, len);
if ((dist % 2) != 0 || dist == 6) {
halfchunk_t halfchunk_load = GET_HALFCHUNK_MAG(from, &chunk_mod, (unsigned)dist);
if (len == sizeof(halfchunk_t)) {
storehalfchunk(out, &halfchunk_load);
len -= sizeof(halfchunk_t);
out += sizeof(halfchunk_t);
}
chunk_load = halfchunk2whole(&halfchunk_load);
goto rem_bytes;
}
}
#endif
#ifdef HAVE_CHUNKMEMSET_2
if (dist == 2) {
chunkmemset_2(from, &chunk_load);
} else
#endif
#ifdef HAVE_CHUNKMEMSET_4
if (dist == 4) {
chunkmemset_4(from, &chunk_load);
} else
#endif
#ifdef HAVE_CHUNKMEMSET_8
if (dist == 8) {
chunkmemset_8(from, &chunk_load);
} else
#endif
#ifdef HAVE_CHUNKMEMSET_16
if (dist == 16) {
chunkmemset_16(from, &chunk_load);
} else
#endif
chunk_load = GET_CHUNK_MAG(from, &chunk_mod, (unsigned)dist);
adv_amount = sizeof(chunk_t) - chunk_mod;
while (len >= (2 * sizeof(chunk_t))) {
storechunk(out, &chunk_load);
storechunk(out + adv_amount, &chunk_load);
out += 2 * adv_amount;
len -= 2 * adv_amount;
}
/* If we don't have a "dist" length that divides evenly into a vector
* register, we can write the whole vector register but we need only
* advance by the amount of the whole string that fits in our chunk_t.
* If we do divide evenly into the vector length, adv_amount = chunk_t size*/
while (len >= sizeof(chunk_t)) {
storechunk(out, &chunk_load);
len -= adv_amount;
out += adv_amount;
}
#ifdef HAVE_HALF_CHUNK
rem_bytes:
#endif
if (len) {
memcpy(out, &chunk_load, len);
out += len;
}
return out;
}
Z_INTERNAL uint8_t* CHUNKMEMSET_SAFE(uint8_t *out, uint8_t *from, unsigned len, unsigned left) {
#if OPTIMAL_CMP < 32
static const uint32_t align_mask = 7;
#elif OPTIMAL_CMP == 32
static const uint32_t align_mask = 3;
#endif
len = MIN(len, left);
#if OPTIMAL_CMP < 64
while (((uintptr_t)out & align_mask) && (len > 0)) {
*out++ = *from++;
--len;
--left;
}
#endif
#ifndef HAVE_MASKED_READWRITE
if (UNLIKELY(left < sizeof(chunk_t))) {
while (len > 0) {
*out++ = *from++;
--len;
}
return out;
}
#endif
if (len)
out = CHUNKMEMSET(out, from, len);
return out;
}
static inline uint8_t *CHUNKCOPY_SAFE(uint8_t *out, uint8_t *from, uint64_t len, uint8_t *safe)
{
if (out == from)
return out + len;
uint64_t safelen = (safe - out);
len = MIN(len, safelen);
#ifndef HAVE_MASKED_READWRITE
uint64_t from_dist = (uint64_t)llabs(safe - from);
if (UNLIKELY(from_dist < sizeof(chunk_t) || safelen < sizeof(chunk_t))) {
while (len--) {
*out++ = *from++;
}
return out;
}
#endif
return CHUNKMEMSET(out, from, (unsigned)len);
}
+133
View File
@@ -0,0 +1,133 @@
/* compare256_rle.h -- 256 byte run-length encoding comparison
* Copyright (C) 2022 Nathan Moinvaziri
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "zmemory.h"
#include "fallback_builtins.h"
typedef uint32_t (*compare256_rle_func)(const uint8_t* src0, const uint8_t* src1);
/* 8-bit integer comparison */
static inline uint32_t compare256_rle_8(const uint8_t *src0, const uint8_t *src1) {
uint32_t len = 0;
do {
if (*src0 != *src1)
return len;
src1 += 1, len += 1;
if (*src0 != *src1)
return len;
src1 += 1, len += 1;
if (*src0 != *src1)
return len;
src1 += 1, len += 1;
if (*src0 != *src1)
return len;
src1 += 1, len += 1;
if (*src0 != *src1)
return len;
src1 += 1, len += 1;
if (*src0 != *src1)
return len;
src1 += 1, len += 1;
if (*src0 != *src1)
return len;
src1 += 1, len += 1;
if (*src0 != *src1)
return len;
src1 += 1, len += 1;
} while (len < 256);
return 256;
}
/* 16-bit integer comparison */
static inline uint32_t compare256_rle_16(const uint8_t *src0, const uint8_t *src1) {
uint32_t len = 0;
uint16_t src0_cmp;
src0_cmp = zng_memread_2(src0);
do {
if (src0_cmp != zng_memread_2(src1))
return len + (*src0 == *src1);
src1 += 2, len += 2;
if (src0_cmp != zng_memread_2(src1))
return len + (*src0 == *src1);
src1 += 2, len += 2;
if (src0_cmp != zng_memread_2(src1))
return len + (*src0 == *src1);
src1 += 2, len += 2;
if (src0_cmp != zng_memread_2(src1))
return len + (*src0 == *src1);
src1 += 2, len += 2;
} while (len < 256);
return 256;
}
#ifdef HAVE_BUILTIN_CTZ
/* 32-bit integer comparison */
static inline uint32_t compare256_rle_32(const uint8_t *src0, const uint8_t *src1) {
uint32_t sv, len = 0;
uint16_t src0_cmp;
src0_cmp = zng_memread_2(src0);
sv = ((uint32_t)src0_cmp << 16) | src0_cmp;
do {
uint32_t mv, diff;
mv = zng_memread_4(src1);
diff = sv ^ mv;
if (diff) {
#if BYTE_ORDER == LITTLE_ENDIAN
uint32_t match_byte = __builtin_ctz(diff) / 8;
#else
uint32_t match_byte = __builtin_clz(diff) / 8;
#endif
return len + match_byte;
}
src1 += 4, len += 4;
} while (len < 256);
return 256;
}
#endif
#ifdef HAVE_BUILTIN_CTZLL
/* 64-bit integer comparison */
static inline uint32_t compare256_rle_64(const uint8_t *src0, const uint8_t *src1) {
uint32_t src0_cmp32, len = 0;
uint16_t src0_cmp;
uint64_t sv;
src0_cmp = zng_memread_2(src0);
src0_cmp32 = ((uint32_t)src0_cmp << 16) | src0_cmp;
sv = ((uint64_t)src0_cmp32 << 32) | src0_cmp32;
do {
uint64_t mv, diff;
mv = zng_memread_8(src1);
diff = sv ^ mv;
if (diff) {
#if BYTE_ORDER == LITTLE_ENDIAN
uint64_t match_byte = __builtin_ctzll(diff) / 8;
#else
uint64_t match_byte = __builtin_clzll(diff) / 8;
#endif
return len + (uint32_t)match_byte;
}
src1 += 8, len += 8;
} while (len < 256);
return 256;
}
#endif
+98
View File
@@ -0,0 +1,98 @@
/* compress.c -- compress a memory buffer
* Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "zutil.h"
/* ===========================================================================
* Architecture-specific hooks.
*/
#ifdef S390_DFLTCC_DEFLATE
# include "arch/s390/dfltcc_common.h"
#else
/* Returns the upper bound on compressed data length based on uncompressed data length, assuming default settings.
* Zero means that arch-specific deflation code behaves identically to the regular zlib-ng algorithms. */
# define DEFLATE_BOUND_COMPLEN(source_len) 0
#endif
/* ===========================================================================
Compresses the source buffer into the destination buffer. The level
parameter has the same meaning as in deflateInit. sourceLen is the byte
length of the source buffer. Upon entry, destLen is the total size of the
destination buffer, which must be at least 0.1% larger than sourceLen plus
12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_BUF_ERROR if there was not enough room in the output buffer,
Z_STREAM_ERROR if the level parameter is invalid.
*/
int Z_EXPORT PREFIX(compress2)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source,
z_uintmax_t sourceLen, int level) {
PREFIX3(stream) stream;
int err;
const unsigned int max = (unsigned int)-1;
z_size_t left;
left = *destLen;
*destLen = 0;
stream.zalloc = NULL;
stream.zfree = NULL;
stream.opaque = NULL;
err = PREFIX(deflateInit)(&stream, level);
if (err != Z_OK)
return err;
stream.next_out = dest;
stream.avail_out = 0;
stream.next_in = (z_const unsigned char *)source;
stream.avail_in = 0;
do {
if (stream.avail_out == 0) {
stream.avail_out = left > (unsigned long)max ? max : (unsigned int)left;
left -= stream.avail_out;
}
if (stream.avail_in == 0) {
stream.avail_in = sourceLen > (unsigned long)max ? max : (unsigned int)sourceLen;
sourceLen -= stream.avail_in;
}
err = PREFIX(deflate)(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
} while (err == Z_OK);
*destLen = stream.total_out;
PREFIX(deflateEnd)(&stream);
return err == Z_STREAM_END ? Z_OK : err;
}
/* ===========================================================================
*/
int Z_EXPORT PREFIX(compress)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t sourceLen) {
return PREFIX(compress2)(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
}
/* ===========================================================================
If the default memLevel or windowBits for deflateInit() is changed, then
this function needs to be updated.
*/
z_uintmax_t Z_EXPORT PREFIX(compressBound)(z_uintmax_t sourceLen) {
z_uintmax_t complen = DEFLATE_BOUND_COMPLEN(sourceLen);
if (complen > 0)
/* Architecture-specific code provided an upper bound. */
return complen + ZLIB_WRAPLEN;
#ifndef NO_QUICK_STRATEGY
return sourceLen /* The source size itself */
+ (sourceLen == 0 ? 1 : 0) /* Always at least one byte for any input */
+ (sourceLen < 9 ? 1 : 0) /* One extra byte for lengths less than 9 */
+ DEFLATE_QUICK_OVERHEAD(sourceLen) /* Source encoding overhead, padded to next full byte */
+ DEFLATE_BLOCK_OVERHEAD /* Deflate block overhead bytes */
+ ZLIB_WRAPLEN; /* zlib wrapper */
#else
return sourceLen + (sourceLen >> 4) + 7 + ZLIB_WRAPLEN;
#endif
}
+2339
View File
File diff suppressed because it is too large. Load diff
+23
View File
@@ -0,0 +1,23 @@
/* cpu_features.c -- CPU architecture feature check
* Copyright (C) 2017 Hans Kristian Rosbach
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "cpu_features.h"
#include <string.h>
Z_INTERNAL void cpu_check_features(struct cpu_features *features) {
memset(features, 0, sizeof(struct cpu_features));
#if defined(X86_FEATURES)
x86_check_features(&features->x86);
#elif defined(ARM_FEATURES)
arm_check_features(&features->arm);
#elif defined(PPC_FEATURES) || defined(POWER_FEATURES)
power_check_features(&features->power);
#elif defined(S390_FEATURES)
s390_check_features(&features->s390);
#elif defined(RISCV_FEATURES)
riscv_check_features(&features->riscv);
#endif
}
+43
View File
@@ -0,0 +1,43 @@
/* cpu_features.h -- CPU architecture feature check
* Copyright (C) 2017 Hans Kristian Rosbach
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef CPU_FEATURES_H_
#define CPU_FEATURES_H_
#ifndef DISABLE_RUNTIME_CPU_DETECTION
#if defined(X86_FEATURES)
# include "arch/x86/x86_features.h"
#elif defined(ARM_FEATURES)
# include "arch/arm/arm_features.h"
#elif defined(PPC_FEATURES) || defined(POWER_FEATURES)
# include "arch/power/power_features.h"
#elif defined(S390_FEATURES)
# include "arch/s390/s390_features.h"
#elif defined(RISCV_FEATURES)
# include "arch/riscv/riscv_features.h"
#endif
struct cpu_features {
#if defined(X86_FEATURES)
struct x86_cpu_features x86;
#elif defined(ARM_FEATURES)
struct arm_cpu_features arm;
#elif defined(PPC_FEATURES) || defined(POWER_FEATURES)
struct power_cpu_features power;
#elif defined(S390_FEATURES)
struct s390_cpu_features s390;
#elif defined(RISCV_FEATURES)
struct riscv_cpu_features riscv;
#else
char empty;
#endif
};
void cpu_check_features(struct cpu_features *features);
#endif
#endif
+42
View File
@@ -0,0 +1,42 @@
/* crc32.c -- compute the CRC-32 of a data stream
* Copyright (C) 1995-2022 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*
* This interleaved implementation of a CRC makes use of pipelined multiple
* arithmetic-logic units, commonly found in modern CPU cores. It is due to
* Kadatch and Jenkins (2010). See doc/crc-doc.1.0.pdf in this distribution.
*/
#include "zbuild.h"
#include "functable.h"
#include "crc32_braid_tbl.h"
/* ========================================================================= */
const uint32_t * Z_EXPORT PREFIX(get_crc_table)(void) {
return (const uint32_t *)crc_table;
}
#ifdef ZLIB_COMPAT
unsigned long Z_EXPORT PREFIX(crc32_z)(unsigned long crc, const unsigned char *buf, size_t len) {
if (buf == NULL) return 0;
return (unsigned long)FUNCTABLE_CALL(crc32)((uint32_t)crc, buf, len);
}
#else
uint32_t Z_EXPORT PREFIX(crc32_z)(uint32_t crc, const unsigned char *buf, size_t len) {
if (buf == NULL) return 0;
return FUNCTABLE_CALL(crc32)(crc, buf, len);
}
#endif
#ifdef ZLIB_COMPAT
unsigned long Z_EXPORT PREFIX(crc32)(unsigned long crc, const unsigned char *buf, unsigned int len) {
return (unsigned long)PREFIX(crc32_z)((uint32_t)crc, buf, len);
}
#else
uint32_t Z_EXPORT PREFIX(crc32)(uint32_t crc, const unsigned char *buf, uint32_t len) {
return PREFIX(crc32_z)(crc, buf, len);
}
#endif
+23
View File
@@ -0,0 +1,23 @@
/* crc32.h -- crc32 folding interface
* Copyright (C) 2021 Nathan Moinvaziri
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef CRC32_H_
#define CRC32_H_
/* sizeof(__m128i) * (4 folds) */
#define CRC32_FOLD_BUFFER_SIZE (16 * 4)
/* Size thresholds for Chorba algorithm variants */
#define CHORBA_LARGE_THRESHOLD (sizeof(z_word_t) * 64 * 1024)
#define CHORBA_MEDIUM_UPPER_THRESHOLD 32768
#define CHORBA_MEDIUM_LOWER_THRESHOLD 8192
#define CHORBA_SMALL_THRESHOLD_64BIT 72
#define CHORBA_SMALL_THRESHOLD_32BIT 80
typedef struct crc32_fold_s {
uint8_t fold[CRC32_FOLD_BUFFER_SIZE];
uint32_t value;
} crc32_fold;
#endif
+56
View File
@@ -0,0 +1,56 @@
/* crc32_braid_comb.c -- compute the CRC-32 of a data stream
* Copyright (C) 1995-2022 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*
* This interleaved implementation of a CRC makes use of pipelined multiple
* arithmetic-logic units, commonly found in modern CPU cores. It is due to
* Kadatch and Jenkins (2010). See doc/crc-doc.1.0.pdf in this distribution.
*/
#include "zutil.h"
#include "crc32_braid_p.h"
#include "crc32_braid_tbl.h"
#include "crc32_braid_comb_p.h"
/* ========================================================================= */
static uint32_t crc32_combine_(uint32_t crc1, uint32_t crc2, z_off64_t len2) {
return multmodp(x2nmodp(len2, 3), crc1) ^ crc2;
}
static uint32_t crc32_combine_gen_(z_off64_t len2) {
return x2nmodp(len2, 3);
}
static uint32_t crc32_combine_op_(uint32_t crc1, uint32_t crc2, const uint32_t op) {
return multmodp(op, crc1) ^ crc2;
}
/* ========================================================================= */
#ifdef ZLIB_COMPAT
unsigned long Z_EXPORT PREFIX(crc32_combine)(unsigned long crc1, unsigned long crc2, z_off_t len2) {
return (unsigned long)crc32_combine_((uint32_t)crc1, (uint32_t)crc2, len2);
}
unsigned long Z_EXPORT PREFIX4(crc32_combine)(unsigned long crc1, unsigned long crc2, z_off64_t len2) {
return (unsigned long)crc32_combine_((uint32_t)crc1, (uint32_t)crc2, len2);
}
unsigned long Z_EXPORT PREFIX(crc32_combine_gen)(z_off_t len2) {
return crc32_combine_gen_(len2);
}
unsigned long Z_EXPORT PREFIX4(crc32_combine_gen)(z_off64_t len2) {
return crc32_combine_gen_(len2);
}
unsigned long Z_EXPORT PREFIX(crc32_combine_op)(unsigned long crc1, unsigned long crc2, const unsigned long op) {
return (unsigned long)crc32_combine_op_((uint32_t)crc1, (uint32_t)crc2, (uint32_t)op);
}
#else
uint32_t Z_EXPORT PREFIX4(crc32_combine)(uint32_t crc1, uint32_t crc2, z_off64_t len2) {
return crc32_combine_(crc1, crc2, len2);
}
uint32_t Z_EXPORT PREFIX(crc32_combine_gen)(z_off64_t len2) {
return crc32_combine_gen_(len2);
}
uint32_t Z_EXPORT PREFIX(crc32_combine_op)(uint32_t crc1, uint32_t crc2, const uint32_t op) {
return crc32_combine_op_(crc1, crc2, op);
}
#endif
/* ========================================================================= */
+42
View File
@@ -0,0 +1,42 @@
#ifndef CRC32_BRAID_COMB_P_H_
#define CRC32_BRAID_COMB_P_H_
/*
Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC polynomial,
reflected. For speed, this requires that a not be zero.
*/
static uint32_t multmodp(uint32_t a, uint32_t b) {
uint32_t m, p;
m = (uint32_t)1 << 31;
p = 0;
for (;;) {
if (a & m) {
p ^= b;
if ((a & (m - 1)) == 0)
break;
}
m >>= 1;
b = b & 1 ? (b >> 1) ^ POLY : b >> 1;
}
return p;
}
/*
Return x^(n * 2^k) modulo p(x). Requires that x2n_table[] has been
initialized.
*/
static uint32_t x2nmodp(z_off64_t n, unsigned k) {
uint32_t p;
p = (uint32_t)1 << 31; /* x^0 == 1 */
while (n) {
if (n & 1)
p = multmodp(x2n_table[k & 31], p);
n >>= 1;
k++;
}
return p;
}
#endif /* CRC32_BRAID_COMB_P_H_ */
+40
View File
@@ -0,0 +1,40 @@
#ifndef CRC32_BRAID_P_H_
#define CRC32_BRAID_P_H_
#include "zendian.h"
/* Define BRAID_N, valid range is 1..6 */
#define BRAID_N 5
/* Define BRAID_W and the associated z_word_t type. If BRAID_W is not defined, then a braided
calculation is not used, and the associated tables and code are not compiled.
*/
#if defined(__x86_64__) || defined(_M_AMD64) || defined(__aarch64__) || defined(_M_ARM64) || defined(__powerpc64__)
# define BRAID_W 8
typedef uint64_t z_word_t;
#else
# define BRAID_W 4
typedef uint32_t z_word_t;
#endif
#if BYTE_ORDER == LITTLE_ENDIAN
# define ZSWAPWORD(word) (word)
# define BRAID_TABLE crc_braid_table
#elif BYTE_ORDER == BIG_ENDIAN
# if BRAID_W == 8
# define ZSWAPWORD(word) ZSWAP64(word)
# elif BRAID_W == 4
# define ZSWAPWORD(word) ZSWAP32(word)
# endif
# define BRAID_TABLE crc_braid_big_table
#else
# error "No endian defined"
#endif
#define CRC_DO1 c = crc_table[(c ^ *buf++) & 0xff] ^ (c >> 8)
#define CRC_DO8 CRC_DO1; CRC_DO1; CRC_DO1; CRC_DO1; CRC_DO1; CRC_DO1; CRC_DO1; CRC_DO1
/* CRC polynomial. */
#define POLY 0xedb88320 /* p(x) reflected, with x^32 implied */
#endif /* CRC32_BRAID_P_H_ */
File diff suppressed because it is too large. Load diff
+1513
View File
File diff suppressed because it is too large. Load diff
+456
View File
@@ -0,0 +1,456 @@
#ifndef DEFLATE_H_
#define DEFLATE_H_
/* deflate.h -- internal compression state
* Copyright (C) 1995-2016 Jean-loup Gailly
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* WARNING: this file should *not* be used by applications. It is
part of the implementation of the compression library and is
subject to change. Applications should only use zlib.h.
*/
#include "zutil.h"
#include "zendian.h"
#include "zmemory.h"
#include "crc32.h"
#ifdef S390_DFLTCC_DEFLATE
# include "arch/s390/dfltcc_common.h"
# define HAVE_ARCH_DEFLATE_STATE
#endif
/* define NO_GZIP when compiling if you want to disable gzip header and
trailer creation by deflate(). NO_GZIP would be used to avoid linking in
the crc code when it is not needed. For shared libraries, gzip encoding
should be left enabled. */
#ifndef NO_GZIP
# define GZIP
#endif
/* define LIT_MEM to slightly increase the speed of deflate (order 1% to 2%) at
the cost of a larger memory footprint */
#ifndef NO_LIT_MEM
# define LIT_MEM
#endif
/* ===========================================================================
* Internal compression state.
*/
#define LENGTH_CODES 29
/* number of length codes, not counting the special END_BLOCK code */
#define LITERALS 256
/* number of literal bytes 0..255 */
#define L_CODES (LITERALS+1+LENGTH_CODES)
/* number of Literal or Length codes, including the END_BLOCK code */
#define D_CODES 30
/* number of distance codes */
#define BL_CODES 19
/* number of codes used to transfer the bit lengths */
#define HEAP_SIZE (2*L_CODES+1)
/* maximum heap size */
#define BIT_BUF_SIZE 64
/* size of bit buffer in bi_buf */
#define END_BLOCK 256
/* end of block literal code */
#define INIT_STATE 1 /* zlib header -> BUSY_STATE */
#ifdef GZIP
# define GZIP_STATE 4 /* gzip header -> BUSY_STATE | EXTRA_STATE */
# define EXTRA_STATE 5 /* gzip extra block -> NAME_STATE */
# define NAME_STATE 6 /* gzip file name -> COMMENT_STATE */
# define COMMENT_STATE 7 /* gzip comment -> HCRC_STATE */
# define HCRC_STATE 8 /* gzip header CRC -> BUSY_STATE */
#endif
#define BUSY_STATE 2 /* deflate -> FINISH_STATE */
#define FINISH_STATE 3 /* stream complete */
#ifdef GZIP
# define MAX_STATE HCRC_STATE
#else
# define MAX_STATE FINISH_STATE
#endif
/* Stream status */
#define HASH_BITS 16u /* log2(HASH_SIZE) */
#ifndef HASH_SIZE
# define HASH_SIZE 65536u /* number of elements in hash table */
#endif
#define HASH_MASK (HASH_SIZE - 1u) /* HASH_SIZE-1 */
/* Data structure describing a single value and its code string. */
typedef struct ct_data_s {
union {
uint16_t freq; /* frequency count */
uint16_t code; /* bit string */
} fc;
union {
uint16_t dad; /* father node in Huffman tree */
uint16_t len; /* length of bit string */
} dl;
} ct_data;
#define Freq fc.freq
#define Code fc.code
#define Dad dl.dad
#define Len dl.len
typedef struct static_tree_desc_s static_tree_desc;
typedef struct tree_desc_s {
ct_data *dyn_tree; /* the dynamic tree */
int max_code; /* largest code with non zero frequency */
const static_tree_desc *stat_desc; /* the corresponding static tree */
} tree_desc;
typedef uint16_t Pos;
/* A Pos is an index in the character window. We use short instead of int to
* save space in the various tables.
*/
/* Type definitions for hash callbacks */
typedef struct internal_state deflate_state;
typedef uint32_t (* update_hash_cb) (uint32_t h, uint32_t val);
typedef void (* insert_string_cb) (deflate_state *const s, uint32_t str, uint32_t count);
typedef Pos (* quick_insert_string_cb)(deflate_state *const s, uint32_t str);
uint32_t update_hash (uint32_t h, uint32_t val);
void insert_string (deflate_state *const s, uint32_t str, uint32_t count);
Pos quick_insert_string (deflate_state *const s, uint32_t str);
uint32_t update_hash_roll (uint32_t h, uint32_t val);
void insert_string_roll (deflate_state *const s, uint32_t str, uint32_t count);
Pos quick_insert_string_roll(deflate_state *const s, uint32_t str);
/* Struct for memory allocation handling */
typedef struct deflate_allocs_s {
char *buf_start;
free_func zfree;
deflate_state *state;
unsigned char *window;
unsigned char *pending_buf;
Pos *prev;
Pos *head;
} deflate_allocs;
struct ALIGNED_(64) internal_state {
PREFIX3(stream) *strm; /* pointer back to this zlib stream */
unsigned char *pending_buf; /* output still pending */
unsigned char *pending_out; /* next pending byte to output to the stream */
uint32_t pending_buf_size; /* size of pending_buf */
uint32_t pending; /* nb of bytes in the pending buffer */
int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
uint32_t gzindex; /* where in extra, name, or comment */
PREFIX(gz_headerp) gzhead; /* gzip header information to write */
int status; /* as the name implies */
int last_flush; /* value of flush param for previous deflate call */
int reproducible; /* Whether reproducible compression results are required. */
int block_open;
/* Whether or not a block is currently open for the QUICK deflation scheme.
* This is set to 1 if there is an active block, or 0 if the block was just closed.
*/
/* used by deflate.c: */
unsigned int w_size; /* LZ77 window size (32K by default) */
unsigned int w_bits; /* log2(w_size) (8..16) */
unsigned int w_mask; /* w_size - 1 */
unsigned int lookahead; /* number of valid bytes ahead in window */
unsigned int high_water;
/* High water mark offset in window for initialized bytes -- bytes above
* this are set to zero in order to avoid memory check warnings when
* longest match routines access bytes past the input. This is then
* updated to the new high water mark.
*/
unsigned int window_size;
/* Actual size of window: 2*wSize, except when the user input buffer
* is directly used as sliding window.
*/
unsigned char *window;
/* Sliding window. Input bytes are read into the second half of the window,
* and move to the first half later to keep a dictionary of at least wSize
* bytes. With this organization, matches are limited to a distance of
* wSize-STD_MAX_MATCH bytes, but this ensures that IO is always
* performed with a length multiple of the block size. Also, it limits
* the window size to 64K, which is quite useful on MSDOS.
* To do: use the user input buffer as sliding window.
*/
Pos *prev;
/* Link to older string with same hash index. To limit the size of this
* array to 64K, this link is maintained only for the last 32K strings.
* An index in this array is thus a window index modulo 32K.
*/
Pos *head; /* Heads of the hash chains or 0. */
uint32_t ins_h; /* hash index of string to be inserted */
int block_start;
/* Window position at the beginning of the current output block. Gets
* negative when the window is moved backwards.
*/
unsigned int match_length; /* length of best match */
Pos prev_match; /* previous match */
int match_available; /* set if previous match exists */
unsigned int strstart; /* start of string to insert */
unsigned int match_start; /* start of matching string */
unsigned int prev_length;
/* Length of the best match at previous step. Matches not greater than this
* are discarded. This is used in the lazy match evaluation.
*/
unsigned int max_chain_length;
/* To speed up deflation, hash chains are never searched beyond this length.
* A higher limit improves compression ratio but degrades the speed.
*/
unsigned int max_lazy_match;
/* Attempt to find a better match only when the current match is strictly smaller
* than this value. This mechanism is used only for compression levels >= 4.
*/
# define max_insert_length max_lazy_match
/* Insert new strings in the hash table only if the match length is not
* greater than this length. This saves time but degrades compression.
* max_insert_length is used only for compression levels <= 3.
*/
update_hash_cb update_hash;
insert_string_cb insert_string;
quick_insert_string_cb quick_insert_string;
/* Hash function callbacks that can be configured depending on the deflate
* algorithm being used */
int level; /* compression level (1..9) */
int strategy; /* favor or force Huffman coding*/
unsigned int good_match;
/* Use a faster search when the previous match is longer than this */
int nice_match; /* Stop searching when current match exceeds this */
#if defined(_M_IX86) || defined(_M_ARM)
int padding[2];
#endif
struct crc32_fold_s ALIGNED_(16) crc_fold;
/* used by trees.c: */
/* Didn't use ct_data typedef below to suppress compiler warning */
struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
struct tree_desc_s l_desc; /* desc. for literal tree */
struct tree_desc_s d_desc; /* desc. for distance tree */
struct tree_desc_s bl_desc; /* desc. for bit length tree */
uint16_t bl_count[MAX_BITS+1];
/* number of codes at each bit length for an optimal tree */
int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
int heap_len; /* number of elements in the heap */
int heap_max; /* element of largest frequency */
/* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
* The same heap array is used to build all trees.
*/
unsigned char depth[2*L_CODES+1];
/* Depth of each subtree used as tie breaker for trees of equal frequency
*/
unsigned int lit_bufsize;
/* Size of match buffer for literals/lengths. There are 4 reasons for
* limiting lit_bufsize to 64K:
* - frequencies can be kept in 16 bit counters
* - if compression is not successful for the first block, all input
* data is still in the window so we can still emit a stored block even
* when input comes from standard input. (This can also be done for
* all blocks if lit_bufsize is not greater than 32K.)
* - if compression is not successful for a file smaller than 64K, we can
* even emit a stored file instead of a stored block (saving 5 bytes).
* This is applicable only for zip (not gzip or zlib).
* - creating new Huffman trees less frequently may not provide fast
* adaptation to changes in the input data statistics. (Take for
* example a binary file with poorly compressible code followed by
* a highly compressible string table.) Smaller buffer sizes give
* fast adaptation but have of course the overhead of transmitting
* trees more frequently.
* - I can't count above 4
*/
#ifdef LIT_MEM
# define LIT_BUFS 5
uint16_t *d_buf; /* buffer for distances */
unsigned char *l_buf; /* buffer for literals/lengths */
#else
# define LIT_BUFS 4
unsigned char *sym_buf; /* buffer for distances and literals/lengths */
#endif
unsigned int sym_next; /* running index in symbol buffer */
unsigned int sym_end; /* symbol table full when sym_next reaches this */
unsigned long opt_len; /* bit length of current block with optimal trees */
unsigned long static_len; /* bit length of current block with static trees */
unsigned int matches; /* number of string matches in current block */
unsigned int insert; /* bytes at end of window left to insert */
/* compressed_len and bits_sent are only used if ZLIB_DEBUG is defined */
unsigned long compressed_len; /* total bit length of compressed file mod 2^32 */
unsigned long bits_sent; /* bit length of compressed data sent mod 2^32 */
deflate_allocs *alloc_bufs;
#ifdef HAVE_ARCH_DEFLATE_STATE
arch_deflate_state arch; /* architecture-specific extensions */
#endif
uint64_t bi_buf;
/* Output buffer. bits are inserted starting at the bottom (least significant bits). */
int32_t bi_valid;
/* Number of valid bits in bi_buf. All bits above the last valid bit are always zero. */
/* Reserved for future use and alignment purposes */
int32_t reserved[19];
#if defined(_M_IX86) || defined(_M_ARM)
int32_t padding2[4];
#endif
};
typedef enum {
need_more, /* block not completed, need more input or more output */
block_done, /* block flush performed */
finish_started, /* finish started, need only more output at next deflate */
finish_done /* finish done, accept no more input or output */
} block_state;
/* Output a byte on the stream.
* IN assertion: there is enough room in pending_buf.
*/
#define put_byte(s, c) { \
s->pending_buf[s->pending++] = (unsigned char)(c); \
}
/* ===========================================================================
* Output a short LSB first on the stream.
* IN assertion: there is enough room in pending_buf.
*/
static inline void put_short(deflate_state *s, uint16_t w) {
#if BYTE_ORDER == BIG_ENDIAN
w = ZSWAP16(w);
#endif
zng_memwrite_2(&s->pending_buf[s->pending], w);
s->pending += 2;
}
/* ===========================================================================
* Output a short MSB first on the stream.
* IN assertion: there is enough room in pending_buf.
*/
static inline void put_short_msb(deflate_state *s, uint16_t w) {
#if BYTE_ORDER == LITTLE_ENDIAN
w = ZSWAP16(w);
#endif
zng_memwrite_2(&s->pending_buf[s->pending], w);
s->pending += 2;
}
/* ===========================================================================
* Output a 32-bit unsigned int LSB first on the stream.
* IN assertion: there is enough room in pending_buf.
*/
static inline void put_uint32(deflate_state *s, uint32_t dw) {
#if BYTE_ORDER == BIG_ENDIAN
dw = ZSWAP32(dw);
#endif
zng_memwrite_4(&s->pending_buf[s->pending], dw);
s->pending += 4;
}
/* ===========================================================================
* Output a 32-bit unsigned int MSB first on the stream.
* IN assertion: there is enough room in pending_buf.
*/
static inline void put_uint32_msb(deflate_state *s, uint32_t dw) {
#if BYTE_ORDER == LITTLE_ENDIAN
dw = ZSWAP32(dw);
#endif
zng_memwrite_4(&s->pending_buf[s->pending], dw);
s->pending += 4;
}
/* ===========================================================================
* Output a 64-bit unsigned int LSB first on the stream.
* IN assertion: there is enough room in pending_buf.
*/
static inline void put_uint64(deflate_state *s, uint64_t lld) {
#if BYTE_ORDER == BIG_ENDIAN
lld = ZSWAP64(lld);
#endif
zng_memwrite_8(&s->pending_buf[s->pending], lld);
s->pending += 8;
}
#define MIN_LOOKAHEAD (STD_MAX_MATCH + STD_MIN_MATCH + 1)
/* Minimum amount of lookahead, except at the end of the input file.
* See deflate.c for comments about the STD_MIN_MATCH+1.
*/
#define MAX_DIST(s) ((s)->w_size - MIN_LOOKAHEAD)
/* In order to simplify the code, particularly on 16 bit machines, match
* distances are limited to MAX_DIST instead of WSIZE.
*/
#define WIN_INIT STD_MAX_MATCH
/* Number of bytes after end of data in window to initialize in order to avoid
memory checker errors from longest match routines */
void Z_INTERNAL PREFIX(fill_window)(deflate_state *s);
void Z_INTERNAL slide_hash_c(deflate_state *s);
/* in trees.c */
void Z_INTERNAL zng_tr_init(deflate_state *s);
void Z_INTERNAL zng_tr_flush_block(deflate_state *s, char *buf, uint32_t stored_len, int last);
void Z_INTERNAL zng_tr_flush_bits(deflate_state *s);
void Z_INTERNAL zng_tr_align(deflate_state *s);
void Z_INTERNAL zng_tr_stored_block(deflate_state *s, char *buf, uint32_t stored_len, int last);
uint16_t Z_INTERNAL PREFIX(bi_reverse)(unsigned code, int len);
void Z_INTERNAL PREFIX(flush_pending)(PREFIX3(streamp) strm);
#define d_code(dist) ((dist) < 256 ? zng_dist_code[dist] : zng_dist_code[256+((dist)>>7)])
/* Mapping from a distance to a distance code. dist is the distance - 1 and
* must not have side effects. zng_dist_code[256] and zng_dist_code[257] are never
* used.
*/
/* Bit buffer and compress bits calculation debugging */
#ifdef ZLIB_DEBUG
# define cmpr_bits_add(s, len) s->compressed_len += (len)
# define cmpr_bits_align(s) s->compressed_len = (s->compressed_len + 7) & ~7L
# define sent_bits_add(s, bits) s->bits_sent += (bits)
# define sent_bits_align(s) s->bits_sent = (s->bits_sent + 7) & ~7L
#else
# define cmpr_bits_add(s, len) Z_UNUSED(len)
# define cmpr_bits_align(s)
# define sent_bits_add(s, bits) Z_UNUSED(bits)
# define sent_bits_align(s)
#endif
#endif /* DEFLATE_H_ */
+104
View File
@@ -0,0 +1,104 @@
/* deflate_fast.c -- compress data using the fast strategy of deflation algorithm
*
* Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "deflate.h"
#include "deflate_p.h"
#include "functable.h"
/* ===========================================================================
* Compress as much as possible from the input stream, return the current
* block state.
* This function does not perform lazy evaluation of matches and inserts
* new strings in the dictionary only for unmatched strings or for short
* matches. It is used only for the fast compression options.
*/
Z_INTERNAL block_state deflate_fast(deflate_state *s, int flush) {
Pos hash_head; /* head of the hash chain */
int bflush = 0; /* set if current block must be flushed */
int64_t dist;
uint32_t match_len = 0;
for (;;) {
/* Make sure that we always have enough lookahead, except
* at the end of the input file. We need STD_MAX_MATCH bytes
* for the next match, plus WANT_MIN_MATCH bytes to insert the
* string following the next match.
*/
if (s->lookahead < MIN_LOOKAHEAD) {
PREFIX(fill_window)(s);
if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
return need_more;
}
if (UNLIKELY(s->lookahead == 0))
break; /* flush the current block */
}
/* Insert the string window[strstart .. strstart+2] in the
* dictionary, and set hash_head to the head of the hash chain:
*/
if (s->lookahead >= WANT_MIN_MATCH) {
hash_head = quick_insert_string(s, s->strstart);
dist = (int64_t)s->strstart - hash_head;
/* Find the longest match, discarding those <= prev_length.
* At this point we have always match length < WANT_MIN_MATCH
*/
if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
/* To simplify the code, we prevent matches with the string
* of window index 0 (in particular we have to avoid a match
* of the string with itself at the start of the input file).
*/
match_len = FUNCTABLE_CALL(longest_match)(s, hash_head);
/* longest_match() sets match_start */
}
}
if (match_len >= WANT_MIN_MATCH) {
Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
Assert(s->match_start <= UINT16_MAX, "match_start should fit in uint16_t");
check_match(s, (Pos)s->strstart, (Pos)s->match_start, match_len);
bflush = zng_tr_tally_dist(s, s->strstart - s->match_start, match_len - STD_MIN_MATCH);
s->lookahead -= match_len;
/* Insert new strings in the hash table only if the match length
* is not too large. This saves time but degrades compression.
*/
if (match_len <= s->max_insert_length && s->lookahead >= WANT_MIN_MATCH) {
match_len--; /* string at strstart already in table */
s->strstart++;
insert_string(s, s->strstart, match_len);
s->strstart += match_len;
} else {
s->strstart += match_len;
quick_insert_string(s, s->strstart + 2 - STD_MIN_MATCH);
/* If lookahead < STD_MIN_MATCH, ins_h is garbage, but it does not
* matter since it will be recomputed at next deflate call.
*/
}
match_len = 0;
} else {
/* No match, output a literal byte */
bflush = zng_tr_tally_lit(s, s->window[s->strstart]);
s->lookahead--;
s->strstart++;
}
if (UNLIKELY(bflush))
FLUSH_BLOCK(s, 0);
}
s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
if (UNLIKELY(flush == Z_FINISH)) {
FLUSH_BLOCK(s, 1);
return finish_done;
}
if (UNLIKELY(s->sym_next))
FLUSH_BLOCK(s, 0);
return block_done;
}
+45
View File
@@ -0,0 +1,45 @@
/* deflate_huff.c -- compress data using huffman encoding only strategy
*
* Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "deflate.h"
#include "deflate_p.h"
#include "functable.h"
/* ===========================================================================
* For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
* (It will be regenerated if this run of deflate switches away from Huffman.)
*/
Z_INTERNAL block_state deflate_huff(deflate_state *s, int flush) {
int bflush = 0; /* set if current block must be flushed */
for (;;) {
/* Make sure that we have a literal to write. */
if (s->lookahead == 0) {
PREFIX(fill_window)(s);
if (s->lookahead == 0) {
if (flush == Z_NO_FLUSH)
return need_more;
break; /* flush the current block */
}
}
/* Output a literal byte */
bflush = zng_tr_tally_lit(s, s->window[s->strstart]);
s->lookahead--;
s->strstart++;
if (bflush)
FLUSH_BLOCK(s, 0);
}
s->insert = 0;
if (flush == Z_FINISH) {
FLUSH_BLOCK(s, 1);
return finish_done;
}
if (s->sym_next)
FLUSH_BLOCK(s, 0);
return block_done;
}
+278
View File
@@ -0,0 +1,278 @@
/* deflate_medium.c -- The deflate_medium deflate strategy
*
* Copyright (C) 2013 Intel Corporation. All rights reserved.
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
*
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef NO_MEDIUM_STRATEGY
#include "zbuild.h"
#include "deflate.h"
#include "deflate_p.h"
#include "functable.h"
struct match {
uint16_t match_start;
uint16_t match_length;
uint16_t strstart;
uint16_t orgstart;
};
static int emit_match(deflate_state *s, struct match match) {
int bflush = 0;
/* matches that are not long enough we need to emit as literals */
if (match.match_length < WANT_MIN_MATCH) {
while (match.match_length) {
bflush += zng_tr_tally_lit(s, s->window[match.strstart]);
s->lookahead--;
match.strstart++;
match.match_length--;
}
return bflush;
}
check_match(s, match.strstart, match.match_start, match.match_length);
bflush += zng_tr_tally_dist(s, match.strstart - match.match_start, match.match_length - STD_MIN_MATCH);
s->lookahead -= match.match_length;
return bflush;
}
static void insert_match(deflate_state *s, struct match match) {
if (UNLIKELY(s->lookahead <= (unsigned int)(match.match_length + WANT_MIN_MATCH)))
return;
/* string at strstart already in table */
match.strstart++;
match.match_length--;
/* matches that are not long enough we need to emit as literals */
if (LIKELY(match.match_length < WANT_MIN_MATCH - 1)) {
if (UNLIKELY(match.match_length > 0)) {
if (match.strstart >= match.orgstart) {
if (match.strstart + match.match_length - 1 >= match.orgstart) {
insert_string(s, match.strstart, match.match_length);
} else {
insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
}
match.strstart += match.match_length;
match.match_length = 0;
}
}
return;
}
/* Insert into hash table. */
if (LIKELY(match.strstart >= match.orgstart)) {
if (LIKELY(match.strstart + match.match_length - 1 >= match.orgstart)) {
insert_string(s, match.strstart, match.match_length);
} else {
insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
}
} else if (match.orgstart < match.strstart + match.match_length) {
insert_string(s, match.orgstart, match.strstart + match.match_length - match.orgstart);
}
match.strstart += match.match_length;
match.match_length = 0;
}
static void fizzle_matches(deflate_state *s, struct match *current, struct match *next) {
Pos limit;
unsigned char *match, *orig;
int changed = 0;
struct match c, n;
/* step zero: sanity checks */
if (current->match_length <= 1)
return;
if (UNLIKELY(current->match_length > 1 + next->match_start))
return;
if (UNLIKELY(current->match_length > 1 + next->strstart))
return;
match = s->window - current->match_length + 1 + next->match_start;
orig = s->window - current->match_length + 1 + next->strstart;
/* quick exit check.. if this fails then don't bother with anything else */
if (LIKELY(*match != *orig))
return;
c = *current;
n = *next;
/* step one: try to move the "next" match to the left as much as possible */
limit = next->strstart > MAX_DIST(s) ? next->strstart - (Pos)MAX_DIST(s) : 0;
match = s->window + n.match_start - 1;
orig = s->window + n.strstart - 1;
while (*match == *orig) {
if (UNLIKELY(c.match_length < 1))
break;
if (UNLIKELY(n.strstart <= limit))
break;
if (UNLIKELY(n.match_length >= 256))
break;
if (UNLIKELY(n.match_start <= 1))
break;
n.strstart--;
n.match_start--;
n.match_length++;
c.match_length--;
match--;
orig--;
changed++;
}
if (!changed)
return;
if (c.match_length <= 1 && n.match_length != 2) {
n.orgstart++;
*current = c;
*next = n;
} else {
return;
}
}
Z_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
/* Align the first struct to start on a new cacheline, this allows us to fit both structs in one cacheline */
ALIGNED_(16) struct match current_match;
struct match next_match;
/* For levels below 5, don't check the next position for a better match */
int early_exit = s->level < 5;
memset(&current_match, 0, sizeof(struct match));
memset(&next_match, 0, sizeof(struct match));
for (;;) {
Pos hash_head = 0; /* head of the hash chain */
int bflush = 0; /* set if current block must be flushed */
int64_t dist;
/* Make sure that we always have enough lookahead, except
* at the end of the input file. We need STD_MAX_MATCH bytes
* for the next match, plus WANT_MIN_MATCH bytes to insert the
* string following the next current_match.
*/
if (s->lookahead < MIN_LOOKAHEAD) {
PREFIX(fill_window)(s);
if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
return need_more;
}
if (UNLIKELY(s->lookahead == 0))
break; /* flush the current block */
next_match.match_length = 0;
}
/* Insert the string window[strstart .. strstart+2] in the
* dictionary, and set hash_head to the head of the hash chain:
*/
/* If we already have a future match from a previous round, just use that */
if (!early_exit && next_match.match_length > 0) {
current_match = next_match;
next_match.match_length = 0;
} else {
hash_head = 0;
if (s->lookahead >= WANT_MIN_MATCH) {
hash_head = quick_insert_string(s, s->strstart);
}
current_match.strstart = (uint16_t)s->strstart;
current_match.orgstart = current_match.strstart;
/* Find the longest match, discarding those <= prev_length.
* At this point we have always match_length < WANT_MIN_MATCH
*/
dist = (int64_t)s->strstart - hash_head;
if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
/* To simplify the code, we prevent matches with the string
* of window index 0 (in particular we have to avoid a match
* of the string with itself at the start of the input file).
*/
current_match.match_length = (uint16_t)FUNCTABLE_CALL(longest_match)(s, hash_head);
current_match.match_start = (uint16_t)s->match_start;
if (UNLIKELY(current_match.match_length < WANT_MIN_MATCH))
current_match.match_length = 1;
if (UNLIKELY(current_match.match_start >= current_match.strstart)) {
/* this can happen due to some restarts */
current_match.match_length = 1;
}
} else {
/* Set up the match to be a 1 byte literal */
current_match.match_start = 0;
current_match.match_length = 1;
}
}
insert_match(s, current_match);
/* now, look ahead one */
if (LIKELY(!early_exit && s->lookahead > MIN_LOOKAHEAD && (uint32_t)(current_match.strstart + current_match.match_length) < (s->window_size - MIN_LOOKAHEAD))) {
s->strstart = current_match.strstart + current_match.match_length;
hash_head = quick_insert_string(s, s->strstart);
next_match.strstart = (uint16_t)s->strstart;
next_match.orgstart = next_match.strstart;
/* Find the longest match, discarding those <= prev_length.
* At this point we have always match_length < WANT_MIN_MATCH
*/
dist = (int64_t)s->strstart - hash_head;
if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
/* To simplify the code, we prevent matches with the string
* of window index 0 (in particular we have to avoid a match
* of the string with itself at the start of the input file).
*/
next_match.match_length = (uint16_t)FUNCTABLE_CALL(longest_match)(s, hash_head);
next_match.match_start = (uint16_t)s->match_start;
if (UNLIKELY(next_match.match_start >= next_match.strstart)) {
/* this can happen due to some restarts */
next_match.match_length = 1;
}
if (next_match.match_length < WANT_MIN_MATCH)
next_match.match_length = 1;
else
fizzle_matches(s, &current_match, &next_match);
} else {
/* Set up the match to be a 1 byte literal */
next_match.match_start = 0;
next_match.match_length = 1;
}
s->strstart = current_match.strstart;
} else {
next_match.match_length = 0;
}
/* now emit the current match */
bflush = emit_match(s, current_match);
/* move the "cursor" forward */
s->strstart += current_match.match_length;
if (UNLIKELY(bflush))
FLUSH_BLOCK(s, 0);
}
s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
if (flush == Z_FINISH) {
FLUSH_BLOCK(s, 1);
return finish_done;
}
if (UNLIKELY(s->sym_next))
FLUSH_BLOCK(s, 0);
return block_done;
}
#endif
+128
View File
@@ -0,0 +1,128 @@
/* deflate_p.h -- Private inline functions and macros shared with more than
* one deflate method
*
* Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*
*/
#ifndef DEFLATE_P_H
#define DEFLATE_P_H
/* Forward declare common non-inlined functions declared in deflate.c */
#ifdef ZLIB_DEBUG
/* ===========================================================================
* Check that the match at match_start is indeed a match.
*/
static inline void check_match(deflate_state *s, Pos start, Pos match, int length) {
/* check that the match length is valid*/
if (length < STD_MIN_MATCH || length > STD_MAX_MATCH) {
fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
z_error("invalid match length");
}
/* check that the match isn't at the same position as the start string */
if (match == start) {
fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
z_error("invalid match position");
}
/* check that the match is indeed a match */
if (memcmp(s->window + match, s->window + start, length) != 0) {
int32_t i = 0;
fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
do {
fprintf(stderr, " %03d: match [%02x] start [%02x]\n", i++,
s->window[match++], s->window[start++]);
} while (--length != 0);
z_error("invalid match");
}
if (z_verbose > 1) {
fprintf(stderr, "\\[%u,%d]", start-match, length);
do {
putc(s->window[start++], stderr);
} while (--length != 0);
}
}
#else
#define check_match(s, start, match, length)
#endif
Z_INTERNAL void PREFIX(flush_pending)(PREFIX3(stream) *strm);
Z_INTERNAL unsigned PREFIX(read_buf)(PREFIX3(stream) *strm, unsigned char *buf, unsigned size);
/* ===========================================================================
* Save the match info and tally the frequency counts. Return true if
* the current block must be flushed.
*/
extern const unsigned char Z_INTERNAL zng_length_code[];
extern const unsigned char Z_INTERNAL zng_dist_code[];
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
/* c is the unmatched char */
#ifdef LIT_MEM
s->d_buf[s->sym_next] = 0;
s->l_buf[s->sym_next++] = c;
#else
s->sym_buf[s->sym_next++] = 0;
s->sym_buf[s->sym_next++] = 0;
s->sym_buf[s->sym_next++] = c;
#endif
s->dyn_ltree[c].Freq++;
Tracevv((stderr, "%c", c));
Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
return (s->sym_next == s->sym_end);
}
static inline int zng_tr_tally_dist(deflate_state* s, uint32_t dist, uint32_t len) {
/* dist: distance of matched string */
/* len: match length-STD_MIN_MATCH */
#ifdef LIT_MEM
Assert(dist <= UINT16_MAX, "dist should fit in uint16_t");
Assert(len <= UINT8_MAX, "len should fit in uint8_t");
s->d_buf[s->sym_next] = (uint16_t)dist;
s->l_buf[s->sym_next++] = (uint8_t)len;
#else
s->sym_buf[s->sym_next++] = (uint8_t)(dist);
s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8);
s->sym_buf[s->sym_next++] = (uint8_t)len;
#endif
s->matches++;
dist--;
Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
"zng_tr_tally: bad match");
s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
s->dyn_dtree[d_code(dist)].Freq++;
return (s->sym_next == s->sym_end);
}
/* ===========================================================================
* Flush the current block, with given end-of-file flag.
* IN assertion: strstart is set to the end of the current match.
*/
#define FLUSH_BLOCK_ONLY(s, last) { \
zng_tr_flush_block(s, (s->block_start >= 0 ? \
(char *)&s->window[(unsigned)s->block_start] : \
NULL), \
(uint32_t)((int)s->strstart - s->block_start), \
(last)); \
s->block_start = (int)s->strstart; \
PREFIX(flush_pending)(s->strm); \
}
/* Same but force premature exit if necessary. */
#define FLUSH_BLOCK(s, last) { \
FLUSH_BLOCK_ONLY(s, last); \
if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
}
/* Maximum stored block length in deflate format (not including header). */
#define MAX_STORED 65535
/* Compression function. Returns the block state after the call. */
typedef block_state (*compress_func) (deflate_state *s, int flush);
/* Match function. Returns the longest match. */
typedef uint32_t (*match_func) (deflate_state *const s, Pos cur_match);
#endif
+130
View File
@@ -0,0 +1,130 @@
/*
* The deflate_quick deflate strategy, designed to be used when cycles are
* at a premium.
*
* Copyright (C) 2013 Intel Corporation. All rights reserved.
* Authors:
* Wajdi Feghali <wajdi.k.feghali@intel.com>
* Jim Guilford <james.guilford@intel.com>
* Vinodh Gopal <vinodh.gopal@intel.com>
* Erdinc Ozturk <erdinc.ozturk@intel.com>
* Jim Kukunas <james.t.kukunas@linux.intel.com>
*
* Portions are Copyright (C) 2016 12Sided Technology, LLC.
* Author:
* Phil Vachon <pvachon@12sidedtech.com>
*
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "zmemory.h"
#include "deflate.h"
#include "deflate_p.h"
#include "functable.h"
#include "trees_emit.h"
extern const ct_data static_ltree[L_CODES+2];
extern const ct_data static_dtree[D_CODES];
#define QUICK_START_BLOCK(s, last) { \
zng_tr_emit_tree(s, STATIC_TREES, last); \
s->block_open = 1 + (int)last; \
s->block_start = (int)s->strstart; \
}
#define QUICK_END_BLOCK(s, last) { \
if (s->block_open) { \
zng_tr_emit_end_block(s, static_ltree, last); \
s->block_open = 0; \
s->block_start = (int)s->strstart; \
PREFIX(flush_pending)(s->strm); \
if (s->strm->avail_out == 0) \
return (last) ? finish_started : need_more; \
} \
}
Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
Pos hash_head;
int64_t dist;
unsigned match_len, last;
last = (flush == Z_FINISH) ? 1 : 0;
if (UNLIKELY(last && s->block_open != 2)) {
/* Emit end of previous block */
QUICK_END_BLOCK(s, 0);
/* Emit start of last block */
QUICK_START_BLOCK(s, last);
} else if (UNLIKELY(s->block_open == 0 && s->lookahead > 0)) {
/* Start new block only when we have lookahead data, so that if no
input data is given an empty block will not be written */
QUICK_START_BLOCK(s, last);
}
for (;;) {
if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) {
PREFIX(flush_pending)(s->strm);
if (s->strm->avail_out == 0) {
return (last && s->strm->avail_in == 0 && s->bi_valid == 0 && s->block_open == 0) ? finish_started : need_more;
}
}
if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
PREFIX(fill_window)(s);
if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
return need_more;
}
if (UNLIKELY(s->lookahead == 0))
break;
if (UNLIKELY(s->block_open == 0)) {
/* Start new block when we have lookahead data, so that if no
input data is given an empty block will not be written */
QUICK_START_BLOCK(s, last);
}
}
if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
hash_head = quick_insert_string(s, s->strstart);
dist = (int64_t)s->strstart - hash_head;
if (dist <= MAX_DIST(s) && dist > 0) {
const uint8_t *str_start = s->window + s->strstart;
const uint8_t *match_start = s->window + hash_head;
if (zng_memcmp_2(str_start, match_start) == 0) {
match_len = FUNCTABLE_CALL(compare256)(str_start+2, match_start+2) + 2;
if (match_len >= WANT_MIN_MATCH) {
if (UNLIKELY(match_len > s->lookahead))
match_len = s->lookahead;
if (UNLIKELY(match_len > STD_MAX_MATCH))
match_len = STD_MAX_MATCH;
Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
check_match(s, (Pos)s->strstart, hash_head, match_len);
zng_tr_emit_dist(s, static_ltree, static_dtree, match_len - STD_MIN_MATCH, (uint32_t)dist);
s->lookahead -= match_len;
s->strstart += match_len;
continue;
}
}
}
}
zng_tr_emit_lit(s, static_ltree, s->window[s->strstart]);
s->strstart++;
s->lookahead--;
}
s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
if (UNLIKELY(last)) {
QUICK_END_BLOCK(s, 1);
return finish_done;
}
QUICK_END_BLOCK(s, 0);
return block_done;
}
+84
View File
@@ -0,0 +1,84 @@
/* deflate_rle.c -- compress data using RLE strategy of deflation algorithm
*
* Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "deflate.h"
#include "deflate_p.h"
#include "functable.h"
#include "compare256_rle.h"
#if OPTIMAL_CMP == 8
# define compare256_rle compare256_rle_8
#elif defined(HAVE_BUILTIN_CTZLL)
# define compare256_rle compare256_rle_64
#elif defined(HAVE_BUILTIN_CTZ)
# define compare256_rle compare256_rle_32
#else
# define compare256_rle compare256_rle_16
#endif
/* ===========================================================================
* For Z_RLE, simply look for runs of bytes, generate matches only of distance
* one. Do not maintain a hash table. (It will be regenerated if this run of
* deflate switches away from Z_RLE.)
*/
Z_INTERNAL block_state deflate_rle(deflate_state *s, int flush) {
int bflush = 0; /* set if current block must be flushed */
unsigned char *scan; /* scan goes up to strend for length of run */
uint32_t match_len = 0;
for (;;) {
/* Make sure that we always have enough lookahead, except
* at the end of the input file. We need STD_MAX_MATCH bytes
* for the longest run, plus one for the unrolled loop.
*/
if (s->lookahead <= STD_MAX_MATCH) {
PREFIX(fill_window)(s);
if (s->lookahead <= STD_MAX_MATCH && flush == Z_NO_FLUSH)
return need_more;
if (s->lookahead == 0)
break; /* flush the current block */
}
/* See how many times the previous byte repeats */
if (s->lookahead >= STD_MIN_MATCH && s->strstart > 0) {
scan = s->window + s->strstart - 1;
if (scan[0] == scan[1] && scan[1] == scan[2]) {
match_len = compare256_rle(scan, scan+3)+2;
match_len = MIN(match_len, s->lookahead);
match_len = MIN(match_len, STD_MAX_MATCH);
}
Assert(scan+match_len <= s->window + s->window_size - 1, "wild scan");
}
/* Emit match if have run of STD_MIN_MATCH or longer, else emit literal */
if (match_len >= STD_MIN_MATCH) {
Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
check_match(s, (Pos)s->strstart, (Pos)(s->strstart - 1), match_len);
bflush = zng_tr_tally_dist(s, 1, match_len - STD_MIN_MATCH);
s->lookahead -= match_len;
s->strstart += match_len;
match_len = 0;
} else {
/* No match, output a literal byte */
bflush = zng_tr_tally_lit(s, s->window[s->strstart]);
s->lookahead--;
s->strstart++;
}
if (bflush)
FLUSH_BLOCK(s, 0);
}
s->insert = 0;
if (flush == Z_FINISH) {
FLUSH_BLOCK(s, 1);
return finish_done;
}
if (s->sym_next)
FLUSH_BLOCK(s, 0);
return block_done;
}
+144
View File
@@ -0,0 +1,144 @@
/* deflate_slow.c -- compress data using the slow strategy of deflation algorithm
*
* Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "deflate.h"
#include "deflate_p.h"
#include "functable.h"
/* ===========================================================================
* Same as deflate_medium, but achieves better compression. We use a lazy
* evaluation for matches: a match is finally adopted only if there is
* no better match at the next window position.
*/
Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
Pos hash_head; /* head of hash chain */
int bflush; /* set if current block must be flushed */
int64_t dist;
uint32_t match_len;
match_func longest_match;
if (s->max_chain_length <= 1024)
longest_match = FUNCTABLE_FPTR(longest_match);
else
longest_match = FUNCTABLE_FPTR(longest_match_slow);
/* Process the input block. */
for (;;) {
/* Make sure that we always have enough lookahead, except
* at the end of the input file. We need STD_MAX_MATCH bytes
* for the next match, plus WANT_MIN_MATCH bytes to insert the
* string following the next match.
*/
if (s->lookahead < MIN_LOOKAHEAD) {
PREFIX(fill_window)(s);
if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
return need_more;
}
if (UNLIKELY(s->lookahead == 0))
break; /* flush the current block */
}
/* Insert the string window[strstart .. strstart+2] in the
* dictionary, and set hash_head to the head of the hash chain:
*/
hash_head = 0;
if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
hash_head = s->quick_insert_string(s, s->strstart);
}
/* Find the longest match, discarding those <= prev_length.
*/
s->prev_match = (Pos)s->match_start;
match_len = STD_MIN_MATCH - 1;
dist = (int64_t)s->strstart - hash_head;
if (dist <= MAX_DIST(s) && dist > 0 && s->prev_length < s->max_lazy_match && hash_head != 0) {
/* To simplify the code, we prevent matches with the string
* of window index 0 (in particular we have to avoid a match
* of the string with itself at the start of the input file).
*/
match_len = longest_match(s, hash_head);
/* longest_match() sets match_start */
if (match_len <= 5 && (s->strategy == Z_FILTERED)) {
/* If prev_match is also WANT_MIN_MATCH, match_start is garbage
* but we will ignore the current match anyway.
*/
match_len = STD_MIN_MATCH - 1;
}
}
/* If there was a match at the previous step and the current
* match is not better, output the previous match:
*/
if (s->prev_length >= STD_MIN_MATCH && match_len <= s->prev_length) {
unsigned int max_insert = s->strstart + s->lookahead - STD_MIN_MATCH;
/* Do not insert strings in hash table beyond this. */
Assert((s->strstart-1) <= UINT16_MAX, "strstart-1 should fit in uint16_t");
check_match(s, (Pos)(s->strstart - 1), s->prev_match, s->prev_length);
bflush = zng_tr_tally_dist(s, s->strstart -1 - s->prev_match, s->prev_length - STD_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
* enough lookahead, the last two strings are not inserted in
* the hash table.
*/
s->prev_length -= 1;
s->lookahead -= s->prev_length;
unsigned int mov_fwd = s->prev_length - 1;
if (max_insert > s->strstart) {
unsigned int insert_cnt = mov_fwd;
if (UNLIKELY(insert_cnt > max_insert - s->strstart))
insert_cnt = max_insert - s->strstart;
s->insert_string(s, s->strstart + 1, insert_cnt);
}
s->prev_length = 0;
s->match_available = 0;
s->strstart += mov_fwd + 1;
if (UNLIKELY(bflush))
FLUSH_BLOCK(s, 0);
} else if (s->match_available) {
/* If there was no match at the previous position, output a
* single literal. If there was a match but the current match
* is longer, truncate the previous match to a single literal.
*/
bflush = zng_tr_tally_lit(s, s->window[s->strstart-1]);
if (UNLIKELY(bflush))
FLUSH_BLOCK_ONLY(s, 0);
s->prev_length = match_len;
s->strstart++;
s->lookahead--;
if (UNLIKELY(s->strm->avail_out == 0))
return need_more;
} else {
/* There is no previous match to compare with, wait for
* the next step to decide.
*/
s->prev_length = match_len;
s->match_available = 1;
s->strstart++;
s->lookahead--;
}
}
Assert(flush != Z_NO_FLUSH, "no flush?");
if (UNLIKELY(s->match_available)) {
Z_UNUSED(zng_tr_tally_lit(s, s->window[s->strstart-1]));
s->match_available = 0;
}
s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
if (UNLIKELY(flush == Z_FINISH)) {
FLUSH_BLOCK(s, 1);
return finish_done;
}
if (UNLIKELY(s->sym_next))
FLUSH_BLOCK(s, 0);
return block_done;
}
+186
View File
@@ -0,0 +1,186 @@
/* deflate_stored.c -- store data without compression using deflation algorithm
*
* Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "deflate.h"
#include "deflate_p.h"
#include "functable.h"
/* ===========================================================================
* Copy without compression as much as possible from the input stream, return
* the current block state.
*
* In case deflateParams() is used to later switch to a non-zero compression
* level, s->matches (otherwise unused when storing) keeps track of the number
* of hash table slides to perform. If s->matches is 1, then one hash table
* slide will be done when switching. If s->matches is 2, the maximum value
* allowed here, then the hash table will be cleared, since two or more slides
* is the same as a clear.
*
* deflate_stored() is written to minimize the number of times an input byte is
* copied. It is most efficient with large input and output buffers, which
* maximizes the opportunities to have a single copy from next_in to next_out.
*/
Z_INTERNAL block_state deflate_stored(deflate_state *s, int flush) {
/* Smallest worthy block size when not flushing or finishing. By default
* this is 32K. This can be as small as 507 bytes for memLevel == 1. For
* large input and output buffers, the stored block size will be larger.
*/
unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
/* Copy as many min_block or larger stored blocks directly to next_out as
* possible. If flushing, copy the remaining available input to next_out as
* stored blocks, if there is enough space.
*/
unsigned len, left, have, last = 0;
unsigned used = s->strm->avail_in;
do {
/* Set len to the maximum size block that we can copy directly with the
* available input data and output space. Set left to how much of that
* would be copied from what's left in the window.
*/
len = MAX_STORED; /* maximum deflate stored block length */
have = (s->bi_valid + 42) >> 3; /* number of header bytes */
if (s->strm->avail_out < have) /* need room for header */
break;
/* maximum stored block length that will fit in avail_out: */
have = s->strm->avail_out - have;
left = (int)s->strstart - s->block_start; /* bytes left in window */
if (len > (unsigned long)left + s->strm->avail_in)
len = left + s->strm->avail_in; /* limit len to the input */
len = MIN(len, have); /* limit len to the output */
/* If the stored block would be less than min_block in length, or if
* unable to copy all of the available input when flushing, then try
* copying to the window and the pending buffer instead. Also don't
* write an empty block when flushing -- deflate() does that.
*/
if (len < min_block && ((len == 0 && flush != Z_FINISH) || flush == Z_NO_FLUSH || len != left + s->strm->avail_in))
break;
/* Make a dummy stored block in pending to get the header bytes,
* including any pending bits. This also updates the debugging counts.
*/
last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
zng_tr_stored_block(s, (char *)0, 0L, last);
/* Replace the lengths in the dummy stored block with len. */
s->pending -= 4;
put_short(s, (uint16_t)len);
put_short(s, (uint16_t)~len);
/* Write the stored block header bytes. */
PREFIX(flush_pending)(s->strm);
/* Update debugging counts for the data about to be copied. */
cmpr_bits_add(s, len << 3);
sent_bits_add(s, len << 3);
/* Copy uncompressed bytes from the window to next_out. */
if (left) {
left = MIN(left, len);
memcpy(s->strm->next_out, s->window + s->block_start, left);
s->strm->next_out += left;
s->strm->avail_out -= left;
s->strm->total_out += left;
s->block_start += (int)left;
len -= left;
}
/* Copy uncompressed bytes directly from next_in to next_out, updating
* the check value.
*/
if (len) {
PREFIX(read_buf)(s->strm, s->strm->next_out, len);
s->strm->next_out += len;
s->strm->avail_out -= len;
s->strm->total_out += len;
}
} while (last == 0);
/* Update the sliding window with the last s->w_size bytes of the copied
* data, or append all of the copied data to the existing window if less
* than s->w_size bytes were copied. Also update the number of bytes to
* insert in the hash tables, in the event that deflateParams() switches to
* a non-zero compression level.
*/
used -= s->strm->avail_in; /* number of input bytes directly copied */
if (used) {
/* If any input was used, then no unused input remains in the window,
* therefore s->block_start == s->strstart.
*/
if (used >= s->w_size) { /* supplant the previous history */
s->matches = 2; /* clear hash */
memcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
s->strstart = s->w_size;
s->insert = s->strstart;
} else {
if (s->window_size - s->strstart <= used) {
/* Slide the window down. */
s->strstart -= s->w_size;
memcpy(s->window, s->window + s->w_size, s->strstart);
if (s->matches < 2)
s->matches++; /* add a pending slide_hash() */
s->insert = MIN(s->insert, s->strstart);
}
memcpy(s->window + s->strstart, s->strm->next_in - used, used);
s->strstart += used;
s->insert += MIN(used, s->w_size - s->insert);
}
s->block_start = (int)s->strstart;
}
s->high_water = MAX(s->high_water, s->strstart);
/* If the last block was written to next_out, then done. */
if (last)
return finish_done;
/* If flushing and all input has been consumed, then done. */
if (flush != Z_NO_FLUSH && flush != Z_FINISH && s->strm->avail_in == 0 && (int)s->strstart == s->block_start)
return block_done;
/* Fill the window with any remaining input. */
have = s->window_size - s->strstart;
if (s->strm->avail_in > have && s->block_start >= (int)s->w_size) {
/* Slide the window down. */
s->block_start -= (int)s->w_size;
s->strstart -= s->w_size;
memcpy(s->window, s->window + s->w_size, s->strstart);
if (s->matches < 2)
s->matches++; /* add a pending slide_hash() */
have += s->w_size; /* more space now */
s->insert = MIN(s->insert, s->strstart);
}
have = MIN(have, s->strm->avail_in);
if (have) {
PREFIX(read_buf)(s->strm, s->window + s->strstart, have);
s->strstart += have;
s->insert += MIN(have, s->w_size - s->insert);
}
s->high_water = MAX(s->high_water, s->strstart);
/* There was not enough avail_out to write a complete worthy or flushed
* stored block to next_out. Write a stored block to pending instead, if we
* have enough input for a worthy block, or if flushing and there is enough
* room for the remaining input as a stored block in the pending buffer.
*/
have = (s->bi_valid + 42) >> 3; /* number of header bytes */
/* maximum stored block length that will fit in pending: */
have = MIN(s->pending_buf_size - have, MAX_STORED);
min_block = MIN(have, s->w_size);
left = (int)s->strstart - s->block_start;
if (left >= min_block || ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH && s->strm->avail_in == 0 && left <= have)) {
len = MIN(left, have);
last = flush == Z_FINISH && s->strm->avail_in == 0 && len == left ? 1 : 0;
zng_tr_stored_block(s, (char *)s->window + s->block_start, len, last);
s->block_start += (int)len;
PREFIX(flush_pending)(s->strm);
}
/* We've done all we can with the available input and output. */
return last ? finish_started : need_more;
}
+47
View File
@@ -0,0 +1,47 @@
#ifndef FALLBACK_BUILTINS_H
#define FALLBACK_BUILTINS_H
#if defined(_MSC_VER) && !defined(__clang__)
#if defined(_M_IX86) || defined(_M_AMD64) || defined(_M_IA64) || defined(_M_ARM) || defined(_M_ARM64) || defined(_M_ARM64EC)
#include <intrin.h>
/* This is not a general purpose replacement for __builtin_ctz. The function expects that value is != 0.
* Because of that assumption trailing_zero is not initialized and the return value is not checked.
* Tzcnt and bsf give identical results except when input value is 0, therefore this can not be allowed.
* If tzcnt instruction is not supported, the cpu will itself execute bsf instead.
* Performance tzcnt/bsf is identical on Intel cpu, tzcnt is faster than bsf on AMD cpu.
*/
static __forceinline int __builtin_ctz(unsigned int value) {
Assert(value != 0, "Invalid input value: 0");
# if defined(X86_FEATURES) && !(_MSC_VER < 1700)
return (int)_tzcnt_u32(value);
# else
unsigned long trailing_zero;
_BitScanForward(&trailing_zero, value);
return (int)trailing_zero;
# endif
}
#define HAVE_BUILTIN_CTZ
#ifdef _M_AMD64
/* This is not a general purpose replacement for __builtin_ctzll. The function expects that value is != 0.
* Because of that assumption trailing_zero is not initialized and the return value is not checked.
*/
static __forceinline int __builtin_ctzll(unsigned long long value) {
Assert(value != 0, "Invalid input value: 0");
# if defined(X86_FEATURES) && !(_MSC_VER < 1700)
return (int)_tzcnt_u64(value);
# else
unsigned long trailing_zero;
_BitScanForward64(&trailing_zero, value);
return (int)trailing_zero;
# endif
}
#define HAVE_BUILTIN_CTZLL
#endif // Microsoft AMD64
#endif // Microsoft AMD64/IA64/x86/ARM/ARM64 test
#endif // _MSC_VER & !clang
#endif // include guard FALLBACK_BUILTINS_H
+363
View File
@@ -0,0 +1,363 @@
/* functable.c -- Choose relevant optimized functions at runtime
* Copyright (C) 2017 Hans Kristian Rosbach
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef DISABLE_RUNTIME_CPU_DETECTION
#include "zbuild.h"
#if defined(_MSC_VER)
# include <intrin.h>
#endif
#include "functable.h"
#include "cpu_features.h"
#include "arch_functions.h"
/* Platform has pointer size atomic store */
#if defined(__GNUC__) || defined(__clang__)
# define FUNCTABLE_ASSIGN(VAR, FUNC_NAME) \
__atomic_store(&(functable.FUNC_NAME), &(VAR.FUNC_NAME), __ATOMIC_SEQ_CST)
# define FUNCTABLE_BARRIER() __atomic_thread_fence(__ATOMIC_SEQ_CST)
#elif defined(_MSC_VER)
# define FUNCTABLE_ASSIGN(VAR, FUNC_NAME) \
_InterlockedExchangePointer((void * volatile *)&(functable.FUNC_NAME), (void *)(VAR.FUNC_NAME))
# if defined(_M_ARM) || defined(_M_ARM64)
# define FUNCTABLE_BARRIER() do { \
_ReadWriteBarrier(); \
__dmb(0xB); /* _ARM_BARRIER_ISH */ \
_ReadWriteBarrier(); \
} while (0)
# else
# define FUNCTABLE_BARRIER() _ReadWriteBarrier()
# endif
#else
# warning Unable to detect atomic intrinsic support.
# define FUNCTABLE_ASSIGN(VAR, FUNC_NAME) \
*((void * volatile *)&(functable.FUNC_NAME)) = (void *)(VAR.FUNC_NAME)
# define FUNCTABLE_BARRIER() do { /* Empty */ } while (0)
#endif
static void force_init_empty(void) {
// empty
}
static void init_functable(void) {
struct functable_s ft;
struct cpu_features cf;
cpu_check_features(&cf);
// Generic code
ft.force_init = &force_init_empty;
ft.adler32 = &adler32_c;
ft.adler32_fold_copy = &adler32_fold_copy_c;
ft.chunkmemset_safe = &chunkmemset_safe_c;
ft.chunksize = &chunksize_c;
ft.crc32 = &crc32_c;
ft.crc32_fold = &crc32_fold_c;
ft.crc32_fold_copy = &crc32_fold_copy_c;
ft.crc32_fold_final = &crc32_fold_final_c;
ft.crc32_fold_reset = &crc32_fold_reset_c;
ft.inflate_fast = &inflate_fast_c;
ft.slide_hash = &slide_hash_c;
ft.longest_match = &longest_match_c;
ft.longest_match_slow = &longest_match_slow_c;
ft.compare256 = &compare256_c;
// Select arch-optimized functions
// X86 - SSE2
#ifdef X86_SSE2
# if !defined(__x86_64__) && !defined(_M_X64) && !defined(X86_NOCHECK_SSE2)
if (cf.x86.has_sse2)
# endif
{
ft.chunkmemset_safe = &chunkmemset_safe_sse2;
ft.chunksize = &chunksize_sse2;
ft.inflate_fast = &inflate_fast_sse2;
ft.slide_hash = &slide_hash_sse2;
# ifdef HAVE_BUILTIN_CTZ
ft.compare256 = &compare256_sse2;
ft.longest_match = &longest_match_sse2;
ft.longest_match_slow = &longest_match_slow_sse2;
# endif
}
#endif
// X86 - SSSE3
#ifdef X86_SSSE3
if (cf.x86.has_ssse3) {
ft.adler32 = &adler32_ssse3;
ft.chunkmemset_safe = &chunkmemset_safe_ssse3;
ft.inflate_fast = &inflate_fast_ssse3;
}
#endif
// X86 - SSE4.2
#ifdef X86_SSE42
if (cf.x86.has_sse42) {
ft.adler32_fold_copy = &adler32_fold_copy_sse42;
}
#endif
// X86 - PCLMUL
#ifdef X86_PCLMULQDQ_CRC
if (cf.x86.has_pclmulqdq) {
ft.crc32 = &crc32_pclmulqdq;
ft.crc32_fold = &crc32_fold_pclmulqdq;
ft.crc32_fold_copy = &crc32_fold_pclmulqdq_copy;
ft.crc32_fold_final = &crc32_fold_pclmulqdq_final;
ft.crc32_fold_reset = &crc32_fold_pclmulqdq_reset;
}
#endif
// X86 - AVX
#ifdef X86_AVX2
/* BMI2 support is all but implicit with AVX2 but let's sanity check this just in case. Enabling BMI2 allows for
* flagless shifts, resulting in fewer flag stalls for the pipeline, and allows us to set destination registers
* for the shift results as an operand, eliminating several register-register moves when the original value needs
* to remain intact. They also allow for a count operand that isn't the CL register, avoiding contention there */
if (cf.x86.has_avx2 && cf.x86.has_bmi2) {
ft.adler32 = &adler32_avx2;
ft.adler32_fold_copy = &adler32_fold_copy_avx2;
ft.chunkmemset_safe = &chunkmemset_safe_avx2;
ft.chunksize = &chunksize_avx2;
ft.inflate_fast = &inflate_fast_avx2;
ft.slide_hash = &slide_hash_avx2;
# ifdef HAVE_BUILTIN_CTZ
ft.compare256 = &compare256_avx2;
ft.longest_match = &longest_match_avx2;
ft.longest_match_slow = &longest_match_slow_avx2;
# endif
}
#endif
// X86 - AVX512 (F,DQ,BW,Vl)
#ifdef X86_AVX512
if (cf.x86.has_avx512_common) {
ft.adler32 = &adler32_avx512;
ft.adler32_fold_copy = &adler32_fold_copy_avx512;
ft.chunkmemset_safe = &chunkmemset_safe_avx512;
ft.chunksize = &chunksize_avx512;
ft.inflate_fast = &inflate_fast_avx512;
}
#endif
#ifdef X86_AVX512VNNI
if (cf.x86.has_avx512vnni) {
ft.adler32 = &adler32_avx512_vnni;
ft.adler32_fold_copy = &adler32_fold_copy_avx512_vnni;
}
#endif
// X86 - VPCLMULQDQ
#ifdef X86_VPCLMULQDQ_CRC
if (cf.x86.has_pclmulqdq && cf.x86.has_avx512_common && cf.x86.has_vpclmulqdq) {
ft.crc32 = &crc32_vpclmulqdq;
ft.crc32_fold = &crc32_fold_vpclmulqdq;
ft.crc32_fold_copy = &crc32_fold_vpclmulqdq_copy;
ft.crc32_fold_final = &crc32_fold_vpclmulqdq_final;
ft.crc32_fold_reset = &crc32_fold_vpclmulqdq_reset;
}
#endif
// ARM - SIMD
#ifdef ARM_SIMD
# ifndef ARM_NOCHECK_SIMD
if (cf.arm.has_simd)
# endif
{
ft.slide_hash = &slide_hash_armv6;
}
#endif
// ARM - NEON
#ifdef ARM_NEON
# ifndef ARM_NOCHECK_NEON
if (cf.arm.has_neon)
# endif
{
ft.adler32 = &adler32_neon;
ft.adler32_fold_copy = &adler32_fold_copy_neon;
ft.chunkmemset_safe = &chunkmemset_safe_neon;
ft.chunksize = &chunksize_neon;
ft.inflate_fast = &inflate_fast_neon;
ft.slide_hash = &slide_hash_neon;
# ifdef HAVE_BUILTIN_CTZLL
ft.compare256 = &compare256_neon;
ft.longest_match = &longest_match_neon;
ft.longest_match_slow = &longest_match_slow_neon;
# endif
}
#endif
// ARM - CRC32
#ifdef ARM_CRC32
if (cf.arm.has_crc32) {
ft.crc32 = &crc32_armv8;
}
#endif
// Power - VMX
#ifdef PPC_VMX
if (cf.power.has_altivec) {
ft.adler32 = &adler32_vmx;
ft.slide_hash = &slide_hash_vmx;
}
#endif
// Power8 - VSX
#ifdef POWER8_VSX
if (cf.power.has_arch_2_07) {
ft.adler32 = &adler32_power8;
ft.chunkmemset_safe = &chunkmemset_safe_power8;
ft.chunksize = &chunksize_power8;
ft.inflate_fast = &inflate_fast_power8;
ft.slide_hash = &slide_hash_power8;
}
#endif
#ifdef POWER8_VSX_CRC32
if (cf.power.has_arch_2_07)
ft.crc32 = &crc32_power8;
#endif
// Power9
#ifdef POWER9
if (cf.power.has_arch_3_00) {
ft.compare256 = &compare256_power9;
ft.longest_match = &longest_match_power9;
ft.longest_match_slow = &longest_match_slow_power9;
}
#endif
// RISCV - RVV
#ifdef RISCV_RVV
if (cf.riscv.has_rvv) {
ft.adler32 = &adler32_rvv;
ft.adler32_fold_copy = &adler32_fold_copy_rvv;
ft.chunkmemset_safe = &chunkmemset_safe_rvv;
ft.chunksize = &chunksize_rvv;
ft.compare256 = &compare256_rvv;
ft.inflate_fast = &inflate_fast_rvv;
ft.longest_match = &longest_match_rvv;
ft.longest_match_slow = &longest_match_slow_rvv;
ft.slide_hash = &slide_hash_rvv;
}
#endif
// S390
#ifdef S390_CRC32_VX
if (cf.s390.has_vx)
ft.crc32 = crc32_s390_vx;
#endif
// Assign function pointers individually for atomic operation
FUNCTABLE_ASSIGN(ft, force_init);
FUNCTABLE_ASSIGN(ft, adler32);
FUNCTABLE_ASSIGN(ft, adler32_fold_copy);
FUNCTABLE_ASSIGN(ft, chunkmemset_safe);
FUNCTABLE_ASSIGN(ft, chunksize);
FUNCTABLE_ASSIGN(ft, compare256);
FUNCTABLE_ASSIGN(ft, crc32);
FUNCTABLE_ASSIGN(ft, crc32_fold);
FUNCTABLE_ASSIGN(ft, crc32_fold_copy);
FUNCTABLE_ASSIGN(ft, crc32_fold_final);
FUNCTABLE_ASSIGN(ft, crc32_fold_reset);
FUNCTABLE_ASSIGN(ft, inflate_fast);
FUNCTABLE_ASSIGN(ft, longest_match);
FUNCTABLE_ASSIGN(ft, longest_match_slow);
FUNCTABLE_ASSIGN(ft, slide_hash);
// Memory barrier for weak memory order CPUs
FUNCTABLE_BARRIER();
}
/* stub functions */
static void force_init_stub(void) {
init_functable();
}
static uint32_t adler32_stub(uint32_t adler, const uint8_t* buf, size_t len) {
init_functable();
return functable.adler32(adler, buf, len);
}
static uint32_t adler32_fold_copy_stub(uint32_t adler, uint8_t* dst, const uint8_t* src, size_t len) {
init_functable();
return functable.adler32_fold_copy(adler, dst, src, len);
}
static uint8_t* chunkmemset_safe_stub(uint8_t* out, uint8_t *from, unsigned len, unsigned left) {
init_functable();
return functable.chunkmemset_safe(out, from, len, left);
}
static uint32_t chunksize_stub(void) {
init_functable();
return functable.chunksize();
}
static uint32_t compare256_stub(const uint8_t* src0, const uint8_t* src1) {
init_functable();
return functable.compare256(src0, src1);
}
static uint32_t crc32_stub(uint32_t crc, const uint8_t* buf, size_t len) {
init_functable();
return functable.crc32(crc, buf, len);
}
static void crc32_fold_stub(crc32_fold* crc, const uint8_t* src, size_t len, uint32_t init_crc) {
init_functable();
functable.crc32_fold(crc, src, len, init_crc);
}
static void crc32_fold_copy_stub(crc32_fold* crc, uint8_t* dst, const uint8_t* src, size_t len) {
init_functable();
functable.crc32_fold_copy(crc, dst, src, len);
}
static uint32_t crc32_fold_final_stub(crc32_fold* crc) {
init_functable();
return functable.crc32_fold_final(crc);
}
static uint32_t crc32_fold_reset_stub(crc32_fold* crc) {
init_functable();
return functable.crc32_fold_reset(crc);
}
static void inflate_fast_stub(PREFIX3(stream) *strm, uint32_t start) {
init_functable();
functable.inflate_fast(strm, start);
}
static uint32_t longest_match_stub(deflate_state* const s, Pos cur_match) {
init_functable();
return functable.longest_match(s, cur_match);
}
static uint32_t longest_match_slow_stub(deflate_state* const s, Pos cur_match) {
init_functable();
return functable.longest_match_slow(s, cur_match);
}
static void slide_hash_stub(deflate_state* s) {
init_functable();
functable.slide_hash(s);
}
/* functable init */
Z_INTERNAL struct functable_s functable = {
force_init_stub,
adler32_stub,
adler32_fold_copy_stub,
chunkmemset_safe_stub,
chunksize_stub,
compare256_stub,
crc32_stub,
crc32_fold_stub,
crc32_fold_copy_stub,
crc32_fold_final_stub,
crc32_fold_reset_stub,
inflate_fast_stub,
longest_match_stub,
longest_match_slow_stub,
slide_hash_stub,
};
#endif
+55
View File
@@ -0,0 +1,55 @@
/* functable.h -- Struct containing function pointers to optimized functions
* Copyright (C) 2017 Hans Kristian Rosbach
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef FUNCTABLE_H_
#define FUNCTABLE_H_
#include "deflate.h"
#include "crc32.h"
#ifdef DISABLE_RUNTIME_CPU_DETECTION
# include "arch_functions.h"
/* When compiling with native instructions it is not necessary to use functable.
* Instead we use native_ macro indicating the best available variant of arch-specific
* functions for the current platform.
*/
# define FUNCTABLE_INIT ((void)0)
# define FUNCTABLE_CALL(name) native_ ## name
# define FUNCTABLE_FPTR(name) &native_ ## name
#else
struct functable_s {
void (* force_init) (void);
uint32_t (* adler32) (uint32_t adler, const uint8_t *buf, size_t len);
uint32_t (* adler32_fold_copy) (uint32_t adler, uint8_t *dst, const uint8_t *src, size_t len);
uint8_t* (* chunkmemset_safe) (uint8_t *out, uint8_t *from, unsigned len, unsigned left);
uint32_t (* chunksize) (void);
uint32_t (* compare256) (const uint8_t *src0, const uint8_t *src1);
uint32_t (* crc32) (uint32_t crc, const uint8_t *buf, size_t len);
void (* crc32_fold) (struct crc32_fold_s *crc, const uint8_t *src, size_t len, uint32_t init_crc);
void (* crc32_fold_copy) (struct crc32_fold_s *crc, uint8_t *dst, const uint8_t *src, size_t len);
uint32_t (* crc32_fold_final) (struct crc32_fold_s *crc);
uint32_t (* crc32_fold_reset) (struct crc32_fold_s *crc);
void (* inflate_fast) (PREFIX3(stream) *strm, uint32_t start);
uint32_t (* longest_match) (deflate_state *const s, Pos cur_match);
uint32_t (* longest_match_slow) (deflate_state *const s, Pos cur_match);
void (* slide_hash) (deflate_state *s);
};
Z_INTERNAL extern struct functable_s functable;
/* Explicitly indicate functions are conditionally dispatched.
*/
# define FUNCTABLE_INIT functable.force_init()
# define FUNCTABLE_CALL(name) functable.name
# define FUNCTABLE_FPTR(name) functable.name
#endif
#endif
+146
View File
@@ -0,0 +1,146 @@
#ifndef GZGUTS_H_
#define GZGUTS_H_
/* gzguts.h -- zlib internal header definitions for gz* operations
* Copyright (C) 2004-2024 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifdef _LARGEFILE64_SOURCE
# ifndef _LARGEFILE_SOURCE
# define _LARGEFILE_SOURCE 1
# endif
# undef _FILE_OFFSET_BITS
# undef _TIME_BITS
#endif
#if defined(HAVE_VISIBILITY_INTERNAL)
# define Z_INTERNAL __attribute__((visibility ("internal")))
#elif defined(HAVE_VISIBILITY_HIDDEN)
# define Z_INTERNAL __attribute__((visibility ("hidden")))
#else
# define Z_INTERNAL
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <fcntl.h>
#if defined(ZLIB_COMPAT)
# include "zlib.h"
#else
# include "zlib-ng.h"
#endif
#ifdef _WIN32
# include <stddef.h>
#endif
#if defined(_WIN32)
# include <io.h>
# define WIDECHAR
#endif
#ifdef WINAPI_FAMILY
# define open _open
# define read _read
# define write _write
# define close _close
#endif
/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
#if !defined(STDC99) && !defined(__CYGWIN__) && !defined(__MINGW__) && defined(_WIN32)
# if !defined(vsnprintf)
# if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
# define vsnprintf _vsnprintf
# endif
# endif
#endif
/* unlike snprintf (which is required in C99), _snprintf does not guarantee
null termination of the result -- however this is only used in gzlib.c
where the result is assured to fit in the space provided */
#if defined(_MSC_VER) && _MSC_VER < 1900
# define snprintf _snprintf
#endif
/* get errno and strerror definition */
#ifndef NO_STRERROR
# include <errno.h>
# define zstrerror() strerror(errno)
#else
# define zstrerror() "stdio error (consult errno)"
#endif
/* default memLevel */
#if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
#else
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
#endif
/* default i/o buffer size -- double this for output when reading (this and
twice this must be able to fit in an unsigned type) */
#ifndef GZBUFSIZE
# define GZBUFSIZE 131072
#endif
/* gzip modes, also provide a little integrity check on the passed structure */
#define GZ_NONE 0
#define GZ_READ 7247
#define GZ_WRITE 31153
#define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */
/* values for gz_state how */
#define LOOK 0 /* look for a gzip header */
#define COPY 1 /* copy input directly */
#define GZIP 2 /* decompress a gzip stream */
/* internal gzip file state data structure */
typedef struct {
/* exposed contents for gzgetc() macro */
struct gzFile_s x; /* "x" for exposed */
/* x.have: number of bytes available at x.next */
/* x.next: next output data to deliver or write */
/* x.pos: current position in uncompressed data */
/* used for both reading and writing */
int mode; /* see gzip modes above */
int fd; /* file descriptor */
char *path; /* path or fd for error messages */
unsigned size; /* buffer size, zero if not allocated yet */
unsigned want; /* requested buffer size, default is GZBUFSIZE */
unsigned char *in; /* input buffer (double-sized when writing) */
unsigned char *out; /* output buffer (double-sized when reading) */
int direct; /* 0 if processing gzip, 1 if transparent */
/* just for reading */
int how; /* 0: get header, 1: copy, 2: decompress */
z_off64_t start; /* where the gzip data started, for rewinding */
int eof; /* true if end of input file reached */
int past; /* true if read requested past end */
/* just for writing */
int level; /* compression level */
int strategy; /* compression strategy */
int reset; /* true if a reset is pending after a Z_FINISH */
/* seek request */
z_off64_t skip; /* amount to skip (already rewound if backwards) */
int seek; /* true if seek request pending */
/* error information */
int err; /* error code */
char *msg; /* error message */
/* zlib inflate or deflate stream */
PREFIX3(stream) strm; /* stream structure in-place (not a pointer) */
} gz_state;
typedef gz_state *gz_statep;
/* shared functions */
void Z_INTERNAL gz_error(gz_state *, int, const char *);
#ifdef ZLIB_COMPAT
unsigned Z_INTERNAL gz_intmax(void);
#endif
/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
value -- needed when comparing unsigned to z_off64_t, which is signed
(possible z_off64_t types off_t, off64_t, and long are all signed) */
#define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
#endif /* GZGUTS_H_ */
+531
View File
@@ -0,0 +1,531 @@
/* gzlib.c -- zlib functions common to reading and writing gzip files
* Copyright (C) 2004-2024 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "zutil_p.h"
#include "gzguts.h"
#if defined(_WIN32)
# define LSEEK _lseeki64
#else
#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
# define LSEEK lseek64
#else
# define LSEEK lseek
#endif
#endif
/* Local functions */
static void gz_reset(gz_state *);
static gzFile gz_open(const void *, int, const char *);
/* Reset gzip file state */
static void gz_reset(gz_state *state) {
state->x.have = 0; /* no output data available */
if (state->mode == GZ_READ) { /* for reading ... */
state->eof = 0; /* not at end of file */
state->past = 0; /* have not read past end yet */
state->how = LOOK; /* look for gzip header */
}
else /* for writing ... */
state->reset = 0; /* no deflateReset pending */
state->seek = 0; /* no seek request pending */
gz_error(state, Z_OK, NULL); /* clear error */
state->x.pos = 0; /* no uncompressed data yet */
state->strm.avail_in = 0; /* no input data yet */
}
/* Open a gzip file either by name or file descriptor. */
static gzFile gz_open(const void *path, int fd, const char *mode) {
gz_state *state;
size_t len;
int oflag;
#ifdef O_CLOEXEC
int cloexec = 0;
#endif
#ifdef O_EXCL
int exclusive = 0;
#endif
/* check input */
if (path == NULL)
return NULL;
/* allocate gzFile structure to return */
state = (gz_state *)zng_alloc(sizeof(gz_state));
if (state == NULL)
return NULL;
state->size = 0; /* no buffers allocated yet */
state->want = GZBUFSIZE; /* requested buffer size */
state->msg = NULL; /* no error message yet */
/* interpret mode */
state->mode = GZ_NONE;
state->level = Z_DEFAULT_COMPRESSION;
state->strategy = Z_DEFAULT_STRATEGY;
state->direct = 0;
while (*mode) {
if (*mode >= '0' && *mode <= '9') {
state->level = *mode - '0';
} else {
switch (*mode) {
case 'r':
state->mode = GZ_READ;
break;
#ifndef NO_GZCOMPRESS
case 'w':
state->mode = GZ_WRITE;
break;
case 'a':
state->mode = GZ_APPEND;
break;
#endif
case '+': /* can't read and write at the same time */
zng_free(state);
return NULL;
case 'b': /* ignore -- will request binary anyway */
break;
#ifdef O_CLOEXEC
case 'e':
cloexec = 1;
break;
#endif
#ifdef O_EXCL
case 'x':
exclusive = 1;
break;
#endif
case 'f':
state->strategy = Z_FILTERED;
break;
case 'h':
state->strategy = Z_HUFFMAN_ONLY;
break;
case 'R':
state->strategy = Z_RLE;
break;
case 'F':
state->strategy = Z_FIXED;
break;
case 'T':
state->direct = 1;
break;
default: /* could consider as an error, but just ignore */
{}
}
}
mode++;
}
/* must provide an "r", "w", or "a" */
if (state->mode == GZ_NONE) {
zng_free(state);
return NULL;
}
/* can't force transparent read */
if (state->mode == GZ_READ) {
if (state->direct) {
zng_free(state);
return NULL;
}
state->direct = 1; /* for empty file */
}
/* save the path name for error messages */
#ifdef WIDECHAR
if (fd == -2) {
len = wcstombs(NULL, (const wchar_t *)path, 0);
if (len == (size_t)-1)
len = 0;
} else
#endif
len = strlen((const char *)path);
state->path = (char *)malloc(len + 1);
if (state->path == NULL) {
zng_free(state);
return NULL;
}
#ifdef WIDECHAR
if (fd == -2)
if (len) {
wcstombs(state->path, (const wchar_t *)path, len + 1);
} else {
*(state->path) = 0;
}
else
#endif
(void)snprintf(state->path, len + 1, "%s", (const char *)path);
/* compute the flags for open() */
oflag =
#ifdef O_LARGEFILE
O_LARGEFILE |
#endif
#ifdef O_BINARY
O_BINARY |
#endif
#ifdef O_CLOEXEC
(cloexec ? O_CLOEXEC : 0) |
#endif
(state->mode == GZ_READ ?
O_RDONLY :
(O_WRONLY | O_CREAT |
#ifdef O_EXCL
(exclusive ? O_EXCL : 0) |
#endif
(state->mode == GZ_WRITE ?
O_TRUNC :
O_APPEND)));
/* open the file with the appropriate flags (or just use fd) */
state->fd = fd > -1 ? fd : (
#if defined(_WIN32)
fd == -2 ? _wopen((const wchar_t *)path, oflag, 0666) :
#elif __CYGWIN__
fd == -2 ? open(state->path, oflag, 0666) :
#endif
open((const char *)path, oflag, 0666));
if (state->fd == -1) {
free(state->path);
zng_free(state);
return NULL;
}
if (state->mode == GZ_APPEND) {
LSEEK(state->fd, 0, SEEK_END); /* so gzoffset() is correct */
state->mode = GZ_WRITE; /* simplify later checks */
}
/* save the current position for rewinding (only if reading) */
if (state->mode == GZ_READ) {
state->start = LSEEK(state->fd, 0, SEEK_CUR);
if (state->start == -1) state->start = 0;
}
/* initialize stream */
gz_reset(state);
/* return stream */
return (gzFile)state;
}
/* -- see zlib.h -- */
gzFile Z_EXPORT PREFIX(gzopen)(const char *path, const char *mode) {
return gz_open(path, -1, mode);
}
#ifdef ZLIB_COMPAT
gzFile Z_EXPORT PREFIX4(gzopen)(const char *path, const char *mode) {
return gz_open(path, -1, mode);
}
#endif
/* -- see zlib.h -- */
gzFile Z_EXPORT PREFIX(gzdopen)(int fd, const char *mode) {
char *path; /* identifier for error messages */
gzFile gz;
if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
return NULL;
(void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); /* for debugging */
gz = gz_open(path, fd, mode);
free(path);
return gz;
}
/* -- see zlib.h -- */
#ifdef WIDECHAR
gzFile Z_EXPORT PREFIX(gzopen_w)(const wchar_t *path, const char *mode) {
return gz_open(path, -2, mode);
}
#endif
int Z_EXPORT PREFIX(gzclose)(gzFile file) {
#ifndef NO_GZCOMPRESS
gz_state *state;
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_state *)file;
return state->mode == GZ_READ ? PREFIX(gzclose_r)(file) : PREFIX(gzclose_w)(file);
#else
return PREFIX(gzclose_r)(file);
#endif
}
/* -- see zlib.h -- */
int Z_EXPORT PREFIX(gzbuffer)(gzFile file, unsigned size) {
gz_state *state;
/* get internal structure and check integrity */
if (file == NULL)
return -1;
state = (gz_state *)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return -1;
/* make sure we haven't already allocated memory */
if (state->size != 0)
return -1;
/* check and set requested size */
if ((size << 1) < size)
return -1; /* need to be able to double it */
if (size < 8)
size = 8; /* needed to behave well with flushing */
state->want = size;
return 0;
}
/* -- see zlib.h -- */
int Z_EXPORT PREFIX(gzrewind)(gzFile file) {
gz_state *state;
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_state *)file;
/* check that we're reading and that there's no error */
if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
return -1;
/* back up and start over */
if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
return -1;
gz_reset(state);
return 0;
}
/* -- see zlib.h -- */
z_off64_t Z_EXPORT PREFIX4(gzseek)(gzFile file, z_off64_t offset, int whence) {
unsigned n;
z_off64_t ret;
gz_state *state;
/* get internal structure and check integrity */
if (file == NULL)
return -1;
state = (gz_state *)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return -1;
/* check that there's no error */
if (state->err != Z_OK && state->err != Z_BUF_ERROR)
return -1;
/* can only seek from start or relative to current position */
if (whence != SEEK_SET && whence != SEEK_CUR)
return -1;
/* normalize offset to a SEEK_CUR specification */
if (whence == SEEK_SET)
offset -= state->x.pos;
else if (state->seek)
offset += state->skip;
state->seek = 0;
/* if within raw area while reading, just go there */
if (state->mode == GZ_READ && state->how == COPY && state->x.pos + offset >= 0) {
ret = LSEEK(state->fd, offset - (z_off64_t)state->x.have, SEEK_CUR);
if (ret == -1)
return -1;
state->x.have = 0;
state->eof = 0;
state->past = 0;
state->seek = 0;
gz_error(state, Z_OK, NULL);
state->strm.avail_in = 0;
state->x.pos += offset;
return state->x.pos;
}
/* calculate skip amount, rewinding if needed for back seek when reading */
if (offset < 0) {
if (state->mode != GZ_READ) /* writing -- can't go backwards */
return -1;
offset += state->x.pos;
if (offset < 0) /* before start of file! */
return -1;
if (PREFIX(gzrewind)(file) == -1) /* rewind, then skip to offset */
return -1;
}
/* if reading, skip what's in output buffer (one less gzgetc() check) */
if (state->mode == GZ_READ) {
n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ? (unsigned)offset : state->x.have;
state->x.have -= n;
state->x.next += n;
state->x.pos += n;
offset -= n;
}
/* request skip (if not zero) */
if (offset) {
state->seek = 1;
state->skip = offset;
}
return state->x.pos + offset;
}
/* -- see zlib.h -- */
#ifdef ZLIB_COMPAT
z_off_t Z_EXPORT PREFIX(gzseek)(gzFile file, z_off_t offset, int whence) {
z_off64_t ret;
ret = PREFIX4(gzseek)(file, (z_off64_t)offset, whence);
return ret == (z_off_t)ret ? (z_off_t)ret : -1;
}
#endif
/* -- see zlib.h -- */
z_off64_t Z_EXPORT PREFIX4(gztell)(gzFile file) {
gz_state *state;
/* get internal structure and check integrity */
if (file == NULL)
return -1;
state = (gz_state *)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return -1;
/* return position */
return state->x.pos + (state->seek ? state->skip : 0);
}
/* -- see zlib.h -- */
#ifdef ZLIB_COMPAT
z_off_t Z_EXPORT PREFIX(gztell)(gzFile file) {
z_off64_t ret;
ret = PREFIX4(gztell)(file);
return ret == (z_off_t)ret ? (z_off_t)ret : -1;
}
#endif
/* -- see zlib.h -- */
z_off64_t Z_EXPORT PREFIX4(gzoffset)(gzFile file) {
z_off64_t offset;
gz_state *state;
/* get internal structure and check integrity */
if (file == NULL)
return -1;
state = (gz_state *)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return -1;
/* compute and return effective offset in file */
offset = LSEEK(state->fd, 0, SEEK_CUR);
if (offset == -1)
return -1;
if (state->mode == GZ_READ) /* reading */
offset -= state->strm.avail_in; /* don't count buffered input */
return offset;
}
/* -- see zlib.h -- */
#ifdef ZLIB_COMPAT
z_off_t Z_EXPORT PREFIX(gzoffset)(gzFile file) {
z_off64_t ret;
ret = PREFIX4(gzoffset)(file);
return ret == (z_off_t)ret ? (z_off_t)ret : -1;
}
#endif
/* -- see zlib.h -- */
int Z_EXPORT PREFIX(gzeof)(gzFile file) {
gz_state *state;
/* get internal structure and check integrity */
if (file == NULL)
return 0;
state = (gz_state *)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return 0;
/* return end-of-file state */
return state->mode == GZ_READ ? state->past : 0;
}
/* -- see zlib.h -- */
const char * Z_EXPORT PREFIX(gzerror)(gzFile file, int *errnum) {
gz_state *state;
/* get internal structure and check integrity */
if (file == NULL)
return NULL;
state = (gz_state *)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return NULL;
/* return error information */
if (errnum != NULL)
*errnum = state->err;
return state->err == Z_MEM_ERROR ? "out of memory" : (state->msg == NULL ? "" : state->msg);
}
/* -- see zlib.h -- */
void Z_EXPORT PREFIX(gzclearerr)(gzFile file) {
gz_state *state;
/* get internal structure and check integrity */
if (file == NULL)
return;
state = (gz_state *)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return;
/* clear error and end-of-file */
if (state->mode == GZ_READ) {
state->eof = 0;
state->past = 0;
}
gz_error(state, Z_OK, NULL);
}
/* Create an error message in allocated memory and set state->err and
state->msg accordingly. Free any previous error message already there. Do
not try to free or allocate space if the error is Z_MEM_ERROR (out of
memory). Simply save the error message as a static string. If there is an
allocation failure constructing the error message, then convert the error to
out of memory. */
void Z_INTERNAL gz_error(gz_state *state, int err, const char *msg) {
/* free previously allocated message and clear */
if (state->msg != NULL) {
if (state->err != Z_MEM_ERROR)
free(state->msg);
state->msg = NULL;
}
/* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
if (err != Z_OK && err != Z_BUF_ERROR)
state->x.have = 0;
/* set error code, and if no message, then done */
state->err = err;
if (msg == NULL)
return;
/* for an out of memory error, return literal string when requested */
if (err == Z_MEM_ERROR)
return;
/* construct error message with path */
if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
state->err = Z_MEM_ERROR;
return;
}
(void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, "%s%s%s", state->path, ": ", msg);
}
#ifdef ZLIB_COMPAT
unsigned Z_INTERNAL gz_intmax(void) {
return INT_MAX;
}
#endif
+606
View File
@@ -0,0 +1,606 @@
/* gzread.c -- zlib functions for reading gzip files
* Copyright (C) 2004-2017 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "zutil_p.h"
#include "gzguts.h"
/* Local functions */
static int gz_load(gz_state *, unsigned char *, unsigned, unsigned *);
static int gz_avail(gz_state *);
static int gz_look(gz_state *);
static int gz_decomp(gz_state *);
static int gz_fetch(gz_state *);
static int gz_skip(gz_state *, z_off64_t);
static size_t gz_read(gz_state *, void *, size_t);
/* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from
state->fd, and update state->eof, state->err, and state->msg as appropriate.
This function needs to loop on read(), since read() is not guaranteed to
read the number of bytes requested, depending on the type of descriptor. */
static int gz_load(gz_state *state, unsigned char *buf, unsigned len, unsigned *have) {
ssize_t ret;
*have = 0;
do {
ret = read(state->fd, buf + *have, len - *have);
if (ret <= 0)
break;
*have += (unsigned)ret;
} while (*have < len);
if (ret < 0) {
gz_error(state, Z_ERRNO, zstrerror());
return -1;
}
if (ret == 0)
state->eof = 1;
return 0;
}
/* Load up input buffer and set eof flag if last data loaded -- return -1 on
error, 0 otherwise. Note that the eof flag is set when the end of the input
file is reached, even though there may be unused data in the buffer. Once
that data has been used, no more attempts will be made to read the file.
If strm->avail_in != 0, then the current data is moved to the beginning of
the input buffer, and then the remainder of the buffer is loaded with the
available data from the input file. */
static int gz_avail(gz_state *state) {
unsigned got;
PREFIX3(stream) *strm = &(state->strm);
if (state->err != Z_OK && state->err != Z_BUF_ERROR)
return -1;
if (state->eof == 0) {
if (strm->avail_in) { /* copy what's there to the start */
unsigned char *p = state->in;
unsigned const char *q = strm->next_in;
unsigned n = strm->avail_in;
do {
*p++ = *q++;
} while (--n);
}
if (gz_load(state, state->in + strm->avail_in, state->size - strm->avail_in, &got) == -1)
return -1;
strm->avail_in += got;
strm->next_in = state->in;
}
return 0;
}
/* Look for gzip header, set up for inflate or copy. state->x.have must be 0.
If this is the first time in, allocate required memory. state->how will be
left unchanged if there is no more input data available, will be set to COPY
if there is no gzip header and direct copying will be performed, or it will
be set to GZIP for decompression. If direct copying, then leftover input
data from the input buffer will be copied to the output buffer. In that
case, all further file reads will be directly to either the output buffer or
a user buffer. If decompressing, the inflate state will be initialized.
gz_look() will return 0 on success or -1 on failure. */
static int gz_look(gz_state *state) {
PREFIX3(stream) *strm = &(state->strm);
/* allocate read buffers and inflate memory */
if (state->size == 0) {
/* allocate buffers */
state->in = (unsigned char *)zng_alloc(state->want);
state->out = (unsigned char *)zng_alloc(state->want << 1);
if (state->in == NULL || state->out == NULL) {
zng_free(state->out);
zng_free(state->in);
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
state->size = state->want;
/* allocate inflate memory */
state->strm.zalloc = NULL;
state->strm.zfree = NULL;
state->strm.opaque = NULL;
state->strm.avail_in = 0;
state->strm.next_in = NULL;
if (PREFIX(inflateInit2)(&(state->strm), MAX_WBITS + 16) != Z_OK) { /* gunzip */
zng_free(state->out);
zng_free(state->in);
state->size = 0;
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
}
/* get at least the magic bytes in the input buffer */
if (strm->avail_in < 2) {
if (gz_avail(state) == -1)
return -1;
if (strm->avail_in == 0)
return 0;
}
/* look for gzip magic bytes -- if there, do gzip decoding (note: there is
a logical dilemma here when considering the case of a partially written
gzip file, to wit, if a single 31 byte is written, then we cannot tell
whether this is a single-byte file, or just a partially written gzip
file -- for here we assume that if a gzip file is being written, then
the header will be written in a single operation, so that reading a
single byte is sufficient indication that it is not a gzip file) */
if (strm->avail_in > 1 &&
strm->next_in[0] == 31 && strm->next_in[1] == 139) {
PREFIX(inflateReset)(strm);
state->how = GZIP;
state->direct = 0;
return 0;
}
/* no gzip header -- if we were decoding gzip before, then this is trailing
garbage. Ignore the trailing garbage and finish. */
if (state->direct == 0) {
strm->avail_in = 0;
state->eof = 1;
state->x.have = 0;
return 0;
}
/* doing raw i/o, copy any leftover input to output -- this assumes that
the output buffer is larger than the input buffer, which also assures
space for gzungetc() */
state->x.next = state->out;
memcpy(state->x.next, strm->next_in, strm->avail_in);
state->x.have = strm->avail_in;
strm->avail_in = 0;
state->how = COPY;
state->direct = 1;
return 0;
}
/* Decompress from input to the provided next_out and avail_out in the state.
On return, state->x.have and state->x.next point to the just decompressed
data. If the gzip stream completes, state->how is reset to LOOK to look for
the next gzip stream or raw data, once state->x.have is depleted. Returns 0
on success, -1 on failure. */
static int gz_decomp(gz_state *state) {
int ret = Z_OK;
unsigned had;
PREFIX3(stream) *strm = &(state->strm);
/* fill output buffer up to end of deflate stream */
had = strm->avail_out;
do {
/* get more input for inflate() */
if (strm->avail_in == 0 && gz_avail(state) == -1)
return -1;
if (strm->avail_in == 0) {
gz_error(state, Z_BUF_ERROR, "unexpected end of file");
break;
}
/* decompress and handle errors */
ret = PREFIX(inflate)(strm, Z_NO_FLUSH);
if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
gz_error(state, Z_STREAM_ERROR, "internal error: inflate stream corrupt");
return -1;
}
if (ret == Z_MEM_ERROR) {
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
if (ret == Z_DATA_ERROR) { /* deflate stream invalid */
gz_error(state, Z_DATA_ERROR, strm->msg == NULL ? "compressed data error" : strm->msg);
return -1;
}
} while (strm->avail_out && ret != Z_STREAM_END);
/* update available output */
state->x.have = had - strm->avail_out;
state->x.next = strm->next_out - state->x.have;
/* if the gzip stream completed successfully, look for another */
if (ret == Z_STREAM_END)
state->how = LOOK;
/* good decompression */
return 0;
}
/* Fetch data and put it in the output buffer. Assumes state->x.have is 0.
Data is either copied from the input file or decompressed from the input
file depending on state->how. If state->how is LOOK, then a gzip header is
looked for to determine whether to copy or decompress. Returns -1 on error,
otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the
end of the input file has been reached and all data has been processed. */
static int gz_fetch(gz_state *state) {
PREFIX3(stream) *strm = &(state->strm);
do {
switch (state->how) {
case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
if (gz_look(state) == -1)
return -1;
if (state->how == LOOK)
return 0;
break;
case COPY: /* -> COPY */
if (gz_load(state, state->out, state->size << 1, &(state->x.have))
== -1)
return -1;
state->x.next = state->out;
return 0;
case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */
strm->avail_out = state->size << 1;
strm->next_out = state->out;
if (gz_decomp(state) == -1)
return -1;
}
} while (state->x.have == 0 && (!state->eof || strm->avail_in));
return 0;
}
/* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
static int gz_skip(gz_state *state, z_off64_t len) {
unsigned n;
/* skip over len bytes or reach end-of-file, whichever comes first */
while (len)
/* skip over whatever is in output buffer */
if (state->x.have) {
n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
(unsigned)len : state->x.have;
state->x.have -= n;
state->x.next += n;
state->x.pos += n;
len -= n;
} else if (state->eof && state->strm.avail_in == 0) {
/* output buffer empty -- return if we're at the end of the input */
break;
} else {
/* need more data to skip -- load up output buffer */
/* get more output, looking for header if required */
if (gz_fetch(state) == -1)
return -1;
}
return 0;
}
/* Read len bytes into buf from file, or less than len up to the end of the
input. Return the number of bytes read. If zero is returned, either the
end of file was reached, or there was an error. state->err must be
consulted in that case to determine which. */
static size_t gz_read(gz_state *state, void *buf, size_t len) {
size_t got;
unsigned n;
/* if len is zero, avoid unnecessary operations */
if (len == 0)
return 0;
/* process a skip request */
if (state->seek) {
state->seek = 0;
if (gz_skip(state, state->skip) == -1)
return 0;
}
/* get len bytes to buf, or less than len if at the end */
got = 0;
do {
/* set n to the maximum amount of len that fits in an unsigned int */
n = (unsigned)-1;
if (n > len)
n = (unsigned)len;
/* first just try copying data from the output buffer */
if (state->x.have) {
if (state->x.have < n)
n = state->x.have;
memcpy(buf, state->x.next, n);
state->x.next += n;
state->x.have -= n;
}
/* output buffer empty -- return if we're at the end of the input */
else if (state->eof && state->strm.avail_in == 0) {
state->past = 1; /* tried to read past end */
break;
}
/* need output data -- for small len or new stream load up our output
buffer */
else if (state->how == LOOK || n < (state->size << 1)) {
/* get more output, looking for header if required */
if (gz_fetch(state) == -1)
return 0;
continue; /* no progress yet -- go back to copy above */
/* the copy above assures that we will leave with space in the
output buffer, allowing at least one gzungetc() to succeed */
}
/* large len -- read directly into user buffer */
else if (state->how == COPY) { /* read directly */
if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
return 0;
}
/* large len -- decompress directly into user buffer */
else { /* state->how == GZIP */
state->strm.avail_out = n;
state->strm.next_out = (unsigned char *)buf;
if (gz_decomp(state) == -1)
return 0;
n = state->x.have;
state->x.have = 0;
}
/* update progress */
len -= n;
buf = (char *)buf + n;
got += n;
state->x.pos += n;
} while (len);
/* return number of bytes read into user buffer */
return got;
}
/* -- see zlib.h -- */
int Z_EXPORT PREFIX(gzread)(gzFile file, void *buf, unsigned len) {
gz_state *state;
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_state *)file;
/* check that we're reading and that there's no (serious) error */
if (state->mode != GZ_READ ||
(state->err != Z_OK && state->err != Z_BUF_ERROR))
return -1;
/* since an int is returned, make sure len fits in one, otherwise return
with an error (this avoids a flaw in the interface) */
if ((int)len < 0) {
gz_error(state, Z_STREAM_ERROR, "request does not fit in an int");
return -1;
}
/* read len or fewer bytes to buf */
len = (unsigned)gz_read(state, buf, len);
/* check for an error */
if (len == 0 && state->err != Z_OK && state->err != Z_BUF_ERROR)
return -1;
/* return the number of bytes read (this is assured to fit in an int) */
return (int)len;
}
/* -- see zlib.h -- */
size_t Z_EXPORT PREFIX(gzfread)(void *buf, size_t size, size_t nitems, gzFile file) {
size_t len;
gz_state *state;
/* Exit early if size is zero, also prevents potential division by zero */
if (size == 0)
return 0;
/* get internal structure */
if (file == NULL)
return 0;
state = (gz_state *)file;
/* check that we're reading and that there's no (serious) error */
if (state->mode != GZ_READ ||
(state->err != Z_OK && state->err != Z_BUF_ERROR))
return 0;
/* compute bytes to read -- error on overflow */
if (size && SIZE_MAX / size < nitems) {
gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
return 0;
}
len = nitems * size;
/* read len or fewer bytes to buf, return the number of full items read */
return len ? gz_read(state, buf, len) / size : 0;
}
/* -- see zlib.h -- */
#undef @ZLIB_SYMBOL_PREFIX@gzgetc
#undef @ZLIB_SYMBOL_PREFIX@zng_gzgetc
int Z_EXPORT PREFIX(gzgetc)(gzFile file) {
unsigned char buf[1];
gz_state *state;
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_state *)file;
/* check that we're reading and that there's no (serious) error */
if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
return -1;
/* try output buffer (no need to check for skip request) */
if (state->x.have) {
state->x.have--;
state->x.pos++;
return *(state->x.next)++;
}
/* nothing there -- try gz_read() */
return gz_read(state, buf, 1) < 1 ? -1 : buf[0];
}
#ifdef ZLIB_COMPAT
int Z_EXPORT PREFIX(gzgetc_)(gzFile file) {
return PREFIX(gzgetc)(file);
}
#endif
/* -- see zlib.h -- */
int Z_EXPORT PREFIX(gzungetc)(int c, gzFile file) {
gz_state *state;
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_state *)file;
/* in case this was just opened, set up the input buffer */
if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
(void)gz_look(state);
/* check that we're reading and that there's no (serious) error */
if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
return -1;
/* process a skip request */
if (state->seek) {
state->seek = 0;
if (gz_skip(state, state->skip) == -1)
return -1;
}
/* can't push EOF */
if (c < 0)
return -1;
/* if output buffer empty, put byte at end (allows more pushing) */
if (state->x.have == 0) {
state->x.have = 1;
state->x.next = state->out + (state->size << 1) - 1;
state->x.next[0] = (unsigned char)c;
state->x.pos--;
state->past = 0;
return c;
}
/* if no room, give up (must have already done a gzungetc()) */
if (state->x.have == (state->size << 1)) {
gz_error(state, Z_DATA_ERROR, "out of room to push characters");
return -1;
}
/* slide output data if needed and insert byte before existing data */
if (state->x.next == state->out) {
unsigned char *src = state->out + state->x.have;
unsigned char *dest = state->out + (state->size << 1);
while (src > state->out)
*--dest = *--src;
state->x.next = dest;
}
state->x.have++;
state->x.next--;
state->x.next[0] = (unsigned char)c;
state->x.pos--;
state->past = 0;
return c;
}
/* -- see zlib.h -- */
char * Z_EXPORT PREFIX(gzgets)(gzFile file, char *buf, int len) {
unsigned left, n;
char *str;
unsigned char *eol;
gz_state *state;
/* check parameters and get internal structure */
if (file == NULL || buf == NULL || len < 1)
return NULL;
state = (gz_state *)file;
/* check that we're reading and that there's no (serious) error */
if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
return NULL;
/* process a skip request */
if (state->seek) {
state->seek = 0;
if (gz_skip(state, state->skip) == -1)
return NULL;
}
/* copy output bytes up to new line or len - 1, whichever comes first --
append a terminating zero to the string (we don't check for a zero in
the contents, let the user worry about that) */
str = buf;
left = (unsigned)len - 1;
if (left) {
do {
/* assure that something is in the output buffer */
if (state->x.have == 0 && gz_fetch(state) == -1)
return NULL; /* error */
if (state->x.have == 0) { /* end of file */
state->past = 1; /* read past end */
break; /* return what we have */
}
/* look for end-of-line in current output buffer */
n = state->x.have > left ? left : state->x.have;
eol = (unsigned char *)memchr(state->x.next, '\n', n);
if (eol != NULL)
n = (unsigned)(eol - state->x.next) + 1;
/* copy through end-of-line, or remainder if not found */
memcpy(buf, state->x.next, n);
state->x.have -= n;
state->x.next += n;
state->x.pos += n;
left -= n;
buf += n;
} while (left && eol == NULL);
}
/* return terminated string, or if nothing, end of file */
if (buf == str)
return NULL;
buf[0] = 0;
return str;
}
/* -- see zlib.h -- */
int Z_EXPORT PREFIX(gzdirect)(gzFile file) {
gz_state *state;
/* get internal structure */
if (file == NULL)
return 0;
state = (gz_state *)file;
/* if the state is not known, but we can find out, then do so (this is
mainly for right after a gzopen() or gzdopen()) */
if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
(void)gz_look(state);
/* return 1 if transparent, 0 if processing a gzip stream */
return state->direct;
}
/* -- see zlib.h -- */
int Z_EXPORT PREFIX(gzclose_r)(gzFile file) {
int ret, err;
gz_state *state;
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_state *)file;
/* check that we're reading */
if (state->mode != GZ_READ)
return Z_STREAM_ERROR;
/* free memory and close file */
if (state->size) {
PREFIX(inflateEnd)(&(state->strm));
zng_free(state->out);
zng_free(state->in);
}
err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
gz_error(state, Z_OK, NULL);
free(state->path);
ret = close(state->fd);
zng_free(state);
return ret ? Z_ERRNO : err;
}
+526
View File
@@ -0,0 +1,526 @@
/* gzwrite.c -- zlib functions for writing gzip files
* Copyright (C) 2004-2019 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "zutil_p.h"
#include <stdarg.h>
#include "gzguts.h"
/* Local functions */
static int gz_init(gz_state *);
static int gz_comp(gz_state *, int);
static int gz_zero(gz_state *, z_off64_t);
static size_t gz_write(gz_state *, void const *, size_t);
/* Initialize state for writing a gzip file. Mark initialization by setting
state->size to non-zero. Return -1 on a memory allocation failure, or 0 on
success. */
static int gz_init(gz_state *state) {
int ret;
PREFIX3(stream) *strm = &(state->strm);
/* allocate input buffer (double size for gzprintf) */
state->in = (unsigned char *)zng_alloc(state->want << 1);
if (state->in == NULL) {
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
memset(state->in, 0, state->want << 1);
/* only need output buffer and deflate state if compressing */
if (!state->direct) {
/* allocate output buffer */
state->out = (unsigned char *)zng_alloc(state->want);
if (state->out == NULL) {
zng_free(state->in);
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
/* allocate deflate memory, set up for gzip compression */
strm->zalloc = NULL;
strm->zfree = NULL;
strm->opaque = NULL;
ret = PREFIX(deflateInit2)(strm, state->level, Z_DEFLATED, MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
if (ret != Z_OK) {
zng_free(state->out);
zng_free(state->in);
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
strm->next_in = NULL;
}
/* mark state as initialized */
state->size = state->want;
/* initialize write buffer if compressing */
if (!state->direct) {
strm->avail_out = state->size;
strm->next_out = state->out;
state->x.next = strm->next_out;
}
return 0;
}
/* Compress whatever is at avail_in and next_in and write to the output file.
Return -1 if there is an error writing to the output file or if gz_init()
fails to allocate memory, otherwise 0. flush is assumed to be a valid
deflate() flush value. If flush is Z_FINISH, then the deflate() state is
reset to start a new gzip stream. If gz->direct is true, then simply write
to the output file without compressing, and ignore flush. */
static int gz_comp(gz_state *state, int flush) {
int ret;
ssize_t got;
unsigned have;
PREFIX3(stream) *strm = &(state->strm);
/* allocate memory if this is the first time through */
if (state->size == 0 && gz_init(state) == -1)
return -1;
/* write directly if requested */
if (state->direct) {
got = write(state->fd, strm->next_in, strm->avail_in);
if (got < 0 || (unsigned)got != strm->avail_in) {
gz_error(state, Z_ERRNO, zstrerror());
return -1;
}
strm->avail_in = 0;
return 0;
}
/* check for a pending reset */
if (state->reset) {
/* don't start a new gzip member unless there is data to write */
if (strm->avail_in == 0)
return 0;
PREFIX(deflateReset)(strm);
state->reset = 0;
}
/* run deflate() on provided input until it produces no more output */
ret = Z_OK;
do {
/* write out current buffer contents if full, or if flushing, but if
doing Z_FINISH then don't write until we get to Z_STREAM_END */
if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && (flush != Z_FINISH || ret == Z_STREAM_END))) {
have = (unsigned)(strm->next_out - state->x.next);
if (have && ((got = write(state->fd, state->x.next, (unsigned long)have)) < 0 || (unsigned)got != have)) {
gz_error(state, Z_ERRNO, zstrerror());
return -1;
}
if (strm->avail_out == 0) {
strm->avail_out = state->size;
strm->next_out = state->out;
state->x.next = state->out;
}
state->x.next = strm->next_out;
}
/* compress */
have = strm->avail_out;
ret = PREFIX(deflate)(strm, flush);
if (ret == Z_STREAM_ERROR) {
gz_error(state, Z_STREAM_ERROR, "internal error: deflate stream corrupt");
return -1;
}
have -= strm->avail_out;
} while (have);
/* if that completed a deflate stream, allow another to start */
if (flush == Z_FINISH)
state->reset = 1;
/* all done, no errors */
return 0;
}
/* Compress len zeros to output. Return -1 on a write error or memory
allocation failure by gz_comp(), or 0 on success. */
static int gz_zero(gz_state *state, z_off64_t len) {
int first;
unsigned n;
PREFIX3(stream) *strm = &(state->strm);
/* consume whatever's left in the input buffer */
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
return -1;
/* compress len zeros (len guaranteed > 0) */
first = 1;
while (len) {
n = GT_OFF(state->size) || (z_off64_t)state->size > len ? (unsigned)len : state->size;
if (first) {
memset(state->in, 0, n);
first = 0;
}
strm->avail_in = n;
strm->next_in = state->in;
state->x.pos += n;
if (gz_comp(state, Z_NO_FLUSH) == -1)
return -1;
len -= n;
}
return 0;
}
/* Write len bytes from buf to file. Return the number of bytes written. If
the returned value is less than len, then there was an error. */
static size_t gz_write(gz_state *state, void const *buf, size_t len) {
size_t put = len;
/* if len is zero, avoid unnecessary operations */
if (len == 0)
return 0;
/* allocate memory if this is the first time through */
if (state->size == 0 && gz_init(state) == -1)
return 0;
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return 0;
}
/* for small len, copy to input buffer, otherwise compress directly */
if (len < state->size) {
/* copy to input buffer, compress when full */
do {
unsigned have, copy;
if (state->strm.avail_in == 0)
state->strm.next_in = state->in;
have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
state->in);
copy = state->size - have;
if (copy > len)
copy = (unsigned)len;
memcpy(state->in + have, buf, copy);
state->strm.avail_in += copy;
state->x.pos += copy;
buf = (const char *)buf + copy;
len -= copy;
if (len && gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
} while (len);
} else {
/* consume whatever's left in the input buffer */
if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
/* directly compress user buffer to file */
state->strm.next_in = (z_const unsigned char *) buf;
do {
unsigned n = (unsigned)-1;
if (n > len)
n = (unsigned)len;
state->strm.avail_in = n;
state->x.pos += n;
if (gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
len -= n;
} while (len);
}
/* input was all buffered or compressed */
return put;
}
/* -- see zlib.h -- */
int Z_EXPORT PREFIX(gzwrite)(gzFile file, void const *buf, unsigned len) {
gz_state *state;
/* get internal structure */
if (file == NULL)
return 0;
state = (gz_state *)file;
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return 0;
/* since an int is returned, make sure len fits in one, otherwise return
with an error (this avoids a flaw in the interface) */
if ((int)len < 0) {
gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
return 0;
}
/* write len bytes from buf (the return value will fit in an int) */
return (int)gz_write(state, buf, len);
}
/* -- see zlib.h -- */
size_t Z_EXPORT PREFIX(gzfwrite)(void const *buf, size_t size, size_t nitems, gzFile file) {
size_t len;
gz_state *state;
/* Exit early if size is zero, also prevents potential division by zero */
if (size == 0)
return 0;
/* get internal structure */
if (file == NULL)
return 0;
state = (gz_state *)file;
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return 0;
/* compute bytes to read -- error on overflow */
len = nitems * size;
if (len / size != nitems) {
gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
return 0;
}
/* write len bytes to buf, return the number of full items written */
return len ? gz_write(state, buf, len) / size : 0;
}
/* -- see zlib.h -- */
int Z_EXPORT PREFIX(gzputc)(gzFile file, int c) {
unsigned have;
unsigned char buf[1];
gz_state *state;
PREFIX3(stream) *strm;
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_state *)file;
strm = &(state->strm);
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return -1;
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return -1;
}
/* try writing to input buffer for speed (state->size == 0 if buffer not
initialized) */
if (state->size) {
if (strm->avail_in == 0)
strm->next_in = state->in;
have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
if (have < state->size) {
state->in[have] = (unsigned char)c;
strm->avail_in++;
state->x.pos++;
return c & 0xff;
}
}
/* no room in buffer or not initialized, use gz_write() */
buf[0] = (unsigned char)c;
if (gz_write(state, buf, 1) != 1)
return -1;
return c & 0xff;
}
/* -- see zlib.h -- */
int Z_EXPORT PREFIX(gzputs)(gzFile file, const char *s) {
size_t len, put;
gz_state *state;
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_state *)file;
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return -1;
/* write string */
len = strlen(s);
if ((int)len < 0 || (unsigned)len != len) {
gz_error(state, Z_STREAM_ERROR, "string length does not fit in int");
return -1;
}
put = gz_write(state, s, len);
return put < len ? -1 : (int)len;
}
/* -- see zlib.h -- */
int Z_EXPORTVA PREFIX(gzvprintf)(gzFile file, const char *format, va_list va) {
int len;
unsigned left;
char *next;
gz_state *state;
PREFIX3(stream) *strm;
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_state *)file;
strm = &(state->strm);
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return Z_STREAM_ERROR;
/* make sure we have some buffer space */
if (state->size == 0 && gz_init(state) == -1)
return state->err;
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return state->err;
}
/* do the printf() into the input buffer, put length in len -- the input
buffer is double-sized just for this function, so there is guaranteed to
be state->size bytes available after the current contents */
if (strm->avail_in == 0)
strm->next_in = state->in;
next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in);
next[state->size - 1] = 0;
len = vsnprintf(next, state->size, format, va);
/* check that printf() results fit in buffer */
if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0)
return 0;
/* update buffer and position, compress first half if past that */
strm->avail_in += (unsigned)len;
state->x.pos += len;
if (strm->avail_in >= state->size) {
left = strm->avail_in - state->size;
strm->avail_in = state->size;
if (gz_comp(state, Z_NO_FLUSH) == -1)
return state->err;
memmove(state->in, state->in + state->size, left);
strm->next_in = state->in;
strm->avail_in = left;
}
return len;
}
int Z_EXPORTVA PREFIX(gzprintf)(gzFile file, const char *format, ...) {
va_list va;
int ret;
va_start(va, format);
ret = PREFIX(gzvprintf)(file, format, va);
va_end(va);
return ret;
}
/* -- see zlib.h -- */
int Z_EXPORT PREFIX(gzflush)(gzFile file, int flush) {
gz_state *state;
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_state *)file;
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return Z_STREAM_ERROR;
/* check flush parameter */
if (flush < 0 || flush > Z_FINISH)
return Z_STREAM_ERROR;
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return state->err;
}
/* compress remaining data with requested flush */
(void)gz_comp(state, flush);
return state->err;
}
/* -- see zlib.h -- */
int Z_EXPORT PREFIX(gzsetparams)(gzFile file, int level, int strategy) {
gz_state *state;
PREFIX3(stream) *strm;
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_state *)file;
strm = &(state->strm);
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK || state->direct)
return Z_STREAM_ERROR;
/* if no change is requested, then do nothing */
if (level == state->level && strategy == state->strategy)
return Z_OK;
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return state->err;
}
/* change compression parameters for subsequent input */
if (state->size) {
/* flush previous input with previous parameters before changing */
if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
return state->err;
PREFIX(deflateParams)(strm, level, strategy);
}
state->level = level;
state->strategy = strategy;
return Z_OK;
}
/* -- see zlib.h -- */
int Z_EXPORT PREFIX(gzclose_w)(gzFile file) {
int ret = Z_OK;
gz_state *state;
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_state *)file;
/* check that we're writing */
if (state->mode != GZ_WRITE)
return Z_STREAM_ERROR;
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
ret = state->err;
}
/* flush, free memory, and close file */
if (gz_comp(state, Z_FINISH) == -1)
ret = state->err;
if (state->size) {
if (!state->direct) {
(void)PREFIX(deflateEnd)(&(state->strm));
zng_free(state->out);
}
zng_free(state->in);
}
gz_error(state, Z_OK, NULL);
free(state->path);
if (close(state->fd) == -1)
ret = Z_ERRNO;
zng_free(state);
return ret;
}
+523
View File
@@ -0,0 +1,523 @@
/* infback.c -- inflate using a call-back interface
* Copyright (C) 1995-2022 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/*
This code is largely copied from inflate.c. Normally either infback.o or
inflate.o would be linked into an application--not both. The interface
with inffast.c is retained so that optimized assembler-coded versions of
inflate_fast() can be used with either inflate.c or infback.c.
*/
#include "zbuild.h"
#include "zutil.h"
#include "inftrees.h"
#include "inflate.h"
#include "inflate_p.h"
#include "functable.h"
/* Avoid conflicts with zlib.h macros */
#ifdef ZLIB_COMPAT
# undef inflateBackInit
#endif
/*
strm provides memory allocation functions in zalloc and zfree, or
NULL to use the library memory allocation functions.
windowBits is in the range 8..15, and window is a user-supplied
window and output buffer that is 2**windowBits bytes.
This function is hidden in ZLIB_COMPAT builds.
*/
int32_t ZNG_CONDEXPORT PREFIX(inflateBackInit)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window) {
struct inflate_state *state;
if (strm == NULL || window == NULL || windowBits < MIN_WBITS || windowBits > MAX_WBITS)
return Z_STREAM_ERROR;
strm->msg = NULL; /* in case we return an error */
if (strm->zalloc == NULL) {
strm->zalloc = PREFIX(zcalloc);
strm->opaque = NULL;
}
if (strm->zfree == NULL)
strm->zfree = PREFIX(zcfree);
inflate_allocs *alloc_bufs = alloc_inflate(strm);
if (alloc_bufs == NULL)
return Z_MEM_ERROR;
state = alloc_bufs->state;
state->alloc_bufs = alloc_bufs;
Tracev((stderr, "inflate: allocated\n"));
strm->state = (struct internal_state *)state;
state->wbits = (unsigned int)windowBits;
state->wsize = 1U << windowBits;
state->wbufsize = 1U << windowBits;
state->window = window;
state->wnext = 0;
state->whave = 0;
state->chunksize = FUNCTABLE_CALL(chunksize)();
#ifdef INFLATE_STRICT
state->dmax = 32768U;
#endif
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
state->sane = 1;
#endif
return Z_OK;
}
/* Function used by zlib.h and zlib-ng version 2.0 macros */
int32_t Z_EXPORT PREFIX(inflateBackInit_)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window,
const char *version, int32_t stream_size) {
if (CHECK_VER_STSIZE(version, stream_size))
return Z_VERSION_ERROR;
return PREFIX(inflateBackInit)(strm, windowBits, window);
}
/*
Private macros for inflateBack()
Look in inflate_p.h for macros shared with inflate()
*/
/* Assure that some input is available. If input is requested, but denied,
then return a Z_BUF_ERROR from inflateBack(). */
#define PULL() \
do { \
if (have == 0) { \
have = in(in_desc, &next); \
if (have == 0) { \
next = NULL; \
ret = Z_BUF_ERROR; \
goto inf_leave; \
} \
} \
} while (0)
/* Get a byte of input into the bit accumulator, or return from inflateBack()
with an error if there is no input available. */
#define PULLBYTE() \
do { \
PULL(); \
have--; \
hold += ((uint64_t)(*next++) << bits); \
bits += 8; \
} while (0)
/* Assure that some output space is available, by writing out the window
if it's full. If the write fails, return from inflateBack() with a
Z_BUF_ERROR. */
#define ROOM() \
do { \
if (left == 0) { \
put = state->window; \
left = state->wsize; \
state->whave = left; \
if (out(out_desc, put, left)) { \
ret = Z_BUF_ERROR; \
goto inf_leave; \
} \
} \
} while (0)
/*
strm provides the memory allocation functions and window buffer on input,
and provides information on the unused input on return. For Z_DATA_ERROR
returns, strm will also provide an error message.
in() and out() are the call-back input and output functions. When
inflateBack() needs more input, it calls in(). When inflateBack() has
filled the window with output, or when it completes with data in the
window, it calls out() to write out the data. The application must not
change the provided input until in() is called again or inflateBack()
returns. The application must not change the window/output buffer until
inflateBack() returns.
in() and out() are called with a descriptor parameter provided in the
inflateBack() call. This parameter can be a structure that provides the
information required to do the read or write, as well as accumulated
information on the input and output such as totals and check values.
in() should return zero on failure. out() should return non-zero on
failure. If either in() or out() fails, than inflateBack() returns a
Z_BUF_ERROR. strm->next_in can be checked for NULL to see whether it
was in() or out() that caused in the error. Otherwise, inflateBack()
returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
error, or Z_MEM_ERROR if it could not allocate memory for the state.
inflateBack() can also return Z_STREAM_ERROR if the input parameters
are not correct, i.e. strm is NULL or the state was not initialized.
*/
int32_t Z_EXPORT PREFIX(inflateBack)(PREFIX3(stream) *strm, in_func in, void *in_desc, out_func out, void *out_desc) {
struct inflate_state *state;
z_const unsigned char *next; /* next input */
unsigned char *put; /* next output */
unsigned have, left; /* available input and output */
uint64_t hold; /* bit buffer */
unsigned bits; /* bits in bit buffer */
unsigned copy; /* number of stored or match bytes to copy */
unsigned char *from; /* where to copy match bytes from */
code here; /* current decoding table entry */
code last; /* parent table entry */
unsigned len; /* length to copy for repeats, bits to drop */
int32_t ret; /* return code */
static const uint16_t order[19] = /* permutation of code lengths */
{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
/* Check that the strm exists and that the state was initialized */
if (strm == NULL || strm->state == NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
/* Reset the state */
strm->msg = NULL;
state->mode = TYPE;
state->last = 0;
state->whave = 0;
next = strm->next_in;
have = next != NULL ? strm->avail_in : 0;
hold = 0;
bits = 0;
put = state->window;
left = state->wsize;
/* Inflate until end of block marked as last */
for (;;)
switch (state->mode) {
case TYPE:
/* determine and dispatch block type */
if (state->last) {
BYTEBITS();
state->mode = DONE;
break;
}
NEEDBITS(3);
state->last = BITS(1);
DROPBITS(1);
switch (BITS(2)) {
case 0: /* stored block */
Tracev((stderr, "inflate: stored block%s\n", state->last ? " (last)" : ""));
state->mode = STORED;
break;
case 1: /* fixed block */
PREFIX(fixedtables)(state);
Tracev((stderr, "inflate: fixed codes block%s\n", state->last ? " (last)" : ""));
state->mode = LEN; /* decode codes */
break;
case 2: /* dynamic block */
Tracev((stderr, "inflate: dynamic codes block%s\n", state->last ? " (last)" : ""));
state->mode = TABLE;
break;
case 3:
SET_BAD("invalid block type");
}
DROPBITS(2);
break;
case STORED:
/* get and verify stored block length */
BYTEBITS(); /* go to byte boundary */
NEEDBITS(32);
if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
SET_BAD("invalid stored block lengths");
break;
}
state->length = (uint16_t)hold;
Tracev((stderr, "inflate: stored length %u\n", state->length));
INITBITS();
/* copy stored block from input to output */
while (state->length != 0) {
copy = state->length;
PULL();
ROOM();
copy = MIN(copy, have);
copy = MIN(copy, left);
memcpy(put, next, copy);
have -= copy;
next += copy;
left -= copy;
put += copy;
state->length -= copy;
}
Tracev((stderr, "inflate: stored end\n"));
state->mode = TYPE;
break;
case TABLE:
/* get dynamic table entries descriptor */
NEEDBITS(14);
state->nlen = BITS(5) + 257;
DROPBITS(5);
state->ndist = BITS(5) + 1;
DROPBITS(5);
state->ncode = BITS(4) + 4;
DROPBITS(4);
#ifndef PKZIP_BUG_WORKAROUND
if (state->nlen > 286 || state->ndist > 30) {
SET_BAD("too many length or distance symbols");
break;
}
#endif
Tracev((stderr, "inflate: table sizes ok\n"));
state->have = 0;
/* get code length code lengths (not a typo) */
while (state->have < state->ncode) {
NEEDBITS(3);
state->lens[order[state->have++]] = (uint16_t)BITS(3);
DROPBITS(3);
}
while (state->have < 19)
state->lens[order[state->have++]] = 0;
state->next = state->codes;
state->lencode = (const code *)(state->next);
state->lenbits = 7;
ret = zng_inflate_table(CODES, state->lens, 19, &(state->next), &(state->lenbits), state->work);
if (ret) {
SET_BAD("invalid code lengths set");
break;
}
Tracev((stderr, "inflate: code lengths ok\n"));
state->have = 0;
/* get length and distance code code lengths */
while (state->have < state->nlen + state->ndist) {
for (;;) {
here = state->lencode[BITS(state->lenbits)];
if (here.bits <= bits) break;
PULLBYTE();
}
if (here.val < 16) {
DROPBITS(here.bits);
state->lens[state->have++] = here.val;
} else {
if (here.val == 16) {
NEEDBITS(here.bits + 2);
DROPBITS(here.bits);
if (state->have == 0) {
SET_BAD("invalid bit length repeat");
break;
}
len = state->lens[state->have - 1];
copy = 3 + BITS(2);
DROPBITS(2);
} else if (here.val == 17) {
NEEDBITS(here.bits + 3);
DROPBITS(here.bits);
len = 0;
copy = 3 + BITS(3);
DROPBITS(3);
} else {
NEEDBITS(here.bits + 7);
DROPBITS(here.bits);
len = 0;
copy = 11 + BITS(7);
DROPBITS(7);
}
if (state->have + copy > state->nlen + state->ndist) {
SET_BAD("invalid bit length repeat");
break;
}
while (copy) {
--copy;
state->lens[state->have++] = (uint16_t)len;
}
}
}
/* handle error breaks in while */
if (state->mode == BAD)
break;
/* check for end-of-block code (better have one) */
if (state->lens[256] == 0) {
SET_BAD("invalid code -- missing end-of-block");
break;
}
/* build code tables -- note: do not change the lenbits or distbits
values here (10 and 9) without reading the comments in inftrees.h
concerning the ENOUGH constants, which depend on those values */
state->next = state->codes;
state->lencode = (const code *)(state->next);
state->lenbits = 10;
ret = zng_inflate_table(LENS, state->lens, state->nlen, &(state->next), &(state->lenbits), state->work);
if (ret) {
SET_BAD("invalid literal/lengths set");
break;
}
state->distcode = (const code *)(state->next);
state->distbits = 9;
ret = zng_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
&(state->next), &(state->distbits), state->work);
if (ret) {
SET_BAD("invalid distances set");
break;
}
Tracev((stderr, "inflate: codes ok\n"));
state->mode = LEN;
Z_FALLTHROUGH;
case LEN:
/* use inflate_fast() if we have enough input and output */
if (have >= INFLATE_FAST_MIN_HAVE &&
left >= INFLATE_FAST_MIN_LEFT) {
RESTORE();
if (state->whave < state->wsize)
state->whave = state->wsize - left;
FUNCTABLE_CALL(inflate_fast)(strm, state->wsize);
LOAD();
break;
}
/* get a literal, length, or end-of-block code */
for (;;) {
here = state->lencode[BITS(state->lenbits)];
if (here.bits <= bits)
break;
PULLBYTE();
}
if (here.op && (here.op & 0xf0) == 0) {
last = here;
for (;;) {
here = state->lencode[last.val + (BITS(last.bits + last.op) >> last.bits)];
if ((unsigned)last.bits + (unsigned)here.bits <= bits)
break;
PULLBYTE();
}
DROPBITS(last.bits);
}
DROPBITS(here.bits);
state->length = here.val;
/* process literal */
if ((int)(here.op) == 0) {
Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
"inflate: literal '%c'\n" :
"inflate: literal 0x%02x\n", here.val));
ROOM();
*put++ = (unsigned char)(state->length);
left--;
state->mode = LEN;
break;
}
/* process end of block */
if (here.op & 32) {
Tracevv((stderr, "inflate: end of block\n"));
state->mode = TYPE;
break;
}
/* invalid code */
if (here.op & 64) {
SET_BAD("invalid literal/length code");
break;
}
/* length code -- get extra bits, if any */
state->extra = (here.op & MAX_BITS);
if (state->extra) {
NEEDBITS(state->extra);
state->length += BITS(state->extra);
DROPBITS(state->extra);
}
Tracevv((stderr, "inflate: length %u\n", state->length));
/* get distance code */
for (;;) {
here = state->distcode[BITS(state->distbits)];
if (here.bits <= bits)
break;
PULLBYTE();
}
if ((here.op & 0xf0) == 0) {
last = here;
for (;;) {
here = state->distcode[last.val + (BITS(last.bits + last.op) >> last.bits)];
if ((unsigned)last.bits + (unsigned)here.bits <= bits)
break;
PULLBYTE();
}
DROPBITS(last.bits);
}
DROPBITS(here.bits);
if (here.op & 64) {
SET_BAD("invalid distance code");
break;
}
state->offset = here.val;
state->extra = (here.op & MAX_BITS);
/* get distance extra bits, if any */
if (state->extra) {
NEEDBITS(state->extra);
state->offset += BITS(state->extra);
DROPBITS(state->extra);
}
#ifdef INFLATE_STRICT
if (state->offset > state->wsize - (state->whave < state->wsize ? left : 0)) {
SET_BAD("invalid distance too far back");
break;
}
#endif
Tracevv((stderr, "inflate: distance %u\n", state->offset));
/* copy match from window to output */
do {
ROOM();
copy = state->wsize - state->offset;
if (copy < left) {
from = put + copy;
copy = left - copy;
} else {
from = put - state->offset;
copy = left;
}
copy = MIN(copy, state->length);
state->length -= copy;
left -= copy;
do {
*put++ = *from++;
} while (--copy);
} while (state->length != 0);
break;
case DONE:
/* inflate stream terminated properly */
ret = Z_STREAM_END;
goto inf_leave;
case BAD:
ret = Z_DATA_ERROR;
goto inf_leave;
default: /* can't happen, but makes compilers happy */
ret = Z_STREAM_ERROR;
goto inf_leave;
}
/* Write leftover output and return unused input */
inf_leave:
if (left < state->wsize) {
if (out(out_desc, state->window, state->wsize - left) && (ret == Z_STREAM_END)) {
ret = Z_BUF_ERROR;
}
}
strm->next_in = next;
strm->avail_in = have;
return ret;
}
int32_t Z_EXPORT PREFIX(inflateBackEnd)(PREFIX3(stream) *strm) {
if (strm == NULL || strm->state == NULL || strm->zfree == NULL)
return Z_STREAM_ERROR;
/* Free allocated buffers */
free_inflate(strm);
Tracev((stderr, "inflate: end\n"));
return Z_OK;
}
+332
View File
@@ -0,0 +1,332 @@
/* inffast.c -- fast decoding
* Copyright (C) 1995-2017 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "zendian.h"
#include "zutil.h"
#include "inftrees.h"
#include "inflate.h"
#include "inflate_p.h"
#include "functable.h"
/*
Decode literal, length, and distance codes and write out the resulting
literal and match bytes until either not enough input or output is
available, an end-of-block is encountered, or a data error is encountered.
When large enough input and output buffers are supplied to inflate(), for
example, a 16K input buffer and a 64K output buffer, more than 95% of the
inflate execution time is spent in this routine.
Entry assumptions:
state->mode == LEN
strm->avail_in >= INFLATE_FAST_MIN_HAVE
strm->avail_out >= INFLATE_FAST_MIN_LEFT
start >= strm->avail_out
state->bits < 8
On return, state->mode is one of:
LEN -- ran out of enough output space or enough available input
TYPE -- reached end of block code, inflate() to interpret next block
BAD -- error in block data
Notes:
- The maximum input bits used by a length/distance pair is 15 bits for the
length code, 5 bits for the length extra, 15 bits for the distance code,
and 13 bits for the distance extra. This totals 48 bits, or six bytes.
Therefore if strm->avail_in >= 6, then there is enough input to avoid
checking for available input while decoding.
- On some architectures, it can be significantly faster (e.g. up to 1.2x
faster on x86_64) to load from strm->next_in 64 bits, or 8 bytes, at a
time, so INFLATE_FAST_MIN_HAVE == 8.
- The maximum bytes that a single length/distance pair can output is 258
bytes, which is the maximum length that can be coded. inflate_fast()
requires strm->avail_out >= 258 for each loop to avoid checking for
output space.
*/
void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start) {
/* start: inflate()'s starting value for strm->avail_out */
struct inflate_state *state;
z_const unsigned char *in; /* local strm->next_in */
const unsigned char *last; /* have enough input while in < last */
unsigned char *out; /* local strm->next_out */
unsigned char *beg; /* inflate()'s initial strm->next_out */
unsigned char *end; /* while out < end, enough space available */
unsigned char *safe; /* can use chunkcopy provided out < safe */
unsigned char *window; /* allocated sliding window, if wsize != 0 */
unsigned wsize; /* window size or zero if not using window */
unsigned whave; /* valid bytes in the window */
unsigned wnext; /* window write index */
/* hold is a local copy of strm->hold. By default, hold satisfies the same
invariants that strm->hold does, namely that (hold >> bits) == 0. This
invariant is kept by loading bits into hold one byte at a time, like:
hold |= next_byte_of_input << bits; in++; bits += 8;
If we need to ensure that bits >= 15 then this code snippet is simply
repeated. Over one iteration of the outermost do/while loop, this
happens up to six times (48 bits of input), as described in the NOTES
above.
However, on some little endian architectures, it can be significantly
faster to load 64 bits once instead of 8 bits six times:
if (bits <= 16) {
hold |= next_8_bytes_of_input << bits; in += 6; bits += 48;
}
Unlike the simpler one byte load, shifting the next_8_bytes_of_input
by bits will overflow and lose those high bits, up to 2 bytes' worth.
The conservative estimate is therefore that we have read only 6 bytes
(48 bits). Again, as per the NOTES above, 48 bits is sufficient for the
rest of the iteration, and we will not need to load another 8 bytes.
Inside this function, we no longer satisfy (hold >> bits) == 0, but
this is not problematic, even if that overflow does not land on an 8 bit
byte boundary. Those excess bits will eventually shift down lower as the
Huffman decoder consumes input, and when new input bits need to be loaded
into the bits variable, the same input bits will be or'ed over those
existing bits. A bitwise or is idempotent: (a | b | b) equals (a | b).
Note that we therefore write that load operation as "hold |= etc" and not
"hold += etc".
Outside that loop, at the end of the function, hold is bitwise and'ed
with (1<<bits)-1 to drop those excess bits so that, on function exit, we
keep the invariant that (state->hold >> state->bits) == 0.
*/
unsigned bits; /* local strm->bits */
uint64_t hold; /* local strm->hold */
unsigned lmask; /* mask for first level of length codes */
unsigned dmask; /* mask for first level of distance codes */
code const *lcode; /* local strm->lencode */
code const *dcode; /* local strm->distcode */
const code *here; /* retrieved table entry */
unsigned op; /* code bits, operation, extra bits, or */
/* window position, window bytes to copy */
unsigned len; /* match length, unused bytes */
unsigned char *from; /* where to copy match from */
unsigned dist; /* match distance */
unsigned extra_safe; /* copy chunks safely in all cases */
/* copy state to local variables */
state = (struct inflate_state *)strm->state;
in = strm->next_in;
last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1));
out = strm->next_out;
beg = out - (start - strm->avail_out);
end = out + (strm->avail_out - (INFLATE_FAST_MIN_LEFT - 1));
safe = out + strm->avail_out;
wsize = state->wsize;
whave = state->whave;
wnext = state->wnext;
window = state->window;
hold = state->hold;
bits = state->bits;
lcode = state->lencode;
dcode = state->distcode;
lmask = (1U << state->lenbits) - 1;
dmask = (1U << state->distbits) - 1;
/* Detect if out and window point to the same memory allocation. In this instance it is
necessary to use safe chunk copy functions to prevent overwriting the window. If the
window is overwritten then future matches with far distances will fail to copy correctly. */
extra_safe = (wsize != 0 && out >= window && out + INFLATE_FAST_MIN_LEFT <= window + state->wbufsize);
#define REFILL() do { \
hold |= load_64_bits(in, bits); \
in += 7; \
in -= ((bits >> 3) & 7); \
bits |= 56; \
} while (0)
/* decode literals and length/distances until end-of-block or not enough
input data or output space */
do {
REFILL();
here = lcode + (hold & lmask);
if (here->op == 0) {
*out++ = (unsigned char)(here->val);
DROPBITS(here->bits);
here = lcode + (hold & lmask);
if (here->op == 0) {
*out++ = (unsigned char)(here->val);
DROPBITS(here->bits);
here = lcode + (hold & lmask);
}
}
dolen:
DROPBITS(here->bits);
op = here->op;
if (op == 0) { /* literal */
Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ?
"inflate: literal '%c'\n" :
"inflate: literal 0x%02x\n", here->val));
*out++ = (unsigned char)(here->val);
} else if (op & 16) { /* length base */
len = here->val;
op &= MAX_BITS; /* number of extra bits */
len += BITS(op);
DROPBITS(op);
Tracevv((stderr, "inflate: length %u\n", len));
here = dcode + (hold & dmask);
if (bits < MAX_BITS + MAX_DIST_EXTRA_BITS) {
REFILL();
}
dodist:
DROPBITS(here->bits);
op = here->op;
if (op & 16) { /* distance base */
dist = here->val;
op &= MAX_BITS; /* number of extra bits */
dist += BITS(op);
#ifdef INFLATE_STRICT
if (dist > state->dmax) {
SET_BAD("invalid distance too far back");
break;
}
#endif
DROPBITS(op);
Tracevv((stderr, "inflate: distance %u\n", dist));
op = (unsigned)(out - beg); /* max distance in output */
if (dist > op) { /* see if copy from window */
op = dist - op; /* distance back in window */
if (op > whave) {
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
if (state->sane) {
SET_BAD("invalid distance too far back");
break;
}
if (len <= op - whave) {
do {
*out++ = 0;
} while (--len);
continue;
}
len -= op - whave;
do {
*out++ = 0;
} while (--op > whave);
if (op == 0) {
from = out - dist;
do {
*out++ = *from++;
} while (--len);
continue;
}
#else
SET_BAD("invalid distance too far back");
break;
#endif
}
from = window;
if (wnext == 0) { /* very common case */
from += wsize - op;
} else if (wnext >= op) { /* contiguous in window */
from += wnext - op;
} else { /* wrap around window */
op -= wnext;
from += wsize - op;
if (op < len) { /* some from end of window */
len -= op;
out = CHUNKCOPY_SAFE(out, from, op, safe);
from = window; /* more from start of window */
op = wnext;
/* This (rare) case can create a situation where
the first chunkcopy below must be checked.
*/
}
}
if (op < len) { /* still need some from output */
len -= op;
if (!extra_safe) {
out = CHUNKCOPY_SAFE(out, from, op, safe);
out = CHUNKUNROLL(out, &dist, &len);
out = CHUNKCOPY_SAFE(out, out - dist, len, safe);
} else {
out = chunkcopy_safe(out, from, op, safe);
out = chunkcopy_safe(out, out - dist, len, safe);
}
} else {
#ifndef HAVE_MASKED_READWRITE
if (extra_safe)
out = chunkcopy_safe(out, from, len, safe);
else
#endif
out = CHUNKCOPY_SAFE(out, from, len, safe);
}
#ifndef HAVE_MASKED_READWRITE
} else if (extra_safe) {
/* Whole reference is in range of current output. */
out = chunkcopy_safe(out, out - dist, len, safe);
#endif
} else {
/* Whole reference is in range of current output. No range checks are
necessary because we start with room for at least 258 bytes of output,
so unroll and roundoff operations can write beyond `out+len` so long
as they stay within 258 bytes of `out`.
*/
if (dist >= len || dist >= state->chunksize)
out = CHUNKCOPY(out, out - dist, len);
else
out = CHUNKMEMSET(out, out - dist, len);
}
} else if ((op & 64) == 0) { /* 2nd level distance code */
here = dcode + here->val + BITS(op);
goto dodist;
} else {
SET_BAD("invalid distance code");
break;
}
} else if ((op & 64) == 0) { /* 2nd level length code */
here = lcode + here->val + BITS(op);
goto dolen;
} else if (op & 32) { /* end-of-block */
Tracevv((stderr, "inflate: end of block\n"));
state->mode = TYPE;
break;
} else {
SET_BAD("invalid literal/length code");
break;
}
} while (in < last && out < end);
/* return unused bytes (on entry, bits < 8, so in won't go too far back) */
len = bits >> 3;
in -= len;
bits -= len << 3;
hold &= (UINT64_C(1) << bits) - 1;
/* update state and return */
strm->next_in = in;
strm->next_out = out;
strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in)
: (INFLATE_FAST_MIN_HAVE - 1) - (in - last));
strm->avail_out = (unsigned)(out < end ? (INFLATE_FAST_MIN_LEFT - 1) + (end - out)
: (INFLATE_FAST_MIN_LEFT - 1) - (out - end));
Assert(bits <= 32, "Remaining bits greater than 32");
state->hold = (uint32_t)hold;
state->bits = bits;
return;
}
/*
inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
- Using bit fields for code structure
- Different op definition to avoid & for extra bits (do & for table bits)
- Three separate decoding do-loops for direct, window, and wnext == 0
- Special case for distance > 1 copies to do overlapped load and store copy
- Explicit branch predictions (based on measured branch probabilities)
- Deferring match copy and interspersed it with decoding subsequent codes
- Swapping literal/length else
- Swapping window/direct else
- Larger unrolled copy loops (three is about right)
- Moving len -= 3 statement into middle of loop
*/
+94
View File
@@ -0,0 +1,94 @@
/* inffixed_tbl.h -- table for decoding fixed codes
* Generated automatically by makefixed().
*/
/* WARNING: this file should *not* be used by applications.
* It is part of the implementation of this library and is
* subject to change. Applications should only use zlib.h.
*/
static const code lenfix[512] = {
{96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
{0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
{0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
{0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
{0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
{21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
{0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
{0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
{18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
{0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
{0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
{0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
{20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
{0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
{0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
{0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
{16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
{0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
{0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
{0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
{0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
{0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
{0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
{0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
{17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
{0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
{0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
{0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
{19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
{0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
{0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
{0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
{16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
{0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
{0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
{0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
{0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
{20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
{0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
{0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
{17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
{0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
{0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
{0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
{20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
{0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
{0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
{0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
{16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
{0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
{0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
{0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
{0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
{0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
{0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
{0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
{16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
{0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
{0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
{0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
{19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
{0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
{0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
{0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
{16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
{0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
{0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
{0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
{0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
{64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
{0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
{0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
{18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
{0,9,255}
};
static const code distfix[32] = {
{16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
{21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
{18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
{19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
{16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
{22,5,193},{64,5,0}
};
+1462
View File
File diff suppressed because it is too large. Load diff
+172
View File
@@ -0,0 +1,172 @@
/* inflate.h -- internal inflate state definition
* Copyright (C) 1995-2019 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* WARNING: this file should *not* be used by applications. It is
part of the implementation of the compression library and is
subject to change. Applications should only use zlib.h.
*/
#ifndef INFLATE_H_
#define INFLATE_H_
#include "crc32.h"
#ifdef S390_DFLTCC_INFLATE
# include "arch/s390/dfltcc_common.h"
# define HAVE_ARCH_INFLATE_STATE
#endif
/* define NO_GZIP when compiling if you want to disable gzip header and trailer decoding by inflate().
NO_GZIP would be used to avoid linking in the crc code when it is not needed.
For shared libraries, gzip decoding should be left enabled. */
#ifndef NO_GZIP
# define GUNZIP
#endif
/* Possible inflate modes between inflate() calls */
typedef enum {
HEAD = 16180, /* i: waiting for magic header */
FLAGS, /* i: waiting for method and flags (gzip) */
TIME, /* i: waiting for modification time (gzip) */
OS, /* i: waiting for extra flags and operating system (gzip) */
EXLEN, /* i: waiting for extra length (gzip) */
EXTRA, /* i: waiting for extra bytes (gzip) */
NAME, /* i: waiting for end of file name (gzip) */
COMMENT, /* i: waiting for end of comment (gzip) */
HCRC, /* i: waiting for header crc (gzip) */
DICTID, /* i: waiting for dictionary check value */
DICT, /* waiting for inflateSetDictionary() call */
TYPE, /* i: waiting for type bits, including last-flag bit */
TYPEDO, /* i: same, but skip check to exit inflate on new block */
STORED, /* i: waiting for stored size (length and complement) */
COPY_, /* i/o: same as COPY below, but only first time in */
COPY, /* i/o: waiting for input or output to copy stored block */
TABLE, /* i: waiting for dynamic block table lengths */
LENLENS, /* i: waiting for code length code lengths */
CODELENS, /* i: waiting for length/lit and distance code lengths */
LEN_, /* i: same as LEN below, but only first time in */
LEN, /* i: waiting for length/lit/eob code */
LENEXT, /* i: waiting for length extra bits */
DIST, /* i: waiting for distance code */
DISTEXT, /* i: waiting for distance extra bits */
MATCH, /* o: waiting for output space to copy string */
LIT, /* o: waiting for output space to write literal */
CHECK, /* i: waiting for 32-bit check value */
LENGTH, /* i: waiting for 32-bit length (gzip) */
DONE, /* finished check, done -- remain here until reset */
BAD, /* got a data error -- remain here until reset */
SYNC /* looking for synchronization bytes to restart inflate() */
} inflate_mode;
/*
State transitions between above modes -
(most modes can go to BAD on error -- not shown for clarity)
Process header:
HEAD -> (gzip) or (zlib) or (raw)
(gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
HCRC -> TYPE
(zlib) -> DICTID or TYPE
DICTID -> DICT -> TYPE
(raw) -> TYPEDO
Read deflate blocks:
TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
STORED -> COPY_ -> COPY -> TYPE
TABLE -> LENLENS -> CODELENS -> LEN_
LEN_ -> LEN
Read deflate codes in fixed or dynamic block:
LEN -> LENEXT or LIT or TYPE
LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
LIT -> LEN
Process trailer:
CHECK -> LENGTH -> DONE
*/
typedef struct inflate_state inflate_state;
/* Struct for memory allocation handling */
typedef struct inflate_allocs_s {
char *buf_start;
free_func zfree;
inflate_state *state;
unsigned char *window;
} inflate_allocs;
/* State maintained between inflate() calls -- approximately 7K bytes, not
including the allocated sliding window, which is up to 32K bytes. */
struct ALIGNED_(64) inflate_state {
PREFIX3(stream) *strm; /* pointer back to this zlib stream */
inflate_mode mode; /* current inflate mode */
int last; /* true if processing last block */
int wrap; /* bit 0 true for zlib, bit 1 true for gzip,
bit 2 true to validate check value */
int havedict; /* true if dictionary provided */
int flags; /* gzip header method and flags, 0 if zlib, or
-1 if raw or no header yet */
unsigned was; /* initial length of match, for inflateMark */
unsigned long check; /* protected copy of check value */
unsigned long total; /* protected copy of output count */
PREFIX(gz_headerp) head; /* where to save gzip header information */
int back; /* bits back of last unprocessed length/lit */
/* sliding window */
unsigned wbits; /* log base 2 of requested window size */
uint32_t wsize; /* window size or zero if not using window */
uint32_t wbufsize; /* real size of the allocated window buffer, including padding */
uint32_t whave; /* valid bytes in the window */
uint32_t wnext; /* window write index */
unsigned char *window; /* allocated sliding window, if needed */
uint32_t chunksize; /* size of memory copying chunk */
/* bit accumulator */
uint64_t hold; /* input bit accumulator */
unsigned bits; /* number of bits in "in" */
/* fixed and dynamic code tables */
unsigned lenbits; /* index bits for lencode */
code const *lencode; /* starting table for length/literal codes */
code const *distcode; /* starting table for distance codes */
unsigned distbits; /* index bits for distcode */
/* for string and stored block copying */
uint32_t length; /* literal or length of data to copy */
unsigned offset; /* distance back to copy string from */
/* for table and code decoding */
unsigned extra; /* extra bits needed */
/* dynamic table building */
unsigned ncode; /* number of code length code lengths */
unsigned nlen; /* number of length code lengths */
unsigned ndist; /* number of distance code lengths */
uint32_t have; /* number of code lengths in lens[] */
code *next; /* next available space in codes[] */
#if defined(_M_IX86) || defined(_M_ARM)
uint32_t padding[1];
#endif
struct crc32_fold_s ALIGNED_(16) crc_fold;
uint16_t lens[320]; /* temporary storage for code lengths */
uint16_t work[288]; /* work area for code table building */
code codes[ENOUGH]; /* space for code tables */
inflate_allocs *alloc_bufs; /* struct for handling memory allocations */
#ifdef INFLATE_STRICT
unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
#endif
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
int sane; /* if false, allow invalid distance too far */
#endif
#ifdef HAVE_ARCH_INFLATE_STATE
arch_inflate_state arch; /* architecture-specific extensions */
#endif
#if defined(_M_IX86) || defined(_M_ARM)
int padding2[8];
#endif
};
void Z_INTERNAL PREFIX(fixedtables)(struct inflate_state *state);
Z_INTERNAL inflate_allocs* alloc_inflate(PREFIX3(stream) *strm);
Z_INTERNAL void free_inflate(PREFIX3(stream) *strm);
#endif /* INFLATE_H_ */
+215
View File
@@ -0,0 +1,215 @@
/* inflate_p.h -- Private inline functions and macros shared with more than one deflate method
*
*/
#ifndef INFLATE_P_H
#define INFLATE_P_H
#include <stdlib.h>
#include "zmemory.h"
/* Architecture-specific hooks. */
#ifdef S390_DFLTCC_INFLATE
# include "arch/s390/dfltcc_inflate.h"
/* DFLTCC instructions require window to be page-aligned */
# define PAD_WINDOW PAD_4096
# define WINDOW_PAD_SIZE 4096
# define HINT_ALIGNED_WINDOW HINT_ALIGNED_4096
#else
# define PAD_WINDOW PAD_64
# define WINDOW_PAD_SIZE 64
# define HINT_ALIGNED_WINDOW HINT_ALIGNED_64
/* Adjust the window size for the arch-specific inflate code. */
# define INFLATE_ADJUST_WINDOW_SIZE(n) (n)
/* Invoked at the end of inflateResetKeep(). Useful for initializing arch-specific extension blocks. */
# define INFLATE_RESET_KEEP_HOOK(strm) do {} while (0)
/* Invoked at the beginning of inflatePrime(). Useful for updating arch-specific buffers. */
# define INFLATE_PRIME_HOOK(strm, bits, value) do {} while (0)
/* Invoked at the beginning of each block. Useful for plugging arch-specific inflation code. */
# define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0)
/* Returns whether zlib-ng should compute a checksum. Set to 0 if arch-specific inflation code already does that. */
# define INFLATE_NEED_CHECKSUM(strm) 1
/* Returns whether zlib-ng should update a window. Set to 0 if arch-specific inflation code already does that. */
# define INFLATE_NEED_UPDATEWINDOW(strm) 1
/* Invoked at the beginning of inflateMark(). Useful for updating arch-specific pointers and offsets. */
# define INFLATE_MARK_HOOK(strm) do {} while (0)
/* Invoked at the beginning of inflateSyncPoint(). Useful for performing arch-specific state checks. */
# define INFLATE_SYNC_POINT_HOOK(strm) do {} while (0)
/* Invoked at the beginning of inflateSetDictionary(). Useful for checking arch-specific window data. */
# define INFLATE_SET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
/* Invoked at the beginning of inflateGetDictionary(). Useful for adjusting arch-specific window data. */
# define INFLATE_GET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
#endif
/*
* Macros shared by inflate() and inflateBack()
*/
/* check function to use adler32() for zlib or crc32() for gzip */
#ifdef GUNZIP
# define UPDATE(check, buf, len) \
(state->flags ? PREFIX(crc32)(check, buf, len) : FUNCTABLE_CALL(adler32)(check, buf, len))
#else
# define UPDATE(check, buf, len) FUNCTABLE_CALL(adler32)(check, buf, len)
#endif
/* check macros for header crc */
#ifdef GUNZIP
# define CRC2(check, word) \
do { \
hbuf[0] = (unsigned char)(word); \
hbuf[1] = (unsigned char)((word) >> 8); \
check = PREFIX(crc32)(check, hbuf, 2); \
} while (0)
# define CRC4(check, word) \
do { \
hbuf[0] = (unsigned char)(word); \
hbuf[1] = (unsigned char)((word) >> 8); \
hbuf[2] = (unsigned char)((word) >> 16); \
hbuf[3] = (unsigned char)((word) >> 24); \
check = PREFIX(crc32)(check, hbuf, 4); \
} while (0)
#endif
/* Load registers with state in inflate() for speed */
#define LOAD() \
do { \
put = strm->next_out; \
left = strm->avail_out; \
next = strm->next_in; \
have = strm->avail_in; \
hold = state->hold; \
bits = state->bits; \
} while (0)
/* Restore state from registers in inflate() */
#define RESTORE() \
do { \
strm->next_out = put; \
strm->avail_out = left; \
strm->next_in = (z_const unsigned char *)next; \
strm->avail_in = have; \
state->hold = hold; \
state->bits = bits; \
} while (0)
/* Clear the input bit accumulator */
#define INITBITS() \
do { \
hold = 0; \
bits = 0; \
} while (0)
/* Ensure that there is at least n bits in the bit accumulator. If there is
not enough available input to do that, then return from inflate()/inflateBack(). */
#define NEEDBITS(n) \
do { \
while (bits < (unsigned)(n)) \
PULLBYTE(); \
} while (0)
/* Return the low n bits of the bit accumulator (n < 16) */
#define BITS(n) \
(hold & ((1U << (unsigned)(n)) - 1))
/* Remove n bits from the bit accumulator */
#define DROPBITS(n) \
do { \
hold >>= (n); \
bits -= (unsigned)(n); \
} while (0)
/* Remove zero to seven bits as needed to go to a byte boundary */
#define BYTEBITS() \
do { \
hold >>= bits & 7; \
bits -= bits & 7; \
} while (0)
/* Set mode=BAD and prepare error message */
#define SET_BAD(errmsg) \
do { \
state->mode = BAD; \
strm->msg = (char *)errmsg; \
} while (0)
#define INFLATE_FAST_MIN_HAVE 15
#define INFLATE_FAST_MIN_LEFT 260
/* Load 64 bits from IN and place the bytes at offset BITS in the result. */
static inline uint64_t load_64_bits(const unsigned char *in, unsigned bits) {
uint64_t chunk = zng_memread_8(in);
#if BYTE_ORDER == LITTLE_ENDIAN
return chunk << bits;
#else
return ZSWAP64(chunk) << bits;
#endif
}
/* Behave like chunkcopy, but avoid writing beyond of legal output. */
static inline uint8_t* chunkcopy_safe(uint8_t *out, uint8_t *from, uint64_t len, uint8_t *safe) {
uint64_t safelen = safe - out;
len = MIN(len, safelen);
int32_t olap_src = from >= out && from < out + len;
int32_t olap_dst = out >= from && out < from + len;
uint64_t tocopy;
/* For all cases without overlap, memcpy is ideal */
if (!(olap_src || olap_dst)) {
memcpy(out, from, (size_t)len);
return out + len;
}
/* Complete overlap: Source == destination */
if (out == from) {
return out + len;
}
/* We are emulating a self-modifying copy loop here. To do this in a way that doesn't produce undefined behavior,
* we have to get a bit clever. First if the overlap is such that src falls between dst and dst+len, we can do the
* initial bulk memcpy of the nonoverlapping region. Then, we can leverage the size of this to determine the safest
* atomic memcpy size we can pick such that we have non-overlapping regions. This effectively becomes a safe look
* behind or lookahead distance. */
uint64_t non_olap_size = llabs(from - out); // llabs vs labs for compatibility with windows
/* So this doesn't give use a worst case scenario of function calls in a loop,
* we want to instead break this down into copy blocks of fixed lengths
*
* TODO: The memcpy calls aren't inlined on architectures with strict memory alignment
*/
while (len) {
tocopy = MIN(non_olap_size, len);
len -= tocopy;
while (tocopy >= 16) {
memcpy(out, from, 16);
out += 16;
from += 16;
tocopy -= 16;
}
if (tocopy >= 8) {
memcpy(out, from, 8);
out += 8;
from += 8;
tocopy -= 8;
}
if (tocopy >= 4) {
memcpy(out, from, 4);
out += 4;
from += 4;
tocopy -= 4;
}
while (tocopy--) {
*out++ = *from++;
}
}
return out;
}
#endif
+295
View File
@@ -0,0 +1,295 @@
/* inftrees.c -- generate Huffman trees for efficient decoding
* Copyright (C) 1995-2024 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "zutil.h"
#include "inftrees.h"
const char PREFIX(inflate_copyright)[] = " inflate 1.3.1 Copyright 1995-2024 Mark Adler ";
/*
If you use the zlib library in a product, an acknowledgment is welcome
in the documentation of your product. If for some reason you cannot
include such an acknowledgment, I would appreciate that you keep this
copyright string in the executable of your product.
*/
/*
Build a set of tables to decode the provided canonical Huffman code.
The code lengths are lens[0..codes-1]. The result starts at *table,
whose indices are 0..2^bits-1. work is a writable array of at least
lens shorts, which is used as a work area. type is the type of code
to be generated, CODES, LENS, or DISTS. On return, zero is success,
-1 is an invalid code, and +1 means that ENOUGH isn't enough. table
on return points to the next available entry's address. bits is the
requested root table index bits, and on return it is the actual root
table index bits. It will differ if the request is greater than the
longest code or if it is less than the shortest code.
*/
int Z_INTERNAL zng_inflate_table(codetype type, uint16_t *lens, unsigned codes,
code * *table, unsigned *bits, uint16_t *work) {
unsigned len; /* a code's length in bits */
unsigned sym; /* index of code symbols */
unsigned min, max; /* minimum and maximum code lengths */
unsigned root; /* number of index bits for root table */
unsigned curr; /* number of index bits for current table */
unsigned drop; /* code bits to drop for sub-table */
int left; /* number of prefix codes available */
unsigned used; /* code entries in table used */
unsigned huff; /* Huffman code */
unsigned incr; /* for incrementing code, index */
unsigned fill; /* index for replicating entries */
unsigned low; /* low bits for current root entry */
unsigned mask; /* mask for low root bits */
code here; /* table entry for duplication */
code *next; /* next available space in table */
const uint16_t *base; /* base value table to use */
const uint16_t *extra; /* extra bits table to use */
unsigned match; /* use base and extra for symbol >= match */
uint16_t count[MAX_BITS+1]; /* number of codes of each length */
uint16_t offs[MAX_BITS+1]; /* offsets in table for each length */
static const uint16_t lbase[31] = { /* Length codes 257..285 base */
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
static const uint16_t lext[31] = { /* Length codes 257..285 extra */
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 203, 77};
static const uint16_t dbase[32] = { /* Distance codes 0..29 base */
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
8193, 12289, 16385, 24577, 0, 0};
static const uint16_t dext[32] = { /* Distance codes 0..29 extra */
16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
28, 28, 29, 29, 64, 64};
/*
Process a set of code lengths to create a canonical Huffman code. The
code lengths are lens[0..codes-1]. Each length corresponds to the
symbols 0..codes-1. The Huffman code is generated by first sorting the
symbols by length from short to long, and retaining the symbol order
for codes with equal lengths. Then the code starts with all zero bits
for the first code of the shortest length, and the codes are integer
increments for the same length, and zeros are appended as the length
increases. For the deflate format, these bits are stored backwards
from their more natural integer increment ordering, and so when the
decoding tables are built in the large loop below, the integer codes
are incremented backwards.
This routine assumes, but does not check, that all of the entries in
lens[] are in the range 0..MAXBITS. The caller must assure this.
1..MAXBITS is interpreted as that code length. zero means that that
symbol does not occur in this code.
The codes are sorted by computing a count of codes for each length,
creating from that a table of starting indices for each length in the
sorted table, and then entering the symbols in order in the sorted
table. The sorted table is work[], with that space being provided by
the caller.
The length counts are used for other purposes as well, i.e. finding
the minimum and maximum length codes, determining if there are any
codes at all, checking for a valid set of lengths, and looking ahead
at length counts to determine sub-table sizes when building the
decoding tables.
*/
/* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
for (len = 0; len <= MAX_BITS; len++)
count[len] = 0;
for (sym = 0; sym < codes; sym++)
count[lens[sym]]++;
/* bound code lengths, force root to be within code lengths */
root = *bits;
for (max = MAX_BITS; max >= 1; max--)
if (count[max] != 0) break;
root = MIN(root, max);
if (UNLIKELY(max == 0)) { /* no symbols to code at all */
here.op = (unsigned char)64; /* invalid code marker */
here.bits = (unsigned char)1;
here.val = (uint16_t)0;
*(*table)++ = here; /* make a table to force an error */
*(*table)++ = here;
*bits = 1;
return 0; /* no symbols, but wait for decoding to report error */
}
for (min = 1; min < max; min++)
if (count[min] != 0) break;
root = MAX(root, min);
/* check for an over-subscribed or incomplete set of lengths */
left = 1;
for (len = 1; len <= MAX_BITS; len++) {
left <<= 1;
left -= count[len];
if (left < 0) return -1; /* over-subscribed */
}
if (left > 0 && (type == CODES || max != 1))
return -1; /* incomplete set */
/* generate offsets into symbol table for each length for sorting */
offs[1] = 0;
for (len = 1; len < MAX_BITS; len++)
offs[len + 1] = offs[len] + count[len];
/* sort symbols by length, by symbol order within each length */
for (sym = 0; sym < codes; sym++)
if (lens[sym] != 0) work[offs[lens[sym]]++] = (uint16_t)sym;
/*
Create and fill in decoding tables. In this loop, the table being
filled is at next and has curr index bits. The code being used is huff
with length len. That code is converted to an index by dropping drop
bits off of the bottom. For codes where len is less than drop + curr,
those top drop + curr - len bits are incremented through all values to
fill the table with replicated entries.
root is the number of index bits for the root table. When len exceeds
root, sub-tables are created pointed to by the root entry with an index
of the low root bits of huff. This is saved in low to check for when a
new sub-table should be started. drop is zero when the root table is
being filled, and drop is root when sub-tables are being filled.
When a new sub-table is needed, it is necessary to look ahead in the
code lengths to determine what size sub-table is needed. The length
counts are used for this, and so count[] is decremented as codes are
entered in the tables.
used keeps track of how many table entries have been allocated from the
provided *table space. It is checked for LENS and DIST tables against
the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
the initial root table size constants. See the comments in inftrees.h
for more information.
sym increments through all symbols, and the loop terminates when
all codes of length max, i.e. all codes, have been processed. This
routine permits incomplete codes, so another loop after this one fills
in the rest of the decoding tables with invalid code markers.
*/
/* set up for code type */
switch (type) {
case CODES:
base = extra = work; /* dummy value--not used */
match = 20;
break;
case LENS:
base = lbase;
extra = lext;
match = 257;
break;
default: /* DISTS */
base = dbase;
extra = dext;
match = 0;
}
/* initialize state for loop */
huff = 0; /* starting code */
sym = 0; /* starting code symbol */
len = min; /* starting code length */
next = *table; /* current table to fill in */
curr = root; /* current table index bits */
drop = 0; /* current bits to drop from code for index */
low = (unsigned)(-1); /* trigger new sub-table when len > root */
used = 1U << root; /* use root table entries */
mask = used - 1; /* mask for comparing low */
/* check available table space */
if ((type == LENS && used > ENOUGH_LENS) ||
(type == DISTS && used > ENOUGH_DISTS))
return 1;
/* process all codes and make table entries */
for (;;) {
/* create table entry */
here.bits = (unsigned char)(len - drop);
if (LIKELY(work[sym] >= match)) {
here.op = (unsigned char)(extra[work[sym] - match]);
here.val = base[work[sym] - match];
} else if (work[sym] + 1U < match) {
here.op = (unsigned char)0;
here.val = work[sym];
} else {
here.op = (unsigned char)(32 + 64); /* end of block */
here.val = 0;
}
/* replicate for those indices with low len bits equal to huff */
incr = 1U << (len - drop);
fill = 1U << curr;
min = fill; /* save offset to next table */
do {
fill -= incr;
next[(huff >> drop) + fill] = here;
} while (fill != 0);
/* backwards increment the len-bit code huff */
incr = 1U << (len - 1);
while (huff & incr)
incr >>= 1;
if (incr != 0) {
huff &= incr - 1;
huff += incr;
} else {
huff = 0;
}
/* go to next symbol, update count, len */
sym++;
if (--(count[len]) == 0) {
if (len == max)
break;
len = lens[work[sym]];
}
/* create new sub-table if needed */
if (len > root && (huff & mask) != low) {
/* if first time, transition to sub-tables */
if (drop == 0)
drop = root;
/* increment past last table */
next += min; /* here min is 1 << curr */
/* determine length of next table */
curr = len - drop;
left = (int)(1 << curr);
while (curr + drop < max) {
left -= count[curr + drop];
if (left <= 0)
break;
curr++;
left <<= 1;
}
/* check for enough space */
used += 1U << curr;
if ((type == LENS && used > ENOUGH_LENS) || (type == DISTS && used > ENOUGH_DISTS))
return 1;
/* point entry in root table to sub-table */
low = huff & mask;
(*table)[low].op = (unsigned char)curr;
(*table)[low].bits = (unsigned char)root;
(*table)[low].val = (uint16_t)(next - *table);
}
}
/* fill in remaining table entry if code is incomplete (guaranteed to have
at most one remaining entry, since if the code is incomplete, the
maximum code length that was allowed to get this far is one bit) */
if (UNLIKELY(huff != 0)) {
here.op = (unsigned char)64; /* invalid code marker */
here.bits = (unsigned char)(len - drop);
here.val = (uint16_t)0;
next[huff] = here;
}
/* set return parameters */
*table += used;
*bits = root;
return 0;
}
+66
View File
@@ -0,0 +1,66 @@
#ifndef INFTREES_H_
#define INFTREES_H_
/* inftrees.h -- header to use inftrees.c
* Copyright (C) 1995-2022 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* WARNING: this file should *not* be used by applications. It is
part of the implementation of the compression library and is
subject to change. Applications should only use zlib.h.
*/
/* Structure for decoding tables. Each entry provides either the
information needed to do the operation requested by the code that
indexed that table entry, or it provides a pointer to another
table that indexes more bits of the code. op indicates whether
the entry is a pointer to another table, a literal, a length or
distance, an end-of-block, or an invalid code. For a table
pointer, the low four bits of op is the number of index bits of
that table. For a length or distance, the low four bits of op
is the number of extra bits to get after the code. bits is
the number of bits in this code or part of the code to drop off
of the bit buffer. val is the actual byte to output in the case
of a literal, the base length or distance, or the offset from
the current table to the next table. Each entry is four bytes. */
typedef struct {
unsigned char op; /* operation, extra bits, table bits */
unsigned char bits; /* bits in this part of the code */
uint16_t val; /* offset in table or code value */
} code;
/* op values as set by inflate_table():
00000000 - literal
0000tttt - table link, tttt != 0 is the number of table index bits
0001eeee - length or distance, eeee is the number of extra bits
01100000 - end of block
01000000 - invalid code
*/
/* Maximum size of the dynamic table. The maximum number of code structures is
1924, which is the sum of 1332 for literal/length codes and 592 for distance
codes. These values were found by exhaustive searches using the program
examples/enough.c found in the zlib distributions. The arguments to that
program are the number of symbols, the initial root table size, and the
maximum bit length of a code. "enough 286 10 15" for literal/length codes
returns 1332, and "enough 30 9 15" for distance codes returns 592.
The initial root table size (10 or 9) is found in the fifth argument of the
inflate_table() calls in inflate.c and infback.c. If the root table size is
changed, then these maximum sizes would be need to be recalculated and
updated. */
#define ENOUGH_LENS 1332
#define ENOUGH_DISTS 592
#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
/* Type of code to build for inflate_table() */
typedef enum {
CODES,
LENS,
DISTS
} codetype;
int Z_INTERNAL zng_inflate_table (codetype type, uint16_t *lens, unsigned codes,
code * *table, unsigned *bits, uint16_t *work);
#endif /* INFTREES_H_ */
+21
View File
@@ -0,0 +1,21 @@
/* insert_string.c -- insert_string integer hash variant
*
* Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*
*/
#include "zbuild.h"
#include "deflate.h"
#define HASH_SLIDE 16
#define HASH_CALC(h, val) h = ((val * 2654435761U) >> HASH_SLIDE);
#define HASH_CALC_VAR h
#define HASH_CALC_VAR_INIT uint32_t h = 0
#define UPDATE_HASH update_hash
#define INSERT_STRING insert_string
#define QUICK_INSERT_STRING quick_insert_string
#include "insert_string_tpl.h"
+24
View File
@@ -0,0 +1,24 @@
/* insert_string_roll.c -- insert_string rolling hash variant
*
* Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*
*/
#include "zbuild.h"
#include "deflate.h"
#define HASH_SLIDE 5
#define HASH_CALC(h, val) h = ((h << HASH_SLIDE) ^ ((uint8_t)val))
#define HASH_CALC_VAR s->ins_h
#define HASH_CALC_VAR_INIT
#define HASH_CALC_READ val = strstart[0]
#define HASH_CALC_MASK (32768u - 1u)
#define HASH_CALC_OFFSET (STD_MIN_MATCH-1)
#define UPDATE_HASH update_hash_roll
#define INSERT_STRING insert_string_roll
#define QUICK_INSERT_STRING quick_insert_string_roll
#include "insert_string_tpl.h"
+106
View File
@@ -0,0 +1,106 @@
#ifndef INSERT_STRING_H_
#define INSERT_STRING_H_
/* insert_string_tpl.h -- Private insert_string functions shared with more than
* one insert string implementation
*
* Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
*
* Copyright (C) 2013 Intel Corporation. All rights reserved.
* Authors:
* Wajdi Feghali <wajdi.k.feghali@intel.com>
* Jim Guilford <james.guilford@intel.com>
* Vinodh Gopal <vinodh.gopal@intel.com>
* Erdinc Ozturk <erdinc.ozturk@intel.com>
* Jim Kukunas <james.t.kukunas@linux.intel.com>
*
* Portions are Copyright (C) 2016 12Sided Technology, LLC.
* Author:
* Phil Vachon <pvachon@12sidedtech.com>
*
* For conditions of distribution and use, see copyright notice in zlib.h
*
*/
#include "zmemory.h"
#ifndef HASH_CALC_OFFSET
# define HASH_CALC_OFFSET 0
#endif
#ifndef HASH_CALC_MASK
# define HASH_CALC_MASK HASH_MASK
#endif
#ifndef HASH_CALC_READ
# if BYTE_ORDER == LITTLE_ENDIAN
# define HASH_CALC_READ \
val = zng_memread_4(strstart);
# else
# define HASH_CALC_READ \
val = ZSWAP32(zng_memread_4(strstart));
# endif
#endif
/* ===========================================================================
* Update a hash value with the given input byte
* IN assertion: all calls to UPDATE_HASH are made with consecutive
* input characters, so that a running hash key can be computed from the
* previous key instead of complete recalculation each time.
*/
Z_INTERNAL uint32_t UPDATE_HASH(uint32_t h, uint32_t val) {
HASH_CALC(h, val);
return h & HASH_CALC_MASK;
}
/* ===========================================================================
* Quick insert string str in the dictionary and set match_head to the previous head
* of the hash chain (the most recent string with same hash key). Return
* the previous length of the hash chain.
*/
Z_INTERNAL Pos QUICK_INSERT_STRING(deflate_state *const s, uint32_t str) {
Pos head;
uint8_t *strstart = s->window + str + HASH_CALC_OFFSET;
uint32_t val, hm;
HASH_CALC_VAR_INIT;
HASH_CALC_READ;
HASH_CALC(HASH_CALC_VAR, val);
HASH_CALC_VAR &= HASH_CALC_MASK;
hm = HASH_CALC_VAR;
head = s->head[hm];
if (LIKELY(head != str)) {
s->prev[str & s->w_mask] = head;
s->head[hm] = (Pos)str;
}
return head;
}
/* ===========================================================================
* Insert string str in the dictionary and set match_head to the previous head
* of the hash chain (the most recent string with same hash key). Return
* the previous length of the hash chain.
* IN assertion: all calls to INSERT_STRING are made with consecutive
* input characters and the first STD_MIN_MATCH bytes of str are valid
* (except for the last STD_MIN_MATCH-1 bytes of the input file).
*/
Z_INTERNAL void INSERT_STRING(deflate_state *const s, uint32_t str, uint32_t count) {
uint8_t *strstart = s->window + str + HASH_CALC_OFFSET;
uint8_t *strend = strstart + count;
for (Pos idx = (Pos)str; strstart < strend; idx++, strstart++) {
uint32_t val, hm;
HASH_CALC_VAR_INIT;
HASH_CALC_READ;
HASH_CALC(HASH_CALC_VAR, val);
HASH_CALC_VAR &= HASH_CALC_MASK;
hm = HASH_CALC_VAR;
Pos head = s->head[hm];
if (LIKELY(head != idx)) {
s->prev[idx & s->w_mask] = head;
s->head[hm] = idx;
}
}
}
#endif
+250
View File
@@ -0,0 +1,250 @@
/* match_tpl.h -- find longest match template for compare256 variants
*
* Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*
* Portions copyright (C) 2014-2021 Konstantin Nosov
* Fast-zlib optimized longest_match
* https://github.com/gildor2/fast_zlib
*/
#ifndef MATCH_TPL_H
#define MATCH_TPL_H
#define EARLY_EXIT_TRIGGER_LEVEL 5
#endif
/* Set match_start to the longest match starting at the given string and
* return its length. Matches shorter or equal to prev_length are discarded,
* in which case the result is equal to prev_length and match_start is garbage.
*
* IN assertions: cur_match is the head of the hash chain for the current
* string (strstart) and its distance is <= MAX_DIST, and prev_length >=1
* OUT assertion: the match length is not greater than s->lookahead
*
* The LONGEST_MATCH_SLOW variant spends more time to attempt to find longer
* matches once a match has already been found.
*/
Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) {
unsigned int strstart = s->strstart;
const unsigned wmask = s->w_mask;
unsigned char *window = s->window;
unsigned char *scan = window + strstart;
Z_REGISTER unsigned char *mbase_start = window;
Z_REGISTER unsigned char *mbase_end;
const Pos *prev = s->prev;
Pos limit;
#ifdef LONGEST_MATCH_SLOW
Pos limit_base;
#else
int32_t early_exit;
#endif
uint32_t chain_length, nice_match, best_len, offset;
uint32_t lookahead = s->lookahead;
Pos match_offset = 0;
uint64_t scan_start;
uint64_t scan_end;
#define GOTO_NEXT_CHAIN \
if (--chain_length && (cur_match = prev[cur_match & wmask]) > limit) \
continue; \
return best_len;
/* The code is optimized for STD_MAX_MATCH-2 multiple of 16. */
Assert(STD_MAX_MATCH == 258, "Code too clever");
best_len = s->prev_length ? s->prev_length : STD_MIN_MATCH-1;
/* Calculate read offset which should only extend an extra byte
* to find the next best match length.
*/
offset = best_len-1;
if (best_len >= sizeof(uint32_t)) {
offset -= 2;
if (best_len >= sizeof(uint64_t))
offset -= 4;
}
scan_start = zng_memread_8(scan);
scan_end = zng_memread_8(scan+offset);
mbase_end = (mbase_start+offset);
/* Do not waste too much time if we already have a good match */
chain_length = s->max_chain_length;
if (best_len >= s->good_match)
chain_length >>= 2;
nice_match = (uint32_t)s->nice_match;
/* Stop when cur_match becomes <= limit. To simplify the code,
* we prevent matches with the string of window index 0
*/
limit = strstart > MAX_DIST(s) ? (Pos)(strstart - MAX_DIST(s)) : 0;
#ifdef LONGEST_MATCH_SLOW
limit_base = limit;
if (best_len >= STD_MIN_MATCH) {
/* We're continuing search (lazy evaluation). */
uint32_t i, hash;
Pos pos;
/* Find a most distant chain starting from scan with index=1 (index=0 corresponds
* to cur_match). We cannot use s->prev[strstart+1,...] immediately, because
* these strings are not yet inserted into the hash table.
*/
hash = s->update_hash(0, scan[1]);
hash = s->update_hash(hash, scan[2]);
for (i = 3; i <= best_len; i++) {
hash = s->update_hash(hash, scan[i]);
/* If we're starting with best_len >= 3, we can use offset search. */
pos = s->head[hash];
if (pos < cur_match) {
match_offset = (Pos)(i - 2);
cur_match = pos;
}
}
/* Update offset-dependent variables */
limit = limit_base+match_offset;
if (cur_match <= limit)
goto break_matching;
mbase_start -= match_offset;
mbase_end -= match_offset;
}
#else
early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL;
#endif
Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
for (;;) {
if (cur_match >= strstart)
break;
/* Skip to next match if the match length cannot increase or if the match length is
* less than 2. Note that the checks below for insufficient lookahead only occur
* occasionally for performance reasons.
* Therefore uninitialized memory will be accessed and conditional jumps will be made
* that depend on those values. However the length of the match is limited to the
* lookahead, so the output of deflate is not affected by the uninitialized values.
*/
if (best_len < sizeof(uint32_t)) {
for (;;) {
if (zng_memcmp_2(mbase_end+cur_match, &scan_end) == 0 &&
zng_memcmp_2(mbase_start+cur_match, &scan_start) == 0)
break;
GOTO_NEXT_CHAIN;
}
} else if (best_len >= sizeof(uint64_t)) {
for (;;) {
if (zng_memcmp_8(mbase_end+cur_match, &scan_end) == 0 &&
zng_memcmp_8(mbase_start+cur_match, &scan_start) == 0)
break;
GOTO_NEXT_CHAIN;
}
} else {
for (;;) {
if (zng_memcmp_4(mbase_end+cur_match, &scan_end) == 0 &&
zng_memcmp_4(mbase_start+cur_match, &scan_start) == 0)
break;
GOTO_NEXT_CHAIN;
}
}
uint32_t len = COMPARE256(scan+2, mbase_start+cur_match+2) + 2;
Assert(scan+len <= window+(unsigned)(s->window_size-1), "wild scan");
if (len > best_len) {
uint32_t match_start = cur_match - match_offset;
s->match_start = match_start;
/* Do not look for matches beyond the end of the input. */
if (len > lookahead)
return lookahead;
best_len = len;
if (best_len >= nice_match)
return best_len;
offset = best_len-1;
if (best_len >= sizeof(uint32_t)) {
offset -= 2;
if (best_len >= sizeof(uint64_t))
offset -= 4;
}
scan_end = zng_memread_8(scan+offset);
#ifdef LONGEST_MATCH_SLOW
/* Look for a better string offset */
if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) {
Pos pos, next_pos;
uint32_t i, hash;
unsigned char *scan_endstr;
/* Go back to offset 0 */
cur_match -= match_offset;
match_offset = 0;
next_pos = cur_match;
for (i = 0; i <= len - STD_MIN_MATCH; i++) {
pos = prev[(cur_match + i) & wmask];
if (pos < next_pos) {
/* Hash chain is more distant, use it */
if (pos <= limit_base + i)
goto break_matching;
next_pos = pos;
match_offset = (Pos)i;
}
}
/* Switch cur_match to next_pos chain */
cur_match = next_pos;
/* Try hash head at len-(STD_MIN_MATCH-1) position to see if we could get
* a better cur_match at the end of string. Using (STD_MIN_MATCH-1) lets
* us include one more byte into hash - the byte which will be checked
* in main loop now, and which allows to grow match by 1.
*/
scan_endstr = scan + len - (STD_MIN_MATCH+1);
hash = s->update_hash(0, scan_endstr[0]);
hash = s->update_hash(hash, scan_endstr[1]);
hash = s->update_hash(hash, scan_endstr[2]);
pos = s->head[hash];
if (pos < cur_match) {
match_offset = (Pos)(len - (STD_MIN_MATCH+1));
if (pos <= limit_base + match_offset)
goto break_matching;
cur_match = pos;
}
/* Update offset-dependent variables */
limit = limit_base+match_offset;
mbase_start = window-match_offset;
mbase_end = (mbase_start+offset);
continue;
}
#endif
mbase_end = (mbase_start+offset);
}
#ifndef LONGEST_MATCH_SLOW
else if (UNLIKELY(early_exit)) {
/* The probability of finding a match later if we here is pretty low, so for
* performance it's best to outright stop here for the lower compression levels
*/
break;
}
#endif
GOTO_NEXT_CHAIN;
}
return best_len;
#ifdef LONGEST_MATCH_SLOW
break_matching:
if (best_len < s->lookahead)
return best_len;
return s->lookahead;
#endif
}
#undef LONGEST_MATCH_SLOW
#undef LONGEST_MATCH
+818
View File
@@ -0,0 +1,818 @@
/* trees.c -- output deflated data using Huffman coding
* Copyright (C) 1995-2024 Jean-loup Gailly
* detect_data_type() function provided freely by Cosmin Truta, 2006
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/*
* ALGORITHM
*
* The "deflation" process uses several Huffman trees. The more
* common source values are represented by shorter bit sequences.
*
* Each code tree is stored in a compressed form which is itself
* a Huffman encoding of the lengths of all the code strings (in
* ascending order by source values). The actual code strings are
* reconstructed from the lengths in the inflate process, as described
* in the deflate specification.
*
* REFERENCES
*
* Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
* Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
*
* Storer, James A.
* Data Compression: Methods and Theory, pp. 49-50.
* Computer Science Press, 1988. ISBN 0-7167-8156-5.
*
* Sedgewick, R.
* Algorithms, p290.
* Addison-Wesley, 1983. ISBN 0-201-06672-6.
*/
#include "zbuild.h"
#include "deflate.h"
#include "trees.h"
#include "trees_emit.h"
#include "trees_tbl.h"
/* The lengths of the bit length codes are sent in order of decreasing
* probability, to avoid transmitting the lengths for unused bit length codes.
*/
/* ===========================================================================
* Local data. These are initialized only once.
*/
struct static_tree_desc_s {
const ct_data *static_tree; /* static tree or NULL */
const int *extra_bits; /* extra bits for each code or NULL */
int extra_base; /* base index for extra_bits */
int elems; /* max number of elements in the tree */
unsigned int max_length; /* max bit length for the codes */
};
static const static_tree_desc static_l_desc =
{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
static const static_tree_desc static_d_desc =
{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
static const static_tree_desc static_bl_desc =
{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
/* ===========================================================================
* Local (static) routines in this file.
*/
static void init_block (deflate_state *s);
static void pqdownheap (deflate_state *s, ct_data *tree, int k);
static void gen_bitlen (deflate_state *s, tree_desc *desc);
static void build_tree (deflate_state *s, tree_desc *desc);
static void scan_tree (deflate_state *s, ct_data *tree, int max_code);
static void send_tree (deflate_state *s, ct_data *tree, int max_code);
static int build_bl_tree (deflate_state *s);
static void send_all_trees (deflate_state *s, int lcodes, int dcodes, int blcodes);
static void compress_block (deflate_state *s, const ct_data *ltree, const ct_data *dtree);
static int detect_data_type (deflate_state *s);
/* ===========================================================================
* Initialize the tree data structures for a new zlib stream.
*/
void Z_INTERNAL zng_tr_init(deflate_state *s) {
s->l_desc.dyn_tree = s->dyn_ltree;
s->l_desc.stat_desc = &static_l_desc;
s->d_desc.dyn_tree = s->dyn_dtree;
s->d_desc.stat_desc = &static_d_desc;
s->bl_desc.dyn_tree = s->bl_tree;
s->bl_desc.stat_desc = &static_bl_desc;
s->bi_buf = 0;
s->bi_valid = 0;
#ifdef ZLIB_DEBUG
s->compressed_len = 0L;
s->bits_sent = 0L;
#endif
/* Initialize the first block of the first file: */
init_block(s);
}
/* ===========================================================================
* Initialize a new block.
*/
static void init_block(deflate_state *s) {
int n; /* iterates over tree elements */
/* Initialize the trees. */
for (n = 0; n < L_CODES; n++)
s->dyn_ltree[n].Freq = 0;
for (n = 0; n < D_CODES; n++)
s->dyn_dtree[n].Freq = 0;
for (n = 0; n < BL_CODES; n++)
s->bl_tree[n].Freq = 0;
s->dyn_ltree[END_BLOCK].Freq = 1;
s->opt_len = s->static_len = 0L;
s->sym_next = s->matches = 0;
}
#define SMALLEST 1
/* Index within the heap array of least frequent node in the Huffman tree */
/* ===========================================================================
* Remove the smallest element from the heap and recreate the heap with
* one less element. Updates heap and heap_len.
*/
#define pqremove(s, tree, top) \
{\
top = s->heap[SMALLEST]; \
s->heap[SMALLEST] = s->heap[s->heap_len--]; \
pqdownheap(s, tree, SMALLEST); \
}
/* ===========================================================================
* Compares to subtrees, using the tree depth as tie breaker when
* the subtrees have equal frequency. This minimizes the worst case length.
*/
#define smaller(tree, n, m, depth) \
(tree[n].Freq < tree[m].Freq || \
(tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
/* ===========================================================================
* Restore the heap property by moving down the tree starting at node k,
* exchanging a node with the smallest of its two sons if necessary, stopping
* when the heap property is re-established (each father smaller than its
* two sons).
*/
static void pqdownheap(deflate_state *s, ct_data *tree, int k) {
/* tree: the tree to restore */
/* k: node to move down */
int v = s->heap[k];
int j = k << 1; /* left son of k */
while (j <= s->heap_len) {
/* Set j to the smallest of the two sons: */
if (j < s->heap_len && smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
j++;
}
/* Exit if v is smaller than both sons */
if (smaller(tree, v, s->heap[j], s->depth))
break;
/* Exchange v with the smallest son */
s->heap[k] = s->heap[j];
k = j;
/* And continue down the tree, setting j to the left son of k */
j <<= 1;
}
s->heap[k] = v;
}
/* ===========================================================================
* Compute the optimal bit lengths for a tree and update the total bit length
* for the current block.
* IN assertion: the fields freq and dad are set, heap[heap_max] and
* above are the tree nodes sorted by increasing frequency.
* OUT assertions: the field len is set to the optimal bit length, the
* array bl_count contains the frequencies for each bit length.
* The length opt_len is updated; static_len is also updated if stree is
* not null.
*/
static void gen_bitlen(deflate_state *s, tree_desc *desc) {
/* desc: the tree descriptor */
ct_data *tree = desc->dyn_tree;
int max_code = desc->max_code;
const ct_data *stree = desc->stat_desc->static_tree;
const int *extra = desc->stat_desc->extra_bits;
int base = desc->stat_desc->extra_base;
unsigned int max_length = desc->stat_desc->max_length;
int h; /* heap index */
int n, m; /* iterate over the tree elements */
unsigned int bits; /* bit length */
int xbits; /* extra bits */
uint16_t f; /* frequency */
int overflow = 0; /* number of elements with bit length too large */
for (bits = 0; bits <= MAX_BITS; bits++)
s->bl_count[bits] = 0;
/* In a first pass, compute the optimal bit lengths (which may
* overflow in the case of the bit length tree).
*/
tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
for (h = s->heap_max + 1; h < HEAP_SIZE; h++) {
n = s->heap[h];
bits = tree[tree[n].Dad].Len + 1u;
if (bits > max_length){
bits = max_length;
overflow++;
}
tree[n].Len = (uint16_t)bits;
/* We overwrite tree[n].Dad which is no longer needed */
if (n > max_code) /* not a leaf node */
continue;
s->bl_count[bits]++;
xbits = 0;
if (n >= base)
xbits = extra[n-base];
f = tree[n].Freq;
s->opt_len += (unsigned long)f * (unsigned int)(bits + xbits);
if (stree)
s->static_len += (unsigned long)f * (unsigned int)(stree[n].Len + xbits);
}
if (overflow == 0)
return;
Tracev((stderr, "\nbit length overflow\n"));
/* This happens for example on obj2 and pic of the Calgary corpus */
/* Find the first bit length which could increase: */
do {
bits = max_length - 1;
while (s->bl_count[bits] == 0)
bits--;
s->bl_count[bits]--; /* move one leaf down the tree */
s->bl_count[bits+1] += 2u; /* move one overflow item as its brother */
s->bl_count[max_length]--;
/* The brother of the overflow item also moves one step up,
* but this does not affect bl_count[max_length]
*/
overflow -= 2;
} while (overflow > 0);
/* Now recompute all bit lengths, scanning in increasing frequency.
* h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
* lengths instead of fixing only the wrong ones. This idea is taken
* from 'ar' written by Haruhiko Okumura.)
*/
for (bits = max_length; bits != 0; bits--) {
n = s->bl_count[bits];
while (n != 0) {
m = s->heap[--h];
if (m > max_code)
continue;
if (tree[m].Len != bits) {
Tracev((stderr, "code %d bits %d->%u\n", m, tree[m].Len, bits));
s->opt_len += (unsigned long)(bits * tree[m].Freq);
s->opt_len -= (unsigned long)(tree[m].Len * tree[m].Freq);
tree[m].Len = (uint16_t)bits;
}
n--;
}
}
}
/* ===========================================================================
* Generate the codes for a given tree and bit counts (which need not be
* optimal).
* IN assertion: the array bl_count contains the bit length statistics for
* the given tree and the field len is set for all tree elements.
* OUT assertion: the field code is set for all tree elements of non
* zero code length.
*/
Z_INTERNAL void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) {
/* tree: the tree to decorate */
/* max_code: largest code with non zero frequency */
/* bl_count: number of codes at each bit length */
uint16_t next_code[MAX_BITS+1]; /* next code value for each bit length */
unsigned int code = 0; /* running code value */
int bits; /* bit index */
int n; /* code index */
/* The distribution counts are first used to generate the code values
* without bit reversal.
*/
for (bits = 1; bits <= MAX_BITS; bits++) {
code = (code + bl_count[bits-1]) << 1;
next_code[bits] = (uint16_t)code;
}
/* Check that the bit counts in bl_count are consistent. The last code
* must be all ones.
*/
Assert(code + bl_count[MAX_BITS]-1 == (1 << MAX_BITS)-1, "inconsistent bit counts");
Tracev((stderr, "\ngen_codes: max_code %d ", max_code));
for (n = 0; n <= max_code; n++) {
int len = tree[n].Len;
if (len == 0)
continue;
/* Now reverse the bits */
tree[n].Code = PREFIX(bi_reverse)(next_code[len]++, len);
Tracecv(tree != static_ltree, (stderr, "\nn %3d %c l %2d c %4x (%x) ",
n, (isgraph(n & 0xff) ? n : ' '), len, tree[n].Code, next_code[len]-1));
}
}
/* ===========================================================================
* Construct one Huffman tree and assigns the code bit strings and lengths.
* Update the total bit length for the current block.
* IN assertion: the field freq is set for all tree elements.
* OUT assertions: the fields len and code are set to the optimal bit length
* and corresponding code. The length opt_len is updated; static_len is
* also updated if stree is not null. The field max_code is set.
*/
static void build_tree(deflate_state *s, tree_desc *desc) {
/* desc: the tree descriptor */
ct_data *tree = desc->dyn_tree;
const ct_data *stree = desc->stat_desc->static_tree;
int elems = desc->stat_desc->elems;
int n, m; /* iterate over heap elements */
int max_code = -1; /* largest code with non zero frequency */
int node; /* new node being created */
/* Construct the initial heap, with least frequent element in
* heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
* heap[0] is not used.
*/
s->heap_len = 0;
s->heap_max = HEAP_SIZE;
for (n = 0; n < elems; n++) {
if (tree[n].Freq != 0) {
s->heap[++(s->heap_len)] = max_code = n;
s->depth[n] = 0;
} else {
tree[n].Len = 0;
}
}
/* The pkzip format requires that at least one distance code exists,
* and that at least one bit should be sent even if there is only one
* possible code. So to avoid special checks later on we force at least
* two codes of non zero frequency.
*/
while (s->heap_len < 2) {
node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
tree[node].Freq = 1;
s->depth[node] = 0;
s->opt_len--;
if (stree)
s->static_len -= stree[node].Len;
/* node is 0 or 1 so it does not have extra bits */
}
desc->max_code = max_code;
/* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
* establish sub-heaps of increasing lengths:
*/
for (n = s->heap_len/2; n >= 1; n--)
pqdownheap(s, tree, n);
/* Construct the Huffman tree by repeatedly combining the least two
* frequent nodes.
*/
node = elems; /* next internal node of the tree */
do {
pqremove(s, tree, n); /* n = node of least frequency */
m = s->heap[SMALLEST]; /* m = node of next least frequency */
s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
s->heap[--(s->heap_max)] = m;
/* Create a new node father of n and m */
tree[node].Freq = tree[n].Freq + tree[m].Freq;
s->depth[node] = (unsigned char)((s->depth[n] >= s->depth[m] ?
s->depth[n] : s->depth[m]) + 1);
tree[n].Dad = tree[m].Dad = (uint16_t)node;
#ifdef DUMP_BL_TREE
if (tree == s->bl_tree) {
fprintf(stderr, "\nnode %d(%d), sons %d(%d) %d(%d)",
node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
}
#endif
/* and insert the new node in the heap */
s->heap[SMALLEST] = node++;
pqdownheap(s, tree, SMALLEST);
} while (s->heap_len >= 2);
s->heap[--(s->heap_max)] = s->heap[SMALLEST];
/* At this point, the fields freq and dad are set. We can now
* generate the bit lengths.
*/
gen_bitlen(s, (tree_desc *)desc);
/* The field len is now set, we can generate the bit codes */
gen_codes((ct_data *)tree, max_code, s->bl_count);
}
/* ===========================================================================
* Scan a literal or distance tree to determine the frequencies of the codes
* in the bit length tree.
*/
static void scan_tree(deflate_state *s, ct_data *tree, int max_code) {
/* tree: the tree to be scanned */
/* max_code: and its largest code of non zero frequency */
int n; /* iterates over all tree elements */
int prevlen = -1; /* last emitted length */
int curlen; /* length of current code */
int nextlen = tree[0].Len; /* length of next code */
uint16_t count = 0; /* repeat count of the current code */
uint16_t max_count = 7; /* max repeat count */
uint16_t min_count = 4; /* min repeat count */
if (nextlen == 0)
max_count = 138, min_count = 3;
tree[max_code+1].Len = (uint16_t)0xffff; /* guard */
for (n = 0; n <= max_code; n++) {
curlen = nextlen;
nextlen = tree[n+1].Len;
if (++count < max_count && curlen == nextlen) {
continue;
} else if (count < min_count) {
s->bl_tree[curlen].Freq += count;
} else if (curlen != 0) {
if (curlen != prevlen)
s->bl_tree[curlen].Freq++;
s->bl_tree[REP_3_6].Freq++;
} else if (count <= 10) {
s->bl_tree[REPZ_3_10].Freq++;
} else {
s->bl_tree[REPZ_11_138].Freq++;
}
count = 0;
prevlen = curlen;
if (nextlen == 0) {
max_count = 138, min_count = 3;
} else if (curlen == nextlen) {
max_count = 6, min_count = 3;
} else {
max_count = 7, min_count = 4;
}
}
}
/* ===========================================================================
* Send a literal or distance tree in compressed form, using the codes in
* bl_tree.
*/
static void send_tree(deflate_state *s, ct_data *tree, int max_code) {
/* tree: the tree to be scanned */
/* max_code and its largest code of non zero frequency */
int n; /* iterates over all tree elements */
int prevlen = -1; /* last emitted length */
int curlen; /* length of current code */
int nextlen = tree[0].Len; /* length of next code */
int count = 0; /* repeat count of the current code */
int max_count = 7; /* max repeat count */
int min_count = 4; /* min repeat count */
/* tree[max_code+1].Len = -1; */ /* guard already set */
if (nextlen == 0)
max_count = 138, min_count = 3;
// Temp local variables
uint32_t bi_valid = s->bi_valid;
uint64_t bi_buf = s->bi_buf;
for (n = 0; n <= max_code; n++) {
curlen = nextlen;
nextlen = tree[n+1].Len;
if (++count < max_count && curlen == nextlen) {
continue;
} else if (count < min_count) {
do {
send_code(s, curlen, s->bl_tree, bi_buf, bi_valid);
} while (--count != 0);
} else if (curlen != 0) {
if (curlen != prevlen) {
send_code(s, curlen, s->bl_tree, bi_buf, bi_valid);
count--;
}
Assert(count >= 3 && count <= 6, " 3_6?");
send_code(s, REP_3_6, s->bl_tree, bi_buf, bi_valid);
send_bits(s, count-3, 2, bi_buf, bi_valid);
} else if (count <= 10) {
send_code(s, REPZ_3_10, s->bl_tree, bi_buf, bi_valid);
send_bits(s, count-3, 3, bi_buf, bi_valid);
} else {
send_code(s, REPZ_11_138, s->bl_tree, bi_buf, bi_valid);
send_bits(s, count-11, 7, bi_buf, bi_valid);
}
count = 0;
prevlen = curlen;
if (nextlen == 0) {
max_count = 138, min_count = 3;
} else if (curlen == nextlen) {
max_count = 6, min_count = 3;
} else {
max_count = 7, min_count = 4;
}
}
// Store back temp variables
s->bi_buf = bi_buf;
s->bi_valid = bi_valid;
}
/* ===========================================================================
* Construct the Huffman tree for the bit lengths and return the index in
* bl_order of the last bit length code to send.
*/
static int build_bl_tree(deflate_state *s) {
int max_blindex; /* index of last bit length code of non zero freq */
/* Determine the bit length frequencies for literal and distance trees */
scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
/* Build the bit length tree: */
build_tree(s, (tree_desc *)(&(s->bl_desc)));
/* opt_len now includes the length of the tree representations, except
* the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
*/
/* Determine the number of bit length codes to send. The pkzip format
* requires that at least 4 bit length codes be sent. (appnote.txt says
* 3 but the actual value used is 4.)
*/
for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
if (s->bl_tree[bl_order[max_blindex]].Len != 0)
break;
}
/* Update opt_len to include the bit length tree and counts */
s->opt_len += 3*((unsigned long)max_blindex+1) + 5+5+4;
Tracev((stderr, "\ndyn trees: dyn %lu, stat %lu", s->opt_len, s->static_len));
return max_blindex;
}
/* ===========================================================================
* Send the header for a block using dynamic Huffman trees: the counts, the
* lengths of the bit length codes, the literal tree and the distance tree.
* IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
*/
static void send_all_trees(deflate_state *s, int lcodes, int dcodes, int blcodes) {
int rank; /* index in bl_order */
Assert(lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
Assert(lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, "too many codes");
// Temp local variables
uint32_t bi_valid = s->bi_valid;
uint64_t bi_buf = s->bi_buf;
Tracev((stderr, "\nbl counts: "));
send_bits(s, lcodes-257, 5, bi_buf, bi_valid); /* not +255 as stated in appnote.txt */
send_bits(s, dcodes-1, 5, bi_buf, bi_valid);
send_bits(s, blcodes-4, 4, bi_buf, bi_valid); /* not -3 as stated in appnote.txt */
for (rank = 0; rank < blcodes; rank++) {
Tracev((stderr, "\nbl code %2u ", bl_order[rank]));
send_bits(s, s->bl_tree[bl_order[rank]].Len, 3, bi_buf, bi_valid);
}
Tracev((stderr, "\nbl tree: sent %lu", s->bits_sent));
// Store back temp variables
s->bi_buf = bi_buf;
s->bi_valid = bi_valid;
send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
Tracev((stderr, "\nlit tree: sent %lu", s->bits_sent));
send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
Tracev((stderr, "\ndist tree: sent %lu", s->bits_sent));
}
/* ===========================================================================
* Send a stored block
*/
void Z_INTERNAL zng_tr_stored_block(deflate_state *s, char *buf, uint32_t stored_len, int last) {
/* buf: input block */
/* stored_len: length of input block */
/* last: one if this is the last block for a file */
zng_tr_emit_tree(s, STORED_BLOCK, last); /* send block type */
zng_tr_emit_align(s); /* align on byte boundary */
cmpr_bits_align(s);
put_short(s, (uint16_t)stored_len);
put_short(s, (uint16_t)~stored_len);
cmpr_bits_add(s, 32);
sent_bits_add(s, 32);
if (stored_len) {
memcpy(s->pending_buf + s->pending, (unsigned char *)buf, stored_len);
s->pending += stored_len;
cmpr_bits_add(s, stored_len << 3);
sent_bits_add(s, stored_len << 3);
}
}
/* ===========================================================================
* Send one empty static block to give enough lookahead for inflate.
* This takes 10 bits, of which 7 may remain in the bit buffer.
*/
void Z_INTERNAL zng_tr_align(deflate_state *s) {
zng_tr_emit_tree(s, STATIC_TREES, 0);
zng_tr_emit_end_block(s, static_ltree, 0);
zng_tr_flush_bits(s);
}
/* ===========================================================================
* Determine the best encoding for the current block: dynamic trees, static
* trees or store, and write out the encoded block.
*/
void Z_INTERNAL zng_tr_flush_block(deflate_state *s, char *buf, uint32_t stored_len, int last) {
/* buf: input block, or NULL if too old */
/* stored_len: length of input block */
/* last: one if this is the last block for a file */
unsigned long opt_lenb, static_lenb; /* opt_len and static_len in bytes */
int max_blindex = 0; /* index of last bit length code of non zero freq */
/* Build the Huffman trees unless a stored block is forced */
if (UNLIKELY(s->sym_next == 0)) {
/* Emit an empty static tree block with no codes */
opt_lenb = static_lenb = 0;
s->static_len = 7;
} else if (s->level > 0) {
/* Check if the file is binary or text */
if (s->strm->data_type == Z_UNKNOWN)
s->strm->data_type = detect_data_type(s);
/* Construct the literal and distance trees */
build_tree(s, (tree_desc *)(&(s->l_desc)));
Tracev((stderr, "\nlit data: dyn %lu, stat %lu", s->opt_len, s->static_len));
build_tree(s, (tree_desc *)(&(s->d_desc)));
Tracev((stderr, "\ndist data: dyn %lu, stat %lu", 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);
/* 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 %u lit %u ",
opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
s->sym_next / 3));
if (static_lenb <= opt_lenb || s->strategy == Z_FIXED)
opt_lenb = static_lenb;
} else {
Assert(buf != NULL, "lost buf");
opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
}
if (stored_len+4 <= opt_lenb && buf != NULL) {
/* 4: two words for the lengths
* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
* Otherwise we can't have processed more than WSIZE input bytes since
* the last block flush, because compression would have been
* successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
* transform a block into a stored block.
*/
zng_tr_stored_block(s, buf, stored_len, last);
} else if (static_lenb == opt_lenb) {
zng_tr_emit_tree(s, STATIC_TREES, last);
compress_block(s, (const ct_data *)static_ltree, (const ct_data *)static_dtree);
cmpr_bits_add(s, s->static_len);
} else {
zng_tr_emit_tree(s, DYN_TREES, last);
send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, max_blindex+1);
compress_block(s, (const ct_data *)s->dyn_ltree, (const ct_data *)s->dyn_dtree);
cmpr_bits_add(s, s->opt_len);
}
Assert(s->compressed_len == s->bits_sent, "bad compressed size");
/* The above check is made mod 2^32, for files larger than 512 MB
* and unsigned long implemented on 32 bits.
*/
init_block(s);
if (last) {
zng_tr_emit_align(s);
}
Tracev((stderr, "\ncomprlen %lu(%lu) ", s->compressed_len>>3, s->compressed_len-7*last));
}
/* ===========================================================================
* Send the block data compressed using the given Huffman trees
*/
static void compress_block(deflate_state *s, const ct_data *ltree, const ct_data *dtree) {
/* ltree: literal tree */
/* dtree: distance tree */
unsigned dist; /* distance of matched string */
int lc; /* match length or unmatched char (if dist == 0) */
unsigned sx = 0; /* running index in symbol buffers */
if (s->sym_next != 0) {
do {
#ifdef LIT_MEM
dist = s->d_buf[sx];
lc = s->l_buf[sx++];
#else
dist = s->sym_buf[sx++] & 0xff;
dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8;
lc = s->sym_buf[sx++];
#endif
if (dist == 0) {
zng_emit_lit(s, ltree, lc);
} else {
zng_emit_dist(s, ltree, dtree, lc, dist);
} /* literal or match pair ? */
/* Check for no overlay of pending_buf on needed symbols */
#ifdef LIT_MEM
Assert(s->pending < 2 * (s->lit_bufsize + sx), "pending_buf overflow");
#else
Assert(s->pending < s->lit_bufsize + sx, "pending_buf overflow");
#endif
} while (sx < s->sym_next);
}
zng_emit_end_block(s, ltree, 0);
}
/* ===========================================================================
* Check if the data type is TEXT or BINARY, using the following algorithm:
* - TEXT if the two conditions below are satisfied:
* a) There are no non-portable control characters belonging to the
* "black list" (0..6, 14..25, 28..31).
* b) There is at least one printable character belonging to the
* "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
* - BINARY otherwise.
* - The following partially-portable control characters form a
* "gray list" that is ignored in this detection algorithm:
* (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
* IN assertion: the fields Freq of dyn_ltree are set.
*/
static int detect_data_type(deflate_state *s) {
/* black_mask is the bit mask of black-listed bytes
* set bits 0..6, 14..25, and 28..31
* 0xf3ffc07f = binary 11110011111111111100000001111111
*/
unsigned long black_mask = 0xf3ffc07fUL;
int n;
/* Check for non-textual ("black-listed") bytes. */
for (n = 0; n <= 31; n++, black_mask >>= 1)
if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
return Z_BINARY;
/* Check for textual ("white-listed") bytes. */
if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 || s->dyn_ltree[13].Freq != 0)
return Z_TEXT;
for (n = 32; n < LITERALS; n++)
if (s->dyn_ltree[n].Freq != 0)
return Z_TEXT;
/* There are no "black-listed" or "white-listed" bytes:
* this stream either is empty or has tolerated ("gray-listed") bytes only.
*/
return Z_BINARY;
}
/* ===========================================================================
* Flush the bit buffer, keeping at most 7 bits in it.
*/
void Z_INTERNAL zng_tr_flush_bits(deflate_state *s) {
if (s->bi_valid >= 48) {
put_uint32(s, (uint32_t)s->bi_buf);
put_short(s, (uint16_t)(s->bi_buf >> 32));
s->bi_buf >>= 48;
s->bi_valid -= 48;
} else if (s->bi_valid >= 32) {
put_uint32(s, (uint32_t)s->bi_buf);
s->bi_buf >>= 32;
s->bi_valid -= 32;
}
if (s->bi_valid >= 16) {
put_short(s, (uint16_t)s->bi_buf);
s->bi_buf >>= 16;
s->bi_valid -= 16;
}
if (s->bi_valid >= 8) {
put_byte(s, s->bi_buf);
s->bi_buf >>= 8;
s->bi_valid -= 8;
}
}
/* ===========================================================================
* Reverse the first len bits of a code using bit manipulation
*/
Z_INTERNAL uint16_t PREFIX(bi_reverse)(unsigned code, int len) {
/* code: the value to invert */
/* len: its bit length */
Assert(len >= 1 && len <= 15, "code length must be 1-15");
#define bitrev8(b) \
(uint8_t)((((uint8_t)(b) * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32)
return (bitrev8(code >> 8) | (uint16_t)bitrev8(code) << 8) >> (16 - len);
}
+40
View File
@@ -0,0 +1,40 @@
#ifndef TREES_H_
#define TREES_H_
/* Constants */
#define DIST_CODE_LEN 512
/* see definition of array dist_code in trees.c */
#define MAX_BL_BITS 7
/* Bit length codes must not exceed MAX_BL_BITS bits */
#define REP_3_6 16
/* repeat previous bit length 3-6 times (2 bits of repeat count) */
#define REPZ_3_10 17
/* repeat a zero length 3-10 times (3 bits of repeat count) */
#define REPZ_11_138 18
/* repeat a zero length 11-138 times (7 bits of repeat count) */
static const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
= {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
static const int extra_dbits[D_CODES] /* extra bits for each distance code */
= {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
static const int extra_blbits[BL_CODES] /* extra bits for each bit length code */
= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
static const unsigned char bl_order[BL_CODES]
= {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
/* The lengths of the bit length codes are sent in order of decreasing
* probability, to avoid transmitting the lengths for unused bit length codes.
*/
/* Function definitions */
void gen_codes (ct_data *tree, int max_code, uint16_t *bl_count);
#endif
+231
View File
@@ -0,0 +1,231 @@
#ifndef TREES_EMIT_H_
#define TREES_EMIT_H_
#include "zbuild.h"
#include "trees.h"
#ifdef ZLIB_DEBUG
# include <ctype.h>
# include <inttypes.h>
#endif
/* trees.h */
extern Z_INTERNAL const ct_data static_ltree[L_CODES+2];
extern Z_INTERNAL const ct_data static_dtree[D_CODES];
extern const unsigned char Z_INTERNAL zng_dist_code[DIST_CODE_LEN];
extern const unsigned char Z_INTERNAL zng_length_code[STD_MAX_MATCH-STD_MIN_MATCH+1];
extern Z_INTERNAL const int base_length[LENGTH_CODES];
extern Z_INTERNAL const int base_dist[D_CODES];
/* Bit buffer and deflate code stderr tracing */
#ifdef ZLIB_DEBUG
# define send_bits_trace(s, value, length) { \
Tracevv((stderr, " l %2d v %4llx ", (int)(length), (long long)(value))); \
Assert(length > 0 && length <= BIT_BUF_SIZE, "invalid length"); \
}
# define send_code_trace(s, c) \
if (z_verbose > 2) { \
fprintf(stderr, "\ncd %3d ", (c)); \
}
#else
# define send_bits_trace(s, value, length)
# define send_code_trace(s, c)
#endif
/* If not enough room in bi_buf, use (valid) bits from bi_buf and
* (64 - bi_valid) bits from value, leaving (width - (64-bi_valid))
* unused bits in value.
*
* NOTE: Static analyzers can't evaluate value of total_bits, so we
* also need to make sure bi_valid is within acceptable range,
* otherwise the shifts will overflow.
*/
#define send_bits(s, t_val, t_len, bi_buf, bi_valid) {\
uint64_t val = (uint64_t)t_val;\
uint32_t len = (uint32_t)t_len;\
uint32_t total_bits = bi_valid + len;\
send_bits_trace(s, val, len);\
sent_bits_add(s, len);\
if (total_bits < BIT_BUF_SIZE && bi_valid < BIT_BUF_SIZE) {\
bi_buf |= val << bi_valid;\
bi_valid = total_bits;\
} else if (bi_valid >= BIT_BUF_SIZE) {\
put_uint64(s, bi_buf);\
bi_buf = val;\
bi_valid = len;\
} else {\
bi_buf |= val << bi_valid;\
put_uint64(s, bi_buf);\
bi_buf = val >> (BIT_BUF_SIZE - bi_valid);\
bi_valid = total_bits - BIT_BUF_SIZE;\
}\
}
/* Send a code of the given tree. c and tree must not have side effects */
#ifdef ZLIB_DEBUG
# define send_code(s, c, tree, bi_buf, bi_valid) { \
send_code_trace(s, c); \
send_bits(s, tree[c].Code, tree[c].Len, bi_buf, bi_valid); \
}
#else
# define send_code(s, c, tree, bi_buf, bi_valid) \
send_bits(s, tree[c].Code, tree[c].Len, bi_buf, bi_valid)
#endif
/* ===========================================================================
* Flush the bit buffer and align the output on a byte boundary
*/
static void bi_windup(deflate_state *s) {
if (s->bi_valid > 56) {
put_uint64(s, s->bi_buf);
} else {
if (s->bi_valid > 24) {
put_uint32(s, (uint32_t)s->bi_buf);
s->bi_buf >>= 32;
s->bi_valid -= 32;
}
if (s->bi_valid > 8) {
put_short(s, (uint16_t)s->bi_buf);
s->bi_buf >>= 16;
s->bi_valid -= 16;
}
if (s->bi_valid > 0) {
put_byte(s, s->bi_buf);
}
}
s->bi_buf = 0;
s->bi_valid = 0;
}
/* ===========================================================================
* Emit literal code
*/
static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
uint32_t bi_valid = s->bi_valid;
uint64_t bi_buf = s->bi_buf;
send_code(s, c, ltree, bi_buf, bi_valid);
s->bi_valid = bi_valid;
s->bi_buf = bi_buf;
Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
return ltree[c].Len;
}
/* ===========================================================================
* Emit match distance/length code
*/
static inline uint32_t zng_emit_dist(deflate_state *s, const ct_data *ltree, const ct_data *dtree,
uint32_t lc, uint32_t dist) {
uint32_t c, extra;
uint8_t code;
uint64_t match_bits;
uint32_t match_bits_len;
uint32_t bi_valid = s->bi_valid;
uint64_t bi_buf = s->bi_buf;
/* Send the length code, len is the match length - STD_MIN_MATCH */
code = zng_length_code[lc];
c = code+LITERALS+1;
Assert(c < L_CODES, "bad l_code");
send_code_trace(s, c);
match_bits = ltree[c].Code;
match_bits_len = ltree[c].Len;
extra = extra_lbits[code];
if (extra != 0) {
lc -= base_length[code];
match_bits |= ((uint64_t)lc << match_bits_len);
match_bits_len += extra;
}
dist--; /* dist is now the match distance - 1 */
code = d_code(dist);
Assert(code < D_CODES, "bad d_code");
send_code_trace(s, code);
/* Send the distance code */
match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
match_bits_len += dtree[code].Len;
extra = extra_dbits[code];
if (extra != 0) {
dist -= base_dist[code];
match_bits |= ((uint64_t)dist << match_bits_len);
match_bits_len += extra;
}
send_bits(s, match_bits, match_bits_len, bi_buf, bi_valid);
s->bi_valid = bi_valid;
s->bi_buf = bi_buf;
return match_bits_len;
}
/* ===========================================================================
* Emit end block
*/
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
uint32_t bi_valid = s->bi_valid;
uint64_t bi_buf = s->bi_buf;
send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
s->bi_valid = bi_valid;
s->bi_buf = bi_buf;
Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
last, s->pending, (uint64_t)s->strm->total_out));
Z_UNUSED(last);
}
/* ===========================================================================
* Emit literal and count bits
*/
static inline void zng_tr_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
cmpr_bits_add(s, zng_emit_lit(s, ltree, c));
}
/* ===========================================================================
* Emit match and count bits
*/
static inline void zng_tr_emit_dist(deflate_state *s, const ct_data *ltree, const ct_data *dtree,
uint32_t lc, uint32_t dist) {
cmpr_bits_add(s, zng_emit_dist(s, ltree, dtree, lc, dist));
}
/* ===========================================================================
* Emit start of block
*/
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
uint32_t bi_valid = s->bi_valid;
uint64_t bi_buf = s->bi_buf;
uint32_t header_bits = (type << 1) + last;
send_bits(s, header_bits, 3, bi_buf, bi_valid);
cmpr_bits_add(s, 3);
s->bi_valid = bi_valid;
s->bi_buf = bi_buf;
Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
}
/* ===========================================================================
* Align bit buffer on a byte boundary and count bits
*/
static inline void zng_tr_emit_align(deflate_state *s) {
bi_windup(s); /* align on byte boundary */
sent_bits_align(s);
}
/* ===========================================================================
* Emit an end block and align bit buffer if last block
*/
static inline void zng_tr_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
zng_emit_end_block(s, ltree, last);
cmpr_bits_add(s, 7);
if (last)
zng_tr_emit_align(s);
}
#endif
+132
View File
@@ -0,0 +1,132 @@
#ifndef TREES_TBL_H_
#define TREES_TBL_H_
/* header created automatically with maketrees.c */
Z_INTERNAL const ct_data static_ltree[L_CODES+2] = {
{{ 12},{8}}, {{140},{8}}, {{ 76},{8}}, {{204},{8}}, {{ 44},{8}},
{{172},{8}}, {{108},{8}}, {{236},{8}}, {{ 28},{8}}, {{156},{8}},
{{ 92},{8}}, {{220},{8}}, {{ 60},{8}}, {{188},{8}}, {{124},{8}},
{{252},{8}}, {{ 2},{8}}, {{130},{8}}, {{ 66},{8}}, {{194},{8}},
{{ 34},{8}}, {{162},{8}}, {{ 98},{8}}, {{226},{8}}, {{ 18},{8}},
{{146},{8}}, {{ 82},{8}}, {{210},{8}}, {{ 50},{8}}, {{178},{8}},
{{114},{8}}, {{242},{8}}, {{ 10},{8}}, {{138},{8}}, {{ 74},{8}},
{{202},{8}}, {{ 42},{8}}, {{170},{8}}, {{106},{8}}, {{234},{8}},
{{ 26},{8}}, {{154},{8}}, {{ 90},{8}}, {{218},{8}}, {{ 58},{8}},
{{186},{8}}, {{122},{8}}, {{250},{8}}, {{ 6},{8}}, {{134},{8}},
{{ 70},{8}}, {{198},{8}}, {{ 38},{8}}, {{166},{8}}, {{102},{8}},
{{230},{8}}, {{ 22},{8}}, {{150},{8}}, {{ 86},{8}}, {{214},{8}},
{{ 54},{8}}, {{182},{8}}, {{118},{8}}, {{246},{8}}, {{ 14},{8}},
{{142},{8}}, {{ 78},{8}}, {{206},{8}}, {{ 46},{8}}, {{174},{8}},
{{110},{8}}, {{238},{8}}, {{ 30},{8}}, {{158},{8}}, {{ 94},{8}},
{{222},{8}}, {{ 62},{8}}, {{190},{8}}, {{126},{8}}, {{254},{8}},
{{ 1},{8}}, {{129},{8}}, {{ 65},{8}}, {{193},{8}}, {{ 33},{8}},
{{161},{8}}, {{ 97},{8}}, {{225},{8}}, {{ 17},{8}}, {{145},{8}},
{{ 81},{8}}, {{209},{8}}, {{ 49},{8}}, {{177},{8}}, {{113},{8}},
{{241},{8}}, {{ 9},{8}}, {{137},{8}}, {{ 73},{8}}, {{201},{8}},
{{ 41},{8}}, {{169},{8}}, {{105},{8}}, {{233},{8}}, {{ 25},{8}},
{{153},{8}}, {{ 89},{8}}, {{217},{8}}, {{ 57},{8}}, {{185},{8}},
{{121},{8}}, {{249},{8}}, {{ 5},{8}}, {{133},{8}}, {{ 69},{8}},
{{197},{8}}, {{ 37},{8}}, {{165},{8}}, {{101},{8}}, {{229},{8}},
{{ 21},{8}}, {{149},{8}}, {{ 85},{8}}, {{213},{8}}, {{ 53},{8}},
{{181},{8}}, {{117},{8}}, {{245},{8}}, {{ 13},{8}}, {{141},{8}},
{{ 77},{8}}, {{205},{8}}, {{ 45},{8}}, {{173},{8}}, {{109},{8}},
{{237},{8}}, {{ 29},{8}}, {{157},{8}}, {{ 93},{8}}, {{221},{8}},
{{ 61},{8}}, {{189},{8}}, {{125},{8}}, {{253},{8}}, {{ 19},{9}},
{{275},{9}}, {{147},{9}}, {{403},{9}}, {{ 83},{9}}, {{339},{9}},
{{211},{9}}, {{467},{9}}, {{ 51},{9}}, {{307},{9}}, {{179},{9}},
{{435},{9}}, {{115},{9}}, {{371},{9}}, {{243},{9}}, {{499},{9}},
{{ 11},{9}}, {{267},{9}}, {{139},{9}}, {{395},{9}}, {{ 75},{9}},
{{331},{9}}, {{203},{9}}, {{459},{9}}, {{ 43},{9}}, {{299},{9}},
{{171},{9}}, {{427},{9}}, {{107},{9}}, {{363},{9}}, {{235},{9}},
{{491},{9}}, {{ 27},{9}}, {{283},{9}}, {{155},{9}}, {{411},{9}},
{{ 91},{9}}, {{347},{9}}, {{219},{9}}, {{475},{9}}, {{ 59},{9}},
{{315},{9}}, {{187},{9}}, {{443},{9}}, {{123},{9}}, {{379},{9}},
{{251},{9}}, {{507},{9}}, {{ 7},{9}}, {{263},{9}}, {{135},{9}},
{{391},{9}}, {{ 71},{9}}, {{327},{9}}, {{199},{9}}, {{455},{9}},
{{ 39},{9}}, {{295},{9}}, {{167},{9}}, {{423},{9}}, {{103},{9}},
{{359},{9}}, {{231},{9}}, {{487},{9}}, {{ 23},{9}}, {{279},{9}},
{{151},{9}}, {{407},{9}}, {{ 87},{9}}, {{343},{9}}, {{215},{9}},
{{471},{9}}, {{ 55},{9}}, {{311},{9}}, {{183},{9}}, {{439},{9}},
{{119},{9}}, {{375},{9}}, {{247},{9}}, {{503},{9}}, {{ 15},{9}},
{{271},{9}}, {{143},{9}}, {{399},{9}}, {{ 79},{9}}, {{335},{9}},
{{207},{9}}, {{463},{9}}, {{ 47},{9}}, {{303},{9}}, {{175},{9}},
{{431},{9}}, {{111},{9}}, {{367},{9}}, {{239},{9}}, {{495},{9}},
{{ 31},{9}}, {{287},{9}}, {{159},{9}}, {{415},{9}}, {{ 95},{9}},
{{351},{9}}, {{223},{9}}, {{479},{9}}, {{ 63},{9}}, {{319},{9}},
{{191},{9}}, {{447},{9}}, {{127},{9}}, {{383},{9}}, {{255},{9}},
{{511},{9}}, {{ 0},{7}}, {{ 64},{7}}, {{ 32},{7}}, {{ 96},{7}},
{{ 16},{7}}, {{ 80},{7}}, {{ 48},{7}}, {{112},{7}}, {{ 8},{7}},
{{ 72},{7}}, {{ 40},{7}}, {{104},{7}}, {{ 24},{7}}, {{ 88},{7}},
{{ 56},{7}}, {{120},{7}}, {{ 4},{7}}, {{ 68},{7}}, {{ 36},{7}},
{{100},{7}}, {{ 20},{7}}, {{ 84},{7}}, {{ 52},{7}}, {{116},{7}},
{{ 3},{8}}, {{131},{8}}, {{ 67},{8}}, {{195},{8}}, {{ 35},{8}},
{{163},{8}}, {{ 99},{8}}, {{227},{8}}
};
Z_INTERNAL const ct_data static_dtree[D_CODES] = {
{{ 0},{5}}, {{16},{5}}, {{ 8},{5}}, {{24},{5}}, {{ 4},{5}},
{{20},{5}}, {{12},{5}}, {{28},{5}}, {{ 2},{5}}, {{18},{5}},
{{10},{5}}, {{26},{5}}, {{ 6},{5}}, {{22},{5}}, {{14},{5}},
{{30},{5}}, {{ 1},{5}}, {{17},{5}}, {{ 9},{5}}, {{25},{5}},
{{ 5},{5}}, {{21},{5}}, {{13},{5}}, {{29},{5}}, {{ 3},{5}},
{{19},{5}}, {{11},{5}}, {{27},{5}}, {{ 7},{5}}, {{23},{5}}
};
const unsigned char Z_INTERNAL zng_dist_code[DIST_CODE_LEN] = {
0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
};
const unsigned char Z_INTERNAL zng_length_code[STD_MAX_MATCH-STD_MIN_MATCH+1] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
};
Z_INTERNAL const int base_length[LENGTH_CODES] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
64, 80, 96, 112, 128, 160, 192, 224, 0
};
Z_INTERNAL const int base_dist[D_CODES] = {
0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
};
#endif /* TREES_TBL_H_ */
+80
View File
@@ -0,0 +1,80 @@
/* uncompr.c -- decompress a memory buffer
* Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "zutil.h"
/* ===========================================================================
Decompresses the source buffer into the destination buffer. *sourceLen is
the byte length of the source buffer. Upon entry, *destLen is the total size
of the destination buffer, which must be large enough to hold the entire
uncompressed data. (The size of the uncompressed data must have been saved
previously by the compressor and transmitted to the decompressor by some
mechanism outside the scope of this compression library.) Upon exit,
*destLen is the size of the decompressed data and *sourceLen is the number
of source bytes consumed. Upon return, source + *sourceLen points to the
first unused input byte.
uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
Z_DATA_ERROR if the input data was corrupted, including if the input data is
an incomplete zlib stream.
*/
int Z_EXPORT PREFIX(uncompress2)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t *sourceLen) {
PREFIX3(stream) stream;
int err;
const unsigned int max = (unsigned int)-1;
z_uintmax_t len, left;
unsigned char buf[1]; /* for detection of incomplete stream when *destLen == 0 */
len = *sourceLen;
if (*destLen) {
left = *destLen;
*destLen = 0;
} else {
left = 1;
dest = buf;
}
stream.next_in = (z_const unsigned char *)source;
stream.avail_in = 0;
stream.zalloc = NULL;
stream.zfree = NULL;
stream.opaque = NULL;
err = PREFIX(inflateInit)(&stream);
if (err != Z_OK) return err;
stream.next_out = dest;
stream.avail_out = 0;
do {
if (stream.avail_out == 0) {
stream.avail_out = left > (unsigned long)max ? max : (unsigned int)left;
left -= stream.avail_out;
}
if (stream.avail_in == 0) {
stream.avail_in = len > (unsigned long)max ? max : (unsigned int)len;
len -= stream.avail_in;
}
err = PREFIX(inflate)(&stream, Z_NO_FLUSH);
} while (err == Z_OK);
*sourceLen -= len + stream.avail_in;
if (dest != buf)
*destLen = (z_size_t)stream.total_out;
else if (stream.total_out && err == Z_BUF_ERROR)
left = 1;
PREFIX(inflateEnd)(&stream);
return err == Z_STREAM_END ? Z_OK :
err == Z_NEED_DICT ? Z_DATA_ERROR :
err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
err;
}
int Z_EXPORT PREFIX(uncompress)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t sourceLen) {
return PREFIX(uncompress2)(dest, destLen, source, &sourceLen);
}
+256
View File
@@ -0,0 +1,256 @@
# Makefile for zlib using Microsoft (Visual) C
# zlib is copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
#
# Usage:
# nmake -f win32/Makefile.a64 (standard build)
# nmake -f win32/Makefile.a64 LOC=-DFOO (nonstandard build)
# The toplevel directory of the source tree.
#
TOP = .
# optional build flags
LOC =
# variables
STATICLIB = zlib.lib
SHAREDLIB = zlib1.dll
IMPLIB = zdll.lib
SYMBOL_PREFIX =
CC = cl
LD = link
AR = lib
RC = rc
CP = copy /y
INCLUDES = -I$(TOP) -I$(TOP)/arch/arm -I$(TOP)/arch/generic
CFLAGS = -nologo -MD -W3 -O2 -Oy- -Zi -Fd"zlib" $(LOC) $(INCLUDES)
WFLAGS = \
-D_ARM64_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE=1 \
-D_CRT_SECURE_NO_DEPRECATE \
-D_CRT_NONSTDC_NO_DEPRECATE \
-DARM_FEATURES \
-DARM_NEON_HASLD4 \
#
LDFLAGS = -nologo -debug -incremental:no -opt:ref -manifest
ARFLAGS = -nologo
RCFLAGS = /dARM64 /r
DEFFILE = zlib.def
RCFILE = zlib1.rc
RESFILE = zlib1.res
WITH_GZFILEOP = yes
ZLIB_COMPAT =
SUFFIX =
OBJS = \
adler32.obj \
adler32_c.obj \
adler32_fold_c.obj \
arm_features.obj \
chunkset_c.obj \
compare256_c.obj \
compress.obj \
cpu_features.obj \
crc32.obj \
crc32_braid_c.obj \
crc32_braid_comb.obj \
crc32_c.obj \
crc32_chorba_c.obj \
crc32_fold_c.obj \
deflate.obj \
deflate_fast.obj \
deflate_huff.obj \
deflate_medium.obj \
deflate_quick.obj \
deflate_rle.obj \
deflate_slow.obj \
deflate_stored.obj \
functable.obj \
infback.obj \
inflate.obj \
inftrees.obj \
insert_string.obj \
insert_string_roll.obj \
slide_hash_c.obj \
trees.obj \
uncompr.obj \
zutil.obj \
#
!if "$(ZLIB_COMPAT)" != ""
WITH_GZFILEOP = yes
WFLAGS = $(WFLAGS) -DZLIB_COMPAT
DEFFILE = zlibcompat.def
!else
STATICLIB = zlib-ng.lib
SHAREDLIB = zlib-ng1.dll
IMPLIB = zngdll.lib
DEFFILE = zlib-ng.def
RCFILE = zlib-ng1.rc
RESFILE = zlib-ng1.res
SUFFIX = -ng
!endif
!if "$(WITH_GZFILEOP)" != ""
WFLAGS = $(WFLAGS) -DWITH_GZFILEOP
OBJS = $(OBJS) gzlib.obj gzread.obj gzwrite.obj
!endif
WFLAGS = $(WFLAGS) \
-DARM_CRC32 \
-D__ARM_NEON__=1 \
-DARM_NEON \
-DARM_NOCHECK_NEON \
#
OBJS = $(OBJS) crc32_armv8.obj adler32_neon.obj chunkset_neon.obj compare256_neon.obj slide_hash_neon.obj
# targets
all: $(STATICLIB) $(SHAREDLIB) $(IMPLIB) \
example.exe minigzip.exe example_d.exe minigzip_d.exe
!if "$(SYMBOL_PREFIX)" != ""
zlib_name_mangling$(SUFFIX).h: zlib_name_mangling$(SUFFIX).h.in
cscript $(TOP)\win32\replace.vbs $(TOP)\zlib_name_mangling$(SUFFIX).h.in zlib_name_mangling$(SUFFIX).h "@ZLIB_SYMBOL_PREFIX@" "$(SYMBOL_PREFIX)"
!else
zlib_name_mangling$(SUFFIX).h: zlib_name_mangling.h.empty
$(CP) $(TOP)\zlib_name_mangling.h.empty zlib_name_mangling$(SUFFIX).h
!endif
zlib$(SUFFIX).h: zlib$(SUFFIX).h.in
cscript $(TOP)\win32\replace.vbs $(TOP)\zlib$(SUFFIX).h.in zlib$(SUFFIX).h "@ZLIB_SYMBOL_PREFIX@" "$(SYMBOL_PREFIX)"
gzread.c: gzread.c.in
cscript $(TOP)\win32\replace.vbs $(TOP)\gzread.c.in gzread.c "@ZLIB_SYMBOL_PREFIX@" "$(SYMBOL_PREFIX)"
zconf: $(TOP)/zconf$(SUFFIX).h.in $(TOP)/zlib$(SUFFIX).h $(TOP)/zlib_name_mangling$(SUFFIX).h
$(CP) $(TOP)\zconf$(SUFFIX).h.in $(TOP)\zconf$(SUFFIX).h
$(TOP)/win32/$(DEFFILE): $(TOP)/win32/$(DEFFILE).in
cscript $(TOP)\win32\replace.vbs $(TOP)/win32/$(DEFFILE).in $(TOP)/win32/$(DEFFILE) "@ZLIB_SYMBOL_PREFIX@" "$(SYMBOL_PREFIX)"
$(STATICLIB): zconf $(OBJS)
$(AR) $(ARFLAGS) -out:$@ $(OBJS)
$(IMPLIB): $(SHAREDLIB)
$(SHAREDLIB): zconf $(TOP)/win32/$(DEFFILE) $(OBJS) $(RESFILE)
$(LD) $(LDFLAGS) -def:$(TOP)/win32/$(DEFFILE) -dll -implib:$(IMPLIB) \
-out:$@ -base:0x55A4C0000 $(OBJS) $(RESFILE)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;2
example.exe: example.obj gzlib2.obj gzread2.obj gzwrite2.obj $(STATICLIB)
$(LD) $(LDFLAGS) example.obj gzlib2.obj gzread2.obj gzwrite2.obj $(STATICLIB)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;1
minigzip.exe: minigzip.obj gzlib2.obj gzread2.obj gzwrite2.obj $(STATICLIB)
$(LD) $(LDFLAGS) minigzip.obj gzlib2.obj gzread2.obj gzwrite2.obj $(STATICLIB)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;1
example_d.exe: example.obj gzlib2.obj gzread2.obj gzwrite2.obj $(IMPLIB)
$(LD) $(LDFLAGS) -out:$@ example.obj gzlib2.obj gzread2.obj gzwrite2.obj $(IMPLIB)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;1
minigzip_d.exe: minigzip.obj gzlib2.obj gzread2.obj gzwrite2.obj $(IMPLIB)
$(LD) $(LDFLAGS) -out:$@ minigzip.obj gzlib2.obj gzread2.obj gzwrite2.obj $(IMPLIB)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;1
{$(TOP)}.c.obj:
$(CC) -c $(WFLAGS) $(CFLAGS) $<
gzlib2.obj: gzlib.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
$(CC) -c $(WFLAGS) $(CFLAGS) -DWITH_GZFILEOP -Fogzlib2.obj gzlib.c
gzread2.obj: gzread.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
$(CC) -c $(WFLAGS) $(CFLAGS) -DWITH_GZFILEOP -Fogzread2.obj gzread.c
gzwrite2.obj: gzwrite.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
$(CC) -c $(WFLAGS) $(CFLAGS) -DWITH_GZFILEOP -Fogzwrite2.obj gzwrite.c
{$(TOP)/arch/arm}.c.obj:
$(CC) -c -I$(TOP) $(WFLAGS) $(CFLAGS) $<
{$(TOP)/arch/generic}.c.obj:
$(CC) -c -I$(TOP) $(WFLAGS) $(CFLAGS) $<
{$(TOP)/test}.c.obj:
$(CC) -c -I$(TOP) $(WFLAGS) $(CFLAGS) -DWITH_GZFILEOP $<
$(TOP)/zconf$(SUFFIX).h: zconf
adler32.obj: $(TOP)/adler32.c $(TOP)/zbuild.h $(TOP)/functable.h $(TOP)/adler32_p.h
adler32_c.obj: $(TOP)/arch/generic/adler32_c.c $(TOP)/zbuild.h $(TOP)/functable.h $(TOP)/adler32_p.h
adler32_fold_c.obj: $(TOP)/arch/generic/adler32_fold_c.c $(TOP)/zbuild.h $(TOP)/functable.h
chunkset_c.obj: $(TOP)/arch/generic/chunkset_c.c $(TOP)/zbuild.h $(TOP)/chunkset_tpl.h $(TOP)/inffast_tpl.h
compare256_c.obj: $(TOP)/arch/generic/compare256_c.c $(TOP)/zbuild.h $(TOP)/zmemory.h $(TOP)/deflate.h $(TOP)/fallback_builtins.h $(TOP)/match_tpl.h
compress.obj: $(TOP)/compress.c $(TOP)/zbuild.h $(TOP)/zutil.h
cpu_features.obj: $(TOP)/cpu_features.c $(TOP)/cpu_features.h $(TOP)/zbuild.h
crc32.obj: $(TOP)/crc32.c $(TOP)/zbuild.h $(TOP)/functable.h $(TOP)/crc32_braid_tbl.h
crc32_braid_c.obj: $(TOP)/arch/generic/crc32_braid_c.c $(TOP)/zbuild.h $(TOP)/crc32_braid_p.h $(TOP)/crc32_braid_tbl.h
crc32_braid_comb.obj: $(TOP)/crc32_braid_comb.c $(TOP)/zutil.h $(TOP)/crc32_braid_p.h $(TOP)/crc32_braid_tbl.h $(TOP)/crc32_braid_comb_p.h
crc32_c.obj: $(TOP)/arch/generic/crc32_c.c $(TOP)/zbuild.h $(TOP)/crc32_braid_p.h
crc32_chorba_c.obj: $(TOP)/arch/generic/crc32_chorba_c.c $(TOP)/zbuild.h $(TOP)/crc32_braid_p.h $(TOP)/crc32_braid_tbl.h
crc32_fold_c.obj: $(TOP)/arch/generic/crc32_fold_c.c $(TOP)/zbuild.h $(TOP)/crc32.h $(TOP)/functable.h $(TOP)/zutil.h
deflate.obj: $(TOP)/deflate.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_fast.obj: $(TOP)/deflate_fast.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_huff.obj: $(TOP)/deflate_huff.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_medium.obj: $(TOP)/deflate_medium.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_quick.obj: $(TOP)/deflate_quick.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h $(TOP)/trees_emit.h $(TOP)/zmemory.h
deflate_rle.obj: $(TOP)/deflate_rle.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h $(TOP)/compare256_rle.h
deflate_slow.obj: $(TOP)/deflate_slow.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_stored.obj: $(TOP)/deflate_stored.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
functable.obj: $(TOP)/functable.c $(TOP)/zbuild.h $(TOP)/functable.h $(TOP)/cpu_features.h $(TOP)/arch/arm/arm_features.h $(TOP)/arch_functions.h
gzlib.obj: $(TOP)/gzlib.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
gzread.obj: $(TOP)/gzread.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
gzwrite.obj: $(TOP)/gzwrite.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
infback.obj: $(TOP)/infback.c $(TOP)/zbuild.h $(TOP)/zutil.h $(TOP)/inftrees.h $(TOP)/inflate.h $(TOP)/inflate_p.h $(TOP)/functable.h
inflate.obj: $(TOP)/inflate.c $(TOP)/zbuild.h $(TOP)/zutil.h $(TOP)/inftrees.h $(TOP)/inflate.h $(TOP)/inflate_p.h $(TOP)/functable.h $(TOP)/inffixed_tbl.h
inftrees.obj: $(TOP)/inftrees.c $(TOP)/zbuild.h $(TOP)/zutil.h $(TOP)/inftrees.h
insert_string.obj: $(TOP)/insert_string.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/insert_string_tpl.h
insert_string_roll.obj: $(TOP)/insert_string_roll.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/insert_string_tpl.h
slide_hash_c.obj: $(TOP)/arch/generic/slide_hash_c.c $(TOP)/zbuild.h $(TOP)/deflate.h
slide_hash_neon.obj: $(TOP)/arch/arm/slide_hash_neon.c $(TOP)/arch/arm/neon_intrins.h $(TOP)/zbuild.h $(TOP)/deflate.h
trees.obj: $(TOP)/trees.c $(TOP)/trees.h $(TOP)/trees_emit.h $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/trees_tbl.h
uncompr.obj: $(TOP)/uncompr.c $(TOP)/zbuild.h $(TOP)/zutil.h
zutil.obj: $(TOP)/zutil.c $(TOP)/zbuild.h $(TOP)/zutil.h $(TOP)/zutil_p.h
$(RESFILE): $(TOP)/win32/$(RCFILE)
$(RC) $(RCFLAGS) /fo$@ $(TOP)/win32/$(RCFILE)
# testing
test: example.exe minigzip.exe
example
echo hello world | minigzip | minigzip -d
testdll: example_d.exe minigzip_d.exe
example_d
echo hello world | minigzip_d | minigzip_d -d
example.obj: $(TOP)/test/example.c $(TOP)/zbuild.h $(TOP)/zlib$(SUFFIX).h $(TOP)/deflate.h $(TOP)/test/test_shared_ng.h
minigzip.obj: $(TOP)/test/minigzip.c $(TOP)/zbuild.h $(TOP)/zlib$(SUFFIX).h
# cleanup
clean:
-del $(STATICLIB)
-del $(SHAREDLIB)
-del $(IMPLIB)
-del *.obj
-del *.res
-del *.exp
-del *.exe
-del *.pdb
-del *.manifest
distclean: clean
-del zconf$(SUFFIX).h
-del zlib$(SUFFIX).h
-del zlib_name_mangling$(SUFFIX).h
-del $(TOP)\win32\zlib.def
-del $(TOP)\win32\zlibcompat.def
-del $(TOP)\win32\zlib-ng.def
-del gzread.c
+276
View File
@@ -0,0 +1,276 @@
# Makefile for zlib using Microsoft (Visual) C
# zlib is copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
#
# Usage:
# nmake -f win32/Makefile.arm (standard build)
# nmake -f win32/Makefile.arm LOC=-DFOO (nonstandard build)
# The toplevel directory of the source tree.
#
TOP = .
# optional build flags
LOC =
# variables
STATICLIB = zlib.lib
SHAREDLIB = zlib1.dll
IMPLIB = zdll.lib
SYMBOL_PREFIX =
CC = cl
LD = link
AR = lib
RC = rc
CP = copy /y
INCLUDES = -I$(TOP) -I$(TOP)/arch/arm -I$(TOP)/arch/generic
CFLAGS = -nologo -MD -W3 -O2 -Oy- -Zi -Fd"zlib" $(LOC) $(INCLUDES)
WFLAGS = \
-D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE=1 \
-D_CRT_SECURE_NO_DEPRECATE \
-D_CRT_NONSTDC_NO_DEPRECATE \
-DARM_FEATURES \
-DARM_NEON_HASLD4 \
#
LDFLAGS = -nologo -debug -incremental:no -opt:ref -manifest
ARFLAGS = -nologo
RCFLAGS = /dARM /r
DEFFILE = zlib.def
RCFILE = zlib1.rc
RESFILE = zlib1.res
WITH_GZFILEOP = yes
ZLIB_COMPAT =
WITH_ARMV8 =
WITH_NEON =
WITH_ARMV6 =
WITH_VFPV3 =
NEON_ARCH = /arch:VFPv4
SUFFIX =
OBJS = \
adler32.obj \
adler32_c.obj \
adler32_fold_c.obj \
arm_features.obj \
chunkset_c.obj \
compare256_c.obj \
compress.obj \
cpu_features.obj \
crc32.obj \
crc32_braid_c.obj \
crc32_braid_comb.obj \
crc32_c.obj \
crc32_chorba_c.obj \
crc32_fold_c.obj \
deflate.obj \
deflate_fast.obj \
deflate_huff.obj \
deflate_medium.obj \
deflate_quick.obj \
deflate_rle.obj \
deflate_slow.obj \
deflate_stored.obj \
functable.obj \
infback.obj \
inflate.obj \
inftrees.obj \
insert_string.obj \
insert_string_roll.obj \
slide_hash_c.obj \
trees.obj \
uncompr.obj \
zutil.obj \
#
!if "$(ZLIB_COMPAT)" != ""
WITH_GZFILEOP = yes
WFLAGS = $(WFLAGS) -DZLIB_COMPAT
DEFFILE = zlibcompat.def
!else
STATICLIB = zlib-ng.lib
SHAREDLIB = zlib-ng1.dll
IMPLIB = zngdll.lib
DEFFILE = zlib-ng.def
RCFILE = zlib-ng1.rc
RESFILE = zlib-ng1.res
SUFFIX = -ng
!endif
!if "$(WITH_GZFILEOP)" != ""
WFLAGS = $(WFLAGS) -DWITH_GZFILEOP
OBJS = $(OBJS) gzlib.obj gzread.obj gzwrite.obj
!endif
!if "$(WITH_ARMV8)" != ""
WFLAGS = $(WFLAGS) -DARM_CRC32
OBJS = $(OBJS) crc32_armv8.obj
!endif
!if "$(WITH_VFPV3)" != ""
NEON_ARCH = /arch:VFPv3
!endif
!if "$(WITH_NEON)" != ""
CFLAGS = $(CFLAGS) $(NEON_ARCH)
WFLAGS = $(WFLAGS) \
-D__ARM_NEON__=1 \
-DARM_NEON \
-DARM_NOCHECK_NEON \
#
OBJS = $(OBJS) adler32_neon.obj chunkset_neon.obj compare256_neon.obj slide_hash_neon.obj
!endif
!if "$(WITH_ARMV6)" != ""
WFLAGS = $(WFLAGS) \
-DARM_SIMD \
-DARM_NOCHECK_SIMD \
#
OBJS = $(OBJS) slide_hash_armv6.obj
!endif
# targets
all: $(STATICLIB) $(SHAREDLIB) $(IMPLIB) \
example.exe minigzip.exe example_d.exe minigzip_d.exe
!if "$(SYMBOL_PREFIX)" != ""
zlib_name_mangling$(SUFFIX).h: zlib_name_mangling$(SUFFIX).h.in
cscript $(TOP)\win32\replace.vbs $(TOP)\zlib_name_mangling$(SUFFIX).h.in zlib_name_mangling$(SUFFIX).h "@ZLIB_SYMBOL_PREFIX@" "$(SYMBOL_PREFIX)"
!else
zlib_name_mangling$(SUFFIX).h: zlib_name_mangling.h.empty
$(CP) $(TOP)\zlib_name_mangling.h.empty zlib_name_mangling$(SUFFIX).h
!endif
zlib$(SUFFIX).h: zlib$(SUFFIX).h.in
cscript $(TOP)\win32\replace.vbs $(TOP)\zlib$(SUFFIX).h.in zlib$(SUFFIX).h "@ZLIB_SYMBOL_PREFIX@" "$(SYMBOL_PREFIX)"
gzread.c: gzread.c.in
cscript $(TOP)\win32\replace.vbs $(TOP)\gzread.c.in gzread.c "@ZLIB_SYMBOL_PREFIX@" "$(SYMBOL_PREFIX)"
zconf: $(TOP)/zconf$(SUFFIX).h.in $(TOP)/zlib$(SUFFIX).h $(TOP)/zlib_name_mangling$(SUFFIX).h
$(CP) $(TOP)\zconf$(SUFFIX).h.in $(TOP)\zconf$(SUFFIX).h
$(TOP)/win32/$(DEFFILE): $(TOP)/win32/$(DEFFILE).in
cscript $(TOP)\win32\replace.vbs $(TOP)/win32/$(DEFFILE).in $(TOP)/win32/$(DEFFILE) "@ZLIB_SYMBOL_PREFIX@" "$(SYMBOL_PREFIX)"
$(STATICLIB): zconf $(OBJS)
$(AR) $(ARFLAGS) -out:$@ $(OBJS)
$(IMPLIB): $(SHAREDLIB)
$(SHAREDLIB): zconf $(TOP)/win32/$(DEFFILE) $(OBJS) $(RESFILE)
$(LD) $(LDFLAGS) -def:$(TOP)/win32/$(DEFFILE) -dll -implib:$(IMPLIB) \
-out:$@ -base:0x5A4C0000 $(OBJS) $(RESFILE)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;2
example.exe: example.obj gzlib2.obj gzread2.obj gzwrite2.obj $(STATICLIB)
$(LD) $(LDFLAGS) example.obj gzlib2.obj gzread2.obj gzwrite2.obj $(STATICLIB)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;1
minigzip.exe: minigzip.obj gzlib2.obj gzread2.obj gzwrite2.obj $(STATICLIB)
$(LD) $(LDFLAGS) minigzip.obj gzlib2.obj gzread2.obj gzwrite2.obj $(STATICLIB)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;1
example_d.exe: example.obj gzlib2.obj gzread2.obj gzwrite2.obj $(IMPLIB)
$(LD) $(LDFLAGS) -out:$@ example.obj gzlib2.obj gzread2.obj gzwrite2.obj $(IMPLIB)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;1
minigzip_d.exe: minigzip.obj gzlib2.obj gzread2.obj gzwrite2.obj $(IMPLIB)
$(LD) $(LDFLAGS) -out:$@ minigzip.obj gzlib2.obj gzread2.obj gzwrite2.obj $(IMPLIB)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;1
{$(TOP)}.c.obj:
$(CC) -c $(WFLAGS) $(CFLAGS) $<
gzlib2.obj: gzlib.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
$(CC) -c $(WFLAGS) $(CFLAGS) -DWITH_GZFILEOP -Fogzlib2.obj gzlib.c
gzread2.obj: gzread.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
$(CC) -c $(WFLAGS) $(CFLAGS) -DWITH_GZFILEOP -Fogzread2.obj gzread.c
gzwrite2.obj: gzwrite.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
$(CC) -c $(WFLAGS) $(CFLAGS) -DWITH_GZFILEOP -Fogzwrite2.obj gzwrite.c
{$(TOP)/arch/arm}.c.obj:
$(CC) -c -I$(TOP) $(WFLAGS) $(CFLAGS) $<
{$(TOP)/arch/generic}.c.obj:
$(CC) -c -I$(TOP) $(WFLAGS) $(CFLAGS) $<
{$(TOP)/test}.c.obj:
$(CC) -c -I$(TOP) $(WFLAGS) $(CFLAGS) -DWITH_GZFILEOP $<
$(TOP)/zconf$(SUFFIX).h: zconf
adler32.obj: $(TOP)/adler32.c $(TOP)/zbuild.h $(TOP)/functable.h $(TOP)/adler32_p.h
adler32_c.obj: $(TOP)/arch/generic/adler32_c.c $(TOP)/zbuild.h $(TOP)/functable.h $(TOP)/adler32_p.h
adler32_fold_c.obj: $(TOP)/arch/generic/adler32_fold_c.c $(TOP)/zbuild.h $(TOP)/functable.h
chunkset_c.obj: $(TOP)/arch/generic/chunkset_c.c $(TOP)/zbuild.h $(TOP)/chunkset_tpl.h $(TOP)/inffast_tpl.h
compare256_c.obj: $(TOP)/arch/generic/compare256_c.c $(TOP)/zbuild.h $(TOP)/zmemory.h $(TOP)/deflate.h $(TOP)/fallback_builtins.h $(TOP)/match_tpl.h
compress.obj: $(TOP)/compress.c $(TOP)/zbuild.h $(TOP)/zutil.h
cpu_features.obj: $(TOP)/cpu_features.c $(TOP)/cpu_features.h $(TOP)/zbuild.h
crc32.obj: $(TOP)/crc32.c $(TOP)/zbuild.h $(TOP)/functable.h $(TOP)/crc32_braid_tbl.h
crc32_braid_c.obj: $(TOP)/arch/generic/crc32_braid_c.c $(TOP)/zbuild.h $(TOP)/crc32_braid_p.h $(TOP)/crc32_braid_tbl.h
crc32_braid_comb.obj: $(TOP)/crc32_braid_comb.c $(TOP)/zutil.h $(TOP)/crc32_braid_p.h $(TOP)/crc32_braid_tbl.h $(TOP)/crc32_braid_comb_p.h
crc32_c.obj: $(TOP)/arch/generic/crc32_c.c $(TOP)/zbuild.h $(TOP)/crc32_braid_p.h
crc32_chorba_c.obj: $(TOP)/arch/generic/crc32_chorba_c.c $(TOP)/zbuild.h $(TOP)/crc32_braid_p.h $(TOP)/crc32_braid_tbl.h
crc32_fold_c.obj: $(TOP)/arch/generic/crc32_fold_c.c $(TOP)/zbuild.h $(TOP)/crc32.h $(TOP)/functable.h $(TOP)/zutil.h
deflate.obj: $(TOP)/deflate.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_fast.obj: $(TOP)/deflate_fast.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_huff.obj: $(TOP)/deflate_huff.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_medium.obj: $(TOP)/deflate_medium.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_quick.obj: $(TOP)/deflate_quick.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h $(TOP)/trees_emit.h $(TOP)/zmemory.h
deflate_rle.obj: $(TOP)/deflate_rle.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h $(TOP)/compare256_rle.h
deflate_slow.obj: $(TOP)/deflate_slow.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_stored.obj: $(TOP)/deflate_stored.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
functable.obj: $(TOP)/functable.c $(TOP)/zbuild.h $(TOP)/functable.h $(TOP)/cpu_features.h $(TOP)/arch/arm/arm_features.h $(TOP)/arch_functions.h
gzlib.obj: $(TOP)/gzlib.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
gzread.obj: $(TOP)/gzread.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
gzwrite.obj: $(TOP)/gzwrite.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
infback.obj: $(TOP)/infback.c $(TOP)/zbuild.h $(TOP)/zutil.h $(TOP)/inftrees.h $(TOP)/inflate.h $(TOP)/inflate_p.h $(TOP)/functable.h
inflate.obj: $(TOP)/inflate.c $(TOP)/zbuild.h $(TOP)/zutil.h $(TOP)/inftrees.h $(TOP)/inflate.h $(TOP)/inflate_p.h $(TOP)/functable.h $(TOP)/inffixed_tbl.h
inftrees.obj: $(TOP)/inftrees.c $(TOP)/zbuild.h $(TOP)/zutil.h $(TOP)/inftrees.h
insert_string.obj: $(TOP)/insert_string.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/insert_string_tpl.h
insert_string_roll.obj: $(TOP)/insert_string_roll.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/insert_string_tpl.h
slide_hash_c.obj: $(TOP)/arch/generic/slide_hash_c.c $(TOP)/zbuild.h $(TOP)/deflate.h
trees.obj: $(TOP)/trees.c $(TOP)/trees.h $(TOP)/trees_emit.h $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/trees_tbl.h
uncompr.obj: $(TOP)/uncompr.c $(TOP)/zbuild.h $(TOP)/zutil.h
zutil.obj: $(TOP)/zutil.c $(TOP)/zbuild.h $(TOP)/zutil.h $(TOP)/zutil_p.h
$(RESFILE): $(TOP)/win32/$(RCFILE)
$(RC) $(RCFLAGS) /fo$@ $(TOP)/win32/$(RCFILE)
# testing
test: example.exe minigzip.exe
example
echo hello world | minigzip | minigzip -d
testdll: example_d.exe minigzip_d.exe
example_d
echo hello world | minigzip_d | minigzip_d -d
example.obj: $(TOP)/test/example.c $(TOP)/zbuild.h $(TOP)/zlib$(SUFFIX).h $(TOP)/deflate.h $(TOP)/test/test_shared_ng.h
minigzip.obj: $(TOP)/test/minigzip.c $(TOP)/zbuild.h $(TOP)/zlib$(SUFFIX).h
# cleanup
clean:
-del $(STATICLIB)
-del $(SHAREDLIB)
-del $(IMPLIB)
-del *.obj
-del *.res
-del *.exp
-del *.exe
-del *.pdb
-del *.manifest
distclean: clean
-del zconf$(SUFFIX).h
-del zlib$(SUFFIX).h
-del zlib_name_mangling$(SUFFIX).h
-del $(TOP)\win32\zlib.def
-del $(TOP)\win32\zlibcompat.def
-del $(TOP)\win32\zlib-ng.def
-del gzread.c
+296
View File
@@ -0,0 +1,296 @@
# Makefile for zlib using Microsoft (Visual) C
# zlib is copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
#
# Usage:
# nmake -f win32/Makefile.msc (standard build)
# nmake -f win32/Makefile.msc LOC=-DFOO (nonstandard build)
# The toplevel directory of the source tree.
#
TOP = .
# optional build flags
LOC =
# variables
STATICLIB = zlib.lib
SHAREDLIB = zlib1.dll
IMPLIB = zdll.lib
SYMBOL_PREFIX =
CC = cl
CXX = cl
LD = link
AR = lib
RC = rc
CP = copy /y
INCLUDES = -I$(TOP) -I$(TOP)/arch/x86 -I$(TOP)/arch/generic
CFLAGS = -nologo -MD -W3 -O2 -Oy- -Zi -Fd"zlib" $(LOC) $(INCLUDES)
CXXFLAGS = -nologo -EHsc -MD -W3 -O2 -Oy- -Zi -Fd"zlib" $(LOC) $(INCLUDES)
WFLAGS = \
-D_CRT_SECURE_NO_DEPRECATE \
-D_CRT_NONSTDC_NO_DEPRECATE \
-DX86_FEATURES \
-DX86_PCLMULQDQ_CRC \
-DX86_SSE2 \
-DX86_SSE42 \
-DX86_SSSE3 \
-DX86_AVX2
LDFLAGS = -nologo -debug -incremental:no -opt:ref -manifest
ARFLAGS = -nologo
RCFLAGS = /dWIN32 /r
DEFFILE = zlib.def
RCFILE = zlib1.rc
RESFILE = zlib1.res
WITH_GZFILEOP = yes
ZLIB_COMPAT =
SUFFIX =
OBJS = \
adler32.obj \
adler32_c.obj \
adler32_avx2.obj \
adler32_avx512.obj \
adler32_avx512_vnni.obj \
adler32_sse42.obj \
adler32_ssse3.obj \
adler32_fold_c.obj \
chunkset_c.obj \
chunkset_avx2.obj \
chunkset_sse2.obj \
chunkset_ssse3.obj \
compare256_c.obj \
compare256_avx2.obj \
compare256_sse2.obj \
compress.obj \
cpu_features.obj \
crc32.obj \
crc32_braid_c.obj \
crc32_braid_comb.obj \
crc32_c.obj \
crc32_chorba_c.obj \
crc32_fold_c.obj \
crc32_pclmulqdq.obj \
deflate.obj \
deflate_fast.obj \
deflate_huff.obj \
deflate_medium.obj \
deflate_quick.obj \
deflate_rle.obj \
deflate_slow.obj \
deflate_stored.obj \
functable.obj \
infback.obj \
inflate.obj \
inftrees.obj \
insert_string.obj \
insert_string_roll.obj \
slide_hash_c.obj \
slide_hash_avx2.obj \
slide_hash_sse2.obj \
trees.obj \
uncompr.obj \
zutil.obj \
x86_features.obj \
#
!if "$(ZLIB_COMPAT)" != ""
WITH_GZFILEOP = yes
WFLAGS = $(WFLAGS) -DZLIB_COMPAT
DEFFILE = zlibcompat.def
!else
STATICLIB = zlib-ng.lib
SHAREDLIB = zlib-ng1.dll
IMPLIB = zngdll.lib
DEFFILE = zlib-ng.def
RCFILE = zlib-ng1.rc
RESFILE = zlib-ng1.res
SUFFIX = -ng
!endif
!if "$(WITH_GZFILEOP)" != ""
WFLAGS = $(WFLAGS) -DWITH_GZFILEOP
OBJS = $(OBJS) gzlib.obj gzread.obj gzwrite.obj
!endif
# targets
all: $(STATICLIB) $(SHAREDLIB) $(IMPLIB) \
example.exe minigzip.exe example_d.exe minigzip_d.exe
!if "$(SYMBOL_PREFIX)" != ""
zlib_name_mangling$(SUFFIX).h: zlib_name_mangling$(SUFFIX).h.in
cscript $(TOP)\win32\replace.vbs $(TOP)\zlib_name_mangling$(SUFFIX).h.in zlib_name_mangling$(SUFFIX).h "@ZLIB_SYMBOL_PREFIX@" "$(SYMBOL_PREFIX)"
!else
zlib_name_mangling$(SUFFIX).h: zlib_name_mangling.h.empty
$(CP) $(TOP)\zlib_name_mangling.h.empty zlib_name_mangling$(SUFFIX).h
!endif
zlib$(SUFFIX).h: zlib$(SUFFIX).h.in
cscript $(TOP)\win32\replace.vbs $(TOP)\zlib$(SUFFIX).h.in zlib$(SUFFIX).h "@ZLIB_SYMBOL_PREFIX@" "$(SYMBOL_PREFIX)"
gzread.c: gzread.c.in
cscript $(TOP)\win32\replace.vbs $(TOP)\gzread.c.in gzread.c "@ZLIB_SYMBOL_PREFIX@" "$(SYMBOL_PREFIX)"
zconf: $(TOP)/zconf$(SUFFIX).h.in $(TOP)/zlib$(SUFFIX).h $(TOP)/zlib_name_mangling$(SUFFIX).h
$(CP) $(TOP)\zconf$(SUFFIX).h.in $(TOP)\zconf$(SUFFIX).h
$(TOP)/win32/$(DEFFILE): $(TOP)/win32/$(DEFFILE).in
cscript $(TOP)\win32\replace.vbs $(TOP)/win32/$(DEFFILE).in $(TOP)/win32/$(DEFFILE) "@ZLIB_SYMBOL_PREFIX@" "$(SYMBOL_PREFIX)"
$(STATICLIB): zconf $(OBJS)
$(AR) $(ARFLAGS) -out:$@ $(OBJS)
$(IMPLIB): $(SHAREDLIB)
$(SHAREDLIB): zconf $(TOP)/win32/$(DEFFILE) $(OBJS) $(RESFILE)
$(LD) $(LDFLAGS) -def:$(TOP)/win32/$(DEFFILE) -dll -implib:$(IMPLIB) \
-out:$@ $(OBJS) $(RESFILE)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;2
depcheck.exe: depcheck.obj
$(LD) $(LDFLAGS) depcheck.obj
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;1
example.exe: example.obj gzlib2.obj gzread2.obj gzwrite2.obj $(STATICLIB)
$(LD) $(LDFLAGS) example.obj gzlib2.obj gzread2.obj gzwrite2.obj $(STATICLIB)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;1
minigzip.exe: minigzip.obj gzlib2.obj gzread2.obj gzwrite2.obj $(STATICLIB)
$(LD) $(LDFLAGS) minigzip.obj gzlib2.obj gzread2.obj gzwrite2.obj $(STATICLIB)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;1
example_d.exe: example.obj gzlib2.obj gzread2.obj gzwrite2.obj $(IMPLIB)
$(LD) $(LDFLAGS) -out:$@ example.obj gzlib2.obj gzread2.obj gzwrite2.obj $(IMPLIB)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;1
minigzip_d.exe: minigzip.obj gzlib2.obj gzread2.obj gzwrite2.obj $(IMPLIB)
$(LD) $(LDFLAGS) -out:$@ minigzip.obj gzlib2.obj gzread2.obj gzwrite2.obj $(IMPLIB)
if exist $@.manifest \
mt -nologo -manifest $@.manifest -outputresource:$@;1
{$(TOP)}.c.obj:
$(CC) -c $(WFLAGS) $(CFLAGS) $<
gzlib2.obj: gzlib.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
$(CC) -c $(WFLAGS) $(CFLAGS) -DWITH_GZFILEOP -Fogzlib2.obj gzlib.c
gzread2.obj: gzread.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
$(CC) -c $(WFLAGS) $(CFLAGS) -DWITH_GZFILEOP -Fogzread2.obj gzread.c
gzwrite2.obj: gzwrite.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
$(CC) -c $(WFLAGS) $(CFLAGS) -DWITH_GZFILEOP -Fogzwrite2.obj gzwrite.c
{$(TOP)/arch/x86}.c.obj:
$(CC) -c -I$(TOP) $(WFLAGS) $(CFLAGS) $<
{$(TOP)/arch/generic}.c.obj:
$(CC) -c -I$(TOP) $(WFLAGS) $(CFLAGS) $<
{$(TOP)/test}.c.obj:
$(CC) -c -I$(TOP) $(WFLAGS) $(CFLAGS) -DWITH_GZFILEOP $<
$(TOP)/zconf$(SUFFIX).h: zconf
{$(TOP)/win32}.cpp.obj:
$(CXX) -c -I$(TOP) $(WFLAGS) $(CXXFLAGS) $<
adler32.obj: $(TOP)/adler32.c $(TOP)/zbuild.h $(TOP)/functable.h $(TOP)/adler32_p.h
adler32_c.obj: $(TOP)/arch/generic/adler32_c.c $(TOP)/zbuild.h $(TOP)/functable.h $(TOP)/adler32_p.h
adler32_avx2.obj: $(TOP)/arch/x86/adler32_avx2.c $(TOP)/zbuild.h $(TOP)/adler32_p.h $(TOP)/arch/x86/adler32_avx2_p.h $(TOP)/arch/x86/x86_intrins.h
adler32_avx512.obj: $(TOP)/arch/x86/adler32_avx512.c $(TOP)/zbuild.h $(TOP)/arch_functions.h $(TOP)/adler32_p.h $(TOP)/arch/x86/adler32_avx512_p.h $(TOP)/arch/x86/x86_intrins.h
adler32_avx512_vnni.obj: $(TOP)/arch/x86/adler32_avx512_vnni.c $(TOP)/zbuild.h $(TOP)/arch_functions.h $(TOP)/adler32_p.h $(TOP)/arch/x86/adler32_avx512_p.h \
$(TOP)/arch/x86/adler32_avx2_p.h $(TOP)/arch/x86/x86_intrins.h
adler32_sse42.obj: $(TOP)/arch/x86/adler32_sse42.c $(TOP)/zbuild.h $(TOP)/adler32_p.h \
$(TOP)/arch/x86/adler32_ssse3_p.h
adler32_ssse3.obj: $(TOP)/arch/x86/adler32_ssse3.c $(TOP)/zbuild.h $(TOP)/adler32_p.h \
$(TOP)/arch/x86/adler32_ssse3_p.h
adler32_fold_c.obj: $(TOP)/arch/generic/adler32_fold_c.c $(TOP)/zbuild.h $(TOP)/functable.h
chunkset_c.obj: $(TOP)/arch/generic/chunkset_c.c $(TOP)/zbuild.h $(TOP)/chunkset_tpl.h $(TOP)/inffast_tpl.h
chunkset_avx2.obj: $(TOP)/arch/x86/chunkset_avx2.c $(TOP)/zbuild.h $(TOP)/chunkset_tpl.h $(TOP)/inffast_tpl.h $(TOP)/arch/generic/chunk_permute_table.h
chunkset_sse2.obj: $(TOP)/arch/x86/chunkset_sse2.c $(TOP)/zbuild.h $(TOP)/chunkset_tpl.h $(TOP)/inffast_tpl.h
chunkset_ssse3.obj: $(TOP)/arch/x86/chunkset_ssse3.c $(TOP)/zbuild.h $(TOP)/chunkset_tpl.h $(TOP)/inffast_tpl.h $(TOP)/arch/generic/chunk_permute_table.h
compare256_c.obj: $(TOP)/arch/generic/compare256_c.c $(TOP)/zbuild.h $(TOP)/zmemory.h $(TOP)/deflate.h $(TOP)/fallback_builtins.h $(TOP)/match_tpl.h
compare256_avx2.obj: $(TOP)/arch/x86/compare256_avx2.c $(TOP)/zbuild.h $(TOP)/zmemory.h $(TOP)/deflate.h $(TOP)/fallback_builtins.h $(TOP)/match_tpl.h
compare256_sse2.obj: $(TOP)/arch/x86/compare256_sse2.c $(TOP)/zbuild.h $(TOP)/zmemory.h $(TOP)/deflate.h $(TOP)/fallback_builtins.h $(TOP)/match_tpl.h
compress.obj: $(TOP)/compress.c $(TOP)/zbuild.h $(TOP)/zutil.h
cpu_features.obj: $(TOP)/cpu_features.c $(TOP)/cpu_features.h $(TOP)/zbuild.h
crc32.obj: $(TOP)/crc32.c $(TOP)/zbuild.h $(TOP)/functable.h $(TOP)/crc32_braid_tbl.h
crc32_braid_c.obj: $(TOP)/arch/generic/crc32_braid_c.c $(TOP)/zbuild.h $(TOP)/crc32_braid_p.h $(TOP)/crc32_braid_tbl.h
crc32_braid_comb.obj: $(TOP)/crc32_braid_comb.c $(TOP)/zutil.h $(TOP)/crc32_braid_p.h $(TOP)/crc32_braid_tbl.h $(TOP)/crc32_braid_comb_p.h
crc32_c.obj: $(TOP)/arch/generic/crc32_c.c $(TOP)/zbuild.h $(TOP)/crc32_braid_p.h
crc32_chorba_c.obj: $(TOP)/arch/generic/crc32_chorba_c.c $(TOP)/zbuild.h $(TOP)/crc32_braid_p.h $(TOP)/crc32_braid_tbl.h
crc32_fold_c.obj: $(TOP)/arch/generic/crc32_fold_c.c $(TOP)/zbuild.h $(TOP)/crc32.h $(TOP)/functable.h $(TOP)/zutil.h
crc32_pclmulqdq.obj: $(TOP)/arch/x86/crc32_pclmulqdq.c $(TOP)/arch/x86/crc32_pclmulqdq_tpl.h
deflate.obj: $(TOP)/deflate.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_fast.obj: $(TOP)/deflate_fast.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_huff.obj: $(TOP)/deflate_huff.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_medium.obj: $(TOP)/deflate_medium.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_quick.obj: $(TOP)/deflate_quick.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h $(TOP)/trees_emit.h $(TOP)/zmemory.h
deflate_rle.obj: $(TOP)/deflate_rle.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h $(TOP)/compare256_rle.h
deflate_slow.obj: $(TOP)/deflate_slow.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
deflate_stored.obj: $(TOP)/deflate_stored.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/deflate_p.h $(TOP)/functable.h
functable.obj: $(TOP)/functable.c $(TOP)/zbuild.h $(TOP)/functable.h $(TOP)/cpu_features.h $(TOP)/arch/x86/x86_features.h $(TOP)/arch_functions.h
gzlib.obj: $(TOP)/gzlib.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
gzread.obj: $(TOP)/gzread.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
gzwrite.obj: $(TOP)/gzwrite.c $(TOP)/zbuild.h $(TOP)/gzguts.h $(TOP)/zutil_p.h
infback.obj: $(TOP)/infback.c $(TOP)/zbuild.h $(TOP)/zutil.h $(TOP)/inftrees.h $(TOP)/inflate.h $(TOP)/inflate_p.h $(TOP)/functable.h
inflate.obj: $(TOP)/inflate.c $(TOP)/zbuild.h $(TOP)/zutil.h $(TOP)/inftrees.h $(TOP)/inflate.h $(TOP)/inflate_p.h $(TOP)/functable.h $(TOP)/inffixed_tbl.h
inftrees.obj: $(TOP)/inftrees.c $(TOP)/zbuild.h $(TOP)/zutil.h $(TOP)/inftrees.h
insert_string.obj: $(TOP)/insert_string.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/insert_string_tpl.h
insert_string_roll.obj: $(TOP)/insert_string_roll.c $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/insert_string_tpl.h
slide_hash_c.obj: $(TOP)/arch/generic/slide_hash_c.c $(TOP)/zbuild.h $(TOP)/deflate.h
slide_hash_avx2.obj: $(TOP)/arch/x86/slide_hash_avx2.c $(TOP)/zbuild.h $(TOP)/deflate.h
slide_hash_sse2.obj: $(TOP)/arch/x86/slide_hash_sse2.c $(TOP)/zbuild.h $(TOP)/deflate.h
trees.obj: $(TOP)/trees.c $(TOP)/trees.h $(TOP)/trees_emit.h $(TOP)/zbuild.h $(TOP)/deflate.h $(TOP)/trees_tbl.h
uncompr.obj: $(TOP)/uncompr.c $(TOP)/zbuild.h $(TOP)/zutil.h
zutil.obj: $(TOP)/zutil.c $(TOP)/zbuild.h $(TOP)/zutil.h $(TOP)/zutil_p.h
$(RESFILE): $(TOP)/win32/$(RCFILE)
$(RC) $(RCFLAGS) /fo$@ $(TOP)/win32/$(RCFILE)
# testing
depcheck: depcheck.exe
depcheck win32\Makefile.msc .
depcheck win32\Makefile.arm .
depcheck win32\Makefile.a64 .
test: example.exe minigzip.exe depcheck
example
echo hello world | minigzip | minigzip -d
testdll: example_d.exe minigzip_d.exe
example_d
echo hello world | minigzip_d | minigzip_d -d
depcheck.obj: $(TOP)/win32/depcheck.cpp
example.obj: $(TOP)/test/example.c $(TOP)/zbuild.h $(TOP)/zlib$(SUFFIX).h $(TOP)/deflate.h $(TOP)/test/test_shared_ng.h
minigzip.obj: $(TOP)/test/minigzip.c $(TOP)/zbuild.h $(TOP)/zlib$(SUFFIX).h
# cleanup
clean:
-del $(STATICLIB)
-del $(SHAREDLIB)
-del $(IMPLIB)
-del *.obj
-del *.res
-del *.exp
-del *.exe
-del *.pdb
-del *.manifest
distclean: clean
-del zconf$(SUFFIX).h
-del zlib$(SUFFIX).h
-del zlib_name_mangling$(SUFFIX).h
-del $(TOP)\win32\zlib.def
-del $(TOP)\win32\zlibcompat.def
-del $(TOP)\win32\zlib-ng.def
-del gzread.c
+321
View File
@@ -0,0 +1,321 @@
/* depcheck.cpp - Dependency checker for NMake Makefiles
* Copyright (c) 2024 Mika T. Lindqvist
*/
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
int main(int argc, char* argv[]) {
if (argc != 3) {
printf("Usage: depcheck Makefile <top_directory>\n");
return -1;
}
std::filebuf fb;
if (fb.open (argv[1],std::ios::in)) {
std::istream is(&fb);
std::string makefile = argv[1];
std::string l, tmp, tmp2;
while (is) {
std::getline(is, l);
while (l.back() == '\\') {
std::getline(is, tmp);
l.replace(l.length() - 1, 1, tmp);
}
size_t pos = l.find("obj:");
if (pos != std::string::npos) {
std::string objfile = l.substr(0, pos+3);
printf("File: %s\n", objfile.c_str());
std::vector<std::string> files;
std::stringstream ss(l.substr(pos+4));
while(getline(ss, tmp, ' ')){
if (tmp != "" && tmp != "/") {
files.push_back(tmp);
}
}
for (auto it = files.begin(); it != files.end(); ++it) {
printf("Dependency: %s\n", (*it).c_str());
}
if (!files.empty()) {
std::filebuf fb2;
std::string src = files[0];
size_t pos2 = src.find("$(TOP)");
if (pos2 != std::string::npos) {
src.replace(pos2, 6, argv[2]);
}
printf("Source: %s\n", src.c_str());
if (fb2.open(src.c_str(),std::ios::in)) {
std::istream is2(&fb2);
std::vector<std::string> includes;
while (is2) {
std::getline(is2, l);
pos = l.find("#");
if (pos != std::string::npos) {
pos2 = l.find("include");
size_t pos3 = l.find("\"");
if (pos2 != std::string::npos && pos3 != std::string::npos && pos2 > pos && pos3 > pos2) {
tmp = l.substr(pos3 + 1);
pos2 = tmp.find("\"");
if (pos2 != std::string::npos) {
tmp = tmp.substr(0, pos2);
}
pos2 = tmp.find("../");
if (pos2 != std::string::npos) {
tmp = tmp.substr(3);
}
printf("Line: %s\n", tmp.c_str());
int found = 0;
for (size_t i = 1; i < files.size(); i++) {
pos3 = files[i].find("$(SUFFIX)");
if (pos3 != std::string::npos) {
tmp2 = files[i].substr(0, pos3).append(files[i].substr(pos3 + 9));
printf("Comparing dependency \"%s\" and \"%s\"\n", tmp2.c_str(), tmp.c_str());
if (tmp2 == tmp) {
printf("Dependency %s OK\n", tmp.c_str());
found = 1;
includes.push_back(tmp);
break;
}
printf("Comparing dependency \"%s\" and \"$(TOP)/%s\"\n", tmp2.c_str(), tmp.c_str());
if (tmp2 == std::string("$(TOP)/").append(tmp)) {
printf("Dependency %s OK\n", tmp.c_str());
found = 1;
includes.push_back(tmp);
break;
}
tmp2 = files[i].substr(0, pos3).append("-ng").append(files[i].substr(pos3 + 9));
printf("Comparing dependency \"%s\" and \"%s\"\n", tmp2.c_str(), tmp.c_str());
if (tmp2 == tmp) {
printf("Dependency %s OK\n", tmp.c_str());
found = 1;
includes.push_back(tmp);
break;
}
printf("Comparing dependency \"%s\" and \"$(TOP)/%s\"\n", tmp2.c_str(), tmp.c_str());
if (tmp2 == std::string("$(TOP)/").append(tmp)) {
printf("Dependency %s OK\n", tmp.c_str());
found = 1;
includes.push_back(tmp);
break;
}
} else {
printf("Comparing dependency \"%s\" and \"%s\"\n", files[i].c_str(), tmp.c_str());
if (files[i] == tmp) {
printf("Dependency %s OK\n", tmp.c_str());
found = 1;
includes.push_back(tmp);
break;
}
printf("Comparing dependency \"%s\" and \"$(TOP)/%s\"\n", files[i].c_str(), tmp.c_str());
if (files[i] == std::string("$(TOP)/").append(tmp)) {
printf("Dependency %s OK\n", tmp.c_str());
found = 1;
includes.push_back(tmp);
break;
}
printf("Comparing dependency \"%s\" and \"$(TOP)/arch/%s\"\n", files[i].c_str(), tmp.c_str());
if (files[i] == std::string("$(TOP)/arch/").append(tmp)) {
printf("Dependency %s OK\n", tmp.c_str());
found = 1;
includes.push_back(tmp);
break;
}
printf("Comparing dependency \"%s\" and \"$(TOP)/arch/generic/%s\"\n", files[i].c_str(), tmp.c_str());
if (files[i] == std::string("$(TOP)/arch/generic/").append(tmp)) {
printf("Dependency %s OK\n", tmp.c_str());
found = 1;
includes.push_back(tmp);
break;
}
printf("Comparing dependency \"%s\" and \"$(TOP)/arch/arm/%s\"\n", files[i].c_str(), tmp.c_str());
if (files[i] == std::string("$(TOP)/arch/arm/").append(tmp)) {
printf("Dependency %s OK\n", tmp.c_str());
found = 1;
includes.push_back(tmp);
break;
}
printf("Comparing dependency \"%s\" and \"$(TOP)/arch/x86/%s\"\n", files[i].c_str(), tmp.c_str());
if (files[i] == std::string("$(TOP)/arch/x86/").append(tmp)) {
printf("Dependency %s OK\n", tmp.c_str());
found = 1;
includes.push_back(tmp);
break;
}
printf("Comparing dependency \"%s\" and \"$(TOP)/test/%s\"\n", files[i].c_str(), tmp.c_str());
if (files[i] == std::string("$(TOP)/test/").append(tmp)) {
printf("Dependency %s OK\n", tmp.c_str());
found = 1;
includes.push_back(tmp);
break;
}
}
}
// Skip irrelevant dependencies
if (tmp.substr(0, 9) == "arch/s390") found = 1;
if (tmp == "zlib-ng.h" && std::find(includes.begin(), includes.end(), "zlib.h") != includes.end()) found = 1;
if (found == 0) {
printf("%s: Dependency %s missing for %s!\n", makefile.c_str(), tmp.c_str(), objfile.c_str());
return -1;
}
}
}
}
for (size_t i = 1; i < files.size(); i++) {
int found = 0;
tmp = files[i];
printf("Dependency: %s\n", tmp.c_str());
pos2 = tmp.find("$(TOP)");
if (pos2 != std::string::npos) {
tmp = tmp.substr(7);
}
for (size_t j = 0; j < includes.size(); j++) {
pos2 = tmp.find("$(SUFFIX)");
if (pos2 != std::string::npos) {
std::string tmp1 = tmp.substr(0, pos2).append(tmp.substr(pos2 + 9));
printf("[%zd/%zd] Comparing dependency \"%s\" and \"%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
if (tmp1 == includes[j]) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
if (tmp1 == std::string("arch/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/generic/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
if (tmp1 == std::string("arch/generic/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/arm/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
if (tmp1 == std::string("arch/arm/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/x86/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
if (tmp1 == std::string("arch/x86/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"test/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
if (tmp1 == std::string("test/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
tmp1 = tmp.substr(0, pos2).append("-ng").append(tmp.substr(pos2 + 9));
printf("[%zd/%zd] Comparing dependency \"%s\" and \"%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
if (tmp1 == includes[j]) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
if (tmp1 == std::string("arch/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/generic/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
if (tmp1 == std::string("arch/generic/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/arm/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
if (tmp1 == std::string("arch/arm/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/x86/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
if (tmp1 == std::string("arch/x86/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"test/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
if (tmp1 == std::string("test/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
} else {
printf("[%zd/%zd] Comparing dependency \"%s\" and \"%s\"\n", j, includes.size(), tmp.c_str(), includes[j].c_str());
if (tmp == includes[j]) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/%s\"\n", j, includes.size(), tmp.c_str(), includes[j].c_str());
if (tmp == std::string("arch/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/generic/%s\"\n", j, includes.size(), tmp.c_str(), includes[j].c_str());
if (tmp == std::string("arch/generic/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/arm/%s\"\n", j, includes.size(), tmp.c_str(), includes[j].c_str());
if (tmp == std::string("arch/arm/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/x86/%s\"\n", j, includes.size(), tmp.c_str(), includes[j].c_str());
if (tmp == std::string("arch/x86/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
printf("[%zd/%zd] Comparing dependency \"%s\" and \"test/%s\"\n", j, includes.size(), tmp.c_str(), includes[j].c_str());
if (tmp == std::string("test/").append(includes[j])) {
printf("Dependency %s OK\n", files[i].c_str());
found = 1;
break;
}
}
}
// Skip indirect dependencies
if (tmp.find("arm_features.h") != std::string::npos
&& std::find(includes.begin(), includes.end(), "cpu_features.h") != includes.end()
&& (makefile.find(".arm") != std::string::npos
|| makefile.find(".a64") != std::string::npos)) found = 1;
if (tmp.find("x86_features.h") != std::string::npos
&& std::find(includes.begin(), includes.end(), "cpu_features.h") != includes.end()
&& makefile.find(".msc") != std::string::npos) found = 1;
//
if (tmp.find("generic_functions.h") != std::string::npos
&& std::find(includes.begin(), includes.end(), "arch_functions.h") != includes.end()) found = 1;
if (tmp.find("arm_functions.h") != std::string::npos
&& std::find(includes.begin(), includes.end(), "arch_functions.h") != includes.end()
&& (makefile.find(".arm") != std::string::npos
|| makefile.find(".a64") != std::string::npos)) found = 1;
if (tmp.find("x86_functions.h") != std::string::npos
&& std::find(includes.begin(), includes.end(), "arch_functions.h") != includes.end()
&& makefile.find(".msc") != std::string::npos) found = 1;
if (found == 0) {
printf("%s: Dependency %s not needed for %s\n", makefile.c_str(), files[i].c_str(), objfile.c_str());
return -1;
}
}
fb2.close();
}
}
}
}
fb.close();
}
return 0;
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+15
View File
@@ -0,0 +1,15 @@
strInputFileName = Wscript.Arguments(0)
strOutputFileName = Wscript.Arguments(1)
strOldText = Wscript.Arguments(2)
strNewText = Wscript.Arguments(3)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strInputFileName, 1)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strOutputFileName, 2, True)
objFile.Write strNewText
objFile.Close
+206
View File
@@ -0,0 +1,206 @@
/* zconf.h -- configuration of the zlib compression library
* Copyright (C) 1995-2024 Jean-loup Gailly, Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef ZCONF_H
#define ZCONF_H
#include "zlib_name_mangling.h"
#if !defined(_WIN32) && defined(__WIN32__)
# define _WIN32
#endif
/* Clang macro for detecting declspec support
* https://clang.llvm.org/docs/LanguageExtensions.html#has-declspec-attribute
*/
#ifndef __has_declspec_attribute
# define __has_declspec_attribute(x) 0
#endif
#if defined(ZLIB_CONST) && !defined(z_const)
# define z_const const
#else
# define z_const
#endif
/* Maximum value for memLevel in deflateInit2 */
#ifndef MAX_MEM_LEVEL
# define MAX_MEM_LEVEL 9
#endif
/* Maximum value for windowBits in deflateInit2 and inflateInit2.
* WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
* created by gzip. (Files created by minigzip can still be extracted by
* gzip.)
*/
#ifndef MIN_WBITS
# define MIN_WBITS 8 /* 256 LZ77 window */
#endif
#ifndef MAX_WBITS
# define MAX_WBITS 15 /* 32K LZ77 window */
#endif
/* The memory requirements for deflate are (in bytes):
(1 << (windowBits+2)) + (1 << (memLevel+9))
that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
plus a few kilobytes for small objects. For example, if you want to reduce
the default memory requirements from 256K to 128K, compile with
make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
Of course this will generally degrade compression (there's no free lunch).
The memory requirements for inflate are (in bytes) 1 << windowBits
that is, 32K for windowBits=15 (default value) plus about 7 kilobytes
for small objects.
*/
/* Type declarations */
#ifndef OF /* function prototypes */
# define OF(args) args
#endif
#ifdef ZLIB_INTERNAL
# define Z_INTERNAL ZLIB_INTERNAL
#endif
/* If building or using zlib as a DLL, define ZLIB_DLL.
* This is not mandatory, but it offers a little performance increase.
*/
#if defined(ZLIB_DLL) && (defined(_WIN32) || (__has_declspec_attribute(dllexport) && __has_declspec_attribute(dllimport)))
# ifdef Z_INTERNAL
# define Z_EXTERN extern __declspec(dllexport)
# else
# define Z_EXTERN extern __declspec(dllimport)
# endif
#endif
/* If building or using zlib with the WINAPI/WINAPIV calling convention,
* define ZLIB_WINAPI.
* Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
*/
#if defined(ZLIB_WINAPI) && defined(_WIN32)
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
/* No need for _export, use ZLIB.DEF instead. */
/* For complete Windows compatibility, use WINAPI, not __stdcall. */
# define Z_EXPORT WINAPI
# define Z_EXPORTVA WINAPIV
#endif
#ifndef Z_EXTERN
# define Z_EXTERN extern
#endif
#ifndef Z_EXPORT
# define Z_EXPORT
#endif
#ifndef Z_EXPORTVA
# define Z_EXPORTVA
#endif
/* Conditional exports */
#define ZNG_CONDEXPORT Z_INTERNAL
/* For backwards compatibility */
#ifndef ZEXTERN
# define ZEXTERN Z_EXTERN
#endif
#ifndef ZEXPORT
# define ZEXPORT Z_EXPORT
#endif
#ifndef ZEXPORTVA
# define ZEXPORTVA Z_EXPORTVA
#endif
#ifndef FAR
# define FAR
#endif
/* Legacy zlib typedefs for backwards compatibility. Don't assume stdint.h is defined. */
typedef unsigned char Byte;
typedef Byte Bytef;
typedef unsigned int uInt; /* 16 bits or more */
typedef unsigned long uLong; /* 32 bits or more */
typedef char charf;
typedef int intf;
typedef uInt uIntf;
typedef uLong uLongf;
typedef void const *voidpc;
typedef void *voidpf;
typedef void *voidp;
typedef unsigned int z_crc_t;
#if 1 /* was set to #if 1 by configure/cmake/etc */
# define Z_HAVE_UNISTD_H
#endif
#ifdef NEED_PTRDIFF_T /* may be set to #if 1 by configure/cmake/etc */
typedef PTRDIFF_TYPE ptrdiff_t;
#endif
#include <sys/types.h> /* for off_t */
#include <stddef.h> /* for wchar_t and NULL */
/* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and
* "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even
* though the former does not conform to the LFS document), but considering
* both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as
* equivalently requesting no 64-bit operations
*/
#if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1
# undef _LARGEFILE64_SOURCE
#endif
#if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE)
# include <unistd.h> /* for SEEK_*, off_t, and _LFS64_LARGEFILE */
# ifndef z_off_t
# define z_off_t off_t
# endif
#endif
#if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0
# define Z_LFS64
#endif
#if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64)
# define Z_LARGE64
#endif
#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS-0 == 64 && defined(Z_LFS64)
# define Z_WANT64
#endif
#if !defined(SEEK_SET)
# define SEEK_SET 0 /* Seek from beginning of file. */
# define SEEK_CUR 1 /* Seek from current position. */
# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
#endif
#ifndef z_off_t
# define z_off_t long
#endif
#if !defined(_WIN32) && defined(Z_LARGE64)
# define z_off64_t off64_t
#else
# if defined(__MSYS__)
# define z_off64_t _off64_t
# elif defined(_WIN32) && !defined(__GNUC__)
# define z_off64_t __int64
# else
# define z_off64_t z_off_t
# endif
#endif
typedef size_t z_size_t;
#endif /* ZCONF_H */
+60
View File
@@ -0,0 +1,60 @@
; zlib-ng data compression library
EXPORTS
; basic functions
@ZLIB_SYMBOL_PREFIX@zlibng_version
@ZLIB_SYMBOL_PREFIX@zng_deflate
@ZLIB_SYMBOL_PREFIX@zng_deflateEnd
@ZLIB_SYMBOL_PREFIX@zng_deflateInit
@ZLIB_SYMBOL_PREFIX@zng_deflateInit2
@ZLIB_SYMBOL_PREFIX@zng_inflate
@ZLIB_SYMBOL_PREFIX@zng_inflateEnd
@ZLIB_SYMBOL_PREFIX@zng_inflateInit
@ZLIB_SYMBOL_PREFIX@zng_inflateInit2
@ZLIB_SYMBOL_PREFIX@zng_inflateBackInit
; advanced functions
@ZLIB_SYMBOL_PREFIX@zng_deflateSetDictionary
@ZLIB_SYMBOL_PREFIX@zng_deflateGetDictionary
@ZLIB_SYMBOL_PREFIX@zng_deflateCopy
@ZLIB_SYMBOL_PREFIX@zng_deflateReset
@ZLIB_SYMBOL_PREFIX@zng_deflateParams
@ZLIB_SYMBOL_PREFIX@zng_deflateTune
@ZLIB_SYMBOL_PREFIX@zng_deflateBound
@ZLIB_SYMBOL_PREFIX@zng_deflatePending
@ZLIB_SYMBOL_PREFIX@zng_deflatePrime
@ZLIB_SYMBOL_PREFIX@zng_deflateSetHeader
@ZLIB_SYMBOL_PREFIX@zng_deflateSetParams
@ZLIB_SYMBOL_PREFIX@zng_deflateGetParams
@ZLIB_SYMBOL_PREFIX@zng_inflateSetDictionary
@ZLIB_SYMBOL_PREFIX@zng_inflateGetDictionary
@ZLIB_SYMBOL_PREFIX@zng_inflateSync
@ZLIB_SYMBOL_PREFIX@zng_inflateCopy
@ZLIB_SYMBOL_PREFIX@zng_inflateReset
@ZLIB_SYMBOL_PREFIX@zng_inflateReset2
@ZLIB_SYMBOL_PREFIX@zng_inflatePrime
@ZLIB_SYMBOL_PREFIX@zng_inflateMark
@ZLIB_SYMBOL_PREFIX@zng_inflateGetHeader
@ZLIB_SYMBOL_PREFIX@zng_inflateBack
@ZLIB_SYMBOL_PREFIX@zng_inflateBackEnd
@ZLIB_SYMBOL_PREFIX@zng_zlibCompileFlags
; utility functions
@ZLIB_SYMBOL_PREFIX@zng_compress
@ZLIB_SYMBOL_PREFIX@zng_compress2
@ZLIB_SYMBOL_PREFIX@zng_compressBound
@ZLIB_SYMBOL_PREFIX@zng_uncompress
@ZLIB_SYMBOL_PREFIX@zng_uncompress2
; checksum functions
@ZLIB_SYMBOL_PREFIX@zng_adler32
@ZLIB_SYMBOL_PREFIX@zng_adler32_z
@ZLIB_SYMBOL_PREFIX@zng_crc32
@ZLIB_SYMBOL_PREFIX@zng_crc32_z
@ZLIB_SYMBOL_PREFIX@zng_adler32_combine
@ZLIB_SYMBOL_PREFIX@zng_crc32_combine
; various hacks, don't look :)
@ZLIB_SYMBOL_PREFIX@zng_zError
@ZLIB_SYMBOL_PREFIX@zng_inflateSyncPoint
@ZLIB_SYMBOL_PREFIX@zng_get_crc_table
@ZLIB_SYMBOL_PREFIX@zng_inflateUndermine
@ZLIB_SYMBOL_PREFIX@zng_inflateValidate
@ZLIB_SYMBOL_PREFIX@zng_inflateCodesUsed
@ZLIB_SYMBOL_PREFIX@zng_inflateResetKeep
@ZLIB_SYMBOL_PREFIX@zng_deflateResetKeep
+36
View File
@@ -0,0 +1,36 @@
#include <winver.h>
#include "zlib-ng.h"
VS_VERSION_INFO VERSIONINFO
FILEVERSION ZLIBNG_VER_MAJOR,ZLIBNG_VER_MINOR,ZLIBNG_VER_REVISION,0
PRODUCTVERSION ZLIBNG_VER_MAJOR,ZLIBNG_VER_MINOR,ZLIBNG_VER_REVISION,0
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
#ifdef _DEBUG
FILEFLAGS 1
#else
FILEFLAGS 0
#endif
FILEOS VOS__WINDOWS32
FILETYPE VFT_DLL
FILESUBTYPE 0 // not used
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904E4"
//language ID = U.S. English, char set = Windows, Multilingual
BEGIN
VALUE "FileDescription", "zlib data compression library\0"
VALUE "FileVersion", ZLIBNG_VERSION "\0"
VALUE "InternalName", "zlib-ng1.dll\0"
VALUE "LegalCopyright", "(C) 1995-2024 Jean-loup Gailly & Mark Adler\0"
VALUE "OriginalFilename", "zlib-ng1.dll\0"
VALUE "ProductName", "zlib\0"
VALUE "ProductVersion", ZLIBNG_VERSION "\0"
VALUE "Comments", "For more information visit https://www.zlib.net/\0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x0409, 1252
END
END
+64
View File
@@ -0,0 +1,64 @@
; zlib data compression library
EXPORTS
; basic functions
@ZLIB_SYMBOL_PREFIX@zlibVersion
@ZLIB_SYMBOL_PREFIX@deflate
@ZLIB_SYMBOL_PREFIX@deflateEnd
@ZLIB_SYMBOL_PREFIX@inflate
@ZLIB_SYMBOL_PREFIX@inflateEnd
; advanced functions
@ZLIB_SYMBOL_PREFIX@deflateSetDictionary
@ZLIB_SYMBOL_PREFIX@deflateGetDictionary
@ZLIB_SYMBOL_PREFIX@deflateCopy
@ZLIB_SYMBOL_PREFIX@deflateReset
@ZLIB_SYMBOL_PREFIX@deflateParams
@ZLIB_SYMBOL_PREFIX@deflateTune
@ZLIB_SYMBOL_PREFIX@deflateBound
@ZLIB_SYMBOL_PREFIX@deflatePending
@ZLIB_SYMBOL_PREFIX@deflatePrime
@ZLIB_SYMBOL_PREFIX@deflateSetHeader
@ZLIB_SYMBOL_PREFIX@inflateSetDictionary
@ZLIB_SYMBOL_PREFIX@inflateGetDictionary
@ZLIB_SYMBOL_PREFIX@inflateSync
@ZLIB_SYMBOL_PREFIX@inflateCopy
@ZLIB_SYMBOL_PREFIX@inflateReset
@ZLIB_SYMBOL_PREFIX@inflateReset2
@ZLIB_SYMBOL_PREFIX@inflatePrime
@ZLIB_SYMBOL_PREFIX@inflateMark
@ZLIB_SYMBOL_PREFIX@inflateGetHeader
@ZLIB_SYMBOL_PREFIX@inflateBack
@ZLIB_SYMBOL_PREFIX@inflateBackEnd
@ZLIB_SYMBOL_PREFIX@zlibCompileFlags
; utility functions
@ZLIB_SYMBOL_PREFIX@compress
@ZLIB_SYMBOL_PREFIX@compress2
@ZLIB_SYMBOL_PREFIX@compressBound
@ZLIB_SYMBOL_PREFIX@uncompress
@ZLIB_SYMBOL_PREFIX@uncompress2
; large file functions
@ZLIB_SYMBOL_PREFIX@adler32_combine64
@ZLIB_SYMBOL_PREFIX@crc32_combine64
@ZLIB_SYMBOL_PREFIX@crc32_combine_gen64
; checksum functions
@ZLIB_SYMBOL_PREFIX@adler32
@ZLIB_SYMBOL_PREFIX@adler32_z
@ZLIB_SYMBOL_PREFIX@crc32
@ZLIB_SYMBOL_PREFIX@crc32_z
@ZLIB_SYMBOL_PREFIX@adler32_combine
@ZLIB_SYMBOL_PREFIX@crc32_combine
@ZLIB_SYMBOL_PREFIX@crc32_combine_gen
@ZLIB_SYMBOL_PREFIX@crc32_combine_op
; various hacks, don't look :)
@ZLIB_SYMBOL_PREFIX@deflateInit_
@ZLIB_SYMBOL_PREFIX@deflateInit2_
@ZLIB_SYMBOL_PREFIX@inflateInit_
@ZLIB_SYMBOL_PREFIX@inflateInit2_
@ZLIB_SYMBOL_PREFIX@inflateBackInit_
@ZLIB_SYMBOL_PREFIX@zError
@ZLIB_SYMBOL_PREFIX@inflateSyncPoint
@ZLIB_SYMBOL_PREFIX@get_crc_table
@ZLIB_SYMBOL_PREFIX@inflateUndermine
@ZLIB_SYMBOL_PREFIX@inflateValidate
@ZLIB_SYMBOL_PREFIX@inflateCodesUsed
@ZLIB_SYMBOL_PREFIX@inflateResetKeep
@ZLIB_SYMBOL_PREFIX@deflateResetKeep
File diff suppressed because it is too large. Load diff
+36
View File
@@ -0,0 +1,36 @@
#include <winver.h>
#include "zlib.h"
VS_VERSION_INFO VERSIONINFO
FILEVERSION ZLIB_VER_MAJOR,ZLIB_VER_MINOR,ZLIB_VER_REVISION,0
PRODUCTVERSION ZLIB_VER_MAJOR,ZLIB_VER_MINOR,ZLIB_VER_REVISION,0
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
#ifdef _DEBUG
FILEFLAGS 1
#else
FILEFLAGS 0
#endif
FILEOS VOS__WINDOWS32
FILETYPE VFT_DLL
FILESUBTYPE 0 // not used
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904E4"
//language ID = U.S. English, char set = Windows, Multilingual
BEGIN
VALUE "FileDescription", "zlib data compression library\0"
VALUE "FileVersion", ZLIB_VERSION "\0"
VALUE "InternalName", "zlib1.dll\0"
VALUE "LegalCopyright", "(C) 1995-2024 Jean-loup Gailly & Mark Adler\0"
VALUE "OriginalFilename", "zlib1.dll\0"
VALUE "ProductName", "zlib\0"
VALUE "ProductVersion", ZLIB_VERSION "\0"
VALUE "Comments", "For more information visit https://www.zlib.net/\0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x0409, 1252
END
END
+8
View File
@@ -0,0 +1,8 @@
/* zlib_name_mangling.h has been automatically generated from
* zlib_name_mangling.h.empty because ZLIB_SYMBOL_PREFIX was NOT set.
*/
#ifndef ZLIB_NAME_MANGLING_H
#define ZLIB_NAME_MANGLING_H
#endif /* ZLIB_NAME_MANGLING_H */
+97
View File
@@ -0,0 +1,97 @@
; zlib data compression library
EXPORTS
; basic functions
@ZLIB_SYMBOL_PREFIX@zlibVersion
@ZLIB_SYMBOL_PREFIX@deflate
@ZLIB_SYMBOL_PREFIX@deflateEnd
@ZLIB_SYMBOL_PREFIX@inflate
@ZLIB_SYMBOL_PREFIX@inflateEnd
; advanced functions
@ZLIB_SYMBOL_PREFIX@deflateSetDictionary
@ZLIB_SYMBOL_PREFIX@deflateGetDictionary
@ZLIB_SYMBOL_PREFIX@deflateCopy
@ZLIB_SYMBOL_PREFIX@deflateReset
@ZLIB_SYMBOL_PREFIX@deflateParams
@ZLIB_SYMBOL_PREFIX@deflateTune
@ZLIB_SYMBOL_PREFIX@deflateBound
@ZLIB_SYMBOL_PREFIX@deflatePending
@ZLIB_SYMBOL_PREFIX@deflatePrime
@ZLIB_SYMBOL_PREFIX@deflateSetHeader
@ZLIB_SYMBOL_PREFIX@inflateSetDictionary
@ZLIB_SYMBOL_PREFIX@inflateGetDictionary
@ZLIB_SYMBOL_PREFIX@inflateSync
@ZLIB_SYMBOL_PREFIX@inflateCopy
@ZLIB_SYMBOL_PREFIX@inflateReset
@ZLIB_SYMBOL_PREFIX@inflateReset2
@ZLIB_SYMBOL_PREFIX@inflatePrime
@ZLIB_SYMBOL_PREFIX@inflateMark
@ZLIB_SYMBOL_PREFIX@inflateGetHeader
@ZLIB_SYMBOL_PREFIX@inflateBack
@ZLIB_SYMBOL_PREFIX@inflateBackEnd
@ZLIB_SYMBOL_PREFIX@zlibCompileFlags
; utility functions
@ZLIB_SYMBOL_PREFIX@compress
@ZLIB_SYMBOL_PREFIX@compress2
@ZLIB_SYMBOL_PREFIX@compressBound
@ZLIB_SYMBOL_PREFIX@uncompress
@ZLIB_SYMBOL_PREFIX@uncompress2
@ZLIB_SYMBOL_PREFIX@gzopen
@ZLIB_SYMBOL_PREFIX@gzdopen
@ZLIB_SYMBOL_PREFIX@gzbuffer
@ZLIB_SYMBOL_PREFIX@gzsetparams
@ZLIB_SYMBOL_PREFIX@gzread
@ZLIB_SYMBOL_PREFIX@gzfread
@ZLIB_SYMBOL_PREFIX@gzwrite
@ZLIB_SYMBOL_PREFIX@gzfwrite
@ZLIB_SYMBOL_PREFIX@gzprintf
@ZLIB_SYMBOL_PREFIX@gzvprintf
@ZLIB_SYMBOL_PREFIX@gzputs
@ZLIB_SYMBOL_PREFIX@gzgets
@ZLIB_SYMBOL_PREFIX@gzputc
@ZLIB_SYMBOL_PREFIX@gzgetc
@ZLIB_SYMBOL_PREFIX@gzungetc
@ZLIB_SYMBOL_PREFIX@gzflush
@ZLIB_SYMBOL_PREFIX@gzseek
@ZLIB_SYMBOL_PREFIX@gzrewind
@ZLIB_SYMBOL_PREFIX@gztell
@ZLIB_SYMBOL_PREFIX@gzoffset
@ZLIB_SYMBOL_PREFIX@gzeof
@ZLIB_SYMBOL_PREFIX@gzdirect
@ZLIB_SYMBOL_PREFIX@gzclose
@ZLIB_SYMBOL_PREFIX@gzclose_r
@ZLIB_SYMBOL_PREFIX@gzclose_w
@ZLIB_SYMBOL_PREFIX@gzerror
@ZLIB_SYMBOL_PREFIX@gzclearerr
; large file functions
@ZLIB_SYMBOL_PREFIX@gzopen64
@ZLIB_SYMBOL_PREFIX@gzseek64
@ZLIB_SYMBOL_PREFIX@gztell64
@ZLIB_SYMBOL_PREFIX@gzoffset64
@ZLIB_SYMBOL_PREFIX@adler32_combine64
@ZLIB_SYMBOL_PREFIX@crc32_combine64
@ZLIB_SYMBOL_PREFIX@crc32_combine_gen64
; checksum functions
@ZLIB_SYMBOL_PREFIX@adler32
@ZLIB_SYMBOL_PREFIX@adler32_z
@ZLIB_SYMBOL_PREFIX@crc32
@ZLIB_SYMBOL_PREFIX@crc32_z
@ZLIB_SYMBOL_PREFIX@adler32_combine
@ZLIB_SYMBOL_PREFIX@crc32_combine
@ZLIB_SYMBOL_PREFIX@crc32_combine_gen
@ZLIB_SYMBOL_PREFIX@crc32_combine_op
; various hacks, don't look :)
@ZLIB_SYMBOL_PREFIX@deflateInit_
@ZLIB_SYMBOL_PREFIX@deflateInit2_
@ZLIB_SYMBOL_PREFIX@inflateInit_
@ZLIB_SYMBOL_PREFIX@inflateInit2_
@ZLIB_SYMBOL_PREFIX@inflateBackInit_
@ZLIB_SYMBOL_PREFIX@gzgetc_
@ZLIB_SYMBOL_PREFIX@zError
@ZLIB_SYMBOL_PREFIX@inflateSyncPoint
@ZLIB_SYMBOL_PREFIX@get_crc_table
@ZLIB_SYMBOL_PREFIX@inflateUndermine
@ZLIB_SYMBOL_PREFIX@inflateValidate
@ZLIB_SYMBOL_PREFIX@inflateCodesUsed
@ZLIB_SYMBOL_PREFIX@inflateResetKeep
@ZLIB_SYMBOL_PREFIX@deflateResetKeep
@ZLIB_SYMBOL_PREFIX@gzopen_w
+353
View File
@@ -0,0 +1,353 @@
#ifndef _ZBUILD_H
#define _ZBUILD_H
#define _POSIX_SOURCE 1 /* fileno */
#ifndef _POSIX_C_SOURCE
# define _POSIX_C_SOURCE 200809L /* snprintf, posix_memalign, strdup */
#endif
#ifndef _ISOC11_SOURCE
# define _ISOC11_SOURCE 1 /* aligned_alloc */
#endif
#ifdef __OpenBSD__
# define _BSD_SOURCE 1
#endif
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
/* Determine compiler version of C Standard */
#ifdef __STDC_VERSION__
# if __STDC_VERSION__ >= 199901L
# ifndef STDC99
# define STDC99
# endif
# endif
# if __STDC_VERSION__ >= 201112L
# ifndef STDC11
# define STDC11
# endif
# endif
#endif
#ifndef Z_HAS_ATTRIBUTE
# if defined(__has_attribute)
# define Z_HAS_ATTRIBUTE(a) __has_attribute(a)
# else
# define Z_HAS_ATTRIBUTE(a) 0
# endif
#endif
#ifndef Z_FALLTHROUGH
# if Z_HAS_ATTRIBUTE(__fallthrough__) || (defined(__GNUC__) && (__GNUC__ >= 7))
# define Z_FALLTHROUGH __attribute__((__fallthrough__))
# else
# define Z_FALLTHROUGH do {} while(0) /* fallthrough */
# endif
#endif
#ifndef Z_TARGET
# if Z_HAS_ATTRIBUTE(__target__)
# define Z_TARGET(x) __attribute__((__target__(x)))
# else
# define Z_TARGET(x)
# endif
#endif
/* This has to be first include that defines any types */
#if defined(_MSC_VER)
# if defined(_WIN64)
typedef __int64 ssize_t;
# else
typedef long ssize_t;
# endif
# if defined(_WIN64)
#define SSIZE_MAX _I64_MAX
# else
#define SSIZE_MAX LONG_MAX
# endif
#endif
/* A forced inline decorator */
#if defined(_MSC_VER)
# define Z_FORCEINLINE __forceinline
#elif defined(__GNUC__)
# define Z_FORCEINLINE inline __attribute__((always_inline))
#else
/* It won't actually force inlining but it will suggest it */
# define Z_FORCEINLINE inline
#endif
/* MS Visual Studio does not allow inline in C, only C++.
But it provides __inline instead, so use that. */
#if defined(_MSC_VER) && !defined(inline) && !defined(__cplusplus)
# define inline __inline
#endif
#if defined(ZLIB_COMPAT)
# define PREFIX(x) x
# define PREFIX2(x) ZLIB_ ## x
# define PREFIX3(x) z_ ## x
# define PREFIX4(x) x ## 64
# define zVersion zlibVersion
#else
# define PREFIX(x) zng_ ## x
# define PREFIX2(x) ZLIBNG_ ## x
# define PREFIX3(x) zng_ ## x
# define PREFIX4(x) zng_ ## x
# define zVersion zlibng_version
# define z_size_t size_t
#endif
/* In zlib-compat some functions and types use unsigned long, but zlib-ng use size_t */
#if defined(ZLIB_COMPAT)
# define z_uintmax_t unsigned long
#else
# define z_uintmax_t size_t
#endif
/* Minimum of a and b. */
#define MIN(a, b) ((a) > (b) ? (b) : (a))
/* Maximum of a and b. */
#define MAX(a, b) ((a) < (b) ? (b) : (a))
/* Ignore unused variable warning */
#define Z_UNUSED(var) (void)(var)
#if defined(HAVE_VISIBILITY_INTERNAL)
# define Z_INTERNAL __attribute__((visibility ("internal")))
#elif defined(HAVE_VISIBILITY_HIDDEN)
# define Z_INTERNAL __attribute__((visibility ("hidden")))
#else
# define Z_INTERNAL
#endif
/* Symbol versioning helpers, allowing multiple versions of a function to exist.
* Functions using this must also be added to zlib-ng.map for each version.
* Double @@ means this is the default for newly compiled applications to link against.
* Single @ means this is kept for backwards compatibility.
* This is only used for Zlib-ng native API, and only on platforms supporting this.
*/
#if defined(HAVE_SYMVER)
# define ZSYMVER(func,alias,ver) __asm__(".symver " func ", " alias "@ZLIB_NG_" ver);
# define ZSYMVER_DEF(func,alias,ver) __asm__(".symver " func ", " alias "@@ZLIB_NG_" ver);
#else
# define ZSYMVER(func,alias,ver)
# define ZSYMVER_DEF(func,alias,ver)
#endif
#ifndef __cplusplus
# define Z_REGISTER register
#else
# define Z_REGISTER
#endif
/* Reverse the bytes in a value. Use compiler intrinsics when
possible to take advantage of hardware implementations. */
#if defined(_MSC_VER) && (_MSC_VER >= 1300)
# include <stdlib.h>
# pragma intrinsic(_byteswap_ulong)
# define ZSWAP16(q) _byteswap_ushort(q)
# define ZSWAP32(q) _byteswap_ulong(q)
# define ZSWAP64(q) _byteswap_uint64(q)
#elif defined(__clang__) || (defined(__GNUC__) && \
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
# define ZSWAP16(q) __builtin_bswap16(q)
# define ZSWAP32(q) __builtin_bswap32(q)
# define ZSWAP64(q) __builtin_bswap64(q)
#elif defined(__GNUC__) && (__GNUC__ >= 2) && defined(__linux__)
# include <byteswap.h>
# define ZSWAP16(q) bswap_16(q)
# define ZSWAP32(q) bswap_32(q)
# define ZSWAP64(q) bswap_64(q)
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
# include <sys/endian.h>
# define ZSWAP16(q) bswap16(q)
# define ZSWAP32(q) bswap32(q)
# define ZSWAP64(q) bswap64(q)
#elif defined(__OpenBSD__)
# include <sys/endian.h>
# define ZSWAP16(q) swap16(q)
# define ZSWAP32(q) swap32(q)
# define ZSWAP64(q) swap64(q)
#elif defined(__INTEL_COMPILER)
/* ICC does not provide a two byte swap. */
# define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8))
# define ZSWAP32(q) _bswap(q)
# define ZSWAP64(q) _bswap64(q)
#else
# define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8))
# define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
(((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
# define ZSWAP64(q) \
(((q & 0xFF00000000000000u) >> 56u) | \
((q & 0x00FF000000000000u) >> 40u) | \
((q & 0x0000FF0000000000u) >> 24u) | \
((q & 0x000000FF00000000u) >> 8u) | \
((q & 0x00000000FF000000u) << 8u) | \
((q & 0x0000000000FF0000u) << 24u) | \
((q & 0x000000000000FF00u) << 40u) | \
((q & 0x00000000000000FFu) << 56u))
#endif
/* Only enable likely/unlikely if the compiler is known to support it */
#if (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__INTEL_COMPILER) || defined(__clang__)
# define LIKELY_NULL(x) __builtin_expect((x) != 0, 0)
# define LIKELY(x) __builtin_expect(!!(x), 1)
# define UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
# define LIKELY_NULL(x) x
# define LIKELY(x) x
# define UNLIKELY(x) x
#endif /* (un)likely */
#if defined(HAVE_ATTRIBUTE_ALIGNED)
# define ALIGNED_(x) __attribute__ ((aligned(x)))
#elif defined(_MSC_VER)
# define ALIGNED_(x) __declspec(align(x))
#else
/* TODO: Define ALIGNED_ for your compiler */
# define ALIGNED_(x)
#endif
#ifdef HAVE_BUILTIN_ASSUME_ALIGNED
# define HINT_ALIGNED(p,n) __builtin_assume_aligned((void *)(p),(n))
#else
# define HINT_ALIGNED(p,n) (p)
#endif
#define HINT_ALIGNED_16(p) HINT_ALIGNED((p),16)
#define HINT_ALIGNED_64(p) HINT_ALIGNED((p),64)
#define HINT_ALIGNED_4096(p) HINT_ALIGNED((p),4096)
/* PADSZ returns needed bytes to pad bpos to pad size
* PAD_NN calculates pad size and adds it to bpos, returning the result.
* All take an integer or a pointer as bpos input.
*/
#define PADSZ(bpos, pad) (((pad) - ((uintptr_t)(bpos) % (pad))) % (pad))
#define PAD_16(bpos) ((bpos) + PADSZ((bpos),16))
#define PAD_64(bpos) ((bpos) + PADSZ((bpos),64))
#define PAD_4096(bpos) ((bpos) + PADSZ((bpos),4096))
/* Diagnostic functions */
#ifdef ZLIB_DEBUG
# include <stdio.h>
extern int Z_INTERNAL z_verbose;
extern void Z_INTERNAL z_error(const char *m);
# define Assert(cond, msg) {int _cond = (cond); if (!_cond) z_error(msg);}
# define Trace(x) {if (z_verbose >= 0) fprintf x;}
# define Tracev(x) {if (z_verbose > 0) fprintf x;}
# define Tracevv(x) {if (z_verbose > 1) fprintf x;}
# define Tracec(c, x) {if (z_verbose > 0 && (c)) fprintf x;}
# define Tracecv(c, x) {if (z_verbose > 1 && (c)) fprintf x;}
#else
# define Assert(cond, msg)
# define Trace(x)
# define Tracev(x)
# define Tracevv(x)
# define Tracec(c, x)
# define Tracecv(c, x)
#endif
/* OPTIMAL_CMP values determine the comparison width:
* 64: Best for 64-bit architectures with unaligned access
* 32: Best for 32-bit architectures with unaligned access
* 16: Safe default for unknown architectures
* 8: Safe fallback for architectures without unaligned access
* Note: The unaligned access mentioned is cpu-support, this allows compiler or
* separate unaligned intrinsics to utilize safe unaligned access, without
* utilizing unaligned C pointers that are known to have undefined behavior.
*/
#if !defined(OPTIMAL_CMP)
# if defined(__x86_64__) || defined(_M_X64) || defined(__amd64__) || defined(_M_AMD64)
# define OPTIMAL_CMP 64
# elif defined(__i386__) || defined(__i486__) || defined(__i586__) || \
defined(__i686__) || defined(_X86_) || defined(_M_IX86)
# define OPTIMAL_CMP 32
# elif defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
# if defined(__ARM_FEATURE_UNALIGNED) || defined(_WIN32)
# define OPTIMAL_CMP 64
# else
# define OPTIMAL_CMP 8
# endif
# elif defined(__arm__) || defined(_M_ARM)
# if defined(__ARM_FEATURE_UNALIGNED) || defined(_WIN32)
# define OPTIMAL_CMP 32
# else
# define OPTIMAL_CMP 8
# endif
# elif defined(__powerpc64__) || defined(__ppc64__)
# define OPTIMAL_CMP 64
# elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)
# define OPTIMAL_CMP 32
# endif
#endif
#if !defined(OPTIMAL_CMP)
# define OPTIMAL_CMP 16
#endif
#if defined(__has_feature)
# if __has_feature(address_sanitizer)
# define Z_ADDRESS_SANITIZER 1
# endif
#elif defined(__SANITIZE_ADDRESS__)
# define Z_ADDRESS_SANITIZER 1
#endif
/*
* __asan_loadN() and __asan_storeN() calls are inserted by compilers in order to check memory accesses.
* They can be called manually too, with the following caveats:
* gcc says: "warning: implicit declaration of function '...'"
* g++ says: "error: new declaration '...' ambiguates built-in declaration '...'"
* Accommodate both.
*/
#ifdef Z_ADDRESS_SANITIZER
#ifndef __cplusplus
void __asan_loadN(void *, long);
void __asan_storeN(void *, long);
#endif
#else
# define __asan_loadN(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0)
# define __asan_storeN(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0)
#endif
#if defined(__has_feature)
# if __has_feature(memory_sanitizer)
# define Z_MEMORY_SANITIZER 1
# include <sanitizer/msan_interface.h>
# endif
#endif
#ifndef Z_MEMORY_SANITIZER
# define __msan_check_mem_is_initialized(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0)
# define __msan_unpoison(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0)
#endif
/* Notify sanitizer runtime about an upcoming read access. */
#define instrument_read(a, size) do { \
void *__a = (void *)(a); \
long __size = size; \
__asan_loadN(__a, __size); \
__msan_check_mem_is_initialized(__a, __size); \
} while (0)
/* Notify sanitizer runtime about an upcoming write access. */
#define instrument_write(a, size) do { \
void *__a = (void *)(a); \
long __size = size; \
__asan_storeN(__a, __size); \
} while (0)
/* Notify sanitizer runtime about an upcoming read/write access. */
#define instrument_read_write(a, size) do { \
void *__a = (void *)(a); \
long __size = size; \
__asan_storeN(__a, __size); \
__msan_check_mem_is_initialized(__a, __size); \
} while (0)
#endif
+176
View File
@@ -0,0 +1,176 @@
/* zconf-ng.h -- configuration of the zlib-ng compression library
* Copyright (C) 1995-2024 Jean-loup Gailly, Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef ZCONFNG_H
#define ZCONFNG_H
#include "zlib_name_mangling-ng.h"
#if !defined(_WIN32) && defined(__WIN32__)
# define _WIN32
#endif
/* Clang macro for detecting declspec support
* https://clang.llvm.org/docs/LanguageExtensions.html#has-declspec-attribute
*/
#ifndef __has_declspec_attribute
# define __has_declspec_attribute(x) 0
#endif
/* Always define z_const as const */
#define z_const const
/* Maximum value for memLevel in deflateInit2 */
#ifndef MAX_MEM_LEVEL
# define MAX_MEM_LEVEL 9
#endif
/* Maximum value for windowBits in deflateInit2 and inflateInit2.
* WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
* created by gzip. (Files created by minigzip can still be extracted by
* gzip.)
*/
#ifndef MIN_WBITS
# define MIN_WBITS 8 /* 256 LZ77 window */
#endif
#ifndef MAX_WBITS
# define MAX_WBITS 15 /* 32K LZ77 window */
#endif
/* The memory requirements for deflate are (in bytes):
(1 << (windowBits+2)) + (1 << (memLevel+9))
that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
plus a few kilobytes for small objects. For example, if you want to reduce
the default memory requirements from 256K to 128K, compile with
make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
Of course this will generally degrade compression (there's no free lunch).
The memory requirements for inflate are (in bytes) 1 << windowBits
that is, 32K for windowBits=15 (default value) plus about 7 kilobytes
for small objects.
*/
/* Type declarations */
#ifdef ZLIB_INTERNAL
# define Z_INTERNAL ZLIB_INTERNAL
#endif
/* If building or using zlib as a DLL, define ZLIB_DLL.
* This is not mandatory, but it offers a little performance increase.
*/
#if defined(ZLIB_DLL) && (defined(_WIN32) || (__has_declspec_attribute(dllexport) && __has_declspec_attribute(dllimport)))
# ifdef Z_INTERNAL
# define Z_EXTERN extern __declspec(dllexport)
# else
# define Z_EXTERN extern __declspec(dllimport)
# endif
#endif
/* If building or using zlib with the WINAPI/WINAPIV calling convention,
* define ZLIB_WINAPI.
* Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
*/
#if defined(ZLIB_WINAPI) && defined(_WIN32)
# include <windows.h>
/* No need for _export, use ZLIB.DEF instead. */
/* For complete Windows compatibility, use WINAPI, not __stdcall. */
# define Z_EXPORT WINAPI
# define Z_EXPORTVA WINAPIV
#endif
#ifndef Z_EXTERN
# define Z_EXTERN extern
#endif
#ifndef Z_EXPORT
# define Z_EXPORT
#endif
#ifndef Z_EXPORTVA
# define Z_EXPORTVA
#endif
/* Conditional exports */
#define ZNG_CONDEXPORT Z_EXPORT
/* Fallback for something that includes us. */
typedef unsigned char Byte;
typedef Byte Bytef;
typedef unsigned int uInt; /* 16 bits or more */
typedef unsigned long uLong; /* 32 bits or more */
typedef char charf;
typedef int intf;
typedef uInt uIntf;
typedef uLong uLongf;
typedef void const *voidpc;
typedef void *voidpf;
typedef void *voidp;
#ifdef HAVE_UNISTD_H /* may be set to #if 1 by configure/cmake/etc */
# define Z_HAVE_UNISTD_H
#endif
#ifdef NEED_PTRDIFF_T /* may be set to #if 1 by configure/cmake/etc */
typedef PTRDIFF_TYPE ptrdiff_t;
#endif
#include <sys/types.h> /* for off_t */
#include <stddef.h> /* for wchar_t and NULL */
/* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and
* "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even
* though the former does not conform to the LFS document), but considering
* both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as
* equivalently requesting no 64-bit operations
*/
#if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1
# undef _LARGEFILE64_SOURCE
#endif
#if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE)
# include <unistd.h> /* for SEEK_*, off_t, and _LFS64_LARGEFILE */
# ifndef z_off_t
# define z_off_t off_t
# endif
#endif
#if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0
# define Z_LFS64
#endif
#if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64)
# define Z_LARGE64
#endif
#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS-0 == 64 && defined(Z_LFS64)
# define Z_WANT64
#endif
#if !defined(SEEK_SET) && defined(WITH_GZFILEOP)
# define SEEK_SET 0 /* Seek from beginning of file. */
# define SEEK_CUR 1 /* Seek from current position. */
# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
#endif
#ifndef z_off_t
# define z_off_t long
#endif
#if !defined(_WIN32) && defined(Z_LARGE64)
# define z_off64_t off64_t
#else
# if defined(__MSYS__)
# define z_off64_t _off64_t
# elif defined(_WIN32) && !defined(__GNUC__)
# define z_off64_t __int64
# else
# define z_off64_t z_off_t
# endif
#endif
#endif /* ZCONFNG_H */
+206
View File
@@ -0,0 +1,206 @@
/* zconf.h -- configuration of the zlib compression library
* Copyright (C) 1995-2024 Jean-loup Gailly, Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef ZCONF_H
#define ZCONF_H
#include "zlib_name_mangling.h"
#if !defined(_WIN32) && defined(__WIN32__)
# define _WIN32
#endif
/* Clang macro for detecting declspec support
* https://clang.llvm.org/docs/LanguageExtensions.html#has-declspec-attribute
*/
#ifndef __has_declspec_attribute
# define __has_declspec_attribute(x) 0
#endif
#if defined(ZLIB_CONST) && !defined(z_const)
# define z_const const
#else
# define z_const
#endif
/* Maximum value for memLevel in deflateInit2 */
#ifndef MAX_MEM_LEVEL
# define MAX_MEM_LEVEL 9
#endif
/* Maximum value for windowBits in deflateInit2 and inflateInit2.
* WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
* created by gzip. (Files created by minigzip can still be extracted by
* gzip.)
*/
#ifndef MIN_WBITS
# define MIN_WBITS 8 /* 256 LZ77 window */
#endif
#ifndef MAX_WBITS
# define MAX_WBITS 15 /* 32K LZ77 window */
#endif
/* The memory requirements for deflate are (in bytes):
(1 << (windowBits+2)) + (1 << (memLevel+9))
that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
plus a few kilobytes for small objects. For example, if you want to reduce
the default memory requirements from 256K to 128K, compile with
make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
Of course this will generally degrade compression (there's no free lunch).
The memory requirements for inflate are (in bytes) 1 << windowBits
that is, 32K for windowBits=15 (default value) plus about 7 kilobytes
for small objects.
*/
/* Type declarations */
#ifndef OF /* function prototypes */
# define OF(args) args
#endif
#ifdef ZLIB_INTERNAL
# define Z_INTERNAL ZLIB_INTERNAL
#endif
/* If building or using zlib as a DLL, define ZLIB_DLL.
* This is not mandatory, but it offers a little performance increase.
*/
#if defined(ZLIB_DLL) && (defined(_WIN32) || (__has_declspec_attribute(dllexport) && __has_declspec_attribute(dllimport)))
# ifdef Z_INTERNAL
# define Z_EXTERN extern __declspec(dllexport)
# else
# define Z_EXTERN extern __declspec(dllimport)
# endif
#endif
/* If building or using zlib with the WINAPI/WINAPIV calling convention,
* define ZLIB_WINAPI.
* Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
*/
#if defined(ZLIB_WINAPI) && defined(_WIN32)
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
/* No need for _export, use ZLIB.DEF instead. */
/* For complete Windows compatibility, use WINAPI, not __stdcall. */
# define Z_EXPORT WINAPI
# define Z_EXPORTVA WINAPIV
#endif
#ifndef Z_EXTERN
# define Z_EXTERN extern
#endif
#ifndef Z_EXPORT
# define Z_EXPORT
#endif
#ifndef Z_EXPORTVA
# define Z_EXPORTVA
#endif
/* Conditional exports */
#define ZNG_CONDEXPORT Z_INTERNAL
/* For backwards compatibility */
#ifndef ZEXTERN
# define ZEXTERN Z_EXTERN
#endif
#ifndef ZEXPORT
# define ZEXPORT Z_EXPORT
#endif
#ifndef ZEXPORTVA
# define ZEXPORTVA Z_EXPORTVA
#endif
#ifndef FAR
# define FAR
#endif
/* Legacy zlib typedefs for backwards compatibility. Don't assume stdint.h is defined. */
typedef unsigned char Byte;
typedef Byte Bytef;
typedef unsigned int uInt; /* 16 bits or more */
typedef unsigned long uLong; /* 32 bits or more */
typedef char charf;
typedef int intf;
typedef uInt uIntf;
typedef uLong uLongf;
typedef void const *voidpc;
typedef void *voidpf;
typedef void *voidp;
typedef unsigned int z_crc_t;
#ifdef HAVE_UNISTD_H /* may be set to #if 1 by configure/cmake/etc */
# define Z_HAVE_UNISTD_H
#endif
#ifdef NEED_PTRDIFF_T /* may be set to #if 1 by configure/cmake/etc */
typedef PTRDIFF_TYPE ptrdiff_t;
#endif
#include <sys/types.h> /* for off_t */
#include <stddef.h> /* for wchar_t and NULL */
/* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and
* "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even
* though the former does not conform to the LFS document), but considering
* both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as
* equivalently requesting no 64-bit operations
*/
#if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1
# undef _LARGEFILE64_SOURCE
#endif
#if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE)
# include <unistd.h> /* for SEEK_*, off_t, and _LFS64_LARGEFILE */
# ifndef z_off_t
# define z_off_t off_t
# endif
#endif
#if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0
# define Z_LFS64
#endif
#if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64)
# define Z_LARGE64
#endif
#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS-0 == 64 && defined(Z_LFS64)
# define Z_WANT64
#endif
#if !defined(SEEK_SET)
# define SEEK_SET 0 /* Seek from beginning of file. */
# define SEEK_CUR 1 /* Seek from current position. */
# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
#endif
#ifndef z_off_t
# define z_off_t long
#endif
#if !defined(_WIN32) && defined(Z_LARGE64)
# define z_off64_t off64_t
#else
# if defined(__MSYS__)
# define z_off64_t _off64_t
# elif defined(_WIN32) && !defined(__GNUC__)
# define z_off64_t __int64
# else
# define z_off64_t z_off_t
# endif
#endif
typedef size_t z_size_t;
#endif /* ZCONF_H */
+60
View File
@@ -0,0 +1,60 @@
/* zendian.h -- define BYTE_ORDER for endian tests
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef ENDIAN_H_
#define ENDIAN_H_
/* First check whether the compiler knows the target __BYTE_ORDER__. */
#if defined(__BYTE_ORDER__)
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
# if !defined(LITTLE_ENDIAN)
# define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
# endif
# if !defined(BYTE_ORDER)
# define BYTE_ORDER LITTLE_ENDIAN
# endif
# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# if !defined(BIG_ENDIAN)
# define BIG_ENDIAN __ORDER_BIG_ENDIAN__
# endif
# if !defined(BYTE_ORDER)
# define BYTE_ORDER BIG_ENDIAN
# endif
# endif
#elif defined(__MINGW32__)
# include <sys/param.h>
#elif defined(_WIN32)
# define LITTLE_ENDIAN 1234
# define BIG_ENDIAN 4321
# if defined(_M_IX86) || defined(_M_AMD64) || defined(_M_IA64) || defined (_M_ARM) || defined (_M_ARM64) || defined (_M_ARM64EC)
# define BYTE_ORDER LITTLE_ENDIAN
# else
# error Unknown endianness!
# endif
#elif defined(__linux__)
# include <endian.h>
#elif defined(__APPLE__)
# include <machine/endian.h>
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__DragonFly__)
# include <sys/endian.h>
#elif defined(__sun) || defined(sun)
# include <sys/byteorder.h>
# if !defined(LITTLE_ENDIAN)
# define LITTLE_ENDIAN 4321
# endif
# if !defined(BIG_ENDIAN)
# define BIG_ENDIAN 1234
# endif
# if !defined(BYTE_ORDER)
# if defined(_BIG_ENDIAN)
# define BYTE_ORDER BIG_ENDIAN
# else
# define BYTE_ORDER LITTLE_ENDIAN
# endif
# endif
#else
# include <endian.h>
#endif
#endif
+12
View File
@@ -0,0 +1,12 @@
set(ZLIB_VERSION @ZLIB_HEADER_VERSION@)
@PACKAGE_INIT@
set_and_check(ZLIB_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@")
set(ZLIB_INCLUDE_DIRS "${ZLIB_INCLUDE_DIR}")
set_and_check(ZLIB_LIB_DIR "@PACKAGE_LIB_INSTALL_DIR@")
set(ZLIB_LIBRARIES ZLIB::ZLIB)
include("${CMAKE_CURRENT_LIST_DIR}/ZLIB.cmake")
check_required_components(ZLIB)
+10
View File
@@ -0,0 +1,10 @@
set(zlib-ng_VERSION @ZLIBNG_HEADER_VERSION@)
@PACKAGE_INIT@
set_and_check(zlib-ng_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@")
set_and_check(zlib-ng_LIB_DIR "@PACKAGE_LIB_INSTALL_DIR@")
include("${CMAKE_CURRENT_LIST_DIR}/zlib-ng.cmake")
check_required_components(zlib-ng)
File diff suppressed because it is too large. Load diff
+111
View File
@@ -0,0 +1,111 @@
ZLIB_NG_2.1.0 {
global:
zng_deflateInit;
zng_deflateInit2;
zng_inflateBackInit;
zng_inflateInit;
zng_inflateInit2;
zlibng_version;
};
ZLIB_NG_2.0.0 {
global:
zng_adler32;
zng_adler32_combine;
zng_adler32_z;
zng_compress;
zng_compress2;
zng_compressBound;
zng_crc32;
zng_crc32_combine;
zng_crc32_combine_gen;
zng_crc32_combine_op;
zng_crc32_z;
zng_deflate;
zng_deflateBound;
zng_deflateCopy;
zng_deflateEnd;
zng_deflateGetDictionary;
zng_deflateGetParams;
zng_deflateInit_;
zng_deflateInit2_;
zng_deflateParams;
zng_deflatePending;
zng_deflatePrime;
zng_deflateReset;
zng_deflateResetKeep;
zng_deflateSetDictionary;
zng_deflateSetHeader;
zng_deflateSetParams;
zng_deflateTune;
zng_get_crc_table;
zng_inflate;
zng_inflateBack;
zng_inflateBackEnd;
zng_inflateBackInit_;
zng_inflateCodesUsed;
zng_inflateCopy;
zng_inflateEnd;
zng_inflateGetDictionary;
zng_inflateGetHeader;
zng_inflateInit_;
zng_inflateInit2_;
zng_inflateMark;
zng_inflatePrime;
zng_inflateReset;
zng_inflateReset2;
zng_inflateResetKeep;
zng_inflateSetDictionary;
zng_inflateSync;
zng_inflateSyncPoint;
zng_inflateUndermine;
zng_inflateValidate;
zng_uncompress;
zng_uncompress2;
zng_zError;
zng_zlibCompileFlags;
zng_vstring;
local:
zng_deflate_copyright;
zng_inflate_copyright;
zng_zcalloc;
zng_zcfree;
zng_z_errmsg;
gz_error;
_*;
};
ZLIB_NG_GZ_2.0.0 {
global:
zng_gzbuffer;
zng_gzclearerr;
zng_gzclose;
zng_gzclose_r;
zng_gzclose_w;
zng_gzdirect;
zng_gzdopen;
zng_gzeof;
zng_gzerror;
zng_gzflush;
zng_gzfread;
zng_gzfwrite;
zng_gzgetc;
zng_gzgets;
zng_gzoffset;
zng_gzopen;
zng_gzprintf;
zng_gzputc;
zng_gzputs;
zng_gzread;
zng_gzrewind;
zng_gzseek;
zng_gzsetparams;
zng_gztell;
zng_gzungetc;
zng_gzvprintf;
zng_gzwrite;
};
FAIL {
local: *;
};
+1859
View File
File diff suppressed because it is too large. Load diff
+98
View File
@@ -0,0 +1,98 @@
ZLIB_1.2.0 {
global:
compressBound;
deflateBound;
inflateBack;
inflateBackEnd;
inflateBackInit_;
inflateCopy;
local:
deflate_copyright;
inflate_copyright;
zcalloc;
zcfree;
z_errmsg;
gz_error;
gz_intmax;
_*;
};
ZLIB_1.2.0.2 {
gzclearerr;
gzungetc;
zlibCompileFlags;
} ZLIB_1.2.0;
ZLIB_1.2.0.8 {
deflatePrime;
} ZLIB_1.2.0.2;
ZLIB_1.2.2 {
adler32_combine;
crc32_combine;
deflateSetHeader;
inflateGetHeader;
} ZLIB_1.2.0.8;
ZLIB_1.2.2.3 {
deflateTune;
gzdirect;
} ZLIB_1.2.2;
ZLIB_1.2.2.4 {
inflatePrime;
} ZLIB_1.2.2.3;
ZLIB_1.2.3.3 {
adler32_combine64;
crc32_combine64;
gzopen64;
gzseek64;
gztell64;
inflateUndermine;
} ZLIB_1.2.2.4;
ZLIB_1.2.3.4 {
inflateReset2;
inflateMark;
} ZLIB_1.2.3.3;
ZLIB_1.2.3.5 {
gzbuffer;
gzoffset;
gzoffset64;
gzclose_r;
gzclose_w;
} ZLIB_1.2.3.4;
ZLIB_1.2.5.1 {
deflatePending;
} ZLIB_1.2.3.5;
ZLIB_1.2.5.2 {
deflateResetKeep;
gzgetc_;
inflateResetKeep;
} ZLIB_1.2.5.1;
ZLIB_1.2.7.1 {
inflateGetDictionary;
gzvprintf;
} ZLIB_1.2.5.2;
ZLIB_1.2.9 {
inflateCodesUsed;
inflateValidate;
uncompress2;
gzfread;
gzfwrite;
deflateGetDictionary;
adler32_z;
crc32_z;
} ZLIB_1.2.7.1;
ZLIB_1.2.12 {
crc32_combine_gen;
crc32_combine_gen64;
crc32_combine_op;
} ZLIB_1.2.9;
+14
View File
@@ -0,0 +1,14 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
symbol_prefix=@ZLIB_SYMBOL_PREFIX@
libdir=@PC_LIB_INSTALL_DIR@
sharedlibdir=${libdir}
includedir=@PC_INC_INSTALL_DIR@
Name: zlib@SUFFIX@
Description: zlib-ng compression library
Version: @ZLIB_FULL_VERSION@
Requires:
Libs: -L${libdir} -L${sharedlibdir} -lz@SUFFIX@
Cflags: -I${includedir} @PKG_CONFIG_CFLAGS@
+14
View File
@@ -0,0 +1,14 @@
prefix=@prefix@
exec_prefix=@exec_prefix@
symbol_prefix=@symbol_prefix@
libdir=@libdir@
sharedlibdir=@sharedlibdir@
includedir=@includedir@
Name: zlib@SUFFIX@
Description: zlib-ng compression library
Version: @VERSION@
Requires:
Libs: -L${libdir} -L${sharedlibdir} -lz@SUFFIX@
Cflags: -I${includedir} @PKG_CONFIG_CFLAGS@
+178
View File
@@ -0,0 +1,178 @@
/* zlib_name_mangling.h has been automatically generated from
* zlib_name_mangling.h.in because ZLIB_SYMBOL_PREFIX was set.
*/
#ifndef ZLIB_NAME_MANGLING_H
#define ZLIB_NAME_MANGLING_H
/* all linked symbols and init macros */
#define zng__dist_code @ZLIB_SYMBOL_PREFIX@zng__dist_code
#define zng__length_code @ZLIB_SYMBOL_PREFIX@zng__length_code
#define zng__tr_align @ZLIB_SYMBOL_PREFIX@zng__tr_align
#define zng__tr_flush_bits @ZLIB_SYMBOL_PREFIX@zng__tr_flush_bits
#define zng__tr_flush_block @ZLIB_SYMBOL_PREFIX@zng__tr_flush_block
#define zng__tr_init @ZLIB_SYMBOL_PREFIX@zng__tr_init
#define zng__tr_stored_block @ZLIB_SYMBOL_PREFIX@zng__tr_stored_block
#define zng__tr_tally @ZLIB_SYMBOL_PREFIX@zng__tr_tally
#define zng_adler32 @ZLIB_SYMBOL_PREFIX@zng_adler32
#define zng_adler32_combine @ZLIB_SYMBOL_PREFIX@zng_adler32_combine
#define zng_adler32_combine64 @ZLIB_SYMBOL_PREFIX@zng_adler32_combine64
#define zng_adler32_z @ZLIB_SYMBOL_PREFIX@zng_adler32_z
#define zng_compress @ZLIB_SYMBOL_PREFIX@zng_compress
#define zng_compress2 @ZLIB_SYMBOL_PREFIX@zng_compress2
#define zng_compressBound @ZLIB_SYMBOL_PREFIX@zng_compressBound
#define zng_crc32 @ZLIB_SYMBOL_PREFIX@zng_crc32
#define zng_crc32_combine @ZLIB_SYMBOL_PREFIX@zng_crc32_combine
#define zng_crc32_combine64 @ZLIB_SYMBOL_PREFIX@zng_crc32_combine64
#define zng_crc32_combine_gen @ZLIB_SYMBOL_PREFIX@zng_crc32_combine_gen
#define zng_crc32_combine_gen64 @ZLIB_SYMBOL_PREFIX@zng_crc32_combine_gen64
#define zng_crc32_combine_op @ZLIB_SYMBOL_PREFIX@zng_crc32_combine_op
#define zng_crc32_z @ZLIB_SYMBOL_PREFIX@zng_crc32_z
#define zng_deflate @ZLIB_SYMBOL_PREFIX@zng_deflate
#define zng_deflateBound @ZLIB_SYMBOL_PREFIX@zng_deflateBound
#define zng_deflateCopy @ZLIB_SYMBOL_PREFIX@zng_deflateCopy
#define zng_deflateEnd @ZLIB_SYMBOL_PREFIX@zng_deflateEnd
#define zng_deflateGetDictionary @ZLIB_SYMBOL_PREFIX@zng_deflateGetDictionary
#define zng_deflateInit @ZLIB_SYMBOL_PREFIX@zng_deflateInit
#define zng_deflateInit2 @ZLIB_SYMBOL_PREFIX@zng_deflateInit2
#define zng_deflateInit2_ @ZLIB_SYMBOL_PREFIX@zng_deflateInit2_
#define zng_deflateInit_ @ZLIB_SYMBOL_PREFIX@zng_deflateInit_
#define zng_deflateParams @ZLIB_SYMBOL_PREFIX@zng_deflateParams
#define zng_deflatePending @ZLIB_SYMBOL_PREFIX@zng_deflatePending
#define zng_deflatePrime @ZLIB_SYMBOL_PREFIX@zng_deflatePrime
#define zng_deflateReset @ZLIB_SYMBOL_PREFIX@zng_deflateReset
#define zng_deflateResetKeep @ZLIB_SYMBOL_PREFIX@zng_deflateResetKeep
#define zng_deflateSetDictionary @ZLIB_SYMBOL_PREFIX@zng_deflateSetDictionary
#define zng_deflateSetHeader @ZLIB_SYMBOL_PREFIX@zng_deflateSetHeader
#define zng_deflateTune @ZLIB_SYMBOL_PREFIX@zng_deflateTune
#define zng_deflate_copyright @ZLIB_SYMBOL_PREFIX@zng_deflate_copyright
#define zng_fill_window @ZLIB_SYMBOL_PREFIX@zng_fill_window
#define zng_fixedtables @ZLIB_SYMBOL_PREFIX@zng_fixedtables
#define zng_flush_pending @ZLIB_SYMBOL_PREFIX@zng_flush_pending
#define zng_get_crc_table @ZLIB_SYMBOL_PREFIX@zng_get_crc_table
#ifdef WITH_GZFILEOP
# define zng_gz_error @ZLIB_SYMBOL_PREFIX@zng_gz_error
# define zng_gz_strwinerror @ZLIB_SYMBOL_PREFIX@zng_gz_strwinerror
# define zng_gzbuffer @ZLIB_SYMBOL_PREFIX@zng_gzbuffer
# define zng_gzclearerr @ZLIB_SYMBOL_PREFIX@zng_gzclearerr
# define zng_gzclose @ZLIB_SYMBOL_PREFIX@zng_gzclose
# define zng_gzclose_r @ZLIB_SYMBOL_PREFIX@zng_gzclose_r
# define zng_gzclose_w @ZLIB_SYMBOL_PREFIX@zng_gzclose_w
# define zng_gzdirect @ZLIB_SYMBOL_PREFIX@zng_gzdirect
# define zng_gzdopen @ZLIB_SYMBOL_PREFIX@zng_gzdopen
# define zng_gzeof @ZLIB_SYMBOL_PREFIX@zng_gzeof
# define zng_gzerror @ZLIB_SYMBOL_PREFIX@zng_gzerror
# define zng_gzflush @ZLIB_SYMBOL_PREFIX@zng_gzflush
# define zng_gzfread @ZLIB_SYMBOL_PREFIX@zng_gzfread
# define zng_gzfwrite @ZLIB_SYMBOL_PREFIX@zng_gzfwrite
# define zng_gzgetc @ZLIB_SYMBOL_PREFIX@zng_gzgetc
# define zng_gzgetc_ @ZLIB_SYMBOL_PREFIX@zng_gzgetc_
# define zng_gzgets @ZLIB_SYMBOL_PREFIX@zng_gzgets
# define zng_gzoffset @ZLIB_SYMBOL_PREFIX@zng_gzoffset
# define zng_gzoffset64 @ZLIB_SYMBOL_PREFIX@zng_gzoffset64
# define zng_gzopen @ZLIB_SYMBOL_PREFIX@zng_gzopen
# define zng_gzopen64 @ZLIB_SYMBOL_PREFIX@zng_gzopen64
# ifdef _WIN32
# define zng_gzopen_w @ZLIB_SYMBOL_PREFIX@zng_gzopen_w
# endif
# define zng_gzprintf @ZLIB_SYMBOL_PREFIX@zng_gzprintf
# define zng_gzputc @ZLIB_SYMBOL_PREFIX@zng_gzputc
# define zng_gzputs @ZLIB_SYMBOL_PREFIX@zng_gzputs
# define zng_gzread @ZLIB_SYMBOL_PREFIX@zng_gzread
# define zng_gzrewind @ZLIB_SYMBOL_PREFIX@zng_gzrewind
# define zng_gzseek @ZLIB_SYMBOL_PREFIX@zng_gzseek
# define zng_gzseek64 @ZLIB_SYMBOL_PREFIX@zng_gzseek64
# define zng_gzsetparams @ZLIB_SYMBOL_PREFIX@zng_gzsetparams
# define zng_gztell @ZLIB_SYMBOL_PREFIX@zng_gztell
# define zng_gztell64 @ZLIB_SYMBOL_PREFIX@zng_gztell64
# define zng_gzungetc @ZLIB_SYMBOL_PREFIX@zng_gzungetc
# define zng_gzvprintf @ZLIB_SYMBOL_PREFIX@zng_gzvprintf
# define zng_gzwrite @ZLIB_SYMBOL_PREFIX@zng_gzwrite
#endif
#define zng_inflate @ZLIB_SYMBOL_PREFIX@zng_inflate
#define zng_inflateBack @ZLIB_SYMBOL_PREFIX@zng_inflateBack
#define zng_inflateBackEnd @ZLIB_SYMBOL_PREFIX@zng_inflateBackEnd
#define zng_inflateBackInit @ZLIB_SYMBOL_PREFIX@zng_inflateBackInit
#define zng_inflateBackInit_ @ZLIB_SYMBOL_PREFIX@zng_inflateBackInit_
#define zng_inflateCodesUsed @ZLIB_SYMBOL_PREFIX@zng_inflateCodesUsed
#define zng_inflateCopy @ZLIB_SYMBOL_PREFIX@zng_inflateCopy
#define zng_inflateEnd @ZLIB_SYMBOL_PREFIX@zng_inflateEnd
#define zng_inflateGetDictionary @ZLIB_SYMBOL_PREFIX@zng_inflateGetDictionary
#define zng_inflateGetHeader @ZLIB_SYMBOL_PREFIX@zng_inflateGetHeader
#define zng_inflateInit @ZLIB_SYMBOL_PREFIX@zng_inflateInit
#define zng_inflateInit2 @ZLIB_SYMBOL_PREFIX@zng_inflateInit2
#define zng_inflateInit2_ @ZLIB_SYMBOL_PREFIX@zng_inflateInit2_
#define zng_inflateInit_ @ZLIB_SYMBOL_PREFIX@zng_inflateInit_
#define zng_inflateMark @ZLIB_SYMBOL_PREFIX@zng_inflateMark
#define zng_inflatePrime @ZLIB_SYMBOL_PREFIX@zng_inflatePrime
#define zng_inflateReset @ZLIB_SYMBOL_PREFIX@zng_inflateReset
#define zng_inflateReset2 @ZLIB_SYMBOL_PREFIX@zng_inflateReset2
#define zng_inflateResetKeep @ZLIB_SYMBOL_PREFIX@zng_inflateResetKeep
#define zng_inflateSetDictionary @ZLIB_SYMBOL_PREFIX@zng_inflateSetDictionary
#define zng_inflateSync @ZLIB_SYMBOL_PREFIX@zng_inflateSync
#define zng_inflateSyncPoint @ZLIB_SYMBOL_PREFIX@zng_inflateSyncPoint
#define zng_inflateUndermine @ZLIB_SYMBOL_PREFIX@zng_inflateUndermine
#define zng_inflateValidate @ZLIB_SYMBOL_PREFIX@zng_inflateValidate
#define zng_inflate_copyright @ZLIB_SYMBOL_PREFIX@zng_inflate_copyright
#define zng_inflate_ensure_window @ZLIB_SYMBOL_PREFIX@zng_inflate_ensure_window
#define zng_inflate_fast @ZLIB_SYMBOL_PREFIX@zng_inflate_fast
#define zng_inflate_table @ZLIB_SYMBOL_PREFIX@zng_inflate_table
#define zng_read_buf @ZLIB_SYMBOL_PREFIX@zng_read_buf
#define zng_uncompress @ZLIB_SYMBOL_PREFIX@zng_uncompress
#define zng_uncompress2 @ZLIB_SYMBOL_PREFIX@zng_uncompress2
#define zng_zError @ZLIB_SYMBOL_PREFIX@zng_zError
#define zng_zcalloc @ZLIB_SYMBOL_PREFIX@zng_zcalloc
#define zng_zcfree @ZLIB_SYMBOL_PREFIX@zng_zcfree
#define zng_zlibCompileFlags @ZLIB_SYMBOL_PREFIX@zng_zlibCompileFlags
#define zng_zlibVersion @ZLIB_SYMBOL_PREFIX@zng_zlibVersion
/* all zlib typedefs in zlib.h and zconf.h */
#define Byte @ZLIB_SYMBOL_PREFIX@Byte
#define Bytef @ZLIB_SYMBOL_PREFIX@Bytef
#define alloc_func @ZLIB_SYMBOL_PREFIX@alloc_func
#define charf @ZLIB_SYMBOL_PREFIX@charf
#define free_func @ZLIB_SYMBOL_PREFIX@free_func
#ifdef WITH_GZFILEOP
# define gzFile @ZLIB_SYMBOL_PREFIX@gzFile
#endif
#define gz_header @ZLIB_SYMBOL_PREFIX@gz_header
#define gz_headerp @ZLIB_SYMBOL_PREFIX@gz_headerp
#define in_func @ZLIB_SYMBOL_PREFIX@in_func
#define intf @ZLIB_SYMBOL_PREFIX@intf
#define out_func @ZLIB_SYMBOL_PREFIX@out_func
#define uInt @ZLIB_SYMBOL_PREFIX@uInt
#define uIntf @ZLIB_SYMBOL_PREFIX@uIntf
#define uLong @ZLIB_SYMBOL_PREFIX@uLong
#define uLongf @ZLIB_SYMBOL_PREFIX@uLongf
#define voidp @ZLIB_SYMBOL_PREFIX@voidp
#define voidpc @ZLIB_SYMBOL_PREFIX@voidpc
#define voidpf @ZLIB_SYMBOL_PREFIX@voidpf
/* all zlib structs in zlib.h and zconf.h */
#define zng_gz_header_s @ZLIB_SYMBOL_PREFIX@zng_gz_header_s
#define internal_state @ZLIB_SYMBOL_PREFIX@internal_state
/* zlib-ng specific symbols */
#define zng_deflate_param @ZLIB_SYMBOL_PREFIX@zng_deflate_param
#define zng_deflate_param_value @ZLIB_SYMBOL_PREFIX@zng_deflate_param_value
#define zng_deflateSetParams @ZLIB_SYMBOL_PREFIX@zng_deflateSetParams
#define zng_deflateGetParams @ZLIB_SYMBOL_PREFIX@zng_deflateGetParams
#define zlibng_version @ZLIB_SYMBOL_PREFIX@zlibng_version
#define zng_vstring @ZLIB_SYMBOL_PREFIX@zng_vstring
#define zng_zError @ZLIB_SYMBOL_PREFIX@zng_zError
#define zng_alloc_aligned @ZLIB_SYMBOL_PREFIX@zng_alloc_aligned
#define zng_free_aligned @ZLIB_SYMBOL_PREFIX@zng_free_aligned
#define zng_get_crc_table @ZLIB_SYMBOL_PREFIX@zng_get_crc_table
#define zng_inflateSyncPoint @ZLIB_SYMBOL_PREFIX@zng_inflateSyncPoint
#define zng_inflateUndermine @ZLIB_SYMBOL_PREFIX@zng_inflateUndermine
#define zng_inflateValidate @ZLIB_SYMBOL_PREFIX@zng_inflateValidate
#define zng_inflateCodesUsed @ZLIB_SYMBOL_PREFIX@zng_inflateCodesUsed
#define zng_inflateResetKeep @ZLIB_SYMBOL_PREFIX@zng_inflateResetKeep
#define zng_deflateResetKeep @ZLIB_SYMBOL_PREFIX@zng_deflateResetKeep
#define zng_gzopen_w @ZLIB_SYMBOL_PREFIX@zng_gzopen_w
#define zng_gzvprintf @ZLIB_SYMBOL_PREFIX@zng_gzvprintf
#endif /* ZLIB_NAME_MANGLING_H */
+8
View File
@@ -0,0 +1,8 @@
/* zlib_name_mangling.h has been automatically generated from
* zlib_name_mangling.h.empty because ZLIB_SYMBOL_PREFIX was NOT set.
*/
#ifndef ZLIB_NAME_MANGLING_H
#define ZLIB_NAME_MANGLING_H
#endif /* ZLIB_NAME_MANGLING_H */
+170
View File
@@ -0,0 +1,170 @@
/* zlib_name_mangling.h has been automatically generated from
* zlib_name_mangling.h.in because ZLIB_SYMBOL_PREFIX was set.
*/
#ifndef ZLIB_NAME_MANGLING_H
#define ZLIB_NAME_MANGLING_H
/* all linked symbols and init macros */
#define _dist_code @ZLIB_SYMBOL_PREFIX@_dist_code
#define _length_code @ZLIB_SYMBOL_PREFIX@_length_code
#define _tr_align @ZLIB_SYMBOL_PREFIX@_tr_align
#define _tr_flush_bits @ZLIB_SYMBOL_PREFIX@_tr_flush_bits
#define _tr_flush_block @ZLIB_SYMBOL_PREFIX@_tr_flush_block
#define _tr_init @ZLIB_SYMBOL_PREFIX@_tr_init
#define _tr_stored_block @ZLIB_SYMBOL_PREFIX@_tr_stored_block
#define _tr_tally @ZLIB_SYMBOL_PREFIX@_tr_tally
#define adler32 @ZLIB_SYMBOL_PREFIX@adler32
#define adler32_combine @ZLIB_SYMBOL_PREFIX@adler32_combine
#define adler32_combine64 @ZLIB_SYMBOL_PREFIX@adler32_combine64
#define adler32_z @ZLIB_SYMBOL_PREFIX@adler32_z
#ifndef Z_SOLO
# define compress @ZLIB_SYMBOL_PREFIX@compress
# define compress2 @ZLIB_SYMBOL_PREFIX@compress2
# define compressBound @ZLIB_SYMBOL_PREFIX@compressBound
#endif
#define crc32 @ZLIB_SYMBOL_PREFIX@crc32
#define crc32_combine @ZLIB_SYMBOL_PREFIX@crc32_combine
#define crc32_combine64 @ZLIB_SYMBOL_PREFIX@crc32_combine64
#define crc32_combine_gen @ZLIB_SYMBOL_PREFIX@crc32_combine_gen
#define crc32_combine_gen64 @ZLIB_SYMBOL_PREFIX@crc32_combine_gen64
#define crc32_combine_op @ZLIB_SYMBOL_PREFIX@crc32_combine_op
#define crc32_z @ZLIB_SYMBOL_PREFIX@crc32_z
#define deflate @ZLIB_SYMBOL_PREFIX@deflate
#define deflateBound @ZLIB_SYMBOL_PREFIX@deflateBound
#define deflateCopy @ZLIB_SYMBOL_PREFIX@deflateCopy
#define deflateEnd @ZLIB_SYMBOL_PREFIX@deflateEnd
#define deflateGetDictionary @ZLIB_SYMBOL_PREFIX@deflateGetDictionary
#define deflateInit @ZLIB_SYMBOL_PREFIX@deflateInit
#define deflateInit2 @ZLIB_SYMBOL_PREFIX@deflateInit2
#define deflateInit2_ @ZLIB_SYMBOL_PREFIX@deflateInit2_
#define deflateInit_ @ZLIB_SYMBOL_PREFIX@deflateInit_
#define deflateParams @ZLIB_SYMBOL_PREFIX@deflateParams
#define deflatePending @ZLIB_SYMBOL_PREFIX@deflatePending
#define deflatePrime @ZLIB_SYMBOL_PREFIX@deflatePrime
#define deflateReset @ZLIB_SYMBOL_PREFIX@deflateReset
#define deflateResetKeep @ZLIB_SYMBOL_PREFIX@deflateResetKeep
#define deflateSetDictionary @ZLIB_SYMBOL_PREFIX@deflateSetDictionary
#define deflateSetHeader @ZLIB_SYMBOL_PREFIX@deflateSetHeader
#define deflateTune @ZLIB_SYMBOL_PREFIX@deflateTune
#define deflate_copyright @ZLIB_SYMBOL_PREFIX@deflate_copyright
#define fill_window @ZLIB_SYMBOL_PREFIX@fill_window
#define fixedtables @ZLIB_SYMBOL_PREFIX@fixedtables
#define flush_pending @ZLIB_SYMBOL_PREFIX@flush_pending
#define get_crc_table @ZLIB_SYMBOL_PREFIX@get_crc_table
#ifndef Z_SOLO
# define gz_error @ZLIB_SYMBOL_PREFIX@gz_error
# define gz_strwinerror @ZLIB_SYMBOL_PREFIX@gz_strwinerror
# define gzbuffer @ZLIB_SYMBOL_PREFIX@gzbuffer
# define gzclearerr @ZLIB_SYMBOL_PREFIX@gzclearerr
# define gzclose @ZLIB_SYMBOL_PREFIX@gzclose
# define gzclose_r @ZLIB_SYMBOL_PREFIX@gzclose_r
# define gzclose_w @ZLIB_SYMBOL_PREFIX@gzclose_w
# define gzdirect @ZLIB_SYMBOL_PREFIX@gzdirect
# define gzdopen @ZLIB_SYMBOL_PREFIX@gzdopen
# define gzeof @ZLIB_SYMBOL_PREFIX@gzeof
# define gzerror @ZLIB_SYMBOL_PREFIX@gzerror
# define gzflush @ZLIB_SYMBOL_PREFIX@gzflush
# define gzfread @ZLIB_SYMBOL_PREFIX@gzfread
# define gzfwrite @ZLIB_SYMBOL_PREFIX@gzfwrite
# define gzgetc @ZLIB_SYMBOL_PREFIX@gzgetc
# define gzgetc_ @ZLIB_SYMBOL_PREFIX@gzgetc_
# define gzgets @ZLIB_SYMBOL_PREFIX@gzgets
# define gzoffset @ZLIB_SYMBOL_PREFIX@gzoffset
# define gzoffset64 @ZLIB_SYMBOL_PREFIX@gzoffset64
# define gzopen @ZLIB_SYMBOL_PREFIX@gzopen
# define gzopen64 @ZLIB_SYMBOL_PREFIX@gzopen64
# ifdef _WIN32
# define gzopen_w @ZLIB_SYMBOL_PREFIX@gzopen_w
# endif
# define gzprintf @ZLIB_SYMBOL_PREFIX@gzprintf
# define gzputc @ZLIB_SYMBOL_PREFIX@gzputc
# define gzputs @ZLIB_SYMBOL_PREFIX@gzputs
# define gzread @ZLIB_SYMBOL_PREFIX@gzread
# define gzrewind @ZLIB_SYMBOL_PREFIX@gzrewind
# define gzseek @ZLIB_SYMBOL_PREFIX@gzseek
# define gzseek64 @ZLIB_SYMBOL_PREFIX@gzseek64
# define gzsetparams @ZLIB_SYMBOL_PREFIX@gzsetparams
# define gztell @ZLIB_SYMBOL_PREFIX@gztell
# define gztell64 @ZLIB_SYMBOL_PREFIX@gztell64
# define gzungetc @ZLIB_SYMBOL_PREFIX@gzungetc
# define gzvprintf @ZLIB_SYMBOL_PREFIX@gzvprintf
# define gzwrite @ZLIB_SYMBOL_PREFIX@gzwrite
#endif
#define inflate @ZLIB_SYMBOL_PREFIX@inflate
#define inflateBack @ZLIB_SYMBOL_PREFIX@inflateBack
#define inflateBackEnd @ZLIB_SYMBOL_PREFIX@inflateBackEnd
#define inflateBackInit @ZLIB_SYMBOL_PREFIX@inflateBackInit
#define inflateBackInit_ @ZLIB_SYMBOL_PREFIX@inflateBackInit_
#define inflateCodesUsed @ZLIB_SYMBOL_PREFIX@inflateCodesUsed
#define inflateCopy @ZLIB_SYMBOL_PREFIX@inflateCopy
#define inflateEnd @ZLIB_SYMBOL_PREFIX@inflateEnd
#define inflateGetDictionary @ZLIB_SYMBOL_PREFIX@inflateGetDictionary
#define inflateGetHeader @ZLIB_SYMBOL_PREFIX@inflateGetHeader
#define inflateInit @ZLIB_SYMBOL_PREFIX@inflateInit
#define inflateInit2 @ZLIB_SYMBOL_PREFIX@inflateInit2
#define inflateInit2_ @ZLIB_SYMBOL_PREFIX@inflateInit2_
#define inflateInit_ @ZLIB_SYMBOL_PREFIX@inflateInit_
#define inflateMark @ZLIB_SYMBOL_PREFIX@inflateMark
#define inflatePrime @ZLIB_SYMBOL_PREFIX@inflatePrime
#define inflateReset @ZLIB_SYMBOL_PREFIX@inflateReset
#define inflateReset2 @ZLIB_SYMBOL_PREFIX@inflateReset2
#define inflateResetKeep @ZLIB_SYMBOL_PREFIX@inflateResetKeep
#define inflateSetDictionary @ZLIB_SYMBOL_PREFIX@inflateSetDictionary
#define inflateSync @ZLIB_SYMBOL_PREFIX@inflateSync
#define inflateSyncPoint @ZLIB_SYMBOL_PREFIX@inflateSyncPoint
#define inflateUndermine @ZLIB_SYMBOL_PREFIX@inflateUndermine
#define inflateValidate @ZLIB_SYMBOL_PREFIX@inflateValidate
#define inflate_copyright @ZLIB_SYMBOL_PREFIX@inflate_copyright
#define inflate_ensure_window @ZLIB_SYMBOL_PREFIX@inflate_ensure_window
#define inflate_fast @ZLIB_SYMBOL_PREFIX@inflate_fast
#define inflate_table @ZLIB_SYMBOL_PREFIX@inflate_table
#define read_buf @ZLIB_SYMBOL_PREFIX@read_buf
#ifndef Z_SOLO
# define uncompress @ZLIB_SYMBOL_PREFIX@uncompress
# define uncompress2 @ZLIB_SYMBOL_PREFIX@uncompress2
#endif
#define zError @ZLIB_SYMBOL_PREFIX@zError
#ifndef Z_SOLO
# define zcalloc @ZLIB_SYMBOL_PREFIX@zcalloc
# define zcfree @ZLIB_SYMBOL_PREFIX@zcfree
#endif
#define zlibCompileFlags @ZLIB_SYMBOL_PREFIX@zlibCompileFlags
#define zlibVersion @ZLIB_SYMBOL_PREFIX@zlibVersion
/* all zlib typedefs in zlib.h and zconf.h */
#define Byte @ZLIB_SYMBOL_PREFIX@Byte
#define Bytef @ZLIB_SYMBOL_PREFIX@Bytef
#define alloc_func @ZLIB_SYMBOL_PREFIX@alloc_func
#define charf @ZLIB_SYMBOL_PREFIX@charf
#define free_func @ZLIB_SYMBOL_PREFIX@free_func
#ifndef Z_SOLO
# define gzFile @ZLIB_SYMBOL_PREFIX@gzFile
#endif
#define gz_header @ZLIB_SYMBOL_PREFIX@gz_header
#define gz_headerp @ZLIB_SYMBOL_PREFIX@gz_headerp
#define in_func @ZLIB_SYMBOL_PREFIX@in_func
#define intf @ZLIB_SYMBOL_PREFIX@intf
#define out_func @ZLIB_SYMBOL_PREFIX@out_func
#define uInt @ZLIB_SYMBOL_PREFIX@uInt
#define uIntf @ZLIB_SYMBOL_PREFIX@uIntf
#define uLong @ZLIB_SYMBOL_PREFIX@uLong
#define uLongf @ZLIB_SYMBOL_PREFIX@uLongf
#define voidp @ZLIB_SYMBOL_PREFIX@voidp
#define voidpc @ZLIB_SYMBOL_PREFIX@voidpc
#define voidpf @ZLIB_SYMBOL_PREFIX@voidpf
/* all zlib structs in zlib.h and zconf.h */
#define gz_header_s @ZLIB_SYMBOL_PREFIX@gz_header_s
#define internal_state @ZLIB_SYMBOL_PREFIX@internal_state
/* all zlib structs in zutil.h */
#define z_errmsg @ZLIB_SYMBOL_PREFIX@z_errmsg
#define z_vstring @ZLIB_SYMBOL_PREFIX@z_vstring
#define zlibng_version @ZLIB_SYMBOL_PREFIX@zlibng_version
/* zlib-ng specific symbols */
#define zng_alloc_aligned @ZLIB_SYMBOL_PREFIX@zng_alloc_aligned
#define zng_free_aligned @ZLIB_SYMBOL_PREFIX@zng_free_aligned
#endif /* ZLIB_NAME_MANGLING_H */
+99
View File
@@ -0,0 +1,99 @@
/* zmemory.h -- Private inline functions used internally in zlib-ng
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef _ZMEMORY_H
#define _ZMEMORY_H
#if defined(__GNUC__) && (__GNUC__ >= 4)
# define HAVE_MAY_ALIAS
#endif
static inline uint16_t zng_memread_2(const void *ptr) {
#if defined(HAVE_MAY_ALIAS)
typedef struct { uint16_t val; } __attribute__ ((__packed__, __may_alias__)) unaligned_uint16_t;
return ((const unaligned_uint16_t *)ptr)->val;
#else
uint16_t val;
memcpy(&val, ptr, sizeof(val));
return val;
#endif
}
static inline uint32_t zng_memread_4(const void *ptr) {
#if defined(HAVE_MAY_ALIAS)
typedef struct { uint32_t val; } __attribute__ ((__packed__, __may_alias__)) unaligned_uint32_t;
return ((const unaligned_uint32_t *)ptr)->val;
#else
uint32_t val;
memcpy(&val, ptr, sizeof(val));
return val;
#endif
}
static inline uint64_t zng_memread_8(const void *ptr) {
#if defined(HAVE_MAY_ALIAS)
typedef struct { uint64_t val; } __attribute__ ((__packed__, __may_alias__)) unaligned_uint64_t;
return ((const unaligned_uint64_t *)ptr)->val;
#else
uint64_t val;
memcpy(&val, ptr, sizeof(val));
return val;
#endif
}
static inline void zng_memwrite_2(void *ptr, uint16_t val) {
#if defined(HAVE_MAY_ALIAS)
typedef struct { uint16_t val; } __attribute__ ((__packed__, __may_alias__)) unaligned_uint16_t;
((unaligned_uint16_t *)ptr)->val = val;
#else
memcpy(ptr, &val, sizeof(val));
#endif
}
static inline void zng_memwrite_4(void *ptr, uint32_t val) {
#if defined(HAVE_MAY_ALIAS)
typedef struct { uint32_t val; } __attribute__ ((__packed__, __may_alias__)) unaligned_uint32_t;
((unaligned_uint32_t *)ptr)->val = val;
#else
memcpy(ptr, &val, sizeof(val));
#endif
}
static inline void zng_memwrite_8(void *ptr, uint64_t val) {
#if defined(HAVE_MAY_ALIAS)
typedef struct { uint64_t val; } __attribute__ ((__packed__, __may_alias__)) unaligned_uint64_t;
((unaligned_uint64_t *)ptr)->val = val;
#else
memcpy(ptr, &val, sizeof(val));
#endif
}
/* Use zng_memread_* instead of memcmp to avoid older compilers not converting memcmp
calls to unaligned comparisons when unaligned access is supported. Use memcmp only when
unaligned support is not available to avoid an extra call to memcpy. */
static inline int32_t zng_memcmp_2(const void *src0, const void *src1) {
#if defined(HAVE_MAY_ALIAS)
return zng_memread_2(src0) != zng_memread_2(src1);
#else
return memcmp(src0, src1, 2);
#endif
}
static inline int32_t zng_memcmp_4(const void *src0, const void *src1) {
#if defined(HAVE_MAY_ALIAS)
return zng_memread_4(src0) != zng_memread_4(src1);
#else
return memcmp(src0, src1, 4);
#endif
}
static inline int32_t zng_memcmp_8(const void *src0, const void *src1) {
#if defined(HAVE_MAY_ALIAS)
return zng_memread_8(src0) != zng_memread_8(src1);
#else
return memcmp(src0, src1, 8);
#endif
}
#endif
+111
View File
@@ -0,0 +1,111 @@
/* zutil.c -- target dependent utility functions for the compression library
* Copyright (C) 1995-2017 Jean-loup Gailly
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "zutil_p.h"
#include "zutil.h"
z_const char * const PREFIX(z_errmsg)[10] = {
(z_const char *)"need dictionary", /* Z_NEED_DICT 2 */
(z_const char *)"stream end", /* Z_STREAM_END 1 */
(z_const char *)"", /* Z_OK 0 */
(z_const char *)"file error", /* Z_ERRNO (-1) */
(z_const char *)"stream error", /* Z_STREAM_ERROR (-2) */
(z_const char *)"data error", /* Z_DATA_ERROR (-3) */
(z_const char *)"insufficient memory", /* Z_MEM_ERROR (-4) */
(z_const char *)"buffer error", /* Z_BUF_ERROR (-5) */
(z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */
(z_const char *)""
};
const char PREFIX3(vstring)[] =
" zlib-ng 2.2.4";
#ifdef ZLIB_COMPAT
const char * Z_EXPORT zlibVersion(void) {
return ZLIB_VERSION;
}
#else
const char * Z_EXPORT zlibng_version(void) {
return ZLIBNG_VERSION;
}
#endif
unsigned long Z_EXPORT PREFIX(zlibCompileFlags)(void) {
unsigned long flags;
flags = 0;
switch ((int)(sizeof(unsigned int))) {
case 2: break;
case 4: flags += 1; break;
case 8: flags += 2; break;
default: flags += 3;
}
switch ((int)(sizeof(unsigned long))) {
case 2: break;
case 4: flags += 1 << 2; break;
case 8: flags += 2 << 2; break;
default: flags += 3 << 2;
}
switch ((int)(sizeof(void *))) {
case 2: break;
case 4: flags += 1 << 4; break;
case 8: flags += 2 << 4; break;
default: flags += 3 << 4;
}
switch ((int)(sizeof(z_off_t))) {
case 2: break;
case 4: flags += 1 << 6; break;
case 8: flags += 2 << 6; break;
default: flags += 3 << 6;
}
#ifdef ZLIB_DEBUG
flags += 1 << 8;
#endif
#ifdef ZLIB_WINAPI
flags += 1 << 10;
#endif
/* Bit 13 reserved for DYNAMIC_CRC_TABLE */
#ifdef NO_GZCOMPRESS
flags += 1L << 16;
#endif
#ifdef NO_GZIP
flags += 1L << 17;
#endif
#ifdef PKZIP_BUG_WORKAROUND
flags += 1L << 20;
#endif
return flags;
}
#ifdef ZLIB_DEBUG
# include <stdlib.h>
# ifndef verbose
# define verbose 0
# endif
int Z_INTERNAL z_verbose = verbose;
void Z_INTERNAL z_error(const char *m) {
fprintf(stderr, "%s\n", m);
exit(1);
}
#endif
/* exported to allow conversion of error code to string for compress() and
* uncompress()
*/
const char * Z_EXPORT PREFIX(zError)(int err) {
return ERR_MSG(err);
}
void Z_INTERNAL *PREFIX(zcalloc)(void *opaque, unsigned items, unsigned size) {
Z_UNUSED(opaque);
return zng_alloc((size_t)items * (size_t)size);
}
void Z_INTERNAL PREFIX(zcfree)(void *opaque, void *ptr) {
Z_UNUSED(opaque);
zng_free(ptr);
}
+140
View File
@@ -0,0 +1,140 @@
#ifndef ZUTIL_H_
#define ZUTIL_H_
/* zutil.h -- internal interface and configuration of the compression library
* Copyright (C) 1995-2024 Jean-loup Gailly, Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* WARNING: this file should *not* be used by applications. It is
part of the implementation of the compression library and is
subject to change. Applications should only use zlib.h.
*/
#include "zbuild.h"
#ifdef ZLIB_COMPAT
# include "zlib.h"
#else
# include "zlib-ng.h"
#endif
typedef unsigned char uch; /* Included for compatibility with external code only */
typedef uint16_t ush; /* Included for compatibility with external code only */
typedef unsigned long ulg;
extern z_const char * const PREFIX(z_errmsg)[10]; /* indexed by 2-zlib_error */
/* (size given to avoid silly warnings with Visual C++) */
#define ERR_MSG(err) PREFIX(z_errmsg)[(err) < -6 || (err) > 2 ? 9 : 2 - (err)]
#define ERR_RETURN(strm, err) return (strm->msg = ERR_MSG(err), (err))
/* To be used only when the state is known to be valid */
/* common constants */
#ifndef DEF_WBITS
# define DEF_WBITS MAX_WBITS
#endif
/* default windowBits for decompression. MAX_WBITS is for compression only */
#define MAX_BITS 15
/* all codes must not exceed MAX_BITS bits */
#define MAX_DIST_EXTRA_BITS 13
/* maximum number of extra distance bits */
#if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
#else
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
#endif
/* default memLevel */
#define STORED_BLOCK 0
#define STATIC_TREES 1
#define DYN_TREES 2
/* The three kinds of block type */
#define STD_MIN_MATCH 3
#define STD_MAX_MATCH 258
/* The minimum and maximum match lengths mandated by the deflate standard */
#define WANT_MIN_MATCH 4
/* The minimum wanted match length, affects deflate_quick, deflate_fast, deflate_medium and deflate_slow */
#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
#define ADLER32_INITIAL_VALUE 1 /* initial adler-32 hash value */
#define CRC32_INITIAL_VALUE 0 /* initial crc-32 hash value */
#define ZLIB_WRAPLEN 6 /* zlib format overhead */
#define GZIP_WRAPLEN 18 /* gzip format overhead */
#define DEFLATE_HEADER_BITS 3
#define DEFLATE_EOBS_BITS 15
#define DEFLATE_PAD_BITS 6
#define DEFLATE_BLOCK_OVERHEAD ((DEFLATE_HEADER_BITS + DEFLATE_EOBS_BITS + DEFLATE_PAD_BITS) >> 3)
/* deflate block overhead: 3 bits for block start + 15 bits for block end + padding to nearest byte */
#define DEFLATE_QUICK_LIT_MAX_BITS 9
#define DEFLATE_QUICK_OVERHEAD(x) ((x * (DEFLATE_QUICK_LIT_MAX_BITS - 8) + 7) >> 3)
/* deflate_quick worst-case overhead: 9 bits per literal, round up to next byte (+7) */
/* target dependencies */
#ifdef AMIGA
# define OS_CODE 1
#endif
#ifdef __370__
# if __TARGET_LIB__ < 0x20000000
# define OS_CODE 4
# elif __TARGET_LIB__ < 0x40000000
# define OS_CODE 11
# else
# define OS_CODE 8
# endif
#endif
#if defined(ATARI) || defined(atarist)
# define OS_CODE 5
#endif
#ifdef OS2
# define OS_CODE 6
#endif
#if defined(MACOS)
# define OS_CODE 7
#endif
#ifdef __acorn
# define OS_CODE 13
#endif
#if defined(_WIN32) && !defined(__CYGWIN__)
# define OS_CODE 10
#endif
#ifdef __APPLE__
# define OS_CODE 19
#endif
/* common defaults */
#ifndef OS_CODE
# define OS_CODE 3 /* assume Unix */
#endif
/* macros */
#define CHECK_VER_STSIZE(_ver,_stsize) ((_ver) == NULL || (_ver)[0] != PREFIX2(VERSION)[0] || (_stsize) != (int32_t)sizeof(PREFIX3(stream)))
/* memory allocation functions */
void Z_INTERNAL *PREFIX(zcalloc)(void *opaque, unsigned items, unsigned size);
void Z_INTERNAL PREFIX(zcfree)(void *opaque, void *ptr);
typedef void *zng_calloc_func(void *opaque, unsigned items, unsigned size);
typedef void zng_cfree_func(void *opaque, void *ptr);
#endif /* ZUTIL_H_ */
+51
View File
@@ -0,0 +1,51 @@
/* zutil_p.h -- Private inline functions used internally in zlib-ng
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef ZUTIL_P_H
#define ZUTIL_P_H
#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_ALIGNED_ALLOC)
# include <stdlib.h>
#elif defined(__FreeBSD__)
# include <stdlib.h>
# include <malloc_np.h>
#elif defined(HAVE_MEMALIGN) || defined(HAVE__ALIGNED_MALLOC)
# include <malloc.h>
#else
/* Fallback, when no other aligned allocation function was found */
# include <stdlib.h>
#endif
/* Function to allocate 16 or 64-byte aligned memory */
static inline void *zng_alloc(size_t size) {
#ifdef HAVE_ALIGNED_ALLOC
/* Size must be a multiple of alignment */
size = (size + (64 - 1)) & ~(64 - 1);
return (void *)aligned_alloc(64, size); /* Defined in C11 */
#elif defined(HAVE_POSIX_MEMALIGN)
void *ptr;
return posix_memalign(&ptr, 64, size) ? NULL : ptr;
#elif defined(HAVE__ALIGNED_MALLOC)
/* Fallback: Use MSVC extensions: _aligned_malloc() / _aligned_free() */
return (void *)_aligned_malloc(size, 64);
#elif defined(HAVE_MEMALIGN)
/* Fallback: Use deprecated GNU extension: memalign() */
return (void *)memalign(64, size);
#else
/* Fallback: Use a normal allocation (On macOS, alignment is 16 bytes) */
/* zlib-ng adjust allocations for [de]compression to be properly aligned */
return (void *)malloc(size);
#endif
}
/* Function that can free aligned memory */
static inline void zng_free(void *ptr) {
#if defined(HAVE__ALIGNED_MALLOC) && !defined(HAVE_POSIX_MEMALIGN) && !defined(HAVE_ALIGNED_ALLOC)
_aligned_free(ptr);
#else
free(ptr);
#endif
}
#endif