Advertisement
Monni

zlib-ng diff mtl1979/develop mtl1979/archclean

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