From adbd39ab7c3d4e67e8709af43176e60f31141ba8 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Thu, 8 May 2025 21:39:12 -0400 Subject: [Rust] Flow graph API improvements - Fixed some misc bugs - Added real FlowGraphEdge type - Added accessors for node edges - Added interaction handler to the flow graph example to dump the flow graph to stdin - Split out the flow graph API into smaller files --- rust/src/flowgraph/edge.rs | 113 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 rust/src/flowgraph/edge.rs (limited to 'rust/src/flowgraph/edge.rs') diff --git a/rust/src/flowgraph/edge.rs b/rust/src/flowgraph/edge.rs new file mode 100644 index 00000000..7862f156 --- /dev/null +++ b/rust/src/flowgraph/edge.rs @@ -0,0 +1,113 @@ +use binaryninjacore_sys::*; + +use crate::flowgraph::node::FlowGraphNode; +use crate::flowgraph::{BranchType, EdgePenStyle, ThemeColor}; +use crate::rc::{CoreArrayProvider, CoreArrayProviderInner, Ref}; + +#[derive(Clone, Debug, PartialEq)] +pub struct FlowGraphEdge { + pub branch_type: BranchType, + pub target: Ref, + pub points: Vec, + pub back_edge: bool, + pub style: EdgeStyle, +} + +impl FlowGraphEdge { + pub fn from_raw(value: &BNFlowGraphEdge) -> Self { + let raw_points = unsafe { std::slice::from_raw_parts(value.points, value.pointCount) }; + let points: Vec<_> = raw_points.iter().copied().map(Point::from).collect(); + Self { + branch_type: value.type_, + target: unsafe { FlowGraphNode::from_raw(value.target) }.to_owned(), + points, + back_edge: value.backEdge, + style: value.style.into(), + } + } +} + +impl CoreArrayProvider for FlowGraphEdge { + type Raw = BNFlowGraphEdge; + type Context = (); + type Wrapped<'a> = FlowGraphEdge; +} + +unsafe impl CoreArrayProviderInner for FlowGraphEdge { + unsafe fn free(raw: *mut Self::Raw, count: usize, _: &Self::Context) { + BNFreeFlowGraphNodeEdgeList(raw, count); + } + + unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { + Self::from_raw(raw) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Default)] +pub struct Point { + pub x: f32, + pub y: f32, +} + +impl Point { + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} + +impl From for Point { + fn from(value: BNPoint) -> Self { + Self { + x: value.x, + y: value.y, + } + } +} + +impl From for BNPoint { + fn from(value: Point) -> Self { + Self { + x: value.x, + y: value.y, + } + } +} + +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] +pub struct EdgeStyle { + style: EdgePenStyle, + width: usize, + color: ThemeColor, +} + +impl EdgeStyle { + pub fn new(style: EdgePenStyle, width: usize, color: ThemeColor) -> Self { + Self { + style, + width, + color, + } + } +} + +impl Default for EdgeStyle { + fn default() -> Self { + Self::new(EdgePenStyle::SolidLine, 0, ThemeColor::AddressColor) + } +} + +impl From for EdgeStyle { + fn from(style: BNEdgeStyle) -> Self { + Self::new(style.style, style.width, style.color) + } +} + +impl From for BNEdgeStyle { + fn from(style: EdgeStyle) -> Self { + Self { + style: style.style, + width: style.width, + color: style.color, + } + } +} -- cgit v1.3.1