Skip to content

Commit 0ee0f51

Browse files
committed
Replace BnStrCompatible with AsCStr trait
1 parent b34ddd0 commit 0ee0f51

38 files changed

+1494
-2519
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -281,21 +281,21 @@ fn search_sym_store(bv: &BinaryView, store_path: &String, pdb_info: &PDBInfo) ->
281281
}
282282

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

289289
// This is stored in the BV by the PE loader
290-
let file_path = match view.get_metadata::<String, _>("PDB_FILENAME") {
290+
let file_path = match view.get_metadata::<String>("PDB_FILENAME") {
291291
Some(Ok(md)) => md,
292292
_ => return None,
293293
};
294-
let mut guid = match view.get_metadata::<Vec<u8>, _>("PDB_GUID") {
294+
let mut guid = match view.get_metadata::<Vec<u8>>("PDB_GUID") {
295295
Some(Ok(md)) => md,
296296
_ => return None,
297297
};
298-
let age = match view.get_metadata::<u64, _>("PDB_AGE") {
298+
let age = match view.get_metadata::<u64>("PDB_AGE") {
299299
Some(Ok(md)) => md as u32,
300300
_ => return None,
301301
};

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,10 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
11401140
let vt_pointer = Type::pointer(
11411141
&self.arch,
11421142
&Conf::new(
1143-
Type::named_type_from_type(&QualifiedName::from(vt_name), vt_type.as_ref()),
1143+
Type::named_type_from_type(
1144+
QualifiedName::from(vt_name).string(),
1145+
vt_type.as_ref(),
1146+
),
11441147
max_confidence(),
11451148
),
11461149
);

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},
@@ -1077,8 +1076,8 @@ impl CoreArchitecture {
10771076
CoreArchitectureList(archs, count)
10781077
}
10791078

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

10831082
match res.is_null() {
10841083
false => Some(CoreArchitecture(res)),
@@ -1599,12 +1598,8 @@ macro_rules! cc_func {
15991598

16001599
/// Contains helper methods for all types implementing 'Architecture'
16011600
pub trait ArchitectureExt: Architecture {
1602-
fn register_by_name<S: BnStrCompatible>(&self, name: S) -> Option<Self::Register> {
1603-
let name = name.into_bytes_with_nul();
1604-
1605-
match unsafe {
1606-
BNGetArchitectureRegisterByName(self.as_ref().0, name.as_ref().as_ptr() as *mut _)
1607-
} {
1601+
fn register_by_name(&self, name: impl AsCStr) -> Option<Self::Register> {
1602+
match unsafe { BNGetArchitectureRegisterByName(self.as_ref().0, name.as_cstr().as_ptr()) } {
16081603
0xffff_ffff => None,
16091604
reg => self.register_from_id(reg),
16101605
}
@@ -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
@@ -35,10 +35,8 @@ impl BackgroundTask {
3535
Self { handle }
3636
}
3737

38-
pub fn new<S: BnStrCompatible>(initial_text: S, can_cancel: bool) -> Result<Ref<Self>> {
39-
let text = initial_text.into_bytes_with_nul();
40-
41-
let handle = unsafe { BNBeginBackgroundTask(text.as_ref().as_ptr() as *mut _, can_cancel) };
38+
pub fn new(initial_text: impl AsCStr, can_cancel: bool) -> Result<Ref<Self>> {
39+
let handle = unsafe { BNBeginBackgroundTask(initial_text.as_cstr().as_ptr(), can_cancel) };
4240

4341
if handle.is_null() {
4442
return Err(());
@@ -71,12 +69,8 @@ impl BackgroundTask {
7169
unsafe { BNFinishBackgroundTask(self.handle) }
7270
}
7371

74-
pub fn set_progress_text<S: BnStrCompatible>(&self, text: S) {
75-
let progress_text = text.into_bytes_with_nul();
76-
77-
unsafe {
78-
BNSetBackgroundTaskProgressText(self.handle, progress_text.as_ref().as_ptr() as *mut _)
79-
}
72+
pub fn set_progress_text(&self, text: impl AsCStr) {
73+
unsafe { BNSetBackgroundTaskProgressText(self.handle, text.as_cstr().as_ptr()) }
8074
}
8175

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

0 commit comments

Comments
 (0)