-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.c
62 lines (52 loc) · 1.07 KB
/
decode.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
#include <stdlib.h>
#include "huff.h"
char cl[ALFSIZ];
short alfused;
static short clidx;
static short g_bit;
static short g_char;
node * root;
void bld_tree(void);
node * get_tree(void);
void align(void);
int get_bit(void);
int gethch(void);
extern void error(char *fmt, ...);
void bld_tree(void) {
short i;
for(i = 0 ; i < alfused ; i++)
cl[i] = getchar();
align();
clidx = 0;
root = get_tree();
}
node * get_tree(void) {
register node * tp;
if(!(tp = (node *)calloc(sizeof(node), 1)))
error("Out of memory");
if(get_bit()) {
tp->n_left = get_tree();
tp->n_right = get_tree();
} else
tp->n_c = cl[clidx++];
return tp;
}
void align(void) {
g_char = 0;
g_bit = CHAR_BIT;
}
int get_bit(void) {
if(g_bit == CHAR_BIT)
if((g_char = getchar()) == EOF)
error("Read error or EOF on huf file");
else
g_bit = 0;
return g_char & (1 << g_bit++);
}
int gethch(void) {
register node * tp;
tp = root;
while(tp->n_left)
tp = get_bit() ? tp->n_left : tp->n_right;
return tp->n_c & 0xFF;
}