From 960b0fc211ffa946ca9b0d3771119e5bc8835bf3 Mon Sep 17 00:00:00 2001 From: pancake Date: Sun, 12 Aug 2018 00:37:53 +0200 Subject: [PATCH] Initial cleanup of the rbin api --- libr/bin/bin.c | 52 +++++++++++++++++------------------------- libr/core/cmd_open.c | 7 +++--- libr/core/cmd_search.c | 2 +- libr/core/file.c | 2 +- libr/include/r_bin.h | 4 ++-- 5 files changed, 28 insertions(+), 39 deletions(-) diff --git a/libr/bin/bin.c b/libr/bin/bin.c index da9079d03f..2b1973c5bb 100644 --- a/libr/bin/bin.c +++ b/libr/bin/bin.c @@ -32,7 +32,18 @@ static RBinPlugin *bin_static_plugins[] = { R_BIN_STATIC_PLUGINS, NULL }; static RBinXtrPlugin *bin_xtr_static_plugins[] = { R_BIN_XTR_STATIC_PLUGINS, NULL }; static RBinLdrPlugin *bin_ldr_static_plugins[] = { R_BIN_LDR_STATIC_PLUGINS, NULL }; -static bool r_bin_load_io_at_offset_as(RBin *bin, int fd, ut64 baseaddr, ut64 loadaddr, int xtr_idx, ut64 offset, const char *name); +// TODO: try to deprecate and just call the r_bin_load_io_at_offset_as_az +R_API bool r_bin_load_io (RBin *bin, int fd, ut64 baseaddr, ut64 loadaddr, int xtr_idx, ut64 offset, const char *name) { + // adding file_sz to help reduce the performance impact on the system + // in this case the number of bytes read will be limited to 2MB + // (MIN_LOAD_SIZE) + // if it fails, the whole file is loaded. + const ut64 MAX_LOAD_SIZE = 0; // 0xfffff; //128 * (1 << 10 << 10); + int res = r_bin_load_io2 (bin, fd, baseaddr, + loadaddr, xtr_idx, offset, name, MAX_LOAD_SIZE); + return res? res: r_bin_load_io2 (bin, fd, baseaddr, + loadaddr, xtr_idx, offset, name, UT64_MAX); +} static int getoffset(RBin *bin, int type, int idx) { RBinFile *a = r_bin_cur (bin); @@ -248,7 +259,7 @@ R_API int r_bin_load(RBin *bin, const char *file, ut64 baseaddr, ut64 loadaddr, return false; } //Use the current RIODesc otherwise r_io_map_select can swap them later on - return r_bin_load_io (bin, fd, baseaddr, loadaddr, xtr_idx); + return r_bin_load_io (bin, fd, baseaddr, loadaddr, xtr_idx, 0, NULL); } R_API int r_bin_load_as(RBin *bin, const char *file, ut64 baseaddr, @@ -264,8 +275,7 @@ R_API int r_bin_load_as(RBin *bin, const char *file, ut64 baseaddr, if (fd < 0) { return false; } - return r_bin_load_io_at_offset_as (bin, fd, baseaddr, loadaddr, - xtr_idx, fileoffset, name); + return r_bin_load_io (bin, fd, baseaddr, loadaddr, xtr_idx, fileoffset, name); } R_API int r_bin_reload(RBin *bin, int fd, ut64 baseaddr) { @@ -356,15 +366,13 @@ R_API int r_bin_reload(RBin *bin, int fd, ut64 baseaddr) { if (r_list_length (the_obj_list) == 1) { RBinObject *old_o = (RBinObject *)r_list_get_n (the_obj_list, 0); - res = r_bin_load_io_at_offset_as (bin, fd, baseaddr, - old_o->loadaddr, 0, old_o->boffset, NULL); + res = r_bin_load_io (bin, fd, baseaddr, old_o->loadaddr, 0, old_o->boffset, NULL); } else { RListIter *iter = NULL; RBinObject *old_o; r_list_foreach (the_obj_list, iter, old_o) { // XXX - naive. do we need a way to prevent multiple "anys" from being opened? - res = r_bin_load_io_at_offset_as (bin, fd, baseaddr, - old_o->loadaddr, 0, old_o->boffset, old_o->plugin->name); + res = r_bin_load_io (bin, fd, baseaddr, old_o->loadaddr, 0, old_o->boffset, old_o->plugin->name); } } bf->o = r_list_get_n (bf->objs, 0); @@ -375,12 +383,7 @@ error: return res; } -R_API int r_bin_load_io(RBin *bin, int fd, ut64 baseaddr, ut64 loadaddr, int xtr_idx) { - return r_bin_load_io_at_offset_as (bin, fd, baseaddr, loadaddr, xtr_idx, 0, NULL); -} - -R_API int r_bin_load_io_at_offset_as_sz(RBin *bin, int fd, ut64 baseaddr, - ut64 loadaddr, int xtr_idx, ut64 offset, const char *name, ut64 sz) { +R_API bool r_bin_load_io2(RBin *bin, int fd, ut64 baseaddr, ut64 loadaddr, int xtr_idx, ut64 offset, const char *name, ut64 sz) { RIOBind *iob = &(bin->iob); RIO *io = iob? iob->io: NULL; RListIter *it; @@ -422,7 +425,9 @@ R_API int r_bin_load_io_at_offset_as_sz(RBin *bin, int fd, ut64 baseaddr, //from the memory if (tfd >= 0) { buf_bytes = calloc (1, sz + 1); - iob->fd_read_at (io, tfd, 0, buf_bytes, sz); + if (buf_bytes) { + iob->fd_read_at (io, tfd, 0, buf_bytes, sz); + } // iob->fd_close (io, tfd); } } @@ -485,22 +490,6 @@ R_API int r_bin_load_io_at_offset_as_sz(RBin *bin, int fd, ut64 baseaddr, return binfile? r_bin_file_set_cur_binfile (bin, binfile): false; } -static bool r_bin_load_io_at_offset_as(RBin *bin, int fd, ut64 baseaddr, - ut64 loadaddr, int xtr_idx, ut64 offset, const char *name) { - // adding file_sz to help reduce the performance impact on the system - // in this case the number of bytes read will be limited to 2MB - // (MIN_LOAD_SIZE) - // if it fails, the whole file is loaded. - const ut64 MAX_LOAD_SIZE = 0; // 0xfffff; //128 * (1 << 10 << 10); - int res = r_bin_load_io_at_offset_as_sz (bin, fd, baseaddr, - loadaddr, xtr_idx, offset, name, MAX_LOAD_SIZE); - if (!res) { - res = r_bin_load_io_at_offset_as_sz (bin, fd, baseaddr, - loadaddr, xtr_idx, offset, name, UT64_MAX); - } - return res; -} - R_API RBinPlugin *r_bin_get_binplugin_by_name(RBin *bin, const char *name) { RBinPlugin *plugin; RListIter *it; @@ -545,6 +534,7 @@ R_API RBinXtrPlugin *r_bin_get_xtrplugin_by_name(RBin *bin, const char *name) { return NULL; } +// TODO: deprecate R_API RBinPlugin *r_bin_get_binplugin_any(RBin *bin) { return r_bin_get_binplugin_by_name (bin, "any"); } diff --git a/libr/core/cmd_open.c b/libr/core/cmd_open.c index 8c6b4a9ed0..e94f1f957d 100644 --- a/libr/core/cmd_open.c +++ b/libr/core/cmd_open.c @@ -290,7 +290,7 @@ static void cmd_open_bin(RCore *core, const char *input) { if (desc) { *filename = 0; ut64 addr = r_num_math (core->num, arg); - r_bin_load_io (core->bin, desc->fd, addr, 0, 0); + r_bin_load_io (core->bin, desc->fd, addr, 0, 0, 0, NULL); r_io_desc_close (desc); r_core_cmd0 (core, ".is*"); } else { @@ -301,7 +301,7 @@ static void cmd_open_bin(RCore *core, const char *input) { int fd = r_io_fd_get_current (core->io); RIODesc *desc = r_io_desc_get (core->io, fd); if (desc) { - r_bin_load_io (core->bin, desc->fd, addr, 0, 0); + r_bin_load_io (core->bin, desc->fd, addr, 0, 0, 0, NULL); r_core_cmd0 (core, ".is*"); } else { eprintf ("No file to load bin from?\n"); @@ -314,13 +314,12 @@ static void cmd_open_bin(RCore *core, const char *input) { RIODesc *desc; RListIter *iter; r_list_foreach (files, iter, desc) { - r_bin_load_io (core->bin, desc->fd, core->offset, 0, 0); + r_bin_load_io (core->bin, desc->fd, core->offset, 0, 0, 0, NULL); r_core_cmd0 (core, ".is*"); break; } r_list_free (files); } - //r_bin_load_io_at_offset_as (core->bin, core->file->desc, break; case 'b': // "obb" { diff --git a/libr/core/cmd_search.c b/libr/core/cmd_search.c index 85900d33f7..909970df9c 100644 --- a/libr/core/cmd_search.c +++ b/libr/core/cmd_search.c @@ -256,7 +256,7 @@ static void cmd_search_bin(RCore *core, RInterval itv) { r_cons_printf ("0x%08"PFMT64x " %s\n", from, plug->name); // TODO: load the bin and calculate its size if (plug->size) { - r_bin_load_io_at_offset_as_sz (core->bin, core->file->fd, + r_bin_load_io2 (core->bin, core->file->fd, 0, 0, 0, core->offset, plug->name, 4096); size = plug->size (core->bin->cur); if (size > 0) { diff --git a/libr/core/file.c b/libr/core/file.c index 0a65bd9e84..8d22859193 100644 --- a/libr/core/file.c +++ b/libr/core/file.c @@ -382,7 +382,7 @@ static int r_core_file_do_load_for_io_plugin(RCore *r, ut64 baseaddr, ut64 loada return false; } r_io_use_fd (r->io, fd); - if (!r_bin_load_io (r->bin, fd, baseaddr, loadaddr, xtr_idx)) { + if (!r_bin_load_io (r->bin, fd, baseaddr, loadaddr, xtr_idx, 0, NULL)) { //eprintf ("Failed to load the bin with an IO Plugin.\n"); return false; } diff --git a/libr/include/r_bin.h b/libr/include/r_bin.h index 7f6d5ecc08..ce8c36f3a7 100644 --- a/libr/include/r_bin.h +++ b/libr/include/r_bin.h @@ -631,8 +631,8 @@ R_API bool r_bin_query(RBin *bin, const char *query); /* load */ R_API int r_bin_load(RBin *bin, const char *file, ut64 baseaddr, ut64 loadaddr, int xtr_idx, int fd, int rawstr); R_API int r_bin_load_as(RBin *bin, const char *file, ut64 baseaddr, ut64 loadaddr, int xtr_idx, int fd, int rawstr, int fileoffset, const char *name); -R_API int r_bin_load_io(RBin *bin, int fd, ut64 baseaddr, ut64 loadaddr, int xtr_idx); -R_API int r_bin_load_io_at_offset_as_sz(RBin *bin, int fd, ut64 baseaddr, ut64 loadaddr, int xtr_idx, ut64 offset, const char *name, ut64 sz); +R_API bool r_bin_load_io (RBin *bin, int fd, ut64 baseaddr, ut64 loadaddr, int xtr_idx, ut64 offset, const char *name); +R_API bool r_bin_load_io2(RBin *bin, int fd, ut64 baseaddr, ut64 loadaddr, int xtr_idx, ut64 offset, const char *name, ut64 sz); /* bin.c */ R_API void r_bin_load_filter(RBin *bin, ut64 rules);