52
52
#include "deflate.h"
53
53
54
54
const 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 " ;
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
@@ -132,12 +132,12 @@ typedef struct config_s {
132
132
local const config configuration_table [2 ] = {
133
133
/* good lazy nice chain */
134
134
/* 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 */
136
136
#else
137
137
local const config configuration_table [10 ] = {
138
138
/* good lazy nice chain */
139
139
/* 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 */
141
141
/* 2 */ {4 , 5 , 16 , 8 , deflate_fast },
142
142
/* 3 */ {4 , 6 , 32 , 32 , deflate_fast },
143
143
@@ -146,7 +146,7 @@ local const config configuration_table[10] = {
146
146
/* 6 */ {8 , 16 , 128 , 128 , deflate_slow },
147
147
/* 7 */ {8 , 32 , 128 , 256 , deflate_slow },
148
148
/* 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 */
150
150
#endif
151
151
152
152
/* 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,
225
225
int stream_size ;
226
226
{
227
227
deflate_state * s ;
228
- int noheader = 0 ;
228
+ int wrap = 1 ;
229
229
static const char my_version [] = ZLIB_VERSION ;
230
230
231
231
ushf * overlay ;
@@ -252,10 +252,16 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
252
252
if (level == Z_DEFAULT_COMPRESSION ) level = 6 ;
253
253
#endif
254
254
255
- if (windowBits < 0 ) { /* undocumented feature: suppress zlib header */
256
- noheader = 1 ;
255
+ if (windowBits < 0 ) { /* suppress zlib wrapper */
256
+ wrap = 0 ;
257
257
windowBits = - windowBits ;
258
258
}
259
+ #ifdef GZIP
260
+ else if (windowBits > 15 ) {
261
+ wrap = 2 ; /* write gzip wrapper instead */
262
+ windowBits -= 16 ;
263
+ }
264
+ #endif
259
265
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
260
266
windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
261
267
strategy < 0 || strategy > Z_RLE ) {
@@ -267,7 +273,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
267
273
strm -> state = (struct internal_state FAR * )s ;
268
274
s -> strm = strm ;
269
275
270
- s -> noheader = noheader ;
276
+ s -> wrap = wrap ;
271
277
s -> w_bits = windowBits ;
272
278
s -> w_size = 1 << s -> w_bits ;
273
279
s -> w_mask = s -> w_size - 1 ;
@@ -316,11 +322,12 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
316
322
IPos hash_head = 0 ;
317
323
318
324
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 ))
320
327
return Z_STREAM_ERROR ;
321
328
322
329
s = strm -> state ;
323
- if (! s -> noheader )
330
+ if (s -> wrap )
324
331
strm -> adler = adler32 (strm -> adler , dictionary , dictLength );
325
332
326
333
if (length < MIN_MATCH ) return Z_OK ;
@@ -364,11 +371,15 @@ int ZEXPORT deflateReset (strm)
364
371
s -> pending = 0 ;
365
372
s -> pending_out = s -> pending_buf ;
366
373
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); */
369
376
}
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 );
372
383
s -> last_flush = Z_NO_FLUSH ;
373
384
374
385
_tr_init (s );
@@ -428,7 +439,7 @@ int ZEXPORT deflateParams(strm, level, strategy)
428
439
* can emit on compressed data for some combinations of the parameters.
429
440
*
430
441
* 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 .
432
443
* But even the conservative upper bound of about 14% expansion does not
433
444
* seem onerous for output buffer allocation.
434
445
*/
@@ -519,33 +530,53 @@ int ZEXPORT deflate (strm, flush)
519
530
old_flush = s -> last_flush ;
520
531
s -> last_flush = flush ;
521
532
522
- /* Write the zlib header */
533
+ /* Write the header */
523
534
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
+ }
534
552
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 );
547
579
}
548
- strm -> adler = 1L ;
549
580
}
550
581
551
582
/* Flush as much pending output as possible */
@@ -622,16 +653,31 @@ int ZEXPORT deflate (strm, flush)
622
653
Assert (strm -> avail_out > 0 , "bug2" );
623
654
624
655
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
+ }
630
676
flush_pending (strm );
631
677
/* If avail_out is zero, the application will call deflate again
632
678
* to flush the rest.
633
679
*/
634
- s -> noheader = -1 ; /* write the trailer only once! */
680
+ if ( s -> wrap > 0 ) s -> wrap = - s -> wrap ; /* write the trailer only once! */
635
681
return s -> pending != 0 ? Z_OK : Z_STREAM_END ;
636
682
}
637
683
@@ -740,9 +786,14 @@ local int read_buf(strm, buf, size)
740
786
741
787
strm -> avail_in -= len ;
742
788
743
- if (! strm -> state -> noheader ) {
789
+ if (strm -> state -> wrap == 1 ) {
744
790
strm -> adler = adler32 (strm -> adler , strm -> next_in , len );
745
791
}
792
+ #ifdef GZIP
793
+ else if (strm -> state -> wrap == 2 ) {
794
+ strm -> adler = crc32 (strm -> adler , strm -> next_in , len );
795
+ }
796
+ #endif
746
797
zmemcpy (buf , strm -> next_in , len );
747
798
strm -> next_in += len ;
748
799
strm -> total_in += len ;
@@ -1044,7 +1095,7 @@ local void fill_window(s)
1044
1095
1045
1096
} else if (more == (unsigned )(-1 )) {
1046
1097
/* 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)
1048
1099
*/
1049
1100
more -- ;
1050
1101
}
@@ -1271,7 +1322,7 @@ local block_state deflate_fast(s, flush)
1271
1322
#ifndef FASTEST
1272
1323
if (s -> match_length <= s -> max_insert_length &&
1273
1324
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 */
1275
1326
do {
1276
1327
s -> strstart ++ ;
1277
1328
INSERT_STRING (s , s -> strstart , hash_head );
0 commit comments