-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathDelphiUtils.RangeChecks.pas
128 lines (106 loc) · 2.93 KB
/
DelphiUtils.RangeChecks.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
unit DelphiUtils.RangeChecks;
{
This module introduces helper functions for performing strict range checks
during parsing.
}
interface
uses
NtUtils;
// Check if an address belongs to a memory region
function CheckAddress(
const Region: TMemory;
[in] Address: Pointer
): Boolean;
// Check if a range of addresses belongs to a memory region
function CheckRange(
const Region: TMemory;
[in] BlockStart: Pointer;
[in] BlockEnd: Pointer
): Boolean;
// Check if a structure allocated at an address belongs to a memory region
function CheckStruct(
const Region: TMemory;
[in] BlockStart: Pointer;
const BlockSize: UInt64
): Boolean;
// Check if an offset belongs to the memory region
function CheckOffset(
RegionSize: NativeUInt;
const Offset: UInt64
): Boolean;
// Check if a structure allocated at an offset fits into a memory region
function CheckOffsetStruct(
const Region: TMemory;
const BlockOffset: UInt64;
const BlockSize: UInt64
): Boolean;
// Check if an array of elements fits into a buffer
function CheckArraySize(
RangeSize: NativeUInt;
const ElementSize: UInt64;
const ElementCount: UInt64
): Boolean;
// Check if an array allocated at an address belongs to a memory region
function CheckArray(
const Region: TMemory;
[in] ArrayStart: Pointer;
const ElementSize: UInt64;
const ElementCount: UInt64
): Boolean;
// Check if an array allocated at an offset fits into a memory region
function CheckOffsetArray(
const Region: TMemory;
const ArrayOffset: UInt64;
const ElementSize: UInt64;
const ElementCount: UInt64
): Boolean;
implementation
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function CheckAddress;
begin
Result := (UIntPtr(Address) >= UIntPtr(Region.Address)) and
((UIntPtr(Address) <= UIntPtr(Region.Offset(Region.Size))));
end;
function CheckRange;
begin
Result := (UIntPtr(BlockEnd) >= UIntPtr(BlockStart)) and
CheckAddress(Region, BlockStart) and CheckAddress(Region, BlockEnd);
end;
function CheckStruct;
begin
Result := (BlockSize <= Region.Size) and CheckAddress(Region, BlockStart) and
CheckAddress(Region, PByte(BlockStart) + BlockSize);
end;
function CheckOffset;
begin
Result := Offset <= RegionSize;
end;
function CheckOffsetStruct;
begin
{$Q-}
Result := (BlockSize <= Region.Size) and (BlockOffset <= Region.Size) and
(Region.Size - BlockOffset >= BlockSize);
{$IFDEF Q+}{$Q+}{$ENDIF}
end;
function CheckArraySize;
begin
Result := (ElementSize = 0) or ((RangeSize div ElementSize) >= ElementCount);
end;
function CheckArray;
begin
{$Q-}
Result := CheckAddress(Region, ArrayStart) and CheckArraySize(
UIntPtr(Region.Offset(Region.Size)) - UIntPtr(ArrayStart), ElementSize,
ElementCount);
{$IFDEF Q+}{$Q+}{$ENDIF}
end;
function CheckOffsetArray;
begin
{$Q-}
Result := (ArrayOffset <= Region.Size) and CheckArraySize(
Region.Size - ArrayOffset, ElementSize, ElementCount);
{$IFDEF Q+}{$Q+}{$ENDIF}
end;
end.