-
Notifications
You must be signed in to change notification settings - Fork 0
/
archiver.cpp
324 lines (284 loc) · 8.74 KB
/
archiver.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "archiver.h"
#include <fstream>
#include <QApplication>
#include <QFile>
Archiver::Archiver(QProgressBar *progressBar) {
this->progressBar = progressBar;
}
const QChar SEPARATOR = '/';
const int BYTE_SIZE = 8;
std::map<unsigned char, int> mainMap;
std::map<unsigned char, QVector<bool>> table;
QVector<bool> code;
Node *root;
bool comp(Node *&a, Node *&b) { return a->k < b->k; }
QString Archiver::getPath(QString str) {
return str.left(str.lastIndexOf(SEPARATOR));
}
QString Archiver::cutPath(QString str) {
return str.right(str.size() - str.lastIndexOf(SEPARATOR) - 1);
}
QString Archiver::getExtension(QString str) {
return str.mid(str.lastIndexOf(".") + 1).toLower();
}
std::pair<QString, QString> Archiver::err(const char *title, const char *text) {
return std::make_pair(QString(title), QString(text));
}
void Archiver::encode(QString path) {
if (path.isEmpty()) {
throw err("Empty path", "Please select a file");
} else if (!QFile(path).exists()) {
throw err("No such file", "File does not exist");
} else if (getExtension(path) == "xxx") {
throw err("Error", "You can't encode '.xxx' files");
}
filePath = path;
encode();
}
void Archiver::decode(QString path) {
if (path.isEmpty()) {
throw err("Empty path", "Please select a file");
} else if (!QFile(path).exists()) {
throw err("No such file", "File does not exist");
} else if (getExtension(path) != "xxx") {
throw err("Error", "You can decode only '.xxx' files");
}
filePath = path;
decode();
}
void Archiver::reset() {
root->clear();
root = nullptr;
mainMap.clear();
table.clear();
code.clear();
}
void Archiver::buildTree() {
QVector<Node *> v;
std::map<unsigned char, int>::iterator k;
for (k = mainMap.begin(); k != mainMap.end(); ++k) {
Node *p = new Node();
p->c = k->first;
p->k = k->second;
p->b = true;
v.push_back(p);
}
if (v.size() == 1) {
Node *q = new Node();
q->l = new Node();
q->l->b = true;
q->l->c = v.front()->c;
q->l->k = v.front()->k;
root = q;
} else {
Node *tl, *tr;
while (v.size() != 1) {
std::sort(v.begin(), v.end(), comp);
tl = v.front();
v.pop_front();
tr = v.front();
v.pop_front();
Node *tmp = new Node();
tmp->l = tl;
tmp->r = tr;
tmp->k = tl->k + tr->k;
v.push_back(tmp);
}
root = v.front();
}
}
void Archiver::buildTable(Node *p) {
if (p->l != nullptr) {
code.push_back(0);
buildTable(p->l);
}
if (p->r != nullptr) {
code.push_back(1);
buildTable(p->r);
}
if (p->b) table[p->c] = code;
if (!code.isEmpty()) code.pop_back();
}
void Archiver::encode() {
reset();
char c;
std::ifstream fin(filePath.toStdString(), std::ios::binary);
while (!fin.eof()) {
fin.read(&c, sizeof(char));
++mainMap[c];
QApplication::processEvents();
}
--mainMap[c];
buildTree();
buildTable(root);
std::map<unsigned char, int>::iterator h;
QString newName(filePath + ".xxx");
std::ofstream fout(newName.toStdString(), std::ios::binary);
fout << (unsigned char)(cutPath(filePath).length());
fout << cutPath(filePath).toStdString();
fout << (unsigned char)(mainMap.size() >> BYTE_SIZE);
fout << (unsigned char)(mainMap.size());
std::map<unsigned char, int>::iterator it;
for (it = mainMap.begin(); it != mainMap.end(); ++it) {
unsigned char charSelf = it->first;
fout << charSelf;
fout << (unsigned char)(table[charSelf].size());
QVector<bool> currentByteCode = table[charSelf];
if (table[charSelf].size() % BYTE_SIZE) {
for (int i = 0; i < BYTE_SIZE - (table[charSelf].size() % BYTE_SIZE); ++i) {
currentByteCode.push_back(0);
}
}
unsigned char currentByte = 0;
int bufferCount = 0;
for (int i = 0; i < currentByteCode.size(); ++i) {
currentByte <<= 1;
if (currentByteCode[i]) {
currentByte |= 1;
}
++bufferCount;
if (bufferCount == BYTE_SIZE) {
bufferCount = 0;
fout << currentByte;
currentByte = 0;
}
}
}
long long rawDataSize = 0;
std::map<unsigned char, int>::iterator q;
for (q = mainMap.begin(); q != mainMap.end(); ++q) {
rawDataSize += q->second * table[q->first].size();
}
if (progressBar) progressBar->setMaximum(rawDataSize / BYTE_SIZE);
unsigned char currentByte;
for (int j = 64 - BYTE_SIZE; j >= 0; j -= BYTE_SIZE) {
currentByte = (unsigned char)(rawDataSize >> j);
fout << currentByte;
}
fin.clear();
fin.seekg(0, fin.beg);
c = 0;
int bufferCount = 0;
currentByte = 0;
while (!fin.eof()) {
if (progressBar) progressBar->setValue(progressBar->value() + 1);
fin.read(&c, sizeof(char));
if (fin.eof()) break;
QVector<bool> v = table[c];
for (int j = 0; j < v.size(); ++j) {
currentByte <<= 1;
if (v[j]) {
currentByte |= 1;
}
++bufferCount;
if (bufferCount == BYTE_SIZE) {
bufferCount = 0;
fout << currentByte;
currentByte = 0;
QCoreApplication::processEvents();
}
}
}
currentByte <<= (BYTE_SIZE - bufferCount);
fout << currentByte;
fout << rawDataSize;
fout.close();
fin.close();
}
void Archiver::decode() {
reset();
QApplication::processEvents();
std::ifstream fin(filePath.toStdString(), std::ios::binary);
char c;
fin.read(&c, sizeof(char));
int nameSize = int(c);
QString originalName = "";
for (int i = 0; i < nameSize; ++i) {
fin.read(&c, sizeof(char));
originalName += c;
}
std::ofstream fout((getPath(filePath) + SEPARATOR + originalName).toStdString(),
std::ios::binary);
fin.read(&c, sizeof(char));
int tableSize = ((int)c) << BYTE_SIZE;
fin.read(&c, sizeof(char));
tableSize |= c;
Node *localRoot = new Node();
Node *p;
for (int i = 0; i < tableSize; ++i) {
char charSelf;
fin.read(&charSelf, sizeof(char));
fin.read(&c, sizeof(char));
int charCodeSize = c;
int charCodeByteSize = charCodeSize / BYTE_SIZE + (charCodeSize % BYTE_SIZE ? 1 : 0);
std::vector<bool> charCode;
int cnt = 0;
for (int j = 0; j < charCodeByteSize; ++j) {
fin.read(&c, sizeof(char));
for (int k = 0; k < BYTE_SIZE; ++k) {
if (c & 128) {
charCode.push_back(1);
} else {
charCode.push_back(0);
}
c <<= 1;
cnt++;
if (cnt == charCodeSize) break;
}
}
p = localRoot;
for (int j = 0; j < charCodeSize; ++j) {
if (charCode[j]) {
if (p->r == nullptr) {
p->r = new Node;
}
p = p->r;
if (j == charCodeSize - 1) {
p->b = true;
p->c = charSelf;
}
} else {
if (p->l == nullptr) {
p->l = new Node;
}
p = p->l;
if (j == charCodeSize - 1) {
p->b = true;
p->c = charSelf;
}
}
}
charCode.clear();
}
long long rawDataSize = 0;
for (int j = BYTE_SIZE - 1; j >= 0; --j) {
fin.read(&c, sizeof(char));
long long tmpLong = (unsigned char)c;
rawDataSize |= tmpLong << (j * BYTE_SIZE);;
}
long long rawDataByteSize = (rawDataSize / BYTE_SIZE + (rawDataSize % BYTE_SIZE ? 1 : 0));
if (progressBar) progressBar->setMaximum(rawDataByteSize);
Node *cur = localRoot;
long long cnt = 0;
for (int j = 0; j < rawDataByteSize; ++j) {
if (progressBar) progressBar->setValue(progressBar->value() + 1);
fin.read(&c, sizeof(char));
for (int k = 0; k < BYTE_SIZE; ++k) {
if (c & 128) {
cur = cur->r;
} else {
cur = cur->l;
}
if (cur->b) {
fout << cur->c;
cur = localRoot;
QCoreApplication::processEvents();
}
c <<= 1;
++cnt;
if (cnt == rawDataSize) break;
}
}
fout.close();
fin.close();
}