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