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/debuginfo.rs | 119 +++++++++++++++++++------------------------------- 1 file changed, 46 insertions(+), 73 deletions(-) (limited to 'rust/src/debuginfo.rs') diff --git a/rust/src/debuginfo.rs b/rust/src/debuginfo.rs index c4bc7add..0afe4c77 100644 --- a/rust/src/debuginfo.rs +++ b/rust/src/debuginfo.rs @@ -83,7 +83,7 @@ use crate::{ binary_view::BinaryView, platform::Platform, rc::*, - string::{raw_to_string, BnStrCompatible, BnString}, + string::{raw_to_string, AsCStr, BnString}, types::{NameAndType, Type}, }; @@ -115,8 +115,8 @@ impl DebugInfoParser { } /// Returns debug info parser of the given name, if it exists - pub fn from_name(name: S) -> Result, ()> { - let name = name.into_bytes_with_nul(); + pub fn from_name(name: S) -> Result, ()> { + let name = name.to_cstr(); let parser = unsafe { BNGetDebugInfoParserByName(name.as_ref().as_ptr() as *mut _) }; if parser.is_null() { @@ -209,7 +209,7 @@ impl DebugInfoParser { // Registers a DebugInfoParser. See `binaryninja::debuginfo::DebugInfoParser` for more details. pub fn register(name: S, parser_callbacks: C) -> Ref where - S: BnStrCompatible, + S: AsCStr, C: CustomDebugInfoParser, { extern "C" fn cb_is_valid(ctxt: *mut c_void, view: *mut BNBinaryView) -> bool @@ -259,7 +259,7 @@ impl DebugInfoParser { }) } - let name = name.into_bytes_with_nul(); + let name = name.to_cstr(); let name_ptr = name.as_ref().as_ptr() as *mut _; let ctxt = Box::into_raw(Box::new(parser_callbacks)); @@ -417,8 +417,8 @@ impl DebugInfo { } /// Returns all types within the parser - pub fn types_by_name(&self, parser_name: S) -> Vec { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn types_by_name(&self, parser_name: S) -> Vec { + let parser_name = parser_name.to_cstr(); let mut count: usize = 0; let debug_types_ptr = unsafe { @@ -455,8 +455,8 @@ impl DebugInfo { } /// Returns all functions within the parser - pub fn functions_by_name(&self, parser_name: S) -> Vec { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn functions_by_name(&self, parser_name: S) -> Vec { + let parser_name = parser_name.to_cstr(); let mut count: usize = 0; let functions_ptr = unsafe { @@ -495,11 +495,11 @@ impl DebugInfo { } /// Returns all data variables within the parser - pub fn data_variables_by_name( + pub fn data_variables_by_name( &self, parser_name: S, ) -> Vec { - let parser_name = parser_name.into_bytes_with_nul(); + let parser_name = parser_name.to_cstr(); let mut count: usize = 0; let data_variables_ptr = unsafe { @@ -537,9 +537,9 @@ impl DebugInfo { result } - pub fn type_by_name(&self, parser_name: S, name: S) -> Option> { - let parser_name = parser_name.into_bytes_with_nul(); - let name = name.into_bytes_with_nul(); + pub fn type_by_name(&self, parser_name: S, name: S) -> Option> { + let parser_name = parser_name.to_cstr(); + let name = name.to_cstr(); let result = unsafe { BNGetDebugTypeByName( @@ -555,13 +555,13 @@ impl DebugInfo { } } - pub fn get_data_variable_by_name( + pub fn get_data_variable_by_name( &self, parser_name: S, name: S, ) -> Option { - let parser_name = parser_name.into_bytes_with_nul(); - let name = name.into_bytes_with_nul(); + let parser_name = parser_name.to_cstr(); + let name = name.to_cstr(); let mut dv = BNDataVariableAndName::default(); unsafe { if BNGetDebugDataVariableByName( @@ -577,12 +577,12 @@ impl DebugInfo { } } - pub fn get_data_variable_by_address( + pub fn get_data_variable_by_address( &self, parser_name: S, address: u64, ) -> Option { - let parser_name = parser_name.into_bytes_with_nul(); + let parser_name = parser_name.to_cstr(); let mut dv = BNDataVariableAndName::default(); unsafe { if BNGetDebugDataVariableByAddress( @@ -599,9 +599,9 @@ impl DebugInfo { } /// Returns a list of [`NameAndType`] where the `name` is the parser the type originates from. - pub fn get_types_by_name(&self, name: S) -> Vec { + pub fn get_types_by_name(&self, name: S) -> Vec { let mut count: usize = 0; - let name = name.into_bytes_with_nul(); + let name = name.to_cstr(); let raw_names_and_types_ptr = unsafe { BNGetDebugTypesByName(self.handle, name.as_ref().as_ptr() as *mut _, &mut count) }; @@ -619,11 +619,8 @@ impl DebugInfo { } // The tuple is (DebugInfoParserName, address, type) - pub fn get_data_variables_by_name( - &self, - name: S, - ) -> Vec<(String, u64, Ref)> { - let name = name.into_bytes_with_nul(); + pub fn get_data_variables_by_name(&self, name: S) -> Vec<(String, u64, Ref)> { + let name = name.to_cstr(); let mut count: usize = 0; let raw_variables_and_names = unsafe { @@ -674,37 +671,37 @@ impl DebugInfo { result } - pub fn remove_parser_info(&self, parser_name: S) -> bool { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn remove_parser_info(&self, parser_name: S) -> bool { + let parser_name = parser_name.to_cstr(); unsafe { BNRemoveDebugParserInfo(self.handle, parser_name.as_ref().as_ptr() as *mut _) } } - pub fn remove_parser_types(&self, parser_name: S) -> bool { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn remove_parser_types(&self, parser_name: S) -> bool { + let parser_name = parser_name.to_cstr(); unsafe { BNRemoveDebugParserTypes(self.handle, parser_name.as_ref().as_ptr() as *mut _) } } - pub fn remove_parser_functions(&self, parser_name: S) -> bool { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn remove_parser_functions(&self, parser_name: S) -> bool { + let parser_name = parser_name.to_cstr(); unsafe { BNRemoveDebugParserFunctions(self.handle, parser_name.as_ref().as_ptr() as *mut _) } } - pub fn remove_parser_data_variables(&self, parser_name: S) -> bool { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn remove_parser_data_variables(&self, parser_name: S) -> bool { + let parser_name = parser_name.to_cstr(); unsafe { BNRemoveDebugParserDataVariables(self.handle, parser_name.as_ref().as_ptr() as *mut _) } } - pub fn remove_type_by_name(&self, parser_name: S, name: S) -> bool { - let parser_name = parser_name.into_bytes_with_nul(); - let name = name.into_bytes_with_nul(); + pub fn remove_type_by_name(&self, parser_name: S, name: S) -> bool { + let parser_name = parser_name.to_cstr(); + let name = name.to_cstr(); unsafe { BNRemoveDebugTypeByName( @@ -715,12 +712,8 @@ impl DebugInfo { } } - pub fn remove_function_by_index( - &self, - parser_name: S, - index: usize, - ) -> bool { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn remove_function_by_index(&self, parser_name: S, index: usize) -> bool { + let parser_name = parser_name.to_cstr(); unsafe { BNRemoveDebugFunctionByIndex( @@ -731,12 +724,8 @@ impl DebugInfo { } } - pub fn remove_data_variable_by_address( - &self, - parser_name: S, - address: u64, - ) -> bool { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn remove_data_variable_by_address(&self, parser_name: S, address: u64) -> bool { + let parser_name = parser_name.to_cstr(); unsafe { BNRemoveDebugDataVariableByAddress( @@ -748,16 +737,11 @@ impl DebugInfo { } /// Adds a type scoped under the current parser's name to the debug info - pub fn add_type( - &self, - name: S, - new_type: &Type, - components: &[&str], - ) -> bool { + pub fn add_type(&self, name: S, new_type: &Type, components: &[&str]) -> bool { // SAFETY: Lifetime of `components` will live long enough, so passing as_ptr is safe. let raw_components: Vec<_> = components.iter().map(|&c| c.as_ptr()).collect(); - let name = name.into_bytes_with_nul(); + let name = name.to_cstr(); unsafe { BNAddDebugType( self.handle, @@ -771,24 +755,15 @@ impl DebugInfo { /// Adds a function scoped under the current parser's name to the debug info pub fn add_function(&self, new_func: &DebugFunctionInfo) -> bool { - let short_name_bytes = new_func - .short_name - .as_ref() - .map(|name| name.into_bytes_with_nul()); + let short_name_bytes = new_func.short_name.as_ref().map(|name| name.to_cstr()); let short_name = short_name_bytes .as_ref() .map_or(std::ptr::null_mut() as *mut _, |name| name.as_ptr() as _); - let full_name_bytes = new_func - .full_name - .as_ref() - .map(|name| name.into_bytes_with_nul()); + let full_name_bytes = new_func.full_name.as_ref().map(|name| name.to_cstr()); let full_name = full_name_bytes .as_ref() .map_or(std::ptr::null_mut() as *mut _, |name| name.as_ptr() as _); - let raw_name_bytes = new_func - .raw_name - .as_ref() - .map(|name| name.into_bytes_with_nul()); + let raw_name_bytes = new_func.raw_name.as_ref().map(|name| name.to_cstr()); let raw_name = raw_name_bytes .as_ref() .map_or(std::ptr::null_mut() as *mut _, |name| name.as_ptr() as _); @@ -801,9 +776,7 @@ impl DebugInfo { unsafe { for component in &new_func.components { - components_array.push(BNAllocString( - component.clone().into_bytes_with_nul().as_ptr() as _, - )); + components_array.push(BNAllocString(component.clone().to_cstr().as_ptr() as _)); } for local_variable in &new_func.local_variables { @@ -845,7 +818,7 @@ impl DebugInfo { } /// Adds a data variable scoped under the current parser's name to the debug info - pub fn add_data_variable( + pub fn add_data_variable( &self, address: u64, t: &Type, @@ -860,7 +833,7 @@ impl DebugInfo { match name { Some(name) => { - let name = name.into_bytes_with_nul(); + let name = name.to_cstr(); unsafe { BNAddDebugDataVariable( self.handle, -- cgit v1.3.1