X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fcompress.c;h=79fe4ddf54e320aeeac7da13e8110a6ada23944c;hb=7bac2f53ecca0fe8d6f9e55a9bf8f86512256660;hp=76043d632b98f6f3e670c948f5f09b8d62521d90;hpb=4ecf344e77e1f5891055881950a6e89e32b16008;p=wimlib diff --git a/src/compress.c b/src/compress.c index 76043d63..79fe4ddf 100644 --- a/src/compress.c +++ b/src/compress.c @@ -28,6 +28,7 @@ #endif #include "wimlib/assert.h" +#include "wimlib/endianness.h" #include "wimlib/compiler.h" #include "wimlib/compress.h" #include "wimlib/util.h" @@ -121,12 +122,12 @@ init_output_bitstream(struct output_bitstream *ostream, ostream->bit_output = data; ostream->next_bit_output = data + 2; ostream->output = data + 4; - ostream->bytes_remaining = num_bytes; + ostream->bytes_remaining = num_bytes - 4; ostream->overrun = false; } typedef struct { - freq_t freq; + input_idx_t freq; u16 sym; union { u16 path_len; @@ -231,26 +232,26 @@ huffman_tree_compute_path_lengths(HuffmanNode *base_node, u16 cur_len) * @num_syms: The number of symbols in the alphabet. * * @max_codeword_len: The maximum allowed length of a codeword in the code. - * Note that if the code being created runs up against - * this restriction, the code ultimately created will be - * suboptimal, although there are some advantages for - * limiting the length of the codewords. + * Note that if the code being created runs up against + * this restriction, the code ultimately created will be + * suboptimal, although there are some advantages for + * limiting the length of the codewords. * * @freq_tab: An array of length @num_syms that contains the frequencies - * of each symbol in the uncompressed data. + * of each symbol in the uncompressed data. * * @lens: An array of length @num_syms into which the lengths of the - * codewords for each symbol will be written. + * codewords for each symbol will be written. * * @codewords: An array of @num_syms short integers into which the - * codewords for each symbol will be written. The first - * lens[i] bits of codewords[i] will contain the codeword - * for symbol i. + * codewords for each symbol will be written. The first + * lens[i] bits of codewords[i] will contain the codeword + * for symbol i. */ 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]) { @@ -427,8 +428,8 @@ try_building_tree_again: * 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++;