Rizin
unix-like reverse engineering framework and cli tools
rz_buf.h File Reference
#include <rz_util/rz_mem.h>
#include <rz_types.h>
#include <rz_list.h>
#include <rz_util/rz_assert.h>

Go to the source code of this file.

Classes

struct  rz_buffer_methods_t
 
struct  rz_buf_t
 
struct  rz_buf_sparse_chunk_t
 

Macros

#define RZ_BUF_SET   0
 
#define RZ_BUF_CUR   1
 
#define RZ_BUF_END   2
 
#define DEFINE_RZ_BUF_READ_BLE(size)
 
#define DEFINE_RZ_BUF_WRITE_BLE(size)
 
#define DEFINE_RZ_BUF_READ_OFFSET_BLE(size)
 Read a big endian or little endian (ut16, ut32, ut64) at the specified address or cursor in the buffer. More...
 
#define DEFINE_RZ_BUF_WRITE_OFFSET_BLE(size)
 
#define rz_buf_read_ble8_at(b, addr, result, endian)   rz_buf_read8_at(b, addr, result)
 
#define rz_buf_write_ble8_at(b, addr, value, endian)   rz_buf_write8_at(b, addr, value)
 
#define rz_buf_read_le16(b, result)   rz_buf_read_ble16(b, result, false)
 Read a big endian or little endian (ut16, ut32, ut64) at the specified offset in the buffer and shifts the offset. More...
 
#define rz_buf_read_le32(b, result)   rz_buf_read_ble32(b, result, false)
 
#define rz_buf_read_le64(b, result)   rz_buf_read_ble64(b, result, false)
 
#define rz_buf_read_le16_at(b, addr, result)   rz_buf_read_ble16_at(b, addr, result, false)
 
#define rz_buf_read_le32_at(b, addr, result)   rz_buf_read_ble32_at(b, addr, result, false)
 
#define rz_buf_read_le64_at(b, addr, result)   rz_buf_read_ble64_at(b, addr, result, false)
 
#define rz_buf_read8_offset(b, offset, result)   rz_buf_read_ble8_offset(b, offset, result, false)
 
#define rz_buf_read_le16_offset(b, offset, result)   rz_buf_read_ble16_offset(b, offset, result, false)
 
#define rz_buf_read_le32_offset(b, offset, result)   rz_buf_read_ble32_offset(b, offset, result, false)
 
#define rz_buf_read_le64_offset(b, offset, result)   rz_buf_read_ble64_offset(b, offset, result, false)
 
#define rz_buf_read_be16(b, result)   rz_buf_read_ble16(b, result, true)
 
#define rz_buf_read_be32(b, result)   rz_buf_read_ble32(b, result, true)
 
#define rz_buf_read_be64(b, result)   rz_buf_read_ble64(b, result, true)
 
#define rz_buf_read_be16_at(b, addr, result)   rz_buf_read_ble16_at(b, addr, result, true)
 
#define rz_buf_read_be32_at(b, addr, result)   rz_buf_read_ble32_at(b, addr, result, true)
 
#define rz_buf_read_be64_at(b, addr, result)   rz_buf_read_ble64_at(b, addr, result, true)
 
#define rz_buf_read_be16_offset(b, offset, result)   rz_buf_read_ble16_offset(b, offset, result, true)
 
#define rz_buf_read_be32_offset(b, offset, result)   rz_buf_read_ble32_offset(b, offset, result, true)
 
#define rz_buf_read_be64_offset(b, offset, result)   rz_buf_read_ble64_offset(b, offset, result, true)
 
#define rz_buf_write_le16(b, value)   rz_buf_write_ble16(b, value, false)
 
#define rz_buf_write_le32(b, value)   rz_buf_write_ble32(b, value, false)
 
#define rz_buf_write_le64(b, value)   rz_buf_write_ble64(b, value, false)
 
#define rz_buf_write_le16_at(b, addr, value)   rz_buf_write_ble16_at(b, addr, value, false)
 
#define rz_buf_write_le32_at(b, addr, value)   rz_buf_write_ble32_at(b, addr, value, false)
 
#define rz_buf_write_le64_at(b, addr, value)   rz_buf_write_ble64_at(b, addr, value, false)
 
#define rz_buf_write8_offset(b, offset, value)   rz_buf_write_ble8_offset(b, offset, value, false)
 
#define rz_buf_write_le16_offset(b, offset, value)   rz_buf_write_ble16_offset(b, offset, value, false)
 
#define rz_buf_write_le32_offset(b, offset, value)   rz_buf_write_ble32_offset(b, offset, value, false)
 
#define rz_buf_write_le64_offset(b, offset, value)   rz_buf_write_ble64_offset(b, offset, value, false)
 
#define rz_buf_write_be16(b, value)   rz_buf_write_ble16(b, value, true)
 
#define rz_buf_write_be32(b, value)   rz_buf_write_ble32(b, value, true)
 
#define rz_buf_write_be64(b, value)   rz_buf_write_ble64(b, value, true)
 
#define rz_buf_write_be16_at(b, addr, value)   rz_buf_write_ble16_at(b, addr, value, true)
 
#define rz_buf_write_be32_at(b, addr, value)   rz_buf_write_ble32_at(b, addr, value, true)
 
#define rz_buf_write_be64_at(b, addr, value)   rz_buf_write_ble64_at(b, addr, value, true)
 
#define rz_buf_write_be16_offset(b, offset, value)   rz_buf_write_ble16_offset(b, offset, value, true)
 
#define rz_buf_write_be32_offset(b, offset, value)   rz_buf_write_ble32_offset(b, offset, value, true)
 
#define rz_buf_write_be64_offset(b, offset, value)   rz_buf_write_ble64_offset(b, offset, value, true)
 

Typedefs

typedef struct rz_buf_t RzBuffer
 
typedef bool(* RzBufferInit) (RzBuffer *b, const void *user)
 
typedef bool(* RzBufferFini) (RzBuffer *b)
 
typedef st64(* RzBufferRead) (RzBuffer *b, ut8 *buf, ut64 len)
 
typedef st64(* RzBufferWrite) (RzBuffer *b, const ut8 *buf, ut64 len)
 
typedef ut64(* RzBufferGetSize) (RzBuffer *b)
 
typedef bool(* RzBufferResize) (RzBuffer *b, ut64 newsize)
 
typedef st64(* RzBufferSeek) (RzBuffer *b, st64 addr, int whence)
 
typedef ut8 *(* RzBufferGetWholeBuf) (RzBuffer *b, ut64 *sz)
 
typedef void(* RzBufferFreeWholeBuf) (RzBuffer *b)
 
typedef RzList *(* RzBufferNonEmptyList) (RzBuffer *b)
 
typedef struct rz_buffer_methods_t RzBufferMethods
 
typedef struct rz_buf_sparse_chunk_t RzBufferSparseChunk
 
typedef ut64(* RzBufferFwdScan) (RZ_BORROW RZ_NONNULL const ut8 *buf, ut64 len, RZ_NULLABLE void *user)
 

Enumerations

enum  RzBufferSparseWriteMode { RZ_BUF_SPARSE_WRITE_MODE_SPARSE , RZ_BUF_SPARSE_WRITE_MODE_THROUGH }
 

Functions

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) More...
 
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 *file, int flags, 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 len)
 Append an array of bytes to the buffer. More...
 
RZ_API bool rz_buf_append_nbytes (RZ_NONNULL RzBuffer *b, ut64 len)
 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 *buf, 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 len)
 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 len)
 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 len)
 Insert an array of bytes in the buffer. More...
 
RZ_API st64 rz_buf_read (RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
 
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...
 
static st64 rz_buf_uleb128_at (RzBuffer *b, ut64 addr, ut64 *v)
 
static st64 rz_buf_sleb128_at (RzBuffer *b, ut64 addr, st64 *v)
 
RZ_API const RzBufferSparseChunkrz_buf_sparse_get_chunks (RzBuffer *b, RZ_NONNULL size_t *count)
 Only for sparse RzBuffers, get all sparse data chunks currently populated. More...
 
RZ_API void rz_buf_sparse_set_write_mode (RzBuffer *b, RzBufferSparseWriteMode mode)
 Only for sparse RzBuffers. More...
 
RZ_API bool rz_buf_sparse_populated_in (RzBuffer *b, ut64 from, ut64 to)
 
RZ_API bool rz_deflatew_buf (RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed, int wbits)
 
RZ_API bool rz_deflate_buf (RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed)
 deflate uncompressed data in RzBbuffer to zlib or gzipped, use MAX_WBITS as the window size logarithm. More...
 
RZ_API bool rz_inflatew_buf (RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed, int wbits)
 
RZ_API bool rz_inflate_buf (RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed)
 inflate compressed data in RzBbuffer, use MAX_WBITS as the window size logarithm. More...
 
RZ_API bool rz_lzma_dec_buf (RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed)
 Decompress the src buffer with LZMA algorithm and put the decompressed data in dst. More...
 
RZ_API bool rz_lzma_enc_buf (RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed)
 Compress the src buffer with LZMA algorithm and put the compressed data in dst. More...
 

Macro Definition Documentation

◆ DEFINE_RZ_BUF_READ_BLE

#define DEFINE_RZ_BUF_READ_BLE (   size)
Value:
static inline bool rz_buf_read_ble##size(RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut##size *result, bool big_endian) { \
rz_return_val_if_fail(b &&result, false); \
\
ut8 tmp[sizeof(ut##size)]; \
if (rz_buf_read(b, tmp, sizeof(tmp)) != sizeof(tmp)) { \
return false; \
} \
\
*result = rz_read_ble##size(tmp, big_endian); \
return true; \
} \
\
static inline bool rz_buf_read_ble##size##_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut##size *result, bool big_endian) { \
rz_return_val_if_fail(b &&result, false); \
\
ut8 tmp[sizeof(ut##size)]; \
if (rz_buf_read_at(b, addr, tmp, sizeof(tmp)) != sizeof(tmp)) { \
return false; \
} \
\
*result = rz_read_ble##size(tmp, big_endian); \
return true; \
}
voidpf void uLong size
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
RZ_API st64 rz_buf_read(RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
static ut64 rz_read_ble(const void *src, bool big_endian, int size)
Definition: rz_endian.h:517
#define RZ_OUT
Definition: rz_types.h:51
#define RZ_NONNULL
Definition: rz_types.h:64
#define b(i)
Definition: sha256.c:42
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58

Definition at line 153 of file rz_buf.h.

◆ DEFINE_RZ_BUF_READ_OFFSET_BLE

#define DEFINE_RZ_BUF_READ_OFFSET_BLE (   size)
Value:
static inline bool rz_buf_read_ble##size##_offset(RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT ut##size *result, bool big_endian) { \
rz_return_val_if_fail(b &&offset &&result, false); \
if (!rz_buf_read_ble##size##_at(b, *offset, result, big_endian)) { \
return false; \
} \
*offset += sizeof(*result); \
return true; \
}
voidpf uLong offset
Definition: ioapi.h:144
#define RZ_INOUT
Definition: rz_types.h:52

Read a big endian or little endian (ut16, ut32, ut64) at the specified address or cursor in the buffer.

Parameters
b...
addr(optional)
result...
big_endian...
Returns
Return the status of the operation.

Write a big endian or little endian ut16 at the specified address or cursor in the buffer.

Parameters
b...
addr(optional)
result...
big_endian...
Returns
Return the status of the operation.

Definition at line 217 of file rz_buf.h.

◆ DEFINE_RZ_BUF_WRITE_BLE

#define DEFINE_RZ_BUF_WRITE_BLE (   size)
Value:
static inline bool rz_buf_write_ble##size(RZ_NONNULL RzBuffer *b, ut##size value, bool big_endian) { \
ut8 tmp[sizeof(ut##size)]; \
rz_write_ble##size(tmp, value, big_endian); \
\
return rz_buf_write(b, tmp, sizeof(tmp)) == sizeof(tmp); \
} \
\
static inline bool rz_buf_write_ble##size##_at(RZ_NONNULL RzBuffer *b, ut64 addr, ut##size value, bool big_endian) { \
ut8 tmp[sizeof(ut##size)]; \
rz_write_ble##size(tmp, value, big_endian); \
\
return rz_buf_write_at(b, addr, tmp, sizeof(tmp)) == sizeof(tmp); \
}
static int value
Definition: cmd_api.c:93
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_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

Definition at line 178 of file rz_buf.h.

◆ DEFINE_RZ_BUF_WRITE_OFFSET_BLE

#define DEFINE_RZ_BUF_WRITE_OFFSET_BLE (   size)
Value:
static inline bool rz_buf_write_ble##size##_offset(RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_INOUT ut64 *offset, ut##size value, bool big_endian) { \
rz_return_val_if_fail(b &&offset, false); \
if (!rz_buf_write_ble##size##_at(b, *offset, value, big_endian)) { \
return false; \
} \
*offset += sizeof(value); \
return true; \
}

Definition at line 227 of file rz_buf.h.

◆ RZ_BUF_CUR

#define RZ_BUF_CUR   1

Definition at line 15 of file rz_buf.h.

◆ RZ_BUF_END

#define RZ_BUF_END   2

Definition at line 16 of file rz_buf.h.

◆ rz_buf_read8_offset

#define rz_buf_read8_offset (   b,
  offset,
  result 
)    rz_buf_read_ble8_offset(b, offset, result, false)

Definition at line 274 of file rz_buf.h.

◆ rz_buf_read_be16

#define rz_buf_read_be16 (   b,
  result 
)    rz_buf_read_ble16(b, result, true)

Definition at line 280 of file rz_buf.h.

◆ rz_buf_read_be16_at

#define rz_buf_read_be16_at (   b,
  addr,
  result 
)    rz_buf_read_ble16_at(b, addr, result, true)

Definition at line 284 of file rz_buf.h.

◆ rz_buf_read_be16_offset

#define rz_buf_read_be16_offset (   b,
  offset,
  result 
)    rz_buf_read_ble16_offset(b, offset, result, true)

Definition at line 288 of file rz_buf.h.

◆ rz_buf_read_be32

#define rz_buf_read_be32 (   b,
  result 
)    rz_buf_read_ble32(b, result, true)

Definition at line 281 of file rz_buf.h.

◆ rz_buf_read_be32_at

#define rz_buf_read_be32_at (   b,
  addr,
  result 
)    rz_buf_read_ble32_at(b, addr, result, true)

Definition at line 285 of file rz_buf.h.

◆ rz_buf_read_be32_offset

#define rz_buf_read_be32_offset (   b,
  offset,
  result 
)    rz_buf_read_ble32_offset(b, offset, result, true)

Definition at line 289 of file rz_buf.h.

◆ rz_buf_read_be64

#define rz_buf_read_be64 (   b,
  result 
)    rz_buf_read_ble64(b, result, true)

Definition at line 282 of file rz_buf.h.

◆ rz_buf_read_be64_at

#define rz_buf_read_be64_at (   b,
  addr,
  result 
)    rz_buf_read_ble64_at(b, addr, result, true)

Definition at line 286 of file rz_buf.h.

◆ rz_buf_read_be64_offset

#define rz_buf_read_be64_offset (   b,
  offset,
  result 
)    rz_buf_read_ble64_offset(b, offset, result, true)

Definition at line 290 of file rz_buf.h.

◆ rz_buf_read_ble8_at

#define rz_buf_read_ble8_at (   b,
  addr,
  result,
  endian 
)    rz_buf_read8_at(b, addr, result)

Definition at line 237 of file rz_buf.h.

◆ rz_buf_read_le16

#define rz_buf_read_le16 (   b,
  result 
)    rz_buf_read_ble16(b, result, false)

Read a big endian or little endian (ut16, ut32, ut64) at the specified offset in the buffer and shifts the offset.

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

Write a big endian or little endian ut16 at the specified address or cursor in the buffer and shifts the offset.

Parameters
b...
addr(optional)
result...
big_endian...
Returns
Return the status of the operation.

Definition at line 266 of file rz_buf.h.

◆ rz_buf_read_le16_at

#define rz_buf_read_le16_at (   b,
  addr,
  result 
)    rz_buf_read_ble16_at(b, addr, result, false)

Definition at line 270 of file rz_buf.h.

◆ rz_buf_read_le16_offset

#define rz_buf_read_le16_offset (   b,
  offset,
  result 
)    rz_buf_read_ble16_offset(b, offset, result, false)

Definition at line 276 of file rz_buf.h.

◆ rz_buf_read_le32

#define rz_buf_read_le32 (   b,
  result 
)    rz_buf_read_ble32(b, result, false)

Definition at line 267 of file rz_buf.h.

◆ rz_buf_read_le32_at

#define rz_buf_read_le32_at (   b,
  addr,
  result 
)    rz_buf_read_ble32_at(b, addr, result, false)

Definition at line 271 of file rz_buf.h.

◆ rz_buf_read_le32_offset

#define rz_buf_read_le32_offset (   b,
  offset,
  result 
)    rz_buf_read_ble32_offset(b, offset, result, false)

Definition at line 277 of file rz_buf.h.

◆ rz_buf_read_le64

#define rz_buf_read_le64 (   b,
  result 
)    rz_buf_read_ble64(b, result, false)

Definition at line 268 of file rz_buf.h.

◆ rz_buf_read_le64_at

#define rz_buf_read_le64_at (   b,
  addr,
  result 
)    rz_buf_read_ble64_at(b, addr, result, false)

Definition at line 272 of file rz_buf.h.

◆ rz_buf_read_le64_offset

#define rz_buf_read_le64_offset (   b,
  offset,
  result 
)    rz_buf_read_ble64_offset(b, offset, result, false)

Definition at line 278 of file rz_buf.h.

◆ RZ_BUF_SET

#define RZ_BUF_SET   0

Definition at line 14 of file rz_buf.h.

◆ rz_buf_write8_offset

#define rz_buf_write8_offset (   b,
  offset,
  value 
)    rz_buf_write_ble8_offset(b, offset, value, false)

Definition at line 300 of file rz_buf.h.

◆ rz_buf_write_be16

#define rz_buf_write_be16 (   b,
  value 
)    rz_buf_write_ble16(b, value, true)

Definition at line 306 of file rz_buf.h.

◆ rz_buf_write_be16_at

#define rz_buf_write_be16_at (   b,
  addr,
  value 
)    rz_buf_write_ble16_at(b, addr, value, true)

Definition at line 310 of file rz_buf.h.

◆ rz_buf_write_be16_offset

#define rz_buf_write_be16_offset (   b,
  offset,
  value 
)    rz_buf_write_ble16_offset(b, offset, value, true)

Definition at line 314 of file rz_buf.h.

◆ rz_buf_write_be32

#define rz_buf_write_be32 (   b,
  value 
)    rz_buf_write_ble32(b, value, true)

Definition at line 307 of file rz_buf.h.

◆ rz_buf_write_be32_at

#define rz_buf_write_be32_at (   b,
  addr,
  value 
)    rz_buf_write_ble32_at(b, addr, value, true)

Definition at line 311 of file rz_buf.h.

◆ rz_buf_write_be32_offset

#define rz_buf_write_be32_offset (   b,
  offset,
  value 
)    rz_buf_write_ble32_offset(b, offset, value, true)

Definition at line 315 of file rz_buf.h.

◆ rz_buf_write_be64

#define rz_buf_write_be64 (   b,
  value 
)    rz_buf_write_ble64(b, value, true)

Definition at line 308 of file rz_buf.h.

◆ rz_buf_write_be64_at

#define rz_buf_write_be64_at (   b,
  addr,
  value 
)    rz_buf_write_ble64_at(b, addr, value, true)

Definition at line 312 of file rz_buf.h.

◆ rz_buf_write_be64_offset

#define rz_buf_write_be64_offset (   b,
  offset,
  value 
)    rz_buf_write_ble64_offset(b, offset, value, true)

Definition at line 316 of file rz_buf.h.

◆ rz_buf_write_ble8_at

#define rz_buf_write_ble8_at (   b,
  addr,
  value,
  endian 
)    rz_buf_write8_at(b, addr, value)

Definition at line 238 of file rz_buf.h.

◆ rz_buf_write_le16

#define rz_buf_write_le16 (   b,
  value 
)    rz_buf_write_ble16(b, value, false)

Definition at line 292 of file rz_buf.h.

◆ rz_buf_write_le16_at

#define rz_buf_write_le16_at (   b,
  addr,
  value 
)    rz_buf_write_ble16_at(b, addr, value, false)

Definition at line 296 of file rz_buf.h.

◆ rz_buf_write_le16_offset

#define rz_buf_write_le16_offset (   b,
  offset,
  value 
)    rz_buf_write_ble16_offset(b, offset, value, false)

Definition at line 302 of file rz_buf.h.

◆ rz_buf_write_le32

#define rz_buf_write_le32 (   b,
  value 
)    rz_buf_write_ble32(b, value, false)

Definition at line 293 of file rz_buf.h.

◆ rz_buf_write_le32_at

#define rz_buf_write_le32_at (   b,
  addr,
  value 
)    rz_buf_write_ble32_at(b, addr, value, false)

Definition at line 297 of file rz_buf.h.

◆ rz_buf_write_le32_offset

#define rz_buf_write_le32_offset (   b,
  offset,
  value 
)    rz_buf_write_ble32_offset(b, offset, value, false)

Definition at line 303 of file rz_buf.h.

◆ rz_buf_write_le64

#define rz_buf_write_le64 (   b,
  value 
)    rz_buf_write_ble64(b, value, false)

Definition at line 294 of file rz_buf.h.

◆ rz_buf_write_le64_at

#define rz_buf_write_le64_at (   b,
  addr,
  value 
)    rz_buf_write_ble64_at(b, addr, value, false)

Definition at line 298 of file rz_buf.h.

◆ rz_buf_write_le64_offset

#define rz_buf_write_le64_offset (   b,
  offset,
  value 
)    rz_buf_write_ble64_offset(b, offset, value, false)

Definition at line 304 of file rz_buf.h.

Typedef Documentation

◆ RzBuffer

typedef struct rz_buf_t RzBuffer

Definition at line 1 of file rz_buf.h.

◆ RzBufferFini

typedef bool(* RzBufferFini) (RzBuffer *b)

Definition at line 21 of file rz_buf.h.

◆ RzBufferFreeWholeBuf

typedef void(* RzBufferFreeWholeBuf) (RzBuffer *b)

Definition at line 28 of file rz_buf.h.

◆ RzBufferFwdScan

typedef ut64(* RzBufferFwdScan) (RZ_BORROW RZ_NONNULL const ut8 *buf, ut64 len, RZ_NULLABLE void *user)

Definition at line 136 of file rz_buf.h.

◆ RzBufferGetSize

typedef ut64(* RzBufferGetSize) (RzBuffer *b)

Definition at line 24 of file rz_buf.h.

◆ RzBufferGetWholeBuf

typedef ut8*(* RzBufferGetWholeBuf) (RzBuffer *b, ut64 *sz)

Definition at line 27 of file rz_buf.h.

◆ RzBufferInit

typedef bool(* RzBufferInit) (RzBuffer *b, const void *user)

Definition at line 20 of file rz_buf.h.

◆ RzBufferMethods

◆ RzBufferNonEmptyList

typedef RzList*(* RzBufferNonEmptyList) (RzBuffer *b)

Definition at line 29 of file rz_buf.h.

◆ RzBufferRead

typedef st64(* RzBufferRead) (RzBuffer *b, ut8 *buf, ut64 len)

Definition at line 22 of file rz_buf.h.

◆ RzBufferResize

typedef bool(* RzBufferResize) (RzBuffer *b, ut64 newsize)

Definition at line 25 of file rz_buf.h.

◆ RzBufferSeek

typedef st64(* RzBufferSeek) (RzBuffer *b, st64 addr, int whence)

Definition at line 26 of file rz_buf.h.

◆ RzBufferSparseChunk

◆ RzBufferWrite

typedef st64(* RzBufferWrite) (RzBuffer *b, const ut8 *buf, ut64 len)

Definition at line 23 of file rz_buf.h.

Enumeration Type Documentation

◆ RzBufferSparseWriteMode

Enumerator
RZ_BUF_SPARSE_WRITE_MODE_SPARSE 

all writes are performed in the sparse overlay

RZ_BUF_SPARSE_WRITE_MODE_THROUGH 

all writes are performed in the underlying base buffer

Definition at line 59 of file rz_buf.h.

59  {
RzBufferSparseWriteMode
Definition: rz_buf.h:59
@ RZ_BUF_SPARSE_WRITE_MODE_SPARSE
all writes are performed in the sparse overlay
Definition: rz_buf.h:60
@ RZ_BUF_SPARSE_WRITE_MODE_THROUGH
all writes are performed in the underlying base buffer
Definition: rz_buf.h:61

Function Documentation

◆ 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
uint8_t ut8
Definition: lh5801.h:11
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#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 }
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 r
Definition: crypto_rc6.c:12
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define RZ_NEWS(x, y)
Definition: rz_types.h:283
#define st64
Definition: rz_types_base.h:10

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
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
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
voidpf void * buf
Definition: ioapi.h:138
#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 }
RZ_API bool rz_buf_resize(RZ_NONNULL RzBuffer *b, ut64 newsize)
Resize the buffer size.
Definition: buf.c:890
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225

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 }
int n
Definition: mipsasm.c:19

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 }
#define NULL
Definition: cris-opc.c:27

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
static void buf_whole_buf_free(RzBuffer *b)
Definition: buf.c:29

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
char * dst
Definition: lz4.h:724
#define UT64_MAX
Definition: rz_types_base.h:86

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
static int buf_size
Definition: debug_qnx.c:35
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 }
lzma_index * src
Definition: index.h:567

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
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

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 }
size_t len
Definition: 6502dis.c:15
static RzBuffer * new_buffer(RzBufferType type, void *user)
Definition: buf.c:244
@ RZ_BUFFER_BYTES
Definition: buf.c:13
#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 }
@ RZ_BUFFER_FILE
Definition: buf.c:10
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 }
@ RZ_BUFFER_MMAP
Definition: buf.c:14
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 }
@ RZ_BUFFER_REF
Definition: buf.c:16
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 }
@ RZ_BUFFER_SPARSE
Definition: buf.c:15

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 }
@ RZ_BUFFER_IO
Definition: buf.c:12

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 }
@ RZ_BUFFER_IO_FD
Definition: buf.c:11
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_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 }
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
#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
uint32_t ut32
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_sleb128_at()

static st64 rz_buf_sleb128_at ( RzBuffer b,
ut64  addr,
st64 v 
)
inlinestatic

Definition at line 146 of file rz_buf.h.

146  {
148  return rz_buf_sleb128(b, v);
149 }
const char * v
Definition: dsignal.c:12
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
RZ_API st64 rz_buf_sleb128(RZ_NONNULL RzBuffer *buffer, RZ_NONNULL st64 *value)
Decodes SLEB128 from RzBuffer.
Definition: buf.c:1384

References addr, b, rz_buf_seek(), RZ_BUF_SET, rz_buf_sleb128(), and v.

◆ rz_buf_sparse_get_chunks()

RZ_API const RzBufferSparseChunk* rz_buf_sparse_get_chunks ( RzBuffer b,
RZ_NONNULL size_t count 
)

Only for sparse RzBuffers, get all sparse data chunks currently populated.

Definition at line 313 of file buf_sparse.c.

313  {
315  if (b->methods != &buffer_sparse_methods) {
316  *count = 0;
317  return NULL;
318  }
319  SparsePriv *priv = get_priv_sparse(b);
320  *count = rz_vector_len(&priv->chunks);
321  return rz_vector_index_ptr(&priv->chunks, 0);
322 }
static const RzBufferMethods buffer_sparse_methods
Definition: buf_sparse.c:302
static struct buf_sparse_priv * get_priv_sparse(RzBuffer *b)
Definition: buf_sparse.c:117
static void * rz_vector_index_ptr(RzVector *vec, size_t index)
Definition: rz_vector.h:88
static size_t rz_vector_len(const RzVector *vec)
Definition: rz_vector.h:82
RzVector chunks
of RzBufferSparseChunk, non-overlapping, ordered by from addr
Definition: buf_sparse.c:14

References b, buffer_sparse_methods, buf_sparse_priv::chunks, count, get_priv_sparse(), NULL, rz_return_val_if_fail, rz_vector_index_ptr(), and rz_vector_len().

Referenced by __write(), and ihex_write().

◆ rz_buf_sparse_populated_in()

RZ_API bool rz_buf_sparse_populated_in ( RzBuffer b,
ut64  from,
ut64  to 
)
Parameters
frominclusive
toinclusive
Returns
whether the given interval contains chunks populated in the sparse buffer

Definition at line 339 of file buf_sparse.c.

339  {
340  rz_return_val_if_fail(b, false);
341  if (b->methods != &buffer_sparse_methods) {
342  return false;
343  }
344  SparsePriv *priv = get_priv_sparse(b);
345  size_t from_i = chunk_index_in(priv, from);
346  if (from_i) {
347  RzBufferSparseChunk *c = rz_vector_index_ptr(&priv->chunks, from_i - 1);
348  if (from <= c->to) {
349  return true;
350  }
351  }
352  size_t to_i = chunk_index_in(priv, to);
353  return to_i > from_i;
354 }
static size_t chunk_index_in(SparsePriv *priv, ut64 addr)
Definition: buf_sparse.c:41
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr socklen_t static fromlen const void const struct sockaddr to
Definition: sfsocketcall.h:125
#define c(i)
Definition: sha256.c:43

References b, buffer_sparse_methods, c, chunk_index_in(), buf_sparse_priv::chunks, from, get_priv_sparse(), rz_return_val_if_fail, rz_vector_index_ptr(), and to.

Referenced by rz_bin_relocs_patch_maps().

◆ rz_buf_sparse_set_write_mode()

RZ_API void rz_buf_sparse_set_write_mode ( RzBuffer b,
RzBufferSparseWriteMode  mode 
)

Only for sparse RzBuffers.

Definition at line 325 of file buf_sparse.c.

325  {
327  if (b->methods != &buffer_sparse_methods) {
328  return;
329  }
330  SparsePriv *priv = get_priv_sparse(b);
331  priv->write_mode = mode;
332 }
RzBufferSparseWriteMode write_mode
Definition: buf_sparse.c:16

References b, buffer_sparse_methods, get_priv_sparse(), rz_return_if_fail, and buf_sparse_priv::write_mode.

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

◆ 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_uleb128_at()

static st64 rz_buf_uleb128_at ( RzBuffer b,
ut64  addr,
ut64 v 
)
inlinestatic

Definition at line 142 of file rz_buf.h.

142  {
144  return rz_buf_uleb128(b, v);
145 }
RZ_API st64 rz_buf_uleb128(RZ_NONNULL RzBuffer *buffer, RZ_NONNULL ut64 *value)
Decodes ULEB128 from RzBuffer.
Definition: buf.c:1353

References addr, b, rz_buf_seek(), RZ_BUF_SET, rz_buf_uleb128(), and v.

◆ 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().

◆ rz_deflate_buf()

RZ_API bool rz_deflate_buf ( RZ_NONNULL RzBuffer src,
RZ_NONNULL RzBuffer dst,
ut64  block_size,
ut8 src_consumed 
)

deflate uncompressed data in RzBbuffer to zlib or gzipped, use MAX_WBITS as the window size logarithm.

See also
rz_deflatew_buf()

Definition at line 350 of file compression.c.

350  {
351  rz_return_val_if_fail(src && dst, false);
352  rz_return_val_if_fail(block_size > 0, false);
353  return rz_deflatew_buf(src, dst, block_size, src_consumed, MAX_WBITS + 16);
354 }
RZ_API bool rz_deflatew_buf(RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed, int wbits)
Definition: compression.c:327
#define MAX_WBITS
Definition: flirt.c:105

References dst, MAX_WBITS, rz_deflatew_buf(), rz_return_val_if_fail, and src.

Referenced by rz_file_deflate().

◆ rz_deflatew_buf()

RZ_API bool rz_deflatew_buf ( RZ_NONNULL RzBuffer src,
RZ_NONNULL RzBuffer dst,
ut64  block_size,
ut8 src_consumed,
int  wbits 
)

Definition at line 327 of file compression.c.

327  {
328  return false;
329 }

Referenced by rz_deflate_buf(), and rz_sign_flirt_write_compressed_pattern_to_buffer().

◆ rz_inflate_buf()

RZ_API bool rz_inflate_buf ( RZ_NONNULL RzBuffer src,
RZ_NONNULL RzBuffer dst,
ut64  block_size,
ut8 src_consumed 
)

inflate compressed data in RzBbuffer, use MAX_WBITS as the window size logarithm.

See also
rz_inflatew_buf()

Definition at line 360 of file compression.c.

360  {
361  rz_return_val_if_fail(src && dst, false);
362  rz_return_val_if_fail(block_size > 0, false);
363  return rz_inflatew_buf(src, dst, block_size, src_consumed, MAX_WBITS + 32);
364 }
RZ_API bool rz_inflatew_buf(RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed, int wbits)
Definition: compression.c:331

References dst, MAX_WBITS, rz_inflatew_buf(), rz_return_val_if_fail, and src.

Referenced by rz_file_inflate().

◆ rz_inflatew_buf()

RZ_API bool rz_inflatew_buf ( RZ_NONNULL RzBuffer src,
RZ_NONNULL RzBuffer dst,
ut64  block_size,
ut8 src_consumed,
int  wbits 
)

Definition at line 331 of file compression.c.

331  {
332  return false;
333 }

Referenced by rz_inflate_buf().

◆ rz_lzma_dec_buf()

RZ_API bool rz_lzma_dec_buf ( RZ_NONNULL RzBuffer src,
RZ_NONNULL RzBuffer dst,
ut64  block_size,
ut8 src_consumed 
)

Decompress the src buffer with LZMA algorithm and put the decompressed data in dst.

Parameters
srcWhere to read the compressed data from
dstWhere to write the decompressed data to
block_sizeDecompression can happen block after block. Specify the size of the block here.
Returns
true if decompression was successful, false otherwise

Definition at line 462 of file compression.c.

462  {
463  return lzma_action_buf(src, dst, block_size, src_consumed, false);
464 }
static bool lzma_action_buf(RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed, bool encode)
Definition: compression.c:449

References dst, lzma_action_buf(), and src.

Referenced by get_gnu_debugdata_elf_symbols().

◆ rz_lzma_enc_buf()

RZ_API bool rz_lzma_enc_buf ( RZ_NONNULL RzBuffer src,
RZ_NONNULL RzBuffer dst,
ut64  block_size,
ut8 src_consumed 
)

Compress the src buffer with LZMA algorithm and put the compressed data in dst.

Parameters
srcWhere to read the decompressed data from
dstWhere to write the compressed data to
block_sizeCompression can happen block after block. Specify the size of the block here.
Returns
true if compression was successful, false otherwise

Definition at line 474 of file compression.c.

474  {
475  return lzma_action_buf(src, dst, block_size, src_consumed, true);
476 }

References dst, lzma_action_buf(), and src.

◆ rz_seek_offset()

static ut64 rz_seek_offset ( ut64  cur,
ut64  length,
st64  addr,
int  whence 
)
inlinestatic

change cur according to addr and whence (RZ_BUF_SET/RZ_BUF_CUR/RZ_BUF_END)

Definition at line 67 of file rz_buf.h.

67  {
68  switch (whence) {
69  case RZ_BUF_CUR:
70  return cur + (ut64)addr;
71  case RZ_BUF_SET:
72  return addr;
73  case RZ_BUF_END:
74  return length + (ut64)addr;
75  default:
77  return cur;
78  }
79 }
#define rz_warn_if_reached()
Definition: rz_assert.h:29

References addr, length, RZ_BUF_CUR, RZ_BUF_END, RZ_BUF_SET, rz_warn_if_reached, and ut64().

Referenced by buf_bytes_seek(), buf_io_seek(), and buf_seek().