]> wimlib.net Git - wimlib/blobdiff - src/lzx-decompress.c
make_huffman_decode_table(): Add SSE2 entry filling (with aliasing handled correctly)
[wimlib] / src / lzx-decompress.c
index 18fa3d008b6b137bc77b8235839c8a93679a4871..93ab84137be886c773757bdb86e86c22b2abcad4 100644 (file)
  * succeed.
  */
 
-#include "util.h"
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include "wimlib.h"
-#include "lzx.h"
-#include "decompress.h"
+#include "wimlib/decompress.h"
+#include "wimlib/lzx.h"
+#include "wimlib/util.h"
+
 #include <string.h>
 
 /* Huffman decoding tables and maps from symbols to code lengths. */
 struct lzx_tables {
 
        u16 maintree_decode_table[(1 << LZX_MAINTREE_TABLEBITS) +
-                                       (LZX_MAINTREE_NUM_SYMBOLS * 2)];
+                                       (LZX_MAINTREE_NUM_SYMBOLS * 2)]
+                                       _aligned_attribute(DECODE_TABLE_ALIGNMENT);
        u8 maintree_lens[LZX_MAINTREE_NUM_SYMBOLS];
 
 
        u16 lentree_decode_table[(1 << LZX_LENTREE_TABLEBITS) +
-                                       (LZX_LENTREE_NUM_SYMBOLS * 2)];
+                                       (LZX_LENTREE_NUM_SYMBOLS * 2)]
+                                       _aligned_attribute(DECODE_TABLE_ALIGNMENT);
        u8 lentree_lens[LZX_LENTREE_NUM_SYMBOLS];
 
 
        u16 alignedtree_decode_table[(1 << LZX_ALIGNEDTREE_TABLEBITS) +
-                                       (LZX_ALIGNEDTREE_NUM_SYMBOLS * 2)];
+                                       (LZX_ALIGNEDTREE_NUM_SYMBOLS * 2)]
+                                       _aligned_attribute(DECODE_TABLE_ALIGNMENT);
        u8 alignedtree_lens[LZX_ALIGNEDTREE_NUM_SYMBOLS];
-};
+} _aligned_attribute(DECODE_TABLE_ALIGNMENT);
 
 
 /*
@@ -327,7 +335,7 @@ lzx_read_block_header(struct input_bitstream *istream,
        unsigned len;
 
        ret = bitstream_ensure_bits(istream, 4);
-       if (ret != 0) {
+       if (ret) {
                ERROR("LZX input stream overrun");
                return ret;
        }
@@ -345,7 +353,7 @@ lzx_read_block_header(struct input_bitstream *istream,
                block_size = 32768;
        } else {
                ret = bitstream_read_bits(istream, 16, &block_size);
-               if (ret != 0)
+               if (ret)
                        return ret;
                block_size = le16_to_cpu(block_size);
        }
@@ -359,7 +367,7 @@ lzx_read_block_header(struct input_bitstream *istream,
                        ret = bitstream_read_bits(istream,
                                                  LZX_ALIGNEDTREE_ELEMENT_SIZE,
                                                  &len);
-                       if (ret != 0)
+                       if (ret)
                                return ret;
                        tables->alignedtree_lens[i] = len;
                }
@@ -370,7 +378,7 @@ lzx_read_block_header(struct input_bitstream *istream,
                                                LZX_ALIGNEDTREE_TABLEBITS,
                                                tables->alignedtree_lens,
                                                8);
-               if (ret != 0) {
+               if (ret) {
                        ERROR("lzx_decompress(): Failed to make the decode "
                              "table for the aligned offset tree");
                        return ret;
@@ -388,7 +396,7 @@ lzx_read_block_header(struct input_bitstream *istream,
                 * tree. */
                ret = lzx_read_code_lens(istream, tables->maintree_lens,
                                         LZX_NUM_CHARS);
-               if (ret != 0) {
+               if (ret) {
                        ERROR("lzx_decompress(): Failed to read the code "
                              "lengths for the first 256 elements of the "
                              "main tree");
@@ -403,7 +411,7 @@ lzx_read_block_header(struct input_bitstream *istream,
                ret = lzx_read_code_lens(istream,
                                         tables->maintree_lens + LZX_NUM_CHARS,
                                         LZX_MAINTREE_NUM_SYMBOLS - LZX_NUM_CHARS);
-               if (ret != 0) {
+               if (ret) {
                        ERROR("lzx_decompress(): Failed to read the path "
                              "lengths for the remaining elements of the main "
                              "tree");
@@ -418,7 +426,7 @@ lzx_read_block_header(struct input_bitstream *istream,
                                                LZX_MAINTREE_TABLEBITS,
                                                tables->maintree_lens,
                                                LZX_MAX_CODEWORD_LEN);
-               if (ret != 0) {
+               if (ret) {
                        ERROR("lzx_decompress(): Failed to make the decode "
                              "table for the main tree");
                        return ret;
@@ -427,7 +435,7 @@ lzx_read_block_header(struct input_bitstream *istream,
                LZX_DEBUG("Reading path lengths for the length tree.");
                ret = lzx_read_code_lens(istream, tables->lentree_lens,
                                         LZX_LENTREE_NUM_SYMBOLS);
-               if (ret != 0) {
+               if (ret) {
                        ERROR("lzx_decompress(): Failed to read the path "
                              "lengths for the length tree");
                        return ret;
@@ -439,7 +447,7 @@ lzx_read_block_header(struct input_bitstream *istream,
                                                LZX_LENTREE_TABLEBITS,
                                                tables->lentree_lens,
                                                LZX_MAX_CODEWORD_LEN);
-               if (ret != 0) {
+               if (ret) {
                        ERROR("lzx_decompress(): Failed to build the length "
                              "Huffman tree");
                        return ret;
@@ -456,13 +464,19 @@ lzx_read_block_header(struct input_bitstream *istream,
                 * *already* aligned, the correct thing to do is to throw away
                 * the next 16 bits. */
                if (istream->bitsleft == 0) {
-                       if (istream->data_bytes_left < 14)
+                       if (istream->data_bytes_left < 14) {
+                               ERROR("lzx_decompress(): Insufficient length in "
+                                     "uncompressed block");
                                return -1;
+                       }
                        istream->data += 2;
                        istream->data_bytes_left -= 2;
                } else {
-                       if (istream->data_bytes_left < 12)
+                       if (istream->data_bytes_left < 12) {
+                               ERROR("lzx_decompress(): Insufficient length in "
+                                     "uncompressed block");
                                return -1;
+                       }
                        istream->bitsleft = 0;
                        istream->bitbuf = 0;
                }
@@ -678,10 +692,10 @@ lzx_decode_match(unsigned main_element, int block_type,
 
 static void
 undo_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;
 
        abs_offset = le32_to_cpu(*call_insn_target);
        if (abs_offset >= -input_pos && abs_offset < file_size) {
@@ -763,7 +777,7 @@ lzx_decompress_block(int block_type, unsigned block_size,
        while (window_pos < end) {
                ret = read_huffsym_using_maintree(istream, tables,
                                                  &main_element);
-               if (ret != 0)
+               if (ret)
                        return ret;
 
                if (main_element < LZX_NUM_CHARS) {
@@ -830,7 +844,7 @@ wimlib_lzx_decompress(const void *compressed_data, unsigned compressed_len,
                LZX_DEBUG("Reading block header.");
                ret = lzx_read_block_header(&istream, &block_size,
                                            &block_type, &tables, &queue);
-               if (ret != 0)
+               if (ret)
                        return ret;
 
                LZX_DEBUG("block_size = %u, window_pos = %u",
@@ -857,7 +871,7 @@ wimlib_lzx_decompress(const void *compressed_data, unsigned compressed_len,
                                                   &tables,
                                                   &queue,
                                                   &istream);
-                       if (ret != 0)
+                       if (ret)
                                return ret;
                        if (tables.maintree_lens[0xe8] != 0)
                                e8_preprocessing_done = true;