forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBytePattern.cs
More file actions
176 lines (148 loc) · 4.16 KB
/
Copy pathBytePattern.cs
File metadata and controls
176 lines (148 loc) · 4.16 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
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
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using ReClassNET.Util;
namespace ReClassNET.MemoryScanner
{
public class BytePattern
{
private struct PatternByte
{
private struct Nibble
{
public int Value;
public bool IsWildcard;
}
private Nibble nibble1;
private Nibble nibble2;
public bool HasWildcard => nibble1.IsWildcard || nibble2.IsWildcard;
public byte ByteValue => !HasWildcard ? (byte)((nibble1.Value << 4) + nibble2.Value) : throw new InvalidOperationException();
private static bool IsHexValue(char c)
{
return '0' <= c && c <= '9'
|| 'A' <= c && c <= 'F'
|| 'a' <= c && c <= 'f';
}
private static int HexToInt(char c)
{
if ('0' <= c && c <= '9') return c - '0';
if ('A' <= c && c <= 'F') return c - 'A' + 10;
return c - 'a' + 10;
}
public bool TryRead(StringReader sr)
{
Contract.Requires(sr != null);
var temp = sr.ReadSkipWhitespaces();
if (temp == -1 || !IsHexValue((char)temp) && (char)temp != '?')
{
return false;
}
nibble1.Value = HexToInt((char)temp) & 0xF;
nibble1.IsWildcard = (char)temp == '?';
temp = sr.Read();
if (temp == -1 || char.IsWhiteSpace((char)temp) || (char)temp == '?')
{
nibble2.IsWildcard = true;
return true;
}
if (!IsHexValue((char)temp))
{
return false;
}
nibble2.Value = HexToInt((char)temp) & 0xF;
nibble2.IsWildcard = false;
return true;
}
public bool Equals(byte b)
{
var matched = 0;
if (nibble1.IsWildcard || ((b >> 4) & 0xF) == nibble1.Value)
{
++matched;
}
if (nibble2.IsWildcard || (b & 0xF) == nibble2.Value)
{
++matched;
}
return matched == 2;
}
}
private readonly List<PatternByte> pattern = new List<PatternByte>();
/// <summary>
/// Gets the length of the pattern in byte.
/// </summary>
public int Length => pattern.Count;
/// <summary>
/// Gets if the pattern contains wildcards.
/// </summary>
public bool HasWildcards => pattern.Any(pb => pb.HasWildcard);
/// <summary>
/// Parses the provided string for a byte pattern. Wildcards are supported by nibble.
/// </summary>
/// <example>
/// Valid patterns:
/// AA BB CC DD
/// AABBCCDD
/// aabb CCdd
/// A? ?B ?? DD
/// </example>
/// <exception cref="ArgumentException">Thrown if the provided string doesn't contain a valid byte pattern.</exception>
/// <param name="value">The byte pattern in hex format.</param>
/// <returns>The corresponding <see cref="BytePattern"/>.</returns>
public static BytePattern Parse(string value)
{
Contract.Requires(!string.IsNullOrEmpty(value));
Contract.Ensures(Contract.Result<BytePattern>() != null);
var pattern = new BytePattern();
using (var sr = new StringReader(value))
{
var pb = new PatternByte();
while (pb.TryRead(sr))
{
pattern.pattern.Add(pb);
}
// Check if we are not at the end of the stream
if (sr.Peek() != -1)
{
throw new ArgumentException($"'{value}' is not a valid byte pattern.");
}
}
return pattern;
}
/// <summary>
/// Tests if the provided byte array matches the byte pattern at the provided index.
/// </summary>
/// <param name="data">The byte array to be compared.</param>
/// <param name="index">The index into the byte array.</param>
/// <returns>True if the pattern matches, false if they are not.</returns>
public bool Equals(byte[] data, int index)
{
Contract.Requires(data != null);
for (var j = 0; j < pattern.Count; ++j)
{
if (!pattern[j].Equals(data[index + j]))
{
return false;
}
}
return true;
}
/// <summary>
/// Converts this <see cref="BytePattern"/> to a byte array.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if the pattern contains wildcards.</exception>
/// <returns>The bytes of the pattern.
/// </returns>
public byte[] ToByteArray()
{
Contract.Ensures(Contract.Result<byte[]>() != null);
if (HasWildcards)
{
throw new InvalidOperationException();
}
return pattern.Select(pb => pb.ByteValue).ToArray();
}
}
}