Skip to content

Commit 422b866

Browse files
committed
Replace BnStrCompatible with AsCStr trait
1 parent d36b962 commit 422b866

37 files changed

+1250
-2320
lines changed

rust/examples/pdb-ng/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -280,21 +280,21 @@ fn search_sym_store(store_path: &String, pdb_info: &PDBInfo) -> Result<Option<Ve
280280
}
281281

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

288288
// This is stored in the BV by the PE loader
289-
let file_path = match view.get_metadata::<String, _>("PDB_FILENAME") {
289+
let file_path = match view.get_metadata::<String>("PDB_FILENAME") {
290290
Some(Ok(md)) => md,
291291
_ => return None,
292292
};
293-
let mut guid = match view.get_metadata::<Vec<u8>, _>("PDB_GUID") {
293+
let mut guid = match view.get_metadata::<Vec<u8>>("PDB_GUID") {
294294
Some(Ok(md)) => md,
295295
_ => return None,
296296
};
297-
let age = match view.get_metadata::<u64, _>("PDB_AGE") {
297+
let age = match view.get_metadata::<u64>("PDB_AGE") {
298298
Some(Ok(md)) => md as u32,
299299
_ => return None,
300300
};

rust/examples/pdb-ng/src/type_parser.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,10 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
11361136
let vt_pointer = Type::pointer(
11371137
&self.arch,
11381138
&Conf::new(
1139-
Type::named_type_from_type(&QualifiedName::from(vt_name), vt_type.as_ref()),
1139+
Type::named_type_from_type(
1140+
QualifiedName::from(vt_name).string(),
1141+
vt_type.as_ref(),
1142+
),
11401143
max_confidence(),
11411144
),
11421145
);

rust/src/architecture.rs

+14-22
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use crate::{
3737
platform::Platform,
3838
rc::*,
3939
relocation::CoreRelocationHandler,
40-
string::BnStrCompatible,
4140
string::*,
4241
types::{Conf, NameAndType, Type},
4342
{BranchType, Endianness},
@@ -1085,8 +1084,8 @@ impl CoreArchitecture {
10851084
CoreArchitectureList(archs, count)
10861085
}
10871086

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

10911090
match res.is_null() {
10921091
false => Some(CoreArchitecture(res)),
@@ -1607,12 +1606,8 @@ macro_rules! cc_func {
16071606

16081607
/// Contains helper methods for all types implementing 'Architecture'
16091608
pub trait ArchitectureExt: Architecture {
1610-
fn register_by_name<S: BnStrCompatible>(&self, name: S) -> Option<Self::Register> {
1611-
let name = name.into_bytes_with_nul();
1612-
1613-
match unsafe {
1614-
BNGetArchitectureRegisterByName(self.as_ref().0, name.as_ref().as_ptr() as *mut _)
1615-
} {
1609+
fn register_by_name(&self, name: impl AsCStr) -> Option<Self::Register> {
1610+
match unsafe { BNGetArchitectureRegisterByName(self.as_ref().0, name.as_cstr().as_ptr()) } {
16161611
0xffff_ffff => None,
16171612
reg => self.register_from_id(reg),
16181613
}
@@ -1677,7 +1672,7 @@ pub trait ArchitectureExt: Architecture {
16771672

16781673
fn register_relocation_handler<S, R, F>(&self, name: S, func: F)
16791674
where
1680-
S: BnStrCompatible,
1675+
S: AsCStr,
16811676
R: 'static
16821677
+ RelocationHandler<Handle = CustomRelocationHandlerHandle<R>>
16831678
+ Send
@@ -1700,7 +1695,7 @@ impl<T: Architecture> ArchitectureExt for T {}
17001695

17011696
pub fn register_architecture<S, A, F>(name: S, func: F) -> &'static A
17021697
where
1703-
S: BnStrCompatible,
1698+
S: AsCStr,
17041699
A: 'static + Architecture<Handle = CustomArchitectureHandle<A>> + Send + Sync + Sized,
17051700
F: FnOnce(CustomArchitectureHandle<A>, CoreArchitecture) -> A,
17061701
{
@@ -1889,7 +1884,7 @@ where
18891884
let custom_arch = unsafe { &*(ctxt as *mut A) };
18901885

18911886
match custom_arch.register_from_id(reg) {
1892-
Some(reg) => BnString::new(reg.name().as_ref()).into_raw(),
1887+
Some(reg) => BnString::new(reg.name()).into_raw(),
18931888
None => BnString::new("invalid_reg").into_raw(),
18941889
}
18951890
}
@@ -1901,7 +1896,7 @@ where
19011896
let custom_arch = unsafe { &*(ctxt as *mut A) };
19021897

19031898
match custom_arch.flag_from_id(flag) {
1904-
Some(flag) => BnString::new(flag.name().as_ref()).into_raw(),
1899+
Some(flag) => BnString::new(flag.name()).into_raw(),
19051900
None => BnString::new("invalid_flag").into_raw(),
19061901
}
19071902
}
@@ -1913,7 +1908,7 @@ where
19131908
let custom_arch = unsafe { &*(ctxt as *mut A) };
19141909

19151910
match custom_arch.flag_write_from_id(flag_write) {
1916-
Some(flag_write) => BnString::new(flag_write.name().as_ref()).into_raw(),
1911+
Some(flag_write) => BnString::new(flag_write.name()).into_raw(),
19171912
None => BnString::new("invalid_flag_write").into_raw(),
19181913
}
19191914
}
@@ -1925,7 +1920,7 @@ where
19251920
let custom_arch = unsafe { &*(ctxt as *mut A) };
19261921

19271922
match custom_arch.flag_class_from_id(class) {
1928-
Some(class) => BnString::new(class.name().as_ref()).into_raw(),
1923+
Some(class) => BnString::new(class.name()).into_raw(),
19291924
None => BnString::new("invalid_flag_class").into_raw(),
19301925
}
19311926
}
@@ -1937,7 +1932,7 @@ where
19371932
let custom_arch = unsafe { &*(ctxt as *mut A) };
19381933

19391934
match custom_arch.flag_group_from_id(group) {
1940-
Some(group) => BnString::new(group.name().as_ref()).into_raw(),
1935+
Some(group) => BnString::new(group.name()).into_raw(),
19411936
None => BnString::new("invalid_flag_group").into_raw(),
19421937
}
19431938
}
@@ -2400,7 +2395,7 @@ where
24002395
let custom_arch = unsafe { &*(ctxt as *mut A) };
24012396

24022397
match custom_arch.register_stack_from_id(stack) {
2403-
Some(stack) => BnString::new(stack.name().as_ref()).into_raw(),
2398+
Some(stack) => BnString::new(stack.name()).into_raw(),
24042399
None => BnString::new("invalid_reg_stack").into_raw(),
24052400
}
24062401
}
@@ -2466,7 +2461,7 @@ where
24662461
{
24672462
let custom_arch = unsafe { &*(ctxt as *mut A) };
24682463
match custom_arch.intrinsic_from_id(intrinsic) {
2469-
Some(intrinsic) => BnString::new(intrinsic.name().as_ref()).into_raw(),
2464+
Some(intrinsic) => BnString::new(intrinsic.name()).into_raw(),
24702465
None => BnString::new("invalid_intrinsic").into_raw(),
24712466
}
24722467
}
@@ -2752,8 +2747,6 @@ where
27522747
custom_arch.skip_and_return_value(data, addr, val)
27532748
}
27542749

2755-
let name = name.into_bytes_with_nul();
2756-
27572750
let uninit_arch = ArchitectureBuilder {
27582751
arch: MaybeUninit::zeroed(),
27592752
func: Some(func),
@@ -2841,8 +2834,7 @@ where
28412834
};
28422835

28432836
unsafe {
2844-
let res =
2845-
BNRegisterArchitecture(name.as_ref().as_ptr() as *mut _, &mut custom_arch as *mut _);
2837+
let res = BNRegisterArchitecture(name.as_cstr().as_ptr(), &mut custom_arch as *mut _);
28462838

28472839
assert!(!res.is_null());
28482840

rust/src/backgroundtask.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ pub struct BackgroundTask {
3030
}
3131

3232
impl BackgroundTask {
33-
pub fn new<S: BnStrCompatible>(initial_text: S, can_cancel: bool) -> Result<Ref<Self>> {
34-
let text = initial_text.into_bytes_with_nul();
35-
36-
let handle = unsafe { BNBeginBackgroundTask(text.as_ref().as_ptr() as *mut _, can_cancel) };
33+
pub fn new(initial_text: impl AsCStr, can_cancel: bool) -> Result<Ref<Self>> {
34+
let handle = unsafe { BNBeginBackgroundTask(initial_text.as_cstr().as_ptr(), can_cancel) };
3735

3836
if handle.is_null() {
3937
return Err(());
@@ -66,12 +64,8 @@ impl BackgroundTask {
6664
unsafe { BNFinishBackgroundTask(self.handle) }
6765
}
6866

69-
pub fn set_progress_text<S: BnStrCompatible>(&self, text: S) {
70-
let progress_text = text.into_bytes_with_nul();
71-
72-
unsafe {
73-
BNSetBackgroundTaskProgressText(self.handle, progress_text.as_ref().as_ptr() as *mut _)
74-
}
67+
pub fn set_progress_text(&self, text: impl AsCStr) {
68+
unsafe { BNSetBackgroundTaskProgressText(self.handle, text.as_cstr().as_ptr()) }
7569
}
7670

7771
pub fn running_tasks() -> Array<BackgroundTask> {

0 commit comments

Comments
 (0)