source: Daodan/src/Daodan_Patch.c@ 748

Last change on this file since 748 was 689, checked in by alloc, 12 years ago

Daodan: Cleaned up code so no major warnings exist

File size: 2.3 KB
Line 
1#include "Daodan_Patch.h"
2#include <windows.h>
3#include <stdlib.h>
4#include <string.h>
5
6bool DDrPatch_MakeJump(void* from, void* to)
7{
8 DWORD oldp;
9
10 if (VirtualProtect(from, 5, PAGE_EXECUTE_READWRITE, &oldp))
11 {
12 *((unsigned char*)from) = 0xe9; // jmp rel32
13 from = (char*)from + 1;
14 *(int*)from = (unsigned int)to - (unsigned int)from - 4;
15 VirtualProtect(from, 5, oldp, &oldp);
16 return true;
17 }
18 else
19 return false;
20}
21
22bool DDrPatch_MakeCall(void* from, void* to)
23{
24 DWORD oldp;
25
26 if (VirtualProtect(from, 5, PAGE_EXECUTE_READWRITE, &oldp))
27 {
28 *((unsigned char*)from) = 0xe8; // call rel32
29 from = (char*)from + 1;
30 *(int*)from = (unsigned int)to - (unsigned int)from - 4;
31 VirtualProtect(from, 5, oldp, &oldp);
32 return true;
33 }
34 else
35 return false;
36}
37
38bool DDrPatch_String(char* dest, const unsigned char* string, int length)
39{
40 DWORD oldp;
41
42 if (VirtualProtect(dest, length, PAGE_EXECUTE_READWRITE, &oldp))
43 {
44 memcpy(dest, string, length);
45 VirtualProtect(dest, length, oldp, &oldp);
46 return true;
47 }
48 else
49 return false;
50}
51
52bool DDrPatch_Byte(char* dest, unsigned char value)
53{
54 DWORD oldp;
55
56 if (VirtualProtect(dest, 1, PAGE_EXECUTE_READWRITE, &oldp))
57 {
58 *dest = value;
59 VirtualProtect(dest, 1, oldp, &oldp);
60 return true;
61 }
62 else
63 return false;
64}
65
66bool DDrPatch_Int32(int* dest, unsigned int value)
67{
68 DWORD oldp;
69
70 if (VirtualProtect(dest, 4, PAGE_EXECUTE_READWRITE, &oldp))
71 {
72 *dest = value;
73 VirtualProtect(dest, 4, oldp, &oldp);
74 return true;
75 }
76 else
77 return false;
78}
79
80bool DDrPatch_Int16(short* dest, unsigned short value)
81{
82 DWORD oldp;
83
84 if (VirtualProtect(dest, 2, PAGE_EXECUTE_READWRITE, &oldp))
85 {
86 *dest = value;
87 VirtualProtect(dest, 2, oldp, &oldp);
88 return true;
89 }
90 else
91 return false;
92}
93
94bool DDrPatch__strdup(int* dest, const char* value)
95{
96 DWORD oldp;
97
98 if (VirtualProtect(dest, 4, PAGE_EXECUTE_READWRITE, &oldp))
99 {
100 *dest = (int)_strdup(value);
101 VirtualProtect(dest, 4, oldp, &oldp);
102 return true;
103 }
104 else
105 return false;
106}
107
108bool DDrPatch_NOOP(char* dest, unsigned int length)
109{
110 DWORD oldp;
111
112 if (VirtualProtect(dest, length, PAGE_EXECUTE_READWRITE, &oldp))
113 {
114 memset(dest, 0x90, length);
115 VirtualProtect(dest, length, oldp, &oldp);
116 return true;
117 }
118 else
119 return false;
120}
Note: See TracBrowser for help on using the repository browser.