forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.cs
More file actions
192 lines (154 loc) · 3.94 KB
/
Copy pathMemory.cs
File metadata and controls
192 lines (154 loc) · 3.94 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using System.Text;
using ReClassNET.Util;
namespace ReClassNET.Memory
{
public class MemoryBuffer
{
public RemoteProcess Process { get; set; }
private byte[] data = new byte[0];
public int Size
{
get
{
return data.Length;
}
set
{
if (value != data.Length)
{
data = new byte[value];
}
}
}
public int Offset { get; set; }
public MemoryBuffer()
{
Contract.Ensures(data != null);
data = new byte[0];
}
public MemoryBuffer(MemoryBuffer other)
{
Contract.Requires(other != null);
Contract.Ensures(data != null);
data = other.data;
}
public MemoryBuffer Clone()
{
Contract.Ensures(Contract.Result<MemoryBuffer>() != null);
return new MemoryBuffer(this)
{
Offset = Offset,
Process = Process
};
}
public void Update(IntPtr address)
{
Process.ReadRemoteMemoryIntoBuffer(address, ref data);
}
public byte ReadByte(IntPtr offset)
{
return ReadByte(offset.ToInt32());
}
public byte ReadByte(int offset)
{
Contract.Requires(offset >= 0);
if (Offset + offset > data.Length)
{
return 0;
}
return data[Offset + offset];
}
public byte[] ReadBytes(int offset, int length)
{
Contract.Requires(offset >= 0);
Contract.Requires(length >= 0);
var bytes = new byte[length];
if (Offset + offset + length > data.Length)
{
return bytes;
}
Array.Copy(data, Offset + offset, bytes, 0, length);
return bytes;
}
public T ReadObject<T>(IntPtr offset) where T : struct
{
return ReadObject<T>(offset.ToInt32());
}
public T ReadObject<T>(int offset) where T : struct
{
Contract.Requires(offset >= 0);
if (Offset + offset + Marshal.SizeOf(typeof(T)) > data.Length)
{
return default(T);
}
var handle = GCHandle.Alloc(data, GCHandleType.Pinned);
var obj = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject() + Offset + offset, typeof(T));
handle.Free();
return obj;
}
public string ReadPrintableASCIIString(IntPtr offset, int length)
{
Contract.Requires(length >= 0);
Contract.Ensures(Contract.Result<string>() != null);
return ReadPrintableASCIIString(offset.ToInt32(), length);
}
public string ReadPrintableASCIIString(int offset, int length)
{
Contract.Requires(offset >= 0);
Contract.Requires(length >= 0);
Contract.Ensures(Contract.Result<string>() != null);
if (Offset + offset + length > data.Length)
{
length = data.Length - Offset - offset;
}
var sb = new StringBuilder(length);
for (var i = 0; i < length; ++i)
{
var c = (char)data[Offset + offset + i];
sb.Append(c.IsPrintable() ? c : '.');
}
return sb.ToString();
}
private string ReadString(Encoding encoding, int offset, int length)
{
Contract.Requires(encoding != null);
Contract.Requires(offset >= 0);
Contract.Requires(length >= 0);
Contract.Ensures(Contract.Result<string>() != null);
if (Offset + offset + length > data.Length)
{
length = data.Length - Offset - offset;
}
var sb = new StringBuilder(encoding.GetString(data, offset, length));
for (var i = 0; i < sb.Length; ++i)
{
if (!sb[i].IsPrintable())
{
sb[i] = '.';
}
}
return sb.ToString();
}
public string ReadUTF8String(IntPtr offset, int length)
{
Contract.Requires(length >= 0);
Contract.Ensures(Contract.Result<string>() != null);
return ReadString(Encoding.UTF8, offset.ToInt32(), length);
}
public string ReadUTF16String(IntPtr offset, int length)
{
Contract.Requires(length >= 0);
Contract.Ensures(Contract.Result<string>() != null);
return ReadString(Encoding.Unicode, offset.ToInt32(), length);
}
public string ReadUTF32String(IntPtr offset, int length)
{
Contract.Requires(length >= 0);
Contract.Ensures(Contract.Result<string>() != null);
return ReadString(Encoding.UTF32, offset.ToInt32(), length);
}
}
}