]> wimlib.net Git - wimlib/blob - include/wimlib/lzx.h
da0c55143c4bc9a569bdc5550b51c0e16c16d80b
[wimlib] / include / wimlib / lzx.h
1 /*
2  * lzx.h
3  *
4  * Declarations shared between LZX compression and decompression.
5  */
6
7 #ifndef _WIMLIB_LZX_H
8 #define _WIMLIB_LZX_H
9
10 #include "wimlib/assert.h"
11 #include "wimlib/compiler.h"
12 #include "wimlib/lzx_constants.h"
13 #include "wimlib/util.h"
14 #include "wimlib/types.h"
15
16 //#define ENABLE_LZX_DEBUG
17 #ifdef ENABLE_LZX_DEBUG
18 #       define LZX_ASSERT wimlib_assert
19 #else
20 #       define LZX_ASSERT(...)
21 #endif
22
23 extern const u32 lzx_offset_slot_base[LZX_MAX_OFFSET_SLOTS];
24
25 extern const u8 lzx_extra_offset_bits[LZX_MAX_OFFSET_SLOTS];
26
27 /* Returns the LZX offset slot that corresponds to a given adjusted offset.
28  *
29  * Logically, this returns the smallest i such that
30  * adjusted_offset >= lzx_offset_slot_base[i].
31  *
32  * The actual implementation below takes advantage of the regularity of the
33  * numbers in the lzx_offset_slot_base array to calculate the slot directly from
34  * the adjusted offset without actually looking at the array.
35  */
36 static inline unsigned
37 lzx_get_offset_slot_raw(u32 adjusted_offset)
38 {
39         if (adjusted_offset >= 196608) {
40                 return (adjusted_offset >> 17) + 34;
41         } else {
42                 LZX_ASSERT(2 <= adjusted_offset && adjusted_offset < 655360);
43                 unsigned mssb_idx = bsr32(adjusted_offset);
44                 return (mssb_idx << 1) |
45                         ((adjusted_offset >> (mssb_idx - 1)) & 1);
46         }
47 }
48
49 extern unsigned lzx_get_window_order(size_t max_block_size);
50
51 extern unsigned lzx_get_num_main_syms(unsigned window_order);
52
53 /* Least-recently used queue for match offsets.  */
54 struct lzx_lru_queue {
55         u32 R[LZX_NUM_RECENT_OFFSETS];
56 } _aligned_attribute(sizeof(unsigned long));
57
58 /* Initialize the LZX least-recently-used match offset queue at the beginning of
59  * a new window for either decompression or compression.  */
60 static inline void
61 lzx_lru_queue_init(struct lzx_lru_queue *queue)
62 {
63         for (unsigned i = 0; i < LZX_NUM_RECENT_OFFSETS; i++)
64                 queue->R[i] = 1;
65 }
66
67 extern void
68 lzx_do_e8_preprocessing(u8 *data, u32 size);
69
70 extern void
71 lzx_undo_e8_preprocessing(u8 *data, u32 size);
72
73 #endif /* _WIMLIB_LZX_H */