diff options
| author | KyleMiles <krm504@nyu.edu> | 2021-01-21 18:35:20 +0000 |
|---|---|---|
| committer | KyleMiles <krm504@nyu.edu> | 2021-01-21 19:07:07 +0000 |
| commit | 2fcacc55d5466a7d17bdc0b3ce988772aa6d0653 (patch) | |
| tree | 9d0f2ba19117843575936f2b93e0b6a578a84dc8 /rust/src/databuffer.rs | |
| parent | a0da07c5c2860a5bd69921d38e438bbc1d43912f (diff) | |
cargo fmt and all my changes
Diffstat (limited to 'rust/src/databuffer.rs')
| -rw-r--r-- | rust/src/databuffer.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/rust/src/databuffer.rs b/rust/src/databuffer.rs new file mode 100644 index 00000000..d660f570 --- /dev/null +++ b/rust/src/databuffer.rs @@ -0,0 +1,71 @@ +// Copyright 2021 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; + +// TODO : DataBuffers are RefCounted objects, this needs to be changed to only return Refs to DataBuffers + +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); + } + } + } +} |
