-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathhuffman.c
356 lines (324 loc) · 6.67 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
/* Title: Huffman Coding for files
URL: htts://github.com/TheniL/huffman
Date_start: 25/11/2014
OC 1: 01/12/2014
Author: niLesh */
/*
TODO: handle Codewords longer than MAX
TODO: Use free() to deallocate memory
TODO: sort linked list in non-increasing order, at genCode().
TODO: Store code in bit array form(in Header), rather than string form, to save space
*/
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include "huffman.h"
#define INTERNAL 1
#define LEAF 0
typedef struct node
{
char x;
int freq;
char *code;
int type;
struct node *next;
struct node *left;
struct node *right;
}node;
node *HEAD,*ROOT;
void printll();
void makeTree();
void genCode(node *p,char* code);
void insert(node *p,node *m);
void addSymbol(char c);
void writeHeader(FILE *f);
void writeBit(int b,FILE *f);
void writeCode(char ch,FILE *f);
char *getCode(char ch);
node* newNode(char c)
{
node *q;
q=(node *)malloc(sizeof(node));
q->x=c;
q->type=LEAF; //leafnode
q->freq=1;
q->next=NULL;
q->left=NULL;
q->right=NULL;
return q;
}
void printll()
{
node *p;
p=HEAD;
while(p!=NULL)
{
printf("[%c|%d]=>",p->x,p->freq);
p=p->next;
}
}
int main(int argc, char *argv[])
{
FILE *fp,*fp2;
char ch;
int t;
HEAD=NULL;
ROOT=NULL;
if(argc<=2)
{
printf("Usage:\n %s <input-file-to-zip> <zipped-output-file>\n***Huffman File Encoder***\nAuthor: niLesh Akhade\nhttps://github.com/TheniL/huffman\n\n",argv[0]);
if(argc==2)
{
argv[2]=(char *)malloc(sizeof(char)*(strlen(argv[1])+strlen(ext)+1));
strcpy(argv[2],argv[1]);
strcat(argv[2],ext);
argc++;
}
else
return 0;
}
fp=fopen(argv[1],"rb");
if(fp==NULL)
{
printf("[!]Input file cannot be opened.\n");
return -1;
}
printf("\n[Pass1]");
printf("\nReading input file %s",argv[1]);
while(fread(&ch,sizeof(char),1,fp)!=0)
addSymbol(ch);
fclose(fp);
printf("\nConstructing Huffman-Tree..");
makeTree();
printf("\nAssigning Codewords.\n");
genCode(ROOT,"\0"); //preorder traversal
printf("\n[Pass2]");
fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("\n[!]Input file cannot be opened.\n");
return -1;
}
fp2=fopen(argv[2],"wb");
if(fp2==NULL)
{
printf("\n[!]Output file cannot be opened.\n");
return -2;
}
printf("\nReading input file %s",argv[1]);
printf("\nWriting file %s",argv[2]);
printf("\nWriting File Header.");
writeHeader(fp2);
printf("\nWriting compressed content.");
while(fread(&ch,sizeof(char),1,fp)!=0)
{
//printf("\n%c replaced with ",ch);
writeCode(ch,fp2); //write corresponding code into fp2
}
fclose(fp);
fclose(fp2);
printf("\nDone..\n");
return 0;
}
void writeHeader(FILE *f)
{//Table mapping 'codewords' to actual symbols
symCode record;
node *p;
int temp=0,i=0;
p=HEAD;
while(p!=NULL) //determine number of unique symbols and padding of bits
{
temp+=(strlen(p->code)) * (p->freq); //temp stores padding
if(strlen(p->code) > MAX) printf("\n[!] Codewords are longer than usual."); //TODO: Solve this case
temp%=8;
i++;
p=p->next;
}
if(i==256)
N=0; //if 256 diff bit combinations exist, then alias 256 as 0
else
N=i;
fwrite(&N,sizeof(unsigned char),1,f); //read these many structures while reading
printf("\nN=%u",i);
p=HEAD;
while(p!=NULL) //start from HEAD, write each char & its code
{
record.x=p->x;
strcpy(record.code,p->code);
fwrite(&record,sizeof(symCode),1,f);
// printf("\n%c|%s",record.x,record.code);
p=p->next;
}
//discard 'padding' bits before data, while reading
padding=8-(char)temp; //int to char & padding = 8-bitsExtra
fwrite(&padding,sizeof(char),1,f);
printf("\nPadding=%d",padding);
//do actual padding
for(i=0;i<padding;i++)
writeBit(0,f);
}//fun
void writeCode(char ch,FILE *f)
{
char *code;
code=getCode(ch);
//printf("\n%s\n",code);
while(*code!='\0')
{
if(*code=='1')
writeBit(1,f); //write bit 1 into file f
else
writeBit(0,f);
code++;
}
return;
}
void writeBit(int b,FILE *f)
{//My Logic: Maintain static buffer, if it is full, write into file
static char byte;
static int cnt;
char temp;
//printf("\nSetting %dth bit = %d of %d ",cnt,b,byte);
if(b==1)
{ temp=1;
temp=temp<<(7-cnt); //right shift bits
byte=byte | temp;
}
cnt++;
if(cnt==8) //buffer full
{
// printf("[%s]",bitsInChar(byte));
fwrite(&byte,sizeof(char),1,f);
cnt=0; byte=0; //reset buffer
return;// buffer written to file
}
return;
}
char *getCode(char ch)
{
node *p=HEAD;
while(p!=NULL)
{
if(p->x==ch)
return p->code;
p=p->next;
}
return NULL; //not found
}
void insert(node *p,node *m)
{ // insert p in list as per its freq., start from m to right,
// we cant place node smaller than m since we dont have ptr to node left to m
if(m->next==NULL)
{ m->next=p; return;}
while(m->next->freq < p->freq)
{ m=m->next;
if(m->next==NULL)
{ m->next=p; return; }
}
p->next=m->next;
m->next=p;
}
void addSymbol(char c)
{// Insert symbols into linked list if its new, otherwise freq++
node *p,*q,*m;
int t;
if(HEAD==NULL)
{ HEAD=newNode(c);
return;
}
p=HEAD; q=NULL;
if(p->x==c) //item found in HEAD
{
p->freq+=1;
if(p->next==NULL)
return;
if(p->freq > p->next->freq)
{
HEAD=p->next;
p->next=NULL;
insert(p,HEAD);
}
return;
}
while(p->next!=NULL && p->x!=c)
{
q=p; p=p->next;
}
if(p->x==c)
{
p->freq+=1;
if(p->next==NULL)
return;
if(p->freq > p->next->freq)
{
m=p->next;
q->next=p->next;
p->next=NULL;
insert(p,HEAD);
}
}
else //p->next==NULL , all list traversed c is not found, insert it at beginning
{
q=newNode(c);
q->next=HEAD; //first because freq is minimum
HEAD=q;
}
}
void makeTree()
{
node *p,*q;
p=HEAD;
while(p!=NULL)
{
q=newNode('@');
q->type=INTERNAL; //internal node
q->left=p; //join left subtree/node
q->freq=p->freq;
if(p->next!=NULL)
{
p=p->next;
q->right=p; //join right subtree /node
q->freq+=p->freq;
}
p=p->next; //consider next node frm list
if(p==NULL) //list ends
break;
//insert new subtree rooted at q into list starting from p
//if q smaller than p
if(q->freq <= p->freq)
{//place it before p
q->next=p;
p=q;
}
else
insert(q,p); //find appropriate position
}//while
ROOT=q; //q created at last iteration is ROOT of h-tree
}
void genCode(node *p,char* code)
{
char *lcode,*rcode;
static node *s;
static int flag;
if(p!=NULL)
{
//sort linked list as it was
if(p->type==LEAF) //leaf node
{ if(flag==0) //first leaf node
{flag=1; HEAD=p;}
else //other leaf nodes
{ s->next=p;} //sorting LL
p->next=NULL;
s=p;
}
//assign code
p->code=code; //assign code to current node
// printf("[%c|%d|%s|%d]",p->x,p->freq,p->code,p->type);
lcode=(char *)malloc(strlen(code)+2);
rcode=(char *)malloc(strlen(code)+2);
sprintf(lcode,"%s0",code);
sprintf(rcode,"%s1",code);
//recursive DFS
genCode(p->left,lcode); //left child has 0 appended to current node's code
genCode(p->right,rcode);
}
}