TypesInteractionDialog: use RzType API to edit a single type (#2760)

This commit is contained in:
Riccardo Schirone 2021-09-18 10:17:43 +02:00 committed by GitHub
parent 14c57f5daa
commit 3e1b3ce865
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 45 deletions

View File

@ -3571,23 +3571,6 @@ QList<TypeDescription> CutterCore::getAllTypedefs()
return getBaseType(RZ_BASE_TYPE_KIND_TYPEDEF, "Typedef"); return getBaseType(RZ_BASE_TYPE_KIND_TYPEDEF, "Typedef");
} }
QString CutterCore::addTypes(const char *str)
{
CORE_LOCK();
char *error_msg = nullptr;
int parsed = rz_type_parse_string(core->analysis->typedb, str, &error_msg);
QString error;
if (!parsed && error_msg) {
error = error_msg;
rz_mem_free(error_msg);
} else if (!parsed) {
error = QString("Failed to load new type %1").arg(str);
}
return error;
}
QString CutterCore::getTypeAsC(QString name) QString CutterCore::getTypeAsC(QString name)
{ {
CORE_LOCK(); CORE_LOCK();
@ -3602,11 +3585,6 @@ QString CutterCore::getTypeAsC(QString name)
return result; return result;
} }
bool CutterCore::deleteType(const char *typeName) {
CORE_LOCK();
return rz_type_db_del(core->analysis->typedb, typeName);
}
bool CutterCore::isAddressMapped(RVA addr) bool CutterCore::isAddressMapped(RVA addr)
{ {
// If value returned by "om. @ addr" is empty means that address is not mapped // If value returned by "om. @ addr" is empty means that address is not mapped

View File

@ -594,25 +594,6 @@ public:
*/ */
QString getTypeAsC(QString name); QString getTypeAsC(QString name);
/**
* @brief Adds new types
* It first uses the rz_parse_c_string() function from Rizin API to parse the
* supplied C file (in the form of a string). If there were errors, they are displayed.
* If there were no errors, it uses sdb_query_lines() function from Rizin API
* to save the parsed types returned by rz_parse_c_string()
* \param str Contains the definition of the data types
* \return returns an empty QString if there was no error, else returns the error
*/
QString addTypes(const char *str);
QString addTypes(const QString &str) { return addTypes(str.toUtf8().constData()); }
/**
* @brief Remove a type
* @param typeName Name of the type to remove
*/
bool deleteType(const char *typeName);
bool deleteType(QString typeName) { return deleteType(typeName.toUtf8().constData()); }
/** /**
* @brief Checks if the given address is mapped to a region * @brief Checks if the given address is mapped to a region
* @param addr The address to be checked * @param addr The address to be checked

View File

@ -22,10 +22,16 @@ TypesInteractionDialog::TypesInteractionDialog(QWidget *parent, bool readOnly)
syntaxHighLighter = Config()->createSyntaxHighlighter(ui->plainTextEdit->document()); syntaxHighLighter = Config()->createSyntaxHighlighter(ui->plainTextEdit->document());
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
ui->plainTextEdit->setReadOnly(readOnly); ui->plainTextEdit->setReadOnly(readOnly);
this->typeName = "";
} }
TypesInteractionDialog::~TypesInteractionDialog() {} TypesInteractionDialog::~TypesInteractionDialog() {}
void TypesInteractionDialog::setTypeName(QString name)
{
this->typeName = name;
}
void TypesInteractionDialog::on_selectFileButton_clicked() void TypesInteractionDialog::on_selectFileButton_clicked()
{ {
QString filename = QString filename =
@ -57,8 +63,11 @@ void TypesInteractionDialog::on_plainTextEdit_textChanged()
void TypesInteractionDialog::done(int r) void TypesInteractionDialog::done(int r)
{ {
if (r == QDialog::Accepted) { if (r == QDialog::Accepted) {
QString error = Core()->addTypes(ui->plainTextEdit->toPlainText()); RzCoreLocked core(Core());
if (error.isEmpty()) { bool edited = rz_type_db_edit_base_type(
core->analysis->typedb, this->typeName.toUtf8().constData(),
ui->plainTextEdit->toPlainText().toUtf8().constData());
if (edited) {
emit newTypesLoaded(); emit newTypesLoaded();
QDialog::done(r); QDialog::done(r);
return; return;
@ -67,7 +76,6 @@ void TypesInteractionDialog::done(int r)
QMessageBox popup(this); QMessageBox popup(this);
popup.setWindowTitle(tr("Error")); popup.setWindowTitle(tr("Error"));
popup.setText(tr("There was some error while loading new types")); popup.setText(tr("There was some error while loading new types"));
popup.setDetailedText(error);
popup.setStandardButtons(QMessageBox::Ok); popup.setStandardButtons(QMessageBox::Ok);
popup.exec(); popup.exec();
} else { } else {

View File

@ -27,6 +27,12 @@ public:
*/ */
void fillTextArea(QString content); void fillTextArea(QString content);
/**
* @brief Set the name of the type that is going to be changed
* @param name - name of the type
*/
void setTypeName(QString name);
private slots: private slots:
/** /**
* @brief Executed when the user clicks the selectFileButton * @brief Executed when the user clicks the selectFileButton
@ -50,6 +56,7 @@ private slots:
private: private:
std::unique_ptr<Ui::TypesInteractionDialog> ui; std::unique_ptr<Ui::TypesInteractionDialog> ui;
QSyntaxHighlighter *syntaxHighLighter; QSyntaxHighlighter *syntaxHighLighter;
QString typeName;
signals: signals:
/** /**

View File

@ -76,7 +76,8 @@ QVariant TypesModel::headerData(int section, Qt::Orientation, int role) const
bool TypesModel::removeRows(int row, int count, const QModelIndex &parent) bool TypesModel::removeRows(int row, int count, const QModelIndex &parent)
{ {
Core()->deleteType(types->at(row).type); RzCoreLocked core(Core());
rz_type_db_del(core->analysis->typedb, types->at(row).type.toUtf8().constData());
beginRemoveRows(parent, row, row + count - 1); beginRemoveRows(parent, row, row + count - 1);
while (count--) { while (count--) {
types->removeAt(row); types->removeAt(row);
@ -282,9 +283,17 @@ void TypesWidget::on_actionExport_Types_triggered()
void TypesWidget::on_actionLoad_New_Types_triggered() void TypesWidget::on_actionLoad_New_Types_triggered()
{ {
QModelIndex index = ui->typesTreeView->currentIndex();
if (!index.isValid()) {
return;
}
TypeDescription t = index.data(TypesModel::TypeDescriptionRole).value<TypeDescription>();
TypesInteractionDialog dialog(this); TypesInteractionDialog dialog(this);
connect(&dialog, &TypesInteractionDialog::newTypesLoaded, this, &TypesWidget::refreshTypes); connect(&dialog, &TypesInteractionDialog::newTypesLoaded, this, &TypesWidget::refreshTypes);
dialog.setWindowTitle(tr("Load New Types")); dialog.setWindowTitle(tr("Load New Types"));
dialog.setTypeName(t.type);
dialog.exec(); dialog.exec();
} }
@ -306,6 +315,7 @@ void TypesWidget::viewType(bool readOnly)
dialog.setWindowTitle(tr("View Type: ") + t.type + tr(" (Read Only)")); dialog.setWindowTitle(tr("View Type: ") + t.type + tr(" (Read Only)"));
} }
dialog.fillTextArea(Core()->getTypeAsC(t.type)); dialog.fillTextArea(Core()->getTypeAsC(t.type));
dialog.setTypeName(t.type);
dialog.exec(); dialog.exec();
} }
@ -354,5 +364,6 @@ void TypesWidget::typeItemDoubleClicked(const QModelIndex &index)
} }
dialog.fillTextArea(Core()->getTypeAsC(t.type)); dialog.fillTextArea(Core()->getTypeAsC(t.type));
dialog.setWindowTitle(tr("View Type: ") + t.type + tr(" (Read Only)")); dialog.setWindowTitle(tr("View Type: ") + t.type + tr(" (Read Only)"));
dialog.setTypeName(t.type);
dialog.exec(); dialog.exec();
} }