forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryninjaapi.h
2845 lines (2351 loc) · 109 KB
/
binaryninjaapi.h
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2015-2017 Vector 35 LLC
//
// 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.
#pragma once
#ifdef WIN32
#define NOMINMAX
#include <windows.h>
#endif
#include <stddef.h>
#include <string>
#include <vector>
#include <map>
#include <exception>
#include <functional>
#include <set>
#include <mutex>
#include "binaryninjacore.h"
#include "json/json.h"
#ifdef _MSC_VER
#define NOEXCEPT
#else
#define NOEXCEPT noexcept
#endif
namespace BinaryNinja
{
class RefCountObject
{
public:
int m_refs;
RefCountObject(): m_refs(0) {}
virtual ~RefCountObject() {}
RefCountObject* GetObject() { return this; }
void AddRef()
{
#ifdef WIN32
InterlockedIncrement((LONG*)&m_refs);
#else
__sync_fetch_and_add(&m_refs, 1);
#endif
}
void Release()
{
#ifdef WIN32
if (InterlockedDecrement((LONG*)&m_refs) == 0)
delete this;
#else
if (__sync_fetch_and_add(&m_refs, -1) == 1)
delete this;
#endif
}
};
template <class T, T* (*AddObjectReference)(T*), void (*FreeObjectReference)(T*)>
class CoreRefCountObject
{
void AddRefInternal()
{
#ifdef WIN32
InterlockedIncrement((LONG*)&m_refs);
#else
__sync_fetch_and_add(&m_refs, 1);
#endif
}
void ReleaseInternal()
{
#ifdef WIN32
if (InterlockedDecrement((LONG*)&m_refs) == 0)
delete this;
#else
if (__sync_fetch_and_add(&m_refs, -1) == 1)
delete this;
#endif
}
public:
int m_refs;
T* m_object;
CoreRefCountObject(): m_refs(0), m_object(nullptr) {}
virtual ~CoreRefCountObject() {}
T* GetObject() const { return m_object; }
void AddRef()
{
if (m_object && (m_refs != 0))
AddObjectReference(m_object);
AddRefInternal();
}
void Release()
{
if (m_object)
FreeObjectReference(m_object);
ReleaseInternal();
}
void AddRefForRegistration()
{
AddRefInternal();
}
void ReleaseForRegistration()
{
m_object = nullptr;
ReleaseInternal();
}
};
template <class T>
class StaticCoreRefCountObject
{
void AddRefInternal()
{
#ifdef WIN32
InterlockedIncrement((LONG*)&m_refs);
#else
__sync_fetch_and_add(&m_refs, 1);
#endif
}
void ReleaseInternal()
{
#ifdef WIN32
if (InterlockedDecrement((LONG*)&m_refs) == 0)
delete this;
#else
if (__sync_fetch_and_add(&m_refs, -1) == 1)
delete this;
#endif
}
public:
int m_refs;
T* m_object;
StaticCoreRefCountObject(): m_refs(0), m_object(nullptr) {}
virtual ~StaticCoreRefCountObject() {}
T* GetObject() const { return m_object; }
void AddRef()
{
AddRefInternal();
}
void Release()
{
ReleaseInternal();
}
void AddRefForRegistration()
{
AddRefInternal();
}
};
template <class T>
class Ref
{
T* m_obj;
public:
Ref<T>(): m_obj(NULL)
{
}
Ref<T>(T* obj): m_obj(obj)
{
if (m_obj)
m_obj->AddRef();
}
Ref<T>(const Ref<T>& obj): m_obj(obj.m_obj)
{
if (m_obj)
m_obj->AddRef();
}
~Ref<T>()
{
if (m_obj)
m_obj->Release();
}
Ref<T>& operator=(const Ref<T>& obj)
{
T* oldObj = m_obj;
m_obj = obj.m_obj;
if (m_obj)
m_obj->AddRef();
if (oldObj)
oldObj->Release();
return *this;
}
Ref<T>& operator=(T* obj)
{
T* oldObj = m_obj;
m_obj = obj;
if (m_obj)
m_obj->AddRef();
if (oldObj)
oldObj->Release();
return *this;
}
operator T*() const
{
return m_obj;
}
T* operator->() const
{
return m_obj;
}
T& operator*() const
{
return *m_obj;
}
bool operator!() const
{
return m_obj == NULL;
}
bool operator==(const T* obj) const
{
return m_obj->GetObject() == obj->GetObject();
}
bool operator==(const Ref<T>& obj) const
{
return m_obj->GetObject() == obj.m_obj->GetObject();
}
bool operator!=(const T* obj) const
{
return m_obj->GetObject() != obj->GetObject();
}
bool operator!=(const Ref<T>& obj) const
{
return m_obj->GetObject() != obj.m_obj->GetObject();
}
bool operator<(const T* obj) const
{
return m_obj->GetObject() < obj->GetObject();
}
bool operator<(const Ref<T>& obj) const
{
return m_obj->GetObject() < obj.m_obj->GetObject();
}
T* GetPtr() const
{
return m_obj;
}
};
class LogListener
{
static void LogMessageCallback(void* ctxt, BNLogLevel level, const char* msg);
static void CloseLogCallback(void* ctxt);
static BNLogLevel GetLogLevelCallback(void* ctxt);
public:
virtual ~LogListener() {}
static void RegisterLogListener(LogListener* listener);
static void UnregisterLogListener(LogListener* listener);
static void UpdateLogListeners();
virtual void LogMessage(BNLogLevel level, const std::string& msg) = 0;
virtual void CloseLog() {}
virtual BNLogLevel GetLogLevel() { return WarningLog; }
};
class Architecture;
class Platform;
class Type;
class DataBuffer;
class MainThreadAction;
class MainThreadActionHandler;
class InteractionHandler;
class QualifiedName;
struct FormInputField;
/*! Logs to the error console with the given BNLogLevel.
\param level BNLogLevel debug log level
\param fmt C-style format string.
\param ... Variable arguments corresponding to the format string.
*/
void Log(BNLogLevel level, const char* fmt, ...);
/*! LogDebug only writes text to the error console if the console is set to log level: DebugLog
Log level DebugLog is the most verbose logging level.
\param fmt C-style format string.
\param ... Variable arguments corresponding to the format string.
*/
void LogDebug(const char* fmt, ...);
/*! LogInfo always writes text to the error console, and corresponds to the log level: InfoLog.
Log level InfoLog is the second most verbose logging level.
\param fmt C-style format string.
\param ... Variable arguments corresponding to the format string.
*/
void LogInfo(const char* fmt, ...);
/*! LogWarn writes text to the error console including a warning icon,
and also shows a warning icon in the bottom pane. LogWarn corresponds to the log level: WarningLog.
\param fmt C-style format string.
\param ... Variable arguments corresponding to the format string.
*/
void LogWarn(const char* fmt, ...);
/*! LogError writes text to the error console and pops up the error console. Additionall,
Errors in the console log include a error icon. LogError corresponds to the log level: ErrorLog.
\param fmt C-style format string.
\param ... Variable arguments corresponding to the format string.
*/
void LogError(const char* fmt, ...);
/*! LogAlert pops up a message box displaying the alert message and logs to the error console.
LogAlert corresponds to the log level: AlertLog.
\param fmt C-style format string.
\param ... Variable arguments corresponding to the format string.
*/
void LogAlert(const char* fmt, ...);
void LogToStdout(BNLogLevel minimumLevel);
void LogToStderr(BNLogLevel minimumLevel);
bool LogToFile(BNLogLevel minimumLevel, const std::string& path, bool append = false);
void CloseLogs();
std::string EscapeString(const std::string& s);
std::string UnescapeString(const std::string& s);
bool PreprocessSource(const std::string& source, const std::string& fileName,
std::string& output, std::string& errors,
const std::vector<std::string>& includeDirs = std::vector<std::string>());
void InitCorePlugins();
void InitUserPlugins();
void InitRepoPlugins();
std::string GetBundledPluginDirectory();
void SetBundledPluginDirectory(const std::string& path);
std::string GetInstallDirectory();
std::string GetUserPluginDirectory();
std::string GetPathRelativeToBundledPluginDirectory(const std::string& path);
std::string GetPathRelativeToUserPluginDirectory(const std::string& path);
bool ExecuteWorkerProcess(const std::string& path, const std::vector<std::string>& args, const DataBuffer& input,
std::string& output, std::string& errors, bool stdoutIsText=false, bool stderrIsText=true);
std::string GetVersionString();
std::string GetProduct();
std::string GetProductType();
int GetLicenseCount();
uint32_t GetBuildId();
bool AreAutoUpdatesEnabled();
void SetAutoUpdatesEnabled(bool enabled);
uint64_t GetTimeSinceLastUpdateCheck();
void UpdatesChecked();
std::string GetActiveUpdateChannel();
void SetActiveUpdateChannel(const std::string& channel);
void SetCurrentPluginLoadOrder(BNPluginLoadOrder order);
void AddRequiredPluginDependency(const std::string& name);
void AddOptionalPluginDependency(const std::string& name);
bool DemangleMS(Architecture* arch,
const std::string& mangledName,
Type** outType,
QualifiedName& outVarName);
bool DemangleGNU3(Architecture* arch,
const std::string& mangledName,
Type** outType,
QualifiedName& outVarName);
void RegisterMainThread(MainThreadActionHandler* handler);
Ref<MainThreadAction> ExecuteOnMainThread(const std::function<void()>& action);
void ExecuteOnMainThreadAndWait(const std::function<void()>& action);
void WorkerEnqueue(const std::function<void()>& action);
void WorkerEnqueue(RefCountObject* owner, const std::function<void()>& action);
void WorkerPriorityEnqueue(const std::function<void()>& action);
void WorkerPriorityEnqueue(RefCountObject* owner, const std::function<void()>& action);
void WorkerInteractiveEnqueue(const std::function<void()>& action);
void WorkerInteractiveEnqueue(RefCountObject* owner, const std::function<void()>& action);
size_t GetWorkerThreadCount();
void SetWorkerThreadCount(size_t count);
std::string MarkdownToHTML(const std::string& contents);
void RegisterInteractionHandler(InteractionHandler* handler);
void ShowPlainTextReport(const std::string& title, const std::string& contents);
void ShowMarkdownReport(const std::string& title, const std::string& contents,
const std::string& plainText = "");
void ShowHTMLReport(const std::string& title, const std::string& contents,
const std::string& plainText = "");
bool GetTextLineInput(std::string& result, const std::string& prompt, const std::string& title);
bool GetIntegerInput(int64_t& result, const std::string& prompt, const std::string& title);
bool GetAddressInput(uint64_t& result, const std::string& prompt, const std::string& title);
bool GetChoiceInput(size_t& idx, const std::string& prompt, const std::string& title,
const std::vector<std::string>& choices);
bool GetOpenFileNameInput(std::string& result, const std::string& prompt, const std::string& ext = "");
bool GetSaveFileNameInput(std::string& result, const std::string& prompt, const std::string& ext = "",
const std::string& defaultName = "");
bool GetDirectoryNameInput(std::string& result, const std::string& prompt, const std::string& defaultName = "");
bool GetFormInput(std::vector<FormInputField>& fields, const std::string& title);
BNMessageBoxButtonResult ShowMessageBox(const std::string& title, const std::string& text,
BNMessageBoxButtonSet buttons = OKButtonSet, BNMessageBoxIcon icon = InformationIcon);
std::string GetUniqueIdentifierString();
class QualifiedName
{
std::vector<std::string> m_name;
public:
QualifiedName();
QualifiedName(const std::string& name);
QualifiedName(const std::vector<std::string>& name);
QualifiedName(const QualifiedName& name);
QualifiedName& operator=(const std::string& name);
QualifiedName& operator=(const std::vector<std::string>& name);
QualifiedName& operator=(const QualifiedName& name);
bool operator==(const QualifiedName& other) const;
bool operator!=(const QualifiedName& other) const;
bool operator<(const QualifiedName& other) const;
QualifiedName operator+(const QualifiedName& other) const;
std::string& operator[](size_t i);
const std::string& operator[](size_t i) const;
std::vector<std::string>::iterator begin();
std::vector<std::string>::iterator end();
std::vector<std::string>::const_iterator begin() const;
std::vector<std::string>::const_iterator end() const;
std::string& front();
const std::string& front() const;
std::string& back();
const std::string& back() const;
void insert(std::vector<std::string>::iterator loc, const std::string& name);
void insert(std::vector<std::string>::iterator loc, std::vector<std::string>::iterator b,
std::vector<std::string>::iterator e);
void erase(std::vector<std::string>::iterator i);
void clear();
void push_back(const std::string& name);
size_t size() const;
std::string GetString() const;
BNQualifiedName GetAPIObject() const;
static void FreeAPIObject(BNQualifiedName* name);
static QualifiedName FromAPIObject(BNQualifiedName* name);
};
class DataBuffer
{
BNDataBuffer* m_buffer;
public:
DataBuffer();
DataBuffer(size_t len);
DataBuffer(const void* data, size_t len);
DataBuffer(const DataBuffer& buf);
DataBuffer(BNDataBuffer* buf);
~DataBuffer();
DataBuffer& operator=(const DataBuffer& buf);
BNDataBuffer* GetBufferObject() const { return m_buffer; }
void* GetData();
const void* GetData() const;
void* GetDataAt(size_t offset);
const void* GetDataAt(size_t offset) const;
size_t GetLength() const;
void SetSize(size_t len);
void Clear();
void Append(const void* data, size_t len);
void Append(const DataBuffer& buf);
void AppendByte(uint8_t val);
DataBuffer GetSlice(size_t start, size_t len);
uint8_t& operator[](size_t offset);
const uint8_t& operator[](size_t offset) const;
std::string ToEscapedString() const;
static DataBuffer FromEscapedString(const std::string& src);
std::string ToBase64() const;
static DataBuffer FromBase64(const std::string& src);
bool ZlibCompress(DataBuffer& output) const;
bool ZlibDecompress(DataBuffer& output) const;
};
class TemporaryFile: public CoreRefCountObject<BNTemporaryFile, BNNewTemporaryFileReference, BNFreeTemporaryFile>
{
public:
TemporaryFile();
TemporaryFile(const DataBuffer& contents);
TemporaryFile(const std::string& contents);
TemporaryFile(BNTemporaryFile* file);
bool IsValid() const { return m_object != nullptr; }
std::string GetPath() const;
DataBuffer GetContents();
};
class NavigationHandler
{
private:
BNNavigationHandler m_callbacks;
static char* GetCurrentViewCallback(void* ctxt);
static uint64_t GetCurrentOffsetCallback(void* ctxt);
static bool NavigateCallback(void* ctxt, const char* view, uint64_t offset);
public:
NavigationHandler();
virtual ~NavigationHandler() {}
BNNavigationHandler* GetCallbacks() { return &m_callbacks; }
virtual std::string GetCurrentView() = 0;
virtual uint64_t GetCurrentOffset() = 0;
virtual bool Navigate(const std::string& view, uint64_t offset) = 0;
};
class BinaryView;
class UndoAction
{
private:
std::string m_typeName;
BNActionType m_actionType;
static void FreeCallback(void* ctxt);
static void UndoCallback(void* ctxt, BNBinaryView* data);
static void RedoCallback(void* ctxt, BNBinaryView* data);
static char* SerializeCallback(void* ctxt);
public:
UndoAction(const std::string& name, BNActionType action);
virtual ~UndoAction() {}
const std::string& GetTypeName() const { return m_typeName; }
BNActionType GetActionType() const { return m_actionType; }
BNUndoAction GetCallbacks();
void Add(BNBinaryView* view);
virtual void Undo(BinaryView* data) = 0;
virtual void Redo(BinaryView* data) = 0;
virtual Json::Value Serialize() = 0;
};
class UndoActionType
{
protected:
std::string m_nameForRegister;
static bool DeserializeCallback(void* ctxt, const char* data, BNUndoAction* result);
public:
UndoActionType(const std::string& name);
virtual ~UndoActionType() {}
static void Register(UndoActionType* type);
virtual UndoAction* Deserialize(const Json::Value& data) = 0;
};
class FileMetadata: public CoreRefCountObject<BNFileMetadata, BNNewFileReference, BNFreeFileMetadata>
{
public:
FileMetadata();
FileMetadata(const std::string& filename);
FileMetadata(BNFileMetadata* file);
void Close();
void SetNavigationHandler(NavigationHandler* handler);
std::string GetFilename() const;
void SetFilename(const std::string& name);
bool IsModified() const;
bool IsAnalysisChanged() const;
void MarkFileModified();
void MarkFileSaved();
bool IsBackedByDatabase() const;
bool CreateDatabase(const std::string& name, BinaryView* data);
bool CreateDatabase(const std::string& name, BinaryView* data,
const std::function<void(size_t progress, size_t total)>& progressCallback);
Ref<BinaryView> OpenExistingDatabase(const std::string& path);
Ref<BinaryView> OpenExistingDatabase(const std::string& path,
const std::function<void(size_t progress, size_t total)>& progressCallback);
bool SaveAutoSnapshot(BinaryView* data);
bool SaveAutoSnapshot(BinaryView* data,
const std::function<void(size_t progress, size_t total)>& progressCallback);
void BeginUndoActions();
void CommitUndoActions();
bool Undo();
bool Redo();
std::string GetCurrentView();
uint64_t GetCurrentOffset();
bool Navigate(const std::string& view, uint64_t offset);
BinaryNinja::Ref<BinaryNinja::BinaryView> GetViewOfType(const std::string& name);
};
class BinaryView;
class Function;
struct DataVariable;
class BinaryDataNotification
{
private:
BNBinaryDataNotification m_callbacks;
static void DataWrittenCallback(void* ctxt, BNBinaryView* data, uint64_t offset, size_t len);
static void DataInsertedCallback(void* ctxt, BNBinaryView* data, uint64_t offset, size_t len);
static void DataRemovedCallback(void* ctxt, BNBinaryView* data, uint64_t offset, uint64_t len);
static void FunctionAddedCallback(void* ctxt, BNBinaryView* data, BNFunction* func);
static void FunctionRemovedCallback(void* ctxt, BNBinaryView* data, BNFunction* func);
static void FunctionUpdatedCallback(void* ctxt, BNBinaryView* data, BNFunction* func);
static void DataVariableAddedCallback(void* ctxt, BNBinaryView* data, BNDataVariable* var);
static void DataVariableRemovedCallback(void* ctxt, BNBinaryView* data, BNDataVariable* var);
static void DataVariableUpdatedCallback(void* ctxt, BNBinaryView* data, BNDataVariable* var);
static void StringFoundCallback(void* ctxt, BNBinaryView* data, BNStringType type, uint64_t offset, size_t len);
static void StringRemovedCallback(void* ctxt, BNBinaryView* data, BNStringType type, uint64_t offset, size_t len);
static void TypeDefinedCallback(void* ctxt, BNBinaryView* data, BNQualifiedName* name, BNType* type);
static void TypeUndefinedCallback(void* ctxt, BNBinaryView* data, BNQualifiedName* name, BNType* type);
public:
BinaryDataNotification();
virtual ~BinaryDataNotification() {}
BNBinaryDataNotification* GetCallbacks() { return &m_callbacks; }
virtual void OnBinaryDataWritten(BinaryView* view, uint64_t offset, size_t len) { (void)view; (void)offset; (void)len; }
virtual void OnBinaryDataInserted(BinaryView* view, uint64_t offset, size_t len) { (void)view; (void)offset; (void)len; }
virtual void OnBinaryDataRemoved(BinaryView* view, uint64_t offset, uint64_t len) { (void)view; (void)offset; (void)len; }
virtual void OnAnalysisFunctionAdded(BinaryView* view, Function* func) { (void)view; (void)func; }
virtual void OnAnalysisFunctionRemoved(BinaryView* view, Function* func) { (void)view; (void)func; }
virtual void OnAnalysisFunctionUpdated(BinaryView* view, Function* func) { (void)view; (void)func; }
virtual void OnDataVariableAdded(BinaryView* view, const DataVariable& var) { (void)view; (void)var; }
virtual void OnDataVariableRemoved(BinaryView* view, const DataVariable& var) { (void)view; (void)var; }
virtual void OnDataVariableUpdated(BinaryView* view, const DataVariable& var) { (void)view; (void)var; }
virtual void OnStringFound(BinaryView* data, BNStringType type, uint64_t offset, size_t len) { (void)data; (void)type; (void)offset; (void)len; }
virtual void OnStringRemoved(BinaryView* data, BNStringType type, uint64_t offset, size_t len) { (void)data; (void)type; (void)offset; (void)len; }
virtual void OnTypeDefined(BinaryView* data, const QualifiedName& name, Type* type) { (void)data; (void)name; (void)type; }
virtual void OnTypeUndefined(BinaryView* data, const QualifiedName& name, Type* type) { (void)data; (void)name; (void)type; }
};
class FileAccessor
{
protected:
BNFileAccessor m_callbacks;
private:
static uint64_t GetLengthCallback(void* ctxt);
static size_t ReadCallback(void* ctxt, void* dest, uint64_t offset, size_t len);
static size_t WriteCallback(void* ctxt, uint64_t offset, const void* src, size_t len);
public:
FileAccessor();
FileAccessor(BNFileAccessor* accessor);
virtual ~FileAccessor() {}
BNFileAccessor* GetCallbacks() { return &m_callbacks; }
virtual bool IsValid() const = 0;
virtual uint64_t GetLength() const = 0;
virtual size_t Read(void* dest, uint64_t offset, size_t len) = 0;
virtual size_t Write(uint64_t offset, const void* src, size_t len) = 0;
};
class CoreFileAccessor: public FileAccessor
{
public:
CoreFileAccessor(BNFileAccessor* accessor);
virtual bool IsValid() const override { return true; }
virtual uint64_t GetLength() const override;
virtual size_t Read(void* dest, uint64_t offset, size_t len) override;
virtual size_t Write(uint64_t offset, const void* src, size_t len) override;
};
class Function;
class BasicBlock;
class Symbol: public CoreRefCountObject<BNSymbol, BNNewSymbolReference, BNFreeSymbol>
{
public:
Symbol(BNSymbolType type, const std::string& shortName, const std::string& fullName,
const std::string& rawName, uint64_t addr);
Symbol(BNSymbolType type, const std::string& name, uint64_t addr);
Symbol(BNSymbol* sym);
BNSymbolType GetType() const;
std::string GetShortName() const;
std::string GetFullName() const;
std::string GetRawName() const;
uint64_t GetAddress() const;
bool IsAutoDefined() const;
void SetAutoDefined(bool val);
static Ref<Symbol> ImportedFunctionFromImportAddressSymbol(Symbol* sym, uint64_t addr);
};
struct ReferenceSource
{
Ref<Function> func;
Ref<Architecture> arch;
uint64_t addr;
};
struct InstructionTextToken
{
BNInstructionTextTokenType type;
std::string text;
uint64_t value;
size_t size, operand;
BNInstructionTextTokenContext context;
uint64_t address;
InstructionTextToken();
InstructionTextToken(BNInstructionTextTokenType type, const std::string& text, uint64_t value = 0,
size_t size = 0, size_t operand = BN_INVALID_OPERAND);
InstructionTextToken(BNInstructionTextTokenType type, BNInstructionTextTokenContext context,
const std::string& text, uint64_t address, uint64_t value = 0, size_t size = 0,
size_t operand = BN_INVALID_OPERAND);
};
struct DisassemblyTextLine
{
uint64_t addr;
std::vector<InstructionTextToken> tokens;
};
struct LinearDisassemblyPosition
{
Ref<Function> function;
Ref<BasicBlock> block;
uint64_t address;
};
struct LinearDisassemblyLine
{
BNLinearDisassemblyLineType type;
Ref<Function> function;
Ref<BasicBlock> block;
size_t lineOffset;
DisassemblyTextLine contents;
};
class DisassemblySettings;
class AnalysisCompletionEvent: public CoreRefCountObject<BNAnalysisCompletionEvent,
BNNewAnalysisCompletionEventReference, BNFreeAnalysisCompletionEvent>
{
protected:
std::function<void()> m_callback;
std::recursive_mutex m_mutex;
static void CompletionCallback(void* ctxt);
public:
AnalysisCompletionEvent(BinaryView* view, const std::function<void()>& callback);
void Cancel();
};
struct DataVariable
{
uint64_t address;
Ref<Type> type;
bool autoDiscovered;
};
struct Segment
{
uint64_t start, length;
uint64_t dataOffset, dataLength;
uint32_t flags;
};
struct Section
{
std::string name, type;
uint64_t start, length;
std::string linkedSection, infoSection;
uint64_t infoData;
uint64_t align, entrySize;
};
struct QualifiedNameAndType;
/*! BinaryView is the base class for creating views on binary data (e.g. ELF, PE, Mach-O).
BinaryView should be subclassed to create a new BinaryView
*/
class BinaryView: public CoreRefCountObject<BNBinaryView, BNNewViewReference, BNFreeBinaryView>
{
protected:
Ref<FileMetadata> m_file; //!< The underlying file
/*! BinaryView constructor
\param typeName name of the BinaryView (e.g. ELF, PE, Mach-O, ...)
\param file a file to create a view from
\param parentView optional view that contains the raw data used by this view
*/
BinaryView(const std::string& typeName, FileMetadata* file, BinaryView* parentView = nullptr);
/*! PerformRead provides a mapping between the flat file and virtual offsets in the file.
\param dest the address to write len number of bytes.
\param offset the virtual offset to find and read len bytes from
....\param len the number of bytes to read from offset and write to dest
*/
virtual size_t PerformRead(void* dest, uint64_t offset, size_t len) { (void)dest; (void)offset; (void)len; return 0; }
virtual size_t PerformWrite(uint64_t offset, const void* data, size_t len) { (void)offset; (void)data; (void)len; return 0; }
virtual size_t PerformInsert(uint64_t offset, const void* data, size_t len) { (void)offset; (void)data; (void)len; return 0; }
virtual size_t PerformRemove(uint64_t offset, uint64_t len) { (void)offset; (void)len; return 0; }
virtual BNModificationStatus PerformGetModification(uint64_t offset) { (void)offset; return Original; }
virtual bool PerformIsValidOffset(uint64_t offset);
virtual bool PerformIsOffsetReadable(uint64_t offset);
virtual bool PerformIsOffsetWritable(uint64_t offset);
virtual bool PerformIsOffsetExecutable(uint64_t offset);
virtual bool PerformIsOffsetBackedByFile(uint64_t offset);
virtual uint64_t PerformGetNextValidOffset(uint64_t offset);
virtual uint64_t PerformGetStart() const { return 0; }
virtual uint64_t PerformGetLength() const { return 0; }
virtual uint64_t PerformGetEntryPoint() const { return 0; }
virtual bool PerformIsExecutable() const { return false; }
virtual BNEndianness PerformGetDefaultEndianness() const;
virtual size_t PerformGetAddressSize() const;
virtual bool PerformSave(FileAccessor* file);
void NotifyDataWritten(uint64_t offset, size_t len);
void NotifyDataInserted(uint64_t offset, size_t len);
void NotifyDataRemoved(uint64_t offset, uint64_t len);
private:
static bool InitCallback(void* ctxt);
static void FreeCallback(void* ctxt);
static size_t ReadCallback(void* ctxt, void* dest, uint64_t offset, size_t len);
static size_t WriteCallback(void* ctxt, uint64_t offset, const void* src, size_t len);
static size_t InsertCallback(void* ctxt, uint64_t offset, const void* src, size_t len);
static size_t RemoveCallback(void* ctxt, uint64_t offset, uint64_t len);
static BNModificationStatus GetModificationCallback(void* ctxt, uint64_t offset);
static bool IsValidOffsetCallback(void* ctxt, uint64_t offset);
static bool IsOffsetReadableCallback(void* ctxt, uint64_t offset);
static bool IsOffsetWritableCallback(void* ctxt, uint64_t offset);
static bool IsOffsetExecutableCallback(void* ctxt, uint64_t offset);
static bool IsOffsetBackedByFileCallback(void* ctxt, uint64_t offset);
static uint64_t GetNextValidOffsetCallback(void* ctxt, uint64_t offset);
static uint64_t GetStartCallback(void* ctxt);
static uint64_t GetLengthCallback(void* ctxt);
static uint64_t GetEntryPointCallback(void* ctxt);
static bool IsExecutableCallback(void* ctxt);
static BNEndianness GetDefaultEndiannessCallback(void* ctxt);
static size_t GetAddressSizeCallback(void* ctxt);
static bool SaveCallback(void* ctxt, BNFileAccessor* file);
public:
BinaryView(BNBinaryView* view);
virtual bool Init() { return true; }
FileMetadata* GetFile() const { return m_file; }
Ref<BinaryView> GetParentView() const;
std::string GetTypeName() const;
bool IsModified() const;
bool IsAnalysisChanged() const;
bool IsBackedByDatabase() const;
bool CreateDatabase(const std::string& path);
bool CreateDatabase(const std::string& path,
const std::function<void(size_t progress, size_t total)>& progressCallback);
bool SaveAutoSnapshot();
bool SaveAutoSnapshot(const std::function<void(size_t progress, size_t total)>& progressCallback);
void BeginUndoActions();
void AddUndoAction(UndoAction* action);
void CommitUndoActions();
bool Undo();
bool Redo();
std::string GetCurrentView();
uint64_t GetCurrentOffset();
bool Navigate(const std::string& view, uint64_t offset);
size_t Read(void* dest, uint64_t offset, size_t len);
DataBuffer ReadBuffer(uint64_t offset, size_t len);
size_t Write(uint64_t offset, const void* data, size_t len);
size_t WriteBuffer(uint64_t offset, const DataBuffer& data);
size_t Insert(uint64_t offset, const void* data, size_t len);
size_t InsertBuffer(uint64_t offset, const DataBuffer& data);
size_t Remove(uint64_t offset, uint64_t len);
BNModificationStatus GetModification(uint64_t offset);
std::vector<BNModificationStatus> GetModification(uint64_t offset, size_t len);
bool IsValidOffset(uint64_t offset) const;
bool IsOffsetReadable(uint64_t offset) const;
bool IsOffsetWritable(uint64_t offset) const;
bool IsOffsetExecutable(uint64_t offset) const;
bool IsOffsetBackedByFile(uint64_t offset) const;
uint64_t GetNextValidOffset(uint64_t offset) const;
uint64_t GetStart() const;
uint64_t GetEnd() const;
uint64_t GetLength() const;
uint64_t GetEntryPoint() const;
Ref<Architecture> GetDefaultArchitecture() const;
void SetDefaultArchitecture(Architecture* arch);
Ref<Platform> GetDefaultPlatform() const;
void SetDefaultPlatform(Platform* platform);
BNEndianness GetDefaultEndianness() const;
size_t GetAddressSize() const;
bool IsExecutable() const;
bool Save(FileAccessor* file);
bool Save(const std::string& path);
void RegisterNotification(BinaryDataNotification* notify);
void UnregisterNotification(BinaryDataNotification* notify);
void AddFunctionForAnalysis(Platform* platform, uint64_t addr);
void AddEntryPointForAnalysis(Platform* platform, uint64_t start);
void RemoveAnalysisFunction(Function* func);
void CreateUserFunction(Platform* platform, uint64_t start);
void RemoveUserFunction(Function* func);
void UpdateAnalysis();
void AbortAnalysis();
void DefineDataVariable(uint64_t addr, Type* type);
void DefineUserDataVariable(uint64_t addr, Type* type);
void UndefineDataVariable(uint64_t addr);
void UndefineUserDataVariable(uint64_t addr);