Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8a2acbffc8 | ||
|
|
56bcb184fa |
32
ChangeLog
32
ChangeLog
@@ -1,6 +1,38 @@
|
||||
|
||||
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
|
||||
|
||||
@@ -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"
|
||||
|
||||
18
Makefile
18
Makefile
@@ -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
|
||||
|
||||
55
README
55
README
@@ -1,11 +1,9 @@
|
||||
zlib 0.95 is a beta version of a general purpose compression library.
|
||||
This should be the last version before the first official
|
||||
version (1.0), so please test it now. (At least compile and run it with
|
||||
"make test"). 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
|
||||
@@ -20,21 +18,42 @@ 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.95 are documented in the file ChangeLog.
|
||||
The main changes since 0.94 are:
|
||||
- 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).
|
||||
Questions about zlib should be sent to <zlib@quest.jpl.nasa.gov>.
|
||||
|
||||
For MSDOS, the small and medium models have been tested only with Microsoft C.
|
||||
(This should now work for Borland C also, but I don't have a recent 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
|
||||
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
|
||||
|
||||
@@ -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;
|
||||
|
||||
313
deflate.c
313
deflate.c
@@ -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
|
||||
@@ -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);
|
||||
|
||||
11
deflate.h
11
deflate.h
@@ -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: */
|
||||
|
||||
@@ -267,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));
|
||||
|
||||
210
example.c
210
example.c
@@ -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,7 +367,9 @@ 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");
|
||||
@@ -280,16 +378,28 @@ int main(argc, argv)
|
||||
} 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 */
|
||||
|
||||
11
gzio.c
11
gzio.c
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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, */
|
||||
{
|
||||
|
||||
@@ -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";
|
||||
|
||||
55
inftrees.c
55
inftrees.c
@@ -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 */
|
||||
@@ -30,10 +37,6 @@ local voidpf falloc OF((
|
||||
uInt, /* number of items */
|
||||
uInt)); /* size of item */
|
||||
|
||||
local void ffree OF((
|
||||
voidpf q, /* opaque pointer (not used) */
|
||||
voidpf 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;
|
||||
@@ -378,24 +379,14 @@ local inflate_huft *fixed_td;
|
||||
|
||||
|
||||
local voidpf falloc(q, n, s)
|
||||
voidpf q; /* opaque pointer (not used) */
|
||||
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 (voidpf)(fixed_mem + fixed_left);
|
||||
}
|
||||
|
||||
|
||||
local void ffree(q, p)
|
||||
voidpf q;
|
||||
voidpf 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;
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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>
|
||||
|
||||
74
trees.c
74
trees.c
@@ -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));
|
||||
@@ -187,7 +186,7 @@ local void send_bits(s, value, length)
|
||||
int value; /* value to send */
|
||||
int length; /* number of bits */
|
||||
{
|
||||
Tracev((stderr," l %2d v %4x ", length, value));
|
||||
Tracevv((stderr," l %2d v %4x ", length, value));
|
||||
Assert(length > 0 && length <= 15, "invalid length");
|
||||
s->bits_sent += (ulg)length;
|
||||
|
||||
@@ -227,11 +226,13 @@ local void send_bits(s, value, length)
|
||||
/* 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 */
|
||||
@@ -240,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++) {
|
||||
@@ -248,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:
|
||||
@@ -263,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;
|
||||
@@ -271,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;
|
||||
@@ -291,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;
|
||||
|
||||
@@ -523,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));
|
||||
}
|
||||
}
|
||||
@@ -783,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 */
|
||||
@@ -799,12 +801,15 @@ void ct_stored_block(s, buf, stored_len, eof)
|
||||
/* ===========================================================================
|
||||
* 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 EOB
|
||||
* code for the previous block was coded on 5 bits or less, inflate
|
||||
* may have only 5+3 bits of lookahead to decode this EOB.
|
||||
* (There are no problems if the previous block is stored or fixed.)
|
||||
* 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);
|
||||
@@ -812,10 +817,11 @@ void ct_align(s)
|
||||
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 EOB of the previous
|
||||
* block was thus its length plus what we have just 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 (s->last_eob_len + 10 - s->bi_valid < 9) {
|
||||
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;
|
||||
@@ -829,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 */
|
||||
@@ -838,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);
|
||||
|
||||
@@ -868,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
|
||||
@@ -899,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 */
|
||||
@@ -933,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) */
|
||||
@@ -949,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++;
|
||||
@@ -959,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 *
|
||||
|
||||
25
zconf.h
25
zconf.h
@@ -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>
|
||||
@@ -99,7 +102,7 @@
|
||||
# endif
|
||||
#endif
|
||||
#if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__))
|
||||
# define FAR __far /* completely untested, just a best guess */
|
||||
# define FAR _far /* completely untested, just a best guess */
|
||||
#endif
|
||||
#ifndef FAR
|
||||
# define FAR
|
||||
|
||||
72
zlib.h
72
zlib.h
@@ -1,7 +1,7 @@
|
||||
/* zlib.h -- interface of the 'zlib' general purpose compression library
|
||||
version 0.95, Aug 16th, 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.95"
|
||||
#define ZLIB_VERSION "1.0"
|
||||
|
||||
/*
|
||||
The 'zlib' compression library provides in-memory compression and
|
||||
@@ -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
|
||||
@@ -527,7 +556,8 @@ 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.
|
||||
@@ -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 */
|
||||
|
||||
24
zutil.c
24
zutil.c
@@ -11,7 +11,7 @@
|
||||
|
||||
struct internal_state {int dummy;}; /* for buggy compilers */
|
||||
|
||||
#ifndef __GO32__
|
||||
#ifndef STDC
|
||||
extern void exit OF((int));
|
||||
#endif
|
||||
|
||||
@@ -59,7 +59,8 @@ void zmemzero(dest, len)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined( __TURBOC__) && !defined(__SMALL__) && !defined(__MEDIUM__)
|
||||
#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
|
||||
*/
|
||||
@@ -94,8 +95,14 @@ voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
|
||||
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);
|
||||
@@ -107,6 +114,10 @@ voidpf zcalloc (voidpf 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;
|
||||
}
|
||||
|
||||
@@ -131,10 +142,11 @@ void zcfree (voidpf opaque, voidpf ptr)
|
||||
ptr = opaque; /* just to make some compilers happy */
|
||||
z_error("zcfree: ptr not found");
|
||||
}
|
||||
#endif
|
||||
#endif /* __TURBOC__ */
|
||||
|
||||
#if defined(M_I86SM)||defined(M_I86MM)||defined(M_I86CM)||defined(M_I86LM)
|
||||
/* Microsoft C */
|
||||
|
||||
#ifdef M_I86 /* Microsoft C */
|
||||
|
||||
# define MY_ZCALLOC
|
||||
|
||||
@@ -160,7 +172,7 @@ void zcfree (voidpf opaque, voidpf 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((voidpf ptr));
|
||||
#endif
|
||||
|
||||
9
zutil.h
9
zutil.h
@@ -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,13 +39,11 @@ 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 MAX_WBITS
|
||||
#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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user