2011-10-05 00:38:37 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2011 */
|
2010-08-12 12:54:24 +00:00
|
|
|
/* nibble<.ds@gmail.com> */
|
|
|
|
|
|
|
|
#include <r_types.h>
|
|
|
|
#include <r_core.h>
|
|
|
|
#include <r_asm.h>
|
|
|
|
|
2010-09-30 17:25:47 +00:00
|
|
|
R_API RCoreAsmHit *r_core_asm_hit_new() {
|
|
|
|
RCoreAsmHit *hit = R_NEW (RCoreAsmHit);
|
|
|
|
if (hit) {
|
|
|
|
hit->code = NULL;
|
2010-10-06 16:04:47 +00:00
|
|
|
hit->len = 0;
|
2010-09-30 17:25:47 +00:00
|
|
|
hit->addr = -1;
|
|
|
|
}
|
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API RList *r_core_asm_hit_list_new() {
|
|
|
|
RList *list = r_list_new ();
|
|
|
|
list->free = &r_core_asm_hit_free;
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_core_asm_hit_free(void *_hit) {
|
|
|
|
RCoreAsmHit *hit = _hit;
|
|
|
|
if (hit) {
|
|
|
|
if (hit->code)
|
|
|
|
free (hit->code);
|
|
|
|
free (hit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-12 12:54:24 +00:00
|
|
|
R_API char* r_core_asm_search(RCore *core, const char *input, ut64 from, ut64 to) {
|
|
|
|
RAsmCode *acode;
|
|
|
|
char *ret;
|
|
|
|
if (!(acode = r_asm_massemble (core->assembler, input)))
|
|
|
|
return NULL;
|
|
|
|
ret = strdup (acode->buf_hex);
|
|
|
|
r_asm_code_free (acode);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-08-19 00:30:12 +00:00
|
|
|
#define OPSZ 8
|
2011-11-22 14:12:29 +00:00
|
|
|
// TODO: add support for byte-per-byte opcode search
|
2010-09-30 17:25:47 +00:00
|
|
|
R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut64 to) {
|
|
|
|
RCoreAsmHit *hit;
|
2011-02-24 15:50:29 +00:00
|
|
|
RAsmOp op;
|
2010-09-30 17:25:47 +00:00
|
|
|
RList *hits;
|
2010-08-12 12:54:24 +00:00
|
|
|
ut64 at, toff = core->offset;
|
|
|
|
ut8 *buf;
|
2010-09-30 18:55:20 +00:00
|
|
|
char *tok, *tokens[1024], *code = NULL, *ptr;
|
2010-08-12 12:54:24 +00:00
|
|
|
int idx, tidx, ret, len;
|
2010-09-30 17:25:47 +00:00
|
|
|
int tokcount, matchcount;
|
2010-08-12 12:54:24 +00:00
|
|
|
|
2010-10-06 16:04:47 +00:00
|
|
|
if (core->blocksize<=OPSZ) {
|
|
|
|
eprintf ("error: block size too small\n");
|
2010-09-30 18:55:20 +00:00
|
|
|
return NULL;
|
2010-10-06 16:04:47 +00:00
|
|
|
}
|
|
|
|
if (!(buf = (ut8 *)malloc (core->blocksize))){
|
2010-09-30 17:25:47 +00:00
|
|
|
return NULL;
|
2010-10-06 16:04:47 +00:00
|
|
|
}
|
|
|
|
if (!(ptr = strdup (input))) {
|
|
|
|
free (buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!(hits = r_core_asm_hit_list_new ())) {
|
|
|
|
free (buf);
|
|
|
|
free (ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-11-22 14:12:29 +00:00
|
|
|
for (tokcount=0; ; tokcount++) {
|
|
|
|
tok = strtok (tokcount? NULL: ptr, ",");
|
2010-08-12 12:54:24 +00:00
|
|
|
if (tok == NULL)
|
|
|
|
break;
|
2010-09-30 17:25:47 +00:00
|
|
|
tokens[tokcount] = r_str_trim_head_tail (tok);
|
2010-08-12 12:54:24 +00:00
|
|
|
}
|
2010-09-30 17:25:47 +00:00
|
|
|
for (at = from, matchcount = 0; at < to; at += core->blocksize-OPSZ) {
|
2010-08-12 12:54:24 +00:00
|
|
|
if (r_cons_singleton ()->breaked)
|
|
|
|
break;
|
|
|
|
ret = r_io_read_at (core->io, at, buf, core->blocksize);
|
|
|
|
if (ret != core->blocksize)
|
|
|
|
break;
|
|
|
|
idx = 0, matchcount = 0;
|
|
|
|
while (idx<core->blocksize) {
|
|
|
|
r_asm_set_pc (core->assembler, at+idx);
|
2011-02-24 15:50:29 +00:00
|
|
|
if (!(len = r_asm_disassemble (core->assembler, &op, buf+idx, core->blocksize-idx))) {
|
2010-08-12 12:54:24 +00:00
|
|
|
if (matchcount != 0)
|
|
|
|
idx = tidx+1;
|
|
|
|
else idx++;
|
|
|
|
matchcount = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2011-02-24 15:50:29 +00:00
|
|
|
if (strstr (op.buf_asm, tokens[matchcount])) {
|
2011-07-12 21:22:33 +00:00
|
|
|
code = r_str_concatf (code, "%s", op.buf_asm);
|
2010-08-12 12:54:24 +00:00
|
|
|
if (matchcount == tokcount-1) {
|
|
|
|
if (tokcount == 1)
|
|
|
|
tidx = idx;
|
2010-09-30 17:25:47 +00:00
|
|
|
if (!(hit = r_core_asm_hit_new ())) {
|
|
|
|
r_list_destroy (hits);
|
2010-10-06 16:04:47 +00:00
|
|
|
free (buf);
|
|
|
|
free (ptr);
|
|
|
|
free (code);
|
2010-09-30 17:25:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
hit->addr = at+tidx;
|
2010-10-06 16:04:47 +00:00
|
|
|
hit->len = idx+len-tidx;
|
2011-11-22 01:00:48 +00:00
|
|
|
if (hit->len == -1) {
|
|
|
|
r_core_asm_hit_free (hit);
|
|
|
|
return hits;
|
|
|
|
}
|
2010-09-30 17:25:47 +00:00
|
|
|
hit->code = strdup (code);
|
|
|
|
r_list_append (hits, hit);
|
|
|
|
R_FREE (code);
|
2010-08-12 12:54:24 +00:00
|
|
|
matchcount = 0;
|
|
|
|
idx = tidx+1;
|
|
|
|
} else if (matchcount == 0) {
|
|
|
|
tidx = idx;
|
|
|
|
matchcount++;
|
|
|
|
idx += len;
|
|
|
|
} else {
|
|
|
|
matchcount++;
|
|
|
|
idx += len;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (matchcount != 0)
|
|
|
|
idx = tidx+1;
|
|
|
|
else idx++;
|
2010-09-30 17:25:47 +00:00
|
|
|
R_FREE (code);
|
2010-08-12 12:54:24 +00:00
|
|
|
matchcount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r_asm_set_pc (core->assembler, toff);
|
|
|
|
free (buf);
|
2010-09-30 18:55:20 +00:00
|
|
|
free (ptr);
|
|
|
|
free (code);
|
2010-09-30 17:25:47 +00:00
|
|
|
return hits;
|
2010-08-12 12:54:24 +00:00
|
|
|
}
|
2010-10-06 16:04:47 +00:00
|
|
|
|
|
|
|
R_API RList *r_core_asm_bwdisassemble (RCore *core, ut64 addr, int n, int len) {
|
|
|
|
RCoreAsmHit *hit;
|
2011-02-24 15:50:29 +00:00
|
|
|
RAsmOp op;
|
2010-10-06 16:04:47 +00:00
|
|
|
RList *hits = NULL;
|
|
|
|
ut8 *buf;
|
|
|
|
ut64 at;
|
|
|
|
int instrlen, ni, idx;
|
|
|
|
|
|
|
|
if (!(hits = r_core_asm_hit_list_new ()))
|
|
|
|
return NULL;
|
|
|
|
buf = (ut8 *)malloc (len);
|
|
|
|
if (!buf) {
|
|
|
|
r_list_destroy (hits);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (r_io_read_at (core->io, addr-len, buf, len) != len) {
|
|
|
|
r_list_destroy (hits);
|
|
|
|
free (buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (idx = 1; idx < len; idx++) {
|
|
|
|
if (r_cons_singleton ()->breaked)
|
|
|
|
break;
|
|
|
|
at = addr - idx; ni = 1;
|
|
|
|
while (at < addr) {
|
|
|
|
r_asm_set_pc (core->assembler, at);
|
|
|
|
//XXX HACK We need another way to detect invalid disasm!!
|
2011-02-24 15:50:29 +00:00
|
|
|
if (!(instrlen = r_asm_disassemble (core->assembler, &op, buf+(len-(addr-at)), addr-at)) || strstr (op.buf_asm, "invalid")) {
|
2010-10-06 16:04:47 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
at += instrlen;
|
|
|
|
if (at == addr) {
|
|
|
|
if (ni == n) {
|
|
|
|
if (!(hit = r_core_asm_hit_new ())) {
|
|
|
|
r_list_destroy (hits);
|
|
|
|
free (buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
hit->addr = addr-idx;
|
|
|
|
hit->len = idx;
|
|
|
|
hit->code = NULL;
|
|
|
|
r_list_append (hits, hit);
|
|
|
|
}
|
2011-10-05 00:38:37 +00:00
|
|
|
} else ni++;
|
2010-10-06 16:04:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r_asm_set_pc (core->assembler, addr);
|
|
|
|
free (buf);
|
|
|
|
return hits;
|
|
|
|
}
|