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 pa to new shell #2824

Merged
merged 4 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
202 changes: 70 additions & 132 deletions librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -776,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@#";
Expand Down Expand Up @@ -4694,6 +4668,76 @@ 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please print an error message as well. Here and in the other commands. These are useful to users so they get some feedback if things go wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rz_core_hex_of_assembly will output the error message when buf == NULL . The user can get feedback. The same goes for other commands.

}
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_print(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) {
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);
free(buf);
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) {
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;
}

RZ_IPI int rz_cmd_print(void *data, const char *input) {
RzCore *core = (RzCore *)data;
st64 l;
Expand Down Expand Up @@ -4848,109 +4892,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);
PeiweiHu marked this conversation as resolved.
Show resolved Hide resolved
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");
Expand Down Expand Up @@ -5047,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,
Expand Down
97 changes: 97 additions & 0 deletions librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -465,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];
Expand Down Expand Up @@ -10417,6 +10422,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_STRING,
.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_STRING,
.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=<true|false>", .arg_str = NULL, .comment = "Change endianness for pch, pcw and pcd commands" },
{ .text = "pc @! <n>", .arg_str = NULL, .comment = "Change the N of bytes (i.e. block size)." },
Expand Down Expand Up @@ -10943,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",
};
Expand Down Expand Up @@ -16175,6 +16258,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);
Expand Down Expand Up @@ -16295,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);
Expand Down
5 changes: 5 additions & 0 deletions librz/core/cmd_descs/cmd_descs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -800,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);
Expand Down
Loading