Convert to rizin APIs pc* px* (#3007)

This commit is contained in:
billow 2022-08-09 23:51:20 +08:00 committed by GitHub
parent 35f9bfe135
commit a4a4b9d2ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 29 deletions

View File

@ -4246,28 +4246,36 @@ QList<DisassemblyLine> CutterCore::disassembleLines(RVA offset, int lines)
*/ */
QString CutterCore::hexdump(RVA address, int size, HexdumpFormats format) QString CutterCore::hexdump(RVA address, int size, HexdumpFormats format)
{ {
QString command = "px"; CORE_LOCK();
char *res = nullptr;
switch (format) { switch (format) {
case HexdumpFormats::Normal: case HexdumpFormats::Normal:
res = rz_core_print_hexdump_or_hexdiff_str(core, RZ_OUTPUT_MODE_STANDARD, address, size,
false);
break; break;
case HexdumpFormats::Half: 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; break;
case HexdumpFormats::Word: 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; break;
case HexdumpFormats::Quad: 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; break;
case HexdumpFormats::Signed: 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; break;
case HexdumpFormats::Octal: 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; break;
} }
return cmdRawAt(QString("%1 %2").arg(command).arg(size), address); return fromOwnedCharPtr(res);
} }
QByteArray CutterCore::hexStringToBytes(const QString &hex) QByteArray CutterCore::hexStringToBytes(const QString &hex)

View File

@ -17,6 +17,7 @@
#include <QMutex> #include <QMutex>
#include <QDir> #include <QDir>
#include <functional> #include <functional>
#include <memory>
class AsyncTaskManager; class AsyncTaskManager;
class BasicInstructionHighlighter; class BasicInstructionHighlighter;
@ -169,6 +170,17 @@ public:
return ret; return ret;
} }
std::unique_ptr<RVA, std::function<void(const RVA *)>> seekTemp(RVA address)
{
auto seekBack = [&](const RVA *x) {
seekSilent(*x);
delete x;
};
std::unique_ptr<RVA, decltype(seekBack)> p { new RVA(getOffset()), seekBack };
seekSilent(address);
return p;
}
CutterJson cmdj(const char *str); CutterJson cmdj(const char *str);
CutterJson cmdj(const QString &str) { return cmdj(str.toUtf8().constData()); } CutterJson cmdj(const QString &str) { return cmdj(str.toUtf8().constData()); }
CutterJson cmdjAt(const char *str, RVA address); CutterJson cmdjAt(const char *str, RVA address);

View File

@ -149,7 +149,7 @@ void MainWindow::initUI()
&MainWindow::addExtraDisassembly); &MainWindow::addExtraDisassembly);
connect(ui->actionExtraHexdump, &QAction::triggered, this, &MainWindow::addExtraHexdump); connect(ui->actionExtraHexdump, &QAction::triggered, this, &MainWindow::addExtraHexdump);
connect(ui->actionCommitChanges, &QAction::triggered, this, connect(ui->actionCommitChanges, &QAction::triggered, this,
[this]() { Core()->commitWriteCache(); }); []() { Core()->commitWriteCache(); });
ui->actionCommitChanges->setEnabled(false); ui->actionCommitChanges->setEnabled(false);
connect(Core(), &CutterCore::ioCacheChanged, ui->actionCommitChanges, &QAction::setEnabled); 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() void MainWindow::on_actionExport_as_code_triggered()
{ {
QStringList filters; QStringList filters;
QMap<QString, QString> cmdMap; QMap<QString, RzLangByteArrayType> typMap;
const bool big_endian = Core()->getConfigb("big_endian");
filters << tr("C uin8_t array (*.c)"); 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)"); 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)"); 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)"); filters << tr("C uin64_t array (*.c)");
cmdMap[filters.last()] = "pcd"; typMap[filters.last()] = TYPE_BIG_ENDIAN(RZ_LANG_BYTE_ARRAY_C_CPP_DOUBLEWORDS, big_endian);
filters << tr("C string (*.c)");
cmdMap[filters.last()] = "pcs"; filters << tr("Go array (*.go)");
filters << tr("Shell-script that reconstructs the bin (*.sh)"); typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_GOLANG;
cmdMap[filters.last()] = "pcS"; filters << tr("Java array (*.java)");
typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_JAVA;
filters << tr("JSON array (*.json)"); filters << tr("JSON array (*.json)");
cmdMap[filters.last()] = "pcj"; typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_JSON;
filters << tr("JavaScript array (*.js)"); filters << tr("Kotlin array (*.kt)");
cmdMap[filters.last()] = "pcJ"; 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)"); 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)"); 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)"); filters << tr("GAS .byte blob (*.asm, *.s)");
cmdMap[filters.last()] = "pca"; typMap[filters.last()] = RZ_LANG_BYTE_ARRAY_ASM;
filters << tr(".bytes with instructions in comments (*.txt)");
cmdMap[filters.last()] = "pcA"; 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")); QFileDialog dialog(this, tr("Export as code"));
dialog.setAcceptMode(QFileDialog::AcceptSave); dialog.setAcceptMode(QFileDialog::AcceptSave);
@ -1736,13 +1756,19 @@ void MainWindow::on_actionExport_as_code_triggered()
qWarning() << "Can't open file"; qWarning() << "Can't open file";
return; return;
} }
TempConfig tempConfig; TempConfig tempConfig;
tempConfig.set("io.va", false); tempConfig.set("io.va", false);
QTextStream fileOut(&file); QTextStream fileOut(&file);
QString &cmd = cmdMap[dialog.selectedNameFilter()]; auto ps = core->seekTemp(0);
auto rc = core->core();
// Use cmd because cmdRaw would not handle such input std::unique_ptr<char, decltype(free) *> string {
fileOut << Core()->cmd(cmd + " $s @ 0"); 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() void MainWindow::on_actionApplySigFromFile_triggered()