summaryrefslogtreecommitdiff
path: root/rust/src/binary_view.rs
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2025-06-13 16:42:24 -0400
committerMason Reed <mason@vector35.com>2025-06-13 16:43:01 -0400
commit6a65bc11674312beb455426502cb791d0db40106 (patch)
treeb87298569f82ec6bd65e436ca2fe41f00d31c882 /rust/src/binary_view.rs
parent607ca37184716da4641aa178cf0cf2dc3c95f116 (diff)
[Rust] Add `BinaryViewExt::strings` and `BinaryViewExt::strings_in_range`
Diffstat (limited to 'rust/src/binary_view.rs')
-rw-r--r--rust/src/binary_view.rs76
1 files changed, 76 insertions, 0 deletions
diff --git a/rust/src/binary_view.rs b/rust/src/binary_view.rs
index 822c421a..0a1a380b 100644
--- a/rust/src/binary_view.rs
+++ b/rust/src/binary_view.rs
@@ -76,6 +76,7 @@ pub type Result<R> = result::Result<R, ()>;
pub type BinaryViewEventType = BNBinaryViewEventType;
pub type AnalysisState = BNAnalysisState;
pub type ModificationStatus = BNModificationStatus;
+pub type StringType = BNStringType;
#[allow(clippy::len_without_is_empty)]
pub trait BinaryViewBase: AsRef<BinaryView> {
@@ -1905,6 +1906,38 @@ pub trait BinaryViewExt: BinaryViewBase {
let name = QualifiedName::from_owned_raw(result_name);
Some((lib, name))
}
+
+ /// Retrieve all known strings in the binary.
+ ///
+ /// NOTE: This returns a list of [`StringReference`] as strings may not be representable
+ /// as a [`String`] or even a [`BnString`]. It is the caller's responsibility to read the underlying
+ /// data and convert it to a representable form.
+ fn strings(&self) -> Array<StringReference> {
+ unsafe {
+ let mut count = 0;
+ let strings = BNGetStrings(self.as_ref().handle, &mut count);
+ Array::new(strings, count, ())
+ }
+ }
+
+ /// Retrieve all known strings within the provided `range`.
+ ///
+ /// NOTE: This returns a list of [`StringReference`] as strings may not be representable
+ /// as a [`String`] or even a [`BnString`]. It is the caller's responsibility to read the underlying
+ /// data and convert it to a representable form.
+ fn strings_in_range(&self, range: Range<u64>) -> Array<StringReference> {
+ unsafe {
+ let mut count = 0;
+ let strings = BNGetStringsInRange(
+ self.as_ref().handle,
+ range.start,
+ range.end - range.start,
+ &mut count,
+ );
+ Array::new(strings, count, ())
+ }
+ }
+
//
// fn type_archives(&self) -> Array<TypeArchive> {
// let mut ids: *mut *mut c_char = std::ptr::null_mut();
@@ -2190,3 +2223,46 @@ where
);
}
}
+
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
+pub struct StringReference {
+ pub ty: StringType,
+ pub start: u64,
+ pub length: usize,
+}
+
+impl From<BNStringReference> for StringReference {
+ fn from(raw: BNStringReference) -> Self {
+ Self {
+ ty: raw.type_,
+ start: raw.start,
+ length: raw.length,
+ }
+ }
+}
+
+impl From<StringReference> for BNStringReference {
+ fn from(raw: StringReference) -> Self {
+ Self {
+ type_: raw.ty,
+ start: raw.start,
+ length: raw.length,
+ }
+ }
+}
+
+impl CoreArrayProvider for StringReference {
+ type Raw = BNStringReference;
+ type Context = ();
+ type Wrapped<'a> = Self;
+}
+
+unsafe impl CoreArrayProviderInner for StringReference {
+ unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
+ BNFreeStringReferenceList(raw)
+ }
+
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
+ Self::from(*raw)
+ }
+}