summaryrefslogtreecommitdiff
path: root/rust/src/workflow.rs
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2025-05-04 19:10:56 -0400
committerMason Reed <35282038+emesare@users.noreply.github.com>2025-05-12 17:45:24 -0400
commita826c589dfc10c542deba7ca3343a462e02d6bde (patch)
treef116254bef39f787268bbecc5eac19da310db9ce /rust/src/workflow.rs
parent28b3c4044af06fdc32c9c85bf8381b5058306427 (diff)
[Rust] Simplify `BnStrCompatible` trait
Followup to https://github.com/Vector35/binaryninja-api/pull/5897/ This simplifies usage of the trait in user code, should just be able to `to_cstr` to get the cstr repr and then call `as_ptr`. Co-authored-by: Michael Krasnitski <michael.krasnitski@gmail.com>
Diffstat (limited to 'rust/src/workflow.rs')
-rw-r--r--rust/src/workflow.rs119
1 files changed, 49 insertions, 70 deletions
diff --git a/rust/src/workflow.rs b/rust/src/workflow.rs
index 71a60824..b5023815 100644
--- a/rust/src/workflow.rs
+++ b/rust/src/workflow.rs
@@ -9,7 +9,7 @@ use crate::low_level_il::function::{LowLevelILFunction, Mutable, NonSSA, NonSSAV
use crate::low_level_il::MutableLiftedILFunction;
use crate::medium_level_il::MediumLevelILFunction;
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable};
-use crate::string::{BnStrCompatible, BnString};
+use crate::string::{AsCStr, BnString};
use std::ffi::{c_char, c_void};
use std::ptr::NonNull;
@@ -108,8 +108,8 @@ impl AnalysisContext {
}
}
- pub fn inform<S: BnStrCompatible>(&self, request: S) -> bool {
- let request = request.into_bytes_with_nul();
+ pub fn inform<S: AsCStr>(&self, request: S) -> bool {
+ let request = request.to_cstr();
unsafe {
BNAnalysisContextInform(
self.handle.as_ptr(),
@@ -166,9 +166,9 @@ impl Activity {
Ref::new(Self { handle })
}
- pub fn new<S: BnStrCompatible>(config: S) -> Ref<Self> {
+ pub fn new<S: AsCStr>(config: S) -> Ref<Self> {
unsafe extern "C" fn cb_action_nop(_: *mut c_void, _: *mut BNAnalysisContext) {}
- let config = config.into_bytes_with_nul();
+ let config = config.to_cstr();
let result = unsafe {
BNCreateActivity(
config.as_ref().as_ptr() as *const c_char,
@@ -181,7 +181,7 @@ impl Activity {
pub fn new_with_action<S, F>(config: S, mut action: F) -> Ref<Self>
where
- S: BnStrCompatible,
+ S: AsCStr,
F: FnMut(&AnalysisContext),
{
unsafe extern "C" fn cb_action<F: FnMut(&AnalysisContext)>(
@@ -193,7 +193,7 @@ impl Activity {
ctxt(&AnalysisContext::from_raw(analysis))
}
}
- let config = config.into_bytes_with_nul();
+ let config = config.to_cstr();
let result = unsafe {
BNCreateActivity(
config.as_ref().as_ptr() as *const c_char,
@@ -250,8 +250,8 @@ impl Workflow {
/// Create a new unregistered [Workflow] with no activities.
///
/// To get a copy of an existing registered [Workflow] use [Workflow::clone_to].
- pub fn new<S: BnStrCompatible>(name: S) -> Ref<Self> {
- let name = name.into_bytes_with_nul();
+ pub fn new<S: AsCStr>(name: S) -> Ref<Self> {
+ let name = name.to_cstr();
let result = unsafe { BNCreateWorkflow(name.as_ref().as_ptr() as *const c_char) };
unsafe { Workflow::ref_from_raw(NonNull::new(result).unwrap()) }
}
@@ -260,7 +260,7 @@ impl Workflow {
///
/// * `name` - the name for the new [Workflow]
#[must_use]
- pub fn clone_to<S: BnStrCompatible + Clone>(&self, name: S) -> Ref<Workflow> {
+ pub fn clone_to<S: AsCStr + Clone>(&self, name: S) -> Ref<Workflow> {
self.clone_to_with_root(name, "")
}
@@ -269,13 +269,13 @@ impl Workflow {
/// * `name` - the name for the new [Workflow]
/// * `root_activity` - perform the clone operation with this activity as the root
#[must_use]
- pub fn clone_to_with_root<S: BnStrCompatible, A: BnStrCompatible>(
+ pub fn clone_to_with_root<S: AsCStr, A: AsCStr>(
&self,
name: S,
root_activity: A,
) -> Ref<Workflow> {
- let raw_name = name.into_bytes_with_nul();
- let activity = root_activity.into_bytes_with_nul();
+ let raw_name = name.to_cstr();
+ let activity = root_activity.to_cstr();
unsafe {
Self::ref_from_raw(
NonNull::new(BNWorkflowClone(
@@ -288,10 +288,9 @@ impl Workflow {
}
}
- pub fn instance<S: BnStrCompatible>(name: S) -> Ref<Workflow> {
- let result = unsafe {
- BNWorkflowInstance(name.into_bytes_with_nul().as_ref().as_ptr() as *const c_char)
- };
+ pub fn instance<S: AsCStr>(name: S) -> Ref<Workflow> {
+ let result =
+ unsafe { BNWorkflowInstance(name.to_cstr().as_ref().as_ptr() as *const c_char) };
unsafe { Workflow::ref_from_raw(NonNull::new(result).unwrap()) }
}
@@ -317,8 +316,8 @@ impl Workflow {
/// Register this [Workflow], making it immutable and available for use.
///
/// * `configuration` - a JSON representation of the workflow configuration
- pub fn register_with_config<S: BnStrCompatible>(&self, config: S) -> Result<(), ()> {
- let config = config.into_bytes_with_nul();
+ pub fn register_with_config<S: AsCStr>(&self, config: S) -> Result<(), ()> {
+ let config = config.to_cstr();
if unsafe {
BNRegisterWorkflow(
self.handle.as_ptr(),
@@ -349,12 +348,9 @@ impl Workflow {
) -> Result<Ref<Activity>, ()>
where
I: IntoIterator,
- I::Item: BnStrCompatible,
+ I::Item: AsCStr,
{
- let subactivities_raw: Vec<_> = subactivities
- .into_iter()
- .map(|x| x.into_bytes_with_nul())
- .collect();
+ let subactivities_raw: Vec<_> = subactivities.into_iter().map(|x| x.to_cstr()).collect();
let mut subactivities_ptr: Vec<*const _> = subactivities_raw
.iter()
.map(|x| x.as_ref().as_ptr() as *const c_char)
@@ -372,11 +368,11 @@ impl Workflow {
}
/// Determine if an Activity exists in this [Workflow].
- pub fn contains<A: BnStrCompatible>(&self, activity: A) -> bool {
+ pub fn contains<A: AsCStr>(&self, activity: A) -> bool {
unsafe {
BNWorkflowContains(
self.handle.as_ptr(),
- activity.into_bytes_with_nul().as_ref().as_ptr() as *const c_char,
+ activity.to_cstr().as_ref().as_ptr() as *const c_char,
)
}
}
@@ -390,11 +386,11 @@ impl Workflow {
/// [Workflow], just for the given `activity`.
///
/// `activity` - return the configuration for the `activity`
- pub fn configuration_with_activity<A: BnStrCompatible>(&self, activity: A) -> String {
+ pub fn configuration_with_activity<A: AsCStr>(&self, activity: A) -> String {
let result = unsafe {
BNWorkflowGetConfiguration(
self.handle.as_ptr(),
- activity.into_bytes_with_nul().as_ref().as_ptr() as *const c_char,
+ activity.to_cstr().as_ref().as_ptr() as *const c_char,
)
};
assert!(!result.is_null());
@@ -411,8 +407,8 @@ impl Workflow {
}
/// Retrieve the Activity object for the specified `name`.
- pub fn activity<A: BnStrCompatible>(&self, name: A) -> Option<Ref<Activity>> {
- let name = name.into_bytes_with_nul();
+ pub fn activity<A: AsCStr>(&self, name: A) -> Option<Ref<Activity>> {
+ let name = name.to_cstr();
let result = unsafe {
BNWorkflowGetActivity(
self.handle.as_ptr(),
@@ -426,12 +422,12 @@ impl Workflow {
/// specified just for the given `activity`.
///
/// * `activity` - if specified, return the roots for the `activity`
- pub fn activity_roots<A: BnStrCompatible>(&self, activity: A) -> Array<BnString> {
+ pub fn activity_roots<A: AsCStr>(&self, activity: A) -> Array<BnString> {
let mut count = 0;
let result = unsafe {
BNWorkflowGetActivityRoots(
self.handle.as_ptr(),
- activity.into_bytes_with_nul().as_ref().as_ptr() as *const c_char,
+ activity.to_cstr().as_ref().as_ptr() as *const c_char,
&mut count,
)
};
@@ -443,16 +439,12 @@ impl Workflow {
///
/// * `activity` - if specified, return the direct children and optionally the descendants of the `activity` (includes `activity`)
/// * `immediate` - whether to include only direct children of `activity` or all descendants
- pub fn subactivities<A: BnStrCompatible>(
- &self,
- activity: A,
- immediate: bool,
- ) -> Array<BnString> {
+ pub fn subactivities<A: AsCStr>(&self, activity: A, immediate: bool) -> Array<BnString> {
let mut count = 0;
let result = unsafe {
BNWorkflowGetSubactivities(
self.handle.as_ptr(),
- activity.into_bytes_with_nul().as_ref().as_ptr() as *const c_char,
+ activity.to_cstr().as_ref().as_ptr() as *const c_char,
immediate,
&mut count,
)
@@ -467,14 +459,11 @@ impl Workflow {
/// * `activities` - the list of Activities to assign
pub fn assign_subactivities<A, I>(&self, activity: A, activities: I) -> bool
where
- A: BnStrCompatible,
+ A: AsCStr,
I: IntoIterator,
- I::Item: BnStrCompatible,
+ I::Item: AsCStr,
{
- let input_list: Vec<_> = activities
- .into_iter()
- .map(|a| a.into_bytes_with_nul())
- .collect();
+ let input_list: Vec<_> = activities.into_iter().map(|a| a.to_cstr()).collect();
let mut input_list_ptr: Vec<*const _> = input_list
.iter()
.map(|x| x.as_ref().as_ptr() as *const c_char)
@@ -482,7 +471,7 @@ impl Workflow {
unsafe {
BNWorkflowAssignSubactivities(
self.handle.as_ptr(),
- activity.into_bytes_with_nul().as_ref().as_ptr() as *const c_char,
+ activity.to_cstr().as_ref().as_ptr() as *const c_char,
input_list_ptr.as_mut_ptr(),
input_list.len(),
)
@@ -500,14 +489,11 @@ impl Workflow {
/// * `activities` - the list of Activities to insert
pub fn insert<A, I>(&self, activity: A, activities: I) -> bool
where
- A: BnStrCompatible,
+ A: AsCStr,
I: IntoIterator,
- I::Item: BnStrCompatible,
+ I::Item: AsCStr,
{
- let input_list: Vec<_> = activities
- .into_iter()
- .map(|a| a.into_bytes_with_nul())
- .collect();
+ let input_list: Vec<_> = activities.into_iter().map(|a| a.to_cstr()).collect();
let mut input_list_ptr: Vec<*const _> = input_list
.iter()
.map(|x| x.as_ref().as_ptr() as *const c_char)
@@ -515,7 +501,7 @@ impl Workflow {
unsafe {
BNWorkflowInsert(
self.handle.as_ptr(),
- activity.into_bytes_with_nul().as_ref().as_ptr() as *const c_char,
+ activity.to_cstr().as_ref().as_ptr() as *const c_char,
input_list_ptr.as_mut_ptr(),
input_list.len(),
)
@@ -528,14 +514,11 @@ impl Workflow {
/// * `activities` - the list of Activities to insert
pub fn insert_after<A, I>(&self, activity: A, activities: I) -> bool
where
- A: BnStrCompatible,
+ A: AsCStr,
I: IntoIterator,
- I::Item: BnStrCompatible,
+ I::Item: AsCStr,
{
- let input_list: Vec<_> = activities
- .into_iter()
- .map(|a| a.into_bytes_with_nul())
- .collect();
+ let input_list: Vec<_> = activities.into_iter().map(|a| a.to_cstr()).collect();
let mut input_list_ptr: Vec<*const _> = input_list
.iter()
.map(|x| x.as_ref().as_ptr() as *const c_char)
@@ -543,7 +526,7 @@ impl Workflow {
unsafe {
BNWorkflowInsertAfter(
self.handle.as_ptr(),
- activity.into_bytes_with_nul().as_ref().as_ptr() as *const c_char,
+ activity.to_cstr().as_ref().as_ptr() as *const c_char,
input_list_ptr.as_mut_ptr(),
input_list.len(),
)
@@ -551,11 +534,11 @@ impl Workflow {
}
/// Remove the specified `activity`
- pub fn remove<A: BnStrCompatible>(&self, activity: A) -> bool {
+ pub fn remove<A: AsCStr>(&self, activity: A) -> bool {
unsafe {
BNWorkflowRemove(
self.handle.as_ptr(),
- activity.into_bytes_with_nul().as_ref().as_ptr() as *const c_char,
+ activity.to_cstr().as_ref().as_ptr() as *const c_char,
)
}
}
@@ -564,16 +547,12 @@ impl Workflow {
///
/// * `activity` - the Activity to replace
/// * `new_activity` - the replacement Activity
- pub fn replace<A: BnStrCompatible, N: BnStrCompatible>(
- &self,
- activity: A,
- new_activity: N,
- ) -> bool {
+ pub fn replace<A: AsCStr, N: AsCStr>(&self, activity: A, new_activity: N) -> bool {
unsafe {
BNWorkflowReplace(
self.handle.as_ptr(),
- activity.into_bytes_with_nul().as_ref().as_ptr() as *const c_char,
- new_activity.into_bytes_with_nul().as_ref().as_ptr() as *const c_char,
+ activity.to_cstr().as_ref().as_ptr() as *const c_char,
+ new_activity.to_cstr().as_ref().as_ptr() as *const c_char,
)
}
}
@@ -582,13 +561,13 @@ impl Workflow {
///
/// * `activity` - if specified, generate the Flowgraph using `activity` as the root
/// * `sequential` - whether to generate a **Composite** or **Sequential** style graph
- pub fn graph<A: BnStrCompatible>(
+ pub fn graph<A: AsCStr>(
&self,
activity: A,
sequential: Option<bool>,
) -> Option<Ref<FlowGraph>> {
let sequential = sequential.unwrap_or(false);
- let activity_name = activity.into_bytes_with_nul();
+ let activity_name = activity.to_cstr();
let graph = unsafe {
BNWorkflowGetGraph(
self.handle.as_ptr(),