mirror of
https://github.com/radareorg/radare2.git
synced 2025-01-26 15:54:59 +00:00
Simplify r_bin_open_io function (#15307) ##bin
* remove file_sz field from r_bin_file_new_from_buffer and r_bin_file_xtr_load_buffer * remove all the messy buffer size handling and just rely on r_buf_size
This commit is contained in:
parent
0f430b5871
commit
219eefbd5d
@ -476,10 +476,10 @@ static bool xtr_metadata_match(RBinXtrData *xtr_data, const char *arch, int bits
|
||||
return bits == iter_bits && !strcmp (iter_arch, arch) && !xtr_data->loaded;
|
||||
}
|
||||
|
||||
R_IPI RBinFile *r_bin_file_new_from_buffer(RBin *bin, const char *file, RBuffer *buf, ut64 file_sz, int rawstr, ut64 baseaddr, ut64 loadaddr, int fd, const char *pluginname) {
|
||||
r_return_val_if_fail (bin && file && buf && file_sz != UT64_MAX, NULL);
|
||||
R_IPI RBinFile *r_bin_file_new_from_buffer(RBin *bin, const char *file, RBuffer *buf, int rawstr, ut64 baseaddr, ut64 loadaddr, int fd, const char *pluginname) {
|
||||
r_return_val_if_fail (bin && file && buf, NULL);
|
||||
|
||||
RBinFile *bf = r_bin_file_new (bin, file, file_sz, rawstr, fd, pluginname, NULL, false);
|
||||
RBinFile *bf = r_bin_file_new (bin, file, r_buf_size (buf), rawstr, fd, pluginname, NULL, false);
|
||||
if (bf) {
|
||||
bf->buf = r_buf_ref (buf);
|
||||
RBinPlugin *plugin = get_plugin_from_buffer (bin, pluginname, bf->buf);
|
||||
@ -491,7 +491,7 @@ R_IPI RBinFile *r_bin_file_new_from_buffer(RBin *bin, const char *file, RBuffer
|
||||
// size is set here because the reported size of the object depends on
|
||||
// if loaded from xtr plugin or partially read
|
||||
if (!o->size) {
|
||||
o->size = file_sz;
|
||||
o->size = r_buf_size (buf);
|
||||
}
|
||||
r_list_append (bin->binfiles, bf);
|
||||
}
|
||||
@ -698,12 +698,12 @@ R_API void r_bin_file_free(void /*RBinFile*/ *_bf) {
|
||||
free (bf);
|
||||
}
|
||||
|
||||
R_IPI RBinFile *r_bin_file_xtr_load_buffer(RBin *bin, RBinXtrPlugin *xtr, const char *filename, RBuffer *buf, ut64 file_sz, ut64 baseaddr, ut64 loadaddr, int idx, int fd, int rawstr) {
|
||||
R_IPI RBinFile *r_bin_file_xtr_load_buffer(RBin *bin, RBinXtrPlugin *xtr, const char *filename, RBuffer *buf, ut64 baseaddr, ut64 loadaddr, int idx, int fd, int rawstr) {
|
||||
r_return_val_if_fail (bin && xtr && buf, NULL);
|
||||
|
||||
RBinFile *bf = r_bin_file_find_by_name (bin, filename);
|
||||
if (!bf) {
|
||||
bf = r_bin_file_new (bin, filename, file_sz, rawstr, fd, xtr->name, bin->sdb, false);
|
||||
bf = r_bin_file_new (bin, filename, r_buf_size (buf), rawstr, fd, xtr->name, bin->sdb, false);
|
||||
if (!bf) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -296,58 +296,50 @@ R_API bool r_bin_open_io(RBin *bin, RBinOptions *opt) {
|
||||
RIO *io = iob? iob->io: NULL;
|
||||
RListIter *it;
|
||||
RBinXtrPlugin *xtr;
|
||||
int tfd = opt->fd;
|
||||
|
||||
bool is_debugger = iob->fd_is_dbg (io, opt->fd);
|
||||
const char *fname = iob->fd_get_name (io, opt->fd);
|
||||
|
||||
bin->rawstr = opt->rawstr;
|
||||
bin->file = fname;
|
||||
if (opt->loadaddr == UT64_MAX) {
|
||||
opt->loadaddr = 0;
|
||||
}
|
||||
ut64 file_sz = iob->fd_size (io, opt->fd);
|
||||
if (file_sz == UT64_MAX) {
|
||||
if (is_debugger) {
|
||||
tfd = iob->fd_open (io, fname, R_PERM_R, 0644);
|
||||
if (tfd >= 1) {
|
||||
file_sz = iob->fd_size (io, tfd);
|
||||
}
|
||||
} else {
|
||||
if (bin->verbose) {
|
||||
eprintf ("r_bin_open_io: unknown file size, Loading from memory..\n");
|
||||
}
|
||||
// return false;
|
||||
// Seems like thanks to the new IO buf doesnt really matters how big is this
|
||||
file_sz = 1024 * 1024 * 1024;
|
||||
}
|
||||
|
||||
// Create RBuffer from the opened file
|
||||
// When debugging something, we want to open the backed file because
|
||||
// not all binary info are mapped in the virtual space. If that is not
|
||||
// possible (e.g. remote file) just try to load bin info from the
|
||||
// debugee process.
|
||||
RBuffer *buf = NULL;
|
||||
if (is_debugger) {
|
||||
buf = r_buf_new_file (fname, O_RDONLY, 0);
|
||||
is_debugger = false;
|
||||
}
|
||||
if (opt->sz) {
|
||||
opt->sz = R_MIN (file_sz, opt->sz);
|
||||
} else {
|
||||
opt->sz = file_sz;
|
||||
if (!buf) {
|
||||
buf = r_buf_new_with_io (&bin->iob, opt->fd);
|
||||
}
|
||||
// check if blockdevice?
|
||||
if (opt->sz >= UT32_MAX) {
|
||||
opt->sz = 1024 * 32;
|
||||
}
|
||||
RBuffer *buf = r_buf_new_with_io (&bin->iob, tfd);
|
||||
if (!buf) {
|
||||
return false;
|
||||
}
|
||||
bin->file = fname;
|
||||
ut64 seekaddr = opt->loadaddr;
|
||||
|
||||
if (!is_debugger && seekaddr > 0 && seekaddr != UT64_MAX) {
|
||||
// slice buffer if necessary
|
||||
RBuffer *nb = r_buf_new_slice (buf, seekaddr, opt->sz);
|
||||
if (nb) {
|
||||
r_buf_free (buf);
|
||||
buf = nb;
|
||||
}
|
||||
if (!opt->sz) {
|
||||
opt->sz = r_buf_size (buf);
|
||||
}
|
||||
|
||||
// Slice buffer if necessary
|
||||
RBuffer *slice = buf;
|
||||
if (!is_debugger && (opt->loadaddr != 0 || opt->sz != r_buf_size (buf))) {
|
||||
slice = r_buf_new_slice (buf, opt->loadaddr, opt->sz);
|
||||
} else if (is_debugger && opt->baseaddr != UT64_MAX && opt->baseaddr != 0) {
|
||||
slice = r_buf_new_slice (buf, opt->baseaddr, opt->sz);
|
||||
}
|
||||
if (slice != buf) {
|
||||
r_buf_free (buf);
|
||||
buf = slice;
|
||||
}
|
||||
|
||||
RBinFile *bf = NULL;
|
||||
if (bin->use_xtr && !opt->pluginname && (st64)opt->sz > 0) {
|
||||
if (bin->use_xtr && !opt->pluginname) {
|
||||
// XXX - for the time being this is fine, but we may want to
|
||||
// change the name to something like
|
||||
// <xtr_name>:<bin_type_name>
|
||||
@ -359,29 +351,15 @@ R_API bool r_bin_open_io(RBin *bin, RBinOptions *opt) {
|
||||
if (xtr->check_buffer (buf)) {
|
||||
if (xtr->extract_from_buffer || xtr->extractall_from_buffer ||
|
||||
xtr->extract_from_bytes || xtr->extractall_from_bytes) {
|
||||
if (is_debugger && opt->sz != file_sz) {
|
||||
if (tfd < 0) {
|
||||
tfd = iob->fd_open (io, fname, R_PERM_R, 0);
|
||||
}
|
||||
opt->sz = iob->fd_size (io, tfd);
|
||||
if (opt->sz != UT64_MAX) {
|
||||
r_buf_seek (buf, 0, R_BUF_SET);
|
||||
//buf->base_priv = 0;
|
||||
}
|
||||
// DOUBLECLOSE UAF : iob->fd_close (io, tfd);
|
||||
tfd = -1; // marking it closed
|
||||
}
|
||||
bf = r_bin_file_xtr_load_buffer (bin, xtr,
|
||||
fname, buf, file_sz,
|
||||
opt->baseaddr, opt->loadaddr, opt->xtr_idx,
|
||||
opt->fd, bin->rawstr);
|
||||
fname, buf, opt->baseaddr, opt->loadaddr,
|
||||
opt->xtr_idx, opt->fd, bin->rawstr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!bf) {
|
||||
bf = r_bin_file_new_from_buffer (
|
||||
bin, fname, buf, file_sz, bin->rawstr,
|
||||
bf = r_bin_file_new_from_buffer (bin, fname, buf, bin->rawstr,
|
||||
opt->baseaddr, opt->loadaddr, opt->fd, opt->pluginname);
|
||||
if (!bf) {
|
||||
return false;
|
||||
|
@ -37,6 +37,6 @@ R_IPI void r_bin_class_free(RBinClass *c);
|
||||
R_IPI RBinSymbol *r_bin_class_add_method(RBinFile *binfile, const char *classname, const char *name, int nargs);
|
||||
R_IPI void r_bin_class_add_field(RBinFile *binfile, const char *classname, const char *name);
|
||||
|
||||
R_IPI RBinFile *r_bin_file_xtr_load_buffer(RBin *bin, RBinXtrPlugin *xtr, const char *filename, RBuffer *buf, ut64 file_sz, ut64 baseaddr, ut64 loadaddr, int idx, int fd, int rawstr);
|
||||
R_IPI RBinFile *r_bin_file_new_from_buffer(RBin *bin, const char *file, RBuffer *buf, ut64 file_sz, int rawstr, ut64 baseaddr, ut64 loadaddr, int fd, const char *pluginname);
|
||||
R_IPI RBinFile *r_bin_file_xtr_load_buffer(RBin *bin, RBinXtrPlugin *xtr, const char *filename, RBuffer *buf, ut64 baseaddr, ut64 loadaddr, int idx, int fd, int rawstr);
|
||||
R_IPI RBinFile *r_bin_file_new_from_buffer(RBin *bin, const char *file, RBuffer *buf, int rawstr, ut64 baseaddr, ut64 loadaddr, int fd, const char *pluginname);
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user