]> 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 2b3c0f903149f73965fadfb47dd3457d1d057b70..93ab84137be886c773757bdb86e86c22b2abcad4 100644 (file)
@@ -7,7 +7,7 @@
  */
 
 /*
- * Copyright (C) 2012 Eric Biggers
+ * Copyright (C) 2012, 2013 Eric Biggers
  *
  * This file is part of wimlib, a library for working with WIM files.
  *
  * succeed.
  */
 
-#include "util.h"
-#include "lzx.h"
-#include "decompress.h"
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include "wimlib.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);
 
 
 /*
  * Reads a Huffman-encoded symbol using the pre-tree.
  */
-static inline int read_huffsym_using_pretree(struct input_bitstream *istream,
-                                            const u16 pretree_decode_table[],
-                                            const u8 pretree_lens[], unsigned *n)
+static inline int
+read_huffsym_using_pretree(struct input_bitstream *istream,
+                          const u16 pretree_decode_table[],
+                          const u8 pretree_lens[], unsigned *n)
 {
        return read_huffsym(istream, pretree_decode_table, pretree_lens,
                            LZX_PRETREE_NUM_SYMBOLS, LZX_PRETREE_TABLEBITS, n,
@@ -143,9 +153,10 @@ static inline int read_huffsym_using_pretree(struct input_bitstream *istream,
 }
 
 /* Reads a Huffman-encoded symbol using the main tree. */
-static inline int read_huffsym_using_maintree(struct input_bitstream *istream,
-                                             const struct lzx_tables *tables,
-                                             unsigned *n)
+static inline int
+read_huffsym_using_maintree(struct input_bitstream *istream,
+                           const struct lzx_tables *tables,
+                           unsigned *n)
 {
        return read_huffsym(istream, tables->maintree_decode_table,
                            tables->maintree_lens, LZX_MAINTREE_NUM_SYMBOLS,
@@ -153,9 +164,10 @@ static inline int read_huffsym_using_maintree(struct input_bitstream *istream,
 }
 
 /* Reads a Huffman-encoded symbol using the length tree. */
-static inline int read_huffsym_using_lentree(struct input_bitstream *istream,
-                                            const struct lzx_tables *tables,
-                                            unsigned *n)
+static inline int
+read_huffsym_using_lentree(struct input_bitstream *istream,
+                          const struct lzx_tables *tables,
+                          unsigned *n)
 {
        return read_huffsym(istream, tables->lentree_decode_table,
                            tables->lentree_lens, LZX_LENTREE_NUM_SYMBOLS,
@@ -163,9 +175,10 @@ static inline int read_huffsym_using_lentree(struct input_bitstream *istream,
 }
 
 /* Reads a Huffman-encoded symbol using the aligned offset tree. */
-static inline int read_huffsym_using_alignedtree(struct input_bitstream *istream,
-                                                const struct lzx_tables *tables,
-                                                unsigned *n)
+static inline int
+read_huffsym_using_alignedtree(struct input_bitstream *istream,
+                              const struct lzx_tables *tables,
+                              unsigned *n)
 {
        return read_huffsym(istream, tables->alignedtree_decode_table,
                            tables->alignedtree_lens,
@@ -185,8 +198,9 @@ static inline int read_huffsym_using_alignedtree(struct input_bitstream *istream
  * @num_lens:  Number of length values to decode and return.
  *
  */
-static int lzx_read_code_lens(struct input_bitstream *istream, u8 lens[],
-                             unsigned num_lens)
+static int
+lzx_read_code_lens(struct input_bitstream *istream, u8 lens[],
+                  unsigned num_lens)
 {
        /* Declare the decoding table and length table for the pretree. */
        u16 pretree_decode_table[(1 << LZX_PRETREE_TABLEBITS) +
@@ -306,11 +320,12 @@ static int lzx_read_code_lens(struct input_bitstream *istream, u8 lens[],
  *                     R0, R1, and R2 will be written (only for uncompressed
  *                     blocks, which contain this information in the header)
  */
-static int lzx_read_block_header(struct input_bitstream *istream,
-                                unsigned *block_size_ret,
-                                unsigned *block_type_ret,
-                                struct lzx_tables *tables,
-                                struct lru_queue *queue)
+static int
+lzx_read_block_header(struct input_bitstream *istream,
+                     unsigned *block_size_ret,
+                     unsigned *block_type_ret,
+                     struct lzx_tables *tables,
+                     struct lru_queue *queue)
 {
        int ret;
        unsigned block_type;
@@ -318,10 +333,9 @@ static int lzx_read_block_header(struct input_bitstream *istream,
        unsigned s;
        unsigned i;
        unsigned len;
-       u32 R[3];
 
        ret = bitstream_ensure_bits(istream, 4);
-       if (ret != 0) {
+       if (ret) {
                ERROR("LZX input stream overrun");
                return ret;
        }
@@ -339,7 +353,7 @@ static int 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);
        }
@@ -353,7 +367,7 @@ static int 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;
                }
@@ -364,7 +378,7 @@ static int 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;
@@ -382,7 +396,7 @@ static int 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");
@@ -397,7 +411,7 @@ static int 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");
@@ -412,7 +426,7 @@ static int 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;
@@ -421,7 +435,7 @@ static int 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;
@@ -433,7 +447,7 @@ static int 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;
@@ -450,13 +464,19 @@ static int 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;
                }
@@ -512,12 +532,13 @@ static int lzx_read_block_header(struct input_bitstream *istream,
  *     - Match refers to data before the window.
  *     - The input bitstream ended unexpectedly.
  */
-static int lzx_decode_match(unsigned main_element, int block_type,
-                           unsigned bytes_remaining, u8 *window,
-                           unsigned window_pos,
-                           const struct lzx_tables *tables,
-                           struct lru_queue *queue,
-                           struct input_bitstream *istream)
+static int
+lzx_decode_match(unsigned main_element, int block_type,
+                unsigned bytes_remaining, u8 *window,
+                unsigned window_pos,
+                const struct lzx_tables *tables,
+                struct lru_queue *queue,
+                struct input_bitstream *istream)
 {
        unsigned length_header;
        unsigned position_slot;
@@ -669,11 +690,12 @@ static int lzx_decode_match(unsigned main_element, int block_type,
        return match_len;
 }
 
-static void undo_call_insn_translation(u32 *call_insn_target, int input_pos,
-                                      int32_t file_size)
+static void
+undo_call_insn_translation(u32 *call_insn_target, int input_pos,
+                          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) {
@@ -710,8 +732,8 @@ static void undo_call_insn_translation(u32 *call_insn_target, int input_pos,
  * Call instruction processing is supposed to take the file size as a parameter,
  * as it is used in calculating the translated jump targets.  But in WIM files,
  * this file size is always the same (LZX_WIM_MAGIC_FILESIZE == 12000000).*/
-static void undo_call_insn_preprocessing(u8 uncompressed_data[],
-                                        int uncompressed_data_len)
+static void
+undo_call_insn_preprocessing(u8 uncompressed_data[], int uncompressed_data_len)
 {
        for (int i = 0; i < uncompressed_data_len - 10; i++) {
                if (uncompressed_data[i] == 0xe8) {
@@ -738,12 +760,13 @@ static void undo_call_insn_preprocessing(u8 uncompressed_data[],
  * @queue:     The least-recently-used queue for match offsets.
  * @istream:   The input bitstream for the compressed literals.
  */
-static int lzx_decompress_block(int block_type, unsigned block_size,
-                               u8 *window,
-                               unsigned window_pos,
-                               const struct lzx_tables *tables,
-                               struct lru_queue *queue,
-                               struct input_bitstream *istream)
+static int
+lzx_decompress_block(int block_type, unsigned block_size,
+                    u8 *window,
+                    unsigned window_pos,
+                    const struct lzx_tables *tables,
+                    struct lru_queue *queue,
+                    struct input_bitstream *istream)
 {
        unsigned main_element;
        unsigned end;
@@ -754,7 +777,7 @@ static int 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) {
@@ -778,26 +801,10 @@ static int lzx_decompress_block(int block_type, unsigned block_size,
        return 0;
 }
 
-/*
- * Decompresses a block of LZX-compressed data as used in the WIM file format.
- *
- * Note that this will NOT work unmodified for LZX as used in the cabinet
- * format, which is not the same as in the WIM format!
- *
- * @compressed_data:   A pointer to the compressed data.
- *
- * @compressed_len:    The length of the compressed data, in bytes.
- *
- * @uncompressed_data: A pointer to the buffer into which to write the
- *                     uncompressed data.
- *
- * @uncompressed_len:  The length of the uncompressed data.  It must be
- *                     32768 bytes or less.
- *
- * Return 0 on success; non-zero on failure.
- */
-int lzx_decompress(const void *compressed_data, unsigned compressed_len,
-                  void *uncompressed_data, unsigned uncompressed_len)
+/* Documented in wimlib.h */
+WIMLIBAPI int
+wimlib_lzx_decompress(const void *compressed_data, unsigned compressed_len,
+                     void *uncompressed_data, unsigned uncompressed_len)
 {
        struct lzx_tables tables;
        struct input_bitstream istream;
@@ -837,7 +844,7 @@ int 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",
@@ -864,7 +871,7 @@ int 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;