Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port command p6 to new shell #2819

Merged
merged 3 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 30 additions & 36 deletions librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -7365,3 +7329,33 @@ 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) {
ut8 *buf = calloc(1, (core->blocksize * 4) + 1);

PeiweiHu marked this conversation as resolved.
Show resolved Hide resolved
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);
PeiweiHu marked this conversation as resolved.
Show resolved Hide resolved
rz_cons_println((const char *)buf);
rz_free(buf);
PeiweiHu marked this conversation as resolved.
Show resolved Hide resolved
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);

PeiweiHu marked this conversation as resolved.
Show resolved Hide resolved
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) {
PeiweiHu marked this conversation as resolved.
Show resolved Hide resolved
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);
PeiweiHu marked this conversation as resolved.
Show resolved Hide resolved
return RZ_CMD_STATUS_OK;
}
27 changes: 27 additions & 0 deletions librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions librz/core/cmd_descs/cmd_descs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions librz/core/cmd_descs/cmd_print.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
21 changes: 21 additions & 0 deletions test/db/cmd/cmd_p6
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
NAME=p6 base64 decode
FILE==
CMDS=<<EOF
w cml6aW4=
p6d @! 8
EOF
EXPECT=<<EOF
rizin
EOF
RUN

NAME=p6 base64 encode
FILE==
CMDS=<<EOF
w rizin
p6e @! 5
EOF
EXPECT=<<EOF
cml6aW4=
EOF
RUN