-
Notifications
You must be signed in to change notification settings - Fork 6
/
MemoryAlloc.inc
113 lines (95 loc) · 3.06 KB
/
MemoryAlloc.inc
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
#if defined _MemoryEx_MemoryAlloc_include_
#endinput
#endif
#define _MemoryEx_MemoryAlloc_include_
#include <MemoryEx/ServerLibrary>
stock int GetProcessHeap()
{
static Address pFunc;
static Handle h;
if(pFunc == Address_Null)
{
pFunc = GetProcAddress("kernel32", "GetProcessHeap");
if(pFunc != Address_Null)
{
StartPrepSDKCall(SDKCall_Static);
PrepSDKCall_SetAddress(pFunc);
PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain);
h = EndPrepSDKCall();
}
}
return SDKCall(h);
}
stock Address VirtualAlloc(int iSize)
{
static Address pFunc;
static Handle h;
if(pFunc == Address_Null)
{
if(GetServerOS() == OS_Windows)
{
pFunc = GetImportAddress("kernel32", "HeapAlloc");
if(pFunc != Address_Null)
{
StartPrepSDKCall(SDKCall_Static);
PrepSDKCall_SetAddress(pFunc);
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain);
h = EndPrepSDKCall();
}
}
else
{
pFunc = GetImportAddress(NULL_STRING, "malloc");
StartPrepSDKCall(SDKCall_Static);
PrepSDKCall_SetAddress(pFunc);
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain);
h = EndPrepSDKCall();
}
}
if(GetServerOS() == OS_Windows)
{
return SDKCall(h, GetProcessHeap(), 0x00000008, iSize); // 0x08 == HEAP_ZERO_MEMORY => https://docs.microsoft.com/en-us/windows/win32/api/heapapi/nf-heapapi-heapalloc
}
return SDKCall(h, iSize);
}
stock void FreeMemory(Address pBase)
{
static Address pFunc;
static Handle h;
if(pFunc == Address_Null)
{
if(GetServerOS() == OS_Windows)
{
pFunc = GetImportAddress("kernel32", "HeapFree");
if(pFunc != Address_Null)
{
StartPrepSDKCall(SDKCall_Static);
PrepSDKCall_SetAddress(pFunc);
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
h = EndPrepSDKCall();
}
}
else
{
pFunc = GetImportAddress(NULL_STRING, "free");
StartPrepSDKCall(SDKCall_Static);
PrepSDKCall_SetAddress(pFunc);
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
h = EndPrepSDKCall();
}
}
if(GetServerOS() == OS_Windows)
{
SDKCall(h, GetProcessHeap(), 0x00, pBase);
}
else
{
SDKCall(h, pBase);
}
}