4 #include "wimlib/assert.h"
5 #include "wimlib/types.h"
7 //#define ENABLE_LZX_DEBUG
8 #ifdef ENABLE_LZX_DEBUG
9 # define LZX_DEBUG DEBUG
11 # define LZX_DEBUG(format, ...)
14 /* Constants, most of which are defined by the LZX specification: */
16 /* The smallest and largest allowed match lengths. */
17 #define LZX_MIN_MATCH 2
18 #define LZX_MAX_MATCH 257
20 /* Number of values an uncompressed literal byte can represent. */
21 #define LZX_NUM_CHARS 256
23 /* Each LZX block begins with 3 bits that determines the block type. Below are
24 * the valid block types. Values 0, and 4 through 7, are invalid. */
25 #define LZX_BLOCKTYPE_VERBATIM 1
26 #define LZX_BLOCKTYPE_ALIGNED 2
27 #define LZX_BLOCKTYPE_UNCOMPRESSED 3
29 #define LZX_NUM_PRIMARY_LENS 7 /* this one missing from spec! */
31 /* NOTE: There are really 51 position slots in the LZX format as a whole, but
32 * only 30 are needed to allow for the window to be up to 32768 bytes long,
33 * which is the maximum in the WIM format. */
34 #define LZX_NUM_POSITION_SLOTS 30
36 /* Read the LZX specification for information about the Huffman trees used in
37 * the LZX compression format. Basically there are 4 of them: The main tree,
38 * the length tree, the pre tree, and the aligned tree. The main tree and
39 * length tree are given at the beginning of VERBATIM and ALIGNED blocks as a
40 * list of *_NUM_SYMBOLS code length values. They are read using the
41 * read_code_lens() function and built using the make_decode_table() function.
42 * The decode table is not a real tree but rather a table that we can index by
43 * some number of bits (*_TABLEBITS) of the input to quickly look up the symbol
44 * corresponding to a Huffman code.
46 * The ALIGNED tree is only present on ALIGNED blocks.
48 * A PRETREE is used to encode the code lengths for the main tree and the length
49 * tree. There is a separate pretree for each half of the main tree. */
51 #define LZX_MAINTREE_NUM_SYMBOLS (LZX_NUM_CHARS + \
52 (LZX_NUM_POSITION_SLOTS << 3))
53 #define LZX_MAINTREE_TABLEBITS 11
55 #define LZX_LENTREE_NUM_SYMBOLS 249
56 #define LZX_LENTREE_TABLEBITS 10
58 #define LZX_PRETREE_NUM_SYMBOLS 20
59 #define LZX_PRETREE_TABLEBITS 6
60 #define LZX_PRETREE_ELEMENT_SIZE 4
62 #define LZX_ALIGNEDTREE_NUM_SYMBOLS 8
63 #define LZX_ALIGNEDTREE_TABLEBITS 7
64 #define LZX_ALIGNEDTREE_ELEMENT_SIZE 3
66 /* Maximum allowed length of a Huffman code. */
67 #define LZX_MAX_CODEWORD_LEN 16
69 /* For the LZX-compressed blocks in WIM files, this value is always used as the
70 * filesize parameter for the call instruction (0xe8 byte) preprocessing, even
71 * though the blocks themselves are not this size, and the size of the actual
72 * file resource in the WIM file is very likely to be something entirely
73 * different as well. */
74 #define LZX_WIM_MAGIC_FILESIZE 12000000
76 #define USE_LZX_EXTRA_BITS_ARRAY
78 #ifdef USE_LZX_EXTRA_BITS_ARRAY
79 extern const u8 lzx_extra_bits[LZX_NUM_POSITION_SLOTS];
82 /* Given the number of a LZX position slot, return the number of extra bits that
83 * are needed to encode the match offset. */
84 static inline unsigned
85 lzx_get_num_extra_bits(unsigned position_slot)
87 #ifdef USE_LZX_EXTRA_BITS_ARRAY
89 return lzx_extra_bits[position_slot];
91 /* Calculate directly using a shift and subtraction. */
92 wimlib_assert(position_slot >= 2 && position_slot <= 37);
93 return (position_slot >> 1) - 1;
97 extern const u32 lzx_position_base[LZX_NUM_POSITION_SLOTS];
99 /* Least-recently used queue for match offsets. */
106 #endif /* _WIMLIB_LZX_H */