@@ -228,11 +228,6 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
228228 int wrap = 1 ;
229229 static const char my_version [] = ZLIB_VERSION ;
230230
231- ushf * overlay ;
232- /* We overlay pending_buf and d_buf+l_buf. This works since the average
233- * output size for (length,distance) codes is <= 24 bits.
234- */
235-
236231 if (version == Z_NULL || version [0 ] != my_version [0 ] ||
237232 stream_size != sizeof (z_stream )) {
238233 return Z_VERSION_ERROR ;
@@ -290,9 +285,47 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
290285
291286 s -> lit_bufsize = 1 << (memLevel + 6 ); /* 16K elements by default */
292287
293- overlay = (ushf * ) ZALLOC (strm , s -> lit_bufsize , sizeof (ush )+ 2 );
294- s -> pending_buf = (uchf * ) overlay ;
295- s -> pending_buf_size = (ulg )s -> lit_bufsize * (sizeof (ush )+ 2L );
288+ /* We overlay pending_buf and sym_buf. This works since the average size
289+ * for length/distance pairs over any compressed block is assured to be 31
290+ * bits or less.
291+ *
292+ * Analysis: The longest fixed codes are a length code of 8 bits plus 5
293+ * extra bits, for lengths 131 to 257. The longest fixed distance codes are
294+ * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
295+ * possible fixed-codes length/distance pair is then 31 bits total.
296+ *
297+ * sym_buf starts one-fourth of the way into pending_buf. So there are
298+ * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
299+ * in sym_buf is three bytes -- two for the distance and one for the
300+ * literal/length. As each symbol is consumed, the pointer to the next
301+ * sym_buf value to read moves forward three bytes. From that symbol, up to
302+ * 31 bits are written to pending_buf. The closest the written pending_buf
303+ * bits gets to the next sym_buf symbol to read is just before the last
304+ * code is written. At that time, 31*(n-2) bits have been written, just
305+ * after 24*(n-2) bits have been consumed from sym_buf. sym_buf starts at
306+ * 8*n bits into pending_buf. (Note that the symbol buffer fills when n-1
307+ * symbols are written.) The closest the writing gets to what is unread is
308+ * then n+14 bits. Here n is lit_bufsize, which is 16384 by default, and
309+ * can range from 128 to 32768.
310+ *
311+ * Therefore, at a minimum, there are 142 bits of space between what is
312+ * written and what is read in the overlain buffers, so the symbols cannot
313+ * be overwritten by the compressed data. That space is actually 139 bits,
314+ * due to the three-bit fixed-code block header.
315+ *
316+ * That covers the case where either Z_FIXED is specified, forcing fixed
317+ * codes, or when the use of fixed codes is chosen, because that choice
318+ * results in a smaller compressed block than dynamic codes. That latter
319+ * condition then assures that the above analysis also covers all dynamic
320+ * blocks. A dynamic-code block will only be chosen to be emitted if it has
321+ * fewer bits than a fixed-code block would for the same set of symbols.
322+ * Therefore its average symbol length is assured to be less than 31. So
323+ * the compressed data for a dynamic block also cannot overwrite the
324+ * symbols from which it is being constructed.
325+ */
326+
327+ s -> pending_buf = (uchf * ) ZALLOC (strm , s -> lit_bufsize , 4 );
328+ s -> pending_buf_size = (ulg )s -> lit_bufsize * 4 ;
296329
297330 if (s -> window == Z_NULL || s -> prev == Z_NULL || s -> head == Z_NULL ||
298331 s -> pending_buf == Z_NULL ) {
@@ -301,8 +334,12 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
301334 deflateEnd (strm );
302335 return Z_MEM_ERROR ;
303336 }
304- s -> d_buf = overlay + s -> lit_bufsize /sizeof (ush );
305- s -> l_buf = s -> pending_buf + (1 + sizeof (ush ))* s -> lit_bufsize ;
337+ s -> sym_buf = s -> pending_buf + s -> lit_bufsize ;
338+ s -> sym_end = (s -> lit_bufsize - 1 ) * 3 ;
339+ /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
340+ * on 16 bit machines and because stored blocks are restricted to
341+ * 64K-1 bytes.
342+ */
306343
307344 s -> level = level ;
308345 s -> strategy = strategy ;
@@ -920,8 +957,7 @@ int ZEXPORT deflateCopy (dest, source)
920957 ds -> window = (Bytef * ) ZALLOC (dest , ds -> w_size , 2 * sizeof (Byte ));
921958 ds -> prev = (Posf * ) ZALLOC (dest , ds -> w_size , sizeof (Pos ));
922959 ds -> head = (Posf * ) ZALLOC (dest , ds -> hash_size , sizeof (Pos ));
923- overlay = (ushf * ) ZALLOC (dest , ds -> lit_bufsize , sizeof (ush )+ 2 );
924- ds -> pending_buf = (uchf * ) overlay ;
960+ ds -> pending_buf = (uchf * ) ZALLOC (dest , ds -> lit_bufsize , 4 );
925961
926962 if (ds -> window == Z_NULL || ds -> prev == Z_NULL || ds -> head == Z_NULL ||
927963 ds -> pending_buf == Z_NULL ) {
@@ -935,8 +971,7 @@ int ZEXPORT deflateCopy (dest, source)
935971 zmemcpy (ds -> pending_buf , ss -> pending_buf , (uInt )ds -> pending_buf_size );
936972
937973 ds -> pending_out = ds -> pending_buf + (ss -> pending_out - ss -> pending_buf );
938- ds -> d_buf = overlay + ds -> lit_bufsize /sizeof (ush );
939- ds -> l_buf = ds -> pending_buf + (1 + sizeof (ush ))* ds -> lit_bufsize ;
974+ ds -> sym_buf = ds -> pending_buf + ds -> lit_bufsize ;
940975
941976 ds -> l_desc .dyn_tree = ds -> dyn_ltree ;
942977 ds -> d_desc .dyn_tree = ds -> dyn_dtree ;
0 commit comments