]> wimlib.net Git - wimlib/blob - include/wimlib/compress_common.h
Place common decompression/compression code in public domain
[wimlib] / include / wimlib / compress_common.h
1 /*
2  * compress_common.h
3  *
4  * Header for compression code shared by multiple compression formats.
5  *
6  * The author dedicates this file to the public domain.
7  * You can do whatever you want with this file.
8  */
9
10 #ifndef _WIMLIB_COMPRESS_COMMON_H
11 #define _WIMLIB_COMPRESS_COMMON_H
12
13 #include "wimlib/types.h"
14
15 /* Variable type that can represent all possible window positions.  */
16 #ifndef INPUT_IDX_T_DEFINED
17 #define INPUT_IDX_T_DEFINED
18 typedef u32 input_idx_t;
19 #endif
20
21 /* Structure to keep track of the current state sending bits and bytes to the
22  * compressed output buffer.  */
23 struct output_bitstream {
24
25         /* Variable that holds up to 16 bits that haven't yet been flushed to
26          * the output.  */
27         u16 bitbuf;
28
29         /* Number of free bits in @bitbuf; that is, 16 minus the number of valid
30          * bits in @bitbuf.  */
31         unsigned free_bits;
32
33         /* Pointer to the start of the output buffer.  */
34         u8 *output_start;
35
36         /* Position at which to write the next 16 bits.  */
37         u8 *bit_output;
38
39         /* Next position to write 16 bits, after they are written to bit_output.
40          * This is after @next_bit_output and may be separated from @bit_output
41          * by literal bytes.  */
42         u8 *next_bit_output;
43
44         /* Next position to write literal bytes.  This is after @bit_output and
45          * @next_bit_output, and may be separated from them by literal bytes.
46          */
47         u8 *output;
48
49         /* Number of bytes remaining in the @output buffer.  */
50         input_idx_t bytes_remaining;
51
52         /* Set to true if the buffer has been exhausted.  */
53         bool overrun;
54 };
55
56 extern void
57 init_output_bitstream(struct output_bitstream *ostream,
58                       void *data, unsigned num_bytes);
59
60 extern input_idx_t
61 flush_output_bitstream(struct output_bitstream *ostream);
62
63 extern void
64 bitstream_put_bits(struct output_bitstream *ostream,
65                    u32 bits, unsigned num_bits);
66
67 extern void
68 bitstream_put_byte(struct output_bitstream *ostream, u8 n);
69
70 extern void
71 make_canonical_huffman_code(unsigned num_syms,
72                             unsigned max_codeword_len,
73                             const input_idx_t freq_tab[restrict],
74                             u8 lens[restrict],
75                             u32 codewords[restrict]);
76
77 #endif /* _WIMLIB_COMPRESS_COMMON_H */