2016-06-20 09:12:44 +02:00
|
|
|
/* radare - LGPL - Copyright 2008-2016 - pancake */
|
2009-02-05 22:08:46 +01:00
|
|
|
|
|
|
|
#include "r_io.h"
|
|
|
|
#include "r_lib.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2011-01-20 22:52:16 +01:00
|
|
|
typedef struct {
|
|
|
|
int fd;
|
|
|
|
ut8 *buf;
|
|
|
|
ut32 size;
|
2013-11-14 07:52:03 -05:00
|
|
|
ut64 offset;
|
2011-01-20 22:52:16 +01:00
|
|
|
} RIOMalloc;
|
2009-02-05 22:08:46 +01:00
|
|
|
|
2011-01-21 00:21:32 +01:00
|
|
|
#define RIOMALLOC_FD(x) (((RIOMalloc*)x->data)->fd)
|
|
|
|
#define RIOMALLOC_SZ(x) (((RIOMalloc*)x->data)->size)
|
|
|
|
#define RIOMALLOC_BUF(x) (((RIOMalloc*)x->data)->buf)
|
2013-11-14 07:52:03 -05:00
|
|
|
#define RIOMALLOC_OFF(x) (((RIOMalloc*)x->data)->offset)
|
2011-01-20 22:52:16 +01:00
|
|
|
|
2012-02-27 02:02:44 +01:00
|
|
|
static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
|
2016-06-20 09:12:44 +02:00
|
|
|
if (!fd || !buf || count < 0 || !fd->data) {
|
2011-01-20 22:52:16 +01:00
|
|
|
return -1;
|
2016-06-20 09:12:44 +02:00
|
|
|
}
|
|
|
|
if (RIOMALLOC_OFF (fd) > RIOMALLOC_SZ (fd)) {
|
2013-10-14 01:12:09 +02:00
|
|
|
return -1;
|
2016-06-20 09:12:44 +02:00
|
|
|
}
|
|
|
|
if (RIOMALLOC_OFF (fd) + count > RIOMALLOC_SZ (fd)) {
|
2013-11-14 07:52:03 -05:00
|
|
|
count -= (RIOMALLOC_OFF (fd) + count-(RIOMALLOC_SZ (fd)));
|
2016-06-20 09:12:44 +02:00
|
|
|
}
|
2013-11-14 07:52:03 -05:00
|
|
|
if (count > 0) {
|
|
|
|
memcpy (RIOMALLOC_BUF (fd) + RIOMALLOC_OFF (fd), buf, count);
|
|
|
|
RIOMALLOC_OFF (fd) += count;
|
2013-10-14 01:12:09 +02:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
return -1;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2016-06-29 11:35:16 +02:00
|
|
|
static bool __resize(RIO *io, RIODesc *fd, ut64 count) {
|
2014-03-24 22:00:26 -05:00
|
|
|
ut8 * new_buf = NULL;
|
2016-09-19 13:44:47 +01:00
|
|
|
if (!fd || !fd->data || count == 0) {
|
2016-06-29 11:35:16 +02:00
|
|
|
return false;
|
2016-06-20 09:12:44 +02:00
|
|
|
}
|
|
|
|
if (RIOMALLOC_OFF (fd) > RIOMALLOC_SZ (fd)) {
|
2016-06-29 11:35:16 +02:00
|
|
|
return false;
|
2016-06-20 09:12:44 +02:00
|
|
|
}
|
2014-03-24 22:00:26 -05:00
|
|
|
new_buf = malloc (count);
|
2014-06-10 11:50:11 +02:00
|
|
|
if (!new_buf) return -1;
|
2016-06-29 11:35:16 +02:00
|
|
|
memcpy (new_buf, RIOMALLOC_BUF (fd), R_MIN (count, RIOMALLOC_SZ (fd)));
|
|
|
|
if (count > RIOMALLOC_SZ (fd)) {
|
|
|
|
memset (new_buf + RIOMALLOC_SZ (fd), 0, count - RIOMALLOC_SZ (fd));
|
|
|
|
}
|
2014-03-24 22:00:26 -05:00
|
|
|
free (RIOMALLOC_BUF (fd));
|
|
|
|
RIOMALLOC_BUF (fd) = new_buf;
|
|
|
|
RIOMALLOC_SZ (fd) = count;
|
2016-06-29 11:35:16 +02:00
|
|
|
return true;
|
2014-03-24 22:00:26 -05:00
|
|
|
}
|
|
|
|
|
2012-02-27 02:02:44 +01:00
|
|
|
static int __read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
|
|
|
|
memset (buf, 0xff, count);
|
2016-06-20 09:12:44 +02:00
|
|
|
if (!fd || !fd->data) {
|
2011-01-20 22:52:16 +01:00
|
|
|
return -1;
|
2016-06-20 09:12:44 +02:00
|
|
|
}
|
|
|
|
if (RIOMALLOC_OFF (fd) > RIOMALLOC_SZ (fd)) {
|
2011-01-20 22:52:16 +01:00
|
|
|
return -1;
|
2016-06-20 09:12:44 +02:00
|
|
|
}
|
|
|
|
if (RIOMALLOC_OFF (fd) + count >= RIOMALLOC_SZ (fd)) {
|
2013-11-14 07:52:03 -05:00
|
|
|
count = RIOMALLOC_SZ (fd) - RIOMALLOC_OFF (fd);
|
2016-06-20 09:12:44 +02:00
|
|
|
}
|
2013-11-14 07:52:03 -05:00
|
|
|
memcpy (buf, RIOMALLOC_BUF (fd) + RIOMALLOC_OFF (fd), count);
|
2010-06-13 12:24:07 +02:00
|
|
|
return count;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2011-01-20 22:52:16 +01:00
|
|
|
static int __close(RIODesc *fd) {
|
2011-01-21 00:21:32 +01:00
|
|
|
RIOMalloc *riom;
|
2016-06-20 09:12:44 +02:00
|
|
|
if (!fd || !fd->data) {
|
2009-02-05 22:08:46 +01:00
|
|
|
return -1;
|
2016-06-20 09:12:44 +02:00
|
|
|
}
|
2011-01-21 00:21:32 +01:00
|
|
|
riom = fd->data;
|
|
|
|
free (riom->buf);
|
|
|
|
riom->buf = NULL;
|
2011-01-20 22:52:16 +01:00
|
|
|
free (fd->data);
|
|
|
|
fd->data = NULL;
|
|
|
|
fd->state = R_IO_DESC_TYPE_CLOSED;
|
2009-02-05 22:08:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-21 15:40:10 +01:00
|
|
|
static ut64 __lseek(RIO* io, RIODesc *fd, ut64 offset, int whence) {
|
2013-11-14 07:52:03 -05:00
|
|
|
ut64 r_offset = offset;
|
2016-06-20 09:12:44 +02:00
|
|
|
if (!fd || !fd->data) {
|
2014-01-21 15:40:10 +01:00
|
|
|
return offset;
|
2016-06-20 09:12:44 +02:00
|
|
|
}
|
2010-06-13 12:24:07 +02:00
|
|
|
switch (whence) {
|
2013-11-14 07:52:03 -05:00
|
|
|
case SEEK_SET:
|
|
|
|
r_offset = (offset <= RIOMALLOC_SZ (fd)) ? offset : RIOMALLOC_SZ (fd);
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
r_offset = (RIOMALLOC_OFF (fd) + offset <= RIOMALLOC_SZ (fd)) ? RIOMALLOC_OFF (fd) + offset : RIOMALLOC_SZ (fd);
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
r_offset = RIOMALLOC_SZ (fd);
|
|
|
|
break;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
2013-11-14 07:52:03 -05:00
|
|
|
RIOMALLOC_OFF (fd) = r_offset;
|
|
|
|
return RIOMALLOC_OFF (fd);
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 09:22:34 +02:00
|
|
|
static bool __check(RIO *io, const char *pathname, bool many) {
|
2016-06-29 11:35:16 +02:00
|
|
|
return (!strncmp (pathname, "malloc://", 9)) || (!strncmp (pathname, "hex://", 6));
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2013-11-23 02:44:06 +01:00
|
|
|
static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) {
|
2016-06-20 09:22:34 +02:00
|
|
|
if (__check (io, pathname, 0)) {
|
2016-05-24 21:22:15 +01:00
|
|
|
RIOMalloc *mal = R_NEW0 (RIOMalloc);
|
|
|
|
if (!mal) return NULL;
|
2016-07-03 22:36:42 +02:00
|
|
|
rw = 7; // RWX
|
2013-06-07 10:26:37 +02:00
|
|
|
mal->fd = -2; /* causes r_io_desc_new() to set the correct fd */
|
2015-03-16 01:19:25 +01:00
|
|
|
if (!strncmp (pathname, "hex://", 6)) {
|
2011-10-09 19:45:34 +02:00
|
|
|
mal->size = strlen (pathname);
|
2016-06-20 09:12:44 +02:00
|
|
|
mal->buf = malloc (mal->size + 1);
|
2016-05-24 21:22:15 +01:00
|
|
|
if (!mal->buf) {
|
|
|
|
free (mal);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-11-14 07:52:03 -05:00
|
|
|
mal->offset = 0;
|
2011-10-09 19:45:34 +02:00
|
|
|
memset (mal->buf, 0, mal->size);
|
2016-06-20 09:12:44 +02:00
|
|
|
mal->size = r_hex_str2bin (pathname + 6, mal->buf);
|
2016-06-29 11:35:16 +02:00
|
|
|
if ((int)mal->size < 1) {
|
2016-06-20 09:12:44 +02:00
|
|
|
R_FREE (mal->buf);
|
2013-12-29 02:47:46 +01:00
|
|
|
}
|
2011-10-09 19:45:34 +02:00
|
|
|
} else {
|
2016-06-20 09:12:44 +02:00
|
|
|
mal->size = r_num_math (NULL, pathname + 9);
|
2015-09-04 15:12:42 +02:00
|
|
|
if (((int)mal->size) <= 0) {
|
|
|
|
free (mal);
|
2016-07-03 22:36:42 +02:00
|
|
|
eprintf ("Cannot allocate (%s) 0 bytes\n", pathname + 9);
|
2012-02-03 20:52:20 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-09-04 15:12:42 +02:00
|
|
|
mal->offset = 0;
|
2016-06-20 09:12:44 +02:00
|
|
|
mal->buf = calloc (1, mal->size + 1);
|
2011-01-20 22:52:16 +01:00
|
|
|
}
|
2016-06-20 09:12:44 +02:00
|
|
|
if (mal->buf) {
|
2013-11-23 02:44:06 +01:00
|
|
|
RETURN_IO_DESC_NEW (&r_io_plugin_malloc,
|
2016-07-03 22:36:42 +02:00
|
|
|
mal->fd, pathname, rw, mode, mal);
|
2013-11-14 23:22:56 +01:00
|
|
|
}
|
2016-07-03 22:36:42 +02:00
|
|
|
eprintf ("Cannot allocate (%s) %d bytes\n", pathname + 9, mal->size);
|
2011-01-20 22:52:16 +01:00
|
|
|
free (mal);
|
|
|
|
}
|
|
|
|
return NULL;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 09:22:34 +02:00
|
|
|
RIOPlugin r_io_plugin_malloc = {
|
2010-02-23 00:26:13 +01:00
|
|
|
.name = "malloc",
|
2013-12-10 04:19:04 +01:00
|
|
|
.desc = "memory allocation (malloc://1024 hex://cd8090)",
|
|
|
|
.license = "LGPL3",
|
2013-11-14 07:52:03 -05:00
|
|
|
.open = __open,
|
|
|
|
.close = __close,
|
2009-02-05 22:08:46 +01:00
|
|
|
.read = __read,
|
2016-06-20 09:12:44 +02:00
|
|
|
.check = __check,
|
2009-02-05 22:08:46 +01:00
|
|
|
.lseek = __lseek,
|
|
|
|
.write = __write,
|
2014-03-24 22:00:26 -05:00
|
|
|
.resize = __resize,
|
2009-02-05 22:08:46 +01:00
|
|
|
};
|
|
|
|
|
2009-08-22 03:11:33 +00:00
|
|
|
#ifndef CORELIB
|
2016-11-14 04:24:50 +01:00
|
|
|
RLibStruct radare_plugin = {
|
2009-02-05 22:08:46 +01:00
|
|
|
.type = R_LIB_TYPE_IO,
|
2015-07-12 16:04:10 +02:00
|
|
|
.data = &r_io_plugin_malloc,
|
|
|
|
.version = R2_VERSION
|
2009-02-05 22:08:46 +01:00
|
|
|
};
|
2009-08-22 03:11:33 +00:00
|
|
|
#endif
|