#endif
#include "wimlib/assert.h"
+#include "wimlib/endianness.h"
#include "wimlib/compiler.h"
#include "wimlib/compress.h"
#include "wimlib/util.h"
bitstream_put_bits(struct output_bitstream *ostream, u32 bits,
unsigned num_bits)
{
+ bits &= (1U << num_bits) - 1;
while (num_bits > ostream->free_bits) {
/* Buffer variable does not have space for the new bits. It
* needs to be flushed as a 16-bit integer. Bits in the second
}
typedef struct {
- u32 freq;
+ input_idx_t freq;
u16 sym;
union {
u16 path_len;
const HuffmanNode *leaf1 = _leaf1;
const HuffmanNode *leaf2 = _leaf2;
- int freq_diff = (int)leaf1->freq - (int)leaf2->freq;
-
- if (freq_diff == 0)
- return (int)leaf1->sym - (int)leaf2->sym;
+ if (leaf1->freq > leaf2->freq)
+ return 1;
+ else if (leaf1->freq < leaf2->freq)
+ return -1;
else
- return freq_diff;
+ return (int)leaf1->sym - (int)leaf2->sym;
}
/* Comparator function for HuffmanNodes. Sorts primarily by code length and
void
make_canonical_huffman_code(unsigned num_syms,
unsigned max_codeword_len,
- const freq_t freq_tab[restrict],
+ const input_idx_t freq_tab[restrict],
u8 lens[restrict],
u16 codewords[restrict])
{
/* We require at least 2 possible symbols in the alphabet to produce a
* valid Huffman decoding table. It is allowed that fewer than 2 symbols
* are actually used, though. */
- wimlib_assert(num_syms >= 2);
+ wimlib_assert(num_syms >= 2 && num_syms < INVALID_SYMBOL);
/* Initialize the lengths and codewords to 0 */
memset(lens, 0, num_syms * sizeof(lens[0]));
* log_2(num_used_symbols).
* */
for (unsigned i = 0; i < num_used_symbols; i++)
- if (leaves[i].freq > 1)
- leaves[i].freq >>= 1;
+ leaves[i].freq = (leaves[i].freq + 1) >> 1;
+
goto try_building_tree_again;
}
next_inode++;