Rizin
unix-like reverse engineering framework and cli tools
buf_ref.c File Reference
#include <rz_util.h>

Go to the source code of this file.

Classes

struct  buf_ref_user
 
struct  buf_ref_priv
 

Functions

static struct buf_ref_privget_priv_ref (RzBuffer *b)
 
static bool buf_ref_init (RzBuffer *b, const void *user)
 
static bool buf_ref_fini (RzBuffer *b)
 
static bool buf_ref_resize (RzBuffer *b, ut64 newsize)
 
static st64 buf_ref_read (RzBuffer *b, ut8 *buf, ut64 len)
 
static ut64 buf_ref_get_size (RzBuffer *b)
 
static st64 buf_ref_seek (RzBuffer *b, st64 addr, int whence)
 

Variables

static const RzBufferMethods buffer_ref_methods
 

Function Documentation

◆ buf_ref_fini()

static bool buf_ref_fini ( RzBuffer b)
static

Definition at line 44 of file buf_ref.c.

44  {
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 }
static struct buf_ref_priv * get_priv_ref(RzBuffer *b)
Definition: buf_ref.c:19
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
#define RZ_FREE(x)
Definition: rz_types.h:369
#define b(i)
Definition: sha256.c:42
RzBuffer * parent
Definition: buf_ref.c:13

References b, get_priv_ref(), buf_ref_priv::parent, rz_buf_free(), and RZ_FREE.

◆ buf_ref_get_size()

static ut64 buf_ref_get_size ( RzBuffer b)
static

Definition at line 72 of file buf_ref.c.

72  {
73  struct buf_ref_priv *priv = get_priv_ref(b);
74  return priv->size;
75 }
ut64 size
Definition: buf_ref.c:16

References b, get_priv_ref(), and buf_ref_priv::size.

◆ buf_ref_init()

static bool buf_ref_init ( RzBuffer b,
const void *  user 
)
static

Definition at line 25 of file buf_ref.c.

25  {
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 }
RZ_API RzBuffer * rz_buf_ref(RzBuffer *b)
Increment the reference count of the buffer.
Definition: buf.c:668
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_MIN(x, y)
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
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References b, buf_ref_priv::base, buf_ref_user::offset, buf_ref_user::parent, buf_ref_priv::parent, rz_buf_ref(), rz_buf_size(), RZ_MIN, RZ_NEW0, buf_ref_user::size, buf_ref_priv::size, and ut64().

◆ buf_ref_read()

static st64 buf_ref_read ( RzBuffer b,
ut8 buf,
ut64  len 
)
static

Definition at line 58 of file buf_ref.c.

58  {
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 }
size_t len
Definition: 6502dis.c:15
#define r
Definition: crypto_rc6.c:12
voidpf void * buf
Definition: ioapi.h:138
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 st64
Definition: rz_types_base.h:10
ut64 cur
Definition: buf_ref.c:14

References b, buf_ref_priv::base, buf_ref_priv::cur, get_priv_ref(), len, buf_ref_priv::parent, r, rz_buf_read_at(), RZ_MIN, buf_ref_priv::size, and st64.

◆ buf_ref_resize()

static bool buf_ref_resize ( RzBuffer b,
ut64  newsize 
)
static

Definition at line 51 of file buf_ref.c.

51  {
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 }

References b, buf_ref_priv::base, get_priv_ref(), buf_ref_priv::parent, rz_buf_size(), RZ_MIN, buf_ref_priv::size, and ut64().

◆ buf_ref_seek()

static st64 buf_ref_seek ( RzBuffer b,
st64  addr,
int  whence 
)
static

Definition at line 77 of file buf_ref.c.

77  {
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 }
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#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
static int addr
Definition: z80asm.c:58

References addr, b, buf_ref_priv::cur, get_priv_ref(), RZ_BUF_CUR, RZ_BUF_END, RZ_BUF_SET, rz_warn_if_reached, and buf_ref_priv::size.

◆ get_priv_ref()

static struct buf_ref_priv* get_priv_ref ( RzBuffer b)
inlinestatic

Definition at line 19 of file buf_ref.c.

19  {
20  struct buf_ref_priv *priv = (struct buf_ref_priv *)b->priv;
21  rz_warn_if_fail(priv);
22  return priv;
23 }
#define rz_warn_if_fail(expr)
Definition: rz_assert.h:35

References b, and rz_warn_if_fail.

Referenced by buf_ref_fini(), buf_ref_get_size(), buf_ref_read(), buf_ref_resize(), and buf_ref_seek().

Variable Documentation

◆ buffer_ref_methods

const RzBufferMethods buffer_ref_methods
static
Initial value:
= {
.init = buf_ref_init,
.fini = buf_ref_fini,
.read = buf_ref_read,
.get_size = buf_ref_get_size,
.resize = buf_ref_resize,
.seek = buf_ref_seek,
}
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 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

Definition at line 96 of file buf_ref.c.

Referenced by new_buffer().