5252#include "deflate.h"
5353
5454const char deflate_copyright [] =
55- " deflate 1.2.0.3 Copyright 1995-2003 Jean-loup Gailly " ;
55+ " deflate 1.2.0.4 Copyright 1995-2003 Jean-loup Gailly " ;
5656/*
5757 If you use the zlib library in a product, an acknowledgment is welcome
5858 in the documentation of your product. If for some reason you cannot
@@ -132,12 +132,12 @@ typedef struct config_s {
132132local const config configuration_table [2 ] = {
133133/* good lazy nice chain */
134134/* 0 */ {0 , 0 , 0 , 0 , deflate_stored }, /* store only */
135- /* 1 */ {4 , 4 , 8 , 4 , deflate_fast }}; /* maximum speed, no lazy matches */
135+ /* 1 */ {4 , 4 , 8 , 4 , deflate_fast }}; /* max speed, no lazy matches */
136136#else
137137local const config configuration_table [10 ] = {
138138/* good lazy nice chain */
139139/* 0 */ {0 , 0 , 0 , 0 , deflate_stored }, /* store only */
140- /* 1 */ {4 , 4 , 8 , 4 , deflate_fast }, /* maximum speed, no lazy matches */
140+ /* 1 */ {4 , 4 , 8 , 4 , deflate_fast }, /* max speed, no lazy matches */
141141/* 2 */ {4 , 5 , 16 , 8 , deflate_fast },
142142/* 3 */ {4 , 6 , 32 , 32 , deflate_fast },
143143
@@ -146,7 +146,7 @@ local const config configuration_table[10] = {
146146/* 6 */ {8 , 16 , 128 , 128 , deflate_slow },
147147/* 7 */ {8 , 32 , 128 , 256 , deflate_slow },
148148/* 8 */ {32 , 128 , 258 , 1024 , deflate_slow },
149- /* 9 */ {32 , 258 , 258 , 4096 , deflate_slow }}; /* maximum compression */
149+ /* 9 */ {32 , 258 , 258 , 4096 , deflate_slow }}; /* max compression */
150150#endif
151151
152152/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
@@ -225,7 +225,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
225225 int stream_size ;
226226{
227227 deflate_state * s ;
228- int noheader = 0 ;
228+ int wrap = 1 ;
229229 static const char my_version [] = ZLIB_VERSION ;
230230
231231 ushf * overlay ;
@@ -252,10 +252,16 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
252252 if (level == Z_DEFAULT_COMPRESSION ) level = 6 ;
253253#endif
254254
255- if (windowBits < 0 ) { /* undocumented feature: suppress zlib header */
256- noheader = 1 ;
255+ if (windowBits < 0 ) { /* suppress zlib wrapper */
256+ wrap = 0 ;
257257 windowBits = - windowBits ;
258258 }
259+ #ifdef GZIP
260+ else if (windowBits > 15 ) {
261+ wrap = 2 ; /* write gzip wrapper instead */
262+ windowBits -= 16 ;
263+ }
264+ #endif
259265 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
260266 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
261267 strategy < 0 || strategy > Z_RLE ) {
@@ -267,7 +273,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
267273 strm -> state = (struct internal_state FAR * )s ;
268274 s -> strm = strm ;
269275
270- s -> noheader = noheader ;
276+ s -> wrap = wrap ;
271277 s -> w_bits = windowBits ;
272278 s -> w_size = 1 << s -> w_bits ;
273279 s -> w_mask = s -> w_size - 1 ;
@@ -316,11 +322,12 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
316322 IPos hash_head = 0 ;
317323
318324 if (strm == Z_NULL || strm -> state == Z_NULL || dictionary == Z_NULL ||
319- (!strm -> state -> noheader && strm -> state -> status != INIT_STATE ))
325+ strm -> state -> wrap == 2 ||
326+ (strm -> state -> wrap == 1 && strm -> state -> status != INIT_STATE ))
320327 return Z_STREAM_ERROR ;
321328
322329 s = strm -> state ;
323- if (! s -> noheader )
330+ if (s -> wrap )
324331 strm -> adler = adler32 (strm -> adler , dictionary , dictLength );
325332
326333 if (length < MIN_MATCH ) return Z_OK ;
@@ -364,11 +371,15 @@ int ZEXPORT deflateReset (strm)
364371 s -> pending = 0 ;
365372 s -> pending_out = s -> pending_buf ;
366373
367- if (s -> noheader < 0 ) {
368- s -> noheader = 0 ; /* was set to -1 by deflate(..., Z_FINISH); */
374+ if (s -> wrap < 0 ) {
375+ s -> wrap = - s -> wrap ; /* was made negative by deflate(..., Z_FINISH); */
369376 }
370- s -> status = s -> noheader ? BUSY_STATE : INIT_STATE ;
371- strm -> adler = 1 ;
377+ s -> status = s -> wrap ? INIT_STATE : BUSY_STATE ;
378+ strm -> adler =
379+ #ifdef GZIP
380+ s -> wrap == 2 ? crc32 (0L , Z_NULL , 0 ) :
381+ #endif
382+ adler32 (0L , Z_NULL , 0 );
372383 s -> last_flush = Z_NO_FLUSH ;
373384
374385 _tr_init (s );
@@ -428,7 +439,7 @@ int ZEXPORT deflateParams(strm, level, strategy)
428439 * can emit on compressed data for some combinations of the parameters.
429440 *
430441 * This function could be more sophisticated to provide closer upper bounds
431- * for every combination of windowBits and memLevel, as well as noheader .
442+ * for every combination of windowBits and memLevel, as well as wrap .
432443 * But even the conservative upper bound of about 14% expansion does not
433444 * seem onerous for output buffer allocation.
434445 */
@@ -519,33 +530,53 @@ int ZEXPORT deflate (strm, flush)
519530 old_flush = s -> last_flush ;
520531 s -> last_flush = flush ;
521532
522- /* Write the zlib header */
533+ /* Write the header */
523534 if (s -> status == INIT_STATE ) {
524-
525- uInt header = (Z_DEFLATED + ((s -> w_bits - 8 )<<4 )) << 8 ;
526- uInt level_flags ;
527-
528- if (s -> strategy >= Z_HUFFMAN_ONLY || s -> level < 2 )
529- level_flags = 0 ;
530- else if (s -> level < 6 )
531- level_flags = 1 ;
532- else if (s -> level == 6 )
533- level_flags = 2 ;
535+ #ifdef GZIP
536+ if (s -> wrap == 2 ) {
537+ put_byte (s , 31 );
538+ put_byte (s , 139 );
539+ put_byte (s , 8 );
540+ put_byte (s , 0 );
541+ put_byte (s , 0 );
542+ put_byte (s , 0 );
543+ put_byte (s , 0 );
544+ put_byte (s , 0 );
545+ put_byte (s , s -> level == 9 ? 2 :
546+ (s -> strategy >= Z_HUFFMAN_ONLY || s -> level < 2 ?
547+ 4 : 0 ));
548+ put_byte (s , 255 );
549+ s -> status = BUSY_STATE ;
550+ strm -> adler = crc32 (0L , Z_NULL , 0 );
551+ }
534552 else
535- level_flags = 3 ;
536- header |= (level_flags << 6 );
537- if (s -> strstart != 0 ) header |= PRESET_DICT ;
538- header += 31 - (header % 31 );
539-
540- s -> status = BUSY_STATE ;
541- putShortMSB (s , header );
542-
543- /* Save the adler32 of the preset dictionary: */
544- if (s -> strstart != 0 ) {
545- putShortMSB (s , (uInt )(strm -> adler >> 16 ));
546- putShortMSB (s , (uInt )(strm -> adler & 0xffff ));
553+ #endif
554+ {
555+ uInt header = (Z_DEFLATED + ((s -> w_bits - 8 )<<4 )) << 8 ;
556+ uInt level_flags ;
557+
558+ if (s -> strategy >= Z_HUFFMAN_ONLY || s -> level < 2 )
559+ level_flags = 0 ;
560+ else if (s -> level < 6 )
561+ level_flags = 1 ;
562+ else if (s -> level == 6 )
563+ level_flags = 2 ;
564+ else
565+ level_flags = 3 ;
566+ header |= (level_flags << 6 );
567+ if (s -> strstart != 0 ) header |= PRESET_DICT ;
568+ header += 31 - (header % 31 );
569+
570+ s -> status = BUSY_STATE ;
571+ putShortMSB (s , header );
572+
573+ /* Save the adler32 of the preset dictionary: */
574+ if (s -> strstart != 0 ) {
575+ putShortMSB (s , (uInt )(strm -> adler >> 16 ));
576+ putShortMSB (s , (uInt )(strm -> adler & 0xffff ));
577+ }
578+ strm -> adler = adler32 (0L , Z_NULL , 0 );
547579 }
548- strm -> adler = 1L ;
549580 }
550581
551582 /* Flush as much pending output as possible */
@@ -622,16 +653,31 @@ int ZEXPORT deflate (strm, flush)
622653 Assert (strm -> avail_out > 0 , "bug2" );
623654
624655 if (flush != Z_FINISH ) return Z_OK ;
625- if (s -> noheader ) return Z_STREAM_END ;
626-
627- /* Write the zlib trailer (adler32) */
628- putShortMSB (s , (uInt )(strm -> adler >> 16 ));
629- putShortMSB (s , (uInt )(strm -> adler & 0xffff ));
656+ if (s -> wrap <= 0 ) return Z_STREAM_END ;
657+
658+ /* Write the trailer */
659+ #ifdef GZIP
660+ if (s -> wrap == 2 ) {
661+ put_byte (s , (Byte )(strm -> adler & 0xff ));
662+ put_byte (s , (Byte )((strm -> adler >> 8 ) & 0xff ));
663+ put_byte (s , (Byte )((strm -> adler >> 16 ) & 0xff ));
664+ put_byte (s , (Byte )((strm -> adler >> 24 ) & 0xff ));
665+ put_byte (s , (Byte )(strm -> total_in & 0xff ));
666+ put_byte (s , (Byte )((strm -> total_in >> 8 ) & 0xff ));
667+ put_byte (s , (Byte )((strm -> total_in >> 16 ) & 0xff ));
668+ put_byte (s , (Byte )((strm -> total_in >> 24 ) & 0xff ));
669+ }
670+ else
671+ #endif
672+ {
673+ putShortMSB (s , (uInt )(strm -> adler >> 16 ));
674+ putShortMSB (s , (uInt )(strm -> adler & 0xffff ));
675+ }
630676 flush_pending (strm );
631677 /* If avail_out is zero, the application will call deflate again
632678 * to flush the rest.
633679 */
634- s -> noheader = -1 ; /* write the trailer only once! */
680+ if ( s -> wrap > 0 ) s -> wrap = - s -> wrap ; /* write the trailer only once! */
635681 return s -> pending != 0 ? Z_OK : Z_STREAM_END ;
636682}
637683
@@ -740,9 +786,14 @@ local int read_buf(strm, buf, size)
740786
741787 strm -> avail_in -= len ;
742788
743- if (! strm -> state -> noheader ) {
789+ if (strm -> state -> wrap == 1 ) {
744790 strm -> adler = adler32 (strm -> adler , strm -> next_in , len );
745791 }
792+ #ifdef GZIP
793+ else if (strm -> state -> wrap == 2 ) {
794+ strm -> adler = crc32 (strm -> adler , strm -> next_in , len );
795+ }
796+ #endif
746797 zmemcpy (buf , strm -> next_in , len );
747798 strm -> next_in += len ;
748799 strm -> total_in += len ;
@@ -1044,7 +1095,7 @@ local void fill_window(s)
10441095
10451096 } else if (more == (unsigned )(-1 )) {
10461097 /* Very unlikely, but possible on 16 bit machine if
1047- * strstart == 0 && lookahead == 1 (input done one byte at time)
1098+ * strstart == 0 && lookahead == 1 (input done a byte at time)
10481099 */
10491100 more -- ;
10501101 }
@@ -1271,7 +1322,7 @@ local block_state deflate_fast(s, flush)
12711322#ifndef FASTEST
12721323 if (s -> match_length <= s -> max_insert_length &&
12731324 s -> lookahead >= MIN_MATCH ) {
1274- s -> match_length -- ; /* string at strstart already in hash table */
1325+ s -> match_length -- ; /* string at strstart already in table */
12751326 do {
12761327 s -> strstart ++ ;
12771328 INSERT_STRING (s , s -> strstart , hash_head );
0 commit comments