2009-02-15 23:32:17 +01:00
|
|
|
/* radare - LGPL - Copyright 2008-2009 pancake<nopcode.org> */
|
|
|
|
|
|
|
|
#include "r_search.h"
|
2010-01-15 01:32:28 +01:00
|
|
|
#if __UNIX__
|
2009-02-15 23:32:17 +01:00
|
|
|
#include <regex.h>
|
|
|
|
|
2009-09-24 12:29:05 +02:00
|
|
|
R_API int r_search_regexp_update(struct r_search_t *s, ut64 from, const ut8 *buf, int len)
|
2009-02-15 23:32:17 +01:00
|
|
|
{
|
|
|
|
struct list_head *pos;
|
|
|
|
char *buffer = malloc(len+1);
|
|
|
|
char *skipz, *end;
|
2009-03-06 00:00:41 +00:00
|
|
|
int count = 0;
|
2009-02-15 23:32:17 +01:00
|
|
|
|
|
|
|
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;
|
2009-02-17 00:09:40 +01:00
|
|
|
int ret, delta = 0;
|
2009-02-15 23:32:17 +01:00
|
|
|
regmatch_t matches[10];
|
|
|
|
regex_t compiled;
|
|
|
|
|
|
|
|
if (strchr(kw->binmask, 'i'))
|
|
|
|
reflags |= REG_ICASE;
|
|
|
|
|
2009-02-17 00:09:40 +01:00
|
|
|
if (regcomp(&compiled, kw->keyword, reflags)) {
|
|
|
|
fprintf(stderr, "Cannot compile '%s' regexp\n",kw->keyword);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-02-15 23:32:17 +01:00
|
|
|
foo:
|
2009-03-06 00:00:41 +00:00
|
|
|
ret = regexec(&compiled, buffer+delta, 1, matches, 0);
|
2009-02-17 00:09:40 +01:00
|
|
|
if (ret) {
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
do {
|
2009-02-15 23:32:17 +01:00
|
|
|
if (s->callback)
|
2009-07-08 13:49:55 +02:00
|
|
|
s->callback(kw, s->user, (ut64)from+matches[0].rm_so+delta);
|
2009-02-15 23:32:17 +01:00
|
|
|
else printf("hit%d_%d 0x%08llx ; %s\n",
|
2009-07-08 13:49:55 +02:00
|
|
|
count, kw->count, (ut64)(from+matches[0].rm_so),
|
2009-02-15 23:32:17 +01:00
|
|
|
buf+matches[0].rm_so+delta);
|
|
|
|
delta += matches[0].rm_so+1;
|
|
|
|
kw->count++;
|
|
|
|
count++;
|
2009-03-06 00:00:41 +00:00
|
|
|
} while(!regexec(&compiled, buffer+delta, 1, matches, 0));
|
2009-02-17 00:09:40 +01:00
|
|
|
if (delta == 0)
|
|
|
|
return 0;
|
2009-02-15 23:32:17 +01:00
|
|
|
|
|
|
|
/* 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;
|
2009-02-17 00:09:40 +01:00
|
|
|
if (kw->count>0)
|
|
|
|
goto foo;
|
2009-02-15 23:32:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2010-01-15 01:32:28 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
R_API int r_search_regexp_update(struct r_search_t *s, ut64 from, const ut8 *buf, int len)
|
|
|
|
{
|
|
|
|
eprintf ("r_search_regexp_update: unimplemented for this platform\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|