Right click -> Select block in hexdump widget (#1006)

* jamieb22 modifications and some sanity checks
* improve warning color
* Fix writeable hexdump
* more sanity checks
This commit is contained in:
Itay Cohen 2018-12-18 19:26:38 +02:00 committed by xarkes
parent a111db2ae2
commit c8d8e667fb
7 changed files with 415 additions and 49 deletions

View File

@ -219,7 +219,8 @@ SOURCES += \
common/ColorSchemeFileSaver.cpp \
dialogs/EditFunctionDialog.cpp \
widgets/CutterTreeView.cpp \
widgets/ComboQuickFilterView.cpp
widgets/ComboQuickFilterView.cpp \
dialogs/HexdumpRangeDialog.cpp
HEADERS += \
Cutter.h \
@ -322,7 +323,8 @@ HEADERS += \
widgets/ColorSchemePrefWidget.h \
dialogs/EditFunctionDialog.h \
widgets/CutterTreeView.h \
widgets/ComboQuickFilterView.h
widgets/ComboQuickFilterView.h \
dialogs/HexdumpRangeDialog.h
FORMS += \
dialogs/AboutDialog.ui \
@ -381,7 +383,8 @@ FORMS += \
dialogs/SetFunctionVarTypes.ui \
widgets/ColorSchemePrefWidget.ui \
widgets/CutterTreeView.ui \
widgets/ComboQuickFilterView.ui
widgets/ComboQuickFilterView.ui \
dialogs/HexdumpRangeDialog.ui
RESOURCES += \
resources.qrc \

View File

@ -0,0 +1,108 @@
#include "HexdumpRangeDialog.h"
#include "ui_HexdumpRangeDialog.h"
HexdumpRangeDialog::HexdumpRangeDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::HexdumpRangeDialog)
{
ui->setupUi(this);
QRegExpValidator *v = new QRegExpValidator(QRegExp("(?:0[xX])?[0-9a-fA-F]+"), this);
ui->lengthLineEdit->setValidator(v);
ui->startAddressLineEdit->setValidator(v);
ui->endAddressLineEdit->setValidator(v);
//subscribe to a text change slot
connect(ui->endAddressLineEdit, &QLineEdit::textEdited, this, &HexdumpRangeDialog::textEdited);
connect(ui->lengthLineEdit, &QLineEdit::textEdited, this, &HexdumpRangeDialog::textEdited);
connect(ui->endAddressRadioButton, &QRadioButton::clicked, this,
&HexdumpRangeDialog::on_radioButtonClicked);
connect(ui->lengthRadioButton, &QRadioButton::clicked, this,
&HexdumpRangeDialog::on_radioButtonClicked);
}
HexdumpRangeDialog::~HexdumpRangeDialog()
{
delete ui;
}
QString HexdumpRangeDialog::getStartAddress() const
{
return ui->startAddressLineEdit->text();
}
QString HexdumpRangeDialog::getEndAddress() const
{
return ui->endAddressLineEdit->text();
}
QString HexdumpRangeDialog::getLength() const
{
return ui->lengthLineEdit->text();
}
bool HexdumpRangeDialog::getEndAddressRadioButtonChecked() const
{
return ui->endAddressRadioButton->isChecked();
}
bool HexdumpRangeDialog::getLengthRadioButtonChecked() const
{
return ui->lengthRadioButton->isChecked();
}
void HexdumpRangeDialog::setStartAddress(ut64 start)
{
ui->startAddressLineEdit->setText(
QString("0x%1").arg(start, 0, 16));
}
void HexdumpRangeDialog::textEdited()
{
bool warningVisibile = false;
ut64 startAddress = Core()->math(ui->startAddressLineEdit->text());
ut64 endAddress = 0;
ut64 length = 0;
if (sender() == ui->endAddressLineEdit) {
endAddress = Core()->math(getEndAddress());
if (endAddress > startAddress) {
length = endAddress - startAddress;
ui->lengthLineEdit->setText(
QString("0x%1").arg(length, 0, 16));
}
else {
ui->lengthLineEdit->setText("Invalid");
}
} else if ( sender() == ui->lengthLineEdit) {
//we edited the length, so update the end address to be start address + length
length = Core()->math(getLength());
endAddress = startAddress + length;
ui->endAddressLineEdit->setText(
QString("0x%1").arg(endAddress, 0, 16));
}
length = Core()->math(getLength());
// Warn the user for potentially heavy operation
if (length > 0x25000) {
warningVisibile = true;
}
ui->selectionWarningLabel->setVisible(warningVisibile);
}
void HexdumpRangeDialog::on_radioButtonClicked(bool checked)
{
if (sender() == ui->endAddressRadioButton && checked == true) {
ui->lengthLineEdit->setEnabled(false);
ui->endAddressLineEdit->setEnabled(true);
ui->endAddressLineEdit->setFocus();
} else if (sender() == ui->lengthRadioButton && checked == true) {
ui->lengthLineEdit->setEnabled(true);
ui->endAddressLineEdit->setEnabled(false);
ui->lengthLineEdit->setFocus();
}
}

View File

@ -0,0 +1,38 @@
#ifndef HEXDUMPRANGEDIALOG_H
#define HEXDUMPRANGEDIALOG_H
#include "Cutter.h"
#include <QDialog>
#include <QRegExpValidator>
namespace Ui {
class HexdumpRangeDialog;
}
class HexdumpRangeDialog : public QDialog
{
Q_OBJECT
public:
explicit HexdumpRangeDialog(QWidget *parent = nullptr);
~HexdumpRangeDialog();
QString getStartAddress() const;
QString getEndAddress() const;
QString getLength() const;
bool getEndAddressRadioButtonChecked() const;
bool getLengthRadioButtonChecked() const;
void setStartAddress(ut64 start);
public slots:
void textEdited();
private:
Ui::HexdumpRangeDialog *ui;
private slots:
void on_radioButtonClicked(bool checked);
};
#endif // HEXDUMPRANGEDIALOG_H

View File

@ -0,0 +1,154 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>HexdumpRangeDialog</class>
<widget class="QDialog" name="HexdumpRangeDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>455</width>
<height>201</height>
</rect>
</property>
<property name="windowTitle">
<string>Select Block</string>
</property>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>10</x>
<y>160</y>
<width>341</width>
<height>32</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="QWidget" name="gridLayoutWidget">
<property name="geometry">
<rect>
<x>29</x>
<y>19</y>
<width>401</width>
<height>121</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QRadioButton" name="endAddressRadioButton">
<property name="text">
<string>End Address:</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
</item>
</layout>
</item>
<item row="0" column="0">
<widget class="QLabel" name="startAddressLabel">
<property name="text">
<string>Start Address:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="startAddressLineEdit">
<property name="maxLength">
<number>18</number>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="endAddressLineEdit">
<property name="maxLength">
<number>18</number>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="lengthLineEdit">
<property name="enabled">
<bool>false</bool>
</property>
<property name="maxLength">
<number>10</number>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QRadioButton" name="lengthRadioButton">
<property name="text">
<string>Length:</string>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
</item>
<item row="4" column="1">
<widget class="QLabel" name="selectionWarningLabel">
<property name="enabled">
<bool>true</bool>
</property>
<property name="visible">
<bool>false</bool>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; color:#ff8585;&quot;&gt;Big selection might cause a delay&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>HexdumpRangeDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>HexdumpRangeDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
<buttongroups>
<buttongroup name="buttonGroup"/>
</buttongroups>
</ui>

View File

@ -12,6 +12,7 @@
#include <QMenu>
#include <QClipboard>
#include <QScrollBar>
#include <QInputDialog>
HexdumpWidget::HexdumpWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
@ -46,12 +47,12 @@ HexdumpWidget::HexdumpWidget(MainWindow *main, QAction *action) :
ui->openSideViewB->hide(); // hide button at startup since side view is visible
connect(closeButton, &QToolButton::clicked, this, [this]{
ui->hexSideTab_2->hide();
ui->openSideViewB->show();
connect(closeButton, &QToolButton::clicked, this, [this] {
ui->hexSideTab_2->hide();
ui->openSideViewB->show();
});
connect(ui->openSideViewB, &QToolButton::clicked, this, [this]{
connect(ui->openSideViewB, &QToolButton::clicked, this, [this] {
ui->hexSideTab_2->show();
ui->openSideViewB->hide();
});
@ -116,6 +117,7 @@ HexdumpWidget::HexdumpWidget(MainWindow *main, QAction *action) :
connect(ui->hexASCIIText, &QTextEdit::cursorPositionChanged, this,
&HexdumpWidget::selectionChanged);
connect(seekable, &CutterSeekableWidget::seekChanged, this, &HexdumpWidget::on_seekChanged);
connect(&rangeDialog, &QDialog::accepted, this, &HexdumpWidget::on_rangeDialogAccepted);
format = Format::Hex;
initParsing();
@ -312,6 +314,8 @@ void HexdumpWidget::highlightHexWords(const QString &str)
void HexdumpWidget::refresh(RVA addr)
{
ut64 loadLines = 0;
ut64 curAddrLineOffset = 0;
connectScroll(true);
updateHeaders();
@ -326,10 +330,17 @@ void HexdumpWidget::refresh(RVA addr)
cols = 16;
// TODO: Figure out how to calculate a sane value for this
bufferLines = qhelpers::getMaxFullyDisplayedLines(ui->hexHexText);
bufferLines = qhelpers::getMaxFullyDisplayedLines(ui->hexHexText) * 10;
ut64 loadLines = bufferLines * 3; // total lines to load
ut64 curAddrLineOffset = bufferLines; // line number where seek should be
if (requestedSelectionEndAddress != 0 && requestedSelectionStartAddress != 0
&& requestedSelectionEndAddress > requestedSelectionStartAddress) {
loadLines = ((requestedSelectionEndAddress - requestedSelectionStartAddress) / cols) +
(bufferLines * 2);
curAddrLineOffset = bufferLines;
} else {
loadLines = bufferLines * 3; // total lines to load
curAddrLineOffset = bufferLines; // line number where seek should be
}
if (addr < curAddrLineOffset * cols) {
curAddrLineOffset = static_cast<int>(addr / cols);
@ -607,6 +618,8 @@ void HexdumpWidget::showHexdumpContextMenu(const QPoint &pt)
formatSubmenu->addAction(ui->actionFormatHex);
formatSubmenu->addAction(ui->actionFormatOctal);
menu->addAction(ui->actionSelect_Block);
menu->addSeparator();
syncAction.setText(tr("Sync/unsync offset"));
menu->addAction(&syncAction);
@ -1017,6 +1030,19 @@ void HexdumpWidget::on_actionFormatOctal_triggered()
refresh();
}
void HexdumpWidget::on_actionSelect_Block_triggered()
{
//get the current hex address from current cursor location
rangeDialog.setStartAddress(
hexPositionToAddress(ui->hexHexText->textCursor().position()));
rangeDialog.setModal(false);
rangeDialog.show();
rangeDialog.activateWindow();
rangeDialog.raise();
}
void HexdumpWidget::on_parseTypeComboBox_currentTextChanged(const QString &)
{
if (ui->parseTypeComboBox->currentIndex() == 0) {
@ -1113,6 +1139,38 @@ void HexdumpWidget::selectHexPreview()
}
}
void HexdumpWidget::on_rangeDialogAccepted()
{
int startPosition;
int endPosition;
QTextCursor targetTextCursor;
requestedSelectionStartAddress = Core()->math(rangeDialog.getStartAddress());
requestedSelectionEndAddress = rangeDialog.getEndAddressRadioButtonChecked() ?
Core()->math(rangeDialog.getEndAddress()) :
requestedSelectionStartAddress + Core()->math(rangeDialog.getLength());
//not sure what the accepted user feedback mechanism is, output to console or a QMessageBox alert
if (requestedSelectionEndAddress <= requestedSelectionStartAddress) {
Core()->message(tr("Error: Could not select range, end address is less then start address"));
return;
}
//seek to the start address and create a text cursor to highlight the desired range
refresh(requestedSelectionStartAddress);
//for large selections, won't be able to calculate the endPosition because hexAddressToPosition assumes the address is loaded?
startPosition = hexAddressToPosition(requestedSelectionStartAddress);
endPosition = hexAddressToPosition(requestedSelectionEndAddress) - 1;
targetTextCursor = ui->hexHexText->textCursor();
targetTextCursor.setPosition(startPosition);
targetTextCursor.setPosition(endPosition, QTextCursor::KeepAnchor);
ui->hexHexText->setTextCursor(targetTextCursor);
}
void HexdumpWidget::showOffsets(bool show)
{
if (show) {

View File

@ -11,6 +11,7 @@
#include "Cutter.h"
#include "CutterDockWidget.h"
#include "CutterSeekableWidget.h"
#include "dialogs/HexdumpRangeDialog.h"
#include "common/Highlighter.h"
#include "common/HexAsciiHighlighter.h"
#include "common/HexHighlighter.h"
@ -44,7 +45,7 @@ public:
public slots:
void initParsing();
void on_rangeDialogAccepted();
void showOffsets(bool show);
void zoomIn(int range = 1);
@ -102,6 +103,9 @@ private:
int bufferLines = 0;
int cols = 0;
ut64 requestedSelectionStartAddress=0;
ut64 requestedSelectionEndAddress=0;
HexdumpRangeDialog rangeDialog;
QAction syncAction;
CutterSeekableWidget *seekable;
@ -137,6 +141,8 @@ private slots:
void on_actionFormatHex_triggered();
void on_actionFormatOctal_triggered();
void on_actionSelect_Block_triggered();
void fontsUpdated();
void colorsUpdatedSlot();

View File

@ -64,43 +64,6 @@
<property name="spacing">
<number>0</number>
</property>
<item row="1" column="2">
<widget class="QTextEdit" name="hexHexText">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum>
</property>
<property name="lineWrapMode">
<enum>QTextEdit::NoWrap</enum>
</property>
<property name="cursorWidth">
<number>3</number>
</property>
<property name="textInteractionFlags">
<set>Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="Line" name="line_2">
<property name="orientation">
@ -222,6 +185,37 @@
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QTextEdit" name="hexHexText">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum>
</property>
<property name="textInteractionFlags">
<set>Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
@ -912,9 +906,14 @@
<string>4 bytes</string>
</property>
</action>
<action name="actionSelect_Block">
<property name="text">
<string>Select Block...</string>
</property>
</action>
</widget>
<resources>
<include location="../resources.qrc"/>
</resources>
<connections/>
</ui>
</ui>