- u8 *window_ptr = &window[window_pos];
- u8 *window_end = window_ptr + block_size;
- unsigned mainsym;
- u32 match_len;
- unsigned offset_slot;
- u32 match_offset;
- unsigned num_extra_bits;
- unsigned ones_if_aligned = 0U - (block_type == LZX_BLOCKTYPE_ALIGNED);
-
- while (window_ptr != window_end) {
-
- mainsym = read_huffsym_using_maincode(istream, tables);
+ u8 * const block_end = out_next + block_size;
+ unsigned min_aligned_offset_slot;
+
+ /*
+ * Build the Huffman decode tables. We always need to build the main
+ * and length decode tables. For aligned blocks we additionally need to
+ * build the aligned offset decode table.
+ */
+
+ if (make_huffman_decode_table(d->maincode_decode_table,
+ d->num_main_syms,
+ LZX_MAINCODE_TABLEBITS,
+ d->maincode_lens,
+ LZX_MAX_MAIN_CODEWORD_LEN,
+ d->maincode_working_space))
+ return -1;
+
+ if (make_huffman_decode_table(d->lencode_decode_table,
+ LZX_LENCODE_NUM_SYMBOLS,
+ LZX_LENCODE_TABLEBITS,
+ d->lencode_lens,
+ LZX_MAX_LEN_CODEWORD_LEN,
+ d->lencode_working_space))
+ return -1;
+
+ if (block_type == LZX_BLOCKTYPE_ALIGNED) {
+ if (make_huffman_decode_table(d->alignedcode_decode_table,
+ LZX_ALIGNEDCODE_NUM_SYMBOLS,
+ LZX_ALIGNEDCODE_TABLEBITS,
+ d->alignedcode_lens,
+ LZX_MAX_ALIGNED_CODEWORD_LEN,
+ d->alignedcode_working_space))
+ return -1;
+ min_aligned_offset_slot = LZX_MIN_ALIGNED_OFFSET_SLOT;
+ memcpy(d->extra_offset_bits, d->extra_offset_bits_minus_aligned,
+ sizeof(lzx_extra_offset_bits));
+ } else {
+ min_aligned_offset_slot = LZX_MAX_OFFSET_SLOTS;
+ memcpy(d->extra_offset_bits, lzx_extra_offset_bits,
+ sizeof(lzx_extra_offset_bits));
+ }
+
+ /* Decode the literals and matches. */
+
+ do {
+ unsigned mainsym;
+ unsigned length;
+ u32 offset;
+ unsigned offset_slot;
+
+ mainsym = read_mainsym(d, is);