From a4a4b9d2ec2d88b9c4b1cabdbffb116ca912958b Mon Sep 17 00:00:00 2001 From: billow Date: Tue, 9 Aug 2022 23:51:20 +0800 Subject: [PATCH] Convert to rizin APIs `pc*` `px*` (#3007) --- src/core/Cutter.cpp | 22 ++++++++----- src/core/Cutter.h | 12 +++++++ src/core/MainWindow.cpp | 70 ++++++++++++++++++++++++++++------------- 3 files changed, 75 insertions(+), 29 deletions(-) diff --git a/src/core/Cutter.cpp b/src/core/Cutter.cpp index 5c7b515f..78e82ec4 100644 --- a/src/core/Cutter.cpp +++ b/src/core/Cutter.cpp @@ -4246,28 +4246,36 @@ QList CutterCore::disassembleLines(RVA offset, int lines) */ QString CutterCore::hexdump(RVA address, int size, HexdumpFormats format) { - QString command = "px"; + CORE_LOCK(); + char *res = nullptr; switch (format) { case HexdumpFormats::Normal: + res = rz_core_print_hexdump_or_hexdiff_str(core, RZ_OUTPUT_MODE_STANDARD, address, size, + false); break; case HexdumpFormats::Half: - command += "h"; + res = rz_core_print_dump_str(core, RZ_OUTPUT_MODE_STANDARD, address, 2, size, + RZ_CORE_PRINT_FORMAT_TYPE_HEXADECIMAL); break; case HexdumpFormats::Word: - command += "w"; + res = rz_core_print_dump_str(core, RZ_OUTPUT_MODE_STANDARD, address, 4, size, + RZ_CORE_PRINT_FORMAT_TYPE_HEXADECIMAL); break; case HexdumpFormats::Quad: - command += "q"; + res = rz_core_print_dump_str(core, RZ_OUTPUT_MODE_STANDARD, address, 8, size, + RZ_CORE_PRINT_FORMAT_TYPE_HEXADECIMAL); break; case HexdumpFormats::Signed: - command += "d"; + res = rz_core_print_dump_str(core, RZ_OUTPUT_MODE_STANDARD, address, 1, size, + RZ_CORE_PRINT_FORMAT_TYPE_INTEGER); break; case HexdumpFormats::Octal: - command += "o"; + res = rz_core_print_dump_str(core, RZ_OUTPUT_MODE_STANDARD, address, 1, size, + RZ_CORE_PRINT_FORMAT_TYPE_OCTAL); break; } - return cmdRawAt(QString("%1 %2").arg(command).arg(size), address); + return fromOwnedCharPtr(res); } QByteArray CutterCore::hexStringToBytes(const QString &hex) diff --git a/src/core/Cutter.h b/src/core/Cutter.h index 574d5258..dd042109 100644 --- a/src/core/Cutter.h +++ b/src/core/Cutter.h @@ -17,6 +17,7 @@ #include #include #include +#include class AsyncTaskManager; class BasicInstructionHighlighter; @@ -169,6 +170,17 @@ public: return ret; } + std::unique_ptr> seekTemp(RVA address) + { + auto seekBack = [&](const RVA *x) { + seekSilent(*x); + delete x; + }; + std::unique_ptr p { new RVA(getOffset()), seekBack }; + seekSilent(address); + return p; + } + CutterJson cmdj(const char *str); CutterJson cmdj(const QString &str) { return cmdj(str.toUtf8().constData()); } CutterJson cmdjAt(const char *str, RVA address); diff --git a/src/core/MainWindow.cpp b/src/core/MainWindow.cpp index e599809e..339884cc 100644 --- a/src/core/MainWindow.cpp +++ b/src/core/MainWindow.cpp @@ -149,7 +149,7 @@ void MainWindow::initUI() &MainWindow::addExtraDisassembly); connect(ui->actionExtraHexdump, &QAction::triggered, this, &MainWindow::addExtraHexdump); connect(ui->actionCommitChanges, &QAction::triggered, this, - [this]() { Core()->commitWriteCache(); }); + []() { Core()->commitWriteCache(); }); ui->actionCommitChanges->setEnabled(false); connect(Core(), &CutterCore::ioCacheChanged, ui->actionCommitChanges, &QAction::setEnabled); @@ -1692,35 +1692,55 @@ void MainWindow::on_actionImportPDB_triggered() } } +#define TYPE_BIG_ENDIAN(type, big_endian) big_endian ? type##_BE : type##_LE + void MainWindow::on_actionExport_as_code_triggered() { QStringList filters; - QMap cmdMap; + QMap typMap; + const bool big_endian = Core()->getConfigb("big_endian"); filters << tr("C uin8_t array (*.c)"); - cmdMap[filters.last()] = "pc"; + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_C_CPP_BYTES; filters << tr("C uin16_t array (*.c)"); - cmdMap[filters.last()] = "pch"; + typMap[filters.last()] = TYPE_BIG_ENDIAN(RZ_LANG_BYTE_ARRAY_C_CPP_HALFWORDS, big_endian); filters << tr("C uin32_t array (*.c)"); - cmdMap[filters.last()] = "pcw"; + typMap[filters.last()] = TYPE_BIG_ENDIAN(RZ_LANG_BYTE_ARRAY_C_CPP_WORDS, big_endian); filters << tr("C uin64_t array (*.c)"); - cmdMap[filters.last()] = "pcd"; - filters << tr("C string (*.c)"); - cmdMap[filters.last()] = "pcs"; - filters << tr("Shell-script that reconstructs the bin (*.sh)"); - cmdMap[filters.last()] = "pcS"; + typMap[filters.last()] = TYPE_BIG_ENDIAN(RZ_LANG_BYTE_ARRAY_C_CPP_DOUBLEWORDS, big_endian); + + filters << tr("Go array (*.go)"); + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_GOLANG; + filters << tr("Java array (*.java)"); + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_JAVA; filters << tr("JSON array (*.json)"); - cmdMap[filters.last()] = "pcj"; - filters << tr("JavaScript array (*.js)"); - cmdMap[filters.last()] = "pcJ"; + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_JSON; + filters << tr("Kotlin array (*.kt)"); + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_KOTLIN; + + filters << tr("Javascript array (*.js)"); + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_NODEJS; + filters << tr("ObjectiveC array (*.m)"); + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_OBJECTIVE_C; filters << tr("Python array (*.py)"); - cmdMap[filters.last()] = "pcp"; + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_PYTHON; + filters << tr("Rust array (*.rs)"); + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_RUST; + + filters << tr("Swift array (*.swift)"); + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_SWIFT; filters << tr("Print 'wx' Rizin commands (*.rz)"); - cmdMap[filters.last()] = "pc*"; + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_RIZIN; + filters << tr("Shell-script that reconstructs the bin (*.sh)"); + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_BASH; filters << tr("GAS .byte blob (*.asm, *.s)"); - cmdMap[filters.last()] = "pca"; - filters << tr(".bytes with instructions in comments (*.txt)"); - cmdMap[filters.last()] = "pcA"; + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_ASM; + + filters << tr("Yara (*.yar)"); + typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_YARA; + /* special case */ + QString instructionsInComments = tr(".bytes with instructions in comments (*.txt)"); + filters << instructionsInComments; QFileDialog dialog(this, tr("Export as code")); dialog.setAcceptMode(QFileDialog::AcceptSave); @@ -1736,13 +1756,19 @@ void MainWindow::on_actionExport_as_code_triggered() qWarning() << "Can't open file"; return; } + TempConfig tempConfig; tempConfig.set("io.va", false); QTextStream fileOut(&file); - QString &cmd = cmdMap[dialog.selectedNameFilter()]; - - // Use cmd because cmdRaw would not handle such input - fileOut << Core()->cmd(cmd + " $s @ 0"); + auto ps = core->seekTemp(0); + auto rc = core->core(); + std::unique_ptr string { + dialog.selectedNameFilter() != instructionsInComments + ? rz_lang_byte_array(rc->block, rc->blocksize, typMap[dialog.selectedNameFilter()]) + : rz_core_print_bytes_with_inst(rc, rc->block, rc->offset, rc->blocksize), + free + }; + fileOut << string.get(); } void MainWindow::on_actionApplySigFromFile_triggered()