-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.cpp
311 lines (255 loc) · 9.79 KB
/
archive.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
#include "archive.h"
#include "crypto.h"
#include "sha3.h"
#include <filesystem>
#include <fstream>
#include <random>
static std::string extension = ".dat";
static std::string password;
struct ARCHIVE_HEADER {
size_t file_num;
uint16_t pass_md;
bool is_encrypted;
bool is_directory;
};
struct FILE_HEADER {
size_t original_size;
size_t pressed_size;
size_t pointer;
size_t path_size;
};
static inline void XorBits(char* bits, size_t size)
{
std::mt19937 engine(size);
for (size_t i = 0; i < size; i++) {
bits[i] ^= engine() & 0xff;
}
}
static inline uint16_t GetPassMD() {
uint16_t md = 0; uint16_t hash[14];
SHA3_224(password.data(), password.size(), hash);
for (int i = 0; i < 14; i++) md ^= hash[i];
return md;
}
static size_t ReadHeader(std::ifstream& ifs, ARCHIVE_HEADER& header, std::vector<FILE_HEADER>& heads, std::vector<std::string>& paths)
{
ifs.seekg(0, std::ios_base::beg);
ifs.read((char*)&header, sizeof(ARCHIVE_HEADER));
XorBits((char*)&header, sizeof(ARCHIVE_HEADER));
if (header.pass_md != GetPassMD()) return 0;
heads.resize(header.file_num);
ifs.read((char*)heads.data(), (uint64_t)sizeof(FILE_HEADER) * header.file_num);
XorBits((char*)heads.data(), (uint64_t)sizeof(FILE_HEADER) * header.file_num);
paths.resize(header.file_num);
for (size_t i = 0; i < header.file_num; i++)
{
paths[i].resize(heads[i].path_size);
ifs.read(paths[i].data(), heads[i].path_size);
XorBits((char*)paths[i].data(), heads[i].path_size);
}
return (size_t)ifs.tellg();
}
static void WriteHeader(std::ofstream& ofs, ARCHIVE_HEADER& header, std::vector<FILE_HEADER>& heads, std::vector<std::string>& paths)
{
XorBits((char*)&header, sizeof(ARCHIVE_HEADER));
XorBits((char*)heads.data(), (uint64_t)sizeof(FILE_HEADER) * heads.size());
for (size_t i = 0; i < paths.size(); i++) XorBits(paths[i].data(), paths[i].size());
ofs.write((char*)&header, sizeof(ARCHIVE_HEADER));
ofs.write((char*)heads.data(), (uint64_t)sizeof(FILE_HEADER) * heads.size());
for (size_t i = 0; i < paths.size(); i++) ofs.write(paths[i].c_str(), paths[i].size());
}
void SetArchivePassword(const std::string& _pass)
{
password = _pass;
}
void SetArchiveExtension(const std::string& _extension)
{
extension = _extension;
}
bool GetFileList(std::string path, std::vector<std::string>& list)
{
for (const auto& file : std::filesystem::recursive_directory_iterator(path))
if (!file.is_directory()) list.insert(list.end(), file.path().string());
return true;
}
bool EncodeArchive(std::string path, int _compress_level, bool _encrypt)
{
const bool is_directory = std::filesystem::is_directory(path);
const auto cd = std::filesystem::current_path();
size_t pos = path.find_last_of('\\');
if (is_directory) {
std::filesystem::current_path(path);
std::filesystem::current_path("..");
}
else if (std::filesystem::exists(path)) {
if (pos != std::string::npos) std::filesystem::current_path(path.substr(0, pos));
}
else return false;
if (pos != std::string::npos) path = path.substr(pos + 1);
std::vector<std::string> paths;
if (is_directory) GetFileList(path, paths);
else paths.insert(paths.end(), path);
if (paths.size() == 0) return false;
if (is_directory) {
std::filesystem::current_path(path);
for (auto& t : paths) t = t.substr(path.size() + 1);
}
std::vector<FILE_HEADER> heads;
heads.resize(paths.size());
std::vector<uint8_t> data;
for (size_t i = 0; i < paths.size(); i++)
{
heads[i].pointer = data.size();
heads[i].path_size = paths[i].size();
heads[i].pressed_size = heads[i].original_size = (size_t)std::filesystem::file_size(paths[i]);
std::ifstream ifs;
ifs.open(paths[i], std::ios_base::in | std::ios_base::binary);
if (!ifs) return false;
uint8_t* original = new uint8_t[heads[i].original_size];
uint8_t* encoded;
ifs.read((char*)original, heads[i].original_size);
uint8_t hash[48], pass[48];
SHA3_384((uint8_t*)paths[i].c_str(), paths[i].size(), hash);
SHA3_384((uint8_t*)password.c_str(), password.size(), pass);
for (int i = 0; i < 48; i++) hash[i] ^= pass[i];
AesCtx ctx;
AesInitKey(&ctx, hash, 32);
heads[i].pressed_size = heads[i].original_size / 7 * 8 + 1024;
encoded = new uint8_t[heads[i].pressed_size];
compress2(encoded, (uLongf*)&heads[i].pressed_size, original, (uLongf)heads[i].original_size, _compress_level);
if (_encrypt) heads[i].pressed_size = AesEncryptCbc(&ctx, hash + 32, encoded, heads[i].pressed_size);
data.resize(data.size() + heads[i].pressed_size);
std::memcpy(data.data() + data.size() - heads[i].pressed_size, encoded, heads[i].pressed_size);
delete[] original; delete[] encoded;
ifs.close();
std::cout << paths[i] << std::endl;
std::cout << "oroginal size: " << heads[i].original_size << " Byte" << std::endl;
std::cout << "compressed size: " << heads[i].pressed_size << " Byte" << std::endl;
std::cout << "compression ratio: " << (float)heads[i].pressed_size / (float)heads[i].original_size * 100.0f << " %" << std::endl;
std::cout << std::endl;
}
ARCHIVE_HEADER header = { paths.size(), GetPassMD(), is_directory, _encrypt };
if (is_directory) std::filesystem::current_path("..");
std::ofstream ofs;
path += extension;
ofs.open(path, std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
if (!ofs) return false;
WriteHeader(ofs, header, heads, paths);
ofs.write((char*)data.data(), data.size());
ofs.close();
std::filesystem::current_path(cd);
return true;
}
bool CheckArchive(std::string path)
{
std::ifstream ifs;
ifs.open(path, std::ios_base::in | std::ios_base::binary);
if (!ifs) return false;
std::vector<FILE_HEADER> head;
std::vector<std::string> paths;
ARCHIVE_HEADER header;
size_t head_size = ReadHeader(ifs, header, head, paths);
if (head_size == 0) return false;
std::string first_dir;
size_t pos = path.find_first_of('\\');
if (header.is_directory && pos != std::string::npos) first_dir = path.substr(0, pos + 1);
for (size_t i = 0; i < head.size(); i++)
{
std::cout << first_dir + paths[i] << std::endl;
std::cout << "oroginal size: " << head[i].original_size << " Byte" << std::endl;
std::cout << "compressed size: " << head[i].pressed_size << " Byte" << std::endl;
std::cout << "compression ratio: " << (float)head[i].pressed_size / (float)head[i].original_size * 100.0f << " %" << std::endl;
std::cout << "pointer: " << head_size + head[i].pointer << std::endl;
std::cout << std::endl;
}
ifs.close();
return true;
}
size_t GetDataFromArchive(std::string path, void* dest, std::string archive_path)
{
size_t pos = path.find_first_of('\\');
if (pos != std::string::npos) archive_path = path.substr(0, pos) + extension;
else if (archive_path.empty()) archive_path = path + extension;
std::ifstream ifs;
ifs.open(archive_path, std::ios_base::in | std::ios_base::binary);
if (!ifs) return 0;
std::vector<FILE_HEADER> head;
std::vector<std::string> paths;
ARCHIVE_HEADER header;
size_t head_size = ReadHeader(ifs, header, head, paths);
if (head_size == 0) return false;
std::string first_dir;
if (header.is_directory) first_dir = path.substr(0, pos + 1);
size_t size = 0;
for (size_t i = 0; i < head.size(); i++)
{
if (path == first_dir + paths[i])
{
size = head[i].original_size;
if (!dest) return size;
ifs.seekg((uint64_t)head_size + head[i].pointer, std::ios_base::beg);
uint8_t* pressed = new uint8_t[head[i].pressed_size];
uint8_t* original = new uint8_t[head[i].original_size + 1024];
ifs.read((char*)pressed, head[i].pressed_size);
uint8_t hash[48], pass[48];
SHA3_384((uint8_t*)paths[i].c_str(), paths[i].size(), hash);
SHA3_384((uint8_t*)password.c_str(), password.size(), pass);
for (int i = 0; i < 48; i++) hash[i] ^= pass[i];
AesCtx ctx;
AesInitKey(&ctx, hash, 32);
if (header.is_encrypted) head[i].pressed_size = AesDecryptCbc(&ctx, hash + 32, pressed, head[i].pressed_size);
uncompress(original, (uLongf*)&head[i].original_size, pressed, (uLongf)head[i].pressed_size);
memcpy(dest, original, head[i].original_size);
delete[] pressed; delete[] original;
break;
}
}
ifs.close();
return size;
}
bool DecodeArchive(std::string path)
{
const auto cd = std::filesystem::current_path();
size_t pos = path.find_last_of('\\');
if (pos != std::string::npos) {
std::filesystem::current_path(path.substr(0, pos));
path = path.substr(pos + 1);
}
std::ifstream ifs;
ifs.open(path, std::ios_base::in | std::ios_base::binary);
if (!ifs) return false;
std::vector<FILE_HEADER> head;
std::vector<std::string> paths;
ARCHIVE_HEADER header;
size_t head_size = ReadHeader(ifs, header, head, paths);
if (head_size == 0) return false;
std::string first_dir;
if (header.is_directory)
{
first_dir = path.substr(0, path.size() - extension.size()) + "\\";
for (auto& p : paths) p = first_dir + p;
}
ifs.close();
for (size_t i = 0; i < head.size(); i++)
{
uint8_t* original = new uint8_t[head[i].original_size];
if (header.is_directory) GetDataFromArchive(paths[i], original);
else GetDataFromArchive(paths[i], original, path);
std::cout << paths[i] << std::endl;
std::cout << "size: " << head[i].original_size << " Byte" << std::endl;
std::cout << std::endl;
size_t pos = paths[i].find_last_of('\\');
if (pos != std::string::npos) {
std::string dir = paths[i].substr(0, pos);
if (!std::filesystem::exists(dir)) std::filesystem::create_directories(dir);
}
std::ofstream ofs;
ofs.open(paths[i], std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
if (!ofs) return false;
ofs.write((char*)original, head[i].original_size);
ofs.close();
delete[] original;
}
std::filesystem::current_path(cd);
return true;
}