From c980b77666bba9e4480c44de72cc8de0ca10c8e4 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Fri, 13 Feb 2026 19:21:22 -0800 Subject: [Rust] Add `TypeLibrary::remove_named_object` and `TypeLibrary::remove_named_type` --- rust/src/types/library.rs | 67 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 16 deletions(-) (limited to 'rust/src/types') diff --git a/rust/src/types/library.rs b/rust/src/types/library.rs index 0570d289..d75ee612 100644 --- a/rust/src/types/library.rs +++ b/rust/src/types/library.rs @@ -1,6 +1,3 @@ -use binaryninjacore_sys::*; -use std::fmt::{Debug, Formatter}; - use crate::rc::{Guard, RefCountable}; use crate::types::TypeContainer; use crate::{ @@ -11,6 +8,9 @@ use crate::{ string::{BnString, IntoCStr}, types::{QualifiedName, QualifiedNameAndType, Type}, }; +use binaryninjacore_sys::*; +use std::fmt::{Debug, Formatter}; +use std::hash::{Hash, Hasher}; use std::path::Path; use std::ptr::NonNull; @@ -264,27 +264,35 @@ impl TypeLibrary { QualifiedName::free_raw(raw_name); } - /// Directly inserts a named object into the type library's object store. - /// This is not done recursively, so care should be taken that types referring to other types - /// through NamedTypeReferences are already appropriately prepared. + pub fn remove_named_object(&self, name: QualifiedName) { + let mut raw_name = QualifiedName::into_raw(name); + unsafe { BNRemoveTypeLibraryNamedObject(self.as_raw(), &mut raw_name) } + QualifiedName::free_raw(raw_name); + } + + /// Directly inserts a named type into the type library's type store. + /// + /// Referenced types will not automatically be added, so make sure to add referenced types to the + /// library or use [`TypeLibrary::add_type_source`] to mark the references originating source. /// - /// To add types and objects from an existing BinaryView, it is recommended to use - /// `export_type_to_library `, which will automatically pull in - /// all referenced types and record additional dependencies as needed. + /// To add types from a binary view, prefer using [`BinaryView::export_type_to_library`] which + /// will automatically pull in all referenced types and record additional dependencies as needed. pub fn add_named_type(&self, name: QualifiedName, type_: &Type) { let mut raw_name = QualifiedName::into_raw(name); unsafe { BNAddTypeLibraryNamedType(self.as_raw(), &mut raw_name, type_.handle) } QualifiedName::free_raw(raw_name); } - /// Manually flag NamedTypeReferences to the given QualifiedName as originating from another source - /// TypeLibrary with the given dependency name. - /// - ///
- /// - /// Use this api with extreme caution. + pub fn remove_named_type(&self, name: QualifiedName) { + let mut raw_name = QualifiedName::into_raw(name); + unsafe { BNRemoveTypeLibraryNamedType(self.as_raw(), &mut raw_name) } + QualifiedName::free_raw(raw_name); + } + + /// Flag any outgoing named type reference with the given `name` as belonging to the `source` type library. /// - ///
+ /// This allows type libraries to share types between them, automatically pulling in dependencies + /// into the binary view as needed. pub fn add_type_source(&self, name: QualifiedName, source: &str) { let source = source.to_cstr(); let mut raw_name = QualifiedName::into_raw(name); @@ -292,6 +300,19 @@ impl TypeLibrary { QualifiedName::free_raw(raw_name); } + /// Retrieve the source type library associated with the given named type, if any. + pub fn get_named_type_source(&self, name: QualifiedName) -> Option { + let mut raw_name = QualifiedName::into_raw(name); + let result = unsafe { BNGetTypeLibraryNamedTypeSource(self.as_raw(), &mut raw_name) }; + QualifiedName::free_raw(raw_name); + let str = unsafe { BnString::into_string(result) }; + if str.is_empty() { + None + } else { + Some(str) + } + } + /// Get the object (function) associated with the given name, if any. /// /// Prefer [`BinaryView::import_type_library_object`] as it will recursively import types required. @@ -346,6 +367,20 @@ impl Debug for TypeLibrary { } } +impl PartialEq for TypeLibrary { + fn eq(&self, other: &Self) -> bool { + self.guid() == other.guid() + } +} + +impl Eq for TypeLibrary {} + +impl Hash for TypeLibrary { + fn hash(&self, state: &mut H) { + self.guid().hash(state); + } +} + unsafe impl RefCountable for TypeLibrary { unsafe fn inc_ref(handle: &Self) -> Ref { Ref::new(Self { -- cgit v1.3.1