Rizin
unix-like reverse engineering framework and cli tools
io.c File Reference
#include <rz_io.h>
#include <sdb.h>
#include <config.h>
#include "io_private.h"

Go to the source code of this file.

Macros

#define CMP(addr, part)
 
#define IO_EXTEND_BLOCK_SZ   256
 

Typedefs

typedef int(* cbOnIterMap) (RzIO *io, int fd, ut64 addr, ut8 *buf, int len, RzIOMap *map, void *user)
 

Functions

 RZ_LIB_VERSION (rz_io)
 
static int fd_read_at_wrap (RzIO *io, int fd, ut64 addr, ut8 *buf, int len, RzIOMap *map, void *user)
 
static int fd_write_at_wrap (RzIO *io, int fd, ut64 addr, ut8 *buf, int len, RzIOMap *map, void *user)
 
static st64 on_map_skyline (RzIO *io, ut64 vaddr, ut8 *buf, int len, int match_flg, cbOnIterMap op, bool prefix_mode)
 
RZ_API RzIOrz_io_new (void)
 
RZ_API RzIOrz_io_init (RzIO *io)
 
RZ_API void rz_io_free (RzIO *io)
 
RZ_API RzIODescrz_io_open_buffer (RzIO *io, RzBuffer *b, int perm, int mode)
 
RZ_API RzIODescrz_io_open_nomap (RzIO *io, const char *uri, int perm, int mode)
 
RZ_API RzIODescrz_io_open (RzIO *io, const char *uri, int perm, int mode)
 
RZ_API RzIODescrz_io_open_at (RzIO *io, const char *uri, int perm, int mode, ut64 at, RZ_NULLABLE RZ_OUT RzIOMap **map)
 Open a file and directly map it at the given offset. More...
 
RZ_API RzListrz_io_open_many (RzIO *io, const char *uri, int perm, int mode)
 
RZ_API bool rz_io_reopen (RzIO *io, int fd, int perm, int mode)
 
RZ_API int rz_io_close_all (RzIO *io)
 
RZ_API int rz_io_pread_at (RzIO *io, ut64 paddr, ut8 *buf, int len)
 
RZ_API int rz_io_pwrite_at (RzIO *io, ut64 paddr, const ut8 *buf, int len)
 
RZ_API bool rz_io_vread_at_mapped (RzIO *io, ut64 vaddr, ut8 *buf, int len)
 
static bool rz_io_vwrite_at (RzIO *io, ut64 vaddr, const ut8 *buf, int len)
 
RZ_API bool rz_io_read_at (RzIO *io, ut64 addr, ut8 *buf, int len)
 
RZ_API bool rz_io_read_at_mapped (RzIO *io, ut64 addr, ut8 *buf, int len)
 
RZ_API int rz_io_nread_at (RzIO *io, ut64 addr, ut8 *buf, int len)
 
RZ_API bool rz_io_write_at (RzIO *io, ut64 addr, const ut8 *buf, int len)
 
RZ_API bool rz_io_read (RzIO *io, ut8 *buf, int len)
 
RZ_API bool rz_io_write (RzIO *io, const ut8 *buf, int len)
 
RZ_API ut64 rz_io_size (RzIO *io)
 
RZ_API bool rz_io_is_listener (RzIO *io)
 
RZ_API char * rz_io_system (RzIO *io, const char *cmd)
 
RZ_API bool rz_io_resize (RzIO *io, ut64 newsize)
 
RZ_API bool rz_io_close (RzIO *io)
 
RZ_API bool rz_io_extend_at (RzIO *io, ut64 addr, ut64 size)
 Extend the RzIODesc at addr by inserting size 0 bytes. More...
 
RZ_API bool rz_io_set_write_mask (RzIO *io, const ut8 *mask, int len)
 Set a mask that is used on all following write operations. More...
 
RZ_API ut64 rz_io_p2v (RzIO *io, ut64 pa)
 
RZ_API ut64 rz_io_v2p (RzIO *io, ut64 va)
 
RZ_API void rz_io_bind (RzIO *io, RzIOBind *bnd)
 
RZ_API bool rz_io_shift (RzIO *io, ut64 start, ut64 end, st64 move)
 
RZ_API ut64 rz_io_seek (RzIO *io, ut64 offset, int whence)
 
RZ_API int rz_io_fini (RzIO *io)
 

Macro Definition Documentation

◆ CMP

#define CMP (   addr,
  part 
)
Value:
((addr) < rz_itv_end(((RzSkylineItem *)(part))->itv) - 1 ? -1 : (addr) > rz_itv_end(((RzSkylineItem *)(part))->itv) - 1 ? 1 \
: 0)
static ut64 rz_itv_end(RzInterval itv)
Definition: rz_itv.h:42
static int addr
Definition: z80asm.c:58

◆ IO_EXTEND_BLOCK_SZ

#define IO_EXTEND_BLOCK_SZ   256

Typedef Documentation

◆ cbOnIterMap

typedef int(* cbOnIterMap) (RzIO *io, int fd, ut64 addr, ut8 *buf, int len, RzIOMap *map, void *user)

Definition at line 26 of file io.c.

Function Documentation

◆ fd_read_at_wrap()

static int fd_read_at_wrap ( RzIO io,
int  fd,
ut64  addr,
ut8 buf,
int  len,
RzIOMap map,
void *  user 
)
static

Definition at line 18 of file io.c.

18  {
19  return rz_io_fd_read_at(io, fd, addr, buf, len);
20 }
size_t len
Definition: 6502dis.c:15
voidpf void * buf
Definition: ioapi.h:138
RZ_API int rz_io_fd_read_at(RzIO *io, int fd, ut64 addr, ut8 *buf, int len)
Definition: io_fd.c:71
static const z80_opcode fd[]
Definition: z80_tab.h:997

References addr, fd, len, and rz_io_fd_read_at().

Referenced by rz_io_nread_at(), rz_io_read_at_mapped(), and rz_io_vread_at_mapped().

◆ fd_write_at_wrap()

static int fd_write_at_wrap ( RzIO io,
int  fd,
ut64  addr,
ut8 buf,
int  len,
RzIOMap map,
void *  user 
)
static

Definition at line 22 of file io.c.

22  {
23  return rz_io_fd_write_at(io, fd, addr, buf, len);
24 }
RZ_API int rz_io_fd_write_at(RzIO *io, int fd, ut64 addr, const ut8 *buf, int len)
Definition: io_fd.c:80

References addr, fd, len, and rz_io_fd_write_at().

Referenced by rz_io_vwrite_at().

◆ on_map_skyline()

static st64 on_map_skyline ( RzIO io,
ut64  vaddr,
ut8 buf,
int  len,
int  match_flg,
cbOnIterMap  op,
bool  prefix_mode 
)
static

Definition at line 30 of file io.c.

30  {
31  RzVector *skyline = &io->map_skyline.v;
32  ut64 addr = vaddr;
33  size_t i;
34  bool ret = true, wrap = !prefix_mode && vaddr + len < vaddr;
35 #define CMP(addr, part) ((addr) < rz_itv_end(((RzSkylineItem *)(part))->itv) - 1 ? -1 : (addr) > rz_itv_end(((RzSkylineItem *)(part))->itv) - 1 ? 1 \
36  : 0)
37  // Let i be the first skyline part whose right endpoint > addr
38  if (!len) {
39  i = rz_vector_len(skyline);
40  } else {
41  rz_vector_lower_bound(skyline, addr, i, CMP);
42  if (i == rz_vector_len(skyline) && wrap) {
43  wrap = false;
44  i = 0;
45  addr = 0;
46  }
47  }
48 #undef CMP
49  while (i < rz_vector_len(skyline)) {
50  const RzSkylineItem *part = rz_vector_index_ptr(skyline, i);
51  // Right endpoint <= addr
52  if (rz_itv_end(part->itv) - 1 < addr) {
53  i++;
54  if (wrap && i == rz_vector_len(skyline)) {
55  wrap = false;
56  i = 0;
57  addr = 0;
58  }
59  continue;
60  }
61  if (addr < part->itv.addr) {
62  // [addr, part->itv.addr) is a gap
63  if (prefix_mode || len <= part->itv.addr - vaddr) {
64  break;
65  }
66  addr = part->itv.addr;
67  }
68  // Now left endpoint <= addr < right endpoint
69  ut64 len1 = RZ_MIN(vaddr + len - addr, rz_itv_end(part->itv) - addr);
70  if (len1 < 1) {
71  break;
72  }
73  RzIOMap *map = part->user;
74  // The map satisfies the permission requirement or p_cache is enabled
75  if (((map->perm & match_flg) == match_flg || io->p_cache)) {
76  st64 result = op(io, map->fd, map->delta + addr - map->itv.addr,
77  buf + (addr - vaddr), len1, map, NULL);
78  if (prefix_mode) {
79  if (result < 0) {
80  return result;
81  }
82  addr += result;
83  if (result != len1) {
84  break;
85  }
86  } else {
87  if (result != len1) {
88  ret = false;
89  }
90  addr += len1;
91  }
92  } else if (prefix_mode) {
93  break;
94  } else {
95  addr += len1;
96  ret = false;
97  }
98  // Reaches the end
99  if (addr == vaddr + len) {
100  break;
101  }
102  // Wrap to the beginning of skyline if address wraps
103  if (!addr) {
104  i = 0;
105  }
106  }
107  return prefix_mode ? addr - vaddr : ret;
108 }
ut8 op
Definition: 6502dis.c:13
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
size_t map(int syms, int left, int len)
Definition: enough.c:237
#define CMP(addr, part)
#define RZ_MIN(x, y)
#define st64
Definition: rz_types_base.h:10
#define rz_vector_lower_bound(vec, x, i, cmp)
Definition: rz_vector.h:190
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
ut64 addr
Definition: rz_itv.h:15
RzSkyline map_skyline
Definition: rz_io.h:74
int p_cache
Definition: rz_io.h:71
RzInterval itv
Definition: rz_skyline.h:8
RzVector v
Definition: rz_skyline.h:13
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References addr, rz_interval_t::addr, CMP, i, rz_skyline_item_t::itv, len, map(), rz_io_t::map_skyline, NULL, op, rz_io_t::p_cache, rz_itv_end(), RZ_MIN, rz_vector_index_ptr(), rz_vector_len(), rz_vector_lower_bound, st64, rz_skyline_item_t::user, ut64(), and rz_skyline_t::v.

Referenced by rz_io_nread_at(), rz_io_read_at_mapped(), rz_io_vread_at_mapped(), and rz_io_vwrite_at().

◆ rz_io_bind()

RZ_API void rz_io_bind ( RzIO io,
RzIOBind bnd 
)

Definition at line 550 of file io.c.

550  {
551  rz_return_if_fail(io && bnd);
552 
553  bnd->io = io;
554  bnd->init = true;
556  bnd->desc_use = rz_io_use_fd;
557  bnd->desc_get = rz_io_desc_get;
558  bnd->desc_size = rz_io_desc_size;
559  bnd->p2v = rz_io_p2v;
560  bnd->v2p = rz_io_v2p;
561  bnd->open = rz_io_open_nomap;
562  bnd->open_at = rz_io_open_at;
563  bnd->close = rz_io_fd_close;
564  bnd->read_at = rz_io_read_at;
565  bnd->write_at = rz_io_write_at;
566  bnd->system = rz_io_system;
567  bnd->fd_open = rz_io_fd_open;
568  bnd->fd_close = rz_io_fd_close;
569  bnd->fd_seek = rz_io_fd_seek;
570  bnd->fd_size = rz_io_fd_size;
571  bnd->fd_resize = rz_io_fd_resize;
572  bnd->fd_read = rz_io_fd_read;
573  bnd->fd_write = rz_io_fd_write;
576  bnd->fd_is_dbg = rz_io_fd_is_dbg;
582  bnd->map_get = rz_io_map_get;
585  bnd->map_add = rz_io_map_add;
586 #if HAVE_PTRACE
587  bnd->ptrace = rz_io_ptrace;
588  bnd->ptrace_func = rz_io_ptrace_func;
589 #endif
590 #if __WINDOWS__
591  bnd->get_w32dbg_wrap = rz_io_get_w32dbg_wrap;
592 #endif
593 }
RZ_API char * rz_io_system(RzIO *io, const char *cmd)
Definition: io.c:411
RZ_API RzIODesc * rz_io_open_nomap(RzIO *io, const char *uri, int perm, int mode)
Definition: io.c:145
RZ_API bool rz_io_read_at(RzIO *io, ut64 addr, ut8 *buf, int len)
Definition: io.c:300
RZ_API ut64 rz_io_v2p(RzIO *io, ut64 va)
Definition: io.c:541
RZ_API RzIODesc * rz_io_open_at(RzIO *io, const char *uri, int perm, int mode, ut64 at, RZ_NULLABLE RZ_OUT RzIOMap **map)
Open a file and directly map it at the given offset.
Definition: io.c:177
RZ_API ut64 rz_io_p2v(RzIO *io, ut64 pa)
Definition: io.c:533
RZ_API bool rz_io_write_at(RzIO *io, ut64 addr, const ut8 *buf, int len)
Definition: io.c:358
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
RZ_API bool rz_io_fd_resize(RzIO *io, int fd, ut64 newsize)
Definition: io_fd.c:58
RZ_API bool rz_io_fd_is_dbg(RzIO *io, int fd)
Definition: io_fd.c:86
RZ_API ut64 rz_io_fd_size(RzIO *io, int fd)
Definition: io_fd.c:42
RZ_API const char * rz_io_fd_get_name(RzIO *io, int fd)
Definition: io_fd.c:112
RZ_API bool rz_io_addr_is_mapped(RzIO *io, ut64 vaddr)
Definition: ioutils.c:11
RZ_API int rz_io_fd_open(RzIO *io, const char *uri, int flags, int mode)
Definition: io_fd.c:6
RZ_API bool rz_io_map_remap_fd(RzIO *io, int fd, ut64 addr)
Definition: io_map.c:69
RZ_API ut64 rz_io_desc_size(RzIODesc *desc)
Definition: io_desc.c:224
RZ_API ut8 * rz_io_fd_get_buf(RzIO *io, int fd, RZ_OUT RZ_NONNULL ut64 *size)
Returns the underlying buffer of the file descriptor.
Definition: io_fd.c:53
RZ_API int rz_io_fd_read(RzIO *io, int fd, ut8 *buf, int len)
Definition: io_fd.c:16
RZ_API RzIOMap * rz_io_map_get_paddr(RzIO *io, ut64 paddr)
Definition: io_map.c:163
RZ_API RzIOMap * rz_io_map_get(RzIO *io, ut64 addr)
Definition: io_map.c:176
RZ_API RzIODesc * rz_io_desc_get(RzIO *io, int fd)
Definition: io_desc.c:73
RZ_API RzList * rz_io_map_get_for_fd(RzIO *io, int fd)
Definition: io_map.c:388
RZ_API bool rz_io_is_valid_offset(RzIO *io, ut64 offset, int hasperm)
Definition: ioutils.c:20
RZ_API int rz_io_fd_write(RzIO *io, int fd, const ut8 *buf, int len)
Definition: io_fd.c:26
RZ_API int rz_io_fd_get_current(RzIO *io)
Definition: io_fd.c:135
RZ_API ut64 rz_io_fd_seek(RzIO *io, int fd, ut64 addr, int whence)
Definition: io_fd.c:35
RZ_API bool rz_io_use_fd(RzIO *io, int fd)
Definition: io_fd.c:118
RZ_API RzIOMap * rz_io_map_add(RzIO *io, int fd, int flags, ut64 delta, ut64 addr, ut64 size)
Definition: io_map.c:151
RZ_API bool rz_io_fd_close(RzIO *io, int fd)
Definition: io_fd.c:11
RzIOFdResize fd_resize
Definition: rz_io.h:247
RzIOOpen open
Definition: rz_io.h:237
RzIOReadAt read_at
Definition: rz_io.h:240
RzIOIsValidOff is_valid_offset
Definition: rz_io.h:257
RzIOFdOpen fd_open
Definition: rz_io.h:243
RzIOClose close
Definition: rz_io.h:239
RzIOV2P v2p
Definition: rz_io.h:262
RzIOMapGet map_get
Definition: rz_io.h:259
RzIOP2V p2v
Definition: rz_io.h:263
RzIODescSize desc_size
Definition: rz_io.h:236
RzIOAddrIsMapped addr_is_mapped
Definition: rz_io.h:258
RzIOSystem system
Definition: rz_io.h:242
RzIOMapAdd map_add
Definition: rz_io.h:261
RzIODescUse desc_use
Definition: rz_io.h:234
RzIOOpenAt open_at
Definition: rz_io.h:238
RzIOGetCurrentFd fd_get_current
Definition: rz_io.h:233
RzIOFdSeek fd_seek
Definition: rz_io.h:245
RzIOWriteAt write_at
Definition: rz_io.h:241
RzIOFdGetBuf fd_getbuf
Definition: rz_io.h:256
RzIO * io
Definition: rz_io.h:232
RzIOFdWrite fd_write
Definition: rz_io.h:249
RzIOFdGetMap fd_get_map
Definition: rz_io.h:254
RzIOFdIsDbg fd_is_dbg
Definition: rz_io.h:252
RzIOFdReadAt fd_read_at
Definition: rz_io.h:250
RzIOFdClose fd_close
Definition: rz_io.h:244
RzIOFdGetName fd_get_name
Definition: rz_io.h:253
RzIOFdRemap fd_remap
Definition: rz_io.h:255
RzIOFdWriteAt fd_write_at
Definition: rz_io.h:251
RzIOFdSize fd_size
Definition: rz_io.h:246
RzIOFdRead fd_read
Definition: rz_io.h:248
RzIOMapGetPaddr map_get_paddr
Definition: rz_io.h:260
int init
Definition: rz_io.h:231
RzIODescGet desc_get
Definition: rz_io.h:235

References rz_io_bind_t::addr_is_mapped, rz_io_bind_t::close, rz_io_bind_t::desc_get, rz_io_bind_t::desc_size, rz_io_bind_t::desc_use, rz_io_bind_t::fd_close, rz_io_bind_t::fd_get_current, rz_io_bind_t::fd_get_map, rz_io_bind_t::fd_get_name, rz_io_bind_t::fd_getbuf, rz_io_bind_t::fd_is_dbg, rz_io_bind_t::fd_open, rz_io_bind_t::fd_read, rz_io_bind_t::fd_read_at, rz_io_bind_t::fd_remap, rz_io_bind_t::fd_resize, rz_io_bind_t::fd_seek, rz_io_bind_t::fd_size, rz_io_bind_t::fd_write, rz_io_bind_t::fd_write_at, rz_io_bind_t::init, rz_io_bind_t::io, rz_io_bind_t::is_valid_offset, rz_io_bind_t::map_add, rz_io_bind_t::map_get, rz_io_bind_t::map_get_paddr, rz_io_bind_t::open, rz_io_bind_t::open_at, rz_io_bind_t::p2v, rz_io_bind_t::read_at, rz_io_addr_is_mapped(), rz_io_desc_get(), rz_io_desc_size(), rz_io_fd_close(), rz_io_fd_get_buf(), rz_io_fd_get_current(), rz_io_fd_get_name(), rz_io_fd_is_dbg(), rz_io_fd_open(), rz_io_fd_read(), rz_io_fd_read_at(), rz_io_fd_resize(), rz_io_fd_seek(), rz_io_fd_size(), rz_io_fd_write(), rz_io_fd_write_at(), rz_io_is_valid_offset(), rz_io_map_add(), rz_io_map_get(), rz_io_map_get_for_fd(), rz_io_map_get_paddr(), rz_io_map_remap_fd(), rz_io_open_at(), rz_io_open_nomap(), rz_io_p2v(), rz_io_read_at(), rz_io_system(), rz_io_use_fd(), rz_io_v2p(), rz_io_write_at(), rz_return_if_fail, rz_io_bind_t::system, rz_io_bind_t::v2p, and rz_io_bind_t::write_at.

Referenced by __open(), download_pdb(), rz_core_file_reopen(), rz_core_init(), rz_diff_file_open(), and rzfind_open_file().

◆ rz_io_close()

RZ_API bool rz_io_close ( RzIO io)

Definition at line 441 of file io.c.

441  {
442  return io ? rz_io_desc_close(io->desc) : false;
443 }
#define false
RZ_API bool rz_io_desc_close(RzIODesc *desc)
Definition: io_desc.c:165
struct rz_io_desc_t * desc
Definition: rz_io.h:60

References rz_io_t::desc, and rz_io_desc_close().

◆ rz_io_close_all()

RZ_API int rz_io_close_all ( RzIO io)

Definition at line 258 of file io.c.

258  { // what about undo?
259  if (!io) {
260  return false;
261  }
262  rz_io_desc_fini(io);
263  rz_io_map_reset(io);
264  rz_io_desc_init(io);
265  rz_io_cache_fini(io);
266  return true;
267 }
RZ_IPI bool rz_io_desc_init(RzIO *io)
Definition: io_desc.c:368
RZ_API void rz_io_map_reset(RzIO *io)
Definition: io_map.c:186
RZ_API void rz_io_cache_fini(RzIO *io)
Definition: io_cache.c:28
RZ_IPI bool rz_io_desc_fini(RzIO *io)
Definition: io_desc.c:390

References rz_io_cache_fini(), rz_io_desc_fini(), rz_io_desc_init(), and rz_io_map_reset().

Referenced by __close_file_cb(), file_load(), rz_core_io_file_open(), and rz_open_close_all_handler().

◆ rz_io_extend_at()

RZ_API bool rz_io_extend_at ( RzIO io,
ut64  addr,
ut64  size 
)

Extend the RzIODesc at addr by inserting size 0 bytes.

Parameters
ioReference to RzIO instance
addrAddress where to insert new 0 bytes
sizeNumber of 0 bytes to insert
Returns
true if extend operation was successful, false otherwise

Definition at line 453 of file io.c.

453  {
454 #define IO_EXTEND_BLOCK_SZ 256
455  rz_return_val_if_fail(io, false);
456 
457  if (!io->desc || !io->desc->plugin) {
458  return false;
459  }
460  if (size == 0) {
461  return true;
462  }
463 
464  if ((io->desc->perm & RZ_PERM_RW) != RZ_PERM_RW) {
465  return false;
466  }
467  ut64 cur_size = rz_io_desc_size(io->desc);
468  if (addr > cur_size) {
469  return false;
470  }
471 
472  // Extend the file to include the additional <size> bytes
473  if (UT64_ADD_OVFCHK(cur_size, size)) {
474  return false;
475  }
476  if (!rz_io_resize(io, cur_size + size)) {
477  return false;
478  }
479 
480  // Shift old data to make space for the zero bytes
481  ut64 tmp = cur_size >= IO_EXTEND_BLOCK_SZ ? RZ_MAX(cur_size - IO_EXTEND_BLOCK_SZ, addr) : addr;
482  ut64 remaining = cur_size - addr;
483 
485  if (!buffer) {
486  return false;
487  }
488  while (remaining) {
490  rz_io_pwrite_at(io, tmp + size, buffer, sz);
491 
493  remaining = remaining > sz ? remaining - sz : 0;
494  }
495  free(buffer);
496 
497  // Put the zero bytes at the right place
498  ut8 *empty = RZ_NEWS0(ut8, size);
499  if (!empty) {
500  return false;
501  }
502  rz_io_pwrite_at(io, addr, empty, size);
503  free(empty);
504 
505  return true;
506 #undef IO_EXTEND_BLOCK_SZ
507 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API int rz_io_pwrite_at(RzIO *io, ut64 paddr, const ut8 *buf, int len)
Definition: io.c:277
#define IO_EXTEND_BLOCK_SZ
RZ_API bool rz_io_resize(RzIO *io, ut64 newsize)
Definition: io.c:418
RZ_API int rz_io_pread_at(RzIO *io, ut64 paddr, ut8 *buf, int len)
Definition: io.c:269
voidpf void uLong size
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define RZ_NEWS(x, y)
Definition: rz_types.h:283
#define RZ_PERM_RW
Definition: rz_types.h:96
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
#define RZ_MAX(x, y)
#define UT64_ADD_OVFCHK(x, y)
Definition: buffer.h:15
struct rz_io_plugin_t * plugin
Definition: rz_io.h:103
int perm
Definition: rz_io.h:97

References addr, rz_io_t::desc, free(), IO_EXTEND_BLOCK_SZ, rz_io_desc_t::perm, rz_io_desc_t::plugin, rz_io_desc_size(), rz_io_pread_at(), rz_io_pwrite_at(), rz_io_resize(), RZ_MAX, RZ_NEWS, RZ_NEWS0, RZ_PERM_RW, rz_return_val_if_fail, autogen_x86imm::tmp, ut64(), and UT64_ADD_OVFCHK.

Referenced by rz_core_extend_at().

◆ rz_io_fini()

RZ_API int rz_io_fini ( RzIO io)

Definition at line 724 of file io.c.

724  {
725  if (!io) {
726  return false;
727  }
729  rz_io_desc_fini(io);
730  rz_io_map_fini(io);
731  rz_list_free(io->plugins);
732  rz_io_cache_fini(io);
733  if (io->runprofile) {
734  RZ_FREE(io->runprofile);
735  }
736  rz_event_free(io->event);
737  free(io->envprofile);
738 #if RZ_IO_USE_PTRACE_WRAP
739  if (io->ptrace_wrap) {
740  ptrace_wrap_instance_stop(io->ptrace_wrap);
741  free(io->ptrace_wrap);
742  }
743 #endif
744 #if __WINDOWS__
745  w32dbg_wrap_free((W32DbgWInst *)io->priv_w32dbg_wrap);
746 #endif
747  return true;
748 }
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
void ptrace_wrap_instance_stop(ptrace_wrap_instance *inst)
Definition: ptrace_wrap.c:36
RZ_API void rz_event_free(RzEvent *ev)
Definition: event.c:37
RZ_API void rz_io_desc_cache_fini_all(RzIO *io)
Definition: p_cache.c:362
RZ_API void rz_io_map_fini(RzIO *io)
Definition: io_map.c:318
#define RZ_FREE(x)
Definition: rz_types.h:369
char * runprofile
Definition: rz_io.h:81
RzEvent * event
Definition: rz_io.h:90
char * envprofile
Definition: rz_io.h:82
RzList * plugins
Definition: rz_io.h:80
void w32dbg_wrap_free(W32DbgWInst *inst)
Definition: w32dbg_wrap.c:58

References rz_io_t::envprofile, rz_io_t::event, free(), rz_io_t::plugins, ptrace_wrap_instance_stop(), rz_io_t::runprofile, rz_event_free(), RZ_FREE, rz_io_cache_fini(), rz_io_desc_cache_fini_all(), rz_io_desc_fini(), rz_io_map_fini(), rz_list_free(), and w32dbg_wrap_free().

Referenced by rz_io_free().

◆ rz_io_free()

RZ_API void rz_io_free ( RzIO io)

Definition at line 126 of file io.c.

126  {
127  if (io) {
128  rz_io_fini(io);
129  free(io);
130  }
131 }
RZ_API int rz_io_fini(RzIO *io)
Definition: io.c:724

References free(), and rz_io_fini().

Referenced by download_pdb(), hash_context_run(), rz_core_fini(), rz_diff_io_close(), rz_diff_io_open(), and rzfind_open_file().

◆ rz_io_init()

RZ_API RzIO* rz_io_init ( RzIO io)

Definition at line 114 of file io.c.

114  {
116  io->addrbytes = 1;
117  rz_io_desc_init(io);
119  rz_io_map_init(io);
120  rz_io_cache_init(io);
121  rz_io_plugin_init(io);
122  io->event = rz_event_new(io);
123  return io;
124 }
RZ_API RzEvent * rz_event_new(void *user)
Definition: event.c:17
RZ_API bool rz_io_plugin_init(RzIO *io)
Definition: io_plugin.c:32
RZ_API void rz_io_cache_init(RzIO *io)
Definition: io_cache.c:21
RZ_API void rz_io_map_init(RzIO *io)
Definition: io_map.c:101
static void rz_skyline_init(RzSkyline *skyline)
Definition: rz_skyline.h:19
size_t addrbytes
Definition: rz_io.h:66

References rz_io_t::addrbytes, rz_io_t::event, rz_io_t::map_skyline, NULL, rz_event_new(), rz_io_cache_init(), rz_io_desc_init(), rz_io_map_init(), rz_io_plugin_init(), rz_return_val_if_fail, and rz_skyline_init().

Referenced by rz_io_new().

◆ rz_io_is_listener()

RZ_API bool rz_io_is_listener ( RzIO io)

Definition at line 404 of file io.c.

404  {
405  if (io && io->desc && io->desc->plugin && io->desc->plugin->listener) {
406  return io->desc->plugin->listener(io->desc);
407  }
408  return false;
409 }
int(* listener)(RzIODesc *io)
Definition: rz_io.h:122

References rz_io_t::desc, rz_io_plugin_t::listener, and rz_io_desc_t::plugin.

Referenced by rz_core_file_open(), and rz_core_rtr_rap_run().

◆ rz_io_new()

RZ_API RzIO* rz_io_new ( void  )

Definition at line 110 of file io.c.

110  {
111  return rz_io_init(RZ_NEW0(RzIO));
112 }
RZ_API RzIO * rz_io_init(RzIO *io)
Definition: io.c:114
#define RZ_NEW0(x)
Definition: rz_types.h:284
Definition: rz_io.h:59

References rz_io_init(), and RZ_NEW0.

Referenced by download_pdb(), hash_context_run(), rz_core_init(), rz_diff_io_open(), and rzfind_open_file().

◆ rz_io_nread_at()

RZ_API int rz_io_nread_at ( RzIO io,
ut64  addr,
ut8 buf,
int  len 
)

Definition at line 338 of file io.c.

338  {
339  int ret;
340  rz_return_val_if_fail(io && buf && len >= 0, -1);
341  if (len == 0) {
342  return 0;
343  }
344  if (io->va) {
345  if (io->ff) {
346  memset(buf, io->Oxff, len);
347  }
348  ret = on_map_skyline(io, addr, buf, len, RZ_PERM_R, fd_read_at_wrap, true);
349  } else {
350  ret = rz_io_pread_at(io, addr, buf, len);
351  }
352  if (ret > 0 && io->cached & RZ_PERM_R) {
353  (void)rz_io_cache_read(io, addr, buf, len);
354  }
355  return ret;
356 }
static int fd_read_at_wrap(RzIO *io, int fd, ut64 addr, ut8 *buf, int len, RzIOMap *map, void *user)
Definition: io.c:18
static st64 on_map_skyline(RzIO *io, ut64 vaddr, ut8 *buf, int len, int match_flg, cbOnIterMap op, bool prefix_mode)
Definition: io.c:30
return memset(p, 0, total)
RZ_API bool rz_io_cache_read(RzIO *io, ut64 addr, ut8 *buf, int len)
Definition: io_cache.c:131
#define RZ_PERM_R
Definition: rz_types.h:93
int ff
Definition: rz_io.h:64
int Oxff
Definition: rz_io.h:65
int cached
Definition: rz_io.h:69
int va
Definition: rz_io.h:63

References addr, rz_io_t::cached, fd_read_at_wrap(), rz_io_t::ff, len, memset(), on_map_skyline(), rz_io_t::Oxff, rz_io_cache_read(), rz_io_pread_at(), RZ_PERM_R, rz_return_val_if_fail, and rz_io_t::va.

Referenced by core_recover_golang_functions_go_1_16(), core_recover_golang_functions_go_1_18(), core_recover_golang_functions_go_1_2(), decode_from_table(), decode_ldr_set_addr(), GH(), go_offset(), op_at_phys(), read_at_kernel_virtual(), recover_string_at(), rizin_compare_words(), rz_cmd_cmp_hex_block_handler(), rz_cmd_cmp_hex_block_hexdiff_handler(), rz_cmd_cmp_hex_diff_lines_handler(), rz_cmd_heap_chunks_print_handler(), rz_core_analysis_recover_golang_functions(), rz_core_analysis_resolve_golang_strings(), rz_core_cmp_disasm(), rz_core_cmp_mem_data(), rz_core_cmp_mem_mem(), rz_core_cmpwatch_add(), rz_core_cmpwatch_update(), rz_core_transform_op(), rz_core_write_duplicate_at(), rz_heap_tcache_content(), and type_match().

◆ rz_io_open()

RZ_API RzIODesc* rz_io_open ( RzIO io,
const char *  uri,
int  perm,
int  mode 
)

Definition at line 156 of file io.c.

156  {
158  RzIODesc *desc = rz_io_open_nomap(io, uri, perm, mode);
159  if (!desc) {
160  return NULL;
161  }
162  rz_io_map_new(io, desc->fd, desc->perm, 0LL, 0LL, rz_io_desc_size(desc));
163  return desc;
164 }
const char * desc
Definition: bin_vsf.c:19
const char int mode
Definition: ioapi.h:137
RZ_API RzIOMap * rz_io_map_new(RzIO *io, int fd, int flags, ut64 delta, ut64 addr, ut64 size)
Definition: io_map.c:50

References desc, NULL, rz_io_desc_size(), rz_io_map_new(), rz_io_open_nomap(), and rz_return_val_if_fail.

Referenced by rz_open_binary_file_handler().

◆ rz_io_open_at()

RZ_API RzIODesc* rz_io_open_at ( RzIO io,
const char *  uri,
int  perm,
int  mode,
ut64  at,
RZ_NULLABLE RZ_OUT RzIOMap **  map 
)

Open a file and directly map it at the given offset.

This executes both rz_io_open_nomap() and rz_io_map_new() and returns their results without updating the skyline.

Parameters
urifile uri to open
atwhere to map the file
mapoptionally returns the created RzIOMap
Returns
the opened RzIODesc of the file

Definition at line 177 of file io.c.

177  {
178  rz_return_val_if_fail(io && uri, NULL);
179 
180  RzIODesc *desc = rz_io_open_nomap(io, uri, perm, mode);
181  if (!desc) {
182  return NULL;
183  }
185  // second map
186  if (size && ((UT64_MAX - size + 1) < at)) {
187  // split map into 2 maps if only 1 big map results into interger overflow
188  io_map_new(io, desc->fd, desc->perm, UT64_MAX - at + 1, 0LL, size - (UT64_MAX - at) - 1);
189  // someone pls take a look at this confusing stuff
190  size = UT64_MAX - at + 1;
191  }
192  // skyline not updated
193  RzIOMap *m = rz_io_map_new(io, desc->fd, desc->perm, 0LL, at, size);
194  if (map) {
195  *map = m;
196  }
197  return desc;
198 }
RzIOMap * io_map_new(RzIO *io, int fd, int perm, ut64 delta, ut64 addr, ut64 size)
Definition: io_map.c:24
#define UT64_MAX
Definition: rz_types_base.h:86

References desc, io_map_new(), regress::m, map(), NULL, rz_io_desc_size(), rz_io_map_new(), rz_io_open_nomap(), rz_return_val_if_fail, ut64(), and UT64_MAX.

Referenced by io_create_mem_map(), open_nobin_file(), and rz_io_bind().

◆ rz_io_open_buffer()

RZ_API RzIODesc* rz_io_open_buffer ( RzIO io,
RzBuffer b,
int  perm,
int  mode 
)

Definition at line 133 of file io.c.

133  {
134  ut64 bufSize = rz_buf_size(b);
135  char *uri = rz_str_newf("malloc://%" PFMT64d, bufSize);
136  RzIODesc *desc = rz_io_open_nomap(io, uri, perm, mode);
137  if (desc) {
138  const ut8 *tmp = rz_buf_data(b, &bufSize);
139  rz_io_desc_write(desc, tmp, bufSize);
140  }
141  free(uri);
142  return desc;
143 }
RZ_DEPRECATE RZ_API RZ_BORROW ut8 * rz_buf_data(RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut64 *size)
Return a borrowed array of bytes representing the buffer data.
Definition: buf.c:1287
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
RZ_API int rz_io_desc_write(RzIODesc *desc, const ut8 *buf, int count)
Definition: io_desc.c:183
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define PFMT64d
Definition: rz_types.h:394
#define b(i)
Definition: sha256.c:42

References b, desc, free(), PFMT64d, rz_buf_data(), rz_buf_size(), rz_io_desc_write(), rz_io_open_nomap(), rz_str_newf(), autogen_x86imm::tmp, and ut64().

Referenced by handle_tmp_desc(), and rz_core_cmd_subst_i().

◆ rz_io_open_many()

RZ_API RzList* rz_io_open_many ( RzIO io,
const char *  uri,
int  perm,
int  mode 
)

Definition at line 201 of file io.c.

201  {
202  RzList *desc_list;
203  RzListIter *iter;
204  RzIODesc *desc;
205  rz_return_val_if_fail(io && io->files && uri, NULL);
206  RzIOPlugin *plugin = rz_io_plugin_resolve(io, uri, 1);
207  if (!plugin || !plugin->open_many || !plugin->close) {
208  return NULL;
209  }
210  if (!(desc_list = plugin->open_many(io, uri, perm, mode))) {
211  return NULL;
212  }
213  rz_list_foreach (desc_list, iter, desc) {
214  if (desc) {
215  if (!desc->plugin) {
216  desc->plugin = plugin;
217  }
218  if (!desc->uri) {
219  desc->uri = strdup(uri);
220  }
221  // should autofd be honored here?
222  rz_io_desc_add(io, desc);
223  if (!io->desc) {
224  io->desc = desc;
225  }
226  }
227  }
228  return desc_list;
229 }
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
RZ_API bool rz_io_desc_add(RzIO *io, RzIODesc *desc)
Definition: io_desc.c:49
RZ_API RzIOPlugin * rz_io_plugin_resolve(RzIO *io, const char *filename, bool many)
Definition: io_plugin.c:54
int(* close)(RzIODesc *desc)
Definition: rz_io.h:132
RzList *(* open_many)(RzIO *io, const char *, int perm, int mode)
Definition: rz_io.h:128
RzIDStorage * files
Definition: rz_io.h:75

References rz_io_plugin_t::close, desc, rz_io_t::desc, rz_io_t::files, NULL, rz_io_plugin_t::open_many, rz_io_desc_add(), rz_io_plugin_resolve(), rz_return_val_if_fail, and strdup().

Referenced by rz_core_file_open_many().

◆ rz_io_open_nomap()

RZ_API RzIODesc* rz_io_open_nomap ( RzIO io,
const char *  uri,
int  perm,
int  mode 
)

Definition at line 145 of file io.c.

145  {
146  rz_return_val_if_fail(io && uri, NULL);
147  RzIODesc *desc = rz_io_desc_open(io, uri, perm, mode);
148  if ((io->autofd || !io->desc) && desc) {
149  io->desc = desc;
150  }
151  // set desc as current if autofd or io->desc==NULL
152  return desc;
153 }
RZ_API RzIODesc * rz_io_desc_open(RzIO *io, const char *uri, int flags, int mode)
Definition: io_desc.c:112
int autofd
Definition: rz_io.h:68

References rz_io_t::autofd, desc, rz_io_t::desc, NULL, rz_io_desc_open(), and rz_return_val_if_fail.

Referenced by add_map(), hash_context_create_desc_io_stdin(), hash_context_create_desc_io_string(), hash_context_run(), perform_mapped_file_yank(), reopen_in_malloc_cb(), rz_core_file_open(), rz_core_rtr_rap_run(), rz_diff_io_open(), rz_io_bind(), rz_io_open(), rz_io_open_at(), rz_io_open_buffer(), rz_io_reopen(), and rzfind_open_file().

◆ rz_io_p2v()

RZ_API ut64 rz_io_p2v ( RzIO io,
ut64  pa 
)

Definition at line 533 of file io.c.

533  {
534  RzIOMap *map = rz_io_map_get_paddr(io, pa);
535  if (map) {
536  return pa - map->delta + map->itv.addr;
537  }
538  return UT64_MAX;
539 }

References map(), rz_io_map_get_paddr(), and UT64_MAX.

Referenced by annotated_hexdump(), rz_cmd_help(), rz_io_bind(), and rz_print_hexdump_bits_handler().

◆ rz_io_pread_at()

RZ_API int rz_io_pread_at ( RzIO io,
ut64  paddr,
ut8 buf,
int  len 
)

◆ rz_io_pwrite_at()

RZ_API int rz_io_pwrite_at ( RzIO io,
ut64  paddr,
const ut8 buf,
int  len 
)

Definition at line 277 of file io.c.

277  {
278  rz_return_val_if_fail(io && buf && len > 0, -1);
279  return rz_io_desc_write_at(io->desc, paddr, buf, len);
280 }
RZ_API int rz_io_desc_write_at(RzIODesc *desc, ut64 addr, const ut8 *buf, int len)
Definition: io_desc.c:358

References rz_io_t::desc, len, rz_io_desc_write_at(), and rz_return_val_if_fail.

Referenced by __desc_cache_commit_cb(), hash_context_create_desc_io_stdin(), hash_context_create_desc_io_string(), rz_io_extend_at(), and rz_io_write_at().

◆ rz_io_read()

RZ_API bool rz_io_read ( RzIO io,
ut8 buf,
int  len 
)

Definition at line 383 of file io.c.

383  {
384  if (io && rz_io_read_at(io, io->off, buf, len)) {
385  io->off += len;
386  return true;
387  }
388  return false;
389 }
ut64 off
Definition: rz_io.h:61

References len, rz_io_t::off, and rz_io_read_at().

◆ rz_io_read_at()

RZ_API bool rz_io_read_at ( RzIO io,
ut64  addr,
ut8 buf,
int  len 
)

Definition at line 300 of file io.c.

300  {
301  rz_return_val_if_fail(io && buf && len >= 0, false);
302  if (len == 0) {
303  return false;
304  }
305  bool ret = (io->va)
307  : rz_io_pread_at(io, addr, buf, len) > 0;
308  if (io->cached & RZ_PERM_R) {
309  (void)rz_io_cache_read(io, addr, buf, len);
310  }
311  return ret;
312 }
RZ_API bool rz_io_vread_at_mapped(RzIO *io, ut64 vaddr, ut8 *buf, int len)
Definition: io.c:283

References addr, rz_io_t::cached, len, rz_io_cache_read(), rz_io_pread_at(), rz_io_vread_at_mapped(), RZ_PERM_R, rz_return_val_if_fail, and rz_io_t::va.

Referenced by __analysis_esil_function(), __core_cmd_search_asm_byteswap(), __core_cmd_search_asm_infinite(), _analysis_calls(), _cb_hit(), cmd_aea(), cmd_debug_backtrace(), cmd_p_minus_e(), cmd_print_bars(), cmd_print_pxA(), cmd_write_bits(), core_analysis_graph_construct_nodes(), core_walk_function_blocks(), disassembly_as_table(), do_analysis_search(), do_debug_trace_calls(), do_ref_search(), do_section_search(), do_string_search(), do_syscall_search(), ds_print_ptr(), ds_show_refs(), dump_maps(), esilbreak_mem_read(), func_walk_blocks(), get_ptr(), get_ptr_ble(), getpcfromstack(), graph_construct_nodes(), ioMemcpy(), is_string_at(), jemalloc_get_bins(), jemalloc_get_chunks(), jemalloc_print_narenas(), meta_string_8bit_add(), myregwrite(), num_callback(), objc_build_refs(), perform_mapped_file_yank(), prevop_addr(), print_cmd_analysis_after_traps_print(), print_double_linked_list_bin_graph(), print_double_linked_list_bin_simple(), print_format_values(), print_heap_chunk(), print_malloc_info(), print_rop(), propagate_types_among_used_variables(), readDword(), readQword(), rz_analysis_try_get_fcn(), rz_analysis_xrefs_from_list_handler(), rz_cmd_analysis(), rz_cmd_debug_step_prog_handler(), rz_cmd_disassemble_ropchain_handler(), rz_cmd_disassembly_basic_block_as_text_json_handler(), rz_cmd_disassembly_basic_block_handler(), rz_cmd_disassembly_function_handler(), rz_cmd_print(), rz_core_analysis_bytes(), rz_core_analysis_data(), rz_core_analysis_esil(), rz_core_analysis_esil_emulate(), rz_core_analysis_hasrefs_to_depth(), rz_core_analysis_hint_set_offset(), rz_core_analysis_name(), rz_core_analysis_op(), rz_core_analysis_read_at(), rz_core_analysis_rename(), rz_core_analysis_search(), rz_core_analysis_search_xrefs(), rz_core_arg_get(), rz_core_asm_back_disassemble(), rz_core_asm_back_disassemble_all(), rz_core_asm_bwdisassemble(), rz_core_asm_strsearch(), rz_core_bin_export_info(), rz_core_bin_whole_strings(), rz_core_block_read(), rz_core_debug_step_skip(), rz_core_disasm_instruction(), rz_core_dump(), rz_core_esil_step(), rz_core_link_stroff(), rz_core_op_analysis(), rz_core_op_str(), rz_core_print_disasm(), rz_core_print_disasm_all(), rz_core_print_dump_str(), rz_core_print_function_disasm_json(), rz_core_print_hexdump_byline_str(), rz_core_print_hexdump_diff_str(), rz_core_print_hexdump_or_hexdiff_str(), rz_core_rtr_gdb_cb(), rz_core_search_cb(), rz_core_search_prelude(), rz_core_search_rop(), rz_core_shift_block(), rz_core_syscall_as_string(), rz_core_visual_asm(), rz_core_yank(), rz_core_yank_string(), rz_heap_bin_content(), rz_heap_chunks_list(), rz_heap_fastbin_content(), rz_heap_update_main_arena(), rz_io_bind(), rz_io_cache_write(), rz_io_read(), rz_io_read_i(), rz_io_shift(), rz_open_malloc_handler(), rz_print_byte_bitstream_handler(), rz_print_hexdump_hexless_words_handler(), rz_write_from_io_xchg_handler(), search_hash(), search_similar_pattern_in(), step_until_inst(), step_until_optype(), stringAt(), tcache_chunk_size(), and tcache_read().

◆ rz_io_read_at_mapped()

RZ_API bool rz_io_read_at_mapped ( RzIO io,
ut64  addr,
ut8 buf,
int  len 
)

Definition at line 318 of file io.c.

318  {
319  bool ret;
320  rz_return_val_if_fail(io && buf, false);
321  if (io->ff) {
322  memset(buf, io->Oxff, len);
323  }
324  if (io->va) {
325  ret = on_map_skyline(io, addr, buf, len, RZ_PERM_R, fd_read_at_wrap, false);
326  } else {
327  ret = rz_io_pread_at(io, addr, buf, len) > 0;
328  }
329  if (io->cached & RZ_PERM_R) {
330  (void)rz_io_cache_read(io, addr, buf, len);
331  }
332  return ret;
333 }

References addr, rz_io_t::cached, fd_read_at_wrap(), rz_io_t::ff, len, memset(), on_map_skyline(), rz_io_t::Oxff, rz_io_cache_read(), rz_io_pread_at(), RZ_PERM_R, rz_return_val_if_fail, and rz_io_t::va.

Referenced by read_ahead(), rz_core_esil_step(), rz_core_search_value_in_range(), and rz_core_write_value_inc_at().

◆ rz_io_reopen()

RZ_API bool rz_io_reopen ( RzIO io,
int  fd,
int  perm,
int  mode 
)

Definition at line 231 of file io.c.

231  {
232  RzIODesc *old, *new;
233  char *uri;
234  if (!(old = rz_io_desc_get(io, fd))) {
235  return false;
236  }
237  // does this really work, or do we have to handler debuggers ugly
238  uri = old->referer ? old->referer : old->uri;
239 #if __WINDOWS__ // TODO: workaround, see https://github.com/rizinorg/rizin/issues/8840
240  if (old->plugin->close && old->plugin->close(old)) {
241  return false; // TODO: this is an unrecoverable scenario
242  }
243  if (!(new = rz_io_open_nomap(io, uri, perm, mode))) {
244  return false;
245  }
246  rz_io_desc_exchange(io, old->fd, new->fd);
247  rz_io_desc_del(io, old->fd);
248  return true;
249 #else
250  if (!(new = rz_io_open_nomap(io, uri, perm, mode))) {
251  return false;
252  }
253  rz_io_desc_exchange(io, old->fd, new->fd);
254  return rz_io_desc_close(old); // magic
255 #endif // __WINDOWS__
256 }
RZ_API bool rz_io_desc_exchange(RzIO *io, int fd, int fdx)
Definition: io_desc.c:275
RZ_API bool rz_io_desc_del(RzIO *io, int fd)
Definition: io_desc.c:61
int fd
Definition: rz_io.h:96
char * uri
Definition: rz_io.h:98
char * referer
Definition: rz_io.h:100

References rz_io_plugin_t::close, fd, rz_io_desc_t::fd, rz_io_desc_t::plugin, rz_io_desc_t::referer, rz_io_desc_close(), rz_io_desc_del(), rz_io_desc_exchange(), rz_io_desc_get(), rz_io_open_nomap(), and rz_io_desc_t::uri.

Referenced by rz_core_file_reopen_debug(), rz_core_io_file_open(), and rz_core_io_file_reopen().

◆ rz_io_resize()

RZ_API bool rz_io_resize ( RzIO io,
ut64  newsize 
)

Definition at line 418 of file io.c.

418  {
419  if (io) {
420  RzList *maps = rz_io_map_get_for_fd(io, io->desc->fd);
421  RzIOMap *current_map;
422  RzListIter *iter;
423  ut64 fd_size = rz_io_fd_size(io, io->desc->fd);
424  bool ret = rz_io_desc_resize(io->desc, newsize);
425  if (!ret) {
427  return false;
428  }
429  rz_list_foreach (maps, iter, current_map) {
430  // we just resize map of the same size of its fd
431  if (current_map->itv.size == fd_size) {
432  rz_io_map_resize(io, current_map->id, newsize);
433  }
434  }
436  return true;
437  }
438  return false;
439 }
static RzList * maps(RzBinFile *bf)
Definition: bin_bf.c:116
RZ_API bool rz_io_desc_resize(RzIODesc *desc, ut64 newsize)
Definition: io_desc.c:250
RZ_API bool rz_io_map_resize(RzIO *io, ut32 id, ut64 newsize)
Definition: io_map.c:403
ut64 size
Definition: rz_itv.h:16
ut32 id
Definition: rz_io.h:148
RzInterval itv
Definition: rz_io.h:149

References rz_io_t::desc, rz_io_desc_t::fd, rz_io_map_t::id, rz_io_map_t::itv, maps(), rz_io_desc_resize(), rz_io_fd_size(), rz_io_map_get_for_fd(), rz_io_map_resize(), rz_list_free(), rz_interval_t::size, and ut64().

Referenced by file_resize(), and rz_io_extend_at().

◆ rz_io_seek()

RZ_API ut64 rz_io_seek ( RzIO io,
ut64  offset,
int  whence 
)

Definition at line 632 of file io.c.

632  {
633  if (!io) {
634  return 0LL;
635  }
636  switch (whence) {
637  case RZ_IO_SEEK_SET:
638  io->off = offset;
639  break;
640  case RZ_IO_SEEK_CUR:
641  io->off += offset;
642  break;
643  case RZ_IO_SEEK_END:
644  default:
645  io->off = rz_io_desc_seek(io->desc, offset, whence);
646  break;
647  }
648  return io->off;
649 }
voidpf uLong offset
Definition: ioapi.h:144
#define RZ_IO_SEEK_CUR
Definition: rz_io.h:16
RZ_API ut64 rz_io_desc_seek(RzIODesc *desc, ut64 offset, int whence)
Definition: io_desc.c:217
#define RZ_IO_SEEK_SET
Definition: rz_io.h:15
#define RZ_IO_SEEK_END
Definition: rz_io.h:17

References rz_io_t::desc, rz_io_t::off, rz_io_desc_seek(), RZ_IO_SEEK_CUR, RZ_IO_SEEK_END, and RZ_IO_SEEK_SET.

Referenced by perform_mapped_file_yank(), rz_core_seek(), and rzfind_open_file().

◆ rz_io_set_write_mask()

RZ_API bool rz_io_set_write_mask ( RzIO io,
const ut8 mask,
int  len 
)

Set a mask that is used on all following write operations.

Parameters
ioReference to RzIo instance
maskMask to apply
lenNumber of bytes in the mask
Returns
true if the mask was correctly set, false otherwise

Definition at line 517 of file io.c.

517  {
518  rz_return_val_if_fail(io, false);
519  rz_return_val_if_fail(mask || len == 0, false);
520 
521  free(io->write_mask);
522  if (!mask) {
523  io->write_mask = NULL;
524  io->write_mask_len = 0;
525  return true;
526  }
527  io->write_mask = (ut8 *)malloc(len);
528  memcpy(io->write_mask, mask, len);
529  io->write_mask_len = len;
530  return true;
531 }
#define mask()
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
ut8 * write_mask
Definition: rz_io.h:78
int write_mask_len
Definition: rz_io.h:79

References free(), len, malloc(), mask, memcpy(), NULL, rz_return_val_if_fail, rz_io_t::write_mask, and rz_io_t::write_mask_len.

Referenced by rz_write_mask_reset_handler(), and rz_write_mask_set_handler().

◆ rz_io_shift()

RZ_API bool rz_io_shift ( RzIO io,
ut64  start,
ut64  end,
st64  move 
)

Definition at line 596 of file io.c.

596  {
597  ut8 *buf;
598  ut64 chunksize = 0x10000;
599  ut64 saved_off = io->off;
600  ut64 src, shiftsize = rz_num_abs(move);
601  if (!shiftsize || (end - start) <= shiftsize) {
602  return false;
603  }
604  ut64 rest = (end - start) - shiftsize;
605  if (!(buf = calloc(1, chunksize + 1))) {
606  return false;
607  }
608  if (move > 0) {
609  src = end - shiftsize;
610  } else {
611  src = start + shiftsize;
612  }
613  while (rest > 0) {
614  if (chunksize > rest) {
615  chunksize = rest;
616  }
617  if (move > 0) {
618  src -= chunksize;
619  }
621  rz_io_write_at(io, src + move, buf, chunksize);
622  if (move < 0) {
623  src += chunksize;
624  }
625  rest -= chunksize;
626  }
627  free(buf);
628  io->off = rz_io_desc_seek(io->desc, saved_off, RZ_IO_SEEK_SET);
629  return true;
630 }
lzma_index * src
Definition: index.h:567
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
#define chunksize
static st64 rz_num_abs(st64 num)
Definition: rz_num.h:108

References calloc(), chunksize, rz_io_t::desc, test_evm::end, free(), rz_io_t::off, rz_io_desc_seek(), rz_io_read_at(), RZ_IO_SEEK_SET, rz_io_write_at(), rz_num_abs(), src, start, and ut64().

Referenced by file_resize().

◆ rz_io_size()

RZ_API ut64 rz_io_size ( RzIO io)

Definition at line 399 of file io.c.

399  {
400  // TODO: rethink this, maybe not needed
401  return io ? rz_io_desc_size(io->desc) : 0LL;
402 }

References rz_io_t::desc, and rz_io_desc_size().

Referenced by basefind_create_pointer_map(), core_search_for_xrefs_in_boundaries(), flagbar_foreach(), perform_mapped_file_yank(), rz_basefind(), rz_core_get_boundaries_prot(), rz_core_shift_block(), rz_core_visual_title(), and rzfind_open_file().

◆ rz_io_system()

RZ_API char* rz_io_system ( RzIO io,
const char *  cmd 
)

Definition at line 411 of file io.c.

411  {
412  if (io && io->desc && io->desc->plugin && io->desc->plugin->system && RZ_STR_ISNOTEMPTY(cmd)) {
413  return io->desc->plugin->system(io, io->desc, cmd);
414  }
415  return NULL;
416 }
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 cmd
Definition: sflib.h:79
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
char *(* system)(RzIO *io, RzIODesc *fd, const char *)
Definition: rz_io.h:126

References cmd, rz_io_t::desc, NULL, rz_io_desc_t::plugin, RZ_STR_ISNOTEMPTY, and rz_io_plugin_t::system.

Referenced by __rap_continue(), __rap_reg_profile(), __rap_reg_read(), __rap_step(), cb_dbg_gdb_page_size(), cb_dbg_gdb_retries(), handle_ts_stmt(), rz_cmd_remote(), rz_core_cmd_subst_i(), rz_core_debug_attach(), rz_debug_select(), rz_io_bind(), and rz_io_system_run_oldhandler().

◆ rz_io_v2p()

RZ_API ut64 rz_io_v2p ( RzIO io,
ut64  va 
)

Definition at line 541 of file io.c.

541  {
542  RzIOMap *map = rz_io_map_get(io, va);
543  if (map) {
544  st64 delta = va - map->itv.addr;
545  return map->delta + delta;
546  }
547  return UT64_MAX;
548 }
static st64 delta
Definition: vmenus.c:2425

References delta, map(), rz_io_map_get(), st64, and UT64_MAX.

Referenced by add_new_bin_string(), add_new_func_symbol(), meta_string_guess_add(), rz_cmd_help(), rz_core_meta_pascal_string_add(), and rz_io_bind().

◆ rz_io_vread_at_mapped()

RZ_API bool rz_io_vread_at_mapped ( RzIO io,
ut64  vaddr,
ut8 buf,
int  len 
)

Definition at line 283 of file io.c.

283  {
284  rz_return_val_if_fail(io && buf && len > 0, false);
285  if (io->ff) {
286  memset(buf, io->Oxff, len);
287  }
288  return on_map_skyline(io, vaddr, buf, len, RZ_PERM_R, fd_read_at_wrap, false);
289 }

References fd_read_at_wrap(), rz_io_t::ff, len, memset(), on_map_skyline(), rz_io_t::Oxff, RZ_PERM_R, and rz_return_val_if_fail.

Referenced by rz_io_read_at().

◆ rz_io_vwrite_at()

static bool rz_io_vwrite_at ( RzIO io,
ut64  vaddr,
const ut8 buf,
int  len 
)
static

Definition at line 291 of file io.c.

291  {
292  return on_map_skyline(io, vaddr, (ut8 *)buf, len, RZ_PERM_W, fd_write_at_wrap, false);
293 }
static int fd_write_at_wrap(RzIO *io, int fd, ut64 addr, ut8 *buf, int len, RzIOMap *map, void *user)
Definition: io.c:22
#define RZ_PERM_W
Definition: rz_types.h:94

References fd_write_at_wrap(), len, on_map_skyline(), and RZ_PERM_W.

Referenced by rz_io_write_at().

◆ rz_io_write()

RZ_API bool rz_io_write ( RzIO io,
const ut8 buf,
int  len 
)

Definition at line 391 of file io.c.

391  {
392  if (io && buf && len > 0 && rz_io_write_at(io, io->off, buf, len)) {
393  io->off += len;
394  return true;
395  }
396  return false;
397 }

References len, rz_io_t::off, and rz_io_write_at().

◆ rz_io_write_at()

RZ_API bool rz_io_write_at ( RzIO io,
ut64  addr,
const ut8 buf,
int  len 
)

Definition at line 358 of file io.c.

358  {
359  int i;
360  bool ret = false;
361  ut8 *mybuf = (ut8 *)buf;
362  rz_return_val_if_fail(io && buf && len > 0, false);
363  if (io->write_mask) {
364  mybuf = rz_mem_dup((void *)buf, len);
365  for (i = 0; i < len; i++) {
366  // TODO: this needs some love because it is not optimal.
367  mybuf[i] &= io->write_mask[i % io->write_mask_len];
368  }
369  }
370  if (io->cached & RZ_PERM_W) {
371  ret = rz_io_cache_write(io, addr, mybuf, len);
372  } else if (io->va) {
373  ret = rz_io_vwrite_at(io, addr, mybuf, len);
374  } else {
375  ret = rz_io_pwrite_at(io, addr, mybuf, len) > 0;
376  }
377  if (buf != mybuf) {
378  free(mybuf);
379  }
380  return ret;
381 }
static bool rz_io_vwrite_at(RzIO *io, ut64 vaddr, const ut8 *buf, int len)
Definition: io.c:291
RZ_API bool rz_io_cache_write(RzIO *io, ut64 addr, const ut8 *buf, int len)
Definition: io_cache.c:93
RZ_API void * rz_mem_dup(const void *s, int l)
Definition: mem.c:319

References addr, rz_io_t::cached, free(), i, len, rz_io_cache_write(), rz_io_pwrite_at(), rz_io_vwrite_at(), rz_mem_dup(), RZ_PERM_W, rz_return_val_if_fail, rz_io_t::va, rz_io_t::write_mask, and rz_io_t::write_mask_len.

Referenced by ioMemcpy(), op_at_phys(), rz_cmd_debug_dml_handler(), rz_core_shift_block(), rz_core_visual_asm(), rz_core_visual_cmd(), rz_core_write_at(), rz_heap_write_heap_chunk(), rz_io_bind(), rz_io_cache_commit(), rz_io_cache_invalidate(), rz_io_shift(), rz_io_write(), rz_io_write_i(), rz_main_rizin(), rz_write_from_file_handler(), rz_write_from_io_xchg_handler(), rz_write_from_socket_handler(), and rzfind_open_file().

◆ RZ_LIB_VERSION()

RZ_LIB_VERSION ( rz_io  )