2014-03-21 23:37:25 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2014 - pancake & dso */
|
2009-02-18 12:10:59 +00:00
|
|
|
|
|
|
|
#include "r_core.h"
|
2014-03-20 16:59:08 +00:00
|
|
|
#include "r_print.h"
|
2014-03-21 04:16:41 +00:00
|
|
|
#include "r_io.h"
|
2009-02-18 12:10:59 +00:00
|
|
|
|
2014-04-02 18:10:03 +00:00
|
|
|
#ifdef IFDBG
|
|
|
|
#undef IFDBG
|
|
|
|
#endif
|
|
|
|
|
2014-03-23 01:20:58 +00:00
|
|
|
#define DO_THE_DBG 0
|
2014-03-21 04:16:41 +00:00
|
|
|
#define IFDBG if (DO_THE_DBG)
|
|
|
|
|
2014-03-22 20:44:21 +00:00
|
|
|
/*
|
|
|
|
* perform_mapped_file_yank will map in a file and yank from offset the number of len bytes from
|
|
|
|
* filename. if the len is -1, the all the bytes are mapped into the yank buffer.
|
|
|
|
*/
|
|
|
|
static int perform_mapped_file_yank (RCore *core, ut64 offset, ut64 len, const char *filename);
|
|
|
|
static ut32 find_next_char (const char *input, char b);
|
|
|
|
static ut32 consume_chars (const char *input, char b);
|
|
|
|
|
2014-03-21 04:16:41 +00:00
|
|
|
static ut32 find_next_char (const char *input, char b) {
|
|
|
|
ut32 i = 0;
|
|
|
|
if (!input) return i;
|
|
|
|
for (; *input != b; i++, input++) {}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ut32 consume_chars (const char *input, char b) {
|
|
|
|
ut32 i = 0;
|
|
|
|
if (!input) return i;
|
|
|
|
for (; *input == b; i++, input++) {}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2014-03-22 20:44:21 +00:00
|
|
|
static int perform_mapped_file_yank (RCore *core, ut64 offset, ut64 len, const char *filename) {
|
|
|
|
// grab the current file descriptor, so we can reset core and io state
|
|
|
|
// after our io op is done
|
|
|
|
RIODesc *yankfd = NULL;
|
2014-05-28 02:34:12 +00:00
|
|
|
ut64 fd = core->file ? core->file->desc->fd : -1, yank_file_sz = 0,
|
2014-03-22 20:44:21 +00:00
|
|
|
loadaddr = 0, addr = offset;
|
|
|
|
int res = R_FALSE;
|
|
|
|
|
|
|
|
if (filename && *filename) {
|
|
|
|
ut64 load_align = r_config_get_i (core->config,
|
|
|
|
"file.loadalign");
|
|
|
|
RIOMap * map = NULL;
|
2014-09-06 21:05:59 +00:00
|
|
|
yankfd = r_io_open_nomap (core->io, filename, R_IO_READ, 0644);
|
2014-03-22 20:44:21 +00:00
|
|
|
// map the file in for IO operations.
|
2014-05-24 00:01:53 +00:00
|
|
|
if (yankfd && load_align) {
|
2014-03-22 20:44:21 +00:00
|
|
|
yank_file_sz = r_io_size (core->io);
|
|
|
|
map = r_io_map_add_next_available (core->io,
|
|
|
|
yankfd->fd,
|
|
|
|
R_IO_READ, 0, 0,
|
|
|
|
yank_file_sz,
|
|
|
|
load_align);
|
|
|
|
loadaddr = map ? map->from : -1;
|
|
|
|
if (yankfd && map && loadaddr != -1) {
|
|
|
|
// ***NOTE*** this is important, we need to
|
|
|
|
// address the file at its physical address!
|
|
|
|
addr += loadaddr;
|
|
|
|
} else if (yankfd) {
|
|
|
|
eprintf ("Unable to map the opened file: %s", filename);
|
|
|
|
r_io_close (core->io, yankfd);
|
|
|
|
yankfd = NULL;
|
|
|
|
} else {
|
|
|
|
eprintf ("Unable to open the file: %s", filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if len is -1 then we yank in everything
|
|
|
|
if (len == -1) len = yank_file_sz;
|
|
|
|
|
2014-04-02 18:10:03 +00:00
|
|
|
IFDBG eprintf ("yankfd: %p, yank->fd = %d, fd=%d\n", yankfd,
|
2014-04-03 10:42:53 +00:00
|
|
|
(int)(yankfd ? yankfd->fd : -1), (int)fd);
|
2014-03-22 20:44:21 +00:00
|
|
|
// this wont happen if the file failed to open or the file failed to
|
|
|
|
// map into the IO layer
|
|
|
|
if (yankfd) {
|
|
|
|
ut64 res = r_io_seek (core->io, addr, R_IO_SEEK_SET),
|
|
|
|
actual_len = len <= yank_file_sz ? len : 0;
|
|
|
|
ut8 *buf = NULL;
|
|
|
|
IFDBG eprintf (
|
|
|
|
"Addr (%"PFMT64d
|
|
|
|
") file_sz (%"PFMT64d
|
|
|
|
") actual_len (%"PFMT64d
|
|
|
|
") len (%"PFMT64d
|
|
|
|
") bytes from file: %s\n", addr, yank_file_sz,
|
|
|
|
actual_len, len, filename);
|
|
|
|
if (actual_len > 0 && res == addr) {
|
|
|
|
IFDBG eprintf (
|
|
|
|
"Creating buffer and reading %"PFMT64d
|
|
|
|
" bytes from file: %s\n", actual_len, filename);
|
|
|
|
buf = malloc (actual_len);
|
|
|
|
actual_len = r_io_read_at (core->io, addr, buf,
|
|
|
|
actual_len);
|
|
|
|
IFDBG eprintf (
|
|
|
|
"Reading %"PFMT64d " bytes from file: %s\n",
|
|
|
|
actual_len, filename);
|
2014-04-02 18:10:03 +00:00
|
|
|
/*IFDBG {
|
2014-03-22 20:44:21 +00:00
|
|
|
int i = 0;
|
|
|
|
eprintf ("Read these bytes from file: \n");
|
|
|
|
for (i = 0; i < actual_len; i++)
|
|
|
|
eprintf ("%02x", buf[i]);
|
|
|
|
eprintf ("\n");
|
2014-04-02 18:10:03 +00:00
|
|
|
}*/
|
2014-03-22 20:44:21 +00:00
|
|
|
r_core_yank_set (core, R_CORE_FOREIGN_ADDR, buf, len);
|
|
|
|
res = R_TRUE;
|
|
|
|
} else if (res != addr) {
|
|
|
|
eprintf (
|
|
|
|
"ERROR: Unable to yank data from file: (loadaddr (0x%"
|
|
|
|
PFMT64x ") (addr (0x%"
|
|
|
|
PFMT64x ") > file_sz (0x%"PFMT64x ")\n", res, addr,
|
|
|
|
yank_file_sz );
|
|
|
|
} else if (actual_len == 0) {
|
|
|
|
eprintf (
|
|
|
|
"ERROR: Unable to yank from file: addr+len (0x%"
|
|
|
|
PFMT64x ") > file_sz (0x%"PFMT64x ")\n", addr+len,
|
|
|
|
yank_file_sz );
|
|
|
|
}
|
|
|
|
r_io_close (core->io, yankfd);
|
|
|
|
free (buf);
|
|
|
|
}
|
2014-04-02 18:10:03 +00:00
|
|
|
if (fd != -1) {
|
|
|
|
r_io_raise (core->io, fd);
|
|
|
|
core->switch_file_view = 1;
|
|
|
|
r_core_block_read (core, 0);
|
|
|
|
}
|
2014-03-22 20:44:21 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-03-21 04:16:41 +00:00
|
|
|
R_API int r_core_yank_set (RCore *core, ut64 addr, const ut8 *buf, ut32 len) {
|
2014-03-20 16:59:08 +00:00
|
|
|
//free (core->yank_buf);
|
2014-03-21 04:16:41 +00:00
|
|
|
if (buf && len) {
|
|
|
|
r_buf_set_bytes (core->yank_buf, buf, len);
|
2014-03-21 22:27:17 +00:00
|
|
|
core->yank_buf->base = addr;
|
2013-11-10 21:57:08 +00:00
|
|
|
return R_TRUE;
|
2012-01-31 01:45:17 +00:00
|
|
|
}
|
2013-11-10 21:57:08 +00:00
|
|
|
return R_FALSE;
|
2012-01-31 01:45:17 +00:00
|
|
|
}
|
2014-03-21 23:37:25 +00:00
|
|
|
|
2014-03-20 16:59:08 +00:00
|
|
|
// Call set and then null terminate the bytes.
|
2014-03-21 23:37:25 +00:00
|
|
|
R_API int r_core_yank_set_str (RCore *core, ut64 addr, const char *str,
|
|
|
|
ut32 len) {
|
2014-03-20 16:59:08 +00:00
|
|
|
//free (core->yank_buf);
|
2014-03-21 23:37:25 +00:00
|
|
|
int res = r_core_yank_set (core, addr, (ut8*)str, len);
|
2014-03-20 16:59:08 +00:00
|
|
|
if (res == R_TRUE)
|
|
|
|
core->yank_buf->buf[len-1] = 0;
|
|
|
|
return res;
|
|
|
|
}
|
2012-01-31 01:45:17 +00:00
|
|
|
|
2010-03-08 11:45:22 +00:00
|
|
|
R_API int r_core_yank(struct r_core_t *core, ut64 addr, int len) {
|
2010-01-12 01:12:18 +00:00
|
|
|
ut64 curseek = core->offset;
|
2014-03-21 04:16:41 +00:00
|
|
|
ut8 *buf = NULL;
|
2013-11-14 03:15:40 +00:00
|
|
|
if (len<0) {
|
|
|
|
eprintf ("r_core_yank: cannot yank negative bytes\n");
|
2013-11-10 21:57:08 +00:00
|
|
|
return R_FALSE;
|
2013-11-14 03:15:40 +00:00
|
|
|
}
|
2014-03-20 16:59:08 +00:00
|
|
|
if (len == 0) len = core->blocksize;
|
|
|
|
//free (core->yank_buf);
|
|
|
|
buf = malloc (len);
|
|
|
|
//core->yank_buf = (ut8 *)malloc (len);
|
2010-01-12 01:12:18 +00:00
|
|
|
if (addr != core->offset)
|
2010-03-11 00:04:59 +00:00
|
|
|
r_core_seek (core, addr, 1);
|
2014-03-20 16:59:08 +00:00
|
|
|
|
|
|
|
r_core_read_at (core, addr, buf, len);
|
|
|
|
r_core_yank_set (core, addr, buf, len);
|
|
|
|
|
2009-02-18 12:10:59 +00:00
|
|
|
if (curseek != addr)
|
2010-03-11 00:04:59 +00:00
|
|
|
r_core_seek (core, curseek, 1);
|
2014-05-23 10:39:05 +00:00
|
|
|
free (buf);
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy a zero terminated string to the clipboard. Clamp to maxlen or blocksize. */
|
|
|
|
R_API int r_core_yank_string(RCore *core, ut64 addr, int maxlen) {
|
|
|
|
ut64 curseek = core->offset;
|
|
|
|
ut8 *buf = NULL;
|
|
|
|
if (maxlen<0) {
|
|
|
|
eprintf ("r_core_yank_string: cannot yank negative bytes\n");
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
if (addr != core->offset)
|
|
|
|
r_core_seek (core, addr, 1);
|
|
|
|
/* Ensure space and safe termination for largest possible string allowed */
|
|
|
|
buf = malloc (core->blocksize + 1);
|
2014-05-24 00:01:53 +00:00
|
|
|
if (!buf)
|
|
|
|
return R_FALSE;
|
2014-05-23 10:39:05 +00:00
|
|
|
buf[core->blocksize] = 0;
|
|
|
|
r_core_read_at (core, addr, buf, core->blocksize);
|
|
|
|
if (maxlen == 0) {
|
2014-06-02 09:18:47 +00:00
|
|
|
maxlen = r_str_nlen ((const char*)buf, core->blocksize); //Don't use strnlen, see: http://sourceforge.net/p/mingw/bugs/1912/
|
2014-05-23 10:39:05 +00:00
|
|
|
} else if (maxlen > core->blocksize) {
|
|
|
|
maxlen = core->blocksize;
|
|
|
|
}
|
|
|
|
r_core_yank_set (core, addr, buf, maxlen);
|
|
|
|
if (curseek != addr)
|
|
|
|
r_core_seek (core, curseek, 1);
|
|
|
|
free (buf);
|
2009-02-18 12:10:59 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2013-03-07 22:47:41 +00:00
|
|
|
R_API int r_core_yank_paste(RCore *core, ut64 addr, int len) {
|
2013-11-10 21:57:08 +00:00
|
|
|
if (len<0) return R_FALSE;
|
2014-03-21 23:37:25 +00:00
|
|
|
if (len == 0 || len >= core->yank_buf->length) len =
|
|
|
|
core->yank_buf->length;
|
2014-03-20 16:59:08 +00:00
|
|
|
r_core_write_at (core, addr, core->yank_buf->buf, len);
|
2009-02-18 12:10:59 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
2012-02-05 00:14:09 +00:00
|
|
|
|
2012-08-09 10:42:44 +00:00
|
|
|
R_API int r_core_yank_to(RCore *core, const char *_arg) {
|
2012-02-05 00:14:09 +00:00
|
|
|
ut64 len = 0;
|
|
|
|
ut64 pos = -1;
|
2012-08-09 10:42:44 +00:00
|
|
|
char *str, *arg;
|
2014-03-20 16:59:08 +00:00
|
|
|
int res = R_FALSE;
|
|
|
|
|
2012-08-09 10:42:44 +00:00
|
|
|
while (*_arg==' ') _arg++;
|
|
|
|
arg = strdup (_arg);
|
2012-02-05 00:14:09 +00:00
|
|
|
str = strchr (arg, ' ');
|
|
|
|
if (str) {
|
2014-03-21 23:37:25 +00:00
|
|
|
str[0] = '\0';
|
2012-02-05 00:14:09 +00:00
|
|
|
len = r_num_math (core->num, arg);
|
|
|
|
pos = r_num_math (core->num, str+1);
|
2014-03-21 23:37:25 +00:00
|
|
|
str[0] = ' ';
|
2012-02-05 00:14:09 +00:00
|
|
|
}
|
2014-05-02 11:01:38 +00:00
|
|
|
if (len < 1) {
|
|
|
|
free (arg);
|
2014-03-20 16:59:08 +00:00
|
|
|
return res;
|
2014-05-02 11:01:38 +00:00
|
|
|
}
|
2012-02-05 00:14:09 +00:00
|
|
|
if ((str == NULL) || (pos == -1) || (len == 0)) {
|
|
|
|
eprintf ("Usage: yt [len] [dst-addr]\n");
|
2012-08-09 10:42:44 +00:00
|
|
|
free (arg);
|
2014-03-20 16:59:08 +00:00
|
|
|
return res;
|
2013-11-10 21:57:08 +00:00
|
|
|
}
|
2012-02-05 00:14:09 +00:00
|
|
|
|
2014-03-21 23:37:25 +00:00
|
|
|
if (r_core_yank (core, core->offset, len) == R_TRUE)
|
2014-03-20 16:59:08 +00:00
|
|
|
res = r_core_yank_paste (core, pos, len);
|
|
|
|
|
2012-08-09 10:42:44 +00:00
|
|
|
free (arg);
|
2014-03-20 16:59:08 +00:00
|
|
|
return res;
|
2012-02-05 00:14:09 +00:00
|
|
|
}
|
2014-03-20 16:59:08 +00:00
|
|
|
|
|
|
|
R_API int r_core_yank_dump (RCore *core, ut64 pos) {
|
2014-03-21 23:37:25 +00:00
|
|
|
int res = R_FALSE, i = 0;
|
|
|
|
int ybl = core->yank_buf->length;
|
|
|
|
if (ybl>0) {
|
|
|
|
if (pos<ybl) {
|
|
|
|
r_cons_printf ("0x%08"PFMT64x " %d ",
|
|
|
|
core->yank_buf->base+pos,
|
|
|
|
core->yank_buf->length-pos);
|
|
|
|
for (i = pos; i < core->yank_buf->length; i++)
|
|
|
|
r_cons_printf ("%02x",
|
|
|
|
core->yank_buf->buf[i]);
|
|
|
|
r_cons_newline ();
|
|
|
|
res = R_TRUE;
|
|
|
|
} else eprintf ("Position exceeds buffer length.\n");
|
2014-03-20 16:59:08 +00:00
|
|
|
} else eprintf ("No buffer yanked already\n");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_core_yank_hexdump (RCore *core, ut64 pos) {
|
|
|
|
int res = R_FALSE;
|
2014-03-21 23:37:25 +00:00
|
|
|
int ybl = core->yank_buf->length;
|
|
|
|
if (ybl>0) {
|
|
|
|
if (pos < ybl) {
|
|
|
|
r_print_hexdump (core->print, pos,
|
|
|
|
core->yank_buf->buf+pos,
|
|
|
|
ybl-pos, 16, 4);
|
|
|
|
res = R_TRUE;
|
|
|
|
} else eprintf ("Position exceeds buffer length.\n");
|
2014-03-20 16:59:08 +00:00
|
|
|
} else eprintf ("No buffer yanked already\n");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_core_yank_cat (RCore *core, ut64 pos) {
|
|
|
|
int res = R_FALSE;
|
2014-03-21 23:37:25 +00:00
|
|
|
int ybl = core->yank_buf->length;
|
|
|
|
if (ybl>0) {
|
|
|
|
if (pos < ybl) {
|
|
|
|
r_cons_memcat ((const char*)core->yank_buf->buf+pos,
|
|
|
|
core->yank_buf->length-pos);
|
|
|
|
r_cons_newline ();
|
|
|
|
res = R_TRUE;
|
|
|
|
} else eprintf ("Position exceeds buffer length.\n");
|
2014-03-20 16:59:08 +00:00
|
|
|
} else r_cons_newline ();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_core_yank_hud_file (RCore *core, const char *input) {
|
|
|
|
char *buf = NULL;
|
|
|
|
ut32 len = 0;
|
|
|
|
int res = R_FALSE;
|
2015-06-21 19:57:30 +00:00
|
|
|
|
|
|
|
if (!input || !*input) return res;
|
2014-03-20 16:59:08 +00:00
|
|
|
|
2014-03-21 23:37:25 +00:00
|
|
|
for (input++; *input==' '; input++) ;
|
2014-03-20 16:59:08 +00:00
|
|
|
|
|
|
|
buf = r_cons_hud_file (input);
|
2014-03-21 23:37:25 +00:00
|
|
|
len = buf ? strlen ((const char*)buf) + 1 : 0;
|
2014-03-21 04:16:41 +00:00
|
|
|
res = r_core_yank_set_str (core, R_CORE_FOREIGN_ADDR, buf, len);
|
2015-02-11 18:04:16 +00:00
|
|
|
free (buf);
|
2014-03-20 16:59:08 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_core_yank_hud_path (RCore *core, const char *input, int dir) {
|
|
|
|
char *buf = NULL;
|
|
|
|
ut32 len = 0;
|
|
|
|
int res = R_FALSE;
|
2014-03-21 23:37:25 +00:00
|
|
|
for (input++; *input==' '; input++) ;
|
2014-03-20 16:59:08 +00:00
|
|
|
|
|
|
|
buf = r_cons_hud_path (input, dir);
|
2014-03-21 23:37:25 +00:00
|
|
|
len = buf ? strlen ((const char*)buf) + 1 : 0;
|
2014-03-21 04:16:41 +00:00
|
|
|
res = r_core_yank_set_str (core, R_CORE_FOREIGN_ADDR, buf, len);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-03-22 20:44:21 +00:00
|
|
|
R_API int r_core_yank_file_ex (RCore *core, const char *input) {
|
|
|
|
ut64 len = 0, adv = 0, addr = 0;
|
2014-03-21 04:16:41 +00:00
|
|
|
int res = R_FALSE;
|
|
|
|
|
|
|
|
if (!input) return res;
|
|
|
|
|
|
|
|
// get the number of bytes to yank
|
|
|
|
adv = consume_chars (input, ' ');
|
|
|
|
len = r_num_math (core->num, input+adv);
|
|
|
|
if (len == 0) {
|
|
|
|
eprintf ("ERROR: Number of bytes read must be > 0\n");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
// get the addr/offset from in the file we want to read
|
|
|
|
adv += find_next_char (input+adv, ' ');
|
|
|
|
if (adv == 0) {
|
|
|
|
eprintf ("ERROR: Address must be specified\n");
|
|
|
|
return res;
|
|
|
|
}
|
2014-03-21 23:37:25 +00:00
|
|
|
adv++;
|
2014-03-21 04:16:41 +00:00
|
|
|
|
|
|
|
IFDBG eprintf ("Handling the input: %s\n", input+adv);
|
|
|
|
// XXX - bug, will fail if address needs to be computed and has spaces
|
|
|
|
addr = r_num_math (core->num, input+adv);
|
|
|
|
|
|
|
|
adv += find_next_char (input+adv, ' ');
|
|
|
|
if (adv == 0) {
|
|
|
|
eprintf ("ERROR: File must be specified\n");
|
|
|
|
return res;
|
|
|
|
}
|
2014-03-21 23:37:25 +00:00
|
|
|
adv++;
|
2014-03-21 04:16:41 +00:00
|
|
|
|
2014-03-22 20:44:21 +00:00
|
|
|
IFDBG eprintf ("Filename: %s\n", input+adv);
|
2014-03-21 23:37:25 +00:00
|
|
|
// grab the current file descriptor, so we can reset core and io state
|
|
|
|
// after our io op is done
|
2014-03-22 20:44:21 +00:00
|
|
|
return perform_mapped_file_yank (core, addr, len, input+adv);
|
|
|
|
}
|
2014-03-21 04:16:41 +00:00
|
|
|
|
2014-03-22 20:44:21 +00:00
|
|
|
R_API int r_core_yank_file_all (RCore *core, const char *input) {
|
|
|
|
ut64 adv = 0;
|
|
|
|
if (!input) return R_FALSE;
|
|
|
|
adv = consume_chars (input, ' ');
|
|
|
|
IFDBG eprintf ("Filename: %s\n", input+adv);
|
|
|
|
return perform_mapped_file_yank (core, 0, -1, input+adv);
|
2014-03-21 23:37:25 +00:00
|
|
|
}
|