From ad27532d344d3bf2995f623002753d2845c6ce20 Mon Sep 17 00:00:00 2001 From: Glenn Smith Date: Mon, 18 Jul 2022 19:53:47 -0400 Subject: [Rust API] Add support for Metadata --- rust/src/binaryview.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'rust/src/binaryview.rs') diff --git a/rust/src/binaryview.rs b/rust/src/binaryview.rs index a372b778..e64dbb61 100644 --- a/rust/src/binaryview.rs +++ b/rust/src/binaryview.rs @@ -18,10 +18,12 @@ //! TODO : Mirror the Python docs for this use binaryninjacore_sys::*; +use std::convert::TryFrom; pub use binaryninjacore_sys::BNModificationStatus as ModificationStatus; use std::ops; +use std::os::raw::c_char; use std::ptr; use std::result; @@ -36,6 +38,7 @@ use crate::flowgraph::FlowGraph; use crate::function::{Function, NativeBlock}; use crate::linearview::LinearDisassemblyLine; use crate::linearview::LinearViewCursor; +use crate::metadata::Metadata; use crate::platform::Platform; use crate::section::{Section, SectionBuilder}; use crate::segment::{Segment, SegmentBuilder}; @@ -133,7 +136,7 @@ pub trait BinaryViewBase: AsRef { } pub trait BinaryViewExt: BinaryViewBase { - fn metadata(&self) -> Ref { + fn file(&self) -> Ref { unsafe { let raw = BNGetFileForView(self.as_ref().handle); @@ -829,6 +832,36 @@ pub trait BinaryViewExt: BinaryViewBase { result } + + fn query_metadata(&self, key: S) -> Option> { + let value: *mut BNMetadata = unsafe { + BNBinaryViewQueryMetadata( + self.as_ref().handle, + key.as_bytes_with_nul().as_ref().as_ptr() as *const c_char, + ) + }; + if value.is_null() { + None + } else { + Some(unsafe { Metadata::ref_from_raw(value) }) + } + } + + fn get_metadata, S: BnStrCompatible>( + &self, + key: S, + ) -> Option> { + self.query_metadata(key).map(|md| T::try_from(md)) + } + + fn remove_metadata(&self, key: S) { + unsafe { + BNBinaryViewRemoveMetadata( + self.as_ref().handle, + key.as_bytes_with_nul().as_ref().as_ptr() as *const c_char, + ) + }; + } } impl BinaryViewExt for T {} -- cgit v1.3.1