Initial huge refactoring of r_io still work in progress

This commit is contained in:
pancake 2014-05-28 04:34:12 +02:00
parent 650c824171
commit 5e33a8a282
29 changed files with 446 additions and 241 deletions

View File

@ -486,8 +486,8 @@ int main(int argc, char **argv, char **envp) {
if (asmbits) r_config_set (r.config, "asm.bits", asmbits);
if (asmos) r_config_set (r.config, "asm.os", asmos);
r_core_bin_update_arch_bits (&r);
debug = r.file && r.file->fd && r.file->fd->plugin && \
r.file->fd->plugin->debug != NULL;
debug = r.file && r.file->desc && r.file->desc->plugin && \
r.file->desc->plugin->debug != NULL;
if (debug) {
r_core_setup_debugger (&r, debugbackend);
}

View File

@ -686,7 +686,7 @@ static int java_analyze_fns( RAnal *anal, ut64 start, ut64 end, int reftype, int
methods_list = (RList *) r_bin_java_get_methods_list (bin);
if (methods_list) {
ut64 loadaddr = bin->loadaddr;
const char * bin_name = bin && bin->file ? bin->file : anal->iob.io->fd->name;
const char * bin_name = bin && bin->file ? bin->file : anal->iob.io->desc->name;
IFDBG eprintf ("Analyzing java functions for %s\n", bin_name);
IFDBG eprintf ("Analyzing functions. binobj = %p, methods_list = %p, Analysing from buffer? %d\n", bin, methods_list, methods_list == NULL);
// loop over all methods in the binary object and analyse

View File

@ -429,7 +429,7 @@ static int cmpaddr (void *_a, void *_b) {
}
static int iscodesection(RCore *core, ut64 addr) {
RIOSection *s = r_io_section_getv (core->io, addr);
RIOSection *s = r_io_section_vget (core->io, addr);
return (s && s->rwx & R_IO_EXEC)? 1: 0;
}
@ -1088,7 +1088,7 @@ R_API int r_core_anal_search(RCore *core, ut64 from, ut64 to, ut64 ref) {
do_bckwrd_srch = bckwrds = core->search->bckwrds;
if (buf==NULL)
return -1;
r_io_set_fd (core->io, core->file->fd);
r_io_use_desc (core->io, core->file->desc);
if (ref==0LL)
eprintf ("Null reference search is not supported\n");
else

View File

@ -139,13 +139,13 @@ static void cmd_debug_pid(RCore *core, const char *input) {
if (input[2]) {
r_debug_attach (core->dbg, (int) r_num_math (
core->num, input+2));
} else r_debug_attach (core->dbg, core->file->fd->fd);
} else r_debug_attach (core->dbg, core->file->desc->fd);
r_debug_select (core->dbg, core->dbg->pid, core->dbg->tid);
r_config_set_i (core->config, "dbg.swstep",
(core->dbg->h && !core->dbg->h->canstep));
break;
case 'f':
r_debug_select (core->dbg, core->file->fd->fd, core->dbg->tid);
r_debug_select (core->dbg, core->file->desc->fd, core->dbg->tid);
break;
case '=':
r_debug_select (core->dbg,
@ -1037,7 +1037,7 @@ static int cmd_debug(void *data, const char *input) {
r_io_read_at (core->io, addr, buf, sizeof (buf));
r_anal_op (core->anal, &aop, addr, buf, sizeof (buf));
if (aop.type == R_ANAL_OP_TYPE_CALL) {
RIOSection *s = r_io_section_get (core->io, aop.jump);
RIOSection *s = r_io_section_vget (core->io, aop.jump);
if (!s) {
r_debug_step_over (core->dbg, times);
continue;
@ -1184,7 +1184,7 @@ static int cmd_debug(void *data, const char *input) {
r_debug_reg_sync (core->dbg, R_REG_TYPE_GPR, R_FALSE);
pc = r_debug_reg_get (core->dbg, "pc");
eprintf (" %d %"PFMT64x"\r", n++, pc);
s = r_io_section_get (core->io, pc);
s = r_io_section_vget (core->io, pc);
if (r_cons_singleton ()->breaked)
break;
} while (!s);

View File

@ -321,7 +321,7 @@ static int cmd_help(void *data, const char *input) {
ut64 n = (input[0] && input[1])?
r_num_math (core->num, input+2): core->offset;
n = r_io_section_vaddr_to_offset (core->io, n);
s = r_io_section_get (core->io, n);
s = r_io_section_vget (core->io, n);
if (s && *(s->name))
r_cons_printf ("%s\n", s->name);
} break;

View File

@ -43,7 +43,7 @@ static void r_core_file_info (RCore *core, int mode) {
if (cf && mode == R_CORE_BIN_JSON) {
r_cons_printf ("\"file\":\"%s\"", fn);
if (dbg) dbg = R_IO_WRITE | R_IO_EXEC;
r_cons_printf (",\"fd\":%d", cf->fd->fd);
r_cons_printf (",\"fd\":%d", cf->desc->fd);
r_cons_printf (",\"size\":%d", cf->size);
r_cons_printf (",\"mode\":\"%s\"", r_str_rwx_i (
cf->rwx | dbg));
@ -62,7 +62,7 @@ static void r_core_file_info (RCore *core, int mode) {
//r_cons_printf ("# Core file info\n");
r_cons_printf ("file\t%s\n", fn);
if (dbg) dbg = R_IO_WRITE | R_IO_EXEC;
r_cons_printf ("fd\t%d\n", cf->fd->fd);
r_cons_printf ("fd\t%d\n", cf->desc->fd);
r_cons_printf ("size\t0x%x\n", cf->size);
r_cons_printf ("mode\t%s\n", r_str_rwx_i (cf->rwx | dbg));
r_cons_printf ("block\t0x%x\n", core->blocksize);

View File

@ -45,7 +45,7 @@ static int cmd_open(void *data, const char *input) {
RCoreFile *f;
core->switch_file_view = 0;
r_list_foreach (core->files, iter, f) {
if (f->fd->fd == num) {
if (f->desc->fd == num) {
r_io_raise (core->io, num);
core->switch_file_view = 1;
break;
@ -176,7 +176,7 @@ static int cmd_open(void *data, const char *input) {
} else {
cur = core->offset;
new = r_num_math (core->num, input+3);
map = r_io_map_resolve (core->io, core->file->fd->fd);
map = r_io_map_resolve (core->io, core->file->desc->fd);
if (map) {
ut64 diff = map->to - map->from;
map->from = new;

View File

@ -16,6 +16,7 @@ static int cmd_section(void *data, const char *input) {
"| Sr [name] ; rename section on current seek\n"
"| S [off] [vaddr] [sz] [vsz] [name] [rwx] ; add new section\n"
"| S-[id|0xoff|*] ; remove this section definition\n");
// TODO: add command to resize current section
break;
case 'a':
switch (input[1]) {
@ -66,13 +67,13 @@ static int cmd_section(void *data, const char *input) {
if (input[1]==' ') {
RIOSection *s;
int len = 0;
ut64 addr;
ut64 vaddr;
char *p = strchr (input+2, ' ');
if (p) {
addr = r_num_math (core->num, p+1);
vaddr = r_num_math (core->num, p+1);
len = (int)(size_t)(p-input+2);
} else addr = core->offset;
s = r_io_section_get (core->io, addr);
} else vaddr = core->offset;
s = r_io_section_vget (core->io, vaddr);
if (s) {
if (!len) len = sizeof (s->name);
r_str_ncpy (s->name, input+2, len);
@ -143,9 +144,8 @@ static int cmd_section(void *data, const char *input) {
r_io_section_init (core->io);
} else
if (input[1] == '0' && input[2]=='x') {
RIOSection *s = r_io_section_get (core->io, r_num_get (NULL, input+1));
if (!s)
return 0;
RIOSection *s = r_io_section_vget (core->io, r_num_get (NULL, input+1));
if (!s) return 0;
// use offset
r_io_section_rm (core->io, s->id);
} else {

View File

@ -242,18 +242,14 @@ static int cmd_seek(void *data, const char *input) {
break;
case 'g':
{
RIOSection *s = r_io_section_get (core->io,
r_io_section_vaddr_to_offset (core->io,
core->offset));
RIOSection *s = r_io_section_vget (core->io, core->offset);
if (s) r_core_seek (core, s->vaddr, 1);
else r_core_seek (core, 0, 1);
}
break;
case 'G':
{
RIOSection *s = r_io_section_get (core->io,
r_io_section_vaddr_to_offset (core->io,
core->offset));
RIOSection *s = r_io_section_vget (core->io, core->offset);
// XXX: this +2 is a hack. must fix gap between sections
if (s) r_core_seek (core, s->vaddr+s->size+2, 1);
else r_core_seek (core, core->file->size, 1);

View File

@ -329,7 +329,7 @@ static int cmd_write(void *data, const char *input) {
len = r_str_unescape (str);
r_core_write_at (core, core->offset, (const ut8*)str, len);
#if 0
r_io_set_fd (core->io, core->file->fd);
r_io_use_desc (core->io, core->file->desc);
r_io_write_at (core->io, core->offset, (const ut8*)str, len);
#endif
WSEEK (core, len);
@ -362,7 +362,7 @@ static int cmd_write(void *data, const char *input) {
}
} else
if ((buf = (ut8*) r_file_slurp (arg, &size))) {
r_io_set_fd (core->io, core->file->fd);
r_io_use_desc (core->io, core->file->desc);
r_io_write_at (core->io, core->offset, buf, size);
WSEEK (core, size);
free (buf);
@ -387,7 +387,7 @@ static int cmd_write(void *data, const char *input) {
}
} else
if ((buf = r_file_slurp_hexpairs (arg, &size))) {
r_io_set_fd (core->io, core->file->fd);
r_io_use_desc (core->io, core->file->desc);
r_io_write_at (core->io, core->offset, buf, size);
WSEEK (core, size);
free (buf);
@ -405,7 +405,7 @@ static int cmd_write(void *data, const char *input) {
else tmp[i] = str[i>>1];
}
str = tmp;
r_io_set_fd (core->io, core->file->fd);
r_io_use_desc (core->io, core->file->desc);
r_io_write_at (core->io, core->offset, (const ut8*)str, len);
WSEEK (core, len);
r_core_block_read (core, 0);
@ -519,7 +519,7 @@ static int cmd_write(void *data, const char *input) {
break;
case ' ':
if (size>0) {
r_io_set_fd (core->io, core->file->fd);
r_io_use_desc (core->io, core->file->desc);
r_io_set_write_mask (core->io, (const ut8*)str, size);
WSEEK (core, size);
eprintf ("Write mask set to '");
@ -551,7 +551,7 @@ static int cmd_write(void *data, const char *input) {
case '8': type = 8; break;
}
off = r_num_math (core->num, input+2);
r_io_set_fd (core->io, core->file->fd);
r_io_use_desc (core->io, core->file->desc);
r_io_seek (core->io, core->offset, R_IO_SEEK_SET);
if (type == 0)
type = (off&UT64_32U)? 8: 4;
@ -666,7 +666,7 @@ static int cmd_write(void *data, const char *input) {
case '?':
if (core->oobi) {
eprintf ("Writing oobi buffer!\n");
r_io_set_fd (core->io, core->file->fd);
r_io_use_desc (core->io, core->file->desc);
r_io_write (core->io, core->oobi, core->oobi_len);
WSEEK (core, core->oobi_len);
r_core_block_read (core, 0);

View File

@ -92,7 +92,7 @@ static int cmd_zign(void *data, const char *input) {
fin = ini+r_num_math (core->num, input+2);
}
} else {
s = r_io_section_get (core->io, core->io->off);
s = r_io_section_vget (core->io, core->io->off);
if (s) {
ini = core->io->va?s->vaddr:s->offset;
fin = ini + (core->io->va?s->vsize:s->size);

View File

@ -316,8 +316,8 @@ static int cb_cfgdebug(void *user, void *data) {
if (!strcmp (dbgbackend, "bf"))
r_config_set (core->config, "asm.arch", "bf");
if (core->file) {
r_debug_select (core->dbg, core->file->fd->fd,
core->file->fd->fd);
r_debug_select (core->dbg, core->file->desc->fd,
core->file->desc->fd);
}
} else if (core->dbg) r_debug_use (core->dbg, NULL);
r_config_set (core->config, "io.raw", "true");

View File

@ -130,7 +130,7 @@ static ut64 num_callback(RNum *userptr, const char *str, int *ok) {
case 's': return core->file->size;
case 'w': return r_config_get_i (core->config, "asm.bits") / 8;
case 'S':
s = r_io_section_get (core->io,
s = r_io_section_vget (core->io,
r_io_section_vaddr_to_offset (core->io,
core->offset));
return s? (str[2]=='S'? s->size: s->offset): 0;
@ -899,8 +899,9 @@ reaccept:
file = r_core_file_open (core, (const char *)ptr, R_IO_READ, 0); // XXX: write mode?
if (file) {
r_core_bin_load (core, NULL, baddr);
file->map = r_io_map_add (core->io, file->fd->fd, R_IO_READ, 0, 0, file->size);
pipefd = core->file->fd->fd;
file->map = r_io_map_add (core->io, file->desc->fd,
R_IO_READ, 0, 0, file->size);
pipefd = core->file->desc->fd;
eprintf ("(flags: %d) len: %d filename: '%s'\n",
flg, cmd, ptr); //config.file);
} else {

View File

@ -23,8 +23,8 @@ R_API int r_core_file_reopen(RCore *core, const char *args, int perm) {
RCoreFile *file = NULL,
*ofile = core->file;
RBinFile *bf = ofile && ofile->fd ? r_bin_file_find_by_fd (core->bin, ofile->fd->fd) : NULL;
RIODesc *odesc = ofile ? ofile->fd : NULL;
RBinFile *bf = ofile && ofile->desc ? r_bin_file_find_by_fd (core->bin, ofile->desc->fd) : NULL;
RIODesc *odesc = ofile ? ofile->desc : NULL;
char *ofilepath = ofile ? strdup(ofile->uri) : NULL,
*obinfilepath = bf ? strdup(bf->file) : NULL;
int newpid, ret = R_FALSE;
@ -72,8 +72,8 @@ R_API int r_core_file_reopen(RCore *core, const char *args, int perm) {
}
/*
if (core->bin->cur && file->fd) {
core->bin->cur->fd = file->fd->fd;
if (core->bin->cur && file->desc) {
core->bin->cur->fd = file->desc->fd;
ret = R_TRUE;
}*/
// close old file
@ -89,15 +89,15 @@ R_API int r_core_file_reopen(RCore *core, const char *args, int perm) {
// TODO: in debugger must select new PID
if (r_config_get_i (core->config, "cfg.debug")) {
// XXX - select the right backend
if (core->file && core->file->fd)
newpid = core->file->fd->fd;
if (core->file && core->file->desc)
newpid = core->file->desc->fd;
r_debug_select (core->dbg, newpid, newpid);
r_core_setup_debugger (core, "native");
}
if (core->file) {
RCoreFile * cf = core->file;
RIODesc *desc = cf ? cf->fd : NULL;
RIODesc *desc = cf ? cf->desc : NULL;
if (desc){
r_io_raise (core->io, desc->fd);
core->switch_file_view = 1;
@ -225,7 +225,7 @@ static ut64 get_base_from_maps(RCore *core, const char *file) {
R_API int r_core_bin_reload(RCore *r, const char *file, ut64 baseaddr) {
int result = 0;
RCoreFile *cf = r_core_file_cur (r);
RIODesc *desc = cf ? cf->fd : NULL;
RIODesc *desc = cf ? cf->desc : NULL;
RBinFile *bf = NULL;
if (desc) result = r_bin_reload (r->bin, desc, baseaddr);
@ -237,7 +237,7 @@ R_API int r_core_bin_reload(RCore *r, const char *file, ut64 baseaddr) {
// XXX - need to handle index selection during debugging
static int r_core_file_do_load_for_debug (RCore *r, ut64 loadaddr, const char *filenameuri) {
RCoreFile *cf = r_core_file_cur (r);
RIODesc *desc = cf ? cf->fd : NULL;
RIODesc *desc = cf ? cf->desc : NULL;
RBinFile *binfile = NULL;
RBinPlugin *plugin;
ut64 baseaddr = 0;
@ -288,13 +288,13 @@ static int r_core_file_do_load_for_debug (RCore *r, ut64 loadaddr, const char *f
static int r_core_file_do_load_for_io_plugin (RCore *r, ut64 baseaddr, ut64 loadaddr) {
RCoreFile *cf = r_core_file_cur (r);
RIODesc *desc = cf ? cf->fd : NULL;
RIODesc *desc = cf ? cf->desc : NULL;
RBinFile *binfile = NULL;
int xtr_idx = 0; // if 0, load all if xtr is used
RBinPlugin * plugin;
if (!desc) return R_FALSE;
r_io_set_fd (r->io, desc);
r_io_use_desc (r->io, desc);
if ( !r_bin_load_io (r->bin, desc, baseaddr, loadaddr, xtr_idx)) {
//eprintf ("Failed to load the bin with an IO Plugin.\n");
return R_FALSE;
@ -391,7 +391,7 @@ R_API int r_core_bin_load(RCore *r, const char *filenameuri, ut64 baddr) {
ut64 loadaddr = 0;
RCoreFile *cf = r_core_file_cur (r);
RBinFile *binfile = NULL;
RIODesc *desc = cf ? cf->fd : NULL;
RIODesc *desc = cf ? cf->desc : NULL;
RBinPlugin *plugin = NULL;
int is_io_load = desc && desc->plugin;
@ -461,11 +461,11 @@ R_API RIOMap *r_core_file_get_next_map (RCore *core, RCoreFile * fh, int mode, u
ut64 load_align = r_config_get_i (core->config, "file.loadalign");
RIOMap *map = NULL;
if (!strcmp (loadmethod, "overwrite"))
map = r_io_map_new (core->io, fh->fd->fd, mode, 0, loadaddr, fh->size);
map = r_io_map_new (core->io, fh->desc->fd, mode, 0, loadaddr, fh->size);
if (!strcmp (loadmethod, "fail"))
map = r_io_map_add (core->io, fh->fd->fd, mode, 0, loadaddr, fh->size);
map = r_io_map_add (core->io, fh->desc->fd, mode, 0, loadaddr, fh->size);
if (!strcmp (loadmethod, "append") && load_align)
map = r_io_map_add_next_available (core->io, fh->fd->fd, mode, 0, loadaddr, fh->size, load_align);
map = r_io_map_add_next_available (core->io, fh->desc->fd, mode, 0, loadaddr, fh->size, load_align);
if (!strcmp (suppress_warning, "false")) {
if (!map)
eprintf ("r_core_file_get_next_map: Unable to load specified file to 0x%08"PFMT64x"\n", loadaddr);
@ -522,7 +522,7 @@ R_API RCoreFile *r_core_file_open_many(RCore *r, const char *file, int mode, ut6
fh->alive = 1;
fh->core = r;
fh->uri = strdup (file);
fh->fd = fd;
fh->desc = fd;
fh->size = r_io_desc_size (r->io, fd);
fh->filename = strdup (fd->name);
fh->rwx = mode;
@ -606,7 +606,7 @@ R_API RCoreFile *r_core_file_open(RCore *r, const char *file, int mode, ut64 loa
fh->alive = 1;
fh->core = r;
fh->uri = strdup (file);
fh->fd = fd;
fh->desc = fd;
fh->size = r_io_desc_size (r->io, fd);
fh->filename = strdup (fd->name);
fh->rwx = mode;
@ -645,11 +645,11 @@ R_API void r_core_file_free(RCoreFile *cf) {
if (!res && cf->alive) {
// double free libr/io/io.c:70 performs free
cf->alive = 0;
const RIO *io = cf->fd ? cf->fd->io : NULL;
const RIO *io = cf->desc ? cf->desc->io : NULL;
if (io && cf->map) r_io_map_del_at ((RIO *)io, cf->map->from);
if (io) r_io_close ((RIO *) io, cf->fd);
cf->fd = NULL;
if (io) r_io_close ((RIO *) io, cf->desc);
cf->desc = NULL;
cf->map = NULL;
cf->filename = NULL;
@ -664,7 +664,7 @@ R_API void r_core_file_free(RCoreFile *cf) {
}
R_API int r_core_file_close(RCore *r, RCoreFile *fh) {
RIODesc *desc = fh && fh->fd? fh->fd : NULL;
RIODesc *desc = fh && fh->desc? fh->desc : NULL;
RCoreFile *prev_cf = r->file != fh ? r->file : NULL;
// XXX -these checks are intended to *try* and catch
// stale objects. Unfortunately, if the file handle
@ -685,7 +685,7 @@ R_API int r_core_file_close(RCore *r, RCoreFile *fh) {
prev_cf = (RCoreFile *) r_list_get_n (r->files, 0);
if (prev_cf) {
RIODesc *desc = prev_cf->fd;
RIODesc *desc = prev_cf->desc;
if (!desc)
eprintf ("Error: RCoreFile's found with out a supporting RIODesc.\n");
ret = r_core_file_set_by_file (r, prev_cf);
@ -699,7 +699,7 @@ R_API RCoreFile *r_core_file_get_by_fd(RCore *core, int fd) {
RCoreFile *file;
RListIter *iter;
r_list_foreach (core->files, iter, file) {
if (file->fd->fd == fd)
if (file->desc->fd == fd)
return file;
}
return NULL;
@ -712,10 +712,10 @@ R_API int r_core_file_list(RCore *core) {
r_list_foreach (core->files, iter, f) {
if (f->map)
r_cons_printf ("%c %d %s @ 0x%"PFMT64x" ; %s\n",
core->io->raised == f->fd->fd?'*':'-',
f->fd->fd, f->uri, f->map->from,
f->fd->flags & R_IO_WRITE? "rw": "r");
else r_cons_printf ("- %d %s\n", f->fd->fd, f->uri);
core->io->raised == f->desc->fd?'*':'-',
f->desc->fd, f->uri, f->map->from,
f->desc->flags & R_IO_WRITE? "rw": "r");
else r_cons_printf ("- %d %s\n", f->desc->fd, f->uri);
count++;
}
return count;
@ -747,13 +747,13 @@ R_API int r_core_file_binlist(RCore *core) {
if (!binfiles) return R_FALSE;
r_list_foreach (binfiles, iter, binfile) {
ut32 fd = binfile->fd;
int fd = binfile->fd;
cf = r_core_file_get_by_fd (core, fd);
if (cf && cf->map) {
r_cons_printf ("%c %d %s @ 0x%"PFMT64x" ; %s\n",
core->io->raised == cf->fd->fd?'*':'-',
core->io->raised == cf->desc->fd?'*':'-',
fd, cf->uri, cf->map->from,
cf->fd->flags & R_IO_WRITE? "rw": "r");
cf->desc->flags & R_IO_WRITE? "rw": "r");
}
}
r_core_file_set_by_file (core, cur_cf);
@ -765,7 +765,7 @@ R_API int r_core_file_close_fd(RCore *core, int fd) {
RCoreFile *file;
RListIter *iter;
r_list_foreach (core->files, iter, file) {
if (file->fd->fd == fd) {
if (file->desc->fd == fd) {
r_core_file_close (core, file);
#if 0
if (r_list_empty (core->files))
@ -821,7 +821,7 @@ R_API RCoreFile * r_core_file_find_by_fd (RCore *core, ut64 fd) {
RCoreFile *cf = NULL;
r_list_foreach (core->files, iter, cf) {
if (cf && cf->fd && cf->fd->fd == fd) break;
if (cf && cf->desc && cf->desc->fd == fd) break;
cf = NULL;
}
return cf;
@ -850,11 +850,11 @@ R_API int r_core_file_set_by_name (RCore * core, const char * name) {
R_API int r_core_file_set_by_file (RCore * core, RCoreFile *cf) {
if (cf) {
RIODesc *desc = cf->fd;
RIODesc *desc = cf->desc;
core->offset = cf && cf->map ? cf->map->from : 0;
core->file = cf;
if (desc) {
r_io_set_fdn (core->io, desc->fd);
r_io_use_fd (core->io, desc->fd);
r_core_bin_set_by_fd (core, desc->fd);
//r_core_bin_bind (core, NULL);
}
@ -864,7 +864,7 @@ R_API int r_core_file_set_by_file (RCore * core, RCoreFile *cf) {
}
R_API ut32 r_core_file_cur_fd (RCore *core) {
RIODesc *desc = core->file ? core->file->fd : NULL;
RIODesc *desc = core->file ? core->file->desc : NULL;
if (desc) {
return desc->fd;
}

View File

@ -6,7 +6,7 @@ R_API int r_core_setup_debugger (RCore *r, const char *debugbackend) {
int pid, *p = NULL;
ut8 is_gdb = (strcmp (debugbackend, "gdb") == 0);
RIODesc * fd = r->file ? r->file->fd : NULL;
RIODesc * fd = r->file ? r->file->desc : NULL;
p = fd ? fd->data : NULL;
r_config_set_i (r->config, "cfg.debug", 1);
if (!p) {
@ -217,7 +217,7 @@ R_API boolt r_core_seek(RCore *core, ut64 addr, boolt rb) {
ut64 ret;
/* XXX unnecesary call */
//r_io_set_fd (core->io, core->file->fd);
//r_io_use_fd (core->io, core->file->desc);
core->io->section = core->section; // HACK
ret = r_io_seek (core->io, addr, R_IO_SEEK_SET);
newsection = core->io->section;
@ -284,7 +284,7 @@ R_API int r_core_write_at(RCore *core, ut64 addr, const ut8 *buf, int size) {
int ret;
if (!core->io || !core->file || size<1)
return R_FALSE;
ret = r_io_set_fd (core->io, core->file->fd);
ret = r_io_use_fd (core->io, core->file->desc);
if (ret != -1) {
ret = r_io_write_at (core->io, addr, buf, size);
if (addr >= core->offset && addr <= core->offset+core->blocksize)
@ -298,7 +298,7 @@ R_API int r_core_extend_at(RCore *core, ut64 addr, int size) {
int ret;
if (!core->io || !core->file || size<1)
return R_FALSE;
ret = r_io_set_fd (core->io, core->file->fd);
ret = r_io_use_fd (core->io, core->file->desc);
if (ret != -1) {
ret = r_io_extend_at (core->io, addr, size);
if (addr >= core->offset && addr <= core->offset+core->blocksize)
@ -315,7 +315,7 @@ R_API int r_core_shift_block(RCore *core, ut64 addr, ut64 b_size, st64 dist) {
int res = R_FALSE;
if (b_size == 0 || b_size == (ut64) -1) {
res = r_io_set_fd (core->io, core->file->fd);
res = r_io_use_fd (core->io, core->file->desc);
file_sz = r_io_size (core->io);
bstart = r_io_seek (core->io, addr, R_IO_SEEK_SET);
fend = r_io_seek (core->io, 0, R_IO_SEEK_END);
@ -349,7 +349,7 @@ R_API int r_core_shift_block(RCore *core, ut64 addr, ut64 b_size, st64 dist) {
else if ( (addr) + dist > fend) {
res = R_FALSE;
} else {
res = r_io_set_fd (core->io, core->file->fd);
res = r_io_use_fd (core->io, core->file->desc);
r_io_read_at (core->io, addr, shift_buf, b_size);
r_io_write_at (core->io, addr+dist, shift_buf, b_size);
res = R_TRUE;
@ -365,8 +365,8 @@ static RCoreFile * r_core_file_set_first_valid(RCore *core) {
RCoreFile *file = NULL;
r_list_foreach (core->files, iter, file) {
if (file && file->fd){
core->io->raised = file->fd->fd;
if (file && file->desc){
core->io->raised = file->desc->fd;
core->switch_file_view = 1;
break;
}
@ -380,7 +380,7 @@ R_API int r_core_block_read(RCore *core, int next) {
memset (core->block, 0xff, core->blocksize);
return -1;
}
r_io_set_fdn (core->io, core->io->raised);
r_io_use_fd (core->io, core->io->raised);
if (core->switch_file_view) {
r_core_sync_view_by_fd (core, core->io->raised);
core->switch_file_view = 0;
@ -404,12 +404,12 @@ R_API int r_core_read_at(RCore *core, ut64 addr, ut8 *buf, int size) {
if (!core->io || !core->file || size<1)
return R_FALSE;
#if 0
r_io_set_fd (core->io, core->file->fd); // XXX ignore ret? -- ultra slow method.. inverse resolution of io plugin brbrb
r_io_use_fd (core->io, core->file->desc); // XXX ignore ret? -- ultra slow method.. inverse resolution of io plugin brbrb
ret = r_io_read_at (core->io, addr, buf, size);
if (addr>=core->offset && addr<=core->offset+core->blocksize)
r_core_block_read (core, 0);
#else
r_io_set_fd (core->io, core->file->fd); // XXX ignore ret? -- ultra slow method.. inverse resolution of io plugin brbrb
r_io_use_fd (core->io, core->file->desc); // XXX ignore ret? -- ultra slow method.. inverse resolution of io plugin brbrb
//ret = r_io_read_at (core->io, addr, buf, size);
r_io_seek (core->io, addr, R_IO_SEEK_SET);
ret = r_io_read (core->io, buf, size);

View File

@ -522,7 +522,7 @@ static int r_cmd_java_get_cp_bytes_and_write (RCore *core, RBinJavaObj *obj, ut1
if (res == R_TRUE) {
ut64 n_file_sz = 0;
ut8 * bin_buffer = NULL;
res = r_io_set_fd (core->io, core->file->fd);
res = r_io_use_desc (core->io, core->file->desc);
n_file_sz = r_io_size (core->io);
bin_buffer = n_file_sz > 0 ? malloc (n_file_sz) : NULL;
@ -847,7 +847,7 @@ static int r_cmd_java_handle_reload_bin (RCore *core, const char *cmd) {
// XXX this may cause problems cause the file we are looking at may not be the bin we want.
// lets pretend it is for now
if (buf_size == 0) {
res = r_io_set_fd (core->io, core->file->fd);
res = r_io_use_desc (core->io, core->file->desc);
buf_size = r_io_size (core->io);
buf = malloc (buf_size);
memset (buf, 0, buf_size);

View File

@ -37,7 +37,7 @@ static int perform_mapped_file_yank (RCore *core, ut64 offset, ut64 len, const c
// grab the current file descriptor, so we can reset core and io state
// after our io op is done
RIODesc *yankfd = NULL;
ut64 fd = core->file ? core->file->fd->fd : -1, yank_file_sz = 0,
ut64 fd = core->file ? core->file->desc->fd : -1, yank_file_sz = 0,
loadaddr = 0, addr = offset;
int res = R_FALSE;

View File

@ -7,11 +7,11 @@
#include "bfvm.c"
typedef struct {
int fd;
int desc;
ut8 *buf;
ut32 size;
BfvmCPU *bfvm;
} RIOBfdbg;
} RIOBdescbg;
struct bfvm_regs {
ut32 pc;
@ -28,15 +28,15 @@ struct bfvm_regs {
static struct bfvm_regs r;
static int is_io_bf(RDebug *dbg) {
RIODesc *d = dbg->iob.io->fd;
RIODesc *d = dbg->iob.io->desc;
if (d && d->plugin && d->plugin->name)
if (!strcmp ("bfdbg", d->plugin->name))
if (!strcmp ("bdescbg", d->plugin->name))
return R_TRUE;
return R_FALSE;
}
static int r_debug_bf_step_over(RDebug *dbg) {
RIOBfdbg *o = dbg->iob.io->fd->data;
RIOBdescbg *o = dbg->iob.io->desc->data;
int op, oop = 0;
for (;;) {
op = bfvm_op (o->bfvm);
@ -51,20 +51,20 @@ static int r_debug_bf_step_over(RDebug *dbg) {
}
static int r_debug_bf_step(RDebug *dbg) {
RIOBfdbg *o = dbg->iob.io->fd->data;
RIOBdescbg *o = dbg->iob.io->desc->data;
bfvm_step (o->bfvm, 0);
return R_TRUE;
}
static int r_debug_bf_reg_read(RDebug *dbg, int type, ut8 *buf, int size) {
RIOBfdbg *o;
RIOBdescbg *o;
if (!dbg)
return R_FALSE;
if (!is_io_bf (dbg))
return 0;
if (!(dbg->iob.io) || !(dbg->iob.io->fd) || !(dbg->iob.io->fd->data))
if (!(dbg->iob.io) || !(dbg->iob.io->desc) || !(dbg->iob.io->desc->data))
return 0;
o = dbg->iob.io->fd->data;
o = dbg->iob.io->desc->data;
r.pc = o->bfvm->eip;
r.ptr = o->bfvm->ptr;
r.sp = o->bfvm->esp;
@ -80,14 +80,14 @@ static int r_debug_bf_reg_read(RDebug *dbg, int type, ut8 *buf, int size) {
}
static int r_debug_bf_reg_write(RDebug *dbg, int type, const ut8 *buf, int size) {
RIOBfdbg *o;
RIOBdescbg *o;
if (!dbg)
return R_FALSE;
if (!is_io_bf (dbg))
return 0;
if (!(dbg->iob.io) || !(dbg->iob.io->fd) || !(dbg->iob.io->fd->data))
if (!(dbg->iob.io) || !(dbg->iob.io->desc) || !(dbg->iob.io->desc->data))
return 0;
o = dbg->iob.io->fd->data;
o = dbg->iob.io->desc->data;
memcpy (&r, buf, sizeof (r));
o->bfvm->eip = r.pc;
o->bfvm->ptr = r.ptr; // dup
@ -102,13 +102,13 @@ static int r_debug_bf_reg_write(RDebug *dbg, int type, const ut8 *buf, int size)
}
static int r_debug_bf_continue(RDebug *dbg, int pid, int tid, int sig) {
RIOBfdbg *o = dbg->iob.io->fd->data;
RIOBdescbg *o = dbg->iob.io->desc->data;
bfvm_cont (o->bfvm, UT64_MAX);
return R_TRUE;
}
static int r_debug_bf_continue_syscall(RDebug *dbg, int pid, int num) {
RIOBfdbg *o = dbg->iob.io->fd->data;
RIOBdescbg *o = dbg->iob.io->desc->data;
bfvm_contsc (o->bfvm);
return R_TRUE;
}
@ -122,8 +122,8 @@ static int r_debug_bf_attach(RDebug *dbg, int pid) {
if (!is_io_bf (dbg))
return R_FALSE;
#if 0
RIOBfdbg *o;
o = dbg->iob.io->fd->data;
RIOBdescbg *o;
o = dbg->iob.io->desc->data;
eprintf ("base = %llx\n", o->bfvm->base);
eprintf ("screen = %llx\n", o->bfvm->screen);
eprintf ("input = %llx\n", o->bfvm->input);
@ -159,13 +159,13 @@ static int r_debug_bf_breakpoint (void *user, int type, ut64 addr, int hw, int r
}
static int r_debug_bf_kill(RDebug *dbg, int pid, int tid, int sig) {
RIOBfdbg *o = dbg->iob.io->fd->data;
RIOBdescbg *o = dbg->iob.io->desc->data;
bfvm_reset (o->bfvm);
return R_TRUE;
}
static RList *r_debug_native_map_get(RDebug *dbg) {
RIOBfdbg *o = dbg->iob.io->fd->data;
RIOBdescbg *o = dbg->iob.io->desc->data;
BfvmCPU *c = o->bfvm;
RList *list = r_list_new ();
list->free = (RListFree)r_debug_map_free;
@ -181,7 +181,7 @@ static RList *r_debug_native_map_get(RDebug *dbg) {
}
static int r_debug_bf_stop(RDebug *dbg) {
RIOBfdbg *o = dbg->iob.io->fd->data;
RIOBdescbg *o = dbg->iob.io->desc->data;
BfvmCPU *c = o->bfvm;
c->breaked = R_TRUE;
return R_TRUE;

View File

@ -5,7 +5,7 @@
#include <r_debug.h>
static int is_io_esil(RDebug *dbg) {
RIODesc *d = dbg->iob.io->fd;
RIODesc *d = dbg->iob.io->desc;
if (d && d->plugin && d->plugin->name)
if (!strcmp ("esil", d->plugin->name))
return R_TRUE;
@ -67,8 +67,8 @@ static int r_debug_esil_attach(RDebug *dbg, int pid) {
if (!is_io_esil (dbg))
return R_FALSE;
#if 0
RIOBfdbg *o;
o = dbg->iob.io->fd->data;
RIOBdescbg *o;
o = dbg->iob.io->desc->data;
eprintf ("base = %llx\n", o->bfvm->base);
eprintf ("screen = %llx\n", o->bfvm->screen);
eprintf ("input = %llx\n", o->bfvm->input);

View File

@ -56,7 +56,7 @@ static int r_debug_gdb_wait(RDebug *dbg, int pid) {
static int r_debug_gdb_attach(RDebug *dbg, int pid) {
RIODesc *d = dbg->iob.io->fd;
RIODesc *d = dbg->iob.io->desc;
if (d && d->plugin && d->plugin->name && d->data) {
if (!strcmp ("gdb", d->plugin->name)) {
RIOGdb *g = d->data;

View File

@ -30,7 +30,7 @@ static int r_debug_rap_wait(RDebug *dbg, int pid) {
static int r_debug_rap_attach(RDebug *dbg, int pid) {
// XXX TODO PID must be a socket here !!1
RIODesc *d = dbg->iob.io->fd;
RIODesc *d = dbg->iob.io->desc;
if (d && d->plugin && d->plugin->name) {
if (!strcmp ("rap", d->plugin->name)) {
eprintf ("SUCCESS: rap attach with inferior rap rio worked\n");

View File

@ -80,7 +80,7 @@ typedef struct r_core_file_t {
RIOMap *map;
int rwx;
int dbg;
RIODesc *fd;
RIODesc *desc;
RBinBind binb;
const struct r_core_t *core;
ut8 alive;

View File

@ -105,7 +105,7 @@ typedef struct r_io_undo_w_t {
} RIOUndoWrite;
typedef struct r_io_t {
RIODesc *fd;
RIODesc *desc;
int enforce_rwx;
int enforce_seek;
int cached;
@ -131,7 +131,7 @@ typedef struct r_io_t {
RIOSection *section; /* current section (cache) */
/* maps */
RList *maps; /*<RIOMap>*/
RList *desc;
RList *files;
RList *cache;
int zeromap;
//XXX: Need by rap
@ -139,6 +139,7 @@ typedef struct r_io_t {
int (*core_cmd_cb)(void *user, const char *str);
RCache *buffer;
int buffer_enabled;
int ff;
} RIO;
typedef struct r_io_plugin_t {
@ -273,10 +274,17 @@ R_API RIODesc *r_io_open(RIO *io, const char *file, int flags, int mode);
R_API RList *r_io_open_many(RIO *io, const char *file, int flags, int mode);
R_API RIODesc *r_io_open_as(RIO *io, const char *urihandler, const char *file, int flags, int mode);
R_API int r_io_redirect(RIO *io, const char *file);
// TODO: deprecate
R_API int r_io_set_fd(RIO *io, RIODesc *fd);
R_API int r_io_set_fdn(RIO *io, int fd);
R_API RIODesc *r_io_use_fd (RIO *io, int fd);
R_API int r_io_use_desc(RIO *io, RIODesc *fd);
R_API const ut8* r_io_get_raw (RIO *io, ut64 addr, int *len);
R_API RBuffer *r_io_read_buf(RIO *io, ut64 addr, int len);
R_API int r_io_vread (RIO *io, ut64 vaddr, ut8 *buf, int len);
R_API int r_io_read(RIO *io, ut8 *buf, int len);
R_API int r_io_read_at(RIO *io, ut64 addr, ut8 *buf, int len);
R_API ut64 r_io_read_i(RIO *io, ut64 addr, int sz, int endian);
@ -337,6 +345,7 @@ R_API RIOSection *r_io_section_get_name(RIO *io, const char *name);
R_API RIOSection *r_io_section_get_i(RIO *io, int idx);
R_API RIOSection *r_io_section_getv(RIO *io, ut64 vaddr);
R_API RIOSection *r_io_section_vget(RIO *io, ut64 addr);
R_API RIOSection *r_io_section_pget(RIO *io, ut64 addr);
R_API int r_io_section_set_archbits(RIO *io, ut64 addr, const char *arch, int bits);
R_API const char *r_io_section_get_archbits(RIO* io, ut64 addr, int *bits);
R_API void r_io_section_clear(RIO *io);

View File

@ -6,6 +6,8 @@ STATIC_OBJS=$(subst ..,p/..,$(subst io_,p/io_,$(STATIC_OBJ)))
OBJS=${STATIC_OBJS}
OBJS+=io.o plugin.o map.o section.o desc.o cache.o undo.o buffer.o
OBJS+=vio.o
CFLAGS+=-Wall -DCORELIB
include ../socket/deps.mk

View File

@ -4,28 +4,27 @@
// TODO: to be deprecated.. this is slow and boring
R_API void r_io_desc_init(RIO *io) {
io->desc = r_list_new ();
io->desc->free = (RListFree)r_io_desc_free;
io->files= r_list_new ();
io->files->free = (RListFree)r_io_desc_free;
}
R_API void r_io_desc_fini(RIO *io) {
r_list_free (io->desc);
r_list_free (io->files);
}
R_API ut64 r_io_desc_size(RIO *io, RIODesc *desc){
RIODesc *old = NULL;
ut64 sz = -1;
if (desc && io->fd != desc){
old = io->fd;
r_io_set_fd(io, desc);
if (desc && io->desc != desc){
old = io->desc;
r_io_use_desc (io, desc);
}
if (desc) sz = r_io_size(io);
if(old){
r_io_set_fd(io, old);
}
if(old)
r_io_use_desc (io, old);
return sz;
}
@ -77,7 +76,7 @@ R_API int r_io_desc_add(RIO *io, RIODesc *desc) {
RIODesc *foo = r_io_desc_get (io, desc->fd);
if (!foo){
desc->io = io;
r_list_append (io->desc, desc);
r_list_append (io->files, desc);
}
return foo? 1: 0;
}
@ -86,9 +85,9 @@ R_API int r_io_desc_del(RIO *io, int fd) {
RListIter *iter;
RIODesc *d;
/* No _safe loop necessary because we return immediately after the delete. */
r_list_foreach (io->desc, iter, d) {
r_list_foreach (io->files, iter, d) {
if (d->fd == fd) {
r_list_delete (io->desc, iter);
r_list_delete (io->files, iter);
return R_TRUE;
}
}
@ -98,7 +97,9 @@ R_API int r_io_desc_del(RIO *io, int fd) {
R_API RIODesc *r_io_desc_get(RIO *io, int fd) {
RListIter *iter;
RIODesc *d;
r_list_foreach (io->desc, iter, d) {
if (fd<0)
return NULL;
r_list_foreach (io->files, iter, d) {
if (d && d->fd == fd)
return d;
}

View File

@ -6,6 +6,9 @@
R_LIB_VERSION (r_io);
// XXX: this is buggy. must use seek+read
#define USE_CACHE 1
#define USE_P_API 1
#define DO_THE_IO_DBG 0
#define IO_IFDBG if (DO_THE_IO_DBG == 1)
@ -18,12 +21,13 @@ R_API RIO *r_io_new() {
io->buffer = r_cache_new (); // TODO: use RBuffer here
io->buffer_enabled = 0;
io->zeromap = R_FALSE; // if true, then 0 is mapped with contents of file
io->fd = NULL;
io->desc = NULL;
io->write_mask_fd = -1;
io->redirect = NULL;
io->printf = (void*) printf;
io->bits = (sizeof(void*) == 8)? 64: 32;
io->va = -1;
io->ff = 1;
io->plugin = NULL;
io->raised = -1;
io->off = 0;
@ -44,7 +48,7 @@ R_API void r_io_raise(RIO *io, int fd) {
R_API int r_io_is_listener(RIO *io) {
if (io && io->plugin && io->plugin->listener)
return io->plugin->listener (io->fd);
return io->plugin->listener (io->desc);
return R_FALSE;
}
@ -121,8 +125,8 @@ static inline RIODesc *__getioplugin(RIO *io, const char *_uri, int flags, int m
r_io_desc_add (io, desc);
if (desc->fd != -1)
r_io_plugin_open (io, desc->fd, plugin);
if (desc != io->fd) {
r_io_set_fd (io, desc);
if (desc != io->desc) {
r_io_use_desc (io, desc);
}
}
}
@ -135,8 +139,8 @@ static inline RIODesc *__getioplugin(RIO *io, const char *_uri, int flags, int m
r_io_desc_add (io, desc);
if (desc->fd != -1)
r_io_plugin_open (io, desc->fd, plugin);
if (desc != io->fd) {
r_io_set_fd (io, desc);
if (desc != io->desc) {
r_io_use_desc (io, desc);
}
}
}
@ -188,7 +192,7 @@ R_API RIODesc *r_io_open(RIO *io, const char *file, int flags, int mode) {
return NULL;
if (desc) {
r_io_desc_add (io, desc);
r_io_set_fd (io, desc);
r_io_use_desc (io, desc);
// add a map that represents the file
} else eprintf ("Unable to open file: %s\n", file);
@ -212,32 +216,24 @@ R_API RList *r_io_open_many(RIO *io, const char *file, int flags, int mode) {
return list_fds;
}
R_API int r_io_set_fd(RIO *io, RIODesc *fd) {
if (fd != NULL) {
io->fd = fd;
io->plugin = fd->plugin;
R_API int r_io_use_desc (RIO *io, RIODesc *d) {
if (d >= 0) {
io->desc = d;
io->plugin = d->plugin;
return R_TRUE;
}
return R_FALSE;
}
static int r_io_update_desc (RIO *io, int fd) {
R_API RIODesc *r_io_use_fd (RIO *io, int fd) {
RIODesc *desc = r_io_desc_get (io, fd);
if (!desc)
return R_FALSE;
io->fd = desc;
if (!desc) return NULL;
io->desc = desc;
io->plugin = desc->plugin;
return R_TRUE;
}
R_API int r_io_set_fdn(RIO *io, int fd) {
if (fd != -1 && !(io->fd && fd == io->fd->fd))
return r_io_update_desc (io, fd);
else if (!io->fd && fd != -1)
return r_io_update_desc (io, fd);
return R_FALSE;
return desc;
}
#if !USE_P_API
static inline int r_io_read_internal(RIO *io, ut8 *buf, int len) {
int bytes_read = 0;
const char *read_from = NULL;
@ -245,29 +241,29 @@ static inline int r_io_read_internal(RIO *io, ut8 *buf, int len) {
read_from = "buffer";
bytes_read = r_io_buffer_read (io, io->off, buf, len);
}
if (io->fd && io->fd->plugin && io->fd->plugin->read){
read_from = io->fd->plugin->name;
bytes_read = io->fd->plugin->read (io, io->fd, buf, len);
}
else if (!io->fd) {
if (io->desc && io->desc->plugin && io->desc->plugin->read){
read_from = io->desc->plugin->name;
bytes_read = io->desc->plugin->read (io, io->desc, buf, len);
} else if (!io->desc) {
eprintf ("Something really bad has happened, and r2 is going to die soon. sorry! :-(\n");
read_from = "FAILED";
bytes_read = 0;
} else {
read_from = "File";
bytes_read = read (io->fd->fd, buf, len);
bytes_read = read (io->desc->fd, buf, len);
}
IO_IFDBG {
if (io->fd) eprintf ("Data source: %s\n", io->fd->name);
if (io->desc) eprintf ("Data source: %s\n", io->desc->name);
eprintf ("Asked for %d bytes, provided %d from %s\n",
len, bytes_read, read_from);
}
return bytes_read;
}
#endif
R_API int r_io_read(RIO *io, ut8 *buf, int len) {
int ret;
if (io==NULL || io->fd == NULL)
if (io==NULL || io->desc == NULL)
return -1;
//if (io->off ==UT64_MAX) asm("int3");
if (io->off==UT64_MAX)
@ -281,9 +277,11 @@ R_API int r_io_read(RIO *io, ut8 *buf, int len) {
return ret;
}
// XXX: this is buggy. must use seek+read
#define USE_CACHE 1
R_API int r_io_read_at(RIO *io, ut64 addr, ut8 *buf, int len) {
#if USE_P_API
return r_io_vread (io, addr, buf, len);
#else
ut64 paddr, last, last2;
int ms, ret, l, olen = len, w = 0;
@ -421,6 +419,7 @@ if (len>0) {
break;
}
return olen;
#endif
}
R_API ut64 r_io_read_i(RIO *io, ut64 addr, int sz, int endian) {
@ -433,12 +432,13 @@ R_API ut64 r_io_read_i(RIO *io, ut64 addr, int sz, int endian) {
return ret;
}
// TODO. this is a physical resize
R_API int r_io_resize(RIO *io, ut64 newsize) {
if (io->plugin) {
if (io->plugin->resize) {
int res = io->plugin->resize (io, io->fd, newsize);
int res = io->plugin->resize (io, io->desc, newsize);
if (res)
r_io_map_truncate_update (io, io->fd->fd, newsize);
r_io_map_truncate_update (io, io->desc->fd, newsize);
return res;
}
return R_FALSE;
@ -454,7 +454,7 @@ R_API int r_io_extend(RIO *io, ut64 size) {
if (!size) return R_FALSE;
if (io->plugin && io->plugin->extend)
return io->plugin->extend (io, io->fd, size);
return io->plugin->extend (io, io->desc, size);
if (!r_io_resize (io, size+cur_size)) return R_FALSE;
@ -493,7 +493,7 @@ R_API int r_io_extend_at(RIO *io, ut64 addr, ut64 size) {
R_API int r_io_set_write_mask(RIO *io, const ut8 *buf, int len) {
int ret = R_FALSE;
if (len>0) {
io->write_mask_fd = io->fd->fd;
io->write_mask_fd = io->desc->fd;
io->write_mask_buf = (ut8 *)malloc (len);
memcpy (io->write_mask_buf, buf, len);
io->write_mask_len = len;
@ -539,15 +539,15 @@ R_API int r_io_write(struct r_io_t *io, const ut8 *buf, int len) {
if (io->plugin) {
if (io->plugin->write)
ret = io->plugin->write (io, io->fd, buf, len);
ret = io->plugin->write (io, io->desc, buf, len);
else eprintf ("r_io_write: io handler with no write callback\n");
} else {
ret = write (io->fd->fd, buf, len);
ret = write (io->desc->fd, buf, len);
}
if (ret == -1)
eprintf ("r_io_write: cannot write on fd %d\n", io->fd->fd);
eprintf ("r_io_write: cannot write on fd %d\n", io->desc->fd);
else{
r_io_map_write_update (io, io->fd->fd, io->off, ret);
r_io_map_write_update (io, io->desc->fd, io->off, ret);
io->off += ret;
}
if (data)
@ -597,11 +597,11 @@ R_API ut64 r_io_seek(RIO *io, ut64 offset, int whence) {
// if resolution fails... just return as invalid address
if (offset==UT64_MAX)
return UT64_MAX;
if (io->fd != NULL) {
if (io->desc != NULL) {
if (io->plugin && io->plugin->lseek)
ret = io->plugin->lseek (io, io->fd, offset, whence);
ret = io->plugin->lseek (io, io->desc, offset, whence);
// XXX can be problematic on w32..so no 64 bit offset?
else ret = (ut64)lseek (io->fd->fd, offset, posix_whence);
else ret = (ut64)lseek (io->desc->fd, offset, posix_whence);
if (ret != UT64_MAX) {
if (whence == R_IO_SEEK_SET)
io->off = offset; // FIX linux-arm-32-bs at 0x10000
@ -638,15 +638,15 @@ R_API ut64 r_io_size(RIO *io) {
R_API int r_io_system(RIO *io, const char *cmd) {
int ret = -1;
if (io->plugin && io->plugin->system)
ret = io->plugin->system (io, io->fd, cmd);
ret = io->plugin->system (io, io->desc, cmd);
return ret;
}
R_API int r_io_close(RIO *io, RIODesc *fd) {
if (io == NULL || fd == NULL)
R_API int r_io_close(RIO *io, RIODesc *d) {
if (io == NULL || d == NULL)
return -1;
if (r_io_set_fd (io, fd)) {
int nfd = fd->fd;
if (r_io_use_desc (io, d)) {
int nfd = d->fd;
RIODesc *desc = r_io_desc_get (io, nfd);
if (desc) {
r_io_map_del (io, nfd);
@ -656,7 +656,7 @@ R_API int r_io_close(RIO *io, RIODesc *fd) {
r_io_desc_del (io, desc->fd);
}
}
io->fd = NULL; // unset current fd
io->desc = NULL; // unset current fd
return R_FALSE;
}
@ -691,7 +691,7 @@ R_API int r_io_bind(RIO *io, RIOBind *bnd) {
R_API int r_io_accept(RIO *io, int fd) {
if (r_io_is_listener (io) && io->plugin && io->plugin->accept)
return io->plugin->accept (io, io->fd, fd);
return io->plugin->accept (io, io->desc, fd);
return R_FALSE;
}
@ -734,6 +734,7 @@ R_API void r_io_sort_maps (RIO *io) {
r_list_sort (io->maps, (RListComparator) r_io_map_sort);
}
// THIS IS pread.. a weird one
static ut8 * r_io_desc_read (RIO *io, RIODesc * desc, ut64 *out_sz) {
ut8 *buf_bytes = NULL;
ut64 off = 0;

View File

@ -223,9 +223,9 @@ R_API ut64 r_io_map_select(RIO *io, ut64 off) {
if (off>=im->from) {
if (prevfrom) {
if (im->from<prevfrom)
r_io_set_fdn (io, im->fd);
r_io_use_fd (io, im->fd);
} else {
r_io_set_fdn (io, im->fd);
r_io_use_fd (io, im->fd);
}
prevfrom = im->from;
}
@ -238,7 +238,7 @@ R_API ut64 r_io_map_select(RIO *io, ut64 off) {
}
}
if (done == 0) {
r_io_set_fdn (io, fd);
r_io_use_fd (io, fd);
r_io_seek (io, -1, R_IO_SEEK_SET);
return paddr;
}
@ -246,11 +246,11 @@ R_API ut64 r_io_map_select(RIO *io, ut64 off) {
r_io_seek (io, off, R_IO_SEEK_SET);
return off;
}
r_io_set_fdn (io, fd);
r_io_use_fd (io, fd);
if (io->debug) /* HACK */
r_io_seek (io, off, R_IO_SEEK_SET);
else r_io_seek (io, paddr, R_IO_SEEK_SET);
r_io_set_fdn (io, fd);
r_io_use_fd (io, fd);
return paddr;
}

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2008-2013 - pancake, nibble */
/* radare - LGPL - Copyright 2008-2014 - pancake, nibble */
#include "r_io.h"
@ -158,17 +158,7 @@ R_API void r_io_section_list_visual(RIO *io, ut64 seek, ut64 len) {
}
}
R_API RIOSection *r_io_section_vget(RIO *io, ut64 addr) {
RListIter *iter;
RIOSection *s;
r_list_foreach (io->sections, iter, s) {
if (addr >= s->vaddr && addr < s->vaddr + s->size)
return s;
}
return NULL;
}
R_API RIOSection *r_io_section_getv(RIO *io, ut64 vaddr) {
R_API RIOSection *r_io_section_vget(RIO *io, ut64 vaddr) {
RListIter *iter;
RIOSection *s;
r_list_foreach (io->sections, iter, s) {
@ -178,32 +168,29 @@ R_API RIOSection *r_io_section_getv(RIO *io, ut64 vaddr) {
return NULL;
}
R_API RIOSection *r_io_section_get(RIO *io, ut64 addr) {
RListIter *iter;
R_API RIOSection *r_io_section_mget(RIO *io, ut64 maddr) {
RIOSection *s;
//addr = r_io_section_vaddr_to_offset(io, addr);
RListIter *iter;
r_list_foreach (io->sections, iter, s) {
if (addr >= s->offset && addr < s->offset + s->size) {
if ((maddr >= s->offset && maddr < (s->offset + s->size)))
return s;
}
}
return NULL;
}
R_API ut64 r_io_section_get_offset(RIO *io, ut64 addr) {
RIOSection *s = r_io_section_get(io, addr);
RIOSection *s = r_io_section_mget(io, addr);
return s? s->offset: -1;
}
R_API ut64 r_io_section_get_vaddr(RIO *io, ut64 offset) {
RIOSection *s = r_io_section_get (io, offset);
RIOSection *s = r_io_section_mget (io, offset);
return s? s->vaddr: -1;
}
// TODO: deprecate
R_API int r_io_section_get_rwx(RIO *io, ut64 offset) {
RIOSection *s = r_io_section_get (io, offset);
RIOSection *s = r_io_section_mget (io, offset);
return s?s->rwx:R_IO_READ|R_IO_WRITE|R_IO_EXEC;
}
@ -227,6 +214,7 @@ R_API int r_io_section_overlaps(RIO *io, RIOSection *s) {
return -1;
}
// TODO: rename to r_io_section_vaddr_to_maddr
R_API ut64 r_io_section_vaddr_to_offset(RIO *io, ut64 vaddr) {
RListIter *iter;
RIOSection *s;
@ -243,48 +231,29 @@ R_API ut64 r_io_section_vaddr_to_offset(RIO *io, ut64 vaddr) {
return vaddr;
}
// TODO: rename to r_io_section_maddr_to_vaddr
R_API ut64 r_io_section_offset_to_vaddr(RIO *io, ut64 offset) {
RIOSection *s;
RListIter *iter;
r_list_foreach (io->sections, iter, s) {
if (offset >= s->offset && offset < s->offset + s->size) {
/* XXX: Do we need this hack?
if (s->vaddr == 0) // hack
return offset;
*/
io->section = s;
return (s->vaddr + offset - s->offset);
}
RIOSection *s = r_io_section_vget (io, offset);
if (s) {
io->section = s;
return (s->vaddr + offset - s->offset);
}
return UT64_MAX;
}
R_API int r_io_section_exists_for_paddr (RIO *io, ut64 paddr) {
int res = R_FALSE;
RIOSection *s;
RListIter *iter;
r_list_foreach (io->sections, iter, s) {
if (s->offset <= paddr && paddr < s->offset + s->size) {
res = R_TRUE;
break;
}
}
return res;
// TODO: deprecate ?
R_API int r_io_section_exists_for_paddr (RIO *io, ut64 maddr) {
RIOSection *s = r_io_section_mget (io, maddr);
return s?1:0;
}
// TODO: deprecate ?
R_API int r_io_section_exists_for_vaddr (RIO *io, ut64 vaddr) {
int res = R_FALSE;
RIOSection *s;
RListIter *iter;
r_list_foreach (io->sections, iter, s) {
if (s->vaddr <= vaddr && vaddr < s->vaddr + s->size) {
res = R_TRUE;
break;
}
}
return res;
RIOSection *s = r_io_section_vget (io, vaddr);
return s?1:0;
}
// dupped in vio.c
R_API ut64 r_io_section_next(RIO *io, ut64 o) {
int oset = 0;
ut64 newsec = 0LL;
@ -350,7 +319,7 @@ R_API RIOSection * r_io_section_get_first_in_vaddr_range(RIO *io, ut64 addr, ut6
}
R_API int r_io_section_set_archbits(RIO *io, ut64 addr, const char *arch, int bits) {
RIOSection *s = r_io_section_getv (io, addr);
RIOSection *s = r_io_section_vget (io, addr);
if (!s) return R_FALSE;
if (arch) {
s->arch = r_sys_arch_id (arch);
@ -363,7 +332,7 @@ R_API int r_io_section_set_archbits(RIO *io, ut64 addr, const char *arch, int bi
}
R_API const char *r_io_section_get_archbits(RIO* io, ut64 addr, int *bits) {
RIOSection *s = r_io_section_getv (io, addr);
RIOSection *s = r_io_section_vget (io, addr);
if (!s || !s->bits || !s->arch) return NULL;
if (bits) *bits = s->bits;
return r_sys_arch_str (s->arch);

226
libr/io/vio.c Normal file
View File

@ -0,0 +1,226 @@
/* radare - LGPL - Copyright 2008-2014 - pancake */
#include <r_io.h>
#if 0
| io.va | | io.ff |
.------------------.
| |
| VADDR ---> MADDR | ---> PADDR ---> PLUGINS
| | maddr->paddr+fd
`------------------'
virtual addresses are used when io.va is enabled
and it checks for sections in order to find an address
inside a mapped range.
If a virtual address is not found in any section,
then it looks into the map addresses.
mapped addresses are used to map an RIODesc at a
specific physical address. A file can be opened, but
not mapped, in this case it will not be visible.
cache: where should it
undo api: works at vaddr level too
TODO:
- How to share data pointers?
- Load RBuffer as file?
maddr -> vaddr
it is possible to get a list of virtual addresses that
point to a given physical address from a virtual one.
#endif
/* Virtual Addressing API */
/* Return Physical Address and associated RIODesc */
/* RIOmaddr type is used to store the virtual address requested,
and the associated map address. Which must be used to
read from the physical address as specified by the map */
typedef struct r_io_maddr_t {
RIODesc *desc; // NULL if cannot be resolved
RIOSection *section;
RIOMap *map;
ut64 paddr;
ut64 maddr;
ut64 vaddr;
ut64 next_vaddr;
int has_next;
} RIOmaddr;
R_API int r_io_pread (RIO *io, ut64 paddr, ut8 *buf, int len);
static ut64 r_io_m2p(RIOMap *map, RIOmaddr *m) {
if (map) {
if (m->maddr >= map->from && m->maddr < map->to) {
return m->maddr - map->from + map->delta;
}
return UT64_MAX;
}
if (!m || !m->desc || !m->map)
return UT64_MAX;
if (m->maddr > m->map->from) {
return m->maddr - m->map->from + m->map->delta;
}
return UT64_MAX;
}
static ut64 findNextVaddr (RIO *io, RIOmaddr *ma) {
ut64 diff, hit, cur = ma->vaddr;
RListIter *iter;
RIOSection *s, *_s = NULL;
RIOMap *m, *_m = NULL;
#define foundSectionHit() {_s=s;_m=NULL;hit=diff;}
#define foundMapHit() {_s=NULL;_m=m;hit=diff;}
hit = UT64_MAX;
/* find begining of next section */
r_list_foreach (io->sections, iter, s) {
#define checkSectionHit(x) (cur<x && (diff=x-cur,diff<hit))
if (checkSectionHit (s->vaddr))
foundSectionHit ();
if (checkSectionHit (s->vaddr+s->vsize))
foundSectionHit ();
}
/* or map */
r_list_foreach (io->maps, iter, m) {
if (cur<m->from) {
diff = m->from - cur;
if (diff < hit)
hit = diff;
}
if (cur<m->to) {
diff = m->to - cur;
if (diff < hit)
hit = diff;
}
}
/* if not found */
if (hit == UT64_MAX) {
ma->has_next = R_FALSE;
return ma->next_vaddr = UT64_MAX;
}
ma->has_next = R_TRUE;
// XX: thi is wrong
//if (_m) ma->map = _m;
//if (_s) ma->section = _s;
return ma->next_vaddr = cur+hit;
}
static RIOmaddr r_io_v2m(RIO *io, ut64 vaddr) {
RIOmaddr pat = {0};
RListIter *iter;
RIOSection *s;
RIOMap *m;
int found = 0;
pat.vaddr = vaddr;
/* find current */
r_list_foreach (io->sections, iter, s) {
if (vaddr >= s->vaddr && vaddr < (s->vaddr+s->size)) {
found = 1;
pat.maddr = (vaddr-s->vaddr)+s->offset;
pat.map = r_io_map_get (io, pat.maddr);
pat.desc = pat.map? r_io_desc_get (io, pat.map->fd): NULL;
findNextVaddr (io, &pat);
pat.section = s;
pat.paddr = r_io_m2p (NULL, &pat);
}
}
/* if not found in any segment, check the maps */
if (!found)
r_list_foreach (io->maps, iter, m) {
if (vaddr >= m->from && vaddr < m->to) {
found = 1;
pat.maddr = (vaddr-m->from)+m->delta;
pat.map = r_io_map_get (io, pat.maddr);
pat.desc = pat.map? r_io_desc_get (io, pat.map->fd): NULL;
findNextVaddr (io, &pat);
pat.paddr = r_io_m2p (m, &pat);
}
}
return pat;
}
R_API int r_io_vread (RIO *io, ut64 vaddr, ut8 *buf, int len) {
int bufsz;
int left = len;
int ret, skip = 0;
RIOmaddr pat;
memset (&pat, 0, sizeof (pat));
io->ff =1;
while (left>0) {
pat = r_io_v2m (io, vaddr);
if (pat.has_next)
bufsz = R_MIN (left, (pat.next_vaddr-vaddr));
else bufsz = left;
if (pat.desc) {
/* read next_vaddr-vaddr from paddr using r_io_pread */
ret = r_io_pread (io, pat.paddr, buf+skip, bufsz);
/* if physical address fails to read.. */
if (ret<1) {
if (pat.has_next) {
if (!io->ff) {
r_io_cache_read (io, vaddr, buf, len);
return skip;
}
memset (buf+skip, 0xff, bufsz);
}
}
} else ret = 1; // avoid infinite loopz
skip += ret;
left -= ret;
if (!pat.has_next)
break;
//eprintf ("NEXT 0x%llx -> 0x%llx\n", vaddr, pat.next_vaddr);
vaddr = pat.next_vaddr;
}
if (io->ff) {
if (left>0)
memset (buf+skip, 0xff, left);
r_io_cache_read (io, vaddr, buf, len);
return len;
}
r_io_cache_read (io, vaddr, buf, len);
return skip;
}
R_API int r_io_mread (RIO *io, ut64 maddr, ut8 *buf, int len) {
// resolve paddr for given maddr
return r_io_pread (io, maddr, buf, len);
}
R_API int r_io_pread (RIO *io, ut64 paddr, ut8 *buf, int len) {
int bytes_read = 0;
const char *read_from = NULL;
// TODO: implement cache at physical level
if (paddr == UT64_MAX) {
if (io->ff) {
memset (buf, 0xff, len);
return len;
}
return -1;
}
if (io->buffer_enabled){
read_from = "buffer";
bytes_read = r_io_buffer_read (io, io->off, buf, len);
}
if (io->desc && io->desc->plugin && io->desc->plugin->read){
read_from = io->desc->plugin->name;
bytes_read = io->desc->plugin->read (io, io->desc, buf, len);
} else if (!io->desc) {
eprintf ("Something really bad has happened, and r2 is going to die soon. sorry! :-(\n");
read_from = "FAILED";
bytes_read = 0;
} else {
read_from = "File";
bytes_read = read (io->desc->fd, buf, len);
}
return bytes_read;
}
// TODO: Implement vresize, mresize and presize