radare2/libr/search/bytepat.c

130 lines
2.9 KiB
C
Raw Normal View History

2014-04-03 22:55:16 +02:00
/* radare - LGPL - Copyright 2006-2014 - esteve, pancake */
#include "r_search.h"
2014-05-16 04:07:03 +02:00
#include "r_print.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define CTXMINB 5
#define BSIZE (1024*1024)
#define MAX_PATLEN 1024
typedef struct _fnditem {
unsigned char str[MAX_PATLEN];
void* next;
} fnditem;
static fnditem* init_fi() {
fnditem* n;
n = (fnditem*) malloc (sizeof (fnditem));
2016-05-24 21:22:15 +01:00
if (!n) return NULL;
n->next = NULL;
return n;
}
static void fini_fi(fnditem* fi) {
fnditem *fu;
fu = fi;
while (fi->next) {
fu = fi;
fi = fi->next;
free (fu);
fu = NULL;
}
free (fu);
}
static void add_fi (fnditem* n, unsigned char* blk, int patlen) {
fnditem* p;
for (p=n; p->next!=NULL; p=p->next);
p->next = (fnditem*) malloc (sizeof (fnditem));
p = p->next;
memcpy (p->str, blk, patlen);
p->next = NULL;
}
static int is_fi_present(fnditem* n, unsigned char* blk , int patlen) {
fnditem* p;
2014-04-03 22:55:16 +02:00
for (p=n; p->next!=NULL; p=p->next)
if (!memcmp (blk, p->str, patlen))
2015-09-14 02:08:31 +02:00
return true;
return false;
}
R_API int r_search_pattern(RSearch *s, ut64 from, ut64 to) {
ut8 block[BSIZE+MAX_PATLEN], sblk[BSIZE+MAX_PATLEN+1];
2014-04-03 22:55:16 +02:00
ut64 addr, bact, bytes, intaddr, rb, bproc = 0;
int nr,i, moar=0, pcnt, cnt = 0, k = 0;
int patlen = s->pattern_size;
fnditem* root;
eprintf ("Searching patterns between 0x%08"PFMT64x" and 0x%08"PFMT64x"\n", from, to);
if (patlen < 1 || patlen > MAX_PATLEN) {
eprintf ("Invalid pattern length (must be > 1 and < %d)\n", MAX_PATLEN);
2015-09-14 02:08:31 +02:00
return false;
}
bact = from;
bytes = to;
2014-04-03 22:55:16 +02:00
//bytes += bact;
root = init_fi ();
pcnt = -1;
2014-04-03 22:55:16 +02:00
// bact = from
// bytes = to
// bproc = from2
while (bact < bytes) {
addr = bact;
2014-05-14 23:24:46 +02:00
if (r_print_is_interrupted ()) {
break;
}
2014-04-03 22:55:16 +02:00
bproc = bact + patlen ;
// read ( fd, sblk, patlen );
2009-03-06 12:53:19 +01:00
//XXX bytepattern should be used with a read callback
2016-12-18 17:14:30 +01:00
nr = ((bytes - bproc) < BSIZE)?(bytes - bproc):BSIZE;
//XXX radare_read_at(bact, sblk, patlen);
2014-04-03 22:55:16 +02:00
rb = s->iob.read_at (s->iob.io, addr, sblk, nr);
sblk[patlen] = 0; // XXX
intaddr = bact;
cnt = 0;
while (bproc < bytes) {
2014-04-03 22:55:16 +02:00
// TODO: handle ^C here
2016-12-18 17:14:30 +01:00
nr = ((bytes - bproc) < BSIZE)?(bytes - bproc):BSIZE;
nr += (patlen - (nr % patlen)); // tamany de bloc llegit multiple superior de tamany busqueda
2014-04-03 22:55:16 +02:00
rb = s->iob.read_at (s->iob.io, bproc, block, nr);
2016-12-18 17:14:30 +01:00
if (rb < 1) {
bproc += nr;
2014-04-03 22:55:16 +02:00
break;
}
nr = rb;
addr += nr;
moar = 0;
2016-12-18 17:14:30 +01:00
for (i = 0; i<nr; i++) {
if (!memcmp (&block[i], sblk, patlen) && !is_fi_present (root, sblk, patlen)){
if (cnt == 0) {
add_fi (root, sblk, patlen);
pcnt++;
eprintf ("\nbytes: %d: ", pcnt);
for (k = 0; k<patlen; k++)
eprintf ("%02x", sblk[k]);
eprintf ("\nfound: %d: 0x%08"PFMT64x" ", pcnt, intaddr);
}
moar++;
cnt++;
eprintf ("0x%08"PFMT64x" ", bproc+i);
}
}
if (moar>0)
eprintf ("\ncount: %d: %d\n", pcnt, moar+1);
bproc += rb;
}
2016-12-18 17:14:30 +01:00
bact += (moar > 0)? patlen: 1;
}
eprintf ("\n");
fini_fi (root);
return 0;
}