Fix r2pipe:// (initial \x00 issue and cmd chop messages properly)

This commit is contained in:
pancake 2016-08-27 02:32:43 +02:00
parent 66772837c0
commit 7cf0e1b633
4 changed files with 44 additions and 26 deletions

View File

@ -1,23 +1,25 @@
/* radare - LGPL - Copyright 2009-2015 - pancake */
/* radare - LGPL - Copyright 2009-2016 - pancake */
#include <r_core.h>
/* maybe move this into RCore */
static bool marks_init = false;
static ut64 marks[UT8_MAX+1];
static ut64 marks[UT8_MAX + 1];
R_API void r_core_visual_mark_reset(RCore *core) {
int i;
marks_init = true;
for (i=0; i<UT8_MAX; i++)
for (i = 0; i < UT8_MAX; i++) {
marks[i] = UT64_MAX;
}
}
R_API void r_core_visual_mark_dump(RCore *core) {
int i;
if (!marks_init)
if (!marks_init) {
return;
for (i=0; i<UT8_MAX; i++) {
}
for (i = 0; i < UT8_MAX; i++) {
if (marks[i] != UT64_MAX) {
r_cons_printf ("fV %d 0x%"PFMT64x"\n", i, marks[i]);
}
@ -36,6 +38,7 @@ R_API void r_core_visual_mark(RCore *core, ut8 ch) {
}
R_API void r_core_visual_mark_seek(RCore *core, ut8 ch) {
if (marks_init && marks[ch] != UT64_MAX)
if (marks_init && marks[ch] != UT64_MAX) {
r_core_seek (core, marks[ch], 1);
}
}

View File

@ -11,12 +11,12 @@ if (!nfd_in || !nfd_out) {
var fd_in = fs.createReadStream(null, {fd: nfd_in});
var fd_out = fs.createWriteStream(null, {fd: nfd_out});
console.log ("Running r2pipe io using fds: ", nfd_in, nfd_out);
console.error ("Running r2pipe io using fds: ", nfd_in, nfd_out);
fd_in.on('data', function(data) {
data = data.slice(0,-1);
var obj_in = JSON.parse (data);
console.log ("got data(",obj_in,")");
console.error ("got data(",obj_in,")");
var obj = {result:obj_in.count, data:[1,2,3]};
fd_out.write (JSON.stringify (obj)+"\x00");
});

View File

@ -37,37 +37,47 @@ static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
res = r2p_read (R2P (fd));
/* TODO: parse json back */
r = strstr (res, "result");
if (r) { count = atoi (r+6+1); }
if (r) {
count = atoi (r + 6 + 1);
}
free (res);
return rescount;
}
static int __read(RIO *io, RIODesc *fd, ut8 *buf, const int count) {
static int __read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
char fmt[4096], num[128];
int rv, rescount = -1;
int bufi, numi;
char *res, *r;
if (fd == NULL || fd->data == NULL)
if (!fd || !fd->data) {
return -1;
}
if (count > 1024) {
count = 1024;
}
snprintf (fmt, sizeof (fmt),
"{\"op\":\"read\",\"address\":%"PFMT64d",\"count\":%d}",
io->off, count);
rv = r2p_write (R2P (fd), fmt);
if (rv <1) {
if (rv < 1) {
eprintf ("r2p_write: error\n");
return -1;
}
res = r2p_read (R2P (fd));
/* TODO: parse json back */
r = strstr (res, "result");
if (r) { rescount = atoi (r+6+2); }
if (r) {
rescount = atoi (r + 6 + 2);
}
r = strstr (res, "data");
if (r) {
char *arr = strchr (r, ':');
if (!arr) goto beach;
if (arr[1]!='[') goto beach;
if (!arr || arr[1]!='[') {
goto beach;
}
arr += 2;
for (num[0]=numi=bufi=0; bufi<count && *arr; arr++) {
for (num[0] = numi = bufi = 0; bufi < count && *arr; arr++) {
switch (*arr) {
case '0'...'9':
num[numi++] = *arr;
@ -114,14 +124,14 @@ static ut64 __lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
return offset;
}
static bool __plugin_open(RIO *io, const char *pathname, bool many) {
static bool __check(RIO *io, const char *pathname, bool many) {
return (!strncmp (pathname, "r2pipe://", 9));
}
static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) {
R2Pipe *r2p = NULL;
if (__plugin_open (io, pathname, 0)) {
r2p = r2p_open (pathname+9);
if (__check (io, pathname, 0)) {
r2p = r2p_open (pathname + 9);
}
return r2p? r_io_desc_new (&r_io_plugin_r2pipe,
r2p->child, pathname, rw, mode, r2p): NULL;
@ -156,7 +166,7 @@ RIOPlugin r_io_plugin_r2pipe = {
.open = __open,
.close = __close,
.read = __read,
.check = __plugin_open,
.check = __check,
.lseek = __lseek,
.write = __write,
.system = __system

View File

@ -1,6 +1,7 @@
/* radare - LGPL - Copyright 2015 - pancake */
/* radare - LGPL - Copyright 2015-2016 - pancake */
#include <r_util.h>
#include <r_cons.h>
#include <r_socket.h>
#define R2P_MAGIC 0x329193
@ -133,12 +134,17 @@ R_API R2Pipe *r2p_open(const char *cmd) {
if (r2p->child) {
char ch;
eprintf ("Child is %d\n", r2p->child);
eprintf ("[+] r2pipe child is %d\n", r2p->child);
#if 0
if (read (r2p->output[0], &ch, 1) != 1) {
eprintf ("Failed to read 1 byte\n");
r2p_close (r2p);
return NULL;
}
if (ch == 0x00) {
eprintf ("[+] r2pipe-io link stablished\n");
}
#endif
} else {
int rc = 0;
if (cmd && *cmd) {
@ -198,14 +204,13 @@ R_API char *r2p_cmdf(R2Pipe *r2p, const char *fmt, ...) {
R_API int r2p_write(R2Pipe *r2p, const char *str) {
char *cmd;
int ret, len;
if (!r2p || !str)
if (!r2p || !str) {
return -1;
}
len = strlen (str) + 1; /* include \x00 */
cmd = malloc (len + 2);
if (!cmd) return 0;
memcpy (cmd, str, len);
cmd[len++] = '\n';
cmd[len] = 0;
#if __WINDOWS__ && !defined(__CYGWIN__)
DWORD dwWritten = -1;
WriteFile (r2p->pipe, cmd, len, &dwWritten, NULL);
@ -233,7 +238,7 @@ R_API char *r2p_read(R2Pipe *r2p) {
if (!bSuccess || !buf[0]) {
return NULL;
}
if (dwRead>0) {
if (dwRead > 0) {
buf[dwRead] = 0;
}
buf[bufsz-1] = 0;