* Added basic working example in swig-python for r_util

* Add _write and _puts methods for r_socket api
  - fgets -> gets
  - fix r_socket_printf
* More random fixes
* typedef all structures in r_util
This commit is contained in:
pancake 2009-12-22 13:27:43 +01:00
parent 6a576ba6ce
commit 78dbab76d6
17 changed files with 168 additions and 81 deletions

12
TODO
View File

@ -14,11 +14,6 @@
* public void each(RangeEachFunc each_func) { ... }
* r_io must need a reviewd undo API
** Add R_ASM_PLUGIN_PREFIX... in short
** in a vapi file you can define a different constructor class Foo { bar Bar(); }..
definition of Bar will override the previous Bar() class definition
** .h is not included correctly for each vapi maybe its about order???
<{include libr/TODO}>
* correct result (R_TRUFAE), but with warnings (implement r_errno and r_errstr in r_util?)
@ -53,3 +48,10 @@
scripting language, so we can for example prepare
a .c stub interface for python/perl/ruby/..
- Maybe having a preprocessor tool for this..
Vala issues
===========
** in a vapi file you can define a different constructor class Foo { bar Bar(); }..
definition of Bar will override the previous Bar() class definition
** .h is not included correctly for each vapi maybe its about order???

View File

@ -1,5 +1,23 @@
Code analysis module
====================
* Direction of the stack? increase? decrease?
* Register value source type
- This is static entropy level for a register at some point
- Constant value
mov eax, 33
mov eax, [const] ; from ro memory
static_entropy = 0;
- Variable
mov eax, [rwmem] ; from rw memory (variable)
static_entropy = 1;
- Modification
add eax, ebx ; from rw memory (variable)
static_entropy ++;
* At any point of the program we can determine if a register
has a static fixed value or the level of possible polimorfism
-- allow to load register values from traces
* TODO: Add static plugin support here
@ -19,6 +37,7 @@ foreach var (calls) {
}
an.accept(ctx);
////////////////////////////////////////
Global picture

View File

@ -1,5 +1,7 @@
* Use r_mem_pool
* Serialize/Deserialize from disk
* Return FALSE when adding two elements with the same
attributes (get before add) if the db is unique
// not necessary, we can just do this by manually checkingc

View File

@ -39,11 +39,12 @@ R_API int r_socket_flush(int fd);
R_API void r_socket_block(int fd, int block);
R_API int r_socket_ready(int fd, int secs, int usecs);
R_API int r_socket_read(int fd, unsigned char *read, int len);
R_API int r_socket_write(int fd, unsigned char *buf, int len);
R_API int r_socket_puts(int fd, char *buf);
R_API int r_socket_write(int fd, void *buf, int len);
R_API int r_socket_connect(char *host, int port);
R_API int r_socket_listen(int port);
R_API int r_socket_accept(int fd);
R_API int r_socket_fgets(int fd, char *buf, int size);
R_API int r_socket_gets(int fd, char *buf, int size);
R_API void r_socket_printf(int fd, const char *fmt, ...);
R_API char *r_socket_to_string(int fd);

View File

@ -12,7 +12,7 @@
#endif
/* pool */
struct r_mem_pool_t {
typedef struct r_mem_pool_t {
void **nodes;
int ncount;
int npool;
@ -20,7 +20,7 @@ struct r_mem_pool_t {
int nodesize;
int poolsize;
int poolcount;
};
} rMemPool;
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);
@ -30,11 +30,11 @@ R_API void* r_mem_pool_alloc(struct r_mem_pool_t *pool);
R_API int r_mem_count(ut8 **addr);
/* buf */
struct r_buf_t {
typedef struct r_buf_t {
ut8 *buf;
int length;
ut64 base;
};
} rBuf;
R_API struct r_buf_t *r_buf_init(struct r_buf_t *b);
R_API struct r_buf_t *r_buf_new();
@ -46,20 +46,20 @@ R_API void r_buf_free(struct r_buf_t *b);
/* r_cache */
// TOTHINK: move into a separated library?
struct r_cache_item_t {
typedef struct r_cache_item_t {
ut64 addr;
char *str;
struct list_head list;
};
} rCacheItem;
struct r_cache_t {
typedef struct r_cache_t {
ut64 start;
ut64 end;
struct list_head items;
};
} rCache;
R_API void r_cache_init(struct r_cache_t *lang);
R_API struct r_cache_t *r_cache_new();
R_API rCache* r_cache_new();
R_API void r_cache_free(struct r_cache_t *c);
R_API char *r_cache_get(struct r_cache_t *c, ut64 addr);
R_API int r_cache_set(struct r_cache_t *c, ut64 addr, char *str);
@ -83,14 +83,14 @@ R_API int r_mem_cmp_mask(const ut8 *dest, const ut8 *orig, const ut8 *mask, int
R_API const ut8 *r_mem_mem(const ut8 *haystack, int hlen, const ut8 *needle, int nlen);
/* numbers */
struct r_num_t {
typedef struct r_num_t {
ut64 (*callback)(void *userptr, const char *str, int *ok);
ut64 value;
void *userptr;
};
} Num;
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); // XXX this can be a cpp macro :??
R_API ut64 r_num_math(struct r_num_t *num, const char *str);
R_API ut64 r_num_get(struct r_num_t *num, const char *str);
R_API struct r_num_t *r_num_new(ut64 (*cb)(void*,const char *,int*), void *ptr);
@ -169,5 +169,9 @@ 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(const char *cmd, const char *input, int *len);
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);
#endif

View File

@ -3,8 +3,8 @@
#define USE_SOCKETS
#include <errno.h>
#include "r_types.h"
#include "r_socket.h"
#include <r_types.h>
#include <r_socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
@ -26,7 +26,7 @@
#define BUFFER_SIZE 4096
R_API int r_socket_write(int fd, unsigned char *buf, int len)
R_API int r_socket_write(int fd, void *buf, int len)
{
int delta = 0;
int ret;
@ -47,6 +47,12 @@ R_API int r_socket_write(int fd, unsigned char *buf, int len)
return delta;
}
R_API int r_socket_puts(int fd, char *buf)
{
int len = strlen(buf);
return r_socket_write(fd, buf, len);
}
// XXX: rewrite it to use select //
/* waits secs until new data is received. */
/* returns -1 on error, 0 is false, 1 is true */
@ -92,21 +98,16 @@ R_API void r_socket_block(int fd, int block)
R_API void r_socket_printf(int fd, const char *fmt, ...)
{
#if !__linux__
char buf[BUFFER_SIZE];
#endif
va_list ap;
va_start(ap, fmt);
if (fd <= 0)
if (fd < 0)
return;
#if __linux__
dprintf(fd, fmt, ap);
#else
snprintf(buf,BUFFER_SIZE,fmt,ap);
socket_write(fd, buf, strlen(buf));
#endif
va_start(ap, fmt);
vsnprintf(buf, BUFFER_SIZE, fmt, ap);
r_socket_write(fd, buf, strlen(buf));
va_end(ap);
}
@ -256,7 +257,7 @@ R_API int r_socket_flush(int fd)
return 0;
}
R_API int r_socket_fgets(int fd, char *buf, int size)
R_API int r_socket_gets(int fd, char *buf, int size)
{
int i = 0;
int ret = 0;

View File

@ -20,7 +20,7 @@ static int bufidx = 0;
static ut8 *bufnext = 0;
static ut8 *bufmax;
int r_alloca_init()
R_API int r_alloca_init()
{
buf = (ut8 *)malloc(ALLOC_SIZE);
if (buf == NULL)
@ -31,7 +31,7 @@ int r_alloca_init()
return R_TRUE;
}
ut8 *r_alloca_bytes(int len)
R_API ut8 *r_alloca_bytes(int len)
{
ut8 *next = bufnext;
ut8 *tnext = bufnext + len;
@ -41,7 +41,7 @@ ut8 *r_alloca_bytes(int len)
return next;
}
char *r_alloca_str(const char *str)
R_API char *r_alloca_str(const char *str)
{
int len;
ut8 *p;
@ -59,7 +59,7 @@ char *r_alloca_str(const char *str)
}
/* free last allocated buffer */
int r_alloca_ret_i(int n)
R_API int r_alloca_ret_i(int n)
{
/* check for underflows */
if (bufidx==0) return n;

View File

@ -26,7 +26,7 @@ R_API struct btree_node *btree_remove(struct btree_node *p, BTREE_DEL(del))
} else rp->left = p->left;
} else rp = p->right;
} else rp = p->left;
if (del) del(p->data);
if (del) del (p->data);
free(p);
return(rp);
}
@ -55,9 +55,9 @@ R_API int btree_del(struct btree_node *proot, void *x, BTREE_CMP(cmp), BTREE_DEL
struct btree_node *p = btree_search (proot, x, cmp, 1);
if (p) {
p->right = btree_remove (p->left, del);
return 1;
return R_TRUE;
}
return 0;
return R_FALSE;
}
R_API void *btree_get(struct btree_node *proot, void *x, BTREE_CMP(cmp))
@ -88,7 +88,9 @@ R_API void btree_insert(struct btree_node **T, struct btree_node *p, BTREE_CMP(c
else (*T)->left = p;
} else if (ret>0) {
if ((*T)->right) btree_insert (&(*T)->right, p, cmp);
else (*T)->right = p; } }
else (*T)->right = p;
}
}
R_API void btree_add(struct btree_node **T, void *e, BTREE_CMP(cmp))
{
@ -145,8 +147,8 @@ struct mydata {
int shownode(char *str, struct mydata *m)
{
if (m == NULL)
printf("==> not found\n");
else printf("==> %s: %s, %lld\n", str, m->str, m->addr);
printf ("==> not found\n");
else printf ("==> %s: %s, %lld\n", str, m->str, m->addr);
return 0;
}
int mycmp(const void *a, const void *b)
@ -169,11 +171,11 @@ int main()
printf("EMPTY TREE: %d\n", btree_empty(&bt));
btree_add(&bt, &foo, mycmp);
btree_add(&bt, &bar, mycmp);
printf("EMPTY TREE: %d\n", btree_empty(&bt));
printf ("EMPTY TREE: %d\n", btree_empty(&bt));
printf("==== go search ====\n");
/* find existent data */
struct mydata *p = btree_get(bt, &bar, mycmp);
struct mydata *p = btree_get (bt, &bar, mycmp);
shownode("result for 20: ", p);
printf("==== go search ====\n");

View File

@ -2,16 +2,15 @@
#include "r_util.h"
int r_num_is_float(struct r_num_t *num, const char *str)
R_API int r_num_is_float(struct r_num_t *num, const char *str)
{
// TODO: also support 'f' terminated strings
return (int) strchr(str, '.');
return (int) strchr (str, '.');
}
double r_num_get_float(struct r_num_t *num, const char *str)
R_API double r_num_get_float(struct r_num_t *num, const char *str)
{
double d = 0.0f;
sscanf(str, "%lf", &d);
sscanf (str, "%lf", &d);
return d;
}

View File

@ -20,25 +20,27 @@ R_API struct r_mem_pool_t* r_mem_pool_deinit(struct r_mem_pool_t *pool)
return pool;
}
R_API struct r_mem_pool_t* r_mem_pool_init(struct r_mem_pool_t *pool, int nodesize, int poolsize, int poolcount)
R_API struct r_mem_pool_t* r_mem_pool_init(struct r_mem_pool_t *pool, int nsize, int psize, int pcount)
{
if (pool) {
if (poolsize < 1) poolsize = ALLOC_POOL_SIZE;
if (poolcount < 1) poolcount = ALLOC_POOL_COUNT;
if (psize < 1)
psize = ALLOC_POOL_SIZE;
if (pcount < 1)
pcount = ALLOC_POOL_COUNT;
// TODO: assert nodesize?;
pool->poolsize = poolsize;
pool->poolcount = poolcount;
pool->nodesize = nodesize;
pool->poolsize = psize;
pool->poolcount = pcount;
pool->nodesize = nsize;
pool->npool = -1;
pool->ncount = pool->poolsize; // force init
pool->nodes = (void**) malloc(sizeof(void*)*pool->poolcount);
pool->nodes = (void**) malloc (sizeof (void*) * pool->poolcount);
}
return pool;
}
R_API struct r_mem_pool_t *r_mem_pool_new(int nodesize, int poolsize, int poolcount)
{
return r_mem_pool_init(MALLOC_STRUCT(struct r_mem_pool_t),
return r_mem_pool_init (MALLOC_STRUCT (struct r_mem_pool_t),
nodesize, poolsize, poolcount);
}
@ -62,5 +64,3 @@ R_API void* r_mem_pool_alloc(struct r_mem_pool_t *pool)
// TODO: fix warning
return (void *)(&(pool->nodes[pool->npool][pool->ncount++]));
}
//does not yet supports arguments R_DEFINE_OBJECT(r_mem_pool);

View File

@ -9,7 +9,7 @@
#endif
/* returns 1 if 'str' matches 'reg' regexp */
int r_str_re_match(const char *str, const char *reg)
R_API int r_str_re_match(const char *str, const char *reg)
{
#if HAVE_REGEXP
regex_t preg;
@ -22,7 +22,7 @@ int r_str_re_match(const char *str, const char *reg)
#endif
}
int r_str_re_replace(const char *str, const char *reg, const char *sub)
R_API int r_str_re_replace(const char *str, const char *reg, const char *sub)
{
/* TODO: not yet implemented */
return -1;

View File

@ -16,8 +16,8 @@ public struct Radare.Socket : int
public Socket.listen(int port);
public int close();
[NoArrayLength]
public int fgets(string buf, int len);
public int printf(string *str, ...);
public int gets(string buf, int len);
public int printf(string str, ...);
public int accept();
public void block(bool blocking);
public bool flush();

View File

@ -31,7 +31,7 @@ namespace Radare {
public bool err(string str);
}
[CCode (cprefix="r_buf")]
[CCode (cprefix="r_buf_")]
public class Buffer {
public Buffer();
public int read_at(uint64 addr, uint8 *buf, int len);
@ -98,6 +98,4 @@ namespace Radare {
[CCode (cname="rarray_iterator")] //, generic_type_pos=2)]
public Rarray<G> iterator();
}
}

View File

@ -85,7 +85,8 @@ hash:
gcc hash.c `pkg-config gobject-2.0 --libs --cflags` -I../../include/ -lr_hash -Wl,-R../../hash -L../../hash -o hash
socket:
valac -C --profile=posix --vapidir=${PWD}/.. socket.vala --pkg r_socket
#valac -C --profile=posix --vapidir=${PWD}/.. socket.vala --pkg r_socket
valac -C --vapidir=${PWD}/.. socket.vala --pkg r_socket --pkg posix
gcc socket.c `pkg-config gobject-2.0 --libs --cflags` -I../../include/ -lr_socket -Wl,-R../../socket -L../../socket -o socket
clean:

View File

@ -1,26 +1,51 @@
/* valac --profile=posix --pkg r_socket socket.vala */
using Posix;
using GLib;
using Radare;
public static void main()
public static void main(string[] args)
{
string str = (string) new char[4096];
#if POSIX
unowned GLib.FILE stdin = &Posix.stdin;
unowned GLib.FILE stdout = &Posix.stdout;
#else
// XXX: not yet supported by vala
unowned GLib.FileStream stdin = ref GLib.stdin;
unowned GLib.FileStream stdout = ref GLib.stdout;
#endif
//var fd = Socket.connect("www.google.com", 80);
var fd = Socket.connect("localhost", 9999);
Socket fd;
if (args.length>2)
fd = Socket.connect(args[1], args[2].to_int());
else fd = Socket.connect("radare.org", 80);
//var fd = Socket.connect("localhost", 9999);
if (fd == -1) {
printf("Cannot connect\n");
return;
}
print("Connected\n");
fd.printf("GET /\r\n\r\n");
stdout.printf("[-] waiting for output\n");
//while(!fd.ready(0,0));
printf("[-] waiting for output\n");
while(!fd.ready(0,0));
printf("[-] reading data\n");
while(fd.fgets(str, 1024)>0) {
printf(str+"\n");
}
print("[-] reading data\n");
//fd.printf("GET /\r\n\r\n");
do {
string s = (string) new char[1024];
stdin.scanf("%s", s);
stdout.printf("==> (%s)\n", s);
print("length is = %d\n", (int)s.size());
fd.printf("GET %s HTTP/1.1\r\nHost: radare.org\r\n\r\n", s);
if (fd.gets(str, 1024)>0)
printf(str+"\n");
else break;
} while(true);
//while(fd.gets(str, 1024)>0) {
// printf(str+"\n");
//}
fd.close();
}

View File

@ -1,6 +1,7 @@
#!/bin/sh
MOD=r_util
CFLAGS="-I ../libr/include -I /usr/include/python2.5"
CFLAGS="-I ../libr/include -I /usr/include/python2.6"
export CFLAGS
swig -python ${MOD}.i
#swig -python -shadow ${MOD}.i
gcc -shared ${MOD}_wrap.c ${CFLAGS} -o _${MOD}.so ../libr/util/*.o

View File

@ -5,3 +5,35 @@
/* stabilized */
#define R_SWIG 1
%include "../libr/include/r_util.h"
#if 0
%typemap(in) ut64 * {
//#ifdef SWIG<PYTHON>
#define HAVE_LONG_LONG 1
#if HAVE_LONG_LONG
$result = ($type) PyLong_AsUnsignedLongLong ($1);
#else
#warning python without long long support??
#endif
// .. support for perl, ruby ..
//#endif
}
#endif
%extend Num {
Num () {
return r_num_new (NULL, NULL);
}
unsigned long long get (char *foo) {
return r_num_get (self, foo);
}
unsigned long long math (char *foo) {
return r_num_math (self, foo);
}
/*
static void minmap_swap (int *OUTPUT, int *OUTPUT) {
r_num_minmap_swap_i ( ... )
}
*/
};