Rizin
unix-like reverse engineering framework and cli tools
buf_bytes.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_bytes_user {
7  const ut8 *data;
8  const ut8 *data_steal;
10  bool steal;
11 };
12 
14  ut8 *buf;
18 };
19 
20 static inline struct buf_bytes_priv *get_priv_bytes(RzBuffer *b) {
21  struct buf_bytes_priv *priv = (struct buf_bytes_priv *)b->priv;
22  rz_warn_if_fail(priv);
23  return priv;
24 }
25 
26 static bool buf_bytes_init(RzBuffer *b, const void *user) {
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 }
52 
53 static bool buf_bytes_fini(RzBuffer *b) {
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 }
61 
62 static bool buf_bytes_resize(RzBuffer *b, ut64 newsize) {
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 }
75 
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 }
83 
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 }
96 
98  struct buf_bytes_priv *priv = get_priv_bytes(b);
99  return priv->length;
100 }
101 
102 static st64 buf_bytes_seek(RzBuffer *b, st64 addr, int whence) {
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 }
109 
111  struct buf_bytes_priv *priv = get_priv_bytes(b);
112  if (sz) {
113  *sz = priv->length;
114  }
115  return priv->buf;
116 }
117 
119  .init = buf_bytes_init,
120  .fini = buf_bytes_fini,
121  .read = buf_bytes_read,
122  .write = buf_bytes_write,
123  .get_size = buf_bytes_get_size,
124  .resize = buf_bytes_resize,
125  .seek = buf_bytes_seek,
126  .get_whole_buf = buf_bytes_get_whole_buf
127 };
size_t len
Definition: 6502dis.c:15
static struct buf_bytes_priv * get_priv_bytes(RzBuffer *b)
Definition: buf_bytes.c:20
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 const RzBufferMethods buffer_bytes_methods
Definition: buf_bytes.c:118
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
#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
return memset(p, 0, total)
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
void * malloc(size_t size)
Definition: malloc.c:123
#define rz_warn_if_fail(expr)
Definition: rz_assert.h:35
RZ_API bool rz_buf_resize(RZ_NONNULL RzBuffer *b, ut64 newsize)
Resize the buffer size.
Definition: buf.c:890
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_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
bool is_bufowner
Definition: buf_bytes.c:17
const ut8 * data
Definition: buf_bytes.c:7
const ut8 * data_steal
Definition: buf_bytes.c:8
RzBufferInit init
Definition: rz_buf.h:32
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58