lzx_common: add constant for maximum number of extra offset bits
authorEric Biggers <ebiggers3@gmail.com>
Sat, 9 Jul 2016 15:01:20 +0000 (10:01 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Sat, 9 Jul 2016 15:01:20 +0000 (10:01 -0500)
include/wimlib/lzx_constants.h
src/lzx_compress.c

index 788eded..20c4c62 100644 (file)
 /* The offset slot base for LZX_MIN_ALIGNED_OFFSET_SLOT.  */
 #define LZX_MIN_ALIGNED_OFFSET         14
 
+/* The maximum number of extra offset bits in verbatim blocks.  (One would need
+ * to subtract LZX_NUM_ALIGNED_OFFSET_BITS to get the number of extra offset
+ * bits in *aligned* blocks.)  */
+#define LZX_MAX_NUM_EXTRA_BITS         17
+
 /* Maximum lengths (in bits) for length-limited Huffman code construction.  */
 #define LZX_MAX_MAIN_CODEWORD_LEN      16
 #define LZX_MAX_LEN_CODEWORD_LEN       16
index 8aff56f..10b5190 100644 (file)
@@ -976,8 +976,11 @@ lzx_write_sequences(struct lzx_output_bitstream *os, int block_type,
                extra_bits = adjusted_offset - (lzx_offset_slot_base[offset_slot] +
                                                LZX_OFFSET_ADJUSTMENT);
 
-       #define MAX_MATCH_BITS  (MAIN_CODEWORD_LIMIT + LENGTH_CODEWORD_LIMIT + \
-                                14 + ALIGNED_CODEWORD_LIMIT)
+       #define MAX_MATCH_BITS (MAIN_CODEWORD_LIMIT +           \
+                               LENGTH_CODEWORD_LIMIT +         \
+                               LZX_MAX_NUM_EXTRA_BITS -        \
+                               LZX_NUM_ALIGNED_OFFSET_BITS +   \
+                               ALIGNED_CODEWORD_LIMIT)
 
                /* Verify optimization is enabled on 64-bit  */
                STATIC_ASSERT(WORDBITS < 64 || CAN_BUFFER(MAX_MATCH_BITS));
@@ -1011,7 +1014,8 @@ lzx_write_sequences(struct lzx_output_bitstream *os, int block_type,
                        lzx_add_bits(os, extra_bits >> LZX_NUM_ALIGNED_OFFSET_BITS,
                                     num_extra_bits - LZX_NUM_ALIGNED_OFFSET_BITS);
                        if (!CAN_BUFFER(MAX_MATCH_BITS))
-                               lzx_flush_bits(os, 14);
+                               lzx_flush_bits(os, LZX_MAX_NUM_EXTRA_BITS -
+                                                  LZX_NUM_ALIGNED_OFFSET_BITS);
 
                        lzx_add_bits(os, codes->codewords.aligned[adjusted_offset &
                                                                  LZX_ALIGNED_OFFSET_BITMASK],
@@ -1020,11 +1024,11 @@ lzx_write_sequences(struct lzx_output_bitstream *os, int block_type,
                        if (!CAN_BUFFER(MAX_MATCH_BITS))
                                lzx_flush_bits(os, ALIGNED_CODEWORD_LIMIT);
                } else {
-                       STATIC_ASSERT(CAN_BUFFER(17));
+                       STATIC_ASSERT(CAN_BUFFER(LZX_MAX_NUM_EXTRA_BITS));
 
                        lzx_add_bits(os, extra_bits, num_extra_bits);
                        if (!CAN_BUFFER(MAX_MATCH_BITS))
-                               lzx_flush_bits(os, 17);
+                               lzx_flush_bits(os, LZX_MAX_NUM_EXTRA_BITS);
                }
 
                if (CAN_BUFFER(MAX_MATCH_BITS))