Rizin
unix-like reverse engineering framework and cli tools
io_sparse.c File Reference
#include "rz_io.h"
#include "rz_lib.h"
#include "rz_util.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

Go to the source code of this file.

Classes

struct  RzIOSparse
 

Macros

#define RzIOSPARSE_FD(x)   (((RzIOSparse *)(x)->data)->fd)
 
#define RzIOSPARSE_BUF(x)   (((RzIOSparse *)(x)->data)->buf)
 
#define RzIOSPARSE_OFF(x)   (((RzIOSparse *)(x)->data)->offset)
 

Functions

static int __write (RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
 
static int __read (RzIO *io, RzIODesc *fd, ut8 *buf, int count)
 
static int __close (RzIODesc *fd)
 
static ut64 __lseek (RzIO *io, RzIODesc *fd, ut64 offset, int whence)
 
static bool __plugin_open (struct rz_io_t *io, const char *pathname, bool many)
 
static RzIODesc__open (RzIO *io, const char *pathname, int rw, int mode)
 

Variables

RzIOPlugin rz_io_plugin_sparse
 
RZ_API RzLibStruct rizin_plugin
 

Macro Definition Documentation

◆ RzIOSPARSE_BUF

#define RzIOSPARSE_BUF (   x)    (((RzIOSparse *)(x)->data)->buf)

Definition at line 18 of file io_sparse.c.

◆ RzIOSPARSE_FD

#define RzIOSPARSE_FD (   x)    (((RzIOSparse *)(x)->data)->fd)

Definition at line 17 of file io_sparse.c.

◆ RzIOSPARSE_OFF

#define RzIOSPARSE_OFF (   x)    (((RzIOSparse *)(x)->data)->offset)

Definition at line 19 of file io_sparse.c.

Function Documentation

◆ __close()

static int __close ( RzIODesc fd)
static

Definition at line 51 of file io_sparse.c.

51  {
52  RzIOSparse *riom;
53  if (!fd || !fd->data) {
54  return -1;
55  }
56  riom = fd->data;
57  RZ_FREE(riom->buf);
58  RZ_FREE(fd->data);
59  return 0;
60 }
#define RZ_FREE(x)
Definition: rz_types.h:369
RzBuffer * buf
Definition: io_sparse.c:13
static const z80_opcode fd[]
Definition: z80_tab.h:997

References RzIOSparse::buf, fd, and RZ_FREE.

◆ __lseek()

static ut64 __lseek ( RzIO io,
RzIODesc fd,
ut64  offset,
int  whence 
)
static

Definition at line 62 of file io_sparse.c.

62  {
63  RzBuffer *b;
64  ut64 rz_offset = offset;
65  if (!fd->data) {
66  return offset;
67  }
68  b = RzIOSPARSE_BUF(fd);
69  rz_offset = rz_buf_seek(b, offset, whence);
70  // if (rz_offset != UT64_MAX)
71  RzIOSPARSE_OFF(fd) = rz_offset;
72  return rz_offset;
73 }
#define RzIOSPARSE_OFF(x)
Definition: io_sparse.c:19
#define RzIOSPARSE_BUF(x)
Definition: io_sparse.c:18
voidpf uLong offset
Definition: ioapi.h:144
RZ_API st64 rz_buf_seek(RZ_NONNULL RzBuffer *b, st64 addr, int whence)
Modify the current cursor position in the buffer.
Definition: buf.c:1166
#define b(i)
Definition: sha256.c:42
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References b, fd, rz_buf_seek(), RzIOSPARSE_BUF, RzIOSPARSE_OFF, and ut64().

◆ __open()

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

Definition at line 79 of file io_sparse.c.

79  {
80  if (__plugin_open(io, pathname, 0)) {
82  int size = (int)rz_num_math(NULL, pathname + 9);
83  mal->buf = rz_buf_new_sparse(io->Oxff);
84  if (!mal->buf) {
85  free(mal);
86  return NULL;
87  }
88  if (size > 0) {
89  ut8 *data = malloc(size);
90  if (!data) {
91  eprintf("Cannot allocate (%s) %d byte(s)\n",
92  pathname + 9, size);
93  mal->offset = 0;
94  } else {
95  memset(data, 0x00, size);
96  rz_buf_write_at(mal->buf, 0, data, size);
97  free(data);
98  }
99  }
100  if (mal->buf) {
102  pathname, rw, mode, mal);
103  }
104  rz_buf_free(mal->buf);
105  free(mal);
106  }
107  return NULL;
108 }
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static bool __plugin_open(struct rz_io_t *io, const char *pathname, bool many)
Definition: io_sparse.c:75
RzIOPlugin rz_io_plugin_sparse
Definition: io_sparse.c:110
voidpf void uLong size
Definition: ioapi.h:138
const char int mode
Definition: ioapi.h:137
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
void * malloc(size_t size)
Definition: malloc.c:123
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
#define eprintf(x, y...)
Definition: rlcc.c:7
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 void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_API RZ_OWN RzBuffer * rz_buf_new_sparse(ut8 Oxff)
Creates a sparse buffer.
Definition: buf.c:408
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
RZ_API ut64 rz_num_math(RzNum *num, const char *str)
Definition: unum.c:456
#define RZ_NEW0(x)
Definition: rz_types.h:284
static int
Definition: sfsocketcall.h:114
int Oxff
Definition: rz_io.h:65
static struct @626 mal

References __plugin_open(), eprintf, free(), int, mal, malloc(), memset(), NULL, rz_io_t::Oxff, pathname, rz_buf_free(), rz_buf_new_sparse(), rz_buf_write_at(), rz_io_desc_new(), rz_io_plugin_sparse, RZ_NEW0, and rz_num_math().

◆ __plugin_open()

static bool __plugin_open ( struct rz_io_t io,
const char *  pathname,
bool  many 
)
static

Definition at line 75 of file io_sparse.c.

75  {
76  return (!strncmp(pathname, "sparse://", 9));
77 }

References pathname.

Referenced by __open().

◆ __read()

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

Definition at line 36 of file io_sparse.c.

36  {
37  ut64 o;
38  RzBuffer *b;
39  if (!fd || !fd->data) {
40  return -1;
41  }
42  b = RzIOSPARSE_BUF(fd);
43  o = RzIOSPARSE_OFF(fd);
44  int r = rz_buf_read_at(b, o, buf, count);
45  if (r >= 0) {
47  }
48  return count;
49 }
#define r
Definition: crypto_rc6.c:12
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
#define RZ_BUF_CUR
Definition: rz_buf.h:15
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 b, count, fd, r, RZ_BUF_CUR, rz_buf_read_at(), rz_buf_seek(), RzIOSPARSE_BUF, RzIOSPARSE_OFF, and ut64().

◆ __write()

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

Definition at line 21 of file io_sparse.c.

21  {
22  ut64 o;
23  RzBuffer *b;
24  if (!fd || !fd->data) {
25  return -1;
26  }
27  b = RzIOSPARSE_BUF(fd);
28  o = RzIOSPARSE_OFF(fd);
29  int r = rz_buf_write_at(b, o, buf, count);
30  if (r >= 0) {
32  }
33  return r;
34 }

References b, count, fd, r, RZ_BUF_CUR, rz_buf_seek(), rz_buf_write_at(), RzIOSPARSE_BUF, RzIOSPARSE_OFF, and ut64().

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_IO,
}
@ RZ_LIB_TYPE_IO
Definition: rz_lib.h:69
#define RZ_VERSION
Definition: rz_version.h:8
const char * version
Definition: rz_io.h:117

Definition at line 125 of file io_sparse.c.

◆ rz_io_plugin_sparse

RzIOPlugin rz_io_plugin_sparse
Initial value:
= {
.name = "sparse",
.desc = "Sparse buffer allocation plugin",
.uris = "sparse://",
.license = "LGPL3",
.open = __open,
.close = __close,
.read = __read,
.check = __plugin_open,
.lseek = __lseek,
.write = __write,
.resize = NULL,
}
static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count)
Definition: io_sparse.c:36
static RzIODesc * __open(RzIO *io, const char *pathname, int rw, int mode)
Definition: io_sparse.c:79
static int __write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
Definition: io_sparse.c:21
static ut64 __lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence)
Definition: io_sparse.c:62
static int __close(RzIODesc *fd)
Definition: io_sparse.c:51

Definition at line 110 of file io_sparse.c.

Referenced by __open().