From 1dd4042505ea3610868cc710e8dfd4a11b3642e5 Mon Sep 17 00:00:00 2001 From: pancake Date: Sat, 5 Sep 2009 23:58:02 +0000 Subject: [PATCH] * Lot of work on refactoring r_io - Added desc.c to handle file descriptors (avoid dupped, register handlers, ..) - Fix some bugs and optimize io paths - Added resolution methods - Maps are incomplete but better interfaced. Needs more work - Added io/t/map.c test program (not yet working) * Some fixups in many vapi files - Fix broken ones (syntax parsing error) - Complete them a bit more * Initial working version of a demo of list.h vala interface - Implemented as a Generic class - Designed as an Iterator, so 'foreach' can be used to iterate - Added example program iterating from vala and generating a list in C --- libr/include/r_io.h | 32 +++++++++--- libr/io/Makefile | 2 +- libr/io/README | 6 ++- libr/io/desc.c | 57 +++++++++++++++++++-- libr/io/handle.c | 7 --- libr/io/io.c | 28 +++++++---- libr/io/map.c | 111 +++++++++++++++++++---------------------- libr/io/p/io_dbg.c | 6 --- libr/io/p/io_malloc.c | 6 --- libr/io/p/io_ptrace.c | 11 ---- libr/io/p/io_shm.c | 6 --- libr/io/section.c | 8 +-- libr/io/t/Makefile | 17 ++++--- libr/io/t/cat.c | 2 +- libr/io/t/map.c | 11 ++++ libr/io/t/read4.c | 8 ++- libr/vapi/r_io.vapi | 69 ++++++++++++++++++++----- libr/vapi/r_range.vapi | 2 +- libr/vapi/r_util.vapi | 45 +++++++++-------- libr/vapi/t/Makefile | 9 +++- libr/vapi/t/io.vala | 18 +++++++ libr/vapi/t/iter.vala | 5 +- libr/vapi/t/list.vala | 14 ++++++ libr/vapi/t/list_c.c | 33 ++++++++++++ libr/vapi/t/list_c.h | 18 +++++++ 25 files changed, 361 insertions(+), 170 deletions(-) create mode 100644 libr/io/t/map.c create mode 100644 libr/vapi/t/io.vala create mode 100644 libr/vapi/t/list.vala create mode 100644 libr/vapi/t/list_c.c create mode 100644 libr/vapi/t/list_c.h diff --git a/libr/include/r_io.h b/libr/include/r_io.h index 8c125f02ee..8201b56c5f 100644 --- a/libr/include/r_io.h +++ b/libr/include/r_io.h @@ -6,7 +6,7 @@ #define R_IO_READ 0 #define R_IO_WRITE 1 -#define R_IO_RDWR 2 +#define R_IO_RDWR 2 // ??? #define R_IO_NFDS 32 @@ -14,10 +14,11 @@ #define R_IO_SEEK_CUR 1 #define R_IO_SEEK_END 2 -#define IO_MAP_N 32 -struct r_io_maps_t { +#define IO_MAP_N 128 +struct r_io_map_t { int fd; - char file[128]; + int flags; + ut64 delta; ut64 from; ut64 to; struct list_head list; @@ -48,7 +49,9 @@ struct r_io_t { struct list_head io_list; ut64 last_align; struct list_head sections; + /* maps */ struct list_head maps; + struct list_head desc; }; //struct r_io_handle_fd_t { @@ -70,7 +73,7 @@ struct r_io_handle_t { int (*write)(struct r_io_t *io, int fd, const ut8 *buf, int count); int (*close)(struct r_io_t *io, int fd); int (*handle_open)(struct r_io_t *io, const char *); - int (*handle_fd)(struct r_io_t *, int); + //int (*handle_fd)(struct r_io_t *, int); int fds[R_IO_NFDS]; }; @@ -93,7 +96,7 @@ R_API struct r_io_handle_t *r_io_handle_resolve(struct r_io_t *io, const char *f R_API struct r_io_handle_t *r_io_handle_resolve_fd(struct r_io_t *io, int fd); /* io/io.c */ -R_API int r_io_init(struct r_io_t *io); +R_API struct r_io_t* r_io_init(struct r_io_t *io); R_API int r_io_set_write_mask(struct r_io_t *io, const ut8 *buf, int len); R_API int r_io_open(struct r_io_t *io, const char *file, int flags, int mode); R_API int r_io_redirect(struct r_io_t *io, const char *file); @@ -110,11 +113,11 @@ R_API ut64 r_io_size(struct r_io_t *io, int fd); /* io/map.c */ R_API void r_io_map_init(struct r_io_t *io); -R_API int r_io_map_rm(struct r_io_t *io, int fd); +R_API int r_io_map_del(struct r_io_t *io, int fd); R_API int r_io_map_list(struct r_io_t *io); R_API int r_io_map(struct r_io_t *io, const char *file, ut64 offset); R_API int r_io_map_read_at(struct r_io_t *io, ut64 off, ut8 *buf, ut64 len); -R_API int r_io_map_read_rest(struct r_io_t *io, ut64 off, ut8 *buf, ut64 len); +//R_API int r_io_map_read_rest(struct r_io_t *io, ut64 off, ut8 *buf, ut64 len); R_API int r_io_map_write_at(struct r_io_t *io, ut64 off, const ut8 *buf, ut64 len); /* sections */ @@ -147,6 +150,19 @@ R_API void r_io_section_init(struct r_io_t *io); R_API int r_io_section_overlaps(struct r_io_t *io, struct r_io_section_t *s); R_API ut64 r_io_section_align(struct r_io_t *io, ut64 addr, ut64 vaddr, ut64 paddr); +struct r_io_desc_t { + int fd; + int flags; + char name[4096]; + struct r_io_handle_t *handle; + struct list_head list; +}; + +R_API int r_io_desc_init(struct r_io_t *io); +R_API int r_io_desc_add(struct r_io_t *io, int fd, const char *file, int flags, struct r_io_handle_t *handle); +R_API int r_io_desc_del(struct r_io_t *io, int fd); +R_API struct r_io_desc_t *r_io_desc_get(struct r_io_t *io, int fd); +R_API int r_io_desc_generate(struct r_io_t *io); #if 0 #define CB_READ int (*cb_read)(struct r_io_t *user, int pid, ut64 addr, ut8 *buf, int len) #define CB_WRITE int (*cb_write)(struct r_io_t *user, int pid, ut64 addr, const ut8 *buf, int len) diff --git a/libr/io/Makefile b/libr/io/Makefile index e4ab5e8009..9b64afc396 100644 --- a/libr/io/Makefile +++ b/libr/io/Makefile @@ -9,7 +9,7 @@ STATIC_OBJS= include ../config.mk include ${STATIC_IO_PLUGINS} STATIC_OBJS=$(subst ..,p/..,$(subst io_,p/io_,$(STATIC_OBJ))) -OBJ=${STATIC_OBJS} io.o handle.o map.o section.o +OBJ=${STATIC_OBJS} io.o handle.o map.o section.o desc.o pre: @echo STATICOBJS: ${STATIC_OBJS} diff --git a/libr/io/README b/libr/io/README index 2f8430f596..0c33d91787 100644 --- a/libr/io/README +++ b/libr/io/README @@ -19,7 +19,9 @@ CACHE - caches write operations to emulate fake reads NOTES: each plugin handle must provide a 'optimal' read size.. or io must be configured to this -var io = new Radare.IO(); +var io = new Radare.Io(); int fd = io.open("/bin/ls"); - +foreach (Io.Handle handle in io.handle_list()) { + stdout.printf(" %s\n", handle.name); +} diff --git a/libr/io/desc.c b/libr/io/desc.c index be7ea66efe..ef13ed126b 100644 --- a/libr/io/desc.c +++ b/libr/io/desc.c @@ -1,5 +1,56 @@ /* radare - LGPL - Copyright 2009 pancake */ -/* this file manages file descriptors to store seek and io_handler */ -int r_io_desc() -{} +#include + +R_API int r_io_desc_init(struct r_io_t *io) +{ + INIT_LIST_HEAD(&io->desc); + return R_TRUE; +} + +R_API int r_io_desc_add(struct r_io_t *io, int fd, const char *file, int flags, struct r_io_handle_t *handle) +{ + struct r_io_desc_t *desc = MALLOC_STRUCT(struct r_io_desc_t); + if (desc == NULL) + return R_FALSE; + strncpy(desc->name, file, sizeof(desc->name)); + desc->flags = flags; + desc->fd = fd; + desc->handle = handle; + list_add_tail(&(desc->list), &(io->desc)); + return R_TRUE; +} + +R_API int r_io_desc_del(struct r_io_t *io, int fd) +{ + int ret = R_FALSE; + struct list_head *pos; + list_for_each_prev(pos, &io->desc) { + struct r_io_desc_t *d = list_entry(pos, struct r_io_desc_t, list); + if (d->fd == fd) { + list_del((&d->list)); + ret = R_TRUE; + break; + } + } + return ret; +} + +R_API struct r_io_desc_t *r_io_desc_get(struct r_io_t *io, int fd) +{ + struct list_head *pos; + list_for_each_prev(pos, &io->desc) { + struct r_io_desc_t *d = list_entry(pos, struct r_io_desc_t, list); + if (d->fd == fd) + return d; + } + return NULL; +} + +R_API int r_io_desc_generate(struct r_io_t *io) +{ + int fd; + do fd = 0xf000 + rand()%0xfff; + while (r_io_desc_get(io, fd)); + return fd; +} diff --git a/libr/io/handle.c b/libr/io/handle.c index 8026a0560c..c45e306b4e 100644 --- a/libr/io/handle.c +++ b/libr/io/handle.c @@ -64,13 +64,6 @@ R_API struct r_io_handle_t *r_io_handle_resolve_fd(struct r_io_t *io, int fd) return NULL; } -R_API int r_io_handle_generate(struct r_io_t *io) -{ - // TODO: ensure srand here (( implement in r_util a decent random helper - // TODO: register io plugin with new fd - return (rand()%666)+1024; -} - R_API int r_io_handle_open(struct r_io_t *io, int fd, struct r_io_handle_t *plugin) { int i=0; diff --git a/libr/io/io.c b/libr/io/io.c index 25be15cefe..806a1abc37 100644 --- a/libr/io/io.c +++ b/libr/io/io.c @@ -4,7 +4,7 @@ #include "r_util.h" #include -R_API int r_io_init(struct r_io_t *io) +R_API struct r_io_t *r_io_init(struct r_io_t *io) { io->write_mask_fd = -1; io->last_align = 0; @@ -13,22 +13,24 @@ R_API int r_io_init(struct r_io_t *io) r_io_map_init(io); r_io_section_init(io); r_io_handle_init(io); - return 0; + r_io_desc_init(io); + return io; } R_API struct r_io_t *r_io_new() { struct r_io_t *io = MALLOC_STRUCT(struct r_io_t); - r_io_init(io); - return io; + return r_io_init(io); } R_API struct r_io_t *r_io_free(struct r_io_t *io) { + /* TODO: properly free inner nfo */ free(io); return NULL; } +/* used by uri handler plugins */ R_API int r_io_redirect(struct r_io_t *io, const char *file) { free(io->redirect); @@ -62,8 +64,11 @@ R_API int r_io_open(struct r_io_t *io, const char *file, int flags, int mode) } if (fd == -2) fd = open(file, flags, mode); - if (fd > -1) r_io_set_fd(io, fd); - else fd = -1; + if (fd >= 0) { + r_io_set_fd(io, fd); + r_io_desc_add(io, fd, file, flags, io->plugin); + } else fd = -1; + free((void *)uri); return fd; } @@ -215,21 +220,24 @@ R_API ut64 r_io_size(struct r_io_t *io, int fd) R_API int r_io_system(struct r_io_t *io, const char *cmd) { + int ret = -1; if (io->plugin && io->plugin->system) - return io->plugin->system(io, io->fd, cmd); - return 0; + ret = io->plugin->system(io, io->fd, cmd); + return ret; } // TODO: remove int fd here??? R_API int r_io_close(struct r_io_t *io, int fd) { fd = r_io_set_fd(io, fd); - if (io->plugin) { - io->fd = fd; + if (fd != -1 && io->plugin) { + r_io_desc_del(io, fd); + r_io_map_del(io, fd); r_io_handle_close(io, fd, io->plugin); if (io->plugin->close) return io->plugin->close(io, fd); } + io->fd = -1; // unset current fd return close(fd); } diff --git a/libr/io/map.c b/libr/io/map.c index 53dc6c3a23..4e5f4a713b 100644 --- a/libr/io/map.c +++ b/libr/io/map.c @@ -6,81 +6,73 @@ #include "r_io.h" #include "list.h" - -#if 0 -static int maps_n = 0; -static int maps[10]; -#endif - -void r_io_map_init(struct r_io_t *io) +R_API void r_io_map_init(struct r_io_t *io) { INIT_LIST_HEAD(&io->maps); } -int r_io_map_rm(struct r_io_t *io, int fd) +R_API struct r_io_map_t *r_io_map_resolve(struct r_io_t *io, int fd) { struct list_head *pos; list_for_each_prev(pos, &io->maps) { - struct r_io_maps_t *im = list_entry(pos, struct r_io_maps_t, list); - if (im->fd == fd) { - /* FREE THIS */ - r_io_handle_close(io, - fd, r_io_handle_resolve_fd(io, fd)); - fprintf(stderr, "r_io_map_rm: TODO\n"); - return 0; - } + struct r_io_map_t *im = list_entry(pos, struct r_io_map_t, list); + if (im->fd == fd) + return im; } - fprintf(stderr, "Not found\n"); - return 0; + return NULL; } +/* remove all maps of a fd */ +R_API int r_io_map_del(struct r_io_t *io, int fd) +{ + int ret = R_FALSE; + struct list_head *pos, *n; + list_for_each_safe(pos, n, &io->maps) { + struct r_io_map_t *im = list_entry(pos, struct r_io_map_t, list); + if (im->fd == fd) { + list_del(&im->list); + ret = R_TRUE; + } + } + return ret; +} + +R_API int r_io_map_add(struct r_io_t *io, int fd, int flags, ut64 delta, ut64 offset, ut64 size) +{ + struct r_io_map_t *im = MALLOC_STRUCT(struct r_io_map_t); + if (im == NULL) + return R_FALSE; + list_add_tail(&(im->list), &(io->maps)); + im->fd = fd; + im->flags = flags; + im->delta = delta; + im->from = offset; + im->to = offset + size; + return R_TRUE; +} + +/* TODO: Use r_iter here ?? */ int r_io_map_list(struct r_io_t *io) { int n = 0; struct list_head *pos; list_for_each_prev(pos, &io->maps) { - struct r_io_maps_t *im = list_entry(pos, struct r_io_maps_t, list); - if (im->file[0] != '\0') { - printf("0x%08llx 0x%08llx %s\n", - im->from, im->to, im->file); - n++; - } + struct r_io_map_t *im = list_entry(pos, struct r_io_map_t, list); + printf("0x%08llx 0x%08llx delta=0x%08llx fd=%d flags=%x\n", + im->from, im->to, im->delta, im->fd, im->flags); + n++; } return n; } -int r_io_map(struct r_io_t *io, const char *file, ut64 offset) -{ - struct r_io_maps_t *im; - int fd = r_io_open(io, file, R_IO_READ, 0644); - if (fd == -1) - return -1; - im = MALLOC_STRUCT(struct r_io_maps_t); -//(struct r_io_maps_t*)malloc(sizeof(struct r_io_maps_t)); - if (im == NULL) { - r_io_close(io, fd); - return -1; - } - im->fd = fd; - strncpy(im->file, file, 127); - im->from = offset; - im->to = offset+lseek(fd, 0, SEEK_END); - list_add_tail(&(im->list), &(io->maps)); - return fd; -} - int r_io_map_read_at(struct r_io_t *io, ut64 off, ut8 *buf, ut64 len) { struct list_head *pos; - - if (io == NULL) - return 0; list_for_each_prev(pos, &io->maps) { - struct r_io_maps_t *im = list_entry(pos, struct r_io_maps_t, list); - if (im) - if (im->file && im->file[0] != '\0' && off >= im->from && off < im->to) { + struct r_io_map_t *im = list_entry(pos, struct r_io_map_t, list); + if (im && off >= im->from && off < im->to) { r_io_set_fd(io, im->fd); - return r_io_read_at(io, off-im->from, buf, len); + return r_io_read_at(io, off-im->from + im->delta, buf, len); } } return 0; @@ -89,24 +81,24 @@ int r_io_map_read_at(struct r_io_t *io, ut64 off, ut8 *buf, ut64 len) int r_io_map_write_at(struct r_io_t *io, ut64 off, const ut8 *buf, ut64 len) { struct list_head *pos; - list_for_each_prev(pos, &io->maps) { - struct r_io_maps_t *im = list_entry(pos, struct r_io_maps_t, list); - if (im) - if (im->file[0] != '\0' && off >= im->from && off < im->to) { - r_io_set_fd(io, im->fd); - return r_io_write_at(io, off-im->from, buf, len); + struct r_io_map_t *im = list_entry(pos, struct r_io_map_t, list); + if (im && off >= im->from && off < im->to) { + if (im->flags & R_IO_WRITE) { + r_io_set_fd(io, im->fd); + return r_io_write_at(io, off-im->from + im->delta, buf, len); + } else return -1; } } return 0; } +#if 0 int r_io_map_read_rest(struct r_io_t *io, ut64 off, ut8 *buf, ut64 len) { struct list_head *pos; - list_for_each_prev(pos, &io->maps) { - struct r_io_maps_t *im = list_entry(pos, struct r_io_maps_t, list); + struct r_io_map_t *im = list_entry(pos, struct r_io_map_t, list); if (im->file[0] != '\0' && off+len >= im->from && off < im->to) { lseek(im->fd, 0, SEEK_SET); // XXX VERY BROKEN @@ -115,3 +107,4 @@ int r_io_map_read_rest(struct r_io_t *io, ut64 off, ut8 *buf, ut64 len) } return 0; } +#endif diff --git a/libr/io/p/io_dbg.c b/libr/io/p/io_dbg.c index d3a8bcf41d..29945d6a5a 100644 --- a/libr/io/p/io_dbg.c +++ b/libr/io/p/io_dbg.c @@ -119,11 +119,6 @@ static int __open(struct r_io_t *io, const char *file, int rw, int mode) return -1; } -static int __handle_fd(struct r_io_t *io, int fd) -{ - return R_FALSE; -} - static int __init(struct r_io_t *io) { printf("dbg init\n"); @@ -136,7 +131,6 @@ struct r_io_handle_t r_io_plugin_dbg = { .desc = "Debug a program or pid. dbg:///bin/ls, dbg://1388", .open = __open, .handle_open = __handle_open, - .handle_fd = __handle_fd, .lseek = NULL, .system = NULL, .debug = (void *)1, diff --git a/libr/io/p/io_malloc.c b/libr/io/p/io_malloc.c index 37e7eca617..7fd95cd071 100644 --- a/libr/io/p/io_malloc.c +++ b/libr/io/p/io_malloc.c @@ -62,11 +62,6 @@ static ut64 __lseek(struct r_io_t *io, int fildes, ut64 offset, int whence) return malloc_seek; } -static int __handle_fd(struct r_io_t *io, int fd) -{ - return (fd == malloc_fd); -} - static int __handle_open(struct r_io_t *io, const char *pathname) { return (!memcmp(pathname, "malloc://", 9)); @@ -115,7 +110,6 @@ struct r_io_handle_t r_io_plugin_malloc = { .close = __close, .read = __read, .handle_open = __handle_open, - .handle_fd = __handle_fd, .lseek = __lseek, .system = __system, .init = __init, diff --git a/libr/io/p/io_ptrace.c b/libr/io/p/io_ptrace.c index ea130df4f6..39089c0a87 100644 --- a/libr/io/p/io_ptrace.c +++ b/libr/io/p/io_ptrace.c @@ -161,16 +161,6 @@ static int __open(struct r_io_t *io, const char *file, int rw, int mode) return ret; } -static int __handle_fd(struct r_io_t *io, int fd) -{ - int i; - for(i=0;iseek; } -static int shm__handle_fd(struct r_io_t *io, int fd) -{ - return (fd == shm_fd); -} - static int shm__handle_open(struct r_io_t *io, const char *pathname) { return (!memcmp(pathname, "shm://", 6)); @@ -100,7 +95,6 @@ struct r_io_handle_t r_io_plugin_shm = { .close = shm__close, .read = shm__read, .handle_open = shm__handle_open, - .handle_fd = shm__handle_fd, .lseek = shm__lseek, .system = NULL, // shm__system, .init = shm__init, diff --git a/libr/io/section.c b/libr/io/section.c index 91a013c22f..2fe6eac09a 100644 --- a/libr/io/section.c +++ b/libr/io/section.c @@ -134,10 +134,10 @@ void r_io_section_list_visual(struct r_io_t *io, ut64 seek, ut64 len) if (i>0 && len != 0) { cons_printf("=> 0x%08llx |", seek); for(j=0;j= seek && (j*mul)+min <= seek+len) - cons_printf("#"); - else - cons_printf("-"); + cons_printf( + ((j*mul)+min >= seek && + (j*mul)+min <= seek+len) + ?"#":"-"); } cons_printf("| 0x%08llx\n", seek+len); } diff --git a/libr/io/t/Makefile b/libr/io/t/Makefile index a978173ba9..ed8c42b4e5 100644 --- a/libr/io/t/Makefile +++ b/libr/io/t/Makefile @@ -1,10 +1,15 @@ -all: cat read4 +CFLAGS=-I ../../include -g -cat: - ${CC} -I ../../include cat.c -Wl,-R.. -lr_io -g -o cat +all: map cat read4 -read4: - ${CC} -I ../../include read4.c -Wl,-R.. -lr_io -g -o read4 +cat: cat.o + ${CC} cat.o -Wl,-R.. -lr_io -g -o cat + +map: map.o + ${CC} map.o -Wl,-R.. -lr_io -g -o map + +read4: read4.o + ${CC} read4.o -Wl,-R.. -lr_io -g -o read4 clean: - rm -f cat read4 + rm -f cat read4 map *.o diff --git a/libr/io/t/cat.c b/libr/io/t/cat.c index f022ae369a..fc85323095 100644 --- a/libr/io/t/cat.c +++ b/libr/io/t/cat.c @@ -1,4 +1,4 @@ -#include "r_io.h" +#include int main(int argc, char **argv) { diff --git a/libr/io/t/map.c b/libr/io/t/map.c new file mode 100644 index 0000000000..5fd791fb9d --- /dev/null +++ b/libr/io/t/map.c @@ -0,0 +1,11 @@ +#include + +int main () { + char buf[1024]; + struct r_io_t *io = r_io_new(); + int fd = r_io_open(io, "/bin/ls", R_IO_READ, 0); + r_io_map_add(io, fd, R_IO_READ, 0, 0xf00000, 0xffff); + + r_io_read_at(&io, 0xf00000, buf, 1024); + r_io_free(io); +} diff --git a/libr/io/t/read4.c b/libr/io/t/read4.c index 1252a5a31d..d71263959b 100644 --- a/libr/io/t/read4.c +++ b/libr/io/t/read4.c @@ -5,10 +5,16 @@ int main(int argc, char **argv) char buf[1024]; int fd; struct r_io_t io; + char *file; r_io_init(&io); - fd = r_io_open(&io, argc>1?argv[1]:"/bin/ls", R_IO_READ, 0); + file = argc>1?argv[1]:"/bin/ls"; + fd = r_io_open(&io, file, R_IO_READ, 0); + if (fd == -1) { + printf("Cannot open file '%s'\n", file); + return 1; + } printf("FD = %d\n", fd); r_io_lseek(&io, 1, R_IO_SEEK_SET); memset(buf, '\0', sizeof(buf)); diff --git a/libr/vapi/r_io.vapi b/libr/vapi/r_io.vapi index ba835b7cba..421040a41f 100644 --- a/libr/vapi/r_io.vapi +++ b/libr/vapi/r_io.vapi @@ -6,6 +6,20 @@ namespace Radare [Compact] [CCode (cname="struct r_io_t", free_function="r_io_free", cprefix="r_io_")] public class Io { + public enum Flags { + READ = 0, + WRITE = 1, + RDWR = 2, + } + + public enum Seek { + SET = 0, + CUR = 1, + END = 2, + } + /** + * Do the io! :D + */ public Io(); public Io* free(); public bool init(); @@ -13,22 +27,51 @@ namespace Radare public int open(string uri, int flags, int mode); public int read(int fd, out uint8 *buf, int len); public int write(int fd, uint8 *buf, int len); - public uint64 lseek(int fd, ut64 addr, int whence); + public uint64 lseek(int fd, uint64 addr, int whence); public int system(int fd, string cmd); public int close(int fd); - public ut64 size(int fd); - } + public uint64 size(int fd); - //[Compact] - [CCode (cprefix="r_io_handle_")] - public struct Handle { - public static bool init(); - - } + /* handle */ + public struct Handle { + string name; + string desc; + // TODO: lot of missing stuff here :) + } + public bool handle_open(int fd, Io.Handle plugin); + public bool handle_add(Io.Handle plugin); + public int handle_generate(); + public void handle_list(); - //[Compact] - [CCode (cprefix="r_io_map_")] - public struct Map { - public static bool init(); + /* maps */ + public struct Map { + int fd; + uint64 from; + uint64 to; + } + public Io.Map map_resolve(int fd); + public bool map_add(int fd, uint64 addr, uint64 size); + public bool map_del(int fd); + public void map_list(); // DEPRECATE + public int map_read_at(uint64 addr, uint8 *buf, uint64 len); + public int map_write_at(uint64 addr, uint8 *buf, uint64 len); + /* sections */ + [CCode (cname="struct r_io_section_t")] + public struct Section { + string comment; + uint64 from; + uint64 to; + uint64 vaddr; + uint64 paddr; + int rwx; // TODO: use perms + } + /* desc */ + [CCode (cname="struct r_io_desc_t")] + public struct Desc { + int fd; + int flags; + const string name; + } + public bool desc_add(int fd, string file, Io.Flags flags); } } diff --git a/libr/vapi/r_range.vapi b/libr/vapi/r_range.vapi index 03699483e2..5e765e9f84 100644 --- a/libr/vapi/r_range.vapi +++ b/libr/vapi/r_range.vapi @@ -28,7 +28,7 @@ namespace Radare { public static struct Item { public uint64 from; public uint64 to; - public uint8 *data + public uint8 *data; public int datalen; } } diff --git a/libr/vapi/r_util.vapi b/libr/vapi/r_util.vapi index 9874b1a4bb..07e022ffcb 100644 --- a/libr/vapi/r_util.vapi +++ b/libr/vapi/r_util.vapi @@ -19,31 +19,13 @@ namespace Radare { } /* Generic Iterator interfaced with r_iter */ -/* - [Compact] - [CCode (cprefix="r_iter_", cname="void")] - public class Iter { - public Iter* (int size); - public Iter* get (); - public Iter* next (); - public Iter* next_n (int idx); - public Iter* prev (); - public void delete (); - public Iter* first (); - public bool last (); - // TODO: foreach() - public Iter* free (); - public void set (int idx, owned void *data); - } -*/ - [Compact] [CCode (cprefix="r_iter_", cname="void")] - public class GenericIter { - public GenericIter (int size); + public class Iter { + public Iter (int size); public unowned G get (); - public unowned GenericIter next (); - public unowned GenericIter next_n (int idx); + public unowned Iter next (); + public unowned Iter next_n (int idx); public unowned G prev (); public void delete (); public unowned G first (); @@ -52,6 +34,25 @@ namespace Radare { public unowned G free (); public void set (int idx, owned G data); } + + [Compact] + [CCode (cprefix="ralist_", cheader_filename="list_c.h", cname="struct list_head")] + public static class List { //: Iterator { + [CCode (cname="ralist_next")] + public bool next(); + [CCode (cname="")] + public G @free(G arg); + //[CCode (cname="calist_entry")] + public G get(); + public List iterator(); + } } +// DEMO TEST DEMO TEST DEMO TEST DEMO TEST DEMO TEST // +[Compact] +[CCode (cname="foo", cheader_filename="list_c.h")] +public class Foo { + public string name; + [CCode (cname="")] + public void free(); } diff --git a/libr/vapi/t/Makefile b/libr/vapi/t/Makefile index ee2ca16765..a1130c5fe7 100644 --- a/libr/vapi/t/Makefile +++ b/libr/vapi/t/Makefile @@ -1,10 +1,17 @@ -all: core hash sc socket asm search iter bin db +all: core hash sc socket asm search iter bin db io list @true +# XXX +list: + valac -o list list_c.c list.vala --pkg r_util --vapidir=${PWD}/.. -X -I. + genie: valac --vapidir=.. --pkg r_asm --pkg libr asm.gs valac --vapidir=.. --pkg r_search --pkg libr search.gs +io: + valac --vapidir=${PWD}/.. --pkg r_io io.vala + db: valac --vapidir=${PWD}/.. --pkg r_db db.vala diff --git a/libr/vapi/t/io.vala b/libr/vapi/t/io.vala new file mode 100644 index 0000000000..555e3cbf49 --- /dev/null +++ b/libr/vapi/t/io.vala @@ -0,0 +1,18 @@ +using Radare; + +void main(string[] args) +{ + var io = new Radare.Io (); + int fd = io.open ("/bin/ls", 0,0); //Io.Flags.READ, 0); + if (fd != -1) { + stdout.printf("cannot open file\n"); + } +/* + Radare.Iter handle = io.handle_list(); + while (!handle->last()) { + stdout.printf(" Handle: %s\n", handle->name); + handle = handle->next(); + } + handle->free(); +*/ +} diff --git a/libr/vapi/t/iter.vala b/libr/vapi/t/iter.vala index caae22267e..b11f285f6d 100644 --- a/libr/vapi/t/iter.vala +++ b/libr/vapi/t/iter.vala @@ -8,9 +8,9 @@ public class IterableObject { } } -Radare.GenericIter* get_iter_list () +Radare.Iter* get_iter_list () { - GenericIter list = new GenericIter(4); + Iter list = new Iter(5); list.set(0, new IterableObject("patata")); list.set(1, new IterableObject("cacatua")); @@ -34,4 +34,5 @@ void main() IterableObject io = foo->get (); stdout.printf("name: %s\n", io.name); } + foo->free(); } diff --git a/libr/vapi/t/list.vala b/libr/vapi/t/list.vala new file mode 100644 index 0000000000..ecca65a09a --- /dev/null +++ b/libr/vapi/t/list.vala @@ -0,0 +1,14 @@ +/* radare - LGPL - Copyright 2009 pancake<@nopcode.org> */ + +using Radare; + +[Import] +[CCode (cname="get_list")] +public static extern Radare.List get_list(); + +void main() { + Radare.List head = get_list(); + foreach (Foo f in head) { + stdout.printf(" - %p %s\n", f, f.name); + } +} diff --git a/libr/vapi/t/list_c.c b/libr/vapi/t/list_c.c new file mode 100644 index 0000000000..d3fc4e6c69 --- /dev/null +++ b/libr/vapi/t/list_c.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include + +struct list_head head; + +struct foo { + char *name; + struct list_head list; +}; + +struct list_head *get_list() { + INIT_LIST_HEAD(&head); + struct foo *a = malloc(sizeof(struct foo)); + a->name = strdup("hello"); + list_add(&a->list, &head); + struct foo *b = malloc(sizeof(struct foo)); + b->name = strdup("world"); + list_add(&b->list, &head); + return &head; +} + +#if TEST +main() { + struct list_head *list = get_list(); + struct list_head *pos; + list_for_each_prev(pos, list) { + struct foo *p = list_entry(pos, struct foo, list); + printf("%s\n", p->name); + } +} +#endif diff --git a/libr/vapi/t/list_c.h b/libr/vapi/t/list_c.h new file mode 100644 index 0000000000..1f747e9805 --- /dev/null +++ b/libr/vapi/t/list_c.h @@ -0,0 +1,18 @@ +#include +#include +#include +#include + +struct list_head head; + +typedef struct foo { + char *name; + struct list_head list; +} foo; + +#define ralist_iterator(x) x->next +#define ralist_get(x) list_entry(x, struct foo, list); x=x->next +#define ralist_next(x) (x=x->next, (x != head)) +#define ralist_free(x) (x) +#define foo_free(x) x +//void *ralist_free_(void *a) { return NULL; }