-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
251 lines (209 loc) · 5.46 KB
/
main.cpp
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
//Author: Ankudinov Alexander
//for working with large files
#define _FILE_OFFSET_BITS 64
#ifndef BUF_SIZE
#define BUF_SIZE 262144
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
#ifdef DEBUG
#define DBG(fmt, a...) fprintf(stderr, "In %s on line %d: " fmt "\n", __FUNCTION__, __LINE__, ## a)
#else
#define DBG(fmt, a...)
#endif
#define INFO(fmt, a...) fprintf(stderr, fmt, ## a)
using namespace std;
const char HEADER[] = "HFMN";
const short TSIZE = 256 * 2 - 1;
struct node_t {
short v;
unsigned long long k; // count in file
short l, r, p;
};
struct code_t {
short k;
char c[33];
};
code_t hcode[256];
node_t htree[TSIZE];
char buf[BUF_SIZE];
struct setcomp {
bool operator() (const short& a, const short& b) const
{
if (htree[a].k != htree[b].k)
return htree[a].k < htree[b].k;
else
return a < b;
}
};
set<short, setcomp> mintree;
void init_htree()
{
memset(htree, 0, sizeof(htree));
for (short i = 0; i < TSIZE; ++i)
htree[i].v = i;
htree[TSIZE - 1].p = TSIZE - 1;
}
inline void write_bit(char* &ptr, char &pos, const char & bit)
{
*ptr |= bit << pos;
++pos;
if (pos >= 8) {
pos = 0;
++ptr;
}
}
inline char read_bit(char* &ptr, char &pos)
{
char res = (*ptr & 1 << pos) >> pos;
++pos;
if (pos >= 8) {
pos = 0;
++ptr;
}
return res;
}
void hcode_rec(short v, char* &ptr, char &pos, short &k)
{
if (v == TSIZE - 1)
return;
short t = htree[v].p;
hcode_rec(htree[v].p, ptr, pos, k);
write_bit(ptr, pos, (htree[t].l == v) ? 0 : 1);
++k;
}
int compress(FILE *in, FILE *out)
{
off64_t fsize;
// counting bytes in file
INFO("Preparing... ");
while(!feof(in)) {
int c = fgetc(in);
++htree[c].k;
}
fsize = ftell(in);
rewind(in);
INFO("OK\n");
// building haffman tree
INFO("Building haffman tree... ");
for (short i = 0; i < 256; ++i)
mintree.insert(i);
for (short i = 256; i < TSIZE; ++i) {
node_t & t = htree[i];
t.l = *mintree.begin();
mintree.erase(mintree.begin());
t.r = *mintree.begin();
mintree.erase(mintree.begin());
t.k = htree[t.l].k + htree[t.r].k;
htree[t.l].p = htree[t.r].p = i;
mintree.insert(i);
}
INFO("OK\n");
// precalc haffman bit codes for each byte
for (short i = 0; i < 256; ++i) {
char *ptr = hcode[i].c;
char pos = 0;
short & k = hcode[i].k;
hcode_rec(i, ptr, pos, k);
}
// write header with size information and Haffman tree
fputs(HEADER, out);
fwrite(&fsize, sizeof(fsize), 1, out);
for (short i = 256; i < TSIZE; ++i) {
fwrite(&htree[i].l, sizeof(htree[i].l), 1, out);
fwrite(&htree[i].r, sizeof(htree[i].r), 1, out);
}
// reading input for second time and encoding file
// using buffer buf
INFO("Writing resulting archive... ");
char *buf_ptr = buf;
char buf_pos = 0;
while (!feof(in)) {
int c = fgetc(in);
char *ptr = hcode[c].c;
char pos = 0;
for (short i = 0; i < hcode[c].k; ++i) {
write_bit(buf_ptr, buf_pos, read_bit(ptr, pos));
if (buf_ptr >= buf + BUF_SIZE) {
fwrite(buf, sizeof(char), BUF_SIZE, out);
memset(buf, 0, sizeof(buf));
buf_ptr = buf;
}
}
}
if (buf_pos > 0) ++buf_ptr;
fwrite(buf, sizeof(char), buf_ptr - buf, out);
fclose(in);
fclose(out);
INFO("DONE\n");
}
/*TODO: add checks for eof, and other problems with archive*/
int extract(FILE *in, FILE *out)
{
off64_t fsize;
char tmp_s[5];
// reading first 4 bytes and checking them
memset(tmp_s, 0, sizeof(tmp_s));
fgets(tmp_s, 5, in);
if (strcmp(tmp_s, HEADER) != 0) {
INFO("Not a haffman archive!\n");
return -1;
}
// reading filesize and haffman table
fread(&fsize, sizeof(fsize), 1, in);
for (short i = 256; i < TSIZE; ++i) {
fread(&htree[i].l, sizeof(htree[i].l), 1, in);
fread(&htree[i].r, sizeof(htree[i].r), 1, in);
}
// set output buffering
setvbuf(out, NULL, _IOLBF, BUF_SIZE);
off64_t tsize = 0;
short v = TSIZE - 1;
while (tsize < fsize && !feof(in)) {
int count = fread(buf, sizeof(char), BUF_SIZE, in);
char *ptr = buf;
char pos = 0;
for (int i = 0; i < 8 * count; ++i) {
if (read_bit(ptr, pos) == 0)
v = htree[v].l;
else
v = htree[v].r;
if (v < 256) {
fputc(v, out); ++tsize;
if (tsize >= fsize) break;
v = TSIZE - 1;
}
}
}
fclose(in);
fclose(out);
}
void help(char *name)
{
printf("Usage: %s [-x] file.in file.out\n", name);
printf("file.in must exist\n", name);
exit(EXIT_FAILURE);
}
#define FOR(a,b) for( int (a) = 0; (a) < (b); ++ (a))
int main(int argc, char *argv[])
{
if (argc < 3 || argc > 4)
help(argv[0]);
if (argc == 4 && strcmp(argv[1], "-x") != 0 )
help(argv[0]);
FILE *in, *out;
in = fopen(argv[argc - 2], "rb");
out = fopen(argv[argc - 1], "wb");
if (in == NULL || out == NULL) {
printf("Can`t open files\n\n");
help(argv[0]);
}
init_htree();
if (argc == 3)
return compress(in, out);
else
return extract(in, out);
return 0;
}