]> wimlib.net Git - wimlib/blob - include/wimlib/lzx.h
Add WIMLIB_OPEN_FLAG_WRITE_ACCESS flag
[wimlib] / include / wimlib / lzx.h
1 #ifndef _WIMLIB_LZX_H
2 #define _WIMLIB_LZX_H
3
4 #include "wimlib/assert.h"
5 #include "wimlib/types.h"
6
7 //#define ENABLE_LZX_DEBUG
8 #ifdef ENABLE_LZX_DEBUG
9 #       define LZX_DEBUG DEBUG
10 #else
11 #       define LZX_DEBUG(format, ...)
12 #endif
13
14 /* Constants, most of which are defined by the LZX specification: */
15
16 /* The smallest and largest allowed match lengths. */
17 #define LZX_MIN_MATCH                2
18 #define LZX_MAX_MATCH                257
19
20 /* Number of values an uncompressed literal byte can represent. */
21 #define LZX_NUM_CHARS                256
22
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
28
29 #define LZX_NUM_PRIMARY_LENS         7  /* this one missing from spec! */
30
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
35
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.
45  *
46  * The ALIGNED tree is only present on ALIGNED blocks.
47  *
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.  */
50
51 #define LZX_MAINTREE_NUM_SYMBOLS         (LZX_NUM_CHARS + \
52                                         (LZX_NUM_POSITION_SLOTS << 3))
53 #define LZX_MAINTREE_TABLEBITS          11
54
55 #define LZX_LENTREE_NUM_SYMBOLS         249
56 #define LZX_LENTREE_TABLEBITS           10
57
58 #define LZX_PRETREE_NUM_SYMBOLS         20
59 #define LZX_PRETREE_TABLEBITS           6
60 #define LZX_PRETREE_ELEMENT_SIZE        4
61
62 #define LZX_ALIGNEDTREE_NUM_SYMBOLS     8
63 #define LZX_ALIGNEDTREE_TABLEBITS       7
64 #define LZX_ALIGNEDTREE_ELEMENT_SIZE    3
65
66 /* Maximum allowed length of a Huffman code. */
67 #define LZX_MAX_CODEWORD_LEN            16
68
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
75
76 #define USE_LZX_EXTRA_BITS_ARRAY
77
78 #ifdef USE_LZX_EXTRA_BITS_ARRAY
79 extern const u8 lzx_extra_bits[LZX_NUM_POSITION_SLOTS];
80 #endif
81
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)
86 {
87 #ifdef USE_LZX_EXTRA_BITS_ARRAY
88         /* Use a table */
89         return lzx_extra_bits[position_slot];
90 #else
91         /* Calculate directly using a shift and subtraction. */
92         wimlib_assert(position_slot >= 2 && position_slot <= 37);
93         return (position_slot >> 1) - 1;
94 #endif
95 }
96
97 extern const u32 lzx_position_base[LZX_NUM_POSITION_SLOTS];
98
99 /* Least-recently used queue for match offsets. */
100 struct lru_queue {
101         u32 R0;
102         u32 R1;
103         u32 R2;
104 };
105
106 #endif /* _WIMLIB_LZX_H */