Rizin
unix-like reverse engineering framework and cli tools
io_fd.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2017-2020 condret <condr3t@protonmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_io.h>
5 
6 RZ_API int rz_io_fd_open(RzIO *io, const char *uri, int flags, int mode) {
7  RzIODesc *desc = rz_io_desc_open(io, uri, flags, mode);
8  return desc ? desc->fd : -1;
9 }
10 
11 RZ_API bool rz_io_fd_close(RzIO *io, int fd) {
12  return rz_io_desc_close(rz_io_desc_get(io, fd));
13 }
14 
15 // returns length of read bytes
16 RZ_API int rz_io_fd_read(RzIO *io, int fd, ut8 *buf, int len) {
17  rz_return_val_if_fail(io && buf, -1);
18  if (len < 0) {
19  return -1;
20  }
22  return desc ? rz_io_desc_read(desc, buf, len) : -1;
23 }
24 
25 // returns length of written bytes
26 RZ_API int rz_io_fd_write(RzIO *io, int fd, const ut8 *buf, int len) {
27  rz_return_val_if_fail(io && buf, -1);
28  if (len < 0) {
29  return -1;
30  }
32  return desc ? rz_io_desc_write(desc, buf, len) : -1;
33 }
34 
35 RZ_API ut64 rz_io_fd_seek(RzIO *io, int fd, ut64 addr, int whence) {
36  if (!io) {
37  return (ut64)-2;
38  }
39  return rz_io_desc_seek(rz_io_desc_get(io, fd), addr, whence);
40 }
41 
43  return rz_io_desc_size(rz_io_desc_get(io, fd));
44 }
45 
56 }
57 
58 RZ_API bool rz_io_fd_resize(RzIO *io, int fd, ut64 newsize) {
59  return rz_io_desc_resize(rz_io_desc_get(io, fd), newsize);
60 }
61 
64 }
65 
68 }
69 
70 // returns length of read bytes
71 RZ_API int rz_io_fd_read_at(RzIO *io, int fd, ut64 addr, ut8 *buf, int len) {
72  RzIODesc *desc;
73  if (!io || !buf || (len < 1) || !(desc = rz_io_desc_get(io, fd))) {
74  return 0;
75  }
76  return rz_io_desc_read_at(desc, addr, buf, len);
77 }
78 
79 // returns length of written bytes
80 RZ_API int rz_io_fd_write_at(RzIO *io, int fd, ut64 addr, const ut8 *buf, int len) {
81  rz_return_val_if_fail(io && buf, false);
83  return desc ? rz_io_desc_write_at(desc, addr, buf, len) : -1;
84 }
85 
86 RZ_API bool rz_io_fd_is_dbg(RzIO *io, int fd) {
87  rz_return_val_if_fail(io && io->files, false);
89  return desc ? rz_io_desc_is_dbg(desc) : false;
90 }
91 
92 RZ_API int rz_io_fd_get_pid(RzIO *io, int fd) {
93  if (!io || !io->files) {
94  return -2;
95  }
97  return rz_io_desc_get_pid(desc);
98 }
99 
101  rz_return_val_if_fail(io && io->files, -2);
102  RzIODesc *desc = rz_io_desc_get(io, fd);
103  return rz_io_desc_get_tid(desc);
104 }
105 
106 RZ_API bool rz_io_fd_get_base(RzIO *io, int fd, ut64 *base) {
107  rz_return_val_if_fail(io && io->files && base, false);
108  RzIODesc *desc = rz_io_desc_get(io, fd);
109  return rz_io_desc_get_base(desc, base);
110 }
111 
112 RZ_API const char *rz_io_fd_get_name(RzIO *io, int fd) {
113  rz_return_val_if_fail(io && io->files, NULL);
114  RzIODesc *desc = rz_io_desc_get(io, fd);
115  return desc ? desc->name : NULL;
116 }
117 
118 RZ_API bool rz_io_use_fd(RzIO *io, int fd) {
119  rz_return_val_if_fail(io, false);
120  if (!io->desc) {
121  io->desc = rz_io_desc_get(io, fd);
122  return io->desc != NULL;
123  }
124  if (io->desc->fd != fd) {
125  RzIODesc *desc;
126  // update io->desc if fd is not the same
127  if (!(desc = rz_io_desc_get(io, fd))) {
128  return false;
129  }
130  io->desc = desc;
131  }
132  return true;
133 }
134 
136  rz_return_val_if_fail(io, -1);
137  if (io->desc) {
138  return io->desc->fd;
139  }
140  return -1;
141 }
142 
144  rz_return_val_if_fail(io, -1);
145  int ret = fd;
146  if (!rz_id_storage_get_next(io->files, (ut32 *)&ret)) {
147  return -1;
148  }
149  return ret;
150 }
151 
153  rz_return_val_if_fail(io, -1);
154  int ret = fd;
155  if (!rz_id_storage_get_prev(io->files, (ut32 *)&ret)) {
156  return -1;
157  }
158  return ret;
159 }
160 
162  rz_return_val_if_fail(io, -1);
163  int fd = -1;
164  if (!rz_id_storage_get_highest(io->files, (ut32 *)&fd)) {
165  return -1;
166  }
167  return fd;
168 }
169 
171  rz_return_val_if_fail(io, -1);
172  int fd = -1;
173  if (!rz_id_storage_get_lowest(io->files, (ut32 *)&fd)) {
174  return -1;
175  }
176  return fd;
177 }
size_t len
Definition: 6502dis.c:15
const char * desc
Definition: bin_vsf.c:19
#define RZ_API
#define NULL
Definition: cris-opc.c:27
uint32_t ut32
RZ_API bool rz_io_fd_resize(RzIO *io, int fd, ut64 newsize)
Definition: io_fd.c:58
RZ_API bool rz_io_fd_is_dbg(RzIO *io, int fd)
Definition: io_fd.c:86
RZ_API ut64 rz_io_fd_size(RzIO *io, int fd)
Definition: io_fd.c:42
RZ_API const char * rz_io_fd_get_name(RzIO *io, int fd)
Definition: io_fd.c:112
RZ_API bool rz_io_fd_is_chardevice(RzIO *io, int fd)
Definition: io_fd.c:66
RZ_API bool rz_io_fd_get_base(RzIO *io, int fd, ut64 *base)
Definition: io_fd.c:106
RZ_API int rz_io_fd_get_tid(RzIO *io, int fd)
Definition: io_fd.c:100
RZ_API int rz_io_fd_open(RzIO *io, const char *uri, int flags, int mode)
Definition: io_fd.c:6
RZ_API bool rz_io_fd_is_blockdevice(RzIO *io, int fd)
Definition: io_fd.c:62
RZ_API ut8 * rz_io_fd_get_buf(RzIO *io, int fd, RZ_OUT RZ_NONNULL ut64 *size)
Returns the underlying buffer of the file descriptor.
Definition: io_fd.c:53
RZ_API int rz_io_fd_read(RzIO *io, int fd, ut8 *buf, int len)
Definition: io_fd.c:16
RZ_API int rz_io_fd_read_at(RzIO *io, int fd, ut64 addr, ut8 *buf, int len)
Definition: io_fd.c:71
RZ_API int rz_io_fd_write_at(RzIO *io, int fd, ut64 addr, const ut8 *buf, int len)
Definition: io_fd.c:80
RZ_API int rz_io_fd_write(RzIO *io, int fd, const ut8 *buf, int len)
Definition: io_fd.c:26
RZ_API int rz_io_fd_get_current(RzIO *io)
Definition: io_fd.c:135
RZ_API int rz_io_fd_get_pid(RzIO *io, int fd)
Definition: io_fd.c:92
RZ_API ut64 rz_io_fd_seek(RzIO *io, int fd, ut64 addr, int whence)
Definition: io_fd.c:35
RZ_API int rz_io_fd_get_highest(RzIO *io)
Definition: io_fd.c:161
RZ_API bool rz_io_use_fd(RzIO *io, int fd)
Definition: io_fd.c:118
RZ_API int rz_io_fd_get_prev(RzIO *io, int fd)
Definition: io_fd.c:152
RZ_API bool rz_io_fd_close(RzIO *io, int fd)
Definition: io_fd.c:11
RZ_API int rz_io_fd_get_lowest(RzIO *io)
Definition: io_fd.c:170
RZ_API int rz_io_fd_get_next(RzIO *io, int fd)
Definition: io_fd.c:143
voidpf void uLong size
Definition: ioapi.h:138
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API bool rz_id_storage_get_prev(RzIDStorage *storage, ut32 *id)
Definition: idpool.c:206
RZ_API bool rz_id_storage_get_lowest(RzIDStorage *storage, ut32 *id)
Definition: idpool.c:169
RZ_API bool rz_id_storage_get_next(RzIDStorage *storage, ut32 *id)
Definition: idpool.c:191
RZ_API bool rz_id_storage_get_highest(RzIDStorage *storage, ut32 *id)
Definition: idpool.c:178
RZ_API bool rz_io_desc_is_blockdevice(RzIODesc *desc)
Definition: io_desc.c:261
RZ_API int rz_io_desc_write(RzIODesc *desc, const ut8 *buf, int count)
Definition: io_desc.c:183
RZ_API ut64 rz_io_desc_seek(RzIODesc *desc, ut64 offset, int whence)
Definition: io_desc.c:217
RZ_API ut64 rz_io_desc_size(RzIODesc *desc)
Definition: io_desc.c:224
RZ_API int rz_io_desc_get_tid(RzIODesc *desc)
Definition: io_desc.c:327
RZ_API bool rz_io_desc_resize(RzIODesc *desc, ut64 newsize)
Definition: io_desc.c:250
RZ_API int rz_io_desc_read_at(RzIODesc *desc, ut64 addr, ut8 *buf, int len)
Definition: io_desc.c:351
RZ_API int rz_io_desc_read(RzIODesc *desc, ut8 *buf, int count)
Definition: io_desc.c:197
RZ_API RzIODesc * rz_io_desc_get(RzIO *io, int fd)
Definition: io_desc.c:73
RZ_API int rz_io_desc_get_pid(RzIODesc *desc)
Definition: io_desc.c:310
RZ_API bool rz_io_desc_is_chardevice(RzIODesc *desc)
Definition: io_desc.c:268
RZ_API int rz_io_desc_write_at(RzIODesc *desc, ut64 addr, const ut8 *buf, int len)
Definition: io_desc.c:358
RZ_API bool rz_io_desc_is_dbg(RzIODesc *desc)
Definition: io_desc.c:303
RZ_API bool rz_io_desc_close(RzIODesc *desc)
Definition: io_desc.c:165
RZ_API bool rz_io_desc_get_base(RzIODesc *desc, ut64 *base)
Definition: io_desc.c:344
RZ_API RzIODesc * rz_io_desc_open(RzIO *io, const char *uri, int flags, int mode)
Definition: io_desc.c:112
RZ_API ut8 * rz_io_desc_get_buf(RzIODesc *desc, RZ_OUT RZ_NONNULL ut64 *size)
Returns the underlying buffer of the io descriptor.
Definition: io_desc.c:241
#define RZ_OUT
Definition: rz_types.h:51
#define RZ_NONNULL
Definition: rz_types.h:64
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
int fd
Definition: rz_io.h:96
Definition: rz_io.h:59
RzIDStorage * files
Definition: rz_io.h:75
struct rz_io_desc_t * desc
Definition: rz_io.h:60
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997
static int addr
Definition: z80asm.c:58