Skip to content

Commit

Permalink
updated entropy chart, strings and copy-paste
Browse files Browse the repository at this point in the history
  • Loading branch information
gcarmix committed Feb 4, 2024
1 parent 4db2703 commit 3997770
Show file tree
Hide file tree
Showing 16 changed files with 332 additions and 29 deletions.
26 changes: 24 additions & 2 deletions hexwalk/entropychart.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "entropychart.h"

#include <QDebug>
EntropyChart::EntropyChart(QWidget *parent):
QChartView(parent)
{
Expand All @@ -12,7 +12,29 @@ void EntropyChart::mousePressEvent(QMouseEvent * event){
auto pickVal = this->mapToScene(curPoint);
pickVal = this->chart()->mapFromScene(curPoint);
pickVal = this->chart()->mapToValue(curPoint,this->chart()->series().at(0));

emit rubberBandEvent();
emit mousePressed(qint64(pickVal.x()));

QChartView::mousePressEvent(event);

}
void EntropyChart::resizeEvent(QResizeEvent *event){

emit rubberBandEvent();
QChartView::resizeEvent(event);
}
void EntropyChart::mouseReleaseEvent(QMouseEvent *event){
emit rubberBandEvent();
QChartView::mouseReleaseEvent(event);
}

void EntropyChart::mouseMoveEvent(QMouseEvent * event){
auto curPoint = QCursor::pos();
curPoint = this->mapFromGlobal(curPoint);
auto pickVal = this->mapToScene(curPoint);
pickVal = this->chart()->mapFromScene(curPoint);
pickVal = this->chart()->mapToValue(curPoint,this->chart()->series().at(0));

emit mouseMoved(qint64(pickVal.x()));
QChartView::mouseMoveEvent(event);
}
6 changes: 6 additions & 0 deletions hexwalk/entropychart.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ Q_OBJECT

signals:
void mousePressed(qint64 value);
void mouseMoved(qint64 address);
void rubberBandEvent();
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void resizeEvent(QResizeEvent *event);
void mouseReleaseEvent(QMouseEvent *event);




Expand Down
69 changes: 65 additions & 4 deletions hexwalk/entropydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ui_entropydialog.h"
#include <QLineSeries>
#include <QChart>
#include <QDebug>

EntropyDialog::EntropyDialog(QHexEdit * hexedit,QWidget *parent) :
QDialog(parent),
Expand Down Expand Up @@ -35,14 +36,24 @@ void EntropyDialog::calculate()
{
qint64 cursor = 0;
qint64 dataSize = 1024;
QLineSeries *series = new QLineSeries();
qint64 blockSize = 1024;

if(_hexed->getSize() > 32*1024*1024)
{
blockSize = 4096;
}
if(_hexed->getSize() > 128*1024*1024)
{
blockSize = 16384;
}
series = new QLineSeries();
progrDialog = new QProgressDialog("Entropy calculation in progress...","Cancel",0,100,this);
progrDialog->setValue(0);
progrDialog->show();

while(cursor < _hexed->getSize())
{
dataSize = 1024;
dataSize = blockSize;
if(dataSize > (_hexed->getSize()-cursor))
{
dataSize = (_hexed->getSize()-cursor);
Expand All @@ -63,7 +74,7 @@ void EntropyDialog::calculate()
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->axes(Qt::Vertical).back()->setRange(0.0,1.1);
chart->axes(Qt::Vertical).back()->setRange(0.0,1.0);
chart->axes(Qt::Vertical).back()->setLabelsBrush(QBrush(QColor("lightgray")));
chart->axes(Qt::Horizontal).back()->setLabelsBrush(QBrush(QColor("lightgray")));
chart->setTitle("Entropy chart");
Expand All @@ -73,7 +84,8 @@ void EntropyDialog::calculate()
//entropyView = new EntropyChart(chart,this);
//entropyView->setRenderHint(QPainter::Antialiasing);
ui->entropyChart->setChart(chart);

ui->entropyChart->setRubberBand(QChartView::HorizontalRubberBand);
ui->entropyChart->setDragMode(QGraphicsView::ScrollHandDrag);
//QLayoutItem *item;

/*while((item=ui->verticalLayout->takeAt(0)) != NULL)
Expand All @@ -82,6 +94,8 @@ void EntropyDialog::calculate()
}
ui->verticalLayout->addWidget(entropyView);*/
connect(ui->entropyChart,SIGNAL(mousePressed(qint64)),this,SLOT(mousePressed(qint64)));
connect(ui->entropyChart,SIGNAL(mouseMoved(qint64)),this,SLOT(mouseMoved(qint64)));
connect(ui->entropyChart,SIGNAL(rubberBandEvent()),this,SLOT(limitZoomOut()));

}

Expand All @@ -90,6 +104,53 @@ void EntropyDialog::mousePressed(qint64 value)
_hexed->setCursorPosition(2*value);
_hexed->ensureVisible();
}
void EntropyDialog::mouseMoved(qint64 address)
{
if(address > 0 && address < _hexed->getSize()){

ui->AddressEdt->setText(QString::asprintf("%lld",address));
ui->ValueEdt->setText(QString::asprintf("%3.2f",findClosestPoint(series,address).y()));
}
}

void EntropyDialog::limitZoomOut() {

// Limit zoom out to 1.0x



QValueAxis *axisX = dynamic_cast<QValueAxis *>(ui->entropyChart->chart()->axisX());
if (axisX) {
qreal newMin = axisX->min();
qreal newMax = axisX->max();
if(axisX->min()< 0.0)
{
ui->entropyChart->chart()->axes(Qt::Horizontal).back()->setRange(0.0,axisX->max());
}
if(axisX->max()>_hexed->getSize())
{
ui->entropyChart->chart()->axes(Qt::Horizontal).back()->setRange(axisX->min(),_hexed->getSize());
}
}

}

QPointF EntropyDialog::findClosestPoint(QLineSeries* lineSeries, qreal x) {
QPointF closestPoint;
qreal minDistance = std::numeric_limits<qreal>::max();

// Itera sui punti della serie e trova il punto più vicino
for (const QPointF& point : lineSeries->points()) {
qreal distance = qAbs(point.x() - x);
if (distance < minDistance) {
minDistance = distance;
closestPoint = point;
}
}

return closestPoint;
}

EntropyDialog::~EntropyDialog()
{

Expand Down
4 changes: 4 additions & 0 deletions hexwalk/entropydialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class EntropyDialog : public QDialog
void calculate();
public slots:
void mousePressed(qint64 value);
void mouseMoved(qint64 address);
void limitZoomOut();
private slots:
void on_buttonBox_clicked(QAbstractButton *button);

Expand All @@ -32,6 +34,8 @@ private slots:
double blockEntropy(QByteArray * data);
EntropyChart *entropyView;
QProgressDialog *progrDialog;
QLineSeries *series = NULL;
QPointF findClosestPoint(QLineSeries* lineSeries, qreal x);
};

#endif // ENTROPYDIALOG_H
48 changes: 39 additions & 9 deletions hexwalk/entropydialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,46 @@
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="EntropyChart" name="entropyChart">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
<widget class="EntropyChart" name="entropyChart"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="Valuelbl">
<property name="text">
<string>Value:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="ValueEdt"/>
</item>
<item>
<widget class="QLabel" name="Addresslbl">
<property name="text">
<string>Address:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="AddressEdt"/>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</item>
Expand Down
2 changes: 1 addition & 1 deletion hexwalk/hexwalk.pro
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ QMAKE_LFLAGS += -no-pie -lstdc++ -Bstatic -static-libgcc -static-libstdc++ -stat
#Only for Mac:
#ICON = images/hexwalk.icns
###############
VERSION = "1.5.0"
VERSION = "1.6.0"
QMAKE_TARGET_COPYRIGHT = "gcarmix"
QMAKE_TARGET_PRODUCT = "HexWalk"
HEADERS = \
Expand Down
13 changes: 13 additions & 0 deletions hexwalk/hexwalkmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ void HexWalkMain::createMenus()
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(undoAct);
editMenu->addAction(redoAct);
editMenu->addAction(copyAct);
editMenu->addAction(pasteAct);
editMenu->addAction(cutAct);
editMenu->addAction(saveSelectionReadable);
editMenu->addSeparator();
editMenu->addAction(advancedFindAct);
Expand Down Expand Up @@ -219,6 +222,16 @@ void HexWalkMain::createActions()
redoAct->setShortcuts(QKeySequence::Redo);
connect(redoAct, SIGNAL(triggered()), hexEdit, SLOT(redo()));

copyAct = new QAction( tr("&Copy"), this);
copyAct->setShortcuts(QKeySequence::Copy);
connect(copyAct, SIGNAL(triggered()), hexEdit, SLOT(copyText()));
pasteAct = new QAction(tr("&Paste"), this);
pasteAct->setShortcuts(QKeySequence::Paste);
connect(pasteAct, SIGNAL(triggered()), hexEdit, SLOT(pasteText()));
cutAct = new QAction( tr("&Cut"), this);
cutAct->setShortcuts(QKeySequence::Cut);
connect(cutAct, SIGNAL(triggered()), hexEdit, SLOT(cutText()));

saveSelectionReadable = new QAction(tr("&Save Selection Readable..."), this);
saveSelectionReadable->setStatusTip(tr("Save selection in readable form"));
connect(saveSelectionReadable, SIGNAL(triggered()), this, SLOT(saveSelectionToReadableFile()));
Expand Down
3 changes: 3 additions & 0 deletions hexwalk/hexwalkmain.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ private slots:

QAction *undoAct;
QAction *redoAct;
QAction *copyAct;
QAction *pasteAct;
QAction *cutAct;
QAction *saveSelectionReadable;

QAction *aboutAct;
Expand Down
37 changes: 32 additions & 5 deletions hexwalk/stringsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ void StringsDialog::searchStrings()
progrDialog = new QProgressDialog("Search in progress...","Cancel",0,100,this);
progrDialog->setValue(0);
progrDialog->show();
while(ui->tableWidget->rowCount() > 0)
/*while(ui->tableWidget->rowCount() > 0)
{
ui->tableWidget->removeRow(0);
}
}*/
ui->tableWidget->clearContents();
ui->tableWidget->setRowCount(0);

while(cursor < _hexEdit->getSize())
{
dataSize = 4096;
dataSize = 16384;
if(dataSize > (_hexEdit->getSize()-cursor))
{
dataSize = (_hexEdit->getSize()-cursor);
Expand Down Expand Up @@ -166,12 +169,12 @@ void StringsDialog::searchStrings()
cursor+=dataSize;
progrDialog->setValue(int(100.0*(double(cursor)/double(_hexEdit->getSize()))));
QCoreApplication::processEvents();
if(occurrencies > 65535)
/*if(occurrencies > 100000)
{
QMessageBox::warning(this, tr("HexWalk"),tr("Too much occurrencies found, stopping search."));
break;
}
}*/
if(progrDialog->wasCanceled())
break;
}
Expand Down Expand Up @@ -214,3 +217,27 @@ void StringsDialog::on_tableWidget_clicked(const QModelIndex &index)

}


void StringsDialog::on_btnNext_clicked()
{
findStringInColumn(ui->edtFind->text());
}

bool StringsDialog::findStringInColumn(const QString& target) {
int rowCount = ui->tableWidget->rowCount();
int currentRow = ui->tableWidget->currentRow() +1;
for (int row = currentRow; row < rowCount; ++row) {
QTableWidgetItem* item = ui->tableWidget->item(row, 1);
if (item && item->text().contains(target)) {
//item->setSelected(true);
ui->tableWidget->setCurrentItem(ui->tableWidget->item(row, 1));

// Scroll to make the item visible
ui->tableWidget->scrollToItem(ui->tableWidget->item(row, 1), QAbstractItemView::PositionAtTop);
qDebug() << "Found at row:" << row << "column:" << 1;
return true;
}
}
qDebug() << "String not found in column:" << 1;
return false;
}
3 changes: 3 additions & 0 deletions hexwalk/stringsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ private slots:

void on_tableWidget_clicked(const QModelIndex &index);

void on_btnNext_clicked();

private:
Ui::StringsDialog *ui;
QHexEdit * _hexEdit;
void searchStrings();
QProgressDialog *progrDialog;
bool findStringInColumn(const QString& target);

};

Expand Down
Loading

0 comments on commit 3997770

Please sign in to comment.