diff --git a/radare2 b/radare2 index 82d1e96b..399bba41 160000 --- a/radare2 +++ b/radare2 @@ -1 +1 @@ -Subproject commit 82d1e96b7af0f56d7168179946c51fe93f5d4fe4 +Subproject commit 399bba4152e47e15126a69a21c8163f610bd932f diff --git a/src/core/Cutter.cpp b/src/core/Cutter.cpp index 17f303b5..f586d4ed 100644 --- a/src/core/Cutter.cpp +++ b/src/core/Cutter.cpp @@ -773,20 +773,26 @@ void CutterCore::setBBSize(int size) setConfig("anal.bb.maxsize", size); } -QString CutterCore::assemble(const QString &code) +QByteArray CutterCore::assemble(const QString &code) { CORE_LOCK(); RAsmCode *ac = r_asm_massemble(core_->assembler, code.toUtf8().constData()); - QString hex(ac != nullptr ? ac->buf_hex : ""); + QByteArray res; + if (ac && ac->bytes) { + res = QByteArray(reinterpret_cast(ac->bytes), ac->len); + } r_asm_code_free(ac); - return hex; + return res; } -QString CutterCore::disassemble(const QString &hex) +QString CutterCore::disassemble(const QByteArray &data) { CORE_LOCK(); - RAsmCode *ac = r_asm_mdisassemble_hexstr(core_->assembler, NULL, hex.toUtf8().constData()); - QString code = QString(ac != nullptr ? ac->buf_asm : ""); + RAsmCode *ac = r_asm_mdisassemble(core_->assembler, reinterpret_cast(data.constData()), data.length()); + QString code; + if (ac && ac->assembly) { + code = QString::fromUtf8(ac->assembly); + } r_asm_code_free(ac); return code; } @@ -2570,6 +2576,24 @@ QList CutterCore::disassembleLines(RVA offset, int lines) return r; } +QByteArray CutterCore::hexStringToBytes(const QString &hex) +{ + QByteArray hexChars = hex.toUtf8(); + QByteArray bytes; + bytes.reserve(hexChars.length() / 2); + int size = r_hex_str2bin(hexChars.constData(), reinterpret_cast(bytes.data())); + bytes.resize(size); + return bytes; +} + +QString CutterCore::bytesToHexString(const QByteArray &bytes) +{ + QByteArray hex; + hex.resize(bytes.length() * 2); + r_hex_bin2str(reinterpret_cast(bytes.constData()), bytes.size(), hex.data()); + return QString::fromUtf8(hex); +} + void CutterCore::loadScript(const QString &scriptname) { r_core_task_sync_begin(core_); diff --git a/src/core/Cutter.h b/src/core/Cutter.h index 59b3131c..75e9fcff 100644 --- a/src/core/Cutter.h +++ b/src/core/Cutter.h @@ -185,10 +185,14 @@ public: QList getColorThemes(); /* Assembly related methods */ - QString assemble(const QString &code); - QString disassemble(const QString &hex); + QByteArray assemble(const QString &code); + QString disassemble(const QByteArray &data); QString disassembleSingleInstruction(RVA addr); QList disassembleLines(RVA offset, int lines); + + static QByteArray hexStringToBytes(const QString &hex); + static QString bytesToHexString(const QByteArray &bytes); + void setCPU(QString arch, QString cpu, int bits); void setEndianness(bool big); void setBBSize(int size); diff --git a/src/dialogs/EditInstructionDialog.cpp b/src/dialogs/EditInstructionDialog.cpp index 1881cbae..874aca53 100644 --- a/src/dialogs/EditInstructionDialog.cpp +++ b/src/dialogs/EditInstructionDialog.cpp @@ -45,9 +45,11 @@ void EditInstructionDialog::updatePreview(const QString &input) ui->instructionLabel->setText(""); return; } else if (editMode == EDIT_BYTES) { - result = Core()->disassemble(input).trimmed(); + QByteArray data = CutterCore::hexStringToBytes(input); + result = Core()->disassemble(data).trimmed(); } else if (editMode == EDIT_TEXT) { - result = Core()->assemble(input).trimmed(); + QByteArray data = Core()->assemble(input); + result = CutterCore::bytesToHexString(data).trimmed(); } if (result.isEmpty() || result.contains(QLatin1Char('\n'))) {