forked from sims3fiend/Sims3SettingsSetter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_scan.cpp
More file actions
82 lines (67 loc) · 2.08 KB
/
pattern_scan.cpp
File metadata and controls
82 lines (67 loc) · 2.08 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
#include "pattern_scan.h"
#include <Psapi.h>
#include <sstream>
namespace Pattern {
std::vector<std::pair<uint8_t, bool>> ParsePattern(const char* pattern) {
std::vector<std::pair<uint8_t, bool>> bytes;
std::stringstream ss(pattern);
std::string byte;
while (ss >> byte) {
if (byte == "?" || byte == "??") {
bytes.push_back({0, false});
} else {
bytes.push_back({(uint8_t)std::stoi(byte, nullptr, 16), true});
}
}
return bytes;
}
uintptr_t Scan(const char* pattern, const char* mask) {
return ScanModule(GetModuleHandle(nullptr), pattern, mask);
}
uintptr_t ScanModule(HMODULE module, const char* pattern, const char* mask) {
if (!module) return 0;
MODULEINFO moduleInfo;
if (!GetModuleInformation(GetCurrentProcess(), module, &moduleInfo, sizeof(moduleInfo))) {
return 0;
}
std::vector<std::pair<uint8_t, bool>> bytes;
if (mask) {
// Using raw pattern + mask
const uint8_t* pat = (const uint8_t*)pattern;
bytes.reserve(strlen(mask));
for (size_t i = 0; mask[i]; i++) {
bytes.push_back({pat[i], mask[i] == 'x'});
}
} else {
// Using space-separated hex string pattern
bytes = ParsePattern(pattern);
}
uintptr_t start = (uintptr_t)module;
size_t size = moduleInfo.SizeOfImage;
for (uintptr_t i = 0; i < size - bytes.size(); i++) {
bool found = true;
for (size_t j = 0; j < bytes.size(); j++) {
if (bytes[j].second) { // If this byte should be checked
if (*(uint8_t*)(start + i + j) != bytes[j].first) {
found = false;
break;
}
}
}
if (found) {
return start + i;
}
}
return 0;
}
// AUUHHHHHHHGHHHHHHHHHHHH
std::string CreateMask(const char* pattern) {
std::stringstream ss(pattern);
std::string byte;
std::string mask;
while (ss >> byte) {
mask += (byte == "?" || byte == "??") ? "?" : "x";
}
return mask;
}
}