2011-01-23 16:48:31 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2011 pancake<nopcode.org> */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-04-15 18:24:19 +00:00
|
|
|
#include <r_core.h>
|
2010-08-22 19:42:08 +00:00
|
|
|
#include <r_socket.h>
|
2009-04-11 21:22:20 +00:00
|
|
|
#include "../config.h"
|
2011-04-01 16:33:26 +00:00
|
|
|
#if __UNIX__
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-08-22 19:42:08 +00:00
|
|
|
static int endian = 1; // XXX HACK
|
|
|
|
|
2011-08-27 18:25:37 +00:00
|
|
|
static int core_cmd_callback (void *user, const char *cmd) {
|
2011-08-04 19:28:07 +00:00
|
|
|
RCore *core = (RCore *)user;
|
|
|
|
return r_core_cmd0 (core, cmd);
|
|
|
|
}
|
|
|
|
|
2010-05-19 22:59:42 +00:00
|
|
|
static ut64 num_callback(RNum *userptr, const char *str, int *ok) {
|
|
|
|
RCore *core = (RCore *)userptr; // XXX ?
|
2010-04-14 20:28:45 +00:00
|
|
|
RFlagItem *flag;
|
2011-02-24 15:50:29 +00:00
|
|
|
RAnalOp op;
|
2011-02-23 01:10:28 +00:00
|
|
|
ut64 ret = 0;
|
|
|
|
*ok = 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
if (str[0]=='$') {
|
2011-02-23 01:10:28 +00:00
|
|
|
*ok = 1;
|
2011-02-24 15:50:29 +00:00
|
|
|
r_anal_op (core->anal, &op, core->offset,
|
2011-02-23 01:10:28 +00:00
|
|
|
core->block, core->blocksize);
|
2010-02-02 10:09:52 +00:00
|
|
|
switch (str[1]) {
|
2009-04-15 18:24:19 +00:00
|
|
|
case '{':
|
|
|
|
{
|
2011-02-23 01:10:28 +00:00
|
|
|
char *ptr, *bptr = strdup (str+2);
|
|
|
|
ptr = strchr (bptr, '}');
|
2009-04-15 18:24:19 +00:00
|
|
|
if (ptr != NULL) {
|
2009-07-08 11:49:55 +00:00
|
|
|
ut64 ret;
|
2009-04-15 18:24:19 +00:00
|
|
|
ptr[0]='\0';
|
2010-05-19 22:59:42 +00:00
|
|
|
ret = r_config_get_i (core->config, bptr);
|
2010-03-26 16:01:25 +00:00
|
|
|
free (bptr);
|
2009-04-15 18:24:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2011-02-24 15:50:29 +00:00
|
|
|
case 'e': return op.eob;
|
|
|
|
case 'j': return op.jump;
|
|
|
|
case 'f': return op.fail;
|
|
|
|
case 'r': return op.ref;
|
2011-06-26 21:49:11 +00:00
|
|
|
case 'l': return op.length;
|
2009-02-05 21:08:46 +00:00
|
|
|
case 'b': return core->blocksize;
|
2009-04-15 00:29:01 +00:00
|
|
|
case 's': return core->file->size;
|
2010-05-19 22:59:42 +00:00
|
|
|
case '?': return core->num->value;
|
2011-02-23 01:10:28 +00:00
|
|
|
case '$': return core->offset;
|
2011-03-01 18:16:29 +00:00
|
|
|
case 'o': return core->io->off;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-04 00:34:54 +00:00
|
|
|
if (*str>'A') {
|
2011-02-23 01:10:28 +00:00
|
|
|
if ((flag = r_flag_get (core->flags, str))) {
|
|
|
|
ret = flag->offset;
|
|
|
|
*ok = R_TRUE;
|
|
|
|
} else *ok = ret = 0;
|
|
|
|
}
|
2010-06-16 19:44:19 +00:00
|
|
|
return ret;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 03:02:23 +00:00
|
|
|
R_API RCore *r_core_new() {
|
|
|
|
RCore *c = R_NEW (struct r_core_t);
|
2010-01-30 13:02:53 +00:00
|
|
|
r_core_init (c);
|
2009-02-05 21:08:46 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2009-04-07 11:28:22 +00:00
|
|
|
/*-----------------------------------*/
|
2011-02-05 01:21:40 +00:00
|
|
|
#define CMDS (sizeof (radare_argv)/sizeof(const char*))
|
|
|
|
static const char *radare_argv[] = {
|
|
|
|
"?",
|
2011-06-08 23:20:02 +00:00
|
|
|
"dH", "ds", "dso", "dsl", "dc", "dd", "dm", "db", "dp", "dr", "dcu",
|
2011-02-05 01:21:40 +00:00
|
|
|
"S",
|
|
|
|
"s", "s+", "s++", "s-", "s--", "s*", "sa", "sb", "sr",
|
|
|
|
"!", "!!",
|
|
|
|
"#sha1", "#crc32", "#pcprint", "#sha256", "#sha512", "#md4", "#md5",
|
2011-07-11 19:10:14 +00:00
|
|
|
"#!python", "#!perl", "#!vala",
|
2011-02-05 01:21:40 +00:00
|
|
|
"V",
|
2011-09-04 00:34:54 +00:00
|
|
|
"aa", "ab", "af", "ar", "ag", "at", "a?",
|
2011-08-27 23:49:29 +00:00
|
|
|
"aga", "agc", "agd", "agl", "agfl",
|
2011-02-05 01:21:40 +00:00
|
|
|
"e", "e-", "e*", "e!",
|
|
|
|
"i", "ii", "iI", "is", "iS", "iz",
|
|
|
|
"q",
|
|
|
|
"f", "fr", "f-", "f*", "fs", "fS", "fr", "f?",
|
|
|
|
"m", "m*", "ml", "m-", "my", "mg", "md", "mp", "m?",
|
2011-02-13 00:37:02 +00:00
|
|
|
"o", "o-",
|
2011-02-05 01:21:40 +00:00
|
|
|
"x",
|
2011-08-27 23:49:29 +00:00
|
|
|
".", ".!", ".(", "./",
|
2011-02-05 11:51:37 +00:00
|
|
|
"r", "r+", "r-",
|
2011-02-05 01:21:40 +00:00
|
|
|
"b", "bf", "b?",
|
|
|
|
"/", "//", "/a", "/c", "/m", "/x", "/v",
|
|
|
|
"y", "yy", "y?",
|
|
|
|
"wx", "ww", "wf", "w?",
|
2011-02-23 01:10:28 +00:00
|
|
|
"p6d", "p6e", "p8", "pb", "pc", "pd", "pD", "px", "pX", "po",
|
2011-07-11 19:10:14 +00:00
|
|
|
"pm", "pr", "pt", "ps", "pz", "pu", "pU", "p?",
|
2011-02-05 01:21:40 +00:00
|
|
|
NULL
|
2009-04-07 11:28:22 +00:00
|
|
|
};
|
|
|
|
|
2010-06-25 17:47:47 +00:00
|
|
|
#define TMP_ARGV_SZ 256
|
2010-06-29 23:13:09 +00:00
|
|
|
static const char *tmp_argv[TMP_ARGV_SZ];
|
2010-06-25 17:47:47 +00:00
|
|
|
static int autocomplete(RLine *line) {
|
|
|
|
RCore *core = line->user;
|
2010-12-23 23:51:01 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RFlagItem *flag;
|
2010-06-25 17:47:47 +00:00
|
|
|
if (core) {
|
2011-02-09 23:21:05 +00:00
|
|
|
char *ptr = strchr (line->buffer.data, '@');
|
|
|
|
if (ptr && line->buffer.data+line->buffer.index >= ptr) {
|
2011-02-09 23:55:30 +00:00
|
|
|
int sdelta, n, i = 0;
|
|
|
|
ptr = (char *)r_str_chop_ro (ptr+1);
|
2011-02-09 23:21:05 +00:00
|
|
|
n = strlen (ptr);//(line->buffer.data+sdelta);
|
2011-02-09 23:55:30 +00:00
|
|
|
sdelta = (int)(size_t)(ptr - line->buffer.data);
|
2011-02-09 23:21:05 +00:00
|
|
|
r_list_foreach (core->flags->flags, iter, flag) {
|
|
|
|
if (!memcmp (flag->name, line->buffer.data+sdelta, n)) {
|
|
|
|
tmp_argv[i++] = flag->name;
|
|
|
|
if (i==TMP_ARGV_SZ)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp_argv[i] = NULL;
|
|
|
|
line->completion.argc = i;
|
|
|
|
line->completion.argv = tmp_argv;
|
|
|
|
} else
|
2011-02-13 00:37:02 +00:00
|
|
|
if ((!memcmp (line->buffer.data, "o ", 2)) ||
|
2011-09-11 01:59:24 +00:00
|
|
|
!memcmp (line->buffer.data, ". ", 2) ||
|
|
|
|
!memcmp (line->buffer.data, "pm ", 3) ||
|
|
|
|
!memcmp (line->buffer.data, "/m ", 3)) {
|
2011-02-13 00:37:02 +00:00
|
|
|
// XXX: SO MANY FUCKING MEMORY LEAKS
|
|
|
|
char *str, *p, *path;
|
|
|
|
RList *list;
|
|
|
|
int n, i = 0;
|
2011-09-11 01:59:24 +00:00
|
|
|
int sdelta = (line->buffer.data[1]==' ')?2:3;
|
2011-02-13 00:37:02 +00:00
|
|
|
if (!line->buffer.data[sdelta]) {
|
2011-09-04 00:34:54 +00:00
|
|
|
path = r_sys_getdir ();
|
2011-02-13 00:37:02 +00:00
|
|
|
} else {
|
|
|
|
path = strdup (line->buffer.data+sdelta);
|
|
|
|
}
|
|
|
|
p = r_str_lchr (path, '/');
|
|
|
|
if (p) {
|
2011-07-11 19:10:14 +00:00
|
|
|
if (p==path) path = "/";
|
2011-09-11 01:59:24 +00:00
|
|
|
else if (p!=path+1)
|
|
|
|
*p = 0;
|
2011-02-13 00:37:02 +00:00
|
|
|
p++;
|
|
|
|
}
|
2011-06-26 02:55:27 +00:00
|
|
|
if (p) {
|
|
|
|
if (*p) n = strlen (p);
|
|
|
|
else p = "";
|
|
|
|
}
|
2011-09-11 01:59:24 +00:00
|
|
|
if (*path=='~') {
|
|
|
|
char *lala = r_str_home (path+1);
|
|
|
|
free (path);
|
|
|
|
path = lala;
|
|
|
|
} else if (*path!='.' && *path!='/') {
|
|
|
|
char *o = malloc (strlen (path)+4);
|
|
|
|
memcpy (o, "./", 3);
|
|
|
|
p = o+3;
|
|
|
|
n = strlen (path);
|
|
|
|
memcpy (o+3, path, strlen (path)+1);
|
|
|
|
free (path);
|
|
|
|
path = o;
|
|
|
|
}
|
2011-02-13 00:37:02 +00:00
|
|
|
#if 0
|
|
|
|
printf ("DIR(%s)\n", path);
|
|
|
|
printf ("FILE(%s)\n", p);
|
|
|
|
printf ("FILEN %d\n", n);
|
|
|
|
#endif
|
2011-09-11 01:59:24 +00:00
|
|
|
list = p? r_sys_dir (path):NULL;
|
2011-02-13 00:37:02 +00:00
|
|
|
if (list) {
|
|
|
|
char buf[4096];
|
|
|
|
r_list_foreach (list, iter, str) {
|
|
|
|
if (*str == '.')
|
|
|
|
continue;
|
|
|
|
if (!*p || !memcmp (str, p, n)) {
|
|
|
|
snprintf (buf, sizeof (buf), "%s%s%s",
|
|
|
|
path, strlen(path)>1?"/":"", str);
|
|
|
|
tmp_argv[i++] = strdup (buf);
|
|
|
|
if (i==TMP_ARGV_SZ)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-09-11 01:59:24 +00:00
|
|
|
r_list_free (list);
|
2011-02-13 00:37:02 +00:00
|
|
|
// XXX LEAK r_list_destroy (list);
|
|
|
|
} else eprintf ("\nInvalid directory\n");
|
|
|
|
tmp_argv[i] = NULL;
|
|
|
|
line->completion.argc = i;
|
|
|
|
line->completion.argv = tmp_argv;
|
|
|
|
//free (path);
|
|
|
|
} else
|
2010-06-25 17:47:47 +00:00
|
|
|
if ((!memcmp (line->buffer.data, "s ", 2)) ||
|
2011-08-27 23:49:29 +00:00
|
|
|
(!memcmp (line->buffer.data, "ag ", 3)) ||
|
|
|
|
(!memcmp (line->buffer.data, "aga ", 4)) ||
|
|
|
|
(!memcmp (line->buffer.data, "agc ", 4)) ||
|
|
|
|
(!memcmp (line->buffer.data, "agl ", 4)) ||
|
|
|
|
(!memcmp (line->buffer.data, "agd ", 4)) ||
|
|
|
|
(!memcmp (line->buffer.data, "agfl ", 5)) ||
|
2011-02-05 01:21:40 +00:00
|
|
|
(!memcmp (line->buffer.data, "b ", 2)) ||
|
2011-06-08 23:20:02 +00:00
|
|
|
(!memcmp (line->buffer.data, "dcu ", 4)) ||
|
2011-02-05 01:21:40 +00:00
|
|
|
(!memcmp (line->buffer.data, "/v ", 3)) ||
|
2010-06-25 17:47:47 +00:00
|
|
|
(!memcmp (line->buffer.data, "f ", 2)) ||
|
2010-07-12 17:27:05 +00:00
|
|
|
(!memcmp (line->buffer.data, "fr ", 3)) ||
|
2010-07-12 12:46:43 +00:00
|
|
|
(!memcmp (line->buffer.data, "/a ", 3)) ||
|
2010-06-25 17:47:47 +00:00
|
|
|
(!memcmp (line->buffer.data, "? ", 2))) {
|
|
|
|
int n, i = 0;
|
2011-06-08 23:20:02 +00:00
|
|
|
int sdelta = (line->buffer.data[1]==' ')?2:
|
|
|
|
(line->buffer.data[2]==' ')?3:4;
|
2010-07-12 12:46:43 +00:00
|
|
|
n = strlen (line->buffer.data+sdelta);
|
2010-12-23 23:51:01 +00:00
|
|
|
r_list_foreach (core->flags->flags, iter, flag) {
|
2010-07-12 12:46:43 +00:00
|
|
|
if (!memcmp (flag->name, line->buffer.data+sdelta, n)) {
|
2010-06-25 17:47:47 +00:00
|
|
|
tmp_argv[i++] = flag->name;
|
|
|
|
if (i==TMP_ARGV_SZ)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp_argv[i] = NULL;
|
|
|
|
line->completion.argc = i;
|
|
|
|
line->completion.argv = tmp_argv;
|
|
|
|
} else
|
|
|
|
if (!memcmp (line->buffer.data, "e ", 2)) {
|
2011-03-17 18:05:39 +00:00
|
|
|
RConfigNode *bt;
|
|
|
|
RListIter *iter;
|
2010-06-25 17:47:47 +00:00
|
|
|
int i = 0, n = strlen (line->buffer.data+2);
|
2011-03-17 18:05:39 +00:00
|
|
|
r_list_foreach (core->config->nodes, iter, bt) {
|
2010-06-25 17:47:47 +00:00
|
|
|
if (!memcmp (bt->name, line->buffer.data+2, n)) {
|
|
|
|
tmp_argv[i++] = bt->name;
|
|
|
|
if (i==TMP_ARGV_SZ)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp_argv[i] = NULL;
|
|
|
|
line->completion.argc = i;
|
|
|
|
line->completion.argv = tmp_argv;
|
2010-06-28 23:18:30 +00:00
|
|
|
} else {
|
2011-02-09 23:21:05 +00:00
|
|
|
int i, j;
|
2010-06-28 23:18:30 +00:00
|
|
|
for (i=j=0; radare_argv[i] && i<CMDS; i++)
|
|
|
|
if (!memcmp (radare_argv[i], line->buffer.data, line->buffer.index))
|
|
|
|
tmp_argv[j++] = radare_argv[i];
|
|
|
|
tmp_argv[j] = NULL;
|
|
|
|
line->completion.argc = j;
|
|
|
|
line->completion.argv = tmp_argv;
|
2010-06-25 17:47:47 +00:00
|
|
|
}
|
2010-06-28 23:18:30 +00:00
|
|
|
} else {
|
|
|
|
int i,j;
|
|
|
|
for (i=j=0; radare_argv[i] && i<CMDS; i++)
|
|
|
|
if (!memcmp (radare_argv[i], line->buffer.data, line->buffer.index))
|
|
|
|
tmp_argv[j++] = radare_argv[i];
|
|
|
|
tmp_argv[j] = NULL;
|
|
|
|
line->completion.argc = j;
|
|
|
|
line->completion.argv = tmp_argv;
|
2010-06-25 17:47:47 +00:00
|
|
|
}
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-02-28 21:58:21 +00:00
|
|
|
static int myfgets(char *buf, int len) {
|
2009-04-07 11:28:22 +00:00
|
|
|
/* TODO: link against dietline if possible for autocompletion */
|
|
|
|
char *ptr;
|
2010-02-18 15:36:55 +00:00
|
|
|
RLine *rli = r_line_singleton ();
|
2009-04-07 11:28:22 +00:00
|
|
|
buf[0]='\0';
|
2010-02-18 15:36:55 +00:00
|
|
|
rli->completion.argc = CMDS;
|
|
|
|
rli->completion.argv = radare_argv;
|
2010-06-25 17:47:47 +00:00
|
|
|
rli->completion.run = autocomplete;
|
2010-01-30 13:02:53 +00:00
|
|
|
ptr = r_line_readline (); //CMDS, radare_argv);
|
2009-04-07 11:28:22 +00:00
|
|
|
if (ptr == NULL)
|
2010-03-04 11:43:16 +00:00
|
|
|
return -2;
|
2010-01-09 01:05:04 +00:00
|
|
|
strncpy (buf, ptr, len);
|
|
|
|
//free(ptr); // XXX leak
|
|
|
|
return strlen (buf)+1;
|
2009-04-07 11:28:22 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------*/
|
|
|
|
|
2009-09-10 20:51:34 +00:00
|
|
|
#if 0
|
2009-07-08 11:49:55 +00:00
|
|
|
static int __dbg_read(void *user, int pid, ut64 addr, ut8 *buf, int len)
|
2009-04-12 23:10:22 +00:00
|
|
|
{
|
2010-03-19 03:02:23 +00:00
|
|
|
RCore *core = (RCore *)user;
|
2009-04-12 23:10:22 +00:00
|
|
|
// TODO: pid not used
|
|
|
|
return r_core_read_at(core, addr, buf, len);
|
|
|
|
}
|
|
|
|
|
2011-05-20 17:49:50 +00:00
|
|
|
static int __dbg_write(void *user, int pid, ut64 addr, const ut8 *buf, int len) {
|
2010-03-19 03:02:23 +00:00
|
|
|
RCore *core = (RCore *)user;
|
2009-04-12 23:10:22 +00:00
|
|
|
// TODO: pid not used
|
|
|
|
return r_core_write_at(core, addr, buf, len);
|
|
|
|
}
|
2009-09-10 20:51:34 +00:00
|
|
|
#endif
|
2009-04-12 23:10:22 +00:00
|
|
|
|
2011-05-20 17:49:50 +00:00
|
|
|
static const char *r_core_print_offname(void *p, ut64 addr) {
|
|
|
|
RCore *c = (RCore*)p;
|
|
|
|
RFlagItem *item = r_flag_get_i (c->flags, addr);
|
|
|
|
if (item) return item->name;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-03-19 03:02:23 +00:00
|
|
|
R_API int r_core_init(RCore *core) {
|
2010-05-28 00:44:51 +00:00
|
|
|
static int singleton = R_TRUE;
|
2010-08-24 09:58:09 +00:00
|
|
|
core->rtr_n = 0;
|
2011-05-21 12:27:46 +00:00
|
|
|
core->visual = R_FALSE;
|
2010-01-31 13:22:27 +00:00
|
|
|
core->ffio = 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
core->oobi = NULL;
|
|
|
|
core->oobi_len = 0;
|
2011-06-04 01:14:04 +00:00
|
|
|
core->printidx = 0;
|
2011-08-27 18:25:37 +00:00
|
|
|
core->lastcmd = NULL;
|
2011-06-04 01:14:04 +00:00
|
|
|
core->cmdqueue = NULL;
|
2011-08-27 18:25:37 +00:00
|
|
|
core->cmdrepeat = R_TRUE;
|
2009-02-18 12:10:59 +00:00
|
|
|
core->yank = NULL;
|
2010-05-19 22:59:42 +00:00
|
|
|
core->reflines = NULL;
|
2010-10-26 16:24:14 +00:00
|
|
|
core->reflines2 = NULL;
|
2009-02-18 12:10:59 +00:00
|
|
|
core->yank_len = 0;
|
|
|
|
core->yank_off = 0LL;
|
2010-05-19 22:59:42 +00:00
|
|
|
core->num = r_num_new (&num_callback, core);
|
|
|
|
//core->num->callback = &num_callback;
|
|
|
|
//core->num->userptr = core;
|
2011-02-15 15:19:18 +00:00
|
|
|
core->curasmstep = 0;
|
2011-10-04 08:10:00 +00:00
|
|
|
core->egg = r_egg_new ();
|
|
|
|
r_egg_setup (core->egg, R_SYS_ARCH, R_SYS_BITS, 0, R_SYS_OS);
|
2009-04-01 00:28:13 +00:00
|
|
|
|
|
|
|
/* initialize libraries */
|
2010-05-28 00:44:51 +00:00
|
|
|
if (singleton) {
|
2010-06-25 17:47:47 +00:00
|
|
|
RLine *line = r_line_new ();
|
2010-05-28 00:44:51 +00:00
|
|
|
r_cons_new ();
|
2010-06-25 17:47:47 +00:00
|
|
|
line->user = core;
|
2010-05-28 00:44:51 +00:00
|
|
|
r_cons_singleton()->user_fgets = (void *)myfgets;
|
2010-06-25 17:47:47 +00:00
|
|
|
//r_line_singleton()->user = (void *)core;
|
2010-05-28 00:44:51 +00:00
|
|
|
r_line_hist_load (".radare2_history");
|
|
|
|
singleton = R_FALSE;
|
|
|
|
}
|
2011-06-26 21:49:11 +00:00
|
|
|
core->cons = r_cons_singleton ();
|
|
|
|
core->cons->num = core->num;
|
2011-06-04 01:14:04 +00:00
|
|
|
core->blocksize = R_CORE_BLOCKSIZE;
|
|
|
|
core->block = (ut8*)malloc (R_CORE_BLOCKSIZE);
|
|
|
|
if (core->block == NULL) {
|
|
|
|
eprintf ("Cannot allocate %d bytes\n", R_CORE_BLOCKSIZE);
|
|
|
|
/* XXX memory leak */
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
2010-05-19 22:59:42 +00:00
|
|
|
core->print = r_print_new ();
|
2011-05-20 17:49:50 +00:00
|
|
|
core->print->user = core;
|
|
|
|
core->print->offname = r_core_print_offname;
|
2010-05-19 22:59:42 +00:00
|
|
|
core->print->printf = (void *)r_cons_printf;
|
|
|
|
core->lang = r_lang_new ();
|
|
|
|
r_lang_define (core->lang, "RCore", "core", core);
|
|
|
|
r_lang_set_user_ptr (core->lang, core);
|
|
|
|
core->anal = r_anal_new ();
|
|
|
|
r_anal_set_user_ptr (core->anal, core);
|
2011-03-01 23:02:50 +00:00
|
|
|
core->anal->meta->printf = (void *) r_cons_printf;
|
2010-05-19 22:59:42 +00:00
|
|
|
core->assembler = r_asm_new ();
|
|
|
|
r_asm_set_user_ptr (core->assembler, core);
|
|
|
|
core->parser = r_parse_new ();
|
|
|
|
r_parse_set_user_ptr (core->parser, core);
|
|
|
|
core->bin = r_bin_new ();
|
|
|
|
r_bin_set_user_ptr (core->bin, core);
|
|
|
|
core->io = r_io_new ();
|
2011-08-04 19:28:07 +00:00
|
|
|
core->io->user = (void *)core;
|
|
|
|
core->io->core_cmd_cb = core_cmd_callback;
|
2010-05-19 22:59:42 +00:00
|
|
|
core->sign = r_sign_new ();
|
|
|
|
core->search = r_search_new (R_SEARCH_KEYWORD);
|
|
|
|
r_io_undo_enable (core->io, 1, 0); // TODO: configurable via eval
|
2011-01-14 00:02:20 +00:00
|
|
|
core->fs = r_fs_new ();
|
2011-06-26 18:29:24 +00:00
|
|
|
r_bin_bind (core->bin, &(core->assembler->binb));
|
2011-03-02 10:45:20 +00:00
|
|
|
r_io_bind (core->io, &(core->search->iob));
|
2011-02-16 23:58:54 +00:00
|
|
|
r_io_bind (core->io, &(core->print->iob));
|
2011-02-02 23:20:39 +00:00
|
|
|
r_io_bind (core->io, &(core->anal->iob));
|
2011-01-14 00:02:20 +00:00
|
|
|
r_io_bind (core->io, &(core->fs->iob));
|
2010-03-18 21:22:21 +00:00
|
|
|
//r_cmd_macro_init (&core->macro);
|
2009-02-05 21:08:46 +00:00
|
|
|
core->file = NULL;
|
2011-02-07 08:46:01 +00:00
|
|
|
core->files = r_list_new ();
|
2011-02-09 23:55:30 +00:00
|
|
|
core->files->free = (RListFree)r_core_file_free;
|
2010-01-12 01:12:18 +00:00
|
|
|
core->offset = 0LL;
|
2010-01-30 13:02:53 +00:00
|
|
|
r_core_cmd_init (core);
|
2010-05-19 22:59:42 +00:00
|
|
|
core->flags = r_flag_new ();
|
|
|
|
core->dbg = r_debug_new (R_TRUE);
|
2010-06-04 21:47:35 +00:00
|
|
|
core->dbg->anal = core->anal; // XXX: dupped instance.. can cause lost pointerz
|
2011-05-05 22:59:10 +00:00
|
|
|
//r_debug_use (core->dbg, "native");
|
2010-12-22 00:23:35 +00:00
|
|
|
r_reg_arena_push (core->dbg->reg); // create a 2 level register state stack
|
2010-06-16 19:44:19 +00:00
|
|
|
// core->dbg->anal->reg = core->anal->reg; // XXX: dupped instance.. can cause lost pointerz
|
2010-05-19 22:59:42 +00:00
|
|
|
core->sign->printf = r_cons_printf;
|
|
|
|
core->io->printf = r_cons_printf;
|
|
|
|
core->dbg->printf = r_cons_printf;
|
2010-06-29 23:13:09 +00:00
|
|
|
core->dbg->bp->printf = r_cons_printf;
|
2010-05-19 22:59:42 +00:00
|
|
|
r_debug_io_bind (core->dbg, core->io);
|
2010-01-30 13:02:53 +00:00
|
|
|
r_core_config_init (core);
|
2009-02-05 21:08:46 +00:00
|
|
|
// XXX fix path here
|
2009-04-01 00:28:13 +00:00
|
|
|
|
|
|
|
/* load plugins */
|
2010-01-26 00:28:33 +00:00
|
|
|
r_core_loadlibs (core);
|
|
|
|
|
|
|
|
// TODO: get arch from r_bin or from native arch
|
2010-05-19 22:59:42 +00:00
|
|
|
r_asm_use (core->assembler, R_SYS_ARCH);
|
|
|
|
r_anal_use (core->anal, R_SYS_ARCH);
|
|
|
|
r_bp_use (core->dbg->bp, R_SYS_ARCH);
|
2010-03-04 00:46:25 +00:00
|
|
|
if (R_SYS_BITS & R_SYS_BITS_64)
|
2010-05-19 22:59:42 +00:00
|
|
|
r_config_set_i (core->config, "asm.bits", 64);
|
2010-03-04 00:46:25 +00:00
|
|
|
else
|
|
|
|
if (R_SYS_BITS & R_SYS_BITS_32)
|
2010-05-19 22:59:42 +00:00
|
|
|
r_config_set_i (core->config, "asm.bits", 32);
|
|
|
|
r_config_set (core->config, "asm.arch", R_SYS_ARCH);
|
2009-02-05 21:08:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-19 03:02:23 +00:00
|
|
|
R_API RCore *r_core_free(RCore *c) {
|
2010-01-26 00:28:33 +00:00
|
|
|
/* TODO: it leaks as shit */
|
2011-10-04 08:10:00 +00:00
|
|
|
r_egg_free (c->egg);
|
2010-01-30 13:02:53 +00:00
|
|
|
free (c);
|
2009-02-05 21:08:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-09-04 00:34:54 +00:00
|
|
|
|
2011-08-27 02:32:27 +00:00
|
|
|
R_API void r_core_prompt_loop(RCore *r) {
|
|
|
|
int ret;
|
|
|
|
do {
|
|
|
|
if (r_core_prompt (r, R_FALSE)<1)
|
|
|
|
break;
|
|
|
|
// if (lock) r_th_lock_enter (lock);
|
|
|
|
if ((ret = r_core_prompt_exec (r))==-1)
|
|
|
|
eprintf ("Invalid command\n");
|
|
|
|
/* if (lock) r_th_lock_leave (lock);
|
|
|
|
if (rabin_th && !r_th_wait_async (rabin_th)) {
|
|
|
|
eprintf ("rabin thread end \n");
|
|
|
|
r_th_free (rabin_th);
|
|
|
|
r_th_lock_free (lock);
|
|
|
|
lock = NULL;
|
|
|
|
rabin_th = NULL;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
} while (ret != R_CORE_CMD_EXIT);
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-06-23 15:30:16 +00:00
|
|
|
R_API int r_core_prompt(RCore *r, int sync) {
|
2009-02-05 21:08:46 +00:00
|
|
|
int ret;
|
2010-01-09 01:05:04 +00:00
|
|
|
char line[1024];
|
|
|
|
char prompt[32];
|
2010-05-19 22:59:42 +00:00
|
|
|
const char *cmdprompt = r_config_get (r->config, "cmd.prompt");
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2011-07-05 23:29:18 +00:00
|
|
|
if (cmdprompt && *cmdprompt)
|
2010-02-28 21:58:21 +00:00
|
|
|
ret = r_core_cmd (r, cmdprompt, 0);
|
2009-02-18 00:43:57 +00:00
|
|
|
|
2010-10-27 17:00:45 +00:00
|
|
|
if (!r_line_singleton()->echo)
|
2010-03-24 10:35:41 +00:00
|
|
|
*prompt = 0;
|
2010-09-24 17:04:33 +00:00
|
|
|
#if __UNIX__
|
2010-05-19 22:59:42 +00:00
|
|
|
else if (r_config_get_i (r->config, "scr.color"))
|
2011-08-27 18:25:37 +00:00
|
|
|
snprintf (prompt, sizeof (prompt),
|
|
|
|
Color_YELLOW"[0x%08"PFMT64x"]> "Color_RESET, r->offset);
|
2010-09-24 17:04:33 +00:00
|
|
|
#endif
|
2010-04-14 11:02:23 +00:00
|
|
|
else sprintf (prompt, "[0x%08"PFMT64x"]> ", r->offset);
|
2010-02-18 15:36:55 +00:00
|
|
|
r_line_singleton()->prompt = prompt;
|
2010-01-09 01:05:04 +00:00
|
|
|
ret = r_cons_fgets (line, sizeof (line), 0, NULL);
|
2011-08-27 18:25:37 +00:00
|
|
|
if (ret == -2) return R_CORE_CMD_EXIT;
|
|
|
|
if (ret == -1) return R_FALSE;
|
|
|
|
if (sync) return r_core_prompt_exec (r);
|
|
|
|
r->cmdqueue = line;
|
|
|
|
return R_TRUE;
|
2010-06-23 15:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_core_prompt_exec(RCore *r) {
|
|
|
|
int ret = r_core_cmd (r, r->cmdqueue, R_TRUE);
|
2010-01-09 01:05:04 +00:00
|
|
|
r_cons_flush ();
|
2009-02-05 21:08:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-19 03:02:23 +00:00
|
|
|
R_API int r_core_block_size(RCore *core, ut32 bsize) {
|
2010-03-04 11:43:16 +00:00
|
|
|
int ret = R_FALSE;
|
2011-01-23 16:48:31 +00:00
|
|
|
if (bsize == core->blocksize)
|
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
if (bsize<1)
|
2010-03-04 11:43:16 +00:00
|
|
|
bsize = R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
else if (bsize> R_CORE_BLOCKSIZE_MAX)
|
|
|
|
bsize = R_CORE_BLOCKSIZE_MAX;
|
2010-03-04 11:43:16 +00:00
|
|
|
else ret = R_TRUE;
|
2011-06-04 01:14:04 +00:00
|
|
|
core->block = realloc (core->block, bsize+1);
|
2010-04-08 16:01:45 +00:00
|
|
|
if (core->block == NULL) {
|
|
|
|
eprintf ("Oops. cannot allocate that much (%u)\n", bsize);
|
|
|
|
core->block = malloc (R_CORE_BLOCKSIZE);
|
|
|
|
core->blocksize = R_CORE_BLOCKSIZE;
|
2011-07-15 16:13:00 +00:00
|
|
|
if (core->block == NULL) {
|
|
|
|
eprintf ("Panic in the heap!\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
2010-04-08 16:01:45 +00:00
|
|
|
} else core->blocksize = bsize;
|
2011-07-15 16:13:00 +00:00
|
|
|
memset (core->block, 0xff, core->blocksize);
|
2010-01-09 01:05:04 +00:00
|
|
|
r_core_block_read (core, 0);
|
2009-02-05 21:08:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-19 03:02:23 +00:00
|
|
|
R_API int r_core_seek_align(RCore *core, ut64 align, int times) {
|
2009-03-12 12:30:32 +00:00
|
|
|
int inc = (times>=0)?1:-1;
|
2010-01-12 01:12:18 +00:00
|
|
|
int diff = core->offset%align;
|
|
|
|
ut64 seek = core->offset;
|
2009-03-12 12:30:32 +00:00
|
|
|
|
2010-01-09 01:05:04 +00:00
|
|
|
if (times == 0)
|
|
|
|
diff = -diff;
|
2009-03-12 22:19:58 +00:00
|
|
|
else if (diff) {
|
|
|
|
if (inc>0) diff += align-diff;
|
|
|
|
else diff = -diff;
|
|
|
|
if (times) times -= inc;
|
2009-03-12 12:30:32 +00:00
|
|
|
}
|
2010-01-09 01:05:04 +00:00
|
|
|
while ((times*inc)>0) {
|
2009-03-12 12:30:32 +00:00
|
|
|
times -= inc;
|
|
|
|
diff += align*inc;
|
|
|
|
}
|
2010-02-02 10:09:52 +00:00
|
|
|
if (diff<0 && -diff>seek)
|
|
|
|
seek = diff = 0;
|
2010-01-09 01:05:04 +00:00
|
|
|
return r_core_seek (core, seek+diff, 1);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2009-02-18 00:43:57 +00:00
|
|
|
|
2010-03-19 03:02:23 +00:00
|
|
|
R_API int r_core_seek_delta(RCore *core, st64 addr) {
|
2010-01-12 01:12:18 +00:00
|
|
|
ut64 tmp = core->offset;
|
2009-02-18 00:43:57 +00:00
|
|
|
int ret;
|
|
|
|
if (addr == 0)
|
|
|
|
return R_TRUE;
|
2010-04-14 20:28:45 +00:00
|
|
|
if (addr>0LL) {
|
2009-02-18 00:43:57 +00:00
|
|
|
/* check end of file */
|
2010-01-30 13:02:53 +00:00
|
|
|
if (0) addr = 0; // XXX tmp+addr>) {
|
2010-01-09 01:05:04 +00:00
|
|
|
else addr += tmp;
|
2009-02-18 00:43:57 +00:00
|
|
|
} else {
|
|
|
|
/* check < 0 */
|
2010-02-02 10:09:52 +00:00
|
|
|
if (-addr > tmp) addr = 0;
|
2010-01-30 13:02:53 +00:00
|
|
|
else addr += tmp;
|
2009-02-18 00:43:57 +00:00
|
|
|
}
|
2010-01-12 01:12:18 +00:00
|
|
|
core->offset = addr;
|
2010-01-09 01:05:04 +00:00
|
|
|
ret = r_core_block_read (core, 0);
|
2011-04-18 22:59:16 +00:00
|
|
|
//if (ret == -1)
|
|
|
|
// memset (core->block, 0xff, core->blocksize);
|
|
|
|
// core->offset = tmp;
|
2009-02-18 00:43:57 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2010-05-20 07:29:39 +00:00
|
|
|
|
|
|
|
R_API char *r_core_op_str(RCore *core, ut64 addr) {
|
2011-02-24 15:50:29 +00:00
|
|
|
RAsmOp op;
|
2010-06-04 21:47:35 +00:00
|
|
|
ut8 buf[64];
|
|
|
|
int ret;
|
2010-05-20 07:29:39 +00:00
|
|
|
r_asm_set_pc (core->assembler, addr);
|
|
|
|
r_core_read_at (core, addr, buf, sizeof (buf));
|
2011-02-24 15:50:29 +00:00
|
|
|
ret = r_asm_disassemble (core->assembler, &op, buf, sizeof (buf));
|
|
|
|
return (ret>0)?strdup (op.buf_asm): NULL;
|
2010-05-20 07:29:39 +00:00
|
|
|
}
|
|
|
|
|
2010-05-20 23:46:26 +00:00
|
|
|
R_API RAnalOp *r_core_op_anal(RCore *core, ut64 addr) {
|
2010-05-20 07:29:39 +00:00
|
|
|
ut8 buf[64];
|
2011-02-24 15:50:29 +00:00
|
|
|
RAnalOp *op = R_NEW (RAnalOp);
|
2010-05-20 07:29:39 +00:00
|
|
|
r_core_read_at (core, addr, buf, sizeof (buf));
|
2011-02-24 15:50:29 +00:00
|
|
|
r_anal_op (core->anal, op, addr, buf, sizeof (buf));
|
|
|
|
return op;
|
2010-05-20 07:29:39 +00:00
|
|
|
}
|
2010-08-22 19:42:08 +00:00
|
|
|
|
2011-02-04 10:30:08 +00:00
|
|
|
// TODO: move into core/io/rap? */
|
|
|
|
R_API int r_core_serve(RCore *core, RIODesc *file) {
|
2010-08-22 19:42:08 +00:00
|
|
|
ut8 cmd, flg, *ptr, buf[1024];
|
2011-08-16 00:55:46 +00:00
|
|
|
int i, j, pipefd;
|
2010-08-22 19:42:08 +00:00
|
|
|
ut64 x;
|
2011-03-21 00:47:17 +00:00
|
|
|
RSocket *c, *fd;
|
2011-04-17 18:58:32 +00:00
|
|
|
RIORap *rior;
|
2011-08-04 19:28:07 +00:00
|
|
|
RListIter *iter;
|
2010-08-22 19:42:08 +00:00
|
|
|
|
2011-04-17 18:58:32 +00:00
|
|
|
rior = (RIORap *)file->data;
|
|
|
|
if (rior == NULL|| rior->fd == NULL) {
|
2010-08-22 19:42:08 +00:00
|
|
|
eprintf ("rap: cannot listen.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-04-17 18:58:32 +00:00
|
|
|
fd = rior->fd;
|
2010-08-22 19:42:08 +00:00
|
|
|
|
2011-08-04 19:28:07 +00:00
|
|
|
eprintf ("RAP Server started (rap.loop=%s)\n",
|
|
|
|
r_config_get (core->config, "rap.loop"));
|
2010-08-22 19:42:08 +00:00
|
|
|
#if __UNIX__
|
|
|
|
// XXX: ugly workaround
|
|
|
|
signal (SIGINT, SIG_DFL);
|
2010-08-23 04:39:23 +00:00
|
|
|
signal (SIGPIPE, SIG_DFL);
|
2010-08-22 19:42:08 +00:00
|
|
|
#endif
|
2010-08-23 04:39:23 +00:00
|
|
|
reaccept:
|
2011-02-04 23:20:28 +00:00
|
|
|
core->io->plugin = NULL;
|
2010-08-22 19:42:08 +00:00
|
|
|
while ((c = r_socket_accept (fd))) {
|
2011-03-21 00:47:17 +00:00
|
|
|
if (c == NULL) {
|
2010-08-22 19:42:08 +00:00
|
|
|
eprintf ("rap: cannot accept\n");
|
|
|
|
r_socket_close (c);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-23 04:39:23 +00:00
|
|
|
eprintf ("rap: client connected\n");
|
2010-08-22 19:42:08 +00:00
|
|
|
for (;;) {
|
2011-08-04 19:28:07 +00:00
|
|
|
if (!r_socket_read (c, &cmd, 1)) {
|
2010-08-23 04:39:23 +00:00
|
|
|
eprintf ("rap: connection closed\n");
|
2011-02-04 23:20:28 +00:00
|
|
|
if (r_config_get_i (core->config, "rap.loop")) {
|
|
|
|
eprintf ("rap: waiting for new connection\n");
|
2010-08-23 04:39:23 +00:00
|
|
|
goto reaccept;
|
2011-02-04 23:20:28 +00:00
|
|
|
}
|
2010-08-23 04:39:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-08-22 19:42:08 +00:00
|
|
|
|
|
|
|
switch ((ut8)cmd) {
|
|
|
|
case RMT_OPEN:
|
|
|
|
r_socket_read_block (c, &flg, 1); // flags
|
|
|
|
eprintf ("open (%d): ", cmd);
|
|
|
|
r_socket_read_block (c, &cmd, 1); // len
|
2010-08-23 04:39:23 +00:00
|
|
|
pipefd = -1;
|
2011-02-12 00:52:41 +00:00
|
|
|
ptr = malloc (cmd);
|
2010-11-08 18:30:25 +00:00
|
|
|
//XXX cmd is ut8..so <256 if (cmd<RMT_MAX) {
|
2011-02-12 00:52:41 +00:00
|
|
|
if (ptr == NULL) {
|
|
|
|
eprintf ("Cannot malloc in rmt-open len = %d\n", cmd);
|
|
|
|
} else {
|
|
|
|
RCoreFile *file;
|
|
|
|
r_socket_read_block (c, ptr, cmd); //filename
|
|
|
|
ptr[cmd] = 0;
|
|
|
|
file = r_core_file_open (core, (const char *)ptr, R_IO_READ, 0); // XXX: write mode?
|
|
|
|
if (file) {
|
2011-08-04 19:28:07 +00:00
|
|
|
r_core_bin_load (core, NULL);
|
2011-02-12 00:52:41 +00:00
|
|
|
file->map = r_io_map_add (core->io, file->fd->fd, R_IO_READ, 0, 0, file->size);
|
2011-01-20 21:52:16 +00:00
|
|
|
pipefd = core->file->fd->fd;
|
2011-02-12 00:52:41 +00:00
|
|
|
eprintf ("(flags: %hhd) len: %hhd filename: '%s'\n",
|
2010-08-23 04:39:23 +00:00
|
|
|
flg, cmd, ptr); //config.file);
|
2011-02-12 00:52:41 +00:00
|
|
|
} else {
|
|
|
|
pipefd = -1;
|
|
|
|
eprintf ("Cannot open file (%s)\n", ptr);
|
2011-04-19 12:43:48 +00:00
|
|
|
return -1; //XXX: Close conection and goto accept
|
2010-08-23 04:39:23 +00:00
|
|
|
}
|
2011-02-12 00:52:41 +00:00
|
|
|
}
|
2010-08-22 19:42:08 +00:00
|
|
|
buf[0] = RMT_OPEN | RMT_REPLY;
|
2010-08-23 04:39:23 +00:00
|
|
|
r_mem_copyendian (buf+1, (ut8 *)&pipefd, 4, !endian);
|
|
|
|
r_socket_write (c, buf, 5);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2011-08-04 19:28:07 +00:00
|
|
|
|
|
|
|
/* Write meta info */
|
|
|
|
RMetaItem *d;
|
|
|
|
r_list_foreach (core->anal->meta->data, iter, d) {
|
|
|
|
if (d->type == R_META_TYPE_COMMENT)
|
|
|
|
snprintf ((char *)buf, sizeof (buf), "%s %s @ 0x%08"PFMT64x,
|
|
|
|
r_meta_type_to_string (d->type), d->str, d->from);
|
|
|
|
else
|
|
|
|
snprintf ((char *)buf, sizeof (buf),
|
|
|
|
"%s %d %s @ 0x%08"PFMT64x,
|
|
|
|
r_meta_type_to_string (d->type),
|
|
|
|
(int)(d->to-d->from), d->str, d->from);
|
|
|
|
i = strlen ((char *)buf);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !endian);
|
|
|
|
r_socket_write (c, (ut8 *)&j, 4);
|
2011-08-04 19:28:07 +00:00
|
|
|
r_socket_write (c, buf, i);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2011-08-04 19:28:07 +00:00
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
RIOSection *s;
|
|
|
|
r_list_foreach_prev (core->io->sections, iter, s) {
|
|
|
|
snprintf ((char *)buf, sizeof (buf),
|
|
|
|
"S 0x%08"PFMT64x" 0x%08"PFMT64x" 0x%08"PFMT64x" 0x%08"PFMT64x" %s %d",
|
|
|
|
s->offset, s->vaddr, s->size, s->vsize, s->name, s->rwx);
|
|
|
|
i = strlen ((char *)buf);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !endian);
|
|
|
|
r_socket_write (c, (ut8 *)&j, 4);
|
2011-08-04 19:28:07 +00:00
|
|
|
r_socket_write (c, buf, i);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2011-08-04 19:28:07 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
int fs = -1;
|
|
|
|
RFlagItem *flag;
|
|
|
|
r_list_foreach_prev (core->flags->flags, iter, flag) {
|
|
|
|
if (fs == -1 || flag->space != fs) {
|
|
|
|
fs = flag->space;
|
|
|
|
snprintf ((char *)buf, sizeof (buf),
|
|
|
|
"fs %s", r_flag_space_get_i (core->flags, fs));
|
|
|
|
i = strlen ((char *)buf);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !endian);
|
|
|
|
r_socket_write (c, (ut8 *)&j, 4);
|
2011-08-04 19:28:07 +00:00
|
|
|
r_socket_write (c, buf, i);
|
|
|
|
}
|
|
|
|
snprintf ((char *)buf, sizeof (buf),
|
|
|
|
"f %s %"PFMT64d" 0x%08"PFMT64x,
|
|
|
|
flag->name, flag->size, flag->offset);
|
|
|
|
i = strlen ((char *)buf);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !endian);
|
|
|
|
r_socket_write (c, (ut8 *)&j, 4);
|
2011-08-04 19:28:07 +00:00
|
|
|
r_socket_write (c, buf, i);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2011-08-04 19:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snprintf ((char *)buf, sizeof (buf), "s 0x%"PFMT64x, core->offset);
|
|
|
|
i = strlen ((char *)buf);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !endian);
|
|
|
|
r_socket_write (c, (ut8 *)&j, 4);
|
2011-08-04 19:28:07 +00:00
|
|
|
r_socket_write (c, buf, i);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
r_socket_write (c, (ut8 *)&i, 4);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2010-08-23 04:39:23 +00:00
|
|
|
free (ptr);
|
2010-08-22 19:42:08 +00:00
|
|
|
break;
|
|
|
|
case RMT_READ:
|
|
|
|
r_socket_read_block (c, (ut8*)&buf, 4);
|
2010-08-23 04:39:23 +00:00
|
|
|
r_mem_copyendian ((ut8*)&i, buf, 4, !endian);
|
|
|
|
ptr = (ut8 *)malloc (i+core->blocksize+5);
|
2010-08-22 19:42:08 +00:00
|
|
|
if (ptr==NULL) {
|
|
|
|
eprintf ("Cannot read %d bytes\n", i);
|
|
|
|
// TODO: reply error here
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
r_core_block_read (core, 0);
|
|
|
|
ptr[0] = RMT_READ|RMT_REPLY;
|
2010-08-23 04:39:23 +00:00
|
|
|
if (i>RMT_MAX)
|
|
|
|
i = RMT_MAX;
|
2011-08-04 19:28:07 +00:00
|
|
|
if (i>core->blocksize)
|
2010-08-22 19:42:08 +00:00
|
|
|
r_core_block_size (core, i);
|
2010-08-23 04:39:23 +00:00
|
|
|
r_mem_copyendian (ptr+1, (ut8 *)&i, 4, !endian);
|
2010-08-22 19:42:08 +00:00
|
|
|
memcpy (ptr+5, core->block, i); //core->blocksize);
|
2011-03-21 00:47:17 +00:00
|
|
|
r_socket_write (c, ptr, i+5);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2010-08-22 19:42:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RMT_CMD:
|
|
|
|
{
|
|
|
|
char bufr[8], *bufw = NULL;
|
|
|
|
char *cmd = NULL, *cmd_output = NULL;
|
|
|
|
int i, cmd_len = 0;
|
|
|
|
|
|
|
|
/* read */
|
|
|
|
r_socket_read_block (c, (ut8*)&bufr, 4);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_mem_copyendian ((ut8*)&i, (ut8 *)bufr, 4, !endian);
|
2010-11-08 18:30:25 +00:00
|
|
|
if (i>0 && i<RMT_MAX) {
|
2010-08-23 04:39:23 +00:00
|
|
|
if ((cmd=malloc(i))) {
|
|
|
|
r_socket_read_block (c, (ut8*)cmd, i);
|
|
|
|
cmd[i] = '\0';
|
|
|
|
eprintf ("len: %d cmd: '%s'\n",
|
|
|
|
i, cmd); fflush(stdout);
|
|
|
|
cmd_output = r_core_cmd_str (core, cmd);
|
|
|
|
free(cmd);
|
|
|
|
} else eprintf ("rap: cannot malloc\n");
|
|
|
|
} else eprintf ("rap: invalid length '%d'\n", i);
|
|
|
|
/* write */
|
|
|
|
if (cmd_output)
|
|
|
|
cmd_len = strlen(cmd_output) + 1;
|
|
|
|
else {
|
|
|
|
cmd_output = strdup("");
|
|
|
|
cmd_len = 0;
|
2010-08-22 19:42:08 +00:00
|
|
|
}
|
2010-08-23 04:39:23 +00:00
|
|
|
bufw = malloc(cmd_len + 5);
|
|
|
|
bufw[0] = RMT_CMD | RMT_REPLY;
|
|
|
|
r_mem_copyendian((ut8*)bufw+1, (ut8 *)&cmd_len, 4, !endian);
|
|
|
|
memcpy(bufw+5, cmd_output, cmd_len);
|
|
|
|
r_socket_write (c, bufw, cmd_len+5);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2010-08-23 04:39:23 +00:00
|
|
|
free(bufw);
|
|
|
|
free(cmd_output);
|
2010-08-22 19:42:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RMT_WRITE:
|
2011-03-21 00:47:17 +00:00
|
|
|
r_socket_read (c, buf, 5);
|
2010-08-22 19:42:08 +00:00
|
|
|
r_mem_copyendian((ut8 *)&x, buf+1, 4, endian);
|
|
|
|
ptr = malloc (x);
|
2011-03-21 00:47:17 +00:00
|
|
|
r_socket_read (c, ptr, x);
|
2010-08-22 19:42:08 +00:00
|
|
|
r_core_write_at (core, core->offset, ptr, x);
|
|
|
|
free (ptr);
|
|
|
|
break;
|
|
|
|
case RMT_SEEK:
|
|
|
|
r_socket_read_block (c, buf, 9);
|
2010-08-23 04:39:23 +00:00
|
|
|
r_mem_copyendian((ut8 *)&x, buf+1, 8, !endian);
|
|
|
|
if (buf[0]!=2) {
|
|
|
|
r_core_seek (core, x, buf[0]);
|
|
|
|
x = core->offset;
|
|
|
|
} else x = core->file->size;
|
2010-08-22 19:42:08 +00:00
|
|
|
buf[0] = RMT_SEEK | RMT_REPLY;
|
2010-08-23 04:39:23 +00:00
|
|
|
r_mem_copyendian (buf+1, (ut8*)&x, 8, !endian);
|
|
|
|
r_socket_write (c, buf, 9);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2010-08-22 19:42:08 +00:00
|
|
|
break;
|
|
|
|
case RMT_CLOSE:
|
2010-08-23 04:39:23 +00:00
|
|
|
eprintf ("CLOSE\n");
|
2010-08-22 19:42:08 +00:00
|
|
|
// XXX : proper shutdown
|
|
|
|
r_socket_read_block (c, buf, 4);
|
|
|
|
r_mem_copyendian ((ut8*)&i, buf, 4, endian);
|
|
|
|
{
|
2011-03-21 00:47:17 +00:00
|
|
|
//FIXME: Use r_socket_close
|
|
|
|
int ret = close (i);
|
2010-08-23 04:39:23 +00:00
|
|
|
r_mem_copyendian (buf+1, (ut8*)&ret, 4, !endian);
|
2010-08-22 19:42:08 +00:00
|
|
|
buf[0] = RMT_CLOSE | RMT_REPLY;
|
2010-08-23 04:39:23 +00:00
|
|
|
r_socket_write (c, buf, 5);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2010-08-22 19:42:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RMT_SYSTEM:
|
|
|
|
// read
|
|
|
|
r_socket_read_block (c, buf, 4);
|
2010-08-23 04:39:23 +00:00
|
|
|
r_mem_copyendian((ut8*)&i, buf, 4, !endian);
|
|
|
|
if (i>0&&i<RMT_MAX) {
|
|
|
|
ptr = (ut8 *) malloc(i+6);
|
|
|
|
ptr[5]='!';
|
|
|
|
r_socket_read_block (c, ptr+6, i);
|
|
|
|
ptr[6+i]='\0';
|
|
|
|
//env_update();
|
|
|
|
//pipe_stdout_to_tmp_file((char*)&buf, (char*)ptr+5);
|
|
|
|
strcpy((char*)buf, "/tmp/.out");
|
|
|
|
pipefd = r_cons_pipe_open ((const char *)buf, 0);
|
2011-02-04 10:30:08 +00:00
|
|
|
//eprintf("SYSTEM(%s)\n", ptr+6);
|
2010-08-23 04:39:23 +00:00
|
|
|
system((const char*)ptr+6);
|
|
|
|
r_cons_pipe_close (pipefd);
|
|
|
|
{
|
|
|
|
FILE *fd = fopen((char*)buf, "r");
|
|
|
|
i = 0;
|
|
|
|
if (fd == NULL) {
|
|
|
|
eprintf("Cannot open tmpfile\n");
|
|
|
|
i = -1;
|
|
|
|
} else {
|
|
|
|
fseek(fd, 0, SEEK_END);
|
|
|
|
i = ftell(fd);
|
|
|
|
fseek(fd, 0, SEEK_SET);
|
|
|
|
free(ptr);
|
|
|
|
ptr = (ut8 *) malloc(i+5);
|
|
|
|
fread(ptr+5, i, 1, fd);
|
|
|
|
ptr[i+5]='\0';
|
|
|
|
fclose(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
char *out = r_file_slurp ((char*)buf, &i);
|
|
|
|
free(ptr);
|
2011-02-04 10:30:08 +00:00
|
|
|
//eprintf("PIPE(%s)\n", out);
|
2010-08-23 04:39:23 +00:00
|
|
|
ptr = (ut8 *) malloc(i+5);
|
|
|
|
if (ptr) {
|
|
|
|
memcpy (ptr+5, out, i);
|
|
|
|
free(out);
|
|
|
|
}
|
2010-08-22 19:42:08 +00:00
|
|
|
}
|
2010-08-23 04:39:23 +00:00
|
|
|
//unlink((char*)buf);
|
2010-08-22 19:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send
|
|
|
|
ptr[0] = (RMT_SYSTEM | RMT_REPLY);
|
2011-08-17 17:50:40 +00:00
|
|
|
r_mem_copyendian ((ut8*)ptr+1, (ut8*)&i, 4, !endian);
|
2010-08-22 19:42:08 +00:00
|
|
|
if (i<0)i=0;
|
2010-08-23 04:39:23 +00:00
|
|
|
r_socket_write (c, ptr, i+5);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2010-08-22 19:42:08 +00:00
|
|
|
eprintf ("REPLY SENT (%d) (%s)\n", i, ptr+5);
|
|
|
|
free (ptr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
eprintf ("unknown command 0x%02x\n", cmd);
|
|
|
|
r_socket_close (c);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-08-23 04:39:23 +00:00
|
|
|
eprintf ("client: disconnected\n");
|
2010-08-22 19:42:08 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2010-11-15 10:06:10 +00:00
|
|
|
|
|
|
|
R_API int r_core_search_cb(RCore *core, ut64 from, ut64 to, RCoreSearchCallback cb) {
|
|
|
|
int ret, len = core->blocksize;
|
|
|
|
ut8 *buf;
|
|
|
|
if ((buf = malloc (len)) == NULL)
|
|
|
|
eprintf ("Cannot allocate blocksize\n");
|
|
|
|
else while (from<to) {
|
|
|
|
ut64 delta = to-from;
|
|
|
|
if (delta<len)
|
|
|
|
len = (int)delta;
|
|
|
|
if (!r_io_read_at (core->io, from, buf, len)) {
|
|
|
|
eprintf ("Cannot read at 0x%"PFMT64x"\n", from);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (ret=0; ret<len;) {
|
|
|
|
int done = cb (core, from, buf+ret, len-ret);
|
|
|
|
if (done<1) /* interrupted */
|
|
|
|
return R_FALSE;
|
|
|
|
ret += done;
|
|
|
|
}
|
|
|
|
from += len;
|
|
|
|
}
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
2011-05-21 12:27:46 +00:00
|
|
|
|
|
|
|
R_API char *r_core_editor (RCore *core, const char *str) {
|
|
|
|
char *name, *ret;
|
|
|
|
int len, fd = r_file_mkstemp ("r2editor", &name);
|
|
|
|
if (fd == -1)
|
|
|
|
return NULL;
|
|
|
|
if (str) write (fd, str, strlen (str));
|
|
|
|
close (fd);
|
|
|
|
r_sys_cmdf ("%s %s", r_config_get (core->config, "cfg.editor"), name);
|
|
|
|
ret = r_file_slurp (name, &len);
|
|
|
|
ret[len-1] = 0; // chop
|
|
|
|
r_file_rm (name);
|
|
|
|
free (name);
|
|
|
|
return ret;
|
|
|
|
}
|