From 3712cf152922cfdd9a4e71e48502eb66e1997267 Mon Sep 17 00:00:00 2001 From: xarkes Date: Sun, 4 Feb 2018 15:32:18 +0100 Subject: [PATCH] Added ResourcesWidget (fix #259) --- src/MainWindow.cpp | 29 ++------ src/MainWindow.h | 63 +++++++++--------- src/MainWindow.ui | 9 +++ src/cutter.cpp | 23 +++++++ src/cutter.h | 12 ++++ src/cutter.pro | 6 +- src/widgets/ClassesWidget.cpp | 9 +-- src/widgets/ClassesWidget.h | 17 ++--- src/widgets/ResourcesWidget.cpp | 114 ++++++++++++++++++++++++++++++++ src/widgets/ResourcesWidget.h | 48 ++++++++++++++ src/widgets/StringsWidget.cpp | 4 -- src/widgets/StringsWidget.h | 2 - 12 files changed, 254 insertions(+), 82 deletions(-) create mode 100644 src/widgets/ResourcesWidget.cpp create mode 100644 src/widgets/ResourcesWidget.h diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index b91d928a..0a8a2e49 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -64,6 +64,7 @@ #include "widgets/EntrypointWidget.h" #include "dialogs/SaveProjectDialog.h" #include "widgets/ClassesWidget.h" +#include "widgets/ResourcesWidget.h" // graphics #include @@ -87,29 +88,7 @@ static void registerCustomFonts() MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), core(CutterCore::getInstance()), - notepadDock(nullptr), - pseudocodeDock(nullptr), - asmDock(nullptr), - calcDock(nullptr), - omnibar(nullptr), - ui(new Ui::MainWindow), - highlighter(nullptr), - hex_highlighter(nullptr), - visualNavbar(nullptr), - entrypointDock(nullptr), - functionsDock(nullptr), - importsDock(nullptr), - exportsDock(nullptr), - symbolsDock(nullptr), - relocsDock(nullptr), - commentsDock(nullptr), - stringsDock(nullptr), - flagsDock(nullptr), - dashboardDock(nullptr), - gotoEntry(nullptr), - sdbDock(nullptr), - sectionsDock(nullptr), - consoleDock(nullptr) + ui(new Ui::MainWindow) { panelLock = false; tabsOnTop = false; @@ -180,7 +159,7 @@ void MainWindow::initUI() /* * Dock Widgets */ - dockWidgets.reserve(14); + dockWidgets.reserve(20); #define ADD_DOCK(cls, dockMember, action) \ { \ @@ -243,6 +222,7 @@ void MainWindow::initUI() ADD_DOCK(Dashboard, dashboardDock, ui->actionDashboard); ADD_DOCK(SdbDock, sdbDock, ui->actionSDBBrowser); ADD_DOCK(ClassesWidget, classesDock, ui->actionClasses); + ADD_DOCK(ResourcesWidget, resourcesDock, ui->actionResources); #undef ADD_DOCK @@ -548,6 +528,7 @@ void MainWindow::restoreDocks() tabifyDockWidget(dashboardDock, symbolsDock); tabifyDockWidget(dashboardDock, notepadDock); tabifyDockWidget(dashboardDock, classesDock); + tabifyDockWidget(dashboardDock, resourcesDock); updateDockActionsChecked(); } diff --git a/src/MainWindow.h b/src/MainWindow.h index 21b405a8..800a8e7e 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -37,6 +37,7 @@ class ConsoleWidget; class EntrypointWidget; class DisassemblerGraphView; class ClassesWidget; +class ResourcesWidget; class QDockWidget; @@ -145,48 +146,46 @@ private slots: void projectSaved(const QString &name); private: - CutterCore *core; - Notepad *notepadDock; - DisassemblyWidget *disassemblyDock; - SidebarWidget *sidebarDock; - HexdumpWidget *hexdumpDock; - PseudocodeWidget *pseudocodeDock; - QDockWidget *graphDock; - DisassemblerGraphView *graphView; - QDockWidget *asmDock; - QDockWidget *calcDock; - Omnibar *omnibar; - //SideBar *sideBar; - Configuration *configuration; - + CutterCore *core; bool panelLock; bool tabsOnTop; ut64 hexdumpTopOffset; ut64 hexdumpBottomOffset; QString filename; std::unique_ptr ui; - Highlighter *highlighter; + Highlighter *highlighter; AsciiHighlighter *hex_highlighter; - VisualNavbar *visualNavbar; - EntrypointWidget *entrypointDock; - FunctionsWidget *functionsDock; - ImportsWidget *importsDock; - ExportsWidget *exportsDock; - SymbolsWidget *symbolsDock; - RelocsWidget *relocsDock; - CommentsWidget *commentsDock; - StringsWidget *stringsDock; - FlagsWidget *flagsDock; - Dashboard *dashboardDock; - QLineEdit *gotoEntry; - SdbDock *sdbDock; - //QAction *sidebar_action; - SectionsDock *sectionsDock; - ConsoleWidget *consoleDock; - ClassesWidget *classesDock; + VisualNavbar *visualNavbar; + Omnibar *omnibar; + Configuration *configuration; QList dockWidgets; QMap dockWidgetActions; + Notepad *notepadDock = nullptr; + DisassemblyWidget *disassemblyDock = nullptr; + SidebarWidget *sidebarDock = nullptr; + HexdumpWidget *hexdumpDock = nullptr; + PseudocodeWidget *pseudocodeDock = nullptr; + QDockWidget *graphDock = nullptr; + EntrypointWidget *entrypointDock = nullptr; + FunctionsWidget *functionsDock = nullptr; + ImportsWidget *importsDock = nullptr; + ExportsWidget *exportsDock = nullptr; + SymbolsWidget *symbolsDock = nullptr; + RelocsWidget *relocsDock = nullptr; + CommentsWidget *commentsDock = nullptr; + StringsWidget *stringsDock = nullptr; + FlagsWidget *flagsDock = nullptr; + Dashboard *dashboardDock = nullptr; + QLineEdit *gotoEntry = nullptr; + SdbDock *sdbDock = nullptr; + SectionsDock *sectionsDock = nullptr; + ConsoleWidget *consoleDock = nullptr; + ClassesWidget *classesDock = nullptr; + ResourcesWidget *resourcesDock = nullptr; + DisassemblerGraphView *graphView = nullptr; + QDockWidget *asmDock = nullptr; + QDockWidget *calcDock = nullptr; void toggleDockWidget(QDockWidget *dock_widget, bool show); diff --git a/src/MainWindow.ui b/src/MainWindow.ui index db10b492..fce548b6 100644 --- a/src/MainWindow.ui +++ b/src/MainWindow.ui @@ -249,6 +249,7 @@ border-top: 0px; + @@ -1041,6 +1042,14 @@ QToolButton:pressed { Analyze + + + true + + + Resources + + diff --git a/src/cutter.cpp b/src/cutter.cpp index bd7da1d3..d7f8eeed 100644 --- a/src/cutter.cpp +++ b/src/cutter.cpp @@ -1204,6 +1204,29 @@ QList CutterCore::getAllClasses() return ret; } +QList CutterCore::getAllResources() +{ + CORE_LOCK(); + QList ret; + + QJsonArray resourcesArray = cmdj("iRj").array(); + for (QJsonValueRef value : resourcesArray) + { + QJsonObject resourceObject = value.toObject(); + + ResourcesDescription res; + res.name = resourceObject["name"].toString(); + res.vaddr = resourceObject["vaddr"].toVariant().toULongLong(); + res.index = resourceObject["index"].toVariant().toULongLong(); + res.type = resourceObject["type"].toString(); + res.size = resourceObject["size"].toVariant().toULongLong(); + res.lang = resourceObject["lang"].toString(); + + ret << res; + } + return ret; +} + QList CutterCore::getXRefs(RVA addr, bool to, bool whole_function, const QString &filterType) { QList ret = QList(); diff --git a/src/cutter.h b/src/cutter.h index 67ac459d..5df2c283 100644 --- a/src/cutter.h +++ b/src/cutter.h @@ -195,6 +195,16 @@ struct ClassDescription QList fields; }; +struct ResourcesDescription +{ + QString name; + RVA vaddr; + ut64 index; + QString type; + ut64 size; + QString lang; +}; + Q_DECLARE_METATYPE(FunctionDescription) Q_DECLARE_METATYPE(ImportDescription) Q_DECLARE_METATYPE(ExportDescription) @@ -210,6 +220,7 @@ Q_DECLARE_METATYPE(RBinPluginDescription) Q_DECLARE_METATYPE(ClassMethodDescription) Q_DECLARE_METATYPE(ClassFieldDescription) Q_DECLARE_METATYPE(ClassDescription) +Q_DECLARE_METATYPE(ResourcesDescription) Q_DECLARE_METATYPE(const ClassDescription *) Q_DECLARE_METATYPE(const ClassMethodDescription *) Q_DECLARE_METATYPE(const ClassFieldDescription *) @@ -341,6 +352,7 @@ public: QList getAllSections(); QList getAllEntrypoint(); QList getAllClasses(); + QList getAllResources(); QList getXRefs(RVA addr, bool to, bool whole_function, const QString &filterType = QString::null); diff --git a/src/cutter.pro b/src/cutter.pro index a1e49201..204a2a12 100644 --- a/src/cutter.pro +++ b/src/cutter.pro @@ -86,7 +86,8 @@ SOURCES += \ dialogs/preferences/GeneralOptionsWidget.cpp \ dialogs/preferences/GraphOptionsWidget.cpp \ widgets/QuickFilterView.cpp \ - widgets/ClassesWidget.cpp + widgets/ClassesWidget.cpp \ + widgets/ResourcesWidget.cpp HEADERS += \ cutter.h \ @@ -143,7 +144,8 @@ HEADERS += \ dialogs/preferences/GeneralOptionsWidget.h \ dialogs/preferences/GraphOptionsWidget.h \ widgets/QuickFilterView.h \ - widgets/ClassesWidget.h + widgets/ClassesWidget.h \ + widgets/ResourcesWidget.h FORMS += \ dialogs/AboutDialog.ui \ diff --git a/src/widgets/ClassesWidget.cpp b/src/widgets/ClassesWidget.cpp index 89a62002..9b24289f 100644 --- a/src/widgets/ClassesWidget.cpp +++ b/src/widgets/ClassesWidget.cpp @@ -3,7 +3,6 @@ #include "ClassesWidget.h" #include "ui_ClassesWidget.h" -#include "MainWindow.h" #include "utils/Helpers.h" ClassesModel::ClassesModel(QList *classes, QObject *parent) @@ -243,16 +242,12 @@ bool ClassesSortFilterProxyModel::lessThan(const QModelIndex &left, const QModel -ClassesWidget::ClassesWidget(MainWindow *main, QWidget *parent) : +ClassesWidget::ClassesWidget(QWidget *parent) : QDockWidget(parent), - ui(new Ui::ClassesWidget), - main(main) + ui(new Ui::ClassesWidget) { ui->setupUi(this); - // Radare core found in: - this->main = main; - model = new ClassesModel(&classes, this); proxy_model = new ClassesSortFilterProxyModel(model, this); ui->classesTreeView->setModel(proxy_model); diff --git a/src/widgets/ClassesWidget.h b/src/widgets/ClassesWidget.h index 916ae3a7..2a0bb737 100644 --- a/src/widgets/ClassesWidget.h +++ b/src/widgets/ClassesWidget.h @@ -1,5 +1,5 @@ -#ifndef CLASSWSWIDGET_H -#define CLASSWSWIDGET_H +#ifndef CLASSESWIDGET_H +#define CLASSESWIDGET_H #include @@ -9,16 +9,12 @@ #include #include -class MainWindow; -class QTreeWidget; - namespace Ui { class ClassesWidget; } - -class MainWindow; +class QTreeWidget; class QTreeWidgetItem; @@ -52,7 +48,7 @@ public: void endReload(); }; -Q_DECLARE_METATYPE(ClassesModel::RowType); +Q_DECLARE_METATYPE(ClassesModel::RowType) class ClassesSortFilterProxyModel : public QSortFilterProxyModel @@ -74,7 +70,7 @@ class ClassesWidget : public QDockWidget Q_OBJECT public: - explicit ClassesWidget(MainWindow *main, QWidget *parent = 0); + explicit ClassesWidget(QWidget *parent = nullptr); ~ClassesWidget(); private slots: @@ -84,7 +80,6 @@ private slots: private: std::unique_ptr ui; - MainWindow *main; ClassesModel *model; ClassesSortFilterProxyModel *proxy_model; @@ -92,4 +87,4 @@ private: }; -#endif // CLASSWSWIDGET_H +#endif // CLASSESWIDGET_H diff --git a/src/widgets/ResourcesWidget.cpp b/src/widgets/ResourcesWidget.cpp new file mode 100644 index 00000000..95e2b993 --- /dev/null +++ b/src/widgets/ResourcesWidget.cpp @@ -0,0 +1,114 @@ +#include "ResourcesWidget.h" +#include + +ResourcesModel::ResourcesModel(QList *resources, QObject *parent) + : QAbstractListModel(parent), + resources(resources) +{ +} + +int ResourcesModel::rowCount(const QModelIndex &) const +{ + return resources->count(); +} + +int ResourcesModel::columnCount(const QModelIndex &) const +{ + return Columns::COUNT; +} + +QVariant ResourcesModel::data(const QModelIndex &index, int role) const +{ + const ResourcesDescription *res = &resources->at(index.row()); + switch (role) + { + case Qt::DisplayRole: + switch (index.column()) + { + case NAME: + return res->name; + case VADDR: + return RAddressString(res->vaddr); + case INDEX: + return res->index; + case TYPE: + return res->type; + case SIZE: + return res->size; + case LANG: + return res->lang; + default: + return QVariant(); + } + default: + return QVariant(); + } +} + +QVariant ResourcesModel::headerData(int section, Qt::Orientation, int role) const +{ + switch (role) + { + case Qt::DisplayRole: + switch (section) + { + case NAME: + return tr("Name"); + case VADDR: + return tr("Vaddr"); + case INDEX: + return tr("Index"); + case TYPE: + return tr("Type"); + case SIZE: + return tr("Size"); + case LANG: + return tr("Lang"); + default: + return QVariant(); + } + default: + return QVariant(); + } +} + +void ResourcesModel::beginReload() +{ + beginResetModel(); +} + +void ResourcesModel::endReload() +{ + endResetModel(); +} + +ResourcesWidget::ResourcesWidget(QWidget *parent) + : QDockWidget(parent) +{ + model = new ResourcesModel(&resources, this); + + // Configure widget + this->setWindowTitle(tr("Resources")); + + // Add resources tree view + view = new QTreeView(this); + view->setModel(model); + view->show(); + this->setWidget(view); + + connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshResources())); + connect(view, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(onDoubleClicked(const QModelIndex &))); +} + +void ResourcesWidget::refreshResources() +{ + model->beginReload(); + resources = Core()->getAllResources(); + model->endReload(); +} + +void ResourcesWidget::onDoubleClicked(const QModelIndex &index) +{ + ResourcesDescription res = index.data().value(); + CutterCore::getInstance()->seek(res.vaddr); +} diff --git a/src/widgets/ResourcesWidget.h b/src/widgets/ResourcesWidget.h new file mode 100644 index 00000000..7ff16b12 --- /dev/null +++ b/src/widgets/ResourcesWidget.h @@ -0,0 +1,48 @@ +#ifndef RESOURCESWIDGET_H +#define RESOURCESWIDGET_H + +#include "cutter.h" + +#include +#include +#include + +class ResourcesModel : public QAbstractListModel +{ + Q_OBJECT + +private: + QList *resources; + +public: + enum Columns { INDEX = 0, NAME, VADDR, TYPE, SIZE, LANG, COUNT }; + explicit ResourcesModel(QList *resources, QObject *parent = nullptr); + + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + + QVariant data(const QModelIndex &index, int role) const override; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + + void beginReload(); + void endReload(); +}; + +class ResourcesWidget : public QDockWidget +{ + Q_OBJECT + +private: + ResourcesModel *model; + QTreeView *view; + QList resources; + +public: + ResourcesWidget(QWidget *parent = nullptr); + +private slots: + void refreshResources(); + void onDoubleClicked(const QModelIndex &); +}; + +#endif // RESOURCESWIDGET_H diff --git a/src/widgets/StringsWidget.cpp b/src/widgets/StringsWidget.cpp index 2052a895..fe55ebb9 100644 --- a/src/widgets/StringsWidget.cpp +++ b/src/widgets/StringsWidget.cpp @@ -79,10 +79,6 @@ void StringsModel::endReload() endResetModel(); } - - - - StringsSortFilterProxyModel::StringsSortFilterProxyModel(StringsModel *source_model, QObject *parent) : QSortFilterProxyModel(parent) { diff --git a/src/widgets/StringsWidget.h b/src/widgets/StringsWidget.h index 81a1ae30..97d24823 100644 --- a/src/widgets/StringsWidget.h +++ b/src/widgets/StringsWidget.h @@ -17,8 +17,6 @@ namespace Ui class StringsWidget; } - - class StringsModel: public QAbstractListModel { Q_OBJECT