cutter/src/widgets/HexdumpWidget.cpp

365 lines
10 KiB
C++
Raw Normal View History

#include "HexdumpWidget.h"
#include "ui_HexdumpWidget.h"
2018-10-17 07:55:53 +00:00
#include "common/Helpers.h"
#include "common/Configuration.h"
#include "common/TempConfig.h"
2018-01-27 10:08:05 +00:00
#include <QJsonObject>
#include <QJsonArray>
#include <QElapsedTimer>
#include <QTextDocumentFragment>
#include <QMenu>
2018-01-27 10:08:05 +00:00
#include <QClipboard>
#include <QScrollBar>
#include <QInputDialog>
#include <QShortcut>
2017-11-15 21:42:39 +00:00
HexdumpWidget::HexdumpWidget(MainWindow *main, QAction *action) :
MemoryDockWidget(CutterCore::MemoryWidgetType::Hexdump, main, action),
ui(new Ui::HexdumpWidget),
seekable(new CutterSeekable(this))
{
ui->setupUi(this);
/*
* Ugly hack just for the layout issue
* QSettings saves the state with the object names
* By doing this hack,
* you can at least avoid some mess by dismissing all the Extra Widgets
*/
QString name = "Hexdump";
if (!action) {
name = "Extra Hexdump";
}
setObjectName(name);
ui->copyMD5->setIcon(QIcon(":/img/icons/copy.svg"));
ui->copySHA1->setIcon(QIcon(":/img/icons/copy.svg"));
2017-11-19 21:21:02 +00:00
ui->splitter->setChildrenCollapsible(false);
QToolButton *closeButton = new QToolButton;
QIcon closeIcon = QIcon(":/img/icons/delete.svg");
closeButton->setIcon(closeIcon);
closeButton->setAutoRaise(true);
ui->hexSideTab_2->setCornerWidget(closeButton);
ui->openSideViewB->hide(); // hide button at startup since side view is visible
connect(closeButton, &QToolButton::clicked, this, [this] {
showSidePanel(false);
});
connect(ui->openSideViewB, &QToolButton::clicked, this, [this] {
showSidePanel(true);
});
2018-03-05 14:10:17 +00:00
ui->bytesMD5->setPlaceholderText("Select bytes to display information");
ui->bytesEntropy->setPlaceholderText("Select bytes to display information");
ui->bytesSHA1->setPlaceholderText("Select bytes to display information");
ui->hexDisasTextEdit->setPlaceholderText("Select bytes to display information");
setupFonts();
2018-01-27 10:08:05 +00:00
ui->openSideViewB->setStyleSheet(""
"QToolButton {"
" border : 0px;"
" padding : 0px;"
" margin : 0px;"
"}"
"QToolButton:hover {"
" border : 1px solid;"
" border-width : 1px;"
" border-color : #3daee9"
"}");
this->setWindowTitle(tr("Hexdump"));
2019-01-13 15:00:07 +00:00
refreshDeferrer = createReplacingRefreshDeferrer<RVA>(false, [this](const RVA *offset) {
refresh(offset ? *offset : RVA_INVALID);
});
connect(&syncAction, SIGNAL(triggered(bool)), this, SLOT(toggleSync()));
syncAction.setText(tr("Sync/unsync offset"));
this->ui->hexTextView->addAction(&syncAction);
connect(Config(), SIGNAL(fontsUpdated()), this, SLOT(fontsUpdated()));
connect(this, &QDockWidget::visibilityChanged, this, [](bool visibility) {
2018-03-21 20:32:32 +00:00
if (visibility) {
Core()->setMemoryWidgetPriority(CutterCore::MemoryWidgetType::Hexdump);
}
});
2019-05-16 16:03:48 +00:00
connect(Core(), &CutterCore::refreshAll, this, [this]() { refresh(); });
connect(seekable, &CutterSeekable::seekableSeekChanged, this, &HexdumpWidget::onSeekChanged);
2019-05-16 16:03:48 +00:00
connect(ui->hexTextView, &HexWidget::positionChanged, this, [this](RVA addr) {
if (!sent_seek) {
sent_seek = true;
seekable->seek(addr);
sent_seek = false;
}
});
2019-05-16 16:03:48 +00:00
connect(ui->hexTextView, &HexWidget::selectionChanged, this, &HexdumpWidget::selectionChanged);
connect(ui->hexSideTab_2, &QTabWidget::currentChanged, this, &HexdumpWidget::refreshSelectionInfo);
initParsing();
2017-12-03 01:44:12 +00:00
selectHexPreview();
}
void HexdumpWidget::onSeekChanged(RVA addr)
{
2018-03-21 20:32:32 +00:00
if (sent_seek) {
2018-01-27 10:08:05 +00:00
sent_seek = false;
return;
}
refresh(addr);
2017-11-17 12:41:30 +00:00
}
HexdumpWidget::~HexdumpWidget() {}
2019-05-16 16:03:48 +00:00
void HexdumpWidget::refresh()
{
refresh(RVA_INVALID);
}
void HexdumpWidget::refresh(RVA addr)
2017-04-09 19:55:06 +00:00
{
2019-01-13 15:00:07 +00:00
if (!refreshDeferrer->attemptRefresh(addr == RVA_INVALID ? nullptr : new RVA(addr))) {
return;
}
sent_seek = true;
2019-05-16 16:03:48 +00:00
if (addr != RVA_INVALID) {
ui->hexTextView->seek(addr);
} else {
ui->hexTextView->refresh();
refreshSelectionInfo();
}
sent_seek = false;
2017-11-17 12:41:30 +00:00
}
2017-11-19 21:21:02 +00:00
void HexdumpWidget::initParsing()
{
// Fill the plugins combo for the hexdump sidebar
ui->parseArchComboBox->insertItems(0, Core()->getAsmPluginNames());
ui->parseEndianComboBox->setCurrentIndex(Core()->getConfigb("cfg.bigendian") ? 1 : 0);
}
2019-05-16 16:03:48 +00:00
void HexdumpWidget::selectionChanged(HexWidget::Selection selection)
{
if (selection.empty) {
clearParseWindow();
2018-03-21 20:32:32 +00:00
} else {
updateParseWindow(selection.startAddress, selection.endAddress - selection.startAddress + 1);
2018-01-27 10:08:05 +00:00
}
}
void HexdumpWidget::on_parseArchComboBox_currentTextChanged(const QString &/*arg1*/)
{
refreshSelectionInfo();
}
void HexdumpWidget::on_parseBitsComboBox_currentTextChanged(const QString &/*arg1*/)
{
refreshSelectionInfo();
}
void HexdumpWidget::toggleSync()
{
QString windowTitle = tr("Hexdump");
seekable->toggleSynchronization();
if (seekable->isSynchronized()) {
setWindowTitle(windowTitle);
} else {
setWindowTitle(windowTitle + CutterSeekable::tr(" (unsynced)"));
}
}
void HexdumpWidget::setupFonts()
2017-04-09 19:55:06 +00:00
{
QFont font = Config()->getFont();
ui->hexDisasTextEdit->setFont(font);
2019-05-16 16:03:48 +00:00
ui->hexTextView->setMonospaceFont(font);
}
void HexdumpWidget::refreshSelectionInfo()
{
selectionChanged(ui->hexTextView->getSelection());
}
void HexdumpWidget::fontsUpdated()
2017-11-20 16:38:10 +00:00
{
setupFonts();
2018-01-27 10:08:05 +00:00
}
void HexdumpWidget::clearParseWindow()
{
ui->hexDisasTextEdit->setPlainText("");
ui->bytesEntropy->setText("");
ui->bytesMD5->setText("");
ui->bytesSHA1->setText("");
}
void HexdumpWidget::showSidePanel(bool show)
{
ui->hexSideTab_2->setVisible(show);
ui->openSideViewB->setHidden(show);
if (show) {
refreshSelectionInfo();
}
}
2018-01-27 10:08:05 +00:00
void HexdumpWidget::updateParseWindow(RVA start_address, int size)
{
if (!ui->hexSideTab_2->isVisible()) {
return;
}
2018-01-27 10:08:05 +00:00
QString address = RAddressString(start_address);
2018-01-27 13:11:30 +00:00
QString argument = QString("%1@" + address).arg(size);
2018-01-27 10:08:05 +00:00
if (ui->hexSideTab_2->currentIndex() == 0) {
2018-03-21 20:32:32 +00:00
// scope for TempConfig
// Get selected combos
QString arch = ui->parseArchComboBox->currentText();
QString bits = ui->parseBitsComboBox->currentText();
bool bigEndian = ui->parseEndianComboBox->currentIndex() == 1;
2018-01-27 10:08:05 +00:00
TempConfig tempConfig;
tempConfig
2018-03-21 20:32:32 +00:00
.set("asm.arch", arch)
.set("asm.bits", bits)
.set("cfg.bigendian", bigEndian);
2018-01-27 10:08:05 +00:00
2018-03-21 20:32:32 +00:00
switch (ui->parseTypeComboBox->currentIndex()) {
case 0: // Disassembly
ui->hexDisasTextEdit->setPlainText(Core()->cmd("pda " + argument));
break;
case 1: // String
ui->hexDisasTextEdit->setPlainText(Core()->cmd("pcs " + argument));
break;
case 2: // Assembler
ui->hexDisasTextEdit->setPlainText(Core()->cmd("pca " + argument));
break;
case 3: // C byte array
ui->hexDisasTextEdit->setPlainText(Core()->cmd("pc " + argument));
break;
case 4: // C half-word
ui->hexDisasTextEdit->setPlainText(Core()->cmd("pch " + argument));
break;
case 5: // C word
ui->hexDisasTextEdit->setPlainText(Core()->cmd("pcw " + argument));
break;
case 6: // C dword
ui->hexDisasTextEdit->setPlainText(Core()->cmd("pcd " + argument));
break;
case 7: // Python
ui->hexDisasTextEdit->setPlainText(Core()->cmd("pcp " + argument));
break;
case 8: // JSON
ui->hexDisasTextEdit->setPlainText(Core()->cmd("pcj " + argument));
break;
case 9: // JavaScript
ui->hexDisasTextEdit->setPlainText(Core()->cmd("pcJ " + argument));
break;
default:
ui->hexDisasTextEdit->setPlainText("");
2018-01-27 10:08:05 +00:00
}
2018-03-21 20:32:32 +00:00
} else {
// Fill the information tab hashes and entropy
ui->bytesMD5->setText(Core()->cmd("ph md5 " + argument).trimmed());
ui->bytesSHA1->setText(Core()->cmd("ph sha1 " + argument).trimmed());
ui->bytesEntropy->setText(Core()->cmd("ph entropy " + argument).trimmed());
ui->bytesMD5->setCursorPosition(0);
ui->bytesSHA1->setCursorPosition(0);
}
}
2018-01-27 10:08:05 +00:00
void HexdumpWidget::on_parseTypeComboBox_currentTextChanged(const QString &)
2017-04-09 19:55:06 +00:00
{
2018-03-21 20:32:32 +00:00
if (ui->parseTypeComboBox->currentIndex() == 0) {
2018-01-27 10:08:05 +00:00
ui->hexSideFrame_2->show();
2018-03-21 20:32:32 +00:00
} else {
2018-01-27 10:08:05 +00:00
ui->hexSideFrame_2->hide();
}
refreshSelectionInfo();
}
2018-01-27 10:08:05 +00:00
void HexdumpWidget::on_parseEndianComboBox_currentTextChanged(const QString &)
2017-04-09 19:55:06 +00:00
{
refreshSelectionInfo();
}
void HexdumpWidget::on_hexSideTab_2_currentChanged(int /*index*/)
{
/*
if (index == 2) {
// Add data to HTML Polar functions graph
QFile html(":/html/bar.html");
if(!html.open(QIODevice::ReadOnly)) {
QMessageBox::information(0,"error",html.errorString());
}
QString code = html.readAll();
html.close();
this->histoWebView->setHtml(code);
this->histoWebView->show();
} else {
this->histoWebView->hide();
}
*/
}
2018-01-27 10:08:05 +00:00
void HexdumpWidget::resizeEvent(QResizeEvent *event)
2017-04-09 19:55:06 +00:00
{
QDockWidget::resizeEvent(event);
2018-01-27 10:08:05 +00:00
refresh();
}
2019-05-16 16:03:48 +00:00
QWidget *HexdumpWidget::widgetToFocusOnRaise()
{
return ui->hexTextView;
}
void HexdumpWidget::on_copyMD5_clicked()
{
QString md5 = ui->bytesMD5->text();
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(md5);
2017-10-09 18:08:35 +00:00
// FIXME
// this->main->addOutput("MD5 copied to clipboard: " + md5);
}
void HexdumpWidget::on_copySHA1_clicked()
{
QString sha1 = ui->bytesSHA1->text();
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(sha1);
2017-10-09 18:08:35 +00:00
// FIXME
// this->main->addOutput("SHA1 copied to clipboard: " + sha1);
}
2018-01-27 10:08:05 +00:00
void HexdumpWidget::selectHexPreview()
2017-04-09 19:55:06 +00:00
{
// Pre-select arch and bits in the hexdump sidebar
QString arch = Core()->getConfig("asm.arch");
QString bits = Core()->getConfig("asm.bits");
2018-03-21 20:32:32 +00:00
if (ui->parseArchComboBox->findText(arch) != -1) {
ui->parseArchComboBox->setCurrentIndex(ui->parseArchComboBox->findText(arch));
}
2018-03-21 20:32:32 +00:00
if (ui->parseBitsComboBox->findText(bits) != -1) {
ui->parseBitsComboBox->setCurrentIndex(ui->parseBitsComboBox->findText(bits));
}
}