* Added r_parse lib

* Added pseudo syntax poc to radare2 (asm.pseudo)

--HG--
rename : libr/parse/pseudo.c => libr/parse/p/parse_x86_pseudo.c
This commit is contained in:
Nibble 2009-02-24 15:58:21 +01:00
parent 1b71e0e52d
commit 801ed768fc
14 changed files with 209 additions and 16 deletions

View File

@ -2,7 +2,7 @@
PREFIX=../prefix
# Libraries
LIBLIST=io util lib meta lang flags bin macro hash line cons line print config syscall range socket cmd asm anal search diff debug reg core var
LIBLIST=io util lib meta lang flags bin macro hash line cons line print config syscall range socket cmd asm anal parse search diff debug reg core var
# Under development

View File

@ -1,5 +1,5 @@
NAME=r_core
DEPS=r_config r_cons r_line r_io r_cmd r_util r_print r_flags r_asm r_lib r_macro r_debug r_hash r_bin r_lang r_io r_asm r_anal r_config r_macro r_print
DEPS=r_config r_cons r_line r_io r_cmd r_util r_print r_flags r_asm r_lib r_macro r_debug r_hash r_bin r_lang r_io r_asm r_anal r_parse r_config r_macro r_print
OBJ=core.o cmd.o file.o config.o visual.o io.o yank.o

View File

@ -294,15 +294,20 @@ static int cmd_print(void *data, const char *input)
/* XXX hardcoded */
int ret, idx;
u8 *buf = core->block;
u64 pseudo = r_config_get_i(&core->config, "asm.pseudo");
char str[128];
struct r_asm_aop_t aop;
r_asm_set_pc(&core->assembler, core->seek);
r_asm_set(&core->assembler, "asm_x86");
r_parse_set(&core->parser, "parse_x86_pseudo");
for(idx=ret=0; idx < len; idx+=ret) {
r_asm_set_pc(&core->assembler, core->assembler.pc + ret);
ret = r_asm_disassemble(&core->assembler, &aop, buf+idx, len-idx);
r_parse_parse(&core->parser, aop.buf_asm, str);
r_cons_printf("0x%08llx %14s %s\n",
core->seek+idx, aop.buf_hex, aop.buf_asm);
core->seek+idx, aop.buf_hex,
pseudo?str:aop.buf_asm);
}
}
break;

View File

@ -11,6 +11,7 @@ int r_core_config_init(struct r_core_t *core)
r_config_set(cfg, "asm.arch", "x86");
r_config_set_i(cfg, "asm.bits", 32);
r_config_set(cfg, "asm.syntax", "x86");
r_config_set(cfg, "asm.pseudo", "false");
r_config_set(cfg, "asm.os", "linux");
r_config_set(cfg, "cmd.prompt", "");
r_config_set(cfg, "cmd.vprompt", "");

View File

@ -92,6 +92,18 @@ int __lib_asm_cb(struct r_lib_plugin_t *pl, void *user, void *data)
int __lib_asm_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; }
/* parse callback */
int __lib_parse_cb(struct r_lib_plugin_t *pl, void *user, void *data)
{
struct r_parse_handle_t *hand = (struct r_parse_handle_t *)data;
struct r_core_t *core = (struct r_core_t *)user;
//printf(" * Added (dis)assembly handler\n");
r_parse_add(&core->parser, hand);
return R_TRUE;
}
int __lib_parse_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; }
int r_core_init(struct r_core_t *core)
{
core->oobi = NULL;
@ -107,6 +119,8 @@ int r_core_init(struct r_core_t *core)
r_anal_set_user_ptr(&core->anal, core);
r_asm_init(&core->assembler);
r_asm_set_user_ptr(&core->assembler, core);
r_parse_init(&core->parser);
r_parse_set_user_ptr(&core->parser, core);
r_meta_init(&core->meta);
r_cons_init();
core->search = r_search_new(R_SEARCH_KEYWORD);
@ -135,6 +149,8 @@ int r_core_init(struct r_core_t *core)
&__lib_anl_cb, &__lib_anl_dt, core);
r_lib_add_handler(&core->lib, R_LIB_TYPE_ASM, "(dis)assembly plugins",
&__lib_asm_cb, &__lib_asm_dt, core);
r_lib_add_handler(&core->lib, R_LIB_TYPE_PARSE, "parsing plugins",
&__lib_parse_cb, &__lib_parse_dt, core);
r_lib_opendir(&core->lib, getenv("LIBR_PLUGINS"));
{
char *homeplugindir = r_str_home(".radare/plugins");

View File

@ -1,6 +1,6 @@
OBJ=radare2.o
BIN=radare2
BINDEPS=r_core r_cons r_macro r_util r_flags r_lib r_io r_hash r_cmd r_config r_asm r_anal r_lang r_debug r_print r_line r_bin r_search r_meta
BINDEPS=r_core r_cons r_macro r_util r_flags r_lib r_io r_hash r_cmd r_config r_asm r_anal r_parse r_lang r_debug r_print r_line r_bin r_search r_meta
#LIBS=-lr_core -L..
### shared

View File

@ -6,6 +6,7 @@
#include "r_lib.h"
#include "r_lang.h"
#include "r_asm.h"
#include "r_parse.h"
#include "r_anal.h"
#include "r_cmd.h"
#include "r_meta.h"
@ -50,6 +51,7 @@ struct r_core_t {
struct r_cmd_t cmd;
struct r_anal_t anal;
struct r_asm_t assembler;
struct r_parse_t parser;
struct r_meta_t meta;
struct r_lang_t lang;
struct r_debug_t dbg;

View File

@ -41,6 +41,7 @@ enum {
R_LIB_TYPE_LANG, /* language */
R_LIB_TYPE_ASM, /* assembler */
R_LIB_TYPE_ANAL, /* analysis */
R_LIB_TYPE_PARSE,/* parsers */
};
struct r_lib_t {

35
libr/include/r_parse.h Normal file
View File

@ -0,0 +1,35 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#ifndef _INCLUDE_R_PARSE_H_
#define _INCLUDE_R_PARSE_H_
#include <r_types.h>
#include <list.h>
struct r_parse_t {
void *user;
struct r_parse_handle_t *cur;
struct list_head parsers;
};
struct r_parse_handle_t {
char *name;
char *desc;
int (*init)(void *user);
int (*fini)(void *user);
int (*parse)(struct r_parse_t *p, void *data, char *str);
struct list_head list;
};
/* parse.c */
struct r_parse_t *r_parse_new();
void r_parse_free(struct r_parse_t *p);
int r_parse_init(struct r_parse_t *p);
void r_parse_set_user_ptr(struct r_parse_t *p, void *user);
int r_parse_add(struct r_parse_t *p, struct r_parse_handle_t *foo);
int r_parse_list(struct r_parse_t *p);
int r_parse_set(struct r_parse_t *p, const char *name);
int r_parse_parse(struct r_parse_t *p, void *data, char *str);
#endif

4
libr/parse/Makefile Normal file
View File

@ -0,0 +1,4 @@
NAME=r_parse
OBJ=parse.o
include ../rules.mk

15
libr/parse/p/Makefile Normal file
View File

@ -0,0 +1,15 @@
CFLAGS=-I../../include -I../arch/ -I../arch/include -Wall -fPIC -shared -Wl,-R..
all: parse_dummy.so parse_x86_pseudo.so
@true
parse_dummy.so:
${CC} ${CFLAGS} -o parse_dummy.so parse_dummy.c
@#strip -s parse_dummy.so
parse_x86_pseudo.so: parse_x86_pseudo.o
${CC} ${CFLAGS} -o parse_x86_pseudo.so parse_x86_pseudo.o
@#strip -s parse_x86_pseudo.so
clean:
-rm -f *.so *.o

View File

@ -0,0 +1,30 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <r_types.h>
#include <r_lib.h>
#include <r_util.h>
#include <r_parse.h>
static int parse(struct r_parse_t *p, void *data, char *str)
{
printf("Dummy parsing plugin");
return R_FALSE;
}
static struct r_parse_handle_t r_parse_plugin_parse_dummy = {
.name = "parse_dummy",
.desc = "dummy parsing plugin",
.init = NULL,
.fini = NULL,
.parse = &parse,
};
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_PARSE,
.data = &r_parse_plugin_parse_dummy
};

View File

@ -5,10 +5,11 @@
#include <string.h>
#include <r_types.h>
#include <r_lib.h>
#include <r_util.h>
#include <r_asm.h>
#include <r_parse.h>
static int r_asm_x86_replace(int argc, const char *argv[], char *newstr)
static int replace(int argc, const char *argv[], char *newstr)
{
int i,j,k;
struct {
@ -65,31 +66,31 @@ static int r_asm_x86_replace(int argc, const char *argv[], char *newstr)
return R_FALSE;
}
int r_asm_x86_pseudo(struct r_asm_t *a)
static int parse(struct r_parse_t *p, void *data, char *str)
{
int i, len = strlen(a->buf_asm);
int i, len = strlen((char*)data);
char w0[32];
char w1[32];
char w2[32];
char w3[32];
char *str, *ptr, *optr;
char *buf, *ptr, *optr;
if ((str = alloca(len+1)) == NULL)
if ((buf = alloca(len+1)) == NULL)
return R_FALSE;
memcpy(str, a->buf_asm, len+1);
memcpy(buf, (char*)data, len+1);
if (str[0]!='\0') {
if (buf[0]!='\0') {
w0[0]='\0';
w1[0]='\0';
w2[0]='\0';
w3[0]='\0';
ptr = strchr(str, ' ');
ptr = strchr(buf, ' ');
if (ptr == NULL)
ptr = strchr(str, '\t');
ptr = strchr(buf, '\t');
if (ptr) {
ptr[0]='\0';
for(ptr=ptr+1;ptr[0]==' ';ptr=ptr+1);
strcpy(w0, str);
strcpy(w0, buf);
strcpy(w1, ptr);
optr=ptr;
@ -117,9 +118,22 @@ int r_asm_x86_pseudo(struct r_asm_t *a)
nw++;
}
r_asm_x86_replace(nw, wa, (char*)a->aux);
replace(nw, wa, str);
}
}
return R_TRUE;
}
static struct r_parse_handle_t r_parse_plugin_parse_x86_pseudo = {
.name = "parse_x86_pseudo",
.desc = "X86 pseudo syntax",
.init = NULL,
.fini = NULL,
.parse = &parse,
};
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_PARSE,
.data = &r_parse_plugin_parse_x86_pseudo
};

70
libr/parse/parse.c Normal file
View File

@ -0,0 +1,70 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <r_types.h>
#include <r_parse.h>
#include <list.h>
struct r_parse_t *r_parse_new()
{
struct r_parse_t *p = MALLOC_STRUCT(struct r_parse_t);
r_parse_init(p);
return p;
}
void r_parse_free(struct r_parse_t *p)
{
free(p);
}
int r_parse_init(struct r_parse_t *p)
{
p->user = NULL;
INIT_LIST_HEAD(&p->parsers);
return R_TRUE;
}
void r_parse_set_user_ptr(struct r_parse_t *p, void *user)
{
p->user = user;
}
int r_parse_add(struct r_parse_t *p, struct r_parse_handle_t *foo)
{
if (foo->init)
foo->init(p->user);
list_add_tail(&(foo->list), &(p->parsers));
return R_TRUE;
}
int r_parse_list(struct r_parse_t *p)
{
struct list_head *pos;
list_for_each_prev(pos, &p->parsers) {
struct r_parse_handle_t *h = list_entry(pos, struct r_parse_handle_t, list);
printf(" %s: %s\n", h->name, h->desc);
}
return R_FALSE;
}
int r_parse_set(struct r_parse_t *p, const char *name)
{
struct list_head *pos;
list_for_each_prev(pos, &p->parsers) {
struct r_parse_handle_t *h = list_entry(pos, struct r_parse_handle_t, list);
if (!strcmp(h->name, name)) {
p->cur = h;
return R_TRUE;
}
}
return R_FALSE;
}
int r_parse_parse(struct r_parse_t *p, void *data, char *str)
{
if (p->cur && p->cur->parse)
return p->cur->parse(p, data, str);
return R_FALSE;
}