]> wimlib.net Git - wimlib/blob - include/wimlib/lzx_common.h
Various cleanups
[wimlib] / include / wimlib / lzx_common.h
1 /*
2  * lzx_common.h
3  *
4  * Declarations shared between LZX compression and decompression.
5  */
6
7 #ifndef _LZX_COMMON_H
8 #define _LZX_COMMON_H
9
10 #include "wimlib/bitops.h"
11 #include "wimlib/lzx_constants.h"
12 #include "wimlib/types.h"
13
14 //#define ENABLE_LZX_DEBUG
15 #ifdef ENABLE_LZX_DEBUG
16 #  include "wimlib/assert.h"
17 #  define LZX_ASSERT wimlib_assert
18 #else
19 #  define LZX_ASSERT(...)
20 #endif
21
22 extern const u32 lzx_offset_slot_base[LZX_MAX_OFFSET_SLOTS + 1];
23
24 extern const u8 lzx_extra_offset_bits[LZX_MAX_OFFSET_SLOTS];
25
26 /*
27  * Return the offset slot for the specified match offset.
28  *
29  * This returns the smallest i such that:
30  *
31  *      offset + LZX_OFFSET_ADJUSTMENT >= lzx_offset_slot_base[i]
32  *
33  * However, the actual implementation below takes advantage of the regularity of
34  * the offset slot bases to calculate the slot directly from the adjusted offset
35  * without actually looking at the array.
36  */
37 static inline unsigned
38 lzx_get_offset_slot(u32 offset)
39 {
40         u32 adjusted_offset = offset + LZX_OFFSET_ADJUSTMENT;
41         if (adjusted_offset >= 196608) {
42                 return (adjusted_offset >> 17) + 34;
43         } else {
44                 unsigned mssb_idx = fls32(adjusted_offset);
45                 return (mssb_idx << 1) |
46                         ((adjusted_offset >> (mssb_idx - 1)) & 1);
47         }
48 }
49
50 static inline unsigned
51 lzx_main_symbol_for_literal(unsigned literal)
52 {
53         return literal;
54 }
55
56 static inline unsigned
57 lzx_main_symbol_for_match(unsigned offset_slot, unsigned len_header)
58 {
59         return LZX_NUM_CHARS + (offset_slot * LZX_NUM_LEN_HEADERS) + len_header;
60 }
61
62 extern unsigned
63 lzx_get_window_order(size_t max_bufsize);
64
65 extern unsigned
66 lzx_get_num_offset_slots(unsigned window_order);
67
68 extern unsigned
69 lzx_get_num_main_syms(unsigned window_order);
70
71 extern void
72 lzx_do_e8_preprocessing(u8 *data, u32 size);
73
74 extern void
75 lzx_undo_e8_preprocessing(u8 *data, u32 size);
76
77 #endif /* _LZX_COMMON_H */