Skip to content

Commit

Permalink
Replace BnStrCompatible with AsCStr trait
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski committed Sep 1, 2024
1 parent d36b962 commit 7aa2bb1
Show file tree
Hide file tree
Showing 37 changed files with 1,243 additions and 2,309 deletions.
8 changes: 4 additions & 4 deletions rust/examples/pdb-ng/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,21 @@ fn search_sym_store(store_path: &String, pdb_info: &PDBInfo) -> Result<Option<Ve
}

fn parse_pdb_info(view: &BinaryView) -> Option<PDBInfo> {
match view.get_metadata::<u64, _>("DEBUG_INFO_TYPE") {
match view.get_metadata::<u64>("DEBUG_INFO_TYPE") {
Some(Ok(0x53445352 /* 'SDSR' */)) => {}
_ => return None,
}

// This is stored in the BV by the PE loader
let file_path = match view.get_metadata::<String, _>("PDB_FILENAME") {
let file_path = match view.get_metadata::<String>("PDB_FILENAME") {
Some(Ok(md)) => md,
_ => return None,
};
let mut guid = match view.get_metadata::<Vec<u8>, _>("PDB_GUID") {
let mut guid = match view.get_metadata::<Vec<u8>>("PDB_GUID") {
Some(Ok(md)) => md,
_ => return None,
};
let age = match view.get_metadata::<u64, _>("PDB_AGE") {
let age = match view.get_metadata::<u64>("PDB_AGE") {
Some(Ok(md)) => md as u32,
_ => return None,
};
Expand Down
5 changes: 4 additions & 1 deletion rust/examples/pdb-ng/src/type_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,10 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
let vt_pointer = Type::pointer(
&self.arch,
&Conf::new(
Type::named_type_from_type(&QualifiedName::from(vt_name), vt_type.as_ref()),
Type::named_type_from_type(
QualifiedName::from(vt_name).string(),
vt_type.as_ref(),
),
max_confidence(),
),
);
Expand Down
36 changes: 14 additions & 22 deletions rust/src/architecture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use crate::{
platform::Platform,
rc::*,
relocation::CoreRelocationHandler,
string::BnStrCompatible,
string::*,
types::{Conf, NameAndType, Type},
{BranchType, Endianness},
Expand Down Expand Up @@ -1085,8 +1084,8 @@ impl CoreArchitecture {
CoreArchitectureList(archs, count)
}

pub fn by_name(name: &str) -> Option<Self> {
let res = unsafe { BNGetArchitectureByName(name.into_bytes_with_nul().as_ptr() as *mut _) };
pub fn by_name(name: impl AsCStr) -> Option<Self> {
let res = unsafe { BNGetArchitectureByName(name.as_cstr().as_ptr()) };

match res.is_null() {
false => Some(CoreArchitecture(res)),
Expand Down Expand Up @@ -1607,12 +1606,8 @@ macro_rules! cc_func {

/// Contains helper methods for all types implementing 'Architecture'
pub trait ArchitectureExt: Architecture {
fn register_by_name<S: BnStrCompatible>(&self, name: S) -> Option<Self::Register> {
let name = name.into_bytes_with_nul();

match unsafe {
BNGetArchitectureRegisterByName(self.as_ref().0, name.as_ref().as_ptr() as *mut _)
} {
fn register_by_name(&self, name: impl AsCStr) -> Option<Self::Register> {
match unsafe { BNGetArchitectureRegisterByName(self.as_ref().0, name.as_cstr().as_ptr()) } {
0xffff_ffff => None,
reg => self.register_from_id(reg),
}
Expand Down Expand Up @@ -1677,7 +1672,7 @@ pub trait ArchitectureExt: Architecture {

fn register_relocation_handler<S, R, F>(&self, name: S, func: F)
where
S: BnStrCompatible,
S: AsCStr,
R: 'static
+ RelocationHandler<Handle = CustomRelocationHandlerHandle<R>>
+ Send
Expand All @@ -1700,7 +1695,7 @@ impl<T: Architecture> ArchitectureExt for T {}

pub fn register_architecture<S, A, F>(name: S, func: F) -> &'static A
where
S: BnStrCompatible,
S: AsCStr,
A: 'static + Architecture<Handle = CustomArchitectureHandle<A>> + Send + Sync + Sized,
F: FnOnce(CustomArchitectureHandle<A>, CoreArchitecture) -> A,
{
Expand Down Expand Up @@ -1889,7 +1884,7 @@ where
let custom_arch = unsafe { &*(ctxt as *mut A) };

match custom_arch.register_from_id(reg) {
Some(reg) => BnString::new(reg.name().as_ref()).into_raw(),
Some(reg) => BnString::new(reg.name()).into_raw(),
None => BnString::new("invalid_reg").into_raw(),
}
}
Expand All @@ -1901,7 +1896,7 @@ where
let custom_arch = unsafe { &*(ctxt as *mut A) };

match custom_arch.flag_from_id(flag) {
Some(flag) => BnString::new(flag.name().as_ref()).into_raw(),
Some(flag) => BnString::new(flag.name()).into_raw(),
None => BnString::new("invalid_flag").into_raw(),
}
}
Expand All @@ -1913,7 +1908,7 @@ where
let custom_arch = unsafe { &*(ctxt as *mut A) };

match custom_arch.flag_write_from_id(flag_write) {
Some(flag_write) => BnString::new(flag_write.name().as_ref()).into_raw(),
Some(flag_write) => BnString::new(flag_write.name()).into_raw(),
None => BnString::new("invalid_flag_write").into_raw(),
}
}
Expand All @@ -1925,7 +1920,7 @@ where
let custom_arch = unsafe { &*(ctxt as *mut A) };

match custom_arch.flag_class_from_id(class) {
Some(class) => BnString::new(class.name().as_ref()).into_raw(),
Some(class) => BnString::new(class.name()).into_raw(),
None => BnString::new("invalid_flag_class").into_raw(),
}
}
Expand All @@ -1937,7 +1932,7 @@ where
let custom_arch = unsafe { &*(ctxt as *mut A) };

match custom_arch.flag_group_from_id(group) {
Some(group) => BnString::new(group.name().as_ref()).into_raw(),
Some(group) => BnString::new(group.name()).into_raw(),
None => BnString::new("invalid_flag_group").into_raw(),
}
}
Expand Down Expand Up @@ -2400,7 +2395,7 @@ where
let custom_arch = unsafe { &*(ctxt as *mut A) };

match custom_arch.register_stack_from_id(stack) {
Some(stack) => BnString::new(stack.name().as_ref()).into_raw(),
Some(stack) => BnString::new(stack.name()).into_raw(),
None => BnString::new("invalid_reg_stack").into_raw(),
}
}
Expand Down Expand Up @@ -2466,7 +2461,7 @@ where
{
let custom_arch = unsafe { &*(ctxt as *mut A) };
match custom_arch.intrinsic_from_id(intrinsic) {
Some(intrinsic) => BnString::new(intrinsic.name().as_ref()).into_raw(),
Some(intrinsic) => BnString::new(intrinsic.name()).into_raw(),
None => BnString::new("invalid_intrinsic").into_raw(),
}
}
Expand Down Expand Up @@ -2752,8 +2747,6 @@ where
custom_arch.skip_and_return_value(data, addr, val)
}

let name = name.into_bytes_with_nul();

let uninit_arch = ArchitectureBuilder {
arch: MaybeUninit::zeroed(),
func: Some(func),
Expand Down Expand Up @@ -2841,8 +2834,7 @@ where
};

unsafe {
let res =
BNRegisterArchitecture(name.as_ref().as_ptr() as *mut _, &mut custom_arch as *mut _);
let res = BNRegisterArchitecture(name.as_cstr().as_ptr(), &mut custom_arch as *mut _);

assert!(!res.is_null());

Expand Down
14 changes: 4 additions & 10 deletions rust/src/backgroundtask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ pub struct BackgroundTask {
}

impl BackgroundTask {
pub fn new<S: BnStrCompatible>(initial_text: S, can_cancel: bool) -> Result<Ref<Self>> {
let text = initial_text.into_bytes_with_nul();

let handle = unsafe { BNBeginBackgroundTask(text.as_ref().as_ptr() as *mut _, can_cancel) };
pub fn new(initial_text: impl AsCStr, can_cancel: bool) -> Result<Ref<Self>> {
let handle = unsafe { BNBeginBackgroundTask(initial_text.as_cstr().as_ptr(), can_cancel) };

if handle.is_null() {
return Err(());
Expand Down Expand Up @@ -66,12 +64,8 @@ impl BackgroundTask {
unsafe { BNFinishBackgroundTask(self.handle) }
}

pub fn set_progress_text<S: BnStrCompatible>(&self, text: S) {
let progress_text = text.into_bytes_with_nul();

unsafe {
BNSetBackgroundTaskProgressText(self.handle, progress_text.as_ref().as_ptr() as *mut _)
}
pub fn set_progress_text(&self, text: impl AsCStr) {
unsafe { BNSetBackgroundTaskProgressText(self.handle, text.as_cstr().as_ptr()) }
}

pub fn running_tasks() -> Array<BackgroundTask> {
Expand Down
Loading

0 comments on commit 7aa2bb1

Please sign in to comment.