Fix string search to use proper implementation (#3414)

This commit is contained in:
Anton Kochkov 2023-03-03 15:50:11 +08:00 committed by GitHub
parent ac1ea726b0
commit 3f065201b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 99 deletions

View file

@ -5,7 +5,6 @@ rz_search_sources = [
'regexp.c',
'privkey-find.c',
'search.c',
'strings.c'
]
rz_search = library('rz_search', rz_search_sources,

View file

@ -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;

View file

@ -1,98 +0,0 @@
// SPDX-FileCopyrightText: 2006-2018 pancake <pancake@nopcode.org>
// 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;
}