-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman_encode.c
96 lines (68 loc) · 1.73 KB
/
huffman_encode.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include "huffman.h"
FILE *fpIn = NULL;
FILE *fpOut = NULL;
entry *head = NULL;
entry *root = NULL;
unsigned char buffer = 0;
int buffer_fill_count = 0;
int main(int argc, char **argv) {
assert(argc == 2);
char *inputFile = argv[1];
char *outputFile = calloc(sizeof(*inputFile), MAX_STRING_LEN);
// Create Output File
strcpy(outputFile, inputFile);
strcat(outputFile, ".huff");
fpIn = fopen(inputFile, "rb");
fpOut = fopen(outputFile, "wb");
assert(fpIn != NULL);
assert(fpOut != NULL);
unsigned char val;
while(fread(&val, sizeof(val), 1, fpIn) == 1) {
entry *rover = head;
while(rover != NULL) {
if(*rover->val == val) {
rover->freq++;
break;
}
rover = rover->fwd;
}
if(rover == NULL) create_entry(val);
}
entry *rover = head;
for(int i = 0; i < 256; i++) {
if(rover != NULL && *rover->val == i) {
fwrite(&(rover->freq), sizeof(rover->freq), 1, fpOut);
rover = rover->fwd;
} else {
int zero = 0;
fwrite(&zero, sizeof(int), 1, fpOut);
}
}
list_sort_by_freq();
build_tree();
rewind(fpIn);
buffer = 0;
buffer_fill_count = 0;
while(fread(&val, sizeof(val), 1, fpIn) == 1) {
sequence *currentSeq = build_encoded_sequence(val, root);
sequence *rover = currentSeq;
while(rover != NULL) {
buffer |= rover->val << (7 - buffer_fill_count); // Upper Most Bit is First
buffer_fill_count = (buffer_fill_count + 1) % 8;
if(buffer_fill_count == 0) {
fwrite(&buffer, sizeof(buffer), 1, fpOut);
buffer = 0;
}
rover = rover->fwd;
}
}
if(buffer_fill_count != 0) fwrite(&buffer, sizeof(buffer), 1, fpOut);
fclose(fpIn);
fclose(fpOut);
return 0;
}