summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2011-09-09 23:14:39 -0700
committerMark Adler <madler@alumni.caltech.edu>2011-09-09 23:14:39 -0700
commit423eb40306489f9c88f7dba32c2f69179166730b (patch)
treeb5a83b0b1e52bbe0de973dcbc7ec008c1d7cf7d9
parent8a2acbffc86012de3523ecf91db2c4ea1b1c4ea2 (diff)
downloadzlib-423eb40306489f9c88f7dba32c2f69179166730b.tar.gz
zlib-423eb40306489f9c88f7dba32c2f69179166730b.tar.bz2
zlib-423eb40306489f9c88f7dba32c2f69179166730b.zip
zlib 1.0.1v1.0.1
-rw-r--r--ChangeLog68
-rw-r--r--INDEX46
-rw-r--r--Makefile120
-rw-r--r--Makefile.b32104
-rw-r--r--Makefile.bor2
-rw-r--r--Makefile.dj279
-rw-r--r--Makefile.in129
-rw-r--r--Makefile.qnx75
-rw-r--r--Makefile.sas64
-rw-r--r--Makefile.wat103
-rw-r--r--README91
-rw-r--r--adler32.c20
-rw-r--r--compress.c8
-rwxr-xr-xconfigure86
-rw-r--r--crc32.c62
-rw-r--r--deflate.c230
-rw-r--r--deflate.h27
-rw-r--r--example.c170
-rw-r--r--gzio.c369
-rw-r--r--infblock.c33
-rw-r--r--infblock.h7
-rw-r--r--infcodes.c9
-rw-r--r--infcodes.h2
-rw-r--r--inffast.c9
-rw-r--r--inffast.h2
-rw-r--r--inflate.c96
-rw-r--r--inftrees.c31
-rw-r--r--inftrees.h2
-rw-r--r--infutil.c8
-rw-r--r--infutil.h30
-rw-r--r--minigzip.c19
-rw-r--r--trees.c36
-rw-r--r--uncompr.c8
-rw-r--r--zconf.h67
-rw-r--r--zlib.h213
-rw-r--r--zutil.c44
-rw-r--r--zutil.h26
37 files changed, 1814 insertions, 681 deletions
diff --git a/ChangeLog b/ChangeLog
index 6a7ff7a..36e05dc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,20 +1,54 @@
1 1
2 ChangeLog file for zlib 2 ChangeLog file for zlib
3 3
4Changes in 1.0 (15 Jan 96) 4Changes in 1.0.1 (20 May 96) [1.0 skipped to avoid confusion]
5- fix array overlay in deflate.c which sometimes caused bad compressed data
6- fix inflate bug with empty stored block
7- fix MSDOS medium model which was broken in 0.99
8- fix deflateParams() which could generated bad compressed data.
9- Bytef is define'd instead of typedef'ed (work around Borland bug)
10- added an INDEX file
11- new makefiles for DJGPP (Makefile.dj2), 32-bit Borland (Makefile.b32),
12 Watcom (Makefile.wat), Amiga SAS/C (Makefile.sas)
13- speed up adler32 for modern machines without auto-increment
14- added -ansi for IRIX in configure
15- static_init_done in trees.c is an int
16- define unlink as delete for VMS
17- fix configure for QNX
18- add configure branch for SCO and HPUX
19- avoid many warnings (unused variables, dead assignments, etc...)
20- no fdopen for BeOS
21- fix the Watcom fix for 32 bit mode (define FAR as empty)
22- removed redefinition of Byte for MKWERKS
23- work around an MWKERKS bug (incorrect merge of all .h files)
24
25Changes in 0.99 (27 Jan 96)
26- allow preset dictionary shared between compressor and decompressor
27- allow compression level 0 (no compression)
28- add deflateParams in zlib.h: allow dynamic change of compression level
29 and compression strategy.
30- test large buffers and deflateParams in example.c
31- add optional "configure" to build zlib as a shared library
32- suppress Makefile.qnx, use configure instead
5- fixed deflate for 64-bit systems (detected on Cray) 33- fixed deflate for 64-bit systems (detected on Cray)
6- fixed inflate_blocks for 64-bit systems (detected on Alpha) 34- fixed inflate_blocks for 64-bit systems (detected on Alpha)
35- declare Z_DEFLATED in zlib.h (possible parameter for deflateInit2)
36- always return Z_BUF_ERROR when deflate() has nothing to do
37- deflateInit and inflateInit are now macros to allow version checking
38- prefix all global functions and types with z_ with -DZ_PREFIX
7- make falloc completely reentrant (inftrees.c) 39- make falloc completely reentrant (inftrees.c)
8- fixed very unlikely race condition in ct_static_init 40- fixed very unlikely race condition in ct_static_init
9- always return Z_BUF_ERROR when deflate() has nothing to do
10- add deflateParams in zlib.h.
11- test large buffers and deflateParams in example.c
12- declare Z_DEFLATED in zlib.h (possible parameter for deflateInit2)
13- Document Z_SYNC_FLUSH
14- added deflateGetAdler32 and deflateSetAdler32
15- free in reverse order of allocation to help memory manager 41- free in reverse order of allocation to help memory manager
42- use zlib-1.0/* instead of zlib/* inside the tar.gz
43- make zlib warning-free with "gcc -O3 -Wall -Wwrite-strings -Wpointer-arith
44 -Wconversion -Wstrict-prototypes -Wmissing-prototypes"
45- allow gzread on concatenated .gz files
46- deflateEnd now returns Z_DATA_ERROR if it was premature
47- deflate is finally (?) fully deterministic (no matches beyond end of input)
48- Document Z_SYNC_FLUSH
49- add uninstall in Makefile
16- Check for __cpluplus in zlib.h 50- Check for __cpluplus in zlib.h
17- Finer test in ct_align for partial flush 51- Better test in ct_align for partial flush
18- avoid harmless warnings for Borland C++ 52- avoid harmless warnings for Borland C++
19- initialize hash_head in deflate.c 53- initialize hash_head in deflate.c
20- avoid warning on fdopen (gzio.c) for HP cc -Aa 54- avoid warning on fdopen (gzio.c) for HP cc -Aa
@@ -23,7 +57,7 @@ Changes in 1.0 (15 Jan 96)
23- ignore error if ranlib doesn't exist 57- ignore error if ranlib doesn't exist
24- call ranlib twice for NeXTSTEP 58- call ranlib twice for NeXTSTEP
25- use exec_prefix instead of prefix for libz.a 59- use exec_prefix instead of prefix for libz.a
26- renamed ct_* as tr_* to avoid conflict with another application. 60- renamed ct_* as _tr_* to avoid conflict with applications
27- clear z->msg in inflateInit2 before any error return 61- clear z->msg in inflateInit2 before any error return
28- initialize opaque in example.c, gzio.c, deflate.c and inflate.c 62- initialize opaque in example.c, gzio.c, deflate.c and inflate.c
29- fixed typo in zconf.h (_GNUC__ => __GNUC__) 63- fixed typo in zconf.h (_GNUC__ => __GNUC__)
@@ -32,6 +66,22 @@ Changes in 1.0 (15 Jan 96)
32- in fcalloc, normalize pointer if size > 65520 bytes 66- in fcalloc, normalize pointer if size > 65520 bytes
33- don't use special fcalloc for 32 bit Borland C++ 67- don't use special fcalloc for 32 bit Borland C++
34- use STDC instead of __GO32__ to avoid redeclaring exit, calloc, etc... 68- use STDC instead of __GO32__ to avoid redeclaring exit, calloc, etc...
69- use Z_BINARY instead of BINARY
70- document that gzclose after gzdopen will close the file
71- allow "a" as mode in gzopen.
72- fix error checking in gzread
73- allow skipping .gz extra-field on pipes
74- added reference to Perl interface in README
75- put the crc table in FAR data (I dislike more and more the medium model :)
76- added get_crc_table
77- added a dimension to all arrays (Borland C can't count).
78- workaround Borland C bug in declaration of inflate_codes_new & inflate_fast
79- guard against multiple inclusion of *.h (for precompiled header on Mac)
80- Watcom C pretends to be Microsoft C small model even in 32 bit mode.
81- don't use unsized arrays to avoid silly warnings by Visual C++:
82 warning C4746: 'inflate_mask' : unsized array treated as '__far'
83 (what's wrong with far data in far model?).
84- define enum out of inflate_blocks_state to allow compilation with C++
35 85
36Changes in 0.95 (16 Aug 95) 86Changes in 0.95 (16 Aug 95)
37- fix MSDOS small and medium model (now easier to adapt to any compiler) 87- fix MSDOS small and medium model (now easier to adapt to any compiler)
diff --git a/INDEX b/INDEX
new file mode 100644
index 0000000..c4a3ede
--- /dev/null
+++ b/INDEX
@@ -0,0 +1,46 @@
1ChangeLog history of changes
2INDEX this file
3Make_vms.com script for Vax/VMS
4Makefile makefile for Unix (generated by configure)
5Makefile.b32 makefile for Borland C++ 32-bit
6Makefile.bor makefile for Borland C/C++ 16-bit
7Makefile.dj2 makefile for DJGPP 2.x
8Makefile.in makefile for Unix (template for configure)
9Makefile.msc makefile for Microsoft C 16-bit
10Makefile.sas makefile for Amiga SAS/C
11Makefile.tc makefile for Turbo C
12Makefile.wat makefile for Watcom C
13README guess what
14configure configure script for Unix
15descrip.mms makefile for Vax/VMS
16
17 zlib public header files (must be kept):
18zconf.h
19zlib.h
20
21 private source files used to build the zlib library:
22adler32.c
23compress.c
24crc32.c
25deflate.c
26deflate.h
27gzio.c
28infblock.c
29infblock.h
30infcodes.c
31infcodes.h
32inffast.c
33inffast.h
34inflate.c
35inftrees.c
36inftrees.h
37infutil.c
38infutil.h
39trees.c
40uncompr.c
41zutil.c
42zutil.h
43
44 source files for sample programs:
45example.c
46minigzip.c
diff --git a/Makefile b/Makefile
index 45c96e4..3b41059 100644
--- a/Makefile
+++ b/Makefile
@@ -2,78 +2,128 @@
2# Copyright (C) 1995-1996 Jean-loup Gailly. 2# Copyright (C) 1995-1996 Jean-loup Gailly.
3# For conditions of distribution and use, see copyright notice in zlib.h 3# For conditions of distribution and use, see copyright notice in zlib.h
4 4
5# To compile and test, type:
6# ./configure; make test
7# The call of configure is optional if you don't have special requirements
8
9# To install /usr/local/lib/libz.* and /usr/local/include/zlib.h, type:
10# make install
11# To install in $HOME instead of /usr/local, use:
12# make install prefix=$HOME
13
5CC=cc 14CC=cc
15
6CFLAGS=-O 16CFLAGS=-O
7#use -O3 for gcc 17#CFLAGS=-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7
8#CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
9#CFLAGS=-g -DDEBUG 18#CFLAGS=-g -DDEBUG
19#CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \
20# -Wstrict-prototypes -Wmissing-prototypes
21
10LDFLAGS=-L. -lz 22LDFLAGS=-L. -lz
23LDSHARED=$(CC)
11 24
25VER=1.0.1
26LIBS=libz.a
27
28AR=ar rc
12RANLIB=ranlib 29RANLIB=ranlib
30TAR=tar
13 31
14prefix=/usr/local 32prefix=/usr/local
15exec_prefix = ${prefix} 33exec_prefix = $(prefix)
16 34
17OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \ 35OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \
18 zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o inffast.o 36 zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o inffast.o
19 37
20TEST_OBJS = example.o minigzip.o 38TEST_OBJS = example.o minigzip.o
21 39
40DISTFILES = README INDEX ChangeLog configure Make*[a-z0-9] descrip.mms *.[ch]
41
22all: example minigzip 42all: example minigzip
23 43
24test: all 44test: all
25 ./example 45 ./example
26 echo hello world | ./minigzip | ./minigzip -d 46 echo hello world | ./minigzip | ./minigzip -d
27 47
28install: libz.a
29 -@mkdir $(prefix)/include
30 -@mkdir $(exec_prefix)/lib
31 cp zlib.h zconf.h $(prefix)/include
32 chmod 644 $(prefix)/include/zlib.h $(prefix)/include/zconf.h
33 cp libz.a $(exec_prefix)/lib
34 chmod 644 $(exec_prefix)/lib/libz.a
35 -@$(RANLIB) $(prefix)/lib/libz.a
36# This second ranlib is needed on NeXTSTEP which checks file times
37
38libz.a: $(OBJS) 48libz.a: $(OBJS)
39 ar rc $@ $(OBJS) 49 $(AR) $@ $(OBJS)
40 -@$(RANLIB) $@ 50 -@ ($(RANLIB) $@ || true) 2>/dev/null
51
52libz.so.$(VER): $(OBJS)
53 $(LDSHARED) -o $@ $(OBJS)
54 rm -f libz.so; ln -s $@ libz.so
41 55
42example: example.o libz.a 56example: example.o $(LIBS)
43 $(CC) $(CFLAGS) -o $@ example.o $(LDFLAGS) 57 $(CC) $(CFLAGS) -o $@ example.o $(LDFLAGS)
44 58
45minigzip: minigzip.o libz.a 59minigzip: minigzip.o $(LIBS)
46 $(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS) 60 $(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS)
47 61
62install: $(LIBS)
63 -@if [ ! $(prefix)/include ]; then mkdir $(prefix)/include; fi
64 -@if [ ! $(exec_prefix)/lib ]; then mkdir $(exec_prefix)/lib; fi
65 cp zlib.h zconf.h $(prefix)/include
66 chmod 644 $(prefix)/include/zlib.h $(prefix)/include/zconf.h
67 cp $(LIBS) $(exec_prefix)/lib
68 cd $(exec_prefix)/lib; chmod 644 $(LIBS)
69 -@(cd $(exec_prefix)/lib; $(RANLIB) libz.a || true) >/dev/null 2>&1
70 cd $(exec_prefix)/lib; if test -f libz.so.$(VER); then \
71 ln -s libz.so.$(VER) libz.so; \
72 fi
73# The ranlib in install is needed on NeXTSTEP which checks file times
74
75uninstall:
76 cd $(exec_prefix)/lib; rm -f $(LIBS); \
77 if test -f libz.so; then \
78 v=`sed -n '/VERSION "/s/.*"\(.*\)".*/\1/p'<$(prefix)/include/zlib.h`;\
79 rm -f libz.so.$$v libz.so; \
80 fi
81 cd $(prefix)/include; rm -f zlib.h zconf.h
82
48clean: 83clean:
49 rm -f *.o example minigzip libz.a foo.gz 84 rm -f *.o *~ example minigzip libz.a libz.so* foo.gz
50 85
51zip: 86zip:
52 zip -ul9 zlib README ChangeLog Makefile Make????.??? Makefile.?? \ 87 mv Makefile Makefile~; cp -p Makefile.in Makefile
53 descrip.mms *.[ch] 88 v=`sed -n -e 's/\./-/g' -e '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`;\
54 89 zip -ul9 zlib-$$v $(DISTFILES)
55tgz: 90 mv Makefile~ Makefile
56 cd ..; tar cfz zlib/zlib.tgz zlib/README zlib/ChangeLog zlib/Makefile \ 91
57 zlib/Make????.??? zlib/Makefile.?? zlib/descrip.mms zlib/*.[ch] 92dist:
58 93 mv Makefile Makefile~; cp -p Makefile.in Makefile
59TAGS: 94 d=zlib-`sed -n '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`;\
95 rm -f $$d.tar.gz; \
96 if test ! -d ../$$d; then rm -f ../$$d; ln -s `pwd` ../$$d; fi; \
97 files=""; \
98 for f in $(DISTFILES); do files="$$files $$d/$$f"; done; \
99 cd ..; \
100 GZIP=-9 $(TAR) chofz $$d/$$d.tar.gz $$files; \
101 if test ! -d $$d; then rm -f $$d; fi
102 mv Makefile~ Makefile
103
104tags:
60 etags *.[ch] 105 etags *.[ch]
61 106
107depend:
108 makedepend -- $(CFLAGS) -- *.[ch]
109
62# DO NOT DELETE THIS LINE -- make depend depends on it. 110# DO NOT DELETE THIS LINE -- make depend depends on it.
63 111
64adler32.o: zutil.h zlib.h zconf.h 112adler32.o: zlib.h zconf.h
65compress.o: zlib.h zconf.h 113compress.o: zlib.h zconf.h
66crc32.o: zutil.h zlib.h zconf.h 114crc32.o: zlib.h zconf.h
67deflate.o: deflate.h zutil.h zlib.h zconf.h 115deflate.o: deflate.h zutil.h zlib.h zconf.h
68example.o: zlib.h zconf.h 116example.o: zlib.h zconf.h
69gzio.o: zutil.h zlib.h zconf.h 117gzio.o: zutil.h zlib.h zconf.h
70infblock.o: zutil.h zlib.h zconf.h infblock.h inftrees.h infcodes.h infutil.h 118infblock.o: infblock.h inftrees.h infcodes.h infutil.h zutil.h zlib.h zconf.h
71infcodes.o: zutil.h zlib.h zconf.h inftrees.h infutil.h infcodes.h inffast.h 119infcodes.o: zutil.h zlib.h zconf.h
72inffast.o: zutil.h zlib.h zconf.h inftrees.h infutil.h inffast.h 120infcodes.o: inftrees.h infblock.h infcodes.h infutil.h inffast.h
121inffast.o: zutil.h zlib.h zconf.h inftrees.h
122inffast.o: infblock.h infcodes.h infutil.h inffast.h
73inflate.o: zutil.h zlib.h zconf.h infblock.h 123inflate.o: zutil.h zlib.h zconf.h infblock.h
74inftrees.o: zutil.h zlib.h zconf.h inftrees.h 124inftrees.o: zutil.h zlib.h zconf.h inftrees.h
75infutil.o: zutil.h zlib.h zconf.h inftrees.h infutil.h 125infutil.o: zutil.h zlib.h zconf.h infblock.h inftrees.h infcodes.h infutil.h
76minigzip.o: zlib.h zconf.h 126minigzip.o: zlib.h zconf.h
77trees.o: deflate.h zutil.h zlib.h zconf.h 127trees.o: deflate.h zutil.h zlib.h zconf.h
78uncompr.o: zlib.h zconf.h 128uncompr.o: zlib.h zconf.h
79zutil.o: zutil.h zlib.h zconf.h 129zutil.o: zutil.h zlib.h zconf.h
diff --git a/Makefile.b32 b/Makefile.b32
new file mode 100644
index 0000000..fc3ac68
--- /dev/null
+++ b/Makefile.b32
@@ -0,0 +1,104 @@
1# Makefile for zlib
2# Borland C++
3
4# This version of the zlib makefile was adapted by Chris Young for use
5# with Borland C 4.5x with the Dos Power Pack for a 32-bit protected mode
6# flat memory model. It was created for use with POV-Ray ray tracer and
7# you may choose to edit the CFLAGS to suit your needs but the
8# switches -WX and -DMSDOS are required.
9# -- Chris Young 76702.1655@compuserve.com
10
11# To use, do "make -fmakefile.b32"
12
13# See zconf.h for details about the memory requirements.
14
15# ------------- Borland C++ -------------
16MODEL=-WX
17CFLAGS= $(MODEL) -P-C -K -N- -k- -d -3 -r- -v- -f -DMSDOS
18CC=bcc32
19LD=bcc32
20LIB=tlib
21LDFLAGS= $(MODEL)
22O=.obj
23
24# variables
25OBJ1 = adler32$(O) compress$(O) crc32$(O) gzio$(O) uncompr$(O) deflate$(O) \
26 trees$(O)
27OBJP1 = adler32$(O)+compress$(O)+crc32$(O)+gzio$(O)+uncompr$(O)+deflate$(O)+\
28 trees$(O)
29OBJ2 = zutil$(O) inflate$(O) infblock$(O) inftrees$(O) infcodes$(O) \
30 infutil$(O) inffast$(O)
31OBJP2 = zutil$(O)+inflate$(O)+infblock$(O)+inftrees$(O)+infcodes$(O)+\
32 infutil$(O)+inffast$(O)
33
34all: test
35
36adler32.obj: adler32.c zutil.h zlib.h zconf.h
37 $(CC) -c $(CFLAGS) $*.c
38
39compress.obj: compress.c zlib.h zconf.h
40 $(CC) -c $(CFLAGS) $*.c
41
42crc32.obj: crc32.c zutil.h zlib.h zconf.h
43 $(CC) -c $(CFLAGS) $*.c
44
45deflate.obj: deflate.c deflate.h zutil.h zlib.h zconf.h
46 $(CC) -c $(CFLAGS) $*.c
47
48gzio.obj: gzio.c zutil.h zlib.h zconf.h
49 $(CC) -c $(CFLAGS) $*.c
50
51infblock.obj: infblock.c zutil.h zlib.h zconf.h infblock.h inftrees.h\
52 infcodes.h infutil.h
53 $(CC) -c $(CFLAGS) $*.c
54
55infcodes.obj: infcodes.c zutil.h zlib.h zconf.h inftrees.h infutil.h\
56 infcodes.h inffast.h
57 $(CC) -c $(CFLAGS) $*.c
58
59inflate.obj: inflate.c zutil.h zlib.h zconf.h infblock.h
60 $(CC) -c $(CFLAGS) $*.c
61
62inftrees.obj: inftrees.c zutil.h zlib.h zconf.h inftrees.h
63 $(CC) -c $(CFLAGS) $*.c
64
65infutil.obj: infutil.c zutil.h zlib.h zconf.h inftrees.h infutil.h
66 $(CC) -c $(CFLAGS) $*.c
67
68inffast.obj: inffast.c zutil.h zlib.h zconf.h inftrees.h infutil.h inffast.h
69 $(CC) -c $(CFLAGS) $*.c
70
71trees.obj: trees.c deflate.h zutil.h zlib.h zconf.h
72 $(CC) -c $(CFLAGS) $*.c
73
74uncompr.obj: uncompr.c zlib.h zconf.h
75 $(CC) -c $(CFLAGS) $*.c
76
77zutil.obj: zutil.c zutil.h zlib.h zconf.h
78 $(CC) -c $(CFLAGS) $*.c
79
80example.obj: example.c zlib.h zconf.h
81 $(CC) -c $(CFLAGS) $*.c
82
83minigzip.obj: minigzip.c zlib.h zconf.h
84 $(CC) -c $(CFLAGS) $*.c
85
86# we must cut the command line to fit in the MS/DOS 128 byte limit:
87zlib.lib: $(OBJ1) $(OBJ2)
88 del zlib.lib
89 $(LIB) zlib +$(OBJP1)
90 $(LIB) zlib +$(OBJP2)
91
92example.exe: example.obj zlib.lib
93 $(LD) $(LDFLAGS) example.obj zlib.lib
94
95minigzip.exe: minigzip.obj zlib.lib
96 $(LD) $(LDFLAGS) minigzip.obj zlib.lib
97
98test: example.exe minigzip.exe
99 example
100 echo hello world | minigzip | minigzip -d
101
102#clean:
103# del *.obj
104# del *.exe
diff --git a/Makefile.bor b/Makefile.bor
index 536b76d..2116563 100644
--- a/Makefile.bor
+++ b/Makefile.bor
@@ -18,7 +18,7 @@ CFLAGS=-O2 -Z $(MODEL)
18CC=bcc 18CC=bcc
19LD=bcc 19LD=bcc
20LIB=tlib 20LIB=tlib
21# replace bcc with tcc for Turbo C++ 1.0 21# replace bcc with tcc for Turbo C++ 1.0, with bcc32 for the 32 bit version
22LDFLAGS=$(MODEL) 22LDFLAGS=$(MODEL)
23O=.obj 23O=.obj
24 24
diff --git a/Makefile.dj2 b/Makefile.dj2
new file mode 100644
index 0000000..82f55ea
--- /dev/null
+++ b/Makefile.dj2
@@ -0,0 +1,79 @@
1# Makefile for zlib. Modified for djgpp v2.0 by F. J. Donahoe, 3/15/96.
2# Copyright (C) 1995-1996 Jean-loup Gailly.
3# For conditions of distribution and use, see copyright notice in zlib.h
4
5# To compile and test, type:
6# make -fmakefile.dj2; make test -fmakefile.dj2
7
8# To install libz.a, zconf.h and zlib.h in the djgpp directories, type:
9# make install -fmakefile.dj2
10# after first defining LIBRARY_PATH and INCLUDE_PATH in djgpp.env for [make].
11# Alternately these variables may be defined below.
12
13CC=gcc
14
15#CFLAGS=-MMD -O
16#CFLAGS=-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7
17#CFLAGS=-MMD -g -DDEBUG
18CFLAGS=-MMD -O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \
19 -Wstrict-prototypes -Wmissing-prototypes
20INSTALL=install
21LDLIBS=-L. -lz
22LD=$(CC) -s -o
23LDSHARED=$(CC)
24
25VER=1.0
26LIBS=libz.a
27
28AR=ar rcs
29# The default value of RM is "rm -f." If "rm.exe" is not found, uncomment:
30# RM=del
31
32prefix=/usr/local
33exec_prefix = $(prefix)
34
35OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \
36 zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o inffast.o
37
38TEST_OBJS = example.o minigzip.o
39
40DISTFILES = README Change.Log configur Makefile.in Makefile Makefile.msc \
41 Makefile.bor Makefile.tc Make_vms.com descrip.mms zlib099.pat \
42 *.bak *.old *.[ch]
43
44all: example.exe minigzip.exe
45
46test: all
47 ./example
48 echo hello world | .\minigzip | .\minigzip -d
49
50%.o : %.c
51 $(CC) $(CFLAGS) -c $< -o $@
52
53libz.a: $(OBJS)
54 $(AR) $@ $(OBJS)
55
56libz.so.$(VER): $(OBJS)
57 $(LDSHARED) -o $@ $(OBJS)
58 rm -f libz.so; ln -s $@ libz.so
59
60%.exe : %.o $(LIBS)
61 $(LD) $@ $< $(LDLIBS)
62
63# INCLUDE_PATH and LIBRARY_PATH were set for [make] in djgpp.env
64
65install: $(LIBS)
66 $(INSTALL) zlib.h zconf.h $(INCLUDE_PATH)
67 $(INSTALL) $(LIBS) $(LIBRARY_PATH)
68
69uninstall:
70 $(RM) $(LIBRARY_PATH)/$(LIBS)
71 $(RM) $(INCLUDE_PATH)/zlib.h $(INCLUDE_PATH)/zconf.h
72
73clean:
74 $(RM) *.d *.o *.~ *.exe libz.a libz.so* foo.gz
75
76DEPS := $(wildcard *.d)
77ifneq ($(DEPS),)
78include $(DEPS)
79endif
diff --git a/Makefile.in b/Makefile.in
new file mode 100644
index 0000000..3b41059
--- /dev/null
+++ b/Makefile.in
@@ -0,0 +1,129 @@
1# Makefile for zlib
2# Copyright (C) 1995-1996 Jean-loup Gailly.
3# For conditions of distribution and use, see copyright notice in zlib.h
4
5# To compile and test, type:
6# ./configure; make test
7# The call of configure is optional if you don't have special requirements
8
9# To install /usr/local/lib/libz.* and /usr/local/include/zlib.h, type:
10# make install
11# To install in $HOME instead of /usr/local, use:
12# make install prefix=$HOME
13
14CC=cc
15
16CFLAGS=-O
17#CFLAGS=-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7
18#CFLAGS=-g -DDEBUG
19#CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \
20# -Wstrict-prototypes -Wmissing-prototypes
21
22LDFLAGS=-L. -lz
23LDSHARED=$(CC)
24
25VER=1.0.1
26LIBS=libz.a
27
28AR=ar rc
29RANLIB=ranlib
30TAR=tar
31
32prefix=/usr/local
33exec_prefix = $(prefix)
34
35OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \
36 zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o inffast.o
37
38TEST_OBJS = example.o minigzip.o
39
40DISTFILES = README INDEX ChangeLog configure Make*[a-z0-9] descrip.mms *.[ch]
41
42all: example minigzip
43
44test: all
45 ./example
46 echo hello world | ./minigzip | ./minigzip -d
47
48libz.a: $(OBJS)
49 $(AR) $@ $(OBJS)
50 -@ ($(RANLIB) $@ || true) 2>/dev/null
51
52libz.so.$(VER): $(OBJS)
53 $(LDSHARED) -o $@ $(OBJS)
54 rm -f libz.so; ln -s $@ libz.so
55
56example: example.o $(LIBS)
57 $(CC) $(CFLAGS) -o $@ example.o $(LDFLAGS)
58
59minigzip: minigzip.o $(LIBS)
60 $(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS)
61
62install: $(LIBS)
63 -@if [ ! $(prefix)/include ]; then mkdir $(prefix)/include; fi
64 -@if [ ! $(exec_prefix)/lib ]; then mkdir $(exec_prefix)/lib; fi
65 cp zlib.h zconf.h $(prefix)/include
66 chmod 644 $(prefix)/include/zlib.h $(prefix)/include/zconf.h
67 cp $(LIBS) $(exec_prefix)/lib
68 cd $(exec_prefix)/lib; chmod 644 $(LIBS)
69 -@(cd $(exec_prefix)/lib; $(RANLIB) libz.a || true) >/dev/null 2>&1
70 cd $(exec_prefix)/lib; if test -f libz.so.$(VER); then \
71 ln -s libz.so.$(VER) libz.so; \
72 fi
73# The ranlib in install is needed on NeXTSTEP which checks file times
74
75uninstall:
76 cd $(exec_prefix)/lib; rm -f $(LIBS); \
77 if test -f libz.so; then \
78 v=`sed -n '/VERSION "/s/.*"\(.*\)".*/\1/p'<$(prefix)/include/zlib.h`;\
79 rm -f libz.so.$$v libz.so; \
80 fi
81 cd $(prefix)/include; rm -f zlib.h zconf.h
82
83clean:
84 rm -f *.o *~ example minigzip libz.a libz.so* foo.gz
85
86zip:
87 mv Makefile Makefile~; cp -p Makefile.in Makefile
88 v=`sed -n -e 's/\./-/g' -e '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`;\
89 zip -ul9 zlib-$$v $(DISTFILES)
90 mv Makefile~ Makefile
91
92dist:
93 mv Makefile Makefile~; cp -p Makefile.in Makefile
94 d=zlib-`sed -n '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`;\
95 rm -f $$d.tar.gz; \
96 if test ! -d ../$$d; then rm -f ../$$d; ln -s `pwd` ../$$d; fi; \
97 files=""; \
98 for f in $(DISTFILES); do files="$$files $$d/$$f"; done; \
99 cd ..; \
100 GZIP=-9 $(TAR) chofz $$d/$$d.tar.gz $$files; \
101 if test ! -d $$d; then rm -f $$d; fi
102 mv Makefile~ Makefile
103
104tags:
105 etags *.[ch]
106
107depend:
108 makedepend -- $(CFLAGS) -- *.[ch]
109
110# DO NOT DELETE THIS LINE -- make depend depends on it.
111
112adler32.o: zlib.h zconf.h
113compress.o: zlib.h zconf.h
114crc32.o: zlib.h zconf.h
115deflate.o: deflate.h zutil.h zlib.h zconf.h
116example.o: zlib.h zconf.h
117gzio.o: zutil.h zlib.h zconf.h
118infblock.o: infblock.h inftrees.h infcodes.h infutil.h zutil.h zlib.h zconf.h
119infcodes.o: zutil.h zlib.h zconf.h
120infcodes.o: inftrees.h infblock.h infcodes.h infutil.h inffast.h
121inffast.o: zutil.h zlib.h zconf.h inftrees.h
122inffast.o: infblock.h infcodes.h infutil.h inffast.h
123inflate.o: zutil.h zlib.h zconf.h infblock.h
124inftrees.o: zutil.h zlib.h zconf.h inftrees.h
125infutil.o: zutil.h zlib.h zconf.h infblock.h inftrees.h infcodes.h infutil.h
126minigzip.o: zlib.h zconf.h
127trees.o: deflate.h zutil.h zlib.h zconf.h
128uncompr.o: zlib.h zconf.h
129zutil.o: zutil.h zlib.h zconf.h
diff --git a/Makefile.qnx b/Makefile.qnx
deleted file mode 100644
index 22b1a23..0000000
--- a/Makefile.qnx
+++ /dev/null
@@ -1,75 +0,0 @@
1# Makefile for zlib
2# Copyright (C) 1995 Jean-loup Gailly.
3# For conditions of distribution and use, see copyright notice in zlib.h
4
5# Modified slightly for QNX by Chris Herborth (chrish@qnx.com)
6
7CC=cc
8CFLAGS=-4 -O -Q
9#use -O3 for gcc to take advantage of inlining
10#CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
11#CFLAGS=-g -DDEBUG
12LDFLAGS=-L. -lz
13
14#RANLIB=ranlib
15
16prefix=/usr/local
17
18OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \
19 zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o inffast.o
20
21TEST_OBJS = example.o minigzip.o
22
23all: example minigzip
24
25test: all
26 ./example
27 echo hello world | ./minigzip | ./minigzip -d
28
29install: zlib.lib
30 -@mkdir $(prefix)/include
31 -@mkdir $(prefix)/lib
32 cp zlib.h zconf.h $(prefix)/include
33 chmod 644 $(prefix)/include/zlib.h $(prefix)/include/zconf.h
34 cp zlib.lib $(prefix)/lib
35 chmod 644 $(prefix)/lib/zlib.lib
36
37zlib.lib: $(OBJS)
38 cc -A $@ $(OBJS)
39# ar rc $@ $(OBJS)
40# $(RANLIB) $@
41
42example: example.o zlib.lib
43 $(CC) $(CFLAGS) -o $@ example.o $(LDFLAGS)
44
45minigzip: minigzip.o zlib.lib
46 $(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS)
47
48clean:
49 rm -f *.o example minigzip zlib.lib foo.gz
50
51zip:
52 zip -ul9 zlib README ChangeLog Makefile Makefile.??? Makefile.?? *.[ch]
53
54tgz:
55 cd ..; tar cfz zlib/zlib.tgz zlib/README zlib/ChangeLog zlib/Makefile \
56 zlib/Makefile.??? zlib/Makefile.?? zlib/*.[ch]
57
58# DO NOT DELETE THIS LINE -- make depend depends on it.
59
60adler32.o: zutil.h zlib.h zconf.h
61compress.o: zlib.h zconf.h
62crc32.o: zutil.h zlib.h zconf.h
63deflate.o: deflate.h zutil.h zlib.h zconf.h
64example.o: zlib.h zconf.h
65gzio.o: zutil.h zlib.h zconf.h
66infblock.o: zutil.h zlib.h zconf.h infblock.h inftrees.h infcodes.h infutil.h
67infcodes.o: zutil.h zlib.h zconf.h inftrees.h infutil.h infcodes.h inffast.h
68inffast.o: zutil.h zlib.h zconf.h inftrees.h infutil.h inffast.h
69inflate.o: zutil.h zlib.h zconf.h infblock.h
70inftrees.o: zutil.h zlib.h zconf.h inftrees.h
71infutil.o: zutil.h zlib.h zconf.h inftrees.h infutil.h
72minigzip.o: zlib.h zconf.h
73trees.o: deflate.h zutil.h zlib.h zconf.h
74uncompr.o: zlib.h zconf.h
75zutil.o: zutil.h zlib.h zconf.h
diff --git a/Makefile.sas b/Makefile.sas
new file mode 100644
index 0000000..5323e82
--- /dev/null
+++ b/Makefile.sas
@@ -0,0 +1,64 @@
1# SMakefile for zlib
2# Modified from the standard UNIX Makefile Copyright Jean-loup Gailly
3# Osma Ahvenlampi <Osma.Ahvenlampi@hut.fi>
4# Amiga, SAS/C 6.56 & Smake
5
6CC=sc
7CFLAGS=OPT
8#CFLAGS=OPT CPU=68030
9#CFLAGS=DEBUG=LINE
10LDFLAGS=LIB z.lib
11
12SCOPTIONS=OPTSCHED OPTINLINE OPTALIAS OPTTIME OPTINLOCAL STRMERGE \
13 NOICONS PARMS=BOTH NOSTACKCHECK UTILLIB NOVERSION ERRORREXX
14
15OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \
16 zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o inffast.o
17
18TEST_OBJS = example.o minigzip.o
19
20all: SCOPTIONS example minigzip
21
22test: all
23 `cd`/example
24 echo hello world | minigzip | minigzip -d
25
26install: z.lib
27 copy zlib.h zconf.h INCLUDE: clone
28 copy z.lib LIB: clone
29
30z.lib: $(OBJS)
31 oml z.lib r $(OBJS)
32
33example: example.o z.lib
34 $(CC) $(CFLAGS) LINK TO $@ example.o $(LDFLAGS)
35
36minigzip: minigzip.o z.lib
37 $(CC) $(CFLAGS) LINK TO $@ minigzip.o $(LDFLAGS)
38
39clean:
40 -delete force quiet *.o example minigzip z.lib foo.gz *.lnk SCOPTIONS
41
42SCOPTIONS: Smakefile
43 copy to $@ <from <
44$(SCOPTIONS)
45<
46
47# DO NOT DELETE THIS LINE -- make depend depends on it.
48
49adler32.o: zutil.h zlib.h zconf.h
50compress.o: zlib.h zconf.h
51crc32.o: zutil.h zlib.h zconf.h
52deflate.o: deflate.h zutil.h zlib.h zconf.h
53example.o: zlib.h zconf.h
54gzio.o: zutil.h zlib.h zconf.h
55infblock.o: zutil.h zlib.h zconf.h infblock.h inftrees.h infcodes.h infutil.h
56infcodes.o: zutil.h zlib.h zconf.h inftrees.h infutil.h infcodes.h inffast.h
57inffast.o: zutil.h zlib.h zconf.h inftrees.h infutil.h inffast.h
58inflate.o: zutil.h zlib.h zconf.h infblock.h
59inftrees.o: zutil.h zlib.h zconf.h inftrees.h
60infutil.o: zutil.h zlib.h zconf.h inftrees.h infutil.h
61minigzip.o: zlib.h zconf.h
62trees.o: deflate.h zutil.h zlib.h zconf.h
63uncompr.o: zlib.h zconf.h
64zutil.o: zutil.h zlib.h zconf.h
diff --git a/Makefile.wat b/Makefile.wat
new file mode 100644
index 0000000..2a3b629
--- /dev/null
+++ b/Makefile.wat
@@ -0,0 +1,103 @@
1# Makefile for zlib
2# Watcom 10a
3
4# This version of the zlib makefile was adapted by Chris Young for use
5# with Watcom 10a 32-bit protected mode flat memory model. It was created
6# for use with POV-Ray ray tracer and you may choose to edit the CFLAGS to
7# suit your needs but the -DMSDOS is required.
8# -- Chris Young 76702.1655@compuserve.com
9
10# To use, do "wmake -f makefile.wat"
11
12# See zconf.h for details about the memory requirements.
13
14# ------------- Watcom 10a -------------
15MODEL=-mf
16CFLAGS= $(MODEL) -fpi87 -fp5 -zp4 -5r -w5 -oneatx -DMSDOS
17CC=wcc386
18LD=wcl386
19LIB=wlib -b -c
20LDFLAGS=
21O=.obj
22
23# variables
24OBJ1=adler32$(O) compress$(O) crc32$(O) gzio$(O) uncompr$(O) deflate$(O)
25OBJ2=trees$(O) zutil$(O) inflate$(O) infblock$(O) inftrees$(O) infcodes$(O)
26OBJ3=infutil$(O) inffast$(O)
27OBJP1=adler32$(O)+compress$(O)+crc32$(O)+gzio$(O)+uncompr$(O)+deflate$(O)
28OBJP2=trees$(O)+zutil$(O)+inflate$(O)+infblock$(O)+inftrees$(O)+infcodes$(O)
29OBJP3=infutil$(O)+inffast$(O)
30
31all: test
32
33adler32.obj: adler32.c zutil.h zlib.h zconf.h
34 $(CC) $(CFLAGS) $*.c
35
36compress.obj: compress.c zlib.h zconf.h
37 $(CC) $(CFLAGS) $*.c
38
39crc32.obj: crc32.c zutil.h zlib.h zconf.h
40 $(CC) $(CFLAGS) $*.c
41
42deflate.obj: deflate.c deflate.h zutil.h zlib.h zconf.h
43 $(CC) $(CFLAGS) $*.c
44
45gzio.obj: gzio.c zutil.h zlib.h zconf.h
46 $(CC) $(CFLAGS) $*.c
47
48infblock.obj: infblock.c zutil.h zlib.h zconf.h infblock.h inftrees.h &
49 infcodes.h infutil.h
50 $(CC) $(CFLAGS) $*.c
51
52infcodes.obj: infcodes.c zutil.h zlib.h zconf.h inftrees.h infutil.h &
53 infcodes.h inffast.h
54 $(CC) $(CFLAGS) $*.c
55
56inflate.obj: inflate.c zutil.h zlib.h zconf.h infblock.h
57 $(CC) $(CFLAGS) $*.c
58
59inftrees.obj: inftrees.c zutil.h zlib.h zconf.h inftrees.h
60 $(CC) $(CFLAGS) $*.c
61
62infutil.obj: infutil.c zutil.h zlib.h zconf.h inftrees.h infutil.h
63 $(CC) $(CFLAGS) $*.c
64
65inffast.obj: inffast.c zutil.h zlib.h zconf.h inftrees.h infutil.h inffast.h
66 $(CC) $(CFLAGS) $*.c
67
68trees.obj: trees.c deflate.h zutil.h zlib.h zconf.h
69 $(CC) $(CFLAGS) $*.c
70
71uncompr.obj: uncompr.c zlib.h zconf.h
72 $(CC) $(CFLAGS) $*.c
73
74zutil.obj: zutil.c zutil.h zlib.h zconf.h
75 $(CC) $(CFLAGS) $*.c
76
77example.obj: example.c zlib.h zconf.h
78 $(CC) $(CFLAGS) $*.c
79
80minigzip.obj: minigzip.c zlib.h zconf.h
81 $(CC) $(CFLAGS) $*.c
82
83# we must cut the command line to fit in the MS/DOS 128 byte limit:
84zlib.lib: $(OBJ1) $(OBJ2) $(OBJ3)
85 del zlib.lib
86 $(LIB) zlib.lib +$(OBJP1)
87 $(LIB) zlib.lib +$(OBJP2)
88 $(LIB) zlib.lib +$(OBJP3)
89
90example.exe: example.obj zlib.lib
91 $(LD) $(LDFLAGS) example.obj zlib.lib
92
93minigzip.exe: minigzip.obj zlib.lib
94 $(LD) $(LDFLAGS) minigzip.obj zlib.lib
95
96test: minigzip.exe example.exe
97 example
98 echo hello world | minigzip | minigzip -d >test
99 type test
100
101#clean:
102# del *.obj
103# del *.exe
diff --git a/README b/README
index b4ad14e..dd72d94 100644
--- a/README
+++ b/README
@@ -1,55 +1,62 @@
1zlib 1.0 is a general purpose data compression library. 1zlib 1.0.1 is a general purpose data compression library. All the code
2All the code is reentrant (thread safe). 2is reentrant (thread safe). The data format used by the zlib library
3 3is described in the files zlib-3.3.doc, deflate-1.3.doc and
4The data format used by the zlib library is described in the 4gzip-4.3.doc, available in various formats from
5files zlib-3.2.doc, deflate-1.2.doc and gzip-4.2.doc, available 5ftp://swrinde.nde.swri.edu/pub/png/documents/zlib/
6in ftp://ftp.uu.net/pub/archiving/zip/doc
7 6
8All functions of the compression library are documented in the file 7All functions of the compression library are documented in the file
9zlib.h. A usage example of the library is given in the file example.c 8zlib.h. A usage example of the library is given in the file example.c
10which also tests that the library is working correctly. Another 9which also tests that the library is working correctly. Another
11example is given in the file minigzip.c. 10example is given in the file minigzip.c. The compression library itself
12 11is composed of all source files except example.c and minigzip.c.
13To compile all files and run the test program, just type: make test 12
14(For MSDOS, use one of the special makefiles such as Makefile.msc; 13To compile all files and run the test program, follow the instructions
15for VMS, use Make_vms.com or descrip.mms.) 14given at the top of Makefile. In short "make test; make install"
16To install the zlib library (libz.a) in /usr/local/lib, type: make install 15should work for most machines. For MSDOS, use one of the special
17To install in a different directory, use for example: 16makefiles such as Makefile.msc; for VMS, use Make_vms.com or descrip.mms.
18 make install prefix=$HOME 17
19This will install in $HOME/lib instead of /usr/local/lib. 18Questions about zlib should be sent to <zlib@quest.jpl.nasa.gov> or,
20 19if this fails, to the addresses given below in the Copyright section.
21Questions about zlib should be sent to <zlib@quest.jpl.nasa.gov>. 20
22 21The changes made in version 1.0.1 are documented in the file ChangeLog.
23The changes made in version 1.0 are documented in the file ChangeLog. 22Version number 1.0 was skipped to avoid confusion with version 0.99
24The main changes since 0.95 are: 23which had some references to 1.0 already. The main changes since 0.99 are:
25- allow compression level 0 (no compression) 24
26- add deflateParams in zlib.h: allow dynamic change of compression level 25- fix array overlay in deflate.c which sometimes caused bad compressed data
27 and compression strategy. 26- fix inflate bug with empty stored block
28- test large buffers and deflateParams in example.c 27- fix MSDOS medium model which was broken in 0.99
29- declare Z_DEFLATED in zlib.h (possible parameter for deflateInit2) 28- fix deflateParams() which could generated bad compressed data.
30- fixes for 64-bit systems (needed for Alpha and Cray) 29- added an INDEX file
31- always return Z_BUF_ERROR when deflate() has nothing to do 30- new makefiles for DJGPP (Makefile.dj2), 32-bit Borland (Makefile.b32),
32- fix some very unlikely race conditions. 31 Watcom (Makefile.wat), Amiga SAS/C (Makefile.sas)
33- several minor fixes for better portability 32- several portability improvements
34- free in reverse order of allocation to help memory manager
35 33
36Notes for some targets: 34Notes for some targets:
37 35
38- For MSDOS, the small and medium models have been tested only with 36- For Turbo C the small model is supported only with reduced performance to
39 Microsoft C. (This should work for Borland C also, but I don't have 37 avoid any far allocation; it was tested with -DMAX_WBITS=11 -DMAX_MEM_LEVEL=3
40 a Borland compiler to test with.) The small model was tested with
41 Turbo C but only with reduced performance to avoid any far
42 allocation; it was tested with -DMAX_WBITS=11 -DMAX_MEM_LEVEL=3
43
44 MS Visual C++1.5 in far model gives:
45 warning C4746: 'z_errmsg' : unsized array treated as '__far'
46 warning C4746: 'inflate_mask' : unsized array treated as '__far'
47 Ignore those warnings.
48 38
49- For 64-bit Iris, deflate.c must be compiled without any optimization. 39- For 64-bit Iris, deflate.c must be compiled without any optimization.
50 With -O, one libpng test fails. The test works in 32 bit mode (with the 40 With -O, one libpng test fails. The test works in 32 bit mode (with
51 -32 compiler flag). 41 the -32 compiler flag).
42
43- zlib doesn't work with gcc 2.6.3 on a DEC 3000/300LX under OSF/1 2.1
44 it works when compiled with cc.
45
46- zlib doesn't work on HP-UX 9.05 with one cc compiler (the one not
47 accepting the -O option). It works with the other cc compiler.
48
49A Perl interface to zlib written by Paul Marquess <pmarquess@bfsec.bt.co.uk>
50is in the CPAN (Comprehensive Perl Archive Network) sites, such as:
51ftp://ftp.cis.ufl.edu/pub/perl/CPAN/modules/by-module/Compress/Compress-Zlib*
52
53
54Acknowledgments:
52 55
56 The deflate format used by zlib was defined by Phil Katz. The deflate
57 and zlib specifications were written by Peter Deutsch. Thanks to all the
58 people who reported problems and suggested various improvements in zlib;
59 they are too numerous to cite here.
53 60
54Copyright notice: 61Copyright notice:
55 62
diff --git a/adler32.c b/adler32.c
index 7e2e3f5..b6b5519 100644
--- a/adler32.c
+++ b/adler32.c
@@ -1,9 +1,9 @@
1/* adler32.c -- compute the Adler-32 checksum of a data stream 1/* adler32.c -- compute the Adler-32 checksum of a data stream
2 * Copyright (C) 1995 Mark Adler 2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: adler32.c,v 1.6 1995/05/03 17:27:08 jloup Exp $ */ 6/* $Id: adler32.c,v 1.9 1996/01/30 21:59:09 me Exp $ */
7 7
8#include "zlib.h" 8#include "zlib.h"
9 9
@@ -11,16 +11,16 @@
11#define NMAX 5552 11#define NMAX 5552
12/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ 12/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
13 13
14#define DO1(buf) {s1 += *buf++; s2 += s1;} 14#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
15#define DO2(buf) DO1(buf); DO1(buf); 15#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
16#define DO4(buf) DO2(buf); DO2(buf); 16#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
17#define DO8(buf) DO4(buf); DO4(buf); 17#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
18#define DO16(buf) DO8(buf); DO8(buf); 18#define DO16(buf) DO8(buf,0); DO8(buf,8);
19 19
20/* ========================================================================= */ 20/* ========================================================================= */
21uLong adler32(adler, buf, len) 21uLong adler32(adler, buf, len)
22 uLong adler; 22 uLong adler;
23 Bytef *buf; 23 const Bytef *buf;
24 uInt len; 24 uInt len;
25{ 25{
26 unsigned long s1 = adler & 0xffff; 26 unsigned long s1 = adler & 0xffff;
@@ -34,10 +34,12 @@ uLong adler32(adler, buf, len)
34 len -= k; 34 len -= k;
35 while (k >= 16) { 35 while (k >= 16) {
36 DO16(buf); 36 DO16(buf);
37 buf += 16;
37 k -= 16; 38 k -= 16;
38 } 39 }
39 if (k != 0) do { 40 if (k != 0) do {
40 DO1(buf); 41 s1 += *buf++;
42 s2 += s1;
41 } while (--k); 43 } while (--k);
42 s1 %= BASE; 44 s1 %= BASE;
43 s2 %= BASE; 45 s2 %= BASE;
diff --git a/compress.c b/compress.c
index 5addd8d..1c58400 100644
--- a/compress.c
+++ b/compress.c
@@ -1,9 +1,9 @@
1/* compress.c -- compress a memory buffer 1/* compress.c -- compress a memory buffer
2 * Copyright (C) 1995 Jean-loup Gailly. 2 * Copyright (C) 1995-1996 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: compress.c,v 1.6 1995/05/03 17:27:08 jloup Exp $ */ 6/* $Id: compress.c,v 1.9 1996/01/30 21:59:09 me Exp $ */
7 7
8#include "zlib.h" 8#include "zlib.h"
9 9
@@ -22,13 +22,13 @@
22int compress (dest, destLen, source, sourceLen) 22int compress (dest, destLen, source, sourceLen)
23 Bytef *dest; 23 Bytef *dest;
24 uLongf *destLen; 24 uLongf *destLen;
25 Bytef *source; 25 const Bytef *source;
26 uLong sourceLen; 26 uLong sourceLen;
27{ 27{
28 z_stream stream; 28 z_stream stream;
29 int err; 29 int err;
30 30
31 stream.next_in = source; 31 stream.next_in = (Bytef*)source;
32 stream.avail_in = (uInt)sourceLen; 32 stream.avail_in = (uInt)sourceLen;
33 /* Check for source > 64K on 16-bit machine: */ 33 /* Check for source > 64K on 16-bit machine: */
34 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 34 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
diff --git a/configure b/configure
new file mode 100755
index 0000000..a354e4c
--- /dev/null
+++ b/configure
@@ -0,0 +1,86 @@
1#!/bin/sh
2# configure script for zlib. This script is needed only if
3# you wish to build a shared library and your system supports them,
4# of if you need special compiler, flags or install directory.
5# Otherwise, you can just use directly "make test; make install"
6#
7# To impose specific compiler or flags or install directory, use for example:
8# prefix=$HOME CC=cc CFLAGS="-O4" ./configure
9# or for csh/tcsh users:
10# (setenv prefix $HOME; setenv CC cc; setenv CFLAGS "-O4"; ./configure)
11# LDSHARED is the command to be used to create a shared library
12
13LIBS=libz.a
14VER=`sed -n -e '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`
15AR=${AR-"ar rc"}
16RANLIB=${RANLIB-"ranlib"}
17prefix=${prefix-/usr/local}
18exec_prefix=${exec_prefix-$prefix}
19
20test -z "$CC" && echo Checking for gcc...
21test=ztest$$
22cat > $test.c <<EOF
23int hello() { printf("hello\n"); }
24EOF
25if test -z "$CC" && (gcc -c -O3 $test.c) 2>/dev/null; then
26 CC=gcc
27 SFLAGS=${CFLAGS-"-fPIC -O3"}
28 CFLAGS=${CFLAGS-"-O3"}
29 LDSHARED=${LDSHARED-"gcc -shared"}
30else
31 # find system name and corresponding cc options
32 CC=${CC-cc}
33 case `(uname -sr || echo unknown) 2>/dev/null` in
34 SunOS\ 5*) SFLAGS=${CFLAGS-"-fast -xcg89 -KPIC -R."}
35 CFLAGS=${CFLAGS-"-fast -xcg89"}
36 LDSHARED=${LDSHARED-"cc -G"};;
37 SunOS\ 4*) SFLAGS=${CFLAGS-"-O2 -PIC"}
38 CFLAGS=${CFLAGS-"-O2"}
39 LDSHARED=${LDSHARED-"ld"};;
40 IRIX*) SFLAGS=${CFLAGS-"-ansi -O2 -rpath ."}
41 CFLAGS=${CFLAGS-"-ansi -O2"}
42 LDSHARED=${LDSHARED-"cc -shared"};;
43 QNX*) SFLAGS=${CFLAGS-"-4 -O"}
44 CFLAGS=${CFLAGS-"-4 -O"}
45 LDSHARED=${LDSHARED-"cc"}
46 RANLIB=${RANLIB-"true"}
47 AR="cc -A";;
48 SCO_SV\ 3.2*) SFLAGS=${CFLAGS-"-O3 -dy -KPIC "}
49 CFLAGS=${CFLAGS-"-O3"}
50 LDSHARED=${LDSHARED-"cc -dy -KPIC -G"};;
51 HP-UX*) SFLAGS=${CFLAGS-"-O +z"}
52 CFLAGS=${CFLAGS-"-O"}
53 LDSHARED=${LDSHARED-"ld -b"}
54 SHAREDLIBS='libz.sl';;
55 # send working options for other systems to gzip@prep.ai.mit.edu
56 *) SFLAGS=${CFLAGS-"-O"}
57 CFLAGS=${CFLAGS-"-O"}
58 LDSHARED=${LDSHARED-"cc -shared"};;
59 esac
60fi
61
62echo Checking for shared library support...
63# we must test in two steps (cc then ld), required at least on SunOS 4.x
64if test "`($CC -c $SFLAGS $test.c) 2>&1`" = "" &&
65 test "`($LDSHARED -o $test.so $test.o) 2>&1`" = ""; then
66 CFLAGS="$SFLAGS"
67 LIBS='libz.so.$(VER)'
68 echo Building shared library libz.so.$VER with $CC.
69else
70 LDSHARED="$CC"
71 echo Building static library $LIBS version $VER with $CC.
72fi
73rm -f $test.[co] $test.so
74
75# udpate Makefile
76sed < Makefile.in "
77/^CC *=/s/=.*/=$CC/
78/^CFLAGS *=/s/=.*/=$CFLAGS/
79/^LDSHARED *=/s/=.*/=$LDSHARED/
80/^LIBS *=/s,=.*,=$LIBS,
81/^AR *=/s/=.*/=$AR/
82/^RANLIB *=/s,=.*,=$RANLIB,
83/^VER *=/s/=.*/=$VER/
84/^prefix *=/s,=.*,=$prefix,
85/^exec_prefix *=/s,=.*,=$exec_prefix,
86" > Makefile
diff --git a/crc32.c b/crc32.c
index 4089255..58fc60e 100644
--- a/crc32.c
+++ b/crc32.c
@@ -1,32 +1,62 @@
1/* crc32.c -- compute the CRC-32 of a data stream 1/* crc32.c -- compute the CRC-32 of a data stream
2 * Copyright (C) 1995 Mark Adler 2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: crc32.c,v 1.4 1995/04/14 14:55:12 jloup Exp $ */ 6/* $Id: crc32.c,v 1.8 1996/01/30 21:59:10 me Exp $ */
7 7
8#include "zlib.h" 8#include "zlib.h"
9 9
10#define local static 10#define local static
11 11
12#ifdef DYNAMIC_CRC_TABLE 12#ifdef DYNAMIC_CRC_TABLE
13/* ========================================================================= 13
14 * Make the crc table. This function is needed only if you want to compute
15 * the table dynamically.
16 */
17local int crc_table_empty = 1; 14local int crc_table_empty = 1;
18local uLong crc_table[256]; 15local uLongf crc_table[256];
16local void make_crc_table OF((void));
17
18/*
19 Generate a table for a byte-wise 32-bit CRC calculation on the polynomial:
20 x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
19 21
22 Polynomials over GF(2) are represented in binary, one bit per coefficient,
23 with the lowest powers in the most significant bit. Then adding polynomials
24 is just exclusive-or, and multiplying a polynomial by x is a right shift by
25 one. If we call the above polynomial p, and represent a byte as the
26 polynomial q, also with the lowest power in the most significant bit (so the
27 byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
28 where a mod b means the remainder after dividing a by b.
29
30 This calculation is done using the shift-register method of multiplying and
31 taking the remainder. The register is initialized to zero, and for each
32 incoming bit, x^32 is added mod p to the register if the bit is a one (where
33 x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
34 x (which is shifting right by one and adding x^32 mod p if the bit shifted
35 out is a one). We start with the highest power (least significant bit) of
36 q and repeat for all eight bits of q.
37
38 The table is simply the CRC of all possible eight bit values. This is all
39 the information needed to generate CRC's on data a byte at a time for all
40 combinations of CRC register values and incoming bytes.
41*/
20local void make_crc_table() 42local void make_crc_table()
21{ 43{
22 uLong c; 44 uLong c;
23 int n, k; 45 int n, k;
46 uLong poly; /* polynomial exclusive-or pattern */
47 /* terms of polynomial defining this crc (except x^32): */
48 static Byte p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
49
50 /* make exclusive-or pattern from polynomial (0xedb88320L) */
51 poly = 0L;
52 for (n = 0; n < sizeof(p)/sizeof(Byte); n++)
53 poly |= 1L << (31 - p[n]);
24 54
25 for (n = 0; n < 256; n++) 55 for (n = 0; n < 256; n++)
26 { 56 {
27 c = (uLong)n; 57 c = (uLong)n;
28 for (k = 0; k < 8; k++) 58 for (k = 0; k < 8; k++)
29 c = c & 1 ? 0xedb88320L ^ (c >> 1) : c >> 1; 59 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
30 crc_table[n] = c; 60 crc_table[n] = c;
31 } 61 }
32 crc_table_empty = 0; 62 crc_table_empty = 0;
@@ -35,7 +65,7 @@ local void make_crc_table()
35/* ======================================================================== 65/* ========================================================================
36 * Table of CRC-32's of all single-byte values (made by make_crc_table) 66 * Table of CRC-32's of all single-byte values (made by make_crc_table)
37 */ 67 */
38local uLong crc_table[] = { 68local uLongf crc_table[256] = {
39 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, 69 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
40 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L, 70 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
41 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, 71 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
@@ -91,6 +121,18 @@ local uLong crc_table[] = {
91}; 121};
92#endif 122#endif
93 123
124/* =========================================================================
125 * This function can be used by asm versions of crc32()
126 */
127uLongf *get_crc_table()
128{
129#ifdef DYNAMIC_CRC_TABLE
130 if (crc_table_empty) make_crc_table();
131#endif
132 return (uLongf *)crc_table;
133}
134
135/* ========================================================================= */
94#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8); 136#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
95#define DO2(buf) DO1(buf); DO1(buf); 137#define DO2(buf) DO1(buf); DO1(buf);
96#define DO4(buf) DO2(buf); DO2(buf); 138#define DO4(buf) DO2(buf); DO2(buf);
@@ -99,7 +141,7 @@ local uLong crc_table[] = {
99/* ========================================================================= */ 141/* ========================================================================= */
100uLong crc32(crc, buf, len) 142uLong crc32(crc, buf, len)
101 uLong crc; 143 uLong crc;
102 Bytef *buf; 144 const Bytef *buf;
103 uInt len; 145 uInt len;
104{ 146{
105 if (buf == Z_NULL) return 0L; 147 if (buf == Z_NULL) return 0L;
diff --git a/deflate.c b/deflate.c
index 46ddc55..64710e6 100644
--- a/deflate.c
+++ b/deflate.c
@@ -1,5 +1,5 @@
1/* deflate.c -- compress data using the deflation algorithm 1/* deflate.c -- compress data using the deflation algorithm
2 * Copyright (C) 1995 Jean-loup Gailly. 2 * Copyright (C) 1995-1996 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -47,11 +47,11 @@
47 * 47 *
48 */ 48 */
49 49
50/* $Id: deflate.c,v 1.8 1995/05/03 17:27:08 jloup Exp $ */ 50/* $Id: deflate.c,v 1.12 1996/01/30 21:59:11 me Exp $ */
51 51
52#include "deflate.h" 52#include "deflate.h"
53 53
54char deflate_copyright[] = " deflate 1.0 Copyright 1995 Jean-loup Gailly "; 54char deflate_copyright[] = " deflate 1.0.1 Copyright 1995-1996 Jean-loup Gailly ";
55/* 55/*
56 If you use the zlib library in a product, an acknowledgment is welcome 56 If you use the zlib library in a product, an acknowledgment is welcome
57 in the documentation of your product. If for some reason you cannot 57 in the documentation of your product. If for some reason you cannot
@@ -60,15 +60,14 @@ char deflate_copyright[] = " deflate 1.0 Copyright 1995 Jean-loup Gailly ";
60 */ 60 */
61 61
62/* =========================================================================== 62/* ===========================================================================
63 * Prototypes for local functions. 63 * Function prototypes.
64 */ 64 */
65
66local void fill_window OF((deflate_state *s)); 65local void fill_window OF((deflate_state *s));
67local int deflate_stored OF((deflate_state *s, int flush)); 66local int deflate_stored OF((deflate_state *s, int flush));
68local int deflate_fast OF((deflate_state *s, int flush)); 67local int deflate_fast OF((deflate_state *s, int flush));
69local int deflate_slow OF((deflate_state *s, int flush)); 68local int deflate_slow OF((deflate_state *s, int flush));
70local void lm_init OF((deflate_state *s)); 69local void lm_init OF((deflate_state *s));
71local int longest_match OF((deflate_state *s, IPos cur_match)); 70local uInt longest_match OF((deflate_state *s, IPos cur_match));
72local void putShortMSB OF((deflate_state *s, uInt b)); 71local void putShortMSB OF((deflate_state *s, uInt b));
73local void flush_pending OF((z_stream *strm)); 72local void flush_pending OF((z_stream *strm));
74local int read_buf OF((z_stream *strm, charf *buf, unsigned size)); 73local int read_buf OF((z_stream *strm, charf *buf, unsigned size));
@@ -158,7 +157,7 @@ struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
158#define INSERT_STRING(s, str, match_head) \ 157#define INSERT_STRING(s, str, match_head) \
159 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 158 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
160 s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \ 159 s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
161 s->head[s->ins_h] = (str)) 160 s->head[s->ins_h] = (Pos)(str))
162 161
163/* =========================================================================== 162/* ===========================================================================
164 * Initialize the hash table (avoiding 64K overflow for 16 bit systems). 163 * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
@@ -169,26 +168,41 @@ struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
169 zmemzero((charf *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); 168 zmemzero((charf *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
170 169
171/* ========================================================================= */ 170/* ========================================================================= */
172int deflateInit (strm, level) 171int deflateInit_(strm, level, version, stream_size)
173 z_stream *strm; 172 z_stream *strm;
174 int level; 173 int level;
174 const char *version;
175 int stream_size;
175{ 176{
176 return deflateInit2 (strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 0); 177 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
178 Z_DEFAULT_STRATEGY, version, stream_size);
177 /* To do: ignore strm->next_in if we use it as window */ 179 /* To do: ignore strm->next_in if we use it as window */
178} 180}
179 181
180/* ========================================================================= */ 182/* ========================================================================= */
181int deflateInit2 (strm, level, method, windowBits, memLevel, strategy) 183int deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
184 version, stream_size)
182 z_stream *strm; 185 z_stream *strm;
183 int level; 186 int level;
184 int method; 187 int method;
185 int windowBits; 188 int windowBits;
186 int memLevel; 189 int memLevel;
187 int strategy; 190 int strategy;
191 const char *version;
192 int stream_size;
188{ 193{
189 deflate_state *s; 194 deflate_state *s;
190 int noheader = 0; 195 int noheader = 0;
191 196
197 ushf *overlay;
198 /* We overlay pending_buf and d_buf+l_buf. This works since the average
199 * output size for (length,distance) codes is <= 24 bits.
200 */
201
202 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
203 stream_size != sizeof(z_stream)) {
204 return Z_VERSION_ERROR;
205 }
192 if (strm == Z_NULL) return Z_STREAM_ERROR; 206 if (strm == Z_NULL) return Z_STREAM_ERROR;
193 207
194 strm->msg = Z_NULL; 208 strm->msg = Z_NULL;
@@ -230,20 +244,17 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
230 244
231 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ 245 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
232 246
233 s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 2*sizeof(ush)); 247 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
248 s->pending_buf = (uchf *) overlay;
234 249
235 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 250 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
236 s->pending_buf == Z_NULL) { 251 s->pending_buf == Z_NULL) {
237 strm->msg = z_errmsg[1-Z_MEM_ERROR]; 252 strm->msg = ERR_MSG(Z_MEM_ERROR);
238 deflateEnd (strm); 253 deflateEnd (strm);
239 return Z_MEM_ERROR; 254 return Z_MEM_ERROR;
240 } 255 }
241 s->l_buf = (uchf *) &(s->pending_buf[s->lit_bufsize]); 256 s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
242 s->d_buf = (ushf *) &(s->pending_buf[2*s->lit_bufsize]); 257 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
243 /* We overlay pending_buf and d_buf+l_buf. This works since the average
244 * output size for (length,distance) codes is <= 32 bits (worst case
245 * is 15+15+13=33). d_buf is put last in case sizeof(short)>2.
246 */
247 258
248 s->level = level; 259 s->level = level;
249 s->strategy = strategy; 260 s->strategy = strategy;
@@ -252,32 +263,42 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
252 return deflateReset(strm); 263 return deflateReset(strm);
253} 264}
254 265
255/* ========================================================================= 266/* ========================================================================= */
256 * Undocumented function to return the Adler32 of a stream. It can be 267int deflateSetDictionary (strm, dictionary, dictLength)
257 * called immediately after a flush with Z_SYNC_FLUSH, to allow later
258 * resumption of the compressor with deflateSetAdler32.
259 */
260uLong deflateGetAdler32 (strm)
261 z_stream *strm; 268 z_stream *strm;
269 const Bytef *dictionary;
270 uInt dictLength;
262{ 271{
263 if (strm == Z_NULL || strm->state == Z_NULL) return (uLong)Z_STREAM_ERROR; 272 deflate_state *s;
264 /* Z_STREAM_ERROR happens to be an invalid Adler32 value. */ 273 uInt length = dictLength;
274 uInt n;
275 IPos hash_head = 0;
265 276
266 return strm->state->adler; 277 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
267} 278 strm->state->status != INIT_STATE) return Z_STREAM_ERROR;
268 279
269/* ========================================================================= 280 s = strm->state;
270 * Undocumented function to set the Adler32 of a stream. It can be called 281 strm->adler = adler32(strm->adler, dictionary, dictLength);
271 * immediately after deflateInit to reset the Adler32 of a compression
272 * stream which had been interrupted.
273 */
274int deflateSetAdler32 (strm, adler)
275 z_stream *strm;
276 uLong adler;
277{
278 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
279 282
280 strm->state->adler = adler; 283 if (length < MIN_MATCH) return Z_OK;
284 if (length > MAX_DIST(s)) {
285 length = MAX_DIST(s);
286 dictionary += dictLength - length;
287 }
288 zmemcpy((charf *)s->window, dictionary, length);
289 s->strstart = length;
290 s->block_start = (long)length;
291
292 /* Insert all strings in the hash table (except for the last two bytes).
293 * s->lookahead stays null, so s->ins_h will be recomputed at the next
294 * call of fill_window.
295 */
296 s->ins_h = s->window[0];
297 UPDATE_HASH(s, s->ins_h, s->window[1]);
298 for (n = 0; n <= length - MIN_MATCH; n++) {
299 INSERT_STRING(s, n, hash_head);
300 }
301 if (hash_head) hash_head = 0; /* to make compiler happy */
281 return Z_OK; 302 return Z_OK;
282} 303}
283 304
@@ -302,10 +323,10 @@ int deflateReset (strm)
302 s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */ 323 s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
303 } 324 }
304 s->status = s->noheader ? BUSY_STATE : INIT_STATE; 325 s->status = s->noheader ? BUSY_STATE : INIT_STATE;
305 s->adler = 1; 326 strm->adler = 1;
306 s->last_flush = Z_NO_FLUSH; 327 s->last_flush = Z_NO_FLUSH;
307 328
308 tr_init(s); 329 _tr_init(s);
309 lm_init(s); 330 lm_init(s);
310 331
311 return Z_OK; 332 return Z_OK;
@@ -319,6 +340,7 @@ int deflateParams(strm, level, strategy)
319{ 340{
320 deflate_state *s; 341 deflate_state *s;
321 compress_func func; 342 compress_func func;
343 int err = Z_OK;
322 344
323 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 345 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
324 s = strm->state; 346 s = strm->state;
@@ -331,11 +353,9 @@ int deflateParams(strm, level, strategy)
331 } 353 }
332 func = configuration_table[s->level].func; 354 func = configuration_table[s->level].func;
333 355
334 if (func != configuration_table[level].func 356 if (func != configuration_table[level].func && strm->total_in != 0) {
335 && strm->state->lookahead != 0) {
336
337 /* Flush the last buffer: */ 357 /* Flush the last buffer: */
338 (void)(*func)(strm->state, Z_PARTIAL_FLUSH); 358 err = deflate(strm, Z_PARTIAL_FLUSH);
339 } 359 }
340 if (s->level != level) { 360 if (s->level != level) {
341 s->level = level; 361 s->level = level;
@@ -345,7 +365,7 @@ int deflateParams(strm, level, strategy)
345 s->max_chain_length = configuration_table[level].max_chain; 365 s->max_chain_length = configuration_table[level].max_chain;
346 } 366 }
347 s->strategy = strategy; 367 s->strategy = strategy;
348 return Z_OK; 368 return err;
349} 369}
350 370
351/* ========================================================================= 371/* =========================================================================
@@ -392,36 +412,47 @@ int deflate (strm, flush)
392 int flush; 412 int flush;
393{ 413{
394 int old_flush; /* value of flush param for previous deflate call */ 414 int old_flush; /* value of flush param for previous deflate call */
415 deflate_state *s;
395 416
396 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 417 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
397 418
419 s = strm->state;
420
398 if (strm->next_out == Z_NULL || 421 if (strm->next_out == Z_NULL ||
399 (strm->next_in == Z_NULL && strm->avail_in != 0) || 422 (strm->next_in == Z_NULL && strm->avail_in != 0) ||
400 (strm->state->status == FINISH_STATE && flush != Z_FINISH)) { 423 (s->status == FINISH_STATE && flush != Z_FINISH)) {
401 ERR_RETURN(strm, Z_STREAM_ERROR); 424 ERR_RETURN(strm, Z_STREAM_ERROR);
402 } 425 }
403 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); 426 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
404 427
405 strm->state->strm = strm; /* just in case */ 428 s->strm = strm; /* just in case */
406 old_flush = strm->state->last_flush; 429 old_flush = s->last_flush;
407 strm->state->last_flush = flush; 430 s->last_flush = flush;
408 431
409 /* Write the zlib header */ 432 /* Write the zlib header */
410 if (strm->state->status == INIT_STATE) { 433 if (s->status == INIT_STATE) {
411 434
412 uInt header = (Z_DEFLATED + ((strm->state->w_bits-8)<<4)) << 8; 435 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
413 uInt level_flags = (strm->state->level-1) >> 1; 436 uInt level_flags = (s->level-1) >> 1;
414 437
415 if (level_flags > 3) level_flags = 3; 438 if (level_flags > 3) level_flags = 3;
416 header |= (level_flags << 6); 439 header |= (level_flags << 6);
440 if (s->strstart != 0) header |= PRESET_DICT;
417 header += 31 - (header % 31); 441 header += 31 - (header % 31);
418 442
419 strm->state->status = BUSY_STATE; 443 s->status = BUSY_STATE;
420 putShortMSB(strm->state, header); 444 putShortMSB(s, header);
445
446 /* Save the adler32 of the preset dictionary: */
447 if (s->strstart != 0) {
448 putShortMSB(s, (uInt)(strm->adler >> 16));
449 putShortMSB(s, (uInt)(strm->adler & 0xffff));
450 strm->adler = 1L;
451 }
421 } 452 }
422 453
423 /* Flush as much pending output as possible */ 454 /* Flush as much pending output as possible */
424 if (strm->state->pending != 0) { 455 if (s->pending != 0) {
425 flush_pending(strm); 456 flush_pending(strm);
426 if (strm->avail_out == 0) return Z_OK; 457 if (strm->avail_out == 0) return Z_OK;
427 458
@@ -435,21 +466,20 @@ int deflate (strm, flush)
435 } 466 }
436 467
437 /* User must not provide more input after the first FINISH: */ 468 /* User must not provide more input after the first FINISH: */
438 if (strm->state->status == FINISH_STATE && strm->avail_in != 0) { 469 if (s->status == FINISH_STATE && strm->avail_in != 0) {
439 ERR_RETURN(strm, Z_BUF_ERROR); 470 ERR_RETURN(strm, Z_BUF_ERROR);
440 } 471 }
441 472
442 /* Start a new block or continue the current one. 473 /* Start a new block or continue the current one.
443 */ 474 */
444 if (strm->avail_in != 0 || strm->state->lookahead != 0 || 475 if (strm->avail_in != 0 || s->lookahead != 0 ||
445 (flush != Z_NO_FLUSH && strm->state->status != FINISH_STATE)) { 476 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
446 int quit; 477 int quit;
447 478
448 if (flush == Z_FINISH) { 479 if (flush == Z_FINISH) {
449 strm->state->status = FINISH_STATE; 480 s->status = FINISH_STATE;
450 } 481 }
451 quit = (*(configuration_table[strm->state->level].func)) 482 quit = (*(configuration_table[s->level].func))(s, flush);
452 (strm->state, flush);
453 483
454 if (quit || strm->avail_out == 0) return Z_OK; 484 if (quit || strm->avail_out == 0) return Z_OK;
455 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call 485 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
@@ -461,14 +491,14 @@ int deflate (strm, flush)
461 */ 491 */
462 if (flush != Z_NO_FLUSH && flush != Z_FINISH) { 492 if (flush != Z_NO_FLUSH && flush != Z_FINISH) {
463 if (flush == Z_PARTIAL_FLUSH) { 493 if (flush == Z_PARTIAL_FLUSH) {
464 tr_align(strm->state); 494 _tr_align(s);
465 } else { /* FULL_FLUSH or SYNC_FLUSH */ 495 } else { /* FULL_FLUSH or SYNC_FLUSH */
466 tr_stored_block(strm->state, (char*)0, 0L, 0); 496 _tr_stored_block(s, (char*)0, 0L, 0);
467 /* For a full flush, this empty block will be recognized 497 /* For a full flush, this empty block will be recognized
468 * as a special marker by inflate_sync(). 498 * as a special marker by inflate_sync().
469 */ 499 */
470 if (flush == Z_FULL_FLUSH) { 500 if (flush == Z_FULL_FLUSH) {
471 CLEAR_HASH(strm->state); /* forget history */ 501 CLEAR_HASH(s); /* forget history */
472 } 502 }
473 } 503 }
474 flush_pending(strm); 504 flush_pending(strm);
@@ -478,23 +508,25 @@ int deflate (strm, flush)
478 Assert(strm->avail_out > 0, "bug2"); 508 Assert(strm->avail_out > 0, "bug2");
479 509
480 if (flush != Z_FINISH) return Z_OK; 510 if (flush != Z_FINISH) return Z_OK;
481 if (strm->state->noheader) return Z_STREAM_END; 511 if (s->noheader) return Z_STREAM_END;
482 512
483 /* Write the zlib trailer (adler32) */ 513 /* Write the zlib trailer (adler32) */
484 putShortMSB(strm->state, (uInt)(strm->state->adler >> 16)); 514 putShortMSB(s, (uInt)(strm->adler >> 16));
485 putShortMSB(strm->state, (uInt)(strm->state->adler & 0xffff)); 515 putShortMSB(s, (uInt)(strm->adler & 0xffff));
486 flush_pending(strm); 516 flush_pending(strm);
487 /* If avail_out is zero, the application will call deflate again 517 /* If avail_out is zero, the application will call deflate again
488 * to flush the rest. 518 * to flush the rest.
489 */ 519 */
490 strm->state->noheader = -1; /* write the trailer only once! */ 520 s->noheader = -1; /* write the trailer only once! */
491 return strm->state->pending != 0 ? Z_OK : Z_STREAM_END; 521 return s->pending != 0 ? Z_OK : Z_STREAM_END;
492} 522}
493 523
494/* ========================================================================= */ 524/* ========================================================================= */
495int deflateEnd (strm) 525int deflateEnd (strm)
496 z_stream *strm; 526 z_stream *strm;
497{ 527{
528 int status;
529
498 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 530 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
499 531
500 /* Deallocate in reverse order of allocations: */ 532 /* Deallocate in reverse order of allocations: */
@@ -503,10 +535,11 @@ int deflateEnd (strm)
503 TRY_FREE(strm, strm->state->prev); 535 TRY_FREE(strm, strm->state->prev);
504 TRY_FREE(strm, strm->state->window); 536 TRY_FREE(strm, strm->state->window);
505 537
538 status = strm->state->status;
506 ZFREE(strm, strm->state); 539 ZFREE(strm, strm->state);
507 strm->state = Z_NULL; 540 strm->state = Z_NULL;
508 541
509 return Z_OK; 542 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
510} 543}
511 544
512/* ========================================================================= */ 545/* ========================================================================= */
@@ -549,7 +582,7 @@ local int read_buf(strm, buf, size)
549 strm->avail_in -= len; 582 strm->avail_in -= len;
550 583
551 if (!strm->state->noheader) { 584 if (!strm->state->noheader) {
552 strm->state->adler = adler32(strm->state->adler, strm->next_in, len); 585 strm->adler = adler32(strm->adler, strm->next_in, len);
553 } 586 }
554 zmemcpy(buf, strm->next_in, len); 587 zmemcpy(buf, strm->next_in, len);
555 strm->next_in += len; 588 strm->next_in += len;
@@ -593,12 +626,13 @@ local void lm_init (s)
593 * garbage. 626 * garbage.
594 * IN assertions: cur_match is the head of the hash chain for the current 627 * IN assertions: cur_match is the head of the hash chain for the current
595 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 628 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
629 * OUT assertion: the match length is not greater than s->lookahead.
596 */ 630 */
597#ifndef ASMV 631#ifndef ASMV
598/* For 80x86 and 680x0, an optimized version will be provided in match.asm or 632/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
599 * match.S. The code will be functionally equivalent. 633 * match.S. The code will be functionally equivalent.
600 */ 634 */
601local int longest_match(s, cur_match) 635local uInt longest_match(s, cur_match)
602 deflate_state *s; 636 deflate_state *s;
603 IPos cur_match; /* current match */ 637 IPos cur_match; /* current match */
604{ 638{
@@ -607,6 +641,7 @@ local int longest_match(s, cur_match)
607 register Bytef *match; /* matched string */ 641 register Bytef *match; /* matched string */
608 register int len; /* length of current match */ 642 register int len; /* length of current match */
609 int best_len = s->prev_length; /* best match length so far */ 643 int best_len = s->prev_length; /* best match length so far */
644 int nice_match = s->nice_match; /* stop if match long enough */
610 IPos limit = s->strstart > (IPos)MAX_DIST(s) ? 645 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
611 s->strstart - (IPos)MAX_DIST(s) : NIL; 646 s->strstart - (IPos)MAX_DIST(s) : NIL;
612 /* Stop when cur_match becomes <= limit. To simplify the code, 647 /* Stop when cur_match becomes <= limit. To simplify the code,
@@ -637,6 +672,11 @@ local int longest_match(s, cur_match)
637 if (s->prev_length >= s->good_match) { 672 if (s->prev_length >= s->good_match) {
638 chain_length >>= 2; 673 chain_length >>= 2;
639 } 674 }
675 /* Do not look for matches beyond the end of the input. This is necessary
676 * to make deflate deterministic.
677 */
678 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
679
640 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 680 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
641 681
642 do { 682 do {
@@ -715,7 +755,7 @@ local int longest_match(s, cur_match)
715 if (len > best_len) { 755 if (len > best_len) {
716 s->match_start = cur_match; 756 s->match_start = cur_match;
717 best_len = len; 757 best_len = len;
718 if (len >= s->nice_match) break; 758 if (len >= nice_match) break;
719#ifdef UNALIGNED_OK 759#ifdef UNALIGNED_OK
720 scan_end = *(ushf*)(scan+best_len-1); 760 scan_end = *(ushf*)(scan+best_len-1);
721#else 761#else
@@ -726,7 +766,8 @@ local int longest_match(s, cur_match)
726 } while ((cur_match = prev[cur_match & wmask]) > limit 766 } while ((cur_match = prev[cur_match & wmask]) > limit
727 && --chain_length != 0); 767 && --chain_length != 0);
728 768
729 return best_len; 769 if ((uInt)best_len <= s->lookahead) return best_len;
770 return s->lookahead;
730} 771}
731#endif /* ASMV */ 772#endif /* ASMV */
732 773
@@ -740,13 +781,13 @@ local void check_match(s, start, match, length)
740 int length; 781 int length;
741{ 782{
742 /* check that the match is indeed a match */ 783 /* check that the match is indeed a match */
743 if (memcmp((charf *)s->window + match, 784 if (zmemcmp((charf *)s->window + match,
744 (charf *)s->window + start, length) != EQUAL) { 785 (charf *)s->window + start, length) != EQUAL) {
745 fprintf(stderr, 786 fprintf(stderr, " start %u, match %u, length %d\n",
746 " start %u, match %u, length %d\n", 787 start, match, length);
747 start, match, length); 788 do {
748 do { fprintf(stderr, "%c%c", s->window[match++], 789 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
749 s->window[start++]); } while (--length != 0); 790 } while (--length != 0);
750 z_error("invalid match"); 791 z_error("invalid match");
751 } 792 }
752 if (verbose > 1) { 793 if (verbose > 1) {
@@ -862,9 +903,11 @@ local void fill_window(s)
862 * IN assertion: strstart is set to the end of the current match. 903 * IN assertion: strstart is set to the end of the current match.
863 */ 904 */
864#define FLUSH_BLOCK_ONLY(s, eof) { \ 905#define FLUSH_BLOCK_ONLY(s, eof) { \
865 tr_flush_block(s, (s->block_start >= 0L ? \ 906 _tr_flush_block(s, (s->block_start >= 0L ? \
866 (charf *)&s->window[(unsigned)s->block_start] : \ 907 (charf *)&s->window[(unsigned)s->block_start] : \
867 (charf *)Z_NULL), (long)s->strstart - s->block_start, (eof)); \ 908 (charf *)Z_NULL), \
909 (ulg)((long)s->strstart - s->block_start), \
910 (eof)); \
868 s->block_start = s->strstart; \ 911 s->block_start = s->strstart; \
869 flush_pending(s->strm); \ 912 flush_pending(s->strm); \
870 Tracev((stderr,"[FLUSH]")); \ 913 Tracev((stderr,"[FLUSH]")); \
@@ -906,7 +949,7 @@ local int deflate_stored(s, flush)
906 s->lookahead = 0; 949 s->lookahead = 0;
907 950
908 /* Stored blocks are limited to 0xffff bytes: */ 951 /* Stored blocks are limited to 0xffff bytes: */
909 if (s->strstart == 0 || s->strstart > 0xffff) { 952 if (s->strstart == 0 || s->strstart > 0xfffe) {
910 /* strstart == 0 is possible when wraparound on 16-bit machine */ 953 /* strstart == 0 is possible when wraparound on 16-bit machine */
911 s->lookahead = s->strstart - 0xffff; 954 s->lookahead = s->strstart - 0xffff;
912 s->strstart = 0xffff; 955 s->strstart = 0xffff;
@@ -967,14 +1010,12 @@ local int deflate_fast(s, flush)
967 s->match_length = longest_match (s, hash_head); 1010 s->match_length = longest_match (s, hash_head);
968 } 1011 }
969 /* longest_match() sets match_start */ 1012 /* longest_match() sets match_start */
970
971 if (s->match_length > s->lookahead) s->match_length = s->lookahead;
972 } 1013 }
973 if (s->match_length >= MIN_MATCH) { 1014 if (s->match_length >= MIN_MATCH) {
974 check_match(s, s->strstart, s->match_start, s->match_length); 1015 check_match(s, s->strstart, s->match_start, s->match_length);
975 1016
976 bflush = tr_tally(s, s->strstart - s->match_start, 1017 bflush = _tr_tally(s, s->strstart - s->match_start,
977 s->match_length - MIN_MATCH); 1018 s->match_length - MIN_MATCH);
978 1019
979 s->lookahead -= s->match_length; 1020 s->lookahead -= s->match_length;
980 1021
@@ -1007,7 +1048,7 @@ local int deflate_fast(s, flush)
1007 } else { 1048 } else {
1008 /* No match, output a literal byte */ 1049 /* No match, output a literal byte */
1009 Tracevv((stderr,"%c", s->window[s->strstart])); 1050 Tracevv((stderr,"%c", s->window[s->strstart]));
1010 bflush = tr_tally (s, 0, s->window[s->strstart]); 1051 bflush = _tr_tally (s, 0, s->window[s->strstart]);
1011 s->lookahead--; 1052 s->lookahead--;
1012 s->strstart++; 1053 s->strstart++;
1013 } 1054 }
@@ -1065,7 +1106,6 @@ local int deflate_slow(s, flush)
1065 s->match_length = longest_match (s, hash_head); 1106 s->match_length = longest_match (s, hash_head);
1066 } 1107 }
1067 /* longest_match() sets match_start */ 1108 /* longest_match() sets match_start */
1068 if (s->match_length > s->lookahead) s->match_length = s->lookahead;
1069 1109
1070 if (s->match_length <= 5 && (s->strategy == Z_FILTERED || 1110 if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
1071 (s->match_length == MIN_MATCH && 1111 (s->match_length == MIN_MATCH &&
@@ -1086,8 +1126,8 @@ local int deflate_slow(s, flush)
1086 1126
1087 check_match(s, s->strstart-1, s->prev_match, s->prev_length); 1127 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1088 1128
1089 bflush = tr_tally(s, s->strstart -1 - s->prev_match, 1129 bflush = _tr_tally(s, s->strstart -1 - s->prev_match,
1090 s->prev_length - MIN_MATCH); 1130 s->prev_length - MIN_MATCH);
1091 1131
1092 /* Insert in hash table all strings up to the end of the match. 1132 /* Insert in hash table all strings up to the end of the match.
1093 * strstart-1 and strstart are already inserted. If there is not 1133 * strstart-1 and strstart are already inserted. If there is not
@@ -1113,7 +1153,7 @@ local int deflate_slow(s, flush)
1113 * is longer, truncate the previous match to a single literal. 1153 * is longer, truncate the previous match to a single literal.
1114 */ 1154 */
1115 Tracevv((stderr,"%c", s->window[s->strstart-1])); 1155 Tracevv((stderr,"%c", s->window[s->strstart-1]));
1116 if (tr_tally (s, 0, s->window[s->strstart-1])) { 1156 if (_tr_tally (s, 0, s->window[s->strstart-1])) {
1117 FLUSH_BLOCK_ONLY(s, 0); 1157 FLUSH_BLOCK_ONLY(s, 0);
1118 } 1158 }
1119 s->strstart++; 1159 s->strstart++;
@@ -1131,7 +1171,7 @@ local int deflate_slow(s, flush)
1131 Assert (flush != Z_NO_FLUSH, "no flush?"); 1171 Assert (flush != Z_NO_FLUSH, "no flush?");
1132 if (s->match_available) { 1172 if (s->match_available) {
1133 Tracevv((stderr,"%c", s->window[s->strstart-1])); 1173 Tracevv((stderr,"%c", s->window[s->strstart-1]));
1134 tr_tally (s, 0, s->window[s->strstart-1]); 1174 _tr_tally (s, 0, s->window[s->strstart-1]);
1135 s->match_available = 0; 1175 s->match_available = 0;
1136 } 1176 }
1137 FLUSH_BLOCK(s, flush == Z_FINISH); 1177 FLUSH_BLOCK(s, flush == Z_FINISH);
diff --git a/deflate.h b/deflate.h
index 834563b..95ca0c2 100644
--- a/deflate.h
+++ b/deflate.h
@@ -1,5 +1,5 @@
1/* deflate.h -- internal compression state 1/* deflate.h -- internal compression state
2 * Copyright (C) 1995 Jean-loup Gailly 2 * Copyright (C) 1995-1996 Jean-loup Gailly
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -8,7 +8,10 @@
8 subject to change. Applications should only use zlib.h. 8 subject to change. Applications should only use zlib.h.
9 */ 9 */
10 10
11/* $Id: deflate.h,v 1.5 1995/05/03 17:27:09 jloup Exp $ */ 11/* $Id: deflate.h,v 1.9 1996/01/30 21:59:13 me Exp $ */
12
13#ifndef _DEFLATE_H
14#define _DEFLATE_H
12 15
13#include "zutil.h" 16#include "zutil.h"
14 17
@@ -16,11 +19,6 @@
16 * Internal compression state. 19 * Internal compression state.
17 */ 20 */
18 21
19/* Data type */
20#define BINARY 0
21#define ASCII 1
22#define UNKNOWN 2
23
24#define LENGTH_CODES 29 22#define LENGTH_CODES 29
25/* number of length codes, not counting the special END_BLOCK code */ 23/* number of length codes, not counting the special END_BLOCK code */
26 24
@@ -87,7 +85,6 @@ typedef struct internal_state {
87 Bytef *pending_buf; /* output still pending */ 85 Bytef *pending_buf; /* output still pending */
88 Bytef *pending_out; /* next pending byte to output to the stream */ 86 Bytef *pending_out; /* next pending byte to output to the stream */
89 int pending; /* nb of bytes in the pending buffer */ 87 int pending; /* nb of bytes in the pending buffer */
90 uLong adler; /* adler32 of uncompressed data */
91 int noheader; /* suppress zlib header and adler32 */ 88 int noheader; /* suppress zlib header and adler32 */
92 Byte data_type; /* UNKNOWN, BINARY or ASCII */ 89 Byte data_type; /* UNKNOWN, BINARY or ASCII */
93 Byte method; /* STORED (for zip only) or DEFLATED */ 90 Byte method; /* STORED (for zip only) or DEFLATED */
@@ -268,9 +265,11 @@ typedef struct internal_state {
268 */ 265 */
269 266
270 /* in trees.c */ 267 /* in trees.c */
271void tr_init OF((deflate_state *s)); 268void _tr_init OF((deflate_state *s));
272int tr_tally OF((deflate_state *s, int dist, int lc)); 269int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
273ulg tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len, int eof)); 270ulg _tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len,
274void tr_align OF((deflate_state *s)); 271 int eof));
275void tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len, 272void _tr_align OF((deflate_state *s));
276 int eof)); 273void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
274 int eof));
275#endif
diff --git a/example.c b/example.c
index fddb8f8..175fa81 100644
--- a/example.c
+++ b/example.c
@@ -1,9 +1,9 @@
1/* example.c -- usage example of the zlib compression library 1/* example.c -- usage example of the zlib compression library
2 * Copyright (C) 1995 Jean-loup Gailly. 2 * Copyright (C) 1995-1996 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: example.c,v 1.9 1995/05/03 17:27:09 jloup Exp $ */ 6/* $Id: example.c,v 1.13 1996/01/30 21:59:13 me Exp $ */
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include "zlib.h" 9#include "zlib.h"
@@ -22,38 +22,44 @@
22 } \ 22 } \
23} 23}
24 24
25char *hello = "hello, hello!"; 25const char hello[] = "hello, hello!";
26/* "hello world" would be more standard, but the repeated "hello" 26/* "hello world" would be more standard, but the repeated "hello"
27 * stresses the compression code better, sorry... 27 * stresses the compression code better, sorry...
28 */ 28 */
29 29
30void test_compress OF((Bytef *compr, uLong comprLen, 30const char dictionary[] = "hello";
31 Bytef *uncompr, uLong uncomprLen)); 31uLong dictId; /* Adler32 value of the dictionary */
32void test_gzio OF((char *out, char *in, 32
33 Bytef *uncompr, int uncomprLen)); 33void test_compress OF((Byte *compr, uLong comprLen,
34void test_deflate OF((Bytef *compr, uLong comprLen)); 34 Byte *uncompr, uLong uncomprLen));
35void test_inflate OF((Bytef *compr, uLong comprLen, 35void test_gzio OF((const char *out, const char *in,
36 Bytef *uncompr, uLong uncomprLen)); 36 Byte *uncompr, int uncomprLen));
37void test_large_deflate OF((Bytef *compr, uLong comprLen, 37void test_deflate OF((Byte *compr, uLong comprLen));
38 Bytef *uncompr, uLong uncomprLen)); 38void test_inflate OF((Byte *compr, uLong comprLen,
39void test_large_inflate OF((Bytef *compr, uLong comprLen, 39 Byte *uncompr, uLong uncomprLen));
40 Bytef *uncompr, uLong uncomprLen)); 40void test_large_deflate OF((Byte *compr, uLong comprLen,
41void test_flush OF((Bytef *compr, uLong comprLen)); 41 Byte *uncompr, uLong uncomprLen));
42void test_sync OF((Bytef *compr, uLong comprLen, 42void test_large_inflate OF((Byte *compr, uLong comprLen,
43 Bytef *uncompr, uLong uncomprLen)); 43 Byte *uncompr, uLong uncomprLen));
44void test_flush OF((Byte *compr, uLong comprLen));
45void test_sync OF((Byte *compr, uLong comprLen,
46 Byte *uncompr, uLong uncomprLen));
47void test_dict_deflate OF((Byte *compr, uLong comprLen));
48void test_dict_inflate OF((Byte *compr, uLong comprLen,
49 Byte *uncompr, uLong uncomprLen));
44int main OF((int argc, char *argv[])); 50int main OF((int argc, char *argv[]));
45 51
46/* =========================================================================== 52/* ===========================================================================
47 * Test compress() and uncompress() 53 * Test compress() and uncompress()
48 */ 54 */
49void test_compress(compr, comprLen, uncompr, uncomprLen) 55void test_compress(compr, comprLen, uncompr, uncomprLen)
50 Bytef *compr, *uncompr; 56 Byte *compr, *uncompr;
51 uLong comprLen, uncomprLen; 57 uLong comprLen, uncomprLen;
52{ 58{
53 int err; 59 int err;
54 uLong len = strlen(hello)+1; 60 uLong len = strlen(hello)+1;
55 61
56 err = compress(compr, &comprLen, (Byte*)hello, len); 62 err = compress(compr, &comprLen, (const Bytef*)hello, len);
57 CHECK_ERR(err, "compress"); 63 CHECK_ERR(err, "compress");
58 64
59 strcpy((char*)uncompr, "garbage"); 65 strcpy((char*)uncompr, "garbage");
@@ -72,9 +78,9 @@ void test_compress(compr, comprLen, uncompr, uncomprLen)
72 * Test read/write of .gz files 78 * Test read/write of .gz files
73 */ 79 */
74void test_gzio(out, in, uncompr, uncomprLen) 80void test_gzio(out, in, uncompr, uncomprLen)
75 char *out; /* output file */ 81 const char *out; /* output file */
76 char *in; /* input file */ 82 const char *in; /* input file */
77 Bytef *uncompr; 83 Byte *uncompr;
78 int uncomprLen; 84 int uncomprLen;
79{ 85{
80 int err; 86 int err;
@@ -87,7 +93,7 @@ void test_gzio(out, in, uncompr, uncomprLen)
87 exit(1); 93 exit(1);
88 } 94 }
89 95
90 if (gzwrite(file, hello, len) != len) { 96 if (gzwrite(file, (const voidp)hello, (unsigned)len) != len) {
91 fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err)); 97 fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err));
92 } 98 }
93 gzclose(file); 99 gzclose(file);
@@ -98,7 +104,7 @@ void test_gzio(out, in, uncompr, uncomprLen)
98 } 104 }
99 strcpy((char*)uncompr, "garbage"); 105 strcpy((char*)uncompr, "garbage");
100 106
101 uncomprLen = gzread(file, uncompr, uncomprLen); 107 uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen);
102 if (uncomprLen != len) { 108 if (uncomprLen != len) {
103 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); 109 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
104 } 110 }
@@ -115,7 +121,7 @@ void test_gzio(out, in, uncompr, uncomprLen)
115 * Test deflate() with small buffers 121 * Test deflate() with small buffers
116 */ 122 */
117void test_deflate(compr, comprLen) 123void test_deflate(compr, comprLen)
118 Bytef *compr; 124 Byte *compr;
119 uLong comprLen; 125 uLong comprLen;
120{ 126{
121 z_stream c_stream; /* compression stream */ 127 z_stream c_stream; /* compression stream */
@@ -153,7 +159,7 @@ void test_deflate(compr, comprLen)
153 * Test inflate() with small buffers 159 * Test inflate() with small buffers
154 */ 160 */
155void test_inflate(compr, comprLen, uncompr, uncomprLen) 161void test_inflate(compr, comprLen, uncompr, uncomprLen)
156 Bytef *compr, *uncompr; 162 Byte *compr, *uncompr;
157 uLong comprLen, uncomprLen; 163 uLong comprLen, uncomprLen;
158{ 164{
159 int err; 165 int err;
@@ -171,7 +177,7 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen)
171 d_stream.next_in = compr; 177 d_stream.next_in = compr;
172 d_stream.next_out = uncompr; 178 d_stream.next_out = uncompr;
173 179
174 while (d_stream.total_out < uncomprLen) { 180 while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
175 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ 181 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
176 err = inflate(&d_stream, Z_NO_FLUSH); 182 err = inflate(&d_stream, Z_NO_FLUSH);
177 if (err == Z_STREAM_END) break; 183 if (err == Z_STREAM_END) break;
@@ -192,7 +198,7 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen)
192 * Test deflate() with large buffers and dynamic change of compression level 198 * Test deflate() with large buffers and dynamic change of compression level
193 */ 199 */
194void test_large_deflate(compr, comprLen, uncompr, uncomprLen) 200void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
195 Bytef *compr, *uncompr; 201 Byte *compr, *uncompr;
196 uLong comprLen, uncomprLen; 202 uLong comprLen, uncomprLen;
197{ 203{
198 z_stream c_stream; /* compression stream */ 204 z_stream c_stream; /* compression stream */
@@ -206,7 +212,7 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
206 CHECK_ERR(err, "deflateInit"); 212 CHECK_ERR(err, "deflateInit");
207 213
208 c_stream.next_out = compr; 214 c_stream.next_out = compr;
209 c_stream.avail_out = comprLen; 215 c_stream.avail_out = (uInt)comprLen;
210 216
211 /* At this point, uncompr is still mostly zeroes, so it should compress 217 /* At this point, uncompr is still mostly zeroes, so it should compress
212 * very well: 218 * very well:
@@ -245,7 +251,7 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
245 * Test inflate() with large buffers 251 * Test inflate() with large buffers
246 */ 252 */
247void test_large_inflate(compr, comprLen, uncompr, uncomprLen) 253void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
248 Bytef *compr, *uncompr; 254 Byte *compr, *uncompr;
249 uLong comprLen, uncomprLen; 255 uLong comprLen, uncomprLen;
250{ 256{
251 int err; 257 int err;
@@ -265,7 +271,7 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
265 271
266 for (;;) { 272 for (;;) {
267 d_stream.next_out = uncompr; /* discard the output */ 273 d_stream.next_out = uncompr; /* discard the output */
268 d_stream.avail_out = uncomprLen; 274 d_stream.avail_out = (uInt)uncomprLen;
269 err = inflate(&d_stream, Z_NO_FLUSH); 275 err = inflate(&d_stream, Z_NO_FLUSH);
270 if (err == Z_STREAM_END) break; 276 if (err == Z_STREAM_END) break;
271 CHECK_ERR(err, "large inflate"); 277 CHECK_ERR(err, "large inflate");
@@ -285,7 +291,7 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
285 * Test deflate() with full flush 291 * Test deflate() with full flush
286 */ 292 */
287void test_flush(compr, comprLen) 293void test_flush(compr, comprLen)
288 Bytef *compr; 294 Byte *compr;
289 uLong comprLen; 295 uLong comprLen;
290{ 296{
291 z_stream c_stream; /* compression stream */ 297 z_stream c_stream; /* compression stream */
@@ -321,7 +327,7 @@ void test_flush(compr, comprLen)
321 * Test inflateSync() 327 * Test inflateSync()
322 */ 328 */
323void test_sync(compr, comprLen, uncompr, uncomprLen) 329void test_sync(compr, comprLen, uncompr, uncomprLen)
324 Bytef *compr, *uncompr; 330 Byte *compr, *uncompr;
325 uLong comprLen, uncomprLen; 331 uLong comprLen, uncomprLen;
326{ 332{
327 int err; 333 int err;
@@ -344,7 +350,7 @@ void test_sync(compr, comprLen, uncompr, uncomprLen)
344 inflate(&d_stream, Z_NO_FLUSH); 350 inflate(&d_stream, Z_NO_FLUSH);
345 CHECK_ERR(err, "inflate"); 351 CHECK_ERR(err, "inflate");
346 352
347 d_stream.avail_in = (uInt)uncomprLen-2; /* read all compressed data */ 353 d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */
348 err = inflateSync(&d_stream); /* but skip the damaged part */ 354 err = inflateSync(&d_stream); /* but skip the damaged part */
349 CHECK_ERR(err, "inflateSync"); 355 CHECK_ERR(err, "inflateSync");
350 356
@@ -360,6 +366,91 @@ void test_sync(compr, comprLen, uncompr, uncomprLen)
360} 366}
361 367
362/* =========================================================================== 368/* ===========================================================================
369 * Test deflate() with preset dictionary
370 */
371void test_dict_deflate(compr, comprLen)
372 Byte *compr;
373 uLong comprLen;
374{
375 z_stream c_stream; /* compression stream */
376 int err;
377
378 c_stream.zalloc = (alloc_func)0;
379 c_stream.zfree = (free_func)0;
380 c_stream.opaque = (voidpf)0;
381
382 err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
383 CHECK_ERR(err, "deflateInit");
384
385 err = deflateSetDictionary(&c_stream,
386 (const Bytef*)dictionary, sizeof(dictionary));
387 CHECK_ERR(err, "deflateSetDictionary");
388
389 dictId = c_stream.adler;
390 c_stream.next_out = compr;
391 c_stream.avail_out = (uInt)comprLen;
392
393 c_stream.next_in = (Bytef*)hello;
394 c_stream.avail_in = (uInt)strlen(hello)+1;
395
396 err = deflate(&c_stream, Z_FINISH);
397 if (err != Z_STREAM_END) {
398 fprintf(stderr, "deflate should report Z_STREAM_END\n");
399 }
400 err = deflateEnd(&c_stream);
401 CHECK_ERR(err, "deflateEnd");
402}
403
404/* ===========================================================================
405 * Test inflate() with a preset dictionary
406 */
407void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
408 Byte *compr, *uncompr;
409 uLong comprLen, uncomprLen;
410{
411 int err;
412 z_stream d_stream; /* decompression stream */
413
414 strcpy((char*)uncompr, "garbage");
415
416 d_stream.zalloc = (alloc_func)0;
417 d_stream.zfree = (free_func)0;
418 d_stream.opaque = (voidpf)0;
419
420 err = inflateInit(&d_stream);
421 CHECK_ERR(err, "inflateInit");
422
423 d_stream.next_in = compr;
424 d_stream.avail_in = (uInt)comprLen;
425
426 d_stream.next_out = uncompr;
427 d_stream.avail_out = (uInt)uncomprLen;
428
429 for (;;) {
430 err = inflate(&d_stream, Z_NO_FLUSH);
431 if (err == Z_STREAM_END) break;
432 if (err == Z_NEED_DICT) {
433 if (d_stream.adler != dictId) {
434 fprintf(stderr, "unexpected dictionary");
435 exit(1);
436 }
437 err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
438 sizeof(dictionary));
439 }
440 CHECK_ERR(err, "inflate with dict");
441 }
442
443 err = inflateEnd(&d_stream);
444 CHECK_ERR(err, "inflateEnd");
445
446 if (strcmp((char*)uncompr, hello)) {
447 fprintf(stderr, "bad inflate with dict\n");
448 } else {
449 printf("inflate with dictionary: %s\n", uncompr);
450 }
451}
452
453/* ===========================================================================
363 * Usage: example [output.gz [input.gz]] 454 * Usage: example [output.gz [input.gz]]
364 */ 455 */
365 456
@@ -367,8 +458,8 @@ int main(argc, argv)
367 int argc; 458 int argc;
368 char *argv[]; 459 char *argv[];
369{ 460{
370 Bytef *compr, *uncompr; 461 Byte *compr, *uncompr;
371 uLong comprLen = 32750*sizeof(int); /* don't overflow on MSDOS */ 462 uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
372 uLong uncomprLen = comprLen; 463 uLong uncomprLen = comprLen;
373 464
374 if (zlib_version[0] != ZLIB_VERSION[0]) { 465 if (zlib_version[0] != ZLIB_VERSION[0]) {
@@ -379,8 +470,8 @@ int main(argc, argv)
379 fprintf(stderr, "warning: different zlib version\n"); 470 fprintf(stderr, "warning: different zlib version\n");
380 } 471 }
381 472
382 compr = (Bytef*)malloc(comprLen); 473 compr = (Byte*)malloc((uInt)comprLen);
383 uncompr = (Bytef*)calloc(uncomprLen, 1); /* must be cleared initially */ 474 uncompr = (Byte*)calloc((uInt)uncomprLen, 1); /* must be cleared */
384 if (compr == Z_NULL || uncompr == Z_NULL) { 475 if (compr == Z_NULL || uncompr == Z_NULL) {
385 printf("out of memory\n"); 476 printf("out of memory\n");
386 exit(1); 477 exit(1);
@@ -401,6 +492,9 @@ int main(argc, argv)
401 test_flush(compr, comprLen); 492 test_flush(compr, comprLen);
402 test_sync(compr, comprLen, uncompr, uncomprLen); 493 test_sync(compr, comprLen, uncompr, uncomprLen);
403 494
495 test_dict_deflate(compr, comprLen);
496 test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
497
404 exit(0); 498 exit(0);
405 return 0; /* to avoid warning */ 499 return 0; /* to avoid warning */
406} 500}
diff --git a/gzio.c b/gzio.c
index 03a4674..6f6832b 100644
--- a/gzio.c
+++ b/gzio.c
@@ -1,9 +1,9 @@
1/* gzio.c -- IO on .gz files 1/* gzio.c -- IO on .gz files
2 * Copyright (C) 1995 Jean-loup Gailly. 2 * Copyright (C) 1995-1996 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: gzio.c,v 1.8 1995/05/03 17:27:09 jloup Exp $ */ 6/* $Id: gzio.c,v 1.12 1996/01/30 21:59:14 me Exp $ */
7 7
8#include <stdio.h> 8#include <stdio.h>
9 9
@@ -16,8 +16,7 @@ struct internal_state {int dummy;}; /* for buggy compilers */
16#define ALLOC(size) malloc(size) 16#define ALLOC(size) malloc(size)
17#define TRYFREE(p) {if (p) free(p);} 17#define TRYFREE(p) {if (p) free(p);}
18 18
19#define GZ_MAGIC_1 0x1f 19static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
20#define GZ_MAGIC_2 0x8b
21 20
22/* gzip flag byte */ 21/* gzip flag byte */
23#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ 22#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
@@ -27,10 +26,6 @@ struct internal_state {int dummy;}; /* for buggy compilers */
27#define COMMENT 0x10 /* bit 4 set: file comment present */ 26#define COMMENT 0x10 /* bit 4 set: file comment present */
28#define RESERVED 0xE0 /* bits 5..7: reserved */ 27#define RESERVED 0xE0 /* bits 5..7: reserved */
29 28
30#ifndef SEEK_CUR
31# define SEEK_CUR 1
32#endif
33
34typedef struct gz_stream { 29typedef struct gz_stream {
35 z_stream stream; 30 z_stream stream;
36 int z_err; /* error code for last stream operation */ 31 int z_err; /* error code for last stream operation */
@@ -46,40 +41,12 @@ typedef struct gz_stream {
46} gz_stream; 41} gz_stream;
47 42
48 43
49local int destroy OF((gz_stream *s)); 44local gzFile gz_open OF((const char *path, const char *mode, int fd));
50local gzFile gz_open OF((char *path, char *mode, int fd)); 45local int get_byte OF((gz_stream *s));
51local void putLong OF((FILE *file, uLong x)); 46local void check_header OF((gz_stream *s));
52local uLong getLong OF((Bytef *buf)); 47local int destroy OF((gz_stream *s));
53 48local void putLong OF((FILE *file, uLong x));
54 /* =========================================================================== 49local uLong getLong OF((gz_stream *s));
55 * Cleanup then free the given gz_stream. Return a zlib error code.
56 */
57local int destroy (s)
58 gz_stream *s;
59{
60 int err = Z_OK;
61
62 if (!s) return Z_STREAM_ERROR;
63
64 TRYFREE(s->inbuf);
65 TRYFREE(s->outbuf);
66 TRYFREE(s->path);
67 TRYFREE(s->msg);
68
69 if (s->stream.state != NULL) {
70 if (s->mode == 'w') {
71 err = deflateEnd(&(s->stream));
72 } else if (s->mode == 'r') {
73 err = inflateEnd(&(s->stream));
74 }
75 }
76 if (s->file != NULL && fclose(s->file)) {
77 err = Z_ERRNO;
78 }
79 if (s->z_err < 0) err = s->z_err;
80 TRYFREE(s);
81 return err;
82}
83 50
84/* =========================================================================== 51/* ===========================================================================
85 Opens a gzip (.gz) file for reading or writing. The mode parameter 52 Opens a gzip (.gz) file for reading or writing. The mode parameter
@@ -91,15 +58,20 @@ local int destroy (s)
91 zlib error is Z_MEM_ERROR). 58 zlib error is Z_MEM_ERROR).
92*/ 59*/
93local gzFile gz_open (path, mode, fd) 60local gzFile gz_open (path, mode, fd)
94 char *path; 61 const char *path;
95 char *mode; 62 const char *mode;
96 int fd; 63 int fd;
97{ 64{
98 int err; 65 int err;
99 int level = Z_DEFAULT_COMPRESSION; /* compression level */ 66 int level = Z_DEFAULT_COMPRESSION; /* compression level */
100 char *p = mode; 67 char *p = (char*)mode;
101 gz_stream *s = (gz_stream *)ALLOC(sizeof(gz_stream)); 68 gz_stream *s;
69 char fmode[80]; /* copy of mode, without the compression level */
70 char *m = fmode;
71
72 if (!path || !mode) return Z_NULL;
102 73
74 s = (gz_stream *)ALLOC(sizeof(gz_stream));
103 if (!s) return Z_NULL; 75 if (!s) return Z_NULL;
104 76
105 s->stream.zalloc = (alloc_func)0; 77 s->stream.zalloc = (alloc_func)0;
@@ -124,9 +96,13 @@ local gzFile gz_open (path, mode, fd)
124 s->mode = '\0'; 96 s->mode = '\0';
125 do { 97 do {
126 if (*p == 'r') s->mode = 'r'; 98 if (*p == 'r') s->mode = 'r';
127 if (*p == 'w') s->mode = 'w'; 99 if (*p == 'w' || *p == 'a') s->mode = 'w';
128 if (*p >= '1' && *p <= '9') level = *p - '0'; 100 if (*p >= '0' && *p <= '9') {
129 } while (*p++); 101 level = *p - '0';
102 } else {
103 *m++ = *p; /* copy the mode */
104 }
105 } while (*p++ && m != fmode + sizeof(fmode));
130 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL; 106 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
131 107
132 if (s->mode == 'w') { 108 if (s->mode == 'w') {
@@ -150,7 +126,7 @@ local gzFile gz_open (path, mode, fd)
150 s->stream.avail_out = Z_BUFSIZE; 126 s->stream.avail_out = Z_BUFSIZE;
151 127
152 errno = 0; 128 errno = 0;
153 s->file = fd < 0 ? FOPEN(path, mode) : (FILE*)fdopen(fd, mode); 129 s->file = fd < 0 ? FOPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
154 130
155 if (s->file == NULL) { 131 if (s->file == NULL) {
156 return destroy(s), (gzFile)Z_NULL; 132 return destroy(s), (gzFile)Z_NULL;
@@ -158,50 +134,10 @@ local gzFile gz_open (path, mode, fd)
158 if (s->mode == 'w') { 134 if (s->mode == 'w') {
159 /* Write a very simple .gz header: 135 /* Write a very simple .gz header:
160 */ 136 */
161 fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", GZ_MAGIC_1, GZ_MAGIC_2, 137 fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
162 Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE); 138 Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
163 } else { 139 } else {
164 /* Check and skip the header: 140 check_header(s); /* skip the .gz header */
165 */
166 Byte c1 = 0, c2 = 0;
167 Byte method = 0;
168 Byte flags = 0;
169 Byte xflags = 0;
170 Byte time[4];
171 Byte osCode;
172 int c;
173
174 s->stream.avail_in = fread(s->inbuf, 1, 2, s->file);
175 if (s->stream.avail_in != 2 || s->inbuf[0] != GZ_MAGIC_1
176 || s->inbuf[1] != GZ_MAGIC_2) {
177 s->transparent = 1;
178 return (gzFile)s;
179 }
180 s->stream.avail_in = 0;
181 fscanf(s->file,"%c%c%4c%c%c", &method, &flags, time, &xflags, &osCode);
182
183 if (method != Z_DEFLATED || feof(s->file) || (flags & RESERVED) != 0) {
184 s->z_err = Z_DATA_ERROR;
185 return (gzFile)s;
186 }
187 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
188 long len;
189 fscanf(s->file, "%c%c", &c1, &c2);
190 len = c1 + ((long)c2<<8);
191 fseek(s->file, len, SEEK_CUR);
192 }
193 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
194 while ((c = getc(s->file)) != 0 && c != EOF) ;
195 }
196 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
197 while ((c = getc(s->file)) != 0 && c != EOF) ;
198 }
199 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
200 fscanf(s->file, "%c%c", &c1, &c2);
201 }
202 if (feof(s->file)) {
203 s->z_err = Z_DATA_ERROR;
204 }
205 } 141 }
206 return (gzFile)s; 142 return (gzFile)s;
207} 143}
@@ -210,26 +146,139 @@ local gzFile gz_open (path, mode, fd)
210 Opens a gzip (.gz) file for reading or writing. 146 Opens a gzip (.gz) file for reading or writing.
211*/ 147*/
212gzFile gzopen (path, mode) 148gzFile gzopen (path, mode)
213 char *path; 149 const char *path;
214 char *mode; 150 const char *mode;
215{ 151{
216 return gz_open (path, mode, -1); 152 return gz_open (path, mode, -1);
217} 153}
218 154
219/* =========================================================================== 155/* ===========================================================================
220 Associate a gzFile with the file descriptor fd. 156 Associate a gzFile with the file descriptor fd. fd is not dup'ed here
157 to mimic the behavio(u)r of fdopen.
221*/ 158*/
222gzFile gzdopen (fd, mode) 159gzFile gzdopen (fd, mode)
223 int fd; 160 int fd;
224 char *mode; 161 const char *mode;
225{ 162{
226 char name[20]; 163 char name[20];
164
165 if (fd < 0) return (gzFile)Z_NULL;
227 sprintf(name, "<fd:%d>", fd); /* for debugging */ 166 sprintf(name, "<fd:%d>", fd); /* for debugging */
228 167
229 return gz_open (name, mode, fd); 168 return gz_open (name, mode, fd);
230} 169}
231 170
232/* =========================================================================== 171/* ===========================================================================
172 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
173 for end of file.
174 IN assertion: the stream s has been sucessfully opened for reading.
175*/
176local int get_byte(s)
177 gz_stream *s;
178{
179 if (s->z_eof) return EOF;
180 if (s->stream.avail_in == 0) {
181 errno = 0;
182 s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
183 if (s->stream.avail_in == 0) {
184 s->z_eof = 1;
185 if (ferror(s->file)) s->z_err = Z_ERRNO;
186 return EOF;
187 }
188 s->stream.next_in = s->inbuf;
189 }
190 s->stream.avail_in--;
191 return *(s->stream.next_in)++;
192}
193
194/* ===========================================================================
195 Check the gzip header of a gz_stream opened for reading. Set the stream
196 mode to transparent if the gzip magic header is not present; set s->err
197 to Z_DATA_ERROR if the magic header is present but the rest of the header
198 is incorrect.
199 IN assertion: the stream s has already been created sucessfully;
200 s->stream.avail_in is zero for the first time, but may be non-zero
201 for concatenated .gz files.
202*/
203local void check_header(s)
204 gz_stream *s;
205{
206 int method; /* method byte */
207 int flags; /* flags byte */
208 uInt len;
209 int c;
210
211 /* Check the gzip magic header */
212 for (len = 0; len < 2; len++) {
213 c = get_byte(s);
214 if (c != gz_magic[len]) {
215 s->transparent = 1;
216 if (c != EOF) s->stream.avail_in++, s->stream.next_in--;
217 s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
218 return;
219 }
220 }
221 method = get_byte(s);
222 flags = get_byte(s);
223 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
224 s->z_err = Z_DATA_ERROR;
225 return;
226 }
227
228 /* Discard time, xflags and OS code: */
229 for (len = 0; len < 6; len++) (void)get_byte(s);
230
231 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
232 len = (uInt)get_byte(s);
233 len += ((uInt)get_byte(s))<<8;
234 /* len is garbage if EOF but the loop below will quit anyway */
235 while (len-- != 0 && get_byte(s) != EOF) ;
236 }
237 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
238 while ((c = get_byte(s)) != 0 && c != EOF) ;
239 }
240 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
241 while ((c = get_byte(s)) != 0 && c != EOF) ;
242 }
243 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
244 for (len = 0; len < 2; len++) (void)get_byte(s);
245 }
246 s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
247}
248
249 /* ===========================================================================
250 * Cleanup then free the given gz_stream. Return a zlib error code.
251 Try freeing in the reverse order of allocations.
252 */
253local int destroy (s)
254 gz_stream *s;
255{
256 int err = Z_OK;
257
258 if (!s) return Z_STREAM_ERROR;
259
260 TRYFREE(s->msg);
261
262 if (s->stream.state != NULL) {
263 if (s->mode == 'w') {
264 err = deflateEnd(&(s->stream));
265 } else if (s->mode == 'r') {
266 err = inflateEnd(&(s->stream));
267 }
268 }
269 if (s->file != NULL && fclose(s->file)) {
270 err = Z_ERRNO;
271 }
272 if (s->z_err < 0) err = s->z_err;
273
274 TRYFREE(s->inbuf);
275 TRYFREE(s->outbuf);
276 TRYFREE(s->path);
277 TRYFREE(s);
278 return err;
279}
280
281/* ===========================================================================
233 Reads the given number of uncompressed bytes from the compressed file. 282 Reads the given number of uncompressed bytes from the compressed file.
234 gzread returns the number of bytes actually read (0 for end of file). 283 gzread returns the number of bytes actually read (0 for end of file).
235*/ 284*/
@@ -239,52 +288,73 @@ int gzread (file, buf, len)
239 unsigned len; 288 unsigned len;
240{ 289{
241 gz_stream *s = (gz_stream*)file; 290 gz_stream *s = (gz_stream*)file;
291 Bytef *start = buf; /* starting point for crc computation */
292 Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */
242 293
243 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR; 294 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
244 295
245 if (s->transparent) { 296 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
246 int n = 0; 297 if (s->z_err == Z_STREAM_END) return 0; /* EOF */
247 Byte *b = (Byte*)buf;
248 /* Copy the first two (non-magic) bytes if not done already */
249 while (s->stream.avail_in > 0 && len > 0) {
250 *b++ = *s->stream.next_in++;
251 s->stream.avail_in--;
252 len--; n++;
253 }
254 if (len == 0) return n;
255 return n + fread(b, 1, len, s->file);
256 }
257 if (s->z_err == Z_DATA_ERROR) return -1; /* bad .gz file */
258 if (s->z_err == Z_STREAM_END) return 0; /* don't read crc as data */
259 298
260 s->stream.next_out = buf; 299 s->stream.next_out = next_out = buf;
261 s->stream.avail_out = len; 300 s->stream.avail_out = len;
262 301
263 while (s->stream.avail_out != 0) { 302 while (s->stream.avail_out != 0) {
264 303
304 if (s->transparent) {
305 /* Copy first the lookahead bytes: */
306 uInt n = s->stream.avail_in;
307 if (n > s->stream.avail_out) n = s->stream.avail_out;
308 if (n > 0) {
309 zmemcpy(s->stream.next_out, s->stream.next_in, n);
310 next_out += n;
311 s->stream.next_out = next_out;
312 s->stream.next_in += n;
313 s->stream.avail_out -= n;
314 s->stream.avail_in -= n;
315 }
316 if (s->stream.avail_out > 0) {
317 s->stream.avail_out -= fread(next_out, 1, s->stream.avail_out,
318 s->file);
319 }
320 return (int)(len - s->stream.avail_out);
321 }
265 if (s->stream.avail_in == 0 && !s->z_eof) { 322 if (s->stream.avail_in == 0 && !s->z_eof) {
266 323
267 errno = 0; 324 errno = 0;
268 s->stream.avail_in = 325 s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
269 fread(s->inbuf, 1, Z_BUFSIZE, s->file);
270 if (s->stream.avail_in == 0) { 326 if (s->stream.avail_in == 0) {
271 s->z_eof = 1; 327 s->z_eof = 1;
272 } else if (s->stream.avail_in == (uInt)EOF) { 328 if (ferror(s->file)) {
273 s->stream.avail_in = 0; 329 s->z_err = Z_ERRNO;
274 s->z_eof = 1; 330 break;
275 s->z_err = Z_ERRNO; 331 }
276 break;
277 } 332 }
278 s->stream.next_in = s->inbuf; 333 s->stream.next_in = s->inbuf;
279 } 334 }
280 s->z_err = inflate(&(s->stream), Z_NO_FLUSH); 335 s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
281 336
282 if (s->z_err == Z_STREAM_END || 337 if (s->z_err == Z_STREAM_END) {
283 s->z_err != Z_OK || s->z_eof) break; 338 /* Check CRC and original size */
339 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
340 start = s->stream.next_out;
341
342 if (getLong(s) != s->crc || getLong(s) != s->stream.total_out) {
343 s->z_err = Z_DATA_ERROR;
344 } else {
345 /* Check for concatenated .gz files: */
346 check_header(s);
347 if (s->z_err == Z_OK) {
348 inflateReset(&(s->stream));
349 s->crc = crc32(0L, Z_NULL, 0);
350 }
351 }
352 }
353 if (s->z_err != Z_OK || s->z_eof) break;
284 } 354 }
285 len -= s->stream.avail_out; 355 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
286 s->crc = crc32(s->crc, buf, len); 356
287 return (int)len; 357 return (int)(len - s->stream.avail_out);
288} 358}
289 359
290/* =========================================================================== 360/* ===========================================================================
@@ -293,7 +363,7 @@ int gzread (file, buf, len)
293*/ 363*/
294int gzwrite (file, buf, len) 364int gzwrite (file, buf, len)
295 gzFile file; 365 gzFile file;
296 voidp buf; 366 const voidp buf;
297 unsigned len; 367 unsigned len;
298{ 368{
299 gz_stream *s = (gz_stream*)file; 369 gz_stream *s = (gz_stream*)file;
@@ -344,7 +414,7 @@ int gzflush (file, flush)
344 len = Z_BUFSIZE - s->stream.avail_out; 414 len = Z_BUFSIZE - s->stream.avail_out;
345 415
346 if (len != 0) { 416 if (len != 0) {
347 if (fwrite(s->outbuf, 1, len, s->file) != len) { 417 if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
348 s->z_err = Z_ERRNO; 418 s->z_err = Z_ERRNO;
349 return Z_ERRNO; 419 return Z_ERRNO;
350 } 420 }
@@ -380,18 +450,19 @@ local void putLong (file, x)
380} 450}
381 451
382/* =========================================================================== 452/* ===========================================================================
383 Reads a long in LSB order from the given buffer 453 Reads a long in LSB order from the given gz_stream. Sets
384*/ 454*/
385local uLong getLong (buf) 455local uLong getLong (s)
386 Bytef *buf; 456 gz_stream *s;
387{ 457{
388 uLong x = 0; 458 uLong x = (uLong)get_byte(s);
389 Bytef *p = buf+4; 459 int c;
390 460
391 do { 461 x += ((uLong)get_byte(s))<<8;
392 x <<= 8; 462 x += ((uLong)get_byte(s))<<16;
393 x |= *--p; 463 c = get_byte(s);
394 } while (p != buf); 464 if (c == EOF) s->z_err = Z_DATA_ERROR;
465 x += ((uLong)c)<<24;
395 return x; 466 return x;
396} 467}
397 468
@@ -402,7 +473,6 @@ local uLong getLong (buf)
402int gzclose (file) 473int gzclose (file)
403 gzFile file; 474 gzFile file;
404{ 475{
405 uInt n;
406 int err; 476 int err;
407 gz_stream *s = (gz_stream*)file; 477 gz_stream *s = (gz_stream*)file;
408 478
@@ -415,25 +485,6 @@ int gzclose (file)
415 putLong (s->file, s->crc); 485 putLong (s->file, s->crc);
416 putLong (s->file, s->stream.total_in); 486 putLong (s->file, s->stream.total_in);
417 487
418 } else if (s->mode == 'r' && s->z_err == Z_STREAM_END) {
419
420 /* slide CRC and original size if they are at the end of inbuf */
421 if ((n = s->stream.avail_in) < 8 && !s->z_eof) {
422 Byte *p = s->inbuf;
423 Bytef *q = s->stream.next_in;
424 while (n--) { *p++ = *q++; };
425
426 n = s->stream.avail_in;
427 n += fread(p, 1, 8, s->file);
428 s->stream.next_in = s->inbuf;
429 }
430 /* check CRC and original size */
431 if (n < 8 ||
432 getLong(s->stream.next_in) != s->crc ||
433 getLong(s->stream.next_in + 4) != s->stream.total_out) {
434
435 s->z_err = Z_DATA_ERROR;
436 }
437 } 488 }
438 return destroy(file); 489 return destroy(file);
439} 490}
@@ -454,14 +505,14 @@ char* gzerror (file, errnum)
454 505
455 if (s == NULL) { 506 if (s == NULL) {
456 *errnum = Z_STREAM_ERROR; 507 *errnum = Z_STREAM_ERROR;
457 return z_errmsg[1-Z_STREAM_ERROR]; 508 return ERR_MSG(Z_STREAM_ERROR);
458 } 509 }
459 *errnum = s->z_err; 510 *errnum = s->z_err;
460 if (*errnum == Z_OK) return ""; 511 if (*errnum == Z_OK) return (char*)"";
461 512
462 m = *errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg; 513 m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
463 514
464 if (m == NULL || *m == '\0') m = z_errmsg[1-s->z_err]; 515 if (m == NULL || *m == '\0') m = ERR_MSG(s->z_err);
465 516
466 TRYFREE(s->msg); 517 TRYFREE(s->msg);
467 s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3); 518 s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
diff --git a/infblock.c b/infblock.c
index 9c6e6a8..733e983 100644
--- a/infblock.c
+++ b/infblock.c
@@ -1,5 +1,5 @@
1/* infblock.c -- interpret and process block types to last block 1/* infblock.c -- interpret and process block types to last block
2 * Copyright (C) 1995 Mark Adler 2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -81,7 +81,7 @@ uLongf *c;
81 s->bitb = 0; 81 s->bitb = 0;
82 s->read = s->write = s->window; 82 s->read = s->write = s->window;
83 if (s->checkfn != Z_NULL) 83 if (s->checkfn != Z_NULL)
84 s->check = (*s->checkfn)(0L, Z_NULL, 0); 84 z->adler = s->check = (*s->checkfn)(0L, Z_NULL, 0);
85 Trace((stderr, "inflate: blocks reset\n")); 85 Trace((stderr, "inflate: blocks reset\n"));
86} 86}
87 87
@@ -110,6 +110,9 @@ uInt w;
110} 110}
111 111
112 112
113#ifdef DEBUG
114 extern uInt inflate_hufts;
115#endif
113int inflate_blocks(s, z, r) 116int inflate_blocks(s, z, r)
114inflate_blocks_statef *s; 117inflate_blocks_statef *s;
115z_stream *z; 118z_stream *z;
@@ -172,7 +175,7 @@ int r;
172 case 3: /* illegal */ 175 case 3: /* illegal */
173 DUMPBITS(3) 176 DUMPBITS(3)
174 s->mode = BAD; 177 s->mode = BAD;
175 z->msg = "invalid block type"; 178 z->msg = (char*)"invalid block type";
176 r = Z_DATA_ERROR; 179 r = Z_DATA_ERROR;
177 LEAVE 180 LEAVE
178 } 181 }
@@ -182,14 +185,14 @@ int r;
182 if ((((~b) >> 16) & 0xffff) != (b & 0xffff)) 185 if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
183 { 186 {
184 s->mode = BAD; 187 s->mode = BAD;
185 z->msg = "invalid stored block lengths"; 188 z->msg = (char*)"invalid stored block lengths";
186 r = Z_DATA_ERROR; 189 r = Z_DATA_ERROR;
187 LEAVE 190 LEAVE
188 } 191 }
189 s->sub.left = (uInt)b & 0xffff; 192 s->sub.left = (uInt)b & 0xffff;
190 b = k = 0; /* dump bits */ 193 b = k = 0; /* dump bits */
191 Tracev((stderr, "inflate: stored length %u\n", s->sub.left)); 194 Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
192 s->mode = s->sub.left ? STORED : TYPE; 195 s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
193 break; 196 break;
194 case STORED: 197 case STORED:
195 if (n == 0) 198 if (n == 0)
@@ -215,7 +218,7 @@ int r;
215 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) 218 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
216 { 219 {
217 s->mode = BAD; 220 s->mode = BAD;
218 z->msg = "too many length or distance symbols"; 221 z->msg = (char*)"too many length or distance symbols";
219 r = Z_DATA_ERROR; 222 r = Z_DATA_ERROR;
220 LEAVE 223 LEAVE
221 } 224 }
@@ -285,7 +288,7 @@ int r;
285 (c == 16 && i < 1)) 288 (c == 16 && i < 1))
286 { 289 {
287 s->mode = BAD; 290 s->mode = BAD;
288 z->msg = "invalid bit length repeat"; 291 z->msg = (char*)"invalid bit length repeat";
289 r = Z_DATA_ERROR; 292 r = Z_DATA_ERROR;
290 LEAVE 293 LEAVE
291 } 294 }
@@ -306,6 +309,9 @@ int r;
306 bl = 9; /* must be <= 9 for lookahead assumptions */ 309 bl = 9; /* must be <= 9 for lookahead assumptions */
307 bd = 6; /* must be <= 9 for lookahead assumptions */ 310 bd = 6; /* must be <= 9 for lookahead assumptions */
308 t = s->sub.trees.table; 311 t = s->sub.trees.table;
312#ifdef DEBUG
313 inflate_hufts = 0;
314#endif
309 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), 315 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
310 s->sub.trees.blens, &bl, &bd, &tl, &td, z); 316 s->sub.trees.blens, &bl, &bd, &tl, &td, z);
311 if (t != Z_OK) 317 if (t != Z_OK)
@@ -315,7 +321,8 @@ int r;
315 r = t; 321 r = t;
316 LEAVE 322 LEAVE
317 } 323 }
318 Tracev((stderr, "inflate: trees ok\n")); 324 Tracev((stderr, "inflate: trees ok, %d * %d bytes used\n",
325 inflate_hufts, sizeof(inflate_huft)));
319 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL) 326 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
320 { 327 {
321 inflate_trees_free(td, z); 328 inflate_trees_free(td, z);
@@ -383,3 +390,13 @@ uLongf *c;
383 Trace((stderr, "inflate: blocks freed\n")); 390 Trace((stderr, "inflate: blocks freed\n"));
384 return Z_OK; 391 return Z_OK;
385} 392}
393
394
395void inflate_set_dictionary(s, d, n)
396inflate_blocks_statef *s;
397const Bytef *d;
398uInt n;
399{
400 zmemcpy((charf *)s->window, d, n);
401 s->read = s->write = s->window + n;
402}
diff --git a/infblock.h b/infblock.h
index aba6e24..ee2c063 100644
--- a/infblock.h
+++ b/infblock.h
@@ -1,5 +1,5 @@
1/* infblock.h -- header to use infblock.c 1/* infblock.h -- header to use infblock.c
2 * Copyright (C) 1995 Mark Adler 2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -30,3 +30,8 @@ extern int inflate_blocks_free OF((
30 inflate_blocks_statef *, 30 inflate_blocks_statef *,
31 z_stream *, 31 z_stream *,
32 uLongf *)); /* check value on output */ 32 uLongf *)); /* check value on output */
33
34extern void inflate_set_dictionary OF((
35 inflate_blocks_statef *s,
36 const Bytef *d, /* dictionary */
37 uInt n)); /* dictionary length */
diff --git a/infcodes.c b/infcodes.c
index a5fa8cc..6314e80 100644
--- a/infcodes.c
+++ b/infcodes.c
@@ -1,5 +1,5 @@
1/* infcodes.c -- process literals and length/distance pairs 1/* infcodes.c -- process literals and length/distance pairs
2 * Copyright (C) 1995 Mark Adler 2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -58,7 +58,8 @@ struct inflate_codes_state {
58 58
59inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z) 59inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
60uInt bl, bd; 60uInt bl, bd;
61inflate_huft *tl, *td; 61inflate_huft *tl;
62inflate_huft *td; /* need separate declaration for Borland C++ */
62z_stream *z; 63z_stream *z;
63{ 64{
64 inflate_codes_statef *c; 65 inflate_codes_statef *c;
@@ -152,7 +153,7 @@ int r;
152 break; 153 break;
153 } 154 }
154 c->mode = BADCODE; /* invalid code */ 155 c->mode = BADCODE; /* invalid code */
155 z->msg = "invalid literal/length code"; 156 z->msg = (char*)"invalid literal/length code";
156 r = Z_DATA_ERROR; 157 r = Z_DATA_ERROR;
157 LEAVE 158 LEAVE
158 case LENEXT: /* i: getting length extra (have base) */ 159 case LENEXT: /* i: getting length extra (have base) */
@@ -184,7 +185,7 @@ int r;
184 break; 185 break;
185 } 186 }
186 c->mode = BADCODE; /* invalid code */ 187 c->mode = BADCODE; /* invalid code */
187 z->msg = "invalid distance code"; 188 z->msg = (char*)"invalid distance code";
188 r = Z_DATA_ERROR; 189 r = Z_DATA_ERROR;
189 LEAVE 190 LEAVE
190 case DISTEXT: /* i: getting distance extra */ 191 case DISTEXT: /* i: getting distance extra */
diff --git a/infcodes.h b/infcodes.h
index a772e42..8671efa 100644
--- a/infcodes.h
+++ b/infcodes.h
@@ -1,5 +1,5 @@
1/* infcodes.h -- header to use infcodes.c 1/* infcodes.h -- header to use infcodes.c
2 * Copyright (C) 1995 Mark Adler 2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
diff --git a/inffast.c b/inffast.c
index 64c6289..209c0ee 100644
--- a/inffast.c
+++ b/inffast.c
@@ -1,5 +1,5 @@
1/* inffast.c -- process literals and length/distance pairs fast 1/* inffast.c -- process literals and length/distance pairs fast
2 * Copyright (C) 1995 Mark Adler 2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -29,7 +29,8 @@ struct inflate_codes_state {int dummy;}; /* for buggy compilers */
29 29
30int inflate_fast(bl, bd, tl, td, s, z) 30int inflate_fast(bl, bd, tl, td, s, z)
31uInt bl, bd; 31uInt bl, bd;
32inflate_huft *tl, *td; 32inflate_huft *tl;
33inflate_huft *td; /* need separate declaration for Borland C++ */
33inflate_blocks_statef *s; 34inflate_blocks_statef *s;
34z_stream *z; 35z_stream *z;
35{ 36{
@@ -122,7 +123,7 @@ z_stream *z;
122 e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop; 123 e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop;
123 else 124 else
124 { 125 {
125 z->msg = "invalid distance code"; 126 z->msg = (char*)"invalid distance code";
126 UNGRAB 127 UNGRAB
127 UPDATE 128 UPDATE
128 return Z_DATA_ERROR; 129 return Z_DATA_ERROR;
@@ -152,7 +153,7 @@ z_stream *z;
152 } 153 }
153 else 154 else
154 { 155 {
155 z->msg = "invalid literal/length code"; 156 z->msg = (char*)"invalid literal/length code";
156 UNGRAB 157 UNGRAB
157 UPDATE 158 UPDATE
158 return Z_DATA_ERROR; 159 return Z_DATA_ERROR;
diff --git a/inffast.h b/inffast.h
index ad3cf97..843051f 100644
--- a/inffast.h
+++ b/inffast.h
@@ -1,5 +1,5 @@
1/* inffast.h -- header to use inffast.c 1/* inffast.h -- header to use inffast.c
2 * Copyright (C) 1995 Mark Adler 2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
diff --git a/inflate.c b/inflate.c
index b71f74f..4eed100 100644
--- a/inflate.c
+++ b/inflate.c
@@ -1,5 +1,5 @@
1/* inflate.c -- zlib interface to inflate modules 1/* inflate.c -- zlib interface to inflate modules
2 * Copyright (C) 1995 Mark Adler 2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -15,6 +15,11 @@ struct internal_state {
15 enum { 15 enum {
16 METHOD, /* waiting for method byte */ 16 METHOD, /* waiting for method byte */
17 FLAG, /* waiting for flag byte */ 17 FLAG, /* waiting for flag byte */
18 DICT4, /* four dictionary check bytes to go */
19 DICT3, /* three dictionary check bytes to go */
20 DICT2, /* two dictionary check bytes to go */
21 DICT1, /* one dictionary check byte to go */
22 DICT0, /* waiting for inflateSetDictionary */
18 BLOCKS, /* decompressing blocks */ 23 BLOCKS, /* decompressing blocks */
19 CHECK4, /* four check bytes to go */ 24 CHECK4, /* four check bytes to go */
20 CHECK3, /* three check bytes to go */ 25 CHECK3, /* three check bytes to go */
@@ -75,10 +80,16 @@ z_stream *z;
75} 80}
76 81
77 82
78int inflateInit2(z, w) 83int inflateInit2_(z, w, version, stream_size)
79z_stream *z; 84z_stream *z;
80int w; 85int w;
86const char *version;
87int stream_size;
81{ 88{
89 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
90 stream_size != sizeof(z_stream))
91 return Z_VERSION_ERROR;
92
82 /* initialize state */ 93 /* initialize state */
83 if (z == Z_NULL) 94 if (z == Z_NULL)
84 return Z_STREAM_ERROR; 95 return Z_STREAM_ERROR;
@@ -112,7 +123,7 @@ int w;
112 123
113 /* create inflate_blocks state */ 124 /* create inflate_blocks state */
114 if ((z->state->blocks = 125 if ((z->state->blocks =
115 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, 1 << w)) 126 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
116 == Z_NULL) 127 == Z_NULL)
117 { 128 {
118 inflateEnd(z); 129 inflateEnd(z);
@@ -126,10 +137,12 @@ int w;
126} 137}
127 138
128 139
129int inflateInit(z) 140int inflateInit_(z, version, stream_size)
130z_stream *z; 141z_stream *z;
142const char *version;
143int stream_size;
131{ 144{
132 return inflateInit2(z, DEF_WBITS); 145 return inflateInit2_(z, DEF_WBITS, version, stream_size);
133} 146}
134 147
135 148
@@ -140,10 +153,10 @@ int inflate(z, f)
140z_stream *z; 153z_stream *z;
141int f; 154int f;
142{ 155{
143 int r = f; /* to avoid warning about unused f */ 156 int r;
144 uInt b; 157 uInt b;
145 158
146 if (z == Z_NULL || z->next_in == Z_NULL) 159 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL || f < 0)
147 return Z_STREAM_ERROR; 160 return Z_STREAM_ERROR;
148 r = Z_BUF_ERROR; 161 r = Z_BUF_ERROR;
149 while (1) switch (z->state->mode) 162 while (1) switch (z->state->mode)
@@ -153,36 +166,58 @@ int f;
153 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED) 166 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
154 { 167 {
155 z->state->mode = BAD; 168 z->state->mode = BAD;
156 z->msg = "unknown compression method"; 169 z->msg = (char*)"unknown compression method";
157 z->state->sub.marker = 5; /* can't try inflateSync */ 170 z->state->sub.marker = 5; /* can't try inflateSync */
158 break; 171 break;
159 } 172 }
160 if ((z->state->sub.method >> 4) + 8 > z->state->wbits) 173 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
161 { 174 {
162 z->state->mode = BAD; 175 z->state->mode = BAD;
163 z->msg = "invalid window size"; 176 z->msg = (char*)"invalid window size";
164 z->state->sub.marker = 5; /* can't try inflateSync */ 177 z->state->sub.marker = 5; /* can't try inflateSync */
165 break; 178 break;
166 } 179 }
167 z->state->mode = FLAG; 180 z->state->mode = FLAG;
168 case FLAG: 181 case FLAG:
169 NEEDBYTE 182 NEEDBYTE
170 if ((b = NEXTBYTE) & 0x20) 183 b = NEXTBYTE;
171 {
172 z->state->mode = BAD;
173 z->msg = "invalid reserved bit";
174 z->state->sub.marker = 5; /* can't try inflateSync */
175 break;
176 }
177 if (((z->state->sub.method << 8) + b) % 31) 184 if (((z->state->sub.method << 8) + b) % 31)
178 { 185 {
179 z->state->mode = BAD; 186 z->state->mode = BAD;
180 z->msg = "incorrect header check"; 187 z->msg = (char*)"incorrect header check";
181 z->state->sub.marker = 5; /* can't try inflateSync */ 188 z->state->sub.marker = 5; /* can't try inflateSync */
182 break; 189 break;
183 } 190 }
184 Trace((stderr, "inflate: zlib header ok\n")); 191 Trace((stderr, "inflate: zlib header ok\n"));
185 z->state->mode = BLOCKS; 192 if (!(b & PRESET_DICT))
193 {
194 z->state->mode = BLOCKS;
195 break;
196 }
197 z->state->mode = DICT4;
198 case DICT4:
199 NEEDBYTE
200 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
201 z->state->mode = DICT3;
202 case DICT3:
203 NEEDBYTE
204 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
205 z->state->mode = DICT2;
206 case DICT2:
207 NEEDBYTE
208 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
209 z->state->mode = DICT1;
210 case DICT1:
211 NEEDBYTE
212 z->state->sub.check.need += (uLong)NEXTBYTE;
213 z->adler = z->state->sub.check.need;
214 z->state->mode = DICT0;
215 return Z_NEED_DICT;
216 case DICT0:
217 z->state->mode = BAD;
218 z->msg = (char*)"need dictionary";
219 z->state->sub.marker = 0; /* can try inflateSync */
220 return Z_STREAM_ERROR;
186 case BLOCKS: 221 case BLOCKS:
187 r = inflate_blocks(z->state->blocks, z, r); 222 r = inflate_blocks(z->state->blocks, z, r);
188 if (r == Z_DATA_ERROR) 223 if (r == Z_DATA_ERROR)
@@ -220,7 +255,7 @@ int f;
220 if (z->state->sub.check.was != z->state->sub.check.need) 255 if (z->state->sub.check.was != z->state->sub.check.need)
221 { 256 {
222 z->state->mode = BAD; 257 z->state->mode = BAD;
223 z->msg = "incorrect data check"; 258 z->msg = (char*)"incorrect data check";
224 z->state->sub.marker = 5; /* can't try inflateSync */ 259 z->state->sub.marker = 5; /* can't try inflateSync */
225 break; 260 break;
226 } 261 }
@@ -236,6 +271,29 @@ int f;
236} 271}
237 272
238 273
274int inflateSetDictionary(z, dictionary, dictLength)
275z_stream *z;
276const Bytef *dictionary;
277uInt dictLength;
278{
279 uInt length = dictLength;
280
281 if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
282 return Z_STREAM_ERROR;
283 if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
284 z->adler = 1L;
285
286 if (length >= ((uInt)1<<z->state->wbits))
287 {
288 length = (1<<z->state->wbits)-1;
289 dictionary += dictLength - length;
290 }
291 inflate_set_dictionary(z->state->blocks, dictionary, length);
292 z->state->mode = BLOCKS;
293 return Z_OK;
294}
295
296
239int inflateSync(z) 297int inflateSync(z)
240z_stream *z; 298z_stream *z;
241{ 299{
diff --git a/inftrees.c b/inftrees.c
index e4d7bdf..3bf39a6 100644
--- a/inftrees.c
+++ b/inftrees.c
@@ -1,12 +1,12 @@
1/* inftrees.c -- generate Huffman trees for efficient decoding 1/* inftrees.c -- generate Huffman trees for efficient decoding
2 * Copyright (C) 1995 Mark Adler 2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6#include "zutil.h" 6#include "zutil.h"
7#include "inftrees.h" 7#include "inftrees.h"
8 8
9char inflate_copyright[] = " inflate 1.0 Copyright 1995 Mark Adler "; 9char inflate_copyright[] = " inflate 1.0.1 Copyright 1995-1996 Mark Adler ";
10/* 10/*
11 If you use the zlib library in a product, an acknowledgment is welcome 11 If you use the zlib library in a product, an acknowledgment is welcome
12 in the documentation of your product. If for some reason you cannot 12 in the documentation of your product. If for some reason you cannot
@@ -38,18 +38,18 @@ local voidpf falloc OF((
38 uInt)); /* size of item */ 38 uInt)); /* size of item */
39 39
40/* Tables for deflate from PKZIP's appnote.txt. */ 40/* Tables for deflate from PKZIP's appnote.txt. */
41local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */ 41local uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
42 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 42 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
43 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; 43 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
44 /* actually lengths - 2; also see note #13 above about 258 */ 44 /* actually lengths - 2; also see note #13 above about 258 */
45local uInt cplext[] = { /* Extra bits for literal codes 257..285 */ 45local uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
46 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 46 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
47 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 192, 192}; /* 192==invalid */ 47 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 192, 192}; /* 192==invalid */
48local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */ 48local uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
49 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 49 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
50 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 50 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
51 8193, 12289, 16385, 24577}; 51 8193, 12289, 16385, 24577};
52local uInt cpdext[] = { /* Extra bits for distance codes */ 52local uInt cpdext[30] = { /* Extra bits for distance codes */
53 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 53 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
54 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 54 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
55 12, 12, 13, 13}; 55 12, 12, 13, 13};
@@ -215,7 +215,8 @@ z_stream *zs; /* for zalloc function */
215 w += l; /* previous table always l bits */ 215 w += l; /* previous table always l bits */
216 216
217 /* compute minimum size table less than or equal to l bits */ 217 /* compute minimum size table less than or equal to l bits */
218 z = (z = g - w) > (uInt)l ? l : z; /* table size upper limit */ 218 z = g - w;
219 z = z > (uInt)l ? l : z; /* table size upper limit */
219 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */ 220 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
220 { /* too few codes for k-w bit table */ 221 { /* too few codes for k-w bit table */
221 f -= a + 1; /* deduct codes from patterns left */ 222 f -= a + 1; /* deduct codes from patterns left */
@@ -268,7 +269,7 @@ z_stream *zs; /* for zalloc function */
268 } 269 }
269 else 270 else
270 { 271 {
271 r.exop = (Byte)e[*p - s] + 16 + 64; /* non-simple--look up in lists */ 272 r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
272 r.base = d[*p++ - s]; 273 r.base = d[*p++ - s];
273 } 274 }
274 275
@@ -307,11 +308,11 @@ z_stream *z; /* for zfree function */
307 308
308 r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z); 309 r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z);
309 if (r == Z_DATA_ERROR) 310 if (r == Z_DATA_ERROR)
310 z->msg = "oversubscribed dynamic bit lengths tree"; 311 z->msg = (char*)"oversubscribed dynamic bit lengths tree";
311 else if (r == Z_BUF_ERROR) 312 else if (r == Z_BUF_ERROR)
312 { 313 {
313 inflate_trees_free(*tb, z); 314 inflate_trees_free(*tb, z);
314 z->msg = "incomplete dynamic bit lengths tree"; 315 z->msg = (char*)"incomplete dynamic bit lengths tree";
315 r = Z_DATA_ERROR; 316 r = Z_DATA_ERROR;
316 } 317 }
317 return r; 318 return r;
@@ -334,11 +335,11 @@ z_stream *z; /* for zfree function */
334 if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK) 335 if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK)
335 { 336 {
336 if (r == Z_DATA_ERROR) 337 if (r == Z_DATA_ERROR)
337 z->msg = "oversubscribed literal/length tree"; 338 z->msg = (char*)"oversubscribed literal/length tree";
338 else if (r == Z_BUF_ERROR) 339 else if (r == Z_BUF_ERROR)
339 { 340 {
340 inflate_trees_free(*tl, z); 341 inflate_trees_free(*tl, z);
341 z->msg = "incomplete literal/length tree"; 342 z->msg = (char*)"incomplete literal/length tree";
342 r = Z_DATA_ERROR; 343 r = Z_DATA_ERROR;
343 } 344 }
344 return r; 345 return r;
@@ -348,14 +349,14 @@ z_stream *z; /* for zfree function */
348 if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK) 349 if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK)
349 { 350 {
350 if (r == Z_DATA_ERROR) 351 if (r == Z_DATA_ERROR)
351 z->msg = "oversubscribed literal/length tree"; 352 z->msg = (char*)"oversubscribed literal/length tree";
352 else if (r == Z_BUF_ERROR) { 353 else if (r == Z_BUF_ERROR) {
353#ifdef PKZIP_BUG_WORKAROUND 354#ifdef PKZIP_BUG_WORKAROUND
354 r = Z_OK; 355 r = Z_OK;
355 } 356 }
356#else 357#else
357 inflate_trees_free(*td, z); 358 inflate_trees_free(*td, z);
358 z->msg = "incomplete literal/length tree"; 359 z->msg = (char*)"incomplete literal/length tree";
359 r = Z_DATA_ERROR; 360 r = Z_DATA_ERROR;
360 } 361 }
361 inflate_trees_free(*tl, z); 362 inflate_trees_free(*tl, z);
@@ -385,7 +386,7 @@ uInt s; /* size of item */
385{ 386{
386 Assert(s == sizeof(inflate_huft) && n <= *(intf *)q, 387 Assert(s == sizeof(inflate_huft) && n <= *(intf *)q,
387 "inflate_trees falloc overflow"); 388 "inflate_trees falloc overflow");
388 *(intf *)q -= n; 389 *(intf *)q -= n+s-s; /* s-s to avoid warning */
389 return (voidpf)(fixed_mem + *(intf *)q); 390 return (voidpf)(fixed_mem + *(intf *)q);
390} 391}
391 392
diff --git a/inftrees.h b/inftrees.h
index 9eaaa66..0f0ca2b 100644
--- a/inftrees.h
+++ b/inftrees.h
@@ -1,5 +1,5 @@
1/* inftrees.h -- header to use inftrees.c 1/* inftrees.h -- header to use inftrees.c
2 * Copyright (C) 1995 Mark Adler 2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
diff --git a/infutil.c b/infutil.c
index 96db3a4..7099b6d 100644
--- a/infutil.c
+++ b/infutil.c
@@ -1,5 +1,5 @@
1/* inflate_util.c -- data and routines common to blocks and codes 1/* inflate_util.c -- data and routines common to blocks and codes
2 * Copyright (C) 1995 Mark Adler 2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -12,7 +12,7 @@
12struct inflate_codes_state {int dummy;}; /* for buggy compilers */ 12struct inflate_codes_state {int dummy;}; /* for buggy compilers */
13 13
14/* And'ing with mask[n] masks the lower n bits */ 14/* And'ing with mask[n] masks the lower n bits */
15uInt inflate_mask[] = { 15uInt inflate_mask[17] = {
16 0x0000, 16 0x0000,
17 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, 17 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
18 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff 18 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
@@ -43,7 +43,7 @@ int r;
43 43
44 /* update check information */ 44 /* update check information */
45 if (s->checkfn != Z_NULL) 45 if (s->checkfn != Z_NULL)
46 s->check = (*s->checkfn)(s->check, q, n); 46 z->adler = s->check = (*s->checkfn)(s->check, q, n);
47 47
48 /* copy as far as end of window */ 48 /* copy as far as end of window */
49 zmemcpy(p, q, n); 49 zmemcpy(p, q, n);
@@ -69,7 +69,7 @@ int r;
69 69
70 /* update check information */ 70 /* update check information */
71 if (s->checkfn != Z_NULL) 71 if (s->checkfn != Z_NULL)
72 s->check = (*s->checkfn)(s->check, q, n); 72 z->adler = s->check = (*s->checkfn)(s->check, q, n);
73 73
74 /* copy */ 74 /* copy */
75 zmemcpy(p, q, n); 75 zmemcpy(p, q, n);
diff --git a/infutil.h b/infutil.h
index c523155..ff7ac15 100644
--- a/infutil.h
+++ b/infutil.h
@@ -1,5 +1,5 @@
1/* infutil.h -- types and macros common to blocks and codes 1/* infutil.h -- types and macros common to blocks and codes
2 * Copyright (C) 1995 Mark Adler 2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -8,11 +8,10 @@
8 subject to change. Applications should only use zlib.h. 8 subject to change. Applications should only use zlib.h.
9 */ 9 */
10 10
11/* inflate blocks semi-private state */ 11#ifndef _INFUTIL_H
12struct inflate_blocks_state { 12#define _INFUTIL_H
13 13
14 /* mode */ 14typedef enum {
15 enum {
16 TYPE, /* get type bits (3, including end bit) */ 15 TYPE, /* get type bits (3, including end bit) */
17 LENS, /* get lengths for stored */ 16 LENS, /* get lengths for stored */
18 STORED, /* processing stored block */ 17 STORED, /* processing stored block */
@@ -23,7 +22,13 @@ struct inflate_blocks_state {
23 DRY, /* output remaining window bytes */ 22 DRY, /* output remaining window bytes */
24 DONE, /* finished last block, done */ 23 DONE, /* finished last block, done */
25 BAD} /* got a data error--stuck here */ 24 BAD} /* got a data error--stuck here */
26 mode; /* current inflate_block mode */ 25inflate_block_mode;
26
27/* inflate blocks semi-private state */
28struct inflate_blocks_state {
29
30 /* mode */
31 inflate_block_mode mode; /* current inflate_block mode */
27 32
28 /* mode dependent information */ 33 /* mode dependent information */
29 union { 34 union {
@@ -36,7 +41,8 @@ struct inflate_blocks_state {
36 inflate_huft *tb; /* bit length decoding tree */ 41 inflate_huft *tb; /* bit length decoding tree */
37 } trees; /* if DTREE, decoding info for trees */ 42 } trees; /* if DTREE, decoding info for trees */
38 struct { 43 struct {
39 inflate_huft *tl, *td; /* trees to free */ 44 inflate_huft *tl;
45 inflate_huft *td; /* trees to free */
40 inflate_codes_statef 46 inflate_codes_statef
41 *codes; 47 *codes;
42 } decode; /* if CODES, current state */ 48 } decode; /* if CODES, current state */
@@ -71,16 +77,16 @@ struct inflate_blocks_state {
71#define DUMPBITS(j) {b>>=(j);k-=(j);} 77#define DUMPBITS(j) {b>>=(j);k-=(j);}
72/* output bytes */ 78/* output bytes */
73#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q) 79#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
74#define LOADOUT {q=s->write;m=WAVAIL;} 80#define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
75#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=WAVAIL;}} 81#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
76#define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT} 82#define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
77#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;} 83#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
78#define OUTBYTE(a) {*q++=(Byte)(a);m--;} 84#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
79/* load local pointers */ 85/* load local pointers */
80#define LOAD {LOADIN LOADOUT} 86#define LOAD {LOADIN LOADOUT}
81 87
82/* masks for lower bits */ 88/* masks for lower bits (size given to avoid silly warnings with Visual C++) */
83extern uInt inflate_mask[]; 89extern uInt inflate_mask[17];
84 90
85/* copy as much as possible from the sliding window to the output area */ 91/* copy as much as possible from the sliding window to the output area */
86extern int inflate_flush OF(( 92extern int inflate_flush OF((
@@ -89,3 +95,5 @@ extern int inflate_flush OF((
89 int)); 95 int));
90 96
91struct internal_state {int dummy;}; /* for buggy compilers */ 97struct internal_state {int dummy;}; /* for buggy compilers */
98
99#endif
diff --git a/minigzip.c b/minigzip.c
index fa0a188..eee52a5 100644
--- a/minigzip.c
+++ b/minigzip.c
@@ -1,5 +1,5 @@
1/* minigzip.c -- simulate gzip using the zlib compression library 1/* minigzip.c -- simulate gzip using the zlib compression library
2 * Copyright (C) 1995 Jean-loup Gailly. 2 * Copyright (C) 1995-1996 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -13,7 +13,7 @@
13 * or in pipe mode. 13 * or in pipe mode.
14 */ 14 */
15 15
16/* $Id: minigzip.c,v 1.5 1995/05/03 17:27:11 jloup Exp $ */ 16/* $Id: minigzip.c,v 1.8 1996/01/30 21:59:23 me Exp $ */
17 17
18#include <stdio.h> 18#include <stdio.h>
19#include "zlib.h" 19#include "zlib.h"
@@ -24,7 +24,7 @@
24#else 24#else
25 extern void exit OF((int)); 25 extern void exit OF((int));
26#endif 26#endif
27extern int unlink OF((const char *)); 27
28 28
29#if defined(MSDOS) || defined(OS2) || defined(WIN32) 29#if defined(MSDOS) || defined(OS2) || defined(WIN32)
30# include <fcntl.h> 30# include <fcntl.h>
@@ -35,12 +35,15 @@ extern int unlink OF((const char *));
35#endif 35#endif
36 36
37#ifdef VMS 37#ifdef VMS
38# define unlink delete
38# define GZ_SUFFIX "-gz" 39# define GZ_SUFFIX "-gz"
39#else 40#else
40# define GZ_SUFFIX ".gz" 41# define GZ_SUFFIX ".gz"
41#endif 42#endif
42#define SUFFIX_LEN sizeof(GZ_SUFFIX) 43#define SUFFIX_LEN sizeof(GZ_SUFFIX)
43 44
45extern int unlink OF((const char *));
46
44#define BUFLEN 4096 47#define BUFLEN 4096
45#define MAX_NAME_LEN 1024 48#define MAX_NAME_LEN 1024
46 49
@@ -52,7 +55,7 @@ extern int unlink OF((const char *));
52 55
53char *prog; 56char *prog;
54 57
55void error OF((char *msg)); 58void error OF((const char *msg));
56void gz_compress OF((FILE *in, gzFile out)); 59void gz_compress OF((FILE *in, gzFile out));
57void gz_uncompress OF((gzFile in, FILE *out)); 60void gz_uncompress OF((gzFile in, FILE *out));
58void file_compress OF((char *file)); 61void file_compress OF((char *file));
@@ -63,7 +66,7 @@ int main OF((int argc, char *argv[]));
63 * Display error message and exit 66 * Display error message and exit
64 */ 67 */
65void error(msg) 68void error(msg)
66 char *msg; 69 const char *msg;
67{ 70{
68 fprintf(stderr, "%s: %s\n", prog, msg); 71 fprintf(stderr, "%s: %s\n", prog, msg);
69 exit(1); 72 exit(1);
@@ -88,7 +91,7 @@ void gz_compress(in, out)
88 } 91 }
89 if (len == 0) break; 92 if (len == 0) break;
90 93
91 if (gzwrite(out, buf, len) != len) error(gzerror(out, &err)); 94 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
92 } 95 }
93 fclose(in); 96 fclose(in);
94 if (gzclose(out) != Z_OK) error("failed gzclose"); 97 if (gzclose(out) != Z_OK) error("failed gzclose");
@@ -110,7 +113,9 @@ void gz_uncompress(in, out)
110 if (len < 0) error (gzerror(in, &err)); 113 if (len < 0) error (gzerror(in, &err));
111 if (len == 0) break; 114 if (len == 0) break;
112 115
113 if (fwrite(buf, 1, len, out) != (uInt)len) error("failed fwrite"); 116 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
117 error("failed fwrite");
118 }
114 } 119 }
115 if (fclose(out)) error("failed fclose"); 120 if (fclose(out)) error("failed fclose");
116 121
diff --git a/trees.c b/trees.c
index 0ba0bad..fcfe68b 100644
--- a/trees.c
+++ b/trees.c
@@ -1,5 +1,5 @@
1/* trees.c -- output deflated data using Huffman coding 1/* trees.c -- output deflated data using Huffman coding
2 * Copyright (C) 1995 Jean-loup Gailly 2 * Copyright (C) 1995-1996 Jean-loup Gailly
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -83,7 +83,7 @@ local uch bl_order[BL_CODES]
83local ct_data static_ltree[L_CODES+2]; 83local ct_data static_ltree[L_CODES+2];
84/* The static literal tree. Since the bit lengths are imposed, there is no 84/* The static literal tree. Since the bit lengths are imposed, there is no
85 * need for the L_CODES extra codes used during heap construction. However 85 * need for the L_CODES extra codes used during heap construction. However
86 * The codes 286 and 287 are needed to build a canonical tree (see tr_init 86 * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
87 * below). 87 * below).
88 */ 88 */
89 89
@@ -232,7 +232,7 @@ local void send_bits(s, value, length)
232 */ 232 */
233local void tr_static_init() 233local void tr_static_init()
234{ 234{
235 static static_init_done = 0; 235 static int static_init_done = 0;
236 int n; /* iterates over tree elements */ 236 int n; /* iterates over tree elements */
237 int bits; /* bit counter */ 237 int bits; /* bit counter */
238 int length; /* length value */ 238 int length; /* length value */
@@ -292,7 +292,7 @@ local void tr_static_init()
292 /* The static distance tree is trivial: */ 292 /* The static distance tree is trivial: */
293 for (n = 0; n < D_CODES; n++) { 293 for (n = 0; n < D_CODES; n++) {
294 static_dtree[n].Len = 5; 294 static_dtree[n].Len = 5;
295 static_dtree[n].Code = bi_reverse(n, 5); 295 static_dtree[n].Code = bi_reverse((unsigned)n, 5);
296 } 296 }
297 static_init_done = 1; 297 static_init_done = 1;
298} 298}
@@ -300,7 +300,7 @@ local void tr_static_init()
300/* =========================================================================== 300/* ===========================================================================
301 * Initialize the tree data structures for a new zlib stream. 301 * Initialize the tree data structures for a new zlib stream.
302 */ 302 */
303void tr_init(s) 303void _tr_init(s)
304 deflate_state *s; 304 deflate_state *s;
305{ 305{
306 tr_static_init(); 306 tr_static_init();
@@ -785,14 +785,14 @@ local void send_all_trees(s, lcodes, dcodes, blcodes)
785/* =========================================================================== 785/* ===========================================================================
786 * Send a stored block 786 * Send a stored block
787 */ 787 */
788void tr_stored_block(s, buf, stored_len, eof) 788void _tr_stored_block(s, buf, stored_len, eof)
789 deflate_state *s; 789 deflate_state *s;
790 charf *buf; /* input block */ 790 charf *buf; /* input block */
791 ulg stored_len; /* length of input block */ 791 ulg stored_len; /* length of input block */
792 int eof; /* true if this is the last block for a file */ 792 int eof; /* true if this is the last block for a file */
793{ 793{
794 send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */ 794 send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
795 s->compressed_len = (s->compressed_len + 3 + 7) & ~7L; 795 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
796 s->compressed_len += (stored_len + 4) << 3; 796 s->compressed_len += (stored_len + 4) << 3;
797 797
798 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */ 798 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
@@ -809,7 +809,7 @@ void tr_stored_block(s, buf, stored_len, eof)
809 * To simplify the code, we assume the worst case of last real code encoded 809 * To simplify the code, we assume the worst case of last real code encoded
810 * on one bit only. 810 * on one bit only.
811 */ 811 */
812void tr_align(s) 812void _tr_align(s)
813 deflate_state *s; 813 deflate_state *s;
814{ 814{
815 send_bits(s, STATIC_TREES<<1, 3); 815 send_bits(s, STATIC_TREES<<1, 3);
@@ -835,20 +835,20 @@ void tr_align(s)
835 * trees or store, and output the encoded block to the zip file. This function 835 * trees or store, and output the encoded block to the zip file. This function
836 * returns the total compressed length for the file so far. 836 * returns the total compressed length for the file so far.
837 */ 837 */
838ulg tr_flush_block(s, buf, stored_len, eof) 838ulg _tr_flush_block(s, buf, stored_len, eof)
839 deflate_state *s; 839 deflate_state *s;
840 charf *buf; /* input block, or NULL if too old */ 840 charf *buf; /* input block, or NULL if too old */
841 ulg stored_len; /* length of input block */ 841 ulg stored_len; /* length of input block */
842 int eof; /* true if this is the last block for a file */ 842 int eof; /* true if this is the last block for a file */
843{ 843{
844 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ 844 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
845 int max_blindex; /* index of last bit length code of non zero freq */ 845 int max_blindex = 0; /* index of last bit length code of non zero freq */
846 846
847 /* Build the Huffman trees unless a stored block is forced */ 847 /* Build the Huffman trees unless a stored block is forced */
848 if (s->level > 0) { 848 if (s->level > 0) {
849 849
850 /* Check if the file is ascii or binary */ 850 /* Check if the file is ascii or binary */
851 if (s->data_type == UNKNOWN) set_data_type(s); 851 if (s->data_type == Z_UNKNOWN) set_data_type(s);
852 852
853 /* Construct the literal and distance trees */ 853 /* Construct the literal and distance trees */
854 build_tree(s, (tree_desc *)(&(s->l_desc))); 854 build_tree(s, (tree_desc *)(&(s->l_desc)));
@@ -879,7 +879,7 @@ ulg tr_flush_block(s, buf, stored_len, eof)
879 879
880 } else { 880 } else {
881 Assert(buf != (char*)0, "lost buf"); 881 Assert(buf != (char*)0, "lost buf");
882 opt_lenb = stored_len + 5; /* force a stored block */ 882 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
883 } 883 }
884 884
885 /* If compression failed and this is the first and last block, 885 /* If compression failed and this is the first and last block,
@@ -913,7 +913,7 @@ ulg tr_flush_block(s, buf, stored_len, eof)
913 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to 913 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
914 * transform a block into a stored block. 914 * transform a block into a stored block.
915 */ 915 */
916 tr_stored_block(s, buf, stored_len, eof); 916 _tr_stored_block(s, buf, stored_len, eof);
917 917
918#ifdef FORCE_STATIC 918#ifdef FORCE_STATIC
919 } else if (static_lenb >= 0) { /* force static trees */ 919 } else if (static_lenb >= 0) { /* force static trees */
@@ -947,10 +947,10 @@ ulg tr_flush_block(s, buf, stored_len, eof)
947 * Save the match info and tally the frequency counts. Return true if 947 * Save the match info and tally the frequency counts. Return true if
948 * the current block must be flushed. 948 * the current block must be flushed.
949 */ 949 */
950int tr_tally (s, dist, lc) 950int _tr_tally (s, dist, lc)
951 deflate_state *s; 951 deflate_state *s;
952 int dist; /* distance of matched string */ 952 unsigned dist; /* distance of matched string */
953 int lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ 953 unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
954{ 954{
955 s->d_buf[s->last_lit] = (ush)dist; 955 s->d_buf[s->last_lit] = (ush)dist;
956 s->l_buf[s->last_lit++] = (uch)lc; 956 s->l_buf[s->last_lit++] = (uch)lc;
@@ -963,7 +963,7 @@ int tr_tally (s, dist, lc)
963 dist--; /* dist = match distance - 1 */ 963 dist--; /* dist = match distance - 1 */
964 Assert((ush)dist < (ush)MAX_DIST(s) && 964 Assert((ush)dist < (ush)MAX_DIST(s) &&
965 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && 965 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
966 (ush)d_code(dist) < (ush)D_CODES, "tr_tally: bad match"); 966 (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
967 967
968 s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++; 968 s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++;
969 s->dyn_dtree[d_code(dist)].Freq++; 969 s->dyn_dtree[d_code(dist)].Freq++;
@@ -1057,7 +1057,7 @@ local void set_data_type(s)
1057 while (n < 7) bin_freq += s->dyn_ltree[n++].Freq; 1057 while (n < 7) bin_freq += s->dyn_ltree[n++].Freq;
1058 while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq; 1058 while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq;
1059 while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq; 1059 while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq;
1060 s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? BINARY : ASCII); 1060 s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? Z_BINARY : Z_ASCII);
1061} 1061}
1062 1062
1063/* =========================================================================== 1063/* ===========================================================================
diff --git a/uncompr.c b/uncompr.c
index b423696..c3de969 100644
--- a/uncompr.c
+++ b/uncompr.c
@@ -1,9 +1,9 @@
1/* uncompr.c -- decompress a memory buffer 1/* uncompr.c -- decompress a memory buffer
2 * Copyright (C) 1995 Jean-loup Gailly. 2 * Copyright (C) 1995-1996 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: uncompr.c,v 1.5 1995/05/03 17:27:12 jloup Exp $ */ 6/* $Id: uncompr.c,v 1.8 1996/01/30 21:59:26 me Exp $ */
7 7
8#include "zlib.h" 8#include "zlib.h"
9 9
@@ -25,13 +25,13 @@
25int uncompress (dest, destLen, source, sourceLen) 25int uncompress (dest, destLen, source, sourceLen)
26 Bytef *dest; 26 Bytef *dest;
27 uLongf *destLen; 27 uLongf *destLen;
28 Bytef *source; 28 const Bytef *source;
29 uLong sourceLen; 29 uLong sourceLen;
30{ 30{
31 z_stream stream; 31 z_stream stream;
32 int err; 32 int err;
33 33
34 stream.next_in = source; 34 stream.next_in = (Bytef*)source;
35 stream.avail_in = (uInt)sourceLen; 35 stream.avail_in = (uInt)sourceLen;
36 /* Check for source > 64K on 16-bit machine: */ 36 /* Check for source > 64K on 16-bit machine: */
37 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 37 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
diff --git a/zconf.h b/zconf.h
index 3d78742..6ab1e4f 100644
--- a/zconf.h
+++ b/zconf.h
@@ -1,5 +1,5 @@
1/* zconf.h -- configuration of the zlib compression library 1/* zconf.h -- configuration of the zlib compression library
2 * Copyright (C) 1995 Jean-loup Gailly. 2 * Copyright (C) 1995-1996 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -9,11 +9,42 @@
9#define _ZCONF_H 9#define _ZCONF_H
10 10
11/* 11/*
12 The library does not install any signal handler. It is recommended to 12 * If you *really* need a unique prefix for all types and library functions,
13 add at least a handler for SIGSEGV when decompressing; the library checks 13 * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
14 the consistency of the input data whenever possible but may go nuts
15 for some forms of corrupted input.
16 */ 14 */
15#ifdef Z_PREFIX
16# define deflateInit_ z_deflateInit_
17# define deflate z_deflate
18# define deflateEnd z_deflateEnd
19# define inflateInit_ z_inflateInit_
20# define inflate z_inflate
21# define inflateEnd z_inflateEnd
22# define deflateInit2_ z_deflateInit2_
23# define deflateSetDictionary z_deflateSetDictionary
24# define deflateCopy z_deflateCopy
25# define deflateReset z_deflateReset
26# define deflateParams z_deflateParams
27# define inflateInit2_ z_inflateInit2_
28# define inflateSetDictionary z_inflateSetDictionary
29# define inflateSync z_inflateSync
30# define inflateReset z_inflateReset
31# define compress z_compress
32# define uncompress z_uncompress
33# define adler32 z_adler32
34# define crc32 z_crc32
35# define get_crc_table z_get_crc_table
36
37# define Byte z_Byte
38# define uInt z_uInt
39# define uLong z_uLong
40/* # define Bytef z_Bytef */
41# define charf z_charf
42# define intf z_intf
43# define uIntf z_uIntf
44# define uLongf z_uLongf
45# define voidpf z_voidpf
46# define voidp z_voidp
47#endif
17 48
18#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32) 49#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
19# define WIN32 50# define WIN32
@@ -43,12 +74,15 @@
43# define STDC 74# define STDC
44#endif 75#endif
45 76
46#if !defined(STDC) && !defined(const) 77#ifndef STDC
47# define const 78# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
79# define const
80# endif
48#endif 81#endif
49 82
50#ifdef __MWERKS__ /* Metrowerks CodeWarrior declares fileno() in unix.h */ 83#ifdef __MWERKS__ /* Metrowerks CodeWarrior declares fileno() in unix.h */
51# include <unix.h> 84# include <unix.h>
85# define NO_DUMMY_DECL /* buggy compiler merges all .h files incorrectly */
52#endif 86#endif
53 87
54/* Maximum value for memLevel in deflateInit2 */ 88/* Maximum value for memLevel in deflateInit2 */
@@ -95,6 +129,7 @@
95 * just define FAR to be empty. 129 * just define FAR to be empty.
96 */ 130 */
97#if defined(M_I86SM) || defined(M_I86MM) /* MSC small or medium model */ 131#if defined(M_I86SM) || defined(M_I86MM) /* MSC small or medium model */
132# define SMALL_MEDIUM
98# ifdef _MSC_VER 133# ifdef _MSC_VER
99# define FAR __far 134# define FAR __far
100# else 135# else
@@ -102,20 +137,28 @@
102# endif 137# endif
103#endif 138#endif
104#if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__)) 139#if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__))
105# define FAR _far /* completely untested, just a best guess */ 140# define SMALL_MEDIUM
141# define FAR __far
106#endif 142#endif
107#ifndef FAR 143#ifndef FAR
108# define FAR 144# define FAR
109#endif 145#endif
146/* The Watcom compiler defines M_I86SM and __SMALL__ even in 32 bit mode */
147#if defined(__WATCOMC__) && defined(__386__)
148# undef FAR
149# define FAR
150# undef SMALL_MEDIUM
151#endif
110 152
111typedef unsigned char Byte; /* 8 bits */ 153typedef unsigned char Byte; /* 8 bits */
112typedef unsigned int uInt; /* 16 bits or more */ 154typedef unsigned int uInt; /* 16 bits or more */
113typedef unsigned long uLong; /* 32 bits or more */ 155typedef unsigned long uLong; /* 32 bits or more */
114 156
115typedef Byte FAR Bytef; 157/* "typedef Byte FAR Bytef;" doesn't work with Borland C/C++ */
116typedef char FAR charf; 158#define Bytef Byte FAR
117typedef int FAR intf; 159typedef char FAR charf;
118typedef uInt FAR uIntf; 160typedef int FAR intf;
161typedef uInt FAR uIntf;
119typedef uLong FAR uLongf; 162typedef uLong FAR uLongf;
120 163
121#ifdef STDC 164#ifdef STDC
diff --git a/zlib.h b/zlib.h
index a1c48ac..47960c2 100644
--- a/zlib.h
+++ b/zlib.h
@@ -1,5 +1,5 @@
1/* zlib.h -- interface of the 'zlib' general purpose compression library 1/* zlib.h -- interface of the 'zlib' general purpose compression library
2 version 1.0, Jan 14th, 1996. 2 version 1.0.1, May 20th, 1996.
3 3
4 Copyright (C) 1995-1996 Jean-loup Gailly and Mark Adler 4 Copyright (C) 1995-1996 Jean-loup Gailly and Mark Adler
5 5
@@ -32,7 +32,7 @@ extern "C" {
32 32
33#include "zconf.h" 33#include "zconf.h"
34 34
35#define ZLIB_VERSION "1.0" 35#define ZLIB_VERSION "1.0.1"
36 36
37/* 37/*
38 The 'zlib' compression library provides in-memory compression and 38 The 'zlib' compression library provides in-memory compression and
@@ -51,6 +51,11 @@ extern "C" {
51 repeated calls of the compression function. In the latter case, the 51 repeated calls of the compression function. In the latter case, the
52 application must provide more input and/or consume the output 52 application must provide more input and/or consume the output
53 (providing more output space) before each call. 53 (providing more output space) before each call.
54
55 The library does not install any signal handler. It is recommended to
56 add at least a handler for SIGSEGV when decompressing; the library checks
57 the consistency of the input data whenever possible but may go nuts
58 for some forms of corrupted input.
54*/ 59*/
55 60
56typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); 61typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
@@ -74,8 +79,9 @@ typedef struct z_stream_s {
74 free_func zfree; /* used to free the internal state */ 79 free_func zfree; /* used to free the internal state */
75 voidpf opaque; /* private data object passed to zalloc and zfree */ 80 voidpf opaque; /* private data object passed to zalloc and zfree */
76 81
77 Byte data_type; /* best guess about the data type: ascii or binary */ 82 int data_type; /* best guess about the data type: ascii or binary */
78 83 uLong adler; /* adler32 value of the uncompressed data */
84 uLong reserved; /* reserved for future use */
79} z_stream; 85} z_stream;
80 86
81/* 87/*
@@ -118,12 +124,16 @@ typedef struct z_stream_s {
118 124
119#define Z_OK 0 125#define Z_OK 0
120#define Z_STREAM_END 1 126#define Z_STREAM_END 1
127#define Z_NEED_DICT 2
121#define Z_ERRNO (-1) 128#define Z_ERRNO (-1)
122#define Z_STREAM_ERROR (-2) 129#define Z_STREAM_ERROR (-2)
123#define Z_DATA_ERROR (-3) 130#define Z_DATA_ERROR (-3)
124#define Z_MEM_ERROR (-4) 131#define Z_MEM_ERROR (-4)
125#define Z_BUF_ERROR (-5) 132#define Z_BUF_ERROR (-5)
126/* error codes for the compression/decompression functions */ 133#define Z_VERSION_ERROR (-6)
134/* Return codes for the compression/decompression functions. Negative
135 * values are errors, positive values are used for special but normal events.
136 */
127 137
128#define Z_NO_COMPRESSION 0 138#define Z_NO_COMPRESSION 0
129#define Z_BEST_SPEED 1 139#define Z_BEST_SPEED 1
@@ -134,18 +144,19 @@ typedef struct z_stream_s {
134#define Z_FILTERED 1 144#define Z_FILTERED 1
135#define Z_HUFFMAN_ONLY 2 145#define Z_HUFFMAN_ONLY 2
136#define Z_DEFAULT_STRATEGY 0 146#define Z_DEFAULT_STRATEGY 0
147/* compression strategy; see deflateInit2() below for details */
137 148
138#define Z_BINARY 0 149#define Z_BINARY 0
139#define Z_ASCII 1 150#define Z_ASCII 1
140#define Z_UNKNOWN 2 151#define Z_UNKNOWN 2
141/* Used to set the data_type field */ 152/* Possible values of the data_type field */
142 153
143#define Z_DEFLATED 8 154#define Z_DEFLATED 8
144/* The deflate compression method (the only one supported in this version) */ 155/* The deflate compression method (the only one supported in this version) */
145 156
146#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ 157#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
147 158
148extern char *zlib_version; 159extern const char *zlib_version;
149/* The application can compare zlib_version and ZLIB_VERSION for consistency. 160/* The application can compare zlib_version and ZLIB_VERSION for consistency.
150 If the first character differs, the library code actually used is 161 If the first character differs, the library code actually used is
151 not compatible with the zlib.h header file used by the application. 162 not compatible with the zlib.h header file used by the application.
@@ -153,8 +164,9 @@ extern char *zlib_version;
153 164
154 /* basic functions */ 165 /* basic functions */
155 166
156extern int deflateInit OF((z_stream *strm, int level));
157/* 167/*
168extern int deflateInit OF((z_stream *strm, int level));
169
158 Initializes the internal stream state for compression. The fields 170 Initializes the internal stream state for compression. The fields
159 zalloc, zfree and opaque must be initialized before by the caller. 171 zalloc, zfree and opaque must be initialized before by the caller.
160 If zalloc and zfree are set to Z_NULL, deflateInit updates them to 172 If zalloc and zfree are set to Z_NULL, deflateInit updates them to
@@ -167,7 +179,9 @@ extern int deflateInit OF((z_stream *strm, int level));
167 compression (currently equivalent to level 6). 179 compression (currently equivalent to level 6).
168 180
169 deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not 181 deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
170 enough memory, Z_STREAM_ERROR if level is not a valid compression level. 182 enough memory, Z_STREAM_ERROR if level is not a valid compression level,
183 Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
184 with the version assumed by the caller (ZLIB_VERSION).
171 msg is set to null if there is no error message. deflateInit does not 185 msg is set to null if there is no error message. deflateInit does not
172 perform any compression: this will be done by deflate(). 186 perform any compression: this will be done by deflate().
173*/ 187*/
@@ -193,7 +207,9 @@ extern int deflate OF((z_stream *strm, int flush));
193 more output, and updating avail_in or avail_out accordingly; avail_out 207 more output, and updating avail_in or avail_out accordingly; avail_out
194 should never be zero before the call. The application can consume the 208 should never be zero before the call. The application can consume the
195 compressed output when it wants, for example when the output buffer is full 209 compressed output when it wants, for example when the output buffer is full
196 (avail_out == 0), or after each call of deflate(). 210 (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
211 and with zero avail_out, it must be called again after making room in the
212 output buffer because there might be more output pending.
197 213
198 If the parameter flush is set to Z_PARTIAL_FLUSH, the current compression 214 If the parameter flush is set to Z_PARTIAL_FLUSH, the current compression
199 block is terminated and flushed to the output buffer so that the 215 block is terminated and flushed to the output buffer so that the
@@ -247,22 +263,26 @@ extern int deflateEnd OF((z_stream *strm));
247 pending output. 263 pending output.
248 264
249 deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the 265 deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
250 stream state was inconsistent. In the error case, msg may be set 266 stream state was inconsistent, Z_DATA_ERROR if the stream was freed
251 but then points to a static string (which must not be deallocated). 267 prematurely (some input or output was discarded). In the error case,
268 msg may be set but then points to a static string (which must not be
269 deallocated).
252*/ 270*/
253 271
254 272
255extern int inflateInit OF((z_stream *strm));
256/* 273/*
274extern int inflateInit OF((z_stream *strm));
275
257 Initializes the internal stream state for decompression. The fields 276 Initializes the internal stream state for decompression. The fields
258 zalloc, zfree and opaque must be initialized before by the caller. If 277 zalloc, zfree and opaque must be initialized before by the caller. If
259 zalloc and zfree are set to Z_NULL, inflateInit updates them to use default 278 zalloc and zfree are set to Z_NULL, inflateInit updates them to use default
260 allocation functions. 279 allocation functions.
261 280
262 inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not 281 inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
263 enough memory. msg is set to null if there is no error message. 282 enough memory, Z_VERSION_ERROR if the zlib library version is incompatible
264 inflateInit does not perform any decompression: this will be done by 283 with the version assumed by the caller. msg is set to null if there is no
265 inflate(). 284 error message. inflateInit does not perform any decompression: this will be
285 done by inflate().
266*/ 286*/
267 287
268 288
@@ -276,15 +296,18 @@ extern int inflate OF((z_stream *strm, int flush));
276 will resume at this point for the next call of inflate(). 296 will resume at this point for the next call of inflate().
277 297
278 - Provide more output starting at next_out and update next_out and avail_out 298 - Provide more output starting at next_out and update next_out and avail_out
279 accordingly. inflate() always provides as much output as possible 299 accordingly. inflate() provides as much output as possible, until there
280 (until there is no more input data or no more space in the output buffer). 300 is no more input data or no more space in the output buffer (see below
301 about the flush parameter).
281 302
282 Before the call of inflate(), the application should ensure that at least 303 Before the call of inflate(), the application should ensure that at least
283 one of the actions is possible, by providing more input and/or consuming 304 one of the actions is possible, by providing more input and/or consuming
284 more output, and updating the next_* and avail_* values accordingly. 305 more output, and updating the next_* and avail_* values accordingly.
285 The application can consume the uncompressed output when it wants, for 306 The application can consume the uncompressed output when it wants, for
286 example when the output buffer is full (avail_out == 0), or after each 307 example when the output buffer is full (avail_out == 0), or after each
287 call of inflate(). 308 call of inflate(). If inflate returns Z_OK and with zero avail_out, it
309 must be called again after making room in the output buffer because there
310 might be more output pending.
288 311
289 If the parameter flush is set to Z_PARTIAL_FLUSH, inflate flushes as much 312 If the parameter flush is set to Z_PARTIAL_FLUSH, inflate flushes as much
290 output as possible to the output buffer. The flushing behavior of inflate is 313 output as possible to the output buffer. The flushing behavior of inflate is
@@ -306,12 +329,15 @@ extern int inflate OF((z_stream *strm, int flush));
306 inflate() returns Z_OK if some progress has been made (more input 329 inflate() returns Z_OK if some progress has been made (more input
307 processed or more output produced), Z_STREAM_END if the end of the 330 processed or more output produced), Z_STREAM_END if the end of the
308 compressed data has been reached and all uncompressed output has been 331 compressed data has been reached and all uncompressed output has been
309 produced, Z_DATA_ERROR if the input data was corrupted, Z_STREAM_ERROR if 332 produced, Z_NEED_DICT if a preset dictionary is needed at this point (see
310 the stream structure was inconsistent (for example if next_in or next_out 333 inflateSetDictionary below), Z_DATA_ERROR if the input data was corrupted,
311 was NULL), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if no 334 Z_STREAM_ERROR if the stream structure was inconsistent (for example if
312 progress is possible or if there was not enough room in the output buffer 335 next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
313 when Z_FINISH is used. In the Z_DATA_ERROR case, the application may then 336 Z_BUF_ERROR if no progress is possible or if there was not enough room in
314 call inflateSync to look for a good compression block. 337 the output buffer when Z_FINISH is used. In the Z_DATA_ERROR case, the
338 application may then call inflateSync to look for a good compression block.
339 In the Z_NEED_DICT case, strm->adler is set to the Adler32 value of the
340 dictionary chosen by the compressor.
315*/ 341*/
316 342
317 343
@@ -326,19 +352,20 @@ extern int inflateEnd OF((z_stream *strm));
326 static string (which must not be deallocated). 352 static string (which must not be deallocated).
327*/ 353*/
328 354
329 /* advanced functions */ 355 /* Advanced functions */
330 356
331/* 357/*
332 The following functions are needed only in some special applications. 358 The following functions are needed only in some special applications.
333*/ 359*/
334 360
361/*
335extern int deflateInit2 OF((z_stream *strm, 362extern int deflateInit2 OF((z_stream *strm,
336 int level, 363 int level,
337 int method, 364 int method,
338 int windowBits, 365 int windowBits,
339 int memLevel, 366 int memLevel,
340 int strategy)); 367 int strategy));
341/* 368
342 This is another version of deflateInit with more compression options. The 369 This is another version of deflateInit with more compression options. The
343 fields next_in, zalloc, zfree and opaque must be initialized before by 370 fields next_in, zalloc, zfree and opaque must be initialized before by
344 the caller. 371 the caller.
@@ -353,20 +380,22 @@ extern int deflateInit2 OF((z_stream *strm,
353 values of this parameter result in better compression at the expense of 380 values of this parameter result in better compression at the expense of
354 memory usage. The default value is 15 if deflateInit is used instead. 381 memory usage. The default value is 15 if deflateInit is used instead.
355 382
356 The memLevel parameter specifies how much memory should be allocated 383 The memLevel parameter specifies how much memory should be allocated
357 for the internal compression state. memLevel=1 uses minimum memory but 384 for the internal compression state. memLevel=1 uses minimum memory but
358 is slow and reduces compression ratio; memLevel=9 uses maximum memory 385 is slow and reduces compression ratio; memLevel=9 uses maximum memory
359 for optimal speed. The default value is 8. See zconf.h for total memory 386 for optimal speed. The default value is 8. See zconf.h for total memory
360 usage as a function of windowBits and memLevel. 387 usage as a function of windowBits and memLevel.
361 388
362 The strategy parameter is used to tune the compression algorithm. Use 389 The strategy parameter is used to tune the compression algorithm. Use the
363 the value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data 390 value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
364 produced by a filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman 391 filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
365 encoding only (no string match). Filtered data consists mostly of small 392 string match). Filtered data consists mostly of small values with a
366 values with a somewhat random distribution. In this case, the 393 somewhat random distribution. In this case, the compression algorithm is
367 compression algorithm is tuned to compress them better. The strategy 394 tuned to compress them better. The effect of Z_FILTERED is to force more
368 parameter only affects the compression ratio but not the correctness of 395 Huffman coding and less string matching; it is somewhat intermediate
369 the compressed output even if it is not set appropriately. 396 between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
397 the compression ratio but not the correctness of the compressed output even
398 if it is not set appropriately.
370 399
371 If next_in is not null, the library will use this buffer to hold also 400 If next_in is not null, the library will use this buffer to hold also
372 some history information; the buffer must either hold the entire input 401 some history information; the buffer must either hold the entire input
@@ -385,9 +414,38 @@ extern int deflateInit2 OF((z_stream *strm,
385 not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as 414 not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as
386 an invalid method). msg is set to null if there is no error message. 415 an invalid method). msg is set to null if there is no error message.
387 deflateInit2 does not perform any compression: this will be done by 416 deflateInit2 does not perform any compression: this will be done by
388 deflate(). 417 deflate().
389*/ 418*/
390 419
420extern int deflateSetDictionary OF((z_stream *strm,
421 const Bytef *dictionary,
422 uInt dictLength));
423/*
424 Initializes the compression dictionary (history buffer) from the given
425 byte sequence without producing any compressed output. This function must
426 be called immediately after deflateInit or deflateInit2, before any call
427 of deflate. The compressor and decompressor must use exactly the same
428 dictionary (see inflateSetDictionary).
429 The dictionary should consist of strings (byte sequences) that are likely
430 to be encountered later in the data to be compressed, with the most commonly
431 used strings preferably put towards the end of the dictionary. Using a
432 dictionary is most useful when the data to be compressed is short and
433 can be predicted with good accuracy; the data can then be compressed better
434 than with the default empty dictionary. In this version of the library,
435 only the last 32K bytes of the dictionary are used.
436 Upon return of this function, strm->adler is set to the Adler32 value
437 of the dictionary; the decompressor may later use this value to determine
438 which dictionary has been used by the compressor. (The Adler32 value
439 applies to the whole dictionary even if only a subset of the dictionary is
440 actually used by the compressor.)
441
442 deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
443 parameter is invalid (such as NULL dictionary) or the stream state
444 is inconsistent (for example if deflate has already been called for this
445 stream). deflateSetDictionary does not perform any compression: this will
446 be done by deflate().
447*/
448
391extern int deflateCopy OF((z_stream *dest, 449extern int deflateCopy OF((z_stream *dest,
392 z_stream *source)); 450 z_stream *source));
393/* 451/*
@@ -398,14 +456,14 @@ extern int deflateCopy OF((z_stream *dest,
398 application to provide the correct values of next_out and avail_out for the 456 application to provide the correct values of next_out and avail_out for the
399 next call of deflate. 457 next call of deflate.
400 458
401 This function is useful when several compression strategies will be 459 This function can be useful when several compression strategies will be
402 tried, for example when there are several ways of pre-processing the input 460 tried, for example when there are several ways of pre-processing the input
403 data with a filter. The streams that will be discarded should then be freed 461 data with a filter. The streams that will be discarded should then be freed
404 by calling deflateEnd. Note that deflateCopy duplicates the internal 462 by calling deflateEnd. Note that deflateCopy duplicates the internal
405 compression state which can be quite large, so this strategy is slow and 463 compression state which can be quite large, so this strategy is slow and
406 can consume lots of memory. 464 can consume lots of memory.
407 465
408 deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not 466 deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
409 enough memory, Z_STREAM_ERROR if the source stream state was inconsistent 467 enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
410 (such as zalloc being NULL). msg is left unchanged in both source and 468 (such as zalloc being NULL). msg is left unchanged in both source and
411 destination. 469 destination.
@@ -431,13 +489,19 @@ extern int deflateParams OF((z_stream *strm, int level, int strategy));
431 available so far is compressed with the old level (and may be flushed); 489 available so far is compressed with the old level (and may be flushed);
432 the new level will take effect only at the next call of deflate(). 490 the new level will take effect only at the next call of deflate().
433 491
492 Before the call of deflateParams, the stream state must be set as for
493 a call of deflate(), since the currently available input may have to
494 be compressed and flushed. In particular, strm->avail_out must be non-zero.
495
434 deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source 496 deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
435 stream state was inconsistent or if a parameter was invalid. 497 stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
498 if strm->avail_out was zero.
436*/ 499*/
437 500
501/*
438extern int inflateInit2 OF((z_stream *strm, 502extern int inflateInit2 OF((z_stream *strm,
439 int windowBits)); 503 int windowBits));
440/* 504
441 This is another version of inflateInit with more compression options. The 505 This is another version of inflateInit with more compression options. The
442 fields next_out, zalloc, zfree and opaque must be initialized before by 506 fields next_out, zalloc, zfree and opaque must be initialized before by
443 the caller. 507 the caller.
@@ -469,6 +533,25 @@ extern int inflateInit2 OF((z_stream *strm,
469 inflate(). 533 inflate().
470*/ 534*/
471 535
536extern int inflateSetDictionary OF((z_stream *strm,
537 const Bytef *dictionary,
538 uInt dictLength));
539/*
540 Initializes the decompression dictionary (history buffer) from the given
541 uncompressed byte sequence. This function must be called immediately after
542 a call of inflate if this call returned Z_NEED_DICT. The dictionary chosen
543 by the compressor can be determined from the Adler32 value returned by this
544 call of inflate. The compressor and decompressor must use exactly the same
545 dictionary (see deflateSetDictionary).
546
547 inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
548 parameter is invalid (such as NULL dictionary) or the stream state is
549 inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
550 expected one (incorrect Adler32 value). inflateSetDictionary does not
551 perform any decompression: this will be done by subsequent calls of
552 inflate().
553*/
554
472extern int inflateSync OF((z_stream *strm)); 555extern int inflateSync OF((z_stream *strm));
473/* 556/*
474 Skips invalid compressed data until the special marker (see deflate() 557 Skips invalid compressed data until the special marker (see deflate()
@@ -506,7 +589,7 @@ extern int inflateReset OF((z_stream *strm));
506*/ 589*/
507 590
508extern int compress OF((Bytef *dest, uLongf *destLen, 591extern int compress OF((Bytef *dest, uLongf *destLen,
509 Bytef *source, uLong sourceLen)); 592 const Bytef *source, uLong sourceLen));
510/* 593/*
511 Compresses the source buffer into the destination buffer. sourceLen is 594 Compresses the source buffer into the destination buffer. sourceLen is
512 the byte length of the source buffer. Upon entry, destLen is the total 595 the byte length of the source buffer. Upon entry, destLen is the total
@@ -521,7 +604,7 @@ extern int compress OF((Bytef *dest, uLongf *destLen,
521*/ 604*/
522 605
523extern int uncompress OF((Bytef *dest, uLongf *destLen, 606extern int uncompress OF((Bytef *dest, uLongf *destLen,
524 Bytef *source, uLong sourceLen)); 607 const Bytef *source, uLong sourceLen));
525/* 608/*
526 Decompresses the source buffer into the destination buffer. sourceLen is 609 Decompresses the source buffer into the destination buffer. sourceLen is
527 the byte length of the source buffer. Upon entry, destLen is the total 610 the byte length of the source buffer. Upon entry, destLen is the total
@@ -541,7 +624,7 @@ extern int uncompress OF((Bytef *dest, uLongf *destLen,
541 624
542typedef voidp gzFile; 625typedef voidp gzFile;
543 626
544extern gzFile gzopen OF((char *path, char *mode)); 627extern gzFile gzopen OF((const char *path, const char *mode));
545/* 628/*
546 Opens a gzip (.gz) file for reading or writing. The mode parameter 629 Opens a gzip (.gz) file for reading or writing. The mode parameter
547 is as in fopen ("rb" or "wb") but can also include a compression level 630 is as in fopen ("rb" or "wb") but can also include a compression level
@@ -553,12 +636,15 @@ extern gzFile gzopen OF((char *path, char *mode));
553 zlib error is Z_MEM_ERROR). 636 zlib error is Z_MEM_ERROR).
554*/ 637*/
555 638
556extern gzFile gzdopen OF((int fd, char *mode)); 639extern gzFile gzdopen OF((int fd, const char *mode));
557/* 640/*
558 gzdopen() associates a gzFile with the file descriptor fd. File 641 gzdopen() associates a gzFile with the file descriptor fd. File
559 descriptors are obtained from calls like open, dup, creat, pipe or 642 descriptors are obtained from calls like open, dup, creat, pipe or
560 fileno (in the file has been previously opened with fopen). 643 fileno (in the file has been previously opened with fopen).
561 The mode parameter is as in fopen ("rb" or "wb"). 644 The mode parameter is as in gzopen.
645 The next call of gzclose on the returned gzFile will also close the
646 file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
647 descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
562 gzdopen returns NULL if there was insufficient memory to allocate 648 gzdopen returns NULL if there was insufficient memory to allocate
563 the (de)compression state. 649 the (de)compression state.
564*/ 650*/
@@ -571,7 +657,7 @@ extern int gzread OF((gzFile file, voidp buf, unsigned len));
571 gzread returns the number of uncompressed bytes actually read (0 for 657 gzread returns the number of uncompressed bytes actually read (0 for
572 end of file, -1 for error). */ 658 end of file, -1 for error). */
573 659
574extern int gzwrite OF((gzFile file, voidp buf, unsigned len)); 660extern int gzwrite OF((gzFile file, const voidp buf, unsigned len));
575/* 661/*
576 Writes the given number of uncompressed bytes into the compressed file. 662 Writes the given number of uncompressed bytes into the compressed file.
577 gzwrite returns the number of uncompressed bytes actually written 663 gzwrite returns the number of uncompressed bytes actually written
@@ -612,7 +698,7 @@ extern char* gzerror OF((gzFile file, int *errnum));
612 compression library. 698 compression library.
613*/ 699*/
614 700
615extern uLong adler32 OF((uLong adler, Bytef *buf, uInt len)); 701extern uLong adler32 OF((uLong adler, const Bytef *buf, uInt len));
616 702
617/* 703/*
618 Update a running Adler-32 checksum with the bytes buf[0..len-1] and 704 Update a running Adler-32 checksum with the bytes buf[0..len-1] and
@@ -629,7 +715,7 @@ extern uLong adler32 OF((uLong adler, Bytef *buf, uInt len));
629 if (adler != original_adler) error(); 715 if (adler != original_adler) error();
630*/ 716*/
631 717
632extern uLong crc32 OF((uLong crc, Bytef *buf, uInt len)); 718extern uLong crc32 OF((uLong crc, const Bytef *buf, uInt len));
633/* 719/*
634 Update a running crc with the bytes buf[0..len-1] and return the updated 720 Update a running crc with the bytes buf[0..len-1] and return the updated
635 crc. If buf is NULL, this function returns the required initial value 721 crc. If buf is NULL, this function returns the required initial value
@@ -645,10 +731,37 @@ extern uLong crc32 OF((uLong crc, Bytef *buf, uInt len));
645 if (crc != original_crc) error(); 731 if (crc != original_crc) error();
646*/ 732*/
647 733
648#ifndef _Z_UTIL_H 734
735 /* various hacks, don't look :) */
736
737/* deflateInit and inflateInit are macros to allow checking the zlib version
738 * and the compiler's view of z_stream:
739 */
740extern int deflateInit_ OF((z_stream *strm, int level,
741 const char *version, int stream_size));
742extern int inflateInit_ OF((z_stream *strm,
743 const char *version, int stream_size));
744extern int deflateInit2_ OF((z_stream *strm, int level, int method,
745 int windowBits, int memLevel, int strategy,
746 const char *version, int stream_size));
747extern int inflateInit2_ OF((z_stream *strm, int windowBits,
748 const char *version, int stream_size));
749#define deflateInit(strm, level) \
750 deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
751#define inflateInit(strm) \
752 inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
753#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
754 deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
755 (strategy), ZLIB_VERSION, sizeof(z_stream))
756#define inflateInit2(strm, windowBits) \
757 inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
758
759#if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)
649 struct internal_state {int dummy;}; /* hack for buggy compilers */ 760 struct internal_state {int dummy;}; /* hack for buggy compilers */
650#endif 761#endif
651 762
763uLongf *get_crc_table OF((void)); /* can be used by asm versions of crc32() */
764
652#ifdef __cplusplus 765#ifdef __cplusplus
653} 766}
654#endif 767#endif
diff --git a/zutil.c b/zutil.c
index 97e74ea..66ddae4 100644
--- a/zutil.c
+++ b/zutil.c
@@ -1,9 +1,9 @@
1/* zutil.c -- target dependent utility functions for the compression library 1/* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995 Jean-loup Gailly. 2 * Copyright (C) 1995-1996 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: zutil.c,v 1.8 1995/05/03 17:27:12 jloup Exp $ */ 6/* $Id: zutil.c,v 1.12 1996/01/30 21:59:29 me Exp $ */
7 7
8#include <stdio.h> 8#include <stdio.h>
9 9
@@ -15,16 +15,18 @@ struct internal_state {int dummy;}; /* for buggy compilers */
15extern void exit OF((int)); 15extern void exit OF((int));
16#endif 16#endif
17 17
18char *zlib_version = ZLIB_VERSION; 18const char *zlib_version = ZLIB_VERSION;
19 19
20char *z_errmsg[] = { 20const char *z_errmsg[10] = {
21"stream end", /* Z_STREAM_END 1 */ 21"need dictionary", /* Z_NEED_DICT 2 */
22"", /* Z_OK 0 */ 22"stream end", /* Z_STREAM_END 1 */
23"file error", /* Z_ERRNO (-1) */ 23"", /* Z_OK 0 */
24"stream error", /* Z_STREAM_ERROR (-2) */ 24"file error", /* Z_ERRNO (-1) */
25"data error", /* Z_DATA_ERROR (-3) */ 25"stream error", /* Z_STREAM_ERROR (-2) */
26"insufficient memory", /* Z_MEM_ERROR (-4) */ 26"data error", /* Z_DATA_ERROR (-3) */
27"buffer error", /* Z_BUF_ERROR (-5) */ 27"insufficient memory", /* Z_MEM_ERROR (-4) */
28"buffer error", /* Z_BUF_ERROR (-5) */
29"incompatible version",/* Z_VERSION_ERROR (-6) */
28""}; 30""};
29 31
30 32
@@ -60,9 +62,9 @@ void zmemzero(dest, len)
60#endif 62#endif
61 63
62#ifdef __TURBOC__ 64#ifdef __TURBOC__
63#if !defined(__SMALL__) && !defined(__MEDIUM__) && !defined(__32BIT__) 65#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
64/* Small and medium model are for now limited to near allocation with 66/* Small and medium model in Turbo C are for now limited to near allocation
65 * reduced MAX_WBITS and MAX_MEM_LEVEL 67 * with reduced MAX_WBITS and MAX_MEM_LEVEL
66 */ 68 */
67# define MY_ZCALLOC 69# define MY_ZCALLOC
68 70
@@ -100,9 +102,6 @@ voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
100 */ 102 */
101 if (bsize < 65520L) { 103 if (bsize < 65520L) {
102 buf = farmalloc(bsize); 104 buf = farmalloc(bsize);
103#ifdef DEBUG
104 zmemzero(buf, (uInt)bsize);
105#endif
106 if (*(ush*)&buf != 0) return buf; 105 if (*(ush*)&buf != 0) return buf;
107 } else { 106 } else {
108 buf = farmalloc(bsize + 16L); 107 buf = farmalloc(bsize + 16L);
@@ -114,10 +113,6 @@ voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
114 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; 113 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
115 *(ush*)&buf = 0; 114 *(ush*)&buf = 0;
116 table[next_ptr++].new_ptr = buf; 115 table[next_ptr++].new_ptr = buf;
117#ifdef DEBUG
118 zmemzero(buf, (uInt)65535);
119 ((uch*)buf)[65535] = 0;
120#endif
121 return buf; 116 return buf;
122} 117}
123 118
@@ -146,7 +141,8 @@ void zcfree (voidpf opaque, voidpf ptr)
146#endif /* __TURBOC__ */ 141#endif /* __TURBOC__ */
147 142
148 143
149#ifdef M_I86 /* Microsoft C */ 144#if defined(M_I86) && !(defined(__WATCOMC__) && defined(__386__))
145/* Microsoft C */
150 146
151# define MY_ZCALLOC 147# define MY_ZCALLOC
152 148
@@ -182,6 +178,7 @@ voidpf zcalloc (opaque, items, size)
182 unsigned items; 178 unsigned items;
183 unsigned size; 179 unsigned size;
184{ 180{
181 if (opaque) items += size - size; /* make compiler happy */
185 return (voidpf)calloc(items, size); 182 return (voidpf)calloc(items, size);
186} 183}
187 184
@@ -190,6 +187,7 @@ void zcfree (opaque, ptr)
190 voidpf ptr; 187 voidpf ptr;
191{ 188{
192 free(ptr); 189 free(ptr);
190 if (opaque) return; /* make compiler happy */
193} 191}
194 192
195#endif /* MY_ZCALLOC */ 193#endif /* MY_ZCALLOC */
diff --git a/zutil.h b/zutil.h
index bc2d772..3abd8c4 100644
--- a/zutil.h
+++ b/zutil.h
@@ -1,5 +1,5 @@
1/* zutil.h -- internal interface and configuration of the compression library 1/* zutil.h -- internal interface and configuration of the compression library
2 * Copyright (C) 1995 Jean-loup Gailly. 2 * Copyright (C) 1995-1996 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -8,14 +8,14 @@
8 subject to change. Applications should only use zlib.h. 8 subject to change. Applications should only use zlib.h.
9 */ 9 */
10 10
11/* $Id: zutil.h,v 1.9 1995/05/03 17:27:12 jloup Exp $ */ 11/* $Id: zutil.h,v 1.13 1996/01/30 21:59:29 me Exp $ */
12 12
13#ifndef _Z_UTIL_H 13#ifndef _Z_UTIL_H
14#define _Z_UTIL_H 14#define _Z_UTIL_H
15 15
16#include "zlib.h" 16#include "zlib.h"
17 17
18#if defined(MSDOS) || defined(VMS) || defined(CRAY) 18#if defined(MSDOS) || defined(VMS) || defined(CRAY) || defined(WIN32)
19# include <stddef.h> 19# include <stddef.h>
20# include <errno.h> 20# include <errno.h>
21#else 21#else
@@ -37,9 +37,13 @@ typedef unsigned short ush;
37typedef ush FAR ushf; 37typedef ush FAR ushf;
38typedef unsigned long ulg; 38typedef unsigned long ulg;
39 39
40extern char *z_errmsg[]; /* indexed by 1-zlib_error */ 40extern const char *z_errmsg[10]; /* indexed by 2-zlib_error */
41/* (size given to avoid silly warnings with Visual C++) */
41 42
42#define ERR_RETURN(strm,err) return (strm->msg = z_errmsg[1-(err)], (err)) 43#define ERR_MSG(err) (char*)z_errmsg[Z_NEED_DICT-(err)]
44
45#define ERR_RETURN(strm,err) \
46 return (strm->msg = ERR_MSG(err), (err))
43/* To be used only when the state is known to be valid */ 47/* To be used only when the state is known to be valid */
44 48
45 /* common constants */ 49 /* common constants */
@@ -65,6 +69,8 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
65#define MAX_MATCH 258 69#define MAX_MATCH 258
66/* The minimum and maximum match lengths */ 70/* The minimum and maximum match lengths */
67 71
72#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
73
68 /* target dependencies */ 74 /* target dependencies */
69 75
70#ifdef MSDOS 76#ifdef MSDOS
@@ -110,6 +116,10 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
110# define OS_CODE 0x0a 116# define OS_CODE 0x0a
111#endif 117#endif
112 118
119#ifdef _BEOS_
120# define fdopen(fd,mode) NULL /* No fdopen() */
121#endif
122
113 /* Common defaults */ 123 /* Common defaults */
114 124
115#ifndef OS_CODE 125#ifndef OS_CODE
@@ -142,11 +152,13 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
142# define HAVE_MEMCPY 152# define HAVE_MEMCPY
143#endif 153#endif
144#ifdef HAVE_MEMCPY 154#ifdef HAVE_MEMCPY
145# if defined(M_I86SM) || defined(M_I86MM) /* MSC small or medium model */ 155# ifdef SMALL_MEDIUM /* MSDOS small or medium model */
146# define zmemcpy _fmemcpy 156# define zmemcpy _fmemcpy
157# define zmemcmp _fmemcmp
147# define zmemzero(dest, len) _fmemset(dest, 0, len) 158# define zmemzero(dest, len) _fmemset(dest, 0, len)
148# else 159# else
149# define zmemcpy memcpy 160# define zmemcpy memcpy
161# define zmemcmp memcmp
150# define zmemzero(dest, len) memset(dest, 0, len) 162# define zmemzero(dest, len) memset(dest, 0, len)
151# endif 163# endif
152#else 164#else
@@ -176,7 +188,7 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
176#endif 188#endif
177 189
178 190
179typedef uLong (*check_func) OF((uLong check, Bytef *buf, uInt len)); 191typedef uLong (*check_func) OF((uLong check, const Bytef *buf, uInt len));
180 192
181extern void z_error OF((char *m)); 193extern void z_error OF((char *m));
182 194