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 --- component.cpp | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 component.cpp (limited to 'component.cpp') diff --git a/component.cpp b/component.cpp new file mode 100644 index 00000000..88e7a20a --- /dev/null +++ b/component.cpp @@ -0,0 +1,163 @@ + +#include "binaryninjaapi.h" + +using namespace BinaryNinja; +using namespace std; + +bool Component::operator==(const Component& other) const +{ + return BNComponentsEqual(m_object, other.m_object); +} + + +bool Component::operator!=(const Component& other) const +{ + return BNComponentsNotEqual(m_object, other.m_object); +} + + + +Component::Component(BNComponent* component) +{ + m_object = component; +} + + +std::string Component::GetDisplayName() +{ + return BNComponentGetDisplayName(m_object); +} + + +std::string Component::GetName() +{ + return BNComponentGetOriginalName(m_object); +} + + +Ref Component::GetView() +{ + return new BinaryView(BNComponentGetView(m_object)); +} + + +void Component::SetName(const std::string &name) +{ + BNComponentSetName(m_object, name.c_str()); +} + + +Ref Component::GetParent() +{ + return new Component(BNComponentGetParent(m_object)); +} + + +std::string Component::GetGuid() +{ + return string(BNComponentGetGuid(m_object)); +} + + +bool Component::AddFunction(Ref func) +{ + return BNComponentAddFunctionReference(m_object, func->GetObject()); +} + + +bool Component::AddComponent(Ref component) +{ + return BNComponentAddComponent(m_object, component->m_object); +} + + +bool Component::RemoveComponent(Ref component) +{ + return BNComponentRemoveComponent(component->m_object); +} + + +bool Component::RemoveFunction(Ref func) +{ + return BNComponentRemoveFunctionReference(m_object, func->GetObject()); +} + + +std::vector> Component::GetContainedComponents() +{ + std::vector> components; + + size_t count; + BNComponent** list = BNComponentGetContainedComponents(m_object, &count); + + components.reserve(count); + for (size_t i = 0; i < count; i++) + { + Ref component = new Component(BNNewComponentReference(list[i])); + components.push_back(component); + } + + BNFreeComponents(list, count); + + return components; +} + + +std::vector> Component::GetContainedFunctions() +{ + std::vector> functions; + + size_t count; + BNFunction** list = BNComponentGetContainedFunctions(m_object, &count); + + functions.reserve(count); + for (size_t i = 0; i < count; i++) + { + Ref function = new Function(BNNewFunctionReference(list[i])); + functions.push_back(function); + } + + BNFreeFunctionList(list, count); + + return functions; +} + + +std::vector> Component::GetReferencedTypes() +{ + std::vector> types; + + size_t count; + BNType** list = BNComponentGetReferencedTypes(m_object, &count); + + types.reserve(count); + for (size_t i = 0; i < count; i++) + { + Ref type = new Type(BNNewTypeReference(list[i])); + types.push_back(type); + } + + BNComponentFreeReferencedTypes(list, count); + + return types; +} + + +std::vector Component::GetReferencedDataVariables() +{ + vector result; + + size_t count; + BNDataVariable* variables = BNComponentGetReferencedDataVariables(m_object, &count); + + result.reserve(count); + for (size_t i = 0; i < count; ++i) + { + result.emplace_back(variables[i].address, + Confidence(new Type(BNNewTypeReference(variables[i].type)), variables[i].typeConfidence), + variables[i].autoDiscovered); + } + + BNFreeDataVariables(variables, count); + return result; +} -- cgit v1.3.1