Rizin
unix-like reverse engineering framework and cli tools
buf_ref.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 
6 struct buf_ref_user {
10 };
11 
12 struct buf_ref_priv {
17 };
18 
19 static inline struct buf_ref_priv *get_priv_ref(RzBuffer *b) {
20  struct buf_ref_priv *priv = (struct buf_ref_priv *)b->priv;
21  rz_warn_if_fail(priv);
22  return priv;
23 }
24 
25 static bool buf_ref_init(RzBuffer *b, const void *user) {
26  const struct buf_ref_user *u = (const struct buf_ref_user *)user;
27  struct buf_ref_priv *priv = RZ_NEW0(struct buf_ref_priv);
28  if (!priv) {
29  return false;
30  }
31 
32  // NOTE: we only support readonly ref-buffers for now. Supporting
33  // read-write would mean to choose how we want to handle writing to the
34  // referencer. Copy-on-write? Write to the buffer underneath?
35  ut64 parent_sz = rz_buf_size(u->parent);
36  b->readonly = true;
37  priv->parent = rz_buf_ref(u->parent);
38  priv->base = RZ_MIN(u->offset, parent_sz);
39  priv->size = RZ_MIN(parent_sz - priv->base, u->size);
40  b->priv = priv;
41  return true;
42 }
43 
44 static bool buf_ref_fini(RzBuffer *b) {
45  struct buf_ref_priv *priv = get_priv_ref(b);
46  rz_buf_free(priv->parent);
47  RZ_FREE(b->priv);
48  return true;
49 }
50 
51 static bool buf_ref_resize(RzBuffer *b, ut64 newsize) {
52  struct buf_ref_priv *priv = get_priv_ref(b);
53  ut64 parent_sz = rz_buf_size(priv->parent);
54  priv->size = RZ_MIN(parent_sz - priv->base, newsize);
55  return true;
56 }
57 
59  struct buf_ref_priv *priv = get_priv_ref(b);
60  if (priv->size < priv->cur) {
61  return -1;
62  }
63  len = RZ_MIN(len, priv->size - priv->cur);
64  st64 r = rz_buf_read_at(priv->parent, priv->base + priv->cur, buf, len);
65  if (r < 0) {
66  return r;
67  }
68  priv->cur += r;
69  return r;
70 }
71 
73  struct buf_ref_priv *priv = get_priv_ref(b);
74  return priv->size;
75 }
76 
77 static st64 buf_ref_seek(RzBuffer *b, st64 addr, int whence) {
78  struct buf_ref_priv *priv = get_priv_ref(b);
79  switch (whence) {
80  case RZ_BUF_CUR:
81  priv->cur += addr;
82  break;
83  case RZ_BUF_SET:
84  priv->cur = addr;
85  break;
86  case RZ_BUF_END:
87  priv->cur = priv->size + addr;
88  break;
89  default:
91  return -1;
92  }
93  return priv->cur;
94 }
95 
97  .init = buf_ref_init,
98  .fini = buf_ref_fini,
99  .read = buf_ref_read,
100  .get_size = buf_ref_get_size,
101  .resize = buf_ref_resize,
102  .seek = buf_ref_seek,
103 };
size_t len
Definition: 6502dis.c:15
static const RzBufferMethods buffer_ref_methods
Definition: buf_ref.c:96
static st64 buf_ref_read(RzBuffer *b, ut8 *buf, ut64 len)
Definition: buf_ref.c:58
static ut64 buf_ref_get_size(RzBuffer *b)
Definition: buf_ref.c:72
static st64 buf_ref_seek(RzBuffer *b, st64 addr, int whence)
Definition: buf_ref.c:77
static bool buf_ref_resize(RzBuffer *b, ut64 newsize)
Definition: buf_ref.c:51
static struct buf_ref_priv * get_priv_ref(RzBuffer *b)
Definition: buf_ref.c:19
static bool buf_ref_fini(RzBuffer *b)
Definition: buf_ref.c:44
static bool buf_ref_init(RzBuffer *b, const void *user)
Definition: buf_ref.c:25
#define r
Definition: crypto_rc6.c:12
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
RZ_API RzBuffer * rz_buf_ref(RzBuffer *b)
Increment the reference count of the buffer.
Definition: buf.c:668
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136
#define RZ_BUF_END
Definition: rz_buf.h:16
#define RZ_BUF_SET
Definition: rz_buf.h:14
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_FREE(x)
Definition: rz_types.h:369
#define RZ_MIN(x, y)
#define st64
Definition: rz_types_base.h:10
#define b(i)
Definition: sha256.c:42
ut64 size
Definition: buf_ref.c:16
ut64 cur
Definition: buf_ref.c:14
RzBuffer * parent
Definition: buf_ref.c:13
ut64 base
Definition: buf_ref.c:15
RzBuffer * parent
Definition: buf_ref.c:7
ut64 offset
Definition: buf_ref.c:8
ut64 size
Definition: buf_ref.c:9
RzBufferInit init
Definition: rz_buf.h:32
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58