1414
1515//! Interfaces for creating and displaying pretty CFGs in Binary Ninja.
1616
17+ use crate :: disassembly:: DisassemblyTextLine ;
1718use binaryninjacore_sys:: * ;
1819use std:: slice;
19- use crate :: disassembly:: DisassemblyTextLine ;
2020
2121use crate :: rc:: * ;
2222
23+ use crate :: basic_block:: { BasicBlock , BlockContext } ;
24+ use crate :: function:: HighlightColor ;
2325use std:: marker:: PhantomData ;
2426
2527pub type BranchType = BNBranchType ;
@@ -37,15 +39,19 @@ impl FlowGraph {
3739 Self { handle : raw }
3840 }
3941
42+ pub ( crate ) unsafe fn ref_from_raw ( raw : * mut BNFlowGraph ) -> Ref < Self > {
43+ Ref :: new ( Self { handle : raw } )
44+ }
45+
4046 pub fn new ( ) -> Ref < Self > {
41- unsafe { Ref :: new ( FlowGraph :: from_raw ( BNCreateFlowGraph ( ) ) ) }
47+ unsafe { FlowGraph :: ref_from_raw ( BNCreateFlowGraph ( ) ) }
4248 }
4349
4450 pub fn nodes < ' a > ( & self ) -> Vec < Ref < FlowGraphNode < ' a > > > {
4551 let mut count: usize = 0 ;
46- let mut nodes_ptr = unsafe { BNGetFlowGraphNodes ( self . handle , & mut count as * mut usize ) } ;
52+ let nodes_ptr = unsafe { BNGetFlowGraphNodes ( self . handle , & mut count as * mut usize ) } ;
4753
48- let mut nodes = unsafe { slice:: from_raw_parts_mut ( nodes_ptr, count) } ;
54+ let nodes = unsafe { slice:: from_raw_parts_mut ( nodes_ptr, count) } ;
4955
5056 let mut result = vec ! [ ] ;
5157 result. reserve ( count) ;
@@ -131,8 +137,37 @@ impl<'a> FlowGraphNode<'a> {
131137 }
132138 }
133139
134- pub fn new ( graph : & FlowGraph ) -> Self {
135- unsafe { FlowGraphNode :: from_raw ( BNCreateFlowGraphNode ( graph. handle ) ) }
140+ pub ( crate ) unsafe fn ref_from_raw ( raw : * mut BNFlowGraphNode ) -> Ref < Self > {
141+ Ref :: new ( Self {
142+ handle : raw,
143+ _data : PhantomData ,
144+ } )
145+ }
146+
147+ pub fn new ( graph : & FlowGraph ) -> Ref < Self > {
148+ unsafe { FlowGraphNode :: ref_from_raw ( BNCreateFlowGraphNode ( graph. handle ) ) }
149+ }
150+
151+ pub fn basic_block < C : BlockContext > ( & self , context : C ) -> Option < Ref < BasicBlock < C > > > {
152+ let block_ptr = unsafe { BNGetFlowGraphBasicBlock ( self . handle ) } ;
153+ if block_ptr. is_null ( ) {
154+ return None ;
155+ }
156+ Some ( unsafe { BasicBlock :: ref_from_raw ( block_ptr, context) } )
157+ }
158+
159+ pub fn set_basic_block < C : BlockContext > ( & self , block : Option < & BasicBlock < C > > ) {
160+ match block {
161+ Some ( block) => unsafe { BNSetFlowGraphBasicBlock ( self . handle , block. handle ) } ,
162+ None => unsafe { BNSetFlowGraphBasicBlock ( self . handle , std:: ptr:: null_mut ( ) ) } ,
163+ }
164+ }
165+
166+ pub fn lines ( & self ) -> Array < DisassemblyTextLine > {
167+ let mut count = 0 ;
168+ let result = unsafe { BNGetFlowGraphNodeLines ( self . handle , & mut count) } ;
169+ assert ! ( !result. is_null( ) ) ;
170+ unsafe { Array :: new ( result, count, ( ) ) }
136171 }
137172
138173 pub fn set_lines ( & self , lines : impl IntoIterator < Item = DisassemblyTextLine > ) {
@@ -149,6 +184,30 @@ impl<'a> FlowGraphNode<'a> {
149184 }
150185 }
151186
187+ /// Returns the graph position of the node in X, Y form.
188+ pub fn position ( & self ) -> ( i32 , i32 ) {
189+ let pos_x = unsafe { BNGetFlowGraphNodeX ( self . handle ) } ;
190+ let pos_y = unsafe { BNGetFlowGraphNodeY ( self . handle ) } ;
191+ ( pos_x, pos_y)
192+ }
193+
194+ /// Sets the graph position of the node.
195+ pub fn set_position ( & self , x : i32 , y : i32 ) {
196+ unsafe { BNFlowGraphNodeSetX ( self . handle , x) } ;
197+ unsafe { BNFlowGraphNodeSetX ( self . handle , y) } ;
198+ }
199+
200+ pub fn highlight_color ( & self ) -> HighlightColor {
201+ let raw = unsafe { BNGetFlowGraphNodeHighlight ( self . handle ) } ;
202+ HighlightColor :: from ( raw)
203+ }
204+
205+ pub fn set_highlight_color ( & self , highlight : HighlightColor ) {
206+ unsafe { BNSetFlowGraphNodeHighlight ( self . handle , highlight. into ( ) ) } ;
207+ }
208+
209+ // TODO: Add getters and setters for edges
210+
152211 pub fn add_outgoing_edge (
153212 & self ,
154213 type_ : BranchType ,
0 commit comments