Initial implementation of io.map plugin

This commit is contained in:
pancake 2013-03-12 01:50:55 +01:00
parent dc7289e1f3
commit 8f4d23a5ce
11 changed files with 152 additions and 25 deletions

View File

@ -9,7 +9,7 @@
static int check(RBin *bin) {
ut8 *h, buf[4];
int off, ret = R_FALSE;
RMmap *m = r_file_mmap (bin->file, R_FALSE);
RMmap *m = r_file_mmap (bin->file, R_FALSE, 0);
if (!m || !m->buf) {
r_file_mmap_free (m);
return R_FALSE;

View File

@ -346,6 +346,7 @@ extern RIOPlugin r_io_plugin_bfdbg;
extern RIOPlugin r_io_plugin_w32;
extern RIOPlugin r_io_plugin_ewf;
extern RIOPlugin r_io_plugin_zip;
extern RIOPlugin r_io_plugin_mmap;
#endif
#endif

View File

@ -65,6 +65,7 @@ typedef struct r_mem_pool_factory_t {
typedef struct r_mmap_t {
ut8 *buf;
ut64 base;
int len;
int fd;
int rw;
@ -218,7 +219,9 @@ R_API RGraphNode* r_graph_pop(RGraph *t);
R_API int r_file_size(const char *str);
R_API char *r_file_root(const char *root, const char *path);
R_API boolt r_file_is_directory(const char *str);
R_API RMmap *r_file_mmap (const char *file, boolt rw);
R_API RMmap *r_file_mmap (const char *file, boolt rw, ut64 base);
R_API int r_file_mmap_read (const char *file, ut64 addr, ut8 *buf, int len);
R_API int r_file_mmap_write(const char *file, ut64 addr, const ut8 *buf, int len);
R_API void r_file_mmap_free (RMmap *m);
// TODO: find better names and write vapis

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2012 pancake<nopcode.org> */
/* radare - LGPL - Copyright 2009-2013 - pancake */
#include <r_io.h>
// TODO: to be deprecated.. this is slow and boring

View File

@ -160,9 +160,11 @@ static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) {
if (mal->buf != NULL) {
memcpy (mal->buf, out, rlen);
free (out);
return r_io_desc_new (&r_io_plugin_bfdbg, mal->fd, pathname, rw, mode, mal);
return r_io_desc_new (&r_io_plugin_bfdbg,
mal->fd, pathname, rw, mode, mal);
}
eprintf ("Cannot allocate (%s) %d bytes\n", pathname+9, mal->size);
eprintf ("Cannot allocate (%s) %d bytes\n",
pathname+9, mal->size);
free (mal);
free (out);
}

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2012 - pancake */
/* radare - LGPL - Copyright 2009-2013 - pancake */
#include <r_userconf.h>
@ -260,7 +260,7 @@ struct r_io_plugin_t r_io_plugin_mach = {
#else
struct r_io_plugin_t r_io_plugin_mach = {
.name = "io.mach",
.name = "mach",
.desc = "mach debug io (unsupported in this platform)"
};
#endif

57
libr/io/p/io_mmap.c Normal file
View File

@ -0,0 +1,57 @@
/* radare - LGPL - Copyright 2013 - pancake */
#include <r_userconf.h>
#include <r_io.h>
#include <r_lib.h>
static int __plugin_open(RIO *io, const char *file) {
return (!memcmp (file, "mmap://", 7));
}
static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
if (r_file_mmap_read (file+7, 0, NULL, 0)==0)
return r_io_desc_new (&r_io_plugin_mmap,
-1, file+7, rw, mode, NULL);
return NULL;
}
static int __read(RIO *io, RIODesc *fd, ut8 *buf, int len) {
return r_file_mmap_read (fd->name, io->off, buf, len);
}
static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int len) {
if (fd->flags & 2)
return r_file_mmap_write (fd->name, io->off, buf, len);
return -1;
}
static ut64 __lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
switch (whence) {
case SEEK_SET: return offset;
case SEEK_CUR: return io->off + offset;
case SEEK_END: return r_file_size (fd->name);
}
return offset;
}
static int __close(RIODesc *fd) {
return 0;
}
struct r_io_plugin_t r_io_plugin_mmap = {
.name = "mmap",
.desc = "open file using mmap://",
.open = __open,
.close = __close,
.read = __read,
.plugin_open = __plugin_open,
.lseek = __lseek,
.write = __write,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_IO,
.data = &r_io_plugin_mach
};
#endif

18
libr/io/p/mmap.mk Normal file
View File

@ -0,0 +1,18 @@
OBJ_MMAP=io_mmap.o
STATIC_OBJ+=${OBJ_MMAP}
TARGET_MMAP=io_mmap.${EXT_SO}
ALL_TARGETS+=${TARGET_MMAP}
ifeq (${WITHPIC},0)
LINKFLAGS+=../../util/libr_util.a
LINKFLAGS+=../../lib/libr_lib.a
LINKFLAGS+=../../io/libr_io.a
else
LINKFLAGS+=-L../../lib -lr_lib
LINKFLAGS+=-L../../util -lr_util
LINKFLAGS+=-L.. -L../../lib -lr_lib -lr_io
endif
${TARGET_MMAP}: ${OBJ_MMAP}
${CC_LIB} $(call libname,io_mmap) ${CFLAGS} -o ${TARGET_MMAP} ${OBJ_MMAP} ${LINKFLAGS}

View File

@ -29,26 +29,23 @@ R_API RBuffer *r_buf_new() {
R_API RBuffer *r_buf_mmap (const char *file, int rw) {
RBuffer *b = r_buf_new ();
if (!b) return NULL;
b->mmap = r_file_mmap (file, rw);
b->mmap = r_file_mmap (file, rw, 0);
if (b->mmap && b->mmap->len>0) {
b->buf = b->mmap->buf;
b->length = b->mmap->len;
} else {
r_buf_free (b);
return NULL; /* we just freed b, don't return it */
return b;
}
return b;
r_buf_free (b);
return NULL; /* we just freed b, don't return it */
}
R_API RBuffer *r_buf_file (const char *file) {
RBuffer *b = r_buf_new ();
if (!b) return NULL;
b->buf = (ut8*)r_file_slurp (file, &b->length);
if (!b->buf) {
r_buf_free (b);
return NULL; /* we just freed b, don't return it */
}
return b;
if (b->buf) return b;
r_buf_free (b);
return NULL; /* we just freed b, don't return it */
}
R_API int r_buf_set_bits(RBuffer *b, int bitoff, int bitsize, ut64 value) {
@ -58,10 +55,8 @@ R_API int r_buf_set_bits(RBuffer *b, int bitoff, int bitsize, ut64 value) {
}
R_API int r_buf_set_bytes(RBuffer *b, const ut8 *buf, int length) {
if (b->buf)
free (b->buf);
if (length<0)
return R_FALSE;
if (b->buf) free (b->buf);
if (length<0) return R_FALSE;
if (!(b->buf = malloc (length+1)))
return R_FALSE;
memcpy (b->buf, buf, length);

View File

@ -231,7 +231,7 @@ R_API char *r_file_root(const char *root, const char *path) {
R_API boolt r_file_dump(const char *file, const ut8 *buf, int len) {
int ret;
FILE *fd = r_sandbox_fopen(file, "wb");
FILE *fd = r_sandbox_fopen (file, "wb");
if (fd == NULL) {
eprintf ("Cannot open '%s' for writing\n", file);
return R_FALSE;
@ -251,8 +251,57 @@ R_API boolt r_file_rm(const char *file) {
#endif
}
R_API int r_file_mmap_write(const char *file, ut64 addr, const ut8 *buf, int len) {
int fd;
#if __WINDOWS__
fd = r_sandbox_open (file, O_BINARY, 0644);
#else
fd = r_sandbox_open (file, O_RDWR|O_SYNC, 0644);
#endif
#if __UNIX__
const int pagesize = 4096;
int mmlen = len+pagesize;
int rest = addr%pagesize;
ut8 *mmap_buf;
if (fd == -1) return -1;
mmap_buf = mmap (NULL, mmlen*2, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, (off_t)addr-rest);
if (((int)(size_t)mmap_buf)==-1)
return -1;
memcpy (mmap_buf+rest, buf, len);
munmap (mmap_buf, mmlen*2);
close (fd);
return len;
#endif
return 0;
}
R_API int r_file_mmap_read (const char *file, ut64 addr, ut8 *buf, int len) {
#if __WINDOWS__
int fd = r_sandbox_open (file, O_BINARY, 0644);
#else
int fd = r_sandbox_open (file, O_RDONLY, 0644);
#endif
#if __UNIX__
const int pagesize = 4096;
int mmlen = len+pagesize;
int rest = addr%pagesize;
ut8 *mmap_buf;
if (fd == -1) return -1;
mmap_buf = mmap (NULL, mmlen*2, PROT_READ,
MAP_SHARED, fd, (off_t)addr-rest);
if (((int)(size_t)mmap_buf)==-1)
return -1;
memcpy (buf, mmap_buf+rest, len);
munmap (mmap_buf, mmlen*2);
close (fd);
return len;
#endif
return 0;
}
// TODO: add rwx support?
R_API RMmap *r_file_mmap (const char *file, boolt rw) {
R_API RMmap *r_file_mmap (const char *file, boolt rw, ut64 base) {
RMmap *m = NULL;
#if __WINDOWS__
int fd = r_sandbox_open (file, O_BINARY, 0644);
@ -265,12 +314,13 @@ R_API RMmap *r_file_mmap (const char *file, boolt rw) {
close (fd);
return NULL;
}
m->base = base;
m->rw = rw;
m->fd = fd;
m->len = lseek (fd, (off_t)0, SEEK_END);
#if __UNIX__
m->buf = mmap (NULL, m->len, rw?PROT_READ|PROT_WRITE:PROT_READ,
MAP_SHARED, fd, (off_t)0);
MAP_SHARED, fd, (off_t)base);
if (m->buf == MAP_FAILED) {
free (m);
m = NULL;
@ -293,7 +343,7 @@ R_API RMmap *r_file_mmap (const char *file, boolt rw) {
}
if (m->fm != INVALID_HANDLE_VALUE) {
m->buf = MapViewOfFile (m->fm, rw?
FILE_MAP_READ|FILE_MAP_WRITE:FILE_MAP_READ, 0, 0, 0);
FILE_MAP_READ|FILE_MAP_WRITE:FILE_MAP_READ, base, 0, 0);
} else {
CloseHandle (m->fh);
free (m);

View File

@ -98,6 +98,7 @@ io.http
io.bfdbg
io.gdb
io.haret
io.mmap
io.mach
io.w32
io.w32dbg