X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Flzx-decompress.c;h=96332dd8f40cf6066ed43d19c482d38c7c6e5408;hb=926ff6c5ed956b96fcb521b2c20afcf9ac890b14;hp=18fa3d008b6b137bc77b8235839c8a93679a4871;hpb=92d96f9e2db42196a778b727cfa91d18a5cc6f49;p=wimlib diff --git a/src/lzx-decompress.c b/src/lzx-decompress.c index 18fa3d00..96332dd8 100644 --- a/src/lzx-decompress.c +++ b/src/lzx-decompress.c @@ -106,29 +106,37 @@ * 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 /* 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); /* @@ -196,7 +204,8 @@ lzx_read_code_lens(struct input_bitstream *istream, u8 lens[], { /* Declare the decoding table and length table for the pretree. */ u16 pretree_decode_table[(1 << LZX_PRETREE_TABLEBITS) + - (LZX_PRETREE_NUM_SYMBOLS * 2)]; + (LZX_PRETREE_NUM_SYMBOLS * 2)] + _aligned_attribute(DECODE_TABLE_ALIGNMENT); u8 pretree_lens[LZX_PRETREE_NUM_SYMBOLS]; unsigned i; unsigned len; @@ -236,7 +245,7 @@ lzx_read_code_lens(struct input_bitstream *istream, u8 lens[], unsigned num_zeroes; unsigned code; unsigned num_same; - char value; + signed char value; ret = read_huffsym_using_pretree(istream, pretree_decode_table, pretree_lens, &tree_code); @@ -276,7 +285,7 @@ lzx_read_code_lens(struct input_bitstream *istream, u8 lens[], &code); if (ret != 0) return ret; - value = (char)*lens - (char)code; + value = (signed char)*lens - (signed char)code; if (value < 0) value += 17; while (num_same--) { @@ -286,7 +295,7 @@ lzx_read_code_lens(struct input_bitstream *istream, u8 lens[], } break; default: /* Difference from old length. */ - value = (char)*lens - (char)tree_code; + value = (signed char)*lens - (signed char)tree_code; if (value < 0) value += 17; *lens = value; @@ -327,8 +336,8 @@ lzx_read_block_header(struct input_bitstream *istream, unsigned len; ret = bitstream_ensure_bits(istream, 4); - if (ret != 0) { - ERROR("LZX input stream overrun"); + if (ret) { + DEBUG("LZX input stream overrun"); return ret; } @@ -345,7 +354,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 +368,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,8 +379,8 @@ lzx_read_block_header(struct input_bitstream *istream, LZX_ALIGNEDTREE_TABLEBITS, tables->alignedtree_lens, 8); - if (ret != 0) { - ERROR("lzx_decompress(): Failed to make the decode " + if (ret) { + DEBUG("lzx_decompress(): Failed to make the decode " "table for the aligned offset tree"); return ret; } @@ -388,8 +397,8 @@ lzx_read_block_header(struct input_bitstream *istream, * tree. */ ret = lzx_read_code_lens(istream, tables->maintree_lens, LZX_NUM_CHARS); - if (ret != 0) { - ERROR("lzx_decompress(): Failed to read the code " + if (ret) { + DEBUG("lzx_decompress(): Failed to read the code " "lengths for the first 256 elements of the " "main tree"); return ret; @@ -403,8 +412,8 @@ 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) { - ERROR("lzx_decompress(): Failed to read the path " + if (ret) { + DEBUG("lzx_decompress(): Failed to read the path " "lengths for the remaining elements of the main " "tree"); return ret; @@ -418,8 +427,8 @@ lzx_read_block_header(struct input_bitstream *istream, LZX_MAINTREE_TABLEBITS, tables->maintree_lens, LZX_MAX_CODEWORD_LEN); - if (ret != 0) { - ERROR("lzx_decompress(): Failed to make the decode " + if (ret) { + DEBUG("lzx_decompress(): Failed to make the decode " "table for the main tree"); return ret; } @@ -427,8 +436,8 @@ 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) { - ERROR("lzx_decompress(): Failed to read the path " + if (ret) { + DEBUG("lzx_decompress(): Failed to read the path " "lengths for the length tree"); return ret; } @@ -439,8 +448,8 @@ lzx_read_block_header(struct input_bitstream *istream, LZX_LENTREE_TABLEBITS, tables->lentree_lens, LZX_MAX_CODEWORD_LEN); - if (ret != 0) { - ERROR("lzx_decompress(): Failed to build the length " + if (ret) { + DEBUG("lzx_decompress(): Failed to build the length " "Huffman tree"); return ret; } @@ -456,13 +465,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) { + DEBUG("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) { + DEBUG("lzx_decompress(): Insufficient length in " + "uncompressed block"); return -1; + } istream->bitsleft = 0; istream->bitbuf = 0; } @@ -475,7 +490,7 @@ lzx_read_block_header(struct input_bitstream *istream, * be read in lzx_decompress(). */ break; default: - ERROR("lzx_decompress(): Found invalid block"); + DEBUG("lzx_decompress(): Found invalid block"); return -1; } *block_type_ret = block_type; @@ -641,22 +656,23 @@ lzx_decode_match(unsigned main_element, int block_type, /* Verify that the match is in the bounds of the part of the window * currently in use, then copy the source of the match to the current * position. */ - match_dest = window + window_pos; - match_src = match_dest - match_offset; if (match_len > bytes_remaining) { - ERROR("lzx_decode_match(): Match of length %u bytes overflows " + DEBUG("lzx_decode_match(): Match of length %u bytes overflows " "uncompressed block size", match_len); return -1; } - if (match_src < window) { - ERROR("lzx_decode_match(): Match of length %u bytes references " + if (match_offset > window_pos) { + DEBUG("lzx_decode_match(): Match of length %u bytes references " "data before window (match_offset = %u, window_pos = %u)", match_len, match_offset, window_pos); return -1; } + match_dest = window + window_pos; + match_src = match_dest - match_offset; + #if 0 printf("Match: src %u, dst %u, len %u\n", match_src - window, match_dest - window, @@ -678,10 +694,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 +779,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) { @@ -787,7 +803,7 @@ lzx_decompress_block(int block_type, unsigned block_size, return 0; } -/* Documented in wimlib.h */ +/* API function documented in wimlib.h */ WIMLIBAPI int wimlib_lzx_decompress(const void *compressed_data, unsigned compressed_len, void *uncompressed_data, unsigned uncompressed_len) @@ -830,14 +846,14 @@ 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", block_size, window_pos); if (block_size > uncompressed_len - window_pos) { - ERROR("lzx_decompress(): Expected a block size of at " + DEBUG("lzx_decompress(): Expected a block size of at " "most %u bytes (found %u bytes)", uncompressed_len - window_pos, block_size); return -1; @@ -857,7 +873,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; @@ -865,7 +881,7 @@ wimlib_lzx_decompress(const void *compressed_data, unsigned compressed_len, case LZX_BLOCKTYPE_UNCOMPRESSED: LZX_DEBUG("LZX_BLOCKTYPE_UNCOMPRESSED"); if (istream.data_bytes_left < block_size) { - ERROR("Unexpected end of input when " + DEBUG("Unexpected end of input when " "reading %u bytes from LZX bitstream " "(only have %u bytes left)", block_size, istream.data_bytes_left);