-
Notifications
You must be signed in to change notification settings - Fork 12
/
huff.c
398 lines (341 loc) · 12 KB
/
huff.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
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include "global.h"
#include "huff.h"
static int cmp_tree(const void * a0, const void * b0) {
return ((struct HuffData *)a0)->value - ((struct HuffData *)b0)->value;
}
typedef int (*cmpfun)(const void *, const void *);
int msort_r(void * data, size_t count, size_t size, cmpfun cmp, void * buffer) {
/*
* Out-of-place mergesort (stable sort)
* Returns 1 on success, 0 on failure
*/
void * leftPtr;
void * rightPtr;
void * leftEnd;
void * rightEnd;
int i;
switch (count) {
case 0:
// Should never be here
return 0;
case 1:
// Nothing to do here
break;
case 2:
// Swap the two entries if the right one compares higher.
if (cmp(data, PTR_ADD(data, size)) > 0) {
memcpy(buffer, data, size);
memcpy(data, PTR_ADD(data, size), size);
memcpy(PTR_ADD(data, size), buffer, size);
}
break;
default:
// Merge sort out-of-place.
leftPtr = data;
leftEnd = rightPtr = PTR_ADD(data, count / 2 * size);
rightEnd = PTR_ADD(data, count * size);
// Sort the left half
if (!msort_r(leftPtr, count / 2, size, cmp, buffer))
return 0;
// Sort the right half
if (!msort_r(rightPtr, count / 2 + (count & 1), size, cmp, buffer))
return 0;
// Merge the sorted halves out of place
i = 0;
do {
if (cmp(leftPtr, rightPtr) <= 0) {
memcpy(PTR_ADD(buffer, i * size), leftPtr, size);
PTR_IADD(leftPtr, size);
} else {
memcpy(PTR_ADD(buffer, i * size), rightPtr, size);
PTR_IADD(rightPtr, size);
}
} while (++i < count && leftPtr < leftEnd && rightPtr < rightEnd);
// Copy the remainder
if (i < count) {
if (leftPtr < leftEnd) {
memcpy(PTR_ADD(buffer, i * size), leftPtr, PTR_DIFF(leftEnd, leftPtr));
}
else {
memcpy(PTR_ADD(buffer, i * size), rightPtr, PTR_DIFF(rightEnd, rightPtr));
}
}
// Copy the merged data back
memcpy(data, buffer, count * size);
break;
}
return 1;
}
int msort(void * data, size_t count, size_t size, cmpfun cmp) {
void * buffer = malloc(count * size);
if (buffer == NULL) return 0;
int result = msort_r(data, count, size, cmp, buffer);
free(buffer);
return result;
}
static void write_tree(unsigned char * dest, HuffNode_t * tree, int nitems, struct BitEncoding * encoding) {
/*
* The example used to guide this function encodes the tree in a
* breadth-first manner. We attempt to emulate that here.
*/
int i, j, k;
// There are (2 * nitems - 1) nodes in the binary tree. Allocate that.
HuffNode_t * traversal = calloc(2 * nitems - 1, sizeof(HuffNode_t));
if (traversal == NULL)
FATAL_ERROR("Fatal error while compressing Huff file.\n");
// The first node is the root of the tree.
traversal[0] = *tree;
i = 1;
// Copy the tree into a breadth-first ordering using brute force.
for (int depth = 1; i < 2 * nitems - 1; depth++) {
// Consider every possible path up to the current depth.
for (j = 0; i < 2 * nitems - 1 && j < 1 << depth; j++) {
// The index of the path is used to encode the path itself.
// Start from the most significant relevant bit and work our way down.
// Keep track of the current and previous nodes.
HuffNode_t * currNode = traversal;
HuffNode_t * parent = NULL;
for (k = 0; k < depth; k++) {
if (currNode->header.isLeaf)
break;
parent = currNode;
if ((j >> (depth - k - 1)) & 1)
currNode = currNode->branch.right;
else
currNode = currNode->branch.left;
}
// Check that the length of the current path equals the current depth.
if (k == depth) {
// Make sure we can encode the current branch.
// Bail here if we cannot.
// This is only applicable for 8-bit encodings.
if (traversal + i - parent > 128)
FATAL_ERROR("Fatal error while compressing Huff file: unable to encode binary tree.\n");
// Copy the current node, and update its parent.
traversal[i] = *currNode;
if (parent != NULL) {
if ((j & 1) == 1)
parent->branch.right = traversal + i;
else
parent->branch.left = traversal + i;
}
// Encode the path through the tree in the lookup table
if (traversal[i].header.isLeaf) {
encoding[traversal[i].leaf.key].nbits = depth;
encoding[traversal[i].leaf.key].bitstring = j;
}
i++;
}
}
}
// Encode the size of the tree.
// This is used by the decompressor to skip the tree.
dest[4] = nitems - 1;
// Encode each node in the tree.
for (i = 0; i < 2 * nitems - 1; i++) {
HuffNode_t * currNode = traversal + i;
if (currNode->header.isLeaf) {
dest[5 + i] = traversal[i].leaf.key;
} else {
dest[5 + i] = (unsigned char)(((currNode->branch.right - traversal - i) / 2) - 1);
if (currNode->branch.left->header.isLeaf)
dest[5 + i] |= 0x80;
if (currNode->branch.right->header.isLeaf)
dest[5 + i] |= 0x40;
}
}
free(traversal);
}
static inline void write_32_le(unsigned char * dest, int * destPos, uint32_t * buff, int * buffPos) {
dest[*destPos] = *buff;
dest[*destPos + 1] = *buff >> 8;
dest[*destPos + 2] = *buff >> 16;
dest[*destPos + 3] = *buff >> 24;
*destPos += 4;
*buff = 0;
*buffPos = 0;
}
static inline void read_32_le(unsigned char * src, int * srcPos, uint32_t * buff) {
uint32_t tmp = src[*srcPos];
tmp |= src[*srcPos + 1] << 8;
tmp |= src[*srcPos + 2] << 16;
tmp |= src[*srcPos + 3] << 24;
*srcPos += 4;
*buff = tmp;
}
static void write_bits(unsigned char * dest, int * destPos, struct BitEncoding * encoding, int value, uint32_t * buff, int * buffBits) {
int nbits = (int)encoding[value].nbits;
uint32_t bitstring = (uint32_t)encoding[value].bitstring;
if (*buffBits + nbits >= 32) {
int diff = *buffBits + nbits - 32;
*buff <<= nbits - diff;
*buff |= bitstring >> diff;
bitstring &= ~(1 << diff);
nbits = diff;
write_32_le(dest, destPos, buff, buffBits);
}
if (nbits != 0) {
*buff <<= nbits;
*buff |= bitstring;
*buffBits += nbits;
}
}
/*
=======================================
MAIN COMPRESSION/DECOMPRESSION ROUTINES
=======================================
*/
unsigned char * HuffCompress(unsigned char * src, int srcSize, int * compressedSize_p, int bitDepth) {
if (srcSize <= 0)
goto fail;
int worstCaseDestSize = 4 + (2 << bitDepth) + srcSize * 3;
unsigned char *dest = malloc(worstCaseDestSize);
if (dest == NULL)
goto fail;
int nitems = 1 << bitDepth;
HuffNode_t * freqs = calloc(nitems, sizeof(HuffNode_t));
if (freqs == NULL)
goto fail;
struct BitEncoding * encoding = calloc(nitems, sizeof(struct BitEncoding));
if (encoding == NULL)
goto fail;
// Set up the frequencies table. This will inform the tree.
for (int i = 0; i < nitems; i++) {
freqs[i].header.isLeaf = 1;
freqs[i].header.value = 0;
freqs[i].leaf.key = i;
}
// Count each nybble or byte.
for (int i = 0; i < srcSize; i++) {
if (bitDepth == 8) {
freqs[src[i]].header.value++;
} else {
freqs[src[i] >> 4].header.value++;
freqs[src[i] & 0xF].header.value++;
}
}
#ifdef DEBUG
for (int i = 0; i < nitems; i++) {
fprintf(stderr, "%d: %d\n", i, freqs[i].header.value);
}
#endif // DEBUG
// Sort the frequency table.
if (!msort(freqs, nitems, sizeof(HuffNode_t), cmp_tree))
goto fail;
// Prune zero-frequency values.
for (int i = 0; i < nitems; i++) {
if (freqs[i].header.value != 0) {
if (i > 0) {
for (int j = i; j < nitems; j++) {
freqs[j - i] = freqs[j];
}
nitems -= i;
}
break;
}
// This should never happen:
if (i == nitems - 1)
goto fail;
}
HuffNode_t * tree = calloc(nitems * 2 - 1, sizeof(HuffNode_t));
if (tree == NULL)
goto fail;
// Iteratively collapse the two least frequent nodes.
HuffNode_t * endptr = freqs + nitems - 2;
for (int i = 0; i < nitems - 1; i++) {
HuffNode_t * left = freqs;
HuffNode_t * right = freqs + 1;
tree[i * 2] = *right;
tree[i * 2 + 1] = *left;
for (int j = 0; j < nitems - i - 2; j++)
freqs[j] = freqs[j + 2];
endptr->header.isLeaf = 0;
endptr->header.value = tree[i * 2].header.value + tree[i * 2 + 1].header.value;
endptr->branch.left = tree + i * 2;
endptr->branch.right = tree + i * 2 + 1;
endptr--;
if (i < nitems - 2 && !msort(freqs, nitems - i - 1, sizeof(HuffNode_t), cmp_tree))
goto fail;
}
// Write the tree breadth-first, and create the path lookup table.
write_tree(dest, freqs, nitems, encoding);
free(tree);
free(freqs);
// Encode the data itself.
int destPos = 4 + nitems * 2;
uint32_t destBuf = 0;
uint32_t srcBuf = 0;
int destBitPos = 0;
for (int srcPos = 0; srcPos < srcSize;) {
read_32_le(src, &srcPos, &srcBuf);
for (int i = 0; i < 32 / bitDepth; i++) {
write_bits(dest, &destPos, encoding, srcBuf & (0xFF >> (8 - bitDepth)), &destBuf, &destBitPos);
srcBuf >>= bitDepth;
}
}
if (destBitPos != 0) {
write_32_le(dest, &destPos, &destBuf, &destBitPos);
}
free(encoding);
// Write the header.
dest[0] = bitDepth | 0x20;
dest[1] = srcSize;
dest[2] = srcSize >> 8;
dest[3] = srcSize >> 16;
*compressedSize_p = (destPos + 3) & ~3;
return dest;
fail:
FATAL_ERROR("Fatal error while compressing Huff file.\n");
}
unsigned char * HuffDecompress(unsigned char * src, int srcSize, int * uncompressedSize_p) {
if (srcSize < 4)
goto fail;
int bitDepth = *src & 15;
if (bitDepth != 4 && bitDepth != 8)
goto fail;
int destSize = (src[3] << 16) | (src[2] << 8) | src[1];
unsigned char *dest = malloc(destSize);
if (dest == NULL)
goto fail;
int treePos = 5;
int treeSize = (src[4] + 1) * 2;
int srcPos = 4 + treeSize;
int destPos = 0;
int curValPos = 0;
uint32_t destTmp = 0;
uint32_t window;
for (;;)
{
if (srcPos >= srcSize)
goto fail;
read_32_le(src, &srcPos, &window);
for (int i = 0; i < 32; i++) {
int curBit = (window >> 31) & 1;
unsigned char treeView = src[treePos];
bool isLeaf = ((treeView << curBit) & 0x80) != 0;
treePos &= ~1; // align
treePos += ((treeView & 0x3F) + 1) * 2 + curBit;
if (isLeaf) {
destTmp >>= bitDepth;
destTmp |= (src[treePos] << (32 - bitDepth));
curValPos++;
if (curValPos == 32 / bitDepth) {
write_32_le(dest, &destPos, &destTmp, &curValPos);
if (destPos == destSize) {
*uncompressedSize_p = destSize;
return dest;
}
}
treePos = 5;
}
window <<= 1;
}
}
fail:
FATAL_ERROR("Fatal error while decompressing Huff file.\n");
}