11/* deflate.c -- compress data using the deflation algorithm
2- * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
2+ * Copyright (C) 1995-2022 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.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler " ;
55+ " deflate 1.2.12 Copyright 1995-2022 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
@@ -190,8 +190,11 @@ local const config configuration_table[10] = {
190190 * prev[] will be initialized on the fly.
191191 */
192192#define CLEAR_HASH (s ) \
193- s->head[s->hash_size-1] = NIL; \
194- zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
193+ do { \
194+ s->head[s->hash_size-1] = NIL; \
195+ zmemzero((Bytef *)s->head, \
196+ (unsigned)(s->hash_size-1)*sizeof(*s->head)); \
197+ } while (0)
195198
196199/* ===========================================================================
197200 * Slide the hash table when sliding the window down (could be avoided with 32
@@ -252,11 +255,6 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
252255 int wrap = 1 ;
253256 static const char my_version [] = ZLIB_VERSION ;
254257
255- ushf * overlay ;
256- /* We overlay pending_buf and d_buf+l_buf. This works since the average
257- * output size for (length,distance) codes is <= 24 bits.
258- */
259-
260258 if (version == Z_NULL || version [0 ] != my_version [0 ] ||
261259 stream_size != sizeof (z_stream )) {
262260 return Z_VERSION_ERROR ;
@@ -326,9 +324,47 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
326324
327325 s -> lit_bufsize = 1 << (memLevel + 6 ); /* 16K elements by default */
328326
329- overlay = (ushf * ) ZALLOC (strm , s -> lit_bufsize , sizeof (ush )+ 2 );
330- s -> pending_buf = (uchf * ) overlay ;
331- s -> pending_buf_size = (ulg )s -> lit_bufsize * (sizeof (ush )+ 2L );
327+ /* We overlay pending_buf and sym_buf. This works since the average size
328+ * for length/distance pairs over any compressed block is assured to be 31
329+ * bits or less.
330+ *
331+ * Analysis: The longest fixed codes are a length code of 8 bits plus 5
332+ * extra bits, for lengths 131 to 257. The longest fixed distance codes are
333+ * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
334+ * possible fixed-codes length/distance pair is then 31 bits total.
335+ *
336+ * sym_buf starts one-fourth of the way into pending_buf. So there are
337+ * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
338+ * in sym_buf is three bytes -- two for the distance and one for the
339+ * literal/length. As each symbol is consumed, the pointer to the next
340+ * sym_buf value to read moves forward three bytes. From that symbol, up to
341+ * 31 bits are written to pending_buf. The closest the written pending_buf
342+ * bits gets to the next sym_buf symbol to read is just before the last
343+ * code is written. At that time, 31*(n-2) bits have been written, just
344+ * after 24*(n-2) bits have been consumed from sym_buf. sym_buf starts at
345+ * 8*n bits into pending_buf. (Note that the symbol buffer fills when n-1
346+ * symbols are written.) The closest the writing gets to what is unread is
347+ * then n+14 bits. Here n is lit_bufsize, which is 16384 by default, and
348+ * can range from 128 to 32768.
349+ *
350+ * Therefore, at a minimum, there are 142 bits of space between what is
351+ * written and what is read in the overlain buffers, so the symbols cannot
352+ * be overwritten by the compressed data. That space is actually 139 bits,
353+ * due to the three-bit fixed-code block header.
354+ *
355+ * That covers the case where either Z_FIXED is specified, forcing fixed
356+ * codes, or when the use of fixed codes is chosen, because that choice
357+ * results in a smaller compressed block than dynamic codes. That latter
358+ * condition then assures that the above analysis also covers all dynamic
359+ * blocks. A dynamic-code block will only be chosen to be emitted if it has
360+ * fewer bits than a fixed-code block would for the same set of symbols.
361+ * Therefore its average symbol length is assured to be less than 31. So
362+ * the compressed data for a dynamic block also cannot overwrite the
363+ * symbols from which it is being constructed.
364+ */
365+
366+ s -> pending_buf = (uchf * ) ZALLOC (strm , s -> lit_bufsize , 4 );
367+ s -> pending_buf_size = (ulg )s -> lit_bufsize * 4 ;
332368
333369 if (s -> window == Z_NULL || s -> prev == Z_NULL || s -> head == Z_NULL ||
334370 s -> pending_buf == Z_NULL ) {
@@ -337,8 +373,12 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
337373 deflateEnd (strm );
338374 return Z_MEM_ERROR ;
339375 }
340- s -> d_buf = overlay + s -> lit_bufsize /sizeof (ush );
341- s -> l_buf = s -> pending_buf + (1 + sizeof (ush ))* s -> lit_bufsize ;
376+ s -> sym_buf = s -> pending_buf + s -> lit_bufsize ;
377+ s -> sym_end = (s -> lit_bufsize - 1 ) * 3 ;
378+ /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
379+ * on 16 bit machines and because stored blocks are restricted to
380+ * 64K-1 bytes.
381+ */
342382
343383 s -> level = level ;
344384 s -> strategy = strategy ;
@@ -488,13 +528,13 @@ int ZEXPORT deflateResetKeep (strm)
488528#ifdef GZIP
489529 s -> wrap == 2 ? GZIP_STATE :
490530#endif
491- s -> wrap ? INIT_STATE : BUSY_STATE ;
531+ INIT_STATE ;
492532 strm -> adler =
493533#ifdef GZIP
494534 s -> wrap == 2 ? crc32 (0L , Z_NULL , 0 ) :
495535#endif
496536 adler32 (0L , Z_NULL , 0 );
497- s -> last_flush = Z_NO_FLUSH ;
537+ s -> last_flush = -2 ;
498538
499539 _tr_init (s );
500540
@@ -549,7 +589,8 @@ int ZEXPORT deflatePrime (strm, bits, value)
549589
550590 if (deflateStateCheck (strm )) return Z_STREAM_ERROR ;
551591 s = strm -> state ;
552- if ((Bytef * )(s -> d_buf ) < s -> pending_out + ((Buf_size + 7 ) >> 3 ))
592+ if (bits < 0 || bits > 16 ||
593+ s -> sym_buf < s -> pending_out + ((Buf_size + 7 ) >> 3 ))
553594 return Z_BUF_ERROR ;
554595 do {
555596 put = Buf_size - s -> bi_valid ;
@@ -587,12 +628,12 @@ int ZEXPORT deflateParams(strm, level, strategy)
587628 func = configuration_table [s -> level ].func ;
588629
589630 if ((strategy != s -> strategy || func != configuration_table [level ].func ) &&
590- s -> high_water ) {
631+ s -> last_flush != -2 ) {
591632 /* Flush the last buffer: */
592633 int err = deflate (strm , Z_BLOCK );
593634 if (err == Z_STREAM_ERROR )
594635 return err ;
595- if (strm -> avail_out == 0 )
636+ if (strm -> avail_in || ( s -> strstart - s -> block_start ) + s -> lookahead )
596637 return Z_BUF_ERROR ;
597638 }
598639 if (s -> level != level ) {
@@ -811,6 +852,8 @@ int ZEXPORT deflate (strm, flush)
811852 }
812853
813854 /* Write the header */
855+ if (s -> status == INIT_STATE && s -> wrap == 0 )
856+ s -> status = BUSY_STATE ;
814857 if (s -> status == INIT_STATE ) {
815858 /* zlib header */
816859 uInt header = (Z_DEFLATED + ((s -> w_bits - 8 )<<4 )) << 8 ;
@@ -1108,7 +1151,6 @@ int ZEXPORT deflateCopy (dest, source)
11081151#else
11091152 deflate_state * ds ;
11101153 deflate_state * ss ;
1111- ushf * overlay ;
11121154
11131155
11141156 if (deflateStateCheck (source ) || dest == Z_NULL ) {
@@ -1128,8 +1170,7 @@ int ZEXPORT deflateCopy (dest, source)
11281170 ds -> window = (Bytef * ) ZALLOC (dest , ds -> w_size , 2 * sizeof (Byte ));
11291171 ds -> prev = (Posf * ) ZALLOC (dest , ds -> w_size , sizeof (Pos ));
11301172 ds -> head = (Posf * ) ZALLOC (dest , ds -> hash_size , sizeof (Pos ));
1131- overlay = (ushf * ) ZALLOC (dest , ds -> lit_bufsize , sizeof (ush )+ 2 );
1132- ds -> pending_buf = (uchf * ) overlay ;
1173+ ds -> pending_buf = (uchf * ) ZALLOC (dest , ds -> lit_bufsize , 4 );
11331174
11341175 if (ds -> window == Z_NULL || ds -> prev == Z_NULL || ds -> head == Z_NULL ||
11351176 ds -> pending_buf == Z_NULL ) {
@@ -1143,8 +1184,7 @@ int ZEXPORT deflateCopy (dest, source)
11431184 zmemcpy (ds -> pending_buf , ss -> pending_buf , (uInt )ds -> pending_buf_size );
11441185
11451186 ds -> pending_out = ds -> pending_buf + (ss -> pending_out - ss -> pending_buf );
1146- ds -> d_buf = overlay + ds -> lit_bufsize /sizeof (ush );
1147- ds -> l_buf = ds -> pending_buf + (1 + sizeof (ush ))* ds -> lit_bufsize ;
1187+ ds -> sym_buf = ds -> pending_buf + ds -> lit_bufsize ;
11481188
11491189 ds -> l_desc .dyn_tree = ds -> dyn_ltree ;
11501190 ds -> d_desc .dyn_tree = ds -> dyn_dtree ;
@@ -1513,6 +1553,8 @@ local void fill_window(s)
15131553 s -> match_start -= wsize ;
15141554 s -> strstart -= wsize ; /* we now have strstart >= MAX_DIST */
15151555 s -> block_start -= (long ) wsize ;
1556+ if (s -> insert > s -> strstart )
1557+ s -> insert = s -> strstart ;
15161558 slide_hash (s );
15171559 more += wsize ;
15181560 }
@@ -1742,6 +1784,7 @@ local block_state deflate_stored(s, flush)
17421784 s -> matches = 2 ; /* clear hash */
17431785 zmemcpy (s -> window , s -> strm -> next_in - s -> w_size , s -> w_size );
17441786 s -> strstart = s -> w_size ;
1787+ s -> insert = s -> strstart ;
17451788 }
17461789 else {
17471790 if (s -> window_size - s -> strstart <= used ) {
@@ -1750,12 +1793,14 @@ local block_state deflate_stored(s, flush)
17501793 zmemcpy (s -> window , s -> window + s -> w_size , s -> strstart );
17511794 if (s -> matches < 2 )
17521795 s -> matches ++ ; /* add a pending slide_hash() */
1796+ if (s -> insert > s -> strstart )
1797+ s -> insert = s -> strstart ;
17531798 }
17541799 zmemcpy (s -> window + s -> strstart , s -> strm -> next_in - used , used );
17551800 s -> strstart += used ;
1801+ s -> insert += MIN (used , s -> w_size - s -> insert );
17561802 }
17571803 s -> block_start = s -> strstart ;
1758- s -> insert += MIN (used , s -> w_size - s -> insert );
17591804 }
17601805 if (s -> high_water < s -> strstart )
17611806 s -> high_water = s -> strstart ;
@@ -1770,7 +1815,7 @@ local block_state deflate_stored(s, flush)
17701815 return block_done ;
17711816
17721817 /* Fill the window with any remaining input. */
1773- have = s -> window_size - s -> strstart - 1 ;
1818+ have = s -> window_size - s -> strstart ;
17741819 if (s -> strm -> avail_in > have && s -> block_start >= (long )s -> w_size ) {
17751820 /* Slide the window down. */
17761821 s -> block_start -= s -> w_size ;
@@ -1779,12 +1824,15 @@ local block_state deflate_stored(s, flush)
17791824 if (s -> matches < 2 )
17801825 s -> matches ++ ; /* add a pending slide_hash() */
17811826 have += s -> w_size ; /* more space now */
1827+ if (s -> insert > s -> strstart )
1828+ s -> insert = s -> strstart ;
17821829 }
17831830 if (have > s -> strm -> avail_in )
17841831 have = s -> strm -> avail_in ;
17851832 if (have ) {
17861833 read_buf (s -> strm , s -> window + s -> strstart , have );
17871834 s -> strstart += have ;
1835+ s -> insert += MIN (have , s -> w_size - s -> insert );
17881836 }
17891837 if (s -> high_water < s -> strstart )
17901838 s -> high_water = s -> strstart ;
@@ -1912,7 +1960,7 @@ local block_state deflate_fast(s, flush)
19121960 FLUSH_BLOCK (s , 1 );
19131961 return finish_done ;
19141962 }
1915- if (s -> last_lit )
1963+ if (s -> sym_next )
19161964 FLUSH_BLOCK (s , 0 );
19171965 return block_done ;
19181966}
@@ -2043,7 +2091,7 @@ local block_state deflate_slow(s, flush)
20432091 FLUSH_BLOCK (s , 1 );
20442092 return finish_done ;
20452093 }
2046- if (s -> last_lit )
2094+ if (s -> sym_next )
20472095 FLUSH_BLOCK (s , 0 );
20482096 return block_done ;
20492097}
@@ -2118,7 +2166,7 @@ local block_state deflate_rle(s, flush)
21182166 FLUSH_BLOCK (s , 1 );
21192167 return finish_done ;
21202168 }
2121- if (s -> last_lit )
2169+ if (s -> sym_next )
21222170 FLUSH_BLOCK (s , 0 );
21232171 return block_done ;
21242172}
@@ -2157,7 +2205,7 @@ local block_state deflate_huff(s, flush)
21572205 FLUSH_BLOCK (s , 1 );
21582206 return finish_done ;
21592207 }
2160- if (s -> last_lit )
2208+ if (s -> sym_next )
21612209 FLUSH_BLOCK (s , 0 );
21622210 return block_done ;
21632211}
0 commit comments