Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add rz_lang_byte_array and remove rz_print_code #2444

Merged
merged 8 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 121 additions & 1 deletion librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -6124,7 +6124,70 @@ 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]);
wargio marked this conversation as resolved.
Show resolved Hide resolved
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" // 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);
wargio marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand Down Expand Up @@ -7098,3 +7161,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
Loading