aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2011-09-09 23:08:07 -0700
committerMark Adler <madler@alumni.caltech.edu>2011-09-09 23:08:07 -0700
commitbdde4e09d21edff02ea5093b7f6eccbf166b272f (patch)
treea64632a98a6bea6e5df864d6e5b6f2e51ea69c1c
parent1c71d8b13b54f91ddec361d3053ecce26e6ff761 (diff)
downloadzlib-bdde4e09d21edff02ea5093b7f6eccbf166b272f.tar.gz
zlib-bdde4e09d21edff02ea5093b7f6eccbf166b272f.tar.bz2
zlib-bdde4e09d21edff02ea5093b7f6eccbf166b272f.zip
zlib 0.92v0.92
-rw-r--r--ChangeLog9
-rw-r--r--Makefile11
-rw-r--r--README9
-rw-r--r--adler32.c24
-rw-r--r--compress.c6
-rw-r--r--deflate.c195
-rw-r--r--deflate.h10
-rw-r--r--example.c60
-rw-r--r--gzio.c284
-rw-r--r--infblock.c264
-rw-r--r--infblock.h2
-rw-r--r--infcodes.c184
-rw-r--r--inffast.c136
-rw-r--r--inflate.c80
-rw-r--r--inftest.c69
-rw-r--r--inftrees.c160
-rw-r--r--inftrees.h46
-rw-r--r--infutil.h56
-rw-r--r--minigzip.c90
-rw-r--r--trees.c42
-rw-r--r--uncompr.c6
-rw-r--r--zconf.h4
-rw-r--r--zlib.h12
-rw-r--r--zutil.c32
-rw-r--r--zutil.h19
25 files changed, 892 insertions, 918 deletions
diff --git a/ChangeLog b/ChangeLog
index e97948e..57f9986 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
1 ChangeLog file for zlib 1 ChangeLog file for zlib
2 2
3Changes in 0.92 (3 May 95)
4- don't assume that char is signed (problem on SGI)
5- Clear bit buffer when starting a stored block
6- no memcpy on Pyramid
7- suppressed inftest.c
8- optimized fill_window, put longest_match inline for gcc
9- optimized inflate on stored blocks.
10- untabify all sources to simplify patches
11
3Changes in 0.91 (2 May 95) 12Changes in 0.91 (2 May 95)
4- Default MEM_LEVEL is 8 (not 9 for Unix) as documented in zlib.h 13- Default MEM_LEVEL is 8 (not 9 for Unix) as documented in zlib.h
5- Document the memory requirements in zconf.h 14- Document the memory requirements in zconf.h
diff --git a/Makefile b/Makefile
index 9a48249..2d0cdc7 100644
--- a/Makefile
+++ b/Makefile
@@ -4,6 +4,7 @@
4 4
5CC=cc 5CC=cc
6CFLAGS=-O 6CFLAGS=-O
7#use -O3 for gcc to take advantage of inlining
7#CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" 8#CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
8#CFLAGS=-g -DDEBUG 9#CFLAGS=-g -DDEBUG
9LDFLAGS=-L. -lgz 10LDFLAGS=-L. -lgz
@@ -15,9 +16,9 @@ prefix=/usr/local
15OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \ 16OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \
16 zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o inffast.o 17 zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o inffast.o
17 18
18TEST_OBJS = example.o minigzip.o inftest.o 19TEST_OBJS = example.o minigzip.o
19 20
20all: example minigzip inftest 21all: example minigzip
21 22
22test: all 23test: all
23 ./example 24 ./example
@@ -41,11 +42,8 @@ example: example.o libgz.a
41minigzip: minigzip.o libgz.a 42minigzip: minigzip.o libgz.a
42 $(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS) 43 $(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS)
43 44
44inftest: inftest.o libgz.a
45 $(CC) $(CFLAGS) -o $@ inftest.o $(LDFLAGS)
46
47clean: 45clean:
48 rm -f *.o example minigzip inftest libgz.a foo.gz 46 rm -f *.o example minigzip libgz.a foo.gz
49 47
50zip: 48zip:
51 zip -ul9 zlib README ChangeLog Makefile Makefile.??? Makefile.?? *.[ch] 49 zip -ul9 zlib README ChangeLog Makefile Makefile.??? Makefile.?? *.[ch]
@@ -66,7 +64,6 @@ infblock.o: zutil.h zlib.h zconf.h infblock.h inftrees.h infcodes.h infutil.h
66infcodes.o: zutil.h zlib.h zconf.h inftrees.h infutil.h infcodes.h inffast.h 64infcodes.o: zutil.h zlib.h zconf.h inftrees.h infutil.h infcodes.h inffast.h
67inffast.o: zutil.h zlib.h zconf.h inftrees.h infutil.h inffast.h 65inffast.o: zutil.h zlib.h zconf.h inftrees.h infutil.h inffast.h
68inflate.o: zutil.h zlib.h zconf.h infblock.h 66inflate.o: zutil.h zlib.h zconf.h infblock.h
69inftest.o: zutil.h zlib.h zconf.h
70inftrees.o: zutil.h zlib.h zconf.h inftrees.h 67inftrees.o: zutil.h zlib.h zconf.h inftrees.h
71infutil.o: zutil.h zlib.h zconf.h inftrees.h infutil.h 68infutil.o: zutil.h zlib.h zconf.h inftrees.h infutil.h
72minigzip.o: zlib.h zconf.h 69minigzip.o: zlib.h zconf.h
diff --git a/README b/README
index caeccb9..bc4a663 100644
--- a/README
+++ b/README
@@ -1,4 +1,4 @@
1zlib 0.91 is a beta version of a general purpose compression library. 1zlib 0.92 is a beta version of a general purpose compression library.
2 2
3The data format used by the zlib library is described in the 3The data format used by the zlib library is described in the
4files zlib-3.1.doc, deflate-1.1.doc and gzip-4.1.doc, available 4files zlib-3.1.doc, deflate-1.1.doc and gzip-4.1.doc, available
@@ -14,12 +14,15 @@ To install the zlib library (libgz.a) in /usr/local/lib, type: make install
14To install in a different directory, use for example: make install prefix=$HOME 14To install in a different directory, use for example: make install prefix=$HOME
15This will install in $HOME/lib instead of /usr/local/lib. 15This will install in $HOME/lib instead of /usr/local/lib.
16 16
17The changes made in version 0.91 are documented in the file ChangeLog. 17The changes made in version 0.92 are documented in the file ChangeLog.
18The main changes since 0.9 are: 18The main changes since 0.9 are:
19- don't assume that char is signed (problem on SGI)
19- Default MEM_LEVEL is 8 (not 9 for Unix) as documented in zlib.h 20- Default MEM_LEVEL is 8 (not 9 for Unix) as documented in zlib.h
20- Document the memory requirements in zconf.h 21- Document the memory requirements in zconf.h
21- added "make install" 22- added "make install"
22- added support for DJGPP 23- added support for DJGPP and Pyramid
24- fix an inflate bug for stored blocks.
25- various speedups
23 26
24On MSDOS, this version works in both large and small model. However 27On MSDOS, this version works in both large and small model. However
25small model compression works only for small values of MAX_MEM_LEVEL 28small model compression works only for small values of MAX_MEM_LEVEL
diff --git a/adler32.c b/adler32.c
index 0b2b820..0ad9bec 100644
--- a/adler32.c
+++ b/adler32.c
@@ -3,7 +3,7 @@
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: adler32.c,v 1.5 1995/04/14 14:49:51 jloup Exp $ */ 6/* $Id: adler32.c,v 1.6 1995/05/03 17:27:08 jloup Exp $ */
7 7
8#include "zutil.h" 8#include "zutil.h"
9 9
@@ -30,17 +30,17 @@ uLong adler32(adler, buf, len)
30 if (buf == Z_NULL) return 1L; 30 if (buf == Z_NULL) return 1L;
31 31
32 while (len > 0) { 32 while (len > 0) {
33 k = len < NMAX ? len : NMAX; 33 k = len < NMAX ? len : NMAX;
34 len -= k; 34 len -= k;
35 while (k >= 16) { 35 while (k >= 16) {
36 DO16(buf); 36 DO16(buf);
37 k -= 16; 37 k -= 16;
38 } 38 }
39 if (k != 0) do { 39 if (k != 0) do {
40 DO1(buf); 40 DO1(buf);
41 } while (--k); 41 } while (--k);
42 s1 %= BASE; 42 s1 %= BASE;
43 s2 %= BASE; 43 s2 %= BASE;
44 } 44 }
45 return (s2 << 16) | s1; 45 return (s2 << 16) | s1;
46} 46}
diff --git a/compress.c b/compress.c
index 762a9df..98ced3e 100644
--- a/compress.c
+++ b/compress.c
@@ -3,7 +3,7 @@
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: compress.c,v 1.5 1995/04/29 17:18:43 jloup Exp $ */ 6/* $Id: compress.c,v 1.6 1995/05/03 17:27:08 jloup Exp $ */
7 7
8#include "zlib.h" 8#include "zlib.h"
9 9
@@ -45,8 +45,8 @@ int compress (dest, destLen, source, sourceLen)
45 45
46 err = deflate(&stream, Z_FINISH); 46 err = deflate(&stream, Z_FINISH);
47 if (err != Z_STREAM_END) { 47 if (err != Z_STREAM_END) {
48 deflateEnd(&stream); 48 deflateEnd(&stream);
49 return err == Z_OK ? Z_BUF_ERROR : err; 49 return err == Z_OK ? Z_BUF_ERROR : err;
50 } 50 }
51 *destLen = stream.total_out; 51 *destLen = stream.total_out;
52 52
diff --git a/deflate.c b/deflate.c
index b65ba25..6c0cd0a 100644
--- a/deflate.c
+++ b/deflate.c
@@ -47,7 +47,7 @@
47 * 47 *
48 */ 48 */
49 49
50/* $Id: deflate.c,v 1.7 1995/05/02 13:28:18 jloup Exp $ */ 50/* $Id: deflate.c,v 1.8 1995/05/03 17:27:08 jloup Exp $ */
51 51
52#include "deflate.h" 52#include "deflate.h"
53 53
@@ -127,7 +127,7 @@ local int read_buf __P((z_stream *strm, char *buf, unsigned size));
127 127
128#ifdef DEBUG 128#ifdef DEBUG
129local void check_match __P((deflate_state *s, IPos start, IPos match, 129local void check_match __P((deflate_state *s, IPos start, IPos match,
130 int length)); 130 int length));
131#endif 131#endif
132 132
133 133
@@ -190,12 +190,12 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
190 if (level == Z_DEFAULT_COMPRESSION) level = 6; 190 if (level == Z_DEFAULT_COMPRESSION) level = 6;
191 191
192 if (windowBits < 0) { /* undocumented feature: suppress zlib header */ 192 if (windowBits < 0) { /* undocumented feature: suppress zlib header */
193 noheader = 1; 193 noheader = 1;
194 windowBits = -windowBits; 194 windowBits = -windowBits;
195 } 195 }
196 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != DEFLATED || 196 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != DEFLATED ||
197 windowBits < 8 || windowBits > 15 || level < 1 || level > 9) { 197 windowBits < 8 || windowBits > 15 || level < 1 || level > 9) {
198 return Z_STREAM_ERROR; 198 return Z_STREAM_ERROR;
199 } 199 }
200 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); 200 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
201 if (s == Z_NULL) return Z_MEM_ERROR; 201 if (s == Z_NULL) return Z_MEM_ERROR;
@@ -221,10 +221,10 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
221 s->pending_buf = (uch*) ZALLOC(strm, s->lit_bufsize, 2*sizeof(ush)); 221 s->pending_buf = (uch*) ZALLOC(strm, s->lit_bufsize, 2*sizeof(ush));
222 222
223 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 223 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
224 s->pending_buf == Z_NULL) { 224 s->pending_buf == Z_NULL) {
225 strm->msg = z_errmsg[1-Z_MEM_ERROR]; 225 strm->msg = z_errmsg[1-Z_MEM_ERROR];
226 deflateEnd (strm); 226 deflateEnd (strm);
227 return Z_MEM_ERROR; 227 return Z_MEM_ERROR;
228 } 228 }
229 s->d_buf = (ush*) &(s->pending_buf[s->lit_bufsize]); 229 s->d_buf = (ush*) &(s->pending_buf[s->lit_bufsize]);
230 s->l_buf = (uch*) &(s->pending_buf[3*s->lit_bufsize]); 230 s->l_buf = (uch*) &(s->pending_buf[3*s->lit_bufsize]);
@@ -247,7 +247,7 @@ int deflateReset (strm)
247 deflate_state *s; 247 deflate_state *s;
248 248
249 if (strm == Z_NULL || strm->state == Z_NULL || 249 if (strm == Z_NULL || strm->state == Z_NULL ||
250 strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR; 250 strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR;
251 251
252 strm->total_in = strm->total_out = 0; 252 strm->total_in = strm->total_out = 0;
253 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ 253 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
@@ -297,7 +297,7 @@ local void flush_pending(strm)
297 strm->avail_out -= len; 297 strm->avail_out -= len;
298 strm->state->pending -= len; 298 strm->state->pending -= len;
299 if (strm->state->pending == 0) { 299 if (strm->state->pending == 0) {
300 strm->state->pending_out = strm->state->pending_buf; 300 strm->state->pending_out = strm->state->pending_buf;
301 } 301 }
302} 302}
303 303
@@ -309,7 +309,7 @@ int deflate (strm, flush)
309 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 309 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
310 310
311 if (strm->next_out == Z_NULL || strm->next_in == Z_NULL) { 311 if (strm->next_out == Z_NULL || strm->next_in == Z_NULL) {
312 ERR_RETURN(strm, Z_STREAM_ERROR); 312 ERR_RETURN(strm, Z_STREAM_ERROR);
313 } 313 }
314 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); 314 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
315 315
@@ -318,49 +318,49 @@ int deflate (strm, flush)
318 /* Write the zlib header */ 318 /* Write the zlib header */
319 if (strm->state->status == INIT_STATE) { 319 if (strm->state->status == INIT_STATE) {
320 320
321 uInt header = (DEFLATED + ((strm->state->w_bits-8)<<4)) << 8; 321 uInt header = (DEFLATED + ((strm->state->w_bits-8)<<4)) << 8;
322 uInt level_flags = (strm->state->level-1) >> 1; 322 uInt level_flags = (strm->state->level-1) >> 1;
323 323
324 if (level_flags > 3) level_flags = 3; 324 if (level_flags > 3) level_flags = 3;
325 header |= (level_flags << 6); 325 header |= (level_flags << 6);
326 header += 31 - (header % 31); 326 header += 31 - (header % 31);
327 327
328 strm->state->status = BUSY_STATE; 328 strm->state->status = BUSY_STATE;
329 putShortMSB(strm->state, header); 329 putShortMSB(strm->state, header);
330 } 330 }
331 331
332 /* Flush as much pending output as possible */ 332 /* Flush as much pending output as possible */
333 if (strm->state->pending != 0) { 333 if (strm->state->pending != 0) {
334 flush_pending(strm); 334 flush_pending(strm);
335 if (strm->avail_out == 0) return Z_OK; 335 if (strm->avail_out == 0) return Z_OK;
336 } 336 }
337 337
338 /* User must not provide more input after the first FINISH: */ 338 /* User must not provide more input after the first FINISH: */
339 if (strm->state->status == FINISH_STATE && strm->avail_in != 0) { 339 if (strm->state->status == FINISH_STATE && strm->avail_in != 0) {
340 ERR_RETURN(strm, Z_BUF_ERROR); 340 ERR_RETURN(strm, Z_BUF_ERROR);
341 } 341 }
342 342
343 /* Start a new block or continue the current one. 343 /* Start a new block or continue the current one.
344 */ 344 */
345 if (strm->avail_in != 0 || 345 if (strm->avail_in != 0 ||
346 (flush == Z_FINISH && strm->state->status != FINISH_STATE)) { 346 (flush == Z_FINISH && strm->state->status != FINISH_STATE)) {
347 int quit; 347 int quit;
348 348
349 if (flush == Z_FINISH) { 349 if (flush == Z_FINISH) {
350 strm->state->status = FINISH_STATE; 350 strm->state->status = FINISH_STATE;
351 } 351 }
352 if (strm->state->level <= 3) { 352 if (strm->state->level <= 3) {
353 quit = deflate_fast(strm->state, flush); 353 quit = deflate_fast(strm->state, flush);
354 } else { 354 } else {
355 quit = deflate_slow(strm->state, flush); 355 quit = deflate_slow(strm->state, flush);
356 } 356 }
357 if (flush == Z_FULL_FLUSH) { 357 if (flush == Z_FULL_FLUSH) {
358 ct_stored_block(strm->state, (char*)0, 0L, 0); /* special marker */ 358 ct_stored_block(strm->state, (char*)0, 0L, 0); /* special marker */
359 flush_pending(strm); 359 flush_pending(strm);
360 CLEAR_HASH(strm->state); /* forget history */ 360 CLEAR_HASH(strm->state); /* forget history */
361 if (strm->avail_out == 0) return Z_OK; 361 if (strm->avail_out == 0) return Z_OK;
362 } 362 }
363 if (quit) return Z_OK; 363 if (quit) return Z_OK;
364 } 364 }
365 Assert(strm->avail_out > 0, "bug2"); 365 Assert(strm->avail_out > 0, "bug2");
366 366
@@ -401,13 +401,13 @@ int deflateCopy (dest, source)
401 z_stream *source; 401 z_stream *source;
402{ 402{
403 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { 403 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
404 return Z_STREAM_ERROR; 404 return Z_STREAM_ERROR;
405 } 405 }
406 *dest = *source; 406 *dest = *source;
407 return Z_STREAM_ERROR; /* to be implemented */ 407 return Z_STREAM_ERROR; /* to be implemented */
408#if 0 408#if 0
409 dest->state = (struct internal_state *) 409 dest->state = (struct internal_state *)
410 (*dest->zalloc)(1, sizeof(deflate_state)); 410 (*dest->zalloc)(1, sizeof(deflate_state));
411 if (dest->state == Z_NULL) return Z_MEM_ERROR; 411 if (dest->state == Z_NULL) return Z_MEM_ERROR;
412 412
413 *(dest->state) = *(source->state); 413 *(dest->state) = *(source->state);
@@ -432,7 +432,7 @@ local int read_buf(strm, buf, size)
432 strm->avail_in -= len; 432 strm->avail_in -= len;
433 433
434 if (!strm->state->noheader) { 434 if (!strm->state->noheader) {
435 strm->state->adler = adler32(strm->state->adler, strm->next_in, len); 435 strm->state->adler = adler32(strm->state->adler, strm->next_in, len);
436 } 436 }
437 zmemcpy(buf, strm->next_in, len); 437 zmemcpy(buf, strm->next_in, len);
438 strm->next_in += len; 438 strm->next_in += len;
@@ -488,7 +488,7 @@ local void lm_init (s)
488/* For 80x86 and 680x0, an optimized version will be provided in match.asm or 488/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
489 * match.S. The code will be functionally equivalent. 489 * match.S. The code will be functionally equivalent.
490 */ 490 */
491local int longest_match(s, cur_match) 491local INLINE int longest_match(s, cur_match)
492 deflate_state *s; 492 deflate_state *s;
493 IPos cur_match; /* current match */ 493 IPos cur_match; /* current match */
494{ 494{
@@ -498,10 +498,12 @@ local int longest_match(s, cur_match)
498 register int len; /* length of current match */ 498 register int len; /* length of current match */
499 int best_len = s->prev_length; /* best match length so far */ 499 int best_len = s->prev_length; /* best match length so far */
500 IPos limit = s->strstart > (IPos)MAX_DIST(s) ? 500 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
501 s->strstart - (IPos)MAX_DIST(s) : NIL; 501 s->strstart - (IPos)MAX_DIST(s) : NIL;
502 /* Stop when cur_match becomes <= limit. To simplify the code, 502 /* Stop when cur_match becomes <= limit. To simplify the code,
503 * we prevent matches with the string of window index 0. 503 * we prevent matches with the string of window index 0.
504 */ 504 */
505 Pos *prev = s->prev;
506 uInt wmask = s->w_mask;
505 507
506#ifdef UNALIGNED_OK 508#ifdef UNALIGNED_OK
507 /* Compare two bytes at a time. Note: this is not always beneficial. 509 /* Compare two bytes at a time. Note: this is not always beneficial.
@@ -609,7 +611,7 @@ local int longest_match(s, cur_match)
609 scan_end = scan[best_len]; 611 scan_end = scan[best_len];
610#endif 612#endif
611 } 613 }
612 } while ((cur_match = s->prev[cur_match & s->w_mask]) > limit 614 } while ((cur_match = prev[cur_match & wmask]) > limit
613 && --chain_length != 0); 615 && --chain_length != 0);
614 616
615 return best_len; 617 return best_len;
@@ -634,8 +636,8 @@ local void check_match(s, start, match, length)
634 z_error("invalid match"); 636 z_error("invalid match");
635 } 637 }
636 if (verbose > 1) { 638 if (verbose > 1) {
637 fprintf(stderr,"\\[%d,%d]", start-match, length); 639 fprintf(stderr,"\\[%d,%d]", start-match, length);
638 do { putc(s->window[start++], stderr); } while (--length != 0); 640 do { putc(s->window[start++], stderr); } while (--length != 0);
639 } 641 }
640} 642}
641#else 643#else
@@ -656,14 +658,16 @@ local void fill_window(s)
656 deflate_state *s; 658 deflate_state *s;
657{ 659{
658 register unsigned n, m; 660 register unsigned n, m;
661 register Pos *p;
659 unsigned more; /* Amount of free space at the end of the window. */ 662 unsigned more; /* Amount of free space at the end of the window. */
663 uInt wsize = s->w_size;
660 664
661 do { 665 do {
662 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); 666 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
663 667
664 /* Deal with !@#$% 64K limit: */ 668 /* Deal with !@#$% 64K limit: */
665 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { 669 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
666 more = s->w_size; 670 more = wsize;
667 } else if (more == (unsigned)(-1)) { 671 } else if (more == (unsigned)(-1)) {
668 /* Very unlikely, but possible on 16 bit machine if strstart == 0 672 /* Very unlikely, but possible on 16 bit machine if strstart == 0
669 * and lookahead == 1 (input done one byte at time) 673 * and lookahead == 1 (input done one byte at time)
@@ -673,30 +677,39 @@ local void fill_window(s)
673 /* If the window is almost full and there is insufficient lookahead, 677 /* If the window is almost full and there is insufficient lookahead,
674 * move the upper half to the lower one to make room in the upper half. 678 * move the upper half to the lower one to make room in the upper half.
675 */ 679 */
676 } else if (s->strstart >= s->w_size+MAX_DIST(s)) { 680 } else if (s->strstart >= wsize+MAX_DIST(s)) {
677 681
678 /* By the IN assertion, the window is not empty so we can't confuse 682 /* By the IN assertion, the window is not empty so we can't confuse
679 * more == 0 with more == 64K on a 16 bit machine. 683 * more == 0 with more == 64K on a 16 bit machine.
680 */ 684 */
681 memcpy((char*)s->window, (char*)s->window+s->w_size, 685 zmemcpy((char*)s->window, (char*)s->window+wsize,
682 (unsigned)s->w_size); 686 (unsigned)wsize);
683 s->match_start -= s->w_size; 687 s->match_start -= wsize;
684 s->strstart -= s->w_size; /* we now have strstart >= MAX_DIST */ 688 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
685 689
686 s->block_start -= (long) s->w_size; 690 s->block_start -= (long) wsize;
687 691
688 for (n = 0; n < s->hash_size; n++) { 692 /* Slide the hash table (could be avoided with 32 bit values
689 m = s->head[n]; 693 at the expense of memory usage):
690 s->head[n] = (Pos)(m >= s->w_size ? m-s->w_size : NIL); 694 */
691 } 695 n = s->hash_size;
692 for (n = 0; n < s->w_size; n++) { 696 p = &s->head[n-1];
693 m = s->prev[n]; 697 do {
694 s->prev[n] = (Pos)(m >= s->w_size ? m-s->w_size : NIL); 698 m = *p;
699 *p-- = (Pos)(m >= wsize ? m-wsize : NIL);
700 } while (--n);
701
702 n = wsize;
703 p = &s->prev[n-1];
704 do {
705 m = *p;
706 *p-- = (Pos)(m >= wsize ? m-wsize : NIL);
695 /* If n is not on any hash chain, prev[n] is garbage but 707 /* If n is not on any hash chain, prev[n] is garbage but
696 * its value will never be used. 708 * its value will never be used.
697 */ 709 */
698 } 710 } while (--n);
699 more += s->w_size; 711
712 more += wsize;
700 } 713 }
701 if (s->strm->avail_in == 0) return; 714 if (s->strm->avail_in == 0) return;
702 715
@@ -714,8 +727,8 @@ local void fill_window(s)
714 Assert(more >= 2, "more < 2"); 727 Assert(more >= 2, "more < 2");
715 728
716 n = read_buf(s->strm, (char*)s->window + s->strstart + s->lookahead, 729 n = read_buf(s->strm, (char*)s->window + s->strstart + s->lookahead,
717 more); 730 more);
718 s->lookahead += n; 731 s->lookahead += n;
719 732
720 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); 733 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
721} 734}
@@ -726,7 +739,7 @@ local void fill_window(s)
726 */ 739 */
727#define FLUSH_BLOCK_ONLY(s, eof) { \ 740#define FLUSH_BLOCK_ONLY(s, eof) { \
728 ct_flush_block(s, (s->block_start >= 0L ? \ 741 ct_flush_block(s, (s->block_start >= 0L ? \
729 (char*)&s->window[(unsigned)s->block_start] : \ 742 (char*)&s->window[(unsigned)s->block_start] : \
730 (char*)Z_NULL), (long)s->strstart - s->block_start, (eof)); \ 743 (char*)Z_NULL), (long)s->strstart - s->block_start, (eof)); \
731 s->block_start = s->strstart; \ 744 s->block_start = s->strstart; \
732 flush_pending(s->strm); \ 745 flush_pending(s->strm); \
@@ -761,11 +774,11 @@ local int deflate_fast(s, flush)
761 * string following the next match. 774 * string following the next match.
762 */ 775 */
763 if (s->lookahead < MIN_LOOKAHEAD) { 776 if (s->lookahead < MIN_LOOKAHEAD) {
764 fill_window(s); 777 fill_window(s);
765 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1; 778 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1;
766 779
767 if (s->lookahead == 0) break; /* flush the current block */ 780 if (s->lookahead == 0) break; /* flush the current block */
768 } 781 }
769 782
770 /* Insert the string window[strstart .. strstart+2] in the 783 /* Insert the string window[strstart .. strstart+2] in the
771 * dictionary, and set hash_head to the head of the hash chain: 784 * dictionary, and set hash_head to the head of the hash chain:
@@ -780,9 +793,9 @@ local int deflate_fast(s, flush)
780 * of window index 0 (in particular we have to avoid a match 793 * of window index 0 (in particular we have to avoid a match
781 * of the string with itself at the start of the input file). 794 * of the string with itself at the start of the input file).
782 */ 795 */
783 if (s->strategy != Z_HUFFMAN_ONLY) { 796 if (s->strategy != Z_HUFFMAN_ONLY) {
784 s->match_length = longest_match (s, hash_head); 797 s->match_length = longest_match (s, hash_head);
785 } 798 }
786 /* longest_match() sets match_start */ 799 /* longest_match() sets match_start */
787 800
788 if (s->match_length > s->lookahead) s->match_length = s->lookahead; 801 if (s->match_length > s->lookahead) s->match_length = s->lookahead;
@@ -791,7 +804,7 @@ local int deflate_fast(s, flush)
791 check_match(s, s->strstart, s->match_start, s->match_length); 804 check_match(s, s->strstart, s->match_start, s->match_length);
792 805
793 bflush = ct_tally(s, s->strstart - s->match_start, 806 bflush = ct_tally(s, s->strstart - s->match_start,
794 s->match_length - MIN_MATCH); 807 s->match_length - MIN_MATCH);
795 808
796 s->lookahead -= s->match_length; 809 s->lookahead -= s->match_length;
797 810
@@ -852,11 +865,11 @@ local int deflate_slow(s, flush)
852 * string following the next match. 865 * string following the next match.
853 */ 866 */
854 if (s->lookahead < MIN_LOOKAHEAD) { 867 if (s->lookahead < MIN_LOOKAHEAD) {
855 fill_window(s); 868 fill_window(s);
856 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1; 869 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1;
857 870
858 if (s->lookahead == 0) break; /* flush the current block */ 871 if (s->lookahead == 0) break; /* flush the current block */
859 } 872 }
860 873
861 /* Insert the string window[strstart .. strstart+2] in the 874 /* Insert the string window[strstart .. strstart+2] in the
862 * dictionary, and set hash_head to the head of the hash chain: 875 * dictionary, and set hash_head to the head of the hash chain:
@@ -874,15 +887,15 @@ local int deflate_slow(s, flush)
874 * of window index 0 (in particular we have to avoid a match 887 * of window index 0 (in particular we have to avoid a match
875 * of the string with itself at the start of the input file). 888 * of the string with itself at the start of the input file).
876 */ 889 */
877 if (s->strategy != Z_HUFFMAN_ONLY) { 890 if (s->strategy != Z_HUFFMAN_ONLY) {
878 s->match_length = longest_match (s, hash_head); 891 s->match_length = longest_match (s, hash_head);
879 } 892 }
880 /* longest_match() sets match_start */ 893 /* longest_match() sets match_start */
881 if (s->match_length > s->lookahead) s->match_length = s->lookahead; 894 if (s->match_length > s->lookahead) s->match_length = s->lookahead;
882 895
883 if (s->match_length <= 5 && (s->strategy == Z_FILTERED || 896 if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
884 (s->match_length == MIN_MATCH && 897 (s->match_length == MIN_MATCH &&
885 s->strstart - s->match_start > TOO_FAR))) { 898 s->strstart - s->match_start > TOO_FAR))) {
886 899
887 /* If prev_match is also MIN_MATCH, match_start is garbage 900 /* If prev_match is also MIN_MATCH, match_start is garbage
888 * but we will ignore the current match anyway. 901 * but we will ignore the current match anyway.
@@ -898,7 +911,7 @@ local int deflate_slow(s, flush)
898 check_match(s, s->strstart-1, s->prev_match, s->prev_length); 911 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
899 912
900 bflush = ct_tally(s, s->strstart -1 - s->prev_match, 913 bflush = ct_tally(s, s->strstart -1 - s->prev_match,
901 s->prev_length - MIN_MATCH); 914 s->prev_length - MIN_MATCH);
902 915
903 /* Insert in hash table all strings up to the end of the match. 916 /* Insert in hash table all strings up to the end of the match.
904 * strstart-1 and strstart are already inserted. 917 * strstart-1 and strstart are already inserted.
@@ -927,11 +940,11 @@ local int deflate_slow(s, flush)
927 */ 940 */
928 Tracevv((stderr,"%c", s->window[s->strstart-1])); 941 Tracevv((stderr,"%c", s->window[s->strstart-1]));
929 if (ct_tally (s, 0, s->window[s->strstart-1])) { 942 if (ct_tally (s, 0, s->window[s->strstart-1])) {
930 FLUSH_BLOCK_ONLY(s, 0); 943 FLUSH_BLOCK_ONLY(s, 0);
931 } 944 }
932 s->strstart++; 945 s->strstart++;
933 s->lookahead--; 946 s->lookahead--;
934 if (s->strm->avail_out == 0) return 1; 947 if (s->strm->avail_out == 0) return 1;
935 } else { 948 } else {
936 /* There is no previous match to compare with, wait for 949 /* There is no previous match to compare with, wait for
937 * the next step to decide. 950 * the next step to decide.
@@ -942,8 +955,8 @@ local int deflate_slow(s, flush)
942 } 955 }
943 } 956 }
944 if (s->match_available) { 957 if (s->match_available) {
945 ct_tally (s, 0, s->window[s->strstart-1]); 958 ct_tally (s, 0, s->window[s->strstart-1]);
946 s->match_available = 0; 959 s->match_available = 0;
947 } 960 }
948 FLUSH_BLOCK(s, flush == Z_FINISH); 961 FLUSH_BLOCK(s, flush == Z_FINISH);
949 return 0; 962 return 0;
diff --git a/deflate.h b/deflate.h
index 1f8068e..c292fc7 100644
--- a/deflate.h
+++ b/deflate.h
@@ -8,7 +8,7 @@
8 subject to change. Applications should only use zlib.h. 8 subject to change. Applications should only use zlib.h.
9 */ 9 */
10 10
11/* $Id: deflate.h,v 1.4 1995/05/01 15:08:45 jloup Exp $ */ 11/* $Id: deflate.h,v 1.5 1995/05/03 17:27:09 jloup Exp $ */
12 12
13#include "zutil.h" 13#include "zutil.h"
14 14
@@ -90,7 +90,7 @@ typedef struct internal_state {
90 Byte data_type; /* UNKNOWN, BINARY or ASCII */ 90 Byte data_type; /* UNKNOWN, BINARY or ASCII */
91 Byte method; /* STORED (for zip only) or DEFLATED */ 91 Byte method; /* STORED (for zip only) or DEFLATED */
92 92
93 /* used by deflate.c: */ 93 /* used by deflate.c: */
94 94
95 uInt w_size; /* LZ77 window size (32K by default) */ 95 uInt w_size; /* LZ77 window size (32K by default) */
96 uInt w_bits; /* log2(w_size) (8..16) */ 96 uInt w_bits; /* log2(w_size) (8..16) */
@@ -173,7 +173,7 @@ typedef struct internal_state {
173 173
174 int nice_match; /* Stop searching when current match exceeds this */ 174 int nice_match; /* Stop searching when current match exceeds this */
175 175
176 /* used by trees.c: */ 176 /* used by trees.c: */
177 177
178 ct_data dyn_ltree[HEAP_SIZE]; /* literal and length tree */ 178 ct_data dyn_ltree[HEAP_SIZE]; /* literal and length tree */
179 ct_data dyn_dtree[2*D_CODES+1]; /* distance tree */ 179 ct_data dyn_dtree[2*D_CODES+1]; /* distance tree */
@@ -264,9 +264,9 @@ typedef struct internal_state {
264 * distances are limited to MAX_DIST instead of WSIZE. 264 * distances are limited to MAX_DIST instead of WSIZE.
265 */ 265 */
266 266
267 /* in trees.c */ 267 /* in trees.c */
268void ct_init __P((deflate_state *s)); 268void ct_init __P((deflate_state *s));
269int ct_tally __P((deflate_state *s, int dist, int lc)); 269int ct_tally __P((deflate_state *s, int dist, int lc));
270ulg ct_flush_block __P((deflate_state *s, char *buf, ulg stored_len, int eof)); 270ulg ct_flush_block __P((deflate_state *s, char *buf, ulg stored_len, int eof));
271void ct_stored_block __P((deflate_state *s, char *buf, ulg stored_len, 271void ct_stored_block __P((deflate_state *s, char *buf, ulg stored_len,
272 int eof)); 272 int eof));
diff --git a/example.c b/example.c
index 5c34733..a2213e0 100644
--- a/example.c
+++ b/example.c
@@ -3,7 +3,7 @@
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: example.c,v 1.8 1995/05/02 15:52:32 jloup Exp $ */ 6/* $Id: example.c,v 1.9 1995/05/03 17:27:09 jloup Exp $ */
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include "zlib.h" 9#include "zlib.h"
@@ -27,7 +27,7 @@ extern void exit __P((int));
27#define CHECK_ERR(err, msg) { \ 27#define CHECK_ERR(err, msg) { \
28 if (err != Z_OK) { \ 28 if (err != Z_OK) { \
29 fprintf(stderr, "%s error: %d\n", msg, err); \ 29 fprintf(stderr, "%s error: %d\n", msg, err); \
30 exit(1); \ 30 exit(1); \
31 } \ 31 } \
32} 32}
33 33
@@ -60,9 +60,9 @@ void test_compress()
60 CHECK_ERR(err, "uncompress"); 60 CHECK_ERR(err, "uncompress");
61 61
62 if (strcmp((char*)uncompr, hello)) { 62 if (strcmp((char*)uncompr, hello)) {
63 fprintf(stderr, "bad uncompress\n"); 63 fprintf(stderr, "bad uncompress\n");
64 } else { 64 } else {
65 printf("uncompress(): %s\n", uncompr); 65 printf("uncompress(): %s\n", uncompr);
66 } 66 }
67} 67}
68 68
@@ -81,31 +81,31 @@ void test_gzio(out, in)
81 81
82 file = gzopen(out, "wb"); 82 file = gzopen(out, "wb");
83 if (file == NULL) { 83 if (file == NULL) {
84 fprintf(stderr, "gzopen error\n"); 84 fprintf(stderr, "gzopen error\n");
85 exit(1); 85 exit(1);
86 } 86 }
87 87
88 if (gzwrite(file, hello, len) != len) { 88 if (gzwrite(file, hello, len) != len) {
89 fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err)); 89 fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err));
90 } 90 }
91 gzclose(file); 91 gzclose(file);
92 92
93 file = gzopen(in, "rb"); 93 file = gzopen(in, "rb");
94 if (file == NULL) { 94 if (file == NULL) {
95 fprintf(stderr, "gzopen error\n"); 95 fprintf(stderr, "gzopen error\n");
96 } 96 }
97 strcpy((char*)uncompr, "garbage"); 97 strcpy((char*)uncompr, "garbage");
98 98
99 uncomprLen = gzread(file, uncompr, uncomprLen); 99 uncomprLen = gzread(file, uncompr, uncomprLen);
100 if (uncomprLen != len) { 100 if (uncomprLen != len) {
101 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); 101 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
102 } 102 }
103 gzclose(file); 103 gzclose(file);
104 104
105 if (strcmp((char*)uncompr, hello)) { 105 if (strcmp((char*)uncompr, hello)) {
106 fprintf(stderr, "bad gzread\n"); 106 fprintf(stderr, "bad gzread\n");
107 } else { 107 } else {
108 printf("gzread(): %s\n", uncompr); 108 printf("gzread(): %s\n", uncompr);
109 } 109 }
110} 110}
111 111
@@ -129,16 +129,16 @@ void test_deflate(compr)
129 c_stream.next_out = compr; 129 c_stream.next_out = compr;
130 130
131 while (c_stream.total_in != (uLong)len) { 131 while (c_stream.total_in != (uLong)len) {
132 c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */ 132 c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
133 err = deflate(&c_stream, Z_NO_FLUSH); 133 err = deflate(&c_stream, Z_NO_FLUSH);
134 CHECK_ERR(err, "deflate"); 134 CHECK_ERR(err, "deflate");
135 } 135 }
136 /* Finish the stream, still forcing small buffers: */ 136 /* Finish the stream, still forcing small buffers: */
137 for (;;) { 137 for (;;) {
138 c_stream.avail_out = 1; 138 c_stream.avail_out = 1;
139 err = deflate(&c_stream, Z_FINISH); 139 err = deflate(&c_stream, Z_FINISH);
140 if (err == Z_STREAM_END) break; 140 if (err == Z_STREAM_END) break;
141 CHECK_ERR(err, "deflate"); 141 CHECK_ERR(err, "deflate");
142 } 142 }
143 143
144 err = deflateEnd(&c_stream); 144 err = deflateEnd(&c_stream);
@@ -167,19 +167,19 @@ void test_inflate(compr)
167 d_stream.next_out = uncompr; 167 d_stream.next_out = uncompr;
168 168
169 for (;;) { 169 for (;;) {
170 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ 170 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
171 err = inflate(&d_stream, Z_NO_FLUSH); 171 err = inflate(&d_stream, Z_NO_FLUSH);
172 if (err == Z_STREAM_END) break; 172 if (err == Z_STREAM_END) break;
173 CHECK_ERR(err, "inflate"); 173 CHECK_ERR(err, "inflate");
174 } 174 }
175 175
176 err = inflateEnd(&d_stream); 176 err = inflateEnd(&d_stream);
177 CHECK_ERR(err, "inflateEnd"); 177 CHECK_ERR(err, "inflateEnd");
178 178
179 if (strcmp((char*)uncompr, hello)) { 179 if (strcmp((char*)uncompr, hello)) {
180 fprintf(stderr, "bad inflate\n"); 180 fprintf(stderr, "bad inflate\n");
181 } else { 181 } else {
182 printf("inflate(): %s\n", uncompr); 182 printf("inflate(): %s\n", uncompr);
183 } 183 }
184} 184}
185 185
@@ -211,7 +211,7 @@ void test_flush(compr)
211 211
212 err = deflate(&c_stream, Z_FINISH); 212 err = deflate(&c_stream, Z_FINISH);
213 if (err != Z_STREAM_END) { 213 if (err != Z_STREAM_END) {
214 CHECK_ERR(err, "deflate"); 214 CHECK_ERR(err, "deflate");
215 } 215 }
216 err = deflateEnd(&c_stream); 216 err = deflateEnd(&c_stream);
217 CHECK_ERR(err, "deflateEnd"); 217 CHECK_ERR(err, "deflateEnd");
@@ -250,7 +250,7 @@ void test_sync(compr)
250 err = inflate(&d_stream, Z_FINISH); 250 err = inflate(&d_stream, Z_FINISH);
251 if (err != Z_DATA_ERROR) { 251 if (err != Z_DATA_ERROR) {
252 fprintf(stderr, "inflate should report DATA_ERROR\n"); 252 fprintf(stderr, "inflate should report DATA_ERROR\n");
253 /* Because of incorrect adler32 */ 253 /* Because of incorrect adler32 */
254 } 254 }
255 err = inflateEnd(&d_stream); 255 err = inflateEnd(&d_stream);
256 CHECK_ERR(err, "inflateEnd"); 256 CHECK_ERR(err, "inflateEnd");
@@ -269,16 +269,16 @@ void main(argc, argv)
269 local Byte compr[BUFLEN]; 269 local Byte compr[BUFLEN];
270 270
271 if (zlib_version[0] != ZLIB_VERSION[0]) { 271 if (zlib_version[0] != ZLIB_VERSION[0]) {
272 fprintf(stderr, "incompatible zlib version\n"); 272 fprintf(stderr, "incompatible zlib version\n");
273 exit(1); 273 exit(1);
274 274
275 } else if (strcmp(zlib_version, ZLIB_VERSION) != 0) { 275 } else if (strcmp(zlib_version, ZLIB_VERSION) != 0) {
276 fprintf(stderr, "warning: different zlib version\n"); 276 fprintf(stderr, "warning: different zlib version\n");
277 } 277 }
278 test_compress(); 278 test_compress();
279 279
280 test_gzio((argc > 1 ? argv[1] : "foo.gz"), 280 test_gzio((argc > 1 ? argv[1] : "foo.gz"),
281 (argc > 2 ? argv[2] : "foo.gz")); 281 (argc > 2 ? argv[2] : "foo.gz"));
282 282
283 test_deflate(compr); 283 test_deflate(compr);
284 test_inflate(compr); 284 test_inflate(compr);
diff --git a/gzio.c b/gzio.c
index 7a3d05a..d323fbc 100644
--- a/gzio.c
+++ b/gzio.c
@@ -3,7 +3,7 @@
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: gzio.c,v 1.7 1995/05/02 12:22:08 jloup Exp $ */ 6/* $Id: gzio.c,v 1.8 1995/05/03 17:27:09 jloup Exp $ */
7 7
8#include <stdio.h> 8#include <stdio.h>
9 9
@@ -68,13 +68,13 @@ local int destroy (s)
68 68
69 if (s->stream.state != NULL) { 69 if (s->stream.state != NULL) {
70 if (s->mode == 'w') { 70 if (s->mode == 'w') {
71 err = deflateEnd(&(s->stream)); 71 err = deflateEnd(&(s->stream));
72 } else if (s->mode == 'r') { 72 } else if (s->mode == 'r') {
73 err = inflateEnd(&(s->stream)); 73 err = inflateEnd(&(s->stream));
74 } 74 }
75 } 75 }
76 if (s->file != NULL && fclose(s->file)) { 76 if (s->file != NULL && fclose(s->file)) {
77 err = Z_ERRNO; 77 err = Z_ERRNO;
78 } 78 }
79 if (s->z_err < 0) err = s->z_err; 79 if (s->z_err < 0) err = s->z_err;
80 zcfree((voidp)0, s); 80 zcfree((voidp)0, s);
@@ -115,34 +115,34 @@ local gzFile gz_open (path, mode, fd)
115 115
116 s->path = (char*)ALLOC(strlen(path)+1); 116 s->path = (char*)ALLOC(strlen(path)+1);
117 if (s->path == NULL) { 117 if (s->path == NULL) {
118 return destroy(s), (gzFile)Z_NULL; 118 return destroy(s), (gzFile)Z_NULL;
119 } 119 }
120 strcpy(s->path, path); /* do this early for debugging */ 120 strcpy(s->path, path); /* do this early for debugging */
121 121
122 s->mode = '\0'; 122 s->mode = '\0';
123 do { 123 do {
124 if (*p == 'r') s->mode = 'r'; 124 if (*p == 'r') s->mode = 'r';
125 if (*p == 'w') s->mode = 'w'; 125 if (*p == 'w') s->mode = 'w';
126 } while (*p++); 126 } while (*p++);
127 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL; 127 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
128 128
129 if (s->mode == 'w') { 129 if (s->mode == 'w') {
130 err = deflateInit2(&(s->stream), Z_DEFAULT_COMPRESSION, 130 err = deflateInit2(&(s->stream), Z_DEFAULT_COMPRESSION,
131 DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0); 131 DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0);
132 /* windowBits is passed < 0 to suppress zlib header */ 132 /* windowBits is passed < 0 to suppress zlib header */
133 133
134 s->stream.next_out = s->outbuf = ALLOC(Z_BUFSIZE); 134 s->stream.next_out = s->outbuf = ALLOC(Z_BUFSIZE);
135 135
136 if (err != Z_OK || s->outbuf == Z_NULL) { 136 if (err != Z_OK || s->outbuf == Z_NULL) {
137 return destroy(s), (gzFile)Z_NULL; 137 return destroy(s), (gzFile)Z_NULL;
138 } 138 }
139 } else { 139 } else {
140 err = inflateInit2(&(s->stream), -MAX_WBITS); 140 err = inflateInit2(&(s->stream), -MAX_WBITS);
141 s->stream.next_in = s->inbuf = ALLOC(Z_BUFSIZE); 141 s->stream.next_in = s->inbuf = ALLOC(Z_BUFSIZE);
142 142
143 if (err != Z_OK || s->inbuf == Z_NULL) { 143 if (err != Z_OK || s->inbuf == Z_NULL) {
144 return destroy(s), (gzFile)Z_NULL; 144 return destroy(s), (gzFile)Z_NULL;
145 } 145 }
146 } 146 }
147 s->stream.avail_out = Z_BUFSIZE; 147 s->stream.avail_out = Z_BUFSIZE;
148 148
@@ -150,55 +150,55 @@ local gzFile gz_open (path, mode, fd)
150 s->file = fd < 0 ? FOPEN(path, mode) : fdopen(fd, mode); 150 s->file = fd < 0 ? FOPEN(path, mode) : fdopen(fd, mode);
151 151
152 if (s->file == NULL) { 152 if (s->file == NULL) {
153 return destroy(s), (gzFile)Z_NULL; 153 return destroy(s), (gzFile)Z_NULL;
154 } 154 }
155 if (s->mode == 'w') { 155 if (s->mode == 'w') {
156 /* Write a very simple .gz header: 156 /* Write a very simple .gz header:
157 */ 157 */
158 fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", GZ_MAGIC_1, GZ_MAGIC_2, 158 fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", GZ_MAGIC_1, GZ_MAGIC_2,
159 DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE); 159 DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
160 } else { 160 } else {
161 /* Check and skip the header: 161 /* Check and skip the header:
162 */ 162 */
163 Byte c1 = 0, c2 = 0; 163 Byte c1 = 0, c2 = 0;
164 Byte method = 0; 164 Byte method = 0;
165 Byte flags = 0; 165 Byte flags = 0;
166 Byte xflags = 0; 166 Byte xflags = 0;
167 Byte time[4]; 167 Byte time[4];
168 Byte osCode; 168 Byte osCode;
169 int c; 169 int c;
170 170
171 s->stream.avail_in = fread(s->inbuf, 1, 2, s->file); 171 s->stream.avail_in = fread(s->inbuf, 1, 2, s->file);
172 if (s->stream.avail_in != 2 || s->inbuf[0] != GZ_MAGIC_1 172 if (s->stream.avail_in != 2 || s->inbuf[0] != GZ_MAGIC_1
173 || s->inbuf[1] != GZ_MAGIC_2) { 173 || s->inbuf[1] != GZ_MAGIC_2) {
174 s->transparent = 1; 174 s->transparent = 1;
175 return (gzFile)s; 175 return (gzFile)s;
176 } 176 }
177 s->stream.avail_in = 0; 177 s->stream.avail_in = 0;
178 fscanf(s->file,"%c%c%4c%c%c", &method, &flags, time, &xflags, &osCode); 178 fscanf(s->file,"%c%c%4c%c%c", &method, &flags, time, &xflags, &osCode);
179 179
180 if (method != DEFLATED || feof(s->file) || (flags & RESERVED) != 0) { 180 if (method != DEFLATED || feof(s->file) || (flags & RESERVED) != 0) {
181 s->z_err = Z_DATA_ERROR; 181 s->z_err = Z_DATA_ERROR;
182 return (gzFile)s; 182 return (gzFile)s;
183 } 183 }
184 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */ 184 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
185 long len; 185 long len;
186 fscanf(s->file, "%c%c", &c1, &c2); 186 fscanf(s->file, "%c%c", &c1, &c2);
187 len = c1 + ((long)c2<<8); 187 len = c1 + ((long)c2<<8);
188 fseek(s->file, len, SEEK_CUR); 188 fseek(s->file, len, SEEK_CUR);
189 } 189 }
190 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */ 190 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
191 while ((c = getc(s->file)) != 0 && c != EOF) ; 191 while ((c = getc(s->file)) != 0 && c != EOF) ;
192 } 192 }
193 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */ 193 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
194 while ((c = getc(s->file)) != 0 && c != EOF) ; 194 while ((c = getc(s->file)) != 0 && c != EOF) ;
195 } 195 }
196 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */ 196 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
197 fscanf(s->file, "%c%c", &c1, &c2); 197 fscanf(s->file, "%c%c", &c1, &c2);
198 } 198 }
199 if (feof(s->file)) { 199 if (feof(s->file)) {
200 s->z_err = Z_DATA_ERROR; 200 s->z_err = Z_DATA_ERROR;
201 } 201 }
202 } 202 }
203 return (gzFile)s; 203 return (gzFile)s;
204} 204}
@@ -240,16 +240,16 @@ int gzread (file, buf, len)
240 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR; 240 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
241 241
242 if (s->transparent) { 242 if (s->transparent) {
243 unsigned n = 0; 243 unsigned n = 0;
244 Byte *b = (Byte*)buf; 244 Byte *b = (Byte*)buf;
245 /* Copy the first two (non-magic) bytes if not done already */ 245 /* Copy the first two (non-magic) bytes if not done already */
246 while (s->stream.avail_in > 0 && len > 0) { 246 while (s->stream.avail_in > 0 && len > 0) {
247 *b++ = *s->stream.next_in++; 247 *b++ = *s->stream.next_in++;
248 s->stream.avail_in--; 248 s->stream.avail_in--;
249 len--; n++; 249 len--; n++;
250 } 250 }
251 if (len == 0) return n; 251 if (len == 0) return n;
252 return n + fread(b, 1, len, s->file); 252 return n + fread(b, 1, len, s->file);
253 } 253 }
254 if (s->z_err == Z_DATA_ERROR) return -1; /* bad .gz file */ 254 if (s->z_err == Z_DATA_ERROR) return -1; /* bad .gz file */
255 if (s->z_err == Z_STREAM_END) return 0; /* don't read crc as data */ 255 if (s->z_err == Z_STREAM_END) return 0; /* don't read crc as data */
@@ -259,24 +259,24 @@ int gzread (file, buf, len)
259 259
260 while (s->stream.avail_out != 0) { 260 while (s->stream.avail_out != 0) {
261 261
262 if (s->stream.avail_in == 0 && !s->z_eof) { 262 if (s->stream.avail_in == 0 && !s->z_eof) {
263 263
264 errno = 0; 264 errno = 0;
265 s->stream.avail_in = 265 s->stream.avail_in =
266 fread(s->inbuf, 1, Z_BUFSIZE, s->file); 266 fread(s->inbuf, 1, Z_BUFSIZE, s->file);
267 if (s->stream.avail_in == 0) { 267 if (s->stream.avail_in == 0) {
268 s->z_eof = 1; 268 s->z_eof = 1;
269 } else if (s->stream.avail_in == (uInt)EOF) { 269 } else if (s->stream.avail_in == (uInt)EOF) {
270 s->stream.avail_in = 0; 270 s->stream.avail_in = 0;
271 s->z_eof = 1; 271 s->z_eof = 1;
272 s->z_err = Z_ERRNO; 272 s->z_err = Z_ERRNO;
273 break; 273 break;
274 } 274 }
275 s->stream.next_in = s->inbuf; 275 s->stream.next_in = s->inbuf;
276 } 276 }
277 s->z_err = inflate(&(s->stream), Z_NO_FLUSH); 277 s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
278 278
279 if (s->z_err == Z_STREAM_END || 279 if (s->z_err == Z_STREAM_END ||
280 s->z_err != Z_OK || s->z_eof) break; 280 s->z_err != Z_OK || s->z_eof) break;
281 } 281 }
282 len -= s->stream.avail_out; 282 len -= s->stream.avail_out;
@@ -302,18 +302,18 @@ int gzwrite (file, buf, len)
302 302
303 while (s->stream.avail_in != 0) { 303 while (s->stream.avail_in != 0) {
304 304
305 if (s->stream.avail_out == 0) { 305 if (s->stream.avail_out == 0) {
306 306
307 s->stream.next_out = s->outbuf; 307 s->stream.next_out = s->outbuf;
308 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) { 308 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
309 s->z_err = Z_ERRNO; 309 s->z_err = Z_ERRNO;
310 break; 310 break;
311 } 311 }
312 s->stream.avail_out = Z_BUFSIZE; 312 s->stream.avail_out = Z_BUFSIZE;
313 } 313 }
314 s->z_err = deflate(&(s->stream), Z_NO_FLUSH); 314 s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
315 315
316 if (s->z_err != Z_OK) break; 316 if (s->z_err != Z_OK) break;
317 } 317 }
318 s->crc = crc32(s->crc, buf, len); 318 s->crc = crc32(s->crc, buf, len);
319 319
@@ -339,25 +339,25 @@ int gzflush (file, flush)
339 s->stream.avail_in = 0; /* should be zero already anyway */ 339 s->stream.avail_in = 0; /* should be zero already anyway */
340 340
341 for (;;) { 341 for (;;) {
342 len = Z_BUFSIZE - s->stream.avail_out; 342 len = Z_BUFSIZE - s->stream.avail_out;
343 343
344 if (len != 0) { 344 if (len != 0) {
345 if (fwrite(s->outbuf, 1, len, s->file) != len) { 345 if (fwrite(s->outbuf, 1, len, s->file) != len) {
346 s->z_err = Z_ERRNO; 346 s->z_err = Z_ERRNO;
347 return Z_ERRNO; 347 return Z_ERRNO;
348 } 348 }
349 s->stream.next_out = s->outbuf; 349 s->stream.next_out = s->outbuf;
350 s->stream.avail_out = Z_BUFSIZE; 350 s->stream.avail_out = Z_BUFSIZE;
351 } 351 }
352 if (done) break; 352 if (done) break;
353 s->z_err = deflate(&(s->stream), flush); 353 s->z_err = deflate(&(s->stream), flush);
354 354
355 /* deflate has finished flushing only when it hasn't used up 355 /* deflate has finished flushing only when it hasn't used up
356 * all the available space in the output buffer: 356 * all the available space in the output buffer:
357 */ 357 */
358 done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END); 358 done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
359 359
360 if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break; 360 if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
361 } 361 }
362 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err; 362 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
363} 363}
@@ -371,8 +371,8 @@ local void putLong (file, x)
371{ 371{
372 int n; 372 int n;
373 for (n = 0; n < 4; n++) { 373 for (n = 0; n < 4; n++) {
374 fputc((int)(x & 0xff), file); 374 fputc((int)(x & 0xff), file);
375 x >>= 8; 375 x >>= 8;
376 } 376 }
377} 377}
378 378
@@ -386,8 +386,8 @@ local uLong getLong (buf)
386 Byte *p = buf+4; 386 Byte *p = buf+4;
387 387
388 do { 388 do {
389 x <<= 8; 389 x <<= 8;
390 x |= *--p; 390 x |= *--p;
391 } while (p != buf); 391 } while (p != buf);
392 return x; 392 return x;
393} 393}
@@ -406,31 +406,31 @@ int gzclose (file)
406 if (s == NULL) return Z_STREAM_ERROR; 406 if (s == NULL) return Z_STREAM_ERROR;
407 407
408 if (s->mode == 'w') { 408 if (s->mode == 'w') {
409 err = gzflush (file, Z_FINISH); 409 err = gzflush (file, Z_FINISH);
410 if (err != Z_OK) return destroy(file); 410 if (err != Z_OK) return destroy(file);
411 411
412 putLong (s->file, s->crc); 412 putLong (s->file, s->crc);
413 putLong (s->file, s->stream.total_in); 413 putLong (s->file, s->stream.total_in);
414 414
415 } else if (s->mode == 'r' && s->z_err == Z_STREAM_END) { 415 } else if (s->mode == 'r' && s->z_err == Z_STREAM_END) {
416 416
417 /* slide CRC and original size if they are at the end of inbuf */ 417 /* slide CRC and original size if they are at the end of inbuf */
418 if ((n = s->stream.avail_in) < 8 && !s->z_eof) { 418 if ((n = s->stream.avail_in) < 8 && !s->z_eof) {
419 Byte *p = s->inbuf; 419 Byte *p = s->inbuf;
420 Byte *q = s->stream.next_in; 420 Byte *q = s->stream.next_in;
421 while (n--) { *p++ = *q++; }; 421 while (n--) { *p++ = *q++; };
422 422
423 n = s->stream.avail_in; 423 n = s->stream.avail_in;
424 n += fread(p, 1, 8, s->file); 424 n += fread(p, 1, 8, s->file);
425 s->stream.next_in = s->inbuf; 425 s->stream.next_in = s->inbuf;
426 } 426 }
427 /* check CRC and original size */ 427 /* check CRC and original size */
428 if (n < 8 || 428 if (n < 8 ||
429 getLong(s->stream.next_in) != s->crc || 429 getLong(s->stream.next_in) != s->crc ||
430 getLong(s->stream.next_in + 4) != s->stream.total_out) { 430 getLong(s->stream.next_in + 4) != s->stream.total_out) {
431 431
432 s->z_err = Z_DATA_ERROR; 432 s->z_err = Z_DATA_ERROR;
433 } 433 }
434 } 434 }
435 return destroy(file); 435 return destroy(file);
436} 436}
@@ -450,8 +450,8 @@ char* gzerror (file, errnum)
450 gz_stream *s = (gz_stream*)file; 450 gz_stream *s = (gz_stream*)file;
451 451
452 if (s == NULL) { 452 if (s == NULL) {
453 *errnum = Z_STREAM_ERROR; 453 *errnum = Z_STREAM_ERROR;
454 return z_errmsg[1-Z_STREAM_ERROR]; 454 return z_errmsg[1-Z_STREAM_ERROR];
455 } 455 }
456 *errnum = s->z_err; 456 *errnum = s->z_err;
457 if (*errnum == Z_OK) return ""; 457 if (*errnum == Z_OK) return "";
diff --git a/infblock.c b/infblock.c
index 538fa62..3a9cf85 100644
--- a/infblock.c
+++ b/infblock.c
@@ -12,7 +12,7 @@
12struct inflate_codes_state {int dummy;}; /* for buggy compilers */ 12struct inflate_codes_state {int dummy;}; /* for buggy compilers */
13 13
14/* Table for deflate from PKZIP's appnote.txt. */ 14/* Table for deflate from PKZIP's appnote.txt. */
15local uInt border[] = { /* Order of the bit length code lengths */ 15local uInt border[] = { /* Order of the bit length code lengths */
16 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; 16 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
17 17
18/* 18/*
@@ -111,13 +111,13 @@ struct inflate_blocks_state *s;
111z_stream *z; 111z_stream *z;
112int r; 112int r;
113{ 113{
114 uInt t; /* temporary storage */ 114 uInt t; /* temporary storage */
115 uLong b; /* bit buffer */ 115 uLong b; /* bit buffer */
116 uInt k; /* bits in bit buffer */ 116 uInt k; /* bits in bit buffer */
117 Byte *p; /* input data pointer */ 117 Byte *p; /* input data pointer */
118 uInt n; /* bytes available there */ 118 uInt n; /* bytes available there */
119 Byte *q; /* output window write pointer */ 119 Byte *q; /* output window write pointer */
120 uInt m; /* bytes to end of window or read pointer */ 120 uInt m; /* bytes to end of window or read pointer */
121 121
122 /* copy input/output information to locals (UPDATE macro restores) */ 122 /* copy input/output information to locals (UPDATE macro restores) */
123 LOAD 123 LOAD
@@ -131,44 +131,44 @@ int r;
131 s->last = t & 1; 131 s->last = t & 1;
132 switch (t >> 1) 132 switch (t >> 1)
133 { 133 {
134 case 0: /* stored */ 134 case 0: /* stored */
135 Trace((stderr, "inflate: stored block%s\n", 135 Trace((stderr, "inflate: stored block%s\n",
136 s->last ? " (last)" : "")); 136 s->last ? " (last)" : ""));
137 DUMPBITS(3) 137 DUMPBITS(3)
138 t = k & 7; /* go to byte boundary */ 138 t = k & 7; /* go to byte boundary */
139 DUMPBITS(t) 139 DUMPBITS(t)
140 s->mode = LENS; /* get length of stored block */ 140 s->mode = LENS; /* get length of stored block */
141 break; 141 break;
142 case 1: /* fixed */ 142 case 1: /* fixed */
143 Trace((stderr, "inflate: fixed codes block%s\n", 143 Trace((stderr, "inflate: fixed codes block%s\n",
144 s->last ? " (last)" : "")); 144 s->last ? " (last)" : ""));
145 { 145 {
146 uInt bl, bd; 146 uInt bl, bd;
147 inflate_huft *tl, *td; 147 inflate_huft *tl, *td;
148 148
149 inflate_trees_fixed(&bl, &bd, &tl, &td); 149 inflate_trees_fixed(&bl, &bd, &tl, &td);
150 s->sub.codes = inflate_codes_new(bl, bd, tl, td, z); 150 s->sub.codes = inflate_codes_new(bl, bd, tl, td, z);
151 if (s->sub.codes == Z_NULL) 151 if (s->sub.codes == Z_NULL)
152 { 152 {
153 r = Z_MEM_ERROR; 153 r = Z_MEM_ERROR;
154 LEAVE 154 LEAVE
155 } 155 }
156 } 156 }
157 DUMPBITS(3) 157 DUMPBITS(3)
158 s->mode = CODES; 158 s->mode = CODES;
159 break; 159 break;
160 case 2: /* dynamic */ 160 case 2: /* dynamic */
161 Trace((stderr, "inflate: dynamic codes block%s\n", 161 Trace((stderr, "inflate: dynamic codes block%s\n",
162 s->last ? " (last)" : "")); 162 s->last ? " (last)" : ""));
163 DUMPBITS(3) 163 DUMPBITS(3)
164 s->mode = TABLE; 164 s->mode = TABLE;
165 break; 165 break;
166 case 3: /* illegal */ 166 case 3: /* illegal */
167 DUMPBITS(3) 167 DUMPBITS(3)
168 s->mode = BAD; 168 s->mode = BAD;
169 z->msg = "invalid block type"; 169 z->msg = "invalid block type";
170 r = Z_DATA_ERROR; 170 r = Z_DATA_ERROR;
171 LEAVE 171 LEAVE
172 } 172 }
173 break; 173 break;
174 case LENS: 174 case LENS:
@@ -176,24 +176,30 @@ int r;
176 if ((~b) >> 16 != (b & 0xffff)) 176 if ((~b) >> 16 != (b & 0xffff))
177 { 177 {
178 s->mode = BAD; 178 s->mode = BAD;
179 z->msg = "invalid stored block lengths"; 179 z->msg = "invalid stored block lengths";
180 r = Z_DATA_ERROR; 180 r = Z_DATA_ERROR;
181 LEAVE 181 LEAVE
182 } 182 }
183 k = 0; /* dump bits */
184 s->sub.left = (uInt)b & 0xffff; 183 s->sub.left = (uInt)b & 0xffff;
184 k = b = 0; /* dump bits */
185 Tracev((stderr, "inflate: stored length %u\n", s->sub.left)); 185 Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
186 s->mode = s->sub.left ? STORED : TYPE; 186 s->mode = s->sub.left ? STORED : TYPE;
187 break; 187 break;
188 case STORED: 188 case STORED:
189 do { 189 if (n == 0)
190 NEEDBYTE 190 LEAVE
191 NEEDOUT 191 NEEDOUT
192 OUTBYTE(NEXTBYTE) 192 t = s->sub.left;
193 } while (--s->sub.left); 193 if (t > n) t = n;
194 if (t > m) t = m;
195 zmemcpy(q, p, t);
196 p += t; n -= t;
197 q += t; m -= t;
198 if ((s->sub.left -= t) != 0)
199 break;
194 Tracev((stderr, "inflate: stored end, %lu total out\n", 200 Tracev((stderr, "inflate: stored end, %lu total out\n",
195 z->total_out + (q >= s->read ? q - s->read : 201 z->total_out + (q >= s->read ? q - s->read :
196 (s->end - s->read) + (q - s->window)))); 202 (s->end - s->read) + (q - s->window))));
197 s->mode = s->last ? DRY : TYPE; 203 s->mode = s->last ? DRY : TYPE;
198 break; 204 break;
199 case TABLE: 205 case TABLE:
@@ -204,8 +210,8 @@ int r;
204 { 210 {
205 s->mode = BAD; 211 s->mode = BAD;
206 z->msg = "too many length or distance symbols"; 212 z->msg = "too many length or distance symbols";
207 r = Z_DATA_ERROR; 213 r = Z_DATA_ERROR;
208 LEAVE 214 LEAVE
209 } 215 }
210#endif 216#endif
211 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f); 217 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
@@ -214,7 +220,7 @@ int r;
214 if ((s->sub.trees.blens = (uInt*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL) 220 if ((s->sub.trees.blens = (uInt*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
215 { 221 {
216 r = Z_MEM_ERROR; 222 r = Z_MEM_ERROR;
217 LEAVE 223 LEAVE
218 } 224 }
219 DUMPBITS(14) 225 DUMPBITS(14)
220 s->sub.trees.index = 0; 226 s->sub.trees.index = 0;
@@ -224,118 +230,118 @@ int r;
224 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10)) 230 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
225 { 231 {
226 NEEDBITS(3) 232 NEEDBITS(3)
227 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7; 233 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
228 DUMPBITS(3) 234 DUMPBITS(3)
229 } 235 }
230 while (s->sub.trees.index < 19) 236 while (s->sub.trees.index < 19)
231 s->sub.trees.blens[border[s->sub.trees.index++]] = 0; 237 s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
232 s->sub.trees.bb = 7; 238 s->sub.trees.bb = 7;
233 t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb, 239 t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
234 &s->sub.trees.tb, z); 240 &s->sub.trees.tb, z);
235 if (t != Z_OK) 241 if (t != Z_OK)
236 { 242 {
237 r = t; 243 r = t;
238 if (r == Z_DATA_ERROR) 244 if (r == Z_DATA_ERROR)
239 s->mode = BAD; 245 s->mode = BAD;
240 LEAVE 246 LEAVE
241 } 247 }
242 s->sub.trees.index = 0; 248 s->sub.trees.index = 0;
243 Tracev((stderr, "inflate: bits tree ok\n")); 249 Tracev((stderr, "inflate: bits tree ok\n"));
244 s->mode = DTREE; 250 s->mode = DTREE;
245 case DTREE: 251 case DTREE:
246 while (t = s->sub.trees.table, 252 while (t = s->sub.trees.table,
247 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f)) 253 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
248 { 254 {
249 inflate_huft *h; 255 inflate_huft *h;
250 uInt i, j, c; 256 uInt i, j, c;
251 257
252 t = s->sub.trees.bb; 258 t = s->sub.trees.bb;
253 NEEDBITS(t) 259 NEEDBITS(t)
254 h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]); 260 h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
255 t = h->word.what.Bits; 261 t = h->word.what.Bits;
256 c = h->more.Base; 262 c = h->more.Base;
257 if (c < 16) 263 if (c < 16)
258 { 264 {
259 DUMPBITS(t) 265 DUMPBITS(t)
260 s->sub.trees.blens[s->sub.trees.index++] = c; 266 s->sub.trees.blens[s->sub.trees.index++] = c;
261 } 267 }
262 else /* c == 16..18 */ 268 else /* c == 16..18 */
263 { 269 {
264 i = c == 18 ? 7 : c - 14; 270 i = c == 18 ? 7 : c - 14;
265 j = c == 18 ? 11 : 3; 271 j = c == 18 ? 11 : 3;
266 NEEDBITS(t + i) 272 NEEDBITS(t + i)
267 DUMPBITS(t) 273 DUMPBITS(t)
268 j += (uInt)b & inflate_mask[i]; 274 j += (uInt)b & inflate_mask[i];
269 DUMPBITS(i) 275 DUMPBITS(i)
270 i = s->sub.trees.index; 276 i = s->sub.trees.index;
271 t = s->sub.trees.table; 277 t = s->sub.trees.table;
272 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || 278 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
273 (c == 16 && i < 1)) 279 (c == 16 && i < 1))
274 { 280 {
275 s->mode = BAD; 281 s->mode = BAD;
276 z->msg = "invalid bit length repeat"; 282 z->msg = "invalid bit length repeat";
277 r = Z_DATA_ERROR; 283 r = Z_DATA_ERROR;
278 LEAVE 284 LEAVE
279 } 285 }
280 c = c == 16 ? s->sub.trees.blens[i - 1] : 0; 286 c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
281 do { 287 do {
282 s->sub.trees.blens[i++] = c; 288 s->sub.trees.blens[i++] = c;
283 } while (--j); 289 } while (--j);
284 s->sub.trees.index = i; 290 s->sub.trees.index = i;
285 } 291 }
286 } 292 }
287 inflate_trees_free(s->sub.trees.tb, z); 293 inflate_trees_free(s->sub.trees.tb, z);
288 s->sub.trees.tb = Z_NULL; 294 s->sub.trees.tb = Z_NULL;
289 { 295 {
290 uInt bl, bd; 296 uInt bl, bd;
291 inflate_huft *tl, *td; 297 inflate_huft *tl, *td;
292 struct inflate_codes_state *c; 298 struct inflate_codes_state *c;
293 299
294 bl = 9; 300 bl = 9;
295 bd = 6; 301 bd = 6;
296 t = s->sub.trees.table; 302 t = s->sub.trees.table;
297 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), 303 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
298 s->sub.trees.blens, &bl, &bd, &tl, &td, z); 304 s->sub.trees.blens, &bl, &bd, &tl, &td, z);
299 if (t != Z_OK) 305 if (t != Z_OK)
300 { 306 {
301 if (t == (uInt)Z_DATA_ERROR) 307 if (t == (uInt)Z_DATA_ERROR)
302 s->mode = BAD; 308 s->mode = BAD;
303 r = t; 309 r = t;
304 LEAVE 310 LEAVE
305 } 311 }
306 Tracev((stderr, "inflate: trees ok\n")); 312 Tracev((stderr, "inflate: trees ok\n"));
307 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL) 313 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
308 { 314 {
309 inflate_trees_free(td, z); 315 inflate_trees_free(td, z);
310 inflate_trees_free(tl, z); 316 inflate_trees_free(tl, z);
311 r = Z_MEM_ERROR; 317 r = Z_MEM_ERROR;
312 LEAVE 318 LEAVE
313 } 319 }
314 ZFREE(z, s->sub.trees.blens); 320 ZFREE(z, s->sub.trees.blens);
315 s->sub.codes = c; 321 s->sub.codes = c;
316 } 322 }
317 s->mode = CODES; 323 s->mode = CODES;
318 case CODES: 324 case CODES:
319 UPDATE 325 UPDATE
320 if ((r = inflate_codes(s, z, r)) != Z_STREAM_END) 326 if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
321 return inflate_flush(s, z, r); 327 return inflate_flush(s, z, r);
322 r = Z_OK; 328 r = Z_OK;
323 inflate_codes_free(s->sub.codes, z); 329 inflate_codes_free(s->sub.codes, z);
324 LOAD 330 LOAD
325 Tracev((stderr, "inflate: codes end, %lu total out\n", 331 Tracev((stderr, "inflate: codes end, %lu total out\n",
326 z->total_out + (q >= s->read ? q - s->read : 332 z->total_out + (q >= s->read ? q - s->read :
327 (s->end - s->read) + (q - s->window)))); 333 (s->end - s->read) + (q - s->window))));
328 if (!s->last) 334 if (!s->last)
329 { 335 {
330 s->mode = TYPE; 336 s->mode = TYPE;
331 break; 337 break;
332 } 338 }
333 if (k > 7) /* return unused byte, if any */ 339 if (k > 7) /* return unused byte, if any */
334 { 340 {
335 Assert(k < 16, "inflate_codes grabbed too many bytes") 341 Assert(k < 16, "inflate_codes grabbed too many bytes")
336 k -= 8; 342 k -= 8;
337 n++; 343 n++;
338 p--; /* can always return one */ 344 p--; /* can always return one */
339 } 345 }
340 s->mode = DRY; 346 s->mode = DRY;
341 case DRY: 347 case DRY:
diff --git a/infblock.h b/infblock.h
index 33e1696..c0712c1 100644
--- a/infblock.h
+++ b/infblock.h
@@ -18,7 +18,7 @@ extern struct inflate_blocks_state * inflate_blocks_new __P((
18extern int inflate_blocks __P(( 18extern int inflate_blocks __P((
19 struct inflate_blocks_state *, 19 struct inflate_blocks_state *,
20 z_stream *, 20 z_stream *,
21 int)); /* initial return code */ 21 int)); /* initial return code */
22 22
23extern void inflate_blocks_reset __P(( 23extern void inflate_blocks_reset __P((
24 struct inflate_blocks_state *, 24 struct inflate_blocks_state *,
diff --git a/infcodes.c b/infcodes.c
index fc56547..87e661b 100644
--- a/infcodes.c
+++ b/infcodes.c
@@ -19,38 +19,38 @@
19struct inflate_codes_state { 19struct inflate_codes_state {
20 20
21 /* mode */ 21 /* mode */
22 enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ 22 enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
23 START, /* x: set up for LEN */ 23 START, /* x: set up for LEN */
24 LEN, /* i: get length/literal/eob next */ 24 LEN, /* i: get length/literal/eob next */
25 LENEXT, /* i: getting length extra (have base) */ 25 LENEXT, /* i: getting length extra (have base) */
26 DIST, /* i: get distance next */ 26 DIST, /* i: get distance next */
27 DISTEXT, /* i: getting distance extra */ 27 DISTEXT, /* i: getting distance extra */
28 COPY, /* o: copying bytes in window, waiting for space */ 28 COPY, /* o: copying bytes in window, waiting for space */
29 LIT, /* o: got literal, waiting for output space */ 29 LIT, /* o: got literal, waiting for output space */
30 WASH, /* o: got eob, possibly still output waiting */ 30 WASH, /* o: got eob, possibly still output waiting */
31 END, /* x: got eob and all data flushed */ 31 END, /* x: got eob and all data flushed */
32 BADCODE} /* x: got error */ 32 BADCODE} /* x: got error */
33 mode; /* current inflate_codes mode */ 33 mode; /* current inflate_codes mode */
34 34
35 /* mode dependent information */ 35 /* mode dependent information */
36 uInt len; 36 uInt len;
37 union { 37 union {
38 struct { 38 struct {
39 inflate_huft *tree; /* pointer into tree */ 39 inflate_huft *tree; /* pointer into tree */
40 uInt need; /* bits needed */ 40 uInt need; /* bits needed */
41 } code; /* if LEN or DIST, where in tree */ 41 } code; /* if LEN or DIST, where in tree */
42 uInt lit; /* if LIT, literal */ 42 uInt lit; /* if LIT, literal */
43 struct { 43 struct {
44 uInt get; /* bits to get for extra */ 44 uInt get; /* bits to get for extra */
45 uInt dist; /* distance back to copy from */ 45 uInt dist; /* distance back to copy from */
46 } copy; /* if EXT or COPY, where and how much */ 46 } copy; /* if EXT or COPY, where and how much */
47 } sub; /* submode */ 47 } sub; /* submode */
48 48
49 /* mode independent information */ 49 /* mode independent information */
50 Byte lbits; /* ltree bits decoded per branch */ 50 Byte lbits; /* ltree bits decoded per branch */
51 Byte dbits; /* dtree bits decoder per branch */ 51 Byte dbits; /* dtree bits decoder per branch */
52 inflate_huft *ltree; /* literal/length/eob tree */ 52 inflate_huft *ltree; /* literal/length/eob tree */
53 inflate_huft *dtree; /* distance tree */ 53 inflate_huft *dtree; /* distance tree */
54 54
55}; 55};
56 56
@@ -81,79 +81,79 @@ struct inflate_blocks_state *s;
81z_stream *z; 81z_stream *z;
82int r; 82int r;
83{ 83{
84 uInt j; /* temporary storage */ 84 uInt j; /* temporary storage */
85 inflate_huft *t; /* temporary pointer */ 85 inflate_huft *t; /* temporary pointer */
86 int e; /* extra bits or operation */ 86 int e; /* extra bits or operation */
87 uLong b; /* bit buffer */ 87 uLong b; /* bit buffer */
88 uInt k; /* bits in bit buffer */ 88 uInt k; /* bits in bit buffer */
89 Byte *p; /* input data pointer */ 89 Byte *p; /* input data pointer */
90 uInt n; /* bytes available there */ 90 uInt n; /* bytes available there */
91 Byte *q; /* output window write pointer */ 91 Byte *q; /* output window write pointer */
92 uInt m; /* bytes to end of window or read pointer */ 92 uInt m; /* bytes to end of window or read pointer */
93 Byte *f; /* pointer to copy strings from */ 93 Byte *f; /* pointer to copy strings from */
94 struct inflate_codes_state *c = s->sub.codes; /* codes state */ 94 struct inflate_codes_state *c = s->sub.codes; /* codes state */
95 95
96 /* copy input/output information to locals (UPDATE macro restores) */ 96 /* copy input/output information to locals (UPDATE macro restores) */
97 LOAD 97 LOAD
98 98
99 /* process input and output based on current state */ 99 /* process input and output based on current state */
100 while (1) switch (c->mode) 100 while (1) switch (c->mode)
101 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ 101 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
102 case START: /* x: set up for LEN */ 102 case START: /* x: set up for LEN */
103#ifndef SLOW 103#ifndef SLOW
104 if (m >= 258 && n >= 10) 104 if (m >= 258 && n >= 10)
105 { 105 {
106 UPDATE 106 UPDATE
107 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z); 107 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
108 LOAD 108 LOAD
109 if (r != Z_OK) 109 if (r != Z_OK)
110 { 110 {
111 c->mode = r == Z_STREAM_END ? WASH : BADCODE; 111 c->mode = r == Z_STREAM_END ? WASH : BADCODE;
112 break; 112 break;
113 } 113 }
114 } 114 }
115#endif /* !SLOW */ 115#endif /* !SLOW */
116 c->sub.code.need = c->lbits; 116 c->sub.code.need = c->lbits;
117 c->sub.code.tree = c->ltree; 117 c->sub.code.tree = c->ltree;
118 c->mode = LEN; 118 c->mode = LEN;
119 case LEN: /* i: get length/literal/eob next */ 119 case LEN: /* i: get length/literal/eob next */
120 j = c->sub.code.need; 120 j = c->sub.code.need;
121 NEEDBITS(j) 121 NEEDBITS(j)
122 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); 122 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
123 DUMPBITS(t->bits) 123 DUMPBITS(t->bits)
124 if ((e = (int)(t->exop)) < 0) 124 if ((e = (int)(t->exop)) < 0)
125 { 125 {
126 if (e == -128) /* invalid code */ 126 if (e == -128) /* invalid code */
127 { 127 {
128 c->mode = BADCODE; 128 c->mode = BADCODE;
129 z->msg = "invalid literal/length code"; 129 z->msg = "invalid literal/length code";
130 r = Z_DATA_ERROR; 130 r = Z_DATA_ERROR;
131 LEAVE 131 LEAVE
132 } 132 }
133 e = -e; 133 e = -e;
134 if (e & 64) /* end of block */ 134 if (e & 64) /* end of block */
135 { 135 {
136 Tracevv((stderr, "inflate: end of block\n")); 136 Tracevv((stderr, "inflate: end of block\n"));
137 c->mode = WASH; 137 c->mode = WASH;
138 break; 138 break;
139 } 139 }
140 c->sub.code.need = e; 140 c->sub.code.need = e;
141 c->sub.code.tree = t->next; 141 c->sub.code.tree = t->next;
142 break; 142 break;
143 } 143 }
144 if (e & 16) /* literal */ 144 if (e & 16) /* literal */
145 { 145 {
146 c->sub.lit = t->base; 146 c->sub.lit = t->base;
147 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? 147 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
148 "inflate: literal '%c'\n" : 148 "inflate: literal '%c'\n" :
149 "inflate: literal 0x%02x\n", t->base)); 149 "inflate: literal 0x%02x\n", t->base));
150 c->mode = LIT; 150 c->mode = LIT;
151 break; 151 break;
152 } 152 }
153 c->sub.copy.get = e; 153 c->sub.copy.get = e;
154 c->len = t->base; 154 c->len = t->base;
155 c->mode = LENEXT; 155 c->mode = LENEXT;
156 case LENEXT: /* i: getting length extra (have base) */ 156 case LENEXT: /* i: getting length extra (have base) */
157 j = c->sub.copy.get; 157 j = c->sub.copy.get;
158 NEEDBITS(j) 158 NEEDBITS(j)
159 c->len += (uInt)b & inflate_mask[j]; 159 c->len += (uInt)b & inflate_mask[j];
@@ -162,7 +162,7 @@ int r;
162 c->sub.code.tree = c->dtree; 162 c->sub.code.tree = c->dtree;
163 Tracevv((stderr, "inflate: length %u\n", c->len)); 163 Tracevv((stderr, "inflate: length %u\n", c->len));
164 c->mode = DIST; 164 c->mode = DIST;
165 case DIST: /* i: get distance next */ 165 case DIST: /* i: get distance next */
166 j = c->sub.code.need; 166 j = c->sub.code.need;
167 NEEDBITS(j) 167 NEEDBITS(j)
168 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); 168 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
@@ -170,46 +170,46 @@ int r;
170 if ((e = (int)(t->exop)) < 0) 170 if ((e = (int)(t->exop)) < 0)
171 { 171 {
172 if (e == -128) 172 if (e == -128)
173 { 173 {
174 c->mode = BADCODE; 174 c->mode = BADCODE;
175 z->msg = "invalid distance code"; 175 z->msg = "invalid distance code";
176 r = Z_DATA_ERROR; 176 r = Z_DATA_ERROR;
177 LEAVE 177 LEAVE
178 } 178 }
179 c->sub.code.need = -e; 179 c->sub.code.need = -e;
180 c->sub.code.tree = t->next; 180 c->sub.code.tree = t->next;
181 break; 181 break;
182 } 182 }
183 c->sub.copy.dist = t->base; 183 c->sub.copy.dist = t->base;
184 c->sub.copy.get = e; 184 c->sub.copy.get = e;
185 c->mode = DISTEXT; 185 c->mode = DISTEXT;
186 case DISTEXT: /* i: getting distance extra */ 186 case DISTEXT: /* i: getting distance extra */
187 j = c->sub.copy.get; 187 j = c->sub.copy.get;
188 NEEDBITS(j) 188 NEEDBITS(j)
189 c->sub.copy.dist += (uInt)b & inflate_mask[j]; 189 c->sub.copy.dist += (uInt)b & inflate_mask[j];
190 DUMPBITS(j) 190 DUMPBITS(j)
191 Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist)); 191 Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
192 c->mode = COPY; 192 c->mode = COPY;
193 case COPY: /* o: copying bytes in window, waiting for space */ 193 case COPY: /* o: copying bytes in window, waiting for space */
194 f = (uInt)(q - s->window) < c->sub.copy.dist ? 194 f = (uInt)(q - s->window) < c->sub.copy.dist ?
195 s->end - (c->sub.copy.dist - (q - s->window)) : 195 s->end - (c->sub.copy.dist - (q - s->window)) :
196 q - c->sub.copy.dist; 196 q - c->sub.copy.dist;
197 while (c->len) 197 while (c->len)
198 { 198 {
199 NEEDOUT 199 NEEDOUT
200 OUTBYTE(*f++) 200 OUTBYTE(*f++)
201 if (f == s->end) 201 if (f == s->end)
202 f = s->window; 202 f = s->window;
203 c->len--; 203 c->len--;
204 } 204 }
205 c->mode = START; 205 c->mode = START;
206 break; 206 break;
207 case LIT: /* o: got literal, waiting for output space */ 207 case LIT: /* o: got literal, waiting for output space */
208 NEEDOUT 208 NEEDOUT
209 OUTBYTE(c->sub.lit) 209 OUTBYTE(c->sub.lit)
210 c->mode = START; 210 c->mode = START;
211 break; 211 break;
212 case WASH: /* o: got eob, possibly more output */ 212 case WASH: /* o: got eob, possibly more output */
213 FLUSH 213 FLUSH
214 if (s->read != s->write) 214 if (s->read != s->write)
215 LEAVE 215 LEAVE
@@ -217,7 +217,7 @@ int r;
217 case END: 217 case END:
218 r = Z_STREAM_END; 218 r = Z_STREAM_END;
219 LEAVE 219 LEAVE
220 case BADCODE: /* x: got error */ 220 case BADCODE: /* x: got error */
221 r = Z_DATA_ERROR; 221 r = Z_DATA_ERROR;
222 LEAVE 222 LEAVE
223 default: 223 default:
diff --git a/inffast.c b/inffast.c
index 980a925..2fd707e 100644
--- a/inffast.c
+++ b/inffast.c
@@ -35,19 +35,19 @@ inflate_huft *tl, *td;
35struct inflate_blocks_state *s; 35struct inflate_blocks_state *s;
36z_stream *z; 36z_stream *z;
37{ 37{
38 inflate_huft *t; /* temporary pointer */ 38 inflate_huft *t; /* temporary pointer */
39 int e; /* extra bits or operation */ 39 int e; /* extra bits or operation */
40 uLong b; /* bit buffer */ 40 uLong b; /* bit buffer */
41 uInt k; /* bits in bit buffer */ 41 uInt k; /* bits in bit buffer */
42 Byte *p; /* input data pointer */ 42 Byte *p; /* input data pointer */
43 uInt n; /* bytes available there */ 43 uInt n; /* bytes available there */
44 Byte *q; /* output window write pointer */ 44 Byte *q; /* output window write pointer */
45 uInt m; /* bytes to end of window or read pointer */ 45 uInt m; /* bytes to end of window or read pointer */
46 uInt ml; /* mask for literal/length tree */ 46 uInt ml; /* mask for literal/length tree */
47 uInt md; /* mask for distance tree */ 47 uInt md; /* mask for distance tree */
48 uInt c; /* bytes to copy */ 48 uInt c; /* bytes to copy */
49 uInt d; /* distance back to copy from */ 49 uInt d; /* distance back to copy from */
50 Byte *r; /* copy source pointer */ 50 Byte *r; /* copy source pointer */
51 51
52 /* load input, output, bit values */ 52 /* load input, output, bit values */
53 LOAD 53 LOAD
@@ -57,40 +57,40 @@ z_stream *z;
57 md = inflate_mask[bd]; 57 md = inflate_mask[bd];
58 58
59 /* do until not enough input or output space for fast loop */ 59 /* do until not enough input or output space for fast loop */
60 do { /* assume called with m >= 258 && n >= 10 */ 60 do { /* assume called with m >= 258 && n >= 10 */
61 /* get literal/length code */ 61 /* get literal/length code */
62 GRABBITS(20) /* max bits for literal/length code */ 62 GRABBITS(20) /* max bits for literal/length code */
63 if ((e = (t = tl + ((uInt)b & ml))->exop) < 0) 63 if ((e = (t = tl + ((uInt)b & ml))->exop) < 0)
64 do { 64 do {
65 if (e == -128) 65 if (e == -128)
66 { 66 {
67 z->msg = "invalid literal/length code"; 67 z->msg = "invalid literal/length code";
68 UNGRAB 68 UNGRAB
69 UPDATE 69 UPDATE
70 return Z_DATA_ERROR; 70 return Z_DATA_ERROR;
71 } 71 }
72 DUMPBITS(t->bits) 72 DUMPBITS(t->bits)
73 e = -e; 73 e = -e;
74 if (e & 64) /* end of block */ 74 if (e & 64) /* end of block */
75 { 75 {
76 Tracevv((stderr, "inflate: * end of block\n")); 76 Tracevv((stderr, "inflate: * end of block\n"));
77 UNGRAB 77 UNGRAB
78 UPDATE 78 UPDATE
79 return Z_STREAM_END; 79 return Z_STREAM_END;
80 } 80 }
81 } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0); 81 } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0);
82 DUMPBITS(t->bits) 82 DUMPBITS(t->bits)
83 83
84 /* process literal or length (end of block already trapped) */ 84 /* process literal or length (end of block already trapped) */
85 if (e & 16) /* then it's a literal */ 85 if (e & 16) /* then it's a literal */
86 { 86 {
87 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? 87 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
88 "inflate: * literal '%c'\n" : 88 "inflate: * literal '%c'\n" :
89 "inflate: * literal 0x%02x\n", t->base)); 89 "inflate: * literal 0x%02x\n", t->base));
90 *q++ = (Byte)t->base; 90 *q++ = (Byte)t->base;
91 m--; 91 m--;
92 } 92 }
93 else /* it's a length */ 93 else /* it's a length */
94 { 94 {
95 /* get length of block to copy (already have extra bits) */ 95 /* get length of block to copy (already have extra bits) */
96 c = t->base + ((uInt)b & inflate_mask[e]); 96 c = t->base + ((uInt)b & inflate_mask[e]);
@@ -98,53 +98,53 @@ z_stream *z;
98 Tracevv((stderr, "inflate: * length %u\n", c)); 98 Tracevv((stderr, "inflate: * length %u\n", c));
99 99
100 /* decode distance base of block to copy */ 100 /* decode distance base of block to copy */
101 GRABBITS(15); /* max bits for distance code */ 101 GRABBITS(15); /* max bits for distance code */
102 if ((e = (t = td + ((uInt)b & md))->exop) < 0) 102 if ((e = (t = td + ((uInt)b & md))->exop) < 0)
103 do { 103 do {
104 if (e == -128) 104 if (e == -128)
105 { 105 {
106 z->msg = "invalid distance code"; 106 z->msg = "invalid distance code";
107 UNGRAB 107 UNGRAB
108 UPDATE 108 UPDATE
109 return Z_DATA_ERROR; 109 return Z_DATA_ERROR;
110 } 110 }
111 DUMPBITS(t->bits) 111 DUMPBITS(t->bits)
112 e = -e; 112 e = -e;
113 } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0); 113 } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0);
114 DUMPBITS(t->bits) 114 DUMPBITS(t->bits)
115 115
116 /* get extra bits to add to distance base */ 116 /* get extra bits to add to distance base */
117 GRABBITS((uInt)e) /* get extra bits (up to 13) */ 117 GRABBITS((uInt)e) /* get extra bits (up to 13) */
118 d = t->base + ((uInt)b & inflate_mask[e]); 118 d = t->base + ((uInt)b & inflate_mask[e]);
119 DUMPBITS(e) 119 DUMPBITS(e)
120 Tracevv((stderr, "inflate: * distance %u\n", d)); 120 Tracevv((stderr, "inflate: * distance %u\n", d));
121 121
122 /* do the copy */ 122 /* do the copy */
123 m -= c; 123 m -= c;
124 if ((uInt)(q - s->window) >= d) /* if offset before destination, */ 124 if ((uInt)(q - s->window) >= d) /* if offset before destination, */
125 { /* just copy */ 125 { /* just copy */
126 r = q - d; 126 r = q - d;
127 *q++ = *r++; c--; /* minimum count is three, */ 127 *q++ = *r++; c--; /* minimum count is three, */
128 *q++ = *r++; c--; /* so unroll loop a little */ 128 *q++ = *r++; c--; /* so unroll loop a little */
129 do { 129 do {
130 *q++ = *r++; 130 *q++ = *r++;
131 } while (--c); 131 } while (--c);
132 } 132 }
133 else /* else offset after destination */ 133 else /* else offset after destination */
134 { 134 {
135 e = d - (q - s->window); /* bytes from offset to end */ 135 e = d - (q - s->window); /* bytes from offset to end */
136 r = s->end - e; /* pointer to offset */ 136 r = s->end - e; /* pointer to offset */
137 if (c > (uInt)e) /* if source crosses, */ 137 if (c > (uInt)e) /* if source crosses, */
138 { 138 {
139 c -= e; /* copy to end of window */ 139 c -= e; /* copy to end of window */
140 do { 140 do {
141 *q++ = *r++; 141 *q++ = *r++;
142 } while (--e); 142 } while (--e);
143 r = s->window; /* copy rest from start of window */ 143 r = s->window; /* copy rest from start of window */
144 } 144 }
145 do { /* copy all or what's left */ 145 do { /* copy all or what's left */
146 *q++ = *r++; 146 *q++ = *r++;
147 } while (--c); 147 } while (--c);
148 } 148 }
149 } 149 }
150 } while (m >= 258 && n >= 10); 150 } while (m >= 258 && n >= 10);
diff --git a/inflate.c b/inflate.c
index 0b0ee87..1c58d98 100644
--- a/inflate.c
+++ b/inflate.c
@@ -13,32 +13,32 @@ struct internal_state {
13 13
14 /* mode */ 14 /* mode */
15 enum { 15 enum {
16 METHOD, /* waiting for method byte */ 16 METHOD, /* waiting for method byte */
17 FLAG, /* waiting for flag byte */ 17 FLAG, /* waiting for flag byte */
18 BLOCKS, /* decompressing blocks */ 18 BLOCKS, /* decompressing blocks */
19 CHECK4, /* four check bytes to go */ 19 CHECK4, /* four check bytes to go */
20 CHECK3, /* three check bytes to go */ 20 CHECK3, /* three check bytes to go */
21 CHECK2, /* two check bytes to go */ 21 CHECK2, /* two check bytes to go */
22 CHECK1, /* one check byte to go */ 22 CHECK1, /* one check byte to go */
23 DONE, /* finished check, done */ 23 DONE, /* finished check, done */
24 BAD} /* got an error--stay here */ 24 BAD} /* got an error--stay here */
25 mode; /* current inflate mode */ 25 mode; /* current inflate mode */
26 26
27 /* mode dependent information */ 27 /* mode dependent information */
28 union { 28 union {
29 uInt method; /* if FLAGS, method byte */ 29 uInt method; /* if FLAGS, method byte */
30 struct { 30 struct {
31 uLong was; /* computed check value */ 31 uLong was; /* computed check value */
32 uLong need; /* stream check value */ 32 uLong need; /* stream check value */
33 } check; /* if CHECK, check values to compare */ 33 } check; /* if CHECK, check values to compare */
34 uInt marker; /* if BAD, inflateSync's marker bytes count */ 34 uInt marker; /* if BAD, inflateSync's marker bytes count */
35 } sub; /* submode */ 35 } sub; /* submode */
36 36
37 /* mode independent information */ 37 /* mode independent information */
38 int nowrap; /* flag for no wrapper */ 38 int nowrap; /* flag for no wrapper */
39 uInt wbits; /* log2(window size) (8..15, defaults to 15) */ 39 uInt wbits; /* log2(window size) (8..15, defaults to 15) */
40 struct inflate_blocks_state 40 struct inflate_blocks_state
41 *blocks; /* current inflate_blocks state */ 41 *blocks; /* current inflate_blocks state */
42 42
43}; 43};
44 44
@@ -135,7 +135,7 @@ int inflate(z, f)
135z_stream *z; 135z_stream *z;
136int f; 136int f;
137{ 137{
138 int r = f; /* to avoid warning about unused f */ 138 int r = f; /* to avoid warning about unused f */
139 uInt b; 139 uInt b;
140 140
141 if (z == Z_NULL || z->next_in == Z_NULL) 141 if (z == Z_NULL || z->next_in == Z_NULL)
@@ -148,16 +148,16 @@ int f;
148 if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED) 148 if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED)
149 { 149 {
150 z->state->mode = BAD; 150 z->state->mode = BAD;
151 z->msg = "unknown compression method"; 151 z->msg = "unknown compression method";
152 z->state->sub.marker = 5; /* can't try inflateSync */ 152 z->state->sub.marker = 5; /* can't try inflateSync */
153 break; 153 break;
154 } 154 }
155 if ((z->state->sub.method >> 4) + 8 > z->state->wbits) 155 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
156 { 156 {
157 z->state->mode = BAD; 157 z->state->mode = BAD;
158 z->msg = "invalid window size"; 158 z->msg = "invalid window size";
159 z->state->sub.marker = 5; /* can't try inflateSync */ 159 z->state->sub.marker = 5; /* can't try inflateSync */
160 break; 160 break;
161 } 161 }
162 z->state->mode = FLAG; 162 z->state->mode = FLAG;
163 case FLAG: 163 case FLAG:
@@ -165,16 +165,16 @@ int f;
165 if ((b = NEXTBYTE) & 0x20) 165 if ((b = NEXTBYTE) & 0x20)
166 { 166 {
167 z->state->mode = BAD; 167 z->state->mode = BAD;
168 z->msg = "invalid reserved bit"; 168 z->msg = "invalid reserved bit";
169 z->state->sub.marker = 5; /* can't try inflateSync */ 169 z->state->sub.marker = 5; /* can't try inflateSync */
170 break; 170 break;
171 } 171 }
172 if (((z->state->sub.method << 8) + b) % 31) 172 if (((z->state->sub.method << 8) + b) % 31)
173 { 173 {
174 z->state->mode = BAD; 174 z->state->mode = BAD;
175 z->msg = "incorrect header check"; 175 z->msg = "incorrect header check";
176 z->state->sub.marker = 5; /* can't try inflateSync */ 176 z->state->sub.marker = 5; /* can't try inflateSync */
177 break; 177 break;
178 } 178 }
179 Trace((stderr, "inflate: zlib header ok\n")); 179 Trace((stderr, "inflate: zlib header ok\n"));
180 z->state->mode = BLOCKS; 180 z->state->mode = BLOCKS;
@@ -183,17 +183,17 @@ int f;
183 if (r == Z_DATA_ERROR) 183 if (r == Z_DATA_ERROR)
184 { 184 {
185 z->state->mode = BAD; 185 z->state->mode = BAD;
186 z->state->sub.marker = 0; /* can try inflateSync */ 186 z->state->sub.marker = 0; /* can try inflateSync */
187 break; 187 break;
188 } 188 }
189 if (r != Z_STREAM_END) 189 if (r != Z_STREAM_END)
190 return r; 190 return r;
191 r = Z_OK; 191 r = Z_OK;
192 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was); 192 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
193 if (z->state->nowrap) 193 if (z->state->nowrap)
194 { 194 {
195 z->state->mode = DONE; 195 z->state->mode = DONE;
196 break; 196 break;
197 } 197 }
198 z->state->mode = CHECK4; 198 z->state->mode = CHECK4;
199 case CHECK4: 199 case CHECK4:
@@ -216,8 +216,8 @@ int f;
216 { 216 {
217 z->state->mode = BAD; 217 z->state->mode = BAD;
218 z->msg = "incorrect data check"; 218 z->msg = "incorrect data check";
219 z->state->sub.marker = 5; /* can't try inflateSync */ 219 z->state->sub.marker = 5; /* can't try inflateSync */
220 break; 220 break;
221 } 221 }
222 Trace((stderr, "inflate: zlib check ok\n")); 222 Trace((stderr, "inflate: zlib check ok\n"));
223 z->state->mode = DONE; 223 z->state->mode = DONE;
@@ -234,10 +234,10 @@ int f;
234int inflateSync(z) 234int inflateSync(z)
235z_stream *z; 235z_stream *z;
236{ 236{
237 uInt n; /* number of bytes to look at */ 237 uInt n; /* number of bytes to look at */
238 Byte *p; /* pointer to bytes */ 238 Byte *p; /* pointer to bytes */
239 uInt m; /* number of marker bytes found in a row */ 239 uInt m; /* number of marker bytes found in a row */
240 uLong r, w; /* temporaries to save total_in and total_out */ 240 uLong r, w; /* temporaries to save total_in and total_out */
241 241
242 /* set up */ 242 /* set up */
243 if (z == Z_NULL || z->state == Z_NULL) 243 if (z == Z_NULL || z->state == Z_NULL)
diff --git a/inftest.c b/inftest.c
deleted file mode 100644
index d711bfa..0000000
--- a/inftest.c
+++ /dev/null
@@ -1,69 +0,0 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include "zutil.h"
4
5void main __P((void));
6
7/* This test is in honor of Ed Hamrick who suggested that the interface
8 to inflate be a byte at a time--this implements that, and is, of course,
9 monumentally slow. It has the virtue though of stressing the push-pull
10 interface for testing purposes. */
11
12void main()
13{
14 int a, r;
15 char c;
16 z_stream z;
17
18 z.zalloc = Z_NULL;
19 z.zfree = Z_NULL;
20 r = inflateInit(&z);
21 if (r != Z_OK)
22 fprintf(stderr, "init error: %s\n", z_errmsg[1 - r]);
23 while ((a = getchar()) != EOF)
24 {
25 /* feed one byte of input */
26 z.avail_out = 0;
27 c = (char)a;
28 z.next_in = (Byte*)&c;
29 z.avail_in = 1;
30 r = inflate(&z, 0);
31 if (r == Z_STREAM_END)
32 break;
33 if (r != Z_OK)
34 {
35 fprintf(stderr, "inflate error: %s\n", z_errmsg[1 - r]);
36 break;
37 }
38 if (z.avail_in != 0)
39 {
40 fprintf(stderr, "inflate didn't eat byte and didn't say buf err!\n");
41 break;
42 }
43
44 /* empty output one byte at a time */
45 while (1)
46 {
47 z.next_out = (Byte*)&c;
48 z.avail_out = 1;
49 r = inflate(&z, 0);
50 if (r == Z_STREAM_END)
51 break;
52 if (r != Z_OK && r != Z_BUF_ERROR)
53 {
54 fprintf(stderr, "inflate error: %s\n", z_errmsg[1 - r]);
55 break;
56 }
57 if (z.avail_out == 0)
58 putchar(c);
59 else
60 break;
61 }
62 if (r != Z_OK && r != Z_BUF_ERROR)
63 break;
64 }
65 inflateEnd(&z);
66 fprintf(stderr, "%ld bytes in, %ld bytes out\n", z.total_in, z.total_out);
67 if (z.msg != NULL)
68 fprintf(stderr, "msg is <%s>\n", z.msg);
69}
diff --git a/inftrees.c b/inftrees.c
index ab0ed2c..9858927 100644
--- a/inftrees.c
+++ b/inftrees.c
@@ -16,37 +16,37 @@ struct internal_state {int dummy;}; /* for buggy compilers */
16 16
17 17
18local int huft_build __P(( 18local int huft_build __P((
19 uInt *, /* code lengths in bits */ 19 uInt *, /* code lengths in bits */
20 uInt, /* number of codes */ 20 uInt, /* number of codes */
21 uInt, /* number of "simple" codes */ 21 uInt, /* number of "simple" codes */
22 uInt *, /* list of base values for non-simple codes */ 22 uInt *, /* list of base values for non-simple codes */
23 uInt *, /* list of extra bits for non-simple codes */ 23 uInt *, /* list of extra bits for non-simple codes */
24 inflate_huft **, /* result: starting table */ 24 inflate_huft **, /* result: starting table */
25 uInt *, /* maximum lookup bits (returns actual) */ 25 uInt *, /* maximum lookup bits (returns actual) */
26 z_stream *)); /* for zalloc function */ 26 z_stream *)); /* for zalloc function */
27 27
28local voidp falloc __P(( 28local voidp falloc __P((
29 voidp, /* opaque pointer (not used) */ 29 voidp, /* opaque pointer (not used) */
30 uInt, /* number of items */ 30 uInt, /* number of items */
31 uInt)); /* size of item */ 31 uInt)); /* size of item */
32 32
33local void ffree __P(( 33local void ffree __P((
34 voidp q, /* opaque pointer (not used) */ 34 voidp q, /* opaque pointer (not used) */
35 voidp p)); /* what to free (not used) */ 35 voidp p)); /* what to free (not used) */
36 36
37/* Tables for deflate from PKZIP's appnote.txt. */ 37/* Tables for deflate from PKZIP's appnote.txt. */
38local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */ 38local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */
39 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 39 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
40 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; 40 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
41 /* actually lengths - 2; also see note #13 above about 258 */ 41 /* actually lengths - 2; also see note #13 above about 258 */
42local uInt cplext[] = { /* Extra bits for literal codes 257..285 */ 42local uInt cplext[] = { /* Extra bits for literal codes 257..285 */
43 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 43 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
44 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 128, 128}; /* 128==invalid */ 44 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 128, 128}; /* 128==invalid */
45local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */ 45local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */
46 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 46 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
47 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 47 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
48 8193, 12289, 16385, 24577}; 48 8193, 12289, 16385, 24577};
49local uInt cpdext[] = { /* Extra bits for distance codes */ 49local uInt cpdext[] = { /* Extra bits for distance codes */
50 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 50 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
51 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 51 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
52 12, 12, 13, 13}; 52 12, 12, 13, 13};
@@ -100,32 +100,32 @@ uInt *d; /* list of base values for non-simple codes */
100uInt *e; /* list of extra bits for non-simple codes */ 100uInt *e; /* list of extra bits for non-simple codes */
101inflate_huft **t; /* result: starting table */ 101inflate_huft **t; /* result: starting table */
102uInt *m; /* maximum lookup bits, returns actual */ 102uInt *m; /* maximum lookup bits, returns actual */
103z_stream *zs; /* for zalloc function */ 103z_stream *zs; /* for zalloc function */
104/* Given a list of code lengths and a maximum table size, make a set of 104/* Given a list of code lengths and a maximum table size, make a set of
105 tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR 105 tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
106 if the given code set is incomplete (the tables are still built in this 106 if the given code set is incomplete (the tables are still built in this
107 case), Z_DATA_ERROR if the input is invalid (all zero length codes or an 107 case), Z_DATA_ERROR if the input is invalid (all zero length codes or an
108 over-subscribed set of lengths), or Z_MEM_ERROR if not enough memory. */ 108 over-subscribed set of lengths), or Z_MEM_ERROR if not enough memory. */
109{ 109{
110 uInt a; /* counter for codes of length k */ 110 uInt a; /* counter for codes of length k */
111 uInt c[BMAX+1]; /* bit length count table */ 111 uInt c[BMAX+1]; /* bit length count table */
112 uInt f; /* i repeats in table every f entries */ 112 uInt f; /* i repeats in table every f entries */
113 int g; /* maximum code length */ 113 int g; /* maximum code length */
114 int h; /* table level */ 114 int h; /* table level */
115 register uInt i; /* counter, current code */ 115 register uInt i; /* counter, current code */
116 register uInt j; /* counter */ 116 register uInt j; /* counter */
117 register int k; /* number of bits in current code */ 117 register int k; /* number of bits in current code */
118 int l; /* bits per table (returned in m) */ 118 int l; /* bits per table (returned in m) */
119 register uInt *p; /* pointer into c[], b[], or v[] */ 119 register uInt *p; /* pointer into c[], b[], or v[] */
120 register inflate_huft *q; /* points to current table */ 120 register inflate_huft *q; /* points to current table */
121 inflate_huft r; /* table entry for structure assignment */ 121 inflate_huft r; /* table entry for structure assignment */
122 inflate_huft *u[BMAX]; /* table stack */ 122 inflate_huft *u[BMAX]; /* table stack */
123 uInt v[N_MAX]; /* values in order of bit length */ 123 uInt v[N_MAX]; /* values in order of bit length */
124 register int w; /* bits before this table == (l * h) */ 124 register int w; /* bits before this table == (l * h) */
125 uInt x[BMAX+1]; /* bit offsets, then code stack */ 125 uInt x[BMAX+1]; /* bit offsets, then code stack */
126 uInt *xp; /* pointer into x */ 126 uInt *xp; /* pointer into x */
127 int y; /* number of dummy codes added */ 127 int y; /* number of dummy codes added */
128 uInt z; /* number of entries in current table */ 128 uInt z; /* number of entries in current table */
129 129
130 130
131 /* Generate counts for each bit length */ 131 /* Generate counts for each bit length */
@@ -133,7 +133,7 @@ z_stream *zs; /* for zalloc function */
133#define C0 *p++ = 0; 133#define C0 *p++ = 0;
134#define C2 C0 C0 C0 C0 134#define C2 C0 C0 C0 C0
135#define C4 C2 C2 C2 C2 135#define C4 C2 C2 C2 C2
136 C4 /* clear c[]--assume BMAX+1 is 16 */ 136 C4 /* clear c[]--assume BMAX+1 is 16 */
137 p = b; i = n; 137 p = b; i = n;
138 do { 138 do {
139 c[*p++]++; /* assume all entries <= BMAX */ 139 c[*p++]++; /* assume all entries <= BMAX */
@@ -193,8 +193,8 @@ z_stream *zs; /* for zalloc function */
193 p = v; /* grab values in bit order */ 193 p = v; /* grab values in bit order */
194 h = -1; /* no tables yet--level -1 */ 194 h = -1; /* no tables yet--level -1 */
195 w = -l; /* bits decoded == (l * h) */ 195 w = -l; /* bits decoded == (l * h) */
196 u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */ 196 u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */
197 q = (inflate_huft *)Z_NULL; /* ditto */ 197 q = (inflate_huft *)Z_NULL; /* ditto */
198 z = 0; /* ditto */ 198 z = 0; /* ditto */
199 199
200 /* go through the bit lengths (k already is bits in shortest code) */ 200 /* go through the bit lengths (k already is bits in shortest code) */
@@ -217,25 +217,25 @@ z_stream *zs; /* for zalloc function */
217 f -= a + 1; /* deduct codes from patterns left */ 217 f -= a + 1; /* deduct codes from patterns left */
218 xp = c + k; 218 xp = c + k;
219 if (j < z) 219 if (j < z)
220 while (++j < z) /* try smaller tables up to z bits */ 220 while (++j < z) /* try smaller tables up to z bits */
221 { 221 {
222 if ((f <<= 1) <= *++xp) 222 if ((f <<= 1) <= *++xp)
223 break; /* enough codes to use up j bits */ 223 break; /* enough codes to use up j bits */
224 f -= *xp; /* else deduct codes from patterns */ 224 f -= *xp; /* else deduct codes from patterns */
225 } 225 }
226 } 226 }
227 z = 1 << j; /* table entries for j-bit table */ 227 z = 1 << j; /* table entries for j-bit table */
228 228
229 /* allocate and link in new table */ 229 /* allocate and link in new table */
230 if ((q = (inflate_huft *)ZALLOC 230 if ((q = (inflate_huft *)ZALLOC
231 (zs,z + 1,sizeof(inflate_huft))) == Z_NULL) 231 (zs,z + 1,sizeof(inflate_huft))) == Z_NULL)
232 { 232 {
233 if (h) 233 if (h)
234 inflate_trees_free(u[0], zs); 234 inflate_trees_free(u[0], zs);
235 return Z_MEM_ERROR; /* not enough memory */ 235 return Z_MEM_ERROR; /* not enough memory */
236 } 236 }
237#ifdef DEBUG 237#ifdef DEBUG
238 inflate_hufts += z + 1; 238 inflate_hufts += z + 1;
239#endif 239#endif
240 *t = q + 1; /* link to list for huft_free() */ 240 *t = q + 1; /* link to list for huft_free() */
241 *(t = &(q->next)) = (inflate_huft *)Z_NULL; 241 *(t = &(q->next)) = (inflate_huft *)Z_NULL;
@@ -245,8 +245,8 @@ z_stream *zs; /* for zalloc function */
245 if (h) 245 if (h)
246 { 246 {
247 x[h] = i; /* save pattern for backing up */ 247 x[h] = i; /* save pattern for backing up */
248 r.bits = (char)l; /* bits to dump before this table */ 248 r.bits = (Byte)l; /* bits to dump before this table */
249 r.exop = -(char)j; /* bits in this table */ 249 r.exop = -(Char)j; /* bits in this table */
250 r.next = q; /* pointer to this table */ 250 r.next = q; /* pointer to this table */
251 j = i >> (w - l); /* (get around Turbo C bug) */ 251 j = i >> (w - l); /* (get around Turbo C bug) */
252 u[h-1][j] = r; /* connect to last table */ 252 u[h-1][j] = r; /* connect to last table */
@@ -254,17 +254,17 @@ z_stream *zs; /* for zalloc function */
254 } 254 }
255 255
256 /* set up table entry in r */ 256 /* set up table entry in r */
257 r.bits = (char)(k - w); 257 r.bits = (Byte)(k - w);
258 if (p >= v + n) 258 if (p >= v + n)
259 r.exop = -128; /* out of values--invalid code */ 259 r.exop = (Char)(-128); /* out of values--invalid code */
260 else if (*p < s) 260 else if (*p < s)
261 { 261 {
262 r.exop = (char)(*p < 256 ? 16 : -64); /* 256 is end-of-block code */ 262 r.exop = (Char)(*p < 256 ? 16 : -64); /* 256 is end-of-block code */
263 r.base = *p++; /* simple code is just the value */ 263 r.base = *p++; /* simple code is just the value */
264 } 264 }
265 else 265 else
266 { 266 {
267 r.exop = (char)e[*p - s]; /* non-simple--look up in lists */ 267 r.exop = (Char)e[*p - s]; /* non-simple--look up in lists */
268 r.base = d[*p++ - s]; 268 r.base = d[*p++ - s];
269 } 269 }
270 270
@@ -294,10 +294,10 @@ z_stream *zs; /* for zalloc function */
294 294
295 295
296int inflate_trees_bits(c, bb, tb, z) 296int inflate_trees_bits(c, bb, tb, z)
297uInt *c; /* 19 code lengths */ 297uInt *c; /* 19 code lengths */
298uInt *bb; /* bits tree desired/actual depth */ 298uInt *bb; /* bits tree desired/actual depth */
299inflate_huft **tb; /* bits tree result */ 299inflate_huft **tb; /* bits tree result */
300z_stream *z; /* for zfree function */ 300z_stream *z; /* for zfree function */
301{ 301{
302 int r; 302 int r;
303 303
@@ -315,14 +315,14 @@ z_stream *z; /* for zfree function */
315 315
316 316
317int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z) 317int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z)
318uInt nl; /* number of literal/length codes */ 318uInt nl; /* number of literal/length codes */
319uInt nd; /* number of distance codes */ 319uInt nd; /* number of distance codes */
320uInt *c; /* that many (total) code lengths */ 320uInt *c; /* that many (total) code lengths */
321uInt *bl; /* literal desired/actual bit depth */ 321uInt *bl; /* literal desired/actual bit depth */
322uInt *bd; /* distance desired/actual bit depth */ 322uInt *bd; /* distance desired/actual bit depth */
323inflate_huft **tl; /* literal/length tree result */ 323inflate_huft **tl; /* literal/length tree result */
324inflate_huft **td; /* distance tree result */ 324inflate_huft **td; /* distance tree result */
325z_stream *z; /* for zfree function */ 325z_stream *z; /* for zfree function */
326{ 326{
327 int r; 327 int r;
328 328
@@ -367,7 +367,7 @@ z_stream *z; /* for zfree function */
367/* build fixed tables only once--keep them here */ 367/* build fixed tables only once--keep them here */
368local int fixed_lock = 0; 368local int fixed_lock = 0;
369local int fixed_built = 0; 369local int fixed_built = 0;
370#define FIXEDH 530 /* number of hufts used by fixed tables */ 370#define FIXEDH 530 /* number of hufts used by fixed tables */
371local uInt fixed_left = FIXEDH; 371local uInt fixed_left = FIXEDH;
372local inflate_huft fixed_mem[FIXEDH]; 372local inflate_huft fixed_mem[FIXEDH];
373local uInt fixed_bl; 373local uInt fixed_bl;
@@ -377,9 +377,9 @@ local inflate_huft *fixed_td;
377 377
378 378
379local voidp falloc(q, n, s) 379local voidp falloc(q, n, s)
380voidp q; /* opaque pointer (not used) */ 380voidp q; /* opaque pointer (not used) */
381uInt n; /* number of items */ 381uInt n; /* number of items */
382uInt s; /* size of item */ 382uInt s; /* size of item */
383{ 383{
384 Assert(s == sizeof(inflate_huft) && n <= fixed_left, 384 Assert(s == sizeof(inflate_huft) && n <= fixed_left,
385 "inflate_trees falloc overflow"); 385 "inflate_trees falloc overflow");
@@ -399,19 +399,19 @@ voidp p;
399 399
400 400
401int inflate_trees_fixed(bl, bd, tl, td) 401int inflate_trees_fixed(bl, bd, tl, td)
402uInt *bl; /* literal desired/actual bit depth */ 402uInt *bl; /* literal desired/actual bit depth */
403uInt *bd; /* distance desired/actual bit depth */ 403uInt *bd; /* distance desired/actual bit depth */
404inflate_huft **tl; /* literal/length tree result */ 404inflate_huft **tl; /* literal/length tree result */
405inflate_huft **td; /* distance tree result */ 405inflate_huft **td; /* distance tree result */
406{ 406{
407 /* build fixed tables if not built already--lock out other instances */ 407 /* build fixed tables if not built already--lock out other instances */
408 while (++fixed_lock > 1) 408 while (++fixed_lock > 1)
409 fixed_lock--; 409 fixed_lock--;
410 if (!fixed_built) 410 if (!fixed_built)
411 { 411 {
412 int k; /* temporary variable */ 412 int k; /* temporary variable */
413 unsigned c[288]; /* length list for huft_build */ 413 unsigned c[288]; /* length list for huft_build */
414 z_stream z; /* for falloc function */ 414 z_stream z; /* for falloc function */
415 415
416 /* set up fake z_stream for memory routines */ 416 /* set up fake z_stream for memory routines */
417 z.zalloc = falloc; 417 z.zalloc = falloc;
@@ -449,8 +449,8 @@ inflate_huft **td; /* distance tree result */
449 449
450 450
451int inflate_trees_free(t, z) 451int inflate_trees_free(t, z)
452inflate_huft *t; /* table to free */ 452inflate_huft *t; /* table to free */
453z_stream *z; /* for zfree function */ 453z_stream *z; /* for zfree function */
454/* Free the malloc'ed tables built by huft_build(), which makes a linked 454/* Free the malloc'ed tables built by huft_build(), which makes a linked
455 list of the tables it made, with the links in a dummy first entry of 455 list of the tables it made, with the links in a dummy first entry of
456 each table. */ 456 each table. */
diff --git a/inftrees.h b/inftrees.h
index 6001a4e..27e7222 100644
--- a/inftrees.h
+++ b/inftrees.h
@@ -16,12 +16,18 @@
16 indicates an unused code. If a code with exop == -128 is looked up, 16 indicates an unused code. If a code with exop == -128 is looked up,
17 this implies an error in the data. */ 17 this implies an error in the data. */
18 18
19#if defined(STDC) || defined(sgi)
20typedef signed char Char;
21#else
22typedef char Char; /* just hope that char is signed */
23#endif
24
19typedef struct inflate_huft_s inflate_huft; 25typedef struct inflate_huft_s inflate_huft;
20struct inflate_huft_s { 26struct inflate_huft_s {
21 union { 27 union {
22 struct { 28 struct {
23 char Exop; /* number of extra bits or operation */ 29 Char Exop; /* number of extra bits or operation */
24 char Bits; /* number of bits in this code or subcode */ 30 Byte Bits; /* number of bits in this code or subcode */
25 } what; 31 } what;
26 Byte *pad; /* pad structure to a power of 2 (4 bytes for */ 32 Byte *pad; /* pad structure to a power of 2 (4 bytes for */
27 } word; /* 16-bit, 8 bytes for 32-bit machines) */ 33 } word; /* 16-bit, 8 bytes for 32-bit machines) */
@@ -36,27 +42,27 @@ struct inflate_huft_s {
36#endif 42#endif
37 43
38extern int inflate_trees_bits __P(( 44extern int inflate_trees_bits __P((
39 uInt *, /* 19 code lengths */ 45 uInt *, /* 19 code lengths */
40 uInt *, /* bits tree desired/actual depth */ 46 uInt *, /* bits tree desired/actual depth */
41 inflate_huft **, /* bits tree result */ 47 inflate_huft **, /* bits tree result */
42 z_stream *)); /* for zalloc, zfree functions */ 48 z_stream *)); /* for zalloc, zfree functions */
43 49
44extern int inflate_trees_dynamic __P(( 50extern int inflate_trees_dynamic __P((
45 uInt, /* number of literal/length codes */ 51 uInt, /* number of literal/length codes */
46 uInt, /* number of distance codes */ 52 uInt, /* number of distance codes */
47 uInt *, /* that many (total) code lengths */ 53 uInt *, /* that many (total) code lengths */
48 uInt *, /* literal desired/actual bit depth */ 54 uInt *, /* literal desired/actual bit depth */
49 uInt *, /* distance desired/actual bit depth */ 55 uInt *, /* distance desired/actual bit depth */
50 inflate_huft **, /* literal/length tree result */ 56 inflate_huft **, /* literal/length tree result */
51 inflate_huft **, /* distance tree result */ 57 inflate_huft **, /* distance tree result */
52 z_stream *)); /* for zalloc, zfree functions */ 58 z_stream *)); /* for zalloc, zfree functions */
53 59
54extern int inflate_trees_fixed __P(( 60extern int inflate_trees_fixed __P((
55 uInt *, /* literal desired/actual bit depth */ 61 uInt *, /* literal desired/actual bit depth */
56 uInt *, /* distance desired/actual bit depth */ 62 uInt *, /* distance desired/actual bit depth */
57 inflate_huft **, /* literal/length tree result */ 63 inflate_huft **, /* literal/length tree result */
58 inflate_huft **)); /* distance tree result */ 64 inflate_huft **)); /* distance tree result */
59 65
60extern int inflate_trees_free __P(( 66extern int inflate_trees_free __P((
61 inflate_huft *, /* tables to free */ 67 inflate_huft *, /* tables to free */
62 z_stream *)); /* for zfree function */ 68 z_stream *)); /* for zfree function */
diff --git a/infutil.h b/infutil.h
index f468f1a..f234e9b 100644
--- a/infutil.h
+++ b/infutil.h
@@ -13,42 +13,42 @@ struct inflate_blocks_state {
13 13
14 /* mode */ 14 /* mode */
15 enum { 15 enum {
16 TYPE, /* get type bits (3, including end bit) */ 16 TYPE, /* get type bits (3, including end bit) */
17 LENS, /* get lengths for stored */ 17 LENS, /* get lengths for stored */
18 STORED, /* processing stored block */ 18 STORED, /* processing stored block */
19 TABLE, /* get table lengths */ 19 TABLE, /* get table lengths */
20 BTREE, /* get bit lengths tree for a dynamic block */ 20 BTREE, /* get bit lengths tree for a dynamic block */
21 DTREE, /* get length, distance trees for a dynamic block */ 21 DTREE, /* get length, distance trees for a dynamic block */
22 CODES, /* processing fixed or dynamic block */ 22 CODES, /* processing fixed or dynamic block */
23 DRY, /* output remaining window bytes */ 23 DRY, /* output remaining window bytes */
24 DONE, /* finished last block, done */ 24 DONE, /* finished last block, done */
25 BAD} /* got a data error--stuck here */ 25 BAD} /* got a data error--stuck here */
26 mode; /* current inflate_block mode */ 26 mode; /* current inflate_block mode */
27 27
28 /* mode dependent information */ 28 /* mode dependent information */
29 union { 29 union {
30 uInt left; /* if STORED, bytes left to copy */ 30 uInt left; /* if STORED, bytes left to copy */
31 struct { 31 struct {
32 uInt table; /* table lengths (14 bits) */ 32 uInt table; /* table lengths (14 bits) */
33 uInt index; /* index into blens (or border) */ 33 uInt index; /* index into blens (or border) */
34 uInt *blens; /* bit lengths of codes */ 34 uInt *blens; /* bit lengths of codes */
35 uInt bb; /* bit length tree depth */ 35 uInt bb; /* bit length tree depth */
36 inflate_huft *tb; /* bit length decoding tree */ 36 inflate_huft *tb; /* bit length decoding tree */
37 } trees; /* if DTREE, decoding info for trees */ 37 } trees; /* if DTREE, decoding info for trees */
38 struct inflate_codes_state 38 struct inflate_codes_state
39 *codes; /* if CODES, current state */ 39 *codes; /* if CODES, current state */
40 } sub; /* submode */ 40 } sub; /* submode */
41 uInt last; /* true if this block is the last block */ 41 uInt last; /* true if this block is the last block */
42 42
43 /* mode independent information */ 43 /* mode independent information */
44 uInt bitk; /* bits in bit buffer */ 44 uInt bitk; /* bits in bit buffer */
45 uLong bitb; /* bit buffer */ 45 uLong bitb; /* bit buffer */
46 Byte *window; /* sliding window */ 46 Byte *window; /* sliding window */
47 Byte *end; /* one byte after sliding window */ 47 Byte *end; /* one byte after sliding window */
48 Byte *read; /* window read pointer */ 48 Byte *read; /* window read pointer */
49 Byte *write; /* window write pointer */ 49 Byte *write; /* window write pointer */
50 check_func checkfn; /* check function */ 50 check_func checkfn; /* check function */
51 uLong check; /* check on output */ 51 uLong check; /* check on output */
52 52
53}; 53};
54 54
diff --git a/minigzip.c b/minigzip.c
index 8f58080..017c3e4 100644
--- a/minigzip.c
+++ b/minigzip.c
@@ -13,7 +13,7 @@
13 * or in pipe mode. 13 * or in pipe mode.
14 */ 14 */
15 15
16/* $Id: minigzip.c,v 1.4 1995/05/02 15:54:22 jloup Exp $ */ 16/* $Id: minigzip.c,v 1.5 1995/05/03 17:27:11 jloup Exp $ */
17 17
18#include <stdio.h> 18#include <stdio.h>
19#include "zlib.h" 19#include "zlib.h"
@@ -75,14 +75,14 @@ void gz_compress(in, out)
75 int err; 75 int err;
76 76
77 for (;;) { 77 for (;;) {
78 len = fread(buf, 1, sizeof(buf), in); 78 len = fread(buf, 1, sizeof(buf), in);
79 if (ferror(in)) { 79 if (ferror(in)) {
80 perror("fread"); 80 perror("fread");
81 exit(1); 81 exit(1);
82 } 82 }
83 if (len == 0) break; 83 if (len == 0) break;
84 84
85 if (gzwrite(out, buf, len) != len) error(gzerror(out, &err)); 85 if (gzwrite(out, buf, len) != len) error(gzerror(out, &err));
86 } 86 }
87 fclose(in); 87 fclose(in);
88 if (gzclose(out) != Z_OK) error("failed gzclose"); 88 if (gzclose(out) != Z_OK) error("failed gzclose");
@@ -100,11 +100,11 @@ void gz_uncompress(in, out)
100 int err; 100 int err;
101 101
102 for (;;) { 102 for (;;) {
103 len = gzread(in, buf, sizeof(buf)); 103 len = gzread(in, buf, sizeof(buf));
104 if (len < 0) error (gzerror(in, &err)); 104 if (len < 0) error (gzerror(in, &err));
105 if (len == 0) break; 105 if (len == 0) break;
106 106
107 if (fwrite(buf, 1, len, out) != (uInt)len) error("failed fwrite"); 107 if (fwrite(buf, 1, len, out) != (uInt)len) error("failed fwrite");
108 } 108 }
109 if (fclose(out)) error("failed fclose"); 109 if (fclose(out)) error("failed fclose");
110 110
@@ -128,13 +128,13 @@ void file_compress(file)
128 128
129 in = fopen(file, "rb"); 129 in = fopen(file, "rb");
130 if (in == NULL) { 130 if (in == NULL) {
131 perror(file); 131 perror(file);
132 exit(1); 132 exit(1);
133 } 133 }
134 out = gzopen(outfile, "wb"); 134 out = gzopen(outfile, "wb");
135 if (out == NULL) { 135 if (out == NULL) {
136 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile); 136 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
137 exit(1); 137 exit(1);
138 } 138 }
139 gz_compress(in, out); 139 gz_compress(in, out);
140 140
@@ -157,23 +157,23 @@ void file_uncompress(file)
157 strcpy(buf, file); 157 strcpy(buf, file);
158 158
159 if (len > 3 && strcmp(file+len-3, ".gz") == 0) { 159 if (len > 3 && strcmp(file+len-3, ".gz") == 0) {
160 infile = file; 160 infile = file;
161 outfile = buf; 161 outfile = buf;
162 outfile[len-3] = '\0'; 162 outfile[len-3] = '\0';
163 } else { 163 } else {
164 outfile = file; 164 outfile = file;
165 infile = buf; 165 infile = buf;
166 strcat(infile, ".gz"); 166 strcat(infile, ".gz");
167 } 167 }
168 in = gzopen(infile, "rb"); 168 in = gzopen(infile, "rb");
169 if (in == NULL) { 169 if (in == NULL) {
170 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile); 170 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
171 exit(1); 171 exit(1);
172 } 172 }
173 out = fopen(outfile, "wb"); 173 out = fopen(outfile, "wb");
174 if (out == NULL) { 174 if (out == NULL) {
175 perror(file); 175 perror(file);
176 exit(1); 176 exit(1);
177 } 177 }
178 178
179 gz_uncompress(in, out); 179 gz_uncompress(in, out);
@@ -197,31 +197,31 @@ void main(argc, argv)
197 argc--, argv++; 197 argc--, argv++;
198 198
199 if (argc > 0) { 199 if (argc > 0) {
200 uncompr = (strcmp(*argv, "-d") == 0); 200 uncompr = (strcmp(*argv, "-d") == 0);
201 if (uncompr) { 201 if (uncompr) {
202 argc--, argv++; 202 argc--, argv++;
203 } 203 }
204 } 204 }
205 if (argc == 0) { 205 if (argc == 0) {
206 SET_BINARY_MODE(stdin); 206 SET_BINARY_MODE(stdin);
207 SET_BINARY_MODE(stdout); 207 SET_BINARY_MODE(stdout);
208 if (uncompr) { 208 if (uncompr) {
209 file = gzdopen(fileno(stdin), "rb"); 209 file = gzdopen(fileno(stdin), "rb");
210 if (file == NULL) error("can't gzdopen stdin"); 210 if (file == NULL) error("can't gzdopen stdin");
211 gz_uncompress(file, stdout); 211 gz_uncompress(file, stdout);
212 } else { 212 } else {
213 file = gzdopen(fileno(stdout), "wb"); 213 file = gzdopen(fileno(stdout), "wb");
214 if (file == NULL) error("can't gzdopen stdout"); 214 if (file == NULL) error("can't gzdopen stdout");
215 gz_compress(stdin, file); 215 gz_compress(stdin, file);
216 } 216 }
217 } else { 217 } else {
218 do { 218 do {
219 if (uncompr) { 219 if (uncompr) {
220 file_uncompress(*argv); 220 file_uncompress(*argv);
221 } else { 221 } else {
222 file_compress(*argv); 222 file_compress(*argv);
223 } 223 }
224 } while (argv++, --argc); 224 } while (argv++, --argc);
225 } 225 }
226 exit(0); 226 exit(0);
227} 227}
diff --git a/trees.c b/trees.c
index eb69d49..f85716e 100644
--- a/trees.c
+++ b/trees.c
@@ -29,7 +29,7 @@
29 * Addison-Wesley, 1983. ISBN 0-201-06672-6. 29 * Addison-Wesley, 1983. ISBN 0-201-06672-6.
30 */ 30 */
31 31
32/* $Id: trees.c,v 1.4 1995/05/01 16:53:44 jloup Exp $ */ 32/* $Id: trees.c,v 1.5 1995/05/03 17:27:12 jloup Exp $ */
33 33
34#include "deflate.h" 34#include "deflate.h"
35 35
@@ -139,15 +139,15 @@ local void scan_tree __P((deflate_state *s, ct_data *tree, int max_code));
139local void send_tree __P((deflate_state *s, ct_data *tree, int max_code)); 139local void send_tree __P((deflate_state *s, ct_data *tree, int max_code));
140local int build_bl_tree __P((deflate_state *s)); 140local int build_bl_tree __P((deflate_state *s));
141local void send_all_trees __P((deflate_state *s, int lcodes, int dcodes, 141local void send_all_trees __P((deflate_state *s, int lcodes, int dcodes,
142 int blcodes)); 142 int blcodes));
143local void compress_block __P((deflate_state *s, ct_data *ltree, 143local void compress_block __P((deflate_state *s, ct_data *ltree,
144 ct_data *dtree)); 144 ct_data *dtree));
145local void set_data_type __P((deflate_state *s)); 145local void set_data_type __P((deflate_state *s));
146local void send_bits __P((deflate_state *s, int value, int length)); 146local void send_bits __P((deflate_state *s, int value, int length));
147local unsigned bi_reverse __P((unsigned value, int length)); 147local unsigned bi_reverse __P((unsigned value, int length));
148local void bi_windup __P((deflate_state *s)); 148local void bi_windup __P((deflate_state *s));
149local void copy_block __P((deflate_state *s, char *buf, unsigned len, 149local void copy_block __P((deflate_state *s, char *buf, unsigned len,
150 int header)); 150 int header));
151 151
152#ifndef DEBUG 152#ifndef DEBUG
153# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) 153# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
@@ -243,7 +243,7 @@ void ct_init(s)
243 deflate_state *s; 243 deflate_state *s;
244{ 244{
245 if (static_dtree[0].Len == 0) { 245 if (static_dtree[0].Len == 0) {
246 ct_static_init(); /* To do: at compile time */ 246 ct_static_init(); /* To do: at compile time */
247 } 247 }
248 248
249 s->compressed_len = 0L; 249 s->compressed_len = 0L;
@@ -324,9 +324,9 @@ local void pqdownheap(s, tree, k)
324 while (j <= s->heap_len) { 324 while (j <= s->heap_len) {
325 /* Set j to the smallest of the two sons: */ 325 /* Set j to the smallest of the two sons: */
326 if (j < s->heap_len && 326 if (j < s->heap_len &&
327 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) { 327 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
328 j++; 328 j++;
329 } 329 }
330 /* Exit if v is smaller than both sons */ 330 /* Exit if v is smaller than both sons */
331 if (smaller(tree, v, s->heap[j], s->depth)) break; 331 if (smaller(tree, v, s->heap[j], s->depth)) break;
332 332
@@ -420,7 +420,7 @@ local void gen_bitlen(s, desc)
420 if (tree[m].Len != (unsigned) bits) { 420 if (tree[m].Len != (unsigned) bits) {
421 Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); 421 Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
422 s->opt_len += ((long)bits - (long)tree[m].Len) 422 s->opt_len += ((long)bits - (long)tree[m].Len)
423 *(long)tree[m].Freq; 423 *(long)tree[m].Freq;
424 tree[m].Len = (ush)bits; 424 tree[m].Len = (ush)bits;
425 } 425 }
426 n--; 426 n--;
@@ -686,7 +686,7 @@ local int build_bl_tree(s)
686 /* Update opt_len to include the bit length tree and counts */ 686 /* Update opt_len to include the bit length tree and counts */
687 s->opt_len += 3*(max_blindex+1) + 5+5+4; 687 s->opt_len += 3*(max_blindex+1) + 5+5+4;
688 Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", 688 Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
689 s->opt_len, s->static_len)); 689 s->opt_len, s->static_len));
690 690
691 return max_blindex; 691 return max_blindex;
692} 692}
@@ -758,11 +758,11 @@ ulg ct_flush_block(s, buf, stored_len, eof)
758 /* Construct the literal and distance trees */ 758 /* Construct the literal and distance trees */
759 build_tree(s, (tree_desc *)(&(s->l_desc))); 759 build_tree(s, (tree_desc *)(&(s->l_desc)));
760 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, 760 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
761 s->static_len)); 761 s->static_len));
762 762
763 build_tree(s, (tree_desc *)(&(s->d_desc))); 763 build_tree(s, (tree_desc *)(&(s->d_desc)));
764 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, 764 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
765 s->static_len)); 765 s->static_len));
766 /* At this point, opt_len and static_len are the total bit lengths of 766 /* At this point, opt_len and static_len are the total bit lengths of
767 * the compressed block data, excluding the tree representations. 767 * the compressed block data, excluding the tree representations.
768 */ 768 */
@@ -813,7 +813,7 @@ ulg ct_flush_block(s, buf, stored_len, eof)
813 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to 813 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
814 * transform a block into a stored block. 814 * transform a block into a stored block.
815 */ 815 */
816 ct_stored_block(s, buf, stored_len, eof); 816 ct_stored_block(s, buf, stored_len, eof);
817 817
818#ifdef FORCE_STATIC 818#ifdef FORCE_STATIC
819 } else if (static_lenb >= 0) { /* force static trees */ 819 } else if (static_lenb >= 0) { /* force static trees */
@@ -826,7 +826,7 @@ ulg ct_flush_block(s, buf, stored_len, eof)
826 } else { 826 } else {
827 send_bits(s, (DYN_TREES<<1)+eof, 3); 827 send_bits(s, (DYN_TREES<<1)+eof, 3);
828 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, 828 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
829 max_blindex+1); 829 max_blindex+1);
830 compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree); 830 compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
831 s->compressed_len += 3 + s->opt_len; 831 s->compressed_len += 3 + s->opt_len;
832 } 832 }
@@ -858,7 +858,7 @@ int ct_tally (s, dist, lc)
858 /* lc is the unmatched char */ 858 /* lc is the unmatched char */
859 s->dyn_ltree[lc].Freq++; 859 s->dyn_ltree[lc].Freq++;
860 } else { 860 } else {
861 s->matches++; 861 s->matches++;
862 /* Here, lc is the match length - MIN_MATCH */ 862 /* Here, lc is the match length - MIN_MATCH */
863 dist--; /* dist = match distance - 1 */ 863 dist--; /* dist = match distance - 1 */
864 Assert((ush)dist < (ush)MAX_DIST(s) && 864 Assert((ush)dist < (ush)MAX_DIST(s) &&
@@ -877,7 +877,7 @@ int ct_tally (s, dist, lc)
877 int dcode; 877 int dcode;
878 for (dcode = 0; dcode < D_CODES; dcode++) { 878 for (dcode = 0; dcode < D_CODES; dcode++) {
879 out_length += (ulg)s->dyn_dtree[dcode].Freq * 879 out_length += (ulg)s->dyn_dtree[dcode].Freq *
880 (5L+extra_dbits[dcode]); 880 (5L+extra_dbits[dcode]);
881 } 881 }
882 out_length >>= 3; 882 out_length >>= 3;
883 Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", 883 Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
@@ -907,7 +907,7 @@ local void compress_block(s, ltree, dtree)
907 int extra; /* number of extra bits to send */ 907 int extra; /* number of extra bits to send */
908 908
909 if (s->last_lit != 0) do { 909 if (s->last_lit != 0) do {
910 dist = s->d_buf[lx]; 910 dist = s->d_buf[lx];
911 lc = s->l_buf[lx++]; 911 lc = s->l_buf[lx++];
912 if (dist == 0) { 912 if (dist == 0) {
913 send_code(s, lc, ltree); /* send a literal byte */ 913 send_code(s, lc, ltree); /* send a literal byte */
@@ -921,7 +921,7 @@ local void compress_block(s, ltree, dtree)
921 lc -= base_length[code]; 921 lc -= base_length[code];
922 send_bits(s, lc, extra); /* send the extra length bits */ 922 send_bits(s, lc, extra); /* send the extra length bits */
923 } 923 }
924 dist--; /* dist is now the match distance - 1 */ 924 dist--; /* dist is now the match distance - 1 */
925 code = d_code(dist); 925 code = d_code(dist);
926 Assert (code < D_CODES, "bad d_code"); 926 Assert (code < D_CODES, "bad d_code");
927 927
@@ -933,8 +933,8 @@ local void compress_block(s, ltree, dtree)
933 } 933 }
934 } /* literal or match pair ? */ 934 } /* literal or match pair ? */
935 935
936 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ 936 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
937 Assert(s->pending < s->lit_bufsize + 2*lx, "pendingBuf overflow"); 937 Assert(s->pending < s->lit_bufsize + 2*lx, "pendingBuf overflow");
938 938
939 } while (lx < s->last_lit); 939 } while (lx < s->last_lit);
940 940
@@ -1055,6 +1055,6 @@ local void copy_block(s, buf, len, header)
1055 s->bits_sent += (ulg)len<<3; 1055 s->bits_sent += (ulg)len<<3;
1056#endif 1056#endif
1057 while (len--) { 1057 while (len--) {
1058 put_byte(s, *buf++); 1058 put_byte(s, *buf++);
1059 } 1059 }
1060} 1060}
diff --git a/uncompr.c b/uncompr.c
index 4c8b3af..c76286c 100644
--- a/uncompr.c
+++ b/uncompr.c
@@ -3,7 +3,7 @@
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: uncompr.c,v 1.4 1995/04/10 16:22:22 jloup Exp $ */ 6/* $Id: uncompr.c,v 1.5 1995/05/03 17:27:12 jloup Exp $ */
7 7
8#include "zlib.h" 8#include "zlib.h"
9 9
@@ -48,8 +48,8 @@ int uncompress (dest, destLen, source, sourceLen)
48 48
49 err = inflate(&stream, Z_FINISH); 49 err = inflate(&stream, Z_FINISH);
50 if (err != Z_STREAM_END) { 50 if (err != Z_STREAM_END) {
51 inflateEnd(&stream); 51 inflateEnd(&stream);
52 return err; 52 return err;
53 } 53 }
54 *destLen = stream.total_out; 54 *destLen = stream.total_out;
55 55
diff --git a/zconf.h b/zconf.h
index 1abb0ea..ce93061 100644
--- a/zconf.h
+++ b/zconf.h
@@ -3,7 +3,7 @@
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: zconf.h,v 1.11 1995/05/02 13:07:21 jloup Exp $ */ 6/* $Id: zconf.h,v 1.12 1995/05/03 17:27:12 jloup Exp $ */
7 7
8#ifndef _ZCONF_H 8#ifndef _ZCONF_H
9#define _ZCONF_H 9#define _ZCONF_H
@@ -59,7 +59,7 @@
59 for small objects. 59 for small objects.
60*/ 60*/
61 61
62 /* Type declarations */ 62 /* Type declarations */
63 63
64#ifndef __P /* function prototypes */ 64#ifndef __P /* function prototypes */
65# ifdef STDC 65# ifdef STDC
diff --git a/zlib.h b/zlib.h
index 1712209..00a4394 100644
--- a/zlib.h
+++ b/zlib.h
@@ -1,5 +1,5 @@
1/* zlib.h -- interface of the 'zlib' general purpose compression library 1/* zlib.h -- interface of the 'zlib' general purpose compression library
2 version 0.91 May 2nd, 1995. 2 version 0.92 May 3rd, 1995.
3 3
4 Copyright (C) 1995 Jean-loup Gailly and Mark Adler 4 Copyright (C) 1995 Jean-loup Gailly and Mark Adler
5 5
@@ -28,7 +28,7 @@
28 28
29#include "zconf.h" 29#include "zconf.h"
30 30
31#define ZLIB_VERSION "0.91" 31#define ZLIB_VERSION "0.92"
32 32
33/* 33/*
34 The 'zlib' compression library provides in-memory compression and 34 The 'zlib' compression library provides in-memory compression and
@@ -103,7 +103,7 @@ typedef struct z_stream_s {
103 a single step). 103 a single step).
104*/ 104*/
105 105
106 /* constants */ 106 /* constants */
107 107
108#define Z_NO_FLUSH 0 108#define Z_NO_FLUSH 0
109#define Z_PARTIAL_FLUSH 1 109#define Z_PARTIAL_FLUSH 1
@@ -142,7 +142,7 @@ extern char *zlib_version;
142 not compatible with the zlib.h header file used by the application. 142 not compatible with the zlib.h header file used by the application.
143 */ 143 */
144 144
145 /* basic functions */ 145 /* basic functions */
146 146
147extern int deflateInit __P((z_stream *strm, int level)); 147extern int deflateInit __P((z_stream *strm, int level));
148/* 148/*
@@ -313,8 +313,8 @@ extern int inflateEnd __P((z_stream *strm));
313*/ 313*/
314 314
315extern int deflateInit2 __P((z_stream *strm, 315extern int deflateInit2 __P((z_stream *strm,
316 int level, 316 int level,
317 int method, 317 int method,
318 int windowBits, 318 int windowBits,
319 int memLevel, 319 int memLevel,
320 int strategy)); 320 int strategy));
diff --git a/zutil.c b/zutil.c
index 508ad62..2cddcff 100644
--- a/zutil.c
+++ b/zutil.c
@@ -3,7 +3,7 @@
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6/* $Id: zutil.c,v 1.7 1995/05/02 15:54:47 jloup Exp $ */ 6/* $Id: zutil.c,v 1.8 1995/05/03 17:27:12 jloup Exp $ */
7 7
8#include <stdio.h> 8#include <stdio.h>
9 9
@@ -42,7 +42,7 @@ void zmemcpy(dest, source, len)
42{ 42{
43 if (len == 0) return; 43 if (len == 0) return;
44 do { 44 do {
45 *dest++ = *source++; /* ??? to be unrolled */ 45 *dest++ = *source++; /* ??? to be unrolled */
46 } while (--len != 0); 46 } while (--len != 0);
47} 47}
48 48
@@ -52,7 +52,7 @@ void zmemzero(dest, len)
52{ 52{
53 if (len == 0) return; 53 if (len == 0) return;
54 do { 54 do {
55 *dest++ = 0; /* ??? to be unrolled */ 55 *dest++ = 0; /* ??? to be unrolled */
56 } while (--len != 0); 56 } while (--len != 0);
57} 57}
58#endif 58#endif
@@ -91,10 +91,10 @@ voidp zcalloc (voidp opaque, unsigned items, unsigned size)
91 ulg bsize = (ulg)items*size; 91 ulg bsize = (ulg)items*size;
92 92
93 if (bsize < 65536L) { 93 if (bsize < 65536L) {
94 buf = farmalloc(bsize); 94 buf = farmalloc(bsize);
95 if (*(ush*)&buf != 0) return buf; 95 if (*(ush*)&buf != 0) return buf;
96 } else { 96 } else {
97 buf = farmalloc(bsize + 16L); 97 buf = farmalloc(bsize + 16L);
98 } 98 }
99 if (buf == NULL || next_ptr >= MAX_PTR) return NULL; 99 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
100 table[next_ptr].org_ptr = buf; 100 table[next_ptr].org_ptr = buf;
@@ -110,19 +110,19 @@ void zcfree (voidp opaque, voidp ptr)
110{ 110{
111 int n; 111 int n;
112 if (*(ush*)&ptr != 0) { /* object < 64K */ 112 if (*(ush*)&ptr != 0) { /* object < 64K */
113 farfree(ptr); 113 farfree(ptr);
114 return; 114 return;
115 } 115 }
116 /* Find the original pointer */ 116 /* Find the original pointer */
117 for (n = 0; n < next_ptr; n++) { 117 for (n = 0; n < next_ptr; n++) {
118 if (ptr != table[n].new_ptr) continue; 118 if (ptr != table[n].new_ptr) continue;
119 119
120 farfree(table[n].org_ptr); 120 farfree(table[n].org_ptr);
121 while (++n < next_ptr) { 121 while (++n < next_ptr) {
122 table[n-1] = table[n]; 122 table[n-1] = table[n];
123 } 123 }
124 next_ptr--; 124 next_ptr--;
125 return; 125 return;
126 } 126 }
127 ptr = opaque; /* just to make some compilers happy */ 127 ptr = opaque; /* just to make some compilers happy */
128 z_error("zcfree: ptr not found"); 128 z_error("zcfree: ptr not found");
diff --git a/zutil.h b/zutil.h
index 503a445..bc8af52 100644
--- a/zutil.h
+++ b/zutil.h
@@ -8,13 +8,19 @@
8 subject to change. Applications should only use zlib.h. 8 subject to change. Applications should only use zlib.h.
9 */ 9 */
10 10
11/* $Id: zutil.h,v 1.8 1995/05/02 15:44:46 jloup Exp $ */ 11/* $Id: zutil.h,v 1.9 1995/05/03 17:27:12 jloup Exp $ */
12 12
13#ifndef _Z_UTIL_H 13#ifndef _Z_UTIL_H
14#define _Z_UTIL_H 14#define _Z_UTIL_H
15 15
16#include "zlib.h" 16#include "zlib.h"
17 17
18#ifdef __GNUC__
19# define INLINE inline
20#else
21# define INLINE
22#endif
23
18#ifdef MSDOS 24#ifdef MSDOS
19# include <stddef.h> 25# include <stddef.h>
20# include <errno.h> 26# include <errno.h>
@@ -39,7 +45,7 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
39#define ERR_RETURN(strm,err) return (strm->msg=z_errmsg[1-err], err) 45#define ERR_RETURN(strm,err) return (strm->msg=z_errmsg[1-err], err)
40/* To be used only when the state is known to be valid */ 46/* To be used only when the state is known to be valid */
41 47
42 /* common constants */ 48 /* common constants */
43 49
44#define DEFLATED 8 50#define DEFLATED 8
45 51
@@ -62,7 +68,7 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
62#define MAX_MATCH 258 68#define MAX_MATCH 258
63/* The minimum and maximum match lengths */ 69/* The minimum and maximum match lengths */
64 70
65 /* target dependencies */ 71 /* target dependencies */
66 72
67#ifdef MSDOS 73#ifdef MSDOS
68# define OS_CODE 0x00 74# define OS_CODE 0x00
@@ -107,7 +113,7 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
107# define OS_CODE 0x0a 113# define OS_CODE 0x0a
108#endif 114#endif
109 115
110 /* Common defaults */ 116 /* Common defaults */
111 117
112#ifndef OS_CODE 118#ifndef OS_CODE
113# define OS_CODE 0x03 /* assume Unix */ 119# define OS_CODE 0x03 /* assume Unix */
@@ -126,7 +132,10 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
126# define zstrerror(errnum) "" 132# define zstrerror(errnum) ""
127#endif 133#endif
128 134
129#if defined(STDC) && !defined(HAVE_MEMCPY) 135#if defined(pyr) && !defined(NO_MEMCPY)
136# define NO_MEMCPY
137#endif
138#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
130# define HAVE_MEMCPY 139# define HAVE_MEMCPY
131#endif 140#endif
132#ifdef HAVE_MEMCPY 141#ifdef HAVE_MEMCPY