Rizin
unix-like reverse engineering framework and cli tools
buf_io_fd.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2020 ret2libc <sirmy15@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_util.h>
5 #include <rz_io.h>
6 
7 struct buf_io_fd_user {
9  int fd;
10 };
11 
14  int fd;
15 };
16 
17 static inline struct buf_io_fd_priv *get_priv_io(RzBuffer *b) {
18  struct buf_io_fd_priv *priv = (struct buf_io_fd_priv *)b->priv;
19  rz_warn_if_fail(priv);
20  return priv;
21 }
22 
23 static bool buf_io_fd_init(RzBuffer *b, const void *user) {
24  const struct buf_io_fd_user *u = (const struct buf_io_fd_user *)user;
25  struct buf_io_fd_priv *priv = RZ_NEW0(struct buf_io_fd_priv);
26  if (!priv) {
27  return false;
28  }
29  priv->iob = u->iob;
30  priv->fd = u->fd;
31  b->priv = priv;
32  b->fd = priv->fd;
33  return true;
34 }
35 
36 static bool buf_io_fd_fini(RzBuffer *b) {
37  // struct buf_io_fd_priv *priv = get_priv_io (b);
38  RZ_FREE(b->priv);
39  return true;
40 }
41 
42 static st64 buf_io_fd_seek(RzBuffer *b, st64 addr, int whence) {
43  struct buf_io_fd_priv *priv = get_priv_io(b);
44  int io_whence;
45 
46  switch (whence) {
47  default:
49  // fallthrough
50  case RZ_BUF_SET:
51  io_whence = RZ_IO_SEEK_SET;
52  break;
53  case RZ_BUF_END:
54  io_whence = RZ_IO_SEEK_END;
55  break;
56  case RZ_BUF_CUR:
57  io_whence = RZ_IO_SEEK_CUR;
58  break;
59  }
60  return priv->iob->fd_seek(priv->iob->io, priv->fd, addr, io_whence);
61 }
62 
64  struct buf_io_fd_priv *priv = get_priv_io(b);
65  return priv->iob->fd_size(priv->iob->io, priv->fd);
66 }
67 
68 static bool buf_io_fd_resize(RzBuffer *b, ut64 newsize) {
69  struct buf_io_fd_priv *priv = get_priv_io(b);
70  return priv->iob->fd_resize(priv->iob->io, priv->fd, newsize);
71 }
72 
74  struct buf_io_fd_priv *priv = get_priv_io(b);
75  return priv->iob->fd_read(priv->iob->io, priv->fd, buf, len);
76 }
77 
79  struct buf_io_fd_priv *priv = get_priv_io(b);
80  return priv->iob->fd_write(priv->iob->io, priv->fd, buf, len);
81 }
82 
84  struct buf_io_fd_priv *priv = get_priv_io(b);
85  return priv->iob->fd_getbuf(priv->iob->io, priv->fd, size);
86 }
87 
90  .fini = buf_io_fd_fini,
91  .read = buf_io_fd_read,
92  .write = buf_io_fd_write,
93  .get_size = buf_io_fd_get_size,
94  .resize = buf_io_fd_resize,
95  .seek = buf_io_fd_seek,
96  .get_whole_buf = buf_io_fd_get_whole_buf
97 };
size_t len
Definition: 6502dis.c:15
static ut64 buf_io_fd_get_size(RzBuffer *b)
Definition: buf_io_fd.c:63
static st64 buf_io_fd_seek(RzBuffer *b, st64 addr, int whence)
Definition: buf_io_fd.c:42
static bool buf_io_fd_init(RzBuffer *b, const void *user)
Definition: buf_io_fd.c:23
static st64 buf_io_fd_write(RzBuffer *b, const ut8 *buf, ut64 len)
Definition: buf_io_fd.c:78
static struct buf_io_fd_priv * get_priv_io(RzBuffer *b)
Definition: buf_io_fd.c:17
static const RzBufferMethods buffer_io_fd_methods
Definition: buf_io_fd.c:88
static st64 buf_io_fd_read(RzBuffer *b, ut8 *buf, ut64 len)
Definition: buf_io_fd.c:73
static ut8 * buf_io_fd_get_whole_buf(RzBuffer *b, ut64 *size)
Definition: buf_io_fd.c:83
static bool buf_io_fd_resize(RzBuffer *b, ut64 newsize)
Definition: buf_io_fd.c:68
static bool buf_io_fd_fini(RzBuffer *b)
Definition: buf_io_fd.c:36
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#define rz_warn_if_fail(expr)
Definition: rz_assert.h:35
#define RZ_BUF_CUR
Definition: rz_buf.h:15
#define RZ_BUF_END
Definition: rz_buf.h:16
#define RZ_BUF_SET
Definition: rz_buf.h:14
#define RZ_IO_SEEK_CUR
Definition: rz_io.h:16
#define RZ_IO_SEEK_SET
Definition: rz_io.h:15
#define RZ_IO_SEEK_END
Definition: rz_io.h:17
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_FREE(x)
Definition: rz_types.h:369
#define st64
Definition: rz_types_base.h:10
#define b(i)
Definition: sha256.c:42
RzIOBind * iob
Definition: buf_io_fd.c:13
RzIOBind * iob
Definition: buf_io_fd.c:8
RzBufferInit init
Definition: rz_buf.h:32
RzIOFdResize fd_resize
Definition: rz_io.h:247
RzIOFdSeek fd_seek
Definition: rz_io.h:245
RzIOFdGetBuf fd_getbuf
Definition: rz_io.h:256
RzIO * io
Definition: rz_io.h:232
RzIOFdWrite fd_write
Definition: rz_io.h:249
RzIOFdSize fd_size
Definition: rz_io.h:246
RzIOFdRead fd_read
Definition: rz_io.h:248
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58