Rizin
unix-like reverse engineering framework and cli tools
io_malloc.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2008-2017 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "rz_lib.h"
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "../io_memory.h"
8 
9 static bool __check(RzIO *io, const char *pathname, bool many) {
10  return (!strncmp(pathname, "malloc://", 9)) || (!strncmp(pathname, "hex://", 6));
11 }
12 
13 static RzIODesc *__open(RzIO *io, const char *pathname, int rw, int mode) {
14  if (__check(io, pathname, 0)) {
16  if (!mal) {
17  return NULL;
18  }
19  if (!strncmp(pathname, "hex://", 6)) {
20  mal->size = strlen(pathname);
21  mal->buf = calloc(1, mal->size + 1);
22  if (!mal->buf) {
23  free(mal);
24  return NULL;
25  }
26  mal->offset = 0;
27  mal->size = rz_hex_str2bin(pathname + 6, mal->buf);
28  if ((int)mal->size < 1) {
29  RZ_FREE(mal->buf);
30  }
31  } else {
32  mal->size = rz_num_math(NULL, pathname + 9);
33  if (((int)mal->size) <= 0) {
34  free(mal);
35  eprintf("Cannot allocate (%s) 0 bytes\n", pathname + 9);
36  return NULL;
37  }
38  mal->offset = 0;
39  mal->buf = calloc(1, mal->size + 1);
40  }
41  if (mal->buf) {
43  }
44  eprintf("Cannot allocate (%s) %d byte(s)\n", pathname + 9, mal->size);
45  free(mal);
46  }
47  return NULL;
48 }
49 
51  .name = "malloc",
52  .desc = "Memory allocation plugin",
53  .uris = "malloc://,hex://",
54  .license = "LGPL3",
55  .open = __open,
56  .close = io_memory_close,
57  .read = io_memory_read,
58  .check = __check,
59  .lseek = io_memory_lseek,
60  .write = io_memory_write,
61  .resize = io_memory_resize,
62 };
63 
64 #ifndef RZ_PLUGIN_INCORE
67  .data = &rz_io_plugin_malloc,
69 };
70 #endif
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static bool __check(RzIO *io, const char *pathname, bool many)
Definition: io_malloc.c:9
static RzIODesc * __open(RzIO *io, const char *pathname, int rw, int mode)
Definition: io_malloc.c:13
RZ_API RzLibStruct rizin_plugin
Definition: io_malloc.c:65
RzIOPlugin rz_io_plugin_malloc
Definition: io_malloc.c:50
bool io_memory_resize(RzIO *io, RzIODesc *fd, ut64 count)
Definition: io_memory.c:74
int io_memory_read(RzIO *io, RzIODesc *fd, ut8 *buf, int count)
Definition: io_memory.c:97
int io_memory_write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
Definition: io_memory.c:56
ut64 io_memory_lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence)
Definition: io_memory.c:125
int io_memory_close(RzIODesc *fd)
Definition: io_memory.c:114
const char int mode
Definition: ioapi.h:137
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
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 int rz_hex_str2bin(const char *in, ut8 *out)
Convert an input string in into the binary form in out.
Definition: hex.c:444
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_PERM_RW
Definition: rz_types.h:96
#define RZ_FREE(x)
Definition: rz_types.h:369
#define RZ_VERSION
Definition: rz_version.h:8
const char * name
Definition: rz_io.h:115
const char * version
Definition: rz_io.h:117
Definition: rz_io.h:59
static struct @626 mal