forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryBuffer.cs
More file actions
286 lines (228 loc) · 5.75 KB
/
Copy pathMemoryBuffer.cs
File metadata and controls
286 lines (228 loc) · 5.75 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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;
private byte[] historyData;
private bool hasHistory;
public int Size
{
get
{
return data.Length;
}
set
{
if (value != data.Length)
{
data = new byte[value];
historyData = new byte[value];
hasHistory = false;
}
}
}
public int Offset { get; set; }
[ContractInvariantMethod]
private void ObjectInvariants()
{
Contract.Invariant(data != null);
Contract.Invariant(historyData != null);
}
public MemoryBuffer()
{
Contract.Ensures(data != null);
Contract.Ensures(historyData != null);
data = new byte[0];
historyData = new byte[0];
}
public MemoryBuffer(MemoryBuffer other)
{
Contract.Requires(other != null);
Contract.Ensures(data != null);
Contract.Ensures(historyData != null);
data = other.data;
historyData = other.historyData;
hasHistory = other.hasHistory;
}
public MemoryBuffer Clone()
{
Contract.Ensures(Contract.Result<MemoryBuffer>() != null);
return new MemoryBuffer(this)
{
Offset = Offset,
Process = Process
};
}
public void Update(IntPtr address)
{
Update(address, true);
}
public void Update(IntPtr address, bool setHistory)
{
Contract.Requires(Process != null);
if (setHistory)
{
Array.Copy(data, historyData, data.Length);
hasHistory = true;
}
Process.ReadRemoteMemoryIntoBuffer(address, ref data);
}
public byte ReadByte(IntPtr offset)
{
Contract.Requires(offset.ToInt32() >= 0);
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 buffer = new byte[length];
ReadBytes(offset, buffer);
return buffer;
}
public void ReadBytes(IntPtr offset, byte[] buffer)
{
Contract.Requires(buffer != null);
ReadBytes(offset.ToInt32(), buffer);
}
public void ReadBytes(int offset, byte[] buffer)
{
Contract.Requires(offset >= 0);
Contract.Requires(buffer != null);
if (Offset + offset + buffer.Length > data.Length)
{
return;
}
Array.Copy(data, Offset + offset, buffer, 0, buffer.Length);
}
public T ReadObject<T>(IntPtr offset) where T : struct
{
Contract.Requires(offset.ToInt32() >= 0);
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 = Marshal.PtrToStructure(handle.AddrOfPinnedObject() + Offset + offset, typeof(T));
handle.Free();
if (obj == null)
{
return default(T);
}
return (T)obj;
}
public string ReadPrintableASCIIString(IntPtr offset, int length)
{
Contract.Requires(offset.ToInt32() >= 0);
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 = Math.Max(data.Length - Offset - offset, 0);
}
if (length <= 0)
{
return string.Empty;
}
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(offset.ToInt32() >= 0);
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(offset.ToInt32() >= 0);
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(offset.ToInt32() >= 0);
Contract.Requires(length >= 0);
Contract.Ensures(Contract.Result<string>() != null);
return ReadString(Encoding.UTF32, offset.ToInt32(), length);
}
public bool HasChanged(IntPtr offset, int length)
{
return HasChanged(offset.ToInt32(), length);
}
public bool HasChanged(int offset, int length)
{
if (hasHistory)
{
return false;
}
if (Offset + offset + length > data.Length)
{
return false;
}
var end = Offset + offset + length;
for (var i = Offset + offset; i < end; ++i)
{
if (data[i] != historyData[i])
{
return true;
}
}
return false;
}
}
}