2013-02-25 08:36:07 +00:00
|
|
|
/* radare2 - LGPL - Copyright 2009-2013 - pancake */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
#include "r_core.h"
|
|
|
|
|
2012-04-21 12:28:53 +00:00
|
|
|
R_API int r_core_dump(RCore *core, const char *file, ut64 addr, ut64 size) {
|
|
|
|
ut64 i;
|
|
|
|
ut8 *buf;
|
|
|
|
int bs = core->blocksize;
|
|
|
|
FILE *fd;
|
2012-08-10 09:35:38 +00:00
|
|
|
r_sys_truncate (file, 0);
|
2012-10-19 22:31:18 +00:00
|
|
|
fd = r_sandbox_fopen (file, "wb");
|
2012-04-21 12:28:53 +00:00
|
|
|
if (!fd) {
|
|
|
|
eprintf ("Cannot open '%s' for writing\n", file);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
buf = malloc (bs);
|
|
|
|
r_cons_break (NULL, NULL);
|
2013-01-12 03:29:45 +00:00
|
|
|
for (i=0; i<size; i+=bs) {
|
2012-04-21 12:28:53 +00:00
|
|
|
if (r_cons_singleton ()->breaked)
|
|
|
|
break;
|
|
|
|
if ((i+bs)>size)
|
|
|
|
bs = size-i;
|
|
|
|
r_io_read_at (core->io, addr+i, buf, bs);
|
|
|
|
if (fwrite (buf, bs, 1, fd) <1) {
|
|
|
|
eprintf ("write error\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eprintf ("dumped 0x%"PFMT64x" bytes\n", i);
|
|
|
|
r_cons_break_end ();
|
|
|
|
fclose (fd);
|
|
|
|
free (buf);
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-03-08 11:45:22 +00:00
|
|
|
R_API int r_core_write_op(RCore *core, const char *arg, char op) {
|
2012-09-26 08:01:43 +00:00
|
|
|
int i, j, len, ret = R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
char *str;
|
2009-07-08 11:49:55 +00:00
|
|
|
ut8 *buf;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
// XXX we can work with config.block instead of dupping it
|
2010-02-01 10:55:56 +00:00
|
|
|
buf = (ut8 *)malloc (core->blocksize);
|
2012-09-26 08:01:43 +00:00
|
|
|
str = (char *)malloc (strlen (arg));
|
|
|
|
if (buf == NULL || str == NULL)
|
|
|
|
goto beach;
|
2010-02-01 10:55:56 +00:00
|
|
|
memcpy (buf, core->block, core->blocksize);
|
2013-05-02 21:49:58 +00:00
|
|
|
if (op!='e') {
|
|
|
|
len = r_hex_str2bin (arg, (ut8 *)str);
|
|
|
|
if (len==-1) {
|
|
|
|
eprintf ("Invalid hexpair string\n");
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
} else len = 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2013-05-02 21:49:58 +00:00
|
|
|
if (op=='e') {
|
|
|
|
char *p, *s = strdup (arg);
|
|
|
|
int n, from = 0, to = 0, dif = 0, step = 1;
|
|
|
|
n = from = to;
|
|
|
|
to = UT8_MAX;
|
|
|
|
//
|
|
|
|
p = strchr (s, ' ');
|
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
|
|
|
step = atoi (p+1);
|
|
|
|
}
|
|
|
|
p = strchr (s, '-');
|
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
|
|
|
to = atoi (p+1);
|
|
|
|
}
|
|
|
|
if (to<1 || to>UT8_MAX) to = UT8_MAX;
|
|
|
|
from = atoi (s);
|
|
|
|
free (s);
|
|
|
|
dif = (to<=from)? UT8_MAX: (to-from)+1;
|
|
|
|
from %= (UT8_MAX+1);
|
|
|
|
if (dif<1) dif = UT8_MAX+1;
|
|
|
|
if (step<1) step = 1;
|
|
|
|
for (i=n=0; i<core->blocksize; i++, n+= step)
|
|
|
|
buf[i] = (ut8)(n%dif)+from;
|
|
|
|
} else
|
2010-03-08 11:45:22 +00:00
|
|
|
if (op=='2' || op=='4') {
|
2010-02-01 10:55:56 +00:00
|
|
|
op -= '0';
|
2010-03-08 11:45:22 +00:00
|
|
|
for (i=0; i<core->blocksize; i+=op) {
|
2010-02-01 10:55:56 +00:00
|
|
|
/* endian swap */
|
|
|
|
ut8 tmp = buf[i];
|
|
|
|
buf[i] = buf[i+3];
|
|
|
|
buf[i+3] = tmp;
|
|
|
|
if (op==4) {
|
|
|
|
tmp = buf[i+1];
|
2010-04-06 16:21:41 +00:00
|
|
|
buf[i+1] = buf[i+2];
|
|
|
|
buf[i+2] = tmp;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-02-01 10:55:56 +00:00
|
|
|
}
|
2010-03-08 11:45:22 +00:00
|
|
|
} else {
|
2010-02-03 17:15:31 +00:00
|
|
|
for (i=j=0; i<core->blocksize; i++) {
|
2010-02-01 10:55:56 +00:00
|
|
|
switch (op) {
|
2010-03-08 11:45:22 +00:00
|
|
|
case 'x': buf[i] ^= str[j]; break;
|
|
|
|
case 'a': buf[i] += str[j]; break;
|
|
|
|
case 's': buf[i] -= str[j]; break;
|
|
|
|
case 'm': buf[i] *= str[j]; break;
|
2012-11-07 03:25:42 +00:00
|
|
|
case 'w': buf[i] = str[j]; break;
|
2012-10-04 15:05:43 +00:00
|
|
|
case 'd': buf[i] = (str[j])? buf[i] / str[j]: 0; break;
|
2010-03-08 11:45:22 +00:00
|
|
|
case 'r': buf[i] >>= str[j]; break;
|
|
|
|
case 'l': buf[i] <<= str[j]; break;
|
|
|
|
case 'o': buf[i] |= str[j]; break;
|
|
|
|
case 'A': buf[i] &= str[j]; break;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-02-01 10:55:56 +00:00
|
|
|
j++; if (j>=len) j=0; /* cyclic key */
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-01-31 01:30:59 +00:00
|
|
|
ret = r_core_write_at (core, core->offset, buf, core->blocksize);
|
2012-09-26 08:01:43 +00:00
|
|
|
beach:
|
2010-01-31 13:22:27 +00:00
|
|
|
free (buf);
|
2012-09-26 08:01:43 +00:00
|
|
|
free (str);
|
2009-02-05 21:08:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-31 02:34:46 +00:00
|
|
|
R_API int r_core_seek_archbits (RCore *core, ut64 addr) {
|
|
|
|
static char *oldarch = NULL;
|
2013-03-31 23:45:28 +00:00
|
|
|
static int oldbits = 32;
|
2013-03-31 02:34:46 +00:00
|
|
|
int bits = 0;// = core->io->section->bits;
|
2013-03-31 23:45:28 +00:00
|
|
|
const char *arch = r_io_section_get_archbits (core->io, addr, &bits);
|
2013-03-31 02:34:46 +00:00
|
|
|
if (arch && bits) {
|
2013-03-31 23:45:28 +00:00
|
|
|
if (!oldarch) {
|
|
|
|
RBinInfo *info = r_bin_get_info (core->bin);
|
|
|
|
if (info) {
|
|
|
|
oldarch = strdup (info->arch);
|
|
|
|
oldbits = info->bits;
|
|
|
|
} else {
|
|
|
|
oldarch = strdup (r_config_get (core->config, "asm.arch"));
|
|
|
|
oldbits = 32;
|
|
|
|
}
|
|
|
|
}
|
2013-03-31 02:34:46 +00:00
|
|
|
r_config_set (core->config, "asm.arch", arch);
|
|
|
|
r_config_set_i (core->config, "asm.bits", bits);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (oldarch) {
|
|
|
|
r_config_set (core->config, "asm.arch", oldarch);
|
|
|
|
r_config_set_i (core->config, "asm.bits", oldbits);
|
|
|
|
free (oldarch);
|
|
|
|
oldarch = NULL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-18 23:49:05 +00:00
|
|
|
R_API boolt r_core_seek(RCore *core, ut64 addr, boolt rb) {
|
2012-11-30 00:06:30 +00:00
|
|
|
RIOSection *newsection;
|
2010-01-31 01:30:59 +00:00
|
|
|
ut64 old = core->offset;
|
2011-01-20 22:28:20 +00:00
|
|
|
ut64 ret;
|
2010-02-21 10:35:49 +00:00
|
|
|
|
2010-01-31 01:30:59 +00:00
|
|
|
/* XXX unnecesary call */
|
2011-02-09 23:21:05 +00:00
|
|
|
//r_io_set_fd (core->io, core->file->fd);
|
2012-11-30 00:06:30 +00:00
|
|
|
core->io->section = core->section; // HACK
|
2010-06-28 12:12:34 +00:00
|
|
|
ret = r_io_seek (core->io, addr, R_IO_SEEK_SET);
|
2012-11-30 00:06:30 +00:00
|
|
|
newsection = core->io->section;
|
2012-12-05 23:55:22 +00:00
|
|
|
|
2011-01-20 22:28:20 +00:00
|
|
|
if (ret == UT64_MAX) {
|
2012-10-04 13:05:51 +00:00
|
|
|
//eprintf ("RET =%d %llx\n", ret, addr);
|
|
|
|
/*
|
|
|
|
XXX handle read errors correctly
|
|
|
|
if (core->ffio) {
|
|
|
|
core->offset = addr;
|
|
|
|
} else return R_FALSE;
|
|
|
|
*/
|
2011-01-20 22:28:20 +00:00
|
|
|
//core->offset = addr;
|
2011-04-18 22:59:16 +00:00
|
|
|
if (!core->io->va)
|
|
|
|
return R_FALSE;
|
2013-03-31 02:34:46 +00:00
|
|
|
//memset (core->block, 0xff, core->blocksize);
|
2010-06-28 12:12:34 +00:00
|
|
|
} else core->offset = addr;
|
2010-01-31 01:30:59 +00:00
|
|
|
if (rb) {
|
2010-04-08 12:04:34 +00:00
|
|
|
ret = r_core_block_read (core, 0);
|
2010-06-13 10:40:01 +00:00
|
|
|
if (core->ffio) {
|
|
|
|
if (ret<1 || ret > core->blocksize)
|
2010-01-31 13:22:27 +00:00
|
|
|
memset (core->block, 0xff, core->blocksize);
|
2010-06-13 10:40:01 +00:00
|
|
|
else memset (core->block+ret, 0xff, core->blocksize-ret);
|
2010-06-28 12:12:34 +00:00
|
|
|
ret = core->blocksize;
|
2010-06-13 10:40:01 +00:00
|
|
|
core->offset = addr;
|
|
|
|
} else {
|
|
|
|
if (ret<1) {
|
|
|
|
core->offset = old;
|
2010-06-28 00:12:35 +00:00
|
|
|
//eprintf ("Cannot read block at 0x%08"PFMT64x"\n", addr);
|
2010-06-13 10:40:01 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-07 11:28:22 +00:00
|
|
|
}
|
2012-11-30 00:06:30 +00:00
|
|
|
if (core->section != newsection) {//&& core->io->section->arch) {
|
2013-03-31 02:34:46 +00:00
|
|
|
r_core_seek_archbits (core, core->offset);
|
2012-11-30 00:06:30 +00:00
|
|
|
core->section = core->io->section;
|
|
|
|
}
|
2012-09-07 02:12:24 +00:00
|
|
|
return (ret==-1)? R_FALSE: R_TRUE;
|
2009-04-07 11:28:22 +00:00
|
|
|
}
|
|
|
|
|
2013-03-31 02:34:46 +00:00
|
|
|
R_API int r_core_seek_delta(RCore *core, st64 addr) {
|
|
|
|
ut64 tmp = core->offset;
|
|
|
|
int ret;
|
|
|
|
if (addr == 0)
|
|
|
|
return R_TRUE;
|
|
|
|
if (addr>0LL) {
|
|
|
|
/* check end of file */
|
|
|
|
if (0) addr = 0;
|
|
|
|
else addr += tmp;
|
|
|
|
} else {
|
|
|
|
/* check < 0 */
|
|
|
|
if (-addr > tmp) addr = 0;
|
|
|
|
else addr += tmp;
|
|
|
|
}
|
|
|
|
core->offset = addr;
|
|
|
|
ret = r_core_seek (core, addr, 1);
|
|
|
|
//ret = r_core_block_read (core, 0);
|
|
|
|
//if (ret == -1)
|
|
|
|
// memset (core->block, 0xff, core->blocksize);
|
|
|
|
// core->offset = tmp;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-08 11:45:22 +00:00
|
|
|
R_API int r_core_write_at(RCore *core, ut64 addr, const ut8 *buf, int size) {
|
2010-11-22 00:27:20 +00:00
|
|
|
int ret;
|
|
|
|
if (!core->io || !core->file || size<1)
|
|
|
|
return R_FALSE;
|
|
|
|
ret = r_io_set_fd (core->io, core->file->fd);
|
2009-09-02 00:10:51 +00:00
|
|
|
if (ret != -1) {
|
2010-05-19 22:59:42 +00:00
|
|
|
ret = r_io_write_at (core->io, addr, buf, size);
|
2010-01-12 01:12:18 +00:00
|
|
|
if (addr >= core->offset && addr <= core->offset+core->blocksize)
|
2010-04-06 16:21:41 +00:00
|
|
|
r_core_block_read (core, 0);
|
2009-09-02 00:10:51 +00:00
|
|
|
}
|
2012-08-13 23:25:50 +00:00
|
|
|
core->file->size = r_io_size (core->io);
|
2012-02-09 00:38:16 +00:00
|
|
|
return (ret==-1)? R_FALSE: R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 11:45:22 +00:00
|
|
|
R_API int r_core_block_read(RCore *core, int next) {
|
2011-04-18 22:59:16 +00:00
|
|
|
ut64 off;
|
2011-02-12 00:52:41 +00:00
|
|
|
if (core->file == NULL) {
|
|
|
|
memset (core->block, 0xff, core->blocksize);
|
2009-04-07 11:28:22 +00:00
|
|
|
return -1;
|
2011-02-12 00:52:41 +00:00
|
|
|
}
|
2010-05-19 22:59:42 +00:00
|
|
|
r_io_set_fd (core->io, core->file->fd);
|
2011-04-18 22:59:16 +00:00
|
|
|
off = r_io_seek (core->io, core->offset+((next)?core->blocksize:0), R_IO_SEEK_SET);
|
|
|
|
if (off == UT64_MAX) {
|
|
|
|
memset (core->block, 0xff, core->blocksize);
|
2013-03-17 23:38:04 +00:00
|
|
|
// TODO: do continuation in io
|
|
|
|
if (!core->io->va)
|
|
|
|
return -1;
|
2011-04-18 22:59:16 +00:00
|
|
|
}
|
|
|
|
return (int)r_io_read (core->io, core->block, core->blocksize);
|
2009-04-07 11:28:22 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 11:45:22 +00:00
|
|
|
R_API int r_core_read_at(RCore *core, ut64 addr, ut8 *buf, int size) {
|
2010-11-22 00:27:20 +00:00
|
|
|
int ret;
|
|
|
|
if (!core->io || !core->file || size<1)
|
|
|
|
return R_FALSE;
|
2012-08-13 02:33:01 +00:00
|
|
|
#if 0
|
2011-01-20 21:52:16 +00:00
|
|
|
r_io_set_fd (core->io, core->file->fd); // XXX ignore ret? -- ultra slow method.. inverse resolution of io plugin brbrb
|
2010-05-19 22:59:42 +00:00
|
|
|
ret = r_io_read_at (core->io, addr, buf, size);
|
2012-08-13 02:33:01 +00:00
|
|
|
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
|
|
|
|
//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);
|
2011-04-25 18:09:09 +00:00
|
|
|
if (ret != size) {
|
2012-10-04 13:05:51 +00:00
|
|
|
if (ret>=size || ret<0) ret = 0;
|
|
|
|
memset (buf+ret, 0xff, size-ret);
|
2011-04-25 18:09:09 +00:00
|
|
|
}
|
2010-02-01 10:55:56 +00:00
|
|
|
if (addr>=core->offset && addr<=core->offset+core->blocksize)
|
|
|
|
r_core_block_read (core, 0);
|
2012-08-13 02:33:01 +00:00
|
|
|
#endif
|
2012-08-31 09:45:06 +00:00
|
|
|
return (ret==size); //UT64_MAX);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|