From a0475a4b13f161ac910e68fa4477ed323d2826ee Mon Sep 17 00:00:00 2001 From: Peiwei Hu Date: Tue, 19 Jul 2022 10:13:37 +0800 Subject: [PATCH 1/3] Port command p6 to new shell --- librz/core/cmd/cmd_print.c | 69 ++++++++++++++--------------- librz/core/cmd_descs/cmd_descs.c | 27 +++++++++++ librz/core/cmd_descs/cmd_descs.h | 2 + librz/core/cmd_descs/cmd_print.yaml | 15 +++++++ test/db/cmd/cmd_p6 | 21 +++++++++ 5 files changed, 98 insertions(+), 36 deletions(-) create mode 100644 test/db/cmd/cmd_p6 diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index 8f663024544..3e90f5c4cec 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -62,13 +62,6 @@ static const char *help_msg_pc[] = { NULL }; -static const char *help_msg_p6[] = { - "Usage: p6[de]", "[len]", "base64 decoding/encoding", - "p6d", "[len]", "decode base64", - "p6e", "[len]", "encode base64", - NULL -}; - static const char *help_msg_pF[] = { "Usage: pF[apdbA]", "[len]", "parse ASN1, PKCS, X509, DER, protobuf, axml", "pFa", "[len]", "decode ASN1 from current block", @@ -6224,35 +6217,6 @@ RZ_IPI int rz_cmd_print(void *data, const char *input) { } } break; - case '6': // "p6" - if (l) { - int malen = (core->blocksize * 4) + 1; - ut8 *buf = malloc(malen); - if (!buf) { - break; - } - memset(buf, 0, malen); - switch (input[1]) { - case 'd': // "p6d" - if (rz_base64_decode(buf, (const char *)block, len)) { - rz_cons_println((const char *)buf); - } else { - eprintf("rz_base64_decode: invalid stream\n"); - } - break; - case 'e': // "p6e" - len = len > core->blocksize ? core->blocksize : len; - rz_base64_encode((char *)buf, block, len); - rz_cons_println((const char *)buf); - break; - case '?': - default: - rz_core_cmd_help(core, help_msg_p6); - break; - } - free(buf); - } - break; case '8': // "p8" if (input[1] == '?') { rz_cons_printf("|Usage: p8[fj] [len] 8bit hexpair list of bytes (see pcj)\n"); @@ -7365,3 +7329,36 @@ RZ_IPI RzCmdStatus rz_cmd_disassemble_summarize_block_handler(RzCore *core, int disasm_strings(core, input_cmd, NULL); return RZ_CMD_STATUS_OK; } + +RZ_IPI RzCmdStatus rz_cmd_base64_encode_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) { + ut32 malen = (core->blocksize * 4) + 1; + ut8 *buf = malloc(malen); + + if (!buf) { + RZ_LOG_ERROR("Fail to allocate memory\n"); + return RZ_CMD_STATUS_ERROR; + } + rz_base64_encode((char *)buf, (const unsigned char *)core->block, core->blocksize); + rz_cons_println((const char *)buf); + rz_free(buf); + return RZ_CMD_STATUS_OK; +} + +RZ_IPI RzCmdStatus rz_cmd_base64_decode_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) { + ut32 malen = (core->blocksize * 4) + 1; + ut8 *buf = malloc(malen); + + if (!buf) { + RZ_LOG_ERROR("Fail to allocate memory\n"); + return RZ_CMD_STATUS_ERROR; + } + memset(buf, 0, malen); + if (rz_base64_decode(buf, (const char *)core->block, core->blocksize) < 0) { + RZ_LOG_ERROR("rz_base64_decode: invalid stream\n"); + rz_free(buf); + return RZ_CMD_STATUS_ERROR; + } + rz_cons_println((const char *)buf); + rz_free(buf); + return RZ_CMD_STATUS_OK; +} \ No newline at end of file diff --git a/librz/core/cmd_descs/cmd_descs.c b/librz/core/cmd_descs/cmd_descs.c index 6b4c6d1cc2e..824792fd678 100644 --- a/librz/core/cmd_descs/cmd_descs.c +++ b/librz/core/cmd_descs/cmd_descs.c @@ -11065,6 +11065,25 @@ static const RzCmdDescHelp print_utf32be_help = { .args = print_utf32be_args, }; +static const RzCmdDescHelp p6_help = { + .summary = "Base64 decoding/encoding", +}; +static const RzCmdDescArg cmd_base64_encode_args[] = { + { 0 }, +}; +static const RzCmdDescHelp cmd_base64_encode_help = { + .summary = "Base64 encoding", + .args = cmd_base64_encode_args, +}; + +static const RzCmdDescArg cmd_base64_decode_args[] = { + { 0 }, +}; +static const RzCmdDescHelp cmd_base64_decode_help = { + .summary = "Base64 decoding", + .args = cmd_base64_decode_args, +}; + static const RzCmdDescHelp P_help = { .summary = "Project management", }; @@ -16327,6 +16346,14 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) { RzCmdDesc *print_utf32be_cd = rz_cmd_desc_argv_modes_new(core->rcmd, cmd_print_cd, "psM", RZ_OUTPUT_MODE_STANDARD | RZ_OUTPUT_MODE_JSON, rz_print_utf32be_handler, &print_utf32be_help); rz_warn_if_fail(print_utf32be_cd); + RzCmdDesc *p6_cd = rz_cmd_desc_group_new(core->rcmd, cmd_print_cd, "p6", NULL, NULL, &p6_help); + rz_warn_if_fail(p6_cd); + RzCmdDesc *cmd_base64_encode_cd = rz_cmd_desc_argv_modes_new(core->rcmd, p6_cd, "p6e", RZ_OUTPUT_MODE_STANDARD, rz_cmd_base64_encode_handler, &cmd_base64_encode_help); + rz_warn_if_fail(cmd_base64_encode_cd); + + RzCmdDesc *cmd_base64_decode_cd = rz_cmd_desc_argv_modes_new(core->rcmd, p6_cd, "p6d", RZ_OUTPUT_MODE_STANDARD, rz_cmd_base64_decode_handler, &cmd_base64_decode_help); + rz_warn_if_fail(cmd_base64_decode_cd); + RzCmdDesc *P_cd = rz_cmd_desc_group_new(core->rcmd, root_cd, "P", NULL, NULL, &P_help); rz_warn_if_fail(P_cd); RzCmdDesc *project_save_cd = rz_cmd_desc_argv_new(core->rcmd, P_cd, "Ps", rz_project_save_handler, &project_save_help); diff --git a/librz/core/cmd_descs/cmd_descs.h b/librz/core/cmd_descs/cmd_descs.h index 47c47732049..8099f96eab3 100644 --- a/librz/core/cmd_descs/cmd_descs.h +++ b/librz/core/cmd_descs/cmd_descs.h @@ -811,6 +811,8 @@ RZ_IPI RzCmdStatus rz_print_utf16le_handler(RzCore *core, int argc, const char * RZ_IPI RzCmdStatus rz_print_utf32le_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode); RZ_IPI RzCmdStatus rz_print_utf16be_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode); RZ_IPI RzCmdStatus rz_print_utf32be_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode); +RZ_IPI RzCmdStatus rz_cmd_base64_encode_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode); +RZ_IPI RzCmdStatus rz_cmd_base64_decode_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode); RZ_IPI int rz_cmd_print(void *data, const char *input); RZ_IPI RzCmdStatus rz_project_save_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_project_open_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..f1fbca6e307 100644 --- a/librz/core/cmd_descs/cmd_print.yaml +++ b/librz/core/cmd_descs/cmd_print.yaml @@ -429,3 +429,18 @@ commands: - name: type type: RZ_CMD_ARG_TYPE_NUM optional: true + - name: p6 + summary: Base64 decoding/encoding + subcommands: + - name: p6e + summary: Base64 encoding + cname: cmd_base64_encode + modes: + - RZ_OUTPUT_MODE_STANDARD + args: [] + - name: p6d + summary: Base64 decoding + cname: cmd_base64_decode + modes: + - RZ_OUTPUT_MODE_STANDARD + args: [] \ No newline at end of file diff --git a/test/db/cmd/cmd_p6 b/test/db/cmd/cmd_p6 new file mode 100644 index 00000000000..4f5fd0d4b15 --- /dev/null +++ b/test/db/cmd/cmd_p6 @@ -0,0 +1,21 @@ +NAME=p6 base64 decode +FILE== +CMDS=< Date: Tue, 19 Jul 2022 10:54:50 +0800 Subject: [PATCH 2/3] fixup! Port command p6 to new shell --- librz/core/cmd/cmd_print.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index 3e90f5c4cec..1e76b0f0be3 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -7331,8 +7331,7 @@ RZ_IPI RzCmdStatus rz_cmd_disassemble_summarize_block_handler(RzCore *core, int } RZ_IPI RzCmdStatus rz_cmd_base64_encode_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) { - ut32 malen = (core->blocksize * 4) + 1; - ut8 *buf = malloc(malen); + ut8 *buf = calloc(1, (core->blocksize * 4) + 1); if (!buf) { RZ_LOG_ERROR("Fail to allocate memory\n"); @@ -7345,14 +7344,12 @@ RZ_IPI RzCmdStatus rz_cmd_base64_encode_handler(RzCore *core, int argc, const ch } RZ_IPI RzCmdStatus rz_cmd_base64_decode_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) { - ut32 malen = (core->blocksize * 4) + 1; - ut8 *buf = malloc(malen); + ut8 *buf = calloc(1, (core->blocksize * 4) + 1); if (!buf) { RZ_LOG_ERROR("Fail to allocate memory\n"); return RZ_CMD_STATUS_ERROR; } - memset(buf, 0, malen); if (rz_base64_decode(buf, (const char *)core->block, core->blocksize) < 0) { RZ_LOG_ERROR("rz_base64_decode: invalid stream\n"); rz_free(buf); From 1d10aa592c4aab171c9a783c5a103ae89232bb3f Mon Sep 17 00:00:00 2001 From: Peiwei Hu Date: Tue, 19 Jul 2022 14:59:22 +0800 Subject: [PATCH 3/3] fixup! fixup! Port command p6 to new shell --- librz/core/cmd/cmd_print.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index 1e76b0f0be3..aa87916b172 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -7331,31 +7331,23 @@ RZ_IPI RzCmdStatus rz_cmd_disassemble_summarize_block_handler(RzCore *core, int } RZ_IPI RzCmdStatus rz_cmd_base64_encode_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) { - ut8 *buf = calloc(1, (core->blocksize * 4) + 1); - + char *buf = rz_base64_encode_dyn((const unsigned char *)core->block, core->blocksize); if (!buf) { - RZ_LOG_ERROR("Fail to allocate memory\n"); + RZ_LOG_ERROR("rz_base64_encode_dyn: error\n"); return RZ_CMD_STATUS_ERROR; } - rz_base64_encode((char *)buf, (const unsigned char *)core->block, core->blocksize); rz_cons_println((const char *)buf); - rz_free(buf); + free(buf); return RZ_CMD_STATUS_OK; } RZ_IPI RzCmdStatus rz_cmd_base64_decode_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) { - ut8 *buf = calloc(1, (core->blocksize * 4) + 1); - + ut8 *buf = rz_base64_decode_dyn((const char *)core->block, core->blocksize); if (!buf) { - RZ_LOG_ERROR("Fail to allocate memory\n"); - return RZ_CMD_STATUS_ERROR; - } - if (rz_base64_decode(buf, (const char *)core->block, core->blocksize) < 0) { - RZ_LOG_ERROR("rz_base64_decode: invalid stream\n"); - rz_free(buf); + RZ_LOG_ERROR("rz_base64_decode_dyn: error\n"); return RZ_CMD_STATUS_ERROR; } rz_cons_println((const char *)buf); - rz_free(buf); + free(buf); return RZ_CMD_STATUS_OK; } \ No newline at end of file