forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demangle.cpp
129 lines (109 loc) · 3.64 KB
/
demangle.cpp
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
#include "binaryninjaapi.h"
#include <string>
using namespace std;
namespace BinaryNinja {
bool DemangleMS(Architecture* arch, const std::string& mangledName, Type** outType, QualifiedName& outVarName,
const Ref<BinaryView>& view)
{
const bool simplify = Settings::Instance()->Get<bool>("analysis.types.TemplateSimplifier", view);
return DemangleMS(arch, mangledName, outType, outVarName, simplify);
}
bool DemangleMS(Architecture* arch, const std::string& mangledName, Type** outType, QualifiedName& outVarName,
const bool simplify)
{
BNType* localType = nullptr;
char** localVarName = nullptr;
size_t localSize = 0;
if (!BNDemangleMS(arch->GetObject(), mangledName.c_str(), &localType, &localVarName, &localSize, simplify))
return false;
if (!localType)
return false;
*outType = new Type(BNNewTypeReference(localType));
for (size_t i = 0; i < localSize; i++)
{
outVarName.push_back(localVarName[i]);
BNFreeString(localVarName[i]);
}
delete[] localVarName;
return true;
}
bool DemangleGNU3(Ref<Architecture> arch, const std::string& mangledName, Type** outType, QualifiedName& outVarName,
const Ref<BinaryView>& view)
{
const bool simplify = Settings::Instance()->Get<bool>("analysis.types.TemplateSimplifier", view);
return DemangleGNU3(arch, mangledName, outType, outVarName, simplify);
}
bool DemangleGNU3(Ref<Architecture> arch, const std::string& mangledName, Type** outType, QualifiedName& outVarName,
const bool simplify)
{
BNType* localType;
char** localVarName = nullptr;
size_t localSize = 0;
if (!BNDemangleGNU3(arch->GetObject(), mangledName.c_str(), &localType, &localVarName, &localSize, simplify))
return false;
if (!localType)
return false;
*outType = new Type(BNNewTypeReference(localType));
for (size_t i = 0; i < localSize; i++)
{
outVarName.push_back(localVarName[i]);
BNFreeString(localVarName[i]);
}
delete[] localVarName;
return true;
}
string SimplifyName::to_string(const string& input)
{
return (string)SimplifyName(input, SimplifierDest::str, true);
}
string SimplifyName::to_string(const QualifiedName& input)
{
return (string)SimplifyName(input.GetString(), SimplifierDest::str, true);
}
QualifiedName SimplifyName::to_qualified_name(const string& input, bool simplify)
{
return SimplifyName(input, SimplifierDest::fqn, simplify).operator QualifiedName();
}
QualifiedName SimplifyName::to_qualified_name(const QualifiedName& input)
{
return SimplifyName(input.GetString(), SimplifierDest::fqn, true).operator QualifiedName();
}
SimplifyName::SimplifyName(const string& input, const SimplifierDest dest, const bool simplify) :
m_rust_string(nullptr), m_rust_array(nullptr), m_length(0)
{
if (dest == SimplifierDest::str)
m_rust_string = BNRustSimplifyStrToStr(input.c_str());
else
m_rust_array = const_cast<const char**>(BNRustSimplifyStrToFQN(input.c_str(), simplify));
}
SimplifyName::~SimplifyName()
{
if (m_rust_string)
BNRustFreeString(m_rust_string);
if (m_rust_array)
{
if (m_length == 0)
{
// Should never reach here
LogWarn("Deallocating SimplifyName without having been used; Likely misuse of API.\n");
uint64_t index = 0;
while (m_rust_array[index][0] != 0x0)
++index;
m_length = index + 1;
}
BNRustFreeStringArray(m_rust_array, m_length);
}
}
SimplifyName::operator string() const { return string(m_rust_string); }
SimplifyName::operator QualifiedName()
{
QualifiedName result;
uint64_t index = 0;
while (m_rust_array[index][0] != 0x0)
{
result.push_back(string(m_rust_array[index++]));
}
m_length = index;
return result;
}
} // namespace BinaryNinja