From 4f15eea06d84ec090e41231d8101ec3f4ac87b01 Mon Sep 17 00:00:00 2001 From: Nick Stephens Date: Tue, 3 Feb 2015 18:03:44 -0800 Subject: [PATCH] Fix #1945 - Add wide string search to rafind2 Fix styling issues in r_search_keyword_new_wide --- binr/rafind2/rafind2.c | 23 ++++++++++++++++++----- libr/include/r_search.h | 1 + libr/search/keyword.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/binr/rafind2/rafind2.c b/binr/rafind2/rafind2.c index 7aa2b230fc..8e16dab558 100644 --- a/binr/rafind2/rafind2.c +++ b/binr/rafind2/rafind2.c @@ -26,6 +26,7 @@ static ut8 *buf = NULL; static char *curfile = NULL; static ut64 bsize = 4096; static int hexstr = 0; +static int widestr = 0; static struct r_print_t *pr = NULL; static RList *keywords; @@ -48,7 +49,7 @@ static int hit(RSearchKeyword *kw, void *user, ut64 addr) { } static int show_help(char *argv0, int line) { - printf ("Usage: %s [-Xnzhv] [-b sz] [-f/t from/to] [-[m|s|e] str] [-x hex] file ..\n", argv0); + printf ("Usage: %s [-Xnzhv] [-b sz] [-f/t from/to] [-[m|s|S|e] str] [-x hex] file ..\n", argv0); if (line) return 0; printf ( " -h show this help\n" @@ -60,6 +61,7 @@ static int show_help(char *argv0, int line) { " -n do not stop on read errors\n" " -s [str] search for a specific string (can be used multiple times)\n" + " -S [str] search for a specific wide string (can be used multiple times)\n" " -x [hex] search for hexpair string (909090) (can be used multiple times)\n" " -e [regex] search for regular expression string matches\n" " -m [str] set a binary mask to be applied on keywords\n" @@ -99,9 +101,12 @@ static int rafind_open(char *file) { } if (mode == R_SEARCH_KEYWORD) { r_list_foreach (keywords, iter, kw) { - r_search_kw_add (rs, (hexstr)? - r_search_keyword_new_hex (kw, mask, NULL) : - r_search_keyword_new_str (kw, mask, NULL, 0)); + if (hexstr) + r_search_kw_add (rs, r_search_keyword_new_hex (kw, mask, NULL)); + else if (widestr) + r_search_kw_add (rs, r_search_keyword_new_wide (kw, mask, NULL, 0)); + else + r_search_kw_add (rs, r_search_keyword_new_str (kw, mask, NULL, 0)); } } else if (mode == R_SEARCH_STRING) { r_search_kw_add (rs, @@ -141,7 +146,7 @@ int main(int argc, char **argv) { int c; keywords = r_list_new (); - while ((c = getopt(argc, argv, "e:b:m:s:x:Xzf:t:rnhvZ")) != -1) { + while ((c = getopt(argc, argv, "e:b:m:s:S:x:Xzf:t:rnhvZ")) != -1) { switch (c) { case 'r': rad = 1; @@ -157,14 +162,22 @@ int main(int argc, char **argv) { case 's': mode = R_SEARCH_KEYWORD; hexstr = 0; + widestr = 0; r_list_append (keywords, optarg); break; + case 'S': + mode = R_SEARCH_KEYWORD; + hexstr = 0; + widestr = 1; + r_list_append(keywords, optarg); + break; case 'b': bsize = r_num_math (NULL, optarg); break; case 'x': mode = R_SEARCH_KEYWORD; hexstr = 1; + widestr = 0; r_list_append (keywords, optarg); break; case 'm': diff --git a/libr/include/r_search.h b/libr/include/r_search.h index 14b40dc375..a7c288679b 100644 --- a/libr/include/r_search.h +++ b/libr/include/r_search.h @@ -88,6 +88,7 @@ R_API int r_search_update_i(RSearch *s, ut64 from, const ut8 *buf, long len); 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_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); diff --git a/libr/search/keyword.c b/libr/search/keyword.c index a479c20b95..3ca596f7cb 100644 --- a/libr/search/keyword.c +++ b/libr/search/keyword.c @@ -53,6 +53,43 @@ 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) { + RSearchKeyword *kw; + int len; + const char *p2; + char *p, *str; + ut8 *bmbuf = NULL; + int bmlen = 0; + + if (bmstr) { + bmbuf = malloc (strlen (bmstr)+1); + if (!bmbuf) return NULL; + bmlen = r_hex_str2bin (bmstr, bmbuf); + if (bmlen < 1) { + free(bmbuf); + bmbuf = NULL; + } + } + + len = strlen(kwbuf); + str = malloc((len+1)*2); + for (p2=kwbuf, p=str; *p2; p+=2, p2++) { + if (ignore_case) + p[0] = tolower((const unsigned char)*p2); + else + p[0] = *p2; + p[1] = 0; + } + + kw = r_search_keyword_new ((ut8 *)str, len*2, bmbuf, bmlen, data); + free(str); + if (kw) { + kw->icase = ignore_case; + } + free(bmbuf); + return kw; +} + R_API RSearchKeyword* r_search_keyword_new_hex(const char *kwstr, const char *bmstr, const char *data) { RSearchKeyword *kw; ut8 *kwbuf, *bmbuf;