-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.c
More file actions
279 lines (249 loc) · 6.07 KB
/
decode.c
File metadata and controls
279 lines (249 loc) · 6.07 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
/* Huffman Decode Algorithm Implementation
*/
# include <sys/types.h> // open
# include <sys/stat.h> // open
# include <fcntl.h> // open
# include <unistd.h> // read
# include <getopt.h>
# include <string.h> // strncpy
# include "bv.h" // bit vectors
# include "stack.h"
# ifndef MAX_BUF
# define MAX_BUF 128
# endif
# ifndef MAGIC_NUM
# define MAGIC_NUM 0xdeadd00d
# endif
# ifndef BITS
# define BITS 8
# endif
# ifndef EMPTY
# define EMPTY(str) (str[0] == '\0')
# endif
uint64_t readSFile(char *file, uint16_t *leaves, treeNode **h, bitV **b);
treeNode *loadTree(uint8_t savedTree[], uint16_t treeBytes);
void writeOFile(char oFile[MAX_BUF], uint64_t oFileBytes, treeNode *r, bitV *v);
void decodeSymbols(uint8_t sym[], uint64_t oFileBytes, treeNode *r, bitV *v);
void printStatistics(uint64_t oFileBits, uint16_t leaves);
int main(int argc, char **argv)
{
char in[MAX_BUF] = {'\0'}; // set input path is invalid by default
char out[MAX_BUF] = {'\0'}; // set output path is invalid by default
bool verbose = false;
bool pFlag = false;
int opt;
while ((opt = getopt(argc, argv, "i:o:vp")) != -1)
{
switch (opt)
{
case 'i':
{
strncpy(in, optarg, sizeof(char) * MAX_BUF);
// strncpy does not null-term strings if the buf is maxed in size
if (in[MAX_BUF - 1] != '\0')
{
in[MAX_BUF - 1] = '\0';
}
break;
}
case 'o':
{
strncpy(out, optarg, sizeof(char) * MAX_BUF);
// strncpy does not null-term strings if the buf is maxed in size
if (out[MAX_BUF - 1] != '\0')
{
out[MAX_BUF - 1] = '\0';
}
break;
}
case 'v':
{
verbose = true;
break;
}
case 'p':
{
pFlag = true;
break;
}
case '?':
{
break;
}
default:
{
break;
}
}
}
while (EMPTY(in))
{
printf("Enter an input file path: ");
scanf("%s", in);
}
uint16_t leafCount;
treeNode *huf;
bitV *bits;
uint64_t oFileSize = readSFile(in, &leafCount, &huf, &bits);
writeOFile(out, oFileSize, huf, bits);
if (verbose)
{
printStatistics(oFileSize * BITS, leafCount);
}
if (pFlag)
{
printTree(huf, 0);
}
delVec(bits);
delTree(huf);
return 0;
}
/* readSFile:
*
* Scans a file to gather information about:
* magicNumber Was this file properly encoded?
* oFileBytes Size of the original, uncompressed file
* treeSize How big is the Huffman Tree (bytes)?
* savedTree How was the tree structured? (then reconstructs it)
* v What are the bit paths being traversed in the tree to decode this file?
*/
uint64_t readSFile(char *file, uint16_t *leaves, treeNode **h, bitV **b)
{
int fd = open(file, O_RDONLY);
if (fd == -1)
{
perror("Cannot open input file");
return 0;
}
uint32_t magicNumber;
read(fd, &magicNumber, sizeof(magicNumber));
if (magicNumber != MAGIC_NUM)
{
printf("Wrong magic number. Exiting\n");
close(fd);
return 1;
}
uint64_t oFileBytes;
read(fd, &oFileBytes, sizeof(oFileBytes));
uint16_t treeSize;
read(fd, &treeSize, sizeof(treeSize));
*leaves = treeSize;
uint8_t savedTree[treeSize];
read(fd, savedTree, treeSize);
treeNode *t = loadTree(savedTree, treeSize);
*h = t;
struct stat buffer;
fstat(fd, &buffer);
bitV *v = newVec(buffer.st_size * BITS);
v->l = read(fd, v->v, buffer.st_size) * BITS;
*b = v;
close(fd);
return oFileBytes;
}
/* loadTree:
*
* Takes in an array of characters which represent a
* post-order traversal of the Huffman Tree (leaf, leaf, parent).
*
* An 'L' (leaf) is followed by the symbol of the node.
* An 'I' (interior node) has no following symbol.
*
* Since the array follows post-order traversal, an interior node is
* the parent of the two nodes before it. Therefore, we can reconstruct the tree
* by pushing leaf nodes onto a stack, popping them when encountering an interior node,
* and pushing a joined tree of the left and right trees back onto the stack.
*/
treeNode *loadTree(uint8_t savedTree[], uint16_t treeBytes)
{
stack *s = newStack(treeBytes);
uint64_t i = 0;
while (i < treeBytes)
{
if (savedTree[i] == 'L') // leaf
{
treeNode *n = newNode(savedTree[i + 1], 0, true);
push(s, *n);
delNode(n);
i += 2;
}
else // interior node
{
treeNode l, r;
pop(s, &r);
pop(s, &l);
treeNode *j = join(convert(l), convert(r));
push(s, *j);
delNode(j);
i += 1;
}
}
treeNode huf;
pop(s, &huf);
delStack(s);
return convert(huf);
}
/* writeOFile:
*
* Decodes an encoded file. Writes the decoded file.
*/
void writeOFile(char oFile[MAX_BUF], uint64_t oFileBytes, treeNode *r, bitV *v)
{
int fdOut;
if (!EMPTY(oFile))
{
fdOut = open(oFile, O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU | S_IRGRP | S_IROTH);
}
else
{
fdOut = STDIN_FILENO;
}
if (fdOut == -1)
{
perror("Cannot open output file");
return;
}
if (oFileBytes)
{
uint8_t *sym = (uint8_t *) calloc(oFileBytes, sizeof(uint8_t));
decodeSymbols(sym, oFileBytes, r, v);
write(fdOut, sym, oFileBytes);
free(sym);
sym = NIL;
}
close(fdOut);
}
/* decodeSymbols:
*
* Traverses a Huffman Tree by following the bit paths stored in a vector.
* If a leaf node is found, store it in a collection of bytes to be written to the output.
*
* Also keeps track of the furthest line feed to signal the end of the file by POSIX standards.
*
* For each bit
* Step through the tree following the bit's value.
* If a leaf is encountered, store it.
*
* Returns the number of symbols decoded.
*/
void decodeSymbols(uint8_t sym[], uint64_t oFileBytes, treeNode *r, bitV *v)
{
treeNode *c = r; // holds the current node after stepping through the tree. Start at the root.
uint64_t len = 0; // position to store the next symbol
uint64_t bit_i = 0; // position of the current bit
while (len < oFileBytes)
{
uint32_t val = ((v->v)[bit_i >> 3] & (0x1 << (bit_i % 8))) >> (bit_i % 8);
int32_t step = stepTree(r, &c, val);
if (step != -1) // leaf node
{
sym[len] = step;
len += 1;
}
bit_i += 1;
}
}
void printStatistics(uint64_t oFileBits, uint16_t leaves)
{
printf("Original %lu bits: ", oFileBits);
printf("leaves %u (%u bytes)\n", leaves, (3 * leaves) - 1);
}