Skip to content

Commit

Permalink
Expand pc commands to accept size argument (#3738)
Browse files Browse the repository at this point in the history
  • Loading branch information
haruInDisguise authored Aug 10, 2023
1 parent 4ffdf27 commit e3617f7
Show file tree
Hide file tree
Showing 5 changed files with 394 additions and 48 deletions.
23 changes: 21 additions & 2 deletions librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -3600,7 +3600,16 @@ RZ_IPI RzCmdStatus rz_print_hexdump_oct_handler(RzCore *core, int argc, const ch

#define CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(name, type) \
RZ_IPI RzCmdStatus name(RzCore *core, int argc, const char **argv) { \
char *code = rz_lang_byte_array(core->block, core->blocksize, type); \
const int size = argc > 1 ? rz_num_math(core->num, argv[1]) : core->blocksize; \
if (size > core->blocksize_max) { \
RZ_LOG_ERROR("Size exceeds max size (%u)\n", core->blocksize_max); \
return RZ_CMD_STATUS_ERROR; \
} \
if (size <= 0) { \
RZ_LOG_ERROR("Size must be greater 0"); \
return RZ_CMD_STATUS_ERROR; \
} \
char *code = rz_lang_byte_array(core->block, size, type); \
if (RZ_STR_ISNOTEMPTY(code)) { \
rz_cons_println(code); \
} \
Expand All @@ -3620,6 +3629,7 @@ RZ_IPI RzCmdStatus rz_print_hexdump_oct_handler(RzCore *core, int argc, const ch
free(code); \
return result; \
}

CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_rizin_handler, RZ_LANG_BYTE_ARRAY_RIZIN);
CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_asm_handler, RZ_LANG_BYTE_ARRAY_ASM);
CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_bash_handler, RZ_LANG_BYTE_ARRAY_BASH);
Expand All @@ -3642,7 +3652,16 @@ CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_yara_handler, RZ_LAN

RZ_IPI RzCmdStatus rz_cmd_print_byte_array_with_inst_handler(RzCore *core, int argc, const char **argv) {
rz_core_block_read(core);
char *code = rz_core_print_bytes_with_inst(core, core->block, core->offset, (int)core->blocksize);
const int size = argc > 1 ? rz_num_math(core->num, argv[1]) : core->blocksize;
if (size > core->blocksize_max) {
RZ_LOG_ERROR("Size exceeds max size (%u)\n", core->blocksize_max);
return RZ_CMD_STATUS_ERROR;
}
if (size <= 0) {
RZ_LOG_ERROR("Size must be greater 0\n");
return RZ_CMD_STATUS_ERROR;
}
char *code = rz_core_print_bytes_with_inst(core, core->block, core->offset, size);
if (!code) {
return RZ_CMD_STATUS_ERROR;
}
Expand Down
150 changes: 135 additions & 15 deletions librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,21 @@ 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_print_byte_array_c_cpp_bytes_args[2];
static const RzCmdDescArg cmd_print_byte_array_asm_args[2];
static const RzCmdDescArg cmd_print_byte_array_with_inst_args[2];
static const RzCmdDescArg cmd_print_byte_array_bash_args[2];
static const RzCmdDescArg cmd_print_byte_array_golang_args[2];
static const RzCmdDescArg cmd_print_byte_array_java_args[2];
static const RzCmdDescArg cmd_print_byte_array_json_args[2];
static const RzCmdDescArg cmd_print_byte_array_kotlin_args[2];
static const RzCmdDescArg cmd_print_byte_array_nodejs_args[2];
static const RzCmdDescArg cmd_print_byte_array_objc_args[2];
static const RzCmdDescArg cmd_print_byte_array_python_args[2];
static const RzCmdDescArg cmd_print_byte_array_rust_args[2];
static const RzCmdDescArg cmd_print_byte_array_swift_args[2];
static const RzCmdDescArg cmd_print_byte_array_yara_args[2];
static const RzCmdDescArg cmd_print_byte_array_rizin_args[2];
static const RzCmdDescArg cmd_disassembly_n_bytes_args[2];
static const RzCmdDescArg print_columns_disassembly_args[2];
static const RzCmdDescArg print_columns_debug_args[2];
Expand Down Expand Up @@ -12542,8 +12557,8 @@ static const RzCmdDescDetailEntry cmd_print_byte_array_Useful_space_modifiers_de

static const RzCmdDescDetailEntry cmd_print_byte_array_Example_space_of_space_usages_detail_entries[] = {
{ .text = "pch @! 64 @e:cfg.bigendian=true", .arg_str = NULL, .comment = "Generate a C 32 bits array in big endian format, using 64 bytes" },
{ .text = "pcp @! 1024", .arg_str = NULL, .comment = "Generate a Python byte array of size 1024" },
{ .text = "pcj @! 10", .arg_str = NULL, .comment = "Generate a JSON bytes array of size 10" },
{ .text = "pcp 1024", .arg_str = NULL, .comment = "Generate a Python byte array of size 1024" },
{ .text = "pcj 10", .arg_str = NULL, .comment = "Generate a JSON bytes array of size 10" },
{ 0 },
};
static const RzCmdDescDetail cmd_print_byte_array_details[] = {
Expand All @@ -12556,6 +12571,13 @@ static const RzCmdDescHelp cmd_print_byte_array_help = {
.details = cmd_print_byte_array_details,
};
static const RzCmdDescArg cmd_print_byte_array_c_cpp_bytes_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_c_cpp_bytes_help = {
Expand Down Expand Up @@ -12588,6 +12610,13 @@ static const RzCmdDescHelp cmd_print_byte_array_c_cpp_double_word_help = {
};

static const RzCmdDescArg cmd_print_byte_array_asm_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_asm_help = {
Expand All @@ -12596,14 +12625,28 @@ static const RzCmdDescHelp cmd_print_byte_array_asm_help = {
};

static const RzCmdDescArg cmd_print_byte_array_with_inst_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_with_inst_help = {
.summary = "Generate a byte array in GAS assembly with instructions in comments",
.summary = "Generate a byte array in GAS assembly with instructions in comments.",
.args = cmd_print_byte_array_with_inst_args,
};

static const RzCmdDescArg cmd_print_byte_array_bash_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_bash_help = {
Expand All @@ -12612,6 +12655,13 @@ static const RzCmdDescHelp cmd_print_byte_array_bash_help = {
};

static const RzCmdDescArg cmd_print_byte_array_golang_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_golang_help = {
Expand All @@ -12620,6 +12670,13 @@ static const RzCmdDescHelp cmd_print_byte_array_golang_help = {
};

static const RzCmdDescArg cmd_print_byte_array_java_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_java_help = {
Expand All @@ -12628,6 +12685,13 @@ static const RzCmdDescHelp cmd_print_byte_array_java_help = {
};

static const RzCmdDescArg cmd_print_byte_array_json_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_json_help = {
Expand All @@ -12636,6 +12700,13 @@ static const RzCmdDescHelp cmd_print_byte_array_json_help = {
};

static const RzCmdDescArg cmd_print_byte_array_kotlin_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_kotlin_help = {
Expand All @@ -12644,6 +12715,13 @@ static const RzCmdDescHelp cmd_print_byte_array_kotlin_help = {
};

static const RzCmdDescArg cmd_print_byte_array_nodejs_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_nodejs_help = {
Expand All @@ -12652,6 +12730,13 @@ static const RzCmdDescHelp cmd_print_byte_array_nodejs_help = {
};

static const RzCmdDescArg cmd_print_byte_array_objc_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_objc_help = {
Expand All @@ -12660,6 +12745,13 @@ static const RzCmdDescHelp cmd_print_byte_array_objc_help = {
};

static const RzCmdDescArg cmd_print_byte_array_python_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_python_help = {
Expand All @@ -12668,6 +12760,13 @@ static const RzCmdDescHelp cmd_print_byte_array_python_help = {
};

static const RzCmdDescArg cmd_print_byte_array_rust_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_rust_help = {
Expand All @@ -12676,6 +12775,13 @@ static const RzCmdDescHelp cmd_print_byte_array_rust_help = {
};

static const RzCmdDescArg cmd_print_byte_array_swift_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_swift_help = {
Expand All @@ -12684,6 +12790,13 @@ static const RzCmdDescHelp cmd_print_byte_array_swift_help = {
};

static const RzCmdDescArg cmd_print_byte_array_yara_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_yara_help = {
Expand All @@ -12692,6 +12805,13 @@ static const RzCmdDescHelp cmd_print_byte_array_yara_help = {
};

static const RzCmdDescArg cmd_print_byte_array_rizin_args[] = {
{
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp cmd_print_byte_array_rizin_help = {
Expand Down Expand Up @@ -12719,7 +12839,7 @@ static const RzCmdDescHelp pC_help = {
};
static const RzCmdDescArg print_columns_disassembly_args[] = {
{
.name = "N",
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,
Expand All @@ -12728,13 +12848,13 @@ static const RzCmdDescArg print_columns_disassembly_args[] = {
{ 0 },
};
static const RzCmdDescHelp print_columns_disassembly_help = {
.summary = "Print <N> lines of instructions disassembly in columns",
.summary = "Print <len> lines of instructions disassembly in columns",
.args = print_columns_disassembly_args,
};

static const RzCmdDescArg print_columns_debug_args[] = {
{
.name = "N",
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,
Expand All @@ -12743,13 +12863,13 @@ static const RzCmdDescArg print_columns_debug_args[] = {
{ 0 },
};
static const RzCmdDescHelp print_columns_debug_help = {
.summary = "Print <N> lines of the debug registers and stack in columns",
.summary = "Print <len> lines of the debug registers and stack in columns",
.args = print_columns_debug_args,
};

static const RzCmdDescArg print_columns_hex_annotated_args[] = {
{
.name = "N",
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,
Expand All @@ -12758,13 +12878,13 @@ static const RzCmdDescArg print_columns_hex_annotated_args[] = {
{ 0 },
};
static const RzCmdDescHelp print_columns_hex_annotated_help = {
.summary = "Print <N> lines of annotated hexdump in columns",
.summary = "Print <len> lines of annotated hexdump in columns",
.args = print_columns_hex_annotated_args,
};

static const RzCmdDescArg print_columns_hex_op_colored_args[] = {
{
.name = "N",
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,
Expand All @@ -12773,13 +12893,13 @@ static const RzCmdDescArg print_columns_hex_op_colored_args[] = {
{ 0 },
};
static const RzCmdDescHelp print_columns_hex_op_colored_help = {
.summary = "Print <N> lines of op analysis color map in columns",
.summary = "Print <len> lines of op analysis color map in columns",
.args = print_columns_hex_op_colored_args,
};

static const RzCmdDescArg print_columns_hex_args[] = {
{
.name = "N",
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,
Expand All @@ -12788,13 +12908,13 @@ static const RzCmdDescArg print_columns_hex_args[] = {
{ 0 },
};
static const RzCmdDescHelp print_columns_hex_help = {
.summary = "Print <N> lines of hexdump in columns",
.summary = "Print <len> lines of hexdump in columns",
.args = print_columns_hex_args,
};

static const RzCmdDescArg print_columns_hex_words_args[] = {
{
.name = "N",
.name = "len",
.type = RZ_CMD_ARG_TYPE_RZNUM,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,
Expand All @@ -12803,7 +12923,7 @@ static const RzCmdDescArg print_columns_hex_words_args[] = {
{ 0 },
};
static const RzCmdDescHelp print_columns_hex_words_help = {
.summary = "Print <N> lines of 4-byte integer hexdump in columns",
.summary = "Print <len> lines of 4-byte integer hexdump in columns",
.args = print_columns_hex_words_args,
};

Expand Down
Loading

0 comments on commit e3617f7

Please sign in to comment.