-/* Decode the matches and literal bytes in a region of XPRESS-encoded data. */
-static int
-xpress_decode_window(struct input_bitstream *istream, const u16 *decode_table,
- u8 *window, unsigned window_size)
-{
- u8 *window_ptr = window;
- u8 *window_end = &window[window_size];
- unsigned sym;
- unsigned match_len;
- unsigned offset_high_bit;
- unsigned match_offset;
-
- while (window_ptr != window_end) {
-
- sym = read_huffsym(istream, decode_table,
- XPRESS_TABLEBITS, XPRESS_MAX_CODEWORD_LEN);
- if (sym < XPRESS_NUM_CHARS) {
- /* Literal */
- *window_ptr++ = sym;
- continue;
- }
-
- /* Match */
- match_len = sym & 0xf;
- offset_high_bit = (sym >> 4) & 0xf;
-
- bitstream_ensure_bits(istream, 16);
-
- match_offset = (1 << offset_high_bit) |
- bitstream_pop_bits(istream, offset_high_bit);
-
- if (match_len == 0xf) {
- match_len += bitstream_read_byte(istream);
- if (match_len == 0xf + 0xff)
- match_len = bitstream_read_u16(istream);
- }
- match_len += XPRESS_MIN_MATCH_LEN;
-
- if (unlikely(match_offset > window_ptr - window))
- return -1;
-
- if (unlikely(match_len > window_end - window_ptr))
- return -1;
-
- lz_copy(window_ptr, match_len, match_offset, window_end,
- XPRESS_MIN_MATCH_LEN);
-
- window_ptr += match_len;
- }
- return 0;
-}
-