binaryninja/
settings.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
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
// Copyright 2021-2025 Vector 35 Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! An interface for reading, writing, and creating new settings

use binaryninjacore_sys::*;
use std::ffi::c_char;
use std::fmt::Debug;

use crate::binary_view::BinaryView;
use crate::rc::*;
use crate::string::{BnStrCompatible, BnString};

use crate::function::Function;

pub type SettingsScope = BNSettingsScope;

pub const DEFAULT_INSTANCE_ID: &str = "default";
pub const GLOBAL_INSTANCE_ID: &str = "";

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

impl Settings {
    pub(crate) unsafe fn from_raw(handle: *mut BNSettings) -> Ref<Self> {
        debug_assert!(!handle.is_null());
        Ref::new(Self { handle })
    }

    pub fn new() -> Ref<Self> {
        Self::new_with_id(GLOBAL_INSTANCE_ID)
    }

    pub fn new_with_id<S: BnStrCompatible>(instance_id: S) -> Ref<Self> {
        let instance_id = instance_id.into_bytes_with_nul();
        unsafe {
            let handle = BNCreateSettings(instance_id.as_ref().as_ptr() as *mut _);
            debug_assert!(!handle.is_null());
            Ref::new(Self { handle })
        }
    }

    pub fn set_resource_id<S: BnStrCompatible>(&self, resource_id: S) {
        let resource_id = resource_id.into_bytes_with_nul();
        unsafe { BNSettingsSetResourceId(self.handle, resource_id.as_ref().as_ptr() as *mut _) };
    }

    pub fn serialize_schema(&self) -> BnString {
        unsafe { BnString::from_raw(BNSettingsSerializeSchema(self.handle)) }
    }

    pub fn deserialize_schema<S: BnStrCompatible>(&self, schema: S) -> bool {
        self.deserialize_schema_with_scope(schema, SettingsScope::SettingsAutoScope)
    }

    pub fn deserialize_schema_with_scope<S: BnStrCompatible>(
        &self,
        schema: S,
        scope: SettingsScope,
    ) -> bool {
        let schema = schema.into_bytes_with_nul();
        unsafe {
            BNSettingsDeserializeSchema(
                self.handle,
                schema.as_ref().as_ptr() as *mut _,
                scope,
                true,
            )
        }
    }

    pub fn contains<S: BnStrCompatible>(&self, key: S) -> bool {
        let key = key.into_bytes_with_nul();

        unsafe { BNSettingsContains(self.handle, key.as_ref().as_ptr() as *mut _) }
    }

    pub fn keys(&self) -> Array<BnString> {
        let mut count = 0;
        let result = unsafe { BNSettingsKeysList(self.handle, &mut count) };
        assert!(!result.is_null());
        unsafe { Array::new(result as *mut *mut c_char, count, ()) }
    }

    // TODO Update the settings API to take an optional BinaryView or Function. Separate functions or...?

    pub fn get_bool<S: BnStrCompatible>(&self, key: S) -> bool {
        self.get_bool_with_opts(key, &mut QueryOptions::default())
    }

    pub fn get_bool_with_opts<S: BnStrCompatible>(
        &self,
        key: S,
        options: &mut QueryOptions,
    ) -> bool {
        let key = key.into_bytes_with_nul();
        let view_ptr = match options.view.as_ref() {
            Some(view) => view.handle,
            _ => std::ptr::null_mut(),
        };
        let func_ptr = match options.function.as_ref() {
            Some(func) => func.handle,
            _ => std::ptr::null_mut(),
        };
        unsafe {
            BNSettingsGetBool(
                self.handle,
                key.as_ref().as_ptr() as *mut _,
                view_ptr,
                func_ptr,
                &mut options.scope,
            )
        }
    }

    pub fn get_double<S: BnStrCompatible>(&self, key: S) -> f64 {
        self.get_double_with_opts(key, &mut QueryOptions::default())
    }

    pub fn get_double_with_opts<S: BnStrCompatible>(
        &self,
        key: S,
        options: &mut QueryOptions,
    ) -> f64 {
        let key = key.into_bytes_with_nul();
        let view_ptr = match options.view.as_ref() {
            Some(view) => view.handle,
            _ => std::ptr::null_mut(),
        };
        let func_ptr = match options.function.as_ref() {
            Some(func) => func.handle,
            _ => std::ptr::null_mut(),
        };
        unsafe {
            BNSettingsGetDouble(
                self.handle,
                key.as_ref().as_ptr() as *mut _,
                view_ptr,
                func_ptr,
                &mut options.scope,
            )
        }
    }

    pub fn get_integer<S: BnStrCompatible>(&self, key: S) -> u64 {
        self.get_integer_with_opts(key, &mut QueryOptions::default())
    }

    pub fn get_integer_with_opts<S: BnStrCompatible>(
        &self,
        key: S,
        options: &mut QueryOptions,
    ) -> u64 {
        let key = key.into_bytes_with_nul();
        let view_ptr = match options.view.as_ref() {
            Some(view) => view.handle,
            _ => std::ptr::null_mut(),
        };
        let func_ptr = match options.function.as_ref() {
            Some(func) => func.handle,
            _ => std::ptr::null_mut(),
        };
        unsafe {
            BNSettingsGetUInt64(
                self.handle,
                key.as_ref().as_ptr() as *mut _,
                view_ptr,
                func_ptr,
                &mut options.scope,
            )
        }
    }

    pub fn get_string<S: BnStrCompatible>(&self, key: S) -> BnString {
        self.get_string_with_opts(key, &mut QueryOptions::default())
    }

    pub fn get_string_with_opts<S: BnStrCompatible>(
        &self,
        key: S,
        options: &mut QueryOptions,
    ) -> BnString {
        let key = key.into_bytes_with_nul();
        let view_ptr = match options.view.as_ref() {
            Some(view) => view.handle,
            _ => std::ptr::null_mut(),
        };
        let func_ptr = match options.function.as_ref() {
            Some(func) => func.handle,
            _ => std::ptr::null_mut(),
        };
        unsafe {
            BnString::from_raw(BNSettingsGetString(
                self.handle,
                key.as_ref().as_ptr() as *mut _,
                view_ptr,
                func_ptr,
                &mut options.scope,
            ))
        }
    }

    pub fn get_string_list<S: BnStrCompatible>(&self, key: S) -> Array<BnString> {
        self.get_string_list_with_opts(key, &mut QueryOptions::default())
    }

    pub fn get_string_list_with_opts<S: BnStrCompatible>(
        &self,
        key: S,
        options: &mut QueryOptions,
    ) -> Array<BnString> {
        let key = key.into_bytes_with_nul();
        let view_ptr = match options.view.as_ref() {
            Some(view) => view.handle,
            _ => std::ptr::null_mut(),
        };
        let func_ptr = match options.function.as_ref() {
            Some(func) => func.handle,
            _ => std::ptr::null_mut(),
        };
        let mut size: usize = 0;
        unsafe {
            Array::new(
                BNSettingsGetStringList(
                    self.handle,
                    key.as_ref().as_ptr() as *mut _,
                    view_ptr,
                    func_ptr,
                    &mut options.scope,
                    &mut size,
                ) as *mut *mut c_char,
                size,
                (),
            )
        }
    }

    pub fn get_json<S: BnStrCompatible>(&self, key: S) -> BnString {
        self.get_json_with_opts(key, &mut QueryOptions::default())
    }

    pub fn get_json_with_opts<S: BnStrCompatible>(
        &self,
        key: S,
        options: &mut QueryOptions,
    ) -> BnString {
        let key = key.into_bytes_with_nul();
        let view_ptr = match options.view.as_ref() {
            Some(view) => view.handle,
            _ => std::ptr::null_mut(),
        };
        let func_ptr = match options.function.as_ref() {
            Some(func) => func.handle,
            _ => std::ptr::null_mut(),
        };
        unsafe {
            BnString::from_raw(BNSettingsGetJson(
                self.handle,
                key.as_ref().as_ptr() as *mut _,
                view_ptr,
                func_ptr,
                &mut options.scope,
            ))
        }
    }

    pub fn set_bool<S: BnStrCompatible>(&self, key: S, value: bool) {
        self.set_bool_with_opts(key, value, &QueryOptions::default())
    }

    pub fn set_bool_with_opts<S: BnStrCompatible>(
        &self,
        key: S,
        value: bool,
        options: &QueryOptions,
    ) {
        let key = key.into_bytes_with_nul();
        let view_ptr = match options.view.as_ref() {
            Some(view) => view.handle,
            _ => std::ptr::null_mut(),
        };
        let func_ptr = match options.function.as_ref() {
            Some(func) => func.handle,
            _ => std::ptr::null_mut(),
        };
        unsafe {
            BNSettingsSetBool(
                self.handle,
                view_ptr,
                func_ptr,
                options.scope,
                key.as_ref().as_ptr() as *mut _,
                value,
            );
        }
    }

    pub fn set_double<S: BnStrCompatible>(&self, key: S, value: f64) {
        self.set_double_with_opts(key, value, &QueryOptions::default())
    }
    pub fn set_double_with_opts<S: BnStrCompatible>(
        &self,
        key: S,
        value: f64,
        options: &QueryOptions,
    ) {
        let key = key.into_bytes_with_nul();
        let view_ptr = match options.view.as_ref() {
            Some(view) => view.handle,
            _ => std::ptr::null_mut(),
        };
        let func_ptr = match options.function.as_ref() {
            Some(func) => func.handle,
            _ => std::ptr::null_mut(),
        };
        unsafe {
            BNSettingsSetDouble(
                self.handle,
                view_ptr,
                func_ptr,
                options.scope,
                key.as_ref().as_ptr() as *mut _,
                value,
            );
        }
    }

    pub fn set_integer<S: BnStrCompatible>(&self, key: S, value: u64) {
        self.set_integer_with_opts(key, value, &QueryOptions::default())
    }

    pub fn set_integer_with_opts<S: BnStrCompatible>(
        &self,
        key: S,
        value: u64,
        options: &QueryOptions,
    ) {
        let key = key.into_bytes_with_nul();
        let view_ptr = match options.view.as_ref() {
            Some(view) => view.handle,
            _ => std::ptr::null_mut(),
        };
        let func_ptr = match options.function.as_ref() {
            Some(func) => func.handle,
            _ => std::ptr::null_mut(),
        };
        unsafe {
            BNSettingsSetUInt64(
                self.handle,
                view_ptr,
                func_ptr,
                options.scope,
                key.as_ref().as_ptr() as *mut _,
                value,
            );
        }
    }

    pub fn set_string<S1: BnStrCompatible, S2: BnStrCompatible>(&self, key: S1, value: S2) {
        self.set_string_with_opts(key, value, &QueryOptions::default())
    }

    pub fn set_string_with_opts<S1: BnStrCompatible, S2: BnStrCompatible>(
        &self,
        key: S1,
        value: S2,
        options: &QueryOptions,
    ) {
        let key = key.into_bytes_with_nul();
        let value = value.into_bytes_with_nul();
        let view_ptr = match options.view.as_ref() {
            Some(view) => view.handle,
            _ => std::ptr::null_mut(),
        };
        let func_ptr = match options.function.as_ref() {
            Some(func) => func.handle,
            _ => std::ptr::null_mut(),
        };
        unsafe {
            BNSettingsSetString(
                self.handle,
                view_ptr,
                func_ptr,
                options.scope,
                key.as_ref().as_ptr() as *mut _,
                value.as_ref().as_ptr() as *mut _,
            );
        }
    }

    pub fn set_string_list<S1: BnStrCompatible, S2: BnStrCompatible, I: Iterator<Item = S2>>(
        &self,
        key: S1,
        value: I,
    ) -> bool {
        self.set_string_list_with_opts(key, value, &QueryOptions::default())
    }

    pub fn set_string_list_with_opts<
        S1: BnStrCompatible,
        S2: BnStrCompatible,
        I: Iterator<Item = S2>,
    >(
        &self,
        key: S1,
        value: I,
        options: &QueryOptions,
    ) -> bool {
        let key = key.into_bytes_with_nul();
        let raw_list: Vec<_> = value.map(|s| s.into_bytes_with_nul()).collect();
        let mut raw_list_ptr: Vec<_> = raw_list
            .iter()
            .map(|s| s.as_ref().as_ptr() as *const c_char)
            .collect();

        let view_ptr = match options.view.as_ref() {
            Some(view) => view.handle,
            _ => std::ptr::null_mut(),
        };
        let func_ptr = match options.function.as_ref() {
            Some(func) => func.handle,
            _ => std::ptr::null_mut(),
        };
        unsafe {
            BNSettingsSetStringList(
                self.handle,
                view_ptr,
                func_ptr,
                options.scope,
                key.as_ref().as_ptr() as *mut _,
                raw_list_ptr.as_mut_ptr(),
                raw_list_ptr.len(),
            )
        }
    }

    pub fn set_json<S1: BnStrCompatible, S2: BnStrCompatible>(&self, key: S1, value: S2) -> bool {
        self.set_json_with_opts(key, value, &QueryOptions::default())
    }

    pub fn set_json_with_opts<S1: BnStrCompatible, S2: BnStrCompatible>(
        &self,
        key: S1,
        value: S2,
        options: &QueryOptions,
    ) -> bool {
        let key = key.into_bytes_with_nul();
        let value = value.into_bytes_with_nul();
        let view_ptr = match options.view.as_ref() {
            Some(view) => view.handle,
            _ => std::ptr::null_mut(),
        };
        let func_ptr = match options.function.as_ref() {
            Some(func) => func.handle,
            _ => std::ptr::null_mut(),
        };
        unsafe {
            BNSettingsSetJson(
                self.handle,
                view_ptr,
                func_ptr,
                options.scope,
                key.as_ref().as_ptr() as *mut _,
                value.as_ref().as_ptr() as *mut _,
            )
        }
    }

    pub fn get_property_string<S: BnStrCompatible>(&self, key: S, property: S) -> BnString {
        let key = key.into_bytes_with_nul();
        let property = property.into_bytes_with_nul();
        unsafe {
            BnString::from_raw(BNSettingsQueryPropertyString(
                self.handle,
                key.as_ref().as_ptr() as *mut _,
                property.as_ref().as_ptr() as *mut _,
            ))
        }
    }

    pub fn get_property_string_list<S: BnStrCompatible>(
        &self,
        key: S,
        property: S,
    ) -> Array<BnString> {
        let key = key.into_bytes_with_nul();
        let property = property.into_bytes_with_nul();
        let mut size: usize = 0;
        unsafe {
            Array::new(
                BNSettingsQueryPropertyStringList(
                    self.handle,
                    key.as_ref().as_ptr() as *mut _,
                    property.as_ref().as_ptr() as *mut _,
                    &mut size,
                ) as *mut *mut c_char,
                size,
                (),
            )
        }
    }

    pub fn update_bool_property<S: BnStrCompatible>(&self, key: S, property: S, value: bool) {
        let key = key.into_bytes_with_nul();
        let property = property.into_bytes_with_nul();
        unsafe {
            BNSettingsUpdateBoolProperty(
                self.handle,
                key.as_ref().as_ptr() as *mut _,
                property.as_ref().as_ptr() as *mut _,
                value,
            );
        }
    }

    pub fn update_integer_property<S: BnStrCompatible>(&self, key: S, property: S, value: u64) {
        let key = key.into_bytes_with_nul();
        let property = property.into_bytes_with_nul();
        unsafe {
            BNSettingsUpdateUInt64Property(
                self.handle,
                key.as_ref().as_ptr() as *mut _,
                property.as_ref().as_ptr() as *mut _,
                value,
            );
        }
    }

    pub fn update_double_property<S: BnStrCompatible>(&self, key: S, property: S, value: f64) {
        let key = key.into_bytes_with_nul();
        let property = property.into_bytes_with_nul();
        unsafe {
            BNSettingsUpdateDoubleProperty(
                self.handle,
                key.as_ref().as_ptr() as *mut _,
                property.as_ref().as_ptr() as *mut _,
                value,
            );
        }
    }

    pub fn update_string_property<S: BnStrCompatible>(&self, key: S, property: S, value: S) {
        let key = key.into_bytes_with_nul();
        let property = property.into_bytes_with_nul();
        let value = value.into_bytes_with_nul();
        unsafe {
            BNSettingsUpdateStringProperty(
                self.handle,
                key.as_ref().as_ptr() as *mut _,
                property.as_ref().as_ptr() as *mut _,
                value.as_ref().as_ptr() as *mut _,
            );
        }
    }

    pub fn update_string_list_property<S: BnStrCompatible, I: Iterator<Item = S>>(
        &self,
        key: S,
        property: S,
        value: I,
    ) {
        let key = key.into_bytes_with_nul();
        let property = property.into_bytes_with_nul();
        let raw_list: Vec<_> = value.map(|s| s.into_bytes_with_nul()).collect();
        let mut raw_list_ptr: Vec<_> = raw_list
            .iter()
            .map(|s| s.as_ref().as_ptr() as *const c_char)
            .collect();

        unsafe {
            BNSettingsUpdateStringListProperty(
                self.handle,
                key.as_ref().as_ptr() as *mut _,
                property.as_ref().as_ptr() as *mut _,
                raw_list_ptr.as_mut_ptr(),
                raw_list_ptr.len(),
            );
        }
    }

    pub fn register_group<S1: BnStrCompatible, S2: BnStrCompatible>(
        &self,
        group: S1,
        title: S2,
    ) -> bool {
        let group = group.into_bytes_with_nul();
        let title = title.into_bytes_with_nul();

        unsafe {
            BNSettingsRegisterGroup(
                self.handle,
                group.as_ref().as_ptr() as *mut _,
                title.as_ref().as_ptr() as *mut _,
            )
        }
    }

    pub fn register_setting_json<S1: BnStrCompatible, S2: BnStrCompatible>(
        &self,
        group: S1,
        properties: S2,
    ) -> bool {
        let group = group.into_bytes_with_nul();
        let properties = properties.into_bytes_with_nul();

        unsafe {
            BNSettingsRegisterSetting(
                self.handle,
                group.as_ref().as_ptr() as *mut _,
                properties.as_ref().as_ptr() as *mut _,
            )
        }
    }

    // TODO: register_setting but type-safely turn it into json
}

impl Default for Ref<Settings> {
    fn default() -> Self {
        Settings::new_with_id(DEFAULT_INSTANCE_ID)
    }
}

unsafe impl Send for Settings {}
unsafe impl Sync for Settings {}

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

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

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

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

#[derive(Debug, Clone)]
pub struct QueryOptions<'a> {
    pub scope: SettingsScope,
    pub view: Option<&'a BinaryView>,
    pub function: Option<Ref<Function>>,
}

impl<'a> QueryOptions<'a> {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn new_with_view(view: &'a BinaryView) -> Self {
        Self {
            view: Some(view),
            ..Default::default()
        }
    }

    pub fn new_with_func(func: Ref<Function>) -> Self {
        Self {
            function: Some(func),
            ..Default::default()
        }
    }

    /// Set the query to target a specific view, this will be overridden if a function is targeted.
    pub fn with_view(mut self, view: &'a BinaryView) -> Self {
        self.view = Some(view);
        self
    }

    pub fn with_scope(mut self, scope: SettingsScope) -> Self {
        self.scope = scope;
        self
    }

    /// Set the query to target a specific function, this will override the target view.
    pub fn with_function(mut self, function: Ref<Function>) -> Self {
        self.function = Some(function);
        self
    }
}

impl Default for QueryOptions<'_> {
    fn default() -> Self {
        Self {
            view: None,
            scope: SettingsScope::SettingsAutoScope,
            function: None,
        }
    }
}