binaryninja/repository/
plugin.rs

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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable};
use crate::repository::{PluginStatus, PluginType};
use crate::string::BnString;
use crate::VersionInfo;
use binaryninjacore_sys::*;
use std::ffi::c_char;
use std::fmt::Debug;
use std::path::PathBuf;
use std::ptr::NonNull;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

#[repr(transparent)]
pub struct RepositoryPlugin {
    handle: NonNull<BNRepoPlugin>,
}

impl RepositoryPlugin {
    pub(crate) unsafe fn from_raw(handle: NonNull<BNRepoPlugin>) -> Self {
        Self { handle }
    }

    pub(crate) unsafe fn ref_from_raw(handle: NonNull<BNRepoPlugin>) -> Ref<Self> {
        Ref::new(Self { handle })
    }

    /// String indicating the API used by the plugin
    pub fn apis(&self) -> Array<BnString> {
        let mut count = 0;
        let result = unsafe { BNPluginGetApis(self.handle.as_ptr(), &mut count) };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// String of the plugin author
    pub fn author(&self) -> String {
        let result = unsafe { BNPluginGetAuthor(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::into_string(result as *mut c_char) }
    }

    /// String short description of the plugin
    pub fn description(&self) -> String {
        let result = unsafe { BNPluginGetDescription(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::into_string(result as *mut c_char) }
    }

    /// String complete license text for the given plugin
    pub fn license_text(&self) -> String {
        let result = unsafe { BNPluginGetLicenseText(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::into_string(result as *mut c_char) }
    }

    /// String long description of the plugin
    pub fn long_description(&self) -> String {
        let result = unsafe { BNPluginGetLongdescription(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::into_string(result as *mut c_char) }
    }

    /// Minimum version info the plugin was tested on
    pub fn minimum_version_info(&self) -> VersionInfo {
        let result = unsafe { BNPluginGetMinimumVersionInfo(self.handle.as_ptr()) };
        VersionInfo::from_owned_raw(result)
    }

    /// Maximum version info the plugin will support
    pub fn maximum_version_info(&self) -> VersionInfo {
        let result = unsafe { BNPluginGetMaximumVersionInfo(self.handle.as_ptr()) };
        VersionInfo::from_owned_raw(result)
    }

    /// String plugin name
    pub fn name(&self) -> String {
        let result = unsafe { BNPluginGetName(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::into_string(result as *mut c_char) }
    }

    /// String URL of the plugin's git repository
    pub fn project_url(&self) -> String {
        let result = unsafe { BNPluginGetProjectUrl(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::into_string(result as *mut c_char) }
    }

    /// String URL of the plugin's git repository
    pub fn package_url(&self) -> String {
        let result = unsafe { BNPluginGetPackageUrl(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::into_string(result as *mut c_char) }
    }

    /// String URL of the plugin author's url
    pub fn author_url(&self) -> String {
        let result = unsafe { BNPluginGetAuthorUrl(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::into_string(result as *mut c_char) }
    }
    /// String version of the plugin
    pub fn version(&self) -> String {
        let result = unsafe { BNPluginGetVersion(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::into_string(result as *mut c_char) }
    }

    /// String of the commit of this plugin git repository
    pub fn commit(&self) -> String {
        let result = unsafe { BNPluginGetCommit(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::into_string(result as *mut c_char) }
    }

    /// Relative path from the base of the repository to the actual plugin
    pub fn path(&self) -> PathBuf {
        let result = unsafe { BNPluginGetPath(self.handle.as_ptr()) };
        assert!(!result.is_null());
        let result_str = unsafe { BnString::into_string(result as *mut c_char) };
        PathBuf::from(result_str)
    }

    /// Optional sub-directory the plugin code lives in as a relative path from the plugin root
    pub fn subdir(&self) -> PathBuf {
        let result = unsafe { BNPluginGetSubdir(self.handle.as_ptr()) };
        assert!(!result.is_null());
        let result_str = unsafe { BnString::into_string(result as *mut c_char) };
        PathBuf::from(result_str)
    }

    /// Dependencies required for installing this plugin
    pub fn dependencies(&self) -> String {
        let result = unsafe { BNPluginGetDependencies(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::into_string(result as *mut c_char) }
    }

    /// true if the plugin is installed, false otherwise
    pub fn is_installed(&self) -> bool {
        unsafe { BNPluginIsInstalled(self.handle.as_ptr()) }
    }

    /// true if the plugin is enabled, false otherwise
    pub fn is_enabled(&self) -> bool {
        unsafe { BNPluginIsEnabled(self.handle.as_ptr()) }
    }

    pub fn status(&self) -> PluginStatus {
        unsafe { BNPluginGetPluginStatus(self.handle.as_ptr()) }
    }

    /// List of PluginType enumeration objects indicating the plugin type(s)
    pub fn types(&self) -> Array<PluginType> {
        let mut count = 0;
        let result = unsafe { BNPluginGetPluginTypes(self.handle.as_ptr(), &mut count) };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Enable this plugin, optionally trying to force it.
    /// Force loading a plugin with ignore platform and api constraints.
    pub fn enable(&self, force: bool) -> bool {
        unsafe { BNPluginEnable(self.handle.as_ptr(), force) }
    }

    pub fn disable(&self) -> bool {
        unsafe { BNPluginDisable(self.handle.as_ptr()) }
    }

    /// Attempt to install the given plugin
    pub fn install(&self) -> bool {
        unsafe { BNPluginInstall(self.handle.as_ptr()) }
    }

    pub fn install_dependencies(&self) -> bool {
        unsafe { BNPluginInstallDependencies(self.handle.as_ptr()) }
    }

    /// Attempt to uninstall the given plugin
    pub fn uninstall(&self) -> bool {
        unsafe { BNPluginUninstall(self.handle.as_ptr()) }
    }

    pub fn updated(&self) -> bool {
        unsafe { BNPluginUpdate(self.handle.as_ptr()) }
    }

    /// List of platforms this plugin can execute on
    pub fn platforms(&self) -> Array<BnString> {
        let mut count = 0;
        let result = unsafe { BNPluginGetPlatforms(self.handle.as_ptr(), &mut count) };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    pub fn repository(&self) -> String {
        let result = unsafe { BNPluginGetRepository(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::into_string(result as *mut c_char) }
    }

    /// Boolean status indicating that the plugin is being deleted
    pub fn is_being_deleted(&self) -> bool {
        unsafe { BNPluginIsBeingDeleted(self.handle.as_ptr()) }
    }

    /// Boolean status indicating that the plugin is being updated
    pub fn is_being_updated(&self) -> bool {
        unsafe { BNPluginIsBeingUpdated(self.handle.as_ptr()) }
    }

    /// Boolean status indicating that the plugin is currently running
    pub fn is_running(&self) -> bool {
        unsafe { BNPluginIsRunning(self.handle.as_ptr()) }
    }

    /// Boolean status indicating that the plugin has updates will be installed after the next restart
    pub fn is_update_pending(&self) -> bool {
        unsafe { BNPluginIsUpdatePending(self.handle.as_ptr()) }
    }

    /// Boolean status indicating that the plugin will be disabled after the next restart
    pub fn is_disable_pending(&self) -> bool {
        unsafe { BNPluginIsDisablePending(self.handle.as_ptr()) }
    }

    /// Boolean status indicating that the plugin will be deleted after the next restart
    pub fn is_delete_pending(&self) -> bool {
        unsafe { BNPluginIsDeletePending(self.handle.as_ptr()) }
    }

    /// Boolean status indicating that the plugin has updates available
    pub fn is_updated_available(&self) -> bool {
        unsafe { BNPluginIsUpdateAvailable(self.handle.as_ptr()) }
    }

    /// Boolean status indicating that the plugin's dependencies are currently being installed
    pub fn are_dependencies_being_installed(&self) -> bool {
        unsafe { BNPluginAreDependenciesBeingInstalled(self.handle.as_ptr()) }
    }

    /// Gets a json object of the project data field
    pub fn project_data(&self) -> String {
        let result = unsafe { BNPluginGetProjectData(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::into_string(result) }
    }

    /// Returns a datetime object representing the plugins last update
    pub fn last_update(&self) -> SystemTime {
        let result = unsafe { BNPluginGetLastUpdate(self.handle.as_ptr()) };
        UNIX_EPOCH + Duration::from_secs(result)
    }
}

impl Debug for RepositoryPlugin {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RepositoryPlugin")
            .field("name", &self.name())
            .field("version", &self.version())
            .field("author", &self.author())
            .field("description", &self.description())
            .field("minimum_version_info", &self.minimum_version_info())
            .field("maximum_version_info", &self.maximum_version_info())
            .field("last_update", &self.last_update())
            .field("status", &self.status())
            .finish()
    }
}

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

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

unsafe impl RefCountable for RepositoryPlugin {
    unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
        Self::ref_from_raw(NonNull::new(BNNewPluginReference(handle.handle.as_ptr())).unwrap())
    }

    unsafe fn dec_ref(handle: &Self) {
        BNFreePlugin(handle.handle.as_ptr())
    }
}

impl CoreArrayProvider for RepositoryPlugin {
    type Raw = *mut BNRepoPlugin;
    type Context = ();
    type Wrapped<'a> = Guard<'a, Self>;
}

unsafe impl CoreArrayProviderInner for RepositoryPlugin {
    unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
        BNFreeRepositoryPluginList(raw)
    }

    unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
        Guard::new(Self::from_raw(NonNull::new(*raw).unwrap()), context)
    }
}