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 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)

View File

@ -17,6 +17,7 @@
#include <QMutex>
#include <QDir>
#include <functional>
#include <memory>
class AsyncTaskManager;
class BasicInstructionHighlighter;
@ -169,6 +170,17 @@ public:
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 QString &str) { return cmdj(str.toUtf8().constData()); }
CutterJson cmdjAt(const char *str, RVA address);

View File

@ -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<QString, QString> cmdMap;
QMap<QString, RzLangByteArrayType> 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<char, decltype(free) *> 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()