-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.c
272 lines (240 loc) · 4.4 KB
/
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
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
#include "huff.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct {
node * np_node;
uchar np_len;
} np;
node * root;
chent clist[ALFSIZ];
filent flist[MAXFILENT];
short alfused;
chent * cptrs[ALFSIZ];
static np nptrs[ALFSIZ];
static uchar p_bit;
static uchar p_char;
static uchar level;
static short nidx;
/* extern char * calloc(); */
extern void error(char *fmt, ...);
void make_tree(char **);
node * newnode(void);
node * bld(short, short);
int cmpr(chent **, chent **);
int cmpnp(np *, np *);
void bld_bits(node *, h_char);
void pinit(void);
void align(void);
void put_bit(int);
void puthch(uchar);
void put_tchrs(node *);
void walk_tree(node *);
void put_tree(void);
/*
*
*/
void make_tree(char ** namlist) {
FILE * fp;
int c, fno;
h_char tch;
register char * cp;
fno = 0;
while(*namlist) {
if(**namlist == '-')
switch(namlist[0][1]) {
case 'a':
case 'A':
ascii = 1;
namlist++;
continue;
case 'b':
case 'B':
ascii = 0;
namlist++;
continue;
default:
break;
}
if(!(fp = fopen(*namlist, ascii ? "r" : "rb"))) {
fprintf(stderr, "Can't open %s\n", *namlist++);
continue;
}
flist[fno].f_name = *namlist;
flist[fno].f_asc = ascii;
while((c = getc(fp)) != EOF) {
clist[c & (ALFSIZ-1)].c_freq++;
flist[fno].f_nchrs++;
}
fclose(fp);
for(cp = *namlist ; *cp ; )
clist[*cp++ & (ALFSIZ-1)].c_freq++;
clist[0].c_freq++;
namlist++;
fno++;
}
for(c =0 ; c < ALFSIZ ; c++) {
cptrs[c] = clist+c;
clist[c].c_chr = c;
}
qsort(cptrs, ALFSIZ, sizeof cptrs[0], cmpr);
for(alfused = 0 ; alfused < ALFSIZ && cptrs[alfused]->c_freq ; alfused++)
continue;
level = 0;
nidx = 0;
root = bld(0, alfused-1);
qsort(nptrs, alfused, sizeof nptrs[0], cmpnp);
for(c = 0 ; c < alfused ; c++)
nptrs[c].np_node->n_c = cptrs[c]->c_chr;
tch.h_nbits = 0;
bld_bits(root, tch);
}
/*
*
*/
node * newnode(void) {
register node * np;
if(!(np = (node *)calloc(1, sizeof(node))))
error("Out of memory");
return np;
}
/*
*
*/
node * bld(short first, short last) {
int i;
long tot, run;
register node * l, * np;
if(first == last) {
l = newnode();
nptrs[nidx].np_node = l;
nptrs[nidx++].np_len = level+1;
l->n_left = l->n_right = (node *)0;
return l;
}
level++;
for(i = first, tot = 0 ; i <= last ; i++)
tot += cptrs[i]->c_freq;
tot /= factor;
run = cptrs[i = first]->c_freq;
while(run < tot && i != last)
run += cptrs[++i]->c_freq;
if(i == last)
i = (first+last)/2;
if(i == first) {
l = newnode();
nptrs[nidx].np_node = l;
nptrs[nidx++].np_len = level+1;
l->n_left = l->n_right = (node *)0;
} else
l = bld(first, i);
np = newnode();
np->n_left = l;
np->n_right = bld(i+1, last);
level--;
return np;
}
/*
*
*/
int cmpr(register chent ** p1, chent ** p2) {
long i;
i = (*p2)->c_freq - (*p1)->c_freq;
if(i > 0)
return 1;
if(i < 0)
return -1;
return 0;
}
/*
*
*/
int cmpnp(np * p1, np * p2) {
return p1->np_len - p2->np_len;
}
/*
*
*/
void bld_bits(register node * nodep, h_char tch) {
if(!nodep->n_left) { /* leaf node */
clist[nodep->n_c].c_bits = tch;
return;
}
SET(tch.h_cbits, tch.h_nbits);
tch.h_nbits++;
bld_bits(nodep->n_left, tch);
CLR(tch.h_cbits, tch.h_nbits-1);
bld_bits(nodep->n_right, tch);
}
/*
*
*/
void pinit(void) {
p_char = 0;
p_bit = 0;
}
/*
*
*/
void align(void) {
if(p_bit)
putchar(p_char);
pinit();
}
/*
*
*/
void put_bit(int i) {
if(i)
p_char |= 1 << p_bit;
if(++p_bit == CHAR_BIT) {
putchar(p_char);
p_char = 0;
p_bit = 0;
}
}
/*
*
*/
void puthch(uchar c) {
h_char tch;
short i;
tch = clist[c].c_bits;
for(i = 0 ; i < tch.h_nbits ; i++) {
if(TST(tch.h_cbits, i))
p_char |= 1 << p_bit;
if(++p_bit == CHAR_BIT) {
putchar(p_char);
p_char = 0;
p_bit = 0;
}
}
}
/*
*
*/
void put_tchrs(register node * tp) {
if(tp->n_left) {
put_tchrs(tp->n_left);
put_tchrs(tp->n_right);
} else
putchar(tp->n_c);
}
/*
*
*/
void walk_tree(register node * tp) {
if(tp->n_left) {
put_bit(1);
walk_tree(tp->n_left);
put_bit(0);
walk_tree(tp->n_right);
}
}
/*
*
*/
void put_tree(void) {
put_tchrs(root);
walk_tree(root);
put_bit(0); /* a safeguard */
}