-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuffman.c
More file actions
229 lines (187 loc) · 4.35 KB
/
Copy pathhuffman.c
File metadata and controls
229 lines (187 loc) · 4.35 KB
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
#include <stdint.h>
#include "huffman.h"
// Selection sort to sort the nodes by frequency (weight)
static void sort(struct p_list *list) {
int i, j;
node temp;
for (i = 0; i < list->size; i++) {
for (j = i; j < list->size; j++) {
if (list->arr[i]->weight < list->arr[j]->weight) {
temp = list->arr[i];
list->arr[i] = list->arr[j];
list->arr[j] = temp;
}
}
}
}
// Traverse the binary tree to generate Huffman codes
// Uses recursion
void gen_code(node root, code_list list, char _code[], int len){
char code[40];
_code[len] = '\0';
strcpy(code,_code);
if(root->type == TYPE_LEAF){
strcpy(list->code[list->size],code);
list->ch[list->size] = root->ch;
list->size++;
}
else if(root->type == TYPE_BRANCH){
code[len] = '0';
len = len + 1;
gen_code(root->left, list, code, len);
len = len - 1;
code[len] = '1';
len++;
gen_code(root->right, list, code, len);
}
else{
printf("root->type unknown. "
"Probably a memory corruption somewhere\n");
exit(1);
}
}
node new_node(char c, int weight, int type){
node x = malloc(sizeof(*x));
x->type = type;
x->ch = c;
x->weight = weight;
x->parent = NULL;
x->right = NULL;
x->left = NULL;
return x;
}
// Take an array of sorted nodes to create the binary tree
node create_tree(struct p_list list){
while(list.size != 1){
int weight = list.arr[list.size-1]->weight + list.arr[list.size-2]->weight;
node branch = new_node('\0', weight, TYPE_BRANCH);
branch->left = list.arr[list.size-1];
branch->right = list.arr[list.size-2];
list.arr[list.size-2] = branch;
list.size--;
sort(&list);
}
return list.arr[0];
}
struct p_list create_nodes(char str[]){
int hash[256] = {0};
int i;
struct p_list list;
list.size = 0;
for(i=0; str[i] != '\0'; i++){
hash[(int)str[i]]++;
}
for(i=0; i<256; i++){
if(hash[i] != 0){
node x = new_node((char)i, hash[i], TYPE_LEAF);
list.arr[list.size] = x;
list.size++;
}
}
sort(&list);
return list;
}
// Combine the above functions to generate Huffman codes
code_list encode(struct p_list list){
code_list codes = malloc(sizeof(*codes));
codes->size = 0;
codes->freq_list = list;
char cd[10];
gen_code(create_tree(list),codes,cd,0);
return codes;
}
// Get the Huffman code for a given char
char* get_code(code_list list, char ch){
int i;
for(i=0; i < list->size; i++){
if(list->ch[i] == ch){
return list->code[i];
}
}
return (char *)NULL;
}
void display_codes(code_list list){
int i;
for(i = 0; i < list->size; i++){
printf("%c -> %s\n", list->ch[i], list->code[i]);
}
}
uint8_t get_bit(char *code, int pos){
if(code[pos] != '\0')
return code[pos] - '0';
else
return 2;
}
void write_code(FILE *f, code_list list, char str[]){
fputc(list->freq_list.size, f);
int i;
for(i=0; i < list->freq_list.size; i++){
fputc( list->freq_list.arr[i]->ch, f);
fwrite(&list->freq_list.arr[i]->weight, sizeof(int), 1, f);
}
long int len = strlen(str);
fwrite(&len, sizeof(long int), 1, f);
i = 0;
int bit_pos=0;
unsigned char out = (char)0;
while(i<len){
uint8_t bit;
char *code = get_code(list, str[i]);
int pos = 0;
while((bit = get_bit(code, pos++)) != 2){
out <<= 1;
out |= bit;
if(++bit_pos == 8){
fputc(out, f);
bit_pos = 0;
out = (char)0;
}
}
i++;
}
if(bit_pos != 0){
out <<= (8-bit_pos);
fputc(out,f);
}
}
char *read_code(FILE *f){
fseek(f, 0, SEEK_END);
fseek(f, 0, SEEK_SET);
int i = (int)fgetc(f);
int j;
struct p_list list;
list.size = i;
for(j=0; j<i; j++){
int weight;
char ch = fgetc(f);
fread(&weight, sizeof(int), 1, f);
node x = new_node(ch, weight, TYPE_LEAF);
list.arr[j] = x;
}
sort(&list);
long int length;
fread(&length, sizeof(long int), 1, f);
char *out = malloc(length+200);
long int size = 0;
node _root = create_tree(list);
int pos = -1;
unsigned char c;
while(size<length){
node root = _root;
while(root->type != TYPE_LEAF){
if(pos == -1){
pos = 7;
c = fgetc(f);
}
int bit = c & (1 << pos);
if(bit == 0)
root = root->left;
else
root = root->right;
pos--;
}
out[size++] = root->ch;
}
out[size] = '\0';
return out;
}