// 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 #include #include #include "binaryninjaapi.h" using namespace BinaryNinja; using namespace std; struct SymbolQueueResolveContext { std::function<:pair>, Ref>()> resolve; }; struct SymbolQueueAddContext { std::function add; }; uint64_t BinaryDataNotification::NotificationBarrierCallback(void* ctxt, BNBinaryView* object) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); return notify->OnNotificationBarrier(view); } void BinaryDataNotification::DataWrittenCallback(void* ctxt, BNBinaryView* object, uint64_t offset, size_t len) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); notify->OnBinaryDataWritten(view, offset, len); } void BinaryDataNotification::DataInsertedCallback(void* ctxt, BNBinaryView* object, uint64_t offset, size_t len) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); notify->OnBinaryDataInserted(view, offset, len); } void BinaryDataNotification::DataRemovedCallback(void* ctxt, BNBinaryView* object, uint64_t offset, uint64_t len) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); notify->OnBinaryDataRemoved(view, offset, len); } void BinaryDataNotification::FunctionAddedCallback(void* ctxt, BNBinaryView* object, BNFunction* func) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); Ref funcObj = new Function(BNNewFunctionReference(func)); notify->OnAnalysisFunctionAdded(view, funcObj); } void BinaryDataNotification::FunctionRemovedCallback(void* ctxt, BNBinaryView* object, BNFunction* func) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); Ref funcObj = new Function(BNNewFunctionReference(func)); notify->OnAnalysisFunctionRemoved(view, funcObj); } void BinaryDataNotification::FunctionUpdatedCallback(void* ctxt, BNBinaryView* object, BNFunction* func) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); Ref funcObj = new Function(BNNewFunctionReference(func)); notify->OnAnalysisFunctionUpdated(view, funcObj); } void BinaryDataNotification::FunctionUpdateRequestedCallback(void* ctxt, BNBinaryView* object, BNFunction* func) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); Ref funcObj = new Function(BNNewFunctionReference(func)); notify->OnAnalysisFunctionUpdateRequested(view, funcObj); } void BinaryDataNotification::DataVariableAddedCallback(void* ctxt, BNBinaryView* object, BNDataVariable* var) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); DataVariable varObj(var->address, Confidence>(new Type(BNNewTypeReference(var->type)), var->typeConfidence), var->autoDiscovered); notify->OnDataVariableAdded(view, varObj); } void BinaryDataNotification::DataVariableRemovedCallback(void* ctxt, BNBinaryView* object, BNDataVariable* var) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); DataVariable varObj(var->address, Confidence>(new Type(BNNewTypeReference(var->type)), var->typeConfidence), var->autoDiscovered); notify->OnDataVariableRemoved(view, varObj); } void BinaryDataNotification::DataVariableUpdatedCallback(void* ctxt, BNBinaryView* object, BNDataVariable* var) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); DataVariable varObj(var->address, Confidence>(new Type(BNNewTypeReference(var->type)), var->typeConfidence), var->autoDiscovered); notify->OnDataVariableUpdated(view, varObj); } void BinaryDataNotification::DataMetadataUpdatedCallback(void* ctxt, BNBinaryView* object, uint64_t offset) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); notify->OnDataMetadataUpdated(view, offset); } void BinaryDataNotification::TagTypeUpdatedCallback(void* ctxt, BNBinaryView* object, BNTagType* tagType) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); Ref tagTypeRef = new TagType(BNNewTagTypeReference(tagType)); notify->OnTagTypeUpdated(view, tagTypeRef); } void BinaryDataNotification::TagAddedCallback(void* ctxt, BNBinaryView* object, BNTagReference* tagRef) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); notify->OnTagAdded(view, TagReference(*tagRef)); } void BinaryDataNotification::TagUpdatedCallback(void* ctxt, BNBinaryView* object, BNTagReference* tagRef) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); notify->OnTagUpdated(view, TagReference(*tagRef)); } void BinaryDataNotification::TagRemovedCallback(void* ctxt, BNBinaryView* object, BNTagReference* tagRef) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); notify->OnTagRemoved(view, TagReference(*tagRef)); } void BinaryDataNotification::SymbolAddedCallback(void* ctxt, BNBinaryView* object, BNSymbol* symobj) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); Ref sym = new Symbol(BNNewSymbolReference(symobj)); notify->OnSymbolAdded(view, sym); } void BinaryDataNotification::SymbolUpdatedCallback(void* ctxt, BNBinaryView* object, BNSymbol* symobj) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); Ref sym = new Symbol(BNNewSymbolReference(symobj)); notify->OnSymbolUpdated(view, sym); } void BinaryDataNotification::SymbolRemovedCallback(void* ctxt, BNBinaryView* object, BNSymbol* symobj) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); Ref sym = new Symbol(BNNewSymbolReference(symobj)); notify->OnSymbolRemoved(view, sym); } void BinaryDataNotification::StringFoundCallback( void* ctxt, BNBinaryView* object, BNStringType type, uint64_t offset, size_t len) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); notify->OnStringFound(view, type, offset, len); } void BinaryDataNotification::StringRemovedCallback( void* ctxt, BNBinaryView* object, BNStringType type, uint64_t offset, size_t len) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(object)); notify->OnStringRemoved(view, type, offset, len); } void BinaryDataNotification::TypeDefinedCallback(void* ctxt, BNBinaryView* data, BNQualifiedName* name, BNType* type) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref typeObj = new Type(BNNewTypeReference(type)); notify->OnTypeDefined(view, QualifiedName::FromAPIObject(name), typeObj); } void BinaryDataNotification::TypeUndefinedCallback(void* ctxt, BNBinaryView* data, BNQualifiedName* name, BNType* type) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref typeObj = new Type(BNNewTypeReference(type)); notify->OnTypeUndefined(view, QualifiedName::FromAPIObject(name), typeObj); } void BinaryDataNotification::TypeReferenceChangedCallback( void* ctxt, BNBinaryView* data, BNQualifiedName* name, BNType* type) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref typeObj = new Type(BNNewTypeReference(type)); notify->OnTypeReferenceChanged(view, QualifiedName::FromAPIObject(name), typeObj); } void BinaryDataNotification::TypeFieldReferenceChangedCallback( void* ctxt, BNBinaryView* data, BNQualifiedName* name, uint64_t offset) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); notify->OnTypeFieldReferenceChanged(view, QualifiedName::FromAPIObject(name), offset); } void BinaryDataNotification::SegmentAddedCallback(void* ctxt, BNBinaryView* data, BNSegment* segment) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref segmentObj = new Segment(BNNewSegmentReference(segment)); notify->OnSegmentAdded(view, segmentObj); } void BinaryDataNotification::SegmentUpdatedCallback(void* ctxt, BNBinaryView* data, BNSegment* segment) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref segmentObj = new Segment(BNNewSegmentReference(segment)); notify->OnSegmentUpdated(view, segmentObj); } void BinaryDataNotification::SegmentRemovedCallback(void* ctxt, BNBinaryView* data, BNSegment* segment) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref segmentObj = new Segment(BNNewSegmentReference(segment)); notify->OnSegmentRemoved(view, segmentObj); } void BinaryDataNotification::SectionAddedCallback(void* ctxt, BNBinaryView* data, BNSection* section) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref

sectionObj = new Section(BNNewSectionReference(section)); notify->OnSectionAdded(view, sectionObj); } void BinaryDataNotification::SectionUpdatedCallback(void* ctxt, BNBinaryView* data, BNSection* section) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref
sectionObj = new Section(BNNewSectionReference(section)); notify->OnSectionUpdated(view, sectionObj); } void BinaryDataNotification::SectionRemovedCallback(void* ctxt, BNBinaryView* data, BNSection* section) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref
sectionObj = new Section(BNNewSectionReference(section)); notify->OnSectionRemoved(view, sectionObj); } void BinaryDataNotification::ComponentNameUpdatedCallback(void* ctxt, BNBinaryView* data, char *previousName, BNComponent* bnComponent) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref component = new Component(BNNewComponentReference(bnComponent)); std::string prevName = previousName; BNFreeString(previousName); notify->OnComponentNameUpdated(view, prevName, component); } void BinaryDataNotification::ComponentAddedCallback(void* ctxt, BNBinaryView* data, BNComponent* bnComponent) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref component = new Component(BNNewComponentReference(bnComponent)); notify->OnComponentAdded(view, component); } void BinaryDataNotification::ComponentRemovedCallback(void* ctxt, BNBinaryView* data, BNComponent* bnFormerParent, BNComponent* bnComponent) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref formerParent = new Component(BNNewComponentReference(bnFormerParent)); Ref component = new Component(BNNewComponentReference(bnComponent)); notify->OnComponentRemoved(view, formerParent, component); } void BinaryDataNotification::ComponentMovedCallback(void* ctxt, BNBinaryView* data, BNComponent* bnFormerParent, BNComponent* bnNewParent, BNComponent* bnComponent) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref formerParent = new Component(BNNewComponentReference(bnFormerParent)); Ref newParent = new Component(BNNewComponentReference(bnNewParent)); Ref component = new Component(BNNewComponentReference(bnComponent)); notify->OnComponentMoved(view, formerParent, newParent, component); } void BinaryDataNotification::ComponentFunctionAddedCallback(void* ctxt, BNBinaryView* data, BNComponent* bnComponent, BNFunction* func) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref component = new Component(BNNewComponentReference(bnComponent)); Ref function = new Function(BNNewFunctionReference(func)); notify->OnComponentFunctionAdded(view, component, function); } void BinaryDataNotification::ComponentFunctionRemovedCallback(void* ctxt, BNBinaryView* data, BNComponent* bnComponent, BNFunction* func) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref component = new Component(BNNewComponentReference(bnComponent)); Ref function = new Function(BNNewFunctionReference(func)); notify->OnComponentFunctionRemoved(view, component, function); } void BinaryDataNotification::ComponentDataVariableAddedCallback(void* ctxt, BNBinaryView* data, BNComponent* bnComponent, BNDataVariable* var) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref component = new Component(BNNewComponentReference(bnComponent)); DataVariable varObj(var->address, Confidence>(new Type(BNNewTypeReference(var->type)), var->typeConfidence), var->autoDiscovered); notify->OnComponentDataVariableAdded(view, component, varObj); } void BinaryDataNotification::ComponentDataVariableRemovedCallback(void* ctxt, BNBinaryView* data, BNComponent* bnComponent, BNDataVariable* var) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref component = new Component(BNNewComponentReference(bnComponent)); DataVariable varObj(var->address, Confidence>(new Type(BNNewTypeReference(var->type)), var->typeConfidence), var->autoDiscovered); notify->OnComponentDataVariableRemoved(view, component, varObj); } void BinaryDataNotification::ExternalLibraryAddedCallback(void* ctxt, BNBinaryView* data, BNExternalLibrary* library) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref libraryObj = new ExternalLibrary(BNNewExternalLibraryReference(library)); notify->OnExternalLibraryAdded(view, libraryObj); } void BinaryDataNotification::ExternalLibraryUpdatedCallback(void* ctxt, BNBinaryView* data, BNExternalLibrary* library) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref libraryObj = new ExternalLibrary(BNNewExternalLibraryReference(library)); notify->OnExternalLibraryUpdated(view, libraryObj); } void BinaryDataNotification::ExternalLibraryRemovedCallback(void* ctxt, BNBinaryView* data, BNExternalLibrary* library) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref libraryObj = new ExternalLibrary(BNNewExternalLibraryReference(library)); notify->OnExternalLibraryRemoved(view, libraryObj); } void BinaryDataNotification::ExternalLocationAddedCallback(void* ctxt, BNBinaryView* data, BNExternalLocation* location) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref locationObj = new ExternalLocation(BNNewExternalLocationReference(location)); notify->OnExternalLocationAdded(view, locationObj); } void BinaryDataNotification::ExternalLocationUpdatedCallback(void* ctxt, BNBinaryView* data, BNExternalLocation* location) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref locationObj = new ExternalLocation(BNNewExternalLocationReference(location)); notify->OnExternalLocationUpdated(view, locationObj); } void BinaryDataNotification::ExternalLocationRemovedCallback(void* ctxt, BNBinaryView* data, BNExternalLocation* location) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref locationObj = new ExternalLocation(BNNewExternalLocationReference(location)); notify->OnExternalLocationRemoved(view, locationObj); } void BinaryDataNotification::TypeArchiveAttachedCallback(void* ctxt, BNBinaryView* data, const char* id, const char* path) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); notify->OnTypeArchiveAttached(view, id, path); } void BinaryDataNotification::TypeArchiveDetachedCallback(void* ctxt, BNBinaryView* data, const char* id, const char* path) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); notify->OnTypeArchiveDetached(view, id, path); } void BinaryDataNotification::TypeArchiveConnectedCallback(void* ctxt, BNBinaryView* data, BNTypeArchive* archive) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref apiArchive = new TypeArchive(BNNewTypeArchiveReference(archive)); notify->OnTypeArchiveConnected(view, apiArchive); } void BinaryDataNotification::TypeArchiveDisconnectedCallback(void* ctxt, BNBinaryView* data, BNTypeArchive* archive) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref apiArchive = new TypeArchive(BNNewTypeArchiveReference(archive)); notify->OnTypeArchiveDisconnected(view, apiArchive); } void BinaryDataNotification::UndoEntryAddedCallback(void* ctxt, BNBinaryView* data, BNUndoEntry* entry) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref apiEntry = new UndoEntry(BNNewUndoEntryReference(entry)); notify->OnUndoEntryAdded(view, apiEntry); } void BinaryDataNotification::UndoEntryTakenCallback(void* ctxt, BNBinaryView* data, BNUndoEntry* entry) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref apiEntry = new UndoEntry(BNNewUndoEntryReference(entry)); notify->OnUndoEntryTaken(view, apiEntry); } void BinaryDataNotification::RedoEntryTakenCallback(void* ctxt, BNBinaryView* data, BNUndoEntry* entry) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view = new BinaryView(BNNewViewReference(data)); Ref apiEntry = new UndoEntry(BNNewUndoEntryReference(entry)); notify->OnRedoEntryTaken(view, apiEntry); } void BinaryDataNotification::RebasedCallback(void *ctxt, BNBinaryView *oldView, BNBinaryView *newView) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; Ref view1 = new BinaryView(BNNewViewReference(oldView)); Ref view2 = new BinaryView(BNNewViewReference(newView)); notify->OnRebased(view1, view2); } BinaryDataNotification::BinaryDataNotification() { m_callbacks.context = this; m_callbacks.notificationBarrier = nullptr; m_callbacks.dataWritten = DataWrittenCallback; m_callbacks.dataInserted = DataInsertedCallback; m_callbacks.dataRemoved = DataRemovedCallback; m_callbacks.functionAdded = FunctionAddedCallback; m_callbacks.functionRemoved = FunctionRemovedCallback; m_callbacks.functionUpdated = FunctionUpdatedCallback; m_callbacks.functionUpdateRequested = FunctionUpdateRequestedCallback; m_callbacks.dataVariableAdded = DataVariableAddedCallback; m_callbacks.dataVariableRemoved = DataVariableRemovedCallback; m_callbacks.dataVariableUpdated = DataVariableUpdatedCallback; m_callbacks.dataMetadataUpdated = DataMetadataUpdatedCallback; m_callbacks.tagTypeUpdated = TagTypeUpdatedCallback; m_callbacks.tagAdded = TagAddedCallback; m_callbacks.tagUpdated = TagUpdatedCallback; m_callbacks.tagRemoved = TagRemovedCallback; m_callbacks.symbolAdded = SymbolAddedCallback; m_callbacks.symbolUpdated = SymbolUpdatedCallback; m_callbacks.symbolRemoved = SymbolRemovedCallback; m_callbacks.stringFound = StringFoundCallback; m_callbacks.stringRemoved = StringRemovedCallback; m_callbacks.typeDefined = TypeDefinedCallback; m_callbacks.typeUndefined = TypeUndefinedCallback; m_callbacks.typeReferenceChanged = TypeReferenceChangedCallback; m_callbacks.typeFieldReferenceChanged = TypeFieldReferenceChangedCallback; m_callbacks.segmentAdded = SegmentAddedCallback; m_callbacks.segmentUpdated = SegmentUpdatedCallback; m_callbacks.segmentRemoved = SegmentRemovedCallback; m_callbacks.sectionAdded = SectionAddedCallback; m_callbacks.sectionUpdated = SectionUpdatedCallback; m_callbacks.sectionRemoved = SectionRemovedCallback; m_callbacks.componentNameUpdated = ComponentNameUpdatedCallback; m_callbacks.componentAdded = ComponentAddedCallback; m_callbacks.componentRemoved = ComponentRemovedCallback; m_callbacks.componentMoved = ComponentMovedCallback; m_callbacks.componentFunctionAdded = ComponentFunctionAddedCallback; m_callbacks.componentFunctionRemoved = ComponentFunctionRemovedCallback; m_callbacks.componentDataVariableAdded = ComponentDataVariableAddedCallback; m_callbacks.componentDataVariableRemoved = ComponentDataVariableRemovedCallback; m_callbacks.externalLibraryAdded = ExternalLibraryAddedCallback; m_callbacks.externalLibraryUpdated = ExternalLibraryUpdatedCallback; m_callbacks.externalLibraryRemoved = ExternalLibraryRemovedCallback; m_callbacks.externalLocationAdded = ExternalLocationAddedCallback; m_callbacks.externalLocationUpdated = ExternalLocationUpdatedCallback; m_callbacks.externalLocationRemoved = ExternalLocationRemovedCallback; m_callbacks.typeArchiveAttached = TypeArchiveAttachedCallback; m_callbacks.typeArchiveDetached = TypeArchiveDetachedCallback; m_callbacks.typeArchiveConnected = TypeArchiveConnectedCallback; m_callbacks.typeArchiveDisconnected = TypeArchiveDisconnectedCallback; m_callbacks.undoEntryAdded = UndoEntryAddedCallback; m_callbacks.undoEntryTaken = UndoEntryTakenCallback; m_callbacks.redoEntryTaken = RedoEntryTakenCallback; m_callbacks.rebased = RebasedCallback; } BinaryDataNotification::BinaryDataNotification(NotificationTypes notifications) { m_callbacks.context = this; m_callbacks.notificationBarrier = (notifications & NotificationType::NotificationBarrier) ? NotificationBarrierCallback : nullptr; m_callbacks.dataWritten = (notifications & NotificationType::DataWritten) ? DataWrittenCallback : nullptr; m_callbacks.dataInserted = (notifications & NotificationType::DataInserted) ? DataInsertedCallback : nullptr; m_callbacks.dataRemoved = (notifications & NotificationType::DataRemoved) ? DataRemovedCallback : nullptr; m_callbacks.functionAdded = (notifications & NotificationType::FunctionAdded) ? FunctionAddedCallback : nullptr; m_callbacks.functionRemoved = (notifications & NotificationType::FunctionRemoved) ? FunctionRemovedCallback : nullptr; m_callbacks.functionUpdated = (notifications & NotificationType::FunctionUpdated) ? FunctionUpdatedCallback : nullptr; m_callbacks.functionUpdateRequested = (notifications & NotificationType::FunctionUpdateRequested) ? FunctionUpdateRequestedCallback : nullptr; m_callbacks.dataVariableAdded = (notifications & NotificationType::DataVariableAdded) ? DataVariableAddedCallback : nullptr; m_callbacks.dataVariableRemoved = (notifications & NotificationType::DataVariableRemoved) ? DataVariableRemovedCallback : nullptr; m_callbacks.dataVariableUpdated = (notifications & NotificationType::DataVariableUpdated) ? DataVariableUpdatedCallback : nullptr; m_callbacks.dataMetadataUpdated = (notifications & NotificationType::DataMetadataUpdated) ? DataMetadataUpdatedCallback : nullptr; m_callbacks.tagTypeUpdated = (notifications & NotificationType::TagTypeUpdated) ? TagTypeUpdatedCallback : nullptr; m_callbacks.tagAdded = (notifications & NotificationType::TagAdded) ? TagAddedCallback : nullptr; m_callbacks.tagUpdated = (notifications & NotificationType::TagUpdated) ? TagUpdatedCallback : nullptr; m_callbacks.tagRemoved = (notifications & NotificationType::TagRemoved) ? TagRemovedCallback : nullptr; m_callbacks.symbolAdded = (notifications & NotificationType::SymbolAdded) ? SymbolAddedCallback : nullptr; m_callbacks.symbolUpdated = (notifications & NotificationType::SymbolUpdated) ? SymbolUpdatedCallback : nullptr; m_callbacks.symbolRemoved = (notifications & NotificationType::SymbolRemoved) ? SymbolRemovedCallback : nullptr; m_callbacks.stringFound = (notifications & NotificationType::StringFound) ? StringFoundCallback : nullptr; m_callbacks.stringRemoved = (notifications & NotificationType::StringRemoved) ? StringRemovedCallback : nullptr; m_callbacks.typeDefined = (notifications & NotificationType::TypeDefined) ? TypeDefinedCallback : nullptr; m_callbacks.typeUndefined = (notifications & NotificationType::TypeUndefined) ? TypeUndefinedCallback : nullptr; m_callbacks.typeReferenceChanged = (notifications & NotificationType::TypeReferenceChanged) ? TypeReferenceChangedCallback : nullptr; m_callbacks.typeFieldReferenceChanged = (notifications & NotificationType::TypeFieldReferenceChanged) ? TypeFieldReferenceChangedCallback : nullptr; m_callbacks.segmentAdded = (notifications & NotificationType::SegmentAdded) ? SegmentAddedCallback : nullptr; m_callbacks.segmentUpdated = (notifications & NotificationType::SegmentUpdated) ? SegmentUpdatedCallback : nullptr; m_callbacks.segmentRemoved = (notifications & NotificationType::SegmentRemoved) ? SegmentRemovedCallback : nullptr; m_callbacks.sectionAdded = (notifications & NotificationType::SectionAdded) ? SectionAddedCallback : nullptr; m_callbacks.sectionUpdated = (notifications & NotificationType::SectionUpdated) ? SectionUpdatedCallback : nullptr; m_callbacks.sectionRemoved = (notifications & NotificationType::SectionRemoved) ? SectionRemovedCallback : nullptr; m_callbacks.componentNameUpdated = (notifications & NotificationType::ComponentNameUpdated) ? ComponentNameUpdatedCallback : nullptr; m_callbacks.componentAdded = (notifications & NotificationType::ComponentAdded) ? ComponentAddedCallback : nullptr; m_callbacks.componentRemoved = (notifications & NotificationType::ComponentRemoved) ? ComponentRemovedCallback : nullptr; m_callbacks.componentMoved = (notifications & NotificationType::ComponentMoved) ? ComponentMovedCallback : nullptr; m_callbacks.componentFunctionAdded = (notifications & NotificationType::ComponentFunctionAdded) ? ComponentFunctionAddedCallback : nullptr; m_callbacks.componentFunctionRemoved = (notifications & NotificationType::ComponentFunctionRemoved) ? ComponentFunctionRemovedCallback : nullptr; m_callbacks.componentDataVariableAdded = (notifications & NotificationType::ComponentDataVariableAdded) ? ComponentDataVariableAddedCallback : nullptr; m_callbacks.componentDataVariableRemoved = (notifications & NotificationType::ComponentDataVariableRemoved) ? ComponentDataVariableRemovedCallback : nullptr; m_callbacks.externalLibraryAdded = (notifications & NotificationType::ExternalLibraryAdded) ? ExternalLibraryAddedCallback : nullptr; m_callbacks.externalLibraryUpdated = (notifications & NotificationType::ExternalLibraryUpdated) ? ExternalLibraryUpdatedCallback : nullptr; m_callbacks.externalLibraryRemoved = (notifications & NotificationType::ExternalLibraryRemoved) ? ExternalLibraryRemovedCallback : nullptr; m_callbacks.externalLocationAdded = (notifications & NotificationType::ExternalLocationAdded) ? ExternalLocationAddedCallback : nullptr; m_callbacks.externalLocationUpdated = (notifications & NotificationType::ExternalLocationUpdated) ? ExternalLocationUpdatedCallback : nullptr; m_callbacks.externalLocationRemoved = (notifications & NotificationType::ExternalLocationRemoved) ? ExternalLocationRemovedCallback : nullptr; m_callbacks.typeArchiveAttached = (notifications & NotificationType::TypeArchiveAttached) ? TypeArchiveAttachedCallback : nullptr; m_callbacks.typeArchiveDetached = (notifications & NotificationType::TypeArchiveDetached) ? TypeArchiveDetachedCallback : nullptr; m_callbacks.typeArchiveConnected = (notifications & NotificationType::TypeArchiveConnected) ? TypeArchiveConnectedCallback : nullptr; m_callbacks.typeArchiveDisconnected = (notifications & NotificationType::TypeArchiveDisconnected) ? TypeArchiveDisconnectedCallback : nullptr; m_callbacks.undoEntryAdded = (notifications & NotificationType::UndoEntryAdded) ? UndoEntryAddedCallback : nullptr; m_callbacks.undoEntryTaken = (notifications & NotificationType::UndoEntryTaken) ? UndoEntryTakenCallback : nullptr; m_callbacks.redoEntryTaken = (notifications & NotificationType::RedoEntryTaken) ? RedoEntryTakenCallback : nullptr; m_callbacks.rebased = (notifications & NotificationType::Rebased) ? RebasedCallback : nullptr; } Symbol::Symbol(BNSymbolType type, const string& shortName, const string& fullName, const string& rawName, uint64_t addr, BNSymbolBinding binding, const NameSpace& nameSpace, uint64_t ordinal) { BNNameSpace ns = nameSpace.GetAPIObject(); m_object = BNCreateSymbol(type, shortName.c_str(), fullName.c_str(), rawName.c_str(), addr, binding, &ns, ordinal); NameSpace::FreeAPIObject(&ns); } Symbol::Symbol(BNSymbolType type, const std::string& name, uint64_t addr, BNSymbolBinding binding, const NameSpace& nameSpace, uint64_t ordinal) { BNNameSpace ns = nameSpace.GetAPIObject(); m_object = BNCreateSymbol(type, name.c_str(), name.c_str(), name.c_str(), addr, binding, &ns, ordinal); NameSpace::FreeAPIObject(&ns); } Symbol::Symbol(BNSymbol* sym) { m_object = sym; } BNSymbolType Symbol::GetType() const { return BNGetSymbolType(m_object); } BNSymbolBinding Symbol::GetBinding() const { return BNGetSymbolBinding(m_object); } NameSpace Symbol::GetNameSpace() const { BNNameSpace name = BNGetSymbolNameSpace(m_object); NameSpace result = NameSpace::FromAPIObject(&name); BNFreeNameSpace(&name); return result; } string Symbol::GetShortName() const { char* name = BNGetSymbolShortName(m_object); string result = name; BNFreeString(name); return result; } string Symbol::GetFullName() const { char* name = BNGetSymbolFullName(m_object); string result = name; BNFreeString(name); return result; } string Symbol::GetRawName() const { char* name = BNGetSymbolRawName(m_object); string result = name; BNFreeString(name); return result; } uint64_t Symbol::GetAddress() const { return BNGetSymbolAddress(m_object); } uint64_t Symbol::GetOrdinal() const { return BNGetSymbolOrdinal(m_object); } bool Symbol::IsAutoDefined() const { return BNIsSymbolAutoDefined(m_object); } Ref Symbol::ImportedFunctionFromImportAddressSymbol(Symbol* sym, uint64_t addr) { return new Symbol(BNImportedFunctionFromImportAddressSymbol(sym->GetObject(), addr)); } AnalysisCompletionEvent::AnalysisCompletionEvent(BinaryView* view, const std::function& callback) : m_callback(callback) { m_object = BNAddAnalysisCompletionEvent(view->GetObject(), this, CompletionCallback); } void AnalysisCompletionEvent::CompletionCallback(void* ctxt) { AnalysisCompletionEvent* event = (AnalysisCompletionEvent*)ctxt; unique_lock lock(event->m_mutex); event->m_callback(); event->m_callback = []() { }; } void AnalysisCompletionEvent::Cancel() { unique_lock lock(m_mutex); m_callback = []() { }; // This allows the API side to free the BinaryNinja::AnalysisCompletionEvent object BNCancelAnalysisCompletionEvent(m_object); } TagType::TagType(BNTagType* tagType) { m_object = tagType; } TagType::TagType(BinaryView* view) { m_object = BNCreateTagType(view->GetObject()); } TagType::TagType(BinaryView* view, const std::string& name, const std::string& icon, bool visible, TagType::Type type) { m_object = BNCreateTagType(view->GetObject()); SetName(name); SetIcon(icon); SetVisible(visible); SetType(type); } BinaryView* TagType::GetView() const { return new BinaryView(BNTagTypeGetView(m_object)); } std::string TagType::GetId() const { char* str = BNTagTypeGetId(m_object); string result = str; BNFreeString(str); return result; } std::string TagType::GetName() const { char* str = BNTagTypeGetName(m_object); string result = str; BNFreeString(str); return result; } void TagType::SetName(const std::string& name) { BNTagTypeSetName(m_object, name.c_str()); } std::string TagType::GetIcon() const { return BNTagTypeGetIcon(m_object); } void TagType::SetIcon(const std::string& icon) { BNTagTypeSetIcon(m_object, icon.c_str()); } bool TagType::GetVisible() const { return BNTagTypeGetVisible(m_object); } void TagType::SetVisible(bool visible) { BNTagTypeSetVisible(m_object, visible); } TagType::Type TagType::GetType() const { return BNTagTypeGetType(m_object); } void TagType::SetType(TagType::Type type) { BNTagTypeSetType(m_object, type); } Tag::Tag(BNTag* tag) { m_object = tag; } Tag::Tag(Ref type, const std::string& data) { m_object = BNCreateTag(type->GetObject(), data.c_str()); } std::string Tag::GetId() const { char* id = BNTagGetId(m_object); std::string result = id; BNFreeString(id); return result; } Ref Tag::GetType() const { return new TagType(BNTagGetType(m_object)); } std::string Tag::GetData() const { return BNTagGetData(m_object); } void Tag::SetData(const std::string& data) { BNTagSetData(m_object, data.c_str()); } BNTag** Tag::CreateTagList(const std::vector>& tags, size_t* count) { *count = tags.size(); BNTag** result = new BNTag*[tags.size()]; for (size_t i = 0; i < tags.size(); i++) result[i] = tags[i]->GetObject(); return result; } std::vector> Tag::ConvertTagList(BNTag** tags, size_t count) { std::vector> result; result.reserve(count); for (size_t i = 0; i < count; i++) result.emplace_back(new Tag(BNNewTagReference(tags[i]))); return result; } void Tag::FreeTagList(BNTag** tags, size_t count) { delete[] tags; (void)count; } std::vector> Tag::ConvertAndFreeTagList(BNTag** tags, size_t count) { auto result = ConvertTagList(tags, count); BNFreeTagList(tags, count); return result; } TagReference::TagReference() {} TagReference::TagReference(const BNTagReference& ref) { refType = ref.refType; autoDefined = ref.autoDefined; tag = ref.tag ? new Tag(BNNewTagReference(ref.tag)) : nullptr; arch = ref.arch ? new CoreArchitecture(ref.arch) : nullptr; func = ref.func ? new Function(BNNewFunctionReference(ref.func)) : nullptr; addr = ref.addr; } bool TagReference::operator==(const TagReference& other) const { if (refType != other.refType) return false; if (autoDefined != other.autoDefined) return false; if (tag != other.tag) return false; switch (refType) { case AddressTagReference: return func == other.func && arch == other.arch && addr == other.addr; case FunctionTagReference: return func == other.func; case DataTagReference: return addr == other.addr; default: return false; } } bool TagReference::operator!=(const TagReference& other) const { return !((*this) == other); } TagReference::operator BNTagReference() const { BNTagReference ret; ret.refType = refType; ret.autoDefined = autoDefined; ret.tag = tag->GetObject(); ret.arch = arch ? arch->GetObject() : nullptr; ret.func = func ? func->GetObject() : nullptr; ret.addr = addr; return ret; } BNTagReference* TagReference::CreateTagReferenceList(const std::vector& tags, size_t* count) { *count = tags.size(); BNTagReference* refs = new BNTagReference[*count]; for (size_t i = 0; i < *count; i++) { refs[i] = tags[i]; } return refs; } std::vector TagReference::ConvertTagReferenceList(BNTagReference* tags, size_t count) { std::vector result; result.reserve(count); for (size_t i = 0; i < count; i++) { result.emplace_back(tags[i]); } return result; } void TagReference::FreeTagReferenceList(BNTagReference* tags, size_t count) { delete[] tags; (void)count; } std::vector TagReference::ConvertAndFreeTagReferenceList(BNTagReference* tags, size_t count) { auto result = ConvertTagReferenceList(tags, count); BNFreeTagReferences(tags, count); return result; } Segment::Segment(BNSegment* seg) { m_object = seg; } uint64_t Segment::GetStart() const { return BNSegmentGetStart(m_object); } uint64_t Segment::GetLength() const { return BNSegmentGetLength(m_object); } uint64_t Segment::GetEnd() const { return BNSegmentGetEnd(m_object); } uint64_t Segment::GetDataEnd() const { return BNSegmentGetDataEnd(m_object); } uint64_t Segment::GetDataOffset() const { return BNSegmentGetDataOffset(m_object); } uint64_t Segment::GetDataLength() const { return BNSegmentGetDataLength(m_object); } uint32_t Segment::GetFlags() const { return BNSegmentGetFlags(m_object); } bool Segment::IsAutoDefined() const { return BNSegmentIsAutoDefined(m_object); } Section::Section(BNSection* sec) { m_object = sec; } std::string Section::GetName() const { char* str = BNSectionGetName(m_object); string result = str; BNFreeString(str); return result; } std::string Section::GetType() const { char* str = BNSectionGetType(m_object); string result = str; BNFreeString(str); return result; } uint64_t Section::GetStart() const { return BNSectionGetStart(m_object); } uint64_t Section::GetLength() const { return BNSectionGetLength(m_object); } uint64_t Section::GetEnd() const { return BNSectionGetEnd(m_object); } uint64_t Section::GetInfoData() const { return BNSectionGetInfoData(m_object); } uint64_t Section::GetAlignment() const { return BNSectionGetAlign(m_object); } uint64_t Section::GetEntrySize() const { return BNSectionGetEntrySize(m_object); } std::string Section::GetLinkedSection() const { return BNSectionGetLinkedSection(m_object); } std::string Section::GetInfoSection() const { return BNSectionGetInfoSection(m_object); } BNSectionSemantics Section::GetSemantics() const { return BNSectionGetSemantics(m_object); } bool Section::AutoDefined() const { return BNSectionIsAutoDefined(m_object); } BinaryView::BinaryView(const std::string& typeName, FileMetadata* file, BinaryView* parentView) { BNCustomBinaryView view; view.context = this; view.init = InitCallback; view.freeObject = FreeCallback; view.externalRefTaken = nullptr; view.externalRefReleased = nullptr; view.read = ReadCallback; view.write = WriteCallback; view.insert = InsertCallback; view.remove = RemoveCallback; view.getModification = GetModificationCallback; view.isValidOffset = IsValidOffsetCallback; view.isOffsetReadable = IsOffsetReadableCallback; view.isOffsetWritable = IsOffsetWritableCallback; view.isOffsetExecutable = IsOffsetExecutableCallback; view.isOffsetBackedByFile = IsOffsetBackedByFileCallback; view.getNextValidOffset = GetNextValidOffsetCallback; view.getStart = GetStartCallback; view.getLength = GetLengthCallback; view.getEntryPoint = GetEntryPointCallback; view.isExecutable = IsExecutableCallback; view.getDefaultEndianness = GetDefaultEndiannessCallback; view.isRelocatable = IsRelocatableCallback; view.getAddressSize = GetAddressSizeCallback; view.save = SaveCallback; m_file = file; AddRefForRegistration(); m_object = BNCreateCustomBinaryView( typeName.c_str(), m_file->GetObject(), parentView ? parentView->GetObject() : nullptr, &view); m_memoryMap = std::make_unique(m_object); } BinaryView::BinaryView(BNBinaryView* view) { m_object = view; m_file = new FileMetadata(BNGetFileForView(m_object)); m_memoryMap = std::make_unique(m_object); } bool BinaryView::InitCallback(void* ctxt) { CallbackRef view(ctxt); return view->Init(); } void BinaryView::FreeCallback(void* ctxt) { BinaryView* view = (BinaryView*)ctxt; view->ReleaseForRegistration(); } size_t BinaryView::ReadCallback(void* ctxt, void* dest, uint64_t offset, size_t len) { CallbackRef view(ctxt); return view->PerformRead(dest, offset, len); } size_t BinaryView::WriteCallback(void* ctxt, uint64_t offset, const void* src, size_t len) { CallbackRef view(ctxt); return view->PerformWrite(offset, src, len); } size_t BinaryView::InsertCallback(void* ctxt, uint64_t offset, const void* src, size_t len) { CallbackRef view(ctxt); return view->PerformInsert(offset, src, len); } size_t BinaryView::RemoveCallback(void* ctxt, uint64_t offset, uint64_t len) { CallbackRef view(ctxt); return view->PerformRemove(offset, len); } BNModificationStatus BinaryView::GetModificationCallback(void* ctxt, uint64_t offset) { CallbackRef view(ctxt); return view->PerformGetModification(offset); } bool BinaryView::IsValidOffsetCallback(void* ctxt, uint64_t offset) { CallbackRef view(ctxt); return view->PerformIsValidOffset(offset); } bool BinaryView::IsOffsetReadableCallback(void* ctxt, uint64_t offset) { CallbackRef view(ctxt); return view->PerformIsOffsetReadable(offset); } bool BinaryView::IsOffsetWritableCallback(void* ctxt, uint64_t offset) { CallbackRef view(ctxt); return view->PerformIsOffsetWritable(offset); } bool BinaryView::IsOffsetExecutableCallback(void* ctxt, uint64_t offset) { CallbackRef view(ctxt); return view->PerformIsOffsetExecutable(offset); } bool BinaryView::IsOffsetBackedByFileCallback(void* ctxt, uint64_t offset) { CallbackRef view(ctxt); return view->PerformIsOffsetBackedByFile(offset); } uint64_t BinaryView::GetNextValidOffsetCallback(void* ctxt, uint64_t offset) { CallbackRef view(ctxt); return view->PerformGetNextValidOffset(offset); } uint64_t BinaryView::GetStartCallback(void* ctxt) { CallbackRef view(ctxt); return view->PerformGetStart(); } uint64_t BinaryView::GetLengthCallback(void* ctxt) { CallbackRef view(ctxt); return view->PerformGetLength(); } uint64_t BinaryView::GetEntryPointCallback(void* ctxt) { CallbackRef view(ctxt); return view->PerformGetEntryPoint(); } bool BinaryView::IsExecutableCallback(void* ctxt) { CallbackRef view(ctxt); return view->PerformIsExecutable(); } BNEndianness BinaryView::GetDefaultEndiannessCallback(void* ctxt) { CallbackRef view(ctxt); return view->PerformGetDefaultEndianness(); } bool BinaryView::IsRelocatableCallback(void* ctxt) { CallbackRef view(ctxt); return view->PerformIsRelocatable(); } size_t BinaryView::GetAddressSizeCallback(void* ctxt) { CallbackRef view(ctxt); return view->PerformGetAddressSize(); } bool BinaryView::SaveCallback(void* ctxt, BNFileAccessor* file) { CallbackRef view(ctxt); CoreFileAccessor accessor(file); return view->PerformSave(&accessor); } bool BinaryView::PerformIsValidOffset(uint64_t offset) { uint8_t val; return PerformRead(&val, offset, 1) == 1; } bool BinaryView::PerformIsOffsetReadable(uint64_t offset) { return PerformIsValidOffset(offset); } bool BinaryView::PerformIsOffsetWritable(uint64_t offset) { return PerformIsValidOffset(offset); } bool BinaryView::PerformIsOffsetExecutable(uint64_t offset) { return PerformIsValidOffset(offset); } bool BinaryView::PerformIsOffsetBackedByFile(uint64_t offset) { return PerformIsValidOffset(offset); } uint64_t BinaryView::PerformGetNextValidOffset(uint64_t offset) { if (offset < PerformGetStart()) return PerformGetStart(); return offset; } BNEndianness BinaryView::PerformGetDefaultEndianness() const { Ref arch = GetDefaultArchitecture(); if (arch) return arch->GetEndianness(); return LittleEndian; } bool BinaryView::PerformIsRelocatable() const { return false; } size_t BinaryView::PerformGetAddressSize() const { Ref arch = GetDefaultArchitecture(); if (arch) return arch->GetAddressSize(); if (GetEnd() > (1LL << 32)) return 8; return 4; } bool BinaryView::PerformSave(FileAccessor* file) { Ref parent = GetParentView(); if (parent) return parent->Save(file); return false; } void BinaryView::PerformDefineRelocation(Architecture* arch, BNRelocationInfo& info, uint64_t target, uint64_t reloc) { DefineRelocation(arch, info, target, reloc); } void BinaryView::PerformDefineRelocation(Architecture* arch, BNRelocationInfo& info, Ref target, uint64_t reloc) { DefineRelocation(arch, info, target, reloc); } void BinaryView::NotifyDataWritten(uint64_t offset, size_t len) { BNNotifyDataWritten(m_object, offset, len); } void BinaryView::NotifyDataInserted(uint64_t offset, size_t len) { BNNotifyDataInserted(m_object, offset, len); } void BinaryView::NotifyDataRemoved(uint64_t offset, uint64_t len) { BNNotifyDataRemoved(m_object, offset, len); } Ref BinaryView::GetParentView() const { BNBinaryView* view = BNGetParentView(m_object); if (!view) return nullptr; return new BinaryView(view); } string BinaryView::GetTypeName() const { char* str = BNGetViewType(m_object); string result = str; BNFreeString(str); return result; } bool BinaryView::IsModified() const { return BNIsViewModified(m_object); } bool BinaryView::IsAnalysisChanged() const { return m_file->IsAnalysisChanged(); } bool BinaryView::CreateDatabase(const string& path, Ref settings) { auto parent = GetParentView(); if (parent) return parent->CreateDatabase(path, settings); return m_file->CreateDatabase(path, this, settings); } bool BinaryView::CreateDatabase(const string& path, const function& progressCallback, Ref settings) { auto parent = GetParentView(); if (parent) return parent->CreateDatabase(path, settings); return m_file->CreateDatabase(path, this, progressCallback, settings); } bool BinaryView::SaveAutoSnapshot(Ref settings) { return m_file->SaveAutoSnapshot(this, settings); } bool BinaryView::SaveAutoSnapshot( const function& progressCallback, Ref settings) { return m_file->SaveAutoSnapshot(this, progressCallback, settings); } bool BinaryView::RunUndoableTransaction(std::function func) { return m_file->RunUndoableTransaction(func); } string BinaryView::BeginUndoActions(bool anonymousAllowed) { return m_file->BeginUndoActions(anonymousAllowed); } void BinaryView::CommitUndoActions(const string& id) { m_file->CommitUndoActions(id); } void BinaryView::RevertUndoActions(const string& id) { m_file->RevertUndoActions(id); } void BinaryView::ForgetUndoActions(const std::string &id) { m_file->ForgetUndoActions(id); } bool BinaryView::CanUndo() { return m_file->CanUndo(); } bool BinaryView::Undo() { return m_file->Undo(); } bool BinaryView::CanRedo() { return m_file->CanRedo(); } bool BinaryView::Redo() { return m_file->Redo(); } string BinaryView::GetCurrentView() { return m_file->GetCurrentView(); } uint64_t BinaryView::GetCurrentOffset() { return m_file->GetCurrentOffset(); } bool BinaryView::Navigate(const string& view, uint64_t offset) { return m_file->Navigate(view, offset); } DataBuffer BinaryView::ReadBuffer(uint64_t offset, size_t len) { BNDataBuffer* result = BNReadViewBuffer(m_object, offset, len); return DataBuffer(result); } size_t BinaryView::WriteBuffer(uint64_t offset, const DataBuffer& data) { return BNWriteViewBuffer(m_object, offset, data.GetBufferObject()); } size_t BinaryView::InsertBuffer(uint64_t offset, const DataBuffer& data) { return BNInsertViewBuffer(m_object, offset, data.GetBufferObject()); } vector BinaryView::GetEntropy(uint64_t offset, size_t len, size_t blockSize) { if (!blockSize) blockSize = len; float* entopy = new float[(len / blockSize) + 1]; len = BNGetEntropy(m_object, offset, len, blockSize, entopy); vector result; result.reserve(len); for (size_t i = 0; i < len; i++) result.push_back(entopy[i]); delete[] entopy; return result; } vector BinaryView::GetModification(uint64_t offset, size_t len) { BNModificationStatus* mod = new BNModificationStatus[len]; len = BNGetModificationArray(m_object, offset, mod, len); vector result; result.reserve(len); for (size_t i = 0; i < len; i++) result.push_back(mod[i]); delete[] mod; return result; } uint64_t BinaryView::GetEnd() const { return BNGetEndOffset(m_object); } bool BinaryView::Save(const string& path) { return BNSaveToFilename(m_object, path.c_str()); } void BinaryView::DefineRelocation(Architecture* arch, BNRelocationInfo& info, uint64_t target, uint64_t reloc) { BNDefineRelocation(m_object, arch->GetObject(), &info, target, reloc); } void BinaryView::DefineRelocation(Architecture* arch, BNRelocationInfo& info, Ref target, uint64_t reloc) { BNDefineSymbolRelocation(m_object, arch->GetObject(), &info, target->GetObject(), reloc); } vector> BinaryView::GetRelocationRanges() const { size_t count = 0; BNRange* ranges = BNGetRelocationRanges(m_object, &count); vector> result(count); for (size_t i = 0; i < count; i++) { result.push_back({ranges[i].start, ranges[i].end}); } BNFreeRelocationRanges(ranges); return result; } vector> BinaryView::GetRelocationRangesAtAddress(uint64_t addr) const { size_t count = 0; BNRange* ranges = BNGetRelocationRangesAtAddress(m_object, addr, &count); vector> result(count); for (size_t i = 0; i < count; i++) { result.push_back({ranges[i].start, ranges[i].end}); } BNFreeRelocationRanges(ranges); return result; } vector> BinaryView::GetRelocationRangesInRange(uint64_t addr, size_t size) const { size_t count = 0; BNRange* ranges = BNGetRelocationRangesInRange(m_object, addr, size, &count); vector> result(count); for (size_t i = 0; i < count; i++) { result.push_back({ranges[i].start, ranges[i].end}); } BNFreeRelocationRanges(ranges); return result; } bool BinaryView::RangeContainsRelocation(uint64_t addr, size_t size) const { return BNRangeContainsRelocation(m_object, addr, size); } std::vector> BinaryView::GetRelocationsAt(uint64_t addr) const { size_t count = 0; BNRelocation** relocations = BNGetRelocationsAt(m_object, addr, &count); std::vector> result; result.reserve(count); for (size_t i = 0; i < count; i++) { result.push_back(new Relocation(relocations[i])); } BNFreeRelocationList(relocations, count); return result; } void BinaryView::RegisterNotification(BinaryDataNotification* notify) { BNRegisterDataNotification(m_object, notify->GetCallbacks()); } void BinaryView::UnregisterNotification(BinaryDataNotification* notify) { BNUnregisterDataNotification(m_object, notify->GetCallbacks()); } size_t BinaryView::Read(void* dest, uint64_t offset, size_t len) { return BNReadViewData(m_object, dest, offset, len); } size_t BinaryView::Write(uint64_t offset, const void* data, size_t len) { return BNWriteViewData(m_object, offset, data, len); } size_t BinaryView::Insert(uint64_t offset, const void* data, size_t len) { return BNInsertViewData(m_object, offset, data, len); } size_t BinaryView::Remove(uint64_t offset, uint64_t len) { return BNRemoveViewData(m_object, offset, len); } BNModificationStatus BinaryView::GetModification(uint64_t offset) { return BNGetModification(m_object, offset); } bool BinaryView::IsValidOffset(uint64_t offset) const { return BNIsValidOffset(m_object, offset); } bool BinaryView::IsOffsetReadable(uint64_t offset) const { return BNIsOffsetReadable(m_object, offset); } bool BinaryView::IsOffsetWritable(uint64_t offset) const { return BNIsOffsetWritable(m_object, offset); } bool BinaryView::IsOffsetExecutable(uint64_t offset) const { return BNIsOffsetExecutable(m_object, offset); } bool BinaryView::IsOffsetBackedByFile(uint64_t offset) const { return BNIsOffsetBackedByFile(m_object, offset); } bool BinaryView::IsOffsetCodeSemantics(uint64_t offset) const { return BNIsOffsetCodeSemantics(m_object, offset); } bool BinaryView::IsOffsetExternSemantics(uint64_t offset) const { return BNIsOffsetExternSemantics(m_object, offset); } bool BinaryView::IsOffsetWritableSemantics(uint64_t offset) const { return BNIsOffsetWritableSemantics(m_object, offset); } uint64_t BinaryView::GetNextValidOffset(uint64_t offset) const { return BNGetNextValidOffset(m_object, offset); } uint64_t BinaryView::GetImageBase() const { return BNGetImageBase(m_object); } uint64_t BinaryView::GetOriginalImageBase() const { return BNGetOriginalImageBase(m_object); } void BinaryView::SetOriginalImageBase(uint64_t imageBase) { return BNSetOriginalImageBase(m_object, imageBase); } uint64_t BinaryView::GetOriginalBase() const { return BNGetOriginalImageBase(m_object); } void BinaryView::SetOriginalBase(uint64_t base) { return BNSetOriginalImageBase(m_object, base); } uint64_t BinaryView::GetStart() const { return BNGetStartOffset(m_object); } uint64_t BinaryView::GetLength() const { return BNGetViewLength(m_object); } uint64_t BinaryView::GetEntryPoint() const { return BNGetEntryPoint(m_object); } Ref BinaryView::GetDefaultArchitecture() const { BNArchitecture* arch = BNGetDefaultArchitecture(m_object); if (!arch) return nullptr; return new CoreArchitecture(arch); } void BinaryView::SetDefaultArchitecture(Architecture* arch) { if (arch) BNSetDefaultArchitecture(m_object, arch->GetObject()); else BNSetDefaultArchitecture(m_object, nullptr); } Ref BinaryView::GetDefaultPlatform() const { BNPlatform* platform = BNGetDefaultPlatform(m_object); if (!platform) return nullptr; return new CorePlatform(platform); } void BinaryView::SetDefaultPlatform(Platform* platform) { if (platform) BNSetDefaultPlatform(m_object, platform->GetObject()); else BNSetDefaultPlatform(m_object, nullptr); } BNEndianness BinaryView::GetDefaultEndianness() const { return BNGetDefaultEndianness(m_object); } bool BinaryView::IsRelocatable() const { return BNIsRelocatable(m_object); } size_t BinaryView::GetAddressSize() const { return BNGetViewAddressSize(m_object); } bool BinaryView::IsExecutable() const { return BNIsExecutableView(m_object); } bool BinaryView::Save(FileAccessor* file) { return BNSaveToFile(m_object, file->GetCallbacks()); } void BinaryView::AddAnalysisOption(const string& name) { BNAddAnalysisOption(m_object, name.c_str()); } Ref BinaryView::AddFunctionForAnalysis(Platform* platform, uint64_t addr, bool autoDiscovered, Type* type) { BNFunction* func = BNAddFunctionForAnalysis( m_object, platform->GetObject(), addr, autoDiscovered, type ? type->GetObject() : nullptr); if (!func) return nullptr; return new Function(func); } void BinaryView::AddEntryPointForAnalysis(Platform* platform, uint64_t addr) { BNAddEntryPointForAnalysis(m_object, platform->GetObject(), addr); } void BinaryView::AddToEntryFunctions(Function* func) { BNAddToEntryFunctions(m_object, func->GetObject()); } void BinaryView::RemoveAnalysisFunction(Function* func, bool updateRefs) { BNRemoveAnalysisFunction(m_object, func->GetObject(), updateRefs); } Ref BinaryView::CreateUserFunction(Platform* platform, uint64_t start) { BNFunction* func = BNCreateUserFunction(m_object, platform->GetObject(), start); if (!func) return nullptr; return new Function(func); } void BinaryView::RemoveUserFunction(Function* func) { BNRemoveUserFunction(m_object, func->GetObject()); } bool BinaryView::HasInitialAnalysis() { return BNHasInitialAnalysis(m_object); } void BinaryView::SetAnalysisHold(bool enable) { BNSetAnalysisHold(m_object, enable); } bool BinaryView::GetFunctionAnalysisUpdateDisabled() { return BNGetFunctionAnalysisUpdateDisabled(m_object); } void BinaryView::SetFunctionAnalysisUpdateDisabled(bool disabled) { BNSetFunctionAnalysisUpdateDisabled(m_object, disabled); } void BinaryView::UpdateAnalysisAndWait() { BNUpdateAnalysisAndWait(m_object); } void BinaryView::UpdateAnalysis() { BNUpdateAnalysis(m_object); } void BinaryView::AbortAnalysis() { BNAbortAnalysis(m_object); } void BinaryView::DefineDataVariable(uint64_t addr, const Confidence>& type) { BNTypeWithConfidence tc; tc.type = type->GetObject(); tc.confidence = type.GetConfidence(); BNDefineDataVariable(m_object, addr, &tc); } void BinaryView::DefineUserDataVariable(uint64_t addr, const Confidence>& type) { BNTypeWithConfidence tc; tc.type = type->GetObject(); tc.confidence = type.GetConfidence(); BNDefineUserDataVariable(m_object, addr, &tc); } void BinaryView::UndefineDataVariable(uint64_t addr) { BNUndefineDataVariable(m_object, addr); } void BinaryView::UndefineUserDataVariable(uint64_t addr) { BNUndefineUserDataVariable(m_object, addr); } map BinaryView::GetDataVariables() { size_t count; BNDataVariable* vars = BNGetDataVariables(m_object, &count); map result; for (size_t i = 0; i < count; i++) { result.emplace(piecewise_construct, forward_as_tuple(vars[i].address), forward_as_tuple(vars[i].address, Confidence>(new Type(BNNewTypeReference(vars[i].type)), vars[i].typeConfidence), vars[i].autoDiscovered)); } BNFreeDataVariables(vars, count); return result; } bool BinaryView::GetDataVariableAtAddress(uint64_t addr, DataVariable& var) { var.address = 0; var.type = Confidence>(nullptr, 0); var.autoDiscovered = false; BNDataVariable result; if (!BNGetDataVariableAtAddress(m_object, addr, &result)) return false; var.address = result.address; var.type = Confidence>(new Type(result.type), result.typeConfidence); var.autoDiscovered = result.autoDiscovered; return true; } vector> BinaryView::GetAnalysisFunctionList() { size_t count; BNFunction** list = BNGetAnalysisFunctionList(m_object, &count); vector> result; result.reserve(count); for (size_t i = 0; i < count; i++) result.push_back(new Function(BNNewFunctionReference(list[i]))); BNFreeFunctionList(list, count); return result; } AnalysisInfo BinaryView::GetAnalysisInfo() { AnalysisInfo result; BNAnalysisInfo* info = BNGetAnalysisInfo(m_object); result.state = info->state; result.analysisTime = info->analysisTime; result.activeInfo.reserve(info->count); for (size_t i = 0; i < info->count; i++) result.activeInfo.emplace_back(new Function(BNNewFunctionReference(info->activeInfo[i].func)), info->activeInfo[i].analysisTime, info->activeInfo[i].submitCount, info->activeInfo[i].updateCount); BNFreeAnalysisInfo(info); return result; } bool BinaryView::HasFunctions() const { return BNHasFunctions(m_object); } bool BinaryView::HasSymbols() const { return BNHasSymbols(m_object); } bool BinaryView::HasDataVariables() const { return BNHasDataVariables(m_object); } Ref BinaryView::GetAnalysisFunction(Platform* platform, uint64_t addr) { BNFunction* func = BNGetAnalysisFunction(m_object, platform->GetObject(), addr); if (!func) return nullptr; return new Function(func); } Ref BinaryView::GetRecentAnalysisFunctionForAddress(uint64_t addr) { BNFunction* func = BNGetRecentAnalysisFunctionForAddress(m_object, addr); if (!func) return nullptr; return new Function(func); } vector> BinaryView::GetAnalysisFunctionsForAddress(uint64_t addr) { size_t count; BNFunction** list = BNGetAnalysisFunctionsForAddress(m_object, addr, &count); vector> result; result.reserve(count); for (size_t i = 0; i < count; i++) result.push_back(new Function(BNNewFunctionReference(list[i]))); BNFreeFunctionList(list, count); return result; } vector> BinaryView::GetAnalysisFunctionsContainingAddress(uint64_t addr) { size_t count; BNFunction** list = BNGetAnalysisFunctionsContainingAddress(m_object, addr, &count); vector> result; result.reserve(count); for (size_t i = 0; i < count; i++) result.push_back(new Function(BNNewFunctionReference(list[i]))); BNFreeFunctionList(list, count); return result; } Ref BinaryView::GetAnalysisEntryPoint() { BNFunction* func = BNGetAnalysisEntryPoint(m_object); if (!func) return nullptr; return new Function(func); } vector> BinaryView::GetAllEntryFunctions() { size_t count; BNFunction** funcs = BNGetAllEntryFunctions(m_object, &count); if (count == 0) return {}; vector> result; for (size_t i = 0; i < count; i++) result.push_back(new Function(BNNewFunctionReference(funcs[i]))); BNFreeFunctionList(funcs, count); return result; } Ref BinaryView::GetRecentBasicBlockForAddress(uint64_t addr) { BNBasicBlock* block = BNGetRecentBasicBlockForAddress(m_object, addr); if (!block) return nullptr; return new BasicBlock(block); } vector> BinaryView::GetBasicBlocksForAddress(uint64_t addr) { size_t count; BNBasicBlock** blocks = BNGetBasicBlocksForAddress(m_object, addr, &count); vector> result; result.reserve(count); for (size_t i = 0; i < count; i++) result.push_back(new BasicBlock(BNNewBasicBlockReference(blocks[i]))); BNFreeBasicBlockList(blocks, count); return result; } vector> BinaryView::GetBasicBlocksStartingAtAddress(uint64_t addr) { size_t count; BNBasicBlock** blocks = BNGetBasicBlocksStartingAtAddress(m_object, addr, &count); vector> result; result.reserve(count); for (size_t i = 0; i < count; i++) result.push_back(new BasicBlock(BNNewBasicBlockReference(blocks[i]))); BNFreeBasicBlockList(blocks, count); return result; } vector BinaryView::GetCodeReferences(uint64_t addr) { size_t count; BNReferenceSource* refs = BNGetCodeReferences(m_object, addr, &count); vector result; result.reserve(count); for (size_t i = 0; i < count; i++) { ReferenceSource src; src.func = new Function(BNNewFunctionReference(refs[i].func)); src.arch = new CoreArchitecture(refs[i].arch); src.addr = refs[i].addr; result.push_back(src); } BNFreeCodeReferences(refs, count); return result; } vector BinaryView::GetCodeReferences(uint64_t addr, uint64_t len) { size_t count; BNReferenceSource* refs = BNGetCodeReferencesInRange(m_object, addr, len, &count); vector result; result.reserve(count); for (size_t i = 0; i < count; i++) { ReferenceSource src; src.func = new Function(BNNewFunctionReference(refs[i].func)); src.arch = new CoreArchitecture(refs[i].arch); src.addr = refs[i].addr; result.push_back(src); } BNFreeCodeReferences(refs, count); return result; } vector BinaryView::GetCodeReferencesFrom(ReferenceSource src) { size_t count; BNReferenceSource _src {src.func->m_object, src.arch->m_object, src.addr}; uint64_t* refs = BNGetCodeReferencesFrom(m_object, &_src, &count); vector result(refs, &refs[count]); BNFreeAddressList(refs); return result; } vector BinaryView::GetCodeReferencesFrom(ReferenceSource src, uint64_t len) { size_t count; BNReferenceSource _src {src.func->m_object, src.arch->m_object, src.addr}; uint64_t* refs = BNGetCodeReferencesFromInRange(m_object, &_src, len, &count); vector result(refs, &refs[count]); BNFreeAddressList(refs); return result; } vector BinaryView::GetDataReferences(uint64_t addr) { size_t count; uint64_t* refs = BNGetDataReferences(m_object, addr, &count); vector result(refs, &refs[count]); BNFreeDataReferences(refs); return result; } vector BinaryView::GetDataReferences(uint64_t addr, uint64_t len) { size_t count; uint64_t* refs = BNGetDataReferencesInRange(m_object, addr, len, &count); vector result(refs, &refs[count]); BNFreeDataReferences(refs); return result; } vector BinaryView::GetDataReferencesFrom(uint64_t addr) { size_t count; uint64_t* refs = BNGetDataReferencesFrom(m_object, addr, &count); vector result(refs, &refs[count]); BNFreeDataReferences(refs); return result; } vector BinaryView::GetDataReferencesFrom(uint64_t addr, uint64_t len) { size_t count; uint64_t* refs = BNGetDataReferencesFromInRange(m_object, addr, len, &count); vector result(refs, &refs[count]); BNFreeDataReferences(refs); return result; } void BinaryView::AddUserDataReference(uint64_t fromAddr, uint64_t toAddr) { BNAddUserDataReference(m_object, fromAddr, toAddr); } void BinaryView::RemoveUserDataReference(uint64_t fromAddr, uint64_t toAddr) { BNRemoveUserDataReference(m_object, fromAddr, toAddr); } vector BinaryView::GetCodeReferencesForType(const QualifiedName& type) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); BNReferenceSource* refs = BNGetCodeReferencesForType(m_object, &nameObj, &count); QualifiedName::FreeAPIObject(&nameObj); vector result; result.reserve(count); for (size_t i = 0; i < count; i++) { ReferenceSource src; src.func = new Function(BNNewFunctionReference(refs[i].func)); src.arch = new CoreArchitecture(refs[i].arch); src.addr = refs[i].addr; result.push_back(src); } BNFreeCodeReferences(refs, count); return result; } vector BinaryView::GetDataReferencesForType(const QualifiedName& type) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); uint64_t* refs = BNGetDataReferencesForType(m_object, &nameObj, &count); QualifiedName::FreeAPIObject(&nameObj); vector result(refs, &refs[count]); BNFreeDataReferences(refs); return result; } vector BinaryView::GetTypeReferencesForType(const QualifiedName& type) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); BNTypeReferenceSource* refs = BNGetTypeReferencesForType(m_object, &nameObj, &count); QualifiedName::FreeAPIObject(&nameObj); vector result; result.reserve(count); for (size_t i = 0; i < count; i++) { TypeReferenceSource src; src.name = QualifiedName::FromAPIObject(&refs[i].name); src.offset = refs[i].offset; src.type = refs[i].type; result.push_back(src); } BNFreeTypeReferences(refs, count); return result; } vector BinaryView::GetCodeReferencesForTypeField(const QualifiedName& type, uint64_t offset) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); BNTypeFieldReference* refs = BNGetCodeReferencesForTypeField(m_object, &nameObj, offset, &count); QualifiedName::FreeAPIObject(&nameObj); vector result; result.reserve(count); for (size_t i = 0; i < count; i++) { TypeFieldReference src; src.func = new Function(BNNewFunctionReference(refs[i].func)); src.arch = new CoreArchitecture(refs[i].arch); src.addr = refs[i].addr; src.size = refs[i].size; BNTypeWithConfidence& tc = refs[i].incomingType; Ref type = tc.type ? new Type(BNNewTypeReference(tc.type)) : nullptr; src.incomingType = Confidence>(type, tc.confidence); result.push_back(src); } BNFreeTypeFieldReferences(refs, count); return result; } vector BinaryView::GetDataReferencesForTypeField(const QualifiedName& type, uint64_t offset) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); uint64_t* refs = BNGetDataReferencesForTypeField(m_object, &nameObj, offset, &count); QualifiedName::FreeAPIObject(&nameObj); vector result(refs, &refs[count]); BNFreeDataReferences(refs); return result; } vector BinaryView::GetDataReferencesFromForTypeField(const QualifiedName& type, uint64_t offset) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); uint64_t* refs = BNGetDataReferencesFromForTypeField(m_object, &nameObj, offset, &count); QualifiedName::FreeAPIObject(&nameObj); vector result(refs, &refs[count]); BNFreeDataReferences(refs); return result; } vector BinaryView::GetTypeReferencesForTypeField(const QualifiedName& type, uint64_t offset) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); BNTypeReferenceSource* refs = BNGetTypeReferencesForTypeField(m_object, &nameObj, offset, &count); QualifiedName::FreeAPIObject(&nameObj); vector result; result.reserve(count); for (size_t i = 0; i < count; i++) { TypeReferenceSource src; src.name = QualifiedName::FromAPIObject(&refs[i].name); src.offset = refs[i].offset; src.type = refs[i].type; result.push_back(src); } BNFreeTypeReferences(refs, count); return result; } vector BinaryView::GetCodeReferencesForTypeFrom(ReferenceSource src) { size_t count; BNReferenceSource _src {src.func->m_object, src.arch->m_object, src.addr}; BNTypeReferenceSource* refs = BNGetCodeReferencesForTypeFrom(m_object, &_src, &count); vector result; result.reserve(count); for (size_t i = 0; i < count; i++) { TypeReferenceSource src; src.name = QualifiedName::FromAPIObject(&refs[i].name); src.offset = refs[i].offset; src.type = refs[i].type; result.push_back(src); } BNFreeTypeReferences(refs, count); return result; } vector BinaryView::GetCodeReferencesForTypeFrom(ReferenceSource src, uint64_t len) { size_t count; BNReferenceSource _src {src.func->m_object, src.arch->m_object, src.addr}; BNTypeReferenceSource* refs = BNGetCodeReferencesForTypeFromInRange(m_object, &_src, len, &count); vector result; result.reserve(count); for (size_t i = 0; i < count; i++) { TypeReferenceSource src; src.name = QualifiedName::FromAPIObject(&refs[i].name); src.offset = refs[i].offset; src.type = refs[i].type; result.push_back(src); } BNFreeTypeReferences(refs, count); return result; } vector BinaryView::GetCodeReferencesForTypeFieldFrom(ReferenceSource src) { size_t count; BNReferenceSource _src {src.func->m_object, src.arch->m_object, src.addr}; BNTypeReferenceSource* refs = BNGetCodeReferencesForTypeFieldsFrom(m_object, &_src, &count); vector result; result.reserve(count); for (size_t i = 0; i < count; i++) { TypeReferenceSource src; src.name = QualifiedName::FromAPIObject(&refs[i].name); src.offset = refs[i].offset; src.type = refs[i].type; result.push_back(src); } BNFreeTypeReferences(refs, count); return result; } vector BinaryView::GetCodeReferencesForTypeFieldFrom(ReferenceSource src, uint64_t len) { size_t count; BNReferenceSource _src {src.func->m_object, src.arch->m_object, src.addr}; BNTypeReferenceSource* refs = BNGetCodeReferencesForTypeFieldsFromInRange(m_object, &_src, len, &count); vector result; result.reserve(count); for (size_t i = 0; i < count; i++) { TypeReferenceSource src; src.name = QualifiedName::FromAPIObject(&refs[i].name); src.offset = refs[i].offset; src.type = refs[i].type; result.push_back(src); } BNFreeTypeReferences(refs, count); return result; } vector BinaryView::GetAllFieldsReferenced(const QualifiedName& type) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); uint64_t* fields = BNGetAllFieldsReferenced(m_object, &nameObj, &count); vector result(fields, &fields[count]); // Data refs and the fields above are both an array of uint64_t, so they can be freed in // the same way BNFreeDataReferences(fields); return result; } std::map> BinaryView::GetAllSizesReferenced(const QualifiedName& type) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); BNTypeFieldReferenceSizeInfo* fields = BNGetAllSizesReferenced(m_object, &nameObj, &count); std::map> result; for (size_t i = 0; i < count; i++) { auto& sizes = result[fields[i].offset]; for (size_t j = 0; j < fields[i].count; j++) { sizes.push_back(fields[i].sizes[j]); } } BNFreeTypeFieldReferenceSizeInfo(fields, count); return result; } std::map>>> BinaryView::GetAllTypesReferenced(const QualifiedName& type) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); BNTypeFieldReferenceTypeInfo* fields = BNGetAllTypesReferenced(m_object, &nameObj, &count); std::map>>> result; for (size_t i = 0; i < count; i++) { auto& types = result[fields[i].offset]; for (size_t j = 0; j < fields[i].count; j++) { BNTypeWithConfidence tc = fields[i].types[j]; Ref type = tc.type ? new Type(BNNewTypeReference(tc.type)) : nullptr; types.push_back(Confidence>(type, tc.confidence)); } } BNFreeTypeFieldReferenceTypeInfo(fields, count); return result; } std::vector BinaryView::GetSizesReferenced(const QualifiedName& type, uint64_t offset) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); size_t* refs = BNGetSizesReferenced(m_object, &nameObj, offset, &count); std::vector result; result.reserve(count); for (size_t i = 0; i < count; i++) result[i] = refs[i]; BNFreeTypeFieldReferenceSizes(refs, count); return result; } std::vector>> BinaryView::GetTypesReferenced(const QualifiedName& type, uint64_t offset) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); BNTypeWithConfidence* types = BNGetTypesReferenced(m_object, &nameObj, offset, &count); std::vector>> result; result.reserve(count); for (size_t i = 0; i < count; i++) { BNTypeWithConfidence tc = types[i]; Ref type = tc.type ? new Type(BNNewTypeReference(tc.type)) : nullptr; result.push_back(Confidence>(type, tc.confidence)); } BNFreeTypeFieldReferenceTypes(types, count); return result; } unordered_set BinaryView::GetOutgoingDirectTypeReferences(const QualifiedName& type) { size_t count; BNQualifiedName apiType = type.GetAPIObject(); BNQualifiedName* apiResult = BNGetOutgoingDirectTypeReferences(m_object, &apiType, &count); QualifiedName::FreeAPIObject(&apiType); if (!apiResult) return {}; unordered_set result; for (size_t i = 0; i < count; i ++) { result.insert(QualifiedName::FromAPIObject(&apiResult[i])); } BNFreeTypeNameList(apiResult, count); return result; } unordered_set BinaryView::GetOutgoingRecursiveTypeReferences(const QualifiedName& type) { return GetOutgoingRecursiveTypeReferences(unordered_set{type}); } unordered_set BinaryView::GetOutgoingRecursiveTypeReferences(const unordered_set& types) { size_t count; vector