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 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 )
@@ -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 ) {
0 commit comments