4 * Functions useful for compression, mainly bitstreams.
6 * Copyright (C) 2012 Eric Biggers
8 * wimlib - Library for working with WIM files
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
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.
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
24 #ifndef _WIMLIB_COMP_H
25 #define _WIMLIB_COMP_H
28 #include "endianness.h"
30 typedef u16 output_bitbuf_t;
32 /* Structure to keep track of the current position in the compressed output. */
33 struct output_bitstream {
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;
39 /* Number of free bits in @bitbuf */
45 /* Pointer to the next byte in the compressed output. */
49 /* Number of bytes left in the memory pointed to by @output. */
50 int num_bytes_remaining;
54 static inline int bitstream_put_byte(struct output_bitstream *ostream,
57 if (ostream->num_bytes_remaining == 0)
61 ostream->num_bytes_remaining--;
65 static inline int bitstream_put_two_bytes(struct output_bitstream *ostream,
68 if (ostream->num_bytes_remaining < 2)
70 *(u16*)ostream->output = to_le16(n);
72 ostream->num_bytes_remaining -= 2;
87 typedef uint (*lz_record_match_t)(uint, uint, void *, void *);
88 typedef uint (*lz_record_literal_t)(u8, void *);
90 extern uint lz_analyze_block(const u8 uncompressed_data[],
91 uint uncompressed_len,
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);
100 extern int bitstream_put_bits(struct output_bitstream *ostream,
101 output_bitbuf_t bits, unsigned num_bits);
103 extern void init_output_bitstream(struct output_bitstream *ostream,
104 void *data, unsigned num_bytes);
106 extern int flush_output_bitstream(struct output_bitstream *ostream);
108 extern void make_canonical_huffman_code(uint num_syms, uint max_codeword_len,
109 const u32 freq_tab[], u8 lens[],
112 #endif /* _WIMLIB_COMP_H */