· 9 years ago · Feb 11, 2017, 12:32 AM
1diff --git a/.gitattributes b/.gitattributes
2new file mode 100644
3index 0000000..68ec3a1
4--- /dev/null
5+++ b/.gitattributes
6@@ -0,0 +1,4 @@
7+* text=auto
8+*.c text
9+*.h text
10+Makefile text
11diff --git a/.gitignore b/.gitignore
12index 0beb44b..a7f2c3c 100644
13--- a/.gitignore
14+++ b/.gitignore
15@@ -21,7 +21,6 @@
16 /minigzip64
17 /minigzipsh
18 /zlib.pc
19-/CVE-2003-0107
20
21 .DS_Store
22 *.obj
23@@ -33,6 +32,12 @@
24 *.res
25 foo.gz
26 *.manifest
27+*.opensdf
28+*.sln
29+*.sdf
30+*.vcxproj
31+*.vcxproj.filters
32+.vs
33
34 CMakeCache.txt
35 CMakeFiles
36@@ -47,6 +52,16 @@ ztest*
37 configure.log
38 a.out
39
40+/Makefile
41 /arch/arm/Makefile
42 /arch/generic/Makefile
43 /arch/x86/Makefile
44+.kdev4
45+*.kdev4
46+
47+/Debug
48+/example.dir
49+/minigzip.dir
50+/zlib.dir
51+/zlibstatic.dir
52+/win32/Debug
53diff --git a/.travis.yml b/.travis.yml
54index 6080169..264d97d 100644
55--- a/.travis.yml
56+++ b/.travis.yml
57@@ -3,10 +3,15 @@ compiler:
58 - gcc
59 - clang
60 env:
61+ - BUILDDIR=. TOOL="./configure"
62+ - BUILDDIR=. TOOL="./configure --native"
63 - BUILDDIR=. TOOL="./configure --zlib-compat"
64- - BUILDDIR=../build TOOL="../zlib-ng/configure --zlib-compat"
65 - BUILDDIR=. TOOL="./configure --zlib-compat --without-optimizations --without-new-strategies"
66+ - BUILDDIR=../build TOOL="../zlib-ng/configure"
67 - BUILDDIR=. TOOL="cmake ."
68+ - BUILDDIR=. TOOL="cmake -DWITH_NATIVE_INSTRUCTIONS=on ."
69+ - BUILDDIR=. TOOL="cmake -DZLIB_COMPAT=on ."
70+ - BUILDDIR=. TOOL="cmake -DZLIB_COMPAT=on -DWITH_OPTIM=off -DWITH_NEW_STRATEGIES=off ."
71 - BUILDDIR=../build TOOL="cmake ../zlib-ng"
72 script: mkdir -p $BUILDDIR && cd $BUILDDIR &&
73 $TOOL && make && make test
74diff --git a/CMakeLists.txt b/CMakeLists.txt
75index ea9a5a8..0bb530c 100644
76--- a/CMakeLists.txt
77+++ b/CMakeLists.txt
78@@ -1,6 +1,5 @@
79-cmake_minimum_required(VERSION 2.4.4)
80+cmake_minimum_required(VERSION 2.8.4)
81 set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
82-set(CMAKE_LEGACY_CYGWIN_WIN32 0) # Remove when CMake >= 2.8.4 is required
83
84 project(zlib C)
85
86@@ -13,9 +12,25 @@ set(INSTALL_MAN_DIR "${CMAKE_INSTALL_PREFIX}/share/man" CACHE PATH "Installation
87 set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_PREFIX}/share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
88
89 include(CheckTypeSize)
90+include(CheckSymbolExists)
91 include(CheckFunctionExists)
92 include(CheckIncludeFile)
93 include(CheckCSourceCompiles)
94+include(CheckCSourceRuns)
95+include(CheckLibraryExists)
96+include(FeatureSummary)
97+
98+# make sure we use an appropriate BUILD_TYPE by default, "Release" to be exact
99+# this should select the maximum generic optimisation on the current platform (i.e. -O3 for gcc/clang)
100+if(NOT CMAKE_BUILD_TYPE)
101+ set(CMAKE_BUILD_TYPE "Release" CACHE STRING
102+ "Choose the type of build, standard options are: Debug Release RelWithDebInfo MinSizeRel."
103+ FORCE)
104+ add_feature_info(CMAKE_BUILD_TYPE 1 "Build type: ${CMAKE_BUILD_TYPE} (default)")
105+else()
106+ add_feature_info(CMAKE_BUILD_TYPE 1 "Build type: ${CMAKE_BUILD_TYPE} (selected)")
107+endif()
108+
109 enable_testing()
110
111 check_include_file(sys/types.h HAVE_SYS_TYPES_H)
112@@ -25,20 +40,95 @@ check_include_file(stddef.h HAVE_STDDEF_H)
113 #
114 # Options parsing
115 #
116+set(ARCH ${CMAKE_HOST_SYSTEM_PROCESSOR})
117+message(STATUS "Architecture: ${ARCH}")
118
119 option (ZLIB_COMPAT "Compile with zlib compatible API" OFF)
120 if (ZLIB_COMPAT)
121- message(STATUS "ZLIB_COMPAT")
122 add_definitions(-DZLIB_COMPAT)
123 set (WITH_GZFILEOP ON)
124 endif (ZLIB_COMPAT)
125
126 option (WITH_GZFILEOP "Compile with support for gzFile related functions" OFF)
127 if (WITH_GZFILEOP)
128- message(STATUS "WITH_GZFILEOP")
129 add_definitions(-DWITH_GZFILEOP)
130 endif (WITH_GZFILEOP)
131
132+option(WITH_OPTIM "Build with optimisation" ON)
133+option(WITH_NEW_STRATEGIES "Use new strategies" ON)
134+option(WITH_NATIVE_INSTRUCTIONS
135+ "Instruct the compiler to use the full instruction set on this host (gcc/clang -march=native)" OFF)
136+
137+if(${CMAKE_C_COMPILER} MATCHES "icc" OR ${CMAKE_C_COMPILER} MATCHES "icpc" OR ${CMAKE_C_COMPILER} MATCHES "icl")
138+ if(WITH_NATIVE_INSTRUCTIONS)
139+ message(STATUS "Ignoring WITH_NATIVE_INSTRUCTIONS; not supported on this configuration")
140+ endif()
141+ if(CMAKE_HOST_UNIX)
142+ if(NOT SSE2FLAG)
143+ set(SSE2FLAG "-msse2")
144+ endif()
145+ if(NOT SSE4FLAG)
146+ set(SSE4FLAG "-msse4.2")
147+ endif()
148+ else()
149+ if(NOT SSE2FLAG)
150+ set(SSE2FLAG "/arch:SSE2")
151+ endif()
152+ if(NOT SSE4FLAG)
153+ set(SSE4FLAG "/arch:SSE4.2")
154+ endif()
155+ endif()
156+elseif(MSVC)
157+ # TODO. ICC can be used through MSVC. I'm not sure if we'd ever see that combination
158+ # (who'd use cmake from an IDE...) but checking for ICC before checking for MSVC should
159+ # avoid mistakes.
160+ # /Oi ?
161+ if(NOT ${ARCH} MATCHES "AMD64")
162+ set(SSE2FLAG "/arch:SSE2")
163+ endif()
164+ if(WITH_NATIVE_INSTRUCTIONS)
165+ message(STATUS "Ignoring WITH_NATIVE_INSTRUCTIONS; not supported on this configuration")
166+ endif()
167+else()
168+ execute_process(COMMAND ${CMAKE_C_COMPILER} --version OUTPUT_VARIABLE COMPILER_VERSION)
169+ if("${COMPILER_VERSION}" MATCHES "gcc" OR "${COMPILER_VERSION}" MATCHES "clang")
170+ set(__GNUC__ ON)
171+ endif()
172+ if(WITH_NATIVE_INSTRUCTIONS)
173+ if(__GNUC__)
174+ set(NATIVEFLAG "-march=native")
175+ else()
176+ message(STATUS "Ignoring WITH_NATIVE_INSTRUCTIONS; not implemented yet on this configuration")
177+ endif()
178+ endif()
179+ if(NOT NATIVEFLAG)
180+ if(NOT SSE2FLAG)
181+ if(__GNUC__)
182+ set(SSE2FLAG "-msse2")
183+ endif()
184+ endif()
185+ if(NOT SSE4FLAG)
186+ if(__GNUC__)
187+ set(SSE4FLAG "-msse4")
188+ endif()
189+ endif()
190+ if(NOT PCLMULFLAG)
191+ if(__GNUC__)
192+ set(PCLMULFLAG "-mpclmul")
193+ endif()
194+ endif()
195+ else(NOT NATIVEFLAG)
196+ set(SSE2FLAG ${NATIVEFLAG})
197+ set(SSE4FLAG ${NATIVEFLAG})
198+ set(PCLMULFLAG ${NATIVEFLAG})
199+ endif(NOT NATIVEFLAG)
200+endif()
201+
202+add_feature_info(ZLIB_COMPAT ZLIB_COMPAT "Provide a zlib-compatible API")
203+add_feature_info(WITH_GZFILEOP WITH_GZFILEOP "Compile with support for gzFile-related functions")
204+add_feature_info(WITH_OPTIM WITH_OPTIM "Build with optimisation")
205+add_feature_info(WITH_NEW_STRATEGIES WITH_NEW_STRATEGIES "Use new strategies")
206+
207 #
208 # Check to see if we have large file support
209 #
210@@ -67,30 +157,179 @@ endif()
211 set(CMAKE_REQUIRED_DEFINITIONS) # clear variable
212
213 #
214-# Check for fseeko
215+# Check for fseeko and other optional functions
216 #
217 check_function_exists(fseeko HAVE_FSEEKO)
218 if(NOT HAVE_FSEEKO)
219 add_definitions(-DNO_FSEEKO)
220 endif()
221+check_function_exists(strerror HAVE_STRERROR)
222+if(NOT HAVE_STRERROR)
223+ add_definitions(-DNO_STRERROR)
224+endif()
225
226 #
227-# Check for unistd.h
228+# Check for unistd.h and stdarg.h
229 #
230 check_include_file(unistd.h Z_HAVE_UNISTD_H)
231+check_include_file(stdarg.h Z_HAVE_STDARG_H)
232+
233+#
234+# Check if we can hide zlib internal symbols that are linked between separate source files using hidden
235+#
236+check_c_source_compiles(
237+ "#define ZLIB_INTERNAL __attribute__((visibility (\"hidden\")))
238+ int ZLIB_INTERNAL foo;
239+ int main()
240+ {
241+ return 0;
242+ }"
243+ HAVE_ATTRIBUTE_VISIBILITY_HIDDEN FAIL_REGEX "not supported")
244+if(HAVE_ATTRIBUTE_VISIBILITY_HIDDEN)
245+ add_definitions(-DHAVE_HIDDEN)
246+endif()
247+
248+#
249+# Check if we can hide zlib internal symbols that are linked between separate source files using internal
250+#
251+check_c_source_compiles(
252+ "#define ZLIB_INTERNAL __attribute__((visibility (\"internal\")))
253+ int ZLIB_INTERNAL foo;
254+ int main()
255+ {
256+ return 0;
257+ }"
258+ HAVE_ATTRIBUTE_VISIBILITY_INTERNAL FAIL_REGEX "not supported")
259+if(HAVE_ATTRIBUTE_VISIBILITY_INTERNAL)
260+ add_definitions(-DHAVE_INTERNAL)
261+endif()
262+
263+#
264+# check for __builtin_ctzl() support in the compiler
265+#
266+check_c_source_compiles(
267+ "int main(void)
268+ {
269+ unsigned int zero = 0;
270+ long test = __builtin_ctzl(zero);
271+ (void)test;
272+ return 0;
273+ }"
274+ HAVE_BUILTIN_CTZL
275+)
276+if(HAVE_BUILTIN_CTZL)
277+ add_definitions(-DHAVE_BUILTIN_CTZL)
278+endif()
279+
280+set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DZLIB_DEBUG")
281
282 if(MSVC)
283 set(CMAKE_DEBUG_POSTFIX "d")
284 add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
285 add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
286 include_directories(${CMAKE_CURRENT_SOURCE_DIR})
287+else()
288+ #
289+ # not MSVC, so we need to check if we have the MS-style SSE etc. intrinsics
290+ #
291+ if(WITH_NATIVE_INSTRUCTIONS)
292+ set(CMAKE_REQUIRED_FLAGS "${NATIVEFLAG}")
293+ else()
294+ set(CMAKE_REQUIRED_FLAGS "${SSE2FLAG}")
295+ endif()
296+ check_c_source_runs(
297+ "#include <immintrin.h>
298+ int main(void)
299+ {
300+ __m128i zero = _mm_setzero_si128();
301+ (void)zero;
302+ return 0;
303+ }"
304+ HAVE_SSE2_INTRIN
305+ )
306+ if(WITH_NATIVE_INSTRUCTIONS)
307+ set(CMAKE_REQUIRED_FLAGS "${NATIVEFLAG}")
308+ else()
309+ # use the generic SSE4 enabler option to check for the SSE4.2 instruction we require:
310+ set(CMAKE_REQUIRED_FLAGS "${SSE4FLAG}")
311+ endif()
312+ check_c_source_runs(
313+ "int main(void)
314+ {
315+ unsigned val = 0, h = 0;
316+ __asm__ __volatile__ ( \"crc32 %1,%0\" : \"+r\" (h) : \"r\" (val) );
317+ return (int) h;
318+ }"
319+ HAVE_SSE42_INTRIN
320+ )
321+ if(WITH_NATIVE_INSTRUCTIONS)
322+ set(CMAKE_REQUIRED_FLAGS "${NATIVEFLAG}")
323+ else()
324+ # the PCLMUL instruction we use also requires an SSE4.1 instruction check for both
325+ set(CMAKE_REQUIRED_FLAGS "${SSE4FLAG} ${PCLMULFLAG}")
326+ endif()
327+ check_c_source_runs(
328+ "#include <immintrin.h>
329+ #include <smmintrin.h>
330+ #include <wmmintrin.h>
331+ int main(void)
332+ {
333+ __m128i a = _mm_setzero_si128();
334+ __m128i b = _mm_setzero_si128();
335+ __m128i c = _mm_clmulepi64_si128(a, b, 0x10);
336+ int d = _mm_extract_epi32(c, 2);
337+ return d;
338+ }"
339+ HAVE_PCLMULQDQ_INTRIN
340+ )
341 endif()
342
343+#
344+# Enable deflate_medium at level 4-5
345+#
346+if(WITH_NEW_STRATEGIES)
347+ add_definitions(-DMEDIUM_STRATEGY)
348+endif()
349+
350+#
351+# macro to add either the given intrinsics option to the global compiler options,
352+# or ${NATIVEFLAG} (-march=native) if that is appropriate and possible.
353+# An alternative version of this macro would take a file argument, and set ${flag}
354+# only for that file as opposed to ${NATIVEFLAG} globally, to limit side-effect of
355+# using ${flag} globally.
356+#
357+macro(add_intrinsics_option flag)
358+ if(WITH_NATIVE_INSTRUCTIONS AND NATIVEFLAG)
359+ if (NOT "${CMAKE_C_FLAGS} " MATCHES ".*${NATIVEFLAG} .*")
360+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${NATIVEFLAG}")
361+ endif()
362+ else()
363+ if (NOT "${CMAKE_C_FLAGS} " MATCHES ".*${flag} .*")
364+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
365+ endif()
366+ endif()
367+endmacro(add_intrinsics_option)
368+
369+set(ZLIB_ARCH_SRCS)
370+set(ARCHDIR "arch/generic")
371+if("${ARCH}" MATCHES "x86_64" OR "${ARCH}" MATCHES "AMD64")
372+ set(ARCHDIR "arch/x86")
373+ add_definitions(-DX86_64 -DX86_NOCHECK_SSE2 -DUNALIGNED_OK -DUNROLL_LESS)
374+ add_feature_info(SSE2 1 "Use the SSE2 instruction set, using \"${SSE2FLAG}\"")
375+elseif("${ARCH}" MATCHES "arm")
376+ set(ARCHDIR "arch/arm")
377+ add_definitions(-DUNALIGNED_OK -DUNROLL_LESS)
378+else()
379+ set(ARCHDIR "arch/x86")
380+ add_definitions(-DX86 -DUNALIGNED_OK -DUNROLL_LESS)
381+ add_feature_info(SSE2 1 "Support the SSE2 instruction set, using \"${SSE2FLAG}\"")
382+endif()
383 if("${ARCHDIR}" MATCHES "arch/x86" AND WITH_OPTIM)
384 add_definitions("-DX86_CPUID")
385 set(ZLIB_ARCH_SRCS ${ZLIB_ARCH_SRCS} ${ARCHDIR}/x86.c)
386 if(HAVE_SSE42_INTRIN)
387 add_definitions(-DX86_SSE4_2_CRC_HASH)
388+ set(ZLIB_ARCH_SRCS ${ZLIB_ARCH_SRCS} ${ARCHDIR}/insert_string_sse.c)
389 add_feature_info(SSE4_CRC 1 "Support CRC hash generation using the SSE4.2 instruction set, using \"${SSE4FLAG}\"")
390 add_intrinsics_option(${SSE4FLAG})
391 if(WITH_NEW_STRATEGIES)
392@@ -102,7 +341,9 @@ if("${ARCHDIR}" MATCHES "arch/x86" AND WITH_OPTIM)
393 if(HAVE_SSE2_INTRIN)
394 add_definitions(-DX86_SSE2_FILL_WINDOW)
395 set(ZLIB_ARCH_SRCS ${ZLIB_ARCH_SRCS} ${ARCHDIR}/fill_window_sse.c)
396- add_intrinsics_option(${SSE2FLAG})
397+ if(NOT ${ARCH} MATCHES "x86_64")
398+ add_intrinsics_option(${SSE2FLAG})
399+ endif()
400 endif()
401 if(HAVE_PCLMULQDQ_INTRIN)
402 add_definitions(-DX86_PCLMULQDQ_CRC)
403@@ -121,7 +362,7 @@ message(STATUS "Architecture-specific source files: ${ZLIB_ARCH_SRCS}")
404 #============================================================================
405
406 macro(generate_cmakein input output)
407- execute_process(COMMAND sed "/#define ZCONF_H/ a\\\n#cmakedefine Z_HAVE_UNISTD_H\n" -
408+ execute_process(COMMAND sed "/#define ZCONF_H/ a\\\n#cmakedefine Z_HAVE_UNISTD_H\\\n#cmakedefine Z_HAVE_STDARG_H\n"
409 INPUT_FILE ${input}
410 OUTPUT_FILE ${output}
411 )
412@@ -157,7 +398,7 @@ configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zlib.pc.cmakein
413 ${ZLIB_PC} @ONLY)
414 configure_file( ${CMAKE_CURRENT_BINARY_DIR}/zconf.h.cmakein
415 ${CMAKE_CURRENT_BINARY_DIR}/zconf.h @ONLY)
416-include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR})
417+include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
418
419
420 #============================================================================
421@@ -236,8 +477,8 @@ if(MINGW OR MSYS)
422 set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
423 endif(MINGW OR MSYS)
424
425-add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_GZFILE_SRCS} ${ZLIB_ASMS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
426-add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_GZFILE_SRCS} ${ZLIB_ASMS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
427+add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_GZFILE_SRCS} ${ZLIB_ARCH_SRCS} ${ZLIB_ASMS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
428+add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_GZFILE_SRCS} ${ZLIB_ARCH_SRCS} ${ZLIB_ASMS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
429
430 set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
431 set_target_properties(zlib PROPERTIES SOVERSION 1)
432@@ -304,3 +545,5 @@ if(HAVE_OFF64_T)
433 target_link_libraries(minigzip64 zlib)
434 set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
435 endif()
436+
437+FEATURE_SUMMARY(WHAT ALL INCLUDE_QUIET_PACKAGES)
438diff --git a/ChangeLog.zlib b/ChangeLog.zlib
439index 909d11f..2adb468 100644
440--- a/ChangeLog.zlib
441+++ b/ChangeLog.zlib
442@@ -200,7 +200,7 @@ Changes in 1.2.5.2 (17 Dec 2011)
443 - Add a transparent write mode to gzopen() when 'T' is in the mode
444 - Update python link in zlib man page
445 - Get inffixed.h and MAKEFIXED result to match
446-- Add a ./config --solo option to make zlib subset with no libary use
447+- Add a ./config --solo option to make zlib subset with no library use
448 - Add undocumented inflateResetKeep() function for CAB file decoding
449 - Add --cover option to ./configure for gcc coverage testing
450 - Add #define ZLIB_CONST option to use const in the z_stream interface
451@@ -1273,7 +1273,7 @@ Changes in 1.0.1 (20 May 96) [1.0 skipped to avoid confusion]
452 - fix array overlay in deflate.c which sometimes caused bad compressed data
453 - fix inflate bug with empty stored block
454 - fix MSDOS medium model which was broken in 0.99
455-- fix deflateParams() which could generated bad compressed data.
456+- fix deflateParams() which could generate bad compressed data.
457 - Bytef is define'd instead of typedef'ed (work around Borland bug)
458 - added an INDEX file
459 - new makefiles for DJGPP (Makefile.dj2), 32-bit Borland (Makefile.b32),
460diff --git a/Makefile b/Makefile
461deleted file mode 100644
462index 6bba86c..0000000
463--- a/Makefile
464+++ /dev/null
465@@ -1,5 +0,0 @@
466-all:
467- -@echo "Please use ./configure first. Thank you."
468-
469-distclean:
470- make -f Makefile.in distclean
471diff --git a/Makefile.in b/Makefile.in
472index c69175e..0ecafdf 100644
473--- a/Makefile.in
474+++ b/Makefile.in
475@@ -16,14 +16,15 @@ CC=cc
476
477 CFLAGS=-O
478 #CFLAGS=-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7
479-#CFLAGS=-g -DDEBUG
480+#CFLAGS=-g -DZLIB_DEBUG
481 #CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \
482 # -Wstrict-prototypes -Wmissing-prototypes
483
484 SFLAGS=-O
485-LDFLAGS=
486-TEST_LDFLAGS=-L. libz.a
487+LDFLAGS=-L.
488+TEST_LIBS=libz.a
489 LDSHARED=$(CC)
490+LDSHAREDFLAGS=-shared
491
492 STATICLIB=libz.a
493 SHAREDLIB=libz.so
494@@ -87,6 +88,8 @@ all64: example64$(EXE) minigzip64$(EXE)
495
496 check: test
497
498+.SECONDARY:
499+
500 $(ARCHDIR)/%.o: $(SRCDIR)/$(ARCHDIR)/%.c
501 $(MAKE) -C $(ARCHDIR) $(notdir $@)
502
503@@ -99,14 +102,44 @@ $(ARCHDIR)/%.lo: $(SRCDIR)/$(ARCHDIR)/%.c
504 %.lo: $(ARCHDIR)/%.lo
505 -cp $< $@
506
507-test: all
508- $(MAKE) -C test
509+test: all teststatic testshared
510+
511+teststatic: static
512+ @TMPST=tmpst_$$; \
513+ if echo hello world | ./minigzip | ./minigzip -d && ./example $$TMPST ; then \
514+ echo ' *** zlib test OK ***'; \
515+ else \
516+ echo ' *** zlib test FAILED ***'; false; \
517+ fi; \
518+ rm -f $$TMPST
519+
520+testshared: shared
521+ @LD_LIBRARY_PATH=`pwd`:$(LD_LIBRARY_PATH) ; export LD_LIBRARY_PATH; \
522+ LD_LIBRARYN32_PATH=`pwd`:$(LD_LIBRARYN32_PATH) ; export LD_LIBRARYN32_PATH; \
523+ DYLD_LIBRARY_PATH=`pwd`:$(DYLD_LIBRARY_PATH) ; export DYLD_LIBRARY_PATH; \
524+ SHLIB_PATH=`pwd`:$(SHLIB_PATH) ; export SHLIB_PATH; \
525+ TMPSH=tmpsh_$$; \
526+ if echo hello world | ./minigzipsh | ./minigzipsh -d && ./examplesh $$TMPSH; then \
527+ echo ' *** zlib shared test OK ***'; \
528+ else \
529+ echo ' *** zlib shared test FAILED ***'; false; \
530+ fi; \
531+ rm -f $$TMPSH
532+
533+test64: all64
534+ @TMP64=tmp64_$$; \
535+ if echo hello world | ./minigzip64 | ./minigzip64 -d && ./example64 $$TMP64; then \
536+ echo ' *** zlib 64-bit test OK ***'; \
537+ else \
538+ echo ' *** zlib 64-bit test FAILED ***'; false; \
539+ fi; \
540+ rm -f $$TMP64
541
542 infcover.o: $(SRCDIR)/test/infcover.c $(SRCDIR)/zlib.h zconf.h
543 $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/test/infcover.c
544
545 infcover$(EXE): infcover.o $(STATICLIB)
546- $(CC) $(CFLAGS) -o $@ infcover.o $(STATICLIB)
547+ $(CC) $(LDFLAGS) -o $@ infcover.o $(STATICLIB)
548 ifneq ($(STRIP),)
549 $(STRIP) $@
550 endif
551@@ -127,10 +160,10 @@ minigzip.o: $(SRCDIR)/test/minigzip.c $(SRCDIR)/zlib.h zconf.h
552 $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/test/minigzip.c
553
554 example64.o: $(SRCDIR)/test/example.c $(SRCDIR)/zlib.h zconf.h
555- $(CC) $(CFLAGS) $(INCLUDES) -D_FILE_OFFSET_BITS=64 -c -o $@ $(SRCDIR)/test/example.c
556+ $(CC) $(CFLAGS) -D_FILE_OFFSET_BITS=64 $(INCLUDES) -c -o $@ $(SRCDIR)/test/example.c
557
558 minigzip64.o: $(SRCDIR)/test/minigzip.c $(SRCDIR)/zlib.h zconf.h
559- $(CC) $(CFLAGS) $(INCLUDES) -D_FILE_OFFSET_BITS=64 -c -o $@ $(SRCDIR)/test/minigzip.c
560+ $(CC) $(CFLAGS) -D_FILE_OFFSET_BITS=64 $(INCLUDES) -c -o $@ $(SRCDIR)/test/minigzip.c
561
562 zlibrc.o: win32/zlib1.rc
563 $(RC) $(RCFLAGS) -o $@ win32/zlib1.rc
564@@ -138,14 +171,14 @@ zlibrc.o: win32/zlib1.rc
565 .SUFFIXES: .lo
566
567 %.o: $(SRCDIR)/%.c
568- $(CC) $(INCLUDES) $(CFLAGS) -c -o $@ $<
569+ $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
570
571 %.lo: $(SRCDIR)/%.c
572- $(CC) $(INCLUDES) $(SFLAGS) -DPIC -c -o $@ $<
573+ $(CC) $(SFLAGS) -DPIC $(INCLUDES) -c -o $@ $<
574
575 $(SHAREDTARGET): $(PIC_OBJS) $(DEFFILE) $(RCOBJS)
576 ifneq ($(SHAREDTARGET),)
577- $(LDSHARED) $(SFLAGS) -o $@ $(DEFFILE) $(PIC_OBJS) $(RCOBJS) $(LDSHAREDLIBC) $(LDFLAGS)
578+ $(LDSHARED) $(LDSHAREDFLAGS) $(LDFLAGS) -o $@ $(DEFFILE) $(PIC_OBJS) $(RCOBJS) $(LDSHAREDLIBC)
579 ifneq ($(STRIP),)
580 $(STRIP) $@
581 endif
582@@ -157,37 +190,37 @@ endif
583 endif
584
585 example$(EXE): example.o $(STATICLIB)
586- $(CC) $(CFLAGS) -o $@ example.o $(TEST_LDFLAGS)
587+ $(CC) $(LDFLAGS) -o $@ example.o $(TEST_LIBS)
588 ifneq ($(STRIP),)
589 $(STRIP) $@
590 endif
591
592 minigzip$(EXE): minigzip.o $(STATICLIB)
593- $(CC) $(CFLAGS) -o $@ minigzip.o $(TEST_LDFLAGS)
594+ $(CC) $(LDFLAGS) -o $@ minigzip.o $(TEST_LIBS)
595 ifneq ($(STRIP),)
596 $(STRIP) $@
597 endif
598
599 examplesh$(EXE): example.o $(SHAREDTARGET)
600- $(CC) $(CFLAGS) -o $@ example.o -L. $(SHAREDTARGET)
601+ $(CC) $(LDFLAGS) -o $@ example.o $(SHAREDTARGET)
602 ifneq ($(STRIP),)
603 $(STRIP) $@
604 endif
605
606 minigzipsh$(EXE): minigzip.o $(SHAREDTARGET)
607- $(CC) $(CFLAGS) -o $@ minigzip.o -L. $(SHAREDTARGET)
608+ $(CC) $(LDFLAGS) -o $@ minigzip.o $(SHAREDTARGET)
609 ifneq ($(STRIP),)
610 $(STRIP) $@
611 endif
612
613 example64$(EXE): example64.o $(STATICLIB)
614- $(CC) $(CFLAGS) -o $@ example64.o $(TEST_LDFLAGS)
615+ $(CC) $(LDFLAGS) -o $@ example64.o $(TEST_LIBS)
616 ifneq ($(STRIP),)
617 $(STRIP) $@
618 endif
619
620 minigzip64$(EXE): minigzip64.o $(STATICLIB)
621- $(CC) $(CFLAGS) -o $@ minigzip64.o $(TEST_LDFLAGS)
622+ $(CC) $(LDFLAGS) -o $@ minigzip64.o $(TEST_LIBS)
623 ifneq ($(STRIP),)
624 $(STRIP) $@
625 endif
626@@ -254,7 +287,6 @@ zlib.3.pdf: $(SRCDIR)/zlib.3
627 mostlyclean: clean
628 clean:
629 @if [ -f $(ARCHDIR)/Makefile ]; then $(MAKE) -C $(ARCHDIR) clean; fi
630- @if [ -f test/Makefile ]; then $(MAKE) -C test clean; fi
631 rm -f *.o *.lo *~ \
632 example$(EXE) minigzip$(EXE) examplesh$(EXE) minigzipsh$(EXE) \
633 example64$(EXE) minigzip64$(EXE) \
634@@ -264,66 +296,71 @@ clean:
635 _match.s maketree
636 rm -rf objs
637 rm -f *.gcda *.gcno *.gcov
638- rm -f a.out
639+ rm -f a.out a.exe
640
641 maintainer-clean: distclean
642 distclean: clean
643 @if [ -f $(ARCHDIR)/Makefile ]; then $(MAKE) -C $(ARCHDIR) distclean; fi
644- @if [ -f test/Makefile ]; then $(MAKE) -C test distclean; fi
645- rm -f zlib.pc configure.log zconf.h zconf.h.cmakein
646+ rm -f Makefile zlib.pc configure.log zconf.h zconf.h.cmakein
647 -@rm -f .DS_Store
648-# Reset Makefile if building inside source tree
649- @if [ -f Makefile.in ]; then \
650- printf 'all:\n\t-@echo "Please use ./configure first. Thank you."\n' > Makefile ; \
651- printf '\ndistclean:\n\tmake -f Makefile.in distclean\n' >> Makefile ; \
652- touch -r $(SRCDIR)/Makefile.in Makefile ; fi
653 # Reset zconf.h and zconf.h.cmakein if building inside source tree
654 @if [ -f zconf.h.in ]; then \
655 cp -p $(SRCDIR)/zconf.h.in zconf.h ; \
656 TEMPFILE=zconfh_$$ ; \
657- echo "/#define ZCONF_H/ a\\\\\n#cmakedefine Z_HAVE_UNISTD_H\n" >> $$TEMPFILE &&\
658+ echo "/#define ZCONF_H/ a\\\n#cmakedefine Z_HAVE_UNISTD_H\\n#cmakedefine Z_HAVE_STDARG_H\n" >> $$TEMPFILE &&\
659 sed -f $$TEMPFILE $(SRCDIR)/zconf.h.in > zconf.h.cmakein &&\
660 touch -r $(SRCDIR)/zconf.h.in zconf.h.cmakein &&\
661 rm $$TEMPFILE ; fi
662 # Cleanup these files if building outside source tree
663- @if [ ! -f zlib.3 ]; then rm -f zlib.3.pdf Makefile; fi
664-# Remove arch and test directory if building outside source tree
665+ @if [ ! -f zlib.3 ]; then rm -f zlib.3.pdf; fi
666+# Remove arch directory if building outside source tree
667 @if [ ! -f $(ARCHDIR)/Makefile.in ]; then rm -rf arch; fi
668- @if [ ! -f test/Makefile.in ]; then rm -rf test; fi
669
670 tags:
671 etags $(SRCDIR)/*.[ch]
672
673 depend:
674- makedepend -- $(CFLAGS) -- $(SRCDIR)/*.[ch]
675- makedepend -a -o.lo -- $(SFLAGS) -- $(SRCDIR)/*.[ch]
676- @sed "s=^$(SRCDIR)/\([a-zA-Z0-9_]*\.\(lo\|o\):\)=\1=g" < Makefile > Makefile.tmp
677+ makedepend -Y -- $(CFLAGS) -- $(SRCDIR)/*.c $(SRCDIR)/$(ARCHDIR)/*.c
678+ makedepend -Y -a -o.lo -- $(SFLAGS) -- $(SRCDIR)/*.c $(SRCDIR)/$(ARCHDIR)/*.c
679+ @sed "s=^$(SRCDIR)/\($(ARCHDIR)/\)\?\([a-zA-Z0-9_]*\.\(lo\|o\):\)=\1\2=g" < Makefile > Makefile.tmp
680 @mv -f Makefile.tmp Makefile
681
682 # DO NOT DELETE THIS LINE -- make depend depends on it.
683
684-adler32.o zutil.o: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
685+adler32.o: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
686 gzclose.o gzlib.o gzread.o gzwrite.o: $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/gzguts.h
687 compress.o example.o minigzip.o uncompr.o: $(SRCDIR)/zlib.h zconf.h
688 crc32.o: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/crc32.h
689 deflate.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
690-deflate_fast.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h
691-deflate_medium.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h
692-deflate_slow.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h
693+deflate_fast.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
694+deflate_medium.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
695+deflate_slow.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
696 infback.o inflate.o: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/inftrees.h $(SRCDIR)/inflate.h $(SRCDIR)/inffast.h $(SRCDIR)/inffixed.h
697 inffast.o: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/inftrees.h $(SRCDIR)/inflate.h $(SRCDIR)/inffast.h
698 inftrees.o: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/inftrees.h
699 trees.o: $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/trees.h
700-
701-adler32.lo zutil.lo: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
702+zutil.o: $(SRCDIR)/zutil.h $(SRCDIR)/gzguts.h $(SRCDIR)/zlib.h zconf.h
703+arch/x86/crc_folding.o: $(SRCDIR)/arch/x86/crc_folding.h $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
704+arch/x86/crc_pclmulqdq.o: $(SRCDIR)/arch/x86/x86.h $(SRCDIR)/arch/x86/crc_folding.h $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
705+arch/x86/deflate_quick.o: $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
706+arch/x86/fill_window_sse.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
707+arch/x86/x86.o: $(SRCDIR)/arch/x86/x86.h
708+
709+adler32.lo: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
710 gzclose.lo gzlib.lo gzread.lo gzwrite.lo: $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/gzguts.h
711 compress.lo example.lo minigzip.lo uncompr.lo: $(SRCDIR)/zlib.h zconf.h
712 crc32.lo: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/crc32.h
713 deflate.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
714-deflate_fast.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h
715-deflate_medium.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h
716-deflate_slow.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h
717+deflate_fast.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
718+deflate_medium.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
719+deflate_slow.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
720 infback.lo inflate.lo: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/inftrees.h $(SRCDIR)/inflate.h $(SRCDIR)/inffast.h $(SRCDIR)/inffixed.h
721 inffast.lo: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/inftrees.h $(SRCDIR)/inflate.h $(SRCDIR)/inffast.h
722 inftrees.lo: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/inftrees.h
723 trees.lo: $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/trees.h
724+zutil.lo: $(SRCDIR)/zutil.h $(SRCDIR)/gzguts.h $(SRCDIR)/zlib.h zconf.h
725+arch/x86/crc_folding.lo: $(SRCDIR)/arch/x86/crc_folding.h $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
726+arch/x86/crc_pclmulqdq.lo: $(SRCDIR)/arch/x86/x86.h $(SRCDIR)/arch/x86/crc_folding.h $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
727+arch/x86/deflate_quick.lo: $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
728+arch/x86/fill_window_sse.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
729+arch/x86/x86.lo: $(SRCDIR)/arch/x86/x86.h
730diff --git a/README.md b/README.md
731index 35ea054..c28745a 100644
732--- a/README.md
733+++ b/README.md
734@@ -1,4 +1,6 @@
735-Travis CI: [](https://travis-ci.org/Dead2/zlib-ng/)
736+Travis CI: [](https://travis-ci.org/mtl1979/zlib-ng/)
737+
738+Coverity: [](https://scan.coverity.com/projects/mtl1979-zlib-ng)
739
740 zlib-ng - zlib for the next generation systems
741
742diff --git a/adler32.c b/adler32.c
743index 495101d..81c2639 100644
744--- a/adler32.c
745+++ b/adler32.c
746@@ -80,7 +80,7 @@ uint32_t ZEXPORT adler32(uint32_t adler, const unsigned char *buf, uint32_t len)
747 }
748
749 /* initial Adler-32 value (deferred check for len == 1 speed) */
750- if (buf == Z_NULL)
751+ if (buf == NULL)
752 return 1L;
753
754 /* in case short lengths are provided, keep it somewhat fast */
755diff --git a/arch/arm/Makefile.in b/arch/arm/Makefile.in
756index 759a121..be8c185 100644
757--- a/arch/arm/Makefile.in
758+++ b/arch/arm/Makefile.in
759@@ -7,8 +7,9 @@ CFLAGS=
760 SFLAGS=
761 INCLUDES=
762
763-SRCDIR=
764-SRCTOP=
765+SRCDIR=.
766+SRCTOP=../..
767+TOPDIR=$(SRCTOP)
768
769 all:
770
771diff --git a/arch/generic/Makefile.in b/arch/generic/Makefile.in
772index 759a121..be8c185 100644
773--- a/arch/generic/Makefile.in
774+++ b/arch/generic/Makefile.in
775@@ -7,8 +7,9 @@ CFLAGS=
776 SFLAGS=
777 INCLUDES=
778
779-SRCDIR=
780-SRCTOP=
781+SRCDIR=.
782+SRCTOP=../..
783+TOPDIR=$(SRCTOP)
784
785 all:
786
787diff --git a/arch/x86/Makefile.in b/arch/x86/Makefile.in
788index b327a05..e305779 100644
789--- a/arch/x86/Makefile.in
790+++ b/arch/x86/Makefile.in
791@@ -7,8 +7,13 @@ CFLAGS=
792 SFLAGS=
793 INCLUDES=
794
795+SSE2FLAG=-msse2
796+SSE4FLAG=-msse4
797+PCLMULFLAG=-mpclmul -msse4
798+
799 SRCDIR=.
800 SRCTOP=../..
801+TOPDIR=$(SRCTOP)
802
803 all: x86.o x86.lo fill_window_sse.o fill_window_sse.lo deflate_quick.o deflate_quick.lo insert_string_sse.o insert_string_sse.lo crc_folding.o crc_folding.lo
804
805@@ -19,28 +24,28 @@ x86.lo: $(SRCDIR)/x86.c
806 $(CC) $(SFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/x86.c
807
808 fill_window_sse.o: $(SRCDIR)/fill_window_sse.c
809- $(CC) $(CFLAGS) -msse2 $(INCLUDES) -c -o $@ $(SRCDIR)/fill_window_sse.c
810+ $(CC) $(CFLAGS) $(SSE2FLAG) $(INCLUDES) -c -o $@ $(SRCDIR)/fill_window_sse.c
811
812 fill_window_sse.lo: $(SRCDIR)/fill_window_sse.c
813- $(CC) $(SFLAGS) -msse2 -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/fill_window_sse.c
814+ $(CC) $(SFLAGS) $(SSE2FLAG) -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/fill_window_sse.c
815
816 deflate_quick.o: $(SRCDIR)/deflate_quick.c
817- $(CC) $(CFLAGS) -msse4 $(INCLUDES) -c -o $@ $(SRCDIR)/deflate_quick.c
818+ $(CC) $(CFLAGS) $(SSE4FLAG) $(INCLUDES) -c -o $@ $(SRCDIR)/deflate_quick.c
819
820 deflate_quick.lo: $(SRCDIR)/deflate_quick.c
821- $(CC) $(SFLAGS) -msse4 -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/deflate_quick.c
822+ $(CC) $(SFLAGS) $(SSE4FLAG) -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/deflate_quick.c
823
824 insert_string_sse.o: $(SRCDIR)/insert_string_sse.c
825- $(CC) $(CFLAGS) -msse4 $(INCLUDES) -c -o $@ $(SRCDIR)/insert_string_sse.c
826+ $(CC) $(CFLAGS) $(SSE4FLAG) $(INCLUDES) -c -o $@ $(SRCDIR)/insert_string_sse.c
827
828 insert_string_sse.lo: $(SRCDIR)/insert_string_sse.c
829- $(CC) $(SFLAGS) -msse4 -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/insert_string_sse.c
830+ $(CC) $(SFLAGS) $(SSE4FLAG) -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/insert_string_sse.c
831
832 crc_folding.o: $(SRCDIR)/crc_folding.c
833- $(CC) $(CFLAGS) -mpclmul -msse4 $(INCLUDES) -c -o $@ $(SRCDIR)/crc_folding.c
834+ $(CC) $(CFLAGS) $(PCLMULFLAG) $(INCLUDES) -c -o $@ $(SRCDIR)/crc_folding.c
835
836 crc_folding.lo: $(SRCDIR)/crc_folding.c
837- $(CC) $(SFLAGS) -mpclmul -msse4 -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/crc_folding.c
838+ $(CC) $(SFLAGS) $(PCLMULFLAG) -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/crc_folding.c
839
840 crc_pclmulqdq.o: $(SRCDIR)/crc_pclmulqdq.c
841 $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/crc_pclmulqdq.c
842@@ -56,3 +61,23 @@ clean:
843
844 distclean:
845 rm -f Makefile
846+
847+depend:
848+ makedepend -Y -- $(CFLAGS) -- $(SRCDIR)/*.c
849+ makedepend -Y -a -o.lo -- $(SFLAGS) -- $(SRCDIR)/*.c
850+ @sed "s=^$(SRCDIR)/\([a-zA-Z0-9_]*\.\(lo\|o\):\)=\1=g" < Makefile > Makefile.tmp
851+ @mv -f Makefile.tmp Makefile
852+
853+# DO NOT DELETE THIS LINE -- make depend depends on it.
854+
855+crc_folding.o: $(SRCDIR)/crc_folding.h $(SRCTOP)/deflate.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
856+crc_pclmulqdq.o: $(SRCDIR)/x86.h $(SRCDIR)/crc_folding.h $(SRCTOP)/deflate.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
857+deflate_quick.o: $(SRCTOP)/deflate.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
858+fill_window_sse.o: $(SRCTOP)/deflate.h $(SRCTOP)/deflate_p.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
859+x86.o: $(SRCDIR)/x86.h
860+
861+crc_folding.lo: $(SRCDIR)/crc_folding.h $(SRCTOP)/deflate.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
862+crc_pclmulqdq.lo: $(SRCDIR)/x86.h $(SRCDIR)/crc_folding.h $(SRCTOP)/deflate.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
863+deflate_quick.lo: $(SRCTOP)/deflate.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
864+fill_window_sse.lo: $(SRCTOP)/deflate.h $(SRCTOP)/deflate_p.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
865+x86.lo: $(SRCDIR)/x86.h
866diff --git a/arch/x86/crc_folding.c b/arch/x86/crc_folding.c
867index 04c621d..fe9c4d9 100644
868--- a/arch/x86/crc_folding.c
869+++ b/arch/x86/crc_folding.c
870@@ -54,7 +54,7 @@ ZLIB_INTERNAL void crc_fold_init(deflate_state *const s) {
871 s->strm->adler = 0;
872 }
873
874-local void fold_1(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
875+static void fold_1(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
876 const __m128i xmm_fold4 = _mm_set_epi32(
877 0x00000001, 0x54442bd4,
878 0x00000001, 0xc6e41596);
879@@ -77,7 +77,7 @@ local void fold_1(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1,
880 *xmm_crc3 = _mm_castps_si128(ps_res);
881 }
882
883-local void fold_2(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
884+static void fold_2(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
885 const __m128i xmm_fold4 = _mm_set_epi32(
886 0x00000001, 0x54442bd4,
887 0x00000001, 0xc6e41596);
888@@ -108,7 +108,7 @@ local void fold_2(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1,
889 *xmm_crc3 = _mm_castps_si128(ps_res31);
890 }
891
892-local void fold_3(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
893+static void fold_3(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
894 const __m128i xmm_fold4 = _mm_set_epi32(
895 0x00000001, 0x54442bd4,
896 0x00000001, 0xc6e41596);
897@@ -145,7 +145,7 @@ local void fold_3(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1,
898 *xmm_crc3 = _mm_castps_si128(ps_res32);
899 }
900
901-local void fold_4(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
902+static void fold_4(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
903 const __m128i xmm_fold4 = _mm_set_epi32(
904 0x00000001, 0x54442bd4,
905 0x00000001, 0xc6e41596);
906@@ -190,7 +190,7 @@ local void fold_4(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1,
907 *xmm_crc3 = _mm_castps_si128(ps_res3);
908 }
909
910-local const unsigned ALIGNED_(32) pshufb_shf_table[60] = {
911+static const unsigned ALIGNED_(32) pshufb_shf_table[60] = {
912 0x84838281, 0x88878685, 0x8c8b8a89, 0x008f8e8d, /* shl 15 (16 - 1)/shr1 */
913 0x85848382, 0x89888786, 0x8d8c8b8a, 0x01008f8e, /* shl 14 (16 - 3)/shr2 */
914 0x86858483, 0x8a898887, 0x8e8d8c8b, 0x0201008f, /* shl 13 (16 - 4)/shr3 */
915@@ -208,7 +208,7 @@ local const unsigned ALIGNED_(32) pshufb_shf_table[60] = {
916 0x0201008f, 0x06050403, 0x0a090807, 0x0e0d0c0b /* shl 1 (16 -15)/shr15*/
917 };
918
919-local void partial_fold(deflate_state *const s, const size_t len, __m128i *xmm_crc0, __m128i *xmm_crc1,
920+static void partial_fold(deflate_state *const s, const size_t len, __m128i *xmm_crc0, __m128i *xmm_crc1,
921 __m128i *xmm_crc2, __m128i *xmm_crc3, __m128i *xmm_crc_part) {
922
923 const __m128i xmm_fold4 = _mm_set_epi32(
924@@ -378,7 +378,7 @@ done:
925 CRC_SAVE(s)
926 }
927
928-local const unsigned ALIGNED_(16) crc_k[] = {
929+static const unsigned ALIGNED_(16) crc_k[] = {
930 0xccaa009e, 0x00000000, /* rk1 */
931 0x751997d0, 0x00000001, /* rk2 */
932 0xccaa009e, 0x00000000, /* rk5 */
933@@ -387,11 +387,11 @@ local const unsigned ALIGNED_(16) crc_k[] = {
934 0xdb710640, 0x00000001 /* rk8 */
935 };
936
937-local const unsigned ALIGNED_(16) crc_mask[4] = {
938+static const unsigned ALIGNED_(16) crc_mask[4] = {
939 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000
940 };
941
942-local const unsigned ALIGNED_(16) crc_mask2[4] = {
943+static const unsigned ALIGNED_(16) crc_mask2[4] = {
944 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
945 };
946
947diff --git a/arch/x86/crc_pclmulqdq.c b/arch/x86/crc_pclmulqdq.c
948index 04281ca..a513d6c 100644
949--- a/arch/x86/crc_pclmulqdq.c
950+++ b/arch/x86/crc_pclmulqdq.c
951@@ -14,7 +14,7 @@ ZLIB_INTERNAL void crc_reset(deflate_state *const s) {
952 crc_fold_init(s);
953 return;
954 }
955- s->strm->adler = crc32(0L, Z_NULL, 0);
956+ s->strm->adler = crc32(0L, NULL, 0);
957 }
958
959 ZLIB_INTERNAL void crc_finalize(deflate_state *const s) {
960diff --git a/arch/x86/deflate_quick.c b/arch/x86/deflate_quick.c
961index b5190d5..7596f75 100644
962--- a/arch/x86/deflate_quick.c
963+++ b/arch/x86/deflate_quick.c
964@@ -10,6 +10,10 @@
965 * Erdinc Ozturk <erdinc.ozturk@intel.com>
966 * Jim Kukunas <james.t.kukunas@linux.intel.com>
967 *
968+ * Portions are Copyright (C) 2016 12Sided Technology, LLC.
969+ * Author:
970+ * Phil Vachon <pvachon@12sidedtech.com>
971+ *
972 * For conditions of distribution and use, see copyright notice in zlib.h
973 */
974
975@@ -19,10 +23,14 @@
976 #endif
977 #include "deflate.h"
978
979+#ifdef ZLIB_DEBUG
980+#include <ctype.h>
981+#endif
982+
983 extern void fill_window_sse(deflate_state *s);
984 extern void flush_pending(z_stream *strm);
985
986-local inline long compare258(const unsigned char *const src0, const unsigned char *const src1) {
987+static inline long compare258(const unsigned char *const src0, const unsigned char *const src1) {
988 #ifdef _MSC_VER
989 long cnt;
990
991@@ -111,31 +119,29 @@ local inline long compare258(const unsigned char *const src0, const unsigned cha
992 #endif
993 }
994
995-local const unsigned quick_len_codes[MAX_MATCH-MIN_MATCH+1];
996-local const unsigned quick_dist_codes[8192];
997+static const unsigned quick_len_codes[MAX_MATCH-MIN_MATCH+1];
998+static const unsigned quick_dist_codes[8192];
999
1000-local inline void quick_send_bits(deflate_state *const s, const int value, const int length) {
1001- unsigned code, out, w, b;
1002+static inline void quick_send_bits(deflate_state *const s, const int value, const int length) {
1003+ unsigned out, width, bytes_out;
1004
1005- out = s->bi_buf;
1006- w = s->bi_valid;
1007+ /* Concatenate the new bits with the bits currently in the buffer */
1008+ out = s->bi_buf | (value << s->bi_valid);
1009+ width = s->bi_valid + length;
1010
1011- code = value << s->bi_valid;
1012- out |= code;
1013- w += length;
1014+ /* Taking advantage of the fact that LSB comes first, write to output buffer */
1015+ *(unsigned *)(s->pending_buf + s->pending) = out;
1016
1017- if (s->pending + 4 >= s->pending_buf_size)
1018- flush_pending(s->strm);
1019+ bytes_out = width / 8;
1020
1021- *(unsigned *)(s->pending_buf + s->pending) = out;
1022+ s->pending += bytes_out;
1023
1024- b = w >> 3;
1025- s->pending += b;
1026- s->bi_buf = out >> (b << 3);
1027- s->bi_valid = w - (b << 3);
1028+ /* Shift out the valid LSBs written out */
1029+ s->bi_buf = out >> (bytes_out * 8);
1030+ s->bi_valid = width - (bytes_out * 8);
1031 }
1032
1033-local inline void static_emit_ptr(deflate_state *const s, const int lc, const unsigned dist) {
1034+static inline void static_emit_ptr(deflate_state *const s, const int lc, const unsigned dist) {
1035 unsigned code, len;
1036
1037 code = quick_len_codes[lc] >> 8;
1038@@ -149,30 +155,32 @@ local inline void static_emit_ptr(deflate_state *const s, const int lc, const un
1039
1040 const ct_data static_ltree[L_CODES+2];
1041
1042-local inline void static_emit_lit(deflate_state *const s, const int lit) {
1043+static inline void static_emit_lit(deflate_state *const s, const int lit) {
1044 quick_send_bits(s, static_ltree[lit].Code, static_ltree[lit].Len);
1045 Tracecv(isgraph(lit), (stderr, " '%c' ", lit));
1046 }
1047
1048-local void static_emit_tree(deflate_state *const s, const int flush) {
1049+static void static_emit_tree(deflate_state *const s, const int flush) {
1050 unsigned last;
1051
1052 last = flush == Z_FINISH ? 1 : 0;
1053+ Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
1054 send_bits(s, (STATIC_TREES << 1)+ last, 3);
1055 }
1056
1057-
1058-local void static_emit_end_block(deflate_state *const s, int last) {
1059+static void static_emit_end_block(deflate_state *const s, int last) {
1060 send_code(s, END_BLOCK, static_ltree);
1061+ Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %u\n", last, s->pending, s->strm->total_out));
1062
1063 if (last)
1064 bi_windup(s);
1065
1066 s->block_start = s->strstart;
1067 flush_pending(s->strm);
1068+ s->block_open = 0;
1069 }
1070
1071-local inline Pos quick_insert_string(deflate_state *const s, const Pos str) {
1072+static inline Pos quick_insert_string(deflate_state *const s, const Pos str) {
1073 Pos ret;
1074 unsigned h = 0;
1075
1076@@ -196,9 +204,17 @@ ZLIB_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
1077 IPos hash_head;
1078 unsigned dist, match_len;
1079
1080- static_emit_tree(s, flush);
1081+ if (s->block_open == 0) {
1082+ static_emit_tree(s, flush);
1083+ s->block_open = 1;
1084+ }
1085
1086 do {
1087+ if (s->pending + 4 >= s->pending_buf_size) {
1088+ flush_pending(s->strm);
1089+ return need_more;
1090+ }
1091+
1092 if (s->lookahead < MIN_LOOKAHEAD) {
1093 fill_window_sse(s);
1094 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1095@@ -252,7 +268,7 @@ ZLIB_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
1096 return block_done;
1097 }
1098
1099-local const unsigned quick_len_codes[MAX_MATCH-MIN_MATCH+1] = {
1100+static const unsigned quick_len_codes[MAX_MATCH-MIN_MATCH+1] = {
1101 0x00004007, 0x00002007, 0x00006007, 0x00001007,
1102 0x00005007, 0x00003007, 0x00007007, 0x00000807,
1103 0x00004808, 0x0000c808, 0x00002808, 0x0000a808,
1104@@ -319,7 +335,7 @@ local const unsigned quick_len_codes[MAX_MATCH-MIN_MATCH+1] = {
1105 0x001c230d, 0x001d230d, 0x001e230d, 0x0000a308,
1106 };
1107
1108-local const unsigned quick_dist_codes[8192] = {
1109+static const unsigned quick_dist_codes[8192] = {
1110 0x00000005, 0x00001005, 0x00000805, 0x00001805,
1111 0x00000406, 0x00002406, 0x00001406, 0x00003406,
1112 0x00000c07, 0x00002c07, 0x00004c07, 0x00006c07,
1113diff --git a/arch/x86/fill_window_sse.c b/arch/x86/fill_window_sse.c
1114index d82b1d1..72de0f4 100644
1115--- a/arch/x86/fill_window_sse.c
1116+++ b/arch/x86/fill_window_sse.c
1117@@ -12,6 +12,7 @@
1118
1119 #include <immintrin.h>
1120 #include "deflate.h"
1121+#include "deflate_p.h"
1122
1123 extern int read_buf(z_stream *strm, unsigned char *buf, unsigned size);
1124
1125@@ -107,20 +108,38 @@ ZLIB_INTERNAL void fill_window_sse(deflate_state *s) {
1126 if (s->lookahead + s->insert >= MIN_MATCH) {
1127 unsigned int str = s->strstart - s->insert;
1128 s->ins_h = s->window[str];
1129+#ifndef NOT_TWEAK_COMPILER
1130+ {
1131+ unsigned int insert_cnt = s->insert;
1132+ unsigned int slen;
1133+ if (unlikely(s->lookahead < MIN_MATCH))
1134+ insert_cnt += s->lookahead - MIN_MATCH;
1135+ slen = insert_cnt;
1136+ if (str >= (MIN_MATCH - 2))
1137+ {
1138+ str += 2 - MIN_MATCH;
1139+ insert_cnt += MIN_MATCH - 2;
1140+ }
1141+ if (insert_cnt > 0)
1142+ {
1143+ insert_string(s, str, insert_cnt);
1144+ s->insert -= slen;
1145+ }
1146+ }
1147+#else
1148 if (str >= 1)
1149- UPDATE_HASH(s, s->ins_h, str + 1 - (MIN_MATCH-1));
1150+ insert_string(s, str + 2 - MIN_MATCH, 1);
1151 #if MIN_MATCH != 3
1152- Call UPDATE_HASH() MIN_MATCH-3 more times
1153+#warning Call insert_string() MIN_MATCH-3 more times
1154 #endif
1155 while (s->insert) {
1156- UPDATE_HASH(s, s->ins_h, str);
1157- s->prev[str & s->w_mask] = s->head[s->ins_h];
1158- s->head[s->ins_h] = (Pos)str;
1159+ insert_string(s, str, 1);
1160 str++;
1161 s->insert--;
1162 if (s->lookahead + s->insert < MIN_MATCH)
1163 break;
1164 }
1165+#endif
1166 }
1167 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1168 * but this is not important since only literal bytes will be emitted.
1169diff --git a/arch/x86/insert_string_sse.c b/arch/x86/insert_string_sse.c
1170index 6d4ddae..a927dc0 100644
1171--- a/arch/x86/insert_string_sse.c
1172+++ b/arch/x86/insert_string_sse.c
1173@@ -19,14 +19,14 @@
1174 Pos insert_string_sse(deflate_state *const s, const Pos str, unsigned int count) {
1175 Pos ret = 0;
1176 unsigned int idx;
1177- unsigned *ip, val, h = 0;
1178+ unsigned *ip, val, h;
1179
1180 for (idx = 0; idx < count; idx++) {
1181 ip = (unsigned *)&s->window[str+idx];
1182 val = *ip;
1183 h = 0;
1184
1185- if (s->level >= 6)
1186+ if (s->level >= TRIGGER_LEVEL)
1187 val &= 0xFFFFFF;
1188
1189 #ifdef _MSC_VER
1190diff --git a/arch/x86/x86.c b/arch/x86/x86.c
1191index 4c255af..f09c4af 100644
1192--- a/arch/x86/x86.c
1193+++ b/arch/x86/x86.c
1194@@ -10,18 +10,18 @@
1195
1196 #include "x86.h"
1197
1198-ZLIB_INTERNAL int x86_cpu_has_sse2;
1199-ZLIB_INTERNAL int x86_cpu_has_sse42;
1200-ZLIB_INTERNAL int x86_cpu_has_pclmulqdq;
1201-ZLIB_INTERNAL int x86_cpu_has_tzcnt;
1202-
1203 #ifdef _MSC_VER
1204-#include <intrin.h>
1205+# include <intrin.h>
1206 #else
1207 // Newer versions of GCC and clang come with cpuid.h
1208-#include <cpuid.h>
1209+# include <cpuid.h>
1210 #endif
1211
1212+ZLIB_INTERNAL int x86_cpu_has_sse2;
1213+ZLIB_INTERNAL int x86_cpu_has_sse42;
1214+ZLIB_INTERNAL int x86_cpu_has_pclmulqdq;
1215+ZLIB_INTERNAL int x86_cpu_has_tzcnt;
1216+
1217 static void cpuid(int info, unsigned* eax, unsigned* ebx, unsigned* ecx, unsigned* edx) {
1218 #ifdef _MSC_VER
1219 unsigned int registers[4];
1220diff --git a/compress.c b/compress.c
1221index 9f6f140..d4f99bb 100644
1222--- a/compress.c
1223+++ b/compress.c
1224@@ -23,14 +23,14 @@ int ZEXPORT compress2(unsigned char *dest, size_t *destLen, const unsigned char
1225 size_t sourceLen, int level) {
1226 z_stream stream;
1227 int err;
1228- const unsigned int max = (unsigned int)0 - 1;
1229+ const unsigned int max = (unsigned int)-1;
1230 size_t left;
1231
1232 left = *destLen;
1233 *destLen = 0;
1234
1235- stream.zalloc = (alloc_func)0;
1236- stream.zfree = (free_func)0;
1237+ stream.zalloc = NULL;
1238+ stream.zfree = NULL;
1239 stream.opaque = NULL;
1240
1241 err = deflateInit(&stream, level);
1242diff --git a/configure b/configure
1243index 5892162..d2326bd 100755
1244--- a/configure
1245+++ b/configure
1246@@ -83,6 +83,7 @@ fi
1247
1248 # set defaults before processing command line options
1249 LDCONFIG=${LDCONFIG-"ldconfig"}
1250+LDFLAGS=${LDFLAGS-"-L."}
1251 LDSHAREDLIBC="${LDSHAREDLIBC--lc}"
1252 DEFFILE=
1253 RC=
1254@@ -104,9 +105,14 @@ compat=0
1255 cover=0
1256 build32=0
1257 build64=0
1258+native=0
1259+sse2flag="-msse2"
1260+sse4flag="-msse4"
1261+pclmulflag="-mpclmul -msse4"
1262 without_optimizations=0
1263 without_new_strategies=0
1264 gcc=0
1265+debug=0
1266 old_cc="$CC"
1267 old_cflags="$CFLAGS"
1268 OBJC='$(OBJZ)'
1269@@ -114,8 +120,6 @@ PIC_OBJC='$(PIC_OBJZ)'
1270 INSTALLTARGETS="install-shared install-static"
1271 UNINSTALLTARGETS="uninstall-shared uninstall-static"
1272
1273-TEST="teststatic"
1274-
1275 # leave this script, optionally in a bad way
1276 leave()
1277 {
1278@@ -155,11 +159,13 @@ case "$1" in
1279 --cover) cover=1; shift ;;
1280 -3* | --32) build32=1; shift ;;
1281 -6* | --64) build64=1; shift ;;
1282+ -n | --native) native=1; shift ;;
1283 -a*=* | --archs=*) ARCHS=`echo $1 | sed 's/.*=//'`; shift ;;
1284 --sysconfdir=*) echo "ignored option: --sysconfdir" | tee -a configure.log; shift ;;
1285 --localstatedir=*) echo "ignored option: --localstatedir" | tee -a configure.log; shift ;;
1286 -noopt | --without-optimizations) without_optimizations=1; shift;;
1287 -oldstrat | --without-new-strategies) without_new_strategies=1; shift;;
1288+ -d* | --debug) debug=1; shift ;;
1289 *)
1290 echo "unknown option: $1" | tee -a configure.log
1291 echo "$0 --help for help" | tee -a configure.log
1292@@ -197,6 +203,7 @@ case "$cc" in
1293 esac
1294 case `$cc -v 2>&1` in
1295 *gcc*) gcc=1 ;;
1296+ *clang*) gcc=1 ;;
1297 esac
1298
1299 show $cc -c $test.c
1300@@ -219,28 +226,42 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
1301 ARCH=$GCC_ARCH
1302 fi ;;
1303 esac
1304- CFLAGS="${CFLAGS--O3} ${ARCHS} -Wall"
1305+ CFLAGS="${CFLAGS--O3}"
1306+ if test -n "${ARCHS}"; then
1307+ CFLAGS="${CFLAGS} ${ARCHS}"
1308+ LDFLAGS="${LDFLAGS} ${ARCHS}"
1309+ fi
1310+ CFLAGS="${CFLAGS} -Wall"
1311 SFLAGS="${CFLAGS--O3} -fPIC"
1312- LDFLAGS="${LDFLAGS} ${ARCHS}"
1313 if test $build64 -eq 1; then
1314 CFLAGS="${CFLAGS} -m64"
1315 SFLAGS="${SFLAGS} -m64"
1316 fi
1317+ if test $native -eq 1; then
1318+ sse2flag="-march=native"
1319+ sse4flag="-march=native"
1320+ pclmulflag="-march=native"
1321+ fi
1322 if test "${ZLIBGCCWARN}" = "YES"; then
1323 CFLAGS="${CFLAGS} -Wextra -pedantic"
1324 fi
1325+ if test $debug -eq 1; then
1326+ CFLAGS="${CFLAGS} -DZLIB_DEBUG"
1327+ SFLAGS="${SFLAGS} -DZLIB_DEBUG"
1328+ fi
1329 if test -z "$uname"; then
1330 uname=`(uname -s || echo unknown) 2>/dev/null`
1331 fi
1332 case "$uname" in
1333 Linux* | linux* | GNU | GNU/* | solaris*)
1334- LDSHARED=${LDSHARED-"$cc -shared -Wl,-soname,libz.so.1,--version-script,${SRCDIR}/zlib.map"} ;;
1335+ LDSHARED=${LDSHARED-"$cc"}
1336+ LDSHAREDFLAGS="-shared -Wl,-soname,libz.so.1,--version-script,${SRCDIR}/zlib.map" ;;
1337 *BSD | *bsd* | DragonFly)
1338- LDSHARED=${LDSHARED-"$cc -shared -Wl,-soname,libz.so.1,--version-script,${SRCDIR}/zlib.map"}
1339+ LDSHARED=${LDSHARED-"$cc"}
1340+ LDSHAREDFLAGS="-shared -Wl,-soname,libz.so.1,--version-script,${SRCDIR}/zlib.map"
1341 LDCONFIG="ldconfig -m" ;;
1342 CYGWIN* | Cygwin* | cygwin*)
1343 ARFLAGS="rcs"
1344- CFLAGS="${CFLAGS} -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
1345 SFLAGS="${CFLAGS}"
1346 shared_ext='.dll'
1347 sharedlibdir='${bindir}'
1348@@ -249,7 +270,8 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
1349 SHAREDLIBV=''
1350 SHAREDTARGET=$SHAREDLIB
1351 IMPORTLIB='libz.dll.a'
1352- LDSHARED=${LDSHARED-"$cc -shared -Wl,--out-implib,${IMPORTLIB},--version-script,${SRCDIR}/zlib.map"}
1353+ LDSHARED=${LDSHARED-"$cc"}
1354+ LDSHAREDFLAGS="-shared -Wl,--out-implib,${IMPORTLIB},--version-script,${SRCDIR}/zlib.map"
1355 LDSHAREDLIBC=""
1356 DEFFILE='win32/zlib.def'
1357 RC='windres'
1358@@ -267,7 +289,8 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
1359 SHAREDLIBV=''
1360 SHAREDTARGET=$SHAREDLIB
1361 IMPORTLIB='libz.dll.a'
1362- LDSHARED=${LDSHARED-"$cc -shared -Wl,--out-implib,${IMPORTLIB}"}
1363+ LDSHARED=${LDSHARED-"$cc"}
1364+ LDSHAREDFLAGS="-shared -Wl,--out-implib,${IMPORTLIB}"
1365 LDSHAREDLIBC=""
1366 DEFFILE='win32/zlib.def'
1367 RC='windres'
1368@@ -277,7 +300,7 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
1369 EXE='.exe' ;;
1370 MINGW* | mingw*)
1371 ARFLAGS="rcs"
1372- CFLAGS="${CFLAGS} -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_POSIX_C_SOURCE=200809L -D_GNU_SOURCE=1"
1373+ CFLAGS="${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_GNU_SOURCE=1"
1374 SFLAGS="${CFLAGS}"
1375 shared_ext='.dll'
1376 sharedlibdir='${bindir}'
1377@@ -286,7 +309,8 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
1378 SHAREDLIBV=''
1379 SHAREDTARGET=$SHAREDLIB
1380 IMPORTLIB='libz.dll.a'
1381- LDSHARED=${LDSHARED-"$cc -shared -Wl,--out-implib=${IMPORTLIB} -Wl,--version-script=${SRCDIR}/zlib.map"}
1382+ LDSHARED=${LDSHARED-"$cc"}
1383+ LDSHAREDFLAGS="-shared -Wl,--out-implib=${IMPORTLIB} -Wl,--version-script=${SRCDIR}/zlib.map"
1384 LDSHAREDLIBC=""
1385 DEFFILE='win32/zlib.def'
1386 RC='windres'
1387@@ -301,9 +325,11 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
1388 EXE='.exe' ;;
1389 QNX*) # This is for QNX6. I suppose that the QNX rule below is for QNX2,QNX4
1390 # (alain.bonnefoy@icbt.com)
1391- LDSHARED=${LDSHARED-"$cc -shared -Wl,-hlibz.so.1"} ;;
1392+ LDSHARED=${LDSHARED-"$cc"}
1393+ LDSHAREDFLAGS="-shared -Wl,-hlibz.so.1" ;;
1394 HP-UX*)
1395- LDSHARED=${LDSHARED-"$cc -shared $SFLAGS"}
1396+ LDSHARED=${LDSHARED-"$cc"}
1397+ LDSHAREDFLAGS="-shared"
1398 case `(uname -m || echo unknown) 2>/dev/null` in
1399 ia64)
1400 shared_ext='.so'
1401@@ -318,14 +344,16 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
1402 SHAREDLIBV=libz.$VER$shared_ext
1403 SHAREDLIBM=libz.$VER1$shared_ext
1404 SHAREDTARGET=$SHAREDLIBV
1405- LDSHARED=${LDSHARED-"$cc -dynamiclib -install_name $libdir/$SHAREDLIBM -compatibility_version $VER1 -current_version $VER3"}
1406+ LDSHARED=${LDSHARED-"$cc"}
1407+ LDSHAREDFLAGS="-dynamiclib -install_name $libdir/$SHAREDLIBM -compatibility_version $VER1 -current_version $VER3"
1408 if libtool -V 2>&1 | grep Apple > /dev/null; then
1409 AR="libtool"
1410 else
1411 AR="/usr/bin/libtool"
1412 fi
1413 ARFLAGS="-o" ;;
1414- *) LDSHARED=${LDSHARED-"$cc -shared"} ;;
1415+ *) LDSHARED=${LDSHARED-"$cc"}
1416+ LDSHAREDFLAGS="-shared" ;;
1417 esac
1418 else
1419 # find system name and corresponding cc options
1420@@ -338,8 +366,8 @@ else
1421 case "$uname" in
1422 HP-UX*) SFLAGS=${CFLAGS-"-O +z"}
1423 CFLAGS=${CFLAGS-"-O"}
1424-# LDSHARED=${LDSHARED-"ld -b +vnocompatwarnings"}
1425- LDSHARED=${LDSHARED-"ld -b"}
1426+ LDSHARED=${LDSHARED-"ld"}
1427+ LDSHAREDFLAGS="-b"
1428 case `(uname -m || echo unknown) 2>/dev/null` in
1429 ia64)
1430 shared_ext='.so'
1431@@ -351,11 +379,13 @@ else
1432 AIX*) # Courtesy of dbakker@arrayasolutions.com
1433 SFLAGS=${CFLAGS-"-O -qmaxmem=8192"}
1434 CFLAGS=${CFLAGS-"-O -qmaxmem=8192"}
1435- LDSHARED=${LDSHARED-"xlc -G"} ;;
1436+ LDSHARED=${LDSHARED-"xlc"}
1437+ LDSHAREDFLAGS="-G" ;;
1438 # send working options for other systems to zlib@gzip.org
1439 *) SFLAGS=${CFLAGS-"-O"}
1440 CFLAGS=${CFLAGS-"-O"}
1441- LDSHARED=${LDSHARED-"cc -shared"} ;;
1442+ LDSHARED=${LDSHARED-"cc"}
1443+ LDSHAREDFLAGS="-shared" ;;
1444 esac
1445 fi
1446
1447@@ -426,7 +456,7 @@ if test $shared -eq 1; then
1448 echo Checking for shared library support... | tee -a configure.log
1449 # we must test in two steps (cc then ld), required at least on SunOS 4.x
1450 if try $CC -w -c $SFLAGS $test.c &&
1451- try $LDSHARED $SFLAGS -o $test$shared_ext $test.o; then
1452+ try $LDSHARED $LDSHAREDFLAGS $LDFLAGS -o $test$shared_ext $test.o; then
1453 echo Building shared library $SHAREDTARGET with $CC. | tee -a configure.log
1454 elif test -z "$old_cc" -a -z "$old_cflags"; then
1455 echo No shared library support. | tee -a configure.log
1456@@ -438,7 +468,9 @@ if test $shared -eq 1; then
1457 fi
1458 if test $shared -eq 0; then
1459 LDSHARED="$CC"
1460+ LDSHAREDFLAGS=""
1461 ALL="static"
1462+ TEST="all teststatic"
1463 SHAREDLIB=""
1464 SHAREDLIBV=""
1465 SHAREDLIBM=""
1466@@ -448,7 +480,7 @@ if test $shared -eq 0; then
1467 echo Building static library $STATICLIB version $VER with $CC. | tee -a configure.log
1468 else
1469 ALL="static shared"
1470- TEST="${TEST} testshared"
1471+ TEST="all teststatic testshared"
1472 fi
1473
1474 echo >> configure.log
1475@@ -456,9 +488,6 @@ echo >> configure.log
1476 # check for large file support, and if none, check for fseeko()
1477 cat > $test.c <<EOF
1478 #include <sys/types.h>
1479-#ifdef __MSYS__
1480-# define off64_t _off64_t
1481-#endif
1482 off64_t dummy = 0;
1483 EOF
1484 if try $CC -c $CFLAGS -D_LARGEFILE64_SOURCE=1 $test.c; then
1485@@ -472,6 +501,21 @@ else
1486 echo "Checking for off64_t... No." | tee -a configure.log
1487 echo >> configure.log
1488 cat > $test.c <<EOF
1489+#include <sys/types.h>
1490+int main() {
1491+ _off64_t dummy = 0;
1492+ return 0;
1493+}
1494+EOF
1495+ if try $CC $CFLAGS -o $test $test.c $LDSHAREDLIBC; then
1496+ echo "Checking for _off64_t... Yes." | tee -a configure.log
1497+ ALL="${ALL} all64"
1498+ TEST="${TEST} test64"
1499+ else
1500+ echo "Checking for _off64_t... No." | tee -a configure.log
1501+ fi
1502+ echo >> configure.log
1503+ cat > $test.c <<EOF
1504 #include <stdio.h>
1505 int main(void) {
1506 fseeko(NULL, 0, 0);
1507@@ -655,7 +699,7 @@ int main(void)
1508 return 0;
1509 }
1510 EOF
1511-if try ${CC} ${CFLAGS} -msse2 $test.c; then
1512+if try ${CC} ${CFLAGS} ${sse2flag} $test.c; then
1513 echo "Checking for SSE2 intrinsics ... Yes." | tee -a configure.log
1514 HAVE_SSE2_INTRIN=1
1515 else
1516@@ -676,7 +720,7 @@ int main(void)
1517 return 0;
1518 }
1519 EOF
1520-if try ${CC} ${CFLAGS} -mpclmul $test.c; then
1521+if try ${CC} ${CFLAGS} ${pclmulflag} $test.c; then
1522 echo "Checking for PCLMULQDQ intrinsics ... Yes." | tee -a configure.log
1523 HAVE_PCLMULQDQ_INTRIN=1
1524 else
1525@@ -684,7 +728,7 @@ else
1526 HAVE_PCLMULQDQ_INTRIN=0
1527 fi
1528
1529-# Enable deflate_medium at level 4-6
1530+# Enable deflate_medium at level 4-5
1531 if test $without_new_strategies -eq 0; then
1532 CFLAGS="${CFLAGS} -DMEDIUM_STRATEGY"
1533 SFLAGS="${SFLAGS} -DMEDIUM_STRATEGY"
1534@@ -784,6 +828,7 @@ echo EXE = $EXE >> configure.log
1535 echo LDCONFIG = $LDCONFIG >> configure.log
1536 echo LDFLAGS = $LDFLAGS >> configure.log
1537 echo LDSHARED = $LDSHARED >> configure.log
1538+echo LDSHAREDFLAGS = $LDSHAREDFLAGS >> configure.log
1539 echo LDSHAREDLIBC = $LDSHAREDLIBC >> configure.log
1540 echo DEFFILE = $DEFFILE >> configure.log
1541 echo RC = $RC >> configure.log
1542@@ -814,6 +859,9 @@ echo mandir = $mandir >> configure.log
1543 echo prefix = $prefix >> configure.log
1544 echo sharedlibdir = $sharedlibdir >> configure.log
1545 echo uname = $uname >> configure.log
1546+echo sse2flag = $sse2flag >> configure.log
1547+echo sse4flag = $sse4flag >> configure.log
1548+echo pclmulflag = $pclmulflag >> configure.log
1549 echo ARCHDIR = ${ARCHDIR} >> configure.log
1550 echo ARCH_STATIC_OBJS = ${ARCH_STATIC_OBJS} >> configure.log
1551 echo ARCH_SHARED_OBJS = ${ARCH_SHARED_OBJS} >> configure.log
1552@@ -829,6 +877,7 @@ sed < $SRCDIR/Makefile.in "
1553 /^SFLAGS *=/s#=.*#=$SFLAGS#
1554 /^LDFLAGS *=/s#=.*#=$LDFLAGS#
1555 /^LDSHARED *=/s#=.*#=$LDSHARED#
1556+/^LDSHAREDFLAGS *=/s#=.*#=$LDSHAREDFLAGS#
1557 /^STATICLIB *=/s#=.*#=$STATICLIB#
1558 /^SHAREDLIB *=/s#=.*#=$SHAREDLIB#
1559 /^SHAREDLIBV *=/s#=.*#=$SHAREDLIBV#
1560@@ -858,6 +907,7 @@ sed < $SRCDIR/Makefile.in "
1561 /^OBJC *=/s#=.*#= $OBJC#
1562 /^PIC_OBJC *=/s#=.*#= $PIC_OBJC#
1563 /^all: */s#:.*#: $ALL#
1564+/^test: */s#:.*#: $TEST#
1565 /^install-libs: */s#:.*#: $INSTALLTARGETS#
1566 /^uninstall-libs: */s#:.*#: $UNINSTALLTARGETS#
1567 /^ARCHDIR *=/s#=.*#=$ARCHDIR#
1568@@ -878,29 +928,18 @@ sed < $SRCDIR/$ARCHDIR/Makefile.in "
1569 /^INCLUDES *=/s#=.*#=$ARCHINCLUDES#
1570 /^SRCDIR *=/s#=.*#=$SRCDIR/$ARCHDIR#
1571 /^SRCTOP *=/s#=.*#=$SRCDIR#
1572+/^TOPDIR *=/s#=.*#=$BUILDDIR#
1573+/^SSE2FLAG *=/s#=.*#=$sse2flag#
1574+/^SSE4FLAG *=/s#=.*#=$sse4flag#
1575+/^PCLMULFLAG *=/s#=.*#=$pclmulflag#
1576 " > $ARCHDIR/Makefile
1577
1578-# Generate Makefile in test dir
1579-mkdir -p test
1580-TESTINCLUDES="-I$SRCDIR"
1581-if [ "$SRCDIR" != "$BUILDDIR" ]; then TESTINCLUDES="${TESTINCLUDES} -I$BUILDDIR"; fi
1582-if test $compat -eq 1; then COMPATTESTS="compattests"; fi
1583-sed < $SRCDIR/test/Makefile.in "
1584-/^CC *=/s#=.*#=$CC#
1585-/^CFLAGS *=/s#=.*#=$CFLAGS#
1586-/^EXE *=/s#=.*#=$EXE#
1587-/^oldtests: */s#:.*#: $TEST#
1588-/^INCLUDES *=/s#=.*#=$TESTINCLUDES#
1589-/^SRCDIR *=/s#=.*#=$SRCDIR/test#
1590-/^SRCTOP *=/s#=.*#=$SRCDIR#
1591-/^COMPATTESTS *=/s#=.*#=$COMPATTESTS#
1592-" > test/Makefile
1593-
1594 # create zlib.pc with the configure results
1595 sed < $SRCDIR/zlib.pc.in "
1596 /^CC *=/s#=.*#=$CC#
1597 /^CFLAGS *=/s#=.*#=$CFLAGS#
1598 /^LDSHARED *=/s#=.*#=$LDSHARED#
1599+/^LDSHAREDFLAGS *=/s#=.*#=$LDSHAREDFLAGS#
1600 /^STATICLIB *=/s#=.*#=$STATICLIB#
1601 /^SHAREDLIB *=/s#=.*#=$SHAREDLIB#
1602 /^SHAREDLIBV *=/s#=.*#=$SHAREDLIBV#
1603diff --git a/crc32.c b/crc32.c
1604index 4e88a1e..8787183 100644
1605--- a/crc32.c
1606+++ b/crc32.c
1607@@ -208,7 +208,7 @@ const uint32_t * ZEXPORT get_crc_table(void) {
1608 }
1609
1610 uint32_t ZEXPORT crc32(uint32_t crc, const unsigned char *buf, z_off64_t len) {
1611- if (buf == Z_NULL) return 0;
1612+ if (buf == NULL) return 0;
1613
1614 #ifdef DYNAMIC_CRC_TABLE
1615 if (crc_table_empty)
1616@@ -255,6 +255,18 @@ ZLIB_INTERNAL uint32_t crc32_generic(uint32_t crc, const unsigned char *buf, z_o
1617 }
1618
1619
1620+/*
1621+ This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit
1622+ integer pointer type. This violates the strict aliasing rule, where a
1623+ compiler can assume, for optimization purposes, that two pointers to
1624+ fundamentally different types won't ever point to the same memory. This can
1625+ manifest as a problem only if one of the pointers is written to. This code
1626+ only reads from those pointers. So long as this code remains isolated in
1627+ this compilation unit, there won't be a problem. For this reason, this code
1628+ should not be copied and pasted into a compilation unit in which other code
1629+ writes to the buffer that is passed to these routines.
1630+ */
1631+
1632 /* ========================================================================= */
1633 #if BYTE_ORDER == LITTLE_ENDIAN
1634 #define DOLIT4 c ^= *buf4++; \
1635@@ -299,7 +311,7 @@ ZLIB_INTERNAL uint32_t crc32_little(uint32_t crc, const unsigned char *buf, z_of
1636
1637 /* ========================================================================= */
1638 #if BYTE_ORDER == BIG_ENDIAN
1639-#define DOBIG4 c ^= *++buf4; \
1640+#define DOBIG4 c ^= *buf4++; \
1641 c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
1642 crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
1643 #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
1644@@ -317,7 +329,6 @@ ZLIB_INTERNAL uint32_t crc32_big(uint32_t crc, const unsigned char *buf, z_off64
1645 }
1646
1647 buf4 = (const uint32_t *)(const void *)buf;
1648- buf4--;
1649
1650 #ifndef UNROLL_LESS
1651 while (len >= 32) {
1652@@ -330,7 +341,6 @@ ZLIB_INTERNAL uint32_t crc32_big(uint32_t crc, const unsigned char *buf, z_off64
1653 DOBIG4;
1654 len -= 4;
1655 }
1656- buf4++;
1657 buf = (const unsigned char *)buf4;
1658
1659 if (len) do {
1660@@ -429,7 +439,7 @@ uint32_t ZEXPORT crc32_combine64(uint32_t crc1, uint32_t crc2, z_off64_t len2) {
1661
1662 #ifndef X86_PCLMULQDQ_CRC
1663 ZLIB_INTERNAL void crc_reset(deflate_state *const s) {
1664- s->strm->adler = crc32(0L, Z_NULL, 0);
1665+ s->strm->adler = crc32(0L, NULL, 0);
1666 }
1667
1668 ZLIB_INTERNAL void copy_with_crc(z_stream *strm, unsigned char *dst, unsigned long size) {
1669diff --git a/deflate.c b/deflate.c
1670index 1a1fcda..c0faabd 100644
1671--- a/deflate.c
1672+++ b/deflate.c
1673@@ -1,5 +1,5 @@
1674 /* deflate.c -- compress data using the deflation algorithm
1675- * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
1676+ * Copyright (C) 1995-2016 Jean-loup Gailly and Mark Adler
1677 * For conditions of distribution and use, see copyright notice in zlib.h
1678 */
1679
1680@@ -53,7 +53,7 @@
1681 #include "deflate_p.h"
1682 #include "match.h"
1683
1684-const char deflate_copyright[] = " deflate 1.2.8.f Copyright 1995-2013 Jean-loup Gailly and Mark Adler ";
1685+const char deflate_copyright[] = " deflate 1.2.8.f Copyright 1995-2016 Jean-loup Gailly and Mark Adler ";
1686 /*
1687 If you use the zlib library in a product, an acknowledgment is welcome
1688 in the documentation of your product. If for some reason you cannot
1689@@ -68,20 +68,21 @@ const char deflate_copyright[] = " deflate 1.2.8.f Copyright 1995-2013 Jean-loup
1690 typedef block_state (*compress_func) (deflate_state *s, int flush);
1691 /* Compression function. Returns the block state after the call. */
1692
1693-void fill_window (deflate_state *s);
1694-local block_state deflate_stored (deflate_state *s, int flush);
1695-block_state deflate_fast (deflate_state *s, int flush);
1696-block_state deflate_quick (deflate_state *s, int flush);
1697+static int deflateStateCheck (z_streamp strm);
1698+static void slide_hash (deflate_state *s);
1699+static block_state deflate_stored (deflate_state *s, int flush);
1700+block_state deflate_fast (deflate_state *s, int flush);
1701+block_state deflate_quick (deflate_state *s, int flush);
1702 #ifdef MEDIUM_STRATEGY
1703-block_state deflate_medium (deflate_state *s, int flush);
1704+block_state deflate_medium (deflate_state *s, int flush);
1705 #endif
1706-block_state deflate_slow (deflate_state *s, int flush);
1707-local block_state deflate_rle (deflate_state *s, int flush);
1708-local block_state deflate_huff (deflate_state *s, int flush);
1709-local void lm_init (deflate_state *s);
1710-local void putShortMSB (deflate_state *s, uint16_t b);
1711-ZLIB_INTERNAL void flush_pending (z_stream *strm);
1712-ZLIB_INTERNAL int read_buf (z_stream *strm, unsigned char *buf, unsigned size);
1713+block_state deflate_slow (deflate_state *s, int flush);
1714+static block_state deflate_rle (deflate_state *s, int flush);
1715+static block_state deflate_huff (deflate_state *s, int flush);
1716+static void lm_init (deflate_state *s);
1717+static void putShortMSB (deflate_state *s, uint16_t b);
1718+ZLIB_INTERNAL void flush_pending (z_stream *strm);
1719+ZLIB_INTERNAL int read_buf (z_stream *strm, unsigned char *buf, unsigned size);
1720
1721 extern void crc_reset(deflate_state *const s);
1722 #ifdef X86_PCLMULQDQ_CRC
1723@@ -109,7 +110,7 @@ typedef struct config_s {
1724 compress_func func;
1725 } config;
1726
1727-local const config configuration_table[10] = {
1728+static const config configuration_table[10] = {
1729 /* good lazy nice chain */
1730 /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
1731
1732@@ -124,16 +125,22 @@ local const config configuration_table[10] = {
1733 /* 3 */ {4, 6, 32, 32, deflate_fast},
1734
1735 #ifdef MEDIUM_STRATEGY
1736+# ifdef NOT_TWEAK_COMPILER
1737 /* 4 */ {4, 4, 16, 16, deflate_medium}, /* lazy matches */
1738 /* 5 */ {8, 16, 32, 32, deflate_medium},
1739 /* 6 */ {8, 16, 128, 128, deflate_medium},
1740-#else
1741-/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
1742-/* 5 */ {8, 16, 32, 32, deflate_slow},
1743+# else
1744+/* 4 */ {4, 8, 16, 16, deflate_medium}, /* lazy matches */
1745+/* 5 */ {8, 16, 64, 128, deflate_medium},
1746 /* 6 */ {8, 16, 128, 128, deflate_slow},
1747+# endif /* NOT_TWEAK_COMPILER */
1748+#else
1749+/* 4 */ {4, 12, 32, 32, deflate_slow}, /* lazy matches */
1750+/* 5 */ {8, 16, 48, 64, deflate_slow},
1751+/* 6 */ {8, 32, 128, 128, deflate_slow},
1752 #endif
1753
1754-/* 7 */ {8, 32, 128, 256, deflate_slow},
1755+/* 7 */ {8, 32, 128, 256, deflate_slow},
1756 /* 8 */ {32, 128, 258, 1024, deflate_slow},
1757 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
1758
1759@@ -142,9 +149,6 @@ local const config configuration_table[10] = {
1760 * meaning.
1761 */
1762
1763-#define EQUAL 0
1764-/* result of memcmp for equal strings */
1765-
1766 /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
1767 #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
1768
1769@@ -157,6 +161,36 @@ local const config configuration_table[10] = {
1770 s->head[s->hash_size-1] = NIL; \
1771 memset((unsigned char *)s->head, 0, (unsigned)(s->hash_size-1)*sizeof(*s->head));
1772
1773+/* ===========================================================================
1774+ * Slide the hash table when sliding the window down (could be avoided with 32
1775+ * bit values at the expense of memory usage). We slide even when level == 0 to
1776+ * keep the hash table consistent if we switch back to level > 0 later.
1777+ */
1778+static void slide_hash(deflate_state *s)
1779+{
1780+ unsigned n, m;
1781+ Pos *p;
1782+ unsigned int wsize = s->w_size;
1783+
1784+ n = s->hash_size;
1785+ p = &s->head[n];
1786+ do {
1787+ m = *--p;
1788+ *p = (Pos)(m >= wsize ? m - wsize : NIL);
1789+ } while (--n);
1790+ n = wsize;
1791+#ifndef FASTEST
1792+ p = &s->prev[n];
1793+ do {
1794+ m = *--p;
1795+ *p = (Pos)(m >= wsize ? m - wsize : NIL);
1796+ /* If n is not on any hash chain, prev[n] is garbage but
1797+ * its value will never be used.
1798+ */
1799+ } while (--n);
1800+#endif
1801+}
1802+
1803 /* ========================================================================= */
1804 int ZEXPORT deflateInit_(z_stream *strm, int level, const char *version, int stream_size) {
1805 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, version, stream_size);
1806@@ -180,18 +214,18 @@ int ZEXPORT deflateInit2_(z_stream *strm, int level, int method, int windowBits,
1807 x86_check_features();
1808 #endif
1809
1810- if (version == Z_NULL || version[0] != my_version[0] || stream_size != sizeof(z_stream)) {
1811+ if (version == NULL || version[0] != my_version[0] || stream_size != sizeof(z_stream)) {
1812 return Z_VERSION_ERROR;
1813 }
1814- if (strm == Z_NULL)
1815+ if (strm == NULL)
1816 return Z_STREAM_ERROR;
1817
1818- strm->msg = Z_NULL;
1819- if (strm->zalloc == (alloc_func)0) {
1820+ strm->msg = NULL;
1821+ if (strm->zalloc == NULL) {
1822 strm->zalloc = zcalloc;
1823 strm->opaque = NULL;
1824 }
1825- if (strm->zfree == (free_func)0)
1826+ if (strm->zfree == NULL)
1827 strm->zfree = zcfree;
1828
1829 if (level == Z_DEFAULT_COMPRESSION)
1830@@ -207,7 +241,8 @@ int ZEXPORT deflateInit2_(z_stream *strm, int level, int method, int windowBits,
1831 #endif
1832 }
1833 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || windowBits < 8 ||
1834- windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
1835+ windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED ||
1836+ (windowBits == 8 && wrap != 1)) {
1837 return Z_STREAM_ERROR;
1838 }
1839 if (windowBits == 8)
1840@@ -219,13 +254,14 @@ int ZEXPORT deflateInit2_(z_stream *strm, int level, int method, int windowBits,
1841 #endif
1842
1843 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
1844- if (s == Z_NULL)
1845+ if (s == NULL)
1846 return Z_MEM_ERROR;
1847 strm->state = (struct internal_state *)s;
1848 s->strm = strm;
1849+ s->status = INIT_STATE; /* to pass state test in deflateReset() */
1850
1851 s->wrap = wrap;
1852- s->gzhead = Z_NULL;
1853+ s->gzhead = NULL;
1854 s->w_bits = windowBits;
1855 s->w_size = 1 << s->w_bits;
1856 s->w_mask = s->w_size - 1;
1857@@ -257,8 +293,8 @@ int ZEXPORT deflateInit2_(z_stream *strm, int level, int method, int windowBits,
1858 s->pending_buf = (unsigned char *) overlay;
1859 s->pending_buf_size = (unsigned long)s->lit_bufsize * (sizeof(uint16_t)+2L);
1860
1861- if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
1862- s->pending_buf == Z_NULL) {
1863+ if (s->window == NULL || s->prev == NULL || s->head == NULL ||
1864+ s->pending_buf == NULL) {
1865 s->status = FINISH_STATE;
1866 strm->msg = ERR_MSG(Z_MEM_ERROR);
1867 deflateEnd(strm);
1868@@ -270,10 +306,35 @@ int ZEXPORT deflateInit2_(z_stream *strm, int level, int method, int windowBits,
1869 s->level = level;
1870 s->strategy = strategy;
1871 s->method = (unsigned char)method;
1872+ s->block_open = 0;
1873
1874 return deflateReset(strm);
1875 }
1876
1877+/* =========================================================================
1878+ * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
1879+ */
1880+static int deflateStateCheck (z_stream *strm)
1881+{
1882+ deflate_state *s;
1883+ if (strm == NULL ||
1884+ strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
1885+ return 1;
1886+ s = strm->state;
1887+ if (s == NULL || s->strm != strm || (s->status != INIT_STATE &&
1888+#ifdef GZIP
1889+ s->status != GZIP_STATE &&
1890+#endif
1891+ s->status != EXTRA_STATE &&
1892+ s->status != NAME_STATE &&
1893+ s->status != COMMENT_STATE &&
1894+ s->status != HCRC_STATE &&
1895+ s->status != BUSY_STATE &&
1896+ s->status != FINISH_STATE))
1897+ return 1;
1898+ return 0;
1899+}
1900+
1901 /* ========================================================================= */
1902 int ZEXPORT deflateSetDictionary(z_stream *strm, const unsigned char *dictionary, unsigned int dictLength) {
1903 deflate_state *s;
1904@@ -282,7 +343,7 @@ int ZEXPORT deflateSetDictionary(z_stream *strm, const unsigned char *dictionary
1905 uint32_t avail;
1906 const unsigned char *next;
1907
1908- if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
1909+ if (deflateStateCheck(strm) || dictionary == NULL)
1910 return Z_STREAM_ERROR;
1911 s = strm->state;
1912 wrap = s->wrap;
1913@@ -315,7 +376,7 @@ int ZEXPORT deflateSetDictionary(z_stream *strm, const unsigned char *dictionary
1914 while (s->lookahead >= MIN_MATCH) {
1915 str = s->strstart;
1916 n = s->lookahead - (MIN_MATCH-1);
1917- bulk_insert_str(s, str, n);
1918+ insert_string(s, str, n);
1919 s->strstart = str + n;
1920 s->lookahead = MIN_MATCH-1;
1921 fill_window(s);
1922@@ -336,12 +397,12 @@ int ZEXPORT deflateSetDictionary(z_stream *strm, const unsigned char *dictionary
1923 int ZEXPORT deflateResetKeep(z_stream *strm) {
1924 deflate_state *s;
1925
1926- if (strm == Z_NULL || strm->state == Z_NULL || strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
1927+ if (deflateStateCheck(strm)) {
1928 return Z_STREAM_ERROR;
1929 }
1930
1931 strm->total_in = strm->total_out = 0;
1932- strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
1933+ strm->msg = NULL; /* use zfree if we ever allocate msg dynamically */
1934 strm->data_type = Z_UNKNOWN;
1935
1936 s = (deflate_state *)strm->state;
1937@@ -351,12 +412,18 @@ int ZEXPORT deflateResetKeep(z_stream *strm) {
1938 if (s->wrap < 0) {
1939 s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
1940 }
1941- s->status = s->wrap ? INIT_STATE : BUSY_STATE;
1942+ s->status =
1943 #ifdef GZIP
1944- strm->adler = s->wrap == 2 ? crc32(0L, Z_NULL, 0) : adler32(0L, Z_NULL, 0);
1945-#else
1946- strm->adler = adler32(0L, Z_NULL, 0);
1947+ s->wrap == 2 ? GZIP_STATE :
1948 #endif
1949+ s->wrap ? INIT_STATE : BUSY_STATE;
1950+
1951+#ifdef GZIP
1952+ if (s->wrap == 2)
1953+ crc_reset(s);
1954+ else
1955+#endif
1956+ strm->adler = adler32(0L, NULL, 0);
1957 s->last_flush = Z_NO_FLUSH;
1958
1959 _tr_init(s);
1960@@ -376,9 +443,7 @@ int ZEXPORT deflateReset(z_stream *strm) {
1961
1962 /* ========================================================================= */
1963 int ZEXPORT deflateSetHeader(z_stream *strm, gz_headerp head) {
1964- if (strm == Z_NULL || strm->state == Z_NULL)
1965- return Z_STREAM_ERROR;
1966- if (strm->state->wrap != 2)
1967+ if (deflateStateCheck(strm) || strm->state->wrap != 2)
1968 return Z_STREAM_ERROR;
1969 strm->state->gzhead = head;
1970 return Z_OK;
1971@@ -386,11 +451,11 @@ int ZEXPORT deflateSetHeader(z_stream *strm, gz_headerp head) {
1972
1973 /* ========================================================================= */
1974 int ZEXPORT deflatePending(z_stream *strm, uint32_t *pending, int *bits) {
1975- if (strm == Z_NULL || strm->state == Z_NULL)
1976+ if (deflateStateCheck(strm))
1977 return Z_STREAM_ERROR;
1978- if (pending != Z_NULL)
1979+ if (pending != NULL)
1980 *pending = strm->state->pending;
1981- if (bits != Z_NULL)
1982+ if (bits != NULL)
1983 *bits = strm->state->bi_valid;
1984 return Z_OK;
1985 }
1986@@ -400,7 +465,7 @@ int ZEXPORT deflatePrime(z_stream *strm, int bits, int value) {
1987 deflate_state *s;
1988 int put;
1989
1990- if (strm == Z_NULL || strm->state == Z_NULL)
1991+ if (deflateStateCheck(strm))
1992 return Z_STREAM_ERROR;
1993 s = strm->state;
1994 if ((unsigned char *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
1995@@ -422,9 +487,8 @@ int ZEXPORT deflatePrime(z_stream *strm, int bits, int value) {
1996 int ZEXPORT deflateParams(z_stream *strm, int level, int strategy) {
1997 deflate_state *s;
1998 compress_func func;
1999- int err = Z_OK;
2000
2001- if (strm == Z_NULL || strm->state == Z_NULL)
2002+ if (deflateStateCheck(strm))
2003 return Z_STREAM_ERROR;
2004 s = strm->state;
2005
2006@@ -437,11 +501,20 @@ int ZEXPORT deflateParams(z_stream *strm, int level, int strategy) {
2007
2008 if ((strategy != s->strategy || func != configuration_table[level].func)) {
2009 /* Flush the last buffer: */
2010- err = deflate(strm, Z_BLOCK);
2011- if (err == Z_BUF_ERROR && s->pending == 0)
2012- err = Z_OK;
2013+ int err = deflate(strm, Z_BLOCK);
2014+ if (err == Z_STREAM_ERROR)
2015+ return err;
2016+ if (strm->avail_out == 0)
2017+ return Z_BUF_ERROR;
2018 }
2019 if (s->level != level) {
2020+ if (s->level == 0 && s->matches != 0) {
2021+ if (s->matches == 1)
2022+ slide_hash(s);
2023+ else
2024+ CLEAR_HASH(s);
2025+ s->matches = 0;
2026+ }
2027 s->level = level;
2028 s->max_lazy_match = configuration_table[level].max_lazy;
2029 s->good_match = configuration_table[level].good_length;
2030@@ -449,14 +522,14 @@ int ZEXPORT deflateParams(z_stream *strm, int level, int strategy) {
2031 s->max_chain_length = configuration_table[level].max_chain;
2032 }
2033 s->strategy = strategy;
2034- return err;
2035+ return Z_OK;
2036 }
2037
2038 /* ========================================================================= */
2039 int ZEXPORT deflateTune(z_stream *strm, int good_length, int max_lazy, int nice_length, int max_chain) {
2040 deflate_state *s;
2041
2042- if (strm == Z_NULL || strm->state == Z_NULL)
2043+ if (deflateStateCheck(strm))
2044 return Z_STREAM_ERROR;
2045 s = strm->state;
2046 s->good_match = good_length;
2047@@ -486,13 +559,12 @@ int ZEXPORT deflateTune(z_stream *strm, int good_length, int max_lazy, int nice_
2048 unsigned long ZEXPORT deflateBound(z_stream *strm, unsigned long sourceLen) {
2049 deflate_state *s;
2050 unsigned long complen, wraplen;
2051- unsigned char *str;
2052
2053 /* conservative upper bound for compressed data */
2054 complen = sourceLen + ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
2055
2056 /* if can't get parameters, return conservative bound plus zlib wrapper */
2057- if (strm == Z_NULL || strm->state == Z_NULL)
2058+ if (deflateStateCheck(strm))
2059 return complen + 6;
2060
2061 /* compute wrapper length */
2062@@ -504,20 +576,22 @@ unsigned long ZEXPORT deflateBound(z_stream *strm, unsigned long sourceLen) {
2063 case 1: /* zlib wrapper */
2064 wraplen = 6 + (s->strstart ? 4 : 0);
2065 break;
2066+#ifdef GZIP
2067 case 2: /* gzip wrapper */
2068 wraplen = 18;
2069- if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
2070- if (s->gzhead->extra != Z_NULL) {
2071+ if (s->gzhead != NULL) { /* user-supplied gzip header */
2072+ unsigned char *str;
2073+ if (s->gzhead->extra != NULL) {
2074 wraplen += 2 + s->gzhead->extra_len;
2075 }
2076 str = s->gzhead->name;
2077- if (str != Z_NULL) {
2078+ if (str != NULL) {
2079 do {
2080 wraplen++;
2081 } while (*str++);
2082 }
2083 str = s->gzhead->comment;
2084- if (str != Z_NULL) {
2085+ if (str != NULL) {
2086 do {
2087 wraplen++;
2088 } while (*str++);
2089@@ -526,6 +600,7 @@ unsigned long ZEXPORT deflateBound(z_stream *strm, unsigned long sourceLen) {
2090 wraplen += 2;
2091 }
2092 break;
2093+#endif
2094 default: /* for compiler happiness */
2095 wraplen = 6;
2096 }
2097@@ -543,16 +618,16 @@ unsigned long ZEXPORT deflateBound(z_stream *strm, unsigned long sourceLen) {
2098 * IN assertion: the stream state is correct and there is enough room in
2099 * pending_buf.
2100 */
2101-local void putShortMSB(deflate_state *s, uint16_t b) {
2102+static void putShortMSB(deflate_state *s, uint16_t b) {
2103 put_byte(s, (unsigned char)(b >> 8));
2104 put_byte(s, (unsigned char)(b & 0xff));
2105 }
2106
2107 /* =========================================================================
2108- * Flush as much pending output as possible. All deflate() output goes
2109- * through this function so some applications may wish to modify it
2110- * to avoid allocating a large strm->next_out buffer and copying into it.
2111- * (See also read_buf()).
2112+ * Flush as much pending output as possible. All deflate() output, except for
2113+ * some deflate_stored() output, goes through this function so some
2114+ * applications may wish to modify it to avoid allocating a large
2115+ * strm->next_out buffer and copying into it. (See also read_buf()).
2116 */
2117 ZLIB_INTERNAL void flush_pending(z_stream *strm) {
2118 uint32_t len;
2119@@ -569,24 +644,33 @@ ZLIB_INTERNAL void flush_pending(z_stream *strm) {
2120 strm->next_out += len;
2121 s->pending_out += len;
2122 strm->total_out += len;
2123- strm->avail_out -= len;
2124- s->pending -= len;
2125+ strm->avail_out -= len;
2126+ s->pending -= len;
2127 if (s->pending == 0) {
2128 s->pending_out = s->pending_buf;
2129 }
2130 }
2131
2132+/* ===========================================================================
2133+ * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].
2134+ */
2135+#define HCRC_UPDATE(beg) \
2136+ do { \
2137+ if (s->gzhead->hcrc && s->pending > (beg)) \
2138+ strm->adler = crc32(strm->adler, s->pending_buf + (beg), s->pending - (beg)); \
2139+ } while (0)
2140+
2141 /* ========================================================================= */
2142 int ZEXPORT deflate(z_stream *strm, int flush) {
2143 int old_flush; /* value of flush param for previous deflate call */
2144 deflate_state *s;
2145
2146- if (strm == Z_NULL || strm->state == Z_NULL || flush > Z_BLOCK || flush < 0) {
2147+ if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
2148 return Z_STREAM_ERROR;
2149 }
2150 s = strm->state;
2151
2152- if (strm->next_out == Z_NULL || (strm->avail_in != 0 && strm->next_in == Z_NULL) ||
2153+ if (strm->next_out == NULL || (strm->avail_in != 0 && strm->next_in == NULL) ||
2154 (s->status == FINISH_STATE && flush != Z_FINISH)) {
2155 ERR_RETURN(strm, Z_STREAM_ERROR);
2156 }
2157@@ -597,203 +681,212 @@ int ZEXPORT deflate(z_stream *strm, int flush) {
2158 old_flush = s->last_flush;
2159 s->last_flush = flush;
2160
2161+ /* Flush as much pending output as possible */
2162+ if (s->pending != 0) {
2163+ flush_pending(strm);
2164+ if (strm->avail_out == 0) {
2165+ /* Since avail_out is 0, deflate will be called again with
2166+ * more output space, but possibly with both pending and
2167+ * avail_in equal to zero. There won't be anything to do,
2168+ * but this is not an error situation so make sure we
2169+ * return OK instead of BUF_ERROR at next call of deflate:
2170+ */
2171+ s->last_flush = -1;
2172+ return Z_OK;
2173+ }
2174+
2175+ /* Make sure there is something to do and avoid duplicate consecutive
2176+ * flushes. For repeated and useless calls with Z_FINISH, we keep
2177+ * returning Z_STREAM_END instead of Z_BUF_ERROR.
2178+ */
2179+ } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
2180+ flush != Z_FINISH) {
2181+ ERR_RETURN(strm, Z_BUF_ERROR);
2182+ }
2183+
2184+ /* User must not provide more input after the first FINISH: */
2185+ if (s->status == FINISH_STATE && strm->avail_in != 0) {
2186+ ERR_RETURN(strm, Z_BUF_ERROR);
2187+ }
2188+
2189 /* Write the header */
2190 if (s->status == INIT_STATE) {
2191-#ifdef GZIP
2192- if (s->wrap == 2) {
2193- crc_reset(s);
2194- put_byte(s, 31);
2195- put_byte(s, 139);
2196- put_byte(s, 8);
2197- if (s->gzhead == Z_NULL) {
2198- put_byte(s, 0);
2199- put_byte(s, 0);
2200- put_byte(s, 0);
2201- put_byte(s, 0);
2202- put_byte(s, 0);
2203- put_byte(s, s->level == 9 ? 2 :
2204- (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
2205- 4 : 0));
2206- put_byte(s, OS_CODE);
2207- s->status = BUSY_STATE;
2208- } else {
2209- put_byte(s, (s->gzhead->text ? 1 : 0) +
2210- (s->gzhead->hcrc ? 2 : 0) +
2211- (s->gzhead->extra == Z_NULL ? 0 : 4) +
2212- (s->gzhead->name == Z_NULL ? 0 : 8) +
2213- (s->gzhead->comment == Z_NULL ? 0 : 16) );
2214- put_byte(s, (unsigned char)(s->gzhead->time & 0xff));
2215- put_byte(s, (unsigned char)((s->gzhead->time >> 8) & 0xff));
2216- put_byte(s, (unsigned char)((s->gzhead->time >> 16) & 0xff));
2217- put_byte(s, (unsigned char)((s->gzhead->time >> 24) & 0xff));
2218- put_byte(s, s->level == 9 ? 2 :
2219- (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
2220- 4 : 0));
2221- put_byte(s, s->gzhead->os & 0xff);
2222- if (s->gzhead->extra != Z_NULL) {
2223- put_byte(s, s->gzhead->extra_len & 0xff);
2224- put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
2225- }
2226- if (s->gzhead->hcrc)
2227- strm->adler = crc32(strm->adler, s->pending_buf, s->pending);
2228- s->gzindex = 0;
2229- s->status = EXTRA_STATE;
2230- }
2231- } else
2232-#endif
2233- {
2234- unsigned int header = (Z_DEFLATED + ((s->w_bits-8) << 4)) << 8;
2235- unsigned int level_flags;
2236-
2237- if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
2238- level_flags = 0;
2239- else if (s->level < 6)
2240- level_flags = 1;
2241- else if (s->level == 6)
2242- level_flags = 2;
2243- else
2244- level_flags = 3;
2245- header |= (level_flags << 6);
2246- if (s->strstart != 0)
2247- header |= PRESET_DICT;
2248- header += 31 - (header % 31);
2249+ /* zlib header */
2250+ unsigned int header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
2251+ unsigned int level_flags;
2252+
2253+ if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
2254+ level_flags = 0;
2255+ else if (s->level < TRIGGER_LEVEL)
2256+ level_flags = 1;
2257+ else if (s->level == TRIGGER_LEVEL)
2258+ level_flags = 2;
2259+ else
2260+ level_flags = 3;
2261+ header |= (level_flags << 6);
2262+ if (s->strstart != 0) header |= PRESET_DICT;
2263+ header += 31 - (header % 31);
2264+
2265+ putShortMSB(s, header);
2266+
2267+ /* Save the adler32 of the preset dictionary: */
2268+ if (s->strstart != 0) {
2269+ putShortMSB(s, (uint16_t)(strm->adler >> 16));
2270+ putShortMSB(s, (uint16_t)(strm->adler));
2271+ }
2272+ strm->adler = adler32(0L, NULL, 0);
2273+ s->status = BUSY_STATE;
2274
2275+ /* Compression must start with an empty pending buffer */
2276+ flush_pending(strm);
2277+ if (s->pending != 0) {
2278+ s->last_flush = -1;
2279+ return Z_OK;
2280+ }
2281+ }
2282+#ifdef GZIP
2283+ if (s->status == GZIP_STATE) {
2284+ /* gzip header */
2285+ crc_reset(s);
2286+ put_byte(s, 31);
2287+ put_byte(s, 139);
2288+ put_byte(s, 8);
2289+ if (s->gzhead == NULL) {
2290+ put_byte(s, 0);
2291+ put_byte(s, 0);
2292+ put_byte(s, 0);
2293+ put_byte(s, 0);
2294+ put_byte(s, 0);
2295+ put_byte(s, s->level == 9 ? 2 :
2296+ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 4 : 0));
2297+ put_byte(s, OS_CODE);
2298 s->status = BUSY_STATE;
2299- putShortMSB(s, header);
2300
2301- /* Save the adler32 of the preset dictionary: */
2302- if (s->strstart != 0) {
2303- putShortMSB(s, (uint16_t)(strm->adler >> 16));
2304- putShortMSB(s, (uint16_t)strm->adler);
2305+ /* Compression must start with an empty pending buffer */
2306+ flush_pending(strm);
2307+ if (s->pending != 0) {
2308+ s->last_flush = -1;
2309+ return Z_OK;
2310+ }
2311+ }
2312+ else {
2313+ put_byte(s, (s->gzhead->text ? 1 : 0) +
2314+ (s->gzhead->hcrc ? 2 : 0) +
2315+ (s->gzhead->extra == NULL ? 0 : 4) +
2316+ (s->gzhead->name == NULL ? 0 : 8) +
2317+ (s->gzhead->comment == NULL ? 0 : 16)
2318+ );
2319+ put_byte(s, (unsigned char)(s->gzhead->time & 0xff));
2320+ put_byte(s, (unsigned char)((s->gzhead->time >> 8) & 0xff));
2321+ put_byte(s, (unsigned char)((s->gzhead->time >> 16) & 0xff));
2322+ put_byte(s, (unsigned char)((s->gzhead->time >> 24) & 0xff));
2323+ put_byte(s, s->level == 9 ? 2 :
2324+ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 4 : 0));
2325+ put_byte(s, s->gzhead->os & 0xff);
2326+ if (s->gzhead->extra != NULL) {
2327+ put_byte(s, s->gzhead->extra_len & 0xff);
2328+ put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
2329 }
2330- strm->adler = adler32(0L, Z_NULL, 0);
2331+ if (s->gzhead->hcrc)
2332+ strm->adler = crc32(strm->adler, s->pending_buf, s->pending);
2333+ s->gzindex = 0;
2334+ s->status = EXTRA_STATE;
2335 }
2336 }
2337-#ifdef GZIP
2338 if (s->status == EXTRA_STATE) {
2339 if (s->gzhead->extra != Z_NULL) {
2340- uint32_t beg = s->pending; /* start of bytes to update crc */
2341-
2342- while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
2343- if (s->pending == s->pending_buf_size) {
2344- if (s->gzhead->hcrc && s->pending > beg)
2345- strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg);
2346- flush_pending(strm);
2347- beg = s->pending;
2348- if (s->pending == s->pending_buf_size)
2349- break;
2350+ uint32_t beg = s->pending; /* start of bytes to update crc */
2351+ uint32_t left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
2352+
2353+ while (s->pending + left > s->pending_buf_size) {
2354+ uint32_t copy = s->pending_buf_size - s->pending;
2355+ memcpy(s->pending_buf + s->pending, s->gzhead->extra + s->gzindex, copy);
2356+ s->pending = s->pending_buf_size;
2357+ HCRC_UPDATE(beg);
2358+ s->gzindex += copy;
2359+ flush_pending(strm);
2360+ if (s->pending != 0) {
2361+ s->last_flush = -1;
2362+ return Z_OK;
2363 }
2364- put_byte(s, s->gzhead->extra[s->gzindex]);
2365- s->gzindex++;
2366- }
2367- if (s->gzhead->hcrc && s->pending > beg)
2368- strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg);
2369- if (s->gzindex == s->gzhead->extra_len) {
2370- s->gzindex = 0;
2371- s->status = NAME_STATE;
2372+ beg = 0;
2373+ left -= copy;
2374 }
2375- } else {
2376- s->status = NAME_STATE;
2377+ memcpy(s->pending_buf + s->pending, s->gzhead->extra + s->gzindex, left);
2378+ s->pending += left;
2379+ HCRC_UPDATE(beg);
2380+ s->gzindex = 0;
2381 }
2382+ s->status = NAME_STATE;
2383 }
2384 if (s->status == NAME_STATE) {
2385- if (s->gzhead->name != Z_NULL) {
2386- uint32_t beg = s->pending; /* start of bytes to update crc */
2387+ if (s->gzhead->name != NULL) {
2388+ uint32_t beg = s->pending; /* start of bytes to update crc */
2389 int val;
2390
2391 do {
2392 if (s->pending == s->pending_buf_size) {
2393- if (s->gzhead->hcrc && s->pending > beg)
2394- strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg);
2395+ HCRC_UPDATE(beg);
2396 flush_pending(strm);
2397- beg = s->pending;
2398- if (s->pending == s->pending_buf_size) {
2399- val = 1;
2400- break;
2401+ if (s->pending != 0) {
2402+ s->last_flush = -1;
2403+ return Z_OK;
2404 }
2405+ beg = 0;
2406 }
2407 val = s->gzhead->name[s->gzindex++];
2408 put_byte(s, val);
2409 } while (val != 0);
2410- if (s->gzhead->hcrc && s->pending > beg)
2411- strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg);
2412- if (val == 0) {
2413- s->gzindex = 0;
2414- s->status = COMMENT_STATE;
2415- }
2416- } else {
2417- s->status = COMMENT_STATE;
2418+ HCRC_UPDATE(beg);
2419+ s->gzindex = 0;
2420 }
2421+ s->status = COMMENT_STATE;
2422 }
2423 if (s->status == COMMENT_STATE) {
2424- if (s->gzhead->comment != Z_NULL) {
2425+ if (s->gzhead->comment != NULL) {
2426 uint32_t beg = s->pending; /* start of bytes to update crc */
2427 int val;
2428
2429 do {
2430 if (s->pending == s->pending_buf_size) {
2431- if (s->gzhead->hcrc && s->pending > beg)
2432- strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg);
2433+ HCRC_UPDATE(beg);
2434 flush_pending(strm);
2435- beg = s->pending;
2436- if (s->pending == s->pending_buf_size) {
2437- val = 1;
2438- break;
2439+ if (s->pending != 0) {
2440+ s->last_flush = -1;
2441+ return Z_OK;
2442 }
2443+ beg = 0;
2444 }
2445 val = s->gzhead->comment[s->gzindex++];
2446 put_byte(s, val);
2447 } while (val != 0);
2448- if (s->gzhead->hcrc && s->pending > beg)
2449- strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg);
2450- if (val == 0)
2451- s->status = HCRC_STATE;
2452- } else {
2453- s->status = HCRC_STATE;
2454+ HCRC_UPDATE(beg);
2455 }
2456+ s->status = HCRC_STATE;
2457 }
2458 if (s->status == HCRC_STATE) {
2459 if (s->gzhead->hcrc) {
2460- if (s->pending + 2 > s->pending_buf_size)
2461+ if (s->pending + 2 > s->pending_buf_size) {
2462 flush_pending(strm);
2463- if (s->pending + 2 <= s->pending_buf_size) {
2464- put_byte(s, (unsigned char)(strm->adler & 0xff));
2465- put_byte(s, (unsigned char)((strm->adler >> 8) & 0xff));
2466- strm->adler = crc32(0L, Z_NULL, 0);
2467- s->status = BUSY_STATE;
2468+ if (s->pending != 0) {
2469+ s->last_flush = -1;
2470+ return Z_OK;
2471+ }
2472 }
2473- } else {
2474- s->status = BUSY_STATE;
2475+ put_byte(s, (unsigned char)(strm->adler & 0xff));
2476+ put_byte(s, (unsigned char)((strm->adler >> 8) & 0xff));
2477+ crc_reset(s);
2478 }
2479- }
2480-#endif
2481+ s->status = BUSY_STATE;
2482
2483- /* Flush as much pending output as possible */
2484- if (s->pending != 0) {
2485+ /* Compression must start with an empty pending buffer */
2486 flush_pending(strm);
2487- if (strm->avail_out == 0) {
2488- /* Since avail_out is 0, deflate will be called again with
2489- * more output space, but possibly with both pending and
2490- * avail_in equal to zero. There won't be anything to do,
2491- * but this is not an error situation so make sure we
2492- * return OK instead of BUF_ERROR at next call of deflate:
2493- */
2494+ if (s->pending != 0) {
2495 s->last_flush = -1;
2496 return Z_OK;
2497 }
2498-
2499- /* Make sure there is something to do and avoid duplicate consecutive
2500- * flushes. For repeated and useless calls with Z_FINISH, we keep
2501- * returning Z_STREAM_END instead of Z_BUF_ERROR.
2502- */
2503- } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && flush != Z_FINISH) {
2504- ERR_RETURN(strm, Z_BUF_ERROR);
2505- }
2506-
2507- /* User must not provide more input after the first FINISH: */
2508- if (s->status == FINISH_STATE && strm->avail_in != 0) {
2509- ERR_RETURN(strm, Z_BUF_ERROR);
2510 }
2511+#endif
2512
2513 /* Start a new block or continue the current one.
2514 */
2515@@ -806,7 +899,8 @@ int ZEXPORT deflate(z_stream *strm, int flush) {
2516 (s->strategy == Z_RLE ? deflate_rle(s, flush) : deflate_fast(s, flush));
2517 else
2518 #endif
2519- bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
2520+ bstate = s->level == 0 ? deflate_stored(s, flush) :
2521+ s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
2522 (s->strategy == Z_RLE ? deflate_rle(s, flush) : (*(configuration_table[s->level].func))(s, flush));
2523
2524 if (bstate == finish_started || bstate == finish_done) {
2525@@ -889,19 +983,10 @@ int ZEXPORT deflate(z_stream *strm, int flush) {
2526 int ZEXPORT deflateEnd(z_stream *strm) {
2527 int status;
2528
2529- if (strm == Z_NULL || strm->state == Z_NULL)
2530+ if (deflateStateCheck(strm))
2531 return Z_STREAM_ERROR;
2532
2533 status = strm->state->status;
2534- if (status != INIT_STATE &&
2535- status != EXTRA_STATE &&
2536- status != NAME_STATE &&
2537- status != COMMENT_STATE &&
2538- status != HCRC_STATE &&
2539- status != BUSY_STATE &&
2540- status != FINISH_STATE) {
2541- return Z_STREAM_ERROR;
2542- }
2543
2544 /* Deallocate in reverse order of allocations: */
2545 TRY_FREE(strm, strm->state->pending_buf);
2546@@ -910,7 +995,7 @@ int ZEXPORT deflateEnd(z_stream *strm) {
2547 TRY_FREE(strm, strm->state->window);
2548
2549 ZFREE(strm, strm->state);
2550- strm->state = Z_NULL;
2551+ strm->state = NULL;
2552
2553 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
2554 }
2555@@ -923,7 +1008,7 @@ int ZEXPORT deflateCopy(z_stream *dest, z_stream *source) {
2556 deflate_state *ss;
2557 uint16_t *overlay;
2558
2559- if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
2560+ if (deflateStateCheck(source) || dest == NULL) {
2561 return Z_STREAM_ERROR;
2562 }
2563
2564@@ -932,7 +1017,7 @@ int ZEXPORT deflateCopy(z_stream *dest, z_stream *source) {
2565 memcpy((void *)dest, (void *)source, sizeof(z_stream));
2566
2567 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
2568- if (ds == Z_NULL)
2569+ if (ds == NULL)
2570 return Z_MEM_ERROR;
2571 dest->state = (struct internal_state *) ds;
2572 memcpy((void *)ds, (void *)ss, sizeof(deflate_state));
2573@@ -944,7 +1029,7 @@ int ZEXPORT deflateCopy(z_stream *dest, z_stream *source) {
2574 overlay = (uint16_t *) ZALLOC(dest, ds->lit_bufsize, sizeof(uint16_t)+2);
2575 ds->pending_buf = (unsigned char *) overlay;
2576
2577- if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || ds->pending_buf == Z_NULL) {
2578+ if (ds->window == NULL || ds->prev == NULL || ds->head == NULL || ds->pending_buf == NULL) {
2579 deflateEnd(dest);
2580 return Z_MEM_ERROR;
2581 }
2582@@ -1001,7 +1086,7 @@ ZLIB_INTERNAL int read_buf(z_stream *strm, unsigned char *buf, unsigned size) {
2583 /* ===========================================================================
2584 * Initialize the "longest match" routines for a new zlib stream
2585 */
2586-local void lm_init(deflate_state *s) {
2587+static void lm_init(deflate_state *s) {
2588 s->window_size = (unsigned long)2L*s->w_size;
2589
2590 CLEAR_HASH(s);
2591@@ -1022,7 +1107,10 @@ local void lm_init(deflate_state *s) {
2592 s->ins_h = 0;
2593 }
2594
2595-#ifdef DEBUG
2596+#ifdef ZLIB_DEBUG
2597+#define EQUAL 0
2598+/* result of memcmp for equal strings */
2599+
2600 /* ===========================================================================
2601 * Check that the match at match_start is indeed a match.
2602 */
2603@@ -1044,7 +1132,7 @@ void check_match(deflate_state *s, IPos start, IPos match, int length) {
2604 }
2605 #else
2606 # define check_match(s, start, match, length)
2607-#endif /* DEBUG */
2608+#endif /* ZLIB_DEBUG */
2609
2610 /* ===========================================================================
2611 * Fill the window when the lookahead becomes insufficient.
2612@@ -1079,81 +1167,24 @@ void fill_window(deflate_state *s) {
2613 }
2614
2615 void fill_window_c(deflate_state *s) {
2616- register unsigned n;
2617- register Pos *p;
2618- unsigned more; /* Amount of free space at the end of the window. */
2619+ unsigned n;
2620+ unsigned long more; /* Amount of free space at the end of the window. */
2621 unsigned int wsize = s->w_size;
2622
2623 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
2624
2625 do {
2626- more = (unsigned)(s->window_size -(unsigned long)s->lookahead -(unsigned long)s->strstart);
2627+ more = s->window_size - s->lookahead - s->strstart;
2628
2629 /* If the window is almost full and there is insufficient lookahead,
2630 * move the upper half to the lower one to make room in the upper half.
2631 */
2632 if (s->strstart >= wsize+MAX_DIST(s)) {
2633- memcpy(s->window, s->window+wsize, (unsigned)wsize);
2634+ memcpy(s->window, s->window+wsize, wsize - more);
2635 s->match_start -= wsize;
2636 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
2637 s->block_start -= (long) wsize;
2638-
2639- /* Slide the hash table (could be avoided with 32 bit values
2640- at the expense of memory usage). We slide even when level == 0
2641- to keep the hash table consistent if we switch back to level > 0
2642- later. (Using level 0 permanently is not an optimal usage of
2643- zlib, so we don't care about this pathological case.)
2644- */
2645- n = s->hash_size;
2646- p = &s->head[n];
2647-#ifdef NOT_TWEAK_COMPILER
2648- do {
2649- unsigned m;
2650- m = *--p;
2651- *p = (Pos)(m >= wsize ? m-wsize : NIL);
2652- } while (--n);
2653-#else
2654- /* As of I make this change, gcc (4.8.*) isn't able to vectorize
2655- * this hot loop using saturated-subtraction on x86-64 architecture.
2656- * To avoid this defect, we can change the loop such that
2657- * o. the pointer advance forward, and
2658- * o. demote the variable 'm' to be local to the loop, and
2659- * choose type "Pos" (instead of 'unsigned int') for the
2660- * variable to avoid unncessary zero-extension.
2661- */
2662- {
2663- unsigned int i;
2664- Pos *q = p - n;
2665- for (i = 0; i < n; i++) {
2666- Pos m = *q;
2667- Pos t = wsize;
2668- *q++ = (Pos)(m >= t ? m-t: NIL);
2669- }
2670- }
2671-
2672-#endif /* NOT_TWEAK_COMPILER */
2673- n = wsize;
2674- p = &s->prev[n];
2675-#ifdef NOT_TWEAK_COMPILER
2676- do {
2677- unsigned m;
2678- m = *--p;
2679- *p = (Pos)(m >= wsize ? m-wsize : NIL);
2680- /* If n is not on any hash chain, prev[n] is garbage but
2681- * its value will never be used.
2682- */
2683- } while (--n);
2684-#else
2685- {
2686- unsigned int i;
2687- Pos *q = p - n;
2688- for (i = 0; i < n; i++) {
2689- Pos m = *q;
2690- Pos t = wsize;
2691- *q++ = (Pos)(m >= t ? m-t: NIL);
2692- }
2693- }
2694-#endif /* NOT_TWEAK_COMPILER */
2695+ slide_hash(s);
2696 more += wsize;
2697 }
2698 if (s->strm->avail_in == 0)
2699@@ -1179,20 +1210,38 @@ void fill_window_c(deflate_state *s) {
2700 if (s->lookahead + s->insert >= MIN_MATCH) {
2701 unsigned int str = s->strstart - s->insert;
2702 s->ins_h = s->window[str];
2703+#ifndef NOT_TWEAK_COMPILER
2704+ {
2705+ unsigned int insert_cnt = s->insert;
2706+ unsigned int slen;
2707+ if (unlikely(s->lookahead < MIN_MATCH))
2708+ insert_cnt += s->lookahead - MIN_MATCH;
2709+ slen = insert_cnt;
2710+ if (str >= (MIN_MATCH - 2))
2711+ {
2712+ str += 2 - MIN_MATCH;
2713+ insert_cnt += MIN_MATCH - 2;
2714+ }
2715+ if (insert_cnt > 0)
2716+ {
2717+ insert_string(s, str, insert_cnt);
2718+ s->insert -= slen;
2719+ }
2720+ }
2721+#else
2722 if (str >= 1)
2723- UPDATE_HASH(s, s->ins_h, str + 1 - (MIN_MATCH-1));
2724+ insert_string(s, str + 2 - MIN_MATCH, 1);
2725 #if MIN_MATCH != 3
2726- Call UPDATE_HASH() MIN_MATCH-3 more times
2727+#warning Call insert_string() MIN_MATCH-3 more times
2728 #endif
2729 while (s->insert) {
2730- UPDATE_HASH(s, s->ins_h, str);
2731- s->prev[str & s->w_mask] = s->head[s->ins_h];
2732- s->head[s->ins_h] = (Pos)str;
2733+ insert_string(s, str, 1);
2734 str++;
2735 s->insert--;
2736 if (s->lookahead + s->insert < MIN_MATCH)
2737 break;
2738 }
2739+#endif
2740 }
2741 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
2742 * but this is not important since only literal bytes will be emitted.
2743@@ -1207,7 +1256,7 @@ void fill_window_c(deflate_state *s) {
2744 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
2745 */
2746 if (s->high_water < s->window_size) {
2747- unsigned long curr = s->strstart + (unsigned long)(s->lookahead);
2748+ unsigned long curr = s->strstart + (unsigned long)s->lookahead;
2749 unsigned long init;
2750
2751 if (s->high_water < curr) {
2752@@ -1217,17 +1266,18 @@ void fill_window_c(deflate_state *s) {
2753 init = s->window_size - curr;
2754 if (init > WIN_INIT)
2755 init = WIN_INIT;
2756- memset(s->window + curr, 0, (unsigned)init);
2757+ memset(s->window + curr, 0, init);
2758 s->high_water = curr + init;
2759- } else if (s->high_water < (unsigned long)curr + WIN_INIT) {
2760+ } else if (s->high_water < curr + WIN_INIT) {
2761 /* High water mark at or above current data, but below current data
2762 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
2763 * to end of window, whichever is less.
2764 */
2765- init = (unsigned long)curr + WIN_INIT - s->high_water;
2766- if (init > s->window_size - s->high_water)
2767- init = s->window_size - s->high_water;
2768- memset(s->window + s->high_water, 0, (unsigned)init);
2769+ init = curr + WIN_INIT;
2770+ if (init > s->window_size)
2771+ init = s->window_size;
2772+ init -= s->high_water;
2773+ memset(s->window + s->high_water, 0, init);
2774 s->high_water += init;
2775 }
2776 }
2777@@ -1236,67 +1286,190 @@ void fill_window_c(deflate_state *s) {
2778 "not enough room for search");
2779 }
2780
2781+/* Maximum stored block length in deflate format (not including header). */
2782+#define MAX_STORED 65535
2783+
2784+/* Minimum of a and b. */
2785+#define MIN(a, b) ((a) > (b) ? (b) : (a))
2786+
2787 /* ===========================================================================
2788 * Copy without compression as much as possible from the input stream, return
2789 * the current block state.
2790- * This function does not insert new strings in the dictionary since
2791- * uncompressible data is probably not useful. This function is used
2792- * only for the level=0 compression option.
2793- * NOTE: this function should be optimized to avoid extra copying from
2794- * window to pending_buf.
2795+ *
2796+ * In case deflateParams() is used to later switch to a non-zero compression
2797+ * level, s->matches (otherwise unused when storing) keeps track of the number
2798+ * of hash table slides to perform. If s->matches is 1, then one hash table
2799+ * slide will be done when switching. If s->matches is 2, the maximum value
2800+ * allowed here, then the hash table will be cleared, since two or more slides
2801+ * is the same as a clear.
2802+ *
2803+ * deflate_stored() is written to minimize the number of times an input byte is
2804+ * copied. It is most efficient with large input and output buffers, which
2805+ * maximizes the opportunites to have a single copy from next_in to next_out.
2806 */
2807-local block_state deflate_stored(deflate_state *s, int flush) {
2808- /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
2809- * to pending_buf_size, and each stored block has a 5 byte header:
2810+static block_state deflate_stored(deflate_state *s, int flush) {
2811+ /* Smallest worthy block size when not flushing or finishing. By default
2812+ * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
2813+ * large input and output buffers, the stored block size will be larger.
2814 */
2815- unsigned long max_block_size = 0xffff;
2816- unsigned long max_start;
2817+ unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
2818
2819- if (max_block_size > s->pending_buf_size - 5) {
2820- max_block_size = (uint32_t)(s->pending_buf_size - 5);
2821- }
2822-
2823- /* Copy as much as possible from input to output: */
2824+ /* Copy as many min_block or larger stored blocks directly to next_out as
2825+ * possible. If flushing, copy the remaining available input to next_out as
2826+ * stored blocks, if there is enough space.
2827+ */
2828+ unsigned len, left, have, last;
2829+ unsigned used = s->strm->avail_in;
2830 for (;;) {
2831- /* Fill the window as much as possible: */
2832- if (s->lookahead <= 1) {
2833- Assert(s->strstart < s->w_size+MAX_DIST(s) || s->block_start >= (long)s->w_size, "slide too late");
2834+ /* Set len to the maximum size block that we can copy directly with the
2835+ * available input data and output space. Set left to how much of that
2836+ * would be copied from what's left in the window.
2837+ */
2838+ len = MAX_STORED; /* maximum deflate stored block length */
2839+ have = (s->bi_valid + 42) >> 3; /* number of header bytes */
2840+ /* maximum stored block length that will fit in avail_out: */
2841+ have = s->strm->avail_out > have ? s->strm->avail_out - have : 0;
2842+ left = s->strstart - s->block_start; /* bytes left in window */
2843+ if (len > (unsigned long)left + s->strm->avail_in)
2844+ len = left + s->strm->avail_in; /* limit len to the input */
2845+ if (len > have)
2846+ len = have; /* limit len to the output */
2847+ if (left > len)
2848+ left = len; /* limit window pull to len */
2849+
2850+ /* If the stored block would be less than min_block in length, or if
2851+ * unable to copy all of the available input when flushing, then try
2852+ * copying to the window and the pending buffer instead. Also don't
2853+ * write an empty block when flushing -- deflate() does that.
2854+ */
2855+ if (len < min_block && (len == 0 || flush == Z_NO_FLUSH ||
2856+ len - left != s->strm->avail_in))
2857+ break;
2858
2859- fill_window(s);
2860- if (s->lookahead == 0 && flush == Z_NO_FLUSH)
2861- return need_more;
2862+ /* Make a dummy stored block in pending to get the header bytes,
2863+ * including any pending bits. This also updates the debugging counts.
2864+ */
2865+ last = flush == Z_FINISH && len - left == s->strm->avail_in ? 1 : 0;
2866+ _tr_stored_block(s, (char *)0, 0L, last);
2867+
2868+ /* Replace the lengths in the dummy stored block with len. */
2869+ s->pending_buf[s->pending - 4] = len;
2870+ s->pending_buf[s->pending - 3] = len >> 8;
2871+ s->pending_buf[s->pending - 2] = ~len;
2872+ s->pending_buf[s->pending - 1] = ~len >> 8;
2873+
2874+ /* Write the stored block header bytes. */
2875+ flush_pending(s->strm);
2876+
2877+ /* Update debugging counts for the data about to be copied. */
2878+#ifdef ZLIB_DEBUG
2879+ s->compressed_len += len << 3;
2880+ s->bits_sent += len << 3;
2881+#endif
2882
2883- if (s->lookahead == 0)
2884- break; /* flush the current block */
2885+ /* Copy uncompressed bytes from the window to next_out. */
2886+ if (left) {
2887+ memcpy(s->strm->next_out, s->window + s->block_start, left);
2888+ s->strm->next_out += left;
2889+ s->strm->avail_out -= left;
2890+ s->strm->total_out += left;
2891+ s->block_start += left;
2892+ len -= left;
2893 }
2894- Assert(s->block_start >= 0L, "block gone");
2895
2896- s->strstart += s->lookahead;
2897- s->lookahead = 0;
2898-
2899- /* Emit a stored block if pending_buf will be full: */
2900- max_start = s->block_start + max_block_size;
2901- if (s->strstart == 0 || (unsigned long)s->strstart >= max_start) {
2902- /* strstart == 0 is possible when wraparound on 16-bit machine */
2903- s->lookahead = (unsigned int)(s->strstart - max_start);
2904- s->strstart = (unsigned int)max_start;
2905- FLUSH_BLOCK(s, 0);
2906+ /* Copy uncompressed bytes directly from next_in to next_out, updating
2907+ * the check value.
2908+ */
2909+ if (len) {
2910+ read_buf(s->strm, s->strm->next_out, len);
2911+ s->strm->next_out += len;
2912+ s->strm->avail_out -= len;
2913+ s->strm->total_out += len;
2914 }
2915- /* Flush if we may have to slide, otherwise block_start may become
2916- * negative and the data will be gone:
2917+ }
2918+
2919+ /* Update the sliding window with the last s->w_size bytes of the copied
2920+ * data, or append all of the copied data to the existing window if less
2921+ * than s->w_size bytes were copied. Also update the number of bytes to
2922+ * insert in the hash tables, in the event that deflateParams() switches to
2923+ * a non-zero compression level.
2924+ */
2925+ used -= s->strm->avail_in; /* number of input bytes directly copied */
2926+ if (used) {
2927+ /* If any input was used, then no unused input remains in the window,
2928+ * therefore s->block_start == s->strstart.
2929 */
2930- if (s->strstart - (unsigned int)s->block_start >= MAX_DIST(s)) {
2931- FLUSH_BLOCK(s, 0);
2932+ if (used >= s->w_size) { /* supplant the previous history */
2933+ s->matches = 2; /* clear hash */
2934+ memcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
2935+ s->strstart = s->w_size;
2936+ }
2937+ else {
2938+ if (s->window_size - s->strstart <= used) {
2939+ /* Slide the window down. */
2940+ s->strstart -= s->w_size;
2941+ memcpy(s->window, s->window + s->w_size, s->strstart);
2942+ if (s->matches < 2)
2943+ s->matches++; /* add a pending slide_hash() */
2944+ }
2945+ memcpy(s->window + s->strstart, s->strm->next_in - used, used);
2946+ s->strstart += used;
2947 }
2948+ s->block_start = s->strstart;
2949+ s->insert += MIN(used, s->w_size - s->insert);
2950 }
2951- s->insert = 0;
2952- if (flush == Z_FINISH) {
2953- FLUSH_BLOCK(s, 1);
2954- return finish_done;
2955+
2956+ /* If flushing or finishing and all input has been consumed, then done. If
2957+ * the code above couldn't write a complete block to next_out, then the
2958+ * code following this won't be able to either.
2959+ */
2960+ if (flush != Z_NO_FLUSH && s->strm->avail_in == 0 &&
2961+ s->strstart == s->block_start)
2962+ return flush == Z_FINISH ? finish_done : block_done;
2963+
2964+ /* Fill the window with any remaining input. */
2965+ have = s->window_size - s->strstart - 1;
2966+ if (s->strm->avail_in > have && s->block_start >= (long) s->w_size) {
2967+ /* Slide the window down. */
2968+ s->block_start -= s->w_size;
2969+ s->strstart -= s->w_size;
2970+ memcpy(s->window, s->window + s->w_size, s->strstart);
2971+ if (s->matches < 2)
2972+ s->matches++; /* add a pending slide_hash() */
2973+ have += s->w_size; /* more space now */
2974 }
2975- if ((long)s->strstart > s->block_start)
2976- FLUSH_BLOCK(s, 0);
2977- return block_done;
2978+ if (have > s->strm->avail_in)
2979+ have = s->strm->avail_in;
2980+ if (have) {
2981+ read_buf(s->strm, s->window + s->strstart, have);
2982+ s->strstart += have;
2983+ }
2984+
2985+ /* There was not enough avail_out to write a complete worthy or flushed
2986+ * stored block to next_out. Write a stored block to pending instead, if we
2987+ * have enough input for a worthy block, or if flushing and there is enough
2988+ * room for the remaining input as a stored block in the pending buffer.
2989+ */
2990+ have = (s->bi_valid + 42) >> 3; /* number of header bytes */
2991+ /* maximum stored block length that will fit in pending: */
2992+ have = MIN(s->pending_buf_size - have, MAX_STORED);
2993+ min_block = MIN(have, s->w_size);
2994+ left = s->strstart - s->block_start;
2995+ if (left >= min_block ||
2996+ (left && flush != Z_NO_FLUSH && s->strm->avail_in == 0 &&
2997+ left <= have)) {
2998+ len = MIN(left, have);
2999+ last = flush == Z_FINISH && s->strm->avail_in == 0 &&
3000+ len == left ? 1 : 0;
3001+ _tr_stored_block(s, (charf *)s->window + s->block_start, len, last);
3002+ s->block_start += len;
3003+ flush_pending(s->strm);
3004+ if (last)
3005+ return finish_started;
3006+ }
3007+
3008+ /* We've done all we can with the available input and output. */
3009+ return need_more;
3010 }
3011
3012
3013@@ -1305,7 +1478,7 @@ local block_state deflate_stored(deflate_state *s, int flush) {
3014 * one. Do not maintain a hash table. (It will be regenerated if this run of
3015 * deflate switches away from Z_RLE.)
3016 */
3017-local block_state deflate_rle(deflate_state *s, int flush) {
3018+static block_state deflate_rle(deflate_state *s, int flush) {
3019 int bflush; /* set if current block must be flushed */
3020 unsigned int prev; /* byte at distance one to match */
3021 unsigned char *scan, *strend; /* scan goes up to strend for length of run */
3022@@ -1377,7 +1550,7 @@ local block_state deflate_rle(deflate_state *s, int flush) {
3023 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
3024 * (It will be regenerated if this run of deflate switches away from Huffman.)
3025 */
3026-local block_state deflate_huff(deflate_state *s, int flush) {
3027+static block_state deflate_huff(deflate_state *s, int flush) {
3028 int bflush; /* set if current block must be flushed */
3029
3030 for (;;) {
3031@@ -1409,3 +1582,29 @@ local block_state deflate_huff(deflate_state *s, int flush) {
3032 FLUSH_BLOCK(s, 0);
3033 return block_done;
3034 }
3035+
3036+#ifdef ZLIB_DEBUG
3037+/* ===========================================================================
3038+ * Send a value on a given number of bits.
3039+ * IN assertion: length <= 16 and value fits in length bits.
3040+ */
3041+void send_bits(deflate_state *s, int value, int length) {
3042+ Tracevv((stderr, " l %2d v %4x ", length, value));
3043+ Assert(length > 0 && length <= 15, "invalid length");
3044+ s->bits_sent += (unsigned long)length;
3045+
3046+ /* If not enough room in bi_buf, use (valid) bits from bi_buf and
3047+ * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
3048+ * unused bits in value.
3049+ */
3050+ if (s->bi_valid > (int)Buf_size - length) {
3051+ s->bi_buf |= (uint16_t)value << s->bi_valid;
3052+ put_short(s, s->bi_buf);
3053+ s->bi_buf = (uint16_t)value >> (Buf_size - s->bi_valid);
3054+ s->bi_valid += length - Buf_size;
3055+ } else {
3056+ s->bi_buf |= (uint16_t)value << s->bi_valid;
3057+ s->bi_valid += length;
3058+ }
3059+}
3060+#endif
3061diff --git a/deflate.h b/deflate.h
3062index 2d32026..62d1fd8 100644
3063--- a/deflate.h
3064+++ b/deflate.h
3065@@ -57,13 +57,16 @@
3066 #define END_BLOCK 256
3067 /* end of block literal code */
3068
3069-#define INIT_STATE 42
3070-#define EXTRA_STATE 69
3071-#define NAME_STATE 73
3072-#define COMMENT_STATE 91
3073-#define HCRC_STATE 103
3074-#define BUSY_STATE 113
3075-#define FINISH_STATE 666
3076+#define INIT_STATE 42 /* zlib header -> BUSY_STATE */
3077+#ifdef GZIP
3078+# define GZIP_STATE 57 /* gzip header -> BUSY_STATE | EXTRA_STATE */
3079+#endif
3080+#define EXTRA_STATE 69 /* gzip extra block -> NAME_STATE */
3081+#define NAME_STATE 73 /* gzip file name -> COMMENT_STATE */
3082+#define COMMENT_STATE 91 /* gzip comment -> HCRC_STATE */
3083+#define HCRC_STATE 103 /* gzip header CRC -> BUSY_STATE */
3084+#define BUSY_STATE 113 /* deflate -> FINISH_STATE */
3085+#define FINISH_STATE 666 /* stream complete */
3086 /* Stream status */
3087
3088
3089@@ -87,8 +90,8 @@ typedef struct ct_data_s {
3090 typedef struct static_tree_desc_s static_tree_desc;
3091
3092 typedef struct tree_desc_s {
3093- ct_data *dyn_tree; /* the dynamic tree */
3094- int max_code; /* largest code with non zero frequency */
3095+ ct_data *dyn_tree; /* the dynamic tree */
3096+ int max_code; /* largest code with non zero frequency */
3097 const static_tree_desc *stat_desc; /* the corresponding static tree */
3098 } tree_desc;
3099
3100@@ -101,14 +104,14 @@ typedef unsigned IPos;
3101
3102 typedef struct internal_state {
3103 z_stream *strm; /* pointer back to this zlib stream */
3104- int status; /* as the name implies */
3105+ int status; /* as the name implies */
3106 unsigned char *pending_buf; /* output still pending */
3107- unsigned long pending_buf_size; /* size of pending_buf */
3108+ unsigned long pending_buf_size; /* size of pending_buf */
3109 unsigned char *pending_out; /* next pending byte to output to the stream */
3110- unsigned int pending; /* nb of bytes in the pending buffer */
3111+ uint32_t pending; /* nb of bytes in the pending buffer */
3112 int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
3113 gz_headerp gzhead; /* gzip header information to write */
3114- unsigned int gzindex; /* where in extra, name, or comment */
3115+ uint32_t gzindex; /* where in extra, name, or comment */
3116 unsigned char method; /* can only be DEFLATED */
3117 int last_flush; /* value of flush param for previous deflate call */
3118
3119@@ -258,7 +261,7 @@ typedef struct internal_state {
3120 unsigned int matches; /* number of string matches in current block */
3121 unsigned int insert; /* bytes at end of window left to insert */
3122
3123-#ifdef DEBUG
3124+#ifdef ZLIB_DEBUG
3125 unsigned long compressed_len; /* total bit length of compressed file mod 2^32 */
3126 unsigned long bits_sent; /* bit length of compressed data sent mod 2^32 */
3127 #endif
3128@@ -278,6 +281,12 @@ typedef struct internal_state {
3129 * longest match routines access bytes past the input. This is then
3130 * updated to the new high water mark.
3131 */
3132+ int block_open;
3133+ /* Whether or not a block is currently open for the QUICK deflation scheme.
3134+ * This is set to 1 if there is an active block, or 0 if the block was just
3135+ * closed.
3136+ */
3137+
3138 } deflate_state;
3139
3140 typedef enum {
3141@@ -304,7 +313,7 @@ typedef enum {
3142 * - no instructions for extracting bytes from short.
3143 * - needs less registers
3144 * - stores to adjacent bytes are merged into a single store, albeit at the
3145- * cost of penalty of potentially unaligned access.
3146+ * cost of penalty of potentially unaligned access.
3147 */
3148 #define put_short(s, w) { \
3149 s->pending += 2; \
3150@@ -346,7 +355,7 @@ void ZLIB_INTERNAL bi_windup(deflate_state *s);
3151 * used.
3152 */
3153
3154-#ifndef DEBUG
3155+#ifndef ZLIB_DEBUG
3156 /* Inline versions of _tr_tally for speed: */
3157
3158 # if defined(GEN_TREES_H)
3159@@ -386,10 +395,17 @@ void ZLIB_INTERNAL bi_windup(deflate_state *s);
3160 * input characters, so that a running hash key can be computed from the
3161 * previous key instead of complete recalculation each time.
3162 */
3163+
3164+#ifdef NOT_TWEAK_COMPILER
3165+#define TRIGGER_LEVEL 6
3166+#else
3167+#define TRIGGER_LEVEL 5
3168+#endif
3169+
3170 #ifdef X86_SSE4_2_CRC_HASH
3171 #define UPDATE_HASH(s, h, i) \
3172 do {\
3173- if (s->level < 6) \
3174+ if (s->level < TRIGGER_LEVEL) \
3175 h = (3483 * (s->window[i]) +\
3176 23081* (s->window[i+1]) +\
3177 6954 * (s->window[i+2]) +\
3178@@ -400,14 +416,14 @@ void ZLIB_INTERNAL bi_windup(deflate_state *s);
3179 25811* (s->window[i+2])) & s->hash_mask;\
3180 } while (0)
3181 #else
3182-# define UPDATE_HASH(s, h, i) (h = (((h) << s->hash_shift) ^ (s->window[i + (MIN_MATCH-1)])) & s->hash_mask)
3183+# define UPDATE_HASH(s, h, i) (h = ((h << s->hash_shift) ^ (s->window[i + (MIN_MATCH-1)])) & s->hash_mask)
3184 #endif
3185
3186-#ifndef DEBUG
3187+#ifndef ZLIB_DEBUG
3188 # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
3189 /* Send a code of the given tree. c and tree must not have side effects */
3190
3191-#else /* DEBUG */
3192+#else /* ZLIB_DEBUG */
3193 # define send_code(s, c, tree) \
3194 { if (z_verbose > 2) { \
3195 fprintf(stderr, "\ncd %3d ", (c)); \
3196@@ -416,30 +432,8 @@ void ZLIB_INTERNAL bi_windup(deflate_state *s);
3197 }
3198 #endif
3199
3200-#ifdef DEBUG
3201-/* ===========================================================================
3202- * Send a value on a given number of bits.
3203- * IN assertion: length <= 16 and value fits in length bits.
3204- */
3205-local void send_bits(deflate_state *s, int value, int length) {
3206- Tracevv((stderr, " l %2d v %4x ", length, value));
3207- Assert(length > 0 && length <= 15, "invalid length");
3208- s->bits_sent += (unsigned long)length;
3209-
3210- /* If not enough room in bi_buf, use (valid) bits from bi_buf and
3211- * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
3212- * unused bits in value.
3213- */
3214- if (s->bi_valid > (int)Buf_size - length) {
3215- s->bi_buf |= (uint16_t)value << s->bi_valid;
3216- put_short(s, s->bi_buf);
3217- s->bi_buf = (uint16_t)value >> (Buf_size - s->bi_valid);
3218- s->bi_valid += length - Buf_size;
3219- } else {
3220- s->bi_buf |= (uint16_t)value << s->bi_valid;
3221- s->bi_valid += length;
3222- }
3223-}
3224+#ifdef ZLIB_DEBUG
3225+void send_bits(deflate_state *s, int value, int length);
3226 #else
3227 #define send_bits(s, value, length) \
3228 { int len = length;\
3229diff --git a/deflate_fast.c b/deflate_fast.c
3230index edfe53d..c169053 100644
3231--- a/deflate_fast.c
3232+++ b/deflate_fast.c
3233@@ -39,7 +39,7 @@ block_state deflate_fast(deflate_state *s, int flush) {
3234 */
3235 hash_head = NIL;
3236 if (s->lookahead >= MIN_MATCH) {
3237- hash_head = insert_string(s, s->strstart);
3238+ hash_head = insert_string(s, s->strstart, 1);
3239 }
3240
3241 /* Find the longest match, discarding those <= prev_length.
3242@@ -68,7 +68,7 @@ block_state deflate_fast(deflate_state *s, int flush) {
3243 s->strstart++;
3244 #ifdef NOT_TWEAK_COMPILER
3245 do {
3246- insert_string(s, s->strstart);
3247+ insert_string(s, s->strstart, 1);
3248 s->strstart++;
3249 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
3250 * always MIN_MATCH bytes ahead.
3251@@ -76,7 +76,7 @@ block_state deflate_fast(deflate_state *s, int flush) {
3252 } while (--s->match_length != 0);
3253 #else
3254 {
3255- bulk_insert_str(s, s->strstart, s->match_length);
3256+ insert_string(s, s->strstart, s->match_length);
3257 s->strstart += s->match_length;
3258 s->match_length = 0;
3259 }
3260@@ -85,9 +85,13 @@ block_state deflate_fast(deflate_state *s, int flush) {
3261 s->strstart += s->match_length;
3262 s->match_length = 0;
3263 s->ins_h = s->window[s->strstart];
3264- UPDATE_HASH(s, s->ins_h, s->strstart+2 - (MIN_MATCH));
3265+#ifndef NOT_TWEAK_COMPILER
3266+ insert_string(s, s->strstart + 2 - MIN_MATCH, MIN_MATCH - 2);
3267+#else
3268+ insert_string(s, s->strstart + 2 - MIN_MATCH, 1);
3269 #if MIN_MATCH != 3
3270- Call UPDATE_HASH() MIN_MATCH-3 more times
3271+#warning Call insert_string() MIN_MATCH-3 more times
3272+#endif
3273 #endif
3274 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
3275 * matter since it will be recomputed at next deflate call.
3276diff --git a/deflate_medium.c b/deflate_medium.c
3277index 731b8a2..046449f 100644
3278--- a/deflate_medium.c
3279+++ b/deflate_medium.c
3280@@ -63,22 +63,23 @@ static void insert_match(deflate_state *s, struct match match) {
3281
3282 if (match.match_length) {
3283 if (match.strstart >= match.orgstart) {
3284- insert_string(s, match.strstart);
3285+ insert_string(s, match.strstart, 1);
3286 }
3287 }
3288 }
3289 #else
3290- if (likely(match.match_length == 1)) {
3291- match.strstart++;
3292- match.match_length = 0;
3293- }else{
3294- match.strstart++;
3295- match.match_length--;
3296+ match.strstart++;
3297+ match.match_length--;
3298+ if (match.match_length > 0) {
3299 if (match.strstart >= match.orgstart) {
3300- bulk_insert_str(s, match.strstart, match.match_length);
3301+ if (match.strstart + match.match_length - 1 >= match.orgstart) {
3302+ insert_string(s, match.strstart, match.match_length);
3303+ } else {
3304+ insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
3305+ }
3306+ match.strstart += match.match_length;
3307+ match.match_length = 0;
3308 }
3309- match.strstart += match.match_length;
3310- match.match_length = 0;
3311 }
3312 #endif
3313 return;
3314@@ -93,7 +94,7 @@ static void insert_match(deflate_state *s, struct match match) {
3315 #ifdef NOT_TWEAK_COMPILER
3316 do {
3317 if (likely(match.strstart >= match.orgstart)) {
3318- insert_string(s, match.strstart);
3319+ insert_string(s, match.strstart, 1);
3320 }
3321 match.strstart++;
3322 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
3323@@ -102,7 +103,11 @@ static void insert_match(deflate_state *s, struct match match) {
3324 } while (--match.match_length != 0);
3325 #else
3326 if (likely(match.strstart >= match.orgstart)) {
3327- bulk_insert_str(s, match.strstart, match.match_length);
3328+ if (likely(match.strstart + match.match_length - 1 >= match.orgstart)) {
3329+ insert_string(s, match.strstart, match.match_length);
3330+ } else {
3331+ insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
3332+ }
3333 }
3334 match.strstart += match.match_length;
3335 match.match_length = 0;
3336@@ -111,10 +116,14 @@ static void insert_match(deflate_state *s, struct match match) {
3337 match.strstart += match.match_length;
3338 match.match_length = 0;
3339 s->ins_h = s->window[match.strstart];
3340- if (match.strstart >= 1)
3341- UPDATE_HASH(s, s->ins_h, match.strstart+2-MIN_MATCH);
3342+ if (match.strstart >= (MIN_MATCH - 2))
3343+#ifndef NOT_TWEAK_COMPILER
3344+ insert_string(s, match.strstart + 2 - MIN_MATCH, MIN_MATCH - 2);
3345+#else
3346+ insert_string(s, match.strstart + 2 - MIN_MATCH, 1);
3347 #if MIN_MATCH != 3
3348-#warning Call UPDATE_HASH() MIN_MATCH-3 more times
3349+#warning Call insert_string() MIN_MATCH-3 more times
3350+#endif
3351 #endif
3352 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
3353 * matter since it will be recomputed at next deflate call.
3354@@ -229,7 +238,7 @@ block_state deflate_medium(deflate_state *s, int flush) {
3355 } else {
3356 hash_head = 0;
3357 if (s->lookahead >= MIN_MATCH) {
3358- hash_head = insert_string(s, s->strstart);
3359+ hash_head = insert_string(s, s->strstart, 1);
3360 }
3361
3362 /* set up the initial match to be a 1 byte literal */
3363@@ -263,7 +272,7 @@ block_state deflate_medium(deflate_state *s, int flush) {
3364 /* now, look ahead one */
3365 if (s->lookahead > MIN_LOOKAHEAD) {
3366 s->strstart = current_match.strstart + current_match.match_length;
3367- hash_head = insert_string(s, s->strstart);
3368+ hash_head = insert_string(s, s->strstart, 1);
3369
3370 /* set up the initial match to be a 1 byte literal */
3371 next_match.match_start = 0;
3372diff --git a/deflate_p.h b/deflate_p.h
3373index 4b8282d..4229b3e 100644
3374--- a/deflate_p.h
3375+++ b/deflate_p.h
3376@@ -15,7 +15,7 @@
3377
3378 /* Forward declare common non-inlined functions declared in deflate.c */
3379
3380-#ifdef DEBUG
3381+#ifdef ZLIB_DEBUG
3382 void check_match(deflate_state *s, IPos start, IPos match, int length);
3383 #else
3384 #define check_match(s, start, match, length)
3385@@ -33,45 +33,34 @@ void flush_pending(z_stream *strm);
3386 */
3387
3388 #ifdef X86_SSE4_2_CRC_HASH
3389-extern Pos insert_string_sse(deflate_state *const s, const Pos str, uInt count);
3390+extern Pos insert_string_sse(deflate_state *const s, const Pos str, unsigned int count);
3391 #endif
3392
3393-local inline Pos insert_string_c(deflate_state *const s, const Pos str, uInt count) {
3394+static inline Pos insert_string_c(deflate_state *const s, const Pos str, unsigned int count) {
3395 Pos ret = 0;
3396- uInt idx;
3397+ unsigned int idx;
3398+ unsigned int h = s->ins_h;
3399
3400 for (idx = 0; idx < count; idx++) {
3401- UPDATE_HASH(s, s->ins_h, str+idx);
3402- if (s->head[s->ins_h] != str+idx) {
3403- s->prev[(str+idx) & s->w_mask] = s->head[s->ins_h];
3404- s->head[s->ins_h] = str+idx;
3405+ UPDATE_HASH(s, h, str+idx);
3406+ if (s->head[h] != str+idx) {
3407+ s->prev[(str+idx) & s->w_mask] = s->head[h];
3408+ s->head[h] = str+idx;
3409 }
3410 }
3411+ s->ins_h = h;
3412 ret = s->prev[(str+count-1) & s->w_mask];
3413 return ret;
3414 }
3415
3416-local inline Pos insert_string(deflate_state *const s, const Pos str) {
3417+static inline Pos insert_string(deflate_state *const s, const Pos str, unsigned int count) {
3418 #ifdef X86_SSE4_2_CRC_HASH
3419 if (x86_cpu_has_sse42)
3420- return insert_string_sse(s, str, 1);
3421+ return insert_string_sse(s, str, count);
3422 #endif
3423- return insert_string_c(s, str, 1);
3424+ return insert_string_c(s, str, count);
3425 }
3426
3427-#ifndef NOT_TWEAK_COMPILER
3428-local inline void bulk_insert_str(deflate_state *const s, Pos startpos, uInt count) {
3429-# ifdef X86_SSE4_2_CRC_HASH
3430- if (x86_cpu_has_sse42) {
3431- insert_string_sse(s, startpos, count);
3432- } else
3433-# endif
3434- {
3435- insert_string_c(s, startpos, count);
3436- }
3437-}
3438-#endif /* NOT_TWEAK_COMPILER */
3439-
3440 /* ===========================================================================
3441 * Flush the current block, with given end-of-file flag.
3442 * IN assertion: strstart is set to the end of the current match.
3443@@ -79,8 +68,8 @@ local inline void bulk_insert_str(deflate_state *const s, Pos startpos, uInt cou
3444 #define FLUSH_BLOCK_ONLY(s, last) { \
3445 _tr_flush_block(s, (s->block_start >= 0L ? \
3446 (char *)&s->window[(unsigned)s->block_start] : \
3447- (char *)Z_NULL), \
3448- (ulg)((long)s->strstart - s->block_start), \
3449+ NULL), \
3450+ (unsigned long)((long)s->strstart - s->block_start), \
3451 (last)); \
3452 s->block_start = s->strstart; \
3453 flush_pending(s->strm); \
3454diff --git a/deflate_slow.c b/deflate_slow.c
3455index 6a855f0..c0be3ea 100644
3456--- a/deflate_slow.c
3457+++ b/deflate_slow.c
3458@@ -47,7 +47,7 @@ block_state deflate_slow(deflate_state *s, int flush) {
3459 */
3460 hash_head = NIL;
3461 if (s->lookahead >= MIN_MATCH) {
3462- hash_head = insert_string(s, s->strstart);
3463+ hash_head = insert_string(s, s->strstart, 1);
3464 }
3465
3466 /* Find the longest match, discarding those <= prev_length.
3467@@ -79,7 +79,7 @@ block_state deflate_slow(deflate_state *s, int flush) {
3468 * match is not better, output the previous match:
3469 */
3470 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
3471- uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
3472+ unsigned int max_insert = s->strstart + s->lookahead - MIN_MATCH;
3473 /* Do not insert strings in hash table beyond this. */
3474
3475 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
3476@@ -97,7 +97,7 @@ block_state deflate_slow(deflate_state *s, int flush) {
3477 s->prev_length -= 2;
3478 do {
3479 if (++s->strstart <= max_insert) {
3480- insert_string(s, s->strstart);
3481+ insert_string(s, s->strstart, 1);
3482 }
3483 } while (--s->prev_length != 0);
3484 s->match_available = 0;
3485@@ -105,12 +105,12 @@ block_state deflate_slow(deflate_state *s, int flush) {
3486 s->strstart++;
3487 #else
3488 {
3489- uInt mov_fwd = s->prev_length - 2;
3490- uInt insert_cnt = mov_fwd;
3491+ unsigned int mov_fwd = s->prev_length - 2;
3492+ unsigned int insert_cnt = mov_fwd;
3493 if (unlikely(insert_cnt > max_insert - s->strstart))
3494 insert_cnt = max_insert - s->strstart;
3495
3496- bulk_insert_str(s, s->strstart + 1, insert_cnt);
3497+ insert_string(s, s->strstart + 1, insert_cnt);
3498 s->prev_length = 0;
3499 s->match_available = 0;
3500 s->match_length = MIN_MATCH-1;
3501diff --git a/gzguts.h b/gzguts.h
3502index 0921f17..e075bfa 100644
3503--- a/gzguts.h
3504+++ b/gzguts.h
3505@@ -64,11 +64,6 @@
3506 # define snprintf _snprintf
3507 #endif
3508
3509-#ifndef local
3510-# define local static
3511-#endif
3512-/* compile with -Dlocal if your debugger can't find static symbols */
3513-
3514 /* get errno and strerror definition */
3515 #ifndef NO_STRERROR
3516 # include <errno.h>
3517diff --git a/gzlib.c b/gzlib.c
3518index e5ebe5f..f152c1c 100644
3519--- a/gzlib.c
3520+++ b/gzlib.c
3521@@ -16,11 +16,11 @@
3522 #endif
3523
3524 /* Local functions */
3525-local void gz_reset(gz_statep);
3526-local gzFile gz_open(const void *, int, const char *);
3527+static void gz_reset(gz_statep);
3528+static gzFile gz_open(const void *, int, const char *);
3529
3530 /* Reset gzip file state */
3531-local void gz_reset(gz_statep state) {
3532+static void gz_reset(gz_statep state) {
3533 state->x.have = 0; /* no output data available */
3534 if (state->mode == GZ_READ) { /* for reading ... */
3535 state->eof = 0; /* not at end of file */
3536@@ -34,7 +34,7 @@ local void gz_reset(gz_statep state) {
3537 }
3538
3539 /* Open a gzip file either by name or file descriptor. */
3540-local gzFile gz_open(const void *path, int fd, const char *mode) {
3541+static gzFile gz_open(const void *path, int fd, const char *mode) {
3542 gz_statep state;
3543 size_t len;
3544 int oflag;
3545@@ -496,7 +496,6 @@ void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg) {
3546 return;
3547 }
3548 snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, "%s%s%s", state->path, ": ", msg);
3549- return;
3550 }
3551
3552 #ifndef INT_MAX
3553diff --git a/gzread.c b/gzread.c
3554index 5db2c50..57c115a 100644
3555--- a/gzread.c
3556+++ b/gzread.c
3557@@ -6,23 +6,28 @@
3558 #include "gzguts.h"
3559
3560 /* Local functions */
3561-local int gz_load(gz_statep, unsigned char *, unsigned, unsigned *);
3562-local int gz_avail(gz_statep);
3563-local int gz_look(gz_statep);
3564-local int gz_decomp(gz_statep);
3565-local int gz_fetch(gz_statep);
3566-local int gz_skip(gz_statep, z_off64_t);
3567+static int gz_load(gz_statep, unsigned char *, unsigned, unsigned *);
3568+static int gz_avail(gz_statep);
3569+static int gz_look(gz_statep);
3570+static int gz_decomp(gz_statep);
3571+static int gz_fetch(gz_statep);
3572+static int gz_skip(gz_statep, z_off64_t);
3573+static size_t gz_read(gz_statep, void *, size_t);
3574
3575 /* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from
3576 state->fd, and update state->eof, state->err, and state->msg as appropriate.
3577 This function needs to loop on read(), since read() is not guaranteed to
3578 read the number of bytes requested, depending on the type of descriptor. */
3579-local int gz_load(gz_statep state, unsigned char *buf, unsigned len, unsigned *have) {
3580+static int gz_load(gz_statep state, unsigned char *buf, unsigned len, unsigned *have) {
3581 int ret;
3582+ unsigned get, max = ((unsigned)-1 >> 2) + 1;
3583
3584 *have = 0;
3585 do {
3586- ret = read(state->fd, buf + *have, len - *have);
3587+ get = len - *have;
3588+ if (get > max)
3589+ get = max;
3590+ ret = read(state->fd, buf + *have, get);
3591 if (ret <= 0)
3592 break;
3593 *have += ret;
3594@@ -43,7 +48,7 @@ local int gz_load(gz_statep state, unsigned char *buf, unsigned len, unsigned *h
3595 If strm->avail_in != 0, then the current data is moved to the beginning of
3596 the input buffer, and then the remainder of the buffer is loaded with the
3597 available data from the input file. */
3598-local int gz_avail(gz_statep state) {
3599+static int gz_avail(gz_statep state) {
3600 unsigned got;
3601 z_stream *strm = &(state->strm);
3602
3603@@ -75,7 +80,7 @@ local int gz_avail(gz_statep state) {
3604 case, all further file reads will be directly to either the output buffer or
3605 a user buffer. If decompressing, the inflate state will be initialized.
3606 gz_look() will return 0 on success or -1 on failure. */
3607-local int gz_look(gz_statep state) {
3608+static int gz_look(gz_statep state) {
3609 z_stream *strm = &(state->strm);
3610
3611 /* allocate read buffers and inflate memory */
3612@@ -84,21 +89,19 @@ local int gz_look(gz_statep state) {
3613 state->in = (unsigned char *)malloc(state->want);
3614 state->out = (unsigned char *)malloc(state->want << 1);
3615 if (state->in == NULL || state->out == NULL) {
3616- if (state->out != NULL)
3617- free(state->out);
3618- if (state->in != NULL)
3619- free(state->in);
3620+ free(state->out);
3621+ free(state->in);
3622 gz_error(state, Z_MEM_ERROR, "out of memory");
3623 return -1;
3624 }
3625 state->size = state->want;
3626
3627 /* allocate inflate memory */
3628- state->strm.zalloc = Z_NULL;
3629- state->strm.zfree = Z_NULL;
3630- state->strm.opaque = Z_NULL;
3631+ state->strm.zalloc = NULL;
3632+ state->strm.zfree = NULL;
3633+ state->strm.opaque = NULL;
3634 state->strm.avail_in = 0;
3635- state->strm.next_in = Z_NULL;
3636+ state->strm.next_in = NULL;
3637 if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
3638 free(state->out);
3639 free(state->in);
3640@@ -159,7 +162,7 @@ local int gz_look(gz_statep state) {
3641 data. If the gzip stream completes, state->how is reset to LOOK to look for
3642 the next gzip stream or raw data, once state->x.have is depleted. Returns 0
3643 on success, -1 on failure. */
3644-local int gz_decomp(gz_statep state) {
3645+static int gz_decomp(gz_statep state) {
3646 int ret = Z_OK;
3647 unsigned had;
3648 z_stream *strm = &(state->strm);
3649@@ -209,7 +212,7 @@ local int gz_decomp(gz_statep state) {
3650 looked for to determine whether to copy or decompress. Returns -1 on error,
3651 otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the
3652 end of the input file has been reached and all data has been processed. */
3653-local int gz_fetch(gz_statep state) {
3654+static int gz_fetch(gz_statep state) {
3655 z_stream *strm = &(state->strm);
3656
3657 do {
3658@@ -237,7 +240,7 @@ local int gz_fetch(gz_statep state) {
3659 }
3660
3661 /* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
3662-local int gz_skip(gz_statep state, z_off64_t len) {
3663+static int gz_skip(gz_statep state, z_off64_t len) {
3664 unsigned n;
3665
3666 /* skip over len bytes or reach end-of-file, whichever comes first */
3667@@ -262,28 +265,14 @@ local int gz_skip(gz_statep state, z_off64_t len) {
3668 return 0;
3669 }
3670
3671-/* -- see zlib.h -- */
3672-int ZEXPORT gzread(gzFile file, void *buf, unsigned len) {
3673- unsigned got, n;
3674- gz_statep state;
3675- z_stream *strm;
3676-
3677- /* get internal structure */
3678- if (file == NULL)
3679- return -1;
3680- state = (gz_statep)file;
3681- strm = &(state->strm);
3682-
3683- /* check that we're reading and that there's no (serious) error */
3684- if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
3685- return -1;
3686-
3687- /* since an int is returned, make sure len fits in one, otherwise return
3688- with an error (this avoids the flaw in the interface) */
3689- if ((int)len < 0) {
3690- gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
3691- return -1;
3692- }
3693+/* Read len bytes into buf from file, or less than len up to the end of the
3694+ input. Return the number of bytes read. If zero is returned, either the
3695+ end of file was reached, or there was an error. state->err must be
3696+ consulted in that case to determine which. */
3697+static size_t gz_read(gz_statep state, void *buf, size_t len)
3698+{
3699+ size_t got;
3700+ unsigned n;
3701
3702 /* if len is zero, avoid unnecessary operations */
3703 if (len == 0)
3704@@ -293,40 +282,55 @@ int ZEXPORT gzread(gzFile file, void *buf, unsigned len) {
3705 if (state->seek) {
3706 state->seek = 0;
3707 if (gz_skip(state, state->skip) == -1)
3708- return -1;
3709+ return 0;
3710 }
3711
3712 /* get len bytes to buf, or less than len if at the end */
3713 got = 0;
3714 do {
3715+ /* set n to the maximum amount of len that fits in an unsigned int */
3716+ n = -1;
3717+ if (n > len)
3718+ n = len;
3719+
3720 /* first just try copying data from the output buffer */
3721 if (state->x.have) {
3722- n = state->x.have > len ? len : state->x.have;
3723+ if (state->x.have < n)
3724+ n = state->x.have;
3725 memcpy(buf, state->x.next, n);
3726 state->x.next += n;
3727 state->x.have -= n;
3728- } else if (state->eof && strm->avail_in == 0) {
3729- /* output buffer empty -- return if we're at the end of the input */
3730+ }
3731+
3732+ /* output buffer empty -- return if we're at the end of the input */
3733+ else if (state->eof && state->strm.avail_in == 0) {
3734 state->past = 1; /* tried to read past end */
3735 break;
3736- } else if (state->how == LOOK || len < (state->size << 1)) {
3737- /* need output data -- for small len or new stream load up our output buffer */
3738+ }
3739+
3740+ /* need output data -- for small len or new stream load up our output
3741+ buffer */
3742+ else if (state->how == LOOK || n < (state->size << 1)) {
3743 /* get more output, looking for header if required */
3744 if (gz_fetch(state) == -1)
3745- return -1;
3746+ return 0;
3747 continue; /* no progress yet -- go back to copy above */
3748 /* the copy above assures that we will leave with space in the
3749 output buffer, allowing at least one gzungetc() to succeed */
3750- } else if (state->how == COPY) { /* read directly */
3751- /* large len -- read directly into user buffer */
3752- if (gz_load(state, (unsigned char *)buf, len, &n) == -1)
3753- return -1;
3754- } else { /* state->how == GZIP */
3755- /* large len -- decompress directly into user buffer */
3756- strm->avail_out = len;
3757- strm->next_out = (unsigned char *)buf;
3758+ }
3759+
3760+ /* large len -- read directly into user buffer */
3761+ else if (state->how == COPY) { /* read directly */
3762+ if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
3763+ return 0;
3764+ }
3765+
3766+ /* large len -- decompress directly into user buffer */
3767+ else { /* state->how == GZIP */
3768+ state->strm.avail_out = n;
3769+ state->strm.next_out = (unsigned char *)buf;
3770 if (gz_decomp(state) == -1)
3771- return -1;
3772+ return 0;
3773 n = state->x.have;
3774 state->x.have = 0;
3775 }
3776@@ -338,8 +342,71 @@ int ZEXPORT gzread(gzFile file, void *buf, unsigned len) {
3777 state->x.pos += n;
3778 } while (len);
3779
3780- /* return number of bytes read into user buffer (will fit in int) */
3781- return (int)got;
3782+ /* return number of bytes read into user buffer */
3783+ return got;
3784+}
3785+
3786+/* -- see zlib.h -- */
3787+int ZEXPORT gzread(file, buf, len)
3788+ gzFile file;
3789+ voidp buf;
3790+ unsigned len;
3791+{
3792+ gz_statep state;
3793+
3794+ /* get internal structure */
3795+ if (file == NULL)
3796+ return -1;
3797+ state = (gz_statep)file;
3798+
3799+ /* check that we're reading and that there's no (serious) error */
3800+ if (state->mode != GZ_READ ||
3801+ (state->err != Z_OK && state->err != Z_BUF_ERROR))
3802+ return -1;
3803+
3804+ /* since an int is returned, make sure len fits in one, otherwise return
3805+ with an error (this avoids a flaw in the interface) */
3806+ if ((int)len < 0) {
3807+ gz_error(state, Z_STREAM_ERROR, "request does not fit in an int");
3808+ return -1;
3809+ }
3810+
3811+ /* read len or fewer bytes to buf */
3812+ len = gz_read(state, buf, len);
3813+
3814+ /* check for an error */
3815+ if (len == 0 && state->err != Z_OK && state->err != Z_BUF_ERROR)
3816+ return -1;
3817+
3818+ /* return the number of bytes read (this is assured to fit in an int) */
3819+ return (int)len;
3820+}
3821+
3822+/* -- see zlib.h -- */
3823+size_t ZEXPORT gzfread(void *buf, size_t size, size_t nitems, gzFile file)
3824+{
3825+ size_t len;
3826+ gz_statep state;
3827+
3828+ /* get internal structure */
3829+ if (file == NULL)
3830+ return 0;
3831+ state = (gz_statep)file;
3832+
3833+ /* check that we're reading and that there's no (serious) error */
3834+ if (state->mode != GZ_READ ||
3835+ (state->err != Z_OK && state->err != Z_BUF_ERROR))
3836+ return 0;
3837+
3838+ /* compute bytes to read -- error on overflow */
3839+ len = nitems * size;
3840+ if (size && len / size != nitems) {
3841+ gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
3842+ return 0;
3843+ }
3844+
3845+ /* read len or fewer bytes to buf, return the number of full items read */
3846+ return len ? gz_read(state, buf, len) / size : 0;
3847 }
3848
3849 /* -- see zlib.h -- */
3850@@ -365,8 +432,8 @@ int ZEXPORT gzgetc(gzFile file) {
3851 return *(state->x.next)++;
3852 }
3853
3854- /* nothing there -- try gzread() */
3855- ret = gzread(file, buf, 1);
3856+ /* nothing there -- try gz_read() */
3857+ ret = gz_read(state, buf, 1);
3858 return ret < 1 ? -1 : buf[0];
3859 }
3860
3861diff --git a/gzwrite.c b/gzwrite.c
3862index dedaee0..e802449 100644
3863--- a/gzwrite.c
3864+++ b/gzwrite.c
3865@@ -7,13 +7,14 @@
3866 #include "gzguts.h"
3867
3868 /* Local functions */
3869-local int gz_init(gz_statep);
3870-local int gz_comp(gz_statep, int);
3871-local int gz_zero(gz_statep, z_off64_t);
3872+static int gz_init(gz_statep);
3873+static int gz_comp(gz_statep, int);
3874+static int gz_zero(gz_statep, z_off64_t);
3875+static size_t gz_write(gz_statep, void const *, size_t);
3876
3877 /* Initialize state for writing a gzip file. Mark initialization by setting
3878- state->size to non-zero. Return -1 on failure or 0 on success. */
3879-local int gz_init(gz_statep state) {
3880+ state->size to non-zero. Return -1 on a memory allocation failure, or 0 on success. */
3881+static int gz_init(gz_statep state) {
3882 int ret;
3883 z_stream *strm = &(state->strm);
3884
3885@@ -35,9 +36,9 @@ local int gz_init(gz_statep state) {
3886 }
3887
3888 /* allocate deflate memory, set up for gzip compression */
3889- strm->zalloc = Z_NULL;
3890- strm->zfree = Z_NULL;
3891- strm->opaque = Z_NULL;
3892+ strm->zalloc = NULL;
3893+ strm->zfree = NULL;
3894+ strm->opaque = NULL;
3895 ret = deflateInit2(strm, state->level, Z_DEFLATED, MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
3896 if (ret != Z_OK) {
3897 free(state->out);
3898@@ -61,14 +62,14 @@ local int gz_init(gz_statep state) {
3899 }
3900
3901 /* Compress whatever is at avail_in and next_in and write to the output file.
3902- Return -1 if there is an error writing to the output file, otherwise 0.
3903- flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH,
3904- then the deflate() state is reset to start a new gzip stream. If gz->direct
3905- is true, then simply write to the output file without compressing, and
3906- ignore flush. */
3907-local int gz_comp(gz_statep state, int flush) {
3908- int ret, got;
3909- unsigned have;
3910+ Return -1 if there is an error writing to the output file or if gz_init()
3911+ fails to allocate memory, otherwise 0. flush is assumed to be a valid
3912+ deflate() flush value. If flush is Z_FINISH, then the deflate() state is
3913+ reset to start a new gzip stream. If gz->direct is true, then simply write
3914+ to the output file without compressing, and ignore flush. */
3915+static int gz_comp(gz_statep state, int flush) {
3916+ int ret, writ;
3917+ unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
3918 z_stream *strm = &(state->strm);
3919
3920 /* allocate memory if this is the first time through */
3921@@ -78,12 +79,14 @@ local int gz_comp(gz_statep state, int flush) {
3922 /* write directly if requested */
3923 if (state->direct) {
3924 while (strm->avail_in) {
3925- if ((got = write(state->fd, strm->next_in, strm->avail_in)) < 0) {
3926+ put = strm->avail_in > max ? max : strm->avail_in;
3927+ writ = write(state->fd, strm->next_in, put);
3928+ if (writ < 0) {
3929 gz_error(state, Z_ERRNO, zstrerror());
3930 return -1;
3931 }
3932- strm->avail_in -= got;
3933- strm->next_in += got;
3934+ strm->avail_in -= (unsigned)writ;
3935+ strm->next_in += writ;
3936 }
3937 return 0;
3938 }
3939@@ -95,11 +98,14 @@ local int gz_comp(gz_statep state, int flush) {
3940 doing Z_FINISH then don't write until we get to Z_STREAM_END */
3941 if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && (flush != Z_FINISH || ret == Z_STREAM_END))) {
3942 while (strm->next_out > state->x.next) {
3943- if ((got = write(state->fd, state->x.next, strm->next_out - state->x.next)) < 0) {
3944+ put = strm->next_out - state->x.next > (int)max ? max :
3945+ (unsigned)(strm->next_out - state->x.next);
3946+ writ = write(state->fd, state->x.next, put);
3947+ if (writ < 0) {
3948 gz_error(state, Z_ERRNO, zstrerror());
3949 return -1;
3950 }
3951- state->x.next += got;
3952+ state->x.next += writ;
3953 }
3954 if (strm->avail_out == 0) {
3955 strm->avail_out = state->size;
3956@@ -125,8 +131,9 @@ local int gz_comp(gz_statep state, int flush) {
3957 return 0;
3958 }
3959
3960-/* Compress len zeros to output. Return -1 on error, 0 on success. */
3961-local int gz_zero(gz_statep state, z_off64_t len) {
3962+/* Compress len zeros to output. Return -1 on a write error or memory
3963+ allocation failure by gz_comp(), or 0 on success. */
3964+static int gz_zero(gz_statep state, z_off64_t len) {
3965 int first;
3966 unsigned n;
3967 z_stream *strm = &(state->strm);
3968@@ -153,28 +160,11 @@ local int gz_zero(gz_statep state, z_off64_t len) {
3969 return 0;
3970 }
3971
3972-/* -- see zlib.h -- */
3973-int ZEXPORT gzwrite(gzFile file, void const *buf, unsigned len) {
3974- unsigned put = len;
3975- gz_statep state;
3976- z_stream *strm;
3977-
3978- /* get internal structure */
3979- if (file == NULL)
3980- return 0;
3981- state = (gz_statep)file;
3982- strm = &(state->strm);
3983-
3984- /* check that we're writing and that there's no error */
3985- if (state->mode != GZ_WRITE || state->err != Z_OK)
3986- return 0;
3987-
3988- /* since an int is returned, make sure len fits in one, otherwise return
3989- with an error (this avoids the flaw in the interface) */
3990- if ((int)len < 0) {
3991- gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
3992- return 0;
3993- }
3994+/* Write len bytes from buf to file. Return the number of bytes written. If
3995+ the returned value is less than len, then there was an error. */
3996+static size_t gz_write(gz_statep state, void const *buf, size_t len)
3997+{
3998+ size_t put = len;
3999
4000 /* if len is zero, avoid unnecessary operations */
4001 if (len == 0)
4002@@ -197,14 +187,15 @@ int ZEXPORT gzwrite(gzFile file, void const *buf, unsigned len) {
4003 do {
4004 unsigned have, copy;
4005
4006- if (strm->avail_in == 0)
4007- strm->next_in = state->in;
4008- have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
4009+ if (state->strm.avail_in == 0)
4010+ state->strm.next_in = state->in;
4011+ have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
4012+ state->in);
4013 copy = state->size - have;
4014 if (copy > len)
4015 copy = len;
4016 memcpy(state->in + have, buf, copy);
4017- strm->avail_in += copy;
4018+ state->strm.avail_in += copy;
4019 state->x.pos += copy;
4020 buf = (const char *)buf + copy;
4021 len -= copy;
4022@@ -213,19 +204,75 @@ int ZEXPORT gzwrite(gzFile file, void const *buf, unsigned len) {
4023 } while (len);
4024 } else {
4025 /* consume whatever's left in the input buffer */
4026- if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
4027+ if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
4028 return 0;
4029
4030 /* directly compress user buffer to file */
4031- strm->avail_in = len;
4032- strm->next_in = (const unsigned char *)buf;
4033- state->x.pos += len;
4034- if (gz_comp(state, Z_NO_FLUSH) == -1)
4035- return 0;
4036+ state->strm.next_in = (const unsigned char *)buf;
4037+ do {
4038+ unsigned n = -1;
4039+ if (n > len)
4040+ n = len;
4041+ state->strm.avail_in = n;
4042+ state->x.pos += n;
4043+ if (gz_comp(state, Z_NO_FLUSH) == -1)
4044+ return 0;
4045+ len -= n;
4046+ } while (len);
4047 }
4048
4049- /* input was all buffered or compressed (put will fit in int) */
4050- return (int)put;
4051+ /* input was all buffered or compressed */
4052+ return put;
4053+}
4054+
4055+/* -- see zlib.h -- */
4056+int ZEXPORT gzwrite(gzFile file, void const *buf, unsigned len) {
4057+ gz_statep state;
4058+
4059+ /* get internal structure */
4060+ if (file == NULL)
4061+ return 0;
4062+ state = (gz_statep)file;
4063+
4064+ /* check that we're writing and that there's no error */
4065+ if (state->mode != GZ_WRITE || state->err != Z_OK)
4066+ return 0;
4067+
4068+ /* since an int is returned, make sure len fits in one, otherwise return
4069+ with an error (this avoids a flaw in the interface) */
4070+ if ((int)len < 0) {
4071+ gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
4072+ return 0;
4073+ }
4074+
4075+ /* write len bytes from buf (the return value will fit in an int) */
4076+ return (int)gz_write(state, buf, len);
4077+}
4078+
4079+/* -- see zlib.h -- */
4080+size_t ZEXPORT gzfwrite(void const *buf, size_t size, size_t nitems, gzFile file)
4081+{
4082+ size_t len;
4083+ gz_statep state;
4084+
4085+ /* get internal structure */
4086+ if (file == NULL)
4087+ return 0;
4088+ state = (gz_statep)file;
4089+
4090+ /* check that we're writing and that there's no error */
4091+ if (state->mode != GZ_WRITE || state->err != Z_OK)
4092+ return 0;
4093+
4094+ /* compute bytes to read -- error on overflow */
4095+ len = nitems * size;
4096+ if (size && len / size != nitems) {
4097+ gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
4098+ return 0;
4099+ }
4100+
4101+ /* write len bytes to buf, return the number of full items written */
4102+ return len ? gz_write(state, buf, len) / size : 0;
4103 }
4104
4105 /* -- see zlib.h -- */
4106@@ -267,8 +314,8 @@ int ZEXPORT gzputc(gzFile file, int c) {
4107 }
4108
4109 /* no room in buffer or not initialized, use gz_write() */
4110- buf[0] = c;
4111- if (gzwrite(file, buf, 1) != 1)
4112+ buf[0] = (unsigned char)c;
4113+ if (gz_write(state, buf, 1) != 1)
4114 return -1;
4115 return c & 0xff;
4116 }
4117@@ -276,11 +323,21 @@ int ZEXPORT gzputc(gzFile file, int c) {
4118 /* -- see zlib.h -- */
4119 int ZEXPORT gzputs(gzFile file, const char *str) {
4120 int ret;
4121- unsigned len;
4122+ size_t len;
4123+ gz_statep state;
4124+
4125+ /* get internal structure */
4126+ if (file == NULL)
4127+ return -1;
4128+ state = (gz_statep)file;
4129+
4130+ /* check that we're writing and that there's no error */
4131+ if (state->mode != GZ_WRITE || state->err != Z_OK)
4132+ return -1;
4133
4134 /* write string */
4135- len = (unsigned)strlen(str);
4136- ret = gzwrite(file, str, len);
4137+ len = strlen(str);
4138+ ret = gz_write(state, str, len);
4139 return ret == 0 && len != 0 ? -1 : ret;
4140 }
4141
4142@@ -293,23 +350,23 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) {
4143
4144 /* get internal structure */
4145 if (file == NULL)
4146- return -1;
4147+ return Z_STREAM_ERROR;
4148 state = (gz_statep)file;
4149 strm = &(state->strm);
4150
4151 /* check that we're writing and that there's no error */
4152 if (state->mode != GZ_WRITE || state->err != Z_OK)
4153- return 0;
4154+ return Z_STREAM_ERROR;
4155
4156 /* make sure we have some buffer space */
4157 if (state->size == 0 && gz_init(state) == -1)
4158- return 0;
4159+ return state->err;
4160
4161 /* check for seek request */
4162 if (state->seek) {
4163 state->seek = 0;
4164 if (gz_zero(state, state->skip) == -1)
4165- return 0;
4166+ return state->err;
4167 }
4168
4169 /* do the printf() into the input buffer, put length in len -- the input
4170@@ -332,7 +389,7 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) {
4171 left = strm->avail_in - state->size;
4172 strm->avail_in = state->size;
4173 if (gz_comp(state, Z_NO_FLUSH) == -1)
4174- return 0;
4175+ return state->err;
4176 memcpy(state->in, state->in + state->size, left);
4177 strm->next_in = state->in;
4178 strm->avail_in = left;
4179@@ -356,7 +413,7 @@ int ZEXPORT gzflush(gzFile file, int flush) {
4180
4181 /* get internal structure */
4182 if (file == NULL)
4183- return -1;
4184+ return Z_STREAM_ERROR;
4185 state = (gz_statep)file;
4186
4187 /* check that we're writing and that there's no error */
4188@@ -371,11 +428,11 @@ int ZEXPORT gzflush(gzFile file, int flush) {
4189 if (state->seek) {
4190 state->seek = 0;
4191 if (gz_zero(state, state->skip) == -1)
4192- return -1;
4193+ return state->err;
4194 }
4195
4196 /* compress remaining data with requested flush */
4197- gz_comp(state, flush);
4198+ (void)gz_comp(state, flush);
4199 return state->err;
4200 }
4201
4202@@ -402,13 +459,13 @@ int ZEXPORT gzsetparams(gzFile file, int level, int strategy) {
4203 if (state->seek) {
4204 state->seek = 0;
4205 if (gz_zero(state, state->skip) == -1)
4206- return -1;
4207+ return state->err;
4208 }
4209
4210 /* change compression parameters for subsequent input */
4211 if (state->size) {
4212 /* flush previous input with previous parameters before changing */
4213- if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
4214+ if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
4215 return state->err;
4216 deflateParams(strm, level, strategy);
4217 }
4218diff --git a/infback.c b/infback.c
4219index 9f3dc46..4fc783f 100644
4220--- a/infback.c
4221+++ b/infback.c
4222@@ -16,11 +16,11 @@
4223 #include "inffast.h"
4224
4225 /* function prototypes */
4226-local void fixedtables(struct inflate_state *state);
4227+static void fixedtables(struct inflate_state *state);
4228
4229 /*
4230 strm provides memory allocation functions in zalloc and zfree, or
4231- Z_NULL to use the library memory allocation functions.
4232+ NULL to use the library memory allocation functions.
4233
4234 windowBits is in the range 8..15, and window is a user-supplied
4235 window and output buffer that is 2**windowBits bytes.
4236@@ -29,19 +29,19 @@ int ZEXPORT inflateBackInit_(z_stream *strm, int windowBits, unsigned char *wind
4237 const char *version, int stream_size) {
4238 struct inflate_state *state;
4239
4240- if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || stream_size != (int)(sizeof(z_stream)))
4241+ if (version == NULL || version[0] != ZLIB_VERSION[0] || stream_size != (int)(sizeof(z_stream)))
4242 return Z_VERSION_ERROR;
4243- if (strm == Z_NULL || window == Z_NULL || windowBits < 8 || windowBits > 15)
4244+ if (strm == NULL || window == NULL || windowBits < 8 || windowBits > 15)
4245 return Z_STREAM_ERROR;
4246- strm->msg = Z_NULL; /* in case we return an error */
4247- if (strm->zalloc == (alloc_func)0) {
4248+ strm->msg = NULL; /* in case we return an error */
4249+ if (strm->zalloc == NULL) {
4250 strm->zalloc = zcalloc;
4251 strm->opaque = NULL;
4252 }
4253- if (strm->zfree == (free_func)0)
4254+ if (strm->zfree == NULL)
4255 strm->zfree = zcfree;
4256 state = (struct inflate_state *)ZALLOC(strm, 1, sizeof(struct inflate_state));
4257- if (state == Z_NULL)
4258+ if (state == NULL)
4259 return Z_MEM_ERROR;
4260 Tracev((stderr, "inflate: allocated\n"));
4261 strm->state = (struct internal_state *)state;
4262@@ -64,7 +64,7 @@ int ZEXPORT inflateBackInit_(z_stream *strm, int windowBits, unsigned char *wind
4263 used for threaded applications, since the rewriting of the tables and virgin
4264 may not be thread-safe.
4265 */
4266-local void fixedtables(struct inflate_state *state) {
4267+static void fixedtables(struct inflate_state *state) {
4268 #ifdef BUILDFIXED
4269 static int virgin = 1;
4270 static code *lenfix, *distfix;
4271@@ -143,7 +143,7 @@ local void fixedtables(struct inflate_state *state) {
4272 if (have == 0) { \
4273 have = in(in_desc, &next); \
4274 if (have == 0) { \
4275- next = Z_NULL; \
4276+ next = NULL; \
4277 ret = Z_BUF_ERROR; \
4278 goto inf_leave; \
4279 } \
4280@@ -223,12 +223,12 @@ local void fixedtables(struct inflate_state *state) {
4281
4282 in() should return zero on failure. out() should return non-zero on
4283 failure. If either in() or out() fails, than inflateBack() returns a
4284- Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
4285+ Z_BUF_ERROR. strm->next_in can be checked for NULL to see whether it
4286 was in() or out() that caused in the error. Otherwise, inflateBack()
4287 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
4288 error, or Z_MEM_ERROR if it could not allocate memory for the state.
4289 inflateBack() can also return Z_STREAM_ERROR if the input parameters
4290- are not correct, i.e. strm is Z_NULL or the state was not initialized.
4291+ are not correct, i.e. strm is NULL or the state was not initialized.
4292 */
4293 int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_func out, void *out_desc) {
4294 struct inflate_state *state;
4295@@ -247,17 +247,17 @@ int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_func out,
4296 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
4297
4298 /* Check that the strm exists and that the state was initialized */
4299- if (strm == Z_NULL || strm->state == Z_NULL)
4300+ if (strm == NULL || strm->state == NULL)
4301 return Z_STREAM_ERROR;
4302 state = (struct inflate_state *)strm->state;
4303
4304 /* Reset the state */
4305- strm->msg = Z_NULL;
4306+ strm->msg = NULL;
4307 state->mode = TYPE;
4308 state->last = 0;
4309 state->whave = 0;
4310 next = strm->next_in;
4311- have = next != Z_NULL ? strm->avail_in : 0;
4312+ have = next != NULL ? strm->avail_in : 0;
4313 hold = 0;
4314 bits = 0;
4315 put = state->window;
4316@@ -603,10 +603,10 @@ int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_func out,
4317 }
4318
4319 int ZEXPORT inflateBackEnd(z_stream *strm) {
4320- if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
4321+ if (strm == NULL || strm->state == NULL || strm->zfree == NULL)
4322 return Z_STREAM_ERROR;
4323 ZFREE(strm, strm->state);
4324- strm->state = Z_NULL;
4325+ strm->state = NULL;
4326 Tracev((stderr, "inflate: end\n"));
4327 return Z_OK;
4328 }
4329diff --git a/inffast.c b/inffast.c
4330index 8acb261..50ec86e 100644
4331--- a/inffast.c
4332+++ b/inffast.c
4333@@ -8,25 +8,6 @@
4334 #include "inflate.h"
4335 #include "inffast.h"
4336
4337-/* Allow machine dependent optimization for post-increment or pre-increment.
4338- Based on testing to date,
4339- Pre-increment preferred for:
4340- - PowerPC G3 (Adler)
4341- - MIPS R5000 (Randers-Pehrson)
4342- Post-increment preferred for:
4343- - none
4344- No measurable difference:
4345- - Pentium III (Anderson)
4346- - M68060 (Nikl)
4347- */
4348-#ifdef POSTINC
4349-# define OFF 0
4350-# define PUP(a) *(a)++
4351-#else
4352-# define OFF 1
4353-# define PUP(a) *++(a)
4354-#endif
4355-
4356 /* Return the low n bits of the bit accumulator (n < 16) */
4357 #define BITS(n) \
4358 (hold & ((1U << (n)) - 1))
4359@@ -103,9 +84,9 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
4360
4361 /* copy state to local variables */
4362 state = (struct inflate_state *)strm->state;
4363- in = strm->next_in - OFF;
4364+ in = strm->next_in;
4365 last = in + (strm->avail_in - 5);
4366- out = strm->next_out - OFF;
4367+ out = strm->next_out;
4368 beg = out - (start - strm->avail_out);
4369 end = out + (strm->avail_out - 257);
4370 #ifdef INFLATE_STRICT
4371@@ -126,9 +107,9 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
4372 input data or output space */
4373 do {
4374 if (bits < 15) {
4375- hold += (PUP(in) << bits);
4376+ hold += (uint32_t)(*in++) << bits;
4377 bits += 8;
4378- hold += (PUP(in) << bits);
4379+ hold += (uint32_t)(*in++) << bits;
4380 bits += 8;
4381 }
4382 here = lcode[hold & lmask];
4383@@ -139,13 +120,13 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
4384 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
4385 "inflate: literal '%c'\n" :
4386 "inflate: literal 0x%02x\n", here.val));
4387- PUP(out) = (unsigned char)(here.val);
4388+ *out++ = (unsigned char)(here.val);
4389 } else if (op & 16) { /* length base */
4390 len = here.val;
4391 op &= 15; /* number of extra bits */
4392 if (op) {
4393 if (bits < op) {
4394- hold += (PUP(in) << bits);
4395+ hold += (uint32_t)(*in++) << bits;
4396 bits += 8;
4397 }
4398 len += BITS(op);
4399@@ -153,9 +134,9 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
4400 }
4401 Tracevv((stderr, "inflate: length %u\n", len));
4402 if (bits < 15) {
4403- hold += (PUP(in) << bits);
4404+ hold += (uint32_t)(*in++) << bits;
4405 bits += 8;
4406- hold += (PUP(in) << bits);
4407+ hold += (uint32_t)(*in++) << bits;
4408 bits += 8;
4409 }
4410 here = dcode[hold & dmask];
4411@@ -166,10 +147,10 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
4412 dist = here.val;
4413 op &= 15; /* number of extra bits */
4414 if (bits < op) {
4415- hold += (PUP(in) << bits);
4416+ hold += (uint32_t)(*in++) << bits;
4417 bits += 8;
4418 if (bits < op) {
4419- hold += (PUP(in) << bits);
4420+ hold += (uint32_t)(*in++) << bits;
4421 bits += 8;
4422 }
4423 }
4424@@ -195,30 +176,30 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
4425 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
4426 if (len <= op - whave) {
4427 do {
4428- PUP(out) = 0;
4429+ *out++ = 0;
4430 } while (--len);
4431 continue;
4432 }
4433 len -= op - whave;
4434 do {
4435- PUP(out) = 0;
4436+ *out++ = 0;
4437 } while (--op > whave);
4438 if (op == 0) {
4439 from = out - dist;
4440 do {
4441- PUP(out) = PUP(from);
4442+ *out++ = *from++;
4443 } while (--len);
4444 continue;
4445 }
4446 #endif
4447 }
4448- from = window - OFF;
4449+ from = window;
4450 if (wnext == 0) { /* very common case */
4451 from += wsize - op;
4452 if (op < len) { /* some from window */
4453 len -= op;
4454 do {
4455- PUP(out) = PUP(from);
4456+ *out++ = *from++;
4457 } while (--op);
4458 from = out - dist; /* rest from output */
4459 }
4460@@ -228,14 +209,14 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
4461 if (op < len) { /* some from end of window */
4462 len -= op;
4463 do {
4464- PUP(out) = PUP(from);
4465+ *out++ = *from++;
4466 } while (--op);
4467- from = window - OFF;
4468+ from = window;
4469 if (wnext < len) { /* some from start of window */
4470 op = wnext;
4471 len -= op;
4472 do {
4473- PUP(out) = PUP(from);
4474+ *out++ = *from++;
4475 } while (--op);
4476 from = out - dist; /* rest from output */
4477 }
4478@@ -245,34 +226,34 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
4479 if (op < len) { /* some from window */
4480 len -= op;
4481 do {
4482- PUP(out) = PUP(from);
4483+ *out++ = *from++;
4484 } while (--op);
4485 from = out - dist; /* rest from output */
4486 }
4487 }
4488 while (len > 2) {
4489- PUP(out) = PUP(from);
4490- PUP(out) = PUP(from);
4491- PUP(out) = PUP(from);
4492+ *out++ = *from++;
4493+ *out++ = *from++;
4494+ *out++ = *from++;
4495 len -= 3;
4496 }
4497 if (len) {
4498- PUP(out) = PUP(from);
4499+ *out++ = *from++;
4500 if (len > 1)
4501- PUP(out) = PUP(from);
4502+ *out++ = *from++;
4503 }
4504 } else {
4505 from = out - dist; /* copy direct from output */
4506 do { /* minimum length is three */
4507- PUP(out) = PUP(from);
4508- PUP(out) = PUP(from);
4509- PUP(out) = PUP(from);
4510+ *out++ = *from++;
4511+ *out++ = *from++;
4512+ *out++ = *from++;
4513 len -= 3;
4514 } while (len > 2);
4515 if (len) {
4516- PUP(out) = PUP(from);
4517+ *out++ = *from++;
4518 if (len > 1)
4519- PUP(out) = PUP(from);
4520+ *out++ = *from++;
4521 }
4522 }
4523 } else if ((op & 64) == 0) { /* 2nd level distance code */
4524@@ -304,8 +285,8 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
4525 hold &= (1U << bits) - 1;
4526
4527 /* update state and return */
4528- strm->next_in = in + OFF;
4529- strm->next_out = out + OFF;
4530+ strm->next_in = in;
4531+ strm->next_out = out;
4532 strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
4533 strm->avail_out = (unsigned)(out < end ? 257 + (end - out) : 257 - (out - end));
4534 state->hold = hold;
4535diff --git a/inflate.c b/inflate.c
4536index 8d4a992..462045c 100644
4537--- a/inflate.c
4538+++ b/inflate.c
4539@@ -92,28 +92,40 @@
4540 #endif
4541
4542 /* function prototypes */
4543-local void fixedtables(struct inflate_state *state);
4544-local int updatewindow(z_stream *strm, const unsigned char *end, uint32_t copy);
4545+static int inflateStateCheck(z_stream *strm);
4546+static void fixedtables(struct inflate_state *state);
4547+static int updatewindow(z_stream *strm, const unsigned char *end, uint32_t copy);
4548 #ifdef BUILDFIXED
4549 void makefixed(void);
4550 #endif
4551-local uint32_t syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len);
4552+static uint32_t syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len);
4553+
4554+static int inflateStateCheck(z_stream *strm)
4555+{
4556+ struct inflate_state *state;
4557+ if (strm == NULL || strm->zalloc == NULL || strm->zfree == NULL)
4558+ return 1;
4559+ state = (struct inflate_state *)strm->state;
4560+ if (state == NULL || state->strm != strm || state->mode < HEAD || state->mode > SYNC)
4561+ return 1;
4562+ return 0;
4563+}
4564
4565 int ZEXPORT inflateResetKeep(z_stream *strm) {
4566 struct inflate_state *state;
4567
4568- if (strm == Z_NULL || strm->state == Z_NULL)
4569+ if (inflateStateCheck(strm))
4570 return Z_STREAM_ERROR;
4571 state = (struct inflate_state *)strm->state;
4572 strm->total_in = strm->total_out = state->total = 0;
4573- strm->msg = Z_NULL;
4574+ strm->msg = NULL;
4575 if (state->wrap) /* to support ill-conceived Java test suite */
4576 strm->adler = state->wrap & 1;
4577 state->mode = HEAD;
4578 state->last = 0;
4579 state->havedict = 0;
4580 state->dmax = 32768U;
4581- state->head = Z_NULL;
4582+ state->head = NULL;
4583 state->hold = 0;
4584 state->bits = 0;
4585 state->lencode = state->distcode = state->next = state->codes;
4586@@ -126,7 +138,7 @@ int ZEXPORT inflateResetKeep(z_stream *strm) {
4587 int ZEXPORT inflateReset(z_stream *strm) {
4588 struct inflate_state *state;
4589
4590- if (strm == Z_NULL || strm->state == Z_NULL)
4591+ if (inflateStateCheck(strm))
4592 return Z_STREAM_ERROR;
4593 state = (struct inflate_state *)strm->state;
4594 state->wsize = 0;
4595@@ -140,7 +152,7 @@ int ZEXPORT inflateReset2(z_stream *strm, int windowBits) {
4596 struct inflate_state *state;
4597
4598 /* get the state */
4599- if (strm == Z_NULL || strm->state == Z_NULL)
4600+ if (inflateStateCheck(strm))
4601 return Z_STREAM_ERROR;
4602 state = (struct inflate_state *)strm->state;
4603
4604@@ -149,7 +161,7 @@ int ZEXPORT inflateReset2(z_stream *strm, int windowBits) {
4605 wrap = 0;
4606 windowBits = -windowBits;
4607 } else {
4608- wrap = (windowBits >> 4) + 1;
4609+ wrap = (windowBits >> 4) + 5;
4610 #ifdef GUNZIP
4611 if (windowBits < 48)
4612 windowBits &= 15;
4613@@ -159,9 +171,9 @@ int ZEXPORT inflateReset2(z_stream *strm, int windowBits) {
4614 /* set number of window bits, free window if different */
4615 if (windowBits && (windowBits < 8 || windowBits > 15))
4616 return Z_STREAM_ERROR;
4617- if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
4618+ if (state->window != NULL && state->wbits != (unsigned)windowBits) {
4619 ZFREE(strm, state->window);
4620- state->window = Z_NULL;
4621+ state->window = NULL;
4622 }
4623
4624 /* update state and reset the rest of it */
4625@@ -174,27 +186,29 @@ int ZEXPORT inflateInit2_(z_stream *strm, int windowBits, const char *version, i
4626 int ret;
4627 struct inflate_state *state;
4628
4629- if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || stream_size != (int)(sizeof(z_stream)))
4630+ if (version == NULL || version[0] != ZLIB_VERSION[0] || stream_size != (int)(sizeof(z_stream)))
4631 return Z_VERSION_ERROR;
4632- if (strm == Z_NULL)
4633+ if (strm == NULL)
4634 return Z_STREAM_ERROR;
4635- strm->msg = Z_NULL; /* in case we return an error */
4636- if (strm->zalloc == (alloc_func)0) {
4637+ strm->msg = NULL; /* in case we return an error */
4638+ if (strm->zalloc == NULL) {
4639 strm->zalloc = zcalloc;
4640 strm->opaque = NULL;
4641 }
4642- if (strm->zfree == (free_func)0)
4643+ if (strm->zfree == NULL)
4644 strm->zfree = zcfree;
4645 state = (struct inflate_state *) ZALLOC(strm, 1, sizeof(struct inflate_state));
4646- if (state == Z_NULL)
4647+ if (state == NULL)
4648 return Z_MEM_ERROR;
4649 Tracev((stderr, "inflate: allocated\n"));
4650 strm->state = (struct internal_state *)state;
4651- state->window = Z_NULL;
4652+ state->strm = strm;
4653+ state->window = NULL;
4654+ state->mode = HEAD; /* to pass state test in inflateReset2() */
4655 ret = inflateReset2(strm, windowBits);
4656 if (ret != Z_OK) {
4657 ZFREE(strm, state);
4658- strm->state = Z_NULL;
4659+ strm->state = NULL;
4660 }
4661 return ret;
4662 }
4663@@ -206,7 +220,7 @@ int ZEXPORT inflateInit_(z_stream *strm, const char *version, int stream_size) {
4664 int ZEXPORT inflatePrime(z_stream *strm, int bits, int value) {
4665 struct inflate_state *state;
4666
4667- if (strm == Z_NULL || strm->state == Z_NULL)
4668+ if (inflateStateCheck(strm))
4669 return Z_STREAM_ERROR;
4670 state = (struct inflate_state *)strm->state;
4671 if (bits < 0) {
4672@@ -232,7 +246,7 @@ int ZEXPORT inflatePrime(z_stream *strm, int bits, int value) {
4673 used for threaded applications, since the rewriting of the tables and virgin
4674 may not be thread-safe.
4675 */
4676-local void fixedtables(struct inflate_state *state) {
4677+static void fixedtables(struct inflate_state *state) {
4678 #ifdef BUILDFIXED
4679 static int virgin = 1;
4680 static code *lenfix, *distfix;
4681@@ -350,16 +364,16 @@ void makefixed(void) {
4682 output will fall in the output data, making match copies simpler and faster.
4683 The advantage may be dependent on the size of the processor's data caches.
4684 */
4685-local int updatewindow(z_stream *strm, const unsigned char *end, uint32_t copy) {
4686+static int updatewindow(z_stream *strm, const unsigned char *end, uint32_t copy) {
4687 struct inflate_state *state;
4688 uint32_t dist;
4689
4690 state = (struct inflate_state *)strm->state;
4691
4692 /* if it hasn't been done already, allocate space for the window */
4693- if (state->window == Z_NULL) {
4694+ if (state->window == NULL) {
4695 state->window = (unsigned char *) ZALLOC(strm, 1U << state->wbits, sizeof(unsigned char));
4696- if (state->window == Z_NULL)
4697+ if (state->window == NULL)
4698 return 1;
4699 }
4700
4701@@ -592,8 +606,8 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
4702 static const uint16_t order[19] = /* permutation of code lengths */
4703 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
4704
4705- if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
4706- (strm->next_in == Z_NULL && strm->avail_in != 0))
4707+ if (inflateStateCheck(strm) || strm->next_out == NULL ||
4708+ (strm->next_in == NULL && strm->avail_in != 0))
4709 return Z_STREAM_ERROR;
4710
4711 state = (struct inflate_state *)strm->state;
4712@@ -615,14 +629,14 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
4713 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
4714 if (state->wbits == 0)
4715 state->wbits = 15;
4716- state->check = crc32(0L, Z_NULL, 0);
4717+ state->check = crc32(0L, NULL, 0);
4718 CRC2(state->check, hold);
4719 INITBITS();
4720 state->mode = FLAGS;
4721 break;
4722 }
4723 state->flags = 0; /* expect zlib header */
4724- if (state->head != Z_NULL)
4725+ if (state->head != NULL)
4726 state->head->done = -1;
4727 if (!(state->wrap & 1) || /* check if zlib header allowed */
4728 #else
4729@@ -649,7 +663,7 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
4730 }
4731 state->dmax = 1U << len;
4732 Tracev((stderr, "inflate: zlib header ok\n"));
4733- strm->adler = state->check = adler32(0L, Z_NULL, 0);
4734+ strm->adler = state->check = adler32(0L, NULL, 0);
4735 state->mode = hold & 0x200 ? DICTID : TYPE;
4736 INITBITS();
4737 break;
4738@@ -667,27 +681,27 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
4739 state->mode = BAD;
4740 break;
4741 }
4742- if (state->head != Z_NULL)
4743+ if (state->head != NULL)
4744 state->head->text = (int)((hold >> 8) & 1);
4745- if (state->flags & 0x0200)
4746+ if ((state->flags & 0x0200) && (state->wrap & 4))
4747 CRC2(state->check, hold);
4748 INITBITS();
4749 state->mode = TIME;
4750 case TIME:
4751 NEEDBITS(32);
4752- if (state->head != Z_NULL)
4753+ if (state->head != NULL)
4754 state->head->time = hold;
4755- if (state->flags & 0x0200)
4756+ if ((state->flags & 0x0200) && (state->wrap & 4))
4757 CRC4(state->check, hold);
4758 INITBITS();
4759 state->mode = OS;
4760 case OS:
4761 NEEDBITS(16);
4762- if (state->head != Z_NULL) {
4763+ if (state->head != NULL) {
4764 state->head->xflags = (int)(hold & 0xff);
4765 state->head->os = (int)(hold >> 8);
4766 }
4767- if (state->flags & 0x0200)
4768+ if ((state->flags & 0x0200) && (state->wrap & 4))
4769 CRC2(state->check, hold);
4770 INITBITS();
4771 state->mode = EXLEN;
4772@@ -695,13 +709,13 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
4773 if (state->flags & 0x0400) {
4774 NEEDBITS(16);
4775 state->length = (uint16_t)hold;
4776- if (state->head != Z_NULL)
4777+ if (state->head != NULL)
4778 state->head->extra_len = (uint16_t)hold;
4779- if (state->flags & 0x0200)
4780+ if ((state->flags & 0x0200) && (state->wrap & 4))
4781 CRC2(state->check, hold);
4782 INITBITS();
4783- } else if (state->head != Z_NULL) {
4784- state->head->extra = Z_NULL;
4785+ } else if (state->head != NULL) {
4786+ state->head->extra = NULL;
4787 }
4788 state->mode = EXTRA;
4789 case EXTRA:
4790@@ -710,14 +724,14 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
4791 if (copy > have)
4792 copy = have;
4793 if (copy) {
4794- if (state->head != Z_NULL &&
4795- state->head->extra != Z_NULL) {
4796+ if (state->head != NULL &&
4797+ state->head->extra != NULL) {
4798 len = state->head->extra_len - state->length;
4799 memcpy(state->head->extra + len, next,
4800 len + copy > state->head->extra_max ?
4801 state->head->extra_max - len : copy);
4802 }
4803- if (state->flags & 0x0200)
4804+ if ((state->flags & 0x0200) && (state->wrap & 4))
4805 state->check = crc32(state->check, next, copy);
4806 have -= copy;
4807 next += copy;
4808@@ -734,17 +748,17 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
4809 copy = 0;
4810 do {
4811 len = (unsigned)(next[copy++]);
4812- if (state->head != Z_NULL && state->head->name != Z_NULL && state->length < state->head->name_max)
4813+ if (state->head != NULL && state->head->name != NULL && state->length < state->head->name_max)
4814 state->head->name[state->length++] = len;
4815 } while (len && copy < have);
4816- if (state->flags & 0x0200)
4817+ if ((state->flags & 0x0200) && (state->wrap & 4))
4818 state->check = crc32(state->check, next, copy);
4819 have -= copy;
4820 next += copy;
4821 if (len)
4822 goto inf_leave;
4823- } else if (state->head != Z_NULL) {
4824- state->head->name = Z_NULL;
4825+ } else if (state->head != NULL) {
4826+ state->head->name = NULL;
4827 }
4828 state->length = 0;
4829 state->mode = COMMENT;
4830@@ -754,35 +768,35 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
4831 copy = 0;
4832 do {
4833 len = (unsigned)(next[copy++]);
4834- if (state->head != Z_NULL && state->head->comment != Z_NULL
4835+ if (state->head != NULL && state->head->comment != NULL
4836 && state->length < state->head->comm_max)
4837 state->head->comment[state->length++] = len;
4838 } while (len && copy < have);
4839- if (state->flags & 0x0200)
4840+ if ((state->flags & 0x0200) && (state->wrap & 4))
4841 state->check = crc32(state->check, next, copy);
4842 have -= copy;
4843 next += copy;
4844 if (len)
4845 goto inf_leave;
4846- } else if (state->head != Z_NULL) {
4847- state->head->comment = Z_NULL;
4848+ } else if (state->head != NULL) {
4849+ state->head->comment = NULL;
4850 }
4851 state->mode = HCRC;
4852 case HCRC:
4853 if (state->flags & 0x0200) {
4854 NEEDBITS(16);
4855- if (hold != (state->check & 0xffff)) {
4856+ if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
4857 strm->msg = (char *)"header crc mismatch";
4858 state->mode = BAD;
4859 break;
4860 }
4861 INITBITS();
4862 }
4863- if (state->head != Z_NULL) {
4864+ if (state->head != NULL) {
4865 state->head->hcrc = (int)((state->flags >> 9) & 1);
4866 state->head->done = 1;
4867 }
4868- strm->adler = state->check = crc32(0L, Z_NULL, 0);
4869+ strm->adler = state->check = crc32(0L, NULL, 0);
4870 state->mode = TYPE;
4871 break;
4872 #endif
4873@@ -796,7 +810,7 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
4874 RESTORE();
4875 return Z_NEED_DICT;
4876 }
4877- strm->adler = state->check = adler32(0L, Z_NULL, 0);
4878+ strm->adler = state->check = adler32(0L, NULL, 0);
4879 state->mode = TYPE;
4880 case TYPE:
4881 if (flush == Z_BLOCK || flush == Z_TREES)
4882@@ -1155,10 +1169,10 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
4883 out -= left;
4884 strm->total_out += out;
4885 state->total += out;
4886- if (out)
4887+ if ((state->wrap & 4) && out)
4888 strm->adler = state->check = UPDATE(state->check, put - out, out);
4889 out = left;
4890- if ((
4891+ if ((state->wrap & 4) && (
4892 #ifdef GUNZIP
4893 state->flags ? hold :
4894 #endif
4895@@ -1217,7 +1231,7 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
4896 strm->total_in += in;
4897 strm->total_out += out;
4898 state->total += out;
4899- if (state->wrap && out)
4900+ if ((state->wrap & 4) && out)
4901 strm->adler = state->check = UPDATE(state->check, strm->next_out - out, out);
4902 strm->data_type = state->bits + (state->last ? 64 : 0) +
4903 (state->mode == TYPE ? 128 : 0) + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
4904@@ -1228,13 +1242,13 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
4905
4906 int ZEXPORT inflateEnd(z_stream *strm) {
4907 struct inflate_state *state;
4908- if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
4909+ if (inflateStateCheck(strm))
4910 return Z_STREAM_ERROR;
4911 state = (struct inflate_state *)strm->state;
4912- if (state->window != Z_NULL)
4913+ if (state->window != NULL)
4914 ZFREE(strm, state->window);
4915 ZFREE(strm, strm->state);
4916- strm->state = Z_NULL;
4917+ strm->state = NULL;
4918 Tracev((stderr, "inflate: end\n"));
4919 return Z_OK;
4920 }
4921@@ -1243,16 +1257,16 @@ int ZEXPORT inflateGetDictionary(z_stream *strm, unsigned char *dictionary, unsi
4922 struct inflate_state *state;
4923
4924 /* check state */
4925- if (strm == Z_NULL || strm->state == Z_NULL)
4926+ if (inflateStateCheck(strm))
4927 return Z_STREAM_ERROR;
4928 state = (struct inflate_state *)strm->state;
4929
4930 /* copy dictionary */
4931- if (state->whave && dictionary != Z_NULL) {
4932+ if (state->whave && dictionary != NULL) {
4933 memcpy(dictionary, state->window + state->wnext, state->whave - state->wnext);
4934 memcpy(dictionary + state->whave - state->wnext, state->window, state->wnext);
4935 }
4936- if (dictLength != Z_NULL)
4937+ if (dictLength != NULL)
4938 *dictLength = state->whave;
4939 return Z_OK;
4940 }
4941@@ -1263,7 +1277,7 @@ int ZEXPORT inflateSetDictionary(z_stream *strm, const unsigned char *dictionary
4942 int ret;
4943
4944 /* check state */
4945- if (strm == Z_NULL || strm->state == Z_NULL)
4946+ if (inflateStateCheck(strm))
4947 return Z_STREAM_ERROR;
4948 state = (struct inflate_state *)strm->state;
4949 if (state->wrap != 0 && state->mode != DICT)
4950@@ -1271,7 +1285,7 @@ int ZEXPORT inflateSetDictionary(z_stream *strm, const unsigned char *dictionary
4951
4952 /* check for correct dictionary identifier */
4953 if (state->mode == DICT) {
4954- dictid = adler32(0L, Z_NULL, 0);
4955+ dictid = adler32(0L, NULL, 0);
4956 dictid = adler32(dictid, dictionary, dictLength);
4957 if (dictid != state->check)
4958 return Z_DATA_ERROR;
4959@@ -1293,7 +1307,7 @@ int ZEXPORT inflateGetHeader(z_stream *strm, gz_headerp head) {
4960 struct inflate_state *state;
4961
4962 /* check state */
4963- if (strm == Z_NULL || strm->state == Z_NULL)
4964+ if (inflateStateCheck(strm))
4965 return Z_STREAM_ERROR;
4966 state = (struct inflate_state *)strm->state;
4967 if ((state->wrap & 2) == 0)
4968@@ -1316,7 +1330,7 @@ int ZEXPORT inflateGetHeader(z_stream *strm, gz_headerp head) {
4969 called again with more data and the *have state. *have is initialized to
4970 zero for the first call.
4971 */
4972-local unsigned syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len) {
4973+static uint32_t syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len) {
4974 uint32_t got;
4975 uint32_t next;
4976
4977@@ -1342,7 +1356,7 @@ int ZEXPORT inflateSync(z_stream *strm) {
4978 struct inflate_state *state;
4979
4980 /* check parameters */
4981- if (strm == Z_NULL || strm->state == Z_NULL)
4982+ if (inflateStateCheck(strm))
4983 return Z_STREAM_ERROR;
4984 state = (struct inflate_state *)strm->state;
4985 if (strm->avail_in == 0 && state->bits < 8)
4986@@ -1392,7 +1406,7 @@ int ZEXPORT inflateSync(z_stream *strm) {
4987 int ZEXPORT inflateSyncPoint(z_stream *strm) {
4988 struct inflate_state *state;
4989
4990- if (strm == Z_NULL || strm->state == Z_NULL)
4991+ if (inflateStateCheck(strm))
4992 return Z_STREAM_ERROR;
4993 state = (struct inflate_state *)strm->state;
4994 return state->mode == STORED && state->bits == 0;
4995@@ -1405,20 +1419,19 @@ int ZEXPORT inflateCopy(z_stream *dest, z_stream *source) {
4996 unsigned wsize;
4997
4998 /* check input */
4999- if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
5000- source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
5001+ if (inflateStateCheck(source) || dest == NULL)
5002 return Z_STREAM_ERROR;
5003 state = (struct inflate_state *)source->state;
5004
5005 /* allocate space */
5006 copy = (struct inflate_state *)
5007 ZALLOC(source, 1, sizeof(struct inflate_state));
5008- if (copy == Z_NULL)
5009+ if (copy == NULL)
5010 return Z_MEM_ERROR;
5011- window = Z_NULL;
5012- if (state->window != Z_NULL) {
5013+ window = NULL;
5014+ if (state->window != NULL) {
5015 window = (unsigned char *) ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
5016- if (window == Z_NULL) {
5017+ if (window == NULL) {
5018 ZFREE(source, copy);
5019 return Z_MEM_ERROR;
5020 }
5021@@ -1427,12 +1440,13 @@ int ZEXPORT inflateCopy(z_stream *dest, z_stream *source) {
5022 /* copy state */
5023 memcpy((void *)dest, (void *)source, sizeof(z_stream));
5024 memcpy((void *)copy, (void *)state, sizeof(struct inflate_state));
5025+ copy->strm = dest;
5026 if (state->lencode >= state->codes && state->lencode <= state->codes + ENOUGH - 1) {
5027 copy->lencode = copy->codes + (state->lencode - state->codes);
5028 copy->distcode = copy->codes + (state->distcode - state->codes);
5029 }
5030 copy->next = copy->codes + (state->next - state->codes);
5031- if (window != Z_NULL) {
5032+ if (window != NULL) {
5033 wsize = 1U << state->wbits;
5034 memcpy(window, state->window, wsize);
5035 }
5036@@ -1444,7 +1458,7 @@ int ZEXPORT inflateCopy(z_stream *dest, z_stream *source) {
5037 int ZEXPORT inflateUndermine(z_stream *strm, int subvert) {
5038 struct inflate_state *state;
5039
5040- if (strm == Z_NULL || strm->state == Z_NULL)
5041+ if (inflateStateCheck(strm))
5042 return Z_STREAM_ERROR;
5043 state = (struct inflate_state *)strm->state;
5044 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
5045@@ -1456,12 +1470,34 @@ int ZEXPORT inflateUndermine(z_stream *strm, int subvert) {
5046 #endif
5047 }
5048
5049+int ZEXPORT inflateValidate(z_stream *strm, int check)
5050+{
5051+ struct inflate_state *state;
5052+
5053+ if (inflateStateCheck(strm))
5054+ return Z_STREAM_ERROR;
5055+ state = (struct inflate_state *)strm->state;
5056+ if (check)
5057+ state->wrap |= 4;
5058+ else
5059+ state->wrap &= ~4;
5060+ return Z_OK;
5061+}
5062+
5063 long ZEXPORT inflateMark(z_stream *strm) {
5064 struct inflate_state *state;
5065
5066- if (strm == Z_NULL || strm->state == Z_NULL)
5067- return -1L << 16;
5068+ if (inflateStateCheck(strm))
5069+ return -65536;
5070 state = (struct inflate_state *)strm->state;
5071 return ((long)(state->back) << 16) + (state->mode == COPY ? state->length :
5072 (state->mode == MATCH ? state->was - state->length : 0));
5073 }
5074+
5075+unsigned long ZEXPORT inflateCodesUsed(z_stream *strm)
5076+{
5077+ struct inflate_state *state;
5078+ if (inflateStateCheck(strm)) return (unsigned long)-1;
5079+ state = (struct inflate_state *)strm->state;
5080+ return (unsigned long)(state->next - state->codes);
5081+}
5082diff --git a/inflate.h b/inflate.h
5083index 2bf129d..ee385c6 100644
5084--- a/inflate.h
5085+++ b/inflate.h
5086@@ -21,7 +21,7 @@
5087
5088 /* Possible inflate modes between inflate() calls */
5089 typedef enum {
5090- HEAD, /* i: waiting for magic header */
5091+ HEAD = 16180, /* i: waiting for magic header */
5092 FLAGS, /* i: waiting for method and flags (gzip) */
5093 TIME, /* i: waiting for modification time (gzip) */
5094 OS, /* i: waiting for extra flags and operating system (gzip) */
5095@@ -80,11 +80,14 @@ typedef enum {
5096 CHECK -> LENGTH -> DONE
5097 */
5098
5099-/* state maintained between inflate() calls. Approximately 10K bytes. */
5100+/* State maintained between inflate() calls -- approximately 7K bytes, not
5101+ including the allocated sliding window, which is up to 32K bytes. */
5102 struct inflate_state {
5103+ z_streamp strm; /* pointer back to this zlib stream */
5104 inflate_mode mode; /* current inflate mode */
5105 int last; /* true if processing last block */
5106- int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
5107+ int wrap; /* bit 0 true for zlib, bit 1 true for gzip,
5108+ bit 2 true to validate check value */
5109 int havedict; /* true if dictionary provided */
5110 int flags; /* gzip header method and flags (0 if zlib) */
5111 unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
5112diff --git a/inftrees.c b/inftrees.c
5113index e02272c..fa469d3 100644
5114--- a/inftrees.c
5115+++ b/inftrees.c
5116@@ -47,7 +47,7 @@ int ZLIB_INTERNAL inflate_table(codetype type, uint16_t *lens, unsigned codes,
5117 code *next; /* next available space in table */
5118 const uint16_t *base; /* base value table to use */
5119 const uint16_t *extra; /* extra bits table to use */
5120- int end; /* use base and extra for symbol > end */
5121+ unsigned match; /* use base and extra for symbol >= match */
5122 uint16_t count[MAXBITS+1]; /* number of codes of each length */
5123 uint16_t offs[MAXBITS+1]; /* offsets in table for each length */
5124 static const uint16_t lbase[31] = { /* Length codes 257..285 base */
5125@@ -174,19 +174,17 @@ int ZLIB_INTERNAL inflate_table(codetype type, uint16_t *lens, unsigned codes,
5126 switch (type) {
5127 case CODES:
5128 base = extra = work; /* dummy value--not used */
5129- end = 19;
5130+ match = 20;
5131 break;
5132 case LENS:
5133 base = lbase;
5134- base -= 257;
5135 extra = lext;
5136- extra -= 257;
5137- end = 256;
5138+ match = 257;
5139 break;
5140- default: /* DISTS */
5141+ case DISTS:
5142 base = dbase;
5143 extra = dext;
5144- end = -1;
5145+ match = 0;
5146 }
5147
5148 /* initialize state for loop */
5149@@ -209,12 +207,12 @@ int ZLIB_INTERNAL inflate_table(codetype type, uint16_t *lens, unsigned codes,
5150 for (;;) {
5151 /* create table entry */
5152 here.bits = (unsigned char)(len - drop);
5153- if ((int)(work[sym]) < end) {
5154+ if ((uint16_t)(work[sym] + 1) < match) {
5155 here.op = (unsigned char)0;
5156 here.val = work[sym];
5157- } else if ((int)(work[sym]) > end) {
5158- here.op = (unsigned char)(extra[work[sym]]);
5159- here.val = base[work[sym]];
5160+ } else if (work[sym] >= match) {
5161+ here.op = (unsigned char)(extra[work[sym] - match]);
5162+ here.val = base[work[sym] - match];
5163 } else {
5164 here.op = (unsigned char)(32 + 64); /* end of block */
5165 here.val = 0;
5166diff --git a/match.c b/match.c
5167index b111cb4..cbfcb29 100644
5168--- a/match.c
5169+++ b/match.c
5170@@ -143,7 +143,7 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) {
5171 * is pretty low, so for performance it's best to
5172 * outright stop here for the lower compression levels
5173 */
5174- if (s->level < 6)
5175+ if (s->level < TRIGGER_LEVEL)
5176 break;
5177 }
5178 } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length);
5179@@ -264,7 +264,7 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) {
5180 * is pretty low, so for performance it's best to
5181 * outright stop here for the lower compression levels
5182 */
5183- if (s->level < 6)
5184+ if (s->level < TRIGGER_LEVEL)
5185 break;
5186 }
5187 } while (--chain_length && (cur_match = prev[cur_match & wmask]) > limit);
5188@@ -276,6 +276,7 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) {
5189 #endif
5190
5191 #ifdef std3_longest_match
5192+
5193 /* longest_match() with minor change to improve performance (in terms of
5194 * execution time).
5195 *
5196@@ -424,7 +425,6 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) {
5197 if (xor) {
5198 int match_byte = __builtin_ctzl(xor) / 8;
5199 scan += match_byte;
5200- match += match_byte;
5201 break;
5202 } else {
5203 scan += sizeof(unsigned long);
5204@@ -452,7 +452,7 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) {
5205 * is pretty low, so for performance it's best to
5206 * outright stop here for the lower compression levels
5207 */
5208- if (s->level < 6)
5209+ if (s->level < TRIGGER_LEVEL)
5210 break;
5211 }
5212 } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length != 0);
5213diff --git a/test/.gitignore b/test/.gitignore
5214deleted file mode 100644
5215index 2c3af0a..0000000
5216--- a/test/.gitignore
5217+++ /dev/null
5218@@ -1,2 +0,0 @@
5219-# ignore Makefiles; they're all automatically generated
5220-Makefile
5221diff --git a/test/CVE-2002-0059/test.gz b/test/CVE-2002-0059/test.gz
5222deleted file mode 100644
5223index c5c3e18..0000000
5224Binary files a/test/CVE-2002-0059/test.gz and /dev/null differ
5225diff --git a/test/CVE-2003-0107.c b/test/CVE-2003-0107.c
5226deleted file mode 100644
5227index 306421e..0000000
5228--- a/test/CVE-2003-0107.c
5229+++ /dev/null
5230@@ -1,20 +0,0 @@
5231-// http://www.securityfocus.com/archive/1/312869 --- originally by Richard Kettlewell
5232-#include <stdlib.h>
5233-#include <zlib.h>
5234-#include <errno.h>
5235-#include <stdio.h>
5236-
5237-int main(void) {
5238-gzFile f;
5239-int ret;
5240-
5241-if(!(f = gzopen("/dev/null", "w"))) {
5242-perror("/dev/null");
5243-exit(1);
5244-}
5245-ret = gzprintf(f, "%10240s", "");
5246-printf("gzprintf -> %d\n", ret);
5247-ret = gzclose(f);
5248-printf("gzclose -> %d [%d]\n", ret, errno);
5249-exit(0);
5250-}
5251diff --git a/test/CVE-2004-0797/test.gz b/test/CVE-2004-0797/test.gz
5252deleted file mode 100644
5253index 62dcf34..0000000
5254Binary files a/test/CVE-2004-0797/test.gz and /dev/null differ
5255diff --git a/test/CVE-2005-1849/test.gz b/test/CVE-2005-1849/test.gz
5256deleted file mode 100644
5257index b28f278..0000000
5258Binary files a/test/CVE-2005-1849/test.gz and /dev/null differ
5259diff --git a/test/CVE-2005-2096/test.gz b/test/CVE-2005-2096/test.gz
5260deleted file mode 100644
5261index 11590ae..0000000
5262Binary files a/test/CVE-2005-2096/test.gz and /dev/null differ
5263diff --git a/test/INDEX b/test/INDEX
5264deleted file mode 100644
5265index f167df8..0000000
5266--- a/test/INDEX
5267+++ /dev/null
5268@@ -1,10 +0,0 @@
5269-Makefile.in: template for Unix Makefile
5270-
5271-CVE-2003-0107.c:
5272-CVE-2002-0059 :
5273-CVE-2004-0797 :
5274-CVE-2005-1849 :
5275-CVE-2005-2096 : test cases for the relevant CVEs
5276-
5277-testCVEinputs.sh: script to run tests for CVEs where input data is supplied
5278-
5279diff --git a/test/Makefile.in b/test/Makefile.in
5280deleted file mode 100644
5281index 1fd8c00..0000000
5282--- a/test/Makefile.in
5283+++ /dev/null
5284@@ -1,77 +0,0 @@
5285-# Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
5286-# Copyright 2015, Daniel Axtens, IBM Corporation
5287-# zlib license, see zlib.h
5288-
5289-CC=
5290-CFLAGS=
5291-EXE=
5292-SRCDIR=
5293-SRCTOP=
5294-INCLUDES=
5295-TEST_LDFLAGS=-L.. ../libz.a
5296-
5297-COMPATTESTS =
5298-
5299-all: oldtests cvetests $(COMPATTESTS)
5300-
5301-oldtests: #set by ../configure
5302-
5303-teststatic:
5304- @TMPST=tmpst_$$; \
5305- if echo hello world | ../minigzip | ../minigzip -d && ../example $$TMPST ; then \
5306- echo ' *** zlib test OK ***'; \
5307- else \
5308- echo ' *** zlib test FAILED ***'; exit 1; \
5309- fi; \
5310- rm -f $$TMPST
5311-
5312-testshared:
5313- @LD_LIBRARY_PATH=`pwd`/..:$(LD_LIBRARY_PATH) ; export LD_LIBRARY_PATH; \
5314- LD_LIBRARYN32_PATH=`pwd`/..:$(LD_LIBRARYN32_PATH) ; export LD_LIBRARYN32_PATH; \
5315- DYLD_LIBRARY_PATH=`pwd`/..:$(DYLD_LIBRARY_PATH) ; export DYLD_LIBRARY_PATH; \
5316- SHLIB_PATH=`pwd`/..:$(SHLIB_PATH) ; export SHLIB_PATH; \
5317- TMPSH=tmpsh_$$; \
5318- if echo hello world | ../minigzipsh | ../minigzipsh -d && ../examplesh $$TMPSH; then \
5319- echo ' *** zlib shared test OK ***'; \
5320- else \
5321- echo ' *** zlib shared test FAILED ***'; exit 1; \
5322- fi; \
5323- rm -f $$TMPSH
5324-
5325-test64:
5326- @TMP64=tmp64_$$; \
5327- if echo hello world | ../minigzip64 | ../minigzip64 -d && ../example64 $$TMP64; then \
5328- echo ' *** zlib 64-bit test OK ***'; \
5329- else \
5330- echo ' *** zlib 64-bit test FAILED ***'; exit 1; \
5331- fi; \
5332- rm -f $$TMP64
5333-
5334-cvetests: testCVEinputs
5335-
5336-# Tests requiring zlib-ng to be built with --zlib-compat
5337-compattests: testCVE-2003-0107
5338-
5339-testCVEinputs:
5340- @$(SRCDIR)/testCVEinputs.sh
5341-
5342-testCVE-2003-0107: CVE-2003-0107$(EXE)
5343- @if ./CVE-2003-0107$(EXE); then \
5344- echo ' *** zlib not vulnerable to CVE-2003-0107 ***'; \
5345- else \
5346- echo ' *** zlib VULNERABLE to CVE-2003-0107 ***'; exit 1; \
5347- fi
5348-
5349-CVE-2003-0107.o: $(SRCDIR)/CVE-2003-0107.c
5350- $(CC) $(CFLAGS) -I.. -I$(SRCTOP) -c -o $@ $(SRCDIR)/CVE-2003-0107.c
5351-
5352-CVE-2003-0107$(EXE): CVE-2003-0107.o
5353- $(CC) $(CFLAGS) -o $@ CVE-2003-0107.o $(TEST_LDFLAGS)
5354-
5355-
5356-clean:
5357- rm -f *.o *.gcda *.gcno *.gcov
5358- rm -f CVE-2003-0107$(EXE)
5359-
5360-distclean:
5361- rm -f Makefile
5362diff --git a/test/example.c b/test/example.c
5363index 43d1a16..6d9bad3 100644
5364--- a/test/example.c
5365+++ b/test/example.c
5366@@ -40,8 +40,8 @@ void test_dict_inflate (unsigned char *compr, size_t comprLen, unsigned char *u
5367 int main (int argc, char *argv[]);
5368
5369
5370-static alloc_func zalloc = (alloc_func)0;
5371-static free_func zfree = (free_func)0;
5372+static alloc_func zalloc = NULL;
5373+static free_func zfree = NULL;
5374
5375 void test_compress (unsigned char *compr, size_t comprLen,
5376 unsigned char *uncompr, size_t uncomprLen);
5377@@ -512,7 +512,7 @@ int main(int argc, char *argv[])
5378 /* compr and uncompr are cleared to avoid reading uninitialized
5379 * data and to ensure that uncompr compresses well.
5380 */
5381- if (compr == Z_NULL || uncompr == Z_NULL) {
5382+ if (compr == NULL || uncompr == NULL) {
5383 printf("out of memory\n");
5384 exit(1);
5385 }
5386diff --git a/test/infcover.c b/test/infcover.c
5387index 5555a15..ee84636 100644
5388--- a/test/infcover.c
5389+++ b/test/infcover.c
5390@@ -45,7 +45,7 @@
5391 allocated, then "msg" and information about the
5392 problem is printed to stderr. If everything is
5393 normal, nothing is printed. mem_done resets the
5394- strm members to Z_NULL to use the default memory
5395+ strm members to NULL to use the default memory
5396 allocation routines on the next zlib initialization
5397 using strm.
5398 */
5399@@ -226,9 +226,9 @@ static void mem_done(z_stream *strm, char *prefix)
5400
5401 /* free the zone and delete from the stream */
5402 free(zone);
5403- strm->opaque = Z_NULL;
5404- strm->zalloc = Z_NULL;
5405- strm->zfree = Z_NULL;
5406+ strm->opaque = NULL;
5407+ strm->zalloc = NULL;
5408+ strm->zfree = NULL;
5409 }
5410
5411 /* -- inflate test routines -- */
5412@@ -289,7 +289,7 @@ static void inf(char *hex, char *what, unsigned step, int win, unsigned len, int
5413
5414 mem_setup(&strm);
5415 strm.avail_in = 0;
5416- strm.next_in = Z_NULL;
5417+ strm.next_in = NULL;
5418 ret = inflateInit2(&strm, win);
5419 if (ret != Z_OK) {
5420 mem_done(&strm, what);
5421@@ -351,12 +351,12 @@ static void cover_support(void)
5422
5423 mem_setup(&strm);
5424 strm.avail_in = 0;
5425- strm.next_in = Z_NULL;
5426+ strm.next_in = NULL;
5427 ret = inflateInit(&strm); assert(ret == Z_OK);
5428 mem_used(&strm, "inflate init");
5429 ret = inflatePrime(&strm, 5, 31); assert(ret == Z_OK);
5430 ret = inflatePrime(&strm, -1, 0); assert(ret == Z_OK);
5431- ret = inflateSetDictionary(&strm, Z_NULL, 0);
5432+ ret = inflateSetDictionary(&strm, NULL, 0);
5433 assert(ret == Z_STREAM_ERROR);
5434 ret = inflateEnd(&strm); assert(ret == Z_OK);
5435 mem_done(&strm, "prime");
5436@@ -369,13 +369,13 @@ static void cover_support(void)
5437
5438 mem_setup(&strm);
5439 strm.avail_in = 0;
5440- strm.next_in = Z_NULL;
5441+ strm.next_in = NULL;
5442 ret = inflateInit_(&strm, ZLIB_VERSION + 1, (int)sizeof(z_stream));
5443 assert(ret == Z_VERSION_ERROR);
5444 mem_done(&strm, "wrong version");
5445
5446 strm.avail_in = 0;
5447- strm.next_in = Z_NULL;
5448+ strm.next_in = NULL;
5449 ret = inflateInit(&strm); assert(ret == Z_OK);
5450 ret = inflateEnd(&strm); assert(ret == Z_OK);
5451 fputs("inflate built-in memory routines\n", stderr);
5452@@ -388,9 +388,9 @@ static void cover_wrap(void)
5453 z_stream strm, copy;
5454 unsigned char dict[257];
5455
5456- ret = inflate(Z_NULL, 0); assert(ret == Z_STREAM_ERROR);
5457- ret = inflateEnd(Z_NULL); assert(ret == Z_STREAM_ERROR);
5458- ret = inflateCopy(Z_NULL, Z_NULL); assert(ret == Z_STREAM_ERROR);
5459+ ret = inflate(NULL, 0); assert(ret == Z_STREAM_ERROR);
5460+ ret = inflateEnd(NULL); assert(ret == Z_STREAM_ERROR);
5461+ ret = inflateCopy(NULL, NULL); assert(ret == Z_STREAM_ERROR);
5462 fputs("inflate bad parameters\n", stderr);
5463
5464 inf("1f 8b 0 0", "bad gzip method", 0, 31, 0, Z_DATA_ERROR);
5465@@ -409,7 +409,7 @@ static void cover_wrap(void)
5466
5467 mem_setup(&strm);
5468 strm.avail_in = 0;
5469- strm.next_in = Z_NULL;
5470+ strm.next_in = NULL;
5471 ret = inflateInit2(&strm, -8);
5472 strm.avail_in = 2;
5473 strm.next_in = (void *)"\x63";
5474@@ -447,12 +447,12 @@ static unsigned pull(void *desc, const unsigned char **buf)
5475 static unsigned char dat[] = {0x63, 0, 2, 0};
5476 struct inflate_state *state;
5477
5478- if (desc == Z_NULL) {
5479+ if (desc == NULL) {
5480 next = 0;
5481 return 0; /* no input (already provided at next_in) */
5482 }
5483 state = (void *)((z_stream *)desc)->state;
5484- if (state != Z_NULL)
5485+ if (state != NULL)
5486 state->mode = SYNC; /* force an otherwise impossible situation */
5487 return next < sizeof(dat) ? (*buf = dat + next++, 1) : 0;
5488 }
5489@@ -460,7 +460,7 @@ static unsigned pull(void *desc, const unsigned char **buf)
5490 static int push(void *desc, unsigned char *buf, unsigned len)
5491 {
5492 buf += len;
5493- return desc != Z_NULL; /* force error if desc not null */
5494+ return desc != NULL; /* force error if desc not null */
5495 }
5496
5497 /* cover inflateBack() up to common deflate data cases and after those */
5498@@ -470,27 +470,27 @@ static void cover_back(void)
5499 z_stream strm;
5500 unsigned char win[32768];
5501
5502- ret = inflateBackInit_(Z_NULL, 0, win, 0, 0);
5503+ ret = inflateBackInit_(NULL, 0, win, 0, 0);
5504 assert(ret == Z_VERSION_ERROR);
5505- ret = inflateBackInit(Z_NULL, 0, win); assert(ret == Z_STREAM_ERROR);
5506- ret = inflateBack(Z_NULL, Z_NULL, Z_NULL, Z_NULL, Z_NULL);
5507+ ret = inflateBackInit(NULL, 0, win); assert(ret == Z_STREAM_ERROR);
5508+ ret = inflateBack(NULL, NULL, NULL, NULL, NULL);
5509 assert(ret == Z_STREAM_ERROR);
5510- ret = inflateBackEnd(Z_NULL); assert(ret == Z_STREAM_ERROR);
5511+ ret = inflateBackEnd(NULL); assert(ret == Z_STREAM_ERROR);
5512 fputs("inflateBack bad parameters\n", stderr);
5513
5514 mem_setup(&strm);
5515 ret = inflateBackInit(&strm, 15, win); assert(ret == Z_OK);
5516 strm.avail_in = 2;
5517 strm.next_in = (void *)"\x03";
5518- ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
5519+ ret = inflateBack(&strm, pull, NULL, push, NULL);
5520 assert(ret == Z_STREAM_END);
5521 /* force output error */
5522 strm.avail_in = 3;
5523 strm.next_in = (void *)"\x63\x00";
5524- ret = inflateBack(&strm, pull, Z_NULL, push, &strm);
5525+ ret = inflateBack(&strm, pull, NULL, push, &strm);
5526 assert(ret == Z_BUF_ERROR);
5527 /* force mode error by mucking with state */
5528- ret = inflateBack(&strm, pull, &strm, push, Z_NULL);
5529+ ret = inflateBack(&strm, pull, &strm, push, NULL);
5530 assert(ret == Z_STREAM_ERROR);
5531 ret = inflateBackEnd(&strm); assert(ret == Z_OK);
5532 mem_done(&strm, "inflateBack bad state");
5533@@ -527,7 +527,7 @@ static int try(char *hex, char *id, int err)
5534 strcat(prefix, "-late");
5535 mem_setup(&strm);
5536 strm.avail_in = 0;
5537- strm.next_in = Z_NULL;
5538+ strm.next_in = NULL;
5539 ret = inflateInit2(&strm, err < 0 ? 47 : -15);
5540 assert(ret == Z_OK);
5541 strm.avail_in = len;
5542@@ -556,7 +556,7 @@ static int try(char *hex, char *id, int err)
5543 assert(ret == Z_OK);
5544 strm.avail_in = len;
5545 strm.next_in = in;
5546- ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
5547+ ret = inflateBack(&strm, pull, NULL, push, NULL);
5548 assert(ret != Z_STREAM_ERROR);
5549 if (err) {
5550 assert(ret == Z_DATA_ERROR);
5551diff --git a/test/minigzip.c b/test/minigzip.c
5552index 9c71fd1..8f371ec 100644
5553--- a/test/minigzip.c
5554+++ b/test/minigzip.c
5555@@ -111,7 +111,7 @@ gzFile gz_open(const char *path, int fd, const char *mode)
5556 gz->write = strchr(mode, 'w') != NULL;
5557 gz->strm.zalloc = myalloc;
5558 gz->strm.zfree = myfree;
5559- gz->strm.opaque = Z_NULL;
5560+ gz->strm.opaque = NULL;
5561 gz->buf = malloc(gz->write ? BUFLENW : BUFLEN);
5562
5563 if (gz->buf == NULL) {
5564@@ -129,8 +129,8 @@ gzFile gz_open(const char *path, int fd, const char *mode)
5565 if (gz->write)
5566 ret = deflateInit2(&(gz->strm), level, 8, 15 + 16, 8, 0);
5567 else {
5568- gz->strm.next_in = 0;
5569- gz->strm.avail_in = Z_NULL;
5570+ gz->strm.next_in = NULL;
5571+ gz->strm.avail_in = 0;
5572 ret = inflateInit2(&(gz->strm), 15 + 16);
5573 }
5574 if (ret != Z_OK) {
5575@@ -213,7 +213,7 @@ int gzclose(gzFile gz)
5576 return Z_STREAM_ERROR;
5577 strm = &(gz->strm);
5578 if (gz->write) {
5579- strm->next_in = Z_NULL;
5580+ strm->next_in = NULL;
5581 strm->avail_in = 0;
5582 do {
5583 strm->next_out = gz->buf;
5584@@ -239,7 +239,7 @@ const char *gzerror(gzFile gz, int *err)
5585 return gz->msg;
5586 }
5587
5588-#endif
5589+#endif
5590
5591 char *prog;
5592
5593diff --git a/test/testCVEinputs.sh b/test/testCVEinputs.sh
5594deleted file mode 100755
5595index 046856e..0000000
5596--- a/test/testCVEinputs.sh
5597+++ /dev/null
5598@@ -1,22 +0,0 @@
5599-#!/bin/bash
5600-TESTDIR="$(dirname "$0")"
5601-
5602-CVEs="CVE-2002-0059 CVE-2004-0797 CVE-2005-1849 CVE-2005-2096"
5603-
5604-for CVE in $CVEs; do
5605- fail=0
5606- for testcase in ${TESTDIR}/${CVE}/*.gz; do
5607- ../minigzip -d < "$testcase"
5608- # we expect that a 1 error code is OK
5609- # for a vulnerable failure we'd expect 134 or similar
5610- if [ $? -ne 1 ]; then
5611- fail=1
5612- fi
5613- done
5614- if [ $fail -eq 0 ]; then
5615- echo " --- zlib not vulnerable to $CVE ---";
5616- else
5617- echo " --- zlib VULNERABLE to $CVE ---"; exit 1;
5618- fi
5619-done
5620-
5621diff --git a/tools/list_intel_intrinsics.sh b/tools/list_intel_intrinsics.sh
5622new file mode 100755
5623index 0000000..dd6b4ac
5624--- /dev/null
5625+++ b/tools/list_intel_intrinsics.sh
5626@@ -0,0 +1,3 @@
5627+#!/bin/sh
5628+
5629+fgrep _mm_ "$@" | sed -e 's/.*\(_mm_[^\(]*\)(.*/\1/g' | sort -u
5630diff --git a/treebuild.xml b/treebuild.xml
5631index 38d29d7..34a4f77 100644
5632--- a/treebuild.xml
5633+++ b/treebuild.xml
5634@@ -101,7 +101,7 @@
5635 <!--
5636 CFLAGS=-O
5637 #CFLAGS=-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7
5638-#CFLAGS=-g -DDEBUG
5639+#CFLAGS=-g -DZLIB_DEBUG
5640 #CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \
5641 # -Wstrict-prototypes -Wmissing-prototypes
5642
5643diff --git a/trees.c b/trees.c
5644index ee756fc..d600631 100644
5645--- a/trees.c
5646+++ b/trees.c
5647@@ -36,7 +36,7 @@
5648
5649 #include "deflate.h"
5650
5651-#ifdef DEBUG
5652+#ifdef ZLIB_DEBUG
5653 # include <ctype.h>
5654 #endif
5655
5656@@ -56,16 +56,16 @@
5657 #define REPZ_11_138 18
5658 /* repeat a zero length 11-138 times (7 bits of repeat count) */
5659
5660-local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
5661+static const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
5662 = {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};
5663
5664-local const int extra_dbits[D_CODES] /* extra bits for each distance code */
5665+static const int extra_dbits[D_CODES] /* extra bits for each distance code */
5666 = {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};
5667
5668-local const int extra_blbits[BL_CODES] /* extra bits for each bit length code */
5669+static const int extra_blbits[BL_CODES] /* extra bits for each bit length code */
5670 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
5671
5672-local const unsigned char bl_order[BL_CODES]
5673+static const unsigned char bl_order[BL_CODES]
5674 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
5675 /* The lengths of the bit length codes are sent in order of decreasing
5676 * probability, to avoid transmitting the lengths for unused bit length codes.
5677@@ -87,24 +87,24 @@ ZLIB_INTERNAL ct_data static_ltree[L_CODES+2];
5678 * below).
5679 */
5680
5681-local ct_data static_dtree[D_CODES];
5682+static ct_data static_dtree[D_CODES];
5683 /* The static distance tree. (Actually a trivial tree since all codes use
5684 * 5 bits.)
5685 */
5686
5687-uch _dist_code[DIST_CODE_LEN];
5688+unsigned char _dist_code[DIST_CODE_LEN];
5689 /* Distance codes. The first 256 values correspond to the distances
5690 * 3 .. 258, the last 256 values correspond to the top 8 bits of
5691 * the 15 bit distances.
5692 */
5693
5694-uch _length_code[MAX_MATCH-MIN_MATCH+1];
5695+unsigned char _length_code[MAX_MATCH-MIN_MATCH+1];
5696 /* length code for each normalized match length (0 == MIN_MATCH) */
5697
5698-local int base_length[LENGTH_CODES];
5699+static int base_length[LENGTH_CODES];
5700 /* First normalized length for each code (0 = MIN_MATCH) */
5701
5702-local int base_dist[D_CODES];
5703+static int base_dist[D_CODES];
5704 /* First normalized distance for each code (0 = distance of 1) */
5705
5706 #else
5707@@ -119,43 +119,42 @@ struct static_tree_desc_s {
5708 unsigned int max_length; /* max bit length for the codes */
5709 };
5710
5711-local const static_tree_desc static_l_desc =
5712+static const static_tree_desc static_l_desc =
5713 {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
5714
5715-local const static_tree_desc static_d_desc =
5716+static const static_tree_desc static_d_desc =
5717 {static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
5718
5719-local const static_tree_desc static_bl_desc =
5720+static const static_tree_desc static_bl_desc =
5721 {(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
5722
5723 /* ===========================================================================
5724 * Local (static) routines in this file.
5725 */
5726
5727-local void tr_static_init (void);
5728-local void init_block (deflate_state *s);
5729-local void pqdownheap (deflate_state *s, ct_data *tree, int k);
5730-local void gen_bitlen (deflate_state *s, tree_desc *desc);
5731-local void gen_codes (ct_data *tree, int max_code, uint16_t *bl_count);
5732-local void build_tree (deflate_state *s, tree_desc *desc);
5733-local void scan_tree (deflate_state *s, ct_data *tree, int max_code);
5734-local void send_tree (deflate_state *s, ct_data *tree, int max_code);
5735-local int build_bl_tree (deflate_state *s);
5736-local void send_all_trees (deflate_state *s, int lcodes, int dcodes, int blcodes);
5737-local void compress_block (deflate_state *s, const ct_data *ltree, const ct_data *dtree);
5738-local int detect_data_type (deflate_state *s);
5739-local unsigned bi_reverse (unsigned value, int length);
5740-local void bi_flush (deflate_state *s);
5741-local void copy_block (deflate_state *s, char *buf, unsigned len, int header);
5742+static void tr_static_init (void);
5743+static void init_block (deflate_state *s);
5744+static void pqdownheap (deflate_state *s, ct_data *tree, int k);
5745+static void gen_bitlen (deflate_state *s, tree_desc *desc);
5746+static void gen_codes (ct_data *tree, int max_code, uint16_t *bl_count);
5747+static void build_tree (deflate_state *s, tree_desc *desc);
5748+static void scan_tree (deflate_state *s, ct_data *tree, int max_code);
5749+static void send_tree (deflate_state *s, ct_data *tree, int max_code);
5750+static int build_bl_tree (deflate_state *s);
5751+static void send_all_trees (deflate_state *s, int lcodes, int dcodes, int blcodes);
5752+static void compress_block (deflate_state *s, const ct_data *ltree, const ct_data *dtree);
5753+static int detect_data_type (deflate_state *s);
5754+static unsigned bi_reverse (unsigned value, int length);
5755+static void bi_flush (deflate_state *s);
5756
5757 #ifdef GEN_TREES_H
5758-local void gen_trees_header(void);
5759+static void gen_trees_header (void);
5760 #endif
5761
5762 /* ===========================================================================
5763 * Initialize the various 'constant' tables.
5764 */
5765-local void tr_static_init(void) {
5766+static void tr_static_init(void) {
5767 #if defined(GEN_TREES_H)
5768 static int static_init_done = 0;
5769 int n; /* iterates over tree elements */
5770@@ -242,7 +241,7 @@ local void tr_static_init(void) {
5771 * Genererate the file trees.h describing the static trees.
5772 */
5773 #ifdef GEN_TREES_H
5774-# ifndef DEBUG
5775+# ifndef ZLIB_DEBUG
5776 # include <stdio.h>
5777 # endif
5778
5779@@ -255,6 +254,9 @@ void gen_trees_header() {
5780 int i;
5781
5782 Assert(header != NULL, "Can't open trees.h");
5783+ fprintf(header, "#ifndef TREES_H_\n");
5784+ fprintf(header, "#define TREES_H_\n\n");
5785+
5786 fprintf(header, "/* header created automatically with -DGEN_TREES_H */\n\n");
5787
5788 fprintf(header, "ZLIB_INTERNAL const ct_data static_ltree[L_CODES+2] = {\n");
5789@@ -262,7 +264,7 @@ void gen_trees_header() {
5790 fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
5791 }
5792
5793- fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
5794+ fprintf(header, "static const ct_data static_dtree[D_CODES] = {\n");
5795 for (i = 0; i < D_CODES; i++) {
5796 fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
5797 }
5798@@ -277,16 +279,17 @@ void gen_trees_header() {
5799 fprintf(header, "%2u%s", _length_code[i], SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
5800 }
5801
5802- fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
5803+ fprintf(header, "static const int base_length[LENGTH_CODES] = {\n");
5804 for (i = 0; i < LENGTH_CODES; i++) {
5805 fprintf(header, "%d%s", base_length[i], SEPARATOR(i, LENGTH_CODES-1, 20));
5806 }
5807
5808- fprintf(header, "local const int base_dist[D_CODES] = {\n");
5809+ fprintf(header, "static const int base_dist[D_CODES] = {\n");
5810 for (i = 0; i < D_CODES; i++) {
5811 fprintf(header, "%5d%s", base_dist[i], SEPARATOR(i, D_CODES-1, 10));
5812 }
5813
5814+ fprintf(header, "#endif /* TREES_H_ */\n");
5815 fclose(header);
5816 }
5817 #endif /* GEN_TREES_H */
5818@@ -308,7 +311,7 @@ void ZLIB_INTERNAL _tr_init(deflate_state *s) {
5819
5820 s->bi_buf = 0;
5821 s->bi_valid = 0;
5822-#ifdef DEBUG
5823+#ifdef ZLIB_DEBUG
5824 s->compressed_len = 0L;
5825 s->bits_sent = 0L;
5826 #endif
5827@@ -320,7 +323,7 @@ void ZLIB_INTERNAL _tr_init(deflate_state *s) {
5828 /* ===========================================================================
5829 * Initialize a new block.
5830 */
5831-local void init_block(deflate_state *s) {
5832+static void init_block(deflate_state *s) {
5833 int n; /* iterates over tree elements */
5834
5835 /* Initialize the trees. */
5836@@ -365,7 +368,7 @@ local void init_block(deflate_state *s) {
5837 * when the heap property is re-established (each father smaller than its
5838 * two sons).
5839 */
5840-local void pqdownheap(deflate_state *s, ct_data *tree, int k) {
5841+static void pqdownheap(deflate_state *s, ct_data *tree, int k) {
5842 /* tree: the tree to restore */
5843 /* k: node to move down */
5844 int v = s->heap[k];
5845@@ -399,7 +402,7 @@ local void pqdownheap(deflate_state *s, ct_data *tree, int k) {
5846 * The length opt_len is updated; static_len is also updated if stree is
5847 * not null.
5848 */
5849-local void gen_bitlen(deflate_state *s, tree_desc *desc) {
5850+static void gen_bitlen(deflate_state *s, tree_desc *desc) {
5851 /* desc: the tree descriptor */
5852 ct_data *tree = desc->dyn_tree;
5853 int max_code = desc->max_code;
5854@@ -445,7 +448,7 @@ local void gen_bitlen(deflate_state *s, tree_desc *desc) {
5855 if (overflow == 0)
5856 return;
5857
5858- Trace((stderr, "\nbit length overflow\n"));
5859+ Tracev((stderr, "\nbit length overflow\n"));
5860 /* This happens for example on obj2 and pic of the Calgary corpus */
5861
5862 /* Find the first bit length which could increase: */
5863@@ -474,7 +477,7 @@ local void gen_bitlen(deflate_state *s, tree_desc *desc) {
5864 if (m > max_code)
5865 continue;
5866 if (tree[m].Len != bits) {
5867- Trace((stderr, "code %d bits %d->%u\n", m, tree[m].Len, bits));
5868+ Tracev((stderr, "code %d bits %d->%u\n", m, tree[m].Len, bits));
5869 s->opt_len += (long)((bits - tree[m].Len) * tree[m].Freq);
5870 tree[m].Len = (uint16_t)bits;
5871 }
5872@@ -491,7 +494,7 @@ local void gen_bitlen(deflate_state *s, tree_desc *desc) {
5873 * OUT assertion: the field code is set for all tree elements of non
5874 * zero code length.
5875 */
5876-local void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) {
5877+static void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) {
5878 /* tree: the tree to decorate */
5879 /* max_code: largest code with non zero frequency */
5880 /* bl_count: number of codes at each bit length */
5881@@ -532,7 +535,7 @@ local void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) {
5882 * and corresponding code. The length opt_len is updated; static_len is
5883 * also updated if stree is not null. The field max_code is set.
5884 */
5885-local void build_tree(deflate_state *s, tree_desc *desc) {
5886+static void build_tree(deflate_state *s, tree_desc *desc) {
5887 /* desc: the tree descriptor */
5888 ct_data *tree = desc->dyn_tree;
5889 const ct_data *stree = desc->stat_desc->static_tree;
5890@@ -620,7 +623,7 @@ local void build_tree(deflate_state *s, tree_desc *desc) {
5891 * Scan a literal or distance tree to determine the frequencies of the codes
5892 * in the bit length tree.
5893 */
5894-local void scan_tree(deflate_state *s, ct_data *tree, int max_code) {
5895+static void scan_tree(deflate_state *s, ct_data *tree, int max_code) {
5896 /* tree: the tree to be scanned */
5897 /* max_code: and its largest code of non zero frequency */
5898 int n; /* iterates over all tree elements */
5899@@ -668,7 +671,7 @@ local void scan_tree(deflate_state *s, ct_data *tree, int max_code) {
5900 * Send a literal or distance tree in compressed form, using the codes in
5901 * bl_tree.
5902 */
5903-local void send_tree(deflate_state *s, ct_data *tree, int max_code) {
5904+static void send_tree(deflate_state *s, ct_data *tree, int max_code) {
5905 /* tree: the tree to be scanned */
5906 /* max_code and its largest code of non zero frequency */
5907 int n; /* iterates over all tree elements */
5908@@ -726,7 +729,7 @@ local void send_tree(deflate_state *s, ct_data *tree, int max_code) {
5909 * Construct the Huffman tree for the bit lengths and return the index in
5910 * bl_order of the last bit length code to send.
5911 */
5912-local int build_bl_tree(deflate_state *s) {
5913+static int build_bl_tree(deflate_state *s) {
5914 int max_blindex; /* index of last bit length code of non zero freq */
5915
5916 /* Determine the bit length frequencies for literal and distance trees */
5917@@ -759,7 +762,7 @@ local int build_bl_tree(deflate_state *s) {
5918 * lengths of the bit length codes, the literal tree and the distance tree.
5919 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
5920 */
5921-local void send_all_trees(deflate_state *s, int lcodes, int dcodes, int blcodes) {
5922+static void send_all_trees(deflate_state *s, int lcodes, int dcodes, int blcodes) {
5923 int rank; /* index in bl_order */
5924
5925 Assert(lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
5926@@ -789,11 +792,17 @@ void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, char *buf, unsigned long s
5927 /* stored_len: length of input block */
5928 /* last: one if this is the last block for a file */
5929 send_bits(s, (STORED_BLOCK << 1)+last, 3); /* send block type */
5930-#ifdef DEBUG
5931+ bi_windup(s); /* align on byte boundary */
5932+ put_short(s, (uint16_t)stored_len);
5933+ put_short(s, (uint16_t)~stored_len);
5934+ memcpy(s->pending_buf + s->pending, buf, stored_len);
5935+ s->pending += stored_len;
5936+#ifdef ZLIB_DEBUG
5937 s->compressed_len = (s->compressed_len + 3 + 7) & (unsigned long)~7L;
5938 s->compressed_len += (stored_len + 4) << 3;
5939+ s->bits_sent += 2*16;
5940+ s->bits_sent += stored_len<<3;
5941 #endif
5942- copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
5943 }
5944
5945 /* ===========================================================================
5946@@ -810,7 +819,7 @@ void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s) {
5947 void ZLIB_INTERNAL _tr_align(deflate_state *s) {
5948 send_bits(s, STATIC_TREES << 1, 3);
5949 send_code(s, END_BLOCK, static_ltree);
5950-#ifdef DEBUG
5951+#ifdef ZLIB_DEBUG
5952 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
5953 #endif
5954 bi_flush(s);
5955@@ -884,14 +893,14 @@ void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, char *buf, unsigned long st
5956 #endif
5957 send_bits(s, (STATIC_TREES << 1)+last, 3);
5958 compress_block(s, (const ct_data *)static_ltree, (const ct_data *)static_dtree);
5959-#ifdef DEBUG
5960+#ifdef ZLIB_DEBUG
5961 s->compressed_len += 3 + s->static_len;
5962 #endif
5963 } else {
5964 send_bits(s, (DYN_TREES << 1)+last, 3);
5965 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, max_blindex+1);
5966 compress_block(s, (const ct_data *)s->dyn_ltree, (const ct_data *)s->dyn_dtree);
5967-#ifdef DEBUG
5968+#ifdef ZLIB_DEBUG
5969 s->compressed_len += 3 + s->opt_len;
5970 #endif
5971 }
5972@@ -903,7 +912,7 @@ void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, char *buf, unsigned long st
5973
5974 if (last) {
5975 bi_windup(s);
5976-#ifdef DEBUG
5977+#ifdef ZLIB_DEBUG
5978 s->compressed_len += 7; /* align on byte boundary */
5979 #endif
5980 }
5981@@ -961,13 +970,13 @@ int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc) {
5982 /* ===========================================================================
5983 * Send the block data compressed using the given Huffman trees
5984 */
5985-local void compress_block(deflate_state *s, const ct_data *ltree, const ct_data *dtree) {
5986+static void compress_block(deflate_state *s, const ct_data *ltree, const ct_data *dtree) {
5987 /* ltree: literal tree */
5988 /* dtree: distance tree */
5989 unsigned dist; /* distance of matched string */
5990 int lc; /* match length or unmatched char (if dist == 0) */
5991 unsigned lx = 0; /* running index in l_buf */
5992- unsigned code; /* the code to send */
5993+ int code; /* the code to send */
5994 int extra; /* number of extra bits to send */
5995
5996 if (s->last_lit != 0) {
5997@@ -1019,7 +1028,7 @@ local void compress_block(deflate_state *s, const ct_data *ltree, const ct_data
5998 * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
5999 * IN assertion: the fields Freq of dyn_ltree are set.
6000 */
6001-local int detect_data_type(deflate_state *s) {
6002+static int detect_data_type(deflate_state *s) {
6003 /* black_mask is the bit mask of black-listed bytes
6004 * set bits 0..6, 14..25, and 28..31
6005 * 0xf3ffc07f = binary 11110011111111111100000001111111
6006@@ -1050,7 +1059,7 @@ local int detect_data_type(deflate_state *s) {
6007 * method would use a table)
6008 * IN assertion: 1 <= len <= 15
6009 */
6010-local unsigned bi_reverse(unsigned code, int len) {
6011+static unsigned bi_reverse(unsigned code, int len) {
6012 /* code: the value to invert */
6013 /* len: its bit length */
6014 register unsigned res = 0;
6015@@ -1064,7 +1073,7 @@ local unsigned bi_reverse(unsigned code, int len) {
6016 /* ===========================================================================
6017 * Flush the bit buffer, keeping at most 7 bits in it.
6018 */
6019-local void bi_flush(deflate_state *s) {
6020+static void bi_flush(deflate_state *s) {
6021 if (s->bi_valid == 16) {
6022 put_short(s, s->bi_buf);
6023 s->bi_buf = 0;
6024@@ -1087,33 +1096,7 @@ ZLIB_INTERNAL void bi_windup(deflate_state *s) {
6025 }
6026 s->bi_buf = 0;
6027 s->bi_valid = 0;
6028-#ifdef DEBUG
6029+#ifdef ZLIB_DEBUG
6030 s->bits_sent = (s->bits_sent+7) & ~7;
6031 #endif
6032 }
6033-
6034-/* ===========================================================================
6035- * Copy a stored block, storing first the length and its
6036- * one's complement if requested.
6037- */
6038-local void copy_block(deflate_state *s, char *buf, unsigned len, int header) {
6039- /* buf: the input data */
6040- /* len: its length */
6041- /* header: true if block header must be written */
6042- bi_windup(s); /* align on byte boundary */
6043-
6044- if (header) {
6045- put_short(s, (uint16_t)len);
6046- put_short(s, (uint16_t)~len);
6047-#ifdef DEBUG
6048- s->bits_sent += 2*16;
6049-#endif
6050- }
6051-#ifdef DEBUG
6052- s->bits_sent += (unsigned long)len << 3;
6053-#endif
6054- while (len--) {
6055- put_byte(s, *buf++);
6056- }
6057-}
6058-
6059diff --git a/trees.h b/trees.h
6060index debfbdd..6fc1c84 100644
6061--- a/trees.h
6062+++ b/trees.h
6063@@ -64,7 +64,7 @@ ZLIB_INTERNAL const ct_data static_ltree[L_CODES+2] = {
6064 {{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
6065 };
6066
6067-local const ct_data static_dtree[D_CODES] = {
6068+static const ct_data static_dtree[D_CODES] = {
6069 {{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
6070 {{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
6071 {{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
6072@@ -118,12 +118,12 @@ const unsigned char ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {
6073 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
6074 };
6075
6076-local const int base_length[LENGTH_CODES] = {
6077+static const int base_length[LENGTH_CODES] = {
6078 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
6079 64, 80, 96, 112, 128, 160, 192, 224, 0
6080 };
6081
6082-local const int base_dist[D_CODES] = {
6083+static const int base_dist[D_CODES] = {
6084 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
6085 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
6086 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
6087diff --git a/uncompr.c b/uncompr.c
6088index c2af140..7bf47b8 100644
6089--- a/uncompr.c
6090+++ b/uncompr.c
6091@@ -1,5 +1,5 @@
6092 /* uncompr.c -- decompress a memory buffer
6093- * Copyright (C) 1995-2003, 2010, 2014 Jean-loup Gailly, Mark Adler.
6094+ * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler.
6095 * For conditions of distribution and use, see copyright notice in zlib.h
6096 */
6097
6098@@ -9,26 +9,29 @@
6099 #include "zlib.h"
6100
6101 /* ===========================================================================
6102- Decompresses the source buffer into the destination buffer. sourceLen is
6103- the byte length of the source buffer. Upon entry, destLen is the total
6104- size of the destination buffer, which must be large enough to hold the
6105- entire uncompressed data. (The size of the uncompressed data must have
6106- been saved previously by the compressor and transmitted to the decompressor
6107- by some mechanism outside the scope of this compression library.)
6108- Upon exit, destLen is the actual size of the compressed buffer.
6109+ Decompresses the source buffer into the destination buffer. *sourceLen is
6110+ the byte length of the source buffer. Upon entry, *destLen is the total size
6111+ of the destination buffer, which must be large enough to hold the entire
6112+ uncompressed data. (The size of the uncompressed data must have been saved
6113+ previously by the compressor and transmitted to the decompressor by some
6114+ mechanism outside the scope of this compression library.) Upon exit,
6115+ *destLen is the size of the decompressed data and *sourceLen is the number
6116+ of source bytes consumed. Upon return, source + *sourceLen points to the
6117+ first unused input byte.
6118
6119- uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
6120- enough memory, Z_BUF_ERROR if there was not enough room in the output
6121- buffer, or Z_DATA_ERROR if the input data was corrupted, including if the
6122- input data is an incomplete zlib stream.
6123+ uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
6124+ memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
6125+ Z_DATA_ERROR if the input data was corrupted, including if the input data is
6126+ an incomplete zlib stream.
6127 */
6128-int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsigned char *source, size_t sourceLen) {
6129+int ZEXPORT uncompress2(unsigned char *dest, size_t *destLen, const unsigned char *source, size_t *sourceLen) {
6130 z_stream stream;
6131 int err;
6132- const unsigned int max = (unsigned int)0 - 1;
6133- size_t left;
6134+ const unsigned int max = (unsigned int)-1;
6135+ size_t len, left;
6136 unsigned char buf[1]; /* for detection of incomplete stream when *destLen == 0 */
6137
6138+ len = *sourceLen;
6139 if (*destLen) {
6140 left = *destLen;
6141 *destLen = 0;
6142@@ -40,8 +43,8 @@ int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsigned char
6143
6144 stream.next_in = (const unsigned char *)source;
6145 stream.avail_in = 0;
6146- stream.zalloc = (alloc_func)0;
6147- stream.zfree = (free_func)0;
6148+ stream.zalloc = NULL;
6149+ stream.zfree = NULL;
6150 stream.opaque = NULL;
6151
6152 err = inflateInit(&stream);
6153@@ -56,12 +59,13 @@ int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsigned char
6154 left -= stream.avail_out;
6155 }
6156 if (stream.avail_in == 0) {
6157- stream.avail_in = sourceLen > (unsigned long)max ? max : (unsigned int)sourceLen;
6158- sourceLen -= stream.avail_in;
6159+ stream.avail_in = len > (unsigned long)max ? max : (unsigned int)len;
6160+ len -= stream.avail_in;
6161 }
6162 err = inflate(&stream, Z_NO_FLUSH);
6163 } while (err == Z_OK);
6164
6165+ *sourceLen -= len + stream.avail_in;
6166 if (dest != buf)
6167 *destLen = stream.total_out;
6168 else if (stream.total_out && err == Z_BUF_ERROR)
6169@@ -73,3 +77,8 @@ int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsigned char
6170 err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
6171 err;
6172 }
6173+
6174+int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsigned char *source, size_t sourceLen)
6175+{
6176+ return uncompress2(dest, destLen, source, &sourceLen);
6177+}
6178diff --git a/zconf.h.in b/zconf.h.in
6179index 7cacf1b..5649aee 100644
6180--- a/zconf.h.in
6181+++ b/zconf.h.in
6182@@ -48,7 +48,7 @@
6183 Of course this will generally degrade compression (there's no free lunch).
6184
6185 The memory requirements for inflate are (in bytes) 1 << windowBits
6186- that is, 32K for windowBits=15 (default value) plus a few kilobytes
6187+ that is, 32K for windowBits=15 (default value) plus about 7 kilobytes
6188 for small objects.
6189 */
6190
6191@@ -118,9 +118,7 @@ typedef void *voidp;
6192 #include <sys/types.h> /* for off_t */
6193 #include <stdarg.h> /* for va_list */
6194
6195-#ifdef WIN32
6196-# include <stddef.h> /* for wchar_t */
6197-#endif
6198+#include <stddef.h> /* for wchar_t and NULL */
6199
6200 /* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and
6201 * "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even
6202diff --git a/zlib.h b/zlib.h
6203index 3a1cbb2..5ef1e30 100644
6204--- a/zlib.h
6205+++ b/zlib.h
6206@@ -72,7 +72,8 @@ extern "C" {
6207 with "gz". The gzip format is different from the zlib format. gzip is a
6208 gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
6209
6210- This library can optionally read and write gzip streams in memory as well.
6211+ This library can optionally read and write gzip and raw deflate streams in
6212+ memory as well.
6213
6214 The zlib format was designed to be compact and fast for use in memory
6215 and on communications channels. The gzip format was designed for single-
6216@@ -81,7 +82,7 @@ extern "C" {
6217
6218 The library does not install any signal handler. The decoder checks
6219 the consistency of the compressed data, so the library should never crash
6220- even in case of corrupted input.
6221+ even in the case of corrupted input.
6222 */
6223
6224 typedef void *(*alloc_func) (void *opaque, unsigned int items, unsigned int size);
6225@@ -94,7 +95,7 @@ typedef struct z_stream_s {
6226 uint32_t avail_in; /* number of bytes available at next_in */
6227 size_t total_in; /* total number of input bytes read so far */
6228
6229- unsigned char *next_out; /* next output byte should be put there */
6230+ unsigned char *next_out; /* next output byte will go here */
6231 uint32_t avail_out; /* remaining free space at next_out */
6232 size_t total_out; /* total number of bytes output so far */
6233
6234@@ -105,8 +106,9 @@ typedef struct z_stream_s {
6235 free_func zfree; /* used to free the internal state */
6236 void *opaque; /* private data object passed to zalloc and zfree */
6237
6238- int data_type; /* best guess about the data type: binary or text */
6239- uint32_t adler; /* adler32 value of the uncompressed data */
6240+ int data_type; /* best guess about the data type: binary or text
6241+ for deflate, or the decoding state for inflate */
6242+ uint32_t adler; /* Adler-32 or CRC-32 value of the uncompressed data */
6243 unsigned long reserved; /* reserved for future use */
6244 } z_stream;
6245
6246@@ -121,12 +123,12 @@ typedef struct gz_header_s {
6247 unsigned long time; /* modification time */
6248 int xflags; /* extra flags (not used when writing a gzip file) */
6249 int os; /* operating system */
6250- unsigned char *extra; /* pointer to extra field or Z_NULL if none */
6251- unsigned int extra_len; /* extra field length (valid if extra != Z_NULL) */
6252+ unsigned char *extra; /* pointer to extra field or NULL if none */
6253+ unsigned int extra_len; /* extra field length (valid if extra != NULL) */
6254 unsigned int extra_max; /* space at extra (only when reading header) */
6255- unsigned char *name; /* pointer to zero-terminated file name or Z_NULL */
6256+ unsigned char *name; /* pointer to zero-terminated file name or NULL */
6257 unsigned int name_max; /* space at name (only when reading header) */
6258- unsigned char *comment; /* pointer to zero-terminated comment or Z_NULL */
6259+ unsigned char *comment; /* pointer to zero-terminated comment or NULL */
6260 unsigned int comm_max; /* space at comment (only when reading header) */
6261 int hcrc; /* true if there was or will be a header crc */
6262 int done; /* true when done reading gzip header (not used when writing a gzip file) */
6263@@ -146,13 +148,15 @@ typedef gz_header *gz_headerp;
6264 memory management. The compression library attaches no meaning to the
6265 opaque value.
6266
6267- zalloc must return Z_NULL if there is not enough memory for the object.
6268+ zalloc must return NULL if there is not enough memory for the object.
6269 If zlib is used in a multi-threaded application, zalloc and zfree must be
6270- thread safe.
6271+ thread safe. In that case, zlib is thread-safe. When zalloc and zfree are
6272+ Z_NULL on entry to the initialization function, they are set to internal
6273+ routines that use the standard library functions malloc() and free().
6274
6275 The fields total_in and total_out can be used for statistics or progress
6276 reports. After compression, total_in holds the total size of the
6277- uncompressed data and may be saved for use in the decompressor (particularly
6278+ uncompressed data and may be saved for use by the decompressor (particularly
6279 if the decompressor wants to decompress everything in a single step).
6280 */
6281
6282@@ -197,12 +201,12 @@ typedef gz_header *gz_headerp;
6283 #define Z_TEXT 1
6284 #define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */
6285 #define Z_UNKNOWN 2
6286-/* Possible values of the data_type field (though see inflate()) */
6287+/* Possible values of the data_type field for deflate() */
6288
6289 #define Z_DEFLATED 8
6290 /* The deflate compression method (the only one supported in this version) */
6291
6292-#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
6293+#define Z_NULL NULL /* for compatibility with zlib, was for initializing zalloc, zfree, opaque */
6294
6295 #define zlib_version zlibVersion()
6296 /* for compatibility with versions < 1.0.2 */
6297@@ -222,7 +226,7 @@ ZEXTERN int ZEXPORT deflateInit (z_stream *strm, int level);
6298
6299 Initializes the internal stream state for compression. The fields
6300 zalloc, zfree and opaque must be initialized before by the caller. If
6301- zalloc and zfree are set to Z_NULL, deflateInit updates them to use default
6302+ zalloc and zfree are set to NULL, deflateInit updates them to use default
6303 allocation functions.
6304
6305 The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
6306@@ -255,11 +259,11 @@ ZEXTERN int ZEXPORT deflate(z_stream *strm, int flush);
6307 enough room in the output buffer), next_in and avail_in are updated and
6308 processing will resume at this point for the next call of deflate().
6309
6310- - Provide more output starting at next_out and update next_out and avail_out
6311+ - Generate more output starting at next_out and update next_out and avail_out
6312 accordingly. This action is forced if the parameter flush is non zero.
6313 Forcing flush frequently degrades the compression ratio, so this parameter
6314- should be set only when necessary (in interactive applications). Some
6315- output may be provided even if flush is not set.
6316+ should be set only when necessary. Some output may be provided even if
6317+ flush is zero.
6318
6319 Before the call of deflate(), the application should ensure that at least
6320 one of the actions is possible, by providing more input and/or consuming more
6321@@ -268,7 +272,9 @@ ZEXTERN int ZEXPORT deflate(z_stream *strm, int flush);
6322 output when it wants, for example when the output buffer is full (avail_out
6323 == 0), or after each call of deflate(). If deflate returns Z_OK and with
6324 zero avail_out, it must be called again after making room in the output
6325- buffer because there might be more output pending.
6326+ buffer because there might be more output pending. See deflatePending(),
6327+ which can be used if desired to determine whether or not there is more ouput
6328+ in that case.
6329
6330 Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
6331 decide how much data to accumulate before producing output, in order to
6332@@ -289,8 +295,8 @@ ZEXTERN int ZEXPORT deflate(z_stream *strm, int flush);
6333 input data so far will be available to the decompressor, as for Z_SYNC_FLUSH.
6334 This completes the current deflate block and follows it with an empty fixed
6335 codes block that is 10 bits long. This assures that enough bytes are output
6336- in order for the decompressor to finish the block before the empty fixed code
6337- block.
6338+ in order for the decompressor to finish the block before the empty fixed
6339+ codes block.
6340
6341 If flush is set to Z_BLOCK, a deflate block is completed and emitted, as
6342 for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to
6343@@ -316,34 +322,38 @@ ZEXTERN int ZEXPORT deflate(z_stream *strm, int flush);
6344
6345 If the parameter flush is set to Z_FINISH, pending input is processed,
6346 pending output is flushed and deflate returns with Z_STREAM_END if there was
6347- enough output space; if deflate returns with Z_OK, this function must be
6348- called again with Z_FINISH and more output space (updated avail_out) but no
6349- more input data, until it returns with Z_STREAM_END or an error. After
6350- deflate has returned Z_STREAM_END, the only possible operations on the stream
6351- are deflateReset or deflateEnd.
6352-
6353- Z_FINISH can be used immediately after deflateInit if all the compression
6354- is to be done in a single step. In this case, avail_out must be at least the
6355- value returned by deflateBound (see below). Then deflate is guaranteed to
6356- return Z_STREAM_END. If not enough output space is provided, deflate will
6357- not return Z_STREAM_END, and it must be called again as described above.
6358-
6359- deflate() sets strm->adler to the adler32 checksum of all input read
6360- so far (that is, total_in bytes).
6361+ enough output space. If deflate returns with Z_OK or Z_BUF_ERROR, this
6362+ function must be called again with Z_FINISH and more output space (updated
6363+ avail_out) but no more input data, until it returns with Z_STREAM_END or an
6364+ error. After deflate has returned Z_STREAM_END, the only possible operations
6365+ on the stream are deflateReset or deflateEnd.
6366+
6367+ Z_FINISH can be used in the first deflate call after deflateInit if all the
6368+ compression is to be done in a single step. In order to complete in one
6369+ call, avail_out must be at least the value returned by deflateBound (see
6370+ below). Then deflate is guaranteed to return Z_STREAM_END. If not enough
6371+ output space is provided, deflate will not return Z_STREAM_END, and it must
6372+ be called again as described above.
6373+
6374+ deflate() sets strm->adler to the Adler-32 checksum of all input read
6375+ so far (that is, total_in bytes). If a gzip stream is being generated, then
6376+ strm->adler will be the CRC-32 checksum of the input read so far. (See
6377+ deflateInit2 below.)
6378
6379 deflate() may update strm->data_type if it can make a good guess about
6380- the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered
6381- binary. This field is only for information purposes and does not affect the
6382- compression algorithm in any manner.
6383+ the input data type (Z_BINARY or Z_TEXT). If in doubt, the data is
6384+ considered binary. This field is only for information purposes and does not
6385+ affect the compression algorithm in any manner.
6386
6387 deflate() returns Z_OK if some progress has been made (more input
6388 processed or more output produced), Z_STREAM_END if all input has been
6389 consumed and all output has been produced (only when flush is set to
6390 Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
6391- if next_in or next_out was Z_NULL), Z_BUF_ERROR if no progress is possible
6392- (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not
6393- fatal, and deflate() can be called again with more input and more output
6394- space to continue compressing.
6395+ if next_in or next_out was NULL) or the state was inadvertently written over
6396+ by the application), or Z_BUF_ERROR if no progress is possible (for example
6397+ avail_in or avail_out was zero). Note that Z_BUF_ERROR is not fatal, and
6398+ deflate() can be called again with more input and more output space to
6399+ continue compressing.
6400 */
6401
6402
6403@@ -366,23 +376,20 @@ ZEXTERN int ZEXPORT inflateInit (z_stream *strm);
6404
6405 Initializes the internal stream state for decompression. The fields
6406 next_in, avail_in, zalloc, zfree and opaque must be initialized before by
6407- the caller. If next_in is not Z_NULL and avail_in is large enough (the
6408- exact value depends on the compression method), inflateInit determines the
6409- compression method from the zlib header and allocates all data structures
6410- accordingly; otherwise the allocation will be deferred to the first call of
6411- inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
6412+ the caller. In the current version of inflate, the provide input is not
6413+ read or consumed. Any memory allocation will be deferred to the first call
6414+ of inflate. If zalloc and zfree are set to NULL, inflateInit updates them to
6415 use default allocation functions.
6416
6417 inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
6418 memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
6419 version assumed by the caller, or Z_STREAM_ERROR if the parameters are
6420 invalid, such as a null pointer to the structure. msg is set to null if
6421- there is no error message. inflateInit does not perform any decompression
6422- apart from possibly reading the zlib header if present: actual decompression
6423- will be done by inflate(). (So next_in and avail_in may be modified, but
6424- next_out and avail_out are unused and unchanged.) The current implementation
6425- of inflateInit() does not process any header information -- that is deferred
6426- until inflate() is called.
6427+ there is no error message. inflateInit does not perform any decompression.
6428+ Actual decompression will be done by inflate(). So next_in, and avail_in,
6429+ next_out, and avail_out are unused and unchanged. The current
6430+ implementation of inflateInit() does not process any header information --
6431+ that is deferred until inflate() is called.
6432 */
6433
6434
6435@@ -398,17 +405,20 @@ ZEXTERN int ZEXPORT inflate(z_stream *strm, int flush);
6436
6437 - Decompress more input starting at next_in and update next_in and avail_in
6438 accordingly. If not all input can be processed (because there is not
6439- enough room in the output buffer), next_in is updated and processing will
6440- resume at this point for the next call of inflate().
6441+ enough room in the output buffer), then next_in and avail_on are updated
6442+ accordingly, and processing will resume at this point for the next call of
6443+ inflate().
6444
6445- - Provide more output starting at next_out and update next_out and avail_out
6446+ - Generate more output starting at next_out and update next_out and avail_out
6447 accordingly. inflate() provides as much output as possible, until there is
6448 no more input data or no more space in the output buffer (see below about
6449 the flush parameter).
6450
6451 Before the call of inflate(), the application should ensure that at least
6452 one of the actions is possible, by providing more input and/or consuming more
6453- output, and updating the next_* and avail_* values accordingly. The
6454+ output, and updating the next_* and avail_* values accordingly. If the
6455+ caller of inflate() does not provide both available input and available
6456+ output space, it is possible that there will be no progress made. The
6457 application can consume the uncompressed output when it wants, for example
6458 when the output buffer is full (avail_out == 0), or after each call of
6459 inflate(). If inflate returns Z_OK and with zero avail_out, it must be
6460@@ -425,7 +435,7 @@ ZEXTERN int ZEXPORT inflate(z_stream *strm, int flush);
6461 gets to the end of that block, or when it runs out of data.
6462
6463 The Z_BLOCK option assists in appending to or combining deflate streams.
6464- Also to assist in this, on return inflate() will set strm->data_type to the
6465+ To assist in this, on return inflate() always sets strm->data_type to the
6466 number of unused bits in the last byte taken from strm->next_in, plus 64 if
6467 inflate() is currently decoding the last block in the deflate stream, plus
6468 128 if inflate() returned immediately after decoding an end-of-block code or
6469@@ -451,7 +461,7 @@ ZEXTERN int ZEXPORT inflate(z_stream *strm, int flush);
6470 this case all pending input is processed and all pending output is flushed;
6471 avail_out must be large enough to hold all of the uncompressed data for the
6472 operation to complete. (The size of the uncompressed data may have been
6473- saved by the compressor for this purpose.) The use of Z_FINISH is not
6474+ saved by the compressor for this purpose.) The use of Z_FINISH is not
6475 required to perform an inflation in one step. However it may be used to
6476 inform inflate that a faster approach can be used for the single inflate()
6477 call. Z_FINISH also informs inflate to not maintain a sliding window if the
6478@@ -473,32 +483,33 @@ ZEXTERN int ZEXPORT inflate(z_stream *strm, int flush);
6479 chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
6480 strm->adler to the Adler-32 checksum of all output produced so far (that is,
6481 total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
6482- below. At the end of the stream, inflate() checks that its computed adler32
6483+ below. At the end of the stream, inflate() checks that its computed Adler-32
6484 checksum is equal to that saved by the compressor and returns Z_STREAM_END
6485 only if the checksum is correct.
6486
6487 inflate() can decompress and check either zlib-wrapped or gzip-wrapped
6488 deflate data. The header type is detected automatically, if requested when
6489 initializing with inflateInit2(). Any information contained in the gzip
6490- header is not retained, so applications that need that information should
6491- instead use raw inflate, see inflateInit2() below, or inflateBack() and
6492- perform their own processing of the gzip header and trailer. When processing
6493+ header is not retained unless inflateGetHeader() is used. When processing
6494 gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output
6495- producted so far. The CRC-32 is checked against the gzip trailer.
6496+ produced so far. The CRC-32 is checked against the gzip trailer, as is the
6497+ uncompressed length, modulo 2^32.
6498
6499 inflate() returns Z_OK if some progress has been made (more input processed
6500 or more output produced), Z_STREAM_END if the end of the compressed data has
6501 been reached and all uncompressed output has been produced, Z_NEED_DICT if a
6502 preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
6503 corrupted (input stream not conforming to the zlib format or incorrect check
6504- value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
6505- next_in or next_out was Z_NULL), Z_MEM_ERROR if there was not enough memory,
6506- Z_BUF_ERROR if no progress is possible or if there was not enough room in the
6507- output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
6508+ value, in which case strm->msg points to a string with a more specific
6509+ error), Z_STREAM_ERROR if the stream structure was inconsistent (for example
6510+ next_in or next_out was NULL, or the state was inadvertently written over
6511+ by the application), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR
6512+ if no progress is possible or if there was not enough room in the output
6513+ buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
6514 inflate() can be called again with more input and more output space to
6515 continue decompressing. If Z_DATA_ERROR is returned, the application may
6516 then call inflateSync() to look for a good compression block if a partial
6517- recovery of the data is desired.
6518+ recovery of the data is to be attempted.
6519 */
6520
6521
6522@@ -508,9 +519,8 @@ ZEXTERN int ZEXPORT inflateEnd(z_stream *strm);
6523 This function discards any unprocessed input and does not flush any pending
6524 output.
6525
6526- inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
6527- was inconsistent. In the error case, msg may be set but then points to a
6528- static string (which must not be deallocated).
6529+ inflateEnd returns Z_OK if success, or Z_STREAM_ERROR if the stream state
6530+ was inconsistent.
6531 */
6532
6533
6534@@ -551,14 +561,19 @@ ZEXTERN int ZEXPORT deflateInit2 (z_stream *strm,
6535
6536 windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
6537 determines the window size. deflate() will then generate raw deflate data
6538- with no zlib header or trailer, and will not compute an adler32 check value.
6539+ with no zlib header or trailer, and will not compute a check value.
6540
6541 windowBits can also be greater than 15 for optional gzip encoding. Add
6542 16 to windowBits to write a simple gzip header and trailer around the
6543 compressed data instead of a zlib wrapper. The gzip header will have no
6544 file name, no extra data, no comment, no modification time (set to zero), no
6545- header crc, and the operating system will be set to 255 (unknown). If a
6546- gzip stream is being written, strm->adler is a crc32 instead of an adler32.
6547+ header crc, and the operating system will be set to the appropriate value,
6548+ if the operating system was determined at compile time. If a gzip stream is
6549+ being written, strm->adler is a CRC-32 instead of an Adler-32.
6550+
6551+ For raw deflate or gzip encoding, a request for a 256-byte window is
6552+ rejected as invalid, since only the zlib header provides a means of
6553+ transmitting the window size to the decompressor.
6554
6555 The memLevel parameter specifies how much memory should be allocated
6556 for the internal compression state. memLevel=1 uses minimum memory but is
6557@@ -619,15 +634,15 @@ ZEXTERN int ZEXPORT deflateSetDictionary(z_stream *strm,
6558 addition, the current implementation of deflate will use at most the window
6559 size minus 262 bytes of the provided dictionary.
6560
6561- Upon return of this function, strm->adler is set to the adler32 value
6562+ Upon return of this function, strm->adler is set to the Adler-32 value
6563 of the dictionary; the decompressor may later use this value to determine
6564- which dictionary has been used by the compressor. (The adler32 value
6565+ which dictionary has been used by the compressor. (The Adler-32 value
6566 applies to the whole dictionary even if only a subset of the dictionary is
6567 actually used by the compressor.) If a raw deflate was requested, then the
6568- adler32 value is not computed and strm->adler is not set.
6569+ Adler-32 value is not computed and strm->adler is not set.
6570
6571 deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
6572- parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is
6573+ parameter is invalid (e.g. dictionary being NULL) or the stream state is
6574 inconsistent (for example if deflate has already been called for this stream
6575 or if not at a block boundary for raw deflate). deflateSetDictionary does
6576 not perform any compression: this will be done by deflate().
6577@@ -646,7 +661,7 @@ ZEXTERN int ZEXPORT deflateCopy(z_stream *dest, z_stream *source);
6578
6579 deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
6580 enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
6581- (such as zalloc being Z_NULL). msg is left unchanged in both source and
6582+ (such as zalloc being NULL). msg is left unchanged in both source and
6583 destination.
6584 */
6585
6586@@ -658,7 +673,7 @@ ZEXTERN int ZEXPORT deflateReset(z_stream *strm);
6587 set unchanged.
6588
6589 deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
6590- stream state was inconsistent (such as zalloc or state being Z_NULL).
6591+ stream state was inconsistent (such as zalloc or state being NULL).
6592 */
6593
6594 ZEXTERN int ZEXPORT deflateParams(z_stream *strm, int level, int strategy);
6595@@ -674,24 +689,25 @@ ZEXTERN int ZEXPORT deflateParams(z_stream *strm, int level, int strategy);
6596 new level and strategy will take effect at the next call of deflate().
6597
6598 If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does
6599- not have enough output space to complete, then the parameter change will
6600- take effect at an undetermined location in the uncompressed data provided so
6601- far. In order to assure a change in the parameters at a specific location
6602- in the uncompressed data, the deflate stream should first be flushed with
6603- Z_BLOCK or another flush parameter, and deflate() called until
6604- strm.avail_out is not zero, before the call of deflateParams(). Then no
6605- more input data should be provided before the deflateParams() call. If this
6606- is done, the old level and strategy will be applied to the data compressed
6607- before deflateParams(), and the new level and strategy will be applied to
6608- the the data compressed after deflateParams().
6609-
6610- deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source stream
6611+ not have enough output space to complete, then the parameter change will not
6612+ take effect. In this case, deflateParams() can be called again with the
6613+ same parameters and more output space to try again.
6614+
6615+ In order to assure a change in the parameters on the first try, the
6616+ deflate stream should be flushed using deflate() with Z_BLOCK or other flush
6617+ request until strm.avail_out is not zero, before calling deflateParams().
6618+ Then no more input data should be provided before the deflateParams() call.
6619+ If this is done, the old level and strategy will be applied to the data
6620+ compressed before deflateParams(), and the new level and strategy will be
6621+ applied to the the data compressed after deflateParams().
6622+
6623+ deflateParams returns Z_OK on success, Z_STREAM_ERROR if the source stream
6624 state was inconsistent or if a parameter was invalid, or Z_BUF_ERROR if
6625- there was not enough output space to complete the compression before the
6626- parameters were changed. Note that in the case of a Z_BUF_ERROR, the
6627- parameters are changed nevertheless, and will take effect at an undetermined
6628- location in the previously supplied uncompressed data. Compression may
6629- proceed after a Z_BUF_ERROR.
6630+ there was not enough output space to complete the compression of the
6631+ available input data before a change in the strategy or approach. Note that
6632+ in the case of a Z_BUF_ERROR, the parameters are not changed. A return
6633+ value of Z_BUF_ERROR is not fatal, in which case deflateParams() can be
6634+ retried with more output space.
6635 */
6636
6637 ZEXTERN int ZEXPORT deflateTune(z_stream *strm, int good_length, int max_lazy, int nice_length, int max_chain);
6638@@ -728,7 +744,7 @@ ZEXTERN int ZEXPORT deflatePending(z_stream *strm, uint32_t *pending, int *bits)
6639 provided would be due to the available output space having being consumed.
6640 The number of bits of output not provided are between 0 and 7, where they
6641 await more bits to join them in order to fill out a full byte. If pending
6642- or bits are Z_NULL, then those values are not set.
6643+ or bits are NULL, then those values are not set.
6644
6645 deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source
6646 stream state was inconsistent.
6647@@ -757,8 +773,8 @@ ZEXTERN int ZEXPORT deflateSetHeader(z_stream *strm, gz_headerp head);
6648 deflate(). The text, time, os, extra field, name, and comment information
6649 in the provided gz_header structure are written to the gzip header (xflag is
6650 ignored -- the extra flags are set according to the compression level). The
6651- caller must assure that, if not Z_NULL, name and comment are terminated with
6652- a zero byte, and that if extra is not Z_NULL, that extra_len bytes are
6653+ caller must assure that, if not NULL, name and comment are terminated with
6654+ a zero byte, and that if extra is not NULL, that extra_len bytes are
6655 available there. If hcrc is true, a gzip header crc is included. Note that
6656 the current versions of the command-line version of gzip (up through version
6657 1.3.x) do not support header crc's, and will report that it is a "multi-part
6658@@ -798,7 +814,7 @@ ZEXTERN int ZEXPORT inflateInit2(z_stream *strm, int windowBits);
6659 is for use with other formats that use the deflate compressed data format
6660 such as zip. Those formats provide their own check values. If a custom
6661 format is developed using the raw deflate format for compressed data, it is
6662- recommended that a check value such as an adler32 or a crc32 be applied to
6663+ recommended that a check value such as an Adler-32 or a CRC-32 be applied to
6664 the uncompressed data as is done in the zlib, gzip, and zip formats. For
6665 most applications, the zlib format should be used as is. Note that comments
6666 above on the use in deflateInit2() applies to the magnitude of windowBits.
6667@@ -807,7 +823,10 @@ ZEXTERN int ZEXPORT inflateInit2(z_stream *strm, int windowBits);
6668 32 to windowBits to enable zlib and gzip decoding with automatic header
6669 detection, or add 16 to decode only the gzip format (the zlib format will
6670 return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a
6671- crc32 instead of an adler32.
6672+ CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see
6673+ below), inflate() will not automatically decode concatenated gzip streams.
6674+ inflate() will return Z_STREAM_END at the end of the gzip stream. The state
6675+ would need to be reset to continue decoding a subsequent gzip stream.
6676
6677 inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
6678 memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
6679@@ -826,7 +845,7 @@ ZEXTERN int ZEXPORT inflateSetDictionary(z_stream *strm, const unsigned char *di
6680 Initializes the decompression dictionary from the given uncompressed byte
6681 sequence. This function must be called immediately after a call of inflate,
6682 if that call returned Z_NEED_DICT. The dictionary chosen by the compressor
6683- can be determined from the adler32 value returned by that call of inflate.
6684+ can be determined from the Adler-32 value returned by that call of inflate.
6685 The compressor and decompressor must use exactly the same dictionary (see
6686 deflateSetDictionary). For raw inflate, this function can be called at any
6687 time to set the dictionary. If the provided dictionary is smaller than the
6688@@ -835,9 +854,9 @@ ZEXTERN int ZEXPORT inflateSetDictionary(z_stream *strm, const unsigned char *di
6689 that was used for compression is provided.
6690
6691 inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
6692- parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is
6693+ parameter is invalid (e.g. dictionary being NULL) or the stream state is
6694 inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
6695- expected one (incorrect adler32 value). inflateSetDictionary does not
6696+ expected one (incorrect Adler-32 value). inflateSetDictionary does not
6697 perform any decompression: this will be done by subsequent calls of
6698 inflate().
6699 */
6700@@ -848,8 +867,8 @@ ZEXTERN int ZEXPORT inflateGetDictionary(z_stream *strm, unsigned char *dictiona
6701 set to the number of bytes in the dictionary, and that many bytes are copied
6702 to dictionary. dictionary must have enough space, where 32768 bytes is
6703 always enough. If inflateGetDictionary() is called with dictionary equal to
6704- Z_NULL, then only the dictionary length is returned, and nothing is copied.
6705- Similary, if dictLength is Z_NULL, then it is not set.
6706+ NULL, then only the dictionary length is returned, and nothing is copied.
6707+ Similary, if dictLength is NULL, then it is not set.
6708
6709 inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the
6710 stream state is inconsistent.
6711@@ -885,28 +904,30 @@ ZEXTERN int ZEXPORT inflateCopy(z_stream *dest, z_stream *source);
6712
6713 inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
6714 enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
6715- (such as zalloc being Z_NULL). msg is left unchanged in both source and
6716+ (such as zalloc being NULL). msg is left unchanged in both source and
6717 destination.
6718 */
6719
6720 ZEXTERN int ZEXPORT inflateReset(z_stream *strm);
6721 /*
6722 This function is equivalent to inflateEnd followed by inflateInit,
6723- but does not free and reallocate all the internal decompression state. The
6724+ but does not free and reallocate the internal decompression state. The
6725 stream will keep attributes that may have been set by inflateInit2.
6726
6727 inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
6728- stream state was inconsistent (such as zalloc or state being Z_NULL).
6729+ stream state was inconsistent (such as zalloc or state being NULL).
6730 */
6731
6732 ZEXTERN int ZEXPORT inflateReset2(z_stream *strm, int windowBits);
6733 /*
6734 This function is the same as inflateReset, but it also permits changing
6735 the wrap and window size requests. The windowBits parameter is interpreted
6736- the same as it is for inflateInit2.
6737+ the same as it is for inflateInit2. If the window size is changed, then the
6738+ memory allocated for the window is freed, and the window will be reallocated
6739+ by inflate() if needed.
6740
6741 inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source
6742- stream state was inconsistent (such as zalloc or state being Z_NULL), or if
6743+ stream state was inconsistent (such as zalloc or state being NULL), or if
6744 the windowBits parameter is invalid.
6745 */
6746
6747@@ -953,7 +974,7 @@ ZEXTERN long ZEXPORT inflateMark(z_stream *strm);
6748 location in the input stream can be determined from avail_in and data_type
6749 as noted in the description for the Z_BLOCK flush parameter for inflate.
6750
6751- inflateMark returns the value noted above or -65536 if the provided
6752+ inflateMark returns the value noted above, or -65536 if the provided
6753 source stream state was inconsistent.
6754 */
6755
6756@@ -971,16 +992,16 @@ ZEXTERN int ZEXPORT inflateGetHeader(z_stream *strm, gz_headerp head);
6757
6758 The text, time, xflags, and os fields are filled in with the gzip header
6759 contents. hcrc is set to true if there is a header CRC. (The header CRC
6760- was valid if done is set to one.) If extra is not Z_NULL, then extra_max
6761+ was valid if done is set to one.) If extra is not NULL, then extra_max
6762 contains the maximum number of bytes to write to extra. Once done is true,
6763 extra_len contains the actual extra field length, and extra contains the
6764 extra field, or that field truncated if extra_max is less than extra_len.
6765- If name is not Z_NULL, then up to name_max characters are written there,
6766+ If name is not NULL, then up to name_max characters are written there,
6767 terminated with a zero unless the length is greater than name_max. If
6768- comment is not Z_NULL, then up to comm_max characters are written there,
6769+ comment is not NULL, then up to comm_max characters are written there,
6770 terminated with a zero unless the length is greater than comm_max. When any
6771- of extra, name, or comment are not Z_NULL and the respective field is not
6772- present in the header, then that field is set to Z_NULL to signal its
6773+ of extra, name, or comment are not NULL and the respective field is not
6774+ present in the header, then that field is set to NULL to signal its
6775 absence. This allows the use of deflateSetHeader() with the returned
6776 structure to duplicate the header. However if those fields are set to
6777 allocated memory, then the application will need to save those pointers
6778@@ -1001,7 +1022,7 @@ ZEXTERN int ZEXPORT inflateBackInit (z_stream *strm, int windowBits, unsigned ch
6779
6780 Initialize the internal stream state for decompression using inflateBack()
6781 calls. The fields zalloc, zfree and opaque in strm must be initialized
6782- before the call. If zalloc and zfree are Z_NULL, then the default library-
6783+ before the call. If zalloc and zfree are NULL, then the default library-
6784 derived memory allocation routines are used. windowBits is the base two
6785 logarithm of the window size, in the range 8..15. window is a caller
6786 supplied buffer of that size. Except for special applications where it is
6787@@ -1040,9 +1061,9 @@ ZEXTERN int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_f
6788 This routine would normally be used in a utility that reads zip or gzip
6789 files and writes out uncompressed files. The utility would decode the
6790 header and process the trailer on its own, hence this routine expects only
6791- the raw deflate stream to decompress. This is different from the normal
6792- behavior of inflate(), which expects either a zlib or gzip header and
6793- trailer around the deflate stream.
6794+ the raw deflate stream to decompress. This is different from the default
6795+ behavior of inflate(), which expects a zlib header and trailer around the
6796+ deflate stream.
6797
6798 inflateBack() uses two subroutines supplied by the caller that are then
6799 called by inflateBack() for input and output. inflateBack() calls those
6800@@ -1051,12 +1072,12 @@ ZEXTERN int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_f
6801 parameters and return types are defined above in the in_func and out_func
6802 typedefs. inflateBack() will call in(in_desc, &buf) which should return the
6803 number of bytes of provided input, and a pointer to that input in buf. If
6804- there is no input available, in() must return zero--buf is ignored in that
6805- case--and inflateBack() will return a buffer error. inflateBack() will call
6806- out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out()
6807- should return zero on success, or non-zero on failure. If out() returns
6808- non-zero, inflateBack() will return with an error. Neither in() nor out()
6809- are permitted to change the contents of the window provided to
6810+ there is no input available, in() must return zero -- buf is ignored in that
6811+ case -- and inflateBack() will return a buffer error. inflateBack() will
6812+ call out(out_desc, buf, len) to write the uncompressed data buf[0..len-1].
6813+ out() should return zero on success, or non-zero on failure. If out()
6814+ returns non-zero, inflateBack() will return with an error. Neither in() nor
6815+ out() are permitted to change the contents of the window provided to
6816 inflateBackInit(), which is also the buffer that out() uses to write from.
6817 The length written by out() will be at most the window size. Any non-zero
6818 amount of input may be provided by in().
6819@@ -1064,8 +1085,8 @@ ZEXTERN int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_f
6820 For convenience, inflateBack() can be provided input on the first call by
6821 setting strm->next_in and strm->avail_in. If that input is exhausted, then
6822 in() will be called. Therefore strm->next_in must be initialized before
6823- calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called
6824- immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in
6825+ calling inflateBack(). If strm->next_in is NULL, then in() will be called
6826+ immediately for input. If strm->next_in is not NULL, then strm->avail_in
6827 must also be initialized, and then if strm->avail_in is not zero, input will
6828 initially be taken from strm->next_in[0 .. strm->avail_in - 1].
6829
6830@@ -1081,10 +1102,10 @@ ZEXTERN int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_f
6831 in the deflate stream (in which case strm->msg is set to indicate the nature
6832 of the error), or Z_STREAM_ERROR if the stream was not properly initialized.
6833 In the case of Z_BUF_ERROR, an input or output error can be distinguished
6834- using strm->next_in which will be Z_NULL only if in() returned an error. If
6835- strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning
6836+ using strm->next_in which will be NULL only if in() returned an error. If
6837+ strm->next_in is not NULL, then the Z_BUF_ERROR was due to out() returning
6838 non-zero. (in() will always be called before out(), so strm->next_in is
6839- assured to be defined if out() returns non-zero.) Note that inflateBack()
6840+ assured to be defined if out() returns non-zero.) Note that inflateBack()
6841 cannot return Z_OK.
6842 */
6843
6844@@ -1106,7 +1127,7 @@ ZEXTERN unsigned long ZEXPORT zlibCompileFlags(void);
6845 7.6: size of z_off_t
6846
6847 Compiler, assembler, and debug options:
6848- 8: DEBUG
6849+ 8: ZLIB_DEBUG
6850 9: ASMV or ASMINF -- use ASM code
6851 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention
6852 11: 0 (reserved)
6853@@ -1192,7 +1213,7 @@ ZEXTERN int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsig
6854 uncompressed data. (The size of the uncompressed data must have been saved
6855 previously by the compressor and transmitted to the decompressor by some
6856 mechanism outside the scope of this compression library.) Upon exit, destLen
6857- is the actual size of the uncompressed buffer.
6858+ is the actual size of the uncompressed data.
6859
6860 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
6861 enough memory, Z_BUF_ERROR if there was not enough room in the output
6862@@ -1201,6 +1222,16 @@ ZEXTERN int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsig
6863 buffer with the uncompressed data up to that point.
6864 */
6865
6866+
6867+ZEXTERN int ZEXPORT uncompress2 (unsigned char *dest, size_t *destLen,
6868+ const unsigned char *source, size_t *sourceLen);
6869+/*
6870+ Same as uncompress, except that sourceLen is a pointer, where the
6871+ length of the source is *sourceLen. On return, *sourceLen is the number of
6872+ source bytes consumed.
6873+*/
6874+
6875+
6876 #ifdef WITH_GZFILEOP
6877 /* gzip file access functions */
6878
6879@@ -1293,10 +1324,12 @@ ZEXTERN int ZEXPORT gzbuffer(gzFile file, unsigned size);
6880 ZEXTERN int ZEXPORT gzsetparams(gzFile file, int level, int strategy);
6881 /*
6882 Dynamically update the compression level or strategy. See the description
6883- of deflateInit2 for the meaning of these parameters.
6884+ of deflateInit2 for the meaning of these parameters. Previously provided
6885+ data is flushed before the parameter change.
6886
6887- gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
6888- opened for writing.
6889+ gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not
6890+ opened for writing, Z_ERRNO if there is an error writing the flushed data,
6891+ or Z_MEM_ERROR if there is a memory allocation error.
6892 */
6893
6894 ZEXTERN int ZEXPORT gzread(gzFile file, void *buf, unsigned len);
6895@@ -1324,7 +1357,32 @@ ZEXTERN int ZEXPORT gzread(gzFile file, void *buf, unsigned len);
6896 case.
6897
6898 gzread returns the number of uncompressed bytes actually read, less than
6899- len for end of file, or -1 for error.
6900+ len for end of file, or -1 for error. If len is too large to fit in an int,
6901+ then nothing is read, -1 is returned, and the error state is set to
6902+ Z_STREAM_ERROR.
6903+*/
6904+
6905+ZEXTERN size_t ZEXPORT gzfread (void *buf, size_t size, size_t nitems, gzFile file);
6906+/*
6907+ Read up to nitems items of size size from file to buf, otherwise operating
6908+ as gzread() does. This duplicates the interface of stdio's fread(), with
6909+ size_t request and return types.
6910+
6911+ gzfread() returns the number of full items read of size size, or zero if
6912+ the end of the file was reached and a full item could not be read, or if
6913+ there was an error. gzerror() must be consulted if zero is returned in
6914+ order to determine if there was an error. If the multiplication of size and
6915+ nitems overflows, i.e. the product does not fit in a size_t, then nothing
6916+ is read, zero is returned, and the error state is set to Z_STREAM_ERROR.
6917+
6918+ In the event that the end of file is reached and only a partial item is
6919+ available at the end, i.e. the remaining uncompressed data length is not a
6920+ multiple of size, then the final partial item is nevertheless read into buf
6921+ and the end-of-file flag is set. The length of the partial item read is not
6922+ provided, but could be inferred from the result of gztell(). This behavior
6923+ is the same as the behavior of fread() implementations in common libraries,
6924+ but it prevents the direct use of gzfread() to read a concurrently written
6925+ file, reseting and retrying on end-of-file, when size is not 1.
6926 */
6927
6928 ZEXTERN int ZEXPORT gzwrite(gzFile file, void const *buf, unsigned len);
6929@@ -1334,19 +1392,31 @@ ZEXTERN int ZEXPORT gzwrite(gzFile file, void const *buf, unsigned len);
6930 error.
6931 */
6932
6933+ZEXTERN size_t ZEXPORT gzfwrite(void const *buf, size_t size,
6934+ size_t nitems, gzFile file);
6935+/*
6936+ gzfwrite() writes nitems items of size size from buf to file, duplicating
6937+ the interface of stdio's fwrite(), with size_t request and return types.
6938+
6939+ gzfwrite() returns the number of full items written of size size, or zero
6940+ if there was an error. If the multiplication of size and nitems overflows,
6941+ i.e. the product does not fit in a size_t, then nothing is written, zero
6942+ is returned, and the error state is set to Z_STREAM_ERROR.
6943+*/
6944+
6945 ZEXTERN int ZEXPORTVA gzprintf(gzFile file, const char *format, ...);
6946 /*
6947 Converts, formats, and writes the arguments to the compressed file under
6948 control of the format string, as in fprintf. gzprintf returns the number of
6949- uncompressed bytes actually written, or 0 in case of error. The number of
6950- uncompressed bytes written is limited to 8191, or one less than the buffer
6951- size given to gzbuffer(). The caller should assure that this limit is not
6952- exceeded. If it is exceeded, then gzprintf() will return an error (0) with
6953- nothing written. In this case, there may also be a buffer overflow with
6954- unpredictable consequences, which is possible only if zlib was compiled with
6955- the insecure functions sprintf() or vsprintf() because the secure snprintf()
6956- or vsnprintf() functions were not available. This can be determined using
6957- zlibCompileFlags().
6958+ uncompressed bytes actually written, or a negative zlib error code in case
6959+ of error. The number of uncompressed bytes written is limited to 8191, or
6960+ one less than the buffer size given to gzbuffer(). The caller should assure
6961+ that this limit is not exceeded. If it is exceeded, then gzprintf() will
6962+ return an error (0) with nothing written. In this case, there may also be a
6963+ buffer overflow with unpredictable consequences, which is possible only if
6964+ zlib was compiled with the insecure functions sprintf() or vsprintf()
6965+ because the secure snprintf() or vsnprintf() functions were not available.
6966+ This can be determined using zlibCompileFlags().
6967 */
6968
6969 ZEXTERN int ZEXPORT gzputs(gzFile file, const char *s);
6970@@ -1406,7 +1476,7 @@ ZEXTERN int ZEXPORT gzflush(gzFile file, int flush);
6971 If the flush parameter is Z_FINISH, the remaining data is written and the
6972 gzip stream is completed in the output. If gzwrite() is called again, a new
6973 gzip stream will be started in the output. gzread() is able to read such
6974- concatented gzip streams.
6975+ concatenated gzip streams.
6976
6977 gzflush should be called only when strictly necessary because it will
6978 degrade compression if called too often.
6979@@ -1556,15 +1626,15 @@ ZEXTERN void ZEXPORT gzclearerr(gzFile file);
6980 ZEXTERN uint32_t ZEXPORT adler32(uint32_t adler, const unsigned char *buf, uint32_t len);
6981 /*
6982 Update a running Adler-32 checksum with the bytes buf[0..len-1] and
6983- return the updated checksum. If buf is Z_NULL, this function returns the
6984+ return the updated checksum. If buf is NULL, this function returns the
6985 required initial value for the checksum.
6986
6987- An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
6988+ An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed
6989 much faster.
6990
6991 Usage example:
6992
6993- uint32_t adler = adler32(0L, Z_NULL, 0);
6994+ uint32_t adler = adler32(0L, NULL, 0);
6995
6996 while (read_buffer(buffer, length) != EOF) {
6997 adler = adler32(adler, buffer, length);
6998@@ -1586,13 +1656,13 @@ ZEXTERN uint32_t ZEXPORT adler32_combine(uint32_t adler1, uint32_t adler2, z_off
6999 ZEXTERN uint32_t ZEXPORT crc32(uint32_t crc, const unsigned char *buf, z_off64_t len);
7000 /*
7001 Update a running CRC-32 with the bytes buf[0..len-1] and return the
7002- updated CRC-32. If buf is Z_NULL, this function returns the required
7003+ updated CRC-32. If buf is NULL, this function returns the required
7004 initial value for the crc. Pre- and post-conditioning (one's complement) is
7005 performed within this function so it shouldn't be done by the application.
7006
7007 Usage example:
7008
7009- uint32_t crc = crc32(0L, Z_NULL, 0);
7010+ uint32_t crc = crc32(0L, NULL, 0);
7011
7012 while (read_buffer(buffer, length) != EOF) {
7013 crc = crc32(crc, buffer, length);
7014@@ -1647,7 +1717,7 @@ struct gzFile_s {
7015 z_off64_t pos;
7016 };
7017 ZEXTERN int ZEXPORT gzgetc_(gzFile file); /* backward compatibility */
7018-# define gzgetc(g) ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : gzgetc(g))
7019+# define gzgetc(g) ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g))
7020
7021 /* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or
7022 * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if
7023@@ -1711,6 +1781,7 @@ ZEXTERN const char * ZEXPORT zError (int);
7024 ZEXTERN int ZEXPORT inflateSyncPoint (z_stream *);
7025 ZEXTERN const uint32_t * ZEXPORT get_crc_table (void);
7026 ZEXTERN int ZEXPORT inflateUndermine (z_stream *, int);
7027+ZEXTERN int ZEXPORT inflateValidate (z_stream *, int);
7028 ZEXTERN int ZEXPORT inflateResetKeep (z_stream *);
7029 ZEXTERN int ZEXPORT deflateResetKeep (z_stream *);
7030
7031diff --git a/zutil.c b/zutil.c
7032index e46277b..1faf600 100644
7033--- a/zutil.c
7034+++ b/zutil.c
7035@@ -59,7 +59,7 @@ unsigned long ZEXPORT zlibCompileFlags(void)
7036 case 8: flags += 2 << 6; break;
7037 default: flags += 3 << 6;
7038 }
7039-#ifdef DEBUG
7040+#ifdef ZLIB_DEBUG
7041 flags += 1 << 8;
7042 #endif
7043 #ifdef ZLIB_WINAPI
7044@@ -83,7 +83,7 @@ unsigned long ZEXPORT zlibCompileFlags(void)
7045 return flags;
7046 }
7047
7048-#ifdef DEBUG
7049+#ifdef ZLIB_DEBUG
7050
7051 # ifndef verbose
7052 # define verbose 0
7053diff --git a/zutil.h b/zutil.h
7054index b6e858c..088c362 100644
7055--- a/zutil.h
7056+++ b/zutil.h
7057@@ -26,11 +26,6 @@
7058 #include <stdint.h>
7059 #include "zlib.h"
7060
7061-#ifndef local
7062-# define local static
7063-#endif
7064-/* compile with -Dlocal if your debugger can't find static symbols */
7065-
7066 typedef unsigned char uch; /* Included for compatibility with external code only */
7067 typedef uint16_t ush; /* Included for compatibility with external code only */
7068 typedef unsigned long ulg;
7069@@ -70,14 +65,101 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
7070
7071 /* target dependencies */
7072
7073-#ifdef WIN32
7074-# ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */
7075-# define OS_CODE 0x0b
7076+#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
7077+# define OS_CODE 0x00
7078+# ifndef Z_SOLO
7079+# if defined(__TURBOC__) || defined(__BORLANDC__)
7080+# if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
7081+ /* Allow compilation with ANSI keywords only enabled */
7082+ void _Cdecl farfree( void *block );
7083+ void *_Cdecl farmalloc( unsigned long nbytes );
7084+# else
7085+# include <alloc.h>
7086+# endif
7087+# else /* MSC or DJGPP */
7088+# include <malloc.h>
7089+# endif
7090+# endif
7091+#endif
7092+
7093+#ifdef AMIGA
7094+# define OS_CODE 1
7095+#endif
7096+
7097+#if defined(VAXC) || defined(VMS)
7098+# define OS_CODE 2
7099+# define F_OPEN(name, mode) \
7100+ fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
7101+#endif
7102+
7103+#ifdef __370__
7104+# if __TARGET_LIB__ < 0x20000000
7105+# define OS_CODE 4
7106+# elif __TARGET_LIB__ < 0x40000000
7107+# define OS_CODE 11
7108+# else
7109+# define OS_CODE 8
7110+# endif
7111+#endif
7112+
7113+#if defined(ATARI) || defined(atarist)
7114+# define OS_CODE 5
7115+#endif
7116+
7117+#ifdef OS2
7118+# define OS_CODE 6
7119+# if defined(M_I86) && !defined(Z_SOLO)
7120+# include <malloc.h>
7121+# endif
7122+#endif
7123+
7124+#if defined(MACOS) || defined(TARGET_OS_MAC)
7125+# define OS_CODE 7
7126+# ifndef Z_SOLO
7127+# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
7128+# include <unix.h> /* for fdopen */
7129+# else
7130+# ifndef fdopen
7131+# define fdopen(fd,mode) NULL /* No fdopen() */
7132+# endif
7133+# endif
7134 # endif
7135 #endif
7136
7137-#if (defined(_MSC_VER) && (_MSC_VER > 600))
7138-# define fdopen(fd, type) _fdopen(fd, type)
7139+#ifdef __acorn
7140+# define OS_CODE 13
7141+#endif
7142+
7143+#if defined(WIN32) && !defined(__CYGWIN__)
7144+# define OS_CODE 10
7145+#endif
7146+
7147+#ifdef _BEOS_
7148+# define OS_CODE 16
7149+#endif
7150+
7151+#ifdef __TOS_OS400__
7152+# define OS_CODE 18
7153+#endif
7154+
7155+#ifdef __APPLE__
7156+# define OS_CODE 19
7157+#endif
7158+
7159+#if defined(_BEOS_) || defined(RISCOS)
7160+# define fdopen(fd,mode) NULL /* No fdopen() */
7161+#endif
7162+
7163+#if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX
7164+# if defined(_WIN32_WCE)
7165+# define fdopen(fd,mode) NULL /* No fdopen() */
7166+# ifndef _PTRDIFF_T_DEFINED
7167+ typedef int ptrdiff_t;
7168+# define _PTRDIFF_T_DEFINED
7169+# endif
7170+# else
7171+# define fdopen(fd,type) _fdopen(fd,type)
7172+# endif
7173 #endif
7174
7175 /* provide prototypes for these when building zlib without LFS */
7176@@ -95,7 +177,7 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
7177 /* common defaults */
7178
7179 #ifndef OS_CODE
7180-# define OS_CODE 0x03 /* assume Unix */
7181+# define OS_CODE 3 /* assume Unix */
7182 #endif
7183
7184 #ifndef F_OPEN
7185@@ -105,7 +187,7 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
7186 /* functions */
7187
7188 /* Diagnostic functions */
7189-#ifdef DEBUG
7190+#ifdef ZLIB_DEBUG
7191 # include <stdio.h>
7192 extern int ZLIB_INTERNAL z_verbose;
7193 extern void ZLIB_INTERNAL z_error(char *m);