From 2bd3d75f8770bbe72f0c928040e1e51bfe645e54 Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 19 Jun 2024 18:57:59 +0200 Subject: [PATCH] Implement the new fD command to filter/mangle flagnames ##shell --- libr/core/cmd_flag.inc.c | 75 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/libr/core/cmd_flag.inc.c b/libr/core/cmd_flag.inc.c index f811edc468406..75ba633b260dd 100644 --- a/libr/core/cmd_flag.inc.c +++ b/libr/core/cmd_flag.inc.c @@ -42,12 +42,14 @@ static RCoreHelpMessage help_msg_f = { "fc", "[?][name] [color]", "set color for given flag", "fC", " [name] [cmt]", "set comment for given flag", "fd", "[?] addr", "return flag+delta", + "fD", "[?] rawname", "(de)mangle flag or set a new flag", "fe", " [name]", "create flag name.#num# enumerated flag. (f.ex: fe foo @@= 1 2 3 4)", "fe-", "", "resets the enumerator counter", "ff", " ([glob])", "distance in bytes to reach the next flag (see sn/sp)", "fi", " [size] | [from] [to]", "show flags in current block or range", "fg", "[*] ([prefix])", "construct a graph with the flag names", "fj", "", "list flags in JSON format", + "fq", "", "list flags in quiet mode", "fl", " (@[flag]) [size]", "show or set flag length (size)", "fla", " [glob]", "automatically compute the size of all flags matching glob", "fm", " addr", "move flag at current offset to new address", @@ -64,7 +66,6 @@ static RCoreHelpMessage help_msg_f = { "ft", "[?]*", "flag tags, useful to find all flags matching some words", "fV", "[*-] [nkey] [offset]", "dump/restore visual marks (mK/'K)", "fx", "[d]", "show hexdump (or disasm) of flag:flagsize", - "fq", "", "list flags in quiet mode", "fz", "[?][name]", "add named flag zone -name to delete. see fz?[name]", NULL }; @@ -101,13 +102,22 @@ static RCoreHelpMessage help_msg_ft = { NULL }; +static RCoreHelpMessage help_msg_fD = { + "Usage: fD[*.j]", " [rawname]", " # filter/mangle raw symbol name to be valid flag name", + "fD", " rawname" , "print the mangled flag name using the raw name, see the ' command prefix", + "fD.", " rawname", "set a flag using the orig raw name in the current offset", + "fDj", " rawname", "same as fD but output is in json", + "fD*", " rawname", "filter raw name to be a valid flag and output in r2 commands", + NULL +}; + static RCoreHelpMessage help_msg_fd = { "Usage: fd[d]", " [offset|flag|expression]", " # Describe flags", - "fd", " $$" , "# describe flag + delta for given offset", - "fd.", " $$", "# check flags in current address (no delta)", - "fdj", " $$", "# describe current flag in json", - "fdd", " $$", "# describe flag without space restrictions", - "fdw", " [string]", "# filter closest flag by string for current offset", + "fd", " $$" , "describe flag + delta for given offset", + "fd.", " $$", "check flags in current address (no delta)", + "fdj", " $$", "describe current flag in json", + "fdd", " $$", "describe flag without space restrictions", + "fdw", " [string]", "filter closest flag by string for current offset", NULL }; @@ -1588,6 +1598,59 @@ static int cmd_flag(void *data, const char *input) { free (arg); } break; + case 'D': // "fD" + switch (input[1]) { + case ' ': + { + char *orig = r_str_trim_dup (input + 2); + char *nfn = r_name_filter_dup (orig); + r_cons_printf ("%s\n", nfn); + free (nfn); + free (orig); + } + break; + case '*': + if (input[2] == ' ') { + char *orig = r_str_trim_dup (input + 3); + char *nfn = r_name_filter_dup (orig); + r_cons_printf ("f %s\n", nfn); + free (nfn); + free (orig); + } else { + r_core_cmd_help (core, help_msg_fD); + } + break; + case '.': + if (input[2] == ' ') { + char *orig = r_str_trim_dup (input + 3); + char *nfn = r_name_filter_dup (orig); + r_flag_set (core->flags, nfn, core->offset, 1); + free (nfn); + free (orig); + } else { + r_core_cmd_help (core, help_msg_fD); + } + break; + case 'j': + if (input[2] == ' ') { + char *orig = r_str_trim_dup (input + 2); + char *nfn = r_name_filter_dup (orig); + PJ *pj = r_core_pj_new (core); + pj_o (pj); + pj_ks (pj, "orig", orig); + pj_ks (pj, "filtered", nfn); + pj_end (pj); + free (nfn); + free (orig); + } else { + r_core_cmd_help (core, help_msg_fD); + } + break; + default: + r_core_cmd_help (core, help_msg_fD); + break; + } + break; case 'd': // "fd" { ut64 addr = core->offset;