diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index 40a72c22f50..ba893f434a9 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -359,23 +359,6 @@ static const char *help_msg_pif[] = { "print instructions of function in JSON format", }; -static const char *help_msg_po[] = { - "Usage:", "po[24aAdlmorsx]", " [hexpairs] @ addr[!bsize]", - "po[24aAdlmorsx]", "", "without hexpair values, clipboard is used", - "po2", " [val]", "2= 2 byte endian swap", - "po4", " [val]", "4= 4 byte endian swap", - "poa", " [val]", "+= addition (f.ex: poa 0102)", - "poA", " [val]", "&= and", - "pod", " [val]", "/= divide", - "pol", " [val]", "<<= shift left", - "pom", " [val]", "*= multiply", - "poo", " [val]", "|= or", - "por", " [val]", ">>= shift right", - "pos", " [val]", "-= substraction", - "pox", " [val]", "^= xor (f.ex: pox 0x90)", - NULL -}; - static const char *help_msg_ps[] = { "Usage:", "ps[bijqpsuwWxz+] [N]", "Print String", "ps", "", "print string", @@ -2329,105 +2312,22 @@ static bool cmd_print_pxA(RzCore *core, int len, RzOutputMode mode) { return true; } -static ut8 *old_transform_op(RzCore *core, const char *val, char op, int *buflen) { - RzCoreWriteOp wop; - switch (op) { - case '2': - wop = RZ_CORE_WRITE_OP_BYTESWAP2; - break; - case '4': - wop = RZ_CORE_WRITE_OP_BYTESWAP4; - break; - case '8': - wop = RZ_CORE_WRITE_OP_BYTESWAP8; - break; - case 'a': - wop = RZ_CORE_WRITE_OP_ADD; - break; - case 'A': - wop = RZ_CORE_WRITE_OP_AND; - break; - case 'd': - wop = RZ_CORE_WRITE_OP_DIV; - break; - case 'l': - wop = RZ_CORE_WRITE_OP_SHIFT_LEFT; - break; - case 'm': - wop = RZ_CORE_WRITE_OP_MUL; - break; - case 'o': - wop = RZ_CORE_WRITE_OP_OR; - break; - case 'r': - wop = RZ_CORE_WRITE_OP_SHIFT_RIGHT; - break; - case 's': - wop = RZ_CORE_WRITE_OP_SUB; - break; - case 'x': - wop = RZ_CORE_WRITE_OP_XOR; - break; - default: - wop = RZ_CORE_WRITE_OP_XOR; - rz_warn_if_reached(); - break; - } - +/* Uses data from clipboard if value is NULL */ +static bool print_operation_transform(RzCore *core, RzCoreWriteOp op, RZ_NULLABLE const char *val) { ut8 *hex = NULL; - int hexlen = -1; + int hexlen = -1, buflen = -1; if (val) { - val = rz_str_trim_head_ro(val); hex = RZ_NEWS(ut8, (strlen(val) + 1) / 2); if (!hex) { - return NULL; + return false; } - hexlen = rz_hex_str2bin(val, hex); } - ut8 *result = rz_core_transform_op(core, core->offset, wop, hex, hexlen, buflen); + ut8 *buf = rz_core_transform_op(core, core->offset, op, hex, hexlen, &buflen); free(hex); - return result; -} - -static void cmd_print_op(RzCore *core, const char *input) { - ut8 *buf; - int buflen = -1; - - if (!input[0]) - return; - switch (input[1]) { - case 'a': - case 's': - case 'A': - case 'x': - case 'r': - case 'l': - case 'm': - case 'd': - case 'o': - case '2': - case '4': - if (input[2]) { // parse val from arg - buf = old_transform_op(core, input + 3, input[1], &buflen); - } else { // use clipboard instead of val - buf = old_transform_op(core, NULL, input[1], &buflen); - } - break; - case 'n': - buf = old_transform_op(core, "ff", 'x', &buflen); - break; - case '\0': - case '?': - default: - rz_core_cmd_help(core, help_msg_po); - return; - } - if (buf) { - rz_core_print_hexdump(core, core->offset, buf, - buflen, 16, 1, 1); - free(buf); - } + rz_core_print_hexdump(core, core->offset, buf, buflen, 16, 1, 1); + free(buf); + return true; } static void printraw(RzCore *core, int len) { @@ -4721,9 +4621,6 @@ RZ_IPI int rz_cmd_print(void *data, const char *input) { break; } break; - case 'o': // "po" - cmd_print_op(core, input); - break; case 'x': // "px" { bool show_offset = rz_config_get_i(core->config, "hex.offset"); @@ -6563,3 +6460,51 @@ RZ_IPI RzCmdStatus rz_print_url_encode_zero_handler(RzCore *core, int argc, cons core_print_raw_buffer(&opt); return RZ_CMD_STATUS_OK; } + +RZ_IPI RzCmdStatus rz_print_operation_2swap_handler(RzCore *core, int argc, const char **argv) { + return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_BYTESWAP2, NULL)); +} + +RZ_IPI RzCmdStatus rz_print_operation_4swap_handler(RzCore *core, int argc, const char **argv) { + return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_BYTESWAP4, NULL)); +} + +RZ_IPI RzCmdStatus rz_print_operation_8swap_handler(RzCore *core, int argc, const char **argv) { + return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_BYTESWAP8, NULL)); +} + +RZ_IPI RzCmdStatus rz_print_operation_add_handler(RzCore *core, int argc, const char **argv) { + return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_ADD, argv[1])); +} + +RZ_IPI RzCmdStatus rz_print_operation_and_handler(RzCore *core, int argc, const char **argv) { + return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_AND, argv[1])); +} + +RZ_IPI RzCmdStatus rz_print_operation_div_handler(RzCore *core, int argc, const char **argv) { + return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_DIV, argv[1])); +} + +RZ_IPI RzCmdStatus rz_print_operation_shl_handler(RzCore *core, int argc, const char **argv) { + return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_SHIFT_LEFT, argv[1])); +} + +RZ_IPI RzCmdStatus rz_print_operation_mul_handler(RzCore *core, int argc, const char **argv) { + return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_MUL, argv[1])); +} + +RZ_IPI RzCmdStatus rz_print_operation_or_handler(RzCore *core, int argc, const char **argv) { + return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_OR, argv[1])); +} + +RZ_IPI RzCmdStatus rz_print_operation_shr_handler(RzCore *core, int argc, const char **argv) { + return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_SHIFT_RIGHT, argv[1])); +} + +RZ_IPI RzCmdStatus rz_print_operation_sub_handler(RzCore *core, int argc, const char **argv) { + return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_SUB, argv[1])); +} + +RZ_IPI RzCmdStatus rz_print_operation_xor_handler(RzCore *core, int argc, const char **argv) { + return bool2status(print_operation_transform(core, RZ_CORE_WRITE_OP_XOR, argv[1])); +} diff --git a/librz/core/cmd_descs/cmd_descs.c b/librz/core/cmd_descs/cmd_descs.c index 1c12daa1d48..498a5fa54dc 100644 --- a/librz/core/cmd_descs/cmd_descs.c +++ b/librz/core/cmd_descs/cmd_descs.c @@ -528,6 +528,15 @@ static const RzCmdDescArg cmd_print_hash_cfg_args[2]; static const RzCmdDescArg assembly_of_hex_alias_args[2]; static const RzCmdDescArg print_instructions_args[2]; static const RzCmdDescArg cmd_print_magic_args[2]; +static const RzCmdDescArg print_operation_add_args[2]; +static const RzCmdDescArg print_operation_and_args[2]; +static const RzCmdDescArg print_operation_div_args[2]; +static const RzCmdDescArg print_operation_shl_args[2]; +static const RzCmdDescArg print_operation_mul_args[2]; +static const RzCmdDescArg print_operation_or_args[2]; +static const RzCmdDescArg print_operation_shr_args[2]; +static const RzCmdDescArg print_operation_sub_args[2]; +static const RzCmdDescArg print_operation_xor_args[2]; static const RzCmdDescArg print_utf16le_args[2]; static const RzCmdDescArg print_utf32le_args[2]; static const RzCmdDescArg print_utf16be_args[2]; @@ -12354,6 +12363,168 @@ static const RzCmdDescHelp cmd_print_magic_help = { .args = cmd_print_magic_args, }; +static const RzCmdDescHelp po_help = { + .summary = "Print operation applied on the data", +}; +static const RzCmdDescArg print_operation_2swap_args[] = { + { 0 }, +}; +static const RzCmdDescHelp print_operation_2swap_help = { + .summary = "2-byte endian swap", + .args = print_operation_2swap_args, +}; + +static const RzCmdDescArg print_operation_4swap_args[] = { + { 0 }, +}; +static const RzCmdDescHelp print_operation_4swap_help = { + .summary = "4-byte endian swap", + .args = print_operation_4swap_args, +}; + +static const RzCmdDescArg print_operation_8swap_args[] = { + { 0 }, +}; +static const RzCmdDescHelp print_operation_8swap_help = { + .summary = "8-byte endian swap", + .args = print_operation_8swap_args, +}; + +static const RzCmdDescArg print_operation_add_args[] = { + { + .name = "value", + .type = RZ_CMD_ARG_TYPE_RZNUM, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp print_operation_add_help = { + .summary = "Apply addition", + .args = print_operation_add_args, +}; + +static const RzCmdDescArg print_operation_and_args[] = { + { + .name = "value", + .type = RZ_CMD_ARG_TYPE_RZNUM, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp print_operation_and_help = { + .summary = "Apply AND operation", + .args = print_operation_and_args, +}; + +static const RzCmdDescArg print_operation_div_args[] = { + { + .name = "value", + .type = RZ_CMD_ARG_TYPE_RZNUM, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp print_operation_div_help = { + .summary = "Apply division operation", + .args = print_operation_div_args, +}; + +static const RzCmdDescArg print_operation_shl_args[] = { + { + .name = "value", + .type = RZ_CMD_ARG_TYPE_RZNUM, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp print_operation_shl_help = { + .summary = "Apply shift left operation", + .args = print_operation_shl_args, +}; + +static const RzCmdDescArg print_operation_mul_args[] = { + { + .name = "value", + .type = RZ_CMD_ARG_TYPE_RZNUM, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp print_operation_mul_help = { + .summary = "Apply multiplication operation", + .args = print_operation_mul_args, +}; + +static const RzCmdDescArg print_operation_or_args[] = { + { + .name = "value", + .type = RZ_CMD_ARG_TYPE_RZNUM, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp print_operation_or_help = { + .summary = "Apply OR operation", + .args = print_operation_or_args, +}; + +static const RzCmdDescArg print_operation_shr_args[] = { + { + .name = "value", + .type = RZ_CMD_ARG_TYPE_RZNUM, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp print_operation_shr_help = { + .summary = "Apply shift right operation", + .args = print_operation_shr_args, +}; + +static const RzCmdDescArg print_operation_sub_args[] = { + { + .name = "value", + .type = RZ_CMD_ARG_TYPE_RZNUM, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp print_operation_sub_help = { + .summary = "Apply subtraction operation", + .args = print_operation_sub_args, +}; + +static const RzCmdDescArg print_operation_xor_args[] = { + { + .name = "value", + .type = RZ_CMD_ARG_TYPE_RZNUM, + .flags = RZ_CMD_ARG_FLAG_LAST, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp print_operation_xor_help = { + .summary = "Apply XOR operation", + .args = print_operation_xor_args, +}; + static const RzCmdDescArg print_string_c_cpp_args[] = { { 0 }, }; @@ -18534,6 +18705,44 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) { RzCmdDesc *cmd_print_magic_cd = rz_cmd_desc_argv_modes_new(core->rcmd, cmd_print_cd, "pm", RZ_OUTPUT_MODE_JSON, rz_cmd_print_magic_handler, &cmd_print_magic_help); rz_warn_if_fail(cmd_print_magic_cd); + RzCmdDesc *po_cd = rz_cmd_desc_group_new(core->rcmd, cmd_print_cd, "po", NULL, NULL, &po_help); + rz_warn_if_fail(po_cd); + RzCmdDesc *print_operation_2swap_cd = rz_cmd_desc_argv_new(core->rcmd, po_cd, "po2", rz_print_operation_2swap_handler, &print_operation_2swap_help); + rz_warn_if_fail(print_operation_2swap_cd); + + RzCmdDesc *print_operation_4swap_cd = rz_cmd_desc_argv_new(core->rcmd, po_cd, "po4", rz_print_operation_4swap_handler, &print_operation_4swap_help); + rz_warn_if_fail(print_operation_4swap_cd); + + RzCmdDesc *print_operation_8swap_cd = rz_cmd_desc_argv_new(core->rcmd, po_cd, "po8", rz_print_operation_8swap_handler, &print_operation_8swap_help); + rz_warn_if_fail(print_operation_8swap_cd); + + RzCmdDesc *print_operation_add_cd = rz_cmd_desc_argv_new(core->rcmd, po_cd, "poa", rz_print_operation_add_handler, &print_operation_add_help); + rz_warn_if_fail(print_operation_add_cd); + + RzCmdDesc *print_operation_and_cd = rz_cmd_desc_argv_new(core->rcmd, po_cd, "poA", rz_print_operation_and_handler, &print_operation_and_help); + rz_warn_if_fail(print_operation_and_cd); + + RzCmdDesc *print_operation_div_cd = rz_cmd_desc_argv_new(core->rcmd, po_cd, "pod", rz_print_operation_div_handler, &print_operation_div_help); + rz_warn_if_fail(print_operation_div_cd); + + RzCmdDesc *print_operation_shl_cd = rz_cmd_desc_argv_new(core->rcmd, po_cd, "pol", rz_print_operation_shl_handler, &print_operation_shl_help); + rz_warn_if_fail(print_operation_shl_cd); + + RzCmdDesc *print_operation_mul_cd = rz_cmd_desc_argv_new(core->rcmd, po_cd, "pom", rz_print_operation_mul_handler, &print_operation_mul_help); + rz_warn_if_fail(print_operation_mul_cd); + + RzCmdDesc *print_operation_or_cd = rz_cmd_desc_argv_new(core->rcmd, po_cd, "poo", rz_print_operation_or_handler, &print_operation_or_help); + rz_warn_if_fail(print_operation_or_cd); + + RzCmdDesc *print_operation_shr_cd = rz_cmd_desc_argv_new(core->rcmd, po_cd, "por", rz_print_operation_shr_handler, &print_operation_shr_help); + rz_warn_if_fail(print_operation_shr_cd); + + RzCmdDesc *print_operation_sub_cd = rz_cmd_desc_argv_new(core->rcmd, po_cd, "pos", rz_print_operation_sub_handler, &print_operation_sub_help); + rz_warn_if_fail(print_operation_sub_cd); + + RzCmdDesc *print_operation_xor_cd = rz_cmd_desc_argv_new(core->rcmd, po_cd, "pox", rz_print_operation_xor_handler, &print_operation_xor_help); + rz_warn_if_fail(print_operation_xor_cd); + RzCmdDesc *print_string_c_cpp_cd = rz_cmd_desc_argv_modes_new(core->rcmd, cmd_print_cd, "psc", RZ_OUTPUT_MODE_STANDARD, rz_print_string_c_cpp_handler, &print_string_c_cpp_help); rz_warn_if_fail(print_string_c_cpp_cd); diff --git a/librz/core/cmd_descs/cmd_descs.h b/librz/core/cmd_descs/cmd_descs.h index c85935e9035..728498258b5 100644 --- a/librz/core/cmd_descs/cmd_descs.h +++ b/librz/core/cmd_descs/cmd_descs.h @@ -1759,6 +1759,30 @@ RZ_IPI RzCmdStatus rz_cmd_print_timestamp_hfs_handler(RzCore *core, int argc, co RZ_IPI RzCmdStatus rz_cmd_print_timestamp_ntfs_handler(RzCore *core, int argc, const char **argv); // "pm" RZ_IPI RzCmdStatus rz_cmd_print_magic_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode); +// "po2" +RZ_IPI RzCmdStatus rz_print_operation_2swap_handler(RzCore *core, int argc, const char **argv); +// "po4" +RZ_IPI RzCmdStatus rz_print_operation_4swap_handler(RzCore *core, int argc, const char **argv); +// "po8" +RZ_IPI RzCmdStatus rz_print_operation_8swap_handler(RzCore *core, int argc, const char **argv); +// "poa" +RZ_IPI RzCmdStatus rz_print_operation_add_handler(RzCore *core, int argc, const char **argv); +// "poA" +RZ_IPI RzCmdStatus rz_print_operation_and_handler(RzCore *core, int argc, const char **argv); +// "pod" +RZ_IPI RzCmdStatus rz_print_operation_div_handler(RzCore *core, int argc, const char **argv); +// "pol" +RZ_IPI RzCmdStatus rz_print_operation_shl_handler(RzCore *core, int argc, const char **argv); +// "pom" +RZ_IPI RzCmdStatus rz_print_operation_mul_handler(RzCore *core, int argc, const char **argv); +// "poo" +RZ_IPI RzCmdStatus rz_print_operation_or_handler(RzCore *core, int argc, const char **argv); +// "por" +RZ_IPI RzCmdStatus rz_print_operation_shr_handler(RzCore *core, int argc, const char **argv); +// "pos" +RZ_IPI RzCmdStatus rz_print_operation_sub_handler(RzCore *core, int argc, const char **argv); +// "pox" +RZ_IPI RzCmdStatus rz_print_operation_xor_handler(RzCore *core, int argc, const char **argv); // "psc" RZ_IPI RzCmdStatus rz_print_string_c_cpp_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode); // "psw" diff --git a/librz/core/cmd_descs/cmd_print.yaml b/librz/core/cmd_descs/cmd_print.yaml index 0d5f1abc164..1954aae9aa1 100644 --- a/librz/core/cmd_descs/cmd_print.yaml +++ b/librz/core/cmd_descs/cmd_print.yaml @@ -508,6 +508,84 @@ commands: optional: true modes: - RZ_OUTPUT_MODE_JSON + - name: po + summary: Print operation applied on the data + subcommands: + - name: po2 + summary: 2-byte endian swap + cname: print_operation_2swap + args: [] + - name: po4 + summary: 4-byte endian swap + cname: print_operation_4swap + args: [] + - name: po8 + summary: 8-byte endian swap + cname: print_operation_8swap + args: [] + - name: poa + summary: Apply addition + cname: print_operation_add + args: + - name: value + type: RZ_CMD_ARG_TYPE_RZNUM + optional: true + - name: poA + summary: Apply AND operation + cname: print_operation_and + args: + - name: value + type: RZ_CMD_ARG_TYPE_RZNUM + optional: true + - name: pod + summary: Apply division operation + cname: print_operation_div + args: + - name: value + type: RZ_CMD_ARG_TYPE_RZNUM + optional: true + - name: pol + summary: Apply shift left operation + cname: print_operation_shl + args: + - name: value + type: RZ_CMD_ARG_TYPE_RZNUM + optional: true + - name: pom + summary: Apply multiplication operation + cname: print_operation_mul + args: + - name: value + type: RZ_CMD_ARG_TYPE_RZNUM + optional: true + - name: poo + summary: Apply OR operation + cname: print_operation_or + args: + - name: value + type: RZ_CMD_ARG_TYPE_RZNUM + optional: true + - name: por + summary: Apply shift right operation + cname: print_operation_shr + args: + - name: value + type: RZ_CMD_ARG_TYPE_RZNUM + optional: true + - name: pos + summary: Apply subtraction operation + cname: print_operation_sub + args: + - name: value + type: RZ_CMD_ARG_TYPE_RZNUM + optional: true + - name: pox + summary: Apply XOR operation + cname: print_operation_xor + args: + - name: value + type: RZ_CMD_ARG_TYPE_RZNUM + optional: true - name: psc summary: Generate a C/C++ string cname: print_string_c_cpp