2021-01-08 02:36:48 +00:00
|
|
|
/* radare - LGPL - Copyright 2008-2020 - pancake, TheLemonMan */
|
2009-02-15 22:32:17 +00:00
|
|
|
|
|
|
|
#include "r_search.h"
|
2021-12-22 23:36:51 +00:00
|
|
|
#include "search.h"
|
2011-09-14 00:07:06 +00:00
|
|
|
#include <r_regex.h>
|
2009-02-15 22:32:17 +00:00
|
|
|
|
2021-12-22 23:36:51 +00:00
|
|
|
R_IPI int search_regex_read(RSearch *s, ut64 from, ut64 to) {
|
|
|
|
RSearchKeyword *kw;
|
|
|
|
RListIter *iter;
|
|
|
|
RRegexMatch match;
|
|
|
|
RRegex rx = {0};
|
|
|
|
const int old_nhits = s->nhits;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ut64 buflen = 0x1000;
|
|
|
|
ut8 *buf = malloc (buflen);
|
|
|
|
if (!buf) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_list_foreach (s->kws, iter, kw) {
|
|
|
|
ut64 addr = from;
|
|
|
|
int reflags = R_REGEX_EXTENDED;
|
|
|
|
|
|
|
|
if (kw->icase) {
|
|
|
|
reflags |= R_REGEX_ICASE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r_regex_init (&rx, (char *)kw->bin_keyword, reflags)) {
|
2022-08-01 07:56:51 +00:00
|
|
|
R_LOG_ERROR ("Cannot compile '%s' regexp", kw->bin_keyword);
|
2021-12-22 23:36:51 +00:00
|
|
|
ret = -1;
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: allow user to configure according to the maximum expected
|
|
|
|
// match length to prevent FN on matches that span boundaries.
|
|
|
|
while (addr < to) { // get buffer
|
|
|
|
if (s->consb.is_breaked ()) {
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
int len = R_MIN (to - addr, buflen);
|
|
|
|
if (!s->iob.read_at (s->iob.io, addr, buf, len)) {
|
|
|
|
ret = -1; // failed to read
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
match.rm_so = 0;
|
|
|
|
match.rm_eo = len;
|
|
|
|
int m = r_regex_exec (&rx, (char *)buf, 1, &match, R_REGEX_STARTEND);
|
|
|
|
if (!m) { // match
|
|
|
|
ut32 mtch_len = match.rm_eo - match.rm_so;
|
|
|
|
if (match.rm_eo < match.rm_so || !mtch_len) {
|
|
|
|
// <= zero length match (ie /a*/ matches everything)
|
|
|
|
ret = -1;
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
// match extends to end of this buffer, but maybe even further?, so try again at match start
|
|
|
|
if (match.rm_eo == len && !match.rm_so && mtch_len < len) {
|
|
|
|
addr += match.rm_so;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int t = r_search_hit_sz (s, kw, addr + match.rm_so, mtch_len);
|
|
|
|
if (!t) {
|
|
|
|
ret = -1;
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
if (t > 1) { // max matches reached
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
// adjust where buffer starts next loop
|
|
|
|
if (s->overlap) {
|
|
|
|
addr += match.rm_so + 1;
|
|
|
|
} else {
|
|
|
|
addr += match.rm_eo;
|
|
|
|
}
|
|
|
|
} else if (m == R_REGEX_NOMATCH) {
|
|
|
|
// if a match exists accross buffer boundary, this will still
|
|
|
|
// find it, unless start of match is withen first 7/8th of buffer
|
|
|
|
addr += buflen - (buflen / 8);
|
|
|
|
} else { // regex error
|
|
|
|
ret = -1;
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
beach:
|
|
|
|
r_regex_fini (&rx);
|
|
|
|
free (buf);
|
|
|
|
if (!ret) {
|
|
|
|
ret = s->nhits - old_nhits;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-12 10:46:46 +00:00
|
|
|
R_IPI int search_regexp_update(RSearch *s, ut64 from, const ut8 *buf, int len) {
|
2014-06-04 21:35:53 +00:00
|
|
|
RSearchKeyword *kw;
|
2010-08-08 17:03:51 +00:00
|
|
|
RListIter *iter;
|
2014-06-04 21:35:53 +00:00
|
|
|
RRegexMatch match;
|
2021-01-08 02:36:48 +00:00
|
|
|
RRegex rx = {0};
|
2017-09-20 15:00:18 +00:00
|
|
|
const int old_nhits = s->nhits;
|
|
|
|
int ret = 0;
|
2009-02-15 22:32:17 +00:00
|
|
|
|
2010-08-08 17:03:51 +00:00
|
|
|
r_list_foreach (s->kws, iter, kw) {
|
2011-09-14 00:07:06 +00:00
|
|
|
int reflags = R_REGEX_EXTENDED;
|
2009-02-15 22:32:17 +00:00
|
|
|
|
2018-09-13 08:17:26 +00:00
|
|
|
if (kw->icase) {
|
2011-09-14 00:07:06 +00:00
|
|
|
reflags |= R_REGEX_ICASE;
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2009-02-15 22:32:17 +00:00
|
|
|
|
2021-01-08 02:36:48 +00:00
|
|
|
if (r_regex_init (&rx, (char *)kw->bin_keyword, reflags)) {
|
2022-08-01 07:56:51 +00:00
|
|
|
R_LOG_ERROR ("Cannot compile '%s' regexp", kw->bin_keyword);
|
2009-02-16 23:09:40 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-02-15 22:32:17 +00:00
|
|
|
|
2014-06-04 21:35:53 +00:00
|
|
|
match.rm_so = 0;
|
|
|
|
match.rm_eo = len;
|
|
|
|
|
2021-01-08 02:36:48 +00:00
|
|
|
while (!r_regex_exec (&rx, (char *)buf, 1, &match, R_REGEX_STARTEND)) {
|
2021-12-19 15:56:09 +00:00
|
|
|
if (match.rm_eo <= match.rm_so) {
|
|
|
|
// empty match
|
|
|
|
match.rm_so++;
|
|
|
|
match.rm_eo = len;
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-20 15:00:18 +00:00
|
|
|
int t = r_search_hit_new (s, kw, from + match.rm_so);
|
|
|
|
if (!t) {
|
|
|
|
ret = -1;
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
if (t > 1) {
|
|
|
|
goto beach;
|
|
|
|
}
|
2014-06-04 22:32:01 +00:00
|
|
|
/* Setup the boundaries for R_REGEX_STARTEND */
|
|
|
|
match.rm_so = match.rm_eo;
|
|
|
|
match.rm_eo = len;
|
2017-09-20 15:00:18 +00:00
|
|
|
}
|
2009-02-15 22:32:17 +00:00
|
|
|
}
|
2014-06-04 21:35:53 +00:00
|
|
|
|
2017-09-20 15:00:18 +00:00
|
|
|
beach:
|
2021-01-08 02:36:48 +00:00
|
|
|
r_regex_fini (&rx);
|
2017-09-20 15:00:18 +00:00
|
|
|
if (!ret) {
|
|
|
|
ret = s->nhits - old_nhits;
|
|
|
|
}
|
|
|
|
return ret;
|
2009-02-15 22:32:17 +00:00
|
|
|
}
|