forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexe_reader.cpp
465 lines (401 loc) · 15.1 KB
/
exe_reader.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
// All of this code is unused on EMSCRIPTEN. *Do not use it*!
#ifndef EMSCRIPTEN
#include "exe_reader.h"
#include "image_bmp.h"
#include "output.h"
#include <algorithm>
#include <iostream>
#include <fstream>
#include <zlib.h>
namespace {
// hashes of known RPG_RT startup logos
std::array<uint32_t, 5> logo_crc32 = { 0xdf3d86a7, 0x2ece66f9, 0x2fe0de56, 0x25c4618f, 0x91b2635a };
}
EXEReader::EXEReader(Filesystem_Stream::InputStream core) : corefile(std::move(core)) {
// The Incredibly Dumb PE parser (tm)
// Extracts data from the resource section for engine detection and can read ExFont.
uint32_t ofs = GetU32(0x3C);
uint16_t machine = GetU16(ofs + 4);
if (machine != 0x14c) {
// FIXME: Newer versions of Maniac Patch can be 64 Bit
Output::Debug("EXEReader: Machine type is not i386 ({:#x})", machine);
file_info.is_i386 = false;
return;
}
// The largest known exe has 11 segments, guard against bogus section data here
uint16_t sections = std::min<uint16_t>(GetU16(ofs + 6), 11);
uint32_t sectionsOfs = ofs + 0x18 + GetU16(ofs + 0x14);
resource_rva = GetU32(ofs + 0x88);
if (!resource_rva) {
// Is some kind of encrypted EXE -> Give up
return;
}
while (sections) {
uint32_t secName = GetU32(sectionsOfs);
uint32_t sectVs = GetU32(sectionsOfs + 0x08);
uint32_t sectRs = GetU32(sectionsOfs + 0x10);
if (sectRs > sectVs) {
// Actually a problem in some files.
sectVs = sectRs;
}
if (secName == 0x45444F43) { // CODE
file_info.code_size = sectVs;
} else if (secName == 0x52454843) { // CHER(RY)
file_info.cherry_size = sectVs;
} else if (secName == 0x50454547) { // GEEP
file_info.geep_size = sectVs;
} else if (secName == 0x30585055) { // UPX0
Output::Debug("EXEReader: EXE is UPX compressed. Engine detection could be incorrect.");
}
uint32_t sectRva = GetU32(sectionsOfs + 0x0C);
uint32_t sectRdptr = GetU32(sectionsOfs + 0x14);
if (resource_ofs == 0 && (sectRva <= resource_rva) && ((sectRva + sectVs) > resource_rva)) {
// Resources located.
resource_ofs = sectRdptr + (resource_rva - sectRva);
}
sections--;
sectionsOfs += 0x28;
}
}
static std::vector<uint8_t> ExtractExFont(Filesystem_Stream::InputStream& corefile, uint32_t position, uint32_t len) {
std::vector<uint8_t> exfont;
constexpr int header_size = 14; // Size of BITMAPFILEHEADER
exfont.resize(len + header_size);
corefile.seekg(position, std::ios_base::beg);
corefile.read(reinterpret_cast<char*>(exfont.data()) + header_size, len);
if (corefile.gcount() != len) {
Output::Debug("EXEReader: ExFont: Error reading resource (read {}, expected {})", corefile.gcount(), len);
return {};
}
auto* exfont_data = reinterpret_cast<const uint8_t*>(exfont.data()) + header_size;
auto* e = exfont_data + len;
auto header = ImageBMP::ParseHeader(exfont_data, e);
// As it turns out, EXFONTs appear to operate on all the same restrictions as an ordinary BMP.
// Bitmap resources lack the BITMAPFILEHEADER. This header must be generated based on the BITMAPINFOHEADER.
// And the header that's going to be prepended.
int header_len = header_size + header.size;
if (header.depth != 8) {
Output::Debug("EXEReader: ExFont: Unsupported depth {}", header.depth);
return {};
}
header_len += header.num_colors * 4;
// 0 (these are in decimal)
int pos = 0;
exfont[pos++] = 'B';
exfont[pos++] = 'M';
// 2
uint32_t totallen = exfont.size();
exfont[pos++] = (totallen) & 0xFF;
exfont[pos++] = (totallen >> 8) & 0xFF;
exfont[pos++] = (totallen >> 16) & 0xFF;
exfont[pos++] = (totallen >> 24) & 0xFF;
// 6 - Reserved data
exfont[pos++] = 'E';
exfont[pos++] = 'x';
exfont[pos++] = 'F';
exfont[pos++] = 'n';
// 10
exfont[pos++] = (header_len) & 0xFF;
exfont[pos++] = (header_len >> 8) & 0xFF;
exfont[pos++] = (header_len >> 16) & 0xFF;
exfont[pos++] = (header_len >> 24) & 0xFF;
// Check if the ExFont is the original through a fast hash function
auto crc = crc32(0, exfont.data() + header_size, exfont.size() - header_size);
if (crc != 0x86bc6c68) {
Output::Debug("EXEReader: Custom ExFont found");
}
return exfont;
}
static uint32_t exe_reader_roffset(uint32_t bas, uint32_t ofs) {
return bas + (ofs ^ 0x80000000);
}
std::vector<uint8_t> EXEReader::GetExFont() {
corefile.clear();
auto bitmapDBase = ResOffsetByType(2);
if (bitmapDBase == 0) {
Output::Debug("EXEReader: BITMAP not found.");
return {};
}
// Looking for a named entry.
uint16_t resourcesNDEs = GetU16(bitmapDBase + 0x0C) + (uint32_t) GetU16(bitmapDBase + 0x0E);
uint32_t resourcesNDEbase = bitmapDBase + 0x10;
while (resourcesNDEs) {
uint32_t name = GetU32(resourcesNDEbase);
// Actually a name?
if (name & 0x80000000) {
name = exe_reader_roffset(resource_ofs, name);
if (ResNameCheck(name, "EXFONT")) {
uint32_t dataent = GetU32(resourcesNDEbase + 4);
if (dataent & 0x80000000) {
dataent = exe_reader_roffset(resource_ofs, dataent);
dataent = resource_ofs + GetU32(dataent + 0x14);
}
uint32_t filebase = (GetU32(dataent) - resource_rva) + resource_ofs;
uint32_t filesize = GetU32(dataent + 0x04);
Output::Debug("EXEReader: EXFONT resource found (DE {:#x}; {:#x}; len {:#x})", dataent, filebase, filesize);
return ExtractExFont(corefile, filebase, filesize);
}
}
resourcesNDEbase += 8;
resourcesNDEs--;
}
Output::Debug("EXEReader: EXFONT not found in dbase at {:#x}", bitmapDBase);
return {};
}
std::vector<std::vector<uint8_t>> EXEReader::GetLogos() {
corefile.clear();
if (!resource_ofs) {
return {};
}
if (Player::player_config.show_startup_logos.Get() == StartupLogos::None) {
return {};
}
std::vector<std::vector<uint8_t>> logos;
uint32_t resourcesIDEs = GetU16(resource_ofs + 0x0C);
if (resourcesIDEs == 1) {
uint32_t resourcesIDEbase = resource_ofs + 0x10;
if (ResNameCheck(exe_reader_roffset(resource_ofs, GetU32(resourcesIDEbase)), "XYZ")) {
uint32_t xyz_base = exe_reader_roffset(resource_ofs, GetU32(resourcesIDEbase + 4));
uint16_t xyz_logos = std::min<uint16_t>(GetU16(xyz_base + 0x0C), 9);
uint32_t xyz_logo_base = xyz_base + 0x10;
bool only_custom_logos = (Player::player_config.show_startup_logos.Get() == StartupLogos::Custom);
std::string res_name = "LOGOX";
for (int i = 0; i <= xyz_logos; ++i) {
uint32_t name = GetU32(xyz_logo_base);
// Actually a name?
if (name & 0x80000000) {
name = exe_reader_roffset(resource_ofs, name);
res_name.back() = '1' + i;
if (ResNameCheck(name, res_name.c_str()) || (i == 0 && ResNameCheck(name, "LOGO"))) {
uint32_t dataent = GetU32(xyz_logo_base + 4);
if (dataent & 0x80000000) {
dataent = exe_reader_roffset(resource_ofs, dataent);
dataent = resource_ofs + GetU32(dataent + 0x14);
}
uint32_t filebase = (GetU32(dataent) - resource_rva) + resource_ofs;
uint32_t filesize = GetU32(dataent + 0x04);
Output::Debug("EXEReader: {} resource found (DE {:#x}; {:#x}; len {:#x})", res_name, dataent, filebase, filesize);
std::vector<uint8_t> logo;
logo.resize(filesize);
corefile.seekg(filebase, std::ios_base::beg);
corefile.read(reinterpret_cast<char*>(logo.data()), filesize);
if (logo.size() < 8 || strncmp(reinterpret_cast<char*>(logo.data()), "XYZ1", 4) != 0) {
Output::Debug("EXEReader: {}: Not a XYZ image", res_name);
return {};
}
if (corefile.gcount() != filesize) {
Output::Debug("EXEReader: {}: Error reading resource (read {}, expected {})", res_name, corefile.gcount(), filesize);
return {};
}
if (only_custom_logos) {
auto crc = static_cast<uint32_t>(crc32(0, logo.data(), logo.size()));
if (std::find(logo_crc32.begin(), logo_crc32.end(), crc) == logo_crc32.end()) {
logos.push_back(logo);
}
} else {
logos.push_back(logo);
}
}
}
xyz_logo_base += 8;
}
}
}
return logos;
}
const EXEReader::FileInfo& EXEReader::GetFileInfo() {
corefile.clear();
file_info.logos = GetLogoCount();
auto versionDBase = ResOffsetByType(16);
if (versionDBase == 0) {
return file_info;
}
uint16_t resourcesNDEs = GetU16(versionDBase + 0x0C) + (uint32_t) GetU16(versionDBase + 0x0E);
uint32_t resourcesNDEbase = versionDBase + 0x10;
while (resourcesNDEs) {
uint32_t id = GetU32(resourcesNDEbase);
if (id == 1) {
id = exe_reader_roffset(resource_ofs, id);
uint32_t dataent = GetU32(resourcesNDEbase + 4);
if (dataent & 0x80000000) {
dataent = exe_reader_roffset(resource_ofs, dataent);
dataent = resource_ofs + GetU32(dataent + 0x14);
}
uint32_t filebase = (GetU32(dataent) - resource_rva) + resource_ofs;
uint32_t filesize = GetU32(dataent + 0x04);
std::vector<uint8_t> version_info(filesize);
corefile.seekg(filebase, std::ios_base::beg);
corefile.read(reinterpret_cast<char*>(version_info.data()), filesize);
// The start of VS_FIXEDFILEINFO structure is aligned on a 32 bit boundary
// Instead of calculating search for the signature
std::array<uint8_t, 4> signature = {0xBD, 0x04, 0xEF, 0xFE};
auto sig_it = std::search(version_info.begin(), version_info.end(), signature.begin(), signature.end());
if (sig_it != version_info.end()) {
uint32_t product_version_off = std::distance(version_info.begin(), sig_it + 16);
uint32_t version_high = GetU32(filebase + product_version_off);
uint32_t version_low = GetU32(filebase + product_version_off + 4);
file_info.version = (static_cast<uint64_t>(version_high) << 32) | version_low;
file_info.version_str = fmt::format("{}.{}.{}.{}", (version_high >> 16) & 0xFFFF, version_high & 0xFFFF, (version_low >> 16) & 0xFFFF, version_low & 0xFFFF);
}
std::array<uint8_t, 30> easyrpg_player_str = {
0x45, 0x00, 0x61, 0x00, 0x73, 0x00, 0x79, 0x00, 0x52, 0x00, 0x50, 0x00, 0x47, 0x00, 0x20, 0x00,
0x50, 0x00, 0x6C, 0x00, 0x61, 0x00, 0x79, 0x00, 0x65, 0x00, 0x72, 0x00, 0x00, 0x00
};
auto ep_it = std::search(version_info.begin(), version_info.end(), easyrpg_player_str.begin(), easyrpg_player_str.end());
file_info.is_easyrpg_player = ep_it != version_info.end();
Output::Debug("EXEReader: VERSIONINFO resource found (DE {:#x}; {:#x}; len {:#x})", dataent, filebase, filesize);
return file_info;
}
resourcesNDEbase += 8;
resourcesNDEs--;
}
Output::Debug("EXEReader: VERSIONINFO not found in dbase at {:#x}", versionDBase);
return file_info;
}
uint8_t EXEReader::GetU8(uint32_t i) {
corefile.seekg(i, std::ios_base::beg);
int ch = corefile.get();
if (ch == -1)
ch = 0;
return (uint8_t) ch;
}
uint16_t EXEReader::GetU16(uint32_t i) {
uint16_t v = GetU8(i);
v |= ((uint32_t) GetU8(i + 1)) << 8;
return v;
}
uint32_t EXEReader::GetU32(uint32_t i) {
uint32_t v = GetU16(i);
v |= ((uint32_t) GetU16(i + 2)) << 16;
return v;
}
uint32_t EXEReader::ResOffsetByType(uint32_t type) {
// Part 2 of the resource grabber.
if (!resource_ofs) {
return 0;
}
// For each ID/Name entry in the outer...
uint32_t resourcesIDEs = GetU16(resource_ofs + 0x0C) + (uint32_t) GetU16(resource_ofs + 0x0E);
uint32_t resourcesIDEbase = resource_ofs + 0x10;
while (resourcesIDEs) {
if (GetU32(resourcesIDEbase) == type) {
return exe_reader_roffset(resource_ofs, GetU32(resourcesIDEbase + 4));
}
resourcesIDEbase += 8;
resourcesIDEs--;
}
return 0;
}
uint32_t EXEReader::GetLogoCount() {
if (!resource_ofs) {
return 0;
}
uint32_t resourcesIDEs = GetU16(resource_ofs + 0x0C);
if (resourcesIDEs == 1) {
uint32_t resourcesIDEbase = resource_ofs + 0x10;
if (ResNameCheck(exe_reader_roffset(resource_ofs, GetU32(resourcesIDEbase)), "XYZ")) {
uint32_t xyz_logo_base = exe_reader_roffset(resource_ofs, GetU32(resourcesIDEbase + 4));
return static_cast<uint32_t>(GetU16(xyz_logo_base + 0x0C));
}
}
return 0;
}
bool EXEReader::ResNameCheck(uint32_t i, const char* p) {
if (GetU16(i) != strlen(p))
return false;
while (*p) {
i += 2;
if (GetU16(i) != *p)
return false;
p++;
}
return true;
}
void EXEReader::FileInfo::Print() const {
Output::Debug("RPG_RT information: version={} logos={} code={:#x} cherry={:#x} geep={:#x} i386={} easyrpg={}", version_str, logos, code_size, cherry_size, geep_size, is_i386, is_easyrpg_player);
}
int EXEReader::FileInfo::GetEngineType(bool& is_maniac_patch) const {
is_maniac_patch = false;
if (is_easyrpg_player || !is_i386) {
return Player::EngineNone;
}
if (version_str.empty()) {
// RPG2k and Rpg2k3 < 1.0.2.1 has no VERSIONINFO
if (logos == 3) {
// three logos only appear in old RPG2k
return Player::EngineRpg2k;
} else if (logos == 1) {
// VALUE! or Rpg2k3 < 1.0.2.1
// Check CODE segment size to be sure
if (code_size > 0xB0000) {
if (code_size >= 0xC7400) {
// Code segment size for >= 1.0.5.0
// In theory this check is unnecessary because this version has a VERSIONINFO.
// However the modified exe shipped with Ahriman's Prophecy is a 1.0.8.0 without a VERSIONINFO.
return Player::EngineRpg2k3 | Player::EngineMajorUpdated;
} else {
return Player::EngineRpg2k3;
}
}
return Player::EngineRpg2k | Player::EngineMajorUpdated;
}
return Player::EngineNone;
}
std::array<decltype(version), 4> ver = {
(version >> 48) & 0xFF,
(version >> 32) & 0xFF,
(version >> 16) & 0xFF,
(version & 0xFF)};
if (logos == 0) {
// New version of Maniac Patch is version 1.1.2.1 and is an rewrite of the engine
// Has no logos, no CODE segment and no CHERRY segment
if (ver[0] == 1 && ver[1] == 1 && ver[2] == 2 && ver[3] == 1 && code_size == 0 && cherry_size == 0) {
is_maniac_patch = true;
return Player::EngineRpg2k3 | Player::EngineMajorUpdated | Player::EngineEnglish;
}
return Player::EngineNone;
}
// Everything else with a VERSIONINFO must have one logo
if (logos != 1) {
return Player::EngineNone;
}
if (ver[0] == 1) {
if (ver[1] == 6) {
// English release of RPG Maker 2000 has a version info (thanks Cherry!)
return Player::EngineRpg2k | Player::EngineMajorUpdated | Player::EngineEnglish;
} else if (ver[1] == 0) {
// Everything else with a version info is RPG Maker 2003
if (ver[2] < 5) {
return Player::EngineRpg2k3;
} else {
return Player::EngineRpg2k3 | Player::EngineMajorUpdated;
}
} else if (ver[1] == 1) {
if (ver[2] == 2 && ver[3] == 1) {
// Old versions of Maniac Patch are a hacked 1.1.2.1
// The first versions have a GEEP segment (No idea what this abbreviation means)
// Later versions have no GEEP segment but an enlarged CHERRY segment
is_maniac_patch = (geep_size > 0 || cherry_size > 0x10000);
}
return Player::EngineRpg2k3 | Player::EngineMajorUpdated | Player::EngineEnglish;
}
}
return Player::EngineNone;
}
#endif