-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathFunctionNode.Tokenizer.cs
More file actions
353 lines (292 loc) · 8.63 KB
/
FunctionNode.Tokenizer.cs
File metadata and controls
353 lines (292 loc) · 8.63 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Linq;
using ReClassNET.Memory;
using ReClassNET.UI;
using ReClassNET.Util;
namespace ReClassNET.Nodes
{
public class FunctionNode : BaseNode
{
private IntPtr address = IntPtr.Zero;
private readonly List<string> instructions = new List<string>();
private readonly List<Tuple<string, string, string>> instructions2 = new List<Tuple<string, string, string>>();
public string Signature { get; set; } = "void function()";
public ClassNode BelongsToClass { get; set; }
private int memorySize = IntPtr.Size;
/// <summary>Size of the node in bytes.</summary>
public override int MemorySize => memorySize;
public override string GetToolTipText(HotSpot spot, MemoryBuffer memory)
{
DisassembleRemoteCode(memory, spot.Address);
return string.Join("\n", instructions);
}
public override int Draw(ViewInfo view, int x, int y)
{
Contract.Requires(view != null);
if (IsHidden)
{
return DrawHidden(view, x, y);
}
AddSelection(view, x, y, view.Font.Height);
AddDelete(view, x, y);
AddTypeDrop(view, x, y);
x += TextPadding;
x = AddIcon(view, x, y, Icons.Function, -1, HotSpotType.None);
var tx = x;
x = AddAddressOffset(view, x, y);
x = AddText(view, x, y, view.Settings.TypeColor, HotSpot.NoneId, "Function") + view.Font.Width;
x = AddText(view, x, y, view.Settings.NameColor, HotSpot.NameId, Name) + view.Font.Width;
x = AddOpenClose(view, x, y) + view.Font.Width;
x = AddComment(view, x, y);
var ptr = view.Address.Add(Offset);
DisassembleRemoteCode(view.Memory, ptr);
if (levelsOpen[view.Level])
{
y += view.Font.Height;
x = AddText(view, tx, y, view.Settings.TypeColor, HotSpot.NoneId, "Signature:") + view.Font.Width;
x = AddText(view, x, y, view.Settings.ValueColor, 0, Signature);
y += view.Font.Height;
x = AddText(view, tx, y, view.Settings.TextColor, HotSpot.NoneId, "Belongs to: ");
x = AddText(view, x, y, view.Settings.ValueColor, HotSpot.NoneId, BelongsToClass == null ? "<None>" : $"<{BelongsToClass.Name}>");
x = AddIcon(view, x, y, Icons.Change, 1, HotSpotType.ChangeType);
var minWidth = 26 * view.Font.Width;
var addressColor = Color.FromArgb(128, 128, 128);
y += 4;
foreach (var line in instructions2)
{
y += view.Font.Height;
//AddText(view, tx, y, view.Settings.NameColor, HotSpot.ReadOnlyId, line);
x = AddText(view, tx, y, view.Settings.AddressColor, HotSpot.ReadOnlyId, line.Item1) + 15;
x = Math.Max(AddText(view, x, y, view.Settings.HexColor, HotSpot.ReadOnlyId, line.Item2), x + minWidth) + 6;
/*foreach (var token in new AssemblerTokenizer().Read(line.Item3))
{
var color = Color.Black;
if (token.TokenType == TokenType.Keyword)
{
if (AssemblerTokenizer.IsKeyword(token.Value))
{
color = Color.Navy;
}
else if (AssemblerTokenizer.IsRegister(token.Value))
{
color = Color.Green;
}
}
else if (token.TokenType == TokenType.Bracket)
{
color = Color.Purple;
}
else if (token.TokenType == TokenType.Operation)
{
color = Color.Red;
}
x = AddText(view, x, y, color, HotSpot.NoneId, token.Value) + 5;
}*/
AddText(view, x, y, view.Settings.ValueColor, HotSpot.ReadOnlyId, line.Item3);
}
y += 4;
}
return y + view.Font.Height;
}
public override int CalculateHeight(ViewInfo view)
{
if (IsHidden)
{
return HiddenHeight;
}
var h = view.Font.Height;
if (levelsOpen[view.Level])
{
h += instructions.Count() * view.Font.Height;
}
return h;
}
public override void Update(HotSpot spot)
{
base.Update(spot);
if (spot.Id == 0) // Signature
{
Signature = spot.Text;
}
}
private void DisassembleRemoteCode(MemoryBuffer memory, IntPtr address)
{
Contract.Requires(memory != null);
if (this.address != address)
{
instructions.Clear();
this.address = address;
if (!address.IsNull() && memory.Process.IsValid)
{
memorySize = 0;
var disassembler = new Disassembler(memory.Process.CoreFunctions);
foreach (var instruction in disassembler.RemoteDisassembleFunction(memory.Process, address, 8192))
{
memorySize += instruction.Length;
instructions.Add($"{instruction.Address.ToString(Constants.StringHexFormat)} {instruction.Instruction}");
instructions2.Add(Tuple.Create(instruction.Address.ToString(Constants.StringHexFormat), Utils.BytesToString(instruction.Data, instruction.Length), instruction.Instruction));
}
ParentNode?.ChildHasChanged(this);
}
}
}
}
enum TokenType
{
Number,
Keyword,
Operation,
Bracket,
Register,
Text
}
class Token
{
/// <summary>The type of the token.</summary>
public TokenType TokenType { get; }
/// <summary>The value of the token.</summary>
public string Value { get; }
public Token(TokenType type, string value)
{
Contract.Requires(value != null);
TokenType = type;
Value = value;
}
public override string ToString()
{
return $"{TokenType} {Value}";
}
}
class AssemblerTokenizer
{
public List<Token> Read(string instruction)
{
Contract.Requires(instruction != null);
var tokens = new List<Token>();
var characters = instruction.ToCharArray();
for (var i = 0; i < characters.Length; ++i)
{
if (IsPartOfKeyword(characters[i], true))
{
var buffer = characters[i].ToString();
while (++i < characters.Length && IsPartOfKeyword(characters[i], false))
{
buffer += characters[i];
}
if (IsRegister(buffer))
{
tokens.Add(new Token(TokenType.Register, buffer));
}
else if (IsKeyword(buffer))
{
tokens.Add(new Token(TokenType.Keyword, buffer));
}
else
{
tokens.Add(new Token(TokenType.Text, buffer));
}
if (i == characters.Length)
{
continue;
}
}
if (IsPartOfNumber(characters[i], true))
{
var buffer = characters[i].ToString();
while (++i < characters.Length && IsPartOfNumber(characters[i], false))
{
buffer += characters[i];
}
tokens.Add(new Token(TokenType.Number, buffer));
if (i == characters.Length)
{
continue;
}
}
switch (characters[i])
{
case ' ':
continue;
case ',':
case ':':
tokens.Add(new Token(TokenType.Text, characters[i].ToString()));
break;
case '+':
case '-':
case '*':
case '/':
tokens.Add(new Token(TokenType.Operation, characters[i].ToString()));
break;
case '[':
case ']':
tokens.Add(new Token(TokenType.Bracket, characters[i].ToString()));
break;
default:
throw new Exception($"Invalid token '{characters[i]}' detected at position {i}.");
}
}
return tokens;
}
private bool IsPartOfKeyword(char character, bool isFirstCharacter)
{
return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (!isFirstCharacter && (character >= '0' && character <= '9'));
}
private bool IsPartOfNumber(char character, bool isFirstCharacter)
{
return (character >= '0' && character <= '9') || (character >= 'a' && character <= 'f') || (character >= 'A' && character <= 'F') || (!isFirstCharacter && (character == 'x' || character == 'X'));
}
private static HashSet<string> Registers = new HashSet<string>
{
"rax", "eax", "ax", "al", "ah",
"rbx", "ebx", "bx", "bl", "bh",
"rcx", "ecx", "cx", "cl", "ch",
"rdx", "edx", "dx", "dl", "dh",
"rsi", "esi", "si", "sil",
"rdi", "edi", "di", "dil",
"rbp", "ebp", "bp", "bpl",
"rsp", "esp", "sp", "spl",
"r8", "r8d", "r8w", "r8b",
"r9", "r9d", "r9w", "r9b",
"r10", "r10d", "r10w", "r10b",
"r11", "r11d", "r11w", "r11b",
"r12", "r12d", "r12w", "r12b",
"r13", "r13d", "r13w", "r13b",
"r14", "r14d", "r14w", "r14b",
"r15", "r15d", "r15w", "r15b",
"xmm0", "ymm0",
"xmm1", "ymm1",
"xmm2", "ymm2",
"xmm3", "ymm3",
"xmm4", "ymm4",
"xmm5", "ymm5",
"xmm6", "ymm6",
"xmm7", "ymm7",
"xmm8", "ymm8",
"xmm9", "ymm9",
"xmm10", "ymm10",
"xmm11", "ymm11",
"xmm12", "ymm12",
"xmm13", "ymm13",
"xmm14", "ymm14",
"xmm15", "ymm15"
};
public static bool IsRegister(string text)
{
return Registers.Contains(text);
}
private static HashSet<string> Keywords = new HashSet<string>
{
"ret", "retn",
"call",
"jl", "ja", "jb", "jbe", "je", "jz", "js", "jne", "jnz", "jns", "jae", "jmp",
"push", "pop"
};
public static bool IsKeyword(string text)
{
return Keywords.Contains(text);
}
}
}