-
Notifications
You must be signed in to change notification settings - Fork 214
/
binaryviewtype.cpp
376 lines (288 loc) · 10.1 KB
/
binaryviewtype.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright (c) 2015-2024 Vector 35 Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "binaryninjaapi.h"
using namespace BinaryNinja;
using namespace std;
BNBinaryView* BinaryViewType::CreateCallback(void* ctxt, BNBinaryView* data)
{
CallbackRef<BinaryViewType> type(ctxt);
Ref<BinaryView> view = new BinaryView(BNNewViewReference(data));
Ref<BinaryView> result = type->Create(view);
if (!result)
return nullptr;
return BNNewViewReference(result->GetObject());
}
BNBinaryView* BinaryViewType::ParseCallback(void* ctxt, BNBinaryView* data)
{
CallbackRef<BinaryViewType> type(ctxt);
Ref<BinaryView> view = new BinaryView(BNNewViewReference(data));
Ref<BinaryView> result = type->Parse(view);
if (!result)
return nullptr;
return BNNewViewReference(result->GetObject());
}
bool BinaryViewType::IsValidCallback(void* ctxt, BNBinaryView* data)
{
CallbackRef<BinaryViewType> type(ctxt);
Ref<BinaryView> view = new BinaryView(BNNewViewReference(data));
return type->IsTypeValidForData(view);
}
bool BinaryViewType::IsDeprecatedCallback(void* ctxt)
{
CallbackRef<BinaryViewType> type(ctxt);
return type->IsDeprecated();
}
bool BinaryViewType::IsForceLoadableCallback(void* ctxt)
{
CallbackRef<BinaryViewType> type(ctxt);
return type->IsForceLoadable();
}
BNSettings* BinaryViewType::GetSettingsCallback(void* ctxt, BNBinaryView* data)
{
CallbackRef<BinaryViewType> type(ctxt);
Ref<BinaryView> view = new BinaryView(BNNewViewReference(data));
Ref<Settings> result = type->GetLoadSettingsForData(view);
if (!result)
return nullptr;
return BNNewSettingsReference(result->GetObject());
}
BinaryViewType::BinaryViewType(BNBinaryViewType* type)
{
m_object = type;
}
BinaryViewType::BinaryViewType(const string& name, const string& longName) :
m_nameForRegister(name), m_longNameForRegister(longName)
{
m_object = nullptr;
}
void BinaryViewType::Register(BinaryViewType* type)
{
BNCustomBinaryViewType callbacks;
callbacks.context = type;
callbacks.create = CreateCallback;
callbacks.parse = ParseCallback;
callbacks.isValidForData = IsValidCallback;
callbacks.isDeprecated = IsDeprecatedCallback;
callbacks.isForceLoadable = IsForceLoadableCallback;
callbacks.getLoadSettingsForData = GetSettingsCallback;
type->AddRefForRegistration();
type->m_object =
BNRegisterBinaryViewType(type->m_nameForRegister.c_str(), type->m_longNameForRegister.c_str(), &callbacks);
}
Ref<BinaryViewType> BinaryViewType::GetByName(const string& name)
{
BNBinaryViewType* type = BNGetBinaryViewTypeByName(name.c_str());
if (!type)
return nullptr;
return new CoreBinaryViewType(type);
}
vector<Ref<BinaryViewType>> BinaryViewType::GetViewTypes()
{
BNBinaryViewType** types;
size_t count;
types = BNGetBinaryViewTypes(&count);
vector<Ref<BinaryViewType>> result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
result.push_back(new CoreBinaryViewType(types[i]));
BNFreeBinaryViewTypeList(types);
return result;
}
vector<Ref<BinaryViewType>> BinaryViewType::GetViewTypesForData(BinaryView* data)
{
BNBinaryViewType** types;
size_t count;
types = BNGetBinaryViewTypesForData(data->GetObject(), &count);
vector<Ref<BinaryViewType>> result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
result.push_back(new CoreBinaryViewType(types[i]));
BNFreeBinaryViewTypeList(types);
return result;
}
void BinaryViewType::RegisterArchitecture(const string& name, uint32_t id, BNEndianness endian, Architecture* arch)
{
Ref<BinaryViewType> type = BinaryViewType::GetByName(name);
if (!type)
return;
type->RegisterArchitecture(id, endian, arch);
}
void BinaryViewType::RegisterArchitecture(uint32_t id, BNEndianness endian, Architecture* arch)
{
BNRegisterArchitectureForViewType(m_object, id, endian, arch->GetObject());
}
Ref<Architecture> BinaryViewType::GetArchitecture(uint32_t id, BNEndianness endian)
{
BNArchitecture* arch = BNGetArchitectureForViewType(m_object, id, endian);
if (!arch)
return nullptr;
return new CoreArchitecture(arch);
}
void BinaryViewType::RegisterPlatform(const string& name, uint32_t id, Architecture* arch, Platform* platform)
{
Ref<BinaryViewType> type = BinaryViewType::GetByName(name);
if (!type)
return;
type->RegisterPlatform(id, arch, platform);
}
void BinaryViewType::RegisterDefaultPlatform(const string& name, Architecture* arch, Platform* platform)
{
Ref<BinaryViewType> type = BinaryViewType::GetByName(name);
if (!type)
return;
type->RegisterDefaultPlatform(arch, platform);
}
void BinaryViewType::RegisterPlatform(uint32_t id, Architecture* arch, Platform* platform)
{
BNRegisterPlatformForViewType(m_object, id, arch->GetObject(), platform->GetObject());
}
void BinaryViewType::RegisterDefaultPlatform(Architecture* arch, Platform* platform)
{
BNRegisterDefaultPlatformForViewType(m_object, arch->GetObject(), platform->GetObject());
}
Ref<Platform> BinaryViewType::GetPlatform(uint32_t id, Architecture* arch)
{
BNPlatform* platform = BNGetPlatformForViewType(m_object, id, arch->GetObject());
if (!platform)
return nullptr;
return new Platform(platform);
}
void BinaryViewType::RegisterPlatformRecognizer(uint64_t id, BNEndianness endian,
const std::function<Ref<Platform>(BinaryView* view, Metadata* metadata)>& callback)
{
PlatformRecognizerFunction* ctxt = new PlatformRecognizerFunction;
ctxt->action = callback;
BNRegisterPlatformRecognizerForViewType(m_object, id, endian, PlatformRecognizerCallback, ctxt);
}
Ref<Platform> BinaryViewType::RecognizePlatform(uint64_t id, BNEndianness endian, BinaryView* view, Metadata* metadata)
{
BNPlatform* platform =
BNRecognizePlatformForViewType(m_object, id, endian, view->GetObject(), metadata->GetObject());
if (!platform)
return nullptr;
return new Platform(platform);
}
string BinaryViewType::GetName()
{
char* contents = BNGetBinaryViewTypeName(m_object);
string result = contents;
BNFreeString(contents);
return result;
}
string BinaryViewType::GetLongName()
{
char* contents = BNGetBinaryViewTypeLongName(m_object);
string result = contents;
BNFreeString(contents);
return result;
}
bool BinaryViewType::IsDeprecated()
{
return false;
}
bool BinaryViewType::IsForceLoadable()
{
return false;
}
void BinaryViewType::RegisterBinaryViewFinalizationEvent(const function<void(BinaryView* view)>& callback)
{
BinaryViewEvent* event = new BinaryViewEvent;
event->action = callback;
BNRegisterBinaryViewEvent(BinaryViewFinalizationEvent, BinaryViewEventCallback, event);
}
void BinaryViewType::RegisterBinaryViewInitialAnalysisCompletionEvent(const function<void(BinaryView* view)>& callback)
{
BinaryViewEvent* event = new BinaryViewEvent;
event->action = callback;
BNRegisterBinaryViewEvent(BinaryViewInitialAnalysisCompletionEvent, BinaryViewEventCallback, event);
}
void BinaryViewType::BinaryViewEventCallback(void* ctxt, BNBinaryView* view)
{
BinaryViewEvent* event = (BinaryViewEvent*)ctxt;
Ref<BinaryView> viewObject = new BinaryView(BNNewViewReference(view));
event->action(viewObject);
}
BNPlatform* BinaryViewType::PlatformRecognizerCallback(void* ctxt, BNBinaryView* view, BNMetadata* metadata)
{
PlatformRecognizerFunction* callback = (PlatformRecognizerFunction*)ctxt;
Ref<BinaryView> viewObject = new BinaryView(BNNewViewReference(view));
Ref<Metadata> metadataObject = new Metadata(BNNewMetadataReference(metadata));
Ref<Platform> result = callback->action(viewObject, metadataObject);
if (!result)
return nullptr;
return BNNewPlatformReference(result->GetObject());
}
Ref<BinaryView> BinaryViewType::Parse(BinaryView* data)
{
Ref<BinaryView> viewRef;
// Create ephemeral BinaryView to generate information for preview
if (data && (GetName() != data->GetTypeName()))
{
viewRef = Create(data);
if (!viewRef || !viewRef->Init())
LogError("View type '%s' could not be created", GetName().c_str());
}
return viewRef;
}
Ref<Settings> BinaryViewType::GetLoadSettingsForData(BinaryView* data)
{
return GetDefaultLoadSettingsForData(data);
}
Ref<Settings> BinaryViewType::GetDefaultLoadSettingsForData(BinaryView* data)
{
BNSettings* settings = BNGetBinaryViewDefaultLoadSettingsForData(m_object, data->GetObject());
if (!settings)
return nullptr;
return new Settings(settings);
}
CoreBinaryViewType::CoreBinaryViewType(BNBinaryViewType* type) : BinaryViewType(type) {}
Ref<BinaryView> CoreBinaryViewType::Create(BinaryView* data)
{
BNBinaryView* view = BNCreateBinaryViewOfType(m_object, data->GetObject());
if (!view)
return nullptr;
return new BinaryView(view);
}
Ref<BinaryView> CoreBinaryViewType::Parse(BinaryView* data)
{
BNBinaryView* view = BNParseBinaryViewOfType(m_object, data->GetObject());
if (!view)
return nullptr;
return new BinaryView(view);
}
bool CoreBinaryViewType::IsTypeValidForData(BinaryView* data)
{
return BNIsBinaryViewTypeValidForData(m_object, data->GetObject());
}
bool CoreBinaryViewType::IsDeprecated()
{
return BNIsBinaryViewTypeDeprecated(m_object);
}
bool CoreBinaryViewType::IsForceLoadable()
{
return BNIsBinaryViewTypeForceLoadable(m_object);
}
Ref<Settings> CoreBinaryViewType::GetLoadSettingsForData(BinaryView* data)
{
BNSettings* settings = BNGetBinaryViewLoadSettingsForData(m_object, data->GetObject());
if (!settings)
return nullptr;
return new Settings(settings);
}