-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbpf-recompress.cpp
330 lines (258 loc) · 9.81 KB
/
dbpf-recompress.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
323
324
325
326
327
328
329
330
#include "dbpf.h"
#include <fcntl.h>
#include <io.h>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool validatePackage(dbpf::Package& oldPackage, dbpf::Package& newPackage, fstream& oldFile, fstream& newFile, wstring displayPath, dbpf::Mode mode);
//trys to delete a file, fails silently
void tryDelete(wstring fileName) {
try { filesystem::remove(fileName); }
catch(filesystem::filesystem_error) {}
}
//using wide chars and wide strings to support UTF-16 file names
int wmain(int argc, wchar_t *argv[]) {
_setmode(_fileno(stdout), _O_U16TEXT); //fix for wcout
if(argc == 1) {
wcout << L"No arguments provided" << endl;
return 0;
}
//parse args
wstring arg = argv[1];
if(arg == L"help") {
wcout << L"dbpf-recompress.exe -args package_file_or_folder" << endl;
wcout << L" -d decompress" << endl;
wcout << endl;
return 0;
}
dbpf::Mode default_mode = dbpf::RECOMPRESS;
int fileArgIndex = 1;
if(arg == L"-d") {
default_mode = dbpf::DECOMPRESS;
fileArgIndex = 2;
}
if(fileArgIndex > argc - 1) {
wcout << L"No file path provided" << endl;
return 0;
}
wstring pathName = argv[fileArgIndex];
auto files = vector<filesystem::directory_entry>();
bool is_dir = false;
if(filesystem::is_regular_file(pathName)) {
auto file_entry = filesystem::directory_entry(pathName);
if(file_entry.path().extension() != ".package") {
wcout << L"Not a package file" << endl;
return 0;
}
files.push_back(file_entry);
} else if(filesystem::is_directory(pathName)) {
is_dir = true;
for(auto& dir_entry: filesystem::recursive_directory_iterator(pathName)) {
if(dir_entry.is_regular_file() && dir_entry.path().extension() == ".package") {
files.push_back(dir_entry);
}
}
} else {
wcout << L"File not found" << endl;
return 0;
}
for(auto& dir_entry: files) {
auto mode = default_mode;
//open file
wstring fileName = dir_entry.path().wstring();
wstring tempFileName = fileName + L".new";
float current_size = dir_entry.file_size() / 1024.0;
wstring displayPath; //for cout
if(is_dir) {
displayPath = filesystem::relative(fileName, pathName).wstring();
} else {
displayPath = fileName;
}
fstream file = fstream(fileName, ios::in | ios::binary);
if(!file.is_open()) {
wcout << displayPath << L": Failed to open file" << endl;
continue;
}
//get package
dbpf::Package package = dbpf::getPackage(file, displayPath, mode);
dbpf::Package oldPackage = package; //copy
//optimization: if the package file has the compressor's signature then skip it
if(mode == dbpf::RECOMPRESS && package.signature_in_package) {
mode = dbpf::SKIP;
file.close();
}
//error unpacking package, getPackage already prints an error so there is no need to print one here
if(!package.unpacked) {
file.close();
continue;
}
//optimization: for DECOMPRESS mode skip the package file if all of it's entries are decompressed
if(mode == dbpf::DECOMPRESS) {
bool all_entries_decompressed = true;
for(auto& entry: package.entries) {
if(entry.compressed) {
all_entries_decompressed = false;
break;
}
}
if(all_entries_decompressed) {
mode = dbpf::SKIP;
file.close();
}
}
if(mode != dbpf::SKIP) {
//compress entries, pack package, and write to temp file
fstream tempFile = fstream(tempFileName, ios::in | ios::out | ios::binary | ios::trunc);
if(tempFile.is_open()) {
dbpf::putPackage(tempFile, file, package, mode);
} else {
wcout << displayPath << L": Failed to create temp file" << endl;
file.close();
continue;
}
//validate new file
tempFile.seekg(0, ios::beg);
dbpf::Package newPackage = dbpf::getPackage(tempFile, tempFileName, mode);
bool is_valid = validatePackage(oldPackage, newPackage, file, tempFile, displayPath, mode);
file.close();
tempFile.close();
if(!is_valid) {
tryDelete(tempFileName);
continue;
}
float new_size = filesystem::file_size(tempFileName) / 1024.0;
//overwrite old file
try {
filesystem::rename(tempFileName, fileName);
}
catch(filesystem::filesystem_error) {
wcout << displayPath << L": Failed to overwrite file" << endl;
tryDelete(tempFileName);
continue;
}
}
float new_size = filesystem::file_size(fileName) / 1024.0;
//output file size to console
wcout << displayPath << L" " << fixed << setprecision(2);
if(current_size >= 1000) {
wcout << current_size / 1024.0 << L" MB";
} else {
wcout << current_size << L" KB";
}
wcout << " -> ";
if(new_size >= 1000) {
wcout << new_size / 1024.0 << L" MB";
} else {
wcout << new_size << L" KB";
}
wcout << endl;
}
wcout << endl;
return 0;
}
//checks if the new package file is valid
bool validatePackage(dbpf::Package& oldPackage, dbpf::Package& newPackage, fstream& oldFile, fstream& newFile, wstring displayPath, dbpf::Mode mode) {
//package unpacking failed, getPackage already prints an error
if(!newPackage.unpacked) {
return false;
}
//compare headers
bytes oldHeader = dbpf::readFile(oldFile, 0, 96);
bytes newHeader = dbpf::readFile(newFile, 0, 96);
if(bytes(oldHeader.begin(), oldHeader.begin() + 36) != bytes(newHeader.begin(), newHeader.begin() + 36)
|| bytes(oldHeader.begin() + 60, oldHeader.end()) != bytes(newHeader.begin() + 60, newHeader.end())) {
wcout << displayPath << L": New header does not match the old header" << endl;
return false;
}
if(mode == dbpf::RECOMPRESS) {
//should only have one hole for the compressor signature
if(newPackage.header.holeIndexEntryCount != 1) {
wcout << displayPath << L": Wrong hole index count" << endl;
return false;
}
//one hole index entry is 8 bytes long
if(newPackage.header.holeIndexSize != 8) {
wcout << displayPath << L": Wrong hole index size" << endl;
return false;
}
dbpf::Hole hole = newPackage.holes[0];
//compressor signature is 8 bytes long
if(hole.size != 8) {
wcout << L": Wrong hole size" << endl;
return false;
}
bytes holeData = dbpf::readFile(newFile, hole.location, 8);
uint pos = 0;
uint sig = dbpf::getInt(holeData, pos);
//if the file was compressed then the signature should be "BRG5"
if(sig != dbpf::SIGNATURE) {
wcout << displayPath << L": Compressor signature not found" << endl;
return false;
}
uint fileSizeInHole = dbpf::getInt(holeData, pos);
uint fileSize = dbpf::getFileSize(newFile);
//file size written in the hole should match the actual file size
if(fileSizeInHole != fileSize) {
wcout << displayPath << L": File size in signature does not match the actual file size" << endl;
return false;
}
}
//should have the exact number of entries as the original package
//NOTE: getPackage does not include the directory of compressed files entry in the entries vector for both packages
if(oldPackage.entries.size() != newPackage.entries.size()) {
wcout << displayPath << L": Number of entries between old package and new package not matching" << endl;
return false;
}
//compare entries
for(uint i = 0; i < oldPackage.entries.size(); i++) {
auto& oldEntry = oldPackage.entries[i];
auto& newEntry = newPackage.entries[i];
//compare TGIRs
if(oldEntry.type != newEntry.type || oldEntry.group != newEntry.group || oldEntry.instance != newEntry.instance || oldEntry.resource != newEntry.resource) {
wcout << displayPath << L": Types, groups, instances, or resources of entries not matching" << endl;
return false;
}
//check entry content
bytes oldContent = dbpf::readFile(oldFile, oldEntry.location, oldEntry.size);
bytes newContent = dbpf::readFile(newFile, newEntry.location, newEntry.size);
//compression info in the directory of compressed files should match the information in the compression header
bool compressed_in_header = newContent.size() >= 9 && newContent[4] == 0x10 && newContent[5] == 0xFB;
auto iter = newPackage.compressedEntries.find(dbpf::CompressedEntry{newEntry.type, newEntry.group, newEntry.instance, newEntry.resource});
bool in_clst = iter != newPackage.compressedEntries.end();
if(compressed_in_header != in_clst) {
wcout << displayPath << L": Incorrect compression information" << endl;
return false;
}
if(newEntry.compressed) {
uint tempPos = 0;
uint uncompressedSize = dbpf::getUncompressedSize(newContent);
uint compressedSize = dbpf::getInt(newContent, tempPos);
if(uncompressedSize != iter->uncompressedSize) {
wcout << displayPath << L": Mismatch between the uncompressed size in the compression header and the uncompressed size in the CLST" << endl;
return false;
}
if(compressedSize != newEntry.size) {
wcout << displayPath << L": Mismatch between the compressed size in the compression header and the compressed size in the index" << endl;
return false;
}
//the compressor should only produce compressed entries that are smaller than the original decompressed entries
if(compressedSize > uncompressedSize) {
wcout << displayPath << L": Compressed size is larger than the uncompressed size for one entry" << endl;
return false;
}
}
//decompress the entries and compare them
oldContent = dbpf::decompressEntry(oldEntry, oldContent);
newContent = dbpf::decompressEntry(newEntry, newContent);
if(oldContent != newContent) {
wcout << displayPath << L": Mismatch between old entry and new entry" << endl;
return false;
}
}
//if all passes then return true
return true;
}