Fix /w and /wi, add tests, minor code cleanup ##search

This commit is contained in:
pancake 2022-10-29 01:17:08 +02:00 committed by GitHub
parent 432791f8ce
commit 9cef5d68ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 82 additions and 58 deletions

View File

@ -9,7 +9,7 @@ static int cmd_search(void *data, const char *input);
#define USE_EMULATION 1
#define AES_SEARCH_LENGTH 40
#define SM4_SEARCH_LENGTH 24
#define SM4_SEARCH_LENGTH 24
#define PRIVATE_KEY_SEARCH_LENGTH 11
static const char *help_msg_search_wide_string[] = {
@ -4425,72 +4425,55 @@ reread:
break;
}
if (input[2]) {
if (input[2] == '?') {
if (input[2] == '?') { // "/w?"
r_core_cmd_help (core, help_msg_search_wide_string);
break;
}
if (input[1] == 'j' || input[2] == 'j') {
if (input[1] == 'j' || input[2] == 'j') { // "/wj"
param.outmode = R_MODE_JSON;
}
if (input[1] == 'i' || input[2] == 'i') {
if (input[1] == 'i' || input[2] == 'i') { // "/wi"
ignorecase = true;
}
} else {
param.outmode = R_MODE_RADARE;
}
size_t shift = 1 + ignorecase;
if (param.outmode == R_MODE_JSON) {
shift++;
}
size_t strstart;
const char *p2;
char *p;
strstart = shift + 1;
len = strlen (input + strstart);
inp = calloc ((len + 1), 2);
for (p2 = input + strstart, p = inp; *p2; p += 2, p2++) {
if (ignorecase) {
p[0] = tolower ((const ut8) *p2);
} else {
p[0] = *p2;
}
p[1] = 0;
}
size_t strstart = shift + 1;
r_search_reset (core->search, R_SEARCH_KEYWORD);
r_search_set_distance (core->search, (int)
r_config_get_i (core->config, "search.distance"));
RSearchKeyword *skw;
skw = r_search_keyword_new ((const ut8 *) inp, len * 2, NULL, 0, NULL);
free (inp);
RSearchKeyword *skw = r_search_keyword_new_wide (input + strstart, NULL, NULL, ignorecase);
if (skw) {
skw->icase = ignorecase;
r_search_kw_add (core->search, skw);
r_search_begin (core->search);
dosearch = true;
} else {
eprintf ("Invalid keyword\n");
R_LOG_ERROR ("Invalid keyword");
break;
}
break;
case 'i': // "/i"
if (input[1] == '?') {
r_core_cmd_help (core, help_msg_search_string_no_case);
break;
}
if (input[param_offset - 1] != ' ') {
eprintf ("Missing ' ' after /i\n");
R_LOG_ERROR ("Missing ' ' after /i");
r_core_return_value (core, R_CMD_RC_FAILURE);
goto beach;
}
ignorecase = true;
// fallthrough
case 'j': // "/j"
if (input[0] == 'j' && input[1] == ' ') {
param.outmode = R_MODE_JSON;
}
// fallthrough
case ' ': // "/ " search string
inp = strdup (input + 1 + ignorecase + (param.outmode == R_MODE_JSON ? 1 : 0));
len = r_str_unescape (inp);
#if 0
if (!json) {
eprintf ("Searching %d byte(s) from 0x%08"PFMT64x " to 0x%08"PFMT64x ": ",
@ -4501,45 +4484,40 @@ reread:
eprintf ("\n");
}
#endif
r_search_reset (core->search, R_SEARCH_KEYWORD);
r_search_set_distance (core->search, (int)
r_config_get_i (core->config, "search.distance"));
{
RSearchKeyword *skw;
skw = r_search_keyword_new ((const ut8 *) inp, len, NULL, 0, NULL);
free (inp);
const int distance = r_config_get_i (core->config, "search.distance");
inp = strdup (input + 1 + ignorecase + (param.outmode == R_MODE_JSON ? 1 : 0));
len = r_str_unescape (inp);
r_search_reset (core->search, R_SEARCH_KEYWORD);
r_search_set_distance (core->search, distance);
RSearchKeyword *skw = r_search_keyword_new_str (inp, NULL, NULL, ignorecase);
if (skw) {
skw->icase = ignorecase;
skw->type = R_SEARCH_KEYWORD_TYPE_STRING;
r_search_kw_add (core->search, skw);
r_search_begin (core->search);
dosearch = true;
} else {
eprintf ("Invalid keyword\n");
break;
R_LOG_ERROR ("Invalid keyword");
}
}
r_search_begin (core->search);
dosearch = true;
break;
case 'k': // "/k" Rabin Karp String search
inp = r_str_trim_dup (input + 1);
len = r_str_unescape (inp);
r_search_reset (core->search, R_SEARCH_RABIN_KARP);
r_search_set_distance (core->search, (int)r_config_get_i (core->config, "search.distance"));
{
RSearchKeyword *skw;
skw = r_search_keyword_new ((const ut8 *)inp, len, NULL, 0, NULL);
inp = r_str_trim_dup (input + 1);
len = r_str_unescape (inp);
r_search_reset (core->search, R_SEARCH_RABIN_KARP);
r_search_set_distance (core->search, (int)r_config_get_i (core->config, "search.distance"));
RSearchKeyword *skw = r_search_keyword_new_str (inp, NULL, NULL, ignorecase);
free (inp);
if (skw) {
skw->icase = ignorecase;
skw->type = R_SEARCH_KEYWORD_TYPE_STRING;
r_search_kw_add (core->search, skw);
r_search_begin (core->search);
dosearch_read = true;
} else {
eprintf ("Invalid keyword\n");
break;
R_LOG_ERROR ("Invalid keyword");
}
}
r_search_begin (core->search);
dosearch_read = true;
break;
case 'e': // "/e" match regexp
if (input[1] == '?') {
@ -4559,7 +4537,7 @@ reread:
r_search_begin (core->search);
dosearch_read = true;
} else {
eprintf ("Missing regex\n");
R_LOG_ERROR ("Missing regex");
}
break;
case 'E': // "/E"
@ -4580,7 +4558,7 @@ reread:
r_search_begin (core->search);
dosearch = true;
} else {
eprintf ("Missing delta\n");
R_LOG_ERROR ("Missing delta");
}
break;
case 'h': // "/h"
@ -4607,7 +4585,7 @@ reread:
search_hash (core, arg, p, min, max, &param);
}
} else {
eprintf ("Missing hash. See ph?\n");
R_LOG_ERROR ("Missing hash. See ph?");
}
free (arg);
}

View File

@ -41,8 +41,8 @@ typedef struct r_search_keyword_t {
ut32 binmask_length;
void *data;
int count;
int kwidx;
int icase; // ignore case
int kwidx; // index
bool icase; // ignore case
int type;
ut64 last; // last hit hint
} RSearchKeyword;
@ -109,8 +109,8 @@ R_API int r_search_maps(RSearch *s, RList /*<<RIOMap>>*/ *maps);
R_API void r_search_keyword_free(RSearchKeyword *kw);
R_API RSearchKeyword* r_search_keyword_new(const ut8 *kw, int kwlen, const ut8 *bm, int bmlen, const char *data);
R_API RSearchKeyword* r_search_keyword_new_str(const char *kw, const char *bm, const char *data, int icase);
R_API RSearchKeyword* r_search_keyword_new_wide(const char *kw, const char *bm, const char *data, int icase);
R_API RSearchKeyword* r_search_keyword_new_str(const char *kw, const char *bm, const char *data, bool icase);
R_API RSearchKeyword* r_search_keyword_new_wide(const char *kw, const char *bm, const char *data, bool icase);
R_API RSearchKeyword* r_search_keyword_new_hex(const char *kwstr, const char *bmstr, const char *data);
R_API RSearchKeyword* r_search_keyword_new_hexmask(const char *kwstr, const char *data);
R_API RSearchKeyword *r_search_keyword_new_regexp(const char *str, const char *data);

View File

@ -54,7 +54,7 @@ R_API void r_search_keyword_free(RSearchKeyword *kw) {
free (kw);
}
R_API RSearchKeyword* r_search_keyword_new_str(const char *kwbuf, const char *bmstr, const char *data, int ignore_case) {
R_API RSearchKeyword* r_search_keyword_new_str(const char *kwbuf, const char *bmstr, const char *data, bool ignore_case) {
r_return_val_if_fail (kwbuf, NULL);
ut8 *bmbuf = NULL;
int bmlen = 0;
@ -78,7 +78,7 @@ R_API RSearchKeyword* r_search_keyword_new_str(const char *kwbuf, const char *bm
return kw;
}
R_API RSearchKeyword* r_search_keyword_new_wide(const char *kwbuf, const char *bmstr, const char *data, int ignore_case) {
R_API RSearchKeyword* r_search_keyword_new_wide(const char *kwbuf, const char *bmstr, const char *data, bool ignore_case) {
RSearchKeyword *kw;
int len;
const char *p2;

View File

@ -0,0 +1,46 @@
NAME=trailing comment
FILE=malloc://1024
ARGS=-w
CMDS=<<EOF
s 0x20
ww hello
s 0x30
ww Hello
s 0x40
ww world
s 0x60
w hello # shouldnt be listed as its not wide!
s 0x80
ww WORLD
s 0
x 0xa0
?e sensitive
/w hello
/w world
?e insensitive
/wi hello
/wi world
EOF
EXPECT=<<EOF
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
0x00000000 0000 0000 0000 0000 0000 0000 0000 0000 ................
0x00000010 0000 0000 0000 0000 0000 0000 0000 0000 ................
0x00000020 6800 6500 6c00 6c00 6f00 0000 0000 0000 h.e.l.l.o.......
0x00000030 4800 6500 6c00 6c00 6f00 0000 0000 0000 H.e.l.l.o.......
0x00000040 7700 6f00 7200 6c00 6400 0000 0000 0000 w.o.r.l.d.......
0x00000050 0000 0000 0000 0000 0000 0000 0000 0000 ................
0x00000060 6865 6c6c 6f00 0000 0000 0000 0000 0000 hello...........
0x00000070 0000 0000 0000 0000 0000 0000 0000 0000 ................
0x00000080 5700 4f00 5200 4c00 4400 0000 0000 0000 W.O.R.L.D.......
0x00000090 0000 0000 0000 0000 0000 0000 0000 0000 ................
sensitive
0x00000020 hit0_0 680065006c006c006f00
0x00000040 hit1_0 77006f0072006c006400
insensitive
0x00000020 hit2_0 680065006c006c006f00
0x00000030 hit2_1 480065006c006c006f00
0x00000040 hit3_0 77006f0072006c006400
0x00000080 hit3_1 57004f0052004c004400
EOF
RUN