11/* 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
33 * For conditions of distribution and use, see copyright notice in zlib.h
44 */
55
5252#include "deflate.h"
5353
5454const 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 " ;
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
@@ -493,7 +493,7 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
493493 * symbols from which it is being constructed.
494494 */
495495
496- s -> pending_buf = (uchf * ) ZALLOC (strm , s -> lit_bufsize , 4 );
496+ s -> pending_buf = (uchf * ) ZALLOC (strm , s -> lit_bufsize , LIT_BUFS );
497497 s -> pending_buf_size = (ulg )s -> lit_bufsize * 4 ;
498498
499499 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,
503503 deflateEnd (strm );
504504 return Z_MEM_ERROR ;
505505 }
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
506511 s -> sym_buf = s -> pending_buf + s -> lit_bufsize ;
507512 s -> sym_end = (s -> lit_bufsize - 1 ) * 3 ;
513+ #endif
508514 /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
509515 * on 16 bit machines and because stored blocks are restricted to
510516 * 64K-1 bytes.
@@ -720,9 +726,15 @@ int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) {
720726
721727 if (deflateStateCheck (strm )) return Z_STREAM_ERROR ;
722728 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
723734 if (bits < 0 || bits > 16 ||
724735 s -> sym_buf < s -> pending_out + ((Buf_size + 7 ) >> 3 ))
725736 return Z_BUF_ERROR ;
737+ #endif
726738 do {
727739 put = Buf_size - s -> bi_valid ;
728740 if (put > bits )
@@ -1294,7 +1306,7 @@ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
12941306 ds -> window = (Bytef * ) ZALLOC (dest , ds -> w_size , 2 * sizeof (Byte ));
12951307 ds -> prev = (Posf * ) ZALLOC (dest , ds -> w_size , sizeof (Pos ));
12961308 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 );
12981310
12991311 if (ds -> window == Z_NULL || ds -> prev == Z_NULL || ds -> head == Z_NULL ||
13001312 ds -> pending_buf == Z_NULL ) {
@@ -1305,10 +1317,15 @@ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
13051317 zmemcpy (ds -> window , ss -> window , ds -> w_size * 2 * sizeof (Byte ));
13061318 zmemcpy ((voidpf )ds -> prev , (voidpf )ss -> prev , ds -> w_size * sizeof (Pos ));
13071319 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 );
13091321
13101322 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
13111327 ds -> sym_buf = ds -> pending_buf + ds -> lit_bufsize ;
1328+ #endif
13121329
13131330 ds -> l_desc .dyn_tree = ds -> dyn_ltree ;
13141331 ds -> d_desc .dyn_tree = ds -> dyn_dtree ;
@@ -1539,13 +1556,21 @@ local uInt longest_match(deflate_state *s, IPos cur_match) {
15391556 */
15401557local void check_match (deflate_state * s , IPos start , IPos match , int length ) {
15411558 /* 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 );
15461571 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 );
15491574 z_error ("invalid match" );
15501575 }
15511576 if (z_verbose > 1 ) {
0 commit comments