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
|
#include <QtWidgets/QScrollBar>
#include <QtGui/QClipboard>
#include <QtGui/QGuiApplication>
#include <QtCore/QStringList>
#include <algorithm>
#include "exports.h"
#include "view.h"
#include "fontsettings.h"
const int OrdinalColumn = 0;
const int AddressColumn = 1;
const int NameColumn = 2;
const int ColumnCount = 3;
const int ColumnVisibleRole = Qt::UserRole;
GenericExportsModel::GenericExportsModel(QWidget* parent, BinaryViewRef data): QAbstractItemModel(parent), BinaryDataNotification(FunctionUpdates | SymbolUpdates)
{
m_sortOrder = Qt::AscendingOrder;
m_data = data;
m_hasOrdinals = false;
if (data->GetTypeName() == "PE")
{
m_hasOrdinals = true;
}
m_updateTimer = new QTimer(this);
m_updateTimer->setInterval(500);
connect(m_updateTimer, &QTimer::timeout, this, &GenericExportsModel::updateModel);
connect(this, &GenericExportsModel::updateTimerOnUIThread, this, [=, this]() {
updateTimer(m_needsUpdate);
});
m_data->RegisterNotification(this);
updateModel();
m_entries = m_allEntries;
}
GenericExportsModel::~GenericExportsModel()
{
m_data->UnregisterNotification(this);
}
void GenericExportsModel::updateModel()
{
if (!m_needsUpdate)
return;
setNeedsUpdate(false);
beginResetModel();
m_allEntries.clear();
for (auto& sym : m_data->GetSymbolsOfType(FunctionSymbol))
{
if ((sym->GetBinding() == GlobalBinding) || (sym->GetBinding() == WeakBinding))
m_allEntries.push_back(sym);
}
for (auto& sym : m_data->GetSymbolsOfType(DataSymbol))
{
if ((sym->GetBinding() == GlobalBinding) || (sym->GetBinding() == WeakBinding))
m_allEntries.push_back(sym);
}
endResetModel();
setFilter(m_filter);
}
int GenericExportsModel::columnCount(const QModelIndex&) const
{
return ColumnCount;
}
int GenericExportsModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid())
return 0;
return (int)m_entries.size();
}
QVariant GenericExportsModel::data(const QModelIndex& index, int role) const
{
switch (role)
{
case Qt::DisplayRole:
if (role != Qt::DisplayRole)
return QVariant();
if (!index.isValid() || index.row() >= (int)m_entries.size())
return QVariant();
if (index.column() == AddressColumn)
return QString("0x") + QString::number(m_entries[index.row()]->GetAddress(), 16);
if (index.column() == NameColumn)
return QString::fromStdString(m_entries[index.row()]->GetFullName());
if (index.column() == OrdinalColumn)
return QString::number(m_entries[index.row()]->GetOrdinal());
break;
case Qt::ForegroundRole:
if (index.column() == AddressColumn)
return getThemeColor(AddressColor);
if (index.column() == NameColumn)
return getThemeColor(ExportColor);
break;
default:
break;
}
return QVariant();
}
QVariant GenericExportsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Vertical)
return QVariant();
if (role == ColumnVisibleRole)
{
if (section == OrdinalColumn)
return QVariant(m_hasOrdinals);
return true;
}
if (role != Qt::DisplayRole)
return QVariant();
if (section == AddressColumn)
return QString("Address");
if (section == NameColumn)
return QString("Name");
if (section == OrdinalColumn)
return QString("Ordinal");
return QVariant();
}
QModelIndex GenericExportsModel::index(int row, int col, const QModelIndex& parent) const
{
if (parent.isValid())
return QModelIndex();
if (row >= (int)m_entries.size())
return QModelIndex();
if (col >= ColumnCount)
return QModelIndex();
return createIndex(row, col);
}
QModelIndex GenericExportsModel::parent(const QModelIndex&) const
{
return QModelIndex();
}
SymbolRef GenericExportsModel::getSymbol(const QModelIndex& index)
{
if (!index.isValid() || index.row() >= (int)m_entries.size())
return nullptr;
return m_entries[index.row()];
}
void GenericExportsModel::performSort(int col, Qt::SortOrder order)
{
std::sort(m_entries.begin(), m_entries.end(), [&](SymbolRef a, SymbolRef b) {
if (col == AddressColumn)
{
if (a->GetAddress() != b->GetAddress())
{
if (order == Qt::AscendingOrder)
return a->GetAddress() < b->GetAddress();
else
return a->GetAddress() > b->GetAddress();
}
if (order == Qt::AscendingOrder)
return a->GetFullName() < b->GetFullName();
else
return a->GetFullName() > b->GetFullName();
}
else if (col == NameColumn)
{
if (order == Qt::AscendingOrder)
return a->GetFullName() < b->GetFullName();
else
return a->GetFullName() > b->GetFullName();
}
else if (col == OrdinalColumn)
{
if (a->GetOrdinal() != b->GetOrdinal())
{
if (order == Qt::AscendingOrder)
return a->GetOrdinal() < b->GetOrdinal();
else
return a->GetOrdinal() > b->GetOrdinal();
}
if (a->GetAddress() != b->GetAddress())
{
if (order == Qt::AscendingOrder)
return a->GetAddress() < b->GetAddress();
else
return a->GetAddress() > b->GetAddress();
}
if (order == Qt::AscendingOrder)
return a->GetFullName() < b->GetFullName();
else
return a->GetFullName() > b->GetFullName();
}
return false;
});
}
void GenericExportsModel::sort(int col, Qt::SortOrder order)
{
beginResetModel();
m_sortCol = col;
m_sortOrder = order;
performSort(col, order);
endResetModel();
}
void GenericExportsModel::setFilter(const std::string& filterText)
{
m_filter = filterText;
beginResetModel();
m_entries.clear();
for (auto& entry : m_allEntries)
{
if (FilteredView::match(entry->GetFullName(), filterText))
m_entries.push_back(entry);
else if (FilteredView::match(std::to_string(entry->GetOrdinal()), filterText))
m_entries.push_back(entry);
}
performSort(m_sortCol, m_sortOrder);
endResetModel();
}
void GenericExportsModel::setNeedsUpdate(bool needed)
{
if (m_needsUpdate.exchange(needed) == needed)
return;
updateTimer(needed);
}
void GenericExportsModel::updateTimer(bool needsUpdate)
{
if (needsUpdate && !m_updateTimer->isActive())
m_updateTimer->start();
if (!needsUpdate && m_updateTimer->isActive())
m_updateTimer->stop();
}
void GenericExportsModel::pauseUpdates()
{
m_updatesPaused = true;
setNeedsUpdate(false);
}
void GenericExportsModel::resumeUpdates()
{
m_updatesPaused = false;
setNeedsUpdate(true);
}
void GenericExportsModel::onBinaryViewNotification()
{
if (m_updatesPaused)
return;
// This can be called from any thread so we cannot directly
// update the timer. Emitting a signal is relatively expensive
// given how frequently we receive notifications, so we only
// emit a signal if we didn't already need an update.
if (!m_needsUpdate.exchange(true))
emit updateTimerOnUIThread();
}
void GenericExportsModel::OnSymbolAdded(BinaryNinja::BinaryView* view, BinaryNinja::Symbol* sym)
{
if ((sym->GetBinding() == GlobalBinding) || (sym->GetBinding() == WeakBinding))
onBinaryViewNotification();
}
void GenericExportsModel::OnSymbolUpdated(BinaryNinja::BinaryView* view, BinaryNinja::Symbol* sym)
{
onBinaryViewNotification();
}
void GenericExportsModel::OnSymbolRemoved(BinaryNinja::BinaryView* view, BinaryNinja::Symbol* sym)
{
onBinaryViewNotification();
}
ExportsTreeView::ExportsTreeView(ExportsWidget* parent, TriageView* view, BinaryViewRef data) : QTreeView(parent)
{
m_data = data;
m_parent = parent;
m_view = view;
m_selection.clear();
m_scroll = 0;
// Allow view-specific shortcuts when imports are focused
m_actionHandler.setupActionHandler(this);
m_actionHandler.setActionContext([=, this]() { return m_view->actionContext(); });
setFont(getMonospaceFont(this));
m_model = new GenericExportsModel(this, m_data);
setModel(m_model);
setRootIsDecorated(false);
setUniformRowHeights(true);
setSortingEnabled(true);
setSelectionMode(QAbstractItemView::ExtendedSelection);
setSelectionBehavior(QAbstractItemView::SelectRows);
setAllColumnsShowFocus(true);
sortByColumn(AddressColumn, Qt::AscendingOrder);
setColumnWidth(OrdinalColumn, 55);
for (int i = 0; i < m_model->columnCount(QModelIndex()); i ++)
{
setColumnHidden(i, !m_model->headerData(i, Qt::Horizontal, ColumnVisibleRole).toBool());
}
connect(selectionModel(), &QItemSelectionModel::currentChanged, this, &ExportsTreeView::exportSelected);
connect(this, &QTreeView::doubleClicked, this, &ExportsTreeView::exportDoubleClicked);
connect(m_model, &QAbstractItemModel::modelAboutToBeReset, this, [=, this]() {
m_selection = selectionModel()->selectedIndexes();
m_scroll = verticalScrollBar()->value();
});
connect(m_model, &QAbstractItemModel::modelReset, this, [=, this]() {
for (auto& idx : m_selection)
{
setCurrentIndex(idx);
}
verticalScrollBar()->setValue(m_scroll);
});
m_actionHandler.bindAction("Copy", UIAction([this]() { copySelection(); }, [this]() { return canCopySelection(); }));
}
void ExportsTreeView::copySelection()
{
if (!model() || !selectionModel())
return;
QModelIndexList rows = selectionModel()->selectedRows();
if (rows.isEmpty())
return;
std::sort(rows.begin(), rows.end(), [](const QModelIndex& a, const QModelIndex& b) { return a.row() < b.row(); });
QStringList lines;
for (const QModelIndex& rowIndex : rows)
{
QStringList cells;
for (int column = 0; column < m_model->columnCount(QModelIndex()); column++)
{
if (isColumnHidden(column))
continue;
QModelIndex idx = m_model->index(rowIndex.row(), column, QModelIndex());
cells << m_model->data(idx, Qt::DisplayRole).toString();
}
lines << cells.join("\t");
}
if (QClipboard* clipboard = QGuiApplication::clipboard())
clipboard->setText(lines.join("\n"));
}
bool ExportsTreeView::canCopySelection() const
{
return !selectionModel()->selectedRows().isEmpty();
}
void ExportsTreeView::exportSelected(const QModelIndex& cur, const QModelIndex&)
{
SymbolRef sym = m_model->getSymbol(cur);
if (sym)
m_view->setCurrentOffset(sym->GetAddress());
}
void ExportsTreeView::exportDoubleClicked(const QModelIndex& cur)
{
SymbolRef sym = m_model->getSymbol(cur);
if (sym)
{
ViewFrame* viewFrame = ViewFrame::viewFrameForWidget(this);
if (viewFrame)
{
if (BinaryNinja::Settings::Instance()->Get<bool>("ui.view.graph.preferred") &&
viewFrame->getCurrentBinaryView() &&
m_data->GetAnalysisFunctionsForAddress(sym->GetAddress()).size() > 0)
{
viewFrame->navigate("Graph:" + viewFrame->getCurrentDataType(), sym->GetAddress());
}
else
{
viewFrame->navigate("Linear:" + viewFrame->getCurrentDataType(), sym->GetAddress());
}
}
}
}
void ExportsTreeView::setFilter(const std::string& filterText)
{
m_model->setFilter(filterText);
}
void ExportsTreeView::scrollToFirstItem()
{
scrollToTop();
}
void ExportsTreeView::scrollToCurrentItem()
{
scrollTo(currentIndex());
}
void ExportsTreeView::ensureSelection()
{
if (auto current = currentIndex(); !current.isValid())
setCurrentIndex(m_model->index(0, 0, QModelIndex()));
}
void ExportsTreeView::activateSelection()
{
ensureSelection();
if (auto current = currentIndex(); current.isValid())
exportDoubleClicked(current);
}
void ExportsTreeView::closeFilter()
{
setFocus(Qt::OtherFocusReason);
}
void ExportsTreeView::keyPressEvent(QKeyEvent* event)
{
if ((event->text().size() == 1) && (event->text()[0] > ' ') && (event->text()[0] <= '~'))
{
m_parent->showFilter(event->text());
event->accept();
}
else if ((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter))
{
QList<QModelIndex> sel = selectionModel()->selectedIndexes();
if (sel.size() != 0)
exportDoubleClicked(sel[0]);
}
else if (event->matches(QKeySequence::Copy))
{
copySelection();
event->accept();
return;
}
QTreeView::keyPressEvent(event);
}
void ExportsTreeView::showEvent(QShowEvent* event)
{
QTreeView::showEvent(event);
m_model->resumeUpdates();
}
void ExportsTreeView::hideEvent(QHideEvent* event)
{
QTreeView::hideEvent(event);
m_model->pauseUpdates();
}
ExportsWidget::ExportsWidget(QWidget* parent, TriageView* view, BinaryViewRef data) : QWidget(parent)
{
QVBoxLayout* layout = new QVBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
ExportsTreeView* exports = new ExportsTreeView(this, view, data);
m_filter = new FilteredView(this, exports, exports);
m_filter->setFilterPlaceholderText("Search exports");
layout->addWidget(m_filter, 1);
setLayout(layout);
setMinimumSize(UIContext::getScaledWindowSize(100, 196));
}
void ExportsWidget::showFilter(const QString& filter)
{
m_filter->showFilter(filter);
}
|