Skip to content

Commit 1925885

Browse files
committed
StringRef and direct access to symbol names
1 parent 88b3648 commit 1925885

3 files changed

Lines changed: 173 additions & 0 deletions

File tree

binaryninjaapi.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3888,6 +3888,35 @@ namespace BinaryNinja {
38883888
static NameSpace FromAPIObject(const BNNameSpace* name);
38893889
};
38903890

3891+
class StringRef
3892+
{
3893+
BNStringRef* m_ref;
3894+
3895+
public:
3896+
StringRef();
3897+
explicit StringRef(BNStringRef* ref);
3898+
StringRef(const StringRef& other);
3899+
StringRef(StringRef&& other);
3900+
~StringRef();
3901+
StringRef& operator=(const StringRef& other);
3902+
StringRef& operator=(StringRef&& other);
3903+
3904+
operator std::string_view() const { return std::string_view(c_str(), size()); }
3905+
operator std::string const() { return c_str(); }
3906+
3907+
const char* c_str() const;
3908+
size_t size() const;
3909+
BNStringRef* GetObject() { return m_ref; }
3910+
3911+
bool operator==(const StringRef& other) const { return this->operator std::string_view() == other.operator std::string_view(); }
3912+
bool operator!=(const StringRef& other) const { return this->operator std::string_view() != other.operator std::string_view(); }
3913+
bool operator<(const StringRef& other) const { return this->operator std::string_view() < other.operator std::string_view(); }
3914+
bool operator==(const std::string& other) const { return this->operator std::string_view() == other; }
3915+
bool operator!=(const std::string& other) const { return this->operator std::string_view() != other; }
3916+
bool operator==(const std::string_view& other) const { return this->operator std::string_view() == other; }
3917+
bool operator!=(const std::string_view& other) const { return this->operator std::string_view() != other; }
3918+
};
3919+
38913920
/*!
38923921
\ingroup types
38933922
*/
@@ -3934,16 +3963,31 @@ namespace BinaryNinja {
39343963
*/
39353964
std::string GetShortName() const;
39363965

3966+
/*!
3967+
\return Symbol short name
3968+
*/
3969+
StringRef GetShortNameRef() const;
3970+
39373971
/*!
39383972
\return Symbol full name
39393973
*/
39403974
std::string GetFullName() const;
39413975

3976+
/*!
3977+
\return Symbol full name
3978+
*/
3979+
StringRef GetFullNameRef() const;
3980+
39423981
/*!
39433982
\return Symbol raw name
39443983
*/
39453984
std::string GetRawName() const;
39463985

3986+
/*!
3987+
\return Symbol raw name
3988+
*/
3989+
StringRef GetRawNameRef() const;
3990+
39473991
/*!
39483992
\return Symbol Address
39493993
*/
@@ -20662,6 +20706,15 @@ namespace std
2066220706
return std::hash<decltype(T::GetObject(value.GetPtr()))>()(T::GetObject(value.GetPtr()));
2066320707
}
2066420708
};
20709+
20710+
template<> struct hash<BinaryNinja::StringRef>
20711+
{
20712+
typedef BinaryNinja::StringRef argument_type;
20713+
size_t operator()(argument_type const& value) const
20714+
{
20715+
return std::hash<std::string_view>()(value.operator std::string_view());
20716+
}
20717+
};
2066520718
} // namespace std
2066620719

2066720720

@@ -20695,6 +20748,19 @@ template<> struct fmt::formatter<BinaryNinja::NameList>
2069520748
constexpr auto parse(format_parse_context& ctx) -> format_parse_context::iterator { return ctx.begin(); }
2069620749
};
2069720750

20751+
20752+
template<> struct fmt::formatter<BinaryNinja::StringRef> : fmt::formatter<std::string_view>
20753+
{
20754+
format_context::iterator format(const BinaryNinja::StringRef& obj, format_context& ctx) const
20755+
{
20756+
return fmt::formatter<std::string_view>::format(obj.operator std::string_view(), ctx);
20757+
}
20758+
constexpr auto parse(format_parse_context& ctx) -> format_parse_context::iterator
20759+
{
20760+
return fmt::formatter<std::string_view>::parse(ctx);
20761+
}
20762+
};
20763+
2069820764
template<typename T>
2069920765
struct fmt::formatter<T, char, std::enable_if_t<std::is_enum_v<T>, void>>
2070020766
{

binaryninjacore.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ extern "C"
305305
typedef struct BNFirmwareNinjaRelationship BNFirmwareNinjaRelationship;
306306
typedef struct BNLineFormatter BNLineFormatter;
307307
typedef struct BNRenderLayer BNRenderLayer;
308+
typedef struct BNStringRef BNStringRef;
308309

309310
//! Console log levels
310311
typedef enum BNLogLevel
@@ -5654,8 +5655,11 @@ extern "C"
56545655
BINARYNINJACOREAPI BNSymbolBinding BNGetSymbolBinding(BNSymbol* sym);
56555656
BINARYNINJACOREAPI BNNameSpace BNGetSymbolNameSpace(BNSymbol* sym);
56565657
BINARYNINJACOREAPI char* BNGetSymbolShortName(BNSymbol* sym);
5658+
BINARYNINJACOREAPI BNStringRef* BNGetSymbolShortNameRef(BNSymbol* sym);
56575659
BINARYNINJACOREAPI char* BNGetSymbolFullName(BNSymbol* sym);
5660+
BINARYNINJACOREAPI BNStringRef* BNGetSymbolFullNameRef(BNSymbol* sym);
56585661
BINARYNINJACOREAPI char* BNGetSymbolRawName(BNSymbol* sym);
5662+
BINARYNINJACOREAPI BNStringRef* BNGetSymbolRawNameRef(BNSymbol* sym);
56595663
BINARYNINJACOREAPI void* BNGetSymbolRawBytes(BNSymbol* sym, size_t* count);
56605664
BINARYNINJACOREAPI void BNFreeSymbolRawBytes(void* bytes);
56615665

@@ -8216,6 +8220,11 @@ extern "C"
82168220
size_t* outLineCount
82178221
);
82188222

8223+
BINARYNINJACOREAPI void BNFreeStringRef(BNStringRef* ref);
8224+
BINARYNINJACOREAPI BNStringRef* BNDuplicateStringRef(BNStringRef* ref);
8225+
BINARYNINJACOREAPI const char* BNGetStringRefContents(BNStringRef* ref);
8226+
BINARYNINJACOREAPI size_t BNGetStringRefSize(BNStringRef* ref);
8227+
82198228
#ifdef __cplusplus
82208229
}
82218230
#endif

binaryview.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,83 @@ BinaryDataNotification::BinaryDataNotification(NotificationTypes notifications)
650650
}
651651

652652

653+
StringRef::StringRef()
654+
{
655+
m_ref = nullptr;
656+
}
657+
658+
659+
StringRef::StringRef(BNStringRef* ref)
660+
{
661+
m_ref = ref;
662+
}
663+
664+
665+
StringRef::StringRef(const StringRef& other)
666+
{
667+
m_ref = BNDuplicateStringRef(other.m_ref);
668+
}
669+
670+
671+
StringRef::StringRef(StringRef&& other)
672+
{
673+
m_ref = other.m_ref;
674+
other.m_ref = nullptr;
675+
}
676+
677+
678+
StringRef::~StringRef()
679+
{
680+
if (m_ref)
681+
{
682+
BNFreeStringRef(m_ref);
683+
}
684+
}
685+
686+
687+
StringRef& StringRef::operator=(const StringRef& other)
688+
{
689+
if (m_ref)
690+
{
691+
BNFreeStringRef(m_ref);
692+
m_ref = nullptr;
693+
}
694+
if (other.m_ref)
695+
{
696+
m_ref = BNDuplicateStringRef(other.m_ref);
697+
}
698+
return *this;
699+
}
700+
701+
702+
StringRef& StringRef::operator=(StringRef&& other)
703+
{
704+
if (m_ref)
705+
{
706+
BNFreeStringRef(m_ref);
707+
}
708+
m_ref = other.m_ref;
709+
other.m_ref = nullptr;
710+
return *this;
711+
}
712+
713+
714+
const char* StringRef::c_str() const
715+
{
716+
if (!m_ref)
717+
return ""; // todo: nullptr?
718+
return BNGetStringRefContents(m_ref);
719+
}
720+
721+
722+
size_t StringRef::size() const
723+
{
724+
if (!m_ref)
725+
return 0;
726+
return BNGetStringRefSize(m_ref);
727+
}
728+
729+
653730
Symbol::Symbol(BNSymbolType type, const string& shortName, const string& fullName, const string& rawName, uint64_t addr,
654731
BNSymbolBinding binding, const NameSpace& nameSpace, uint64_t ordinal)
655732
{
@@ -710,6 +787,13 @@ string Symbol::GetShortName() const
710787
}
711788

712789

790+
StringRef Symbol::GetShortNameRef() const
791+
{
792+
BNStringRef* name = BNGetSymbolShortNameRef(m_object);
793+
return StringRef(name);
794+
}
795+
796+
713797
string Symbol::GetFullName() const
714798
{
715799
char* name = BNGetSymbolFullName(m_object);
@@ -719,6 +803,13 @@ string Symbol::GetFullName() const
719803
}
720804

721805

806+
StringRef Symbol::GetFullNameRef() const
807+
{
808+
BNStringRef* name = BNGetSymbolFullNameRef(m_object);
809+
return StringRef(name);
810+
}
811+
812+
722813
string Symbol::GetRawName() const
723814
{
724815
char* name = BNGetSymbolRawName(m_object);
@@ -728,6 +819,13 @@ string Symbol::GetRawName() const
728819
}
729820

730821

822+
StringRef Symbol::GetRawNameRef() const
823+
{
824+
BNStringRef* name = BNGetSymbolRawNameRef(m_object);
825+
return StringRef(name);
826+
}
827+
828+
731829
uint64_t Symbol::GetAddress() const
732830
{
733831
return BNGetSymbolAddress(m_object);

0 commit comments

Comments
 (0)