binaryninja/collaboration/
snapshot.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
use std::ffi::{c_char, c_void};
use std::ptr::NonNull;
use std::time::SystemTime;

use super::{sync, Remote, RemoteFile, RemoteProject};
use crate::binary_view::{BinaryView, BinaryViewExt};
use crate::collaboration::undo::{RemoteUndoEntry, RemoteUndoEntryId};
use crate::database::snapshot::Snapshot;
use crate::progress::{NoProgressCallback, ProgressCallback};
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable};
use crate::string::{BnStrCompatible, BnString};
use binaryninjacore_sys::*;

// TODO: RemoteSnapshotId ?

#[repr(transparent)]
pub struct RemoteSnapshot {
    pub(crate) handle: NonNull<BNCollaborationSnapshot>,
}

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

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

    /// Get the remote snapshot associated with a local snapshot (if it exists)
    pub fn get_for_local_snapshot(snapshot: &Snapshot) -> Result<Option<Ref<RemoteSnapshot>>, ()> {
        sync::get_remote_snapshot_from_local(snapshot)
    }

    /// Owning File
    pub fn file(&self) -> Result<Ref<RemoteFile>, ()> {
        let result = unsafe { BNCollaborationSnapshotGetFile(self.handle.as_ptr()) };
        let raw = NonNull::new(result).ok_or(())?;
        Ok(unsafe { RemoteFile::ref_from_raw(raw) })
    }

    /// Owning Project
    pub fn project(&self) -> Result<Ref<RemoteProject>, ()> {
        let result = unsafe { BNCollaborationSnapshotGetProject(self.handle.as_ptr()) };
        let raw = NonNull::new(result).ok_or(())?;
        Ok(unsafe { RemoteProject::ref_from_raw(raw) })
    }

    /// Owning Remote
    pub fn remote(&self) -> Result<Ref<Remote>, ()> {
        let result = unsafe { BNCollaborationSnapshotGetRemote(self.handle.as_ptr()) };
        let raw = NonNull::new(result).ok_or(())?;
        Ok(unsafe { Remote::ref_from_raw(raw) })
    }

    /// Web api endpoint url
    pub fn url(&self) -> BnString {
        let value = unsafe { BNCollaborationSnapshotGetUrl(self.handle.as_ptr()) };
        assert!(!value.is_null());
        unsafe { BnString::from_raw(value) }
    }

    /// Unique id
    pub fn id(&self) -> BnString {
        let value = unsafe { BNCollaborationSnapshotGetId(self.handle.as_ptr()) };
        assert!(!value.is_null());
        unsafe { BnString::from_raw(value) }
    }

    /// Name of snapshot
    pub fn name(&self) -> BnString {
        let value = unsafe { BNCollaborationSnapshotGetName(self.handle.as_ptr()) };
        assert!(!value.is_null());
        unsafe { BnString::from_raw(value) }
    }

    /// Get the title of a snapshot: the first line of its name
    pub fn title(&self) -> BnString {
        let value = unsafe { BNCollaborationSnapshotGetTitle(self.handle.as_ptr()) };
        assert!(!value.is_null());
        unsafe { BnString::from_raw(value) }
    }

    /// Get the description of a snapshot: the lines of its name after the first line
    pub fn description(&self) -> BnString {
        let value = unsafe { BNCollaborationSnapshotGetDescription(self.handle.as_ptr()) };
        assert!(!value.is_null());
        unsafe { BnString::from_raw(value) }
    }

    /// Get the user id of the author of a snapshot
    pub fn author(&self) -> BnString {
        let value = unsafe { BNCollaborationSnapshotGetAuthor(self.handle.as_ptr()) };
        assert!(!value.is_null());
        unsafe { BnString::from_raw(value) }
    }

    /// Get the username of the author of a snapshot, if possible (vs author which is user id)
    pub fn author_username(&self) -> BnString {
        let value = unsafe { BNCollaborationSnapshotGetAuthorUsername(self.handle.as_ptr()) };
        assert!(!value.is_null());
        unsafe { BnString::from_raw(value) }
    }

    /// Created date of Snapshot
    pub fn created(&self) -> SystemTime {
        let timestamp = unsafe { BNCollaborationSnapshotGetCreated(self.handle.as_ptr()) };
        crate::ffi::time_from_bn(timestamp.try_into().unwrap())
    }

    /// Date of last modification to the snapshot
    pub fn last_modified(&self) -> SystemTime {
        let timestamp = unsafe { BNCollaborationSnapshotGetLastModified(self.handle.as_ptr()) };
        crate::ffi::time_from_bn(timestamp.try_into().unwrap())
    }

    /// Hash of snapshot data (analysis and markup, etc)
    /// No specific hash algorithm is guaranteed
    pub fn hash(&self) -> BnString {
        let value = unsafe { BNCollaborationSnapshotGetHash(self.handle.as_ptr()) };
        assert!(!value.is_null());
        unsafe { BnString::from_raw(value) }
    }

    /// Hash of file contents in snapshot
    /// No specific hash algorithm is guaranteed
    pub fn snapshot_file_hash(&self) -> BnString {
        let value = unsafe { BNCollaborationSnapshotGetSnapshotFileHash(self.handle.as_ptr()) };
        assert!(!value.is_null());
        unsafe { BnString::from_raw(value) }
    }

    /// If the snapshot has pulled undo entries yet
    pub fn has_pulled_undo_entries(&self) -> bool {
        unsafe { BNCollaborationSnapshotHasPulledUndoEntries(self.handle.as_ptr()) }
    }

    /// If the snapshot has been finalized on the server and is no longer editable
    pub fn is_finalized(&self) -> bool {
        unsafe { BNCollaborationSnapshotIsFinalized(self.handle.as_ptr()) }
    }

    /// List of ids of all remote parent Snapshots
    pub fn parent_ids(&self) -> Result<Array<BnString>, ()> {
        let mut count = 0;
        let raw = unsafe { BNCollaborationSnapshotGetParentIds(self.handle.as_ptr(), &mut count) };
        (!raw.is_null())
            .then(|| unsafe { Array::new(raw, count, ()) })
            .ok_or(())
    }

    /// List of ids of all remote child Snapshots
    pub fn child_ids(&self) -> Result<Array<BnString>, ()> {
        let mut count = 0;
        let raw = unsafe { BNCollaborationSnapshotGetChildIds(self.handle.as_ptr(), &mut count) };
        (!raw.is_null())
            .then(|| unsafe { Array::new(raw, count, ()) })
            .ok_or(())
    }

    /// List of all parent Snapshot objects
    pub fn parents(&self) -> Result<Array<RemoteSnapshot>, ()> {
        let mut count = 0;
        let raw = unsafe { BNCollaborationSnapshotGetParents(self.handle.as_ptr(), &mut count) };
        (!raw.is_null())
            .then(|| unsafe { Array::new(raw, count, ()) })
            .ok_or(())
    }

    /// List of all child Snapshot objects
    pub fn children(&self) -> Result<Array<RemoteSnapshot>, ()> {
        let mut count = 0;
        let raw = unsafe { BNCollaborationSnapshotGetChildren(self.handle.as_ptr(), &mut count) };
        (!raw.is_null())
            .then(|| unsafe { Array::new(raw, count, ()) })
            .ok_or(())
    }

    /// Get the list of undo entries stored in this snapshot.
    ///
    /// NOTE: If undo entries have not been pulled, they will be pulled upon calling this.
    pub fn undo_entries(&self) -> Result<Array<RemoteUndoEntry>, ()> {
        if !self.has_pulled_undo_entries() {
            self.pull_undo_entries()?;
        }
        let mut count = 0;
        let raw =
            unsafe { BNCollaborationSnapshotGetUndoEntries(self.handle.as_ptr(), &mut count) };
        (!raw.is_null())
            .then(|| unsafe { Array::new(raw, count, ()) })
            .ok_or(())
    }

    /// Get a specific Undo Entry in the Snapshot by its id
    ///
    /// NOTE: If undo entries have not been pulled, they will be pulled upon calling this.
    pub fn get_undo_entry_by_id(
        &self,
        id: RemoteUndoEntryId,
    ) -> Result<Option<Ref<RemoteUndoEntry>>, ()> {
        if !self.has_pulled_undo_entries() {
            self.pull_undo_entries()?;
        }
        let raw = unsafe { BNCollaborationSnapshotGetUndoEntryById(self.handle.as_ptr(), id.0) };
        Ok(NonNull::new(raw).map(|handle| unsafe { RemoteUndoEntry::ref_from_raw(handle) }))
    }

    /// Pull the list of Undo Entries from the Remote.
    pub fn pull_undo_entries(&self) -> Result<(), ()> {
        self.pull_undo_entries_with_progress(NoProgressCallback)
    }

    /// Pull the list of Undo Entries from the Remote.
    pub fn pull_undo_entries_with_progress<P: ProgressCallback>(
        &self,
        mut progress: P,
    ) -> Result<(), ()> {
        let success = unsafe {
            BNCollaborationSnapshotPullUndoEntries(
                self.handle.as_ptr(),
                Some(P::cb_progress_callback),
                &mut progress as *mut P as *mut c_void,
            )
        };
        success.then_some(()).ok_or(())
    }

    /// Create a new Undo Entry in this snapshot.
    pub fn create_undo_entry<S: BnStrCompatible>(
        &self,
        parent: Option<u64>,
        data: S,
    ) -> Result<Ref<RemoteUndoEntry>, ()> {
        let data = data.into_bytes_with_nul();
        let value = unsafe {
            BNCollaborationSnapshotCreateUndoEntry(
                self.handle.as_ptr(),
                parent.is_some(),
                parent.unwrap_or(0),
                data.as_ref().as_ptr() as *const c_char,
            )
        };
        let handle = NonNull::new(value).ok_or(())?;
        Ok(unsafe { RemoteUndoEntry::ref_from_raw(handle) })
    }

    /// Mark a snapshot as Finalized, committing it to the Remote, preventing future updates,
    /// and allowing snapshots to be children of it.
    pub fn finalize(&self) -> Result<(), ()> {
        let success = unsafe { BNCollaborationSnapshotFinalize(self.handle.as_ptr()) };
        success.then_some(()).ok_or(())
    }

    // TODO what kind of struct is this and how to free it?
    ///// Download the contents of the file in the Snapshot.
    //pub fn download_snapshot_file<P: ProgressCallback>(
    //    &self,
    //    mut progress: P,
    //) -> Result<BnData, ()> {
    //    let mut data = ptr::null_mut();
    //    let mut count = 0;
    //    let success = unsafe {
    //        BNCollaborationSnapshotDownloadSnapshotFile(
    //            self.handle.as_ptr(),
    //            Some(P::cb_progress_callback),
    //            &mut progress as *mut P as *mut ffi::c_void,
    //            &mut data,
    //            &mut count,
    //        )
    //    };
    //    todo!();
    //}
    //
    /////  Download the snapshot fields blob, compatible with KeyValueStore.
    //pub fn download<P: ProgressCallback>(
    //    &self,
    //    mut progress: P,
    //) -> Result<BnData, ()> {
    //    let mut data = ptr::null_mut();
    //    let mut count = 0;
    //    let success = unsafe {
    //        BNCollaborationSnapshotDownload(
    //            self.handle.as_ptr(),
    //            Some(P::cb_progress_callback),
    //            &mut progress as *mut P as *mut ffi::c_void,
    //            &mut data,
    //            &mut count,
    //        )
    //    };
    //    todo!();
    //}
    //
    ///// Download the analysis cache fields blob, compatible with KeyValueStore.
    //pub fn download_analysis_cache<P: ProgressCallback>(
    //    &self,
    //    mut progress: P,
    //) -> Result<BnData, ()> {
    //    let mut data = ptr::null_mut();
    //    let mut count = 0;
    //    let success = unsafe {
    //        BNCollaborationSnapshotDownloadAnalysisCache(
    //            self.handle.as_ptr(),
    //            Some(P::cb_progress_callback),
    //            &mut progress as *mut P as *mut ffi::c_void,
    //            &mut data,
    //            &mut count,
    //        )
    //    };
    //    todo!();
    //}

    /// Get the local snapshot associated with a remote snapshot (if it exists)
    pub fn get_local_snapshot(&self, bv: &BinaryView) -> Result<Option<Ref<Snapshot>>, ()> {
        let Some(db) = bv.file().database() else {
            return Ok(None);
        };
        sync::get_local_snapshot_for_remote(self, &db)
    }

    pub fn analysis_cache_build_id(&self) -> u64 {
        unsafe { BNCollaborationSnapshotGetAnalysisCacheBuildId(self.handle.as_ptr()) }
    }
}

impl PartialEq for RemoteSnapshot {
    fn eq(&self, other: &Self) -> bool {
        self.id() == other.id()
    }
}
impl Eq for RemoteSnapshot {}

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

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

unsafe impl RefCountable for RemoteSnapshot {
    unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
        Ref::new(Self {
            handle: NonNull::new(BNNewCollaborationSnapshotReference(handle.handle.as_ptr()))
                .unwrap(),
        })
    }

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

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

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

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