-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.c
493 lines (414 loc) · 11.7 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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "huffman.h"
#include "queue.h"
#include "heap.h"
#define HAS_CHILDREN(node) (node->left || node->right)
typedef struct _node {
int repeat; //use as probability
unsigned char value;
struct _node *left, *right;
} Node;
static int CompareByRepeat(void *a, void *b) {
Node *x = a;
Node *y = b;
return x->repeat - y->repeat;
}
static int FileGetSize(FILE *f) {
int initial = ftell(f);
int pos;
fseek(f, 0, SEEK_END);
pos = ftell(f);
fseek(f, initial, SEEK_SET);
return pos;
}
static void FreeCodes(char *codes[]) {
int i;
for(i=0; i<256; i++)
if(codes[i])
free(codes[i]);
}
static int MyStrcmp(const void *a, const void *b) {
const FileEntryHeader *x = a;
const FileEntryHeader *y = b;
return strcmp(x->file_name, y->file_name);
}
/* path/to/file returns file */
static char* TruncateName(char *file) {
char *c;
c = strrchr(file, '/');
if (c)
return c+1;
return file;
}
/* Open files to compress and deal with errors.
* Also, save details of the archive, to be
* written to file.
*/
static void OpenFiles(FILE **f, HuffmanArchive *HA, int argc, char *argv[]) {
int i, j, files_n;
char *tmp;
HA->fh = NULL;
HA->ah.file_entries_n = 0;
files_n = HA->ah.file_entries_n;
for(i=0; i<argc-3; i++) {
f[i] = fopen(argv[3+i], "rb");
if (f[i] == NULL) {
fprintf(stderr, "Error: %s can't be opened\n", argv[3+i]);
continue;
}
tmp = TruncateName(argv[3+i]);
/* verify if it's a duplicate */
for(j=0; j<files_n; j++)
if (!strcmp (tmp, HA->fh[j].file_name))
break;
if (j != files_n) {
fclose(f[i]);
f[i] = NULL;
continue;
}
/* make room for this new file, which is not a duplicate */
HA->fh = realloc (HA->fh, (files_n+1) * sizeof(FileEntryHeader));
memset(&HA->fh[files_n].file_name, 0, MAX_FILENAME_SIZE);
memcpy(HA->fh[files_n].file_name, tmp, strlen(tmp)+1);
files_n = ++HA->ah.file_entries_n;
}
if (!files_n) {
fprintf(stderr, "Error: No input files\n");
exit(1);
}
}
/* This function adds codes to a table as strings;
* if going on left, concatenate 0, if right, 1
* if it's a leaf, add the code obtained
*/
static void ObtainCodifications(Node *root, char *code, char **codes) {
if (root == NULL)
return;
if (root->left) {
strcat(code, "0");
ObtainCodifications (root->left, code, codes);
code[strlen(code)-1] = '\0';
}
if (root->right) {
strcat(code, "1");
ObtainCodifications (root->right, code, codes);
code[strlen(code)-1] = '\0';
}
if ( !HAS_CHILDREN(root) ) {
codes[root->value] = strdup(code);
return;
}
}
/* This function updates the HuffmanNode vector;
* it uses a queue to represent the tree in order
* to be writen to file
*/
static void UpdateNodes(Node *root, HuffmanNode *nodes) {
Queue q = QueueCreate();
Node *node = NULL;
int n = 0;
if (root == NULL)
return;
Enqueue(q, root);
while (!QueueEmpty(q)) {
node = Dequeue(q);
if (node->left) {
Enqueue(q, node->left);
nodes[n].left = QueueSize(q) + n;
} else
nodes[n].left = -1;
if (node->right) {
Enqueue(q, node->right);
nodes[n].right = QueueSize(q) + n;
} else
nodes[n].right = -1;
if ( !HAS_CHILDREN(node) ) {
nodes[n].value = node->value;
nodes[n].left = (nodes[n].right = -1);
}
n++;
}
QueueFree(q);
}
static int ObtainBytes_n(unsigned char *bytes, int nr_bytes, char** codes) {
int i, s = 0;
for(i=0; i<nr_bytes; i++)
s += strlen(codes[bytes[i]]);
return s/8 + ((s % 8 == 0) ? 0 : 1);
}
/* Write FileEntryHeaders and their codes to
* files; also obtain the compressed codes
*/
static void WriteFiles(HuffmanArchive *HA, int argc, FILE **f,
char **codes, FILE *farchive) {
int i, file_len, j, l, k, incr = 0;
unsigned char *bytes, byte = 0, bit;
int bits_n = 0;
for(i=0; i<argc-3; i++) {
if (f[i] == NULL)
continue;
fseek(f[i], 0, SEEK_END);
file_len = ftell(f[i]);
rewind(f[i]);
/* read all bytes from a file */
bytes = calloc (file_len, sizeof(char));
fread(bytes, sizeof(char), file_len, f[i]);
fclose(f[i]);
/* write FileEntryHeader */
HA->fh[incr].bytes_n = ObtainBytes_n(bytes, file_len, codes);
fwrite(&HA->fh[incr], sizeof(struct _FileEntryHeader), 1, farchive);
/* form a byte from char codes; no padding */
for(j=0; j<file_len; j++) {
l = strlen(codes[bytes[j]]);
for(k=0; k<l; k++) {
if (bits_n == 8) {
bits_n = 0;
fwrite(&byte, sizeof(unsigned char), 1, farchive);
byte = 0;
}
/* fill a byte and write it to file when full */
bit = codes[bytes[j]][k] - '0';
bit <<= ( 8 - ++bits_n );
byte |= bit;
}
}
if (bits_n > 0 && file_len)
fwrite(&byte, sizeof(unsigned char), 1, farchive);
byte = 0;
bits_n = 0;
incr++;
free(bytes);
}
}
/* Write the formed HuffmanArchive structure
*/
static void WriteArchive(HuffmanArchive *HA, int argc, char **argv,
FILE **f, char **codes) {
int i;
FILE *farchive = fopen(argv[2], "wb");
fwrite(&HA->ah, sizeof(struct _ArchiveHeader), 1, farchive);
for(i=0; i<HA->ah.huffman_nodes_n; i++)
fwrite(&HA->nodes[i], sizeof(struct _HuffmanNode), 1, farchive);
WriteFiles(HA, argc, f, codes, farchive);
fclose(farchive);
}
/* Find how many times characters from all
* files repeat; also calculate checksum
*/
static void FormNodes(Node *n, int argc, FILE **f, HuffmanArchive *HA) {
unsigned char *byte;
int i, j, bytes, incr = 0;
for(i=0; i<256; i++)
n[i].value = i;
for(i=0; i<argc-3; i++) {
if (f[i] == NULL)
continue;
bytes = FileGetSize(f[i]);
HA->fh[incr].file_size = bytes;
HA->fh[incr].checksum = 0;
byte = calloc (bytes, sizeof(unsigned char));
fread(byte, sizeof(unsigned char), bytes, f[i]);
/* read each byte of file */
for(j=0; j<bytes; j++) {
n[byte[j]].repeat++;
HA->fh[incr].checksum += byte[j];
}
incr++;
free(byte);
}
}
static void AddNodesToHeap(Heap *h, Node *n) {
int i;
/* add the nodes for the HuffmanTree into heap */
for(i=0; i<256; i++)
if (n[i].repeat)
HeapInsert(h, &n[i], CompareByRepeat);
}
static void HuffmanTreeCreate(Heap *h, Node *n, HuffmanArchive *HA) {
Node *n1, *n2;
int c = 256;
/* treat this case separately; add another node
so that a code be generated if only one file with
size 1 is inserted */
if (HA->ah.huffman_nodes_n == 1) {
n1 = HeapExtractMin(h, CompareByRepeat);
n[c].repeat = 0;
n[c].left = n1;
HeapInsert(h, n+c, CompareByRepeat);
HA->ah.huffman_nodes_n++;
} else
/* not a special with 1 byte then */
while (HeapHasMoreThanOneElem(h)) {
n1 = HeapExtractMin(h, CompareByRepeat);
n2 = HeapExtractMin(h, CompareByRepeat);
HA->ah.huffman_nodes_n++;
/* insert a parent for n1, n2 into the heap */
n[c].left = n1;
n[c].right = n2;
n[c].repeat = n1->repeat + n2->repeat;
HeapInsert(h, n+c, CompareByRepeat);
c++;
}
}
void HuffmanCompress(int argc, char *argv[]) {
char code[256] = "", *codes[256] = {0};
Node n[511] = {{0,0,0,0}}, *root;
HuffmanArchive HA;
FILE **f;
Heap *h;
if (argc < 4) {
PRINT_USAGE;
exit(1);
}
f = calloc (argc-3, sizeof(FILE*));
memset(&HA, 0, sizeof(HuffmanArchive));
/* initialise HuffmanArchive and nodes */
OpenFiles(f, &HA, argc, argv);
FormNodes(n, argc, f, &HA);
/* add the nodes to heap */
h = HeapCreate(HEAP_SIZE_DF);
AddNodesToHeap(h, n);
HA.ah.huffman_root_node = 0;
HA.ah.huffman_nodes_n = HeapGetSize(h);
// and update the size while new nodes appear
/* form the HuffmanTree */
HuffmanTreeCreate(h, n, &HA);
root = HeapExtractMin(h, CompareByRepeat);
/* update the structures to be written
to file and write them */
ObtainCodifications(root, code, codes);
UpdateNodes(root, HA.nodes);
WriteArchive(&HA, argc, argv, f, codes);
// also closes the files opened
HeapFree(h);
FreeCodes(codes);
free(HA.fh);
free(f);
}
static unsigned char DecodeByte(HuffmanNode *nodes, int root,
unsigned char *bytes, int *bit) {
int element, shift;
unsigned char mask;
while (nodes[root].left != -1 || nodes[root].right != -1) {
/* find the corresponding bit
* to tell me left/right -> 0/1
*/
shift = 7 - (*bit % 8);
mask = 1 << shift;
element = ( mask & bytes[*bit/8] ) >> shift;
root = (element == 0)? nodes[root].left : nodes[root].right;
(*bit)++;
}
return nodes[root].value;
}
static void DecodeAndWriteFiles(HuffmanArchive HA, char *folder_out, FILE *ar) {
unsigned char *read_bytes, *write_bytes;
int i, j, root, bit, offset, nodes_n, size;
unsigned int checksum;
char *file_out = NULL;
FILE *f;
root = HA.ah.huffman_root_node;
nodes_n = HA.ah.huffman_nodes_n;
/* move to part where file details are */
offset = sizeof(ArchiveHeader) + nodes_n * sizeof(HuffmanNode);
fseek(ar, offset, SEEK_SET);
size = ((folder_out)?strlen(folder_out):0) + MAX_FILENAME_SIZE + 2;
/* here we'll write all files */
for(i=0; i<HA.ah.file_entries_n; i++) {
bit = 0;
checksum = 0;
fseek(ar, sizeof(FileEntryHeader), SEEK_CUR);
/* compose the name of the file to be open */
file_out = calloc (size, sizeof(char));
if (folder_out)
sprintf(file_out, "%s/%s", folder_out, HA.fh[i].file_name);
else
strcpy(file_out, HA.fh[i].file_name);
f = fopen(file_out, "w");
if (f == NULL) {
fprintf(stderr, "Error: Can't open %s\n", folder_out);
fseek(ar, HA.fh[i].bytes_n, SEEK_CUR);
free(file_out);
continue;
}
read_bytes = calloc (HA.fh[i].bytes_n, sizeof(unsigned char));
write_bytes = calloc (HA.fh[i].file_size, sizeof(unsigned char));
fread(read_bytes, sizeof(unsigned char), HA.fh[i].bytes_n, ar);
/* decompress all bytes */
for(j=0; j<HA.fh[i].file_size; j++) {
write_bytes[j] = DecodeByte(HA.nodes, root, read_bytes, &bit);
checksum += write_bytes[j];
}
free(read_bytes);
/* write the file, if checksum is correct */
if (checksum != HA.fh[i].checksum)
fprintf(stderr, "Error: %s is CORRUPT\n", HA.fh[i].file_name);
else
fwrite(write_bytes, sizeof(unsigned char), HA.fh[i].file_size, f);
fclose(f);
free(write_bytes);
free(file_out);
}
}
static void FillArchive(HuffmanArchive *HA, FILE *ar, char *archive) {
int i, nr;
nr = fread(&HA->ah, sizeof(ArchiveHeader), 1, ar);
if (nr != 1) {
fprintf(stderr, "Error: %s is CORRUPT\n", archive);
exit(1);
}
HA->fh = calloc (HA->ah.file_entries_n, sizeof(FileEntryHeader));
for(i=0; i<HA->ah.huffman_nodes_n; i++)
fread(&HA->nodes[i], sizeof(HuffmanNode), 1, ar);
/* for each file, read details */
for(i=0; i<HA->ah.file_entries_n; i++) {
fread(&HA->fh[i], sizeof(FileEntryHeader), 1, ar);
/* skip compressed code */
fseek(ar, HA->fh[i].bytes_n, SEEK_CUR);
}
}
void HuffmanExtract(int argc, char *argv[]) {
FILE *archive;
HuffmanArchive HA;
if (argc < 3) {
PRINT_USAGE;
exit(1);
}
archive = fopen(argv[2], "rb");
FillArchive(&HA, archive, argv[2]);
DecodeAndWriteFiles(HA, argv[3], archive);
fclose(archive);
free(HA.fh);
}
void HuffmanList(int argc, char *argv[]) {
HuffmanArchive HA;
FILE *arf; //archive file
int i;
if (argc != 3) {
PRINT_USAGE;
exit(1);
}
arf = fopen(argv[2], "rb");
if (arf == NULL) {
fprintf(stderr, "Error: Can't open %s\n", argv[2]);
exit(1);
}
/* read ArchiveHeader */
fread(&HA.ah, sizeof(ArchiveHeader), 1, arf);
/* skip HuffmanNodes */
fseek(arf, HA.ah.huffman_nodes_n * sizeof(HuffmanNode), SEEK_CUR);
HA.fh = calloc (HA.ah.file_entries_n, sizeof(FileEntryHeader));
/* read FileEntryHeaders and skip compressed codes */
for(i=0; i<HA.ah.file_entries_n; i++) {
fread(&HA.fh[i], sizeof(FileEntryHeader), 1, arf);
fseek(arf, HA.fh[i].bytes_n, SEEK_CUR);
}
/* sort the output */
qsort(HA.fh, HA.ah.file_entries_n, sizeof(FileEntryHeader), MyStrcmp);
for(i=0; i<HA.ah.file_entries_n; i++)
printf("%-30s %d\n", HA.fh[i].file_name, HA.fh[i].file_size);
fclose(arf);
free(HA.fh);
}