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

Go to the source code of this file.

Classes

struct  VFileCtx
 

Macros

#define URI_SCHEME   "vfile://"
 
#define URI_SCHEME_LEN   8
 

Functions

static bool vf_check (RzIO *io, const char *pathname, bool many)
 
static RzBinVirtualFilefind_vfile (RzBinFile *bf, const char *name)
 
static RzIODescvf_open (RzIO *io, const char *pathname, int rw, int mode)
 
static int vf_write (RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
 
static int vf_read (RzIO *io, RzIODesc *fd, ut8 *buf, int count)
 
static int vf_close (RzIODesc *fd)
 
static ut64 vf_lseek (struct rz_io_t *io, RzIODesc *fd, ut64 offset, int whence)
 
static bool vf_resize (RzIO *io, RzIODesc *fd, ut64 size)
 
RZ_IPI void rz_core_vfile_bin_file_deleted (RzCore *core, RzBinFile *bf)
 

Variables

RzIOPlugin rz_core_io_plugin_vfile
 IO Plugin that opens RzBinVirtualFiles supplied by the plugin of an RzBinFile. More...
 
RZ_API RzLibStruct rizin_plugin
 

Macro Definition Documentation

◆ URI_SCHEME

#define URI_SCHEME   "vfile://"

Definition at line 6 of file cvfile.c.

◆ URI_SCHEME_LEN

#define URI_SCHEME_LEN   8

Definition at line 7 of file cvfile.c.

Function Documentation

◆ find_vfile()

static RzBinVirtualFile* find_vfile ( RzBinFile bf,
const char *  name 
)
static

Definition at line 22 of file cvfile.c.

22  {
23  if (!bf->o || !bf->o->vfiles) {
24  return NULL;
25  }
26  RzListIter *it;
27  RzBinVirtualFile *vfile;
28  rz_list_foreach (bf->o->vfiles, it, vfile) {
29  if (!strcmp(vfile->name, name)) {
30  return vfile;
31  }
32  }
33  return NULL;
34 }
#define NULL
Definition: cris-opc.c:27
Definition: z80asm.h:102
RzBinObject * o
Definition: rz_bin.h:305
RzList * vfiles
Definition: rz_bin.h:265
RZ_OWN RZ_NONNULL char * name
Definition: rz_bin.h:596

References rz_bin_virtual_file_t::name, NULL, rz_bin_file_t::o, and rz_bin_object_t::vfiles.

Referenced by vf_open().

◆ rz_core_vfile_bin_file_deleted()

RZ_IPI void rz_core_vfile_bin_file_deleted ( RzCore core,
RzBinFile bf 
)

Definition at line 156 of file cvfile.c.

156  {
157  // close all vfile descs that point into the binfile that is about to be closed
158  // This is strictly necessary because VFileCtx holds pointers into it!
159  RzList *descs = rz_id_storage_list(core->io->files);
160  if (!descs) {
161  return;
162  }
163  RzListIter *it;
164  RzIODesc *desc;
165  rz_list_foreach (descs, it, desc) {
166  if (strcmp(desc->plugin->name, rz_core_io_plugin_vfile.name)) {
167  continue;
168  }
169  VFileCtx *ctx = desc->data;
170  if (ctx->bf == bf) {
172  }
173  }
174  rz_list_free(descs);
175 }
const char * desc
Definition: bin_vsf.c:19
RzIOPlugin rz_core_io_plugin_vfile
IO Plugin that opens RzBinVirtualFiles supplied by the plugin of an RzBinFile.
Definition: cvfile.c:134
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
RZ_API RzList * rz_id_storage_list(RzIDStorage *s)
Definition: idpool.c:283
RZ_API bool rz_io_desc_close(RzIODesc *desc)
Definition: io_desc.c:165
Definition: cvfile.c:9
RzIO * io
Definition: rz_core.h:313
const char * name
Definition: rz_io.h:115
RzIDStorage * files
Definition: rz_io.h:75

References desc, rz_io_t::files, rz_core_t::io, rz_io_plugin_t::name, rz_core_io_plugin_vfile, rz_id_storage_list(), rz_io_desc_close(), and rz_list_free().

Referenced by ev_binfiledel_cb().

◆ vf_check()

static bool vf_check ( RzIO io,
const char *  pathname,
bool  many 
)
static

Definition at line 17 of file cvfile.c.

17  {
18  // be careful if changing this, vf_open relies on its behavior!
19  return !strncmp(pathname, URI_SCHEME, URI_SCHEME_LEN);
20 }
#define URI_SCHEME_LEN
Definition: cvfile.c:7
#define URI_SCHEME
Definition: cvfile.c:6
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char pathname
Definition: sflib.h:66

References pathname, URI_SCHEME, and URI_SCHEME_LEN.

Referenced by vf_open().

◆ vf_close()

static int vf_close ( RzIODesc fd)
static

Definition at line 94 of file cvfile.c.

94  {
95  rz_return_val_if_fail(fd && fd->data, -1);
96  VFileCtx *ctx = fd->data;
97  free(ctx);
98  fd->data = NULL;
99  return 0;
100 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
static const z80_opcode fd[]
Definition: z80_tab.h:997

References fd, free(), NULL, and rz_return_val_if_fail.

◆ vf_lseek()

static ut64 vf_lseek ( struct rz_io_t io,
RzIODesc fd,
ut64  offset,
int  whence 
)
static

Definition at line 102 of file cvfile.c.

102  {
103  rz_return_val_if_fail(fd && fd->data, UT64_MAX);
104  VFileCtx *ctx = fd->data;
105  // we store the offset ourselves instead of using the RzBuffer's because
106  // there might be multiple io files accessing the same vfile.
107  switch (whence) {
108  case SEEK_SET:
109  ctx->off = offset;
110  break;
111  case SEEK_CUR:
112  ctx->off += offset;
113  break;
114  case SEEK_END:
115  ctx->off = rz_buf_size(ctx->vfile->buf);
116  break;
117  }
118  return ctx->off;
119 }
voidpf uLong offset
Definition: ioapi.h:144
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
#define UT64_MAX
Definition: rz_types_base.h:86
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
#define SEEK_END
Definition: zip.c:84

References fd, rz_buf_size(), rz_return_val_if_fail, SEEK_CUR, SEEK_END, SEEK_SET, and UT64_MAX.

◆ vf_open()

static RzIODesc* vf_open ( RzIO io,
const char *  pathname,
int  rw,
int  mode 
)
static

Definition at line 36 of file cvfile.c.

36  {
37  if (!vf_check(io, pathname, false)) {
38  return NULL;
39  }
40  char *vfile_path = strdup(pathname + URI_SCHEME_LEN); // path like "<binfile id>/<filename>"
41  if (!vfile_path) {
42  return NULL;
43  }
44  RzIODesc *desc = NULL;
45  char *filename = strchr(vfile_path, '/');
46  if (!filename) {
47  RZ_LOG_ERROR("Invalid URI \"%s\", expected " URI_SCHEME "<fd>/<filename>\n", pathname);
48  goto beach;
49  }
50  *filename++ = '\0';
51  ut32 bfid = (ut32)strtoull(vfile_path, NULL, 0);
52  RzCore *core = io->corebind.core; // We are in the core already so this is fine.
53  RzBinFile *bf = rz_bin_file_find_by_id(core->bin, bfid);
54  if (!bf) {
55  RZ_LOG_ERROR("No bin file for id %" PFMT32u " from URI \"%s\"\n", bfid, pathname);
56  goto beach;
57  }
58  RzBinVirtualFile *vfile = find_vfile(bf, filename);
59  if (!vfile) {
60  RZ_LOG_ERROR("No virtual file called \"%s\" for bin file id %" PFMT32u " from URI \"%s\"\n", filename, bfid, pathname);
61  goto beach;
62  }
64  if (!ctx) {
65  goto beach;
66  }
67  ctx->bf = bf;
68  ctx->vfile = vfile;
69  ctx->off = 0;
71  if (!desc) {
72  free(ctx);
73  }
74 beach:
75  free(vfile_path);
76  return desc;
77 }
RZ_API RzBinFile * rz_bin_file_find_by_id(RzBin *bin, ut32 bf_id)
Definition: bfile.c:188
static bool vf_check(RzIO *io, const char *pathname, bool many)
Definition: cvfile.c:17
static RzBinVirtualFile * find_vfile(RzBinFile *bf, const char *name)
Definition: cvfile.c:22
uint32_t ut32
const char * filename
Definition: ioapi.h:137
const char int mode
Definition: ioapi.h:137
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 RzIODesc * rz_io_desc_new(RzIO *io, RzIOPlugin *plugin, const char *uri, int flags, int mode, void *data)
Definition: io_desc.c:11
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define PFMT32u
Definition: rz_types.h:409
#define RZ_NEW(x)
Definition: rz_types.h:285
XX curplugin == o->plugin.
Definition: rz_bin.h:298
void * core
Definition: rz_bind.h:31
RzBin * bin
Definition: rz_core.h:298
RzCoreBind corebind
Definition: rz_io.h:92

References rz_core_t::bin, rz_core_bind_t::core, rz_io_t::corebind, desc, find_vfile(), free(), NULL, pathname, PFMT32u, rz_bin_file_find_by_id(), rz_core_io_plugin_vfile, rz_io_desc_new(), RZ_LOG_ERROR, RZ_NEW, strdup(), URI_SCHEME, URI_SCHEME_LEN, and vf_check().

◆ vf_read()

static int vf_read ( RzIO io,
RzIODesc fd,
ut8 buf,
int  count 
)
static

Definition at line 88 of file cvfile.c.

88  {
89  rz_return_val_if_fail(fd && fd->data, -1);
90  VFileCtx *ctx = fd->data;
91  return rz_buf_read_at(ctx->vfile->buf, ctx->off, buf, count);
92 }
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
voidpf void * buf
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

References count, fd, rz_buf_read_at(), and rz_return_val_if_fail.

◆ vf_resize()

static bool vf_resize ( RzIO io,
RzIODesc fd,
ut64  size 
)
static

Definition at line 121 of file cvfile.c.

121  {
122  rz_return_val_if_fail(fd && fd->data, false);
123  VFileCtx *ctx = fd->data;
124  return rz_buf_resize(ctx->vfile->buf, size);
125 }
voidpf void uLong size
Definition: ioapi.h:138
RZ_API bool rz_buf_resize(RZ_NONNULL RzBuffer *b, ut64 newsize)
Resize the buffer size.
Definition: buf.c:890

References fd, rz_buf_resize(), and rz_return_val_if_fail.

◆ vf_write()

static int vf_write ( RzIO io,
RzIODesc fd,
const ut8 buf,
int  count 
)
static

Definition at line 79 of file cvfile.c.

79  {
80  rz_return_val_if_fail(fd && fd->data, -1);
81  if (!(fd->perm & RZ_PERM_W)) {
82  return -1;
83  }
84  VFileCtx *ctx = fd->data;
85  return rz_buf_write_at(ctx->vfile->buf, ctx->off, buf, count);
86 }
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
#define RZ_PERM_W
Definition: rz_types.h:94

References count, fd, rz_buf_write_at(), RZ_PERM_W, and rz_return_val_if_fail.

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_IO,
.data = &rz_io_plugin_vfile,
.version = RZ_VERSION
}
@ RZ_LIB_TYPE_IO
Definition: rz_lib.h:69
#define RZ_VERSION
Definition: rz_version.h:8

Definition at line 149 of file cvfile.c.

◆ rz_core_io_plugin_vfile

RZ_IPI RzIOPlugin rz_core_io_plugin_vfile
Initial value:
= {
.name = "vfile",
.desc = "Virtual Files provided by RzBin Files",
.uris = URI_SCHEME,
.license = "LGPL",
.open = vf_open,
.close = vf_close,
.read = vf_read,
.check = vf_check,
.lseek = vf_lseek,
.write = vf_write,
.resize = vf_resize
}
static int vf_read(RzIO *io, RzIODesc *fd, ut8 *buf, int count)
Definition: cvfile.c:88
static int vf_write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
Definition: cvfile.c:79
static bool vf_resize(RzIO *io, RzIODesc *fd, ut64 size)
Definition: cvfile.c:121
static int vf_close(RzIODesc *fd)
Definition: cvfile.c:94
static ut64 vf_lseek(struct rz_io_t *io, RzIODesc *fd, ut64 offset, int whence)
Definition: cvfile.c:102
static RzIODesc * vf_open(RzIO *io, const char *pathname, int rw, int mode)
Definition: cvfile.c:36

IO Plugin that opens RzBinVirtualFiles supplied by the plugin of an RzBinFile.

URIs look like vfile://1/decompressed_data_0 where 1 is the id of a loaded RzBinFile and decompressed_data_0 is the name of the RzBinVirtualFile inside this RzBinFile that provides the data.

Definition at line 134 of file cvfile.c.

Referenced by rz_core_init(), rz_core_vfile_bin_file_deleted(), and vf_open().