From e18aef1d34d772f345ce34c91f5989c2c405c8c0 Mon Sep 17 00:00:00 2001 From: Peiwei Hu Date: Tue, 19 Jul 2022 16:13:51 +0800 Subject: [PATCH 1/3] Port command pa to new shell --- librz/core/cmd/cmd_print.c | 153 ++++++++-------------------- librz/core/cmd_descs/cmd_descs.c | 78 ++++++++++++++ librz/core/cmd_descs/cmd_descs.h | 4 + librz/core/cmd_descs/cmd_print.yaml | 39 +++++++ librz/core/cprint.c | 135 +++++++++++++++++++++++- librz/include/rz_core.h | 6 +- test/db/cmd/cmd_pae | 19 ++++ 7 files changed, 319 insertions(+), 115 deletions(-) diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index 8f663024544..07470900f48 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -13,16 +13,6 @@ #define PF_USAGE_STR "pf[.k[.f[=v]]|[v]]|[n]|[0|cnt][fmt] [a0 a1 ...]" -static const char *help_msg_pa[] = { - "Usage: pa[edD]", "[asm|hex]", "print (dis)assembled", - "pa", " [assembly]", "print hexpairs of the given assembly expression", - "paD", " [hexpairs]", "print assembly expression from hexpairs and show hexpairs", - "pad", " [hexpairs]", "print assembly expression from hexpairs (alias for pix)", - "pade", " [hexpairs]", "print ESIL expression from hexpairs", - "pae", " [assembly]", "print ESIL expression of the given assembly expression", - NULL -}; - static const char *help_msg_pp[] = { "Usage: pp[d]", "", "print patterns", "pp0", "", "print buffer filled with zeros", @@ -4694,6 +4684,46 @@ RZ_IPI RzCmdStatus rz_print_string_c_cpp_handler(RzCore *core, int argc, const c return RZ_CMD_STATUS_OK; } +RZ_IPI RzCmdStatus rz_hex_of_assembly_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) { + char *buf = rz_core_hex_of_assembly(core, argv[1]); + if (!buf) { + return RZ_CMD_STATUS_ERROR; + } + rz_cons_println(buf); + free(buf); + return RZ_CMD_STATUS_OK; +} + +RZ_IPI RzCmdStatus rz_esil_of_assembly_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) { + char *buf = rz_core_esil_of_assembly(core, argv[1]); + if (!buf) { + return RZ_CMD_STATUS_ERROR; + } + rz_cons_println(buf); + free(buf); + return RZ_CMD_STATUS_OK; +} + +RZ_IPI RzCmdStatus rz_assembly_of_hex_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) { + char *buf = rz_core_assembly_of_hex(core, argv[1]); + if (!buf) { + return RZ_CMD_STATUS_ERROR; + } + rz_cons_print(buf); + free(buf); + return RZ_CMD_STATUS_OK; +} + +RZ_IPI RzCmdStatus rz_esil_of_hex_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) { + char *buf = rz_core_esil_of_hex(core, argv[1]); + if (!buf) { + return RZ_CMD_STATUS_ERROR; + } + rz_cons_print(buf); + free(buf); + return RZ_CMD_STATUS_OK; +} + RZ_IPI int rz_cmd_print(void *data, const char *input) { RzCore *core = (RzCore *)data; st64 l; @@ -4848,109 +4878,6 @@ RZ_IPI int rz_cmd_print(void *data, const char *input) { rz_config_set_i(core->config, "search.to", saved_to); } } break; - case 'a': // "pa" - { - const char *arg = NULL; - if (input[1] != '\0') { - arg = rz_str_trim_head_ro(input + 2); - } - if (input[1] == 'e') { // "pae" - if (input[2] == '?') { - rz_cons_printf("|Usage: pae [asm] print ESIL expression of the given assembly expression\n"); - } else { - int printed = 0; - int bufsz; - RzAnalysisOp aop = { 0 }; - rz_asm_set_pc(core->rasm, core->offset); - RzAsmCode *acode = rz_asm_massemble(core->rasm, input + 2); - if (acode) { - bufsz = acode->len; - while (printed < bufsz) { - aop.size = 0; - if (rz_analysis_op(core->analysis, &aop, core->offset, - (const ut8 *)acode->bytes + printed, bufsz - printed, RZ_ANALYSIS_OP_MASK_ESIL) > 0) { - const char *str = RZ_STRBUF_SAFEGET(&aop.esil); - rz_cons_println(str); - } else { - eprintf("Cannot decode instruction\n"); - break; - } - if (aop.size < 1) { - eprintf("Cannot decode instruction\n"); - break; - } - printed += aop.size; - rz_analysis_op_fini(&aop); - } - } - } - } else if (input[1] == 'D') { // "paD" - if (input[2] == '?') { - rz_cons_printf("|Usage: paD [hex] print assembly expression from hexpairs and show hexpairs\n"); - } else { - rz_core_cmdf(core, "pdq @x:%s", input + 2); - } - } else if (input[1] == 'd') { // "pad*" - switch (input[2]) { - case 'e': // "pade" - if (input[3] == '?') { - rz_cons_printf("|Usage: pade [hex] print ESIL expression from hexpairs\n"); - } else { - int printed = 0; - int bufsz; - RzAnalysisOp aop = { 0 }; - char *hex_arg = calloc(1, strlen(arg) + 1); - if (hex_arg) { - bufsz = rz_hex_str2bin(arg + 1, (ut8 *)hex_arg); - while (printed < bufsz) { - aop.size = 0; - if (rz_analysis_op(core->analysis, &aop, core->offset, - (const ut8 *)hex_arg + printed, bufsz - printed, RZ_ANALYSIS_OP_MASK_ESIL) > 0) { - const char *str = RZ_STRBUF_SAFEGET(&aop.esil); - rz_cons_println(str); - } else { - eprintf("Cannot decode instruction\n"); - break; - } - if (aop.size < 1) { - eprintf("Cannot decode instruction\n"); - break; - } - printed += aop.size; - rz_analysis_op_fini(&aop); - } - free(hex_arg); - } - } - break; - case ' ': // "pad" - __cmd_pad(core, arg); - break; - case '?': // "pad?" - rz_cons_printf("|Usage: pad [hex] print assembly expression from hexpairs\n"); - break; - default: - rz_cons_printf("|Usage: pa[edD] [asm|hex] print (dis)assembled\n"); - break; - } - } else if (input[1] == '?') { - rz_core_cmd_help(core, help_msg_pa); - } else { - int i; - int bytes; - rz_asm_set_pc(core->rasm, core->offset); - RzAsmCode *acode = rz_asm_massemble(core->rasm, input + 1); - if (acode) { - bytes = acode->len; - for (i = 0; i < bytes; i++) { - ut8 b = acode->bytes[i]; // core->print->big_endian? (bytes - 1 - i): i ]; - rz_cons_printf("%02x", b); - } - rz_cons_newline(); - rz_asm_code_free(acode); - } - } - } break; case 'b': { // "pb" if (input[1] == '?') { rz_cons_printf("|Usage: p[bB] [len] ([skip]) ; see also pB and pxb\n"); diff --git a/librz/core/cmd_descs/cmd_descs.c b/librz/core/cmd_descs/cmd_descs.c index 6b4c6d1cc2e..8386d1a377f 100644 --- a/librz/core/cmd_descs/cmd_descs.c +++ b/librz/core/cmd_descs/cmd_descs.c @@ -451,6 +451,10 @@ static const RzCmdDescArg open_maps_prioritize_binid_args[2]; static const RzCmdDescArg open_maps_deprioritize_args[2]; static const RzCmdDescArg open_maps_prioritize_fd_args[2]; static const RzCmdDescArg open_exchange_args[3]; +static const RzCmdDescArg hex_of_assembly_args[2]; +static const RzCmdDescArg esil_of_assembly_args[2]; +static const RzCmdDescArg assembly_of_hex_args[2]; +static const RzCmdDescArg esil_of_hex_args[2]; static const RzCmdDescArg cmd_disassembly_n_bytes_args[2]; static const RzCmdDescArg cmd_disassembly_n_instructions_args[2]; static const RzCmdDescArg cmd_disassembly_all_possible_opcodes_args[2]; @@ -10417,6 +10421,69 @@ static const RzCmdDescHelp open_exchange_help = { static const RzCmdDescHelp cmd_print_help = { .summary = "Print commands", }; +static const RzCmdDescHelp pa_help = { + .summary = "Print (dis)assembly of given hexpairs/assembly", +}; +static const RzCmdDescArg hex_of_assembly_args[] = { + { + .name = "assembly", + .type = RZ_CMD_ARG_TYPE_STRING, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = false, + + }, + { 0 }, +}; +static const RzCmdDescHelp hex_of_assembly_help = { + .summary = "Print hexpairs of the given assembly expression", + .args = hex_of_assembly_args, +}; + +static const RzCmdDescArg esil_of_assembly_args[] = { + { + .name = "assembly", + .type = RZ_CMD_ARG_TYPE_STRING, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = false, + + }, + { 0 }, +}; +static const RzCmdDescHelp esil_of_assembly_help = { + .summary = "Print ESIL expression of the given assembly expression", + .args = esil_of_assembly_args, +}; + +static const RzCmdDescArg assembly_of_hex_args[] = { + { + .name = "hexpair", + .type = RZ_CMD_ARG_TYPE_RZNUM, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = false, + + }, + { 0 }, +}; +static const RzCmdDescHelp assembly_of_hex_help = { + .summary = "Print assembly expression from hexpairs (alias for pix)", + .args = assembly_of_hex_args, +}; + +static const RzCmdDescArg esil_of_hex_args[] = { + { + .name = "hexpair", + .type = RZ_CMD_ARG_TYPE_RZNUM, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = false, + + }, + { 0 }, +}; +static const RzCmdDescHelp esil_of_hex_help = { + .summary = "Print ESIL expression from hexpairs", + .args = esil_of_hex_args, +}; + static const RzCmdDescDetailEntry cmd_print_byte_array_Useful_space_modifiers_detail_entries[] = { { .text = "pch @e:cfg.bigendian=", .arg_str = NULL, .comment = "Change endianness for pch, pcw and pcd commands" }, { .text = "pc @! ", .arg_str = NULL, .comment = "Change the N of bytes (i.e. block size)." }, @@ -16175,6 +16242,17 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) { RzCmdDesc *cmd_print_cd = rz_cmd_desc_oldinput_new(core->rcmd, root_cd, "p", rz_cmd_print, &cmd_print_help); rz_warn_if_fail(cmd_print_cd); + RzCmdDesc *pa_cd = rz_cmd_desc_group_modes_new(core->rcmd, cmd_print_cd, "pa", RZ_OUTPUT_MODE_STANDARD, rz_hex_of_assembly_handler, &hex_of_assembly_help, &pa_help); + rz_warn_if_fail(pa_cd); + RzCmdDesc *esil_of_assembly_cd = rz_cmd_desc_argv_modes_new(core->rcmd, pa_cd, "pae", RZ_OUTPUT_MODE_STANDARD, rz_esil_of_assembly_handler, &esil_of_assembly_help); + rz_warn_if_fail(esil_of_assembly_cd); + + RzCmdDesc *assembly_of_hex_cd = rz_cmd_desc_argv_modes_new(core->rcmd, pa_cd, "pad", RZ_OUTPUT_MODE_STANDARD, rz_assembly_of_hex_handler, &assembly_of_hex_help); + rz_warn_if_fail(assembly_of_hex_cd); + + RzCmdDesc *esil_of_hex_cd = rz_cmd_desc_argv_modes_new(core->rcmd, pa_cd, "pade", RZ_OUTPUT_MODE_STANDARD, rz_esil_of_hex_handler, &esil_of_hex_help); + rz_warn_if_fail(esil_of_hex_cd); + RzCmdDesc *cmd_print_byte_array_cd = rz_cmd_desc_group_new(core->rcmd, cmd_print_cd, "pc", rz_cmd_print_byte_array_c_cpp_bytes_handler, &cmd_print_byte_array_c_cpp_bytes_help, &cmd_print_byte_array_help); rz_warn_if_fail(cmd_print_byte_array_cd); RzCmdDesc *cmd_print_byte_array_c_cpp_half_word_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pch", rz_cmd_print_byte_array_c_cpp_half_word_handler, &cmd_print_byte_array_c_cpp_half_word_help); diff --git a/librz/core/cmd_descs/cmd_descs.h b/librz/core/cmd_descs/cmd_descs.h index 47c47732049..39757aaac0c 100644 --- a/librz/core/cmd_descs/cmd_descs.h +++ b/librz/core/cmd_descs/cmd_descs.h @@ -757,6 +757,10 @@ RZ_IPI RzCmdStatus rz_open_maps_prioritize_binid_handler(RzCore *core, int argc, RZ_IPI RzCmdStatus rz_open_maps_deprioritize_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_open_maps_prioritize_fd_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_open_exchange_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_hex_of_assembly_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode); +RZ_IPI RzCmdStatus rz_esil_of_assembly_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode); +RZ_IPI RzCmdStatus rz_assembly_of_hex_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode); +RZ_IPI RzCmdStatus rz_esil_of_hex_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode); RZ_IPI RzCmdStatus rz_cmd_print_byte_array_c_cpp_bytes_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_byte_array_c_cpp_half_word_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_byte_array_c_cpp_word_handler(RzCore *core, int argc, const char **argv); diff --git a/librz/core/cmd_descs/cmd_print.yaml b/librz/core/cmd_descs/cmd_print.yaml index af9baad07ef..0d35aeb9de0 100644 --- a/librz/core/cmd_descs/cmd_print.yaml +++ b/librz/core/cmd_descs/cmd_print.yaml @@ -3,6 +3,45 @@ --- name: cmd_print commands: + - name: pa + summary: Print (dis)assembly of given hexpairs/assembly + subcommands: + - name: pa + summary: Print hexpairs of the given assembly expression + cname: hex_of_assembly + modes: + - RZ_OUTPUT_MODE_STANDARD + args: + - name: assembly + type: RZ_CMD_ARG_TYPE_STRING + optional: false + - name: pae + summary: Print ESIL expression of the given assembly expression + cname: esil_of_assembly + modes: + - RZ_OUTPUT_MODE_STANDARD + args: + - name: assembly + type: RZ_CMD_ARG_TYPE_STRING + optional: false + - name: pad + summary: Print assembly expression from hexpairs (alias for pix) + cname: assembly_of_hex + modes: + - RZ_OUTPUT_MODE_STANDARD + args: + - name: hexpair + type: RZ_CMD_ARG_TYPE_RZNUM + optional: false + - name: pade + summary: Print ESIL expression from hexpairs + cname: esil_of_hex + modes: + - RZ_OUTPUT_MODE_STANDARD + args: + - name: hexpair + type: RZ_CMD_ARG_TYPE_RZNUM + optional: false - name: pc summary: Print bytes as code byte arrays. cname: cmd_print_byte_array diff --git a/librz/core/cprint.c b/librz/core/cprint.c index 3070dc0e041..387da59e63c 100644 --- a/librz/core/cprint.c +++ b/librz/core/cprint.c @@ -10,7 +10,7 @@ * \param core RzCore * \return a string defination or NULL if the error happens */ -RZ_API char *rz_core_print_string_c_cpp(RzCore *core) { +RZ_API RZ_OWN char *rz_core_print_string_c_cpp(RzCore *core) { ut64 value; size_t size = core->blocksize; RzStrBuf *sb = rz_strbuf_new(NULL); @@ -30,4 +30,137 @@ RZ_API char *rz_core_print_string_c_cpp(RzCore *core) { } rz_strbuf_append(sb, "\";"); return rz_strbuf_drain(sb); +} + +/** + * \brief Get the hexpair of the assembly + * \param core RzCore + * \param assembly assembly + * \return a string containing the hexpair of the assembly + */ +RZ_API RZ_OWN char *rz_core_hex_of_assembly(RzCore *core, const char *assembly) { + RzStrBuf *buf = rz_strbuf_new(""); + if (!buf) { + RZ_LOG_ERROR("Fail to allocate memory\n"); + return NULL; + } + rz_asm_set_pc(core->rasm, core->offset); + RzAsmCode *acode = rz_asm_massemble(core->rasm, assembly); + if (!acode) { + RZ_LOG_ERROR("Fail to assemble by rz_asm_massemble()\n"); + rz_strbuf_free(buf); + return NULL; + } + for (int i = 0; i < acode->len; i++) { + ut8 b = acode->bytes[i]; // core->print->big_endian? (bytes - 1 - i): i ]; + rz_strbuf_appendf(buf, "%02x", b); + } + rz_asm_code_free(acode); + return rz_strbuf_drain(buf); +} + +/** + * \brief Get the esil of the assembly + * \param core RzCore + * \param assembly assembly + * \return a string containing the esil of the assembly + */ +RZ_API RZ_OWN char *rz_core_esil_of_assembly(RzCore *core, const char *assembly) { + RzStrBuf *buf = rz_strbuf_new(""); + if (!buf) { + RZ_LOG_ERROR("Fail to allocate memory\n"); + return NULL; + } + rz_asm_set_pc(core->rasm, core->offset); + RzAsmCode *acode = rz_asm_massemble(core->rasm, assembly); + if (!acode) { + RZ_LOG_ERROR("Fail to assemble by rz_asm_massemble()\n"); + rz_strbuf_free(buf); + return NULL; + } + int printed = 0, bufsz = acode->len; + RzAnalysisOp aop = { 0 }; + while (printed < bufsz) { + aop.size = 0; + if (rz_analysis_op(core->analysis, &aop, core->offset, + (const ut8 *)acode->bytes + printed, bufsz - printed, RZ_ANALYSIS_OP_MASK_ESIL) <= 0 || + aop.size < 1) { + RZ_LOG_ERROR("Cannot decode instruction\n"); + rz_analysis_op_fini(&aop); + rz_strbuf_free(buf); + rz_asm_code_free(acode); + return NULL; + } + rz_strbuf_appendf(buf, "%s\n", RZ_STRBUF_SAFEGET(&aop.esil)); + printed += aop.size; + rz_analysis_op_fini(&aop); + } + rz_asm_code_free(acode); + return rz_strbuf_drain(buf); +} + +/** + * \brief Get the assembly of the hexstr + * \param core RzCore + * \param hexstr hexstr + * \return a string containing the assembly of the hexstr + */ +RZ_API RZ_OWN char *rz_core_assembly_of_hex(RzCore *core, const char *hexstr) { + RzStrBuf *buf = rz_strbuf_new(""); + if (!buf) { + RZ_LOG_ERROR("Fail to allocate memory\n"); + return NULL; + } + rz_asm_set_pc(core->rasm, core->offset); + bool is_pseudo = rz_config_get_i(core->config, "asm.pseudo"); + RzAsmCode *acode = rz_asm_mdisassemble_hexstr(core->rasm, is_pseudo ? core->parser : NULL, hexstr); + if (!acode) { + RZ_LOG_ERROR("Invalid hexstr\n"); + rz_strbuf_free(buf); + return NULL; + } + rz_strbuf_append(buf, acode->assembly); + rz_asm_code_free(acode); + return rz_strbuf_drain(buf); +} + +/** + * \brief Get the esil of the hexstr + * \param core RzCore + * \param hexstr hexstr + * \return a string containing the esil of the hexstr + */ +RZ_API RZ_OWN char *rz_core_esil_of_hex(RzCore *core, const char *hexstr) { + RzStrBuf *buf = rz_strbuf_new(""); + char *hex_arg = calloc(1, strlen(hexstr) + 1); + if (!buf || !hex_arg) { + RZ_LOG_ERROR("Fail to allocate memory\n"); + goto fail; + } + int bufsz = rz_hex_str2bin(hexstr, (ut8 *)hex_arg); + if (bufsz < 1) { + RZ_LOG_ERROR("Invalid hexstr\n"); + goto fail; + } + int printed = 0; + RzAnalysisOp aop = { 0 }; + while (printed < bufsz) { + aop.size = 0; + if (rz_analysis_op(core->analysis, &aop, core->offset, + (const ut8 *)hex_arg + printed, bufsz - printed, RZ_ANALYSIS_OP_MASK_ESIL) <= 0 || + aop.size < 1) { + RZ_LOG_ERROR("Cannot decode instruction\n"); + rz_analysis_op_fini(&aop); + goto fail; + } + rz_strbuf_appendf(buf, "%s\n", RZ_STRBUF_SAFEGET(&aop.esil)); + printed += aop.size; + rz_analysis_op_fini(&aop); + } + free(hex_arg); + return rz_strbuf_drain(buf); +fail: + rz_strbuf_free(buf); + free(hex_arg); + return NULL; } \ No newline at end of file diff --git a/librz/include/rz_core.h b/librz/include/rz_core.h index 35ec3140802..2a5eaf5b906 100644 --- a/librz/include/rz_core.h +++ b/librz/include/rz_core.h @@ -1053,7 +1053,11 @@ RZ_API bool rz_core_meta_string_add(RzCore *core, ut64 addr, ut64 size, RzStrEnc RZ_API bool rz_core_meta_pascal_string_add(RzCore *core, ut64 addr, RzStrEnc encoding, RZ_NULLABLE const char *name); // cprint.c -RZ_API char *rz_core_print_string_c_cpp(RzCore *core); +RZ_API RZ_OWN char *rz_core_print_string_c_cpp(RzCore *core); +RZ_API RZ_OWN char *rz_core_hex_of_assembly(RzCore *core, const char *assembly); +RZ_API RZ_OWN char *rz_core_esil_of_assembly(RzCore *core, const char *assembly); +RZ_API RZ_OWN char *rz_core_assembly_of_hex(RzCore *core, const char *hexstr); +RZ_API RZ_OWN char *rz_core_esil_of_hex(RzCore *core, const char *hexstr); /* rtr */ RZ_API bool rz_core_rtr_init(RZ_NONNULL RzCore *core); diff --git a/test/db/cmd/cmd_pae b/test/db/cmd/cmd_pae index 6fe403b9b8f..61e8997cd4d 100644 --- a/test/db/cmd/cmd_pae +++ b/test/db/cmd/cmd_pae @@ -24,3 +24,22 @@ EXPECT=< Date: Wed, 20 Jul 2022 13:27:25 +0800 Subject: [PATCH 2/3] fixup! Port command pa to new shell --- librz/core/cmd/cmd_print.c | 2 +- test/db/cmd/cmd_pae | 10 ++++++++++ test/db/cmd/cmds | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index 07470900f48..63c3e87a5b0 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -4699,7 +4699,7 @@ RZ_IPI RzCmdStatus rz_esil_of_assembly_handler(RzCore *core, int argc, const cha if (!buf) { return RZ_CMD_STATUS_ERROR; } - rz_cons_println(buf); + rz_cons_print(buf); free(buf); return RZ_CMD_STATUS_OK; } diff --git a/test/db/cmd/cmd_pae b/test/db/cmd/cmd_pae index 61e8997cd4d..928fe31d3b5 100644 --- a/test/db/cmd/cmd_pae +++ b/test/db/cmd/cmd_pae @@ -42,4 +42,14 @@ CMDS=pae xor ebp, ebp EXPECT=< Date: Wed, 20 Jul 2022 20:09:45 +0800 Subject: [PATCH 3/3] fixup! fixup! Port command pa to new shell --- librz/core/cmd/cmd_print.c | 53 +++++++++++++++++------------ librz/core/cmd_descs/cmd_descs.c | 23 +++++++++++-- librz/core/cmd_descs/cmd_descs.h | 1 + librz/core/cmd_descs/cmd_print.yaml | 13 +++++-- librz/core/cprint.c | 27 ++++++--------- librz/include/rz_core.h | 4 +-- 6 files changed, 77 insertions(+), 44 deletions(-) diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index 63c3e87a5b0..c5f0ec0038d 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -766,22 +766,6 @@ static const ut32 colormap[256] = { 0xffffff, }; -static void __cmd_pad(RzCore *core, const char *arg) { - if (*arg == '?') { - eprintf("Usage: pad [hexpairs] # disassembly given bytes\n"); - return; - } - rz_asm_set_pc(core->rasm, core->offset); - bool is_pseudo = rz_config_get_i(core->config, "asm.pseudo"); - RzAsmCode *acode = rz_asm_mdisassemble_hexstr(core->rasm, is_pseudo ? core->parser : NULL, arg); - if (acode) { - rz_cons_print(acode->assembly); - rz_asm_code_free(acode); - } else { - eprintf("Invalid hexstr\n"); - } -} - // colordump static void cmd_prc(RzCore *core, const ut8 *block, int len) { const char *chars = " .,:;!O@#"; @@ -4705,8 +4689,20 @@ RZ_IPI RzCmdStatus rz_esil_of_assembly_handler(RzCore *core, int argc, const cha } RZ_IPI RzCmdStatus rz_assembly_of_hex_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) { - char *buf = rz_core_assembly_of_hex(core, argv[1]); + ut8 *hex = calloc(1, strlen(argv[1]) + 1); + if (!hex) { + RZ_LOG_ERROR("Fail to allocate memory\n"); + return RZ_CMD_STATUS_ERROR; + } + int len = rz_hex_str2bin(argv[1], hex); + if (len < 1) { + RZ_LOG_ERROR("rz_hex_str2bin: invalid hexstr\n"); + free(hex); + return RZ_CMD_STATUS_ERROR; + } + char *buf = rz_core_assembly_of_hex(core, hex, len); if (!buf) { + free(hex); return RZ_CMD_STATUS_ERROR; } rz_cons_print(buf); @@ -4714,13 +4710,31 @@ RZ_IPI RzCmdStatus rz_assembly_of_hex_handler(RzCore *core, int argc, const char return RZ_CMD_STATUS_OK; } +RZ_IPI RzCmdStatus rz_assembly_of_hex_alias_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) { + return rz_assembly_of_hex_handler(core, argc, argv, mode); +} + RZ_IPI RzCmdStatus rz_esil_of_hex_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) { - char *buf = rz_core_esil_of_hex(core, argv[1]); + ut8 *hex = calloc(1, strlen(argv[1]) + 1); + if (!hex) { + RZ_LOG_ERROR("Fail to allocate memory\n"); + return RZ_CMD_STATUS_ERROR; + } + int len = rz_hex_str2bin(argv[1], hex); + if (len < 1) { + RZ_LOG_ERROR("rz_hex_str2bin: invalid hexstr\n"); + free(hex); + return RZ_CMD_STATUS_ERROR; + } + char *buf = rz_core_esil_of_hex(core, hex, len); if (!buf) { + // rz_core_esil_of_hex outputs the error message + free(hex); return RZ_CMD_STATUS_ERROR; } rz_cons_print(buf); free(buf); + free(hex); return RZ_CMD_STATUS_OK; } @@ -4974,9 +4988,6 @@ RZ_IPI int rz_cmd_print(void *data, const char *input) { case 'u': // "piu" disasm until ret/jmp . todo: accept arg to specify type disasm_until_ret(core, core->offset, input[2], input + 2); break; - case 'x': // "pix" - __cmd_pad(core, rz_str_trim_head_ro(input + 2)); - break; case 'a': // "pia" is like "pda", but with "pi" output if (l != 0) { rz_core_print_disasm_all(core, core->offset, diff --git a/librz/core/cmd_descs/cmd_descs.c b/librz/core/cmd_descs/cmd_descs.c index 8386d1a377f..51586442225 100644 --- a/librz/core/cmd_descs/cmd_descs.c +++ b/librz/core/cmd_descs/cmd_descs.c @@ -469,6 +469,7 @@ static const RzCmdDescArg cmd_disassemble_summarize_block_args[2]; static const RzCmdDescArg cmd_print_gadget_add_args[6]; static const RzCmdDescArg cmd_print_gadget_move_args[6]; static const RzCmdDescArg cmd_print_hash_cfg_args[2]; +static const RzCmdDescArg assembly_of_hex_alias_args[2]; static const RzCmdDescArg cmd_print_magic_args[2]; static const RzCmdDescArg print_utf16le_args[2]; static const RzCmdDescArg print_utf32le_args[2]; @@ -10457,7 +10458,7 @@ static const RzCmdDescHelp esil_of_assembly_help = { static const RzCmdDescArg assembly_of_hex_args[] = { { .name = "hexpair", - .type = RZ_CMD_ARG_TYPE_RZNUM, + .type = RZ_CMD_ARG_TYPE_STRING, .flags = RZ_CMD_ARG_FLAG_LAST, .optional = false, @@ -10472,7 +10473,7 @@ static const RzCmdDescHelp assembly_of_hex_help = { static const RzCmdDescArg esil_of_hex_args[] = { { .name = "hexpair", - .type = RZ_CMD_ARG_TYPE_RZNUM, + .type = RZ_CMD_ARG_TYPE_STRING, .flags = RZ_CMD_ARG_FLAG_LAST, .optional = false, @@ -11010,6 +11011,21 @@ static const RzCmdDescHelp cmd_print_hash_cfg_algo_list_help = { .args = cmd_print_hash_cfg_algo_list_args, }; +static const RzCmdDescArg assembly_of_hex_alias_args[] = { + { + .name = "hexpair", + .type = RZ_CMD_ARG_TYPE_STRING, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = false, + + }, + { 0 }, +}; +static const RzCmdDescHelp assembly_of_hex_alias_help = { + .summary = "Print assembly expression from hexpairs (alias for pad)", + .args = assembly_of_hex_alias_args, +}; + static const RzCmdDescHelp cmd_print_timestamp_help = { .summary = "Print timestamps", }; @@ -16373,6 +16389,9 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) { RzCmdDesc *cmd_print_hash_cfg_algo_list_cd = rz_cmd_desc_argv_state_new(core->rcmd, cmd_print_default_cd, "phl", RZ_OUTPUT_MODE_STANDARD | RZ_OUTPUT_MODE_RIZIN | RZ_OUTPUT_MODE_JSON | RZ_OUTPUT_MODE_QUIET, rz_cmd_print_hash_cfg_algo_list_handler, &cmd_print_hash_cfg_algo_list_help); rz_warn_if_fail(cmd_print_hash_cfg_algo_list_cd); + RzCmdDesc *assembly_of_hex_alias_cd = rz_cmd_desc_argv_modes_new(core->rcmd, cmd_print_cd, "pix", RZ_OUTPUT_MODE_STANDARD, rz_assembly_of_hex_alias_handler, &assembly_of_hex_alias_help); + rz_warn_if_fail(assembly_of_hex_alias_cd); + RzCmdDesc *cmd_print_timestamp_cd = rz_cmd_desc_group_new(core->rcmd, cmd_print_cd, "pt", rz_cmd_print_timestamp_unix_handler, &cmd_print_timestamp_unix_help, &cmd_print_timestamp_help); rz_warn_if_fail(cmd_print_timestamp_cd); RzCmdDesc *cmd_print_timestamp_current_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_timestamp_cd, "pt.", rz_cmd_print_timestamp_current_handler, &cmd_print_timestamp_current_help); diff --git a/librz/core/cmd_descs/cmd_descs.h b/librz/core/cmd_descs/cmd_descs.h index 39757aaac0c..6073ed8aede 100644 --- a/librz/core/cmd_descs/cmd_descs.h +++ b/librz/core/cmd_descs/cmd_descs.h @@ -804,6 +804,7 @@ RZ_IPI RzCmdStatus rz_cmd_print_gadget_remove_handler(RzCore *core, int argc, co RZ_IPI RzCmdStatus rz_cmd_print_gadget_move_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_hash_cfg_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_hash_cfg_algo_list_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state); +RZ_IPI RzCmdStatus rz_assembly_of_hex_alias_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode); RZ_IPI RzCmdStatus rz_cmd_print_timestamp_unix_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_timestamp_current_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_timestamp_dos_handler(RzCore *core, int argc, const char **argv); diff --git a/librz/core/cmd_descs/cmd_print.yaml b/librz/core/cmd_descs/cmd_print.yaml index 0d35aeb9de0..6b77429a73c 100644 --- a/librz/core/cmd_descs/cmd_print.yaml +++ b/librz/core/cmd_descs/cmd_print.yaml @@ -31,7 +31,7 @@ commands: - RZ_OUTPUT_MODE_STANDARD args: - name: hexpair - type: RZ_CMD_ARG_TYPE_RZNUM + type: RZ_CMD_ARG_TYPE_STRING optional: false - name: pade summary: Print ESIL expression from hexpairs @@ -40,7 +40,7 @@ commands: - RZ_OUTPUT_MODE_STANDARD args: - name: hexpair - type: RZ_CMD_ARG_TYPE_RZNUM + type: RZ_CMD_ARG_TYPE_STRING optional: false - name: pc summary: Print bytes as code byte arrays. @@ -389,6 +389,15 @@ commands: - RZ_OUTPUT_MODE_JSON - RZ_OUTPUT_MODE_QUIET args: [] + - name: pix + summary: Print assembly expression from hexpairs (alias for pad) + cname: assembly_of_hex_alias + modes: + - RZ_OUTPUT_MODE_STANDARD + args: + - name: hexpair + type: RZ_CMD_ARG_TYPE_STRING + optional: false - name: pt summary: Print timestamps cname: cmd_print_timestamp diff --git a/librz/core/cprint.c b/librz/core/cprint.c index 387da59e63c..e8d6dff3250 100644 --- a/librz/core/cprint.c +++ b/librz/core/cprint.c @@ -102,18 +102,18 @@ RZ_API RZ_OWN char *rz_core_esil_of_assembly(RzCore *core, const char *assembly) /** * \brief Get the assembly of the hexstr * \param core RzCore - * \param hexstr hexstr + * \param hex hex + * \param len length of hex * \return a string containing the assembly of the hexstr */ -RZ_API RZ_OWN char *rz_core_assembly_of_hex(RzCore *core, const char *hexstr) { +RZ_API RZ_OWN char *rz_core_assembly_of_hex(RzCore *core, ut8 *hex, int len) { RzStrBuf *buf = rz_strbuf_new(""); if (!buf) { RZ_LOG_ERROR("Fail to allocate memory\n"); return NULL; } rz_asm_set_pc(core->rasm, core->offset); - bool is_pseudo = rz_config_get_i(core->config, "asm.pseudo"); - RzAsmCode *acode = rz_asm_mdisassemble_hexstr(core->rasm, is_pseudo ? core->parser : NULL, hexstr); + RzAsmCode *acode = rz_asm_mdisassemble(core->rasm, hex, len); if (!acode) { RZ_LOG_ERROR("Invalid hexstr\n"); rz_strbuf_free(buf); @@ -127,27 +127,22 @@ RZ_API RZ_OWN char *rz_core_assembly_of_hex(RzCore *core, const char *hexstr) { /** * \brief Get the esil of the hexstr * \param core RzCore - * \param hexstr hexstr + * \param hex hex + * \param len length of hex * \return a string containing the esil of the hexstr */ -RZ_API RZ_OWN char *rz_core_esil_of_hex(RzCore *core, const char *hexstr) { +RZ_API RZ_OWN char *rz_core_esil_of_hex(RzCore *core, ut8 *hex, int len) { RzStrBuf *buf = rz_strbuf_new(""); - char *hex_arg = calloc(1, strlen(hexstr) + 1); - if (!buf || !hex_arg) { + if (!buf) { RZ_LOG_ERROR("Fail to allocate memory\n"); goto fail; } - int bufsz = rz_hex_str2bin(hexstr, (ut8 *)hex_arg); - if (bufsz < 1) { - RZ_LOG_ERROR("Invalid hexstr\n"); - goto fail; - } int printed = 0; RzAnalysisOp aop = { 0 }; - while (printed < bufsz) { + while (printed < len) { aop.size = 0; if (rz_analysis_op(core->analysis, &aop, core->offset, - (const ut8 *)hex_arg + printed, bufsz - printed, RZ_ANALYSIS_OP_MASK_ESIL) <= 0 || + (const ut8 *)hex + printed, len - printed, RZ_ANALYSIS_OP_MASK_ESIL) <= 0 || aop.size < 1) { RZ_LOG_ERROR("Cannot decode instruction\n"); rz_analysis_op_fini(&aop); @@ -157,10 +152,8 @@ RZ_API RZ_OWN char *rz_core_esil_of_hex(RzCore *core, const char *hexstr) { printed += aop.size; rz_analysis_op_fini(&aop); } - free(hex_arg); return rz_strbuf_drain(buf); fail: rz_strbuf_free(buf); - free(hex_arg); return NULL; } \ No newline at end of file diff --git a/librz/include/rz_core.h b/librz/include/rz_core.h index 2a5eaf5b906..73b96eb145c 100644 --- a/librz/include/rz_core.h +++ b/librz/include/rz_core.h @@ -1056,8 +1056,8 @@ RZ_API bool rz_core_meta_pascal_string_add(RzCore *core, ut64 addr, RzStrEnc enc RZ_API RZ_OWN char *rz_core_print_string_c_cpp(RzCore *core); RZ_API RZ_OWN char *rz_core_hex_of_assembly(RzCore *core, const char *assembly); RZ_API RZ_OWN char *rz_core_esil_of_assembly(RzCore *core, const char *assembly); -RZ_API RZ_OWN char *rz_core_assembly_of_hex(RzCore *core, const char *hexstr); -RZ_API RZ_OWN char *rz_core_esil_of_hex(RzCore *core, const char *hexstr); +RZ_API RZ_OWN char *rz_core_assembly_of_hex(RzCore *core, ut8 *hex, int len); +RZ_API RZ_OWN char *rz_core_esil_of_hex(RzCore *core, ut8 *hex, int len); /* rtr */ RZ_API bool rz_core_rtr_init(RZ_NONNULL RzCore *core);