-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
56 lines (48 loc) · 1.4 KB
/
memory.cpp
File metadata and controls
56 lines (48 loc) · 1.4 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
#include "pch.h"
#include "memory.h"
const byte * findData(const byte * data, unsigned dataSize, const byte *start, unsigned length, unsigned startOffset)
{
for (const byte *ptr = start + startOffset; ptr <= (start + length - dataSize); ++ptr)
{
if (memcmp(data, ptr, dataSize) == 0)
{
return ptr;
}
}
return nullptr;
}
const byte * findDataR(const byte *data, unsigned dataSize, const byte *end, unsigned length)
{
for (const byte *ptr = end - dataSize; ptr >= (end - length); --ptr)
{
if (memcmp(data, ptr, dataSize) == 0)
{
return ptr;
}
}
return nullptr;
}
static inline bool matchPattern(const unsigned short *pattern, unsigned patternLength, const byte *ptr)
{
while (patternLength > 0
&& ((((*pattern) & 0xFF00) != 0)
|| byte((*pattern) & 0x00FF) == *ptr))
{
++ptr;
++pattern;
--patternLength;
}
return patternLength == 0;
}
const byte * findPattern(const unsigned short *pattern, unsigned patternLength, const byte *start, unsigned length, unsigned startOffset)
{
assert((startOffset + patternLength) < length);
for (const byte *ptr = start + startOffset; ptr <= (start + length - patternLength); ++ptr)
{
if (matchPattern(pattern, patternLength, ptr))
{
return ptr;
}
}
return nullptr;
}