]> wimlib.net Git - wimlib/blob - src/comp.h
XPRESS compressor: increase max match
[wimlib] / src / comp.h
1 /*
2  * comp.h
3  *
4  * Functions useful for compression, mainly bitstreams.
5  */
6
7 #ifndef _WIMLIB_COMP_H
8 #define _WIMLIB_COMP_H
9
10 #include "util.h"
11 #include "endianness.h"
12
13 typedef u16 output_bitbuf_t;
14
15 /* Structure to keep track of the current position in the compressed output. */
16 struct output_bitstream {
17
18         /* A variable to buffer writing bits to the output and is flushed to the
19          * compressed output when full. */
20         output_bitbuf_t bitbuf;
21
22         /* Number of free bits in @bitbuf */
23         uint free_bits;
24
25         u8 *bit_output;
26         u8 *next_bit_output;
27
28         /* Pointer to the next byte in the compressed output. */
29         u8 *output;
30
31
32         /* Number of bytes left in the memory pointed to by @output. */
33         int num_bytes_remaining;
34 };
35
36 static inline int bitstream_put_byte(struct output_bitstream *ostream,
37                                      u8 n)
38 {
39         if (ostream->num_bytes_remaining < 1)
40                 return 1;
41         *ostream->output = n;
42         ostream->output++;
43         ostream->num_bytes_remaining--;
44         return 0;
45 }
46
47 static inline int bitstream_put_two_bytes(struct output_bitstream *ostream,
48                                           u16 n)
49 {
50         if (ostream->num_bytes_remaining < 2)
51                 return 1;
52         *(u16*)ostream->output = cpu_to_le16(n);
53         ostream->output += 2;
54         ostream->num_bytes_remaining -= 2;
55         return 0;
56 }
57
58 struct lz_params {
59         uint min_match;
60         uint max_match;
61         uint nice_match;
62         uint good_match;
63         uint max_chain_len;
64         uint max_lazy_match;
65         uint too_far;
66 };
67
68 typedef uint (*lz_record_match_t)(uint, uint, void *, void *);
69 typedef uint (*lz_record_literal_t)(u8, void *);
70
71 extern uint lz_analyze_block(const u8 uncompressed_data[],
72                              uint uncompressed_len,
73                              u32 match_tab[],
74                              lz_record_match_t record_match,
75                              lz_record_literal_t record_literal,
76                              void *record_match_arg1,
77                              void *record_match_arg2,
78                              void *record_literal_arg,
79                              const struct lz_params *params);
80
81 extern int bitstream_put_bits(struct output_bitstream *ostream,
82                               output_bitbuf_t bits, unsigned num_bits);
83
84 extern void init_output_bitstream(struct output_bitstream *ostream,
85                                   void *data, unsigned num_bytes);
86
87 extern int flush_output_bitstream(struct output_bitstream *ostream);
88
89 extern void make_canonical_huffman_code(uint num_syms, uint max_codeword_len,
90                                         const u32 freq_tab[], u8 lens[],
91                                         u16 codewords[]);
92
93 #endif /* _WIMLIB_COMP_H */