Rizin
unix-like reverse engineering framework and cli tools
io_fd.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2020 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 <stdio.h>
7 #include <stdlib.h>
8 #include <rz_cons.h>
9 #include <sys/types.h>
10 
11 #if __WINDOWS__
12 #define FDURI "handle://"
13 #else
14 #define FDURI "fd://"
15 #endif
16 
17 typedef struct {
18  int fd;
19 } RzIOFdata;
20 
21 static int __write(RzIO *io, RzIODesc *desc, const ut8 *buf, int count) {
22  RzIOFdata *fdd = (RzIOFdata *)desc->data;
23  if (fdd) {
24  return write(fdd->fd, buf, count);
25  }
26  return -1;
27 }
28 
29 static bool __resize(RzIO *io, RzIODesc *desc, ut64 count) {
30  RzIOFdata *fdd = (RzIOFdata *)desc->data;
31  if (fdd) {
32  return ftruncate(fdd->fd, count) == 0;
33  }
34  return false;
35 }
36 
37 static int __read(RzIO *io, RzIODesc *desc, ut8 *buf, int count) {
38  RzIOFdata *fdd = (RzIOFdata *)desc->data;
39  if (fdd) {
41  int res = read(fdd->fd, buf, count);
43  return res;
44  }
45  return -1;
46 }
47 
48 static int __close(RzIODesc *desc) {
49  RZ_FREE(desc->data);
50  return 0;
51 }
52 
53 static ut64 __lseek(RzIO *io, RzIODesc *desc, ut64 offset, int whence) {
54  RzIOFdata *fdd = (RzIOFdata *)desc->data;
55  if (fdd) {
56  return lseek(fdd->fd, offset, whence);
57  }
58  return 0;
59 }
60 
61 static bool __check(RzIO *io, const char *pathname, bool many) {
62  return !strncmp(pathname, FDURI, strlen(FDURI));
63 }
64 
65 static RzIODesc *__open(RzIO *io, const char *pathname, int rw, int mode) {
66  if (!__check(io, pathname, 0)) {
67  return NULL;
68  }
69  RzIOFdata *fdd = RZ_NEW0(RzIOFdata);
70  if (fdd) {
71  fdd->fd = rz_num_math(NULL, pathname + strlen(FDURI));
72 #if __WINDOWS__
73  fdd->fd = _open_osfhandle(fdd->fd, 0);
74 #endif
75  if (fdd->fd < 0) {
76  free(fdd);
77  eprintf("Invalid filedescriptor.\n");
78  return NULL;
79  }
80  }
81  return rz_io_desc_new(io, &rz_io_plugin_fd, pathname, RZ_PERM_RW | rw, mode, fdd);
82 }
83 
85 #if __WINDOWS__
86  .name = "handle",
87  .desc = "Local process file handle IO",
88 #else
89  .name = "fd",
90  .desc = "Local process filedescriptor IO",
91 #endif
92  .uris = FDURI,
93  .license = "MIT",
94  .open = __open,
95  .close = __close,
96  .read = __read,
97  .check = __check,
98  .lseek = __lseek,
99  .write = __write,
100  .resize = __resize,
101 };
102 
103 #ifndef RZ_PLUGIN_INCORE
105  .type = RZ_LIB_TYPE_IO,
106  .data = &rz_io_plugin_fd,
108 };
109 #endif
const char * desc
Definition: bin_vsf.c:19
RZ_API void rz_cons_break_pop(void)
Definition: cons.c:361
RZ_API void rz_cons_break_push(RzConsBreak cb, void *user)
Definition: cons.c:357
#define RZ_API
#define NULL
Definition: cris-opc.c:27
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 static offset struct stat static buf void long static basep static whence ftruncate
Definition: sflib.h:113
static static fork write
Definition: sflib.h:33
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 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 static offset struct stat static buf void long static basep lseek
Definition: sflib.h:113
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
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
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
static bool __check(RzIO *io, const char *pathname, bool many)
Definition: io_fd.c:61
#define FDURI
Definition: io_fd.c:14
static int __close(RzIODesc *desc)
Definition: io_fd.c:48
static RzIODesc * __open(RzIO *io, const char *pathname, int rw, int mode)
Definition: io_fd.c:65
RZ_API RzLibStruct rizin_plugin
Definition: io_fd.c:104
static bool __resize(RzIO *io, RzIODesc *desc, ut64 count)
Definition: io_fd.c:29
static int __read(RzIO *io, RzIODesc *desc, ut8 *buf, int count)
Definition: io_fd.c:37
static ut64 __lseek(RzIO *io, RzIODesc *desc, ut64 offset, int whence)
Definition: io_fd.c:53
RzIOPlugin rz_io_plugin_fd
Definition: io_fd.c:84
static int __write(RzIO *io, RzIODesc *desc, const ut8 *buf, int count)
Definition: io_fd.c:21
#define eprintf(x, y...)
Definition: rlcc.c:7
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 ut64 rz_num_math(RzNum *num, const char *str)
Definition: unum.c:456
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_PERM_RW
Definition: rz_types.h:96
#define RZ_FREE(x)
Definition: rz_types.h:369
#define RZ_VERSION
Definition: rz_version.h:8
int fd
Definition: io_fd.c:18
const char * name
Definition: rz_io.h:115
const char * version
Definition: rz_io.h:117
Definition: rz_io.h:59
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115