binaryninja/repository/
plugin.rs1use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable};
2use crate::repository::{PluginStatus, PluginType};
3use crate::string::{raw_to_string, BnString, IntoCStr};
4use crate::VersionInfo;
5use binaryninjacore_sys::*;
6use std::ffi::c_char;
7use std::fmt::Debug;
8use std::path::PathBuf;
9use std::ptr::NonNull;
10use std::slice;
11
12#[derive(Clone, Debug, PartialEq, Eq)]
13pub struct ExtensionVersionPlatform {
14 pub name: String,
15 pub download_url: String,
16 pub untracked_download_url: String,
17}
18
19impl ExtensionVersionPlatform {
20 pub(crate) fn from_raw(value: &BNPluginVersionPlatform) -> Self {
21 Self {
22 name: raw_to_string(value.name as *mut _).unwrap_or_default(),
23 download_url: raw_to_string(value.downloadUrl as *mut _).unwrap_or_default(),
24 untracked_download_url: raw_to_string(value.untrackedDownloadUrl as *mut _)
25 .unwrap_or_default(),
26 }
27 }
28}
29
30#[derive(Clone, Debug, PartialEq, Eq)]
31pub struct ExtensionVersion {
32 pub id: String,
33 pub version: String,
34 pub long_description: String,
35 pub changelog: String,
36 pub minimum_client_version: u64,
37 pub platforms: Vec<ExtensionVersionPlatform>,
38 pub created: String,
39}
40
41impl ExtensionVersion {
42 pub(crate) fn from_raw(value: &BNPluginVersion) -> Self {
43 let platforms = if value.platforms.is_null() || value.platformCount == 0 {
44 Vec::new()
45 } else {
46 unsafe { slice::from_raw_parts(value.platforms, value.platformCount) }
47 .iter()
48 .map(ExtensionVersionPlatform::from_raw)
49 .collect()
50 };
51
52 Self {
53 id: raw_to_string(value.id as *mut _).unwrap_or_default(),
54 version: raw_to_string(value.versionString as *mut _).unwrap_or_default(),
55 long_description: raw_to_string(value.longDescription as *mut _).unwrap_or_default(),
56 changelog: raw_to_string(value.changelog as *mut _).unwrap_or_default(),
57 minimum_client_version: value.minimumClientVersion,
58 platforms,
59 created: raw_to_string(value.created as *mut _).unwrap_or_default(),
60 }
61 }
62
63 pub(crate) fn from_owned_raw(value: BNPluginVersion) -> Self {
64 let owned = Self::from_raw(&value);
65 unsafe { BNPluginFreeVersion(value) };
66 owned
67 }
68}
69
70#[repr(transparent)]
71pub struct Extension {
72 handle: NonNull<BNPlugin>,
73}
74
75impl Extension {
76 pub(crate) unsafe fn from_raw(handle: NonNull<BNPlugin>) -> Self {
77 Self { handle }
78 }
79
80 pub(crate) unsafe fn ref_from_raw(handle: NonNull<BNPlugin>) -> Ref<Self> {
81 Ref::new(Self { handle })
82 }
83
84 pub fn apis(&self) -> Array<BnString> {
86 let mut count = 0;
87 let result = unsafe { BNPluginGetApis(self.handle.as_ptr(), &mut count) };
88 assert!(!result.is_null());
89 unsafe { Array::new(result, count, ()) }
90 }
91
92 pub fn author(&self) -> String {
94 let result = unsafe { BNPluginGetAuthor(self.handle.as_ptr()) };
95 assert!(!result.is_null());
96 unsafe { BnString::into_string(result as *mut c_char) }
97 }
98
99 pub fn description(&self) -> String {
101 let result = unsafe { BNPluginGetDescription(self.handle.as_ptr()) };
102 assert!(!result.is_null());
103 unsafe { BnString::into_string(result as *mut c_char) }
104 }
105
106 pub fn license_text(&self) -> String {
108 let result = unsafe { BNPluginGetLicenseText(self.handle.as_ptr()) };
109 assert!(!result.is_null());
110 unsafe { BnString::into_string(result as *mut c_char) }
111 }
112
113 pub fn minimum_version_info(&self) -> VersionInfo {
115 let result = unsafe { BNPluginGetMinimumVersionInfo(self.handle.as_ptr()) };
116 VersionInfo::from_owned_raw(result)
117 }
118
119 pub fn maximum_version_info(&self) -> VersionInfo {
121 let result = unsafe { BNPluginGetMaximumVersionInfo(self.handle.as_ptr()) };
122 VersionInfo::from_owned_raw(result)
123 }
124
125 pub fn versions(&self) -> Array<ExtensionVersion> {
127 let mut count = 0;
128 let result = unsafe { BNPluginGetVersions(self.handle.as_ptr(), &mut count) };
129 assert!(!result.is_null());
130 unsafe { Array::new(result, count, ()) }
131 }
132
133 pub fn current_version(&self) -> ExtensionVersion {
135 let result = unsafe { BNPluginGetCurrentVersion(self.handle.as_ptr()) };
136 ExtensionVersion::from_owned_raw(result)
137 }
138
139 pub fn name(&self) -> String {
141 let result = unsafe { BNPluginGetName(self.handle.as_ptr()) };
142 assert!(!result.is_null());
143 unsafe { BnString::into_string(result as *mut c_char) }
144 }
145
146 pub fn project_url(&self) -> String {
148 let result = unsafe { BNPluginGetProjectUrl(self.handle.as_ptr()) };
149 assert!(!result.is_null());
150 unsafe { BnString::into_string(result as *mut c_char) }
151 }
152
153 pub fn package_url(&self) -> String {
155 let result = unsafe { BNPluginGetPackageUrl(self.handle.as_ptr()) };
156 assert!(!result.is_null());
157 unsafe { BnString::into_string(result as *mut c_char) }
158 }
159
160 pub fn is_paid(&self) -> bool {
162 unsafe { BNPluginGetIsPaid(self.handle.as_ptr()) }
163 }
164
165 pub fn author_url(&self) -> String {
167 let result = unsafe { BNPluginGetAuthorUrl(self.handle.as_ptr()) };
168 assert!(!result.is_null());
169 unsafe { BnString::into_string(result as *mut c_char) }
170 }
171
172 pub fn commit(&self) -> String {
174 let result = unsafe { BNPluginGetCommit(self.handle.as_ptr()) };
175 assert!(!result.is_null());
176 unsafe { BnString::into_string(result as *mut c_char) }
177 }
178
179 pub fn path(&self) -> PathBuf {
181 let result = unsafe { BNPluginGetPath(self.handle.as_ptr()) };
182 assert!(!result.is_null());
183 let result_str = unsafe { BnString::into_string(result as *mut c_char) };
184 PathBuf::from(result_str)
185 }
186
187 pub fn subdir(&self) -> PathBuf {
189 let result = unsafe { BNPluginGetSubdir(self.handle.as_ptr()) };
190 assert!(!result.is_null());
191 let result_str = unsafe { BnString::into_string(result as *mut c_char) };
192 PathBuf::from(result_str)
193 }
194
195 pub fn dependencies(&self) -> String {
197 let result = unsafe { BNPluginGetDependencies(self.handle.as_ptr()) };
198 assert!(!result.is_null());
199 unsafe { BnString::into_string(result as *mut c_char) }
200 }
201
202 pub fn is_installed(&self) -> bool {
204 unsafe { BNPluginIsInstalled(self.handle.as_ptr()) }
205 }
206
207 pub fn is_enabled(&self) -> bool {
209 unsafe { BNPluginIsEnabled(self.handle.as_ptr()) }
210 }
211
212 pub fn status(&self) -> PluginStatus {
213 unsafe { BNPluginGetPluginStatus(self.handle.as_ptr()) }
214 }
215
216 pub fn types(&self) -> Array<PluginType> {
218 let mut count = 0;
219 let result = unsafe { BNPluginGetPluginTypes(self.handle.as_ptr(), &mut count) };
220 assert!(!result.is_null());
221 unsafe { Array::new(result, count, ()) }
222 }
223
224 pub fn enable(&self, force: bool) -> bool {
227 unsafe { BNPluginEnable(self.handle.as_ptr(), force) }
228 }
229
230 pub fn disable(&self) -> bool {
231 unsafe { BNPluginDisable(self.handle.as_ptr()) }
232 }
233
234 pub fn install(&self, version_id: &str) -> bool {
236 let version_id_raw = version_id.to_cstr();
237 unsafe { BNPluginInstall(self.handle.as_ptr(), version_id_raw.as_ptr()) }
238 }
239
240 pub fn install_dependencies(&self) -> bool {
241 unsafe { BNPluginInstallDependencies(self.handle.as_ptr()) }
242 }
243
244 pub fn uninstall(&self) -> bool {
246 unsafe { BNPluginUninstall(self.handle.as_ptr()) }
247 }
248
249 pub fn updated(&self, version_id: &str) -> bool {
250 let version_id_raw = version_id.to_cstr();
251 unsafe { BNPluginUpdate(self.handle.as_ptr(), version_id_raw.as_ptr()) }
252 }
253
254 pub fn platforms(&self) -> Array<BnString> {
256 let mut count = 0;
257 let result = unsafe { BNPluginGetPlatforms(self.handle.as_ptr(), &mut count) };
258 assert!(!result.is_null());
259 unsafe { Array::new(result, count, ()) }
260 }
261
262 pub fn repository(&self) -> String {
263 let result = unsafe { BNPluginGetRepository(self.handle.as_ptr()) };
264 assert!(!result.is_null());
265 unsafe { BnString::into_string(result as *mut c_char) }
266 }
267
268 pub fn is_being_deleted(&self) -> bool {
270 unsafe { BNPluginIsBeingDeleted(self.handle.as_ptr()) }
271 }
272
273 pub fn is_being_updated(&self) -> bool {
275 unsafe { BNPluginIsBeingUpdated(self.handle.as_ptr()) }
276 }
277
278 pub fn is_running(&self) -> bool {
280 unsafe { BNPluginIsRunning(self.handle.as_ptr()) }
281 }
282
283 pub fn is_update_pending(&self) -> bool {
285 unsafe { BNPluginIsUpdatePending(self.handle.as_ptr()) }
286 }
287
288 pub fn is_disable_pending(&self) -> bool {
290 unsafe { BNPluginIsDisablePending(self.handle.as_ptr()) }
291 }
292
293 pub fn is_delete_pending(&self) -> bool {
295 unsafe { BNPluginIsDeletePending(self.handle.as_ptr()) }
296 }
297
298 pub fn is_updated_available(&self) -> bool {
300 unsafe { BNPluginIsUpdateAvailable(self.handle.as_ptr()) }
301 }
302
303 pub fn are_dependencies_being_installed(&self) -> bool {
305 unsafe { BNPluginAreDependenciesBeingInstalled(self.handle.as_ptr()) }
306 }
307
308 pub fn project_data(&self) -> String {
310 let result = unsafe { BNPluginGetProjectData(self.handle.as_ptr()) };
311 assert!(!result.is_null());
312 unsafe { BnString::into_string(result) }
313 }
314}
315
316impl Debug for Extension {
317 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
318 f.debug_struct("Extension")
319 .field("name", &self.name())
320 .field("author", &self.author())
321 .field("description", &self.description())
322 .field("minimum_version_info", &self.minimum_version_info())
323 .field("maximum_version_info", &self.maximum_version_info())
324 .field("status", &self.status())
325 .finish()
326 }
327}
328
329impl ToOwned for Extension {
330 type Owned = Ref<Self>;
331
332 fn to_owned(&self) -> Self::Owned {
333 unsafe { RefCountable::inc_ref(self) }
334 }
335}
336
337unsafe impl RefCountable for Extension {
338 unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
339 Self::ref_from_raw(NonNull::new(BNNewPluginReference(handle.handle.as_ptr())).unwrap())
340 }
341
342 unsafe fn dec_ref(handle: &Self) {
343 BNFreePlugin(handle.handle.as_ptr())
344 }
345}
346
347impl CoreArrayProvider for Extension {
348 type Raw = *mut BNPlugin;
349 type Context = ();
350 type Wrapped<'a> = Guard<'a, Self>;
351}
352
353unsafe impl CoreArrayProviderInner for Extension {
354 unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
355 BNFreeRepositoryPluginList(raw)
356 }
357
358 unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
359 Guard::new(Self::from_raw(NonNull::new(*raw).unwrap()), context)
360 }
361}
362
363impl CoreArrayProvider for ExtensionVersion {
364 type Raw = BNPluginVersion;
365 type Context = ();
366 type Wrapped<'a> = Self;
367}
368
369unsafe impl CoreArrayProviderInner for ExtensionVersion {
370 unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
371 BNFreePluginVersions(raw, count)
372 }
373
374 unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
375 ExtensionVersion::from_raw(raw)
376 }
377}