7 void reliable_read(raw_input_func_t* input_func, void* userdata, uint8_t* buf, size_t len)
10 ssize_t bytes_read = input_func(userdata, buf, len);
11 assert(bytes_read <= len);
13 // TODO: We need better error handling here. setjmp()/longjmp()
14 // should hopefully do the trick, but we need to take care for
16 if (bytes_read == (ssize_t)-1) {
17 fprintf(stderr, "Input function returned error\n");
20 if (bytes_read == 0) {
21 fprintf(stderr, "Premature EOF\n");
30 uint16_t read_length(raw_input_func_t* input_func, void* userdata)
33 reliable_read(input_func, userdata, buf, 2);
34 return (buf[0] << 8) | buf[1];
37 void read_huffman_tables(huffman_tables_t* dst, raw_input_func_t* input_func, void* userdata)
39 size_t len = read_length(input_func, userdata);
43 // TODO: check for NULL return
44 uint8_t* buf = (uint8_t*)malloc(len);
46 reliable_read(input_func, userdata, buf, len);
49 unsigned char tc_th = *inptr++;
51 unsigned table_class = tc_th >> 4;
52 unsigned table_dest = tc_th & 0x0f;
54 if (table_class != 0 && table_class != 1) {
55 fprintf(stderr, "Error: Unknown table class %u\n", table_class);
58 if (table_dest >= 4) {
59 fprintf(stderr, "Error: Unknown table destination %u\n", table_dest);
63 struct huffman_table* tbl = dst[table_class][table_dest];
65 fprintf(stderr, "Short read for num_codes\n");
68 for (unsigned i = 0; i < 16; ++i) {
69 tbl->num_codes[i] = *inptr++;
72 for (unsigned i = 0, k = 0; i < 16; ++i) {
73 if (len < tbl->num_codes[i]) {
74 fprintf(stderr, "Short read for codes\n");
77 for (unsigned j = 0; j < tbl->num_codes[i]; ++j, ++k) {
78 tbl->codes[k] = *inptr++;
83 // Generate derived tables
85 // generate_size_table (figure C.1)
88 for (unsigned i = 0; i < 16; ++i) {
89 for (unsigned j = 0; j < tbl->num_codes[i]; ++j, ++k) {
90 tbl->huffsize[k] = i + 1;
96 // generate_code_table (figure C.2)
97 unsigned si = tbl->huffsize[0];
98 for (unsigned i = 0, j = 0;; ) {
99 tbl->huffcode[i++] = j++;
100 if (tbl->huffsize[i] == si) {
103 if (tbl->huffsize[i] == 0) {
109 } while (++si != tbl->huffsize[i]);
112 // decoder_tables (figure F.15)
113 for (unsigned i = 0, j = 0; i < 16; ++i) {
114 if (tbl->num_codes[i] == 0) {
115 tbl->maxcode[i] = -1;
120 tbl->mincode[i] = tbl->huffcode[j];
121 j += tbl->num_codes[i];
122 tbl->maxcode[i] = tbl->huffcode[j - 1];
125 // Generate the lookup tables
126 for (unsigned i = 0; i < DEHUF_TABLE_SIZE; ++i) {
127 tbl->lookup_table_codes[i] = DEHUF_SLOW_PATH;
128 tbl->lookup_table_length[i] = DEHUF_SLOW_PATH;
132 for (unsigned i = 0; i < 16; ++i) {
133 for (unsigned j = 0; j < tbl->num_codes[i]; ++j, ++k) {
134 const unsigned code = tbl->huffcode[k];
135 const unsigned length = tbl->huffsize[k];
136 if (length > DEHUF_TABLE_BITS) {
139 const unsigned prefix_min = code << (DEHUF_TABLE_BITS - length);
140 const unsigned prefix_max = ((code + 1) << (DEHUF_TABLE_BITS - length));
142 for (unsigned elem = prefix_min; elem < prefix_max; ++elem) {
143 assert(tbl->lookup_table_codes[elem] == DEHUF_SLOW_PATH);
144 assert(tbl->lookup_table_length[elem] == DEHUF_SLOW_PATH);
145 tbl->lookup_table_codes[elem] = k;
146 tbl->lookup_table_length[elem] = length;
155 unsigned read_huffman_symbol_slow_path(const struct huffman_table* table,
156 struct bit_source* source) {
157 possibly_refill(source, 1);
158 unsigned code = read_bits(source, 1);
161 while (code > table->maxcode[i] || table->maxcode[i] == -1) {
162 possibly_refill(source, 1);
163 code = (code << 1) | read_bits(source, 1);
167 fprintf(stderr, "Error in Huffman decoding: Too long code (%x)\n", code);
172 return table->codes[table->valptr[i] + code - table->mincode[i]];