-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.c
186 lines (161 loc) · 5 KB
/
encode.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
#include "code.h"
#include "defines.h"
#include "header.h"
#include "huffman.h"
#include "io.h"
#include "node.h"
#include "pq.h"
#include "variables.h"
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#define OPTIONS "hvi:o:"
struct stat statbuf;
static inline void write_header(int outfile, Header *h) {
write_bytes(outfile, (uint8_t *) h, sizeof(Header));
}
static inline void write_stats(int infile, Header header) {
fstat(infile, &statbuf);
double stat
= 100 * (1 - (((double) bytes_written - header.file_size) / (double) header.file_size));
fprintf(stderr, "Uncompressed file size: %" PRId64 " bytes\n", header.file_size);
fprintf(stderr, "Compressed file size: %" PRId64 " bytes\n", bytes_written - header.file_size);
fprintf(stderr, "Space saving: %0.2lf%c\n", stat, '%');
}
static inline int seek(int tmp, int infile) {
int file;
if (infile == STDIN_FILENO && tmp != 0) {
lseek(tmp, 0, SEEK_SET);
file = tmp;
} else {
lseek(infile, 0, SEEK_SET);
file = infile;
}
return file;
}
static inline Header create_header(int infile, int outfile) {
Header header;
fstat(infile, &statbuf);
fchmod(outfile, statbuf.st_mode);
header.magic = MAGIC;
header.permissions = statbuf.st_mode;
header.tree_size = (3 * symbols) - 1;
header.file_size = statbuf.st_size;
return header;
}
static inline void traverse(Node *root, int outfile) {
if (root) {
uint8_t l = 'L';
uint8_t i = 'I';
if (root->left == NULL && root->right == NULL) {
write_bytes(outfile, &l, 1);
write_bytes(outfile, &root->symbol, 1);
} else {
traverse(root->left, outfile);
traverse(root->right, outfile);
write_bytes(outfile, &i, 1);
}
}
}
static inline void read_file(int infile, uint64_t *hist, int fno) {
uint8_t buf[BLOCK];
int status = 0;
do {
status = read_bytes(infile, buf, sizeof(buf));
for (uint64_t i = 0; i < bytes_read; i++) {
hist[buf[i]] += 1;
}
if (infile == STDIN_FILENO && fno != 0) {
write_bytes(fno, buf, bytes_read);
}
} while (status != 0);
return;
}
uint8_t huffman(int infile, int outfile, bool verbose) {
uint64_t hist[ALPHABET] = { 0 };
hist[0]++;
hist[255]++;
int fno = 0;
off_t offset;
char file_name[255];
if (offset = lseek(infile, 0, SEEK_SET), offset == -1) {
time_t t;
time(&t);
sprintf(file_name, "/tmp/huffman.%s", ctime(&t));
fno = open(file_name, O_RDWR | O_CREAT | O_EXCL | O_TRUNC, 0600);
}
read_file(infile, hist, fno);
Node *root = build_tree(hist);
Code table[ALPHABET] = { 0 };
build_codes(root, table);
int file = seek(fno, infile);
Header header = create_header(file, outfile);
write_header(outfile, &header);
traverse(root, outfile);
uint8_t buf[BLOCK];
int status = 0;
do {
status = read_bytes(file, buf, sizeof(buf));
for (uint64_t i = 0; i < bytes_read; i++) {
Code code = table[buf[i]];
write_code(outfile, &code);
}
memset(buf, 0, sizeof(buf));
} while (status != 0);
flush_codes(outfile);
if (verbose) {
write_stats(infile, header);
}
if (infile == STDIN_FILENO && fno != 0) {
unlink(file_name);
close(fno);
}
delete_tree(&root);
close(infile);
close(outfile);
return 0;
}
static inline void no_argument() {
fprintf(stderr, "SYNOPSIS\n A Huffman encoder.\n"
" Compresses a file using the Huffman coding algorithm."
"\n\nUSAGE\n ./encode [-h] [-i infile] [-o outfile]"
"\n\nOPTIONS\n -h Program usage and help.\n"
" -v Print compression statistics.\n"
" -i infile Input data to encode.\n"
" -o outfile Output of encoded data.\n");
return;
}
int main(int argc, char *argv[]) {
int infile = STDIN_FILENO, outfile = STDOUT_FILENO;
bool verbose = false;
int opt = 0;
while ((opt = getopt(argc, argv, OPTIONS)) != -1) {
switch (opt) {
case 'h': no_argument(); return 1;
case 'v': verbose = true; break;
case 'i':
if ((infile = open(optarg, O_RDONLY | O_CREAT)) == -1) {
fprintf(stderr, "Error: failed to open infile.\n");
return 1;
}
break;
case 'o':
if ((outfile = open(optarg, O_WRONLY | O_CREAT)) == -1) {
fprintf(stderr, "Error: Unable to write file\n");
return 1;
}
break;
case '?': no_argument(); return 1;
default: abort(); return 1;
}
}
return huffman(infile, outfile, verbose);
}