From 3f065201b18933cc436f2c541d38c2bad3bc145e Mon Sep 17 00:00:00 2001 From: Anton Kochkov Date: Fri, 3 Mar 2023 15:50:11 +0800 Subject: [PATCH] Fix string search to use proper implementation (#3414) --- librz/search/meson.build | 1 - librz/search/search.c | 35 ++++++++++++++ librz/search/strings.c | 98 ---------------------------------------- 3 files changed, 35 insertions(+), 99 deletions(-) delete mode 100644 librz/search/strings.c diff --git a/librz/search/meson.build b/librz/search/meson.build index 0df10d8dfa..ab0bf39e86 100644 --- a/librz/search/meson.build +++ b/librz/search/meson.build @@ -5,7 +5,6 @@ rz_search_sources = [ 'regexp.c', 'privkey-find.c', 'search.c', - 'strings.c' ] rz_search = library('rz_search', rz_search_sources, diff --git a/librz/search/search.c b/librz/search/search.c index 86c8eddb35..5f80b441cc 100644 --- a/librz/search/search.c +++ b/librz/search/search.c @@ -70,6 +70,41 @@ RZ_API int rz_search_set_string_limits(RzSearch *s, ut32 min, ut32 max) { return true; } +RZ_API int rz_search_strings_update(RzSearch *s, ut64 from, const ut8 *buf, int len) { + rz_return_val_if_fail(s && buf && len, -1); + + RzUtilStrScanOptions scan_opt = { + .buf_size = len, + .max_uni_blocks = s->string_max, + .min_str_length = s->string_min, + .prefer_big_endian = false, + }; + RzList *str_list = rz_list_new(); + if (!str_list) { + return 0; + } + + int count = rz_scan_strings_raw(buf, str_list, &scan_opt, from, from + len, RZ_STRING_ENC_GUESS); + if (count <= 0) { + rz_list_free(str_list); + return false; + } + + RzListIter *iter, *iter2; + RzSearchKeyword *kw; + + int matches = 0; + rz_list_foreach (s->kws, iter, kw) { + RzDetectedString *dstr; + rz_list_foreach (str_list, iter2, dstr) { + rz_search_hit_new(s, kw, dstr->addr); + matches++; + } + } + RZ_FREE_CUSTOM(str_list, rz_list_free); + return matches; +} + RZ_API int rz_search_magic_update(RzSearch *s, ut64 from, const ut8 *buf, int len) { eprintf("TODO: import librz/core/cmd_search.c /m implementation into rsearch\n"); return false; diff --git a/librz/search/strings.c b/librz/search/strings.c deleted file mode 100644 index 2f1b704a41..0000000000 --- a/librz/search/strings.c +++ /dev/null @@ -1,98 +0,0 @@ -// SPDX-FileCopyrightText: 2006-2018 pancake -// SPDX-License-Identifier: LGPL-3.0-only - -#include "rz_search.h" - -// TODO: this file needs some love -enum { - ENCODING_ASCII = 0, - ENCODING_CP850 = 1 -}; - -static char *encodings[3] = { "ascii", "cp850", NULL }; -// static int encoding = ENCODING_ASCII; // default -// encoding = resolve_encoding(config_get("cfg.encoding")); - -RZ_API int rz_search_get_encoding(const char *name) { - int i; - if (!name || !*name) { - return ENCODING_ASCII; - } - ut32 lename = strlen(name); - for (i = 0; encodings[i]; i++) { - ut32 sz = RZ_MIN(strlen(encodings[i]), lename); - if (!rz_str_ncasecmp(name, encodings[i], sz)) { - return i; - } - } - return ENCODING_ASCII; -} - -static bool is_encoded(int encoding, unsigned char c) { - switch (encoding) { - case ENCODING_ASCII: - break; - case ENCODING_CP850: - switch (c) { - // CP850 - case 128: // cedilla - case 133: // a grave - case 135: // minicedilla - case 160: // a acute - case 161: // i acute - case 129: // u dieresi - case 130: // e acute - case 139: // i dieresi - case 162: // o acute - case 163: // u acute - case 164: // enye - case 165: // enyemay - case 181: // A acute - case 144: // E acute - case 214: // I acute - case 224: // O acute - case 233: // U acute - return true; - } - break; - } - return false; -} - -RZ_API int rz_search_strings_update(RzSearch *s, ut64 from, const ut8 *buf, int len) { - int i = 0; - int matches = 0; - char str[4096]; - RzListIter *iter; - RzSearchKeyword *kw; - - rz_list_foreach (s->kws, iter, kw) { - for (i = 0; i < len; i++) { - char ch = buf[i]; - // non-cp850 encoded - if (IS_PRINTABLE(ch) || IS_WHITESPACE(ch) || is_encoded(0, ch)) { - str[matches] = ch; - if (matches < sizeof(str)) { - matches++; - } - } else { - /* wide char check \x??\x00\x??\x00 */ - if (matches && i + 2 < len && buf[i + 2] == '\0' && buf[i] == '\0' && buf[i + 1] != '\0') { - return 1; // widechar - } - /* check if the length fits on our request */ - if (matches >= s->string_min && (s->string_max == 0 || matches <= s->string_max)) { - str[matches] = '\0'; - int len = strlen(str); - if (len > 2) { - ut64 off = (ut64)from + i - matches; - rz_search_hit_new(s, kw, off); - } - fflush(stdout); - } - matches = 0; - } - } - } - return 0; -}