-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract.cpp
275 lines (209 loc) · 8.23 KB
/
extract.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
#include <filesystem>
#include <iostream>
#include <regex>
#include <cstring>
#include "utils.hpp"
#include "ooz.hpp"
#include "mmap/mmap.hpp"
// Extract file from memory
void extractFile(const MemoryMappedFile *memoryMappedFile, const std::string &name, const std::string &outPath, size_t offset, size_t size, size_t zSize, size_t compressionMode)
{
std::cout << "Extracting " << name << "...\n";
// Create out directory
auto filePath = fs::path(outPath + name).make_preferred();
mkpath(filePath, outPath.length());
if (mkpath(filePath, outPath.length()) != 0)
throwError("Failed to create " + filePath.parent_path().string() + " path for extraction: " + strerror(errno));
if (fs::is_directory(filePath)) {
filePath += " (1)";
}
if (size == 0) {
// Create empty file and continue
#ifdef _WIN32
FILE *exportFile = _wfopen(filePath.c_str(), L"wb");
#else
FILE *exportFile = fopen(filePath.c_str(), "wb");
#endif
fclose(exportFile);
}
else if (size == zSize) {
// File is decompressed, extract as-is
#ifdef _WIN32
MemoryMappedFile *outFile;
try {
outFile = new MemoryMappedFile(filePath, size, true, true);
}
catch (const std::exception &e) {
throwError("Failed to extract " + filePath.string() + " for reading.");
}
memcpy(outFile->memp, memoryMappedFile->memp + offset, size);
delete outFile;
#else
FILE *exportFile = fopen(filePath.c_str(), "wb");
if (exportFile == nullptr)
throwError("Failed to open " + filePath.string() + " for writing: " + strerror(errno));
fwrite(memoryMappedFile->memp + offset, 1, size, exportFile);
fclose(exportFile);
#endif
}
else {
// File is kraken-compressed, decompress with ooz
// Check oodle flags
if ((compressionMode & 4) != 0) {
offset += 12;
zSize -= 12;
}
// Decompress file
auto *decBytes = new(std::nothrow) unsigned char[size + SAFE_SPACE];
if (decBytes == nullptr)
throwError("Failed to allocate memory for extraction.");
if (Kraken_Decompress(memoryMappedFile->memp + offset, static_cast<int32_t>(zSize),
decBytes, size) != size)
throwError("Failed to decompress " + name + ".");
// Write file to disk
#ifdef _WIN32
MemoryMappedFile *outFile;
try {
outFile = new MemoryMappedFile(filePath, size, true, true);
}
catch (const std::exception &e) {
throwError("Failed to open " + filePath.string() + " for writing.");
}
memcpy(outFile->memp, decBytes, size);
delete outFile;
#else
FILE *exportFile = fopen(filePath.c_str(), "wb");
if (exportFile == nullptr)
throwError("Failed to open " + filePath.string() + " for writing: " + strerror(errno));
fwrite(decBytes, 1, size, exportFile);
fclose(exportFile);
#endif
delete[] decBytes;
}
}
// Check whether we should extract the file based on the include/exclude regexes
bool shouldExtractFile(const std::string &name, const std::vector<std::regex> ®exesToMatch, const std::vector<std::regex> ®exesNotToMatch)
{
bool extract = regexesToMatch.empty();
for (const auto ®ex : regexesToMatch) {
if (std::regex_match(name, regex)) {
extract = true;
break;
}
}
if (!extract) {
return false;
}
for (const auto ®ex : regexesNotToMatch) {
if (std::regex_match(name, regex)) {
extract = false;
break;
}
}
return extract;
}
// Extract all files from resources file
size_t extractResource(MemoryMappedFile *memoryMappedFile, const std::string &outPath, const std::vector<std::regex> ®exesToMatch, const std::vector<std::regex> ®exesNotToMatch)
{
// Read resource data
size_t memPosition = 4;
uint32_t version = memoryMappedFile->readUint32LE(memPosition);
if (version >= 0xD) {
memPosition = 36;
}
else {
memPosition = 32;
}
uint32_t fileCount = memoryMappedFile->readUint32LE(memPosition);
memPosition += 4;
uint32_t dummyCount = memoryMappedFile->readUint32LE(memPosition);
memPosition += 20;
// Get offsets
uint64_t namesOffset = memoryMappedFile->readUint64LE(memPosition);
memPosition += 8;
uint64_t infoOffset = memoryMappedFile->readUint64LE(memPosition);
memPosition += 8;
uint64_t dummyOffset = memoryMappedFile->readUint64LE(memPosition) + dummyCount * sizeof(dummyCount);
memPosition = namesOffset;
// Get filenames for exporting
uint64_t nameCount = memoryMappedFile->readUint64LE(memPosition);
std::vector<std::string> names;
names.reserve(nameCount);
size_t currentPosition = memPosition;
for (int i = 0; i < nameCount; i++) {
memPosition = currentPosition + static_cast<size_t>(i) * 8;
uint64_t currentNameOffset = memoryMappedFile->readUint64LE(memPosition);
memPosition = namesOffset + nameCount * 8 + currentNameOffset + 8;
std::string name(reinterpret_cast<char*>(memoryMappedFile->memp) + memPosition);
memPosition += name.length();
names.push_back(name);
}
memPosition = infoOffset;
// Extract files
size_t filesExtracted = 0;
for (int i = 0; i < fileCount; i++) {
memPosition += 32;
// Read file info for extracting
uint64_t nameIdOffset = memoryMappedFile->readUint64LE(memPosition);
memPosition += 16;
uint64_t offset = memoryMappedFile->readUint64LE(memPosition);
uint64_t zSize = memoryMappedFile->readUint64LE(memPosition);
uint64_t size = memoryMappedFile->readUint64LE(memPosition);
memPosition += 32;
uint64_t zipFlags = memoryMappedFile->readUint64LE(memPosition);
memPosition += 24;
nameIdOffset = (nameIdOffset + 1) * 8 + dummyOffset;
currentPosition = memPosition;
memPosition = nameIdOffset;
uint64_t nameId = memoryMappedFile->readUint64LE(memPosition);
std::string name = names[nameId];
// Match filename with regexes
if (!shouldExtractFile(name, regexesToMatch, regexesNotToMatch)) {
memPosition = currentPosition;
continue;
}
// Extract file
extractFile(memoryMappedFile, name, outPath, offset, size, zSize, zipFlags);
filesExtracted++;
// Seek back to info section
memPosition = currentPosition;
}
return filesExtracted;
}
// Extract all files from WAD7 file
size_t extractWad7(MemoryMappedFile *memoryMappedFile, const std::string &outPath, const std::vector<std::regex> ®exesToMatch, const std::vector<std::regex> ®exesNotToMatch)
{
size_t memPosition = 19;
// Get index position and size
uint64_t indexStart = memoryMappedFile->readUint64BE(memPosition);
uint64_t indexSize = memoryMappedFile->readUint64BE(memPosition);
memPosition = indexStart;
// Get entry count
uint32_t entryCount = memoryMappedFile->readUint32BE(memPosition);
// Extract files
size_t filesExtracted = 0;
for (int i = 0; i < entryCount; i++) {
// Get entry name
uint32_t nameSize = memoryMappedFile->readUint32LE(memPosition);
std::string name(reinterpret_cast<char*>(memoryMappedFile->memp) + memPosition, nameSize);
memPosition += nameSize;
// Match filename with regexes
if (!shouldExtractFile(name, regexesToMatch, regexesNotToMatch)) {
memPosition += 32;
continue;
}
// Get data offset
uint64_t offset = memoryMappedFile->readUint64BE(memPosition);
// Get uncompressed size
uint32_t size = memoryMappedFile->readUint32BE(memPosition);
// Get compressed size
uint32_t zSize = memoryMappedFile->readUint32BE(memPosition);
// Get compression mode
uint32_t compressionMode = memoryMappedFile->readUint32BE(memPosition);
// Extract file
extractFile(memoryMappedFile, name, outPath, offset, size, zSize, compressionMode);
filesExtracted++;
memPosition += 12;
}
return filesExtracted;
}