summaryrefslogtreecommitdiff
path: root/cpan/Compress-Raw-Zlib/zlib-src/trees.c
diff options
context:
space:
mode:
Diffstat (limited to 'cpan/Compress-Raw-Zlib/zlib-src/trees.c')
-rw-r--r--cpan/Compress-Raw-Zlib/zlib-src/trees.c40
1 files changed, 30 insertions, 10 deletions
diff --git a/cpan/Compress-Raw-Zlib/zlib-src/trees.c b/cpan/Compress-Raw-Zlib/zlib-src/trees.c
index de0b7f210c..d9c807d561 100644
--- a/cpan/Compress-Raw-Zlib/zlib-src/trees.c
+++ b/cpan/Compress-Raw-Zlib/zlib-src/trees.c
@@ -1,5 +1,5 @@
/* trees.c -- output deflated data using Huffman coding
- * Copyright (C) 1995-2012 Jean-loup Gailly
+ * Copyright (C) 1995-2010 Jean-loup Gailly
* detect_data_type() function provided freely by Cosmin Truta, 2006
* For conditions of distribution and use, see copyright notice in zlib.h
*/
@@ -74,6 +74,11 @@ local const uch bl_order[BL_CODES]
* probability, to avoid transmitting the lengths for unused bit length codes.
*/
+#define Buf_size (8 * 2*sizeof(char))
+/* Number of bits used within bi_buf. (bi_buf might be implemented on
+ * more than 16 bits on some systems.)
+ */
+
/* ===========================================================================
* Local data. These are initialized only once.
*/
@@ -394,6 +399,7 @@ void ZLIB_INTERNAL _tr_init(
s->bi_buf = 0;
s->bi_valid = 0;
+ s->last_eob_len = 8; /* enough lookahead for inflate */
#ifdef DEBUG
s->compressed_len = 0L;
s->bits_sent = 0L;
@@ -879,17 +885,15 @@ void ZLIB_INTERNAL _tr_stored_block(
}
/* ===========================================================================
- * Flush the bits in the bit buffer to pending output (leaves at most 7 bits)
- */
-void ZLIB_INTERNAL _tr_flush_bits(
- deflate_state *s)
-{
- bi_flush(s);
-}
-
-/* ===========================================================================
* Send one empty static block to give enough lookahead for inflate.
* This takes 10 bits, of which 7 may remain in the bit buffer.
+ * The current inflate code requires 9 bits of lookahead. If the
+ * last two codes for the previous block (real code plus EOB) were coded
+ * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
+ * the last real code. In this case we send two empty static blocks instead
+ * of one. (There are no problems if the previous block is stored or fixed.)
+ * To simplify the code, we assume the worst case of last real code encoded
+ * on one bit only.
*/
void ZLIB_INTERNAL _tr_align(
deflate_state *s)
@@ -900,6 +904,20 @@ void ZLIB_INTERNAL _tr_align(
s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
#endif
bi_flush(s);
+ /* Of the 10 bits for the empty block, we have already sent
+ * (10 - bi_valid) bits. The lookahead for the last real code (before
+ * the EOB of the previous block) was thus at least one plus the length
+ * of the EOB plus what we have just sent of the empty static block.
+ */
+ if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
+ send_bits(s, STATIC_TREES<<1, 3);
+ send_code(s, END_BLOCK, static_ltree);
+#ifdef DEBUG
+ s->compressed_len += 10L;
+#endif
+ bi_flush(s);
+ }
+ s->last_eob_len = 7;
}
/* ===========================================================================
@@ -1102,6 +1120,7 @@ local void compress_block(
} while (lx < s->last_lit);
send_code(s, END_BLOCK, ltree);
+ s->last_eob_len = ltree[END_BLOCK].Len;
}
/* ===========================================================================
@@ -1209,6 +1228,7 @@ local void copy_block(
int header)
{
bi_windup(s); /* align on byte boundary */
+ s->last_eob_len = 8; /* enough lookahead for inflate */
if (header) {
put_short(s, (ush)len);