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
use std::collections::HashMap;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::{ffi, mem, ptr};

use binaryninjacore_sys::*;

use crate::binaryview::BinaryView;
use crate::databuffer::DataBuffer;
use crate::disassembly::InstructionTextToken;
use crate::filemetadata::FileMetadata;
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner};
use crate::string::{BnStrCompatible, BnString};

#[repr(transparent)]
pub struct Database {
    handle: ptr::NonNull<BNDatabase>,
}

impl Database {
    pub(crate) unsafe fn from_raw(handle: ptr::NonNull<BNDatabase>) -> Self {
        Self { handle }
    }

    #[allow(clippy::mut_from_ref)]
    pub(crate) unsafe fn as_raw(&self) -> &mut BNDatabase {
        &mut *self.handle.as_ptr()
    }

    /// Get a snapshot by its id, or None if no snapshot with that id exists
    pub fn snapshot(&self, id: i64) -> Option<Snapshot> {
        let result = unsafe { BNGetDatabaseSnapshot(self.as_raw(), id) };
        ptr::NonNull::new(result).map(|handle| unsafe { Snapshot::from_raw(handle) })
    }

    /// Get a list of all snapshots in the database
    pub fn snapshots(&self) -> Array<Snapshot> {
        let mut count = 0;
        let result = unsafe { BNGetDatabaseSnapshots(self.as_raw(), &mut count) };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Get the current snapshot
    pub fn current_snapshot(&self) -> Option<Snapshot> {
        let result = unsafe { BNGetDatabaseCurrentSnapshot(self.as_raw()) };
        ptr::NonNull::new(result).map(|handle| unsafe { Snapshot::from_raw(handle) })
    }

    pub fn set_current_snapshot(&self, value: &Snapshot) {
        unsafe { BNSetDatabaseCurrentSnapshot(self.as_raw(), value.id()) }
    }

    pub fn write_snapshot_data<N: BnStrCompatible>(
        &self,
        parents: &[i64],
        file: &BinaryView,
        name: N,
        data: &KeyValueStore,
        auto_save: bool,
    ) -> i64 {
        let name_raw = name.into_bytes_with_nul();
        let name_ptr = name_raw.as_ref().as_ptr() as *const ffi::c_char;
        unsafe {
            BNWriteDatabaseSnapshotData(
                self.as_raw(),
                parents.as_ptr() as *mut _,
                parents.len(),
                file.handle,
                name_ptr,
                data.as_raw(),
                auto_save,
                ptr::null_mut(),
                Some(cb_progress_nop),
            )
        }
    }

    pub fn write_snapshot_data_with_progress<N, F>(
        &self,
        parents: &[i64],
        file: &BinaryView,
        name: N,
        data: &KeyValueStore,
        auto_save: bool,
        mut progress: F,
    ) -> i64
    where
        N: BnStrCompatible,
        F: FnMut(usize, usize) -> bool,
    {
        let name_raw = name.into_bytes_with_nul();
        let name_ptr = name_raw.as_ref().as_ptr() as *const ffi::c_char;
        let ctxt = &mut progress as *mut _ as *mut ffi::c_void;
        unsafe {
            BNWriteDatabaseSnapshotData(
                self.as_raw(),
                parents.as_ptr() as *mut _,
                parents.len(),
                file.handle,
                name_ptr,
                data.as_raw(),
                auto_save,
                ctxt,
                Some(cb_progress::<F>),
            )
        }
    }

    /// Trim a snapshot's contents in the database by id, but leave the parent/child
    /// hierarchy intact. Future references to this snapshot will return False for has_contents
    pub fn trim_snapshot(&self, id: i64) -> Result<(), ()> {
        if unsafe { BNTrimDatabaseSnapshot(self.as_raw(), id) } {
            Ok(())
        } else {
            Err(())
        }
    }

    /// Remove a snapshot in the database by id, deleting its contents and references.
    /// Attempting to remove a snapshot with children will raise an exception.
    pub fn remove_snapshot(&self, id: i64) -> Result<(), ()> {
        if unsafe { BNRemoveDatabaseSnapshot(self.as_raw(), id) } {
            Ok(())
        } else {
            Err(())
        }
    }
    pub fn has_global<S: BnStrCompatible>(&self, key: S) -> bool {
        let key_raw = key.into_bytes_with_nul();
        let key_ptr = key_raw.as_ref().as_ptr() as *const ffi::c_char;
        unsafe { BNDatabaseHasGlobal(self.as_raw(), key_ptr) != 0 }
    }

    /// Get a list of keys for all globals in the database
    pub fn global_keys(&self) -> Array<BnString> {
        let mut count = 0;
        let result = unsafe { BNGetDatabaseGlobalKeys(self.as_raw(), &mut count) };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Get a dictionary of all globals
    pub fn globals(&self) -> HashMap<String, String> {
        self.global_keys()
            .iter()
            .filter_map(|key| Some((key.to_string(), self.read_global(key)?.to_string())))
            .collect()
    }

    /// Get a specific global by key
    pub fn read_global<S: BnStrCompatible>(&self, key: S) -> Option<BnString> {
        let key_raw = key.into_bytes_with_nul();
        let key_ptr = key_raw.as_ref().as_ptr() as *const ffi::c_char;
        let result = unsafe { BNReadDatabaseGlobal(self.as_raw(), key_ptr) };
        unsafe { ptr::NonNull::new(result).map(|_| BnString::from_raw(result)) }
    }

    /// Write a global into the database
    pub fn write_global<K: BnStrCompatible, V: BnStrCompatible>(&self, key: K, value: V) -> bool {
        let key_raw = key.into_bytes_with_nul();
        let key_ptr = key_raw.as_ref().as_ptr() as *const ffi::c_char;
        let value_raw = value.into_bytes_with_nul();
        let value_ptr = value_raw.as_ref().as_ptr() as *const ffi::c_char;
        unsafe { BNWriteDatabaseGlobal(self.as_raw(), key_ptr, value_ptr) }
    }

    /// Get a specific global by key, as a binary buffer
    pub fn read_global_data<S: BnStrCompatible>(&self, key: S) -> Option<DataBuffer> {
        let key_raw = key.into_bytes_with_nul();
        let key_ptr = key_raw.as_ref().as_ptr() as *const ffi::c_char;
        let result = unsafe { BNReadDatabaseGlobalData(self.as_raw(), key_ptr) };
        ptr::NonNull::new(result).map(|_| DataBuffer::from_raw(result))
    }

    /// Write a binary buffer into a global in the database
    pub fn write_global_data<K: BnStrCompatible>(&self, key: K, value: &DataBuffer) -> bool {
        let key_raw = key.into_bytes_with_nul();
        let key_ptr = key_raw.as_ref().as_ptr() as *const ffi::c_char;
        unsafe { BNWriteDatabaseGlobalData(self.as_raw(), key_ptr, value.as_raw()) }
    }

    /// Get the owning FileMetadata
    pub fn file(&self) -> FileMetadata {
        let result = unsafe { BNGetDatabaseFile(self.as_raw()) };
        assert!(!result.is_null());
        FileMetadata::from_raw(result)
    }

    /// Get the backing analysis cache kvs
    pub fn analysis_cache(&self) -> KeyValueStore {
        let result = unsafe { BNReadDatabaseAnalysisCache(self.as_raw()) };
        unsafe { KeyValueStore::from_raw(ptr::NonNull::new(result).unwrap()) }
    }

    pub fn reload_connection(&self) {
        unsafe { BNDatabaseReloadConnection(self.as_raw()) }
    }

    pub fn write_analysis_cache(&self, val: &KeyValueStore) -> Result<(), ()> {
        if unsafe { BNWriteDatabaseAnalysisCache(self.as_raw(), val.as_raw()) } {
            Ok(())
        } else {
            Err(())
        }
    }

    pub fn snapshot_has_data(&self, id: i64) -> bool {
        unsafe { BNSnapshotHasData(self.as_raw(), id) }
    }
}

impl Clone for Database {
    fn clone(&self) -> Self {
        unsafe { Self::from_raw(ptr::NonNull::new(BNNewDatabaseReference(self.as_raw())).unwrap()) }
    }
}

impl Drop for Database {
    fn drop(&mut self) {
        unsafe { BNFreeDatabase(self.as_raw()) }
    }
}

#[repr(transparent)]
pub struct Snapshot {
    handle: ptr::NonNull<BNSnapshot>,
}

impl Snapshot {
    pub(crate) unsafe fn from_raw(handle: ptr::NonNull<BNSnapshot>) -> Self {
        Self { handle }
    }

    pub(crate) unsafe fn ref_from_raw(handle: &*mut BNSnapshot) -> &Self {
        mem::transmute(handle)
    }

    #[allow(clippy::mut_from_ref)]
    pub(crate) unsafe fn as_raw(&self) -> &mut BNSnapshot {
        &mut *self.handle.as_ptr()
    }

    /// Get the owning database
    pub fn database(&self) -> Database {
        unsafe {
            Database::from_raw(ptr::NonNull::new(BNGetSnapshotDatabase(self.as_raw())).unwrap())
        }
    }

    /// Get the numerical id (read-only)
    pub fn id(&self) -> i64 {
        unsafe { BNGetSnapshotId(self.as_raw()) }
    }

    /// Get the displayed snapshot name
    pub fn name(&self) -> BnString {
        unsafe { BnString::from_raw(BNGetSnapshotName(self.as_raw())) }
    }

    /// Set the displayed snapshot name
    pub fn set_name<S: BnStrCompatible>(&self, value: S) {
        let value_raw = value.into_bytes_with_nul();
        let value_ptr = value_raw.as_ref().as_ptr() as *const ffi::c_char;
        unsafe { BNSetSnapshotName(self.as_raw(), value_ptr) }
    }

    /// If the snapshot was the result of an auto-save
    pub fn is_auto_save(&self) -> bool {
        unsafe { BNIsSnapshotAutoSave(self.as_raw()) }
    }

    /// If the snapshot has contents, and has not been trimmed
    pub fn has_contents(&self) -> bool {
        unsafe { BNSnapshotHasContents(self.as_raw()) }
    }

    /// If the snapshot has undo data
    pub fn has_undo(&self) -> bool {
        unsafe { BNSnapshotHasUndo(self.as_raw()) }
    }

    /// Get the first parent of the snapshot, or None if it has no parents
    pub fn first_parent(&self) -> Option<Snapshot> {
        let result = unsafe { BNGetSnapshotFirstParent(self.as_raw()) };
        ptr::NonNull::new(result).map(|s| unsafe { Snapshot::from_raw(s) })
    }

    /// Get a list of all parent snapshots of the snapshot
    pub fn parents(&self) -> Array<Snapshot> {
        let mut count = 0;
        let result = unsafe { BNGetSnapshotParents(self.as_raw(), &mut count) };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Get a list of all child snapshots of the snapshot
    pub fn children(&self) -> Array<Snapshot> {
        let mut count = 0;
        let result = unsafe { BNGetSnapshotChildren(self.as_raw(), &mut count) };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Get a buffer of the raw data at the time of the snapshot
    pub fn file_contents(&self) -> Option<DataBuffer> {
        self.has_contents().then(|| unsafe {
            let result = BNGetSnapshotFileContents(self.as_raw());
            assert!(!result.is_null());
            DataBuffer::from_raw(result)
        })
    }

    /// Get a hash of the data at the time of the snapshot
    pub fn file_contents_hash(&self) -> Option<DataBuffer> {
        self.has_contents().then(|| unsafe {
            let result = BNGetSnapshotFileContentsHash(self.as_raw());
            assert!(!result.is_null());
            DataBuffer::from_raw(result)
        })
    }

    /// Get a list of undo entries at the time of the snapshot
    pub fn undo_entries(&self) -> Array<UndoEntry> {
        assert!(self.has_undo());
        let mut count = 0;
        let result = unsafe { BNGetSnapshotUndoEntries(self.as_raw(), &mut count) };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    pub fn undo_entries_with_progress<F: FnMut(usize, usize) -> bool>(
        &self,
        mut progress: F,
    ) -> Array<UndoEntry> {
        assert!(self.has_undo());
        let ctxt = &mut progress as *mut _ as *mut ffi::c_void;
        let mut count = 0;
        let result = unsafe {
            BNGetSnapshotUndoEntriesWithProgress(
                self.as_raw(),
                ctxt,
                Some(cb_progress::<F>),
                &mut count,
            )
        };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Get the backing kvs data with snapshot fields
    pub fn read_data(&self) -> KeyValueStore {
        let result = unsafe { BNReadSnapshotData(self.as_raw()) };
        unsafe { KeyValueStore::from_raw(ptr::NonNull::new(result).unwrap()) }
    }

    pub fn read_data_with_progress<F: FnMut(usize, usize) -> bool>(
        &self,
        mut progress: F,
    ) -> KeyValueStore {
        let ctxt = &mut progress as *mut _ as *mut ffi::c_void;
        let result =
            unsafe { BNReadSnapshotDataWithProgress(self.as_raw(), ctxt, Some(cb_progress::<F>)) };
        unsafe { KeyValueStore::from_raw(ptr::NonNull::new(result).unwrap()) }
    }

    pub fn undo_data(&self) -> DataBuffer {
        let result = unsafe { BNGetSnapshotUndoData(self.as_raw()) };
        assert!(!result.is_null());
        DataBuffer::from_raw(result)
    }

    pub fn store_data<F: FnMut(usize, usize) -> bool>(
        &self,
        data: KeyValueStore,
        mut progress: F,
    ) -> bool {
        let ctxt = &mut progress as *mut _ as *mut ffi::c_void;
        unsafe { BNSnapshotStoreData(self.as_raw(), data.as_raw(), ctxt, Some(cb_progress::<F>)) }
    }

    /// Determine if this snapshot has another as an ancestor
    pub fn has_ancestor(self, other: &Snapshot) -> bool {
        unsafe { BNSnapshotHasAncestor(self.as_raw(), other.as_raw()) }
    }
}

impl Clone for Snapshot {
    fn clone(&self) -> Self {
        unsafe { Self::from_raw(ptr::NonNull::new(BNNewSnapshotReference(self.as_raw())).unwrap()) }
    }
}

impl Drop for Snapshot {
    fn drop(&mut self) {
        unsafe { BNFreeSnapshot(self.as_raw()) }
    }
}

impl CoreArrayProvider for Snapshot {
    type Raw = *mut BNSnapshot;
    type Context = ();
    type Wrapped<'a> = &'a Self;
}

unsafe impl CoreArrayProviderInner for Snapshot {
    unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
        BNFreeSnapshotList(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 KeyValueStore {
    handle: ptr::NonNull<BNKeyValueStore>,
}

impl KeyValueStore {
    pub(crate) unsafe fn from_raw(handle: ptr::NonNull<BNKeyValueStore>) -> Self {
        Self { handle }
    }

    #[allow(clippy::mut_from_ref)]
    pub(crate) unsafe fn as_raw(&self) -> &mut BNKeyValueStore {
        &mut *self.handle.as_ptr()
    }

    /// Get a list of all keys stored in the kvs
    pub fn keys(&self) -> Array<BnString> {
        let mut count = 0;
        let result = unsafe { BNGetKeyValueStoreKeys(self.as_raw(), &mut count) };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Get the value for a single key
    pub fn value<S: BnStrCompatible>(&self, key: S) -> Option<DataBuffer> {
        let key_raw = key.into_bytes_with_nul();
        let key_ptr = key_raw.as_ref().as_ptr() as *const ffi::c_char;
        let result = unsafe { BNGetKeyValueStoreBuffer(self.as_raw(), key_ptr) };
        ptr::NonNull::new(result).map(|_| DataBuffer::from_raw(result))
    }

    /// Set the value for a single key
    pub fn set_value<S: BnStrCompatible>(&self, key: S, value: &DataBuffer) -> bool {
        let key_raw = key.into_bytes_with_nul();
        let key_ptr = key_raw.as_ref().as_ptr() as *const ffi::c_char;
        unsafe { BNSetKeyValueStoreBuffer(self.as_raw(), key_ptr, value.as_raw()) }
    }

    /// Get the stored representation of the kvs
    pub fn serialized_data(&self) -> DataBuffer {
        let result = unsafe { BNGetKeyValueStoreSerializedData(self.as_raw()) };
        assert!(!result.is_null());
        DataBuffer::from_raw(result)
    }

    /// Begin storing new keys into a namespace
    pub fn begin_namespace<S: BnStrCompatible>(&self, name: S) {
        let name_raw = name.into_bytes_with_nul();
        let name_ptr = name_raw.as_ref().as_ptr() as *const ffi::c_char;
        unsafe { BNBeginKeyValueStoreNamespace(self.as_raw(), name_ptr) }
    }

    /// End storing new keys into a namespace
    pub fn end_namespace(&self) {
        unsafe { BNEndKeyValueStoreNamespace(self.as_raw()) }
    }

    /// If the kvs is empty
    pub fn empty(&self) -> bool {
        unsafe { BNIsKeyValueStoreEmpty(self.as_raw()) }
    }

    /// Number of values in the kvs
    pub fn value_size(&self) -> usize {
        unsafe { BNGetKeyValueStoreValueSize(self.as_raw()) }
    }

    /// Length of serialized data
    pub fn data_size(&self) -> usize {
        unsafe { BNGetKeyValueStoreDataSize(self.as_raw()) }
    }

    /// Size of all data in storage
    pub fn value_storage_size(self) -> usize {
        unsafe { BNGetKeyValueStoreValueStorageSize(self.as_raw()) }
    }

    /// Number of namespaces pushed with begin_namespace
    pub fn namespace_size(self) -> usize {
        unsafe { BNGetKeyValueStoreNamespaceSize(self.as_raw()) }
    }
}

impl Clone for KeyValueStore {
    fn clone(&self) -> Self {
        unsafe {
            Self::from_raw(ptr::NonNull::new(BNNewKeyValueStoreReference(self.as_raw())).unwrap())
        }
    }
}

impl Drop for KeyValueStore {
    fn drop(&mut self) {
        unsafe { BNFreeKeyValueStore(self.as_raw()) }
    }
}

#[repr(transparent)]
pub struct UndoEntry {
    handle: ptr::NonNull<BNUndoEntry>,
}

impl UndoEntry {
    pub(crate) unsafe fn from_raw(handle: ptr::NonNull<BNUndoEntry>) -> Self {
        Self { handle }
    }

    pub(crate) unsafe fn ref_from_raw(handle: &*mut BNUndoEntry) -> &Self {
        mem::transmute(handle)
    }

    #[allow(clippy::mut_from_ref)]
    pub(crate) unsafe fn as_raw(&self) -> &mut BNUndoEntry {
        &mut *self.handle.as_ptr()
    }

    pub fn id(&self) -> BnString {
        let result = unsafe { BNUndoEntryGetId(self.as_raw()) };
        assert!(!result.is_null());
        unsafe { BnString::from_raw(result) }
    }

    pub fn actions(&self) -> Array<UndoAction> {
        let mut count = 0;
        let result = unsafe { BNUndoEntryGetActions(self.as_raw(), &mut count) };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    pub fn time(&self) -> SystemTime {
        let m = Duration::from_secs(unsafe { BNUndoEntryGetTimestamp(self.as_raw()) });
        UNIX_EPOCH + m
    }
}

impl Clone for UndoEntry {
    fn clone(&self) -> Self {
        unsafe {
            Self::from_raw(ptr::NonNull::new(BNNewUndoEntryReference(self.as_raw())).unwrap())
        }
    }
}

impl Drop for UndoEntry {
    fn drop(&mut self) {
        unsafe { BNFreeUndoEntry(self.as_raw()) }
    }
}

impl CoreArrayProvider for UndoEntry {
    type Raw = *mut BNUndoEntry;
    type Context = ();
    type Wrapped<'a> = &'a Self;
}

unsafe impl CoreArrayProviderInner for UndoEntry {
    unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
        BNFreeUndoEntryList(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 UndoAction {
    handle: ptr::NonNull<BNUndoAction>,
}

impl UndoAction {
    pub(crate) unsafe fn from_raw(handle: ptr::NonNull<BNUndoAction>) -> Self {
        Self { handle }
    }

    pub(crate) unsafe fn ref_from_raw(handle: &*mut BNUndoAction) -> &Self {
        mem::transmute(handle)
    }

    #[allow(clippy::mut_from_ref)]
    pub(crate) unsafe fn as_raw(&self) -> &mut BNUndoAction {
        &mut *self.handle.as_ptr()
    }

    pub fn summary_text(&self) -> BnString {
        let result = unsafe { BNUndoActionGetSummaryText(self.as_raw()) };
        assert!(!result.is_null());
        unsafe { BnString::from_raw(result) }
    }

    pub fn summary(&self) -> Array<InstructionTextToken> {
        let mut count = 0;
        let result = unsafe { BNUndoActionGetSummary(self.as_raw(), &mut count) };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }
}

impl Clone for UndoAction {
    fn clone(&self) -> Self {
        unsafe {
            Self::from_raw(ptr::NonNull::new(BNNewUndoActionReference(self.as_raw())).unwrap())
        }
    }
}

impl Drop for UndoAction {
    fn drop(&mut self) {
        unsafe { BNFreeUndoAction(self.as_raw()) }
    }
}

impl CoreArrayProvider for UndoAction {
    type Raw = *mut BNUndoAction;
    type Context = ();
    type Wrapped<'a> = &'a Self;
}

unsafe impl CoreArrayProviderInner for UndoAction {
    unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
        BNFreeUndoActionList(raw, count);
    }

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

unsafe extern "C" fn cb_progress<F: FnMut(usize, usize) -> bool>(
    ctxt: *mut ffi::c_void,
    arg1: usize,
    arg2: usize,
) -> bool {
    let ctxt: &mut F = &mut *(ctxt as *mut F);
    ctxt(arg1, arg2)
}

unsafe extern "C" fn cb_progress_nop(_ctxt: *mut ffi::c_void, _arg1: usize, _arg2: usize) -> bool {
    true
}