FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
binaryninja-api/database.cpp at dev · Vector35/binaryninja-api · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Vector35
/
binaryninja-api
Public
Notifications
You must be signed in to change notification settings
Fork
294
Star
1.3k
Code
Issues
1.9k
Pull requests
35
Discussions
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
binaryninja-api
/
database.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
574 lines (448 loc) · 12.6 KB
Breadcrumbs
binaryninja-api
/
database.cpp
Copy path
File metadata and controls
574 lines (448 loc) · 12.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
Copyright (c) 2015-2026 Vector 35 Inc
//
//
Permission is hereby granted, free of charge, to any person obtaining a copy
//
of this software and associated documentation files (the "Software"), to
//
deal in the Software without restriction, including without limitation the
//
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
//
sell copies of the Software, and to permit persons to whom the Software is
//
furnished to do so, subject to the following conditions:
//
//
The above copyright notice and this permission notice shall be included in
//
all copies or substantial portions of the Software.
//
//
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
//
IN THE SOFTWARE.
#
include
<
cstring
>
#
include
"
binaryninjaapi.h
"
using
namespace
BinaryNinja
;
using
namespace
Json
;
using
namespace
std
;
KeyValueStore::KeyValueStore
()
{
m_object =
BNCreateKeyValueStore
();
}
KeyValueStore::KeyValueStore
(
const
DataBuffer& buffer)
{
m_object =
BNCreateKeyValueStoreFromDataBuffer
(buffer.
GetBufferObject
());
}
KeyValueStore::KeyValueStore
(BNKeyValueStore* store)
{
m_object = store;
}
std::vector<std::string>
KeyValueStore::GetKeys
()
const
{
size_t
count;
char
** keys =
BNGetKeyValueStoreKeys
(m_object, &count);
std::vector<std::string> strings;
strings.
reserve
(count);
for
(
size_t
i =
0
; i < count; ++i)
{
strings.
push_back
(keys[i]);
}
BNFreeStringList
(keys, count);
return
strings;
}
bool
KeyValueStore::HasValue
(
const
std::string& name)
const
{
return
BNKeyValueStoreHasValue
(m_object, name.
c_str
());
}
Json::Value
KeyValueStore::GetValue
(
const
std::string& name)
const
{
BNDataBuffer* bnBuffer =
BNGetKeyValueStoreBuffer
(m_object, name.
c_str
());
if
(bnBuffer ==
nullptr
)
{
throw
DatabaseException
(
"
BNGetKeyValueStoreBuffer
"
);
}
DataBuffer value =
DataBuffer
(bnBuffer);
Json::Value json;
std::unique_ptr<Json::CharReader>
reader
(
Json::CharReaderBuilder
().
newCharReader
());
std::string errors;
if
(!reader->
parse
(
static_cast
<
const
char
*>(value.
GetData
()),
static_cast
<
const
char
*>(value.
GetDataAt
(value.
GetLength
())), &json, &errors))
{
throw
DatabaseException
(errors);
}
return
json;
}
DataBuffer
KeyValueStore::GetValueHash
(
const
std::string& name)
const
{
BNDataBuffer* buffer =
BNGetKeyValueStoreValueHash
(m_object, name.
c_str
());
if
(buffer ==
nullptr
)
{
throw
DatabaseException
(
"
Unknown key
"
);
}
return
DataBuffer
(buffer);
}
DataBuffer
KeyValueStore::GetBuffer
(
const
std::string& name)
const
{
BNDataBuffer* buffer =
BNGetKeyValueStoreBuffer
(m_object, name.
c_str
());
if
(buffer ==
nullptr
)
{
throw
DatabaseException
(
"
Unknown key
"
);
}
return
DataBuffer
(buffer);
}
void
KeyValueStore::SetValue
(
const
std::string& name,
const
Json::Value& value)
{
Json::StreamWriterBuilder builder;
builder[
"
indentation
"
] =
"
"
;
string json =
Json::writeString
(builder, value);
if
(!
BNSetKeyValueStoreValue
(m_object, name.
c_str
(), json.
c_str
()))
{
throw
DatabaseException
(
"
BNSetKeyValueStoreValue
"
);
}
}
void
KeyValueStore::SetBuffer
(
const
std::string& name,
const
DataBuffer& value)
{
if
(!
BNSetKeyValueStoreBuffer
(m_object, name.
c_str
(), value.
GetBufferObject
()))
{
throw
DatabaseException
(
"
BNSetKeyValueStoreBuffer
"
);
}
}
DataBuffer
KeyValueStore::GetSerializedData
()
const
{
return
DataBuffer
(
BNGetKeyValueStoreSerializedData
(m_object));
}
void
KeyValueStore::BeginNamespace
(
const
std::string& name)
{
BNBeginKeyValueStoreNamespace
(m_object, name.
c_str
());
}
void
KeyValueStore::EndNamespace
()
{
BNEndKeyValueStoreNamespace
(m_object);
}
bool
KeyValueStore::IsEmpty
()
const
{
return
BNIsKeyValueStoreEmpty
(m_object);
}
size_t
KeyValueStore::ValueSize
()
const
{
return
BNGetKeyValueStoreValueSize
(m_object);
}
size_t
KeyValueStore::DataSize
()
const
{
return
BNGetKeyValueStoreDataSize
(m_object);
}
size_t
KeyValueStore::ValueStorageSize
()
const
{
return
BNGetKeyValueStoreValueStorageSize
(m_object);
}
size_t
KeyValueStore::NamespaceSize
()
const
{
return
BNGetKeyValueStoreNamespaceSize
(m_object);
}
Snapshot::Snapshot
(BNSnapshot* snapshot)
{
m_object = snapshot;
}
Ref<Database>
Snapshot::GetDatabase
()
{
return
new
Database
(
BNGetSnapshotDatabase
(m_object));
}
int64_t
Snapshot::GetId
()
{
return
BNGetSnapshotId
(m_object);
}
std::string
Snapshot::GetName
()
{
char
* cstr =
BNGetSnapshotName
(m_object);
std::string str {cstr};
BNFreeString
(cstr);
return
str;
}
void
Snapshot::SetName
(
const
std::string& name)
{
BNSetSnapshotName
(m_object, name.
c_str
());
}
bool
Snapshot::IsAutoSave
()
{
return
BNIsSnapshotAutoSave
(m_object);
}
bool
Snapshot::HasContents
()
{
return
BNSnapshotHasContents
(m_object);
}
bool
Snapshot::HasData
()
{
return
GetDatabase
()->
SnapshotHasData
(
GetId
());
}
bool
Snapshot::HasUndo
()
{
return
BNSnapshotHasUndo
(m_object);
}
Ref<Snapshot>
Snapshot::GetFirstParent
()
{
BNSnapshot* snap =
BNGetSnapshotFirstParent
(m_object);
if
(snap ==
nullptr
)
return
nullptr
;
return
new
Snapshot
(snap);
}
vector<Ref<Snapshot>>
Snapshot::GetParents
()
{
size_t
count;
BNSnapshot** parents =
BNGetSnapshotParents
(m_object, &count);
vector<Ref<Snapshot>> result;
result.
reserve
(count);
for
(
size_t
i =
0
; i < count; i++)
{
result.
push_back
(
new
Snapshot
(
BNNewSnapshotReference
(parents[i])));
}
BNFreeSnapshotList
(parents, count);
return
result;
}
vector<Ref<Snapshot>>
Snapshot::GetChildren
()
{
size_t
count;
BNSnapshot** children =
BNGetSnapshotChildren
(m_object, &count);
vector<Ref<Snapshot>> result;
result.
reserve
(count);
for
(
size_t
i =
0
; i < count; i++)
{
result.
push_back
(
new
Snapshot
(
BNNewSnapshotReference
(children[i])));
}
BNFreeSnapshotList
(children, count);
return
result;
}
DataBuffer
Snapshot::GetFileContents
()
{
BNDataBuffer* buffer =
BNGetSnapshotFileContents
(m_object);
if
(buffer ==
nullptr
)
{
throw
DatabaseException
(
"
BNGetSnapshotFileContents
"
);
}
return
DataBuffer
(buffer);
}
DataBuffer
Snapshot::GetFileContentsHash
()
{
BNDataBuffer* buffer =
BNGetSnapshotFileContentsHash
(m_object);
if
(buffer ==
nullptr
)
{
throw
DatabaseException
(
"
BNGetSnapshotFileContentsHash
"
);
}
return
DataBuffer
(buffer);
}
DataBuffer
Snapshot::GetUndoData
()
{
BNDataBuffer* buffer =
BNGetSnapshotUndoData
(m_object);
if
(buffer ==
nullptr
)
{
throw
DatabaseException
(
"
BNGetSnapshotUndoData
"
);
}
return
DataBuffer
(buffer);
}
vector<Ref<UndoEntry>>
Snapshot::GetUndoEntries
(Ref<FileMetadata> file)
{
return
GetUndoEntries
(file, [](
size_t
,
size_t
) {
return
true
; });
}
vector<Ref<UndoEntry>>
Snapshot::GetUndoEntries
(Ref<FileMetadata> file,
const
ProgressFunction& progress)
{
ProgressContext pctxt;
pctxt.
callback
= progress;
size_t
count;
BNUndoEntry** entries =
BNGetSnapshotUndoEntriesWithProgress
(m_object, file->
GetObject
(), &pctxt, ProgressCallback, &count);
if
(entries ==
nullptr
)
{
throw
DatabaseException
(
"
BNGetSnapshotUndoEntriesWithProgress
"
);
}
vector<Ref<UndoEntry>> result;
result.
reserve
(count);
for
(
size_t
i =
0
; i < count; i++)
{
result.
push_back
(
new
UndoEntry
(
BNNewUndoEntryReference
(entries[i])));
}
BNFreeUndoEntryList
(entries, count);
return
result;
}
Ref<KeyValueStore>
Snapshot::ReadData
()
{
return
ReadData
([](
size_t
,
size_t
) {
return
true
; });
}
Ref<KeyValueStore>
Snapshot::ReadData
(
const
ProgressFunction& progress)
{
ProgressContext pctxt;
pctxt.
callback
= progress;
BNKeyValueStore* store =
BNReadSnapshotDataWithProgress
(m_object, &pctxt, ProgressCallback);
if
(store ==
nullptr
)
{
throw
DatabaseException
(
"
BNReadSnapshotData
"
);
}
return
new
KeyValueStore
(store);
}
bool
Snapshot::StoreData
(
const
Ref<KeyValueStore>& data,
const
ProgressFunction& progress)
{
ProgressContext pctxt;
pctxt.
callback
= progress;
bool
result =
BNSnapshotStoreData
(m_object, data->
GetObject
(), &pctxt, ProgressCallback);
if
(!result)
{
throw
DatabaseException
(
"
BNSnapshotStoreData
"
);
}
return
result;
}
bool
Snapshot::HasAncestor
(Ref<Snapshot> other)
{
return
BNSnapshotHasAncestor
(m_object, other->
GetObject
());
}
Database::Database
(BNDatabase* database)
{
m_object = database;
}
Ref<Database>
Database::OpenExisting
(
const
std::string& path)
{
Ref<Database> db =
new
Database
(
BNCreateDatabaseInstance
());
if
(!
BNDatabaseOpenExisting
(db->
GetObject
(), path.
c_str
()))
throw
DatabaseException
(
"
BNDatabaseOpenExisting
"
);
return
db;
}
Ref<Snapshot>
Database::GetSnapshot
(
int64_t
id)
{
BNSnapshot* snap =
BNGetDatabaseSnapshot
(m_object, id);
if
(snap ==
nullptr
)
return
nullptr
;
return
new
Snapshot
(snap);
}
vector<Ref<Snapshot>>
Database::GetSnapshots
()
{
size_t
count;
BNSnapshot** snapshots =
BNGetDatabaseSnapshots
(m_object, &count);
vector<Ref<Snapshot>> result;
result.
reserve
(count);
for
(
size_t
i =
0
; i < count; i++)
result.
push_back
(
new
Snapshot
(
BNNewSnapshotReference
(snapshots[i])));
BNFreeSnapshotList
(snapshots, count);
return
result;
}
void
Database::SetCurrentSnapshot
(
int64_t
id)
{
BNSetDatabaseCurrentSnapshot
(m_object, id);
}
Ref<Snapshot>
Database::GetCurrentSnapshot
()
{
BNSnapshot* snap =
BNGetDatabaseCurrentSnapshot
(m_object);
if
(snap ==
nullptr
)
return
nullptr
;
return
new
Snapshot
(snap);
}
int64_t
Database::WriteSnapshotData
(std::vector<
int64_t
> parents, Ref<BinaryView> file,
const
std::string& name,
const
Ref<KeyValueStore>& data,
bool
autoSave,
const
ProgressFunction& progress)
{
ProgressContext pctxt;
pctxt.
callback
= progress;
int64_t
result =
BNWriteDatabaseSnapshotData
(m_object, parents.
data
(), parents.
size
(), file->
GetObject
(),
name.
c_str
(), data->
GetObject
(), autoSave, &pctxt, ProgressCallback);
if
(result <
0
)
{
throw
DatabaseException
(
"
BNWriteDatabaseSnapshotData
"
);
}
return
result;
}
void
Database::TrimSnapshot
(
int64_t
id)
{
if
(!
BNTrimDatabaseSnapshot
(m_object, id))
{
throw
DatabaseException
(
"
BNTrimDatabaseSnapshot
"
);
}
}
void
Database::RemoveSnapshot
(
int64_t
id)
{
if
(!
BNRemoveDatabaseSnapshot
(m_object, id))
{
throw
DatabaseException
(
"
BNRemoveDatabaseSnapshot
"
);
}
}
std::vector<std::string>
Database::GetGlobalKeys
()
const
{
size_t
count;
char
** value =
BNGetDatabaseGlobalKeys
(m_object, &count);
if
(value ==
nullptr
)
{
throw
DatabaseException
(
"
BNDatabaseHasGlobal
"
);
}
std::vector<std::string> result;
result.
reserve
(count);
for
(
size_t
i =
0
; i < count; i++)
{
result.
push_back
(value[i]);
}
BNFreeStringList
(value, count);
return
result;
}
bool
Database::HasGlobal
(
const
std::string& key)
const
{
//
0 - false, 1 - true, <0 - exception
int
value =
BNDatabaseHasGlobal
(m_object, key.
c_str
());
if
(value <
0
)
{
throw
DatabaseException
(
"
BNDatabaseHasGlobal
"
);
}
return
value ==
1
;
}
Json::Value
Database::ReadGlobal
(
const
std::string& key)
const
{
char
* value =
BNReadDatabaseGlobal
(m_object, key.
c_str
());
if
(value ==
nullptr
)
{
throw
DatabaseException
(
"
BNReadDatabaseGlobal
"
);
}
Json::Value json;
std::unique_ptr<Json::CharReader>
reader
(
Json::CharReaderBuilder
().
newCharReader
());
std::string errors;
if
(!reader->
parse
(value, value +
strlen
(value), &json, &errors))
{
throw
DatabaseException
(errors);
}
BNFreeString
(value);
return
json;
}
void
Database::WriteGlobal
(
const
std::string& key,
const
Json::Value& val)
{
Json::StreamWriterBuilder builder;
builder[
"
indentation
"
] =
"
"
;
string json =
Json::writeString
(builder, val);
if
(!
BNWriteDatabaseGlobal
(m_object, key.
c_str
(), json.
c_str
()))
{
throw
DatabaseException
(
"
BNWriteDatabaseGlobal
"
);
}
}
DataBuffer
Database::ReadGlobalData
(
const
std::string& key)
const
{
BNDataBuffer* value =
BNReadDatabaseGlobalData
(m_object, key.
c_str
());
if
(value ==
nullptr
)
{
throw
DatabaseException
(
"
BNReadDatabaseGlobalData
"
);
}
return
DataBuffer
(value);
}
void
Database::WriteGlobalData
(
const
std::string& key,
const
DataBuffer& val)
{
if
(!
BNWriteDatabaseGlobalData
(m_object, key.
c_str
(), val.
GetBufferObject
()))
{
throw
DatabaseException
(
"
BNWriteDatabaseGlobalData
"
);
}
}
void
Database::ReloadConnection
() {}
Ref<KeyValueStore>
Database::ReadAnalysisCache
()
const
{
BNKeyValueStore* store =
BNReadDatabaseAnalysisCache
(m_object);
if
(store ==
nullptr
)
{
throw
DatabaseException
(
"
BNReadDatabaseAnalysisCache
"
);
}
return
new
KeyValueStore
(store);
}
void
Database::WriteAnalysisCache
(Ref<KeyValueStore> val)
{
if
(!
BNWriteDatabaseAnalysisCache
(m_object, val->
GetObject
()))
{
throw
DatabaseException
(
"
BNWriteDatabaseAnalysisCache
"
);
}
}
bool
Database::SnapshotHasData
(
int64_t
id)
{
return
BNSnapshotHasData
(m_object, id);
}
Back
|
FazBrowse Home
|
New Git URL