Convert more commands to rizin APIs (#2964)

`fdj?`, `iRj`, `om.`, `psx`, `p8`, `fj`
This commit is contained in:
billow 2022-06-10 19:05:25 +08:00 committed by Anton Kochkov
parent 05b771b8bb
commit d2e2bcd6b6
4 changed files with 68 additions and 71 deletions

View File

@ -845,15 +845,19 @@ void CutterCore::removeString(RVA addr)
QString CutterCore::getString(RVA addr) QString CutterCore::getString(RVA addr)
{ {
CORE_LOCK(); CORE_LOCK();
char *s = (char *)returnAtSeek( return getString(addr, core->blocksize,
[&]() { rz_str_guess_encoding_from_buffer(core->block, core->blocksize));
RzStrStringifyOpt opt = { 0 }; }
QString CutterCore::getString(RVA addr, uint64_t len, RzStrEnc encoding, bool escape_nl)
{
CORE_LOCK();
RzStrStringifyOpt opt = {};
opt.buffer = core->block; opt.buffer = core->block;
opt.length = core->blocksize; opt.length = len;
opt.encoding = rz_str_guess_encoding_from_buffer(core->block, core->blocksize); opt.encoding = encoding;
return rz_str_stringify_raw_buffer(&opt, NULL); opt.escape_nl = escape_nl;
}, char *s = (char *)returnAtSeek([&]() { return rz_str_stringify_raw_buffer(&opt, NULL); }, addr);
addr);
return fromOwnedCharPtr(s); return fromOwnedCharPtr(s);
} }
@ -1023,9 +1027,7 @@ void CutterCore::updateSeek()
RVA CutterCore::prevOpAddr(RVA startAddr, int count) RVA CutterCore::prevOpAddr(RVA startAddr, int count)
{ {
CORE_LOCK(); CORE_LOCK();
bool ok; return rz_core_prevop_addr_force(core, startAddr, count);
RVA offset = cmdRawAt(QString("/O %1").arg(count), startAddr).toULongLong(&ok, 16);
return ok ? offset : startAddr - count;
} }
RVA CutterCore::nextOpAddr(RVA startAddr, int count) RVA CutterCore::nextOpAddr(RVA startAddr, int count)
@ -1322,17 +1324,14 @@ RVA CutterCore::getLastFunctionInstruction(RVA addr)
return lastBB ? rz_analysis_block_get_op_addr(lastBB, lastBB->ninstr - 1) : RVA_INVALID; return lastBB ? rz_analysis_block_get_op_addr(lastBB, lastBB->ninstr - 1) : RVA_INVALID;
} }
QString CutterCore::cmdFunctionAt(QString addr) QString CutterCore::flagAt(RVA addr)
{ {
QString ret; CORE_LOCK();
// Use cmd because cmdRaw would not work with grep RzFlagItem *f = rz_flag_get_at(core->flags, addr, true);
ret = cmd(QString("fd @ %1~[0]").arg(addr)); if (!f) {
return ret.trimmed(); return {};
} }
return core->flags->realnames && f->realname ? f->realname : f->name;
QString CutterCore::cmdFunctionAt(RVA addr)
{
return cmdFunctionAt(QString::number(addr));
} }
void CutterCore::cmdEsil(const char *command) void CutterCore::cmdEsil(const char *command)
@ -2035,7 +2034,7 @@ void CutterCore::attachRemote(const QString &uri)
[&](RzCore *core) { [&](RzCore *core) {
setConfig("cfg.debug", true); setConfig("cfg.debug", true);
rz_core_file_reopen_remote_debug(core, uri.toStdString().c_str(), 0); rz_core_file_reopen_remote_debug(core, uri.toStdString().c_str(), 0);
return (void *)NULL; return nullptr;
}, },
debugTask)) { debugTask)) {
return; return;
@ -2043,9 +2042,7 @@ void CutterCore::attachRemote(const QString &uri)
emit debugTaskStateChanged(); emit debugTaskStateChanged();
connect(debugTask.data(), &RizinTask::finished, this, [this, uri]() { connect(debugTask.data(), &RizinTask::finished, this, [this, uri]() {
if (debugTaskDialog) {
delete debugTaskDialog; delete debugTaskDialog;
}
debugTask.clear(); debugTask.clear();
// Check if we actually connected // Check if we actually connected
bool connected = false; bool connected = false;
@ -3536,10 +3533,8 @@ QList<BinClassDescription> CutterCore::getAllClassesFromFlags()
QList<BinClassDescription> ret; QList<BinClassDescription> ret;
QMap<QString, BinClassDescription *> classesCache; QMap<QString, BinClassDescription *> classesCache;
for (const CutterJson flagObject : cmdj("fj@F:classes")) { for (const auto &item : getAllFlags("classes")) {
QString flagName = flagObject[RJsonKey::name].toString(); QRegularExpressionMatch match = classFlagRegExp.match(item.name);
QRegularExpressionMatch match = classFlagRegExp.match(flagName);
if (match.hasMatch()) { if (match.hasMatch()) {
QString className = match.captured(1); QString className = match.captured(1);
BinClassDescription *desc = nullptr; BinClassDescription *desc = nullptr;
@ -3553,12 +3548,12 @@ QList<BinClassDescription> CutterCore::getAllClassesFromFlags()
desc = it.value(); desc = it.value();
} }
desc->name = match.captured(1); desc->name = match.captured(1);
desc->addr = flagObject[RJsonKey::offset].toRVA(); desc->addr = item.offset;
desc->index = RVA_INVALID; desc->index = RVA_INVALID;
continue; continue;
} }
match = methodFlagRegExp.match(flagName); match = methodFlagRegExp.match(item.name);
if (match.hasMatch()) { if (match.hasMatch()) {
QString className = match.captured(1); QString className = match.captured(1);
BinClassDescription *classDesc = nullptr; BinClassDescription *classDesc = nullptr;
@ -3578,7 +3573,7 @@ QList<BinClassDescription> CutterCore::getAllClassesFromFlags()
BinClassMethodDescription meth; BinClassMethodDescription meth;
meth.name = match.captured(2); meth.name = match.captured(2);
meth.addr = flagObject[RJsonKey::offset].toRVA(); meth.addr = item.offset;
classDesc->methods << meth; classDesc->methods << meth;
continue; continue;
} }
@ -3745,21 +3740,27 @@ void CutterCore::renameAnalysisMethod(const QString &className, const QString &o
QList<ResourcesDescription> CutterCore::getAllResources() QList<ResourcesDescription> CutterCore::getAllResources()
{ {
CORE_LOCK(); CORE_LOCK();
QList<ResourcesDescription> resources; RzBinFile *bf = rz_bin_cur(core->bin);
if (!bf) {
for (CutterJson resourceObject : cmdj("iRj")) { return {};
ResourcesDescription res;
res.name = resourceObject[RJsonKey::name].toString();
res.vaddr = resourceObject[RJsonKey::vaddr].toRVA();
res.index = resourceObject[RJsonKey::index].toUt64();
res.type = resourceObject[RJsonKey::type].toString();
res.size = resourceObject[RJsonKey::size].toUt64();
res.lang = resourceObject[RJsonKey::lang].toString();
resources << res;
} }
return resources; const RzList *resources = rz_bin_object_get_resources(bf->o);
QList<ResourcesDescription> resourcesDescriptions;
RzBinResource *r;
RzListIter *it;
CutterRzListForeach (resources, it, RzBinResource, r) {
ResourcesDescription description;
description.name = r->name;
description.vaddr = r->vaddr;
description.index = r->index;
description.type = r->type;
description.size = r->size;
description.lang = r->language;
resourcesDescriptions << description;
}
return resourcesDescriptions;
} }
QList<VTableDescription> CutterCore::getAllVTables() QList<VTableDescription> CutterCore::getAllVTables()
@ -3867,8 +3868,8 @@ QString CutterCore::getTypeAsC(QString name)
bool CutterCore::isAddressMapped(RVA addr) bool CutterCore::isAddressMapped(RVA addr)
{ {
// If value returned by "om. @ addr" is empty means that address is not mapped CORE_LOCK();
return !Core()->cmdRawAt(QString("om."), addr).isEmpty(); return rz_io_map_get(core->io, addr);
} }
QList<SearchDescription> CutterCore::getAllSearch(QString searchFor, QString space, QString in) QList<SearchDescription> CutterCore::getAllSearch(QString searchFor, QString space, QString in)
@ -3966,7 +3967,7 @@ QList<XrefDescription> CutterCore::getXRefs(RVA addr, bool to, bool whole_functi
} }
xd.from_str = RzAddressString(xd.from); xd.from_str = RzAddressString(xd.from);
xd.to_str = Core()->cmdRaw(QString("fd %1").arg(xd.to)).trimmed(); xd.to_str = Core()->flagAt(xd.to);
xrefList << xd; xrefList << xd;
} }
@ -3997,13 +3998,15 @@ QString CutterCore::listFlagsAsStringAt(RVA addr)
QString CutterCore::nearestFlag(RVA offset, RVA *flagOffsetOut) QString CutterCore::nearestFlag(RVA offset, RVA *flagOffsetOut)
{ {
auto r = cmdj(QString("fdj @ ") + QString::number(offset)); CORE_LOCK();
QString name = r["name"].toString(); auto r = rz_flag_get_at(core->flags, offset, true);
if (flagOffsetOut) { if (!r) {
auto offsetValue = r["offset"]; return {};
*flagOffsetOut = offsetValue.valid() ? offsetValue.toRVA() : offset;
} }
return name; if (flagOffsetOut) {
*flagOffsetOut = r->offset;
}
return r->name;
} }
void CutterCore::handleREvent(int type, void *data) void CutterCore::handleREvent(int type, void *data)

View File

@ -237,8 +237,7 @@ public:
RVA getFunctionStart(RVA addr); RVA getFunctionStart(RVA addr);
RVA getFunctionEnd(RVA addr); RVA getFunctionEnd(RVA addr);
RVA getLastFunctionInstruction(RVA addr); RVA getLastFunctionInstruction(RVA addr);
QString cmdFunctionAt(QString addr); QString flagAt(RVA addr);
QString cmdFunctionAt(RVA addr);
void createFunctionAt(RVA addr); void createFunctionAt(RVA addr);
void createFunctionAt(RVA addr, QString name); void createFunctionAt(RVA addr, QString name);
QStringList getDisassemblyPreview(RVA address, int num_of_lines); QStringList getDisassemblyPreview(RVA address, int num_of_lines);
@ -297,6 +296,7 @@ public:
* @return string at requested address * @return string at requested address
*/ */
QString getString(RVA addr); QString getString(RVA addr);
QString getString(RVA addr, uint64_t len, RzStrEnc encoding, bool escape_nl = false);
void setToData(RVA addr, int size, int repeat = 1); void setToData(RVA addr, int size, int repeat = 1);
int sizeofDataMeta(RVA addr); int sizeofDataMeta(RVA addr);

View File

@ -128,7 +128,7 @@ QVariant CommentsModel::data(const QModelIndex &index, int role) const
case CommentsModel::OffsetColumn: case CommentsModel::OffsetColumn:
return RzAddressString(comment.offset); return RzAddressString(comment.offset);
case CommentsModel::FunctionColumn: case CommentsModel::FunctionColumn:
return Core()->cmdFunctionAt(comment.offset); return Core()->flagAt(comment.offset);
case CommentsModel::CommentColumn: case CommentsModel::CommentColumn:
return comment.name; return comment.name;
default: default:
@ -220,8 +220,8 @@ bool CommentsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &ri
case CommentsModel::OffsetColumn: case CommentsModel::OffsetColumn:
return leftComment.offset < rightComment.offset; return leftComment.offset < rightComment.offset;
case CommentsModel::FunctionColumn: case CommentsModel::FunctionColumn:
return Core()->cmdFunctionAt(leftComment.offset) return Core()->flagAt(leftComment.offset)
< Core()->cmdFunctionAt(rightComment.offset); < Core()->flagAt(rightComment.offset);
case CommentsModel::CommentColumn: case CommentsModel::CommentColumn:
return leftComment.name < rightComment.name; return leftComment.name < rightComment.name;
default: default:

View File

@ -692,16 +692,10 @@ void HexWidget::copy()
if (selection.isEmpty() || selection.size() > MAX_COPY_SIZE) if (selection.isEmpty() || selection.size() > MAX_COPY_SIZE)
return; return;
QClipboard *clipboard = QApplication::clipboard(); auto x = cursorOnAscii
if (cursorOnAscii) { ? Core()->getString(selection.start(), selection.size(), RZ_STRING_ENC_8BIT, true)
clipboard->setText( : Core()->ioRead(selection.start(), (int)selection.size()).toHex();
Core()->cmdRawAt(QString("psx %1").arg(selection.size()), selection.start()) QApplication::clipboard()->setText(x);
.trimmed());
} else {
clipboard->setText(Core()->cmdRawAt(QString("p8 %1").arg(selection.size()),
selection.start())
.trimmed()); // TODO: copy in the format shown
}
} }
void HexWidget::copyAddress() void HexWidget::copyAddress()