-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffman.c
330 lines (254 loc) · 7.49 KB
/
huffman.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* huffman.c
*
* copyright (c) 2019 Xiongfei Shi
*
* author: Xiongfei Shi <jenson.shixf(a)gmail.com>
* license: Apache2.0
*
* https://github.com/shixiongfei/huffman
*/
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "huffman.h"
#ifndef max
#define max(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef min
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
#define HUFFMAN_POOLSIZE 512
typedef struct huffnode_s huffnode_t;
typedef struct huffcode_s huffcode_t;
typedef struct huffcontext_s huffcontext_t;
struct huffnode_s {
int weight;
unsigned char symbol;
huffnode_t *left, *right;
};
struct huffcode_s {
unsigned short bitlen;
unsigned short code;
};
struct huffcontext_s {
huffnode_t pool[HUFFMAN_POOLSIZE];
huffcode_t codes[HUFFMAN_TABLESIZE];
huffnode_t *queue[HUFFMAN_TABLESIZE];
huffnode_t **pq; /* priority queue */
int nodes, qend;
int maxbits, minbits;
};
struct huffman_s {
huffcontext_t context;
unsigned short table[HUFFMAN_TABLESIZE];
};
static struct huffalloc_s {
void * (*alloc)(size_t);
void (*release)(void *);
} huffalloc = { malloc, free };
void huffman_setalloc(void * (*alloc)(size_t), void(*release)(void *)) {
huffalloc.alloc = alloc ? alloc : malloc;
huffalloc.release = release ? release : free;
}
static huffnode_t *huffnode_new(huffman_t *huffm, int weight,
unsigned char symbol, huffnode_t *l,
huffnode_t *r) {
huffnode_t *n = huffm->context.pool + huffm->context.nodes++;
if (weight) {
n->symbol = symbol;
n->weight = weight;
n->left = NULL;
n->right = NULL;
} else {
n->left = l;
n->right = r;
n->weight = l->weight + r->weight;
n->symbol = 0;
}
return n;
}
static void huffnode_push(huffman_t *huffm, huffnode_t *node) {
int j, i = huffm->context.qend++;
while ((j = i / 2) && huffm->context.pq[j]->weight > node->weight) {
huffm->context.pq[i] = huffm->context.pq[j];
i = j;
}
huffm->context.pq[i] = node;
}
static huffnode_t *huffnode_pop(huffman_t *huffm) {
int i, l;
huffnode_t *n = huffm->context.pq[i = 1];
if (huffm->context.qend < 2)
return NULL;
huffm->context.qend -= 1;
while ((l = i * 2) < huffm->context.qend) {
if ((l + 1) < huffm->context.qend &&
huffm->context.pq[l + 1]->weight < huffm->context.pq[l]->weight)
l += 1;
if (huffm->context.pq[huffm->context.qend]->weight <=
huffm->context.pq[l]->weight)
break;
huffm->context.pq[i] = huffm->context.pq[l];
i = l;
}
huffm->context.pq[i] = huffm->context.pq[huffm->context.qend];
return n;
}
static void bitstream_writebit(unsigned char **bs, int bit, int *bit_pos) {
int bit_offset, byte_offset;
unsigned char *p, stamp;
bit_offset = *bit_pos & 7;
byte_offset = (*bit_pos - bit_offset) / 8;
p = *bs + byte_offset;
stamp = (0 != bit);
stamp <<= 8 - 1;
stamp >>= bit_offset;
*p &= ~(0x80 >> bit_offset); /* clear bit */
*p |= stamp;
*bit_pos += 1;
if (*bit_pos >= 8) {
*bit_pos -= 8;
*bs += 1;
}
}
static int bitstream_readbit(unsigned char **bs, int *bit_pos) {
int bit_offset, byte_offset;
unsigned char *p, val;
bit_offset = *bit_pos & 7;
byte_offset = (*bit_pos - bit_offset) / 8;
p = *bs + byte_offset;
val = *p;
val <<= bit_offset;
val >>= 8 - 1;
*bit_pos += 1;
if (*bit_pos >= 8) {
*bit_pos -= 8;
*bs += 1;
}
return val == 1;
}
#define putbit(c, b, l) \
do { \
unsigned char *bs = (unsigned char *)&(c); \
int bs_off = (l); \
bitstream_writebit(&bs, (b), &bs_off); \
} while (0)
static void huffnode_build_codes(huffman_t *huffm, huffnode_t *node,
unsigned short code, unsigned short bitlen) {
if (node->symbol || (!node->left && !node->right)) {
huffm->context.codes[node->symbol].bitlen = bitlen;
huffm->context.codes[node->symbol].code = code;
huffm->context.maxbits = max(huffm->context.maxbits, bitlen);
huffm->context.minbits = min(huffm->context.minbits, bitlen);
return;
}
putbit(code, 0, bitlen);
huffnode_build_codes(huffm, node->left, code, bitlen + 1);
putbit(code, 1, bitlen);
huffnode_build_codes(huffm, node->right, code, bitlen + 1);
}
static int huffman_rebuild_all(huffman_t *huffm) {
huffnode_t *n;
int i;
memset(&huffm->context, 0, sizeof(huffcontext_t));
huffm->context.pq = huffm->context.queue - 1;
huffm->context.qend = 1;
huffm->context.maxbits = 0;
huffm->context.minbits = 16;
for (i = 0; i < HUFFMAN_TABLESIZE; ++i) {
if (huffm->table[i] < 1)
huffm->table[i] = 1;
n = huffnode_new(huffm, huffm->table[i], i, NULL, NULL);
huffnode_push(huffm, n);
}
while (huffm->context.qend > 2) {
n = huffnode_new(huffm, 0, 0, huffnode_pop(huffm), huffnode_pop(huffm));
huffnode_push(huffm, n);
}
huffnode_build_codes(huffm, huffm->context.pq[1], 0, 0);
return 0;
}
void huffman_table(unsigned short *table, const unsigned char *data, int len) {
int i, j, overflow = 0;
for (i = 0; i < len; ++i) {
if (USHRT_MAX == table[data[i]]) {
overflow = 1;
break;
} else
table[data[i]] += 1;
}
if (overflow)
for (j = 0; j < i; ++j)
table[data[j]] -= 1; /* rollback */
}
huffman_t *huffman_create(unsigned short *table) {
huffman_t *huffm = (huffman_t *)huffalloc.alloc(sizeof(huffman_t));
if (!huffm)
return NULL;
if (0 != huffman_rebuild(huffm, table)) {
huffalloc.release(huffm);
return NULL;
}
return huffm;
}
void huffman_destroy(huffman_t *huffm) {
if (!huffm)
return;
huffalloc.release(huffm);
}
int huffman_rebuild(huffman_t *huffm, unsigned short *table) {
if (table)
memcpy(huffm->table, table, sizeof(huffm->table));
else
memset(huffm->table, 0, sizeof(huffm->table));
return huffman_rebuild_all(huffm);
}
int huffman_enclen(huffman_t *huffm, int bytelen) {
return bytelen * huffm->context.maxbits / 8 + 1;
}
int huffman_declen(huffman_t *huffm, int bitlen) {
return bitlen / huffm->context.minbits;
}
int huffman_encode(huffman_t *huffm, void *outbuf, const void *data, int bytelen) {
const unsigned char *p = (const unsigned char *)data;
unsigned char *rbs, *wbs = (unsigned char *)outbuf;
int i, j, b, roff, woff = 0;
int bitlen = 0;
for (i = 0; i < bytelen; ++i) {
if (huffm->context.codes[p[i]].bitlen <= 0) {
return -1;
}
rbs = (unsigned char *)&huffm->context.codes[p[i]].code;
roff = 0;
for (j = 0; j < huffm->context.codes[p[i]].bitlen; ++j) {
b = bitstream_readbit(&rbs, &roff);
bitstream_writebit(&wbs, b, &woff);
bitlen += 1;
}
}
return bitlen;
}
int huffman_decode(huffman_t *huffm, void *outbuf, const void *data, int bitlen) {
huffnode_t *node = huffm->context.pq[1];
unsigned char *bs = (unsigned char *)data;
unsigned char *decode = (unsigned char *)outbuf;
int i, b, bs_off = 0;
int bytelen = 0;
for (i = 0; i < bitlen; ++i) {
b = bitstream_readbit(&bs, &bs_off);
if (0 == b)
node = node->left;
else
node = node->right;
if (!node)
return -1;
if (node->symbol || (!node->left && !node->right)) {
decode[bytelen++] = node->symbol;
node = huffm->context.pq[1];
}
}
if (node != huffm->context.pq[1])
return -1;
return bytelen;
}