diff options
| author | Mason Reed <mason@vector35.com> | 2025-05-04 19:10:56 -0400 |
|---|---|---|
| committer | Mason Reed <35282038+emesare@users.noreply.github.com> | 2025-05-12 17:45:24 -0400 |
| commit | a826c589dfc10c542deba7ca3343a462e02d6bde (patch) | |
| tree | f116254bef39f787268bbecc5eac19da310db9ce /rust/src/debuginfo.rs | |
| parent | 28b3c4044af06fdc32c9c85bf8381b5058306427 (diff) | |
[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 <michael.krasnitski@gmail.com>
Diffstat (limited to 'rust/src/debuginfo.rs')
| -rw-r--r-- | rust/src/debuginfo.rs | 119 |
1 files changed, 46 insertions, 73 deletions
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<S: BnStrCompatible>(name: S) -> Result<Ref<Self>, ()> { - let name = name.into_bytes_with_nul(); + pub fn from_name<S: AsCStr>(name: S) -> Result<Ref<Self>, ()> { + 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<S, C>(name: S, parser_callbacks: C) -> Ref<Self> where - S: BnStrCompatible, + S: AsCStr, C: CustomDebugInfoParser, { extern "C" fn cb_is_valid<C>(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<S: BnStrCompatible>(&self, parser_name: S) -> Vec<NameAndType> { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn types_by_name<S: AsCStr>(&self, parser_name: S) -> Vec<NameAndType> { + 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<S: BnStrCompatible>(&self, parser_name: S) -> Vec<DebugFunctionInfo> { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn functions_by_name<S: AsCStr>(&self, parser_name: S) -> Vec<DebugFunctionInfo> { + 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<S: BnStrCompatible>( + pub fn data_variables_by_name<S: AsCStr>( &self, parser_name: S, ) -> Vec<NamedDataVariableWithType> { - 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<S: BnStrCompatible>(&self, parser_name: S, name: S) -> Option<Ref<Type>> { - let parser_name = parser_name.into_bytes_with_nul(); - let name = name.into_bytes_with_nul(); + pub fn type_by_name<S: AsCStr>(&self, parser_name: S, name: S) -> Option<Ref<Type>> { + 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<S: BnStrCompatible>( + pub fn get_data_variable_by_name<S: AsCStr>( &self, parser_name: S, name: S, ) -> Option<NamedDataVariableWithType> { - 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<S: BnStrCompatible>( + pub fn get_data_variable_by_address<S: AsCStr>( &self, parser_name: S, address: u64, ) -> Option<NamedDataVariableWithType> { - 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<S: BnStrCompatible>(&self, name: S) -> Vec<NameAndType> { + pub fn get_types_by_name<S: AsCStr>(&self, name: S) -> Vec<NameAndType> { 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<S: BnStrCompatible>( - &self, - name: S, - ) -> Vec<(String, u64, Ref<Type>)> { - let name = name.into_bytes_with_nul(); + pub fn get_data_variables_by_name<S: AsCStr>(&self, name: S) -> Vec<(String, u64, Ref<Type>)> { + 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<S: BnStrCompatible>(&self, parser_name: S) -> bool { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn remove_parser_info<S: AsCStr>(&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<S: BnStrCompatible>(&self, parser_name: S) -> bool { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn remove_parser_types<S: AsCStr>(&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<S: BnStrCompatible>(&self, parser_name: S) -> bool { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn remove_parser_functions<S: AsCStr>(&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<S: BnStrCompatible>(&self, parser_name: S) -> bool { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn remove_parser_data_variables<S: AsCStr>(&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<S: BnStrCompatible>(&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<S: AsCStr>(&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<S: BnStrCompatible>( - &self, - parser_name: S, - index: usize, - ) -> bool { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn remove_function_by_index<S: AsCStr>(&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<S: BnStrCompatible>( - &self, - parser_name: S, - address: u64, - ) -> bool { - let parser_name = parser_name.into_bytes_with_nul(); + pub fn remove_data_variable_by_address<S: AsCStr>(&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<S: BnStrCompatible>( - &self, - name: S, - new_type: &Type, - components: &[&str], - ) -> bool { + pub fn add_type<S: AsCStr>(&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<S: BnStrCompatible>( + pub fn add_data_variable<S: AsCStr>( &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, |
