Files
PresentHookDetection/BattleyePresentCheck/utils.cpp
2022-01-18 23:02:53 +01:00

45 lines
1.3 KiB
C++

#include "utils.h"
uintptr_t utils::scanpattern(uintptr_t base, int size, const char* signature)
{
static auto patternToByte = [](const char* pattern)
{
auto bytes = std::vector<int>{};
const auto start = const_cast<char*>(pattern);
const auto end = const_cast<char*>(pattern) + strlen(pattern);
for (auto current = start; current < end; ++current)
{
if (*current == '?')
{
++current;
if (*current == '?')
++current;
bytes.push_back(-1);
}
else { bytes.push_back(strtoul(current, &current, 16)); }
}
return bytes;
};
auto patternBytes = patternToByte(signature);
const auto scanBytes = reinterpret_cast<std::uint8_t*>(base);
const auto s = patternBytes.size();
const auto d = patternBytes.data();
for (auto i = 0ul; i < size - s; ++i)
{
bool found = true;
for (auto j = 0ul; j < s; ++j)
{
if (scanBytes[i + j] != d[j] && d[j] != -1)
{
found = false;
break;
}
}
if (found) { return reinterpret_cast<uintptr_t>(&scanBytes[i]); }
}
return NULL;
}