summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog61
-rw-r--r--Make_vms.com2
-rw-r--r--Makefile119
-rw-r--r--Makefile.bor2
-rw-r--r--Makefile.in130
-rw-r--r--Makefile.qnx75
-rw-r--r--README101
-rw-r--r--adler32.c4
-rw-r--r--compress.c7
-rwxr-xr-xconfigure81
-rw-r--r--crc32.c60
-rw-r--r--deflate.c399
-rw-r--r--deflate.h26
-rw-r--r--example.c322
-rw-r--r--gzio.c370
-rw-r--r--infblock.c25
-rw-r--r--infblock.h8
-rw-r--r--infcodes.c11
-rw-r--r--infcodes.h2
-rw-r--r--inffast.c11
-rw-r--r--inffast.h2
-rw-r--r--inflate.c103
-rw-r--r--inftrees.c77
-rw-r--r--inftrees.h2
-rw-r--r--infutil.c8
-rw-r--r--infutil.h29
-rw-r--r--minigzip.c21
-rw-r--r--trees.c134
-rw-r--r--uncompr.c6
-rw-r--r--zconf.h86
-rw-r--r--zlib.h259
-rw-r--r--zutil.c46
-rw-r--r--zutil.h25
33 files changed, 1836 insertions, 778 deletions
diff --git a/ChangeLog b/ChangeLog
index 4c69f88..530bb06 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,67 @@
1 1
2 ChangeLog file for zlib 2 ChangeLog file for zlib
3 3
4Changes in 1.0 (15 Jan 96)
5- allow preset dictionary shared between compressor and decompressor
6- allow compression level 0 (no compression)
7- add deflateParams in zlib.h: allow dynamic change of compression level
8 and compression strategy.
9- test large buffers and deflateParams in example.c
10- add optional "configure" to build zlib as a shared library
11- suppress Makefile.qnx, use configure instead
12- fixed deflate for 64-bit systems (detected on Cray)
13- fixed inflate_blocks for 64-bit systems (detected on Alpha)
14- declare Z_DEFLATED in zlib.h (possible parameter for deflateInit2)
15- always return Z_BUF_ERROR when deflate() has nothing to do
16- deflateInit and inflateInit are now macros to allow version checking
17- prefix all global functions and types with z_ with -DZ_PREFIX
18- make falloc completely reentrant (inftrees.c)
19- fixed very unlikely race condition in ct_static_init
20- free in reverse order of allocation to help memory manager
21- use zlib-1.0/* instead of zlib/* inside the tar.gz
22- make zlib warning-free with "gcc -O3 -Wall -Wwrite-strings -Wpointer-arith
23 -Wconversion -Wstrict-prototypes -Wmissing-prototypes"
24- allow gzread on concatenated .gz files
25- deflateEnd now returns Z_DATA_ERROR if it was premature
26- deflate is finally (?) fully deterministic (no matches beyond end of input)
27- Document Z_SYNC_FLUSH
28- add uninstall in Makefile
29- Check for __cpluplus in zlib.h
30- Better test in ct_align for partial flush
31- avoid harmless warnings for Borland C++
32- initialize hash_head in deflate.c
33- avoid warning on fdopen (gzio.c) for HP cc -Aa
34- include stdlib.h for STDC compilers
35- include errno.h for Cray
36- ignore error if ranlib doesn't exist
37- call ranlib twice for NeXTSTEP
38- use exec_prefix instead of prefix for libz.a
39- renamed ct_* as _tr_* to avoid conflict with applications
40- clear z->msg in inflateInit2 before any error return
41- initialize opaque in example.c, gzio.c, deflate.c and inflate.c
42- fixed typo in zconf.h (_GNUC__ => __GNUC__)
43- check for WIN32 in zconf.h and zutil.c (avoid farmalloc in 32-bit mode)
44- fix typo in Make_vms.com (f$trnlnm -> f$getsyi)
45- in fcalloc, normalize pointer if size > 65520 bytes
46- don't use special fcalloc for 32 bit Borland C++
47- use STDC instead of __GO32__ to avoid redeclaring exit, calloc, etc...
48- use Z_BINARY instead of BINARY
49- document that gzclose after gzdopen will close the file
50- allow "a" as mode in gzopen.
51- fix error checking in gzread
52- allow skipping .gz extra-field on pipes
53- added reference to Perl interface in README
54- put the crc table in FAR data (I dislike more and more the medium model :)
55- added get_crc_table
56- added a dimension to all arrays (Borland C can't count).
57- workaround Borland C bug in declaration of inflate_codes_new & inflate_fast
58- guard against multiple inclusion of *.h (for precompiled header on Mac)
59- Watcom C pretends to be Microsoft C small model even in 32 bit mode.
60- don't use unsized arrays to avoid silly warnings by Visual C++:
61 warning C4746: 'inflate_mask' : unsized array treated as '__far'
62 (what's wrong with far data in far model?).
63- define enum out of inflate_blocks_state to allow compilation with C++
64
4Changes in 0.95 (16 Aug 95) 65Changes in 0.95 (16 Aug 95)
5- fix MSDOS small and medium model (now easier to adapt to any compiler) 66- fix MSDOS small and medium model (now easier to adapt to any compiler)
6- inlined send_bits 67- inlined send_bits
diff --git a/Make_vms.com b/Make_vms.com
index 2d09f7d..0008d00 100644
--- a/Make_vms.com
+++ b/Make_vms.com
@@ -4,7 +4,7 @@ $!
4$! Look for the compiler used 4$! Look for the compiler used
5$! 5$!
6$ ccopt = "" 6$ ccopt = ""
7$ if f$trnlnm("HW_MODEL").ge.1024 7$ if f$getsyi("HW_MODEL").ge.1024
8$ then 8$ then
9$ ccopt = "/prefix=all"+ccopt 9$ ccopt = "/prefix=all"+ccopt
10$ comp = "__decc__=1" 10$ comp = "__decc__=1"
diff --git a/Makefile b/Makefile
index d9e11c5..fe5b372 100644
--- a/Makefile
+++ b/Makefile
@@ -1,73 +1,130 @@
1# Makefile for zlib 1# Makefile for zlib
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# 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 to take advantage of inlining 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
26LIBS=libz.a
27
28AR=ar rc
12RANLIB=ranlib 29RANLIB=ranlib
30TAR=tar
13 31
14prefix=/usr/local 32prefix=/usr/local
33exec_prefix = $(prefix)
15 34
16OBJS = 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 \
17 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
18 37
19TEST_OBJS = example.o minigzip.o 38TEST_OBJS = example.o minigzip.o
20 39
40DISTFILES = README ChangeLog configure Makefile.in Makefile Makefile.msc \
41 Makefile.bor Makefile.tc Make_vms.com descrip.mms *.[ch]
42
21all: example minigzip 43all: example minigzip
22 44
23test: all 45test: all
24 ./example 46 ./example
25 echo hello world | ./minigzip | ./minigzip -d 47 echo hello world | ./minigzip | ./minigzip -d
26 48
27install: libz.a
28 -@mkdir $(prefix)/include
29 -@mkdir $(prefix)/lib
30 cp zlib.h zconf.h $(prefix)/include
31 chmod 644 $(prefix)/include/zlib.h $(prefix)/include/zconf.h
32 cp libz.a $(prefix)/lib
33 chmod 644 $(prefix)/lib/libz.a
34
35libz.a: $(OBJS) 49libz.a: $(OBJS)
36 ar rc $@ $(OBJS) 50 $(AR) $@ $(OBJS)
37 $(RANLIB) $@ 51 -@ ($(RANLIB) $@ || true) 2>/dev/null
38 52
39example: example.o libz.a 53libz.so.$(VER): $(OBJS)
54 $(LDSHARED) -o $@ $(OBJS)
55 rm -f libz.so; ln -s $@ libz.so
56
57example: example.o $(LIBS)
40 $(CC) $(CFLAGS) -o $@ example.o $(LDFLAGS) 58 $(CC) $(CFLAGS) -o $@ example.o $(LDFLAGS)
41 59
42minigzip: minigzip.o libz.a 60minigzip: minigzip.o $(LIBS)
43 $(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS) 61 $(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS)
44 62
63install: $(LIBS)
64 -@if [ ! $(prefix)/include ]; then mkdir $(prefix)/include; fi
65 -@if [ ! $(exec_prefix)/lib ]; then mkdir $(exec_prefix)/lib; fi
66 cp zlib.h zconf.h $(prefix)/include
67 chmod 644 $(prefix)/include/zlib.h $(prefix)/include/zconf.h
68 cp $(LIBS) $(exec_prefix)/lib
69 cd $(exec_prefix)/lib; chmod 644 $(LIBS)
70 -@(cd $(exec_prefix)/lib; $(RANLIB) libz.a || true) >/dev/null 2>&1
71 cd $(exec_prefix)/lib; if test -f libz.so.$(VER); then \
72 ln -s libz.so.$(VER) libz.so; \
73 fi
74# The ranlib in install is needed on NeXTSTEP which checks file times
75
76uninstall:
77 cd $(exec_prefix)/lib; rm -f $(LIBS); \
78 if test -f libz.so; then \
79 v=`sed -n '/VERSION "/s/.*"\(.*\)".*/\1/p'<$(prefix)/include/zlib.h`;\
80 rm -f libz.so.$$v libz.so; \
81 fi
82 cd $(prefix)/include; rm -f zlib.h zconf.h
83
45clean: 84clean:
46 rm -f *.o example minigzip libz.a foo.gz 85 rm -f *.o *~ example minigzip libz.a libz.so* foo.gz
47 86
48zip: 87zip:
49 zip -ul9 zlib README ChangeLog Makefile Make????.??? Makefile.?? \ 88 mv Makefile Makefile~; cp -p Makefile.in Makefile
50 descrip.mms *.[ch] 89 v=`sed -n -e 's/\./-/' -e '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`;\
51 90 zip -ul9 zlib-$$v $(DISTFILES)
52tgz: 91 mv Makefile~ Makefile
53 cd ..; tar cfz zlib/zlib.tgz zlib/README zlib/ChangeLog zlib/Makefile \ 92
54 zlib/Make????.??? zlib/Makefile.?? zlib/descrip.mms zlib/*.[ch] 93dist:
94 mv Makefile Makefile~; cp -p Makefile.in Makefile
95 d=zlib-`sed -n '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`;\
96 rm -f $$d.tar.gz; \
97 if test ! -d ../$$d; then rm -f ../$$d; ln -s `pwd` ../$$d; fi; \
98 files=""; \
99 for f in $(DISTFILES); do files="$$files $$d/$$f"; done; \
100 cd ..; \
101 GZIP=-9 $(TAR) chofz $$d/$$d.tar.gz $$files; \
102 if test ! -d $$d; then rm -f $$d; fi
103 mv Makefile~ Makefile
104
105tags:
106 etags *.[ch]
107
108depend:
109 makedepend -- $(CFLAGS) -- *.[ch]
55 110
56# DO NOT DELETE THIS LINE -- make depend depends on it. 111# DO NOT DELETE THIS LINE -- make depend depends on it.
57 112
58adler32.o: zutil.h zlib.h zconf.h 113adler32.o: zlib.h zconf.h
59compress.o: zlib.h zconf.h 114compress.o: zlib.h zconf.h
60crc32.o: zutil.h zlib.h zconf.h 115crc32.o: zlib.h zconf.h
61deflate.o: deflate.h zutil.h zlib.h zconf.h 116deflate.o: deflate.h zutil.h zlib.h zconf.h
62example.o: zlib.h zconf.h 117example.o: zlib.h zconf.h
63gzio.o: zutil.h zlib.h zconf.h 118gzio.o: zutil.h zlib.h zconf.h
64infblock.o: zutil.h zlib.h zconf.h infblock.h inftrees.h infcodes.h infutil.h 119infblock.o: infblock.h inftrees.h infcodes.h infutil.h zutil.h zlib.h zconf.h
65infcodes.o: zutil.h zlib.h zconf.h inftrees.h infutil.h infcodes.h inffast.h 120infcodes.o: zutil.h zlib.h zconf.h
66inffast.o: zutil.h zlib.h zconf.h inftrees.h infutil.h inffast.h 121infcodes.o: inftrees.h infblock.h infcodes.h infutil.h inffast.h
122inffast.o: zutil.h zlib.h zconf.h inftrees.h
123inffast.o: infblock.h infcodes.h infutil.h inffast.h
67inflate.o: zutil.h zlib.h zconf.h infblock.h 124inflate.o: zutil.h zlib.h zconf.h infblock.h
68inftrees.o: zutil.h zlib.h zconf.h inftrees.h 125inftrees.o: zutil.h zlib.h zconf.h inftrees.h
69infutil.o: zutil.h zlib.h zconf.h inftrees.h infutil.h 126infutil.o: zutil.h zlib.h zconf.h infblock.h inftrees.h infcodes.h infutil.h
70minigzip.o: zlib.h zconf.h 127minigzip.o: zlib.h zconf.h
71trees.o: deflate.h zutil.h zlib.h zconf.h 128trees.o: deflate.h zutil.h zlib.h zconf.h
72uncompr.o: zlib.h zconf.h 129uncompr.o: zlib.h zconf.h
73zutil.o: zutil.h zlib.h zconf.h 130zutil.o: zutil.h zlib.h zconf.h
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.in b/Makefile.in
new file mode 100644
index 0000000..fe5b372
--- /dev/null
+++ b/Makefile.in
@@ -0,0 +1,130 @@
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
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 ChangeLog configure Makefile.in Makefile Makefile.msc \
41 Makefile.bor Makefile.tc Make_vms.com descrip.mms *.[ch]
42
43all: example minigzip
44
45test: all
46 ./example
47 echo hello world | ./minigzip | ./minigzip -d
48
49libz.a: $(OBJS)
50 $(AR) $@ $(OBJS)
51 -@ ($(RANLIB) $@ || true) 2>/dev/null
52
53libz.so.$(VER): $(OBJS)
54 $(LDSHARED) -o $@ $(OBJS)
55 rm -f libz.so; ln -s $@ libz.so
56
57example: example.o $(LIBS)
58 $(CC) $(CFLAGS) -o $@ example.o $(LDFLAGS)
59
60minigzip: minigzip.o $(LIBS)
61 $(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS)
62
63install: $(LIBS)
64 -@if [ ! $(prefix)/include ]; then mkdir $(prefix)/include; fi
65 -@if [ ! $(exec_prefix)/lib ]; then mkdir $(exec_prefix)/lib; fi
66 cp zlib.h zconf.h $(prefix)/include
67 chmod 644 $(prefix)/include/zlib.h $(prefix)/include/zconf.h
68 cp $(LIBS) $(exec_prefix)/lib
69 cd $(exec_prefix)/lib; chmod 644 $(LIBS)
70 -@(cd $(exec_prefix)/lib; $(RANLIB) libz.a || true) >/dev/null 2>&1
71 cd $(exec_prefix)/lib; if test -f libz.so.$(VER); then \
72 ln -s libz.so.$(VER) libz.so; \
73 fi
74# The ranlib in install is needed on NeXTSTEP which checks file times
75
76uninstall:
77 cd $(exec_prefix)/lib; rm -f $(LIBS); \
78 if test -f libz.so; then \
79 v=`sed -n '/VERSION "/s/.*"\(.*\)".*/\1/p'<$(prefix)/include/zlib.h`;\
80 rm -f libz.so.$$v libz.so; \
81 fi
82 cd $(prefix)/include; rm -f zlib.h zconf.h
83
84clean:
85 rm -f *.o *~ example minigzip libz.a libz.so* foo.gz
86
87zip:
88 mv Makefile Makefile~; cp -p Makefile.in Makefile
89 v=`sed -n -e 's/\./-/' -e '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`;\
90 zip -ul9 zlib-$$v $(DISTFILES)
91 mv Makefile~ Makefile
92
93dist:
94 mv Makefile Makefile~; cp -p Makefile.in Makefile
95 d=zlib-`sed -n '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`;\
96 rm -f $$d.tar.gz; \
97 if test ! -d ../$$d; then rm -f ../$$d; ln -s `pwd` ../$$d; fi; \
98 files=""; \
99 for f in $(DISTFILES); do files="$$files $$d/$$f"; done; \
100 cd ..; \
101 GZIP=-9 $(TAR) chofz $$d/$$d.tar.gz $$files; \
102 if test ! -d $$d; then rm -f $$d; fi
103 mv Makefile~ Makefile
104
105tags:
106 etags *.[ch]
107
108depend:
109 makedepend -- $(CFLAGS) -- *.[ch]
110
111# DO NOT DELETE THIS LINE -- make depend depends on it.
112
113adler32.o: zlib.h zconf.h
114compress.o: zlib.h zconf.h
115crc32.o: zlib.h zconf.h
116deflate.o: deflate.h zutil.h zlib.h zconf.h
117example.o: zlib.h zconf.h
118gzio.o: zutil.h zlib.h zconf.h
119infblock.o: infblock.h inftrees.h infcodes.h infutil.h zutil.h zlib.h zconf.h
120infcodes.o: zutil.h zlib.h zconf.h
121infcodes.o: inftrees.h infblock.h infcodes.h infutil.h inffast.h
122inffast.o: zutil.h zlib.h zconf.h inftrees.h
123inffast.o: infblock.h infcodes.h infutil.h inffast.h
124inflate.o: zutil.h zlib.h zconf.h infblock.h
125inftrees.o: zutil.h zlib.h zconf.h inftrees.h
126infutil.o: zutil.h zlib.h zconf.h infblock.h inftrees.h infcodes.h infutil.h
127minigzip.o: zlib.h zconf.h
128trees.o: deflate.h zutil.h zlib.h zconf.h
129uncompr.o: zlib.h zconf.h
130zutil.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/README b/README
index 1810978..0d41909 100644
--- a/README
+++ b/README
@@ -1,40 +1,75 @@
1zlib 0.95 is a beta version of a general purpose compression library. 1zlib 1.0 is a general purpose data compression library. All the code
2This should be the last version before the first official 2is reentrant (thread safe). The data format used by the zlib library
3version (1.0), so please test it now. (At least compile and run it with 3is described in the files zlib-3.3.doc, deflate-1.3.doc and
4"make test"). This version has no known bugs. 4gzip-4.3.doc, available in ftp://ftp.uu.net/pub/archiving/zip/doc/
5
6The data format used by the zlib library is described in the
7files zlib-3.1.doc, deflate-1.1.doc and gzip-4.1.doc, available
8in ftp.uu.net:/pub/archiving/zip/doc.
9 5
10All functions of the compression library are documented in the file 6All functions of the compression library are documented in the file
11zlib.h. A usage example of the library is given in the file example.c 7zlib.h. A usage example of the library is given in the file example.c
12which also tests that the library is working correctly. Another 8which also tests that the library is working correctly. Another
13example is given in the file minigzip.c. 9example is given in the file minigzip.c. The compression library itself
14 10is composed of all source files except example.c and minigzip.c.
15To compile all files and run the test program, just type: make test 11
16(For MSDOS, use one of the special makefiles such as Makefile.msc; 12To compile all files and run the test program, follow the instructions
17for VMS, use Make_vms.com or descrip.mms.) 13given at the top of Makefile. In short "make test; make install"
18To install the zlib library (libz.a) in /usr/local/lib, type: make install 14should work for most machines. For MSDOS, use one of the special
19To install in a different directory, use for example: 15makefiles such as Makefile.msc; for VMS, use Make_vms.com or descrip.mms.
20 make install prefix=$HOME 16
21This will install in $HOME/lib instead of /usr/local/lib. 17Questions about zlib should be sent to <zlib@quest.jpl.nasa.gov> or,
22 18if this fails, to the addresses given below in the Copyright section.
23The changes made in version 0.95 are documented in the file ChangeLog. 19
24The main changes since 0.94 are: 20The changes made in version 1.0 are documented in the file ChangeLog.
25- fix MSDOS small and medium model (now easier to adapt to any compiler) 21The main changes since 0.95 are:
26- inlined send_bits 22- allow preset dictionary shared between compressor and decompressor
27- fix the final (:-) bug for deflate with flush (output was correct but 23- allow compression level 0 (no compression)
28 not completely flushed in rare occasions). 24- add deflateParams in zlib.h: allow dynamic change of compression level
29 25 and compression strategy.
30For MSDOS, the small and medium models have been tested only with Microsoft C. 26- test large buffers and deflateParams in example.c
31(This should now work for Borland C also, but I don't have a recent Borland 27- add optional "configure" to build zlib as a shared library
32compiler to test with.) The small model was tested with Turbo C but only with 28- suppress Makefile.qnx, use configure instead
33reduced performance to avoid any far allocation; it was tested with 29- fixes for 64-bit systems (needed for Alpha and Cray)
34 -DMAX_WBITS=11 -DMAX_MEM_LEVEL=3 30- declare Z_DEFLATED in zlib.h (possible parameter for deflateInit2)
35 31- always return Z_BUF_ERROR when deflate() has nothing to do
36 32- deflateInit and inflateInit are now macros to allow version checking
37 (C) 1995 Jean-loup Gailly and Mark Adler 33- prefix all global functions and types with z_ with -DZ_PREFIX
34- fix some very unlikely race conditions in multi-threaded environment
35- several minor fixes for better portability
36- free in reverse order of allocation to help memory manager
37- use zlib-1.0/* instead of zlib/* inside the tar.gz
38- make zlib warning-free with "gcc -O3 -Wall -Wwrite-strings -Wpointer-arith
39 -Wconversion -Wstrict-prototypes -Wmissing-prototypes"
40- allow gzread on concatenated .gz files
41- deflateEnd now returns Z_DATA_ERROR if it was premature
42- deflate is finally (?) fully deterministic
43
44Notes for some targets:
45
46- For Turbo C the small model is supported only with reduced performance to
47 avoid any far allocation; it was tested with -DMAX_WBITS=11 -DMAX_MEM_LEVEL=3
48
49- 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
51 -32 compiler flag).
52
53- zlib doesn't work with gcc 2.6.3 on a DEC 3000/300LX under OSF/1 2.1
54 it works when compiled with cc.
55
56- zlib doesn't work on HP-UX 9.05 with one cc compiler (the one not
57 accepting the -O option). It works with the other cc compiler.
58
59A Perl interface to zlib written by Paul Marquess <pmarquess@bfsec.bt.co.uk>
60is in the CPAN (Comprehensive Perl Archive Network) sites, such as:
61ftp://ftp.cis.ufl.edu/pub/perl/CPAN/modules/by-module/Compress/Compress-Zlib*
62
63Acknowledgments:
64
65 The deflate format used by zlib was defined by Phil Katz. The deflate
66 and zlib specifications were written by Peter Deutsch. Thanks to all the
67 people who reported problems and suggested various improvements in zlib;
68 they are too numerous to cite here.
69
70Copyright notice:
71
72 (C) 1995-1996 Jean-loup Gailly and Mark Adler
38 73
39 This software is provided 'as-is', without any express or implied 74 This software is provided 'as-is', without any express or implied
40 warranty. In no event will the authors be held liable for any damages 75 warranty. In no event will the authors be held liable for any damages
diff --git a/adler32.c b/adler32.c
index 7e2e3f5..73f820a 100644
--- a/adler32.c
+++ b/adler32.c
@@ -1,5 +1,5 @@
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
@@ -20,7 +20,7 @@
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;
diff --git a/compress.c b/compress.c
index a541af4..be9ef64 100644
--- a/compress.c
+++ b/compress.c
@@ -1,5 +1,5 @@
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
@@ -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;
@@ -39,6 +39,7 @@ int compress (dest, destLen, source, sourceLen)
39 39
40 stream.zalloc = (alloc_func)0; 40 stream.zalloc = (alloc_func)0;
41 stream.zfree = (free_func)0; 41 stream.zfree = (free_func)0;
42 stream.opaque = (voidpf)0;
42 43
43 err = deflateInit(&stream, Z_DEFAULT_COMPRESSION); 44 err = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
44 if (err != Z_OK) return err; 45 if (err != Z_OK) return err;
diff --git a/configure b/configure
new file mode 100755
index 0000000..6f3c729
--- /dev/null
+++ b/configure
@@ -0,0 +1,81 @@
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"}
16prefix=${prefix-/usr/local}
17exec_prefix=${exec_prefix-$prefix}
18
19test -z "$CC" && echo Checking for gcc...
20test=ztest$$
21cat > $test.c <<EOF
22int hello() { printf("hello\n"); }
23EOF
24if test -z "$CC" && (gcc -c -O3 $test.c) 2>/dev/null; then
25 CC=gcc
26 SFLAGS=${CFLAGS-"-fPIC -O3"}
27 CFLAGS=${CFLAGS-"-O3"}
28 LDSHARED=${LDSHARED-"gcc -shared"}
29else
30 # find system name and corresponding cc options
31 CC=${CC-cc}
32 case `(uname -sr || echo unknown) 2>/dev/null` in
33 SunOS\ 5*) SFLAGS=${CFLAGS-"-fast -xcg89 -KPIC -R."}
34 CFLAGS=${CFLAGS-"-fast -xcg89"}
35 LDSHARED=${LDSHARED-"cc -G"};;
36 SunOS\ 4*) SFLAGS=${CFLAGS-"-O2 -PIC"}
37 CFLAGS=${CFLAGS-"-O2"}
38 LDSHARED=${LDSHARED-"ld"};;
39 IRIX*) SFLAGS=${CFLAGS-"-O2 -rpath ."}
40 CFLAGS=${CFLAGS-"-O2"}
41 LDSHARED=${LDSHARED-"cc -shared"};;
42 QNX*) SFLAGS=${CFLAGS-"-4 -O -Q"}
43 CFLAGS=${CFLAGS-"-4 -O -Q"}
44 LDSHARED=${LDSHARED-"cc"}
45 LIBS=zlib.lib
46 AR="cc -A";;
47 # send working options for other systems to gzip@prep.ai.mit.edu
48 *) SFLAGS=${CFLAGS-"-O"}
49 CFLAGS=${CFLAGS-"-O"}
50 LDSHARED=${LDSHARED-"-shared"};;
51 esac
52fi
53
54echo Checking for shared library support...
55# we must test in two steps (cc then ld), required at least on SunOS 4.x
56if test "`$CC -c $SFLAGS $test.c 2>&1`" = "" &&
57 test "`$LDSHARED -o $test.so $test.o 2>&1`" = ""; then
58 CFLAGS="$SFLAGS"
59 LIBS='libz.so.$(VER)'
60 echo Building shared library libz.so.$VER with $CC.
61else
62 LDSHARED="$CC"
63 echo Building static library $LIBS version $VER with $CC.
64fi
65rm -f $test.[co] $test.so
66
67# udpate Makefile
68# ed -s Makefile <<EOF
69sed < Makefile.in "
70/^CC *=/s/=.*/=$CC/
71/^CFLAGS *=/s/=.*/=$CFLAGS/
72/^LDSHARED *=/s/=.*/=$LDSHARED/
73/^LIBS *=/s,=.*,=$LIBS,
74/^AR *=/s/=.*/=$AR/
75/^VER *=/s/=.*/=$VER/
76/^prefix *=/s,=.*,=$prefix,
77/^exec_prefix *=/s,=.*,=$exec_prefix,
78" > Makefile
79#w
80#q
81#EOF
diff --git a/crc32.c b/crc32.c
index 4089255..7d6aa02 100644
--- a/crc32.c
+++ b/crc32.c
@@ -1,5 +1,5 @@
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
@@ -10,23 +10,53 @@
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 c531856..31b090e 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
@@ -51,7 +51,7 @@
51 51
52#include "deflate.h" 52#include "deflate.h"
53 53
54char copyright[] = " deflate Copyright 1995 Jean-loup Gailly "; 54char deflate_copyright[] = " deflate 1.0 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
@@ -59,6 +59,31 @@ char copyright[] = " deflate Copyright 1995 Jean-loup Gailly ";
59 copyright string in the executable of your product. 59 copyright string in the executable of your product.
60 */ 60 */
61 61
62/* ===========================================================================
63 * Function prototypes.
64 */
65local void fill_window OF((deflate_state *s));
66local int deflate_stored OF((deflate_state *s, int flush));
67local int deflate_fast OF((deflate_state *s, int flush));
68local int deflate_slow OF((deflate_state *s, int flush));
69local void lm_init OF((deflate_state *s));
70local int longest_match OF((deflate_state *s, IPos cur_match));
71local void putShortMSB OF((deflate_state *s, uInt b));
72local void flush_pending OF((z_stream *strm));
73local int read_buf OF((z_stream *strm, charf *buf, unsigned size));
74#ifdef ASMV
75 void match_init OF((void)); /* asm code initialization */
76#endif
77
78#ifdef DEBUG
79local void check_match OF((deflate_state *s, IPos start, IPos match,
80 int length));
81#endif
82
83/* ===========================================================================
84 * Local data
85 */
86
62#define NIL 0 87#define NIL 0
63/* Tail of hash chains */ 88/* Tail of hash chains */
64 89
@@ -72,32 +97,35 @@ char copyright[] = " deflate Copyright 1995 Jean-loup Gailly ";
72 * See deflate.c for comments about the MIN_MATCH+1. 97 * See deflate.c for comments about the MIN_MATCH+1.
73 */ 98 */
74 99
100typedef int (*compress_func) OF((deflate_state *s, int flush));
101/* Compressing function */
102
75/* Values for max_lazy_match, good_match and max_chain_length, depending on 103/* Values for max_lazy_match, good_match and max_chain_length, depending on
76 * the desired pack level (0..9). The values given below have been tuned to 104 * the desired pack level (0..9). The values given below have been tuned to
77 * exclude worst case performance for pathological files. Better values may be 105 * exclude worst case performance for pathological files. Better values may be
78 * found for specific files. 106 * found for specific files.
79 */ 107 */
80
81typedef struct config_s { 108typedef struct config_s {
82 ush good_length; /* reduce lazy search above this match length */ 109 ush good_length; /* reduce lazy search above this match length */
83 ush max_lazy; /* do not perform lazy search above this match length */ 110 ush max_lazy; /* do not perform lazy search above this match length */
84 ush nice_length; /* quit search above this match length */ 111 ush nice_length; /* quit search above this match length */
85 ush max_chain; 112 ush max_chain;
113 compress_func func;
86} config; 114} config;
87 115
88local config configuration_table[10] = { 116local config configuration_table[10] = {
89/* good lazy nice chain */ 117/* good lazy nice chain */
90/* 0 */ {0, 0, 0, 0}, /* store only */ 118/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
91/* 1 */ {4, 4, 8, 4}, /* maximum speed, no lazy matches */ 119/* 1 */ {4, 4, 8, 4, deflate_fast}, /* maximum speed, no lazy matches */
92/* 2 */ {4, 5, 16, 8}, 120/* 2 */ {4, 5, 16, 8, deflate_fast},
93/* 3 */ {4, 6, 32, 32}, 121/* 3 */ {4, 6, 32, 32, deflate_fast},
94 122
95/* 4 */ {4, 4, 16, 16}, /* lazy matches */ 123/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
96/* 5 */ {8, 16, 32, 32}, 124/* 5 */ {8, 16, 32, 32, deflate_slow},
97/* 6 */ {8, 16, 128, 128}, 125/* 6 */ {8, 16, 128, 128, deflate_slow},
98/* 7 */ {8, 32, 128, 256}, 126/* 7 */ {8, 32, 128, 256, deflate_slow},
99/* 8 */ {32, 128, 258, 1024}, 127/* 8 */ {32, 128, 258, 1024, deflate_slow},
100/* 9 */ {32, 258, 258, 4096}}; /* maximum compression */ 128/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
101 129
102/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 130/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
103 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different 131 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
@@ -110,28 +138,6 @@ local config configuration_table[10] = {
110struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ 138struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
111 139
112/* =========================================================================== 140/* ===========================================================================
113 * Prototypes for local functions.
114 */
115
116local void fill_window OF((deflate_state *s));
117local int deflate_fast OF((deflate_state *s, int flush));
118local int deflate_slow OF((deflate_state *s, int flush));
119local void lm_init OF((deflate_state *s));
120local int longest_match OF((deflate_state *s, IPos cur_match));
121local void putShortMSB OF((deflate_state *s, uInt b));
122local void flush_pending OF((z_stream *strm));
123local int read_buf OF((z_stream *strm, charf *buf, unsigned size));
124#ifdef ASMV
125 void match_init OF((void)); /* asm code initialization */
126#endif
127
128#ifdef DEBUG
129local void check_match OF((deflate_state *s, IPos start, IPos match,
130 int length));
131#endif
132
133
134/* ===========================================================================
135 * Update a hash value with the given input byte 141 * Update a hash value with the given input byte
136 * IN assertion: all calls to to UPDATE_HASH are made with consecutive 142 * IN assertion: all calls to to UPDATE_HASH are made with consecutive
137 * input characters, so that a running hash key can be computed from the 143 * input characters, so that a running hash key can be computed from the
@@ -162,30 +168,43 @@ local void check_match OF((deflate_state *s, IPos start, IPos match,
162 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));
163 169
164/* ========================================================================= */ 170/* ========================================================================= */
165int deflateInit (strm, level) 171int deflateInit_(strm, level, version, stream_size)
166 z_stream *strm; 172 z_stream *strm;
167 int level; 173 int level;
174 const char *version;
175 int stream_size;
168{ 176{
169 return deflateInit2 (strm, level, 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);
170 /* 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 */
171} 180}
172 181
173/* ========================================================================= */ 182/* ========================================================================= */
174int deflateInit2 (strm, level, method, windowBits, memLevel, strategy) 183int deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
184 version, stream_size)
175 z_stream *strm; 185 z_stream *strm;
176 int level; 186 int level;
177 int method; 187 int method;
178 int windowBits; 188 int windowBits;
179 int memLevel; 189 int memLevel;
180 int strategy; 190 int strategy;
191 const char *version;
192 int stream_size;
181{ 193{
182 deflate_state *s; 194 deflate_state *s;
183 int noheader = 0; 195 int noheader = 0;
184 196
197 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
198 stream_size != sizeof(z_stream)) {
199 return Z_VERSION_ERROR;
200 }
185 if (strm == Z_NULL) return Z_STREAM_ERROR; 201 if (strm == Z_NULL) return Z_STREAM_ERROR;
186 202
187 strm->msg = Z_NULL; 203 strm->msg = Z_NULL;
188 if (strm->zalloc == Z_NULL) strm->zalloc = zcalloc; 204 if (strm->zalloc == Z_NULL) {
205 strm->zalloc = zcalloc;
206 strm->opaque = (voidpf)0;
207 }
189 if (strm->zfree == Z_NULL) strm->zfree = zcfree; 208 if (strm->zfree == Z_NULL) strm->zfree = zcfree;
190 209
191 if (level == Z_DEFAULT_COMPRESSION) level = 6; 210 if (level == Z_DEFAULT_COMPRESSION) level = 6;
@@ -194,8 +213,9 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
194 noheader = 1; 213 noheader = 1;
195 windowBits = -windowBits; 214 windowBits = -windowBits;
196 } 215 }
197 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != DEFLATED || 216 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
198 windowBits < 8 || windowBits > 15 || level < 1 || level > 9) { 217 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
218 strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
199 return Z_STREAM_ERROR; 219 return Z_STREAM_ERROR;
200 } 220 }
201 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); 221 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
@@ -223,15 +243,15 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
223 243
224 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 244 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
225 s->pending_buf == Z_NULL) { 245 s->pending_buf == Z_NULL) {
226 strm->msg = z_errmsg[1-Z_MEM_ERROR]; 246 strm->msg = ERR_MSG(Z_MEM_ERROR);
227 deflateEnd (strm); 247 deflateEnd (strm);
228 return Z_MEM_ERROR; 248 return Z_MEM_ERROR;
229 } 249 }
230 s->d_buf = (ushf *) &(s->pending_buf[s->lit_bufsize]); 250 s->l_buf = (uchf *) &(s->pending_buf[s->lit_bufsize]);
231 s->l_buf = (uchf *) &(s->pending_buf[3*s->lit_bufsize]); 251 s->d_buf = (ushf *) &(s->pending_buf[2*s->lit_bufsize]);
232 /* We overlay pending_buf and d_buf+l_buf. This works since the average 252 /* We overlay pending_buf and d_buf+l_buf. This works since the average
233 * output size for (length,distance) codes is <= 32 bits (worst case 253 * output size for (length,distance) codes is <= 32 bits (worst case
234 * is 15+15+13=33). 254 * is 15+15+13=33). d_buf is put last in case sizeof(short)>2.
235 */ 255 */
236 256
237 s->level = level; 257 s->level = level;
@@ -242,6 +262,44 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
242} 262}
243 263
244/* ========================================================================= */ 264/* ========================================================================= */
265int deflateSetDictionary (strm, dictionary, dictLength)
266 z_stream *strm;
267 const Bytef *dictionary;
268 uInt dictLength;
269{
270 deflate_state *s;
271 uInt length = dictLength;
272 uInt n;
273 IPos hash_head;
274
275 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
276 strm->state->status != INIT_STATE) return Z_STREAM_ERROR;
277
278 s = strm->state;
279 strm->adler = adler32(strm->adler, dictionary, dictLength);
280
281 if (length < MIN_MATCH) return Z_OK;
282 if (length > MAX_DIST(s)) {
283 length = MAX_DIST(s);
284 dictionary += dictLength - length;
285 }
286 zmemcpy((charf *)s->window, dictionary, length);
287 s->strstart = length;
288 s->block_start = (long)length;
289
290 /* Insert all strings in the hash table (except for the last two bytes).
291 * s->lookahead stays null, so s->ins_h will be recomputed at the next
292 * call of fill_window.
293 */
294 s->ins_h = s->window[0];
295 UPDATE_HASH(s, s->ins_h, s->window[1]);
296 for (n = 0; n <= length - MIN_MATCH; n++) {
297 INSERT_STRING(s, n, hash_head);
298 }
299 return Z_OK;
300}
301
302/* ========================================================================= */
245int deflateReset (strm) 303int deflateReset (strm)
246 z_stream *strm; 304 z_stream *strm;
247{ 305{
@@ -262,14 +320,52 @@ int deflateReset (strm)
262 s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */ 320 s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
263 } 321 }
264 s->status = s->noheader ? BUSY_STATE : INIT_STATE; 322 s->status = s->noheader ? BUSY_STATE : INIT_STATE;
265 s->adler = 1; 323 strm->adler = 1;
324 s->last_flush = Z_NO_FLUSH;
266 325
267 ct_init(s); 326 _tr_init(s);
268 lm_init(s); 327 lm_init(s);
269 328
270 return Z_OK; 329 return Z_OK;
271} 330}
272 331
332/* ========================================================================= */
333int deflateParams(strm, level, strategy)
334 z_stream *strm;
335 int level;
336 int strategy;
337{
338 deflate_state *s;
339 compress_func func;
340
341 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
342 s = strm->state;
343
344 if (level == Z_DEFAULT_COMPRESSION) {
345 level = 6;
346 }
347 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
348 return Z_STREAM_ERROR;
349 }
350 func = configuration_table[s->level].func;
351
352 if (func != configuration_table[level].func
353 && strm->state->lookahead != 0) {
354
355 /* Flush the last buffer: */
356 (void)(*func)(strm->state, Z_PARTIAL_FLUSH);
357 }
358 if (s->level != level) {
359 s->level = level;
360 s->max_lazy_match = configuration_table[level].max_lazy;
361 s->good_match = configuration_table[level].good_length;
362 s->nice_match = configuration_table[level].nice_length;
363 s->max_chain_length = configuration_table[level].max_chain;
364 }
365 s->strategy = strategy;
366 return Z_OK;
367}
368
273/* ========================================================================= 369/* =========================================================================
274 * Put a short in the pending buffer. The 16-bit value is put in MSB order. 370 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
275 * IN assertion: the stream state is correct and there is enough room in 371 * IN assertion: the stream state is correct and there is enough room in
@@ -284,7 +380,10 @@ local void putShortMSB (s, b)
284} 380}
285 381
286/* ========================================================================= 382/* =========================================================================
287 * Flush as much pending output as possible. 383 * Flush as much pending output as possible. All deflate() output goes
384 * through this function so some applications may wish to modify it
385 * to avoid allocating a large strm->next_out buffer and copying into it.
386 * (See also read_buf()).
288 */ 387 */
289local void flush_pending(strm) 388local void flush_pending(strm)
290 z_stream *strm; 389 z_stream *strm;
@@ -310,55 +409,76 @@ int deflate (strm, flush)
310 z_stream *strm; 409 z_stream *strm;
311 int flush; 410 int flush;
312{ 411{
412 int old_flush; /* value of flush param for previous deflate call */
413 deflate_state *s;
414
313 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 415 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
314 416
417 s = strm->state;
418
315 if (strm->next_out == Z_NULL || 419 if (strm->next_out == Z_NULL ||
316 (strm->next_in == Z_NULL && strm->avail_in != 0)) { 420 (strm->next_in == Z_NULL && strm->avail_in != 0) ||
421 (s->status == FINISH_STATE && flush != Z_FINISH)) {
317 ERR_RETURN(strm, Z_STREAM_ERROR); 422 ERR_RETURN(strm, Z_STREAM_ERROR);
318 } 423 }
319 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); 424 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
320 425
321 strm->state->strm = strm; /* just in case */ 426 s->strm = strm; /* just in case */
427 old_flush = s->last_flush;
428 s->last_flush = flush;
322 429
323 /* Write the zlib header */ 430 /* Write the zlib header */
324 if (strm->state->status == INIT_STATE) { 431 if (s->status == INIT_STATE) {
325 432
326 uInt header = (DEFLATED + ((strm->state->w_bits-8)<<4)) << 8; 433 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
327 uInt level_flags = (strm->state->level-1) >> 1; 434 uInt level_flags = (s->level-1) >> 1;
328 435
329 if (level_flags > 3) level_flags = 3; 436 if (level_flags > 3) level_flags = 3;
330 header |= (level_flags << 6); 437 header |= (level_flags << 6);
438 if (s->strstart != 0) header |= PRESET_DICT;
331 header += 31 - (header % 31); 439 header += 31 - (header % 31);
332 440
333 strm->state->status = BUSY_STATE; 441 s->status = BUSY_STATE;
334 putShortMSB(strm->state, header); 442 putShortMSB(s, header);
443
444 /* Save the adler32 of the preset dictionary: */
445 if (s->strstart != 0) {
446 putShortMSB(s, (uInt)(strm->adler >> 16));
447 putShortMSB(s, (uInt)(strm->adler & 0xffff));
448 strm->adler = 1L;
449 }
335 } 450 }
336 451
337 /* Flush as much pending output as possible */ 452 /* Flush as much pending output as possible */
338 if (strm->state->pending != 0) { 453 if (s->pending != 0) {
339 flush_pending(strm); 454 flush_pending(strm);
340 if (strm->avail_out == 0) return Z_OK; 455 if (strm->avail_out == 0) return Z_OK;
456
457 /* Make sure there is something to do and avoid duplicate consecutive
458 * flushes. For repeated and useless calls with Z_FINISH, we keep
459 * returning Z_STREAM_END instead of Z_BUFF_ERROR.
460 */
461 } else if (strm->avail_in == 0 && flush <= old_flush &&
462 flush != Z_FINISH) {
463 ERR_RETURN(strm, Z_BUF_ERROR);
341 } 464 }
342 465
343 /* User must not provide more input after the first FINISH: */ 466 /* User must not provide more input after the first FINISH: */
344 if (strm->state->status == FINISH_STATE && strm->avail_in != 0) { 467 if (s->status == FINISH_STATE && strm->avail_in != 0) {
345 ERR_RETURN(strm, Z_BUF_ERROR); 468 ERR_RETURN(strm, Z_BUF_ERROR);
346 } 469 }
347 470
348 /* Start a new block or continue the current one. 471 /* Start a new block or continue the current one.
349 */ 472 */
350 if (strm->avail_in != 0 || strm->state->lookahead != 0 || 473 if (strm->avail_in != 0 || s->lookahead != 0 ||
351 (flush != Z_NO_FLUSH && strm->state->status != FINISH_STATE)) { 474 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
352 int quit; 475 int quit;
353 476
354 if (flush == Z_FINISH) { 477 if (flush == Z_FINISH) {
355 strm->state->status = FINISH_STATE; 478 s->status = FINISH_STATE;
356 }
357 if (strm->state->level <= 3) {
358 quit = deflate_fast(strm->state, flush);
359 } else {
360 quit = deflate_slow(strm->state, flush);
361 } 479 }
480 quit = (*(configuration_table[s->level].func))(s, flush);
481
362 if (quit || strm->avail_out == 0) return Z_OK; 482 if (quit || strm->avail_out == 0) return Z_OK;
363 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call 483 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
364 * of deflate should use the same flush parameter to make sure 484 * of deflate should use the same flush parameter to make sure
@@ -367,16 +487,16 @@ int deflate (strm, flush)
367 * ensures that for a very small output buffer, we emit at most 487 * ensures that for a very small output buffer, we emit at most
368 * one empty block. 488 * one empty block.
369 */ 489 */
370 if (flush != Z_OK && flush != Z_FINISH) { 490 if (flush != Z_NO_FLUSH && flush != Z_FINISH) {
371 if (flush == Z_PARTIAL_FLUSH) { 491 if (flush == Z_PARTIAL_FLUSH) {
372 ct_align(strm->state); 492 _tr_align(s);
373 } else { /* FULL_FLUSH or SYNC_FLUSH */ 493 } else { /* FULL_FLUSH or SYNC_FLUSH */
374 ct_stored_block(strm->state, (char*)0, 0L, 0); 494 _tr_stored_block(s, (char*)0, 0L, 0);
375 /* For a full flush, this empty block will be recognized 495 /* For a full flush, this empty block will be recognized
376 * as a special marker by inflate_sync(). 496 * as a special marker by inflate_sync().
377 */ 497 */
378 if (flush == Z_FULL_FLUSH) { 498 if (flush == Z_FULL_FLUSH) {
379 CLEAR_HASH(strm->state); /* forget history */ 499 CLEAR_HASH(s); /* forget history */
380 } 500 }
381 } 501 }
382 flush_pending(strm); 502 flush_pending(strm);
@@ -386,34 +506,38 @@ int deflate (strm, flush)
386 Assert(strm->avail_out > 0, "bug2"); 506 Assert(strm->avail_out > 0, "bug2");
387 507
388 if (flush != Z_FINISH) return Z_OK; 508 if (flush != Z_FINISH) return Z_OK;
389 if (strm->state->noheader) return Z_STREAM_END; 509 if (s->noheader) return Z_STREAM_END;
390 510
391 /* Write the zlib trailer (adler32) */ 511 /* Write the zlib trailer (adler32) */
392 putShortMSB(strm->state, (uInt)(strm->state->adler >> 16)); 512 putShortMSB(s, (uInt)(strm->adler >> 16));
393 putShortMSB(strm->state, (uInt)(strm->state->adler & 0xffff)); 513 putShortMSB(s, (uInt)(strm->adler & 0xffff));
394 flush_pending(strm); 514 flush_pending(strm);
395 /* If avail_out is zero, the application will call deflate again 515 /* If avail_out is zero, the application will call deflate again
396 * to flush the rest. 516 * to flush the rest.
397 */ 517 */
398 strm->state->noheader = -1; /* write the trailer only once! */ 518 s->noheader = -1; /* write the trailer only once! */
399 return strm->state->pending != 0 ? Z_OK : Z_STREAM_END; 519 return s->pending != 0 ? Z_OK : Z_STREAM_END;
400} 520}
401 521
402/* ========================================================================= */ 522/* ========================================================================= */
403int deflateEnd (strm) 523int deflateEnd (strm)
404 z_stream *strm; 524 z_stream *strm;
405{ 525{
526 int status;
527
406 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 528 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
407 529
408 TRY_FREE(strm, strm->state->window); 530 /* Deallocate in reverse order of allocations: */
409 TRY_FREE(strm, strm->state->prev);
410 TRY_FREE(strm, strm->state->head);
411 TRY_FREE(strm, strm->state->pending_buf); 531 TRY_FREE(strm, strm->state->pending_buf);
532 TRY_FREE(strm, strm->state->head);
533 TRY_FREE(strm, strm->state->prev);
534 TRY_FREE(strm, strm->state->window);
412 535
536 status = strm->state->status;
413 ZFREE(strm, strm->state); 537 ZFREE(strm, strm->state);
414 strm->state = Z_NULL; 538 strm->state = Z_NULL;
415 539
416 return Z_OK; 540 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
417} 541}
418 542
419/* ========================================================================= */ 543/* ========================================================================= */
@@ -438,7 +562,10 @@ int deflateCopy (dest, source)
438 562
439/* =========================================================================== 563/* ===========================================================================
440 * Read a new buffer from the current input stream, update the adler32 564 * Read a new buffer from the current input stream, update the adler32
441 * and total number of bytes read. 565 * and total number of bytes read. All deflate() input goes through
566 * this function so some applications may wish to modify it to avoid
567 * allocating a large strm->next_in buffer and copying from it.
568 * (See also flush_pending()).
442 */ 569 */
443local int read_buf(strm, buf, size) 570local int read_buf(strm, buf, size)
444 z_stream *strm; 571 z_stream *strm;
@@ -453,7 +580,7 @@ local int read_buf(strm, buf, size)
453 strm->avail_in -= len; 580 strm->avail_in -= len;
454 581
455 if (!strm->state->noheader) { 582 if (!strm->state->noheader) {
456 strm->state->adler = adler32(strm->state->adler, strm->next_in, len); 583 strm->adler = adler32(strm->adler, strm->next_in, len);
457 } 584 }
458 zmemcpy(buf, strm->next_in, len); 585 zmemcpy(buf, strm->next_in, len);
459 strm->next_in += len; 586 strm->next_in += len;
@@ -482,7 +609,7 @@ local void lm_init (s)
482 s->strstart = 0; 609 s->strstart = 0;
483 s->block_start = 0L; 610 s->block_start = 0L;
484 s->lookahead = 0; 611 s->lookahead = 0;
485 s->match_length = MIN_MATCH-1; 612 s->match_length = s->prev_length = MIN_MATCH-1;
486 s->match_available = 0; 613 s->match_available = 0;
487 s->ins_h = 0; 614 s->ins_h = 0;
488#ifdef ASMV 615#ifdef ASMV
@@ -497,6 +624,7 @@ local void lm_init (s)
497 * garbage. 624 * garbage.
498 * IN assertions: cur_match is the head of the hash chain for the current 625 * IN assertions: cur_match is the head of the hash chain for the current
499 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 626 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
627 * OUT assertion: the match length is not greater than s->lookahead.
500 */ 628 */
501#ifndef ASMV 629#ifndef ASMV
502/* For 80x86 and 680x0, an optimized version will be provided in match.asm or 630/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
@@ -511,6 +639,7 @@ local int longest_match(s, cur_match)
511 register Bytef *match; /* matched string */ 639 register Bytef *match; /* matched string */
512 register int len; /* length of current match */ 640 register int len; /* length of current match */
513 int best_len = s->prev_length; /* best match length so far */ 641 int best_len = s->prev_length; /* best match length so far */
642 int nice_match = s->nice_match; /* stop if match long enough */
514 IPos limit = s->strstart > (IPos)MAX_DIST(s) ? 643 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
515 s->strstart - (IPos)MAX_DIST(s) : NIL; 644 s->strstart - (IPos)MAX_DIST(s) : NIL;
516 /* Stop when cur_match becomes <= limit. To simplify the code, 645 /* Stop when cur_match becomes <= limit. To simplify the code,
@@ -541,6 +670,11 @@ local int longest_match(s, cur_match)
541 if (s->prev_length >= s->good_match) { 670 if (s->prev_length >= s->good_match) {
542 chain_length >>= 2; 671 chain_length >>= 2;
543 } 672 }
673 /* Do not look for matches beyond the end of the input. This is necessary
674 * to make deflate deterministic.
675 */
676 if (nice_match > s->lookahead) nice_match = s->lookahead;
677
544 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 678 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
545 679
546 do { 680 do {
@@ -619,7 +753,7 @@ local int longest_match(s, cur_match)
619 if (len > best_len) { 753 if (len > best_len) {
620 s->match_start = cur_match; 754 s->match_start = cur_match;
621 best_len = len; 755 best_len = len;
622 if (len >= s->nice_match) break; 756 if (len >= nice_match) break;
623#ifdef UNALIGNED_OK 757#ifdef UNALIGNED_OK
624 scan_end = *(ushf*)(scan+best_len-1); 758 scan_end = *(ushf*)(scan+best_len-1);
625#else 759#else
@@ -630,7 +764,8 @@ local int longest_match(s, cur_match)
630 } while ((cur_match = prev[cur_match & wmask]) > limit 764 } while ((cur_match = prev[cur_match & wmask]) > limit
631 && --chain_length != 0); 765 && --chain_length != 0);
632 766
633 return best_len; 767 if (best_len <= s->lookahead) return best_len;
768 return s->lookahead;
634} 769}
635#endif /* ASMV */ 770#endif /* ASMV */
636 771
@@ -644,13 +779,13 @@ local void check_match(s, start, match, length)
644 int length; 779 int length;
645{ 780{
646 /* check that the match is indeed a match */ 781 /* check that the match is indeed a match */
647 if (memcmp((charf *)s->window + match, 782 if (zmemcmp((charf *)s->window + match,
648 (charf *)s->window + start, length) != EQUAL) { 783 (charf *)s->window + start, length) != EQUAL) {
649 fprintf(stderr, 784 fprintf(stderr, " start %u, match %u, length %d\n",
650 " start %u, match %u, length %d\n", 785 start, match, length);
651 start, match, length); 786 do {
652 do { fprintf(stderr, "%c%c", s->window[match++], 787 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
653 s->window[start++]); } while (--length != 0); 788 } while (--length != 0);
654 z_error("invalid match"); 789 z_error("invalid match");
655 } 790 }
656 if (verbose > 1) { 791 if (verbose > 1) {
@@ -686,6 +821,7 @@ local void fill_window(s)
686 /* Deal with !@#$% 64K limit: */ 821 /* Deal with !@#$% 64K limit: */
687 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { 822 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
688 more = wsize; 823 more = wsize;
824
689 } else if (more == (unsigned)(-1)) { 825 } else if (more == (unsigned)(-1)) {
690 /* Very unlikely, but possible on 16 bit machine if strstart == 0 826 /* Very unlikely, but possible on 16 bit machine if strstart == 0
691 * and lookahead == 1 (input done one byte at time) 827 * and lookahead == 1 (input done one byte at time)
@@ -697,9 +833,6 @@ local void fill_window(s)
697 */ 833 */
698 } else if (s->strstart >= wsize+MAX_DIST(s)) { 834 } else if (s->strstart >= wsize+MAX_DIST(s)) {
699 835
700 /* By the IN assertion, the window is not empty so we can't confuse
701 * more == 0 with more == 64K on a 16 bit machine.
702 */
703 zmemcpy((charf *)s->window, (charf *)s->window+wsize, 836 zmemcpy((charf *)s->window, (charf *)s->window+wsize,
704 (unsigned)wsize); 837 (unsigned)wsize);
705 s->match_start -= wsize; 838 s->match_start -= wsize;
@@ -768,9 +901,11 @@ local void fill_window(s)
768 * IN assertion: strstart is set to the end of the current match. 901 * IN assertion: strstart is set to the end of the current match.
769 */ 902 */
770#define FLUSH_BLOCK_ONLY(s, eof) { \ 903#define FLUSH_BLOCK_ONLY(s, eof) { \
771 ct_flush_block(s, (s->block_start >= 0L ? \ 904 _tr_flush_block(s, (s->block_start >= 0L ? \
772 (charf *)&s->window[(unsigned)s->block_start] : \ 905 (charf *)&s->window[(unsigned)s->block_start] : \
773 (charf *)Z_NULL), (long)s->strstart - s->block_start, (eof)); \ 906 (charf *)Z_NULL), \
907 (ulg)((long)s->strstart - s->block_start), \
908 (eof)); \
774 s->block_start = s->strstart; \ 909 s->block_start = s->strstart; \
775 flush_pending(s->strm); \ 910 flush_pending(s->strm); \
776 Tracev((stderr,"[FLUSH]")); \ 911 Tracev((stderr,"[FLUSH]")); \
@@ -783,9 +918,54 @@ local void fill_window(s)
783} 918}
784 919
785/* =========================================================================== 920/* ===========================================================================
921 * Copy without compression as much as possible from the input stream, return
922 * true if processing was terminated prematurely (no more input or output
923 * space). This function does not insert new strings in the dictionary
924 * since uncompressible data is probably not useful. This function is used
925 * only for the level=0 compression option.
926 * NOTE: this function should be optimized to avoid extra copying.
927 */
928local int deflate_stored(s, flush)
929 deflate_state *s;
930 int flush;
931{
932 for (;;) {
933 /* Fill the window as much as possible: */
934 if (s->lookahead <= 1) {
935
936 Assert(s->strstart < s->w_size+MAX_DIST(s) ||
937 s->block_start >= (long)s->w_size, "slide too late");
938
939 fill_window(s);
940 if (s->lookahead == 0 && flush == Z_NO_FLUSH) return 1;
941
942 if (s->lookahead == 0) break; /* flush the current block */
943 }
944 Assert(s->block_start >= 0L, "block gone");
945
946 s->strstart += s->lookahead;
947 s->lookahead = 0;
948
949 /* Stored blocks are limited to 0xffff bytes: */
950 if (s->strstart == 0 || s->strstart > 0xffff) {
951 /* strstart == 0 is possible when wraparound on 16-bit machine */
952 s->lookahead = s->strstart - 0xffff;
953 s->strstart = 0xffff;
954 }
955
956 /* Emit a stored block if it is large enough: */
957 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
958 FLUSH_BLOCK(s, 0);
959 }
960 }
961 FLUSH_BLOCK(s, flush == Z_FINISH);
962 return 0; /* normal exit */
963}
964
965/* ===========================================================================
786 * Compress as much as possible from the input stream, return true if 966 * Compress as much as possible from the input stream, return true if
787 * processing was terminated prematurely (no more input or output space). 967 * processing was terminated prematurely (no more input or output space).
788 * This function does not perform lazy evaluationof matches and inserts 968 * This function does not perform lazy evaluation of matches and inserts
789 * new strings in the dictionary only for unmatched strings or for short 969 * new strings in the dictionary only for unmatched strings or for short
790 * matches. It is used only for the fast compression options. 970 * matches. It is used only for the fast compression options.
791 */ 971 */
@@ -793,10 +973,8 @@ local int deflate_fast(s, flush)
793 deflate_state *s; 973 deflate_state *s;
794 int flush; 974 int flush;
795{ 975{
796 IPos hash_head; /* head of the hash chain */ 976 IPos hash_head = NIL; /* head of the hash chain */
797 int bflush; /* set if current block must be flushed */ 977 int bflush; /* set if current block must be flushed */
798
799 s->prev_length = MIN_MATCH-1;
800 978
801 for (;;) { 979 for (;;) {
802 /* Make sure that we always have enough lookahead, except 980 /* Make sure that we always have enough lookahead, except
@@ -830,14 +1008,12 @@ local int deflate_fast(s, flush)
830 s->match_length = longest_match (s, hash_head); 1008 s->match_length = longest_match (s, hash_head);
831 } 1009 }
832 /* longest_match() sets match_start */ 1010 /* longest_match() sets match_start */
833
834 if (s->match_length > s->lookahead) s->match_length = s->lookahead;
835 } 1011 }
836 if (s->match_length >= MIN_MATCH) { 1012 if (s->match_length >= MIN_MATCH) {
837 check_match(s, s->strstart, s->match_start, s->match_length); 1013 check_match(s, s->strstart, s->match_start, s->match_length);
838 1014
839 bflush = ct_tally(s, s->strstart - s->match_start, 1015 bflush = _tr_tally(s, s->strstart - s->match_start,
840 s->match_length - MIN_MATCH); 1016 s->match_length - MIN_MATCH);
841 1017
842 s->lookahead -= s->match_length; 1018 s->lookahead -= s->match_length;
843 1019
@@ -870,7 +1046,7 @@ local int deflate_fast(s, flush)
870 } else { 1046 } else {
871 /* No match, output a literal byte */ 1047 /* No match, output a literal byte */
872 Tracevv((stderr,"%c", s->window[s->strstart])); 1048 Tracevv((stderr,"%c", s->window[s->strstart]));
873 bflush = ct_tally (s, 0, s->window[s->strstart]); 1049 bflush = _tr_tally (s, 0, s->window[s->strstart]);
874 s->lookahead--; 1050 s->lookahead--;
875 s->strstart++; 1051 s->strstart++;
876 } 1052 }
@@ -889,7 +1065,7 @@ local int deflate_slow(s, flush)
889 deflate_state *s; 1065 deflate_state *s;
890 int flush; 1066 int flush;
891{ 1067{
892 IPos hash_head; /* head of hash chain */ 1068 IPos hash_head = NIL; /* head of hash chain */
893 int bflush; /* set if current block must be flushed */ 1069 int bflush; /* set if current block must be flushed */
894 1070
895 /* Process the input block. */ 1071 /* Process the input block. */
@@ -928,7 +1104,6 @@ local int deflate_slow(s, flush)
928 s->match_length = longest_match (s, hash_head); 1104 s->match_length = longest_match (s, hash_head);
929 } 1105 }
930 /* longest_match() sets match_start */ 1106 /* longest_match() sets match_start */
931 if (s->match_length > s->lookahead) s->match_length = s->lookahead;
932 1107
933 if (s->match_length <= 5 && (s->strategy == Z_FILTERED || 1108 if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
934 (s->match_length == MIN_MATCH && 1109 (s->match_length == MIN_MATCH &&
@@ -949,8 +1124,8 @@ local int deflate_slow(s, flush)
949 1124
950 check_match(s, s->strstart-1, s->prev_match, s->prev_length); 1125 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
951 1126
952 bflush = ct_tally(s, s->strstart -1 - s->prev_match, 1127 bflush = _tr_tally(s, s->strstart -1 - s->prev_match,
953 s->prev_length - MIN_MATCH); 1128 s->prev_length - MIN_MATCH);
954 1129
955 /* Insert in hash table all strings up to the end of the match. 1130 /* Insert in hash table all strings up to the end of the match.
956 * strstart-1 and strstart are already inserted. If there is not 1131 * strstart-1 and strstart are already inserted. If there is not
@@ -976,7 +1151,7 @@ local int deflate_slow(s, flush)
976 * is longer, truncate the previous match to a single literal. 1151 * is longer, truncate the previous match to a single literal.
977 */ 1152 */
978 Tracevv((stderr,"%c", s->window[s->strstart-1])); 1153 Tracevv((stderr,"%c", s->window[s->strstart-1]));
979 if (ct_tally (s, 0, s->window[s->strstart-1])) { 1154 if (_tr_tally (s, 0, s->window[s->strstart-1])) {
980 FLUSH_BLOCK_ONLY(s, 0); 1155 FLUSH_BLOCK_ONLY(s, 0);
981 } 1156 }
982 s->strstart++; 1157 s->strstart++;
@@ -994,7 +1169,7 @@ local int deflate_slow(s, flush)
994 Assert (flush != Z_NO_FLUSH, "no flush?"); 1169 Assert (flush != Z_NO_FLUSH, "no flush?");
995 if (s->match_available) { 1170 if (s->match_available) {
996 Tracevv((stderr,"%c", s->window[s->strstart-1])); 1171 Tracevv((stderr,"%c", s->window[s->strstart-1]));
997 ct_tally (s, 0, s->window[s->strstart-1]); 1172 _tr_tally (s, 0, s->window[s->strstart-1]);
998 s->match_available = 0; 1173 s->match_available = 0;
999 } 1174 }
1000 FLUSH_BLOCK(s, flush == Z_FINISH); 1175 FLUSH_BLOCK(s, flush == Z_FINISH);
diff --git a/deflate.h b/deflate.h
index f3b4c38..cd0a6a3 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
@@ -10,17 +10,15 @@
10 10
11/* $Id: deflate.h,v 1.5 1995/05/03 17:27:09 jloup Exp $ */ 11/* $Id: deflate.h,v 1.5 1995/05/03 17:27:09 jloup Exp $ */
12 12
13#ifndef _DEFLATE_H
14#define _DEFLATE_H
15
13#include "zutil.h" 16#include "zutil.h"
14 17
15/* =========================================================================== 18/* ===========================================================================
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,10 +85,10 @@ 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 */
91 int last_flush; /* value of flush param for previous deflate call */
94 92
95 /* used by deflate.c: */ 93 /* used by deflate.c: */
96 94
@@ -173,7 +171,7 @@ typedef struct internal_state {
173 uInt good_match; 171 uInt good_match;
174 /* Use a faster search when the previous match is longer than this */ 172 /* Use a faster search when the previous match is longer than this */
175 173
176 int nice_match; /* Stop searching when current match exceeds this */ 174 int nice_match; /* Stop searching when current match exceeds this */
177 175
178 /* used by trees.c: */ 176 /* used by trees.c: */
179 /* Didn't use ct_data typedef below to supress compiler warning */ 177 /* Didn't use ct_data typedef below to supress compiler warning */
@@ -267,9 +265,11 @@ typedef struct internal_state {
267 */ 265 */
268 266
269 /* in trees.c */ 267 /* in trees.c */
270void ct_init OF((deflate_state *s)); 268void _tr_init OF((deflate_state *s));
271int ct_tally OF((deflate_state *s, int dist, int lc)); 269int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
272ulg ct_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,
273void ct_align OF((deflate_state *s)); 271 int eof));
274void ct_stored_block OF((deflate_state *s, charf *buf, ulg stored_len, 272void _tr_align OF((deflate_state *s));
273void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
275 int eof)); 274 int eof));
275#endif
diff --git a/example.c b/example.c
index c18a583..7a3047d 100644
--- a/example.c
+++ b/example.c
@@ -1,5 +1,5 @@
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
@@ -10,20 +10,11 @@
10 10
11#ifdef STDC 11#ifdef STDC
12# include <string.h> 12# include <string.h>
13# include <stdlib.h>
14#else
15 extern void exit OF((int));
13#endif 16#endif
14 17
15#ifndef __GO32__
16extern void exit OF((int));
17#endif
18
19#define BUFLEN 4096
20
21#define local static
22/* For MSDOS and other systems with limitation on stack size. For Unix,
23 #define local
24 works also.
25 */
26
27#define CHECK_ERR(err, msg) { \ 18#define CHECK_ERR(err, msg) { \
28 if (err != Z_OK) { \ 19 if (err != Z_OK) { \
29 fprintf(stderr, "%s error: %d\n", msg, err); \ 20 fprintf(stderr, "%s error: %d\n", msg, err); \
@@ -31,32 +22,44 @@ extern void exit OF((int));
31 } \ 22 } \
32} 23}
33 24
34char *hello = "hello, hello!"; 25const char hello[] = "hello, hello!";
35/* "hello world" would be more standard, but the repeated "hello" 26/* "hello world" would be more standard, but the repeated "hello"
36 * stresses the compression code better, sorry... 27 * stresses the compression code better, sorry...
37 */ 28 */
38 29
39void test_compress OF((void)); 30const char dictionary[] = "hello";
40void test_gzio OF((char *out, char *in)); 31uLong dictId; /* Adler32 value of the dictionary */
41void test_deflate OF((Byte compr[])); 32
42void test_inflate OF((Byte compr[])); 33void test_compress OF((Bytef *compr, uLong comprLen,
43void test_flush OF((Byte compr[])); 34 Bytef *uncompr, uLong uncomprLen));
44void test_sync OF((Byte compr[])); 35void test_gzio OF((const char *out, const char *in,
45int main OF((int argc, char *argv[])); 36 Bytef *uncompr, int uncomprLen));
37void test_deflate OF((Bytef *compr, uLong comprLen));
38void test_inflate OF((Bytef *compr, uLong comprLen,
39 Bytef *uncompr, uLong uncomprLen));
40void test_large_deflate OF((Bytef *compr, uLong comprLen,
41 Bytef *uncompr, uLong uncomprLen));
42void test_large_inflate OF((Bytef *compr, uLong comprLen,
43 Bytef *uncompr, uLong uncomprLen));
44void test_flush OF((Bytef *compr, uLong comprLen));
45void test_sync OF((Bytef *compr, uLong comprLen,
46 Bytef *uncompr, uLong uncomprLen));
47void test_dict_deflate OF((Bytef *compr, uLong comprLen));
48void test_dict_inflate OF((Bytef *compr, uLong comprLen,
49 Bytef *uncompr, uLong uncomprLen));
50int main OF((int argc, char *argv[]));
46 51
47/* =========================================================================== 52/* ===========================================================================
48 * Test compress() and uncompress() 53 * Test compress() and uncompress()
49 */ 54 */
50void test_compress() 55void test_compress(compr, comprLen, uncompr, uncomprLen)
56 Bytef *compr, *uncompr;
57 uLong comprLen, uncomprLen;
51{ 58{
52 local Byte compr[BUFLEN];
53 uLong comprLen = sizeof(compr);
54 local Byte uncompr[BUFLEN];
55 uLong uncomprLen = sizeof(uncompr);
56 int err; 59 int err;
57 uLong len = strlen(hello)+1; 60 uLong len = strlen(hello)+1;
58 61
59 err = compress(compr, &comprLen, (Byte*)hello, len); 62 err = compress(compr, &comprLen, (const Bytef*)hello, len);
60 CHECK_ERR(err, "compress"); 63 CHECK_ERR(err, "compress");
61 64
62 strcpy((char*)uncompr, "garbage"); 65 strcpy((char*)uncompr, "garbage");
@@ -74,12 +77,12 @@ void test_compress()
74/* =========================================================================== 77/* ===========================================================================
75 * Test read/write of .gz files 78 * Test read/write of .gz files
76 */ 79 */
77void test_gzio(out, in) 80void test_gzio(out, in, uncompr, uncomprLen)
78 char *out; /* output file */ 81 const char *out; /* output file */
79 char *in; /* input file */ 82 const char *in; /* input file */
83 Bytef *uncompr;
84 int uncomprLen;
80{ 85{
81 local Byte uncompr[BUFLEN];
82 int uncomprLen = sizeof(uncompr);
83 int err; 86 int err;
84 int len = strlen(hello)+1; 87 int len = strlen(hello)+1;
85 gzFile file; 88 gzFile file;
@@ -90,7 +93,7 @@ void test_gzio(out, in)
90 exit(1); 93 exit(1);
91 } 94 }
92 95
93 if (gzwrite(file, hello, len) != len) { 96 if (gzwrite(file, (const voidp)hello, (unsigned)len) != len) {
94 fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err)); 97 fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err));
95 } 98 }
96 gzclose(file); 99 gzclose(file);
@@ -101,7 +104,7 @@ void test_gzio(out, in)
101 } 104 }
102 strcpy((char*)uncompr, "garbage"); 105 strcpy((char*)uncompr, "garbage");
103 106
104 uncomprLen = gzread(file, uncompr, uncomprLen); 107 uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen);
105 if (uncomprLen != len) { 108 if (uncomprLen != len) {
106 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); 109 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
107 } 110 }
@@ -117,8 +120,9 @@ void test_gzio(out, in)
117/* =========================================================================== 120/* ===========================================================================
118 * Test deflate() with small buffers 121 * Test deflate() with small buffers
119 */ 122 */
120void test_deflate(compr) 123void test_deflate(compr, comprLen)
121 Byte compr[]; 124 Bytef *compr;
125 uLong comprLen;
122{ 126{
123 z_stream c_stream; /* compression stream */ 127 z_stream c_stream; /* compression stream */
124 int err; 128 int err;
@@ -126,14 +130,15 @@ void test_deflate(compr)
126 130
127 c_stream.zalloc = (alloc_func)0; 131 c_stream.zalloc = (alloc_func)0;
128 c_stream.zfree = (free_func)0; 132 c_stream.zfree = (free_func)0;
133 c_stream.opaque = (voidpf)0;
129 134
130 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); 135 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
131 CHECK_ERR(err, "deflateInit"); 136 CHECK_ERR(err, "deflateInit");
132 137
133 c_stream.next_in = (Byte*)hello; 138 c_stream.next_in = (Bytef*)hello;
134 c_stream.next_out = compr; 139 c_stream.next_out = compr;
135 140
136 while (c_stream.total_in != (uLong)len) { 141 while (c_stream.total_in != (uLong)len && c_stream.total_out < comprLen) {
137 c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */ 142 c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
138 err = deflate(&c_stream, Z_NO_FLUSH); 143 err = deflate(&c_stream, Z_NO_FLUSH);
139 CHECK_ERR(err, "deflate"); 144 CHECK_ERR(err, "deflate");
@@ -153,10 +158,10 @@ void test_deflate(compr)
153/* =========================================================================== 158/* ===========================================================================
154 * Test inflate() with small buffers 159 * Test inflate() with small buffers
155 */ 160 */
156void test_inflate(compr) 161void test_inflate(compr, comprLen, uncompr, uncomprLen)
157 Byte compr[]; 162 Bytef *compr, *uncompr;
163 uLong comprLen, uncomprLen;
158{ 164{
159 local Byte uncompr[BUFLEN];
160 int err; 165 int err;
161 z_stream d_stream; /* decompression stream */ 166 z_stream d_stream; /* decompression stream */
162 167
@@ -164,6 +169,7 @@ void test_inflate(compr)
164 169
165 d_stream.zalloc = (alloc_func)0; 170 d_stream.zalloc = (alloc_func)0;
166 d_stream.zfree = (free_func)0; 171 d_stream.zfree = (free_func)0;
172 d_stream.opaque = (voidpf)0;
167 173
168 err = inflateInit(&d_stream); 174 err = inflateInit(&d_stream);
169 CHECK_ERR(err, "inflateInit"); 175 CHECK_ERR(err, "inflateInit");
@@ -171,7 +177,7 @@ void test_inflate(compr)
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 for (;;) { 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;
@@ -189,10 +195,104 @@ void test_inflate(compr)
189} 195}
190 196
191/* =========================================================================== 197/* ===========================================================================
198 * Test deflate() with large buffers and dynamic change of compression level
199 */
200void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
201 Bytef *compr, *uncompr;
202 uLong comprLen, uncomprLen;
203{
204 z_stream c_stream; /* compression stream */
205 int err;
206
207 c_stream.zalloc = (alloc_func)0;
208 c_stream.zfree = (free_func)0;
209 c_stream.opaque = (voidpf)0;
210
211 err = deflateInit(&c_stream, Z_BEST_SPEED);
212 CHECK_ERR(err, "deflateInit");
213
214 c_stream.next_out = compr;
215 c_stream.avail_out = (uInt)comprLen;
216
217 /* At this point, uncompr is still mostly zeroes, so it should compress
218 * very well:
219 */
220 c_stream.next_in = uncompr;
221 c_stream.avail_in = (uInt)uncomprLen;
222 err = deflate(&c_stream, Z_NO_FLUSH);
223 CHECK_ERR(err, "deflate");
224 if (c_stream.avail_in != 0) {
225 fprintf(stderr, "deflate not greedy\n");
226 }
227
228 /* Feed in already compressed data and switch to no compression: */
229 deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
230 c_stream.next_in = compr;
231 c_stream.avail_in = (uInt)comprLen/2;
232 err = deflate(&c_stream, Z_NO_FLUSH);
233 CHECK_ERR(err, "deflate");
234
235 /* Switch back to compressing mode: */
236 deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
237 c_stream.next_in = uncompr;
238 c_stream.avail_in = (uInt)uncomprLen;
239 err = deflate(&c_stream, Z_NO_FLUSH);
240 CHECK_ERR(err, "deflate");
241
242 err = deflate(&c_stream, Z_FINISH);
243 if (err != Z_STREAM_END) {
244 fprintf(stderr, "deflate should report Z_STREAM_END\n");
245 }
246 err = deflateEnd(&c_stream);
247 CHECK_ERR(err, "deflateEnd");
248}
249
250/* ===========================================================================
251 * Test inflate() with large buffers
252 */
253void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
254 Bytef *compr, *uncompr;
255 uLong comprLen, uncomprLen;
256{
257 int err;
258 z_stream d_stream; /* decompression stream */
259
260 strcpy((char*)uncompr, "garbage");
261
262 d_stream.zalloc = (alloc_func)0;
263 d_stream.zfree = (free_func)0;
264 d_stream.opaque = (voidpf)0;
265
266 err = inflateInit(&d_stream);
267 CHECK_ERR(err, "inflateInit");
268
269 d_stream.next_in = compr;
270 d_stream.avail_in = (uInt)comprLen;
271
272 for (;;) {
273 d_stream.next_out = uncompr; /* discard the output */
274 d_stream.avail_out = (uInt)uncomprLen;
275 err = inflate(&d_stream, Z_NO_FLUSH);
276 if (err == Z_STREAM_END) break;
277 CHECK_ERR(err, "large inflate");
278 }
279
280 err = inflateEnd(&d_stream);
281 CHECK_ERR(err, "inflateEnd");
282
283 if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
284 fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
285 } else {
286 printf("large_inflate(): OK\n");
287 }
288}
289
290/* ===========================================================================
192 * Test deflate() with full flush 291 * Test deflate() with full flush
193 */ 292 */
194void test_flush(compr) 293void test_flush(compr, comprLen)
195 Byte compr[]; 294 Bytef *compr;
295 uLong comprLen;
196{ 296{
197 z_stream c_stream; /* compression stream */ 297 z_stream c_stream; /* compression stream */
198 int err; 298 int err;
@@ -200,14 +300,15 @@ void test_flush(compr)
200 300
201 c_stream.zalloc = (alloc_func)0; 301 c_stream.zalloc = (alloc_func)0;
202 c_stream.zfree = (free_func)0; 302 c_stream.zfree = (free_func)0;
303 c_stream.opaque = (voidpf)0;
203 304
204 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); 305 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
205 CHECK_ERR(err, "deflateInit"); 306 CHECK_ERR(err, "deflateInit");
206 307
207 c_stream.next_in = (Byte*)hello; 308 c_stream.next_in = (Bytef*)hello;
208 c_stream.next_out = compr; 309 c_stream.next_out = compr;
209 c_stream.avail_in = 3; 310 c_stream.avail_in = 3;
210 c_stream.avail_out = BUFLEN; 311 c_stream.avail_out = (uInt)comprLen;
211 err = deflate(&c_stream, Z_FULL_FLUSH); 312 err = deflate(&c_stream, Z_FULL_FLUSH);
212 CHECK_ERR(err, "deflate"); 313 CHECK_ERR(err, "deflate");
213 314
@@ -225,10 +326,10 @@ void test_flush(compr)
225/* =========================================================================== 326/* ===========================================================================
226 * Test inflateSync() 327 * Test inflateSync()
227 */ 328 */
228void test_sync(compr) 329void test_sync(compr, comprLen, uncompr, uncomprLen)
229 Byte compr[]; 330 Bytef *compr, *uncompr;
331 uLong comprLen, uncomprLen;
230{ 332{
231 local Byte uncompr[BUFLEN];
232 int err; 333 int err;
233 z_stream d_stream; /* decompression stream */ 334 z_stream d_stream; /* decompression stream */
234 335
@@ -236,6 +337,7 @@ void test_sync(compr)
236 337
237 d_stream.zalloc = (alloc_func)0; 338 d_stream.zalloc = (alloc_func)0;
238 d_stream.zfree = (free_func)0; 339 d_stream.zfree = (free_func)0;
340 d_stream.opaque = (voidpf)0;
239 341
240 err = inflateInit(&d_stream); 342 err = inflateInit(&d_stream);
241 CHECK_ERR(err, "inflateInit"); 343 CHECK_ERR(err, "inflateInit");
@@ -243,13 +345,13 @@ void test_sync(compr)
243 d_stream.next_in = compr; 345 d_stream.next_in = compr;
244 d_stream.next_out = uncompr; 346 d_stream.next_out = uncompr;
245 d_stream.avail_in = 2; /* just read the zlib header */ 347 d_stream.avail_in = 2; /* just read the zlib header */
246 d_stream.avail_out = sizeof(uncompr); 348 d_stream.avail_out = (uInt)uncomprLen;
247 349
248 inflate(&d_stream, Z_NO_FLUSH); 350 inflate(&d_stream, Z_NO_FLUSH);
249 CHECK_ERR(err, "inflate"); 351 CHECK_ERR(err, "inflate");
250 352
251 d_stream.avail_in = BUFLEN-2; /* let inflate read all compressed data */ 353 d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */
252 err = inflateSync(&d_stream); /* skip the damaged part */ 354 err = inflateSync(&d_stream); /* but skip the damaged part */
253 CHECK_ERR(err, "inflateSync"); 355 CHECK_ERR(err, "inflateSync");
254 356
255 err = inflate(&d_stream, Z_FINISH); 357 err = inflate(&d_stream, Z_FINISH);
@@ -264,6 +366,91 @@ void test_sync(compr)
264} 366}
265 367
266/* =========================================================================== 368/* ===========================================================================
369 * Test deflate() with preset dictionary
370 */
371void test_dict_deflate(compr, comprLen)
372 Bytef *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 Bytef *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/* ===========================================================================
267 * Usage: example [output.gz [input.gz]] 454 * Usage: example [output.gz [input.gz]]
268 */ 455 */
269 456
@@ -271,7 +458,9 @@ int main(argc, argv)
271 int argc; 458 int argc;
272 char *argv[]; 459 char *argv[];
273{ 460{
274 local Byte compr[BUFLEN]; 461 Bytef *compr, *uncompr;
462 uLong comprLen = 32750*sizeof(int); /* don't overflow on MSDOS */
463 uLong uncomprLen = comprLen;
275 464
276 if (zlib_version[0] != ZLIB_VERSION[0]) { 465 if (zlib_version[0] != ZLIB_VERSION[0]) {
277 fprintf(stderr, "incompatible zlib version\n"); 466 fprintf(stderr, "incompatible zlib version\n");
@@ -280,16 +469,31 @@ int main(argc, argv)
280 } else if (strcmp(zlib_version, ZLIB_VERSION) != 0) { 469 } else if (strcmp(zlib_version, ZLIB_VERSION) != 0) {
281 fprintf(stderr, "warning: different zlib version\n"); 470 fprintf(stderr, "warning: different zlib version\n");
282 } 471 }
283 test_compress(); 472
473 compr = (Bytef*)malloc((uInt)comprLen);
474 uncompr = (Bytef*)calloc((uInt)uncomprLen, 1); /* must be cleared */
475 if (compr == Z_NULL || uncompr == Z_NULL) {
476 printf("out of memory\n");
477 exit(1);
478 }
479
480 test_compress(compr, comprLen, uncompr, uncomprLen);
284 481
285 test_gzio((argc > 1 ? argv[1] : "foo.gz"), 482 test_gzio((argc > 1 ? argv[1] : "foo.gz"),
286 (argc > 2 ? argv[2] : "foo.gz")); 483 (argc > 2 ? argv[2] : "foo.gz"),
484 uncompr, (int)uncomprLen);
485
486 test_deflate(compr, comprLen);
487 test_inflate(compr, comprLen, uncompr, uncomprLen);
488
489 test_large_deflate(compr, comprLen, uncompr, uncomprLen);
490 test_large_inflate(compr, comprLen, uncompr, uncomprLen);
287 491
288 test_deflate(compr); 492 test_flush(compr, comprLen);
289 test_inflate(compr); 493 test_sync(compr, comprLen, uncompr, uncomprLen);
290 494
291 test_flush(compr); 495 test_dict_deflate(compr, comprLen);
292 test_sync(compr); 496 test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
293 497
294 exit(0); 498 exit(0);
295 return 0; /* to avoid warning */ 499 return 0; /* to avoid warning */
diff --git a/gzio.c b/gzio.c
index cd932d3..13c7289 100644
--- a/gzio.c
+++ b/gzio.c
@@ -1,5 +1,5 @@
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
@@ -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,44 +41,16 @@ 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
86 is as in fopen ("rb" or "wb"). The file is given either by file descritor 53 is as in fopen ("rb" or "wb"). The file is given either by file descriptor
87 or path name (if fd == -1). 54 or path name (if fd == -1).
88 gz_open return NULL if the file could not be opened or if there was 55 gz_open return NULL if the file could not be opened or if there was
89 insufficient memory to allocate the (de)compression state; errno 56 insufficient memory to allocate the (de)compression state; errno
@@ -91,19 +58,25 @@ 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;
106 s->stream.zfree = (free_func)0; 78 s->stream.zfree = (free_func)0;
79 s->stream.opaque = (voidpf)0;
107 s->stream.next_in = s->inbuf = Z_NULL; 80 s->stream.next_in = s->inbuf = Z_NULL;
108 s->stream.next_out = s->outbuf = Z_NULL; 81 s->stream.next_out = s->outbuf = Z_NULL;
109 s->stream.avail_in = s->stream.avail_out = 0; 82 s->stream.avail_in = s->stream.avail_out = 0;
@@ -123,14 +96,18 @@ local gzFile gz_open (path, mode, fd)
123 s->mode = '\0'; 96 s->mode = '\0';
124 do { 97 do {
125 if (*p == 'r') s->mode = 'r'; 98 if (*p == 'r') s->mode = 'r';
126 if (*p == 'w') s->mode = 'w'; 99 if (*p == 'w' || *p == 'a') s->mode = 'w';
127 if (*p >= '1' && *p <= '9') level = *p - '0'; 100 if (*p >= '0' && *p <= '9') {
128 } while (*p++); 101 level = *p - '0';
102 } else {
103 *m++ = *p; /* copy the mode */
104 }
105 } while (*p++ && m != fmode + sizeof(fmode));
129 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL; 106 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
130 107
131 if (s->mode == 'w') { 108 if (s->mode == 'w') {
132 err = deflateInit2(&(s->stream), level, 109 err = deflateInit2(&(s->stream), level,
133 DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0); 110 Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0);
134 /* windowBits is passed < 0 to suppress zlib header */ 111 /* windowBits is passed < 0 to suppress zlib header */
135 112
136 s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE); 113 s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
@@ -149,7 +126,7 @@ local gzFile gz_open (path, mode, fd)
149 s->stream.avail_out = Z_BUFSIZE; 126 s->stream.avail_out = Z_BUFSIZE;
150 127
151 errno = 0; 128 errno = 0;
152 s->file = fd < 0 ? FOPEN(path, mode) : fdopen(fd, mode); 129 s->file = fd < 0 ? FOPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
153 130
154 if (s->file == NULL) { 131 if (s->file == NULL) {
155 return destroy(s), (gzFile)Z_NULL; 132 return destroy(s), (gzFile)Z_NULL;
@@ -157,50 +134,10 @@ local gzFile gz_open (path, mode, fd)
157 if (s->mode == 'w') { 134 if (s->mode == 'w') {
158 /* Write a very simple .gz header: 135 /* Write a very simple .gz header:
159 */ 136 */
160 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],
161 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);
162 } else { 139 } else {
163 /* Check and skip the header: 140 check_header(s); /* skip the .gz header */
164 */
165 Byte c1 = 0, c2 = 0;
166 Byte method = 0;
167 Byte flags = 0;
168 Byte xflags = 0;
169 Byte time[4];
170 Byte osCode;
171 int c;
172
173 s->stream.avail_in = fread(s->inbuf, 1, 2, s->file);
174 if (s->stream.avail_in != 2 || s->inbuf[0] != GZ_MAGIC_1
175 || s->inbuf[1] != GZ_MAGIC_2) {
176 s->transparent = 1;
177 return (gzFile)s;
178 }
179 s->stream.avail_in = 0;
180 fscanf(s->file,"%c%c%4c%c%c", &method, &flags, time, &xflags, &osCode);
181
182 if (method != DEFLATED || feof(s->file) || (flags & RESERVED) != 0) {
183 s->z_err = Z_DATA_ERROR;
184 return (gzFile)s;
185 }
186 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
187 long len;
188 fscanf(s->file, "%c%c", &c1, &c2);
189 len = c1 + ((long)c2<<8);
190 fseek(s->file, len, SEEK_CUR);
191 }
192 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
193 while ((c = getc(s->file)) != 0 && c != EOF) ;
194 }
195 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
196 while ((c = getc(s->file)) != 0 && c != EOF) ;
197 }
198 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
199 fscanf(s->file, "%c%c", &c1, &c2);
200 }
201 if (feof(s->file)) {
202 s->z_err = Z_DATA_ERROR;
203 }
204 } 141 }
205 return (gzFile)s; 142 return (gzFile)s;
206} 143}
@@ -209,26 +146,139 @@ local gzFile gz_open (path, mode, fd)
209 Opens a gzip (.gz) file for reading or writing. 146 Opens a gzip (.gz) file for reading or writing.
210*/ 147*/
211gzFile gzopen (path, mode) 148gzFile gzopen (path, mode)
212 char *path; 149 const char *path;
213 char *mode; 150 const char *mode;
214{ 151{
215 return gz_open (path, mode, -1); 152 return gz_open (path, mode, -1);
216} 153}
217 154
218/* =========================================================================== 155/* ===========================================================================
219 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.
220*/ 158*/
221gzFile gzdopen (fd, mode) 159gzFile gzdopen (fd, mode)
222 int fd; 160 int fd;
223 char *mode; 161 const char *mode;
224{ 162{
225 char name[20]; 163 char name[20];
164
165 if (fd < 0) return (gzFile)Z_NULL;
226 sprintf(name, "<fd:%d>", fd); /* for debugging */ 166 sprintf(name, "<fd:%d>", fd); /* for debugging */
227 167
228 return gz_open (name, mode, fd); 168 return gz_open (name, mode, fd);
229} 169}
230 170
231/* =========================================================================== 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 = 0;
207 int flags = 0;
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/* ===========================================================================
232 Reads the given number of uncompressed bytes from the compressed file. 282 Reads the given number of uncompressed bytes from the compressed file.
233 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).
234*/ 284*/
@@ -238,52 +288,71 @@ int gzread (file, buf, len)
238 unsigned len; 288 unsigned len;
239{ 289{
240 gz_stream *s = (gz_stream*)file; 290 gz_stream *s = (gz_stream*)file;
291 Byte *start = buf; /* starting point for crc computation */
241 292
242 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR; 293 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
243 294
244 if (s->transparent) { 295 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
245 int n = 0; 296 if (s->z_err == Z_STREAM_END) return 0; /* EOF */
246 Byte *b = (Byte*)buf;
247 /* Copy the first two (non-magic) bytes if not done already */
248 while (s->stream.avail_in > 0 && len > 0) {
249 *b++ = *s->stream.next_in++;
250 s->stream.avail_in--;
251 len--; n++;
252 }
253 if (len == 0) return n;
254 return n + fread(b, 1, len, s->file);
255 }
256 if (s->z_err == Z_DATA_ERROR) return -1; /* bad .gz file */
257 if (s->z_err == Z_STREAM_END) return 0; /* don't read crc as data */
258 297
259 s->stream.next_out = buf; 298 s->stream.next_out = buf;
260 s->stream.avail_out = len; 299 s->stream.avail_out = len;
261 300
262 while (s->stream.avail_out != 0) { 301 while (s->stream.avail_out != 0) {
263 302
303 if (s->transparent) {
304 /* Copy first the lookahead bytes: */
305 uInt n = s->stream.avail_in;
306 if (n > s->stream.avail_out) n = s->stream.avail_out;
307 if (n > 0) {
308 zmemcpy(s->stream.next_out, s->stream.next_in, n);
309 s->stream.next_out += n;
310 s->stream.next_in += n;
311 s->stream.avail_out -= n;
312 s->stream.avail_in -= n;
313 }
314 if (s->stream.avail_out > 0) {
315 s->stream.avail_out -= fread(s->stream.next_out,
316 1, s->stream.avail_out, s->file);
317 }
318 return (int)(len - s->stream.avail_out);
319 }
264 if (s->stream.avail_in == 0 && !s->z_eof) { 320 if (s->stream.avail_in == 0 && !s->z_eof) {
265 321
266 errno = 0; 322 errno = 0;
267 s->stream.avail_in = 323 s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
268 fread(s->inbuf, 1, Z_BUFSIZE, s->file);
269 if (s->stream.avail_in == 0) { 324 if (s->stream.avail_in == 0) {
270 s->z_eof = 1; 325 s->z_eof = 1;
271 } else if (s->stream.avail_in == (uInt)EOF) { 326 if (ferror(s->file)) {
272 s->stream.avail_in = 0; 327 s->z_err = Z_ERRNO;
273 s->z_eof = 1; 328 break;
274 s->z_err = Z_ERRNO; 329 }
275 break;
276 } 330 }
277 s->stream.next_in = s->inbuf; 331 s->stream.next_in = s->inbuf;
278 } 332 }
279 s->z_err = inflate(&(s->stream), Z_NO_FLUSH); 333 s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
280 334
281 if (s->z_err == Z_STREAM_END || 335 if (s->z_err == Z_STREAM_END) {
282 s->z_err != Z_OK || s->z_eof) break; 336 /* Check CRC and original size */
337 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
338 start = s->stream.next_out;
339
340 if (getLong(s) != s->crc || getLong(s) != s->stream.total_out) {
341 s->z_err = Z_DATA_ERROR;
342 } else {
343 /* Check for concatenated .gz files: */
344 check_header(s);
345 if (s->z_err == Z_OK) {
346 inflateReset(&(s->stream));
347 s->crc = crc32(0L, Z_NULL, 0);
348 }
349 }
350 }
351 if (s->z_err != Z_OK || s->z_eof) break;
283 } 352 }
284 len -= s->stream.avail_out; 353 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
285 s->crc = crc32(s->crc, buf, len); 354
286 return (int)len; 355 return (int)(len - s->stream.avail_out);
287} 356}
288 357
289/* =========================================================================== 358/* ===========================================================================
@@ -292,7 +361,7 @@ int gzread (file, buf, len)
292*/ 361*/
293int gzwrite (file, buf, len) 362int gzwrite (file, buf, len)
294 gzFile file; 363 gzFile file;
295 voidp buf; 364 const voidp buf;
296 unsigned len; 365 unsigned len;
297{ 366{
298 gz_stream *s = (gz_stream*)file; 367 gz_stream *s = (gz_stream*)file;
@@ -343,7 +412,7 @@ int gzflush (file, flush)
343 len = Z_BUFSIZE - s->stream.avail_out; 412 len = Z_BUFSIZE - s->stream.avail_out;
344 413
345 if (len != 0) { 414 if (len != 0) {
346 if (fwrite(s->outbuf, 1, len, s->file) != len) { 415 if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
347 s->z_err = Z_ERRNO; 416 s->z_err = Z_ERRNO;
348 return Z_ERRNO; 417 return Z_ERRNO;
349 } 418 }
@@ -379,18 +448,19 @@ local void putLong (file, x)
379} 448}
380 449
381/* =========================================================================== 450/* ===========================================================================
382 Reads a long in LSB order from the given buffer 451 Reads a long in LSB order from the given gz_stream. Sets
383*/ 452*/
384local uLong getLong (buf) 453local uLong getLong (s)
385 Bytef *buf; 454 gz_stream *s;
386{ 455{
387 uLong x = 0; 456 uLong x = (uLong)get_byte(s);
388 Bytef *p = buf+4; 457 int c;
389 458
390 do { 459 x += ((uLong)get_byte(s))<<8;
391 x <<= 8; 460 x += ((uLong)get_byte(s))<<16;
392 x |= *--p; 461 c = get_byte(s);
393 } while (p != buf); 462 if (c == EOF) s->z_err = Z_DATA_ERROR;
463 x += ((uLong)c)<<24;
394 return x; 464 return x;
395} 465}
396 466
@@ -401,7 +471,6 @@ local uLong getLong (buf)
401int gzclose (file) 471int gzclose (file)
402 gzFile file; 472 gzFile file;
403{ 473{
404 uInt n;
405 int err; 474 int err;
406 gz_stream *s = (gz_stream*)file; 475 gz_stream *s = (gz_stream*)file;
407 476
@@ -414,25 +483,6 @@ int gzclose (file)
414 putLong (s->file, s->crc); 483 putLong (s->file, s->crc);
415 putLong (s->file, s->stream.total_in); 484 putLong (s->file, s->stream.total_in);
416 485
417 } else if (s->mode == 'r' && s->z_err == Z_STREAM_END) {
418
419 /* slide CRC and original size if they are at the end of inbuf */
420 if ((n = s->stream.avail_in) < 8 && !s->z_eof) {
421 Byte *p = s->inbuf;
422 Bytef *q = s->stream.next_in;
423 while (n--) { *p++ = *q++; };
424
425 n = s->stream.avail_in;
426 n += fread(p, 1, 8, s->file);
427 s->stream.next_in = s->inbuf;
428 }
429 /* check CRC and original size */
430 if (n < 8 ||
431 getLong(s->stream.next_in) != s->crc ||
432 getLong(s->stream.next_in + 4) != s->stream.total_out) {
433
434 s->z_err = Z_DATA_ERROR;
435 }
436 } 486 }
437 return destroy(file); 487 return destroy(file);
438} 488}
@@ -453,14 +503,14 @@ char* gzerror (file, errnum)
453 503
454 if (s == NULL) { 504 if (s == NULL) {
455 *errnum = Z_STREAM_ERROR; 505 *errnum = Z_STREAM_ERROR;
456 return z_errmsg[1-Z_STREAM_ERROR]; 506 return ERR_MSG(Z_STREAM_ERROR);
457 } 507 }
458 *errnum = s->z_err; 508 *errnum = s->z_err;
459 if (*errnum == Z_OK) return ""; 509 if (*errnum == Z_OK) return (char*)"";
460 510
461 m = *errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg; 511 m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
462 512
463 if (m == NULL || *m == '\0') m = z_errmsg[1-s->z_err]; 513 if (m == NULL || *m == '\0') m = (char*)z_errmsg[1-s->z_err];
464 514
465 TRYFREE(s->msg); 515 TRYFREE(s->msg);
466 s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3); 516 s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
diff --git a/infblock.c b/infblock.c
index 1fc0726..4c2ff65 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
@@ -172,17 +172,17 @@ int r;
172 case 3: /* illegal */ 172 case 3: /* illegal */
173 DUMPBITS(3) 173 DUMPBITS(3)
174 s->mode = BAD; 174 s->mode = BAD;
175 z->msg = "invalid block type"; 175 z->msg = (char*)"invalid block type";
176 r = Z_DATA_ERROR; 176 r = Z_DATA_ERROR;
177 LEAVE 177 LEAVE
178 } 178 }
179 break; 179 break;
180 case LENS: 180 case LENS:
181 NEEDBITS(32) 181 NEEDBITS(32)
182 if (((~b) >> 16) != (b & 0xffff)) 182 if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
183 { 183 {
184 s->mode = BAD; 184 s->mode = BAD;
185 z->msg = "invalid stored block lengths"; 185 z->msg = (char*)"invalid stored block lengths";
186 r = Z_DATA_ERROR; 186 r = Z_DATA_ERROR;
187 LEAVE 187 LEAVE
188 } 188 }
@@ -215,7 +215,7 @@ int r;
215 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) 215 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
216 { 216 {
217 s->mode = BAD; 217 s->mode = BAD;
218 z->msg = "too many length or distance symbols"; 218 z->msg = (char*)"too many length or distance symbols";
219 r = Z_DATA_ERROR; 219 r = Z_DATA_ERROR;
220 LEAVE 220 LEAVE
221 } 221 }
@@ -285,7 +285,7 @@ int r;
285 (c == 16 && i < 1)) 285 (c == 16 && i < 1))
286 { 286 {
287 s->mode = BAD; 287 s->mode = BAD;
288 z->msg = "invalid bit length repeat"; 288 z->msg = (char*)"invalid bit length repeat";
289 r = Z_DATA_ERROR; 289 r = Z_DATA_ERROR;
290 LEAVE 290 LEAVE
291 } 291 }
@@ -383,3 +383,14 @@ uLongf *c;
383 Trace((stderr, "inflate: blocks freed\n")); 383 Trace((stderr, "inflate: blocks freed\n"));
384 return Z_OK; 384 return Z_OK;
385} 385}
386
387
388void inflate_set_dictionary(s, z, d, n)
389inflate_blocks_statef *s;
390z_stream *z;
391const Bytef *d;
392uInt n;
393{
394 zmemcpy((charf *)s->window, d, n);
395 s->read = s->write = s->window + n;
396}
diff --git a/infblock.h b/infblock.h
index aba6e24..03371ce 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,9 @@ 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 z_stream *z,
37 const Bytef *d, /* dictionary */
38 uInt n)); /* dictionary length */
diff --git a/infcodes.c b/infcodes.c
index 22ce856..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 */
@@ -202,7 +203,7 @@ int r;
202#else 203#else
203 f = q - c->sub.copy.dist; 204 f = q - c->sub.copy.dist;
204 if ((uInt)(q - s->window) < c->sub.copy.dist) 205 if ((uInt)(q - s->window) < c->sub.copy.dist)
205 f = s->end - (c->sub.copy.dist - (q - s->window)); 206 f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
206#endif 207#endif
207 while (c->len) 208 while (c->len)
208 { 209 {
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 d1f1ad2..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{
@@ -102,7 +103,7 @@ z_stream *z;
102 } 103 }
103 else /* else offset after destination */ 104 else /* else offset after destination */
104 { 105 {
105 e = d - (q - s->window); /* bytes from offset to end */ 106 e = d - (uInt)(q - s->window); /* bytes from offset to end */
106 r = s->end - e; /* pointer to offset */ 107 r = s->end - e; /* pointer to offset */
107 if (c > e) /* if source crosses, */ 108 if (c > e) /* if source crosses, */
108 { 109 {
@@ -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 ec0f3a0..1f576e4 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,14 +80,25 @@ 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;
85 if (z->zalloc == Z_NULL) z->zalloc = zcalloc; 96 z->msg = Z_NULL;
97 if (z->zalloc == Z_NULL)
98 {
99 z->zalloc = zcalloc;
100 z->opaque = (voidpf)0;
101 }
86 if (z->zfree == Z_NULL) z->zfree = zcfree; 102 if (z->zfree == Z_NULL) z->zfree = zcfree;
87 if ((z->state = (struct internal_state FAR *) 103 if ((z->state = (struct internal_state FAR *)
88 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL) 104 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
@@ -107,7 +123,7 @@ int w;
107 123
108 /* create inflate_blocks state */ 124 /* create inflate_blocks state */
109 if ((z->state->blocks = 125 if ((z->state->blocks =
110 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))
111 == Z_NULL) 127 == Z_NULL)
112 { 128 {
113 inflateEnd(z); 129 inflateEnd(z);
@@ -121,10 +137,12 @@ int w;
121} 137}
122 138
123 139
124int inflateInit(z) 140int inflateInit_(z, version, stream_size)
125z_stream *z; 141z_stream *z;
142const char *version;
143int stream_size;
126{ 144{
127 return inflateInit2(z, DEF_WBITS); 145 return inflateInit2_(z, DEF_WBITS, version, stream_size);
128} 146}
129 147
130 148
@@ -138,46 +156,68 @@ int f;
138 int r = f; /* to avoid warning about unused f */ 156 int r = f; /* to avoid warning about unused f */
139 uInt b; 157 uInt b;
140 158
141 if (z == Z_NULL || z->next_in == Z_NULL) 159 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
142 return Z_STREAM_ERROR; 160 return Z_STREAM_ERROR;
143 r = Z_BUF_ERROR; 161 r = Z_BUF_ERROR;
144 while (1) switch (z->state->mode) 162 while (1) switch (z->state->mode)
145 { 163 {
146 case METHOD: 164 case METHOD:
147 NEEDBYTE 165 NEEDBYTE
148 if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED) 166 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
149 { 167 {
150 z->state->mode = BAD; 168 z->state->mode = BAD;
151 z->msg = "unknown compression method"; 169 z->msg = (char*)"unknown compression method";
152 z->state->sub.marker = 5; /* can't try inflateSync */ 170 z->state->sub.marker = 5; /* can't try inflateSync */
153 break; 171 break;
154 } 172 }
155 if ((z->state->sub.method >> 4) + 8 > z->state->wbits) 173 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
156 { 174 {
157 z->state->mode = BAD; 175 z->state->mode = BAD;
158 z->msg = "invalid window size"; 176 z->msg = (char*)"invalid window size";
159 z->state->sub.marker = 5; /* can't try inflateSync */ 177 z->state->sub.marker = 5; /* can't try inflateSync */
160 break; 178 break;
161 } 179 }
162 z->state->mode = FLAG; 180 z->state->mode = FLAG;
163 case FLAG: 181 case FLAG:
164 NEEDBYTE 182 NEEDBYTE
165 if ((b = NEXTBYTE) & 0x20) 183 b = NEXTBYTE;
166 {
167 z->state->mode = BAD;
168 z->msg = "invalid reserved bit";
169 z->state->sub.marker = 5; /* can't try inflateSync */
170 break;
171 }
172 if (((z->state->sub.method << 8) + b) % 31) 184 if (((z->state->sub.method << 8) + b) % 31)
173 { 185 {
174 z->state->mode = BAD; 186 z->state->mode = BAD;
175 z->msg = "incorrect header check"; 187 z->msg = (char*)"incorrect header check";
176 z->state->sub.marker = 5; /* can't try inflateSync */ 188 z->state->sub.marker = 5; /* can't try inflateSync */
177 break; 189 break;
178 } 190 }
179 Trace((stderr, "inflate: zlib header ok\n")); 191 Trace((stderr, "inflate: zlib header ok\n"));
180 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;
181 case BLOCKS: 221 case BLOCKS:
182 r = inflate_blocks(z->state->blocks, z, r); 222 r = inflate_blocks(z->state->blocks, z, r);
183 if (r == Z_DATA_ERROR) 223 if (r == Z_DATA_ERROR)
@@ -215,7 +255,7 @@ int f;
215 if (z->state->sub.check.was != z->state->sub.check.need) 255 if (z->state->sub.check.was != z->state->sub.check.need)
216 { 256 {
217 z->state->mode = BAD; 257 z->state->mode = BAD;
218 z->msg = "incorrect data check"; 258 z->msg = (char*)"incorrect data check";
219 z->state->sub.marker = 5; /* can't try inflateSync */ 259 z->state->sub.marker = 5; /* can't try inflateSync */
220 break; 260 break;
221 } 261 }
@@ -231,6 +271,29 @@ int f;
231} 271}
232 272
233 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 >= (1<<z->state->wbits))
287 {
288 length = (1<<z->state->wbits)-1;
289 dictionary += dictLength - length;
290 }
291 inflate_set_dictionary(z->state->blocks, z, dictionary, length);
292 z->state->mode = BLOCKS;
293 return Z_OK;
294}
295
296
234int inflateSync(z) 297int inflateSync(z)
235z_stream *z; 298z_stream *z;
236{ 299{
diff --git a/inftrees.c b/inftrees.c
index ab9d74f..11967c8 100644
--- a/inftrees.c
+++ b/inftrees.c
@@ -1,11 +1,18 @@
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-1996 Mark Adler ";
10/*
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
13 include such an acknowledgment, I would appreciate that you keep this
14 copyright string in the executable of your product.
15 */
9struct internal_state {int dummy;}; /* for buggy compilers */ 16struct internal_state {int dummy;}; /* for buggy compilers */
10 17
11/* simplify the use of the inflate_huft type with some defines */ 18/* simplify the use of the inflate_huft type with some defines */
@@ -30,23 +37,19 @@ local voidpf falloc OF((
30 uInt, /* number of items */ 37 uInt, /* number of items */
31 uInt)); /* size of item */ 38 uInt)); /* size of item */
32 39
33local void ffree OF((
34 voidpf q, /* opaque pointer (not used) */
35 voidpf p)); /* what to free (not used) */
36
37/* Tables for deflate from PKZIP's appnote.txt. */ 40/* Tables for deflate from PKZIP's appnote.txt. */
38local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */ 41local uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
39 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,
40 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};
41 /* actually lengths - 2; also see note #13 above about 258 */ 44 /* actually lengths - 2; also see note #13 above about 258 */
42local uInt cplext[] = { /* Extra bits for literal codes 257..285 */ 45local uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
43 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,
44 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 */
45local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */ 48local uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
46 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,
47 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 50 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
48 8193, 12289, 16385, 24577}; 51 8193, 12289, 16385, 24577};
49local uInt cpdext[] = { /* Extra bits for distance codes */ 52local uInt cpdext[30] = { /* Extra bits for distance codes */
50 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,
51 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 54 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
52 12, 12, 13, 13}; 55 12, 12, 13, 13};
@@ -304,11 +307,11 @@ z_stream *z; /* for zfree function */
304 307
305 r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z); 308 r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z);
306 if (r == Z_DATA_ERROR) 309 if (r == Z_DATA_ERROR)
307 z->msg = "oversubscribed dynamic bit lengths tree"; 310 z->msg = (char*)"oversubscribed dynamic bit lengths tree";
308 else if (r == Z_BUF_ERROR) 311 else if (r == Z_BUF_ERROR)
309 { 312 {
310 inflate_trees_free(*tb, z); 313 inflate_trees_free(*tb, z);
311 z->msg = "incomplete dynamic bit lengths tree"; 314 z->msg = (char*)"incomplete dynamic bit lengths tree";
312 r = Z_DATA_ERROR; 315 r = Z_DATA_ERROR;
313 } 316 }
314 return r; 317 return r;
@@ -331,11 +334,11 @@ z_stream *z; /* for zfree function */
331 if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK) 334 if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK)
332 { 335 {
333 if (r == Z_DATA_ERROR) 336 if (r == Z_DATA_ERROR)
334 z->msg = "oversubscribed literal/length tree"; 337 z->msg = (char*)"oversubscribed literal/length tree";
335 else if (r == Z_BUF_ERROR) 338 else if (r == Z_BUF_ERROR)
336 { 339 {
337 inflate_trees_free(*tl, z); 340 inflate_trees_free(*tl, z);
338 z->msg = "incomplete literal/length tree"; 341 z->msg = (char*)"incomplete literal/length tree";
339 r = Z_DATA_ERROR; 342 r = Z_DATA_ERROR;
340 } 343 }
341 return r; 344 return r;
@@ -345,14 +348,14 @@ z_stream *z; /* for zfree function */
345 if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK) 348 if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK)
346 { 349 {
347 if (r == Z_DATA_ERROR) 350 if (r == Z_DATA_ERROR)
348 z->msg = "oversubscribed literal/length tree"; 351 z->msg = (char*)"oversubscribed literal/length tree";
349 else if (r == Z_BUF_ERROR) { 352 else if (r == Z_BUF_ERROR) {
350#ifdef PKZIP_BUG_WORKAROUND 353#ifdef PKZIP_BUG_WORKAROUND
351 r = Z_OK; 354 r = Z_OK;
352 } 355 }
353#else 356#else
354 inflate_trees_free(*td, z); 357 inflate_trees_free(*td, z);
355 z->msg = "incomplete literal/length tree"; 358 z->msg = (char*)"incomplete literal/length tree";
356 r = Z_DATA_ERROR; 359 r = Z_DATA_ERROR;
357 } 360 }
358 inflate_trees_free(*tl, z); 361 inflate_trees_free(*tl, z);
@@ -366,10 +369,8 @@ z_stream *z; /* for zfree function */
366 369
367 370
368/* build fixed tables only once--keep them here */ 371/* build fixed tables only once--keep them here */
369local int fixed_lock = 0;
370local int fixed_built = 0; 372local int fixed_built = 0;
371#define FIXEDH 530 /* number of hufts used by fixed tables */ 373#define FIXEDH 530 /* number of hufts used by fixed tables */
372local uInt fixed_left = FIXEDH;
373local inflate_huft fixed_mem[FIXEDH]; 374local inflate_huft fixed_mem[FIXEDH];
374local uInt fixed_bl; 375local uInt fixed_bl;
375local uInt fixed_bd; 376local uInt fixed_bd;
@@ -378,24 +379,14 @@ local inflate_huft *fixed_td;
378 379
379 380
380local voidpf falloc(q, n, s) 381local voidpf falloc(q, n, s)
381voidpf q; /* opaque pointer (not used) */ 382voidpf q; /* opaque pointer */
382uInt n; /* number of items */ 383uInt n; /* number of items */
383uInt s; /* size of item */ 384uInt s; /* size of item */
384{ 385{
385 Assert(s == sizeof(inflate_huft) && n <= fixed_left, 386 Assert(s == sizeof(inflate_huft) && n <= *(intf *)q,
386 "inflate_trees falloc overflow"); 387 "inflate_trees falloc overflow");
387 if (q) s++; /* to make some compilers happy */ 388 *(intf *)q -= n+s-s; /* s-s to avoid warning */
388 fixed_left -= n; 389 return (voidpf)(fixed_mem + *(intf *)q);
389 return (voidpf)(fixed_mem + fixed_left);
390}
391
392
393local void ffree(q, p)
394voidpf q;
395voidpf p;
396{
397 Assert(0, "inflate_trees ffree called!");
398 if (q) q = p; /* to make some compilers happy */
399} 390}
400 391
401 392
@@ -405,19 +396,18 @@ uIntf *bd; /* distance desired/actual bit depth */
405inflate_huft * FAR *tl; /* literal/length tree result */ 396inflate_huft * FAR *tl; /* literal/length tree result */
406inflate_huft * FAR *td; /* distance tree result */ 397inflate_huft * FAR *td; /* distance tree result */
407{ 398{
408 /* build fixed tables if not built already--lock out other instances */ 399 /* build fixed tables if not already (multiple overlapped executions ok) */
409 while (++fixed_lock > 1)
410 fixed_lock--;
411 if (!fixed_built) 400 if (!fixed_built)
412 { 401 {
413 int k; /* temporary variable */ 402 int k; /* temporary variable */
414 unsigned c[288]; /* length list for huft_build */ 403 unsigned c[288]; /* length list for huft_build */
415 z_stream z; /* for falloc function */ 404 z_stream z; /* for falloc function */
405 int f = FIXEDH; /* number of hufts left in fixed_mem */
416 406
417 /* set up fake z_stream for memory routines */ 407 /* set up fake z_stream for memory routines */
418 z.zalloc = falloc; 408 z.zalloc = falloc;
419 z.zfree = ffree; 409 z.zfree = Z_NULL;
420 z.opaque = Z_NULL; 410 z.opaque = (voidpf)&f;
421 411
422 /* literal table */ 412 /* literal table */
423 for (k = 0; k < 144; k++) 413 for (k = 0; k < 144; k++)
@@ -438,9 +428,9 @@ inflate_huft * FAR *td; /* distance tree result */
438 huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z); 428 huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z);
439 429
440 /* done */ 430 /* done */
431 Assert(f == 0, "invalid build of fixed tables");
441 fixed_built = 1; 432 fixed_built = 1;
442 } 433 }
443 fixed_lock--;
444 *bl = fixed_bl; 434 *bl = fixed_bl;
445 *bd = fixed_bd; 435 *bd = fixed_bd;
446 *tl = fixed_tl; 436 *tl = fixed_tl;
@@ -456,10 +446,19 @@ z_stream *z; /* for zfree function */
456 list of the tables it made, with the links in a dummy first entry of 446 list of the tables it made, with the links in a dummy first entry of
457 each table. */ 447 each table. */
458{ 448{
459 register inflate_huft *p, *q; 449 register inflate_huft *p, *q, *r;
460 450
451 /* Reverse linked list */
452 p = Z_NULL;
453 q = t;
454 while (q != Z_NULL)
455 {
456 r = (q - 1)->next;
457 (q - 1)->next = p;
458 p = q;
459 q = r;
460 }
461 /* Go through linked list, freeing from the malloced (t[-1]) address. */ 461 /* Go through linked list, freeing from the malloced (t[-1]) address. */
462 p = t;
463 while (p != Z_NULL) 462 while (p != Z_NULL)
464 { 463 {
465 q = (--p)->next; 464 q = (--p)->next;
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 4c8c2d2..c4809a9 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 {
@@ -70,17 +75,17 @@ struct inflate_blocks_state {
70#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}} 75#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
71#define DUMPBITS(j) {b>>=(j);k-=(j);} 76#define DUMPBITS(j) {b>>=(j);k-=(j);}
72/* output bytes */ 77/* output bytes */
73#define WAVAIL (q<s->read?s->read-q-1:s->end-q) 78#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
74#define LOADOUT {q=s->write;m=WAVAIL;} 79#define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
75#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=WAVAIL;}} 80#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} 81#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;} 82#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--;} 83#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
79/* load local pointers */ 84/* load local pointers */
80#define LOAD {LOADIN LOADOUT} 85#define LOAD {LOADIN LOADOUT}
81 86
82/* masks for lower bits */ 87/* masks for lower bits (size given to avoid silly warnings with Visual C++) */
83extern uInt inflate_mask[]; 88extern uInt inflate_mask[17];
84 89
85/* copy as much as possible from the sliding window to the output area */ 90/* copy as much as possible from the sliding window to the output area */
86extern int inflate_flush OF(( 91extern int inflate_flush OF((
@@ -89,3 +94,5 @@ extern int inflate_flush OF((
89 int)); 94 int));
90 95
91struct internal_state {int dummy;}; /* for buggy compilers */ 96struct internal_state {int dummy;}; /* for buggy compilers */
97
98#endif
diff --git a/minigzip.c b/minigzip.c
index 1aa67f2..48c21d5 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
@@ -18,14 +18,13 @@
18#include <stdio.h> 18#include <stdio.h>
19#include "zlib.h" 19#include "zlib.h"
20 20
21#ifndef __GO32__
22extern void exit OF((int));
23#endif
24extern int unlink OF((const char *));
25
26#ifdef STDC 21#ifdef STDC
27# include <string.h> 22# include <string.h>
23# include <stdlib.h>
24#else
25 extern void exit OF((int));
28#endif 26#endif
27extern int unlink OF((const char *));
29 28
30#if defined(MSDOS) || defined(OS2) || defined(WIN32) 29#if defined(MSDOS) || defined(OS2) || defined(WIN32)
31# include <fcntl.h> 30# include <fcntl.h>
@@ -53,7 +52,7 @@ extern int unlink OF((const char *));
53 52
54char *prog; 53char *prog;
55 54
56void error OF((char *msg)); 55void error OF((const char *msg));
57void gz_compress OF((FILE *in, gzFile out)); 56void gz_compress OF((FILE *in, gzFile out));
58void gz_uncompress OF((gzFile in, FILE *out)); 57void gz_uncompress OF((gzFile in, FILE *out));
59void file_compress OF((char *file)); 58void file_compress OF((char *file));
@@ -64,7 +63,7 @@ int main OF((int argc, char *argv[]));
64 * Display error message and exit 63 * Display error message and exit
65 */ 64 */
66void error(msg) 65void error(msg)
67 char *msg; 66 const char *msg;
68{ 67{
69 fprintf(stderr, "%s: %s\n", prog, msg); 68 fprintf(stderr, "%s: %s\n", prog, msg);
70 exit(1); 69 exit(1);
@@ -89,7 +88,7 @@ void gz_compress(in, out)
89 } 88 }
90 if (len == 0) break; 89 if (len == 0) break;
91 90
92 if (gzwrite(out, buf, len) != len) error(gzerror(out, &err)); 91 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
93 } 92 }
94 fclose(in); 93 fclose(in);
95 if (gzclose(out) != Z_OK) error("failed gzclose"); 94 if (gzclose(out) != Z_OK) error("failed gzclose");
@@ -111,7 +110,9 @@ void gz_uncompress(in, out)
111 if (len < 0) error (gzerror(in, &err)); 110 if (len < 0) error (gzerror(in, &err));
112 if (len == 0) break; 111 if (len == 0) break;
113 112
114 if (fwrite(buf, 1, len, out) != (uInt)len) error("failed fwrite"); 113 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
114 error("failed fwrite");
115 }
115 } 116 }
116 if (fclose(out)) error("failed fclose"); 117 if (fclose(out)) error("failed fclose");
117 118
diff --git a/trees.c b/trees.c
index 4cb5454..dd91b73 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
@@ -78,13 +78,12 @@ local uch bl_order[BL_CODES]
78 78
79/* =========================================================================== 79/* ===========================================================================
80 * Local data. These are initialized only once. 80 * Local data. These are initialized only once.
81 * To do: initialize at compile time to be completely reentrant. ???
82 */ 81 */
83 82
84local ct_data static_ltree[L_CODES+2]; 83local ct_data static_ltree[L_CODES+2];
85/* 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
86 * 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
87 * The codes 286 and 287 are needed to build a canonical tree (see ct_init 86 * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
88 * below). 87 * below).
89 */ 88 */
90 89
@@ -129,7 +128,7 @@ local static_tree_desc static_bl_desc =
129 * Local (static) routines in this file. 128 * Local (static) routines in this file.
130 */ 129 */
131 130
132local void ct_static_init OF((void)); 131local void tr_static_init OF((void));
133local void init_block OF((deflate_state *s)); 132local void init_block OF((deflate_state *s));
134local void pqdownheap OF((deflate_state *s, ct_data *tree, int k)); 133local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
135local void gen_bitlen OF((deflate_state *s, tree_desc *desc)); 134local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
@@ -187,7 +186,7 @@ local void send_bits(s, value, length)
187 int value; /* value to send */ 186 int value; /* value to send */
188 int length; /* number of bits */ 187 int length; /* number of bits */
189{ 188{
190 Tracev((stderr," l %2d v %4x ", length, value)); 189 Tracevv((stderr," l %2d v %4x ", length, value));
191 Assert(length > 0 && length <= 15, "invalid length"); 190 Assert(length > 0 && length <= 15, "invalid length");
192 s->bits_sent += (ulg)length; 191 s->bits_sent += (ulg)length;
193 192
@@ -227,11 +226,13 @@ local void send_bits(s, value, length)
227/* the arguments must not have side effects */ 226/* the arguments must not have side effects */
228 227
229/* =========================================================================== 228/* ===========================================================================
230 * Initialize the various 'constant' tables. 229 * Initialize the various 'constant' tables. In a multi-threaded environment,
231 * To do: do this at compile time. 230 * this function may be called by two threads concurrently, but this is
231 * harmless since both invocations do exactly the same thing.
232 */ 232 */
233local void ct_static_init() 233local void tr_static_init()
234{ 234{
235 static static_init_done = 0;
235 int n; /* iterates over tree elements */ 236 int n; /* iterates over tree elements */
236 int bits; /* bit counter */ 237 int bits; /* bit counter */
237 int length; /* length value */ 238 int length; /* length value */
@@ -240,6 +241,8 @@ local void ct_static_init()
240 ush bl_count[MAX_BITS+1]; 241 ush bl_count[MAX_BITS+1];
241 /* number of codes at each bit length for an optimal tree */ 242 /* number of codes at each bit length for an optimal tree */
242 243
244 if (static_init_done) return;
245
243 /* Initialize the mapping length (0..255) -> length code (0..28) */ 246 /* Initialize the mapping length (0..255) -> length code (0..28) */
244 length = 0; 247 length = 0;
245 for (code = 0; code < LENGTH_CODES-1; code++) { 248 for (code = 0; code < LENGTH_CODES-1; code++) {
@@ -248,7 +251,7 @@ local void ct_static_init()
248 length_code[length++] = (uch)code; 251 length_code[length++] = (uch)code;
249 } 252 }
250 } 253 }
251 Assert (length == 256, "ct_static_init: length != 256"); 254 Assert (length == 256, "tr_static_init: length != 256");
252 /* Note that the length 255 (match length 258) can be represented 255 /* Note that the length 255 (match length 258) can be represented
253 * in two different ways: code 284 + 5 bits or code 285, so we 256 * in two different ways: code 284 + 5 bits or code 285, so we
254 * overwrite length_code[255] to use the best encoding: 257 * overwrite length_code[255] to use the best encoding:
@@ -263,7 +266,7 @@ local void ct_static_init()
263 dist_code[dist++] = (uch)code; 266 dist_code[dist++] = (uch)code;
264 } 267 }
265 } 268 }
266 Assert (dist == 256, "ct_static_init: dist != 256"); 269 Assert (dist == 256, "tr_static_init: dist != 256");
267 dist >>= 7; /* from now on, all distances are divided by 128 */ 270 dist >>= 7; /* from now on, all distances are divided by 128 */
268 for ( ; code < D_CODES; code++) { 271 for ( ; code < D_CODES; code++) {
269 base_dist[code] = dist << 7; 272 base_dist[code] = dist << 7;
@@ -271,7 +274,7 @@ local void ct_static_init()
271 dist_code[256 + dist++] = (uch)code; 274 dist_code[256 + dist++] = (uch)code;
272 } 275 }
273 } 276 }
274 Assert (dist == 256, "ct_static_init: 256+dist != 512"); 277 Assert (dist == 256, "tr_static_init: 256+dist != 512");
275 278
276 /* Construct the codes of the static literal tree */ 279 /* Construct the codes of the static literal tree */
277 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; 280 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
@@ -289,19 +292,18 @@ local void ct_static_init()
289 /* The static distance tree is trivial: */ 292 /* The static distance tree is trivial: */
290 for (n = 0; n < D_CODES; n++) { 293 for (n = 0; n < D_CODES; n++) {
291 static_dtree[n].Len = 5; 294 static_dtree[n].Len = 5;
292 static_dtree[n].Code = bi_reverse(n, 5); 295 static_dtree[n].Code = bi_reverse((unsigned)n, 5);
293 } 296 }
297 static_init_done = 1;
294} 298}
295 299
296/* =========================================================================== 300/* ===========================================================================
297 * Initialize the tree data structures for a new zlib stream. 301 * Initialize the tree data structures for a new zlib stream.
298 */ 302 */
299void ct_init(s) 303void _tr_init(s)
300 deflate_state *s; 304 deflate_state *s;
301{ 305{
302 if (static_dtree[0].Len == 0) { 306 tr_static_init();
303 ct_static_init(); /* To do: at compile time */
304 }
305 307
306 s->compressed_len = 0L; 308 s->compressed_len = 0L;
307 309
@@ -523,7 +525,7 @@ local void gen_codes (tree, max_code, bl_count)
523 /* Now reverse the bits */ 525 /* Now reverse the bits */
524 tree[n].Code = bi_reverse(next_code[len]++, len); 526 tree[n].Code = bi_reverse(next_code[len]++, len);
525 527
526 Tracec(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", 528 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
527 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1)); 529 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
528 } 530 }
529} 531}
@@ -783,14 +785,14 @@ local void send_all_trees(s, lcodes, dcodes, blcodes)
783/* =========================================================================== 785/* ===========================================================================
784 * Send a stored block 786 * Send a stored block
785 */ 787 */
786void ct_stored_block(s, buf, stored_len, eof) 788void _tr_stored_block(s, buf, stored_len, eof)
787 deflate_state *s; 789 deflate_state *s;
788 charf *buf; /* input block */ 790 charf *buf; /* input block */
789 ulg stored_len; /* length of input block */ 791 ulg stored_len; /* length of input block */
790 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 */
791{ 793{
792 send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */ 794 send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
793 s->compressed_len = (s->compressed_len + 3 + 7) & ~7L; 795 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
794 s->compressed_len += (stored_len + 4) << 3; 796 s->compressed_len += (stored_len + 4) << 3;
795 797
796 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */ 798 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
@@ -799,12 +801,15 @@ void ct_stored_block(s, buf, stored_len, eof)
799/* =========================================================================== 801/* ===========================================================================
800 * Send one empty static block to give enough lookahead for inflate. 802 * Send one empty static block to give enough lookahead for inflate.
801 * This takes 10 bits, of which 7 may remain in the bit buffer. 803 * This takes 10 bits, of which 7 may remain in the bit buffer.
802 * The current inflate code requires 9 bits of lookahead. If the EOB 804 * The current inflate code requires 9 bits of lookahead. If the
803 * code for the previous block was coded on 5 bits or less, inflate 805 * last two codes for the previous block (real code plus EOB) were coded
804 * may have only 5+3 bits of lookahead to decode this EOB. 806 * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
805 * (There are no problems if the previous block is stored or fixed.) 807 * the last real code. In this case we send two empty static blocks instead
808 * of one. (There are no problems if the previous block is stored or fixed.)
809 * To simplify the code, we assume the worst case of last real code encoded
810 * on one bit only.
806 */ 811 */
807void ct_align(s) 812void _tr_align(s)
808 deflate_state *s; 813 deflate_state *s;
809{ 814{
810 send_bits(s, STATIC_TREES<<1, 3); 815 send_bits(s, STATIC_TREES<<1, 3);
@@ -812,10 +817,11 @@ void ct_align(s)
812 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ 817 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
813 bi_flush(s); 818 bi_flush(s);
814 /* Of the 10 bits for the empty block, we have already sent 819 /* Of the 10 bits for the empty block, we have already sent
815 * (10 - bi_valid) bits. The lookahead for the EOB of the previous 820 * (10 - bi_valid) bits. The lookahead for the last real code (before
816 * block was thus its length plus what we have just sent. 821 * the EOB of the previous block) was thus at least one plus the length
822 * of the EOB plus what we have just sent of the empty static block.
817 */ 823 */
818 if (s->last_eob_len + 10 - s->bi_valid < 9) { 824 if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
819 send_bits(s, STATIC_TREES<<1, 3); 825 send_bits(s, STATIC_TREES<<1, 3);
820 send_code(s, END_BLOCK, static_ltree); 826 send_code(s, END_BLOCK, static_ltree);
821 s->compressed_len += 10L; 827 s->compressed_len += 10L;
@@ -829,44 +835,52 @@ void ct_align(s)
829 * 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
830 * returns the total compressed length for the file so far. 836 * returns the total compressed length for the file so far.
831 */ 837 */
832ulg ct_flush_block(s, buf, stored_len, eof) 838ulg _tr_flush_block(s, buf, stored_len, eof)
833 deflate_state *s; 839 deflate_state *s;
834 charf *buf; /* input block, or NULL if too old */ 840 charf *buf; /* input block, or NULL if too old */
835 ulg stored_len; /* length of input block */ 841 ulg stored_len; /* length of input block */
836 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 */
837{ 843{
838 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 */
839 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 */
840 846
841 /* Check if the file is ascii or binary */ 847 /* Build the Huffman trees unless a stored block is forced */
842 if (s->data_type == UNKNOWN) set_data_type(s); 848 if (s->level > 0) {
843 849
844 /* Construct the literal and distance trees */ 850 /* Check if the file is ascii or binary */
845 build_tree(s, (tree_desc *)(&(s->l_desc))); 851 if (s->data_type == Z_UNKNOWN) set_data_type(s);
846 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
847 s->static_len));
848 852
849 build_tree(s, (tree_desc *)(&(s->d_desc))); 853 /* Construct the literal and distance trees */
850 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, 854 build_tree(s, (tree_desc *)(&(s->l_desc)));
851 s->static_len)); 855 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
852 /* At this point, opt_len and static_len are the total bit lengths of 856 s->static_len));
853 * the compressed block data, excluding the tree representations.
854 */
855 857
856 /* Build the bit length tree for the above two trees, and get the index 858 build_tree(s, (tree_desc *)(&(s->d_desc)));
857 * in bl_order of the last bit length code to send. 859 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
858 */ 860 s->static_len));
859 max_blindex = build_bl_tree(s); 861 /* At this point, opt_len and static_len are the total bit lengths of
862 * the compressed block data, excluding the tree representations.
863 */
860 864
861 /* Determine the best encoding. Compute first the block length in bytes */ 865 /* Build the bit length tree for the above two trees, and get the index
862 opt_lenb = (s->opt_len+3+7)>>3; 866 * in bl_order of the last bit length code to send.
863 static_lenb = (s->static_len+3+7)>>3; 867 */
868 max_blindex = build_bl_tree(s);
864 869
865 Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", 870 /* Determine the best encoding. Compute first the block length in bytes*/
866 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, 871 opt_lenb = (s->opt_len+3+7)>>3;
867 s->last_lit)); 872 static_lenb = (s->static_len+3+7)>>3;
868 873
869 if (static_lenb <= opt_lenb) opt_lenb = static_lenb; 874 Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
875 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
876 s->last_lit));
877
878 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
879
880 } else {
881 Assert(buf != (char*)0, "lost buf");
882 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
883 }
870 884
871 /* If compression failed and this is the first and last block, 885 /* If compression failed and this is the first and last block,
872 * and if the .zip file can be seeked (to rewrite the local header), 886 * and if the .zip file can be seeked (to rewrite the local header),
@@ -874,7 +888,7 @@ ulg ct_flush_block(s, buf, stored_len, eof)
874 */ 888 */
875#ifdef STORED_FILE_OK 889#ifdef STORED_FILE_OK
876# ifdef FORCE_STORED_FILE 890# ifdef FORCE_STORED_FILE
877 if (eof && compressed_len == 0L) { /* force stored file */ 891 if (eof && s->compressed_len == 0L) { /* force stored file */
878# else 892# else
879 if (stored_len <= opt_lenb && eof && s->compressed_len==0L && seekable()) { 893 if (stored_len <= opt_lenb && eof && s->compressed_len==0L && seekable()) {
880# endif 894# endif
@@ -899,7 +913,7 @@ ulg ct_flush_block(s, buf, stored_len, eof)
899 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to 913 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
900 * transform a block into a stored block. 914 * transform a block into a stored block.
901 */ 915 */
902 ct_stored_block(s, buf, stored_len, eof); 916 _tr_stored_block(s, buf, stored_len, eof);
903 917
904#ifdef FORCE_STATIC 918#ifdef FORCE_STATIC
905 } else if (static_lenb >= 0) { /* force static trees */ 919 } else if (static_lenb >= 0) { /* force static trees */
@@ -933,10 +947,10 @@ ulg ct_flush_block(s, buf, stored_len, eof)
933 * 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
934 * the current block must be flushed. 948 * the current block must be flushed.
935 */ 949 */
936int ct_tally (s, dist, lc) 950int _tr_tally (s, dist, lc)
937 deflate_state *s; 951 deflate_state *s;
938 int dist; /* distance of matched string */ 952 unsigned dist; /* distance of matched string */
939 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) */
940{ 954{
941 s->d_buf[s->last_lit] = (ush)dist; 955 s->d_buf[s->last_lit] = (ush)dist;
942 s->l_buf[s->last_lit++] = (uch)lc; 956 s->l_buf[s->last_lit++] = (uch)lc;
@@ -949,7 +963,7 @@ int ct_tally (s, dist, lc)
949 dist--; /* dist = match distance - 1 */ 963 dist--; /* dist = match distance - 1 */
950 Assert((ush)dist < (ush)MAX_DIST(s) && 964 Assert((ush)dist < (ush)MAX_DIST(s) &&
951 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && 965 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
952 (ush)d_code(dist) < (ush)D_CODES, "ct_tally: bad match"); 966 (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
953 967
954 s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++; 968 s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++;
955 s->dyn_dtree[d_code(dist)].Freq++; 969 s->dyn_dtree[d_code(dist)].Freq++;
@@ -959,7 +973,7 @@ int ct_tally (s, dist, lc)
959 if (s->level > 2 && (s->last_lit & 0xfff) == 0) { 973 if (s->level > 2 && (s->last_lit & 0xfff) == 0) {
960 /* Compute an upper bound for the compressed length */ 974 /* Compute an upper bound for the compressed length */
961 ulg out_length = (ulg)s->last_lit*8L; 975 ulg out_length = (ulg)s->last_lit*8L;
962 ulg in_length = (ulg)s->strstart - s->block_start; 976 ulg in_length = (ulg)((long)s->strstart - s->block_start);
963 int dcode; 977 int dcode;
964 for (dcode = 0; dcode < D_CODES; dcode++) { 978 for (dcode = 0; dcode < D_CODES; dcode++) {
965 out_length += (ulg)s->dyn_dtree[dcode].Freq * 979 out_length += (ulg)s->dyn_dtree[dcode].Freq *
@@ -1043,7 +1057,7 @@ local void set_data_type(s)
1043 while (n < 7) bin_freq += s->dyn_ltree[n++].Freq; 1057 while (n < 7) bin_freq += s->dyn_ltree[n++].Freq;
1044 while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq; 1058 while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq;
1045 while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq; 1059 while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq;
1046 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);
1047} 1061}
1048 1062
1049/* =========================================================================== 1063/* ===========================================================================
diff --git a/uncompr.c b/uncompr.c
index b423696..3431561 100644
--- a/uncompr.c
+++ b/uncompr.c
@@ -1,5 +1,5 @@
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
@@ -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 3898094..e17520c 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,22 +9,55 @@
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 * People prefering a unique prefix for all types and library functions
13 add at least a handler for SIGSEGV when decompressing; the library checks 13 * should compile with -DZ_PREFIX
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 deflateCopy z_deflateCopy
24# define deflateReset z_deflateReset
25# define deflateParams z_deflateParams
26# define inflateInit2_ z_inflateInit2_
27# define inflateSync z_inflateSync
28# define inflateReset z_inflateReset
29# define compress z_compress
30# define uncompress z_uncompress
31# define adler32 z_adler32
32# define crc32 z_crc32
33# define get_crc_table z_get_crc_table
34
35# define Byte z_Byte
36# define uInt z_uInt
37# define uLong z_uLong
38# define Bytef z_Bytef
39# define charf z_charf
40# define intf z_intf
41# define uIntf z_uIntf
42# define uLongf z_uLongf
43# define voidpf z_voidpf
44# define voidp z_voidp
45#endif
17 46
18/* 47#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
19 * Compile with -DMAXSEG_64K if the alloc function cannot allocate more 48# define WIN32
20 * than 64k bytes at a time (needed on systems with 16-bit int). 49#endif
21 */ 50#if (defined(__GNUC__) || defined(WIN32)) && !defined(__32BIT__)
22#if defined(_GNUC__) && !defined(__32BIT__)
23# define __32BIT__ 51# define __32BIT__
24#endif 52#endif
25#if defined(__MSDOS__) && !defined(MSDOS) 53#if defined(__MSDOS__) && !defined(MSDOS)
26# define MSDOS 54# define MSDOS
27#endif 55#endif
56
57/*
58 * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
59 * than 64k bytes at a time (needed on systems with 16-bit int).
60 */
28#if defined(MSDOS) && !defined(__32BIT__) 61#if defined(MSDOS) && !defined(__32BIT__)
29# define MAXSEG_64K 62# define MAXSEG_64K
30#endif 63#endif
@@ -32,20 +65,20 @@
32# define UNALIGNED_OK 65# define UNALIGNED_OK
33#endif 66#endif
34 67
35#ifndef STDC 68#if (defined(MSDOS) || defined(_WINDOWS) || defined(WIN32)) && !defined(STDC)
36# if defined(MSDOS) || defined(__STDC__) || defined(__cplusplus) 69# define STDC
37# define STDC 70#endif
38# endif 71#if (defined(__STDC__) || defined(__cplusplus)) && !defined(STDC)
72# define STDC
39#endif 73#endif
40 74
41#ifndef STDC 75#if !defined(STDC) && !defined(const)
42# ifndef const 76# define const
43# define const
44# endif
45#endif 77#endif
46 78
47#ifdef __MWERKS__ /* Metrowerks CodeWarrior declares fileno() in unix.h */ 79#ifdef __MWERKS__ /* Metrowerks CodeWarrior declares fileno() in unix.h */
48# include <unix.h> 80# include <unix.h>
81# define Byte _Byte /* Byte already used on Mac */
49#endif 82#endif
50 83
51/* Maximum value for memLevel in deflateInit2 */ 84/* Maximum value for memLevel in deflateInit2 */
@@ -92,6 +125,7 @@
92 * just define FAR to be empty. 125 * just define FAR to be empty.
93 */ 126 */
94#if defined(M_I86SM) || defined(M_I86MM) /* MSC small or medium model */ 127#if defined(M_I86SM) || defined(M_I86MM) /* MSC small or medium model */
128# define SMALL_MEDIUM
95# ifdef _MSC_VER 129# ifdef _MSC_VER
96# define FAR __far 130# define FAR __far
97# else 131# else
@@ -99,20 +133,26 @@
99# endif 133# endif
100#endif 134#endif
101#if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__)) 135#if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__))
102# define FAR __far /* completely untested, just a best guess */ 136# define SMALL_MEDIUM
137# define FAR __far
103#endif 138#endif
104#ifndef FAR 139#ifndef FAR
105# define FAR 140# define FAR
106#endif 141#endif
142/* The Watcom compiler defines M_I86SM and __SMALL__ even in 32 bit mode */
143#if defined(__WATCOMC__) && defined(__386__)
144# undef FAR
145# undef SMALL_MEDIUM
146#endif
107 147
108typedef unsigned char Byte; /* 8 bits */ 148typedef unsigned char Byte; /* 8 bits */
109typedef unsigned int uInt; /* 16 bits or more */ 149typedef unsigned int uInt; /* 16 bits or more */
110typedef unsigned long uLong; /* 32 bits or more */ 150typedef unsigned long uLong; /* 32 bits or more */
111 151
112typedef Byte FAR Bytef; 152typedef Byte FAR Bytef;
113typedef char FAR charf; 153typedef char FAR charf;
114typedef int FAR intf; 154typedef int FAR intf;
115typedef uInt FAR uIntf; 155typedef uInt FAR uIntf;
116typedef uLong FAR uLongf; 156typedef uLong FAR uLongf;
117 157
118#ifdef STDC 158#ifdef STDC
diff --git a/zlib.h b/zlib.h
index 73185ec..7fc868b 100644
--- a/zlib.h
+++ b/zlib.h
@@ -1,7 +1,7 @@
1/* zlib.h -- interface of the 'zlib' general purpose compression library 1/* zlib.h -- interface of the 'zlib' general purpose compression library
2 version 0.95, Aug 16th, 1995. 2 version 1.0, Jan 27th, 1996.
3 3
4 Copyright (C) 1995 Jean-loup Gailly and Mark Adler 4 Copyright (C) 1995-1996 Jean-loup Gailly and Mark Adler
5 5
6 This software is provided 'as-is', without any express or implied 6 This software is provided 'as-is', without any express or implied
7 warranty. In no event will the authors be held liable for any damages 7 warranty. In no event will the authors be held liable for any damages
@@ -26,9 +26,13 @@
26#ifndef _ZLIB_H 26#ifndef _ZLIB_H
27#define _ZLIB_H 27#define _ZLIB_H
28 28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
29#include "zconf.h" 33#include "zconf.h"
30 34
31#define ZLIB_VERSION "0.95" 35#define ZLIB_VERSION "1.0"
32 36
33/* 37/*
34 The 'zlib' compression library provides in-memory compression and 38 The 'zlib' compression library provides in-memory compression and
@@ -47,6 +51,11 @@
47 repeated calls of the compression function. In the latter case, the 51 repeated calls of the compression function. In the latter case, the
48 application must provide more input and/or consume the output 52 application must provide more input and/or consume the output
49 (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.
50*/ 59*/
51 60
52typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); 61typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
@@ -68,10 +77,11 @@ typedef struct z_stream_s {
68 77
69 alloc_func zalloc; /* used to allocate the internal state */ 78 alloc_func zalloc; /* used to allocate the internal state */
70 free_func zfree; /* used to free the internal state */ 79 free_func zfree; /* used to free the internal state */
71 voidp opaque; /* private data object passed to zalloc and zfree */ 80 voidpf opaque; /* private data object passed to zalloc and zfree */
72
73 Byte data_type; /* best guess about the data type: ascii or binary */
74 81
82 int data_type; /* best guess about the data type: ascii or binary */
83 uLong adler; /* adler32 value of the uncompressed data */
84 uLong reserved; /* reserved for future use */
75} z_stream; 85} z_stream;
76 86
77/* 87/*
@@ -107,20 +117,25 @@ typedef struct z_stream_s {
107 117
108#define Z_NO_FLUSH 0 118#define Z_NO_FLUSH 0
109#define Z_PARTIAL_FLUSH 1 119#define Z_PARTIAL_FLUSH 1
110#define Z_FULL_FLUSH 2 120#define Z_SYNC_FLUSH 2
111#define Z_SYNC_FLUSH 3 /* experimental: partial_flush + byte align */ 121#define Z_FULL_FLUSH 3
112#define Z_FINISH 4 122#define Z_FINISH 4
113/* See deflate() below for the usage of these constants */ 123/* Allowed flush values; see deflate() below for details */
114 124
115#define Z_OK 0 125#define Z_OK 0
116#define Z_STREAM_END 1 126#define Z_STREAM_END 1
127#define Z_NEED_DICT 2
117#define Z_ERRNO (-1) 128#define Z_ERRNO (-1)
118#define Z_STREAM_ERROR (-2) 129#define Z_STREAM_ERROR (-2)
119#define Z_DATA_ERROR (-3) 130#define Z_DATA_ERROR (-3)
120#define Z_MEM_ERROR (-4) 131#define Z_MEM_ERROR (-4)
121#define Z_BUF_ERROR (-5) 132#define Z_BUF_ERROR (-5)
122/* 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 */
123 137
138#define Z_NO_COMPRESSION 0
124#define Z_BEST_SPEED 1 139#define Z_BEST_SPEED 1
125#define Z_BEST_COMPRESSION 9 140#define Z_BEST_COMPRESSION 9
126#define Z_DEFAULT_COMPRESSION (-1) 141#define Z_DEFAULT_COMPRESSION (-1)
@@ -129,15 +144,19 @@ typedef struct z_stream_s {
129#define Z_FILTERED 1 144#define Z_FILTERED 1
130#define Z_HUFFMAN_ONLY 2 145#define Z_HUFFMAN_ONLY 2
131#define Z_DEFAULT_STRATEGY 0 146#define Z_DEFAULT_STRATEGY 0
147/* compression strategy; see deflateInit2() below for details */
132 148
133#define Z_BINARY 0 149#define Z_BINARY 0
134#define Z_ASCII 1 150#define Z_ASCII 1
135#define Z_UNKNOWN 2 151#define Z_UNKNOWN 2
136/* Used to set the data_type field */ 152/* Possible values of the data_type field */
153
154#define Z_DEFLATED 8
155/* The deflate compression method (the only one supported in this version) */
137 156
138#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ 157#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
139 158
140extern char *zlib_version; 159extern const char *zlib_version;
141/* The application can compare zlib_version and ZLIB_VERSION for consistency. 160/* The application can compare zlib_version and ZLIB_VERSION for consistency.
142 If the first character differs, the library code actually used is 161 If the first character differs, the library code actually used is
143 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.
@@ -145,20 +164,24 @@ extern char *zlib_version;
145 164
146 /* basic functions */ 165 /* basic functions */
147 166
148extern int deflateInit OF((z_stream *strm, int level));
149/* 167/*
168extern int deflateInit OF((z_stream *strm, int level));
169
150 Initializes the internal stream state for compression. The fields 170 Initializes the internal stream state for compression. The fields
151 zalloc, zfree and opaque must be initialized before by the caller. 171 zalloc, zfree and opaque must be initialized before by the caller.
152 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
153 use default allocation functions. 173 use default allocation functions.
154 174
155 The compression level must be Z_DEFAULT_COMPRESSION, or between 1 and 9: 175 The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
156 1 gives best speed, 9 gives best compression. Z_DEFAULT_COMPRESSION requests 176 1 gives best speed, 9 gives best compression, 0 gives no compression at
157 a default compromise between speed and compression (currently equivalent 177 all (the input data is simply copied a block at a time).
158 to level 6). 178 Z_DEFAULT_COMPRESSION requests a default compromise between speed and
179 compression (currently equivalent to level 6).
159 180
160 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
161 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).
162 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
163 perform any compression: this will be done by deflate(). 186 perform any compression: this will be done by deflate().
164*/ 187*/
@@ -190,7 +213,12 @@ extern int deflate OF((z_stream *strm, int flush));
190 block is terminated and flushed to the output buffer so that the 213 block is terminated and flushed to the output buffer so that the
191 decompressor can get all input data available so far. For method 9, a future 214 decompressor can get all input data available so far. For method 9, a future
192 variant on method 8, the current block will be flushed but not terminated. 215 variant on method 8, the current block will be flushed but not terminated.
193 If flush is set to Z_FULL_FLUSH, the compression block is terminated, a 216 Z_SYNC_FLUSH has the same effect as partial flush except that the compressed
217 output is byte aligned (the compressor can clear its internal bit buffer)
218 and the current block is always terminated; this can be useful if the
219 compressor has to be restarted from scratch after an interruption (in which
220 case the internal state of the compressor may be lost).
221 If flush is set to Z_FULL_FLUSH, the compression block is terminated, a
194 special marker is output and the compression dictionary is discarded; this 222 special marker is output and the compression dictionary is discarded; this
195 is useful to allow the decompressor to synchronize if one compressed block 223 is useful to allow the decompressor to synchronize if one compressed block
196 has been damaged (see inflateSync below). Flushing degrades compression and 224 has been damaged (see inflateSync below). Flushing degrades compression and
@@ -233,22 +261,26 @@ extern int deflateEnd OF((z_stream *strm));
233 pending output. 261 pending output.
234 262
235 deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the 263 deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
236 stream state was inconsistent. In the error case, msg may be set 264 stream state was inconsistent, Z_DATA_ERROR if the stream was freed
237 but then points to a static string (which must not be deallocated). 265 prematurely (some input or output was discarded). In the error case,
266 msg may be set but then points to a static string (which must not be
267 deallocated).
238*/ 268*/
239 269
240 270
241extern int inflateInit OF((z_stream *strm));
242/* 271/*
272extern int inflateInit OF((z_stream *strm));
273
243 Initializes the internal stream state for decompression. The fields 274 Initializes the internal stream state for decompression. The fields
244 zalloc and zfree must be initialized before by the caller. If zalloc and 275 zalloc, zfree and opaque must be initialized before by the caller. If
245 zfree are set to Z_NULL, inflateInit updates them to use default allocation 276 zalloc and zfree are set to Z_NULL, inflateInit updates them to use default
246 functions. 277 allocation functions.
247 278
248 inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not 279 inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
249 enough memory. msg is set to null if there is no error message. 280 enough memory, Z_VERSION_ERROR if the zlib library version is incompatible
250 inflateInit does not perform any decompression: this will be done by 281 with the version assumed by the caller. msg is set to null if there is no
251 inflate(). 282 error message. inflateInit does not perform any decompression: this will be
283 done by inflate().
252*/ 284*/
253 285
254 286
@@ -292,12 +324,15 @@ extern int inflate OF((z_stream *strm, int flush));
292 inflate() returns Z_OK if some progress has been made (more input 324 inflate() returns Z_OK if some progress has been made (more input
293 processed or more output produced), Z_STREAM_END if the end of the 325 processed or more output produced), Z_STREAM_END if the end of the
294 compressed data has been reached and all uncompressed output has been 326 compressed data has been reached and all uncompressed output has been
295 produced, Z_DATA_ERROR if the input data was corrupted, Z_STREAM_ERROR if 327 produced, Z_NEED_DICT if a preset dictionary is needed at this point (see
296 the stream structure was inconsistent (for example if next_in or next_out 328 inflateSetDictionary below), Z_DATA_ERROR if the input data was corrupted,
297 was NULL), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if no 329 Z_STREAM_ERROR if the stream structure was inconsistent (for example if
298 progress is possible or if there was not enough room in the output buffer 330 next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
299 when Z_FINISH is used. In the Z_DATA_ERROR case, the application may then 331 Z_BUF_ERROR if no progress is possible or if there was not enough room in
300 call inflateSync to look for a good compression block. 332 the output buffer when Z_FINISH is used. In the Z_DATA_ERROR case, the
333 application may then call inflateSync to look for a good compression block.
334 In the Z_NEED_DICT case, strm->adler is set to the Adler32 value of the
335 dictionary chosen by the compressor.
301*/ 336*/
302 337
303 338
@@ -312,24 +347,26 @@ extern int inflateEnd OF((z_stream *strm));
312 static string (which must not be deallocated). 347 static string (which must not be deallocated).
313*/ 348*/
314 349
315 /* advanced functions */ 350 /* Advanced functions */
316 351
317/* 352/*
318 The following functions are needed only in some special applications. 353 The following functions are needed only in some special applications.
319*/ 354*/
320 355
356/*
321extern int deflateInit2 OF((z_stream *strm, 357extern int deflateInit2 OF((z_stream *strm,
322 int level, 358 int level,
323 int method, 359 int method,
324 int windowBits, 360 int windowBits,
325 int memLevel, 361 int memLevel,
326 int strategy)); 362 int strategy));
327/* 363
328 This is another version of deflateInit with more compression options. The 364 This is another version of deflateInit with more compression options. The
329 fields next_in, zalloc and zfree must be initialized before by the caller. 365 fields next_in, zalloc, zfree and opaque must be initialized before by
366 the caller.
330 367
331 The method parameter is the compression method. It must be 8 in this 368 The method parameter is the compression method. It must be Z_DEFLATED in
332 version of the library. (Method 9 will allow a 64K history buffer and 369 this version of the library. (Method 9 will allow a 64K history buffer and
333 partial block flushes.) 370 partial block flushes.)
334 371
335 The windowBits parameter is the base two logarithm of the window size 372 The windowBits parameter is the base two logarithm of the window size
@@ -338,20 +375,22 @@ extern int deflateInit2 OF((z_stream *strm,
338 values of this parameter result in better compression at the expense of 375 values of this parameter result in better compression at the expense of
339 memory usage. The default value is 15 if deflateInit is used instead. 376 memory usage. The default value is 15 if deflateInit is used instead.
340 377
341 The memLevel parameter specifies how much memory should be allocated 378 The memLevel parameter specifies how much memory should be allocated
342 for the internal compression state. memLevel=1 uses minimum memory but 379 for the internal compression state. memLevel=1 uses minimum memory but
343 is slow and reduces compression ratio; memLevel=9 uses maximum memory 380 is slow and reduces compression ratio; memLevel=9 uses maximum memory
344 for optimal speed. The default value is 8. See zconf.h for total memory 381 for optimal speed. The default value is 8. See zconf.h for total memory
345 usage as a function of windowBits and memLevel. 382 usage as a function of windowBits and memLevel.
346 383
347 The strategy parameter is used to tune the compression algorithm. Use 384 The strategy parameter is used to tune the compression algorithm. Use the
348 the value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data 385 value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
349 produced by a filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman 386 filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
350 encoding only (no string match). Filtered data consists mostly of small 387 string match). Filtered data consists mostly of small values with a
351 values with a somewhat random distribution. In this case, the 388 somewhat random distribution. In this case, the compression algorithm is
352 compression algorithm is tuned to compress them better. The strategy 389 tuned to compress them better. The effect of Z_FILTERED is to force more
353 parameter only affects the compression ratio but not the correctness of 390 Huffman coding and less string matching; it is somewhat intermediate
354 the compressed output even if it is not set appropriately. 391 between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
392 the compression ratio but not the correctness of the compressed output even
393 if it is not set appropriately.
355 394
356 If next_in is not null, the library will use this buffer to hold also 395 If next_in is not null, the library will use this buffer to hold also
357 some history information; the buffer must either hold the entire input 396 some history information; the buffer must either hold the entire input
@@ -370,9 +409,38 @@ extern int deflateInit2 OF((z_stream *strm,
370 not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as 409 not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as
371 an invalid method). msg is set to null if there is no error message. 410 an invalid method). msg is set to null if there is no error message.
372 deflateInit2 does not perform any compression: this will be done by 411 deflateInit2 does not perform any compression: this will be done by
373 deflate(). 412 deflate().
374*/ 413*/
375 414
415extern int deflateSetDictionary OF((z_stream *strm,
416 const Bytef *dictionary,
417 uInt dictLength));
418/*
419 Initializes the compression dictionary (history buffer) from the given
420 byte sequence without producing any compressed output. This function must
421 be called immediately after deflateInit or deflateInit2, before any call
422 of deflate. The compressor and decompressor must use exactly the same
423 dictionary (see inflateSetDictionary).
424 The dictionary should consist of strings (byte sequences) that are likely
425 to be encountered later in the data to be compressed, with the most commonly
426 used strings preferably put towards the end of the dictionary. Using a
427 dictionary is most useful when the data to be compressed is short and
428 can be predicted with good accuracy; the data can then be compressed better
429 than with the default empty dictionary. In this version of the library,
430 only the last 32K bytes of the dictionary are used.
431 Upon return of this function, strm->adler is set to the Adler32 value
432 of the dictionary; the decompressor may later use this value to determine
433 which dictionary has been used by the compressor. (The Adler32 value
434 applies to the whole dictionary even if only a subset of the dictionary is
435 actually used by the compressor.)
436
437 deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
438 parameter is invalid (such as NULL dictionary) or the stream state
439 is inconsistent (for example if deflate has already been called for this
440 stream). deflateSetDictionary does not perform any compression: this will
441 be done by deflate().
442*/
443
376extern int deflateCopy OF((z_stream *dest, 444extern int deflateCopy OF((z_stream *dest,
377 z_stream *source)); 445 z_stream *source));
378/* 446/*
@@ -383,14 +451,14 @@ extern int deflateCopy OF((z_stream *dest,
383 application to provide the correct values of next_out and avail_out for the 451 application to provide the correct values of next_out and avail_out for the
384 next call of deflate. 452 next call of deflate.
385 453
386 This function is useful when several compression strategies will be 454 This function can be useful when several compression strategies will be
387 tried, for example when there are several ways of pre-processing the input 455 tried, for example when there are several ways of pre-processing the input
388 data with a filter. The streams that will be discarded should then be freed 456 data with a filter. The streams that will be discarded should then be freed
389 by calling deflateEnd. Note that deflateCopy duplicates the internal 457 by calling deflateEnd. Note that deflateCopy duplicates the internal
390 compression state which can be quite large, so this strategy is slow and 458 compression state which can be quite large, so this strategy is slow and
391 can consume lots of memory. 459 can consume lots of memory.
392 460
393 deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not 461 deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
394 enough memory, Z_STREAM_ERROR if the source stream state was inconsistent 462 enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
395 (such as zalloc being NULL). msg is left unchanged in both source and 463 (such as zalloc being NULL). msg is left unchanged in both source and
396 destination. 464 destination.
@@ -407,11 +475,26 @@ extern int deflateReset OF((z_stream *strm));
407 stream state was inconsistent (such as zalloc or state being NULL). 475 stream state was inconsistent (such as zalloc or state being NULL).
408*/ 476*/
409 477
478extern int deflateParams OF((z_stream *strm, int level, int strategy));
479/*
480 Dynamically update the compression level and compression strategy.
481 This can be used to switch between compression and straight copy of
482 the input data, or to switch to a different kind of input data requiring
483 a different strategy. If the compression level is changed, the input
484 available so far is compressed with the old level (and may be flushed);
485 the new level will take effect only at the next call of deflate().
486
487 deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
488 stream state was inconsistent or if a parameter was invalid.
489*/
490
491/*
410extern int inflateInit2 OF((z_stream *strm, 492extern int inflateInit2 OF((z_stream *strm,
411 int windowBits)); 493 int windowBits));
412/* 494
413 This is another version of inflateInit with more compression options. The 495 This is another version of inflateInit with more compression options. The
414 fields next_out, zalloc and zfree must be initialized before by the caller. 496 fields next_out, zalloc, zfree and opaque must be initialized before by
497 the caller.
415 498
416 The windowBits parameter is the base two logarithm of the maximum window 499 The windowBits parameter is the base two logarithm of the maximum window
417 size (the size of the history buffer). It should be in the range 8..15 for 500 size (the size of the history buffer). It should be in the range 8..15 for
@@ -440,6 +523,25 @@ extern int inflateInit2 OF((z_stream *strm,
440 inflate(). 523 inflate().
441*/ 524*/
442 525
526extern int inflateSetDictionary OF((z_stream *strm,
527 const Bytef *dictionary,
528 uInt dictLength));
529/*
530 Initializes the decompression dictionary (history buffer) from the given
531 uncompressed byte sequence. This function must be called immediately after
532 a call of inflate if this call returned Z_NEED_DICT. The dictionary chosen
533 by the compressor can be determined from the Adler32 value returned by this
534 call of inflate. The compressor and decompressor must use exactly the same
535 dictionary (see deflateSetDictionary).
536
537 inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
538 parameter is invalid (such as NULL dictionary) or the stream state is
539 inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
540 expected one (incorrect Adler32 value). inflateSetDictionary does not
541 perform any decompression: this will be done by subsequent calls of
542 inflate().
543*/
544
443extern int inflateSync OF((z_stream *strm)); 545extern int inflateSync OF((z_stream *strm));
444/* 546/*
445 Skips invalid compressed data until the special marker (see deflate() 547 Skips invalid compressed data until the special marker (see deflate()
@@ -477,7 +579,7 @@ extern int inflateReset OF((z_stream *strm));
477*/ 579*/
478 580
479extern int compress OF((Bytef *dest, uLongf *destLen, 581extern int compress OF((Bytef *dest, uLongf *destLen,
480 Bytef *source, uLong sourceLen)); 582 const Bytef *source, uLong sourceLen));
481/* 583/*
482 Compresses the source buffer into the destination buffer. sourceLen is 584 Compresses the source buffer into the destination buffer. sourceLen is
483 the byte length of the source buffer. Upon entry, destLen is the total 585 the byte length of the source buffer. Upon entry, destLen is the total
@@ -492,7 +594,7 @@ extern int compress OF((Bytef *dest, uLongf *destLen,
492*/ 594*/
493 595
494extern int uncompress OF((Bytef *dest, uLongf *destLen, 596extern int uncompress OF((Bytef *dest, uLongf *destLen,
495 Bytef *source, uLong sourceLen)); 597 const Bytef *source, uLong sourceLen));
496/* 598/*
497 Decompresses the source buffer into the destination buffer. sourceLen is 599 Decompresses the source buffer into the destination buffer. sourceLen is
498 the byte length of the source buffer. Upon entry, destLen is the total 600 the byte length of the source buffer. Upon entry, destLen is the total
@@ -512,7 +614,7 @@ extern int uncompress OF((Bytef *dest, uLongf *destLen,
512 614
513typedef voidp gzFile; 615typedef voidp gzFile;
514 616
515extern gzFile gzopen OF((char *path, char *mode)); 617extern gzFile gzopen OF((const char *path, const char *mode));
516/* 618/*
517 Opens a gzip (.gz) file for reading or writing. The mode parameter 619 Opens a gzip (.gz) file for reading or writing. The mode parameter
518 is as in fopen ("rb" or "wb") but can also include a compression level 620 is as in fopen ("rb" or "wb") but can also include a compression level
@@ -524,11 +626,15 @@ extern gzFile gzopen OF((char *path, char *mode));
524 zlib error is Z_MEM_ERROR). 626 zlib error is Z_MEM_ERROR).
525*/ 627*/
526 628
527extern gzFile gzdopen OF((int fd, char *mode)); 629extern gzFile gzdopen OF((int fd, const char *mode));
528/* 630/*
529 gzdopen() associates a gzFile with the file descriptor fd. File 631 gzdopen() associates a gzFile with the file descriptor fd. File
530 descriptors are obtained from calls like open, dup, creat, or pipe. 632 descriptors are obtained from calls like open, dup, creat, pipe or
633 fileno (in the file has been previously opened with fopen).
531 The mode parameter is as in fopen ("rb" or "wb"). 634 The mode parameter is as in fopen ("rb" or "wb").
635 The next call of gzclose on the returned gzFile will also close the
636 file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
637 descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
532 gzdopen returns NULL if there was insufficient memory to allocate 638 gzdopen returns NULL if there was insufficient memory to allocate
533 the (de)compression state. 639 the (de)compression state.
534*/ 640*/
@@ -541,7 +647,7 @@ extern int gzread OF((gzFile file, voidp buf, unsigned len));
541 gzread returns the number of uncompressed bytes actually read (0 for 647 gzread returns the number of uncompressed bytes actually read (0 for
542 end of file, -1 for error). */ 648 end of file, -1 for error). */
543 649
544extern int gzwrite OF((gzFile file, voidp buf, unsigned len)); 650extern int gzwrite OF((gzFile file, const voidp buf, unsigned len));
545/* 651/*
546 Writes the given number of uncompressed bytes into the compressed file. 652 Writes the given number of uncompressed bytes into the compressed file.
547 gzwrite returns the number of uncompressed bytes actually written 653 gzwrite returns the number of uncompressed bytes actually written
@@ -582,7 +688,7 @@ extern char* gzerror OF((gzFile file, int *errnum));
582 compression library. 688 compression library.
583*/ 689*/
584 690
585extern uLong adler32 OF((uLong adler, Bytef *buf, uInt len)); 691extern uLong adler32 OF((uLong adler, const Bytef *buf, uInt len));
586 692
587/* 693/*
588 Update a running Adler-32 checksum with the bytes buf[0..len-1] and 694 Update a running Adler-32 checksum with the bytes buf[0..len-1] and
@@ -599,7 +705,7 @@ extern uLong adler32 OF((uLong adler, Bytef *buf, uInt len));
599 if (adler != original_adler) error(); 705 if (adler != original_adler) error();
600*/ 706*/
601 707
602extern uLong crc32 OF((uLong crc, Bytef *buf, uInt len)); 708extern uLong crc32 OF((uLong crc, const Bytef *buf, uInt len));
603/* 709/*
604 Update a running crc with the bytes buf[0..len-1] and return the updated 710 Update a running crc with the bytes buf[0..len-1] and return the updated
605 crc. If buf is NULL, this function returns the required initial value 711 crc. If buf is NULL, this function returns the required initial value
@@ -615,8 +721,39 @@ extern uLong crc32 OF((uLong crc, Bytef *buf, uInt len));
615 if (crc != original_crc) error(); 721 if (crc != original_crc) error();
616*/ 722*/
617 723
724
725 /* various hacks, don't look :) */
726
727/* deflateInit and inflateInit are macros to allow checking the zlib version
728 * and the compiler's view of z_stream:
729 */
730extern int deflateInit_ OF((z_stream *strm, int level,
731 const char *version, int stream_size));
732extern int inflateInit_ OF((z_stream *strm,
733 const char *version, int stream_size));
734extern int deflateInit2_ OF((z_stream *strm, int level, int method,
735 int windowBits, int memLevel, int strategy,
736 const char *version, int stream_size));
737extern int inflateInit2_ OF((z_stream *strm, int windowBits,
738 const char *version, int stream_size));
739#define deflateInit(strm, level) \
740 deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
741#define inflateInit(strm) \
742 inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
743#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
744 deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
745 (strategy), ZLIB_VERSION, sizeof(z_stream))
746#define inflateInit2(strm, windowBits) \
747 inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
748
618#ifndef _Z_UTIL_H 749#ifndef _Z_UTIL_H
619 struct internal_state {int dummy;}; /* hack for buggy compilers */ 750 struct internal_state {int dummy;}; /* hack for buggy compilers */
620#endif 751#endif
621 752
753uLongf *get_crc_table OF((void)); /* can be used by asm versions of crc32() */
754
755#ifdef __cplusplus
756}
757#endif
758
622#endif /* _ZLIB_H */ 759#endif /* _ZLIB_H */
diff --git a/zutil.c b/zutil.c
index 67c4e54..f7bd6e5 100644
--- a/zutil.c
+++ b/zutil.c
@@ -1,5 +1,5 @@
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
@@ -11,20 +11,22 @@
11 11
12struct internal_state {int dummy;}; /* for buggy compilers */ 12struct internal_state {int dummy;}; /* for buggy compilers */
13 13
14#ifndef __GO32__ 14#ifndef STDC
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
@@ -59,9 +61,10 @@ void zmemzero(dest, len)
59} 61}
60#endif 62#endif
61 63
62#if defined( __TURBOC__) && !defined(__SMALL__) && !defined(__MEDIUM__) 64#ifdef __TURBOC__
63/* Small and medium model are for now limited to near allocation with 65#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
64 * reduced MAX_WBITS and MAX_MEM_LEVEL 66/* Small and medium model in Turbo C are for now limited to near allocation
67 * with reduced MAX_WBITS and MAX_MEM_LEVEL
65 */ 68 */
66# define MY_ZCALLOC 69# define MY_ZCALLOC
67 70
@@ -94,7 +97,10 @@ voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
94 voidpf buf = opaque; /* just to make some compilers happy */ 97 voidpf buf = opaque; /* just to make some compilers happy */
95 ulg bsize = (ulg)items*size; 98 ulg bsize = (ulg)items*size;
96 99
97 if (bsize < 65536L) { 100 /* If we allocate less than 65520 bytes, we assume that farmalloc
101 * will return a usable pointer which doesn't have to be normalized.
102 */
103 if (bsize < 65520L) {
98 buf = farmalloc(bsize); 104 buf = farmalloc(bsize);
99 if (*(ush*)&buf != 0) return buf; 105 if (*(ush*)&buf != 0) return buf;
100 } else { 106 } else {
@@ -131,9 +137,11 @@ void zcfree (voidpf opaque, voidpf ptr)
131 ptr = opaque; /* just to make some compilers happy */ 137 ptr = opaque; /* just to make some compilers happy */
132 z_error("zcfree: ptr not found"); 138 z_error("zcfree: ptr not found");
133} 139}
140#endif
134#endif /* __TURBOC__ */ 141#endif /* __TURBOC__ */
135 142
136#if defined(M_I86SM)||defined(M_I86MM)||defined(M_I86CM)||defined(M_I86LM) 143
144#if defined(M_I86) && !(defined(__WATCOMC__) && defined(__386__))
137/* Microsoft C */ 145/* Microsoft C */
138 146
139# define MY_ZCALLOC 147# define MY_ZCALLOC
@@ -160,7 +168,7 @@ void zcfree (voidpf opaque, voidpf ptr)
160 168
161#ifndef MY_ZCALLOC /* Any system without a special alloc function */ 169#ifndef MY_ZCALLOC /* Any system without a special alloc function */
162 170
163#ifndef __GO32__ 171#ifndef STDC
164extern voidp calloc OF((uInt items, uInt size)); 172extern voidp calloc OF((uInt items, uInt size));
165extern void free OF((voidpf ptr)); 173extern void free OF((voidpf ptr));
166#endif 174#endif
@@ -170,6 +178,7 @@ voidpf zcalloc (opaque, items, size)
170 unsigned items; 178 unsigned items;
171 unsigned size; 179 unsigned size;
172{ 180{
181 if (opaque) opaque = 0; /* to make compiler happy */
173 return (voidpf)calloc(items, size); 182 return (voidpf)calloc(items, size);
174} 183}
175 184
@@ -177,6 +186,7 @@ void zcfree (opaque, ptr)
177 voidpf opaque; 186 voidpf opaque;
178 voidpf ptr; 187 voidpf ptr;
179{ 188{
189 if (opaque) opaque = 0; /* to make compiler happy */
180 free(ptr); 190 free(ptr);
181} 191}
182 192
diff --git a/zutil.h b/zutil.h
index 4d1b59e..063c0e9 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
@@ -15,7 +15,7 @@
15 15
16#include "zlib.h" 16#include "zlib.h"
17 17
18#if defined(MSDOS) || defined(VMS) 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
@@ -23,6 +23,7 @@
23#endif 23#endif
24#ifdef STDC 24#ifdef STDC
25# include <string.h> 25# include <string.h>
26# include <stdlib.h>
26#endif 27#endif
27 28
28#ifndef local 29#ifndef local
@@ -36,15 +37,17 @@ typedef unsigned short ush;
36typedef ush FAR ushf; 37typedef ush FAR ushf;
37typedef unsigned long ulg; 38typedef unsigned long ulg;
38 39
39extern 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++) */
40 42
41#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))
42/* 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 */
43 48
44 /* common constants */ 49 /* common constants */
45 50
46#define DEFLATED 8
47
48#ifndef DEF_WBITS 51#ifndef DEF_WBITS
49# define DEF_WBITS MAX_WBITS 52# define DEF_WBITS MAX_WBITS
50#endif 53#endif
@@ -66,6 +69,8 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
66#define MAX_MATCH 258 69#define MAX_MATCH 258
67/* The minimum and maximum match lengths */ 70/* The minimum and maximum match lengths */
68 71
72#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
73
69 /* target dependencies */ 74 /* target dependencies */
70 75
71#ifdef MSDOS 76#ifdef MSDOS
@@ -81,7 +86,7 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
81# define OS_CODE 0x06 86# define OS_CODE 0x06
82#endif 87#endif
83 88
84#ifdef WIN32 /* Windows NT */ 89#ifdef WIN32 /* Window 95 & Windows NT */
85# define OS_CODE 0x0b 90# define OS_CODE 0x0b
86#endif 91#endif
87 92
@@ -143,11 +148,13 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
143# define HAVE_MEMCPY 148# define HAVE_MEMCPY
144#endif 149#endif
145#ifdef HAVE_MEMCPY 150#ifdef HAVE_MEMCPY
146# if defined(M_I86SM) || defined(M_I86MM) /* MSC small or medium model */ 151# ifdef SMALL_MEDIUM /* MSDOS small or medium model */
147# define zmemcpy _fmemcpy 152# define zmemcpy _fmemcpy
153# define zmemcmp _fmemcmp
148# define zmemzero(dest, len) _fmemset(dest, 0, len) 154# define zmemzero(dest, len) _fmemset(dest, 0, len)
149# else 155# else
150# define zmemcpy memcpy 156# define zmemcpy memcpy
157# define zmemcmp memcmp
151# define zmemzero(dest, len) memset(dest, 0, len) 158# define zmemzero(dest, len) memset(dest, 0, len)
152# endif 159# endif
153#else 160#else
@@ -177,7 +184,7 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
177#endif 184#endif
178 185
179 186
180typedef uLong (*check_func) OF((uLong check, Bytef *buf, uInt len)); 187typedef uLong (*check_func) OF((uLong check, const Bytef *buf, uInt len));
181 188
182extern void z_error OF((char *m)); 189extern void z_error OF((char *m));
183 190