Skip to content

Commit

Permalink
* Fix vapi/t build
Browse files Browse the repository at this point in the history
* Add regexp search method in r_search
  - Uses libc regcomp+regexec
  - Added regexp test example
* Added libr.pc for pkg-config
  • Loading branch information
radare committed Feb 15, 2009
1 parent 33d94ab commit cd84224
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 9 deletions.
1 change: 1 addition & 0 deletions env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions libr/libr.pc
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion libr/search/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
49 changes: 49 additions & 0 deletions libr/search/regexp.c
Original file line number Diff line number Diff line change
@@ -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;
}
25 changes: 18 additions & 7 deletions libr/search/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -160,44 +171,44 @@ 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 */
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 */
int r_search_kw_add_bin(struct r_search_t *s, const u8 *kw, int kw_len, const u8 *bm, int bm_len)
{
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);
k->keyword_length = kw_len;
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 */
Expand Down
5 changes: 4 additions & 1 deletion libr/search/t/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions libr/search/t/test-regexp.c
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 4 additions & 0 deletions libr/vapi/t/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down

0 comments on commit cd84224

Please sign in to comment.