forked from pavel-a/ddverpatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peExtras.cpp
100 lines (82 loc) · 2.15 KB
/
peExtras.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
//
// PE image related utils
//
#include "stdafx.h"
#include "peExtras.h"
#include "relstamp.h"
#include <imagehlp.h>
#pragma comment(lib, "imagehlp")
#if UNICODE
// only non-unicode form of MapAndLoad exists in imagehlp. Grr.
BOOL MapAndLoadW(
__in PCWSTR ImageName,
__in_opt PCWSTR DllPath,
__out PLOADED_IMAGE LoadedImage,
__in BOOL DotDll,
__in BOOL ReadOnly
);
#define MapAndLoadT MapAndLoadW
#else
#define MapAndLoadT MapAndLoad
#endif
BOOL getFileExtraData(LPCTSTR fname, PVOID *extraData, LPDWORD dwSize)
{
*dwSize = 0;
*extraData = NULL;
BOOL r;
LOADED_IMAGE im;
r = ::MapAndLoadT(
fname,
_T("\\no-implicit-paths"),
&im,
FALSE, // .exe by default
TRUE // readonly
);
if (!r) {
dprint("err open file for reading extra data %d\n", GetLastError() );
return FALSE;
}
ULONG endOfImage = 0;
for (ULONG i = 0; i < im.NumberOfSections; i++)
{
if (endOfImage < im.Sections[i].PointerToRawData + im.Sections[i].SizeOfRawData)
endOfImage = im.Sections[i].PointerToRawData + im.Sections[i].SizeOfRawData;
}
if (im.SizeOfImage > endOfImage)
{
*dwSize = im.SizeOfImage - endOfImage;
*extraData = malloc(*dwSize);
ASSERT(*extraData);
memcpy(*extraData, &im.MappedAddress[endOfImage], *dwSize);
}
r = ::UnMapAndLoad( &im );
if (!r) {
dprint("err unloading file %d\n", GetLastError() );
return FALSE;
}
return TRUE;
}
BOOL appendFileExtraData( PCTSTR fname, PVOID extraData, DWORD dwSize)
{
DWORD bytesWritten = 0;
if (extraData == NULL)
return TRUE;
HANDLE fh = CreateFile(fname, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, NULL);
if (INVALID_HANDLE_VALUE == fh ) {
dtprint(_T("Error opening executable %s err=%d\n"), fname, GetLastError());
return false;
}
DWORD pos = SetFilePointer(fh, 0, NULL, FILE_END);
if ( INVALID_SET_FILE_POINTER == pos ) {
dtprint(_T("Error seeking executable %s err=%d\n"), fname, GetLastError());
CloseHandle(fh);
return false;
}
if ( !WriteFile(fh, extraData, dwSize, &bytesWritten, NULL) || (bytesWritten != dwSize) ) {
dtprint(_T("Error writing extra data %s err=%d\n"), fname, GetLastError());
CloseHandle(fh);
return false;
}
CloseHandle(fh);
return TRUE;
}