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

Go to the source code of this file.

Classes

struct  buf_bytes_user
 
struct  buf_bytes_priv
 

Functions

static struct buf_bytes_privget_priv_bytes (RzBuffer *b)
 
static bool buf_bytes_init (RzBuffer *b, const void *user)
 
static bool buf_bytes_fini (RzBuffer *b)
 
static bool buf_bytes_resize (RzBuffer *b, ut64 newsize)
 
static st64 buf_bytes_read (RzBuffer *b, ut8 *buf, ut64 len)
 
static st64 buf_bytes_write (RzBuffer *b, const ut8 *buf, ut64 len)
 
static ut64 buf_bytes_get_size (RzBuffer *b)
 
static st64 buf_bytes_seek (RzBuffer *b, st64 addr, int whence)
 
static ut8buf_bytes_get_whole_buf (RzBuffer *b, ut64 *sz)
 

Variables

static const RzBufferMethods buffer_bytes_methods
 

Function Documentation

◆ buf_bytes_fini()

static bool buf_bytes_fini ( RzBuffer b)
static

Definition at line 53 of file buf_bytes.c.

53  {
54  struct buf_bytes_priv *priv = get_priv_bytes(b);
55  if (priv->is_bufowner) {
56  free(priv->buf);
57  }
58  RZ_FREE(b->priv);
59  return true;
60 }
static struct buf_bytes_priv * get_priv_bytes(RzBuffer *b)
Definition: buf_bytes.c:20
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define RZ_FREE(x)
Definition: rz_types.h:369
#define b(i)
Definition: sha256.c:42
bool is_bufowner
Definition: buf_bytes.c:17

References b, buf_bytes_priv::buf, free(), get_priv_bytes(), buf_bytes_priv::is_bufowner, and RZ_FREE.

◆ buf_bytes_get_size()

static ut64 buf_bytes_get_size ( RzBuffer b)
static

Definition at line 97 of file buf_bytes.c.

97  {
98  struct buf_bytes_priv *priv = get_priv_bytes(b);
99  return priv->length;
100 }

References b, get_priv_bytes(), and buf_bytes_priv::length.

◆ buf_bytes_get_whole_buf()

static ut8* buf_bytes_get_whole_buf ( RzBuffer b,
ut64 sz 
)
static

Definition at line 110 of file buf_bytes.c.

110  {
111  struct buf_bytes_priv *priv = get_priv_bytes(b);
112  if (sz) {
113  *sz = priv->length;
114  }
115  return priv->buf;
116 }

References b, buf_bytes_priv::buf, get_priv_bytes(), and buf_bytes_priv::length.

◆ buf_bytes_init()

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

Definition at line 26 of file buf_bytes.c.

26  {
27  const struct buf_bytes_user *u = (const struct buf_bytes_user *)user;
28  struct buf_bytes_priv *priv = RZ_NEW0(struct buf_bytes_priv);
29  if (!priv) {
30  return false;
31  }
32 
33  priv->offset = 0;
34  priv->length = u->length;
35  if (u->data_steal) {
36  priv->buf = (ut8 *)u->data_steal;
37  priv->is_bufowner = u->steal;
38  } else {
39  priv->buf = malloc(priv->length);
40  if (!priv->buf) {
41  free(priv);
42  return false;
43  }
44  if (priv->length) {
45  memmove(priv->buf, u->data, priv->length);
46  }
47  priv->is_bufowner = true;
48  }
49  b->priv = priv;
50  return true;
51 }
uint8_t ut8
Definition: lh5801.h:11
void * malloc(size_t size)
Definition: malloc.c:123
#define RZ_NEW0(x)
Definition: rz_types.h:284
const ut8 * data
Definition: buf_bytes.c:7
const ut8 * data_steal
Definition: buf_bytes.c:8

References b, buf_bytes_priv::buf, buf_bytes_user::data, buf_bytes_user::data_steal, free(), buf_bytes_priv::is_bufowner, buf_bytes_user::length, buf_bytes_priv::length, malloc(), buf_bytes_priv::offset, RZ_NEW0, and buf_bytes_user::steal.

◆ buf_bytes_read()

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

Definition at line 76 of file buf_bytes.c.

76  {
77  struct buf_bytes_priv *priv = get_priv_bytes(b);
78  ut64 real_len = priv->length < priv->offset ? 0 : RZ_MIN(priv->length - priv->offset, len);
79  memmove(buf, priv->buf + priv->offset, real_len);
80  priv->offset += real_len;
81  return real_len;
82 }
size_t len
Definition: 6502dis.c:15
voidpf void * buf
Definition: ioapi.h:138
#define RZ_MIN(x, y)
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References b, buf_bytes_priv::buf, get_priv_bytes(), len, buf_bytes_priv::length, buf_bytes_priv::offset, RZ_MIN, and ut64().

◆ buf_bytes_resize()

static bool buf_bytes_resize ( RzBuffer b,
ut64  newsize 
)
static

Definition at line 62 of file buf_bytes.c.

62  {
63  struct buf_bytes_priv *priv = get_priv_bytes(b);
64  if (newsize > priv->length) {
65  ut8 *t = realloc(priv->buf, newsize);
66  if (!t) {
67  return false;
68  }
69  priv->buf = t;
70  memset(priv->buf + priv->length, b->Oxff_priv, newsize - priv->length);
71  }
72  priv->length = newsize;
73  return true;
74 }
return memset(p, 0, total)
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144

References b, buf_bytes_priv::buf, get_priv_bytes(), buf_bytes_priv::length, memset(), and realloc().

◆ buf_bytes_seek()

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

Definition at line 102 of file buf_bytes.c.

102  {
103  struct buf_bytes_priv *priv = get_priv_bytes(b);
104  if (addr < 0 && (-addr) > (st64)priv->offset) {
105  return -1;
106  }
107  return priv->offset = rz_seek_offset(priv->offset, priv->length, addr, whence);
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 st64
Definition: rz_types_base.h:10
static int addr
Definition: z80asm.c:58

References addr, b, get_priv_bytes(), buf_bytes_priv::length, buf_bytes_priv::offset, rz_seek_offset(), and st64.

◆ buf_bytes_write()

static st64 buf_bytes_write ( RzBuffer b,
const ut8 buf,
ut64  len 
)
static

Definition at line 84 of file buf_bytes.c.

84  {
85  struct buf_bytes_priv *priv = get_priv_bytes(b);
86  if (priv->offset > priv->length || priv->offset + len >= priv->length) {
87  bool r = rz_buf_resize(b, priv->offset + len);
88  if (!r) {
89  return -1;
90  }
91  }
92  memmove(priv->buf + priv->offset, buf, len);
93  priv->offset += len;
94  return len;
95 }
#define r
Definition: crypto_rc6.c:12
RZ_API bool rz_buf_resize(RZ_NONNULL RzBuffer *b, ut64 newsize)
Resize the buffer size.
Definition: buf.c:890

References b, buf_bytes_priv::buf, get_priv_bytes(), len, buf_bytes_priv::length, buf_bytes_priv::offset, r, and rz_buf_resize().

◆ get_priv_bytes()

static struct buf_bytes_priv* get_priv_bytes ( RzBuffer b)
inlinestatic

Definition at line 20 of file buf_bytes.c.

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

References b, and rz_warn_if_fail.

Referenced by buf_bytes_fini(), buf_bytes_get_size(), buf_bytes_get_whole_buf(), buf_bytes_read(), buf_bytes_resize(), buf_bytes_seek(), and buf_bytes_write().

Variable Documentation

◆ buffer_bytes_methods

const RzBufferMethods buffer_bytes_methods
static
Initial value:
= {
.init = buf_bytes_init,
.fini = buf_bytes_fini,
.read = buf_bytes_read,
.write = buf_bytes_write,
.get_size = buf_bytes_get_size,
.resize = buf_bytes_resize,
.seek = buf_bytes_seek,
.get_whole_buf = buf_bytes_get_whole_buf
}
static st64 buf_bytes_read(RzBuffer *b, ut8 *buf, ut64 len)
Definition: buf_bytes.c:76
static bool buf_bytes_resize(RzBuffer *b, ut64 newsize)
Definition: buf_bytes.c:62
static ut64 buf_bytes_get_size(RzBuffer *b)
Definition: buf_bytes.c:97
static st64 buf_bytes_seek(RzBuffer *b, st64 addr, int whence)
Definition: buf_bytes.c:102
static bool buf_bytes_init(RzBuffer *b, const void *user)
Definition: buf_bytes.c:26
static ut8 * buf_bytes_get_whole_buf(RzBuffer *b, ut64 *sz)
Definition: buf_bytes.c:110
static st64 buf_bytes_write(RzBuffer *b, const ut8 *buf, ut64 len)
Definition: buf_bytes.c:84
static bool buf_bytes_fini(RzBuffer *b)
Definition: buf_bytes.c:53

Definition at line 118 of file buf_bytes.c.

Referenced by new_buffer().