diff options
| author | Glenn Smith <glenn@vector35.com> | 2022-06-29 17:28:31 -0400 |
|---|---|---|
| committer | Glenn Smith <glenn@vector35.com> | 2022-09-29 21:02:20 -0400 |
| commit | 642d32f688d735decccf2b844d113c436982f5e0 (patch) | |
| tree | 0148b8474bd154345c121c623b2fa23159e0c755 | |
| parent | 207925956bb8ca0174d40523fda70bfb5bc7bd38 (diff) | |
[Rust API] Make more types convertible to BN types
| -rw-r--r-- | rust/src/platform.rs | 2 | ||||
| -rw-r--r-- | rust/src/rc.rs | 13 | ||||
| -rw-r--r-- | rust/src/string.rs | 10 | ||||
| -rw-r--r-- | rust/src/types.rs | 42 |
4 files changed, 65 insertions, 2 deletions
diff --git a/rust/src/platform.rs b/rust/src/platform.rs index d0cc9282..6bdf8c9f 100644 --- a/rust/src/platform.rs +++ b/rust/src/platform.rs @@ -281,7 +281,7 @@ impl TypeParser for Platform { let mut include_dirs = vec![]; for dir in include_directories.iter() { - let d = dir.as_ref().to_string_lossy().as_bytes_with_nul(); + let d = dir.as_ref().to_string_lossy().to_string().as_bytes_with_nul(); include_dirs.push(d.as_ptr() as _); } diff --git a/rust/src/rc.rs b/rust/src/rc.rs index f8507d0c..b21b7b94 100644 --- a/rust/src/rc.rs +++ b/rust/src/rc.rs @@ -15,6 +15,7 @@ //! Reference counting for core Binary Ninja objects. use std::borrow::Borrow; +use std::fmt::{Debug, Display, Formatter}; use std::marker::PhantomData; use std::mem; use std::ops::{Deref, DerefMut}; @@ -99,6 +100,18 @@ impl<T: RefCountable> Clone for Ref<T> { } } +impl<T: RefCountable + Display> Display for Ref<T> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.contents.fmt(f) + } +} + +impl<T: RefCountable + Debug> Debug for Ref<T> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.contents.fmt(f) + } +} + // Guard provides access to a core-allocated resource whose // reference is held indirectly (e.g. a core-allocated array // of raw `*mut BNRawT`). diff --git a/rust/src/string.rs b/rust/src/string.rs index c1ca88d8..c4419b5e 100644 --- a/rust/src/string.rs +++ b/rust/src/string.rs @@ -14,7 +14,7 @@ //! String wrappers for core-owned strings and strings being passed to the core -use std::borrow::Borrow; +use std::borrow::{Borrow, Cow}; use std::ffi::{CStr, CString}; use std::fmt; use std::mem; @@ -231,3 +231,11 @@ unsafe impl BnStrCompatible for String { ret.into_bytes_with_nul() } } + +unsafe impl<'a> BnStrCompatible for &'a Cow<'a, str> { + type Result = &'a [u8]; + + fn as_bytes_with_nul(self) -> Self::Result { + self.as_ref().as_bytes() + } +} diff --git a/rust/src/types.rs b/rust/src/types.rs index 09b1b97d..c1f24c59 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -17,6 +17,7 @@ // TODO : Test the get_enumeration and get_structure methods use binaryninjacore_sys::*; +use std::iter::IntoIterator; use std::{fmt, mem, ptr, result, slice}; use crate::architecture::{Architecture, CoreArchitecture}; @@ -724,6 +725,15 @@ impl Type { Self::int(1, true) } + pub fn wide_char(width: usize) -> Ref<Self> { + unsafe { + Self::ref_from_raw(BNCreateWideCharType( + width, + BnString::new("").as_ptr() as *mut _, + )) + } + } + pub fn int(width: usize, is_signed: bool) -> Ref<Self> { let mut is_signed = Conf::new(is_signed, max_confidence()).into(); unsafe { @@ -1041,6 +1051,18 @@ impl fmt::Display for Type { } } +impl fmt::Debug for Type { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", unsafe { + BnString::from_raw(BNGetTypeString( + self.handle, + ptr::null_mut(), + BNTokenEscapingType::NoTokenEscapingType, + )) + }) + } +} + unsafe impl Send for Type {} unsafe impl Sync for Type {} @@ -1625,6 +1647,26 @@ impl<S: BnStrCompatible> From<S> for QualifiedName { } } +impl<S: BnStrCompatible> From<Vec<S>> for QualifiedName { + fn from(names: Vec<S>) -> Self { + let join = BnString::new("::"); + let names = names + .into_iter() + .map(|n| n.as_bytes_with_nul()) + .collect::<Vec<_>>(); + let mut list = names + .iter() + .map(|n| n.as_ref().as_ptr() as *const _) + .collect::<Vec<_>>(); + + QualifiedName(BNQualifiedName { + name: unsafe { BNAllocStringList(list.as_mut_ptr(), list.len()) }, + join: join.into_raw(), + nameCount: list.len(), + }) + } +} + impl Drop for QualifiedName { fn drop(&mut self) { unsafe { |
