From d65d7c23d7ca77029ab958dde194e9f57ef1c180 Mon Sep 17 00:00:00 2001 From: Sylvain Pelissier Date: Thu, 13 Oct 2022 16:22:23 +0200 Subject: [PATCH] Correct hit results for /m and update tests --- libr/core/cmd_magic.c | 36 ++++++++++------- libr/core/cmd_search.c | 11 +++++- test/db/cmd/cmd_search | 76 ++++++------------------------------ test/db/cmd/cmd_search_magic | 70 ++++++++++++++++++++++++++++++++- test/db/tools/rafind2 | 2 +- 5 files changed, 111 insertions(+), 84 deletions(-) diff --git a/libr/core/cmd_magic.c b/libr/core/cmd_magic.c index e4dfd00276..f283f67d1b 100644 --- a/libr/core/cmd_magic.c +++ b/libr/core/cmd_magic.c @@ -6,19 +6,16 @@ static R_TH_LOCAL int magicdepth = 99; static R_TH_LOCAL RMagic *ck = NULL; // XXX: Use RCore->magic static R_TH_LOCAL char *ofile = NULL; -static R_TH_LOCAL int kw_count = 0; -static void r_core_magic_reset(RCore *core) { - kw_count = 0; -} - -static int r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth, int v, PJ *pj, int *hits) { +static int r_core_magic_at(RCore *core, RSearchKeyword *kw, const char *file, ut64 addr, int depth, int v, PJ *pj, int *hits) { const char *fmt; char *q, *p; const char *str; int delta = 0, adelta = 0, ret; ut64 curoffset = core->offset; int max_hits = r_config_get_i (core->config, "search.maxhits"); + char *flag; + if (max_hits > 0 && *hits >= max_hits) { return 0; } @@ -116,7 +113,7 @@ static int r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth, } p = strdup (str); fmt = p; - // processing newlinez + // processing newline for (q = p; *q; q++) { if (q[0] == '\\' && q[1]=='n') { *q = '\n'; @@ -128,15 +125,22 @@ static int r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth, if (cmdhit && *cmdhit) { r_core_cmd0 (core, cmdhit); } - { - const char *searchprefix = r_config_get (core->config, "search.prefix"); - char *flag = r_str_newf ("%s%d_%d", searchprefix, 0, kw_count++); + + const char *searchprefix = r_config_get (core->config, "search.prefix"); + + // We do not flag for pm command. + if (kw) { + flag = r_str_newf ("%s%d_%d", searchprefix, kw->kwidx, kw->count); r_flag_set (core->flags, flag, addr + adelta, 1); - free (flag); } // TODO: This must be a callback .. move this into RSearch? if (!pj) { - r_cons_printf ("0x%08"PFMT64x" %d %s\n", addr + adelta, magicdepth - depth, p); + if (kw) { + r_cons_printf ("0x%08" PFMT64x " %d %s %s\n", addr + adelta, magicdepth - depth, flag, p); + R_FREE (flag); + } else { + r_cons_printf ("0x%08" PFMT64x " %d %s\n", addr + adelta, magicdepth - depth, p); + } } else { pj_o (pj); pj_kN (pj, "offset", addr + adelta); @@ -144,6 +148,7 @@ static int r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth, pj_ks (pj, "info", p); pj_end (pj); } + if (must_report_progress) { r_cons_clear_line (1); } @@ -166,13 +171,13 @@ static int r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth, if (R_STR_ISEMPTY (fmt)) { fmt = file; } - r_core_magic_at (core, fmt, addr, depth, 1, pj, hits); + r_core_magic_at (core, kw, fmt, addr, depth, 1, pj, hits); *q = '@'; } break; } } - free (p); + R_FREE (p); r_magic_free (ck); ck = NULL; // return adelta+1; @@ -198,8 +203,9 @@ seek_exit: static void r_core_magic(RCore *core, const char *file, int v, PJ *pj) { ut64 addr = core->offset; int hits = 0; + magicdepth = r_config_get_i (core->config, "magic.depth"); // TODO: do not use global var here - r_core_magic_at (core, file, addr, magicdepth, v, pj, &hits); + r_core_magic_at (core, NULL, file, addr, magicdepth, v, pj, &hits); if (pj) { r_cons_newline (); } diff --git a/libr/core/cmd_search.c b/libr/core/cmd_search.c index 620f05d653..01f6c0427a 100644 --- a/libr/core/cmd_search.c +++ b/libr/core/cmd_search.c @@ -4113,10 +4113,17 @@ reread: ut64 addr = search_itv.addr; RListIter *iter; RIOMap *map; + RSearchKeyword *kw; + + kw = r_search_keyword_new_hexmask ("00", NULL); + kw->keyword_length = 1; + r_search_reset (core->search, R_SEARCH_MAGIC); + r_search_kw_add (core->search, kw); + if (param.outmode == R_MODE_JSON) { pj_a (param.pj); } - r_core_magic_reset (core); + int maxHits = r_config_get_i (core->config, "search.maxhits"); int hits = 0; r_list_foreach (param.boundaries, iter, map) { @@ -4128,7 +4135,7 @@ reread: if (r_cons_is_breaked ()) { break; } - ret = r_core_magic_at (core, file, addr, 99, false, param.outmode == R_MODE_JSON ? param.pj : NULL, &hits); + ret = r_core_magic_at (core, kw, file, addr, 99, false, param.outmode == R_MODE_JSON? param.pj: NULL, &hits); if (ret == -1) { // something went terribly wrong. break; diff --git a/test/db/cmd/cmd_search b/test/db/cmd/cmd_search index 55bd45b20e..43ac45ae63 100644 --- a/test/db/cmd/cmd_search +++ b/test/db/cmd/cmd_search @@ -39,22 +39,6 @@ EXPECT=<