From e95b220ef18c9dd9e1aecab32b735ff47237f9c7 Mon Sep 17 00:00:00 2001 From: kat Date: Thu, 22 Sep 2022 09:25:01 -0400 Subject: Add the Components API --- binaryview.cpp | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) (limited to 'binaryview.cpp') diff --git a/binaryview.cpp b/binaryview.cpp index db45cc88..fa00b4e9 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -299,6 +299,71 @@ void BinaryDataNotification::SectionRemovedCallback(void* ctxt, BNBinaryView* da 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); +} + + BinaryDataNotification::BinaryDataNotification() { @@ -333,6 +398,12 @@ BinaryDataNotification::BinaryDataNotification() 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; } @@ -2873,11 +2944,103 @@ Ref BinaryView::CreateUserDataTag(uint64_t addr, Ref tagType, cons return tag; } + +std::optional> BinaryView::GetComponentByGuid(std::string guid) +{ + BNComponent* bncomponent = BNGetComponentByGuid(m_object, guid.c_str()); + + if (bncomponent) + { + auto component = new Component(bncomponent); + return std::optional>{component}; + } + + return std::nullopt; +} + +Ref BinaryView::GetRootComponent() +{ + BNComponent* bncomponent = BNGetRootComponent(m_object); + + return new Component(bncomponent); +} + + +std::optional> BinaryView::GetComponentByPath(std::string path) +{ + BNComponent* component = BNGetComponentByPath(this->m_object, path.c_str()); + if (component) + return {new Component(component)}; + return std::nullopt; +} + + +Ref BinaryView::CreateComponent() +{ + auto bnComponent = BNCreateComponent(m_object); + + return new Component(bnComponent); +} + + +Ref BinaryView::CreateComponent(std::string parentGUID) +{ + BNComponent* bnComponent; + if (!parentGUID.empty()) + bnComponent = BNCreateComponentWithParent(m_object, parentGUID.c_str()); + else + bnComponent = BNCreateComponent(m_object); + + return new Component(bnComponent); +} + + +Ref BinaryView::CreateComponent(Ref parent) +{ + auto bnComponent = BNCreateComponentWithParent(m_object, parent->GetGuid().c_str()); + + return new Component(bnComponent); +} + + +Ref BinaryView::CreateComponentWithName(std::string name, std::string parentGUID) +{ + BNComponent* bnComponent; + if (!parentGUID.empty()) + bnComponent = BNCreateComponentWithParentAndName(m_object, parentGUID.c_str(), name.c_str()); + else + bnComponent = BNCreateComponentWithName(m_object, name.c_str()); + + return new Component(bnComponent); +} + + +Ref BinaryView::CreateComponentWithName(std::string name, Ref parent) +{ + auto bnComponent = BNCreateComponentWithParentAndName(m_object, parent->GetGuid().c_str(), name.c_str()); + + return new Component(bnComponent); +} + + +bool BinaryView::RemoveComponent(Ref component) +{ + return BNRemoveComponent(m_object, component->m_object); +} + + +bool BinaryView::RemoveComponent(std::string guid) +{ + return BNRemoveComponentByGuid(m_object, guid.c_str()); +} + + bool BinaryView::CanAssemble(Architecture* arch) { return BNCanAssemble(m_object, arch->GetObject()); } + bool BinaryView::IsNeverBranchPatchAvailable(Architecture* arch, uint64_t addr) { return BNIsNeverBranchPatchAvailable(m_object, arch->GetObject(), addr); -- cgit v1.3.1