summaryrefslogtreecommitdiff
path: root/rust/src/settings.rs
diff options
context:
space:
mode:
authorKyleMiles <krm504@nyu.edu>2021-05-03 18:45:19 -0400
committerKyleMiles <krm504@nyu.edu>2021-05-11 16:49:24 -0400
commiteb7a52bf4bd3b19c22f2eadda444ab045c674fe3 (patch)
tree2395d577d410908b5d0291682660dac2017c7e97 /rust/src/settings.rs
parent5731e612900bed0d542421e00e64324dbac9367e (diff)
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
Diffstat (limited to 'rust/src/settings.rs')
-rw-r--r--rust/src/settings.rs80
1 files changed, 76 insertions, 4 deletions
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<Self> {
debug_assert!(!handle.is_null());
- Self { handle }
+ Ref::new(Self { handle })
+ }
+
+ pub fn new<S: BnStrCompatible>(instance_id: S) -> Ref<Self> {
+ 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<S: BnStrCompatible>(&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<S: BnStrCompatible>(&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<S: BnStrCompatible>(&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<S1: BnStrCompatible, S2: BnStrCompatible>(
+ &mut self,
+ key: S1,
+ value: S2,
+ view: Option<&BinaryView>,
+ scope: Option<SettingsScope>,
+ ) -> 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 _,
+ )
+ }
}
}