-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdehuff.c
199 lines (180 loc) · 3.92 KB
/
dehuff.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
#include <stdlib.h>
#include "huff.h"
hdr hd;
static short indent;
static uchar listflg;
static uchar xtractflg;
static uchar debug;
static char ** av;
extern void align(void);
extern void bld_tree(void);
extern void error(char *fmt, ...);
extern int casecmp(register char *, char *);
extern int gethch(void);
int isarg(char * s);
char * getname(short i);
void list(short i);
void extract(short i);
void prtree(register node * np);
#if !unix
extern char ** _getargs();
extern int _argc_;
#endif /* unix */
/*
*
*/
int main(int argc, char ** argv) {
short i;
#if !unix
if(argc == 1) {
argv = _getargs(0, "dehuff");
argc = _argc_;
}
#endif /* unix */
argc--;
argv++;
while(argc && **argv == '-') {
switch(argv[0][1]) {
case 'l':
case 'L':
listflg++;
break;
case 'x':
case 'X':
xtractflg++;
break;
case 'd':
case 'D':
debug++;
break;
default:
fprintf(stderr, "Unrecognized flag %s ignored\n", argv[0]);
break;
}
argc--;
argv++;
}
if(argc < 1) {
printf("Usage of dehuff:\n\nTo list the contents of a .HUF file:\n");
printf("\n dehuff file.huf\n\nTo extract files from a .HUF file:\n");
printf("\n dehuff -x file.huf\n\nIf no extra arguments are supplied, then the entire contents\n");
printf("of the .HUF file will be listed or extracted, otherwise only files\n");
printf("matching an argument will be listed or extracted, e.g.\n\n");
printf(" dehuff -x xyz.huf afile.c\n\nwould extract only the file afile.c.\n");
printf("The number reported for each file is its length in bytes\n");
exit(1);
}
av = argv+1;
if(!freopen(*argv, "rb", stdin))
error("Can't open file %s", *argv);
hd.hd_magic = get2();
if(hd.hd_magic != MAGIC)
error("%s is not a huf file", *argv);
hd.hd_nfiles = get2();
alfused = hd.hd_alfsiz = get2();
hd.hd_hpos = get4();
bld_tree();
if(debug)
prtree(root);
for(i = 0 ; i < hd.hd_nfiles ; i++)
if(isarg(getname(i)))
if(xtractflg)
extract(i);
else
list(i);
}
/*
*
*/
int isarg(char * s) {
register char ** p;
if(*av == 0) /* default is all members */
return 1;
for(p = av ; *p ; p++)
if(casecmp(*p, s) == 0)
return 1;
return 0;
}
/*
*
*/
char * getname(short i) {
static char fbuf[100];
register char * cp;
filent hf;
fseek(stdin, hd.hd_hpos+i*FSIZE, 0);
hf.f_npos = get4();
hf.f_nchrs = get4();
hf.f_pos = get4();
hf.f_asc = getchar();
fseek(stdin, hf.f_npos, 0);
align();
for(cp = fbuf ; (*cp++ = gethch()) ; )
continue;
return fbuf;
}
/*
*
*/
void list(short i) {
register char * cp;
filent hf;
char fbuf[100];
fseek(stdin, hd.hd_hpos+i*FSIZE, 0);
hf.f_npos = get4();
hf.f_nchrs = get4();
hf.f_pos = get4();
hf.f_asc = getchar();
fseek(stdin, hf.f_npos, 0);
align();
for(cp = fbuf ; (*cp++ = gethch()) ; )
continue;
fprintf(stderr, "%-20.20s %ld\n", fbuf, hf.f_nchrs);
}
/*
*
*/
void extract(short i) {
register char * cp;
filent hf;
FILE * fp;
char fbuf[100];
fseek(stdin, hd.hd_hpos+i*FSIZE, 0);
hf.f_npos = get4();
hf.f_nchrs = get4();
hf.f_pos = get4();
hf.f_asc = getchar();
fseek(stdin, hf.f_npos, 0);
align();
for(cp = fbuf ; (*cp++ = gethch()) ; )
continue;
if(!(fp = fopen(fbuf, hf.f_asc ? "w" : "wb"))) {
fprintf(stderr, "Can't create %s\n", fbuf);
return;
}
fprintf(stderr, "%-20.20s %ld\n", fbuf, hf.f_nchrs);
fseek(stdin, hf.f_pos, 0);
align();
while(hf.f_nchrs--)
putc(gethch(), fp);
fclose(fp);
}
/*
*
*/
void prtree(register node * np) {
short i;
for(i = indent ; i-- ; )
fputc(' ', stderr);
if(np->n_left) {
fprintf(stderr, "X\n");
indent += 4;
prtree(np->n_left);
prtree(np->n_right);
indent -= 4;
} else
if(np->n_c >= ' ' && np->n_c <= 0176)
fprintf(stderr, "'%c'\n", np->n_c);
else
fprintf(stderr, "%03o\n", np->n_c);
}