-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhuffcode.c
More file actions
293 lines (261 loc) · 5.01 KB
/
huffcode.c
File metadata and controls
293 lines (261 loc) · 5.01 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
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
#include "huffman.h"
#ifdef USE_PARALLEL
#include "huffman_parallel.h"
#endif
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <malloc.h>
extern int getopt(int, char**, char*);
extern char* optarg;
#else
#include <unistd.h>
#endif
#include <bits/getopt_core.h>
static int memory_encode_file(FILE* in, FILE* out, int parallel);
static int memory_decode_file(FILE* in, FILE* out, int parallel);
static void usage(FILE* out)
{
fputs("Usage: huffcode [-i<input file>] [-o<output file>] [-d|-c] [-p]\n"
"-i - input file (default is standard input)\n"
"-o - output file (default is standard output)\n"
"-d - decompress\n"
"-c - compress (default)\n"
"-m - read file into memory, compress, then write to file (not default)\n"
#ifdef USE_PARALLEL
"-p - use parallel version\n"
#endif
,
out);
}
int main(int argc, char** argv)
{
char memory = 0;
char compress = 1;
int parallel = 0;
int opt;
const char *file_in = NULL, *file_out = NULL;
FILE* in = stdin;
FILE* out = stdout;
int close_in = 0;
int close_out = 0;
int rc = 0;
/* Get the command line arguments. */
while ((opt = getopt(argc,
argv,
"i:o:cdhvm"
#ifdef USE_PARALLEL
"p"
#endif
)) != -1)
{
switch (opt)
{
case 'i':
file_in = optarg;
break;
case 'o':
file_out = optarg;
break;
case 'c':
compress = 1;
break;
case 'd':
compress = 0;
break;
case 'h':
usage(stdout);
return 0;
case 'm':
memory = 1;
break;
#ifdef USE_PARALLEL
case 'p':
parallel = 1;
break;
#endif
default:
usage(stderr);
return 1;
}
}
/* If an input file is given then open it. */
if (file_in)
{
in = fopen(file_in, "rb");
if (!in)
{
fprintf(
stderr, "Can't open input file '%s': %s\n", file_in, strerror(errno));
return 1;
}
close_in = 1;
}
/* If an output file is given then create it. */
if (file_out)
{
out = fopen(file_out, "wb");
if (!out)
{
fprintf(
stderr, "Can't open output file '%s': %s\n", file_out, strerror(errno));
return 1;
}
close_out = 1;
}
if (memory)
{
return compress ? memory_encode_file(in, out, parallel)
: memory_decode_file(in, out, parallel);
}
if (compress)
{
#ifdef USE_PARALLEL
if (parallel)
rc = huffman_encode_file_parallel(in, out);
else
#endif
rc = huffman_encode_file(in, out);
}
else
{
#ifdef USE_PARALLEL
if (parallel)
rc = huffman_decode_file_parallel(in, out);
else
#endif
rc = huffman_decode_file(in, out);
}
/* Since exit is about to happen, the fclose calls aren't necessary, but they make valgrind
* happy. */
if (close_in)
{
fclose(in);
}
if (close_out)
{
fclose(out);
}
return rc;
}
static int memory_encode_file(FILE* in, FILE* out, int parallel)
{
(void)parallel; // Suppress unused parameter warning when not using parallel
unsigned char *buf = NULL, *bufout = NULL;
uint32_t len = 0, cur = 0, inc = 1024, bufoutlen = 0;
assert(in && out);
/* Read the file into memory. Use fread return value to detect EOF/errors. */
while (1)
{
size_t got;
unsigned char* tmp;
if (cur + inc > len)
{
len += inc;
tmp = (unsigned char*)realloc(buf, len);
if (!tmp)
{
if (buf)
free(buf);
return 1;
}
buf = tmp;
}
got = fread(buf + cur, 1, inc, in);
cur += (unsigned int)got;
if (got < inc)
break; /* EOF or short read */
}
if (!buf)
return 1;
/* Encode the memory. */
#ifdef USE_PARALLEL
if (parallel)
{
if (huffman_encode_memory_parallel(buf, cur, &bufout, &bufoutlen))
{
free(buf);
return 1;
}
}
else
#endif
{
if (huffman_encode_memory(buf, cur, &bufout, &bufoutlen))
{
free(buf);
return 1;
}
}
free(buf);
/* Write the memory to the file. */
if (fwrite(bufout, 1, bufoutlen, out) != bufoutlen)
{
free(bufout);
return 1;
}
free(bufout);
return 0;
}
static int memory_decode_file(FILE* in, FILE* out, int parallel)
{
(void)parallel; // Suppress unused parameter warning when not using parallel
unsigned char *buf = NULL, *bufout = NULL;
uint32_t len = 0, cur = 0, inc = 1024, bufoutlen = 0;
assert(in && out);
/* Read the file into memory. Use fread return value to detect EOF/errors. */
while (1)
{
size_t got;
unsigned char* tmp;
if (cur + inc > len)
{
len += inc;
tmp = (unsigned char*)realloc(buf, len);
if (!tmp)
{
if (buf)
free(buf);
return 1;
}
buf = tmp;
}
got = fread(buf + cur, 1, inc, in);
cur += (unsigned int)got;
if (got < inc)
break; /* EOF or short read */
}
if (!buf)
return 1;
/* Decode the memory. */
#ifdef USE_PARALLEL
if (parallel)
{
if (huffman_decode_memory_parallel(buf, cur, &bufout, &bufoutlen))
{
free(buf);
return 1;
}
}
else
#endif
{
if (huffman_decode_memory(buf, cur, &bufout, &bufoutlen))
{
free(buf);
return 1;
}
}
free(buf);
/* Write the memory to the file. */
if (fwrite(bufout, 1, bufoutlen, out) != bufoutlen)
{
free(bufout);
return 1;
}
free(bufout);
return 0;
}