1
1
/* deflate.c -- compress data using the deflation algorithm
2
- * Copyright (C) 1995-2023 Jean-loup Gailly and Mark Adler
2
+ * Copyright (C) 1995-2024 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
52
#include "deflate.h"
53
53
54
54
const char deflate_copyright [] =
55
- " deflate 1.3 Copyright 1995-2023 Jean-loup Gailly and Mark Adler " ;
55
+ " deflate 1.3.1.1 Copyright 1995-2024 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
@@ -493,7 +493,7 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
493
493
* symbols from which it is being constructed.
494
494
*/
495
495
496
- s -> pending_buf = (uchf * ) ZALLOC (strm , s -> lit_bufsize , 4 );
496
+ s -> pending_buf = (uchf * ) ZALLOC (strm , s -> lit_bufsize , LIT_BUFS );
497
497
s -> pending_buf_size = (ulg )s -> lit_bufsize * 4 ;
498
498
499
499
if (s -> window == Z_NULL || s -> prev == Z_NULL || s -> head == Z_NULL ||
@@ -503,8 +503,14 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
503
503
deflateEnd (strm );
504
504
return Z_MEM_ERROR ;
505
505
}
506
+ #ifdef LIT_MEM
507
+ s -> d_buf = (ushf * )(s -> pending_buf + (s -> lit_bufsize << 1 ));
508
+ s -> l_buf = s -> pending_buf + (s -> lit_bufsize << 2 );
509
+ s -> sym_end = s -> lit_bufsize - 1 ;
510
+ #else
506
511
s -> sym_buf = s -> pending_buf + s -> lit_bufsize ;
507
512
s -> sym_end = (s -> lit_bufsize - 1 ) * 3 ;
513
+ #endif
508
514
/* We avoid equality with lit_bufsize*3 because of wraparound at 64K
509
515
* on 16 bit machines and because stored blocks are restricted to
510
516
* 64K-1 bytes.
@@ -720,9 +726,15 @@ int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) {
720
726
721
727
if (deflateStateCheck (strm )) return Z_STREAM_ERROR ;
722
728
s = strm -> state ;
729
+ #ifdef LIT_MEM
730
+ if (bits < 0 || bits > 16 ||
731
+ (uchf * )s -> d_buf < s -> pending_out + ((Buf_size + 7 ) >> 3 ))
732
+ return Z_BUF_ERROR ;
733
+ #else
723
734
if (bits < 0 || bits > 16 ||
724
735
s -> sym_buf < s -> pending_out + ((Buf_size + 7 ) >> 3 ))
725
736
return Z_BUF_ERROR ;
737
+ #endif
726
738
do {
727
739
put = Buf_size - s -> bi_valid ;
728
740
if (put > bits )
@@ -834,13 +846,13 @@ uLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen) {
834
846
storelen = sourceLen + (sourceLen >> 5 ) + (sourceLen >> 7 ) +
835
847
(sourceLen >> 11 ) + 7 ;
836
848
837
- /* if can't get parameters, return larger bound plus a zlib wrapper */
849
+ /* if can't get parameters, return larger bound plus a wrapper */
838
850
if (deflateStateCheck (strm ))
839
- return (fixedlen > storelen ? fixedlen : storelen ) + 6 ;
851
+ return (fixedlen > storelen ? fixedlen : storelen ) + 18 ;
840
852
841
853
/* compute wrapper length */
842
854
s = strm -> state ;
843
- switch (s -> wrap ) {
855
+ switch (s -> wrap < 0 ? - s -> wrap : s -> wrap ) {
844
856
case 0 : /* raw deflate */
845
857
wraplen = 0 ;
846
858
break ;
@@ -870,7 +882,7 @@ uLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen) {
870
882
break ;
871
883
#endif
872
884
default : /* for compiler happiness */
873
- wraplen = 6 ;
885
+ wraplen = 18 ;
874
886
}
875
887
876
888
/* if not default parameters, return one of the conservative bounds */
@@ -1294,7 +1306,7 @@ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
1294
1306
ds -> window = (Bytef * ) ZALLOC (dest , ds -> w_size , 2 * sizeof (Byte ));
1295
1307
ds -> prev = (Posf * ) ZALLOC (dest , ds -> w_size , sizeof (Pos ));
1296
1308
ds -> head = (Posf * ) ZALLOC (dest , ds -> hash_size , sizeof (Pos ));
1297
- ds -> pending_buf = (uchf * ) ZALLOC (dest , ds -> lit_bufsize , 4 );
1309
+ ds -> pending_buf = (uchf * ) ZALLOC (dest , ds -> lit_bufsize , LIT_BUFS );
1298
1310
1299
1311
if (ds -> window == Z_NULL || ds -> prev == Z_NULL || ds -> head == Z_NULL ||
1300
1312
ds -> pending_buf == Z_NULL ) {
@@ -1305,10 +1317,15 @@ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
1305
1317
zmemcpy (ds -> window , ss -> window , ds -> w_size * 2 * sizeof (Byte ));
1306
1318
zmemcpy ((voidpf )ds -> prev , (voidpf )ss -> prev , ds -> w_size * sizeof (Pos ));
1307
1319
zmemcpy ((voidpf )ds -> head , (voidpf )ss -> head , ds -> hash_size * sizeof (Pos ));
1308
- zmemcpy (ds -> pending_buf , ss -> pending_buf , ( uInt ) ds -> pending_buf_size );
1320
+ zmemcpy (ds -> pending_buf , ss -> pending_buf , ds -> lit_bufsize * LIT_BUFS );
1309
1321
1310
1322
ds -> pending_out = ds -> pending_buf + (ss -> pending_out - ss -> pending_buf );
1323
+ #ifdef LIT_MEM
1324
+ ds -> d_buf = (ushf * )(ds -> pending_buf + (ds -> lit_bufsize << 1 ));
1325
+ ds -> l_buf = ds -> pending_buf + (ds -> lit_bufsize << 2 );
1326
+ #else
1311
1327
ds -> sym_buf = ds -> pending_buf + ds -> lit_bufsize ;
1328
+ #endif
1312
1329
1313
1330
ds -> l_desc .dyn_tree = ds -> dyn_ltree ;
1314
1331
ds -> d_desc .dyn_tree = ds -> dyn_dtree ;
@@ -1539,13 +1556,21 @@ local uInt longest_match(deflate_state *s, IPos cur_match) {
1539
1556
*/
1540
1557
local void check_match (deflate_state * s , IPos start , IPos match , int length ) {
1541
1558
/* check that the match is indeed a match */
1542
- if (zmemcmp (s -> window + match ,
1543
- s -> window + start , length ) != EQUAL ) {
1544
- fprintf (stderr , " start %u, match %u, length %d\n" ,
1545
- start , match , length );
1559
+ Bytef * back = s -> window + (int )match , * here = s -> window + start ;
1560
+ IPos len = length ;
1561
+ if (match == (IPos )- 1 ) {
1562
+ /* match starts one byte before the current window -- just compare the
1563
+ subsequent length-1 bytes */
1564
+ back ++ ;
1565
+ here ++ ;
1566
+ len -- ;
1567
+ }
1568
+ if (zmemcmp (back , here , len ) != EQUAL ) {
1569
+ fprintf (stderr , " start %u, match %d, length %d\n" ,
1570
+ start , (int )match , length );
1546
1571
do {
1547
- fprintf (stderr , "%c%c " , s -> window [ match ++ ], s -> window [ start ++ ] );
1548
- } while (-- length != 0 );
1572
+ fprintf (stderr , "(%02x %02x) " , * back ++ , * here ++ );
1573
+ } while (-- len != 0 );
1549
1574
z_error ("invalid match" );
1550
1575
}
1551
1576
if (z_verbose > 1 ) {
@@ -1610,7 +1635,8 @@ local block_state deflate_stored(deflate_state *s, int flush) {
1610
1635
* possible. If flushing, copy the remaining available input to next_out as
1611
1636
* stored blocks, if there is enough space.
1612
1637
*/
1613
- unsigned len , left , have , last = 0 ;
1638
+ int last = 0 ;
1639
+ unsigned len , left , have ;
1614
1640
unsigned used = s -> strm -> avail_in ;
1615
1641
do {
1616
1642
/* Set len to the maximum size block that we can copy directly with the
@@ -1646,10 +1672,10 @@ local block_state deflate_stored(deflate_state *s, int flush) {
1646
1672
_tr_stored_block (s , (char * )0 , 0L , last );
1647
1673
1648
1674
/* Replace the lengths in the dummy stored block with len. */
1649
- s -> pending_buf [s -> pending - 4 ] = len ;
1650
- s -> pending_buf [s -> pending - 3 ] = len >> 8 ;
1651
- s -> pending_buf [s -> pending - 2 ] = ~len ;
1652
- s -> pending_buf [s -> pending - 1 ] = ~len >> 8 ;
1675
+ s -> pending_buf [s -> pending - 4 ] = ( Bytef ) len ;
1676
+ s -> pending_buf [s -> pending - 3 ] = ( Bytef )( len >> 8 ) ;
1677
+ s -> pending_buf [s -> pending - 2 ] = ( Bytef ) ~len ;
1678
+ s -> pending_buf [s -> pending - 1 ] = ( Bytef )( ~len >> 8 ) ;
1653
1679
1654
1680
/* Write the stored block header bytes. */
1655
1681
flush_pending (s -> strm );
0 commit comments