diff options
| author | Glenn Smith <glenn@vector35.com> | 2025-02-12 15:29:03 -0500 |
|---|---|---|
| committer | Glenn Smith <glenn@vector35.com> | 2025-02-13 13:51:53 -0500 |
| commit | 1925885513166893508719453618249182735388 (patch) | |
| tree | 073055483454543f5e0043fb627c8514577fe6cc | |
| parent | 88b364801d4d740435b5dfe5c6a07736a00d233d (diff) | |
StringRef and direct access to symbol names
| -rw-r--r-- | binaryninjaapi.h | 66 | ||||
| -rw-r--r-- | binaryninjacore.h | 9 | ||||
| -rw-r--r-- | binaryview.cpp | 98 |
3 files changed, 173 insertions, 0 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 7ec05868..5ee30625 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -3888,6 +3888,35 @@ namespace BinaryNinja { static NameSpace FromAPIObject(const BNNameSpace* name); }; + class StringRef + { + BNStringRef* m_ref; + + public: + StringRef(); + explicit StringRef(BNStringRef* ref); + StringRef(const StringRef& other); + StringRef(StringRef&& other); + ~StringRef(); + StringRef& operator=(const StringRef& other); + StringRef& operator=(StringRef&& other); + + operator std::string_view() const { return std::string_view(c_str(), size()); } + operator std::string const() { return c_str(); } + + const char* c_str() const; + size_t size() const; + BNStringRef* GetObject() { return m_ref; } + + bool operator==(const StringRef& other) const { return this->operator std::string_view() == other.operator std::string_view(); } + bool operator!=(const StringRef& other) const { return this->operator std::string_view() != other.operator std::string_view(); } + bool operator<(const StringRef& other) const { return this->operator std::string_view() < other.operator std::string_view(); } + bool operator==(const std::string& other) const { return this->operator std::string_view() == other; } + bool operator!=(const std::string& other) const { return this->operator std::string_view() != other; } + bool operator==(const std::string_view& other) const { return this->operator std::string_view() == other; } + bool operator!=(const std::string_view& other) const { return this->operator std::string_view() != other; } + }; + /*! \ingroup types */ @@ -3935,16 +3964,31 @@ namespace BinaryNinja { std::string GetShortName() const; /*! + \return Symbol short name + */ + StringRef GetShortNameRef() const; + + /*! \return Symbol full name */ std::string GetFullName() const; /*! + \return Symbol full name + */ + StringRef GetFullNameRef() const; + + /*! \return Symbol raw name */ std::string GetRawName() const; /*! + \return Symbol raw name + */ + StringRef GetRawNameRef() const; + + /*! \return Symbol Address */ uint64_t GetAddress() const; @@ -20662,6 +20706,15 @@ namespace std return std::hash<decltype(T::GetObject(value.GetPtr()))>()(T::GetObject(value.GetPtr())); } }; + + template<> struct hash<BinaryNinja::StringRef> + { + typedef BinaryNinja::StringRef argument_type; + size_t operator()(argument_type const& value) const + { + return std::hash<std::string_view>()(value.operator std::string_view()); + } + }; } // namespace std @@ -20695,6 +20748,19 @@ template<> struct fmt::formatter<BinaryNinja::NameList> constexpr auto parse(format_parse_context& ctx) -> format_parse_context::iterator { return ctx.begin(); } }; + +template<> struct fmt::formatter<BinaryNinja::StringRef> : fmt::formatter<std::string_view> +{ + format_context::iterator format(const BinaryNinja::StringRef& obj, format_context& ctx) const + { + return fmt::formatter<std::string_view>::format(obj.operator std::string_view(), ctx); + } + constexpr auto parse(format_parse_context& ctx) -> format_parse_context::iterator + { + return fmt::formatter<std::string_view>::parse(ctx); + } +}; + template<typename T> struct fmt::formatter<T, char, std::enable_if_t<std::is_enum_v<T>, void>> { diff --git a/binaryninjacore.h b/binaryninjacore.h index 351f5c53..064a4b8e 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -305,6 +305,7 @@ extern "C" typedef struct BNFirmwareNinjaRelationship BNFirmwareNinjaRelationship; typedef struct BNLineFormatter BNLineFormatter; typedef struct BNRenderLayer BNRenderLayer; + typedef struct BNStringRef BNStringRef; //! Console log levels typedef enum BNLogLevel @@ -5654,8 +5655,11 @@ extern "C" BINARYNINJACOREAPI BNSymbolBinding BNGetSymbolBinding(BNSymbol* sym); BINARYNINJACOREAPI BNNameSpace BNGetSymbolNameSpace(BNSymbol* sym); BINARYNINJACOREAPI char* BNGetSymbolShortName(BNSymbol* sym); + BINARYNINJACOREAPI BNStringRef* BNGetSymbolShortNameRef(BNSymbol* sym); BINARYNINJACOREAPI char* BNGetSymbolFullName(BNSymbol* sym); + BINARYNINJACOREAPI BNStringRef* BNGetSymbolFullNameRef(BNSymbol* sym); BINARYNINJACOREAPI char* BNGetSymbolRawName(BNSymbol* sym); + BINARYNINJACOREAPI BNStringRef* BNGetSymbolRawNameRef(BNSymbol* sym); BINARYNINJACOREAPI void* BNGetSymbolRawBytes(BNSymbol* sym, size_t* count); BINARYNINJACOREAPI void BNFreeSymbolRawBytes(void* bytes); @@ -8216,6 +8220,11 @@ extern "C" size_t* outLineCount ); + BINARYNINJACOREAPI void BNFreeStringRef(BNStringRef* ref); + BINARYNINJACOREAPI BNStringRef* BNDuplicateStringRef(BNStringRef* ref); + BINARYNINJACOREAPI const char* BNGetStringRefContents(BNStringRef* ref); + BINARYNINJACOREAPI size_t BNGetStringRefSize(BNStringRef* ref); + #ifdef __cplusplus } #endif diff --git a/binaryview.cpp b/binaryview.cpp index e8638025..cb33d616 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -650,6 +650,83 @@ BinaryDataNotification::BinaryDataNotification(NotificationTypes notifications) } +StringRef::StringRef() +{ + m_ref = nullptr; +} + + +StringRef::StringRef(BNStringRef* ref) +{ + m_ref = ref; +} + + +StringRef::StringRef(const StringRef& other) +{ + m_ref = BNDuplicateStringRef(other.m_ref); +} + + +StringRef::StringRef(StringRef&& other) +{ + m_ref = other.m_ref; + other.m_ref = nullptr; +} + + +StringRef::~StringRef() +{ + if (m_ref) + { + BNFreeStringRef(m_ref); + } +} + + +StringRef& StringRef::operator=(const StringRef& other) +{ + if (m_ref) + { + BNFreeStringRef(m_ref); + m_ref = nullptr; + } + if (other.m_ref) + { + m_ref = BNDuplicateStringRef(other.m_ref); + } + return *this; +} + + +StringRef& StringRef::operator=(StringRef&& other) +{ + if (m_ref) + { + BNFreeStringRef(m_ref); + } + m_ref = other.m_ref; + other.m_ref = nullptr; + return *this; +} + + +const char* StringRef::c_str() const +{ + if (!m_ref) + return ""; // todo: nullptr? + return BNGetStringRefContents(m_ref); +} + + +size_t StringRef::size() const +{ + if (!m_ref) + return 0; + return BNGetStringRefSize(m_ref); +} + + Symbol::Symbol(BNSymbolType type, const string& shortName, const string& fullName, const string& rawName, uint64_t addr, BNSymbolBinding binding, const NameSpace& nameSpace, uint64_t ordinal) { @@ -710,6 +787,13 @@ string Symbol::GetShortName() const } +StringRef Symbol::GetShortNameRef() const +{ + BNStringRef* name = BNGetSymbolShortNameRef(m_object); + return StringRef(name); +} + + string Symbol::GetFullName() const { char* name = BNGetSymbolFullName(m_object); @@ -719,6 +803,13 @@ string Symbol::GetFullName() const } +StringRef Symbol::GetFullNameRef() const +{ + BNStringRef* name = BNGetSymbolFullNameRef(m_object); + return StringRef(name); +} + + string Symbol::GetRawName() const { char* name = BNGetSymbolRawName(m_object); @@ -728,6 +819,13 @@ string Symbol::GetRawName() const } +StringRef Symbol::GetRawNameRef() const +{ + BNStringRef* name = BNGetSymbolRawNameRef(m_object); + return StringRef(name); +} + + uint64_t Symbol::GetAddress() const { return BNGetSymbolAddress(m_object); |
