2018-03-08 12:24:15 +00:00
|
|
|
#include "SearchWidget.h"
|
|
|
|
#include "ui_SearchWidget.h"
|
2019-02-22 16:50:45 +00:00
|
|
|
#include "core/MainWindow.h"
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/Helpers.h"
|
2018-03-08 12:24:15 +00:00
|
|
|
|
2019-03-14 09:28:42 +00:00
|
|
|
#include <QDockWidget>
|
|
|
|
#include <QTreeWidget>
|
|
|
|
#include <QComboBox>
|
|
|
|
#include <QShortcut>
|
|
|
|
|
2019-04-22 08:43:34 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
static const int kMaxTooltipWidth = 500;
|
|
|
|
static const int kMaxTooltipDisasmPreviewLines = 10;
|
|
|
|
static const int kMaxTooltipHexdumpBytes = 64;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:08:02 +00:00
|
|
|
static const QMap<QString, QString> kSearchBoundariesValues {
|
|
|
|
{"io.maps", "All maps"},
|
|
|
|
{"io.map", "Current map"},
|
|
|
|
{"raw", "Raw"},
|
|
|
|
{"dbg.maps", "All memory maps"},
|
|
|
|
{"dbg.map", "Memory map"},
|
|
|
|
{"block", "Current block"},
|
|
|
|
{"bin.section", "Current mapped section"},
|
|
|
|
{"bin.sections", "All mapped sections"},
|
|
|
|
{"dbg.stack", "Stack"},
|
|
|
|
{"dbg.heap", "Heap"}
|
|
|
|
};
|
|
|
|
|
2018-03-08 12:24:15 +00:00
|
|
|
SearchModel::SearchModel(QList<SearchDescription> *search, QObject *parent)
|
|
|
|
: QAbstractListModel(parent),
|
|
|
|
search(search)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int SearchModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return search->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
int SearchModel::columnCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return Columns::COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant SearchModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (index.row() >= search->count())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
const SearchDescription &exp = search->at(index.row());
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
switch (role) {
|
2018-03-08 12:24:15 +00:00
|
|
|
case Qt::DisplayRole:
|
2018-03-21 20:32:32 +00:00
|
|
|
switch (index.column()) {
|
2018-03-08 12:24:15 +00:00
|
|
|
case OFFSET:
|
|
|
|
return RAddressString(exp.offset);
|
|
|
|
case SIZE:
|
|
|
|
return RSizeString(exp.size);
|
|
|
|
case CODE:
|
|
|
|
return exp.code;
|
|
|
|
case DATA:
|
|
|
|
return exp.data;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
2019-04-22 08:43:34 +00:00
|
|
|
case Qt::ToolTipRole: {
|
|
|
|
|
|
|
|
QString previewContent = QString();
|
|
|
|
// if result is CODE, show disassembly
|
|
|
|
if (!exp.code.isEmpty()) {
|
|
|
|
previewContent = Core()->getDisassemblyPreview(exp.offset, kMaxTooltipDisasmPreviewLines)
|
|
|
|
.join("<br>");
|
|
|
|
// if result is DATA or Disassembly is N/A
|
|
|
|
} else if (!exp.data.isEmpty() || previewContent.isEmpty()) {
|
|
|
|
previewContent = Core()->getHexdumpPreview(exp.offset, kMaxTooltipHexdumpBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QFont &fnt = Config()->getFont();
|
|
|
|
QFontMetrics fm{ fnt };
|
|
|
|
|
|
|
|
QString toolTipContent = QString("<html><div style=\"font-family: %1; font-size: %2pt; white-space: nowrap;\">")
|
|
|
|
.arg(fnt.family())
|
|
|
|
.arg(qMax(6, fnt.pointSize() - 1)); // slightly decrease font size, to keep more text in the same box
|
|
|
|
|
|
|
|
toolTipContent += tr("<div style=\"margin-bottom: 10px;\"><strong>Preview</strong>:<br>%1</div>")
|
|
|
|
.arg(previewContent);
|
|
|
|
|
|
|
|
toolTipContent += "</div></html>";
|
|
|
|
return toolTipContent;
|
|
|
|
}
|
2018-03-08 12:24:15 +00:00
|
|
|
case SearchDescriptionRole:
|
|
|
|
return QVariant::fromValue(exp);
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant SearchModel::headerData(int section, Qt::Orientation, int role) const
|
|
|
|
{
|
2018-03-21 20:32:32 +00:00
|
|
|
switch (role) {
|
2018-03-08 12:24:15 +00:00
|
|
|
case Qt::DisplayRole:
|
2018-03-21 20:32:32 +00:00
|
|
|
switch (section) {
|
2018-03-08 12:24:15 +00:00
|
|
|
case SIZE:
|
|
|
|
return tr("Size");
|
|
|
|
case OFFSET:
|
|
|
|
return tr("Offset");
|
|
|
|
case CODE:
|
|
|
|
return tr("Code");
|
|
|
|
case DATA:
|
|
|
|
return tr("Data");
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SearchSortFilterProxyModel::SearchSortFilterProxyModel(SearchModel *source_model, QObject *parent)
|
|
|
|
: QSortFilterProxyModel(parent)
|
|
|
|
{
|
|
|
|
setSourceModel(source_model);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SearchSortFilterProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
QModelIndex index = sourceModel()->index(row, 0, parent);
|
2018-03-21 20:32:32 +00:00
|
|
|
SearchDescription search = index.data(
|
|
|
|
SearchModel::SearchDescriptionRole).value<SearchDescription>();
|
2018-03-08 12:24:15 +00:00
|
|
|
return search.code.contains(filterRegExp());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SearchSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
|
|
|
|
{
|
2018-03-21 20:32:32 +00:00
|
|
|
SearchDescription left_search = left.data(
|
|
|
|
SearchModel::SearchDescriptionRole).value<SearchDescription>();
|
|
|
|
SearchDescription right_search = right.data(
|
|
|
|
SearchModel::SearchDescriptionRole).value<SearchDescription>();
|
2018-03-08 12:24:15 +00:00
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
switch (left.column()) {
|
2018-03-08 12:24:15 +00:00
|
|
|
case SearchModel::SIZE:
|
|
|
|
return left_search.size < right_search.size;
|
|
|
|
case SearchModel::OFFSET:
|
|
|
|
return left_search.offset < right_search.offset;
|
|
|
|
case SearchModel::CODE:
|
|
|
|
return left_search.code < right_search.code;
|
|
|
|
case SearchModel::DATA:
|
|
|
|
return left_search.data < right_search.data;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return left_search.offset < right_search.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-16 21:46:57 +00:00
|
|
|
SearchWidget::SearchWidget(MainWindow *main, QAction *action) :
|
|
|
|
CutterDockWidget(main, action),
|
|
|
|
ui(new Ui::SearchWidget)
|
2018-03-08 12:24:15 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2019-04-22 08:43:34 +00:00
|
|
|
setStyleSheet(QString("QToolTip { max-width: %1px; opacity: 230; }").arg(kMaxTooltipWidth));
|
2018-03-08 12:24:15 +00:00
|
|
|
|
2018-10-23 21:08:02 +00:00
|
|
|
ui->searchInCombo->blockSignals(true);
|
|
|
|
QMap<QString, QString>::const_iterator mapIter;
|
2018-10-24 10:05:36 +00:00
|
|
|
for (mapIter = kSearchBoundariesValues.cbegin(); mapIter != kSearchBoundariesValues.cend(); ++mapIter)
|
2018-10-23 21:08:02 +00:00
|
|
|
ui->searchInCombo->addItem(mapIter.value(), mapIter.key());
|
|
|
|
ui->searchInCombo->blockSignals(false);
|
|
|
|
|
2018-03-08 12:24:15 +00:00
|
|
|
search_model = new SearchModel(&search, this);
|
|
|
|
search_proxy_model = new SearchSortFilterProxyModel(search_model, this);
|
|
|
|
ui->searchTreeView->setModel(search_proxy_model);
|
|
|
|
ui->searchTreeView->sortByColumn(SearchModel::OFFSET, Qt::AscendingOrder);
|
|
|
|
|
|
|
|
setScrollMode();
|
|
|
|
|
|
|
|
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshSearchspaces()));
|
|
|
|
|
|
|
|
QShortcut *enter_press = new QShortcut(QKeySequence(Qt::Key_Return), this);
|
|
|
|
connect(enter_press, &QShortcut::activated, this, [this]() {
|
|
|
|
refreshSearch();
|
|
|
|
});
|
|
|
|
enter_press->setContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
|
|
|
|
connect(ui->searchButton, &QAbstractButton::clicked, this, [this]() {
|
|
|
|
refreshSearch();
|
|
|
|
});
|
|
|
|
|
2018-04-11 09:55:37 +00:00
|
|
|
connect(ui->searchspaceCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
2018-09-30 20:00:53 +00:00
|
|
|
[ = ](int index) { updatePlaceholderText(index);});
|
2018-04-11 09:55:37 +00:00
|
|
|
|
2018-10-23 21:08:02 +00:00
|
|
|
QString currentSearchBoundary = Core()->getConfig("search.in");
|
|
|
|
ui->searchInCombo->setCurrentIndex(ui->searchInCombo->findData(currentSearchBoundary));
|
2018-03-08 12:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SearchWidget::~SearchWidget() {}
|
|
|
|
|
|
|
|
void SearchWidget::on_searchTreeView_doubleClicked(const QModelIndex &index)
|
|
|
|
{
|
2018-04-23 07:54:06 +00:00
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
SearchDescription search = index.data(
|
|
|
|
SearchModel::SearchDescriptionRole).value<SearchDescription>();
|
2018-03-08 12:24:15 +00:00
|
|
|
Core()->seek(search.offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchWidget::searchChanged()
|
|
|
|
{
|
|
|
|
refreshSearchspaces();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchWidget::refreshSearchspaces()
|
|
|
|
{
|
|
|
|
int cur_idx = ui->searchspaceCombo->currentIndex();
|
|
|
|
if (cur_idx < 0)
|
|
|
|
cur_idx = 0;
|
|
|
|
|
|
|
|
ui->searchspaceCombo->clear();
|
|
|
|
ui->searchspaceCombo->addItem(tr("asm code"), QVariant("/cj"));
|
|
|
|
ui->searchspaceCombo->addItem(tr("string"), QVariant("/j"));
|
|
|
|
ui->searchspaceCombo->addItem(tr("hex string"), QVariant("/xj"));
|
2018-03-16 09:07:41 +00:00
|
|
|
ui->searchspaceCombo->addItem(tr("ROP gadgets"), QVariant("/Rj"));
|
2018-04-11 09:55:37 +00:00
|
|
|
ui->searchspaceCombo->addItem(tr("32bit value"), QVariant("/vj"));
|
2018-03-21 20:32:32 +00:00
|
|
|
|
2018-03-08 12:24:15 +00:00
|
|
|
if (cur_idx > 0)
|
|
|
|
ui->searchspaceCombo->setCurrentIndex(cur_idx);
|
|
|
|
|
|
|
|
refreshSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchWidget::refreshSearch()
|
|
|
|
{
|
|
|
|
QString search_for = ui->filterLineEdit->text();
|
|
|
|
QVariant searchspace_data = ui->searchspaceCombo->currentData();
|
|
|
|
QString searchspace = searchspace_data.toString();
|
|
|
|
|
2018-10-10 11:33:55 +00:00
|
|
|
search_model->beginResetModel();
|
2018-03-08 12:24:15 +00:00
|
|
|
search = Core()->getAllSearch(search_for, searchspace);
|
2018-10-10 11:33:55 +00:00
|
|
|
search_model->endResetModel();
|
2018-03-08 12:24:15 +00:00
|
|
|
|
2018-04-01 08:25:31 +00:00
|
|
|
qhelpers::adjustColumns(ui->searchTreeView, 3, 0);
|
2018-03-08 12:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchWidget::setScrollMode()
|
|
|
|
{
|
|
|
|
qhelpers::setVerticalScrollMode(ui->searchTreeView);
|
|
|
|
}
|
2018-04-11 09:55:37 +00:00
|
|
|
|
|
|
|
void SearchWidget::updatePlaceholderText(int index)
|
|
|
|
{
|
2018-09-30 20:00:53 +00:00
|
|
|
switch (index) {
|
|
|
|
case 1: // string
|
|
|
|
ui->filterLineEdit->setPlaceholderText("foobar");
|
|
|
|
break;
|
|
|
|
case 2: // hex string
|
|
|
|
ui->filterLineEdit->setPlaceholderText("deadbeef");
|
|
|
|
break;
|
|
|
|
case 3: // ROP gadgets
|
|
|
|
ui->filterLineEdit->setPlaceholderText("pop,,pop");
|
|
|
|
break;
|
|
|
|
case 4: // 32bit value
|
|
|
|
ui->filterLineEdit->setPlaceholderText("0xdeadbeef");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ui->filterLineEdit->setPlaceholderText("jmp rax");
|
2018-04-11 09:55:37 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 21:08:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
void SearchWidget::on_searchInCombo_currentIndexChanged(int index)
|
|
|
|
{
|
|
|
|
Config()->setConfig("search.in",
|
|
|
|
ui->searchInCombo->itemData(index).toString());
|
|
|
|
}
|