aboutsummaryrefslogtreecommitdiff
path: root/primedev/core/memory.h
blob: a978963e4461d140fe0232c1d8f585517b630a00 (plain)
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
83
84
85
86
87
88
89
90
#pragma once

class CMemoryAddress
{
public:
	uintptr_t m_nAddress;

public:
	CMemoryAddress();
	CMemoryAddress(const uintptr_t nAddress);
	CMemoryAddress(const void* pAddress);

	// operators
	operator uintptr_t() const;
	operator void*() const;
	operator bool() const;

	bool operator==(const CMemoryAddress& other) const;
	bool operator!=(const CMemoryAddress& other) const;
	bool operator==(const uintptr_t& addr) const;
	bool operator!=(const uintptr_t& addr) const;

	CMemoryAddress operator+(const CMemoryAddress& other) const;
	CMemoryAddress operator-(const CMemoryAddress& other) const;
	CMemoryAddress operator+(const uintptr_t& other) const;
	CMemoryAddress operator-(const uintptr_t& other) const;
	CMemoryAddress operator*() const;

	template <typename T> T RCast()
	{
		return reinterpret_cast<T>(m_nAddress);
	}

	// traversal
	CMemoryAddress Offset(const uintptr_t nOffset) const;
	CMemoryAddress Deref(const int nNumDerefs = 1) const;

	// patching
	void Patch(const uint8_t* pBytes, const size_t nSize);
	void Patch(const std::initializer_list<uint8_t> bytes);
	void Patch(const char* pBytes);
	void NOP(const size_t nSize);

	bool IsMemoryReadable(const size_t nSize);
};

// based on https://github.com/Mauler125/r5sdk/blob/master/r5dev/public/include/module.h
class CModule : public CMemoryAddress
{
public:
	struct ModuleSections_t
	{
		ModuleSections_t(void) = default;
		ModuleSections_t(const std::string& svSectionName, uintptr_t pSectionBase, size_t nSectionSize)
			: m_svSectionName(svSectionName), m_pSectionBase(pSectionBase), m_nSectionSize(nSectionSize)
		{
		}

		bool IsSectionValid(void) const
		{
			return m_nSectionSize != 0;
		}

		std::string m_svSectionName; // Name of section.
		uintptr_t m_pSectionBase {}; // Start address of section.
		size_t m_nSectionSize {}; // Size of section.
	};

	ModuleSections_t m_ExecutableCode;
	ModuleSections_t m_ExceptionTable;
	ModuleSections_t m_RunTimeData;
	ModuleSections_t m_ReadOnlyData;

private:
	std::string m_svModuleName;
	uintptr_t m_pModuleBase {};
	DWORD m_nModuleSize {};
	IMAGE_NT_HEADERS64* m_pNTHeaders = nullptr;
	IMAGE_DOS_HEADER* m_pDOSHeader = nullptr;
	std::vector<ModuleSections_t> m_vModuleSections;

public:
	CModule() = delete; // no default, we need a module name
	CModule(const HMODULE pModule);
	CModule(const char* pModuleName);

	CMemoryAddress GetExport(const char* pExportName);
	CMemoryAddress FindPattern(const uint8_t* pPattern, const char* pMask);
	CMemoryAddress FindPattern(const char* pPattern);
};