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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
|
use std::ptr::{null_mut, NonNull};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::{ffi, mem};
use binaryninjacore_sys::*;
use crate::metadata::Metadata;
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner};
use crate::string::{BnStrCompatible, BnString};
#[repr(C)]
pub struct Project {
handle: NonNull<BNProject>,
}
impl Project {
pub(crate) unsafe fn from_raw(handle: NonNull<BNProject>) -> Self {
Project { handle }
}
pub(crate) unsafe fn ref_from_raw(handle: &*mut BNProject) -> &Self {
debug_assert!(!handle.is_null());
mem::transmute(handle)
}
#[allow(clippy::mut_from_ref)]
pub(crate) unsafe fn as_raw(&self) -> &mut BNProject {
&mut *self.handle.as_ptr()
}
pub fn all_open() -> Array<Project> {
let mut count = 0;
let result = unsafe { BNGetOpenProjects(&mut count) };
assert!(!result.is_null());
unsafe { Array::new(result, count, ()) }
}
/// Create a new project
///
/// * `path` - Path to the project directory (.bnpr)
/// * `name` - Name of the new project
pub fn create<P: BnStrCompatible, S: BnStrCompatible>(path: P, name: S) -> Self {
let path_raw = path.into_bytes_with_nul();
let name_raw = name.into_bytes_with_nul();
let handle = unsafe {
BNCreateProject(
path_raw.as_ref().as_ptr() as *const ffi::c_char,
name_raw.as_ref().as_ptr() as *const ffi::c_char,
)
};
unsafe { Self::from_raw(NonNull::new(handle).unwrap()) }
}
/// Open an existing project
///
/// * `path` - Path to the project directory (.bnpr) or project metadata file (.bnpm)
pub fn open_file<P: BnStrCompatible>(path: P) -> Self {
let path_raw = path.into_bytes_with_nul();
let handle = unsafe { BNOpenProject(path_raw.as_ref().as_ptr() as *const ffi::c_char) };
unsafe { Self::from_raw(NonNull::new(handle).unwrap()) }
}
/// Check if the project is currently open
pub fn is_open(&self) -> bool {
unsafe { BNProjectIsOpen(self.as_raw()) }
}
/// Open a closed project
pub fn open(&self) -> Result<(), ()> {
if unsafe { BNProjectOpen(self.as_raw()) } {
Ok(())
} else {
Err(())
}
}
/// Close a open project
pub fn close(&self) -> Result<(), ()> {
if unsafe { BNProjectClose(self.as_raw()) } {
Ok(())
} else {
Err(())
}
}
/// Get the unique id of this project
pub fn id(&self) -> BnString {
unsafe { BnString::from_raw(BNProjectGetId(self.as_raw())) }
}
/// Get the path of the project
pub fn path(&self) -> BnString {
unsafe { BnString::from_raw(BNProjectGetPath(self.as_raw())) }
}
/// Get the name of the project
pub fn name(&self) -> BnString {
unsafe { BnString::from_raw(BNProjectGetName(self.as_raw())) }
}
/// Set the name of the project
pub fn set_name<S: BnStrCompatible>(&self, value: S) {
let value = value.into_bytes_with_nul();
unsafe { BNProjectSetName(self.as_raw(), value.as_ref().as_ptr() as *const ffi::c_char) }
}
/// Get the description of the project
pub fn description(&self) -> BnString {
unsafe { BnString::from_raw(BNProjectGetDescription(self.as_raw())) }
}
/// Set the description of the project
pub fn set_desription<S: BnStrCompatible>(&self, value: S) {
let value = value.into_bytes_with_nul();
unsafe {
BNProjectSetDescription(self.as_raw(), value.as_ref().as_ptr() as *const ffi::c_char)
}
}
/// Retrieves metadata stored under a key from the project
pub fn query_metadata<S: BnStrCompatible>(&self, key: S) -> Metadata {
let key = key.into_bytes_with_nul();
let result = unsafe {
BNProjectQueryMetadata(self.as_raw(), key.as_ref().as_ptr() as *const ffi::c_char)
};
unsafe { Metadata::from_raw(result) }
}
/// Stores metadata within the project
///
/// * `key` - Key under which to store the Metadata object
/// * `value` - Object to store
pub fn store_metadata<S: BnStrCompatible>(&self, key: S, value: &Metadata) -> bool {
let key_raw = key.into_bytes_with_nul();
unsafe {
BNProjectStoreMetadata(
self.as_raw(),
key_raw.as_ref().as_ptr() as *const ffi::c_char,
value.handle,
)
}
}
/// Removes the metadata associated with this `key` from the project
pub fn remove_metadata<S: BnStrCompatible>(&self, key: S) {
let key_raw = key.into_bytes_with_nul();
unsafe {
BNProjectRemoveMetadata(
self.as_raw(),
key_raw.as_ref().as_ptr() as *const ffi::c_char,
)
}
}
pub fn push_folder(&self, file: &ProjectFolder) {
unsafe { BNProjectPushFolder(self.as_raw(), file.as_raw()) }
}
/// Recursively create files and folders in the project from a path on disk
///
/// * `path` - Path to folder on disk
/// * `parent` - Parent folder in the project that will contain the new contents
/// * `description` - Description for created root folder
pub fn create_folder_from_path<P, D>(
&self,
path: P,
parent: Option<&ProjectFolder>,
description: D,
) -> Result<ProjectFolder, ()>
where
P: BnStrCompatible,
D: BnStrCompatible,
{
let path_raw = path.into_bytes_with_nul();
let description_raw = description.into_bytes_with_nul();
let parent_ptr = parent
.map(|p| unsafe { p.as_raw() as *mut _ })
.unwrap_or(null_mut());
unsafe {
let result = BNProjectCreateFolderFromPath(
self.as_raw(),
path_raw.as_ref().as_ptr() as *const ffi::c_char,
parent_ptr,
description_raw.as_ref().as_ptr() as *const ffi::c_char,
null_mut(),
Some(cb_progress_func_nop),
);
Ok(ProjectFolder::from_raw(NonNull::new(result).ok_or(())?))
}
}
/// Recursively create files and folders in the project from a path on disk
///
/// * `path` - Path to folder on disk
/// * `parent` - Parent folder in the project that will contain the new contents
/// * `description` - Description for created root folder
/// * `progress_func` - Progress function that will be called
pub fn create_folder_from_path_with_progress<P, D, F>(
&self,
path: P,
parent: Option<&ProjectFolder>,
description: D,
mut progress_func: F,
) -> Result<ProjectFolder, ()>
where
P: BnStrCompatible,
D: BnStrCompatible,
F: FnMut(usize, usize) -> bool,
{
let path_raw = path.into_bytes_with_nul();
let description_raw = description.into_bytes_with_nul();
let parent_ptr = parent
.map(|p| unsafe { p.as_raw() as *mut _ })
.unwrap_or(null_mut());
let progress_ctx = &mut progress_func as *mut F as *mut ffi::c_void;
unsafe {
let result = BNProjectCreateFolderFromPath(
self.as_raw(),
path_raw.as_ref().as_ptr() as *const ffi::c_char,
parent_ptr,
description_raw.as_ref().as_ptr() as *const ffi::c_char,
progress_ctx,
Some(cb_progress_func::<F>),
);
Ok(ProjectFolder::from_raw(NonNull::new(result).ok_or(())?))
}
}
/// Recursively create files and folders in the project from a path on disk
///
/// * `parent` - Parent folder in the project that will contain the new folder
/// * `name` - Name for the created folder
/// * `description` - Description for created folder
pub fn create_folder<N, D>(
&self,
parent: Option<&ProjectFolder>,
name: N,
description: D,
) -> Result<ProjectFolder, ()>
where
N: BnStrCompatible,
D: BnStrCompatible,
{
let name_raw = name.into_bytes_with_nul();
let description_raw = description.into_bytes_with_nul();
let parent_ptr = parent
.map(|p| unsafe { p.as_raw() as *mut _ })
.unwrap_or(null_mut());
unsafe {
let result = BNProjectCreateFolder(
self.as_raw(),
parent_ptr,
name_raw.as_ref().as_ptr() as *const ffi::c_char,
description_raw.as_ref().as_ptr() as *const ffi::c_char,
);
Ok(ProjectFolder::from_raw(NonNull::new(result).ok_or(())?))
}
}
/// Recursively create files and folders in the project from a path on disk
///
/// * `parent` - Parent folder in the project that will contain the new folder
/// * `name` - Name for the created folder
/// * `description` - Description for created folder
/// * `id` - id unique ID
pub unsafe fn create_folder_unsafe<N, D, I>(
&self,
parent: Option<&ProjectFolder>,
name: N,
description: D,
id: I,
) -> Result<ProjectFolder, ()>
where
N: BnStrCompatible,
D: BnStrCompatible,
I: BnStrCompatible,
{
let name_raw = name.into_bytes_with_nul();
let description_raw = description.into_bytes_with_nul();
let parent_ptr = parent
.map(|p| unsafe { p.as_raw() as *mut _ })
.unwrap_or(null_mut());
let id_raw = id.into_bytes_with_nul();
unsafe {
let result = BNProjectCreateFolderUnsafe(
self.as_raw(),
parent_ptr,
name_raw.as_ref().as_ptr() as *const ffi::c_char,
description_raw.as_ref().as_ptr() as *const ffi::c_char,
id_raw.as_ref().as_ptr() as *const ffi::c_char,
);
Ok(ProjectFolder::from_raw(NonNull::new(result).ok_or(())?))
}
}
/// Get a list of folders in the project
pub fn folders(&self) -> Result<Array<ProjectFolder>, ()> {
let mut count = 0;
let result = unsafe { BNProjectGetFolders(self.as_raw(), &mut count) };
if result.is_null() {
return Err(());
}
Ok(unsafe { Array::new(result, count, ()) })
}
/// Retrieve a folder in the project by unique folder `id`
pub fn folder_by_id<S: BnStrCompatible>(&self, id: S) -> Option<ProjectFolder> {
let id_raw = id.into_bytes_with_nul();
let id_ptr = id_raw.as_ref().as_ptr() as *const ffi::c_char;
let result = unsafe { BNProjectGetFolderById(self.as_raw(), id_ptr) };
let handle = NonNull::new(result)?;
Some(unsafe { ProjectFolder::from_raw(handle) })
}
/// Recursively delete a folder from the project
///
/// * `folder` - Folder to delete recursively
pub fn delete_folder(&self, folder: &ProjectFolder) -> Result<(), ()> {
let result = unsafe {
BNProjectDeleteFolder(
self.as_raw(),
folder.as_raw(),
null_mut(),
Some(cb_progress_func_nop),
)
};
if result {
Ok(())
} else {
Err(())
}
}
/// Recursively delete a folder from the project
///
/// * `folder` - Folder to delete recursively
/// * `progress_func` - Progress function that will be called as objects get deleted
pub fn delete_folder_with_progress<F>(
&self,
folder: &ProjectFolder,
mut progress_func: F,
) -> Result<(), ()>
where
F: FnMut(usize, usize) -> bool,
{
let progress_ctx = &mut progress_func as *mut F as *mut ffi::c_void;
let result = unsafe {
BNProjectDeleteFolder(
self.as_raw(),
folder.as_raw(),
progress_ctx,
Some(cb_progress_func::<F>),
)
};
if result {
Ok(())
} else {
Err(())
}
}
pub fn push_file(&self, file: &ProjectFile) {
unsafe { BNProjectPushFile(self.as_raw(), file.as_raw()) }
}
/// Create a file in the project from a path on disk
///
/// * `path` - Path on disk
/// * `folder` - Folder to place the created file in
/// * `name` - Name to assign to the created file
/// * `description` - Description to assign to the created file
pub fn create_file_from_path<P, N, D>(
&self,
path: P,
folder: Option<&ProjectFolder>,
name: N,
description: D,
) -> Result<ProjectFile, ()>
where
P: BnStrCompatible,
N: BnStrCompatible,
D: BnStrCompatible,
{
let path_raw = path.into_bytes_with_nul();
let name_raw = name.into_bytes_with_nul();
let description_raw = description.into_bytes_with_nul();
unsafe {
let result = BNProjectCreateFileFromPath(
self.as_raw(),
path_raw.as_ref().as_ptr() as *const ffi::c_char,
folder.map(|x| x.as_raw() as *mut _).unwrap_or(null_mut()),
name_raw.as_ref().as_ptr() as *const ffi::c_char,
description_raw.as_ref().as_ptr() as *const ffi::c_char,
null_mut(),
Some(cb_progress_func_nop),
);
Ok(ProjectFile::from_raw(NonNull::new(result).ok_or(())?))
}
}
/// Create a file in the project from a path on disk
///
/// * `path` - Path on disk
/// * `folder` - Folder to place the created file in
/// * `name` - Name to assign to the created file
/// * `description` - Description to assign to the created file
/// * `progress_func` - Progress function that will be called as the file is being added
pub fn create_file_from_path_with_progress<P, N, D, F>(
&self,
path: P,
folder: Option<&ProjectFolder>,
name: N,
description: D,
mut progress_func: F,
) -> Result<ProjectFile, ()>
where
P: BnStrCompatible,
N: BnStrCompatible,
D: BnStrCompatible,
F: FnMut(usize, usize) -> bool,
{
let path_raw = path.into_bytes_with_nul();
let name_raw = name.into_bytes_with_nul();
let description_raw = description.into_bytes_with_nul();
let progress_ctx = &mut progress_func as *mut F as *mut ffi::c_void;
unsafe {
let result = BNProjectCreateFileFromPath(
self.as_raw(),
path_raw.as_ref().as_ptr() as *const ffi::c_char,
folder.map(|x| x.as_raw() as *mut _).unwrap_or(null_mut()),
name_raw.as_ref().as_ptr() as *const ffi::c_char,
description_raw.as_ref().as_ptr() as *const ffi::c_char,
progress_ctx,
Some(cb_progress_func::<F>),
);
Ok(ProjectFile::from_raw(NonNull::new(result).ok_or(())?))
}
}
/// Create a file in the project from a path on disk
///
/// * `path` - Path on disk
/// * `folder` - Folder to place the created file in
/// * `name` - Name to assign to the created file
/// * `description` - Description to assign to the created file
/// * `id` - id unique ID
/// * `creation_time` - Creation time of the file
pub unsafe fn create_file_from_path_unsafe<P, N, D, I>(
&self,
path: P,
folder: Option<&ProjectFolder>,
name: N,
description: D,
id: I,
creation_time: SystemTime,
) -> Result<ProjectFile, ()>
where
P: BnStrCompatible,
N: BnStrCompatible,
D: BnStrCompatible,
I: BnStrCompatible,
{
let path_raw = path.into_bytes_with_nul();
let name_raw = name.into_bytes_with_nul();
let description_raw = description.into_bytes_with_nul();
let id_raw = id.into_bytes_with_nul();
unsafe {
let result = BNProjectCreateFileFromPathUnsafe(
self.as_raw(),
path_raw.as_ref().as_ptr() as *const ffi::c_char,
folder.map(|x| x.as_raw() as *mut _).unwrap_or(null_mut()),
name_raw.as_ref().as_ptr() as *const ffi::c_char,
description_raw.as_ref().as_ptr() as *const ffi::c_char,
id_raw.as_ref().as_ptr() as *const ffi::c_char,
systime_to_bntime(creation_time).unwrap(),
null_mut(),
Some(cb_progress_func_nop),
);
Ok(ProjectFile::from_raw(NonNull::new(result).ok_or(())?))
}
}
/// Create a file in the project from a path on disk
///
/// * `path` - Path on disk
/// * `folder` - Folder to place the created file in
/// * `name` - Name to assign to the created file
/// * `description` - Description to assign to the created file
/// * `id` - id unique ID
/// * `creation_time` - Creation time of the file
/// * `progress_func` - Progress function that will be called as the file is being added
pub unsafe fn create_file_from_path_with_progress_unsafe<P, N, D, I, F>(
&self,
path: P,
folder: Option<&ProjectFolder>,
name: N,
description: D,
id: I,
creation_time: SystemTime,
mut progress_func: F,
) -> Result<ProjectFile, ()>
where
P: BnStrCompatible,
N: BnStrCompatible,
D: BnStrCompatible,
I: BnStrCompatible,
F: FnMut(usize, usize) -> bool,
{
let path_raw = path.into_bytes_with_nul();
let name_raw = name.into_bytes_with_nul();
let description_raw = description.into_bytes_with_nul();
let id_raw = id.into_bytes_with_nul();
let progress_ctx = &mut progress_func as *mut F as *mut ffi::c_void;
unsafe {
let result = BNProjectCreateFileFromPathUnsafe(
self.as_raw(),
path_raw.as_ref().as_ptr() as *const ffi::c_char,
folder.map(|x| x.as_raw() as *mut _).unwrap_or(null_mut()),
name_raw.as_ref().as_ptr() as *const ffi::c_char,
description_raw.as_ref().as_ptr() as *const ffi::c_char,
id_raw.as_ref().as_ptr() as *const ffi::c_char,
systime_to_bntime(creation_time).unwrap(),
progress_ctx,
Some(cb_progress_func::<F>),
);
Ok(ProjectFile::from_raw(NonNull::new(result).ok_or(())?))
}
}
/// Create a file in the project
///
/// * `contents` - Bytes of the file that will be created
/// * `folder` - Folder to place the created file in
/// * `name` - Name to assign to the created file
/// * `description` - Description to assign to the created file
pub fn create_file<N, D>(
&self,
contents: &[u8],
folder: Option<&ProjectFolder>,
name: N,
description: D,
) -> Result<ProjectFile, ()>
where
N: BnStrCompatible,
D: BnStrCompatible,
{
let name_raw = name.into_bytes_with_nul();
let description_raw = description.into_bytes_with_nul();
unsafe {
let result = BNProjectCreateFile(
self.as_raw(),
contents.as_ptr(),
contents.len(),
folder.map(|x| x.as_raw() as *mut _).unwrap_or(null_mut()),
name_raw.as_ref().as_ptr() as *const ffi::c_char,
description_raw.as_ref().as_ptr() as *const ffi::c_char,
null_mut(),
Some(cb_progress_func_nop),
);
Ok(ProjectFile::from_raw(NonNull::new(result).ok_or(())?))
}
}
/// Create a file in the project
///
/// * `contents` - Bytes of the file that will be created
/// * `folder` - Folder to place the created file in
/// * `name` - Name to assign to the created file
/// * `description` - Description to assign to the created file
/// * `progress_func` - Progress function that will be called as the file is being added
pub fn create_file_with_progress<N, D, F>(
&self,
contents: &[u8],
folder: Option<&ProjectFolder>,
name: N,
description: D,
mut progress_func: F,
) -> Result<ProjectFile, ()>
where
N: BnStrCompatible,
D: BnStrCompatible,
F: FnMut(usize, usize) -> bool,
{
let name_raw = name.into_bytes_with_nul();
let description_raw = description.into_bytes_with_nul();
let progress_ctx = &mut progress_func as *mut F as *mut ffi::c_void;
unsafe {
let result = BNProjectCreateFile(
self.as_raw(),
contents.as_ptr(),
contents.len(),
folder.map(|x| x.as_raw() as *mut _).unwrap_or(null_mut()),
name_raw.as_ref().as_ptr() as *const ffi::c_char,
description_raw.as_ref().as_ptr() as *const ffi::c_char,
progress_ctx,
Some(cb_progress_func::<F>),
);
Ok(ProjectFile::from_raw(NonNull::new(result).ok_or(())?))
}
}
/// Create a file in the project
///
/// * `contents` - Bytes of the file that will be created
/// * `folder` - Folder to place the created file in
/// * `name` - Name to assign to the created file
/// * `description` - Description to assign to the created file
/// * `id` - id unique ID
/// * `creation_time` - Creation time of the file
pub unsafe fn create_file_unsafe<N, D, I>(
&self,
contents: &[u8],
folder: Option<&ProjectFolder>,
name: N,
description: D,
id: I,
creation_time: SystemTime,
) -> Result<ProjectFile, ()>
where
N: BnStrCompatible,
D: BnStrCompatible,
I: BnStrCompatible,
{
let name_raw = name.into_bytes_with_nul();
let description_raw = description.into_bytes_with_nul();
let id_raw = id.into_bytes_with_nul();
unsafe {
let result = BNProjectCreateFileUnsafe(
self.as_raw(),
contents.as_ptr(),
contents.len(),
folder.map(|x| x.as_raw() as *mut _).unwrap_or(null_mut()),
name_raw.as_ref().as_ptr() as *const ffi::c_char,
description_raw.as_ref().as_ptr() as *const ffi::c_char,
id_raw.as_ref().as_ptr() as *const ffi::c_char,
systime_to_bntime(creation_time).unwrap(),
null_mut(),
Some(cb_progress_func_nop),
);
Ok(ProjectFile::from_raw(NonNull::new(result).ok_or(())?))
}
}
/// Create a file in the project
///
/// * `contents` - Bytes of the file that will be created
/// * `folder` - Folder to place the created file in
/// * `name` - Name to assign to the created file
/// * `description` - Description to assign to the created file
/// * `id` - id unique ID
/// * `creation_time` - Creation time of the file
/// * `progress_func` - Progress function that will be called as the file is being added
pub unsafe fn create_file_with_progress_unsafe<N, D, I, F>(
&self,
contents: &[u8],
folder: Option<&ProjectFolder>,
name: N,
description: D,
id: I,
creation_time: SystemTime,
mut progress_func: F,
) -> Result<ProjectFile, ()>
where
N: BnStrCompatible,
D: BnStrCompatible,
I: BnStrCompatible,
F: FnMut(usize, usize) -> bool,
{
let name_raw = name.into_bytes_with_nul();
let description_raw = description.into_bytes_with_nul();
let id_raw = id.into_bytes_with_nul();
let progress_ctx = &mut progress_func as *mut F as *mut ffi::c_void;
unsafe {
let result = BNProjectCreateFileUnsafe(
self.as_raw(),
contents.as_ptr(),
contents.len(),
folder.map(|x| x.as_raw() as *mut _).unwrap_or(null_mut()),
name_raw.as_ref().as_ptr() as *const ffi::c_char,
description_raw.as_ref().as_ptr() as *const ffi::c_char,
id_raw.as_ref().as_ptr() as *const ffi::c_char,
systime_to_bntime(creation_time).unwrap(),
progress_ctx,
Some(cb_progress_func::<F>),
);
Ok(ProjectFile::from_raw(NonNull::new(result).ok_or(())?))
}
}
/// Get a list of files in the project
pub fn files(&self) -> Result<Array<ProjectFile>, ()> {
let mut count = 0;
let result = unsafe { BNProjectGetFiles(self.as_raw(), &mut count) };
assert!(!result.is_null());
Ok(unsafe { Array::new(result, count, ()) })
}
/// Retrieve a file in the project by unique `id`
pub fn file_by_id<S: BnStrCompatible>(&self, id: S) -> Option<ProjectFile> {
let id_raw = id.into_bytes_with_nul();
let id_ptr = id_raw.as_ref().as_ptr() as *const ffi::c_char;
let result = unsafe { BNProjectGetFileById(self.as_raw(), id_ptr) };
let handle = NonNull::new(result)?;
Some(unsafe { ProjectFile::from_raw(handle) })
}
/// Retrieve a file in the project by the `path` on disk
pub fn file_by_path<S: BnStrCompatible>(&self, path: S) -> Option<ProjectFile> {
let path_raw = path.into_bytes_with_nul();
let path_ptr = path_raw.as_ref().as_ptr() as *const ffi::c_char;
let result = unsafe { BNProjectGetFileByPathOnDisk(self.as_raw(), path_ptr) };
let handle = NonNull::new(result)?;
Some(unsafe { ProjectFile::from_raw(handle) })
}
/// Delete a file from the project
pub fn delete_file(&self, file: &ProjectFile) -> bool {
unsafe { BNProjectDeleteFile(self.as_raw(), file.as_raw()) }
}
/// A context manager to speed up bulk project operations.
/// Project modifications are synced to disk in chunks,
/// and the project on disk vs in memory may not agree on state
/// if an exception occurs while a bulk operation is happening.
///
/// ```no_run
/// # use binaryninja::project::Project;
/// # let project: Project = todo!();
/// if let Ok(bulk) = project.bulk_operation() {
/// for file in std::fs::read_dir("/bin/").unwrap().into_iter() {
/// let file = file.unwrap();
/// let file_type = file.file_type().unwrap();
/// if file_type.is_file() && !file_type.is_symlink() {
/// bulk.create_file_from_path(
/// "/bin/",
/// None,
/// &file.file_name().to_string_lossy(),
/// "",
/// ).unwrap();
/// }
/// }
/// }
/// ```
// NOTE mut is used here, so only one lock can be aquired at once
pub fn bulk_operation(&mut self) -> Result<ProjectBultOperationLock, ()> {
Ok(ProjectBultOperationLock::lock(self))
}
}
impl Drop for Project {
fn drop(&mut self) {
unsafe { BNFreeProject(self.as_raw()) }
}
}
impl Clone for Project {
fn clone(&self) -> Self {
unsafe { Self::from_raw(NonNull::new(BNNewProjectReference(self.as_raw())).unwrap()) }
}
}
impl CoreArrayProvider for Project {
type Raw = *mut BNProject;
type Context = ();
type Wrapped<'a> = &'a Project;
}
unsafe impl CoreArrayProviderInner for Project {
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
BNFreeProjectList(raw, count)
}
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
Self::ref_from_raw(raw)
}
}
pub struct ProjectBultOperationLock<'a> {
lock: &'a mut Project,
}
impl<'a> ProjectBultOperationLock<'a> {
pub fn lock(project: &'a mut Project) -> Self {
unsafe { BNProjectBeginBulkOperation(project.as_raw()) };
Self { lock: project }
}
pub fn unlock(self) {
// NOTE does nothing, just drop self
}
}
impl std::ops::Deref for ProjectBultOperationLock<'_> {
type Target = Project;
fn deref(&self) -> &Self::Target {
self.lock
}
}
impl Drop for ProjectBultOperationLock<'_> {
fn drop(&mut self) {
unsafe { BNProjectEndBulkOperation(self.lock.as_raw()) };
}
}
#[repr(transparent)]
pub struct ProjectFolder {
handle: NonNull<BNProjectFolder>,
}
impl ProjectFolder {
pub(crate) unsafe fn from_raw(handle: NonNull<BNProjectFolder>) -> Self {
Self { handle }
}
pub(crate) unsafe fn ref_from_raw(handle: &*mut BNProjectFolder) -> &Self {
debug_assert!(!handle.is_null());
mem::transmute(handle)
}
#[allow(clippy::mut_from_ref)]
pub(crate) unsafe fn as_raw(&self) -> &mut BNProjectFolder {
&mut *self.handle.as_ptr()
}
/// Get the project that owns this folder
pub fn project(&self) -> Project {
unsafe {
Project::from_raw(NonNull::new(BNProjectFolderGetProject(self.as_raw())).unwrap())
}
}
/// Get the unique id of this folder
pub fn id(&self) -> BnString {
unsafe { BnString::from_raw(BNProjectFolderGetId(self.as_raw())) }
}
/// Get the name of this folder
pub fn name(&self) -> BnString {
unsafe { BnString::from_raw(BNProjectFolderGetName(self.as_raw())) }
}
/// Set the name of this folder
pub fn set_name<S: BnStrCompatible>(&self, value: S) {
let value_raw = value.into_bytes_with_nul();
unsafe {
BNProjectFolderSetName(
self.as_raw(),
value_raw.as_ref().as_ptr() as *const ffi::c_char,
)
}
}
/// Get the description of this folder
pub fn description(&self) -> BnString {
unsafe { BnString::from_raw(BNProjectFolderGetDescription(self.as_raw())) }
}
/// Set the description of this folder
pub fn set_description<S: BnStrCompatible>(&self, value: S) {
let value_raw = value.into_bytes_with_nul();
unsafe {
BNProjectFolderSetDescription(
self.as_raw(),
value_raw.as_ref().as_ptr() as *const ffi::c_char,
)
}
}
/// Get the folder that contains this folder
pub fn parent(&self) -> Option<ProjectFolder> {
let result = unsafe { BNProjectFolderGetParent(self.as_raw()) };
NonNull::new(result).map(|handle| unsafe { ProjectFolder::from_raw(handle) })
}
/// Set the folder that contains this folder
pub fn set_folder(&self, folder: Option<&ProjectFolder>) {
let folder_handle = folder
.map(|x| unsafe { x.as_raw() as *mut _ })
.unwrap_or(null_mut());
unsafe { BNProjectFolderSetParent(self.as_raw(), folder_handle) }
}
/// Recursively export this folder to disk, returns `true' if the export succeeded
///
/// * `dest` - Destination path for the exported contents
pub fn export<S: BnStrCompatible>(&self, dest: S) -> bool {
let dest_raw = dest.into_bytes_with_nul();
unsafe {
BNProjectFolderExport(
self.as_raw(),
dest_raw.as_ref().as_ptr() as *const ffi::c_char,
null_mut(),
Some(cb_progress_func_nop),
)
}
}
/// Recursively export this folder to disk, returns `true' if the export succeeded
///
/// * `dest` - Destination path for the exported contents
/// * `progress_func` - Progress function that will be called as contents are exporting
pub fn export_with_progress<S, F>(&self, dest: S, mut progress: F) -> bool
where
S: BnStrCompatible,
F: FnMut(usize, usize) -> bool,
{
let dest_raw = dest.into_bytes_with_nul();
unsafe {
BNProjectFolderExport(
self.as_raw(),
dest_raw.as_ref().as_ptr() as *const ffi::c_char,
&mut progress as *mut _ as *mut ffi::c_void,
Some(cb_progress_func::<F>),
)
}
}
}
impl Drop for ProjectFolder {
fn drop(&mut self) {
unsafe { BNFreeProjectFolder(self.as_raw()) }
}
}
impl Clone for ProjectFolder {
fn clone(&self) -> Self {
unsafe { Self::from_raw(NonNull::new(BNNewProjectFolderReference(self.as_raw())).unwrap()) }
}
}
impl CoreArrayProvider for ProjectFolder {
type Raw = *mut BNProjectFolder;
type Context = ();
type Wrapped<'a> = &'a Self;
}
unsafe impl CoreArrayProviderInner for ProjectFolder {
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
BNFreeProjectFolderList(raw, count)
}
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
Self::ref_from_raw(raw)
}
}
#[repr(transparent)]
pub struct ProjectFile {
handle: NonNull<BNProjectFile>,
}
impl ProjectFile {
pub(crate) unsafe fn from_raw(handle: NonNull<BNProjectFile>) -> Self {
Self { handle }
}
pub(crate) unsafe fn ref_from_raw(handle: &*mut BNProjectFile) -> &Self {
debug_assert!(!handle.is_null());
mem::transmute(handle)
}
#[allow(clippy::mut_from_ref)]
pub(crate) unsafe fn as_raw(&self) -> &mut BNProjectFile {
&mut *self.handle.as_ptr()
}
/// Get the project that owns this file
pub fn project(&self) -> Project {
unsafe { Project::from_raw(NonNull::new(BNProjectFileGetProject(self.as_raw())).unwrap()) }
}
/// Get the path on disk to this file's contents
pub fn path_on_disk(&self) -> BnString {
unsafe { BnString::from_raw(BNProjectFileGetPathOnDisk(self.as_raw())) }
}
/// Check if this file's contents exist on disk
pub fn exists_on_disk(&self) -> bool {
unsafe { BNProjectFileExistsOnDisk(self.as_raw()) }
}
/// Get the unique id of this file
pub fn id(&self) -> BnString {
unsafe { BnString::from_raw(BNProjectFileGetId(self.as_raw())) }
}
/// Get the name of this file
pub fn name(&self) -> BnString {
unsafe { BnString::from_raw(BNProjectFileGetName(self.as_raw())) }
}
/// Set the name of this file
pub fn set_name<S: BnStrCompatible>(&self, value: S) {
let value_raw = value.into_bytes_with_nul();
unsafe {
BNProjectFileSetName(
self.as_raw(),
value_raw.as_ref().as_ptr() as *const ffi::c_char,
)
}
}
/// Get the description of this file
pub fn description(&self) -> BnString {
unsafe { BnString::from_raw(BNProjectFileGetDescription(self.as_raw())) }
}
/// Set the description of this file
pub fn set_description<S: BnStrCompatible>(&self, value: S) {
let value_raw = value.into_bytes_with_nul();
unsafe {
BNProjectFileSetDescription(
self.as_raw(),
value_raw.as_ref().as_ptr() as *const ffi::c_char,
)
}
}
/// Get the file creation time
pub fn creation_time(&self) -> SystemTime {
systime_from_bntime(unsafe { BNProjectFileGetCreationTimestamp(self.as_raw()) }).unwrap()
}
/// Get the folder that contains this file
pub fn folder(&self) -> Option<ProjectFolder> {
let result = unsafe { BNProjectFileGetFolder(self.as_raw()) };
NonNull::new(result).map(|handle| unsafe { ProjectFolder::from_raw(handle) })
}
/// Set the folder that contains this file
pub fn set_folder(&self, folder: Option<&ProjectFolder>) {
let folder_handle = folder
.map(|x| unsafe { x.as_raw() as *mut _ })
.unwrap_or(null_mut());
unsafe { BNProjectFileSetFolder(self.as_raw(), folder_handle) }
}
/// Export this file to disk, `true' if the export succeeded
///
/// * `dest` - Destination path for the exported contents
pub fn export<S: BnStrCompatible>(&self, dest: S) -> bool {
let dest_raw = dest.into_bytes_with_nul();
unsafe {
BNProjectFileExport(
self.as_raw(),
dest_raw.as_ref().as_ptr() as *const ffi::c_char,
)
}
}
}
impl Drop for ProjectFile {
fn drop(&mut self) {
unsafe { BNFreeProjectFile(self.as_raw()) }
}
}
impl Clone for ProjectFile {
fn clone(&self) -> Self {
unsafe { Self::from_raw(NonNull::new(BNNewProjectFileReference(self.as_raw())).unwrap()) }
}
}
impl CoreArrayProvider for ProjectFile {
type Raw = *mut BNProjectFile;
type Context = ();
type Wrapped<'a> = &'a Self;
}
unsafe impl CoreArrayProviderInner for ProjectFile {
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
BNFreeProjectFileList(raw, count)
}
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
Self::ref_from_raw(raw)
}
}
fn systime_from_bntime(time: i64) -> Option<SystemTime> {
let m = Duration::from_secs(time.try_into().ok()?);
Some(UNIX_EPOCH + m)
}
fn systime_to_bntime(time: SystemTime) -> Option<i64> {
time.duration_since(UNIX_EPOCH)
.ok()?
.as_secs()
.try_into()
.ok()
}
unsafe extern "C" fn cb_progress_func<F: FnMut(usize, usize) -> bool>(
ctxt: *mut ffi::c_void,
progress: usize,
total: usize,
) -> bool {
if ctxt.is_null() {
return true;
}
let closure: &mut F = mem::transmute(ctxt);
closure(progress, total)
}
unsafe extern "C" fn cb_progress_func_nop(
_ctxt: *mut ffi::c_void,
_progress: usize,
_total: usize,
) -> bool {
true
}
|