-
Notifications
You must be signed in to change notification settings - Fork 4
/
State.cs
120 lines (101 loc) · 3.72 KB
/
State.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace X360Decompiler
{
public class State
{
public List<Function> Functions = new List<Function>();
public List<Function> IgnoredCalls = new List<Function>();
public List<Function> CallIsRet = new List<Function>();
public List<Structure> Structures = new List<Structure>();
public XenonInstructions Instructions;
public XPeParser Pe;
private State()
{
CType.decompiler = this;
Instructions = new XenonInstructions(this);
Instructions.SetupTables();
}
public State(String exeName)
{
CType.decompiler = this;
Instructions = new XenonInstructions(this);
Instructions.SetupTables();
Pe = new XPeParser(exeName);
}
public static State FromFile(String xdp)
{
State state = new State();
state.LoadFromFile(xdp);
return state;
}
public void SaveToFile(String fn)
{
FileStream fs = new FileStream(fn, FileMode.Create, FileAccess.Write, FileShare.None);
BinaryFormatter bin = new BinaryFormatter();
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(Pe.FileName);
bw.Write(Functions.Count);
foreach (Function f in Functions)
{
bw.Write(f.Name);
bw.Write(f.Address);
bw.Write(f.Size);
bw.Write(f.ArgCount);
if (f.ArgCount != -1)
bin.Serialize(fs, f.Arguments);
bin.Serialize(fs, f.Returns);
}
bw.Write(IgnoredCalls.Count);
foreach (Function f in IgnoredCalls)
bw.Write(f.Address);
bw.Write(CallIsRet.Count);
foreach (Function f in CallIsRet)
bw.Write(f.Address);
bin.Serialize(fs, Structures);
fs.Close();
}
public void LoadFromFile(String fn)
{
FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader br = new BinaryReader(fs);
BinaryFormatter bin = new BinaryFormatter();
Pe = new XPeParser(br.ReadString());
int count = br.ReadInt32();
Functions = new List<Function>();
for (int i = 0; i < count; i++)
{
Function f = new Function(this, br.ReadString(), br.ReadUInt32());
f.Size = br.ReadUInt32();
f.ArgCount = br.ReadInt32();
if (f.ArgCount != -1)
f.Arguments = (List<Variable>) bin.Deserialize(fs);
f.Returns = (CType)bin.Deserialize(fs);
Functions.Add(f);
}
count = br.ReadInt32();
IgnoredCalls = new List<Function>();
for (int i = 0; i < count; i++)
{
uint addr = br.ReadUInt32();
Function f = Functions.Find(delegate(Function fnc) { return fnc.Address == addr; });
IgnoredCalls.Add(f);
}
count = br.ReadInt32();
CallIsRet = new List<Function>();
for (int i = 0; i < count; i++)
{
uint addr = br.ReadUInt32();
Function f = Functions.Find(delegate(Function fnc) { return fnc.Address == addr; });
CallIsRet.Add(f);
}
Structures = (List<Structure>)bin.Deserialize(fs);
fs.Close();
}
}
}