Hexeditor: add option to write hex bytes

This commit is contained in:
alexthesys 2021-12-02 12:35:10 +03:00 committed by Anton Kochkov
parent 72ab12a23d
commit 15282d9bc5
2 changed files with 41 additions and 1 deletions

View File

@ -147,7 +147,11 @@ HexWidget::HexWidget(QWidget *parent)
connect(actionWrite64, &QAction::triggered, this, &HexWidget::w_write64);
actionsWriteString.append(actionWrite64);
actionsWriteOther.reserve(4);
actionsWriteOther.reserve(5);
QAction *actionWriteBytes = new QAction(tr("Write hex bytes"), this);
connect(actionWriteBytes, &QAction::triggered, this, &HexWidget::w_writeBytes);
actionsWriteOther.append(actionWriteBytes);
QAction *actionWriteZeros = new QAction(tr("Write zeros"), this);
connect(actionWriteZeros, &QAction::triggered, this, &HexWidget::w_writeZeros);
actionsWriteOther.append(actionWriteZeros);
@ -730,6 +734,41 @@ void HexWidget::w_increaseDecrease()
refresh();
}
void HexWidget::w_writeBytes()
{
if (!ioModesController.prepareForWriting()) {
return;
}
bool ok = false;
int size = INT_MAX;
if (!selection.isEmpty() && selection.size() <= INT_MAX) {
size = static_cast<int>(selection.size());
}
QInputDialog d;
d.setInputMode(QInputDialog::InputMode::TextInput);
QByteArray bytes = d.getText(this, tr("Write hex bytes"), tr("Hex byte string:"),
QLineEdit::Normal, "", &ok)
.toUtf8();
const int offset = bytes.startsWith("\\x") ? 2 : 0;
const int incr = offset + 2;
const int bytes_size = qMin(bytes.size() / incr, size);
if (ok && bytes_size) {
uint8_t *buf = (uint8_t *)malloc(static_cast<size_t>(bytes_size));
if (!buf) {
return;
}
for (int i = 0, j = 0, sz = bytes.size(); i < sz; i += incr, j++) {
buf[j] = static_cast<uint8_t>(bytes.mid(i + offset, 2).toInt(nullptr, 16));
}
RzCoreLocked core(Core());
rz_core_write_at(core, getLocationAddress(), buf, bytes_size);
free(buf);
refresh();
}
}
void HexWidget::w_writeZeros()
{
if (!ioModesController.prepareForWriting()) {

View File

@ -315,6 +315,7 @@ private slots:
// Write command slots
void w_writeString();
void w_increaseDecrease();
void w_writeBytes();
void w_writeZeros();
void w_write64();
void w_writeRandom();