From 5d4fc5f1fb7a0c368d2a048a5d750564c970c9c9 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Mon, 13 Oct 2025 19:35:01 -0400 Subject: Add derived strings and string recognizer API --- binaryview.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'binaryview.cpp') diff --git a/binaryview.cpp b/binaryview.cpp index 292b11ac..3499f2ac 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -222,6 +222,22 @@ void BinaryDataNotification::StringRemovedCallback( } +void BinaryDataNotification::DerivedStringFoundCallback(void* ctxt, BNBinaryView* data, BNDerivedString* str) +{ + BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; + Ref view = new BinaryView(BNNewViewReference(data)); + notify->OnDerivedStringFound(view, DerivedString::FromAPIObject(str, false)); +} + + +void BinaryDataNotification::DerivedStringRemovedCallback(void* ctxt, BNBinaryView* data, BNDerivedString* str) +{ + BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; + Ref view = new BinaryView(BNNewViewReference(data)); + notify->OnDerivedStringRemoved(view, DerivedString::FromAPIObject(str, false)); +} + + void BinaryDataNotification::TypeDefinedCallback(void* ctxt, BNBinaryView* data, BNQualifiedName* name, BNType* type) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; @@ -556,6 +572,8 @@ BinaryDataNotification::BinaryDataNotification() m_callbacks.symbolRemoved = SymbolRemovedCallback; m_callbacks.stringFound = StringFoundCallback; m_callbacks.stringRemoved = StringRemovedCallback; + m_callbacks.derivedStringFound = DerivedStringFoundCallback; + m_callbacks.derivedStringRemoved = DerivedStringRemovedCallback; m_callbacks.typeDefined = TypeDefinedCallback; m_callbacks.typeUndefined = TypeUndefinedCallback; m_callbacks.typeReferenceChanged = TypeReferenceChangedCallback; @@ -615,6 +633,8 @@ BinaryDataNotification::BinaryDataNotification(NotificationTypes notifications) m_callbacks.symbolRemoved = (notifications & NotificationType::SymbolRemoved) ? SymbolRemovedCallback : nullptr; m_callbacks.stringFound = (notifications & NotificationType::StringFound) ? StringFoundCallback : nullptr; m_callbacks.stringRemoved = (notifications & NotificationType::StringRemoved) ? StringRemovedCallback : nullptr; + m_callbacks.derivedStringFound = (notifications & NotificationType::DerivedStringFound) ? DerivedStringFoundCallback : nullptr; + m_callbacks.derivedStringRemoved = (notifications & NotificationType::DerivedStringRemoved) ? DerivedStringRemovedCallback : nullptr; m_callbacks.typeDefined = (notifications & NotificationType::TypeDefined) ? TypeDefinedCallback : nullptr; m_callbacks.typeUndefined = (notifications & NotificationType::TypeUndefined) ? TypeUndefinedCallback : nullptr; m_callbacks.typeReferenceChanged = (notifications & NotificationType::TypeReferenceChanged) ? TypeReferenceChangedCallback : nullptr; @@ -670,7 +690,7 @@ StringRef::StringRef(const std::string& str) StringRef::StringRef(const StringRef& other) { - m_ref = BNDuplicateStringRef(other.m_ref); + m_ref = other.m_ref ? BNDuplicateStringRef(other.m_ref) : nullptr; } @@ -4013,6 +4033,42 @@ vector BinaryView::GetStrings(uint64_t start, uint64_t len) } +vector BinaryView::GetDerivedStrings() +{ + size_t count; + BNDerivedString* strings = BNGetDerivedStrings(m_object, &count); + vector result; + for (size_t i = 0; i < count; i++) + result.push_back(DerivedString::FromAPIObject(&strings[i], false)); + BNFreeDerivedStringList(strings, count); + return result; +} + + +vector BinaryView::GetDerivedStringCodeReferences( + const DerivedString& str, std::optional maxItems) +{ + BNDerivedString derivedStr = str.ToAPIObject(false); + + size_t count; + BNReferenceSource* refs = BNGetDerivedStringCodeReferences( + m_object, &derivedStr, &count, maxItems.has_value(), maxItems.value_or(0)); + vector result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + { + ReferenceSource src; + src.func = new Function(BNNewFunctionReference(refs[i].func)); + src.arch = new CoreArchitecture(refs[i].arch); + src.addr = refs[i].addr; + result.push_back(src); + } + + BNFreeCodeReferences(refs, count); + return result; +} + + // The caller of this function must hold a reference to the returned Ref. // Otherwise, it can be freed before the callback is triggered, leading to a crash. Ref BinaryView::AddAnalysisCompletionEvent(const function& callback) -- cgit v1.3.1