2012-08-20 10:07:21 +02:00
|
|
|
/* radare - LGPL - Copyright 2008-2012 - pancake */
|
2009-02-05 22:08:46 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2011-02-16 09:29:09 +01:00
|
|
|
#include <r_io.h>
|
|
|
|
#include <r_list.h>
|
2009-02-05 22:08:46 +01:00
|
|
|
|
2012-10-25 09:48:45 +02:00
|
|
|
R_API void r_io_map_init(RIO *io) {
|
2010-08-16 11:24:13 +02:00
|
|
|
io->maps = r_list_new ();
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2012-10-25 09:48:45 +02:00
|
|
|
R_API RIOMap *r_io_map_get(RIO *io, ut64 addr) {
|
|
|
|
RIOMap *map;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (io->maps, iter, map) {
|
|
|
|
if (map->from == addr)
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API RIOMap *r_io_map_resolve(RIO *io, int fd) {
|
2010-08-16 11:24:13 +02:00
|
|
|
RIOMap *map;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (io->maps, iter, map) {
|
|
|
|
if (map->fd == fd)
|
|
|
|
return map;
|
2009-09-05 23:58:02 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-25 09:48:45 +02:00
|
|
|
R_API int r_io_map_del(RIO *io, int fd) {
|
2010-08-16 11:24:13 +02:00
|
|
|
RIOMap *map;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (io->maps, iter, map) {
|
|
|
|
if (fd==-1 || map->fd==fd) {
|
|
|
|
r_list_delete (io->maps, iter);
|
2011-04-06 11:35:18 +02:00
|
|
|
return R_TRUE;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
}
|
2011-04-06 11:35:18 +02:00
|
|
|
return R_FALSE;
|
2009-09-05 23:58:02 +00:00
|
|
|
}
|
|
|
|
|
2012-10-25 12:55:28 +02:00
|
|
|
R_API ut64 r_io_map_next(RIO *io, ut64 addr) {
|
|
|
|
ut64 next = 0;
|
|
|
|
RIOMap *map;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (io->maps, iter, map) {
|
|
|
|
if (map->from > addr)
|
|
|
|
if (!next || map->from < next)
|
|
|
|
next = map->from;
|
|
|
|
}
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2012-02-07 01:51:56 +01:00
|
|
|
R_API int r_io_map_del_at(RIO *io, ut64 addr) {
|
|
|
|
RIOMap *map;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (io->maps, iter, map) {
|
|
|
|
if (map->from == addr) {
|
|
|
|
r_list_delete (io->maps, iter);
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-10-25 12:55:28 +02:00
|
|
|
R_API RIOMap *r_io_map_add(RIO *io, int fd, int flags, ut64 delta, ut64 addr, ut64 size) {
|
|
|
|
RIOMap *map;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (io->maps, iter, map) {
|
|
|
|
// cannot map two files on the same address
|
|
|
|
if (map->from == addr)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
map = R_NEW (RIOMap);
|
|
|
|
if (!map) return NULL;
|
|
|
|
map->fd = fd;
|
|
|
|
map->flags = flags;
|
|
|
|
map->delta = delta;
|
|
|
|
map->from = addr;
|
|
|
|
map->to = addr + size;
|
|
|
|
r_list_append (io->maps, map);
|
|
|
|
return map;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2011-02-12 01:52:41 +01:00
|
|
|
R_API int r_io_map_select(RIO *io, ut64 off) {
|
2012-08-13 04:33:01 +02:00
|
|
|
int done = 0;
|
2012-08-20 10:07:21 +02:00
|
|
|
ut64 fd = -1;
|
2012-02-07 01:51:56 +01:00
|
|
|
st32 delta = 0;
|
2011-04-19 00:59:16 +02:00
|
|
|
RIOMap *im = NULL;
|
2010-08-16 11:24:13 +02:00
|
|
|
RListIter *iter;
|
2012-08-20 10:07:21 +02:00
|
|
|
r_list_foreach (io->maps, iter, im) {
|
2012-02-27 02:02:44 +01:00
|
|
|
if (off >= im->from && off < im->to) {
|
2012-02-07 00:44:46 +01:00
|
|
|
delta = off - im->from + im->delta;
|
2011-02-16 09:29:09 +01:00
|
|
|
fd = im->fd;
|
2012-08-13 04:33:01 +02:00
|
|
|
done = 1;
|
2011-02-16 09:29:09 +01:00
|
|
|
if (fd == io->raised)
|
|
|
|
break;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
}
|
2012-08-13 04:33:01 +02:00
|
|
|
if (done == 0) {
|
|
|
|
r_io_set_fdn (io, fd);
|
|
|
|
r_io_seek (io, -1, R_IO_SEEK_SET);
|
|
|
|
return off;
|
|
|
|
}
|
2011-02-16 09:29:09 +01:00
|
|
|
if (fd != -1) {
|
|
|
|
r_io_set_fdn (io, fd);
|
2012-09-23 02:57:43 +02:00
|
|
|
if (io->debug) /* HACK */
|
|
|
|
r_io_seek (io, off, R_IO_SEEK_SET);
|
|
|
|
else r_io_seek (io, delta, R_IO_SEEK_SET);
|
2012-08-13 04:33:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
r_io_seek (io, off, R_IO_SEEK_SET);
|
2011-02-12 01:52:41 +01:00
|
|
|
return R_FALSE;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|