From 8f34566b9228fd707809ab06c665a6b2e7404b66 Mon Sep 17 00:00:00 2001 From: Rot127 Date: Tue, 10 Dec 2024 14:50:35 -0500 Subject: [PATCH] Sorry mister, we don't do search no longer. --- librz/core/cmd/cmd_search.c | 903 +----------------------------------- 1 file changed, 1 insertion(+), 902 deletions(-) diff --git a/librz/core/cmd/cmd_search.c b/librz/core/cmd/cmd_search.c index a6c656a0b17..1755700602b 100644 --- a/librz/core/cmd/cmd_search.c +++ b/librz/core/cmd/cmd_search.c @@ -1723,905 +1723,4 @@ static void __core_cmd_search_asm_byteswap(RzCore *core, int nth) { } } -RZ_IPI int rz_cmd_search(void *data, const char *input) { - bool dosearch = false; - int ret = true; - RzCore *core = (RzCore *)data; - struct search_parameters param = { - .core = core, - .cmd_hit = rz_config_get(core->config, "cmd.hit"), - .outmode = 0, - .inverse = false, - .aes_search = false, - .privkey_search = false, - }; - if (!param.cmd_hit) { - param.cmd_hit = ""; - } - RzSearch *search = core->search; - int ignorecase = false; - int param_offset = 2; - char *inp; - if (!core || !core->io) { - RZ_LOG_ERROR("core: Can't search if we don't have an open file.\n"); - return false; - } - if (core->in_search) { - RZ_LOG_ERROR("core: Can't search from within a search.\n"); - return false; - } - if (input[0] == '/') { - if (core->lastsearch) { - input = core->lastsearch; - } else { - RZ_LOG_ERROR("core: No previous search done\n"); - return false; - } - } else { - free(core->lastsearch); - core->lastsearch = rz_str_dup(input); - } - - core->in_search = true; - rz_flag_space_push(core->flags, "search"); - const ut64 search_from = rz_config_get_i(core->config, "search.from"), - search_to = rz_config_get_i(core->config, "search.to"); - if (search_from > search_to && search_to) { - RZ_LOG_ERROR("core: search.from > search.to is not supported\n"); - ret = false; - goto beach; - } - // {.addr = UT64_MAX, .size = 0} means search range is unspecified - RzInterval search_itv = { search_from, search_to - search_from }; - bool empty_search_itv = search_from == search_to && search_from != UT64_MAX; - if (empty_search_itv) { - RZ_LOG_ERROR("core: `from` address is equal `to`\n"); - ret = false; - goto beach; - } - // TODO full address cannot be represented, shrink 1 byte to [0, UT64_MAX) - if (search_from == UT64_MAX && search_to == UT64_MAX) { - search_itv.addr = 0; - search_itv.size = UT64_MAX; - } - - c = 0; - - searchshow = rz_config_get_i(core->config, "search.show"); - param.mode = rz_config_get(core->config, "search.in"); - param.boundaries = rz_core_get_boundaries_select(core, "search.from", "search.to", "search.in"); - - /* - this introduces a bug until we implement backwards search - for all search types - if (__to < __from) { - eprintf ("Invalid search range. Check 'e search.{from|to}'\n"); - return false; - } - since the backward search will be implemented soon I'm not gonna stick - checks for every case in switch // jjdredd - remove when everything is done - */ - - core->search->align = rz_config_get_i(core->config, "search.align"); - searchflags = rz_config_get_i(core->config, "search.flags"); - core->search->maxhits = rz_config_get_i(core->config, "search.maxhits"); - searchprefix = rz_config_get(core->config, "search.prefix"); - core->search->overlap = rz_config_get_i(core->config, "search.overlap"); - core->search->bckwrds = false; - - /* Quick & dirty check for json output */ - if (input[0] && (input[1] == 'j') && (input[0] != ' ')) { - param.outmode = RZ_MODE_JSON; - param_offset++; - } - param.pj = pj_new(); - -reread: - switch (*input) { - case '!': - input++; - param.inverse = true; - goto reread; - case 'b': // "/b" backward search - if (*(++input) == '?') { - RZ_LOG_ERROR("core: Usage: /b [value] backward search, see '/?'\n"); - goto beach; - } - search->bckwrds = true; - if (core->offset) { - RzInterval itv = { 0, core->offset }; - if (!rz_itv_overlap(search_itv, itv)) { - ret = false; - goto beach; - } else { - search_itv = rz_itv_intersect(search_itv, itv); - } - } - goto reread; - case 'o': { // "/o" print the offset of the Previous opcode - ut64 addr, n = input[param_offset - 1] ? rz_num_math(core->num, input + param_offset) : 1; - n = RZ_ABS((st64)n); - if (((st64)n) < 1) { - n = 1; - } - if (!rz_core_prevop_addr(core, core->offset, n, &addr)) { - addr = UT64_MAX; - (void)rz_core_asm_bwdis_len(core, NULL, &addr, n); - } - if (param.outmode == RZ_MODE_JSON) { - rz_cons_printf("[%" PFMT64u "]", addr); - } else { - rz_cons_printf("0x%08" PFMT64x "\n", addr); - } - break; - } - case 'O': { // "/O" alternative to "/o" - ut64 addr, n = input[param_offset - 1] ? rz_num_math(core->num, input + param_offset) : 1; - if (!n) { - n = 1; - } - addr = rz_core_prevop_addr_force(core, core->offset, n); - if (param.outmode == RZ_MODE_JSON) { - rz_cons_printf("[%" PFMT64u "]", addr); - } else { - rz_cons_printf("0x%08" PFMT64x "\n", addr); - } - break; - } - case 'r': // "/r" - { - ut64 n = (input[1] == ' ' || (input[1] && input[2] == ' ')) - ? rz_num_math(core->num, input + 2) - : UT64_MAX; - if (n == 0LL) { - RZ_LOG_ERROR("core: Cannot find null references.\n"); - break; - } - switch (input[1]) { - case 'c': // "/rc" - { - RzListIter *iter; - RzIOMap *map; - rz_list_foreach (param.boundaries, iter, map) { - eprintf("-- 0x%" PFMT64x " 0x%" PFMT64x "\n", map->itv.addr, rz_itv_end(map->itv)); - rz_core_analysis_search(core, map->itv.addr, rz_itv_end(map->itv), n, 'c'); - } - } break; - case 'a': // "/ra" - { - RzListIter *iter; - RzIOMap *map; - rz_list_foreach (param.boundaries, iter, map) { - eprintf("-- 0x%" PFMT64x " 0x%" PFMT64x "\n", map->itv.addr, rz_itv_end(map->itv)); - rz_core_analysis_search(core, map->itv.addr, rz_itv_end(map->itv), n, 0); - } - } break; - case 'r': // "/rr" - read refs - { - RzListIter *iter; - RzIOMap *map; - rz_list_foreach (param.boundaries, iter, map) { - eprintf("-- 0x%" PFMT64x " 0x%" PFMT64x "\n", map->itv.addr, rz_itv_end(map->itv)); - rz_core_analysis_search(core, map->itv.addr, rz_itv_end(map->itv), n, 'r'); - } - } break; - case 'w': // "/rw" - write refs - { - RzListIter *iter; - RzIOMap *map; - rz_list_foreach (param.boundaries, iter, map) { - eprintf("-- 0x%" PFMT64x " 0x%" PFMT64x "\n", map->itv.addr, rz_itv_end(map->itv)); - rz_core_analysis_search(core, map->itv.addr, rz_itv_end(map->itv), n, 'w'); - } - } break; - case ' ': // "/r $$" - case 0: // "/r" - { - RzListIter *iter; - RzIOMap *map; - rz_list_foreach (param.boundaries, iter, map) { - ut64 from = map->itv.addr; - ut64 to = rz_itv_end(map->itv); - if (input[param_offset - 1] == ' ') { - rz_core_analysis_search(core, from, to, - rz_num_math(core->num, input + 2), 0); - do_ref_search(core, rz_num_math(core->num, input + 2), from, to, ¶m); - } else { - rz_core_analysis_search(core, from, to, core->offset, 0); - do_ref_search(core, core->offset, from, to, ¶m); - } - if (rz_cons_is_breaked()) { - break; - } - } - } break; - case '?': - rz_core_cmd_help(core, help_msg_slash_r); - break; - } - } break; - case 'a': // "/a" - if (input[1] == '?') { - rz_core_cmd_help(core, help_msg_slash_a); - } else if (input[1] == 'd') { // "ad" - dosearch = 0; - do_asm_search(core, ¶m, input + 2, 0, search_itv); - } else if (input[1] == 'e') { // "ae" - dosearch = 0; - do_asm_search(core, ¶m, input + 2, 'e', search_itv); - } else if (input[1] == 'c') { // "/ac" - dosearch = 0; - do_asm_search(core, ¶m, input + 2, 'c', search_itv); - } else if (input[1] == 'o') { // "/ao" - dosearch = 0; - do_asm_search(core, ¶m, input + 2, 'o', search_itv); - } else if (input[1] == 'a') { // "/aa" - dosearch = 0; - do_asm_search(core, ¶m, input + 2, 'a', search_itv); - } else if (input[1] == 'i') { // "/ai" - do_asm_search(core, ¶m, input + 2, 'i', search_itv); - } else if (input[1] == '1') { // "a1" - __core_cmd_search_asm_byteswap(core, (int)rz_num_math(core->num, input + 2)); - } else if (input[1] == 'I') { // "/aI" - infinite - __core_cmd_search_asm_infinite(core, rz_str_trim_head_ro(input + 1)); - } else if (input[1] == ' ') { - if (input[param_offset - 1]) { - char *kwd = rz_core_asm_search(core, input + param_offset); - if (!kwd) { - ret = false; - goto beach; - } - dosearch = true; - rz_search_reset(core->search, RZ_SEARCH_KEYWORD); - rz_search_set_distance(core->search, (int)rz_config_get_i(core->config, "search.distance")); - rz_search_kw_add(core->search, - rz_search_keyword_new_hexmask(kwd, NULL)); - free(kwd); - } - } else if (input[1] == 's') { - if (input[2] == 'l') { // "asl" - rz_core_cmd0(core, "asl"); - } else { // "as" - do_syscall_search(core, ¶m); - } - dosearch = false; - } else { - dosearch = do_analysis_search(core, ¶m, input + 1); - } - break; - case 'c': { // "/c" - dosearch = true; - switch (input[1]) { - case 'c': // "/cc" - { - ret = false; - char *space = strchr(input, ' '); - const char *arg = space ? rz_str_trim_head_ro(space + 1) : NULL; - if (!arg || input[2] == '?') { - RZ_LOG_ERROR("core: Usage: /cc[aAdlpb] [hashname] [hexpairhashvalue]\n"); - RZ_LOG_ERROR("core: /cca - lowercase alphabet chars only\n"); - RZ_LOG_ERROR("core: /ccA - uppercase alphabet chars only\n"); - RZ_LOG_ERROR("core: /ccl - letters (lower + upper alphabet chars)\n"); - RZ_LOG_ERROR("core: /ccd - digits (only numbers)\n"); - RZ_LOG_ERROR("core: /ccp - printable (alpha + digit)\n"); - RZ_LOG_ERROR("core: /ccb - binary (any number is valid)\n"); - goto beach; - } - char *s = rz_str_dup(arg); - char *sp = strchr(s, ' '); - int mode = input[2]; - if (sp) { - *sp = 0; - sp++; - char *hashName = s; - ut8 *hashValue = (ut8 *)rz_str_dup(sp); - if (hashValue) { - if (!rz_str_startswith((const char *)hashValue, "0x")) { - // TODO: support bigger hashes - int hashLength = 4; - ut32 n = (ut32)rz_num_get(NULL, (const char *)hashValue); - memcpy(hashValue, (const ut8 *)&n, sizeof(ut32)); - search_collisions(core, hashName, hashValue, hashLength, mode); - } else { - int hashLength = rz_hex_str2bin(sp, hashValue); - if (hashLength > 0) { - search_collisions(core, hashName, hashValue, hashLength, mode); - } else { - RZ_LOG_ERROR("core: Invalid expected hash hexpairs.\n"); - } - } - free(hashValue); - } else { - RZ_LOG_ERROR("core: Cannot allocate memory.\n"); - } - ret = true; - } else { - RZ_LOG_ERROR("core: Usage: /cc [hashname] [hexpairhashvalue]\n"); - RZ_LOG_ERROR("core: Usage: /CC to search ascii collisions\n"); - } - free(s); - goto beach; - } break; - case 'd': // "cd" - { - // Certificate with version number - RzSearchKeyword *kw_1 = rz_search_keyword_new_hex("30820000308100A0030201", "ffff0000fffc00ffffffff", NULL); - RzSearchKeyword *kw_2 = rz_search_keyword_new_hex("3082000030820000A0030201", "ffff0000fffc0000ffffffff", NULL); - // Certificate with serial number - RzSearchKeyword *kw_3 = rz_search_keyword_new_hex("308200003082000002", "ffff0000fffc0000ff", NULL); - rz_search_reset(core->search, RZ_SEARCH_KEYWORD); - if (kw_1 && kw_2 && kw_3) { - rz_search_kw_add(core->search, kw_1); - rz_search_kw_add(core->search, kw_2); - rz_search_kw_add(core->search, kw_3); - rz_search_begin(core->search); - } else { - RZ_LOG_ERROR("core: null pointer on search keyword\n"); - dosearch = false; - } - } break; - case 'a': // "ca" - { - RzSearchKeyword *kw; - kw = rz_search_keyword_new_hexmask("00", NULL); - // AES search is done over 40 bytes - kw->keyword_length = AES_SEARCH_LENGTH; - rz_search_reset(core->search, RZ_SEARCH_AES); - rz_search_kw_add(search, kw); - rz_search_begin(core->search); - param.aes_search = true; - break; - } - case 'r': // "cr" - { - RzSearchKeyword *kw; - kw = rz_search_keyword_new_hexmask("00", NULL); - // Private key search is at least 11 bytes - kw->keyword_length = PRIVATE_KEY_SEARCH_LENGTH; - rz_search_reset(core->search, RZ_SEARCH_PRIV_KEY); - rz_search_kw_add(search, kw); - rz_search_begin(core->search); - param.privkey_search = true; - break; - } - default: { - dosearch = false; - rz_core_cmd_help(core, help_msg_slash_c); - } - } - } break; - case 'm': // "/m" - dosearch = false; - if (input[1] == '?') { // "/m?" - rz_core_cmd_help(core, help_msg_slash_m); - } else if (input[1] == 'b') { // "/mb" - bool bin_verbose = rz_config_get_i(core->config, "bin.verbose"); - rz_config_set_i(core->config, "bin.verbose", false); - // TODO : iter maps? - cmd_search_bin(core, search_itv); - rz_config_set_i(core->config, "bin.verbose", bin_verbose); - } else if (input[1] == ' ' || input[1] == '\0' || param.outmode == RZ_MODE_JSON) { - int ret; - const char *file = input[param_offset - 1] ? input + param_offset : NULL; - ut64 addr = search_itv.addr; - RzListIter *iter; - RzIOMap *map; - if (param.outmode == RZ_MODE_JSON) { - pj_a(param.pj); - } - rz_core_magic_reset(core); - int maxHits = rz_config_get_i(core->config, "search.maxhits"); - int hits = 0; - rz_list_foreach (param.boundaries, iter, map) { - if (param.outmode != RZ_MODE_JSON) { - eprintf("-- %llx %llx\n", map->itv.addr, rz_itv_end(map->itv)); - } - rz_cons_break_push(NULL, NULL); - for (addr = map->itv.addr; addr < rz_itv_end(map->itv); addr++) { - if (rz_cons_is_breaked()) { - break; - } - ret = rz_core_magic_at(core, file, addr, 99, false, param.outmode == RZ_MODE_JSON ? param.pj : NULL, &hits); - if (ret == -1) { - // something went terribly wrong. - break; - } - if (maxHits && hits >= maxHits) { - break; - } - addr += ret - 1; - } - rz_cons_clear_line(1); - rz_cons_break_pop(); - } - if (param.outmode == RZ_MODE_JSON) { - pj_end(param.pj); - } - } else { - RZ_LOG_ERROR("core: Usage: /m [file]\n"); - } - rz_cons_clear_line(1); - break; - case 'p': // "/p" - { - if (input[param_offset - 1]) { - int ps = atoi(input + param_offset); - if (ps > 1) { - RzListIter *iter; - RzIOMap *map; - rz_list_foreach (param.boundaries, iter, map) { - eprintf("-- %llx %llx\n", map->itv.addr, rz_itv_end(map->itv)); - rz_cons_break_push(NULL, NULL); - rz_search_pattern_size(core->search, ps); - rz_search_pattern(core->search, map->itv.addr, rz_itv_end(map->itv)); - rz_cons_break_pop(); - } - break; - } - } - RZ_LOG_ERROR("core: Invalid pattern size (must be > 0)\n"); - } break; - case 'P': // "/P" - search_similar_pattern(core, atoi(input + 1), ¶m); - break; - case 'V': // "/V" - { - if (input[2] == 'j') { - param.outmode = RZ_MODE_JSON; - param_offset++; - } else if (strchr(input + 1, '*')) { - param.outmode = RZ_MODE_RIZINCMD; - } - int err = 1, vsize = atoi(input + 1); - const char *num_str = input + param_offset + 1; - if (vsize && input[2] && num_str) { - if (param.outmode == RZ_MODE_JSON) { - pj_a(param.pj); - } - char *w = strchr(num_str, ' '); - if (w) { - *w++ = 0; - ut64 vmin = rz_num_math(core->num, num_str); - ut64 vmax = rz_num_math(core->num, w); - if (vsize > 0) { - RzIOMap *map; - RzListIter *iter; - rz_list_foreach (param.boundaries, iter, map) { - err = 0; - int hits = rz_core_search_value_in_range(core, map->itv, - vmin, vmax, vsize, - _CbInRangeSearchV, ¶m); - if (param.outmode != RZ_MODE_JSON) { - eprintf("hits: %d\n", hits); - } - } - } - } - if (param.outmode == RZ_MODE_JSON) { - pj_end(param.pj); - } - } - if (err) { - RZ_LOG_ERROR("core: Usage: /V[1|2|4|8] [minval] [maxval]\n"); - } - } - dosearch = false; - break; - case 'v': // "/v" - if (input[1]) { - if (input[1] == '?') { - rz_cons_print("Usage: /v[1|2|4|8] [value]\n"); - break; - } - if (input[2] == 'j') { - param.outmode = RZ_MODE_JSON; - param_offset++; - } - } - rz_search_reset(core->search, RZ_SEARCH_KEYWORD); - rz_search_set_distance(core->search, (int)rz_config_get_i(core->config, "search.distance")); - char *v_str = (char *)rz_str_trim_head_ro(input + param_offset); - RzList *nums = rz_num_str_split_list(v_str); - int len = rz_list_length(nums); - int bsize = 0; - ut8 *v_buf = NULL; - switch (input[1]) { - case '8': - if (input[param_offset]) { - bsize = sizeof(ut64) * len; - v_buf = v_writebuf(core, nums, len, '8', bsize); - } else { - RZ_LOG_ERROR("core: Usage: /v8 value\n"); - } - break; - case '1': - if (input[param_offset]) { - bsize = sizeof(ut8) * len; - v_buf = v_writebuf(core, nums, len, '1', bsize); - } else { - RZ_LOG_ERROR("core: Usage: /v1 value\n"); - } - break; - case '2': - if (input[param_offset]) { - bsize = sizeof(ut16) * len; - v_buf = v_writebuf(core, nums, len, '2', bsize); - } else { - RZ_LOG_ERROR("core: Usage: /v2 value\n"); - } - break; - default: // default size - case '4': - if (input[param_offset - 1]) { - if (input[param_offset]) { - bsize = sizeof(ut32) * len; - v_buf = v_writebuf(core, nums, len, '4', bsize); - } - } else { - RZ_LOG_ERROR("core: Usage: /v4 value\n"); - } - break; - } - if (v_buf) { - rz_search_kw_add(core->search, - rz_search_keyword_new((const ut8 *)v_buf, bsize, NULL, 0, NULL)); - free(v_buf); - } - rz_search_begin(core->search); - dosearch = true; - break; - case 'w': // "/w" search wide string, includes ignorecase search functionality (/wi cmd)! - if (input[2]) { - if (input[1] == 'j' || input[2] == 'j') { - param.outmode = RZ_MODE_JSON; - } - if (input[1] == 'i' || input[2] == 'i') { - ignorecase = true; - } - } else { - param.outmode = RZ_MODE_RIZINCMD; - } - - size_t shift = 1 + ignorecase; - if (param.outmode == RZ_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; - } - rz_search_reset(core->search, RZ_SEARCH_KEYWORD); - rz_search_set_distance(core->search, (int)rz_config_get_i(core->config, "search.distance")); - RzSearchKeyword *skw; - skw = rz_search_keyword_new((const ut8 *)inp, len * 2, NULL, 0, NULL); - free(inp); - if (skw) { - skw->icase = ignorecase; - rz_search_kw_add(core->search, skw); - rz_search_begin(core->search); - dosearch = true; - } else { - RZ_LOG_ERROR("core: Invalid keyword\n"); - break; - } - break; - case 'i': // "/i" - if (input[param_offset - 1] != ' ') { - RZ_LOG_ERROR("core: Missing ' ' after /i\n"); - ret = false; - goto beach; - } - ignorecase = true; - // fallthrough - case 'j': // "/j" - if (input[0] == 'j' && input[1] == ' ') { - param.outmode = RZ_MODE_JSON; - } - // fallthrough - case ' ': // "/ " search string - inp = rz_str_dup(input + 1 + ignorecase + (param.outmode == RZ_MODE_JSON ? 1 : 0)); - len = rz_str_unescape(inp); - rz_search_reset(core->search, RZ_SEARCH_KEYWORD); - rz_search_set_distance(core->search, (int)rz_config_get_i(core->config, "search.distance")); - { - RzSearchKeyword *skw; - skw = rz_search_keyword_new((const ut8 *)inp, len, NULL, 0, NULL); - free(inp); - if (skw) { - skw->icase = ignorecase; - skw->type = RZ_SEARCH_KEYWORD_TYPE_STRING; - rz_search_kw_add(core->search, skw); - } else { - RZ_LOG_ERROR("core: Invalid keyword\n"); - break; - } - } - rz_search_begin(core->search); - dosearch = true; - break; - case 'e': // "/e" match regexp - if (input[1] == '?') { - RZ_LOG_ERROR("core: Usage: /e /foo/i or /e/foo/i\n"); - } else if (input[1]) { - RzSearchKeyword *kw; - kw = rz_search_keyword_new_regexp(input + 1, NULL); - if (!kw) { - RZ_LOG_ERROR("core: Invalid regexp specified\n"); - break; - } - rz_search_reset(core->search, RZ_SEARCH_REGEXP); - // TODO distance is unused - rz_search_set_distance(core->search, (int)rz_config_get_i(core->config, "search.distance")); - rz_search_kw_add(core->search, kw); - rz_search_begin(core->search); - dosearch = true; - } else { - RZ_LOG_ERROR("core: Missing regex\n"); - } - break; - case 'E': // "/E" - if (core->bin && core->bin->is_debugger) { - rz_debug_map_sync(core->dbg); - } - do_esil_search(core, ¶m, input); - goto beach; - case 'd': // "/d" search delta key - if (input[1]) { - rz_search_reset(core->search, RZ_SEARCH_DELTAKEY); - rz_search_kw_add(core->search, - rz_search_keyword_new_hexmask(input + param_offset, NULL)); - rz_search_begin(core->search); - dosearch = true; - } else { - RZ_LOG_ERROR("core: Missing delta\n"); - } - break; - case 'h': // "/h" - { - char *p, *arg = rz_str_trim_dup(input + 1); - p = strchr(arg, ' '); - if (p) { - *p++ = 0; - if (*arg == '?') { - RZ_LOG_ERROR("core: Usage: /h md5 [hash] [datalen]\n"); - } else { - ut32 min = UT32_MAX; - ut32 max = UT32_MAX; - char *pmax, *pmin = strchr(p, ' '); - if (pmin) { - *pmin++ = 0; - pmax = strchr(pmin, ' '); - if (pmax) { - *pmax++ = 0; - max = rz_num_math(core->num, pmax); - } - min = rz_num_math(core->num, pmin); - } - search_hash(core, arg, p, min, max, ¶m); - } - } else { - RZ_LOG_ERROR("core: Missing hash. See ph?\n"); - } - free(arg); - } break; - case 'f': // "/f" forward search - if (core->offset) { - RzInterval itv = { core->offset, -core->offset }; - if (!rz_itv_overlap(search_itv, itv)) { - ret = false; - goto beach; - } else { - search_itv = rz_itv_intersect(search_itv, itv); - } - } - break; - case 'g': // "/g" graph search - if (input[1] == '?') { - rz_cons_printf("Usage: /g[g] [fromaddr] @ [toaddr]\n"); - rz_cons_printf("(find all graph paths A to B (/gg follow jumps, see search.count and analysis.depth)"); - } else { - ut64 addr = UT64_MAX; - if (input[1]) { - addr = rz_num_math(core->num, input + 2); - } else { - RzAnalysisFunction *fcn = rz_analysis_get_function_at(core->analysis, addr); - if (fcn) { - addr = fcn->addr; - } else { - addr = core->offset; - } - } - const int depth = rz_config_get_i(core->config, "analysis.depth"); - // Va;ifate input length - if (input[1] != '\0') { - rz_core_analysis_paths(core, addr, core->offset, input[1] == 'g', depth, (input[1] == 'j' || input[2] == 'j')); - } - } - break; - case 'F': // "/F" search file /F [file] ([offset] ([sz])) - if (input[param_offset - 1] == ' ') { - int n_args; - char **args = rz_str_argv(input + param_offset, &n_args); - ut8 *buf = NULL; - ut64 offset = 0; - size_t size; - buf = (ut8 *)rz_file_slurp(args[0], &size); - if (!buf) { - RZ_LOG_ERROR("core: Cannot open '%s'\n", args[0]); - rz_str_argv_free(args); - break; - } - if (n_args > 1) { - offset = rz_num_math(core->num, args[1]); - if (size <= offset) { - RZ_LOG_ERROR("core: size <= offset\n"); - rz_str_argv_free(args); - free(buf); - break; - } - } - if (n_args > 2) { - len = rz_num_math(core->num, args[2]); - if (len > size - offset) { - RZ_LOG_ERROR("core: len too large\n"); - rz_str_argv_free(args); - free(buf); - break; - } - } else { - len = size - offset; - } - RzSearchKeyword *kw; - rz_search_reset(core->search, RZ_SEARCH_KEYWORD); - rz_search_set_distance(core->search, (int)rz_config_get_i(core->config, "search.distance")); - kw = rz_search_keyword_new(buf + offset, len, NULL, 0, NULL); - if (kw) { - rz_search_kw_add(core->search, kw); - // eprintf ("Searching %d byte(s)...\n", kw->keyword_length); - rz_search_begin(core->search); - dosearch = true; - } else { - RZ_LOG_ERROR("core: no keyword\n"); - } - - rz_str_argv_free(args); - free(buf); - } else { - RZ_LOG_ERROR("core: Usage: /F[j] [file] ([offset] ([sz]))\n"); - } - break; - case 'x': // "/x" search hex - if (!input[1]) { - RZ_LOG_ERROR("core: missing hexpairs:binmask\n"); - break; - } - if (input[1] == '?') { - rz_core_cmd_help(core, help_msg_slash_x); - } else { - RzSearchKeyword *kw; - char *s, *p = rz_str_dup(input + param_offset); - rz_search_reset(core->search, RZ_SEARCH_KEYWORD); - rz_search_set_distance(core->search, (int)rz_config_get_i(core->config, "search.distance")); - s = strchr(p, ':'); - if (s) { - *s++ = 0; - kw = rz_search_keyword_new_hex(p, s, NULL); - } else { - kw = rz_search_keyword_new_hexmask(p, NULL); - } - if (kw) { - rz_search_kw_add(core->search, kw); - // eprintf ("Searching %d byte(s)...\n", kw->keyword_length); - rz_search_begin(core->search); - dosearch = true; - } else { - RZ_LOG_ERROR("core: no keyword\n"); - } - free(p); - } - break; - case 's': // "/s" - do_section_search(core, ¶m, input + 1); - break; - case '+': // "/+" - if (input[1] == ' ') { - // TODO: support /+j - char *buf = malloc(strlen(input) * 2); - char *str = rz_str_dup(input + 2); - int ochunksize; - int i, len, chunksize = rz_config_get_i(core->config, "search.chunk"); - if (chunksize < 1) { - chunksize = core->rasm->bits / 8; - } - len = rz_str_unescape(str); - ochunksize = chunksize = RZ_MIN(len, chunksize); - RZ_LOG_ERROR("core: Using chunksize: %d\n", chunksize); - core->in_search = false; - for (i = 0; i < len; i += chunksize) { - chunksize = ochunksize; - again: - rz_hex_bin2str((ut8 *)str + i, RZ_MIN(chunksize, len - i), buf); - RZ_LOG_ERROR("core: /x %s\n", buf); - rz_core_cmdf(core, "/x %s", buf); - if (core->num->value == 0) { - chunksize--; - if (chunksize < 1) { - RZ_LOG_ERROR("core: Oops\n"); - free(buf); - free(str); - goto beach; - } - RZ_LOG_ERROR("core: Repeat with chunk size %d\n", chunksize); - goto again; - } - } - free(str); - free(buf); - } else { - RZ_LOG_ERROR("core: Usage: /+ [string]\n"); - } - break; - case 'z': // "/z" search strings of min-max range - { - char *p; - ut32 min, max; - if (!input[1]) { - RZ_LOG_ERROR("core: Usage: /z min max\n"); - break; - } - if ((p = strchr(input + 2, ' '))) { - *p = 0; - max = rz_num_math(core->num, p + 1); - } else { - RZ_LOG_ERROR("core: Usage: /z min max\n"); - break; - } - min = rz_num_math(core->num, input + 2); - if (!rz_search_set_string_limits(core->search, min, max)) { - RZ_LOG_ERROR("core: min must be lower than max\n"); - break; - } - rz_search_reset(core->search, RZ_SEARCH_STRING); - rz_search_set_distance(core->search, (int)rz_config_get_i(core->config, "search.distance")); - { - RzSearchKeyword *kw = rz_search_keyword_new_hexmask("00", NULL); - kw->type = RZ_SEARCH_KEYWORD_TYPE_STRING; - rz_search_kw_add(search, kw); - } - rz_search_begin(search); - dosearch = true; - } break; - case '?': // "/?" - rz_core_cmd_help(core, help_msg_slash); - break; - default: - RZ_LOG_ERROR("core: See /? for help.\n"); - break; - } - rz_config_set_i(core->config, "search.kwidx", search->n_kws); - if (dosearch) { - do_string_search(core, search_itv, ¶m); - } -beach: - core->num->value = search->nhits; - core->in_search = false; - rz_flag_space_pop(core->flags); - if (param.outmode == RZ_MODE_JSON) { - rz_cons_println(pj_string(param.pj)); - } - pj_free(param.pj); - rz_list_free(param.boundaries); - rz_search_kw_reset(search); - return ret; -} +RZ_IPI int rz_cmd_search(void *data, const char *input) { return RZ_CMD_STATUS_ERROR; }