Rizin
unix-like reverse engineering framework and cli tools
buf_io.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 Florian Märkl <info@florianmaerkl.de>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_util.h>
5 #include <rz_io.h>
6 
7 typedef RzIOBind *BufIOUser;
8 
9 typedef struct buf_io_priv {
13 
14 static bool buf_io_init(RzBuffer *b, const void *user) {
15  BufIOUser u = (void *)user;
16  rz_return_val_if_fail(u, false);
17  BufIOPriv *priv = RZ_NEW(BufIOPriv);
18  if (!priv) {
19  return false;
20  }
21  priv->iob = u;
22  priv->offset = 0;
23  b->priv = priv;
24  return true;
25 }
26 
27 static bool buf_io_fini(RzBuffer *b) {
28  free(b->priv);
29  return true;
30 }
31 
32 static st64 buf_io_seek(RzBuffer *b, st64 addr, int whence) {
33  BufIOPriv *priv = b->priv;
34  priv->offset = rz_seek_offset(priv->offset, 0, addr, whence);
35  // can't express the seek right if the highest bit is set,
36  // but at least tell the caller there was no error:
37  return RZ_MIN(priv->offset, ST64_MAX);
38 }
39 
41  BufIOPriv *priv = b->priv;
42  len = RZ_MIN(INT_MAX, len); // remove if read_at takes ut64 at some point
43  bool r = priv->iob->read_at(priv->iob->io, priv->offset, buf, len);
44  return r ? len : -1;
45 }
46 
47 static st64 buf_io_write(RzBuffer *b, const ut8 *buf, ut64 len) {
48  BufIOPriv *priv = b->priv;
49  len = RZ_MIN(INT_MAX, len); // remove if write_at takes ut64 at some point
50  bool r = priv->iob->write_at(priv->iob->io, priv->offset, buf, len);
51  return r ? len : -1;
52 }
53 
55  .init = buf_io_init,
56  .fini = buf_io_fini,
57  .read = buf_io_read,
58  .write = buf_io_write,
59  .seek = buf_io_seek,
60 };
size_t len
Definition: 6502dis.c:15
static st64 buf_io_read(RzBuffer *b, ut8 *buf, ut64 len)
Definition: buf_io.c:40
static bool buf_io_init(RzBuffer *b, const void *user)
Definition: buf_io.c:14
struct buf_io_priv BufIOPriv
static const RzBufferMethods buffer_io_methods
Definition: buf_io.c:54
static bool buf_io_fini(RzBuffer *b)
Definition: buf_io.c:27
static st64 buf_io_write(RzBuffer *b, const ut8 *buf, ut64 len)
Definition: buf_io.c:47
RzIOBind * BufIOUser
Definition: buf_io.c:7
static st64 buf_io_seek(RzBuffer *b, st64 addr, int whence)
Definition: buf_io.c:32
#define INT_MAX
Definition: cp-demangle.c:131
#define r
Definition: crypto_rc6.c:12
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
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
static ut64 rz_seek_offset(ut64 cur, ut64 length, st64 addr, int whence)
change cur according to addr and whence (RZ_BUF_SET/RZ_BUF_CUR/RZ_BUF_END)
Definition: rz_buf.h:67
#define RZ_NEW(x)
Definition: rz_types.h:285
#define RZ_MIN(x, y)
#define st64
Definition: rz_types_base.h:10
#define ST64_MAX
Definition: rz_types_base.h:84
#define b(i)
Definition: sha256.c:42
RzIOBind * iob
Definition: buf_io.c:10
ut64 offset
Definition: buf_io.c:11
RzBufferInit init
Definition: rz_buf.h:32
RzIOReadAt read_at
Definition: rz_io.h:240
RzIOWriteAt write_at
Definition: rz_io.h:241
RzIO * io
Definition: rz_io.h:232
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58