Rizin
unix-like reverse engineering framework and cli tools
io_null.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2017 condret <condr3t@protonmail.com>
2 // SPDX-FileCopyrightText: 2017 pancake <pancake@nopcode.org>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_io.h>
6 #include <rz_lib.h>
7 
8 typedef struct {
11 } RzIONull;
12 
13 static int __write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count) {
14  RzIONull *null;
15  if (!fd || !fd->data || !buf) {
16  return -1;
17  }
18  null = (RzIONull *)fd->data;
19  if ((null->offset + count) > null->size) {
20  int ret = null->size - null->offset;
21  return ret;
22  }
23  null->offset += count;
24  return count;
25 }
26 
27 static bool __resize(RzIO *io, RzIODesc *fd, ut64 count) {
28  if (fd && fd->data) {
29  RzIONull *null = (RzIONull *)fd->data;
30  null->size = count;
31  if (null->offset >= count) {
32  if (count) {
33  null->offset = count - 1;
34  } else {
35  null->offset = 0LL;
36  }
37  }
38  return true;
39  }
40  return false;
41 }
42 
43 static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count) {
44  RzIONull *null;
45  if (!fd || !fd->data || !buf) {
46  return -1;
47  }
48  null = (RzIONull *)fd->data;
49  if ((null->offset + count) > null->size) {
50  int ret = null->size - null->offset;
51  memset(buf, 0x00, ret);
52  null->offset = null->size;
53  return ret;
54  }
55  memset(buf, 0x00, count);
56  null->offset += count;
57  return count;
58 }
59 
60 static int __close(RzIODesc *fd) {
61  RZ_FREE(fd->data);
62  return 0;
63 }
64 
65 static ut64 __lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence) {
66  RzIONull *null;
67  if (!fd || !fd->data) {
68  return offset;
69  }
70  null = (RzIONull *)fd->data;
71  switch (whence) {
72  case SEEK_SET:
73  if (offset >= null->size) {
74  return null->offset = null->size - 1;
75  }
76  return null->offset = offset;
77  case SEEK_CUR:
78  if ((null->offset + offset) >= null->size) {
79  return null->offset = null->size - 1;
80  }
81  return null->offset += offset;
82  case SEEK_END:
83  return null->offset = null->size - 1;
84  }
85  return offset;
86 }
87 
88 static bool __plugin_open(RzIO *io, const char *pathname, bool many) {
89  return (!strncmp(pathname, "null://", 7));
90 }
91 
92 static RzIODesc *__open(RzIO *io, const char *pathname, int rw, int mode) {
93  RzIONull *null;
94  if (__plugin_open(io, pathname, 0)) {
95  if (!strncmp(pathname, "null://", 7) && strlen(pathname + 7)) {
96  null = RZ_NEW0(RzIONull);
97  null->size = rz_num_math(NULL, pathname + 7) + 1; //???
98  null->offset = 0LL;
99  return rz_io_desc_new(io, &rz_io_plugin_null, pathname, rw, mode, null);
100  }
101  }
102  return NULL;
103 }
104 
106  .name = "null",
107  .desc = "Null plugin",
108  .license = "LGPL3",
109  .uris = "null://",
110  .open = __open,
111  .close = __close,
112  .read = __read,
113  .check = __plugin_open,
114  .lseek = __lseek,
115  .write = __write,
116  .resize = __resize,
117 };
118 
119 #ifndef RZ_PLUGIN_INCORE
121  .type = RZ_LIB_TYPE_IO,
122  .data = &rz_io_plugin_null,
124 };
125 #endif
#define RZ_API
#define NULL
Definition: cris-opc.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
static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count)
Definition: io_null.c:43
RzIOPlugin rz_io_plugin_null
Definition: io_null.c:105
static bool __plugin_open(RzIO *io, const char *pathname, bool many)
Definition: io_null.c:88
static RzIODesc * __open(RzIO *io, const char *pathname, int rw, int mode)
Definition: io_null.c:92
RZ_API RzLibStruct rizin_plugin
Definition: io_null.c:120
static int __write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
Definition: io_null.c:13
static ut64 __lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence)
Definition: io_null.c:65
static int __close(RzIODesc *fd)
Definition: io_null.c:60
static bool __resize(RzIO *io, RzIODesc *fd, ut64 count)
Definition: io_null.c:27
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)
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
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
ut64 offset
Definition: io_null.c:10
ut64 size
Definition: io_null.c:9
const char * name
Definition: rz_io.h:115
const char * version
Definition: rz_io.h:117
Definition: rz_io.h:59
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
#define SEEK_END
Definition: zip.c:84