* Lot of fixes in the vala/swig wing

- r_util is now bindable from swig !!
  - Added test cases for r_util
  - r_flist has been mirrored in C, to keep API consistent
* Rename RIO->seek into RIO->off
  - There's a method with the same name
  - Also rename list.h ->next and ->prev into ->n ->n
* Apply patch from whats fixing 'r_cmd_str' EOF for stdin (Thanks!!)
  - Added test program to ensure stdin food works
* Allow '-f -' to rasm2 (assemble file from stdin)
* Added test case in python using RBin, RAsm and RCC to compile
  and assemble a code to be injected in the given target program
This commit is contained in:
pancake 2010-02-15 22:59:26 +01:00
parent c738134c67
commit d6f95d33c3
25 changed files with 372 additions and 198 deletions

View File

@ -249,7 +249,7 @@ R_API struct r_asm_code_t* r_asm_mdisassemble(struct r_asm_t *a, ut8 *buf, ut64
}
R_API struct r_asm_code_t* r_asm_massemble(struct r_asm_t *a, const char *buf) {
char *lbuf = NULL, *ptr2, *ptr = NULL, *ptr_start = NULL, *label_name = NULL,
char *lbuf = NULL, *ptr2, *ptr = NULL, *ptr_start = NULL,
*tokens[R_ASM_BUFSIZE], buf_token[R_ASM_BUFSIZE];
int labels = 0, stage, ret, idx, ctr, i, j;
struct r_asm_aop_t aop;
@ -316,7 +316,7 @@ R_API struct r_asm_code_t* r_asm_massemble(struct r_asm_t *a, const char *buf) {
continue;
//eprintf ("LINE %d %s\n", stage, ptr_start);
if (labels) /* Labels */
if (ptr = strchr (ptr_start, ':')) {
if ((ptr = strchr (ptr_start, ':'))) {
char food[64];
if (stage != 0)
continue;
@ -338,9 +338,11 @@ R_API struct r_asm_code_t* r_asm_massemble(struct r_asm_t *a, const char *buf) {
ret = r_asm_pseudo_bits (a, ptr+6);
else if (!memcmp (ptr, ".byte ", 6))
ret = r_asm_pseudo_byte (&aop, ptr+6);
else if (!memcmp (ptr, ".global ", 8))
eprintf (".global directive not yet implemented\n");
else if (!memcmp (ptr, ".equ ", 5)) {
else if (!memcmp (ptr, ".global ", 8)) {
// eprintf (".global directive not yet implemented\n");
ret = 0;
continue;
} else if (!memcmp (ptr, ".equ ", 5)) {
ptr2 = strchr (ptr+5, ',');
if (ptr2) {
*ptr2 = '\0';

View File

@ -203,13 +203,24 @@ int main(int argc, char *argv[])
if (file) {
char *content;
int length;
content = r_file_slurp (file, &length);
if (content) {
content[length] = '\0';
if (dis) ret = rasm_disasm (content, offset, len, ascii, bin);
else ret = rasm_asm (content, offset, len, bin);
free (content);
} else eprintf ("Cannot open file %s\n", file);
if (!strcmp (file, "-")) {
char buf[R_ASM_BUFSIZE]; // TODO: Fix this limitation
ret = fread (buf, 1, R_ASM_BUFSIZE, stdin);
if (ret == R_ASM_BUFSIZE)
eprintf ("WARNING: Cannot slurp more from stdin\n");
if (ret>=0)
buf[ret] = '\0';
if (dis) ret = rasm_disasm (buf, offset, len, ascii, bin);
else ret = rasm_asm (buf, offset, len, bin);
} else {
content = r_file_slurp (file, &length);
if (content) {
content[length] = '\0';
if (dis) ret = rasm_disasm (content, offset, len, ascii, bin);
else ret = rasm_asm (content, offset, len, bin);
free (content);
} else eprintf ("Cannot open file %s\n", file);
}
} else if (argv[optind]) {
if (!strcmp (argv[optind], "-")) {
char buf[R_ASM_BUFSIZE];

View File

@ -36,21 +36,21 @@ static char* file;
static int rabin_show_help() {
printf ("rabin2 [options] [file]\n"
" -e Entrypoint\n"
" -i Imports (symbols imported from libraries)\n"
" -s Symbols (exports)\n"
" -S Sections\n"
" -z Strings\n"
" -I Binary info\n"
" -H Header fields\n"
" -o [str] Write/Extract operations (str=help for help)\n"
" -f [format] Override file format autodetection\n"
" -r Radare output\n"
" -w Open file in rw mode\n"
" -L List supported bin plugins\n"
" -@ [addr] Show section, symbol or import at addr\n"
" -V Show version information\n"
" -h This help\n");
" -e Entrypoint\n"
" -i Imports (symbols imported from libraries)\n"
" -s Symbols (exports)\n"
" -S Sections\n"
" -z Strings\n"
" -I Binary info\n"
" -H Header fields\n"
" -o [str] Write/Extract operations (str=help for help)\n"
" -f [format] Override file format autodetection\n"
" -r Radare output\n"
" -w Open file in rw mode\n"
" -L List supported bin plugins\n"
" -@ [addr] Show section, symbol or import at addr\n"
" -V Show version information\n"
" -h This help\n");
return R_TRUE;
}
@ -76,11 +76,8 @@ static int rabin_show_entrypoints() {
if (rad) {
printf ("f entry%i @ 0x%08llx\n", i, baddr+entry->rva);
printf ("s entry%i\n", i);
} else {
printf ("address=0x%08llx offset=0x%08llx baddr=0x%08llx\n",
baddr+entry->rva, entry->offset, baddr);
}
} else printf ("address=0x%08llx offset=0x%08llx baddr=0x%08llx\n",
baddr+entry->rva, entry->offset, baddr);
i++;
}

View File

@ -1953,15 +1953,15 @@ R_API int r_core_cmd_file(struct r_core_t *core, const char *file)
R_API int r_core_cmd_command(struct r_core_t *core, const char *command)
{
int len;
char *buf = r_sys_cmd_str (command, 0, &len);
char *rcmd = buf;
char *ptr = buf;
char *buf, *rcmd, *ptr;
rcmd = ptr = buf = r_sys_cmd_str (command, 0, &len);
if (buf == NULL)
return -1;
while ((ptr = strstr (rcmd, "\n"))) {
*ptr = '\0';
if (r_core_cmd (core, rcmd, 0) == -1) {
eprintf ("Error running command '%s'\n", rcmd);
break;
}
rcmd += strlen (rcmd)+1;
}
@ -1998,7 +1998,9 @@ static int cmd_debug(void *data, const char *input) {
char *ptr;
switch (input[0]) {
case 'x':
r_debug_execute (&core->dbg, "\xc7\xc0\x03\x00\x00\x00\x33\xdb\x33\xcc\xc7\xc2\x10\x00\x00\x00\xcd\x80", 18);
r_debug_execute (&core->dbg, (ut8*)
"\xc7\xc0\x03\x00\x00\x00\x33\xdb\x33"
"\xcc\xc7\xc2\x10\x00\x00\x00\xcd\x80", 18);
break;
case 'k':
/* XXX: not for threads? signal is for a whole process!! */

View File

@ -5,15 +5,25 @@
#include <stdlib.h>
#include <string.h>
//#include <r_types.h>
#define r_flist_t void**
#define RFList void**
#define r_flist_rewind(it) for (; it!=*it; it--); it++
#ifdef R_API
#define r_flist_rewind(it) for (; it!=*it; it--); it++;
#define r_flist_next(it) *it!=0
#define r_flist_get(it) *(it++)
#define r_flist_iterator(x) x
#define r_flist_unref(x) x
#define r_flist_iterator(x) x
/*
static inline void **r_flist_iterator(void **it) {
r_flist_iterator(it);
return it;
}
*/
static inline void **r_flist_init(void **it, int n) {
*it = it;
memset (++it, 0, (n+1) * sizeof (void*));
@ -54,5 +64,6 @@ static inline void r_flist_free(void **it) {
r_flist_rewind (it);
free (--it);
}
#endif
#endif

View File

@ -55,7 +55,7 @@ typedef struct r_io_t {
int enforce_seek;
int cached;
int cached_read;
ut64 seek;
ut64 off;
char *redirect;
/* write mask */
void (*printf)(const char *str, ...);

View File

@ -5,7 +5,7 @@ typedef void (*RListFree)(void *ptr);
typedef struct r_list_iter_t {
void *data;
struct r_list_iter_t *next, *prev;
struct r_list_iter_t *n, *p;
} RListIter;
typedef struct r_list_t {
@ -14,6 +14,8 @@ typedef struct r_list_t {
RListFree free;
} RList;
#ifdef R_API
#define r_list_iterator(x) x->head
#define r_list_iter_free(x) free(x)
#define r_list_item_free(x) free(x)
@ -21,18 +23,17 @@ typedef struct r_list_t {
#define r_list_empty(x) (x->head==NULL && x->tail==NULL)
#define r_list_head(x) x->head
#define r_list_tail(x) x->tail
#define r_list_iter_get(x) x->data; x=x->next
#define r_list_iter_get(x) x->data; x=x->n
#define r_list_iter_next(x) (x?1:0)
RList *r_list_new();
RListIter *r_list_append(RList *list, void *data);
RListIter *r_list_prepend(RList *list, void *data);
#ifdef R_API
R_API void r_list_init(RList *list);
R_API void r_list_delete (RList *list, RListIter *item);
R_API RList *r_list_new();
R_API void r_list_iter_init (RListIter *iter, RList *list);
R_API void r_list_destroy (RList *list);
R_API RListIter *r_list_item_new (void *data);
R_API RListIter *r_list_append(RList *list, void *data);
R_API RListIter *r_list_prepend(RList *list, void *data);
R_API void r_list_unlink (RList *list, void *ptr);
#endif

View File

@ -1,14 +1,19 @@
#ifndef _INCLUDE_UTIL_R_
#define _INCLUDE_UTIL_R_
#include "r_types.h"
#include <r_types.h>
#include <btree.h>
#include "r_list.h" // radare linked list
#include "r_flist.h" // radare fixed pointer array iterators
#include "list.h" // kernel linked list
#include <r_list.h> // radare linked list
#include <r_flist.h> // radare fixed pointer array iterators
#include <list.h> // kernel linked list
/* profiling */
#include <sys/time.h>
/* empty classes */
typedef struct { } RSystem;
typedef struct { } RString;
typedef struct { } RLog;
/* pool */
typedef struct r_mem_pool_t {
void **nodes;
@ -56,17 +61,18 @@ typedef struct r_num_t {
typedef ut64 (*RNumCallback)(RNum *self, const char *str, int *ok);
#ifdef R_API
R_API struct r_num_t *r_num_new(RNumCallback *cb, void *ptr);
//R_API RBuffer *r_num_new(RNumCallback *cb, void *ptr);
R_API RNum *r_num_new(RNumCallback cb, void *ptr);
#define R_BUF_CUR -1
R_API struct r_buf_t *r_buf_init(struct r_buf_t *b);
R_API struct r_buf_t *r_buf_new();
R_API int r_buf_set_bits(struct r_buf_t *b, int bitoff, int bitsize, ut64 value);
R_API int r_buf_set_bytes(struct r_buf_t *b, ut8 *buf, int length);
R_API int r_buf_read_at(struct r_buf_t *b, ut64 addr, ut8 *buf, int len);
R_API int r_buf_fread_at(struct r_buf_t *b, ut64 addr, ut8 *buf, const char *fmt, int n);
R_API int r_buf_write_at(struct r_buf_t *b, ut64 addr, const ut8 *buf, int len);
R_API void r_buf_free(struct r_buf_t *b);
R_API RBuffer *r_buf_init(RBuffer *b);
R_API RBuffer *r_buf_new();
R_API int r_buf_set_bits(RBuffer *b, int bitoff, int bitsize, ut64 value);
R_API int r_buf_set_bytes(RBuffer *b, ut8 *buf, int length);
R_API int r_buf_read_at(RBuffer *b, ut64 addr, ut8 *buf, int len);
R_API int r_buf_fread_at(RBuffer *b, ut64 addr, ut8 *buf, const char *fmt, int n);
R_API int r_buf_write_at(RBuffer *b, ut64 addr, const ut8 *buf, int len);
R_API void r_buf_free(RBuffer *b);
R_API struct r_mem_pool_t* r_mem_pool_deinit(struct r_mem_pool_t *pool);
R_API struct r_mem_pool_t* r_mem_pool_init(struct r_mem_pool_t *pool, int nodesize, int poolsize, int poolcount);
@ -111,6 +117,7 @@ R_API void r_num_init(struct r_num_t *num);
#define ishexchar(x) ((x>='0'&&x<='9') || (x>='a'&&x<='f') || (x>='A'&&x<='F')) {
/* stabilized */
R_API char *r_str_new(char *str);
R_API const char *r_str_bool(int b);
R_API const char *r_str_ansi_chrn(const char *str, int n);
R_API int r_str_ansi_len(const char *str);
@ -148,7 +155,7 @@ R_API int r_str_escape(char *buf);
R_API char *r_str_home(const char *str);
R_API char *r_str_concat(char *ptr, const char *string);
R_API char *r_str_concatf(char *ptr, const char *fmt, ...);
R_API inline void r_str_concatch(char *x, char y);
R_API void r_str_concatch(char *x, char y);
/* hex */
R_API int r_hex_pair2bin(const char *arg);
@ -174,11 +181,17 @@ R_API const char *r_sys_getenv(const char *key);
R_API int r_sys_setenv(const char *key, const char *value, int ow);
R_API char *r_sys_cmd_str_full(const char *cmd, const char *input, int *len, char **sterr);
R_API int r_sys_cmd(const char *cmd);
#define r_sys_cmd_str(cmd, input, len) r_sys_cmd_str_full(cmd, input, len, 0)
R_API char *r_sys_cmd_str(const char *cmd, const char *input, int *len);
//#define r_sys_cmd_str(cmd, input, len) r_sys_cmd_str_full(cmd, input, len, 0)
R_API int r_alloca_init();
R_API ut8 *r_alloca_bytes(int len);
R_API char *r_alloca_str(const char *str);
R_API int r_alloca_ret_i(int n);
/* LOG */
R_API int r_log_msg(const char *str);
R_API int r_log_error(const char *str);
R_API int r_log_progress(const char *str, int percent);
#endif
#endif

View File

@ -127,11 +127,11 @@ R_API int r_io_read(struct r_io_t *io, ut8 *buf, int len)
{
int ret;
/* check section permissions */
if (io->enforce_rwx && !(r_io_section_get_rwx(io, io->seek) & R_IO_READ))
if (io->enforce_rwx && !(r_io_section_get_rwx(io, io->off) & R_IO_READ))
return -1;
if (io->cached) {
ret = r_io_cache_read(io, io->seek, buf, len);
ret = r_io_cache_read(io, io->off, buf, len);
if (ret == len)
return len;
if (ret > 0) {
@ -142,7 +142,7 @@ R_API int r_io_read(struct r_io_t *io, ut8 *buf, int len)
if (ret == len)
return len;
}
ret = r_io_map_read_at (io, io->seek, buf, len);
ret = r_io_map_read_at (io, io->off, buf, len);
// partial reads
if (ret != len) {
@ -162,7 +162,7 @@ R_API int r_io_read(struct r_io_t *io, ut8 *buf, int len)
/* if read is cached. cache it :) */
if (ret != -1 && ret == len && io->cached_read)
r_io_cache_write (io, io->seek, buf, len);
r_io_cache_write (io, io->off, buf, len);
return ret;
}
@ -220,11 +220,11 @@ R_API int r_io_write(struct r_io_t *io, const ut8 *buf, int len)
int i, ret = -1;
/* check section permissions */
if (io->enforce_rwx && !(r_io_section_get_rwx(io, io->seek) & R_IO_WRITE))
if (io->enforce_rwx && !(r_io_section_get_rwx(io, io->off) & R_IO_WRITE))
return -1;
if (io->cached) {
ret = r_io_cache_write(io, io->seek, buf, len);
ret = r_io_cache_write(io, io->off, buf, len);
if (ret == len)
return len;
if (ret > 0) {
@ -238,9 +238,9 @@ R_API int r_io_write(struct r_io_t *io, const ut8 *buf, int len)
/* apply write binary mask */
if (io->write_mask_fd != -1) {
ut8 *data = alloca(len);
r_io_seek(io, io->seek, R_IO_SEEK_SET);
r_io_seek(io, io->off, R_IO_SEEK_SET);
r_io_read(io, data, len);
r_io_seek(io, io->seek, R_IO_SEEK_SET);
r_io_seek(io, io->off, R_IO_SEEK_SET);
for(i=0;i<len;i++) {
data[i] = buf[i] & \
io->write_mask_buf[i%io->write_mask_len];
@ -248,7 +248,7 @@ R_API int r_io_write(struct r_io_t *io, const ut8 *buf, int len)
buf = data;
}
if (r_io_map_write_at(io, io->seek, buf, len) != 0)
if (r_io_map_write_at(io, io->off, buf, len) != 0)
return len;
if (io->plugin) {
if (io->plugin->write)
@ -275,28 +275,28 @@ R_API ut64 r_io_seek(struct r_io_t *io, ut64 offset, int whence)
case R_IO_SEEK_SET:
/* TODO: Deprecate remove section align ?? */
offset = r_io_section_align (io, offset, 0, 0);
io->seek = offset;
io->off = offset;
posix_whence = SEEK_SET;
break;
case R_IO_SEEK_CUR:
io->seek += offset;
io->off += offset;
posix_whence = SEEK_CUR;
break;
case R_IO_SEEK_END:
io->seek = UT64_MAX; // XXX: depending on io bitz?
io->off = UT64_MAX; // XXX: depending on io bitz?
posix_whence = SEEK_END;
break;
}
// TODO: implement io->enforce_seek here!
if (io->plugin && io->plugin->lseek)
io->seek = io->plugin->lseek (io, io->fd, offset, whence);
io->off = io->plugin->lseek (io, io->fd, offset, whence);
// XXX can be problematic on w32..so no 64 bit offset?
else io->seek = lseek (io->fd, offset, posix_whence);
else io->off = lseek (io->fd, offset, posix_whence);
r_io_sundo_push (io);
return io->seek;
return io->off;
}
R_API ut64 r_io_size(struct r_io_t *io, int fd)

View File

@ -34,14 +34,14 @@ R_API void r_io_undo_enable(struct r_io_t *io, int s, int w)
R_API ut64 r_io_sundo_last(struct r_io_t *io)
{
return (io->undo.idx>0)?
io->undo.seek[io->undo.idx-2] : io->seek;
io->undo.seek[io->undo.idx-2] : io->off;
}
R_API void r_io_sundo(struct r_io_t *io)
{
if (--io->undo.idx<0)
io->undo.idx = 0;
else io->seek = io->undo.seek[io->undo.idx-1];
else io->off = io->undo.seek[io->undo.idx-1];
}
R_API void r_io_sundo_redo(struct r_io_t *io)
@ -57,9 +57,9 @@ R_API void r_io_sundo_push(struct r_io_t *io)
int i;
if (!io->undo.s_enable)
return;
if (io->undo.seek[io->undo.idx-1] == io->seek)
if (io->undo.seek[io->undo.idx-1] == io->off)
return;
io->undo.seek[io->undo.idx] = io->seek;
io->undo.seek[io->undo.idx] = io->off;
if (io->undo.idx==R_IO_UNDOS-1) {
for(i=1;i<R_IO_UNDOS;i++)
io->undo.seek[i-1] = io->undo.seek[i];

View File

@ -1,6 +1,6 @@
NAME=r_util
include ../config.mk
OBJ=mem.o pool.o num.o str.o re.o hex.o file.o alloca.o
OBJ+=float.o prof.o cache.o sys.o btree.o buf.o list.o
OBJ+=float.o prof.o cache.o sys.o btree.o buf.o list.o flist.o
include ../rules.mk

56
libr/util/flist.c Normal file
View File

@ -0,0 +1,56 @@
/* radare - LGPL - Copyright 2010 pancake <@nopcode.org> */
#include <r_types.h>
//#include <r_flist.h>
// NOTE: reimplemnetation of r_flist in C (if no R_API defined)
#if 1
#define r_flist_t void**
#define RFList void**
#define r_flist_rewind(it) for (; it!=*it; it--); it++
#define r_flist_next(it) *it!=0
#define r_flist_get(it) *(it++)
#define r_flist_iterator(x) x
#define r_flist_unref(x) x
#endif
R_API void **r_flist_init(void **it, int n) {
*it = it;
memset (++it, 0, (n+1) * sizeof (void*));
return it;
}
R_API void **r_flist_new(int n) {
void **it;
if (!(it = (void **)malloc ((n+2) * sizeof (void*))))
return NULL;
return r_flist_init (it, n);
}
R_API void **r_flist_prev(void **it) {
void **p = it--;
return (it==*it)?p:it;
}
R_API void r_flist_set(void **it, int idx, void *data) {
r_flist_rewind (it);
it[idx] = data;
}
R_API void r_flist_delete(void **it, int idx) {
r_flist_rewind (it);
free (it[idx]);
for(it += idx; *it; it++) *it = *(it+1);
}
#define r_flist_foreach(it, pos) \
r_flist_rewind(it); \
while (r_flist_next (it) && (pos = r_flist_get (it)))
R_API void r_flist_free(void **it) {
void *pos;
r_flist_foreach (it, pos)
free (pos);
r_flist_rewind (it);
free (--it);
}

View File

@ -19,13 +19,13 @@ R_API void r_list_unlink (RList *list, void *ptr) {
R_API void r_list_delete (RList *list, RListIter *item) {
if (list->head == item)
list->head = item->next;
list->head = item->n;
if (list->tail == item)
list->tail = item->prev;
if (item->prev)
item->prev->next = item->next;
if (item->next)
item->next->prev = item->prev;
list->tail = item->p;
if (item->p)
item->p->n = item->n;
if (item->n)
item->n->p = item->p;
if (list->free && item->data) {
list->free (item->data);
item->data = NULL;
@ -44,7 +44,7 @@ R_API void r_list_destroy (RList *list) {
if (list->free) {
RListIter *it = list->head;
while (it) {
RListIter *next = it->next;
RListIter *next = it->n;
r_list_delete (list, it);
it = next;
}
@ -64,10 +64,10 @@ R_API RListIter *r_list_append(RList *list, void *data) {
if (data) {
new = R_NEW (RListIter);
if (list->tail)
list->tail->next = new;
list->tail->n = new;
new->data = data;
new->prev = list->tail;
new->next = NULL;
new->p = list->tail;
new->n = NULL;
list->tail = new;
if (list->head == NULL)
list->head = new;
@ -78,10 +78,10 @@ R_API RListIter *r_list_append(RList *list, void *data) {
R_API RListIter *r_list_prepend(RList *list, void *data) {
RListIter *new = MALLOC_STRUCT (RListIter);
if (list->head)
list->head->prev = new;
list->head->p = new;
new->data = data;
new->next = list->head;
new->prev = NULL;
new->n = list->head;
new->p = NULL;
list->head = new;
if (list->tail == NULL)
list->tail = new;
@ -132,7 +132,7 @@ int main () {
{
RListIter* i = r_list_iterator (l);
for (; i; i = i->next) {
for (; i; i = i->n) {
char *str = i->data;
printf (" * %s\n", str);
}

View File

@ -20,14 +20,12 @@ R_API ut64 r_num_htonq(ut64 value) {
return ret;
}
R_API int r_num_rand(int max)
{
R_API int r_num_rand(int max) {
// TODO: add srand here for security and so on
return rand()%max;
}
R_API void r_num_minmax_swap(ut64 *a, ut64 *b)
{
R_API void r_num_minmax_swap(ut64 *a, ut64 *b) {
if (*a>*b) {
ut64 tmp = *a;
*a = *b;
@ -35,8 +33,7 @@ R_API void r_num_minmax_swap(ut64 *a, ut64 *b)
}
}
R_API void r_num_minmax_swap_i(int *a, int *b)
{
R_API void r_num_minmax_swap_i(int *a, int *b) {
if (*a>*b) {
ut64 tmp = *a;
*a = *b;
@ -44,24 +41,21 @@ R_API void r_num_minmax_swap_i(int *a, int *b)
}
}
R_API void r_num_init(struct r_num_t *num)
{
R_API void r_num_init(struct r_num_t *num) {
num->callback = NULL;
num->userptr = NULL;
num->value = 0LL;
}
R_API struct r_num_t *r_num_new(RNumCallback *cb, void *ptr)
{
struct r_num_t *num;
num = (struct r_num_t*) malloc(sizeof(struct r_num_t));
r_num_init(num);
R_API RNum *r_num_new(RNumCallback cb, void *ptr) {
RNum *num = (RNum *) malloc (sizeof (RNum));
r_num_init (num);
num->callback = cb;
return num;
}
/* old get_offset */
R_API ut64 r_num_get(struct r_num_t *num, const char *str)
{
R_API ut64 r_num_get(struct r_num_t *num, const char *str) {
int i, j;
char lch;
ut64 ret = 0LL;
@ -120,8 +114,7 @@ R_API ut64 r_num_get(struct r_num_t *num, const char *str)
return ret;
}
R_API ut64 r_num_op(char op, ut64 a, ut64 b)
{
R_API ut64 r_num_op(char op, ut64 a, ut64 b) {
IFDBG printf("r_num_op: %lld %c %lld\n", a,op,b);
switch(op) {
case '+': return a+b;
@ -135,16 +128,15 @@ R_API ut64 r_num_op(char op, ut64 a, ut64 b)
return b;
}
R_API static ut64 r_num_math_internal(struct r_num_t *num, char *s)
{
R_API static ut64 r_num_math_internal(struct r_num_t *num, char *s) {
ut64 ret = 0LL;
char *p = s;
int i, nop, op='\0';
IFDBG printf("r_num_math_internal: %s\n", s);
IFDBG printf ("r_num_math_internal: %s\n", s);
for(i=0;s[i];i++) {
switch(s[i]) {
for (i=0; s[i]; i++) {
switch (s[i]) {
case '+':
case '-':
case '*':
@ -153,31 +145,31 @@ R_API static ut64 r_num_math_internal(struct r_num_t *num, char *s)
case '^':
case '|':
nop = s[i]; s[i] = '\0';
ret = r_num_op(op, ret, r_num_get(num, p));
ret = r_num_op (op, ret, r_num_get (num, p));
op = s[i] = nop; p = s + i + 1;
break;
}
}
return r_num_op(op, ret, r_num_get(num, p));
return r_num_op (op, ret, r_num_get (num, p));
}
R_API ut64 r_num_math(struct r_num_t *num, const char *str)
{
ut64 ret = 0LL;
char op = '+';
int len = strlen(str)+1;
char *p, *s = alloca(len);
int len = strlen (str)+1;
char *p, *s = alloca (len);
char *group;
IFDBG printf("r_num_math: %s\n", str);
IFDBG printf ("r_num_math: %s\n", str);
memcpy(s, str, len);
for(;*s==' ';s+=1);
memcpy (s, str, len);
for (; *s==' '; s++);
p = s;
do {
group = strchr(p, '(');
group = strchr (p, '(');
if (group) {
group[0]='\0';
ret = r_num_op(op, ret, r_num_math_internal(num, p));

View File

@ -10,6 +10,7 @@
R_API char *r_sys_cmd_strf(const char *cmd, ...)
{
// FIXME Implement r_sys_cmd_strf
return NULL;
}
@ -65,10 +66,11 @@ R_API char *r_sys_cmd_str_full(const char *cmd, const char *input, int *len, cha
int pid = fork();
if (!pid) {
dup2(sh_in[0], 0);
dup2(sh_out[1], 1);
dup2(sh_in[0], 0); close(sh_in[0]); close(sh_in[1]);
dup2(sh_out[1], 1); close(sh_out[0]); close(sh_out[1]);
if (sterr) dup2(sh_err[1], 2);
else close(2);
close(sh_err[0]); close(sh_err[1]);
execl("/bin/sh", "sh", "-c", cmd, NULL);
} else {
char buffer[1024];
@ -76,41 +78,47 @@ R_API char *r_sys_cmd_str_full(const char *cmd, const char *input, int *len, cha
if (sterr)
*sterr = calloc(1, 1024);
close(sh_out[1]);
close(sh_err[1]);
close(sh_in[0]);
if (!inputptr || !*inputptr)
close(sh_in[1]);
while (1) {
fd_set rfds, wfds;
int nfd;
struct timeval tv;
tv.tv_sec=0;
tv.tv_usec=100000;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_SET(sh_out[0], &rfds);
FD_SET(sh_err[0], &rfds);
if (sterr)
FD_SET(sh_err[0], &rfds);
if (inputptr && *inputptr)
FD_SET(sh_in[1], &wfds);
memset(buffer, 0, sizeof(buffer));
nfd = select(sh_err[0] + 1, &rfds, &wfds, NULL, &tv);
if (nfd <= 0) {
if (waitpid(pid, NULL, WNOHANG)) break;
else if (nfd < 0) {
kill(pid, 15);
break;
}
nfd = select(sh_err[0] + 1, &rfds, &wfds, NULL, NULL);
if (nfd < 0) {
break;
} else {
if (FD_ISSET(sh_out[0], &rfds)) {
*len += read(sh_out[0], buffer, sizeof(buffer)-1);
if ((bytes = read(sh_out[0], buffer, sizeof(buffer)-1)) == 0) break;
*len += bytes;
output = r_str_concat(output, buffer);
} else if (FD_ISSET(sh_err[0], &rfds) && sterr) {
read(sh_err[0], buffer, sizeof(buffer)-1);
if (read(sh_err[0], buffer, sizeof(buffer)-1) == 0) break;
*sterr = r_str_concat(*sterr, buffer);
} else if (FD_ISSET(sh_in[1], &wfds) && inputptr && *inputptr) {
bytes = write(sh_in[1], inputptr, strlen(inputptr));
inputptr += bytes;
if (!*inputptr) close(sh_in[1]);
}
}
}
close(sh_out[0]);
close(sh_err[0]);
close(sh_in[1]);
if (strlen(output))
return output;
}
@ -126,3 +134,7 @@ R_API int r_sys_cmd (const char *str)
/* TODO: implement for other systems */
return system (str);
}
R_API char *r_sys_cmd_str(const char *cmd, const char *input, int *len) {
return r_sys_cmd_str_full (cmd, input, len, NULL);
}

View File

@ -16,6 +16,8 @@ BINS+=file_slurp_hexpairs${EXT_EXE}
BINS+=pool${EXT_EXE}
BINS+=test_sys${EXT_EXE}
BINS+=test_str${EXT_EXE}
BINS+=test_file_slurp_hexpairs${EXT_EXE}
BINS+=test_cmd_str${EXT_EXE}
all: ${BINS}
@ -40,8 +42,11 @@ rax2${EXT_EXE}:
array${EXT_EXE}:
${CC} ${FLAGS} array.c ${EFLAGS} -o array${EXT_EXE}
file_slurp_hexpairs${EXT_EXE}:
${CC} ${FLAGS} test_file_slurp_hexpairs.c ${EFLAGS} -o file_slurp_hexpairs${EXT_EXE}
test_file_slurp_hexpairs${EXT_EXE}:
${CC} ${FLAGS} test_file_slurp_hexpairs.c ${EFLAGS} -o test_file_slurp_hexpairs${EXT_EXE}
test_cmd_str${EXT_EXE}:
${CC} ${FLAGS} test_cmd_str.c ${EFLAGS} -o test_cmd_str${EXT_EXE}
myclean:
rm -f ${BINS}

View File

@ -0,0 +1,8 @@
#include <r_util.h>
int main(){
int out;
printf ("%s\n", r_sys_cmd_str("less","hello world\nhow are you\n", &out));
printf ("out=%d\n", out);
return 0;
}

View File

@ -57,11 +57,9 @@ public class Radare.RAsm {
public void *aux;
public RAsm();
public weak RAsm init();
public int list();
public bool use(string name);
// public bool set_arch(Asm.Arch arch);
public bool set_bits(int bits);
public bool set_syntax(Syntax syntax);
public bool set_pc(uint64 addr);
@ -76,5 +74,5 @@ public class Radare.RAsm {
// This is the destructor
public void free();
public static delegate int parse_cb(RAsm a);
public delegate int parse_cb();
}

View File

@ -23,6 +23,7 @@ namespace Radare {
public unowned RIO init();
public bool set_write_mask(uint8 *buf, int len);
//public uint64 off;
/**
* Open a file using an uri specifying flags and mode
*
@ -36,11 +37,12 @@ namespace Radare {
public int read_at(uint64 addr, uint8 *buf, int len);
public RBuffer *read_buf(uint64 addr, int len);
public int write(uint8 *buf, int len);
public uint64 seek(int fd, uint64 addr, int whence);
public uint64 seek(uint64 addr, int whence);
public int system(string cmd);
public int close(int fd);
public uint64 size(int fd);
/* undo */
public void undo_enable(bool set, bool write);
//public uint64 undo_seek();

View File

@ -1,5 +1,6 @@
/* radare - LGPL - Copyright 2009 pancake<@nopcode.org> */
namespace Radare {
//[Compact]
//[CCode (cheader_filename="r_util.h", cprefix="r_util_")]
//public static class Radare.RUtil {
@ -16,48 +17,57 @@
//public static int offsetof(void *type, void *member);
//}
// ??? wtf
[CCode (cheader_filename="r_util.h", cprefix="r_str_")]
public static class RString {
public RString();
public int hash(string str);
}
#if FAILFAIL
[CCode (cheader_filename="r_util.h", cprefix="r_str_")]
public static class RString {
public RString(string arg);
public static int hash(string str);
}
[CCode (cheader_filename="r_util.h", cprefix="r_num_")]
public static class RNum {
public RNum(RNumCallback cb, void *user);
public uint64 get(string str);
public uint64 math(string str);
}
public delegate uint64 RNumCallback (RNum num, string str, int *ok);
[CCode (cheader_filename="r_util.h", cname="", cprefix="r_log_", free_function="")]
public static class RLog {
public static bool msg(string str);
public static bool err(string str);
}
#endif
[CCode (cheader_filename="r_util.h", cprefix="r_log_")]
public static class RLog {
public bool msg(string str);
public bool err(string str);
}
[CCode (cheader_filename="r_util.h", cprefix="r_sys_", free_function="")]
public static class RSystem {
public static int sleep (int secs);
public static int usleep (int usecs);
public static weak string getenv (string key);
//public static string cmd_str_full(string str, string input = "", out int len = null, out string sterr = null);
public static int cmd (string command);
public static string cmd_str (string command, string? input, out int len=null);
}
[Compact]
[CCode (cname="RBuffer", cheader_filename="r_util.h", cprefix="r_buf_")]
public class RBuffer {
public RBuffer();
public int read_at(uint64 addr, uint8 *buf, int len);
public int write_at(uint64 addr, uint8 *buf, int len);
public bool set_bytes(uint8 *buf, int len);
//public bool memcpy(uint64 addr, uint8 *dst, uint8 *src, int len);
/* ... */
[CCode (cheader_filename="r_util.h", cprefix="r_num_", free_function="")]
public static class RNum {
public RNum(RNumCallback cb, void *user);
public uint64 get(string str);
public uint64 math(string str);
}
[CCode (cname="RNumCallback")]
public static delegate uint64 RNumCallback (string str, int *ok);
[Compact]
[CCode (cname="RBuffer", cheader_filename="r_util.h", cprefix="r_buf_", free_function="r_buf_free")]
public static class RBuffer {
public RBuffer();
public int read_at(uint64 addr, uint8 *buf, int len);
public int write_at(uint64 addr, uint8 *buf, int len);
public bool set_bytes(uint8 *buf, int len);
//public bool memcpy(uint64 addr, uint8 *dst, uint8 *src, int len);
// ..
}
}
/* Generic Iterator interfaced with r_flist */
//[Compact] // XXX: Do not uncomment this...or generated vala code sucks and segfaults
[CCode (cprefix="r_flist_", cheader_filename="r_flist.h", cname="void*")]
public static class RFList<G> {
[CCode (cname="r_flist_iterator")]
public class RFList<G> {
public RFList<G> iterator();
[CCode (cname="r_flist_unref")]
public void unref(G *arg);
[CCode (cname="r_flist_next")]
public bool next();
[CCode (cname="r_flist_get")]
public unowned G @get();
}
@ -113,3 +123,4 @@ public static class rArray<G> {
public rArray<G> iterator();
}
*/
//}

View File

@ -2,8 +2,8 @@
all: bin lang core regs hash sc socket asm search db io array list
@true
bin:
valac --vapidir=.. bin.vala --pkg r_bin --pkg r_util
#bin:
# valac --vapidir=.. bin.vala --pkg r_bin --pkg r_util
bintest:
valac --vapidir=.. bintest.vala --pkg r_bin --pkg r_util

View File

@ -2,24 +2,16 @@
using Radare;
public class RBinExample
{
public static int main(string[] args)
{
uint64 baddr;
public void main (string[] args) {
if (args.length != 2)
error("Usage: %s <file>\n", args[0]);
if (args.length != 2)
error("Usage: %s <file>\n", args[0]);
var bin = new RBin ();
if (bin.load (args[1], null) != 1)
error ("Cannot open binary file\n");
RBin bin = new RBin();
if (bin.load(args[1], null) != 1)
error("Cannot open binary file\n");
baddr = bin.get_baddr();
print("Base addr: 0x%08llx\n", baddr);
foreach (RBin.Symbol sym in bin.get_symbols())
print("0x%08llx - %s\n", baddr+sym.rva, sym.name);
return 0;
}
uint64 baddr = bin.get_baddr();
print ("Base addr: 0x%08llx\n", baddr);
foreach (var sym in bin.get_symbols ())
print ("0x%08llx - %s\n", baddr+sym.rva, sym.name);
}

View File

@ -1,6 +1,11 @@
#!/bin/sh
LNG=$1
if [ -z "$LNG" ]; then
echo "Usage: do-test.sh [lang] [files]"
exit 1
fi
if [ ! -d "test/${LNG}" ]; then
echo "Cannot find ${LNG} test suite"
exit 1

12
swig/python/test-r_util.py Executable file
View File

@ -0,0 +1,12 @@
#!/usr/bin/python
from r_util import *
b = RBuffer ()
print dir(b)
rs = RSystem()
str = rs.cmd_str ("ls", "")
#str = RSystem.cmd_str ("ls", "")
print "((%s))"%str

44
swig/python/test-rcc.py Executable file
View File

@ -0,0 +1,44 @@
#!/usr/bin/python
# -- pancake
import sys
from r_asm import *
from r_util import *
from r_bin import *
try:
program = sys.argv[1]
except:
print "Usage: test-rcc [path-to-program]"
sys.exit (1)
a = RAsm ()
a.use ("x86.nasm")
b = RBin ()
off_printf = 0
b.load (program, None)
baddr = b.get_baddr ()
for i in b.get_imports ():
if i.name == "printf":
off_printf = baddr+i.rva
break
if off_printf == 0:
print "Program %s does not imports 'printf'"%program
sys.exit(1)
rcc_code="""
printf@alias(0x%08lx);
main@global(32,32) {
printf ("Hello World\n");
}
"""%(off_printf)
rcc_asm = RSystem.cmd_str ("rcc", rcc_code)[0]
code = a.massemble (rcc_asm)
if code is None:
print "Cannot assemble"
else:
print code.buf_hex