Rizin
unix-like reverse engineering framework and cli tools
buf.c File Reference
#include <rz_types.h>
#include <rz_util.h>
#include <rz_io.h>
#include "buf_file.c"
#include "buf_sparse.c"
#include "buf_bytes.c"
#include "buf_mmap.c"
#include "buf_io_fd.c"
#include "buf_io.c"
#include "buf_ref.c"

Go to the source code of this file.

Macros

#define GET_STRING_BUFFER_SIZE   32
 

Enumerations

enum  RzBufferType {
  RZ_BUFFER_FILE , RZ_BUFFER_IO_FD , RZ_BUFFER_IO , RZ_BUFFER_BYTES ,
  RZ_BUFFER_MMAP , RZ_BUFFER_SPARSE , RZ_BUFFER_REF
}
 

Functions

static void buf_whole_buf_free (RzBuffer *b)
 
static bool buf_init (RzBuffer *b, const void *user)
 
static bool buf_fini (RzBuffer *b)
 
static ut64 buf_get_size (RzBuffer *b)
 
static st64 buf_read (RzBuffer *b, ut8 *buf, size_t len)
 
static st64 buf_write (RzBuffer *b, const ut8 *buf, size_t len)
 
static st64 buf_seek (RzBuffer *b, st64 addr, int whence)
 
static bool buf_resize (RzBuffer *b, ut64 newsize)
 
static st64 buf_format (RzBuffer *dst, RzBuffer *src, const char *fmt, int n)
 
static bool buf_move_back (RZ_NONNULL RzBuffer *b, ut64 addr, ut64 length)
 
static ut8get_whole_buf (RzBuffer *b, ut64 *size)
 
static RzBuffernew_buffer (RzBufferType type, void *user)
 
RZ_API RZ_OWN RzBufferrz_buf_new_empty (ut64 len)
 Creates a new empty buffer with a predefined size;. More...
 
RZ_API RZ_OWN RzBufferrz_buf_new_file (const char *file, int perm, int mode)
 Creates a new buffer from a file. More...
 
RZ_API RZ_OWN RzBufferrz_buf_new_mmap (const char *filename, int perm, int mode)
 Creates a new buffer from a file using rz_file_mmap. More...
 
RZ_API RZ_OWN RzBufferrz_buf_new_slice (RzBuffer *b, ut64 offset, ut64 size)
 Creates a new buffer from a slice of another buffer. More...
 
RZ_API RZ_OWN RzBufferrz_buf_new_slurp (const char *file)
 Creates a new buffer from a file. More...
 
RZ_API RZ_OWN RzBufferrz_buf_new_sparse (ut8 Oxff)
 Creates a sparse buffer. More...
 
RZ_API RZ_OWN RzBufferrz_buf_new_sparse_overlay (RzBuffer *b, RzBufferSparseWriteMode write_mode)
 Creates a sparse buffer from a already populated buffer. More...
 
RZ_API RZ_OWN RzBufferrz_buf_new_with_buf (RzBuffer *b)
 Creates a new buffer from a source buffer. More...
 
RZ_API RZ_OWN RzBufferrz_buf_new_with_bytes (RZ_NULLABLE RZ_BORROW const ut8 *bytes, ut64 len)
 Creates a new buffer with a bytes array. More...
 
RZ_API RZ_OWN RzBufferrz_buf_new_with_io_fd (RZ_NONNULL void *iob, int fd)
 Creates a new buffer wrapping a file descriptor accessed through RzIOBind. More...
 
RZ_API RZ_OWN RzBufferrz_buf_new_with_io (RZ_NONNULL void *iob)
 Creates a new buffer wrapping the memory map exposed by RzIOBind. More...
 
RZ_API RZ_OWN RzBufferrz_buf_new_with_methods (RZ_NONNULL const RzBufferMethods *methods, void *init_user)
 Creates a new buffer with a specific back end. More...
 
RZ_API RZ_OWN RzBufferrz_buf_new_with_pointers (const ut8 *bytes, ut64 len, bool steal)
 Creates a new buffer with a bytes array. More...
 
RZ_API RZ_OWN RzBufferrz_buf_new_with_string (RZ_NONNULL const char *msg)
 Creates a new buffer from a string. More...
 
RZ_API RZ_OWN char * rz_buf_get_nstring (RZ_NONNULL RzBuffer *b, ut64 addr, size_t size)
 Get a string with a max length from the buffer. More...
 
RZ_API RZ_OWN char * rz_buf_get_string (RZ_NONNULL RzBuffer *b, ut64 addr)
 Get a string from the buffer. More...
 
RZ_API RZ_OWN char * rz_buf_to_string (RZ_NONNULL RzBuffer *b)
 Stringify the buffer. More...
 
RZ_API RzBufferrz_buf_ref (RzBuffer *b)
 Increment the reference count of the buffer. More...
 
RZ_API bool rz_buf_append_buf (RZ_NONNULL RzBuffer *b, RZ_NONNULL RzBuffer *a)
 Append the content of the buffer a to the buffer b. More...
 
RZ_API bool rz_buf_append_buf_slice (RZ_NONNULL RzBuffer *b, RZ_NONNULL RzBuffer *a, ut64 offset, ut64 size)
 Append a slice of the buffer a to the buffer b. More...
 
RZ_API bool rz_buf_append_bytes (RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, ut64 length)
 Append an array of bytes to the buffer. More...
 
RZ_API bool rz_buf_append_nbytes (RZ_NONNULL RzBuffer *b, ut64 length)
 Extend the size of the buffer. More...
 
RZ_API bool rz_buf_append_ut16 (RZ_NONNULL RzBuffer *b, ut16 n)
 Add a ut16 number at the end of the buffer. More...
 
RZ_API bool rz_buf_append_ut32 (RZ_NONNULL RzBuffer *b, ut32 n)
 Add a ut32 number at the end of the buffer. More...
 
RZ_API bool rz_buf_append_ut64 (RZ_NONNULL RzBuffer *b, ut64 n)
 Add a ut64 number at the end of the buffer. More...
 
RZ_API bool rz_buf_dump (RZ_NONNULL RzBuffer *b, RZ_NONNULL const char *file)
 Dump the content of the buffer to a file. More...
 
RZ_API bool rz_buf_fini (RzBuffer *b)
 Free all internal data hold by the buffer. More...
 
RZ_API bool rz_buf_prepend_bytes (RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, ut64 length)
 Prepend an array of bytes to the buffer. More...
 
RZ_API bool rz_buf_read8 (RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut8 *result)
 Read a byte at the cursor in the buffer. More...
 
RZ_API bool rz_buf_read8_at (RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *result)
 Read a byte at the specified address in the buffer. More...
 
RZ_API bool rz_buf_resize (RZ_NONNULL RzBuffer *b, ut64 newsize)
 Resize the buffer size. More...
 
RZ_API bool rz_buf_set_bytes (RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, ut64 length)
 Replace the content of the buffer with the bytes array. More...
 
RZ_API bool rz_buf_write8 (RZ_NONNULL RzBuffer *b, ut8 value)
 Write a byte at the cursor in the buffer. More...
 
RZ_API bool rz_buf_write8_at (RZ_NONNULL RzBuffer *b, ut64 addr, ut8 value)
 Write a byte at the specified address in the buffer. More...
 
RZ_API st64 rz_buf_append_string (RZ_NONNULL RzBuffer *b, RZ_NONNULL const char *str)
 Append a string to the buffer. More...
 
RZ_API st64 rz_buf_fread (RZ_NONNULL RzBuffer *b, RZ_NONNULL ut8 *buf, RZ_NONNULL const char *fmt, int n)
 ... More...
 
RZ_API st64 rz_buf_fread_at (RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL ut8 *buf, RZ_NONNULL const char *fmt, int n)
 ... More...
 
RZ_API st64 rz_buf_fwrite (RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, RZ_NONNULL const char *fmt, int n)
 ... More...
 
RZ_API st64 rz_buf_fwrite_at (RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL const ut8 *buf, RZ_NONNULL const char *fmt, int n)
 ... More...
 
RZ_API st64 rz_buf_insert_bytes (RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL const ut8 *buf, ut64 length)
 Insert an array of bytes in the buffer. More...
 
RZ_API st64 rz_buf_read (RZ_NONNULL RzBuffer *b, RZ_NONNULL ut8 RZ_OUT *buf, ut64 len)
 Read len bytes of the buffer at the cursor. More...
 
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. More...
 
RZ_API st64 rz_buf_seek (RZ_NONNULL RzBuffer *b, st64 addr, int whence)
 Modify the current cursor position in the buffer. More...
 
RZ_API st64 rz_buf_write (RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, ut64 len)
 Write len bytes of the buffer at the cursor. More...
 
RZ_API st64 rz_buf_write_at (RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL const ut8 *buf, ut64 len)
 Write len bytes of the buffer at the specified address. More...
 
RZ_API ut64 rz_buf_size (RZ_NONNULL RzBuffer *b)
 Return the size of the buffer. More...
 
RZ_API ut64 rz_buf_tell (RZ_NONNULL RzBuffer *b)
 Return the current cursor position. More...
 
RZ_API void rz_buf_free (RzBuffer *b)
 Free all internal data hold by the buffer and the buffer. More...
 
RZ_API void rz_buf_set_overflow_byte (RZ_NONNULL RzBuffer *b, ut8 Oxff)
 Change the overflow byte used in the RZ_BUFFER_SPARSE. More...
 
RZ_DEPRECATE RZ_API RZ_BORROW ut8rz_buf_data (RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut64 *size)
 Return a borrowed array of bytes representing the buffer data. More...
 
RZ_API ut64 rz_buf_fwd_scan (RZ_NONNULL RzBuffer *b, ut64 start, ut64 amount, RZ_NONNULL RzBufferFwdScan fwd_scan, RZ_NULLABLE void *user)
 Scans buffer linearly in chunks calling fwd_scan for each chunk. More...
 
RZ_API st64 rz_buf_uleb128 (RZ_NONNULL RzBuffer *buffer, RZ_NONNULL ut64 *value)
 Decodes ULEB128 from RzBuffer. More...
 
RZ_API st64 rz_buf_sleb128 (RZ_NONNULL RzBuffer *buffer, RZ_NONNULL st64 *value)
 Decodes SLEB128 from RzBuffer. More...
 

Macro Definition Documentation

◆ GET_STRING_BUFFER_SIZE

#define GET_STRING_BUFFER_SIZE   32

Definition at line 27 of file buf.c.

Enumeration Type Documentation

◆ RzBufferType

Enumerator
RZ_BUFFER_FILE 
RZ_BUFFER_IO_FD 
RZ_BUFFER_IO 
RZ_BUFFER_BYTES 
RZ_BUFFER_MMAP 
RZ_BUFFER_SPARSE 
RZ_BUFFER_REF 

Definition at line 9 of file buf.c.

9  {
17 } RzBufferType;
RzBufferType
Definition: buf.c:9
@ RZ_BUFFER_SPARSE
Definition: buf.c:15
@ RZ_BUFFER_MMAP
Definition: buf.c:14
@ RZ_BUFFER_FILE
Definition: buf.c:10
@ RZ_BUFFER_IO_FD
Definition: buf.c:11
@ RZ_BUFFER_IO
Definition: buf.c:12
@ RZ_BUFFER_REF
Definition: buf.c:16
@ RZ_BUFFER_BYTES
Definition: buf.c:13

Function Documentation

◆ buf_fini()

static bool buf_fini ( RzBuffer b)
static

Definition at line 46 of file buf.c.

46  {
47  rz_return_val_if_fail(b && b->methods, false);
48 
49  return b->methods->fini ? b->methods->fini(b) : true;
50 }
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define b(i)
Definition: sha256.c:42

References b, and rz_return_val_if_fail.

Referenced by rz_buf_fini().

◆ buf_format()

static st64 buf_format ( RzBuffer dst,
RzBuffer src,
const char *  fmt,
int  n 
)
static

Definition at line 84 of file buf.c.

84  {
85  st64 res = 0;
86 
87  for (int i = 0; i < n; i++) {
88  int m = 1;
89  int tsize = 2;
90  bool bigendian = true;
91 
92  for (int j = 0; fmt[j]; j++) {
93  switch (fmt[j]) {
94  case '0':
95  case '1':
96  case '2':
97  case '3':
98  case '4':
99  case '5':
100  case '6':
101  case '7':
102  case '8':
103  case '9':
104  if (m == 1) {
105  m = rz_num_get(NULL, &fmt[j]);
106  }
107  continue;
108  case 's':
109  tsize = 2;
110  bigendian = false;
111  break;
112  case 'S':
113  tsize = 2;
114  bigendian = true;
115  break;
116  case 'i':
117  tsize = 4;
118  bigendian = false;
119  break;
120  case 'I':
121  tsize = 4;
122  bigendian = true;
123  break;
124  case 'l':
125  tsize = 8;
126  bigendian = false;
127  break;
128  case 'L':
129  tsize = 8;
130  bigendian = true;
131  break;
132  case 'c':
133  tsize = 1;
134  bigendian = false;
135  break;
136  default: return -1;
137  }
138 
139  for (int k = 0; k < m; k++) {
140  ut8 tmp[sizeof(ut64)];
141  ut8 d1;
142  ut16 d2;
143  ut32 d3;
144  ut64 d4;
145  st64 r = rz_buf_read(src, tmp, tsize);
146  if (r < tsize) {
147  return -1;
148  }
149 
150  switch (tsize) {
151  case 1:
152  d1 = rz_read_ble8(tmp);
153  r = rz_buf_write(dst, (ut8 *)&d1, 1);
154  break;
155  case 2:
156  d2 = rz_read_ble16(tmp, bigendian);
157  r = rz_buf_write(dst, (ut8 *)&d2, 2);
158  break;
159  case 4:
160  d3 = rz_read_ble32(tmp, bigendian);
161  r = rz_buf_write(dst, (ut8 *)&d3, 4);
162  break;
163  case 8:
164  d4 = rz_read_ble64(tmp, bigendian);
165  r = rz_buf_write(dst, (ut8 *)&d4, 8);
166  break;
167  }
168  if (r < 0) {
169  return -1;
170  }
171  res += r;
172  }
173 
174  m = 1;
175  }
176  }
177 
178  return res;
179 }
lzma_index ** i
Definition: index.h:629
lzma_index * src
Definition: index.h:567
RZ_API st64 rz_buf_read(RZ_NONNULL RzBuffer *b, RZ_NONNULL ut8 RZ_OUT *buf, ut64 len)
Read len bytes of the buffer at the cursor.
Definition: buf.c:1111
RZ_API st64 rz_buf_write(RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, ut64 len)
Write len bytes of the buffer at the cursor.
Definition: buf.c:1181
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
uint16_t ut16
uint32_t ut32
const char * k
Definition: dsignal.c:11
d4
Definition: fp-armv8.s.cs:15
uint8_t ut8
Definition: lh5801.h:11
char * dst
Definition: lz4.h:724
int n
Definition: mipsasm.c:19
static ut64 rz_read_ble64(const void *src, bool big_endian)
Definition: rz_endian.h:501
static ut8 rz_read_ble8(const void *src)
Definition: rz_endian.h:12
static ut32 rz_read_ble32(const void *src, bool big_endian)
Definition: rz_endian.h:497
static ut16 rz_read_ble16(const void *src, bool big_endian)
Definition: rz_endian.h:493
RZ_API ut64 rz_num_get(RzNum *num, const char *str)
Definition: unum.c:172
#define st64
Definition: rz_types_base.h:10
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References d1, d2, d3, d4, dst, i, k, regress::m, n, NULL, r, rz_buf_read(), rz_buf_write(), rz_num_get(), rz_read_ble16(), rz_read_ble32(), rz_read_ble64(), rz_read_ble8(), src, st64, autogen_x86imm::tmp, and ut64().

Referenced by rz_buf_fread(), and rz_buf_fwrite().

◆ buf_get_size()

static ut64 buf_get_size ( RzBuffer b)
static

Definition at line 52 of file buf.c.

52  {
53  rz_return_val_if_fail(b && b->methods, UT64_MAX);
54 
55  return b->methods->get_size ? b->methods->get_size(b) : 0;
56 }
#define UT64_MAX
Definition: rz_types_base.h:86

References b, rz_return_val_if_fail, and UT64_MAX.

Referenced by rz_buf_size().

◆ buf_init()

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

Definition at line 40 of file buf.c.

40  {
41  rz_return_val_if_fail(b && b->methods, false);
42 
43  return b->methods->init ? b->methods->init(b, user) : true;
44 }

References b, and rz_return_val_if_fail.

Referenced by rz_buf_new_with_methods().

◆ buf_move_back()

static bool buf_move_back ( RZ_NONNULL RzBuffer b,
ut64  addr,
ut64  length 
)
static

Definition at line 181 of file buf.c.

181  {
183 
184  ut64 size = rz_buf_size(b);
185  if (size < addr) {
186  return false;
187  }
188 
189  ut8 *tmp = RZ_NEWS(ut8, size - addr);
190  if (!tmp) {
191  return false;
192  }
193 
194  bool res = false;
195  st64 tmp_length = rz_buf_read_at(b, addr, tmp, size - addr);
196  if (tmp_length < 0) {
197  goto err;
198  }
199 
200  if (!rz_buf_resize(b, size + length)) {
201  goto err;
202  }
203 
204  if (rz_buf_write_at(b, addr + length, tmp, tmp_length) < 0) {
205  goto err;
206  }
207 
208  res = true;
209 err:
210  free(tmp);
211  return res;
212 }
static bool err
Definition: armass.c:435
RZ_API bool rz_buf_resize(RZ_NONNULL RzBuffer *b, ut64 newsize)
Resize the buffer size.
Definition: buf.c:890
RZ_API st64 rz_buf_write_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL const ut8 *buf, ut64 len)
Write len bytes of the buffer at the specified address.
Definition: buf.c:1197
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
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
#define RZ_NEWS(x, y)
Definition: rz_types.h:283
static int addr
Definition: z80asm.c:58

References addr, b, err, free(), length, rz_buf_read_at(), rz_buf_resize(), rz_buf_size(), rz_buf_write_at(), RZ_NEWS, rz_return_val_if_fail, st64, autogen_x86imm::tmp, and ut64().

Referenced by rz_buf_insert_bytes().

◆ buf_read()

static st64 buf_read ( RzBuffer b,
ut8 buf,
size_t  len 
)
static

Definition at line 58 of file buf.c.

58  {
59  rz_return_val_if_fail(b && b->methods, -1);
60 
61  return b->methods->read ? b->methods->read(b, buf, len) : -1;
62 }
size_t len
Definition: 6502dis.c:15
voidpf void * buf
Definition: ioapi.h:138

References b, len, and rz_return_val_if_fail.

Referenced by rz_buf_read().

◆ buf_resize()

static bool buf_resize ( RzBuffer b,
ut64  newsize 
)
static

Definition at line 78 of file buf.c.

78  {
79  rz_return_val_if_fail(b && b->methods, -1);
80 
81  return b->methods->resize ? b->methods->resize(b, newsize) : false;
82 }

References b, and rz_return_val_if_fail.

Referenced by rz_buf_resize().

◆ buf_seek()

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

Definition at line 72 of file buf.c.

72  {
73  rz_return_val_if_fail(b && b->methods, -1);
74 
75  return b->methods->seek ? b->methods->seek(b, addr, whence) : -1;
76 }

References addr, b, and rz_return_val_if_fail.

Referenced by rz_buf_seek().

◆ buf_whole_buf_free()

static void buf_whole_buf_free ( RzBuffer b)
static

Definition at line 29 of file buf.c.

29  {
30  // free the whole_buf only if it was initially allocated by the buf types
31  if (b->methods->get_whole_buf) {
32  if (b->methods->free_whole_buf) {
33  b->methods->free_whole_buf(b);
34  }
35  } else {
36  RZ_FREE(b->whole_buf);
37  }
38 }
#define RZ_FREE(x)
Definition: rz_types.h:369

References b, and RZ_FREE.

Referenced by buf_write(), get_whole_buf(), and rz_buf_fini().

◆ buf_write()

static st64 buf_write ( RzBuffer b,
const ut8 buf,
size_t  len 
)
static

Definition at line 64 of file buf.c.

64  {
65  rz_return_val_if_fail(b && b->methods, -1);
66 
68 
69  return b->methods->write ? b->methods->write(b, buf, len) : -1;
70 }
static void buf_whole_buf_free(RzBuffer *b)
Definition: buf.c:29

References b, buf_whole_buf_free(), len, and rz_return_val_if_fail.

Referenced by rz_buf_write().

◆ get_whole_buf()

static ut8* get_whole_buf ( RzBuffer b,
ut64 size 
)
static

Definition at line 214 of file buf.c.

214  {
215  rz_return_val_if_fail(b && size && b->methods, NULL);
216 
218 
219  if (b->methods->get_whole_buf) {
220  return b->methods->get_whole_buf(b, size);
221  }
222 
224  // bsz = 4096; // FAKE MINIMUM SIZE TO READ THE BIN HEADER
225  if (buf_size == UT64_MAX) {
226  return NULL;
227  }
228 
229  b->whole_buf = RZ_NEWS(ut8, buf_size);
230  if (!b->whole_buf) {
231  return NULL;
232  }
233 
234  if (rz_buf_read_at(b, 0, b->whole_buf, buf_size) < 0) {
235  RZ_FREE(b->whole_buf);
236  return NULL;
237  }
238 
239  *size = buf_size;
240 
241  return b->whole_buf;
242 }
static int buf_size
Definition: debug_qnx.c:35

References b, buf_size, buf_whole_buf_free(), NULL, rz_buf_read_at(), rz_buf_size(), RZ_FREE, RZ_NEWS, rz_return_val_if_fail, ut64(), and UT64_MAX.

Referenced by rz_buf_append_buf(), rz_buf_data(), rz_buf_dump(), and rz_buf_new_with_buf().

◆ new_buffer()

static RzBuffer* new_buffer ( RzBufferType  type,
void *  user 
)
static

Definition at line 244 of file buf.c.

244  {
245  const RzBufferMethods *methods = NULL;
246 
247  switch (type) {
248  case RZ_BUFFER_BYTES:
249  methods = &buffer_bytes_methods;
250  break;
251  case RZ_BUFFER_MMAP:
252  methods = &buffer_mmap_methods;
253  break;
254  case RZ_BUFFER_SPARSE:
255  methods = &buffer_sparse_methods;
256  break;
257  case RZ_BUFFER_FILE:
258  methods = &buffer_file_methods;
259  break;
260  case RZ_BUFFER_IO_FD:
261  methods = &buffer_io_fd_methods;
262  break;
263  case RZ_BUFFER_IO:
264  methods = &buffer_io_methods;
265  break;
266  case RZ_BUFFER_REF:
267  methods = &buffer_ref_methods;
268  break;
269  default:
271  return NULL;
272  }
273 
274  return rz_buf_new_with_methods(methods, user);
275 }
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_methods(RZ_NONNULL const RzBufferMethods *methods, void *init_user)
Creates a new buffer with a specific back end.
Definition: buf.c:525
static const RzBufferMethods buffer_bytes_methods
Definition: buf_bytes.c:118
static const RzBufferMethods buffer_file_methods
Definition: buf_file.c:81
static const RzBufferMethods buffer_io_methods
Definition: buf_io.c:54
static const RzBufferMethods buffer_io_fd_methods
Definition: buf_io_fd.c:88
static const RzBufferMethods buffer_mmap_methods
Definition: buf_mmap.c:73
static const RzBufferMethods buffer_ref_methods
Definition: buf_ref.c:96
static const RzBufferMethods buffer_sparse_methods
Definition: buf_sparse.c:302
int type
Definition: mipsasm.c:17
#define rz_warn_if_reached()
Definition: rz_assert.h:29

References buffer_bytes_methods, buffer_file_methods, buffer_io_fd_methods, buffer_io_methods, buffer_mmap_methods, buffer_ref_methods, buffer_sparse_methods, NULL, rz_buf_new_with_methods(), RZ_BUFFER_BYTES, RZ_BUFFER_FILE, RZ_BUFFER_IO, RZ_BUFFER_IO_FD, RZ_BUFFER_MMAP, RZ_BUFFER_REF, RZ_BUFFER_SPARSE, rz_warn_if_reached, and type.

Referenced by palloc(), rz_buf_new_empty(), rz_buf_new_file(), rz_buf_new_mmap(), rz_buf_new_slice(), rz_buf_new_slurp(), rz_buf_new_sparse(), rz_buf_new_sparse_overlay(), rz_buf_new_with_bytes(), rz_buf_new_with_io(), rz_buf_new_with_io_fd(), and rz_buf_new_with_pointers().

◆ rz_buf_append_buf()

RZ_API bool rz_buf_append_buf ( RZ_NONNULL RzBuffer b,
RZ_NONNULL RzBuffer a 
)

Append the content of the buffer a to the buffer b.

Parameters
dstthe destination buffer with write permission
srcthe source buffer
Returns
Return the status of the operation.

The function append the content of the buffer a, the two buffer don't need to have the same back end. WARNING: This function can allocate a lot of memory.

Definition at line 685 of file buf.c.

685  {
686  rz_return_val_if_fail(b && a && !b->readonly, false);
687 
688  ut64 size = 0;
689  const ut8 *tmp = get_whole_buf(a, &size);
690 
691  return rz_buf_append_bytes(b, tmp, size);
692 }
RZ_API bool rz_buf_append_bytes(RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, ut64 length)
Append an array of bytes to the buffer.
Definition: buf.c:732
static ut8 * get_whole_buf(RzBuffer *b, ut64 *size)
Definition: buf.c:214
#define a(i)
Definition: sha256.c:41

References a, b, get_whole_buf(), rz_buf_append_bytes(), rz_return_val_if_fail, autogen_x86imm::tmp, and ut64().

Referenced by build(), and xnu_generate_corefile().

◆ rz_buf_append_buf_slice()

RZ_API bool rz_buf_append_buf_slice ( RZ_NONNULL RzBuffer b,
RZ_NONNULL RzBuffer a,
ut64  offset,
ut64  size 
)

Append a slice of the buffer a to the buffer b.

Parameters
dstthe destination buffer with write permission
srcthe source buffer
offsetThe starting offset in the buffer a.
sizeThe number of bytes that will be extract from the buffer a.
Returns
Return the status of the operation.

The function append a slice of the buffer a, the two buffer don't need to have the same back end. WARNING: This function can allocate a lot of memory.

Definition at line 705 of file buf.c.

705  {
706  rz_return_val_if_fail(b && a && !b->readonly, false);
707 
708  ut8 *tmp = RZ_NEWS(ut8, size);
709  if (!tmp) {
710  return false;
711  }
712 
714  if (r < 0) {
715  free(tmp);
716  return false;
717  }
718 
719  bool result = rz_buf_append_bytes(b, tmp, r);
720 
721  free(tmp);
722  return result;
723 }
voidpf uLong offset
Definition: ioapi.h:144

References a, b, free(), r, rz_buf_append_bytes(), rz_buf_read_at(), RZ_NEWS, rz_return_val_if_fail, st64, and autogen_x86imm::tmp.

◆ rz_buf_append_bytes()

RZ_API bool rz_buf_append_bytes ( RZ_NONNULL RzBuffer b,
RZ_NONNULL const ut8 buf,
ut64  length 
)

Append an array of bytes to the buffer.

Parameters
dstthe destination buffer with write permission
srcthe source buffer
length...
Returns
Return the status of the operation.

Definition at line 732 of file buf.c.

732  {
733  rz_return_val_if_fail(b && buf && !b->readonly, false);
734 
735  if (rz_buf_seek(b, 0, RZ_BUF_END) < 0) {
736  return false;
737  }
738 
739  return rz_buf_write(b, buf, length) == length;
740 }
RZ_API st64 rz_buf_seek(RZ_NONNULL RzBuffer *b, st64 addr, int whence)
Modify the current cursor position in the buffer.
Definition: buf.c:1166
#define RZ_BUF_END
Definition: rz_buf.h:16

References b, length, RZ_BUF_END, rz_buf_seek(), rz_buf_write(), and rz_return_val_if_fail.

Referenced by build(), flirt_write_module(), flirt_write_node(), gprobe_debugoff(), gprobe_debugon(), gprobe_frame_sp(), gprobe_get_reply_sp(), gprobe_getdeviceid(), gprobe_getinformation(), gprobe_read(), gprobe_reset(), gprobe_runcode(), gprobe_write(), rz_buf_append_buf(), rz_buf_append_buf_slice(), rz_buf_append_string(), rz_buf_append_ut16(), rz_buf_append_ut32(), rz_buf_append_ut64(), rz_buf_set_bytes(), rz_egg_append(), rz_egg_append_bytes(), rz_egg_assemble_asm(), rz_egg_include(), rz_egg_load(), rz_egg_printf(), rz_egg_raw(), rz_sign_flirt_write_compressed_pattern_to_buffer(), rz_write_vle16(), rz_write_vle32(), socket_slurp(), xnu_generate_corefile(), and xnu_write_mem_maps_to_buffer().

◆ rz_buf_append_nbytes()

RZ_API bool rz_buf_append_nbytes ( RZ_NONNULL RzBuffer b,
ut64  length 
)

Extend the size of the buffer.

Parameters
bA buffer with write permission.
lengthThe number of bytes to add at the end of the buffer.
Returns
Return the status of the operation.

Definition at line 748 of file buf.c.

748  {
749  rz_return_val_if_fail(b && !b->readonly, false);
750 
751  return rz_buf_resize(b, rz_buf_size(b) + length);
752 }

References b, length, rz_buf_resize(), rz_buf_size(), and rz_return_val_if_fail.

◆ rz_buf_append_string()

RZ_API st64 rz_buf_append_string ( RZ_NONNULL RzBuffer b,
RZ_NONNULL const char *  str 
)

Append a string to the buffer.

Parameters
bA buffer with write permission.
str...
Returns
Return the status of the operation.

...

Definition at line 962 of file buf.c.

962  {
963  rz_return_val_if_fail(b && str && !b->readonly, false);
964 
965  return rz_buf_append_bytes(b, (const ut8 *)str, strlen(str));
966 }

References b, rz_buf_append_bytes(), rz_return_val_if_fail, and cmd_descs_generate::str.

Referenced by add_spaces(), fill_level(), flirt_pat_write_line(), flirt_write_module(), rz_analysis_reflines_str(), rz_sign_flirt_write_compressed_pattern_to_buffer(), and rz_sign_flirt_write_string_pattern_to_buffer().

◆ rz_buf_append_ut16()

RZ_API bool rz_buf_append_ut16 ( RZ_NONNULL RzBuffer b,
ut16  n 
)

Add a ut16 number at the end of the buffer.

Parameters
bA buffer with write permission.
n
Returns
Return the status of the operation.

...

Definition at line 762 of file buf.c.

762  {
763  rz_return_val_if_fail(b && !b->readonly, false);
764 
765  return rz_buf_append_bytes(b, (const ut8 *)&n, sizeof(n));
766 }

References b, n, rz_buf_append_bytes(), and rz_return_val_if_fail.

◆ rz_buf_append_ut32()

RZ_API bool rz_buf_append_ut32 ( RZ_NONNULL RzBuffer b,
ut32  n 
)

Add a ut32 number at the end of the buffer.

Parameters
bA buffer with write permission.
n
Returns
Return the status of the operation.

...

Definition at line 776 of file buf.c.

776  {
777  rz_return_val_if_fail(b && !b->readonly, false);
778 
779  return rz_buf_append_bytes(b, (const ut8 *)&n, sizeof(n));
780 }

References b, n, rz_buf_append_bytes(), and rz_return_val_if_fail.

◆ rz_buf_append_ut64()

RZ_API bool rz_buf_append_ut64 ( RZ_NONNULL RzBuffer b,
ut64  n 
)

Add a ut64 number at the end of the buffer.

Parameters
bA buffer with write permission.
n
Returns
Return the status of the operation.

...

Definition at line 790 of file buf.c.

790  {
791  rz_return_val_if_fail(b && !b->readonly, false);
792 
793  return rz_buf_append_bytes(b, (const ut8 *)&n, sizeof(n));
794 }

References b, n, rz_buf_append_bytes(), and rz_return_val_if_fail.

◆ rz_buf_data()

Return a borrowed array of bytes representing the buffer data.

Parameters
bBuffer to get the data from.
sizeSize of the returned data.

WARNING: this function should be used with care because it may allocate the entire buffer in memory. Consider using the rz_buf_read* APIs or rz_buf_fwd_scan API instead and read only the chunks you need.

Definition at line 1287 of file buf.c.

1287  {
1289 
1290  return get_whole_buf(b, size);
1291 }

References b, get_whole_buf(), NULL, and rz_return_val_if_fail.

Referenced by buf_get_whole_buf(), check_buffer(), create(), gprobe_frame_sp(), gprobe_getinformation(), gprobe_send_request_sp(), io_default_get_buf(), load_buffer(), rz_bin_file_xtr_load_buffer(), rz_cmd_debug(), rz_egg_finalize(), rz_egg_run(), rz_egg_run_rop(), rz_egg_shellcode(), rz_io_open_buffer(), rz_io_zip_flush_file(), rz_main_rz_bin(), and rz_main_rz_gg().

◆ rz_buf_dump()

RZ_API bool rz_buf_dump ( RZ_NONNULL RzBuffer b,
RZ_NONNULL const char *  file 
)

Dump the content of the buffer to a file.

Parameters
bA buffer with write permission.
fileThe output file where the buffer content while be outputted.
Returns
Return the status of the operation.
See also
rz_file_dump()

...

Definition at line 806 of file buf.c.

806  {
807  rz_return_val_if_fail(b && file, false);
808 
809  ut64 size = 0;
810  const ut8 *tmp = get_whole_buf(b, &size);
811 
812  return rz_file_dump(file, tmp, size, 0);
813 }
RZ_API bool rz_file_dump(const char *file, const ut8 *buf, int len, bool append)
Definition: file.c:838
Definition: gzappend.c:170

References b, get_whole_buf(), rz_file_dump(), rz_return_val_if_fail, autogen_x86imm::tmp, and ut64().

◆ rz_buf_fini()

RZ_API bool rz_buf_fini ( RzBuffer b)

Free all internal data hold by the buffer.

Parameters
bA buffer with write permission.
Returns
Return the status of the operation.

...

Definition at line 822 of file buf.c.

822  {
823  if (!b) {
824  return false;
825  }
826 
827  if (b->refctr > 0) {
828  b->refctr--;
829  return false;
830  }
831 
833 
834  return buf_fini(b);
835 }
static bool buf_fini(RzBuffer *b)
Definition: buf.c:46

References b, buf_fini(), and buf_whole_buf_free().

Referenced by rz_asm_op_fini(), and rz_buf_free().

◆ rz_buf_fread()

RZ_API st64 rz_buf_fread ( RZ_NONNULL RzBuffer b,
RZ_NONNULL ut8 buf,
RZ_NONNULL const char *  fmt,
int  n 
)

...

Parameters
b...
buf...
fmt...
n...
Returns
...

...

Definition at line 978 of file buf.c.

978  {
979  rz_return_val_if_fail(b && buf && fmt, -1);
980 
981  // XXX: we assume the caller knows what he's doing
983  st64 res = buf_format(dst, b, fmt, n);
984 
985  rz_buf_free(dst);
986 
987  return res;
988 }
static st64 buf_format(RzBuffer *dst, RzBuffer *src, const char *fmt, int n)
Definition: buf.c:84
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_pointers(const ut8 *bytes, ut64 len, bool steal)
Creates a new buffer with a bytes array.
Definition: buf.c:552
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253

References b, buf_format(), dst, n, rz_buf_free(), rz_buf_new_with_pointers(), rz_return_val_if_fail, st64, and UT64_MAX.

Referenced by rz_bin_coff_init_hdr(), and rz_buf_fread_at().

◆ rz_buf_fread_at()

RZ_API st64 rz_buf_fread_at ( RZ_NONNULL RzBuffer b,
ut64  addr,
RZ_NONNULL ut8 buf,
RZ_NONNULL const char *  fmt,
int  n 
)

...

Parameters
b...
addr...
buf...
fmt...
n...
Returns
...

...

Definition at line 1001 of file buf.c.

1001  {
1002  rz_return_val_if_fail(b && buf && fmt, -1);
1003 
1004  st64 tmp = rz_buf_tell(b);
1005  if (tmp < 0) {
1006  return -1;
1007  }
1008 
1009  if (rz_buf_seek(b, addr, RZ_BUF_SET) < 0) {
1010  return -1;
1011  }
1012 
1013  st64 result = rz_buf_fread(b, buf, fmt, n);
1014 
1015  if (rz_buf_seek(b, tmp, RZ_BUF_SET) < 0) {
1016  return -1;
1017  }
1018 
1019  return result;
1020 }
RZ_API ut64 rz_buf_tell(RZ_NONNULL RzBuffer *b)
Return the current cursor position.
Definition: buf.c:1238
RZ_API st64 rz_buf_fread(RZ_NONNULL RzBuffer *b, RZ_NONNULL ut8 *buf, RZ_NONNULL const char *fmt, int n)
...
Definition: buf.c:978
#define RZ_BUF_SET
Definition: rz_buf.h:14

References addr, b, n, rz_buf_fread(), rz_buf_seek(), RZ_BUF_SET, rz_buf_tell(), rz_return_val_if_fail, st64, and autogen_x86imm::tmp.

Referenced by art_header_load(), bin_pe_dotnet_init_metadata(), carve_deps_at_address(), check_buffer(), create_cache_bins(), entries(), estimate_slide(), get_rebase_info(), get_rebase_infos(), header(), init_items(), lmf_header_load(), load_buffer(), parse_chained_fixups(), parse_thread(), populate_cache_maps(), read_cache_accel(), read_cache_header(), read_cache_images(), read_cache_imgextra(), rz_axml_decode(), rz_bin_coff_init_hdr(), rz_bin_coff_init_opt_hdr(), rz_bin_coff_init_scn_hdr(), rz_bin_coff_init_symtable(), rz_coresym_cache_element_header_new(), rz_dyld_locsym_new(), rz_dyldcache_symbols_from_locsym(), sections(), and sep64_xtr_ctx_new().

◆ rz_buf_free()

RZ_API void rz_buf_free ( RzBuffer b)

Free all internal data hold by the buffer and the buffer.

Parameters
b...
Returns
Return the status of the operation.
See also
rz_buf_fini()

...

Definition at line 1253 of file buf.c.

1253  {
1254  if (!b) {
1255  return;
1256  }
1257  if (rz_buf_fini(b)) {
1258  free(b);
1259  }
1260 }
RZ_API bool rz_buf_fini(RzBuffer *b)
Free all internal data hold by the buffer.
Definition: buf.c:822

References b, free(), and rz_buf_fini().

Referenced by __close(), __open(), ar_close(), ar_open_all(), ar_open_file(), basefind_create_array_of_addresses(), bin_pe_dotnet_destroy_clr(), buf_ref_fini(), buf_sparse_fini(), build(), classes(), cmd_search_bin(), create(), destroy(), egg_patch_free(), extract(), get_gnu_debugdata_elf_symbols(), get_virtual_files(), gprobe_debugoff(), gprobe_debugon(), gprobe_getdeviceid(), gprobe_getinformation(), gprobe_read(), gprobe_reset(), gprobe_runcode(), gprobe_write(), handle_tmp_desc(), mach0_free(), msf_stream_directory_free(), msf_stream_free(), nso_free(), oneshot_buffer(), parse_classes(), rabin_do_operation(), rz_analysis_il_vm_free(), rz_analysis_il_vm_new(), rz_analysis_reflines_str(), rz_asm_massemble(), rz_asm_pseudo_incbin(), rz_axml_decode(), rz_bflt_free(), rz_bin_coff_free(), rz_bin_dex_free(), rz_bin_dmp64_free(), rz_bin_elf_free(), rz_bin_fatmach0_free(), rz_bin_file_compute_hashes(), rz_bin_file_free(), rz_bin_file_set_bytes(), rz_bin_mdmp_free(), rz_bin_mdmp_init_pe_bins(), rz_bin_mz_free(), rz_bin_open_io(), rz_bin_pdb_free(), rz_bin_pdb_parse_from_file(), rz_bin_pe_free(), rz_bin_pemixed_free(), rz_bin_pemixed_init_native(), rz_bin_te_free(), rz_bin_virtual_file_free(), rz_bin_wasm_destroy(), rz_bin_xtrdata_free(), rz_bin_zimg_new_buf(), rz_buf_fread(), rz_buf_fwrite(), rz_cf_value_data_free(), rz_cmd_debug(), rz_core_bin_whole_strings(), rz_core_fini(), rz_core_flirt_convert_file(), rz_core_flirt_create_file(), rz_core_flirt_dump_file(), rz_debug_dmp_init(), rz_dyldcache_free(), rz_egg_encode(), rz_egg_finalize(), rz_egg_free(), rz_egg_reset(), rz_file_deflate(), rz_file_inflate(), rz_il_mem_free(), rz_io_def_mmap_free(), rz_io_zip_free_zipfileobj(), rz_kernel_cache_free(), rz_main_rz_bin(), rz_sign_flirt_apply(), rz_sign_flirt_parse_compressed_pattern_from_buffer(), rz_sign_flirt_write_compressed_pattern_to_buffer(), sep64_xtr_ctx_get_slice(), sigdb_signature_resolve_details(), socket_http_answer(), virtual_files(), and xnu_generate_corefile().

◆ rz_buf_fwd_scan()

RZ_API ut64 rz_buf_fwd_scan ( RZ_NONNULL RzBuffer b,
ut64  start,
ut64  amount,
RZ_NONNULL RzBufferFwdScan  fwd_scan,
RZ_NULLABLE void *  user 
)

Scans buffer linearly in chunks calling fwd_scan for each chunk.

Parameters
bRzBuffer to read
startStart address
amountAmount of bytes to read
fwd_scanFunction to call for each chunk
userUser data to pass to fwd_scan
Returns
Number of bytes read

Definition at line 1303 of file buf.c.

1303  {
1304  rz_return_val_if_fail(b && fwd_scan, 0);
1305  if (!amount) {
1306  return 0;
1307  }
1308  if (b->methods->get_whole_buf) {
1309  ut64 sz;
1310  const ut8 *buf = b->methods->get_whole_buf(b, &sz);
1311  if (buf) {
1312  if (sz <= start) {
1313  return 0;
1314  }
1315  return fwd_scan(buf + start, RZ_MIN(sz - start, amount), user);
1316  }
1317  }
1318  const ut64 size = rz_buf_size(b);
1319  if (size <= start) {
1320  return 0;
1321  }
1322  if (b->whole_buf) {
1323  return fwd_scan(b->whole_buf + start, RZ_MIN(size - start, amount), user);
1324  }
1325  ut64 addr = start;
1326  const ut64 user_end = UT64_ADD_OVFCHK(start, amount) ? UT64_MAX : start + amount;
1327  const ut64 end = RZ_MIN(user_end, size);
1328  const size_t buf_size = RZ_MIN(end - start, 0x1000);
1329  ut8 *buf = malloc(buf_size);
1330  if (!buf) {
1331  return 0;
1332  }
1333  while (addr < end) {
1334  const ut64 read_amount = RZ_MIN(buf_size, end - addr);
1335  rz_buf_read_at(b, addr, buf, read_amount);
1336  ut64 read = fwd_scan(buf, read_amount, user);
1337  if (!read) {
1338  break;
1339  }
1340  addr += read;
1341  }
1342  free(buf);
1343  return addr - start;
1344 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
void * malloc(size_t size)
Definition: malloc.c:123
#define RZ_MIN(x, y)
#define UT64_ADD_OVFCHK(x, y)
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References addr, b, buf_size, test_evm::end, free(), malloc(), read(), rz_buf_read_at(), rz_buf_size(), RZ_MIN, rz_return_val_if_fail, start, ut64(), UT64_ADD_OVFCHK, and UT64_MAX.

Referenced by bin_pe_compute_authentihash(), bin_pe_get_actual_checksum(), find_go_build_info(), and rz_bin_file_compute_hashes().

◆ rz_buf_fwrite()

RZ_API st64 rz_buf_fwrite ( RZ_NONNULL RzBuffer b,
RZ_NONNULL const ut8 buf,
RZ_NONNULL const char *  fmt,
int  n 
)

...

Parameters
b...
addr...
buf...
fmt...
n...
Returns
...

...

Definition at line 1033 of file buf.c.

1033  {
1034  rz_return_val_if_fail(b && buf && fmt && !b->readonly, -1);
1035 
1036  // XXX: we assume the caller knows what he's doing
1038  st64 result = buf_format(b, src, fmt, n);
1039 
1040  rz_buf_free(src);
1041 
1042  return result;
1043 }

References b, buf_format(), n, rz_buf_free(), rz_buf_new_with_pointers(), rz_return_val_if_fail, src, st64, and UT64_MAX.

Referenced by rz_buf_fwrite_at().

◆ rz_buf_fwrite_at()

RZ_API st64 rz_buf_fwrite_at ( RZ_NONNULL RzBuffer b,
ut64  addr,
RZ_NONNULL const ut8 buf,
RZ_NONNULL const char *  fmt,
int  n 
)

...

Parameters
b...
addr...
buf...
fmt...
n...
Returns
...

...

Definition at line 1056 of file buf.c.

1056  {
1057  rz_return_val_if_fail(b && buf && fmt && !b->readonly, -1);
1058 
1059  st64 tmp = rz_buf_tell(b);
1060  if (tmp < 0) {
1061  return -1;
1062  }
1063 
1064  if (rz_buf_seek(b, addr, RZ_BUF_SET) < 0) {
1065  return -1;
1066  }
1067 
1068  st64 result = rz_buf_fwrite(b, buf, fmt, n);
1069 
1070  if (rz_buf_seek(b, tmp, RZ_BUF_SET) < 0) {
1071  return -1;
1072  }
1073 
1074  return result;
1075 }
RZ_API st64 rz_buf_fwrite(RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, RZ_NONNULL const char *fmt, int n)
...
Definition: buf.c:1033

References addr, b, n, rz_buf_fwrite(), rz_buf_seek(), RZ_BUF_SET, rz_buf_tell(), rz_return_val_if_fail, st64, and autogen_x86imm::tmp.

◆ rz_buf_get_nstring()

RZ_API RZ_OWN char* rz_buf_get_nstring ( RZ_NONNULL RzBuffer b,
ut64  addr,
size_t  size 
)

Get a string with a max length from the buffer.

Parameters
bRzBuffer pointer
addrThe address of the string
sizeThe max length authorized
Returns
A string with a length <= size or NULL

Return an heap-allocated string read from the RzBuffer b at address addr. The length depends on the first '\0' found and the arguments size in the buffer. If there is no '\0' in the buffer, there is no string, thus NULL is returned.

Definition at line 585 of file buf.c.

585  {
587 
589 
590  while (true) {
591  char tmp[GET_STRING_BUFFER_SIZE + 1];
592  st64 r = rz_buf_read_at(b, addr, (ut8 *)tmp, sizeof(tmp) - 1);
593  if (r < 1) {
595  return NULL;
596  }
597 
598  size_t count = rz_str_nlen(tmp, r);
600 
601  if (count > size) {
603  return NULL;
604  }
605 
606  if (count != r) {
607  break;
608  }
609 
610  addr += r;
611  size -= count;
612  }
613 
614  char *result = rz_strbuf_drain(buf);
615  return result;
616 }
#define GET_STRING_BUFFER_SIZE
Definition: buf.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
RZ_API size_t rz_str_nlen(const char *s, size_t n)
Definition: str.c:1949
RZ_API RZ_OWN char * rz_strbuf_drain(RzStrBuf *sb)
Definition: strbuf.c:342
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API void rz_strbuf_free(RzStrBuf *sb)
Definition: strbuf.c:358
RZ_API bool rz_strbuf_append_n(RzStrBuf *sb, const char *s, size_t l)
Definition: strbuf.c:229

References addr, b, count, GET_STRING_BUFFER_SIZE, NULL, r, rz_buf_read_at(), rz_return_val_if_fail, rz_str_nlen(), rz_strbuf_append_n(), rz_strbuf_drain(), rz_strbuf_free(), rz_strbuf_new(), st64, and autogen_x86imm::tmp.

Referenced by parse_note_file(), and rz_buf_get_string().

◆ rz_buf_get_string()

RZ_API RZ_OWN char* rz_buf_get_string ( RZ_NONNULL RzBuffer b,
ut64  addr 
)

Get a string from the buffer.

Parameters
bRzBuffer pointer
addrThe address of the string
Returns
A string with a length <= size or NULL

Return an heap-allocated string read from the RzBuffer b at address addr. The length depends on the first '\0' found in the buffer. If there is no '\0' in the buffer, there is no string, thus NULL is returned.

Definition at line 628 of file buf.c.

628  {
630 
632 }
RZ_API RZ_OWN char * rz_buf_get_nstring(RZ_NONNULL RzBuffer *b, ut64 addr, size_t size)
Get a string with a max length from the buffer.
Definition: buf.c:585

References addr, b, NULL, rz_buf_get_nstring(), rz_buf_size(), and rz_return_val_if_fail.

Referenced by mach_headerfields(), parse_dbi_stream_ex_header(), parse_gdata_global(), rz_bin_pe_get_clr_symbols(), and rz_dyldcache_symbols_from_locsym().

◆ rz_buf_insert_bytes()

RZ_API st64 rz_buf_insert_bytes ( RZ_NONNULL RzBuffer b,
ut64  addr,
RZ_NONNULL const ut8 buf,
ut64  length 
)

Insert an array of bytes in the buffer.

Parameters
bA buffer with write permission.
addr...
buf...
length...
Returns
Return the number of bytes written.

...

Definition at line 1087 of file buf.c.

1087  {
1088  rz_return_val_if_fail(b && !b->readonly, -1);
1089 
1090  if (!buf_move_back(b, addr, length)) {
1091  return -1;
1092  }
1093 
1094  st64 result = rz_buf_write_at(b, addr, buf, length);
1095  if (result < 0) {
1096  return -1;
1097  }
1098 
1099  return result;
1100 }
static bool buf_move_back(RZ_NONNULL RzBuffer *b, ut64 addr, ut64 length)
Definition: buf.c:181

References addr, b, buf_move_back(), length, rz_buf_write_at(), rz_return_val_if_fail, and st64.

Referenced by rz_buf_prepend_bytes().

◆ rz_buf_new_empty()

RZ_API RZ_OWN RzBuffer* rz_buf_new_empty ( ut64  len)

Creates a new empty buffer with a predefined size;.

Parameters
lenThe length in byte of the new buffer.
Returns
Return the new allocated buffer.

The function creates a new buffer of the specified length in memory, filled with \0.

Definition at line 285 of file buf.c.

285  {
286  ut8 *buf = RZ_NEWS0(ut8, len);
287  if (!buf) {
288  return NULL;
289  }
290 
291  struct buf_bytes_user u = { 0 };
292  u.data_steal = buf;
293  u.length = len;
294  u.steal = true;
295 
296  RzBuffer *res = new_buffer(RZ_BUFFER_BYTES, &u);
297  if (!res) {
298  free(buf);
299  }
300 
301  return res;
302 }
static RzBuffer * new_buffer(RzBufferType type, void *user)
Definition: buf.c:244
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
const ut8 * data_steal
Definition: buf_bytes.c:8

References buf_bytes_user::data_steal, free(), len, buf_bytes_user::length, new_buffer(), NULL, RZ_BUFFER_BYTES, RZ_NEWS0, and buf_bytes_user::steal.

Referenced by get_gnu_debugdata_elf_symbols(), get_virtual_files(), rz_sign_flirt_write_compressed_pattern_to_buffer(), and virtual_files().

◆ rz_buf_new_file()

RZ_API RZ_OWN RzBuffer* rz_buf_new_file ( const char *  file,
int  perm,
int  mode 
)

Creates a new buffer from a file.

Parameters
fileThe filename used to create the new buffer.
permSame meaning than the symbolic constants defined in sys/stat.h.
modeSame meaning than the symbolic constants use with open (fcntl.h).
Returns
Return the new allocated buffer.
See also
rz_sys_open()

The function creates a new buffer synchronized with the file content, opening it with rz_sys_open and using regular read/write operations to change its content.

Definition at line 317 of file buf.c.

317  {
318  struct buf_file_user u = { 0 };
319 
320  u.file = file;
321  u.perm = perm;
322  u.mode = mode;
323 
324  return new_buffer(RZ_BUFFER_FILE, &u);
325 }
const char int mode
Definition: ioapi.h:137
const char * file
Definition: buf_file.c:8
static int file
Definition: z80asm.c:58

References file, buf_file_user::file, buf_file_user::mode, new_buffer(), buf_file_user::perm, and RZ_BUFFER_FILE.

Referenced by ar_open_all(), ar_open_file(), rz_bin_open_io(), rz_cmd_debug(), rz_core_flirt_convert_file(), rz_core_flirt_create_file(), rz_file_deflate(), rz_file_inflate(), rz_io_def_mmap_create_new_file(), and sigdb_signature_resolve_details().

◆ rz_buf_new_mmap()

RZ_API RZ_OWN RzBuffer* rz_buf_new_mmap ( const char *  filename,
int  perm,
int  mode 
)

Creates a new buffer from a file using rz_file_mmap.

Parameters
fileThe filename used to create the new buffer.
permSame meaning than the symbolic constants defined in sys/stat.h.
modeSame meaning than the symbolic constants use with open (fcntl.h).
Returns
Return the new allocated buffer.
See also
rz_file_mmap()

The function creates a new buffer synchronized with the file content, using mmap to access the file.

Definition at line 339 of file buf.c.

339  {
341 
342  struct buf_mmap_user u = { 0 };
343  u.filename = filename;
344  u.perm = perm;
345  u.mode = mode;
346 
347  return new_buffer(RZ_BUFFER_MMAP, &u);
348 }
const char * filename
Definition: ioapi.h:137
const char * filename
Definition: buf_mmap.c:7

References buf_mmap_user::filename, buf_mmap_user::mode, new_buffer(), NULL, buf_mmap_user::perm, RZ_BUFFER_MMAP, and rz_return_val_if_fail.

Referenced by rz_io_def_mmap_create_new_file().

◆ rz_buf_new_slice()

RZ_API RZ_OWN RzBuffer* rz_buf_new_slice ( RzBuffer b,
ut64  offset,
ut64  size 
)

Creates a new buffer from a slice of another buffer.

Parameters
bThe source buffer used to create the sub buffer.
offsetThe starting offset in the source buffer.
sizeThe number of bytes that will be extract from the source buffer.
Returns
Return the new allocated buffer.
See also
rz_buf_ref()

The function creates a new buffer which shows just a slice of another one, passed as argument. The allocated buffer will reference the source buffer. So both buffer are synchronized.

Definition at line 364 of file buf.c.

364  {
365  struct buf_ref_user u = { 0 };
366 
367  u.parent = b;
368  u.offset = offset;
369  u.size = size;
370 
371  return new_buffer(RZ_BUFFER_REF, &u);
372 }
RzBuffer * parent
Definition: buf_ref.c:7
ut64 offset
Definition: buf_ref.c:8
ut64 size
Definition: buf_ref.c:9

References b, new_buffer(), buf_ref_user::offset, buf_ref_user::parent, RZ_BUFFER_REF, and buf_ref_user::size.

Referenced by bin_pe_dotnet_init_metadata(), cmd_search_bin(), get_gnu_debugdata_elf_symbols(), rz_bin_fatmach0_extract(), rz_bin_open_io(), and sep64_xtr_ctx_get_slice().

◆ rz_buf_new_slurp()

RZ_API RZ_OWN RzBuffer* rz_buf_new_slurp ( const char *  file)

Creates a new buffer from a file.

Parameters
fileThe filename used to create the new buffer.
Returns
Return the new allocated buffer.

The function creates a new buffer in memory, initializing it with the whole content of the specified file.

Definition at line 384 of file buf.c.

384  {
385  size_t len;
386  char *tmp = rz_file_slurp(file, &len);
387  if (!tmp) {
388  return NULL;
389  }
390 
391  struct buf_bytes_user u = { 0 };
392 
393  u.data_steal = (ut8 *)tmp;
394  u.length = (ut64)len;
395  u.steal = true;
396 
397  return new_buffer(RZ_BUFFER_BYTES, &u);
398 }
RZ_API RZ_OWN char * rz_file_slurp(const char *str, RZ_NULLABLE size_t *usz)
Definition: file.c:454

References buf_bytes_user::data_steal, len, buf_bytes_user::length, new_buffer(), NULL, RZ_BUFFER_BYTES, rz_file_slurp(), buf_bytes_user::steal, autogen_x86imm::tmp, and ut64().

Referenced by rz_bin_pdb_parse_from_file(), rz_core_flirt_convert_file(), rz_core_flirt_dump_file(), and rz_sign_flirt_apply().

◆ rz_buf_new_sparse()

RZ_API RZ_OWN RzBuffer* rz_buf_new_sparse ( ut8  Oxff)

Creates a sparse buffer.

Parameters
OxffThe byte used to fill unpopulated bytes.
Returns
Return the new allocated buffer.

The function creates a new allocated buffer using the RZ_BUFFER_SPARSE back end. Creates a new sparse RzBuffer where unpopulated bytes are filled with Oxff parameter.

Definition at line 408 of file buf.c.

408  {
410  if (b) {
411  b->Oxff_priv = Oxff;
412  }
413 
414  return b;
415 }

References b, new_buffer(), NULL, and RZ_BUFFER_SPARSE.

Referenced by __open().

◆ rz_buf_new_sparse_overlay()

RZ_API RZ_OWN RzBuffer* rz_buf_new_sparse_overlay ( RzBuffer b,
RzBufferSparseWriteMode  write_mode 
)

Creates a sparse buffer from a already populated buffer.

Parameters
bThe source buffer used to create the sub buffer.
write_modeDefined how byte are written to the buffer.
Returns
Return the new allocated buffer.

The function creates a new allocated buffer using the RZ_BUFFER_SPARSE back end. Creates a new sparse RzBuffer where unpopulated bytes are taken as-is from b

Definition at line 426 of file buf.c.

426  {
428 
429  SparseInitConfig cfg = {
430  .base = b,
431  .write_mode = write_mode
432  };
433 
434  return new_buffer(RZ_BUFFER_SPARSE, &cfg);
435 }
RzBuffer * base
Definition: buf_sparse.c:8

References b, sparse_init_config_t::base, new_buffer(), NULL, RZ_BUFFER_SPARSE, and rz_return_val_if_fail.

Referenced by bflt_patch_relocs(), patch_relocs(), and rz_coff_get_patched_buf().

◆ rz_buf_new_with_buf()

RZ_API RZ_OWN RzBuffer* rz_buf_new_with_buf ( RzBuffer b)

Creates a new buffer from a source buffer.

Parameters
bThe source buffer used to create the sub buffer.
Returns
Return the new allocated buffer.
See also
rz_buf_new_with_bytes()

The function creates a new allocated buffer using the RZ_BUFFER_BYTES back end. WARNING: this function doesn't use reference counting, this can lead to a lot of memory overhead.

Definition at line 448 of file buf.c.

448  {
449  ut64 size = 0;
450  const ut8 *tmp = get_whole_buf(b, &size);
451 
452  return rz_buf_new_with_bytes(tmp, size);
453 }
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_bytes(RZ_NULLABLE RZ_BORROW const ut8 *bytes, ut64 len)
Creates a new buffer with a bytes array.
Definition: buf.c:465

References b, get_whole_buf(), rz_buf_new_with_bytes(), autogen_x86imm::tmp, and ut64().

Referenced by rabin_do_operation(), rz_bin_mz_new_buf(), rz_bin_pemixed_init_native(), and rz_bin_te_new_buf().

◆ rz_buf_new_with_bytes()

RZ_API RZ_OWN RzBuffer* rz_buf_new_with_bytes ( RZ_NULLABLE RZ_BORROW const ut8 bytes,
ut64  len 
)

Creates a new buffer with a bytes array.

Parameters
bytesThe bytes array used to initialized the buffer.
lenThe length of the bytes array.
Returns
Return the new allocated buffer.

The function creates a new buffer in memory, initializing it with the bytes passed as argument. The bytes parameter can be NULL, but the length should be set to 0.

Definition at line 465 of file buf.c.

465  {
466  rz_return_val_if_fail(bytes || !len, NULL); // if bytes == NULL, then len must be 0
467 
468  struct buf_bytes_user u = { 0 };
469  u.data = bytes;
470  u.length = len;
471 
472  return new_buffer(RZ_BUFFER_BYTES, &u);
473 }
static ut8 bytes[32]
Definition: asm_arc.c:23
const ut8 * data
Definition: buf_bytes.c:7

References bytes, buf_bytes_user::data, len, buf_bytes_user::length, new_buffer(), NULL, RZ_BUFFER_BYTES, and rz_return_val_if_fail.

Referenced by build(), create(), dex_create_relocations(), gprobe_debugoff(), gprobe_debugon(), gprobe_getdeviceid(), gprobe_getinformation(), gprobe_read(), gprobe_reset(), gprobe_runcode(), gprobe_write(), handle_tmp_desc(), rz_analysis_reflines_str(), rz_bin_fatmach0_from_bytes_new(), rz_bin_fatmach0_new(), rz_bin_file_set_bytes(), rz_bin_mdmp_init_pe_bins(), rz_bin_mz_new(), rz_bin_pemixed_from_bytes_new(), rz_bin_pemixed_init_dos(), rz_bin_te_new(), rz_buf_new_with_buf(), rz_buf_new_with_string(), rz_core_cmd_subst_i(), rz_core_init(), rz_egg_finalize(), rz_egg_new(), rz_egg_patch(), rz_egg_reset(), rz_io_zip_create_new_file(), rz_io_zip_slurp_file(), rz_main_rz_gg(), socket_http_answer(), and xnu_generate_corefile().

◆ rz_buf_new_with_io()

RZ_API RZ_OWN RzBuffer* rz_buf_new_with_io ( RZ_NONNULL void *  iob)

Creates a new buffer wrapping the memory map exposed by RzIOBind.

Parameters
iobPointer to RzIOBind structure.
Returns
Return the new allocated buffer.

This buffer will use rz_io_read_at()/rz_io_write_at() as implemented by the RzIOBind given.

Definition at line 509 of file buf.c.

509  {
511  return new_buffer(RZ_BUFFER_IO, iob);
512 }

References buf_io_fd_user::iob, new_buffer(), NULL, RZ_BUFFER_IO, and rz_return_val_if_fail.

Referenced by rz_analysis_il_vm_new(), and rz_debug_dmp_init().

◆ rz_buf_new_with_io_fd()

RZ_API RZ_OWN RzBuffer* rz_buf_new_with_io_fd ( RZ_NONNULL void *  iob,
int  fd 
)

Creates a new buffer wrapping a file descriptor accessed through RzIOBind.

Parameters
iobPointer to RzIOBind structure.
fdFile descriptor to wrap in the buffer.
Returns
Return the new allocated buffer.

The function creates a new buffer wrapping access to a file descriptor through the RzIOBind methods specified in librz/io.

Definition at line 490 of file buf.c.

490  {
491  rz_return_val_if_fail(iob && fd >= 0, NULL);
492 
493  struct buf_io_fd_user u = { 0 };
494 
495  u.iob = (RzIOBind *)iob;
496  u.fd = fd;
497 
498  return new_buffer(RZ_BUFFER_IO_FD, &u);
499 }
RzIOBind * iob
Definition: buf_io_fd.c:8
static const z80_opcode fd[]
Definition: z80_tab.h:997

References fd, buf_io_fd_user::fd, buf_io_fd_user::iob, new_buffer(), NULL, RZ_BUFFER_IO_FD, and rz_return_val_if_fail.

Referenced by basefind_new_bin_file(), cmd_search_bin(), rz_bin_file_compute_hashes(), rz_bin_open_io(), and rz_core_bin_whole_strings().

◆ rz_buf_new_with_methods()

RZ_API RZ_OWN RzBuffer* rz_buf_new_with_methods ( RZ_NONNULL const RzBufferMethods methods,
void *  init_user 
)

Creates a new buffer with a specific back end.

Parameters
methodsA struct holding the list of methods to use.
init_userSome implementation specific information.
Returns
Return the new allocated buffer.
See also
RzBufferMethods

The function creates a new allocated buffer using a custom back end. This function should only be used when no other back end are appropriate.

Definition at line 525 of file buf.c.

525  {
527  if (!b) {
528  return NULL;
529  }
530 
531  b->methods = methods;
532 
533  if (!buf_init(b, init_user)) {
534  free(b);
535  return NULL;
536  }
537 
538  return b;
539 }
static bool buf_init(RzBuffer *b, const void *user)
Definition: buf.c:40
#define RZ_NEW0(x)
Definition: rz_types.h:284

References b, buf_init(), free(), NULL, and RZ_NEW0.

Referenced by new_buffer(), new_rebasing_and_stripping_buf(), rz_dyldcache_new_rebasing_buf(), and rz_xnu_kernelcache_new_rebasing_buf().

◆ rz_buf_new_with_pointers()

RZ_API RZ_OWN RzBuffer* rz_buf_new_with_pointers ( const ut8 bytes,
ut64  len,
bool  steal 
)

Creates a new buffer with a bytes array.

Parameters
bytesThe bytes array used to initialized the buffer.
lenThe length of the bytes array.
stealIf the boolean is true the function take the ownership of the data.
Returns
Return the new allocated buffer.

The function creates a new buffer in memory. The argument bytes is used as the memory backend if steal is true, otherwise a new buffer is allocated and its content is initialized with the one from bytes.

Definition at line 552 of file buf.c.

552  {
553  struct buf_bytes_user u = { 0 };
554 
555  u.data_steal = bytes;
556  u.length = len;
557  u.steal = steal;
558 
559  return new_buffer(RZ_BUFFER_BYTES, &u);
560 }

References bytes, buf_bytes_user::data_steal, len, buf_bytes_user::length, new_buffer(), RZ_BUFFER_BYTES, and buf_bytes_user::steal.

Referenced by extract_slice(), load_buffer(), pdb7_extract_msf_stream_directory(), pdb7_extract_streams(), rz_axml_decode(), rz_bin_file_set_bytes(), rz_buf_fread(), rz_buf_fwrite(), rz_cf_value_data_new(), and rz_sign_flirt_parse_compressed_pattern_from_buffer().

◆ rz_buf_new_with_string()

RZ_API RZ_OWN RzBuffer* rz_buf_new_with_string ( RZ_NONNULL const char *  msg)

Creates a new buffer from a string.

Parameters
msgThe source string used to create the buffer.
Returns
Return the new allocated buffer.

The function creates a new buffer in memory, initializing it with the string passed as argument.

Definition at line 570 of file buf.c.

570  {
571  return rz_buf_new_with_bytes((const ut8 *)msg, (ut64)strlen(msg));
572 }
static struct sockaddr static addrlen static backlog const void msg
Definition: sfsocketcall.h:119

References msg, rz_buf_new_with_bytes(), and ut64().

Referenced by rz_asm_pseudo_incbin().

◆ rz_buf_prepend_bytes()

RZ_API bool rz_buf_prepend_bytes ( RZ_NONNULL RzBuffer b,
RZ_NONNULL const ut8 buf,
ut64  length 
)

Prepend an array of bytes to the buffer.

Parameters
bA buffer with write permission.
buf...
length...
Returns
Return the status of the operation.

...

Definition at line 846 of file buf.c.

846  {
847  rz_return_val_if_fail(b && buf && !b->readonly, false);
848 
849  return rz_buf_insert_bytes(b, 0, buf, length) >= 0;
850 }
RZ_API st64 rz_buf_insert_bytes(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL const ut8 *buf, ut64 length)
Insert an array of bytes in the buffer.
Definition: buf.c:1087

References b, length, rz_buf_insert_bytes(), and rz_return_val_if_fail.

Referenced by gprobe_frame_sp(), rz_egg_prepend_bytes(), and rz_egg_raw_prepend().

◆ rz_buf_read()

RZ_API st64 rz_buf_read ( RZ_NONNULL RzBuffer b,
RZ_NONNULL ut8 RZ_OUT buf,
ut64  len 
)

Read len bytes of the buffer at the cursor.

Parameters
b...
buf...
len...
Returns
Return the number of bytes read.

...

Definition at line 1111 of file buf.c.

1111  {
1112  rz_return_val_if_fail(b && buf, -1);
1113 
1114  st64 result = buf_read(b, buf, len);
1115  if (result < 0) {
1116  return -1;
1117  }
1118 
1119  if (len > result) {
1120  memset(buf + result, b->Oxff_priv, len - result);
1121  }
1122 
1123  return result;
1124 }
static st64 buf_read(RzBuffer *b, ut8 *buf, size_t len)
Definition: buf.c:58
return memset(p, 0, total)

References b, buf_read(), len, memset(), rz_return_val_if_fail, and st64.

Referenced by buf_format(), rz_buf_read8(), rz_buf_read_at(), rz_buf_sleb128(), and rz_buf_uleb128().

◆ rz_buf_read8()

RZ_API bool rz_buf_read8 ( RZ_NONNULL RzBuffer b,
RZ_NONNULL RZ_OUT ut8 result 
)

Read a byte at the cursor in the buffer.

Parameters
b...
result...
Returns
Return the status of the operation.

...

Definition at line 860 of file buf.c.

860  {
861  rz_return_val_if_fail(b && result, false);
862 
863  return rz_buf_read(b, result, sizeof(ut8)) == sizeof(ut8);
864 }
#define ut8
Definition: dcpu16.h:8

References b, rz_buf_read(), rz_return_val_if_fail, and ut8.

Referenced by bin_pe_dotnet_read_method_header(), consume_init_expr_r(), consumeBuffer(), has_non_padding(), java_attribute_set_code(), java_constant_pool_new(), parse_gdata_global(), parse_type_bitfield(), parse_type_mfunction(), parse_type_numeric(), parse_type_procedure(), parse_type_string(), showBuffer(), and skip_padding().

◆ rz_buf_read8_at()

◆ rz_buf_read_at()

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.

Parameters
b...
addr...
buf...
len...
Returns
Return the number of bytes read.

...

Definition at line 1136 of file buf.c.

1136  {
1137  rz_return_val_if_fail(b && buf, -1);
1138 
1139  st64 tmp = rz_buf_tell(b);
1140  if (tmp < 0) {
1141  return -1;
1142  }
1143 
1144  if (rz_buf_seek(b, addr, RZ_BUF_SET) < 0) {
1145  return -1;
1146  }
1147 
1148  st64 result = rz_buf_read(b, buf, len);
1149 
1150  if (rz_buf_seek(b, tmp, RZ_BUF_SET) < 0) {
1151  return -1;
1152  }
1153 
1154  return result;
1155 }

References addr, b, len, rz_buf_read(), rz_buf_seek(), RZ_BUF_SET, rz_buf_tell(), rz_return_val_if_fail, st64, and autogen_x86imm::tmp.

Referenced by __dumpSections(), __ne_get_resources(), __read(), __read_nonnull_str_at(), _parse_resource_directory(), ar_read_at(), bflt_init_hdr(), bin_pe_dotnet_init_metadata(), bin_pe_get_actual_checksum(), bin_pe_init_security(), bin_pe_parse_imports(), binsym(), bootimg_header_load(), buf_move_back(), buf_read(), buf_ref_read(), buf_sparse_read(), carve_deps_at_address(), carve_kexts(), check_buffer(), check_mingw(), check_msvcseh(), check_pe32_buf(), check_pe64_buf(), check_unknow(), checkEntrypointBuffer(), checkHeader(), classes(), create_cache_bins(), create_path_to_index(), decompress(), entries(), extract_binobj(), extract_slice(), get_category_t(), get_class_name(), get_class_ro_t(), get_class_t(), get_entrypoint(), get_filetype(), get_import_addr_mips(), get_ivar_list_t(), get_lib_name(), get_main(), get_main_offset_mips(), get_main_offset_x86_pie(), get_method_list_t(), get_objc_property_list(), get_protocol_list_t(), get_rebase_info(), get_relocs(), get_section_bytes(), get_whole_buf(), gprobe_read(), header(), info(), init_hdr(), init_items(), io_read_va_at(), is_compressed_pdb(), iterate_rebase_list(), kexts_from_load_commands(), knownHeaderBuffer(), le_get_entries(), le_init_header(), le_read_nonnull_str_at(), load_buffer(), lua_load_block(), lua_parse_string(), mach0_info_new(), mach_headerfields(), parse_classes(), parse_dylib(), parse_dysymtab(), parse_function_starts(), parse_go_build_info(), parse_note_prstatus(), parse_relocation_info(), parse_segments(), parse_signature(), parse_symbol_table(), parse_symtab(), parse_thread(), parseCodeDirectory(), parseDragons(), parseHeader(), parseMetadata(), Pe_r_bin_pe_parse_string(), Pe_r_bin_pe_parse_string_file_info(), Pe_r_bin_pe_parse_string_table(), Pe_r_bin_pe_parse_var(), Pe_r_bin_pe_parse_var_file_info(), Pe_r_bin_pe_parse_version_info(), process_constructors(), process_kmod_init_term(), rabin_dump_symbols(), read_and_follow_jump(), read_elf_intrp(), read_image_section_header(), read_le_header_aux(), read_n_bits(), read_ptr_pa(), read_str(), read_xbe_header(), read_xbe_lib(), read_xbe_section(), readString(), rebase_buffer(), reconstruct_chained_fixup(), resolve_mig_subsystem(), resolve_syscalls(), rz_axml_decode(), rz_bin_dmp64_init_bmp_header(), rz_bin_dmp64_init_header(), rz_bin_dmp64_init_memory_runs(), rz_bin_elf_get_compiler(), rz_bin_elf_get_ehdr(), rz_bin_elf_get_main_offset(), rz_bin_elf_strtab_new(), rz_bin_fatmach0_init(), rz_bin_le_get_relocs(), rz_bin_mdmp_init_directory_entry(), rz_bin_mdmp_init_pe_bins(), rz_bin_mz_get_main_vaddr(), rz_bin_mz_init_hdr(), rz_bin_ne_buf_init(), rz_bin_ne_get_imports(), rz_bin_ne_get_relocs(), rz_bin_ne_get_symbols(), rz_bin_pe_get_debug_data(), rz_bin_pe_get_exports(), rz_bin_pe_get_imports(), rz_bin_pe_get_libs(), rz_bin_pe_get_sections(), rz_bin_pemixed_init_dos(), rz_bin_te_get_main_paddr(), rz_bin_te_init_sections(), rz_bin_zimg_new_buf(), rz_buf_append_buf_slice(), rz_buf_fwd_scan(), rz_buf_get_nstring(), rz_buf_read8_at(), rz_buf_to_string(), rz_cf_value_dict_parse(), rz_coff_symbol_name(), rz_core_yank_paste(), rz_core_yank_print(), rz_core_yank_print_hexdump(), rz_core_yank_print_string(), rz_coresym_cache_element_new(), rz_il_mem_load(), rz_io_zip_read(), rz_rebase_info_new_from_mach0(), rz_scan_strings(), sections(), shared_data_read_at(), size(), socket_http_answer(), string_scan_range_cfstring(), symbols(), symbols_from_stubs(), trycatch(), vf_read(), and walk_exports().

◆ rz_buf_ref()

RZ_API RzBuffer* rz_buf_ref ( RzBuffer b)

Increment the reference count of the buffer.

Parameters
bRzBuffer to reference
Returns
Return the same value b

The function increment the reference count of the buffer

Definition at line 668 of file buf.c.

668  {
669  if (b) {
670  b->refctr++;
671  }
672 
673  return b;
674 }

References b.

Referenced by buf_ref_init(), buf_sparse_init(), load_buffer(), rz_bflt_init(), rz_bin_coff_init(), rz_bin_dmp64_new_buf(), rz_bin_elf_new_buf(), rz_bin_fatmach0_from_buffer_new(), rz_bin_file_new_from_buffer(), rz_bin_file_object_new_from_xtr_data(), rz_bin_mdmp_new_buf(), rz_bin_pe_new_buf(), rz_bin_wasm_init(), rz_bin_xtrdata_new(), rz_bin_zimg_new_buf(), rz_dyldcache_new_buf(), and rz_il_mem_new().

◆ rz_buf_resize()

RZ_API bool rz_buf_resize ( RZ_NONNULL RzBuffer b,
ut64  newsize 
)

Resize the buffer size.

Parameters
b...
newsize...
Returns
Return the status of the operation.

...

Definition at line 890 of file buf.c.

890  {
891  rz_return_val_if_fail(b, false);
892 
893  return buf_resize(b, newsize);
894 }
static bool buf_resize(RzBuffer *b, ut64 newsize)
Definition: buf.c:78

References b, buf_resize(), and rz_return_val_if_fail.

Referenced by __resize(), buf_bytes_write(), buf_move_back(), buf_resize(), rz_buf_append_nbytes(), rz_buf_set_bytes(), rz_io_def_mmap_truncate(), rz_io_zip_realloc_buf(), rz_io_zip_truncate_buf(), and vf_resize().

◆ rz_buf_seek()

RZ_API st64 rz_buf_seek ( RZ_NONNULL RzBuffer b,
st64  addr,
int  whence 
)

Modify the current cursor position in the buffer.

Parameters
b...
addr...
whenceThe relative position of the address.
Returns
Return the new cursor position

...

Definition at line 1166 of file buf.c.

1166  {
1167  rz_return_val_if_fail(b, -1);
1168 
1169  return buf_seek(b, addr, whence);
1170 }
static st64 buf_seek(RzBuffer *b, st64 addr, int whence)
Definition: buf.c:72

References addr, b, buf_seek(), and rz_return_val_if_fail.

Referenced by __lseek(), __read(), __write(), ar_parse_header(), bin_pe_dotnet_read_method_header(), consume_r(), consumeBuffer(), dex_class_def_new(), dex_parse(), entries(), get_entries_from_section(), get_entrypoint(), has_non_padding(), java_attribute_set_code(), java_attribute_set_unknown(), java_constant_pool_new(), parse_code_entry(), parse_data_entry(), parse_dbi_stream(), parse_dbi_stream_ex_header(), parse_gdata_global(), parse_gdata_stream(), parse_type_string(), parse_v5_header(), pdb7_extract_msf_stream_directory(), pdb7_extract_streams(), read_desc(), read_entry(), read_hdr(), read_image_clr_header(), read_image_debug_directory_entry(), read_image_delay_import_directory(), read_image_export_directory(), read_image_import_directory(), read_image_metadata_tilde_header(), read_image_resource_data_entry(), read_image_resource_directory(), read_image_resource_directory_entry(), read_memory64_list(), read_module(), read_tls_directory(), rz_bin_dmp64_init_triage(), rz_bin_dmp64_init_triage_datablocks(), rz_bin_dmp64_init_triage_drivers(), rz_bin_reload(), rz_bin_wasm_get_custom_name_entries(), rz_bin_wasm_get_sections(), rz_bin_wasm_get_start(), rz_buf_append_bytes(), rz_buf_fread_at(), rz_buf_fwrite_at(), rz_buf_read_at(), rz_buf_set_bytes(), rz_buf_sleb128_at(), rz_buf_tell(), rz_buf_uleb128_at(), rz_buf_write_at(), rz_egg_compile(), rz_io_def_mmap_seek(), rz_io_zip_lseek(), rz_io_zip_read(), rz_io_zip_write(), rz_main_rz_gg(), showBuffer(), skip_padding(), and symbols().

◆ rz_buf_set_bytes()

RZ_API bool rz_buf_set_bytes ( RZ_NONNULL RzBuffer b,
RZ_NONNULL const ut8 buf,
ut64  length 
)

Replace the content of the buffer with the bytes array.

Parameters
bA buffer with write permission.
buf...
length...
Returns
Return the status of the operation.

...

Definition at line 905 of file buf.c.

905  {
906  rz_return_val_if_fail(b && buf && !b->readonly, false);
907 
908  if (!rz_buf_resize(b, 0)) {
909  return false;
910  }
911 
912  if (rz_buf_seek(b, 0, RZ_BUF_SET) < 0) {
913  return false;
914  }
915 
916  if (!rz_buf_append_bytes(b, buf, length)) {
917  return false;
918  }
919 
920  return rz_buf_seek(b, 0, RZ_BUF_SET) >= 0;
921 }

References b, length, rz_buf_append_bytes(), rz_buf_resize(), rz_buf_seek(), RZ_BUF_SET, and rz_return_val_if_fail.

Referenced by build(), rz_bin_fatmach0_from_bytes_new(), rz_bin_fatmach0_new(), rz_bin_mz_new(), rz_bin_te_new(), rz_core_yank_set(), and rz_io_zip_slurp_file().

◆ rz_buf_set_overflow_byte()

RZ_API void rz_buf_set_overflow_byte ( RZ_NONNULL RzBuffer b,
ut8  Oxff 
)

Change the overflow byte used in the RZ_BUFFER_SPARSE.

Parameters
b...
OxffThe new byte filling value.
See also
rz_buf_new_sparse()
rz_buf_new_sparse_overlay()

Set the content that bytes read outside the buffer bounds should have.

Definition at line 1272 of file buf.c.

1272  {
1274 
1275  b->Oxff_priv = Oxff;
1276 }
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100

References b, and rz_return_if_fail.

◆ rz_buf_size()

RZ_API ut64 rz_buf_size ( RZ_NONNULL RzBuffer b)

Return the size of the buffer.

Parameters
b...
Returns
...

...

Definition at line 1225 of file buf.c.

1225  {
1227 
1228  return buf_get_size(b);
1229 }
static ut64 buf_get_size(RzBuffer *b)
Definition: buf.c:52

References b, buf_get_size(), and rz_return_val_if_fail.

Referenced by __dumpSections(), ar_open_all(), ar_open_file(), art_header_load(), bin_pe_compute_authentihash(), bin_pe_dotnet_init_metadata(), bin_pe_init_imports(), bootimg_header_load(), buf_get_size(), buf_move_back(), buf_ref_init(), buf_ref_resize(), buf_seek(), buf_sparse_size(), build(), check_buffer(), check_buffer_jmp(), check_buffer_rjmp(), checkEntrypointBuffer(), checkHeader(), consume_init_expr_r(), consume_limits_r(), consume_locals_r(), consume_r(), consume_str_r(), consumeBuffer(), create(), dex_parse(), entries(), get_elf_intrp(), get_entries_from_section(), get_main_offset_arm_glibc_thumb(), get_section(), get_whole_buf(), gprobe_read(), handle_tmp_desc(), is_eob(), java_class_parse(), kexts_from_load_commands(), knownHeaderBuffer(), lmf_header_load(), load_buffer(), MACH0_(), mach_headerfields(), make_string(), maps(), parse_header(), parse_omap_stream(), parse_pe_stream(), parse_signature(), parseDragons(), rz_asm_massemble(), rz_bflt_init(), rz_bin_coff_init(), rz_bin_dmp64_init_triage(), rz_bin_dmp64_new_buf(), rz_bin_elf_new_buf(), rz_bin_fatmach0_from_buffer_new(), rz_bin_file_compute_hashes(), rz_bin_file_new_from_buffer(), rz_bin_file_xtr_load_buffer(), rz_bin_mdmp_init_directory_entry(), rz_bin_mdmp_new_buf(), rz_bin_mz_get_segments(), rz_bin_mz_new_buf(), rz_bin_ne_get_relocs(), rz_bin_object_new(), rz_bin_open_io(), rz_bin_pdb_parse_from_buf(), rz_bin_pe_get_debug_data(), rz_bin_pe_new_buf(), rz_bin_te_new_buf(), rz_bin_wasm_get_custom_name_entries(), rz_bin_wasm_get_sections(), rz_bin_wasm_get_start(), rz_bin_wasm_init(), rz_bin_zimg_new_buf(), rz_buf_append_nbytes(), rz_buf_fwd_scan(), rz_buf_get_string(), rz_buf_to_string(), rz_core_cmd_subst_i(), rz_core_yank_as_string(), rz_core_yank_dump(), rz_core_yank_paste(), rz_core_yank_print(), rz_core_yank_print_hexdump(), rz_core_yank_print_string(), rz_coresym_cache_element_new(), rz_egg_finalize(), rz_io_open_buffer(), rz_io_zip_lseek(), rz_io_zip_read(), rz_io_zip_write(), rz_sign_flirt_parse_compressed_pattern_from_buffer(), rz_xnu_kernelcache_buf_is_kernelcache(), sections(), sep64_xtr_ctx_get_slice(), showBuffer(), size(), trycatch(), and vf_lseek().

◆ rz_buf_sleb128()

RZ_API st64 rz_buf_sleb128 ( RZ_NONNULL RzBuffer buffer,
RZ_NONNULL st64 value 
)

Decodes SLEB128 from RzBuffer.

Parameters
bufferBuffer used to decode the SLEB128.
valueDecoded value.
Returns
The number of bytes used.

Definition at line 1384 of file buf.c.

1384  {
1386 
1387  ut64 used = 0, slice;
1388  st64 sum = 0;
1389  ut32 shift = 0;
1390  ut8 byte = 0;
1391  do {
1392  if (rz_buf_read(buffer, &byte, sizeof(byte)) < 1) {
1393  // malformed sleb128 due end of buffer
1394  return -1;
1395  }
1396  used++;
1397  slice = byte & 0x7f;
1398  if ((shift >= 64 && slice != (sum < 0 ? 0x7f : 0x00)) ||
1399  (shift == 63 && slice != 0 && slice != 0x7f)) {
1400  // sleb128 too big for st64
1401  return -1;
1402  }
1403  sum |= slice << shift;
1404  shift += 7;
1405  } while (byte >= 128);
1406 
1407  if (shift < 64 && (byte & 0x40)) {
1408  // extends negative sign
1409  sum |= (-1ull) << shift;
1410  }
1411  *value = sum;
1412  return used;
1413 }
static RZ_NULLABLE RzILOpBitVector * shift(RzILOpBitVector *val, RZ_NULLABLE RzILOpBool **carry_out, arm_shifter type, RZ_OWN RzILOpBitVector *dist)
Definition: arm_il32.c:190
static int value
Definition: cmd_api.c:93
Definition: buffer.h:15

References rz_buf_read(), rz_return_val_if_fail, shift(), st64, ut64(), and value.

Referenced by rz_buf_sleb128_at().

◆ rz_buf_tell()

◆ rz_buf_to_string()

RZ_API RZ_OWN char* rz_buf_to_string ( RZ_NONNULL RzBuffer b)

Stringify the buffer.

Parameters
bRzBuffer pointer
Returns
Return an allocated string.

The string is guaranteed to be NULL terminated even if the buffer doesn't have any NULL bytes.

Definition at line 642 of file buf.c.

642  {
644 
645  ut64 size = rz_buf_size(b);
646  char *result = RZ_NEWS(char, size + 1);
647  if (!result) {
648  return NULL;
649  }
650 
651  if (rz_buf_read_at(b, 0, (ut8 *)result, size) < 0) {
652  free(result);
653  return NULL;
654  }
655 
656  result[size] = '\0';
657 
658  return result;
659 }

References b, free(), NULL, rz_buf_read_at(), rz_buf_size(), RZ_NEWS, rz_return_val_if_fail, and ut64().

Referenced by gprobe_getdeviceid(), rz_analysis_reflines_str(), rz_asm_massemble(), rz_egg_assemble_asm(), rz_egg_get_assembly(), rz_egg_get_source(), and rz_egg_to_string().

◆ rz_buf_uleb128()

RZ_API st64 rz_buf_uleb128 ( RZ_NONNULL RzBuffer buffer,
RZ_NONNULL ut64 value 
)

Decodes ULEB128 from RzBuffer.

Parameters
bufferBuffer used to decode the ULEB128.
valueDecoded value.
Returns
The number of bytes used.

Definition at line 1353 of file buf.c.

1353  {
1355 
1356  ut64 sum = 0, used = 0, slice;
1357  ut32 shift = 0;
1358  ut8 byte = 0;
1359  do {
1360  if (rz_buf_read(buffer, &byte, sizeof(byte)) < 1) {
1361  // malformed uleb128 due end of buffer
1362  return -1;
1363  }
1364  used++;
1365  slice = byte & 0x7f;
1366  if ((shift >= 64 && slice != 0) || ((slice << shift) >> shift) != slice) {
1367  // uleb128 too big for ut64
1368  return -1;
1369  }
1370  sum += slice << shift;
1371  shift += 7;
1372  } while (byte >= 128);
1373  *value = sum;
1374  return used;
1375 }

References rz_buf_read(), rz_return_val_if_fail, shift(), ut64(), and value.

Referenced by dex_class_def_new(), dex_new_encoded_field(), dex_new_encoded_method(), dex_string_new(), and rz_buf_uleb128_at().

◆ rz_buf_write()

RZ_API st64 rz_buf_write ( RZ_NONNULL RzBuffer b,
RZ_NONNULL const ut8 buf,
ut64  len 
)

Write len bytes of the buffer at the cursor.

Parameters
bA buffer with write permission.
buf...
len...
Returns
Return the number of bytes written.

...

Definition at line 1181 of file buf.c.

1181  {
1182  rz_return_val_if_fail(b && buf && !b->readonly, -1);
1183 
1184  return buf_write(b, buf, len);
1185 }
static st64 buf_write(RzBuffer *b, const ut8 *buf, size_t len)
Definition: buf.c:64

References b, buf_write(), len, and rz_return_val_if_fail.

Referenced by buf_format(), rz_buf_append_bytes(), rz_buf_write8(), rz_buf_write_at(), and rz_io_def_mmap_write().

◆ rz_buf_write8()

RZ_API bool rz_buf_write8 ( RZ_NONNULL RzBuffer b,
ut8  value 
)

Write a byte at the cursor in the buffer.

Parameters
b...
result...
Returns
Return the status of the operation.

...

Definition at line 931 of file buf.c.

931  {
932  rz_return_val_if_fail(b, false);
933 
934  return rz_buf_write(b, &value, sizeof(ut8)) == sizeof(ut8);
935 }

References b, rz_buf_write(), rz_return_val_if_fail, ut8, and value.

◆ rz_buf_write8_at()

RZ_API bool rz_buf_write8_at ( RZ_NONNULL RzBuffer b,
ut64  addr,
ut8  value 
)

Write a byte at the specified address in the buffer.

Parameters
b...
addr...
value...
Returns
Return the status of the operation.
See also
rz_buf_write8()

...

Definition at line 948 of file buf.c.

948  {
949  rz_return_val_if_fail(b, false);
950 
951  return rz_buf_write_at(b, addr, &value, sizeof(ut8)) == sizeof(ut8);
952 }

References addr, b, rz_buf_write_at(), rz_return_val_if_fail, ut8, and value.

◆ rz_buf_write_at()

RZ_API st64 rz_buf_write_at ( RZ_NONNULL RzBuffer b,
ut64  addr,
RZ_NONNULL const ut8 buf,
ut64  len 
)

Write len bytes of the buffer at the specified address.

Parameters
bA buffer with write permission.
addr...
buf...
len...
Returns
Return the number of bytes written.

...

Definition at line 1197 of file buf.c.

1197  {
1198  rz_return_val_if_fail(b && buf && !b->readonly, -1);
1199 
1200  st64 tmp = rz_buf_tell(b);
1201  if (tmp < 0) {
1202  return -1;
1203  }
1204 
1205  if (rz_buf_seek(b, addr, RZ_BUF_SET) < 0) {
1206  return -1;
1207  }
1208 
1209  st64 result = rz_buf_write(b, buf, len);
1210 
1211  if (rz_buf_seek(b, tmp, RZ_BUF_SET) < 0) {
1212  return -1;
1213  }
1214 
1215  return result;
1216 }

References addr, b, len, rz_buf_seek(), RZ_BUF_SET, rz_buf_tell(), rz_buf_write(), rz_return_val_if_fail, st64, and autogen_x86imm::tmp.

Referenced by __open(), __write(), _patch_reloc(), ar_write_at(), buf_move_back(), buf_sparse_write(), buf_write(), build(), fill_level(), ihex_parse(), patch_reloc_cb(), rz_analysis_reflines_str(), rz_bin_pemixed_init_native(), rz_buf_insert_bytes(), rz_buf_write8_at(), rz_core_bin_whole_strings(), rz_core_yank_set_str(), rz_egg_finalize(), rz_il_mem_store(), rz_io_zip_write(), srecord_parse(), vf_write(), write_image_section_header(), and write_n_bits().