summaryrefslogtreecommitdiff
path: root/rust/src/types.rs
blob: 80046f871df91219cba5f22254c3cd28cfcdcad8 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::mem;
use std::slice;
use binaryninjacore_sys::*;

use crate::rc::*;

#[derive(PartialEq, Eq, Hash)]
pub struct Type {
    pub(crate) handle: *mut BNType,
}

unsafe impl Send for Type {}
unsafe impl Sync for Type {}

impl Type {
    pub(crate) unsafe fn from_raw(handle: *mut BNType) -> Self {
        debug_assert!(!handle.is_null());

        Self { handle }
    }
}

impl ToOwned for Type {
    type Owned = Ref<Self>;

    fn to_owned(&self) -> Self::Owned {
        unsafe { RefCountable::inc_ref(self) }
    }
}

unsafe impl RefCountable for Type {
    unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
        Ref::new(Self {
            handle: BNNewTypeReference(handle.handle),
        })
    }

    unsafe fn dec_ref(handle: &Self) {
        BNFreeType(handle.handle);
    }
}

#[repr(C)]
pub struct QualifiedName {
    object: BNQualifiedName,
}

impl QualifiedName {
    pub fn string(&self) -> String {
        use std::ffi::CStr;

        unsafe {
            slice::from_raw_parts(self.object.name, self.object.nameCount)
                .iter()
                .map(|c| CStr::from_ptr(*c).to_string_lossy())
                .collect::<Vec<_>>()
                .join("::")
        }
    }
}

impl Drop for QualifiedName {
    fn drop(&mut self) {
        unsafe { BNFreeQualifiedName(&mut self.object); }
    }
}

#[repr(C)]
pub struct QualifiedNameAndType {
    object: BNQualifiedNameAndType,
}

impl QualifiedNameAndType {
    pub fn name(&self) -> &QualifiedName {
        unsafe {
            mem::transmute(&self.object.name)
        }
    }

    pub fn type_object(&self) -> Guard<Type> {
        unsafe {
            Guard::new(Type::from_raw(self.object.type_), self)
        }
    }
}

impl Drop for QualifiedNameAndType {
    fn drop(&mut self) {
        unsafe { BNFreeQualifiedNameAndType(&mut self.object); }
    }
}

unsafe impl CoreOwnedArrayProvider for QualifiedNameAndType {
    type Raw = BNQualifiedNameAndType;
    type Context = ();

    unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
        BNFreeTypeList(raw, count);
    }
}

unsafe impl<'a> CoreOwnedArrayWrapper<'a> for QualifiedNameAndType {
    type Wrapped = &'a QualifiedNameAndType;

    unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
        mem::transmute(raw)
    }
}