summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2011-09-09 23:21:47 -0700
committerMark Adler <madler@alumni.caltech.edu>2011-09-09 23:21:47 -0700
commit7c2a874e50b871d04fbd19501f7b42cff55e5abc (patch)
tree1879cd29182ababb17cde77cee5ce74505db4006
parenta383133c4e7b93113cee912f213cf9502d785fa7 (diff)
downloadzlib-1.2.0.tar.gz
zlib-1.2.0.tar.bz2
zlib-1.2.0.zip
zlib 1.2.0v1.2.0
-rw-r--r--ChangeLog54
-rw-r--r--FAQ173
-rw-r--r--INDEX62
-rw-r--r--Makefile25
-rw-r--r--Makefile.in25
-rw-r--r--README23
-rw-r--r--adler32.c4
-rw-r--r--aix/README9
-rw-r--r--aix/configure.diff57
-rw-r--r--aix/mkexps37
-rw-r--r--algorithm.txt34
-rw-r--r--compress.c10
-rwxr-xr-xconfigure219
-rw-r--r--contrib/README.contrib10
-rw-r--r--contrib/blast/Makefile8
-rw-r--r--contrib/blast/README4
-rw-r--r--contrib/blast/blast.c444
-rw-r--r--contrib/blast/blast.h71
-rw-r--r--contrib/blast/test.pkbin0 -> 8 bytes
-rw-r--r--contrib/blast/test.txt1
-rw-r--r--contrib/inflate86/inffast.S1095
-rw-r--r--contrib/puff/Makefile8
-rw-r--r--contrib/puff/README63
-rw-r--r--contrib/puff/puff.c833
-rw-r--r--contrib/puff/puff.h31
-rw-r--r--contrib/puff/zeros.rawbin0 -> 1213 bytes
-rw-r--r--crc32.c350
-rw-r--r--crc32.h441
-rw-r--r--deflate.c54
-rw-r--r--example.c5
-rw-r--r--gzio.c107
-rw-r--r--infback.c615
-rw-r--r--infblock.c403
-rw-r--r--infblock.h39
-rw-r--r--infcodes.c251
-rw-r--r--infcodes.h27
-rw-r--r--inffast.c439
-rw-r--r--inffast.h10
-rw-r--r--inffixed.h241
-rw-r--r--inflate.c1541
-rw-r--r--inflate.h115
-rw-r--r--inftrees.c711
-rw-r--r--inftrees.h91
-rw-r--r--infutil.c87
-rw-r--r--infutil.h98
-rw-r--r--maketree.c85
-rw-r--r--old/Make_vms.com (renamed from Make_vms.com)0
-rw-r--r--old/Makefile.riscos (renamed from Makefile.riscos)0
-rw-r--r--old/README3
-rw-r--r--old/amiga/Makefile.pup (renamed from amiga/Makefile.pup)0
-rw-r--r--old/amiga/Makefile.sas (renamed from amiga/Makefile.sas)0
-rw-r--r--old/descrip.mms (renamed from descrip.mms)0
-rw-r--r--old/msdos/Makefile.b32 (renamed from msdos/Makefile.b32)0
-rw-r--r--old/msdos/Makefile.bor (renamed from msdos/Makefile.bor)0
-rw-r--r--old/msdos/Makefile.dj2 (renamed from msdos/Makefile.dj2)0
-rw-r--r--old/msdos/Makefile.emx (renamed from msdos/Makefile.emx)0
-rw-r--r--old/msdos/Makefile.msc (renamed from msdos/Makefile.msc)0
-rw-r--r--old/msdos/Makefile.tc (renamed from msdos/Makefile.tc)0
-rw-r--r--old/msdos/Makefile.w32 (renamed from msdos/Makefile.w32)0
-rw-r--r--old/msdos/Makefile.wat (renamed from msdos/Makefile.wat)0
-rw-r--r--old/msdos/zlib.def (renamed from msdos/zlib.def)0
-rw-r--r--old/msdos/zlib.rc (renamed from msdos/zlib.rc)0
-rw-r--r--old/nt/Makefile.emx (renamed from nt/Makefile.emx)0
-rw-r--r--old/nt/Makefile.gcc (renamed from nt/Makefile.gcc)0
-rw-r--r--old/nt/Makefile.nt (renamed from nt/Makefile.nt)0
-rw-r--r--old/nt/zlib.dnt (renamed from nt/zlib.dnt)0
-rw-r--r--old/os2/Makefile.os2 (renamed from os2/Makefile.os2)0
-rw-r--r--old/os2/zlib.def (renamed from os2/zlib.def)0
-rw-r--r--old/zlib.3 (renamed from zlib.3)0
-rw-r--r--old/zlib.html (renamed from zlib.html)0
-rw-r--r--trees.c8
-rw-r--r--uncompr.c6
-rw-r--r--zconf.h93
-rw-r--r--zconf.in.h280
-rw-r--r--zlib.h282
-rw-r--r--zutil.c10
-rw-r--r--zutil.h6
77 files changed, 7228 insertions, 2470 deletions
diff --git a/ChangeLog b/ChangeLog
index bf2e3f9..6193e00 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,57 @@
1 1
2 ChangeLog file for zlib 2 ChangeLog file for zlib
3Changes in 1.2.0 (9 March 2003)
4- New and improved inflate code
5 - About 20% faster
6 - Does not allocate 32K window unless and until needed
7 - Automatically detects and decompresses gzip streams
8 - Raw inflate no longer needs an extra dummy byte at end
9 - Added inflateBack functions using a callback interface--even faster
10 than inflate, useful for file utilities (gzip, zip)
11 - Added inflateCopy() function to record state for random access on
12 externally generated deflate streams (e.g. in gzip files)
13 - More readable code (I hope)
14- New and improved crc32()
15 - About 50% faster, thanks to suggestions from Rodney Brown
16- Add deflateBound() and compressBound() functions
17- Fix memory leak in deflateInit2()
18- Permit setting dictionary for raw deflate (for parallel deflate)
19- Fix const declaration for gzwrite()
20- Check for some malloc() failures in gzio.c
21- Fix bug in gzopen() on single-byte file 0x1f
22- Fix bug in gzread() on concatenated file with 0x1f at end of buffer
23 and next buffer doesn't start with 0x8b
24- Fix uncompress() to return Z_DATA_ERROR on truncated input
25- Free memory at end of example.c
26- Remove MAX #define in trees.c (conflicted with some libraries)
27- Fix static const's in deflate.c, gzio.c, and zutil.[ch]
28- Declare malloc() and free() in gzio.c if STDC not defined
29- Use malloc() instead of calloc() in zutil.c if int big enough
30- Define STDC for AIX
31- Add aix/ with approach for compiling shared library on AIX
32- Add HP-UX support for shared libraries in configure
33- Add OpenUNIX support for shared libraries in configure
34- Use $cc instead of gcc to build shared library
35- Make prefix directory if needed when installing
36- Correct Macintosh avoidance of typedef Byte in zconf.h
37- Correct Turbo C memory allocation when under Linux
38- Use libz.a instead of -lz in Makefile (assure use of compiled library)
39- Update configure to check for snprintf or vsnprintf functions and their
40 return value, warn during make if using an insecure function
41- Fix configure problem with compile-time knowledge of HAVE_UNISTD_H that
42 is lost when library is used--resolution is to build new zconf.h
43- Documentation improvements (in zlib.h):
44 - Document raw deflate and inflate
45 - Update RFCs URL
46 - Point out that zlib and gzip formats are different
47 - Note that Z_BUF_ERROR is not fatal
48 - Document string limit for gzprintf() and possible buffer overflow
49 - Note requirement on avail_out when flushing
50 - Note permitted values of flush parameter of inflate()
51- Add some FAQs (and even answers) to the FAQ
52- Add contrib/inflate86/ for x86 faster inflate
53- Add contrib/blast/ for PKWare Data Compression Library decompression
54- Add contrib/puff/ simple inflate for deflate format description
3 55
4Changes in 1.1.4 (11 March 2002) 56Changes in 1.1.4 (11 March 2002)
5- ZFREE was repeated on same allocation on some error conditions. 57- ZFREE was repeated on same allocation on some error conditions.
@@ -10,7 +62,7 @@ Changes in 1.1.4 (11 March 2002)
10 less than 32K. 62 less than 32K.
11- force windowBits > 8 to avoid a bug in the encoder for a window size 63- force windowBits > 8 to avoid a bug in the encoder for a window size
12 of 256 bytes. (A complete fix will be available in 1.1.5). 64 of 256 bytes. (A complete fix will be available in 1.1.5).
13 65
14Changes in 1.1.3 (9 July 1998) 66Changes in 1.1.3 (9 July 1998)
15- fix "an inflate input buffer bug that shows up on rare but persistent 67- fix "an inflate input buffer bug that shows up on rare but persistent
16 occasions" (Mark) 68 occasions" (Mark)
diff --git a/FAQ b/FAQ
index 47a7d60..abf680c 100644
--- a/FAQ
+++ b/FAQ
@@ -36,6 +36,11 @@ The lastest zlib FAQ is at http://www.gzip.org/zlib/zlib_faq.html
36 Before making the call, make sure that avail_in and avail_out are not 36 Before making the call, make sure that avail_in and avail_out are not
37 zero. When setting the parameter flush equal to Z_FINISH, also make sure 37 zero. When setting the parameter flush equal to Z_FINISH, also make sure
38 that avail_out is big enough to allow processing all pending input. 38 that avail_out is big enough to allow processing all pending input.
39 Note that a Z_BUF_ERROR is not fatal--another call to deflate() or
40 inflate() can be made with more input or output space. A Z_BUF_ERROR
41 may in fact be unavoidable depending on how the functions are used, since
42 it is not possible to tell whether or not there is more output pending
43 when strm.avail_out returns with zero.
39 44
40 6. Where's the zlib documentation (man pages, etc.)? 45 6. Where's the zlib documentation (man pages, etc.)?
41 46
@@ -88,13 +93,171 @@ The lastest zlib FAQ is at http://www.gzip.org/zlib/zlib_faq.html
8814. Why does "make test" fail on Mac OS X? 9314. Why does "make test" fail on Mac OS X?
89 94
90 Mac OS X already includes zlib as a shared library, and so -lz links the 95 Mac OS X already includes zlib as a shared library, and so -lz links the
91 shared library instead of the one that the "make" compiled. For zlib 96 shared library instead of the one that the "make" compiled. The two are
92 1.1.3, the two are incompatible due to different compile-time 97 incompatible due to different compile-time options. Simply change the -lz
93 options. Simply change the -lz in the Makefile to libz.a, and it will use 98 in the Makefile to libz.a, and it will use the compiled library instead
94 the compiled library instead of the shared one and the "make test" will 99 of the shared one and the "make test" will succeed.
95 succeed.
96 100
9715. I have a question about OttoPDF 10115. I have a question about OttoPDF
98 102
99 We are not the authors of OttoPDF. The real author is on the OttoPDF web 103 We are not the authors of OttoPDF. The real author is on the OttoPDF web
100 site Joel Hainley jhainley@myndkryme.com. 104 site Joel Hainley jhainley@myndkryme.com.
105
10616. Why does gzip give an error on a file I make with compress/deflate?
107
108 The compress and deflate functions produce data in the zlib format, which
109 is different and incompatible with the gzip format. The gz* functions in
110 zlib on the other hand use the gzip format. Both the zlib and gzip
111 formats use the same compressed data format, but have different headers
112 and trailers.
113
11417. Ok, so why are there two different formats?
115
116 The gzip format was designed to retain the directory information about
117 a single file, such as the name and last modification date. The zlib
118 format on the other hand was designed for in-memory and communication
119 channel applications, and has a much more compact header and trailer and
120 uses a faster integrity check than gzip.
121
12218. Well that's nice, but how do I make a gzip file in memory?
123
124 Read RFC 1952 for the gzip header and trailer format, and roll your own
125 gzip formatted data using raw deflate and crc32().
126
12719. Is zlib thread-safe?
128
129 Yes. However any library routines that zlib uses and any application-
130 provided memory allocation routines must also be thread-safe. Of course,
131 you should only operate on any given zlib or gzip stream from a single
132 thread. zlib's gz* functions use stdio library routines, and most of
133 zlib's functions use the library memory allocation routines by default.
134 zlib's Init functions allow for the application to provide custom memory
135 allocation routines.
136
13720. Can I use zlib in my commercial application?
138
139 Yes. Please read the license in zlib.h.
140
14121. Is zlib under the GNU license?
142
143 No. Please read the license in zlib.h.
144
14522. Will zlib work on a big-endian or little-endian architecture, and can I
146 exchange compressed data between them?
147
148 Yes and yes.
149
15023. Will zlib work on a 64-bit machine?
151
152 It should. It has been tested on 64-bit machines, and has no dependence
153 on any data types being limited to 32-bits in length. If you have any
154 difficulties, please provide a complete problem report to zlib@gzip.org
155
15624. Will zlib decompress data from the PKWare Data Compression Library?
157
158 No. The PKWare DCL uses a completely different compressed data format
159 than does PKZIP and zlib. However, you can look in zlib's contrib/blast
160 directory for a possible solution to your problem.
161
16225. Can I access data randomly in a compressed stream?
163
164 No, not without some preparation. If when compressing you periodically
165 use Z_FULL_FLUSH, carefully write all the pending data at those points,
166 and keep an index of those locations, then you can start decompression
167 at those points. You have to be careful to not use Z_FULL_FLUSH too
168 often, since it can significantly degrade compression.
169
17026. Does zlib work on MVS, OS/390, CICS, etc.?
171
172 We don't know for sure. We have heard occasional reports of success on
173 these systems. If you do use it on one of these, please provide us with
174 a report, instructions, and patches that we can reference when we get
175 these questions. Thanks.
176
17727. Is there some simpler, easier to read version of inflate I can look at
178 to understand the deflate format?
179
180 First off, you should read RFC 1951. Second, yes. Look in zlib's
181 contrib/puff directory.
182
18328. Does zlib infringe on any patents?
184
185 As far as we know, no. In fact, that was originally the whole point behind
186 zlib. Look here for some more information:
187
188 http://www.gzip.org/#faq11
189
19029. Can zlib work with greater than 4 GB of data?
191
192 Yes. inflate() and deflate() will process any amount of data correctly.
193 However the strm.total_in and strm_total_out counters may be limited to
194 4 GB. The user can easily set up their own counters updated after each
195 call of inflate() or deflate() to count beyond 4 GB. compress() and
196 uncompress() may be limited to 4 GB, since they operate in a single
197 call using unsigned long lengths. gzseek() may be limited to 4 GB
198 depending on how zlib is compiled.
199
20030. Does zlib have any security vulnerabilities?
201
202 The only one that we are aware of is potentially in gzprintf(). If zlib
203 is compiled to use sprintf() or vsprintf(), then there is no protection
204 against a buffer overflow of a 4K string space, other than the caller of
205 gzprintf() assuring that the output will not exceed 4K. On the other
206 hand, if zlib is compiled to use snprintf() or vsnprintf(), then there is
207 no vulnerability.
208
209 Note that you should be using the most recent version of zlib. Versions
210 1.1.3 and before were subject to a double-free vulnerability.
211
21231. Is there a Java version of zlib?
213
214 Probably what you want is to use zlib in Java. zlib is already included
215 as part of the Java SDK in the java.util.zip class. If you really want
216 a version of zlib written in the Java language, look on the zlib home
217 page for links: http://www.zlib.org/
218
21932. I get this or that compiler or source-code scanner warning. Can't you guys
220 write proper code?
221
222 Many years ago, we gave up attempting to avoid warnings on every compiler
223 in the universe. It just got to be a waste of time, and some compilers
224 were downright silly. So now, we simply make sure that the code always
225 works.
226
22733. Will zlib read the (insert any ancient or arcane format here) compressed
228 data format?
229
230 Probably not. Look in the comp.compression FAQ for pointers to various
231 formats and associated software.
232
23334. How can I encrypt/decrypt zip files with zlib?
234
235 zlib doesn't support encryption. PKZIP encryption is very weak and can be
236 broken with freely available programs. To get strong encryption, use gpg
237 which already includes zlib compression.
238
23935. What's the difference between the "gzip" and "deflate" HTTP 1.1 encodings?
240
241 "gzip" is the gzip format, and "deflate" is the zlib format. They should
242 probably have called the second one "zlib" instead to avoid confusion
243 with the raw deflate compressed data format. While the HTTP 1.1 RFC 2616
244 correctly points to the zlib specification in RFC 1950 for the "deflate"
245 transfer encoding, there have been reports of servers and browsers that
246 incorrectly produce or expect raw deflate data per the deflate
247 specficiation in RFC 1951, most notably Microsoft. So even though the
248 "deflate" transfer encoding using the zlib format would be the more
249 efficient approach (and in fact exactly what the zlib format was designed
250 for), using the "gzip" transfer encoding is probably more reliable due to
251 an unfortunate choice of name on the part of the HTTP 1.1 authors.
252
25336. Does zlib support the new "Deflate64" format introduced by PKWare?
254
255 No. PKWare has apparently decided to keep that format proprietary, since
256 they have not documented it as they have previous compression formats.
257 In any case, the compression improvements are so modest compared to other
258 more modern approaches, that it's not worth the effort to implement.
259
26037. Can you please sign these lengthy legal documents and fax them back to us
261 so that we can use your software in our product?
262
263 No.
diff --git a/INDEX b/INDEX
index 8a24576..87df959 100644
--- a/INDEX
+++ b/INDEX
@@ -1,36 +1,16 @@
1ChangeLog history of changes 1ChangeLog history of changes
2INDEX this file
3FAQ Frequently Asked Questions about zlib 2FAQ Frequently Asked Questions about zlib
4Make_vms.com script for Vax/VMS 3INDEX this file
5Makefile makefile for Unix (generated by configure) 4Makefile makefile for Unix (generated by configure)
6Makefile.in makefile for Unix (template for configure) 5Makefile.in makefile for Unix (template for configure)
7Makefile.riscos makefile for RISCOS
8README guess what 6README guess what
9algorithm.txt description of the (de)compression algorithm 7algorithm.txt description of the (de)compression algorithm
10configure configure script for Unix 8configure configure script for Unix
11descrip.mms makefile for Vax/VMS 9zconf.in.h template for zconf.h (used by configure)
12zlib.3 mini man page for zlib (volunteers to write full
13 man pages from zlib.h welcome. write to jloup@gzip.org)
14
15amiga/Makefile.sas makefile for Amiga SAS/C
16amiga/Makefile.pup makefile for Amiga powerUP SAS/C PPC
17
18msdos/Makefile.w32 makefile for Microsoft Visual C++ 32-bit
19msdos/Makefile.b32 makefile for Borland C++ 32-bit
20msdos/Makefile.bor makefile for Borland C/C++ 16-bit
21msdos/Makefile.dj2 makefile for DJGPP 2.x
22msdos/Makefile.emx makefile for EMX 0.9c (32-bit DOS/OS2)
23msdos/Makefile.msc makefile for Microsoft C 16-bit
24msdos/Makefile.tc makefile for Turbo C
25msdos/Makefile.wat makefile for Watcom C
26msdos/zlib.def definition file for Windows DLL
27msdos/zlib.rc definition file for Windows DLL
28
29nt/Makefile.nt makefile for Windows NT
30nt/zlib.dnt definition file for Windows NT DLL
31nt/Makefile.emx makefile for EMX 0.9c/RSXNT 1.41 (Win32 Intel)
32nt/Makefile.gcc makefile for Windows NT using GCC (mingw32)
33 10
11aix/ instructions for building an AIX shared library
12old/ makefiles for various architectures and zlib documentation
13 that has not yet been updated for zlib 1.2.x
34 14
35 zlib public header files (must be kept): 15 zlib public header files (must be kept):
36zconf.h 16zconf.h
@@ -40,22 +20,20 @@ zlib.h
40adler32.c 20adler32.c
41compress.c 21compress.c
42crc32.c 22crc32.c
23crc32.h
43deflate.c 24deflate.c
44deflate.h 25deflate.h
45gzio.c 26gzio.c
46infblock.c 27infback.c
47infblock.h
48infcodes.c
49infcodes.h
50inffast.c 28inffast.c
51inffast.h 29inffast.h
30inffixed.h
52inflate.c 31inflate.c
32inflate.h
53inftrees.c 33inftrees.c
54inftrees.h 34inftrees.h
55infutil.c
56infutil.h
57maketree.c
58trees.c 35trees.c
36trees.h
59uncompr.c 37uncompr.c
60zutil.c 38zutil.c
61zutil.h 39zutil.h
@@ -65,22 +43,4 @@ example.c
65minigzip.c 43minigzip.c
66 44
67 unsupported contribution by third parties 45 unsupported contribution by third parties
68 46See contrib/README.contrib
69contrib/asm386/ by Gilles Vollant <info@winimage.com>
70 386 asm code replacing longest_match().
71
72contrib/minizip/ by Gilles Vollant <info@winimage.com>
73 Mini zip and unzip based on zlib
74 See http://www.winimage.com/zLibDll/unzip.html
75
76contrib/iostream/ by Kevin Ruland <kevin@rodin.wustl.edu>
77 A C++ I/O streams interface to the zlib gz* functions
78
79contrib/iostream2/ by Tyge Løvset <Tyge.Lovset@cmr.no>
80 Another C++ I/O streams interface
81
82contrib/untgz/ by "Pedro A. Aranda Guti\irrez" <paag@tid.es>
83 A very simple tar.gz extractor using zlib
84
85contrib/visual-basic.txt by Carlos Rios <c_rios@sonda.cl>
86 How to use compress(), uncompress() and the gz* functions from VB.
diff --git a/Makefile b/Makefile
index 531562b..087e388 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
1# Makefile for zlib 1# Makefile for zlib
2# Copyright (C) 1995-2002 Jean-loup Gailly. 2# Copyright (C) 1995-2003 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: 5# To compile and test, type:
@@ -20,11 +20,11 @@ CFLAGS=-O
20#CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \ 20#CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \
21# -Wstrict-prototypes -Wmissing-prototypes 21# -Wstrict-prototypes -Wmissing-prototypes
22 22
23LDFLAGS=-L. -lz 23LDFLAGS=libz.a
24LDSHARED=$(CC) 24LDSHARED=$(CC)
25CPP=$(CC) -E 25CPP=$(CC) -E
26 26
27VER=1.1.4 27VER=1.2.0
28LIBS=libz.a 28LIBS=libz.a
29SHAREDLIB=libz.so 29SHAREDLIB=libz.so
30 30
@@ -39,13 +39,14 @@ libdir = ${exec_prefix}/lib
39includedir = ${prefix}/include 39includedir = ${prefix}/include
40 40
41OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \ 41OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \
42 zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o inffast.o 42 zutil.o inflate.o infback.o inftrees.o inffast.o
43 43
44OBJA = 44OBJA =
45# to use the asm code: make OBJA=match.o 45# to use the asm code: make OBJA=match.o
46 46
47TEST_OBJS = example.o minigzip.o 47TEST_OBJS = example.o minigzip.o
48 48
49# Note: this hasn't been updated for zlib 1.2.0
49DISTFILES = README FAQ INDEX ChangeLog configure Make*[a-z0-9] *.[ch] *.mms \ 50DISTFILES = README FAQ INDEX ChangeLog configure Make*[a-z0-9] *.[ch] *.mms \
50 algorithm.txt zlib.3 zlib.html \ 51 algorithm.txt zlib.3 zlib.html \
51 msdos/Make*[a-z0-9] msdos/zlib.def msdos/zlib.rc \ 52 msdos/Make*[a-z0-9] msdos/zlib.def msdos/zlib.rc \
@@ -93,6 +94,7 @@ minigzip: minigzip.o $(LIBS)
93 $(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS) 94 $(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS)
94 95
95install: $(LIBS) 96install: $(LIBS)
97 -@if [ ! -d $(exec_prefix) ]; then mkdir $(exec_prefix); fi
96 -@if [ ! -d $(includedir) ]; then mkdir $(includedir); fi 98 -@if [ ! -d $(includedir) ]; then mkdir $(includedir); fi
97 -@if [ ! -d $(libdir) ]; then mkdir $(libdir); fi 99 -@if [ ! -d $(libdir) ]; then mkdir $(libdir); fi
98 cp zlib.h zconf.h $(includedir) 100 cp zlib.h zconf.h $(includedir)
@@ -126,6 +128,8 @@ clean:
126 _match.s maketree 128 _match.s maketree
127 129
128distclean: clean 130distclean: clean
131 cp -p Makefile.in Makefile
132 cp -p zconf.in.h zconf.h
129 133
130zip: 134zip:
131 mv Makefile Makefile~; cp -p Makefile.in Makefile 135 mv Makefile Makefile~; cp -p Makefile.in Makefile
@@ -135,6 +139,7 @@ zip:
135 mv Makefile~ Makefile 139 mv Makefile~ Makefile
136 140
137dist: 141dist:
142 echo Warning: this hasn't been updated for zlib 1.2.0 -- don't use
138 mv Makefile Makefile~; cp -p Makefile.in Makefile 143 mv Makefile Makefile~; cp -p Makefile.in Makefile
139 rm -f test.c ztest*.c contrib/minizip/test.zip 144 rm -f test.c ztest*.c contrib/minizip/test.zip
140 d=zlib-`sed -n '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`;\ 145 d=zlib-`sed -n '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`;\
@@ -157,18 +162,14 @@ depend:
157 162
158adler32.o: zlib.h zconf.h 163adler32.o: zlib.h zconf.h
159compress.o: zlib.h zconf.h 164compress.o: zlib.h zconf.h
160crc32.o: zlib.h zconf.h 165crc32.o: crc32.h zlib.h zconf.h
161deflate.o: deflate.h zutil.h zlib.h zconf.h 166deflate.o: deflate.h zutil.h zlib.h zconf.h
162example.o: zlib.h zconf.h 167example.o: zlib.h zconf.h
163gzio.o: zutil.h zlib.h zconf.h 168gzio.o: zutil.h zlib.h zconf.h
164infblock.o: infblock.h inftrees.h infcodes.h infutil.h zutil.h zlib.h zconf.h 169inffast.o: zutil.h zlib.h zconf.h inftrees.h inflate.h inffast.h
165infcodes.o: zutil.h zlib.h zconf.h 170inflate.o: zutil.h zlib.h zconf.h inftrees.h inflate.h inffast.h
166infcodes.o: inftrees.h infblock.h infcodes.h infutil.h inffast.h 171infback.o: zutil.h zlib.h zconf.h inftrees.h inflate.h inffast.h
167inffast.o: zutil.h zlib.h zconf.h inftrees.h
168inffast.o: infblock.h infcodes.h infutil.h inffast.h
169inflate.o: zutil.h zlib.h zconf.h infblock.h
170inftrees.o: zutil.h zlib.h zconf.h inftrees.h 172inftrees.o: zutil.h zlib.h zconf.h inftrees.h
171infutil.o: zutil.h zlib.h zconf.h infblock.h inftrees.h infcodes.h infutil.h
172minigzip.o: zlib.h zconf.h 173minigzip.o: zlib.h zconf.h
173trees.o: deflate.h zutil.h zlib.h zconf.h trees.h 174trees.o: deflate.h zutil.h zlib.h zconf.h trees.h
174uncompr.o: zlib.h zconf.h 175uncompr.o: zlib.h zconf.h
diff --git a/Makefile.in b/Makefile.in
index 531562b..087e388 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -1,5 +1,5 @@
1# Makefile for zlib 1# Makefile for zlib
2# Copyright (C) 1995-2002 Jean-loup Gailly. 2# Copyright (C) 1995-2003 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: 5# To compile and test, type:
@@ -20,11 +20,11 @@ CFLAGS=-O
20#CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \ 20#CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \
21# -Wstrict-prototypes -Wmissing-prototypes 21# -Wstrict-prototypes -Wmissing-prototypes
22 22
23LDFLAGS=-L. -lz 23LDFLAGS=libz.a
24LDSHARED=$(CC) 24LDSHARED=$(CC)
25CPP=$(CC) -E 25CPP=$(CC) -E
26 26
27VER=1.1.4 27VER=1.2.0
28LIBS=libz.a 28LIBS=libz.a
29SHAREDLIB=libz.so 29SHAREDLIB=libz.so
30 30
@@ -39,13 +39,14 @@ libdir = ${exec_prefix}/lib
39includedir = ${prefix}/include 39includedir = ${prefix}/include
40 40
41OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \ 41OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \
42 zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o inffast.o 42 zutil.o inflate.o infback.o inftrees.o inffast.o
43 43
44OBJA = 44OBJA =
45# to use the asm code: make OBJA=match.o 45# to use the asm code: make OBJA=match.o
46 46
47TEST_OBJS = example.o minigzip.o 47TEST_OBJS = example.o minigzip.o
48 48
49# Note: this hasn't been updated for zlib 1.2.0
49DISTFILES = README FAQ INDEX ChangeLog configure Make*[a-z0-9] *.[ch] *.mms \ 50DISTFILES = README FAQ INDEX ChangeLog configure Make*[a-z0-9] *.[ch] *.mms \
50 algorithm.txt zlib.3 zlib.html \ 51 algorithm.txt zlib.3 zlib.html \
51 msdos/Make*[a-z0-9] msdos/zlib.def msdos/zlib.rc \ 52 msdos/Make*[a-z0-9] msdos/zlib.def msdos/zlib.rc \
@@ -93,6 +94,7 @@ minigzip: minigzip.o $(LIBS)
93 $(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS) 94 $(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS)
94 95
95install: $(LIBS) 96install: $(LIBS)
97 -@if [ ! -d $(exec_prefix) ]; then mkdir $(exec_prefix); fi
96 -@if [ ! -d $(includedir) ]; then mkdir $(includedir); fi 98 -@if [ ! -d $(includedir) ]; then mkdir $(includedir); fi
97 -@if [ ! -d $(libdir) ]; then mkdir $(libdir); fi 99 -@if [ ! -d $(libdir) ]; then mkdir $(libdir); fi
98 cp zlib.h zconf.h $(includedir) 100 cp zlib.h zconf.h $(includedir)
@@ -126,6 +128,8 @@ clean:
126 _match.s maketree 128 _match.s maketree
127 129
128distclean: clean 130distclean: clean
131 cp -p Makefile.in Makefile
132 cp -p zconf.in.h zconf.h
129 133
130zip: 134zip:
131 mv Makefile Makefile~; cp -p Makefile.in Makefile 135 mv Makefile Makefile~; cp -p Makefile.in Makefile
@@ -135,6 +139,7 @@ zip:
135 mv Makefile~ Makefile 139 mv Makefile~ Makefile
136 140
137dist: 141dist:
142 echo Warning: this hasn't been updated for zlib 1.2.0 -- don't use
138 mv Makefile Makefile~; cp -p Makefile.in Makefile 143 mv Makefile Makefile~; cp -p Makefile.in Makefile
139 rm -f test.c ztest*.c contrib/minizip/test.zip 144 rm -f test.c ztest*.c contrib/minizip/test.zip
140 d=zlib-`sed -n '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`;\ 145 d=zlib-`sed -n '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`;\
@@ -157,18 +162,14 @@ depend:
157 162
158adler32.o: zlib.h zconf.h 163adler32.o: zlib.h zconf.h
159compress.o: zlib.h zconf.h 164compress.o: zlib.h zconf.h
160crc32.o: zlib.h zconf.h 165crc32.o: crc32.h zlib.h zconf.h
161deflate.o: deflate.h zutil.h zlib.h zconf.h 166deflate.o: deflate.h zutil.h zlib.h zconf.h
162example.o: zlib.h zconf.h 167example.o: zlib.h zconf.h
163gzio.o: zutil.h zlib.h zconf.h 168gzio.o: zutil.h zlib.h zconf.h
164infblock.o: infblock.h inftrees.h infcodes.h infutil.h zutil.h zlib.h zconf.h 169inffast.o: zutil.h zlib.h zconf.h inftrees.h inflate.h inffast.h
165infcodes.o: zutil.h zlib.h zconf.h 170inflate.o: zutil.h zlib.h zconf.h inftrees.h inflate.h inffast.h
166infcodes.o: inftrees.h infblock.h infcodes.h infutil.h inffast.h 171infback.o: zutil.h zlib.h zconf.h inftrees.h inflate.h inffast.h
167inffast.o: zutil.h zlib.h zconf.h inftrees.h
168inffast.o: infblock.h infcodes.h infutil.h inffast.h
169inflate.o: zutil.h zlib.h zconf.h infblock.h
170inftrees.o: zutil.h zlib.h zconf.h inftrees.h 172inftrees.o: zutil.h zlib.h zconf.h inftrees.h
171infutil.o: zutil.h zlib.h zconf.h infblock.h inftrees.h infcodes.h infutil.h
172minigzip.o: zlib.h zconf.h 173minigzip.o: zlib.h zconf.h
173trees.o: deflate.h zutil.h zlib.h zconf.h trees.h 174trees.o: deflate.h zutil.h zlib.h zconf.h trees.h
174uncompr.o: zlib.h zconf.h 175uncompr.o: zlib.h zconf.h
diff --git a/README b/README
index 29d6714..26ab2b6 100644
--- a/README
+++ b/README
@@ -1,4 +1,4 @@
1zlib 1.1.4 is a general purpose data compression library. All the code 1zlib 1.2.0 is a general purpose data compression library. All the code
2is thread safe. The data format used by the zlib library 2is thread safe. The data format used by the zlib library
3is described by RFCs (Request for Comments) 1950 to 1952 in the files 3is described by RFCs (Request for Comments) 1950 to 1952 in the files
4http://www.ietf.org/rfc/rfc1950.txt (zlib format), rfc1951.txt (deflate 4http://www.ietf.org/rfc/rfc1950.txt (zlib format), rfc1951.txt (deflate
@@ -32,22 +32,7 @@ Mark Nelson <markn@ieee.org> wrote an article about zlib for the Jan. 1997
32issue of Dr. Dobb's Journal; a copy of the article is available in 32issue of Dr. Dobb's Journal; a copy of the article is available in
33http://dogma.net/markn/articles/zlibtool/zlibtool.htm 33http://dogma.net/markn/articles/zlibtool/zlibtool.htm
34 34
35The changes made in version 1.1.4 are documented in the file ChangeLog. 35The changes made in version 1.2.0 are documented in the file ChangeLog.
36The only changes made since 1.1.3 are bug corrections:
37
38- ZFREE was repeated on same allocation on some error conditions.
39 This creates a security problem described in
40 http://www.zlib.org/advisory-2002-03-11.txt
41- Returned incorrect error (Z_MEM_ERROR) on some invalid data
42- Avoid accesses before window for invalid distances with inflate window
43 less than 32K.
44- force windowBits > 8 to avoid a bug in the encoder for a window size
45 of 256 bytes. (A complete fix will be available in 1.1.5).
46
47The beta version 1.1.5beta includes many more changes. A new official
48version 1.1.5 will be released as soon as extensive testing has been
49completed on it.
50
51 36
52Unsupported third party contributions are provided in directory "contrib". 37Unsupported third party contributions are provided in directory "contrib".
53 38
@@ -55,7 +40,7 @@ A Java implementation of zlib is available in the Java Development Kit
55http://www.javasoft.com/products/JDK/1.1/docs/api/Package-java.util.zip.html 40http://www.javasoft.com/products/JDK/1.1/docs/api/Package-java.util.zip.html
56See the zlib home page http://www.zlib.org for details. 41See the zlib home page http://www.zlib.org for details.
57 42
58A Perl interface to zlib written by Paul Marquess <pmarquess@bfsec.bt.co.uk> 43A Perl interface to zlib written by Paul Marquess <pmqs@cpan.org>
59is in the CPAN (Comprehensive Perl Archive Network) sites 44is in the CPAN (Comprehensive Perl Archive Network) sites
60http://www.cpan.org/modules/by-module/Compress/ 45http://www.cpan.org/modules/by-module/Compress/
61 46
@@ -116,7 +101,7 @@ Acknowledgments:
116 101
117Copyright notice: 102Copyright notice:
118 103
119 (C) 1995-2002 Jean-loup Gailly and Mark Adler 104 (C) 1995-2003 Jean-loup Gailly and Mark Adler
120 105
121 This software is provided 'as-is', without any express or implied 106 This software is provided 'as-is', without any express or implied
122 warranty. In no event will the authors be held liable for any damages 107 warranty. In no event will the authors be held liable for any damages
diff --git a/adler32.c b/adler32.c
index fae88b6..319603c 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-2002 Mark Adler 2 * Copyright (C) 1995-2003 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,7 +30,7 @@ uLong ZEXPORT adler32(adler, buf, len)
30 if (buf == Z_NULL) return 1L; 30 if (buf == Z_NULL) return 1L;
31 31
32 while (len > 0) { 32 while (len > 0) {
33 k = len < NMAX ? len : NMAX; 33 k = len < NMAX ? (int)len : NMAX;
34 len -= k; 34 len -= k;
35 while (k >= 16) { 35 while (k >= 16) {
36 DO16(buf); 36 DO16(buf);
diff --git a/aix/README b/aix/README
new file mode 100644
index 0000000..235ba59
--- /dev/null
+++ b/aix/README
@@ -0,0 +1,9 @@
1To make a shared library:
2
31. Compile a static library
42. Use mkexps on that to create libz.exp
53. Apply the configure.diff patch to configure
64. Run the new configure to make a new Makefile
75. Use the new Makefile to make the shared library
8
9Courtesy of dbakker@arrayasolutions.com
diff --git a/aix/configure.diff b/aix/configure.diff
new file mode 100644
index 0000000..c699129
--- /dev/null
+++ b/aix/configure.diff
@@ -0,0 +1,57 @@
1*** ../orig/zlib-1.1.4/configure Wed Jul 8 14:19:35 1998
2--- configure Sun Feb 9 11:11:19 2003
3***************
4*** 18,23 ****
5--- 18,24 ----
6 # If you have problems, try without defining CC and CFLAGS before reporting
7 # an error.
8
9+ LDFLAGS="-L. -lz"
10 LIBS=libz.a
11 SHAREDLIB=libz.so
12 VER=`sed -n -e '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`
13***************
14*** 116,121 ****
15--- 117,128 ----
16 SFLAGS=${CFLAGS-"-Kconform_pic -O"}
17 CFLAGS=${CFLAGS-"-O"}
18 LDSHARED=${LDSHARED-"cc -G"};;
19+ AIX*)
20+ SFLAGS=${CFLAGS-"-O -qmaxmem=8192"}
21+ CFLAGS=${CFLAGS-"-O -qmaxmem=8192"}
22+ LDTESTSHARED=${LDSHARED-"cc -G"}
23+ LDSHAREDFLAGS="-L. libz.so"
24+ LDSHARED=${LDSHARED-"cc -G"};;
25 # send working options for other systems to support@gzip.org
26 *) SFLAGS=${CFLAGS-"-O"}
27 CFLAGS=${CFLAGS-"-O"}
28***************
29*** 127,135 ****
30 echo Checking for shared library support...
31 # we must test in two steps (cc then ld), required at least on SunOS 4.x
32 if test "`($CC -c $SFLAGS $test.c) 2>&1`" = "" &&
33! test "`($LDSHARED -o $test$shared_ext $test.o) 2>&1`" = ""; then
34 CFLAGS="$SFLAGS"
35 LIBS="$SHAREDLIB.$VER"
36 echo Building shared library $SHAREDLIB.$VER with $CC.
37 elif test -z "$old_cc" -a -z "$old_cflags"; then
38 echo No shared library suppport.
39--- 134,143 ----
40 echo Checking for shared library support...
41 # we must test in two steps (cc then ld), required at least on SunOS 4.x
42 if test "`($CC -c $SFLAGS $test.c) 2>&1`" = "" &&
43! test "`($LDTESTSHARED -o $test$shared_ext $test.o) 2>&1`" = ""; then
44 CFLAGS="$SFLAGS"
45 LIBS="$SHAREDLIB.$VER"
46+ LDFLAGS="$LDSHAREDFLAGS"
47 echo Building shared library $SHAREDLIB.$VER with $CC.
48 elif test -z "$old_cc" -a -z "$old_cflags"; then
49 echo No shared library suppport.
50***************
51*** 209,212 ****
52--- 217,221 ----
53 /^exec_prefix *=/s%=.*%=$exec_prefix%
54 /^libdir *=/s%=.*%=$libdir%
55 /^includedir *=/s%=.*%=$includedir%
56+ /^LDFLAGS *=/s%=.*%=$LDFLAGS%
57 " > Makefile
diff --git a/aix/mkexps b/aix/mkexps
new file mode 100644
index 0000000..6c55eae
--- /dev/null
+++ b/aix/mkexps
@@ -0,0 +1,37 @@
1#!/bin/ksh
2#
3# mkexps - make export list
4# This program creates an export list by combining all the "." and normal names
5# into one list.
6#
7if [[ "$#" -ne 1 ]]
8then
9 print "Usage: mkexps ArchiveFile"
10 exit -2
11fi
12if [[ ! -f $1 ]]
13then
14 print "mkexps: Cannot open file \"$1\""
15 exit -1
16fi
17
18dump -g $1 | awk '
19BEGIN {
20 top = 1
21}
22/^[ ]*[0-9][0-9]*/ {
23 if ( (n = index( $2, "." )) > 0 ) {
24 export_array[ top++ ] = substr( $2, n+1, length( $2 ))
25 }
26 else {
27 export_array[ top++ ] = $2
28 }
29}
30
31END {
32 for ( i = 1; i < top; i++ )
33 {
34 print export_array[ i ]
35 }
36
37}' | sort | uniq
diff --git a/algorithm.txt b/algorithm.txt
index cdc830b..f64f7c3 100644
--- a/algorithm.txt
+++ b/algorithm.txt
@@ -59,10 +59,10 @@ but saves time since there are both fewer insertions and fewer searches.
59 59
602.1 Introduction 602.1 Introduction
61 61
62The real question is, given a Huffman tree, how to decode fast. The most 62The key question is how to represent a Huffman code (or any prefix code) so
63important realization is that shorter codes are much more common than 63that you can decode fast. The most important characteristic is that shorter
64longer codes, so pay attention to decoding the short codes fast, and let 64codes are much more common than longer codes, so pay attention to decoding the
65the long codes take longer to decode. 65short codes fast, and let the long codes take longer to decode.
66 66
67inflate() sets up a first level table that covers some number of bits of 67inflate() sets up a first level table that covers some number of bits of
68input less than the length of longest code. It gets that many bits from the 68input less than the length of longest code. It gets that many bits from the
@@ -77,20 +77,16 @@ table took no time (and if you had infinite memory), then there would only
77be a first level table to cover all the way to the longest code. However, 77be a first level table to cover all the way to the longest code. However,
78building the table ends up taking a lot longer for more bits since short 78building the table ends up taking a lot longer for more bits since short
79codes are replicated many times in such a table. What inflate() does is 79codes are replicated many times in such a table. What inflate() does is
80simply to make the number of bits in the first table a variable, and set it 80simply to make the number of bits in the first table a variable, and then
81for the maximum speed. 81to set that variable for the maximum speed.
82 82
83inflate() sends new trees relatively often, so it is possibly set for a 83For inflate, which has 286 possible codes for the literal/length tree, the size
84smaller first level table than an application that has only one tree for 84of the first table is nine bits. Also the distance trees have 30 possible
85all the data. For inflate, which has 286 possible codes for the 85values, and the size of the first table is six bits. Note that for each of
86literal/length tree, the size of the first table is nine bits. Also the 86those cases, the table ended up one bit longer than the ``average'' code
87distance trees have 30 possible values, and the size of the first table is 87length, i.e. the code length of an approximately flat code which would be a
88six bits. Note that for each of those cases, the table ended up one bit 88little more than eight bits for 286 symbols and a little less than five bits
89longer than the ``average'' code length, i.e. the code length of an 89for 30 symbols.
90approximately flat code which would be a little more than eight bits for
91286 symbols and a little less than five bits for 30 symbols. It would be
92interesting to see if optimizing the first level table for other
93applications gave values within a bit or two of the flat code size.
94 90
95 91
962.2 More details on the inflate table lookup 922.2 More details on the inflate table lookup
@@ -158,7 +154,7 @@ Let's make the first table three bits long (eight entries):
158110: -> table X (gobble 3 bits) 154110: -> table X (gobble 3 bits)
159111: -> table Y (gobble 3 bits) 155111: -> table Y (gobble 3 bits)
160 156
161Each entry is what the bits decode to and how many bits that is, i.e. how 157Each entry is what the bits decode as and how many bits that is, i.e. how
162many bits to gobble. Or the entry points to another table, with the number of 158many bits to gobble. Or the entry points to another table, with the number of
163bits to gobble implicit in the size of the table. 159bits to gobble implicit in the size of the table.
164 160
diff --git a/compress.c b/compress.c
index 814bd9d..f10e234 100644
--- a/compress.c
+++ b/compress.c
@@ -66,3 +66,13 @@ int ZEXPORT compress (dest, destLen, source, sourceLen)
66{ 66{
67 return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); 67 return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
68} 68}
69
70/* ===========================================================================
71 If the default memLevel or windowBits for deflateInit() is changed, then
72 this function needs to be updated.
73 */
74uLong ZEXPORT compressBound (sourceLen)
75 uLong sourceLen;
76{
77 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
78}
diff --git a/configure b/configure
index e894235..51ef10f 100755
--- a/configure
+++ b/configure
@@ -72,8 +72,11 @@ if test "$gcc" -eq 1 && ($cc -c $cflags $test.c) 2>/dev/null; then
72 SFLAGS=${CFLAGS-"-fPIC -O3"} 72 SFLAGS=${CFLAGS-"-fPIC -O3"}
73 CFLAGS="$cflags" 73 CFLAGS="$cflags"
74 case `(uname -s || echo unknown) 2>/dev/null` in 74 case `(uname -s || echo unknown) 2>/dev/null` in
75 Linux | linux) LDSHARED=${LDSHARED-"gcc -shared -Wl,-soname,libz.so.1"};; 75 Linux | linux) LDSHARED=${LDSHARED-"$cc -shared -Wl,-soname,libz.so.1"};;
76 *) LDSHARED=${LDSHARED-"gcc -shared"};; 76 HP-UX*) LDSHARED=${LDSHARED-"$cc -shared $SFLAGS"}
77 shared_ext='.sl'
78 SHAREDLIB='libz.sl';;
79 *) LDSHARED=${LDSHARED-"$cc -shared"};;
77 esac 80 esac
78else 81else
79 # find system name and corresponding cc options 82 # find system name and corresponding cc options
@@ -116,6 +119,10 @@ else
116 SFLAGS=${CFLAGS-"-Kconform_pic -O"} 119 SFLAGS=${CFLAGS-"-Kconform_pic -O"}
117 CFLAGS=${CFLAGS-"-O"} 120 CFLAGS=${CFLAGS-"-O"}
118 LDSHARED=${LDSHARED-"cc -G"};; 121 LDSHARED=${LDSHARED-"cc -G"};;
122 OpenUNIX\ 5)
123 SFLAGS=${CFLAGS-"-KPIC -O"}
124 CFLAGS=${CFLAGS-"-O"}
125 LDSHARED=${LDSHARED-"cc -G"};;
119 # send working options for other systems to support@gzip.org 126 # send working options for other systems to support@gzip.org
120 *) SFLAGS=${CFLAGS-"-O"} 127 *) SFLAGS=${CFLAGS-"-O"}
121 CFLAGS=${CFLAGS-"-O"} 128 CFLAGS=${CFLAGS-"-O"}
@@ -149,13 +156,219 @@ cat > $test.c <<EOF
149int main() { return 0; } 156int main() { return 0; }
150EOF 157EOF
151if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then 158if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
152 CFLAGS="$CFLAGS -DHAVE_UNISTD_H" 159 sed < zconf.in.h "/HAVE_UNISTD_H/s%0%1%" > zconf.h
153 echo "Checking for unistd.h... Yes." 160 echo "Checking for unistd.h... Yes."
154else 161else
162 cp -p zconf.in.h zconf.h
155 echo "Checking for unistd.h... No." 163 echo "Checking for unistd.h... No."
156fi 164fi
157 165
158cat > $test.c <<EOF 166cat > $test.c <<EOF
167#include <stdio.h>
168#include <stdlib.h>
169
170#if (defined(__MSDOS__) || defined(_WINDOWS) || defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__STDC__) || defined(__cplusplus) || defined(__OS2__)) && !defined(STDC)
171# define STDC
172#endif
173
174int main()
175{
176#ifndef STDC
177 choke me
178#endif
179
180 return 0;
181}
182EOF
183
184if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
185 echo "Checking whether to use vsnprintf() or snprintf()... using vsnprintf()"
186
187 cat > $test.c <<EOF
188#include <stdio.h>
189#include <stdarg.h>
190
191int mytest(char *fmt, ...)
192{
193 char buf[20];
194 va_list ap;
195
196 va_start(ap, fmt);
197 vsnprintf(buf, sizeof(buf), fmt, ap);
198 va_end(ap);
199 return 0;
200}
201
202int main()
203{
204 return (mytest("Hello%d\n", 1));
205}
206EOF
207
208 if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
209 echo "Checking for vsnprintf() in stdio.h... Yes."
210
211 cat >$test.c <<EOF
212#include <stdio.h>
213#include <stdarg.h>
214
215int mytest(char *fmt, ...)
216{
217 int i;
218 char buf[20];
219 va_list ap;
220
221 va_start(ap, fmt);
222 i = vsnprintf(buf, sizeof(buf), fmt, ap);
223 va_end(ap);
224 return 0;
225}
226
227int main()
228{
229 return (mytest("Hello%d\n", 1));
230}
231EOF
232
233 if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
234 echo "Checking for return value of vsnprintf()... Yes."
235 else
236 CFLAGS="$CFLAGS -DHAS_vsnprintf_void"
237 echo "Checking for return value of vsnprintf()... No."
238 echo " WARNING: apparently vsnprintf() does not return a value. zlib"
239 echo " can build but will be open to possible string-format security"
240 echo " vulnerabilities."
241 fi
242 else
243 CFLAGS="$CFLAGS -DNO_vsnprintf"
244 echo "Checking for vsnprintf() in stdio.h... No."
245 echo " WARNING: vsnprintf() not found, falling back to vsprintf(). zlib"
246 echo " can build but will be open to possible buffer-overflow security"
247 echo " vulnerabilities."
248
249 cat >$test.c <<EOF
250#include <stdio.h>
251#include <stdarg.h>
252
253int mytest(char *fmt, ...)
254{
255 int i;
256 char buf[20];
257 va_list ap;
258
259 va_start(ap, fmt);
260 i = vsprintf(buf, fmt, ap);
261 va_end(ap);
262 return 0;
263}
264
265int main()
266{
267 return (mytest("Hello%d\n", 1));
268}
269EOF
270
271 if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
272 echo "Checking for return value of vsprintf()... Yes."
273 else
274 CFLAGS="$CFLAGS -DHAS_vsprintf_void"
275 echo "Checking for return value of vsprintf()... No."
276 echo " WARNING: apparently vsprintf() does not return a value. zlib"
277 echo " can build but will be open to possible string-format security"
278 echo " vulnerabilities."
279 fi
280 fi
281else
282 echo "Checking whether to use vsnprintf() or snprintf()... using snprintf()"
283
284 cat >$test.c <<EOF
285#include <stdio.h>
286#include <stdarg.h>
287
288int mytest()
289{
290 char buf[20];
291
292 snprintf(buf, sizeof(buf), "%s", "foo");
293 return 0;
294}
295
296int main()
297{
298 return (mytest());
299}
300EOF
301
302 if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
303 echo "Checking for snprintf() in stdio.h... Yes."
304
305 cat >$test.c <<EOF
306#include <stdio.h>
307#include <stdarg.h>
308
309int mytest(char *fmt, ...)
310{
311 int i;
312 char buf[20];
313
314 i = snprintf(buf, sizeof(buf), "%s", "foo");
315 return 0;
316}
317
318int main()
319{
320 return (mytest());
321}
322EOF
323
324 if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
325 echo "Checking for return value of snprintf()... Yes."
326 else
327 CFLAGS="$CFLAGS -DHAS_snprintf_void"
328 echo "Checking for return value of snprintf()... No."
329 echo " WARNING: apparently snprintf() does not return a value. zlib"
330 echo " can build but will be open to possible string-format security"
331 echo " vulnerabilities."
332 fi
333 else
334 CFLAGS="$CFLAGS -DNO_snprintf"
335 echo "Checking for snprintf() in stdio.h... No."
336 echo " WARNING: snprintf() not found, falling back to sprintf(). zlib"
337 echo " can build but will be open to possible buffer-overflow security"
338 echo " vulnerabilities."
339
340 cat >$test.c <<EOF
341#include <stdio.h>
342#include <stdarg.h>
343
344int mytest(char *fmt, ...)
345{
346 int i;
347 char buf[20];
348
349 i = sprintf(buf, "%s", "foo");
350 return 0;
351}
352
353int main()
354{
355 return (mytest());
356}
357EOF
358
359 if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
360 echo "Checking for return value of sprintf()... Yes."
361 else
362 CFLAGS="$CFLAGS -DHAS_sprintf_void"
363 echo "Checking for return value of sprintf()... No."
364 echo " WARNING: apparently sprintf() does not return a value. zlib"
365 echo " can build but will be open to possible string-format security"
366 echo " vulnerabilities."
367 fi
368 fi
369fi
370
371cat >$test.c <<EOF
159#include <errno.h> 372#include <errno.h>
160int main() { return 0; } 373int main() { return 0; }
161EOF 374EOF
diff --git a/contrib/README.contrib b/contrib/README.contrib
index 7ad191c..fcee020 100644
--- a/contrib/README.contrib
+++ b/contrib/README.contrib
@@ -11,12 +11,18 @@ asm586/ and asm686/ by Brian Raiter <breadbox@muppetlabs.com>
11 asm code for Pentium and Pentium Pro 11 asm code for Pentium and Pentium Pro
12 See http://www.muppetlabs.com/~breadbox/software/assembly.html 12 See http://www.muppetlabs.com/~breadbox/software/assembly.html
13 13
14delphi/ by Bob Dellaca <bobdl@xtra.co.nz> 14blast/ by Mark Adler <madler@alumni.caltech.edu>
15 Decompressor for output of PKWare Data Compression Library
16
17delphi/ by Bob Dellaca <bobdl@xtra.co.nz>
15 Support for Delphi 18 Support for Delphi
16 19
17delphi2/ by Davide Moretti <dave@rimini.com> 20delphi2/ by Davide Moretti <dave@rimini.com>
18 Another support for C++Builder and Delphi 21 Another support for C++Builder and Delphi
19 22
23inflate86/ by Chris Anderson <christop@charm.net>
24 Tuned x86 gcc asm code to replace inflate_fast()
25
20minizip/ by Gilles Vollant <info@winimage.com> 26minizip/ by Gilles Vollant <info@winimage.com>
21 Mini zip and unzip based on zlib 27 Mini zip and unzip based on zlib
22 See http://www.winimage.com/zLibDll/unzip.html 28 See http://www.winimage.com/zLibDll/unzip.html
diff --git a/contrib/blast/Makefile b/contrib/blast/Makefile
new file mode 100644
index 0000000..9be80ba
--- /dev/null
+++ b/contrib/blast/Makefile
@@ -0,0 +1,8 @@
1blast: blast.c blast.h
2 cc -DTEST -o blast blast.c
3
4test: blast
5 blast < test.pk | cmp - test.txt
6
7clean:
8 rm -f blast blast.o
diff --git a/contrib/blast/README b/contrib/blast/README
new file mode 100644
index 0000000..e3a60b3
--- /dev/null
+++ b/contrib/blast/README
@@ -0,0 +1,4 @@
1Read blast.h for purpose and usage.
2
3Mark Adler
4madler@alumni.caltech.edu
diff --git a/contrib/blast/blast.c b/contrib/blast/blast.c
new file mode 100644
index 0000000..67dab4e
--- /dev/null
+++ b/contrib/blast/blast.c
@@ -0,0 +1,444 @@
1/* blast.c
2 * Copyright (C) 2003 Mark Adler
3 * For conditions of distribution and use, see copyright notice in blast.h
4 * version 1.1, 16 Feb 2003
5 *
6 * blast.c decompresses data compressed by the PKWare Compression Library.
7 * This function provides functionality similar to the explode() function of
8 * the PKWare library, hence the name "blast".
9 *
10 * This decompressor is based on the excellent format description provided by
11 * Ben Rudiak-Gould in comp.compression on August 13, 2001. Interestingly, the
12 * example Ben provided in the post is incorrect. The distance 110001 should
13 * instead be 111000. When corrected, the example byte stream becomes:
14 *
15 * 00 04 82 24 25 8f 80 7f
16 *
17 * which decompresses to "AIAIAIAIAIAIA" (without the quotes).
18 */
19
20/*
21 * Change history:
22 *
23 * 1.0 12 Feb 2003 - First version
24 * 1.1 16 Feb 2003 - Fixed distance check for > 4 GB uncompressed data
25 */
26
27#include <setjmp.h> /* for setjmp(), longjmp(), and jmp_buf */
28#include "blast.h" /* prototype for blast() */
29
30#define local static /* for local function definitions */
31#define MAXBITS 13 /* maximum code length */
32#define MAXWIN 4096 /* maximum window size */
33
34/* input and output state */
35struct state {
36 /* input state */
37 blast_in infun; /* input function provided by user */
38 void *inhow; /* opaque information passed to infun() */
39 unsigned char *in; /* next input location */
40 unsigned left; /* available input at in */
41 int bitbuf; /* bit buffer */
42 int bitcnt; /* number of bits in bit buffer */
43
44 /* input limit error return state for bits() and decode() */
45 jmp_buf env;
46
47 /* output state */
48 blast_out outfun; /* output function provided by user */
49 void *outhow; /* opaque information passed to outfun() */
50 unsigned next; /* index of next write location in out[] */
51 int first; /* true to check distances (for first 4K) */
52 unsigned char out[MAXWIN]; /* output buffer and sliding window */
53};
54
55/*
56 * Return need bits from the input stream. This always leaves less than
57 * eight bits in the buffer. bits() works properly for need == 0.
58 *
59 * Format notes:
60 *
61 * - Bits are stored in bytes from the least significant bit to the most
62 * significant bit. Therefore bits are dropped from the bottom of the bit
63 * buffer, using shift right, and new bytes are appended to the top of the
64 * bit buffer, using shift left.
65 */
66local int bits(struct state *s, int need)
67{
68 int val; /* bit accumulator */
69
70 /* load at least need bits into val */
71 val = s->bitbuf;
72 while (s->bitcnt < need) {
73 if (s->left == 0) {
74 s->left = s->infun(s->inhow, &(s->in));
75 if (s->left == 0) longjmp(s->env, 1); /* out of input */
76 }
77 val |= (int)(*(s->in)++) << s->bitcnt; /* load eight bits */
78 s->left--;
79 s->bitcnt += 8;
80 }
81
82 /* drop need bits and update buffer, always zero to seven bits left */
83 s->bitbuf = val >> need;
84 s->bitcnt -= need;
85
86 /* return need bits, zeroing the bits above that */
87 return val & ((1 << need) - 1);
88}
89
90/*
91 * Huffman code decoding tables. count[1..MAXBITS] is the number of symbols of
92 * each length, which for a canonical code are stepped through in order.
93 * symbol[] are the symbol values in canonical order, where the number of
94 * entries is the sum of the counts in count[]. The decoding process can be
95 * seen in the function decode() below.
96 */
97struct huffman {
98 short *count; /* number of symbols of each length */
99 short *symbol; /* canonically ordered symbols */
100};
101
102/*
103 * Decode a code from the stream s using huffman table h. Return the symbol or
104 * a negative value if there is an error. If all of the lengths are zero, i.e.
105 * an empty code, or if the code is incomplete and an invalid code is received,
106 * then -9 is returned after reading MAXBITS bits.
107 *
108 * Format notes:
109 *
110 * - The codes as stored in the compressed data are bit-reversed relative to
111 * a simple integer ordering of codes of the same lengths. Hence below the
112 * bits are pulled from the compressed data one at a time and used to
113 * build the code value reversed from what is in the stream in order to
114 * permit simple integer comparisons for decoding.
115 *
116 * - The first code for the shortest length is all ones. Subsequent codes of
117 * the same length are simply integer decrements of the previous code. When
118 * moving up a length, a one bit is appended to the code. For a complete
119 * code, the last code of the longest length will be all zeros. To support
120 * this ordering, the bits pulled during decoding are inverted to apply the
121 * more "natural" ordering starting with all zeros and incrementing.
122 */
123local int decode(struct state *s, struct huffman *h)
124{
125 int len; /* current number of bits in code */
126 int code; /* len bits being decoded */
127 int first; /* first code of length len */
128 int count; /* number of codes of length len */
129 int index; /* index of first code of length len in symbol table */
130 int bitbuf; /* bits from stream */
131 int left; /* bits left in next or left to process */
132 short *next; /* next number of codes */
133
134 bitbuf = s->bitbuf;
135 left = s->bitcnt;
136 code = first = index = 0;
137 len = 1;
138 next = h->count + 1;
139 while (1) {
140 while (left--) {
141 code |= (bitbuf & 1) ^ 1; /* invert code */
142 bitbuf >>= 1;
143 count = *next++;
144 if (code < first + count) { /* if length len, return symbol */
145 s->bitbuf = bitbuf;
146 s->bitcnt = (s->bitcnt - len) & 7;
147 return h->symbol[index + (code - first)];
148 }
149 index += count; /* else update for next length */
150 first += count;
151 first <<= 1;
152 code <<= 1;
153 len++;
154 }
155 left = (MAXBITS+1) - len;
156 if (left == 0) break;
157 if (s->left == 0) {
158 s->left = s->infun(s->inhow, &(s->in));
159 if (s->left == 0) longjmp(s->env, 1); /* out of input */
160 }
161 bitbuf = *(s->in)++;
162 s->left--;
163 if (left > 8) left = 8;
164 }
165 return -9; /* ran out of codes */
166}
167
168/*
169 * Given a list of repeated code lengths rep[0..n-1], where each byte is a
170 * count (high four bits + 1) and a code length (low four bits), generate the
171 * list of code lengths. This compaction reduces the size of the object code.
172 * Then given the list of code lengths length[0..n-1] representing a canonical
173 * Huffman code for n symbols, construct the tables required to decode those
174 * codes. Those tables are the number of codes of each length, and the symbols
175 * sorted by length, retaining their original order within each length. The
176 * return value is zero for a complete code set, negative for an over-
177 * subscribed code set, and positive for an incomplete code set. The tables
178 * can be used if the return value is zero or positive, but they cannot be used
179 * if the return value is negative. If the return value is zero, it is not
180 * possible for decode() using that table to return an error--any stream of
181 * enough bits will resolve to a symbol. If the return value is positive, then
182 * it is possible for decode() using that table to return an error for received
183 * codes past the end of the incomplete lengths.
184 */
185local int construct(struct huffman *h, const unsigned char *rep, int n)
186{
187 int symbol; /* current symbol when stepping through length[] */
188 int len; /* current length when stepping through h->count[] */
189 int left; /* number of possible codes left of current length */
190 short offs[MAXBITS+1]; /* offsets in symbol table for each length */
191 short length[256]; /* code lengths */
192
193 /* convert compact repeat counts into symbol bit length list */
194 symbol = 0;
195 do {
196 len = *rep++;
197 left = (len >> 4) + 1;
198 len &= 15;
199 do {
200 length[symbol++] = len;
201 } while (--left);
202 } while (--n);
203 n = symbol;
204
205 /* count number of codes of each length */
206 for (len = 0; len <= MAXBITS; len++)
207 h->count[len] = 0;
208 for (symbol = 0; symbol < n; symbol++)
209 (h->count[length[symbol]])++; /* assumes lengths are within bounds */
210 if (h->count[0] == n) /* no codes! */
211 return 0; /* complete, but decode() will fail */
212
213 /* check for an over-subscribed or incomplete set of lengths */
214 left = 1; /* one possible code of zero length */
215 for (len = 1; len <= MAXBITS; len++) {
216 left <<= 1; /* one more bit, double codes left */
217 left -= h->count[len]; /* deduct count from possible codes */
218 if (left < 0) return left; /* over-subscribed--return negative */
219 } /* left > 0 means incomplete */
220
221 /* generate offsets into symbol table for each length for sorting */
222 offs[1] = 0;
223 for (len = 1; len < MAXBITS; len++)
224 offs[len + 1] = offs[len] + h->count[len];
225
226 /*
227 * put symbols in table sorted by length, by symbol order within each
228 * length
229 */
230 for (symbol = 0; symbol < n; symbol++)
231 if (length[symbol] != 0)
232 h->symbol[offs[length[symbol]]++] = symbol;
233
234 /* return zero for complete set, positive for incomplete set */
235 return left;
236}
237
238/*
239 * Decode PKWare Compression Library stream.
240 *
241 * Format notes:
242 *
243 * - First byte is 0 if literals are uncoded or 1 if they are coded. Second
244 * byte is 4, 5, or 6 for the number of extra bits in the distance code.
245 * This is the base-2 logarithm of the dictionary size minus six.
246 *
247 * - Compressed data is a combination of literals and length/distance pairs
248 * terminated by an end code. Literals are either Huffman coded or
249 * uncoded bytes. A length/distance pair is a coded length followed by a
250 * coded distance to represent a string that occurs earlier in the
251 * uncompressed data that occurs again at the current location.
252 *
253 * - A bit preceding a literal or length/distance pair indicates which comes
254 * next, 0 for literals, 1 for length/distance.
255 *
256 * - If literals are uncoded, then the next eight bits are the literal, in the
257 * normal bit order in th stream, i.e. no bit-reversal is needed. Similarly,
258 * no bit reversal is needed for either the length extra bits or the distance
259 * extra bits.
260 *
261 * - Literal bytes are simply written to the output. A length/distance pair is
262 * an instruction to copy previously uncompressed bytes to the output. The
263 * copy is from distance bytes back in the output stream, copying for length
264 * bytes.
265 *
266 * - Distances pointing before the beginning of the output data are not
267 * permitted.
268 *
269 * - Overlapped copies, where the length is greater than the distance, are
270 * allowed and common. For example, a distance of one and a length of 518
271 * simply copies the last byte 518 times. A distance of four and a length of
272 * twelve copies the last four bytes three times. A simple forward copy
273 * ignoring whether the length is greater than the distance or not implements
274 * this correctly.
275 */
276local int decomp(struct state *s)
277{
278 int lit; /* true if literals are coded */
279 int dict; /* log2(dictionary size) - 6 */
280 int symbol; /* decoded symbol, extra bits for distance */
281 int len; /* length for copy */
282 int dist; /* distance for copy */
283 int copy; /* copy counter */
284 unsigned char *from, *to; /* copy pointers */
285 static int virgin = 1; /* build tables once */
286 static short litcnt[MAXBITS+1], litsym[256]; /* litcode memory */
287 static short lencnt[MAXBITS+1], lensym[16]; /* lencode memory */
288 static short distcnt[MAXBITS+1], distsym[64]; /* distcode memory */
289 static struct huffman litcode = {litcnt, litsym}; /* length code */
290 static struct huffman lencode = {lencnt, lensym}; /* length code */
291 static struct huffman distcode = {distcnt, distsym};/* distance code */
292 /* bit lengths of literal codes */
293 static const unsigned char litlen[] = {
294 11, 124, 8, 7, 28, 7, 188, 13, 76, 4, 10, 8, 12, 10, 12, 10, 8, 23, 8,
295 9, 7, 6, 7, 8, 7, 6, 55, 8, 23, 24, 12, 11, 7, 9, 11, 12, 6, 7, 22, 5,
296 7, 24, 6, 11, 9, 6, 7, 22, 7, 11, 38, 7, 9, 8, 25, 11, 8, 11, 9, 12,
297 8, 12, 5, 38, 5, 38, 5, 11, 7, 5, 6, 21, 6, 10, 53, 8, 7, 24, 10, 27,
298 44, 253, 253, 253, 252, 252, 252, 13, 12, 45, 12, 45, 12, 61, 12, 45,
299 44, 173};
300 /* bit lengths of length codes 0..15 */
301 static const unsigned char lenlen[] = {2, 35, 36, 53, 38, 23};
302 /* bit lengths of distance codes 0..63 */
303 static const unsigned char distlen[] = {2, 20, 53, 230, 247, 151, 248};
304 static const short base[16] = { /* base for length codes */
305 3, 2, 4, 5, 6, 7, 8, 9, 10, 12, 16, 24, 40, 72, 136, 264};
306 static const char extra[16] = { /* extra bits for length codes */
307 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8};
308
309 /* set up decoding tables (once--might not be thread-safe) */
310 if (virgin) {
311 construct(&litcode, litlen, sizeof(litlen));
312 construct(&lencode, lenlen, sizeof(lenlen));
313 construct(&distcode, distlen, sizeof(distlen));
314 virgin = 0;
315 }
316
317 /* read header */
318 lit = bits(s, 8);
319 if (lit > 1) return -1;
320 dict = bits(s, 8);
321 if (dict < 4 || dict > 6) return -2;
322
323 /* decode literals and length/distance pairs */
324 do {
325 if (bits(s, 1)) {
326 /* get length */
327 symbol = decode(s, &lencode);
328 len = base[symbol] + bits(s, extra[symbol]);
329 if (len == 519) break; /* end code */
330
331 /* get distance */
332 symbol = len == 2 ? 2 : dict;
333 dist = decode(s, &distcode) << symbol;
334 dist += bits(s, symbol);
335 dist++;
336 if (s->first && dist > s->next)
337 return -3; /* distance too far back */
338
339 /* copy length bytes from distance bytes back */
340 do {
341 to = s->out + s->next;
342 from = to - dist;
343 copy = MAXWIN;
344 if (s->next < dist) {
345 from += copy;
346 copy = dist;
347 }
348 copy -= s->next;
349 if (copy > len) copy = len;
350 len -= copy;
351 s->next += copy;
352 do {
353 *to++ = *from++;
354 } while (--copy);
355 if (s->next == MAXWIN) {
356 if (s->outfun(s->outhow, s->out, s->next)) return 1;
357 s->next = 0;
358 s->first = 0;
359 }
360 } while (len != 0);
361 }
362 else {
363 /* get literal and write it */
364 symbol = lit ? decode(s, &litcode) : bits(s, 8);
365 s->out[s->next++] = symbol;
366 if (s->next == MAXWIN) {
367 if (s->outfun(s->outhow, s->out, s->next)) return 1;
368 s->next = 0;
369 s->first = 0;
370 }
371 }
372 } while (1);
373 return 0;
374}
375
376/* See comments in blast.h */
377int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow)
378{
379 struct state s; /* input/output state */
380 int err; /* return value */
381
382 /* initialize input state */
383 s.infun = infun;
384 s.inhow = inhow;
385 s.left = 0;
386 s.bitbuf = 0;
387 s.bitcnt = 0;
388
389 /* initialize output state */
390 s.outfun = outfun;
391 s.outhow = outhow;
392 s.next = 0;
393 s.first = 1;
394
395 /* return if bits() or decode() tries to read past available input */
396 if (setjmp(s.env) != 0) /* if came back here via longjmp(), */
397 err = 2; /* then skip decomp(), return error */
398 else
399 err = decomp(&s); /* decompress */
400
401 /* write any leftover output and update the error code if needed */
402 if (err != 1 && s.next && s.outfun(s.outhow, s.out, s.next) && err == 0)
403 err = 1;
404 return err;
405}
406
407#ifdef TEST
408/* Example of how to use blast() */
409#include <stdio.h>
410#include <stdlib.h>
411
412#define CHUNK 16384
413
414local unsigned inf(void *how, unsigned char **buf)
415{
416 static unsigned char hold[CHUNK];
417
418 *buf = hold;
419 return fread(hold, 1, CHUNK, (FILE *)how);
420}
421
422local int outf(void *how, unsigned char *buf, unsigned len)
423{
424 return fwrite(buf, 1, len, (FILE *)how) != len;
425}
426
427/* Decompress a PKWare Compression Library stream from stdin to stdout */
428int main(void)
429{
430 int ret, n;
431
432 /* decompress to stdout */
433 ret = blast(inf, stdin, outf, stdout);
434 if (ret != 0) fprintf(stderr, "blast error: %d\n", ret);
435
436 /* see if there are any leftover bytes */
437 n = 0;
438 while (getchar() != EOF) n++;
439 if (n) fprintf(stderr, "blast warning: %d unused bytes of input\n", n);
440
441 /* return blast() error code */
442 return ret;
443}
444#endif
diff --git a/contrib/blast/blast.h b/contrib/blast/blast.h
new file mode 100644
index 0000000..2417837
--- /dev/null
+++ b/contrib/blast/blast.h
@@ -0,0 +1,71 @@
1/* blast.h -- interface for blast.c
2 Copyright (C) 2003 Mark Adler
3 version 1.1, 16 Feb 2003
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the author be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20
21 Mark Adler madler@alumni.caltech.edu
22 */
23
24
25/*
26 * blast() decompresses the PKWare Data Compression Library (DCL) compressed
27 * format. It provides the same functionality as the explode() function in
28 * that library. (Note: PKWare overused the "implode" verb, and the format
29 * used by their library implode() function is completely different and
30 * incompatible with the implode compression method supported by PKZIP.)
31 */
32
33
34typedef unsigned (*blast_in)(void *how, unsigned char **buf);
35typedef int (*blast_out)(void *how, unsigned char *buf, unsigned len);
36/* Definitions for input/output functions passed to blast(). See below for
37 * what the provided functions need to do.
38 */
39
40
41int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow);
42/* Decompress input to output using the provided infun() and outfun() calls.
43 * On success, the return value of blast() is zero. If there is an error in
44 * the source data, i.e. it is not in the proper format, then a negative value
45 * is returned. If there is not enough input available or there is not enough
46 * output space, then a positive error is returned.
47 *
48 * The input function is invoked: len = infun(how, &buf), where buf is set by
49 * infun() to point to the input buffer, and infun() returns the number of
50 * available bytes there. If infun() returns zero, then blast() returns with
51 * an input error. (blast() only asks for input if it needs it.) inhow is for
52 * use by the application to pass an input descriptor to infun(), if desired.
53 *
54 * The output function is invoked: err = outfun(how, buf, len), where the bytes
55 * to be written are buf[0..len-1]. If err is not zero, then blast() returns
56 * with an output error. outfun() is always called with len <= 4096. outhow
57 * is for use by the application to pass an output descriptor to outfun(), if
58 * desired.
59 *
60 * The return codes are:
61 *
62 * 2: ran out of input before completing decompression
63 * 1: output error before completing decompression
64 * 0: successful decompression
65 * -1: literal flag not zero or one
66 * -2: dictionary size not in 4..6
67 * -3: distance is too far back
68 *
69 * At the bottom of blast.c is an example program that uses blast() that can be
70 * compiled to produce a command-line decompression filter by defining TEST.
71 */
diff --git a/contrib/blast/test.pk b/contrib/blast/test.pk
new file mode 100644
index 0000000..be10b2b
--- /dev/null
+++ b/contrib/blast/test.pk
Binary files differ
diff --git a/contrib/blast/test.txt b/contrib/blast/test.txt
new file mode 100644
index 0000000..bfdf1c5
--- /dev/null
+++ b/contrib/blast/test.txt
@@ -0,0 +1 @@
AIAIAIAIAIAIA \ No newline at end of file
diff --git a/contrib/inflate86/inffast.S b/contrib/inflate86/inffast.S
new file mode 100644
index 0000000..d1e80ef
--- /dev/null
+++ b/contrib/inflate86/inffast.S
@@ -0,0 +1,1095 @@
1/*
2 * inffast.S is a hand tuned assembler version of:
3 *
4 * inffast.c -- fast decoding
5 * Copyright (C) 1995-2003 Mark Adler
6 * For conditions of distribution and use, see copyright notice in zlib.h
7 *
8 * Copyright (C) 2003 Chris Anderson <christop@charm.net>
9 * Please use the copyright conditions above.
10 *
11 * This version (Jan-23-2003) of inflate_fast was coded and tested under
12 * GNU/Linux on a pentium 3, using the gcc-3.2 compiler distribution. On that
13 * machine, I found that gzip style archives decompressed about 20% faster than
14 * the gcc-3.2 -O3 -fomit-frame-pointer compiled version. Your results will
15 * depend on how large of a buffer is used for z_stream.next_in & next_out
16 * (8K-32K worked best for my 256K cpu cache) and how much overhead there is in
17 * stream processing I/O and crc32/addler32. In my case, this routine used
18 * 70% of the cpu time and crc32 used 20%.
19 *
20 * I am confident that this version will work in the general case, but I have
21 * not tested a wide variety of datasets or a wide variety of platforms.
22 *
23 * Jan-24-2003 -- Added -DUSE_MMX define for slightly faster inflating.
24 * It should be a runtime flag instead of compile time flag...
25 */
26
27.file "inffast.S"
28
29.globl inflate_fast
30
31.text
32.align 4,0
33.L_invalid_literal_length_code_msg:
34.string "invalid literal/length code"
35
36.align 4,0
37.L_invalid_distance_code_msg:
38.string "invalid distance code"
39
40.align 4,0
41.L_invalid_distance_too_far_msg:
42.string "invalid distance too far back"
43
44#if defined( USE_MMX )
45.align 4,0
46.L_mask: /* mask[N] = ( 1 << N ) - 1 */
47.long 0
48.long 1
49.long 3
50.long 7
51.long 15
52.long 31
53.long 63
54.long 127
55.long 255
56.long 511
57.long 1023
58.long 2047
59.long 4095
60.long 8191
61.long 16383
62.long 32767
63.long 65535
64.long 131071
65.long 262143
66.long 524287
67.long 1048575
68.long 2097151
69.long 4194303
70.long 8388607
71.long 16777215
72.long 33554431
73.long 67108863
74.long 134217727
75.long 268435455
76.long 536870911
77.long 1073741823
78.long 2147483647
79.long 4294967295
80#endif
81
82.text
83
84/*
85 * struct z_stream offsets, in zlib.h
86 */
87#define next_in_strm 0 /* strm->next_in */
88#define avail_in_strm 4 /* strm->avail_in */
89#define next_out_strm 12 /* strm->next_out */
90#define avail_out_strm 16 /* strm->avail_out */
91#define msg_strm 24 /* strm->msg */
92#define state_strm 28 /* strm->state */
93
94/*
95 * struct inflate_state offsets, in inflate.h
96 */
97#define mode_state 0 /* state->mode */
98#define wsize_state 32 /* state->wsize */
99#define write_state 36 /* state->write */
100#define window_state 40 /* state->window */
101#define hold_state 44 /* state->hold */
102#define bits_state 48 /* state->bits */
103#define lencode_state 64 /* state->lencode */
104#define distcode_state 68 /* state->distcode */
105#define lenbits_state 72 /* state->lenbits */
106#define distbits_state 76 /* state->distbits */
107
108/*
109 * inflate_fast's activation record
110 */
111#define local_var_size 56 /* how much local space for vars */
112#define strm_sp 80 /* first arg: z_stream * (local_var_size + 24) */
113#define start_sp 84 /* second arg: unsigned int (local_var_size + 28) */
114
115/*
116 * offsets for local vars on stack
117 */
118#define out 52 /* unsigned char* */
119#define window 48 /* unsigned char* */
120#define wsize 44 /* unsigned int */
121#define write 40 /* unsigned int */
122#define in 36 /* unsigned char* */
123#define beg 32 /* unsigned char* */
124#define dist 28 /* unsigned int */
125#define len 24 /* unsigned int */
126#define last 20 /* unsigned char* */
127#define end 16 /* unsigned char* */
128#define dcode 12 /* code* */
129#define lcode 8 /* code* */
130#define dmask 4 /* unsigned int */
131#define lmask 0 /* unsigned int */
132
133/*
134 * typedef enum inflate_mode consts, in inflate.h
135 */
136#ifndef NO_GUNZIP
137#define GUNZIP
138#endif
139
140#ifdef GUNZIP
141#define INFLATE_MODE_TYPE 11 /* state->mode flags enum-ed in inflate.h */
142#define INFLATE_MODE_BAD 26
143#else
144#define INFLATE_MODE_TYPE 3
145#define INFLATE_MODE_BAD 17
146#endif
147
148
149.align 16,0x90
150inflate_fast:
151 pushl %edi
152 pushl %esi
153 pushl %ebp
154 pushl %ebx
155 pushf /* save eflags (strm_sp, state_sp assumes this is 32 bits) */
156 subl $local_var_size, %esp
157 cld
158#if defined( USE_MMX )
159 emms
160#endif
161
162#define strm_r %esi
163#define state_r %edi
164
165 movl strm_sp(%esp), strm_r
166 movl state_strm(strm_r), state_r
167
168 /* in = strm->next_in;
169 * out = strm->next_out;
170 * last = in + strm->avail_in - 5;
171 * beg = out - (start - strm->avail_out);
172 * end = out + (strm->avail_out - 257);
173 */
174 movl next_in_strm(strm_r), %eax
175 movl next_out_strm(strm_r), %ebx
176 movl avail_in_strm(strm_r), %edx
177 movl avail_out_strm(strm_r), %ecx
178 movl start_sp(%esp), %ebp
179
180 addl %eax, %edx /* avail_in += next_in */
181 subl $5, %edx /* avail_in -= 5 */
182
183 subl %ecx, %ebp /* start -= avail_out */
184 negl %ebp /* start = -start */
185 addl %ebx, %ebp /* start += next_out */
186
187 subl $257, %ecx /* avail_out -= 257 */
188 addl %ebx, %ecx /* avail_out += out */
189
190 movl %eax, in(%esp)
191 movl %ebx, out(%esp)
192 movl %edx, last(%esp)
193 movl %ebp, beg(%esp)
194 movl %ecx, end(%esp)
195
196 /* wsize = state->wsize;
197 * write = state->write;
198 * window = state->window;
199 * hold = state->hold;
200 * bits = state->bits;
201 * lcode = state->lencode;
202 * dcode = state->distcode;
203 * lmask = ( 1 << state->lenbits ) - 1;
204 * dmask = ( 1 << state->distbits ) - 1;
205 */
206
207 movl lencode_state(state_r), %eax
208 movl distcode_state(state_r), %ecx
209
210 movl %eax, lcode(%esp)
211 movl %ecx, dcode(%esp)
212
213 movl $1, %eax
214 movl lenbits_state(state_r), %ecx
215 shll %cl, %eax
216 decl %eax
217 movl %eax, lmask(%esp)
218
219 movl $1, %eax
220 movl distbits_state(state_r), %ecx
221 shll %cl, %eax
222 decl %eax
223 movl %eax, dmask(%esp)
224
225 movl wsize_state(state_r), %eax
226 movl write_state(state_r), %ecx
227 movl window_state(state_r), %edx
228
229 movl %eax, wsize(%esp)
230 movl %ecx, write(%esp)
231 movl %edx, window(%esp)
232
233#if ! defined( USE_MMX )
234
235#define hold_r %ebp
236#define bits_r %bl
237#define bitslong_r %ebx
238
239 movl hold_state(state_r), hold_r
240 movl bits_state(state_r), bitslong_r
241
242#else /* USE_MMX */
243
244#define hold_mm %mm0
245#define bits_r %ebp
246#define bitslong_r %ebp
247
248 movl hold_state(state_r), %ebx
249 movl bits_state(state_r), bitslong_r
250
251#endif
252
253#undef strm_r
254#undef state_r
255#define in_r %esi
256#define from_r %esi
257#define out_r %edi
258
259 movl in(%esp), in_r
260
261#if ! defined ( USE_MMX )
262
263 /* align in_r on word boundary */
264 testl $1, in_r
265 jz .L_is_aligned
266 xorl %eax, %eax
267 movb (in_r), %al
268 incl in_r
269 movb bits_r, %cl
270 addb $8, bits_r
271 shll %cl, %eax
272 orl %eax, hold_r
273
274#else
275 /* align in_r on long boundary */
276.L_align_long:
277 testl $3, in_r
278 jz .L_is_aligned
279 xorl %eax, %eax
280 movb (in_r), %al
281 incl in_r
282 movl bits_r, %ecx
283 addl $8, bits_r
284 shll %cl, %eax
285 orl %eax, %ebx
286 jmp .L_align_long
287
288#endif
289
290.L_is_aligned:
291 movl out(%esp), out_r
292
293#if defined ( USE_MMX )
294
295#define used_mm %mm1
296#define dmask2_mm %mm2
297#define lmask2_mm %mm3
298#define lmask_mm %mm4
299#define dmask_mm %mm5
300#define tmp_mm %mm6
301
302 movl out(%esp), out_r
303 movd lmask(%esp), lmask_mm
304 movq lmask_mm, lmask2_mm
305 movd dmask(%esp), dmask_mm
306 movq dmask_mm, dmask2_mm
307 movd %ebx, hold_mm
308 pxor used_mm, used_mm
309 movl lcode(%esp), %ebx /* ebx = lcode */
310#endif
311
312 jmp .L_do_loop
313
314.align 16,0x90
315
316#if ! defined ( USE_MMX )
317
318.L_do_loop:
319 /* regs: %esi = in, %ebp = hold, %bl = bits, %edi = out
320 *
321 * do {
322 * if (bits < 15) {
323 * hold |= *((unsigned short *)in)++ << bits;
324 * bits += 16
325 * }
326 * this = lcode[hold & lmask]
327 */
328 cmpb $15, bits_r
329 ja .L_get_length_code /* if (15 < bits) */
330
331 xorl %eax, %eax
332 lodsw /* al = *(ushort *)in++ */
333 movb bits_r, %cl /* cl = bits, needs it for shifting */
334 addb $16, bits_r /* bits += 16 */
335 shll %cl, %eax
336 orl %eax, hold_r /* hold |= *((ushort *)in)++ << bits */
337
338.L_get_length_code:
339 movl lmask(%esp), %edx /* edx = lmask */
340 movl lcode(%esp), %ecx /* ecx = lcode */
341 andl hold_r, %edx /* edx &= hold */
342 movl (%ecx,%edx,4), %eax /* eax = lcode[hold & lmask] */
343
344#else /* USE_MMX */
345
346.L_do_loop:
347 psrlq used_mm, hold_mm /* hold_mm >>= last bit length */
348
349 cmpl $32, bits_r
350 ja .L_get_length_code /* if (32 < bits) */
351
352 movd bits_r, tmp_mm
353 movd (in_r), %mm7
354 addl $4, in_r
355 psllq tmp_mm, %mm7
356 addl $32, bits_r
357 por %mm7, hold_mm /* hold_mm |= *((uint *)in)++ << bits */
358
359.L_get_length_code:
360 pand hold_mm, lmask_mm
361 movd lmask_mm, %eax
362 movq lmask2_mm, lmask_mm
363 movl (%ebx,%eax,4), %eax /* eax = lcode[hold & lmask] */
364
365#endif
366
367#if ! defined( USE_MMX )
368
369.L_dolen:
370 /* regs: %esi = in, %ebp = hold, %bl = bits, %edi = out
371 *
372 * dolen:
373 * bits -= this.bits;
374 * hold >>= this.bits
375 */
376 movb %ah, %cl /* cl = this.bits */
377 subb %ah, bits_r /* bits -= this.bits */
378 shrl %cl, hold_r /* hold >>= this.bits */
379
380 /* check if op is a literal
381 * if (op == 0) {
382 * PUP(out) = this.val;
383 * }
384 */
385 testb %al, %al
386 jnz .L_test_for_length_base /* if (op != 0) 45.7% */
387
388 shrl $16, %eax /* output this.val char */
389 stosb
390
391#else /* USE_MMX */
392
393#define len_r %edx
394
395.L_dolen:
396 movzbl %ah, %ecx /* ecx = this.bits */
397 movl %eax, len_r /* len = this */
398 shrl $16, len_r /* len = this.val */
399 movd %ecx, used_mm
400 subl %ecx, bits_r /* bits -= this.bits */
401
402 testb %al, %al
403 jnz .L_test_for_length_base /* if (op != 0) 45.7% */
404
405 movb %dl, (out_r)
406 incl out_r
407
408#endif
409
410.L_while_test:
411 /* while (in < last && out < end)
412 */
413 cmpl out_r, end(%esp)
414 jbe .L_break_loop /* if (out >= end) */
415
416 cmpl in_r, last(%esp)
417 ja .L_do_loop /* if (in < last) */
418 jmp .L_break_loop
419
420#if ! defined( USE_MMX )
421
422.L_test_for_length_base:
423 /* regs: %esi = in, %ebp = hold, %bl = bits, %edi = out, %edx = len
424 *
425 * else if (op & 16) {
426 * len = this.val
427 * op &= 15
428 * if (op) {
429 * if (op > bits) {
430 * hold |= *((unsigned short *)in)++ << bits;
431 * bits += 16
432 * }
433 * len += hold & mask[op];
434 * bits -= op;
435 * hold >>= op;
436 * }
437 */
438#define len_r %edx
439 movl %eax, len_r /* len = this */
440 shrl $16, len_r /* len = this.val */
441 movb %al, %cl
442
443 testb $16, %al
444 jz .L_test_for_second_level_length /* if ((op & 16) == 0) 8% */
445 andb $15, %cl /* op &= 15 */
446 jz .L_save_len /* if (!op) */
447 cmpb %cl, bits_r
448 jae .L_add_bits_to_len /* if (op <= bits) */
449
450 movb %cl, %ch /* stash op in ch, freeing cl */
451 xorl %eax, %eax
452 lodsw /* al = *(ushort *)in++ */
453 movb bits_r, %cl /* cl = bits, needs it for shifting */
454 addb $16, bits_r /* bits += 16 */
455 shll %cl, %eax
456 orl %eax, hold_r /* hold |= *((ushort *)in)++ << bits */
457 movb %ch, %cl /* move op back to ecx */
458
459.L_add_bits_to_len:
460 movl $1, %eax
461 shll %cl, %eax
462 decl %eax
463 subb %cl, bits_r
464 andl hold_r, %eax /* eax &= hold */
465 shrl %cl, hold_r
466 addl %eax, len_r /* len += hold & mask[op] */
467
468.L_save_len:
469 movl len_r, len(%esp) /* save len */
470#undef len_r
471
472.L_decode_distance:
473 /* regs: %esi = in, %ebp = hold, %bl = bits, %edi = out, %edx = dist
474 *
475 * if (bits < 15) {
476 * hold |= *((unsigned short *)in)++ << bits;
477 * bits += 16
478 * }
479 * this = dcode[hold & dmask];
480 * dodist:
481 * bits -= this.bits;
482 * hold >>= this.bits;
483 * op = this.op;
484 */
485
486 cmpb $15, bits_r
487 ja .L_get_distance_code /* if (15 < bits) */
488
489 xorl %eax, %eax
490 lodsw /* al = *(ushort *)in++ */
491 movb bits_r, %cl /* cl = bits, needs it for shifting */
492 addb $16, bits_r /* bits += 16 */
493 shll %cl, %eax
494 orl %eax, hold_r /* hold |= *((ushort *)in)++ << bits */
495
496.L_get_distance_code:
497 movl dmask(%esp), %edx /* edx = dmask */
498 movl dcode(%esp), %ecx /* ecx = dcode */
499 andl hold_r, %edx /* edx &= hold */
500 movl (%ecx,%edx,4), %eax /* eax = dcode[hold & dmask] */
501
502#else /* USE_MMX */
503
504.L_test_for_length_base:
505 testb $16, %al
506 jz .L_test_for_second_level_length /* if ((op & 16) == 0) 8% */
507 andl $15, %eax /* op &= 15 */
508 jz .L_decode_distance /* if (!op) */
509
510 psrlq used_mm, hold_mm /* hold_mm >>= last bit length */
511 movd %eax, used_mm
512 movd hold_mm, %ecx
513 subl %eax, bits_r
514 andl .L_mask(,%eax,4), %ecx
515 addl %ecx, len_r /* len += hold & mask[op] */
516
517.L_decode_distance:
518
519 psrlq used_mm, hold_mm /* hold_mm >>= last bit length */
520
521 cmpl $32, bits_r
522 ja .L_get_dist_code /* if (32 < bits) */
523
524 movd bits_r, tmp_mm
525 movd (in_r), %mm7
526 addl $4, in_r
527 psllq tmp_mm, %mm7
528 addl $32, bits_r
529 por %mm7, hold_mm /* hold_mm |= *((uint *)in)++ << bits */
530
531.L_get_dist_code:
532 movl dcode(%esp), %ebx /* ebx = dcode */
533 pand hold_mm, dmask_mm
534 movd dmask_mm, %eax
535 movq dmask2_mm, dmask_mm
536 movl (%ebx,%eax,4), %eax /* eax = dcode[hold & lmask] */
537
538#endif
539
540#if ! defined( USE_MMX )
541
542#define dist_r %edx
543.L_dodist:
544 movl %eax, dist_r /* dist = this */
545 shrl $16, dist_r /* dist = this.val */
546 movb %ah, %cl
547 subb %ah, bits_r /* bits -= this.bits */
548 shrl %cl, hold_r /* hold >>= this.bits */
549
550 /* if (op & 16) {
551 * dist = this.val
552 * op &= 15
553 * if (op > bits) {
554 * hold |= *((unsigned short *)in)++ << bits;
555 * bits += 16
556 * }
557 * dist += hold & mask[op];
558 * bits -= op;
559 * hold >>= op;
560 */
561 movb %al, %cl /* cl = this.op */
562
563 testb $16, %al /* if ((op & 16) == 0) */
564 jz .L_test_for_second_level_dist
565 andb $15, %cl /* op &= 15 */
566 jz .L_check_dist_one
567 cmpb %cl, bits_r
568 jae .L_add_bits_to_dist /* if (op <= bits) 97.6% */
569
570 movb %cl, %ch /* stash op in ch, freeing cl */
571 xorl %eax, %eax
572 lodsw /* al = *(ushort *)in++ */
573 movb bits_r, %cl /* cl = bits, needs it for shifting */
574 addb $16, bits_r /* bits += 16 */
575 shll %cl, %eax
576 orl %eax, hold_r /* hold |= *((ushort *)in)++ << bits */
577 movb %ch, %cl /* move op back to ecx */
578
579.L_add_bits_to_dist:
580 movl $1, %eax
581 shll %cl, %eax
582 decl %eax /* (1 << op) - 1 */
583 subb %cl, bits_r
584 andl hold_r, %eax /* eax &= hold */
585 shrl %cl, hold_r
586 addl %eax, dist_r /* dist += hold & ((1 << op) - 1) */
587 jmp .L_check_window
588
589#else /* USE_MMX */
590
591#define dist_r %ebx
592.L_dodist:
593 movzbl %ah, %ecx /* ecx = this.bits */
594 movl %eax, dist_r
595 shrl $16, dist_r /* dist = this.val */
596 subl %ecx, bits_r /* bits -= this.bits */
597 movd %ecx, used_mm
598
599 testb $16, %al /* if ((op & 16) == 0) */
600 jz .L_test_for_second_level_dist
601 andl $15, %eax /* op &= 15 */
602 jz .L_check_dist_one
603
604.L_add_bits_to_dist:
605 psrlq used_mm, hold_mm /* hold_mm >>= last bit length */
606 movd %eax, used_mm /* save bit length of current op */
607 movd hold_mm, %ecx /* get the next bits on input stream */
608 subl %eax, bits_r /* bits -= op bits */
609 andl .L_mask(,%eax,4), %ecx /* ecx = hold & mask[op] */
610 addl %ecx, dist_r /* dist += hold & mask[op] */
611 jmp .L_check_window
612
613#endif
614
615.align 16,0x90
616
617.L_check_dist_one:
618 cmpl $1, dist_r
619 jne .L_check_window
620 cmpl out_r, beg(%esp)
621 je .L_check_window
622
623 decl out_r
624#if ! defined( USE_MMX )
625 movl len(%esp), %ecx
626#else
627 movl len_r, %ecx
628#endif
629 movb (out_r), %al
630 subl $3, %ecx
631
632 movb %al, 1(out_r)
633 movb %al, 2(out_r)
634 movb %al, 3(out_r)
635 addl $4, out_r
636 rep stosb
637
638#if defined( USE_MMX )
639 movl lcode(%esp), %ebx /* move lcode back to %ebx, toss dist */
640#endif
641 jmp .L_while_test
642
643.align 16,0x90
644
645.L_check_window:
646 /* regs: %esi = from, %ebp = hold, %bl = bits, %edi = out, %edx = dist
647 * %ecx = nbytes
648 *
649 * nbytes = out - beg;
650 * if (dist <= nbytes) {
651 * from = out - dist;
652 * do {
653 * PUP(out) = PUP(from);
654 * } while (--len > 0) {
655 * }
656 */
657
658 movl in_r, in(%esp) /* save in so from can use it's reg */
659 movl out_r, %eax
660 subl beg(%esp), %eax /* nbytes = out - beg */
661
662 cmpl dist_r, %eax
663 jb .L_clip_window /* if (dist > nbytes) 4.2% */
664
665#if ! defined( USE_MMX )
666 movl len(%esp), %ecx
667#else
668 movl len_r, %ecx
669#endif
670 movl out_r, from_r
671 subl dist_r, from_r /* from = out - dist */
672
673 subl $3, %ecx
674 movb (from_r), %al
675 movb %al, (out_r)
676 movb 1(from_r), %al
677 movb 2(from_r), %dl
678 addl $3, from_r
679 movb %al, 1(out_r)
680 movb %dl, 2(out_r)
681 addl $3, out_r
682 rep movsb
683
684 movl in(%esp), in_r /* move in back to %esi, toss from */
685#if defined( USE_MMX )
686 movl lcode(%esp), %ebx /* move lcode back to %ebx, toss dist */
687#endif
688 jmp .L_while_test
689
690.align 16,0x90
691
692#if ! defined( USE_MMX )
693
694.L_test_for_second_level_length:
695 /* else if ((op & 64) == 0) {
696 * this = lcode[this.val + (hold & mask[op])];
697 * }
698 */
699 testb $64, %al
700 jnz .L_test_for_end_of_block /* if ((op & 64) != 0) */
701
702 movl $1, %eax
703 shll %cl, %eax
704 decl %eax
705 andl hold_r, %eax /* eax &= hold */
706 addl %edx, %eax /* eax += this.val */
707 movl lcode(%esp), %edx /* edx = lcode */
708 movl (%edx,%eax,4), %eax /* eax = lcode[val + (hold&mask[op])] */
709 jmp .L_dolen
710
711#else /* USE_MMX */
712
713.L_test_for_second_level_length:
714 testb $64, %al
715 jnz .L_test_for_end_of_block /* if ((op & 64) != 0) */
716
717 andl $15, %eax
718 psrlq used_mm, hold_mm /* hold_mm >>= last bit length */
719 movd hold_mm, %ecx
720 andl .L_mask(,%eax,4), %ecx
721 addl len_r, %ecx
722 movl (%ebx,%ecx,4), %eax /* eax = lcode[hold & lmask] */
723 jmp .L_dolen
724
725#endif
726
727.align 16,0x90
728
729#if ! defined( USE_MMX )
730
731.L_test_for_second_level_dist:
732 /* else if ((op & 64) == 0) {
733 * this = dcode[this.val + (hold & mask[op])];
734 * }
735 */
736 testb $64, %al
737 jnz .L_invalid_distance_code /* if ((op & 64) != 0) */
738
739 movl $1, %eax
740 shll %cl, %eax
741 decl %eax
742 andl hold_r, %eax /* eax &= hold */
743 addl %edx, %eax /* eax += this.val */
744 movl dcode(%esp), %edx /* edx = dcode */
745 movl (%edx,%eax,4), %eax /* eax = dcode[val + (hold&mask[op])] */
746 jmp .L_dodist
747
748#else /* USE_MMX */
749
750.L_test_for_second_level_dist:
751 testb $64, %al
752 jnz .L_invalid_distance_code /* if ((op & 64) != 0) */
753
754 andl $15, %eax
755 psrlq used_mm, hold_mm /* hold_mm >>= last bit length */
756 movd hold_mm, %ecx
757 andl .L_mask(,%eax,4), %ecx
758 movl dcode(%esp), %eax /* ecx = dcode */
759 addl dist_r, %ecx
760 movl (%eax,%ecx,4), %eax /* eax = lcode[hold & lmask] */
761 jmp .L_dodist
762
763#endif
764
765.align 16,0x90
766.L_clip_window:
767 /* regs: %esi = from, %ebp = hold, %bl = bits, %edi = out, %edx = dist
768 * %ecx = nbytes
769 *
770 * else {
771 * if (dist > wsize) {
772 * invalid distance
773 * }
774 * from = window;
775 * nbytes = dist - nbytes;
776 * if (write == 0) {
777 * from += wsize - nbytes;
778 */
779#define nbytes_r %ecx
780
781 movl %eax, nbytes_r
782 movl wsize(%esp), %eax /* prepare for dist compare */
783 negl nbytes_r /* nbytes = -nbytes */
784 movl window(%esp), from_r /* from = window */
785
786 cmpl dist_r, %eax
787 jb .L_invalid_distance_too_far /* if (dist > wsize) */
788
789 addl dist_r, nbytes_r /* nbytes = dist - nbytes */
790 cmpl $0, write(%esp)
791 jne .L_wrap_around_window /* if (write != 0) */
792
793 subl nbytes_r, %eax
794 addl %eax, from_r /* from += wsize - nbytes */
795
796 /* regs: %esi = from, %ebp = hold, %bl = bits, %edi = out, %edx = dist
797 * %ecx = nbytes, %eax = len
798 *
799 * if (nbytes < len) {
800 * len -= nbytes;
801 * do {
802 * PUP(out) = PUP(from);
803 * } while (--nbytes);
804 * from = out - dist;
805 * }
806 * }
807 */
808
809#if ! defined( USE_MMX )
810#define len_r %eax
811 movl len(%esp), len_r
812#endif
813 cmpl nbytes_r, len_r
814 jbe .L_do_copy1 /* if (nbytes >= len) */
815
816 subl nbytes_r, len_r /* len -= nbytes */
817 rep movsb
818 movl out_r, from_r
819 subl dist_r, from_r /* from = out - dist */
820 jmp .L_do_copy1
821
822 cmpl nbytes_r, len_r
823 jbe .L_do_copy1 /* if (nbytes >= len) */
824
825 subl nbytes_r, len_r /* len -= nbytes */
826 rep movsb
827 movl out_r, from_r
828 subl dist_r, from_r /* from = out - dist */
829 jmp .L_do_copy1
830
831.L_wrap_around_window:
832 /* regs: %esi = from, %ebp = hold, %bl = bits, %edi = out, %edx = dist
833 * %ecx = nbytes, %eax = write, %eax = len
834 *
835 * else if (write < nbytes) {
836 * from += wsize + write - nbytes;
837 * nbytes -= write;
838 * if (nbytes < len) {
839 * len -= nbytes;
840 * do {
841 * PUP(out) = PUP(from);
842 * } while (--nbytes);
843 * from = window;
844 * nbytes = write;
845 * if (nbytes < len) {
846 * len -= nbytes;
847 * do {
848 * PUP(out) = PUP(from);
849 * } while(--nbytes);
850 * from = out - dist;
851 * }
852 * }
853 * }
854 */
855#define write_r %eax
856
857 movl write(%esp), write_r
858 cmpl write_r, nbytes_r
859 jbe .L_contiguous_in_window /* if (write >= nbytes) */
860
861 addl wsize(%esp), from_r
862 addl write_r, from_r
863 subl nbytes_r, from_r /* from += wsize + write - nbytes */
864 subl write_r, nbytes_r /* nbytes -= write */
865#undef write_r
866
867#if ! defined( USE_MMX )
868 movl len(%esp), len_r
869#endif
870 cmpl nbytes_r, len_r
871 jbe .L_do_copy1 /* if (nbytes >= len) */
872
873 subl nbytes_r, len_r /* len -= nbytes */
874 rep movsb
875 movl window(%esp), from_r /* from = window */
876 movl write(%esp), nbytes_r /* nbytes = write */
877 cmpl nbytes_r, len_r
878 jbe .L_do_copy1 /* if (nbytes >= len) */
879
880 subl nbytes_r, len_r /* len -= nbytes */
881 rep movsb
882 movl out_r, from_r
883 subl dist_r, from_r /* from = out - dist */
884 jmp .L_do_copy1
885
886.L_contiguous_in_window:
887 /* regs: %esi = from, %ebp = hold, %bl = bits, %edi = out, %edx = dist
888 * %ecx = nbytes, %eax = write, %eax = len
889 *
890 * else {
891 * from += write - nbytes;
892 * if (nbytes < len) {
893 * len -= nbytes;
894 * do {
895 * PUP(out) = PUP(from);
896 * } while (--nbytes);
897 * from = out - dist;
898 * }
899 * }
900 */
901#define write_r %eax
902
903 addl write_r, from_r
904 subl nbytes_r, from_r /* from += write - nbytes */
905#undef write_r
906
907#if ! defined( USE_MMX )
908 movl len(%esp), len_r
909#endif
910 cmpl nbytes_r, len_r
911 jbe .L_do_copy1 /* if (nbytes >= len) */
912
913 subl nbytes_r, len_r /* len -= nbytes */
914 rep movsb
915 movl out_r, from_r
916 subl dist_r, from_r /* from = out - dist */
917
918.L_do_copy1:
919 /* regs: %esi = from, %esi = in, %ebp = hold, %bl = bits, %edi = out
920 * %eax = len
921 *
922 * while (len > 0) {
923 * PUP(out) = PUP(from);
924 * len--;
925 * }
926 * }
927 * } while (in < last && out < end);
928 */
929#undef nbytes_r
930#define in_r %esi
931
932 movl len_r, %ecx
933 rep movsb
934
935 movl in(%esp), in_r /* move in back to %esi, toss from */
936#if defined( USE_MMX )
937 movl lcode(%esp), %ebx /* move lcode back to %ebx, toss dist */
938#endif
939 jmp .L_while_test
940
941#undef len_r
942#undef from_r
943#undef dist_r
944
945.L_invalid_distance_code:
946 /* else {
947 * strm->msg = "invalid distance code";
948 * state->mode = BAD;
949 * }
950 */
951 movl $.L_invalid_distance_code_msg, %ecx
952 movl $INFLATE_MODE_BAD, %edx
953 jmp .L_update_stream_state
954
955.L_test_for_end_of_block:
956 /* else if (op & 32) {
957 * state->mode = TYPE;
958 * break;
959 * }
960 */
961 testb $32, %al
962 jz .L_invalid_literal_length_code /* if ((op & 32) == 0) */
963
964 movl $0, %ecx
965 movl $INFLATE_MODE_TYPE, %edx
966 jmp .L_update_stream_state
967
968.L_invalid_literal_length_code:
969 /* else {
970 * strm->msg = "invalid literal/length code";
971 * state->mode = BAD;
972 * }
973 */
974 movl $.L_invalid_literal_length_code_msg, %ecx
975 movl $INFLATE_MODE_BAD, %edx
976 jmp .L_update_stream_state
977
978.L_invalid_distance_too_far:
979 /* strm->msg = "invalid distance too far back";
980 * state->mode = BAD;
981 */
982 movl in(%esp), in_r /* from_r has in's reg, put in back */
983 movl $.L_invalid_distance_too_far_msg, %ecx
984 movl $INFLATE_MODE_BAD, %edx
985 jmp .L_update_stream_state
986
987.L_update_stream_state:
988 /* set strm->msg = %ecx, strm->state->mode = %edx */
989 movl strm_sp(%esp), %eax
990 testl %ecx, %ecx /* if (msg != NULL) */
991 jz .L_skip_msg
992 movl %ecx, msg_strm(%eax) /* strm->msg = msg */
993.L_skip_msg:
994 movl state_strm(%eax), %eax /* state = strm->state */
995 movl %edx, mode_state(%eax) /* state->mode = edx (BAD | TYPE) */
996
997.L_break_loop:
998
999#define strm_r %eax
1000#define state_r %edx
1001
1002 /* len = bits >> 3;
1003 * in -= len;
1004 * bits -= len << 3;
1005 * hold &= (1U << bits) - 1;
1006 * state->hold = hold;
1007 * state->bits = bits;
1008 * strm->next_in = in;
1009 * strm->next_out = out;
1010 */
1011 movl strm_sp(%esp), strm_r
1012 movl bitslong_r, %ecx
1013 movl state_strm(strm_r), state_r
1014 shrl $3, %ecx
1015 subl %ecx, in_r
1016 shll $3, %ecx
1017 subl %ecx, bitslong_r
1018 movl out_r, next_out_strm(strm_r)
1019 movl in_r, next_in_strm(strm_r)
1020 movl bitslong_r, bits_state(state_r)
1021
1022 movl bitslong_r, %ecx
1023 movl $1, %ebx /* overwrites bitslong_r, %bl */
1024 shll %cl, %ebx
1025 decl %ebx
1026
1027#undef bits_r
1028#undef bitslong_r
1029
1030#if ! defined( USE_MMX )
1031
1032 andl %ebx, hold_r
1033 movl hold_r, hold_state(state_r)
1034
1035#else /* USE_MMX */
1036
1037 psrlq used_mm, hold_mm /* hold_mm >>= last bit length */
1038 movd hold_mm, %ecx
1039 andl %ebx, %ecx
1040 movl %ecx, hold_state(state_r)
1041
1042#endif
1043
1044#define last_r %ebx
1045
1046 /* strm->avail_in = in < last ? 5 + (last - in) : 5 - (in - last) */
1047 movl last(%esp), last_r
1048 cmpl in_r, last_r
1049 jbe .L_last_is_smaller /* if (in >= last) */
1050
1051 subl in_r, last_r /* last -= in */
1052 addl $5, last_r /* last += 5 */
1053 movl last_r, avail_in_strm(strm_r)
1054 jmp .L_fixup_out
1055.L_last_is_smaller:
1056 subl last_r, in_r /* in -= last */
1057 negl in_r /* in = -in */
1058 addl $5, in_r /* in += 5 */
1059 movl in_r, avail_in_strm(strm_r)
1060
1061#undef last_r
1062#define end_r %ebx
1063
1064.L_fixup_out:
1065 /* strm->avail_out = out < end ? 257 + (end - out) : 257 - (out - end)*/
1066 movl end(%esp), end_r
1067 cmpl out_r, end_r
1068 jbe .L_end_is_smaller /* if (out >= end) */
1069
1070 subl out_r, end_r /* end -= out */
1071 addl $257, end_r /* end += 257 */
1072 movl end_r, avail_out_strm(strm_r)
1073 jmp .L_done
1074.L_end_is_smaller:
1075 subl end_r, out_r /* out -= end */
1076 negl out_r /* out = -out */
1077 addl $257, out_r /* out += 257 */
1078 movl out_r, avail_out_strm(strm_r)
1079
1080#undef end_r
1081
1082.L_done:
1083#if defined( USE_MMX )
1084 emms
1085#endif
1086 addl $local_var_size, %esp
1087 popf
1088 popl %ebx
1089 popl %ebp
1090 popl %esi
1091 popl %edi
1092 ret
1093
1094.type inflate_fast,@function
1095.size inflate_fast,.-inflate_fast
diff --git a/contrib/puff/Makefile b/contrib/puff/Makefile
new file mode 100644
index 0000000..b6b6940
--- /dev/null
+++ b/contrib/puff/Makefile
@@ -0,0 +1,8 @@
1puff: puff.c puff.h
2 cc -DTEST -o puff puff.c
3
4test: puff
5 puff zeros.raw
6
7clean:
8 rm -f puff puff.o
diff --git a/contrib/puff/README b/contrib/puff/README
new file mode 100644
index 0000000..59b3533
--- /dev/null
+++ b/contrib/puff/README
@@ -0,0 +1,63 @@
1Puff -- A Simple Inflate
23 Mar 2003
3Mark Adler
4madler@alumni.caltech.edu
5
6What this is --
7
8puff.c provides the routine puff() to decompress the deflate data format. It
9does so more slowly than zlib, but the code is about one-fifth the size of the
10inflate code in zlib, and written to be very easy to read.
11
12Why I wrote this --
13
14puff.c was written to document the deflate format unambiguously, by virtue of
15being working C code. It is meant to supplement RFC 1951, which formally
16describes the deflate format. I have received many questions on details of the
17deflate format, and I hope that reading this code will answer those questions.
18puff.c is heavily commented with details of the deflate format, especially
19those little nooks and cranies of the format that might not be obvious from a
20specification.
21
22puff.c may also be useful in applications where code size or memory usage is a
23very limited resource, and speed is not as important.
24
25How to use it --
26
27Well, most likely you should just be reading puff.c and using zlib for actual
28applications, but if you must ...
29
30Include puff.h in your code, which provides this prototype:
31
32int puff(unsigned char *dest, /* pointer to destination pointer */
33 unsigned long *destlen, /* amount of output space */
34 unsigned char *source, /* pointer to source data pointer */
35 unsigned long *sourcelen); /* amount of input available */
36
37Then you can call puff() to decompress a deflate stream that is in memory in
38its entirety at source, to a sufficiently sized block of memory for the
39decompressed data at dest. puff() is the only external symbol in puff.c The
40only C library functions that puff.c needs are setjmp() and longjmp(), which
41are used to simplify error checking in the code to improve readabilty. puff.c
42does no memory allocation, and uses less than 2K bytes off of the stack.
43
44If destlen is not enough space for the uncompressed data, then inflate will
45return an error without writing more than destlen bytes. Note that this means
46that in order to decompress the deflate data successfully, you need to know
47the size of the uncompressed data ahead of time.
48
49If needed, puff() can determine the size of the uncompressed data with no
50output space. This is done by passing dest equal to (unsigned char *)0. Then
51the initial value of *destlen is ignored and *destlen is set to the length of
52the uncompressed data. So if the size of the uncompressed data is not known,
53then two passes of puff() can be used--first to determine the size, and second
54to do the actual inflation after allocating the appropriate memory. Not
55pretty, but it works. (This is one of the reasons you should be using zlib.)
56
57The deflate format is self-terminating. If the deflate stream does not end
58in *sourcelen bytes, puff() will return an error without reading at or past
59endsource.
60
61On return, *sourcelen is updated to the amount of input data consumed, and
62*destlen is updated to the size of the uncompressed data. See the comments
63in puff.c for the possible return codes for puff().
diff --git a/contrib/puff/puff.c b/contrib/puff/puff.c
new file mode 100644
index 0000000..b6039dd
--- /dev/null
+++ b/contrib/puff/puff.c
@@ -0,0 +1,833 @@
1/*
2 * puff.c
3 * Copyright (C) 2002, 2003 Mark Adler
4 * For conditions of distribution and use, see copyright notice in puff.h
5 * version 1.7, 3 Mar 2003
6 *
7 * puff.c is a simple inflate written to be an unambiguous way to specify the
8 * deflate format. It is not written for speed but rather simplicity. As a
9 * side benefit, this code might actually be useful when small code is more
10 * important than speed, such as bootstrap applications. For typical deflate
11 * data, zlib's inflate() is about four times as fast as puff(). zlib's
12 * inflate compiles to around 20K on my machine, whereas puff.c compiles to
13 * around 4K on my machine (a PowerPC using GNU cc). If the faster decode()
14 * function here is used, then puff() is only twice as slow as zlib's
15 * inflate().
16 *
17 * All dynamically allocated memory comes from the stack. The stack required
18 * is less than 2K bytes. This code is compatible with 16-bit int's and
19 * assumes that long's are at least 32 bits. puff.c uses the short data type,
20 * assumed to be 16 bits, for arrays in order to to conserve memory. The code
21 * works whether integers are stored big endian or little endian.
22 *
23 * In the comments below are "Format notes" that describe the inflate process
24 * and document some of the less obvious aspects of the format. This source
25 * code is meant to supplement RFC 1951, which formally describes the deflate
26 * format:
27 *
28 * http://www.zlib.org/rfc-deflate.html
29 */
30
31/*
32 * Change history:
33 *
34 * 1.0 10 Feb 2002 - First version
35 * 1.1 17 Feb 2002 - Clarifications of some comments and notes
36 * - Update puff() dest and source pointers on negative
37 * errors to facilitate debugging deflators
38 * - Remove longest from struct huffman -- not needed
39 * - Simplify offs[] index in construct()
40 * - Add input size and checking, using longjmp() to
41 * maintain easy readability
42 * - Use short data type for large arrays
43 * - Use pointers instead of long to specify source and
44 * destination sizes to avoid arbitrary 4 GB limits
45 * 1.2 17 Mar 2002 - Add faster version of decode(), doubles speed (!),
46 * but leave simple version for readabilty
47 * - Make sure invalid distances detected if pointers
48 * are 16 bits
49 * - Fix fixed codes table error
50 * - Provide a scanning mode for determining size of
51 * uncompressed data
52 * 1.3 20 Mar 2002 - Go back to lengths for puff() parameters [Jean-loup]
53 * - Add a puff.h file for the interface
54 * - Add braces in puff() for else do [Jean-loup]
55 * - Use indexes instead of pointers for readability
56 * 1.4 31 Mar 2002 - Simplify construct() code set check
57 * - Fix some comments
58 * - Add FIXLCODES #define
59 * 1.5 6 Apr 2002 - Minor comment fixes
60 * 1.6 7 Aug 2002 - Minor format changes
61 * 1.7 3 Mar 2002 - Added test code for distribution
62 * - Added zlib-like license
63 */
64
65#include <setjmp.h> /* for setjmp(), longjmp(), and jmp_buf */
66#include "puff.h" /* prototype for puff() */
67
68#define local static /* for local function definitions */
69#define NIL ((unsigned char *)0) /* for no output option */
70
71/*
72 * Maximums for allocations and loops. It is not useful to change these --
73 * they are fixed by the deflate format.
74 */
75#define MAXBITS 15 /* maximum bits in a code */
76#define MAXLCODES 286 /* maximum number of literal/length codes */
77#define MAXDCODES 30 /* maximum number of distance codes */
78#define MAXCODES (MAXLCODES+MAXDCODES) /* maximum codes lengths to read */
79#define FIXLCODES 288 /* number of fixed literal/length codes */
80
81/* input and output state */
82struct state {
83 /* output state */
84 unsigned char *out; /* output buffer */
85 unsigned long outlen; /* available space at out */
86 unsigned long outcnt; /* bytes written to out so far */
87
88 /* input state */
89 unsigned char *in; /* input buffer */
90 unsigned long inlen; /* available input at in */
91 unsigned long incnt; /* bytes read so far */
92 int bitbuf; /* bit buffer */
93 int bitcnt; /* number of bits in bit buffer */
94
95 /* input limit error return state for bits() and decode() */
96 jmp_buf env;
97};
98
99/*
100 * Return need bits from the input stream. This always leaves less than
101 * eight bits in the buffer. bits() works properly for need == 0.
102 *
103 * Format notes:
104 *
105 * - Bits are stored in bytes from the least significant bit to the most
106 * significant bit. Therefore bits are dropped from the bottom of the bit
107 * buffer, using shift right, and new bytes are appended to the top of the
108 * bit buffer, using shift left.
109 */
110local int bits(struct state *s, int need)
111{
112 long val; /* bit accumulator (can use up to 20 bits) */
113
114 /* load at least need bits into val */
115 val = s->bitbuf;
116 while (s->bitcnt < need) {
117 if (s->incnt == s->inlen) longjmp(s->env, 1); /* out of input */
118 val |= (long)(s->in[s->incnt++]) << s->bitcnt; /* load eight bits */
119 s->bitcnt += 8;
120 }
121
122 /* drop need bits and update buffer, always zero to seven bits left */
123 s->bitbuf = (int)(val >> need);
124 s->bitcnt -= need;
125
126 /* return need bits, zeroing the bits above that */
127 return (int)(val & ((1L << need) - 1));
128}
129
130/*
131 * Process a stored block.
132 *
133 * Format notes:
134 *
135 * - After the two-bit stored block type (00), the stored block length and
136 * stored bytes are byte-aligned for fast copying. Therefore any leftover
137 * bits in the byte that has the last bit of the type, as many as seven, are
138 * discarded. The value of the discarded bits are not defined and should not
139 * be checked against any expectation.
140 *
141 * - The second inverted copy of the stored block length does not have to be
142 * checked, but it's probably a good idea to do so anyway.
143 *
144 * - A stored block can have zero length. This is sometimes used to byte-align
145 * subsets of the compressed data for random access or partial recovery.
146 */
147local int stored(struct state *s)
148{
149 unsigned len; /* length of stored block */
150
151 /* discard leftover bits from current byte (assumes s->bitcnt < 8) */
152 s->bitbuf = 0;
153 s->bitcnt = 0;
154
155 /* get length and check against its one's complement */
156 if (s->incnt + 4 > s->inlen) return 2; /* not enough input */
157 len = s->in[s->incnt++];
158 len |= s->in[s->incnt++] << 8;
159 if (s->in[s->incnt++] != (~len & 0xff) ||
160 s->in[s->incnt++] != ((~len >> 8) & 0xff))
161 return -2; /* didn't match complement! */
162
163 /* copy len bytes from in to out */
164 if (s->incnt + len > s->inlen) return 2; /* not enough input */
165 if (s->out != NIL) {
166 if (s->outcnt + len > s->outlen)
167 return 1; /* not enough output space */
168 while (len--)
169 s->out[s->outcnt++] = s->in[s->incnt++];
170 }
171 else { /* just scanning */
172 s->outcnt += len;
173 s->incnt += len;
174 }
175
176 /* done with a valid stored block */
177 return 0;
178}
179
180/*
181 * Huffman code decoding tables. count[1..MAXBITS] is the number of symbols of
182 * each length, which for a canonical code are stepped through in order.
183 * symbol[] are the symbol values in canonical order, where the number of
184 * entries is the sum of the counts in count[]. The decoding process can be
185 * seen in the function decode() below.
186 */
187struct huffman {
188 short *count; /* number of symbols of each length */
189 short *symbol; /* canonically ordered symbols */
190};
191
192/*
193 * Decode a code from the stream s using huffman table h. Return the symbol or
194 * a negative value if there is an error. If all of the lengths are zero, i.e.
195 * an empty code, or if the code is incomplete and an invalid code is received,
196 * then -9 is returned after reading MAXBITS bits.
197 *
198 * Format notes:
199 *
200 * - The codes as stored in the compressed data are bit-reversed relative to
201 * a simple integer ordering of codes of the same lengths. Hence below the
202 * bits are pulled from the compressed data one at a time and used to
203 * build the code value reversed from what is in the stream in order to
204 * permit simple integer comparisons for decoding. A table-based decoding
205 * scheme (as used in zlib) does not need to do this reversal.
206 *
207 * - The first code for the shortest length is all zeros. Subsequent codes of
208 * the same length are simply integer increments of the previous code. When
209 * moving up a length, a zero bit is appended to the code. For a complete
210 * code, the last code of the longest length will be all ones.
211 *
212 * - Incomplete codes are handled by this decoder, since they are permitted
213 * in the deflate format. See the format notes for fixed() and dynamic().
214 */
215#ifdef SLOW
216local int decode(struct state *s, struct huffman *h)
217{
218 int len; /* current number of bits in code */
219 int code; /* len bits being decoded */
220 int first; /* first code of length len */
221 int count; /* number of codes of length len */
222 int index; /* index of first code of length len in symbol table */
223
224 code = first = index = 0;
225 for (len = 1; len <= MAXBITS; len++) {
226 code |= bits(s, 1); /* get next bit */
227 count = h->count[len];
228 if (code < first + count) /* if length len, return symbol */
229 return h->symbol[index + (code - first)];
230 index += count; /* else update for next length */
231 first += count;
232 first <<= 1;
233 code <<= 1;
234 }
235 return -9; /* ran out of codes */
236}
237
238/*
239 * A faster version of decode() for real applications of this code. It's not
240 * as readable, but it makes puff() twice as fast. And it only makes the code
241 * a few percent larger.
242 */
243#else /* !SLOW */
244local int decode(struct state *s, struct huffman *h)
245{
246 int len; /* current number of bits in code */
247 int code; /* len bits being decoded */
248 int first; /* first code of length len */
249 int count; /* number of codes of length len */
250 int index; /* index of first code of length len in symbol table */
251 int bitbuf; /* bits from stream */
252 int left; /* bits left in next or left to process */
253 short *next; /* next number of codes */
254
255 bitbuf = s->bitbuf;
256 left = s->bitcnt;
257 code = first = index = 0;
258 len = 1;
259 next = h->count + 1;
260 while (1) {
261 while (left--) {
262 code |= bitbuf & 1;
263 bitbuf >>= 1;
264 count = *next++;
265 if (code < first + count) { /* if length len, return symbol */
266 s->bitbuf = bitbuf;
267 s->bitcnt = (s->bitcnt - len) & 7;
268 return h->symbol[index + (code - first)];
269 }
270 index += count; /* else update for next length */
271 first += count;
272 first <<= 1;
273 code <<= 1;
274 len++;
275 }
276 left = (MAXBITS+1) - len;
277 if (left == 0) break;
278 if (s->incnt == s->inlen) longjmp(s->env, 1); /* out of input */
279 bitbuf = s->in[s->incnt++];
280 if (left > 8) left = 8;
281 }
282 return -9; /* ran out of codes */
283}
284#endif /* SLOW */
285
286/*
287 * Given the list of code lengths length[0..n-1] representing a canonical
288 * Huffman code for n symbols, construct the tables required to decode those
289 * codes. Those tables are the number of codes of each length, and the symbols
290 * sorted by length, retaining their original order within each length. The
291 * return value is zero for a complete code set, negative for an over-
292 * subscribed code set, and positive for an incomplete code set. The tables
293 * can be used if the return value is zero or positive, but they cannot be used
294 * if the return value is negative. If the return value is zero, it is not
295 * possible for decode() using that table to return an error--any stream of
296 * enough bits will resolve to a symbol. If the return value is positive, then
297 * it is possible for decode() using that table to return an error for received
298 * codes past the end of the incomplete lengths.
299 *
300 * Not used by decode(), but used for error checking, h->count[0] is the number
301 * of the n symbols not in the code. So n - h->count[0] is the number of
302 * codes. This is useful for checking for incomplete codes that have more than
303 * one symbol, which is an error in a dynamic block.
304 *
305 * Assumption: for all i in 0..n-1, 0 <= length[i] <= MAXBITS
306 * This is assured by the construction of the length arrays in dynamic() and
307 * fixed() and is not verified by construct().
308 *
309 * Format notes:
310 *
311 * - Permitted and expected examples of incomplete codes are one of the fixed
312 * codes and any code with a single symbol which in deflate is coded as one
313 * bit instead of zero bits. See the format notes for fixed() and dynamic().
314 *
315 * - Within a given code length, the symbols are kept in ascending order for
316 * the code bits definition.
317 */
318local int construct(struct huffman *h, short *length, int n)
319{
320 int symbol; /* current symbol when stepping through length[] */
321 int len; /* current length when stepping through h->count[] */
322 int left; /* number of possible codes left of current length */
323 short offs[MAXBITS+1]; /* offsets in symbol table for each length */
324
325 /* count number of codes of each length */
326 for (len = 0; len <= MAXBITS; len++)
327 h->count[len] = 0;
328 for (symbol = 0; symbol < n; symbol++)
329 (h->count[length[symbol]])++; /* assumes lengths are within bounds */
330 if (h->count[0] == n) /* no codes! */
331 return 0; /* complete, but decode() will fail */
332
333 /* check for an over-subscribed or incomplete set of lengths */
334 left = 1; /* one possible code of zero length */
335 for (len = 1; len <= MAXBITS; len++) {
336 left <<= 1; /* one more bit, double codes left */
337 left -= h->count[len]; /* deduct count from possible codes */
338 if (left < 0) return left; /* over-subscribed--return negative */
339 } /* left > 0 means incomplete */
340
341 /* generate offsets into symbol table for each length for sorting */
342 offs[1] = 0;
343 for (len = 1; len < MAXBITS; len++)
344 offs[len + 1] = offs[len] + h->count[len];
345
346 /*
347 * put symbols in table sorted by length, by symbol order within each
348 * length
349 */
350 for (symbol = 0; symbol < n; symbol++)
351 if (length[symbol] != 0)
352 h->symbol[offs[length[symbol]]++] = symbol;
353
354 /* return zero for complete set, positive for incomplete set */
355 return left;
356}
357
358/*
359 * Decode literal/length and distance codes until an end-of-block code.
360 *
361 * Format notes:
362 *
363 * - Compressed data that is after the block type if fixed or after the code
364 * description if dynamic is a combination of literals and length/distance
365 * pairs terminated by and end-of-block code. Literals are simply Huffman
366 * coded bytes. A length/distance pair is a coded length followed by a
367 * coded distance to represent a string that occurs earlier in the
368 * uncompressed data that occurs again at the current location.
369 *
370 * - Literals, lengths, and the end-of-block code are combined into a single
371 * code of up to 286 symbols. They are 256 literals (0..255), 29 length
372 * symbols (257..285), and the end-of-block symbol (256).
373 *
374 * - There are 256 possible lengths (3..258), and so 29 symbols are not enough
375 * to represent all of those. Lengths 3..10 and 258 are in fact represented
376 * by just a length symbol. Lengths 11..257 are represented as a symbol and
377 * some number of extra bits that are added as an integer to the base length
378 * of the length symbol. The number of extra bits is determined by the base
379 * length symbol. These are in the static arrays below, lens[] for the base
380 * lengths and lext[] for the corresponding number of extra bits.
381 *
382 * - The reason that 258 gets its own symbol is that the longest length is used
383 * often in highly redundant files. Note that 258 can also be coded as the
384 * base value 227 plus the maximum extra value of 31. While a good deflate
385 * should never do this, it is not an error, and should be decoded properly.
386 *
387 * - If a length is decoded, including its extra bits if any, then it is
388 * followed a distance code. There are up to 30 distance symbols. Again
389 * there are many more possible distances (1..32768), so extra bits are added
390 * to a base value represented by the symbol. The distances 1..4 get their
391 * own symbol, but the rest require extra bits. The base distances and
392 * corresponding number of extra bits are below in the static arrays dist[]
393 * and dext[].
394 *
395 * - Literal bytes are simply written to the output. A length/distance pair is
396 * an instruction to copy previously uncompressed bytes to the output. The
397 * copy is from distance bytes back in the output stream, copying for length
398 * bytes.
399 *
400 * - Distances pointing before the beginning of the output data are not
401 * permitted.
402 *
403 * - Overlapped copies, where the length is greater than the distance, are
404 * allowed and common. For example, a distance of one and a length of 258
405 * simply copies the last byte 258 times. A distance of four and a length of
406 * twelve copies the last four bytes three times. A simple forward copy
407 * ignoring whether the length is greater than the distance or not implements
408 * this correctly. You should not use memcpy() since its behavior is not
409 * defined for overlapped arrays. You should not use memmove() or bcopy()
410 * since though their behavior -is- defined for overlapping arrays, it is
411 * defined to do the wrong thing in this case.
412 */
413local int codes(struct state *s,
414 struct huffman *lencode,
415 struct huffman *distcode)
416{
417 int symbol; /* decoded symbol */
418 int len; /* length for copy */
419 unsigned dist; /* distance for copy */
420 static const short lens[29] = { /* Size base for length codes 257..285 */
421 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
422 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
423 static const short lext[29] = { /* Extra bits for length codes 257..285 */
424 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
425 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
426 static const short dists[30] = { /* Offset base for distance codes 0..29 */
427 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
428 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
429 8193, 12289, 16385, 24577};
430 static const short dext[30] = { /* Extra bits for distance codes 0..29 */
431 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
432 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
433 12, 12, 13, 13};
434
435 /* decode literals and length/distance pairs */
436 do {
437 symbol = decode(s, lencode);
438 if (symbol < 0) return symbol; /* invalid symbol */
439 if (symbol < 256) { /* literal: symbol is the byte */
440 /* write out the literal */
441 if (s->out != NIL) {
442 if (s->outcnt == s->outlen) return 1;
443 s->out[s->outcnt] = symbol;
444 }
445 s->outcnt++;
446 }
447 else if (symbol > 256) { /* length */
448 /* get and compute length */
449 symbol -= 257;
450 if (symbol >= 29) return -9; /* invalid fixed code */
451 len = lens[symbol] + bits(s, lext[symbol]);
452
453 /* get and check distance */
454 symbol = decode(s, distcode);
455 if (symbol < 0) return symbol; /* invalid symbol */
456 dist = dists[symbol] + bits(s, dext[symbol]);
457 if (dist > s->outcnt)
458 return -10; /* distance too far back */
459
460 /* copy length bytes from distance bytes back */
461 if (s->out != NIL) {
462 if (s->outcnt + len > s->outlen) return 1;
463 while (len--) {
464 s->out[s->outcnt] = s->out[s->outcnt - dist];
465 s->outcnt++;
466 }
467 }
468 else
469 s->outcnt += len;
470 }
471 } while (symbol != 256); /* end of block symbol */
472
473 /* done with a valid fixed or dynamic block */
474 return 0;
475}
476
477/*
478 * Process a fixed codes block.
479 *
480 * Format notes:
481 *
482 * - This block type can be useful for compressing small amounts of data for
483 * which the size of the code descriptions in a dynamic block exceeds the
484 * benefit of custom codes for that block. For fixed codes, no bits are
485 * spent on code descriptions. Instead the code lengths for literal/length
486 * codes and distance codes are fixed. The specific lengths for each symbol
487 * can be seen in the "for" loops below.
488 *
489 * - The literal/length code is complete, but has two symbols that are invalid
490 * and should result in an error if received. This cannot be implemented
491 * simply as an incomplete code since those two symbols are in the "middle"
492 * of the code. They are eight bits long and the longest literal/length\
493 * code is nine bits. Therefore the code must be constructed with those
494 * symbols, and the invalid symbols must be detected after decoding.
495 *
496 * - The fixed distance codes also have two invalid symbols that should result
497 * in an error if received. Since all of the distance codes are the same
498 * length, this can be implemented as an incomplete code. Then the invalid
499 * codes are detected while decoding.
500 */
501local int fixed(struct state *s)
502{
503 static int virgin = 1;
504 static short lencnt[MAXBITS+1], lensym[FIXLCODES];
505 static short distcnt[MAXBITS+1], distsym[MAXDCODES];
506 static struct huffman lencode = {lencnt, lensym};
507 static struct huffman distcode = {distcnt, distsym};
508
509 /* build fixed huffman tables if first call (may not be thread safe) */
510 if (virgin) {
511 int symbol;
512 short lengths[FIXLCODES];
513
514 /* literal/length table */
515 for (symbol = 0; symbol < 144; symbol++)
516 lengths[symbol] = 8;
517 for (; symbol < 256; symbol++)
518 lengths[symbol] = 9;
519 for (; symbol < 280; symbol++)
520 lengths[symbol] = 7;
521 for (; symbol < FIXLCODES; symbol++)
522 lengths[symbol] = 8;
523 construct(&lencode, lengths, FIXLCODES);
524
525 /* distance table */
526 for (symbol = 0; symbol < MAXDCODES; symbol++)
527 lengths[symbol] = 5;
528 construct(&distcode, lengths, MAXDCODES);
529
530 /* do this just once */
531 virgin = 0;
532 }
533
534 /* decode data until end-of-block code */
535 return codes(s, &lencode, &distcode);
536}
537
538/*
539 * Process a dynamic codes block.
540 *
541 * Format notes:
542 *
543 * - A dynamic block starts with a description of the literal/length and
544 * distance codes for that block. New dynamic blocks allow the compressor to
545 * rapidly adapt to changing data with new codes optimized for that data.
546 *
547 * - The codes used by the deflate format are "canonical", which means that
548 * the actual bits of the codes are generated in an unambiguous way simply
549 * from the number of bits in each code. Therefore the code descriptions
550 * are simply a list of code lengths for each symbol.
551 *
552 * - The code lengths are stored in order for the symbols, so lengths are
553 * provided for each of the literal/length symbols, and for each of the
554 * distance symbols.
555 *
556 * - If a symbol is not used in the block, this is represented by a zero as
557 * as the code length. This does not mean a zero-length code, but rather
558 * that no code should be created for this symbol. There is no way in the
559 * deflate format to represent a zero-length code.
560 *
561 * - The maximum number of bits in a code is 15, so the possible lengths for
562 * any code are 1..15.
563 *
564 * - The fact that a length of zero is not permitted for a code has an
565 * interesting consequence. Normally if only one symbol is used for a given
566 * code, then in fact that code could be represented with zero bits. However
567 * in deflate, that code has to be at least one bit. So for example, if
568 * only a single distance base symbol appears in a block, then it will be
569 * represented by a single code of length one, in particular one 0 bit. This
570 * is an incomplete code, since if a 1 bit is received, it has no meaning,
571 * and should result in an error. So incomplete distance codes of one symbol
572 * should be permitted, and the receipt of invalid codes should be handled.
573 *
574 * - It is also possible to have a single literal/length code, but that code
575 * must be the end-of-block code, since every dynamic block has one. This
576 * is not the most efficient way to create an empty block (an empty fixed
577 * block is fewer bits), but it is allowed by the format. So incomplete
578 * literal/length codes of one symbol should also be permitted.
579 *
580 * - The list of up to 286 length/literal lengths and up to 30 distance lengths
581 * are themselves compressed using Huffman codes and run-length encoding. In
582 * the list of code lengths, a 0 symbol means no code, a 1..15 symbol means
583 * that length, and the symbols 16, 17, and 18 are run-length instructions.
584 * Each of 16, 17, and 18 are follwed by extra bits to define the length of
585 * the run. 16 copies the last length 3 to 6 times. 17 represents 3 to 10
586 * zero lengths, and 18 represents 11 to 138 zero lengths. Unused symbols
587 * are common, hence the special coding for zero lengths.
588 *
589 * - The symbols for 0..18 are Huffman coded, and so that code must be
590 * described first. This is simply a sequence of up to 19 three-bit values
591 * representing no code (0) or the code length for that symbol (1..7).
592 *
593 * - A dynamic block starts with three fixed-size counts from which is computed
594 * the number of literal/length code lengths, the number of distance code
595 * lengths, and the number of code length code lengths (ok, you come up with
596 * a better name!) in the code descriptions. For the literal/length and
597 * distance codes, lengths after those provided are considered zero, i.e. no
598 * code. The code length code lengths are received in a permuted order (see
599 * the order[] array below) to make a short code length code length list more
600 * likely. As it turns out, very short and very long codes are less likely
601 * to be seen in a dynamic code description, hence what may appear initially
602 * to be a peculiar ordering.
603 *
604 * - Given the number of literal/length code lengths (nlen) and distance code
605 * lengths (ndist), then they are treated as one long list of nlen + ndist
606 * code lengths. Therefore run-length coding can and often does cross the
607 * boundary between the two sets of lengths.
608 *
609 * - So to summarize, the code description at the start of a dynamic block is
610 * three counts for the number of code lengths for the literal/length codes,
611 * the distance codes, and the code length codes. This is followed by the
612 * code length code lengths, three bits each. This is used to construct the
613 * code length code which is used to read the remainder of the lengths. Then
614 * the literal/length code lengths and distance lengths are read as a single
615 * set of lengths using the code length codes. Codes are constructed from
616 * the resulting two sets of lengths, and then finally you can start
617 * decoding actual compressed data in the block.
618 *
619 * - For reference, a "typical" size for the code description in a dynamic
620 * block is around 80 bytes.
621 */
622local int dynamic(struct state *s)
623{
624 int nlen, ndist, ncode; /* number of lengths in descriptor */
625 int index; /* index of lengths[] */
626 int err; /* construct() return value */
627 short lengths[MAXCODES]; /* descriptor code lengths */
628 short lencnt[MAXBITS+1], lensym[MAXLCODES]; /* lencode memory */
629 short distcnt[MAXBITS+1], distsym[MAXDCODES]; /* distcode memory */
630 struct huffman lencode = {lencnt, lensym}; /* length code */
631 struct huffman distcode = {distcnt, distsym}; /* distance code */
632 static const short order[19] = /* permutation of code length codes */
633 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
634
635 /* get number of lengths in each table, check lengths */
636 nlen = bits(s, 5) + 257;
637 ndist = bits(s, 5) + 1;
638 ncode = bits(s, 4) + 4;
639 if (nlen > MAXLCODES || ndist > MAXDCODES)
640 return -3; /* bad counts */
641
642 /* read code length code lengths (really), missing lengths are zero */
643 for (index = 0; index < ncode; index++)
644 lengths[order[index]] = bits(s, 3);
645 for (; index < 19; index++)
646 lengths[order[index]] = 0;
647
648 /* build huffman table for code lengths codes (use lencode temporarily) */
649 err = construct(&lencode, lengths, 19);
650 if (err != 0) return -4; /* require complete code set here */
651
652 /* read length/literal and distance code length tables */
653 index = 0;
654 while (index < nlen + ndist) {
655 int symbol; /* decoded value */
656 int len; /* last length to repeat */
657
658 symbol = decode(s, &lencode);
659 if (symbol < 16) /* length in 0..15 */
660 lengths[index++] = symbol;
661 else { /* repeat instruction */
662 len = 0; /* assume repeating zeros */
663 if (symbol == 16) { /* repeat last length 3..6 times */
664 if (index == 0) return -5; /* no last length! */
665 len = lengths[index - 1]; /* last length */
666 symbol = 3 + bits(s, 2);
667 }
668 else if (symbol == 17) /* repeat zero 3..10 times */
669 symbol = 3 + bits(s, 3);
670 else /* == 18, repeat zero 11..138 times */
671 symbol = 11 + bits(s, 7);
672 if (index + symbol > nlen + ndist)
673 return -6; /* too many lengths! */
674 while (symbol--) /* repeat last or zero symbol times */
675 lengths[index++] = len;
676 }
677 }
678
679 /* build huffman table for literal/length codes */
680 err = construct(&lencode, lengths, nlen);
681 if (err < 0 || (err > 0 && nlen - lencode.count[0] != 1))
682 return -7; /* only allow incomplete codes if just one code */
683
684 /* build huffman table for distance codes */
685 err = construct(&distcode, lengths + nlen, ndist);
686 if (err < 0 || (err > 0 && ndist - distcode.count[0] != 1))
687 return -8; /* only allow incomplete codes if just one code */
688
689 /* decode data until end-of-block code */
690 return codes(s, &lencode, &distcode);
691}
692
693/*
694 * Inflate source to dest. On return, destlen and sourcelen are updated to the
695 * size of the uncompressed data and the size of the deflate data respectively.
696 * On success, the return value of puff() is zero. If there is an error in the
697 * source data, i.e. it is not in the deflate format, then a negative value is
698 * returned. If there is not enough input available or there is not enough
699 * output space, then a positive error is returned. In that case, destlen and
700 * sourcelen are not updated to facilitate retrying from the beginning with the
701 * provision of more input data or more output space. In the case of invalid
702 * inflate data (a negative error), the dest and source pointers are updated to
703 * facilitate the debugging of deflators.
704 *
705 * puff() also has a mode to determine the size of the uncompressed output with
706 * no output written. For this dest must be (unsigned char *)0. In this case,
707 * the input value of *destlen is ignored, and on return *destlen is set to the
708 * size of the uncompressed output.
709 *
710 * The return codes are:
711 *
712 * 2: available inflate data did not terminate
713 * 1: output space exhausted before completing inflate
714 * 0: successful inflate
715 * -1: invalid block type (type == 3)
716 * -2: stored block length did not match one's complement
717 * -3: dynamic block code description: too many length or distance codes
718 * -4: dynamic block code description: code lengths codes incomplete
719 * -5: dynamic block code description: repeat lengths with no first length
720 * -6: dynamic block code description: repeat more than specified lengths
721 * -7: dynamic block code description: invalid literal/length code lengths
722 * -8: dynamic block code description: invalid distance code lengths
723 * -9: invalid literal/length or distance code in fixed or dynamic block
724 * -10: distance is too far back in fixed or dynamic block
725 *
726 * Format notes:
727 *
728 * - Three bits are read for each block to determine the kind of block and
729 * whether or not it is the last block. Then the block is decoded and the
730 * process repeated if it was not the last block.
731 *
732 * - The leftover bits in the last byte of the deflate data after the last
733 * block (if it was a fixed or dynamic block) are undefined and have no
734 * expected values to check.
735 */
736int puff(unsigned char *dest, /* pointer to destination pointer */
737 unsigned long *destlen, /* amount of output space */
738 unsigned char *source, /* pointer to source data pointer */
739 unsigned long *sourcelen) /* amount of input available */
740{
741 struct state s; /* input/output state */
742 int last, type; /* block information */
743 int err; /* return value */
744
745 /* initialize output state */
746 s.out = dest;
747 s.outlen = *destlen; /* ignored if dest is NIL */
748 s.outcnt = 0;
749
750 /* initialize input state */
751 s.in = source;
752 s.inlen = *sourcelen;
753 s.incnt = 0;
754 s.bitbuf = 0;
755 s.bitcnt = 0;
756
757 /* return if bits() or decode() tries to read past available input */
758 if (setjmp(s.env) != 0) /* if came back here via longjmp() */
759 err = 2; /* then skip do-loop, return error */
760 else {
761 /* process blocks until last block or error */
762 do {
763 last = bits(&s, 1); /* one if last block */
764 type = bits(&s, 2); /* block type 0..3 */
765 err = type == 0 ? stored(&s) :
766 (type == 1 ? fixed(&s) :
767 (type == 2 ? dynamic(&s) :
768 -1)); /* type == 3, invalid */
769 if (err != 0) break; /* return with error */
770 } while (!last);
771 }
772
773 /* update the lengths and return */
774 if (err <= 0) {
775 *destlen = s.outcnt;
776 *sourcelen = s.incnt;
777 }
778 return err;
779}
780
781#ifdef TEST
782/* Example of how to use puff() */
783#include <stdio.h>
784#include <stdlib.h>
785#include <sys/types.h>
786#include <sys/stat.h>
787
788local unsigned char *yank(char *name, unsigned long *len)
789{
790 unsigned long size;
791 unsigned char *buf;
792 FILE *in;
793 struct stat s;
794
795 *len = 0;
796 if (stat(name, &s)) return NULL;
797 if ((s.st_mode & S_IFMT) != S_IFREG) return NULL;
798 size = (unsigned long)(s.st_size);
799 if (size == 0 || (off_t)size != s.st_size) return NULL;
800 in = fopen(name, "r");
801 if (in == NULL) return NULL;
802 buf = malloc(size);
803 if (buf != NULL && fread(buf, 1, size, in) != size) {
804 free(buf);
805 buf = NULL;
806 }
807 fclose(in);
808 *len = size;
809 return buf;
810}
811
812int main(int argc, char **argv)
813{
814 int ret;
815 unsigned char *source;
816 unsigned long len, sourcelen, destlen;
817
818 if (argc < 2) return 2;
819 source = yank(argv[1], &len);
820 if (source == NULL) return 2;
821 sourcelen = len;
822 ret = puff(NIL, &destlen, source, &sourcelen);
823 if (ret)
824 printf("puff() failed with return code %d\n", ret);
825 else {
826 printf("puff() succeeded uncompressing %lu bytes\n", destlen);
827 if (sourcelen < len) printf("%lu compressed bytes unused\n",
828 len - sourcelen);
829 }
830 free(source);
831 return ret;
832}
833#endif
diff --git a/contrib/puff/puff.h b/contrib/puff/puff.h
new file mode 100644
index 0000000..41ea7e1
--- /dev/null
+++ b/contrib/puff/puff.h
@@ -0,0 +1,31 @@
1/* puff.h
2 Copyright (C) 2002, 2003 Mark Adler, all rights reserved
3 version 1.7, 3 Mar 2002
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the author be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20
21 Mark Adler madler@alumni.caltech.edu
22 */
23
24
25/*
26 * See puff.c for purpose and usage.
27 */
28int puff(unsigned char *dest, /* pointer to destination pointer */
29 unsigned long *destlen, /* amount of output space */
30 unsigned char *source, /* pointer to source data pointer */
31 unsigned long *sourcelen); /* amount of input available */
diff --git a/contrib/puff/zeros.raw b/contrib/puff/zeros.raw
new file mode 100644
index 0000000..637b7be
--- /dev/null
+++ b/contrib/puff/zeros.raw
Binary files differ
diff --git a/crc32.c b/crc32.c
index 60deca2..d0a9f5e 100644
--- a/crc32.c
+++ b/crc32.c
@@ -1,22 +1,68 @@
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-2002 Mark Adler 2 * Copyright (C) 1995-2003 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 *
5 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
6 * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
7 * tables for updating the shift register in one step with three exclusive-ors
8 * instead of four steps with four exclusive-ors. This results about a 50%
9 * increase in speed on a Power PC using gcc -O3.
4 */ 10 */
5 11
6/* @(#) $Id$ */ 12/* @(#) $Id$ */
7 13
14#ifdef MAKECRCH
15# include <stdio.h>
16# ifndef DYNAMIC_CRC_TABLE
17# define DYNAMIC_CRC_TABLE
18# endif /* !DYNAMIC_CRC_TABLE */
19#endif /* MAKECRCH */
20
8#include "zlib.h" 21#include "zlib.h"
9 22
10#define local static 23#define local static
11 24
25/* Find a four-byte integer type for crc32_little() and crc32_big(). */
26#ifndef NOBYFOUR
27# ifdef __STDC__ /* need ANSI C limits.h to determine sizes */
28# include <limits.h>
29# define BYFOUR
30# if (UINT_MAX == 4294967295)
31 typedef unsigned int u4;
32# elif (ULONG_MAX == 4294967295)
33 typedef unsigned long u4;
34# elif (USHRT_MAX == 4294967295)
35 typedef unsigned short u4;
36# else
37# undef BYFOUR /* can't find a four-byte integer type! */
38# endif
39# endif /* __STDC__ */
40#endif /* !NOBYFOUR */
41
42/* Definitions for doing the crc four data bytes at a time. */
43#ifdef BYFOUR
44# define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
45 (((w)&0xff00)<<8)+(((w)&0xff)<<24))
46 local unsigned long crc32_little OF((unsigned long,
47 const unsigned char FAR *, unsigned));
48 local unsigned long crc32_big OF((unsigned long,
49 const unsigned char FAR *, unsigned));
50# define TBLS 8
51#else
52# define TBLS 1
53#endif /* BYFOUR */
54
12#ifdef DYNAMIC_CRC_TABLE 55#ifdef DYNAMIC_CRC_TABLE
13 56
14local int crc_table_empty = 1; 57local int crc_table_empty = 1;
15local uLongf crc_table[256]; 58local unsigned long FAR crc_table[TBLS][256];
16local void make_crc_table OF((void)); 59local void make_crc_table OF((void));
60#ifdef MAKECRCH
61 local void write_table OF((FILE *, const unsigned long FAR *));
62#endif /* MAKECRCH */
17 63
18/* 64/*
19 Generate a table for a byte-wise 32-bit CRC calculation on the polynomial: 65 Generate tables 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. 66 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.
21 67
22 Polynomials over GF(2) are represented in binary, one bit per coefficient, 68 Polynomials over GF(2) are represented in binary, one bit per coefficient,
@@ -35,128 +81,228 @@ local void make_crc_table OF((void));
35 out is a one). We start with the highest power (least significant bit) of 81 out is a one). We start with the highest power (least significant bit) of
36 q and repeat for all eight bits of q. 82 q and repeat for all eight bits of q.
37 83
38 The table is simply the CRC of all possible eight bit values. This is all 84 The first table is simply the CRC of all possible eight bit values. This is
39 the information needed to generate CRC's on data a byte at a time for all 85 all the information needed to generate CRCs on data a byte at a time for all
40 combinations of CRC register values and incoming bytes. 86 combinations of CRC register values and incoming bytes. The remaining tables
87 allow for word-at-a-time CRC calculation for both big-endian and little-
88 endian machines, where a word is four bytes.
41*/ 89*/
42local void make_crc_table() 90local void make_crc_table()
43{ 91{
44 uLong c; 92 unsigned long c;
45 int n, k; 93 int n, k;
46 uLong poly; /* polynomial exclusive-or pattern */ 94 unsigned long poly; /* polynomial exclusive-or pattern */
47 /* terms of polynomial defining this crc (except x^32): */ 95 /* terms of polynomial defining this crc (except x^32): */
48 static const Byte p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; 96 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
49 97
50 /* make exclusive-or pattern from polynomial (0xedb88320L) */ 98 /* make exclusive-or pattern from polynomial (0xedb88320L) */
51 poly = 0L; 99 poly = 0UL;
52 for (n = 0; n < sizeof(p)/sizeof(Byte); n++) 100 for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
53 poly |= 1L << (31 - p[n]); 101 poly |= 1UL << (31 - p[n]);
54 102
55 for (n = 0; n < 256; n++) 103 /* generate a crc for every 8-bit value */
56 { 104 for (n = 0; n < 256; n++) {
57 c = (uLong)n; 105 c = (unsigned long)n;
58 for (k = 0; k < 8; k++) 106 for (k = 0; k < 8; k++)
59 c = c & 1 ? poly ^ (c >> 1) : c >> 1; 107 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
60 crc_table[n] = c; 108 crc_table[0][n] = c;
61 } 109 }
110
111#ifdef BYFOUR
112 /* generate crc for each value followed by one, two, and three zeros, and
113 then the byte reversal of those as well as the first table */
114 for (n = 0; n < 256; n++) {
115 c = crc_table[0][n];
116 crc_table[4][n] = REV(c);
117 for (k = 1; k < 4; k++) {
118 c = crc_table[0][c & 0xff] ^ (c >> 8);
119 crc_table[k][n] = c;
120 crc_table[k + 4][n] = REV(c);
121 }
122 }
123#endif /* BYFOUR */
124
62 crc_table_empty = 0; 125 crc_table_empty = 0;
126
127#ifdef MAKECRCH
128 /* write out CRC tables to crc32.h */
129 {
130 FILE *out;
131
132 out = fopen("crc32.h", "w");
133 if (out == NULL) return;
134 fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
135 fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
136 fprintf(out, "local const unsigned long FAR ");
137 fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
138 write_table(out, crc_table[0]);
139# ifdef BYFOUR
140 fprintf(out, "#ifdef BYFOUR\n");
141 for (k = 1; k < 8; k++) {
142 fprintf(out, " },\n {\n");
143 write_table(out, crc_table[k]);
144 }
145 fprintf(out, "#endif\n");
146# endif /* BYFOUR */
147 fprintf(out, " }\n};\n");
148 fclose(out);
149 }
150#endif /* MAKECRCH */
63} 151}
64#else 152
153#ifdef MAKECRCH
154local void write_table(out, table)
155 FILE *out;
156 const unsigned long FAR *table;
157{
158 int n;
159
160 for (n = 0; n < 256; n++)
161 fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n],
162 n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
163}
164#endif /* MAKECRCH */
165
166#else /* !DYNAMIC_CRC_TABLE */
65/* ======================================================================== 167/* ========================================================================
66 * Table of CRC-32's of all single-byte values (made by make_crc_table) 168 * Tables of CRC-32s of all single-byte values, made by make_crc_table().
67 */ 169 */
68local const uLongf crc_table[256] = { 170#include "crc32.h"
69 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, 171#endif /* DYNAMIC_CRC_TABLE */
70 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
71 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
72 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
73 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
74 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
75 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
76 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
77 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
78 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
79 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
80 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
81 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
82 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
83 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
84 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
85 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
86 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
87 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
88 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
89 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
90 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
91 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
92 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
93 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
94 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
95 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
96 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
97 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
98 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
99 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
100 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
101 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
102 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
103 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
104 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
105 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
106 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
107 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
108 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
109 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
110 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
111 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
112 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
113 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
114 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
115 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
116 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
117 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
118 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
119 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
120 0x2d02ef8dL
121};
122#endif
123 172
124/* ========================================================================= 173/* =========================================================================
125 * This function can be used by asm versions of crc32() 174 * This function can be used by asm versions of crc32()
126 */ 175 */
127const uLongf * ZEXPORT get_crc_table() 176const unsigned long FAR * ZEXPORT get_crc_table()
128{ 177{
129#ifdef DYNAMIC_CRC_TABLE 178#ifdef DYNAMIC_CRC_TABLE
130 if (crc_table_empty) make_crc_table(); 179 if (crc_table_empty) make_crc_table();
131#endif 180#endif /* DYNAMIC_CRC_TABLE */
132 return (const uLongf *)crc_table; 181 return (const unsigned long FAR *)crc_table;
133} 182}
134 183
135/* ========================================================================= */ 184/* ========================================================================= */
136#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8); 185#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
137#define DO2(buf) DO1(buf); DO1(buf); 186#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
138#define DO4(buf) DO2(buf); DO2(buf);
139#define DO8(buf) DO4(buf); DO4(buf);
140 187
141/* ========================================================================= */ 188/* ========================================================================= */
142uLong ZEXPORT crc32(crc, buf, len) 189unsigned long ZEXPORT crc32(crc, buf, len)
143 uLong crc; 190 unsigned long crc;
144 const Bytef *buf; 191 const unsigned char FAR *buf;
145 uInt len; 192 unsigned len;
146{ 193{
147 if (buf == Z_NULL) return 0L; 194 if (buf == Z_NULL) return 0UL;
195
148#ifdef DYNAMIC_CRC_TABLE 196#ifdef DYNAMIC_CRC_TABLE
149 if (crc_table_empty) 197 if (crc_table_empty)
150 make_crc_table(); 198 make_crc_table();
151#endif 199#endif /* DYNAMIC_CRC_TABLE */
152 crc = crc ^ 0xffffffffL; 200
153 while (len >= 8) 201#ifdef BYFOUR
154 { 202 {
155 DO8(buf); 203 u4 endian;
156 len -= 8; 204
205 endian = 1;
206 if (*((unsigned char *)(&endian)))
207 return crc32_little(crc, buf, len);
208 else
209 return crc32_big(crc, buf, len);
210 }
211#else /* !BYFOUR */
212 crc = crc ^ 0xffffffffUL;
213 while (len >= 8) {
214 DO8;
215 len -= 8;
216 }
217 if (len) do {
218 DO1;
219 } while (--len);
220 return crc ^ 0xffffffffUL;
221#endif /* BYFOUR */
222}
223
224#ifdef BYFOUR
225
226/* ========================================================================= */
227#define DOLIT4 c ^= *buf4++; \
228 c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
229 crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
230#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
231
232/* ========================================================================= */
233local unsigned long crc32_little(crc, buf, len)
234 unsigned long crc;
235 const unsigned char FAR *buf;
236 unsigned len;
237{
238 register u4 c;
239 register const u4 FAR *buf4;
240
241 c = (u4)crc;
242 c = ~c;
243 while (len && ((int)buf & 3)) {
244 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
245 len--;
246 }
247
248 buf4 = (const u4 FAR *)buf;
249 while (len >= 32) {
250 DOLIT32;
251 len -= 32;
252 }
253 while (len >= 4) {
254 DOLIT4;
255 len -= 4;
157 } 256 }
257 buf = (const unsigned char FAR *)buf4;
258
158 if (len) do { 259 if (len) do {
159 DO1(buf); 260 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
160 } while (--len); 261 } while (--len);
161 return crc ^ 0xffffffffL; 262 c = ~c;
263 return (unsigned long)c;
162} 264}
265
266/* ========================================================================= */
267#define DOBIG4 c ^= *++buf4; \
268 c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
269 crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
270#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
271
272/* ========================================================================= */
273local unsigned long crc32_big(crc, buf, len)
274 unsigned long crc;
275 const unsigned char FAR *buf;
276 unsigned len;
277{
278 register u4 c;
279 register const u4 FAR *buf4;
280
281 c = REV((u4)crc);
282 c = ~c;
283 while (len && ((int)buf & 3)) {
284 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
285 len--;
286 }
287
288 buf4 = (const u4 FAR *)buf;
289 buf4--;
290 while (len >= 32) {
291 DOBIG32;
292 len -= 32;
293 }
294 while (len >= 4) {
295 DOBIG4;
296 len -= 4;
297 }
298 buf4++;
299 buf = (const unsigned char FAR *)buf4;
300
301 if (len) do {
302 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
303 } while (--len);
304 c = ~c;
305 return (unsigned long)(REV(c));
306}
307
308#endif /* BYFOUR */
diff --git a/crc32.h b/crc32.h
new file mode 100644
index 0000000..8053b61
--- /dev/null
+++ b/crc32.h
@@ -0,0 +1,441 @@
1/* crc32.h -- tables for rapid CRC calculation
2 * Generated automatically by crc32.c
3 */
4
5local const unsigned long FAR crc_table[TBLS][256] =
6{
7 {
8 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL,
9 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL,
10 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL,
11 0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL,
12 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL,
13 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL,
14 0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL,
15 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL,
16 0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL,
17 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL,
18 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL,
19 0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL,
20 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL,
21 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL,
22 0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL,
23 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL,
24 0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL,
25 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL,
26 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL,
27 0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL,
28 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL,
29 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL,
30 0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL,
31 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL,
32 0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL,
33 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL,
34 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL,
35 0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL,
36 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL,
37 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL,
38 0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL,
39 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL,
40 0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL,
41 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL,
42 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL,
43 0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL,
44 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL,
45 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL,
46 0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL,
47 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL,
48 0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL,
49 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL,
50 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL,
51 0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL,
52 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL,
53 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL,
54 0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL,
55 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL,
56 0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL,
57 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
58 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
59 0x2d02ef8dUL
60#ifdef BYFOUR
61 },
62 {
63 0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL,
64 0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL,
65 0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL,
66 0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL,
67 0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL,
68 0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL,
69 0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL,
70 0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL,
71 0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL,
72 0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL,
73 0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL,
74 0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL,
75 0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL,
76 0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL,
77 0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL,
78 0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL,
79 0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL,
80 0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL,
81 0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL,
82 0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL,
83 0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL,
84 0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL,
85 0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL,
86 0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL,
87 0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL,
88 0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL,
89 0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL,
90 0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL,
91 0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL,
92 0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL,
93 0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL,
94 0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL,
95 0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL,
96 0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL,
97 0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL,
98 0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL,
99 0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL,
100 0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL,
101 0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL,
102 0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL,
103 0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL,
104 0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL,
105 0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL,
106 0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL,
107 0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL,
108 0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL,
109 0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL,
110 0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL,
111 0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL,
112 0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL,
113 0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL,
114 0x9324fd72UL
115 },
116 {
117 0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL,
118 0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL,
119 0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL,
120 0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL,
121 0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL,
122 0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL,
123 0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL,
124 0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL,
125 0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL,
126 0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL,
127 0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL,
128 0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL,
129 0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL,
130 0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL,
131 0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL,
132 0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL,
133 0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL,
134 0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL,
135 0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL,
136 0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL,
137 0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL,
138 0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL,
139 0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL,
140 0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL,
141 0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL,
142 0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL,
143 0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL,
144 0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL,
145 0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL,
146 0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL,
147 0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL,
148 0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL,
149 0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL,
150 0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL,
151 0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL,
152 0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL,
153 0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL,
154 0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL,
155 0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL,
156 0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL,
157 0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL,
158 0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL,
159 0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL,
160 0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL,
161 0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL,
162 0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL,
163 0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL,
164 0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL,
165 0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL,
166 0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL,
167 0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL,
168 0xbe9834edUL
169 },
170 {
171 0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL,
172 0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL,
173 0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL,
174 0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL,
175 0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL,
176 0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL,
177 0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL,
178 0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL,
179 0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL,
180 0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL,
181 0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL,
182 0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL,
183 0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL,
184 0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL,
185 0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL,
186 0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL,
187 0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL,
188 0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL,
189 0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL,
190 0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL,
191 0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL,
192 0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL,
193 0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL,
194 0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL,
195 0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL,
196 0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL,
197 0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL,
198 0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL,
199 0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL,
200 0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL,
201 0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL,
202 0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL,
203 0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL,
204 0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL,
205 0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL,
206 0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL,
207 0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL,
208 0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL,
209 0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL,
210 0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL,
211 0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL,
212 0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL,
213 0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL,
214 0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL,
215 0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL,
216 0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL,
217 0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL,
218 0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL,
219 0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL,
220 0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL,
221 0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL,
222 0xde0506f1UL
223 },
224 {
225 0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL,
226 0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL,
227 0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL,
228 0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL,
229 0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL,
230 0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL,
231 0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL,
232 0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL,
233 0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL,
234 0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL,
235 0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL,
236 0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL,
237 0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL,
238 0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL,
239 0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL,
240 0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL,
241 0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL,
242 0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL,
243 0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL,
244 0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL,
245 0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL,
246 0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL,
247 0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL,
248 0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL,
249 0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL,
250 0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL,
251 0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL,
252 0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL,
253 0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL,
254 0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL,
255 0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL,
256 0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL,
257 0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL,
258 0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL,
259 0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL,
260 0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL,
261 0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL,
262 0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL,
263 0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL,
264 0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL,
265 0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL,
266 0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL,
267 0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL,
268 0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL,
269 0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL,
270 0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL,
271 0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL,
272 0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL,
273 0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL,
274 0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL,
275 0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL,
276 0x8def022dUL
277 },
278 {
279 0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL,
280 0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL,
281 0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL,
282 0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL,
283 0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL,
284 0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL,
285 0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL,
286 0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL,
287 0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL,
288 0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL,
289 0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL,
290 0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL,
291 0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL,
292 0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL,
293 0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL,
294 0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL,
295 0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL,
296 0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL,
297 0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL,
298 0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL,
299 0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL,
300 0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL,
301 0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL,
302 0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL,
303 0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL,
304 0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL,
305 0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL,
306 0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL,
307 0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL,
308 0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL,
309 0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL,
310 0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL,
311 0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL,
312 0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL,
313 0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL,
314 0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL,
315 0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL,
316 0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL,
317 0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL,
318 0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL,
319 0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL,
320 0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL,
321 0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL,
322 0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL,
323 0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL,
324 0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL,
325 0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL,
326 0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL,
327 0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL,
328 0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL,
329 0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL,
330 0x72fd2493UL
331 },
332 {
333 0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL,
334 0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL,
335 0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL,
336 0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL,
337 0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL,
338 0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL,
339 0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL,
340 0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL,
341 0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL,
342 0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL,
343 0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL,
344 0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL,
345 0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL,
346 0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL,
347 0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL,
348 0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL,
349 0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL,
350 0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL,
351 0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL,
352 0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL,
353 0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL,
354 0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL,
355 0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL,
356 0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL,
357 0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL,
358 0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL,
359 0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL,
360 0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL,
361 0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL,
362 0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL,
363 0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL,
364 0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL,
365 0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL,
366 0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL,
367 0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL,
368 0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL,
369 0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL,
370 0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL,
371 0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL,
372 0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL,
373 0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL,
374 0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL,
375 0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL,
376 0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL,
377 0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL,
378 0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL,
379 0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL,
380 0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL,
381 0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL,
382 0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL,
383 0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL,
384 0xed3498beUL
385 },
386 {
387 0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL,
388 0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL,
389 0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL,
390 0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL,
391 0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL,
392 0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL,
393 0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL,
394 0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL,
395 0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL,
396 0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL,
397 0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL,
398 0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL,
399 0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL,
400 0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL,
401 0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL,
402 0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL,
403 0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL,
404 0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL,
405 0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL,
406 0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL,
407 0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL,
408 0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL,
409 0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL,
410 0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL,
411 0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL,
412 0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL,
413 0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL,
414 0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL,
415 0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL,
416 0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL,
417 0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL,
418 0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL,
419 0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL,
420 0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL,
421 0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL,
422 0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL,
423 0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL,
424 0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL,
425 0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL,
426 0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL,
427 0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL,
428 0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL,
429 0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL,
430 0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL,
431 0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL,
432 0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL,
433 0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL,
434 0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL,
435 0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL,
436 0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL,
437 0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL,
438 0xf10605deUL
439#endif
440 }
441};
diff --git a/deflate.c b/deflate.c
index 16ebdad..d13627e 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-2002 Jean-loup Gailly. 2 * Copyright (C) 1995-2003 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
@@ -52,7 +52,7 @@
52#include "deflate.h" 52#include "deflate.h"
53 53
54const char deflate_copyright[] = 54const char deflate_copyright[] =
55 " deflate 1.1.4 Copyright 1995-2002 Jean-loup Gailly "; 55 " deflate 1.2.0 Copyright 1995-2003 Jean-loup Gailly ";
56/* 56/*
57 If you use the zlib library in a product, an acknowledgment is welcome 57 If you use the zlib library in a product, an acknowledgment is welcome
58 in the documentation of your product. If for some reason you cannot 58 in the documentation of your product. If for some reason you cannot
@@ -212,7 +212,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
212{ 212{
213 deflate_state *s; 213 deflate_state *s;
214 int noheader = 0; 214 int noheader = 0;
215 static const char* my_version = ZLIB_VERSION; 215 static const char my_version[] = ZLIB_VERSION;
216 216
217 ushf *overlay; 217 ushf *overlay;
218 /* We overlay pending_buf and d_buf+l_buf. This works since the average 218 /* We overlay pending_buf and d_buf+l_buf. This works since the average
@@ -273,6 +273,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
273 273
274 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 274 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
275 s->pending_buf == Z_NULL) { 275 s->pending_buf == Z_NULL) {
276 s->status = FINISH_STATE;
276 strm->msg = (char*)ERR_MSG(Z_MEM_ERROR); 277 strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
277 deflateEnd (strm); 278 deflateEnd (strm);
278 return Z_MEM_ERROR; 279 return Z_MEM_ERROR;
@@ -299,10 +300,12 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
299 IPos hash_head = 0; 300 IPos hash_head = 0;
300 301
301 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL || 302 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
302 strm->state->status != INIT_STATE) return Z_STREAM_ERROR; 303 (!strm->state->noheader && strm->state->status != INIT_STATE))
304 return Z_STREAM_ERROR;
303 305
304 s = strm->state; 306 s = strm->state;
305 strm->adler = adler32(strm->adler, dictionary, dictLength); 307 if (!s->noheader)
308 strm->adler = adler32(strm->adler, dictionary, dictLength);
306 309
307 if (length < MIN_MATCH) return Z_OK; 310 if (length < MIN_MATCH) return Z_OK;
308 if (length > MAX_DIST(s)) { 311 if (length > MAX_DIST(s)) {
@@ -395,6 +398,47 @@ int ZEXPORT deflateParams(strm, level, strategy)
395} 398}
396 399
397/* ========================================================================= 400/* =========================================================================
401 * For the default windowBits of 15 and memLevel of 8, this function returns
402 * a close to exact, as well as small, upper bound on the compressed size.
403 * They are coded as constants here for a reason--if the #define's are
404 * changed, then this function needs to be changed as well. The return
405 * value for 15 and 8 only works for those exact settings.
406 *
407 * For any setting other than those defaults for windowBits and memLevel,
408 * the value returned is a conservative worst case for the maximum expansion
409 * resulting from using fixed blocks instead of stored blocks, which deflate
410 * can emit on compressed data for some combinations of the parameters.
411 *
412 * This function could be more sophisticated to provide closer upper bounds
413 * for every combination of windowBits and memLevel, as well as noheader.
414 * But even the conservative upper bound of about 14% expansion does not
415 * seem onerous for output buffer allocation.
416 */
417uLong ZEXPORT deflateBound(strm, sourceLen)
418 z_streamp strm;
419 uLong sourceLen;
420{
421 deflate_state *s;
422 uLong destLen;
423
424 /* conservative upper bound */
425 destLen = sourceLen +
426 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
427
428 /* if can't get parameters, return conservative bound */
429 if (strm == Z_NULL || strm->state == Z_NULL)
430 return destLen;
431
432 /* if not default parameters, return conservative bound */
433 s = strm->state;
434 if (s->w_bits != 15 || s->hash_bits != 8 + 7)
435 return destLen;
436
437 /* default settings: return tight bound for that case */
438 return compressBound(sourceLen);
439}
440
441/* =========================================================================
398 * Put a short in the pending buffer. The 16-bit value is put in MSB order. 442 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
399 * IN assertion: the stream state is correct and there is enough room in 443 * IN assertion: the stream state is correct and there is enough room in
400 * pending_buf. 444 * pending_buf.
diff --git a/example.c b/example.c
index e7e3673..8820e56 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-2002 Jean-loup Gailly. 2 * Copyright (C) 1995-2003 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
@@ -551,6 +551,9 @@ int main(argc, argv)
551 test_dict_deflate(compr, comprLen); 551 test_dict_deflate(compr, comprLen);
552 test_dict_inflate(compr, comprLen, uncompr, uncomprLen); 552 test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
553 553
554 free(compr);
555 free(uncompr);
556
554 exit(0); 557 exit(0);
555 return 0; /* to avoid warning */ 558 return 0; /* to avoid warning */
556} 559}
diff --git a/gzio.c b/gzio.c
index 09e0a20..3337353 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-2002 Jean-loup Gailly. 2 * Copyright (C) 1995-2003 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 * Compile this file with -DNO_DEFLATE to avoid the compression code. 5 * Compile this file with -DNO_DEFLATE to avoid the compression code.
@@ -24,10 +24,15 @@ struct internal_state {int dummy;}; /* for buggy compilers */
24# define Z_PRINTF_BUFSIZE 4096 24# define Z_PRINTF_BUFSIZE 4096
25#endif 25#endif
26 26
27#ifndef STDC
28extern voidp malloc OF((uInt size));
29extern void free OF((voidpf ptr));
30#endif
31
27#define ALLOC(size) malloc(size) 32#define ALLOC(size) malloc(size)
28#define TRYFREE(p) {if (p) free(p);} 33#define TRYFREE(p) {if (p) free(p);}
29 34
30static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */ 35static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
31 36
32/* gzip flag byte */ 37/* gzip flag byte */
33#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ 38#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
@@ -268,19 +273,33 @@ local void check_header(s)
268 uInt len; 273 uInt len;
269 int c; 274 int c;
270 275
271 /* Check the gzip magic header */ 276 /* Assure two bytes in the buffer so we can peek ahead -- handle case
272 for (len = 0; len < 2; len++) { 277 where first byte of header is at the end of the buffer after the last
273 c = get_byte(s); 278 gzip segment */
274 if (c != gz_magic[len]) { 279 len = s->stream.avail_in;
275 if (len != 0) s->stream.avail_in++, s->stream.next_in--; 280 if (len < 2) {
276 if (c != EOF) { 281 if (len) s->inbuf[0] = s->stream.next_in[0];
277 s->stream.avail_in++, s->stream.next_in--; 282 errno = 0;
278 s->transparent = 1; 283 len = fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
279 } 284 if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
280 s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END; 285 s->stream.avail_in += len;
281 return; 286 s->stream.next_in = s->inbuf;
282 } 287 if (s->stream.avail_in < 2) {
288 s->transparent = s->stream.avail_in;
289 return;
290 }
283 } 291 }
292
293 /* Peek ahead to check the gzip magic header */
294 if (s->stream.next_in[0] != gz_magic[0] ||
295 s->stream.next_in[1] != gz_magic[1]) {
296 s->transparent = 1;
297 return;
298 }
299 s->stream.avail_in -= 2;
300 s->stream.next_in += 2;
301
302 /* Check the rest of the gzip header */
284 method = get_byte(s); 303 method = get_byte(s);
285 flags = get_byte(s); 304 flags = get_byte(s);
286 if (method != Z_DEFLATED || (flags & RESERVED) != 0) { 305 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
@@ -485,7 +504,7 @@ char * ZEXPORT gzgets(file, buf, len)
485*/ 504*/
486int ZEXPORT gzwrite (file, buf, len) 505int ZEXPORT gzwrite (file, buf, len)
487 gzFile file; 506 gzFile file;
488 const voidp buf; 507 voidpc buf;
489 unsigned len; 508 unsigned len;
490{ 509{
491 gz_stream *s = (gz_stream*)file; 510 gz_stream *s = (gz_stream*)file;
@@ -529,14 +548,32 @@ int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
529 int len; 548 int len;
530 549
531 va_start(va, format); 550 va_start(va, format);
532#ifdef HAS_vsnprintf 551#ifdef NO_vsnprintf
533 (void)vsnprintf(buf, sizeof(buf), format, va); 552# ifdef HAS_vsprintf_void
534#else
535 (void)vsprintf(buf, format, va); 553 (void)vsprintf(buf, format, va);
536#endif
537 va_end(va); 554 va_end(va);
538 len = strlen(buf); /* some *sprintf don't return the nb of bytes written */ 555 len = strlen(buf); /* some *sprintf don't return the nb of bytes written */
539 if (len <= 0) return 0; 556 if (len <= 0) return 0;
557# else
558 len = vsprintf(buf, format, va);
559 va_end(va);
560 if (len <= 0 || len >= sizeof(buf))
561 return 0;
562# endif
563#else
564# ifdef HAS_vsnprintf_void
565 (void)vsnprintf(buf, sizeof(buf), format, va);
566 va_end(va);
567 len = strlen(buf);
568 if (len <= 0)
569 return 0;
570# else
571 len = vsnprintf(buf, sizeof(buf), format, va);
572 va_end(va);
573 if (len <= 0 || len >= sizeof(buf))
574 return 0;
575# endif
576#endif
540 577
541 return gzwrite(file, buf, (unsigned)len); 578 return gzwrite(file, buf, (unsigned)len);
542} 579}
@@ -552,15 +589,32 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
552 char buf[Z_PRINTF_BUFSIZE]; 589 char buf[Z_PRINTF_BUFSIZE];
553 int len; 590 int len;
554 591
555#ifdef HAS_snprintf 592#ifdef NO_snprintf
556 snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8, 593# ifdef HAS_sprintf_void
557 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
558#else
559 sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8, 594 sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
560 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); 595 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
561#endif
562 len = strlen(buf); /* old sprintf doesn't return the nb of bytes written */ 596 len = strlen(buf); /* old sprintf doesn't return the nb of bytes written */
563 if (len <= 0) return 0; 597 if (len <= 0) return 0;
598# else
599 len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
600 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
601 if (len <= 0 || len >= sizeof(buf))
602 return 0;
603# endif
604#else
605# ifdef HAS_snprintf_void
606 snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
607 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
608 len = strlen(buf);
609 if (len <= 0)
610 return 0;
611# else
612 len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
613 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
614 if (len <= 0 || len >= sizeof(buf))
615 return 0;
616# endif
617#endif
564 618
565 return gzwrite(file, buf, len); 619 return gzwrite(file, buf, len);
566} 620}
@@ -681,6 +735,7 @@ z_off_t ZEXPORT gzseek (file, offset, whence)
681 /* At this point, offset is the number of zero bytes to write. */ 735 /* At this point, offset is the number of zero bytes to write. */
682 if (s->inbuf == Z_NULL) { 736 if (s->inbuf == Z_NULL) {
683 s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */ 737 s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
738 if (s->inbuf == Z_NULL) return -1L;
684 zmemzero(s->inbuf, Z_BUFSIZE); 739 zmemzero(s->inbuf, Z_BUFSIZE);
685 } 740 }
686 while (offset > 0) { 741 while (offset > 0) {
@@ -723,6 +778,7 @@ z_off_t ZEXPORT gzseek (file, offset, whence)
723 778
724 if (offset != 0 && s->outbuf == Z_NULL) { 779 if (offset != 0 && s->outbuf == Z_NULL) {
725 s->outbuf = (Byte*)ALLOC(Z_BUFSIZE); 780 s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
781 if (s->outbuf == Z_NULL) return -1L;
726 } 782 }
727 while (offset > 0) { 783 while (offset > 0) {
728 int size = Z_BUFSIZE; 784 int size = Z_BUFSIZE;
@@ -862,12 +918,13 @@ const char* ZEXPORT gzerror (file, errnum)
862 *errnum = s->z_err; 918 *errnum = s->z_err;
863 if (*errnum == Z_OK) return (const char*)""; 919 if (*errnum == Z_OK) return (const char*)"";
864 920
865 m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg); 921 m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
866 922
867 if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err); 923 if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
868 924
869 TRYFREE(s->msg); 925 TRYFREE(s->msg);
870 s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3); 926 s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
927 if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
871 strcpy(s->msg, s->path); 928 strcpy(s->msg, s->path);
872 strcat(s->msg, ": "); 929 strcat(s->msg, ": ");
873 strcat(s->msg, m); 930 strcat(s->msg, m);
diff --git a/infback.c b/infback.c
new file mode 100644
index 0000000..46090ad
--- /dev/null
+++ b/infback.c
@@ -0,0 +1,615 @@
1/* infback.c -- inflate using a call-back interface
2 * Copyright (C) 1995-2003 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/*
7 This code is largely copied from inflate.c. Normally either infback.o or
8 inflate.o would be linked into an application--not both. The interface
9 with inffast.c is retained so that optimized assembler-coded versions of
10 inflate_fast() can be used with either inflate.c or infback.c.
11 */
12
13#include "zutil.h"
14#include "inftrees.h"
15#include "inflate.h"
16#include "inffast.h"
17
18/* function prototypes */
19local void fixedtables OF((struct inflate_state FAR *state));
20
21/*
22 strm provides memory allocation functions in zalloc and zfree, or
23 Z_NULL to use the library memory allocation functions.
24
25 windowBits is in the range 8..15, and window is a user-supplied
26 window and output buffer that is 2**windowBits bytes.
27 */
28int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
29z_stream FAR *strm;
30int windowBits;
31unsigned char FAR *window;
32const char *version;
33int stream_size;
34{
35 struct inflate_state FAR *state;
36
37 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
38 stream_size != (int)(sizeof(z_stream)))
39 return Z_VERSION_ERROR;
40 if (strm == Z_NULL || window == Z_NULL ||
41 windowBits < 8 || windowBits > 15)
42 return Z_STREAM_ERROR;
43 strm->msg = Z_NULL; /* in case we return an error */
44 if (strm->zalloc == Z_NULL) {
45 strm->zalloc = zcalloc;
46 strm->opaque = (voidpf)0;
47 }
48 if (strm->zfree == Z_NULL) strm->zfree = zcfree;
49 state = (struct inflate_state FAR *)ZALLOC(strm, 1,
50 sizeof(struct inflate_state));
51 if (state == Z_NULL) return Z_MEM_ERROR;
52 Tracev((stderr, "inflate: allocated\n"));
53 strm->state = (voidpf)state;
54 state->wbits = windowBits;
55 state->wsize = 1U << windowBits;
56 state->window = window;
57 state->write = 0;
58 return Z_OK;
59}
60
61/*
62 Return state with length and distance decoding tables and index sizes set to
63 fixed code decoding. Normally this returns fixed tables from inffixed.h.
64 If BUILDFIXED is defined, then instead this routine builds the tables the
65 first time it's called, and returns those tables the first time and
66 thereafter. This reduces the size of the code by about 2K bytes, in
67 exchange for a little execution time. However, BUILDFIXED should not be
68 used for threaded applications, since the rewriting of the tables and virgin
69 may not be thread-safe.
70 */
71local void fixedtables(state)
72struct inflate_state FAR *state;
73{
74#ifdef BUILDFIXED
75 static int virgin = 1;
76 static code *lenfix, *distfix;
77 static code fixed[544];
78
79 /* build fixed huffman tables if first call (may not be thread safe) */
80 if (virgin) {
81 unsigned sym, bits;
82 static code *next;
83
84 /* literal/length table */
85 sym = 0;
86 while (sym < 144) state->lens[sym++] = 8;
87 while (sym < 256) state->lens[sym++] = 9;
88 while (sym < 280) state->lens[sym++] = 7;
89 while (sym < 288) state->lens[sym++] = 8;
90 next = fixed;
91 lenfix = next;
92 bits = 9;
93 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
94
95 /* distance table */
96 sym = 0;
97 while (sym < 32) state->lens[sym++] = 5;
98 distfix = next;
99 bits = 5;
100 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
101
102 /* do this just once */
103 virgin = 0;
104 }
105#else /* !BUILDFIXED */
106# include "inffixed.h"
107#endif /* BUILDFIXED */
108 state->lencode = lenfix;
109 state->lenbits = 9;
110 state->distcode = distfix;
111 state->distbits = 5;
112}
113
114/* Macros for inflateBack(): */
115
116/* Load returned state from inflate_fast() */
117#define LOAD() \
118 do { \
119 put = strm->next_out; \
120 left = strm->avail_out; \
121 next = strm->next_in; \
122 have = strm->avail_in; \
123 hold = state->hold; \
124 bits = state->bits; \
125 } while (0)
126
127/* Set state from registers for inflate_fast() */
128#define RESTORE() \
129 do { \
130 strm->next_out = put; \
131 strm->avail_out = left; \
132 strm->next_in = next; \
133 strm->avail_in = have; \
134 state->hold = hold; \
135 state->bits = bits; \
136 } while (0)
137
138/* Clear the input bit accumulator */
139#define INITBITS() \
140 do { \
141 hold = 0; \
142 bits = 0; \
143 } while (0)
144
145/* Assure that some input is available. If input is requested, but denied,
146 then return a Z_BUF_ERROR from inflateBack(). */
147#define PULL() \
148 do { \
149 if (have == 0) { \
150 have = in(in_desc, &next); \
151 if (have == 0) { \
152 next = Z_NULL; \
153 ret = Z_BUF_ERROR; \
154 goto leave; \
155 } \
156 } \
157 } while (0)
158
159/* Get a byte of input into the bit accumulator, or return from inflateBack()
160 with an error if there is no input available. */
161#define PULLBYTE() \
162 do { \
163 PULL(); \
164 have--; \
165 hold += (unsigned long)(*next++) << bits; \
166 bits += 8; \
167 } while (0)
168
169/* Assure that there are at least n bits in the bit accumulator. If there is
170 not enough available input to do that, then return from inflateBack() with
171 an error. */
172#define NEEDBITS(n) \
173 do { \
174 while (bits < (unsigned)(n)) \
175 PULLBYTE(); \
176 } while (0)
177
178/* Return the low n bits of the bit accumulator (n < 16) */
179#define BITS(n) \
180 ((unsigned)hold & ((1U << (n)) - 1))
181
182/* Remove n bits from the bit accumulator */
183#define DROPBITS(n) \
184 do { \
185 hold >>= (n); \
186 bits -= (unsigned)(n); \
187 } while (0)
188
189/* Remove zero to seven bits as needed to go to a byte boundary */
190#define BYTEBITS() \
191 do { \
192 hold >>= bits & 7; \
193 bits -= bits & 7; \
194 } while (0)
195
196/* Assure that some output space is available, by writing out the window
197 if it's full. If the write fails, return from inflateBack() with a
198 Z_BUF_ERROR. */
199#define ROOM() \
200 do { \
201 if (left == 0) { \
202 put = state->window; \
203 left = state->wsize; \
204 if (out(out_desc, put, left)) { \
205 ret = Z_BUF_ERROR; \
206 goto leave; \
207 } \
208 } \
209 } while (0)
210
211/*
212 strm provides the memory allocation functions and window buffer on input,
213 and provides information on the unused input on return. For Z_DATA_ERROR
214 returns, strm will also provide an error message.
215
216 in() and out() are the call-back input and output functions. When
217 inflateBack() needs more input, it calls in(). When inflateBack() has
218 filled the window with output, or when it completes with data in the
219 window, it called out() to write out the data. The application must not
220 change the provided input until in() is called again or inflateBack()
221 returns. The application must not change the window/output buffer until
222 inflateBack() returns.
223
224 in() and out() are called with a descriptor parameter provided in the
225 inflateBack() call. This parameter can be a structure that provides the
226 information required to do the read or write, as well as accumulated
227 information on the input and output such as totals and check values.
228
229 in() should return zero on failure. out() should return non-zero on
230 failure. If either in() or out() fails, than inflateBack() returns a
231 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
232 was in() or out() that caused in the error. Otherwise, inflateBack()
233 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
234 error, or Z_MEM_ERROR if it could not allocate memory for the state.
235 inflateBack() can also return Z_STREAM_ERROR if the input parameters
236 are not correct, i.e. strm is Z_NULL or the state was not initialized.
237 */
238int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
239z_stream FAR *strm;
240in_func in;
241void FAR *in_desc;
242out_func out;
243void FAR *out_desc;
244{
245 struct inflate_state FAR *state;
246 unsigned char *next, *put; /* next input and output */
247 unsigned have, left; /* available input and output */
248 unsigned long hold; /* bit buffer */
249 unsigned bits; /* bits in bit buffer */
250 unsigned copy; /* number of stored or match bytes to copy */
251 unsigned char *from; /* where to copy match bytes from */
252 code this; /* current decoding table entry */
253 code last; /* parent table entry */
254 unsigned len; /* length to copy for repeats, bits to drop */
255 int ret; /* return code */
256 static const unsigned short order[19] = /* permutation of code lengths */
257 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
258
259 /* Check that the strm exists and that the state was initialized */
260 if (strm == Z_NULL || strm->state == Z_NULL)
261 return Z_STREAM_ERROR;
262 state = (struct inflate_state FAR *)strm->state;
263
264 /* Reset the state */
265 strm->msg = Z_NULL;
266 state->mode = TYPE;
267 state->last = 0;
268 next = strm->next_in;
269 have = next != Z_NULL ? strm->avail_in : 0;
270 hold = 0;
271 bits = 0;
272 put = state->window;
273 left = state->wsize;
274
275 /* Inflate until end of block marked as last */
276 for (;;)
277 switch (state->mode) {
278 case TYPE:
279 /* determine and dispatch block type */
280 if (state->last) {
281 BYTEBITS();
282 state->mode = DONE;
283 break;
284 }
285 NEEDBITS(3);
286 state->last = BITS(1);
287 DROPBITS(1);
288 switch (BITS(2)) {
289 case 0: /* stored block */
290 Tracev((stderr, "inflate: stored block%s\n",
291 state->last ? " (last)" : ""));
292 state->mode = STORED;
293 break;
294 case 1: /* fixed block */
295 fixedtables(state);
296 Tracev((stderr, "inflate: fixed codes block%s\n",
297 state->last ? " (last)" : ""));
298 state->mode = LEN; /* decode codes */
299 break;
300 case 2: /* dynamic block */
301 Tracev((stderr, "inflate: dynamic codes block%s\n",
302 state->last ? " (last)" : ""));
303 state->mode = TABLE;
304 break;
305 case 3:
306 strm->msg = (char *)"invalid block type";
307 state->mode = BAD;
308 }
309 DROPBITS(2);
310 break;
311
312 case STORED:
313 /* get and verify stored block length */
314 BYTEBITS(); /* go to byte boundary */
315 NEEDBITS(32);
316 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
317 strm->msg = (char *)"invalid stored block lengths";
318 state->mode = BAD;
319 break;
320 }
321 state->length = (unsigned)hold & 0xffff;
322 Tracev((stderr, "inflate: stored length %u\n",
323 state->length));
324 INITBITS();
325
326 /* copy stored block from input to output */
327 while (state->length != 0) {
328 copy = state->length;
329 PULL();
330 ROOM();
331 if (copy > have) copy = have;
332 if (copy > left) copy = left;
333 zmemcpy(put, next, copy);
334 have -= copy;
335 next += copy;
336 left -= copy;
337 put += copy;
338 state->length -= copy;
339 }
340 Tracev((stderr, "inflate: stored end\n"));
341 state->mode = TYPE;
342 break;
343
344 case TABLE:
345 /* get dynamic table entries descriptor */
346 NEEDBITS(14);
347 state->nlen = BITS(5) + 257;
348 DROPBITS(5);
349 state->ndist = BITS(5) + 1;
350 DROPBITS(5);
351 state->ncode = BITS(4) + 4;
352 DROPBITS(4);
353#ifndef PKZIP_BUG_WORKAROUND
354 if (state->nlen > 286 || state->ndist > 30) {
355 strm->msg = (char *)"too many length or distance symbols";
356 state->mode = BAD;
357 break;
358 }
359#endif
360 Tracev((stderr, "inflate: table sizes ok\n"));
361
362 /* get code length code lengths (not a typo) */
363 state->have = 0;
364 while (state->have < state->ncode) {
365 NEEDBITS(3);
366 state->lens[order[state->have++]] = (unsigned short)BITS(3);
367 DROPBITS(3);
368 }
369 while (state->have < 19)
370 state->lens[order[state->have++]] = 0;
371 state->next = state->codes;
372 state->lencode = (code const FAR *)(state->next);
373 state->lenbits = 7;
374 ret = inflate_table(CODES, state->lens, 19, &(state->next),
375 &(state->lenbits), state->work);
376 if (ret) {
377 strm->msg = (char *)"invalid code lengths set";
378 state->mode = BAD;
379 break;
380 }
381 Tracev((stderr, "inflate: code lengths ok\n"));
382
383 /* get length and distance code code lengths */
384 state->have = 0;
385 while (state->have < state->nlen + state->ndist) {
386 for (;;) {
387 this = state->lencode[BITS(state->lenbits)];
388 if ((unsigned)(this.bits) <= bits) break;
389 PULLBYTE();
390 }
391 if (this.val < 16) {
392 NEEDBITS(this.bits);
393 DROPBITS(this.bits);
394 state->lens[state->have++] = this.val;
395 }
396 else {
397 if (this.val == 16) {
398 NEEDBITS(this.bits + 2);
399 DROPBITS(this.bits);
400 if (state->have == 0) {
401 strm->msg = (char *)"invalid bit length repeat";
402 state->mode = BAD;
403 break;
404 }
405 len = (unsigned)(state->lens[state->have - 1]);
406 copy = 3 + BITS(2);
407 DROPBITS(2);
408 }
409 else if (this.val == 17) {
410 NEEDBITS(this.bits + 3);
411 DROPBITS(this.bits);
412 len = 0;
413 copy = 3 + BITS(3);
414 DROPBITS(3);
415 }
416 else {
417 NEEDBITS(this.bits + 7);
418 DROPBITS(this.bits);
419 len = 0;
420 copy = 11 + BITS(7);
421 DROPBITS(7);
422 }
423 if (state->have + copy > state->nlen + state->ndist) {
424 strm->msg = (char *)"invalid bit length repeat";
425 state->mode = BAD;
426 break;
427 }
428 while (copy--)
429 state->lens[state->have++] = (unsigned short)len;
430 }
431 }
432
433 /* build code tables */
434 state->next = state->codes;
435 state->lencode = (code const FAR *)(state->next);
436 state->lenbits = 9;
437 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
438 &(state->lenbits), state->work);
439 if (ret) {
440 strm->msg = (char *)"invalid literal/lengths set";
441 state->mode = BAD;
442 break;
443 }
444 state->distcode = (code const FAR *)(state->next);
445 state->distbits = 6;
446 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
447 &(state->next), &(state->distbits), state->work);
448 if (ret) {
449 strm->msg = (char *)"invalid distances set";
450 state->mode = BAD;
451 break;
452 }
453 Tracev((stderr, "inflate: codes ok\n"));
454 state->mode = LEN;
455
456 case LEN:
457 /* use inflate_fast() if we have enough input and output */
458 if (have >= 6 && left >= 258) {
459 RESTORE();
460 inflate_fast(strm, state->wsize);
461 LOAD();
462 break;
463 }
464
465 /* get a literal, length, or end-of-block code */
466 for (;;) {
467 this = state->lencode[BITS(state->lenbits)];
468 if ((unsigned)(this.bits) <= bits) break;
469 PULLBYTE();
470 }
471 if (this.op && (this.op & 0xf0) == 0) {
472 last = this;
473 for (;;) {
474 this = state->lencode[last.val +
475 (BITS(last.bits + last.op) >> last.bits)];
476 if ((unsigned)(last.bits + this.bits) <= bits) break;
477 PULLBYTE();
478 }
479 DROPBITS(last.bits);
480 }
481 DROPBITS(this.bits);
482 state->length = (unsigned)this.val;
483
484 /* process literal */
485 if (this.op == 0) {
486 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
487 "inflate: literal '%c'\n" :
488 "inflate: literal 0x%02x\n", this.val));
489 ROOM();
490 *put++ = (unsigned char)(state->length);
491 left--;
492 state->mode = LEN;
493 break;
494 }
495
496 /* process end of block */
497 if (this.op & 32) {
498 Tracevv((stderr, "inflate: end of block\n"));
499 state->mode = TYPE;
500 break;
501 }
502
503 /* invalid code */
504 if (this.op & 64) {
505 strm->msg = (char *)"invalid literal/length code";
506 state->mode = BAD;
507 break;
508 }
509
510 /* length code -- get extra bits, if any */
511 state->extra = (unsigned)(this.op) & 15;
512 if (state->extra != 0) {
513 NEEDBITS(state->extra);
514 state->length += BITS(state->extra);
515 DROPBITS(state->extra);
516 }
517 Tracevv((stderr, "inflate: length %u\n", state->length));
518
519 /* get distance code */
520 for (;;) {
521 this = state->distcode[BITS(state->distbits)];
522 if ((unsigned)(this.bits) <= bits) break;
523 PULLBYTE();
524 }
525 if ((this.op & 0xf0) == 0) {
526 last = this;
527 for (;;) {
528 this = state->distcode[last.val +
529 (BITS(last.bits + last.op) >> last.bits)];
530 if ((unsigned)(last.bits + this.bits) <= bits) break;
531 PULLBYTE();
532 }
533 DROPBITS(last.bits);
534 }
535 DROPBITS(this.bits);
536 if (this.op & 64) {
537 strm->msg = (char *)"invalid distance code";
538 state->mode = BAD;
539 break;
540 }
541 state->offset = (unsigned)this.val;
542
543 /* get distance extra bits, if any */
544 state->extra = (unsigned)(this.op) & 15;
545 if (state->extra != 0) {
546 NEEDBITS(state->extra);
547 state->offset += BITS(state->extra);
548 DROPBITS(state->extra);
549 }
550 if (state->offset > state->wsize) {
551 strm->msg = (char *)"invalid distance too far back";
552 state->mode = BAD;
553 break;
554 }
555 Tracevv((stderr, "inflate: distance %u\n", state->offset));
556
557 /* copy match from window to output */
558 do {
559 ROOM();
560 copy = state->wsize - state->offset;
561 if (copy < left) {
562 from = put + copy;
563 copy = left - copy;
564 }
565 else {
566 from = put - state->offset;
567 copy = left;
568 }
569 if (copy > state->length) copy = state->length;
570 state->length -= copy;
571 left -= copy;
572 do {
573 *put++ = *from++;
574 } while (--copy);
575 } while (state->length != 0);
576 break;
577
578 case DONE:
579 /* inflate stream terminated properly -- write leftover output */
580 ret = Z_STREAM_END;
581 if (left < state->wsize) {
582 if (out(out_desc, state->window, state->wsize - left))
583 ret = Z_BUF_ERROR;
584 }
585 goto leave;
586
587 case BAD:
588 ret = Z_DATA_ERROR;
589 goto leave;
590
591 default: /* can't happen, but makes compilers happy */
592 ret = Z_STREAM_ERROR;
593 goto leave;
594 }
595
596 /* Return unused input */
597 leave:
598 strm->next_in = next;
599 strm->avail_in = have;
600 return ret;
601}
602
603int ZEXPORT inflateBackEnd(strm)
604z_stream FAR *strm;
605{
606 struct inflate_state FAR *state;
607
608 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == Z_NULL)
609 return Z_STREAM_ERROR;
610 state = (struct inflate_state FAR *)strm->state;
611 ZFREE(strm, strm->state);
612 strm->state = Z_NULL;
613 Tracev((stderr, "inflate: end\n"));
614 return Z_OK;
615}
diff --git a/infblock.c b/infblock.c
deleted file mode 100644
index dd7a6d4..0000000
--- a/infblock.c
+++ /dev/null
@@ -1,403 +0,0 @@
1/* infblock.c -- interpret and process block types to last block
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6#include "zutil.h"
7#include "infblock.h"
8#include "inftrees.h"
9#include "infcodes.h"
10#include "infutil.h"
11
12struct inflate_codes_state {int dummy;}; /* for buggy compilers */
13
14/* simplify the use of the inflate_huft type with some defines */
15#define exop word.what.Exop
16#define bits word.what.Bits
17
18/* Table for deflate from PKZIP's appnote.txt. */
19local const uInt border[] = { /* Order of the bit length code lengths */
20 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
21
22/*
23 Notes beyond the 1.93a appnote.txt:
24
25 1. Distance pointers never point before the beginning of the output
26 stream.
27 2. Distance pointers can point back across blocks, up to 32k away.
28 3. There is an implied maximum of 7 bits for the bit length table and
29 15 bits for the actual data.
30 4. If only one code exists, then it is encoded using one bit. (Zero
31 would be more efficient, but perhaps a little confusing.) If two
32 codes exist, they are coded using one bit each (0 and 1).
33 5. There is no way of sending zero distance codes--a dummy must be
34 sent if there are none. (History: a pre 2.0 version of PKZIP would
35 store blocks with no distance codes, but this was discovered to be
36 too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
37 zero distance codes, which is sent as one code of zero bits in
38 length.
39 6. There are up to 286 literal/length codes. Code 256 represents the
40 end-of-block. Note however that the static length tree defines
41 288 codes just to fill out the Huffman codes. Codes 286 and 287
42 cannot be used though, since there is no length base or extra bits
43 defined for them. Similarily, there are up to 30 distance codes.
44 However, static trees define 32 codes (all 5 bits) to fill out the
45 Huffman codes, but the last two had better not show up in the data.
46 7. Unzip can check dynamic Huffman blocks for complete code sets.
47 The exception is that a single code would not be complete (see #4).
48 8. The five bits following the block type is really the number of
49 literal codes sent minus 257.
50 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
51 (1+6+6). Therefore, to output three times the length, you output
52 three codes (1+1+1), whereas to output four times the same length,
53 you only need two codes (1+3). Hmm.
54 10. In the tree reconstruction algorithm, Code = Code + Increment
55 only if BitLength(i) is not zero. (Pretty obvious.)
56 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
57 12. Note: length code 284 can represent 227-258, but length code 285
58 really is 258. The last length deserves its own, short code
59 since it gets used a lot in very redundant files. The length
60 258 is special since 258 - 3 (the min match length) is 255.
61 13. The literal/length and distance code bit lengths are read as a
62 single stream of lengths. It is possible (and advantageous) for
63 a repeat code (16, 17, or 18) to go across the boundary between
64 the two sets of lengths.
65 */
66
67
68void inflate_blocks_reset(s, z, c)
69inflate_blocks_statef *s;
70z_streamp z;
71uLongf *c;
72{
73 if (c != Z_NULL)
74 *c = s->check;
75 if (s->mode == BTREE || s->mode == DTREE)
76 ZFREE(z, s->sub.trees.blens);
77 if (s->mode == CODES)
78 inflate_codes_free(s->sub.decode.codes, z);
79 s->mode = TYPE;
80 s->bitk = 0;
81 s->bitb = 0;
82 s->read = s->write = s->window;
83 if (s->checkfn != Z_NULL)
84 z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);
85 Tracev((stderr, "inflate: blocks reset\n"));
86}
87
88
89inflate_blocks_statef *inflate_blocks_new(z, c, w)
90z_streamp z;
91check_func c;
92uInt w;
93{
94 inflate_blocks_statef *s;
95
96 if ((s = (inflate_blocks_statef *)ZALLOC
97 (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
98 return s;
99 if ((s->hufts =
100 (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
101 {
102 ZFREE(z, s);
103 return Z_NULL;
104 }
105 if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
106 {
107 ZFREE(z, s->hufts);
108 ZFREE(z, s);
109 return Z_NULL;
110 }
111 s->end = s->window + w;
112 s->checkfn = c;
113 s->mode = TYPE;
114 Tracev((stderr, "inflate: blocks allocated\n"));
115 inflate_blocks_reset(s, z, Z_NULL);
116 return s;
117}
118
119
120int inflate_blocks(s, z, r)
121inflate_blocks_statef *s;
122z_streamp z;
123int r;
124{
125 uInt t; /* temporary storage */
126 uLong b; /* bit buffer */
127 uInt k; /* bits in bit buffer */
128 Bytef *p; /* input data pointer */
129 uInt n; /* bytes available there */
130 Bytef *q; /* output window write pointer */
131 uInt m; /* bytes to end of window or read pointer */
132
133 /* copy input/output information to locals (UPDATE macro restores) */
134 LOAD
135
136 /* process input based on current state */
137 while (1) switch (s->mode)
138 {
139 case TYPE:
140 NEEDBITS(3)
141 t = (uInt)b & 7;
142 s->last = t & 1;
143 switch (t >> 1)
144 {
145 case 0: /* stored */
146 Tracev((stderr, "inflate: stored block%s\n",
147 s->last ? " (last)" : ""));
148 DUMPBITS(3)
149 t = k & 7; /* go to byte boundary */
150 DUMPBITS(t)
151 s->mode = LENS; /* get length of stored block */
152 break;
153 case 1: /* fixed */
154 Tracev((stderr, "inflate: fixed codes block%s\n",
155 s->last ? " (last)" : ""));
156 {
157 uInt bl, bd;
158 inflate_huft *tl, *td;
159
160 inflate_trees_fixed(&bl, &bd, &tl, &td, z);
161 s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
162 if (s->sub.decode.codes == Z_NULL)
163 {
164 r = Z_MEM_ERROR;
165 LEAVE
166 }
167 }
168 DUMPBITS(3)
169 s->mode = CODES;
170 break;
171 case 2: /* dynamic */
172 Tracev((stderr, "inflate: dynamic codes block%s\n",
173 s->last ? " (last)" : ""));
174 DUMPBITS(3)
175 s->mode = TABLE;
176 break;
177 case 3: /* illegal */
178 DUMPBITS(3)
179 s->mode = BAD;
180 z->msg = (char*)"invalid block type";
181 r = Z_DATA_ERROR;
182 LEAVE
183 }
184 break;
185 case LENS:
186 NEEDBITS(32)
187 if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
188 {
189 s->mode = BAD;
190 z->msg = (char*)"invalid stored block lengths";
191 r = Z_DATA_ERROR;
192 LEAVE
193 }
194 s->sub.left = (uInt)b & 0xffff;
195 b = k = 0; /* dump bits */
196 Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
197 s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
198 break;
199 case STORED:
200 if (n == 0)
201 LEAVE
202 NEEDOUT
203 t = s->sub.left;
204 if (t > n) t = n;
205 if (t > m) t = m;
206 zmemcpy(q, p, t);
207 p += t; n -= t;
208 q += t; m -= t;
209 if ((s->sub.left -= t) != 0)
210 break;
211 Tracev((stderr, "inflate: stored end, %lu total out\n",
212 z->total_out + (q >= s->read ? q - s->read :
213 (s->end - s->read) + (q - s->window))));
214 s->mode = s->last ? DRY : TYPE;
215 break;
216 case TABLE:
217 NEEDBITS(14)
218 s->sub.trees.table = t = (uInt)b & 0x3fff;
219#ifndef PKZIP_BUG_WORKAROUND
220 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
221 {
222 s->mode = BAD;
223 z->msg = (char*)"too many length or distance symbols";
224 r = Z_DATA_ERROR;
225 LEAVE
226 }
227#endif
228 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
229 if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
230 {
231 r = Z_MEM_ERROR;
232 LEAVE
233 }
234 DUMPBITS(14)
235 s->sub.trees.index = 0;
236 Tracev((stderr, "inflate: table sizes ok\n"));
237 s->mode = BTREE;
238 case BTREE:
239 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
240 {
241 NEEDBITS(3)
242 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
243 DUMPBITS(3)
244 }
245 while (s->sub.trees.index < 19)
246 s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
247 s->sub.trees.bb = 7;
248 t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
249 &s->sub.trees.tb, s->hufts, z);
250 if (t != Z_OK)
251 {
252 r = t;
253 if (r == Z_DATA_ERROR)
254 {
255 ZFREE(z, s->sub.trees.blens);
256 s->mode = BAD;
257 }
258 LEAVE
259 }
260 s->sub.trees.index = 0;
261 Tracev((stderr, "inflate: bits tree ok\n"));
262 s->mode = DTREE;
263 case DTREE:
264 while (t = s->sub.trees.table,
265 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
266 {
267 inflate_huft *h;
268 uInt i, j, c;
269
270 t = s->sub.trees.bb;
271 NEEDBITS(t)
272 h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
273 t = h->bits;
274 c = h->base;
275 if (c < 16)
276 {
277 DUMPBITS(t)
278 s->sub.trees.blens[s->sub.trees.index++] = c;
279 }
280 else /* c == 16..18 */
281 {
282 i = c == 18 ? 7 : c - 14;
283 j = c == 18 ? 11 : 3;
284 NEEDBITS(t + i)
285 DUMPBITS(t)
286 j += (uInt)b & inflate_mask[i];
287 DUMPBITS(i)
288 i = s->sub.trees.index;
289 t = s->sub.trees.table;
290 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
291 (c == 16 && i < 1))
292 {
293 ZFREE(z, s->sub.trees.blens);
294 s->mode = BAD;
295 z->msg = (char*)"invalid bit length repeat";
296 r = Z_DATA_ERROR;
297 LEAVE
298 }
299 c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
300 do {
301 s->sub.trees.blens[i++] = c;
302 } while (--j);
303 s->sub.trees.index = i;
304 }
305 }
306 s->sub.trees.tb = Z_NULL;
307 {
308 uInt bl, bd;
309 inflate_huft *tl, *td;
310 inflate_codes_statef *c;
311
312 bl = 9; /* must be <= 9 for lookahead assumptions */
313 bd = 6; /* must be <= 9 for lookahead assumptions */
314 t = s->sub.trees.table;
315 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
316 s->sub.trees.blens, &bl, &bd, &tl, &td,
317 s->hufts, z);
318 if (t != Z_OK)
319 {
320 if (t == (uInt)Z_DATA_ERROR)
321 {
322 ZFREE(z, s->sub.trees.blens);
323 s->mode = BAD;
324 }
325 r = t;
326 LEAVE
327 }
328 Tracev((stderr, "inflate: trees ok\n"));
329 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
330 {
331 r = Z_MEM_ERROR;
332 LEAVE
333 }
334 s->sub.decode.codes = c;
335 }
336 ZFREE(z, s->sub.trees.blens);
337 s->mode = CODES;
338 case CODES:
339 UPDATE
340 if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
341 return inflate_flush(s, z, r);
342 r = Z_OK;
343 inflate_codes_free(s->sub.decode.codes, z);
344 LOAD
345 Tracev((stderr, "inflate: codes end, %lu total out\n",
346 z->total_out + (q >= s->read ? q - s->read :
347 (s->end - s->read) + (q - s->window))));
348 if (!s->last)
349 {
350 s->mode = TYPE;
351 break;
352 }
353 s->mode = DRY;
354 case DRY:
355 FLUSH
356 if (s->read != s->write)
357 LEAVE
358 s->mode = DONE;
359 case DONE:
360 r = Z_STREAM_END;
361 LEAVE
362 case BAD:
363 r = Z_DATA_ERROR;
364 LEAVE
365 default:
366 r = Z_STREAM_ERROR;
367 LEAVE
368 }
369}
370
371
372int inflate_blocks_free(s, z)
373inflate_blocks_statef *s;
374z_streamp z;
375{
376 inflate_blocks_reset(s, z, Z_NULL);
377 ZFREE(z, s->window);
378 ZFREE(z, s->hufts);
379 ZFREE(z, s);
380 Tracev((stderr, "inflate: blocks freed\n"));
381 return Z_OK;
382}
383
384
385void inflate_set_dictionary(s, d, n)
386inflate_blocks_statef *s;
387const Bytef *d;
388uInt n;
389{
390 zmemcpy(s->window, d, n);
391 s->read = s->write = s->window + n;
392}
393
394
395/* Returns true if inflate is currently at the end of a block generated
396 * by Z_SYNC_FLUSH or Z_FULL_FLUSH.
397 * IN assertion: s != Z_NULL
398 */
399int inflate_blocks_sync_point(s)
400inflate_blocks_statef *s;
401{
402 return s->mode == LENS;
403}
diff --git a/infblock.h b/infblock.h
deleted file mode 100644
index 173b226..0000000
--- a/infblock.h
+++ /dev/null
@@ -1,39 +0,0 @@
1/* infblock.h -- header to use infblock.c
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* WARNING: this file should *not* be used by applications. It is
7 part of the implementation of the compression library and is
8 subject to change. Applications should only use zlib.h.
9 */
10
11struct inflate_blocks_state;
12typedef struct inflate_blocks_state FAR inflate_blocks_statef;
13
14extern inflate_blocks_statef * inflate_blocks_new OF((
15 z_streamp z,
16 check_func c, /* check function */
17 uInt w)); /* window size */
18
19extern int inflate_blocks OF((
20 inflate_blocks_statef *,
21 z_streamp ,
22 int)); /* initial return code */
23
24extern void inflate_blocks_reset OF((
25 inflate_blocks_statef *,
26 z_streamp ,
27 uLongf *)); /* check value on output */
28
29extern int inflate_blocks_free OF((
30 inflate_blocks_statef *,
31 z_streamp));
32
33extern void inflate_set_dictionary OF((
34 inflate_blocks_statef *s,
35 const Bytef *d, /* dictionary */
36 uInt n)); /* dictionary length */
37
38extern int inflate_blocks_sync_point OF((
39 inflate_blocks_statef *s));
diff --git a/infcodes.c b/infcodes.c
deleted file mode 100644
index 9abe541..0000000
--- a/infcodes.c
+++ /dev/null
@@ -1,251 +0,0 @@
1/* infcodes.c -- process literals and length/distance pairs
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6#include "zutil.h"
7#include "inftrees.h"
8#include "infblock.h"
9#include "infcodes.h"
10#include "infutil.h"
11#include "inffast.h"
12
13/* simplify the use of the inflate_huft type with some defines */
14#define exop word.what.Exop
15#define bits word.what.Bits
16
17typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
18 START, /* x: set up for LEN */
19 LEN, /* i: get length/literal/eob next */
20 LENEXT, /* i: getting length extra (have base) */
21 DIST, /* i: get distance next */
22 DISTEXT, /* i: getting distance extra */
23 COPY, /* o: copying bytes in window, waiting for space */
24 LIT, /* o: got literal, waiting for output space */
25 WASH, /* o: got eob, possibly still output waiting */
26 END, /* x: got eob and all data flushed */
27 BADCODE} /* x: got error */
28inflate_codes_mode;
29
30/* inflate codes private state */
31struct inflate_codes_state {
32
33 /* mode */
34 inflate_codes_mode mode; /* current inflate_codes mode */
35
36 /* mode dependent information */
37 uInt len;
38 union {
39 struct {
40 inflate_huft *tree; /* pointer into tree */
41 uInt need; /* bits needed */
42 } code; /* if LEN or DIST, where in tree */
43 uInt lit; /* if LIT, literal */
44 struct {
45 uInt get; /* bits to get for extra */
46 uInt dist; /* distance back to copy from */
47 } copy; /* if EXT or COPY, where and how much */
48 } sub; /* submode */
49
50 /* mode independent information */
51 Byte lbits; /* ltree bits decoded per branch */
52 Byte dbits; /* dtree bits decoder per branch */
53 inflate_huft *ltree; /* literal/length/eob tree */
54 inflate_huft *dtree; /* distance tree */
55
56};
57
58
59inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
60uInt bl, bd;
61inflate_huft *tl;
62inflate_huft *td; /* need separate declaration for Borland C++ */
63z_streamp z;
64{
65 inflate_codes_statef *c;
66
67 if ((c = (inflate_codes_statef *)
68 ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
69 {
70 c->mode = START;
71 c->lbits = (Byte)bl;
72 c->dbits = (Byte)bd;
73 c->ltree = tl;
74 c->dtree = td;
75 Tracev((stderr, "inflate: codes new\n"));
76 }
77 return c;
78}
79
80
81int inflate_codes(s, z, r)
82inflate_blocks_statef *s;
83z_streamp z;
84int r;
85{
86 uInt j; /* temporary storage */
87 inflate_huft *t; /* temporary pointer */
88 uInt e; /* extra bits or operation */
89 uLong b; /* bit buffer */
90 uInt k; /* bits in bit buffer */
91 Bytef *p; /* input data pointer */
92 uInt n; /* bytes available there */
93 Bytef *q; /* output window write pointer */
94 uInt m; /* bytes to end of window or read pointer */
95 Bytef *f; /* pointer to copy strings from */
96 inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
97
98 /* copy input/output information to locals (UPDATE macro restores) */
99 LOAD
100
101 /* process input and output based on current state */
102 while (1) switch (c->mode)
103 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
104 case START: /* x: set up for LEN */
105#ifndef SLOW
106 if (m >= 258 && n >= 10)
107 {
108 UPDATE
109 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
110 LOAD
111 if (r != Z_OK)
112 {
113 c->mode = r == Z_STREAM_END ? WASH : BADCODE;
114 break;
115 }
116 }
117#endif /* !SLOW */
118 c->sub.code.need = c->lbits;
119 c->sub.code.tree = c->ltree;
120 c->mode = LEN;
121 case LEN: /* i: get length/literal/eob next */
122 j = c->sub.code.need;
123 NEEDBITS(j)
124 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
125 DUMPBITS(t->bits)
126 e = (uInt)(t->exop);
127 if (e == 0) /* literal */
128 {
129 c->sub.lit = t->base;
130 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
131 "inflate: literal '%c'\n" :
132 "inflate: literal 0x%02x\n", t->base));
133 c->mode = LIT;
134 break;
135 }
136 if (e & 16) /* length */
137 {
138 c->sub.copy.get = e & 15;
139 c->len = t->base;
140 c->mode = LENEXT;
141 break;
142 }
143 if ((e & 64) == 0) /* next table */
144 {
145 c->sub.code.need = e;
146 c->sub.code.tree = t + t->base;
147 break;
148 }
149 if (e & 32) /* end of block */
150 {
151 Tracevv((stderr, "inflate: end of block\n"));
152 c->mode = WASH;
153 break;
154 }
155 c->mode = BADCODE; /* invalid code */
156 z->msg = (char*)"invalid literal/length code";
157 r = Z_DATA_ERROR;
158 LEAVE
159 case LENEXT: /* i: getting length extra (have base) */
160 j = c->sub.copy.get;
161 NEEDBITS(j)
162 c->len += (uInt)b & inflate_mask[j];
163 DUMPBITS(j)
164 c->sub.code.need = c->dbits;
165 c->sub.code.tree = c->dtree;
166 Tracevv((stderr, "inflate: length %u\n", c->len));
167 c->mode = DIST;
168 case DIST: /* i: get distance next */
169 j = c->sub.code.need;
170 NEEDBITS(j)
171 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
172 DUMPBITS(t->bits)
173 e = (uInt)(t->exop);
174 if (e & 16) /* distance */
175 {
176 c->sub.copy.get = e & 15;
177 c->sub.copy.dist = t->base;
178 c->mode = DISTEXT;
179 break;
180 }
181 if ((e & 64) == 0) /* next table */
182 {
183 c->sub.code.need = e;
184 c->sub.code.tree = t + t->base;
185 break;
186 }
187 c->mode = BADCODE; /* invalid code */
188 z->msg = (char*)"invalid distance code";
189 r = Z_DATA_ERROR;
190 LEAVE
191 case DISTEXT: /* i: getting distance extra */
192 j = c->sub.copy.get;
193 NEEDBITS(j)
194 c->sub.copy.dist += (uInt)b & inflate_mask[j];
195 DUMPBITS(j)
196 Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
197 c->mode = COPY;
198 case COPY: /* o: copying bytes in window, waiting for space */
199 f = q - c->sub.copy.dist;
200 while (f < s->window) /* modulo window size-"while" instead */
201 f += s->end - s->window; /* of "if" handles invalid distances */
202 while (c->len)
203 {
204 NEEDOUT
205 OUTBYTE(*f++)
206 if (f == s->end)
207 f = s->window;
208 c->len--;
209 }
210 c->mode = START;
211 break;
212 case LIT: /* o: got literal, waiting for output space */
213 NEEDOUT
214 OUTBYTE(c->sub.lit)
215 c->mode = START;
216 break;
217 case WASH: /* o: got eob, possibly more output */
218 if (k > 7) /* return unused byte, if any */
219 {
220 Assert(k < 16, "inflate_codes grabbed too many bytes")
221 k -= 8;
222 n++;
223 p--; /* can always return one */
224 }
225 FLUSH
226 if (s->read != s->write)
227 LEAVE
228 c->mode = END;
229 case END:
230 r = Z_STREAM_END;
231 LEAVE
232 case BADCODE: /* x: got error */
233 r = Z_DATA_ERROR;
234 LEAVE
235 default:
236 r = Z_STREAM_ERROR;
237 LEAVE
238 }
239#ifdef NEED_DUMMY_RETURN
240 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
241#endif
242}
243
244
245void inflate_codes_free(c, z)
246inflate_codes_statef *c;
247z_streamp z;
248{
249 ZFREE(z, c);
250 Tracev((stderr, "inflate: codes free\n"));
251}
diff --git a/infcodes.h b/infcodes.h
deleted file mode 100644
index 46821a0..0000000
--- a/infcodes.h
+++ /dev/null
@@ -1,27 +0,0 @@
1/* infcodes.h -- header to use infcodes.c
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* WARNING: this file should *not* be used by applications. It is
7 part of the implementation of the compression library and is
8 subject to change. Applications should only use zlib.h.
9 */
10
11struct inflate_codes_state;
12typedef struct inflate_codes_state FAR inflate_codes_statef;
13
14extern inflate_codes_statef *inflate_codes_new OF((
15 uInt, uInt,
16 inflate_huft *, inflate_huft *,
17 z_streamp ));
18
19extern int inflate_codes OF((
20 inflate_blocks_statef *,
21 z_streamp ,
22 int));
23
24extern void inflate_codes_free OF((
25 inflate_codes_statef *,
26 z_streamp ));
27
diff --git a/inffast.c b/inffast.c
index aa7f1d4..8d145c2 100644
--- a/inffast.c
+++ b/inffast.c
@@ -1,183 +1,298 @@
1/* inffast.c -- process literals and length/distance pairs fast 1/* inffast.c -- fast decoding
2 * Copyright (C) 1995-2002 Mark Adler 2 * Copyright (C) 1995-2003 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#include "infblock.h" 8#include "inflate.h"
9#include "infcodes.h"
10#include "infutil.h"
11#include "inffast.h" 9#include "inffast.h"
12 10
13struct inflate_codes_state {int dummy;}; /* for buggy compilers */ 11/* Allow machine dependent optimization for post-increment or pre-increment.
12 Based on testing to date,
13 Pre-increment preferred for:
14 - PowerPC G3 (Adler)
15 - MIPS R5000 (Randers-Pehrson)
16 Post-increment preferred for:
17 - none
18 No measurable difference:
19 - Pentium III (Anderson)
20 */
21#ifdef POSTINC
22# define OFF 0
23# define PUP(a) *(a)++
24#else
25# define OFF 1
26# define PUP(a) *++(a)
27#endif
14 28
15/* simplify the use of the inflate_huft type with some defines */ 29/*
16#define exop word.what.Exop 30 Decode literal, length, and distance codes and write out the resulting
17#define bits word.what.Bits 31 literal and match bytes until either not enough input or output is
32 available, an end-of-block is encountered, or a data error is encountered.
33 When large enough input and output buffers are supplied to inflate(), for
34 example, a 16K input buffer and a 64K output buffer, more than 95% of the
35 inflate execution time is spent in this routine.
18 36
19/* macros for bit input with no checking and for returning unused bytes */ 37 Entry assumptions:
20#define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
21#define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
22 38
23/* Called with number of bytes left to write in window at least 258 39 state->mode == LEN
24 (the maximum string length) and number of input bytes available 40 strm->avail_in >= 6
25 at least ten. The ten bytes are six bytes for the longest length/ 41 strm->avail_out >= 258
26 distance pair plus four bytes for overloading the bit buffer. */ 42 start >= strm->avail_out
43 state->bits < 8
27 44
28int inflate_fast(bl, bd, tl, td, s, z) 45 On return, state->mode is one of:
29uInt bl, bd;
30inflate_huft *tl;
31inflate_huft *td; /* need separate declaration for Borland C++ */
32inflate_blocks_statef *s;
33z_streamp z;
34{
35 inflate_huft *t; /* temporary pointer */
36 uInt e; /* extra bits or operation */
37 uLong b; /* bit buffer */
38 uInt k; /* bits in bit buffer */
39 Bytef *p; /* input data pointer */
40 uInt n; /* bytes available there */
41 Bytef *q; /* output window write pointer */
42 uInt m; /* bytes to end of window or read pointer */
43 uInt ml; /* mask for literal/length tree */
44 uInt md; /* mask for distance tree */
45 uInt c; /* bytes to copy */
46 uInt d; /* distance back to copy from */
47 Bytef *r; /* copy source pointer */
48 46
49 /* load input, output, bit values */ 47 LEN -- ran out of enough output space or enough available input
50 LOAD 48 TYPE -- reached end of block code, inflate() to interpret next block
49 BAD -- error in block data
51 50
52 /* initialize masks */ 51 Notes:
53 ml = inflate_mask[bl];
54 md = inflate_mask[bd];
55 52
56 /* do until not enough input or output space for fast loop */ 53 - The maximum input bits used by a length/distance pair is 15 bits for the
57 do { /* assume called with m >= 258 && n >= 10 */ 54 length code, 5 bits for the length extra, 15 bits for the distance code,
58 /* get literal/length code */ 55 and 13 bits for the distance extra. This totals 48 bits, or six bytes.
59 GRABBITS(20) /* max bits for literal/length code */ 56 Therefore if strm->avail_in >= 6, then there is enough input to avoid
60 if ((e = (t = tl + ((uInt)b & ml))->exop) == 0) 57 checking for available input while decoding.
61 { 58
62 DUMPBITS(t->bits) 59 - The maximum bytes that a single length/distance pair can output is 258
63 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? 60 bytes, which is the maximum length that can be coded. inflate_fast()
64 "inflate: * literal '%c'\n" : 61 requires strm->avail_out >= 258 for each loop to avoid checking for
65 "inflate: * literal 0x%02x\n", t->base)); 62 output space.
66 *q++ = (Byte)t->base; 63 */
67 m--; 64void inflate_fast(strm, start)
68 continue; 65z_streamp strm;
69 } 66unsigned start; /* inflate()'s starting value for strm->avail_out */
70 do { 67{
71 DUMPBITS(t->bits) 68 struct inflate_state FAR *state;
72 if (e & 16) 69 unsigned char FAR *in; /* local strm->next_in */
73 { 70 unsigned char FAR *last; /* while in < last, enough input available */
74 /* get extra bits for length */ 71 unsigned char FAR *out; /* local strm->next_out */
75 e &= 15; 72 unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
76 c = t->base + ((uInt)b & inflate_mask[e]); 73 unsigned char FAR *end; /* while out < end, enough space available */
77 DUMPBITS(e) 74 unsigned wsize; /* window size or zero if not using window */
78 Tracevv((stderr, "inflate: * length %u\n", c)); 75 unsigned write; /* window write index */
76 unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
77 unsigned long hold; /* local strm->hold */
78 unsigned bits; /* local strm->bits */
79 code const FAR *lcode; /* local strm->lencode */
80 code const FAR *dcode; /* local strm->distcode */
81 unsigned lmask; /* mask for first level of length codes */
82 unsigned dmask; /* mask for first level of distance codes */
83 code this; /* retrieved table entry */
84 unsigned op; /* code bits, operation, extra bits, or */
85 /* window position, window bytes to copy */
86 unsigned len; /* match length, unused bytes */
87 unsigned dist; /* match distance */
88 unsigned char FAR *from; /* where to copy match from */
79 89
80 /* decode distance base of block to copy */ 90 /* copy state to local variables */
81 GRABBITS(15); /* max bits for distance code */ 91 state = (struct inflate_state FAR *)strm->state;
82 e = (t = td + ((uInt)b & md))->exop; 92 in = strm->next_in - OFF;
83 do { 93 last = in + (strm->avail_in - 5);
84 DUMPBITS(t->bits) 94 out = strm->next_out - OFF;
85 if (e & 16) 95 beg = out - (start - strm->avail_out);
86 { 96 end = out + (strm->avail_out - 257);
87 /* get extra bits to add to distance base */ 97 wsize = state->wsize;
88 e &= 15; 98 write = state->write;
89 GRABBITS(e) /* get extra bits (up to 13) */ 99 window = state->window;
90 d = t->base + ((uInt)b & inflate_mask[e]); 100 hold = state->hold;
91 DUMPBITS(e) 101 bits = state->bits;
92 Tracevv((stderr, "inflate: * distance %u\n", d)); 102 lcode = state->lencode;
103 dcode = state->distcode;
104 lmask = (1U << state->lenbits) - 1;
105 dmask = (1U << state->distbits) - 1;
93 106
94 /* do the copy */ 107 /* decode literals and length/distances until end-of-block or not enough
95 m -= c; 108 input data or output space */
96 r = q - d; 109 do {
97 if (r < s->window) /* wrap if needed */ 110 if (bits < 15) {
98 { 111 hold += (unsigned long)(PUP(in)) << bits;
99 do { 112 bits += 8;
100 r += s->end - s->window; /* force pointer in window */ 113 hold += (unsigned long)(PUP(in)) << bits;
101 } while (r < s->window); /* covers invalid distances */ 114 bits += 8;
102 e = s->end - r; 115 }
103 if (c > e) 116 this = lcode[hold & lmask];
104 { 117 dolen:
105 c -= e; /* wrapped copy */ 118 op = (unsigned)(this.bits);
106 do { 119 hold >>= op;
107 *q++ = *r++; 120 bits -= op;
108 } while (--e); 121 op = (unsigned)(this.op);
109 r = s->window; 122 if (op == 0) { /* literal */
110 do { 123 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
111 *q++ = *r++; 124 "inflate: literal '%c'\n" :
112 } while (--c); 125 "inflate: literal 0x%02x\n", this.val));
113 } 126 PUP(out) = (unsigned char)(this.val);
114 else /* normal copy */ 127 }
115 { 128 else if (op & 16) { /* length base */
116 *q++ = *r++; c--; 129 len = (unsigned)(this.val);
117 *q++ = *r++; c--; 130 op &= 15; /* number of extra bits */
118 do { 131 if (op) {
119 *q++ = *r++; 132 if (bits < op) {
120 } while (--c); 133 hold += (unsigned long)(PUP(in)) << bits;
121 } 134 bits += 8;
135 }
136 len += hold & ((1U << op) - 1);
137 hold >>= op;
138 bits -= op;
139 }
140 Tracevv((stderr, "inflate: length %u\n", len));
141 if (bits < 15) {
142 hold += (unsigned long)(PUP(in)) << bits;
143 bits += 8;
144 hold += (unsigned long)(PUP(in)) << bits;
145 bits += 8;
122 } 146 }
123 else /* normal copy */ 147 this = dcode[hold & dmask];
124 { 148 dodist:
125 *q++ = *r++; c--; 149 op = (unsigned)(this.bits);
126 *q++ = *r++; c--; 150 hold >>= op;
127 do { 151 bits -= op;
128 *q++ = *r++; 152 op = (unsigned)(this.op);
129 } while (--c); 153 if (op & 16) { /* distance base */
154 dist = (unsigned)(this.val);
155 op &= 15; /* number of extra bits */
156 if (bits < op) {
157 hold += (unsigned long)(PUP(in)) << bits;
158 bits += 8;
159 if (bits < op) {
160 hold += (unsigned long)(PUP(in)) << bits;
161 bits += 8;
162 }
163 }
164 dist += hold & ((1U << op) - 1);
165 hold >>= op;
166 bits -= op;
167 Tracevv((stderr, "inflate: distance %u\n", dist));
168 op = (unsigned)(out - beg); /* max distance in output */
169 if (dist > op) { /* see if copy from window */
170 if (dist > wsize) {
171 strm->msg = (char *)"invalid distance too far back";
172 state->mode = BAD;
173 break;
174 }
175 from = window - OFF;
176 op = dist - op; /* distance back in window */
177 if (write == 0) { /* very common case */
178 from += wsize - op;
179 if (op < len) { /* some from window */
180 len -= op;
181 do {
182 PUP(out) = PUP(from);
183 } while (--op);
184 from = out - dist; /* rest from output */
185 }
186 }
187 else if (write < op) { /* wrap around window */
188 from += wsize + write - op;
189 op -= write;
190 if (op < len) { /* some from end of window */
191 len -= op;
192 do {
193 PUP(out) = PUP(from);
194 } while (--op);
195 from = window - OFF;
196 if (write < len) { /* some from start of window */
197 op = write;
198 len -= op;
199 do {
200 PUP(out) = PUP(from);
201 } while (--op);
202 from = out - dist; /* rest from output */
203 }
204 }
205 }
206 else { /* contiguous in window */
207 from += write - op;
208 if (op < len) { /* some from window */
209 len -= op;
210 do {
211 PUP(out) = PUP(from);
212 } while (--op);
213 from = out - dist; /* rest from output */
214 }
215 }
216 while (len > 2) {
217 PUP(out) = PUP(from);
218 PUP(out) = PUP(from);
219 PUP(out) = PUP(from);
220 len -= 3;
221 }
222 if (len) {
223 PUP(out) = PUP(from);
224 if (len > 1)
225 PUP(out) = PUP(from);
226 }
227 }
228 else {
229 from = out - dist; /* copy direct from output */
230 do { /* minimum length is three */
231 PUP(out) = PUP(from);
232 PUP(out) = PUP(from);
233 PUP(out) = PUP(from);
234 len -= 3;
235 } while (len > 2);
236 if (len) {
237 PUP(out) = PUP(from);
238 if (len > 1)
239 PUP(out) = PUP(from);
240 }
241 }
130 } 242 }
243 else if ((op & 64) == 0) { /* 2nd level distance code */
244 this = dcode[this.val + (hold & ((1U << op) - 1))];
245 goto dodist;
246 }
247 else {
248 strm->msg = (char *)"invalid distance code";
249 state->mode = BAD;
250 break;
251 }
252 }
253 else if ((op & 64) == 0) { /* 2nd level length code */
254 this = lcode[this.val + (hold & ((1U << op) - 1))];
255 goto dolen;
256 }
257 else if (op & 32) { /* end-of-block */
258 Tracevv((stderr, "inflate: end of block\n"));
259 state->mode = TYPE;
131 break; 260 break;
132 }
133 else if ((e & 64) == 0)
134 {
135 t += t->base;
136 e = (t += ((uInt)b & inflate_mask[e]))->exop;
137 }
138 else
139 {
140 z->msg = (char*)"invalid distance code";
141 UNGRAB
142 UPDATE
143 return Z_DATA_ERROR;
144 }
145 } while (1);
146 break;
147 }
148 if ((e & 64) == 0)
149 {
150 t += t->base;
151 if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
152 {
153 DUMPBITS(t->bits)
154 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
155 "inflate: * literal '%c'\n" :
156 "inflate: * literal 0x%02x\n", t->base));
157 *q++ = (Byte)t->base;
158 m--;
159 break;
160 } 261 }
161 } 262 else {
162 else if (e & 32) 263 strm->msg = (char *)"invalid literal/length code";
163 { 264 state->mode = BAD;
164 Tracevv((stderr, "inflate: * end of block\n")); 265 break;
165 UNGRAB 266 }
166 UPDATE 267 } while (in < last && out < end);
167 return Z_STREAM_END; 268
168 } 269 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
169 else 270 len = bits >> 3;
170 { 271 in -= len;
171 z->msg = (char*)"invalid literal/length code"; 272 bits -= len << 3;
172 UNGRAB 273 hold &= (1U << bits) - 1;
173 UPDATE
174 return Z_DATA_ERROR;
175 }
176 } while (1);
177 } while (m >= 258 && n >= 10);
178 274
179 /* not enough input or output--restore pointers and return */ 275 /* update state and return */
180 UNGRAB 276 strm->next_in = in + OFF;
181 UPDATE 277 strm->next_out = out + OFF;
182 return Z_OK; 278 strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
279 strm->avail_out = (unsigned)(out < end ?
280 257 + (end - out) : 257 - (out - end));
281 state->hold = hold;
282 state->bits = bits;
283 return;
183} 284}
285
286/*
287 inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
288 - Using bit fields for code structure
289 - Different op definition to avoid & for extra bits (do & for table bits)
290 - Three separate decoding do-loops for direct, window, and write == 0
291 - Special case for distance > 1 copies to do overlapped load and store copy
292 - Explicit branch predictions (based on measured branch probabilities)
293 - Deferring match copy and interspersed it with decoding subsequent codes
294 - Swapping literal/length else
295 - Swapping window/direct else
296 - Larger unrolled copy loops (three is about right)
297 - Moving len -= 3 statement into middle of loop
298 */
diff --git a/inffast.h b/inffast.h
index a31a4bb..977101b 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-2002 Mark Adler 2 * Copyright (C) 1995-2003 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,10 +8,4 @@
8 subject to change. Applications should only use zlib.h. 8 subject to change. Applications should only use zlib.h.
9 */ 9 */
10 10
11extern int inflate_fast OF(( 11void inflate_fast OF((z_streamp strm, unsigned start));
12 uInt,
13 uInt,
14 inflate_huft *,
15 inflate_huft *,
16 inflate_blocks_statef *,
17 z_streamp ));
diff --git a/inffixed.h b/inffixed.h
index 77f7e76..75ed4b5 100644
--- a/inffixed.h
+++ b/inffixed.h
@@ -1,151 +1,94 @@
1/* inffixed.h -- table for decoding fixed codes 1 /* inffixed.h -- table for decoding fixed codes
2 * Generated automatically by the maketree.c program 2 * Generated automatically by makefixed().
3 */ 3 */
4 4
5/* WARNING: this file should *not* be used by applications. It is 5 /* WARNING: this file should *not* be used by applications. It
6 part of the implementation of the compression library and is 6 is part of the implementation of the compression library and
7 subject to change. Applications should only use zlib.h. 7 is subject to change. Applications should only use zlib.h.
8 */ 8 */
9 9
10local uInt fixed_bl = 9; 10 static const code lenfix[512] = {
11local uInt fixed_bd = 5; 11 {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
12local inflate_huft fixed_tl[] = { 12 {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
13 {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115}, 13 {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
14 {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},192}, 14 {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
15 {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},160}, 15 {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
16 {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},224}, 16 {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
17 {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},144}, 17 {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
18 {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},208}, 18 {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
19 {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},176}, 19 {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
20 {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},240}, 20 {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
21 {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227}, 21 {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
22 {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},200}, 22 {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
23 {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},168}, 23 {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
24 {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},232}, 24 {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
25 {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},152}, 25 {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
26 {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},216}, 26 {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
27 {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},184}, 27 {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
28 {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},248}, 28 {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
29 {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163}, 29 {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
30 {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},196}, 30 {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
31 {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},164}, 31 {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
32 {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},228}, 32 {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
33 {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},148}, 33 {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
34 {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},212}, 34 {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
35 {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},180}, 35 {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
36 {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},244}, 36 {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
37 {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0}, 37 {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
38 {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},204}, 38 {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
39 {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},172}, 39 {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
40 {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},236}, 40 {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
41 {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},156}, 41 {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
42 {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},220}, 42 {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
43 {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},188}, 43 {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
44 {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},252}, 44 {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
45 {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131}, 45 {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
46 {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},194}, 46 {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
47 {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},162}, 47 {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
48 {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},226}, 48 {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
49 {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},146}, 49 {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
50 {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},210}, 50 {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
51 {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},178}, 51 {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
52 {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},242}, 52 {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
53 {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258}, 53 {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
54 {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},202}, 54 {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
55 {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},170}, 55 {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
56 {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},234}, 56 {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
57 {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},154}, 57 {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
58 {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},218}, 58 {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
59 {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},186}, 59 {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
60 {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},250}, 60 {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
61 {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195}, 61 {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
62 {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},198}, 62 {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
63 {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},166}, 63 {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
64 {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},230}, 64 {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
65 {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},150}, 65 {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
66 {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},214}, 66 {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
67 {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},182}, 67 {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
68 {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},246}, 68 {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
69 {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0}, 69 {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
70 {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},206}, 70 {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
71 {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},174}, 71 {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
72 {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},238}, 72 {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
73 {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},158}, 73 {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
74 {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},222}, 74 {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
75 {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},190}, 75 {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
76 {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},254}, 76 {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
77 {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115}, 77 {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
78 {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},193}, 78 {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
79 {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},161}, 79 {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
80 {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},225}, 80 {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
81 {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},145}, 81 {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
82 {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},209}, 82 {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
83 {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},177}, 83 {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
84 {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},241}, 84 {0,9,255}
85 {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227}, 85 };
86 {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},201}, 86
87 {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},169}, 87 static const code distfix[32] = {
88 {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},233}, 88 {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
89 {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},153}, 89 {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
90 {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},217}, 90 {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
91 {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},185}, 91 {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
92 {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},249}, 92 {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
93 {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163}, 93 {22,5,193},{64,5,0}
94 {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},197}, 94 };
95 {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},165},
96 {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},229},
97 {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},149},
98 {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},213},
99 {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},181},
100 {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},245},
101 {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
102 {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},205},
103 {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},173},
104 {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},237},
105 {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},157},
106 {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},221},
107 {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},189},
108 {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},253},
109 {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
110 {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},195},
111 {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},163},
112 {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},227},
113 {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},147},
114 {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},211},
115 {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},179},
116 {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},243},
117 {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
118 {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},203},
119 {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},171},
120 {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},235},
121 {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},155},
122 {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},219},
123 {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},187},
124 {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},251},
125 {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
126 {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},199},
127 {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},167},
128 {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},231},
129 {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},151},
130 {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},215},
131 {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},183},
132 {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},247},
133 {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
134 {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},207},
135 {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},175},
136 {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},239},
137 {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},159},
138 {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},223},
139 {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},191},
140 {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},255}
141 };
142local inflate_huft fixed_td[] = {
143 {{{80,5}},1}, {{{87,5}},257}, {{{83,5}},17}, {{{91,5}},4097},
144 {{{81,5}},5}, {{{89,5}},1025}, {{{85,5}},65}, {{{93,5}},16385},
145 {{{80,5}},3}, {{{88,5}},513}, {{{84,5}},33}, {{{92,5}},8193},
146 {{{82,5}},9}, {{{90,5}},2049}, {{{86,5}},129}, {{{192,5}},24577},
147 {{{80,5}},2}, {{{87,5}},385}, {{{83,5}},25}, {{{91,5}},6145},
148 {{{81,5}},7}, {{{89,5}},1537}, {{{85,5}},97}, {{{93,5}},24577},
149 {{{80,5}},4}, {{{88,5}},769}, {{{84,5}},49}, {{{92,5}},12289},
150 {{{82,5}},13}, {{{90,5}},3073}, {{{86,5}},193}, {{{192,5}},24577}
151 };
diff --git a/inflate.c b/inflate.c
index dfb2e86..36fbb75 100644
--- a/inflate.c
+++ b/inflate.c
@@ -1,366 +1,1249 @@
1/* inflate.c -- zlib interface to inflate modules 1/* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2002 Mark Adler 2 * Copyright (C) 1995-2003 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 */
5
6/*
7 * Change history:
8 *
9 * 1.2.beta0 24 Nov 2002
10 * - First version -- complete rewrite of inflate to simplify code, avoid
11 * creation of window when not needed, minimize use of window when it is
12 * needed, make inffast.c even faster, implement gzip decoding, and to
13 * improve code readability and style over the previous zlib inflate code
14 *
15 * 1.2.beta1 25 Nov 2002
16 * - Use pointers for available input and output checking in inffast.c
17 * - Remove input and output counters in inffast.c
18 * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
19 * - Remove unnecessary second byte pull from length extra in inffast.c
20 * - Unroll direct copy to three copies per loop in inffast.c
21 *
22 * 1.2.beta2 4 Dec 2002
23 * - Change external routine names to reduce potential conflicts
24 * - Correct filename to inffixed.h for fixed tables in inflate.c
25 * - Make hbuf[] unsigned char to match parameter type in inflate.c
26 * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
27 * to avoid negation problem on Alphas (64 bit) in inflate.c
28 *
29 * 1.2.beta3 22 Dec 2002
30 * - Add comments on state->bits assertion in inffast.c
31 * - Add comments on op field in inftrees.h
32 * - Fix bug in reuse of allocated window after inflateReset()
33 * - Remove bit fields--back to byte structure for speed
34 * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
35 * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
36 * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
37 * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
38 * - Use local copies of stream next and avail values, as well as local bit
39 * buffer and bit count in inflate()--for speed when inflate_fast() not used
40 *
41 * 1.2.beta4 1 Jan 2003
42 * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
43 * - Move a comment on output buffer sizes from inffast.c to inflate.c
44 * - Add comments in inffast.c to introduce the inflate_fast() routine
45 * - Rearrange window copies in inflate_fast() for speed and simplification
46 * - Unroll last copy for window match in inflate_fast()
47 * - Use local copies of window variables in inflate_fast() for speed
48 * - Pull out common write == 0 case for speed in inflate_fast()
49 * - Make op and len in inflate_fast() unsigned for consistency
50 * - Add FAR to lcode and dcode declarations in inflate_fast()
51 * - Simplified bad distance check in inflate_fast()
52 * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
53 * source file infback.c to provide a call-back interface to inflate for
54 * programs like gzip and unzip -- uses window as output buffer to avoid
55 * window copying
56 *
57 * 1.2.beta5 1 Jan 2003
58 * - Improved inflateBack() interface to allow the caller to provide initial
59 * input in strm.
60 * - Fixed stored blocks bug in inflateBack()
61 *
62 * 1.2.beta6 4 Jan 2003
63 * - Added comments in inffast.c on effectiveness of POSTINC
64 * - Typecasting all around to reduce compiler warnings
65 * - Changed loops from while (1) or do {} while (1) to for (;;), again to
66 * make compilers happy
67 * - Changed type of window in inflateBackInit() to unsigned char *
68 *
69 * 1.2.beta7 27 Jan 2003
70 * - Changed many types to unsigned or unsigned short to avoid warnings
71 * - Added inflateCopy() function
72 *
73 * 1.2.0 9 Mar 2003
74 * - Changed inflateBack() interface to provide separate opaque descriptors
75 * for the in() and out() functions
76 * - Changed inflateBack() argument and in_func typedef to swap the length
77 * and buffer address return values for the input function
78 * - Check next_in and next_out for Z_NULL on entry to inflate()
79 *
80 * Remainder of change history is in ChangeLog in zlib distribution.
4 */ 81 */
5 82
6#include "zutil.h" 83#include "zutil.h"
7#include "infblock.h" 84#include "inftrees.h"
8 85#include "inflate.h"
9struct inflate_blocks_state {int dummy;}; /* for buggy compilers */ 86#include "inffast.h"
10
11typedef enum {
12 METHOD, /* waiting for method byte */
13 FLAG, /* waiting for flag byte */
14 DICT4, /* four dictionary check bytes to go */
15 DICT3, /* three dictionary check bytes to go */
16 DICT2, /* two dictionary check bytes to go */
17 DICT1, /* one dictionary check byte to go */
18 DICT0, /* waiting for inflateSetDictionary */
19 BLOCKS, /* decompressing blocks */
20 CHECK4, /* four check bytes to go */
21 CHECK3, /* three check bytes to go */
22 CHECK2, /* two check bytes to go */
23 CHECK1, /* one check byte to go */
24 DONE, /* finished check, done */
25 BAD} /* got an error--stay here */
26inflate_mode;
27
28/* inflate private state */
29struct internal_state {
30
31 /* mode */
32 inflate_mode mode; /* current inflate mode */
33
34 /* mode dependent information */
35 union {
36 uInt method; /* if FLAGS, method byte */
37 struct {
38 uLong was; /* computed check value */
39 uLong need; /* stream check value */
40 } check; /* if CHECK, check values to compare */
41 uInt marker; /* if BAD, inflateSync's marker bytes count */
42 } sub; /* submode */
43
44 /* mode independent information */
45 int nowrap; /* flag for no wrapper */
46 uInt wbits; /* log2(window size) (8..15, defaults to 15) */
47 inflate_blocks_statef
48 *blocks; /* current inflate_blocks state */
49
50};
51
52
53int ZEXPORT inflateReset(z)
54z_streamp z;
55{
56 if (z == Z_NULL || z->state == Z_NULL)
57 return Z_STREAM_ERROR;
58 z->total_in = z->total_out = 0;
59 z->msg = Z_NULL;
60 z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
61 inflate_blocks_reset(z->state->blocks, z, Z_NULL);
62 Tracev((stderr, "inflate: reset\n"));
63 return Z_OK;
64}
65 87
88#ifdef MAKEFIXED
89# ifndef BUILDFIXED
90# define BUILDFIXED
91# endif
92#endif
93
94/* function prototypes */
95local void fixedtables OF((struct inflate_state FAR *state));
96local int updatewindow OF((z_streamp strm, unsigned out));
97#ifdef BUILDFIXED
98 void makefixed OF((void));
99#endif
100local unsigned syncsearch OF((unsigned *have, unsigned char FAR *buf,
101 unsigned len));
66 102
67int ZEXPORT inflateEnd(z) 103int ZEXPORT inflateReset(strm)
68z_streamp z; 104z_streamp strm;
69{ 105{
70 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL) 106 struct inflate_state FAR *state;
71 return Z_STREAM_ERROR;
72 if (z->state->blocks != Z_NULL)
73 inflate_blocks_free(z->state->blocks, z);
74 ZFREE(z, z->state);
75 z->state = Z_NULL;
76 Tracev((stderr, "inflate: end\n"));
77 return Z_OK;
78}
79 107
108 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
109 state = (struct inflate_state FAR *)strm->state;
110 strm->total_in = strm->total_out = state->total = 0;
111 strm->msg = Z_NULL;
112 state->mode = HEAD;
113 state->last = 0;
114 state->havedict = 0;
115 state->wsize = 0;
116 state->hold = 0;
117 state->bits = 0;
118 state->lencode = state->distcode = state->next = state->codes;
119 Tracev((stderr, "inflate: reset\n"));
120 return Z_OK;
121}
80 122
81int ZEXPORT inflateInit2_(z, w, version, stream_size) 123int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
82z_streamp z; 124z_streamp strm;
83int w; 125int windowBits;
84const char *version; 126const char *version;
85int stream_size; 127int stream_size;
86{ 128{
87 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || 129 struct inflate_state FAR *state;
88 stream_size != sizeof(z_stream))
89 return Z_VERSION_ERROR;
90
91 /* initialize state */
92 if (z == Z_NULL)
93 return Z_STREAM_ERROR;
94 z->msg = Z_NULL;
95 if (z->zalloc == Z_NULL)
96 {
97 z->zalloc = zcalloc;
98 z->opaque = (voidpf)0;
99 }
100 if (z->zfree == Z_NULL) z->zfree = zcfree;
101 if ((z->state = (struct internal_state FAR *)
102 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
103 return Z_MEM_ERROR;
104 z->state->blocks = Z_NULL;
105
106 /* handle undocumented nowrap option (no zlib header or check) */
107 z->state->nowrap = 0;
108 if (w < 0)
109 {
110 w = - w;
111 z->state->nowrap = 1;
112 }
113
114 /* set window size */
115 if (w < 8 || w > 15)
116 {
117 inflateEnd(z);
118 return Z_STREAM_ERROR;
119 }
120 z->state->wbits = (uInt)w;
121
122 /* create inflate_blocks state */
123 if ((z->state->blocks =
124 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
125 == Z_NULL)
126 {
127 inflateEnd(z);
128 return Z_MEM_ERROR;
129 }
130 Tracev((stderr, "inflate: allocated\n"));
131
132 /* reset state */
133 inflateReset(z);
134 return Z_OK;
135}
136 130
131 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
132 stream_size != (int)(sizeof(z_stream)))
133 return Z_VERSION_ERROR;
134 if (strm == Z_NULL) return Z_STREAM_ERROR;
135 strm->msg = Z_NULL; /* in case we return an error */
136 if (strm->zalloc == Z_NULL) {
137 strm->zalloc = zcalloc;
138 strm->opaque = (voidpf)0;
139 }
140 if (strm->zfree == Z_NULL) strm->zfree = zcfree;
141 state = (struct inflate_state FAR *)
142 ZALLOC(strm, 1, sizeof(struct inflate_state));
143 if (state == Z_NULL) return Z_MEM_ERROR;
144 Tracev((stderr, "inflate: allocated\n"));
145 strm->state = (voidpf)state;
146 if (windowBits < 0) {
147 state->wrap = 0;
148 windowBits = -windowBits;
149 }
150 else
151 state->wrap = 1;
152 if (windowBits < 8 || windowBits > 15) {
153 ZFREE(strm, state);
154 strm->state = Z_NULL;
155 return Z_STREAM_ERROR;
156 }
157 state->wbits = (unsigned)windowBits;
158 state->window = Z_NULL;
159 return inflateReset(strm);
160}
137 161
138int ZEXPORT inflateInit_(z, version, stream_size) 162int ZEXPORT inflateInit_(strm, version, stream_size)
139z_streamp z; 163z_streamp strm;
140const char *version; 164const char *version;
141int stream_size; 165int stream_size;
142{ 166{
143 return inflateInit2_(z, DEF_WBITS, version, stream_size); 167 return inflateInit2_(strm, DEF_WBITS, version, stream_size);
168}
169
170/*
171 Return state with length and distance decoding tables and index sizes set to
172 fixed code decoding. Normally this returns fixed tables from inffixed.h.
173 If BUILDFIXED is defined, then instead this routine builds the tables the
174 first time it's called, and returns those tables the first time and
175 thereafter. This reduces the size of the code by about 2K bytes, in
176 exchange for a little execution time. However, BUILDFIXED should not be
177 used for threaded applications, since the rewriting of the tables and virgin
178 may not be thread-safe.
179 */
180local void fixedtables(state)
181struct inflate_state FAR *state;
182{
183#ifdef BUILDFIXED
184 static int virgin = 1;
185 static code *lenfix, *distfix;
186 static code fixed[544];
187
188 /* build fixed huffman tables if first call (may not be thread safe) */
189 if (virgin) {
190 unsigned sym, bits;
191 static code *next;
192
193 /* literal/length table */
194 sym = 0;
195 while (sym < 144) state->lens[sym++] = 8;
196 while (sym < 256) state->lens[sym++] = 9;
197 while (sym < 280) state->lens[sym++] = 7;
198 while (sym < 288) state->lens[sym++] = 8;
199 next = fixed;
200 lenfix = next;
201 bits = 9;
202 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
203
204 /* distance table */
205 sym = 0;
206 while (sym < 32) state->lens[sym++] = 5;
207 distfix = next;
208 bits = 5;
209 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
210
211 /* do this just once */
212 virgin = 0;
213 }
214#else /* !BUILDFIXED */
215# include "inffixed.h"
216#endif /* BUILDFIXED */
217 state->lencode = lenfix;
218 state->lenbits = 9;
219 state->distcode = distfix;
220 state->distbits = 5;
144} 221}
145 222
223#ifdef MAKEFIXED
224#include <stdio.h>
146 225
147#define NEEDBYTE {if(z->avail_in==0)return r;r=f;} 226/*
148#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++) 227 Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
228 defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
229 those tables to stdout, which would be piped to inffixed.h. A small program
230 can simply call makefixed to do this:
149 231
150int ZEXPORT inflate(z, f) 232 void makefixed(void);
151z_streamp z; 233
152int f; 234 int main(void)
235 {
236 makefixed();
237 return 0;
238 }
239
240 Then that can be linked with zlib built with MAKEFIXED defined and run:
241
242 a.out > inffixed.h
243 */
244void makefixed()
153{ 245{
154 int r; 246 unsigned low, size;
155 uInt b; 247 struct inflate_state state;
156 248
157 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL) 249 fixedtables(&state);
158 return Z_STREAM_ERROR; 250 puts(" /* inffixed.h -- table for decoding fixed codes");
159 f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK; 251 puts(" * Generated automatically by makefixed().");
160 r = Z_BUF_ERROR; 252 puts(" */");
161 while (1) switch (z->state->mode) 253 puts("");
162 { 254 puts(" /* WARNING: this file should *not* be used by applications.");
163 case METHOD: 255 puts(" It is part of the implementation of this library and is");
164 NEEDBYTE 256 puts(" subject to change. Applications should only use zlib.h.");
165 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED) 257 puts(" */");
166 { 258 puts("");
167 z->state->mode = BAD; 259 size = 1U << 9;
168 z->msg = (char*)"unknown compression method"; 260 printf(" static const code lenfix[%u] = {", size);
169 z->state->sub.marker = 5; /* can't try inflateSync */ 261 low = 0;
170 break; 262 for (;;) {
171 } 263 if ((low % 7) == 0) printf("\n ");
172 if ((z->state->sub.method >> 4) + 8 > z->state->wbits) 264 printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
173 { 265 state.lencode[low].val);
174 z->state->mode = BAD; 266 if (++low == size) break;
175 z->msg = (char*)"invalid window size"; 267 putchar(',');
176 z->state->sub.marker = 5; /* can't try inflateSync */ 268 }
177 break; 269 puts("\n };");
178 } 270 size = 1U << 5;
179 z->state->mode = FLAG; 271 printf("\n static const code distfix[%u] = {", size);
180 case FLAG: 272 low = 0;
181 NEEDBYTE 273 for (;;) {
182 b = NEXTBYTE; 274 if ((low % 6) == 0) printf("\n ");
183 if (((z->state->sub.method << 8) + b) % 31) 275 printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
184 { 276 state.distcode[low].val);
185 z->state->mode = BAD; 277 if (++low == size) break;
186 z->msg = (char*)"incorrect header check"; 278 putchar(',');
187 z->state->sub.marker = 5; /* can't try inflateSync */ 279 }
188 break; 280 puts("\n };");
189 } 281}
190 Tracev((stderr, "inflate: zlib header ok\n")); 282#endif /* MAKEFIXED */
191 if (!(b & PRESET_DICT)) 283
192 { 284/*
193 z->state->mode = BLOCKS; 285 Update the window with the last wsize (normally 32K) bytes written before
194 break; 286 returning. If window does not exist yet, create it. This is only called
195 } 287 when a window is already in use, or when output has been written during this
196 z->state->mode = DICT4; 288 inflate call, but the end of the deflate stream has not been reached yet.
197 case DICT4: 289 It is also called to create a window for dictionary data when a dictionary
198 NEEDBYTE 290 is loaded.
199 z->state->sub.check.need = (uLong)NEXTBYTE << 24; 291
200 z->state->mode = DICT3; 292 Providing output buffers larger than 32K to inflate() should provide a speed
201 case DICT3: 293 advantage, since only the last 32K of output is copied to the sliding window
202 NEEDBYTE 294 upon return from inflate(), and since all distances after the first 32K of
203 z->state->sub.check.need += (uLong)NEXTBYTE << 16; 295 output will fall in the output data, making match copies simpler and faster.
204 z->state->mode = DICT2; 296 The advantage may be dependent on the size of the processor's data caches.
205 case DICT2: 297 */
206 NEEDBYTE 298local int updatewindow(strm, out)
207 z->state->sub.check.need += (uLong)NEXTBYTE << 8; 299z_streamp strm;
208 z->state->mode = DICT1; 300unsigned out;
209 case DICT1: 301{
210 NEEDBYTE 302 struct inflate_state FAR *state;
211 z->state->sub.check.need += (uLong)NEXTBYTE; 303 unsigned copy, dist;
212 z->adler = z->state->sub.check.need; 304
213 z->state->mode = DICT0; 305 state = (struct inflate_state FAR *)strm->state;
214 return Z_NEED_DICT; 306
215 case DICT0: 307 /* if it hasn't been done already, allocate space for the window */
216 z->state->mode = BAD; 308 if (state->window == Z_NULL) {
217 z->msg = (char*)"need dictionary"; 309 state->window = (unsigned char FAR *)
218 z->state->sub.marker = 0; /* can try inflateSync */ 310 ZALLOC(strm, 1U << state->wbits,
219 return Z_STREAM_ERROR; 311 sizeof(unsigned char));
220 case BLOCKS: 312 if (state->window == Z_NULL) return 1;
221 r = inflate_blocks(z->state->blocks, z, r); 313 }
222 if (r == Z_DATA_ERROR) 314
223 { 315 /* if window not in use yet, initialize */
224 z->state->mode = BAD; 316 if (state->wsize == 0) {
225 z->state->sub.marker = 0; /* can try inflateSync */ 317 state->wsize = 1U << state->wbits;
226 break; 318 state->write = 0;
227 } 319 }
228 if (r == Z_OK) 320
229 r = f; 321 /* copy state->wsize or less output bytes into the circular window */
230 if (r != Z_STREAM_END) 322 copy = out - strm->avail_out;
231 return r; 323 if (copy >= state->wsize) {
232 r = f; 324 zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
233 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was); 325 state->write = 0;
234 if (z->state->nowrap) 326 }
235 { 327 else {
236 z->state->mode = DONE; 328 dist = state->wsize - state->write;
237 break; 329 if (dist > copy) dist = copy;
238 } 330 zmemcpy(state->window + state->write, strm->next_out - copy, dist);
239 z->state->mode = CHECK4; 331 copy -= dist;
240 case CHECK4: 332 if (copy) {
241 NEEDBYTE 333 zmemcpy(state->window, strm->next_out - copy, copy);
242 z->state->sub.check.need = (uLong)NEXTBYTE << 24; 334 state->write = copy;
243 z->state->mode = CHECK3; 335 }
244 case CHECK3: 336 else {
245 NEEDBYTE 337 state->write += dist;
246 z->state->sub.check.need += (uLong)NEXTBYTE << 16; 338 if (state->write == state->wsize) state->write = 0;
247 z->state->mode = CHECK2; 339 }
248 case CHECK2: 340 }
249 NEEDBYTE 341 return 0;
250 z->state->sub.check.need += (uLong)NEXTBYTE << 8; 342}
251 z->state->mode = CHECK1; 343
252 case CHECK1: 344/* Macros for inflate(): */
253 NEEDBYTE 345
254 z->state->sub.check.need += (uLong)NEXTBYTE; 346/* check function to use adler32() for zlib or crc32() for gzip */
255 347#ifdef GUNZIP
256 if (z->state->sub.check.was != z->state->sub.check.need) 348# define UPDATE(check, buf, len) \
257 { 349 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
258 z->state->mode = BAD; 350#else
259 z->msg = (char*)"incorrect data check"; 351# define UPDATE(check, buf, len) adler32(check, buf, len)
260 z->state->sub.marker = 5; /* can't try inflateSync */ 352#endif
353
354/* check macros for header crc */
355#ifdef GUNZIP
356# define CRC2(check, word) \
357 do { \
358 hbuf[0] = (unsigned char)(word); \
359 hbuf[1] = (unsigned char)((word) >> 8); \
360 check = crc32(check, hbuf, 2); \
361 } while (0)
362
363# define CRC4(check, word) \
364 do { \
365 hbuf[0] = (unsigned char)(word); \
366 hbuf[1] = (unsigned char)((word) >> 8); \
367 hbuf[2] = (unsigned char)((word) >> 16); \
368 hbuf[3] = (unsigned char)((word) >> 24); \
369 check = crc32(check, hbuf, 4); \
370 } while (0)
371#endif
372
373/* Load registers with state in inflate() for speed */
374#define LOAD() \
375 do { \
376 put = strm->next_out; \
377 left = strm->avail_out; \
378 next = strm->next_in; \
379 have = strm->avail_in; \
380 hold = state->hold; \
381 bits = state->bits; \
382 } while (0)
383
384/* Restore state from registers in inflate() */
385#define RESTORE() \
386 do { \
387 strm->next_out = put; \
388 strm->avail_out = left; \
389 strm->next_in = next; \
390 strm->avail_in = have; \
391 state->hold = hold; \
392 state->bits = bits; \
393 } while (0)
394
395/* Clear the input bit accumulator */
396#define INITBITS() \
397 do { \
398 hold = 0; \
399 bits = 0; \
400 } while (0)
401
402/* Get a byte of input into the bit accumulator, or return from inflate()
403 if there is no input available. */
404#define PULLBYTE() \
405 do { \
406 if (have == 0) goto leave; \
407 have--; \
408 hold += (unsigned long)(*next++) << bits; \
409 bits += 8; \
410 } while (0)
411
412/* Assure that there are at least n bits in the bit accumulator. If there is
413 not enough available input to do that, then return from inflate(). */
414#define NEEDBITS(n) \
415 do { \
416 while (bits < (unsigned)(n)) \
417 PULLBYTE(); \
418 } while (0)
419
420/* Return the low n bits of the bit accumulator (n < 16) */
421#define BITS(n) \
422 ((unsigned)hold & ((1U << (n)) - 1))
423
424/* Remove n bits from the bit accumulator */
425#define DROPBITS(n) \
426 do { \
427 hold >>= (n); \
428 bits -= (unsigned)(n); \
429 } while (0)
430
431/* Remove zero to seven bits as needed to go to a byte boundary */
432#define BYTEBITS() \
433 do { \
434 hold >>= bits & 7; \
435 bits -= bits & 7; \
436 } while (0)
437
438/* Reverse the bytes in a 32-bit value */
439#define REVERSE(q) \
440 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
441 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
442
443/*
444 inflate() uses a state machine to process as much input data and generate as
445 much output data as possible before returning. The state machine is
446 structured roughly as follows:
447
448 for (;;) switch (state) {
449 ...
450 case STATEn:
451 if (not enough input data or output space to make progress)
452 return;
453 ... make progress ...
454 state = STATEm;
261 break; 455 break;
262 } 456 ...
263 Tracev((stderr, "inflate: zlib check ok\n")); 457 }
264 z->state->mode = DONE; 458
265 case DONE: 459 so when inflate() is called again, the same case is attempted again, and
266 return Z_STREAM_END; 460 if the appropriate resources are provided, the machine proceeds to the
267 case BAD: 461 next state. The NEEDBITS() macro is usually the way the state evaluates
268 return Z_DATA_ERROR; 462 whether it can proceed or should return. NEEDBITS() does the return if
269 default: 463 the requested bits are not available. The typical use of the BITS macros
270 return Z_STREAM_ERROR; 464 is:
271 } 465
272#ifdef NEED_DUMMY_RETURN 466 NEEDBITS(n);
273 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */ 467 ... do something with BITS(n) ...
468 DROPBITS(n);
469
470 where NEEDBITS(n) either returns from inflate() if there isn't enough
471 input left to load n bits into the accumulator, or it continues. BITS(n)
472 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
473 the low n bits off the accumulator. INITBITS() clears the accumulator
474 and sets the number of available bits to zero. BYTEBITS() discards just
475 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
476 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
477
478 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
479 if there is no input available. The decoding of variable length codes uses
480 PULLBYTE() directly in order to pull just enough bytes to decode the next
481 code, and no more.
482
483 Some states loop until they get enough input, making sure that enough
484 state information is maintained to continue the loop where it left off
485 if NEEDBITS() returns in the loop. For example, want, need, and keep
486 would all have to actually be part of the saved state in case NEEDBITS()
487 returns:
488
489 case STATEw:
490 while (want < need) {
491 NEEDBITS(n);
492 keep[want++] = BITS(n);
493 DROPBITS(n);
494 }
495 state = STATEx;
496 case STATEx:
497
498 As shown above, if the next state is also the next case, then the break
499 is omitted.
500
501 A state may also return if there is not enough output space available to
502 complete that state. Those states are copying stored data, writing a
503 literal byte, and copying a matching string.
504
505 When returning, a "goto leave" is used to update the total counters, update
506 the check value, and determine whether any progress has been made during
507 that inflate() call in order to return the proper return code. Progress is
508 defined as a change in either strm->avail_in or strm->avail_out. When there
509 is a window, goto leave will update the window with the last output written.
510 If a goto leave occurs in the middle of decompression and there is no window
511 currently, goto leave will create one and copy output to the window for the
512 next call of inflate().
513
514 In this implementation, the flush parameter of inflate() only affects the
515 return code (per zlib.h). inflate() always writes as much as possible to
516 strm->next_out, given the space available and the provided input--the effect
517 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
518 the allocation of and copying into a sliding window until necessary, which
519 provides the effect documented in zlib.h for Z_FINISH when the entire input
520 stream available. So the only thing the flush parameter actually does is:
521 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
522 will return Z_BUF_ERROR if it has not reached the end of the stream.
523 */
524
525int ZEXPORT inflate(strm, flush)
526z_streamp strm;
527int flush;
528{
529 struct inflate_state FAR *state;
530 unsigned char *next, *put; /* next input and output */
531 unsigned have, left; /* available input and output */
532 unsigned long hold; /* bit buffer */
533 unsigned bits; /* bits in bit buffer */
534 unsigned in, out; /* save starting available input and output */
535 unsigned copy; /* number of stored or match bytes to copy */
536 unsigned char *from; /* where to copy match bytes from */
537 code this; /* current decoding table entry */
538 code last; /* parent table entry */
539 unsigned len; /* length to copy for repeats, bits to drop */
540 int ret; /* return code */
541#ifdef GUNZIP
542 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
543#endif
544 static const unsigned short order[19] = /* permutation of code lengths */
545 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
546
547 if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
548 (strm->next_in == Z_NULL && strm->avail_in != 0))
549 return Z_STREAM_ERROR;
550
551 state = (struct inflate_state FAR *)strm->state;
552 LOAD();
553 in = have;
554 out = left;
555 ret = Z_OK;
556 for (;;)
557 switch (state->mode) {
558 case HEAD:
559 if (state->wrap == 0) {
560 state->mode = TYPE;
561 break;
562 }
563 NEEDBITS(16);
564#ifdef GUNZIP
565 if (hold == 0x8b1f) { /* gzip header */
566 state->check = crc32(0L, Z_NULL, 0);
567 CRC2(state->check, hold);
568 INITBITS();
569 state->mode = FLAGS;
570 break;
571 }
572 state->flags = 0; /* expect zlib header */
573#endif
574 if (((BITS(8) << 8) + (hold >> 8)) % 31) {
575 strm->msg = (char *)"incorrect header check";
576 state->mode = BAD;
577 break;
578 }
579 if (BITS(4) != Z_DEFLATED) {
580 strm->msg = (char *)"unknown compression method";
581 state->mode = BAD;
582 break;
583 }
584 DROPBITS(4);
585 if (BITS(4) + 8 > state->wbits) {
586 strm->msg = (char *)"invalid window size";
587 state->mode = BAD;
588 break;
589 }
590 Tracev((stderr, "inflate: zlib header ok\n"));
591 strm->adler = state->check = adler32(0L, Z_NULL, 0);
592 state->mode = hold & 0x200 ? DICTID : TYPE;
593 INITBITS();
594 break;
595#ifdef GUNZIP
596 case FLAGS:
597 NEEDBITS(16);
598 state->flags = (int)(hold);
599 if ((state->flags & 0xff) != Z_DEFLATED) {
600 strm->msg = (char *)"unknown compression method";
601 state->mode = BAD;
602 break;
603 }
604 if (state->flags & 0xe000) {
605 strm->msg = (char *)"unknown header flags set";
606 state->mode = BAD;
607 break;
608 }
609 if (state->flags & 0x0200) CRC2(state->check, hold);
610 INITBITS();
611 state->mode = TIME;
612 case TIME:
613 NEEDBITS(32);
614 if (state->flags & 0x0200) CRC4(state->check, hold);
615 INITBITS();
616 state->mode = OS;
617 case OS:
618 NEEDBITS(16);
619 if (state->flags & 0x0200) CRC2(state->check, hold);
620 INITBITS();
621 state->mode = EXLEN;
622 case EXLEN:
623 if (state->flags & 0x0400) {
624 NEEDBITS(16);
625 state->length = (unsigned)(hold);
626 if (state->flags & 0x0200) CRC2(state->check, hold);
627 INITBITS();
628 }
629 state->mode = EXTRA;
630 case EXTRA:
631 if (state->flags & 0x0400) {
632 copy = state->length;
633 if (copy > have) copy = have;
634 if (copy) {
635 if (state->flags & 0x0200)
636 state->check = crc32(state->check, next, copy);
637 have -= copy;
638 next += copy;
639 state->length -= copy;
640 }
641 if (state->length) goto leave;
642 }
643 state->mode = NAME;
644 case NAME:
645 if (state->flags & 0x0800) {
646 if (have == 0) goto leave;
647 copy = 0;
648 do {
649 len = (unsigned)(next[copy++]);
650 } while (len && copy < have);
651 if (state->flags & 0x02000)
652 state->check = crc32(state->check, next, copy);
653 have -= copy;
654 next += copy;
655 if (len) goto leave;
656 }
657 state->mode = COMMENT;
658 case COMMENT:
659 if (state->flags & 0x1000) {
660 if (have == 0) goto leave;
661 copy = 0;
662 do {
663 len = (unsigned)(next[copy++]);
664 } while (len && copy < have);
665 if (state->flags & 0x02000)
666 state->check = crc32(state->check, next, copy);
667 have -= copy;
668 next += copy;
669 if (len) goto leave;
670 }
671 state->mode = HCRC;
672 case HCRC:
673 if (state->flags & 0x0200) {
674 NEEDBITS(16);
675 if (hold != (state->check & 0xffff)) {
676 strm->msg = (char *)"header crc mismatch";
677 state->mode = BAD;
678 break;
679 }
680 INITBITS();
681 }
682 strm->adler = state->check = crc32(0L, Z_NULL, 0);
683 state->mode = TYPE;
684 break;
685#endif
686 case DICTID:
687 NEEDBITS(32);
688 strm->adler = state->check = REVERSE(hold);
689 INITBITS();
690 state->mode = DICT;
691 case DICT:
692 if (state->havedict == 0) {
693 RESTORE();
694 return Z_NEED_DICT;
695 }
696 strm->adler = state->check = adler32(0L, Z_NULL, 0);
697 state->mode = TYPE;
698 case TYPE:
699 if (state->last) {
700 BYTEBITS();
701 state->mode = CHECK;
702 break;
703 }
704 NEEDBITS(3);
705 state->last = BITS(1);
706 DROPBITS(1);
707 switch (BITS(2)) {
708 case 0: /* stored block */
709 Tracev((stderr, "inflate: stored block%s\n",
710 state->last ? " (last)" : ""));
711 state->mode = STORED;
712 break;
713 case 1: /* fixed block */
714 fixedtables(state);
715 Tracev((stderr, "inflate: fixed codes block%s\n",
716 state->last ? " (last)" : ""));
717 state->mode = LEN; /* decode codes */
718 break;
719 case 2: /* dynamic block */
720 Tracev((stderr, "inflate: dynamic codes block%s\n",
721 state->last ? " (last)" : ""));
722 state->mode = TABLE;
723 break;
724 case 3:
725 strm->msg = (char *)"invalid block type";
726 state->mode = BAD;
727 }
728 DROPBITS(2);
729 break;
730 case STORED:
731 BYTEBITS(); /* go to byte boundary */
732 NEEDBITS(32);
733 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
734 strm->msg = (char *)"invalid stored block lengths";
735 state->mode = BAD;
736 break;
737 }
738 state->length = (unsigned)hold & 0xffff;
739 Tracev((stderr, "inflate: stored length %u\n",
740 state->length));
741 INITBITS();
742 state->mode = COPY;
743 case COPY:
744 copy = state->length;
745 if (copy) {
746 if (copy > have) copy = have;
747 if (copy > left) copy = left;
748 if (copy == 0) goto leave;
749 zmemcpy(put, next, copy);
750 have -= copy;
751 next += copy;
752 left -= copy;
753 put += copy;
754 state->length -= copy;
755 break;
756 }
757 Tracev((stderr, "inflate: stored end\n"));
758 state->mode = TYPE;
759 break;
760 case TABLE:
761 NEEDBITS(14);
762 state->nlen = BITS(5) + 257;
763 DROPBITS(5);
764 state->ndist = BITS(5) + 1;
765 DROPBITS(5);
766 state->ncode = BITS(4) + 4;
767 DROPBITS(4);
768#ifndef PKZIP_BUG_WORKAROUND
769 if (state->nlen > 286 || state->ndist > 30) {
770 strm->msg = (char *)"too many length or distance symbols";
771 state->mode = BAD;
772 break;
773 }
774#endif
775 Tracev((stderr, "inflate: table sizes ok\n"));
776 state->have = 0;
777 state->mode = LENLENS;
778 case LENLENS:
779 while (state->have < state->ncode) {
780 NEEDBITS(3);
781 state->lens[order[state->have++]] = (unsigned short)BITS(3);
782 DROPBITS(3);
783 }
784 while (state->have < 19)
785 state->lens[order[state->have++]] = 0;
786 state->next = state->codes;
787 state->lencode = (code const FAR *)(state->next);
788 state->lenbits = 7;
789 ret = inflate_table(CODES, state->lens, 19, &(state->next),
790 &(state->lenbits), state->work);
791 if (ret) {
792 strm->msg = (char *)"invalid code lengths set";
793 state->mode = BAD;
794 break;
795 }
796 Tracev((stderr, "inflate: code lengths ok\n"));
797 state->have = 0;
798 state->mode = CODELENS;
799 case CODELENS:
800 while (state->have < state->nlen + state->ndist) {
801 for (;;) {
802 this = state->lencode[BITS(state->lenbits)];
803 if ((unsigned)(this.bits) <= bits) break;
804 PULLBYTE();
805 }
806 if (this.val < 16) {
807 NEEDBITS(this.bits);
808 DROPBITS(this.bits);
809 state->lens[state->have++] = this.val;
810 }
811 else {
812 if (this.val == 16) {
813 NEEDBITS(this.bits + 2);
814 DROPBITS(this.bits);
815 if (state->have == 0) {
816 strm->msg = (char *)"invalid bit length repeat";
817 state->mode = BAD;
818 break;
819 }
820 len = state->lens[state->have - 1];
821 copy = 3 + BITS(2);
822 DROPBITS(2);
823 }
824 else if (this.val == 17) {
825 NEEDBITS(this.bits + 3);
826 DROPBITS(this.bits);
827 len = 0;
828 copy = 3 + BITS(3);
829 DROPBITS(3);
830 }
831 else {
832 NEEDBITS(this.bits + 7);
833 DROPBITS(this.bits);
834 len = 0;
835 copy = 11 + BITS(7);
836 DROPBITS(7);
837 }
838 if (state->have + copy > state->nlen + state->ndist) {
839 strm->msg = (char *)"invalid bit length repeat";
840 state->mode = BAD;
841 break;
842 }
843 while (copy--)
844 state->lens[state->have++] = (unsigned short)len;
845 }
846 }
847
848 /* build code tables */
849 state->next = state->codes;
850 state->lencode = (code const FAR *)(state->next);
851 state->lenbits = 9;
852 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
853 &(state->lenbits), state->work);
854 if (ret) {
855 strm->msg = (char *)"invalid literal/lengths set";
856 state->mode = BAD;
857 break;
858 }
859 state->distcode = (code const FAR *)(state->next);
860 state->distbits = 6;
861 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
862 &(state->next), &(state->distbits), state->work);
863 if (ret) {
864 strm->msg = (char *)"invalid distances set";
865 state->mode = BAD;
866 break;
867 }
868 Tracev((stderr, "inflate: codes ok\n"));
869 state->mode = LEN;
870 case LEN:
871 if (have >= 6 && left >= 258) {
872 RESTORE();
873 inflate_fast(strm, out);
874 LOAD();
875 break;
876 }
877 for (;;) {
878 this = state->lencode[BITS(state->lenbits)];
879 if ((unsigned)(this.bits) <= bits) break;
880 PULLBYTE();
881 }
882 if (this.op && (this.op & 0xf0) == 0) {
883 last = this;
884 for (;;) {
885 this = state->lencode[last.val +
886 (BITS(last.bits + last.op) >> last.bits)];
887 if ((unsigned)(last.bits + this.bits) <= bits) break;
888 PULLBYTE();
889 }
890 DROPBITS(last.bits);
891 }
892 DROPBITS(this.bits);
893 state->length = (unsigned)this.val;
894 if ((int)(this.op) == 0) {
895 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
896 "inflate: literal '%c'\n" :
897 "inflate: literal 0x%02x\n", this.val));
898 state->mode = LIT;
899 break;
900 }
901 if (this.op & 32) {
902 Tracevv((stderr, "inflate: end of block\n"));
903 state->mode = TYPE;
904 break;
905 }
906 if (this.op & 64) {
907 strm->msg = (char *)"invalid literal/length code";
908 state->mode = BAD;
909 break;
910 }
911 state->extra = (unsigned)(this.op) & 15;
912 state->mode = LENEXT;
913 case LENEXT:
914 if (state->extra) {
915 NEEDBITS(state->extra);
916 state->length += BITS(state->extra);
917 DROPBITS(state->extra);
918 }
919 Tracevv((stderr, "inflate: length %u\n", state->length));
920 state->mode = DIST;
921 case DIST:
922 for (;;) {
923 this = state->distcode[BITS(state->distbits)];
924 if ((unsigned)(this.bits) <= bits) break;
925 PULLBYTE();
926 }
927 if ((this.op & 0xf0) == 0) {
928 last = this;
929 for (;;) {
930 this = state->distcode[last.val +
931 (BITS(last.bits + last.op) >> last.bits)];
932 if ((unsigned)(last.bits + this.bits) <= bits) break;
933 PULLBYTE();
934 }
935 DROPBITS(last.bits);
936 }
937 DROPBITS(this.bits);
938 if (this.op & 64) {
939 strm->msg = (char *)"invalid distance code";
940 state->mode = BAD;
941 break;
942 }
943 state->offset = (unsigned)this.val;
944 state->extra = (unsigned)(this.op) & 15;
945 state->mode = DISTEXT;
946 case DISTEXT:
947 if (state->extra) {
948 NEEDBITS(state->extra);
949 state->offset += BITS(state->extra);
950 DROPBITS(state->extra);
951 }
952 if (state->offset > (state->wsize ? state->wsize :
953 out - left)) {
954 strm->msg = (char *)"invalid distance too far back";
955 state->mode = BAD;
956 break;
957 }
958 Tracevv((stderr, "inflate: distance %u\n", state->offset));
959 state->mode = MATCH;
960 case MATCH:
961 if (left == 0) goto leave;
962 copy = out - left;
963 if (state->offset > copy) { /* copy from window */
964 copy = state->offset - copy;
965 if (copy > state->write) {
966 copy -= state->write;
967 from = state->window + (state->wsize - copy);
968 }
969 else
970 from = state->window + (state->write - copy);
971 if (copy > state->length) copy = state->length;
972 }
973 else { /* copy from output */
974 from = put - state->offset;
975 copy = state->length;
976 }
977 if (copy > left) copy = left;
978 left -= copy;
979 state->length -= copy;
980 do {
981 *put++ = *from++;
982 } while (--copy);
983 if (state->length == 0) state->mode = LEN;
984 break;
985 case LIT:
986 if (left == 0) goto leave;
987 *put++ = (unsigned char)(state->length);
988 left--;
989 state->mode = LEN;
990 break;
991 case CHECK:
992 if (state->wrap) {
993 NEEDBITS(32);
994 out -= left;
995 strm->total_out += out;
996 state->total += out;
997 if (out)
998 strm->adler = state->check =
999 UPDATE(state->check, put - out, out);
1000 out = left;
1001 if ((
1002#ifdef GUNZIP
1003 state->flags ? hold :
274#endif 1004#endif
1005 REVERSE(hold)) != state->check) {
1006 strm->msg = (char *)"incorrect data check";
1007 state->mode = BAD;
1008 break;
1009 }
1010 INITBITS();
1011 Tracev((stderr, "inflate: check matches trailer\n"));
1012 }
1013#ifdef GUNZIP
1014 state->mode = LENGTH;
1015 case LENGTH:
1016 if (state->wrap && state->flags) {
1017 NEEDBITS(32);
1018 if (hold != (state->total & 0xffffffff)) {
1019 strm->msg = (char *)"incorrect length check";
1020 state->mode = BAD;
1021 break;
1022 }
1023 INITBITS();
1024 Tracev((stderr, "inflate: length matches trailer\n"));
1025 }
1026#endif
1027 state->mode = DONE;
1028 case DONE:
1029 ret = Z_STREAM_END;
1030 goto leave;
1031 case BAD:
1032 ret = Z_DATA_ERROR;
1033 goto leave;
1034 case MEM:
1035 return Z_MEM_ERROR;
1036 case SYNC:
1037 default:
1038 return Z_STREAM_ERROR;
1039 }
1040
1041 /*
1042 Return from inflate(), updating the total counts and the check value.
1043 If there was no progress during the inflate() call, return a buffer
1044 error. Call updatewindow() to create and/or update the window state.
1045 Note: a memory error from inflate() is non-recoverable.
1046 */
1047 leave:
1048 RESTORE();
1049 if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
1050 if (updatewindow(strm, out)) {
1051 state->mode = MEM;
1052 return Z_MEM_ERROR;
1053 }
1054 in -= strm->avail_in;
1055 out -= strm->avail_out;
1056 strm->total_in += in;
1057 strm->total_out += out;
1058 state->total += out;
1059 if (state->wrap && out)
1060 strm->adler = state->check =
1061 UPDATE(state->check, strm->next_out - out, out);
1062 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1063 ret = Z_BUF_ERROR;
1064 return ret;
275} 1065}
276 1066
1067int ZEXPORT inflateEnd(strm)
1068z_streamp strm;
1069{
1070 struct inflate_state FAR *state;
1071 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == Z_NULL)
1072 return Z_STREAM_ERROR;
1073 state = (struct inflate_state FAR *)strm->state;
1074 if (state->window != Z_NULL) ZFREE(strm, state->window);
1075 ZFREE(strm, strm->state);
1076 strm->state = Z_NULL;
1077 Tracev((stderr, "inflate: end\n"));
1078 return Z_OK;
1079}
277 1080
278int ZEXPORT inflateSetDictionary(z, dictionary, dictLength) 1081int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
279z_streamp z; 1082z_streamp strm;
280const Bytef *dictionary; 1083const Bytef *dictionary;
281uInt dictLength; 1084uInt dictLength;
282{ 1085{
283 uInt length = dictLength; 1086 struct inflate_state FAR *state;
284 1087 unsigned long id;
285 if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
286 return Z_STREAM_ERROR;
287
288 if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
289 z->adler = 1L;
290
291 if (length >= ((uInt)1<<z->state->wbits))
292 {
293 length = (1<<z->state->wbits)-1;
294 dictionary += dictLength - length;
295 }
296 inflate_set_dictionary(z->state->blocks, dictionary, length);
297 z->state->mode = BLOCKS;
298 return Z_OK;
299}
300 1088
1089 /* check state */
1090 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1091 state = (struct inflate_state FAR *)strm->state;
1092 if (state->mode != DICT) return Z_STREAM_ERROR;
301 1093
302int ZEXPORT inflateSync(z) 1094 /* check for correct dictionary id */
303z_streamp z; 1095 id = adler32(0L, Z_NULL, 0);
304{ 1096 id = adler32(id, dictionary, dictLength);
305 uInt n; /* number of bytes to look at */ 1097 if (id != state->check) return Z_DATA_ERROR;
306 Bytef *p; /* pointer to bytes */ 1098
307 uInt m; /* number of marker bytes found in a row */ 1099 /* copy dictionary to window */
308 uLong r, w; /* temporaries to save total_in and total_out */ 1100 if (updatewindow(strm, strm->avail_out)) {
309 1101 state->mode = MEM;
310 /* set up */ 1102 return Z_MEM_ERROR;
311 if (z == Z_NULL || z->state == Z_NULL) 1103 }
312 return Z_STREAM_ERROR; 1104 if (dictLength > state->wsize)
313 if (z->state->mode != BAD) 1105 zmemcpy(state->window, dictionary + dictLength - state->wsize,
314 { 1106 state->wsize);
315 z->state->mode = BAD;
316 z->state->sub.marker = 0;
317 }
318 if ((n = z->avail_in) == 0)
319 return Z_BUF_ERROR;
320 p = z->next_in;
321 m = z->state->sub.marker;
322
323 /* search */
324 while (n && m < 4)
325 {
326 static const Byte mark[4] = {0, 0, 0xff, 0xff};
327 if (*p == mark[m])
328 m++;
329 else if (*p)
330 m = 0;
331 else 1107 else
332 m = 4 - m; 1108 zmemcpy(state->window + state->wsize - dictLength, dictionary,
333 p++, n--; 1109 dictLength);
334 } 1110 state->havedict = 1;
335 1111 Tracev((stderr, "inflate: dictionary set\n"));
336 /* restore */ 1112 return Z_OK;
337 z->total_in += p - z->next_in;
338 z->next_in = p;
339 z->avail_in = n;
340 z->state->sub.marker = m;
341
342 /* return no joy or set up to restart on a new block */
343 if (m != 4)
344 return Z_DATA_ERROR;
345 r = z->total_in; w = z->total_out;
346 inflateReset(z);
347 z->total_in = r; z->total_out = w;
348 z->state->mode = BLOCKS;
349 return Z_OK;
350} 1113}
351 1114
1115/*
1116 Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1117 or when out of input. When called, *have is the number of pattern bytes
1118 found in order so far, in 0..3. On return *have is updated to the new
1119 state. If on return *have equals four, then the pattern was found and the
1120 return value is how many bytes were read including the last byte of the
1121 pattern. If *have is less than four, then the pattern has not been found
1122 yet and the return value is len. In the latter case, syncsearch() can be
1123 called again with more data and the *have state. *have is initialized to
1124 zero for the first call.
1125 */
1126local unsigned syncsearch(have, buf, len)
1127unsigned *have;
1128unsigned char FAR *buf;
1129unsigned len;
1130{
1131 unsigned got;
1132 unsigned next;
1133
1134 got = *have;
1135 next = 0;
1136 while (next < len && got < 4) {
1137 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1138 got++;
1139 else if (buf[next])
1140 got = 0;
1141 else
1142 got = 4 - got;
1143 next++;
1144 }
1145 *have = got;
1146 return next;
1147}
1148
1149int ZEXPORT inflateSync(strm)
1150z_streamp strm;
1151{
1152 unsigned len; /* number of bytes to look at or looked at */
1153 unsigned long in, out; /* temporary to save total_in and total_out */
1154 unsigned char buf[4]; /* to restore bit buffer to byte string */
1155 struct inflate_state FAR *state;
352 1156
353/* Returns true if inflate is currently at the end of a block generated 1157 /* check parameters */
354 * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP 1158 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
355 * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH 1159 state = (struct inflate_state FAR *)strm->state;
356 * but removes the length bytes of the resulting empty stored block. When 1160 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
357 * decompressing, PPP checks that at the end of input packet, inflate is 1161
358 * waiting for these length bytes. 1162 /* if first time, start search in bit buffer */
1163 if (state->mode != SYNC) {
1164 state->mode = SYNC;
1165 state->hold <<= state->bits & 7;
1166 state->bits -= state->bits & 7;
1167 len = 0;
1168 while (state->bits >= 8) {
1169 buf[len++] = (unsigned char)(state->hold);
1170 state->hold >>= 8;
1171 state->bits -= 8;
1172 }
1173 state->have = 0;
1174 syncsearch(&(state->have), buf, len);
1175 }
1176
1177 /* search available input */
1178 len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1179 strm->avail_in -= len;
1180 strm->next_in += len;
1181 strm->total_in += len;
1182
1183 /* return no joy or set up to restart inflate() on a new block */
1184 if (state->have != 4) return Z_DATA_ERROR;
1185 in = strm->total_in; out = strm->total_out;
1186 inflateReset(strm);
1187 strm->total_in = in; strm->total_out = out;
1188 state->mode = TYPE;
1189 return Z_OK;
1190}
1191
1192/*
1193 Returns true if inflate is currently at the end of a block generated by
1194 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1195 implementation to provide an additional safety check. PPP uses
1196 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1197 block. When decompressing, PPP checks that at the end of input packet,
1198 inflate is waiting for these length bytes.
359 */ 1199 */
360int ZEXPORT inflateSyncPoint(z) 1200int ZEXPORT inflateSyncPoint(strm)
361z_streamp z; 1201z_streamp strm;
1202{
1203 struct inflate_state FAR *state;
1204
1205 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1206 state = (struct inflate_state FAR *)strm->state;
1207 return state->mode == STORED && state->bits == 0;
1208}
1209
1210int ZEXPORT inflateCopy(dest, source)
1211z_streamp dest;
1212z_streamp source;
362{ 1213{
363 if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL) 1214 struct inflate_state FAR *state;
364 return Z_STREAM_ERROR; 1215 struct inflate_state FAR *copy;
365 return inflate_blocks_sync_point(z->state->blocks); 1216 unsigned char FAR *window;
1217
1218 /* check input */
1219 if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
1220 source->zalloc == Z_NULL || source->zfree == Z_NULL)
1221 return Z_STREAM_ERROR;
1222 state = (struct inflate_state FAR *)source->state;
1223
1224 /* allocate space */
1225 copy = (struct inflate_state FAR *)
1226 ZALLOC(source, 1, sizeof(struct inflate_state));
1227 if (copy == Z_NULL) return Z_MEM_ERROR;
1228 window = Z_NULL;
1229 if (state->window != Z_NULL) {
1230 window = (unsigned char FAR *)
1231 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1232 if (window == Z_NULL) {
1233 ZFREE(source, copy);
1234 return Z_MEM_ERROR;
1235 }
1236 }
1237
1238 /* copy state */
1239 *dest = *source;
1240 *copy = *state;
1241 copy->lencode = copy->codes + (state->lencode - state->codes);
1242 copy->distcode = copy->codes + (state->distcode - state->codes);
1243 copy->next = copy->codes + (state->next - state->codes);
1244 if (window != Z_NULL)
1245 zmemcpy(window, state->window, 1U << state->wbits);
1246 copy->window = window;
1247 dest->state = (voidpf)copy;
1248 return Z_OK;
366} 1249}
diff --git a/inflate.h b/inflate.h
new file mode 100644
index 0000000..5bcc82b
--- /dev/null
+++ b/inflate.h
@@ -0,0 +1,115 @@
1/* inflate.h -- internal inflate state definition
2 * Copyright (C) 1995-2003 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* WARNING: this file should *not* be used by applications. It is
7 part of the implementation of the compression library and is
8 subject to change. Applications should only use zlib.h.
9 */
10
11/* define NO_GUNZIP when compiling if you want to disable gzip header and
12 trailer decoding by inflate(). NO_GUNZIP would be used to avoid linking in
13 the crc code when it is not needed. For shared libraries, gzip decoding
14 should be left enabled. */
15#ifndef NO_GUNZIP
16# define GUNZIP
17#endif
18
19/* Possible inflate modes between inflate() calls */
20typedef enum {
21 HEAD, /* i: waiting for magic header */
22#ifdef GUNZIP
23 FLAGS, /* i: waiting for method and flags (gzip) */
24 TIME, /* i: waiting for modification time (gzip) */
25 OS, /* i: waiting for extra flags and operating system (gzip) */
26 EXLEN, /* i: waiting for extra length (gzip) */
27 EXTRA, /* i: waiting for extra bytes (gzip) */
28 NAME, /* i: waiting for end of file name (gzip) */
29 COMMENT, /* i: waiting for end of comment (gzip) */
30 HCRC, /* i: waiting for header crc (gzip) */
31#endif
32 DICTID, /* i: waiting for dictionary check value */
33 DICT, /* waiting for inflateSetDictionary() call */
34 TYPE, /* i: waiting for type bits, including last-flag bit */
35 STORED, /* i: waiting for stored size (length and complement) */
36 COPY, /* i/o: waiting for input or output to copy stored block */
37 TABLE, /* i: waiting for dynamic block table lengths */
38 LENLENS, /* i: waiting for code length code lengths */
39 CODELENS, /* i: waiting for length/lit and distance code lengths */
40 LEN, /* i: waiting for length/lit code */
41 LENEXT, /* i: waiting for length extra bits */
42 DIST, /* i: waiting for distance code */
43 DISTEXT, /* i: waiting for distance extra bits */
44 MATCH, /* o: waiting for output space to copy string */
45 LIT, /* o: waiting for output space to write literal */
46 CHECK, /* i: waiting for 32-bit check value */
47#ifdef GUNZIP
48 LENGTH, /* i: waiting for 32-bit length (gzip) */
49#endif
50 DONE, /* finished check, done -- remain here until reset */
51 BAD, /* got a data error -- remain here until reset */
52 MEM, /* got an inflate() memory error -- remain here until reset */
53 SYNC /* looking for synchronization bytes to restart inflate() */
54} inflate_mode;
55
56/*
57 State transitions between above modes -
58
59 (most modes can go to the BAD or MEM mode -- not shown for clarity)
60
61 Process header:
62 HEAD -> (gzip) or (zlib)
63 (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME
64 NAME -> COMMENT -> HCRC -> TYPE
65 (zlib) -> DICTID or TYPE
66 DICTID -> DICT -> TYPE
67 Read deflate blocks:
68 TYPE -> STORED or TABLE or LEN or CHECK
69 STORED -> COPY -> TYPE
70 TABLE -> LENLENS -> CODELENS -> LEN
71 Read deflate codes:
72 LEN -> LENEXT or LIT or TYPE
73 LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
74 LIT -> LEN
75 Process trailer:
76 CHECK -> LENGTH -> DONE
77 */
78
79/* state maintained between inflate() calls. Approximately 7K bytes. */
80struct inflate_state {
81 inflate_mode mode; /* current inflate mode */
82 int last; /* true if processing last block */
83 int wrap; /* true to process header and trailer */
84 int havedict; /* true if dictionary provided */
85 int flags; /* gzip header method and flags (0 if zlib) */
86 unsigned long check; /* protected copy of check value */
87 unsigned long total; /* protected copy of output count */
88 /* sliding window */
89 unsigned wbits; /* log base 2 of requested window size */
90 unsigned wsize; /* window size or zero if not using window */
91 unsigned write; /* window write index */
92 unsigned char FAR *window; /* allocated sliding window, if needed */
93 /* bit accumulator */
94 unsigned long hold; /* input bit accumulator */
95 unsigned bits; /* number of bits in "in" */
96 /* for string and stored block copying */
97 unsigned length; /* literal or length of data to copy */
98 unsigned offset; /* distance back to copy string from */
99 /* for table and code decoding */
100 unsigned extra; /* extra bits needed */
101 /* fixed and dynamic code tables */
102 code const FAR *lencode; /* starting table for length/literal codes */
103 code const FAR *distcode; /* starting table for distance codes */
104 unsigned lenbits; /* index bits for lencode */
105 unsigned distbits; /* index bits for distcode */
106 /* dynamic table building */
107 unsigned ncode; /* number of code length code lengths */
108 unsigned nlen; /* number of length code lengths */
109 unsigned ndist; /* number of distance code lengths */
110 unsigned have; /* number of code lengths in lens[] */
111 code FAR *next; /* next available space in codes[] */
112 unsigned short lens[320]; /* temporary storage for code lengths */
113 unsigned short work[288]; /* work area for code table building */
114 code codes[ENOUGH]; /* space for code tables */
115};
diff --git a/inftrees.c b/inftrees.c
index 4c32ca3..24ca089 100644
--- a/inftrees.c
+++ b/inftrees.c
@@ -1,454 +1,321 @@
1/* inftrees.c -- generate Huffman trees for efficient decoding 1/* inftrees.c -- generate Huffman trees for efficient decoding
2 * Copyright (C) 1995-2002 Mark Adler 2 * Copyright (C) 1995-2003 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
9#if !defined(BUILDFIXED) && !defined(STDC) 9#define MAXBITS 15
10# define BUILDFIXED /* non ANSI compilers may not accept inffixed.h */
11#endif
12 10
13const char inflate_copyright[] = 11const char inflate_copyright[] =
14 " inflate 1.1.4 Copyright 1995-2002 Mark Adler "; 12 " inflate 1.2.0 Copyright 1995-2003 Mark Adler ";
15/* 13/*
16 If you use the zlib library in a product, an acknowledgment is welcome 14 If you use the zlib library in a product, an acknowledgment is welcome
17 in the documentation of your product. If for some reason you cannot 15 in the documentation of your product. If for some reason you cannot
18 include such an acknowledgment, I would appreciate that you keep this 16 include such an acknowledgment, I would appreciate that you keep this
19 copyright string in the executable of your product. 17 copyright string in the executable of your product.
20 */ 18 */
21struct internal_state {int dummy;}; /* for buggy compilers */
22
23/* simplify the use of the inflate_huft type with some defines */
24#define exop word.what.Exop
25#define bits word.what.Bits
26
27
28local int huft_build OF((
29 uIntf *, /* code lengths in bits */
30 uInt, /* number of codes */
31 uInt, /* number of "simple" codes */
32 const uIntf *, /* list of base values for non-simple codes */
33 const uIntf *, /* list of extra bits for non-simple codes */
34 inflate_huft * FAR*,/* result: starting table */
35 uIntf *, /* maximum lookup bits (returns actual) */
36 inflate_huft *, /* space for trees */
37 uInt *, /* hufts used in space */
38 uIntf * )); /* space for values */
39
40/* Tables for deflate from PKZIP's appnote.txt. */
41local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
42 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
43 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
44 /* see note #13 above about 258 */
45local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
46 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
47 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
48local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
49 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
50 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
51 8193, 12289, 16385, 24577};
52local const uInt cpdext[30] = { /* Extra bits for distance codes */
53 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
54 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
55 12, 12, 13, 13};
56 19
57/* 20/*
58 Huffman code decoding is performed using a multi-level table lookup. 21 Build a set of tables to decode the provided canonical Huffman code.
59 The fastest way to decode is to simply build a lookup table whose 22 The code lengths are lens[0..codes-1]. The result starts at *table,
60 size is determined by the longest code. However, the time it takes 23 whose indices are 0..2^bits-1. work is a writable array of at least
61 to build this table can also be a factor if the data being decoded 24 lens shorts, which is used as a work area. type is the type of code
62 is not very long. The most common codes are necessarily the 25 to be generated, CODES, LENS, or DISTS. On return, zero is success,
63 shortest codes, so those codes dominate the decoding time, and hence 26 -1 is an invalid code, and +1 means that ENOUGH isn't enough. table
64 the speed. The idea is you can have a shorter table that decodes the 27 on return points to the next available entry's address. bits is the
65 shorter, more probable codes, and then point to subsidiary tables for 28 requested root table index bits, and on return it is the actual root
66 the longer codes. The time it costs to decode the longer codes is 29 table index bits. It will differ if the request is greater than the
67 then traded against the time it takes to make longer tables. 30 longest code or if it is less than the shortest code.
68
69 This results of this trade are in the variables lbits and dbits
70 below. lbits is the number of bits the first level table for literal/
71 length codes can decode in one step, and dbits is the same thing for
72 the distance codes. Subsequent tables are also less than or equal to
73 those sizes. These values may be adjusted either when all of the
74 codes are shorter than that, in which case the longest code length in
75 bits is used, or when the shortest code is *longer* than the requested
76 table size, in which case the length of the shortest code in bits is
77 used.
78
79 There are two different values for the two tables, since they code a
80 different number of possibilities each. The literal/length table
81 codes 286 possible values, or in a flat code, a little over eight
82 bits. The distance table codes 30 possible values, or a little less
83 than five bits, flat. The optimum values for speed end up being
84 about one bit more than those, so lbits is 8+1 and dbits is 5+1.
85 The optimum values may differ though from machine to machine, and
86 possibly even between compilers. Your mileage may vary.
87 */ 31 */
88 32int inflate_table(type, lens, codes, table, bits, work)
89 33codetype type;
90/* If BMAX needs to be larger than 16, then h and x[] should be uLong. */ 34unsigned short FAR *lens;
91#define BMAX 15 /* maximum bit length of any code */ 35unsigned codes;
92 36code * FAR *table;
93local int huft_build(b, n, s, d, e, t, m, hp, hn, v) 37unsigned *bits;
94uIntf *b; /* code lengths in bits (all assumed <= BMAX) */ 38unsigned short FAR *work;
95uInt n; /* number of codes (assumed <= 288) */
96uInt s; /* number of simple-valued codes (0..s-1) */
97const uIntf *d; /* list of base values for non-simple codes */
98const uIntf *e; /* list of extra bits for non-simple codes */
99inflate_huft * FAR *t; /* result: starting table */
100uIntf *m; /* maximum lookup bits, returns actual */
101inflate_huft *hp; /* space for trees */
102uInt *hn; /* hufts used in space */
103uIntf *v; /* working area: values in order of bit length */
104/* Given a list of code lengths and a maximum table size, make a set of
105 tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
106 if the given code set is incomplete (the tables are still built in this
107 case), or Z_DATA_ERROR if the input is invalid. */
108{ 39{
40 unsigned len; /* a code's length in bits */
41 unsigned sym; /* index of code symbols */
42 unsigned min, max; /* minimum and maximum code lengths */
43 unsigned root; /* number of index bits for root table */
44 unsigned curr; /* number of index bits for current table */
45 unsigned drop; /* code bits to drop for sub-table */
46 int left; /* number of prefix codes available */
47 unsigned used; /* code entries in table used */
48 unsigned huff; /* Huffman code */
49 unsigned incr; /* for incrementing code, index */
50 unsigned fill; /* index for replicating entries */
51 unsigned low; /* low bits for current root entry */
52 unsigned mask; /* mask for low root bits */
53 code this; /* table entry for duplication */
54 code FAR *next; /* next available space in table */
55 const unsigned short *base; /* base value table to use */
56 const unsigned short *extra; /* extra bits table to use */
57 int end; /* use base and extra for symbol > end */
58 unsigned short count[MAXBITS+1]; /* number of codes of each length */
59 unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
60 static const unsigned short lbase[31] = { /* Length codes 257..285 base */
61 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
62 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
63 static const unsigned short lext[31] = { /* Length codes 257..285 extra */
64 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
65 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 73, 194};
66 static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
67 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
68 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
69 8193, 12289, 16385, 24577, 0, 0};
70 static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
71 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
72 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
73 28, 28, 29, 29, 64, 64};
74
75 /*
76 Process a set of code lengths to create a canonical Huffman code. The
77 code lengths are lens[0..codes-1]. Each length corresponds to the
78 symbols 0..codes-1. The Huffman code is generated by first sorting the
79 symbols by length from short to long, and retaining the symbol order
80 for codes with equal lengths. Then the code starts with all zero bits
81 for the first code of the shortest length, and the codes are integer
82 increments for the same length, and zeros are appended as the length
83 increases. For the deflate format, these bits are stored backwards
84 from their more natural integer increment ordering, and so when the
85 decoding tables are built in the large loop below, the integer codes
86 are incremented backwards.
87
88 This routine assumes, but does not check, that all of the entries in
89 lens[] are in the range 0..MAXBITS. The caller must assure this.
90 1..MAXBITS is interpreted as that code length. zero means that that
91 symbol does not occur in this code.
92
93 The codes are sorted by computing a count of codes for each length,
94 creating from that a table of starting indices for each length in the
95 sorted table, and then entering the symbols in order in the sorted
96 table. The sorted table is work[], with that space being provided by
97 the caller.
98
99 The length counts are used for other purposes as well, i.e. finding
100 the minimum and maximum length codes, determining if there are any
101 codes at all, checking for a valid set of lengths, and looking ahead
102 at length counts to determine sub-table sizes when building the
103 decoding tables.
104 */
105
106 /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
107 for (len = 0; len <= MAXBITS; len++)
108 count[len] = 0;
109 for (sym = 0; sym < codes; sym++)
110 count[lens[sym]]++;
111
112 /* bound code lengths, force root to be within code lengths */
113 root = *bits;
114 for (max = MAXBITS; max >= 1; max--)
115 if (count[max] != 0) break;
116 if (root > max) root = max;
117 if (max == 0) return -1; /* no codes! */
118 for (min = 1; min <= MAXBITS; min++)
119 if (count[min] != 0) break;
120 if (root < min) root = min;
121
122 /* check for an over-subscribed or incomplete set of lengths */
123 left = 1;
124 for (len = 1; len <= MAXBITS; len++) {
125 left <<= 1;
126 left -= count[len];
127 if (left < 0) return -1; /* over-subscribed */
128 }
129 if (left > 0 && (type == CODES || (codes - count[0] != 1)))
130 return -1; /* incomplete set */
131
132 /* generate offsets into symbol table for each length for sorting */
133 offs[1] = 0;
134 for (len = 1; len < MAXBITS; len++)
135 offs[len + 1] = offs[len] + count[len];
136
137 /* sort symbols by length, by symbol order within each length */
138 for (sym = 0; sym < codes; sym++)
139 if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
140
141 /*
142 Create and fill in decoding tables. In this loop, the table being
143 filled is at next and has curr index bits. The code being used is huff
144 with length len. That code is converted to an index by dropping drop
145 bits off of the bottom. For codes where len is less than drop + curr,
146 those top drop + curr - len bits are incremented through all values to
147 fill the table with replicated entries.
148
149 root is the number of index bits for the root table. When len exceeds
150 root, sub-tables are created pointed to by the root entry with an index
151 of the low root bits of huff. This is saved in low to check for when a
152 new sub-table should be started. drop is zero when the root table is
153 being filled, and drop is root when sub-tables are being filled.
154
155 When a new sub-table is needed, it is necessary to look ahead in the
156 code lengths to determine what size sub-table is needed. The length
157 counts are used for this, and so count[] is decremented as codes are
158 entered in the tables.
159
160 used keeps track of how many table entries have been allocated from the
161 provided *table space. It is checked when a LENS table is being made
162 against the space in *table, ENOUGH, minus the maximum space needed by
163 the worst case distance code, MAXD. This should never happen, but the
164 sufficiency of ENOUGH has not been proven exhaustively, hence the check.
165 This assumes that when type == LENS, bits == 9.
166
167 sym increments through all symbols, and the loop terminates when
168 all codes of length max, i.e. all codes, have been processed. This
169 routine permits incomplete codes, so another loop after this one fills
170 in the rest of the decoding tables with invalid code markers.
171 */
172
173 /* set up for code type */
174 switch (type) {
175 case CODES:
176 base = extra = work; /* dummy value--not used */
177 end = 19;
178 break;
179 case LENS:
180 base = lbase;
181 base -= 257;
182 extra = lext;
183 extra -= 257;
184 end = 256;
185 break;
186 default: /* DISTS */
187 base = dbase;
188 extra = dext;
189 end = -1;
190 }
109 191
110 uInt a; /* counter for codes of length k */ 192 /* initialize state for loop */
111 uInt c[BMAX+1]; /* bit length count table */ 193 huff = 0; /* starting code */
112 uInt f; /* i repeats in table every f entries */ 194 sym = 0; /* starting code symbol */
113 int g; /* maximum code length */ 195 len = min; /* starting code length */
114 int h; /* table level */ 196 next = *table; /* current table to fill in */
115 register uInt i; /* counter, current code */ 197 curr = root; /* current table index bits */
116 register uInt j; /* counter */ 198 drop = 0; /* current bits to drop from code for index */
117 register int k; /* number of bits in current code */ 199 low = (unsigned)(-1); /* trigger new sub-table when len > root */
118 int l; /* bits per table (returned in m) */ 200 used = 1U << root; /* use root table entries */
119 uInt mask; /* (1 << w) - 1, to avoid cc -O bug on HP */ 201 mask = used - 1; /* mask for comparing low */
120 register uIntf *p; /* pointer into c[], b[], or v[] */ 202
121 inflate_huft *q; /* points to current table */ 203 /* check available table space */
122 struct inflate_huft_s r; /* table entry for structure assignment */ 204 if (type == LENS && used >= ENOUGH - MAXD)
123 inflate_huft *u[BMAX]; /* table stack */ 205 return 1;
124 register int w; /* bits before this table == (l * h) */ 206
125 uInt x[BMAX+1]; /* bit offsets, then code stack */ 207 /* process all codes and make table entries */
126 uIntf *xp; /* pointer into x */ 208 for (;;) {
127 int y; /* number of dummy codes added */ 209 /* create table entry */
128 uInt z; /* number of entries in current table */ 210 this.bits = (unsigned char)(len - drop);
129 211 if ((int)(work[sym]) < end) {
130 212 this.op = (unsigned char)0;
131 /* Generate counts for each bit length */ 213 this.val = work[sym];
132 p = c;
133#define C0 *p++ = 0;
134#define C2 C0 C0 C0 C0
135#define C4 C2 C2 C2 C2
136 C4 /* clear c[]--assume BMAX+1 is 16 */
137 p = b; i = n;
138 do {
139 c[*p++]++; /* assume all entries <= BMAX */
140 } while (--i);
141 if (c[0] == n) /* null input--all zero length codes */
142 {
143 *t = (inflate_huft *)Z_NULL;
144 *m = 0;
145 return Z_OK;
146 }
147
148
149 /* Find minimum and maximum length, bound *m by those */
150 l = *m;
151 for (j = 1; j <= BMAX; j++)
152 if (c[j])
153 break;
154 k = j; /* minimum code length */
155 if ((uInt)l < j)
156 l = j;
157 for (i = BMAX; i; i--)
158 if (c[i])
159 break;
160 g = i; /* maximum code length */
161 if ((uInt)l > i)
162 l = i;
163 *m = l;
164
165
166 /* Adjust last length count to fill out codes, if needed */
167 for (y = 1 << j; j < i; j++, y <<= 1)
168 if ((y -= c[j]) < 0)
169 return Z_DATA_ERROR;
170 if ((y -= c[i]) < 0)
171 return Z_DATA_ERROR;
172 c[i] += y;
173
174
175 /* Generate starting offsets into the value table for each length */
176 x[1] = j = 0;
177 p = c + 1; xp = x + 2;
178 while (--i) { /* note that i == g from above */
179 *xp++ = (j += *p++);
180 }
181
182
183 /* Make a table of values in order of bit lengths */
184 p = b; i = 0;
185 do {
186 if ((j = *p++) != 0)
187 v[x[j]++] = i;
188 } while (++i < n);
189 n = x[g]; /* set n to length of v */
190
191
192 /* Generate the Huffman codes and for each, make the table entries */
193 x[0] = i = 0; /* first Huffman code is zero */
194 p = v; /* grab values in bit order */
195 h = -1; /* no tables yet--level -1 */
196 w = -l; /* bits decoded == (l * h) */
197 u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */
198 q = (inflate_huft *)Z_NULL; /* ditto */
199 z = 0; /* ditto */
200
201 /* go through the bit lengths (k already is bits in shortest code) */
202 for (; k <= g; k++)
203 {
204 a = c[k];
205 while (a--)
206 {
207 /* here i is the Huffman code of length k bits for value *p */
208 /* make tables up to required level */
209 while (k > w + l)
210 {
211 h++;
212 w += l; /* previous table always l bits */
213
214 /* compute minimum size table less than or equal to l bits */
215 z = g - w;
216 z = z > (uInt)l ? l : z; /* table size upper limit */
217 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
218 { /* too few codes for k-w bit table */
219 f -= a + 1; /* deduct codes from patterns left */
220 xp = c + k;
221 if (j < z)
222 while (++j < z) /* try smaller tables up to z bits */
223 {
224 if ((f <<= 1) <= *++xp)
225 break; /* enough codes to use up j bits */
226 f -= *xp; /* else deduct codes from patterns */
227 }
228 } 214 }
229 z = 1 << j; /* table entries for j-bit table */ 215 else if ((int)(work[sym]) > end) {
230 216 this.op = (unsigned char)(extra[work[sym]]);
231 /* allocate new table */ 217 this.val = base[work[sym]];
232 if (*hn + z > MANY) /* (note: doesn't matter for fixed) */ 218 }
233 return Z_DATA_ERROR; /* overflow of MANY */ 219 else {
234 u[h] = q = hp + *hn; 220 this.op = (unsigned char)(32 + 64); /* end of block */
235 *hn += z; 221 this.val = 0;
236
237 /* connect to last table, if there is one */
238 if (h)
239 {
240 x[h] = i; /* save pattern for backing up */
241 r.bits = (Byte)l; /* bits to dump before this table */
242 r.exop = (Byte)j; /* bits in this table */
243 j = i >> (w - l);
244 r.base = (uInt)(q - u[h-1] - j); /* offset to this table */
245 u[h-1][j] = r; /* connect to last table */
246 } 222 }
247 else
248 *t = q; /* first table is returned result */
249 }
250
251 /* set up table entry in r */
252 r.bits = (Byte)(k - w);
253 if (p >= v + n)
254 r.exop = 128 + 64; /* out of values--invalid code */
255 else if (*p < s)
256 {
257 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */
258 r.base = *p++; /* simple code is just the value */
259 }
260 else
261 {
262 r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
263 r.base = d[*p++ - s];
264 }
265
266 /* fill code-like entries with r */
267 f = 1 << (k - w);
268 for (j = i >> w; j < z; j += f)
269 q[j] = r;
270
271 /* backwards increment the k-bit code i */
272 for (j = 1 << (k - 1); i & j; j >>= 1)
273 i ^= j;
274 i ^= j;
275
276 /* backup over finished tables */
277 mask = (1 << w) - 1; /* needed on HP, cc -O bug */
278 while ((i & mask) != x[h])
279 {
280 h--; /* don't need to update q */
281 w -= l;
282 mask = (1 << w) - 1;
283 }
284 }
285 }
286
287 223
288 /* Return Z_BUF_ERROR if we were given an incomplete table */ 224 /* replicate for those indices with low len bits equal to huff */
289 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK; 225 incr = 1U << (len - drop);
290} 226 fill = 1U << curr;
227 do {
228 fill -= incr;
229 next[(huff >> drop) + fill] = this;
230 } while (fill != 0);
231
232 /* backwards increment the len-bit code huff */
233 incr = 1U << (len - 1);
234 while (huff & incr)
235 incr >>= 1;
236 if (incr != 0) {
237 huff &= incr - 1;
238 huff += incr;
239 }
240 else
241 huff = 0;
291 242
243 /* go to next symbol, update count, len */
244 sym++;
245 if (--(count[len]) == 0) {
246 if (len == max) break;
247 len = lens[work[sym]];
248 }
292 249
293int inflate_trees_bits(c, bb, tb, hp, z) 250 /* create new sub-table if needed */
294uIntf *c; /* 19 code lengths */ 251 if (len > root && (huff & mask) != low) {
295uIntf *bb; /* bits tree desired/actual depth */ 252 /* if first time, transition to sub-tables */
296inflate_huft * FAR *tb; /* bits tree result */ 253 if (drop == 0)
297inflate_huft *hp; /* space for trees */ 254 drop = root;
298z_streamp z; /* for messages */ 255
299{ 256 /* increment past last table */
300 int r; 257 next += 1U << curr;
301 uInt hn = 0; /* hufts used in space */ 258
302 uIntf *v; /* work area for huft_build */ 259 /* determine length of next table */
303 260 curr = len - drop;
304 if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL) 261 left = (int)(1 << curr);
305 return Z_MEM_ERROR; 262 while (curr + drop < max) {
306 r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, 263 left -= count[curr + drop];
307 tb, bb, hp, &hn, v); 264 if (left <= 0) break;
308 if (r == Z_DATA_ERROR) 265 curr++;
309 z->msg = (char*)"oversubscribed dynamic bit lengths tree"; 266 left <<= 1;
310 else if (r == Z_BUF_ERROR || *bb == 0) 267 }
311 {
312 z->msg = (char*)"incomplete dynamic bit lengths tree";
313 r = Z_DATA_ERROR;
314 }
315 ZFREE(z, v);
316 return r;
317}
318 268
269 /* check for enough space */
270 used += 1U << curr;
271 if (type == LENS && used >= ENOUGH - MAXD)
272 return 1;
319 273
320int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z) 274 /* point entry in root table to sub-table */
321uInt nl; /* number of literal/length codes */ 275 low = huff & mask;
322uInt nd; /* number of distance codes */ 276 (*table)[low].op = (unsigned char)curr;
323uIntf *c; /* that many (total) code lengths */ 277 (*table)[low].bits = (unsigned char)root;
324uIntf *bl; /* literal desired/actual bit depth */ 278 (*table)[low].val = (unsigned short)(next - *table);
325uIntf *bd; /* distance desired/actual bit depth */ 279 }
326inflate_huft * FAR *tl; /* literal/length tree result */
327inflate_huft * FAR *td; /* distance tree result */
328inflate_huft *hp; /* space for trees */
329z_streamp z; /* for messages */
330{
331 int r;
332 uInt hn = 0; /* hufts used in space */
333 uIntf *v; /* work area for huft_build */
334
335 /* allocate work area */
336 if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
337 return Z_MEM_ERROR;
338
339 /* build literal/length tree */
340 r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
341 if (r != Z_OK || *bl == 0)
342 {
343 if (r == Z_DATA_ERROR)
344 z->msg = (char*)"oversubscribed literal/length tree";
345 else if (r != Z_MEM_ERROR)
346 {
347 z->msg = (char*)"incomplete literal/length tree";
348 r = Z_DATA_ERROR;
349 }
350 ZFREE(z, v);
351 return r;
352 }
353
354 /* build distance tree */
355 r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
356 if (r != Z_OK || (*bd == 0 && nl > 257))
357 {
358 if (r == Z_DATA_ERROR)
359 z->msg = (char*)"oversubscribed distance tree";
360 else if (r == Z_BUF_ERROR) {
361#ifdef PKZIP_BUG_WORKAROUND
362 r = Z_OK;
363 }
364#else
365 z->msg = (char*)"incomplete distance tree";
366 r = Z_DATA_ERROR;
367 }
368 else if (r != Z_MEM_ERROR)
369 {
370 z->msg = (char*)"empty distance tree with lengths";
371 r = Z_DATA_ERROR;
372 } 280 }
373 ZFREE(z, v);
374 return r;
375#endif
376 }
377
378 /* done */
379 ZFREE(z, v);
380 return Z_OK;
381}
382 281
282 /*
283 Fill in rest of table for incomplete codes. This loop is similar to the
284 loop above in incrementing huff for table indices. It is assumed that
285 len is equal to curr + drop, so there is no loop needed to increment
286 through high index bits. When the current sub-table is filled, the loop
287 drops back to the root table to fill in any remaining entries there.
288 */
289 this.op = (unsigned char)64; /* invalid code marker */
290 this.bits = (unsigned char)(len - drop);
291 this.val = (unsigned short)0;
292 while (huff != 0) {
293 /* when done with sub-table, drop back to root table */
294 if (drop != 0 && (huff & mask) != low) {
295 drop = 0;
296 len = root;
297 next = *table;
298 curr = root;
299 this.bits = (unsigned char)len;
300 }
301
302 /* put invalid code marker in table */
303 next[huff >> drop] = this;
383 304
384/* build fixed tables only once--keep them here */ 305 /* backwards increment the len-bit code huff */
385#ifdef BUILDFIXED 306 incr = 1U << (len - 1);
386local int fixed_built = 0; 307 while (huff & incr)
387#define FIXEDH 544 /* number of hufts used by fixed tables */ 308 incr >>= 1;
388local inflate_huft fixed_mem[FIXEDH]; 309 if (incr != 0) {
389local uInt fixed_bl; 310 huff &= incr - 1;
390local uInt fixed_bd; 311 huff += incr;
391local inflate_huft *fixed_tl; 312 }
392local inflate_huft *fixed_td; 313 else
393#else 314 huff = 0;
394#include "inffixed.h"
395#endif
396
397
398int inflate_trees_fixed(bl, bd, tl, td, z)
399uIntf *bl; /* literal desired/actual bit depth */
400uIntf *bd; /* distance desired/actual bit depth */
401inflate_huft * FAR *tl; /* literal/length tree result */
402inflate_huft * FAR *td; /* distance tree result */
403z_streamp z; /* for memory allocation */
404{
405#ifdef BUILDFIXED
406 /* build fixed tables if not already */
407 if (!fixed_built)
408 {
409 int k; /* temporary variable */
410 uInt f = 0; /* number of hufts used in fixed_mem */
411 uIntf *c; /* length list for huft_build */
412 uIntf *v; /* work area for huft_build */
413
414 /* allocate memory */
415 if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
416 return Z_MEM_ERROR;
417 if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
418 {
419 ZFREE(z, c);
420 return Z_MEM_ERROR;
421 } 315 }
422 316
423 /* literal table */ 317 /* set return parameters */
424 for (k = 0; k < 144; k++) 318 *table += used;
425 c[k] = 8; 319 *bits = root;
426 for (; k < 256; k++) 320 return 0;
427 c[k] = 9;
428 for (; k < 280; k++)
429 c[k] = 7;
430 for (; k < 288; k++)
431 c[k] = 8;
432 fixed_bl = 9;
433 huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl,
434 fixed_mem, &f, v);
435
436 /* distance table */
437 for (k = 0; k < 30; k++)
438 c[k] = 5;
439 fixed_bd = 5;
440 huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd,
441 fixed_mem, &f, v);
442
443 /* done */
444 ZFREE(z, v);
445 ZFREE(z, c);
446 fixed_built = 1;
447 }
448#endif
449 *bl = fixed_bl;
450 *bd = fixed_bd;
451 *tl = fixed_tl;
452 *td = fixed_td;
453 return Z_OK;
454} 321}
diff --git a/inftrees.h b/inftrees.h
index 04b73b7..49eeeb9 100644
--- a/inftrees.h
+++ b/inftrees.h
@@ -1,6 +1,6 @@
1/* inftrees.h -- header to use inftrees.c 1/* inftrees.h -- header to use inftrees.c
2 * Copyright (C) 1995-2002 Mark Adler 2 * Copyright (C) 1995-2003 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/* WARNING: this file should *not* be used by applications. It is 6/* WARNING: this file should *not* be used by applications. It is
@@ -8,51 +8,48 @@
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/* Huffman code lookup table entry--this entry is four bytes for machines 11/* Structure for decoding tables. Each entry provides either the
12 that have 16-bit pointers (e.g. PC's in the small or medium model). */ 12 information needed to do the operation requested by the code that
13 13 indexed that table entry, or it provides a pointer to another
14typedef struct inflate_huft_s FAR inflate_huft; 14 table that indexes more bits of the code. op indicates whether
15 15 the entry is a pointer to another table, a literal, a length or
16struct inflate_huft_s { 16 distance, an end-of-block, or an invalid code. For a table
17 union { 17 pointer, the low four bits of op is the number of index bits of
18 struct { 18 that table. For a length or distance, the low four bits of op
19 Byte Exop; /* number of extra bits or operation */ 19 is the number of extra bits to get after the code. bits is
20 Byte Bits; /* number of bits in this code or subcode */ 20 the number of bits in this code or part of the code to drop off
21 } what; 21 of the bit buffer. val is the actual byte to output in the case
22 uInt pad; /* pad structure to a power of 2 (4 bytes for */ 22 of a literal, the base length or distance, or the offset from
23 } word; /* 16-bit, 8 bytes for 32-bit int's) */ 23 the current table to the next table. Each entry is four bytes. */
24 uInt base; /* literal, length base, distance base, 24typedef struct {
25 or table offset */ 25 unsigned char op; /* operation, extra bits, table bits */
26}; 26 unsigned char bits; /* bits in this part of the code */
27 unsigned short val; /* offset in table or code value */
28} code;
29
30/* op values as set by inflate_table():
31 00000000 - literal
32 0000tttt - table link, tttt != 0 is the number of table index bits
33 0001eeee - length or distance, eeee is the number of extra bits
34 01100000 - end of block
35 01000000 - invalid code
36 */
27 37
28/* Maximum size of dynamic tree. The maximum found in a long but non- 38/* Maximum size of dynamic tree. The maximum found in a long but non-
29 exhaustive search was 1004 huft structures (850 for length/literals 39 exhaustive search was 1004 code structures (850 for length/literals
30 and 154 for distances, the latter actually the result of an 40 and 154 for distances, the latter actually the result of an
31 exhaustive search). The actual maximum is not known, but the 41 exhaustive search). The true maximum is not known, but the value
32 value below is more than safe. */ 42 below is more than safe. */
33#define MANY 1440 43#define ENOUGH 1440
34 44#define MAXD 154
35extern int inflate_trees_bits OF(( 45
36 uIntf *, /* 19 code lengths */ 46/* Type of code to build for inftable() */
37 uIntf *, /* bits tree desired/actual depth */ 47typedef enum {
38 inflate_huft * FAR *, /* bits tree result */ 48 CODES,
39 inflate_huft *, /* space for trees */ 49 LENS,
40 z_streamp)); /* for messages */ 50 DISTS
41 51} codetype;
42extern int inflate_trees_dynamic OF(( 52
43 uInt, /* number of literal/length codes */ 53extern int inflate_table OF((codetype type, unsigned short FAR *lens,
44 uInt, /* number of distance codes */ 54 unsigned codes, code * FAR *table, unsigned *bits,
45 uIntf *, /* that many (total) code lengths */ 55 unsigned short FAR *work));
46 uIntf *, /* literal desired/actual bit depth */
47 uIntf *, /* distance desired/actual bit depth */
48 inflate_huft * FAR *, /* literal/length tree result */
49 inflate_huft * FAR *, /* distance tree result */
50 inflate_huft *, /* space for trees */
51 z_streamp)); /* for messages */
52
53extern int inflate_trees_fixed OF((
54 uIntf *, /* literal desired/actual bit depth */
55 uIntf *, /* distance desired/actual bit depth */
56 inflate_huft * FAR *, /* literal/length tree result */
57 inflate_huft * FAR *, /* distance tree result */
58 z_streamp)); /* for memory allocation */
diff --git a/infutil.c b/infutil.c
deleted file mode 100644
index 9a07622..0000000
--- a/infutil.c
+++ /dev/null
@@ -1,87 +0,0 @@
1/* inflate_util.c -- data and routines common to blocks and codes
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6#include "zutil.h"
7#include "infblock.h"
8#include "inftrees.h"
9#include "infcodes.h"
10#include "infutil.h"
11
12struct inflate_codes_state {int dummy;}; /* for buggy compilers */
13
14/* And'ing with mask[n] masks the lower n bits */
15uInt inflate_mask[17] = {
16 0x0000,
17 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
18 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
19};
20
21
22/* copy as much as possible from the sliding window to the output area */
23int inflate_flush(s, z, r)
24inflate_blocks_statef *s;
25z_streamp z;
26int r;
27{
28 uInt n;
29 Bytef *p;
30 Bytef *q;
31
32 /* local copies of source and destination pointers */
33 p = z->next_out;
34 q = s->read;
35
36 /* compute number of bytes to copy as far as end of window */
37 n = (uInt)((q <= s->write ? s->write : s->end) - q);
38 if (n > z->avail_out) n = z->avail_out;
39 if (n && r == Z_BUF_ERROR) r = Z_OK;
40
41 /* update counters */
42 z->avail_out -= n;
43 z->total_out += n;
44
45 /* update check information */
46 if (s->checkfn != Z_NULL)
47 z->adler = s->check = (*s->checkfn)(s->check, q, n);
48
49 /* copy as far as end of window */
50 zmemcpy(p, q, n);
51 p += n;
52 q += n;
53
54 /* see if more to copy at beginning of window */
55 if (q == s->end)
56 {
57 /* wrap pointers */
58 q = s->window;
59 if (s->write == s->end)
60 s->write = s->window;
61
62 /* compute bytes to copy */
63 n = (uInt)(s->write - q);
64 if (n > z->avail_out) n = z->avail_out;
65 if (n && r == Z_BUF_ERROR) r = Z_OK;
66
67 /* update counters */
68 z->avail_out -= n;
69 z->total_out += n;
70
71 /* update check information */
72 if (s->checkfn != Z_NULL)
73 z->adler = s->check = (*s->checkfn)(s->check, q, n);
74
75 /* copy */
76 zmemcpy(p, q, n);
77 p += n;
78 q += n;
79 }
80
81 /* update pointers */
82 z->next_out = p;
83 s->read = q;
84
85 /* done */
86 return r;
87}
diff --git a/infutil.h b/infutil.h
deleted file mode 100644
index 4401df8..0000000
--- a/infutil.h
+++ /dev/null
@@ -1,98 +0,0 @@
1/* infutil.h -- types and macros common to blocks and codes
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* WARNING: this file should *not* be used by applications. It is
7 part of the implementation of the compression library and is
8 subject to change. Applications should only use zlib.h.
9 */
10
11#ifndef _INFUTIL_H
12#define _INFUTIL_H
13
14typedef enum {
15 TYPE, /* get type bits (3, including end bit) */
16 LENS, /* get lengths for stored */
17 STORED, /* processing stored block */
18 TABLE, /* get table lengths */
19 BTREE, /* get bit lengths tree for a dynamic block */
20 DTREE, /* get length, distance trees for a dynamic block */
21 CODES, /* processing fixed or dynamic block */
22 DRY, /* output remaining window bytes */
23 DONE, /* finished last block, done */
24 BAD} /* got a data error--stuck here */
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 */
32
33 /* mode dependent information */
34 union {
35 uInt left; /* if STORED, bytes left to copy */
36 struct {
37 uInt table; /* table lengths (14 bits) */
38 uInt index; /* index into blens (or border) */
39 uIntf *blens; /* bit lengths of codes */
40 uInt bb; /* bit length tree depth */
41 inflate_huft *tb; /* bit length decoding tree */
42 } trees; /* if DTREE, decoding info for trees */
43 struct {
44 inflate_codes_statef
45 *codes;
46 } decode; /* if CODES, current state */
47 } sub; /* submode */
48 uInt last; /* true if this block is the last block */
49
50 /* mode independent information */
51 uInt bitk; /* bits in bit buffer */
52 uLong bitb; /* bit buffer */
53 inflate_huft *hufts; /* single malloc for tree space */
54 Bytef *window; /* sliding window */
55 Bytef *end; /* one byte after sliding window */
56 Bytef *read; /* window read pointer */
57 Bytef *write; /* window write pointer */
58 check_func checkfn; /* check function */
59 uLong check; /* check on output */
60
61};
62
63
64/* defines for inflate input/output */
65/* update pointers and return */
66#define UPDBITS {s->bitb=b;s->bitk=k;}
67#define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
68#define UPDOUT {s->write=q;}
69#define UPDATE {UPDBITS UPDIN UPDOUT}
70#define LEAVE {UPDATE return inflate_flush(s,z,r);}
71/* get bytes and bits */
72#define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
73#define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
74#define NEXTBYTE (n--,*p++)
75#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
76#define DUMPBITS(j) {b>>=(j);k-=(j);}
77/* output bytes */
78#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
79#define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
80#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
81#define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
82#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
83#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
84/* load local pointers */
85#define LOAD {LOADIN LOADOUT}
86
87/* masks for lower bits (size given to avoid silly warnings with Visual C++) */
88extern uInt inflate_mask[17];
89
90/* copy as much as possible from the sliding window to the output area */
91extern int inflate_flush OF((
92 inflate_blocks_statef *,
93 z_streamp ,
94 int));
95
96struct internal_state {int dummy;}; /* for buggy compilers */
97
98#endif
diff --git a/maketree.c b/maketree.c
deleted file mode 100644
index a16d4b1..0000000
--- a/maketree.c
+++ /dev/null
@@ -1,85 +0,0 @@
1/* maketree.c -- make inffixed.h table for decoding fixed codes
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* WARNING: this file should *not* be used by applications. It is
7 part of the implementation of the compression library and is
8 subject to change. Applications should only use zlib.h.
9 */
10
11/* This program is included in the distribution for completeness.
12 You do not need to compile or run this program since inffixed.h
13 is already included in the distribution. To use this program
14 you need to compile zlib with BUILDFIXED defined and then compile
15 and link this program with the zlib library. Then the output of
16 this program can be piped to inffixed.h. */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include "zutil.h"
21#include "inftrees.h"
22
23/* simplify the use of the inflate_huft type with some defines */
24#define exop word.what.Exop
25#define bits word.what.Bits
26
27/* generate initialization table for an inflate_huft structure array */
28void maketree(uInt b, inflate_huft *t)
29{
30 int i, e;
31
32 i = 0;
33 while (1)
34 {
35 e = t[i].exop;
36 if (e && (e & (16+64)) == 0) /* table pointer */
37 {
38 fprintf(stderr, "maketree: cannot initialize sub-tables!\n");
39 exit(1);
40 }
41 if (i % 4 == 0)
42 printf("\n ");
43 printf(" {{{%u,%u}},%u}", t[i].exop, t[i].bits, t[i].base);
44 if (++i == (1<<b))
45 break;
46 putchar(',');
47 }
48 puts("");
49}
50
51/* create the fixed tables in C initialization syntax */
52void main(void)
53{
54 int r;
55 uInt bl, bd;
56 inflate_huft *tl, *td;
57 z_stream z;
58
59 z.zalloc = zcalloc;
60 z.opaque = (voidpf)0;
61 z.zfree = zcfree;
62 r = inflate_trees_fixed(&bl, &bd, &tl, &td, &z);
63 if (r)
64 {
65 fprintf(stderr, "inflate_trees_fixed error %d\n", r);
66 return;
67 }
68 puts("/* inffixed.h -- table for decoding fixed codes");
69 puts(" * Generated automatically by the maketree.c program");
70 puts(" */");
71 puts("");
72 puts("/* WARNING: this file should *not* be used by applications. It is");
73 puts(" part of the implementation of the compression library and is");
74 puts(" subject to change. Applications should only use zlib.h.");
75 puts(" */");
76 puts("");
77 printf("local uInt fixed_bl = %d;\n", bl);
78 printf("local uInt fixed_bd = %d;\n", bd);
79 printf("local inflate_huft fixed_tl[] = {");
80 maketree(bl, tl);
81 puts(" };");
82 printf("local inflate_huft fixed_td[] = {");
83 maketree(bd, td);
84 puts(" };");
85}
diff --git a/Make_vms.com b/old/Make_vms.com
index 1c57e8f..1c57e8f 100644
--- a/Make_vms.com
+++ b/old/Make_vms.com
diff --git a/Makefile.riscos b/old/Makefile.riscos
index d97f449..d97f449 100644
--- a/Makefile.riscos
+++ b/old/Makefile.riscos
diff --git a/old/README b/old/README
new file mode 100644
index 0000000..ea9cd22
--- /dev/null
+++ b/old/README
@@ -0,0 +1,3 @@
1This directory contains files that have not been updated for zlib 1.2.0.
2
3(Volunteers are encouraged to help clean this up. Thanks.)
diff --git a/amiga/Makefile.pup b/old/amiga/Makefile.pup
index 6cfad1d..6cfad1d 100644
--- a/amiga/Makefile.pup
+++ b/old/amiga/Makefile.pup
diff --git a/amiga/Makefile.sas b/old/amiga/Makefile.sas
index 5323e82..5323e82 100644
--- a/amiga/Makefile.sas
+++ b/old/amiga/Makefile.sas
diff --git a/descrip.mms b/old/descrip.mms
index 9d36459..9d36459 100644
--- a/descrip.mms
+++ b/old/descrip.mms
diff --git a/msdos/Makefile.b32 b/old/msdos/Makefile.b32
index f476da9..f476da9 100644
--- a/msdos/Makefile.b32
+++ b/old/msdos/Makefile.b32
diff --git a/msdos/Makefile.bor b/old/msdos/Makefile.bor
index f5651b4..f5651b4 100644
--- a/msdos/Makefile.bor
+++ b/old/msdos/Makefile.bor
diff --git a/msdos/Makefile.dj2 b/old/msdos/Makefile.dj2
index 0ab431c..0ab431c 100644
--- a/msdos/Makefile.dj2
+++ b/old/msdos/Makefile.dj2
diff --git a/msdos/Makefile.emx b/old/msdos/Makefile.emx
index 0e5e5cc..0e5e5cc 100644
--- a/msdos/Makefile.emx
+++ b/old/msdos/Makefile.emx
diff --git a/msdos/Makefile.msc b/old/msdos/Makefile.msc
index 562201d..562201d 100644
--- a/msdos/Makefile.msc
+++ b/old/msdos/Makefile.msc
diff --git a/msdos/Makefile.tc b/old/msdos/Makefile.tc
index 63e0550..63e0550 100644
--- a/msdos/Makefile.tc
+++ b/old/msdos/Makefile.tc
diff --git a/msdos/Makefile.w32 b/old/msdos/Makefile.w32
index 0a05fa9..0a05fa9 100644
--- a/msdos/Makefile.w32
+++ b/old/msdos/Makefile.w32
diff --git a/msdos/Makefile.wat b/old/msdos/Makefile.wat
index 44bf860..44bf860 100644
--- a/msdos/Makefile.wat
+++ b/old/msdos/Makefile.wat
diff --git a/msdos/zlib.def b/old/msdos/zlib.def
index 6c04412..6c04412 100644
--- a/msdos/zlib.def
+++ b/old/msdos/zlib.def
diff --git a/msdos/zlib.rc b/old/msdos/zlib.rc
index 556d4ff..556d4ff 100644
--- a/msdos/zlib.rc
+++ b/old/msdos/zlib.rc
diff --git a/nt/Makefile.emx b/old/nt/Makefile.emx
index 2d475b1..2d475b1 100644
--- a/nt/Makefile.emx
+++ b/old/nt/Makefile.emx
diff --git a/nt/Makefile.gcc b/old/nt/Makefile.gcc
index cdd652f..cdd652f 100644
--- a/nt/Makefile.gcc
+++ b/old/nt/Makefile.gcc
diff --git a/nt/Makefile.nt b/old/nt/Makefile.nt
index b250f2a..b250f2a 100644
--- a/nt/Makefile.nt
+++ b/old/nt/Makefile.nt
diff --git a/nt/zlib.dnt b/old/nt/zlib.dnt
index 7f9475c..7f9475c 100644
--- a/nt/zlib.dnt
+++ b/old/nt/zlib.dnt
diff --git a/os2/Makefile.os2 b/old/os2/Makefile.os2
index 4f56947..4f56947 100644
--- a/os2/Makefile.os2
+++ b/old/os2/Makefile.os2
diff --git a/os2/zlib.def b/old/os2/zlib.def
index 4c753f1..4c753f1 100644
--- a/os2/zlib.def
+++ b/old/os2/zlib.def
diff --git a/zlib.3 b/old/zlib.3
index 3a6e450..3a6e450 100644
--- a/zlib.3
+++ b/old/zlib.3
diff --git a/zlib.html b/old/zlib.html
index c343703..c343703 100644
--- a/zlib.html
+++ b/old/zlib.html
diff --git a/trees.c b/trees.c
index 0a98405..f7f4a84 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-2002 Jean-loup Gailly 2 * Copyright (C) 1995-2003 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
@@ -230,7 +230,6 @@ local void send_bits(s, value, length)
230#endif /* DEBUG */ 230#endif /* DEBUG */
231 231
232 232
233#define MAX(a,b) (a >= b ? a : b)
234/* the arguments must not have side effects */ 233/* the arguments must not have side effects */
235 234
236/* =========================================================================== 235/* ===========================================================================
@@ -675,7 +674,8 @@ local void build_tree(s, desc)
675 674
676 /* Create a new node father of n and m */ 675 /* Create a new node father of n and m */
677 tree[node].Freq = tree[n].Freq + tree[m].Freq; 676 tree[node].Freq = tree[n].Freq + tree[m].Freq;
678 s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1); 677 s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
678 s->depth[n] : s->depth[m]) + 1);
679 tree[n].Dad = tree[m].Dad = (ush)node; 679 tree[n].Dad = tree[m].Dad = (ush)node;
680#ifdef DUMP_BL_TREE 680#ifdef DUMP_BL_TREE
681 if (tree == s->bl_tree) { 681 if (tree == s->bl_tree) {
@@ -950,7 +950,7 @@ void _tr_flush_block(s, buf, stored_len, eof)
950 */ 950 */
951 max_blindex = build_bl_tree(s); 951 max_blindex = build_bl_tree(s);
952 952
953 /* Determine the best encoding. Compute first the block length in bytes*/ 953 /* Determine the best encoding. Compute the block lengths in bytes. */
954 opt_lenb = (s->opt_len+3+7)>>3; 954 opt_lenb = (s->opt_len+3+7)>>3;
955 static_lenb = (s->static_len+3+7)>>3; 955 static_lenb = (s->static_len+3+7)>>3;
956 956
diff --git a/uncompr.c b/uncompr.c
index a287714..e4e52ce 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-2002 Jean-loup Gailly. 2 * Copyright (C) 1995-2003 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
@@ -49,7 +49,9 @@ int ZEXPORT uncompress (dest, destLen, source, sourceLen)
49 err = inflate(&stream, Z_FINISH); 49 err = inflate(&stream, Z_FINISH);
50 if (err != Z_STREAM_END) { 50 if (err != Z_STREAM_END) {
51 inflateEnd(&stream); 51 inflateEnd(&stream);
52 return err == Z_OK ? Z_BUF_ERROR : err; 52 if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
53 return Z_DATA_ERROR;
54 return err;
53 } 55 }
54 *destLen = stream.total_out; 56 *destLen = stream.total_out;
55 57
diff --git a/zconf.h b/zconf.h
index eb0ae2e..8ef845e 100644
--- a/zconf.h
+++ b/zconf.h
@@ -1,6 +1,6 @@
1/* zconf.h -- configuration of the zlib compression library 1/* zconf.h -- configuration of the zlib compression library
2 * Copyright (C) 1995-2002 Jean-loup Gailly. 2 * Copyright (C) 1995-2003 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* @(#) $Id$ */ 6/* @(#) $Id$ */
@@ -13,39 +13,42 @@
13 * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. 13 * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
14 */ 14 */
15#ifdef Z_PREFIX 15#ifdef Z_PREFIX
16# define deflateInit_ z_deflateInit_ 16# define deflateInit_ z_deflateInit_
17# define deflate z_deflate 17# define deflate z_deflate
18# define deflateEnd z_deflateEnd 18# define deflateEnd z_deflateEnd
19# define inflateInit_ z_inflateInit_ 19# define inflateInit_ z_inflateInit_
20# define inflate z_inflate 20# define inflate z_inflate
21# define inflateEnd z_inflateEnd 21# define inflateEnd z_inflateEnd
22# define deflateInit2_ z_deflateInit2_ 22# define deflateInit2_ z_deflateInit2_
23# define deflateSetDictionary z_deflateSetDictionary 23# define deflateSetDictionary z_deflateSetDictionary
24# define deflateCopy z_deflateCopy 24# define deflateCopy z_deflateCopy
25# define deflateReset z_deflateReset 25# define deflateReset z_deflateReset
26# define deflateParams z_deflateParams 26# define deflateParams z_deflateParams
27# define inflateInit2_ z_inflateInit2_ 27# define deflateBound z_deflateBound
28# define inflateInit2_ z_inflateInit2_
28# define inflateSetDictionary z_inflateSetDictionary 29# define inflateSetDictionary z_inflateSetDictionary
29# define inflateSync z_inflateSync 30# define inflateSync z_inflateSync
30# define inflateSyncPoint z_inflateSyncPoint 31# define inflateSyncPoint z_inflateSyncPoint
31# define inflateReset z_inflateReset 32# define inflateCopy z_inflateCopy
32# define compress z_compress 33# define inflateReset z_inflateReset
33# define compress2 z_compress2 34# define compress z_compress
34# define uncompress z_uncompress 35# define compress2 z_compress2
35# define adler32 z_adler32 36# define compressBound z_compressBound
36# define crc32 z_crc32 37# define uncompress z_uncompress
38# define adler32 z_adler32
39# define crc32 z_crc32
37# define get_crc_table z_get_crc_table 40# define get_crc_table z_get_crc_table
38 41
39# define Byte z_Byte 42# define Byte z_Byte
40# define uInt z_uInt 43# define uInt z_uInt
41# define uLong z_uLong 44# define uLong z_uLong
42# define Bytef z_Bytef 45# define Bytef z_Bytef
43# define charf z_charf 46# define charf z_charf
44# define intf z_intf 47# define intf z_intf
45# define uIntf z_uIntf 48# define uIntf z_uIntf
46# define uLongf z_uLongf 49# define uLongf z_uLongf
47# define voidpf z_voidpf 50# define voidpf z_voidpf
48# define voidp z_voidp 51# define voidp z_voidp
49#endif 52#endif
50 53
51#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32) 54#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
@@ -80,9 +83,15 @@
80# endif 83# endif
81#endif 84#endif
82 85
86#if defined __HOS_AIX__
87# ifndef STDC
88# define STDC
89# endif
90#endif
91
83#ifndef STDC 92#ifndef STDC
84# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ 93# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
85# define const 94# define const /* note: need a more gentle solution here */
86# endif 95# endif
87#endif 96#endif
88 97
@@ -210,7 +219,7 @@
210# define FAR 219# define FAR
211#endif 220#endif
212 221
213#if !defined(MACOS) && !defined(TARGET_OS_MAC) 222#if !defined(__MACTYPES__)
214typedef unsigned char Byte; /* 8 bits */ 223typedef unsigned char Byte; /* 8 bits */
215#endif 224#endif
216typedef unsigned int uInt; /* 16 bits or more */ 225typedef unsigned int uInt; /* 16 bits or more */
@@ -228,14 +237,16 @@ typedef uInt FAR uIntf;
228typedef uLong FAR uLongf; 237typedef uLong FAR uLongf;
229 238
230#ifdef STDC 239#ifdef STDC
240 typedef void const *voidpc;
231 typedef void FAR *voidpf; 241 typedef void FAR *voidpf;
232 typedef void *voidp; 242 typedef void *voidp;
233#else 243#else
244 typedef Byte const *voidpc;
234 typedef Byte FAR *voidpf; 245 typedef Byte FAR *voidpf;
235 typedef Byte *voidp; 246 typedef Byte *voidp;
236#endif 247#endif
237 248
238#ifdef HAVE_UNISTD_H 249#if 0 /* HAVE_UNISTD_H -- this line is updated by ./configure */
239# include <sys/types.h> /* for off_t */ 250# include <sys/types.h> /* for off_t */
240# include <unistd.h> /* for SEEK_* and off_t */ 251# include <unistd.h> /* for SEEK_* and off_t */
241# define z_off_t off_t 252# define z_off_t off_t
@@ -254,26 +265,16 @@ typedef uLong FAR uLongf;
254# pragma map(deflateInit_,"DEIN") 265# pragma map(deflateInit_,"DEIN")
255# pragma map(deflateInit2_,"DEIN2") 266# pragma map(deflateInit2_,"DEIN2")
256# pragma map(deflateEnd,"DEEND") 267# pragma map(deflateEnd,"DEEND")
268# pragma map(deflateBound,"DEBND")
257# pragma map(inflateInit_,"ININ") 269# pragma map(inflateInit_,"ININ")
258# pragma map(inflateInit2_,"ININ2") 270# pragma map(inflateInit2_,"ININ2")
259# pragma map(inflateEnd,"INEND") 271# pragma map(inflateEnd,"INEND")
260# pragma map(inflateSync,"INSY") 272# pragma map(inflateSync,"INSY")
261# pragma map(inflateSetDictionary,"INSEDI") 273# pragma map(inflateSetDictionary,"INSEDI")
262# pragma map(inflate_blocks,"INBL") 274# pragma map(compressBound,"CMBND")
263# pragma map(inflate_blocks_new,"INBLNE") 275# pragma map(inflate_table,"INTABL")
264# pragma map(inflate_blocks_free,"INBLFR")
265# pragma map(inflate_blocks_reset,"INBLRE")
266# pragma map(inflate_codes_free,"INCOFR")
267# pragma map(inflate_codes,"INCO")
268# pragma map(inflate_fast,"INFA") 276# pragma map(inflate_fast,"INFA")
269# pragma map(inflate_flush,"INFLU")
270# pragma map(inflate_mask,"INMA")
271# pragma map(inflate_set_dictionary,"INSEDI2")
272# pragma map(inflate_copyright,"INCOPY") 277# pragma map(inflate_copyright,"INCOPY")
273# pragma map(inflate_trees_bits,"INTRBI")
274# pragma map(inflate_trees_dynamic,"INTRDY")
275# pragma map(inflate_trees_fixed,"INTRFI")
276# pragma map(inflate_trees_free,"INTRFR")
277#endif 278#endif
278 279
279#endif /* _ZCONF_H */ 280#endif /* _ZCONF_H */
diff --git a/zconf.in.h b/zconf.in.h
new file mode 100644
index 0000000..8ef845e
--- /dev/null
+++ b/zconf.in.h
@@ -0,0 +1,280 @@
1/* zconf.h -- configuration of the zlib compression library
2 * Copyright (C) 1995-2003 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* @(#) $Id$ */
7
8#ifndef _ZCONF_H
9#define _ZCONF_H
10
11/*
12 * If you *really* need a unique prefix for all types and library functions,
13 * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
14 */
15#ifdef Z_PREFIX
16# define deflateInit_ z_deflateInit_
17# define deflate z_deflate
18# define deflateEnd z_deflateEnd
19# define inflateInit_ z_inflateInit_
20# define inflate z_inflate
21# define inflateEnd z_inflateEnd
22# define deflateInit2_ z_deflateInit2_
23# define deflateSetDictionary z_deflateSetDictionary
24# define deflateCopy z_deflateCopy
25# define deflateReset z_deflateReset
26# define deflateParams z_deflateParams
27# define deflateBound z_deflateBound
28# define inflateInit2_ z_inflateInit2_
29# define inflateSetDictionary z_inflateSetDictionary
30# define inflateSync z_inflateSync
31# define inflateSyncPoint z_inflateSyncPoint
32# define inflateCopy z_inflateCopy
33# define inflateReset z_inflateReset
34# define compress z_compress
35# define compress2 z_compress2
36# define compressBound z_compressBound
37# define uncompress z_uncompress
38# define adler32 z_adler32
39# define crc32 z_crc32
40# define get_crc_table z_get_crc_table
41
42# define Byte z_Byte
43# define uInt z_uInt
44# define uLong z_uLong
45# define Bytef z_Bytef
46# define charf z_charf
47# define intf z_intf
48# define uIntf z_uIntf
49# define uLongf z_uLongf
50# define voidpf z_voidpf
51# define voidp z_voidp
52#endif
53
54#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
55# define WIN32
56#endif
57#if defined(__GNUC__) || defined(WIN32) || defined(__386__) || defined(i386)
58# ifndef __32BIT__
59# define __32BIT__
60# endif
61#endif
62#if defined(__MSDOS__) && !defined(MSDOS)
63# define MSDOS
64#endif
65
66/*
67 * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
68 * than 64k bytes at a time (needed on systems with 16-bit int).
69 */
70#if defined(MSDOS) && !defined(__32BIT__)
71# define MAXSEG_64K
72#endif
73#ifdef MSDOS
74# define UNALIGNED_OK
75#endif
76
77#if (defined(MSDOS) || defined(_WINDOWS) || defined(WIN32)) && !defined(STDC)
78# define STDC
79#endif
80#if defined(__STDC__) || defined(__cplusplus) || defined(__OS2__)
81# ifndef STDC
82# define STDC
83# endif
84#endif
85
86#if defined __HOS_AIX__
87# ifndef STDC
88# define STDC
89# endif
90#endif
91
92#ifndef STDC
93# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
94# define const /* note: need a more gentle solution here */
95# endif
96#endif
97
98/* Some Mac compilers merge all .h files incorrectly: */
99#if defined(__MWERKS__) || defined(applec) ||defined(THINK_C) ||defined(__SC__)
100# define NO_DUMMY_DECL
101#endif
102
103/* Old Borland C incorrectly complains about missing returns: */
104#if defined(__BORLANDC__) && (__BORLANDC__ < 0x500)
105# define NEED_DUMMY_RETURN
106#endif
107
108
109/* Maximum value for memLevel in deflateInit2 */
110#ifndef MAX_MEM_LEVEL
111# ifdef MAXSEG_64K
112# define MAX_MEM_LEVEL 8
113# else
114# define MAX_MEM_LEVEL 9
115# endif
116#endif
117
118/* Maximum value for windowBits in deflateInit2 and inflateInit2.
119 * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
120 * created by gzip. (Files created by minigzip can still be extracted by
121 * gzip.)
122 */
123#ifndef MAX_WBITS
124# define MAX_WBITS 15 /* 32K LZ77 window */
125#endif
126
127/* The memory requirements for deflate are (in bytes):
128 (1 << (windowBits+2)) + (1 << (memLevel+9))
129 that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
130 plus a few kilobytes for small objects. For example, if you want to reduce
131 the default memory requirements from 256K to 128K, compile with
132 make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
133 Of course this will generally degrade compression (there's no free lunch).
134
135 The memory requirements for inflate are (in bytes) 1 << windowBits
136 that is, 32K for windowBits=15 (default value) plus a few kilobytes
137 for small objects.
138*/
139
140 /* Type declarations */
141
142#ifndef OF /* function prototypes */
143# ifdef STDC
144# define OF(args) args
145# else
146# define OF(args) ()
147# endif
148#endif
149
150/* The following definitions for FAR are needed only for MSDOS mixed
151 * model programming (small or medium model with some far allocations).
152 * This was tested only with MSC; for other MSDOS compilers you may have
153 * to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
154 * just define FAR to be empty.
155 */
156#if (defined(M_I86SM) || defined(M_I86MM)) && !defined(__32BIT__)
157 /* MSC small or medium model */
158# define SMALL_MEDIUM
159# ifdef _MSC_VER
160# define FAR _far
161# else
162# define FAR far
163# endif
164#endif
165#if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__))
166# ifndef __32BIT__
167# define SMALL_MEDIUM
168# define FAR _far
169# endif
170#endif
171
172/* Compile with -DZLIB_DLL for Windows DLL support */
173#if defined(ZLIB_DLL)
174# if defined(_WINDOWS) || defined(WINDOWS)
175# ifdef FAR
176# undef FAR
177# endif
178# include <windows.h>
179# define ZEXPORT WINAPI
180# ifdef WIN32
181# define ZEXPORTVA WINAPIV
182# else
183# define ZEXPORTVA FAR _cdecl _export
184# endif
185# endif
186# if defined (__BORLANDC__)
187# if (__BORLANDC__ >= 0x0500) && defined (WIN32)
188# include <windows.h>
189# define ZEXPORT __declspec(dllexport) WINAPI
190# define ZEXPORTRVA __declspec(dllexport) WINAPIV
191# else
192# if defined (_Windows) && defined (__DLL__)
193# define ZEXPORT _export
194# define ZEXPORTVA _export
195# endif
196# endif
197# endif
198#endif
199
200#if defined (__BEOS__)
201# if defined (ZLIB_DLL)
202# define ZEXTERN extern __declspec(dllexport)
203# else
204# define ZEXTERN extern __declspec(dllimport)
205# endif
206#endif
207
208#ifndef ZEXPORT
209# define ZEXPORT
210#endif
211#ifndef ZEXPORTVA
212# define ZEXPORTVA
213#endif
214#ifndef ZEXTERN
215# define ZEXTERN extern
216#endif
217
218#ifndef FAR
219# define FAR
220#endif
221
222#if !defined(__MACTYPES__)
223typedef unsigned char Byte; /* 8 bits */
224#endif
225typedef unsigned int uInt; /* 16 bits or more */
226typedef unsigned long uLong; /* 32 bits or more */
227
228#ifdef SMALL_MEDIUM
229 /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
230# define Bytef Byte FAR
231#else
232 typedef Byte FAR Bytef;
233#endif
234typedef char FAR charf;
235typedef int FAR intf;
236typedef uInt FAR uIntf;
237typedef uLong FAR uLongf;
238
239#ifdef STDC
240 typedef void const *voidpc;
241 typedef void FAR *voidpf;
242 typedef void *voidp;
243#else
244 typedef Byte const *voidpc;
245 typedef Byte FAR *voidpf;
246 typedef Byte *voidp;
247#endif
248
249#if 0 /* HAVE_UNISTD_H -- this line is updated by ./configure */
250# include <sys/types.h> /* for off_t */
251# include <unistd.h> /* for SEEK_* and off_t */
252# define z_off_t off_t
253#endif
254#ifndef SEEK_SET
255# define SEEK_SET 0 /* Seek from beginning of file. */
256# define SEEK_CUR 1 /* Seek from current position. */
257# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
258#endif
259#ifndef z_off_t
260# define z_off_t long
261#endif
262
263/* MVS linker does not support external names larger than 8 bytes */
264#if defined(__MVS__)
265# pragma map(deflateInit_,"DEIN")
266# pragma map(deflateInit2_,"DEIN2")
267# pragma map(deflateEnd,"DEEND")
268# pragma map(deflateBound,"DEBND")
269# pragma map(inflateInit_,"ININ")
270# pragma map(inflateInit2_,"ININ2")
271# pragma map(inflateEnd,"INEND")
272# pragma map(inflateSync,"INSY")
273# pragma map(inflateSetDictionary,"INSEDI")
274# pragma map(compressBound,"CMBND")
275# pragma map(inflate_table,"INTABL")
276# pragma map(inflate_fast,"INFA")
277# pragma map(inflate_copyright,"INCOPY")
278#endif
279
280#endif /* _ZCONF_H */
diff --git a/zlib.h b/zlib.h
index 52cb529..c514256 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 1.1.4, March 11th, 2002 2 version 1.2.0, March 9th, 2003
3 3
4 Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler 4 Copyright (C) 1995-2003 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
@@ -24,7 +24,7 @@
24 24
25 25
26 The data format used by the zlib library is described by RFCs (Request for 26 The data format used by the zlib library is described by RFCs (Request for
27 Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt 27 Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
28 (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). 28 (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
29*/ 29*/
30 30
@@ -37,7 +37,7 @@
37extern "C" { 37extern "C" {
38#endif 38#endif
39 39
40#define ZLIB_VERSION "1.1.4" 40#define ZLIB_VERSION "1.2.0"
41 41
42/* 42/*
43 The 'zlib' compression library provides in-memory compression and 43 The 'zlib' compression library provides in-memory compression and
@@ -52,8 +52,23 @@ extern "C" {
52 application must provide more input and/or consume the output 52 application must provide more input and/or consume the output
53 (providing more output space) before each call. 53 (providing more output space) before each call.
54 54
55 The compressed data format used by the in-memory functions is the zlib
56 format, which is a zlib wrapper documented in RFC 1950, wrapped around a
57 deflate stream, which is itself documented in RFC 1951.
58
55 The library also supports reading and writing files in gzip (.gz) format 59 The library also supports reading and writing files in gzip (.gz) format
56 with an interface similar to that of stdio. 60 with an interface similar to that of stdio using the functions that start
61 with "gz". The gzip format is different from the zlib format. gzip is a
62 gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
63
64 The zlib format was designed to be compact and fast for use in memory
65 and on communications channels. The gzip format was designed for single-
66 file compression on file systems, has a larger header than zlib to maintain
67 directory information, and uses a different, slower check method than zlib.
68
69 This library does not provide any functions to write gzip files in memory.
70 However such functions could be easily written using zlib's deflate function,
71 the documentation in the gzip RFC, and the examples in gzio.c.
57 72
58 The library does not install any signal handler. The decoder checks 73 The library does not install any signal handler. The decoder checks
59 the consistency of the compressed data, so the library should never 74 the consistency of the compressed data, so the library should never
@@ -244,7 +259,9 @@ ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
244 If deflate returns with avail_out == 0, this function must be called again 259 If deflate returns with avail_out == 0, this function must be called again
245 with the same value of the flush parameter and more output space (updated 260 with the same value of the flush parameter and more output space (updated
246 avail_out), until the flush is complete (deflate returns with non-zero 261 avail_out), until the flush is complete (deflate returns with non-zero
247 avail_out). 262 avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
263 avail_out is greater than six to avoid repeated flush markers due to
264 avail_out == 0 on return.
248 265
249 If the parameter flush is set to Z_FINISH, pending input is processed, 266 If the parameter flush is set to Z_FINISH, pending input is processed,
250 pending output is flushed and deflate returns with Z_STREAM_END if there 267 pending output is flushed and deflate returns with Z_STREAM_END if there
@@ -256,7 +273,7 @@ ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
256 273
257 Z_FINISH can be used immediately after deflateInit if all the compression 274 Z_FINISH can be used immediately after deflateInit if all the compression
258 is to be done in a single step. In this case, avail_out must be at least 275 is to be done in a single step. In this case, avail_out must be at least
259 0.1% larger than avail_in plus 12 bytes. If deflate does not return 276 the value returned by deflateBound (see below). If deflate does not return
260 Z_STREAM_END, then it must be called again as described above. 277 Z_STREAM_END, then it must be called again as described above.
261 278
262 deflate() sets strm->adler to the adler32 checksum of all input read 279 deflate() sets strm->adler to the adler32 checksum of all input read
@@ -272,7 +289,9 @@ ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
272 consumed and all output has been produced (only when flush is set to 289 consumed and all output has been produced (only when flush is set to
273 Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example 290 Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
274 if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible 291 if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
275 (for example avail_in or avail_out was zero). 292 (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not
293 fatal, and deflate() can be called again with more input and more output
294 space to continue compressing.
276*/ 295*/
277 296
278 297
@@ -340,11 +359,9 @@ ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
340 must be called again after making room in the output buffer because there 359 must be called again after making room in the output buffer because there
341 might be more output pending. 360 might be more output pending.
342 361
343 If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much 362 The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, or
344 output as possible to the output buffer. The flushing behavior of inflate is 363 Z_FINISH. Z_SYNC_FLUSH requests that inflate() flush as much output as
345 not specified for values of the flush parameter other than Z_SYNC_FLUSH 364 possible to the output buffer.
346 and Z_FINISH, but the current implementation actually flushes as much output
347 as possible anyway.
348 365
349 inflate() should normally be called until it returns Z_STREAM_END or an 366 inflate() should normally be called until it returns Z_STREAM_END or an
350 error. However if all decompression is to be performed in a single step 367 error. However if all decompression is to be performed in a single step
@@ -354,29 +371,43 @@ ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
354 uncompressed data. (The size of the uncompressed data may have been saved 371 uncompressed data. (The size of the uncompressed data may have been saved
355 by the compressor for this purpose.) The next operation on this stream must 372 by the compressor for this purpose.) The next operation on this stream must
356 be inflateEnd to deallocate the decompression state. The use of Z_FINISH 373 be inflateEnd to deallocate the decompression state. The use of Z_FINISH
357 is never required, but can be used to inform inflate that a faster routine 374 is never required, but can be used to inform inflate that a faster approach
358 may be used for the single inflate() call. 375 may be used for the single inflate() call.
359 376
360 If a preset dictionary is needed at this point (see inflateSetDictionary 377 In this implementation, inflate() always flushes as much output as
361 below), inflate sets strm-adler to the adler32 checksum of the 378 possible to the output buffer, and always uses the faster approach on the
362 dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise 379 first call. So the only effect of the flush parameter in this implementation
363 it sets strm->adler to the adler32 checksum of all output produced 380 is on the return value of inflate(), as noted below.
364 so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or 381
365 an error code as described below. At the end of the stream, inflate() 382 If a preset dictionary is needed after this call (see inflateSetDictionary
366 checks that its computed adler32 checksum is equal to that saved by the 383 below), inflate sets strm-adler to the adler32 checksum of the dictionary
367 compressor and returns Z_STREAM_END only if the checksum is correct. 384 chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
385 strm->adler to the adler32 checksum of all output produced so far (that is,
386 total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
387 below. At the end of the stream, inflate() checks that its computed adler32
388 checksum is equal to that saved by the compressor and returns Z_STREAM_END
389 only if the checksum is correct.
390
391 inflate() will decompress and check either zlib-wrapped or gzip-wrapped
392 deflate data. The header type is detected automatically. Any information
393 contained in the gzip header is not retained, so applications that need that
394 information should instead use raw inflate, see inflateInit2() below, or
395 inflateBack() and perform their own processing of the gzip header and
396 trailer.
368 397
369 inflate() returns Z_OK if some progress has been made (more input processed 398 inflate() returns Z_OK if some progress has been made (more input processed
370 or more output produced), Z_STREAM_END if the end of the compressed data has 399 or more output produced), Z_STREAM_END if the end of the compressed data has
371 been reached and all uncompressed output has been produced, Z_NEED_DICT if a 400 been reached and all uncompressed output has been produced, Z_NEED_DICT if a
372 preset dictionary is needed at this point, Z_DATA_ERROR if the input data was 401 preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
373 corrupted (input stream not conforming to the zlib format or incorrect 402 corrupted (input stream not conforming to the zlib format or incorrect check
374 adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent 403 value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
375 (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not 404 if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
376 enough memory, Z_BUF_ERROR if no progress is possible or if there was not 405 Z_BUF_ERROR if no progress is possible or if there was not enough room in the
377 enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR 406 output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
378 case, the application may then call inflateSync to look for a good 407 inflate() can be called again with more input and more output space to
379 compression block. 408 continue decompressing. If Z_DATA_ERROR is returned, the application may then
409 call inflateSync() to look for a good compression block if a partial recovery
410 of the data is desired.
380*/ 411*/
381 412
382 413
@@ -418,6 +449,10 @@ ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
418 compression at the expense of memory usage. The default value is 15 if 449 compression at the expense of memory usage. The default value is 15 if
419 deflateInit is used instead. 450 deflateInit is used instead.
420 451
452 windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
453 determines the window size. deflate() will then generate raw deflate data
454 with no zlib header or trailer, and will not compute an adler32 check value.
455
421 The memLevel parameter specifies how much memory should be allocated 456 The memLevel parameter specifies how much memory should be allocated
422 for the internal compression state. memLevel=1 uses minimum memory but 457 for the internal compression state. memLevel=1 uses minimum memory but
423 is slow and reduces compression ratio; memLevel=9 uses maximum memory 458 is slow and reduces compression ratio; memLevel=9 uses maximum memory
@@ -464,11 +499,12 @@ ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
464 deflate or deflate2. Thus the strings most likely to be useful should be 499 deflate or deflate2. Thus the strings most likely to be useful should be
465 put at the end of the dictionary, not at the front. 500 put at the end of the dictionary, not at the front.
466 501
467 Upon return of this function, strm->adler is set to the Adler32 value 502 Upon return of this function, strm->adler is set to the adler32 value
468 of the dictionary; the decompressor may later use this value to determine 503 of the dictionary; the decompressor may later use this value to determine
469 which dictionary has been used by the compressor. (The Adler32 value 504 which dictionary has been used by the compressor. (The adler32 value
470 applies to the whole dictionary even if only a subset of the dictionary is 505 applies to the whole dictionary even if only a subset of the dictionary is
471 actually used by the compressor.) 506 actually used by the compressor.) If a raw deflate was requested, then the
507 adler32 value is not computed and strm->adler is not set.
472 508
473 deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a 509 deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
474 parameter is invalid (such as NULL dictionary) or the stream state is 510 parameter is invalid (such as NULL dictionary) or the stream state is
@@ -507,8 +543,8 @@ ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
507*/ 543*/
508 544
509ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, 545ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
510 int level, 546 int level,
511 int strategy)); 547 int strategy));
512/* 548/*
513 Dynamically update the compression level and compression strategy. The 549 Dynamically update the compression level and compression strategy. The
514 interpretation of level and strategy is as in deflateInit2. This can be 550 interpretation of level and strategy is as in deflateInit2. This can be
@@ -527,6 +563,15 @@ ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
527 if strm->avail_out was zero. 563 if strm->avail_out was zero.
528*/ 564*/
529 565
566ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm,
567 uLong sourceLen));
568/*
569 deflateBound() returns an upper bound on the compressed size after
570 deflation of sourceLen bytes. It must be called after deflateInit()
571 or deflateInit2(). This would be used to allocate an output buffer
572 for deflation in a single pass, and so would be called before deflate().
573*/
574
530/* 575/*
531ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, 576ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
532 int windowBits)); 577 int windowBits));
@@ -542,7 +587,18 @@ ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
542 input, inflate() will return with the error code Z_DATA_ERROR instead of 587 input, inflate() will return with the error code Z_DATA_ERROR instead of
543 trying to allocate a larger window. 588 trying to allocate a larger window.
544 589
545 inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 590 windowBits can also be -8..-15 for raw inflate. In this case, -windowBits
591 determines the window size. inflate() will then process raw deflate data,
592 not looking for a zlib or gzip header, not generating a check value, and not
593 looking for any check values for comparison at the end of the stream. This
594 is for use with other formats that use the deflate compressed data format
595 such as zip. Those formats provide their own check values. If a custom
596 format is developed using the raw deflate format for compressed data, it is
597 recommended that a check value such as an adler32 or a crc32 be applied to
598 the uncompressed data as is done in the zlib, gzip, and zip formats. For
599 most applications, the zlib format should be used as is.
600
601 inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
546 memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative 602 memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
547 memLevel). msg is set to null if there is no error message. inflateInit2 603 memLevel). msg is set to null if there is no error message. inflateInit2
548 does not perform any decompression apart from reading the zlib header if 604 does not perform any decompression apart from reading the zlib header if
@@ -557,14 +613,14 @@ ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
557 Initializes the decompression dictionary from the given uncompressed byte 613 Initializes the decompression dictionary from the given uncompressed byte
558 sequence. This function must be called immediately after a call of inflate 614 sequence. This function must be called immediately after a call of inflate
559 if this call returned Z_NEED_DICT. The dictionary chosen by the compressor 615 if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
560 can be determined from the Adler32 value returned by this call of 616 can be determined from the adler32 value returned by this call of
561 inflate. The compressor and decompressor must use exactly the same 617 inflate. The compressor and decompressor must use exactly the same
562 dictionary (see deflateSetDictionary). 618 dictionary (see deflateSetDictionary).
563 619
564 inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a 620 inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
565 parameter is invalid (such as NULL dictionary) or the stream state is 621 parameter is invalid (such as NULL dictionary) or the stream state is
566 inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the 622 inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
567 expected one (incorrect Adler32 value). inflateSetDictionary does not 623 expected one (incorrect adler32 value). inflateSetDictionary does not
568 perform any decompression: this will be done by subsequent calls of 624 perform any decompression: this will be done by subsequent calls of
569 inflate(). 625 inflate().
570*/ 626*/
@@ -584,6 +640,22 @@ ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
584 until success or end of the input data. 640 until success or end of the input data.
585*/ 641*/
586 642
643ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
644 z_streamp source));
645/*
646 Sets the destination stream as a complete copy of the source stream.
647
648 This function can be useful when randomly accessing a large stream. The
649 first pass through the stream can periodically record the inflate state,
650 allowing restarting inflate at those points when randomly accessing the
651 stream.
652
653 inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
654 enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
655 (such as zalloc being NULL). msg is left unchanged in both source and
656 destination.
657*/
658
587ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); 659ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
588/* 660/*
589 This function is equivalent to inflateEnd followed by inflateInit, 661 This function is equivalent to inflateEnd followed by inflateInit,
@@ -594,6 +666,108 @@ ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
594 stream state was inconsistent (such as zalloc or state being NULL). 666 stream state was inconsistent (such as zalloc or state being NULL).
595*/ 667*/
596 668
669/*
670ZEXTERN int ZEXPORT inflateBackInit OF((z_stream FAR *strm, int windowBits,
671 unsigned char FAR *window));
672
673 Initialize the internal stream state for decompression using inflateBack()
674 calls. The fields zalloc, zfree and opaque in strm must be initialized
675 before the call. If zalloc and zfree are Z_NULL, then the default library-
676 derived memory allocation routines are used. windowBits is the base two
677 logarithm of the window size, in the range 8..15. window is a caller
678 supplied buffer of that size. Except for special applications where it is
679 assured that deflate was used with small window sizes, windowBits must be 15
680 and a 32K byte window must be supplied to be able to decompress general
681 deflate streams.
682
683 See inflateBack() for the usage of these routines.
684
685 inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
686 the paramaters are invalid, Z_MEM_ERROR if the internal state could not
687 be allocated, or Z_VERSION_ERROR if the version of the library does not
688 match the version of the header file.
689*/
690
691typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *));
692typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));
693
694ZEXTERN int ZEXPORT inflateBack OF((z_stream FAR *strm,
695 in_func in, void FAR *in_desc,
696 out_func out, void FAR *out_desc));
697/*
698 inflateBack() does a raw inflate with a single call using a call-back
699 interface for input and output. This is more efficient than inflate() for
700 file i/o applications in that it avoids copying between the output and the
701 sliding window by simply making the window itself the output buffer. This
702 function trusts the application to not change the output buffer passed by
703 the output function, at least until inflateBack() returns.
704
705 inflateBackInit() must be called first to allocate the internal state
706 and to initialize the state with the user-provided window buffer.
707 inflateBack() may then be used multiple times to inflate a complete, raw
708 deflate stream with each call. inflateBackEnd() is then called to free
709 the allocated state.
710
711 A raw deflate stream is one with no zlib or gzip header or trailer.
712 This routine would normally be used in a utility that reads zip or gzip
713 files and writes out uncompressed files. The utility would decode the
714 header and process the trailer on its own, hence this routine expects
715 only the raw deflate stream to decompress. This is different from the
716 normal behavior of inflate(), which expects either a zlib or gzip header and
717 trailer around the deflate stream.
718
719 inflateBack() uses two subroutines supplied by the caller that are then
720 called by inflateBack() for input and output. inflateBack() calls those
721 routines until it reads a complete deflate stream and writes out all of the
722 uncompressed data, or until it encounters an error. The function's
723 parameters and return types are defined above in the in_func and out_func
724 typedefs. inflateBack() will call in(in_desc, &buf) which should return the
725 number of bytes of provided input, and a pointer to that input in buf. If
726 there is no input available, in() must return zero--buf is ignored in that
727 case--and inflateBack() will return a buffer error. inflateBack() will call
728 out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out()
729 should return zero on success, or non-zero on failure. If out() returns
730 non-zero, inflateBack() will return with an error. Neither in() nor out()
731 are permitted to change the contents of the window provided to
732 inflateBackInit(), which is also the buffer that out() uses to write from.
733 The length written by out() will be at most the window size. Any non-zero
734 amount of input may be provided by in().
735
736 For convenience, inflateBack() can be provided input on the first call by
737 setting strm->next_in and strm->avail_in. If that input is exhausted, then
738 in() will be called. Therefore strm->next_in must be initialized before
739 calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called
740 immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in
741 must also be initialized, and then if strm->avail_in is not zero, input will
742 initially be taken from strm->next_in[0 .. strm->avail_in - 1].
743
744 The in_desc and out_desc parameters of inflateBack() is passed as the
745 first parameter of in() and out() respectively when they are called. These
746 descriptors can be optinally used to pass any information that the caller-
747 supplied in() and out() functions need to do their job.
748
749 On return, inflateBack() will set strm->next_in and strm->avail_in to
750 pass back any unused input that was provided by the last in() call. The
751 return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
752 if in() or out() returned an error, Z_DATA_ERROR if there was a format
753 error in the deflate stream (in which case strm->msg is set to indicate the
754 nature of the error), or Z_STREAM_ERROR if the stream was not properly
755 initialized. In the case of Z_BUF_ERROR, an input or output error can be
756 distinguished using strm->next_in which will be Z_NULL only if in() returned
757 an error. If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to
758 out() returning non-zero. (in() will always be called before out(), so
759 strm->next_in is assured to be defined if out() returns non-zero.) Note
760 that inflateBack() cannot return Z_OK.
761*/
762
763ZEXTERN int ZEXPORT inflateBackEnd(z_stream FAR *strm);
764/*
765 All memory allocated by inflateBackInit() is freed.
766
767 inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream
768 state was inconsistent.
769*/
770
597 771
598 /* utility functions */ 772 /* utility functions */
599 773
@@ -610,8 +784,8 @@ ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
610/* 784/*
611 Compresses the source buffer into the destination buffer. sourceLen is 785 Compresses the source buffer into the destination buffer. sourceLen is
612 the byte length of the source buffer. Upon entry, destLen is the total 786 the byte length of the source buffer. Upon entry, destLen is the total
613 size of the destination buffer, which must be at least 0.1% larger than 787 size of the destination buffer, which must be at least the value returned
614 sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the 788 by compressBound(sourceLen). Upon exit, destLen is the actual size of the
615 compressed buffer. 789 compressed buffer.
616 This function can be used to compress a whole file at once if the 790 This function can be used to compress a whole file at once if the
617 input file is mmap'ed. 791 input file is mmap'ed.
@@ -627,14 +801,22 @@ ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
627 Compresses the source buffer into the destination buffer. The level 801 Compresses the source buffer into the destination buffer. The level
628 parameter has the same meaning as in deflateInit. sourceLen is the byte 802 parameter has the same meaning as in deflateInit. sourceLen is the byte
629 length of the source buffer. Upon entry, destLen is the total size of the 803 length of the source buffer. Upon entry, destLen is the total size of the
630 destination buffer, which must be at least 0.1% larger than sourceLen plus 804 destination buffer, which must be at least the value returned by
631 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. 805 compressBound(sourceLen). Upon exit, destLen is the actual size of the
806 compressed buffer.
632 807
633 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 808 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
634 memory, Z_BUF_ERROR if there was not enough room in the output buffer, 809 memory, Z_BUF_ERROR if there was not enough room in the output buffer,
635 Z_STREAM_ERROR if the level parameter is invalid. 810 Z_STREAM_ERROR if the level parameter is invalid.
636*/ 811*/
637 812
813ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
814/*
815 compressBound() returns an upper bound on the compressed size after
816 compress() or compress2() on sourceLen bytes. It would be used before
817 a compress() or compress2() call to allocate the destination buffer.
818*/
819
638ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, 820ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
639 const Bytef *source, uLong sourceLen)); 821 const Bytef *source, uLong sourceLen));
640/* 822/*
@@ -650,7 +832,7 @@ ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
650 832
651 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not 833 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
652 enough memory, Z_BUF_ERROR if there was not enough room in the output 834 enough memory, Z_BUF_ERROR if there was not enough room in the output
653 buffer, or Z_DATA_ERROR if the input data was corrupted. 835 buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
654*/ 836*/
655 837
656 838
@@ -702,7 +884,7 @@ ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
702 end of file, -1 for error). */ 884 end of file, -1 for error). */
703 885
704ZEXTERN int ZEXPORT gzwrite OF((gzFile file, 886ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
705 const voidp buf, unsigned len)); 887 voidpc buf, unsigned len));
706/* 888/*
707 Writes the given number of uncompressed bytes into the compressed file. 889 Writes the given number of uncompressed bytes into the compressed file.
708 gzwrite returns the number of uncompressed bytes actually written 890 gzwrite returns the number of uncompressed bytes actually written
@@ -713,7 +895,10 @@ ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...));
713/* 895/*
714 Converts, formats, and writes the args to the compressed file under 896 Converts, formats, and writes the args to the compressed file under
715 control of the format string, as in fprintf. gzprintf returns the number of 897 control of the format string, as in fprintf. gzprintf returns the number of
716 uncompressed bytes actually written (0 in case of error). 898 uncompressed bytes actually written (0 in case of error). The number of
899 uncompressed bytes written is limited to 4095. The caller should assure
900 that this limit is not exceeded, since otherwise a buffer overflow may
901 result.
717*/ 902*/
718 903
719ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); 904ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
@@ -755,7 +940,7 @@ ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
755*/ 940*/
756 941
757ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, 942ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
758 z_off_t offset, int whence)); 943 z_off_t offset, int whence));
759/* 944/*
760 Sets the starting position for the next gzread or gzwrite on the 945 Sets the starting position for the next gzread or gzwrite on the
761 given compressed file. The offset represents a number of bytes in the 946 given compressed file. The offset represents a number of bytes in the
@@ -867,6 +1052,10 @@ ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
867 int stream_size)); 1052 int stream_size));
868ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, 1053ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
869 const char *version, int stream_size)); 1054 const char *version, int stream_size));
1055ZEXTERN int ZEXPORT inflateBackInit_ OF((z_stream FAR *strm, int windowBits,
1056 unsigned char FAR *window,
1057 const char *version,
1058 int stream_size));
870#define deflateInit(strm, level) \ 1059#define deflateInit(strm, level) \
871 deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream)) 1060 deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
872#define inflateInit(strm) \ 1061#define inflateInit(strm) \
@@ -876,6 +1065,9 @@ ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
876 (strategy), ZLIB_VERSION, sizeof(z_stream)) 1065 (strategy), ZLIB_VERSION, sizeof(z_stream))
877#define inflateInit2(strm, windowBits) \ 1066#define inflateInit2(strm, windowBits) \
878 inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream)) 1067 inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
1068#define inflateBackInit(strm, windowBits, window) \
1069 inflateBackInit_((strm), (windowBits), (window), \
1070 ZLIB_VERSION, sizeof(z_stream))
879 1071
880 1072
881#if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL) 1073#if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)
diff --git a/zutil.c b/zutil.c
index dfc38ec..4a8118d 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-2002 Jean-loup Gailly. 2 * Copyright (C) 1995-2003 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -13,7 +13,7 @@ struct internal_state {int dummy;}; /* for buggy compilers */
13extern void exit OF((int)); 13extern void exit OF((int));
14#endif 14#endif
15 15
16const char *z_errmsg[10] = { 16const char * const z_errmsg[10] = {
17"need dictionary", /* Z_NEED_DICT 2 */ 17"need dictionary", /* Z_NEED_DICT 2 */
18"stream end", /* Z_STREAM_END 1 */ 18"stream end", /* Z_STREAM_END 1 */
19"", /* Z_OK 0 */ 19"", /* Z_OK 0 */
@@ -95,6 +95,7 @@ void zmemzero(dest, len)
95 95
96#ifdef __TURBOC__ 96#ifdef __TURBOC__
97#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__) 97#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
98#if !defined(__linux)
98/* Small and medium model in Turbo C are for now limited to near allocation 99/* Small and medium model in Turbo C are for now limited to near allocation
99 * with reduced MAX_WBITS and MAX_MEM_LEVEL 100 * with reduced MAX_WBITS and MAX_MEM_LEVEL
100 */ 101 */
@@ -170,6 +171,7 @@ void zcfree (voidpf opaque, voidpf ptr)
170 Assert(0, "zcfree: ptr not found"); 171 Assert(0, "zcfree: ptr not found");
171} 172}
172#endif 173#endif
174#endif
173#endif /* __TURBOC__ */ 175#endif /* __TURBOC__ */
174 176
175 177
@@ -201,6 +203,7 @@ void zcfree (voidpf opaque, voidpf ptr)
201#ifndef MY_ZCALLOC /* Any system without a special alloc function */ 203#ifndef MY_ZCALLOC /* Any system without a special alloc function */
202 204
203#ifndef STDC 205#ifndef STDC
206extern voidp malloc OF((uInt size));
204extern voidp calloc OF((uInt items, uInt size)); 207extern voidp calloc OF((uInt items, uInt size));
205extern void free OF((voidpf ptr)); 208extern void free OF((voidpf ptr));
206#endif 209#endif
@@ -211,7 +214,8 @@ voidpf zcalloc (opaque, items, size)
211 unsigned size; 214 unsigned size;
212{ 215{
213 if (opaque) items += size - size; /* make compiler happy */ 216 if (opaque) items += size - size; /* make compiler happy */
214 return (voidpf)calloc(items, size); 217 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
218 (voidpf)calloc(items, size);
215} 219}
216 220
217void zcfree (opaque, ptr) 221void zcfree (opaque, ptr)
diff --git a/zutil.h b/zutil.h
index 718ebc1..d9c36ab 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-2002 Jean-loup Gailly. 2 * Copyright (C) 1995-2003 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
@@ -37,7 +37,7 @@ typedef unsigned short ush;
37typedef ush FAR ushf; 37typedef ush FAR ushf;
38typedef unsigned long ulg; 38typedef unsigned long ulg;
39 39
40extern const char *z_errmsg[10]; /* indexed by 2-zlib_error */ 40extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
41/* (size given to avoid silly warnings with Visual C++) */ 41/* (size given to avoid silly warnings with Visual C++) */
42 42
43#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)] 43#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
@@ -207,8 +207,6 @@ extern const char *z_errmsg[10]; /* indexed by 2-zlib_error */
207#endif 207#endif
208 208
209 209
210typedef uLong (ZEXPORT *check_func) OF((uLong check, const Bytef *buf,
211 uInt len));
212voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); 210voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
213void zcfree OF((voidpf opaque, voidpf ptr)); 211void zcfree OF((voidpf opaque, voidpf ptr));
214 212