Rizin
unix-like reverse engineering framework and cli tools
io_rzweb.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015 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 typedef struct {
12  int fd;
13  char *url;
14 } RzIOR2Web;
15 
16 #define rFD(x) (((RzIOR2Web *)(x)->data)->fd)
17 #define rURL(x) (((RzIOR2Web *)(x)->data)->url)
18 
19 static int __write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count) {
20  int code, rlen;
21  char *out, *url, *hexbuf;
22  if (!fd || !fd->data) {
23  return -1;
24  }
25  if (count * 3 < count) {
26  return -1;
27  }
28  hexbuf = malloc(count * 3);
29  if (!hexbuf) {
30  return -1;
31  }
32  hexbuf[0] = 0;
33  rz_hex_bin2str(buf, count, hexbuf);
34  url = rz_str_newf("%s/wx%%20%s@%" PFMT64d,
35  rURL(fd), hexbuf, io->off);
36  out = rz_socket_http_get(url, &code, &rlen);
37  free(out);
38  free(url);
39  free(hexbuf);
40  return count;
41 }
42 
43 static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count) {
44  int code, rlen;
45  char *out, *url;
46  int ret = 0;
47  if (!fd || !fd->data) {
48  return -1;
49  }
50  url = rz_str_newf("%s/p8%%20%d@0x%" PFMT64x,
51  rURL(fd), count, io->off);
52  out = rz_socket_http_get(url, &code, &rlen);
53  if (out && rlen > 0) {
54  ut8 *tmp = calloc(1, rlen + 1);
55  if (!tmp) {
56  goto beach;
57  }
58  ret = rz_hex_str2bin(out, tmp);
59  memcpy(buf, tmp, RZ_MIN(count, rlen));
60  free(tmp);
61  if (ret < 0) {
62  ret = -ret;
63  }
64  }
65 
66 beach:
67  free(out);
68  free(url);
69  return ret;
70 }
71 
72 static int __close(RzIODesc *fd) {
73  RzIOR2Web *riom;
74  if (!fd || !fd->data) {
75  return -1;
76  }
77  riom = fd->data;
78  RZ_FREE(riom->url);
79  RZ_FREE(fd->data);
80  return 0;
81 }
82 
83 static ut64 __lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence) {
84  switch (whence) {
85  case SEEK_SET: return offset;
86  case SEEK_CUR: return io->off + offset;
87  case SEEK_END: return UT64_MAX;
88  }
89  return offset;
90 }
91 
92 static bool __plugin_open(RzIO *io, const char *pathname, bool many) {
93  const char *uri = "rzweb://";
94  return (!strncmp(pathname, uri, strlen(uri)));
95 }
96 
97 static inline int getmalfd(RzIOR2Web *mal) {
98  return 0xfffffff & (int)(size_t)mal;
99 }
100 
101 static RzIODesc *__open(RzIO *io, const char *pathname, int rw, int mode) {
102  char *out;
103  int rlen, code;
104  if (__plugin_open(io, pathname, 0)) {
106  if (!mal) {
107  return NULL;
108  }
109  char *path = strdup(pathname + 8);
110  int path_len = strlen(path);
111  if (path_len > 0) {
112  if (path[path_len - 1] == '/') {
113  path[path_len - 1] = 0;
114  }
115  }
116  char *url = rz_str_newf("http://%s/?V", path);
117  // eprintf ("URL:(%s)\n", url);
118  out = rz_socket_http_get(url, &code, &rlen);
119  // eprintf ("RES %d %d\n", code, rlen);
120  // eprintf ("OUT(%s)\n", out);
121  if (out && rlen > 0) {
122  mal->fd = getmalfd(mal);
123  mal->url = rz_str_newf("http://%s", path);
124  free(path);
125  free(out);
126  free(url);
128  pathname, rw, mode, mal);
129  }
130  free(url);
131  free(mal);
132  free(out);
133  free(path);
134  eprintf("Error: Try http://localhost:9090/cmd/");
135  }
136  return NULL;
137 }
138 
139 static char *__system(RzIO *io, RzIODesc *fd, const char *command) {
140  if (!*command) {
141  return NULL;
142  }
143  int code, rlen;
144  char *cmd = rz_str_uri_encode(command);
145  char *url = rz_str_newf("%s/%s", rURL(fd), cmd);
146  char *out = rz_socket_http_get(url, &code, &rlen);
147  if (out && rlen > 0) {
148  io->cb_printf("%s", out);
149  }
150  free(out);
151  free(url);
152  free(cmd);
153  return NULL;
154 }
155 
157  .name = "rzweb",
158  .desc = "rzweb io client plugin",
159  .uris = "rzweb://",
160  .license = "LGPL3",
161  .open = __open,
162  .close = __close,
163  .read = __read,
164  .check = __plugin_open,
165  .lseek = __lseek,
166  .system = __system,
167  .write = __write,
168 };
169 
170 #ifndef RZ_PLUGIN_INCORE
172  .type = RZ_LIB_TYPE_IO,
173  .data = &rz_io_plugin_rzweb,
175 };
176 #endif
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
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
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 cmd
Definition: sflib.h:79
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count)
Definition: io_rzweb.c:43
static bool __plugin_open(RzIO *io, const char *pathname, bool many)
Definition: io_rzweb.c:92
static RzIODesc * __open(RzIO *io, const char *pathname, int rw, int mode)
Definition: io_rzweb.c:101
RZ_API RzLibStruct rizin_plugin
Definition: io_rzweb.c:171
#define rURL(x)
Definition: io_rzweb.c:17
static int __write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
Definition: io_rzweb.c:19
static ut64 __lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence)
Definition: io_rzweb.c:83
static int __close(RzIODesc *fd)
Definition: io_rzweb.c:72
static int getmalfd(RzIOR2Web *mal)
Definition: io_rzweb.c:97
static char * __system(RzIO *io, RzIODesc *fd, const char *command)
Definition: io_rzweb.c:139
RzIOPlugin rz_io_plugin_rzweb
Definition: io_rzweb.c:156
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
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
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
url
Definition: setup.py:262
const char * code
Definition: pal.c:98
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API int rz_hex_str2bin(const char *in, ut8 *out)
Convert an input string in into the binary form in out.
Definition: hex.c:444
RZ_API int rz_hex_bin2str(const ut8 *in, int len, char *out)
Definition: hex.c:382
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 char * rz_socket_http_get(const char *url, int *code, int *rlen)
Definition: socket_http.c:287
RZ_API char * rz_str_uri_encode(const char *buf)
Definition: str.c:2860
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define PFMT64d
Definition: rz_types.h:394
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_FREE(x)
Definition: rz_types.h:369
#define PFMT64x
Definition: rz_types.h:393
#define RZ_MIN(x, y)
#define UT64_MAX
Definition: rz_types_base.h:86
#define RZ_VERSION
Definition: rz_version.h:8
static int
Definition: sfsocketcall.h:114
int fd
Definition: io_rzweb.c:12
char * url
Definition: io_rzweb.c:13
Definition: inftree9.h:24
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
PrintfCallback cb_printf
Definition: rz_io.h:91
const char * command
Definition: main.c:7
static struct @626 mal
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