forked from Sovos-Compliance/convey-public-libs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CnvGenUtils.pas
272 lines (235 loc) · 6.84 KB
/
CnvGenUtils.pas
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
{ This unit MUST NOT include any other units that reference Classes unit
in its uses clause because this unit is used in the array memory allocator
wich MUST be reference before Classes unit in the global scope of the
project }
unit CnvGenUtils;
{ When running PaxImporter use option -DPAXIMPORT }
{$i DelphiVersion_defines.inc}
{$IFNDEF PAXIMPORT}
{$i LibVer.inc}
{$ENDIF}
interface
uses
Windows;
type
DWORD = Cardinal;
THandle = Longword;
BOOL = LongBool;
const
SZI = SizeOf (Integer);
SZW = SizeOf (Word);
SZT = SizeOf (TObject);
SZP = SizeOf (Pointer);
SZH = SizeOf (THandle);
SZD = SizeOf (Double);
SZL = SizeOf (LongWord);
SZC = SizeOf (Currency);
SZLI = SizeOf (Int64);
iTlsBaseOffset = $0E10;
VER_PLATFORM_WIN32s = 0;
VER_PLATFORM_WIN32_WINDOWS = 1;
VER_PLATFORM_WIN32_NT = 2;
type
PInt = ^Integer;
PByte = ^Byte;
TStrVarRecArray = class;
IStrVarRecArray = interface
['{0EEFBC72-98A5-4D3E-962B-82C7E191048B}']
function Obj : TStrVarRecArray;
end;
TStrVarRecArray = class (TInterfacedObject, IStrVarRecArray)
private
Strings : array of string;
protected
function Obj: TStrVarRecArray;
public
Arr : array of TVarRec;
constructor Create(const Values : array of Variant; FromIndex : integer = 0;
ToIndex : integer = -1);
class function CreateIntf(const Values : array of Variant; FromIndex : integer
= 0; ToIndex : integer = -1): IStrVarRecArray;
procedure SetStr(Index : integer; const AStr : string);
destructor Destroy; override;
end;
function SetVmtEntry(AClass : TClass; VmtOffset : Integer; NewAddr : Pointer): Pointer;
procedure PatchMemory(p : Pointer; DataSize : Integer; Data : Pointer; OldData : pointer); overload;
procedure PatchMemory(p : Pointer; DataSize : Integer; Data : Pointer); overload;
function SetTlsOffset(P: PInt; AOffset: Integer; DirectReplacement : boolean =
false): PInt;
{$IFNDEF WIN64}
function GetThreadVar(TlsSlot : integer): Pointer;
procedure SetThreadVar(TlsSlot : integer; Value : pointer);
{$ENDIF}
function KillProcess(const aProcess: string): Boolean;
function ProcessExists(const AProcess : string): Boolean;
function GetProcessHandle(const AProcess : string; dwDesiredAccess : DWORD): THandle;
function CnvGetComputerName: string;
function ParamCount: Integer;
function ParamStr(Index: Integer): string;
var
Win32Platform : DWORD;
implementation
uses
uWinProcHelper, {$IFDEF WIN64} SysUtils, {$ENDIF} TLHelp32 {$IFDef LEVEL7} , Variants {$EndIf}; // This three units are safe because they not reference Classes
const
FakeData : Pointer = nil;
function SetVmtEntry(AClass : TClass; VmtOffset : Integer; NewAddr : Pointer):
Pointer;
var
VmtPtr : Pointer;
begin
Result := nil;
VmtPtr := Pointer (Integer (AClass) + VmtOffset);
PatchMemory (VmtPtr, SizeOf (Pointer), @NewAddr, Result);
end;
procedure PatchMemory(p : Pointer; DataSize : Integer; Data : Pointer; OldData : pointer); {$IfDef LEVEL7} overload; {$ENDIF}
{$IFNDEF DELPHIXE2}
type
SIZE_T = DWORD;
{$ENDIF}
var
OldProtect : DWORD;
BytesWritten : SIZE_T;
begin
VirtualProtect (p, DataSize, PAGE_EXECUTE_READWRITE, OldProtect);
if OldData <> @FakeData
then Move (p^, OldData^, DataSize);
WriteProcessMemory(GetCurrentProcess, p, Data, DataSize, BytesWritten);
VirtualProtect (p, DataSize, OldProtect, OldProtect);
end;
procedure PatchMemory (p : Pointer; DataSize : Integer; Data : Pointer); {$IfDef LEVEL7} overload; {$EndIf}
begin
PatchMemory (p, DataSize, Data, @FakeData);
end;
function SetTlsOffset(P: PInt; AOffset: Integer; DirectReplacement : boolean =
false): PInt;
var
NewTlsOffset : Integer;
begin
while P^ <> iTlsBaseOffset do
Inc (PByte (P));
if DirectReplacement
then NewTlsOffset := AOffset
else NewTlsOffset := iTlsBaseOffset + AOffset * SZI;
PatchMemory (P, SizeOf (Integer), @NewTlsOffset);
Inc (P);
Result := P;
end;
procedure InitPlatformId;
var
OSVersionInfo: TOSVersionInfo;
begin
OSVersionInfo.dwOSVersionInfoSize := SizeOf(OSVersionInfo);
if GetVersionEx(OSVersionInfo) then
with OSVersionInfo do
Win32Platform := dwPlatformId;
end;
{$IFNDEF WIN64}
function GetThreadVar(TlsSlot : integer): Pointer;
asm
cmp Win32Platform, VER_PLATFORM_WIN32_NT // Check if is WinNT or higher
jge @@GetDirectAccess
push TlsSlot
call TlsGetValue
jmp @@Exit
@@GetDirectAccess:
mov Eax,fs:[TlsSlot]
@@Exit:
end;
procedure SetThreadVar(TlsSlot : integer; Value : pointer);
asm
cmp Win32Platform, VER_PLATFORM_WIN32_NT // Check if is WinNT or higher
jge @@SetDirectAccess
push Value
push TlsSlot
call TlsSetValue
jmp @@Exit
@@SetDirectAccess:
mov fs:[TlsSlot], Value
@@Exit:
end;
{$ENDIF}
function KillProcess(const aProcess: string): Boolean;
begin
Result := uWinProcHelper.KillProcess(AProcess);
end;
function ProcessExists(const AProcess : string): Boolean;
var
h : THandle;
begin
h := GetProcessHandle (AProcess, STANDARD_RIGHTS_REQUIRED);
try
Result := h <> 0;
finally
if h <> 0
then CloseHandle (h);
end;
end;
function GetProcessHandle(const AProcess : string; dwDesiredAccess : DWORD):
THandle;
begin
Result := uWinProcHelper.GetProcessHandle(AProcess, dwDesiredAccess);
end;
function CnvGetComputerName: string;
var
Comp: array[0..255] of Char;
I: DWord;
begin
I := MAX_COMPUTERNAME_LENGTH + 1;
GetComputerName(Comp, I);
Result := string(Comp);
end;
function ParamStr(Index: Integer): string;
begin
Result := system.ParamStr(Index);
end;
function ParamCount: Integer;
begin
Result := system.ParamCount;
end;
{ TStrVarRecArray }
constructor TStrVarRecArray.Create(const Values : array of Variant; FromIndex :
integer = 0; ToIndex : integer = -1);
var
i : Integer;
begin
inherited Create;
{$IFNDEF PAXIMPORT}
if ToIndex = -1
then ToIndex := Length (Values) - 1;
SetLength (Arr, ToIndex - FromIndex + 1);
SetLength (Strings, ToIndex - FromIndex + 1);
for i := FromIndex to ToIndex do
begin
Arr [i - FromIndex].VType := vtAnsiString;
SetStr (i - FromIndex, VarToStr (VarAsType (Values [i], varOleStr)));
end;
{$ENDIF}
end;
procedure TStrVarRecArray.SetStr(Index : integer; const AStr : string);
begin
{$IFNDEF PAXIMPORT}
Strings [Index] := AStr;
Arr [Index].VAnsiString := Pointer (Strings [Index]);
{$ENDIF}
end;
function TStrVarRecArray.Obj: TStrVarRecArray;
begin
Result := Self;
end;
class function TStrVarRecArray.CreateIntf(const Values : array of Variant;
FromIndex : integer = 0; ToIndex : integer = -1): IStrVarRecArray;
begin
{$IFNDEF PAXIMPORT}
Result := TStrVarRecArray.Create (Values, FromIndex, ToIndex);
{$ENDIF}
end;
destructor TStrVarRecArray.Destroy;
begin
SetLength (Arr, 0);
SetLength (Strings, 0);
inherited;
end;
initialization
InitPlatformId;
end.