-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.c
More file actions
167 lines (134 loc) · 3.58 KB
/
Copy pathdecode.c
File metadata and controls
167 lines (134 loc) · 3.58 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
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "code.h"
#include "defines.h"
#include "header.h"
#include "huffman.h"
#include "io.h"
#include "node.h"
#include "pq.h"
#include "stack.h"
static void print_usage() {
fprintf(stderr, "SYNOPSIS\n"
" A Huffman decoder.\n"
" Decompresses a file using the Huffman coding algorithm.\n"
"\n"
"USAGE\n"
" ./decode [-h] [-i infile] [-o outfile]\n"
"\n"
"OPTIONS\n"
" -h Program usage and help.\n"
" -v Print compression statistics.\n"
" -i infile Input file to decompress.\n"
" -o outfile Output of decompressed data.\n");
}
//Checks if the node is a leaf
static bool is_leaf(Node *n) {
if (n->left == NULL && n->right == NULL) {
return true;
}
return false;
}
int main(int argc, char **argv) {
int value;
char *in_file = "stdin";
char *out_file = "stdout";
bool statistics = false;
//Reading input
while ((value = getopt(argc, argv, "hvi:o:")) != -1) {
switch (value) {
case 'v':
statistics = true;
break;
case 'h':
// Print usage and program synopsis
print_usage();
return 0;
break;
case 'i':
in_file = (optarg);
break;
case 'o':
out_file = optarg;
break;
default:
// printf("invalid input");
print_usage();
return 1;
break;
}
}
int input;
int output;
//setting up and preparing files
if (strcmp(out_file, "stdout") == 0) {
output = 1;
} else {
output = open(out_file, O_WRONLY | O_TRUNC| O_CREAT);
}
if (strcmp(in_file, "stdin") == 0) {
input = 0;
} else {
input = open(in_file, O_RDONLY);
}
if(input == -1){
fprintf(stderr, "Input file does not exist\n");
return 1;
}
Header h;
//reading header from file
read_bytes(input, (uint8_t *)&h, sizeof(Header));
uint16_t nbytes = h.tree_size;
uint8_t tree[nbytes];
//reading tree from file and rebuilding it
read_bytes(input, tree, nbytes);
Node *root = rebuild_tree(nbytes, tree);
if (h.magic != MAGIC) {
fprintf(stderr,
"Invalid magic number.\n");
return 1;
}
//setting file permissions
chmod(out_file, h.permissions);
Node *current = root;
Code table[ALPHABET] = {code_init()};
build_codes(root, table);
uint8_t bit = 0;
uint64_t num_printed = 0;
//reading bits until the file reaches the correct size
while (num_printed < h.file_size) {
read_bit(input, &bit);
if (bit == 0) {
current = current->left;
}
if (bit == 1) {
current = current->right;
}
if (is_leaf(current)) {
write_bytes(output, ¤t->symbol, 1);
num_printed++;
current = root;
}
}
//print statistics if its enabled
if (statistics) {
double saving = 100 * (1 - ((float)bytes_read / ((float)bytes_written)));
fprintf(stderr,
"Compressed file size: %lu bytes\n"
"Decompressed file size: %lu bytes\n"
"Space saving: %.2f%%\n",
bytes_read, bytes_written, saving);
}
close(input);
close(output);
delete_tree(&root);
return 0;
}