-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathDistormHelper.cpp
More file actions
184 lines (154 loc) · 4.1 KB
/
DistormHelper.cpp
File metadata and controls
184 lines (154 loc) · 4.1 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
#include "DistormHelper.hpp"
#include <distorm.h>
#include <mnemonics.h>
extern "C"
{
#include <../src/instructions.h>
}
bool AreOperandsStatic(const _DInst &instruction, const int prefixLength)
{
const auto fc = META_GET_FC(instruction.meta);
if (fc == FC_UNC_BRANCH || fc == FC_CND_BRANCH)
{
if (instruction.size - prefixLength < 5)
{
return true;
}
}
const auto ops = instruction.ops;
for (auto i = 0; i < OPERANDS_NO; i++)
{
switch (ops[i].type)
{
case O_NONE:
case O_REG:
case O_IMM1:
case O_IMM2:
continue;
case O_IMM:
if (ops[i].size < 32)
{
continue;
}
return false;
case O_DISP:
case O_SMEM:
case O_MEM:
if (instruction.dispSize < 32)
{
continue;
}
#ifdef RECLASSNET64
if (ops[i].index == R_RIP)
{
continue;
}
#endif
return false;
case O_PC:
case O_PTR:
return false;
}
}
return true;
}
_CodeInfo CreateCodeInfo(const uint8_t* address, int length, const _OffsetType virtualAddress)
{
_CodeInfo info = {};
info.codeOffset = virtualAddress;
info.code = address;
info.codeLen = length;
info.features = DF_NONE;
#ifdef RECLASSNET64
info.dt = Decode64Bits;
#else
info.dt = Decode32Bits;
#endif
return info;
}
int GetStaticInstructionBytes(const _DInst &instruction, const uint8_t *data)
{
auto info = CreateCodeInfo(data, instruction.size, reinterpret_cast<_OffsetType>(data));
_PrefixState ps = {};
int isPrefixed;
inst_lookup(&info, &ps, &isPrefixed);
if (AreOperandsStatic(instruction, ps.count))
{
return instruction.size;
}
return instruction.size - info.codeLen - ps.count;
}
void FillInstructionData(const _CodeInfo& info, const RC_Pointer address, const _DInst& instruction, const bool determineStaticInstructionBytes, InstructionData* data)
{
data->Address = reinterpret_cast<RC_Pointer>(instruction.addr);
data->Length = instruction.size;
std::memcpy(data->Data, address, instruction.size);
data->StaticInstructionBytes = -1;
if (instruction.flags == FLAG_NOT_DECODABLE)
{
std::memcpy(data->Instruction, L"???", sizeof(RC_UnicodeChar) * 3);
}
else
{
_DecodedInst instructionInfo = {};
distorm_format(&info, &instruction, &instructionInfo);
MultiByteToUnicode(
reinterpret_cast<const char*>(instructionInfo.mnemonic.p),
data->Instruction,
instructionInfo.mnemonic.length
);
if (instructionInfo.operands.length != 0)
{
data->Instruction[instructionInfo.mnemonic.length] = ' ';
MultiByteToUnicode(
reinterpret_cast<const char*>(instructionInfo.operands.p),
0,
data->Instruction,
instructionInfo.mnemonic.length + 1,
std::min<int>(64 - 1 - instructionInfo.mnemonic.length, instructionInfo.operands.length)
);
}
if (determineStaticInstructionBytes)
{
data->StaticInstructionBytes = GetStaticInstructionBytes(
instruction,
reinterpret_cast<const uint8_t*>(address)
);
}
}
}
bool DisassembleInstructionsImpl(const RC_Pointer address, const RC_Size length, const RC_Pointer virtualAddress, const bool determineStaticInstructionBytes, EnumerateInstructionCallback callback)
{
auto info = CreateCodeInfo(static_cast<const uint8_t*>(address), static_cast<int>(length), reinterpret_cast<_OffsetType>(virtualAddress));
const unsigned MaxInstructions = 50;
_DInst decodedInstructions[MaxInstructions] = {};
unsigned count = 0;
auto instructionAddress = static_cast<uint8_t*>(address);
while (true)
{
const auto res = distorm_decompose(&info, decodedInstructions, MaxInstructions, &count);
if (res == DECRES_INPUTERR)
{
return false;
}
for (auto i = 0u; i < count; ++i)
{
const auto& instruction = decodedInstructions[i];
InstructionData data = {};
FillInstructionData(info, instructionAddress, instruction, determineStaticInstructionBytes, &data);
if (callback(&data) == false)
{
return true;
}
instructionAddress += instruction.size;
}
if (res == DECRES_SUCCESS || count == 0)
{
return true;
}
const auto offset = static_cast<unsigned>(decodedInstructions[count - 1].addr + decodedInstructions[count - 1].size - info.codeOffset);
info.codeOffset += offset;
info.code += offset;
info.codeLen -= offset;
}
}