Skip to content

Commit

Permalink
Add RzSearchParams struct
Browse files Browse the repository at this point in the history
  * This will allow for a generic search function, since all search
    parameters can be represented using the `RzSearchParams` struct
  * Also, the struct contains search parameters, so any search function
    (whose job is to "search") would never need to modify any of these
    params. This allows for some sort of contraint/invariance and will
    help in making sure that the search functions which get implemented
    are correct.
  • Loading branch information
DMaroo committed Feb 25, 2022
1 parent 43db5b0 commit 021c941
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 132 deletions.
4 changes: 2 additions & 2 deletions librz/core/canalysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -3141,7 +3141,7 @@ RZ_API int rz_core_analysis_search(RzCore *core, ut64 from, ut64 to, ut64 ref, i
// TODO: get current section range here
// ???
// XXX must read bytes correctly
do_bckwrd_srch = bckwrds = core->search->bckwrds;
do_bckwrd_srch = bckwrds = core->search->params.backwards;
if (core->file) {
rz_io_use_fd(core->io, core->file->fd);
}
Expand Down Expand Up @@ -4905,7 +4905,7 @@ static bool stringAt(RzCore *core, ut64 addr) {

RZ_API int rz_core_search_value_in_range(RzCore *core, RzInterval search_itv, ut64 vmin,
ut64 vmax, int vsize, inRangeCb cb, void *cb_user) {
int i, align = core->search->align, hitctr = 0;
int i, align = core->search->params.align, hitctr = 0;
bool vinfun = rz_config_get_b(core->config, "analysis.vinfun");
bool vinfunr = rz_config_get_b(core->config, "analysis.vinfunrange");
bool analyze_strings = rz_config_get_b(core->config, "analysis.strings");
Expand Down
2 changes: 1 addition & 1 deletion librz/core/casm.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ RZ_API RzList *rz_core_asm_strsearch(RzCore *core, const char *input, ut64 from,
RzList *hits;
ut64 at, toff = core->offset;
ut8 *buf;
int align = core->search->align;
int align = core->search->params.align;
RzRegex *rx = NULL;
char *tok, *tokens[1024], *code = NULL, *ptr;
int idx, tidx = 0, len = 0;
Expand Down
8 changes: 5 additions & 3 deletions librz/core/cconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -1950,7 +1950,7 @@ static bool cb_hexstride(void *user, void *data) {
static bool cb_search_kwidx(void *user, void *data) {
RzCore *core = (RzCore *)user;
RzConfigNode *node = (RzConfigNode *)data;
core->search->n_kws = node->i_value;
core->search->params.n_kws = node->i_value;
return true;
}

Expand Down Expand Up @@ -2340,14 +2340,14 @@ static bool cb_scrrows(void *user, void *data) {
static bool cb_contiguous(void *user, void *data) {
RzCore *core = (RzCore *)user;
RzConfigNode *node = (RzConfigNode *)data;
core->search->contiguous = node->i_value;
core->search->params.contiguous = node->i_value;
return true;
}

static bool cb_searchalign(void *user, void *data) {
RzCore *core = (RzCore *)user;
RzConfigNode *node = (RzConfigNode *)data;
core->search->align = node->i_value;
core->search->params.align = node->i_value;
core->print->addrmod = node->i_value;
return true;
}
Expand Down Expand Up @@ -3683,6 +3683,8 @@ RZ_API int rz_core_config_init(RzCore *core) {
SETI("search.chunk", 0, "Chunk size for /+ (default size is asm.bits/8");
SETI("search.esilcombo", 8, "Stop search after N consecutive hits");
SETI("search.distance", 0, "Search string distance");
SETI("search.minlength", 3, "Minimum length of search string");
SETI("search.maxlength", 255, "Maximum length of search string");
SETBPREF("search.flags", "true", "All search results are flagged, otherwise only printed");
SETBPREF("search.overlap", "false", "Look for overlapped search hits");
SETI("search.maxhits", 0, "Maximum number of hits (0: no limit)");
Expand Down
2 changes: 1 addition & 1 deletion librz/core/cil.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ RZ_IPI void rz_core_analysis_esil_emulate(RzCore *core, ut64 addr, ut64 until_ad
(void)rz_analysis_esil_parse(esil, e);
}
}
int inc = (core->search->align > 0) ? core->search->align - 1 : ret - 1;
int inc = (core->search->params.align > 0) ? core->search->params.align - 1 : ret - 1;
if (inc < 0) {
inc = minopcode;
}
Expand Down
8 changes: 4 additions & 4 deletions librz/core/cmd/cmd_magic.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ static int rz_core_magic_at(RzCore *core, const char *file, ut64 addr, int depth
}
#endif
}
if (core->search->align) {
int mod = addr % core->search->align;
if (core->search->params.align) {
int mod = addr % core->search->params.align;
if (mod) {
eprintf("Unaligned search at %d\n", mod);
ret = mod;
Expand Down Expand Up @@ -97,7 +97,7 @@ static int rz_core_magic_at(RzCore *core, const char *file, ut64 addr, int depth
#else
if (!v && (!strcmp(str, "data"))) {
#endif
int mod = core->search->align;
int mod = core->search->params.align;
if (mod < 1) {
mod = 1;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ static int rz_core_magic_at(RzCore *core, const char *file, ut64 addr, int depth
ck = NULL;
#endif
{
int mod = core->search->align;
int mod = core->search->params.align;
if (mod) {
ret = mod; // adelta%addr + deR_ABS(mod-adelta)+1;
goto seek_exit;
Expand Down
50 changes: 30 additions & 20 deletions librz/core/cmd/cmd_search.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static int _cb_hit(RzSearchKeyword *kw, void *user, ut64 addr) {
const RzSearch *search = core->search;
ut64 base_addr = 0;
bool use_color = core->print->flags & RZ_PRINT_FLAGS_COLOR;
int keyword_len = kw ? kw->keyword_length + (search->mode == RZ_SEARCH_DELTAKEY) : 0;
int keyword_len = kw ? kw->keyword_length + (search->params.search_mode == RZ_SEARCH_DELTAKEY) : 0;

if (param->searchshow && kw && kw->keyword_length > 0) {
int len, i, extra, mallocsize;
Expand Down Expand Up @@ -181,12 +181,12 @@ static void do_string_search(RzCore *core, struct search_parameters *param) {
if (!param->searchflags && param->outmode != RZ_MODE_JSON) {
rz_cons_printf("fs hits\n");
}
core->search->inverse = param->inverse;
core->search->params.inverse = param->inverse;
// TODO Bad but is to be compatible with the legacy behavior
if (param->inverse) {
core->search->maxhits = 1;
core->search->params.maxhits = 1;
}
if (core->search->n_kws > 0) {
if (core->search->params.n_kws > 0) {
/* set callback */
/* TODO: handle last block of data */
/* TODO: handle ^C */
Expand All @@ -196,7 +196,7 @@ static void do_string_search(RzCore *core, struct search_parameters *param) {
if (!(buf = malloc(core->blocksize))) {
return;
}
if (search->bckwrds) {
if (search->params.backwards) {
rz_search_string_prepare_backward(search);
}
rz_cons_break_push(NULL, NULL);
Expand All @@ -211,31 +211,31 @@ static void do_string_search(RzCore *core, struct search_parameters *param) {
break;
}
if (param->outmode != RZ_MODE_JSON) {
RzSearchKeyword *kw = rz_list_first(core->search->kws);
RzSearchKeyword *kw = rz_list_first(core->search->params.kws);
int lenstr = kw ? kw->keyword_length : 0;
const char *bytestr = lenstr > 1 ? "bytes" : "byte";
rz_cons_printf("Searching %d %s in [0x%" PFMT64x "-0x%" PFMT64x "]\n",
kw ? kw->keyword_length : 0, bytestr, itv.addr, rz_itv_end(itv));
}
if (!core->search->bckwrds) {
if (!core->search->params.backwards) {
RzListIter *it;
RzSearchKeyword *kw;
rz_list_foreach (core->search->kws, it, kw) {
rz_list_foreach (core->search->params.kws, it, kw) {
kw->last = 0;
}
}

const ut64 from = itv.addr, to = rz_itv_end(itv),
from1 = search->bckwrds ? to : from,
to1 = search->bckwrds ? from : to;
from1 = search->params.backwards ? to : from,
to1 = search->params.backwards ? from : to;
ut64 len;
for (at = from1; at != to1; at = search->bckwrds ? at - len : at + len) {
for (at = from1; at != to1; at = search->params.backwards ? at - len : at + len) {
print_search_progress(at, to1, search->nhits, param);
if (rz_cons_is_breaked()) {
rz_cons_printf("\n\n");
break;
}
if (search->bckwrds) {
if (search->params.backwards) {
len = RZ_MIN(core->blocksize, at - from);
// TODO prefix_read_at
if (!rz_io_is_valid_offset(core->io, at - len, 0)) {
Expand All @@ -261,7 +261,7 @@ static void do_string_search(RzCore *core, struct search_parameters *param) {
len -= PRIVATE_KEY_SEARCH_LENGTH - 1;
}
}
if (core->search->maxhits > 0 && core->search->nhits >= core->search->maxhits) {
if (core->search->params.maxhits > 0 && core->search->nhits >= core->search->params.maxhits) {
goto done;
}
}
Expand Down Expand Up @@ -340,12 +340,12 @@ static bool setup_params(RzCore *core, struct search_parameters *param) {
param->mode = rz_config_get(core->config, "search.in");
param->boundaries = rz_core_get_boundaries_prot(core, -1, param->mode, "search");

core->search->align = rz_config_get_i(core->config, "search.align");
core->search->params.align = rz_config_get_i(core->config, "search.align");
param->searchflags = rz_config_get_i(core->config, "search.flags");
core->search->maxhits = rz_config_get_i(core->config, "search.maxhits");
core->search->params.maxhits = rz_config_get_i(core->config, "search.maxhits");
param->searchprefix = rz_config_get(core->config, "search.prefix");
core->search->overlap = rz_config_get_i(core->config, "search.overlap");
core->search->bckwrds = false;
core->search->params.overlap = rz_config_get_i(core->config, "search.overlap");
core->search->params.backwards = false;

return true;
}
Expand Down Expand Up @@ -387,7 +387,7 @@ RZ_IPI RzCmdStatus rz_cmd_search_string_handler(RzCore *core, int argc, const ch
goto beach;
}
rz_search_begin(core->search);
rz_config_set_i(core->config, "search.kwidx", core->search->n_kws);
rz_config_set_i(core->config, "search.kwidx", core->search->params.n_kws);
do_string_search(core, &param);

beach:
Expand Down Expand Up @@ -443,7 +443,7 @@ RZ_IPI RzCmdStatus rz_cmd_search_hex_string_handler(RzCore *core, int argc, cons
return RZ_CMD_STATUS_ERROR;
}

rz_config_set_i(core->config, "search.kwidx", core->search->n_kws);
rz_config_set_i(core->config, "search.kwidx", core->search->params.n_kws);
do_string_search(core, &param);

beach:
Expand Down Expand Up @@ -487,9 +487,19 @@ RZ_IPI RzCmdStatus rz_cmd_search_assembly_handler(RzCore *core, int argc, const
rz_search_set_distance(core->search, (int)rz_config_get_i(core->config, "search.distance"));
rz_search_kw_add(core->search, rz_search_keyword_new_hexmask(assembled, NULL));
free(assembled);
rz_config_set_i(core->config, "search.kwidx", core->search->n_kws);
rz_config_set_i(core->config, "search.kwidx", core->search->params.n_kws);
do_string_search(core, &param);

if (param.outmode == RZ_MODE_JSON) {
RzListIter *itr;
RzSearchHit *hit;
rz_list_foreach(core->search->hits, itr, hit) {
pj_kn(param.pj, "addr", hit->addr);
pj_kn(param.pj, "size", hit->kw->keyword_length);
pj_ks(param.pj, "opstr", argv[1]);
}
}

beach:
core->num->value = core->search->nhits;
core->in_search = false;
Expand Down
2 changes: 1 addition & 1 deletion librz/core/cmd/cmd_seek.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ RZ_IPI int rz_seek_search(void *data, const char *input) {
const char *pfx = rz_config_get(core->config, "search.prefix");
const ut64 saved_from = rz_config_get_i(core->config, "search.from");
const ut64 saved_maxhits = rz_config_get_i(core->config, "search.maxhits");
int kwidx = core->search->n_kws; // (int)rz_config_get_i (core->config, "search.kwidx")-1;
int kwidx = core->search->params.n_kws; // (int)rz_config_get_i (core->config, "search.kwidx")-1;
if (kwidx < 0) {
kwidx = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions librz/core/cmd/cmd_zign.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ static bool searchRange(RzCore *core, ut64 from, ut64 to, bool rad, struct ctxSe
return false;
}
RzSignSearch *ss = rz_sign_search_new();
ss->search->align = rz_config_get_i(core->config, "search.align");
ss->search->params.align = rz_config_get_i(core->config, "search.align");
rz_sign_search_init(core->analysis, ss, minsz, searchHitCB, ctx);

rz_cons_break_push(NULL, NULL);
Expand Down Expand Up @@ -510,7 +510,7 @@ static bool search(RzCore *core, bool rad, bool only_func) {

if (useBytes && only_func) {
ss = rz_sign_search_new();
ss->search->align = rz_config_get_i(core->config, "search.align");
ss->search->params.align = rz_config_get_i(core->config, "search.align");
int minsz = rz_config_get_i(core->config, "zign.minsz");
rz_sign_search_init(core->analysis, ss, minsz, searchHitCB, &bytes_search_ctx);
}
Expand Down
2 changes: 1 addition & 1 deletion librz/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2482,7 +2482,7 @@ RZ_API bool rz_core_init(RzCore *core) {
rz_bin_bind(core->bin, &(core->analysis->binb));
rz_bin_bind(core->bin, &(core->analysis->binb));

rz_io_bind(core->io, &(core->search->iob));
rz_io_bind(core->io, &(core->search->params.iob));
rz_io_bind(core->io, &(core->print->iob));
rz_io_bind(core->io, &(core->analysis->iob));
rz_io_bind(core->io, &(core->analysis->typedb->iob));
Expand Down
42 changes: 28 additions & 14 deletions librz/include/rz_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,41 @@ typedef struct rz_search_hit_t {

typedef int (*RzSearchCallback)(RzSearchKeyword *kw, void *user, ut64 where);

typedef struct rz_search_t {
int n_kws; // hit${n_kws}_${count}
int mode;
ut32 pattern_size;
struct rz_search_t;

typedef struct rz_search_parameters_t {
RzList *boundaries;
const char *cmd_hit;
bool inverse;
bool aes_search;
bool privkey_search;
bool search_flags;
bool search_show;
ut32 string_min; // max length of strings for RZ_SEARCH_STRING
ut32 string_max; // min length of strings for RZ_SEARCH_STRING
void *data; // data used by search algorithm
void *user; // user data passed to callback
RzSearchCallback callback;
ut64 nhits;
const char *search_prefix;
RzInterval *search_itv;
int search_mode;
int n_kws; // hit${n_kws}_${count}
ut32 pattern_size;
ut64 maxhits; // search.maxhits
RzList *hits;
int distance;
int inverse;
int search_distance;
bool overlap; // whether two matches can overlap
int contiguous;
bool contiguous;
int align;
int (*update)(struct rz_search_t *s, ut64 from, const ut8 *buf, int len);
RzList *kws; // TODO: Use rz_search_kw_new ()
RzIOBind iob;
char bckwrds;
bool backwards;
} RzSearchParameters;

typedef struct rz_search_t {
void *data; // data used by search algorithm
void *user; // user data passed to callback
RzSearchCallback callback;
ut64 nhits;
RzList /*<RzSearchHit>*/ *hits;
RzSearchParameters params;
} RzSearch;

#ifdef RZ_API
Expand All @@ -83,7 +97,7 @@ RZ_API int rz_search_set_mode(RzSearch *s, int mode);
RZ_API RzSearch *rz_search_free(RzSearch *s);

/* keyword management */
RZ_API RzList *rz_search_find(RzSearch *s, ut64 addr, const ut8 *buf, int len);
RZ_API RzList /*<RzSearchHit>*/ *rz_search_find(RzSearch *s, ut64 addr, const ut8 *buf, int len);
RZ_API int rz_search_update(RzSearch *s, ut64 from, const ut8 *buf, long len);
RZ_API int rz_search_update_i(RzSearch *s, ut64 from, const ut8 *buf, long len);

Expand Down
2 changes: 1 addition & 1 deletion librz/main/rz-find.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ static int rzfind_open_file(RzfindOptions *ro, const char *file, const ut8 *data
result = 1;
goto err;
}
rs->align = ro->align;
rs->params.align = ro->align;
rz_search_set_callback(rs, &hit, ro);
ut64 to = ro->to;
if (to == -1) {
Expand Down
4 changes: 2 additions & 2 deletions librz/search/aes-find.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ RZ_API int rz_search_aes_update(RzSearch *s, ut64 from, const ut8 *buf, int len)
RzSearchKeyword *kw;
const int old_nhits = s->nhits;

rz_list_foreach (s->kws, iter, kw) {
rz_list_foreach (s->params.kws, iter, kw) {
if (last >= 0) {
for (i = 0; i < last; i++) {
if (aes128_key_test(buf + i)) {
Expand Down Expand Up @@ -86,4 +86,4 @@ RZ_API int rz_search_aes_update(RzSearch *s, ut64 from, const ut8 *buf, int len)
}
}
return -1;
}
}
6 changes: 3 additions & 3 deletions librz/search/bytepat.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ RZ_API int rz_search_pattern(RzSearch *s, ut64 from, ut64 to) {
ut8 block[BSIZE + MAX_PATLEN], sblk[BSIZE + MAX_PATLEN + 1];
ut64 addr, bact, bytes, intaddr, rb, bproc = 0;
int nr, i, moar = 0, pcnt, cnt = 0, k = 0;
int patlen = s->pattern_size;
int patlen = s->params.pattern_size;
fnditem *root;

eprintf("Searching patterns between 0x%08" PFMT64x " and 0x%08" PFMT64x "\n", from, to);
Expand Down Expand Up @@ -90,7 +90,7 @@ RZ_API int rz_search_pattern(RzSearch *s, ut64 from, ut64 to) {
// XXX bytepattern should be used with a read callback
nr = ((bytes - bproc) < BSIZE) ? (bytes - bproc) : BSIZE;
// XXX rizin_read_at(bact, sblk, patlen);
s->iob.read_at(s->iob.io, addr, sblk, nr);
s->params.iob.read_at(s->params.iob.io, addr, sblk, nr);
sblk[patlen] = 0; // XXX

intaddr = bact;
Expand All @@ -99,7 +99,7 @@ RZ_API int rz_search_pattern(RzSearch *s, ut64 from, ut64 to) {
// TODO: handle ^C here
nr = ((bytes - bproc) < BSIZE) ? (bytes - bproc) : BSIZE;
nr += (patlen - (nr % patlen)); // tamany de bloc llegit multiple superior de tamany busqueda
rb = s->iob.read_at(s->iob.io, bproc, block, nr);
rb = s->params.iob.read_at(s->params.iob.io, bproc, block, nr);
if (rb < 1) {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion librz/search/privkey-find.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ RZ_API int rz_search_privkey_update(RzSearch *s, ut64 from, const ut8 *buf, int
return -1;
}

rz_list_foreach (s->kws, iter, kw) {
rz_list_foreach (s->params.kws, iter, kw) {
// Iteration until the remaining length is too small to contain a key.
for (i = 2; i < len - PRIVKEY_SEARCH_MIN_LENGTH; i++) {
if (memcmp(buf + i, rsa_versionmarker, sizeof(rsa_versionmarker)) &&
Expand Down
Loading

0 comments on commit 021c941

Please sign in to comment.