Begin r_io review. Fix r_socket_http query for xpcshell's httpd.js

- Added libr/io/README in order to documentate the api
- Some minor refactoring and code cleanup in libr/io
- Fix hexdump title
This commit is contained in:
pancake 2013-05-31 02:09:45 +02:00
parent fcf279ed1c
commit 96accbd14b
6 changed files with 59 additions and 47 deletions

View File

@ -132,19 +132,15 @@ typedef struct r_io_t {
int buffer_enabled;
} RIO;
//struct r_io_plugin_fd_t {
// ... store io changes here
//};
typedef struct r_io_plugin_t {
void *plugin;
char *name;
char *desc;
void *widget;
int (*listener)(RIODesc *io);
int (*listener)(RIODesc *io);
int (*init)();
RIOUndo undo;
struct debug_t *debug; // ???
RIOUndo undo;
struct r_debug_t *debug;
int (*system)(RIO *io, RIODesc *fd, const char *);
RIODesc* (*open)(RIO *io, const char *, int rw, int mode);
int (*read)(RIO *io, RIODesc *fd, ut8 *buf, int count);
@ -155,7 +151,6 @@ typedef struct r_io_plugin_t {
int (*accept)(RIO *io, RIODesc *desc, int fd);
int (*create)(RIO *io, const char *file, int mode, int type);
int (*plugin_open)(RIO *io, const char *);
//int (*plugin_fd)(RIO *, int);
} RIOPlugin;
typedef struct r_io_list_t {
@ -282,7 +277,7 @@ R_API const char *r_io_section_get_archbits(RIO* io, ut64 addr, int *bits);
R_API int r_io_section_rm(RIO *io, int idx);
R_API void r_io_section_list(RIO *io, ut64 offset, int rad);
R_API void r_io_section_list_visual(RIO *io, ut64 seek, ut64 len);
R_API struct r_io_section_t *r_io_section_get(RIO *io, ut64 offset);
R_API RIOSection *r_io_section_get(RIO *io, ut64 offset);
R_API ut64 r_io_section_get_offset(RIO *io, ut64 offset);
R_API ut64 r_io_section_get_vaddr(RIO *io, ut64 offset);
R_API int r_io_section_get_rwx(RIO *io, ut64 offset);
@ -316,10 +311,9 @@ R_API void r_io_desc_init(RIO *io);
R_API void r_io_desc_fini(RIO *io);
R_API RIODesc *r_io_desc_new(RIOPlugin *plugin, int fd, const char *name, int flags, int mode, void *data);
R_API void r_io_desc_free(RIODesc *desc);
//R_API void r_io_desc_add(RIO *io, RIODesc *desc);
R_API int r_io_desc_del(RIO *io, int fd);
R_API RIODesc *r_io_desc_get(RIO *io, int fd);
R_API void r_io_desc_add(RIO *io, RIODesc *desc); //int fd, const char *file, int flags, struct r_io_plugin_t *plugin);
R_API int r_io_desc_add(RIO *io, RIODesc *desc);
R_API int r_io_desc_del(RIO *io, int fd);
R_API RIODesc *r_io_desc_get(RIO *io, int fd);
//R_API int r_io_desc_generate(RIO *io);

5
libr/io/README Normal file
View File

@ -0,0 +1,5 @@
RIO design
==========
rio api allows to seamlessly access to underlying io backends
and define rules on top of it to act as an mmu.

View File

@ -13,8 +13,10 @@ R_API void r_io_desc_fini(RIO *io) {
}
R_API RIODesc *r_io_desc_new(RIOPlugin *plugin, int fd, const char *name, int flags, int mode, void *data) {
int i;
RIODesc *desc = R_NEW (RIODesc);
if (!desc) return NULL;
if (fd<0) eprintf ("r_io_desc_new: Warning: fd '%s' is -1\n", name);
desc->state = R_IO_DESC_TYPE_OPENED;
desc->name = strdup (name);
if (desc->name == NULL) {
@ -23,10 +25,12 @@ R_API RIODesc *r_io_desc_new(RIOPlugin *plugin, int fd, const char *name, int fl
}
desc->plugin = plugin;
desc->flags = flags;
if (fd == -1) {
if (fd <0) {
ut8 *p = (ut8 *)&(desc->fd);
desc->fd = ((int) ((size_t) desc) & 0xffffff);
desc->fd = p[0]^p[1]^p[2]^p[3];
desc->fd = p[0];
for (i=1; i<sizeof (desc->fd); i++)
desc->fd ^= p[i];
} else desc->fd = fd;
desc->data = data;
return desc;
@ -40,13 +44,16 @@ R_API void r_io_desc_free(RIODesc *desc) {
free (desc->name);
desc->name = NULL;
}
// free (desc); double free orw at
}
R_API void r_io_desc_add(RIO *io, RIODesc *desc) {
r_list_append (io->desc, desc);
R_API int r_io_desc_add(RIO *io, RIODesc *desc) {
RIODesc *foo = r_io_desc_get (io, desc->fd);
if (foo) r_list_append (io->desc, desc);
return foo? 1: 0;
}
R_API int r_io_desc_del(struct r_io_t *io, int fd) {
R_API int r_io_desc_del(RIO *io, int fd) {
RListIter *iter;
RIODesc *d;
/* No _safe loop necessary because we return immediately after the delete. */

View File

@ -87,57 +87,63 @@ R_API RIODesc *r_io_open_as(struct r_io_t *io, const char *urihandler, const cha
return ret;
}
R_API RIODesc *r_io_open(RIO *io, const char *file, int flags, int mode) {
struct r_io_plugin_t *plugin = NULL;
static inline RIODesc *__getioplugin(RIO *io, const char *uri, int flags, int mode) {
RIOPlugin *plugin, *iop = NULL;
RIODesc *desc = NULL;
int fd = -2;
char *uri;
if (!io) return NULL;
io->plugin = NULL;
uri = strdup (file);
for (;;) {
plugin = r_io_plugin_resolve (io, uri);
if (plugin && plugin->open) {
desc = plugin->open (io, uri, flags, mode);
if (io->redirect) {
// TODO: free desc if not null
free ((void *)uri);
uri = strdup (io->redirect);
uri = io->redirect;
r_io_redirect (io, NULL);
continue;
}
if (desc != NULL) {
r_io_desc_add (io, desc);
fd = desc->fd;
if (fd != -1)
r_io_plugin_open (io, fd, plugin);
if (desc->fd != -1)
r_io_plugin_open (io, desc->fd, plugin);
if (desc != io->fd)
io->plugin = plugin;
iop = plugin;
}
}
break;
}
if (fd == -2) {
io->plugin = iop;
return desc;
}
static int __io_posix_open (RIO *io, const char *file, int flags, int mode) {
int fd;
#if __WINDOWS__
if (flags & R_IO_WRITE) {
fd = r_sandbox_open (uri, O_BINARY | 1, 0);
if (fd == -1)
r_sandbox_creat (uri, O_BINARY);
fd = r_sandbox_open (uri, O_BINARY | 1, 0);
} else fd = r_sandbox_open (uri, O_BINARY, 0);
if (flags & R_IO_WRITE) {
fd = r_sandbox_open (file, O_BINARY | 1, 0);
if (fd == -1)
r_sandbox_creat (file, O_BINARY);
fd = r_sandbox_open (file, O_BINARY | 1, 0);
} else fd = r_sandbox_open (file, O_BINARY, 0);
#else
fd = r_sandbox_open (uri, (flags&R_IO_WRITE)?
O_RDWR: O_RDONLY, mode);
fd = r_sandbox_open (file, (flags&R_IO_WRITE)?
(O_RDWR|O_CREAT): O_RDONLY, mode);
#endif
return fd;
}
R_API RIODesc *r_io_open(RIO *io, const char *file, int flags, int mode) {
RIODesc *desc = __getioplugin (io, file, flags, mode);
int fd;
if (desc) {
fd = desc->fd;
} else {
fd = __io_posix_open (io, file, flags, mode);
if (fd>=0)
desc = r_io_desc_new (io->plugin,
fd, file, flags, mode, NULL);
}
if (fd >= 0) {
if (desc == NULL)
desc = r_io_desc_new (io->plugin,
fd, uri, flags, mode, NULL);
r_io_desc_add (io, desc);
r_io_set_fd (io, desc);
}
free ((void *)uri);
return desc;
}

View File

@ -75,8 +75,8 @@ R_API char *r_socket_http_get (const char *url, int *code, int *rlen) {
"GET /%s HTTP/1.1\r\n"
"User-Agent: radare2 "R2_VERSION"\r\n"
"Accept: */*\r\n"
"Host: %s\r\n"
"\r\n", path, host);
"Host: %s:%s\r\n"
"\r\n", path, host, port);
response = r_socket_http_answer (s, code, rlen);
free (uri);
return response;

View File

@ -387,9 +387,9 @@ R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int ba
ut32 s, a;
a = addr & 0xffff;
s = (addr-a)>>4;
snprintf (soff, sizeof (soff), "%s%04x:%04x", color, s, a);
snprintf (soff, sizeof (soff), "%04x:%04x", s, a);
} else {
snprintf (soff, sizeof (soff), "%s0x%08"PFMT64x, color, addr);
snprintf (soff, sizeof (soff), "0x%08"PFMT64x, addr);
}
delta = strlen (soff) - 10;
for (i=0; i<delta; i++)