diff --git a/env.sh b/env.sh index b0c5badcf9ca6..9230b9854fb5c 100755 --- a/env.sh +++ b/env.sh @@ -4,6 +4,7 @@ new_env=' LIBR_PLUGINS=$PWD/prefix/lib/radare2 PATH=$PATH:$PWD/prefix/bin LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD/prefix/lib +PKG_CONFIG_PATH=$PWD/libr/ ' echo diff --git a/libr/libr.pc b/libr/libr.pc new file mode 100644 index 0000000000000..971788637ddca --- /dev/null +++ b/libr/libr.pc @@ -0,0 +1,11 @@ +prefix=/usr +exec_prefix=${prefix} +libdir=${exec_prefix}/lib +includedir=${prefix}/include + +Name: libr +Description: radare framework libraries +Version: 0.1 +Requires: gobject-2.0 gmodule-no-export-2.0 +Libs: -L${libdir} -lr_core -lr_lang -lr_search -lr_cmd +Cflags: -I${includedir}/libr diff --git a/libr/search/Makefile b/libr/search/Makefile index 2d417e3a68757..7d34bc7cc98bc 100644 --- a/libr/search/Makefile +++ b/libr/search/Makefile @@ -1,5 +1,5 @@ NAME=r_search -OBJ=search.o bytepat.o stripstr.o aes-find.o +OBJ=search.o bytepat.o stripstr.o aes-find.o regexp.o DEPS=r_util CFLAGS+=-g #OBJ+=binparse.o diff --git a/libr/search/regexp.c b/libr/search/regexp.c new file mode 100644 index 0000000000000..7621cf7feca2a --- /dev/null +++ b/libr/search/regexp.c @@ -0,0 +1,49 @@ +/* radare - LGPL - Copyright 2008-2009 pancake */ + +#include "r_search.h" +#include + +int r_search_regexp_update(struct r_search_t *s, u64 from, const u8 *buf, int len) +{ + struct list_head *pos; + char *buffer = malloc(len+1); + char *skipz, *end; + int i, count = 0; + + memcpy(buffer, buf, len); + buffer[len]='\0'; + + list_for_each_prev(pos, &s->kws) { + struct r_search_kw_t *kw = list_entry(pos, struct r_search_kw_t, list); + int reflags = REG_EXTENDED; + int delta = 0; + regmatch_t matches[10]; + regex_t compiled; + + if (strchr(kw->binmask, 'i')) + reflags |= REG_ICASE; + + regcomp(&compiled, kw->keyword, reflags); + foo: + while (!regexec(&compiled, buffer+delta, 1, &matches, 0)) { + if (s->callback) + s->callback(kw, s->user, (u64)from+matches[0].rm_so+delta); + else printf("hit%d_%d 0x%08llx ; %s\n", + count, kw->count, (u64)from+matches[0].rm_so, + buf+matches[0].rm_so+delta); + delta += matches[0].rm_so+1; + kw->count++; + count++; + } + + /* TODO: check if skip 0 works */ + skipz = strchr(buffer, '\0'); + end = buffer+len; + if (skipz && skipz+1 < end) { + for(;!*skipz&&end;skipz=skipz+1); + delta = skipz-buffer; + goto foo; + } + } + return count; +} diff --git a/libr/search/search.c b/libr/search/search.c index fb11fffd6f3a8..21d37350c99d9 100644 --- a/libr/search/search.c +++ b/libr/search/search.c @@ -63,6 +63,7 @@ struct r_search_t *r_search_free(struct r_search_t *s) } /* control */ +/* Rename to start(), begin() .. */ int r_search_initialize(struct r_search_t *s) { struct list_head *pos; @@ -72,6 +73,15 @@ int r_search_initialize(struct r_search_t *s) kw->count = 0; kw->idx = 0; } + +#if 0 + /* TODO: compile regexpes */ + switch(s->mode) { + case R_SEARCH_REGEXP: + break; + } +#endif + return 1; } @@ -126,12 +136,13 @@ int r_search_update(struct r_search_t *s, u64 *from, const u8 *buf, u32 len) int i, ret = 0; switch(s->mode) { case R_SEARCH_KEYWORD: - r_search_mybinparse_update(s, *from, buf, len); + ret += r_search_mybinparse_update(s, *from, buf, len); break; case R_SEARCH_XREFS: //r_search_xrefs_update(s, *from, buf, len); break; case R_SEARCH_REGEXP: + ret += r_search_regexp_update(s, *from, buf, len); break; case R_SEARCH_AES: ret += r_search_aes_update(s, *from, buf, len); @@ -160,14 +171,14 @@ int r_search_kw_add(struct r_search_t *s, const char *kw, const char *bm) { struct r_search_kw_t *k = MALLOC_STRUCT(struct r_search_kw_t); if (k == NULL) - return -1; + return R_FALSE; strncpy(k->keyword, kw, sizeof(k->keyword)); strncpy(k->bin_keyword, kw, sizeof(k->keyword)); k->keyword_length = strlen(kw); strncpy(k->binmask, bm, sizeof(k->binmask)); k->binmask_length = r_hex_str2bin(bm, k->bin_binmask); list_add(&(k->list), &(s->kws)); - return 0; + return R_TRUE; } /* hexpair string */ @@ -175,13 +186,13 @@ int r_search_kw_add_hex(struct r_search_t *s, const char *kw, const char *bm) { struct r_search_kw_t *k = MALLOC_STRUCT(struct r_search_kw_t); if (k == NULL) - return -1; + return R_FALSE; strncpy(k->keyword, kw, sizeof(k->keyword)); k->keyword_length = r_hex_str2bin(kw, k->bin_keyword); strncpy(k->binmask, bm, sizeof(k->binmask)); k->binmask_length = r_hex_str2bin(bm, k->bin_binmask); list_add(&(k->list), &(s->kws)); - return 0; + return R_TRUE; } /* raw bin */ @@ -189,7 +200,7 @@ int r_search_kw_add_bin(struct r_search_t *s, const u8 *kw, int kw_len, const u8 { struct r_search_kw_t *k = MALLOC_STRUCT(struct r_search_kw_t); if (kw == NULL) - return -1; + return R_FALSE; memcpy(k->bin_keyword, kw, kw_len); k->keyword_length = kw_len; memcpy(k->bin_binmask, bm, bm_len); @@ -197,7 +208,7 @@ int r_search_kw_add_bin(struct r_search_t *s, const u8 *kw, int kw_len, const u8 r_hex_bin2str(kw, kw_len, k->keyword); r_hex_bin2str(bm, bm_len, k->binmask); list_add(&(k->list), &(s->kws)); - return 0; + return R_TRUE; } /* show keywords */ diff --git a/libr/search/t/Makefile b/libr/search/t/Makefile index fe3a34b165200..fc251c48e8596 100644 --- a/libr/search/t/Makefile +++ b/libr/search/t/Makefile @@ -2,11 +2,14 @@ BINDEPS=r_util r_search include ../../rules.mk -all: test test-str +all: test test-str test-regexp test: ${CC} -g -I ../../include test.c ${LDFLAGS} -o test +test-regexp: + ${CC} -g -I ../../include test-regexp.c ${LDFLAGS} -o test-regexp + test-str: ${CC} -g -I ../../include test-str.c ${LDFLAGS} -o test-str diff --git a/libr/search/t/test-regexp.c b/libr/search/t/test-regexp.c new file mode 100644 index 0000000000000..b26d9bdd6f557 --- /dev/null +++ b/libr/search/t/test-regexp.c @@ -0,0 +1,25 @@ +#include + +u8 *buffer = "ELF,e,e,e,ELF--fooo"; + +int hit(struct r_search_kw_t *kw, void *user, u64 addr) +{ + const u8 *buf = (u8*)user; + printf("HIT %d AT %lld (%s)\n", kw->count, addr, buffer+addr); + return 1; +} + +int main(int argc, char **argv) +{ + struct r_search_t *rs; + + rs = r_search_new(R_SEARCH_REGEXP); + r_search_set_callback(rs, &hit, buffer); + r_search_kw_add(rs, "E.F", "i"); /* search for /E.F/i */ + r_search_initialize(rs); + printf("Searching strings in '%s'\n", buffer); + r_search_update_i(rs, 0LL, buffer, strlen(buffer)); + rs = r_search_free(rs); + + return 0; +} diff --git a/libr/vapi/t/Makefile b/libr/vapi/t/Makefile index 9d322974b669c..2a60001a6761a 100644 --- a/libr/vapi/t/Makefile +++ b/libr/vapi/t/Makefile @@ -14,6 +14,10 @@ core: -lr_bin -Wl,-R../../bin -L../../bin \ -lr_io -Wl,-R../../io -L../../io \ -lr_cons -Wl,-R../../cons -L../../cons \ + -lr_debug -Wl,-R../../debug -L../../debug \ + -lr_lang -Wl,-R../../lang -L../../lang \ + -lr_line -Wl,-R../../line -L../../line \ + -lr_lib -Wl,-R../../lib -L../../lib \ -lr_flags -Wl,-R../../flags -L../../flags \ -lr_macro -Wl,-R../../macro -L../../macro \ -lr_print -Wl,-R../../print -L../../print \