This repository has been archived by the owner on Oct 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Patcher.cpp
executable file
·87 lines (62 loc) · 1.66 KB
/
Patcher.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
#include "Revelation.h"
#include "Patcher.h"
CPatch::CPatch()
{
m_Offset = NULL;
m_NewData = NULL;
m_OldData = NULL;
m_Size = 0;
m_Applied = false;
m_Valid = false;
}
CPatch::~CPatch()
{
if (m_Applied)
Remove();
m_Offset = NULL;
m_Size = 0;
delete[] m_NewData;
delete[] m_OldData;
}
CPatch::CPatch(DWORD nOffset, BYTE *nData, DWORD nSize)
{
m_Applied = false;
m_Valid = false;
DWORD dwOldProt, dwDummy;
m_Offset = nOffset;
m_Size = nSize;
m_NewData = new BYTE[ m_Size ];
m_OldData = new BYTE[ m_Size ];
if (!memcpy(m_NewData, nData, m_Size)) return;
if (!VirtualProtect((LPVOID)m_Offset, m_Size, PAGE_EXECUTE_READWRITE, &dwOldProt)) return;
if (!memcpy(m_OldData, (LPVOID)m_Offset, m_Size)) return;
VirtualProtect((LPVOID)m_Offset, m_Size, dwOldProt, &dwDummy);
m_Valid = true;
Apply();
}
bool CPatch::Apply()
{
if (!m_Valid || m_Applied)
return false;
DWORD dwOldProt, dwDummy;
if (!VirtualProtect((LPVOID)m_Offset, m_Size, PAGE_EXECUTE_READWRITE, &dwOldProt))
return false;
if (!memcpy((LPVOID)m_Offset, m_NewData, m_Size))
return false;
VirtualProtect((LPVOID)m_Offset, m_Size, dwOldProt, &dwDummy);
m_Applied = true;
return true;
}
bool CPatch::Remove()
{
if (!m_Valid || !m_Applied)
return false;
DWORD dwOldProt, dwDummy;
if (!VirtualProtect((LPVOID)m_Offset, m_Size, PAGE_EXECUTE_READWRITE, &dwOldProt))
return false;
if (!memcpy((LPVOID)m_Offset, m_OldData, m_Size))
return false;
VirtualProtect((LPVOID)m_Offset, m_Size, dwOldProt, &dwDummy);
m_Applied = false;
return true;
}