]> wimlib.net Git - wimlib/commitdiff
cmp_nodes_by_freq(): Correctly handle large frequencies
authorEric Biggers <ebiggers3@gmail.com>
Fri, 13 Dec 2013 16:20:43 +0000 (10:20 -0600)
committerEric Biggers <ebiggers3@gmail.com>
Fri, 13 Dec 2013 16:21:12 +0000 (10:21 -0600)
src/compress.c

index 86c3c2bcd7bb28cd7e7614b191f112b95b252bc0..14046b19463f9bb4c28935b41cd21310440bcc90 100644 (file)
@@ -125,7 +125,7 @@ init_output_bitstream(struct output_bitstream *ostream,
 }
 
 typedef struct {
 }
 
 typedef struct {
-       u32 freq;
+       freq_t freq;
        u16 sym;
        union {
                u16 path_len;
        u16 sym;
        union {
                u16 path_len;
@@ -148,12 +148,12 @@ cmp_nodes_by_freq(const void *_leaf1, const void *_leaf2)
        const HuffmanNode *leaf1 = _leaf1;
        const HuffmanNode *leaf2 = _leaf2;
 
        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
        else
-               return freq_diff;
+               return (int)leaf1->sym - (int)leaf2->sym;
 }
 
 /* Comparator function for HuffmanNodes.  Sorts primarily by code length and
 }
 
 /* Comparator function for HuffmanNodes.  Sorts primarily by code length and
@@ -256,7 +256,7 @@ make_canonical_huffman_code(unsigned num_syms,
        /* 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. */
        /* 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]));
 
        /* Initialize the lengths and codewords to 0 */
        memset(lens, 0, num_syms * sizeof(lens[0]));