-
Notifications
You must be signed in to change notification settings - Fork 274
/
huffman.c
475 lines (320 loc) · 9.61 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//
// huffman.c
// Algorithms - Huffman compress
//
// Created by YourtionGuo on 18/05/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include <limits.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include "bit.h"
#include "bitree.h"
#include "compress.h"
#include "pqueue.h"
#pragma mark - Private
/**
比较两棵霍夫曼树的根节点频率
@param tree1 霍夫曼树1
@param tree2 霍夫曼树2
@return tree1 小于 tree1 返回1, tree1 大于 tree1 返回 -1, 相等返回0
*/
static int compare_freq(const void *tree1, const void *tree2)
{
HuffNode *root1, *root2;
/// 比较两棵已经排序的二叉树树根节点
root1 = (HuffNode *)bitree_data(bitree_root((const BiTree *)tree1));
root2 = (HuffNode *)bitree_data(bitree_root((const BiTree *)tree2));
if (root1->freq < root2->freq) return 1;
if (root1->freq > root2->freq) return -1;
return 0;
}
/**
销毁霍夫曼树
@param tree 霍夫曼树
*/
static void destroy_tree(void *tree)
{
/// 销毁并回收一棵二叉树空间
bitree_destroy(tree);
free(tree);
return;
}
/**
通过字节频率建立霍夫曼树
@param freqs 符号频率数组
@param tree 构建后的霍夫曼树
@return 成功返回 0, 否则返回 -1
*/
static int build_tree(int *freqs, BiTree **tree) {
BiTree *init, *merge, *left, *right;
PQueue pqueue;
HuffNode *data;
int size, c;
/// 初始化二叉树优先队列
*tree = NULL;
pqueue_init(&pqueue, compare_freq, destroy_tree);
for (c = 0; c <= UCHAR_MAX; c++) {
if (freqs[c] != 0) {
/// 创建二叉树并设置当前符号和频率
if ((init = (BiTree *)malloc(sizeof(BiTree))) == NULL) {
pqueue_destroy(&pqueue);
return -1;
}
bitree_init(init, free);
if ((data = (HuffNode *)malloc(sizeof(HuffNode))) == NULL) {
pqueue_destroy(&pqueue);
return -1;
}
data->symbol = c;
data->freq = freqs[c];
if (bitree_ins_left(init, NULL, data) != 0) {
free(data);
bitree_destroy(init);
free(init);
pqueue_destroy(&pqueue);
return -1;
}
/// 插入二叉树到优先队列
if (pqueue_insert(&pqueue, init) != 0) {
bitree_destroy(init);
free(init);
pqueue_destroy(&pqueue);
return -1;
}
}
}
/// 通过合并优先队列中的二叉树构建霍夫曼树
size = pqueue_size(&pqueue);
for (c = 1; c <= size - 1; c++) {
/// 创建合并后树的空间
if ((merge = (BiTree *)malloc(sizeof(BiTree))) == NULL) {
pqueue_destroy(&pqueue);
return -1;
}
/// 提取出两个根节点最小的两棵二叉树
if (pqueue_extract(&pqueue, (void **)&left) != 0) {
pqueue_destroy(&pqueue);
free(merge);
return -1;
}
if (pqueue_extract(&pqueue, (void **)&right) != 0) {
pqueue_destroy(&pqueue);
free(merge);
return -1;
}
/// 创建合并后根节点的数据空间
if ((data = (HuffNode *)malloc(sizeof(HuffNode))) == NULL) {
pqueue_destroy(&pqueue);
free(merge);
return -1;
}
memset(data, 0, sizeof(HuffNode));
/// 计算已经合并二叉树根节点的频率值(求和子节点)
data->freq = ((HuffNode *)bitree_data(bitree_root(left)))->freq +
((HuffNode *)bitree_data(bitree_root(right)))->freq;
/// 合并两棵树
if (bitree_merge(merge, left, right, data) != 0) {
pqueue_destroy(&pqueue);
free(merge);
return -1;
}
/// 将合并的二叉树插入回优先队列并释放相关的空间
if (pqueue_insert(&pqueue, merge) != 0) {
pqueue_destroy(&pqueue);
bitree_destroy(merge);
free(merge);
return -1;
}
free(left);
free(right);
}
/// 优先队列中剩余的最后一棵二叉树就是霍夫曼树
if (pqueue_extract(&pqueue, (void **)tree) != 0) {
pqueue_destroy(&pqueue);
return -1;
} else {
pqueue_destroy(&pqueue);
}
return 0;
}
/**
建立霍夫曼编码表
@param node 霍夫曼树结点
@param code 当前生成的霍夫曼编码
@param size 编码的位数
@param table 霍夫曼编码结果
*/
static void build_table(BiTreeNode *node, unsigned short code, unsigned char size, HuffCode *table) {
if (!bitree_is_eob(node)) {
if (!bitree_is_eob(bitree_left(node))) {
/// 向左移动并在编码末尾追加 0
build_table(bitree_left(node), code << 1, size + 1, table);
}
if (!bitree_is_eob(bitree_right(node))) {
/// 向右移动并在编码末尾追加 1
build_table(bitree_right(node), (code << 1) | 0x0001, size + 1, table);
}
if (bitree_is_eob(bitree_left(node))&&bitree_is_eob(bitree_right(node))) {
/// 确保当前编码是以大端字节格式存放
code = htons(code);
/// 将当前符号编码插入叶子节点
table[((HuffNode *)bitree_data(node))->symbol].used = 1;
table[((HuffNode *)bitree_data(node))->symbol].code = code;
table[((HuffNode *)bitree_data(node))->symbol].size = size;
}
}
return;
}
#pragma mark - Public
int huffman_compress(const unsigned char *original, unsigned char **compressed, int size)
{
BiTree *tree;
HuffCode table[UCHAR_MAX + 1];
int freqs[UCHAR_MAX + 1], max, scale, hsize, ipos, opos, cpos, c, i;
unsigned char *comp, *temp;
/// 初始化最初的压缩结果为空
*compressed = NULL;
/// 获取原始数据中每个符号的频率
for (c = 0; c <= UCHAR_MAX; c++) {
freqs[c] = 0;
}
ipos = 0;
if (size > 0) {
while (ipos < size) {
freqs[original[ipos]]++;
ipos++;
}
}
/// 缩放频率以适应一个 byte 大小(可以只用一个字节来表示)
max = UCHAR_MAX;
for (c = 0; c <= UCHAR_MAX; c++) {
if (freqs[c] > max) {
max = freqs[c];
}
}
for (c = 0; c <= UCHAR_MAX; c++) {
scale = (int)(freqs[c] / ((double)max / (double)UCHAR_MAX));
if (scale == 0 && freqs[c] != 0) {
/// 确保当任何非 0 频率值其缩减为小于 1 时,最终应该将其设为 1 而不是 0
freqs[c] = 1;
} else {
freqs[c] = scale;
}
}
/// 构建霍夫曼树和霍夫曼编码表
if (build_tree(freqs, &tree) != 0) return -1;
for (c = 0; c <= UCHAR_MAX; c++) {
memset(&table[c], 0, sizeof(HuffCode));
}
build_table(bitree_root(tree), 0x0000, 0, table);
bitree_destroy(tree);
free(tree);
/// 写入头部信息
hsize = sizeof(int) + (UCHAR_MAX + 1);
if ((comp = (unsigned char *)malloc(hsize)) == NULL) return -1;
memcpy(comp, &size, sizeof(int));
for (c = 0; c <= UCHAR_MAX; c++) {
comp[sizeof(int) + c] = (unsigned char)freqs[c];
}
/// 压缩数据
ipos = 0;
opos = hsize * 8;
while (ipos < size) {
/// 获取原始数据中的下一个符号
c = original[ipos];
/// 将当前符号的 code 写入压缩后数据
for (i = 0; i < table[c].size; i++) {
if (opos % 8 == 0) {
/// 创建另外一个 byte 用于存放压缩数据
if ((temp = (unsigned char *)realloc(comp,(opos / 8) + 1)) == NULL) {
free(comp);
return -1;
}
comp = temp;
}
cpos = (sizeof(short) * 8) - table[c].size + i;
bit_set(comp, opos, bit_get((unsigned char *)&table[c].code, cpos));
opos++;
}
ipos++;
}
/// 将缓冲区指向已压缩数据
*compressed = comp;
/// 返回压缩后数据的长度
return ((opos - 1) / 8) + 1;
}
int huffman_uncompress(const unsigned char *compressed, unsigned char **original)
{
BiTree *tree;
BiTreeNode *node;
int freqs[UCHAR_MAX + 1], hsize, size, ipos, opos, state, c;
unsigned char *orig, *temp;
/// 初始化最初的解压结果为空
*original = orig = NULL;
/// 获取压缩数据中的头部信息
hsize = sizeof(int) + (UCHAR_MAX + 1);
memcpy(&size, compressed, sizeof(int));
for (c = 0; c <= UCHAR_MAX; c++) {
freqs[c] = compressed[sizeof(int) + c];
}
/// 通过获取的头部信息构建压缩数据时用的霍夫曼树
if (build_tree(freqs, &tree) != 0) return -1;
/// 解压数据
ipos = hsize * 8;
opos = 0;
node = bitree_root(tree);
while (opos < size) {
/// 获取压缩数据中的下一位
state = bit_get(compressed, ipos);
ipos++;
if (state == 0) {
/// 向左移动
if (bitree_is_eob(node) || bitree_is_eob(bitree_left(node))) {
bitree_destroy(tree);
free(tree);
return -1;
} else {
node = bitree_left(node);
}
} else {
/// 向右移动
if (bitree_is_eob(node) || bitree_is_eob(bitree_right(node))) {
bitree_destroy(tree);
free(tree);
return -1;
} else {
node = bitree_right(node);
}
}
if (bitree_is_eob(bitree_left(node))&&bitree_is_eob(bitree_right(node))) {
/// 将叶子节点中的符号写回解压数据
if (opos > 0) {
if ((temp = (unsigned char *)realloc(orig, opos + 1)) == NULL) {
bitree_destroy(tree);
free(tree);
free(orig);
return -1;
}
orig = temp;
} else {
if ((orig = (unsigned char *)malloc(1)) == NULL) {
bitree_destroy(tree);
free(tree);
return -1;
}
}
orig[opos] = ((HuffNode *)bitree_data(node))->symbol;
opos++;
/// 移回树的根节点
node = bitree_root(tree);
}
}
bitree_destroy(tree);
free(tree);
/// 将缓冲区指向已解压数据
*original = orig;
/// 返回解压后数据大小
return opos;
}