From 4108964609171327618627477163339656037111 Mon Sep 17 00:00:00 2001 From: Glenn Smith Date: Mon, 20 Feb 2023 21:58:55 -0500 Subject: Type Containers --- binaryninjaapi.h | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 96 insertions(+), 7 deletions(-) (limited to 'binaryninjaapi.h') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 7a4efee4..d32af5ce 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -5125,6 +5125,9 @@ namespace BinaryNinja { bool ParseTypesFromSource(const std::string& text, const std::vector& options, const std::vector& includeDirs, TypeParserResult& result, std::string& errors, const std::set& typesAllowRedefinition = {}); + class TypeContainer GetTypeContainer(); + class TypeContainer GetAutoTypeContainer(); + class TypeContainer GetUserTypeContainer(); std::map> GetTypes(); /*! List of all types, sorted such that types are after all types on which they depend @@ -7881,7 +7884,7 @@ namespace BinaryNinja { bool AddTypeMemberTokens(BinaryView* data, std::vector& tokens, int64_t offset, std::vector& nameList, size_t size = 0, bool indirect = false); - std::vector GetLines(Ref data, const std::string& name, + std::vector GetLines(const TypeContainer& types, const std::string& name, int lineWidth = 80, bool collapsed = false, BNTokenEscapingType escaping = NoTokenEscapingType); static std::string GetSizeSuffix(size_t size); @@ -8138,7 +8141,7 @@ namespace BinaryNinja { \return The list of structure members */ - std::vector GetMembersIncludingInherited(BinaryView* view) const; + std::vector GetMembersIncludingInherited(const TypeContainer& types) const; /*! Get a structure member (including inherited members) at a certain offset @@ -8147,7 +8150,7 @@ namespace BinaryNinja { \param result Reference to a InheritedStructureMember to copy the result to \return Whether a member was found */ - bool GetMemberIncludingInheritedAtOffset(BinaryView* view, int64_t offset, + bool GetMemberIncludingInheritedAtOffset(BinaryView* view, int64_t offset, InheritedStructureMember& result) const; /*! Get a structure member by name @@ -13573,6 +13576,7 @@ namespace BinaryNinja { std::vector> GetTypeLibrariesByName(const std::string& name); + TypeContainer GetTypeContainer(); Ref GetTypeByName(const QualifiedName& name); Ref GetVariableByName(const QualifiedName& name); @@ -13851,7 +13855,7 @@ namespace BinaryNinja { BNPlatform* platform, BNTokenEscapingType escaping, char** result); static bool GetTypeStringAfterNameCallback(void* ctxt, BNType* type, BNPlatform* platform, BNTokenEscapingType escaping, char** result); - static bool GetTypeLinesCallback(void* ctxt, BNType* type, BNBinaryView* data, + static bool GetTypeLinesCallback(void* ctxt, BNType* type, BNTypeContainer* types, BNQualifiedName* name, int lineWidth, bool collapsed, BNTokenEscapingType escaping, BNTypeDefinitionLine** result, size_t* resultCount); static bool PrintAllTypesCallback(void* ctxt, BNQualifiedName* names, BNType** types, size_t typeCount, @@ -13965,7 +13969,7 @@ namespace BinaryNinja { /*! Generate a multi-line representation of a type \param type Type to print - \param data Binary View in which the type is defined + \param types Type Container in which the type is defined \param name Name of the type \param lineWidth Maximum width of lines, in characters \param collapsed Whether to collapse structure/enum blocks @@ -13974,7 +13978,7 @@ namespace BinaryNinja { */ virtual std::vector GetTypeLines( Ref type, - Ref data, + const TypeContainer& types, const QualifiedName& name, int lineWidth = 80, bool collapsed = false, @@ -14038,7 +14042,7 @@ namespace BinaryNinja { virtual std::string GetTypeStringAfterName(Ref type, Ref platform, BNTokenEscapingType escaping) override; virtual std::vector GetTypeLines(Ref type, - Ref data, const QualifiedName& name, int lineWidth, + const TypeContainer& types, const QualifiedName& name, int lineWidth, bool collapsed, BNTokenEscapingType escaping) override; virtual std::string PrintAllTypes(const std::vector>>& types, Ref data, int lineWidth, BNTokenEscapingType escaping) override; @@ -15203,6 +15207,8 @@ namespace BinaryNinja { std::vector GetParsers() const; + TypeContainer GetTypeContainer(const std::string& parserName); + std::vector GetTypes(const std::string& parserName = "") const; std::vector GetFunctions(const std::string& parserName = "") const; std::vector GetDataVariables(const std::string& parserName = "") const; @@ -15625,6 +15631,12 @@ namespace BinaryNinja { */ void SetGuid(const std::string& guid); + /*! Get a TypeContainer interface for this type library + + \return Container interface + */ + TypeContainer GetTypeContainer(); + /*! Direct extracts a reference to a contained object -- when attempting to extract types from a library into a BinaryView, consider using BinaryView::ImportTypeLibraryObject instead. @@ -15746,6 +15758,61 @@ namespace BinaryNinja { void Finalize(); }; + class TypeContainer + { + BNTypeContainer* m_object; + + public: + explicit TypeContainer(BNTypeContainer* container); + explicit TypeContainer(TypeContainer&& other); + + TypeContainer(Ref data); + TypeContainer(Ref library); + TypeContainer(Ref platform); + + ~TypeContainer(); + TypeContainer(const TypeContainer& other); + TypeContainer& operator=(const TypeContainer& other); + TypeContainer& operator=(TypeContainer&& other); + bool operator==(const TypeContainer& other) const { return GetId() == other.GetId(); } + bool operator!=(const TypeContainer& other) const { return !operator==(other); } + + BNTypeContainer* GetObject() const { return m_object; } + + std::string GetId() const; + std::string GetName() const; + BNTypeContainerType GetType() const; + bool IsMutable() const; + Ref GetPlatform() const; + + std::optional AddType(QualifiedName name, Ref type); + std::optional> AddTypes( + const std::vector>>& types, + std::function progress = {}); + bool RenameType(const std::string& typeId, const QualifiedName& newName); + bool DeleteType(const std::string& typeId); + + std::optional GetTypeId(const QualifiedName& typeName) const; + std::optional GetTypeName(const std::string& typeId) const; + std::optional> GetTypeById(const std::string& typeId) const; + std::optional>>> GetTypes() const; + + std::optional> GetTypeByName(const QualifiedName& typeName) const; + std::optional> GetTypeIds() const; + std::optional> GetTypeNames() const; + std::optional> GetTypeNamesAndIds() const; + + bool ParseTypesFromSource( + const std::string& text, + const std::string& fileName, + const std::vector& options, + const std::vector& includeDirs, + const std::string& autoTypeSource, + TypeParserResult& result, + std::vector& errors + ); + }; + /*! \ingroup binaryview */ @@ -15764,3 +15831,25 @@ namespace BinaryNinja { void Process(); }; } // namespace BinaryNinja + + +namespace std +{ + template<> struct hash + { + typedef BinaryNinja::QualifiedName argument_type; + size_t operator()(argument_type const& value) const + { + return std::hash()(value.GetString()); + } + }; + + template struct hash> + { + typedef BinaryNinja::Ref argument_type; + size_t operator()(argument_type const& value) const + { + return std::hash()(T::GetObject(value.GetPtr())); + } + }; +} // namespace std -- cgit v1.3.1