From 22a1266331d4fb3dc5e9b44027e42f254e8b0d50 Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 18 Aug 2022 17:13:40 +0200 Subject: [PATCH] Implement y- command and some other indentation fixes ##shell --- libr/bin/format/coff/coff.c | 45 ++++++++++++++++++++----------------- libr/bin/format/coff/coff.h | 14 ++++++------ libr/core/cmd.c | 8 +++++++ libr/core/yank.c | 40 +++++++++++++++++---------------- 4 files changed, 61 insertions(+), 46 deletions(-) diff --git a/libr/bin/format/coff/coff.c b/libr/bin/format/coff/coff.c index 082c7aa2fd..1057b80820 100644 --- a/libr/bin/format/coff/coff.c +++ b/libr/bin/format/coff/coff.c @@ -6,7 +6,7 @@ #define bprintf if (obj->verbose)eprintf -bool r_coff_supported_arch(const ut8 *buf) { +R_IPI bool r_coff_supported_arch(const ut8 *buf) { ut16 arch = r_read_le16 (buf); switch (arch) { case COFF_FILE_MACHINE_MIPS16: @@ -34,7 +34,7 @@ bool r_coff_supported_arch(const ut8 *buf) { } } -char *r_coff_symbol_name(struct r_bin_coff_obj *obj, void *ptr) { +R_IPI char *r_coff_symbol_name(RBinCoffObj *obj, void *ptr) { char n[256] = {0}; int len = 0, offset = 0; union { @@ -63,7 +63,7 @@ char *r_coff_symbol_name(struct r_bin_coff_obj *obj, void *ptr) { return strdup (n); } -static int r_coff_rebase_sym(struct r_bin_coff_obj *obj, RBinAddr *addr, struct coff_symbol *sym) { +static int r_coff_rebase_sym(RBinCoffObj *obj, RBinAddr *addr, struct coff_symbol *sym) { if (sym->n_scnum < 1 || sym->n_scnum > obj->hdr.f_nscns) { return 0; } @@ -73,7 +73,7 @@ static int r_coff_rebase_sym(struct r_bin_coff_obj *obj, RBinAddr *addr, struct /* Try to get a valid entrypoint using the methods outlined in * http://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_mono/ld.html#SEC24 */ -RBinAddr *r_coff_get_entry(struct r_bin_coff_obj *obj) { +R_IPI RBinAddr *r_coff_get_entry(RBinCoffObj *obj) { RBinAddr *addr = R_NEW0 (RBinAddr); int i; if (!addr) { @@ -120,7 +120,7 @@ RBinAddr *r_coff_get_entry(struct r_bin_coff_obj *obj) { return addr; } -static bool r_bin_coff_init_hdr(struct r_bin_coff_obj *obj) { +static bool r_bin_coff_init_hdr(RBinCoffObj *obj) { ut16 magic = r_buf_read_le16_at (obj->b, 0); switch (magic) { case COFF_FILE_MACHINE_H8300: @@ -144,7 +144,7 @@ static bool r_bin_coff_init_hdr(struct r_bin_coff_obj *obj) { return true; } -static bool r_bin_coff_init_opt_hdr(struct r_bin_coff_obj *obj) { +static bool r_bin_coff_init_opt_hdr(RBinCoffObj *obj) { int ret; if (!obj->hdr.f_opthdr) { return false; @@ -157,7 +157,7 @@ static bool r_bin_coff_init_opt_hdr(struct r_bin_coff_obj *obj) { return true; } -static bool r_bin_coff_init_scn_hdr(struct r_bin_coff_obj *obj) { +static bool r_bin_coff_init_scn_hdr(RBinCoffObj *obj) { int ret, size; ut64 offset = sizeof (struct coff_hdr) + (obj->hdr.f_opthdr ? sizeof (struct coff_opt_hdr) : 0); if (obj->hdr.f_magic == COFF_FILE_TI_COFF) { @@ -179,7 +179,7 @@ static bool r_bin_coff_init_scn_hdr(struct r_bin_coff_obj *obj) { return true; } -static bool r_bin_coff_init_symtable(struct r_bin_coff_obj *obj) { +static bool r_bin_coff_init_symtable(RBinCoffObj *obj) { int ret, size; ut64 offset = obj->hdr.f_symptr; if (obj->hdr.f_nsyms >= 0xffff || !obj->hdr.f_nsyms) { // too much symbols, probably not allocatable @@ -204,7 +204,7 @@ static bool r_bin_coff_init_symtable(struct r_bin_coff_obj *obj) { return true; } -static bool r_bin_coff_init_scn_va(struct r_bin_coff_obj *obj) { +static bool r_bin_coff_init_scn_va(RBinCoffObj *obj) { obj->scn_va = R_NEWS (ut64, obj->hdr.f_nscns); if (!obj->scn_va) { return false; @@ -223,7 +223,10 @@ static bool r_bin_coff_init_scn_va(struct r_bin_coff_obj *obj) { return true; } -static int r_bin_coff_init(struct r_bin_coff_obj *obj, RBuffer *buf, bool verbose) { +static bool r_bin_coff_init(RBinCoffObj *obj, RBuffer *buf, bool verbose) { + if (!obj || buf) { + return false; + } obj->b = r_buf_ref (buf); obj->size = r_buf_size (buf); obj->verbose = verbose; @@ -249,18 +252,20 @@ static int r_bin_coff_init(struct r_bin_coff_obj *obj, RBuffer *buf, bool verbos return true; } -R_API void r_bin_coff_free(struct r_bin_coff_obj *obj) { - ht_up_free (obj->sym_ht); - ht_up_free (obj->imp_ht); - free (obj->scn_va); - free (obj->scn_hdrs); - free (obj->symbols); - r_buf_free (obj->b); - free (obj); +R_IPI void r_bin_coff_free(RBinCoffObj *obj) { + if (obj) { + ht_up_free (obj->sym_ht); + ht_up_free (obj->imp_ht); + free (obj->scn_va); + free (obj->scn_hdrs); + free (obj->symbols); + r_buf_free (obj->b); + free (obj); + } } -struct r_bin_coff_obj* r_bin_coff_new_buf(RBuffer *buf, bool verbose) { - struct r_bin_coff_obj* bin = R_NEW0 (struct r_bin_coff_obj); +R_IPI RBinCoffObj *r_bin_coff_new_buf(RBuffer *buf, bool verbose) { + RBinCoffObj* bin = R_NEW0 (RBinCoffObj); r_bin_coff_init (bin, buf, verbose); return bin; } diff --git a/libr/bin/format/coff/coff.h b/libr/bin/format/coff/coff.h index 2e58f839e1..868dd34154 100644 --- a/libr/bin/format/coff/coff.h +++ b/libr/bin/format/coff/coff.h @@ -14,7 +14,7 @@ #include "coff_specs.h" -struct r_bin_coff_obj { +typedef struct r_bin_coff_obj { struct coff_hdr hdr; struct coff_opt_hdr opt_hdr; struct coff_scn_hdr *scn_hdrs; @@ -30,12 +30,12 @@ struct r_bin_coff_obj { HtUP *sym_ht; HtUP *imp_ht; ut64 *scn_va; -}; +} RBinCoffObj; -bool r_coff_supported_arch(const ut8 *buf); /* Reads two bytes from buf. */ -struct r_bin_coff_obj* r_bin_coff_new_buf(RBuffer *buf, bool verbose); -void r_bin_coff_free(struct r_bin_coff_obj *obj); -RBinAddr *r_coff_get_entry(struct r_bin_coff_obj *obj); -char *r_coff_symbol_name (struct r_bin_coff_obj *obj, void *ptr); +R_IPI bool r_coff_supported_arch(const ut8 *buf); /* Reads two bytes from buf. */ +R_IPI RBinCoffObj* r_bin_coff_new_buf(RBuffer *buf, bool verbose); +R_IPI void r_bin_coff_free(RBinCoffObj *obj); +R_IPI RBinAddr *r_coff_get_entry(RBinCoffObj *obj); +R_IPI char *r_coff_symbol_name(RBinCoffObj *obj, void *ptr); #endif /* COFF_H */ diff --git a/libr/core/cmd.c b/libr/core/cmd.c index bd60dfdcbe..7bba7f6b7c 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -1064,6 +1064,14 @@ static int cmd_yank(void *data, const char *input) { case ' ': // "y " r_core_yank (core, core->offset, r_num_math (core->num, input + 1)); break; + case '-': // "y-" +#if R2_580 + r_core_yank_unset (core); +#else + r_buf_set_bytes (core->yank_buf, "", 0); + core->yank_addr = UT64_MAX; +#endif + break; case 'l': // "yl" r_core_return_value (core, r_buf_size (core->yank_buf)); break; diff --git a/libr/core/yank.c b/libr/core/yank.c index 271db35c30..a235d12722 100644 --- a/libr/core/yank.c +++ b/libr/core/yank.c @@ -113,7 +113,7 @@ R_API int r_core_yank_set(RCore *core, ut64 addr, const ut8 *buf, ut32 len) { R_API int r_core_yank_set_str(RCore *core, ut64 addr, const char *str, ut32 len) { // free (core->yank_buf); int res = r_core_yank_set (core, addr, (ut8 *)str, len); - if (res == true) { + if (res) { ut8 zero = 0; r_buf_write_at (core->yank_buf, len - 1, &zero, sizeof (zero)); } @@ -356,18 +356,13 @@ R_API int r_core_yank_cat_string(RCore *core, ut64 pos) { } R_API int r_core_yank_hud_file(RCore *core, const char *input) { - char *buf = NULL; - bool res = false; - ut32 len = 0; - if (!input || !*input) { + if (R_STR_ISEMPTY (input)) { return false; } - for (input++; *input == ' '; input++) { - /* nothing */ - } - buf = r_cons_hud_file (input); - len = buf? strlen ((const char *) buf) + 1: 0; - res = r_core_yank_set_str (core, R_CORE_FOREIGN_ADDR, buf, len); + input = r_str_trim_head_ro (input + 1); + char *buf = r_cons_hud_file (input); + ut32 len = buf? strlen ((const char *) buf) + 1: 0; + bool res = r_core_yank_set_str (core, R_CORE_FOREIGN_ADDR, buf, len); free (buf); return res; } @@ -386,8 +381,16 @@ R_API int r_core_yank_hud_path(RCore *core, const char *input, int dir) { return res; } +#if R2_580 +R_API void r_core_yank_unset(RCore *core) { + r_buf_free (core->yank_buf); + core->yank_addr = UT64_MAX; +} + +#endif + R_API bool r_core_yank_hexpair(RCore *core, const char *input) { - if (!input || !*input) { + if (R_STR_ISEMPTY (input)) { return false; } char *out = strdup (input); @@ -400,15 +403,15 @@ R_API bool r_core_yank_hexpair(RCore *core, const char *input) { } R_API bool r_core_yank_file_ex(RCore *core, const char *input) { - ut64 len = 0, adv = 0, addr = 0; + r_return_val_if_fail (core, false); bool res = false; if (!input) { return res; } // get the number of bytes to yank - adv = consume_chars (input, ' '); - len = r_num_math (core->num, input + adv); + ut64 adv = consume_chars (input, ' '); + ut64 len = r_num_math (core->num, input + adv); if (len == 0) { R_LOG_ERROR ("Number of bytes read must be > 0"); return res; @@ -422,7 +425,7 @@ R_API bool r_core_yank_file_ex(RCore *core, const char *input) { adv++; // XXX - bug, will fail if address needs to be computed and has spaces - addr = r_num_math (core->num, input + adv); + ut64 addr = r_num_math (core->num, input + adv); adv += find_next_char (input + adv, ' '); if (adv == 0) { @@ -435,10 +438,9 @@ R_API bool r_core_yank_file_ex(RCore *core, const char *input) { return perform_mapped_file_yank (core, addr, len, input + adv); } +// R2_580 R_API bool r_core_yank_file_all(RCore *core, const char *input) { R_API int r_core_yank_file_all(RCore *core, const char *input) { - if (!input) { - return false; - } + r_return_val_if_fail (core && input, false); ut64 adv = consume_chars (input, ' '); return perform_mapped_file_yank (core, 0, -1, input + adv); }