2015-03-16 00:19:25 +00:00
|
|
|
/* radare - LGPL - Copyright 2008-2015 - pancake */
|
2009-09-08 02:14:19 +00:00
|
|
|
|
2009-09-17 09:48:36 +00:00
|
|
|
// TODO: implement a more inteligent way to store cached memory
|
|
|
|
// TODO: define limit of max mem to cache
|
2009-09-08 02:14:19 +00:00
|
|
|
|
|
|
|
#include "r_io.h"
|
|
|
|
|
2014-06-04 23:50:56 +00:00
|
|
|
static void cache_item_free(RIOCache *cache) {
|
2012-02-07 00:51:56 +00:00
|
|
|
if (!cache)
|
|
|
|
return;
|
2014-10-07 00:52:47 +00:00
|
|
|
free (cache->data);
|
|
|
|
free (cache->odata);
|
2012-02-07 00:51:56 +00:00
|
|
|
free (cache);
|
|
|
|
}
|
|
|
|
|
2010-03-30 21:12:19 +00:00
|
|
|
R_API void r_io_cache_init(RIO *io) {
|
2016-03-29 21:39:32 +00:00
|
|
|
io->cache = r_list_newf ((RListFree)cache_item_free);
|
2015-09-14 00:08:31 +00:00
|
|
|
io->cached = false; // cache write ops
|
|
|
|
io->cached_read = false; // cached read ops
|
2009-09-08 02:14:19 +00:00
|
|
|
}
|
|
|
|
|
2010-03-30 21:12:19 +00:00
|
|
|
R_API void r_io_cache_enable(RIO *io, int read, int write) {
|
2010-03-30 15:37:15 +00:00
|
|
|
io->cached = read | write;
|
2009-09-17 09:48:36 +00:00
|
|
|
io->cached_read = read;
|
2009-09-08 18:16:52 +00:00
|
|
|
}
|
|
|
|
|
2014-10-07 00:52:47 +00:00
|
|
|
R_API void r_io_cache_commit(RIO *io, ut64 from, ut64 to) {
|
2012-02-07 00:51:56 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RIOCache *c;
|
2014-10-07 00:52:47 +00:00
|
|
|
int ioc = io->cached;
|
|
|
|
io->cached = 2;
|
|
|
|
r_list_foreach (io->cache, iter, c) {
|
|
|
|
if (c->from >= from && c->to <= to) {
|
2010-03-30 21:12:19 +00:00
|
|
|
if (!r_io_write_at (io, c->from, c->data, c->size))
|
2010-04-14 11:02:23 +00:00
|
|
|
eprintf ("Error writing change at 0x%08"PFMT64x"\n", c->from);
|
2016-03-22 12:16:32 +00:00
|
|
|
else
|
|
|
|
c->written = true;
|
2014-10-07 00:52:47 +00:00
|
|
|
break;
|
2010-03-30 21:12:19 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-07 00:52:47 +00:00
|
|
|
io->cached = ioc;
|
2010-03-30 21:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_io_cache_reset(RIO *io, int set) {
|
2010-03-23 22:26:59 +00:00
|
|
|
io->cached = set;
|
2012-02-07 00:51:56 +00:00
|
|
|
r_list_purge (io->cache);
|
2009-09-08 18:16:52 +00:00
|
|
|
}
|
|
|
|
|
2010-03-30 21:12:19 +00:00
|
|
|
R_API int r_io_cache_invalidate(RIO *io, ut64 from, ut64 to) {
|
2014-10-26 00:47:08 +00:00
|
|
|
RListIter *iter;
|
2012-02-07 00:51:56 +00:00
|
|
|
RIOCache *c;
|
2015-09-14 00:08:31 +00:00
|
|
|
int done = false;
|
2012-02-07 00:51:56 +00:00
|
|
|
|
2014-10-07 00:52:47 +00:00
|
|
|
if (from<to) {
|
|
|
|
//r_list_foreach_safe (io->cache, iter, iter_tmp, c) {
|
|
|
|
r_list_foreach (io->cache, iter, c) {
|
|
|
|
if (c->from >= from && c->to <= to) {
|
|
|
|
int ioc = io->cached;
|
|
|
|
io->cached = 2; // magic number to skip caching this write
|
|
|
|
r_io_write_at (io, c->from, c->odata, c->size);
|
|
|
|
io->cached = ioc;
|
|
|
|
if (!c->written)
|
|
|
|
r_list_delete (io->cache, iter);
|
2015-09-14 00:08:31 +00:00
|
|
|
c->written = false;
|
|
|
|
done = true;
|
2014-10-07 00:52:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-06-21 22:19:02 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-07 00:52:47 +00:00
|
|
|
return done;
|
2009-09-17 09:48:36 +00:00
|
|
|
}
|
|
|
|
|
2010-03-30 21:12:19 +00:00
|
|
|
R_API int r_io_cache_list(RIO *io, int rad) {
|
2011-06-21 22:19:02 +00:00
|
|
|
int i, j = 0;
|
2012-02-07 00:51:56 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RIOCache *c;
|
2016-07-20 16:43:28 +00:00
|
|
|
if (rad == 2) {
|
2016-07-19 21:55:36 +00:00
|
|
|
io->cb_printf ("[");
|
2016-07-20 16:43:28 +00:00
|
|
|
}
|
2012-02-07 00:51:56 +00:00
|
|
|
r_list_foreach (io->cache, iter, c) {
|
2016-07-19 21:55:36 +00:00
|
|
|
if (rad == 1) {
|
2016-07-20 16:43:28 +00:00
|
|
|
io->cb_printf ("wx ");
|
|
|
|
for (i = 0; i < c->size; i++) {
|
|
|
|
io->cb_printf ("%02x", (ut8)(c->data[i] & 0xff));
|
|
|
|
}
|
|
|
|
io->cb_printf (" @ 0x%08"PFMT64x, c->from);
|
|
|
|
io->cb_printf (" # replaces: ");
|
|
|
|
for (i = 0; i < c->size; i++) {
|
|
|
|
io->cb_printf ("%02x", (ut8)(c->odata[i] & 0xff));
|
|
|
|
}
|
|
|
|
io->cb_printf ("\n");
|
2016-07-19 21:55:36 +00:00
|
|
|
} else if (rad == 2) {
|
2016-07-20 16:43:28 +00:00
|
|
|
io->cb_printf ("{\"idx\":%"PFMT64d",\"addr\":%"PFMT64d",\"size\":%d,", j, c->from, c->size);
|
|
|
|
io->cb_printf ("\"before\":\"");
|
|
|
|
for (i = 0; i < c->size; i++) {
|
|
|
|
io->cb_printf ("%02x", c->odata[i]);
|
|
|
|
}
|
|
|
|
io->cb_printf ("\",\"after\":\"");
|
|
|
|
for (i = 0; i < c->size; i++) {
|
|
|
|
io->cb_printf ("%02x", c->data[i]);
|
|
|
|
}
|
|
|
|
io->cb_printf ("\",\"written\":%s}%s", c->written? "true": "false",iter->n? ",": "");
|
2016-07-19 21:55:36 +00:00
|
|
|
} else if (rad == 0) {
|
2016-07-20 16:43:28 +00:00
|
|
|
io->cb_printf ("idx=%d addr=0x%08"PFMT64x" size=%d ", j, c->from, c->size);
|
|
|
|
for (i = 0; i < c->size; i++) {
|
2015-08-08 18:15:13 +00:00
|
|
|
io->cb_printf ("%02x", c->odata[i]);
|
2016-07-20 16:43:28 +00:00
|
|
|
}
|
2015-08-08 18:15:13 +00:00
|
|
|
io->cb_printf (" -> ");
|
2016-07-20 16:43:28 +00:00
|
|
|
for (i = 0; i < c->size; i++) {
|
2015-08-08 18:15:13 +00:00
|
|
|
io->cb_printf ("%02x", c->data[i]);
|
2016-07-20 16:43:28 +00:00
|
|
|
}
|
2015-11-16 09:29:03 +00:00
|
|
|
io->cb_printf (" %s\n", c->written? "(written)": "(not written)");
|
2011-06-21 22:19:02 +00:00
|
|
|
}
|
|
|
|
j++;
|
2010-03-30 15:37:15 +00:00
|
|
|
}
|
2016-07-19 21:55:36 +00:00
|
|
|
if (rad == 2)
|
|
|
|
io->cb_printf ("]");
|
2015-09-14 00:08:31 +00:00
|
|
|
return false;
|
2010-03-30 15:37:15 +00:00
|
|
|
}
|
|
|
|
|
2010-03-30 21:12:19 +00:00
|
|
|
R_API int r_io_cache_write(RIO *io, ut64 addr, const ut8 *buf, int len) {
|
2014-10-07 00:52:47 +00:00
|
|
|
RIOCache *ch;
|
2014-11-23 21:44:38 +00:00
|
|
|
ch = R_NEW0 (RIOCache);
|
2017-04-26 09:33:26 +00:00
|
|
|
if (!ch) {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-08 02:14:19 +00:00
|
|
|
ch->from = addr;
|
|
|
|
ch->to = addr + len;
|
|
|
|
ch->size = len;
|
2017-05-03 15:19:49 +00:00
|
|
|
ch->odata = (ut8*)calloc (1, len + 1);
|
2017-04-26 09:33:26 +00:00
|
|
|
if (!ch->odata) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-05-03 15:19:49 +00:00
|
|
|
ch->data = (ut8*)calloc (1, len + 1);
|
|
|
|
if (!ch->data) {
|
|
|
|
free (ch->odata);
|
2017-04-26 09:33:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-05-03 15:19:49 +00:00
|
|
|
ch->written = io->cached? false: true;
|
2014-11-23 21:44:38 +00:00
|
|
|
r_io_read_at (io, addr, ch->odata, len);
|
2010-03-23 22:26:59 +00:00
|
|
|
memcpy (ch->data, buf, len);
|
2012-02-07 00:51:56 +00:00
|
|
|
r_list_append (io->cache, ch);
|
2009-09-08 02:14:19 +00:00
|
|
|
}
|
|
|
|
|
2010-03-30 15:37:15 +00:00
|
|
|
R_API int r_io_cache_read(RIO *io, ut64 addr, ut8 *buf, int len) {
|
2017-08-14 16:58:18 +00:00
|
|
|
int l, covered = 0;
|
2012-02-07 00:51:56 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RIOCache *c;
|
|
|
|
r_list_foreach (io->cache, iter, c) {
|
2017-08-14 16:58:18 +00:00
|
|
|
if (addr < c->to && c->from < addr + len) {
|
|
|
|
if (addr < c->from) {
|
|
|
|
l = R_MIN (addr + len - c->from, c->size);
|
|
|
|
memcpy (buf + c->from - addr, c->data, l);
|
2010-03-30 15:37:15 +00:00
|
|
|
} else {
|
2017-08-14 16:58:18 +00:00
|
|
|
l = R_MIN (c->to - addr, len);
|
|
|
|
memcpy (buf, c->data + addr - c->from, l);
|
2010-03-30 15:37:15 +00:00
|
|
|
}
|
2015-12-08 13:59:30 +00:00
|
|
|
covered += l;
|
2009-09-08 02:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-08 13:59:30 +00:00
|
|
|
return covered;
|
2009-09-08 02:14:19 +00:00
|
|
|
}
|