-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryExtractor.cpp
More file actions
75 lines (66 loc) · 2.37 KB
/
binaryExtractor.cpp
File metadata and controls
75 lines (66 loc) · 2.37 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
#include "binaryExtractor.h"
BinaryExtractor::BinaryExtractor(const string& filename) : m_filename(filename) {}
void BinaryExtractor::extract() {
string folder = m_filename.substr(0, m_filename.find_last_of('.'));
mkDIR(folder);
ifstream input(m_filename, std::ios::binary);
if (!input.is_open()) {
cerr << "Error: unable to open file " << m_filename << std::endl;
return;
}
input.seekg(0, ios::end);
size_t file_size = input.tellg();
input.seekg(0, ios::beg);
while (input.tellg() < file_size) {
string name = readcstr(input);
string ftype = readcstr(input);
uint32_t c = readuint32(input);
uint32_t d = readuint32(input);
uint32_t e = readuint32(input);
vector<unsigned char> data(d);
input.read(reinterpret_cast<char*>(data.data()), d);
if (c & 1) {
data = decompress(data, e);
}
string output_filename = folder + "/" + name;
ofstream output(output_filename, ios::binary);
if (!output.is_open()) {
cerr << "Error: unable to create file " << output_filename << endl;
return;
}
output.write(reinterpret_cast<const char*>(data.data()), data.size());
output.close();
cout << name << " " << ftype << endl;
}
cout << "COMPLETED!!!" << endl;
}
void BinaryExtractor::mkDIR(const string& dir) {
#ifdef _WIN32
_mkdir(dir.c_str());
#else
mkdir(dir.c_str(), 0777);
#endif
}
vector.unsigned char> BinaryExtractor::decompress(const vector<unsigned char>& input, size_t output_size) {
vector<unsigned char> output(output_size);
unsigned long output_size_long = static_cast<unsigned long>(output_size);
unsigned long input_size_long = static_cast<unsigned long>(input.size());
int zret = uncompress(output.data(), &output_size_long, input.data(), input_size_long);
if (zret != Z_OK) {
throw runtime_error("Error: decompression failed");
}
return output;
}
uint32_t BinaryExtractor::readuint32(ifstream& input) {
uint32_t value;
input.read(reinterpret_cast<char*>(&value), sizeof(uint32_t));
return value;
}
string BinaryExtractor::readcstr(ifstream& input) {
vector<char> buffer;
char c;
while (input.read(&c, 1) && c != '\0') {
buffer.push_back(c);
}
return string(buffer.begin(), buffer.end());
}