This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHexPattern.cs
59 lines (50 loc) · 1.71 KB
/
HexPattern.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProcessMemory
{
public class HexPattern : IMemoryPattern
{
public string[] Pattern { get; private set; }
public byte?[] PatternBytes { get; private set; }
public long Length => PatternBytes.Length;
public HexPattern(string pattern) {
//Split pattern into an array for each space.
Pattern = pattern.Split(' ');
byte?[] bytes = new byte?[Pattern.Length];
//Loop through each pattern string
for (int i = 0; i < Pattern.Length; i++) {
//If pattern at index is == ?? then we set that byte to null
if (Pattern[i].Equals("??"))
{
bytes[i] = null;
}
else
{
bytes[i] = Convert.ToByte(Pattern[i], 16);
}
//If pattern is not == ?? then convert it to a byte
}
PatternBytes = bytes;
}
public long FindMatch(byte[] source, long length) {
bool allTheSame;
for (int i = 0; i + PatternBytes.Length <= length; i++) {
allTheSame = true;
for (int jj = 0; jj < PatternBytes.Length; jj++) {
if (PatternBytes[jj] == null)
continue;
if (source[i + jj] != PatternBytes[jj]) {
allTheSame = false;
break;
}
}
if (allTheSame)
return i;
}
return -1;
}
}
}