]> wimlib.net Git - wimlib/blob - include/wimlib/compress_common.h
666f10dcc43a031b5a9af818c1a00edbac35364b
[wimlib] / include / wimlib / compress_common.h
1 /*
2  * compress_common.h
3  *
4  * Header for compression code shared by multiple compression formats.
5  */
6
7 #ifndef _WIMLIB_COMPRESS_COMMON_H
8 #define _WIMLIB_COMPRESS_COMMON_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 extern void
68 make_canonical_huffman_code(unsigned num_syms,
69                             unsigned max_codeword_len,
70                             const input_idx_t freq_tab[restrict],
71                             u8 lens[restrict],
72                             u16 codewords[restrict]);
73
74 #endif /* _WIMLIB_COMPRESS_H */