Skip to content

Commit

Permalink
Fix regex & dev support (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
wargio authored Apr 22, 2024
1 parent 06c7b9b commit c387e63
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
5 changes: 3 additions & 2 deletions cutter-plugin/YaraAddDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "ui_YaraAddDialog.h"

#include <QIntValidator>
#include <QRegExpValidator>
#include <QRegularExpressionValidator>
#include "core/Cutter.h"

YaraAddDialog::YaraAddDialog(RVA offset, QWidget *parent)
Expand All @@ -32,7 +32,8 @@ YaraAddDialog::YaraAddDialog(RVA offset, QWidget *parent)
}
}

auto nameValidator = new QRegExpValidator(QRegExp("[A-Za-z0-9_]+"), this);
QRegularExpression rx("[A-Za-z0-9_]+");
auto nameValidator = new QRegularExpressionValidator(rx, this);
ui->nameEdit->setValidator(nameValidator);

auto size_validator = new QIntValidator(ui->sizeEdit);
Expand Down
5 changes: 3 additions & 2 deletions cutter-plugin/YaraAddMetaDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <QToolTip>
#include <QStringList>
#include <QIntValidator>
#include <QRegExpValidator>
#include <QRegularExpressionValidator>
#include "core/Cutter.h"

QStringList YaraAddMetaDialog::FileKeywords = {
Expand All @@ -28,7 +28,8 @@ YaraAddMetaDialog::YaraAddMetaDialog(QWidget *parent)
ui->setupUi(this);
setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));

auto nameValidator = new QRegExpValidator(QRegExp("^[A-Za-z0-9_]+$"), this);
QRegularExpression rx("^[A-Za-z0-9_]+$");
auto nameValidator = new QRegularExpressionValidator(rx, this);
ui->nameEdit->setValidator(nameValidator);
ui->nameEdit->setMaxLength(128);
ui->valueEdit->setMaxLength(128);
Expand Down
2 changes: 1 addition & 1 deletion include/rz_yara.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extern "C" {
#define RZ_YARA_FLAG_PREFIX_ASM_M RZ_YARA_FLAG_SPACE_RULE_ASM_M "."
#define RZ_YARA_FLAG_PREFIX_ASM_U RZ_YARA_FLAG_SPACE_RULE_ASM_U "."

typedef HtPP /*<const char*, const char*>*/ RzYaraMeta;
typedef HtSP /*<const char*, const char*>*/ RzYaraMeta;
typedef struct rz_yara_match_t {
char *rule;
char *string;
Expand Down
11 changes: 3 additions & 8 deletions src/yara_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,12 @@ typedef struct yara_cb_data_t {
const char *date_format;
} YaraCbData;

static void yara_metadata_free_kv(HtPPKv *kv) {
free(kv->key);
free(kv->value);
}

RZ_API RZ_OWN RzYaraMeta *rz_yara_metadata_new() {
return ht_pp_new((HtPPDupValue)strdup, (HtPPKvFreeFunc)yara_metadata_free_kv, (HtPPCalcSizeV)strlen);
return ht_sp_new(HT_STR_DUP, NULL, free);
}

RZ_API void rz_yara_metadata_free(RZ_NULLABLE RzYaraMeta *metadata) {
ht_pp_free(metadata);
ht_sp_free(metadata);
}

static inline void add_metadata_file_hash(YaraCbData *cd, const char *key) {
Expand Down Expand Up @@ -305,7 +300,7 @@ RZ_API char *rz_yara_create_rule_from_bytes(RZ_NONNULL RzCore *core, RZ_NULLABLE

if (metadata && metadata->count > 0) {
rz_strbuf_append(sb, "\tmeta:\n");
ht_pp_foreach(metadata, (HtPPForeachCallback)add_metadata, &cd);
ht_sp_foreach(metadata, (HtSPForeachCallback)add_metadata, &cd);
rz_strbuf_append(sb, "\n");
}

Expand Down
16 changes: 8 additions & 8 deletions src/yara_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#define SETPREFI(x, y, z) SETDESC(rz_config_set_i(cfg, x, y), z)
#define SETPREFB(x, y, z) SETDESC(rz_config_set_b(cfg, x, y), z)

static HtPP *yara_metadata = NULL;
static HtSP *yara_metadata = NULL;

static const RzCmdDescDetailEntry yara_command_grp_complete_create_example[] = {
{ .text = "yarama ", .arg_str = "author \"John Doe\"", .comment = "Adds metadata key 'author' and its value 'John Doe'" },
Expand Down Expand Up @@ -873,19 +873,19 @@ RZ_IPI RzCmdStatus yara_command_metadata_list_handler(RzCore *core, int argc, co
RzCmdStatus res = RZ_CMD_STATUS_OK;
switch (state->mode) {
case RZ_OUTPUT_MODE_STANDARD:
ht_pp_foreach(yara_metadata, (HtPPForeachCallback)yara_metadata_list_standard, NULL);
ht_sp_foreach(yara_metadata, (HtSPForeachCallback)yara_metadata_list_standard, NULL);
break;
case RZ_OUTPUT_MODE_QUIET:
ht_pp_foreach(yara_metadata, (HtPPForeachCallback)yara_metadata_list_quiet, NULL);
ht_sp_foreach(yara_metadata, (HtSPForeachCallback)yara_metadata_list_quiet, NULL);
break;
case RZ_OUTPUT_MODE_JSON:
pj_o(state->d.pj);
ht_pp_foreach(yara_metadata, (HtPPForeachCallback)yara_metadata_list_json, state->d.pj);
ht_sp_foreach(yara_metadata, (HtSPForeachCallback)yara_metadata_list_json, state->d.pj);
pj_end(state->d.pj);
break;
case RZ_OUTPUT_MODE_TABLE:
rz_table_set_columnsf(state->d.t, "ss", "key", "value", NULL);
ht_pp_foreach(yara_metadata, (HtPPForeachCallback)yara_metadata_list_table, state->d.t);
ht_sp_foreach(yara_metadata, (HtSPForeachCallback)yara_metadata_list_table, state->d.t);
break;
default:
rz_warn_if_reached();
Expand All @@ -909,12 +909,12 @@ RZ_IPI RzCmdStatus yara_command_metadata_add_handler(RzCore *core, int argc, con
} else {
value = argv[2];
}
ht_pp_update(yara_metadata, argv[1], (void *)value);
ht_sp_update(yara_metadata, argv[1], (void *)value);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus yara_command_metadata_remove_handler(RzCore *core, int argc, const char **argv) {
ht_pp_delete(yara_metadata, argv[1]);
ht_sp_delete(yara_metadata, argv[1]);
return RZ_CMD_STATUS_OK;
}

Expand Down Expand Up @@ -1008,7 +1008,7 @@ RZ_IPI bool yara_plugin_init(RzCore *core) {

RZ_IPI bool yara_plugin_fini(RzCore *core) {
yr_finalize();
ht_pp_free(yara_metadata);
ht_sp_free(yara_metadata);
RzCmd *cmd = core->rcmd;
RzCmdDesc* desc = rz_cmd_get_desc(cmd, "yara");
return rz_cmd_desc_remove(cmd, desc);
Expand Down

0 comments on commit c387e63

Please sign in to comment.