Compare commits

...

3 Commits

Author SHA1 Message Date
Mark Adler
8a2acbffc8 zlib 1.0-pre 2011-09-09 23:13:27 -07:00
Mark Adler
56bcb184fa zlib 0.99 2011-09-09 23:11:37 -07:00
Mark Adler
25e5325501 zlib 0.95 2011-09-09 23:10:21 -07:00
24 changed files with 861 additions and 404 deletions

View File

@@ -1,6 +1,48 @@
ChangeLog file for zlib
Changes in 1.0 (15 Jan 96)
- fixed deflate for 64-bit systems (detected on Cray)
- fixed inflate_blocks for 64-bit systems (detected on Alpha)
- make falloc completely reentrant (inftrees.c)
- fixed very unlikely race condition in ct_static_init
- always return Z_BUF_ERROR when deflate() has nothing to do
- add deflateParams in zlib.h.
- test large buffers and deflateParams in example.c
- declare Z_DEFLATED in zlib.h (possible parameter for deflateInit2)
- Document Z_SYNC_FLUSH
- added deflateGetAdler32 and deflateSetAdler32
- free in reverse order of allocation to help memory manager
- Check for __cpluplus in zlib.h
- Finer test in ct_align for partial flush
- avoid harmless warnings for Borland C++
- initialize hash_head in deflate.c
- avoid warning on fdopen (gzio.c) for HP cc -Aa
- include stdlib.h for STDC compilers
- include errno.h for Cray
- ignore error if ranlib doesn't exist
- call ranlib twice for NeXTSTEP
- use exec_prefix instead of prefix for libz.a
- renamed ct_* as tr_* to avoid conflict with another application.
- clear z->msg in inflateInit2 before any error return
- initialize opaque in example.c, gzio.c, deflate.c and inflate.c
- fixed typo in zconf.h (_GNUC__ => __GNUC__)
- check for WIN32 in zconf.h and zutil.c (avoid farmalloc in 32-bit mode)
- fix typo in Make_vms.com (f$trnlnm -> f$getsyi)
- in fcalloc, normalize pointer if size > 65520 bytes
- don't use special fcalloc for 32 bit Borland C++
- use STDC instead of __GO32__ to avoid redeclaring exit, calloc, etc...
Changes in 0.95 (16 Aug 95)
- fix MSDOS small and medium model (now easier to adapt to any compiler)
- inlined send_bits
- fix the final (:-) bug for deflate with flush (output was correct but
not completely flushed in rare occasions).
- default window size is same for compression and decompression
(it's now sufficient to set MAX_WBITS in zconf.h).
- voidp -> voidpf and voidnp -> voidp (for consistency with other
typedefs and because voidnp was not near in large model).
Changes in 0.94 (13 Aug 95)
- support MSDOS medium model
- fix deflate with flush (could sometimes generate bad output)
@@ -10,6 +52,7 @@ Changes in 0.94 (13 Aug 95)
- gzflush now calls fflush
- For deflate with flush, flush even if no more input is provided.
- rename libgz.a as libz.a
- avoid complex expression in infcodes.c triggering Turbo C bug
- work around a problem with gcc on Alpha (in INSERT_STRING)
- don't use inline functions (problem with some gcc versions)
- allow renaming of Byte, uInt, etc... with #define.

View File

@@ -4,7 +4,7 @@ $!
$! Look for the compiler used
$!
$ ccopt = ""
$ if f$trnlnm("HW_MODEL").ge.1024
$ if f$getsyi("HW_MODEL").ge.1024
$ then
$ ccopt = "/prefix=all"+ccopt
$ comp = "__decc__=1"

View File

@@ -1,10 +1,10 @@
# Makefile for zlib
# Copyright (C) 1995 Jean-loup Gailly.
# Copyright (C) 1995-1996 Jean-loup Gailly.
# For conditions of distribution and use, see copyright notice in zlib.h
CC=cc
CFLAGS=-O
#use -O3 for gcc to take advantage of inlining
#use -O3 for gcc
#CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
#CFLAGS=-g -DDEBUG
LDFLAGS=-L. -lz
@@ -12,6 +12,7 @@ LDFLAGS=-L. -lz
RANLIB=ranlib
prefix=/usr/local
exec_prefix = ${prefix}
OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \
zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o inffast.o
@@ -26,15 +27,17 @@ test: all
install: libz.a
-@mkdir $(prefix)/include
-@mkdir $(prefix)/lib
-@mkdir $(exec_prefix)/lib
cp zlib.h zconf.h $(prefix)/include
chmod 644 $(prefix)/include/zlib.h $(prefix)/include/zconf.h
cp libz.a $(prefix)/lib
chmod 644 $(prefix)/lib/libz.a
cp libz.a $(exec_prefix)/lib
chmod 644 $(exec_prefix)/lib/libz.a
-@$(RANLIB) $(prefix)/lib/libz.a
# This second ranlib is needed on NeXTSTEP which checks file times
libz.a: $(OBJS)
ar rc $@ $(OBJS)
$(RANLIB) $@
-@$(RANLIB) $@
example: example.o libz.a
$(CC) $(CFLAGS) -o $@ example.o $(LDFLAGS)
@@ -53,6 +56,9 @@ tgz:
cd ..; tar cfz zlib/zlib.tgz zlib/README zlib/ChangeLog zlib/Makefile \
zlib/Make????.??? zlib/Makefile.?? zlib/descrip.mms zlib/*.[ch]
TAGS:
etags *.[ch]
# DO NOT DELETE THIS LINE -- make depend depends on it.
adler32.o: zutil.h zlib.h zconf.h

View File

@@ -2,6 +2,7 @@
# Borland C++ ************ UNTESTED ***********
# To use, do "make -fmakefile.bor"
# To compile in small model, set below: MODEL=-ms
# WARNING: the small model is supported but only for small values of
# MAX_WBITS and MAX_MEM_LEVEL. For example:

View File

@@ -2,6 +2,7 @@
# Microsoft C 5.1 or later
# To use, do "make makefile.msc"
# To compile in small model, set below: MODEL=-AS
# If you wish to reduce the memory requirements (default 256K for big
# objects plus a few K), you can add to CFLAGS below:

View File

@@ -2,10 +2,11 @@
# TurboC 2.0
# To use, do "make -fmakefile.tc"
# To compile in small model, set below: MODEL=-ms
# WARNING: the small model is supported but only for small values of
# MAX_WBITS and MAX_MEM_LEVEL. For example:
# -DMAX_WBITS=11 -DDEF_WBITS=11 -DMAX_MEM_LEVEL=3
# -DMAX_WBITS=11 -DMAX_MEM_LEVEL=3
# If you wish to reduce the memory requirements (default 256K for big
# objects plus a few K), you can add to CFLAGS below:
# -DMAX_MEM_LEVEL=7 -DMAX_WBITS=14
@@ -13,6 +14,7 @@
# ------------- Turbo C 2.0 -------------
MODEL=-ml
# CFLAGS=-O2 -G -Z $(MODEL) -DMAX_WBITS=11 -DMAX_MEM_LEVEL=3
CFLAGS=-O2 -G -Z $(MODEL)
CC=tcc -I\tc\include
LD=tcc -L\tc\lib

64
README
View File

@@ -1,41 +1,59 @@
zlib 0.94 is a beta version of a general purpose compression library.
Unless new bugs are found it will be released again as the first official
version (1.0). This version has no known bugs.
zlib 1.0 is a general purpose data compression library.
All the code is reentrant (thread safe).
The data format used by the zlib library is described in the
files zlib-3.1.doc, deflate-1.1.doc and gzip-4.1.doc, available
in ftp.uu.net:/pub/archiving/zip/doc.
files zlib-3.2.doc, deflate-1.2.doc and gzip-4.2.doc, available
in ftp://ftp.uu.net/pub/archiving/zip/doc
All functions of the compression library are documented in the file
zlib.h. A usage example of the library is given in the file example.c
which also tests that the library is working correctly.
which also tests that the library is working correctly. Another
example is given in the file minigzip.c.
To compile all files and run the test program, just type: make test
(For MSDOS, use one of the special makefiles such as Makefile.msc.)
(For MSDOS, use one of the special makefiles such as Makefile.msc;
for VMS, use Make_vms.com or descrip.mms.)
To install the zlib library (libz.a) in /usr/local/lib, type: make install
To install in a different directory, use for example:
make install prefix=$HOME
This will install in $HOME/lib instead of /usr/local/lib.
The changes made in version 0.94 are documented in the file ChangeLog.
The main changes since 0.93 are:
- support MSDOS small and medium model
- fix deflate with flush (could sometimes generate bad output)
- fix deflateReset (zlib header was incorrectly suppressed)
- added support for VMS
- allow a compression level in gzopen()
- gzflush now calls fflush
- For deflate with flush, flush even if no more input is provided.
- rename libgz.a as libz.a
Questions about zlib should be sent to <zlib@quest.jpl.nasa.gov>.
For MSDOS, the small and medium models are supported only for Microsoft C.
(It should be easy to support them for Borland C also, but I don't have
a recent Borland compiler to test with.) The small model currently works
for Turbo/Borland C but only with reduced performance to avoid any far
allocation (tested with -DMAX_WBITS=11 -DDEF_WBITS=11 -DMAX_MEM_LEVEL=3).
The changes made in version 1.0 are documented in the file ChangeLog.
The main changes since 0.95 are:
- allow compression level 0 (no compression)
- add deflateParams in zlib.h: allow dynamic change of compression level
and compression strategy.
- test large buffers and deflateParams in example.c
- declare Z_DEFLATED in zlib.h (possible parameter for deflateInit2)
- fixes for 64-bit systems (needed for Alpha and Cray)
- always return Z_BUF_ERROR when deflate() has nothing to do
- fix some very unlikely race conditions.
- several minor fixes for better portability
- free in reverse order of allocation to help memory manager
Notes for some targets:
- For MSDOS, the small and medium models have been tested only with
Microsoft C. (This should work for Borland C also, but I don't have
a Borland compiler to test with.) The small model was tested with
Turbo C but only with reduced performance to avoid any far
allocation; it was tested with -DMAX_WBITS=11 -DMAX_MEM_LEVEL=3
MS Visual C++1.5 in far model gives:
warning C4746: 'z_errmsg' : unsized array treated as '__far'
warning C4746: 'inflate_mask' : unsized array treated as '__far'
Ignore those warnings.
- For 64-bit Iris, deflate.c must be compiled without any optimization.
With -O, one libpng test fails. The test works in 32 bit mode (with the
-32 compiler flag).
(C) 1995 Jean-loup Gailly and Mark Adler
Copyright notice:
(C) 1995-1996 Jean-loup Gailly and Mark Adler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages

View File

@@ -39,6 +39,7 @@ int compress (dest, destLen, source, sourceLen)
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
stream.opaque = (voidpf)0;
err = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
if (err != Z_OK) return err;

331
deflate.c
View File

@@ -51,7 +51,7 @@
#include "deflate.h"
char copyright[] = " deflate Copyright 1995 Jean-loup Gailly ";
char deflate_copyright[] = " deflate 1.0 Copyright 1995 Jean-loup Gailly ";
/*
If you use the zlib library in a product, an acknowledgment is welcome
in the documentation of your product. If for some reason you cannot
@@ -59,61 +59,12 @@ char copyright[] = " deflate Copyright 1995 Jean-loup Gailly ";
copyright string in the executable of your product.
*/
#define NIL 0
/* Tail of hash chains */
#ifndef TOO_FAR
# define TOO_FAR 4096
#endif
/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
/* Minimum amount of lookahead, except at the end of the input file.
* See deflate.c for comments about the MIN_MATCH+1.
*/
/* Values for max_lazy_match, good_match and max_chain_length, depending on
* the desired pack level (0..9). The values given below have been tuned to
* exclude worst case performance for pathological files. Better values may be
* found for specific files.
*/
typedef struct config_s {
ush good_length; /* reduce lazy search above this match length */
ush max_lazy; /* do not perform lazy search above this match length */
ush nice_length; /* quit search above this match length */
ush max_chain;
} config;
local config configuration_table[10] = {
/* good lazy nice chain */
/* 0 */ {0, 0, 0, 0}, /* store only */
/* 1 */ {4, 4, 8, 4}, /* maximum speed, no lazy matches */
/* 2 */ {4, 5, 16, 8},
/* 3 */ {4, 6, 32, 32},
/* 4 */ {4, 4, 16, 16}, /* lazy matches */
/* 5 */ {8, 16, 32, 32},
/* 6 */ {8, 16, 128, 128},
/* 7 */ {8, 32, 128, 256},
/* 8 */ {32, 128, 258, 1024},
/* 9 */ {32, 258, 258, 4096}}; /* maximum compression */
/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
* For deflate_fast() (levels <= 3) good is ignored and lazy has a different
* meaning.
*/
#define EQUAL 0
/* result of memcmp for equal strings */
struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
/* ===========================================================================
* Prototypes for local functions.
*/
local void fill_window OF((deflate_state *s));
local int deflate_stored OF((deflate_state *s, int flush));
local int deflate_fast OF((deflate_state *s, int flush));
local int deflate_slow OF((deflate_state *s, int flush));
local void lm_init OF((deflate_state *s));
@@ -130,6 +81,62 @@ local void check_match OF((deflate_state *s, IPos start, IPos match,
int length));
#endif
/* ===========================================================================
* Local data
*/
#define NIL 0
/* Tail of hash chains */
#ifndef TOO_FAR
# define TOO_FAR 4096
#endif
/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
/* Minimum amount of lookahead, except at the end of the input file.
* See deflate.c for comments about the MIN_MATCH+1.
*/
typedef int (*compress_func) OF((deflate_state *s, int flush));
/* Compressing function */
/* Values for max_lazy_match, good_match and max_chain_length, depending on
* the desired pack level (0..9). The values given below have been tuned to
* exclude worst case performance for pathological files. Better values may be
* found for specific files.
*/
typedef struct config_s {
ush good_length; /* reduce lazy search above this match length */
ush max_lazy; /* do not perform lazy search above this match length */
ush nice_length; /* quit search above this match length */
ush max_chain;
compress_func func;
} config;
local config configuration_table[10] = {
/* good lazy nice chain */
/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
/* 1 */ {4, 4, 8, 4, deflate_fast}, /* maximum speed, no lazy matches */
/* 2 */ {4, 5, 16, 8, deflate_fast},
/* 3 */ {4, 6, 32, 32, deflate_fast},
/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
/* 5 */ {8, 16, 32, 32, deflate_slow},
/* 6 */ {8, 16, 128, 128, deflate_slow},
/* 7 */ {8, 32, 128, 256, deflate_slow},
/* 8 */ {32, 128, 258, 1024, deflate_slow},
/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
* For deflate_fast() (levels <= 3) good is ignored and lazy has a different
* meaning.
*/
#define EQUAL 0
/* result of memcmp for equal strings */
struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
/* ===========================================================================
* Update a hash value with the given input byte
@@ -166,7 +173,7 @@ int deflateInit (strm, level)
z_stream *strm;
int level;
{
return deflateInit2 (strm, level, DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 0);
return deflateInit2 (strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 0);
/* To do: ignore strm->next_in if we use it as window */
}
@@ -185,7 +192,10 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
if (strm == Z_NULL) return Z_STREAM_ERROR;
strm->msg = Z_NULL;
if (strm->zalloc == Z_NULL) strm->zalloc = zcalloc;
if (strm->zalloc == Z_NULL) {
strm->zalloc = zcalloc;
strm->opaque = (voidpf)0;
}
if (strm->zfree == Z_NULL) strm->zfree = zcfree;
if (level == Z_DEFAULT_COMPRESSION) level = 6;
@@ -194,8 +204,9 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
noheader = 1;
windowBits = -windowBits;
}
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != DEFLATED ||
windowBits < 8 || windowBits > 15 || level < 1 || level > 9) {
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
return Z_STREAM_ERROR;
}
s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
@@ -227,11 +238,11 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
deflateEnd (strm);
return Z_MEM_ERROR;
}
s->d_buf = (ushf *) &(s->pending_buf[s->lit_bufsize]);
s->l_buf = (uchf *) &(s->pending_buf[3*s->lit_bufsize]);
s->l_buf = (uchf *) &(s->pending_buf[s->lit_bufsize]);
s->d_buf = (ushf *) &(s->pending_buf[2*s->lit_bufsize]);
/* We overlay pending_buf and d_buf+l_buf. This works since the average
* output size for (length,distance) codes is <= 32 bits (worst case
* is 15+15+13=33).
* is 15+15+13=33). d_buf is put last in case sizeof(short)>2.
*/
s->level = level;
@@ -241,6 +252,35 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
return deflateReset(strm);
}
/* =========================================================================
* Undocumented function to return the Adler32 of a stream. It can be
* called immediately after a flush with Z_SYNC_FLUSH, to allow later
* resumption of the compressor with deflateSetAdler32.
*/
uLong deflateGetAdler32 (strm)
z_stream *strm;
{
if (strm == Z_NULL || strm->state == Z_NULL) return (uLong)Z_STREAM_ERROR;
/* Z_STREAM_ERROR happens to be an invalid Adler32 value. */
return strm->state->adler;
}
/* =========================================================================
* Undocumented function to set the Adler32 of a stream. It can be called
* immediately after deflateInit to reset the Adler32 of a compression
* stream which had been interrupted.
*/
int deflateSetAdler32 (strm, adler)
z_stream *strm;
uLong adler;
{
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
strm->state->adler = adler;
return Z_OK;
}
/* ========================================================================= */
int deflateReset (strm)
z_stream *strm;
@@ -263,13 +303,51 @@ int deflateReset (strm)
}
s->status = s->noheader ? BUSY_STATE : INIT_STATE;
s->adler = 1;
s->last_flush = Z_NO_FLUSH;
ct_init(s);
tr_init(s);
lm_init(s);
return Z_OK;
}
/* ========================================================================= */
int deflateParams(strm, level, strategy)
z_stream *strm;
int level;
int strategy;
{
deflate_state *s;
compress_func func;
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
s = strm->state;
if (level == Z_DEFAULT_COMPRESSION) {
level = 6;
}
if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
return Z_STREAM_ERROR;
}
func = configuration_table[s->level].func;
if (func != configuration_table[level].func
&& strm->state->lookahead != 0) {
/* Flush the last buffer: */
(void)(*func)(strm->state, Z_PARTIAL_FLUSH);
}
if (s->level != level) {
s->level = level;
s->max_lazy_match = configuration_table[level].max_lazy;
s->good_match = configuration_table[level].good_length;
s->nice_match = configuration_table[level].nice_length;
s->max_chain_length = configuration_table[level].max_chain;
}
s->strategy = strategy;
return Z_OK;
}
/* =========================================================================
* Put a short in the pending buffer. The 16-bit value is put in MSB order.
* IN assertion: the stream state is correct and there is enough room in
@@ -284,7 +362,10 @@ local void putShortMSB (s, b)
}
/* =========================================================================
* Flush as much pending output as possible.
* Flush as much pending output as possible. All deflate() output goes
* through this function so some applications may wish to modify it
* to avoid allocating a large strm->next_out buffer and copying into it.
* (See also read_buf()).
*/
local void flush_pending(strm)
z_stream *strm;
@@ -310,20 +391,25 @@ int deflate (strm, flush)
z_stream *strm;
int flush;
{
int old_flush; /* value of flush param for previous deflate call */
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
if (strm->next_out == Z_NULL ||
(strm->next_in == Z_NULL && strm->avail_in != 0)) {
(strm->next_in == Z_NULL && strm->avail_in != 0) ||
(strm->state->status == FINISH_STATE && flush != Z_FINISH)) {
ERR_RETURN(strm, Z_STREAM_ERROR);
}
if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
strm->state->strm = strm; /* just in case */
old_flush = strm->state->last_flush;
strm->state->last_flush = flush;
/* Write the zlib header */
if (strm->state->status == INIT_STATE) {
uInt header = (DEFLATED + ((strm->state->w_bits-8)<<4)) << 8;
uInt header = (Z_DEFLATED + ((strm->state->w_bits-8)<<4)) << 8;
uInt level_flags = (strm->state->level-1) >> 1;
if (level_flags > 3) level_flags = 3;
@@ -338,6 +424,14 @@ int deflate (strm, flush)
if (strm->state->pending != 0) {
flush_pending(strm);
if (strm->avail_out == 0) return Z_OK;
/* Make sure there is something to do and avoid duplicate consecutive
* flushes. For repeated and useless calls with Z_FINISH, we keep
* returning Z_STREAM_END instead of Z_BUFF_ERROR.
*/
} else if (strm->avail_in == 0 && flush <= old_flush &&
flush != Z_FINISH) {
ERR_RETURN(strm, Z_BUF_ERROR);
}
/* User must not provide more input after the first FINISH: */
@@ -354,11 +448,9 @@ int deflate (strm, flush)
if (flush == Z_FINISH) {
strm->state->status = FINISH_STATE;
}
if (strm->state->level <= 3) {
quit = deflate_fast(strm->state, flush);
} else {
quit = deflate_slow(strm->state, flush);
}
quit = (*(configuration_table[strm->state->level].func))
(strm->state, flush);
if (quit || strm->avail_out == 0) return Z_OK;
/* If flush != Z_NO_FLUSH && avail_out == 0, the next call
* of deflate should use the same flush parameter to make sure
@@ -367,11 +459,11 @@ int deflate (strm, flush)
* ensures that for a very small output buffer, we emit at most
* one empty block.
*/
if (flush != Z_OK && flush != Z_FINISH) {
if (flush != Z_NO_FLUSH && flush != Z_FINISH) {
if (flush == Z_PARTIAL_FLUSH) {
ct_align(strm->state);
tr_align(strm->state);
} else { /* FULL_FLUSH or SYNC_FLUSH */
ct_stored_block(strm->state, (char*)0, 0L, 0);
tr_stored_block(strm->state, (char*)0, 0L, 0);
/* For a full flush, this empty block will be recognized
* as a special marker by inflate_sync().
*/
@@ -405,10 +497,11 @@ int deflateEnd (strm)
{
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
TRY_FREE(strm, strm->state->window);
TRY_FREE(strm, strm->state->prev);
TRY_FREE(strm, strm->state->head);
/* Deallocate in reverse order of allocations: */
TRY_FREE(strm, strm->state->pending_buf);
TRY_FREE(strm, strm->state->head);
TRY_FREE(strm, strm->state->prev);
TRY_FREE(strm, strm->state->window);
ZFREE(strm, strm->state);
strm->state = Z_NULL;
@@ -438,7 +531,10 @@ int deflateCopy (dest, source)
/* ===========================================================================
* Read a new buffer from the current input stream, update the adler32
* and total number of bytes read.
* and total number of bytes read. All deflate() input goes through
* this function so some applications may wish to modify it to avoid
* allocating a large strm->next_in buffer and copying from it.
* (See also flush_pending()).
*/
local int read_buf(strm, buf, size)
z_stream *strm;
@@ -482,7 +578,7 @@ local void lm_init (s)
s->strstart = 0;
s->block_start = 0L;
s->lookahead = 0;
s->match_length = MIN_MATCH-1;
s->match_length = s->prev_length = MIN_MATCH-1;
s->match_available = 0;
s->ins_h = 0;
#ifdef ASMV
@@ -524,8 +620,8 @@ local int longest_match(s, cur_match)
* Try with and without -DUNALIGNED_OK to check.
*/
register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
register ush scan_start = *(ush*)scan;
register ush scan_end = *(ush*)(scan+best_len-1);
register ush scan_start = *(ushf*)scan;
register ush scan_end = *(ushf*)(scan+best_len-1);
#else
register Bytef *strend = s->window + s->strstart + MAX_MATCH;
register Byte scan_end1 = scan[best_len-1];
@@ -554,8 +650,8 @@ local int longest_match(s, cur_match)
/* This code assumes sizeof(unsigned short) == 2. Do not use
* UNALIGNED_OK if your compiler uses a different size.
*/
if (*(ush*)(match+best_len-1) != scan_end ||
*(ush*)match != scan_start) continue;
if (*(ushf*)(match+best_len-1) != scan_end ||
*(ushf*)match != scan_start) continue;
/* It is not necessary to compare scan[2] and match[2] since they are
* always equal when the other bytes match, given that the hash keys
@@ -569,10 +665,10 @@ local int longest_match(s, cur_match)
Assert(scan[2] == match[2], "scan[2]?");
scan++, match++;
do {
} while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
*(ush*)(scan+=2) == *(ush*)(match+=2) &&
*(ush*)(scan+=2) == *(ush*)(match+=2) &&
*(ush*)(scan+=2) == *(ush*)(match+=2) &&
} while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
scan < strend);
/* The funny "do {}" generates better code on most compilers */
@@ -621,7 +717,7 @@ local int longest_match(s, cur_match)
best_len = len;
if (len >= s->nice_match) break;
#ifdef UNALIGNED_OK
scan_end = *(ush*)(scan+best_len-1);
scan_end = *(ushf*)(scan+best_len-1);
#else
scan_end1 = scan[best_len-1];
scan_end = scan[best_len];
@@ -686,6 +782,7 @@ local void fill_window(s)
/* Deal with !@#$% 64K limit: */
if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
more = wsize;
} else if (more == (unsigned)(-1)) {
/* Very unlikely, but possible on 16 bit machine if strstart == 0
* and lookahead == 1 (input done one byte at time)
@@ -697,9 +794,6 @@ local void fill_window(s)
*/
} else if (s->strstart >= wsize+MAX_DIST(s)) {
/* By the IN assertion, the window is not empty so we can't confuse
* more == 0 with more == 64K on a 16 bit machine.
*/
zmemcpy((charf *)s->window, (charf *)s->window+wsize,
(unsigned)wsize);
s->match_start -= wsize;
@@ -768,7 +862,7 @@ local void fill_window(s)
* IN assertion: strstart is set to the end of the current match.
*/
#define FLUSH_BLOCK_ONLY(s, eof) { \
ct_flush_block(s, (s->block_start >= 0L ? \
tr_flush_block(s, (s->block_start >= 0L ? \
(charf *)&s->window[(unsigned)s->block_start] : \
(charf *)Z_NULL), (long)s->strstart - s->block_start, (eof)); \
s->block_start = s->strstart; \
@@ -782,6 +876,51 @@ local void fill_window(s)
if (s->strm->avail_out == 0) return 1; \
}
/* ===========================================================================
* Copy without compression as much as possible from the input stream, return
* true if processing was terminated prematurely (no more input or output
* space). This function does not insert new strings in the dictionary
* since uncompressible data is probably not useful. This function is used
* only for the level=0 compression option.
* NOTE: this function should be optimized to avoid extra copying.
*/
local int deflate_stored(s, flush)
deflate_state *s;
int flush;
{
for (;;) {
/* Fill the window as much as possible: */
if (s->lookahead <= 1) {
Assert(s->strstart < s->w_size+MAX_DIST(s) ||
s->block_start >= (long)s->w_size, "slide too late");
fill_window(s);
if (s->lookahead == 0 && flush == Z_NO_FLUSH) return 1;
if (s->lookahead == 0) break; /* flush the current block */
}
Assert(s->block_start >= 0L, "block gone");
s->strstart += s->lookahead;
s->lookahead = 0;
/* Stored blocks are limited to 0xffff bytes: */
if (s->strstart == 0 || s->strstart > 0xffff) {
/* strstart == 0 is possible when wraparound on 16-bit machine */
s->lookahead = s->strstart - 0xffff;
s->strstart = 0xffff;
}
/* Emit a stored block if it is large enough: */
if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
FLUSH_BLOCK(s, 0);
}
}
FLUSH_BLOCK(s, flush == Z_FINISH);
return 0; /* normal exit */
}
/* ===========================================================================
* Compress as much as possible from the input stream, return true if
* processing was terminated prematurely (no more input or output space).
@@ -793,11 +932,9 @@ local int deflate_fast(s, flush)
deflate_state *s;
int flush;
{
IPos hash_head; /* head of the hash chain */
IPos hash_head = NIL; /* head of the hash chain */
int bflush; /* set if current block must be flushed */
s->prev_length = MIN_MATCH-1;
for (;;) {
/* Make sure that we always have enough lookahead, except
* at the end of the input file. We need MAX_MATCH bytes
@@ -836,7 +973,7 @@ local int deflate_fast(s, flush)
if (s->match_length >= MIN_MATCH) {
check_match(s, s->strstart, s->match_start, s->match_length);
bflush = ct_tally(s, s->strstart - s->match_start,
bflush = tr_tally(s, s->strstart - s->match_start,
s->match_length - MIN_MATCH);
s->lookahead -= s->match_length;
@@ -870,7 +1007,7 @@ local int deflate_fast(s, flush)
} else {
/* No match, output a literal byte */
Tracevv((stderr,"%c", s->window[s->strstart]));
bflush = ct_tally (s, 0, s->window[s->strstart]);
bflush = tr_tally (s, 0, s->window[s->strstart]);
s->lookahead--;
s->strstart++;
}
@@ -889,7 +1026,7 @@ local int deflate_slow(s, flush)
deflate_state *s;
int flush;
{
IPos hash_head; /* head of hash chain */
IPos hash_head = NIL; /* head of hash chain */
int bflush; /* set if current block must be flushed */
/* Process the input block. */
@@ -949,7 +1086,7 @@ local int deflate_slow(s, flush)
check_match(s, s->strstart-1, s->prev_match, s->prev_length);
bflush = ct_tally(s, s->strstart -1 - s->prev_match,
bflush = tr_tally(s, s->strstart -1 - s->prev_match,
s->prev_length - MIN_MATCH);
/* Insert in hash table all strings up to the end of the match.
@@ -976,7 +1113,7 @@ local int deflate_slow(s, flush)
* is longer, truncate the previous match to a single literal.
*/
Tracevv((stderr,"%c", s->window[s->strstart-1]));
if (ct_tally (s, 0, s->window[s->strstart-1])) {
if (tr_tally (s, 0, s->window[s->strstart-1])) {
FLUSH_BLOCK_ONLY(s, 0);
}
s->strstart++;
@@ -994,7 +1131,7 @@ local int deflate_slow(s, flush)
Assert (flush != Z_NO_FLUSH, "no flush?");
if (s->match_available) {
Tracevv((stderr,"%c", s->window[s->strstart-1]));
ct_tally (s, 0, s->window[s->strstart-1]);
tr_tally (s, 0, s->window[s->strstart-1]);
s->match_available = 0;
}
FLUSH_BLOCK(s, flush == Z_FINISH);

View File

@@ -91,6 +91,7 @@ typedef struct internal_state {
int noheader; /* suppress zlib header and adler32 */
Byte data_type; /* UNKNOWN, BINARY or ASCII */
Byte method; /* STORED (for zip only) or DEFLATED */
int last_flush; /* value of flush param for previous deflate call */
/* used by deflate.c: */
@@ -233,6 +234,7 @@ typedef struct internal_state {
ulg static_len; /* bit length of current block with static trees */
ulg compressed_len; /* total bit length of compressed file */
uInt matches; /* number of string matches in current block */
int last_eob_len; /* bit length of EOB code for last block */
#ifdef DEBUG
ulg bits_sent; /* bit length of the compressed data */
@@ -266,9 +268,9 @@ typedef struct internal_state {
*/
/* in trees.c */
void ct_init OF((deflate_state *s));
int ct_tally OF((deflate_state *s, int dist, int lc));
ulg ct_flush_block OF((deflate_state *s, charf *buf, ulg stored_len, int eof));
void ct_align OF((deflate_state *s));
void ct_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
void tr_init OF((deflate_state *s));
int tr_tally OF((deflate_state *s, int dist, int lc));
ulg tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len, int eof));
void tr_align OF((deflate_state *s));
void tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
int eof));

212
example.c
View File

@@ -10,20 +10,11 @@
#ifdef STDC
# include <string.h>
#endif
#ifndef __GO32__
# include <stdlib.h>
#else
extern void exit OF((int));
#endif
#define BUFLEN 4096
#define local static
/* For MSDOS and other systems with limitation on stack size. For Unix,
#define local
works also.
*/
#define CHECK_ERR(err, msg) { \
if (err != Z_OK) { \
fprintf(stderr, "%s error: %d\n", msg, err); \
@@ -36,23 +27,29 @@ char *hello = "hello, hello!";
* stresses the compression code better, sorry...
*/
void test_compress OF((void));
void test_gzio OF((char *out, char *in));
void test_deflate OF((Byte compr[]));
void test_inflate OF((Byte compr[]));
void test_flush OF((Byte compr[]));
void test_sync OF((Byte compr[]));
void test_compress OF((Bytef *compr, uLong comprLen,
Bytef *uncompr, uLong uncomprLen));
void test_gzio OF((char *out, char *in,
Bytef *uncompr, int uncomprLen));
void test_deflate OF((Bytef *compr, uLong comprLen));
void test_inflate OF((Bytef *compr, uLong comprLen,
Bytef *uncompr, uLong uncomprLen));
void test_large_deflate OF((Bytef *compr, uLong comprLen,
Bytef *uncompr, uLong uncomprLen));
void test_large_inflate OF((Bytef *compr, uLong comprLen,
Bytef *uncompr, uLong uncomprLen));
void test_flush OF((Bytef *compr, uLong comprLen));
void test_sync OF((Bytef *compr, uLong comprLen,
Bytef *uncompr, uLong uncomprLen));
int main OF((int argc, char *argv[]));
/* ===========================================================================
* Test compress() and uncompress()
*/
void test_compress()
void test_compress(compr, comprLen, uncompr, uncomprLen)
Bytef *compr, *uncompr;
uLong comprLen, uncomprLen;
{
local Byte compr[BUFLEN];
uLong comprLen = sizeof(compr);
local Byte uncompr[BUFLEN];
uLong uncomprLen = sizeof(uncompr);
int err;
uLong len = strlen(hello)+1;
@@ -74,12 +71,12 @@ void test_compress()
/* ===========================================================================
* Test read/write of .gz files
*/
void test_gzio(out, in)
void test_gzio(out, in, uncompr, uncomprLen)
char *out; /* output file */
char *in; /* input file */
Bytef *uncompr;
int uncomprLen;
{
local Byte uncompr[BUFLEN];
int uncomprLen = sizeof(uncompr);
int err;
int len = strlen(hello)+1;
gzFile file;
@@ -117,8 +114,9 @@ void test_gzio(out, in)
/* ===========================================================================
* Test deflate() with small buffers
*/
void test_deflate(compr)
Byte compr[];
void test_deflate(compr, comprLen)
Bytef *compr;
uLong comprLen;
{
z_stream c_stream; /* compression stream */
int err;
@@ -126,14 +124,15 @@ void test_deflate(compr)
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.opaque = (voidpf)0;
err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
CHECK_ERR(err, "deflateInit");
c_stream.next_in = (Byte*)hello;
c_stream.next_in = (Bytef*)hello;
c_stream.next_out = compr;
while (c_stream.total_in != (uLong)len) {
while (c_stream.total_in != (uLong)len && c_stream.total_out < comprLen) {
c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
@@ -153,10 +152,10 @@ void test_deflate(compr)
/* ===========================================================================
* Test inflate() with small buffers
*/
void test_inflate(compr)
Byte compr[];
void test_inflate(compr, comprLen, uncompr, uncomprLen)
Bytef *compr, *uncompr;
uLong comprLen, uncomprLen;
{
local Byte uncompr[BUFLEN];
int err;
z_stream d_stream; /* decompression stream */
@@ -164,6 +163,7 @@ void test_inflate(compr)
d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.opaque = (voidpf)0;
err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit");
@@ -171,7 +171,7 @@ void test_inflate(compr)
d_stream.next_in = compr;
d_stream.next_out = uncompr;
for (;;) {
while (d_stream.total_out < uncomprLen) {
d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break;
@@ -188,11 +188,105 @@ void test_inflate(compr)
}
}
/* ===========================================================================
* Test deflate() with large buffers and dynamic change of compression level
*/
void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
Bytef *compr, *uncompr;
uLong comprLen, uncomprLen;
{
z_stream c_stream; /* compression stream */
int err;
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.opaque = (voidpf)0;
err = deflateInit(&c_stream, Z_BEST_SPEED);
CHECK_ERR(err, "deflateInit");
c_stream.next_out = compr;
c_stream.avail_out = comprLen;
/* At this point, uncompr is still mostly zeroes, so it should compress
* very well:
*/
c_stream.next_in = uncompr;
c_stream.avail_in = (uInt)uncomprLen;
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
if (c_stream.avail_in != 0) {
fprintf(stderr, "deflate not greedy\n");
}
/* Feed in already compressed data and switch to no compression: */
deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
c_stream.next_in = compr;
c_stream.avail_in = (uInt)comprLen/2;
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
/* Switch back to compressing mode: */
deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
c_stream.next_in = uncompr;
c_stream.avail_in = (uInt)uncomprLen;
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
err = deflate(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
fprintf(stderr, "deflate should report Z_STREAM_END\n");
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
/* ===========================================================================
* Test inflate() with large buffers
*/
void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
Bytef *compr, *uncompr;
uLong comprLen, uncomprLen;
{
int err;
z_stream d_stream; /* decompression stream */
strcpy((char*)uncompr, "garbage");
d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.opaque = (voidpf)0;
err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit");
d_stream.next_in = compr;
d_stream.avail_in = (uInt)comprLen;
for (;;) {
d_stream.next_out = uncompr; /* discard the output */
d_stream.avail_out = uncomprLen;
err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "large inflate");
}
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
} else {
printf("large_inflate(): OK\n");
}
}
/* ===========================================================================
* Test deflate() with full flush
*/
void test_flush(compr)
Byte compr[];
void test_flush(compr, comprLen)
Bytef *compr;
uLong comprLen;
{
z_stream c_stream; /* compression stream */
int err;
@@ -200,14 +294,15 @@ void test_flush(compr)
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.opaque = (voidpf)0;
err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
CHECK_ERR(err, "deflateInit");
c_stream.next_in = (Byte*)hello;
c_stream.next_in = (Bytef*)hello;
c_stream.next_out = compr;
c_stream.avail_in = 3;
c_stream.avail_out = BUFLEN;
c_stream.avail_out = (uInt)comprLen;
err = deflate(&c_stream, Z_FULL_FLUSH);
CHECK_ERR(err, "deflate");
@@ -225,10 +320,10 @@ void test_flush(compr)
/* ===========================================================================
* Test inflateSync()
*/
void test_sync(compr)
Byte compr[];
void test_sync(compr, comprLen, uncompr, uncomprLen)
Bytef *compr, *uncompr;
uLong comprLen, uncomprLen;
{
local Byte uncompr[BUFLEN];
int err;
z_stream d_stream; /* decompression stream */
@@ -236,6 +331,7 @@ void test_sync(compr)
d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.opaque = (voidpf)0;
err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit");
@@ -243,13 +339,13 @@ void test_sync(compr)
d_stream.next_in = compr;
d_stream.next_out = uncompr;
d_stream.avail_in = 2; /* just read the zlib header */
d_stream.avail_out = sizeof(uncompr);
d_stream.avail_out = (uInt)uncomprLen;
inflate(&d_stream, Z_NO_FLUSH);
CHECK_ERR(err, "inflate");
d_stream.avail_in = BUFLEN-2; /* let inflate read all compressed data */
err = inflateSync(&d_stream); /* skip the damaged part */
d_stream.avail_in = (uInt)uncomprLen-2; /* read all compressed data */
err = inflateSync(&d_stream); /* but skip the damaged part */
CHECK_ERR(err, "inflateSync");
err = inflate(&d_stream, Z_FINISH);
@@ -271,25 +367,39 @@ int main(argc, argv)
int argc;
char *argv[];
{
local Byte compr[BUFLEN];
Bytef *compr, *uncompr;
uLong comprLen = 32750*sizeof(int); /* don't overflow on MSDOS */
uLong uncomprLen = comprLen;
if (zlib_version[0] != ZLIB_VERSION[0]) {
fprintf(stderr, "incompatible zlib version\n");
exit(1);
} else if (zstrcmp(zlib_version, ZLIB_VERSION) != 0) {
} else if (strcmp(zlib_version, ZLIB_VERSION) != 0) {
fprintf(stderr, "warning: different zlib version\n");
}
test_compress();
compr = (Bytef*)malloc(comprLen);
uncompr = (Bytef*)calloc(uncomprLen, 1); /* must be cleared initially */
if (compr == Z_NULL || uncompr == Z_NULL) {
printf("out of memory\n");
exit(1);
}
test_compress(compr, comprLen, uncompr, uncomprLen);
test_gzio((argc > 1 ? argv[1] : "foo.gz"),
(argc > 2 ? argv[2] : "foo.gz"));
(argc > 2 ? argv[2] : "foo.gz"),
uncompr, (int)uncomprLen);
test_deflate(compr);
test_inflate(compr);
test_deflate(compr, comprLen);
test_inflate(compr, comprLen, uncompr, uncomprLen);
test_flush(compr);
test_sync(compr);
test_large_deflate(compr, comprLen, uncompr, uncomprLen);
test_large_inflate(compr, comprLen, uncompr, uncomprLen);
test_flush(compr, comprLen);
test_sync(compr, comprLen, uncompr, uncomprLen);
exit(0);
return 0; /* to avoid warning */

15
gzio.c
View File

@@ -83,7 +83,7 @@ local int destroy (s)
/* ===========================================================================
Opens a gzip (.gz) file for reading or writing. The mode parameter
is as in fopen ("rb" or "wb"). The file is given either by file descritor
is as in fopen ("rb" or "wb"). The file is given either by file descriptor
or path name (if fd == -1).
gz_open return NULL if the file could not be opened or if there was
insufficient memory to allocate the (de)compression state; errno
@@ -104,6 +104,7 @@ local gzFile gz_open (path, mode, fd)
s->stream.zalloc = (alloc_func)0;
s->stream.zfree = (free_func)0;
s->stream.opaque = (voidpf)0;
s->stream.next_in = s->inbuf = Z_NULL;
s->stream.next_out = s->outbuf = Z_NULL;
s->stream.avail_in = s->stream.avail_out = 0;
@@ -130,7 +131,7 @@ local gzFile gz_open (path, mode, fd)
if (s->mode == 'w') {
err = deflateInit2(&(s->stream), level,
DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0);
Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0);
/* windowBits is passed < 0 to suppress zlib header */
s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
@@ -149,7 +150,7 @@ local gzFile gz_open (path, mode, fd)
s->stream.avail_out = Z_BUFSIZE;
errno = 0;
s->file = fd < 0 ? FOPEN(path, mode) : fdopen(fd, mode);
s->file = fd < 0 ? FOPEN(path, mode) : (FILE*)fdopen(fd, mode);
if (s->file == NULL) {
return destroy(s), (gzFile)Z_NULL;
@@ -158,7 +159,7 @@ local gzFile gz_open (path, mode, fd)
/* Write a very simple .gz header:
*/
fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", GZ_MAGIC_1, GZ_MAGIC_2,
DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
} else {
/* Check and skip the header:
*/
@@ -179,7 +180,7 @@ local gzFile gz_open (path, mode, fd)
s->stream.avail_in = 0;
fscanf(s->file,"%c%c%4c%c%c", &method, &flags, time, &xflags, &osCode);
if (method != DEFLATED || feof(s->file) || (flags & RESERVED) != 0) {
if (method != Z_DEFLATED || feof(s->file) || (flags & RESERVED) != 0) {
s->z_err = Z_DATA_ERROR;
return (gzFile)s;
}
@@ -234,7 +235,7 @@ gzFile gzdopen (fd, mode)
*/
int gzread (file, buf, len)
gzFile file;
voidnp buf;
voidp buf;
unsigned len;
{
gz_stream *s = (gz_stream*)file;
@@ -292,7 +293,7 @@ int gzread (file, buf, len)
*/
int gzwrite (file, buf, len)
gzFile file;
voidnp buf;
voidp buf;
unsigned len;
{
gz_stream *s = (gz_stream*)file;

View File

@@ -179,7 +179,7 @@ int r;
break;
case LENS:
NEEDBITS(32)
if (((~b) >> 16) != (b & 0xffff))
if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
{
s->mode = BAD;
z->msg = "invalid stored block lengths";

View File

@@ -202,7 +202,7 @@ int r;
#else
f = q - c->sub.copy.dist;
if ((uInt)(q - s->window) < c->sub.copy.dist)
f = s->end - (c->sub.copy.dist - (q - s->window));
f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
#endif
while (c->len)
{

View File

@@ -102,7 +102,7 @@ z_stream *z;
}
else /* else offset after destination */
{
e = d - (q - s->window); /* bytes from offset to end */
e = d - (uInt)(q - s->window); /* bytes from offset to end */
r = s->end - e; /* pointer to offset */
if (c > e) /* if source crosses, */
{

View File

@@ -82,7 +82,12 @@ int w;
/* initialize state */
if (z == Z_NULL)
return Z_STREAM_ERROR;
if (z->zalloc == Z_NULL) z->zalloc = zcalloc;
z->msg = Z_NULL;
if (z->zalloc == Z_NULL)
{
z->zalloc = zcalloc;
z->opaque = (voidpf)0;
}
if (z->zfree == Z_NULL) z->zfree = zcfree;
if ((z->state = (struct internal_state FAR *)
ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
@@ -145,7 +150,7 @@ int f;
{
case METHOD:
NEEDBYTE
if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED)
if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
{
z->state->mode = BAD;
z->msg = "unknown compression method";

View File

@@ -6,6 +6,13 @@
#include "zutil.h"
#include "inftrees.h"
char inflate_copyright[] = " inflate 1.0 Copyright 1995 Mark Adler ";
/*
If you use the zlib library in a product, an acknowledgment is welcome
in the documentation of your product. If for some reason you cannot
include such an acknowledgment, I would appreciate that you keep this
copyright string in the executable of your product.
*/
struct internal_state {int dummy;}; /* for buggy compilers */
/* simplify the use of the inflate_huft type with some defines */
@@ -25,15 +32,11 @@ local int huft_build OF((
uIntf *, /* maximum lookup bits (returns actual) */
z_stream *)); /* for zalloc function */
local voidp falloc OF((
voidp, /* opaque pointer (not used) */
local voidpf falloc OF((
voidpf, /* opaque pointer (not used) */
uInt, /* number of items */
uInt)); /* size of item */
local void ffree OF((
voidp q, /* opaque pointer (not used) */
voidp p)); /* what to free (not used) */
/* Tables for deflate from PKZIP's appnote.txt. */
local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
@@ -366,10 +369,8 @@ z_stream *z; /* for zfree function */
/* build fixed tables only once--keep them here */
local int fixed_lock = 0;
local int fixed_built = 0;
#define FIXEDH 530 /* number of hufts used by fixed tables */
local uInt fixed_left = FIXEDH;
local inflate_huft fixed_mem[FIXEDH];
local uInt fixed_bl;
local uInt fixed_bd;
@@ -377,25 +378,15 @@ local inflate_huft *fixed_tl;
local inflate_huft *fixed_td;
local voidp falloc(q, n, s)
voidp q; /* opaque pointer (not used) */
local voidpf falloc(q, n, s)
voidpf q; /* opaque pointer */
uInt n; /* number of items */
uInt s; /* size of item */
{
Assert(s == sizeof(inflate_huft) && n <= fixed_left,
Assert(s == sizeof(inflate_huft) && n <= *(intf *)q,
"inflate_trees falloc overflow");
if (q) s++; /* to make some compilers happy */
fixed_left -= n;
return (voidp)(fixed_mem + fixed_left);
}
local void ffree(q, p)
voidp q;
voidp p;
{
Assert(0, "inflate_trees ffree called!");
if (q) q = p; /* to make some compilers happy */
*(intf *)q -= n;
return (voidpf)(fixed_mem + *(intf *)q);
}
@@ -405,19 +396,18 @@ uIntf *bd; /* distance desired/actual bit depth */
inflate_huft * FAR *tl; /* literal/length tree result */
inflate_huft * FAR *td; /* distance tree result */
{
/* build fixed tables if not built already--lock out other instances */
while (++fixed_lock > 1)
fixed_lock--;
/* build fixed tables if not already (multiple overlapped executions ok) */
if (!fixed_built)
{
int k; /* temporary variable */
unsigned c[288]; /* length list for huft_build */
z_stream z; /* for falloc function */
int f = FIXEDH; /* number of hufts left in fixed_mem */
/* set up fake z_stream for memory routines */
z.zalloc = falloc;
z.zfree = ffree;
z.opaque = Z_NULL;
z.zfree = Z_NULL;
z.opaque = (voidpf)&f;
/* literal table */
for (k = 0; k < 144; k++)
@@ -438,9 +428,9 @@ inflate_huft * FAR *td; /* distance tree result */
huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z);
/* done */
Assert(f == 0, "invalid build of fixed tables");
fixed_built = 1;
}
fixed_lock--;
*bl = fixed_bl;
*bd = fixed_bd;
*tl = fixed_tl;
@@ -456,10 +446,19 @@ z_stream *z; /* for zfree function */
list of the tables it made, with the links in a dummy first entry of
each table. */
{
register inflate_huft *p, *q;
register inflate_huft *p, *q, *r;
/* Reverse linked list */
p = Z_NULL;
q = t;
while (q != Z_NULL)
{
r = (q - 1)->next;
(q - 1)->next = p;
p = q;
q = r;
}
/* Go through linked list, freeing from the malloced (t[-1]) address. */
p = t;
while (p != Z_NULL)
{
q = (--p)->next;

View File

@@ -70,7 +70,7 @@ struct inflate_blocks_state {
#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
#define DUMPBITS(j) {b>>=(j);k-=(j);}
/* output bytes */
#define WAVAIL (q<s->read?s->read-q-1:s->end-q)
#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
#define LOADOUT {q=s->write;m=WAVAIL;}
#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=WAVAIL;}}
#define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}

View File

@@ -18,15 +18,14 @@
#include <stdio.h>
#include "zlib.h"
#ifndef __GO32__
#ifdef STDC
# include <string.h>
# include <stdlib.h>
#else
extern void exit OF((int));
#endif
extern int unlink OF((const char *));
#ifdef STDC
# include <string.h>
#endif
#if defined(MSDOS) || defined(OS2) || defined(WIN32)
# include <fcntl.h>
# include <io.h>

196
trees.c
View File

@@ -78,13 +78,12 @@ local uch bl_order[BL_CODES]
/* ===========================================================================
* Local data. These are initialized only once.
* To do: initialize at compile time to be completely reentrant. ???
*/
local ct_data static_ltree[L_CODES+2];
/* The static literal tree. Since the bit lengths are imposed, there is no
* need for the L_CODES extra codes used during heap construction. However
* The codes 286 and 287 are needed to build a canonical tree (see ct_init
* The codes 286 and 287 are needed to build a canonical tree (see tr_init
* below).
*/
@@ -129,7 +128,7 @@ local static_tree_desc static_bl_desc =
* Local (static) routines in this file.
*/
local void ct_static_init OF((void));
local void tr_static_init OF((void));
local void init_block OF((deflate_state *s));
local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
@@ -143,9 +142,9 @@ local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
local void compress_block OF((deflate_state *s, ct_data *ltree,
ct_data *dtree));
local void set_data_type OF((deflate_state *s));
local void send_bits OF((deflate_state *s, int value, int length));
local unsigned bi_reverse OF((unsigned value, int length));
local void bi_windup OF((deflate_state *s));
local void bi_flush OF((deflate_state *s));
local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
int header));
@@ -166,15 +165,74 @@ local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
* used.
*/
/* ===========================================================================
* Output a short LSB first on the stream.
* IN assertion: there is enough room in pendingBuf.
*/
#define put_short(s, w) { \
put_byte(s, (uch)((w) & 0xff)); \
put_byte(s, (uch)((ush)(w) >> 8)); \
}
/* ===========================================================================
* Send a value on a given number of bits.
* IN assertion: length <= 16 and value fits in length bits.
*/
#ifdef DEBUG
local void send_bits OF((deflate_state *s, int value, int length));
local void send_bits(s, value, length)
deflate_state *s;
int value; /* value to send */
int length; /* number of bits */
{
Tracevv((stderr," l %2d v %4x ", length, value));
Assert(length > 0 && length <= 15, "invalid length");
s->bits_sent += (ulg)length;
/* If not enough room in bi_buf, use (valid) bits from bi_buf and
* (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
* unused bits in value.
*/
if (s->bi_valid > (int)Buf_size - length) {
s->bi_buf |= (value << s->bi_valid);
put_short(s, s->bi_buf);
s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
s->bi_valid += length - Buf_size;
} else {
s->bi_buf |= value << s->bi_valid;
s->bi_valid += length;
}
}
#else /* !DEBUG */
#define send_bits(s, value, length) \
{ int len = length;\
if (s->bi_valid > (int)Buf_size - len) {\
int val = value;\
s->bi_buf |= (val << s->bi_valid);\
put_short(s, s->bi_buf);\
s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
s->bi_valid += len - Buf_size;\
} else {\
s->bi_buf |= (value) << s->bi_valid;\
s->bi_valid += len;\
}\
}
#endif /* DEBUG */
#define MAX(a,b) (a >= b ? a : b)
/* the arguments must not have side effects */
/* ===========================================================================
* Initialize the various 'constant' tables.
* To do: do this at compile time.
* Initialize the various 'constant' tables. In a multi-threaded environment,
* this function may be called by two threads concurrently, but this is
* harmless since both invocations do exactly the same thing.
*/
local void ct_static_init()
local void tr_static_init()
{
static static_init_done = 0;
int n; /* iterates over tree elements */
int bits; /* bit counter */
int length; /* length value */
@@ -183,6 +241,8 @@ local void ct_static_init()
ush bl_count[MAX_BITS+1];
/* number of codes at each bit length for an optimal tree */
if (static_init_done) return;
/* Initialize the mapping length (0..255) -> length code (0..28) */
length = 0;
for (code = 0; code < LENGTH_CODES-1; code++) {
@@ -191,7 +251,7 @@ local void ct_static_init()
length_code[length++] = (uch)code;
}
}
Assert (length == 256, "ct_static_init: length != 256");
Assert (length == 256, "tr_static_init: length != 256");
/* Note that the length 255 (match length 258) can be represented
* in two different ways: code 284 + 5 bits or code 285, so we
* overwrite length_code[255] to use the best encoding:
@@ -206,7 +266,7 @@ local void ct_static_init()
dist_code[dist++] = (uch)code;
}
}
Assert (dist == 256, "ct_static_init: dist != 256");
Assert (dist == 256, "tr_static_init: dist != 256");
dist >>= 7; /* from now on, all distances are divided by 128 */
for ( ; code < D_CODES; code++) {
base_dist[code] = dist << 7;
@@ -214,7 +274,7 @@ local void ct_static_init()
dist_code[256 + dist++] = (uch)code;
}
}
Assert (dist == 256, "ct_static_init: 256+dist != 512");
Assert (dist == 256, "tr_static_init: 256+dist != 512");
/* Construct the codes of the static literal tree */
for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
@@ -234,17 +294,16 @@ local void ct_static_init()
static_dtree[n].Len = 5;
static_dtree[n].Code = bi_reverse(n, 5);
}
static_init_done = 1;
}
/* ===========================================================================
* Initialize the tree data structures for a new zlib stream.
*/
void ct_init(s)
void tr_init(s)
deflate_state *s;
{
if (static_dtree[0].Len == 0) {
ct_static_init(); /* To do: at compile time */
}
tr_static_init();
s->compressed_len = 0L;
@@ -259,6 +318,7 @@ void ct_init(s)
s->bi_buf = 0;
s->bi_valid = 0;
s->last_eob_len = 8; /* enough lookahead for inflate */
#ifdef DEBUG
s->bits_sent = 0L;
#endif
@@ -465,7 +525,7 @@ local void gen_codes (tree, max_code, bl_count)
/* Now reverse the bits */
tree[n].Code = bi_reverse(next_code[len]++, len);
Tracec(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
}
}
@@ -725,7 +785,7 @@ local void send_all_trees(s, lcodes, dcodes, blcodes)
/* ===========================================================================
* Send a stored block
*/
void ct_stored_block(s, buf, stored_len, eof)
void tr_stored_block(s, buf, stored_len, eof)
deflate_state *s;
charf *buf; /* input block */
ulg stored_len; /* length of input block */
@@ -739,14 +799,35 @@ void ct_stored_block(s, buf, stored_len, eof)
}
/* ===========================================================================
* Send one empty static block to give enough lookahead for inflate
* Send one empty static block to give enough lookahead for inflate.
* This takes 10 bits, of which 7 may remain in the bit buffer.
* The current inflate code requires 9 bits of lookahead. If the
* last two codes for the previous block (real code plus EOB) were coded
* on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
* the last real code. In this case we send two empty static blocks instead
* of one. (There are no problems if the previous block is stored or fixed.)
* To simplify the code, we assume the worst case of last real code encoded
* on one bit only.
*/
void ct_align(s)
void tr_align(s)
deflate_state *s;
{
send_bits(s, STATIC_TREES<<1, 3);
send_code(s, END_BLOCK, static_ltree);
s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
bi_flush(s);
/* Of the 10 bits for the empty block, we have already sent
* (10 - bi_valid) bits. The lookahead for the last real code (before
* the EOB of the previous block) was thus at least one plus the length
* of the EOB plus what we have just sent of the empty static block.
*/
if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
send_bits(s, STATIC_TREES<<1, 3);
send_code(s, END_BLOCK, static_ltree);
s->compressed_len += 10L;
bi_flush(s);
}
s->last_eob_len = 7;
}
/* ===========================================================================
@@ -754,7 +835,7 @@ void ct_align(s)
* trees or store, and output the encoded block to the zip file. This function
* returns the total compressed length for the file so far.
*/
ulg ct_flush_block(s, buf, stored_len, eof)
ulg tr_flush_block(s, buf, stored_len, eof)
deflate_state *s;
charf *buf; /* input block, or NULL if too old */
ulg stored_len; /* length of input block */
@@ -763,6 +844,9 @@ ulg ct_flush_block(s, buf, stored_len, eof)
ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
int max_blindex; /* index of last bit length code of non zero freq */
/* Build the Huffman trees unless a stored block is forced */
if (s->level > 0) {
/* Check if the file is ascii or binary */
if (s->data_type == UNKNOWN) set_data_type(s);
@@ -793,13 +877,18 @@ ulg ct_flush_block(s, buf, stored_len, eof)
if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
} else {
Assert(buf != (char*)0, "lost buf");
opt_lenb = stored_len + 5; /* force a stored block */
}
/* If compression failed and this is the first and last block,
* and if the .zip file can be seeked (to rewrite the local header),
* the whole file is transformed into a stored file:
*/
#ifdef STORED_FILE_OK
# ifdef FORCE_STORED_FILE
if (eof && compressed_len == 0L) { /* force stored file */
if (eof && s->compressed_len == 0L) { /* force stored file */
# else
if (stored_len <= opt_lenb && eof && s->compressed_len==0L && seekable()) {
# endif
@@ -824,7 +913,7 @@ ulg ct_flush_block(s, buf, stored_len, eof)
* successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
* transform a block into a stored block.
*/
ct_stored_block(s, buf, stored_len, eof);
tr_stored_block(s, buf, stored_len, eof);
#ifdef FORCE_STATIC
} else if (static_lenb >= 0) { /* force static trees */
@@ -858,7 +947,7 @@ ulg ct_flush_block(s, buf, stored_len, eof)
* Save the match info and tally the frequency counts. Return true if
* the current block must be flushed.
*/
int ct_tally (s, dist, lc)
int tr_tally (s, dist, lc)
deflate_state *s;
int dist; /* distance of matched string */
int lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
@@ -874,7 +963,7 @@ int ct_tally (s, dist, lc)
dist--; /* dist = match distance - 1 */
Assert((ush)dist < (ush)MAX_DIST(s) &&
(ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
(ush)d_code(dist) < (ush)D_CODES, "ct_tally: bad match");
(ush)d_code(dist) < (ush)D_CODES, "tr_tally: bad match");
s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++;
s->dyn_dtree[d_code(dist)].Freq++;
@@ -884,7 +973,7 @@ int ct_tally (s, dist, lc)
if (s->level > 2 && (s->last_lit & 0xfff) == 0) {
/* Compute an upper bound for the compressed length */
ulg out_length = (ulg)s->last_lit*8L;
ulg in_length = (ulg)s->strstart - s->block_start;
ulg in_length = (ulg)((long)s->strstart - s->block_start);
int dcode;
for (dcode = 0; dcode < D_CODES; dcode++) {
out_length += (ulg)s->dyn_dtree[dcode].Freq *
@@ -950,6 +1039,7 @@ local void compress_block(s, ltree, dtree)
} while (lx < s->last_lit);
send_code(s, END_BLOCK, ltree);
s->last_eob_len = ltree[END_BLOCK].Len;
}
/* ===========================================================================
@@ -970,44 +1060,6 @@ local void set_data_type(s)
s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? BINARY : ASCII);
}
/* ===========================================================================
* Output a short LSB first on the stream.
* IN assertion: there is enough room in pendingBuf.
*/
#define put_short(s, w) { \
put_byte(s, (uch)((w) & 0xff)); \
put_byte(s, (uch)((ush)(w) >> 8)); \
}
/* ===========================================================================
* Send a value on a given number of bits.
* IN assertion: length <= 16 and value fits in length bits.
*/
local void send_bits(s, value, length)
deflate_state *s;
int value; /* value to send */
int length; /* number of bits */
{
#ifdef DEBUG
Tracev((stderr," l %2d v %4x ", length, value));
Assert(length > 0 && length <= 15, "invalid length");
s->bits_sent += (ulg)length;
#endif
/* If not enough room in bi_buf, use (valid) bits from bi_buf and
* (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
* unused bits in value.
*/
if (s->bi_valid > (int)Buf_size - length) {
s->bi_buf |= (value << s->bi_valid);
put_short(s, s->bi_buf);
s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
s->bi_valid += length - Buf_size;
} else {
s->bi_buf |= value << s->bi_valid;
s->bi_valid += length;
}
}
/* ===========================================================================
* Reverse the first len bits of a code, using straightforward code (a faster
* method would use a table)
@@ -1026,7 +1078,24 @@ local unsigned bi_reverse(code, len)
}
/* ===========================================================================
* Write out any remaining bits in an incomplete byte.
* Flush the bit buffer, keeping at most 7 bits in it.
*/
local void bi_flush(s)
deflate_state *s;
{
if (s->bi_valid == 16) {
put_short(s, s->bi_buf);
s->bi_buf = 0;
s->bi_valid = 0;
} else if (s->bi_valid >= 8) {
put_byte(s, (Byte)s->bi_buf);
s->bi_buf >>= 8;
s->bi_valid -= 8;
}
}
/* ===========================================================================
* Flush the bit buffer and align the output on a byte boundary
*/
local void bi_windup(s)
deflate_state *s;
@@ -1054,6 +1123,7 @@ local void copy_block(s, buf, len, header)
int header; /* true if block header must be written */
{
bi_windup(s); /* align on byte boundary */
s->last_eob_len = 8; /* enough lookahead for inflate */
if (header) {
put_short(s, (ush)len);

56
zconf.h
View File

@@ -15,16 +15,20 @@
for some forms of corrupted input.
*/
/*
* Compile with -DMAXSEG_64K if the alloc function cannot allocate more
* than 64k bytes at a time (needed on systems with 16-bit int).
*/
#if defined(_GNUC__) && !defined(__32BIT__)
#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
# define WIN32
#endif
#if (defined(__GNUC__) || defined(WIN32)) && !defined(__32BIT__)
# define __32BIT__
#endif
#if defined(__MSDOS__) && !defined(MSDOS)
# define MSDOS
#endif
/*
* Compile with -DMAXSEG_64K if the alloc function cannot allocate more
* than 64k bytes at a time (needed on systems with 16-bit int).
*/
#if defined(MSDOS) && !defined(__32BIT__)
# define MAXSEG_64K
#endif
@@ -32,17 +36,16 @@
# define UNALIGNED_OK
#endif
#ifndef STDC
# if defined(MSDOS) || defined(__STDC__) || defined(__cplusplus)
#if (defined(MSDOS) || defined(_WINDOWS) || defined(WIN32)) && !defined(STDC)
# define STDC
#endif
#if (defined(__STDC__) || defined(__cplusplus)) && !defined(STDC)
# define STDC
#endif
#ifndef STDC
# ifndef const
#if !defined(STDC) && !defined(const)
# define const
#endif
#endif
#ifdef __MWERKS__ /* Metrowerks CodeWarrior declares fileno() in unix.h */
# include <unix.h>
@@ -85,17 +88,23 @@
# endif
#endif
#if defined(M_I86SM) || defined(M_I86MM) /* model independent MSC functions */
# define zstrcpy _fstrcpy
# define zstrcat _fstrcat
# define zstrlen _fstrlen
# define zstrcmp _fstrcmp
/* The following definitions for FAR are needed only for MSDOS mixed
* model programming (small or medium model with some far allocations).
* This was tested only with MSC; for other MSDOS compilers you may have
* to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
* just define FAR to be empty.
*/
#if defined(M_I86SM) || defined(M_I86MM) /* MSC small or medium model */
# ifdef _MSC_VER
# define FAR __far
# else
# define zstrcpy strcpy
# define zstrcat strcat
# define zstrlen strlen
# define zstrcmp strcmp
# define FAR far
# endif
#endif
#if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__))
# define FAR _far /* completely untested, just a best guess */
#endif
#ifndef FAR
# define FAR
#endif
@@ -110,12 +119,11 @@ typedef uInt FAR uIntf;
typedef uLong FAR uLongf;
#ifdef STDC
typedef void FAR *voidp;
typedef void *voidnp;
typedef void FAR *voidpf;
typedef void *voidp;
#else
typedef Byte FAR *voidp;
typedef Byte *voidnp;
typedef Byte FAR *voidpf;
typedef Byte *voidp;
#endif
#endif /* _ZCONF_H */

82
zlib.h
View File

@@ -1,7 +1,7 @@
/* zlib.h -- interface of the 'zlib' general purpose compression library
version 0.94, Aug 13th, 1995.
version 1.0, Jan 14th, 1996.
Copyright (C) 1995 Jean-loup Gailly and Mark Adler
Copyright (C) 1995-1996 Jean-loup Gailly and Mark Adler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@@ -26,9 +26,13 @@
#ifndef _ZLIB_H
#define _ZLIB_H
#ifdef __cplusplus
extern "C" {
#endif
#include "zconf.h"
#define ZLIB_VERSION "0.94"
#define ZLIB_VERSION "1.0"
/*
The 'zlib' compression library provides in-memory compression and
@@ -49,8 +53,8 @@
(providing more output space) before each call.
*/
typedef voidp (*alloc_func) OF((voidp opaque, uInt items, uInt size));
typedef void (*free_func) OF((voidp opaque, voidp address));
typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
typedef void (*free_func) OF((voidpf opaque, voidpf address));
struct internal_state;
@@ -68,7 +72,7 @@ typedef struct z_stream_s {
alloc_func zalloc; /* used to allocate the internal state */
free_func zfree; /* used to free the internal state */
voidp opaque; /* private data object passed to zalloc and zfree */
voidpf opaque; /* private data object passed to zalloc and zfree */
Byte data_type; /* best guess about the data type: ascii or binary */
@@ -107,10 +111,10 @@ typedef struct z_stream_s {
#define Z_NO_FLUSH 0
#define Z_PARTIAL_FLUSH 1
#define Z_FULL_FLUSH 2
#define Z_SYNC_FLUSH 3 /* experimental: partial_flush + byte align */
#define Z_SYNC_FLUSH 2
#define Z_FULL_FLUSH 3
#define Z_FINISH 4
/* See deflate() below for the usage of these constants */
/* Allowed flush values; see deflate() below for details */
#define Z_OK 0
#define Z_STREAM_END 1
@@ -121,6 +125,7 @@ typedef struct z_stream_s {
#define Z_BUF_ERROR (-5)
/* error codes for the compression/decompression functions */
#define Z_NO_COMPRESSION 0
#define Z_BEST_SPEED 1
#define Z_BEST_COMPRESSION 9
#define Z_DEFAULT_COMPRESSION (-1)
@@ -135,6 +140,9 @@ typedef struct z_stream_s {
#define Z_UNKNOWN 2
/* Used to set the data_type field */
#define Z_DEFLATED 8
/* The deflate compression method (the only one supported in this version) */
#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
extern char *zlib_version;
@@ -152,10 +160,11 @@ extern int deflateInit OF((z_stream *strm, int level));
If zalloc and zfree are set to Z_NULL, deflateInit updates them to
use default allocation functions.
The compression level must be Z_DEFAULT_COMPRESSION, or between 1 and 9:
1 gives best speed, 9 gives best compression. Z_DEFAULT_COMPRESSION requests
a default compromise between speed and compression (currently equivalent
to level 6).
The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
1 gives best speed, 9 gives best compression, 0 gives no compression at
all (the input data is simply copied a block at a time).
Z_DEFAULT_COMPRESSION requests a default compromise between speed and
compression (currently equivalent to level 6).
deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
enough memory, Z_STREAM_ERROR if level is not a valid compression level.
@@ -190,6 +199,11 @@ extern int deflate OF((z_stream *strm, int flush));
block is terminated and flushed to the output buffer so that the
decompressor can get all input data available so far. For method 9, a future
variant on method 8, the current block will be flushed but not terminated.
Z_SYNC_FLUSH has the same effect as partial flush except that the compressed
output is byte aligned (the compressor can clear its internal bit buffer)
and the current block is always terminated; this can be useful if the
compressor has to be restarted from scratch after an interruption (in which
case the internal state of the compressor may be lost).
If flush is set to Z_FULL_FLUSH, the compression block is terminated, a
special marker is output and the compression dictionary is discarded; this
is useful to allow the decompressor to synchronize if one compressed block
@@ -241,9 +255,9 @@ extern int deflateEnd OF((z_stream *strm));
extern int inflateInit OF((z_stream *strm));
/*
Initializes the internal stream state for decompression. The fields
zalloc and zfree must be initialized before by the caller. If zalloc and
zfree are set to Z_NULL, inflateInit updates them to use default allocation
functions.
zalloc, zfree and opaque must be initialized before by the caller. If
zalloc and zfree are set to Z_NULL, inflateInit updates them to use default
allocation functions.
inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
enough memory. msg is set to null if there is no error message.
@@ -326,10 +340,11 @@ extern int deflateInit2 OF((z_stream *strm,
int strategy));
/*
This is another version of deflateInit with more compression options. The
fields next_in, zalloc and zfree must be initialized before by the caller.
fields next_in, zalloc, zfree and opaque must be initialized before by
the caller.
The method parameter is the compression method. It must be 8 in this
version of the library. (Method 9 will allow a 64K history buffer and
The method parameter is the compression method. It must be Z_DEFLATED in
this version of the library. (Method 9 will allow a 64K history buffer and
partial block flushes.)
The windowBits parameter is the base two logarithm of the window size
@@ -407,11 +422,25 @@ extern int deflateReset OF((z_stream *strm));
stream state was inconsistent (such as zalloc or state being NULL).
*/
extern int deflateParams OF((z_stream *strm, int level, int strategy));
/*
Dynamically update the compression level and compression strategy.
This can be used to switch between compression and straight copy of
the input data, or to switch to a different kind of input data requiring
a different strategy. If the compression level is changed, the input
available so far is compressed with the old level (and may be flushed);
the new level will take effect only at the next call of deflate().
deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
stream state was inconsistent or if a parameter was invalid.
*/
extern int inflateInit2 OF((z_stream *strm,
int windowBits));
/*
This is another version of inflateInit with more compression options. The
fields next_out, zalloc and zfree must be initialized before by the caller.
fields next_out, zalloc, zfree and opaque must be initialized before by
the caller.
The windowBits parameter is the base two logarithm of the maximum window
size (the size of the history buffer). It should be in the range 8..15 for
@@ -510,7 +539,7 @@ extern int uncompress OF((Bytef *dest, uLongf *destLen,
*/
typedef voidnp gzFile;
typedef voidp gzFile;
extern gzFile gzopen OF((char *path, char *mode));
/*
@@ -527,13 +556,14 @@ extern gzFile gzopen OF((char *path, char *mode));
extern gzFile gzdopen OF((int fd, char *mode));
/*
gzdopen() associates a gzFile with the file descriptor fd. File
descriptors are obtained from calls like open, dup, creat, or pipe.
descriptors are obtained from calls like open, dup, creat, pipe or
fileno (in the file has been previously opened with fopen).
The mode parameter is as in fopen ("rb" or "wb").
gzdopen returns NULL if there was insufficient memory to allocate
the (de)compression state.
*/
extern int gzread OF((gzFile file, voidnp buf, unsigned len));
extern int gzread OF((gzFile file, voidp buf, unsigned len));
/*
Reads the given number of uncompressed bytes from the compressed file.
If the input file was not in gzip format, gzread copies the given number
@@ -541,7 +571,7 @@ extern int gzread OF((gzFile file, voidnp buf, unsigned len));
gzread returns the number of uncompressed bytes actually read (0 for
end of file, -1 for error). */
extern int gzwrite OF((gzFile file, voidnp buf, unsigned len));
extern int gzwrite OF((gzFile file, voidp buf, unsigned len));
/*
Writes the given number of uncompressed bytes into the compressed file.
gzwrite returns the number of uncompressed bytes actually written
@@ -619,4 +649,8 @@ extern uLong crc32 OF((uLong crc, Bytef *buf, uInt len));
struct internal_state {int dummy;}; /* hack for buggy compilers */
#endif
#ifdef __cplusplus
}
#endif
#endif /* _ZLIB_H */

59
zutil.c
View File

@@ -11,7 +11,7 @@
struct internal_state {int dummy;}; /* for buggy compilers */
#ifndef __GO32__
#ifndef STDC
extern void exit OF((int));
#endif
@@ -38,8 +38,8 @@ void z_error (m)
#ifndef HAVE_MEMCPY
void zmemcpy(dest, source, len)
Byte* dest;
Byte* source;
Bytef* dest;
Bytef* source;
uInt len;
{
if (len == 0) return;
@@ -49,7 +49,7 @@ void zmemcpy(dest, source, len)
}
void zmemzero(dest, len)
Byte* dest;
Bytef* dest;
uInt len;
{
if (len == 0) return;
@@ -59,8 +59,11 @@ void zmemzero(dest, len)
}
#endif
#if defined(__TURBOC__) && !defined(__SMALL__)
#ifdef __TURBOC__
#if !defined(__SMALL__) && !defined(__MEDIUM__) && !defined(__32BIT__)
/* Small and medium model are for now limited to near allocation with
* reduced MAX_WBITS and MAX_MEM_LEVEL
*/
# define MY_ZCALLOC
/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
@@ -75,8 +78,8 @@ void zmemzero(dest, len)
local int next_ptr = 0;
typedef struct ptr_table_s {
voidp org_ptr;
voidp new_ptr;
voidpf org_ptr;
voidpf new_ptr;
} ptr_table;
local ptr_table table[MAX_PTR];
@@ -87,13 +90,19 @@ local ptr_table table[MAX_PTR];
* a protected system like OS/2. Use Microsoft C instead.
*/
voidp zcalloc (voidp opaque, unsigned items, unsigned size)
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
{
voidp buf = opaque; /* just to make some compilers happy */
voidpf buf = opaque; /* just to make some compilers happy */
ulg bsize = (ulg)items*size;
if (bsize < 65536L) {
/* If we allocate less than 65520 bytes, we assume that farmalloc
* will return a usable pointer which doesn't have to be normalized.
*/
if (bsize < 65520L) {
buf = farmalloc(bsize);
#ifdef DEBUG
zmemzero(buf, (uInt)bsize);
#endif
if (*(ush*)&buf != 0) return buf;
} else {
buf = farmalloc(bsize + 16L);
@@ -105,10 +114,14 @@ voidp zcalloc (voidp opaque, unsigned items, unsigned size)
*((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
*(ush*)&buf = 0;
table[next_ptr++].new_ptr = buf;
#ifdef DEBUG
zmemzero(buf, (uInt)65535);
((uch*)buf)[65535] = 0;
#endif
return buf;
}
void zcfree (voidp opaque, voidp ptr)
void zcfree (voidpf opaque, voidpf ptr)
{
int n;
if (*(ush*)&ptr != 0) { /* object < 64K */
@@ -129,9 +142,11 @@ void zcfree (voidp opaque, voidp ptr)
ptr = opaque; /* just to make some compilers happy */
z_error("zcfree: ptr not found");
}
#endif
#endif /* __TURBOC__ */
#if defined(MSDOS) && !defined(__TURBOC__) /* MSC */
#ifdef M_I86 /* Microsoft C */
# define MY_ZCALLOC
@@ -140,13 +155,13 @@ void zcfree (voidp opaque, voidp ptr)
# define _hfree hfree
#endif
voidp zcalloc (voidp opaque, unsigned items, unsigned size)
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
{
if (opaque) opaque = 0; /* to make compiler happy */
return _halloc((long)items, size);
}
void zcfree (voidp opaque, voidp ptr)
void zcfree (voidpf opaque, voidpf ptr)
{
if (opaque) opaque = 0; /* to make compiler happy */
_hfree(ptr);
@@ -157,22 +172,22 @@ void zcfree (voidp opaque, voidp ptr)
#ifndef MY_ZCALLOC /* Any system without a special alloc function */
#ifndef __GO32__
#ifndef STDC
extern voidp calloc OF((uInt items, uInt size));
extern void free OF((voidp ptr));
extern void free OF((voidpf ptr));
#endif
voidp zcalloc (opaque, items, size)
voidp opaque;
voidpf zcalloc (opaque, items, size)
voidpf opaque;
unsigned items;
unsigned size;
{
return calloc(items, size);
return (voidpf)calloc(items, size);
}
void zcfree (opaque, ptr)
voidp opaque;
voidp ptr;
voidpf opaque;
voidpf ptr;
{
free(ptr);
}

33
zutil.h
View File

@@ -15,7 +15,7 @@
#include "zlib.h"
#if defined(MSDOS) || defined(VMS)
#if defined(MSDOS) || defined(VMS) || defined(CRAY)
# include <stddef.h>
# include <errno.h>
#else
@@ -23,6 +23,7 @@
#endif
#ifdef STDC
# include <string.h>
# include <stdlib.h>
#endif
#ifndef local
@@ -38,15 +39,13 @@ typedef unsigned long ulg;
extern char *z_errmsg[]; /* indexed by 1-zlib_error */
#define ERR_RETURN(strm,err) return (strm->msg=z_errmsg[1-err], err)
#define ERR_RETURN(strm,err) return (strm->msg = z_errmsg[1-(err)], (err))
/* To be used only when the state is known to be valid */
/* common constants */
#define DEFLATED 8
#ifndef DEF_WBITS
# define DEF_WBITS 15
# define DEF_WBITS MAX_WBITS
#endif
/* default windowBits for decompression. MAX_WBITS is for compression only */
@@ -72,7 +71,7 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
# define OS_CODE 0x00
# ifdef __TURBOC__
# include <alloc.h>
# else /* MSC */
# else /* MSC or DJGPP */
# include <malloc.h>
# endif
#endif
@@ -81,7 +80,7 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
# define OS_CODE 0x06
#endif
#ifdef WIN32 /* Windows NT */
#ifdef WIN32 /* Window 95 & Windows NT */
# define OS_CODE 0x0b
#endif
@@ -130,14 +129,20 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
# define zstrerror(errnum) ""
#endif
#if defined(pyr) && !defined(NO_MEMCPY)
#if defined(pyr)
# define NO_MEMCPY
#endif
#if (defined(M_I86SM) || defined(M_I86MM)) && !defined(_MSC_VER)
/* Use our own functions for small and medium model with MSC <= 5.0.
* You may have to use the same strategy for Borland C (untested).
*/
# define NO_MEMCPY
#endif
#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
# define HAVE_MEMCPY
#endif
#ifdef HAVE_MEMCPY
# ifdef M_I86MM /* MSC medium model */
# if defined(M_I86SM) || defined(M_I86MM) /* MSC small or medium model */
# define zmemcpy _fmemcpy
# define zmemzero(dest, len) _fmemset(dest, 0, len)
# else
@@ -145,8 +150,8 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
# define zmemzero(dest, len) memset(dest, 0, len)
# endif
#else
extern void zmemcpy OF((Byte* dest, Byte* source, uInt len));
extern void zmemzero OF((Byte* dest, uInt len));
extern void zmemcpy OF((Bytef* dest, Bytef* source, uInt len));
extern void zmemzero OF((Bytef* dest, uInt len));
#endif
/* Diagnostic functions */
@@ -175,12 +180,12 @@ typedef uLong (*check_func) OF((uLong check, Bytef *buf, uInt len));
extern void z_error OF((char *m));
voidp zcalloc OF((voidp opaque, unsigned items, unsigned size));
void zcfree OF((voidp opaque, voidp ptr));
voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
void zcfree OF((voidpf opaque, voidpf ptr));
#define ZALLOC(strm, items, size) \
(*((strm)->zalloc))((strm)->opaque, (items), (size))
#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidp)(addr))
#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
#endif /* _Z_UTIL_H */