]> wimlib.net Git - wimlib/blobdiff - src/lzx-compress.c
lzx_write_compressed_tree(), lzx_read_code_lens(): Specify signed char
[wimlib] / src / lzx-compress.c
index eb84d0cea108a69dfc16c1f0558d93e49b8724a2..61c484318179a383b2f5f1af4dc9bc58b10d67cd 100644 (file)
@@ -27,8 +27,8 @@
 
 
 /*
- * This file provides lzx_compress(), a function to compress an in-memory buffer
- * of data using LZX compression, as used in the WIM file format.
+ * This file provides wimlib_lzx_compress(), a function to compress an in-memory
+ * buffer of data using LZX compression, as used in the WIM file format.
  *
  * Please see the comments in lzx-decompress.c for more information about this
  * compression format.
  * blocks from one input chunk is not yet implemented.
  */
 
-#include "lzx.h"
-#include "compress.h"
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include "wimlib.h"
+#include "wimlib/compress.h"
+#include "wimlib/error.h"
+#include "wimlib/lzx.h"
+#include "wimlib/util.h"
+
 #include <stdlib.h>
 #include <string.h>
 
@@ -122,9 +130,9 @@ lzx_get_position_slot(unsigned formatted_offset)
 }
 
 static u32
-lzx_record_literal(u8 literal, void *__main_freq_tab)
+lzx_record_literal(u8 literal, void *_main_freq_tab)
 {
-       freq_t *main_freq_tab = __main_freq_tab;
+       freq_t *main_freq_tab = _main_freq_tab;
        main_freq_tab[literal]++;
        return literal;
 }
@@ -135,10 +143,10 @@ lzx_record_literal(u8 literal, void *__main_freq_tab)
  * intermediate representation documented below. */
 static u32
 lzx_record_match(unsigned match_offset, unsigned match_len,
-                void *__freq_tabs, void *__queue)
+                void *_freq_tabs, void *_queue)
 {
-       struct lzx_freq_tables *freq_tabs = __freq_tabs;
-       struct lru_queue *queue = __queue;
+       struct lzx_freq_tables *freq_tabs = _freq_tabs;
+       struct lru_queue *queue = _queue;
        unsigned position_slot;
        unsigned position_footer = 0;
        u32 match;
@@ -417,7 +425,7 @@ lzx_write_compressed_tree(struct output_bitstream *out,
        unsigned i;
        unsigned len_in_run;
        unsigned additional_bits;
-       char delta;
+       signed char delta;
        u8 pretree_sym;
 
        ZERO_ARRAY(pretree_freqs);
@@ -494,7 +502,7 @@ lzx_write_compressed_tree(struct output_bitstream *out,
                         * */
                        while (cur_run_len >= 4) {
                                additional_bits = (cur_run_len > 4);
-                               delta = -(char)len_in_run;
+                               delta = -(signed char)len_in_run;
                                if (delta < 0)
                                        delta += 17;
                                pretree_freqs[19]++;
@@ -510,7 +518,7 @@ lzx_write_compressed_tree(struct output_bitstream *out,
                 * as a difference from the length of that codeword in the
                 * previous tree. */
                while (cur_run_len--) {
-                       delta = -(char)len_in_run;
+                       delta = -(signed char)len_in_run;
                        if (delta < 0)
                                delta += 17;
 
@@ -566,7 +574,7 @@ lzx_write_compressed_tree(struct output_bitstream *out,
 
 /* Builds the canonical Huffman code for the main tree, the length tree, and the
  * aligned offset tree. */
-static void 
+static void
 lzx_make_huffman_codes(const struct lzx_freq_tables *freq_tabs,
                       struct lzx_codes *codes)
 {
@@ -590,10 +598,10 @@ lzx_make_huffman_codes(const struct lzx_freq_tables *freq_tabs,
 
 static void
 do_call_insn_translation(u32 *call_insn_target, int input_pos,
-                        int32_t file_size)
+                        s32 file_size)
 {
-       int32_t abs_offset;
-       int32_t rel_offset;
+       s32 abs_offset;
+       s32 rel_offset;
 
        rel_offset = le32_to_cpu(*call_insn_target);
        if (rel_offset >= -input_pos && rel_offset < file_size) {
@@ -638,18 +646,10 @@ static const struct lz_params lzx_lz_params = {
        .too_far        = 4096,
 };
 
-/*
- * Performs LZX compression on a block of data.
- *
- * Please see the documentation for the 'compress_func_t' type in write.c for
- * the exact behavior of this function and how to call it.
- */
-#ifdef EXPORT_COMPRESSION_FUNCTIONS
-WIMLIBAPI
-#endif
-unsigned
-lzx_compress(const void *__uncompressed_data, unsigned uncompressed_len,
-            void *compressed_data)
+/* Documented in wimlib.h */
+WIMLIBAPI unsigned
+wimlib_lzx_compress(const void *_uncompressed_data, unsigned uncompressed_len,
+                   void *compressed_data)
 {
        struct output_bitstream ostream;
        u8 uncompressed_data[uncompressed_len + 8];
@@ -675,7 +675,8 @@ lzx_compress(const void *__uncompressed_data, unsigned uncompressed_len,
 
        /* The input data must be preprocessed. To avoid changing the original
         * input, copy it to a temporary buffer. */
-       memcpy(uncompressed_data, __uncompressed_data, uncompressed_len);
+       memcpy(uncompressed_data, _uncompressed_data, uncompressed_len);
+       memset(uncompressed_data + uncompressed_len, 0, 8);
 
        /* Before doing any actual compression, do the call instruction (0xe8
         * byte) translation on the uncompressed data. */
@@ -753,21 +754,23 @@ lzx_compress(const void *__uncompressed_data, unsigned uncompressed_len,
 
 #ifdef ENABLE_VERIFY_COMPRESSION
        /* Verify that we really get the same thing back when decompressing. */
-       u8 buf[uncompressed_len];
-       ret = lzx_decompress(compressed_data, compressed_len, buf,
-                            uncompressed_len);
-       if (ret != 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("lzx_compress(): Data we compressed didn't "
-                             "decompress to the original data (difference at "
-                             "byte %u of %u)", i + 1, uncompressed_len);
+       {
+               u8 buf[uncompressed_len];
+               ret = wimlib_lzx_decompress(compressed_data, compressed_len,
+                                           buf, uncompressed_len);
+               if (ret != 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("lzx_compress(): Data we compressed didn't "
+                                     "decompress to the original data (difference at "
+                                     "byte %u of %u)", i + 1, uncompressed_len);
+                               abort();
+                       }
+               }
        }
 #endif
        return compressed_len;