-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
85 lines (79 loc) · 2.97 KB
/
Utils.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ROpCode = System.Reflection.Emit.OpCode;
using ROpCodes = System.Reflection.Emit.OpCodes;
using OperandType = dnlib.DotNet.Emit.OperandType;
using OpCode = dnlib.DotNet.Emit.OpCode;
using OpCodes = dnlib.DotNet.Emit.OpCodes;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using System.Reflection;
using System.Reflection.Emit;
namespace Confuser.Protections.Virt
{
class Utils
{
static Dictionary<OpCode, ROpCode> dnlibToReflection = new Dictionary<OpCode, ROpCode>();
static ROpCode ropcode;
public static ROpCode ConvertOpCode(OpCode opcode)
{
if (dnlibToReflection.TryGetValue(opcode, out ropcode))
return ropcode;
return ROpCodes.Nop;
}
public static void LoadOpCodes()
{
var refDict = new Dictionary<short, ROpCode>(0x100);
foreach (var f in typeof(ROpCodes).GetFields(BindingFlags.Static | BindingFlags.Public))
{
if (f.FieldType != typeof(ROpCode))
continue;
var ropcode = (ROpCode)f.GetValue(null);
refDict[ropcode.Value] = ropcode;
}
foreach (var f in typeof(OpCodes).GetFields(BindingFlags.Static | BindingFlags.Public))
{
if (f.FieldType != typeof(OpCode))
continue;
var opcode = (OpCode)f.GetValue(null);
if (!refDict.TryGetValue(opcode.Value, out ropcode))
continue;
dnlibToReflection[opcode] = ropcode;
}
}
}
class Utils2
{
static Dictionary<ROpCode, OpCode> reflectionToDnlib = new Dictionary<ROpCode, OpCode>();
static OpCode Opcode;
public static OpCode ConvertOpCode(ROpCode ropcode)
{
if (reflectionToDnlib.TryGetValue(ropcode, out Opcode))
return Opcode;
return OpCodes.Nop;
}
public static void LoadOpCodes()
{
var refDict = new Dictionary<short, OpCode>(0x100);
foreach (var f in typeof(OpCodes).GetFields(BindingFlags.Static | BindingFlags.Public))
{
if (f.FieldType != typeof(OpCode))
continue;
var opcode = (OpCode)f.GetValue(null);
refDict[opcode.Value] = opcode;
}
foreach (var f in typeof(ROpCodes).GetFields(BindingFlags.Static | BindingFlags.Public))
{
if (f.FieldType != typeof(ROpCode))
continue;
var ropcode = (ROpCode)f.GetValue(null);
if (!refDict.TryGetValue(ropcode.Value, out Opcode))
continue;
reflectionToDnlib[ropcode] = Opcode;
}
}
}
}