* 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
This commit is contained in:
pancake 2009-09-05 23:58:02 +00:00
parent a9d47cbecb
commit 1dd4042505
25 changed files with 361 additions and 170 deletions

View File

@ -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)

View File

@ -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}

View File

@ -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);
}

View File

@ -1,5 +1,56 @@
/* radare - LGPL - Copyright 2009 pancake<nopcode.org> */
/* this file manages file descriptors to store seek and io_handler */
int r_io_desc()
{}
#include <r_io.h>
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;
}

View File

@ -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;

View File

@ -4,7 +4,7 @@
#include "r_util.h"
#include <stdio.h>
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);
}

View File

@ -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

View File

@ -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,

View File

@ -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,

View File

@ -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;i<nfds;i++) {
if (fds[i]==fd)
return R_TRUE;
}
return R_FALSE;
}
static ut64 __lseek(struct r_io_t *io, int fildes, ut64 offset, int whence)
{
return -1;
@ -246,7 +236,6 @@ struct r_io_handle_t r_io_plugin_ptrace = {
.close = __close,
.read = __read,
.handle_open = __handle_open,
.handle_fd = __handle_fd,
.lseek = __lseek,
.system = __system,
.init = __init,

View File

@ -53,11 +53,6 @@ static ut64 shm__lseek(struct r_io_t *io, int fildes, ut64 offset, int whence)
return io->seek;
}
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,

View File

@ -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<width;j++) {
if ((j*mul)+min >= 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);
}

View File

@ -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

View File

@ -1,4 +1,4 @@
#include "r_io.h"
#include <r_io.h>
int main(int argc, char **argv)
{

11
libr/io/t/map.c Normal file
View File

@ -0,0 +1,11 @@
#include <r_io.h>
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);
}

View File

@ -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));

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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<G> {
public GenericIter (int size);
public class Iter<G> {
public Iter (int size);
public unowned G get ();
public unowned GenericIter<G> next ();
public unowned GenericIter<G> next_n (int idx);
public unowned Iter<G> next ();
public unowned Iter<G> 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<G> { //: Iterator {
[CCode (cname="ralist_next")]
public bool next();
[CCode (cname="")]
public G @free(G arg);
//[CCode (cname="calist_entry")]
public G get();
public List<G> 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();
}

View File

@ -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

18
libr/vapi/t/io.vala Normal file
View File

@ -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<IO.list> handle = io.handle_list();
while (!handle->last()) {
stdout.printf(" Handle: %s\n", handle->name);
handle = handle->next();
}
handle->free();
*/
}

View File

@ -8,9 +8,9 @@ public class IterableObject {
}
}
Radare.GenericIter<IterableObject>* get_iter_list ()
Radare.Iter<IterableObject>* get_iter_list ()
{
GenericIter<IterableObject> list = new GenericIter<IterableObject>(4);
Iter<IterableObject> list = new Iter<IterableObject>(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();
}

14
libr/vapi/t/list.vala Normal file
View File

@ -0,0 +1,14 @@
/* radare - LGPL - Copyright 2009 pancake<@nopcode.org> */
using Radare;
[Import]
[CCode (cname="get_list")]
public static extern Radare.List<Foo> get_list();
void main() {
Radare.List<Foo> head = get_list();
foreach (Foo f in head) {
stdout.printf(" - %p %s\n", f, f.name);
}
}

33
libr/vapi/t/list_c.c Normal file
View File

@ -0,0 +1,33 @@
#include <list.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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

18
libr/vapi/t/list_c.h Normal file
View File

@ -0,0 +1,18 @@
#include <list.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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; }