]> wimlib.net Git - wimlib/blob - src/comp.h
7b28e511d637ac8f15aa80c9cb434186e25453e5
[wimlib] / src / comp.h
1 /*
2  * comp.h
3  *
4  * Functions useful for compression, mainly bitstreams.
5  *
6  * Copyright (C) 2012 Eric Biggers
7  *
8  * wimlib - Library for working with WIM files 
9  *
10  * This library is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU Lesser General Public License as published by the Free
12  * Software Foundation; either version 2.1 of the License, or (at your option) any
13  * later version.
14  *
15  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License along
20  * with this library; if not, write to the Free Software Foundation, Inc., 59
21  * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
22  */
23
24 #ifndef _WIMLIB_COMP_H
25 #define _WIMLIB_COMP_H
26
27 #include "util.h"
28 #include "endianness.h"
29
30 typedef u16 output_bitbuf_t;
31
32 /* Structure to keep track of the current position in the compressed output. */
33 struct output_bitstream {
34
35         /* A variable to buffer writing bits to the output and is flushed to the
36          * compressed output when full. */
37         output_bitbuf_t bitbuf;
38
39         /* Number of free bits in @bitbuf */
40         uint free_bits;
41
42         u8 *bit_output;
43         u8 *next_bit_output;
44
45         /* Pointer to the next byte in the compressed output. */
46         u8 *output;
47
48
49         /* Number of bytes left in the memory pointed to by @output. */
50         int num_bytes_remaining;
51 };
52
53
54 static inline int bitstream_put_byte(struct output_bitstream *ostream,
55                                       u8 n)
56 {
57         if (ostream->num_bytes_remaining == 0)
58                 return 1;
59         *ostream->output = n;
60         ostream->output++;
61         ostream->num_bytes_remaining--;
62         return 0;
63 }
64
65 static inline int bitstream_put_two_bytes(struct output_bitstream *ostream,
66                                            u16 n)
67 {
68         if (ostream->num_bytes_remaining < 2)
69                 return 1;
70         *(u16*)ostream->output = to_le16(n);
71         ostream->output += 2;
72         ostream->num_bytes_remaining -= 2;
73         return 0;
74 }
75
76
77 struct lz_params {
78         uint min_match;
79         uint max_match;
80         uint nice_match;
81         uint good_match;
82         uint max_chain_len;
83         uint max_lazy_match;
84         uint too_far;
85 };
86                                                 
87 typedef uint (*lz_record_match_t)(uint, uint, void *, void *);
88 typedef uint (*lz_record_literal_t)(u8, void *);
89
90 extern uint lz_analyze_block(const u8 uncompressed_data[], 
91                              uint uncompressed_len,
92                              u32 match_tab[], 
93                              lz_record_match_t record_match,
94                              lz_record_literal_t record_literal, 
95                              void *record_match_arg1,
96                              void *record_match_arg2, 
97                              void *record_literal_arg,
98                              const struct lz_params *params);
99
100 extern int bitstream_put_bits(struct output_bitstream *ostream, 
101                               output_bitbuf_t bits, unsigned num_bits);
102
103 extern void init_output_bitstream(struct output_bitstream *ostream,
104                                                 void *data, unsigned num_bytes);
105
106 extern int flush_output_bitstream(struct output_bitstream *ostream);
107
108 extern void make_canonical_huffman_code(uint num_syms, uint max_codeword_len, 
109                                         const u32 freq_tab[], u8 lens[], 
110                                         u16 codewords[]);
111
112 #endif /* _WIMLIB_COMP_H */