aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDedicatedTest/NSMem.h
diff options
context:
space:
mode:
authorNorthstar <northstar@northstar.tf>2022-04-13 19:29:44 -0300
committerBarichello <artur@barichello.me>2022-04-13 19:30:40 -0300
commit47ea109be0d73120ddfe6896494e1fce986515b3 (patch)
treefb440b3ab2a00bf506182a9c9a9500d1c4711e0c /NorthstarDedicatedTest/NSMem.h
parent04b0b417d1c9f45a708d3e2eec9146fabe09c1d1 (diff)
downloadNorthstarLauncher-47ea109be0d73120ddfe6896494e1fce986515b3.tar.gz
NorthstarLauncher-47ea109be0d73120ddfe6896494e1fce986515b3.zip
Format project
Diffstat (limited to 'NorthstarDedicatedTest/NSMem.h')
-rw-r--r--NorthstarDedicatedTest/NSMem.h216
1 files changed, 108 insertions, 108 deletions
diff --git a/NorthstarDedicatedTest/NSMem.h b/NorthstarDedicatedTest/NSMem.h
index 50928dd2..74df3a84 100644
--- a/NorthstarDedicatedTest/NSMem.h
+++ b/NorthstarDedicatedTest/NSMem.h
@@ -6,147 +6,147 @@
#pragma region Pattern Scanning
namespace NSMem
{
-inline std::vector<int> HexBytesToString(const char* str)
-{
- std::vector<int> patternNums;
- int size = strlen(str);
- for (int i = 0; i < size; i++)
+ inline std::vector<int> HexBytesToString(const char* str)
{
- char c = str[i];
+ std::vector<int> patternNums;
+ int size = strlen(str);
+ for (int i = 0; i < size; i++)
+ {
+ char c = str[i];
- // If this is a space character, ignore it
- if (c == ' ' || c == '\t')
- continue;
+ // If this is a space character, ignore it
+ if (c == ' ' || c == '\t')
+ continue;
- if (c == '?')
- {
- // Add a wildcard (-1)
- patternNums.push_back(-1);
- }
- else if (i < size - 1)
- {
- BYTE result = 0;
- for (int j = 0; j < 2; j++)
+ if (c == '?')
{
- int val = 0;
- char c = *(str + i + j);
- if (c >= 'a')
- {
- val = c - 'a' + 0xA;
- }
- else if (c >= 'A')
- {
- val = c - 'A' + 0xA;
- }
- else if (isdigit(c))
- {
- val = c - '0';
- }
- else
+ // Add a wildcard (-1)
+ patternNums.push_back(-1);
+ }
+ else if (i < size - 1)
+ {
+ BYTE result = 0;
+ for (int j = 0; j < 2; j++)
{
- assert(false, "Failed to parse invalid hex string.");
- val = -1;
+ int val = 0;
+ char c = *(str + i + j);
+ if (c >= 'a')
+ {
+ val = c - 'a' + 0xA;
+ }
+ else if (c >= 'A')
+ {
+ val = c - 'A' + 0xA;
+ }
+ else if (isdigit(c))
+ {
+ val = c - '0';
+ }
+ else
+ {
+ assert(false, "Failed to parse invalid hex string.");
+ val = -1;
+ }
+
+ result += (j == 0) ? val * 16 : val;
}
-
- result += (j == 0) ? val * 16 : val;
+ patternNums.push_back(result);
}
- patternNums.push_back(result);
+
+ i++;
}
- i++;
+ return patternNums;
}
- return patternNums;
-}
-
-inline void* PatternScan(void* module, const int* pattern, int patternSize, int offset)
-{
- if (!module)
- return NULL;
+ inline void* PatternScan(void* module, const int* pattern, int patternSize, int offset)
+ {
+ if (!module)
+ return NULL;
- auto dosHeader = (PIMAGE_DOS_HEADER)module;
- auto ntHeaders = (PIMAGE_NT_HEADERS)((BYTE*)module + dosHeader->e_lfanew);
+ auto dosHeader = (PIMAGE_DOS_HEADER)module;
+ auto ntHeaders = (PIMAGE_NT_HEADERS)((BYTE*)module + dosHeader->e_lfanew);
- auto sizeOfImage = ntHeaders->OptionalHeader.SizeOfImage;
+ auto sizeOfImage = ntHeaders->OptionalHeader.SizeOfImage;
- auto scanBytes = (BYTE*)module;
+ auto scanBytes = (BYTE*)module;
- for (auto i = 0; i < sizeOfImage - patternSize; ++i)
- {
- bool found = true;
- for (auto j = 0; j < patternSize; ++j)
+ for (auto i = 0; i < sizeOfImage - patternSize; ++i)
{
- if (scanBytes[i + j] != pattern[j] && pattern[j] != -1)
+ bool found = true;
+ for (auto j = 0; j < patternSize; ++j)
{
- found = false;
- break;
+ if (scanBytes[i + j] != pattern[j] && pattern[j] != -1)
+ {
+ found = false;
+ break;
+ }
}
- }
- if (found)
- {
- uintptr_t addressInt = (uintptr_t)(&scanBytes[i]) + offset;
- return (uint8_t*)addressInt;
+ if (found)
+ {
+ uintptr_t addressInt = (uintptr_t)(&scanBytes[i]) + offset;
+ return (uint8_t*)addressInt;
+ }
}
- }
- return nullptr;
-}
+ return nullptr;
+ }
-inline void* PatternScan(const char* moduleName, const char* pattern, int offset = 0)
-{
- std::vector<int> patternNums = HexBytesToString(pattern);
+ inline void* PatternScan(const char* moduleName, const char* pattern, int offset = 0)
+ {
+ std::vector<int> patternNums = HexBytesToString(pattern);
- return PatternScan(GetModuleHandleA(moduleName), &patternNums[0], patternNums.size(), offset);
-}
+ return PatternScan(GetModuleHandleA(moduleName), &patternNums[0], patternNums.size(), offset);
+ }
-inline void BytePatch(uintptr_t address, const BYTE* vals, int size)
-{
- WriteProcessMemory(GetCurrentProcess(), (LPVOID)address, vals, size, NULL);
-}
+ inline void BytePatch(uintptr_t address, const BYTE* vals, int size)
+ {
+ WriteProcessMemory(GetCurrentProcess(), (LPVOID)address, vals, size, NULL);
+ }
-inline void BytePatch(uintptr_t address, std::initializer_list<BYTE> vals)
-{
- std::vector<BYTE> bytes = vals;
- if (!bytes.empty())
- BytePatch(address, &bytes[0], bytes.size());
-}
+ inline void BytePatch(uintptr_t address, std::initializer_list<BYTE> vals)
+ {
+ std::vector<BYTE> bytes = vals;
+ if (!bytes.empty())
+ BytePatch(address, &bytes[0], bytes.size());
+ }
-inline void BytePatch(uintptr_t address, const char* bytesStr)
-{
- std::vector<int> byteInts = HexBytesToString(bytesStr);
- std::vector<BYTE> bytes;
- for (int v : byteInts)
- bytes.push_back(v);
+ inline void BytePatch(uintptr_t address, const char* bytesStr)
+ {
+ std::vector<int> byteInts = HexBytesToString(bytesStr);
+ std::vector<BYTE> bytes;
+ for (int v : byteInts)
+ bytes.push_back(v);
- if (!bytes.empty())
- BytePatch(address, &bytes[0], bytes.size());
-}
+ if (!bytes.empty())
+ BytePatch(address, &bytes[0], bytes.size());
+ }
-inline void NOP(uintptr_t address, int size)
-{
- BYTE* buf = (BYTE*)malloc(size);
- memset(buf, 0x90, size);
- BytePatch(address, buf, size);
- free(buf);
-}
+ inline void NOP(uintptr_t address, int size)
+ {
+ BYTE* buf = (BYTE*)malloc(size);
+ memset(buf, 0x90, size);
+ BytePatch(address, buf, size);
+ free(buf);
+ }
-inline bool IsMemoryReadable(void* ptr, size_t size)
-{
- static SYSTEM_INFO sysInfo;
- if (!sysInfo.dwPageSize)
- GetSystemInfo(&sysInfo); // This should always be 4096 unless ur playing on NES or some shit but whatever
+ inline bool IsMemoryReadable(void* ptr, size_t size)
+ {
+ static SYSTEM_INFO sysInfo;
+ if (!sysInfo.dwPageSize)
+ GetSystemInfo(&sysInfo); // This should always be 4096 unless ur playing on NES or some shit but whatever
- MEMORY_BASIC_INFORMATION memInfo;
+ MEMORY_BASIC_INFORMATION memInfo;
- if (!VirtualQuery(ptr, &memInfo, sizeof(memInfo)))
- return false;
+ if (!VirtualQuery(ptr, &memInfo, sizeof(memInfo)))
+ return false;
- if (memInfo.RegionSize < size)
- return false;
+ if (memInfo.RegionSize < size)
+ return false;
- return (memInfo.State & MEM_COMMIT) && !(memInfo.Protect & PAGE_NOACCESS);
-}
+ return (memInfo.State & MEM_COMMIT) && !(memInfo.Protect & PAGE_NOACCESS);
+ }
} // namespace NSMem
#pragma region KHOOK