]> wimlib.net Git - wimlib/blobdiff - src/lzx-comp.c
Fix up LZ77 compression code and prepare v1.0.3
[wimlib] / src / lzx-comp.c
index caedc9a1e5d80d59d2b972b44d7614761985a1cb..8267a2eaece40d28187adee2c114a953f2ebe825 100644 (file)
@@ -5,24 +5,26 @@
  *
  * This code was originally based on code written by Matthew T. Russotto
  *     (liblzxcomp).
- *
+ */
+
+/*
  * Copyright (C) 2002 Matthew T. Russotto
  * Copyright (C) 2012 Eric Biggers
  *
- * wimlib - Library for working with WIM files 
+ * This file is part of wimlib, a library for working with WIM files.
  *
- * This library is free software; you can redistribute it and/or modify it under
- * the terms of the GNU Lesser General Public License as published by the Free
- * Software Foundation; either version 2.1 of the License, or (at your option) any
- * later version.
+ * wimlib is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.
  *
- * This library is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
  *
- * You should have received a copy of the GNU Lesser General Public License along
- * with this library; if not, write to the Free Software Foundation, Inc., 59
- * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
+ * You should have received a copy of the GNU General Public License
+ * along with wimlib; if not, see http://www.gnu.org/licenses/.
  */
 
 
@@ -468,7 +470,7 @@ static int lzx_write_compressed_tree(struct output_bitstream *out,
                                if (delta < 0)
                                        delta += 17;
                                pretree_freqs[19]++;
-                               pretree_freqs[delta]++;
+                               pretree_freqs[(unsigned char)delta]++;
                                output_syms[output_syms_idx++] = 19;
                                output_syms[output_syms_idx++] = additional_bits;
                                output_syms[output_syms_idx++] = delta;
@@ -484,7 +486,7 @@ static int lzx_write_compressed_tree(struct output_bitstream *out,
                        if (delta < 0)
                                delta += 17;
 
-                       pretree_freqs[delta]++;
+                       pretree_freqs[(unsigned char)delta]++;
                        output_syms[output_syms_idx++] = delta;
                }
 
@@ -597,7 +599,11 @@ static void do_call_insn_preprocessing(u8 uncompressed_data[],
 
 
 static const struct lz_params lzx_lz_params = {
+
+        /* LZX_MIN_MATCH == 2, but 2-character matches are rarely useful; the
+         * minimum match for compression is set to 3 instead. */
        .min_match      = 3,
+
        .max_match      = LZX_MAX_MATCH,
        .good_match     = LZX_MAX_MATCH,
        .nice_match     = LZX_MAX_MATCH,
@@ -636,8 +642,9 @@ int lzx_compress(const void *__uncompressed_data, uint uncompressed_len,
        uint compressed_len;
        uint i;
        int ret;
+       int block_type = LZX_BLOCKTYPE_ALIGNED;
 
-       LZX_DEBUG("uncompressed_len = %u\n", uncompressed_len);
+       LZX_DEBUG("uncompressed_len = %u", uncompressed_len);
 
        if (uncompressed_len < 100)
                return 1;
@@ -665,8 +672,7 @@ int lzx_compress(const void *__uncompressed_data, uint uncompressed_len,
                                       &queue, freq_tabs.main_freq_table,
                                       &lzx_lz_params);
 
-       LZX_DEBUG("using %u matches\n", num_matches);
-
+       LZX_DEBUG("using %u matches", num_matches);
 
        lzx_make_huffman_codes(&freq_tabs, &codes);
 
@@ -675,7 +681,7 @@ int lzx_compress(const void *__uncompressed_data, uint uncompressed_len,
 
        /* The first three bits tell us what kind of block it is, and are one
         * of the LZX_BLOCKTYPE_* values.  */
-       bitstream_put_bits(&ostream, LZX_BLOCKTYPE_ALIGNED, 3);
+       bitstream_put_bits(&ostream, block_type, 3);
 
        /* The next bit indicates whether the block size is the default (32768),
         * indicated by a 1 bit, or whether the block size is given by the next
@@ -690,9 +696,10 @@ int lzx_compress(const void *__uncompressed_data, uint uncompressed_len,
        /* Write out the aligned offset tree. Note that M$ lies and says that
         * the aligned offset tree comes after the length tree, but that is
         * wrong; it actually is before the main tree.  */
-       for (i = 0; i < LZX_ALIGNEDTREE_NUM_SYMBOLS; i++)
-               bitstream_put_bits(&ostream, codes.aligned_lens[i], 
-                                  LZX_ALIGNEDTREE_ELEMENT_SIZE);
+       if (block_type == LZX_BLOCKTYPE_ALIGNED)
+               for (i = 0; i < LZX_ALIGNEDTREE_NUM_SYMBOLS; i++)
+                       bitstream_put_bits(&ostream, codes.aligned_lens[i],
+                                          LZX_ALIGNEDTREE_ELEMENT_SIZE);
 
        /* Write the pre-tree and lengths for the first LZX_NUM_CHARS symbols in the
         * main tree. */
@@ -716,7 +723,7 @@ int lzx_compress(const void *__uncompressed_data, uint uncompressed_len,
                return ret;
 
        /* Write the compressed literals. */
-       ret = lzx_write_compressed_literals(&ostream, LZX_BLOCKTYPE_ALIGNED,
+       ret = lzx_write_compressed_literals(&ostream, block_type,
                                            match_tab, num_matches, &codes);
        if (ret != 0)
                return ret;
@@ -727,31 +734,31 @@ int lzx_compress(const void *__uncompressed_data, uint uncompressed_len,
 
        compressed_len = ostream.bit_output - (u8*)compressed_data;
 
-       LZX_DEBUG("Compressed %u => %u bytes\n",
-                       uncompressed_len, compressed_len);
+       LZX_DEBUG("Compressed %u => %u bytes",
+                 uncompressed_len, compressed_len);
 
        *compressed_len_ret = compressed_len;
 
 #ifdef ENABLE_VERIFY_COMPRESSION
        /* Verify that we really get the same thing back when decompressing. */
+       LZX_DEBUG("Verifying the compressed data.");
        u8 buf[uncompressed_len];
        ret = lzx_decompress(compressed_data, compressed_len, buf, 
                             uncompressed_len);
        if (ret != 0) {
-               ERROR("ERROR: Failed to decompress data we compressed!\n");
-               exit(0);
+               ERROR("lzx_compress(): Failed to decompress data we compressed");
                abort();
        }
 
        for (i = 0; i < uncompressed_len; i++) {
                if (buf[i] != *((u8*)__uncompressed_data + i)) {
-                       ERROR("Data we compressed didn't decompress to "
-                               "the original data (difference at byte %u of "
-                               "%u)\n", i + 1, uncompressed_len);
+                       ERROR("lzx_compress(): Data we compressed didn't "
+                             "decompress to the original data (difference at "
+                             "byte %u of %u)", i + 1, uncompressed_len);
                        abort();
                }
        }
-       LZX_DEBUG("Compression verified to be correct.\n");
+       LZX_DEBUG("Compression verified to be correct.");
 #endif
 
        return 0;