This repository has been archived by the owner on Sep 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemory.h
39 lines (35 loc) · 1.46 KB
/
Memory.h
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
#pragma once
#include <unordered_map>
#include <vector>
#include <Windows.h>
#define AUTO_ASSEMBLE_TRAMPOLINE(ADDRESS, TRAMPOLINE_LENGTH, INSTRUCTIONS) \
do { \
auto allocMemory = Memory::AllocateNearbyMemory(ADDRESS, sizeof INSTRUCTIONS + 14); \
Memory::CreateTrampoline(ADDRESS, allocMemory, TRAMPOLINE_LENGTH); \
Memory::WriteInstructions(allocMemory, INSTRUCTIONS, sizeof INSTRUCTIONS, ADDRESS + TRAMPOLINE_LENGTH); \
} while (false)
class Memory
{
public:
static void PatchBytes(void* address, const char* bytes, size_t len);
static void PatchBytes(uintptr_t address, const char* bytes);
template <size_t N>
static void PatchBytes(uintptr_t address, const BYTE(&bytes)[N]);
static void NopBytes(uintptr_t address, size_t len);
static void RestoreBytes(uintptr_t address);
static void RestoreBytes(std::initializer_list<uintptr_t> address);
static uintptr_t PatternScan(uintptr_t module, const char* signature);
static void* AllocateNearbyMemory(uintptr_t address, size_t size);
static void CreateTrampoline(uintptr_t address, void* destination, size_t length);
static void RemoveTrampoline(uintptr_t address);
static void WriteInstructions(void* destination, const BYTE instructions[], size_t instructionLen, uintptr_t retAddress);
static void RestoreAllPatches();
private:
struct PatchInfo {
void* address;
std::vector<BYTE> originalBytes;
bool hasTrampoline = false;
void* trampolineDestination = nullptr;
};
static std::unordered_map<void*, PatchInfo> patches;
};