2011-02-12 01:52:41 +01:00
|
|
|
/* radare - LGPL - Copyright 2008-2011 pancake<nopcode.org> */
|
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
|
|
|
|
2010-08-16 11:24:13 +02:00
|
|
|
R_API void r_io_map_init(struct r_io_t *io) {
|
|
|
|
io->maps = r_list_new ();
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2011-02-12 01:52:41 +01:00
|
|
|
R_API RIOMap *r_io_map_resolve(struct r_io_t *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;
|
|
|
|
}
|
|
|
|
|
2010-08-16 11:24:13 +02:00
|
|
|
R_API int r_io_map_del(struct r_io_t *io, int fd) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-02-07 09:46:01 +01:00
|
|
|
R_API RIOMap *r_io_map_add(struct r_io_t *io, int fd, int flags, ut64 delta, ut64 offset, ut64 size) {
|
2010-08-16 11:24:13 +02:00
|
|
|
RIOMap *im = R_NEW (RIOMap);
|
2011-02-12 01:52:41 +01:00
|
|
|
if (!im) return NULL;
|
|
|
|
im->fd = fd;
|
|
|
|
im->flags = flags;
|
|
|
|
im->delta = delta;
|
|
|
|
im->from = offset;
|
|
|
|
im->to = offset + size;
|
|
|
|
r_list_append (io->maps, im);
|
2011-02-07 09:46:01 +01:00
|
|
|
return im;
|
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) {
|
2011-06-30 00:17:12 +02:00
|
|
|
//ut64 delta = 0;
|
2011-04-06 12:26:19 +02:00
|
|
|
ut64 fd = -1;//io->fd;
|
2012-02-07 00:44:46 +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;
|
|
|
|
r_list_foreach (io->maps, iter, im) { // _prev?
|
2009-09-05 23:58:02 +00:00
|
|
|
if (im && 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;
|
|
|
|
if (fd == io->raised)
|
|
|
|
break;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
}
|
2011-02-16 09:29:09 +01:00
|
|
|
if (fd != -1) {
|
|
|
|
r_io_set_fdn (io, fd);
|
2012-02-07 00:44:46 +01:00
|
|
|
//eprintf ("seek ret %d = %llx\n", delta,
|
|
|
|
r_io_seek (io, delta, R_IO_SEEK_SET);
|
2011-02-16 09:29:09 +01:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
2011-02-12 01:52:41 +01:00
|
|
|
return R_FALSE;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2009-09-05 23:58:02 +00:00
|
|
|
#if 0
|
2011-02-12 01:52:41 +01:00
|
|
|
int r_io_map_read_rest(struct r_io_t *io, ut64 off, ut8 *buf, ut64 len) {
|
2009-02-05 22:08:46 +01:00
|
|
|
struct list_head *pos;
|
|
|
|
list_for_each_prev(pos, &io->maps) {
|
2009-09-05 23:58:02 +00:00
|
|
|
struct r_io_map_t *im = list_entry(pos, struct r_io_map_t, list);
|
2009-02-05 22:08:46 +01:00
|
|
|
if (im->file[0] != '\0' && off+len >= im->from && off < im->to) {
|
|
|
|
lseek(im->fd, 0, SEEK_SET);
|
2009-09-02 00:10:51 +00:00
|
|
|
// XXX VERY BROKEN
|
2009-02-05 22:08:46 +01:00
|
|
|
return read(im->fd, buf+(im->from-(off+len)), len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-05 23:58:02 +00:00
|
|
|
#endif
|