summaryrefslogtreecommitdiff
path: root/rust/src/string.rs
blob: 1b2e0913c88c869896c13a0417e7c70b56d91a00 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::fmt;
use std::borrow::Borrow;
use std::ops::Deref;
use std::ffi::{CStr, CString};
use std::os::raw;
use std::mem;

use crate::rc::*;

#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[repr(C)]
pub struct BnStr {
    raw: [u8]
}

impl BnStr {
    pub(crate) unsafe fn from_raw<'a>(ptr: *const raw::c_char) -> &'a Self {
        mem::transmute(CStr::from_ptr(ptr).to_bytes_with_nul())
    }

    pub fn as_str(&self) -> &str {
       self.as_cstr().to_str().unwrap()
    }

    pub fn as_cstr(&self) -> &CStr {
        unsafe { CStr::from_bytes_with_nul_unchecked(&self.raw) }
    }
}

impl Deref for BnStr {
    type Target = str;

    fn deref(&self) -> &str {
        self.as_str()
    }
}

impl AsRef<[u8]> for BnStr {
    fn as_ref(&self) -> &[u8] {
        &self.raw
    }
}

impl AsRef<str> for BnStr {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl Borrow<str> for BnStr {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Display for BnStr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.as_cstr().to_string_lossy())
    }
}

#[repr(C)]
pub struct BnString {
    raw: *mut raw::c_char,
}

/// A nul-terminated C string allocated by the core.
/// 
/// Received from a variety of core function calls, and
/// must be used when giving strings to the core from many
/// core-invoked callbacks.
impl BnString {
    pub fn new<S: BnStrCompatible>(s: S) -> Self {
        use binaryninjacore_sys::BNAllocString;

        let raw = s.as_bytes_with_nul();

        unsafe {
            let ptr = raw.as_ref().as_ptr() as *mut _;

            Self { raw: BNAllocString(ptr) }
        }
    }

    pub(crate) unsafe fn from_raw(raw: *mut raw::c_char) -> Self {
        Self { raw }
    }

    pub(crate) fn into_raw(self) -> *mut raw::c_char {
        let res = self.raw;

        // we're surrendering ownership over the *mut c_char to
        // the core, so ensure we don't free it
        mem::forget(self);

        res
    }
}

impl Drop for BnString {
    fn drop(&mut self) {
        use binaryninjacore_sys::BNFreeString;

        unsafe {
            BNFreeString(self.raw);
        }
    }
}

impl Deref for BnString {
    type Target = BnStr;

    fn deref(&self) -> &BnStr {
        unsafe { BnStr::from_raw(self.raw) }
    }
}

impl AsRef<[u8]> for BnString {
    fn as_ref(&self) -> &[u8] {
        self.as_cstr().to_bytes_with_nul()
    }
}

impl fmt::Display for BnString {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.as_cstr().to_string_lossy())
    }
}

unsafe impl CoreOwnedArrayProvider for BnString {
    type Raw = *mut raw::c_char;
    type Context = ();

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

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

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

pub unsafe trait BnStrCompatible {
    type Result: AsRef<[u8]>;
    fn as_bytes_with_nul(self) -> Self::Result;
}

unsafe impl<'a> BnStrCompatible for &'a BnStr {
    type Result = &'a [u8];

    fn as_bytes_with_nul(self) -> Self::Result {
        self.as_cstr().to_bytes_with_nul()
    }
}

unsafe impl BnStrCompatible for BnString {
    type Result = Self;

    fn as_bytes_with_nul(self) -> Self::Result {
        self
    }
}

unsafe impl<'a> BnStrCompatible for &'a CStr {
    type Result = &'a [u8];

    fn as_bytes_with_nul(self) -> Self::Result {
        self.to_bytes_with_nul()
    }
}

unsafe impl BnStrCompatible for CString {
    type Result = Vec<u8>;

    fn as_bytes_with_nul(self) -> Self::Result {
        self.into_bytes_with_nul()
    }
}

unsafe impl<'a> BnStrCompatible for &'a str {
    type Result = Vec<u8>;

    fn as_bytes_with_nul(self) -> Self::Result {
        let ret = CString::new(self).expect("can't pass strings with internal nul bytes to core!");
        ret.into_bytes_with_nul()
    }
}

unsafe impl BnStrCompatible for String {
    type Result = Vec<u8>;

    fn as_bytes_with_nul(self) -> Self::Result {
        let ret = CString::new(self).expect("can't pass strings with internal nul bytes to core!");
        ret.into_bytes_with_nul()
    }
}