4 * Functions useful for compression, mainly bitstreams.
7 #ifndef _WIMLIB_COMPRESS_H
8 #define _WIMLIB_COMPRESS_H
11 #include "endianness.h"
13 typedef u16 output_bitbuf_t;
15 /* Assuming that WIM chunks are at most 32768 bytes, 16 bits is enough for any
16 * symbol frequency. */
19 /* Structure to keep track of the current position in the compressed output. */
20 struct output_bitstream {
22 /* A variable to buffer writing bits to the output and is flushed to the
23 * compressed output when full. */
24 output_bitbuf_t bitbuf;
26 /* Number of free bits in @bitbuf */
32 /* Pointer to the next byte in the compressed output. */
36 /* Number of bytes left in the memory pointed to by @output. */
37 int num_bytes_remaining;
40 static inline int bitstream_put_byte(struct output_bitstream *ostream,
43 if (ostream->num_bytes_remaining < 1)
47 ostream->num_bytes_remaining--;
51 static inline int bitstream_put_two_bytes(struct output_bitstream *ostream,
54 if (ostream->num_bytes_remaining < 2)
56 *(u16*)ostream->output = cpu_to_le16(n);
58 ostream->num_bytes_remaining -= 2;
67 unsigned max_chain_len;
68 unsigned max_lazy_match;
72 typedef unsigned (*lz_record_match_t)(unsigned, unsigned, void *, void *);
73 typedef unsigned (*lz_record_literal_t)(u8, void *);
75 extern unsigned lz_analyze_block(const u8 uncompressed_data[],
76 unsigned uncompressed_len,
78 lz_record_match_t record_match,
79 lz_record_literal_t record_literal,
80 void *record_match_arg1,
81 void *record_match_arg2,
82 void *record_literal_arg,
83 const struct lz_params *params);
85 extern int bitstream_put_bits(struct output_bitstream *ostream,
86 output_bitbuf_t bits, unsigned num_bits);
88 extern void init_output_bitstream(struct output_bitstream *ostream,
89 void *data, unsigned num_bytes);
91 extern int flush_output_bitstream(struct output_bitstream *ostream);
93 extern void make_canonical_huffman_code(unsigned num_syms,
94 unsigned max_codeword_len,
95 const freq_t freq_tab[],
99 #endif /* _WIMLIB_COMPRESS_H */