use crate::architecture::CoreArchitecture;
use crate::function::Function;
use crate::rc::{CoreArrayProvider, CoreArrayProviderInner, Guard, Ref};
use binaryninjacore_sys::{BNFreeCodeReferences, BNFreeDataReferences, BNReferenceSource};
use std::mem::ManuallyDrop;
#[derive(Debug)]
pub struct CodeReference {
arch: CoreArchitecture,
func: ManuallyDrop<Ref<Function>>,
pub address: u64,
}
pub struct DataReference {
pub address: u64,
}
impl CodeReference {
pub(crate) unsafe fn new(handle: &BNReferenceSource) -> Self {
let func = ManuallyDrop::new(Function::from_raw(handle.func));
let arch = CoreArchitecture::from_raw(handle.arch);
let address = handle.addr;
Self {
func,
arch,
address,
}
}
}
impl<'a> CodeReference {
pub fn function(&'a self) -> &'a Function {
self.func.as_ref()
}
pub fn architecture(&self) -> CoreArchitecture {
self.arch
}
}
impl CoreArrayProvider for CodeReference {
type Raw = BNReferenceSource;
type Context = ();
type Wrapped<'a> = Guard<'a, CodeReference>;
}
unsafe impl CoreArrayProviderInner for CodeReference {
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
BNFreeCodeReferences(raw, count)
}
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(CodeReference::new(raw), &())
}
}
impl CoreArrayProvider for DataReference {
type Raw = u64;
type Context = ();
type Wrapped<'a> = DataReference;
}
unsafe impl CoreArrayProviderInner for DataReference {
unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
BNFreeDataReferences(raw)
}
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
DataReference { address: *raw }
}
}