-
-/* 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;
- u16 sym;
- u16 key_bits;
-
- if (table_bits > bitsleft) {
- key_size = bitsleft;
- bitsleft = 0;
- key_bits = bitstream_peek_bits(istream, key_size) <<
- (table_bits - key_size);
- } else {
- key_size = table_bits;
- bitsleft -= table_bits;
- key_bits = bitstream_peek_bits(istream, table_bits);
- }
-
- sym = decode_table[key_bits];
- if (sym >= num_syms) {
- bitstream_remove_bits(istream, key_size);
- do {
- if (bitsleft == 0)
- return -1;
- key_bits = sym + bitstream_peek_bits(istream, 1);
- bitstream_remove_bits(istream, 1);
- bitsleft--;
- } while ((sym = decode_table[key_bits]) >= num_syms);
- } else {
- bitstream_remove_bits(istream, lens[sym]);
- }
- *n = sym;
- return 0;
-}