-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathMemoryAccessor.cpp
More file actions
126 lines (115 loc) · 3.5 KB
/
Copy pathMemoryAccessor.cpp
File metadata and controls
126 lines (115 loc) · 3.5 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "MemoryAccessor.h"
void MemoryAccessor::Close() {
if (hDevice != INVALID_HANDLE_VALUE) {
CloseHandle(hDevice);
hDevice = INVALID_HANDLE_VALUE;
}
}
bool MemoryAccessor::Open()
{
hDevice = CreateFile(
L"\\\\.\\RTCore64", // Device name, this depends on the actual device name
GENERIC_READ | GENERIC_WRITE, // Access rights
0, // Share mode, 0 means not shared
NULL, // Security attributes
OPEN_EXISTING, // Open an existing device
FILE_ATTRIBUTE_NORMAL, // Attributes and flags
NULL // Template handle
);
if (hDevice == INVALID_HANDLE_VALUE) {
return false;
}
return true;
}
bool MemoryAccessor::ReadUint8(uintptr_t address, uint8_t* buffer)
{
MemoryOperation operation{ 0 };
operation.address = address;
operation.size = sizeof(uint8_t);
if (!DeviceIoControl(hDevice, 0x80002048, &operation, sizeof(operation), &operation, sizeof(operation), NULL, NULL))
{
return false;
}
*buffer = operation.data;
return true;
}
bool MemoryAccessor::ReadUint16(uintptr_t address, uint16_t* buffer)
{
MemoryOperation operation{ 0 };
operation.address = address;
operation.size = sizeof(uint16_t);
if (!DeviceIoControl(hDevice, 0x80002048, &operation, sizeof(operation), &operation, sizeof(operation), NULL, NULL))
{
return false;
}
*buffer = operation.data;
return true;
}
bool MemoryAccessor::ReadUint32(uintptr_t address, uint32_t* buffer)
{
MemoryOperation operation{ 0 };
operation.address = address;
operation.size = sizeof(uint32_t);
if (!DeviceIoControl(hDevice, 0x80002048, &operation, sizeof(operation), &operation, sizeof(operation), NULL, NULL))
{
return false;
}
*buffer = operation.data;
return true;
}
bool MemoryAccessor::ReadMemory(uintptr_t address, void* buffer, size_t size) {
for (size_t i = 0; i < size; ++i) {
if (!ReadUint8(address + i, static_cast<uint8_t*>(buffer) + i)) {
return false; // ¶Áȡʧ°Ü
}
}
return true;
}
bool MemoryAccessor::WriteUint8(uintptr_t address, uint8_t value)
{
MemoryOperation operation{ 0 };
operation.address = address;
operation.size = sizeof(uint8_t);
operation.data = value;
if (!DeviceIoControl(hDevice, 0x8000204C, &operation, sizeof(operation), &operation, sizeof(operation), NULL, NULL))
{
return false;
}
return true;
}
bool MemoryAccessor::WriteUint16(uintptr_t address, uint16_t value)
{
MemoryOperation operation{ 0 };
operation.address = address;
operation.size = sizeof(uint16_t);
operation.data = value;
if (!DeviceIoControl(hDevice, 0x8000204C, &operation, sizeof(operation), &operation, sizeof(operation), NULL, NULL))
{
return false;
}
return true;
}
bool MemoryAccessor::WriteUint32(uintptr_t address, uint32_t value)
{
MemoryOperation operation{ 0 };
operation.address = address;
operation.size = sizeof(uint32_t);
operation.data = value;
if (!DeviceIoControl(hDevice, 0x8000204C, &operation, sizeof(operation), &operation, sizeof(operation), NULL, NULL))
{
return false;
}
return true;
}
bool MemoryAccessor::WriteMemory(uintptr_t address, const void* buffer, size_t size)
{
const uint8_t* data = static_cast<const uint8_t*>(buffer);
for (size_t i = 0; i < size; ++i)
{
if (!WriteUint8(address + i, data[i]))
{
return false; // дÈëʧ°Ü
}
}
return true;
}