From eb7a52bf4bd3b19c22f2eadda444ab045c674fe3 Mon Sep 17 00:00:00 2001 From: KyleMiles Date: Mon, 3 May 2021 18:45:19 -0400 Subject: Rust API : Better headless support; Introducing `binaryninja::open_view` and `binaryninja::open_view_with_options`, updated init/shutdown, script example, more setting support, and misc fixes --- rust/src/settings.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 4 deletions(-) (limited to 'rust/src/settings.rs') diff --git a/rust/src/settings.rs b/rust/src/settings.rs index 09fa14a6..a555cb4c 100644 --- a/rust/src/settings.rs +++ b/rust/src/settings.rs @@ -12,11 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -use binaryninjacore_sys::*; - pub use binaryninjacore_sys::BNSettingsScope as SettingsScope; +use binaryninjacore_sys::*; +use crate::binaryview::BinaryView; use crate::rc::*; +use crate::string::{BnStrCompatible, BnString}; + +use std::ptr; #[derive(PartialEq, Eq, Hash)] pub struct Settings { @@ -27,10 +30,79 @@ unsafe impl Send for Settings {} unsafe impl Sync for Settings {} impl Settings { - pub(crate) unsafe fn from_raw(handle: *mut BNSettings) -> Self { + pub(crate) unsafe fn from_raw(handle: *mut BNSettings) -> Ref { debug_assert!(!handle.is_null()); - Self { handle } + Ref::new(Self { handle }) + } + + pub fn new(instance_id: S) -> Ref { + let instance_id = instance_id.as_bytes_with_nul(); + unsafe { + let handle = BNCreateSettings(instance_id.as_ref().as_ptr() as *mut _); + + debug_assert!(!handle.is_null()); + + Ref::new(Self { handle }) + } + } + + pub fn set_resource_id(&mut self, resource_id: S) { + let resource_id = resource_id.as_bytes_with_nul(); + unsafe { BNSettingsSetResourceId(self.handle, resource_id.as_ref().as_ptr() as *mut _) }; + } + + pub fn serialize_schema(&self) -> BnString { + unsafe { BnString::from_raw(BNSettingsSerializeSchema(self.handle)) } + } + + pub fn deserialize_schema(&self, schema: S) -> bool { + let schema = schema.as_bytes_with_nul(); + unsafe { + BNSettingsDeserializeSchema( + self.handle, + schema.as_ref().as_ptr() as *mut _, + BNSettingsScope::SettingsAutoScope, + true, + ) + } + } + + pub fn contains(&self, key: S) -> bool { + let key = key.as_bytes_with_nul(); + + unsafe { BNSettingsContains(self.handle, key.as_ref().as_ptr() as *mut _) } + } + + pub fn set_json( + &mut self, + key: S1, + value: S2, + view: Option<&BinaryView>, + scope: Option, + ) -> bool { + let key = key.as_bytes_with_nul(); + let value = value.as_bytes_with_nul(); + + let view_handle = match view { + Some(view) => view.handle, + None => ptr::null_mut() as *mut _, + }; + + let scope = match scope { + Some(scope) => scope, + None => SettingsScope::SettingsAutoScope, + }; + + unsafe { + BNSettingsSetJson( + self.handle, + view_handle, + scope, + key.as_ref().as_ptr() as *mut _, + value.as_ref().as_ptr() as *mut _, + ) + } } } -- cgit v1.3.1