Rizin
unix-like reverse engineering framework and cli tools
io_sparse.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015-2016 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "rz_io.h"
5 #include "rz_lib.h"
6 #include "rz_util.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 
11 typedef struct {
12  int fd;
15 } RzIOSparse;
16 
17 #define RzIOSPARSE_FD(x) (((RzIOSparse *)(x)->data)->fd)
18 #define RzIOSPARSE_BUF(x) (((RzIOSparse *)(x)->data)->buf)
19 #define RzIOSPARSE_OFF(x) (((RzIOSparse *)(x)->data)->offset)
20 
21 static int __write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count) {
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 }
35 
36 static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count) {
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 }
50 
51 static int __close(RzIODesc *fd) {
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 }
61 
62 static ut64 __lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence) {
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 }
74 
75 static bool __plugin_open(struct rz_io_t *io, const char *pathname, bool many) {
76  return (!strncmp(pathname, "sparse://", 9));
77 }
78 
79 static RzIODesc *__open(RzIO *io, const char *pathname, int rw, int mode) {
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 }
109 
111  .name = "sparse",
112  .desc = "Sparse buffer allocation plugin",
113  .uris = "sparse://",
114  .license = "LGPL3",
115  .open = __open,
116  .close = __close,
117  .read = __read,
118  .check = __plugin_open,
119  .lseek = __lseek,
120  .write = __write,
121  .resize = NULL,
122 };
123 
124 #ifndef RZ_PLUGIN_INCORE
126  .type = RZ_LIB_TYPE_IO,
127  .data = &rz_io_plugin_sparse,
129 };
130 #endif
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#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
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
static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count)
Definition: io_sparse.c:36
RzIOPlugin rz_io_plugin_sparse
Definition: io_sparse.c:110
static RzIODesc * __open(RzIO *io, const char *pathname, int rw, int mode)
Definition: io_sparse.c:79
#define RzIOSPARSE_OFF(x)
Definition: io_sparse.c:19
RZ_API RzLibStruct rizin_plugin
Definition: io_sparse.c:125
static int __write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
Definition: io_sparse.c:21
#define RzIOSPARSE_BUF(x)
Definition: io_sparse.c:18
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
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
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_seek(RZ_NONNULL RzBuffer *b, st64 addr, int whence)
Modify the current cursor position in the buffer.
Definition: buf.c:1166
#define RZ_BUF_CUR
Definition: rz_buf.h:15
RZ_API st64 rz_buf_write_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL const ut8 *buf, ut64 len)
Write len bytes of the buffer at the specified address.
Definition: buf.c:1197
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136
RZ_API 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_LIB_TYPE_IO
Definition: rz_lib.h:69
RZ_API ut64 rz_num_math(RzNum *num, const char *str)
Definition: unum.c:456
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_FREE(x)
Definition: rz_types.h:369
#define RZ_VERSION
Definition: rz_version.h:8
static int
Definition: sfsocketcall.h:114
#define b(i)
Definition: sha256.c:42
ut64 offset
Definition: io_sparse.c:14
RzBuffer * buf
Definition: io_sparse.c:13
const char * name
Definition: rz_io.h:115
const char * version
Definition: rz_io.h:117
Definition: rz_io.h:59
int Oxff
Definition: rz_io.h:65
static struct @626 mal
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997