-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryUtility.cs
More file actions
111 lines (105 loc) · 3.11 KB
/
MemoryUtility.cs
File metadata and controls
111 lines (105 loc) · 3.11 KB
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
namespace ProcessMemoryScanner
{
public static class MemoryUtility
{
public static bool BytesMatch(byte[] A, byte[] B)
{
if(A.Length != B.Length)
{
return false;
}
for(var i = 0; i < A.Length; i++)
{
if(A[i] != B[i])
{
return false;
}
}
return true;
}
public static byte[] SubBytes(this byte[] data, int startIndex, int Length)
{
var result = new byte[Length];
for (var i = 0; i < Length; i++)
{
result[i] = data[i + startIndex];
}
return result;
}
public static byte?[] SubBytesWithWildCard(this byte?[] data, int startIndex, int Length)
{
var result = new byte?[Length];
for (var i = 0; i < Length; i++)
{
result[i] = data[i + startIndex];
}
return result;
}
public static int IndexOf(this byte[] bytes, byte[] subBytes)
{
var index = -1;
for (var i = 0; i < bytes.Length; i++)
{
if (bytes[i] == subBytes[0])
{
var match = true;
for (var j = 1; j < subBytes.Length; j++)
{
if (bytes[i + j] != subBytes[j])
{
match = false;
break;
}
}
if (match)
{
index = i;
break;
}
}
}
return index;
}
public static int IndexOfWithWildCard(this byte?[] bytes, byte?[] subBytes)
{
var index = -1;
for (var i = 0; i < bytes.Length; i++)
{
if (WildCardMatch(bytes[i], subBytes[0]))
{
var match = true;
for (var j = 1; j < subBytes.Length; j++)
{
if (!WildCardMatch(bytes[i + j], subBytes[j]))
{
match = false;
break;
}
}
if (match)
{
index = i;
break;
}
}
}
return index;
}
public static byte?[] ToWildCardByteArray(this byte[] bytes)
{
var result = new byte?[bytes.Length];
for(var i = 0; i < bytes.Length; i++)
{
result[i] = bytes[i];
}
return result;
}
/// <summary>
/// A==B || A == null || B == null
/// </summary>
public static bool WildCardMatch(byte? A, byte? B)
{
return (A == null) || (B == null) || A == B;
}
}
}