summaryrefslogtreecommitdiff
path: root/rust/src
diff options
context:
space:
mode:
authorRubens Brandao <git@rubens.io>2024-05-27 17:45:25 -0300
committerKyleMiles <krm504@nyu.edu>2024-05-28 17:11:23 -0400
commit53eed1fedbef8c778ba1ea03d6660a7f097886ca (patch)
treeaf3bedbd282f57a54a846afc847eea6cf1eb0064 /rust/src
parentfbf782fcd84aa77f8765589bdda491646a957044 (diff)
allow `load_with_option` to accept json options
Diffstat (limited to 'rust/src')
-rw-r--r--rust/src/lib.rs18
-rw-r--r--rust/src/metadata.rs25
-rw-r--r--rust/src/string.rs12
3 files changed, 48 insertions, 7 deletions
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index de38be4d..f20a33a9 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -171,8 +171,8 @@ pub use binaryninjacore_sys::BNEndianness as Endianness;
use binaryview::BinaryView;
use metadata::Metadata;
use metadata::MetadataType;
-use rc::Ref;
use string::BnStrCompatible;
+use string::IntoJson;
// Commented out to suppress unused warnings
// const BN_MAX_INSTRUCTION_LENGTH: u64 = 256;
@@ -227,24 +227,32 @@ pub fn load<S: BnStrCompatible>(filename: S) -> Option<rc::Ref<binaryview::Binar
/// let bv = binaryninja::load_with_options("/bin/cat", true, Some(settings))
/// .expect("Couldn't open `/bin/cat`");
/// ```
-pub fn load_with_options<S: BnStrCompatible>(
+pub fn load_with_options<S: BnStrCompatible, O: IntoJson>(
filename: S,
update_analysis_and_wait: bool,
- options: Option<Ref<Metadata>>,
+ options: Option<O>,
) -> Option<rc::Ref<binaryview::BinaryView>> {
let filename = filename.into_bytes_with_nul();
let options_or_default = if let Some(opt) = options {
- opt
+ opt.get_json_string()
+ .ok()?
+ .into_bytes_with_nul()
+ .as_ref()
+ .to_vec()
} else {
Metadata::new_of_type(MetadataType::KeyValueDataType)
+ .get_json_string()
+ .ok()?
+ .as_ref()
+ .to_vec()
};
let handle = unsafe {
binaryninjacore_sys::BNLoadFilename(
filename.as_ref().as_ptr() as *mut _,
update_analysis_and_wait,
- options_or_default.get_json_string().unwrap().as_ref().as_ptr() as *mut _,
+ options_or_default.as_ptr() as *mut core::ffi::c_char,
None,
)
};
diff --git a/rust/src/metadata.rs b/rust/src/metadata.rs
index f635700f..96571207 100644
--- a/rust/src/metadata.rs
+++ b/rust/src/metadata.rs
@@ -1,5 +1,5 @@
-use crate::rc::{Array, CoreArrayProvider, Guard, CoreArrayProviderInner, Ref, RefCountable};
-use crate::string::{BnStrCompatible, BnString};
+use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable};
+use crate::string::{BnStrCompatible, BnString, IntoJson};
use binaryninjacore_sys::*;
use std::collections::HashMap;
use std::os::raw::c_char;
@@ -702,3 +702,24 @@ impl TryFrom<&Metadata> for HashMap<String, Ref<Metadata>> {
.map(|m| m.into_iter().map(|(k, v)| (k.to_string(), v)).collect())
}
}
+
+impl IntoJson for &Metadata {
+ type Output = BnString;
+ fn get_json_string(self) -> Result<BnString, ()> {
+ Metadata::get_json_string(self)
+ }
+}
+
+impl IntoJson for Metadata {
+ type Output = BnString;
+ fn get_json_string(self) -> Result<BnString, ()> {
+ Metadata::get_json_string(&self)
+ }
+}
+
+impl IntoJson for Ref<Metadata> {
+ type Output = BnString;
+ fn get_json_string(self) -> Result<BnString, ()> {
+ Metadata::get_json_string(&self)
+ }
+}
diff --git a/rust/src/string.rs b/rust/src/string.rs
index f4098d66..936e3033 100644
--- a/rust/src/string.rs
+++ b/rust/src/string.rs
@@ -246,3 +246,15 @@ unsafe impl BnStrCompatible for &QualifiedName {
self.string().into_bytes_with_nul()
}
}
+
+pub trait IntoJson {
+ type Output: BnStrCompatible;
+ fn get_json_string(self) -> Result<Self::Output, ()>;
+}
+
+impl<S: BnStrCompatible> IntoJson for S {
+ type Output = S;
+ fn get_json_string(self) -> Result<Self::Output, ()> {
+ Ok(self)
+ }
+}