1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- //! A view on binary data and queryable interface of a binary file.
16- //!
17- //! One key job of BinaryView is file format parsing which allows Binary Ninja to read, write,
18- //! insert, remove portions of the file given a virtual address.
19- //!
20- //! For the purposes of this documentation we define a virtual address as the memory address that
21- //! the various pieces of the physical file will be loaded at.
22- //! TODO : Mirror the Python docs for this
15+ //! A view on binary data and queryable interface of a binary files analysis.
2316
2417use binaryninjacore_sys:: * ;
2518
@@ -104,6 +97,7 @@ pub trait BinaryViewBase: AsRef<BinaryView> {
10497 0
10598 }
10699
100+ /// Check if the offset is valid for the current view.
107101 fn offset_valid ( & self , offset : u64 ) -> bool {
108102 let mut buf = [ 0u8 ; 1 ] ;
109103
@@ -112,22 +106,28 @@ pub trait BinaryViewBase: AsRef<BinaryView> {
112106 self . as_ref ( ) . read ( & mut buf[ ..] , offset) == buf. len ( )
113107 }
114108
109+ /// Check if the offset is readable for the current view.
115110 fn offset_readable ( & self , offset : u64 ) -> bool {
116111 self . offset_valid ( offset)
117112 }
118113
114+ /// Check if the offset is writable for the current view.
119115 fn offset_writable ( & self , offset : u64 ) -> bool {
120116 self . offset_valid ( offset)
121117 }
122118
119+ /// Check if the offset is executable for the current view.
123120 fn offset_executable ( & self , offset : u64 ) -> bool {
124121 self . offset_valid ( offset)
125122 }
126123
124+ /// Check if the offset is backed by the original file and not added after the fact.
127125 fn offset_backed_by_file ( & self , offset : u64 ) -> bool {
128126 self . offset_valid ( offset)
129127 }
130128
129+ /// Get the next valid offset after the provided `offset`, useful if you need to iterate over all
130+ /// readable offsets in the view.
131131 fn next_valid_offset_after ( & self , offset : u64 ) -> u64 {
132132 let start = self . as_ref ( ) . start ( ) ;
133133
@@ -138,15 +138,17 @@ pub trait BinaryViewBase: AsRef<BinaryView> {
138138 }
139139 }
140140
141- # [ allow ( unused ) ]
142- fn modification_status ( & self , offset : u64 ) -> ModificationStatus {
141+ /// Whether the data at the given `offset` been modified (patched).
142+ fn modification_status ( & self , _offset : u64 ) -> ModificationStatus {
143143 ModificationStatus :: Original
144144 }
145145
146+ /// The lowest address in the view.
146147 fn start ( & self ) -> u64 {
147148 0
148149 }
149150
151+ /// The length of the view.
150152 fn len ( & self ) -> u64 {
151153 0
152154 }
@@ -2454,6 +2456,26 @@ pub trait BinaryViewExt: BinaryViewBase {
24542456
24552457impl < T : BinaryViewBase > BinaryViewExt for T { }
24562458
2459+ /// Represents the "whole view" of the binary and its analysis.
2460+ ///
2461+ /// Analysis information:
2462+ ///
2463+ /// - [`BinaryView::functions`]
2464+ /// - [`BinaryView::data_variables`]
2465+ /// - [`BinaryView::strings`]
2466+ ///
2467+ /// Annotation information:
2468+ ///
2469+ /// - [`BinaryView::symbols`]
2470+ /// - [`BinaryView::tags_all_scopes`]
2471+ /// - [`BinaryView::comments`]
2472+ ///
2473+ /// Data representation and binary information:
2474+ ///
2475+ /// - [`BinaryView::types`]
2476+ /// - [`BinaryView::segments`]
2477+ /// - [`BinaryView::sections`]
2478+ ///
24572479/// # Cleaning up
24582480///
24592481/// [`BinaryView`] has a cyclic relationship with the associated [`FileMetadata`], each holds a strong
@@ -2476,21 +2498,20 @@ impl BinaryView {
24762498 Ref :: new ( Self { handle } )
24772499 }
24782500
2479- /// Construct the raw binary view from the given metadata. Before calling this make sure you have
2480- /// a valid file path set for the [`FileMetadata`]. It is required that the [`FileMetadata::file_path`]
2481- /// exist on the local filesystem.
2501+ /// Construct the raw binary view from the given metadata.
2502+ ///
2503+ /// Before calling this, make sure you have a valid file path set for the [`FileMetadata`]. It is
2504+ /// required that the [`FileMetadata::file_path`] exist in the local filesystem.
24822505 pub fn from_metadata ( meta : & FileMetadata ) -> Result < Ref < Self > > {
24832506 if !meta. file_path ( ) . exists ( ) {
24842507 return Err ( ( ) ) ;
24852508 }
24862509 let file = meta. file_path ( ) . to_cstr ( ) ;
24872510 let handle =
24882511 unsafe { BNCreateBinaryDataViewFromFilename ( meta. handle , file. as_ptr ( ) as * mut _ ) } ;
2489-
24902512 if handle. is_null ( ) {
24912513 return Err ( ( ) ) ;
24922514 }
2493-
24942515 unsafe { Ok ( Ref :: new ( Self { handle } ) ) }
24952516 }
24962517
@@ -2503,28 +2524,32 @@ impl BinaryView {
25032524 Self :: from_metadata ( meta)
25042525 }
25052526
2506- pub fn from_accessor < A : Accessor > (
2527+ // TODO: Provide an API that manages the lifetime of the accessor and the view.
2528+ /// Construct the raw binary view from the given `accessor` and metadata.
2529+ ///
2530+ /// It is the responsibility of the caller to keep the accessor alive for the lifetime of the view;
2531+ /// because of this, we mark the function as unsafe.
2532+ pub unsafe fn from_accessor < A : Accessor > (
25072533 meta : & FileMetadata ,
2508- file : & mut FileAccessor < A > ,
2534+ accessor : & mut FileAccessor < A > ,
25092535 ) -> Result < Ref < Self > > {
2510- let handle = unsafe { BNCreateBinaryDataViewFromFile ( meta. handle , & mut file. raw ) } ;
2511-
2536+ let handle = unsafe { BNCreateBinaryDataViewFromFile ( meta. handle , & mut accessor. raw ) } ;
25122537 if handle. is_null ( ) {
25132538 return Err ( ( ) ) ;
25142539 }
2515-
25162540 unsafe { Ok ( Ref :: new ( Self { handle } ) ) }
25172541 }
25182542
2543+ /// Construct the raw binary view from the given `data` and metadata.
2544+ ///
2545+ /// The data will be copied into the view, so the caller does not need to keep the data alive.
25192546 pub fn from_data ( meta : & FileMetadata , data : & [ u8 ] ) -> Result < Ref < Self > > {
25202547 let handle = unsafe {
25212548 BNCreateBinaryDataViewFromData ( meta. handle , data. as_ptr ( ) as * mut _ , data. len ( ) )
25222549 } ;
2523-
25242550 if handle. is_null ( ) {
25252551 return Err ( ( ) ) ;
25262552 }
2527-
25282553 unsafe { Ok ( Ref :: new ( Self { handle } ) ) }
25292554 }
25302555
@@ -2571,45 +2596,26 @@ impl BinaryViewBase for BinaryView {
25712596 unsafe { BNRemoveViewData ( self . handle , offset, len as u64 ) }
25722597 }
25732598
2574- /// Check if the offset is valid for the current view.
2575- ///
2576- /// NOTE: If operating within a [`Workflow`], consider using [`AnalysisContext::is_offset_valid`].
25772599 fn offset_valid ( & self , offset : u64 ) -> bool {
25782600 unsafe { BNIsValidOffset ( self . handle , offset) }
25792601 }
25802602
2581- /// Check if the offset is readable for the current view.
2582- ///
2583- /// NOTE: If operating within a [`Workflow`], consider using [`AnalysisContext::is_offset_valid`].
25842603 fn offset_readable ( & self , offset : u64 ) -> bool {
25852604 unsafe { BNIsOffsetReadable ( self . handle , offset) }
25862605 }
25872606
2588- /// Check if the offset is writable for the current view.
2589- ///
2590- /// NOTE: If operating within a [`Workflow`], consider using [`AnalysisContext::is_offset_writable`].
25912607 fn offset_writable ( & self , offset : u64 ) -> bool {
25922608 unsafe { BNIsOffsetWritable ( self . handle , offset) }
25932609 }
25942610
2595- /// Check if the offset is executable for the current view.
2596- ///
2597- /// NOTE: If operating within a [`Workflow`], consider using [`AnalysisContext::is_offset_executable`].
25982611 fn offset_executable ( & self , offset : u64 ) -> bool {
25992612 unsafe { BNIsOffsetExecutable ( self . handle , offset) }
26002613 }
26012614
2602- /// Check if the offset is backed by the original file and not added after the fact.
2603- ///
2604- /// NOTE: If operating within a [`Workflow`], consider using [`AnalysisContext::is_offset_backed_by_file`].
26052615 fn offset_backed_by_file ( & self , offset : u64 ) -> bool {
26062616 unsafe { BNIsOffsetBackedByFile ( self . handle , offset) }
26072617 }
26082618
2609- /// Get the next valid offset after the provided `offset`, useful if you need to iterate over all
2610- /// readable offsets in the view.
2611- ///
2612- /// NOTE: If operating within a [`Workflow`], consider using [`AnalysisContext::next_valid_offset`].
26132619 fn next_valid_offset_after ( & self , offset : u64 ) -> u64 {
26142620 unsafe { BNGetNextValidOffset ( self . handle , offset) }
26152621 }
@@ -2618,16 +2624,10 @@ impl BinaryViewBase for BinaryView {
26182624 unsafe { BNGetModification ( self . handle , offset) }
26192625 }
26202626
2621- /// The lowest address in the view.
2622- ///
2623- /// NOTE: If operating within a [`Workflow`], consider using [`AnalysisContext::start`].
26242627 fn start ( & self ) -> u64 {
26252628 unsafe { BNGetStartOffset ( self . handle ) }
26262629 }
26272630
2628- /// The length of the view, lowest to highest address.
2629- ///
2630- /// NOTE: If operating within a [`Workflow`], consider using [`AnalysisContext::length`].
26312631 fn len ( & self ) -> u64 {
26322632 unsafe { BNGetViewLength ( self . handle ) }
26332633 }
0 commit comments