Rizin
unix-like reverse engineering framework and cli tools
io_rzpipe.c File Reference
#include "rz_io.h"
#include "rz_lib.h"
#include "rz_socket.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

Go to the source code of this file.

Macros

#define RZP(x)   ((RzPipe *)(x)->data)
 

Functions

static int __write (RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
 
static int __read (RzIO *io, RzIODesc *fd, ut8 *buf, int count)
 
static int __close (RzIODesc *fd)
 
static ut64 __lseek (RzIO *io, RzIODesc *fd, ut64 offset, int whence)
 
static bool __check (RzIO *io, const char *pathname, bool many)
 
static RzIODesc__open (RzIO *io, const char *pathname, int rw, int mode)
 
static char * __system (RzIO *io, RzIODesc *fd, const char *msg)
 

Variables

RzIOPlugin rz_io_plugin_rzpipe
 
RZ_API RzLibStruct rizin_plugin
 

Macro Definition Documentation

◆ RZP

#define RZP (   x)    ((RzPipe *)(x)->data)

Definition at line 12 of file io_rzpipe.c.

Function Documentation

◆ __check()

static bool __check ( RzIO io,
const char *  pathname,
bool  many 
)
static

Definition at line 137 of file io_rzpipe.c.

137  {
138  return (!strncmp(pathname, "rzpipe://", 9));
139 }
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

References pathname.

Referenced by __open().

◆ __close()

static int __close ( RzIODesc fd)
static

Definition at line 119 of file io_rzpipe.c.

119  {
120  if (!fd || !fd->data) {
121  return -1;
122  }
123  rzpipe_close(fd->data);
124  fd->data = NULL;
125  return 0;
126 }
#define NULL
Definition: cris-opc.c:27
RZ_API int rzpipe_close(RzPipe *rzpipe)
Definition: rzpipe.c:110
static const z80_opcode fd[]
Definition: z80_tab.h:997

References fd, NULL, and rzpipe_close().

◆ __lseek()

static ut64 __lseek ( RzIO io,
RzIODesc fd,
ut64  offset,
int  whence 
)
static

Definition at line 128 of file io_rzpipe.c.

128  {
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 }
voidpf uLong offset
Definition: ioapi.h:144
#define UT64_MAX
Definition: rz_types_base.h:86
ut64 off
Definition: rz_io.h:61
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
#define SEEK_END
Definition: zip.c:84

References rz_io_t::off, SEEK_CUR, SEEK_END, SEEK_SET, and UT64_MAX.

◆ __open()

static RzIODesc* __open ( RzIO io,
const char *  pathname,
int  rw,
int  mode 
)
static

Definition at line 141 of file io_rzpipe.c.

141  {
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 }
static bool __check(RzIO *io, const char *pathname, bool many)
Definition: io_rzpipe.c:137
RzIOPlugin rz_io_plugin_rzpipe
Definition: io_rzpipe.c:177
const char int mode
Definition: ioapi.h:137
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_API RzPipe * rzpipe_open(const char *cmd)
Definition: rzpipe.c:235

References __check(), NULL, pathname, rz_io_desc_new(), rz_io_plugin_rzpipe, and rzpipe_open().

◆ __read()

static int __read ( RzIO io,
RzIODesc fd,
ut8 buf,
int  count 
)
static

Definition at line 48 of file io_rzpipe.c.

48  {
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 }
#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
#define RZP(x)
Definition: io_rzpipe.c:12
voidpf void * buf
Definition: ioapi.h:138
snprintf
Definition: kernel.h:364
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
#define eprintf(x, y...)
Definition: rlcc.c:7
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

References syscall_preprocessing::arr, count, eprintf, fd, free(), num, rz_io_t::off, PFMT64d, r, RZP, rzpipe_read(), rzpipe_write(), and snprintf.

◆ __system()

static char* __system ( RzIO io,
RzIODesc fd,
const char *  msg 
)
static

Definition at line 151 of file io_rzpipe.c.

151  {
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 }
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
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
static struct sockaddr static addrlen static backlog const void msg
Definition: sfsocketcall.h:119
Definition: rz_pj.h:12

References eprintf, fd, free(), msg, NULL, pj_end(), pj_free(), pj_ks(), pj_new(), pj_o(), pj_string(), r, rz_return_val_if_fail, RZP, rzpipe_read(), and rzpipe_write().

◆ __write()

static int __write ( RzIO io,
RzIODesc fd,
const ut8 buf,
int  count 
)
static

Definition at line 16 of file io_rzpipe.c.

16  {
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 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629

References count, eprintf, fd, i, len, rz_io_t::off, PFMT64d, RZP, rzpipe_read(), rzpipe_write(), and snprintf.

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_IO,
}
@ RZ_LIB_TYPE_IO
Definition: rz_lib.h:69
#define RZ_VERSION
Definition: rz_version.h:8
const char * version
Definition: rz_io.h:117

Definition at line 192 of file io_rzpipe.c.

◆ rz_io_plugin_rzpipe

RzIOPlugin rz_io_plugin_rzpipe
Initial value:
= {
.name = "rzpipe",
.desc = "rzpipe io plugin",
.license = "MIT",
.uris = "rzpipe://",
.open = __open,
.close = __close,
.read = __read,
.check = __check,
.lseek = __lseek,
.write = __write,
.system = __system
}
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
static RzIODesc * __open(RzIO *io, const char *pathname, int rw, int mode)
Definition: io_rzpipe.c:141
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

Definition at line 177 of file io_rzpipe.c.

Referenced by __open().