forked from smarttechnologies/peparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resourcetable.cpp
577 lines (451 loc) · 14.5 KB
/
resourcetable.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// Copyright (c) 2016 SMART Technologies. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include "resourcetable.h"
#include <iomanip>
namespace peparser
{
ResourceEntryPtr ResourceEntry::AtPath(const std::wstring& path) const
{
if (IsData() || Entries().size() == 0)
return ResourceEntryPtr();
size_t pos = path.find_first_of(L"/");
std::wstring current = path.substr(0, pos);
EntryList::const_iterator it = Entries().find(current);
if (it == Entries().end() || !it->second)
return ResourceEntryPtr();
if (pos == std::wstring::npos)
return it->second;
std::wstring remaining = path.substr(pos + 1);
return it->second->AtPath(remaining);
}
void ResourceEntry::PrintInfo(std::wostream& out, const std::wstring& prefix) const
{
out << prefix << FullPath() << L'\n';
for (auto& entry : Entries())
{
if (!entry.second)
continue;
entry.second->PrintInfo(out, prefix);
}
}
std::wstring ResourceEntry::FullPath() const
{
if (Path().empty())
return Name();
return Path() + L"/" + Name();
}
void ResourceEntry::Save(std::ostream& out) const
{
if (!IsData())
return;
out.write((const char*)Address(), Size());
}
ResourceDirectoryTable::ResourceDirectoryTable(LPVOID fileBase, PEDirInfo<IMAGE_RESOURCE_DIRECTORY> base, BlockList& interesting)
: m_base(base)
, m_fileBase(fileBase)
, m_interesting(interesting)
, m_root(new ResourceEntry())
{
m_root->SetName(L"");
ParseDir(m_base[0], m_root);
}
void ResourceDirectoryTable::ParseDir(PIMAGE_RESOURCE_DIRECTORY entry, const ResourceEntryPtr& parent)
{
if (!entry)
return;
size_t count = entry->NumberOfIdEntries + entry->NumberOfNamedEntries;
++entry;
for (size_t i = 0; i < count; ++i)
{
PIMAGE_RESOURCE_DIRECTORY_ENTRY subEntry = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)entry + i;
ResourceEntryPtr directory(new ResourceEntry());
if (parent->Path().empty())
directory->SetPath(parent->Name());
else
directory->SetPath(parent->Path() + L"/" + parent->Name());
if (subEntry->NameIsString)
{
PIMAGE_RESOURCE_DIR_STRING_U name = (PIMAGE_RESOURCE_DIR_STRING_U)((LPBYTE)m_base[0] + subEntry->NameOffset);
directory->SetName(L"@" + std::wstring(name->NameString, name->Length));
}
else
{
directory->SetId(subEntry->Id);
directory->SetName(std::to_wstring(subEntry->Name));
}
if (subEntry->DataIsDirectory)
{
ParseDir((PIMAGE_RESOURCE_DIRECTORY)((LPBYTE)m_base[0] + subEntry->OffsetToDirectory), directory);
parent->AddChild(directory);
}
else
{
ParseData((PIMAGE_RESOURCE_DATA_ENTRY)((LPBYTE)m_base[0] + subEntry->OffsetToData), directory);
parent->AddChild(directory);
}
}
}
void ResourceDirectoryTable::ParseData(PIMAGE_RESOURCE_DATA_ENTRY entry, const ResourceEntryPtr& data)
{
if (!entry || !data)
return;
data->SetSize(entry->Size);
data->SetAddress((LPBYTE)(m_base[0]) + entry->OffsetToData - m_base.Rva());
data->SetFileOffset((size_t)data->Address() - (size_t)m_fileBase);
m_interesting.push_back(Block(L"Resource: " + data->FullPath(), data->FileOffset(), data->Size()));
}
// ================================================================================================
struct VersionInfoHeader
{
WORD length; // Length of the version resource
WORD valueLength; // Length of the value field for this block
WORD type; // type of information: 1==string, 0==binary
};
size_t Alignment(size_t size)
{
if (size % sizeof(DWORD) != 0)
return sizeof(WORD);
else
return 0;
}
LPVOID ShiftByWideString(LPVOID start, int max, size_t addToAlign, std::wstring* copy = NULL)
{
if (!start)
return NULL;
if (max == 0)
return start;
WCHAR* str = (WCHAR*)start;
size_t length = wcsnlen(str, max);
size_t size = sizeof(WCHAR) * (length + 1); // with \0
if (copy)
copy->assign(str, length + 1);
size += Alignment(addToAlign + size);
return ((LPBYTE)start) + size;
}
void WriteString(LPBYTE dest, const std::wstring& str, size_t addToAlign, size_t& shift, bool noAlign = false)
{
size_t size = str.size() * sizeof(wchar_t);
memcpy(dest + shift, str.data(), size);
shift += size;
if (!noAlign)
shift += Alignment(addToAlign + size);
}
// ================================================================================================
VS_Base::VS_Base(LPVOID data, size_t size)
{
m_rawData = data;
m_rawSize = size;
}
VS_Base::~VS_Base()
{
}
// ================================================================================================
StringTableValue::StringTableValue(WCHAR* data, size_t size, const std::wstring& key)
: originalData(data)
, originalSize(size * sizeof(wchar_t))
, key(key)
{
newValue.assign(originalData, size);
}
StringTable::StringTable(LPVOID data, size_t size)
: VS_Base(data, size)
{
VersionInfoHeader* header = (VersionInfoHeader*)data;
if (header->length != size)
return;
LPVOID entryOffset = header + 1;
entryOffset = ShiftByWideString(entryOffset, header->length, sizeof(VersionInfoHeader), &m_tableName);
while (entryOffset < (LPBYTE)data + header->length)
{
LPVOID currentOffset = entryOffset;
VersionInfoHeader* stringHeader = (VersionInfoHeader*)currentOffset;
if (stringHeader->length == 0)
break;
currentOffset = stringHeader + 1;
std::wstring key;
currentOffset = ShiftByWideString(currentOffset, stringHeader->length, sizeof(VersionInfoHeader), &key);
m_strings.push_back(StringTableValue((WCHAR*)currentOffset, stringHeader->valueLength, key));
entryOffset = (LPBYTE)entryOffset + stringHeader->length + Alignment(stringHeader->length);
}
m_wellFormed = true;
}
bool StringTable::OriginalValue(const std::wstring& key, void** data, size_t* size)
{
for (auto& value : m_strings)
{
if (value.key != key)
continue;
*data = value.originalData;
*size = value.originalSize;
return true;
}
return false;
}
std::wstring StringTable::Value(const std::wstring& key) const
{
for (auto& value : m_strings)
{
if (value.key != key)
continue;
return value.newValue;
}
return L"";
}
void StringTable::SetValue(const std::wstring& key, const std::wstring& newValue)
{
for (size_t i = 0; i < m_strings.size(); ++i)
{
if (m_strings[i].key != key)
continue;
m_strings[i].newValue = newValue + L'\0';
return;
}
m_strings.push_back(StringTableValue(key, newValue));
}
size_t StringTable::NewSize() const
{
if (!IsWellFormed())
return OriginalSize();
size_t newSize = 0;
newSize += sizeof(VersionInfoHeader);
newSize += m_tableName.size() * sizeof(wchar_t);
newSize += Alignment(sizeof(VersionInfoHeader) + m_tableName.size() * sizeof(wchar_t));
for (size_t i = 0; i < m_strings.size(); ++i)
{
newSize += sizeof(VersionInfoHeader);
newSize += m_strings[i].key.size() * sizeof(wchar_t);
newSize += Alignment(sizeof(VersionInfoHeader) + m_strings[i].key.size() * sizeof(wchar_t));
newSize += m_strings[i].newValue.size() * sizeof(wchar_t);
if (i == m_strings.size() - 1)
continue;
newSize += Alignment(m_strings[i].newValue.size() * sizeof(wchar_t));
}
return newSize;
}
std::shared_ptr<BYTE> StringTable::NewData()
{
WORD size = (WORD)NewSize();
std::shared_ptr<BYTE> data((LPBYTE)operator new(size));
memset(data.get(), 0, size);
if (!IsWellFormed())
memcpy(data.get(), OriginalData(), size);
else
{
size_t shift = 0;
VersionInfoHeader header;
header.length = size;
header.type = 1;
header.valueLength = 0;
memcpy(data.get() + shift, &header, sizeof(VersionInfoHeader));
shift += sizeof(VersionInfoHeader);
WriteString(data.get(), m_tableName, sizeof(VersionInfoHeader), shift);
for (size_t i = 0; i < m_strings.size(); ++i)
{
VersionInfoHeader header;
header.valueLength = (WORD)m_strings[i].newValue.size(); // in chars
header.length = (WORD)(sizeof(VersionInfoHeader) + header.valueLength * sizeof(wchar_t) + m_strings[i].key.size() * sizeof(wchar_t) + Alignment(sizeof(VersionInfoHeader) + m_strings[i].key.size() * sizeof(wchar_t)));
header.type = 1;
memcpy(data.get() + shift, &header, sizeof(VersionInfoHeader));
shift += sizeof(VersionInfoHeader);
WriteString(data.get(), m_strings[i].key, sizeof(VersionInfoHeader), shift);
WriteString(data.get(), m_strings[i].newValue, 0, shift, i == m_strings.size() - 1);
}
}
return data;
}
void StringTable::SetField(Field field, const std::wstring& value)
{
std::wstring key;
switch (field)
{
case FileDescription:
key = L"FileDescription";
break;
case FileVersion:
key = L"FileVersion";
break;
case InternalName:
key = L"InternalName";
break;
case LegalCopyright:
key = L"LegalCopyright";
break;
case OriginalFilename:
key = L"OriginalFilename";
break;
case ProductName:
key = L"ProductName";
break;
case ProductVersion:
key = L"ProductVersion";
break;
default:
return;
}
key.push_back('\0');
SetValue(key, value);
}
// ================================================================================================
StringFileInfo::StringFileInfo(LPVOID data, size_t size)
:VS_Base(data, size)
{
VersionInfoHeader* header = (VersionInfoHeader*)data;
if (header->length != size)
return;
LPVOID currentOffset = header + 1;
currentOffset = ShiftByWideString(currentOffset, header->length, sizeof(VersionInfoHeader), &m_key);
if (m_key.c_str() != std::wstring(L"StringFileInfo"))
return;
while (currentOffset < (LPBYTE)data + header->length)
{
VersionInfoHeader* stringTableHeader = (VersionInfoHeader*)currentOffset;
if (stringTableHeader->length == 0)
break;
m_strings.push_back(StringTablePtr(new StringTable(currentOffset, stringTableHeader->length)));
currentOffset = (LPBYTE)currentOffset + stringTableHeader->length + Alignment(stringTableHeader->length);
}
m_wellFormed = true;
}
size_t StringFileInfo::NewSize() const
{
if (!IsWellFormed())
return OriginalSize();
size_t newSize = 0;
newSize += sizeof(VersionInfoHeader);
newSize += m_key.size() * sizeof(wchar_t);
newSize += Alignment(sizeof(VersionInfoHeader) + m_key.size() * sizeof(wchar_t));
for (size_t i = 0; i < m_strings.size(); ++i)
{
size_t blockSize = m_strings[i]->NewSize();
newSize += blockSize;
if (i == m_strings.size() - 1)
continue;
newSize += Alignment(blockSize);
}
return newSize;
}
std::shared_ptr<BYTE> StringFileInfo::NewData()
{
size_t size = NewSize();
std::shared_ptr<BYTE> data((LPBYTE)operator new(size));
memset(data.get(), 0, size);
if (!IsWellFormed())
memcpy(data.get(), OriginalData(), size);
else
{
size_t shift = 0;
VersionInfoHeader header;
header.length = (WORD)size;
header.type = 1;
header.valueLength = 0;
memcpy(data.get() + shift, &header, sizeof(VersionInfoHeader));
shift += sizeof(VersionInfoHeader);
WriteString(data.get(), m_key, sizeof(VersionInfoHeader), shift);
for (size_t i = 0; i < m_strings.size(); ++i)
{
size_t nextSize = m_strings[i]->NewSize();
memcpy(data.get() + shift, m_strings[i]->NewData().get(), nextSize);
shift += nextSize;
if (i != m_strings.size() - 1)
shift += Alignment(nextSize);
}
}
return data;
}
void StringFileInfo::SetField(StringTable::Field field, const std::wstring& value)
{
for (auto& entry : m_strings)
entry->SetField(field, value);
}
// ================================================================================================
VS_VersionInfo::VS_VersionInfo(LPVOID data, size_t size)
:VS_Base(data, size)
{
VersionInfoHeader* info = (VersionInfoHeader*)data;
if (info->length != size)
return;
if (info->valueLength != sizeof(VS_FIXEDFILEINFO))
return;
VS_FIXEDFILEINFO* fixedInfo = (VS_FIXEDFILEINFO*)ShiftByWideString(info + 1, info->length, sizeof(VersionInfoHeader), &m_header);
if (fixedInfo->dwSignature != 0xfeef04bd)
return;
m_originalFixedFileInfo = fixedInfo;
memcpy(&m_fixedFileInfo, fixedInfo, sizeof(VS_FIXEDFILEINFO));
LPVOID currentOffset = fixedInfo + 1;
while (currentOffset < (LPBYTE)info + info->length)
{
VersionInfoHeader* header = (VersionInfoHeader*)currentOffset;
m_strings.push_back(StringFileInfoPtr(new StringFileInfo(currentOffset, header->length)));
currentOffset = (LPBYTE)currentOffset + header->length + Alignment(header->length);
}
m_wellFormed = true;
}
size_t VS_VersionInfo::NewSize() const
{
if (!IsWellFormed()) return OriginalSize();
size_t newSize = 0;
newSize += sizeof(VersionInfoHeader);
newSize += m_header.size() * sizeof(wchar_t);
newSize += Alignment(sizeof(VersionInfoHeader) + m_header.size() * sizeof(wchar_t));
newSize += sizeof(VS_FIXEDFILEINFO);
for (auto& entry : m_strings)
{
size_t blockSize = entry->NewSize();
newSize += blockSize + Alignment(blockSize);
}
return newSize;
}
std::shared_ptr<BYTE> VS_VersionInfo::NewData()
{
WORD size = (WORD)NewSize();
std::shared_ptr<BYTE> data((LPBYTE)operator new(size));
memset(data.get(), 0, size);
if (!IsWellFormed())
memcpy(data.get(), OriginalData(), size);
else
{
size_t shift = 0;
VersionInfoHeader header;
header.length = size;
header.type = 0;
header.valueLength = sizeof(VS_FIXEDFILEINFO);
memcpy(data.get() + shift, &header, sizeof(VersionInfoHeader));
shift += sizeof(VersionInfoHeader);
WriteString(data.get(), m_header, sizeof(VersionInfoHeader), shift);
memcpy(data.get() + shift, &m_fixedFileInfo, sizeof(VS_FIXEDFILEINFO));
shift += sizeof(VS_FIXEDFILEINFO);
for (auto& entry : m_strings)
{
size_t nextSize = entry->NewSize();
memcpy(data.get() + shift, entry->NewData().get(), nextSize);
shift += nextSize + Alignment(nextSize);
}
}
return data;
}
void VS_VersionInfo::SetField(StringTable::Field field, const VersionString& version)
{
DWORD ms = version.Major();
ms <<= 8 * sizeof(WORD);
ms += version.Minor();
DWORD ls = version.Build();
ls <<= 8 * sizeof(WORD);
ls += version.Patch();
if (field == StringTable::FileVersion)
{
m_fixedFileInfo.dwFileVersionMS = ms;
m_fixedFileInfo.dwFileVersionLS = ls;
}
else if (field == StringTable::ProductVersion)
{
m_fixedFileInfo.dwProductVersionMS = ms;
m_fixedFileInfo.dwProductVersionLS = ls;
}
SetField(StringTable::ProductVersion, version.FullFormattedForResources());
}
void VS_VersionInfo::SetField(StringTable::Field field, const std::wstring& value)
{
for (auto& entry : m_strings)
entry->SetField(field, value);
}
}