2010-05-19 00:39:01 +00:00
|
|
|
/* radare - LGPL - Copyright 2007-2010 pancake<nopcode.org> */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-09-08 18:16:52 +00:00
|
|
|
#include <r_io.h>
|
|
|
|
|
2009-02-16 23:09:40 +00:00
|
|
|
#if 0
|
2009-09-08 18:16:52 +00:00
|
|
|
* TODO:
|
|
|
|
* - make path of indirections shortr (io->undo.foo is slow) */
|
2010-05-25 23:42:22 +00:00
|
|
|
* - Plugin changes in write and seeks
|
2009-09-08 18:16:52 +00:00
|
|
|
* - Per-fd history log
|
2009-02-16 23:09:40 +00:00
|
|
|
#endif
|
|
|
|
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API int r_io_undo_init(struct r_io_t *io) {
|
2009-09-08 18:16:52 +00:00
|
|
|
io->undo.w_init = 0;
|
2009-09-09 00:35:00 +00:00
|
|
|
io->undo.w_enable = 0;
|
2009-09-08 18:16:52 +00:00
|
|
|
io->undo.idx = 0;
|
|
|
|
io->undo.limit = 0;
|
2009-09-09 00:35:00 +00:00
|
|
|
io->undo.s_enable = 0;
|
|
|
|
io->undo.w_enable = 0;
|
|
|
|
INIT_LIST_HEAD(&(io->undo.w_list));
|
2009-02-16 23:09:40 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API void r_io_undo_enable(struct r_io_t *io, int s, int w) {
|
2009-09-09 00:35:00 +00:00
|
|
|
io->undo.s_enable = s;
|
|
|
|
io->undo.w_enable = w;
|
2009-02-16 23:09:40 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API ut64 r_io_sundo_last(struct r_io_t *io) {
|
2009-09-09 00:35:00 +00:00
|
|
|
return (io->undo.idx>0)?
|
2010-02-15 21:59:26 +00:00
|
|
|
io->undo.seek[io->undo.idx-2] : io->off;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API int r_io_sundo(struct r_io_t *io) {
|
|
|
|
io->undo.idx--;
|
|
|
|
if (io->undo.idx<0)
|
|
|
|
return io->undo.idx = 0;
|
|
|
|
io->off = io->undo.seek[io->undo.idx-1];
|
|
|
|
return R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API int r_io_sundo_redo(struct r_io_t *io) {
|
2009-09-08 18:16:52 +00:00
|
|
|
if (io->undo.idx<io->undo.limit) {
|
|
|
|
io->undo.idx += 2;
|
2010-05-19 00:39:01 +00:00
|
|
|
if (io->undo.idx>=R_IO_UNDOS) {
|
|
|
|
io->undo.idx -= 2;
|
|
|
|
return R_FALSE;
|
|
|
|
} else io->off = io->undo.seek[io->undo.idx];
|
2009-09-09 00:35:00 +00:00
|
|
|
r_io_sundo(io);
|
2010-05-19 00:39:01 +00:00
|
|
|
return R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-05-19 00:39:01 +00:00
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-12 23:00:36 +00:00
|
|
|
R_API void r_io_sundo_push(RIO *io) {
|
|
|
|
ut64 off = (io->va && !list_empty (&io->sections))?
|
|
|
|
r_io_section_offset_to_vaddr (io, io->off) : io->off;
|
2009-09-09 00:35:00 +00:00
|
|
|
if (!io->undo.s_enable)
|
|
|
|
return;
|
2010-07-12 23:00:36 +00:00
|
|
|
if (io->undo.seek[io->undo.idx-1] == off)
|
2009-02-05 21:08:46 +00:00
|
|
|
return;
|
2010-07-12 23:00:36 +00:00
|
|
|
io->undo.seek[io->undo.idx] = off;
|
2010-05-19 00:39:01 +00:00
|
|
|
if (++io->undo.idx==R_IO_UNDOS-1)
|
|
|
|
io->undo.idx--;
|
2009-09-08 18:16:52 +00:00
|
|
|
if (io->undo.limit<io->undo.idx)
|
|
|
|
io->undo.limit = io->undo.idx;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API void r_io_sundo_reset(struct r_io_t *io) {
|
2009-09-08 18:16:52 +00:00
|
|
|
io->undo.idx = 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API void r_io_sundo_list(struct r_io_t *io) {
|
2009-02-05 21:08:46 +00:00
|
|
|
int i;
|
2009-09-08 18:16:52 +00:00
|
|
|
if (io->undo.idx>0) {
|
2010-05-19 00:39:01 +00:00
|
|
|
io->printf ("f undo_idx @ %d\n", io->undo.idx);
|
2009-09-08 18:16:52 +00:00
|
|
|
for(i=io->undo.idx-1;i!=0;i--)
|
2010-05-19 00:39:01 +00:00
|
|
|
io->printf ("f undo_%d @ 0x%"PFMT64x"\n",
|
2009-09-08 18:16:52 +00:00
|
|
|
io->undo.idx-1-i, io->undo.seek[i-1]);
|
|
|
|
} else eprintf("-no seeks done-\n");
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2009-09-09 00:35:00 +00:00
|
|
|
/* undo writez */
|
|
|
|
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API void r_io_wundo_new(struct r_io_t *io, ut64 off, const ut8 *data, int len) {
|
2009-09-09 00:35:00 +00:00
|
|
|
struct r_io_undo_w_t *uw;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-09-09 00:35:00 +00:00
|
|
|
if (!io->undo.w_enable)
|
2009-02-05 21:08:46 +00:00
|
|
|
return;
|
|
|
|
|
2009-09-09 00:35:00 +00:00
|
|
|
/* undo write changes */
|
2010-03-12 12:35:10 +00:00
|
|
|
uw = R_NEW(struct r_io_undo_w_t);
|
2009-09-08 18:16:52 +00:00
|
|
|
uw->set = R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
uw->off = off;
|
|
|
|
uw->len = len;
|
2009-07-08 11:49:55 +00:00
|
|
|
uw->n = (ut8*) malloc(len);
|
2009-02-05 21:08:46 +00:00
|
|
|
memcpy(uw->n, data, len);
|
2009-07-08 11:49:55 +00:00
|
|
|
uw->o = (ut8*) malloc(len);
|
2009-09-08 18:16:52 +00:00
|
|
|
r_io_read_at(io, off, uw->o, len);
|
|
|
|
list_add_tail(&(uw->list), &(io->undo.w_list));
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API void r_io_wundo_clear(struct r_io_t *io) {
|
2009-02-05 21:08:46 +00:00
|
|
|
// XXX memory leak
|
2009-09-08 18:16:52 +00:00
|
|
|
INIT_LIST_HEAD(&(io->undo.w_list));
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2009-09-09 00:35:00 +00:00
|
|
|
// rename to r_io_undo_length ?
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API int r_io_wundo_size(struct r_io_t *io) {
|
2009-02-05 21:08:46 +00:00
|
|
|
struct list_head *p;
|
|
|
|
int i = 0;
|
2009-09-08 18:16:52 +00:00
|
|
|
if (io->undo.w_init)
|
2009-09-09 00:35:00 +00:00
|
|
|
list_for_each_prev(p, &(io->undo.w_list))
|
|
|
|
i++;
|
2009-02-05 21:08:46 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2009-09-08 18:16:52 +00:00
|
|
|
// TODO: Deprecate or so? iterators must be language-wide, but helpers are useful
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API void r_io_wundo_list(struct r_io_t *io) {
|
2009-02-05 21:08:46 +00:00
|
|
|
#define BW 8 /* byte wrap */
|
|
|
|
struct list_head *p;
|
|
|
|
int i = 0, j, len;
|
|
|
|
|
2009-09-08 18:16:52 +00:00
|
|
|
if (io->undo.w_init)
|
|
|
|
list_for_each_prev(p, &(io->undo.w_list)) {
|
|
|
|
struct r_io_undo_w_t *u = list_entry(p, struct r_io_undo_w_t, list);
|
2010-05-19 00:39:01 +00:00
|
|
|
io->printf ("%02d %c %d %08"PFMT64x": ", i, u->set?'+':'-', u->len, u->off);
|
2009-02-05 21:08:46 +00:00
|
|
|
len = (u->len>BW)?BW:u->len;
|
2010-05-19 00:39:01 +00:00
|
|
|
for(j=0;j<len;j++) io->printf ("%02x ", u->o[j]);
|
|
|
|
if (len == BW) io->printf (".. ");
|
|
|
|
io->printf ("=> ");
|
|
|
|
for(j=0;j<len;j++) io->printf ("%02x ", u->n[j]);
|
|
|
|
if (len == BW) io->printf (".. ");
|
|
|
|
io->printf ("\n");
|
2009-02-05 21:08:46 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API int r_io_wundo_apply(struct r_io_t *io, struct r_io_undo_w_t *u, int set) {
|
2009-09-09 00:35:00 +00:00
|
|
|
int orig = io->undo.w_enable;
|
|
|
|
io->undo.w_enable = 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
if (set) {
|
2009-09-08 18:16:52 +00:00
|
|
|
r_io_write_at(io, u->off, u->n, u->len);
|
|
|
|
u->set = R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
} else {
|
2009-09-08 18:16:52 +00:00
|
|
|
r_io_write_at(io, u->off, u->o, u->len);
|
|
|
|
u->set = R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2009-09-09 00:35:00 +00:00
|
|
|
io->undo.w_enable = orig;
|
2009-02-05 21:08:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API void r_io_wundo_apply_all(struct r_io_t *io, int set) {
|
2009-02-05 21:08:46 +00:00
|
|
|
struct list_head *p;
|
|
|
|
|
2010-05-19 00:39:01 +00:00
|
|
|
list_for_each_prev (p, &(io->undo.w_list)) {
|
|
|
|
struct r_io_undo_w_t *u = list_entry (p, struct r_io_undo_w_t, list);
|
|
|
|
r_io_wundo_apply (io, u, set); //UNDO_WRITE_UNSET);
|
|
|
|
eprintf ("%s 0x%08"PFMT64x"\n", set?"redo":"undo", u->off);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sets or unsets the writes done */
|
|
|
|
/* if ( set == 0 ) unset(n) */
|
2010-05-19 00:39:01 +00:00
|
|
|
R_API int r_io_wundo_set(struct r_io_t *io, int n, int set) {
|
2009-09-08 18:16:52 +00:00
|
|
|
struct r_io_undo_w_t *u = NULL;
|
2009-02-05 21:08:46 +00:00
|
|
|
struct list_head *p;
|
|
|
|
int i = 0;
|
2009-09-08 18:16:52 +00:00
|
|
|
if (io->undo.w_init) {
|
|
|
|
list_for_each_prev(p, &(io->undo.w_list)) {
|
2009-02-05 21:08:46 +00:00
|
|
|
if (i++ == n) {
|
2009-09-08 18:16:52 +00:00
|
|
|
u = list_entry(p, struct r_io_undo_w_t, list);
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-05-19 00:39:01 +00:00
|
|
|
if (u) {
|
|
|
|
r_io_wundo_apply(io, u, set);
|
|
|
|
return R_TRUE;
|
|
|
|
} else eprintf ("invalid undo-write index\n");
|
|
|
|
} else eprintf ("no writes done\n");
|
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|