-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuff_dec.c
197 lines (156 loc) · 4.54 KB
/
huff_dec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* @file huff_dec.c
* @author Fabjan Sukalia <fsukalia@gmail.com>
* @date 2016-02-03
*/
#include <stdlib.h>
#include <assert.h>
#include "bit_reader.h"
#include "huff_dec.h"
bool huff_gen_dec(uint8_t code_len[restrict 16], uint8_t symbols[restrict],
struct huff_dec * restrict decoder)
{
assert(code_len != NULL);
assert(symbols != NULL);
assert(decoder != NULL);
uint16_t num_sym = 0;
uint8_t max_bits = 0;
uint8_t min_bits = 0;
for (int i = 0; i < 16; i++) {
num_sym += code_len[i];
max_bits = (code_len[i] != 0) ? i + 1 : max_bits;
min_bits = (min_bits == 0) ? i + 1 : min_bits;
}
if (num_sym > 256 && num_sym == 0) {
fprintf(stderr, "Invalid number of symbols\n");
return false;
}
assert(min_bits <= max_bits);
assert(0 < max_bits && max_bits <= 16);
assert(0 < min_bits && min_bits <= 16);
uint32_t num_entries = 1 << max_bits;
assert(num_entries != 0);
decoder->max_bits = max_bits;
decoder->min_bits = min_bits;
decoder->entries = malloc(sizeof(uint16_t) * num_entries);
if(decoder->entries == NULL) {
perror("Couldn't allocate memory for decode table\n");
return false;
}
uint8_t sym_index = 0;
size_t index = 0;
uint32_t times = 1 << max_bits;
for (int i = 0; i < 16; i++) {
times >>= 1;
for (int j = 0; j < code_len[i]; j++) {
uint8_t symbol = symbols[sym_index];
sym_index++;
for (uint16_t t = 0; t < times; t++) {
decoder->entries[index] = (symbol << 8) | (i + 1);
index++;
assert(index <= num_entries);
}
}
}
/* if the kraft sum is less than 1 then index is less than num_entries
* this isn't really an error, there are just some checks in the decoding
* missing. */
if (index != num_entries) {
fprintf(stderr, "Invalid decode header. Missing entries in decode table\n");
free(decoder->entries);
return false;
}
return true;
}
bool huff_decode(const struct huff_dec * restrict decoder, size_t num_sym,
struct bit_reader * restrict reader,
uint8_t out_buf[restrict])
{
assert(decoder != NULL);
assert(reader != NULL);
assert(out_buf != NULL);
if (num_sym == 0)
return true;
/* FIXME this only works when decoding starts at byte boundary. Add functions
to push back bits in bit_reader and extend decoding to decode at any bit
position. */
uint16_t code;
uint8_t max_bits = decoder->max_bits;
const uint16_t mask = (1 << max_bits) - 1;
uint16_t *table = decoder->entries;
/* FIXME special case with less than max_bits in the data stream. */
if (!bit_reader_next_bits(reader, &code, max_bits)) {
fprintf(stderr, "Error while reading input\n");
return false;
}
for (size_t i = 0; i < num_sym; i++) {
code &= mask;
uint16_t entry = table[code];
uint8_t symbol = (entry >> 8) & 0xFF;
uint8_t length = entry & 0xFF;
assert(length != 0);
assert(length <= max_bits);
out_buf[i] = symbol;
if (i == num_sym - 1)
break;
uint16_t tmp = 0;
if (!bit_reader_next_bits(reader, &tmp, length)) {
fprintf(stderr, "Error while reading input\n");
return false;
}
code = (code << length) | tmp;
}
return true;
}
bool huff_decode_file(const struct huff_dec * restrict decoder, size_t num_sym,
struct bit_reader * restrict reader, FILE *out)
{
assert(decoder != NULL);
assert(reader != NULL);
assert(out != NULL);
if (num_sym == 0)
return true;
/* FIXME this only works when decoding starts at byte boundary. Add functions
to push back bits in bit_reader and extend decoding to decode at any bit
position. */
uint32_t code;
uint8_t max_bits = decoder->max_bits;
const uint16_t mask = (1 << max_bits) - 1;
uint16_t *table = decoder->entries;
/* FIXME special case with less than max_bits in the data stream. */
/* FIXME handle EOF correctly */
uint16_t code16;
bit_reader_next_bits(reader, &code16, max_bits);
code = code16;
/*
fprintf(stderr, "Error while reading input\n");
return false;
}*/
for (size_t i = 0; i < num_sym; i++) {
code &= mask;
uint16_t entry = table[code];
uint8_t symbol = (entry >> 8) & 0xFF;
uint8_t length = entry & 0xFF;
assert(length != 0);
assert(length <= max_bits);
if (fwrite(&symbol, 1, 1, out) != 1) {
fprintf(stderr, "Error while writing output symbols\n");
return false;
}
if (i == num_sym - 1)
break;
uint16_t tmp = 0;
bit_reader_next_bits(reader, &tmp, length);/*
fprintf(stderr, "Error while reading input\n");
return false;
}*/
/* FIXME this can overflow when code is uint16_t */
code = (code << length) | tmp;
}
return true;
}
void huff_destroy(struct huff_dec *dec)
{
assert(dec != NULL);
free(dec->entries);
}