1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/*
* Copyright (C) 2015-2016 Winfried Simon
* Copyright (C) 2023 - 2024 Mikhail Medvedev <e-ink-reader@yandex.ru>
*
*
* This software may be used under the terms of the GNU Lesser General
* Public License version 2.1 as published by the Free Software Foundation
* and appearing in the file license.txt included in the packaging of this file.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "searchdialog.h"
#include "ui_searchdialog.h"
#include <QMessageBox>
SearchDialog::SearchDialog(QHexEdit *hexEdit, QWidget *parent) :
QDialog(parent),
ui(new Ui::SearchDialog)
{
ui->setupUi(this);
_hexEdit = hexEdit;
}
SearchDialog::~SearchDialog()
{
delete ui;
}
qint64 SearchDialog::findNext()
{
qint64 from = _hexEdit->cursorPosition() / 2;
_findBa = getContent(ui->cbFindFormat->currentIndex(), ui->cbFind->currentText());
qint64 idx = -1;
if (_findBa.length() > 0)
{
if (ui->cbBackwards->isChecked())
idx = _hexEdit->lastIndexOf(_findBa, from);
else
idx = _hexEdit->indexOf(_findBa, from);
}
return idx;
}
void SearchDialog::on_pbFind_clicked()
{
findNext();
}
void SearchDialog::on_pbReplace_clicked()
{
int idx = findNext();
if (idx >= 0)
{
QByteArray replaceBa = getContent(ui->cbReplaceFormat->currentIndex(), ui->cbReplace->currentText());
replaceOccurrence(idx, replaceBa);
}
}
void SearchDialog::on_pbReplaceAll_clicked()
{
int replaceCounter = 0;
int idx = 0;
int goOn = QMessageBox::Yes;
while ((idx >= 0) && (goOn == QMessageBox::Yes))
{
idx = findNext();
if (idx >= 0)
{
QByteArray replaceBa = getContent(ui->cbReplaceFormat->currentIndex(), ui->cbReplace->currentText());
int result = replaceOccurrence(idx, replaceBa);
if (result == QMessageBox::Yes)
replaceCounter += 1;
if (result == QMessageBox::Cancel)
goOn = result;
}
}
if (replaceCounter > 0)
QMessageBox::information(this, tr("QHexEdit"), QString(tr("%1 occurrences replaced.")).arg(replaceCounter));
}
QByteArray SearchDialog::getContent(int comboIndex, const QString &input)
{
QByteArray findBa;
switch (comboIndex)
{
case 0: // hex
findBa = QByteArray::fromHex(input.toLatin1());
break;
case 1: // text
findBa = input.toUtf8();
break;
}
return findBa;
}
qint64 SearchDialog::replaceOccurrence(qint64 idx, const QByteArray &replaceBa)
{
int result = QMessageBox::Yes;
if (replaceBa.length() >= 0)
{
if (ui->cbPrompt->isChecked())
{
result = QMessageBox::question(this, tr("QHexEdit"),
tr("Replace occurrence?"),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
if (result == QMessageBox::Yes)
{
_hexEdit->replace(idx, replaceBa.length(), replaceBa);
_hexEdit->update();
}
}
else
{
_hexEdit->replace(idx, _findBa.length(), replaceBa);
}
}
return result;
}
void SearchDialog::on_pb_png_clicked()
{
ui->cbFind->setCurrentText("89504e470d0a1a0a");
}
void SearchDialog::on_pb_jpg_clicked()
{
ui->cbFind->setCurrentText("ffd8ff");
}
void SearchDialog::on_pb_gif_clicked()
{
ui->cbFind->setCurrentText("47494638");
}
void SearchDialog::on_pb_zip_clicked()
{
ui->cbFind->setCurrentText("504b0304");
}
void SearchDialog::on_pb_tar_clicked()
{
ui->cbFind->setCurrentText("7573746172003030");
}
void SearchDialog::on_pb_bios_clicked()
{
ui->cbFind->setCurrentText("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5AA5F00F");
}
void SearchDialog::on_pb_uefi_clicked()
{
ui->cbFind->setCurrentText("0000000000000000000000000000000078E58C8C3D8A1C4F9935896185C32DD3");
}
void SearchDialog::on_pb_gpt_clicked()
{
ui->cbFind->setCurrentText("4546492050415254");
}
|