* Fix vapi/t build

* Add regexp search method in r_search
  - Uses libc regcomp+regexec
  - Added regexp test example
* Added libr.pc for pkg-config
This commit is contained in:
pancake 2009-02-15 23:32:17 +01:00
parent 33d94abaec
commit cd84224709
8 changed files with 113 additions and 9 deletions

1
env.sh
View File

@ -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

11
libr/libr.pc Normal file
View File

@ -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

View File

@ -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

49
libr/search/regexp.c Normal file
View File

@ -0,0 +1,49 @@
/* radare - LGPL - Copyright 2008-2009 pancake<nopcode.org> */
#include "r_search.h"
#include <regex.h>
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;
}

View File

@ -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 */

View File

@ -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

View File

@ -0,0 +1,25 @@
#include <r_search.h>
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;
}

View File

@ -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 \