Skip to content

Commit 9ccdfae

Browse files
committed
[Rust] Add TypeLibrary::remove_named_object and TypeLibrary::remove_named_type
1 parent dc3ed31 commit 9ccdfae

1 file changed

Lines changed: 51 additions & 16 deletions

File tree

rust/src/types/library.rs

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
use binaryninjacore_sys::*;
2-
use std::fmt::{Debug, Formatter};
3-
41
use crate::rc::{Guard, RefCountable};
52
use crate::types::TypeContainer;
63
use crate::{
@@ -11,6 +8,9 @@ use crate::{
118
string::{BnString, IntoCStr},
129
types::{QualifiedName, QualifiedNameAndType, Type},
1310
};
11+
use binaryninjacore_sys::*;
12+
use std::fmt::{Debug, Formatter};
13+
use std::hash::{Hash, Hasher};
1414
use std::path::Path;
1515
use std::ptr::NonNull;
1616

@@ -264,34 +264,55 @@ impl TypeLibrary {
264264
QualifiedName::free_raw(raw_name);
265265
}
266266

267-
/// Directly inserts a named object into the type library's object store.
268-
/// This is not done recursively, so care should be taken that types referring to other types
269-
/// through NamedTypeReferences are already appropriately prepared.
267+
pub fn remove_named_object(&self, name: QualifiedName) {
268+
let mut raw_name = QualifiedName::into_raw(name);
269+
unsafe { BNRemoveTypeLibraryNamedObject(self.as_raw(), &mut raw_name) }
270+
QualifiedName::free_raw(raw_name);
271+
}
272+
273+
/// Directly inserts a named type into the type library's type store.
274+
///
275+
/// Referenced types will not automatically be added, so make sure to add referenced types to the
276+
/// library or use [`TypeLibrary::add_type_source`] to mark the references originating source.
270277
///
271-
/// To add types and objects from an existing BinaryView, it is recommended to use
272-
/// `export_type_to_library <binaryview.BinaryView.export_type_to_library>`, which will automatically pull in
273-
/// all referenced types and record additional dependencies as needed.
278+
/// To add types from a binary view, prefer using [`BinaryView::export_type_to_library`] which
279+
/// will automatically pull in all referenced types and record additional dependencies as needed.
274280
pub fn add_named_type(&self, name: QualifiedName, type_: &Type) {
275281
let mut raw_name = QualifiedName::into_raw(name);
276282
unsafe { BNAddTypeLibraryNamedType(self.as_raw(), &mut raw_name, type_.handle) }
277283
QualifiedName::free_raw(raw_name);
278284
}
279285

280-
/// Manually flag NamedTypeReferences to the given QualifiedName as originating from another source
281-
/// TypeLibrary with the given dependency name.
282-
///
283-
/// <div class="warning">
284-
///
285-
/// Use this api with extreme caution.
286+
pub fn remove_named_type(&self, name: QualifiedName) {
287+
let mut raw_name = QualifiedName::into_raw(name);
288+
unsafe { BNRemoveTypeLibraryNamedType(self.as_raw(), &mut raw_name) }
289+
QualifiedName::free_raw(raw_name);
290+
}
291+
292+
/// Flag any outgoing named type reference with the given `name` as belonging to the `source` type library.
286293
///
287-
/// </div>
294+
/// This allows type libraries to share types between them, automatically pulling in dependencies
295+
/// into the binary view as needed.
288296
pub fn add_type_source(&self, name: QualifiedName, source: &str) {
289297
let source = source.to_cstr();
290298
let mut raw_name = QualifiedName::into_raw(name);
291299
unsafe { BNAddTypeLibraryNamedTypeSource(self.as_raw(), &mut raw_name, source.as_ptr()) }
292300
QualifiedName::free_raw(raw_name);
293301
}
294302

303+
/// Retrieve the source type library associated with the given named type, if any.
304+
pub fn get_named_type_source(&self, name: QualifiedName) -> Option<String> {
305+
let mut raw_name = QualifiedName::into_raw(name);
306+
let result = unsafe { BNGetTypeLibraryNamedTypeSource(self.as_raw(), &mut raw_name) };
307+
QualifiedName::free_raw(raw_name);
308+
let str = unsafe { BnString::into_string(result) };
309+
if str.is_empty() {
310+
None
311+
} else {
312+
Some(str)
313+
}
314+
}
315+
295316
/// Get the object (function) associated with the given name, if any.
296317
///
297318
/// Prefer [`BinaryView::import_type_library_object`] as it will recursively import types required.
@@ -346,6 +367,20 @@ impl Debug for TypeLibrary {
346367
}
347368
}
348369

370+
impl PartialEq for TypeLibrary {
371+
fn eq(&self, other: &Self) -> bool {
372+
self.guid() == other.guid()
373+
}
374+
}
375+
376+
impl Eq for TypeLibrary {}
377+
378+
impl Hash for TypeLibrary {
379+
fn hash<H: Hasher>(&self, state: &mut H) {
380+
self.guid().hash(state);
381+
}
382+
}
383+
349384
unsafe impl RefCountable for TypeLibrary {
350385
unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
351386
Ref::new(Self {

0 commit comments

Comments
 (0)