Rizin
unix-like reverse engineering framework and cli tools
io_rzpipe.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015-2019 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "rz_io.h"
5 #include "rz_lib.h"
6 #include "rz_socket.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 
11 /* --------------------------------------------------------- */
12 #define RZP(x) ((RzPipe *)(x)->data)
13 
14 // TODO: add rzpipe_assert
15 
16 static int __write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count) {
17  char fmt[4096];
18  char *bufn, bufnum[4096];
19  int i, rv, rescount = -1;
20  if (!fd || !fd->data) {
21  return -1;
22  }
23  bufn = bufnum;
24  *bufn = 0;
25  for (i = 0; i < count; i++) {
26  int bufn_sz = sizeof(bufnum) - (bufn - bufnum);
27  snprintf(bufn, bufn_sz, "%s%d", i ? "," : "", buf[i]);
28  bufn += strlen(bufn);
29  }
30  int len = snprintf(fmt, sizeof(fmt),
31  "{\"op\":\"write\",\"address\":%" PFMT64d ",\"data\":[%s]}",
32  io->off, bufnum);
33  if (len >= sizeof(fmt)) {
34  eprintf("rzpipe_write: error, fmt string has been truncated\n");
35  return -1;
36  }
37  rv = rzpipe_write(RZP(fd), fmt);
38  if (rv < 1) {
39  eprintf("rzpipe_write: error\n");
40  return -1;
41  }
42  rzpipe_read(RZP(fd));
43 
44  /* TODO: parse json back */
45  return rescount;
46 }
47 
48 static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count) {
49  char fmt[4096], num[128];
50  int rv, rescount = -1;
51  int bufi, numi;
52  char *res, *r;
53  if (!fd || !fd->data) {
54  return -1;
55  }
56  if (count > 1024) {
57  count = 1024;
58  }
59  snprintf(fmt, sizeof(fmt),
60  "{\"op\":\"read\",\"address\":%" PFMT64d ",\"count\":%d}",
61  io->off, count);
62  rv = rzpipe_write(RZP(fd), fmt);
63  if (rv < 1) {
64  eprintf("rzpipe_write: error\n");
65  return -1;
66  }
67  res = rzpipe_read(RZP(fd));
68 
69  /* TODO: parse json back */
70  r = strstr(res, "result");
71  if (r) {
72  rescount = atoi(r + 6 + 2);
73  }
74  r = strstr(res, "data");
75  if (r) {
76  char *arr = strchr(r, ':');
77  if (!arr || arr[1] != '[') {
78  goto beach;
79  }
80  arr += 2;
81  for (num[0] = numi = bufi = 0; bufi < count && *arr; arr++) {
82  switch (*arr) {
83  case '0':
84  case '1':
85  case '2':
86  case '3':
87  case '4':
88  case '5':
89  case '6':
90  case '7':
91  case '8':
92  case '9':
93  num[numi++] = *arr;
94  num[numi] = 0;
95  break;
96  case ' ':
97  case ',':
98  case ']':
99  if (num[0]) {
100  buf[bufi++] = atoi(num);
101  num[numi = 0] = 0;
102  }
103  break;
104  case 'n':
105  case 'u':
106  case 'l':
107  break;
108  default:
109  goto beach;
110  break;
111  }
112  }
113  }
114 beach:
115  free(res);
116  return rescount;
117 }
118 
119 static int __close(RzIODesc *fd) {
120  if (!fd || !fd->data) {
121  return -1;
122  }
123  rzpipe_close(fd->data);
124  fd->data = NULL;
125  return 0;
126 }
127 
128 static ut64 __lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence) {
129  switch (whence) {
130  case SEEK_SET: return offset;
131  case SEEK_CUR: return io->off + offset;
132  case SEEK_END: return UT64_MAX;
133  }
134  return offset;
135 }
136 
137 static bool __check(RzIO *io, const char *pathname, bool many) {
138  return (!strncmp(pathname, "rzpipe://", 9));
139 }
140 
141 static RzIODesc *__open(RzIO *io, const char *pathname, int rw, int mode) {
142  RzPipe *rzp = NULL;
143  if (__check(io, pathname, 0)) {
144  rzp = rzpipe_open(pathname + 9);
145  }
146  return rzp ? rz_io_desc_new(io, &rz_io_plugin_rzpipe,
147  pathname, rw, mode, rzp)
148  : NULL;
149 }
150 
151 static char *__system(RzIO *io, RzIODesc *fd, const char *msg) {
152  rz_return_val_if_fail(io && fd && msg, NULL);
153  PJ *pj = pj_new();
154  pj_o(pj);
155  pj_ks(pj, "op", "system");
156  pj_ks(pj, "cmd", msg);
157  pj_end(pj);
158  const char *fmt = pj_string(pj);
159  int rv = rzpipe_write(RZP(fd), fmt);
160  pj_free(pj);
161  if (rv < 1) {
162  eprintf("rzpipe_write: error\n");
163  return NULL;
164  }
165  char *res = rzpipe_read(RZP(fd));
166  // eprintf ("%s\n", res);
167  /* TODO: parse json back */
168  char *r = strstr(res, "result");
169  if (r) {
170  int rescount = atoi(r + 6 + 1);
171  eprintf("RESULT %d\n", rescount);
172  }
173  free(res);
174  return NULL;
175 }
176 
178  .name = "rzpipe",
179  .desc = "rzpipe io plugin",
180  .license = "MIT",
181  .uris = "rzpipe://",
182  .open = __open,
183  .close = __close,
184  .read = __read,
185  .check = __check,
186  .lseek = __lseek,
187  .write = __write,
188  .system = __system
189 };
190 
191 #ifndef RZ_PLUGIN_INCORE
193  .type = RZ_LIB_TYPE_IO,
194  .data = &rz_io_plugin_rzpipe,
196 };
197 #endif
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static bool __check(RzIO *io, const char *pathname, bool many)
Definition: io_rzpipe.c:137
static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count)
Definition: io_rzpipe.c:48
static char * __system(RzIO *io, RzIODesc *fd, const char *msg)
Definition: io_rzpipe.c:151
#define RZP(x)
Definition: io_rzpipe.c:12
static RzIODesc * __open(RzIO *io, const char *pathname, int rw, int mode)
Definition: io_rzpipe.c:141
RZ_API RzLibStruct rizin_plugin
Definition: io_rzpipe.c:192
RzIOPlugin rz_io_plugin_rzpipe
Definition: io_rzpipe.c:177
static int __write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
Definition: io_rzpipe.c:16
static ut64 __lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence)
Definition: io_rzpipe.c:128
static int __close(RzIODesc *fd)
Definition: io_rzpipe.c:119
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
snprintf
Definition: kernel.h:364
uint8_t ut8
Definition: lh5801.h:11
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char pathname
Definition: sflib.h:66
#define eprintf(x, y...)
Definition: rlcc.c:7
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API RzIODesc * rz_io_desc_new(RzIO *io, RzIOPlugin *plugin, const char *uri, int flags, int mode, void *data)
Definition: io_desc.c:11
@ RZ_LIB_TYPE_IO
Definition: rz_lib.h:69
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87
RZ_API const char * pj_string(PJ *pj)
Definition: pj.c:57
RZ_API void pj_free(PJ *j)
Definition: pj.c:34
RZ_API PJ * pj_o(PJ *j)
Definition: pj.c:75
RZ_API PJ * pj_ks(PJ *j, const char *k, const char *v)
Definition: pj.c:170
RZ_API int rzpipe_close(RzPipe *rzpipe)
Definition: rzpipe.c:110
RZ_API RzPipe * rzpipe_open(const char *cmd)
Definition: rzpipe.c:235
RZ_API char * rzpipe_read(RzPipe *rzpipe)
Definition: rzpipe.c:61
RZ_API int rzpipe_write(RzPipe *rzpipe, const char *str)
Definition: rzpipe.c:36
#define PFMT64d
Definition: rz_types.h:394
#define UT64_MAX
Definition: rz_types_base.h:86
#define RZ_VERSION
Definition: rz_version.h:8
static struct sockaddr static addrlen static backlog const void msg
Definition: sfsocketcall.h:119
Definition: rz_pj.h:12
const char * name
Definition: rz_io.h:115
const char * version
Definition: rz_io.h:117
Definition: rz_io.h:59
ut64 off
Definition: rz_io.h:61
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
#define SEEK_END
Definition: zip.c:84