logo
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2021-2022 Vector 35 Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use binaryninjacore_sys::*;

use std::ptr;
use std::slice;

pub struct DataBuffer(*mut BNDataBuffer);

impl DataBuffer {
    pub(crate) fn from_raw(raw: *mut BNDataBuffer) -> Self {
        DataBuffer(raw)
    }

    pub fn get_data(&self) -> &[u8] {
        if self.0 == ptr::null_mut() {
            // TODO : Change the default value and remove this
            return &[];
        }
        let buffer = unsafe { BNGetDataBufferContents(self.0) };
        if buffer.is_null() {
            &[]
        } else {
            unsafe { slice::from_raw_parts(buffer as *const _, self.len()) }
        }
    }

    pub fn len(&self) -> usize {
        unsafe { BNGetDataBufferLength(self.0) }
    }

    // pub fn new(data: ?, len: usize) -> Result<Self> {
    //   let read_buffer = unsafe { BNCreateDataBuffer(data, len) };
    //   if read_buffer.is_null() {
    //     Err(())
    //   } else {
    //     Ok(DataBuffer::from_raw(read_buffer))
    //   }
    // }
}

// TODO : delete this
impl Default for DataBuffer {
    fn default() -> Self {
        DataBuffer::from_raw(ptr::null_mut())
    }
}

impl Drop for DataBuffer {
    fn drop(&mut self) {
        if self.0 != ptr::null_mut() {
            unsafe {
                BNFreeDataBuffer(self.0);
            }
        }
    }
}

impl Clone for DataBuffer {
    fn clone(&self) -> Self {
        Self::from_raw(unsafe { BNDuplicateDataBuffer(self.0) })
    }
}