From a826c589dfc10c542deba7ca3343a462e02d6bde Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Sun, 4 May 2025 19:10:56 -0400 Subject: [Rust] Simplify `BnStrCompatible` trait Followup to https://github.com/Vector35/binaryninja-api/pull/5897/ This simplifies usage of the trait in user code, should just be able to `to_cstr` to get the cstr repr and then call `as_ptr`. Co-authored-by: Michael Krasnitski --- rust/src/lib.rs | 52 ++++++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 32 deletions(-) (limited to 'rust/src/lib.rs') diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 4d6b97b4..63872be3 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -102,7 +102,7 @@ use std::cmp; use std::collections::HashMap; use std::ffi::{c_char, c_void, CStr}; use std::path::{Path, PathBuf}; -use string::BnStrCompatible; +use string::AsCStr; use string::BnString; use string::IntoJson; @@ -128,7 +128,7 @@ pub fn load_with_progress( file_path: impl AsRef, mut progress: P, ) -> Option> { - let file_path = file_path.as_ref().into_bytes_with_nul(); + let file_path = file_path.as_ref().to_cstr(); let options = c""; let handle = unsafe { BNLoadFilename( @@ -193,13 +193,9 @@ where O: IntoJson, P: ProgressCallback, { - let file_path = file_path.as_ref().into_bytes_with_nul(); + let file_path = file_path.as_ref().to_cstr(); let options_or_default = if let Some(opt) = options { - opt.get_json_string() - .ok()? - .into_bytes_with_nul() - .as_ref() - .to_vec() + opt.get_json_string().ok()?.to_cstr().to_bytes().to_vec() } else { Metadata::new_of_type(MetadataType::KeyValueDataType) .get_json_string() @@ -247,11 +243,7 @@ where P: ProgressCallback, { let options_or_default = if let Some(opt) = options { - opt.get_json_string() - .ok()? - .into_bytes_with_nul() - .as_ref() - .to_vec() + opt.get_json_string().ok()?.to_cstr().to_bytes().to_vec() } else { Metadata::new_of_type(MetadataType::KeyValueDataType) .get_json_string() @@ -292,7 +284,7 @@ pub fn bundled_plugin_directory() -> Result { } pub fn set_bundled_plugin_directory(new_dir: impl AsRef) { - let new_dir = new_dir.as_ref().into_bytes_with_nul(); + let new_dir = new_dir.as_ref().to_cstr(); unsafe { BNSetBundledPluginDirectory(new_dir.as_ptr() as *const c_char) }; } @@ -336,7 +328,7 @@ pub fn save_last_run() { } pub fn path_relative_to_bundled_plugin_directory(path: impl AsRef) -> Result { - let path_raw = path.as_ref().into_bytes_with_nul(); + let path_raw = path.as_ref().to_cstr(); let s: *mut c_char = unsafe { BNGetPathRelativeToBundledPluginDirectory(path_raw.as_ptr() as *const c_char) }; if s.is_null() { @@ -346,7 +338,7 @@ pub fn path_relative_to_bundled_plugin_directory(path: impl AsRef) -> Resu } pub fn path_relative_to_user_plugin_directory(path: impl AsRef) -> Result { - let path_raw = path.as_ref().into_bytes_with_nul(); + let path_raw = path.as_ref().to_cstr(); let s: *mut c_char = unsafe { BNGetPathRelativeToUserPluginDirectory(path_raw.as_ptr() as *const c_char) }; if s.is_null() { @@ -356,7 +348,7 @@ pub fn path_relative_to_user_plugin_directory(path: impl AsRef) -> Result< } pub fn path_relative_to_user_directory(path: impl AsRef) -> Result { - let path_raw = path.as_ref().into_bytes_with_nul(); + let path_raw = path.as_ref().to_cstr(); let s: *mut c_char = unsafe { BNGetPathRelativeToUserDirectory(path_raw.as_ptr() as *const c_char) }; if s.is_null() { @@ -481,8 +473,8 @@ impl VersionInfo { unsafe { BnString::free_raw(value.channel) }; } - pub fn from_string(string: S) -> Self { - let string = string.into_bytes_with_nul(); + pub fn from_string(string: S) -> Self { + let string = string.to_cstr(); let result = unsafe { BNParseVersionString(string.as_ref().as_ptr() as *const c_char) }; Self::from_owned_raw(result) } @@ -540,14 +532,14 @@ pub fn license_count() -> i32 { /// 1. Check the BN_LICENSE environment variable /// 2. Check the Binary Ninja user directory for license.dat #[cfg(not(feature = "demo"))] -pub fn set_license(license: Option) { - let license = license.unwrap_or_default().into_bytes_with_nul(); +pub fn set_license(license: Option) { + let license = license.unwrap_or_default().to_cstr(); let license_slice = license.as_ref(); unsafe { BNSetLicense(license_slice.as_ptr() as *const c_char) } } #[cfg(feature = "demo")] -pub fn set_license(_license: Option) {} +pub fn set_license(_license: Option) {} pub fn product() -> String { unsafe { BnString::into_string(BNGetProduct()) } @@ -566,8 +558,8 @@ pub fn is_ui_enabled() -> bool { unsafe { BNIsUIEnabled() } } -pub fn is_database(filename: S) -> bool { - let filename = filename.into_bytes_with_nul(); +pub fn is_database(filename: S) -> bool { + let filename = filename.to_cstr(); let filename_slice = filename.as_ref(); unsafe { BNIsDatabase(filename_slice.as_ptr() as *const c_char) } } @@ -596,16 +588,12 @@ pub fn plugin_ui_abi_minimum_version() -> u32 { BN_MINIMUM_UI_ABI_VERSION } -pub fn add_required_plugin_dependency(name: S) { - unsafe { - BNAddRequiredPluginDependency(name.into_bytes_with_nul().as_ref().as_ptr() as *const c_char) - }; +pub fn add_required_plugin_dependency(name: S) { + unsafe { BNAddRequiredPluginDependency(name.to_cstr().as_ref().as_ptr() as *const c_char) }; } -pub fn add_optional_plugin_dependency(name: S) { - unsafe { - BNAddOptionalPluginDependency(name.into_bytes_with_nul().as_ref().as_ptr() as *const c_char) - }; +pub fn add_optional_plugin_dependency(name: S) { + unsafe { BNAddOptionalPluginDependency(name.to_cstr().as_ref().as_ptr() as *const c_char) }; } // Provide ABI version automatically so that the core can verify binary compatibility -- cgit v1.3.1