X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fdecompress.c;h=f149b48c7bcf06f9172d48d9e7da0568b26f80fb;hb=b8df27e5fdd4dae472ababcec11d04eafea8830b;hp=e97aab5c5c2f72cbf407ccacc65f8a750fb62f01;hpb=908adfc82c98c531ca847f183489ccf57e190e16;p=wimlib diff --git a/src/decompress.c b/src/decompress.c index e97aab5c..f149b48c 100644 --- a/src/decompress.c +++ b/src/decompress.c @@ -5,7 +5,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. * @@ -23,15 +23,21 @@ * along with wimlib; if not, see http://www.gnu.org/licenses/. */ -#include "decompress.h" +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "wimlib/decompress.h" +#include "wimlib/util.h" + #include /* - * Builds a fast huffman decoding table from an array that gives the length of - * the codeword for each symbol in the alphabet. Originally based on code - * written by David Tritscher (taken the original LZX decompression code); also - * heavily modified to add some optimizations used in the zlib code, as well as - * more comments. + * make_huffman_decode_table: - Builds a fast huffman decoding table from an + * array that gives the length of the codeword for each symbol in the alphabet. + * Originally based on code written by David Tritscher (taken the original LZX + * decompression code); also heavily modified to add some optimizations used in + * the zlib code, as well as more comments. * * @decode_table: The array in which to create the fast huffman decoding * table. It must have a length of at least @@ -89,9 +95,10 @@ * indices into the decoding table, and symbol entries are distinguished from * pointers by the fact that values less than @num_syms must be symbol values. */ -int make_huffman_decode_table(u16 decode_table[], unsigned num_syms, - unsigned table_bits, const u8 lens[], - unsigned max_codeword_len) +int +make_huffman_decode_table(u16 * restrict decode_table, unsigned num_syms, + unsigned table_bits, const u8 * restrict lens, + unsigned max_codeword_len) { unsigned len_counts[max_codeword_len + 1]; u16 sorted_syms[num_syms]; @@ -164,30 +171,13 @@ int make_huffman_decode_table(u16 decode_table[], unsigned num_syms, break; unsigned num_entries = 1 << (table_bits - codeword_len); - const unsigned entries_per_long = sizeof(unsigned long) / - sizeof(decode_table[0]); - if (num_entries >= entries_per_long) { - wimlib_assert2(decode_table_pos % entries_per_long == 0); - BUILD_BUG_ON(sizeof(unsigned long) != 4 && - sizeof(unsigned long) != 8); - - unsigned long *p = (unsigned long *)&decode_table[decode_table_pos]; - unsigned n = num_entries / entries_per_long; - unsigned long v = sym; - if (sizeof(unsigned long) >= 4) - v |= v << 16; - if (sizeof(unsigned long) >= 8) - v |= v << 32; - do { - *p++ = v; - } while (--n); - - decode_table_pos += num_entries; - } else { - do { - decode_table[decode_table_pos++] = sym; - } while (--num_entries); - } + + /* Fill in the Huffman decode table entries one 16-bit + * integer at a time. */ + do { + decode_table[decode_table_pos++] = sym; + } while (--num_entries); + wimlib_assert2(decode_table_pos <= table_num_entries); if (++i == num_used_syms) { wimlib_assert2(decode_table_pos == table_num_entries); @@ -234,7 +224,6 @@ int make_huffman_decode_table(u16 decode_table[], unsigned num_syms, unsigned sym = sorted_syms[i]; unsigned codeword_len = lens[sym]; unsigned extra_bits = codeword_len - table_bits; - unsigned extra_mask; cur_codeword <<= (codeword_len - prev_codeword_len); prev_codeword_len = codeword_len; @@ -282,14 +271,15 @@ int make_huffman_decode_table(u16 decode_table[], unsigned num_syms, return 0; } -/* Reads a Huffman-encoded symbol when it is known there are less than - * MAX_CODE_LEN bits remaining in the bitstream. */ -int read_huffsym_near_end_of_input(struct input_bitstream *istream, - const u16 decode_table[], - const u8 lens[], - unsigned num_syms, - unsigned table_bits, - unsigned *n) +/* Reads a Huffman-encoded symbol from the bistream when the number of remaining + * bits is less than the maximum codeword length. */ +int +read_huffsym_near_end_of_input(struct input_bitstream *istream, + const u16 decode_table[], + const u8 lens[], + unsigned num_syms, + unsigned table_bits, + unsigned *n) { unsigned bitsleft = istream->bitsleft; unsigned key_size;