Variable LZX window sizes
[wimlib] / include / wimlib / compress.h
1 /*
2  * compress.h
3  *
4  * Header for compression code shared by multiple compression formats.
5  */
6
7 #ifndef _WIMLIB_COMPRESS_H
8 #define _WIMLIB_COMPRESS_H
9
10 #include "wimlib/endianness.h"
11 #include "wimlib/types.h"
12
13 typedef u16 output_bitbuf_t;
14
15 /* Variable type that can represent all possible window positions.  */
16 typedef u32 freq_t;
17 #ifndef INPUT_IDX_T_DEFINED
18 #define INPUT_IDX_T_DEFINED
19 typedef u32 input_idx_t;
20 #endif
21
22 /* Structure to keep track of the current position in the compressed output. */
23 struct output_bitstream {
24
25         /* A variable to buffer writing bits to the output and is flushed to the
26          * compressed output when full. */
27         output_bitbuf_t bitbuf;
28
29         /* Number of free bits in @bitbuf */
30         unsigned free_bits;
31
32         /* Pointer to the start of the output buffer.  */
33         u8 *output_start;
34
35         /* Position at which to write the next 16 bits.  */
36         u8 *bit_output;
37
38         /* Next position to write 16 bits, after they are written to bit_output.
39          * This is after @next_bit_output and may be separated from @bit_output
40          * by literal bytes.  */
41         u8 *next_bit_output;
42
43         /* Next position to write literal bytes.  This is after @bit_output and
44          * @next_bit_output, and may be separated from them by literal bytes.
45          */
46         u8 *output;
47
48         /* Bytes remaining in @output buffer.  */
49         input_idx_t bytes_remaining;
50
51         /* Set to true if the buffer has been exhausted.  */
52         bool overrun;
53 };
54
55 extern void
56 init_output_bitstream(struct output_bitstream *ostream,
57                       void *data, unsigned num_bytes);
58
59 extern input_idx_t
60 flush_output_bitstream(struct output_bitstream *ostream);
61
62 extern void
63 bitstream_put_bits(struct output_bitstream *ostream,
64                    u32 bits, unsigned num_bits);
65
66 extern void
67 bitstream_put_byte(struct output_bitstream *ostream, u8 n);
68
69 struct lz_params {
70         unsigned min_match;
71         unsigned max_match;
72         unsigned max_offset;
73         unsigned nice_match;
74         unsigned good_match;
75         unsigned max_chain_len;
76         unsigned max_lazy_match;
77         unsigned too_far;
78 };
79
80 typedef void (*lz_record_match_t)(unsigned len, unsigned offset, void *ctx);
81 typedef void (*lz_record_literal_t)(u8 lit, void *ctx);
82
83 extern void
84 lz_analyze_block(const u8 window[],
85                  input_idx_t window_size,
86                  lz_record_match_t record_match,
87                  lz_record_literal_t record_literal,
88                  void *record_ctx,
89                  const struct lz_params *params,
90                  input_idx_t prev_tab[]);
91
92 extern void
93 make_canonical_huffman_code(unsigned num_syms,
94                             unsigned max_codeword_len,
95                             const freq_t freq_tab[restrict],
96                             u8 lens[restrict],
97                             u16 codewords[restrict]);
98
99 #endif /* _WIMLIB_COMPRESS_H */