Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/.gitattributes b/.gitattributes
- new file mode 100644
- index 0000000..68ec3a1
- --- /dev/null
- +++ b/.gitattributes
- @@ -0,0 +1,4 @@
- +* text=auto
- +*.c text
- +*.h text
- +Makefile text
- diff --git a/.gitignore b/.gitignore
- index 0beb44b..a7f2c3c 100644
- --- a/.gitignore
- +++ b/.gitignore
- @@ -21,7 +21,6 @@
- /minigzip64
- /minigzipsh
- /zlib.pc
- -/CVE-2003-0107
- .DS_Store
- *.obj
- @@ -33,6 +32,12 @@
- *.res
- foo.gz
- *.manifest
- +*.opensdf
- +*.sln
- +*.sdf
- +*.vcxproj
- +*.vcxproj.filters
- +.vs
- CMakeCache.txt
- CMakeFiles
- @@ -47,6 +52,16 @@ ztest*
- configure.log
- a.out
- +/Makefile
- /arch/arm/Makefile
- /arch/generic/Makefile
- /arch/x86/Makefile
- +.kdev4
- +*.kdev4
- +
- +/Debug
- +/example.dir
- +/minigzip.dir
- +/zlib.dir
- +/zlibstatic.dir
- +/win32/Debug
- diff --git a/.travis.yml b/.travis.yml
- index 6080169..264d97d 100644
- --- a/.travis.yml
- +++ b/.travis.yml
- @@ -3,10 +3,15 @@ compiler:
- - gcc
- - clang
- env:
- + - BUILDDIR=. TOOL="./configure"
- + - BUILDDIR=. TOOL="./configure --native"
- - BUILDDIR=. TOOL="./configure --zlib-compat"
- - - BUILDDIR=../build TOOL="../zlib-ng/configure --zlib-compat"
- - BUILDDIR=. TOOL="./configure --zlib-compat --without-optimizations --without-new-strategies"
- + - BUILDDIR=../build TOOL="../zlib-ng/configure"
- - BUILDDIR=. TOOL="cmake ."
- + - BUILDDIR=. TOOL="cmake -DWITH_NATIVE_INSTRUCTIONS=on ."
- + - BUILDDIR=. TOOL="cmake -DZLIB_COMPAT=on ."
- + - BUILDDIR=. TOOL="cmake -DZLIB_COMPAT=on -DWITH_OPTIM=off -DWITH_NEW_STRATEGIES=off ."
- - BUILDDIR=../build TOOL="cmake ../zlib-ng"
- script: mkdir -p $BUILDDIR && cd $BUILDDIR &&
- $TOOL && make && make test
- diff --git a/CMakeLists.txt b/CMakeLists.txt
- index b102498..0bb530c 100644
- --- a/CMakeLists.txt
- +++ b/CMakeLists.txt
- @@ -1,6 +1,5 @@
- -cmake_minimum_required(VERSION 2.4.4)
- +cmake_minimum_required(VERSION 2.8.4)
- set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
- -set(CMAKE_LEGACY_CYGWIN_WIN32 0) # Remove when CMake >= 2.8.4 is required
- project(zlib C)
- @@ -18,6 +17,7 @@ include(CheckFunctionExists)
- include(CheckIncludeFile)
- include(CheckCSourceCompiles)
- include(CheckCSourceRuns)
- +include(CheckLibraryExists)
- include(FeatureSummary)
- # make sure we use an appropriate BUILD_TYPE by default, "Release" to be exact
- @@ -41,24 +41,28 @@ check_include_file(stddef.h HAVE_STDDEF_H)
- # Options parsing
- #
- set(ARCH ${CMAKE_HOST_SYSTEM_PROCESSOR})
- +message(STATUS "Architecture: ${ARCH}")
- option (ZLIB_COMPAT "Compile with zlib compatible API" OFF)
- if (ZLIB_COMPAT)
- - message(STATUS "ZLIB_COMPAT")
- add_definitions(-DZLIB_COMPAT)
- set (WITH_GZFILEOP ON)
- endif (ZLIB_COMPAT)
- option (WITH_GZFILEOP "Compile with support for gzFile related functions" OFF)
- if (WITH_GZFILEOP)
- - message(STATUS "WITH_GZFILEOP")
- add_definitions(-DWITH_GZFILEOP)
- endif (WITH_GZFILEOP)
- option(WITH_OPTIM "Build with optimisation" ON)
- option(WITH_NEW_STRATEGIES "Use new strategies" ON)
- +option(WITH_NATIVE_INSTRUCTIONS
- + "Instruct the compiler to use the full instruction set on this host (gcc/clang -march=native)" OFF)
- if(${CMAKE_C_COMPILER} MATCHES "icc" OR ${CMAKE_C_COMPILER} MATCHES "icpc" OR ${CMAKE_C_COMPILER} MATCHES "icl")
- + if(WITH_NATIVE_INSTRUCTIONS)
- + message(STATUS "Ignoring WITH_NATIVE_INSTRUCTIONS; not supported on this configuration")
- + endif()
- if(CMAKE_HOST_UNIX)
- if(NOT SSE2FLAG)
- set(SSE2FLAG "-msse2")
- @@ -82,26 +86,42 @@ elseif(MSVC)
- if(NOT ${ARCH} MATCHES "AMD64")
- set(SSE2FLAG "/arch:SSE2")
- endif()
- + if(WITH_NATIVE_INSTRUCTIONS)
- + message(STATUS "Ignoring WITH_NATIVE_INSTRUCTIONS; not supported on this configuration")
- + endif()
- else()
- execute_process(COMMAND ${CMAKE_C_COMPILER} --version OUTPUT_VARIABLE COMPILER_VERSION)
- if("${COMPILER_VERSION}" MATCHES "gcc" OR "${COMPILER_VERSION}" MATCHES "clang")
- set(__GNUC__ ON)
- endif()
- - if(NOT SSE2FLAG)
- + if(WITH_NATIVE_INSTRUCTIONS)
- if(__GNUC__)
- - set(SSE2FLAG "-msse2")
- + set(NATIVEFLAG "-march=native")
- + else()
- + message(STATUS "Ignoring WITH_NATIVE_INSTRUCTIONS; not implemented yet on this configuration")
- endif()
- endif()
- - if(NOT SSE4FLAG)
- - if(__GNUC__)
- - set(SSE4FLAG "-msse4")
- + if(NOT NATIVEFLAG)
- + if(NOT SSE2FLAG)
- + if(__GNUC__)
- + set(SSE2FLAG "-msse2")
- + endif()
- endif()
- - endif()
- - if(NOT PCLMULFLAG)
- - if(__GNUC__)
- - set(PCLMULFLAG "-mpclmul")
- - endif()
- - endif()
- + if(NOT SSE4FLAG)
- + if(__GNUC__)
- + set(SSE4FLAG "-msse4")
- + endif()
- + endif()
- + if(NOT PCLMULFLAG)
- + if(__GNUC__)
- + set(PCLMULFLAG "-mpclmul")
- + endif()
- + endif()
- + else(NOT NATIVEFLAG)
- + set(SSE2FLAG ${NATIVEFLAG})
- + set(SSE4FLAG ${NATIVEFLAG})
- + set(PCLMULFLAG ${NATIVEFLAG})
- + endif(NOT NATIVEFLAG)
- endif()
- add_feature_info(ZLIB_COMPAT ZLIB_COMPAT "Provide a zlib-compatible API")
- @@ -201,6 +221,8 @@ if(HAVE_BUILTIN_CTZL)
- add_definitions(-DHAVE_BUILTIN_CTZL)
- endif()
- +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DZLIB_DEBUG")
- +
- if(MSVC)
- set(CMAKE_DEBUG_POSTFIX "d")
- add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
- @@ -210,7 +232,11 @@ else()
- #
- # not MSVC, so we need to check if we have the MS-style SSE etc. intrinsics
- #
- - set(CMAKE_REQUIRED_FLAGS "${SSE2FLAG}")
- + if(WITH_NATIVE_INSTRUCTIONS)
- + set(CMAKE_REQUIRED_FLAGS "${NATIVEFLAG}")
- + else()
- + set(CMAKE_REQUIRED_FLAGS "${SSE2FLAG}")
- + endif()
- check_c_source_runs(
- "#include <immintrin.h>
- int main(void)
- @@ -221,8 +247,12 @@ else()
- }"
- HAVE_SSE2_INTRIN
- )
- - # use the generic SSE4 enabler option to check for the SSE4.2 instruction we require:
- - set(CMAKE_REQUIRED_FLAGS "${SSE4FLAG}")
- + if(WITH_NATIVE_INSTRUCTIONS)
- + set(CMAKE_REQUIRED_FLAGS "${NATIVEFLAG}")
- + else()
- + # use the generic SSE4 enabler option to check for the SSE4.2 instruction we require:
- + set(CMAKE_REQUIRED_FLAGS "${SSE4FLAG}")
- + endif()
- check_c_source_runs(
- "int main(void)
- {
- @@ -232,8 +262,12 @@ else()
- }"
- HAVE_SSE42_INTRIN
- )
- - # the PCLMUL instruction we use also requires an SSE4.1 instruction check for both
- - set(CMAKE_REQUIRED_FLAGS "${SSE4FLAG} ${PCLMULFLAG}")
- + if(WITH_NATIVE_INSTRUCTIONS)
- + set(CMAKE_REQUIRED_FLAGS "${NATIVEFLAG}")
- + else()
- + # the PCLMUL instruction we use also requires an SSE4.1 instruction check for both
- + set(CMAKE_REQUIRED_FLAGS "${SSE4FLAG} ${PCLMULFLAG}")
- + endif()
- check_c_source_runs(
- "#include <immintrin.h>
- #include <smmintrin.h>
- @@ -251,7 +285,7 @@ else()
- endif()
- #
- -# Enable deflate_medium
- +# Enable deflate_medium at level 4-5
- #
- if(WITH_NEW_STRATEGIES)
- add_definitions(-DMEDIUM_STRATEGY)
- @@ -265,8 +299,14 @@ endif()
- # using ${flag} globally.
- #
- macro(add_intrinsics_option flag)
- - if (NOT "${CMAKE_C_FLAGS} " MATCHES ".*${flag} .*")
- - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
- + if(WITH_NATIVE_INSTRUCTIONS AND NATIVEFLAG)
- + if (NOT "${CMAKE_C_FLAGS} " MATCHES ".*${NATIVEFLAG} .*")
- + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${NATIVEFLAG}")
- + endif()
- + else()
- + if (NOT "${CMAKE_C_FLAGS} " MATCHES ".*${flag} .*")
- + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
- + endif()
- endif()
- endmacro(add_intrinsics_option)
- @@ -284,7 +324,6 @@ else()
- add_definitions(-DX86 -DUNALIGNED_OK -DUNROLL_LESS)
- add_feature_info(SSE2 1 "Support the SSE2 instruction set, using \"${SSE2FLAG}\"")
- endif()
- -
- if("${ARCHDIR}" MATCHES "arch/x86" AND WITH_OPTIM)
- add_definitions("-DX86_CPUID")
- set(ZLIB_ARCH_SRCS ${ZLIB_ARCH_SRCS} ${ARCHDIR}/x86.c)
- diff --git a/ChangeLog.zlib b/ChangeLog.zlib
- index 909d11f..2adb468 100644
- --- a/ChangeLog.zlib
- +++ b/ChangeLog.zlib
- @@ -200,7 +200,7 @@ Changes in 1.2.5.2 (17 Dec 2011)
- - Add a transparent write mode to gzopen() when 'T' is in the mode
- - Update python link in zlib man page
- - Get inffixed.h and MAKEFIXED result to match
- -- Add a ./config --solo option to make zlib subset with no libary use
- +- Add a ./config --solo option to make zlib subset with no library use
- - Add undocumented inflateResetKeep() function for CAB file decoding
- - Add --cover option to ./configure for gcc coverage testing
- - Add #define ZLIB_CONST option to use const in the z_stream interface
- @@ -1273,7 +1273,7 @@ Changes in 1.0.1 (20 May 96) [1.0 skipped to avoid confusion]
- - fix array overlay in deflate.c which sometimes caused bad compressed data
- - fix inflate bug with empty stored block
- - fix MSDOS medium model which was broken in 0.99
- -- fix deflateParams() which could generated bad compressed data.
- +- fix deflateParams() which could generate bad compressed data.
- - Bytef is define'd instead of typedef'ed (work around Borland bug)
- - added an INDEX file
- - new makefiles for DJGPP (Makefile.dj2), 32-bit Borland (Makefile.b32),
- diff --git a/Makefile b/Makefile
- deleted file mode 100644
- index 6bba86c..0000000
- --- a/Makefile
- +++ /dev/null
- @@ -1,5 +0,0 @@
- -all:
- - -@echo "Please use ./configure first. Thank you."
- -
- -distclean:
- - make -f Makefile.in distclean
- diff --git a/Makefile.in b/Makefile.in
- index c69175e..0ecafdf 100644
- --- a/Makefile.in
- +++ b/Makefile.in
- @@ -16,14 +16,15 @@ CC=cc
- CFLAGS=-O
- #CFLAGS=-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7
- -#CFLAGS=-g -DDEBUG
- +#CFLAGS=-g -DZLIB_DEBUG
- #CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \
- # -Wstrict-prototypes -Wmissing-prototypes
- SFLAGS=-O
- -LDFLAGS=
- -TEST_LDFLAGS=-L. libz.a
- +LDFLAGS=-L.
- +TEST_LIBS=libz.a
- LDSHARED=$(CC)
- +LDSHAREDFLAGS=-shared
- STATICLIB=libz.a
- SHAREDLIB=libz.so
- @@ -87,6 +88,8 @@ all64: example64$(EXE) minigzip64$(EXE)
- check: test
- +.SECONDARY:
- +
- $(ARCHDIR)/%.o: $(SRCDIR)/$(ARCHDIR)/%.c
- $(MAKE) -C $(ARCHDIR) $(notdir $@)
- @@ -99,14 +102,44 @@ $(ARCHDIR)/%.lo: $(SRCDIR)/$(ARCHDIR)/%.c
- %.lo: $(ARCHDIR)/%.lo
- -cp $< $@
- -test: all
- - $(MAKE) -C test
- +test: all teststatic testshared
- +
- +teststatic: static
- + @TMPST=tmpst_$$; \
- + if echo hello world | ./minigzip | ./minigzip -d && ./example $$TMPST ; then \
- + echo ' *** zlib test OK ***'; \
- + else \
- + echo ' *** zlib test FAILED ***'; false; \
- + fi; \
- + rm -f $$TMPST
- +
- +testshared: shared
- + @LD_LIBRARY_PATH=`pwd`:$(LD_LIBRARY_PATH) ; export LD_LIBRARY_PATH; \
- + LD_LIBRARYN32_PATH=`pwd`:$(LD_LIBRARYN32_PATH) ; export LD_LIBRARYN32_PATH; \
- + DYLD_LIBRARY_PATH=`pwd`:$(DYLD_LIBRARY_PATH) ; export DYLD_LIBRARY_PATH; \
- + SHLIB_PATH=`pwd`:$(SHLIB_PATH) ; export SHLIB_PATH; \
- + TMPSH=tmpsh_$$; \
- + if echo hello world | ./minigzipsh | ./minigzipsh -d && ./examplesh $$TMPSH; then \
- + echo ' *** zlib shared test OK ***'; \
- + else \
- + echo ' *** zlib shared test FAILED ***'; false; \
- + fi; \
- + rm -f $$TMPSH
- +
- +test64: all64
- + @TMP64=tmp64_$$; \
- + if echo hello world | ./minigzip64 | ./minigzip64 -d && ./example64 $$TMP64; then \
- + echo ' *** zlib 64-bit test OK ***'; \
- + else \
- + echo ' *** zlib 64-bit test FAILED ***'; false; \
- + fi; \
- + rm -f $$TMP64
- infcover.o: $(SRCDIR)/test/infcover.c $(SRCDIR)/zlib.h zconf.h
- $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/test/infcover.c
- infcover$(EXE): infcover.o $(STATICLIB)
- - $(CC) $(CFLAGS) -o $@ infcover.o $(STATICLIB)
- + $(CC) $(LDFLAGS) -o $@ infcover.o $(STATICLIB)
- ifneq ($(STRIP),)
- $(STRIP) $@
- endif
- @@ -127,10 +160,10 @@ minigzip.o: $(SRCDIR)/test/minigzip.c $(SRCDIR)/zlib.h zconf.h
- $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/test/minigzip.c
- example64.o: $(SRCDIR)/test/example.c $(SRCDIR)/zlib.h zconf.h
- - $(CC) $(CFLAGS) $(INCLUDES) -D_FILE_OFFSET_BITS=64 -c -o $@ $(SRCDIR)/test/example.c
- + $(CC) $(CFLAGS) -D_FILE_OFFSET_BITS=64 $(INCLUDES) -c -o $@ $(SRCDIR)/test/example.c
- minigzip64.o: $(SRCDIR)/test/minigzip.c $(SRCDIR)/zlib.h zconf.h
- - $(CC) $(CFLAGS) $(INCLUDES) -D_FILE_OFFSET_BITS=64 -c -o $@ $(SRCDIR)/test/minigzip.c
- + $(CC) $(CFLAGS) -D_FILE_OFFSET_BITS=64 $(INCLUDES) -c -o $@ $(SRCDIR)/test/minigzip.c
- zlibrc.o: win32/zlib1.rc
- $(RC) $(RCFLAGS) -o $@ win32/zlib1.rc
- @@ -138,14 +171,14 @@ zlibrc.o: win32/zlib1.rc
- .SUFFIXES: .lo
- %.o: $(SRCDIR)/%.c
- - $(CC) $(INCLUDES) $(CFLAGS) -c -o $@ $<
- + $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
- %.lo: $(SRCDIR)/%.c
- - $(CC) $(INCLUDES) $(SFLAGS) -DPIC -c -o $@ $<
- + $(CC) $(SFLAGS) -DPIC $(INCLUDES) -c -o $@ $<
- $(SHAREDTARGET): $(PIC_OBJS) $(DEFFILE) $(RCOBJS)
- ifneq ($(SHAREDTARGET),)
- - $(LDSHARED) $(SFLAGS) -o $@ $(DEFFILE) $(PIC_OBJS) $(RCOBJS) $(LDSHAREDLIBC) $(LDFLAGS)
- + $(LDSHARED) $(LDSHAREDFLAGS) $(LDFLAGS) -o $@ $(DEFFILE) $(PIC_OBJS) $(RCOBJS) $(LDSHAREDLIBC)
- ifneq ($(STRIP),)
- $(STRIP) $@
- endif
- @@ -157,37 +190,37 @@ endif
- endif
- example$(EXE): example.o $(STATICLIB)
- - $(CC) $(CFLAGS) -o $@ example.o $(TEST_LDFLAGS)
- + $(CC) $(LDFLAGS) -o $@ example.o $(TEST_LIBS)
- ifneq ($(STRIP),)
- $(STRIP) $@
- endif
- minigzip$(EXE): minigzip.o $(STATICLIB)
- - $(CC) $(CFLAGS) -o $@ minigzip.o $(TEST_LDFLAGS)
- + $(CC) $(LDFLAGS) -o $@ minigzip.o $(TEST_LIBS)
- ifneq ($(STRIP),)
- $(STRIP) $@
- endif
- examplesh$(EXE): example.o $(SHAREDTARGET)
- - $(CC) $(CFLAGS) -o $@ example.o -L. $(SHAREDTARGET)
- + $(CC) $(LDFLAGS) -o $@ example.o $(SHAREDTARGET)
- ifneq ($(STRIP),)
- $(STRIP) $@
- endif
- minigzipsh$(EXE): minigzip.o $(SHAREDTARGET)
- - $(CC) $(CFLAGS) -o $@ minigzip.o -L. $(SHAREDTARGET)
- + $(CC) $(LDFLAGS) -o $@ minigzip.o $(SHAREDTARGET)
- ifneq ($(STRIP),)
- $(STRIP) $@
- endif
- example64$(EXE): example64.o $(STATICLIB)
- - $(CC) $(CFLAGS) -o $@ example64.o $(TEST_LDFLAGS)
- + $(CC) $(LDFLAGS) -o $@ example64.o $(TEST_LIBS)
- ifneq ($(STRIP),)
- $(STRIP) $@
- endif
- minigzip64$(EXE): minigzip64.o $(STATICLIB)
- - $(CC) $(CFLAGS) -o $@ minigzip64.o $(TEST_LDFLAGS)
- + $(CC) $(LDFLAGS) -o $@ minigzip64.o $(TEST_LIBS)
- ifneq ($(STRIP),)
- $(STRIP) $@
- endif
- @@ -254,7 +287,6 @@ zlib.3.pdf: $(SRCDIR)/zlib.3
- mostlyclean: clean
- clean:
- @if [ -f $(ARCHDIR)/Makefile ]; then $(MAKE) -C $(ARCHDIR) clean; fi
- - @if [ -f test/Makefile ]; then $(MAKE) -C test clean; fi
- rm -f *.o *.lo *~ \
- example$(EXE) minigzip$(EXE) examplesh$(EXE) minigzipsh$(EXE) \
- example64$(EXE) minigzip64$(EXE) \
- @@ -264,66 +296,71 @@ clean:
- _match.s maketree
- rm -rf objs
- rm -f *.gcda *.gcno *.gcov
- - rm -f a.out
- + rm -f a.out a.exe
- maintainer-clean: distclean
- distclean: clean
- @if [ -f $(ARCHDIR)/Makefile ]; then $(MAKE) -C $(ARCHDIR) distclean; fi
- - @if [ -f test/Makefile ]; then $(MAKE) -C test distclean; fi
- - rm -f zlib.pc configure.log zconf.h zconf.h.cmakein
- + rm -f Makefile zlib.pc configure.log zconf.h zconf.h.cmakein
- -@rm -f .DS_Store
- -# Reset Makefile if building inside source tree
- - @if [ -f Makefile.in ]; then \
- - printf 'all:\n\t-@echo "Please use ./configure first. Thank you."\n' > Makefile ; \
- - printf '\ndistclean:\n\tmake -f Makefile.in distclean\n' >> Makefile ; \
- - touch -r $(SRCDIR)/Makefile.in Makefile ; fi
- # Reset zconf.h and zconf.h.cmakein if building inside source tree
- @if [ -f zconf.h.in ]; then \
- cp -p $(SRCDIR)/zconf.h.in zconf.h ; \
- TEMPFILE=zconfh_$$ ; \
- - echo "/#define ZCONF_H/ a\\\\\n#cmakedefine Z_HAVE_UNISTD_H\n" >> $$TEMPFILE &&\
- + echo "/#define ZCONF_H/ a\\\n#cmakedefine Z_HAVE_UNISTD_H\\n#cmakedefine Z_HAVE_STDARG_H\n" >> $$TEMPFILE &&\
- sed -f $$TEMPFILE $(SRCDIR)/zconf.h.in > zconf.h.cmakein &&\
- touch -r $(SRCDIR)/zconf.h.in zconf.h.cmakein &&\
- rm $$TEMPFILE ; fi
- # Cleanup these files if building outside source tree
- - @if [ ! -f zlib.3 ]; then rm -f zlib.3.pdf Makefile; fi
- -# Remove arch and test directory if building outside source tree
- + @if [ ! -f zlib.3 ]; then rm -f zlib.3.pdf; fi
- +# Remove arch directory if building outside source tree
- @if [ ! -f $(ARCHDIR)/Makefile.in ]; then rm -rf arch; fi
- - @if [ ! -f test/Makefile.in ]; then rm -rf test; fi
- tags:
- etags $(SRCDIR)/*.[ch]
- depend:
- - makedepend -- $(CFLAGS) -- $(SRCDIR)/*.[ch]
- - makedepend -a -o.lo -- $(SFLAGS) -- $(SRCDIR)/*.[ch]
- - @sed "s=^$(SRCDIR)/\([a-zA-Z0-9_]*\.\(lo\|o\):\)==g" < Makefile > Makefile.tmp
- + makedepend -Y -- $(CFLAGS) -- $(SRCDIR)/*.c $(SRCDIR)/$(ARCHDIR)/*.c
- + makedepend -Y -a -o.lo -- $(SFLAGS) -- $(SRCDIR)/*.c $(SRCDIR)/$(ARCHDIR)/*.c
- + @sed "s=^$(SRCDIR)/\($(ARCHDIR)/\)\?\([a-zA-Z0-9_]*\.\(lo\|o\):\)=\2=g" < Makefile > Makefile.tmp
- @mv -f Makefile.tmp Makefile
- # DO NOT DELETE THIS LINE -- make depend depends on it.
- -adler32.o zutil.o: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- +adler32.o: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- gzclose.o gzlib.o gzread.o gzwrite.o: $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/gzguts.h
- compress.o example.o minigzip.o uncompr.o: $(SRCDIR)/zlib.h zconf.h
- crc32.o: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/crc32.h
- deflate.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- -deflate_fast.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h
- -deflate_medium.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h
- -deflate_slow.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h
- +deflate_fast.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- +deflate_medium.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- +deflate_slow.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- infback.o inflate.o: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/inftrees.h $(SRCDIR)/inflate.h $(SRCDIR)/inffast.h $(SRCDIR)/inffixed.h
- inffast.o: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/inftrees.h $(SRCDIR)/inflate.h $(SRCDIR)/inffast.h
- inftrees.o: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/inftrees.h
- trees.o: $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/trees.h
- -
- -adler32.lo zutil.lo: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- +zutil.o: $(SRCDIR)/zutil.h $(SRCDIR)/gzguts.h $(SRCDIR)/zlib.h zconf.h
- +arch/x86/crc_folding.o: $(SRCDIR)/arch/x86/crc_folding.h $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- +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
- +arch/x86/deflate_quick.o: $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- +arch/x86/fill_window_sse.o: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- +arch/x86/x86.o: $(SRCDIR)/arch/x86/x86.h
- +
- +adler32.lo: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- gzclose.lo gzlib.lo gzread.lo gzwrite.lo: $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/gzguts.h
- compress.lo example.lo minigzip.lo uncompr.lo: $(SRCDIR)/zlib.h zconf.h
- crc32.lo: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/crc32.h
- deflate.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- -deflate_fast.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h
- -deflate_medium.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h
- -deflate_slow.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h
- +deflate_fast.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- +deflate_medium.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- +deflate_slow.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/match.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- infback.lo inflate.lo: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/inftrees.h $(SRCDIR)/inflate.h $(SRCDIR)/inffast.h $(SRCDIR)/inffixed.h
- inffast.lo: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/inftrees.h $(SRCDIR)/inflate.h $(SRCDIR)/inffast.h
- inftrees.lo: $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/inftrees.h
- trees.lo: $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h $(SRCDIR)/trees.h
- +zutil.lo: $(SRCDIR)/zutil.h $(SRCDIR)/gzguts.h $(SRCDIR)/zlib.h zconf.h
- +arch/x86/crc_folding.lo: $(SRCDIR)/arch/x86/crc_folding.h $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- +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
- +arch/x86/deflate_quick.lo: $(SRCDIR)/deflate.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- +arch/x86/fill_window_sse.lo: $(SRCDIR)/deflate.h $(SRCDIR)/deflate_p.h $(SRCDIR)/zutil.h $(SRCDIR)/zlib.h zconf.h
- +arch/x86/x86.lo: $(SRCDIR)/arch/x86/x86.h
- diff --git a/README.md b/README.md
- index 35ea054..c28745a 100644
- --- a/README.md
- +++ b/README.md
- @@ -1,4 +1,6 @@
- -Travis CI: [![build status](https://api.travis-ci.org/Dead2/zlib-ng.svg)](https://travis-ci.org/Dead2/zlib-ng/)
- +Travis CI: [![build status](https://api.travis-ci.org/mtl1979/zlib-ng.svg)](https://travis-ci.org/mtl1979/zlib-ng/)
- +
- +Coverity: [![coverity status](https://scan.coverity.com/projects/8670/badge.svg)](https://scan.coverity.com/projects/mtl1979-zlib-ng)
- zlib-ng - zlib for the next generation systems
- diff --git a/adler32.c b/adler32.c
- index 495101d..81c2639 100644
- --- a/adler32.c
- +++ b/adler32.c
- @@ -80,7 +80,7 @@ uint32_t ZEXPORT adler32(uint32_t adler, const unsigned char *buf, uint32_t len)
- }
- /* initial Adler-32 value (deferred check for len == 1 speed) */
- - if (buf == Z_NULL)
- + if (buf == NULL)
- return 1L;
- /* in case short lengths are provided, keep it somewhat fast */
- diff --git a/arch/arm/Makefile.in b/arch/arm/Makefile.in
- index 759a121..be8c185 100644
- --- a/arch/arm/Makefile.in
- +++ b/arch/arm/Makefile.in
- @@ -7,8 +7,9 @@ CFLAGS=
- SFLAGS=
- INCLUDES=
- -SRCDIR=
- -SRCTOP=
- +SRCDIR=.
- +SRCTOP=../..
- +TOPDIR=$(SRCTOP)
- all:
- diff --git a/arch/generic/Makefile.in b/arch/generic/Makefile.in
- index 759a121..be8c185 100644
- --- a/arch/generic/Makefile.in
- +++ b/arch/generic/Makefile.in
- @@ -7,8 +7,9 @@ CFLAGS=
- SFLAGS=
- INCLUDES=
- -SRCDIR=
- -SRCTOP=
- +SRCDIR=.
- +SRCTOP=../..
- +TOPDIR=$(SRCTOP)
- all:
- diff --git a/arch/x86/Makefile.in b/arch/x86/Makefile.in
- index b327a05..e305779 100644
- --- a/arch/x86/Makefile.in
- +++ b/arch/x86/Makefile.in
- @@ -7,8 +7,13 @@ CFLAGS=
- SFLAGS=
- INCLUDES=
- +SSE2FLAG=-msse2
- +SSE4FLAG=-msse4
- +PCLMULFLAG=-mpclmul -msse4
- +
- SRCDIR=.
- SRCTOP=../..
- +TOPDIR=$(SRCTOP)
- 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
- @@ -19,28 +24,28 @@ x86.lo: $(SRCDIR)/x86.c
- $(CC) $(SFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/x86.c
- fill_window_sse.o: $(SRCDIR)/fill_window_sse.c
- - $(CC) $(CFLAGS) -msse2 $(INCLUDES) -c -o $@ $(SRCDIR)/fill_window_sse.c
- + $(CC) $(CFLAGS) $(SSE2FLAG) $(INCLUDES) -c -o $@ $(SRCDIR)/fill_window_sse.c
- fill_window_sse.lo: $(SRCDIR)/fill_window_sse.c
- - $(CC) $(SFLAGS) -msse2 -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/fill_window_sse.c
- + $(CC) $(SFLAGS) $(SSE2FLAG) -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/fill_window_sse.c
- deflate_quick.o: $(SRCDIR)/deflate_quick.c
- - $(CC) $(CFLAGS) -msse4 $(INCLUDES) -c -o $@ $(SRCDIR)/deflate_quick.c
- + $(CC) $(CFLAGS) $(SSE4FLAG) $(INCLUDES) -c -o $@ $(SRCDIR)/deflate_quick.c
- deflate_quick.lo: $(SRCDIR)/deflate_quick.c
- - $(CC) $(SFLAGS) -msse4 -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/deflate_quick.c
- + $(CC) $(SFLAGS) $(SSE4FLAG) -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/deflate_quick.c
- insert_string_sse.o: $(SRCDIR)/insert_string_sse.c
- - $(CC) $(CFLAGS) -msse4 $(INCLUDES) -c -o $@ $(SRCDIR)/insert_string_sse.c
- + $(CC) $(CFLAGS) $(SSE4FLAG) $(INCLUDES) -c -o $@ $(SRCDIR)/insert_string_sse.c
- insert_string_sse.lo: $(SRCDIR)/insert_string_sse.c
- - $(CC) $(SFLAGS) -msse4 -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/insert_string_sse.c
- + $(CC) $(SFLAGS) $(SSE4FLAG) -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/insert_string_sse.c
- crc_folding.o: $(SRCDIR)/crc_folding.c
- - $(CC) $(CFLAGS) -mpclmul -msse4 $(INCLUDES) -c -o $@ $(SRCDIR)/crc_folding.c
- + $(CC) $(CFLAGS) $(PCLMULFLAG) $(INCLUDES) -c -o $@ $(SRCDIR)/crc_folding.c
- crc_folding.lo: $(SRCDIR)/crc_folding.c
- - $(CC) $(SFLAGS) -mpclmul -msse4 -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/crc_folding.c
- + $(CC) $(SFLAGS) $(PCLMULFLAG) -DPIC $(INCLUDES) -c -o $@ $(SRCDIR)/crc_folding.c
- crc_pclmulqdq.o: $(SRCDIR)/crc_pclmulqdq.c
- $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/crc_pclmulqdq.c
- @@ -56,3 +61,23 @@ clean:
- distclean:
- rm -f Makefile
- +
- +depend:
- + makedepend -Y -- $(CFLAGS) -- $(SRCDIR)/*.c
- + makedepend -Y -a -o.lo -- $(SFLAGS) -- $(SRCDIR)/*.c
- + @sed "s=^$(SRCDIR)/\([a-zA-Z0-9_]*\.\(lo\|o\):\)==g" < Makefile > Makefile.tmp
- + @mv -f Makefile.tmp Makefile
- +
- +# DO NOT DELETE THIS LINE -- make depend depends on it.
- +
- +crc_folding.o: $(SRCDIR)/crc_folding.h $(SRCTOP)/deflate.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
- +crc_pclmulqdq.o: $(SRCDIR)/x86.h $(SRCDIR)/crc_folding.h $(SRCTOP)/deflate.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
- +deflate_quick.o: $(SRCTOP)/deflate.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
- +fill_window_sse.o: $(SRCTOP)/deflate.h $(SRCTOP)/deflate_p.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
- +x86.o: $(SRCDIR)/x86.h
- +
- +crc_folding.lo: $(SRCDIR)/crc_folding.h $(SRCTOP)/deflate.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
- +crc_pclmulqdq.lo: $(SRCDIR)/x86.h $(SRCDIR)/crc_folding.h $(SRCTOP)/deflate.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
- +deflate_quick.lo: $(SRCTOP)/deflate.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
- +fill_window_sse.lo: $(SRCTOP)/deflate.h $(SRCTOP)/deflate_p.h $(SRCTOP)/zutil.h $(SRCTOP)/zlib.h $(TOPDIR)/zconf.h
- +x86.lo: $(SRCDIR)/x86.h
- diff --git a/arch/x86/crc_folding.c b/arch/x86/crc_folding.c
- index 04c621d..fe9c4d9 100644
- --- a/arch/x86/crc_folding.c
- +++ b/arch/x86/crc_folding.c
- @@ -54,7 +54,7 @@ ZLIB_INTERNAL void crc_fold_init(deflate_state *const s) {
- s->strm->adler = 0;
- }
- -local void fold_1(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
- +static void fold_1(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
- const __m128i xmm_fold4 = _mm_set_epi32(
- 0x00000001, 0x54442bd4,
- 0x00000001, 0xc6e41596);
- @@ -77,7 +77,7 @@ local void fold_1(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1,
- *xmm_crc3 = _mm_castps_si128(ps_res);
- }
- -local void fold_2(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
- +static void fold_2(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
- const __m128i xmm_fold4 = _mm_set_epi32(
- 0x00000001, 0x54442bd4,
- 0x00000001, 0xc6e41596);
- @@ -108,7 +108,7 @@ local void fold_2(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1,
- *xmm_crc3 = _mm_castps_si128(ps_res31);
- }
- -local void fold_3(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
- +static void fold_3(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
- const __m128i xmm_fold4 = _mm_set_epi32(
- 0x00000001, 0x54442bd4,
- 0x00000001, 0xc6e41596);
- @@ -145,7 +145,7 @@ local void fold_3(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1,
- *xmm_crc3 = _mm_castps_si128(ps_res32);
- }
- -local void fold_4(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
- +static void fold_4(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1, __m128i *xmm_crc2, __m128i *xmm_crc3) {
- const __m128i xmm_fold4 = _mm_set_epi32(
- 0x00000001, 0x54442bd4,
- 0x00000001, 0xc6e41596);
- @@ -190,7 +190,7 @@ local void fold_4(deflate_state *const s, __m128i *xmm_crc0, __m128i *xmm_crc1,
- *xmm_crc3 = _mm_castps_si128(ps_res3);
- }
- -local const unsigned ALIGNED_(32) pshufb_shf_table[60] = {
- +static const unsigned ALIGNED_(32) pshufb_shf_table[60] = {
- 0x84838281, 0x88878685, 0x8c8b8a89, 0x008f8e8d, /* shl 15 (16 - 1)/shr1 */
- 0x85848382, 0x89888786, 0x8d8c8b8a, 0x01008f8e, /* shl 14 (16 - 3)/shr2 */
- 0x86858483, 0x8a898887, 0x8e8d8c8b, 0x0201008f, /* shl 13 (16 - 4)/shr3 */
- @@ -208,7 +208,7 @@ local const unsigned ALIGNED_(32) pshufb_shf_table[60] = {
- 0x0201008f, 0x06050403, 0x0a090807, 0x0e0d0c0b /* shl 1 (16 -15)/shr15*/
- };
- -local void partial_fold(deflate_state *const s, const size_t len, __m128i *xmm_crc0, __m128i *xmm_crc1,
- +static void partial_fold(deflate_state *const s, const size_t len, __m128i *xmm_crc0, __m128i *xmm_crc1,
- __m128i *xmm_crc2, __m128i *xmm_crc3, __m128i *xmm_crc_part) {
- const __m128i xmm_fold4 = _mm_set_epi32(
- @@ -378,7 +378,7 @@ done:
- CRC_SAVE(s)
- }
- -local const unsigned ALIGNED_(16) crc_k[] = {
- +static const unsigned ALIGNED_(16) crc_k[] = {
- 0xccaa009e, 0x00000000, /* rk1 */
- 0x751997d0, 0x00000001, /* rk2 */
- 0xccaa009e, 0x00000000, /* rk5 */
- @@ -387,11 +387,11 @@ local const unsigned ALIGNED_(16) crc_k[] = {
- 0xdb710640, 0x00000001 /* rk8 */
- };
- -local const unsigned ALIGNED_(16) crc_mask[4] = {
- +static const unsigned ALIGNED_(16) crc_mask[4] = {
- 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000
- };
- -local const unsigned ALIGNED_(16) crc_mask2[4] = {
- +static const unsigned ALIGNED_(16) crc_mask2[4] = {
- 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
- };
- diff --git a/arch/x86/crc_pclmulqdq.c b/arch/x86/crc_pclmulqdq.c
- index 04281ca..a513d6c 100644
- --- a/arch/x86/crc_pclmulqdq.c
- +++ b/arch/x86/crc_pclmulqdq.c
- @@ -14,7 +14,7 @@ ZLIB_INTERNAL void crc_reset(deflate_state *const s) {
- crc_fold_init(s);
- return;
- }
- - s->strm->adler = crc32(0L, Z_NULL, 0);
- + s->strm->adler = crc32(0L, NULL, 0);
- }
- ZLIB_INTERNAL void crc_finalize(deflate_state *const s) {
- diff --git a/arch/x86/deflate_quick.c b/arch/x86/deflate_quick.c
- index b5190d5..7596f75 100644
- --- a/arch/x86/deflate_quick.c
- +++ b/arch/x86/deflate_quick.c
- @@ -10,6 +10,10 @@
- * Erdinc Ozturk <erdinc.ozturk@intel.com>
- * Jim Kukunas <james.t.kukunas@linux.intel.com>
- *
- + * Portions are Copyright (C) 2016 12Sided Technology, LLC.
- + * Author:
- + * Phil Vachon <pvachon@12sidedtech.com>
- + *
- * For conditions of distribution and use, see copyright notice in zlib.h
- */
- @@ -19,10 +23,14 @@
- #endif
- #include "deflate.h"
- +#ifdef ZLIB_DEBUG
- +#include <ctype.h>
- +#endif
- +
- extern void fill_window_sse(deflate_state *s);
- extern void flush_pending(z_stream *strm);
- -local inline long compare258(const unsigned char *const src0, const unsigned char *const src1) {
- +static inline long compare258(const unsigned char *const src0, const unsigned char *const src1) {
- #ifdef _MSC_VER
- long cnt;
- @@ -111,31 +119,29 @@ local inline long compare258(const unsigned char *const src0, const unsigned cha
- #endif
- }
- -local const unsigned quick_len_codes[MAX_MATCH-MIN_MATCH+1];
- -local const unsigned quick_dist_codes[8192];
- +static const unsigned quick_len_codes[MAX_MATCH-MIN_MATCH+1];
- +static const unsigned quick_dist_codes[8192];
- -local inline void quick_send_bits(deflate_state *const s, const int value, const int length) {
- - unsigned code, out, w, b;
- +static inline void quick_send_bits(deflate_state *const s, const int value, const int length) {
- + unsigned out, width, bytes_out;
- - out = s->bi_buf;
- - w = s->bi_valid;
- + /* Concatenate the new bits with the bits currently in the buffer */
- + out = s->bi_buf | (value << s->bi_valid);
- + width = s->bi_valid + length;
- - code = value << s->bi_valid;
- - out |= code;
- - w += length;
- + /* Taking advantage of the fact that LSB comes first, write to output buffer */
- + *(unsigned *)(s->pending_buf + s->pending) = out;
- - if (s->pending + 4 >= s->pending_buf_size)
- - flush_pending(s->strm);
- + bytes_out = width / 8;
- - *(unsigned *)(s->pending_buf + s->pending) = out;
- + s->pending += bytes_out;
- - b = w >> 3;
- - s->pending += b;
- - s->bi_buf = out >> (b << 3);
- - s->bi_valid = w - (b << 3);
- + /* Shift out the valid LSBs written out */
- + s->bi_buf = out >> (bytes_out * 8);
- + s->bi_valid = width - (bytes_out * 8);
- }
- -local inline void static_emit_ptr(deflate_state *const s, const int lc, const unsigned dist) {
- +static inline void static_emit_ptr(deflate_state *const s, const int lc, const unsigned dist) {
- unsigned code, len;
- code = quick_len_codes[lc] >> 8;
- @@ -149,30 +155,32 @@ local inline void static_emit_ptr(deflate_state *const s, const int lc, const un
- const ct_data static_ltree[L_CODES+2];
- -local inline void static_emit_lit(deflate_state *const s, const int lit) {
- +static inline void static_emit_lit(deflate_state *const s, const int lit) {
- quick_send_bits(s, static_ltree[lit].Code, static_ltree[lit].Len);
- Tracecv(isgraph(lit), (stderr, " '%c' ", lit));
- }
- -local void static_emit_tree(deflate_state *const s, const int flush) {
- +static void static_emit_tree(deflate_state *const s, const int flush) {
- unsigned last;
- last = flush == Z_FINISH ? 1 : 0;
- + Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
- send_bits(s, (STATIC_TREES << 1)+ last, 3);
- }
- -
- -local void static_emit_end_block(deflate_state *const s, int last) {
- +static void static_emit_end_block(deflate_state *const s, int last) {
- send_code(s, END_BLOCK, static_ltree);
- + Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %u\n", last, s->pending, s->strm->total_out));
- if (last)
- bi_windup(s);
- s->block_start = s->strstart;
- flush_pending(s->strm);
- + s->block_open = 0;
- }
- -local inline Pos quick_insert_string(deflate_state *const s, const Pos str) {
- +static inline Pos quick_insert_string(deflate_state *const s, const Pos str) {
- Pos ret;
- unsigned h = 0;
- @@ -196,9 +204,17 @@ ZLIB_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
- IPos hash_head;
- unsigned dist, match_len;
- - static_emit_tree(s, flush);
- + if (s->block_open == 0) {
- + static_emit_tree(s, flush);
- + s->block_open = 1;
- + }
- do {
- + if (s->pending + 4 >= s->pending_buf_size) {
- + flush_pending(s->strm);
- + return need_more;
- + }
- +
- if (s->lookahead < MIN_LOOKAHEAD) {
- fill_window_sse(s);
- if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
- @@ -252,7 +268,7 @@ ZLIB_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
- return block_done;
- }
- -local const unsigned quick_len_codes[MAX_MATCH-MIN_MATCH+1] = {
- +static const unsigned quick_len_codes[MAX_MATCH-MIN_MATCH+1] = {
- 0x00004007, 0x00002007, 0x00006007, 0x00001007,
- 0x00005007, 0x00003007, 0x00007007, 0x00000807,
- 0x00004808, 0x0000c808, 0x00002808, 0x0000a808,
- @@ -319,7 +335,7 @@ local const unsigned quick_len_codes[MAX_MATCH-MIN_MATCH+1] = {
- 0x001c230d, 0x001d230d, 0x001e230d, 0x0000a308,
- };
- -local const unsigned quick_dist_codes[8192] = {
- +static const unsigned quick_dist_codes[8192] = {
- 0x00000005, 0x00001005, 0x00000805, 0x00001805,
- 0x00000406, 0x00002406, 0x00001406, 0x00003406,
- 0x00000c07, 0x00002c07, 0x00004c07, 0x00006c07,
- diff --git a/arch/x86/fill_window_sse.c b/arch/x86/fill_window_sse.c
- index d82b1d1..72de0f4 100644
- --- a/arch/x86/fill_window_sse.c
- +++ b/arch/x86/fill_window_sse.c
- @@ -12,6 +12,7 @@
- #include <immintrin.h>
- #include "deflate.h"
- +#include "deflate_p.h"
- extern int read_buf(z_stream *strm, unsigned char *buf, unsigned size);
- @@ -107,20 +108,38 @@ ZLIB_INTERNAL void fill_window_sse(deflate_state *s) {
- if (s->lookahead + s->insert >= MIN_MATCH) {
- unsigned int str = s->strstart - s->insert;
- s->ins_h = s->window[str];
- +#ifndef NOT_TWEAK_COMPILER
- + {
- + unsigned int insert_cnt = s->insert;
- + unsigned int slen;
- + if (unlikely(s->lookahead < MIN_MATCH))
- + insert_cnt += s->lookahead - MIN_MATCH;
- + slen = insert_cnt;
- + if (str >= (MIN_MATCH - 2))
- + {
- + str += 2 - MIN_MATCH;
- + insert_cnt += MIN_MATCH - 2;
- + }
- + if (insert_cnt > 0)
- + {
- + insert_string(s, str, insert_cnt);
- + s->insert -= slen;
- + }
- + }
- +#else
- if (str >= 1)
- - UPDATE_HASH(s, s->ins_h, str + 1 - (MIN_MATCH-1));
- + insert_string(s, str + 2 - MIN_MATCH, 1);
- #if MIN_MATCH != 3
- - Call UPDATE_HASH() MIN_MATCH-3 more times
- +#warning Call insert_string() MIN_MATCH-3 more times
- #endif
- while (s->insert) {
- - UPDATE_HASH(s, s->ins_h, str);
- - s->prev[str & s->w_mask] = s->head[s->ins_h];
- - s->head[s->ins_h] = (Pos)str;
- + insert_string(s, str, 1);
- str++;
- s->insert--;
- if (s->lookahead + s->insert < MIN_MATCH)
- break;
- }
- +#endif
- }
- /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
- * but this is not important since only literal bytes will be emitted.
- diff --git a/arch/x86/insert_string_sse.c b/arch/x86/insert_string_sse.c
- index 6d4ddae..a927dc0 100644
- --- a/arch/x86/insert_string_sse.c
- +++ b/arch/x86/insert_string_sse.c
- @@ -19,14 +19,14 @@
- Pos insert_string_sse(deflate_state *const s, const Pos str, unsigned int count) {
- Pos ret = 0;
- unsigned int idx;
- - unsigned *ip, val, h = 0;
- + unsigned *ip, val, h;
- for (idx = 0; idx < count; idx++) {
- ip = (unsigned *)&s->window[str+idx];
- val = *ip;
- h = 0;
- - if (s->level >= 6)
- + if (s->level >= TRIGGER_LEVEL)
- val &= 0xFFFFFF;
- #ifdef _MSC_VER
- diff --git a/arch/x86/x86.c b/arch/x86/x86.c
- index 4c255af..f09c4af 100644
- --- a/arch/x86/x86.c
- +++ b/arch/x86/x86.c
- @@ -10,18 +10,18 @@
- #include "x86.h"
- -ZLIB_INTERNAL int x86_cpu_has_sse2;
- -ZLIB_INTERNAL int x86_cpu_has_sse42;
- -ZLIB_INTERNAL int x86_cpu_has_pclmulqdq;
- -ZLIB_INTERNAL int x86_cpu_has_tzcnt;
- -
- #ifdef _MSC_VER
- -#include <intrin.h>
- +# include <intrin.h>
- #else
- // Newer versions of GCC and clang come with cpuid.h
- -#include <cpuid.h>
- +# include <cpuid.h>
- #endif
- +ZLIB_INTERNAL int x86_cpu_has_sse2;
- +ZLIB_INTERNAL int x86_cpu_has_sse42;
- +ZLIB_INTERNAL int x86_cpu_has_pclmulqdq;
- +ZLIB_INTERNAL int x86_cpu_has_tzcnt;
- +
- static void cpuid(int info, unsigned* eax, unsigned* ebx, unsigned* ecx, unsigned* edx) {
- #ifdef _MSC_VER
- unsigned int registers[4];
- diff --git a/compress.c b/compress.c
- index 9f6f140..d4f99bb 100644
- --- a/compress.c
- +++ b/compress.c
- @@ -23,14 +23,14 @@ int ZEXPORT compress2(unsigned char *dest, size_t *destLen, const unsigned char
- size_t sourceLen, int level) {
- z_stream stream;
- int err;
- - const unsigned int max = (unsigned int)0 - 1;
- + const unsigned int max = (unsigned int)-1;
- size_t left;
- left = *destLen;
- *destLen = 0;
- - stream.zalloc = (alloc_func)0;
- - stream.zfree = (free_func)0;
- + stream.zalloc = NULL;
- + stream.zfree = NULL;
- stream.opaque = NULL;
- err = deflateInit(&stream, level);
- diff --git a/configure b/configure
- index 5892162..d2326bd 100755
- --- a/configure
- +++ b/configure
- @@ -83,6 +83,7 @@ fi
- # set defaults before processing command line options
- LDCONFIG=${LDCONFIG-"ldconfig"}
- +LDFLAGS=${LDFLAGS-"-L."}
- LDSHAREDLIBC="${LDSHAREDLIBC--lc}"
- DEFFILE=
- RC=
- @@ -104,9 +105,14 @@ compat=0
- cover=0
- build32=0
- build64=0
- +native=0
- +sse2flag="-msse2"
- +sse4flag="-msse4"
- +pclmulflag="-mpclmul -msse4"
- without_optimizations=0
- without_new_strategies=0
- gcc=0
- +debug=0
- old_cc="$CC"
- old_cflags="$CFLAGS"
- OBJC='$(OBJZ)'
- @@ -114,8 +120,6 @@ PIC_OBJC='$(PIC_OBJZ)'
- INSTALLTARGETS="install-shared install-static"
- UNINSTALLTARGETS="uninstall-shared uninstall-static"
- -TEST="teststatic"
- -
- # leave this script, optionally in a bad way
- leave()
- {
- @@ -155,11 +159,13 @@ case "$1" in
- --cover) cover=1; shift ;;
- -3* | --32) build32=1; shift ;;
- -6* | --64) build64=1; shift ;;
- + -n | --native) native=1; shift ;;
- -a*=* | --archs=*) ARCHS=`echo $1 | sed 's/.*=//'`; shift ;;
- --sysconfdir=*) echo "ignored option: --sysconfdir" | tee -a configure.log; shift ;;
- --localstatedir=*) echo "ignored option: --localstatedir" | tee -a configure.log; shift ;;
- -noopt | --without-optimizations) without_optimizations=1; shift;;
- -oldstrat | --without-new-strategies) without_new_strategies=1; shift;;
- + -d* | --debug) debug=1; shift ;;
- *)
- echo "unknown option: $1" | tee -a configure.log
- echo "$0 --help for help" | tee -a configure.log
- @@ -197,6 +203,7 @@ case "$cc" in
- esac
- case `$cc -v 2>&1` in
- *gcc*) gcc=1 ;;
- + *clang*) gcc=1 ;;
- esac
- show $cc -c $test.c
- @@ -219,28 +226,42 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
- ARCH=$GCC_ARCH
- fi ;;
- esac
- - CFLAGS="${CFLAGS--O3} ${ARCHS} -Wall"
- + CFLAGS="${CFLAGS--O3}"
- + if test -n "${ARCHS}"; then
- + CFLAGS="${CFLAGS} ${ARCHS}"
- + LDFLAGS="${LDFLAGS} ${ARCHS}"
- + fi
- + CFLAGS="${CFLAGS} -Wall"
- SFLAGS="${CFLAGS--O3} -fPIC"
- - LDFLAGS="${LDFLAGS} ${ARCHS}"
- if test $build64 -eq 1; then
- CFLAGS="${CFLAGS} -m64"
- SFLAGS="${SFLAGS} -m64"
- fi
- + if test $native -eq 1; then
- + sse2flag="-march=native"
- + sse4flag="-march=native"
- + pclmulflag="-march=native"
- + fi
- if test "${ZLIBGCCWARN}" = "YES"; then
- CFLAGS="${CFLAGS} -Wextra -pedantic"
- fi
- + if test $debug -eq 1; then
- + CFLAGS="${CFLAGS} -DZLIB_DEBUG"
- + SFLAGS="${SFLAGS} -DZLIB_DEBUG"
- + fi
- if test -z "$uname"; then
- uname=`(uname -s || echo unknown) 2>/dev/null`
- fi
- case "$uname" in
- Linux* | linux* | GNU | GNU/* | solaris*)
- - LDSHARED=${LDSHARED-"$cc -shared -Wl,-soname,libz.so.1,--version-script,${SRCDIR}/zlib.map"} ;;
- + LDSHARED=${LDSHARED-"$cc"}
- + LDSHAREDFLAGS="-shared -Wl,-soname,libz.so.1,--version-script,${SRCDIR}/zlib.map" ;;
- *BSD | *bsd* | DragonFly)
- - LDSHARED=${LDSHARED-"$cc -shared -Wl,-soname,libz.so.1,--version-script,${SRCDIR}/zlib.map"}
- + LDSHARED=${LDSHARED-"$cc"}
- + LDSHAREDFLAGS="-shared -Wl,-soname,libz.so.1,--version-script,${SRCDIR}/zlib.map"
- LDCONFIG="ldconfig -m" ;;
- CYGWIN* | Cygwin* | cygwin*)
- ARFLAGS="rcs"
- - CFLAGS="${CFLAGS} -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
- SFLAGS="${CFLAGS}"
- shared_ext='.dll'
- sharedlibdir='${bindir}'
- @@ -249,7 +270,8 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
- SHAREDLIBV=''
- SHAREDTARGET=$SHAREDLIB
- IMPORTLIB='libz.dll.a'
- - LDSHARED=${LDSHARED-"$cc -shared -Wl,--out-implib,${IMPORTLIB},--version-script,${SRCDIR}/zlib.map"}
- + LDSHARED=${LDSHARED-"$cc"}
- + LDSHAREDFLAGS="-shared -Wl,--out-implib,${IMPORTLIB},--version-script,${SRCDIR}/zlib.map"
- LDSHAREDLIBC=""
- DEFFILE='win32/zlib.def'
- RC='windres'
- @@ -267,7 +289,8 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
- SHAREDLIBV=''
- SHAREDTARGET=$SHAREDLIB
- IMPORTLIB='libz.dll.a'
- - LDSHARED=${LDSHARED-"$cc -shared -Wl,--out-implib,${IMPORTLIB}"}
- + LDSHARED=${LDSHARED-"$cc"}
- + LDSHAREDFLAGS="-shared -Wl,--out-implib,${IMPORTLIB}"
- LDSHAREDLIBC=""
- DEFFILE='win32/zlib.def'
- RC='windres'
- @@ -277,7 +300,7 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
- EXE='.exe' ;;
- MINGW* | mingw*)
- ARFLAGS="rcs"
- - CFLAGS="${CFLAGS} -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_POSIX_C_SOURCE=200809L -D_GNU_SOURCE=1"
- + CFLAGS="${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_GNU_SOURCE=1"
- SFLAGS="${CFLAGS}"
- shared_ext='.dll'
- sharedlibdir='${bindir}'
- @@ -286,7 +309,8 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
- SHAREDLIBV=''
- SHAREDTARGET=$SHAREDLIB
- IMPORTLIB='libz.dll.a'
- - LDSHARED=${LDSHARED-"$cc -shared -Wl,--out-implib=${IMPORTLIB} -Wl,--version-script=${SRCDIR}/zlib.map"}
- + LDSHARED=${LDSHARED-"$cc"}
- + LDSHAREDFLAGS="-shared -Wl,--out-implib=${IMPORTLIB} -Wl,--version-script=${SRCDIR}/zlib.map"
- LDSHAREDLIBC=""
- DEFFILE='win32/zlib.def'
- RC='windres'
- @@ -301,9 +325,11 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
- EXE='.exe' ;;
- QNX*) # This is for QNX6. I suppose that the QNX rule below is for QNX2,QNX4
- # (alain.bonnefoy@icbt.com)
- - LDSHARED=${LDSHARED-"$cc -shared -Wl,-hlibz.so.1"} ;;
- + LDSHARED=${LDSHARED-"$cc"}
- + LDSHAREDFLAGS="-shared -Wl,-hlibz.so.1" ;;
- HP-UX*)
- - LDSHARED=${LDSHARED-"$cc -shared $SFLAGS"}
- + LDSHARED=${LDSHARED-"$cc"}
- + LDSHAREDFLAGS="-shared"
- case `(uname -m || echo unknown) 2>/dev/null` in
- ia64)
- shared_ext='.so'
- @@ -318,14 +344,16 @@ if test "$gcc" -eq 1 && ($cc -c $test.c) >> configure.log 2>&1; then
- SHAREDLIBV=libz.$VER$shared_ext
- SHAREDLIBM=libz.$VER1$shared_ext
- SHAREDTARGET=$SHAREDLIBV
- - LDSHARED=${LDSHARED-"$cc -dynamiclib -install_name $libdir/$SHAREDLIBM -compatibility_version $VER1 -current_version $VER3"}
- + LDSHARED=${LDSHARED-"$cc"}
- + LDSHAREDFLAGS="-dynamiclib -install_name $libdir/$SHAREDLIBM -compatibility_version $VER1 -current_version $VER3"
- if libtool -V 2>&1 | grep Apple > /dev/null; then
- AR="libtool"
- else
- AR="/usr/bin/libtool"
- fi
- ARFLAGS="-o" ;;
- - *) LDSHARED=${LDSHARED-"$cc -shared"} ;;
- + *) LDSHARED=${LDSHARED-"$cc"}
- + LDSHAREDFLAGS="-shared" ;;
- esac
- else
- # find system name and corresponding cc options
- @@ -338,8 +366,8 @@ else
- case "$uname" in
- HP-UX*) SFLAGS=${CFLAGS-"-O +z"}
- CFLAGS=${CFLAGS-"-O"}
- -# LDSHARED=${LDSHARED-"ld -b +vnocompatwarnings"}
- - LDSHARED=${LDSHARED-"ld -b"}
- + LDSHARED=${LDSHARED-"ld"}
- + LDSHAREDFLAGS="-b"
- case `(uname -m || echo unknown) 2>/dev/null` in
- ia64)
- shared_ext='.so'
- @@ -351,11 +379,13 @@ else
- AIX*) # Courtesy of dbakker@arrayasolutions.com
- SFLAGS=${CFLAGS-"-O -qmaxmem=8192"}
- CFLAGS=${CFLAGS-"-O -qmaxmem=8192"}
- - LDSHARED=${LDSHARED-"xlc -G"} ;;
- + LDSHARED=${LDSHARED-"xlc"}
- + LDSHAREDFLAGS="-G" ;;
- # send working options for other systems to zlib@gzip.org
- *) SFLAGS=${CFLAGS-"-O"}
- CFLAGS=${CFLAGS-"-O"}
- - LDSHARED=${LDSHARED-"cc -shared"} ;;
- + LDSHARED=${LDSHARED-"cc"}
- + LDSHAREDFLAGS="-shared" ;;
- esac
- fi
- @@ -426,7 +456,7 @@ if test $shared -eq 1; then
- echo Checking for shared library support... | tee -a configure.log
- # we must test in two steps (cc then ld), required at least on SunOS 4.x
- if try $CC -w -c $SFLAGS $test.c &&
- - try $LDSHARED $SFLAGS -o $test$shared_ext $test.o; then
- + try $LDSHARED $LDSHAREDFLAGS $LDFLAGS -o $test$shared_ext $test.o; then
- echo Building shared library $SHAREDTARGET with $CC. | tee -a configure.log
- elif test -z "$old_cc" -a -z "$old_cflags"; then
- echo No shared library support. | tee -a configure.log
- @@ -438,7 +468,9 @@ if test $shared -eq 1; then
- fi
- if test $shared -eq 0; then
- LDSHARED="$CC"
- + LDSHAREDFLAGS=""
- ALL="static"
- + TEST="all teststatic"
- SHAREDLIB=""
- SHAREDLIBV=""
- SHAREDLIBM=""
- @@ -448,7 +480,7 @@ if test $shared -eq 0; then
- echo Building static library $STATICLIB version $VER with $CC. | tee -a configure.log
- else
- ALL="static shared"
- - TEST="${TEST} testshared"
- + TEST="all teststatic testshared"
- fi
- echo >> configure.log
- @@ -456,9 +488,6 @@ echo >> configure.log
- # check for large file support, and if none, check for fseeko()
- cat > $test.c <<EOF
- #include <sys/types.h>
- -#ifdef __MSYS__
- -# define off64_t _off64_t
- -#endif
- off64_t dummy = 0;
- EOF
- if try $CC -c $CFLAGS -D_LARGEFILE64_SOURCE=1 $test.c; then
- @@ -472,6 +501,21 @@ else
- echo "Checking for off64_t... No." | tee -a configure.log
- echo >> configure.log
- cat > $test.c <<EOF
- +#include <sys/types.h>
- +int main() {
- + _off64_t dummy = 0;
- + return 0;
- +}
- +EOF
- + if try $CC $CFLAGS -o $test $test.c $LDSHAREDLIBC; then
- + echo "Checking for _off64_t... Yes." | tee -a configure.log
- + ALL="${ALL} all64"
- + TEST="${TEST} test64"
- + else
- + echo "Checking for _off64_t... No." | tee -a configure.log
- + fi
- + echo >> configure.log
- + cat > $test.c <<EOF
- #include <stdio.h>
- int main(void) {
- fseeko(NULL, 0, 0);
- @@ -655,7 +699,7 @@ int main(void)
- return 0;
- }
- EOF
- -if try ${CC} ${CFLAGS} -msse2 $test.c; then
- +if try ${CC} ${CFLAGS} ${sse2flag} $test.c; then
- echo "Checking for SSE2 intrinsics ... Yes." | tee -a configure.log
- HAVE_SSE2_INTRIN=1
- else
- @@ -676,7 +720,7 @@ int main(void)
- return 0;
- }
- EOF
- -if try ${CC} ${CFLAGS} -mpclmul $test.c; then
- +if try ${CC} ${CFLAGS} ${pclmulflag} $test.c; then
- echo "Checking for PCLMULQDQ intrinsics ... Yes." | tee -a configure.log
- HAVE_PCLMULQDQ_INTRIN=1
- else
- @@ -684,7 +728,7 @@ else
- HAVE_PCLMULQDQ_INTRIN=0
- fi
- -# Enable deflate_medium at level 4-6
- +# Enable deflate_medium at level 4-5
- if test $without_new_strategies -eq 0; then
- CFLAGS="${CFLAGS} -DMEDIUM_STRATEGY"
- SFLAGS="${SFLAGS} -DMEDIUM_STRATEGY"
- @@ -784,6 +828,7 @@ echo EXE = $EXE >> configure.log
- echo LDCONFIG = $LDCONFIG >> configure.log
- echo LDFLAGS = $LDFLAGS >> configure.log
- echo LDSHARED = $LDSHARED >> configure.log
- +echo LDSHAREDFLAGS = $LDSHAREDFLAGS >> configure.log
- echo LDSHAREDLIBC = $LDSHAREDLIBC >> configure.log
- echo DEFFILE = $DEFFILE >> configure.log
- echo RC = $RC >> configure.log
- @@ -814,6 +859,9 @@ echo mandir = $mandir >> configure.log
- echo prefix = $prefix >> configure.log
- echo sharedlibdir = $sharedlibdir >> configure.log
- echo uname = $uname >> configure.log
- +echo sse2flag = $sse2flag >> configure.log
- +echo sse4flag = $sse4flag >> configure.log
- +echo pclmulflag = $pclmulflag >> configure.log
- echo ARCHDIR = ${ARCHDIR} >> configure.log
- echo ARCH_STATIC_OBJS = ${ARCH_STATIC_OBJS} >> configure.log
- echo ARCH_SHARED_OBJS = ${ARCH_SHARED_OBJS} >> configure.log
- @@ -829,6 +877,7 @@ sed < $SRCDIR/Makefile.in "
- /^SFLAGS *=/s#=.*#=$SFLAGS#
- /^LDFLAGS *=/s#=.*#=$LDFLAGS#
- /^LDSHARED *=/s#=.*#=$LDSHARED#
- +/^LDSHAREDFLAGS *=/s#=.*#=$LDSHAREDFLAGS#
- /^STATICLIB *=/s#=.*#=$STATICLIB#
- /^SHAREDLIB *=/s#=.*#=$SHAREDLIB#
- /^SHAREDLIBV *=/s#=.*#=$SHAREDLIBV#
- @@ -858,6 +907,7 @@ sed < $SRCDIR/Makefile.in "
- /^OBJC *=/s#=.*#= $OBJC#
- /^PIC_OBJC *=/s#=.*#= $PIC_OBJC#
- /^all: */s#:.*#: $ALL#
- +/^test: */s#:.*#: $TEST#
- /^install-libs: */s#:.*#: $INSTALLTARGETS#
- /^uninstall-libs: */s#:.*#: $UNINSTALLTARGETS#
- /^ARCHDIR *=/s#=.*#=$ARCHDIR#
- @@ -878,29 +928,18 @@ sed < $SRCDIR/$ARCHDIR/Makefile.in "
- /^INCLUDES *=/s#=.*#=$ARCHINCLUDES#
- /^SRCDIR *=/s#=.*#=$SRCDIR/$ARCHDIR#
- /^SRCTOP *=/s#=.*#=$SRCDIR#
- +/^TOPDIR *=/s#=.*#=$BUILDDIR#
- +/^SSE2FLAG *=/s#=.*#=$sse2flag#
- +/^SSE4FLAG *=/s#=.*#=$sse4flag#
- +/^PCLMULFLAG *=/s#=.*#=$pclmulflag#
- " > $ARCHDIR/Makefile
- -# Generate Makefile in test dir
- -mkdir -p test
- -TESTINCLUDES="-I$SRCDIR"
- -if [ "$SRCDIR" != "$BUILDDIR" ]; then TESTINCLUDES="${TESTINCLUDES} -I$BUILDDIR"; fi
- -if test $compat -eq 1; then COMPATTESTS="compattests"; fi
- -sed < $SRCDIR/test/Makefile.in "
- -/^CC *=/s#=.*#=$CC#
- -/^CFLAGS *=/s#=.*#=$CFLAGS#
- -/^EXE *=/s#=.*#=$EXE#
- -/^oldtests: */s#:.*#: $TEST#
- -/^INCLUDES *=/s#=.*#=$TESTINCLUDES#
- -/^SRCDIR *=/s#=.*#=$SRCDIR/test#
- -/^SRCTOP *=/s#=.*#=$SRCDIR#
- -/^COMPATTESTS *=/s#=.*#=$COMPATTESTS#
- -" > test/Makefile
- -
- # create zlib.pc with the configure results
- sed < $SRCDIR/zlib.pc.in "
- /^CC *=/s#=.*#=$CC#
- /^CFLAGS *=/s#=.*#=$CFLAGS#
- /^LDSHARED *=/s#=.*#=$LDSHARED#
- +/^LDSHAREDFLAGS *=/s#=.*#=$LDSHAREDFLAGS#
- /^STATICLIB *=/s#=.*#=$STATICLIB#
- /^SHAREDLIB *=/s#=.*#=$SHAREDLIB#
- /^SHAREDLIBV *=/s#=.*#=$SHAREDLIBV#
- diff --git a/crc32.c b/crc32.c
- index 4e88a1e..8787183 100644
- --- a/crc32.c
- +++ b/crc32.c
- @@ -208,7 +208,7 @@ const uint32_t * ZEXPORT get_crc_table(void) {
- }
- uint32_t ZEXPORT crc32(uint32_t crc, const unsigned char *buf, z_off64_t len) {
- - if (buf == Z_NULL) return 0;
- + if (buf == NULL) return 0;
- #ifdef DYNAMIC_CRC_TABLE
- if (crc_table_empty)
- @@ -255,6 +255,18 @@ ZLIB_INTERNAL uint32_t crc32_generic(uint32_t crc, const unsigned char *buf, z_o
- }
- +/*
- + This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit
- + integer pointer type. This violates the strict aliasing rule, where a
- + compiler can assume, for optimization purposes, that two pointers to
- + fundamentally different types won't ever point to the same memory. This can
- + manifest as a problem only if one of the pointers is written to. This code
- + only reads from those pointers. So long as this code remains isolated in
- + this compilation unit, there won't be a problem. For this reason, this code
- + should not be copied and pasted into a compilation unit in which other code
- + writes to the buffer that is passed to these routines.
- + */
- +
- /* ========================================================================= */
- #if BYTE_ORDER == LITTLE_ENDIAN
- #define DOLIT4 c ^= *buf4++; \
- @@ -299,7 +311,7 @@ ZLIB_INTERNAL uint32_t crc32_little(uint32_t crc, const unsigned char *buf, z_of
- /* ========================================================================= */
- #if BYTE_ORDER == BIG_ENDIAN
- -#define DOBIG4 c ^= *++buf4; \
- +#define DOBIG4 c ^= *buf4++; \
- c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
- crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
- #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
- @@ -317,7 +329,6 @@ ZLIB_INTERNAL uint32_t crc32_big(uint32_t crc, const unsigned char *buf, z_off64
- }
- buf4 = (const uint32_t *)(const void *)buf;
- - buf4--;
- #ifndef UNROLL_LESS
- while (len >= 32) {
- @@ -330,7 +341,6 @@ ZLIB_INTERNAL uint32_t crc32_big(uint32_t crc, const unsigned char *buf, z_off64
- DOBIG4;
- len -= 4;
- }
- - buf4++;
- buf = (const unsigned char *)buf4;
- if (len) do {
- @@ -429,7 +439,7 @@ uint32_t ZEXPORT crc32_combine64(uint32_t crc1, uint32_t crc2, z_off64_t len2) {
- #ifndef X86_PCLMULQDQ_CRC
- ZLIB_INTERNAL void crc_reset(deflate_state *const s) {
- - s->strm->adler = crc32(0L, Z_NULL, 0);
- + s->strm->adler = crc32(0L, NULL, 0);
- }
- ZLIB_INTERNAL void copy_with_crc(z_stream *strm, unsigned char *dst, unsigned long size) {
- diff --git a/deflate.c b/deflate.c
- index 1a1fcda..c0faabd 100644
- --- a/deflate.c
- +++ b/deflate.c
- @@ -1,5 +1,5 @@
- /* deflate.c -- compress data using the deflation algorithm
- - * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
- + * Copyright (C) 1995-2016 Jean-loup Gailly and Mark Adler
- * For conditions of distribution and use, see copyright notice in zlib.h
- */
- @@ -53,7 +53,7 @@
- #include "deflate_p.h"
- #include "match.h"
- -const char deflate_copyright[] = " deflate 1.2.8.f Copyright 1995-2013 Jean-loup Gailly and Mark Adler ";
- +const char deflate_copyright[] = " deflate 1.2.8.f Copyright 1995-2016 Jean-loup Gailly and Mark Adler ";
- /*
- If you use the zlib library in a product, an acknowledgment is welcome
- in the documentation of your product. If for some reason you cannot
- @@ -68,20 +68,21 @@ const char deflate_copyright[] = " deflate 1.2.8.f Copyright 1995-2013 Jean-loup
- typedef block_state (*compress_func) (deflate_state *s, int flush);
- /* Compression function. Returns the block state after the call. */
- -void fill_window (deflate_state *s);
- -local block_state deflate_stored (deflate_state *s, int flush);
- -block_state deflate_fast (deflate_state *s, int flush);
- -block_state deflate_quick (deflate_state *s, int flush);
- +static int deflateStateCheck (z_streamp strm);
- +static void slide_hash (deflate_state *s);
- +static block_state deflate_stored (deflate_state *s, int flush);
- +block_state deflate_fast (deflate_state *s, int flush);
- +block_state deflate_quick (deflate_state *s, int flush);
- #ifdef MEDIUM_STRATEGY
- -block_state deflate_medium (deflate_state *s, int flush);
- +block_state deflate_medium (deflate_state *s, int flush);
- #endif
- -block_state deflate_slow (deflate_state *s, int flush);
- -local block_state deflate_rle (deflate_state *s, int flush);
- -local block_state deflate_huff (deflate_state *s, int flush);
- -local void lm_init (deflate_state *s);
- -local void putShortMSB (deflate_state *s, uint16_t b);
- -ZLIB_INTERNAL void flush_pending (z_stream *strm);
- -ZLIB_INTERNAL int read_buf (z_stream *strm, unsigned char *buf, unsigned size);
- +block_state deflate_slow (deflate_state *s, int flush);
- +static block_state deflate_rle (deflate_state *s, int flush);
- +static block_state deflate_huff (deflate_state *s, int flush);
- +static void lm_init (deflate_state *s);
- +static void putShortMSB (deflate_state *s, uint16_t b);
- +ZLIB_INTERNAL void flush_pending (z_stream *strm);
- +ZLIB_INTERNAL int read_buf (z_stream *strm, unsigned char *buf, unsigned size);
- extern void crc_reset(deflate_state *const s);
- #ifdef X86_PCLMULQDQ_CRC
- @@ -109,7 +110,7 @@ typedef struct config_s {
- compress_func func;
- } config;
- -local const config configuration_table[10] = {
- +static const config configuration_table[10] = {
- /* good lazy nice chain */
- /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
- @@ -124,16 +125,22 @@ local const config configuration_table[10] = {
- /* 3 */ {4, 6, 32, 32, deflate_fast},
- #ifdef MEDIUM_STRATEGY
- +# ifdef NOT_TWEAK_COMPILER
- /* 4 */ {4, 4, 16, 16, deflate_medium}, /* lazy matches */
- /* 5 */ {8, 16, 32, 32, deflate_medium},
- /* 6 */ {8, 16, 128, 128, deflate_medium},
- -#else
- -/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
- -/* 5 */ {8, 16, 32, 32, deflate_slow},
- +# else
- +/* 4 */ {4, 8, 16, 16, deflate_medium}, /* lazy matches */
- +/* 5 */ {8, 16, 64, 128, deflate_medium},
- /* 6 */ {8, 16, 128, 128, deflate_slow},
- +# endif /* NOT_TWEAK_COMPILER */
- +#else
- +/* 4 */ {4, 12, 32, 32, deflate_slow}, /* lazy matches */
- +/* 5 */ {8, 16, 48, 64, deflate_slow},
- +/* 6 */ {8, 32, 128, 128, deflate_slow},
- #endif
- -/* 7 */ {8, 32, 128, 256, deflate_slow},
- +/* 7 */ {8, 32, 128, 256, deflate_slow},
- /* 8 */ {32, 128, 258, 1024, deflate_slow},
- /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
- @@ -142,9 +149,6 @@ local const config configuration_table[10] = {
- * meaning.
- */
- -#define EQUAL 0
- -/* result of memcmp for equal strings */
- -
- /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
- #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
- @@ -157,6 +161,36 @@ local const config configuration_table[10] = {
- s->head[s->hash_size-1] = NIL; \
- memset((unsigned char *)s->head, 0, (unsigned)(s->hash_size-1)*sizeof(*s->head));
- +/* ===========================================================================
- + * Slide the hash table when sliding the window down (could be avoided with 32
- + * bit values at the expense of memory usage). We slide even when level == 0 to
- + * keep the hash table consistent if we switch back to level > 0 later.
- + */
- +static void slide_hash(deflate_state *s)
- +{
- + unsigned n, m;
- + Pos *p;
- + unsigned int wsize = s->w_size;
- +
- + n = s->hash_size;
- + p = &s->head[n];
- + do {
- + m = *--p;
- + *p = (Pos)(m >= wsize ? m - wsize : NIL);
- + } while (--n);
- + n = wsize;
- +#ifndef FASTEST
- + p = &s->prev[n];
- + do {
- + m = *--p;
- + *p = (Pos)(m >= wsize ? m - wsize : NIL);
- + /* If n is not on any hash chain, prev[n] is garbage but
- + * its value will never be used.
- + */
- + } while (--n);
- +#endif
- +}
- +
- /* ========================================================================= */
- int ZEXPORT deflateInit_(z_stream *strm, int level, const char *version, int stream_size) {
- return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, version, stream_size);
- @@ -180,18 +214,18 @@ int ZEXPORT deflateInit2_(z_stream *strm, int level, int method, int windowBits,
- x86_check_features();
- #endif
- - if (version == Z_NULL || version[0] != my_version[0] || stream_size != sizeof(z_stream)) {
- + if (version == NULL || version[0] != my_version[0] || stream_size != sizeof(z_stream)) {
- return Z_VERSION_ERROR;
- }
- - if (strm == Z_NULL)
- + if (strm == NULL)
- return Z_STREAM_ERROR;
- - strm->msg = Z_NULL;
- - if (strm->zalloc == (alloc_func)0) {
- + strm->msg = NULL;
- + if (strm->zalloc == NULL) {
- strm->zalloc = zcalloc;
- strm->opaque = NULL;
- }
- - if (strm->zfree == (free_func)0)
- + if (strm->zfree == NULL)
- strm->zfree = zcfree;
- if (level == Z_DEFAULT_COMPRESSION)
- @@ -207,7 +241,8 @@ int ZEXPORT deflateInit2_(z_stream *strm, int level, int method, int windowBits,
- #endif
- }
- if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || windowBits < 8 ||
- - windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
- + windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED ||
- + (windowBits == 8 && wrap != 1)) {
- return Z_STREAM_ERROR;
- }
- if (windowBits == 8)
- @@ -219,13 +254,14 @@ int ZEXPORT deflateInit2_(z_stream *strm, int level, int method, int windowBits,
- #endif
- s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
- - if (s == Z_NULL)
- + if (s == NULL)
- return Z_MEM_ERROR;
- strm->state = (struct internal_state *)s;
- s->strm = strm;
- + s->status = INIT_STATE; /* to pass state test in deflateReset() */
- s->wrap = wrap;
- - s->gzhead = Z_NULL;
- + s->gzhead = NULL;
- s->w_bits = windowBits;
- s->w_size = 1 << s->w_bits;
- s->w_mask = s->w_size - 1;
- @@ -257,8 +293,8 @@ int ZEXPORT deflateInit2_(z_stream *strm, int level, int method, int windowBits,
- s->pending_buf = (unsigned char *) overlay;
- s->pending_buf_size = (unsigned long)s->lit_bufsize * (sizeof(uint16_t)+2L);
- - if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
- - s->pending_buf == Z_NULL) {
- + if (s->window == NULL || s->prev == NULL || s->head == NULL ||
- + s->pending_buf == NULL) {
- s->status = FINISH_STATE;
- strm->msg = ERR_MSG(Z_MEM_ERROR);
- deflateEnd(strm);
- @@ -270,10 +306,35 @@ int ZEXPORT deflateInit2_(z_stream *strm, int level, int method, int windowBits,
- s->level = level;
- s->strategy = strategy;
- s->method = (unsigned char)method;
- + s->block_open = 0;
- return deflateReset(strm);
- }
- +/* =========================================================================
- + * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
- + */
- +static int deflateStateCheck (z_stream *strm)
- +{
- + deflate_state *s;
- + if (strm == NULL ||
- + strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
- + return 1;
- + s = strm->state;
- + if (s == NULL || s->strm != strm || (s->status != INIT_STATE &&
- +#ifdef GZIP
- + s->status != GZIP_STATE &&
- +#endif
- + s->status != EXTRA_STATE &&
- + s->status != NAME_STATE &&
- + s->status != COMMENT_STATE &&
- + s->status != HCRC_STATE &&
- + s->status != BUSY_STATE &&
- + s->status != FINISH_STATE))
- + return 1;
- + return 0;
- +}
- +
- /* ========================================================================= */
- int ZEXPORT deflateSetDictionary(z_stream *strm, const unsigned char *dictionary, unsigned int dictLength) {
- deflate_state *s;
- @@ -282,7 +343,7 @@ int ZEXPORT deflateSetDictionary(z_stream *strm, const unsigned char *dictionary
- uint32_t avail;
- const unsigned char *next;
- - if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
- + if (deflateStateCheck(strm) || dictionary == NULL)
- return Z_STREAM_ERROR;
- s = strm->state;
- wrap = s->wrap;
- @@ -315,7 +376,7 @@ int ZEXPORT deflateSetDictionary(z_stream *strm, const unsigned char *dictionary
- while (s->lookahead >= MIN_MATCH) {
- str = s->strstart;
- n = s->lookahead - (MIN_MATCH-1);
- - bulk_insert_str(s, str, n);
- + insert_string(s, str, n);
- s->strstart = str + n;
- s->lookahead = MIN_MATCH-1;
- fill_window(s);
- @@ -336,12 +397,12 @@ int ZEXPORT deflateSetDictionary(z_stream *strm, const unsigned char *dictionary
- int ZEXPORT deflateResetKeep(z_stream *strm) {
- deflate_state *s;
- - if (strm == Z_NULL || strm->state == Z_NULL || strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
- + if (deflateStateCheck(strm)) {
- return Z_STREAM_ERROR;
- }
- strm->total_in = strm->total_out = 0;
- - strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
- + strm->msg = NULL; /* use zfree if we ever allocate msg dynamically */
- strm->data_type = Z_UNKNOWN;
- s = (deflate_state *)strm->state;
- @@ -351,12 +412,18 @@ int ZEXPORT deflateResetKeep(z_stream *strm) {
- if (s->wrap < 0) {
- s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
- }
- - s->status = s->wrap ? INIT_STATE : BUSY_STATE;
- + s->status =
- #ifdef GZIP
- - strm->adler = s->wrap == 2 ? crc32(0L, Z_NULL, 0) : adler32(0L, Z_NULL, 0);
- -#else
- - strm->adler = adler32(0L, Z_NULL, 0);
- + s->wrap == 2 ? GZIP_STATE :
- #endif
- + s->wrap ? INIT_STATE : BUSY_STATE;
- +
- +#ifdef GZIP
- + if (s->wrap == 2)
- + crc_reset(s);
- + else
- +#endif
- + strm->adler = adler32(0L, NULL, 0);
- s->last_flush = Z_NO_FLUSH;
- _tr_init(s);
- @@ -376,9 +443,7 @@ int ZEXPORT deflateReset(z_stream *strm) {
- /* ========================================================================= */
- int ZEXPORT deflateSetHeader(z_stream *strm, gz_headerp head) {
- - if (strm == Z_NULL || strm->state == Z_NULL)
- - return Z_STREAM_ERROR;
- - if (strm->state->wrap != 2)
- + if (deflateStateCheck(strm) || strm->state->wrap != 2)
- return Z_STREAM_ERROR;
- strm->state->gzhead = head;
- return Z_OK;
- @@ -386,11 +451,11 @@ int ZEXPORT deflateSetHeader(z_stream *strm, gz_headerp head) {
- /* ========================================================================= */
- int ZEXPORT deflatePending(z_stream *strm, uint32_t *pending, int *bits) {
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (deflateStateCheck(strm))
- return Z_STREAM_ERROR;
- - if (pending != Z_NULL)
- + if (pending != NULL)
- *pending = strm->state->pending;
- - if (bits != Z_NULL)
- + if (bits != NULL)
- *bits = strm->state->bi_valid;
- return Z_OK;
- }
- @@ -400,7 +465,7 @@ int ZEXPORT deflatePrime(z_stream *strm, int bits, int value) {
- deflate_state *s;
- int put;
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (deflateStateCheck(strm))
- return Z_STREAM_ERROR;
- s = strm->state;
- if ((unsigned char *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
- @@ -422,9 +487,8 @@ int ZEXPORT deflatePrime(z_stream *strm, int bits, int value) {
- int ZEXPORT deflateParams(z_stream *strm, int level, int strategy) {
- deflate_state *s;
- compress_func func;
- - int err = Z_OK;
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (deflateStateCheck(strm))
- return Z_STREAM_ERROR;
- s = strm->state;
- @@ -437,11 +501,20 @@ int ZEXPORT deflateParams(z_stream *strm, int level, int strategy) {
- if ((strategy != s->strategy || func != configuration_table[level].func)) {
- /* Flush the last buffer: */
- - err = deflate(strm, Z_BLOCK);
- - if (err == Z_BUF_ERROR && s->pending == 0)
- - err = Z_OK;
- + int err = deflate(strm, Z_BLOCK);
- + if (err == Z_STREAM_ERROR)
- + return err;
- + if (strm->avail_out == 0)
- + return Z_BUF_ERROR;
- }
- if (s->level != level) {
- + if (s->level == 0 && s->matches != 0) {
- + if (s->matches == 1)
- + slide_hash(s);
- + else
- + CLEAR_HASH(s);
- + s->matches = 0;
- + }
- s->level = level;
- s->max_lazy_match = configuration_table[level].max_lazy;
- s->good_match = configuration_table[level].good_length;
- @@ -449,14 +522,14 @@ int ZEXPORT deflateParams(z_stream *strm, int level, int strategy) {
- s->max_chain_length = configuration_table[level].max_chain;
- }
- s->strategy = strategy;
- - return err;
- + return Z_OK;
- }
- /* ========================================================================= */
- int ZEXPORT deflateTune(z_stream *strm, int good_length, int max_lazy, int nice_length, int max_chain) {
- deflate_state *s;
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (deflateStateCheck(strm))
- return Z_STREAM_ERROR;
- s = strm->state;
- s->good_match = good_length;
- @@ -486,13 +559,12 @@ int ZEXPORT deflateTune(z_stream *strm, int good_length, int max_lazy, int nice_
- unsigned long ZEXPORT deflateBound(z_stream *strm, unsigned long sourceLen) {
- deflate_state *s;
- unsigned long complen, wraplen;
- - unsigned char *str;
- /* conservative upper bound for compressed data */
- complen = sourceLen + ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
- /* if can't get parameters, return conservative bound plus zlib wrapper */
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (deflateStateCheck(strm))
- return complen + 6;
- /* compute wrapper length */
- @@ -504,20 +576,22 @@ unsigned long ZEXPORT deflateBound(z_stream *strm, unsigned long sourceLen) {
- case 1: /* zlib wrapper */
- wraplen = 6 + (s->strstart ? 4 : 0);
- break;
- +#ifdef GZIP
- case 2: /* gzip wrapper */
- wraplen = 18;
- - if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
- - if (s->gzhead->extra != Z_NULL) {
- + if (s->gzhead != NULL) { /* user-supplied gzip header */
- + unsigned char *str;
- + if (s->gzhead->extra != NULL) {
- wraplen += 2 + s->gzhead->extra_len;
- }
- str = s->gzhead->name;
- - if (str != Z_NULL) {
- + if (str != NULL) {
- do {
- wraplen++;
- } while (*str++);
- }
- str = s->gzhead->comment;
- - if (str != Z_NULL) {
- + if (str != NULL) {
- do {
- wraplen++;
- } while (*str++);
- @@ -526,6 +600,7 @@ unsigned long ZEXPORT deflateBound(z_stream *strm, unsigned long sourceLen) {
- wraplen += 2;
- }
- break;
- +#endif
- default: /* for compiler happiness */
- wraplen = 6;
- }
- @@ -543,16 +618,16 @@ unsigned long ZEXPORT deflateBound(z_stream *strm, unsigned long sourceLen) {
- * IN assertion: the stream state is correct and there is enough room in
- * pending_buf.
- */
- -local void putShortMSB(deflate_state *s, uint16_t b) {
- +static void putShortMSB(deflate_state *s, uint16_t b) {
- put_byte(s, (unsigned char)(b >> 8));
- put_byte(s, (unsigned char)(b & 0xff));
- }
- /* =========================================================================
- - * Flush as much pending output as possible. All deflate() output goes
- - * through this function so some applications may wish to modify it
- - * to avoid allocating a large strm->next_out buffer and copying into it.
- - * (See also read_buf()).
- + * Flush as much pending output as possible. All deflate() output, except for
- + * some deflate_stored() output, goes through this function so some
- + * applications may wish to modify it to avoid allocating a large
- + * strm->next_out buffer and copying into it. (See also read_buf()).
- */
- ZLIB_INTERNAL void flush_pending(z_stream *strm) {
- uint32_t len;
- @@ -569,24 +644,33 @@ ZLIB_INTERNAL void flush_pending(z_stream *strm) {
- strm->next_out += len;
- s->pending_out += len;
- strm->total_out += len;
- - strm->avail_out -= len;
- - s->pending -= len;
- + strm->avail_out -= len;
- + s->pending -= len;
- if (s->pending == 0) {
- s->pending_out = s->pending_buf;
- }
- }
- +/* ===========================================================================
- + * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].
- + */
- +#define HCRC_UPDATE(beg) \
- + do { \
- + if (s->gzhead->hcrc && s->pending > (beg)) \
- + strm->adler = crc32(strm->adler, s->pending_buf + (beg), s->pending - (beg)); \
- + } while (0)
- +
- /* ========================================================================= */
- int ZEXPORT deflate(z_stream *strm, int flush) {
- int old_flush; /* value of flush param for previous deflate call */
- deflate_state *s;
- - if (strm == Z_NULL || strm->state == Z_NULL || flush > Z_BLOCK || flush < 0) {
- + if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
- return Z_STREAM_ERROR;
- }
- s = strm->state;
- - if (strm->next_out == Z_NULL || (strm->avail_in != 0 && strm->next_in == Z_NULL) ||
- + if (strm->next_out == NULL || (strm->avail_in != 0 && strm->next_in == NULL) ||
- (s->status == FINISH_STATE && flush != Z_FINISH)) {
- ERR_RETURN(strm, Z_STREAM_ERROR);
- }
- @@ -597,203 +681,212 @@ int ZEXPORT deflate(z_stream *strm, int flush) {
- old_flush = s->last_flush;
- s->last_flush = flush;
- + /* Flush as much pending output as possible */
- + if (s->pending != 0) {
- + flush_pending(strm);
- + if (strm->avail_out == 0) {
- + /* Since avail_out is 0, deflate will be called again with
- + * more output space, but possibly with both pending and
- + * avail_in equal to zero. There won't be anything to do,
- + * but this is not an error situation so make sure we
- + * return OK instead of BUF_ERROR at next call of deflate:
- + */
- + s->last_flush = -1;
- + return Z_OK;
- + }
- +
- + /* Make sure there is something to do and avoid duplicate consecutive
- + * flushes. For repeated and useless calls with Z_FINISH, we keep
- + * returning Z_STREAM_END instead of Z_BUF_ERROR.
- + */
- + } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
- + flush != Z_FINISH) {
- + ERR_RETURN(strm, Z_BUF_ERROR);
- + }
- +
- + /* User must not provide more input after the first FINISH: */
- + if (s->status == FINISH_STATE && strm->avail_in != 0) {
- + ERR_RETURN(strm, Z_BUF_ERROR);
- + }
- +
- /* Write the header */
- if (s->status == INIT_STATE) {
- -#ifdef GZIP
- - if (s->wrap == 2) {
- - crc_reset(s);
- - put_byte(s, 31);
- - put_byte(s, 139);
- - put_byte(s, 8);
- - if (s->gzhead == Z_NULL) {
- - put_byte(s, 0);
- - put_byte(s, 0);
- - put_byte(s, 0);
- - put_byte(s, 0);
- - put_byte(s, 0);
- - put_byte(s, s->level == 9 ? 2 :
- - (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
- - 4 : 0));
- - put_byte(s, OS_CODE);
- - s->status = BUSY_STATE;
- - } else {
- - put_byte(s, (s->gzhead->text ? 1 : 0) +
- - (s->gzhead->hcrc ? 2 : 0) +
- - (s->gzhead->extra == Z_NULL ? 0 : 4) +
- - (s->gzhead->name == Z_NULL ? 0 : 8) +
- - (s->gzhead->comment == Z_NULL ? 0 : 16) );
- - put_byte(s, (unsigned char)(s->gzhead->time & 0xff));
- - put_byte(s, (unsigned char)((s->gzhead->time >> 8) & 0xff));
- - put_byte(s, (unsigned char)((s->gzhead->time >> 16) & 0xff));
- - put_byte(s, (unsigned char)((s->gzhead->time >> 24) & 0xff));
- - put_byte(s, s->level == 9 ? 2 :
- - (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
- - 4 : 0));
- - put_byte(s, s->gzhead->os & 0xff);
- - if (s->gzhead->extra != Z_NULL) {
- - put_byte(s, s->gzhead->extra_len & 0xff);
- - put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
- - }
- - if (s->gzhead->hcrc)
- - strm->adler = crc32(strm->adler, s->pending_buf, s->pending);
- - s->gzindex = 0;
- - s->status = EXTRA_STATE;
- - }
- - } else
- -#endif
- - {
- - unsigned int header = (Z_DEFLATED + ((s->w_bits-8) << 4)) << 8;
- - unsigned int level_flags;
- -
- - if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
- - level_flags = 0;
- - else if (s->level < 6)
- - level_flags = 1;
- - else if (s->level == 6)
- - level_flags = 2;
- - else
- - level_flags = 3;
- - header |= (level_flags << 6);
- - if (s->strstart != 0)
- - header |= PRESET_DICT;
- - header += 31 - (header % 31);
- + /* zlib header */
- + unsigned int header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
- + unsigned int level_flags;
- +
- + if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
- + level_flags = 0;
- + else if (s->level < TRIGGER_LEVEL)
- + level_flags = 1;
- + else if (s->level == TRIGGER_LEVEL)
- + level_flags = 2;
- + else
- + level_flags = 3;
- + header |= (level_flags << 6);
- + if (s->strstart != 0) header |= PRESET_DICT;
- + header += 31 - (header % 31);
- +
- + putShortMSB(s, header);
- +
- + /* Save the adler32 of the preset dictionary: */
- + if (s->strstart != 0) {
- + putShortMSB(s, (uint16_t)(strm->adler >> 16));
- + putShortMSB(s, (uint16_t)(strm->adler));
- + }
- + strm->adler = adler32(0L, NULL, 0);
- + s->status = BUSY_STATE;
- + /* Compression must start with an empty pending buffer */
- + flush_pending(strm);
- + if (s->pending != 0) {
- + s->last_flush = -1;
- + return Z_OK;
- + }
- + }
- +#ifdef GZIP
- + if (s->status == GZIP_STATE) {
- + /* gzip header */
- + crc_reset(s);
- + put_byte(s, 31);
- + put_byte(s, 139);
- + put_byte(s, 8);
- + if (s->gzhead == NULL) {
- + put_byte(s, 0);
- + put_byte(s, 0);
- + put_byte(s, 0);
- + put_byte(s, 0);
- + put_byte(s, 0);
- + put_byte(s, s->level == 9 ? 2 :
- + (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 4 : 0));
- + put_byte(s, OS_CODE);
- s->status = BUSY_STATE;
- - putShortMSB(s, header);
- - /* Save the adler32 of the preset dictionary: */
- - if (s->strstart != 0) {
- - putShortMSB(s, (uint16_t)(strm->adler >> 16));
- - putShortMSB(s, (uint16_t)strm->adler);
- + /* Compression must start with an empty pending buffer */
- + flush_pending(strm);
- + if (s->pending != 0) {
- + s->last_flush = -1;
- + return Z_OK;
- + }
- + }
- + else {
- + put_byte(s, (s->gzhead->text ? 1 : 0) +
- + (s->gzhead->hcrc ? 2 : 0) +
- + (s->gzhead->extra == NULL ? 0 : 4) +
- + (s->gzhead->name == NULL ? 0 : 8) +
- + (s->gzhead->comment == NULL ? 0 : 16)
- + );
- + put_byte(s, (unsigned char)(s->gzhead->time & 0xff));
- + put_byte(s, (unsigned char)((s->gzhead->time >> 8) & 0xff));
- + put_byte(s, (unsigned char)((s->gzhead->time >> 16) & 0xff));
- + put_byte(s, (unsigned char)((s->gzhead->time >> 24) & 0xff));
- + put_byte(s, s->level == 9 ? 2 :
- + (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 4 : 0));
- + put_byte(s, s->gzhead->os & 0xff);
- + if (s->gzhead->extra != NULL) {
- + put_byte(s, s->gzhead->extra_len & 0xff);
- + put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
- }
- - strm->adler = adler32(0L, Z_NULL, 0);
- + if (s->gzhead->hcrc)
- + strm->adler = crc32(strm->adler, s->pending_buf, s->pending);
- + s->gzindex = 0;
- + s->status = EXTRA_STATE;
- }
- }
- -#ifdef GZIP
- if (s->status == EXTRA_STATE) {
- if (s->gzhead->extra != Z_NULL) {
- - uint32_t beg = s->pending; /* start of bytes to update crc */
- -
- - while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
- - if (s->pending == s->pending_buf_size) {
- - if (s->gzhead->hcrc && s->pending > beg)
- - strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg);
- - flush_pending(strm);
- - beg = s->pending;
- - if (s->pending == s->pending_buf_size)
- - break;
- + uint32_t beg = s->pending; /* start of bytes to update crc */
- + uint32_t left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
- +
- + while (s->pending + left > s->pending_buf_size) {
- + uint32_t copy = s->pending_buf_size - s->pending;
- + memcpy(s->pending_buf + s->pending, s->gzhead->extra + s->gzindex, copy);
- + s->pending = s->pending_buf_size;
- + HCRC_UPDATE(beg);
- + s->gzindex += copy;
- + flush_pending(strm);
- + if (s->pending != 0) {
- + s->last_flush = -1;
- + return Z_OK;
- }
- - put_byte(s, s->gzhead->extra[s->gzindex]);
- - s->gzindex++;
- - }
- - if (s->gzhead->hcrc && s->pending > beg)
- - strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg);
- - if (s->gzindex == s->gzhead->extra_len) {
- - s->gzindex = 0;
- - s->status = NAME_STATE;
- + beg = 0;
- + left -= copy;
- }
- - } else {
- - s->status = NAME_STATE;
- + memcpy(s->pending_buf + s->pending, s->gzhead->extra + s->gzindex, left);
- + s->pending += left;
- + HCRC_UPDATE(beg);
- + s->gzindex = 0;
- }
- + s->status = NAME_STATE;
- }
- if (s->status == NAME_STATE) {
- - if (s->gzhead->name != Z_NULL) {
- - uint32_t beg = s->pending; /* start of bytes to update crc */
- + if (s->gzhead->name != NULL) {
- + uint32_t beg = s->pending; /* start of bytes to update crc */
- int val;
- do {
- if (s->pending == s->pending_buf_size) {
- - if (s->gzhead->hcrc && s->pending > beg)
- - strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg);
- + HCRC_UPDATE(beg);
- flush_pending(strm);
- - beg = s->pending;
- - if (s->pending == s->pending_buf_size) {
- - val = 1;
- - break;
- + if (s->pending != 0) {
- + s->last_flush = -1;
- + return Z_OK;
- }
- + beg = 0;
- }
- val = s->gzhead->name[s->gzindex++];
- put_byte(s, val);
- } while (val != 0);
- - if (s->gzhead->hcrc && s->pending > beg)
- - strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg);
- - if (val == 0) {
- - s->gzindex = 0;
- - s->status = COMMENT_STATE;
- - }
- - } else {
- - s->status = COMMENT_STATE;
- + HCRC_UPDATE(beg);
- + s->gzindex = 0;
- }
- + s->status = COMMENT_STATE;
- }
- if (s->status == COMMENT_STATE) {
- - if (s->gzhead->comment != Z_NULL) {
- + if (s->gzhead->comment != NULL) {
- uint32_t beg = s->pending; /* start of bytes to update crc */
- int val;
- do {
- if (s->pending == s->pending_buf_size) {
- - if (s->gzhead->hcrc && s->pending > beg)
- - strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg);
- + HCRC_UPDATE(beg);
- flush_pending(strm);
- - beg = s->pending;
- - if (s->pending == s->pending_buf_size) {
- - val = 1;
- - break;
- + if (s->pending != 0) {
- + s->last_flush = -1;
- + return Z_OK;
- }
- + beg = 0;
- }
- val = s->gzhead->comment[s->gzindex++];
- put_byte(s, val);
- } while (val != 0);
- - if (s->gzhead->hcrc && s->pending > beg)
- - strm->adler = crc32(strm->adler, s->pending_buf + beg, s->pending - beg);
- - if (val == 0)
- - s->status = HCRC_STATE;
- - } else {
- - s->status = HCRC_STATE;
- + HCRC_UPDATE(beg);
- }
- + s->status = HCRC_STATE;
- }
- if (s->status == HCRC_STATE) {
- if (s->gzhead->hcrc) {
- - if (s->pending + 2 > s->pending_buf_size)
- + if (s->pending + 2 > s->pending_buf_size) {
- flush_pending(strm);
- - if (s->pending + 2 <= s->pending_buf_size) {
- - put_byte(s, (unsigned char)(strm->adler & 0xff));
- - put_byte(s, (unsigned char)((strm->adler >> 8) & 0xff));
- - strm->adler = crc32(0L, Z_NULL, 0);
- - s->status = BUSY_STATE;
- + if (s->pending != 0) {
- + s->last_flush = -1;
- + return Z_OK;
- + }
- }
- - } else {
- - s->status = BUSY_STATE;
- + put_byte(s, (unsigned char)(strm->adler & 0xff));
- + put_byte(s, (unsigned char)((strm->adler >> 8) & 0xff));
- + crc_reset(s);
- }
- - }
- -#endif
- + s->status = BUSY_STATE;
- - /* Flush as much pending output as possible */
- - if (s->pending != 0) {
- + /* Compression must start with an empty pending buffer */
- flush_pending(strm);
- - if (strm->avail_out == 0) {
- - /* Since avail_out is 0, deflate will be called again with
- - * more output space, but possibly with both pending and
- - * avail_in equal to zero. There won't be anything to do,
- - * but this is not an error situation so make sure we
- - * return OK instead of BUF_ERROR at next call of deflate:
- - */
- + if (s->pending != 0) {
- s->last_flush = -1;
- return Z_OK;
- }
- -
- - /* Make sure there is something to do and avoid duplicate consecutive
- - * flushes. For repeated and useless calls with Z_FINISH, we keep
- - * returning Z_STREAM_END instead of Z_BUF_ERROR.
- - */
- - } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && flush != Z_FINISH) {
- - ERR_RETURN(strm, Z_BUF_ERROR);
- - }
- -
- - /* User must not provide more input after the first FINISH: */
- - if (s->status == FINISH_STATE && strm->avail_in != 0) {
- - ERR_RETURN(strm, Z_BUF_ERROR);
- }
- +#endif
- /* Start a new block or continue the current one.
- */
- @@ -806,7 +899,8 @@ int ZEXPORT deflate(z_stream *strm, int flush) {
- (s->strategy == Z_RLE ? deflate_rle(s, flush) : deflate_fast(s, flush));
- else
- #endif
- - bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
- + bstate = s->level == 0 ? deflate_stored(s, flush) :
- + s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
- (s->strategy == Z_RLE ? deflate_rle(s, flush) : (*(configuration_table[s->level].func))(s, flush));
- if (bstate == finish_started || bstate == finish_done) {
- @@ -889,19 +983,10 @@ int ZEXPORT deflate(z_stream *strm, int flush) {
- int ZEXPORT deflateEnd(z_stream *strm) {
- int status;
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (deflateStateCheck(strm))
- return Z_STREAM_ERROR;
- status = strm->state->status;
- - if (status != INIT_STATE &&
- - status != EXTRA_STATE &&
- - status != NAME_STATE &&
- - status != COMMENT_STATE &&
- - status != HCRC_STATE &&
- - status != BUSY_STATE &&
- - status != FINISH_STATE) {
- - return Z_STREAM_ERROR;
- - }
- /* Deallocate in reverse order of allocations: */
- TRY_FREE(strm, strm->state->pending_buf);
- @@ -910,7 +995,7 @@ int ZEXPORT deflateEnd(z_stream *strm) {
- TRY_FREE(strm, strm->state->window);
- ZFREE(strm, strm->state);
- - strm->state = Z_NULL;
- + strm->state = NULL;
- return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
- }
- @@ -923,7 +1008,7 @@ int ZEXPORT deflateCopy(z_stream *dest, z_stream *source) {
- deflate_state *ss;
- uint16_t *overlay;
- - if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
- + if (deflateStateCheck(source) || dest == NULL) {
- return Z_STREAM_ERROR;
- }
- @@ -932,7 +1017,7 @@ int ZEXPORT deflateCopy(z_stream *dest, z_stream *source) {
- memcpy((void *)dest, (void *)source, sizeof(z_stream));
- ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
- - if (ds == Z_NULL)
- + if (ds == NULL)
- return Z_MEM_ERROR;
- dest->state = (struct internal_state *) ds;
- memcpy((void *)ds, (void *)ss, sizeof(deflate_state));
- @@ -944,7 +1029,7 @@ int ZEXPORT deflateCopy(z_stream *dest, z_stream *source) {
- overlay = (uint16_t *) ZALLOC(dest, ds->lit_bufsize, sizeof(uint16_t)+2);
- ds->pending_buf = (unsigned char *) overlay;
- - if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || ds->pending_buf == Z_NULL) {
- + if (ds->window == NULL || ds->prev == NULL || ds->head == NULL || ds->pending_buf == NULL) {
- deflateEnd(dest);
- return Z_MEM_ERROR;
- }
- @@ -1001,7 +1086,7 @@ ZLIB_INTERNAL int read_buf(z_stream *strm, unsigned char *buf, unsigned size) {
- /* ===========================================================================
- * Initialize the "longest match" routines for a new zlib stream
- */
- -local void lm_init(deflate_state *s) {
- +static void lm_init(deflate_state *s) {
- s->window_size = (unsigned long)2L*s->w_size;
- CLEAR_HASH(s);
- @@ -1022,7 +1107,10 @@ local void lm_init(deflate_state *s) {
- s->ins_h = 0;
- }
- -#ifdef DEBUG
- +#ifdef ZLIB_DEBUG
- +#define EQUAL 0
- +/* result of memcmp for equal strings */
- +
- /* ===========================================================================
- * Check that the match at match_start is indeed a match.
- */
- @@ -1044,7 +1132,7 @@ void check_match(deflate_state *s, IPos start, IPos match, int length) {
- }
- #else
- # define check_match(s, start, match, length)
- -#endif /* DEBUG */
- +#endif /* ZLIB_DEBUG */
- /* ===========================================================================
- * Fill the window when the lookahead becomes insufficient.
- @@ -1079,81 +1167,24 @@ void fill_window(deflate_state *s) {
- }
- void fill_window_c(deflate_state *s) {
- - register unsigned n;
- - register Pos *p;
- - unsigned more; /* Amount of free space at the end of the window. */
- + unsigned n;
- + unsigned long more; /* Amount of free space at the end of the window. */
- unsigned int wsize = s->w_size;
- Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
- do {
- - more = (unsigned)(s->window_size -(unsigned long)s->lookahead -(unsigned long)s->strstart);
- + more = s->window_size - s->lookahead - s->strstart;
- /* If the window is almost full and there is insufficient lookahead,
- * move the upper half to the lower one to make room in the upper half.
- */
- if (s->strstart >= wsize+MAX_DIST(s)) {
- - memcpy(s->window, s->window+wsize, (unsigned)wsize);
- + memcpy(s->window, s->window+wsize, wsize - more);
- s->match_start -= wsize;
- s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
- s->block_start -= (long) wsize;
- -
- - /* Slide the hash table (could be avoided with 32 bit values
- - at the expense of memory usage). We slide even when level == 0
- - to keep the hash table consistent if we switch back to level > 0
- - later. (Using level 0 permanently is not an optimal usage of
- - zlib, so we don't care about this pathological case.)
- - */
- - n = s->hash_size;
- - p = &s->head[n];
- -#ifdef NOT_TWEAK_COMPILER
- - do {
- - unsigned m;
- - m = *--p;
- - *p = (Pos)(m >= wsize ? m-wsize : NIL);
- - } while (--n);
- -#else
- - /* As of I make this change, gcc (4.8.*) isn't able to vectorize
- - * this hot loop using saturated-subtraction on x86-64 architecture.
- - * To avoid this defect, we can change the loop such that
- - * o. the pointer advance forward, and
- - * o. demote the variable 'm' to be local to the loop, and
- - * choose type "Pos" (instead of 'unsigned int') for the
- - * variable to avoid unncessary zero-extension.
- - */
- - {
- - unsigned int i;
- - Pos *q = p - n;
- - for (i = 0; i < n; i++) {
- - Pos m = *q;
- - Pos t = wsize;
- - *q++ = (Pos)(m >= t ? m-t: NIL);
- - }
- - }
- -
- -#endif /* NOT_TWEAK_COMPILER */
- - n = wsize;
- - p = &s->prev[n];
- -#ifdef NOT_TWEAK_COMPILER
- - do {
- - unsigned m;
- - m = *--p;
- - *p = (Pos)(m >= wsize ? m-wsize : NIL);
- - /* If n is not on any hash chain, prev[n] is garbage but
- - * its value will never be used.
- - */
- - } while (--n);
- -#else
- - {
- - unsigned int i;
- - Pos *q = p - n;
- - for (i = 0; i < n; i++) {
- - Pos m = *q;
- - Pos t = wsize;
- - *q++ = (Pos)(m >= t ? m-t: NIL);
- - }
- - }
- -#endif /* NOT_TWEAK_COMPILER */
- + slide_hash(s);
- more += wsize;
- }
- if (s->strm->avail_in == 0)
- @@ -1179,20 +1210,38 @@ void fill_window_c(deflate_state *s) {
- if (s->lookahead + s->insert >= MIN_MATCH) {
- unsigned int str = s->strstart - s->insert;
- s->ins_h = s->window[str];
- +#ifndef NOT_TWEAK_COMPILER
- + {
- + unsigned int insert_cnt = s->insert;
- + unsigned int slen;
- + if (unlikely(s->lookahead < MIN_MATCH))
- + insert_cnt += s->lookahead - MIN_MATCH;
- + slen = insert_cnt;
- + if (str >= (MIN_MATCH - 2))
- + {
- + str += 2 - MIN_MATCH;
- + insert_cnt += MIN_MATCH - 2;
- + }
- + if (insert_cnt > 0)
- + {
- + insert_string(s, str, insert_cnt);
- + s->insert -= slen;
- + }
- + }
- +#else
- if (str >= 1)
- - UPDATE_HASH(s, s->ins_h, str + 1 - (MIN_MATCH-1));
- + insert_string(s, str + 2 - MIN_MATCH, 1);
- #if MIN_MATCH != 3
- - Call UPDATE_HASH() MIN_MATCH-3 more times
- +#warning Call insert_string() MIN_MATCH-3 more times
- #endif
- while (s->insert) {
- - UPDATE_HASH(s, s->ins_h, str);
- - s->prev[str & s->w_mask] = s->head[s->ins_h];
- - s->head[s->ins_h] = (Pos)str;
- + insert_string(s, str, 1);
- str++;
- s->insert--;
- if (s->lookahead + s->insert < MIN_MATCH)
- break;
- }
- +#endif
- }
- /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
- * but this is not important since only literal bytes will be emitted.
- @@ -1207,7 +1256,7 @@ void fill_window_c(deflate_state *s) {
- * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
- */
- if (s->high_water < s->window_size) {
- - unsigned long curr = s->strstart + (unsigned long)(s->lookahead);
- + unsigned long curr = s->strstart + (unsigned long)s->lookahead;
- unsigned long init;
- if (s->high_water < curr) {
- @@ -1217,17 +1266,18 @@ void fill_window_c(deflate_state *s) {
- init = s->window_size - curr;
- if (init > WIN_INIT)
- init = WIN_INIT;
- - memset(s->window + curr, 0, (unsigned)init);
- + memset(s->window + curr, 0, init);
- s->high_water = curr + init;
- - } else if (s->high_water < (unsigned long)curr + WIN_INIT) {
- + } else if (s->high_water < curr + WIN_INIT) {
- /* High water mark at or above current data, but below current data
- * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
- * to end of window, whichever is less.
- */
- - init = (unsigned long)curr + WIN_INIT - s->high_water;
- - if (init > s->window_size - s->high_water)
- - init = s->window_size - s->high_water;
- - memset(s->window + s->high_water, 0, (unsigned)init);
- + init = curr + WIN_INIT;
- + if (init > s->window_size)
- + init = s->window_size;
- + init -= s->high_water;
- + memset(s->window + s->high_water, 0, init);
- s->high_water += init;
- }
- }
- @@ -1236,67 +1286,190 @@ void fill_window_c(deflate_state *s) {
- "not enough room for search");
- }
- +/* Maximum stored block length in deflate format (not including header). */
- +#define MAX_STORED 65535
- +
- +/* Minimum of a and b. */
- +#define MIN(a, b) ((a) > (b) ? (b) : (a))
- +
- /* ===========================================================================
- * Copy without compression as much as possible from the input stream, return
- * the current block state.
- - * This function does not insert new strings in the dictionary since
- - * uncompressible data is probably not useful. This function is used
- - * only for the level=0 compression option.
- - * NOTE: this function should be optimized to avoid extra copying from
- - * window to pending_buf.
- + *
- + * In case deflateParams() is used to later switch to a non-zero compression
- + * level, s->matches (otherwise unused when storing) keeps track of the number
- + * of hash table slides to perform. If s->matches is 1, then one hash table
- + * slide will be done when switching. If s->matches is 2, the maximum value
- + * allowed here, then the hash table will be cleared, since two or more slides
- + * is the same as a clear.
- + *
- + * deflate_stored() is written to minimize the number of times an input byte is
- + * copied. It is most efficient with large input and output buffers, which
- + * maximizes the opportunites to have a single copy from next_in to next_out.
- */
- -local block_state deflate_stored(deflate_state *s, int flush) {
- - /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
- - * to pending_buf_size, and each stored block has a 5 byte header:
- +static block_state deflate_stored(deflate_state *s, int flush) {
- + /* Smallest worthy block size when not flushing or finishing. By default
- + * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
- + * large input and output buffers, the stored block size will be larger.
- */
- - unsigned long max_block_size = 0xffff;
- - unsigned long max_start;
- + unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
- - if (max_block_size > s->pending_buf_size - 5) {
- - max_block_size = (uint32_t)(s->pending_buf_size - 5);
- - }
- -
- - /* Copy as much as possible from input to output: */
- + /* Copy as many min_block or larger stored blocks directly to next_out as
- + * possible. If flushing, copy the remaining available input to next_out as
- + * stored blocks, if there is enough space.
- + */
- + unsigned len, left, have, last;
- + unsigned used = s->strm->avail_in;
- for (;;) {
- - /* Fill the window as much as possible: */
- - if (s->lookahead <= 1) {
- - Assert(s->strstart < s->w_size+MAX_DIST(s) || s->block_start >= (long)s->w_size, "slide too late");
- + /* Set len to the maximum size block that we can copy directly with the
- + * available input data and output space. Set left to how much of that
- + * would be copied from what's left in the window.
- + */
- + len = MAX_STORED; /* maximum deflate stored block length */
- + have = (s->bi_valid + 42) >> 3; /* number of header bytes */
- + /* maximum stored block length that will fit in avail_out: */
- + have = s->strm->avail_out > have ? s->strm->avail_out - have : 0;
- + left = s->strstart - s->block_start; /* bytes left in window */
- + if (len > (unsigned long)left + s->strm->avail_in)
- + len = left + s->strm->avail_in; /* limit len to the input */
- + if (len > have)
- + len = have; /* limit len to the output */
- + if (left > len)
- + left = len; /* limit window pull to len */
- +
- + /* If the stored block would be less than min_block in length, or if
- + * unable to copy all of the available input when flushing, then try
- + * copying to the window and the pending buffer instead. Also don't
- + * write an empty block when flushing -- deflate() does that.
- + */
- + if (len < min_block && (len == 0 || flush == Z_NO_FLUSH ||
- + len - left != s->strm->avail_in))
- + break;
- - fill_window(s);
- - if (s->lookahead == 0 && flush == Z_NO_FLUSH)
- - return need_more;
- + /* Make a dummy stored block in pending to get the header bytes,
- + * including any pending bits. This also updates the debugging counts.
- + */
- + last = flush == Z_FINISH && len - left == s->strm->avail_in ? 1 : 0;
- + _tr_stored_block(s, (char *)0, 0L, last);
- +
- + /* Replace the lengths in the dummy stored block with len. */
- + s->pending_buf[s->pending - 4] = len;
- + s->pending_buf[s->pending - 3] = len >> 8;
- + s->pending_buf[s->pending - 2] = ~len;
- + s->pending_buf[s->pending - 1] = ~len >> 8;
- +
- + /* Write the stored block header bytes. */
- + flush_pending(s->strm);
- +
- + /* Update debugging counts for the data about to be copied. */
- +#ifdef ZLIB_DEBUG
- + s->compressed_len += len << 3;
- + s->bits_sent += len << 3;
- +#endif
- - if (s->lookahead == 0)
- - break; /* flush the current block */
- + /* Copy uncompressed bytes from the window to next_out. */
- + if (left) {
- + memcpy(s->strm->next_out, s->window + s->block_start, left);
- + s->strm->next_out += left;
- + s->strm->avail_out -= left;
- + s->strm->total_out += left;
- + s->block_start += left;
- + len -= left;
- }
- - Assert(s->block_start >= 0L, "block gone");
- - s->strstart += s->lookahead;
- - s->lookahead = 0;
- -
- - /* Emit a stored block if pending_buf will be full: */
- - max_start = s->block_start + max_block_size;
- - if (s->strstart == 0 || (unsigned long)s->strstart >= max_start) {
- - /* strstart == 0 is possible when wraparound on 16-bit machine */
- - s->lookahead = (unsigned int)(s->strstart - max_start);
- - s->strstart = (unsigned int)max_start;
- - FLUSH_BLOCK(s, 0);
- + /* Copy uncompressed bytes directly from next_in to next_out, updating
- + * the check value.
- + */
- + if (len) {
- + read_buf(s->strm, s->strm->next_out, len);
- + s->strm->next_out += len;
- + s->strm->avail_out -= len;
- + s->strm->total_out += len;
- }
- - /* Flush if we may have to slide, otherwise block_start may become
- - * negative and the data will be gone:
- + }
- +
- + /* Update the sliding window with the last s->w_size bytes of the copied
- + * data, or append all of the copied data to the existing window if less
- + * than s->w_size bytes were copied. Also update the number of bytes to
- + * insert in the hash tables, in the event that deflateParams() switches to
- + * a non-zero compression level.
- + */
- + used -= s->strm->avail_in; /* number of input bytes directly copied */
- + if (used) {
- + /* If any input was used, then no unused input remains in the window,
- + * therefore s->block_start == s->strstart.
- */
- - if (s->strstart - (unsigned int)s->block_start >= MAX_DIST(s)) {
- - FLUSH_BLOCK(s, 0);
- + if (used >= s->w_size) { /* supplant the previous history */
- + s->matches = 2; /* clear hash */
- + memcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
- + s->strstart = s->w_size;
- + }
- + else {
- + if (s->window_size - s->strstart <= used) {
- + /* Slide the window down. */
- + s->strstart -= s->w_size;
- + memcpy(s->window, s->window + s->w_size, s->strstart);
- + if (s->matches < 2)
- + s->matches++; /* add a pending slide_hash() */
- + }
- + memcpy(s->window + s->strstart, s->strm->next_in - used, used);
- + s->strstart += used;
- }
- + s->block_start = s->strstart;
- + s->insert += MIN(used, s->w_size - s->insert);
- }
- - s->insert = 0;
- - if (flush == Z_FINISH) {
- - FLUSH_BLOCK(s, 1);
- - return finish_done;
- +
- + /* If flushing or finishing and all input has been consumed, then done. If
- + * the code above couldn't write a complete block to next_out, then the
- + * code following this won't be able to either.
- + */
- + if (flush != Z_NO_FLUSH && s->strm->avail_in == 0 &&
- + s->strstart == s->block_start)
- + return flush == Z_FINISH ? finish_done : block_done;
- +
- + /* Fill the window with any remaining input. */
- + have = s->window_size - s->strstart - 1;
- + if (s->strm->avail_in > have && s->block_start >= (long) s->w_size) {
- + /* Slide the window down. */
- + s->block_start -= s->w_size;
- + s->strstart -= s->w_size;
- + memcpy(s->window, s->window + s->w_size, s->strstart);
- + if (s->matches < 2)
- + s->matches++; /* add a pending slide_hash() */
- + have += s->w_size; /* more space now */
- }
- - if ((long)s->strstart > s->block_start)
- - FLUSH_BLOCK(s, 0);
- - return block_done;
- + if (have > s->strm->avail_in)
- + have = s->strm->avail_in;
- + if (have) {
- + read_buf(s->strm, s->window + s->strstart, have);
- + s->strstart += have;
- + }
- +
- + /* There was not enough avail_out to write a complete worthy or flushed
- + * stored block to next_out. Write a stored block to pending instead, if we
- + * have enough input for a worthy block, or if flushing and there is enough
- + * room for the remaining input as a stored block in the pending buffer.
- + */
- + have = (s->bi_valid + 42) >> 3; /* number of header bytes */
- + /* maximum stored block length that will fit in pending: */
- + have = MIN(s->pending_buf_size - have, MAX_STORED);
- + min_block = MIN(have, s->w_size);
- + left = s->strstart - s->block_start;
- + if (left >= min_block ||
- + (left && flush != Z_NO_FLUSH && s->strm->avail_in == 0 &&
- + left <= have)) {
- + len = MIN(left, have);
- + last = flush == Z_FINISH && s->strm->avail_in == 0 &&
- + len == left ? 1 : 0;
- + _tr_stored_block(s, (charf *)s->window + s->block_start, len, last);
- + s->block_start += len;
- + flush_pending(s->strm);
- + if (last)
- + return finish_started;
- + }
- +
- + /* We've done all we can with the available input and output. */
- + return need_more;
- }
- @@ -1305,7 +1478,7 @@ local block_state deflate_stored(deflate_state *s, int flush) {
- * one. Do not maintain a hash table. (It will be regenerated if this run of
- * deflate switches away from Z_RLE.)
- */
- -local block_state deflate_rle(deflate_state *s, int flush) {
- +static block_state deflate_rle(deflate_state *s, int flush) {
- int bflush; /* set if current block must be flushed */
- unsigned int prev; /* byte at distance one to match */
- unsigned char *scan, *strend; /* scan goes up to strend for length of run */
- @@ -1377,7 +1550,7 @@ local block_state deflate_rle(deflate_state *s, int flush) {
- * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
- * (It will be regenerated if this run of deflate switches away from Huffman.)
- */
- -local block_state deflate_huff(deflate_state *s, int flush) {
- +static block_state deflate_huff(deflate_state *s, int flush) {
- int bflush; /* set if current block must be flushed */
- for (;;) {
- @@ -1409,3 +1582,29 @@ local block_state deflate_huff(deflate_state *s, int flush) {
- FLUSH_BLOCK(s, 0);
- return block_done;
- }
- +
- +#ifdef ZLIB_DEBUG
- +/* ===========================================================================
- + * Send a value on a given number of bits.
- + * IN assertion: length <= 16 and value fits in length bits.
- + */
- +void send_bits(deflate_state *s, int value, int length) {
- + Tracevv((stderr, " l %2d v %4x ", length, value));
- + Assert(length > 0 && length <= 15, "invalid length");
- + s->bits_sent += (unsigned long)length;
- +
- + /* If not enough room in bi_buf, use (valid) bits from bi_buf and
- + * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
- + * unused bits in value.
- + */
- + if (s->bi_valid > (int)Buf_size - length) {
- + s->bi_buf |= (uint16_t)value << s->bi_valid;
- + put_short(s, s->bi_buf);
- + s->bi_buf = (uint16_t)value >> (Buf_size - s->bi_valid);
- + s->bi_valid += length - Buf_size;
- + } else {
- + s->bi_buf |= (uint16_t)value << s->bi_valid;
- + s->bi_valid += length;
- + }
- +}
- +#endif
- diff --git a/deflate.h b/deflate.h
- index 2d32026..62d1fd8 100644
- --- a/deflate.h
- +++ b/deflate.h
- @@ -57,13 +57,16 @@
- #define END_BLOCK 256
- /* end of block literal code */
- -#define INIT_STATE 42
- -#define EXTRA_STATE 69
- -#define NAME_STATE 73
- -#define COMMENT_STATE 91
- -#define HCRC_STATE 103
- -#define BUSY_STATE 113
- -#define FINISH_STATE 666
- +#define INIT_STATE 42 /* zlib header -> BUSY_STATE */
- +#ifdef GZIP
- +# define GZIP_STATE 57 /* gzip header -> BUSY_STATE | EXTRA_STATE */
- +#endif
- +#define EXTRA_STATE 69 /* gzip extra block -> NAME_STATE */
- +#define NAME_STATE 73 /* gzip file name -> COMMENT_STATE */
- +#define COMMENT_STATE 91 /* gzip comment -> HCRC_STATE */
- +#define HCRC_STATE 103 /* gzip header CRC -> BUSY_STATE */
- +#define BUSY_STATE 113 /* deflate -> FINISH_STATE */
- +#define FINISH_STATE 666 /* stream complete */
- /* Stream status */
- @@ -87,8 +90,8 @@ typedef struct ct_data_s {
- typedef struct static_tree_desc_s static_tree_desc;
- typedef struct tree_desc_s {
- - ct_data *dyn_tree; /* the dynamic tree */
- - int max_code; /* largest code with non zero frequency */
- + ct_data *dyn_tree; /* the dynamic tree */
- + int max_code; /* largest code with non zero frequency */
- const static_tree_desc *stat_desc; /* the corresponding static tree */
- } tree_desc;
- @@ -101,14 +104,14 @@ typedef unsigned IPos;
- typedef struct internal_state {
- z_stream *strm; /* pointer back to this zlib stream */
- - int status; /* as the name implies */
- + int status; /* as the name implies */
- unsigned char *pending_buf; /* output still pending */
- - unsigned long pending_buf_size; /* size of pending_buf */
- + unsigned long pending_buf_size; /* size of pending_buf */
- unsigned char *pending_out; /* next pending byte to output to the stream */
- - unsigned int pending; /* nb of bytes in the pending buffer */
- + uint32_t pending; /* nb of bytes in the pending buffer */
- int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
- gz_headerp gzhead; /* gzip header information to write */
- - unsigned int gzindex; /* where in extra, name, or comment */
- + uint32_t gzindex; /* where in extra, name, or comment */
- unsigned char method; /* can only be DEFLATED */
- int last_flush; /* value of flush param for previous deflate call */
- @@ -258,7 +261,7 @@ typedef struct internal_state {
- unsigned int matches; /* number of string matches in current block */
- unsigned int insert; /* bytes at end of window left to insert */
- -#ifdef DEBUG
- +#ifdef ZLIB_DEBUG
- unsigned long compressed_len; /* total bit length of compressed file mod 2^32 */
- unsigned long bits_sent; /* bit length of compressed data sent mod 2^32 */
- #endif
- @@ -278,6 +281,12 @@ typedef struct internal_state {
- * longest match routines access bytes past the input. This is then
- * updated to the new high water mark.
- */
- + int block_open;
- + /* Whether or not a block is currently open for the QUICK deflation scheme.
- + * This is set to 1 if there is an active block, or 0 if the block was just
- + * closed.
- + */
- +
- } deflate_state;
- typedef enum {
- @@ -304,7 +313,7 @@ typedef enum {
- * - no instructions for extracting bytes from short.
- * - needs less registers
- * - stores to adjacent bytes are merged into a single store, albeit at the
- - * cost of penalty of potentially unaligned access.
- + * cost of penalty of potentially unaligned access.
- */
- #define put_short(s, w) { \
- s->pending += 2; \
- @@ -346,7 +355,7 @@ void ZLIB_INTERNAL bi_windup(deflate_state *s);
- * used.
- */
- -#ifndef DEBUG
- +#ifndef ZLIB_DEBUG
- /* Inline versions of _tr_tally for speed: */
- # if defined(GEN_TREES_H)
- @@ -386,10 +395,17 @@ void ZLIB_INTERNAL bi_windup(deflate_state *s);
- * input characters, so that a running hash key can be computed from the
- * previous key instead of complete recalculation each time.
- */
- +
- +#ifdef NOT_TWEAK_COMPILER
- +#define TRIGGER_LEVEL 6
- +#else
- +#define TRIGGER_LEVEL 5
- +#endif
- +
- #ifdef X86_SSE4_2_CRC_HASH
- #define UPDATE_HASH(s, h, i) \
- do {\
- - if (s->level < 6) \
- + if (s->level < TRIGGER_LEVEL) \
- h = (3483 * (s->window[i]) +\
- 23081* (s->window[i+1]) +\
- 6954 * (s->window[i+2]) +\
- @@ -400,14 +416,14 @@ void ZLIB_INTERNAL bi_windup(deflate_state *s);
- 25811* (s->window[i+2])) & s->hash_mask;\
- } while (0)
- #else
- -# define UPDATE_HASH(s, h, i) (h = (((h) << s->hash_shift) ^ (s->window[i + (MIN_MATCH-1)])) & s->hash_mask)
- +# define UPDATE_HASH(s, h, i) (h = ((h << s->hash_shift) ^ (s->window[i + (MIN_MATCH-1)])) & s->hash_mask)
- #endif
- -#ifndef DEBUG
- +#ifndef ZLIB_DEBUG
- # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
- /* Send a code of the given tree. c and tree must not have side effects */
- -#else /* DEBUG */
- +#else /* ZLIB_DEBUG */
- # define send_code(s, c, tree) \
- { if (z_verbose > 2) { \
- fprintf(stderr, "\ncd %3d ", (c)); \
- @@ -416,30 +432,8 @@ void ZLIB_INTERNAL bi_windup(deflate_state *s);
- }
- #endif
- -#ifdef DEBUG
- -/* ===========================================================================
- - * Send a value on a given number of bits.
- - * IN assertion: length <= 16 and value fits in length bits.
- - */
- -local void send_bits(deflate_state *s, int value, int length) {
- - Tracevv((stderr, " l %2d v %4x ", length, value));
- - Assert(length > 0 && length <= 15, "invalid length");
- - s->bits_sent += (unsigned long)length;
- -
- - /* If not enough room in bi_buf, use (valid) bits from bi_buf and
- - * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
- - * unused bits in value.
- - */
- - if (s->bi_valid > (int)Buf_size - length) {
- - s->bi_buf |= (uint16_t)value << s->bi_valid;
- - put_short(s, s->bi_buf);
- - s->bi_buf = (uint16_t)value >> (Buf_size - s->bi_valid);
- - s->bi_valid += length - Buf_size;
- - } else {
- - s->bi_buf |= (uint16_t)value << s->bi_valid;
- - s->bi_valid += length;
- - }
- -}
- +#ifdef ZLIB_DEBUG
- +void send_bits(deflate_state *s, int value, int length);
- #else
- #define send_bits(s, value, length) \
- { int len = length;\
- diff --git a/deflate_fast.c b/deflate_fast.c
- index edfe53d..c169053 100644
- --- a/deflate_fast.c
- +++ b/deflate_fast.c
- @@ -39,7 +39,7 @@ block_state deflate_fast(deflate_state *s, int flush) {
- */
- hash_head = NIL;
- if (s->lookahead >= MIN_MATCH) {
- - hash_head = insert_string(s, s->strstart);
- + hash_head = insert_string(s, s->strstart, 1);
- }
- /* Find the longest match, discarding those <= prev_length.
- @@ -68,7 +68,7 @@ block_state deflate_fast(deflate_state *s, int flush) {
- s->strstart++;
- #ifdef NOT_TWEAK_COMPILER
- do {
- - insert_string(s, s->strstart);
- + insert_string(s, s->strstart, 1);
- s->strstart++;
- /* strstart never exceeds WSIZE-MAX_MATCH, so there are
- * always MIN_MATCH bytes ahead.
- @@ -76,7 +76,7 @@ block_state deflate_fast(deflate_state *s, int flush) {
- } while (--s->match_length != 0);
- #else
- {
- - bulk_insert_str(s, s->strstart, s->match_length);
- + insert_string(s, s->strstart, s->match_length);
- s->strstart += s->match_length;
- s->match_length = 0;
- }
- @@ -85,9 +85,13 @@ block_state deflate_fast(deflate_state *s, int flush) {
- s->strstart += s->match_length;
- s->match_length = 0;
- s->ins_h = s->window[s->strstart];
- - UPDATE_HASH(s, s->ins_h, s->strstart+2 - (MIN_MATCH));
- +#ifndef NOT_TWEAK_COMPILER
- + insert_string(s, s->strstart + 2 - MIN_MATCH, MIN_MATCH - 2);
- +#else
- + insert_string(s, s->strstart + 2 - MIN_MATCH, 1);
- #if MIN_MATCH != 3
- - Call UPDATE_HASH() MIN_MATCH-3 more times
- +#warning Call insert_string() MIN_MATCH-3 more times
- +#endif
- #endif
- /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
- * matter since it will be recomputed at next deflate call.
- diff --git a/deflate_medium.c b/deflate_medium.c
- index 731b8a2..046449f 100644
- --- a/deflate_medium.c
- +++ b/deflate_medium.c
- @@ -63,22 +63,23 @@ static void insert_match(deflate_state *s, struct match match) {
- if (match.match_length) {
- if (match.strstart >= match.orgstart) {
- - insert_string(s, match.strstart);
- + insert_string(s, match.strstart, 1);
- }
- }
- }
- #else
- - if (likely(match.match_length == 1)) {
- - match.strstart++;
- - match.match_length = 0;
- - }else{
- - match.strstart++;
- - match.match_length--;
- + match.strstart++;
- + match.match_length--;
- + if (match.match_length > 0) {
- if (match.strstart >= match.orgstart) {
- - bulk_insert_str(s, match.strstart, match.match_length);
- + if (match.strstart + match.match_length - 1 >= match.orgstart) {
- + insert_string(s, match.strstart, match.match_length);
- + } else {
- + insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
- + }
- + match.strstart += match.match_length;
- + match.match_length = 0;
- }
- - match.strstart += match.match_length;
- - match.match_length = 0;
- }
- #endif
- return;
- @@ -93,7 +94,7 @@ static void insert_match(deflate_state *s, struct match match) {
- #ifdef NOT_TWEAK_COMPILER
- do {
- if (likely(match.strstart >= match.orgstart)) {
- - insert_string(s, match.strstart);
- + insert_string(s, match.strstart, 1);
- }
- match.strstart++;
- /* strstart never exceeds WSIZE-MAX_MATCH, so there are
- @@ -102,7 +103,11 @@ static void insert_match(deflate_state *s, struct match match) {
- } while (--match.match_length != 0);
- #else
- if (likely(match.strstart >= match.orgstart)) {
- - bulk_insert_str(s, match.strstart, match.match_length);
- + if (likely(match.strstart + match.match_length - 1 >= match.orgstart)) {
- + insert_string(s, match.strstart, match.match_length);
- + } else {
- + insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
- + }
- }
- match.strstart += match.match_length;
- match.match_length = 0;
- @@ -111,10 +116,14 @@ static void insert_match(deflate_state *s, struct match match) {
- match.strstart += match.match_length;
- match.match_length = 0;
- s->ins_h = s->window[match.strstart];
- - if (match.strstart >= 1)
- - UPDATE_HASH(s, s->ins_h, match.strstart+2-MIN_MATCH);
- + if (match.strstart >= (MIN_MATCH - 2))
- +#ifndef NOT_TWEAK_COMPILER
- + insert_string(s, match.strstart + 2 - MIN_MATCH, MIN_MATCH - 2);
- +#else
- + insert_string(s, match.strstart + 2 - MIN_MATCH, 1);
- #if MIN_MATCH != 3
- -#warning Call UPDATE_HASH() MIN_MATCH-3 more times
- +#warning Call insert_string() MIN_MATCH-3 more times
- +#endif
- #endif
- /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
- * matter since it will be recomputed at next deflate call.
- @@ -229,7 +238,7 @@ block_state deflate_medium(deflate_state *s, int flush) {
- } else {
- hash_head = 0;
- if (s->lookahead >= MIN_MATCH) {
- - hash_head = insert_string(s, s->strstart);
- + hash_head = insert_string(s, s->strstart, 1);
- }
- /* set up the initial match to be a 1 byte literal */
- @@ -263,7 +272,7 @@ block_state deflate_medium(deflate_state *s, int flush) {
- /* now, look ahead one */
- if (s->lookahead > MIN_LOOKAHEAD) {
- s->strstart = current_match.strstart + current_match.match_length;
- - hash_head = insert_string(s, s->strstart);
- + hash_head = insert_string(s, s->strstart, 1);
- /* set up the initial match to be a 1 byte literal */
- next_match.match_start = 0;
- diff --git a/deflate_p.h b/deflate_p.h
- index 4b8282d..4229b3e 100644
- --- a/deflate_p.h
- +++ b/deflate_p.h
- @@ -15,7 +15,7 @@
- /* Forward declare common non-inlined functions declared in deflate.c */
- -#ifdef DEBUG
- +#ifdef ZLIB_DEBUG
- void check_match(deflate_state *s, IPos start, IPos match, int length);
- #else
- #define check_match(s, start, match, length)
- @@ -33,45 +33,34 @@ void flush_pending(z_stream *strm);
- */
- #ifdef X86_SSE4_2_CRC_HASH
- -extern Pos insert_string_sse(deflate_state *const s, const Pos str, uInt count);
- +extern Pos insert_string_sse(deflate_state *const s, const Pos str, unsigned int count);
- #endif
- -local inline Pos insert_string_c(deflate_state *const s, const Pos str, uInt count) {
- +static inline Pos insert_string_c(deflate_state *const s, const Pos str, unsigned int count) {
- Pos ret = 0;
- - uInt idx;
- + unsigned int idx;
- + unsigned int h = s->ins_h;
- for (idx = 0; idx < count; idx++) {
- - UPDATE_HASH(s, s->ins_h, str+idx);
- - if (s->head[s->ins_h] != str+idx) {
- - s->prev[(str+idx) & s->w_mask] = s->head[s->ins_h];
- - s->head[s->ins_h] = str+idx;
- + UPDATE_HASH(s, h, str+idx);
- + if (s->head[h] != str+idx) {
- + s->prev[(str+idx) & s->w_mask] = s->head[h];
- + s->head[h] = str+idx;
- }
- }
- + s->ins_h = h;
- ret = s->prev[(str+count-1) & s->w_mask];
- return ret;
- }
- -local inline Pos insert_string(deflate_state *const s, const Pos str) {
- +static inline Pos insert_string(deflate_state *const s, const Pos str, unsigned int count) {
- #ifdef X86_SSE4_2_CRC_HASH
- if (x86_cpu_has_sse42)
- - return insert_string_sse(s, str, 1);
- + return insert_string_sse(s, str, count);
- #endif
- - return insert_string_c(s, str, 1);
- + return insert_string_c(s, str, count);
- }
- -#ifndef NOT_TWEAK_COMPILER
- -local inline void bulk_insert_str(deflate_state *const s, Pos startpos, uInt count) {
- -# ifdef X86_SSE4_2_CRC_HASH
- - if (x86_cpu_has_sse42) {
- - insert_string_sse(s, startpos, count);
- - } else
- -# endif
- - {
- - insert_string_c(s, startpos, count);
- - }
- -}
- -#endif /* NOT_TWEAK_COMPILER */
- -
- /* ===========================================================================
- * Flush the current block, with given end-of-file flag.
- * IN assertion: strstart is set to the end of the current match.
- @@ -79,8 +68,8 @@ local inline void bulk_insert_str(deflate_state *const s, Pos startpos, uInt cou
- #define FLUSH_BLOCK_ONLY(s, last) { \
- _tr_flush_block(s, (s->block_start >= 0L ? \
- (char *)&s->window[(unsigned)s->block_start] : \
- - (char *)Z_NULL), \
- - (ulg)((long)s->strstart - s->block_start), \
- + NULL), \
- + (unsigned long)((long)s->strstart - s->block_start), \
- (last)); \
- s->block_start = s->strstart; \
- flush_pending(s->strm); \
- diff --git a/deflate_slow.c b/deflate_slow.c
- index 6a855f0..c0be3ea 100644
- --- a/deflate_slow.c
- +++ b/deflate_slow.c
- @@ -47,7 +47,7 @@ block_state deflate_slow(deflate_state *s, int flush) {
- */
- hash_head = NIL;
- if (s->lookahead >= MIN_MATCH) {
- - hash_head = insert_string(s, s->strstart);
- + hash_head = insert_string(s, s->strstart, 1);
- }
- /* Find the longest match, discarding those <= prev_length.
- @@ -79,7 +79,7 @@ block_state deflate_slow(deflate_state *s, int flush) {
- * match is not better, output the previous match:
- */
- if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
- - uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
- + unsigned int max_insert = s->strstart + s->lookahead - MIN_MATCH;
- /* Do not insert strings in hash table beyond this. */
- check_match(s, s->strstart-1, s->prev_match, s->prev_length);
- @@ -97,7 +97,7 @@ block_state deflate_slow(deflate_state *s, int flush) {
- s->prev_length -= 2;
- do {
- if (++s->strstart <= max_insert) {
- - insert_string(s, s->strstart);
- + insert_string(s, s->strstart, 1);
- }
- } while (--s->prev_length != 0);
- s->match_available = 0;
- @@ -105,12 +105,12 @@ block_state deflate_slow(deflate_state *s, int flush) {
- s->strstart++;
- #else
- {
- - uInt mov_fwd = s->prev_length - 2;
- - uInt insert_cnt = mov_fwd;
- + unsigned int mov_fwd = s->prev_length - 2;
- + unsigned int insert_cnt = mov_fwd;
- if (unlikely(insert_cnt > max_insert - s->strstart))
- insert_cnt = max_insert - s->strstart;
- - bulk_insert_str(s, s->strstart + 1, insert_cnt);
- + insert_string(s, s->strstart + 1, insert_cnt);
- s->prev_length = 0;
- s->match_available = 0;
- s->match_length = MIN_MATCH-1;
- diff --git a/gzguts.h b/gzguts.h
- index 0921f17..e075bfa 100644
- --- a/gzguts.h
- +++ b/gzguts.h
- @@ -64,11 +64,6 @@
- # define snprintf _snprintf
- #endif
- -#ifndef local
- -# define local static
- -#endif
- -/* compile with -Dlocal if your debugger can't find static symbols */
- -
- /* get errno and strerror definition */
- #ifndef NO_STRERROR
- # include <errno.h>
- diff --git a/gzlib.c b/gzlib.c
- index e5ebe5f..f152c1c 100644
- --- a/gzlib.c
- +++ b/gzlib.c
- @@ -16,11 +16,11 @@
- #endif
- /* Local functions */
- -local void gz_reset(gz_statep);
- -local gzFile gz_open(const void *, int, const char *);
- +static void gz_reset(gz_statep);
- +static gzFile gz_open(const void *, int, const char *);
- /* Reset gzip file state */
- -local void gz_reset(gz_statep state) {
- +static void gz_reset(gz_statep state) {
- state->x.have = 0; /* no output data available */
- if (state->mode == GZ_READ) { /* for reading ... */
- state->eof = 0; /* not at end of file */
- @@ -34,7 +34,7 @@ local void gz_reset(gz_statep state) {
- }
- /* Open a gzip file either by name or file descriptor. */
- -local gzFile gz_open(const void *path, int fd, const char *mode) {
- +static gzFile gz_open(const void *path, int fd, const char *mode) {
- gz_statep state;
- size_t len;
- int oflag;
- @@ -496,7 +496,6 @@ void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg) {
- return;
- }
- snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, "%s%s%s", state->path, ": ", msg);
- - return;
- }
- #ifndef INT_MAX
- diff --git a/gzread.c b/gzread.c
- index 5db2c50..57c115a 100644
- --- a/gzread.c
- +++ b/gzread.c
- @@ -6,23 +6,28 @@
- #include "gzguts.h"
- /* Local functions */
- -local int gz_load(gz_statep, unsigned char *, unsigned, unsigned *);
- -local int gz_avail(gz_statep);
- -local int gz_look(gz_statep);
- -local int gz_decomp(gz_statep);
- -local int gz_fetch(gz_statep);
- -local int gz_skip(gz_statep, z_off64_t);
- +static int gz_load(gz_statep, unsigned char *, unsigned, unsigned *);
- +static int gz_avail(gz_statep);
- +static int gz_look(gz_statep);
- +static int gz_decomp(gz_statep);
- +static int gz_fetch(gz_statep);
- +static int gz_skip(gz_statep, z_off64_t);
- +static size_t gz_read(gz_statep, void *, size_t);
- /* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from
- state->fd, and update state->eof, state->err, and state->msg as appropriate.
- This function needs to loop on read(), since read() is not guaranteed to
- read the number of bytes requested, depending on the type of descriptor. */
- -local int gz_load(gz_statep state, unsigned char *buf, unsigned len, unsigned *have) {
- +static int gz_load(gz_statep state, unsigned char *buf, unsigned len, unsigned *have) {
- int ret;
- + unsigned get, max = ((unsigned)-1 >> 2) + 1;
- *have = 0;
- do {
- - ret = read(state->fd, buf + *have, len - *have);
- + get = len - *have;
- + if (get > max)
- + get = max;
- + ret = read(state->fd, buf + *have, get);
- if (ret <= 0)
- break;
- *have += ret;
- @@ -43,7 +48,7 @@ local int gz_load(gz_statep state, unsigned char *buf, unsigned len, unsigned *h
- If strm->avail_in != 0, then the current data is moved to the beginning of
- the input buffer, and then the remainder of the buffer is loaded with the
- available data from the input file. */
- -local int gz_avail(gz_statep state) {
- +static int gz_avail(gz_statep state) {
- unsigned got;
- z_stream *strm = &(state->strm);
- @@ -75,7 +80,7 @@ local int gz_avail(gz_statep state) {
- case, all further file reads will be directly to either the output buffer or
- a user buffer. If decompressing, the inflate state will be initialized.
- gz_look() will return 0 on success or -1 on failure. */
- -local int gz_look(gz_statep state) {
- +static int gz_look(gz_statep state) {
- z_stream *strm = &(state->strm);
- /* allocate read buffers and inflate memory */
- @@ -84,21 +89,19 @@ local int gz_look(gz_statep state) {
- state->in = (unsigned char *)malloc(state->want);
- state->out = (unsigned char *)malloc(state->want << 1);
- if (state->in == NULL || state->out == NULL) {
- - if (state->out != NULL)
- - free(state->out);
- - if (state->in != NULL)
- - free(state->in);
- + free(state->out);
- + free(state->in);
- gz_error(state, Z_MEM_ERROR, "out of memory");
- return -1;
- }
- state->size = state->want;
- /* allocate inflate memory */
- - state->strm.zalloc = Z_NULL;
- - state->strm.zfree = Z_NULL;
- - state->strm.opaque = Z_NULL;
- + state->strm.zalloc = NULL;
- + state->strm.zfree = NULL;
- + state->strm.opaque = NULL;
- state->strm.avail_in = 0;
- - state->strm.next_in = Z_NULL;
- + state->strm.next_in = NULL;
- if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
- free(state->out);
- free(state->in);
- @@ -159,7 +162,7 @@ local int gz_look(gz_statep state) {
- data. If the gzip stream completes, state->how is reset to LOOK to look for
- the next gzip stream or raw data, once state->x.have is depleted. Returns 0
- on success, -1 on failure. */
- -local int gz_decomp(gz_statep state) {
- +static int gz_decomp(gz_statep state) {
- int ret = Z_OK;
- unsigned had;
- z_stream *strm = &(state->strm);
- @@ -209,7 +212,7 @@ local int gz_decomp(gz_statep state) {
- looked for to determine whether to copy or decompress. Returns -1 on error,
- otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the
- end of the input file has been reached and all data has been processed. */
- -local int gz_fetch(gz_statep state) {
- +static int gz_fetch(gz_statep state) {
- z_stream *strm = &(state->strm);
- do {
- @@ -237,7 +240,7 @@ local int gz_fetch(gz_statep state) {
- }
- /* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
- -local int gz_skip(gz_statep state, z_off64_t len) {
- +static int gz_skip(gz_statep state, z_off64_t len) {
- unsigned n;
- /* skip over len bytes or reach end-of-file, whichever comes first */
- @@ -262,28 +265,14 @@ local int gz_skip(gz_statep state, z_off64_t len) {
- return 0;
- }
- -/* -- see zlib.h -- */
- -int ZEXPORT gzread(gzFile file, void *buf, unsigned len) {
- - unsigned got, n;
- - gz_statep state;
- - z_stream *strm;
- -
- - /* get internal structure */
- - if (file == NULL)
- - return -1;
- - state = (gz_statep)file;
- - strm = &(state->strm);
- -
- - /* check that we're reading and that there's no (serious) error */
- - if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
- - return -1;
- -
- - /* since an int is returned, make sure len fits in one, otherwise return
- - with an error (this avoids the flaw in the interface) */
- - if ((int)len < 0) {
- - gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
- - return -1;
- - }
- +/* Read len bytes into buf from file, or less than len up to the end of the
- + input. Return the number of bytes read. If zero is returned, either the
- + end of file was reached, or there was an error. state->err must be
- + consulted in that case to determine which. */
- +static size_t gz_read(gz_statep state, void *buf, size_t len)
- +{
- + size_t got;
- + unsigned n;
- /* if len is zero, avoid unnecessary operations */
- if (len == 0)
- @@ -293,40 +282,55 @@ int ZEXPORT gzread(gzFile file, void *buf, unsigned len) {
- if (state->seek) {
- state->seek = 0;
- if (gz_skip(state, state->skip) == -1)
- - return -1;
- + return 0;
- }
- /* get len bytes to buf, or less than len if at the end */
- got = 0;
- do {
- + /* set n to the maximum amount of len that fits in an unsigned int */
- + n = -1;
- + if (n > len)
- + n = len;
- +
- /* first just try copying data from the output buffer */
- if (state->x.have) {
- - n = state->x.have > len ? len : state->x.have;
- + if (state->x.have < n)
- + n = state->x.have;
- memcpy(buf, state->x.next, n);
- state->x.next += n;
- state->x.have -= n;
- - } else if (state->eof && strm->avail_in == 0) {
- - /* output buffer empty -- return if we're at the end of the input */
- + }
- +
- + /* output buffer empty -- return if we're at the end of the input */
- + else if (state->eof && state->strm.avail_in == 0) {
- state->past = 1; /* tried to read past end */
- break;
- - } else if (state->how == LOOK || len < (state->size << 1)) {
- - /* need output data -- for small len or new stream load up our output buffer */
- + }
- +
- + /* need output data -- for small len or new stream load up our output
- + buffer */
- + else if (state->how == LOOK || n < (state->size << 1)) {
- /* get more output, looking for header if required */
- if (gz_fetch(state) == -1)
- - return -1;
- + return 0;
- continue; /* no progress yet -- go back to copy above */
- /* the copy above assures that we will leave with space in the
- output buffer, allowing at least one gzungetc() to succeed */
- - } else if (state->how == COPY) { /* read directly */
- - /* large len -- read directly into user buffer */
- - if (gz_load(state, (unsigned char *)buf, len, &n) == -1)
- - return -1;
- - } else { /* state->how == GZIP */
- - /* large len -- decompress directly into user buffer */
- - strm->avail_out = len;
- - strm->next_out = (unsigned char *)buf;
- + }
- +
- + /* large len -- read directly into user buffer */
- + else if (state->how == COPY) { /* read directly */
- + if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
- + return 0;
- + }
- +
- + /* large len -- decompress directly into user buffer */
- + else { /* state->how == GZIP */
- + state->strm.avail_out = n;
- + state->strm.next_out = (unsigned char *)buf;
- if (gz_decomp(state) == -1)
- - return -1;
- + return 0;
- n = state->x.have;
- state->x.have = 0;
- }
- @@ -338,8 +342,71 @@ int ZEXPORT gzread(gzFile file, void *buf, unsigned len) {
- state->x.pos += n;
- } while (len);
- - /* return number of bytes read into user buffer (will fit in int) */
- - return (int)got;
- + /* return number of bytes read into user buffer */
- + return got;
- +}
- +
- +/* -- see zlib.h -- */
- +int ZEXPORT gzread(file, buf, len)
- + gzFile file;
- + voidp buf;
- + unsigned len;
- +{
- + gz_statep state;
- +
- + /* get internal structure */
- + if (file == NULL)
- + return -1;
- + state = (gz_statep)file;
- +
- + /* check that we're reading and that there's no (serious) error */
- + if (state->mode != GZ_READ ||
- + (state->err != Z_OK && state->err != Z_BUF_ERROR))
- + return -1;
- +
- + /* since an int is returned, make sure len fits in one, otherwise return
- + with an error (this avoids a flaw in the interface) */
- + if ((int)len < 0) {
- + gz_error(state, Z_STREAM_ERROR, "request does not fit in an int");
- + return -1;
- + }
- +
- + /* read len or fewer bytes to buf */
- + len = gz_read(state, buf, len);
- +
- + /* check for an error */
- + if (len == 0 && state->err != Z_OK && state->err != Z_BUF_ERROR)
- + return -1;
- +
- + /* return the number of bytes read (this is assured to fit in an int) */
- + return (int)len;
- +}
- +
- +/* -- see zlib.h -- */
- +size_t ZEXPORT gzfread(void *buf, size_t size, size_t nitems, gzFile file)
- +{
- + size_t len;
- + gz_statep state;
- +
- + /* get internal structure */
- + if (file == NULL)
- + return 0;
- + state = (gz_statep)file;
- +
- + /* check that we're reading and that there's no (serious) error */
- + if (state->mode != GZ_READ ||
- + (state->err != Z_OK && state->err != Z_BUF_ERROR))
- + return 0;
- +
- + /* compute bytes to read -- error on overflow */
- + len = nitems * size;
- + if (size && len / size != nitems) {
- + gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
- + return 0;
- + }
- +
- + /* read len or fewer bytes to buf, return the number of full items read */
- + return len ? gz_read(state, buf, len) / size : 0;
- }
- /* -- see zlib.h -- */
- @@ -365,8 +432,8 @@ int ZEXPORT gzgetc(gzFile file) {
- return *(state->x.next)++;
- }
- - /* nothing there -- try gzread() */
- - ret = gzread(file, buf, 1);
- + /* nothing there -- try gz_read() */
- + ret = gz_read(state, buf, 1);
- return ret < 1 ? -1 : buf[0];
- }
- diff --git a/gzwrite.c b/gzwrite.c
- index dedaee0..e802449 100644
- --- a/gzwrite.c
- +++ b/gzwrite.c
- @@ -7,13 +7,14 @@
- #include "gzguts.h"
- /* Local functions */
- -local int gz_init(gz_statep);
- -local int gz_comp(gz_statep, int);
- -local int gz_zero(gz_statep, z_off64_t);
- +static int gz_init(gz_statep);
- +static int gz_comp(gz_statep, int);
- +static int gz_zero(gz_statep, z_off64_t);
- +static size_t gz_write(gz_statep, void const *, size_t);
- /* Initialize state for writing a gzip file. Mark initialization by setting
- - state->size to non-zero. Return -1 on failure or 0 on success. */
- -local int gz_init(gz_statep state) {
- + state->size to non-zero. Return -1 on a memory allocation failure, or 0 on success. */
- +static int gz_init(gz_statep state) {
- int ret;
- z_stream *strm = &(state->strm);
- @@ -35,9 +36,9 @@ local int gz_init(gz_statep state) {
- }
- /* allocate deflate memory, set up for gzip compression */
- - strm->zalloc = Z_NULL;
- - strm->zfree = Z_NULL;
- - strm->opaque = Z_NULL;
- + strm->zalloc = NULL;
- + strm->zfree = NULL;
- + strm->opaque = NULL;
- ret = deflateInit2(strm, state->level, Z_DEFLATED, MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
- if (ret != Z_OK) {
- free(state->out);
- @@ -61,14 +62,14 @@ local int gz_init(gz_statep state) {
- }
- /* Compress whatever is at avail_in and next_in and write to the output file.
- - Return -1 if there is an error writing to the output file, otherwise 0.
- - flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH,
- - then the deflate() state is reset to start a new gzip stream. If gz->direct
- - is true, then simply write to the output file without compressing, and
- - ignore flush. */
- -local int gz_comp(gz_statep state, int flush) {
- - int ret, got;
- - unsigned have;
- + Return -1 if there is an error writing to the output file or if gz_init()
- + fails to allocate memory, otherwise 0. flush is assumed to be a valid
- + deflate() flush value. If flush is Z_FINISH, then the deflate() state is
- + reset to start a new gzip stream. If gz->direct is true, then simply write
- + to the output file without compressing, and ignore flush. */
- +static int gz_comp(gz_statep state, int flush) {
- + int ret, writ;
- + unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
- z_stream *strm = &(state->strm);
- /* allocate memory if this is the first time through */
- @@ -78,12 +79,14 @@ local int gz_comp(gz_statep state, int flush) {
- /* write directly if requested */
- if (state->direct) {
- while (strm->avail_in) {
- - if ((got = write(state->fd, strm->next_in, strm->avail_in)) < 0) {
- + put = strm->avail_in > max ? max : strm->avail_in;
- + writ = write(state->fd, strm->next_in, put);
- + if (writ < 0) {
- gz_error(state, Z_ERRNO, zstrerror());
- return -1;
- }
- - strm->avail_in -= got;
- - strm->next_in += got;
- + strm->avail_in -= (unsigned)writ;
- + strm->next_in += writ;
- }
- return 0;
- }
- @@ -95,11 +98,14 @@ local int gz_comp(gz_statep state, int flush) {
- doing Z_FINISH then don't write until we get to Z_STREAM_END */
- if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && (flush != Z_FINISH || ret == Z_STREAM_END))) {
- while (strm->next_out > state->x.next) {
- - if ((got = write(state->fd, state->x.next, strm->next_out - state->x.next)) < 0) {
- + put = strm->next_out - state->x.next > (int)max ? max :
- + (unsigned)(strm->next_out - state->x.next);
- + writ = write(state->fd, state->x.next, put);
- + if (writ < 0) {
- gz_error(state, Z_ERRNO, zstrerror());
- return -1;
- }
- - state->x.next += got;
- + state->x.next += writ;
- }
- if (strm->avail_out == 0) {
- strm->avail_out = state->size;
- @@ -125,8 +131,9 @@ local int gz_comp(gz_statep state, int flush) {
- return 0;
- }
- -/* Compress len zeros to output. Return -1 on error, 0 on success. */
- -local int gz_zero(gz_statep state, z_off64_t len) {
- +/* Compress len zeros to output. Return -1 on a write error or memory
- + allocation failure by gz_comp(), or 0 on success. */
- +static int gz_zero(gz_statep state, z_off64_t len) {
- int first;
- unsigned n;
- z_stream *strm = &(state->strm);
- @@ -153,28 +160,11 @@ local int gz_zero(gz_statep state, z_off64_t len) {
- return 0;
- }
- -/* -- see zlib.h -- */
- -int ZEXPORT gzwrite(gzFile file, void const *buf, unsigned len) {
- - unsigned put = len;
- - gz_statep state;
- - z_stream *strm;
- -
- - /* get internal structure */
- - if (file == NULL)
- - return 0;
- - state = (gz_statep)file;
- - strm = &(state->strm);
- -
- - /* check that we're writing and that there's no error */
- - if (state->mode != GZ_WRITE || state->err != Z_OK)
- - return 0;
- -
- - /* since an int is returned, make sure len fits in one, otherwise return
- - with an error (this avoids the flaw in the interface) */
- - if ((int)len < 0) {
- - gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
- - return 0;
- - }
- +/* Write len bytes from buf to file. Return the number of bytes written. If
- + the returned value is less than len, then there was an error. */
- +static size_t gz_write(gz_statep state, void const *buf, size_t len)
- +{
- + size_t put = len;
- /* if len is zero, avoid unnecessary operations */
- if (len == 0)
- @@ -197,14 +187,15 @@ int ZEXPORT gzwrite(gzFile file, void const *buf, unsigned len) {
- do {
- unsigned have, copy;
- - if (strm->avail_in == 0)
- - strm->next_in = state->in;
- - have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
- + if (state->strm.avail_in == 0)
- + state->strm.next_in = state->in;
- + have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
- + state->in);
- copy = state->size - have;
- if (copy > len)
- copy = len;
- memcpy(state->in + have, buf, copy);
- - strm->avail_in += copy;
- + state->strm.avail_in += copy;
- state->x.pos += copy;
- buf = (const char *)buf + copy;
- len -= copy;
- @@ -213,19 +204,75 @@ int ZEXPORT gzwrite(gzFile file, void const *buf, unsigned len) {
- } while (len);
- } else {
- /* consume whatever's left in the input buffer */
- - if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
- + if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
- return 0;
- /* directly compress user buffer to file */
- - strm->avail_in = len;
- - strm->next_in = (const unsigned char *)buf;
- - state->x.pos += len;
- - if (gz_comp(state, Z_NO_FLUSH) == -1)
- - return 0;
- + state->strm.next_in = (const unsigned char *)buf;
- + do {
- + unsigned n = -1;
- + if (n > len)
- + n = len;
- + state->strm.avail_in = n;
- + state->x.pos += n;
- + if (gz_comp(state, Z_NO_FLUSH) == -1)
- + return 0;
- + len -= n;
- + } while (len);
- }
- - /* input was all buffered or compressed (put will fit in int) */
- - return (int)put;
- + /* input was all buffered or compressed */
- + return put;
- +}
- +
- +/* -- see zlib.h -- */
- +int ZEXPORT gzwrite(gzFile file, void const *buf, unsigned len) {
- + gz_statep state;
- +
- + /* get internal structure */
- + if (file == NULL)
- + return 0;
- + state = (gz_statep)file;
- +
- + /* check that we're writing and that there's no error */
- + if (state->mode != GZ_WRITE || state->err != Z_OK)
- + return 0;
- +
- + /* since an int is returned, make sure len fits in one, otherwise return
- + with an error (this avoids a flaw in the interface) */
- + if ((int)len < 0) {
- + gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
- + return 0;
- + }
- +
- + /* write len bytes from buf (the return value will fit in an int) */
- + return (int)gz_write(state, buf, len);
- +}
- +
- +/* -- see zlib.h -- */
- +size_t ZEXPORT gzfwrite(void const *buf, size_t size, size_t nitems, gzFile file)
- +{
- + size_t len;
- + gz_statep state;
- +
- + /* get internal structure */
- + if (file == NULL)
- + return 0;
- + state = (gz_statep)file;
- +
- + /* check that we're writing and that there's no error */
- + if (state->mode != GZ_WRITE || state->err != Z_OK)
- + return 0;
- +
- + /* compute bytes to read -- error on overflow */
- + len = nitems * size;
- + if (size && len / size != nitems) {
- + gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
- + return 0;
- + }
- +
- + /* write len bytes to buf, return the number of full items written */
- + return len ? gz_write(state, buf, len) / size : 0;
- }
- /* -- see zlib.h -- */
- @@ -267,8 +314,8 @@ int ZEXPORT gzputc(gzFile file, int c) {
- }
- /* no room in buffer or not initialized, use gz_write() */
- - buf[0] = c;
- - if (gzwrite(file, buf, 1) != 1)
- + buf[0] = (unsigned char)c;
- + if (gz_write(state, buf, 1) != 1)
- return -1;
- return c & 0xff;
- }
- @@ -276,11 +323,21 @@ int ZEXPORT gzputc(gzFile file, int c) {
- /* -- see zlib.h -- */
- int ZEXPORT gzputs(gzFile file, const char *str) {
- int ret;
- - unsigned len;
- + size_t len;
- + gz_statep state;
- +
- + /* get internal structure */
- + if (file == NULL)
- + return -1;
- + state = (gz_statep)file;
- +
- + /* check that we're writing and that there's no error */
- + if (state->mode != GZ_WRITE || state->err != Z_OK)
- + return -1;
- /* write string */
- - len = (unsigned)strlen(str);
- - ret = gzwrite(file, str, len);
- + len = strlen(str);
- + ret = gz_write(state, str, len);
- return ret == 0 && len != 0 ? -1 : ret;
- }
- @@ -293,23 +350,23 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) {
- /* get internal structure */
- if (file == NULL)
- - return -1;
- + return Z_STREAM_ERROR;
- state = (gz_statep)file;
- strm = &(state->strm);
- /* check that we're writing and that there's no error */
- if (state->mode != GZ_WRITE || state->err != Z_OK)
- - return 0;
- + return Z_STREAM_ERROR;
- /* make sure we have some buffer space */
- if (state->size == 0 && gz_init(state) == -1)
- - return 0;
- + return state->err;
- /* check for seek request */
- if (state->seek) {
- state->seek = 0;
- if (gz_zero(state, state->skip) == -1)
- - return 0;
- + return state->err;
- }
- /* do the printf() into the input buffer, put length in len -- the input
- @@ -332,7 +389,7 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) {
- left = strm->avail_in - state->size;
- strm->avail_in = state->size;
- if (gz_comp(state, Z_NO_FLUSH) == -1)
- - return 0;
- + return state->err;
- memcpy(state->in, state->in + state->size, left);
- strm->next_in = state->in;
- strm->avail_in = left;
- @@ -356,7 +413,7 @@ int ZEXPORT gzflush(gzFile file, int flush) {
- /* get internal structure */
- if (file == NULL)
- - return -1;
- + return Z_STREAM_ERROR;
- state = (gz_statep)file;
- /* check that we're writing and that there's no error */
- @@ -371,11 +428,11 @@ int ZEXPORT gzflush(gzFile file, int flush) {
- if (state->seek) {
- state->seek = 0;
- if (gz_zero(state, state->skip) == -1)
- - return -1;
- + return state->err;
- }
- /* compress remaining data with requested flush */
- - gz_comp(state, flush);
- + (void)gz_comp(state, flush);
- return state->err;
- }
- @@ -402,13 +459,13 @@ int ZEXPORT gzsetparams(gzFile file, int level, int strategy) {
- if (state->seek) {
- state->seek = 0;
- if (gz_zero(state, state->skip) == -1)
- - return -1;
- + return state->err;
- }
- /* change compression parameters for subsequent input */
- if (state->size) {
- /* flush previous input with previous parameters before changing */
- - if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
- + if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
- return state->err;
- deflateParams(strm, level, strategy);
- }
- diff --git a/infback.c b/infback.c
- index 9f3dc46..4fc783f 100644
- --- a/infback.c
- +++ b/infback.c
- @@ -16,11 +16,11 @@
- #include "inffast.h"
- /* function prototypes */
- -local void fixedtables(struct inflate_state *state);
- +static void fixedtables(struct inflate_state *state);
- /*
- strm provides memory allocation functions in zalloc and zfree, or
- - Z_NULL to use the library memory allocation functions.
- + NULL to use the library memory allocation functions.
- windowBits is in the range 8..15, and window is a user-supplied
- window and output buffer that is 2**windowBits bytes.
- @@ -29,19 +29,19 @@ int ZEXPORT inflateBackInit_(z_stream *strm, int windowBits, unsigned char *wind
- const char *version, int stream_size) {
- struct inflate_state *state;
- - if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || stream_size != (int)(sizeof(z_stream)))
- + if (version == NULL || version[0] != ZLIB_VERSION[0] || stream_size != (int)(sizeof(z_stream)))
- return Z_VERSION_ERROR;
- - if (strm == Z_NULL || window == Z_NULL || windowBits < 8 || windowBits > 15)
- + if (strm == NULL || window == NULL || windowBits < 8 || windowBits > 15)
- return Z_STREAM_ERROR;
- - strm->msg = Z_NULL; /* in case we return an error */
- - if (strm->zalloc == (alloc_func)0) {
- + strm->msg = NULL; /* in case we return an error */
- + if (strm->zalloc == NULL) {
- strm->zalloc = zcalloc;
- strm->opaque = NULL;
- }
- - if (strm->zfree == (free_func)0)
- + if (strm->zfree == NULL)
- strm->zfree = zcfree;
- state = (struct inflate_state *)ZALLOC(strm, 1, sizeof(struct inflate_state));
- - if (state == Z_NULL)
- + if (state == NULL)
- return Z_MEM_ERROR;
- Tracev((stderr, "inflate: allocated\n"));
- strm->state = (struct internal_state *)state;
- @@ -64,7 +64,7 @@ int ZEXPORT inflateBackInit_(z_stream *strm, int windowBits, unsigned char *wind
- used for threaded applications, since the rewriting of the tables and virgin
- may not be thread-safe.
- */
- -local void fixedtables(struct inflate_state *state) {
- +static void fixedtables(struct inflate_state *state) {
- #ifdef BUILDFIXED
- static int virgin = 1;
- static code *lenfix, *distfix;
- @@ -143,7 +143,7 @@ local void fixedtables(struct inflate_state *state) {
- if (have == 0) { \
- have = in(in_desc, &next); \
- if (have == 0) { \
- - next = Z_NULL; \
- + next = NULL; \
- ret = Z_BUF_ERROR; \
- goto inf_leave; \
- } \
- @@ -223,12 +223,12 @@ local void fixedtables(struct inflate_state *state) {
- in() should return zero on failure. out() should return non-zero on
- failure. If either in() or out() fails, than inflateBack() returns a
- - Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
- + Z_BUF_ERROR. strm->next_in can be checked for NULL to see whether it
- was in() or out() that caused in the error. Otherwise, inflateBack()
- returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
- error, or Z_MEM_ERROR if it could not allocate memory for the state.
- inflateBack() can also return Z_STREAM_ERROR if the input parameters
- - are not correct, i.e. strm is Z_NULL or the state was not initialized.
- + are not correct, i.e. strm is NULL or the state was not initialized.
- */
- int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_func out, void *out_desc) {
- struct inflate_state *state;
- @@ -247,17 +247,17 @@ int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_func out,
- {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
- /* Check that the strm exists and that the state was initialized */
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (strm == NULL || strm->state == NULL)
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)strm->state;
- /* Reset the state */
- - strm->msg = Z_NULL;
- + strm->msg = NULL;
- state->mode = TYPE;
- state->last = 0;
- state->whave = 0;
- next = strm->next_in;
- - have = next != Z_NULL ? strm->avail_in : 0;
- + have = next != NULL ? strm->avail_in : 0;
- hold = 0;
- bits = 0;
- put = state->window;
- @@ -603,10 +603,10 @@ int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_func out,
- }
- int ZEXPORT inflateBackEnd(z_stream *strm) {
- - if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
- + if (strm == NULL || strm->state == NULL || strm->zfree == NULL)
- return Z_STREAM_ERROR;
- ZFREE(strm, strm->state);
- - strm->state = Z_NULL;
- + strm->state = NULL;
- Tracev((stderr, "inflate: end\n"));
- return Z_OK;
- }
- diff --git a/inffast.c b/inffast.c
- index 8acb261..50ec86e 100644
- --- a/inffast.c
- +++ b/inffast.c
- @@ -8,25 +8,6 @@
- #include "inflate.h"
- #include "inffast.h"
- -/* Allow machine dependent optimization for post-increment or pre-increment.
- - Based on testing to date,
- - Pre-increment preferred for:
- - - PowerPC G3 (Adler)
- - - MIPS R5000 (Randers-Pehrson)
- - Post-increment preferred for:
- - - none
- - No measurable difference:
- - - Pentium III (Anderson)
- - - M68060 (Nikl)
- - */
- -#ifdef POSTINC
- -# define OFF 0
- -# define PUP(a) *(a)++
- -#else
- -# define OFF 1
- -# define PUP(a) *++(a)
- -#endif
- -
- /* Return the low n bits of the bit accumulator (n < 16) */
- #define BITS(n) \
- (hold & ((1U << (n)) - 1))
- @@ -103,9 +84,9 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
- /* copy state to local variables */
- state = (struct inflate_state *)strm->state;
- - in = strm->next_in - OFF;
- + in = strm->next_in;
- last = in + (strm->avail_in - 5);
- - out = strm->next_out - OFF;
- + out = strm->next_out;
- beg = out - (start - strm->avail_out);
- end = out + (strm->avail_out - 257);
- #ifdef INFLATE_STRICT
- @@ -126,9 +107,9 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
- input data or output space */
- do {
- if (bits < 15) {
- - hold += (PUP(in) << bits);
- + hold += (uint32_t)(*in++) << bits;
- bits += 8;
- - hold += (PUP(in) << bits);
- + hold += (uint32_t)(*in++) << bits;
- bits += 8;
- }
- here = lcode[hold & lmask];
- @@ -139,13 +120,13 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
- Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
- "inflate: literal '%c'\n" :
- "inflate: literal 0x%02x\n", here.val));
- - PUP(out) = (unsigned char)(here.val);
- + *out++ = (unsigned char)(here.val);
- } else if (op & 16) { /* length base */
- len = here.val;
- op &= 15; /* number of extra bits */
- if (op) {
- if (bits < op) {
- - hold += (PUP(in) << bits);
- + hold += (uint32_t)(*in++) << bits;
- bits += 8;
- }
- len += BITS(op);
- @@ -153,9 +134,9 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
- }
- Tracevv((stderr, "inflate: length %u\n", len));
- if (bits < 15) {
- - hold += (PUP(in) << bits);
- + hold += (uint32_t)(*in++) << bits;
- bits += 8;
- - hold += (PUP(in) << bits);
- + hold += (uint32_t)(*in++) << bits;
- bits += 8;
- }
- here = dcode[hold & dmask];
- @@ -166,10 +147,10 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
- dist = here.val;
- op &= 15; /* number of extra bits */
- if (bits < op) {
- - hold += (PUP(in) << bits);
- + hold += (uint32_t)(*in++) << bits;
- bits += 8;
- if (bits < op) {
- - hold += (PUP(in) << bits);
- + hold += (uint32_t)(*in++) << bits;
- bits += 8;
- }
- }
- @@ -195,30 +176,30 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
- #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
- if (len <= op - whave) {
- do {
- - PUP(out) = 0;
- + *out++ = 0;
- } while (--len);
- continue;
- }
- len -= op - whave;
- do {
- - PUP(out) = 0;
- + *out++ = 0;
- } while (--op > whave);
- if (op == 0) {
- from = out - dist;
- do {
- - PUP(out) = PUP(from);
- + *out++ = *from++;
- } while (--len);
- continue;
- }
- #endif
- }
- - from = window - OFF;
- + from = window;
- if (wnext == 0) { /* very common case */
- from += wsize - op;
- if (op < len) { /* some from window */
- len -= op;
- do {
- - PUP(out) = PUP(from);
- + *out++ = *from++;
- } while (--op);
- from = out - dist; /* rest from output */
- }
- @@ -228,14 +209,14 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
- if (op < len) { /* some from end of window */
- len -= op;
- do {
- - PUP(out) = PUP(from);
- + *out++ = *from++;
- } while (--op);
- - from = window - OFF;
- + from = window;
- if (wnext < len) { /* some from start of window */
- op = wnext;
- len -= op;
- do {
- - PUP(out) = PUP(from);
- + *out++ = *from++;
- } while (--op);
- from = out - dist; /* rest from output */
- }
- @@ -245,34 +226,34 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
- if (op < len) { /* some from window */
- len -= op;
- do {
- - PUP(out) = PUP(from);
- + *out++ = *from++;
- } while (--op);
- from = out - dist; /* rest from output */
- }
- }
- while (len > 2) {
- - PUP(out) = PUP(from);
- - PUP(out) = PUP(from);
- - PUP(out) = PUP(from);
- + *out++ = *from++;
- + *out++ = *from++;
- + *out++ = *from++;
- len -= 3;
- }
- if (len) {
- - PUP(out) = PUP(from);
- + *out++ = *from++;
- if (len > 1)
- - PUP(out) = PUP(from);
- + *out++ = *from++;
- }
- } else {
- from = out - dist; /* copy direct from output */
- do { /* minimum length is three */
- - PUP(out) = PUP(from);
- - PUP(out) = PUP(from);
- - PUP(out) = PUP(from);
- + *out++ = *from++;
- + *out++ = *from++;
- + *out++ = *from++;
- len -= 3;
- } while (len > 2);
- if (len) {
- - PUP(out) = PUP(from);
- + *out++ = *from++;
- if (len > 1)
- - PUP(out) = PUP(from);
- + *out++ = *from++;
- }
- }
- } else if ((op & 64) == 0) { /* 2nd level distance code */
- @@ -304,8 +285,8 @@ void ZLIB_INTERNAL inflate_fast(z_stream *strm, unsigned long start) {
- hold &= (1U << bits) - 1;
- /* update state and return */
- - strm->next_in = in + OFF;
- - strm->next_out = out + OFF;
- + strm->next_in = in;
- + strm->next_out = out;
- strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
- strm->avail_out = (unsigned)(out < end ? 257 + (end - out) : 257 - (out - end));
- state->hold = hold;
- diff --git a/inflate.c b/inflate.c
- index 8d4a992..462045c 100644
- --- a/inflate.c
- +++ b/inflate.c
- @@ -92,28 +92,40 @@
- #endif
- /* function prototypes */
- -local void fixedtables(struct inflate_state *state);
- -local int updatewindow(z_stream *strm, const unsigned char *end, uint32_t copy);
- +static int inflateStateCheck(z_stream *strm);
- +static void fixedtables(struct inflate_state *state);
- +static int updatewindow(z_stream *strm, const unsigned char *end, uint32_t copy);
- #ifdef BUILDFIXED
- void makefixed(void);
- #endif
- -local uint32_t syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len);
- +static uint32_t syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len);
- +
- +static int inflateStateCheck(z_stream *strm)
- +{
- + struct inflate_state *state;
- + if (strm == NULL || strm->zalloc == NULL || strm->zfree == NULL)
- + return 1;
- + state = (struct inflate_state *)strm->state;
- + if (state == NULL || state->strm != strm || state->mode < HEAD || state->mode > SYNC)
- + return 1;
- + return 0;
- +}
- int ZEXPORT inflateResetKeep(z_stream *strm) {
- struct inflate_state *state;
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (inflateStateCheck(strm))
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)strm->state;
- strm->total_in = strm->total_out = state->total = 0;
- - strm->msg = Z_NULL;
- + strm->msg = NULL;
- if (state->wrap) /* to support ill-conceived Java test suite */
- strm->adler = state->wrap & 1;
- state->mode = HEAD;
- state->last = 0;
- state->havedict = 0;
- state->dmax = 32768U;
- - state->head = Z_NULL;
- + state->head = NULL;
- state->hold = 0;
- state->bits = 0;
- state->lencode = state->distcode = state->next = state->codes;
- @@ -126,7 +138,7 @@ int ZEXPORT inflateResetKeep(z_stream *strm) {
- int ZEXPORT inflateReset(z_stream *strm) {
- struct inflate_state *state;
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (inflateStateCheck(strm))
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)strm->state;
- state->wsize = 0;
- @@ -140,7 +152,7 @@ int ZEXPORT inflateReset2(z_stream *strm, int windowBits) {
- struct inflate_state *state;
- /* get the state */
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (inflateStateCheck(strm))
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)strm->state;
- @@ -149,7 +161,7 @@ int ZEXPORT inflateReset2(z_stream *strm, int windowBits) {
- wrap = 0;
- windowBits = -windowBits;
- } else {
- - wrap = (windowBits >> 4) + 1;
- + wrap = (windowBits >> 4) + 5;
- #ifdef GUNZIP
- if (windowBits < 48)
- windowBits &= 15;
- @@ -159,9 +171,9 @@ int ZEXPORT inflateReset2(z_stream *strm, int windowBits) {
- /* set number of window bits, free window if different */
- if (windowBits && (windowBits < 8 || windowBits > 15))
- return Z_STREAM_ERROR;
- - if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
- + if (state->window != NULL && state->wbits != (unsigned)windowBits) {
- ZFREE(strm, state->window);
- - state->window = Z_NULL;
- + state->window = NULL;
- }
- /* update state and reset the rest of it */
- @@ -174,27 +186,29 @@ int ZEXPORT inflateInit2_(z_stream *strm, int windowBits, const char *version, i
- int ret;
- struct inflate_state *state;
- - if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || stream_size != (int)(sizeof(z_stream)))
- + if (version == NULL || version[0] != ZLIB_VERSION[0] || stream_size != (int)(sizeof(z_stream)))
- return Z_VERSION_ERROR;
- - if (strm == Z_NULL)
- + if (strm == NULL)
- return Z_STREAM_ERROR;
- - strm->msg = Z_NULL; /* in case we return an error */
- - if (strm->zalloc == (alloc_func)0) {
- + strm->msg = NULL; /* in case we return an error */
- + if (strm->zalloc == NULL) {
- strm->zalloc = zcalloc;
- strm->opaque = NULL;
- }
- - if (strm->zfree == (free_func)0)
- + if (strm->zfree == NULL)
- strm->zfree = zcfree;
- state = (struct inflate_state *) ZALLOC(strm, 1, sizeof(struct inflate_state));
- - if (state == Z_NULL)
- + if (state == NULL)
- return Z_MEM_ERROR;
- Tracev((stderr, "inflate: allocated\n"));
- strm->state = (struct internal_state *)state;
- - state->window = Z_NULL;
- + state->strm = strm;
- + state->window = NULL;
- + state->mode = HEAD; /* to pass state test in inflateReset2() */
- ret = inflateReset2(strm, windowBits);
- if (ret != Z_OK) {
- ZFREE(strm, state);
- - strm->state = Z_NULL;
- + strm->state = NULL;
- }
- return ret;
- }
- @@ -206,7 +220,7 @@ int ZEXPORT inflateInit_(z_stream *strm, const char *version, int stream_size) {
- int ZEXPORT inflatePrime(z_stream *strm, int bits, int value) {
- struct inflate_state *state;
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (inflateStateCheck(strm))
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)strm->state;
- if (bits < 0) {
- @@ -232,7 +246,7 @@ int ZEXPORT inflatePrime(z_stream *strm, int bits, int value) {
- used for threaded applications, since the rewriting of the tables and virgin
- may not be thread-safe.
- */
- -local void fixedtables(struct inflate_state *state) {
- +static void fixedtables(struct inflate_state *state) {
- #ifdef BUILDFIXED
- static int virgin = 1;
- static code *lenfix, *distfix;
- @@ -350,16 +364,16 @@ void makefixed(void) {
- output will fall in the output data, making match copies simpler and faster.
- The advantage may be dependent on the size of the processor's data caches.
- */
- -local int updatewindow(z_stream *strm, const unsigned char *end, uint32_t copy) {
- +static int updatewindow(z_stream *strm, const unsigned char *end, uint32_t copy) {
- struct inflate_state *state;
- uint32_t dist;
- state = (struct inflate_state *)strm->state;
- /* if it hasn't been done already, allocate space for the window */
- - if (state->window == Z_NULL) {
- + if (state->window == NULL) {
- state->window = (unsigned char *) ZALLOC(strm, 1U << state->wbits, sizeof(unsigned char));
- - if (state->window == Z_NULL)
- + if (state->window == NULL)
- return 1;
- }
- @@ -592,8 +606,8 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
- static const uint16_t order[19] = /* permutation of code lengths */
- {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
- - if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
- - (strm->next_in == Z_NULL && strm->avail_in != 0))
- + if (inflateStateCheck(strm) || strm->next_out == NULL ||
- + (strm->next_in == NULL && strm->avail_in != 0))
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)strm->state;
- @@ -615,14 +629,14 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
- if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
- if (state->wbits == 0)
- state->wbits = 15;
- - state->check = crc32(0L, Z_NULL, 0);
- + state->check = crc32(0L, NULL, 0);
- CRC2(state->check, hold);
- INITBITS();
- state->mode = FLAGS;
- break;
- }
- state->flags = 0; /* expect zlib header */
- - if (state->head != Z_NULL)
- + if (state->head != NULL)
- state->head->done = -1;
- if (!(state->wrap & 1) || /* check if zlib header allowed */
- #else
- @@ -649,7 +663,7 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
- }
- state->dmax = 1U << len;
- Tracev((stderr, "inflate: zlib header ok\n"));
- - strm->adler = state->check = adler32(0L, Z_NULL, 0);
- + strm->adler = state->check = adler32(0L, NULL, 0);
- state->mode = hold & 0x200 ? DICTID : TYPE;
- INITBITS();
- break;
- @@ -667,27 +681,27 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
- state->mode = BAD;
- break;
- }
- - if (state->head != Z_NULL)
- + if (state->head != NULL)
- state->head->text = (int)((hold >> 8) & 1);
- - if (state->flags & 0x0200)
- + if ((state->flags & 0x0200) && (state->wrap & 4))
- CRC2(state->check, hold);
- INITBITS();
- state->mode = TIME;
- case TIME:
- NEEDBITS(32);
- - if (state->head != Z_NULL)
- + if (state->head != NULL)
- state->head->time = hold;
- - if (state->flags & 0x0200)
- + if ((state->flags & 0x0200) && (state->wrap & 4))
- CRC4(state->check, hold);
- INITBITS();
- state->mode = OS;
- case OS:
- NEEDBITS(16);
- - if (state->head != Z_NULL) {
- + if (state->head != NULL) {
- state->head->xflags = (int)(hold & 0xff);
- state->head->os = (int)(hold >> 8);
- }
- - if (state->flags & 0x0200)
- + if ((state->flags & 0x0200) && (state->wrap & 4))
- CRC2(state->check, hold);
- INITBITS();
- state->mode = EXLEN;
- @@ -695,13 +709,13 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
- if (state->flags & 0x0400) {
- NEEDBITS(16);
- state->length = (uint16_t)hold;
- - if (state->head != Z_NULL)
- + if (state->head != NULL)
- state->head->extra_len = (uint16_t)hold;
- - if (state->flags & 0x0200)
- + if ((state->flags & 0x0200) && (state->wrap & 4))
- CRC2(state->check, hold);
- INITBITS();
- - } else if (state->head != Z_NULL) {
- - state->head->extra = Z_NULL;
- + } else if (state->head != NULL) {
- + state->head->extra = NULL;
- }
- state->mode = EXTRA;
- case EXTRA:
- @@ -710,14 +724,14 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
- if (copy > have)
- copy = have;
- if (copy) {
- - if (state->head != Z_NULL &&
- - state->head->extra != Z_NULL) {
- + if (state->head != NULL &&
- + state->head->extra != NULL) {
- len = state->head->extra_len - state->length;
- memcpy(state->head->extra + len, next,
- len + copy > state->head->extra_max ?
- state->head->extra_max - len : copy);
- }
- - if (state->flags & 0x0200)
- + if ((state->flags & 0x0200) && (state->wrap & 4))
- state->check = crc32(state->check, next, copy);
- have -= copy;
- next += copy;
- @@ -734,17 +748,17 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
- copy = 0;
- do {
- len = (unsigned)(next[copy++]);
- - if (state->head != Z_NULL && state->head->name != Z_NULL && state->length < state->head->name_max)
- + if (state->head != NULL && state->head->name != NULL && state->length < state->head->name_max)
- state->head->name[state->length++] = len;
- } while (len && copy < have);
- - if (state->flags & 0x0200)
- + if ((state->flags & 0x0200) && (state->wrap & 4))
- state->check = crc32(state->check, next, copy);
- have -= copy;
- next += copy;
- if (len)
- goto inf_leave;
- - } else if (state->head != Z_NULL) {
- - state->head->name = Z_NULL;
- + } else if (state->head != NULL) {
- + state->head->name = NULL;
- }
- state->length = 0;
- state->mode = COMMENT;
- @@ -754,35 +768,35 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
- copy = 0;
- do {
- len = (unsigned)(next[copy++]);
- - if (state->head != Z_NULL && state->head->comment != Z_NULL
- + if (state->head != NULL && state->head->comment != NULL
- && state->length < state->head->comm_max)
- state->head->comment[state->length++] = len;
- } while (len && copy < have);
- - if (state->flags & 0x0200)
- + if ((state->flags & 0x0200) && (state->wrap & 4))
- state->check = crc32(state->check, next, copy);
- have -= copy;
- next += copy;
- if (len)
- goto inf_leave;
- - } else if (state->head != Z_NULL) {
- - state->head->comment = Z_NULL;
- + } else if (state->head != NULL) {
- + state->head->comment = NULL;
- }
- state->mode = HCRC;
- case HCRC:
- if (state->flags & 0x0200) {
- NEEDBITS(16);
- - if (hold != (state->check & 0xffff)) {
- + if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
- strm->msg = (char *)"header crc mismatch";
- state->mode = BAD;
- break;
- }
- INITBITS();
- }
- - if (state->head != Z_NULL) {
- + if (state->head != NULL) {
- state->head->hcrc = (int)((state->flags >> 9) & 1);
- state->head->done = 1;
- }
- - strm->adler = state->check = crc32(0L, Z_NULL, 0);
- + strm->adler = state->check = crc32(0L, NULL, 0);
- state->mode = TYPE;
- break;
- #endif
- @@ -796,7 +810,7 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
- RESTORE();
- return Z_NEED_DICT;
- }
- - strm->adler = state->check = adler32(0L, Z_NULL, 0);
- + strm->adler = state->check = adler32(0L, NULL, 0);
- state->mode = TYPE;
- case TYPE:
- if (flush == Z_BLOCK || flush == Z_TREES)
- @@ -1155,10 +1169,10 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
- out -= left;
- strm->total_out += out;
- state->total += out;
- - if (out)
- + if ((state->wrap & 4) && out)
- strm->adler = state->check = UPDATE(state->check, put - out, out);
- out = left;
- - if ((
- + if ((state->wrap & 4) && (
- #ifdef GUNZIP
- state->flags ? hold :
- #endif
- @@ -1217,7 +1231,7 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
- strm->total_in += in;
- strm->total_out += out;
- state->total += out;
- - if (state->wrap && out)
- + if ((state->wrap & 4) && out)
- strm->adler = state->check = UPDATE(state->check, strm->next_out - out, out);
- strm->data_type = state->bits + (state->last ? 64 : 0) +
- (state->mode == TYPE ? 128 : 0) + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
- @@ -1228,13 +1242,13 @@ int ZEXPORT inflate(z_stream *strm, int flush) {
- int ZEXPORT inflateEnd(z_stream *strm) {
- struct inflate_state *state;
- - if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
- + if (inflateStateCheck(strm))
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)strm->state;
- - if (state->window != Z_NULL)
- + if (state->window != NULL)
- ZFREE(strm, state->window);
- ZFREE(strm, strm->state);
- - strm->state = Z_NULL;
- + strm->state = NULL;
- Tracev((stderr, "inflate: end\n"));
- return Z_OK;
- }
- @@ -1243,16 +1257,16 @@ int ZEXPORT inflateGetDictionary(z_stream *strm, unsigned char *dictionary, unsi
- struct inflate_state *state;
- /* check state */
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (inflateStateCheck(strm))
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)strm->state;
- /* copy dictionary */
- - if (state->whave && dictionary != Z_NULL) {
- + if (state->whave && dictionary != NULL) {
- memcpy(dictionary, state->window + state->wnext, state->whave - state->wnext);
- memcpy(dictionary + state->whave - state->wnext, state->window, state->wnext);
- }
- - if (dictLength != Z_NULL)
- + if (dictLength != NULL)
- *dictLength = state->whave;
- return Z_OK;
- }
- @@ -1263,7 +1277,7 @@ int ZEXPORT inflateSetDictionary(z_stream *strm, const unsigned char *dictionary
- int ret;
- /* check state */
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (inflateStateCheck(strm))
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)strm->state;
- if (state->wrap != 0 && state->mode != DICT)
- @@ -1271,7 +1285,7 @@ int ZEXPORT inflateSetDictionary(z_stream *strm, const unsigned char *dictionary
- /* check for correct dictionary identifier */
- if (state->mode == DICT) {
- - dictid = adler32(0L, Z_NULL, 0);
- + dictid = adler32(0L, NULL, 0);
- dictid = adler32(dictid, dictionary, dictLength);
- if (dictid != state->check)
- return Z_DATA_ERROR;
- @@ -1293,7 +1307,7 @@ int ZEXPORT inflateGetHeader(z_stream *strm, gz_headerp head) {
- struct inflate_state *state;
- /* check state */
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (inflateStateCheck(strm))
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)strm->state;
- if ((state->wrap & 2) == 0)
- @@ -1316,7 +1330,7 @@ int ZEXPORT inflateGetHeader(z_stream *strm, gz_headerp head) {
- called again with more data and the *have state. *have is initialized to
- zero for the first call.
- */
- -local unsigned syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len) {
- +static uint32_t syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len) {
- uint32_t got;
- uint32_t next;
- @@ -1342,7 +1356,7 @@ int ZEXPORT inflateSync(z_stream *strm) {
- struct inflate_state *state;
- /* check parameters */
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (inflateStateCheck(strm))
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)strm->state;
- if (strm->avail_in == 0 && state->bits < 8)
- @@ -1392,7 +1406,7 @@ int ZEXPORT inflateSync(z_stream *strm) {
- int ZEXPORT inflateSyncPoint(z_stream *strm) {
- struct inflate_state *state;
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (inflateStateCheck(strm))
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)strm->state;
- return state->mode == STORED && state->bits == 0;
- @@ -1405,20 +1419,19 @@ int ZEXPORT inflateCopy(z_stream *dest, z_stream *source) {
- unsigned wsize;
- /* check input */
- - if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
- - source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
- + if (inflateStateCheck(source) || dest == NULL)
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)source->state;
- /* allocate space */
- copy = (struct inflate_state *)
- ZALLOC(source, 1, sizeof(struct inflate_state));
- - if (copy == Z_NULL)
- + if (copy == NULL)
- return Z_MEM_ERROR;
- - window = Z_NULL;
- - if (state->window != Z_NULL) {
- + window = NULL;
- + if (state->window != NULL) {
- window = (unsigned char *) ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
- - if (window == Z_NULL) {
- + if (window == NULL) {
- ZFREE(source, copy);
- return Z_MEM_ERROR;
- }
- @@ -1427,12 +1440,13 @@ int ZEXPORT inflateCopy(z_stream *dest, z_stream *source) {
- /* copy state */
- memcpy((void *)dest, (void *)source, sizeof(z_stream));
- memcpy((void *)copy, (void *)state, sizeof(struct inflate_state));
- + copy->strm = dest;
- if (state->lencode >= state->codes && state->lencode <= state->codes + ENOUGH - 1) {
- copy->lencode = copy->codes + (state->lencode - state->codes);
- copy->distcode = copy->codes + (state->distcode - state->codes);
- }
- copy->next = copy->codes + (state->next - state->codes);
- - if (window != Z_NULL) {
- + if (window != NULL) {
- wsize = 1U << state->wbits;
- memcpy(window, state->window, wsize);
- }
- @@ -1444,7 +1458,7 @@ int ZEXPORT inflateCopy(z_stream *dest, z_stream *source) {
- int ZEXPORT inflateUndermine(z_stream *strm, int subvert) {
- struct inflate_state *state;
- - if (strm == Z_NULL || strm->state == Z_NULL)
- + if (inflateStateCheck(strm))
- return Z_STREAM_ERROR;
- state = (struct inflate_state *)strm->state;
- #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
- @@ -1456,12 +1470,34 @@ int ZEXPORT inflateUndermine(z_stream *strm, int subvert) {
- #endif
- }
- +int ZEXPORT inflateValidate(z_stream *strm, int check)
- +{
- + struct inflate_state *state;
- +
- + if (inflateStateCheck(strm))
- + return Z_STREAM_ERROR;
- + state = (struct inflate_state *)strm->state;
- + if (check)
- + state->wrap |= 4;
- + else
- + state->wrap &= ~4;
- + return Z_OK;
- +}
- +
- long ZEXPORT inflateMark(z_stream *strm) {
- struct inflate_state *state;
- - if (strm == Z_NULL || strm->state == Z_NULL)
- - return -1L << 16;
- + if (inflateStateCheck(strm))
- + return -65536;
- state = (struct inflate_state *)strm->state;
- return ((long)(state->back) << 16) + (state->mode == COPY ? state->length :
- (state->mode == MATCH ? state->was - state->length : 0));
- }
- +
- +unsigned long ZEXPORT inflateCodesUsed(z_stream *strm)
- +{
- + struct inflate_state *state;
- + if (inflateStateCheck(strm)) return (unsigned long)-1;
- + state = (struct inflate_state *)strm->state;
- + return (unsigned long)(state->next - state->codes);
- +}
- diff --git a/inflate.h b/inflate.h
- index 2bf129d..ee385c6 100644
- --- a/inflate.h
- +++ b/inflate.h
- @@ -21,7 +21,7 @@
- /* Possible inflate modes between inflate() calls */
- typedef enum {
- - HEAD, /* i: waiting for magic header */
- + HEAD = 16180, /* i: waiting for magic header */
- FLAGS, /* i: waiting for method and flags (gzip) */
- TIME, /* i: waiting for modification time (gzip) */
- OS, /* i: waiting for extra flags and operating system (gzip) */
- @@ -80,11 +80,14 @@ typedef enum {
- CHECK -> LENGTH -> DONE
- */
- -/* state maintained between inflate() calls. Approximately 10K bytes. */
- +/* State maintained between inflate() calls -- approximately 7K bytes, not
- + including the allocated sliding window, which is up to 32K bytes. */
- struct inflate_state {
- + z_streamp strm; /* pointer back to this zlib stream */
- inflate_mode mode; /* current inflate mode */
- int last; /* true if processing last block */
- - int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
- + int wrap; /* bit 0 true for zlib, bit 1 true for gzip,
- + bit 2 true to validate check value */
- int havedict; /* true if dictionary provided */
- int flags; /* gzip header method and flags (0 if zlib) */
- unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
- diff --git a/inftrees.c b/inftrees.c
- index e02272c..fa469d3 100644
- --- a/inftrees.c
- +++ b/inftrees.c
- @@ -47,7 +47,7 @@ int ZLIB_INTERNAL inflate_table(codetype type, uint16_t *lens, unsigned codes,
- code *next; /* next available space in table */
- const uint16_t *base; /* base value table to use */
- const uint16_t *extra; /* extra bits table to use */
- - int end; /* use base and extra for symbol > end */
- + unsigned match; /* use base and extra for symbol >= match */
- uint16_t count[MAXBITS+1]; /* number of codes of each length */
- uint16_t offs[MAXBITS+1]; /* offsets in table for each length */
- static const uint16_t lbase[31] = { /* Length codes 257..285 base */
- @@ -174,19 +174,17 @@ int ZLIB_INTERNAL inflate_table(codetype type, uint16_t *lens, unsigned codes,
- switch (type) {
- case CODES:
- base = extra = work; /* dummy value--not used */
- - end = 19;
- + match = 20;
- break;
- case LENS:
- base = lbase;
- - base -= 257;
- extra = lext;
- - extra -= 257;
- - end = 256;
- + match = 257;
- break;
- - default: /* DISTS */
- + case DISTS:
- base = dbase;
- extra = dext;
- - end = -1;
- + match = 0;
- }
- /* initialize state for loop */
- @@ -209,12 +207,12 @@ int ZLIB_INTERNAL inflate_table(codetype type, uint16_t *lens, unsigned codes,
- for (;;) {
- /* create table entry */
- here.bits = (unsigned char)(len - drop);
- - if ((int)(work[sym]) < end) {
- + if ((uint16_t)(work[sym] + 1) < match) {
- here.op = (unsigned char)0;
- here.val = work[sym];
- - } else if ((int)(work[sym]) > end) {
- - here.op = (unsigned char)(extra[work[sym]]);
- - here.val = base[work[sym]];
- + } else if (work[sym] >= match) {
- + here.op = (unsigned char)(extra[work[sym] - match]);
- + here.val = base[work[sym] - match];
- } else {
- here.op = (unsigned char)(32 + 64); /* end of block */
- here.val = 0;
- diff --git a/match.c b/match.c
- index b111cb4..cbfcb29 100644
- --- a/match.c
- +++ b/match.c
- @@ -143,7 +143,7 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) {
- * is pretty low, so for performance it's best to
- * outright stop here for the lower compression levels
- */
- - if (s->level < 6)
- + if (s->level < TRIGGER_LEVEL)
- break;
- }
- } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length);
- @@ -264,7 +264,7 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) {
- * is pretty low, so for performance it's best to
- * outright stop here for the lower compression levels
- */
- - if (s->level < 6)
- + if (s->level < TRIGGER_LEVEL)
- break;
- }
- } while (--chain_length && (cur_match = prev[cur_match & wmask]) > limit);
- @@ -276,6 +276,7 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) {
- #endif
- #ifdef std3_longest_match
- +
- /* longest_match() with minor change to improve performance (in terms of
- * execution time).
- *
- @@ -424,7 +425,6 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) {
- if (xor) {
- int match_byte = __builtin_ctzl(xor) / 8;
- scan += match_byte;
- - match += match_byte;
- break;
- } else {
- scan += sizeof(unsigned long);
- @@ -452,7 +452,7 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) {
- * is pretty low, so for performance it's best to
- * outright stop here for the lower compression levels
- */
- - if (s->level < 6)
- + if (s->level < TRIGGER_LEVEL)
- break;
- }
- } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length != 0);
- diff --git a/test/.gitignore b/test/.gitignore
- deleted file mode 100644
- index 2c3af0a..0000000
- --- a/test/.gitignore
- +++ /dev/null
- @@ -1,2 +0,0 @@
- -# ignore Makefiles; they're all automatically generated
- -Makefile
- diff --git a/test/CVE-2002-0059/test.gz b/test/CVE-2002-0059/test.gz
- deleted file mode 100644
- index c5c3e18..0000000
- Binary files a/test/CVE-2002-0059/test.gz and /dev/null differ
- diff --git a/test/CVE-2003-0107.c b/test/CVE-2003-0107.c
- deleted file mode 100644
- index 306421e..0000000
- --- a/test/CVE-2003-0107.c
- +++ /dev/null
- @@ -1,20 +0,0 @@
- -// http://www.securityfocus.com/archive/1/312869 --- originally by Richard Kettlewell
- -#include <stdlib.h>
- -#include <zlib.h>
- -#include <errno.h>
- -#include <stdio.h>
- -
- -int main(void) {
- -gzFile f;
- -int ret;
- -
- -if(!(f = gzopen("/dev/null", "w"))) {
- -perror("/dev/null");
- -exit(1);
- -}
- -ret = gzprintf(f, "%10240s", "");
- -printf("gzprintf -> %d\n", ret);
- -ret = gzclose(f);
- -printf("gzclose -> %d [%d]\n", ret, errno);
- -exit(0);
- -}
- diff --git a/test/CVE-2004-0797/test.gz b/test/CVE-2004-0797/test.gz
- deleted file mode 100644
- index 62dcf34..0000000
- Binary files a/test/CVE-2004-0797/test.gz and /dev/null differ
- diff --git a/test/CVE-2005-1849/test.gz b/test/CVE-2005-1849/test.gz
- deleted file mode 100644
- index b28f278..0000000
- Binary files a/test/CVE-2005-1849/test.gz and /dev/null differ
- diff --git a/test/CVE-2005-2096/test.gz b/test/CVE-2005-2096/test.gz
- deleted file mode 100644
- index 11590ae..0000000
- Binary files a/test/CVE-2005-2096/test.gz and /dev/null differ
- diff --git a/test/INDEX b/test/INDEX
- deleted file mode 100644
- index f167df8..0000000
- --- a/test/INDEX
- +++ /dev/null
- @@ -1,10 +0,0 @@
- -Makefile.in: template for Unix Makefile
- -
- -CVE-2003-0107.c:
- -CVE-2002-0059 :
- -CVE-2004-0797 :
- -CVE-2005-1849 :
- -CVE-2005-2096 : test cases for the relevant CVEs
- -
- -testCVEinputs.sh: script to run tests for CVEs where input data is supplied
- -
- diff --git a/test/Makefile.in b/test/Makefile.in
- deleted file mode 100644
- index 1fd8c00..0000000
- --- a/test/Makefile.in
- +++ /dev/null
- @@ -1,77 +0,0 @@
- -# Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
- -# Copyright 2015, Daniel Axtens, IBM Corporation
- -# zlib license, see zlib.h
- -
- -CC=
- -CFLAGS=
- -EXE=
- -SRCDIR=
- -SRCTOP=
- -INCLUDES=
- -TEST_LDFLAGS=-L.. ../libz.a
- -
- -COMPATTESTS =
- -
- -all: oldtests cvetests $(COMPATTESTS)
- -
- -oldtests: #set by ../configure
- -
- -teststatic:
- - @TMPST=tmpst_$$; \
- - if echo hello world | ../minigzip | ../minigzip -d && ../example $$TMPST ; then \
- - echo ' *** zlib test OK ***'; \
- - else \
- - echo ' *** zlib test FAILED ***'; exit 1; \
- - fi; \
- - rm -f $$TMPST
- -
- -testshared:
- - @LD_LIBRARY_PATH=`pwd`/..:$(LD_LIBRARY_PATH) ; export LD_LIBRARY_PATH; \
- - LD_LIBRARYN32_PATH=`pwd`/..:$(LD_LIBRARYN32_PATH) ; export LD_LIBRARYN32_PATH; \
- - DYLD_LIBRARY_PATH=`pwd`/..:$(DYLD_LIBRARY_PATH) ; export DYLD_LIBRARY_PATH; \
- - SHLIB_PATH=`pwd`/..:$(SHLIB_PATH) ; export SHLIB_PATH; \
- - TMPSH=tmpsh_$$; \
- - if echo hello world | ../minigzipsh | ../minigzipsh -d && ../examplesh $$TMPSH; then \
- - echo ' *** zlib shared test OK ***'; \
- - else \
- - echo ' *** zlib shared test FAILED ***'; exit 1; \
- - fi; \
- - rm -f $$TMPSH
- -
- -test64:
- - @TMP64=tmp64_$$; \
- - if echo hello world | ../minigzip64 | ../minigzip64 -d && ../example64 $$TMP64; then \
- - echo ' *** zlib 64-bit test OK ***'; \
- - else \
- - echo ' *** zlib 64-bit test FAILED ***'; exit 1; \
- - fi; \
- - rm -f $$TMP64
- -
- -cvetests: testCVEinputs
- -
- -# Tests requiring zlib-ng to be built with --zlib-compat
- -compattests: testCVE-2003-0107
- -
- -testCVEinputs:
- - @$(SRCDIR)/testCVEinputs.sh
- -
- -testCVE-2003-0107: CVE-2003-0107$(EXE)
- - @if ./CVE-2003-0107$(EXE); then \
- - echo ' *** zlib not vulnerable to CVE-2003-0107 ***'; \
- - else \
- - echo ' *** zlib VULNERABLE to CVE-2003-0107 ***'; exit 1; \
- - fi
- -
- -CVE-2003-0107.o: $(SRCDIR)/CVE-2003-0107.c
- - $(CC) $(CFLAGS) -I.. -I$(SRCTOP) -c -o $@ $(SRCDIR)/CVE-2003-0107.c
- -
- -CVE-2003-0107$(EXE): CVE-2003-0107.o
- - $(CC) $(CFLAGS) -o $@ CVE-2003-0107.o $(TEST_LDFLAGS)
- -
- -
- -clean:
- - rm -f *.o *.gcda *.gcno *.gcov
- - rm -f CVE-2003-0107$(EXE)
- -
- -distclean:
- - rm -f Makefile
- diff --git a/test/example.c b/test/example.c
- index 43d1a16..6d9bad3 100644
- --- a/test/example.c
- +++ b/test/example.c
- @@ -40,8 +40,8 @@ void test_dict_inflate (unsigned char *compr, size_t comprLen, unsigned char *u
- int main (int argc, char *argv[]);
- -static alloc_func zalloc = (alloc_func)0;
- -static free_func zfree = (free_func)0;
- +static alloc_func zalloc = NULL;
- +static free_func zfree = NULL;
- void test_compress (unsigned char *compr, size_t comprLen,
- unsigned char *uncompr, size_t uncomprLen);
- @@ -512,7 +512,7 @@ int main(int argc, char *argv[])
- /* compr and uncompr are cleared to avoid reading uninitialized
- * data and to ensure that uncompr compresses well.
- */
- - if (compr == Z_NULL || uncompr == Z_NULL) {
- + if (compr == NULL || uncompr == NULL) {
- printf("out of memory\n");
- exit(1);
- }
- diff --git a/test/infcover.c b/test/infcover.c
- index 5555a15..ee84636 100644
- --- a/test/infcover.c
- +++ b/test/infcover.c
- @@ -45,7 +45,7 @@
- allocated, then "msg" and information about the
- problem is printed to stderr. If everything is
- normal, nothing is printed. mem_done resets the
- - strm members to Z_NULL to use the default memory
- + strm members to NULL to use the default memory
- allocation routines on the next zlib initialization
- using strm.
- */
- @@ -226,9 +226,9 @@ static void mem_done(z_stream *strm, char *prefix)
- /* free the zone and delete from the stream */
- free(zone);
- - strm->opaque = Z_NULL;
- - strm->zalloc = Z_NULL;
- - strm->zfree = Z_NULL;
- + strm->opaque = NULL;
- + strm->zalloc = NULL;
- + strm->zfree = NULL;
- }
- /* -- inflate test routines -- */
- @@ -289,7 +289,7 @@ static void inf(char *hex, char *what, unsigned step, int win, unsigned len, int
- mem_setup(&strm);
- strm.avail_in = 0;
- - strm.next_in = Z_NULL;
- + strm.next_in = NULL;
- ret = inflateInit2(&strm, win);
- if (ret != Z_OK) {
- mem_done(&strm, what);
- @@ -351,12 +351,12 @@ static void cover_support(void)
- mem_setup(&strm);
- strm.avail_in = 0;
- - strm.next_in = Z_NULL;
- + strm.next_in = NULL;
- ret = inflateInit(&strm); assert(ret == Z_OK);
- mem_used(&strm, "inflate init");
- ret = inflatePrime(&strm, 5, 31); assert(ret == Z_OK);
- ret = inflatePrime(&strm, -1, 0); assert(ret == Z_OK);
- - ret = inflateSetDictionary(&strm, Z_NULL, 0);
- + ret = inflateSetDictionary(&strm, NULL, 0);
- assert(ret == Z_STREAM_ERROR);
- ret = inflateEnd(&strm); assert(ret == Z_OK);
- mem_done(&strm, "prime");
- @@ -369,13 +369,13 @@ static void cover_support(void)
- mem_setup(&strm);
- strm.avail_in = 0;
- - strm.next_in = Z_NULL;
- + strm.next_in = NULL;
- ret = inflateInit_(&strm, ZLIB_VERSION + 1, (int)sizeof(z_stream));
- assert(ret == Z_VERSION_ERROR);
- mem_done(&strm, "wrong version");
- strm.avail_in = 0;
- - strm.next_in = Z_NULL;
- + strm.next_in = NULL;
- ret = inflateInit(&strm); assert(ret == Z_OK);
- ret = inflateEnd(&strm); assert(ret == Z_OK);
- fputs("inflate built-in memory routines\n", stderr);
- @@ -388,9 +388,9 @@ static void cover_wrap(void)
- z_stream strm, copy;
- unsigned char dict[257];
- - ret = inflate(Z_NULL, 0); assert(ret == Z_STREAM_ERROR);
- - ret = inflateEnd(Z_NULL); assert(ret == Z_STREAM_ERROR);
- - ret = inflateCopy(Z_NULL, Z_NULL); assert(ret == Z_STREAM_ERROR);
- + ret = inflate(NULL, 0); assert(ret == Z_STREAM_ERROR);
- + ret = inflateEnd(NULL); assert(ret == Z_STREAM_ERROR);
- + ret = inflateCopy(NULL, NULL); assert(ret == Z_STREAM_ERROR);
- fputs("inflate bad parameters\n", stderr);
- inf("1f 8b 0 0", "bad gzip method", 0, 31, 0, Z_DATA_ERROR);
- @@ -409,7 +409,7 @@ static void cover_wrap(void)
- mem_setup(&strm);
- strm.avail_in = 0;
- - strm.next_in = Z_NULL;
- + strm.next_in = NULL;
- ret = inflateInit2(&strm, -8);
- strm.avail_in = 2;
- strm.next_in = (void *)"\x63";
- @@ -447,12 +447,12 @@ static unsigned pull(void *desc, const unsigned char **buf)
- static unsigned char dat[] = {0x63, 0, 2, 0};
- struct inflate_state *state;
- - if (desc == Z_NULL) {
- + if (desc == NULL) {
- next = 0;
- return 0; /* no input (already provided at next_in) */
- }
- state = (void *)((z_stream *)desc)->state;
- - if (state != Z_NULL)
- + if (state != NULL)
- state->mode = SYNC; /* force an otherwise impossible situation */
- return next < sizeof(dat) ? (*buf = dat + next++, 1) : 0;
- }
- @@ -460,7 +460,7 @@ static unsigned pull(void *desc, const unsigned char **buf)
- static int push(void *desc, unsigned char *buf, unsigned len)
- {
- buf += len;
- - return desc != Z_NULL; /* force error if desc not null */
- + return desc != NULL; /* force error if desc not null */
- }
- /* cover inflateBack() up to common deflate data cases and after those */
- @@ -470,27 +470,27 @@ static void cover_back(void)
- z_stream strm;
- unsigned char win[32768];
- - ret = inflateBackInit_(Z_NULL, 0, win, 0, 0);
- + ret = inflateBackInit_(NULL, 0, win, 0, 0);
- assert(ret == Z_VERSION_ERROR);
- - ret = inflateBackInit(Z_NULL, 0, win); assert(ret == Z_STREAM_ERROR);
- - ret = inflateBack(Z_NULL, Z_NULL, Z_NULL, Z_NULL, Z_NULL);
- + ret = inflateBackInit(NULL, 0, win); assert(ret == Z_STREAM_ERROR);
- + ret = inflateBack(NULL, NULL, NULL, NULL, NULL);
- assert(ret == Z_STREAM_ERROR);
- - ret = inflateBackEnd(Z_NULL); assert(ret == Z_STREAM_ERROR);
- + ret = inflateBackEnd(NULL); assert(ret == Z_STREAM_ERROR);
- fputs("inflateBack bad parameters\n", stderr);
- mem_setup(&strm);
- ret = inflateBackInit(&strm, 15, win); assert(ret == Z_OK);
- strm.avail_in = 2;
- strm.next_in = (void *)"\x03";
- - ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
- + ret = inflateBack(&strm, pull, NULL, push, NULL);
- assert(ret == Z_STREAM_END);
- /* force output error */
- strm.avail_in = 3;
- strm.next_in = (void *)"\x63\x00";
- - ret = inflateBack(&strm, pull, Z_NULL, push, &strm);
- + ret = inflateBack(&strm, pull, NULL, push, &strm);
- assert(ret == Z_BUF_ERROR);
- /* force mode error by mucking with state */
- - ret = inflateBack(&strm, pull, &strm, push, Z_NULL);
- + ret = inflateBack(&strm, pull, &strm, push, NULL);
- assert(ret == Z_STREAM_ERROR);
- ret = inflateBackEnd(&strm); assert(ret == Z_OK);
- mem_done(&strm, "inflateBack bad state");
- @@ -527,7 +527,7 @@ static int try(char *hex, char *id, int err)
- strcat(prefix, "-late");
- mem_setup(&strm);
- strm.avail_in = 0;
- - strm.next_in = Z_NULL;
- + strm.next_in = NULL;
- ret = inflateInit2(&strm, err < 0 ? 47 : -15);
- assert(ret == Z_OK);
- strm.avail_in = len;
- @@ -556,7 +556,7 @@ static int try(char *hex, char *id, int err)
- assert(ret == Z_OK);
- strm.avail_in = len;
- strm.next_in = in;
- - ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
- + ret = inflateBack(&strm, pull, NULL, push, NULL);
- assert(ret != Z_STREAM_ERROR);
- if (err) {
- assert(ret == Z_DATA_ERROR);
- diff --git a/test/minigzip.c b/test/minigzip.c
- index 9c71fd1..8f371ec 100644
- --- a/test/minigzip.c
- +++ b/test/minigzip.c
- @@ -111,7 +111,7 @@ gzFile gz_open(const char *path, int fd, const char *mode)
- gz->write = strchr(mode, 'w') != NULL;
- gz->strm.zalloc = myalloc;
- gz->strm.zfree = myfree;
- - gz->strm.opaque = Z_NULL;
- + gz->strm.opaque = NULL;
- gz->buf = malloc(gz->write ? BUFLENW : BUFLEN);
- if (gz->buf == NULL) {
- @@ -129,8 +129,8 @@ gzFile gz_open(const char *path, int fd, const char *mode)
- if (gz->write)
- ret = deflateInit2(&(gz->strm), level, 8, 15 + 16, 8, 0);
- else {
- - gz->strm.next_in = 0;
- - gz->strm.avail_in = Z_NULL;
- + gz->strm.next_in = NULL;
- + gz->strm.avail_in = 0;
- ret = inflateInit2(&(gz->strm), 15 + 16);
- }
- if (ret != Z_OK) {
- @@ -213,7 +213,7 @@ int gzclose(gzFile gz)
- return Z_STREAM_ERROR;
- strm = &(gz->strm);
- if (gz->write) {
- - strm->next_in = Z_NULL;
- + strm->next_in = NULL;
- strm->avail_in = 0;
- do {
- strm->next_out = gz->buf;
- @@ -239,7 +239,7 @@ const char *gzerror(gzFile gz, int *err)
- return gz->msg;
- }
- -#endif
- +#endif
- char *prog;
- diff --git a/test/testCVEinputs.sh b/test/testCVEinputs.sh
- deleted file mode 100755
- index 046856e..0000000
- --- a/test/testCVEinputs.sh
- +++ /dev/null
- @@ -1,22 +0,0 @@
- -#!/bin/bash
- -TESTDIR="$(dirname "$0")"
- -
- -CVEs="CVE-2002-0059 CVE-2004-0797 CVE-2005-1849 CVE-2005-2096"
- -
- -for CVE in $CVEs; do
- - fail=0
- - for testcase in ${TESTDIR}/${CVE}/*.gz; do
- - ../minigzip -d < "$testcase"
- - # we expect that a 1 error code is OK
- - # for a vulnerable failure we'd expect 134 or similar
- - if [ $? -ne 1 ]; then
- - fail=1
- - fi
- - done
- - if [ $fail -eq 0 ]; then
- - echo " --- zlib not vulnerable to $CVE ---";
- - else
- - echo " --- zlib VULNERABLE to $CVE ---"; exit 1;
- - fi
- -done
- -
- diff --git a/tools/list_intel_intrinsics.sh b/tools/list_intel_intrinsics.sh
- new file mode 100755
- index 0000000..dd6b4ac
- --- /dev/null
- +++ b/tools/list_intel_intrinsics.sh
- @@ -0,0 +1,3 @@
- +#!/bin/sh
- +
- +fgrep _mm_ "$@" | sed -e 's/.*\(_mm_[^\(]*\)(.*//g' | sort -u
- diff --git a/treebuild.xml b/treebuild.xml
- index 38d29d7..34a4f77 100644
- --- a/treebuild.xml
- +++ b/treebuild.xml
- @@ -101,7 +101,7 @@
- <!--
- CFLAGS=-O
- #CFLAGS=-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7
- -#CFLAGS=-g -DDEBUG
- +#CFLAGS=-g -DZLIB_DEBUG
- #CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \
- # -Wstrict-prototypes -Wmissing-prototypes
- diff --git a/trees.c b/trees.c
- index ee756fc..d600631 100644
- --- a/trees.c
- +++ b/trees.c
- @@ -36,7 +36,7 @@
- #include "deflate.h"
- -#ifdef DEBUG
- +#ifdef ZLIB_DEBUG
- # include <ctype.h>
- #endif
- @@ -56,16 +56,16 @@
- #define REPZ_11_138 18
- /* repeat a zero length 11-138 times (7 bits of repeat count) */
- -local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
- +static const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
- = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
- -local const int extra_dbits[D_CODES] /* extra bits for each distance code */
- +static const int extra_dbits[D_CODES] /* extra bits for each distance code */
- = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
- -local const int extra_blbits[BL_CODES] /* extra bits for each bit length code */
- +static const int extra_blbits[BL_CODES] /* extra bits for each bit length code */
- = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
- -local const unsigned char bl_order[BL_CODES]
- +static const unsigned char bl_order[BL_CODES]
- = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
- /* The lengths of the bit length codes are sent in order of decreasing
- * probability, to avoid transmitting the lengths for unused bit length codes.
- @@ -87,24 +87,24 @@ ZLIB_INTERNAL ct_data static_ltree[L_CODES+2];
- * below).
- */
- -local ct_data static_dtree[D_CODES];
- +static ct_data static_dtree[D_CODES];
- /* The static distance tree. (Actually a trivial tree since all codes use
- * 5 bits.)
- */
- -uch _dist_code[DIST_CODE_LEN];
- +unsigned char _dist_code[DIST_CODE_LEN];
- /* Distance codes. The first 256 values correspond to the distances
- * 3 .. 258, the last 256 values correspond to the top 8 bits of
- * the 15 bit distances.
- */
- -uch _length_code[MAX_MATCH-MIN_MATCH+1];
- +unsigned char _length_code[MAX_MATCH-MIN_MATCH+1];
- /* length code for each normalized match length (0 == MIN_MATCH) */
- -local int base_length[LENGTH_CODES];
- +static int base_length[LENGTH_CODES];
- /* First normalized length for each code (0 = MIN_MATCH) */
- -local int base_dist[D_CODES];
- +static int base_dist[D_CODES];
- /* First normalized distance for each code (0 = distance of 1) */
- #else
- @@ -119,43 +119,42 @@ struct static_tree_desc_s {
- unsigned int max_length; /* max bit length for the codes */
- };
- -local const static_tree_desc static_l_desc =
- +static const static_tree_desc static_l_desc =
- {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
- -local const static_tree_desc static_d_desc =
- +static const static_tree_desc static_d_desc =
- {static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
- -local const static_tree_desc static_bl_desc =
- +static const static_tree_desc static_bl_desc =
- {(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
- /* ===========================================================================
- * Local (static) routines in this file.
- */
- -local void tr_static_init (void);
- -local void init_block (deflate_state *s);
- -local void pqdownheap (deflate_state *s, ct_data *tree, int k);
- -local void gen_bitlen (deflate_state *s, tree_desc *desc);
- -local void gen_codes (ct_data *tree, int max_code, uint16_t *bl_count);
- -local void build_tree (deflate_state *s, tree_desc *desc);
- -local void scan_tree (deflate_state *s, ct_data *tree, int max_code);
- -local void send_tree (deflate_state *s, ct_data *tree, int max_code);
- -local int build_bl_tree (deflate_state *s);
- -local void send_all_trees (deflate_state *s, int lcodes, int dcodes, int blcodes);
- -local void compress_block (deflate_state *s, const ct_data *ltree, const ct_data *dtree);
- -local int detect_data_type (deflate_state *s);
- -local unsigned bi_reverse (unsigned value, int length);
- -local void bi_flush (deflate_state *s);
- -local void copy_block (deflate_state *s, char *buf, unsigned len, int header);
- +static void tr_static_init (void);
- +static void init_block (deflate_state *s);
- +static void pqdownheap (deflate_state *s, ct_data *tree, int k);
- +static void gen_bitlen (deflate_state *s, tree_desc *desc);
- +static void gen_codes (ct_data *tree, int max_code, uint16_t *bl_count);
- +static void build_tree (deflate_state *s, tree_desc *desc);
- +static void scan_tree (deflate_state *s, ct_data *tree, int max_code);
- +static void send_tree (deflate_state *s, ct_data *tree, int max_code);
- +static int build_bl_tree (deflate_state *s);
- +static void send_all_trees (deflate_state *s, int lcodes, int dcodes, int blcodes);
- +static void compress_block (deflate_state *s, const ct_data *ltree, const ct_data *dtree);
- +static int detect_data_type (deflate_state *s);
- +static unsigned bi_reverse (unsigned value, int length);
- +static void bi_flush (deflate_state *s);
- #ifdef GEN_TREES_H
- -local void gen_trees_header(void);
- +static void gen_trees_header (void);
- #endif
- /* ===========================================================================
- * Initialize the various 'constant' tables.
- */
- -local void tr_static_init(void) {
- +static void tr_static_init(void) {
- #if defined(GEN_TREES_H)
- static int static_init_done = 0;
- int n; /* iterates over tree elements */
- @@ -242,7 +241,7 @@ local void tr_static_init(void) {
- * Genererate the file trees.h describing the static trees.
- */
- #ifdef GEN_TREES_H
- -# ifndef DEBUG
- +# ifndef ZLIB_DEBUG
- # include <stdio.h>
- # endif
- @@ -255,6 +254,9 @@ void gen_trees_header() {
- int i;
- Assert(header != NULL, "Can't open trees.h");
- + fprintf(header, "#ifndef TREES_H_\n");
- + fprintf(header, "#define TREES_H_\n\n");
- +
- fprintf(header, "/* header created automatically with -DGEN_TREES_H */\n\n");
- fprintf(header, "ZLIB_INTERNAL const ct_data static_ltree[L_CODES+2] = {\n");
- @@ -262,7 +264,7 @@ void gen_trees_header() {
- fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
- }
- - fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
- + fprintf(header, "static const ct_data static_dtree[D_CODES] = {\n");
- for (i = 0; i < D_CODES; i++) {
- fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
- }
- @@ -277,16 +279,17 @@ void gen_trees_header() {
- fprintf(header, "%2u%s", _length_code[i], SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
- }
- - fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
- + fprintf(header, "static const int base_length[LENGTH_CODES] = {\n");
- for (i = 0; i < LENGTH_CODES; i++) {
- fprintf(header, "%d%s", base_length[i], SEPARATOR(i, LENGTH_CODES-1, 20));
- }
- - fprintf(header, "local const int base_dist[D_CODES] = {\n");
- + fprintf(header, "static const int base_dist[D_CODES] = {\n");
- for (i = 0; i < D_CODES; i++) {
- fprintf(header, "%5d%s", base_dist[i], SEPARATOR(i, D_CODES-1, 10));
- }
- + fprintf(header, "#endif /* TREES_H_ */\n");
- fclose(header);
- }
- #endif /* GEN_TREES_H */
- @@ -308,7 +311,7 @@ void ZLIB_INTERNAL _tr_init(deflate_state *s) {
- s->bi_buf = 0;
- s->bi_valid = 0;
- -#ifdef DEBUG
- +#ifdef ZLIB_DEBUG
- s->compressed_len = 0L;
- s->bits_sent = 0L;
- #endif
- @@ -320,7 +323,7 @@ void ZLIB_INTERNAL _tr_init(deflate_state *s) {
- /* ===========================================================================
- * Initialize a new block.
- */
- -local void init_block(deflate_state *s) {
- +static void init_block(deflate_state *s) {
- int n; /* iterates over tree elements */
- /* Initialize the trees. */
- @@ -365,7 +368,7 @@ local void init_block(deflate_state *s) {
- * when the heap property is re-established (each father smaller than its
- * two sons).
- */
- -local void pqdownheap(deflate_state *s, ct_data *tree, int k) {
- +static void pqdownheap(deflate_state *s, ct_data *tree, int k) {
- /* tree: the tree to restore */
- /* k: node to move down */
- int v = s->heap[k];
- @@ -399,7 +402,7 @@ local void pqdownheap(deflate_state *s, ct_data *tree, int k) {
- * The length opt_len is updated; static_len is also updated if stree is
- * not null.
- */
- -local void gen_bitlen(deflate_state *s, tree_desc *desc) {
- +static void gen_bitlen(deflate_state *s, tree_desc *desc) {
- /* desc: the tree descriptor */
- ct_data *tree = desc->dyn_tree;
- int max_code = desc->max_code;
- @@ -445,7 +448,7 @@ local void gen_bitlen(deflate_state *s, tree_desc *desc) {
- if (overflow == 0)
- return;
- - Trace((stderr, "\nbit length overflow\n"));
- + Tracev((stderr, "\nbit length overflow\n"));
- /* This happens for example on obj2 and pic of the Calgary corpus */
- /* Find the first bit length which could increase: */
- @@ -474,7 +477,7 @@ local void gen_bitlen(deflate_state *s, tree_desc *desc) {
- if (m > max_code)
- continue;
- if (tree[m].Len != bits) {
- - Trace((stderr, "code %d bits %d->%u\n", m, tree[m].Len, bits));
- + Tracev((stderr, "code %d bits %d->%u\n", m, tree[m].Len, bits));
- s->opt_len += (long)((bits - tree[m].Len) * tree[m].Freq);
- tree[m].Len = (uint16_t)bits;
- }
- @@ -491,7 +494,7 @@ local void gen_bitlen(deflate_state *s, tree_desc *desc) {
- * OUT assertion: the field code is set for all tree elements of non
- * zero code length.
- */
- -local void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) {
- +static void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) {
- /* tree: the tree to decorate */
- /* max_code: largest code with non zero frequency */
- /* bl_count: number of codes at each bit length */
- @@ -532,7 +535,7 @@ local void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) {
- * and corresponding code. The length opt_len is updated; static_len is
- * also updated if stree is not null. The field max_code is set.
- */
- -local void build_tree(deflate_state *s, tree_desc *desc) {
- +static void build_tree(deflate_state *s, tree_desc *desc) {
- /* desc: the tree descriptor */
- ct_data *tree = desc->dyn_tree;
- const ct_data *stree = desc->stat_desc->static_tree;
- @@ -620,7 +623,7 @@ local void build_tree(deflate_state *s, tree_desc *desc) {
- * Scan a literal or distance tree to determine the frequencies of the codes
- * in the bit length tree.
- */
- -local void scan_tree(deflate_state *s, ct_data *tree, int max_code) {
- +static void scan_tree(deflate_state *s, ct_data *tree, int max_code) {
- /* tree: the tree to be scanned */
- /* max_code: and its largest code of non zero frequency */
- int n; /* iterates over all tree elements */
- @@ -668,7 +671,7 @@ local void scan_tree(deflate_state *s, ct_data *tree, int max_code) {
- * Send a literal or distance tree in compressed form, using the codes in
- * bl_tree.
- */
- -local void send_tree(deflate_state *s, ct_data *tree, int max_code) {
- +static void send_tree(deflate_state *s, ct_data *tree, int max_code) {
- /* tree: the tree to be scanned */
- /* max_code and its largest code of non zero frequency */
- int n; /* iterates over all tree elements */
- @@ -726,7 +729,7 @@ local void send_tree(deflate_state *s, ct_data *tree, int max_code) {
- * Construct the Huffman tree for the bit lengths and return the index in
- * bl_order of the last bit length code to send.
- */
- -local int build_bl_tree(deflate_state *s) {
- +static int build_bl_tree(deflate_state *s) {
- int max_blindex; /* index of last bit length code of non zero freq */
- /* Determine the bit length frequencies for literal and distance trees */
- @@ -759,7 +762,7 @@ local int build_bl_tree(deflate_state *s) {
- * lengths of the bit length codes, the literal tree and the distance tree.
- * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
- */
- -local void send_all_trees(deflate_state *s, int lcodes, int dcodes, int blcodes) {
- +static void send_all_trees(deflate_state *s, int lcodes, int dcodes, int blcodes) {
- int rank; /* index in bl_order */
- Assert(lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
- @@ -789,11 +792,17 @@ void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, char *buf, unsigned long s
- /* stored_len: length of input block */
- /* last: one if this is the last block for a file */
- send_bits(s, (STORED_BLOCK << 1)+last, 3); /* send block type */
- -#ifdef DEBUG
- + bi_windup(s); /* align on byte boundary */
- + put_short(s, (uint16_t)stored_len);
- + put_short(s, (uint16_t)~stored_len);
- + memcpy(s->pending_buf + s->pending, buf, stored_len);
- + s->pending += stored_len;
- +#ifdef ZLIB_DEBUG
- s->compressed_len = (s->compressed_len + 3 + 7) & (unsigned long)~7L;
- s->compressed_len += (stored_len + 4) << 3;
- + s->bits_sent += 2*16;
- + s->bits_sent += stored_len<<3;
- #endif
- - copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
- }
- /* ===========================================================================
- @@ -810,7 +819,7 @@ void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s) {
- void ZLIB_INTERNAL _tr_align(deflate_state *s) {
- send_bits(s, STATIC_TREES << 1, 3);
- send_code(s, END_BLOCK, static_ltree);
- -#ifdef DEBUG
- +#ifdef ZLIB_DEBUG
- s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
- #endif
- bi_flush(s);
- @@ -884,14 +893,14 @@ void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, char *buf, unsigned long st
- #endif
- send_bits(s, (STATIC_TREES << 1)+last, 3);
- compress_block(s, (const ct_data *)static_ltree, (const ct_data *)static_dtree);
- -#ifdef DEBUG
- +#ifdef ZLIB_DEBUG
- s->compressed_len += 3 + s->static_len;
- #endif
- } else {
- send_bits(s, (DYN_TREES << 1)+last, 3);
- send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, max_blindex+1);
- compress_block(s, (const ct_data *)s->dyn_ltree, (const ct_data *)s->dyn_dtree);
- -#ifdef DEBUG
- +#ifdef ZLIB_DEBUG
- s->compressed_len += 3 + s->opt_len;
- #endif
- }
- @@ -903,7 +912,7 @@ void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, char *buf, unsigned long st
- if (last) {
- bi_windup(s);
- -#ifdef DEBUG
- +#ifdef ZLIB_DEBUG
- s->compressed_len += 7; /* align on byte boundary */
- #endif
- }
- @@ -961,13 +970,13 @@ int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc) {
- /* ===========================================================================
- * Send the block data compressed using the given Huffman trees
- */
- -local void compress_block(deflate_state *s, const ct_data *ltree, const ct_data *dtree) {
- +static void compress_block(deflate_state *s, const ct_data *ltree, const ct_data *dtree) {
- /* ltree: literal tree */
- /* dtree: distance tree */
- unsigned dist; /* distance of matched string */
- int lc; /* match length or unmatched char (if dist == 0) */
- unsigned lx = 0; /* running index in l_buf */
- - unsigned code; /* the code to send */
- + int code; /* the code to send */
- int extra; /* number of extra bits to send */
- if (s->last_lit != 0) {
- @@ -1019,7 +1028,7 @@ local void compress_block(deflate_state *s, const ct_data *ltree, const ct_data
- * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
- * IN assertion: the fields Freq of dyn_ltree are set.
- */
- -local int detect_data_type(deflate_state *s) {
- +static int detect_data_type(deflate_state *s) {
- /* black_mask is the bit mask of black-listed bytes
- * set bits 0..6, 14..25, and 28..31
- * 0xf3ffc07f = binary 11110011111111111100000001111111
- @@ -1050,7 +1059,7 @@ local int detect_data_type(deflate_state *s) {
- * method would use a table)
- * IN assertion: 1 <= len <= 15
- */
- -local unsigned bi_reverse(unsigned code, int len) {
- +static unsigned bi_reverse(unsigned code, int len) {
- /* code: the value to invert */
- /* len: its bit length */
- register unsigned res = 0;
- @@ -1064,7 +1073,7 @@ local unsigned bi_reverse(unsigned code, int len) {
- /* ===========================================================================
- * Flush the bit buffer, keeping at most 7 bits in it.
- */
- -local void bi_flush(deflate_state *s) {
- +static void bi_flush(deflate_state *s) {
- if (s->bi_valid == 16) {
- put_short(s, s->bi_buf);
- s->bi_buf = 0;
- @@ -1087,33 +1096,7 @@ ZLIB_INTERNAL void bi_windup(deflate_state *s) {
- }
- s->bi_buf = 0;
- s->bi_valid = 0;
- -#ifdef DEBUG
- +#ifdef ZLIB_DEBUG
- s->bits_sent = (s->bits_sent+7) & ~7;
- #endif
- }
- -
- -/* ===========================================================================
- - * Copy a stored block, storing first the length and its
- - * one's complement if requested.
- - */
- -local void copy_block(deflate_state *s, char *buf, unsigned len, int header) {
- - /* buf: the input data */
- - /* len: its length */
- - /* header: true if block header must be written */
- - bi_windup(s); /* align on byte boundary */
- -
- - if (header) {
- - put_short(s, (uint16_t)len);
- - put_short(s, (uint16_t)~len);
- -#ifdef DEBUG
- - s->bits_sent += 2*16;
- -#endif
- - }
- -#ifdef DEBUG
- - s->bits_sent += (unsigned long)len << 3;
- -#endif
- - while (len--) {
- - put_byte(s, *buf++);
- - }
- -}
- -
- diff --git a/trees.h b/trees.h
- index debfbdd..6fc1c84 100644
- --- a/trees.h
- +++ b/trees.h
- @@ -64,7 +64,7 @@ ZLIB_INTERNAL const ct_data static_ltree[L_CODES+2] = {
- {{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
- };
- -local const ct_data static_dtree[D_CODES] = {
- +static const ct_data static_dtree[D_CODES] = {
- {{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
- {{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
- {{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
- @@ -118,12 +118,12 @@ const unsigned char ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {
- 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
- };
- -local const int base_length[LENGTH_CODES] = {
- +static const int base_length[LENGTH_CODES] = {
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
- 64, 80, 96, 112, 128, 160, 192, 224, 0
- };
- -local const int base_dist[D_CODES] = {
- +static const int base_dist[D_CODES] = {
- 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
- 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
- 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
- diff --git a/uncompr.c b/uncompr.c
- index c2af140..7bf47b8 100644
- --- a/uncompr.c
- +++ b/uncompr.c
- @@ -1,5 +1,5 @@
- /* uncompr.c -- decompress a memory buffer
- - * Copyright (C) 1995-2003, 2010, 2014 Jean-loup Gailly, Mark Adler.
- + * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler.
- * For conditions of distribution and use, see copyright notice in zlib.h
- */
- @@ -9,26 +9,29 @@
- #include "zlib.h"
- /* ===========================================================================
- - Decompresses the source buffer into the destination buffer. sourceLen is
- - the byte length of the source buffer. Upon entry, destLen is the total
- - size of the destination buffer, which must be large enough to hold the
- - entire uncompressed data. (The size of the uncompressed data must have
- - been saved previously by the compressor and transmitted to the decompressor
- - by some mechanism outside the scope of this compression library.)
- - Upon exit, destLen is the actual size of the compressed buffer.
- + Decompresses the source buffer into the destination buffer. *sourceLen is
- + the byte length of the source buffer. Upon entry, *destLen is the total size
- + of the destination buffer, which must be large enough to hold the entire
- + uncompressed data. (The size of the uncompressed data must have been saved
- + previously by the compressor and transmitted to the decompressor by some
- + mechanism outside the scope of this compression library.) Upon exit,
- + *destLen is the size of the decompressed data and *sourceLen is the number
- + of source bytes consumed. Upon return, source + *sourceLen points to the
- + first unused input byte.
- - uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
- - enough memory, Z_BUF_ERROR if there was not enough room in the output
- - buffer, or Z_DATA_ERROR if the input data was corrupted, including if the
- - input data is an incomplete zlib stream.
- + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
- + memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
- + Z_DATA_ERROR if the input data was corrupted, including if the input data is
- + an incomplete zlib stream.
- */
- -int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsigned char *source, size_t sourceLen) {
- +int ZEXPORT uncompress2(unsigned char *dest, size_t *destLen, const unsigned char *source, size_t *sourceLen) {
- z_stream stream;
- int err;
- - const unsigned int max = (unsigned int)0 - 1;
- - size_t left;
- + const unsigned int max = (unsigned int)-1;
- + size_t len, left;
- unsigned char buf[1]; /* for detection of incomplete stream when *destLen == 0 */
- + len = *sourceLen;
- if (*destLen) {
- left = *destLen;
- *destLen = 0;
- @@ -40,8 +43,8 @@ int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsigned char
- stream.next_in = (const unsigned char *)source;
- stream.avail_in = 0;
- - stream.zalloc = (alloc_func)0;
- - stream.zfree = (free_func)0;
- + stream.zalloc = NULL;
- + stream.zfree = NULL;
- stream.opaque = NULL;
- err = inflateInit(&stream);
- @@ -56,12 +59,13 @@ int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsigned char
- left -= stream.avail_out;
- }
- if (stream.avail_in == 0) {
- - stream.avail_in = sourceLen > (unsigned long)max ? max : (unsigned int)sourceLen;
- - sourceLen -= stream.avail_in;
- + stream.avail_in = len > (unsigned long)max ? max : (unsigned int)len;
- + len -= stream.avail_in;
- }
- err = inflate(&stream, Z_NO_FLUSH);
- } while (err == Z_OK);
- + *sourceLen -= len + stream.avail_in;
- if (dest != buf)
- *destLen = stream.total_out;
- else if (stream.total_out && err == Z_BUF_ERROR)
- @@ -73,3 +77,8 @@ int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsigned char
- err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
- err;
- }
- +
- +int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsigned char *source, size_t sourceLen)
- +{
- + return uncompress2(dest, destLen, source, &sourceLen);
- +}
- diff --git a/zconf.h.in b/zconf.h.in
- index 7cacf1b..5649aee 100644
- --- a/zconf.h.in
- +++ b/zconf.h.in
- @@ -48,7 +48,7 @@
- Of course this will generally degrade compression (there's no free lunch).
- The memory requirements for inflate are (in bytes) 1 << windowBits
- - that is, 32K for windowBits=15 (default value) plus a few kilobytes
- + that is, 32K for windowBits=15 (default value) plus about 7 kilobytes
- for small objects.
- */
- @@ -118,9 +118,7 @@ typedef void *voidp;
- #include <sys/types.h> /* for off_t */
- #include <stdarg.h> /* for va_list */
- -#ifdef WIN32
- -# include <stddef.h> /* for wchar_t */
- -#endif
- +#include <stddef.h> /* for wchar_t and NULL */
- /* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and
- * "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even
- diff --git a/zlib.h b/zlib.h
- index 3a1cbb2..5ef1e30 100644
- --- a/zlib.h
- +++ b/zlib.h
- @@ -72,7 +72,8 @@ extern "C" {
- with "gz". The gzip format is different from the zlib format. gzip is a
- gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
- - This library can optionally read and write gzip streams in memory as well.
- + This library can optionally read and write gzip and raw deflate streams in
- + memory as well.
- The zlib format was designed to be compact and fast for use in memory
- and on communications channels. The gzip format was designed for single-
- @@ -81,7 +82,7 @@ extern "C" {
- The library does not install any signal handler. The decoder checks
- the consistency of the compressed data, so the library should never crash
- - even in case of corrupted input.
- + even in the case of corrupted input.
- */
- typedef void *(*alloc_func) (void *opaque, unsigned int items, unsigned int size);
- @@ -94,7 +95,7 @@ typedef struct z_stream_s {
- uint32_t avail_in; /* number of bytes available at next_in */
- size_t total_in; /* total number of input bytes read so far */
- - unsigned char *next_out; /* next output byte should be put there */
- + unsigned char *next_out; /* next output byte will go here */
- uint32_t avail_out; /* remaining free space at next_out */
- size_t total_out; /* total number of bytes output so far */
- @@ -105,8 +106,9 @@ typedef struct z_stream_s {
- free_func zfree; /* used to free the internal state */
- void *opaque; /* private data object passed to zalloc and zfree */
- - int data_type; /* best guess about the data type: binary or text */
- - uint32_t adler; /* adler32 value of the uncompressed data */
- + int data_type; /* best guess about the data type: binary or text
- + for deflate, or the decoding state for inflate */
- + uint32_t adler; /* Adler-32 or CRC-32 value of the uncompressed data */
- unsigned long reserved; /* reserved for future use */
- } z_stream;
- @@ -121,12 +123,12 @@ typedef struct gz_header_s {
- unsigned long time; /* modification time */
- int xflags; /* extra flags (not used when writing a gzip file) */
- int os; /* operating system */
- - unsigned char *extra; /* pointer to extra field or Z_NULL if none */
- - unsigned int extra_len; /* extra field length (valid if extra != Z_NULL) */
- + unsigned char *extra; /* pointer to extra field or NULL if none */
- + unsigned int extra_len; /* extra field length (valid if extra != NULL) */
- unsigned int extra_max; /* space at extra (only when reading header) */
- - unsigned char *name; /* pointer to zero-terminated file name or Z_NULL */
- + unsigned char *name; /* pointer to zero-terminated file name or NULL */
- unsigned int name_max; /* space at name (only when reading header) */
- - unsigned char *comment; /* pointer to zero-terminated comment or Z_NULL */
- + unsigned char *comment; /* pointer to zero-terminated comment or NULL */
- unsigned int comm_max; /* space at comment (only when reading header) */
- int hcrc; /* true if there was or will be a header crc */
- int done; /* true when done reading gzip header (not used when writing a gzip file) */
- @@ -146,13 +148,15 @@ typedef gz_header *gz_headerp;
- memory management. The compression library attaches no meaning to the
- opaque value.
- - zalloc must return Z_NULL if there is not enough memory for the object.
- + zalloc must return NULL if there is not enough memory for the object.
- If zlib is used in a multi-threaded application, zalloc and zfree must be
- - thread safe.
- + thread safe. In that case, zlib is thread-safe. When zalloc and zfree are
- + Z_NULL on entry to the initialization function, they are set to internal
- + routines that use the standard library functions malloc() and free().
- The fields total_in and total_out can be used for statistics or progress
- reports. After compression, total_in holds the total size of the
- - uncompressed data and may be saved for use in the decompressor (particularly
- + uncompressed data and may be saved for use by the decompressor (particularly
- if the decompressor wants to decompress everything in a single step).
- */
- @@ -197,12 +201,12 @@ typedef gz_header *gz_headerp;
- #define Z_TEXT 1
- #define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */
- #define Z_UNKNOWN 2
- -/* Possible values of the data_type field (though see inflate()) */
- +/* Possible values of the data_type field for deflate() */
- #define Z_DEFLATED 8
- /* The deflate compression method (the only one supported in this version) */
- -#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
- +#define Z_NULL NULL /* for compatibility with zlib, was for initializing zalloc, zfree, opaque */
- #define zlib_version zlibVersion()
- /* for compatibility with versions < 1.0.2 */
- @@ -222,7 +226,7 @@ ZEXTERN int ZEXPORT deflateInit (z_stream *strm, int level);
- Initializes the internal stream state for compression. The fields
- zalloc, zfree and opaque must be initialized before by the caller. If
- - zalloc and zfree are set to Z_NULL, deflateInit updates them to use default
- + zalloc and zfree are set to NULL, deflateInit updates them to use default
- allocation functions.
- The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
- @@ -255,11 +259,11 @@ ZEXTERN int ZEXPORT deflate(z_stream *strm, int flush);
- enough room in the output buffer), next_in and avail_in are updated and
- processing will resume at this point for the next call of deflate().
- - - Provide more output starting at next_out and update next_out and avail_out
- + - Generate more output starting at next_out and update next_out and avail_out
- accordingly. This action is forced if the parameter flush is non zero.
- Forcing flush frequently degrades the compression ratio, so this parameter
- - should be set only when necessary (in interactive applications). Some
- - output may be provided even if flush is not set.
- + should be set only when necessary. Some output may be provided even if
- + flush is zero.
- Before the call of deflate(), the application should ensure that at least
- one of the actions is possible, by providing more input and/or consuming more
- @@ -268,7 +272,9 @@ ZEXTERN int ZEXPORT deflate(z_stream *strm, int flush);
- output when it wants, for example when the output buffer is full (avail_out
- == 0), or after each call of deflate(). If deflate returns Z_OK and with
- zero avail_out, it must be called again after making room in the output
- - buffer because there might be more output pending.
- + buffer because there might be more output pending. See deflatePending(),
- + which can be used if desired to determine whether or not there is more ouput
- + in that case.
- Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
- decide how much data to accumulate before producing output, in order to
- @@ -289,8 +295,8 @@ ZEXTERN int ZEXPORT deflate(z_stream *strm, int flush);
- input data so far will be available to the decompressor, as for Z_SYNC_FLUSH.
- This completes the current deflate block and follows it with an empty fixed
- codes block that is 10 bits long. This assures that enough bytes are output
- - in order for the decompressor to finish the block before the empty fixed code
- - block.
- + in order for the decompressor to finish the block before the empty fixed
- + codes block.
- If flush is set to Z_BLOCK, a deflate block is completed and emitted, as
- for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to
- @@ -316,34 +322,38 @@ ZEXTERN int ZEXPORT deflate(z_stream *strm, int flush);
- If the parameter flush is set to Z_FINISH, pending input is processed,
- pending output is flushed and deflate returns with Z_STREAM_END if there was
- - enough output space; if deflate returns with Z_OK, this function must be
- - called again with Z_FINISH and more output space (updated avail_out) but no
- - more input data, until it returns with Z_STREAM_END or an error. After
- - deflate has returned Z_STREAM_END, the only possible operations on the stream
- - are deflateReset or deflateEnd.
- -
- - Z_FINISH can be used immediately after deflateInit if all the compression
- - is to be done in a single step. In this case, avail_out must be at least the
- - value returned by deflateBound (see below). Then deflate is guaranteed to
- - return Z_STREAM_END. If not enough output space is provided, deflate will
- - not return Z_STREAM_END, and it must be called again as described above.
- -
- - deflate() sets strm->adler to the adler32 checksum of all input read
- - so far (that is, total_in bytes).
- + enough output space. If deflate returns with Z_OK or Z_BUF_ERROR, this
- + function must be called again with Z_FINISH and more output space (updated
- + avail_out) but no more input data, until it returns with Z_STREAM_END or an
- + error. After deflate has returned Z_STREAM_END, the only possible operations
- + on the stream are deflateReset or deflateEnd.
- +
- + Z_FINISH can be used in the first deflate call after deflateInit if all the
- + compression is to be done in a single step. In order to complete in one
- + call, avail_out must be at least the value returned by deflateBound (see
- + below). Then deflate is guaranteed to return Z_STREAM_END. If not enough
- + output space is provided, deflate will not return Z_STREAM_END, and it must
- + be called again as described above.
- +
- + deflate() sets strm->adler to the Adler-32 checksum of all input read
- + so far (that is, total_in bytes). If a gzip stream is being generated, then
- + strm->adler will be the CRC-32 checksum of the input read so far. (See
- + deflateInit2 below.)
- deflate() may update strm->data_type if it can make a good guess about
- - the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered
- - binary. This field is only for information purposes and does not affect the
- - compression algorithm in any manner.
- + the input data type (Z_BINARY or Z_TEXT). If in doubt, the data is
- + considered binary. This field is only for information purposes and does not
- + affect the compression algorithm in any manner.
- deflate() returns Z_OK if some progress has been made (more input
- processed or more output produced), Z_STREAM_END if all input has been
- consumed and all output has been produced (only when flush is set to
- Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
- - if next_in or next_out was Z_NULL), Z_BUF_ERROR if no progress is possible
- - (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not
- - fatal, and deflate() can be called again with more input and more output
- - space to continue compressing.
- + if next_in or next_out was NULL) or the state was inadvertently written over
- + by the application), or Z_BUF_ERROR if no progress is possible (for example
- + avail_in or avail_out was zero). Note that Z_BUF_ERROR is not fatal, and
- + deflate() can be called again with more input and more output space to
- + continue compressing.
- */
- @@ -366,23 +376,20 @@ ZEXTERN int ZEXPORT inflateInit (z_stream *strm);
- Initializes the internal stream state for decompression. The fields
- next_in, avail_in, zalloc, zfree and opaque must be initialized before by
- - the caller. If next_in is not Z_NULL and avail_in is large enough (the
- - exact value depends on the compression method), inflateInit determines the
- - compression method from the zlib header and allocates all data structures
- - accordingly; otherwise the allocation will be deferred to the first call of
- - inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
- + the caller. In the current version of inflate, the provide input is not
- + read or consumed. Any memory allocation will be deferred to the first call
- + of inflate. If zalloc and zfree are set to NULL, inflateInit updates them to
- use default allocation functions.
- inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
- memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
- version assumed by the caller, or Z_STREAM_ERROR if the parameters are
- invalid, such as a null pointer to the structure. msg is set to null if
- - there is no error message. inflateInit does not perform any decompression
- - apart from possibly reading the zlib header if present: actual decompression
- - will be done by inflate(). (So next_in and avail_in may be modified, but
- - next_out and avail_out are unused and unchanged.) The current implementation
- - of inflateInit() does not process any header information -- that is deferred
- - until inflate() is called.
- + there is no error message. inflateInit does not perform any decompression.
- + Actual decompression will be done by inflate(). So next_in, and avail_in,
- + next_out, and avail_out are unused and unchanged. The current
- + implementation of inflateInit() does not process any header information --
- + that is deferred until inflate() is called.
- */
- @@ -398,17 +405,20 @@ ZEXTERN int ZEXPORT inflate(z_stream *strm, int flush);
- - Decompress more input starting at next_in and update next_in and avail_in
- accordingly. If not all input can be processed (because there is not
- - enough room in the output buffer), next_in is updated and processing will
- - resume at this point for the next call of inflate().
- + enough room in the output buffer), then next_in and avail_on are updated
- + accordingly, and processing will resume at this point for the next call of
- + inflate().
- - - Provide more output starting at next_out and update next_out and avail_out
- + - Generate more output starting at next_out and update next_out and avail_out
- accordingly. inflate() provides as much output as possible, until there is
- no more input data or no more space in the output buffer (see below about
- the flush parameter).
- Before the call of inflate(), the application should ensure that at least
- one of the actions is possible, by providing more input and/or consuming more
- - output, and updating the next_* and avail_* values accordingly. The
- + output, and updating the next_* and avail_* values accordingly. If the
- + caller of inflate() does not provide both available input and available
- + output space, it is possible that there will be no progress made. The
- application can consume the uncompressed output when it wants, for example
- when the output buffer is full (avail_out == 0), or after each call of
- inflate(). If inflate returns Z_OK and with zero avail_out, it must be
- @@ -425,7 +435,7 @@ ZEXTERN int ZEXPORT inflate(z_stream *strm, int flush);
- gets to the end of that block, or when it runs out of data.
- The Z_BLOCK option assists in appending to or combining deflate streams.
- - Also to assist in this, on return inflate() will set strm->data_type to the
- + To assist in this, on return inflate() always sets strm->data_type to the
- number of unused bits in the last byte taken from strm->next_in, plus 64 if
- inflate() is currently decoding the last block in the deflate stream, plus
- 128 if inflate() returned immediately after decoding an end-of-block code or
- @@ -451,7 +461,7 @@ ZEXTERN int ZEXPORT inflate(z_stream *strm, int flush);
- this case all pending input is processed and all pending output is flushed;
- avail_out must be large enough to hold all of the uncompressed data for the
- operation to complete. (The size of the uncompressed data may have been
- - saved by the compressor for this purpose.) The use of Z_FINISH is not
- + saved by the compressor for this purpose.) The use of Z_FINISH is not
- required to perform an inflation in one step. However it may be used to
- inform inflate that a faster approach can be used for the single inflate()
- call. Z_FINISH also informs inflate to not maintain a sliding window if the
- @@ -473,32 +483,33 @@ ZEXTERN int ZEXPORT inflate(z_stream *strm, int flush);
- chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
- strm->adler to the Adler-32 checksum of all output produced so far (that is,
- total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
- - below. At the end of the stream, inflate() checks that its computed adler32
- + below. At the end of the stream, inflate() checks that its computed Adler-32
- checksum is equal to that saved by the compressor and returns Z_STREAM_END
- only if the checksum is correct.
- inflate() can decompress and check either zlib-wrapped or gzip-wrapped
- deflate data. The header type is detected automatically, if requested when
- initializing with inflateInit2(). Any information contained in the gzip
- - header is not retained, so applications that need that information should
- - instead use raw inflate, see inflateInit2() below, or inflateBack() and
- - perform their own processing of the gzip header and trailer. When processing
- + header is not retained unless inflateGetHeader() is used. When processing
- gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output
- - producted so far. The CRC-32 is checked against the gzip trailer.
- + produced so far. The CRC-32 is checked against the gzip trailer, as is the
- + uncompressed length, modulo 2^32.
- inflate() returns Z_OK if some progress has been made (more input processed
- or more output produced), Z_STREAM_END if the end of the compressed data has
- been reached and all uncompressed output has been produced, Z_NEED_DICT if a
- preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
- corrupted (input stream not conforming to the zlib format or incorrect check
- - value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
- - next_in or next_out was Z_NULL), Z_MEM_ERROR if there was not enough memory,
- - Z_BUF_ERROR if no progress is possible or if there was not enough room in the
- - output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
- + value, in which case strm->msg points to a string with a more specific
- + error), Z_STREAM_ERROR if the stream structure was inconsistent (for example
- + next_in or next_out was NULL, or the state was inadvertently written over
- + by the application), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR
- + if no progress is possible or if there was not enough room in the output
- + buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
- inflate() can be called again with more input and more output space to
- continue decompressing. If Z_DATA_ERROR is returned, the application may
- then call inflateSync() to look for a good compression block if a partial
- - recovery of the data is desired.
- + recovery of the data is to be attempted.
- */
- @@ -508,9 +519,8 @@ ZEXTERN int ZEXPORT inflateEnd(z_stream *strm);
- This function discards any unprocessed input and does not flush any pending
- output.
- - inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
- - was inconsistent. In the error case, msg may be set but then points to a
- - static string (which must not be deallocated).
- + inflateEnd returns Z_OK if success, or Z_STREAM_ERROR if the stream state
- + was inconsistent.
- */
- @@ -551,14 +561,19 @@ ZEXTERN int ZEXPORT deflateInit2 (z_stream *strm,
- windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
- determines the window size. deflate() will then generate raw deflate data
- - with no zlib header or trailer, and will not compute an adler32 check value.
- + with no zlib header or trailer, and will not compute a check value.
- windowBits can also be greater than 15 for optional gzip encoding. Add
- 16 to windowBits to write a simple gzip header and trailer around the
- compressed data instead of a zlib wrapper. The gzip header will have no
- file name, no extra data, no comment, no modification time (set to zero), no
- - header crc, and the operating system will be set to 255 (unknown). If a
- - gzip stream is being written, strm->adler is a crc32 instead of an adler32.
- + header crc, and the operating system will be set to the appropriate value,
- + if the operating system was determined at compile time. If a gzip stream is
- + being written, strm->adler is a CRC-32 instead of an Adler-32.
- +
- + For raw deflate or gzip encoding, a request for a 256-byte window is
- + rejected as invalid, since only the zlib header provides a means of
- + transmitting the window size to the decompressor.
- The memLevel parameter specifies how much memory should be allocated
- for the internal compression state. memLevel=1 uses minimum memory but is
- @@ -619,15 +634,15 @@ ZEXTERN int ZEXPORT deflateSetDictionary(z_stream *strm,
- addition, the current implementation of deflate will use at most the window
- size minus 262 bytes of the provided dictionary.
- - Upon return of this function, strm->adler is set to the adler32 value
- + Upon return of this function, strm->adler is set to the Adler-32 value
- of the dictionary; the decompressor may later use this value to determine
- - which dictionary has been used by the compressor. (The adler32 value
- + which dictionary has been used by the compressor. (The Adler-32 value
- applies to the whole dictionary even if only a subset of the dictionary is
- actually used by the compressor.) If a raw deflate was requested, then the
- - adler32 value is not computed and strm->adler is not set.
- + Adler-32 value is not computed and strm->adler is not set.
- deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
- - parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is
- + parameter is invalid (e.g. dictionary being NULL) or the stream state is
- inconsistent (for example if deflate has already been called for this stream
- or if not at a block boundary for raw deflate). deflateSetDictionary does
- not perform any compression: this will be done by deflate().
- @@ -646,7 +661,7 @@ ZEXTERN int ZEXPORT deflateCopy(z_stream *dest, z_stream *source);
- deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
- enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
- - (such as zalloc being Z_NULL). msg is left unchanged in both source and
- + (such as zalloc being NULL). msg is left unchanged in both source and
- destination.
- */
- @@ -658,7 +673,7 @@ ZEXTERN int ZEXPORT deflateReset(z_stream *strm);
- set unchanged.
- deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
- - stream state was inconsistent (such as zalloc or state being Z_NULL).
- + stream state was inconsistent (such as zalloc or state being NULL).
- */
- ZEXTERN int ZEXPORT deflateParams(z_stream *strm, int level, int strategy);
- @@ -674,24 +689,25 @@ ZEXTERN int ZEXPORT deflateParams(z_stream *strm, int level, int strategy);
- new level and strategy will take effect at the next call of deflate().
- If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does
- - not have enough output space to complete, then the parameter change will
- - take effect at an undetermined location in the uncompressed data provided so
- - far. In order to assure a change in the parameters at a specific location
- - in the uncompressed data, the deflate stream should first be flushed with
- - Z_BLOCK or another flush parameter, and deflate() called until
- - strm.avail_out is not zero, before the call of deflateParams(). Then no
- - more input data should be provided before the deflateParams() call. If this
- - is done, the old level and strategy will be applied to the data compressed
- - before deflateParams(), and the new level and strategy will be applied to
- - the the data compressed after deflateParams().
- -
- - deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source stream
- + not have enough output space to complete, then the parameter change will not
- + take effect. In this case, deflateParams() can be called again with the
- + same parameters and more output space to try again.
- +
- + In order to assure a change in the parameters on the first try, the
- + deflate stream should be flushed using deflate() with Z_BLOCK or other flush
- + request until strm.avail_out is not zero, before calling deflateParams().
- + Then no more input data should be provided before the deflateParams() call.
- + If this is done, the old level and strategy will be applied to the data
- + compressed before deflateParams(), and the new level and strategy will be
- + applied to the the data compressed after deflateParams().
- +
- + deflateParams returns Z_OK on success, Z_STREAM_ERROR if the source stream
- state was inconsistent or if a parameter was invalid, or Z_BUF_ERROR if
- - there was not enough output space to complete the compression before the
- - parameters were changed. Note that in the case of a Z_BUF_ERROR, the
- - parameters are changed nevertheless, and will take effect at an undetermined
- - location in the previously supplied uncompressed data. Compression may
- - proceed after a Z_BUF_ERROR.
- + there was not enough output space to complete the compression of the
- + available input data before a change in the strategy or approach. Note that
- + in the case of a Z_BUF_ERROR, the parameters are not changed. A return
- + value of Z_BUF_ERROR is not fatal, in which case deflateParams() can be
- + retried with more output space.
- */
- ZEXTERN int ZEXPORT deflateTune(z_stream *strm, int good_length, int max_lazy, int nice_length, int max_chain);
- @@ -728,7 +744,7 @@ ZEXTERN int ZEXPORT deflatePending(z_stream *strm, uint32_t *pending, int *bits)
- provided would be due to the available output space having being consumed.
- The number of bits of output not provided are between 0 and 7, where they
- await more bits to join them in order to fill out a full byte. If pending
- - or bits are Z_NULL, then those values are not set.
- + or bits are NULL, then those values are not set.
- deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source
- stream state was inconsistent.
- @@ -757,8 +773,8 @@ ZEXTERN int ZEXPORT deflateSetHeader(z_stream *strm, gz_headerp head);
- deflate(). The text, time, os, extra field, name, and comment information
- in the provided gz_header structure are written to the gzip header (xflag is
- ignored -- the extra flags are set according to the compression level). The
- - caller must assure that, if not Z_NULL, name and comment are terminated with
- - a zero byte, and that if extra is not Z_NULL, that extra_len bytes are
- + caller must assure that, if not NULL, name and comment are terminated with
- + a zero byte, and that if extra is not NULL, that extra_len bytes are
- available there. If hcrc is true, a gzip header crc is included. Note that
- the current versions of the command-line version of gzip (up through version
- 1.3.x) do not support header crc's, and will report that it is a "multi-part
- @@ -798,7 +814,7 @@ ZEXTERN int ZEXPORT inflateInit2(z_stream *strm, int windowBits);
- is for use with other formats that use the deflate compressed data format
- such as zip. Those formats provide their own check values. If a custom
- format is developed using the raw deflate format for compressed data, it is
- - recommended that a check value such as an adler32 or a crc32 be applied to
- + recommended that a check value such as an Adler-32 or a CRC-32 be applied to
- the uncompressed data as is done in the zlib, gzip, and zip formats. For
- most applications, the zlib format should be used as is. Note that comments
- above on the use in deflateInit2() applies to the magnitude of windowBits.
- @@ -807,7 +823,10 @@ ZEXTERN int ZEXPORT inflateInit2(z_stream *strm, int windowBits);
- 32 to windowBits to enable zlib and gzip decoding with automatic header
- detection, or add 16 to decode only the gzip format (the zlib format will
- return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a
- - crc32 instead of an adler32.
- + CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see
- + below), inflate() will not automatically decode concatenated gzip streams.
- + inflate() will return Z_STREAM_END at the end of the gzip stream. The state
- + would need to be reset to continue decoding a subsequent gzip stream.
- inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
- memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
- @@ -826,7 +845,7 @@ ZEXTERN int ZEXPORT inflateSetDictionary(z_stream *strm, const unsigned char *di
- Initializes the decompression dictionary from the given uncompressed byte
- sequence. This function must be called immediately after a call of inflate,
- if that call returned Z_NEED_DICT. The dictionary chosen by the compressor
- - can be determined from the adler32 value returned by that call of inflate.
- + can be determined from the Adler-32 value returned by that call of inflate.
- The compressor and decompressor must use exactly the same dictionary (see
- deflateSetDictionary). For raw inflate, this function can be called at any
- time to set the dictionary. If the provided dictionary is smaller than the
- @@ -835,9 +854,9 @@ ZEXTERN int ZEXPORT inflateSetDictionary(z_stream *strm, const unsigned char *di
- that was used for compression is provided.
- inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
- - parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is
- + parameter is invalid (e.g. dictionary being NULL) or the stream state is
- inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
- - expected one (incorrect adler32 value). inflateSetDictionary does not
- + expected one (incorrect Adler-32 value). inflateSetDictionary does not
- perform any decompression: this will be done by subsequent calls of
- inflate().
- */
- @@ -848,8 +867,8 @@ ZEXTERN int ZEXPORT inflateGetDictionary(z_stream *strm, unsigned char *dictiona
- set to the number of bytes in the dictionary, and that many bytes are copied
- to dictionary. dictionary must have enough space, where 32768 bytes is
- always enough. If inflateGetDictionary() is called with dictionary equal to
- - Z_NULL, then only the dictionary length is returned, and nothing is copied.
- - Similary, if dictLength is Z_NULL, then it is not set.
- + NULL, then only the dictionary length is returned, and nothing is copied.
- + Similary, if dictLength is NULL, then it is not set.
- inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the
- stream state is inconsistent.
- @@ -885,28 +904,30 @@ ZEXTERN int ZEXPORT inflateCopy(z_stream *dest, z_stream *source);
- inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
- enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
- - (such as zalloc being Z_NULL). msg is left unchanged in both source and
- + (such as zalloc being NULL). msg is left unchanged in both source and
- destination.
- */
- ZEXTERN int ZEXPORT inflateReset(z_stream *strm);
- /*
- This function is equivalent to inflateEnd followed by inflateInit,
- - but does not free and reallocate all the internal decompression state. The
- + but does not free and reallocate the internal decompression state. The
- stream will keep attributes that may have been set by inflateInit2.
- inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
- - stream state was inconsistent (such as zalloc or state being Z_NULL).
- + stream state was inconsistent (such as zalloc or state being NULL).
- */
- ZEXTERN int ZEXPORT inflateReset2(z_stream *strm, int windowBits);
- /*
- This function is the same as inflateReset, but it also permits changing
- the wrap and window size requests. The windowBits parameter is interpreted
- - the same as it is for inflateInit2.
- + the same as it is for inflateInit2. If the window size is changed, then the
- + memory allocated for the window is freed, and the window will be reallocated
- + by inflate() if needed.
- inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source
- - stream state was inconsistent (such as zalloc or state being Z_NULL), or if
- + stream state was inconsistent (such as zalloc or state being NULL), or if
- the windowBits parameter is invalid.
- */
- @@ -953,7 +974,7 @@ ZEXTERN long ZEXPORT inflateMark(z_stream *strm);
- location in the input stream can be determined from avail_in and data_type
- as noted in the description for the Z_BLOCK flush parameter for inflate.
- - inflateMark returns the value noted above or -65536 if the provided
- + inflateMark returns the value noted above, or -65536 if the provided
- source stream state was inconsistent.
- */
- @@ -971,16 +992,16 @@ ZEXTERN int ZEXPORT inflateGetHeader(z_stream *strm, gz_headerp head);
- The text, time, xflags, and os fields are filled in with the gzip header
- contents. hcrc is set to true if there is a header CRC. (The header CRC
- - was valid if done is set to one.) If extra is not Z_NULL, then extra_max
- + was valid if done is set to one.) If extra is not NULL, then extra_max
- contains the maximum number of bytes to write to extra. Once done is true,
- extra_len contains the actual extra field length, and extra contains the
- extra field, or that field truncated if extra_max is less than extra_len.
- - If name is not Z_NULL, then up to name_max characters are written there,
- + If name is not NULL, then up to name_max characters are written there,
- terminated with a zero unless the length is greater than name_max. If
- - comment is not Z_NULL, then up to comm_max characters are written there,
- + comment is not NULL, then up to comm_max characters are written there,
- terminated with a zero unless the length is greater than comm_max. When any
- - of extra, name, or comment are not Z_NULL and the respective field is not
- - present in the header, then that field is set to Z_NULL to signal its
- + of extra, name, or comment are not NULL and the respective field is not
- + present in the header, then that field is set to NULL to signal its
- absence. This allows the use of deflateSetHeader() with the returned
- structure to duplicate the header. However if those fields are set to
- allocated memory, then the application will need to save those pointers
- @@ -1001,7 +1022,7 @@ ZEXTERN int ZEXPORT inflateBackInit (z_stream *strm, int windowBits, unsigned ch
- Initialize the internal stream state for decompression using inflateBack()
- calls. The fields zalloc, zfree and opaque in strm must be initialized
- - before the call. If zalloc and zfree are Z_NULL, then the default library-
- + before the call. If zalloc and zfree are NULL, then the default library-
- derived memory allocation routines are used. windowBits is the base two
- logarithm of the window size, in the range 8..15. window is a caller
- supplied buffer of that size. Except for special applications where it is
- @@ -1040,9 +1061,9 @@ ZEXTERN int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_f
- This routine would normally be used in a utility that reads zip or gzip
- files and writes out uncompressed files. The utility would decode the
- header and process the trailer on its own, hence this routine expects only
- - the raw deflate stream to decompress. This is different from the normal
- - behavior of inflate(), which expects either a zlib or gzip header and
- - trailer around the deflate stream.
- + the raw deflate stream to decompress. This is different from the default
- + behavior of inflate(), which expects a zlib header and trailer around the
- + deflate stream.
- inflateBack() uses two subroutines supplied by the caller that are then
- called by inflateBack() for input and output. inflateBack() calls those
- @@ -1051,12 +1072,12 @@ ZEXTERN int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_f
- parameters and return types are defined above in the in_func and out_func
- typedefs. inflateBack() will call in(in_desc, &buf) which should return the
- number of bytes of provided input, and a pointer to that input in buf. If
- - there is no input available, in() must return zero--buf is ignored in that
- - case--and inflateBack() will return a buffer error. inflateBack() will call
- - out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out()
- - should return zero on success, or non-zero on failure. If out() returns
- - non-zero, inflateBack() will return with an error. Neither in() nor out()
- - are permitted to change the contents of the window provided to
- + there is no input available, in() must return zero -- buf is ignored in that
- + case -- and inflateBack() will return a buffer error. inflateBack() will
- + call out(out_desc, buf, len) to write the uncompressed data buf[0..len-1].
- + out() should return zero on success, or non-zero on failure. If out()
- + returns non-zero, inflateBack() will return with an error. Neither in() nor
- + out() are permitted to change the contents of the window provided to
- inflateBackInit(), which is also the buffer that out() uses to write from.
- The length written by out() will be at most the window size. Any non-zero
- amount of input may be provided by in().
- @@ -1064,8 +1085,8 @@ ZEXTERN int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_f
- For convenience, inflateBack() can be provided input on the first call by
- setting strm->next_in and strm->avail_in. If that input is exhausted, then
- in() will be called. Therefore strm->next_in must be initialized before
- - calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called
- - immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in
- + calling inflateBack(). If strm->next_in is NULL, then in() will be called
- + immediately for input. If strm->next_in is not NULL, then strm->avail_in
- must also be initialized, and then if strm->avail_in is not zero, input will
- initially be taken from strm->next_in[0 .. strm->avail_in - 1].
- @@ -1081,10 +1102,10 @@ ZEXTERN int ZEXPORT inflateBack(z_stream *strm, in_func in, void *in_desc, out_f
- in the deflate stream (in which case strm->msg is set to indicate the nature
- of the error), or Z_STREAM_ERROR if the stream was not properly initialized.
- In the case of Z_BUF_ERROR, an input or output error can be distinguished
- - using strm->next_in which will be Z_NULL only if in() returned an error. If
- - strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning
- + using strm->next_in which will be NULL only if in() returned an error. If
- + strm->next_in is not NULL, then the Z_BUF_ERROR was due to out() returning
- non-zero. (in() will always be called before out(), so strm->next_in is
- - assured to be defined if out() returns non-zero.) Note that inflateBack()
- + assured to be defined if out() returns non-zero.) Note that inflateBack()
- cannot return Z_OK.
- */
- @@ -1106,7 +1127,7 @@ ZEXTERN unsigned long ZEXPORT zlibCompileFlags(void);
- 7.6: size of z_off_t
- Compiler, assembler, and debug options:
- - 8: DEBUG
- + 8: ZLIB_DEBUG
- 9: ASMV or ASMINF -- use ASM code
- 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention
- 11: 0 (reserved)
- @@ -1192,7 +1213,7 @@ ZEXTERN int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsig
- uncompressed data. (The size of the uncompressed data must have been saved
- previously by the compressor and transmitted to the decompressor by some
- mechanism outside the scope of this compression library.) Upon exit, destLen
- - is the actual size of the uncompressed buffer.
- + is the actual size of the uncompressed data.
- uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
- enough memory, Z_BUF_ERROR if there was not enough room in the output
- @@ -1201,6 +1222,16 @@ ZEXTERN int ZEXPORT uncompress(unsigned char *dest, size_t *destLen, const unsig
- buffer with the uncompressed data up to that point.
- */
- +
- +ZEXTERN int ZEXPORT uncompress2 (unsigned char *dest, size_t *destLen,
- + const unsigned char *source, size_t *sourceLen);
- +/*
- + Same as uncompress, except that sourceLen is a pointer, where the
- + length of the source is *sourceLen. On return, *sourceLen is the number of
- + source bytes consumed.
- +*/
- +
- +
- #ifdef WITH_GZFILEOP
- /* gzip file access functions */
- @@ -1293,10 +1324,12 @@ ZEXTERN int ZEXPORT gzbuffer(gzFile file, unsigned size);
- ZEXTERN int ZEXPORT gzsetparams(gzFile file, int level, int strategy);
- /*
- Dynamically update the compression level or strategy. See the description
- - of deflateInit2 for the meaning of these parameters.
- + of deflateInit2 for the meaning of these parameters. Previously provided
- + data is flushed before the parameter change.
- - gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
- - opened for writing.
- + gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not
- + opened for writing, Z_ERRNO if there is an error writing the flushed data,
- + or Z_MEM_ERROR if there is a memory allocation error.
- */
- ZEXTERN int ZEXPORT gzread(gzFile file, void *buf, unsigned len);
- @@ -1324,7 +1357,32 @@ ZEXTERN int ZEXPORT gzread(gzFile file, void *buf, unsigned len);
- case.
- gzread returns the number of uncompressed bytes actually read, less than
- - len for end of file, or -1 for error.
- + len for end of file, or -1 for error. If len is too large to fit in an int,
- + then nothing is read, -1 is returned, and the error state is set to
- + Z_STREAM_ERROR.
- +*/
- +
- +ZEXTERN size_t ZEXPORT gzfread (void *buf, size_t size, size_t nitems, gzFile file);
- +/*
- + Read up to nitems items of size size from file to buf, otherwise operating
- + as gzread() does. This duplicates the interface of stdio's fread(), with
- + size_t request and return types.
- +
- + gzfread() returns the number of full items read of size size, or zero if
- + the end of the file was reached and a full item could not be read, or if
- + there was an error. gzerror() must be consulted if zero is returned in
- + order to determine if there was an error. If the multiplication of size and
- + nitems overflows, i.e. the product does not fit in a size_t, then nothing
- + is read, zero is returned, and the error state is set to Z_STREAM_ERROR.
- +
- + In the event that the end of file is reached and only a partial item is
- + available at the end, i.e. the remaining uncompressed data length is not a
- + multiple of size, then the final partial item is nevertheless read into buf
- + and the end-of-file flag is set. The length of the partial item read is not
- + provided, but could be inferred from the result of gztell(). This behavior
- + is the same as the behavior of fread() implementations in common libraries,
- + but it prevents the direct use of gzfread() to read a concurrently written
- + file, reseting and retrying on end-of-file, when size is not 1.
- */
- ZEXTERN int ZEXPORT gzwrite(gzFile file, void const *buf, unsigned len);
- @@ -1334,19 +1392,31 @@ ZEXTERN int ZEXPORT gzwrite(gzFile file, void const *buf, unsigned len);
- error.
- */
- +ZEXTERN size_t ZEXPORT gzfwrite(void const *buf, size_t size,
- + size_t nitems, gzFile file);
- +/*
- + gzfwrite() writes nitems items of size size from buf to file, duplicating
- + the interface of stdio's fwrite(), with size_t request and return types.
- +
- + gzfwrite() returns the number of full items written of size size, or zero
- + if there was an error. If the multiplication of size and nitems overflows,
- + i.e. the product does not fit in a size_t, then nothing is written, zero
- + is returned, and the error state is set to Z_STREAM_ERROR.
- +*/
- +
- ZEXTERN int ZEXPORTVA gzprintf(gzFile file, const char *format, ...);
- /*
- Converts, formats, and writes the arguments to the compressed file under
- control of the format string, as in fprintf. gzprintf returns the number of
- - uncompressed bytes actually written, or 0 in case of error. The number of
- - uncompressed bytes written is limited to 8191, or one less than the buffer
- - size given to gzbuffer(). The caller should assure that this limit is not
- - exceeded. If it is exceeded, then gzprintf() will return an error (0) with
- - nothing written. In this case, there may also be a buffer overflow with
- - unpredictable consequences, which is possible only if zlib was compiled with
- - the insecure functions sprintf() or vsprintf() because the secure snprintf()
- - or vsnprintf() functions were not available. This can be determined using
- - zlibCompileFlags().
- + uncompressed bytes actually written, or a negative zlib error code in case
- + of error. The number of uncompressed bytes written is limited to 8191, or
- + one less than the buffer size given to gzbuffer(). The caller should assure
- + that this limit is not exceeded. If it is exceeded, then gzprintf() will
- + return an error (0) with nothing written. In this case, there may also be a
- + buffer overflow with unpredictable consequences, which is possible only if
- + zlib was compiled with the insecure functions sprintf() or vsprintf()
- + because the secure snprintf() or vsnprintf() functions were not available.
- + This can be determined using zlibCompileFlags().
- */
- ZEXTERN int ZEXPORT gzputs(gzFile file, const char *s);
- @@ -1406,7 +1476,7 @@ ZEXTERN int ZEXPORT gzflush(gzFile file, int flush);
- If the flush parameter is Z_FINISH, the remaining data is written and the
- gzip stream is completed in the output. If gzwrite() is called again, a new
- gzip stream will be started in the output. gzread() is able to read such
- - concatented gzip streams.
- + concatenated gzip streams.
- gzflush should be called only when strictly necessary because it will
- degrade compression if called too often.
- @@ -1556,15 +1626,15 @@ ZEXTERN void ZEXPORT gzclearerr(gzFile file);
- ZEXTERN uint32_t ZEXPORT adler32(uint32_t adler, const unsigned char *buf, uint32_t len);
- /*
- Update a running Adler-32 checksum with the bytes buf[0..len-1] and
- - return the updated checksum. If buf is Z_NULL, this function returns the
- + return the updated checksum. If buf is NULL, this function returns the
- required initial value for the checksum.
- - An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
- + An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed
- much faster.
- Usage example:
- - uint32_t adler = adler32(0L, Z_NULL, 0);
- + uint32_t adler = adler32(0L, NULL, 0);
- while (read_buffer(buffer, length) != EOF) {
- adler = adler32(adler, buffer, length);
- @@ -1586,13 +1656,13 @@ ZEXTERN uint32_t ZEXPORT adler32_combine(uint32_t adler1, uint32_t adler2, z_off
- ZEXTERN uint32_t ZEXPORT crc32(uint32_t crc, const unsigned char *buf, z_off64_t len);
- /*
- Update a running CRC-32 with the bytes buf[0..len-1] and return the
- - updated CRC-32. If buf is Z_NULL, this function returns the required
- + updated CRC-32. If buf is NULL, this function returns the required
- initial value for the crc. Pre- and post-conditioning (one's complement) is
- performed within this function so it shouldn't be done by the application.
- Usage example:
- - uint32_t crc = crc32(0L, Z_NULL, 0);
- + uint32_t crc = crc32(0L, NULL, 0);
- while (read_buffer(buffer, length) != EOF) {
- crc = crc32(crc, buffer, length);
- @@ -1647,7 +1717,7 @@ struct gzFile_s {
- z_off64_t pos;
- };
- ZEXTERN int ZEXPORT gzgetc_(gzFile file); /* backward compatibility */
- -# define gzgetc(g) ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : gzgetc(g))
- +# define gzgetc(g) ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g))
- /* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or
- * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if
- @@ -1711,6 +1781,7 @@ ZEXTERN const char * ZEXPORT zError (int);
- ZEXTERN int ZEXPORT inflateSyncPoint (z_stream *);
- ZEXTERN const uint32_t * ZEXPORT get_crc_table (void);
- ZEXTERN int ZEXPORT inflateUndermine (z_stream *, int);
- +ZEXTERN int ZEXPORT inflateValidate (z_stream *, int);
- ZEXTERN int ZEXPORT inflateResetKeep (z_stream *);
- ZEXTERN int ZEXPORT deflateResetKeep (z_stream *);
- diff --git a/zutil.c b/zutil.c
- index e46277b..1faf600 100644
- --- a/zutil.c
- +++ b/zutil.c
- @@ -59,7 +59,7 @@ unsigned long ZEXPORT zlibCompileFlags(void)
- case 8: flags += 2 << 6; break;
- default: flags += 3 << 6;
- }
- -#ifdef DEBUG
- +#ifdef ZLIB_DEBUG
- flags += 1 << 8;
- #endif
- #ifdef ZLIB_WINAPI
- @@ -83,7 +83,7 @@ unsigned long ZEXPORT zlibCompileFlags(void)
- return flags;
- }
- -#ifdef DEBUG
- +#ifdef ZLIB_DEBUG
- # ifndef verbose
- # define verbose 0
- diff --git a/zutil.h b/zutil.h
- index b6e858c..088c362 100644
- --- a/zutil.h
- +++ b/zutil.h
- @@ -26,11 +26,6 @@
- #include <stdint.h>
- #include "zlib.h"
- -#ifndef local
- -# define local static
- -#endif
- -/* compile with -Dlocal if your debugger can't find static symbols */
- -
- typedef unsigned char uch; /* Included for compatibility with external code only */
- typedef uint16_t ush; /* Included for compatibility with external code only */
- typedef unsigned long ulg;
- @@ -70,14 +65,101 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
- /* target dependencies */
- -#ifdef WIN32
- -# ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */
- -# define OS_CODE 0x0b
- +#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
- +# define OS_CODE 0x00
- +# ifndef Z_SOLO
- +# if defined(__TURBOC__) || defined(__BORLANDC__)
- +# if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
- + /* Allow compilation with ANSI keywords only enabled */
- + void _Cdecl farfree( void *block );
- + void *_Cdecl farmalloc( unsigned long nbytes );
- +# else
- +# include <alloc.h>
- +# endif
- +# else /* MSC or DJGPP */
- +# include <malloc.h>
- +# endif
- +# endif
- +#endif
- +
- +#ifdef AMIGA
- +# define OS_CODE 1
- +#endif
- +
- +#if defined(VAXC) || defined(VMS)
- +# define OS_CODE 2
- +# define F_OPEN(name, mode) \
- + fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
- +#endif
- +
- +#ifdef __370__
- +# if __TARGET_LIB__ < 0x20000000
- +# define OS_CODE 4
- +# elif __TARGET_LIB__ < 0x40000000
- +# define OS_CODE 11
- +# else
- +# define OS_CODE 8
- +# endif
- +#endif
- +
- +#if defined(ATARI) || defined(atarist)
- +# define OS_CODE 5
- +#endif
- +
- +#ifdef OS2
- +# define OS_CODE 6
- +# if defined(M_I86) && !defined(Z_SOLO)
- +# include <malloc.h>
- +# endif
- +#endif
- +
- +#if defined(MACOS) || defined(TARGET_OS_MAC)
- +# define OS_CODE 7
- +# ifndef Z_SOLO
- +# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
- +# include <unix.h> /* for fdopen */
- +# else
- +# ifndef fdopen
- +# define fdopen(fd,mode) NULL /* No fdopen() */
- +# endif
- +# endif
- # endif
- #endif
- -#if (defined(_MSC_VER) && (_MSC_VER > 600))
- -# define fdopen(fd, type) _fdopen(fd, type)
- +#ifdef __acorn
- +# define OS_CODE 13
- +#endif
- +
- +#if defined(WIN32) && !defined(__CYGWIN__)
- +# define OS_CODE 10
- +#endif
- +
- +#ifdef _BEOS_
- +# define OS_CODE 16
- +#endif
- +
- +#ifdef __TOS_OS400__
- +# define OS_CODE 18
- +#endif
- +
- +#ifdef __APPLE__
- +# define OS_CODE 19
- +#endif
- +
- +#if defined(_BEOS_) || defined(RISCOS)
- +# define fdopen(fd,mode) NULL /* No fdopen() */
- +#endif
- +
- +#if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX
- +# if defined(_WIN32_WCE)
- +# define fdopen(fd,mode) NULL /* No fdopen() */
- +# ifndef _PTRDIFF_T_DEFINED
- + typedef int ptrdiff_t;
- +# define _PTRDIFF_T_DEFINED
- +# endif
- +# else
- +# define fdopen(fd,type) _fdopen(fd,type)
- +# endif
- #endif
- /* provide prototypes for these when building zlib without LFS */
- @@ -95,7 +177,7 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
- /* common defaults */
- #ifndef OS_CODE
- -# define OS_CODE 0x03 /* assume Unix */
- +# define OS_CODE 3 /* assume Unix */
- #endif
- #ifndef F_OPEN
- @@ -105,7 +187,7 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
- /* functions */
- /* Diagnostic functions */
- -#ifdef DEBUG
- +#ifdef ZLIB_DEBUG
- # include <stdio.h>
- extern int ZLIB_INTERNAL z_verbose;
- extern void ZLIB_INTERNAL z_error(char *m);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement