From c30d10c7c190534768413f538baf3cea59e5654e Mon Sep 17 00:00:00 2001 From: wargio Date: Thu, 24 Mar 2022 16:20:52 +0100 Subject: [PATCH 1/8] Added rz_lang_byte_array and removed rz_print_code + refactoring cmds --- librz/core/cmd/cmd_print.c | 128 +++++- librz/core/cmd_descs/cmd_descs.c | 326 ++++++++++++++++ librz/core/cmd_descs/cmd_descs.h | 18 + librz/core/cmd_descs/cmd_print.yaml | 130 +++++++ librz/include/meson.build | 3 +- librz/include/rz_util.h | 1 + librz/include/rz_util/rz_lang_byte_array.h | 36 ++ librz/include/rz_util/rz_print.h | 1 - librz/main/rz-gg.c | 17 +- librz/util/lang_byte_array.c | 316 +++++++++++++++ librz/util/meson.build | 2 +- librz/util/print_code.c | 275 ------------- test/db/cmd/cmd_0 | 75 +++- test/db/cmd/cmd_pc | 429 ++++++++------------- 14 files changed, 1208 insertions(+), 549 deletions(-) create mode 100644 librz/include/rz_util/rz_lang_byte_array.h create mode 100644 librz/util/lang_byte_array.c delete mode 100644 librz/util/print_code.c diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index 8b21d822be4..a38ed2effee 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -6124,7 +6124,76 @@ RZ_IPI int rz_cmd_print(void *data, const char *input) { } rz_cons_printf(".equ shellcode_len, %d\n", len); } else { - rz_print_code(core->print, core->offset, core->block, len, input[1]); + char *str = NULL; + bool big_endian = rz_config_get_b(core->config, "cfg.bigendian"); + // rz_print_code(core->print, core->offset, core->block, len, input[1]); + switch (input[1]) { + case '*': // "pc*" // rizin commands + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_RIZIN); + break; + case 'A': // "pcA" // C bytes with asm + // delete me + break; + case 'i': // "pci" // same as pcA + // delete me + break; + case 'a': // "pca" // GAS asm + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_ASM); + break; + case 'S': // "pcS" // bash shellscript + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_BASH); + break; + case 'J': // "pcJ" // nodejs + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_NODEJS); + break; + case 'G': // "pcG" // golang + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_GOLANG); + break; + case 'k': // "pck" kotlin + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_KOTLIN); + break; + case 'z': // "pcz" // swift + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_SWIFT); + break; + case 'r': // "pcr" // Rust + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_RUST); + break; + case 'o': // "pco" // Objective-C + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_OBJECTIVE_C); + break; + case 'v': // "pcv" // java + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_JAVA); + break; + case 'V': // "pcV" // vlang.io + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_VLANG); + break; + case 'y': // "pcy" // yara + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_YARA); + break; + case 'j': // "pcj" // json + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_JSON); + break; + case 'P': // "pcP" // python + case 'p': // "pcp" // python + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_PYTHON); + break; + case 'h': // "pch" // C half words with asm + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, big_endian ? RZ_LANG_BYTE_ARRAY_C_CPP_HALFWORDS_BE : RZ_LANG_BYTE_ARRAY_C_CPP_HALFWORDS_LE); + break; + case 'w': // "pcw" // C words with asm + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, big_endian ? RZ_LANG_BYTE_ARRAY_C_CPP_WORDS_BE : RZ_LANG_BYTE_ARRAY_C_CPP_WORDS_LE); + break; + case 'd': // "pcd" // C double-words with asm + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, big_endian ? RZ_LANG_BYTE_ARRAY_C_CPP_DOUBLEWORDS_BE : RZ_LANG_BYTE_ARRAY_C_CPP_DOUBLEWORDS_LE); + break; + default: // "pc" // C bytes + str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_C_CPP_BYTES); + break; + } + if (str) { + rz_cons_println(str); + free(str); + } } } break; @@ -7098,3 +7167,60 @@ RZ_IPI RzCmdStatus rz_print_utf32be_handler(RzCore *core, int argc, const char * } return RZ_CMD_STATUS_OK; } + +#define CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(name, type) \ + RZ_IPI RzCmdStatus name(RzCore *core, int argc, const char **argv) { \ + ut64 oldsize = core->blocksize; \ + ut64 len = argc == 2 ? rz_num_math(core->num, argv[1]) : oldsize; \ + if (len > oldsize) { \ + rz_core_block_size(core, len); \ + } \ + char *code = rz_lang_byte_array(core->block, len, type); \ + if (RZ_STR_ISNOTEMPTY(code)) { \ + rz_cons_println(code); \ + free(code); \ + } \ + if (len > oldsize) { \ + rz_core_block_size(core, oldsize); \ + } \ + return code ? RZ_CMD_STATUS_OK : RZ_CMD_STATUS_ERROR; \ + } + +#define CMD_PRINT_BYTE_ARRAY_HANDLER_ENDIAN(name, type) \ + RZ_IPI RzCmdStatus name(RzCore *core, int argc, const char **argv) { \ + ut64 oldsize = core->blocksize; \ + bool big_endian = rz_config_get_b(core->config, "cfg.bigendian"); \ + ut64 len = argc == 2 ? rz_num_math(core->num, argv[1]) : oldsize; \ + if (len > oldsize) { \ + rz_core_block_size(core, len); \ + } \ + char *code = rz_lang_byte_array(core->block, len, big_endian ? type##_BE : type##_LE); \ + if (RZ_STR_ISNOTEMPTY(code)) { \ + rz_cons_println(code); \ + free(code); \ + } \ + if (len > oldsize) { \ + rz_core_block_size(core, oldsize); \ + } \ + return code ? RZ_CMD_STATUS_OK : RZ_CMD_STATUS_ERROR; \ + } +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); +CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_c_cpp_bytes_handler, RZ_LANG_BYTE_ARRAY_C_CPP_BYTES); +CMD_PRINT_BYTE_ARRAY_HANDLER_ENDIAN(rz_cmd_print_byte_array_c_cpp_half_word_handler, RZ_LANG_BYTE_ARRAY_C_CPP_HALFWORDS); +CMD_PRINT_BYTE_ARRAY_HANDLER_ENDIAN(rz_cmd_print_byte_array_c_cpp_word_handler, RZ_LANG_BYTE_ARRAY_C_CPP_WORDS); +CMD_PRINT_BYTE_ARRAY_HANDLER_ENDIAN(rz_cmd_print_byte_array_c_cpp_double_word_handler, RZ_LANG_BYTE_ARRAY_C_CPP_DOUBLEWORDS); +CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_golang_handler, RZ_LANG_BYTE_ARRAY_GOLANG); +CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_java_handler, RZ_LANG_BYTE_ARRAY_JAVA); +CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_json_handler, RZ_LANG_BYTE_ARRAY_JSON); +CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_kotlin_handler, RZ_LANG_BYTE_ARRAY_KOTLIN); +CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_nodejs_handler, RZ_LANG_BYTE_ARRAY_NODEJS); +CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_objc_handler, RZ_LANG_BYTE_ARRAY_OBJECTIVE_C); +CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_python_handler, RZ_LANG_BYTE_ARRAY_PYTHON); +CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_rust_handler, RZ_LANG_BYTE_ARRAY_RUST); +CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_swift_handler, RZ_LANG_BYTE_ARRAY_SWIFT); +CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_vlang_handler, RZ_LANG_BYTE_ARRAY_VLANG); +CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_yara_handler, RZ_LANG_BYTE_ARRAY_YARA); +#undef CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL +#undef CMD_PRINT_BYTE_ARRAY_HANDLER_ENDIAN diff --git a/librz/core/cmd_descs/cmd_descs.c b/librz/core/cmd_descs/cmd_descs.c index 27f62722f08..b775fc04875 100644 --- a/librz/core/cmd_descs/cmd_descs.c +++ b/librz/core/cmd_descs/cmd_descs.c @@ -400,6 +400,24 @@ 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 cmd_print_byte_array_c_cpp_bytes_args[2]; +static const RzCmdDescArg cmd_print_byte_array_c_cpp_half_word_args[2]; +static const RzCmdDescArg cmd_print_byte_array_c_cpp_word_args[2]; +static const RzCmdDescArg cmd_print_byte_array_c_cpp_double_word_args[2]; +static const RzCmdDescArg cmd_print_byte_array_asm_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_vlang_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_print_gadget_add_args[6]; static const RzCmdDescArg cmd_print_gadget_move_args[6]; static const RzCmdDescArg cmd_print_msg_digest_args[2]; @@ -9108,6 +9126,261 @@ static const RzCmdDescHelp open_exchange_help = { static const RzCmdDescHelp cmd_print_help = { .summary = "Print commands", }; +static const RzCmdDescHelp cmd_print_byte_array_help = { + .summary = "Print bytes as code byte arrays.", +}; +static const RzCmdDescArg cmd_print_byte_array_c_cpp_bytes_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_c_cpp_bytes_help = { + .summary = "Print bytes as C/C++ byte array.", + .args = cmd_print_byte_array_c_cpp_bytes_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_c_cpp_half_word_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_c_cpp_half_word_help = { + .summary = "Print bytes as C/C++ 16 bits array (endianness follows cfg.bigendian).", + .args = cmd_print_byte_array_c_cpp_half_word_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_c_cpp_word_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_c_cpp_word_help = { + .summary = "Print bytes as C/C++ 32 bits array (endianness follows cfg.bigendian).", + .args = cmd_print_byte_array_c_cpp_word_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_c_cpp_double_word_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_c_cpp_double_word_help = { + .summary = "Print bytes as C/C++ 64 bits array (endianness follows cfg.bigendian).", + .args = cmd_print_byte_array_c_cpp_double_word_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_asm_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_asm_help = { + .summary = "Print bytes as assembly.", + .args = cmd_print_byte_array_asm_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_bash_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_bash_help = { + .summary = "Print bytes as a bash command.", + .args = cmd_print_byte_array_bash_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_golang_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_golang_help = { + .summary = "Print bytes as golang byte array.", + .args = cmd_print_byte_array_golang_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_java_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_java_help = { + .summary = "Print bytes as Java byte array.", + .args = cmd_print_byte_array_java_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_json_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_json_help = { + .summary = "Print bytes as json byte array.", + .args = cmd_print_byte_array_json_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_kotlin_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_kotlin_help = { + .summary = "Print bytes as Kotlin byte array.", + .args = cmd_print_byte_array_kotlin_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_nodejs_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_nodejs_help = { + .summary = "Print bytes as NodeJS buffer.", + .args = cmd_print_byte_array_nodejs_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_objc_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_objc_help = { + .summary = "Print bytes as Objective-C/C++ byte array.", + .args = cmd_print_byte_array_objc_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_python_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_python_help = { + .summary = "Print bytes as Python byte array.", + .args = cmd_print_byte_array_python_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_rust_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_rust_help = { + .summary = "Print bytes as Rust byte array.", + .args = cmd_print_byte_array_rust_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_swift_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_swift_help = { + .summary = "Print bytes as Swift byte array.", + .args = cmd_print_byte_array_swift_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_vlang_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_vlang_help = { + .summary = "Print bytes as Vlang byte array.", + .args = cmd_print_byte_array_vlang_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_yara_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_yara_help = { + .summary = "Print bytes as Yara byte array.", + .args = cmd_print_byte_array_yara_args, +}; + +static const RzCmdDescArg cmd_print_byte_array_rizin_args[] = { + { + .name = "n_bytes", + .type = RZ_CMD_ARG_TYPE_NUM, + .optional = true, + + }, + { 0 }, +}; +static const RzCmdDescHelp cmd_print_byte_array_rizin_help = { + .summary = "Print bytes as rizin command.", + .args = cmd_print_byte_array_rizin_args, +}; + static const RzCmdDescHelp cmd_print_gadget_help = { .summary = "Print gadgets", }; @@ -14569,6 +14842,59 @@ 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 *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); + rz_warn_if_fail(cmd_print_byte_array_c_cpp_half_word_cd); + + RzCmdDesc *cmd_print_byte_array_c_cpp_word_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcw", rz_cmd_print_byte_array_c_cpp_word_handler, &cmd_print_byte_array_c_cpp_word_help); + rz_warn_if_fail(cmd_print_byte_array_c_cpp_word_cd); + + RzCmdDesc *cmd_print_byte_array_c_cpp_double_word_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcd", rz_cmd_print_byte_array_c_cpp_double_word_handler, &cmd_print_byte_array_c_cpp_double_word_help); + rz_warn_if_fail(cmd_print_byte_array_c_cpp_double_word_cd); + + RzCmdDesc *cmd_print_byte_array_asm_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pca", rz_cmd_print_byte_array_asm_handler, &cmd_print_byte_array_asm_help); + rz_warn_if_fail(cmd_print_byte_array_asm_cd); + + RzCmdDesc *cmd_print_byte_array_bash_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcb", rz_cmd_print_byte_array_bash_handler, &cmd_print_byte_array_bash_help); + rz_warn_if_fail(cmd_print_byte_array_bash_cd); + + RzCmdDesc *cmd_print_byte_array_golang_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcg", rz_cmd_print_byte_array_golang_handler, &cmd_print_byte_array_golang_help); + rz_warn_if_fail(cmd_print_byte_array_golang_cd); + + RzCmdDesc *cmd_print_byte_array_java_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcJ", rz_cmd_print_byte_array_java_handler, &cmd_print_byte_array_java_help); + rz_warn_if_fail(cmd_print_byte_array_java_cd); + + RzCmdDesc *cmd_print_byte_array_json_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcj", rz_cmd_print_byte_array_json_handler, &cmd_print_byte_array_json_help); + rz_warn_if_fail(cmd_print_byte_array_json_cd); + + RzCmdDesc *cmd_print_byte_array_kotlin_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pck", rz_cmd_print_byte_array_kotlin_handler, &cmd_print_byte_array_kotlin_help); + rz_warn_if_fail(cmd_print_byte_array_kotlin_cd); + + RzCmdDesc *cmd_print_byte_array_nodejs_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcn", rz_cmd_print_byte_array_nodejs_handler, &cmd_print_byte_array_nodejs_help); + rz_warn_if_fail(cmd_print_byte_array_nodejs_cd); + + RzCmdDesc *cmd_print_byte_array_objc_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pco", rz_cmd_print_byte_array_objc_handler, &cmd_print_byte_array_objc_help); + rz_warn_if_fail(cmd_print_byte_array_objc_cd); + + RzCmdDesc *cmd_print_byte_array_python_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcp", rz_cmd_print_byte_array_python_handler, &cmd_print_byte_array_python_help); + rz_warn_if_fail(cmd_print_byte_array_python_cd); + + RzCmdDesc *cmd_print_byte_array_rust_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcr", rz_cmd_print_byte_array_rust_handler, &cmd_print_byte_array_rust_help); + rz_warn_if_fail(cmd_print_byte_array_rust_cd); + + RzCmdDesc *cmd_print_byte_array_swift_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcs", rz_cmd_print_byte_array_swift_handler, &cmd_print_byte_array_swift_help); + rz_warn_if_fail(cmd_print_byte_array_swift_cd); + + RzCmdDesc *cmd_print_byte_array_vlang_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcv", rz_cmd_print_byte_array_vlang_handler, &cmd_print_byte_array_vlang_help); + rz_warn_if_fail(cmd_print_byte_array_vlang_cd); + + RzCmdDesc *cmd_print_byte_array_yara_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcy", rz_cmd_print_byte_array_yara_handler, &cmd_print_byte_array_yara_help); + rz_warn_if_fail(cmd_print_byte_array_yara_cd); + + RzCmdDesc *cmd_print_byte_array_rizin_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pc*", rz_cmd_print_byte_array_rizin_handler, &cmd_print_byte_array_rizin_help); + rz_warn_if_fail(cmd_print_byte_array_rizin_cd); + RzCmdDesc *cmd_print_gadget_cd = rz_cmd_desc_group_new(core->rcmd, cmd_print_cd, "pg", rz_cmd_print_gadget_add_handler, &cmd_print_gadget_add_help, &cmd_print_gadget_help); rz_warn_if_fail(cmd_print_gadget_cd); RzCmdDesc *cmd_print_gadget_print_as_rizin_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_gadget_cd, "pg*", rz_cmd_print_gadget_print_as_rizin_handler, &cmd_print_gadget_print_as_rizin_help); diff --git a/librz/core/cmd_descs/cmd_descs.h b/librz/core/cmd_descs/cmd_descs.h index ccb704b2a33..cff1d30c185 100644 --- a/librz/core/cmd_descs/cmd_descs.h +++ b/librz/core/cmd_descs/cmd_descs.h @@ -657,6 +657,24 @@ 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_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); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_c_cpp_double_word_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_asm_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_bash_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_golang_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_java_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_json_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_kotlin_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_nodejs_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_objc_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_python_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_rust_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_swift_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_vlang_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_yara_handler(RzCore *core, int argc, const char **argv); +RZ_IPI RzCmdStatus rz_cmd_print_byte_array_rizin_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_gadget_add_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_gadget_print_as_rizin_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_gadget_remove_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 753e608a88b..b6997e0272f 100644 --- a/librz/core/cmd_descs/cmd_print.yaml +++ b/librz/core/cmd_descs/cmd_print.yaml @@ -3,6 +3,136 @@ --- name: cmd_print commands: + - name: pc + summary: Print bytes as code byte arrays. + cname: cmd_print_byte_array + subcommands: + - name: pc + summary: Print bytes as C/C++ byte array. + cname: cmd_print_byte_array_c_cpp_bytes + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pch + summary: Print bytes as C/C++ 16 bits array (endianness follows cfg.bigendian). + cname: cmd_print_byte_array_c_cpp_half_word + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pcw + summary: Print bytes as C/C++ 32 bits array (endianness follows cfg.bigendian). + cname: cmd_print_byte_array_c_cpp_word + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pcd + summary: Print bytes as C/C++ 64 bits array (endianness follows cfg.bigendian). + cname: cmd_print_byte_array_c_cpp_double_word + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pca + summary: Print bytes as assembly. + cname: cmd_print_byte_array_asm + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pcb + summary: Print bytes as a bash command. + cname: cmd_print_byte_array_bash + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pcg + summary: Print bytes as golang byte array. + cname: cmd_print_byte_array_golang + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pcJ + summary: Print bytes as Java byte array. + cname: cmd_print_byte_array_java + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pcj + summary: Print bytes as json byte array. + cname: cmd_print_byte_array_json + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pck + summary: Print bytes as Kotlin byte array. + cname: cmd_print_byte_array_kotlin + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pcn + summary: Print bytes as NodeJS buffer. + cname: cmd_print_byte_array_nodejs + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pco + summary: Print bytes as Objective-C/C++ byte array. + cname: cmd_print_byte_array_objc + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pcp + summary: Print bytes as Python byte array. + cname: cmd_print_byte_array_python + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pcr + summary: Print bytes as Rust byte array. + cname: cmd_print_byte_array_rust + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pcs + summary: Print bytes as Swift byte array. + cname: cmd_print_byte_array_swift + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pcv + summary: Print bytes as Vlang byte array. + cname: cmd_print_byte_array_vlang + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pcy + summary: Print bytes as Yara byte array. + cname: cmd_print_byte_array_yara + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true + - name: pc* + summary: Print bytes as rizin command. + cname: cmd_print_byte_array_rizin + args: + - name: n_bytes + type: RZ_CMD_ARG_TYPE_NUM + optional: true - name: pg summary: Print gadgets cname: cmd_print_gadget diff --git a/librz/include/meson.build b/librz/include/meson.build index 8e686f9f898..c4f1094cc7a 100644 --- a/librz/include/meson.build +++ b/librz/include/meson.build @@ -76,13 +76,14 @@ rz_util_files = [ 'rz_util/rz_intervaltree.h', 'rz_util/rz_itv.h', 'rz_util/rz_json.h', + 'rz_util/rz_lang_byte_array.h', 'rz_util/rz_log.h', 'rz_util/rz_luhn.h', 'rz_util/rz_mem.h', 'rz_util/rz_name.h', 'rz_util/rz_num.h', - 'rz_util/rz_path.h', 'rz_util/rz_panels.h', + 'rz_util/rz_path.h', 'rz_util/rz_pj.h', 'rz_util/rz_pkcs7.h', 'rz_util/rz_pool.h', diff --git a/librz/include/rz_util.h b/librz/include/rz_util.h index a843e88ed07..e8ba5dec805 100644 --- a/librz/include/rz_util.h +++ b/librz/include/rz_util.h @@ -78,6 +78,7 @@ int gettimeofday(struct timeval *p, void *tz); #include "rz_util/rz_big.h" #include "rz_util/rz_subprocess.h" #include "rz_util/rz_luhn.h" +#include "rz_util/rz_lang_byte_array.h" // requires io, core, ... #include "rz_util/rz_print.h" #ifdef __cplusplus diff --git a/librz/include/rz_util/rz_lang_byte_array.h b/librz/include/rz_util/rz_lang_byte_array.h new file mode 100644 index 00000000000..6cd6337d390 --- /dev/null +++ b/librz/include/rz_util/rz_lang_byte_array.h @@ -0,0 +1,36 @@ +// SPDX-FileCopyrightText: 2022 RizinOrg +// SPDX-FileCopyrightText: 2022 deroad +// SPDX-License-Identifier: LGPL-3.0-only + +#ifndef RZ_LANG_BYTE_ARRAY_H +#define RZ_LANG_BYTE_ARRAY_H + +#include + +typedef enum rz_lang_byte_array_type_t { + RZ_LANG_BYTE_ARRAY_RIZIN = 0, + RZ_LANG_BYTE_ARRAY_ASM, + RZ_LANG_BYTE_ARRAY_BASH, + RZ_LANG_BYTE_ARRAY_C_CPP_BYTES, + RZ_LANG_BYTE_ARRAY_C_CPP_HALFWORDS_BE, + RZ_LANG_BYTE_ARRAY_C_CPP_HALFWORDS_LE, + RZ_LANG_BYTE_ARRAY_C_CPP_WORDS_BE, + RZ_LANG_BYTE_ARRAY_C_CPP_WORDS_LE, + RZ_LANG_BYTE_ARRAY_C_CPP_DOUBLEWORDS_BE, + RZ_LANG_BYTE_ARRAY_C_CPP_DOUBLEWORDS_LE, + RZ_LANG_BYTE_ARRAY_GOLANG, + RZ_LANG_BYTE_ARRAY_JAVA, + RZ_LANG_BYTE_ARRAY_JSON, + RZ_LANG_BYTE_ARRAY_KOTLIN, + RZ_LANG_BYTE_ARRAY_NODEJS, + RZ_LANG_BYTE_ARRAY_OBJECTIVE_C, + RZ_LANG_BYTE_ARRAY_PYTHON, + RZ_LANG_BYTE_ARRAY_RUST, + RZ_LANG_BYTE_ARRAY_SWIFT, + RZ_LANG_BYTE_ARRAY_VLANG, + RZ_LANG_BYTE_ARRAY_YARA, +} RzLangByteArrayType; + +RZ_API RZ_OWN char *rz_lang_byte_array(RZ_NONNULL const ut8 *buffer, size_t size, RzLangByteArrayType type); + +#endif /* RZ_LANG_BYTE_ARRAY_H */ diff --git a/librz/include/rz_util/rz_print.h b/librz/include/rz_util/rz_print.h index 90d731f9588..aa043893f46 100644 --- a/librz/include/rz_util/rz_print.h +++ b/librz/include/rz_util/rz_print.h @@ -157,7 +157,6 @@ RZ_API void rz_print_cursor(RzPrint *p, int cur, int len, int set); RZ_API void rz_print_cursor_range(RzPrint *p, int cur, int to, int set); RZ_API int rz_print_get_cursor(RzPrint *p); RZ_API void rz_print_set_cursor(RzPrint *p, int curset, int ocursor, int cursor); -RZ_API void rz_print_code(RzPrint *p, ut64 addr, const ut8 *buf, int len, char lang); #define SEEFLAG -2 #define JSONOUTPUT -3 diff --git a/librz/main/rz-gg.c b/librz/main/rz-gg.c index 3f8307e1c8b..a94cefd3082 100644 --- a/librz/main/rz-gg.c +++ b/librz/main/rz-gg.c @@ -528,15 +528,19 @@ RZ_API int rz_main_rz_gg(int argc, const char **argv) { eprintf("No format specified\n"); goto fail; } - RzPrint *p = rz_print_new(); + char *code = NULL; ut64 tmpsz; const ut8 *tmp = rz_buf_data(b, &tmpsz); switch (*format) { case 'c': - rz_print_code(p, 0, tmp, tmpsz, 'c'); + code = rz_lang_byte_array(tmp, tmpsz, RZ_LANG_BYTE_ARRAY_C_CPP_BYTES); + printf("%s\n", code); + free(code); break; - case 'j': // JavaScript - rz_print_code(p, 0, tmp, tmpsz, 'j'); + case 'j': // json + code = rz_lang_byte_array(tmp, tmpsz, RZ_LANG_BYTE_ARRAY_JSON); + printf("%s\n", code); + free(code); break; case 'r': if (show_str) { @@ -555,7 +559,9 @@ RZ_API int rz_main_rz_gg(int argc, const char **argv) { break; case 'p': // PE if (strlen(format) >= 2 && format[1] == 'y') { // Python - rz_print_code(p, 0, tmp, tmpsz, 'p'); + code = rz_lang_byte_array(tmp, tmpsz, RZ_LANG_BYTE_ARRAY_PYTHON); + printf("%s\n", code); + free(code); } break; case 'e': // ELF @@ -566,7 +572,6 @@ RZ_API int rz_main_rz_gg(int argc, const char **argv) { eprintf("unknown executable format (%s)\n", format); goto fail; } - rz_print_free(p); } } if (fd != -1) { diff --git a/librz/util/lang_byte_array.c b/librz/util/lang_byte_array.c new file mode 100644 index 00000000000..d9e20f967b3 --- /dev/null +++ b/librz/util/lang_byte_array.c @@ -0,0 +1,316 @@ +// SPDX-FileCopyrightText: 2022 RizinOrg +// SPDX-FileCopyrightText: 2022 deroad +// SPDX-License-Identifier: LGPL-3.0-only + +#include +#define RZ_LANG_BYTE_ARRAY_TRUNK_SIZE 16 +#define RZ_LANG_BYTE_ARRAY_TRUNK_SIZE_STR "16" + +static void lang_byte_array_rizin(RzStrBuf *sb, const ut8 *buffer, size_t size) { + size_t pos = 0; + rz_strbuf_append(sb, "wx "); + for (pos = 0; pos < size; pos++) { + if (pos > 0 && !(pos % RZ_LANG_BYTE_ARRAY_TRUNK_SIZE)) { + rz_strbuf_append(sb, " ; sd +" RZ_LANG_BYTE_ARRAY_TRUNK_SIZE_STR "\nwx "); + } + rz_strbuf_appendf(sb, "%02x", buffer[pos]); + } + if (pos > RZ_LANG_BYTE_ARRAY_TRUNK_SIZE) { + rz_strbuf_appendf(sb, " ; sd -%" PFMTSZd, pos); + } +} + +static void lang_byte_array_bash(RzStrBuf *sb, const ut8 *buffer, size_t size) { + bool append = false; + rz_strbuf_append(sb, "printf \""); + for (size_t pos = 0; pos < size; pos++) { + if (pos > 0 && !(pos % RZ_LANG_BYTE_ARRAY_TRUNK_SIZE)) { + rz_strbuf_appendf(sb, "\" %s data.bin\nprintf \"", append ? ">>" : ">"); + append = true; + } + rz_strbuf_appendf(sb, "\\%03o", buffer[pos]); + } + rz_strbuf_appendf(sb, "\" %s data.bin", append ? ">>" : ">"); +} + +static void lang_byte_array_c_cpp(RzStrBuf *sb, const ut8 *buffer, size_t size, size_t n_bytes, bool big_endian) { + const char *hex_c, *hex_e; + size_t n_bits = n_bytes * 8; + size_t max_print; + ut64 value; + + switch (n_bytes) { + case 2: + hex_c = " 0x%04" PFMT64x ","; + hex_e = " 0x%04" PFMT64x "\n};"; + // ensure that is always aligned + size -= (size % n_bytes); + break; + case 4: + hex_c = " 0x%08" PFMT64x "u,"; + hex_e = " 0x%08" PFMT64x "u\n};"; + // ensure that is always aligned + size -= (size % n_bytes); + break; + case 8: + hex_c = " 0x%016" PFMT64x "ull,"; + hex_e = " 0x%016" PFMT64x "ull\n};"; + // ensure that is always aligned + size -= (size % n_bytes); + break; + default: + hex_c = " 0x%02" PFMT64x ","; + hex_e = " 0x%02" PFMT64x "\n};"; + break; + } + max_print = RZ_LANG_BYTE_ARRAY_TRUNK_SIZE / n_bytes; + + if (size < 1 && n_bytes != 1) { + rz_strbuf_appendf(sb, "// Warning: the number of available bytes is less than %" PFMTSZd, n_bytes); + return; + } + rz_strbuf_appendf(sb, "#define ARRAY_SIZE %" PFMTSZd "\nconst uint%" PFMTSZd "_t array[ARRAY_SIZE] = {\n ", size / n_bytes, n_bits); + for (size_t pos = 0, n_print = 0; pos < size; pos += n_bytes, n_print++) { + if (n_print > 0 && !(n_print % max_print)) { + rz_strbuf_append(sb, "\n "); + } + value = rz_read_ble(buffer + pos, big_endian, n_bits); + rz_strbuf_appendf(sb, (pos + n_bytes) < size ? hex_c : hex_e, value); + } +} + +static void lang_byte_array_asm(RzStrBuf *sb, const ut8 *buffer, size_t size) { + rz_strbuf_append(sb, "byte_array:"); + for (size_t pos = 0; pos < size; pos++) { + if (!(pos % RZ_LANG_BYTE_ARRAY_TRUNK_SIZE)) { + rz_strbuf_appendf(sb, "\n.byte 0x%02x", buffer[pos]); + } else { + rz_strbuf_appendf(sb, ", 0x%02x", buffer[pos]); + } + } + rz_strbuf_appendf(sb, "\n.equ byte_array_len, %" PFMTSZd, size); +} + +static void lang_byte_array_golang(RzStrBuf *sb, const ut8 *buffer, size_t size) { + rz_strbuf_append(sb, "byteArray := []byte{\n "); + for (size_t pos = 0; pos < size; pos++) { + if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) { + rz_strbuf_append(sb, "\n "); + } + rz_strbuf_appendf(sb, " 0x%02x,", buffer[pos]); + } + rz_strbuf_append(sb, "\n}"); +} + +static void lang_byte_array_java(RzStrBuf *sb, const ut8 *buffer, size_t size) { + int value = 0; + rz_strbuf_append(sb, "byte[] byteArray = new byte[] {\n "); + for (size_t pos = 0; pos < size; pos++) { + if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) { + rz_strbuf_append(sb, "\n "); + } + value = (int)(char)buffer[pos]; + rz_strbuf_appendf(sb, " %4d,", value); + } + rz_strbuf_append(sb, "\n};"); +} + +static char *lang_byte_array_json(const ut8 *buffer, size_t size) { + PJ *pj = pj_new(); + if (!pj) { + return NULL; + } + pj_a(pj); + for (size_t pos = 0; pos < size; pos++) { + pj_i(pj, buffer[pos]); + } + pj_end(pj); + return pj_drain(pj); +} + +static void lang_byte_array_kotlin(RzStrBuf *sb, const ut8 *buffer, size_t size) { + int value = 0; + rz_strbuf_append(sb, "val byteArray = byteArrayOf(\n "); + for (size_t pos = 0; pos < size; pos++) { + if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) { + rz_strbuf_append(sb, "\n "); + } + value = (int)(char)buffer[pos]; + rz_strbuf_appendf(sb, " %4d,", value); + } + rz_strbuf_append(sb, "\n);"); +} + +static void lang_byte_array_nodejs(RzStrBuf *sb, const ut8 *buffer, size_t size) { + if (size == 0) { + rz_strbuf_append(sb, "var byteArray = new Buffer('', 'base64');"); + return; + } + + char *base64 = calloc(size, 3); + if (!base64) { + return; + } + rz_base64_encode(base64, buffer, size); + rz_strbuf_appendf(sb, "var byteArray = new Buffer('%s', 'base64');", base64); + free(base64); +} + +static void lang_byte_array_objective_c_cpp(RzStrBuf *sb, const ut8 *buffer, size_t size) { + rz_strbuf_append(sb, "NSData *byteArray = [[NSData alloc] initWithBytes:{\n "); + for (size_t pos = 0; pos < size; pos++) { + if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) { + rz_strbuf_append(sb, "\n "); + } + rz_strbuf_appendf(sb, (pos + 1) < size ? " 0x%02x," : " 0x%02x\n}];", buffer[pos]); + } +} + +static void lang_byte_array_python(RzStrBuf *sb, const ut8 *buffer, size_t size) { + rz_strbuf_append(sb, "byteArray = b'"); + for (size_t pos = 0; pos < size; pos++) { + if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) { + rz_strbuf_append(sb, "'\nbyteArray += b'"); + } + rz_strbuf_appendf(sb, "\\x%02x", buffer[pos]); + } + rz_strbuf_append(sb, "'"); +} + +static void lang_byte_array_rust(RzStrBuf *sb, const ut8 *buffer, size_t size) { + rz_strbuf_appendf(sb, "let _: [u8; %" PFMTSZd "] = [\n ", size); + for (size_t pos = 0; pos < size; pos++) { + if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) { + rz_strbuf_append(sb, "\n "); + } + rz_strbuf_appendf(sb, (pos + 1) < size ? " 0x%02x," : " 0x%02x\n];", buffer[pos]); + } +} + +static void lang_byte_array_swift(RzStrBuf *sb, const ut8 *buffer, size_t size) { + rz_strbuf_append(sb, "let byteArray : [UInt8] = [\n "); + for (size_t pos = 0; pos < size; pos++) { + if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) { + rz_strbuf_append(sb, "\n "); + } + rz_strbuf_appendf(sb, (pos + 1) < size ? " 0x%02x," : " 0x%02x\n];", buffer[pos]); + } +} + +static void lang_byte_array_vlang(RzStrBuf *sb, const ut8 *buffer, size_t size) { + if (size < 1) { + rz_strbuf_append(sb, "// Warning: the number of available bytes is less than 1"); + return; + } else if (size == 1) { + rz_strbuf_appendf(sb, "byteArray := [ byte(%u) ]\n ", buffer[0]); + return; + } + rz_strbuf_appendf(sb, "byteArray := [ byte(%u)\n ", buffer[0]); + for (size_t pos = 1; pos < size; pos++) { + if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) { + rz_strbuf_append(sb, "\n "); + } + rz_strbuf_appendf(sb, (pos + 1) < size ? " %u," : " %u\n];", buffer[pos]); + } +} + +static void lang_byte_array_yara(RzStrBuf *sb, const ut8 *buffer, size_t size) { + rz_strbuf_append(sb, "$byteArray = {\n "); + for (size_t pos = 0; pos < size; pos++) { + if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) { + rz_strbuf_append(sb, "\n "); + } + rz_strbuf_appendf(sb, " %02x", buffer[pos]); + } + rz_strbuf_appendf(sb, "\n};"); +} + +/** + * \brief Generates a string containing a byte array in the specified language + * + * \param buffer The buffer to read + * \param size The buffer size + * \param type The RzLangByteArrayType type + * + * \return On success returns a string, otherwise NULL + */ +RZ_API RZ_OWN char *rz_lang_byte_array(RZ_NONNULL const ut8 *buffer, size_t size, RzLangByteArrayType type) { + rz_return_val_if_fail(buffer, NULL); + RzStrBuf sb; + rz_strbuf_init(&sb); + + if (size == 0) { + return rz_strbuf_drain_nofree(&sb); + } + + switch (type) { + case RZ_LANG_BYTE_ARRAY_RIZIN: + lang_byte_array_rizin(&sb, buffer, size); + break; + case RZ_LANG_BYTE_ARRAY_ASM: + lang_byte_array_asm(&sb, buffer, size); + break; + case RZ_LANG_BYTE_ARRAY_BASH: + lang_byte_array_bash(&sb, buffer, size); + break; + case RZ_LANG_BYTE_ARRAY_C_CPP_BYTES: + lang_byte_array_c_cpp(&sb, buffer, size, 1, false); + break; + case RZ_LANG_BYTE_ARRAY_C_CPP_HALFWORDS_BE: + lang_byte_array_c_cpp(&sb, buffer, size, 2, true); + break; + case RZ_LANG_BYTE_ARRAY_C_CPP_HALFWORDS_LE: + lang_byte_array_c_cpp(&sb, buffer, size, 2, false); + break; + case RZ_LANG_BYTE_ARRAY_C_CPP_WORDS_BE: + lang_byte_array_c_cpp(&sb, buffer, size, 4, true); + break; + case RZ_LANG_BYTE_ARRAY_C_CPP_WORDS_LE: + lang_byte_array_c_cpp(&sb, buffer, size, 4, false); + break; + case RZ_LANG_BYTE_ARRAY_C_CPP_DOUBLEWORDS_BE: + lang_byte_array_c_cpp(&sb, buffer, size, 8, true); + break; + case RZ_LANG_BYTE_ARRAY_C_CPP_DOUBLEWORDS_LE: + lang_byte_array_c_cpp(&sb, buffer, size, 8, false); + break; + case RZ_LANG_BYTE_ARRAY_GOLANG: + lang_byte_array_golang(&sb, buffer, size); + break; + case RZ_LANG_BYTE_ARRAY_JAVA: + lang_byte_array_java(&sb, buffer, size); + break; + case RZ_LANG_BYTE_ARRAY_JSON: + rz_strbuf_fini(&sb); + return lang_byte_array_json(buffer, size); + case RZ_LANG_BYTE_ARRAY_KOTLIN: + lang_byte_array_kotlin(&sb, buffer, size); + break; + case RZ_LANG_BYTE_ARRAY_NODEJS: + lang_byte_array_nodejs(&sb, buffer, size); + break; + case RZ_LANG_BYTE_ARRAY_OBJECTIVE_C: + lang_byte_array_objective_c_cpp(&sb, buffer, size); + break; + case RZ_LANG_BYTE_ARRAY_PYTHON: + lang_byte_array_python(&sb, buffer, size); + break; + case RZ_LANG_BYTE_ARRAY_RUST: + lang_byte_array_rust(&sb, buffer, size); + break; + case RZ_LANG_BYTE_ARRAY_SWIFT: + lang_byte_array_swift(&sb, buffer, size); + break; + case RZ_LANG_BYTE_ARRAY_VLANG: + lang_byte_array_vlang(&sb, buffer, size); + break; + case RZ_LANG_BYTE_ARRAY_YARA: + lang_byte_array_yara(&sb, buffer, size); + break; + default: + rz_strbuf_fini(&sb); + rz_warn_if_reached(); + return NULL; + } + return rz_strbuf_drain_nofree(&sb); +} diff --git a/librz/util/meson.build b/librz/util/meson.build index 8d21da90f4a..2c7989d7422 100644 --- a/librz/util/meson.build +++ b/librz/util/meson.build @@ -25,6 +25,7 @@ rz_util_sources = [ 'intervaltree.c', 'json_indent.c', 'json_parser.c', + 'lang_byte_array.c', 'lib.c', 'list.c', 'log.c', @@ -35,7 +36,6 @@ rz_util_sources = [ 'pj.c', 'pkcs7.c', 'print.c', - 'print_code.c', 'protobuf.c', 'punycode.c', 'queue.c', diff --git a/librz/util/print_code.c b/librz/util/print_code.c deleted file mode 100644 index 0493aa92e6d..00000000000 --- a/librz/util/print_code.c +++ /dev/null @@ -1,275 +0,0 @@ -// SPDX-FileCopyrightText: 2007-2020 pancake -// SPDX-License-Identifier: LGPL-3.0-only - -#include -#include - -static const char *bits_to_c_code_fmtstr(int bits) { - switch (bits) { - case 16: - return "0x%04" PFMT64x; - case 32: - return "0x%08" PFMT64x "U"; - case 64: - return "0x%016" PFMT64x "ULL"; - } - return "0x%02" PFMT64x; -} - -static int get_instruction_size(RzPrint *p, ut64 at) { - char *is = p->coreb.cmdstrf(p->coreb.core, "ao @ 0x%08" PFMT64x "~^size[1]", at); - int res = atoi(is); - free(is); - return res; -} - -static void print_c_instructions(RzPrint *p, ut64 addr, const ut8 *buf, int len) { - const char *fmtstr = bits_to_c_code_fmtstr(8); - - p->cb_printf("#define _BUFFER_SIZE %d\n", len); - p->cb_printf("const uint8_t buffer[_BUFFER_SIZE] = {\n"); - - const int orig_align = p->coreb.cfggeti(p->coreb.core, "asm.cmt.col") - 40; - size_t k, i = 0; - - while (!rz_print_is_interrupted() && i < len) { - ut64 at = addr + i; - int inst_size = get_instruction_size(p, at); - if (inst_size <= 0) { - // some times the instruction size is reported as zero, - // just skip the current instruction and go ahead - inst_size = 1; - } - p->cb_printf(" "); - size_t limit = RZ_MIN(i + inst_size, len); - for (k = i; k < limit; k++) { - rz_print_cursor(p, k, 1, true); - p->cb_printf(fmtstr, rz_read_ble(buf++, p->big_endian, 8)); - rz_print_cursor(p, k, 1, false); - p->cb_printf(", "); - } - size_t j = k - i; - int pad = orig_align - ((j - 1) * 6); - p->cb_printf("%*s", RZ_MAX(pad, 0), ""); - - if (j == inst_size) { - char *instr = p->coreb.cmdstrf(p->coreb.core, "pi 1 @ 0x%08" PFMT64x, at); - rz_str_trim(instr); - p->cb_printf(" /* %s */\n", instr); - free(instr); - } else { - p->cb_printf(" /* invalid */\n"); - } - i += j; - } - p->cb_printf("};\n"); -} - -static void print_c_code(RzPrint *p, ut64 addr, const ut8 *buf, int len, int ws, int w) { - size_t i; - - ws = RZ_MAX(1, RZ_MIN(ws, 8)); - int bits = ws * 8; - const char *fmtstr = bits_to_c_code_fmtstr(bits); - len /= ws; - - p->cb_printf("#define _BUFFER_SIZE %d\n", len); - p->cb_printf("const uint%d_t buffer[_BUFFER_SIZE] = {", bits); - - for (i = 0; !rz_print_is_interrupted() && i < len; i++) { - if (!(i % w)) { - p->cb_printf("\n "); - } - rz_print_cursor(p, i, 1, 1); - p->cb_printf(fmtstr, rz_read_ble(buf, p->big_endian, bits)); - if ((i + 1) < len) { - p->cb_printf(","); - - if ((i + 1) % w) { - p->cb_printf(" "); - } - } - rz_print_cursor(p, i, 1, 0); - buf += ws; - } - p->cb_printf("\n};\n"); -} - -RZ_API void rz_print_code(RzPrint *p, ut64 addr, const ut8 *buf, int len, char lang) { - rz_return_if_fail(p && buf); - int i, w = (int)(p->cols * 0.7); - if (w < 1) { - w = 1; - } - switch (lang) { - case '*': - p->cb_printf("wx "); - for (i = 0; !rz_print_is_interrupted() && i < len; i++) { - if (i && !(i % 16)) { - p->cb_printf(";sd +16\nwx "); - } - p->cb_printf("%02x", buf[i]); - } - if (i && !(i % 16)) { - p->cb_printf(";sd +16\n"); - } else { - p->cb_printf(";sd +%d\n", (i % 16)); - } - p->cb_printf("sd -%d\n", len); - break; - case 'A': // "pcA" - /* implemented in core because of disasm :( */ - break; - case 'a': // "pca" - p->cb_printf("shellcode:"); - for (i = 0; !rz_print_is_interrupted() && i < len; i++) { - if (!(i % 8)) { - p->cb_printf("\n.byte "); - } else { - p->cb_printf(", "); - } - p->cb_printf("0x%02x", buf[i]); - } - p->cb_printf("\n.equ shellcode_len, %d\n", len); - break; - case 's': // "pcs" - p->cb_printf("\""); - for (i = 0; !rz_print_is_interrupted() && i < len; i++) { - p->cb_printf("\\x%02x", buf[i]); - } - p->cb_printf("\"\n"); - break; - case 'S': // "pcS" - { - const int trunksize = 16; - for (i = 0; !rz_print_is_interrupted() && i < len; i++) { - if (!(i % trunksize)) { - p->cb_printf("printf \""); - } - p->cb_printf("\\%03o", buf[i]); - if ((i % trunksize) == (trunksize - 1)) { - p->cb_printf("\" %s bin\n", (i <= trunksize) ? ">" : ">>"); - } - } - if ((i % trunksize)) { - p->cb_printf("\" %s bin\n", (i <= trunksize) ? ">" : ">>"); - } - } break; - case 'J': { // "pcJ" - char *out = malloc(len * 3); - p->cb_printf("var buffer = new Buffer(\""); - out[0] = 0; - rz_base64_encode(out, buf, len); - p->cb_printf("%s", out); - p->cb_printf("\", 'base64');\n"); - free(out); - } break; - case 'k': // "pck" kotlin - p->cb_printf("val arr = byteArrayOfInts("); - for (i = 0; !rz_print_is_interrupted() && i < len; i++) { - rz_print_cursor(p, i, 1, 1); - p->cb_printf("0x%x%s", buf[i], (i + 1 < len) ? "," : ""); - rz_print_cursor(p, i, 1, 0); - } - p->cb_printf(")\n"); - break; - case 'z': // "pcz" // swift - p->cb_printf("let byteArray : [UInt8] = ["); - - for (i = 0; !rz_print_is_interrupted() && i < len; i++) { - rz_print_cursor(p, i, 1, 1); - p->cb_printf("0x%x%s", buf[i], (i + 1 < len) ? ", " : ""); - rz_print_cursor(p, i, 1, 0); - } - p->cb_printf("]\n"); - break; - case 'r': // "pcr" // Rust - p->cb_printf("let _: [u8; %d] = [\n", len); - for (i = 0; !rz_print_is_interrupted() && i < len; i++) { - rz_print_cursor(p, i, 1, 1); - p->cb_printf("0x%x%s", buf[i], (i + 1 < len) ? "," : ""); - rz_print_cursor(p, i, 1, 0); - } - p->cb_printf("];\n"); - break; - case 'o': // "pco" // Objective-C - p->cb_printf("NSData *endMarker = [[NSData alloc] initWithBytes:{\n"); - for (i = 0; !rz_print_is_interrupted() && i < len; i++) { - rz_print_cursor(p, i, 1, 1); - p->cb_printf("0x%x%s", buf[i], (i + 1 < len) ? "," : ""); - rz_print_cursor(p, i, 1, 0); - } - p->cb_printf("}];\n"); - break; - case 'v': // "pcv" // JaVa - p->cb_printf("byte[] ba = {"); - for (i = 0; !rz_print_is_interrupted() && i < len; i++) { - rz_print_cursor(p, i, 1, 1); - p->cb_printf("%d%s", buf[i], (i + 1 < len) ? "," : ""); - rz_print_cursor(p, i, 1, 0); - } - p->cb_printf("};\n"); - break; - case 'V': // "pcV" // vlang.io - p->cb_printf("data := [ byte(%d),\n ", buf[0]); - for (i = 1; !rz_print_is_interrupted() && i < len; i++) { - rz_print_cursor(p, i, 1, 1); - p->cb_printf("%d%s", buf[i], (i + 1 < len) ? ", " : ""); - rz_print_cursor(p, i, 1, 0); - if ((i % 10) == 0) { - p->cb_printf("\n "); - } - } - p->cb_printf("\n]\n"); - break; - case 'y': // "pcy" - p->cb_printf("$hex_%" PFMT64x " = {", addr); - for (i = 0; !rz_print_is_interrupted() && i < len; i++) { - rz_print_cursor(p, i, 1, 1); - p->cb_printf(" %02x", buf[i] & 0xff); - rz_print_cursor(p, i, 1, 0); - } - p->cb_printf(" }\n"); - break; - case 'j': { // "pcj" - PJ *pj = pj_new(); - pj_a(pj); - for (i = 0; !rz_print_is_interrupted() && i < len; i++) { - rz_print_cursor(p, i, 1, 1); - pj_i(pj, buf[i]); - rz_print_cursor(p, i, 1, 0); - } - pj_end(pj); - p->cb_printf("%s\n", pj_string(pj)); - pj_free(pj); - } break; - case 'P': - case 'p': // "pcp" "pcP" - p->cb_printf("import struct\nbuf = struct.pack (\"%dB\", *[", len); - for (i = 0; !rz_print_is_interrupted() && i < len; i++) { - if (!(i % w)) { - p->cb_printf("\n"); - } - rz_print_cursor(p, i, 1, 1); - p->cb_printf("0x%02x%s", buf[i], (i + 1 < len) ? "," : "])"); - rz_print_cursor(p, i, 1, 0); - } - p->cb_printf("\n"); - break; - case 'h': // "pch" - print_c_code(p, addr, buf, len, 2, p->cols / 2); // 9 - break; - case 'w': // "pcw" - print_c_code(p, addr, buf, len, 4, p->cols / 3); // 6); - break; - case 'i': // "pci" - print_c_instructions(p, addr, buf, len); - break; - case 'd': // "pcd" - print_c_code(p, addr, buf, len, 8, p->cols / 5); // 3); - break; - default: - print_c_code(p, addr, buf, len, 1, p->cols / 1.5); // 12); - break; - } -} diff --git a/test/db/cmd/cmd_0 b/test/db/cmd/cmd_0 index 418034709e4..bec9abb5ae9 100644 --- a/test/db/cmd/cmd_0 +++ b/test/db/cmd/cmd_0 @@ -201,9 +201,9 @@ EXPECT=< bin -printf "\307\300\260\005\100\000\110\307\301\040\005\100\000\110\307\307" >> bin -printf "\374\004\100\000\350\267\377\377\377\364\146\220\017\037\100\000" >> bin -printf "\270\027\011\140\000\125\110\055\020\011\140\000\110\203\370\016" >> bin -printf "\110\211\345\167\002\135\303\270\000\000\000\000\110\205\300\164" >> bin -printf "\364\135\277\020\011\140\000\377\340\017\037\200\000\000\000\000" >> bin -printf "\270\020\011\140\000\125\110\055\020\011\140\000\110\301\370\003" >> bin -printf "\110\211\345\110\211\302\110\301\352\077\110\001\320\110\211\306" >> bin -printf "\110\321\376\165\002\135\303\272\000\000\000\000\110\205\322\164" >> bin -printf "\364\135\277\020\011\140\000\377\342\017\037\200\000\000\000\000" >> bin -printf "\200\075\131\004\040\000\000\165\021\125\110\211\345\350\176\377" >> bin -printf "\377\377\135\306\005\106\004\040\000\001\363\303\017\037\100\000" >> bin -printf "\110\203\075\030\002\040\000\000\164\033\270\000\000\000\000\110" >> bin -printf "\205\300\164\021\125\277\360\006\140\000\110\211\345\377\320\135" >> bin -printf "\351\173\377\377\377\351\166\377\377\377\146\220\125\110\211\345" >> bin -printf "\277\304\005\100\000\350\326\376\377\377\270\000\000\000\000\135" >> bin +printf "\200\100\055\351\000\160\215\342\000\002\000\343\001\000\100\343" > data.bin +printf "\361\377\377\353\000\060\260\343\003\000\240\341\361\377" >> data.bin EOF RUN -NAME=pcJ hello-linux-x86_64 -FILE=bins/elf/analysis/hello-linux-x86_64 -CMDS=pcJ +NAME=Print bytes as golang byte array. +FILE=bins/elf/analysis/hello-arm32 +CMDS=pcg 30 EXPECT=< Date: Thu, 24 Mar 2022 17:31:58 +0100 Subject: [PATCH 2/8] Cleanup --- librz/core/cmd/cmd_print.c | 6 ------ librz/core/cmd_descs/cmd_descs.c | 2 +- librz/core/cmd_descs/cmd_print.yaml | 2 +- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index a38ed2effee..a8a682153c9 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -6131,12 +6131,6 @@ RZ_IPI int rz_cmd_print(void *data, const char *input) { case '*': // "pc*" // rizin commands str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_RIZIN); break; - case 'A': // "pcA" // C bytes with asm - // delete me - break; - case 'i': // "pci" // same as pcA - // delete me - break; case 'a': // "pca" // GAS asm str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_ASM); break; diff --git a/librz/core/cmd_descs/cmd_descs.c b/librz/core/cmd_descs/cmd_descs.c index b775fc04875..78740651234 100644 --- a/librz/core/cmd_descs/cmd_descs.c +++ b/librz/core/cmd_descs/cmd_descs.c @@ -9195,7 +9195,7 @@ static const RzCmdDescArg cmd_print_byte_array_asm_args[] = { { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_asm_help = { - .summary = "Print bytes as assembly.", + .summary = "Print bytes as GAS assembly.", .args = cmd_print_byte_array_asm_args, }; diff --git a/librz/core/cmd_descs/cmd_print.yaml b/librz/core/cmd_descs/cmd_print.yaml index b6997e0272f..78096227c7f 100644 --- a/librz/core/cmd_descs/cmd_print.yaml +++ b/librz/core/cmd_descs/cmd_print.yaml @@ -36,7 +36,7 @@ commands: type: RZ_CMD_ARG_TYPE_NUM optional: true - name: pca - summary: Print bytes as assembly. + summary: Print bytes as GAS assembly. cname: cmd_print_byte_array_asm args: - name: n_bytes From 8645dc15bf4801f40dec7755f5dfd37903c091e8 Mon Sep 17 00:00:00 2001 From: wargio Date: Thu, 24 Mar 2022 17:52:33 +0100 Subject: [PATCH 3/8] Added test for big endian --- test/db/cmd/cmd_pc | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/db/cmd/cmd_pc b/test/db/cmd/cmd_pc index 80280cc390e..03957b9590c 100644 --- a/test/db/cmd/cmd_pc +++ b/test/db/cmd/cmd_pc @@ -68,6 +68,18 @@ const uint16_t array[ARRAY_SIZE] = { EOF RUN +NAME=Print bytes as C/C++ half-word (2 bytes value) array (big endian). +FILE=bins/elf/analysis/hello-arm32 +CMDS=pch 30 @e:cfg.bigendian=true +EXPECT=< Date: Thu, 24 Mar 2022 18:15:39 +0100 Subject: [PATCH 4/8] Removed vlang and sync oldshell with newshell cmd changes --- librz/core/cmd/cmd_print.c | 16 +++++----------- librz/core/cmd_descs/cmd_print.yaml | 7 ------- librz/include/rz_util/rz_lang_byte_array.h | 1 - librz/util/lang_byte_array.c | 20 -------------------- 4 files changed, 5 insertions(+), 39 deletions(-) diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index a8a682153c9..1b54becb1ef 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -6126,7 +6126,6 @@ RZ_IPI int rz_cmd_print(void *data, const char *input) { } else { char *str = NULL; bool big_endian = rz_config_get_b(core->config, "cfg.bigendian"); - // rz_print_code(core->print, core->offset, core->block, len, input[1]); switch (input[1]) { case '*': // "pc*" // rizin commands str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_RIZIN); @@ -6134,19 +6133,19 @@ RZ_IPI int rz_cmd_print(void *data, const char *input) { case 'a': // "pca" // GAS asm str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_ASM); break; - case 'S': // "pcS" // bash shellscript + case 'b': // "pcb" // bash shellscript str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_BASH); break; - case 'J': // "pcJ" // nodejs + case 'n': // "pcn" // nodejs str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_NODEJS); break; - case 'G': // "pcG" // golang + case 'g': // "pcg" // golang str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_GOLANG); break; case 'k': // "pck" kotlin str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_KOTLIN); break; - case 'z': // "pcz" // swift + case 's': // "pcs" // swift str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_SWIFT); break; case 'r': // "pcr" // Rust @@ -6155,19 +6154,15 @@ RZ_IPI int rz_cmd_print(void *data, const char *input) { case 'o': // "pco" // Objective-C str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_OBJECTIVE_C); break; - case 'v': // "pcv" // java + case 'J': // "pcJ" // java str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_JAVA); break; - case 'V': // "pcV" // vlang.io - str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_VLANG); - break; case 'y': // "pcy" // yara str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_YARA); break; case 'j': // "pcj" // json str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_JSON); break; - case 'P': // "pcP" // python case 'p': // "pcp" // python str = rz_lang_byte_array(core->block, len < 0 ? 0 : len, RZ_LANG_BYTE_ARRAY_PYTHON); break; @@ -7214,7 +7209,6 @@ CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_objc_handler, RZ_LAN CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_python_handler, RZ_LANG_BYTE_ARRAY_PYTHON); CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_rust_handler, RZ_LANG_BYTE_ARRAY_RUST); CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_swift_handler, RZ_LANG_BYTE_ARRAY_SWIFT); -CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_vlang_handler, RZ_LANG_BYTE_ARRAY_VLANG); CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_yara_handler, RZ_LANG_BYTE_ARRAY_YARA); #undef CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL #undef CMD_PRINT_BYTE_ARRAY_HANDLER_ENDIAN diff --git a/librz/core/cmd_descs/cmd_print.yaml b/librz/core/cmd_descs/cmd_print.yaml index 78096227c7f..276e2d2bd21 100644 --- a/librz/core/cmd_descs/cmd_print.yaml +++ b/librz/core/cmd_descs/cmd_print.yaml @@ -112,13 +112,6 @@ commands: - name: n_bytes type: RZ_CMD_ARG_TYPE_NUM optional: true - - name: pcv - summary: Print bytes as Vlang byte array. - cname: cmd_print_byte_array_vlang - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true - name: pcy summary: Print bytes as Yara byte array. cname: cmd_print_byte_array_yara diff --git a/librz/include/rz_util/rz_lang_byte_array.h b/librz/include/rz_util/rz_lang_byte_array.h index 6cd6337d390..7553bd16523 100644 --- a/librz/include/rz_util/rz_lang_byte_array.h +++ b/librz/include/rz_util/rz_lang_byte_array.h @@ -27,7 +27,6 @@ typedef enum rz_lang_byte_array_type_t { RZ_LANG_BYTE_ARRAY_PYTHON, RZ_LANG_BYTE_ARRAY_RUST, RZ_LANG_BYTE_ARRAY_SWIFT, - RZ_LANG_BYTE_ARRAY_VLANG, RZ_LANG_BYTE_ARRAY_YARA, } RzLangByteArrayType; diff --git a/librz/util/lang_byte_array.c b/librz/util/lang_byte_array.c index d9e20f967b3..3f5b129e053 100644 --- a/librz/util/lang_byte_array.c +++ b/librz/util/lang_byte_array.c @@ -197,23 +197,6 @@ static void lang_byte_array_swift(RzStrBuf *sb, const ut8 *buffer, size_t size) } } -static void lang_byte_array_vlang(RzStrBuf *sb, const ut8 *buffer, size_t size) { - if (size < 1) { - rz_strbuf_append(sb, "// Warning: the number of available bytes is less than 1"); - return; - } else if (size == 1) { - rz_strbuf_appendf(sb, "byteArray := [ byte(%u) ]\n ", buffer[0]); - return; - } - rz_strbuf_appendf(sb, "byteArray := [ byte(%u)\n ", buffer[0]); - for (size_t pos = 1; pos < size; pos++) { - if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) { - rz_strbuf_append(sb, "\n "); - } - rz_strbuf_appendf(sb, (pos + 1) < size ? " %u," : " %u\n];", buffer[pos]); - } -} - static void lang_byte_array_yara(RzStrBuf *sb, const ut8 *buffer, size_t size) { rz_strbuf_append(sb, "$byteArray = {\n "); for (size_t pos = 0; pos < size; pos++) { @@ -301,9 +284,6 @@ RZ_API RZ_OWN char *rz_lang_byte_array(RZ_NONNULL const ut8 *buffer, size_t size case RZ_LANG_BYTE_ARRAY_SWIFT: lang_byte_array_swift(&sb, buffer, size); break; - case RZ_LANG_BYTE_ARRAY_VLANG: - lang_byte_array_vlang(&sb, buffer, size); - break; case RZ_LANG_BYTE_ARRAY_YARA: lang_byte_array_yara(&sb, buffer, size); break; From ef388669b3de2b51ed9aac948c31624c6066229d Mon Sep 17 00:00:00 2001 From: wargio Date: Thu, 24 Mar 2022 18:16:27 +0100 Subject: [PATCH 5/8] removed vlang tests --- test/db/cmd/cmd_0 | 7 ------- test/db/cmd/cmd_pc | 11 ----------- 2 files changed, 18 deletions(-) diff --git a/test/db/cmd/cmd_0 b/test/db/cmd/cmd_0 index bec9abb5ae9..655b381d204 100644 --- a/test/db/cmd/cmd_0 +++ b/test/db/cmd/cmd_0 @@ -306,13 +306,6 @@ EXPECT=< Date: Thu, 24 Mar 2022 19:25:40 +0100 Subject: [PATCH 6/8] fixed test --- test/db/formats/elf/helloworld-gcc-elf | 46 ++++++++++---------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/test/db/formats/elf/helloworld-gcc-elf b/test/db/formats/elf/helloworld-gcc-elf index 666f10da416..ed168555cc2 100644 --- a/test/db/formats/elf/helloworld-gcc-elf +++ b/test/db/formats/elf/helloworld-gcc-elf @@ -382,34 +382,24 @@ NAME=pc C FILE=bins/elf/analysis/x86-helloworld-gcc CMDS=pc EXPECT=< Date: Fri, 25 Mar 2022 11:27:20 +0100 Subject: [PATCH 7/8] Fixed in a more portable way conversion on signed byte for Java/Kotlin --- librz/util/lang_byte_array.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/librz/util/lang_byte_array.c b/librz/util/lang_byte_array.c index 3f5b129e053..4b358629124 100644 --- a/librz/util/lang_byte_array.c +++ b/librz/util/lang_byte_array.c @@ -109,7 +109,10 @@ static void lang_byte_array_java(RzStrBuf *sb, const ut8 *buffer, size_t size) { if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) { rz_strbuf_append(sb, "\n "); } - value = (int)(char)buffer[pos]; + value = buffer[pos]; + if (value > 127) { + value -= 256; + } rz_strbuf_appendf(sb, " %4d,", value); } rz_strbuf_append(sb, "\n};"); @@ -135,7 +138,10 @@ static void lang_byte_array_kotlin(RzStrBuf *sb, const ut8 *buffer, size_t size) if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) { rz_strbuf_append(sb, "\n "); } - value = (int)(char)buffer[pos]; + value = buffer[pos]; + if (value > 127) { + value -= 256; + } rz_strbuf_appendf(sb, " %4d,", value); } rz_strbuf_append(sb, "\n);"); From b595802ebd737e17ee39800fe9280f30aa8cae99 Mon Sep 17 00:00:00 2001 From: wargio Date: Fri, 25 Mar 2022 11:54:46 +0100 Subject: [PATCH 8/8] Resolved comments and added examples to help users --- librz/core/cmd/cmd_print.c | 20 +-- librz/core/cmd_descs/cmd_descs.c | 190 ++++++---------------------- librz/core/cmd_descs/cmd_descs.h | 1 - librz/core/cmd_descs/cmd_print.yaml | 134 +++++++------------- test/db/cmd/cmd_0 | 119 ----------------- test/db/cmd/cmd_pc | 50 +++++--- 6 files changed, 117 insertions(+), 397 deletions(-) diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index 1b54becb1ef..04b077220dc 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -7159,38 +7159,22 @@ RZ_IPI RzCmdStatus rz_print_utf32be_handler(RzCore *core, int argc, const char * #define CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(name, type) \ RZ_IPI RzCmdStatus name(RzCore *core, int argc, const char **argv) { \ - ut64 oldsize = core->blocksize; \ - ut64 len = argc == 2 ? rz_num_math(core->num, argv[1]) : oldsize; \ - if (len > oldsize) { \ - rz_core_block_size(core, len); \ - } \ - char *code = rz_lang_byte_array(core->block, len, type); \ + char *code = rz_lang_byte_array(core->block, core->blocksize, type); \ if (RZ_STR_ISNOTEMPTY(code)) { \ rz_cons_println(code); \ free(code); \ } \ - if (len > oldsize) { \ - rz_core_block_size(core, oldsize); \ - } \ return code ? RZ_CMD_STATUS_OK : RZ_CMD_STATUS_ERROR; \ } #define CMD_PRINT_BYTE_ARRAY_HANDLER_ENDIAN(name, type) \ RZ_IPI RzCmdStatus name(RzCore *core, int argc, const char **argv) { \ - ut64 oldsize = core->blocksize; \ bool big_endian = rz_config_get_b(core->config, "cfg.bigendian"); \ - ut64 len = argc == 2 ? rz_num_math(core->num, argv[1]) : oldsize; \ - if (len > oldsize) { \ - rz_core_block_size(core, len); \ - } \ - char *code = rz_lang_byte_array(core->block, len, big_endian ? type##_BE : type##_LE); \ + char *code = rz_lang_byte_array(core->block, core->blocksize, big_endian ? type##_BE : type##_LE); \ if (RZ_STR_ISNOTEMPTY(code)) { \ rz_cons_println(code); \ free(code); \ } \ - if (len > oldsize) { \ - rz_core_block_size(core, oldsize); \ - } \ return code ? RZ_CMD_STATUS_OK : RZ_CMD_STATUS_ERROR; \ } CMD_PRINT_BYTE_ARRAY_HANDLER_NORMAL(rz_cmd_print_byte_array_rizin_handler, RZ_LANG_BYTE_ARRAY_RIZIN); diff --git a/librz/core/cmd_descs/cmd_descs.c b/librz/core/cmd_descs/cmd_descs.c index 78740651234..964fc720069 100644 --- a/librz/core/cmd_descs/cmd_descs.c +++ b/librz/core/cmd_descs/cmd_descs.c @@ -37,6 +37,7 @@ static const RzCmdDescDetail dr_details[2]; static const RzCmdDescDetail eval_getset_details[2]; static const RzCmdDescDetail egg_config_details[2]; static const RzCmdDescDetail history_list_or_exec_details[2]; +static const RzCmdDescDetail cmd_print_byte_array_details[3]; static const RzCmdDescDetail write_bits_details[2]; static const RzCmdDescDetail wv_details[2]; static const RzCmdDescDetail w1_details[2]; @@ -400,24 +401,6 @@ 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 cmd_print_byte_array_c_cpp_bytes_args[2]; -static const RzCmdDescArg cmd_print_byte_array_c_cpp_half_word_args[2]; -static const RzCmdDescArg cmd_print_byte_array_c_cpp_word_args[2]; -static const RzCmdDescArg cmd_print_byte_array_c_cpp_double_word_args[2]; -static const RzCmdDescArg cmd_print_byte_array_asm_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_vlang_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_print_gadget_add_args[6]; static const RzCmdDescArg cmd_print_gadget_move_args[6]; static const RzCmdDescArg cmd_print_msg_digest_args[2]; @@ -9126,258 +9109,160 @@ static const RzCmdDescHelp open_exchange_help = { static const RzCmdDescHelp cmd_print_help = { .summary = "Print commands", }; +static const RzCmdDescDetailEntry cmd_print_byte_array_Useful_space_modifiers_detail_entries[] = { + { .text = "pch @e:cfg.bigendian=", .arg_str = NULL, .comment = "Change endianness for pch, pcw and pcd commands" }, + { .text = "pc @! ", .arg_str = NULL, .comment = "Change the N of bytes (i.e. block size)." }, + { 0 }, +}; + +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" }, + { 0 }, +}; +static const RzCmdDescDetail cmd_print_byte_array_details[] = { + { .name = "Useful modifiers", .entries = cmd_print_byte_array_Useful_space_modifiers_detail_entries }, + { .name = "Example of usages", .entries = cmd_print_byte_array_Example_space_of_space_usages_detail_entries }, + { 0 }, +}; static const RzCmdDescHelp cmd_print_byte_array_help = { .summary = "Print bytes as code byte arrays.", + .details = cmd_print_byte_array_details, }; static const RzCmdDescArg cmd_print_byte_array_c_cpp_bytes_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_c_cpp_bytes_help = { - .summary = "Print bytes as C/C++ byte array.", + .summary = "Generate a C/C++ byte array.", .args = cmd_print_byte_array_c_cpp_bytes_args, }; static const RzCmdDescArg cmd_print_byte_array_c_cpp_half_word_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_c_cpp_half_word_help = { - .summary = "Print bytes as C/C++ 16 bits array (endianness follows cfg.bigendian).", + .summary = "Generate a C/C++ 16 bits array.", .args = cmd_print_byte_array_c_cpp_half_word_args, }; static const RzCmdDescArg cmd_print_byte_array_c_cpp_word_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_c_cpp_word_help = { - .summary = "Print bytes as C/C++ 32 bits array (endianness follows cfg.bigendian).", + .summary = "Generate a C/C++ 32 bits array.", .args = cmd_print_byte_array_c_cpp_word_args, }; static const RzCmdDescArg cmd_print_byte_array_c_cpp_double_word_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_c_cpp_double_word_help = { - .summary = "Print bytes as C/C++ 64 bits array (endianness follows cfg.bigendian).", + .summary = "Generate a C/C++ 64 bits array.", .args = cmd_print_byte_array_c_cpp_double_word_args, }; static const RzCmdDescArg cmd_print_byte_array_asm_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_asm_help = { - .summary = "Print bytes as GAS assembly.", + .summary = "Generate a byte array in GAS assembly.", .args = cmd_print_byte_array_asm_args, }; static const RzCmdDescArg cmd_print_byte_array_bash_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_bash_help = { - .summary = "Print bytes as a bash command.", + .summary = "Generate a bash script with the byte array.", .args = cmd_print_byte_array_bash_args, }; static const RzCmdDescArg cmd_print_byte_array_golang_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_golang_help = { - .summary = "Print bytes as golang byte array.", + .summary = "Generate a Golang byte array.", .args = cmd_print_byte_array_golang_args, }; static const RzCmdDescArg cmd_print_byte_array_java_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_java_help = { - .summary = "Print bytes as Java byte array.", + .summary = "Generate a Java byte array.", .args = cmd_print_byte_array_java_args, }; static const RzCmdDescArg cmd_print_byte_array_json_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_json_help = { - .summary = "Print bytes as json byte array.", + .summary = "Generate a JSON byte array.", .args = cmd_print_byte_array_json_args, }; static const RzCmdDescArg cmd_print_byte_array_kotlin_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_kotlin_help = { - .summary = "Print bytes as Kotlin byte array.", + .summary = "Generate a Kotlin byte array.", .args = cmd_print_byte_array_kotlin_args, }; static const RzCmdDescArg cmd_print_byte_array_nodejs_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_nodejs_help = { - .summary = "Print bytes as NodeJS buffer.", + .summary = "Generate a NodeJS buffer.", .args = cmd_print_byte_array_nodejs_args, }; static const RzCmdDescArg cmd_print_byte_array_objc_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_objc_help = { - .summary = "Print bytes as Objective-C/C++ byte array.", + .summary = "Generate a Objective-C/C++ byte array.", .args = cmd_print_byte_array_objc_args, }; static const RzCmdDescArg cmd_print_byte_array_python_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_python_help = { - .summary = "Print bytes as Python byte array.", + .summary = "Generate a Python byte array.", .args = cmd_print_byte_array_python_args, }; static const RzCmdDescArg cmd_print_byte_array_rust_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_rust_help = { - .summary = "Print bytes as Rust byte array.", + .summary = "Generate a Rust byte array.", .args = cmd_print_byte_array_rust_args, }; static const RzCmdDescArg cmd_print_byte_array_swift_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_swift_help = { - .summary = "Print bytes as Swift byte array.", + .summary = "Generate a Swift byte array.", .args = cmd_print_byte_array_swift_args, }; -static const RzCmdDescArg cmd_print_byte_array_vlang_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, - { 0 }, -}; -static const RzCmdDescHelp cmd_print_byte_array_vlang_help = { - .summary = "Print bytes as Vlang byte array.", - .args = cmd_print_byte_array_vlang_args, -}; - static const RzCmdDescArg cmd_print_byte_array_yara_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_yara_help = { - .summary = "Print bytes as Yara byte array.", + .summary = "Generate a Yara match pattern.", .args = cmd_print_byte_array_yara_args, }; static const RzCmdDescArg cmd_print_byte_array_rizin_args[] = { - { - .name = "n_bytes", - .type = RZ_CMD_ARG_TYPE_NUM, - .optional = true, - - }, { 0 }, }; static const RzCmdDescHelp cmd_print_byte_array_rizin_help = { - .summary = "Print bytes as rizin command.", + .summary = "Generate a rizin commands for writing the byte array.", .args = cmd_print_byte_array_rizin_args, }; @@ -14886,9 +14771,6 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) { RzCmdDesc *cmd_print_byte_array_swift_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcs", rz_cmd_print_byte_array_swift_handler, &cmd_print_byte_array_swift_help); rz_warn_if_fail(cmd_print_byte_array_swift_cd); - RzCmdDesc *cmd_print_byte_array_vlang_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcv", rz_cmd_print_byte_array_vlang_handler, &cmd_print_byte_array_vlang_help); - rz_warn_if_fail(cmd_print_byte_array_vlang_cd); - RzCmdDesc *cmd_print_byte_array_yara_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_byte_array_cd, "pcy", rz_cmd_print_byte_array_yara_handler, &cmd_print_byte_array_yara_help); rz_warn_if_fail(cmd_print_byte_array_yara_cd); diff --git a/librz/core/cmd_descs/cmd_descs.h b/librz/core/cmd_descs/cmd_descs.h index cff1d30c185..fb96474d505 100644 --- a/librz/core/cmd_descs/cmd_descs.h +++ b/librz/core/cmd_descs/cmd_descs.h @@ -672,7 +672,6 @@ RZ_IPI RzCmdStatus rz_cmd_print_byte_array_objc_handler(RzCore *core, int argc, RZ_IPI RzCmdStatus rz_cmd_print_byte_array_python_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_byte_array_rust_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_byte_array_swift_handler(RzCore *core, int argc, const char **argv); -RZ_IPI RzCmdStatus rz_cmd_print_byte_array_vlang_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_byte_array_yara_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_byte_array_rizin_handler(RzCore *core, int argc, const char **argv); RZ_IPI RzCmdStatus rz_cmd_print_gadget_add_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 276e2d2bd21..b2a33f2c9cb 100644 --- a/librz/core/cmd_descs/cmd_print.yaml +++ b/librz/core/cmd_descs/cmd_print.yaml @@ -6,126 +6,90 @@ commands: - name: pc summary: Print bytes as code byte arrays. cname: cmd_print_byte_array + details: + - name: Useful modifiers + entries: + - text: "pch @e:cfg.bigendian=" + comment: "Change endianness for pch, pcw and pcd commands" + - text: "pc @! " + comment: "Change the N of bytes (i.e. block size)." + - name: Example of usages + entries: + - text: "pch @! 64 @e:cfg.bigendian=true" + comment: "Generate a C 32 bits array in big endian format, using 64 bytes" + - text: "pcp @! 1024" + comment: "Generate a Python byte array of size 1024" + - text: "pcj @! 10" + comment: "Generate a JSON bytes array of size 10" subcommands: - name: pc - summary: Print bytes as C/C++ byte array. + summary: Generate a C/C++ byte array. cname: cmd_print_byte_array_c_cpp_bytes - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pch - summary: Print bytes as C/C++ 16 bits array (endianness follows cfg.bigendian). + summary: Generate a C/C++ 16 bits array. cname: cmd_print_byte_array_c_cpp_half_word - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pcw - summary: Print bytes as C/C++ 32 bits array (endianness follows cfg.bigendian). + summary: Generate a C/C++ 32 bits array. cname: cmd_print_byte_array_c_cpp_word - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pcd - summary: Print bytes as C/C++ 64 bits array (endianness follows cfg.bigendian). + summary: Generate a C/C++ 64 bits array. cname: cmd_print_byte_array_c_cpp_double_word - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pca - summary: Print bytes as GAS assembly. + summary: Generate a byte array in GAS assembly. cname: cmd_print_byte_array_asm - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pcb - summary: Print bytes as a bash command. + summary: Generate a bash script with the byte array. cname: cmd_print_byte_array_bash - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pcg - summary: Print bytes as golang byte array. + summary: Generate a Golang byte array. cname: cmd_print_byte_array_golang - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pcJ - summary: Print bytes as Java byte array. + summary: Generate a Java byte array. cname: cmd_print_byte_array_java - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pcj - summary: Print bytes as json byte array. + summary: Generate a JSON byte array. cname: cmd_print_byte_array_json - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pck - summary: Print bytes as Kotlin byte array. + summary: Generate a Kotlin byte array. cname: cmd_print_byte_array_kotlin - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pcn - summary: Print bytes as NodeJS buffer. + summary: Generate a NodeJS buffer. cname: cmd_print_byte_array_nodejs - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pco - summary: Print bytes as Objective-C/C++ byte array. + summary: Generate a Objective-C/C++ byte array. cname: cmd_print_byte_array_objc - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pcp - summary: Print bytes as Python byte array. + summary: Generate a Python byte array. cname: cmd_print_byte_array_python - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pcr - summary: Print bytes as Rust byte array. + summary: Generate a Rust byte array. cname: cmd_print_byte_array_rust - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pcs - summary: Print bytes as Swift byte array. + summary: Generate a Swift byte array. cname: cmd_print_byte_array_swift - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pcy - summary: Print bytes as Yara byte array. + summary: Generate a Yara match pattern. cname: cmd_print_byte_array_yara - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pc* - summary: Print bytes as rizin command. + summary: Generate a rizin commands for writing the byte array. cname: cmd_print_byte_array_rizin - args: - - name: n_bytes - type: RZ_CMD_ARG_TYPE_NUM - optional: true + args: [] - name: pg summary: Print gadgets cname: cmd_print_gadget diff --git a/test/db/cmd/cmd_0 b/test/db/cmd/cmd_0 index 655b381d204..ebc2d0062d4 100644 --- a/test/db/cmd/cmd_0 +++ b/test/db/cmd/cmd_0 @@ -194,125 +194,6 @@ EXPECT=< data.bin printf "\361\377\377\353\000\060\260\343\003\000\240\341\361\377" >> data.bin @@ -161,7 +171,7 @@ RUN NAME=Print bytes as golang byte array. FILE=bins/elf/analysis/hello-arm32 -CMDS=pcg 30 +CMDS=pcg @! 30 EXPECT=<