Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Update zlib to version 1.2.5

  • Loading branch information...
commit 46ad959976b2068204a8ea234b96d36a9b8709d6 1 parent 827a231
Gonzalo Paniagua Javier authored October 22, 2010
1  support/Makefile.am
@@ -71,7 +71,6 @@ ZLIB_SOURCES = \
71 71
 	adler32.c	\
72 72
 	compress.c	\
73 73
 	crc32.c		\
74  
-	gzio.c		\
75 74
 	uncompr.c	\
76 75
 	deflate.c	\
77 76
 	trees.c		\
38  support/adler32.c
... ...
@@ -1,21 +1,15 @@
1 1
 /* adler32.c -- compute the Adler-32 checksum of a data stream
2  
- * Copyright (C) 1995-2006 Mark Adler
  2
+ * Copyright (C) 1995-2007 Mark Adler
3 3
  * For conditions of distribution and use, see copyright notice in zlib.h
4 4
  */
5 5
 
6 6
 /* @(#) $Id$ */
7 7
 
8  
-#define ZLIB_INTERNAL
9  
-#include "zlib.h"
  8
+#include "zutil.h"
10 9
 
11 10
 #define local static
12 11
 
13  
-#ifdef _LARGEFILE64_SOURCE
14  
-   local uLong adler32_combine_(uLong adler1, uLong adler2, off64_t len2);
15  
-#else
16  
-   local uLong adler32_combine_(uLong adler1, uLong adler2, z_off_t len2);
17  
-#endif
18  
-
  12
+local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2);
19 13
 
20 14
 #define BASE 65521UL    /* largest prime smaller than 65536 */
21 15
 #define NMAX 5552
@@ -137,11 +131,7 @@ uLong ZEXPORT adler32(adler, buf, len)
137 131
 local uLong adler32_combine_(adler1, adler2, len2)
138 132
     uLong adler1;
139 133
     uLong adler2;
140  
-#ifdef _LARGEFILE64_SOURCE
141  
-    off64_t len2;
142  
-#else
143  
-    z_off_t len2;
144  
-#endif
  134
+    z_off64_t len2;
145 135
 {
146 136
     unsigned long sum1;
147 137
     unsigned long sum2;
@@ -154,10 +144,10 @@ local uLong adler32_combine_(adler1, adler2, len2)
154 144
     MOD(sum2);
155 145
     sum1 += (adler2 & 0xffff) + BASE - 1;
156 146
     sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
157  
-    if (sum1 > BASE) sum1 -= BASE;
158  
-    if (sum1 > BASE) sum1 -= BASE;
159  
-    if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
160  
-    if (sum2 > BASE) sum2 -= BASE;
  147
+    if (sum1 >= BASE) sum1 -= BASE;
  148
+    if (sum1 >= BASE) sum1 -= BASE;
  149
+    if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
  150
+    if (sum2 >= BASE) sum2 -= BASE;
161 151
     return sum1 | (sum2 << 16);
162 152
 }
163 153
 
@@ -170,20 +160,10 @@ uLong ZEXPORT adler32_combine(adler1, adler2, len2)
170 160
     return adler32_combine_(adler1, adler2, len2);
171 161
 }
172 162
 
173  
-#ifdef _LARGEFILE64_SOURCE
174 163
 uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
175 164
     uLong adler1;
176 165
     uLong adler2;
177  
-    off64_t len2;
  166
+    z_off64_t len2;
178 167
 {
179 168
     return adler32_combine_(adler1, adler2, len2);
180 169
 }
181  
-#else
182  
-uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
183  
-    uLong adler1;
184  
-    uLong adler2;
185  
-    z_off_t len2;
186  
-{
187  
-    return adler32_combine_(adler1, adler2, len2);
188  
-}
189  
-#endif
36  support/crc32.c
... ...
@@ -1,5 +1,5 @@
1 1
 /* crc32.c -- compute the CRC-32 of a data stream
2  
- * Copyright (C) 1995-2006 Mark Adler
  2
+ * Copyright (C) 1995-2006, 2010 Mark Adler
3 3
  * For conditions of distribution and use, see copyright notice in zlib.h
4 4
  *
5 5
  * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
@@ -53,7 +53,7 @@
53 53
 
54 54
 /* Definitions for doing the crc four data bytes at a time. */
55 55
 #ifdef BYFOUR
56  
-#  define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
  56
+#  define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \
57 57
                 (((w)&0xff00)<<8)+(((w)&0xff)<<24))
58 58
    local unsigned long crc32_little OF((unsigned long,
59 59
                         const unsigned char FAR *, unsigned));
@@ -68,11 +68,7 @@
68 68
 local unsigned long gf2_matrix_times OF((unsigned long *mat,
69 69
                                          unsigned long vec));
70 70
 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
71  
-#ifdef _LARGEFILE64_SOURCE
72  
-   local uLong crc32_combine_(uLong crc1, uLong crc2, off64_t len2);
73  
-#else
74  
-   local uLong crc32_combine_(uLong crc1, uLong crc2, z_off_t len2);
75  
-#endif
  71
+local uLong crc32_combine_(uLong crc1, uLong crc2, z_off64_t len2);
76 72
 
77 73
 
78 74
 #ifdef DYNAMIC_CRC_TABLE
@@ -225,7 +221,7 @@ const unsigned long FAR * ZEXPORT get_crc_table()
225 221
 unsigned long ZEXPORT crc32(crc, buf, len)
226 222
     unsigned long crc;
227 223
     const unsigned char FAR *buf;
228  
-    unsigned len;
  224
+    uInt len;
229 225
 {
230 226
     if (buf == Z_NULL) return 0UL;
231 227
 
@@ -376,23 +372,19 @@ local void gf2_matrix_square(square, mat)
376 372
 local uLong crc32_combine_(crc1, crc2, len2)
377 373
     uLong crc1;
378 374
     uLong crc2;
379  
-#ifdef _LARGEFILE64_SOURCE
380  
-    off64_t len2;
381  
-#else
382  
-    z_off_t len2;
383  
-#endif
  375
+    z_off64_t len2;
384 376
 {
385 377
     int n;
386 378
     unsigned long row;
387 379
     unsigned long even[GF2_DIM];    /* even-power-of-two zeros operator */
388 380
     unsigned long odd[GF2_DIM];     /* odd-power-of-two zeros operator */
389 381
 
390  
-    /* degenerate case */
391  
-    if (len2 == 0)
  382
+    /* degenerate case (also disallow negative lengths) */
  383
+    if (len2 <= 0)
392 384
         return crc1;
393 385
 
394 386
     /* put operator for one zero bit in odd */
395  
-    odd[0] = 0xedb88320L;           /* CRC-32 polynomial */
  387
+    odd[0] = 0xedb88320UL;          /* CRC-32 polynomial */
396 388
     row = 1;
397 389
     for (n = 1; n < GF2_DIM; n++) {
398 390
         odd[n] = row;
@@ -441,20 +433,10 @@ uLong ZEXPORT crc32_combine(crc1, crc2, len2)
441 433
     return crc32_combine_(crc1, crc2, len2);
442 434
 }
443 435
 
444  
-#ifdef _LARGEFILE64_SOURCE
445  
-uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
446  
-    uLong crc1;
447  
-    uLong crc2;
448  
-    off64_t len2;
449  
-{
450  
-    return crc32_combine_(crc1, crc2, len2);
451  
-}
452  
-#else
453 436
 uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
454 437
     uLong crc1;
455 438
     uLong crc2;
456  
-    z_off_t len2;
  439
+    z_off64_t len2;
457 440
 {
458 441
     return crc32_combine_(crc1, crc2, len2);
459 442
 }
460  
-#endif
220  support/deflate.c
... ...
@@ -1,5 +1,5 @@
1 1
 /* deflate.c -- compress data using the deflation algorithm
2  
- * Copyright (C) 1995-2006 Jean-loup Gailly.
  2
+ * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
3 3
  * For conditions of distribution and use, see copyright notice in zlib.h
4 4
  */
5 5
 
@@ -52,7 +52,7 @@
52 52
 #include "deflate.h"
53 53
 
54 54
 const char deflate_copyright[] =
55  
-   " deflate 1.2.3.3 Copyright 1995-2006 Jean-loup Gailly ";
  55
+   " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler ";
56 56
 /*
57 57
   If you use the zlib library in a product, an acknowledgment is welcome
58 58
   in the documentation of your product. If for some reason you cannot
@@ -79,19 +79,18 @@ local block_state deflate_fast   OF((deflate_state *s, int flush));
79 79
 #ifndef FASTEST
80 80
 local block_state deflate_slow   OF((deflate_state *s, int flush));
81 81
 #endif
  82
+local block_state deflate_rle    OF((deflate_state *s, int flush));
  83
+local block_state deflate_huff   OF((deflate_state *s, int flush));
82 84
 local void lm_init        OF((deflate_state *s));
83 85
 local void putShortMSB    OF((deflate_state *s, uInt b));
84 86
 local void flush_pending  OF((z_streamp strm));
85 87
 local int read_buf        OF((z_streamp strm, Bytef *buf, unsigned size));
86  
-#ifndef FASTEST
87 88
 #ifdef ASMV
88 89
       void match_init OF((void)); /* asm code initialization */
89 90
       uInt longest_match  OF((deflate_state *s, IPos cur_match));
90 91
 #else
91 92
 local uInt longest_match  OF((deflate_state *s, IPos cur_match));
92 93
 #endif
93  
-#endif
94  
-local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
95 94
 
96 95
 #ifdef DEBUG
97 96
 local  void check_match OF((deflate_state *s, IPos start, IPos match,
@@ -110,11 +109,6 @@ local  void check_match OF((deflate_state *s, IPos start, IPos match,
110 109
 #endif
111 110
 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
112 111
 
113  
-#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
114  
-/* Minimum amount of lookahead, except at the end of the input file.
115  
- * See deflate.c for comments about the MIN_MATCH+1.
116  
- */
117  
-
118 112
 /* Values for max_lazy_match, good_match and max_chain_length, depending on
119 113
  * the desired pack level (0..9). The values given below have been tuned to
120 114
  * exclude worst case performance for pathological files. Better values may be
@@ -288,6 +282,8 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
288 282
     s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
289 283
     s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
290 284
 
  285
+    s->high_water = 0;      /* nothing written to s->window yet */
  286
+
291 287
     s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
292 288
 
293 289
     overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
@@ -332,8 +328,8 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
332 328
         strm->adler = adler32(strm->adler, dictionary, dictLength);
333 329
 
334 330
     if (length < MIN_MATCH) return Z_OK;
335  
-    if (length > MAX_DIST(s)) {
336  
-        length = MAX_DIST(s);
  331
+    if (length > s->w_size) {
  332
+        length = s->w_size;
337 333
         dictionary += dictLength - length; /* use the tail of the dictionary */
338 334
     }
339 335
     zmemcpy(s->window, dictionary, length);
@@ -435,9 +431,10 @@ int ZEXPORT deflateParams(strm, level, strategy)
435 431
     }
436 432
     func = configuration_table[s->level].func;
437 433
 
438  
-    if (func != configuration_table[level].func && strm->total_in != 0) {
  434
+    if ((strategy != s->strategy || func != configuration_table[level].func) &&
  435
+        strm->total_in != 0) {
439 436
         /* Flush the last buffer: */
440  
-        err = deflate(strm, Z_PARTIAL_FLUSH);
  437
+        err = deflate(strm, Z_BLOCK);
441 438
     }
442 439
     if (s->level != level) {
443 440
         s->level = level;
@@ -513,16 +510,16 @@ uLong ZEXPORT deflateBound(strm, sourceLen)
513 510
         break;
514 511
     case 2:                                 /* gzip wrapper */
515 512
         wraplen = 18;
516  
-        if (s->gzhead != NULL) {            /* user-supplied gzip header */
517  
-            if (s->gzhead->extra != NULL)
  513
+        if (s->gzhead != Z_NULL) {          /* user-supplied gzip header */
  514
+            if (s->gzhead->extra != Z_NULL)
518 515
                 wraplen += 2 + s->gzhead->extra_len;
519 516
             str = s->gzhead->name;
520  
-            if (str != NULL)
  517
+            if (str != Z_NULL)
521 518
                 do {
522 519
                     wraplen++;
523 520
                 } while (*str++);
524 521
             str = s->gzhead->comment;
525  
-            if (str != NULL)
  522
+            if (str != Z_NULL)
526 523
                 do {
527 524
                     wraplen++;
528 525
                 } while (*str++);
@@ -539,7 +536,8 @@ uLong ZEXPORT deflateBound(strm, sourceLen)
539 536
         return complen + wraplen;
540 537
 
541 538
     /* default settings: return tight bound for that case */
542  
-    return compressBound(sourceLen) - 6 + wraplen;
  539
+    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
  540
+           (sourceLen >> 25) + 13 - 6 + wraplen;
543 541
 }
544 542
 
545 543
 /* =========================================================================
@@ -589,7 +587,7 @@ int ZEXPORT deflate (strm, flush)
589 587
     deflate_state *s;
590 588
 
591 589
     if (strm == Z_NULL || strm->state == Z_NULL ||
592  
-        flush > Z_FINISH || flush < 0) {
  590
+        flush > Z_BLOCK || flush < 0) {
593 591
         return Z_STREAM_ERROR;
594 592
     }
595 593
     s = strm->state;
@@ -613,7 +611,7 @@ int ZEXPORT deflate (strm, flush)
613 611
             put_byte(s, 31);
614 612
             put_byte(s, 139);
615 613
             put_byte(s, 8);
616  
-            if (s->gzhead == NULL) {
  614
+            if (s->gzhead == Z_NULL) {
617 615
                 put_byte(s, 0);
618 616
                 put_byte(s, 0);
619 617
                 put_byte(s, 0);
@@ -640,7 +638,7 @@ int ZEXPORT deflate (strm, flush)
640 638
                             (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
641 639
                              4 : 0));
642 640
                 put_byte(s, s->gzhead->os & 0xff);
643  
-                if (s->gzhead->extra != NULL) {
  641
+                if (s->gzhead->extra != Z_NULL) {
644 642
                     put_byte(s, s->gzhead->extra_len & 0xff);
645 643
                     put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
646 644
                 }
@@ -682,7 +680,7 @@ int ZEXPORT deflate (strm, flush)
682 680
     }
683 681
 #ifdef GZIP
684 682
     if (s->status == EXTRA_STATE) {
685  
-        if (s->gzhead->extra != NULL) {
  683
+        if (s->gzhead->extra != Z_NULL) {
686 684
             uInt beg = s->pending;  /* start of bytes to update crc */
687 685
 
688 686
             while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
@@ -710,7 +708,7 @@ int ZEXPORT deflate (strm, flush)
710 708
             s->status = NAME_STATE;
711 709
     }
712 710
     if (s->status == NAME_STATE) {
713  
-        if (s->gzhead->name != NULL) {
  711
+        if (s->gzhead->name != Z_NULL) {
714 712
             uInt beg = s->pending;  /* start of bytes to update crc */
715 713
             int val;
716 714
 
@@ -741,7 +739,7 @@ int ZEXPORT deflate (strm, flush)
741 739
             s->status = COMMENT_STATE;
742 740
     }
743 741
     if (s->status == COMMENT_STATE) {
744  
-        if (s->gzhead->comment != NULL) {
  742
+        if (s->gzhead->comment != Z_NULL) {
745 743
             uInt beg = s->pending;  /* start of bytes to update crc */
746 744
             int val;
747 745
 
@@ -819,7 +817,9 @@ int ZEXPORT deflate (strm, flush)
819 817
         (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
820 818
         block_state bstate;
821 819
 
822  
-        bstate = (*(configuration_table[s->level].func))(s, flush);
  820
+        bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
  821
+                    (s->strategy == Z_RLE ? deflate_rle(s, flush) :
  822
+                        (*(configuration_table[s->level].func))(s, flush));
823 823
 
824 824
         if (bstate == finish_started || bstate == finish_done) {
825 825
             s->status = FINISH_STATE;
@@ -840,13 +840,17 @@ int ZEXPORT deflate (strm, flush)
840 840
         if (bstate == block_done) {
841 841
             if (flush == Z_PARTIAL_FLUSH) {
842 842
                 _tr_align(s);
843  
-            } else { /* FULL_FLUSH or SYNC_FLUSH */
  843
+            } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
844 844
                 _tr_stored_block(s, (char*)0, 0L, 0);
845 845
                 /* For a full flush, this empty block will be recognized
846 846
                  * as a special marker by inflate_sync().
847 847
                  */
848 848
                 if (flush == Z_FULL_FLUSH) {
849 849
                     CLEAR_HASH(s);             /* forget history */
  850
+                    if (s->lookahead == 0) {
  851
+                        s->strstart = 0;
  852
+                        s->block_start = 0L;
  853
+                    }
850 854
                 }
851 855
             }
852 856
             flush_pending(strm);
@@ -1199,12 +1203,13 @@ local uInt longest_match(s, cur_match)
1199 1203
     return s->lookahead;
1200 1204
 }
1201 1205
 #endif /* ASMV */
1202  
-#endif /* FASTEST */
  1206
+
  1207
+#else /* FASTEST */
1203 1208
 
1204 1209
 /* ---------------------------------------------------------------------------
1205  
- * Optimized version for level == 1 or strategy == Z_RLE only
  1210
+ * Optimized version for FASTEST only
1206 1211
  */
1207  
-local uInt longest_match_fast(s, cur_match)
  1212
+local uInt longest_match(s, cur_match)
1208 1213
     deflate_state *s;
1209 1214
     IPos cur_match;                             /* current match */
1210 1215
 {
@@ -1257,6 +1262,8 @@ local uInt longest_match_fast(s, cur_match)
1257 1262
     return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1258 1263
 }
1259 1264
 
  1265
+#endif /* FASTEST */
  1266
+
1260 1267
 #ifdef DEBUG
1261 1268
 /* ===========================================================================
1262 1269
  * Check that the match at match_start is indeed a match.
@@ -1335,7 +1342,6 @@ local void fill_window(s)
1335 1342
                later. (Using level 0 permanently is not an optimal usage of
1336 1343
                zlib, so we don't care about this pathological case.)
1337 1344
              */
1338  
-            /* %%% avoid this when Z_RLE */
1339 1345
             n = s->hash_size;
1340 1346
             p = &s->head[n];
1341 1347
             do {
@@ -1387,27 +1393,61 @@ local void fill_window(s)
1387 1393
          */
1388 1394
 
1389 1395
     } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
  1396
+
  1397
+    /* If the WIN_INIT bytes after the end of the current data have never been
  1398
+     * written, then zero those bytes in order to avoid memory check reports of
  1399
+     * the use of uninitialized (or uninitialised as Julian writes) bytes by
  1400
+     * the longest match routines.  Update the high water mark for the next
  1401
+     * time through here.  WIN_INIT is set to MAX_MATCH since the longest match
  1402
+     * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
  1403
+     */
  1404
+    if (s->high_water < s->window_size) {
  1405
+        ulg curr = s->strstart + (ulg)(s->lookahead);
  1406
+        ulg init;
  1407
+
  1408
+        if (s->high_water < curr) {
  1409
+            /* Previous high water mark below current data -- zero WIN_INIT
  1410
+             * bytes or up to end of window, whichever is less.
  1411
+             */
  1412
+            init = s->window_size - curr;
  1413
+            if (init > WIN_INIT)
  1414
+                init = WIN_INIT;
  1415
+            zmemzero(s->window + curr, (unsigned)init);
  1416
+            s->high_water = curr + init;
  1417
+        }
  1418
+        else if (s->high_water < (ulg)curr + WIN_INIT) {
  1419
+            /* High water mark at or above current data, but below current data
  1420
+             * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
  1421
+             * to end of window, whichever is less.
  1422
+             */
  1423
+            init = (ulg)curr + WIN_INIT - s->high_water;
  1424
+            if (init > s->window_size - s->high_water)
  1425
+                init = s->window_size - s->high_water;
  1426
+            zmemzero(s->window + s->high_water, (unsigned)init);
  1427
+            s->high_water += init;
  1428
+        }
  1429
+    }
1390 1430
 }
1391 1431
 
1392 1432
 /* ===========================================================================
1393 1433
  * Flush the current block, with given end-of-file flag.
1394 1434
  * IN assertion: strstart is set to the end of the current match.
1395 1435
  */
1396  
-#define FLUSH_BLOCK_ONLY(s, eof) { \
  1436
+#define FLUSH_BLOCK_ONLY(s, last) { \
1397 1437
    _tr_flush_block(s, (s->block_start >= 0L ? \
1398 1438
                    (charf *)&s->window[(unsigned)s->block_start] : \
1399 1439
                    (charf *)Z_NULL), \
1400 1440
                 (ulg)((long)s->strstart - s->block_start), \
1401  
-                (eof)); \
  1441
+                (last)); \
1402 1442
    s->block_start = s->strstart; \
1403 1443
    flush_pending(s->strm); \
1404 1444
    Tracev((stderr,"[FLUSH]")); \
1405 1445
 }
1406 1446
 
1407 1447
 /* Same but force premature exit if necessary. */
1408  
-#define FLUSH_BLOCK(s, eof) { \
1409  
-   FLUSH_BLOCK_ONLY(s, eof); \
1410  
-   if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
  1448
+#define FLUSH_BLOCK(s, last) { \
  1449
+   FLUSH_BLOCK_ONLY(s, last); \
  1450
+   if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1411 1451
 }
1412 1452
 
1413 1453
 /* ===========================================================================
@@ -1481,7 +1521,7 @@ local block_state deflate_fast(s, flush)
1481 1521
     deflate_state *s;
1482 1522
     int flush;
1483 1523
 {
1484  
-    IPos hash_head = NIL; /* head of the hash chain */
  1524
+    IPos hash_head;       /* head of the hash chain */
1485 1525
     int bflush;           /* set if current block must be flushed */
1486 1526
 
1487 1527
     for (;;) {
@@ -1501,6 +1541,7 @@ local block_state deflate_fast(s, flush)
1501 1541
         /* Insert the string window[strstart .. strstart+2] in the
1502 1542
          * dictionary, and set hash_head to the head of the hash chain:
1503 1543
          */
  1544
+        hash_head = NIL;
1504 1545
         if (s->lookahead >= MIN_MATCH) {
1505 1546
             INSERT_STRING(s, s->strstart, hash_head);
1506 1547
         }
@@ -1513,19 +1554,8 @@ local block_state deflate_fast(s, flush)
1513 1554
              * of window index 0 (in particular we have to avoid a match
1514 1555
              * of the string with itself at the start of the input file).
1515 1556
              */
1516  
-#ifdef FASTEST
1517  
-            if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
1518  
-                (s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
1519  
-                s->match_length = longest_match_fast (s, hash_head);
1520  
-            }
1521  
-#else
1522  
-            if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
1523  
-                s->match_length = longest_match (s, hash_head);
1524  
-            } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
1525  
-                s->match_length = longest_match_fast (s, hash_head);
1526  
-            }
1527  
-#endif
1528  
-            /* longest_match() or longest_match_fast() sets match_start */
  1557
+            s->match_length = longest_match (s, hash_head);
  1558
+            /* longest_match() sets match_start */
1529 1559
         }
1530 1560
         if (s->match_length >= MIN_MATCH) {
1531 1561
             check_match(s, s->strstart, s->match_start, s->match_length);
@@ -1587,7 +1617,7 @@ local block_state deflate_slow(s, flush)
1587 1617
     deflate_state *s;
1588 1618
     int flush;
1589 1619
 {
1590  
-    IPos hash_head = NIL;    /* head of hash chain */
  1620
+    IPos hash_head;          /* head of hash chain */
1591 1621
     int bflush;              /* set if current block must be flushed */
1592 1622
 
1593 1623
     /* Process the input block. */
@@ -1608,6 +1638,7 @@ local block_state deflate_slow(s, flush)
1608 1638
         /* Insert the string window[strstart .. strstart+2] in the
1609 1639
          * dictionary, and set hash_head to the head of the hash chain:
1610 1640
          */
  1641
+        hash_head = NIL;
1611 1642
         if (s->lookahead >= MIN_MATCH) {
1612 1643
             INSERT_STRING(s, s->strstart, hash_head);
1613 1644
         }
@@ -1623,12 +1654,8 @@ local block_state deflate_slow(s, flush)
1623 1654
              * of window index 0 (in particular we have to avoid a match
1624 1655
              * of the string with itself at the start of the input file).
1625 1656
              */
1626  
-            if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
1627  
-                s->match_length = longest_match (s, hash_head);
1628  
-            } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
1629  
-                s->match_length = longest_match_fast (s, hash_head);
1630  
-            }
1631  
-            /* longest_match() or longest_match_fast() sets match_start */
  1657
+            s->match_length = longest_match (s, hash_head);
  1658
+            /* longest_match() sets match_start */
1632 1659
 
1633 1660
             if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1634 1661
 #if TOO_FAR <= 32767
@@ -1706,7 +1733,6 @@ local block_state deflate_slow(s, flush)
1706 1733
 }
1707 1734
 #endif /* FASTEST */
1708 1735
 
1709  
-#if 0
1710 1736
 /* ===========================================================================
1711 1737
  * For Z_RLE, simply look for runs of bytes, generate matches only of distance
1712 1738
  * one.  Do not maintain a hash table.  (It will be regenerated if this run of
@@ -1716,11 +1742,9 @@ local block_state deflate_rle(s, flush)
1716 1742
     deflate_state *s;
1717 1743
     int flush;
1718 1744
 {
1719  
-    int bflush;         /* set if current block must be flushed */
1720  
-    uInt run;           /* length of run */
1721  
-    uInt max;           /* maximum length of run */
1722  
-    uInt prev;          /* byte at distance one to match */
1723  
-    Bytef *scan;        /* scan for end of run */
  1745
+    int bflush;             /* set if current block must be flushed */
  1746
+    uInt prev;              /* byte at distance one to match */
  1747
+    Bytef *scan, *strend;   /* scan goes up to strend for length of run */
1724 1748
 
1725 1749
     for (;;) {
1726 1750
         /* Make sure that we always have enough lookahead, except
@@ -1736,23 +1760,33 @@ local block_state deflate_rle(s, flush)
1736 1760
         }
1737 1761
 
1738 1762
         /* See how many times the previous byte repeats */
1739  
-        run = 0;
1740  
-        if (s->strstart > 0) {      /* if there is a previous byte, that is */
1741  
-            max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
  1763
+        s->match_length = 0;
  1764
+        if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
1742 1765
             scan = s->window + s->strstart - 1;
1743  
-            prev = *scan++;
1744  
-            do {
1745  
-                if (*scan++ != prev)
1746  
-                    break;
1747  
-            } while (++run < max);
  1766
+            prev = *scan;
  1767
+            if (prev == *++scan && prev == *++scan && prev == *++scan) {
  1768
+                strend = s->window + s->strstart + MAX_MATCH;
  1769
+                do {
  1770
+                } while (prev == *++scan && prev == *++scan &&
  1771
+                         prev == *++scan && prev == *++scan &&
  1772
+                         prev == *++scan && prev == *++scan &&
  1773
+                         prev == *++scan && prev == *++scan &&
  1774
+                         scan < strend);
  1775
+                s->match_length = MAX_MATCH - (int)(strend - scan);
  1776
+                if (s->match_length > s->lookahead)
  1777
+                    s->match_length = s->lookahead;
  1778
+            }
1748 1779
         }
1749 1780
 
1750 1781
         /* Emit match if have run of MIN_MATCH or longer, else emit literal */
1751  
-        if (run >= MIN_MATCH) {
1752  
-            check_match(s, s->strstart, s->strstart - 1, run);
1753  
-            _tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
1754  
-            s->lookahead -= run;
1755  
-            s->strstart += run;
  1782
+        if (s->match_length >= MIN_MATCH) {
  1783
+            check_match(s, s->strstart, s->strstart - 1, s->match_length);
  1784
+
  1785
+            _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
  1786
+
  1787
+            s->lookahead -= s->match_length;
  1788
+            s->strstart += s->match_length;
  1789
+            s->match_length = 0;
1756 1790
         } else {
1757 1791
             /* No match, output a literal byte */
1758 1792
             Tracevv((stderr,"%c", s->window[s->strstart]));
@@ -1765,4 +1799,36 @@ local block_state deflate_rle(s, flush)
1765 1799
     FLUSH_BLOCK(s, flush == Z_FINISH);
1766 1800
     return flush == Z_FINISH ? finish_done : block_done;
1767 1801
 }
1768  
-#endif
  1802
+
  1803
+/* ===========================================================================
  1804
+ * For Z_HUFFMAN_ONLY, do not look for matches.  Do not maintain a hash table.
  1805
+ * (It will be regenerated if this run of deflate switches away from Huffman.)
  1806
+ */
  1807
+local block_state deflate_huff(s, flush)
  1808
+    deflate_state *s;
  1809
+    int flush;
  1810
+{
  1811
+    int bflush;             /* set if current block must be flushed */
  1812
+
  1813
+    for (;;) {
  1814
+        /* Make sure that we have a literal to write. */
  1815
+        if (s->lookahead == 0) {
  1816
+            fill_window(s);
  1817
+            if (s->lookahead == 0) {
  1818
+                if (flush == Z_NO_FLUSH)
  1819
+                    return need_more;
  1820
+                break;      /* flush the current block */
  1821
+            }
  1822
+        }
  1823
+
  1824
+        /* Output a literal byte */
  1825
+        s->match_length = 0;
  1826
+        Tracevv((stderr,"%c", s->window[s->strstart]));
  1827
+        _tr_tally_lit (s, s->window[s->strstart], bflush);
  1828
+        s->lookahead--;
  1829
+        s->strstart++;
  1830
+        if (bflush) FLUSH_BLOCK(s, 0);
  1831
+    }
  1832
+    FLUSH_BLOCK(s, flush == Z_FINISH);
  1833
+    return flush == Z_FINISH ? finish_done : block_done;
  1834
+}
35  support/deflate.h
... ...
@@ -1,5 +1,5 @@
1 1
 /* deflate.h -- internal compression state
2  
- * Copyright (C) 1995-2005 Jean-loup Gailly
  2
+ * Copyright (C) 1995-2010 Jean-loup Gailly
3 3
  * For conditions of distribution and use, see copyright notice in zlib.h
4 4
  */
5 5
 
@@ -260,6 +260,13 @@ typedef struct internal_state {
260 260
      * are always zero.
261 261
      */
262 262
 
  263
+    ulg high_water;
  264
+    /* High water mark offset in window for initialized bytes -- bytes above
  265
+     * this are set to zero in order to avoid memory check warnings when
  266
+     * longest match routines access bytes past the input.  This is then
  267
+     * updated to the new high water mark.
  268
+     */
  269
+
263 270
 } FAR deflate_state;
264 271
 
265 272
 /* Output a byte on the stream.
@@ -278,14 +285,18 @@ typedef struct internal_state {
278 285
  * distances are limited to MAX_DIST instead of WSIZE.
279 286
  */
280 287
 
  288
+#define WIN_INIT MAX_MATCH
  289
+/* Number of bytes after end of data in window to initialize in order to avoid
  290
+   memory checker errors from longest match routines */
  291
+
281 292
         /* in trees.c */
282  
-void _tr_init         OF((deflate_state *s));
283  
-int  _tr_tally        OF((deflate_state *s, unsigned dist, unsigned lc));
284  
-void _tr_flush_block  OF((deflate_state *s, charf *buf, ulg stored_len,
285  
-                          int eof));
286  
-void _tr_align        OF((deflate_state *s));
287  
-void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
288  
-                          int eof));
  293
+void ZLIB_INTERNAL _tr_init OF((deflate_state *s));
  294
+int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
  295
+void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
  296
+                        ulg stored_len, int last));
  297
+void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
  298
+void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
  299
+                        ulg stored_len, int last));
289 300
 
290 301
 #define d_code(dist) \
291 302
    ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
@@ -298,11 +309,11 @@ void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
298 309
 /* Inline versions of _tr_tally for speed: */
299 310
 
300 311
 #if defined(GEN_TREES_H) || !defined(STDC)
301  
-  extern uch _length_code[];
302  
-  extern uch _dist_code[];
  312
+  extern uch ZLIB_INTERNAL _length_code[];
  313
+  extern uch ZLIB_INTERNAL _dist_code[];
303 314
 #else
304  
-  extern const uch _length_code[];
305  
-  extern const uch _dist_code[];
  315
+  extern const uch ZLIB_INTERNAL _length_code[];
  316
+  extern const uch ZLIB_INTERNAL _dist_code[];
306 317
 #endif
307 318
 
308 319
 # define _tr_tally_lit(s, c, flush) \
1,122  support/gzio.c
... ...
@@ -1,1122 +0,0 @@
1  
-#ifdef MONO_DOES_NOT_NEED_THIS
2  
-/* gzio.c -- IO on .gz files
3  
- * Copyright (C) 1995-2006 Jean-loup Gailly.
4  
- * For conditions of distribution and use, see copyright notice in zlib.h
5  
- *
6  
- * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
7  
- */
8  
-
9  
-/* @(#) $Id$ */
10  
-
11  
-#ifdef _LARGEFILE64_SOURCE
12  
-#  ifndef _LARGEFILE_SOURCE
13  
-#    define _LARGEFILE_SOURCE
14  
-#  endif
15  
-#  ifdef _FILE_OFFSET_BITS
16  
-#    undef _FILE_OFFSET_BITS
17  
-#  endif
18  
-#endif
19  
-
20  
-#include "zutil.h"
21  
-#include <stdio.h>
22  
-
23  
-#ifdef NO_DEFLATE       /* for compatibility with old definition */
24  
-#  define NO_GZCOMPRESS
25  
-#endif
26  
-
27  
-#ifndef NO_DUMMY_DECL
28  
-struct internal_state {int dummy;}; /* for buggy compilers */
29  
-#endif
30  
-
31  
-#ifndef Z_BUFSIZE
32  
-#  ifdef MAXSEG_64K
33  
-#    define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
34  
-#  else
35  
-#    define Z_BUFSIZE 16384
36  
-#  endif
37  
-#endif
38  
-#ifndef Z_PRINTF_BUFSIZE
39  
-#  define Z_PRINTF_BUFSIZE 4096
40  
-#endif
41  
-
42  
-#ifdef __MVS__
43  
-#  pragma map (fdopen , "\174\174FDOPEN")
44  
-   FILE *fdopen(int, const char *);
45  
-#endif
46  
-
47  
-#ifndef STDC
48  
-extern voidp  malloc OF((uInt size));
49  
-extern void   free   OF((voidpf ptr));
50  
-#endif
51  
-
52  
-#ifdef NO_FSEEKO
53  
-#  define FSEEK fseek
54  
-#  define FTELL ftell
55  
-#else
56  
-#  define FSEEK fseeko
57  
-#  define FTELL ftello
58  
-#endif
59  
-
60  
-#define ALLOC(size) malloc(size)
61  
-#define TRYFREE(p) {if (p) free(p);}
62  
-
63  
-static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
64  
-
65  
-/* gzip flag byte */
66  
-#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
67  
-#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
68  
-#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
69  
-#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
70  
-#define COMMENT      0x10 /* bit 4 set: file comment present */
71  
-#define RESERVED     0xE0 /* bits 5..7: reserved */
72  
-
73  
-typedef struct gz_stream {
74  
-    z_stream stream;
75  
-    int      z_err;   /* error code for last stream operation */
76  
-    int      z_eof;   /* set if end of input file */
77  
-    FILE     *file;   /* .gz file */
78  
-    Byte     *inbuf;  /* input buffer */
79  
-    Byte     *outbuf; /* output buffer */
80  
-    uLong    crc;     /* crc32 of uncompressed data */
81  
-    char     *msg;    /* error message */
82  
-    char     *path;   /* path name for debugging only */
83  
-    int      transparent; /* 1 if input file is not a .gz file */
84  
-    char     mode;    /* 'w' or 'r' */
85  
-#ifdef _LARGEFILE64_SOURCE
86  
-    off64_t  start;   /* start of compressed data in file (header skipped) */
87  
-    off64_t  in;      /* bytes into deflate or inflate */
88  
-    off64_t  out;     /* bytes out of deflate or inflate */
89  
-#else
90  
-    z_off_t  start;   /* start of compressed data in file (header skipped) */
91  
-    z_off_t  in;      /* bytes into deflate or inflate */
92  
-    z_off_t  out;     /* bytes out of deflate or inflate */
93  
-#endif
94  
-    int      back;    /* one character push-back */
95  
-    int      last;    /* true if push-back is last character */
96  
-} gz_stream;
97  
-
98  
-
99  
-local gzFile gz_open      OF((const char *path, const char *mode, int fd,
100  
-                              int use64));
101  
-#ifdef _LARGEFILE64_SOURCE
102  
-local off64_t gz_seek OF((gzFile file, off64_t offset, int whence, int use64));
103  
-#else
104  
-local z_off_t gz_seek OF((gzFile file, z_off_t offset, int whence, int use64));
105  
-#endif
106  
-local int do_flush        OF((gzFile file, int flush));
107  
-local int    get_byte     OF((gz_stream *s));
108  
-local void   check_header OF((gz_stream *s));
109  
-local int    destroy      OF((gz_stream *s));
110  
-local void   putLong      OF((FILE *file, uLong x));
111  
-local uLong  getLong      OF((gz_stream *s));
112  
-
113  
-/* ===========================================================================
114  
-     Opens a gzip (.gz) file for reading or writing. The mode parameter
115  
-   is as in fopen ("rb" or "wb"). The file is given either by file descriptor
116  
-   or path name (if fd == -1).
117  
-     gz_open returns NULL if the file could not be opened or if there was
118  
-   insufficient memory to allocate the (de)compression state; errno
119  
-   can be checked to distinguish the two cases (if errno is zero, the
120  
-   zlib error is Z_MEM_ERROR).
121  
-*/
122  
-local gzFile gz_open (path, mode, fd, use64)
123  
-    const char *path;
124  
-    const char *mode;
125  
-    int  fd;
126  
-    int use64;
127  
-{
128  
-    int err;
129  
-    int level = Z_DEFAULT_COMPRESSION; /* compression level */
130  
-    int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
131  
-    char *p = (char*)mode;
132  
-    gz_stream *s;
133  
-    char fmode[80]; /* copy of mode, without the compression level */
134  
-    char *m = fmode;
135  
-
136  
-    if (!path || !mode) return Z_NULL;
137  
-
138  
-    s = (gz_stream *)ALLOC(sizeof(gz_stream));
139  
-    if (!s) return Z_NULL;
140  
-
141  
-    s->stream.zalloc = (alloc_func)0;
142  
-    s->stream.zfree = (free_func)0;
143  
-    s->stream.opaque = (voidpf)0;
144  
-    s->stream.next_in = s->inbuf = Z_NULL;
145  
-    s->stream.next_out = s->outbuf = Z_NULL;
146  
-    s->stream.avail_in = s->stream.avail_out = 0;
147  
-    s->file = NULL;
148  
-    s->z_err = Z_OK;
149  
-    s->z_eof = 0;
150  
-    s->in = 0;
151  
-    s->out = 0;
152  
-    s->back = EOF;
153  
-    s->crc = crc32(0L, Z_NULL, 0);
154  
-    s->msg = NULL;
155  
-    s->transparent = 0;
156  
-
157  
-    s->path = (char*)ALLOC(strlen(path)+1);
158  
-    if (s->path == NULL) {
159  
-        return destroy(s), (gzFile)Z_NULL;
160  
-    }
161  
-    strcpy(s->path, path); /* do this early for debugging */
162  
-
163  
-    s->mode = '\0';
164  
-    do {
165  
-        if (*p == 'r') s->mode = 'r';
166  
-        if (*p == 'w' || *p == 'a') s->mode = 'w';
167  
-        if (*p >= '0' && *p <= '9') {
168  
-            level = *p - '0';
169  
-        } else if (*p == 'f') {
170  
-          strategy = Z_FILTERED;
171  
-        } else if (*p == 'h') {
172  
-          strategy = Z_HUFFMAN_ONLY;
173  
-        } else if (*p == 'R') {
174  
-          strategy = Z_RLE;
175  
-        } else {
176  
-            *m++ = *p; /* copy the mode */
177  
-        }
178  
-    } while (*p++ && m != fmode + sizeof(fmode));
179  
-    if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
180  
-
181  
-    if (s->mode == 'w') {
182  
-#ifdef NO_GZCOMPRESS
183  
-        err = Z_STREAM_ERROR;
184  
-#else
185  
-        err = deflateInit2(&(s->stream), level,
186  
-                           Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
187  
-        /* windowBits is passed < 0 to suppress zlib header */
188  
-
189  
-        s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
190  
-#endif
191  
-        if (err != Z_OK || s->outbuf == Z_NULL) {
192  
-            return destroy(s), (gzFile)Z_NULL;
193  
-        }
194  
-    } else {
195  
-        s->stream.next_in  = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
196  
-
197  
-        err = inflateInit2(&(s->stream), -MAX_WBITS);
198  
-        /* windowBits is passed < 0 to tell that there is no zlib header */
199  
-        if (err != Z_OK || s->inbuf == Z_NULL) {
200  
-            return destroy(s), (gzFile)Z_NULL;
201  
-        }
202  
-    }
203  
-    s->stream.avail_out = Z_BUFSIZE;
204  
-
205  
-    errno = 0;
206  
-    s->file = fd < 0 ? (use64 ? F_OPEN64(path, fmode) : F_OPEN(path, fmode)) :
207  
-              (FILE*)fdopen(fd, fmode);
208  
-
209  
-    if (s->file == NULL) {
210  
-        return destroy(s), (gzFile)Z_NULL;
211  
-    }
212  
-    if (s->mode == 'w') {
213  
-        /* Write a very simple .gz header:
214  
-         */
215  
-        fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
216  
-             Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, level == 9 ? 2 :
217  
-                            (strategy >= Z_HUFFMAN_ONLY ||
218  
-                             (level != Z_DEFAULT_COMPRESSION && level < 2) ?
219  
-                             4 : 0) /*xflags*/, OS_CODE);
220  
-        s->start = 10L;
221  
-        /* We use 10L instead of ftell(s->file) to because ftell causes an
222  
-         * fflush on some systems. This version of the library doesn't use
223  
-         * start anyway in write mode, so this initialization is not
224  
-         * necessary.
225  
-         */
226  
-    } else {
227  
-        check_header(s); /* skip the .gz header */
228  
-        s->start = FTELL(s->file) - s->stream.avail_in;
229  
-    }
230  
-
231  
-    return (gzFile)s;
232  
-}
233  
-
234  
-/* ===========================================================================
235  
-     Opens a gzip (.gz) file for reading or writing.
236  
-*/
237  
-gzFile ZEXPORT gzopen (path, mode)
238  
-    const char *path;
239  
-    const char *mode;
240  
-{
241  
-    return gz_open (path, mode, -1, 0);
242  
-}
243  
-
244  
-/* ===========================================================================
245  
-     Opens a gzip (.gz) file for reading or writing for 64-bit offsets
246  
-*/
247  
-gzFile ZEXPORT gzopen64 (path, mode)
248  
-    const char *path;
249  
-    const char *mode;
250  
-{
251  
-    return gz_open (path, mode, -1, 1);
252  
-}
253  
-
254  
-/* ===========================================================================
255  
-     Associate a gzFile with the file descriptor fd. fd is not dup'ed here
256  
-   to mimic the behavio(u)r of fdopen.
257  
-*/
258  
-gzFile ZEXPORT gzdopen (fd, mode)
259  
-    int fd;
260  
-    const char *mode;
261  
-{
262  
-    char name[46];      /* allow for up to 128-bit integers */
263  
-
264  
-    if (fd < 0) return (gzFile)Z_NULL;
265  
-    sprintf(name, "<fd:%d>", fd); /* for debugging */
266  
-
267  
-    return gz_open (name, mode, fd, 0);
268  
-}
269  
-
270  
-/* ===========================================================================
271  
- * Update the compression level and strategy
272  
- */
273  
-int ZEXPORT gzsetparams (file, level, strategy)
274  
-    gzFile file;
275  
-    int level;
276  
-    int strategy;
277  
-{
278  
-    gz_stream *s = (gz_stream*)file;
279  
-
280  
-    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
281  
-
282  
-    /* Make room to allow flushing */
283  
-    if (s->stream.avail_out == 0) {
284  
-
285  
-        s->stream.next_out = s->outbuf;
286  
-        if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
287  
-            s->z_err = Z_ERRNO;
288  
-        }
289  
-        s->stream.avail_out = Z_BUFSIZE;
290  
-    }
291  
-
292  
-    return deflateParams (&(s->stream), level, strategy);
293  
-}
294  
-
295  
-/* ===========================================================================
296  
-     Read a byte from a gz_stream; update next_in and avail_in. Return EOF
297  
-   for end of file.
298  
-   IN assertion: the stream s has been successfully opened for reading.
299  
-*/
300  
-local int get_byte(s)
301  
-    gz_stream *s;
302  
-{
303  
-    if (s->z_eof) return EOF;
304  
-    if (s->stream.avail_in == 0) {
305  
-        errno = 0;
306  
-        s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
307  
-        if (s->stream.avail_in == 0) {
308  
-            s->z_eof = 1;
309  
-            if (ferror(s->file)) s->z_err = Z_ERRNO;
310  
-            return EOF;
311  
-        }
312  
-        s->stream.next_in = s->inbuf;
313  
-    }
314  
-    s->stream.avail_in--;
315  
-    return *(s->stream.next_in)++;
316  
-}
317  
-
318  
-/* ===========================================================================
319  
-      Check the gzip header of a gz_stream opened for reading. Set the stream
320  
-    mode to transparent if the gzip magic header is not present; set s->err
321  
-    to Z_DATA_ERROR if the magic header is present but the rest of the header
322  
-    is incorrect.
323  
-    IN assertion: the stream s has already been created successfully;
324  
-       s->stream.avail_in is zero for the first time, but may be non-zero
325  
-       for concatenated .gz files.
326  
-*/
327  
-local void check_header(s)
328  
-    gz_stream *s;
329  
-{
330  
-    int method; /* method byte */
331  
-    int flags;  /* flags byte */
332  
-    uInt len;
333  
-    int c;
334  
-
335  
-    /* Assure two bytes in the buffer so we can peek ahead -- handle case
336  
-       where first byte of header is at the end of the buffer after the last
337  
-       gzip segment */
338  
-    len = s->stream.avail_in;
339  
-    if (len < 2) {
340  
-        if (len) s->inbuf[0] = s->stream.next_in[0];
341  
-        errno = 0;
342  
-        len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
343  
-        if (len == 0) s->z_eof = 1;
344  
-        if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
345  
-        s->stream.avail_in += len;
346  
-        s->stream.next_in = s->inbuf;
347  
-        if (s->stream.avail_in < 2) {
348  
-            s->transparent = s->stream.avail_in;
349  
-            return;
350  
-        }
351  
-    }
352  
-
353  
-    /* Peek ahead to check the gzip magic header */
354  
-    if (s->stream.next_in[0] != gz_magic[0] ||
355  
-        s->stream.next_in[1] != gz_magic[1]) {
356  
-        s->transparent = 1;
357  
-        return;
358  
-    }
359  
-    s->stream.avail_in -= 2;
360  
-    s->stream.next_in += 2;
361  
-
362  
-    /* Check the rest of the gzip header */
363  
-    method = get_byte(s);
364  
-    flags = get_byte(s);
365  
-    if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
366  
-        s->z_err = Z_DATA_ERROR;
367  
-        return;
368  
-    }
369  
-
370  
-    /* Discard time, xflags and OS code: */
371  
-    for (len = 0; len < 6; len++) (void)get_byte(s);
372  
-
373  
-    if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
374  
-        len  =  (uInt)get_byte(s);
375  
-        len += ((uInt)get_byte(s))<<8;
376  
-        /* len is garbage if EOF but the loop below will quit anyway */
377  
-        while (len-- != 0 && get_byte(s) != EOF) ;
378  
-    }
379  
-    if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
380  
-        while ((c = get_byte(s)) != 0 && c != EOF) ;
381  
-    }
382  
-    if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
383  
-        while ((c = get_byte(s)) != 0 && c != EOF) ;
384  
-    }
385  
-    if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
386  
-        for (len = 0; len < 2; len++) (void)get_byte(s);
387  
-    }
388  
-    s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
389  
-}
390  
-
391  
- /* ===========================================================================
392  
- * Cleanup then free the given gz_stream. Return a zlib error code.
393  
-   Try freeing in the reverse order of allocations.
394  
- */
395  
-local int destroy (s)
396  
-    gz_stream *s;
397  
-{
398  
-    int err = Z_OK;
399  
-
400  
-    if (!s) return Z_STREAM_ERROR;
401  
-
402  
-    TRYFREE(s->msg);
403  
-
404  
-    if (s->stream.state != NULL) {
405  
-        if (s->mode == 'w') {
406  
-#ifdef NO_GZCOMPRESS
407  
-            err = Z_STREAM_ERROR;
408  
-#else
409  
-            err = deflateEnd(&(s->stream));
410  
-#endif
411  
-        } else if (s->mode == 'r') {
412  
-            err = inflateEnd(&(s->stream));
413  
-        }
414  
-    }
415  
-    if (s->file != NULL && fclose(s->file)) {
416  
-#ifdef ESPIPE
417  
-        if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
418  
-#endif
419  
-            err = Z_ERRNO;
420  
-    }
421  
-    if (s->z_err < 0) err = s->z_err;
422  
-
423  
-    TRYFREE(s->inbuf);
424  
-    TRYFREE(s->outbuf);
425  
-    TRYFREE(s->path);
426  
-    TRYFREE(s);
427  
-    return err;
428  
-}
429  
-
430  
-/* ===========================================================================
431  
-     Reads the given number of uncompressed bytes from the compressed file.
432  
-   gzread returns the number of bytes actually read (0 for end of file).
433  
-*/
434  
-int ZEXPORT gzread (file, buf, len)
435  
-    gzFile file;
436  
-    voidp buf;
437  
-    unsigned len;
438  
-{
439  
-    gz_stream *s = (gz_stream*)file;
440  
-    Bytef *start = (Bytef*)buf; /* starting point for crc computation */
441  
-    Byte  *next_out; /* == stream.next_out but not forced far (for MSDOS) */
442  
-
443  
-    if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
444  
-
445  
-    if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
446  
-    if (s->z_err == Z_STREAM_END) return 0;  /* EOF */
447  
-
448  
-    next_out = (Byte*)buf;
449  
-    s->stream.next_out = (Bytef*)buf;
450  
-    s->stream.avail_out = len;
451  
-
452  
-    if (s->stream.avail_out && s->back != EOF) {
453  
-        *next_out++ = s->back;
454  
-        s->stream.next_out++;
455  
-        s->stream.avail_out--;
456  
-        s->back = EOF;
457  
-        s->out++;
458  
-        start++;
459  
-        if (s->last) {
460  
-            s->z_err = Z_STREAM_END;
461  
-            return 1;
462  
-        }
463  
-    }
464  
-
465  
-    while (s->stream.avail_out != 0) {
466  
-
467  
-        if (s->transparent) {
468  
-            /* Copy first the lookahead bytes: */
469  
-            uInt n = s->stream.avail_in;
470  
-            if (n > s->stream.avail_out) n = s->stream.avail_out;
471  
-            if (n > 0) {
472  
-                zmemcpy(s->stream.next_out, s->stream.next_in, n);
473  
-                next_out += n;
474  
-                s->stream.next_out = next_out;
475  
-                s->stream.next_in   += n;
476  
-                s->stream.avail_out -= n;
477  
-                s->stream.avail_in  -= n;