static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) { char fmt[4096]; char *bufn, bufnum[4096]; int i, rv, rescount = -1; char *res, *r; if (!fd || !fd->data) { return -1; } bufn = bufnum; *bufn = 0; for (i=0; i<count; i++) { int bufn_sz = sizeof (bufnum) - (bufn-bufnum); snprintf (bufn, bufn_sz, "%s%d", i?",":"", buf[i]); bufn += strlen (bufn); } snprintf (fmt, sizeof (fmt), "{\"op\":\"write\",\"address\":%"PFMT64d",\"data\":[%s]}", io->off, bufnum); rv = r2p_write (R2P (fd), fmt); 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) { count = atoi (r + 6 + 1); } free (res); return rescount; }
R_API char *r2p_cmd(R2Pipe *r2p, const char *str) { if (!r2p_write (r2p, str)) { perror ("r2p_write"); return NULL; } return r2p_read (r2p); }
static int __read(RIO *io, RIODesc *fd, ut8 *buf, const int count) { char fmt[4096], num[128]; int rv, rescount = -1; int bufi, numi; char *res, *r; if (fd == NULL || fd->data == NULL) return -1; snprintf (fmt, sizeof (fmt), "{\"op\":\"read\",\"address\":%"PFMT64d",\"count\":%d}", io->off, count); rv = r2p_write (R2P (fd), fmt); 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); } r = strstr (res, "data"); if (r) { char *arr = strchr (r, ':'); if (!arr) goto beach; if (arr[1]!='[') goto beach; arr += 2; for (num[0]=numi=bufi=0; bufi<count && *arr; arr++) { switch (*arr) { case '0'...'9': num[numi++] = *arr; num[numi] = 0; break; case ' ': case ',': case ']': if (num[0]) { buf[bufi++] = atoi (num); num[numi = 0] = 0; } break; case 'n': case 'u': case 'l': break; default: goto beach; break; } } } beach: free (res); return rescount; }
static int __system(RIO *io, RIODesc *fd, const char *msg) { char fmt[4096]; int rv, rescount = -1; char *res, *r; if (fd == NULL || fd->data == NULL) return -1; snprintf (fmt, sizeof (fmt), "{\"op\":\"system\",\"cmd\":\"%s\"}", msg); rv = r2p_write (R2P (fd), fmt); if (rv <1) { eprintf ("r2p_write: error\n"); return -1; } res = r2p_read (R2P (fd)); //eprintf ("%s\n", res); /* TODO: parse json back */ r = strstr (res, "result"); if (r) { rescount = atoi (r+6+1); } free (res); return rescount; }