]> wimlib.net Git - wimlib/blobdiff - src/compress.c
Add kind-of-working LZMS decompression using cabinet.dll API
[wimlib] / src / compress.c
index 86c3c2bcd7bb28cd7e7614b191f112b95b252bc0..3a936347df6de044ca48d68a2ef10ac04e740d90 100644 (file)
@@ -28,6 +28,7 @@
 #endif
 
 #include "wimlib/assert.h"
 #endif
 
 #include "wimlib/assert.h"
+#include "wimlib/endianness.h"
 #include "wimlib/compiler.h"
 #include "wimlib/compress.h"
 #include "wimlib/util.h"
 #include "wimlib/compiler.h"
 #include "wimlib/compress.h"
 #include "wimlib/util.h"
@@ -41,6 +42,7 @@ void
 bitstream_put_bits(struct output_bitstream *ostream, u32 bits,
                   unsigned num_bits)
 {
 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
        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
@@ -125,7 +127,7 @@ init_output_bitstream(struct output_bitstream *ostream,
 }
 
 typedef struct {
 }
 
 typedef struct {
-       u32 freq;
+       input_idx_t freq;
        u16 sym;
        union {
                u16 path_len;
        u16 sym;
        union {
                u16 path_len;
@@ -148,12 +150,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
@@ -249,14 +251,14 @@ huffman_tree_compute_path_lengths(HuffmanNode *base_node, u16 cur_len)
 void
 make_canonical_huffman_code(unsigned num_syms,
                            unsigned max_codeword_len,
 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. */
                            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]));
 
        /* Initialize the lengths and codewords to 0 */
        memset(lens, 0, num_syms * sizeof(lens[0]));