mirror of
https://github.com/radareorg/radare2.git
synced 2024-11-28 15:41:38 +00:00
Add r_sign_foreach(). Add dummy z/. Make r2-indent happy
This commit is contained in:
parent
dbe351e372
commit
28f8a23632
@ -17,7 +17,7 @@ static bool deserialize(RSignItem *it, const char *k, const char *v) {
|
||||
v2 = r_str_new (v);
|
||||
|
||||
// Deserialize key: zign,<space>,<name>
|
||||
for (ptr = k2, i = 0; ; ptr = NULL, i++) {
|
||||
for (ptr = k2, i = 0;; ptr = NULL, i++) {
|
||||
token = strtok (ptr, "|");
|
||||
if (!token) {
|
||||
break;
|
||||
@ -37,7 +37,7 @@ static bool deserialize(RSignItem *it, const char *k, const char *v) {
|
||||
}
|
||||
|
||||
// Deserialize val: <type>,size,bytes,mask
|
||||
for (ptr = v2, i = 0; ; ptr = NULL, i++) {
|
||||
for (ptr = v2, i = 0;; ptr = NULL, i++) {
|
||||
token = strtok (ptr, "|");
|
||||
if (!token) {
|
||||
break;
|
||||
@ -52,11 +52,11 @@ static bool deserialize(RSignItem *it, const char *k, const char *v) {
|
||||
break;
|
||||
case 2:
|
||||
it->bytes = malloc (it->size);
|
||||
r_hex_str2bin(token, it->bytes);
|
||||
r_hex_str2bin (token, it->bytes);
|
||||
break;
|
||||
case 3:
|
||||
it->mask = malloc (it->size);
|
||||
r_hex_str2bin(token, it->mask);
|
||||
r_hex_str2bin (token, it->mask);
|
||||
break;
|
||||
default:
|
||||
retval = false;
|
||||
@ -131,7 +131,7 @@ R_API bool r_sign_add_anal(RAnal *a, const char *name, ut64 size, const ut8 *byt
|
||||
bool retval = true;
|
||||
|
||||
mask = r_anal_mask (a, size, bytes);
|
||||
retval = r_sign_add(a, R_SIGN_ANAL, name, size, bytes, mask);
|
||||
retval = r_sign_add (a, R_SIGN_ANAL, name, size, bytes, mask);
|
||||
|
||||
free (mask);
|
||||
return retval;
|
||||
@ -143,7 +143,7 @@ struct ctxDeleteCB {
|
||||
};
|
||||
|
||||
int zignDeleteCB(void *user, const char *k, const char *v) {
|
||||
struct ctxDeleteCB *ctx = (struct ctxDeleteCB *)user;
|
||||
struct ctxDeleteCB *ctx = (struct ctxDeleteCB *) user;
|
||||
|
||||
if (r_str_cmp (k, ctx->buf, strlen (ctx->buf))) {
|
||||
sdb_remove (ctx->anal->sdb_zigns, k, 0);
|
||||
@ -171,7 +171,7 @@ R_API bool r_sign_delete(RAnal *a, const char *name) {
|
||||
}
|
||||
|
||||
// Remove specific zign
|
||||
it.name = (char*)name;
|
||||
it.name = (char *) name;
|
||||
it.space = a->zign_spaces.space_idx;
|
||||
serialize (&it, buf, NULL);
|
||||
return sdb_remove (a->sdb_zigns, buf, 0);
|
||||
@ -184,7 +184,7 @@ struct ctxListCB {
|
||||
};
|
||||
|
||||
int zignListCB(void *user, const char *k, const char *v) {
|
||||
struct ctxListCB *ctx = (struct ctxListCB *)user;
|
||||
struct ctxListCB *ctx = (struct ctxListCB *) user;
|
||||
RSignItem *it = R_NEW0 (RSignItem);
|
||||
RAnal *a = ctx->anal;
|
||||
char *bytes = NULL;
|
||||
@ -203,7 +203,7 @@ int zignListCB(void *user, const char *k, const char *v) {
|
||||
a->cb_printf (",");
|
||||
}
|
||||
|
||||
for (i = 0; i < it->size; i++){
|
||||
for (i = 0; i < it->size; i++) {
|
||||
if (!it->mask[i]) {
|
||||
bytes = r_str_concatf (bytes, "..");
|
||||
} else {
|
||||
@ -225,7 +225,7 @@ int zignListCB(void *user, const char *k, const char *v) {
|
||||
a->cb_printf ("{");
|
||||
}
|
||||
a->cb_printf ("\"name\": \"%s\", \"type\": \"%c\", \"bytes\": \"%s\"}",
|
||||
it->name, it->type, bytes);
|
||||
it->name, it->type, bytes);
|
||||
} else {
|
||||
if (it->space >= 0) {
|
||||
a->cb_printf ("%s.", a->zign_spaces.spaces[it->space]);
|
||||
@ -243,7 +243,7 @@ exit_function:
|
||||
}
|
||||
|
||||
R_API void r_sign_list(RAnal *a, int format) {
|
||||
struct ctxListCB ctx = {0, format, a};
|
||||
struct ctxListCB ctx = { 0, format, a };
|
||||
|
||||
if (format == 'j') {
|
||||
a->cb_printf ("[");
|
||||
@ -262,7 +262,7 @@ struct ctxCountForCB {
|
||||
};
|
||||
|
||||
int zignCountForCB(void *user, const char *k, const char *v) {
|
||||
struct ctxCountForCB *ctx = (struct ctxCountForCB *)user;
|
||||
struct ctxCountForCB *ctx = (struct ctxCountForCB *) user;
|
||||
RSignItem *it = R_NEW0 (RSignItem);
|
||||
|
||||
if (!deserialize (it, k, v)) {
|
||||
@ -282,7 +282,7 @@ exit_function:
|
||||
|
||||
|
||||
R_API int r_sign_space_count_for(RAnal *a, int idx) {
|
||||
struct ctxCountForCB ctx = {idx, 0};
|
||||
struct ctxCountForCB ctx = { idx, 0 };
|
||||
|
||||
sdb_foreach (a->sdb_zigns, zignCountForCB, &ctx);
|
||||
|
||||
@ -295,7 +295,7 @@ struct ctxUnsetForCB {
|
||||
};
|
||||
|
||||
int zignUnsetForCB(void *user, const char *k, const char *v) {
|
||||
struct ctxUnsetForCB *ctx = (struct ctxUnsetForCB *)user;
|
||||
struct ctxUnsetForCB *ctx = (struct ctxUnsetForCB *) user;
|
||||
char nk[R_SIGN_KEY_MAXSZ], nv[R_SIGN_VAL_MAXSZ];
|
||||
RSignItem *it = R_NEW0 (RSignItem);
|
||||
Sdb *db = ctx->anal->sdb_zigns;
|
||||
@ -323,11 +323,46 @@ exit_function:
|
||||
}
|
||||
|
||||
R_API void r_sign_space_unset_for(RAnal *a, int idx) {
|
||||
struct ctxUnsetForCB ctx = {idx, a};
|
||||
struct ctxUnsetForCB ctx = { idx, a };
|
||||
|
||||
sdb_foreach (a->sdb_zigns, zignUnsetForCB, &ctx);
|
||||
}
|
||||
|
||||
struct ctxForeachCB {
|
||||
RAnal *anal;
|
||||
RSignForeachCallback cb;
|
||||
void *user;
|
||||
};
|
||||
|
||||
int zignForeachCB(void *user, const char *k, const char *v) {
|
||||
struct ctxForeachCB *ctx = (struct ctxForeachCB *) user;
|
||||
RSignItem *it = R_NEW0 (RSignItem);
|
||||
RAnal *a = ctx->anal;
|
||||
int retval = 1;
|
||||
|
||||
if (!deserialize (it, k, v)) {
|
||||
eprintf ("error: cannot deserialize zign\n");
|
||||
goto exit_function;
|
||||
}
|
||||
|
||||
if (a->zign_spaces.space_idx != it->space && a->zign_spaces.space_idx != -1) {
|
||||
goto exit_function;
|
||||
}
|
||||
|
||||
retval = ctx->cb (ctx->user, it);
|
||||
|
||||
exit_function:
|
||||
r_sign_item_free (it);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
R_API void r_sign_foreach(RAnal *a, RSignForeachCallback cb, void *user) {
|
||||
struct ctxForeachCB ctx = { a, cb, user };
|
||||
|
||||
sdb_foreach (a->sdb_zigns, zignForeachCB, &ctx);
|
||||
}
|
||||
|
||||
R_API void r_sign_item_free(void *_item) {
|
||||
if (!_item) {
|
||||
return;
|
||||
|
@ -80,7 +80,7 @@ exit_func:
|
||||
}
|
||||
|
||||
static int zignAddAnal(void *data, const char *input) {
|
||||
RCore *core = (RCore *)data;
|
||||
RCore *core = (RCore *) data;
|
||||
|
||||
switch (*input) {
|
||||
case ' ':
|
||||
@ -149,14 +149,13 @@ static int zignAddAnal(void *data, const char *input) {
|
||||
break;
|
||||
default:
|
||||
eprintf ("usage: zaa[f] [args]\n");
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static int zignAddExact(void *data, const char *input) {
|
||||
RCore *core = (RCore *)data;
|
||||
RCore *core = (RCore *) data;
|
||||
|
||||
switch (*input) {
|
||||
case ' ':
|
||||
@ -225,14 +224,13 @@ static int zignAddExact(void *data, const char *input) {
|
||||
break;
|
||||
default:
|
||||
eprintf ("usage: zae[f] [args]\n");
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static int zignAdd(void *data, const char *input) {
|
||||
RCore *core = (RCore *)data;
|
||||
RCore *core = (RCore *) data;
|
||||
|
||||
switch (*input) {
|
||||
case 'a':
|
||||
@ -253,36 +251,36 @@ static int zignAdd(void *data, const char *input) {
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
eprintf ("usage: za[aemg] [args]\n");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static int zignLoad(void *data, const char *input) {
|
||||
RCore *core = (RCore *)data;
|
||||
RCore *core = (RCore *) data;
|
||||
|
||||
switch (*input) {
|
||||
case '?':
|
||||
{
|
||||
const char *help_msg[] = {
|
||||
"Usage:", "zo[dz] [args] ", "# Load zignatures from file",
|
||||
"zo ", "filename", "load zignatures from file",
|
||||
"zod ", "filename", "load zinatures from sdb file",
|
||||
"zoz ", "filename", "load zinagures from gzip file",
|
||||
NULL};
|
||||
r_core_cmd_help (core, help_msg);
|
||||
}
|
||||
break;
|
||||
{
|
||||
const char *help_msg[] = {
|
||||
"Usage:", "zo[dz] [args] ", "# Load zignatures from file",
|
||||
"zo ", "filename", "load zignatures from file",
|
||||
"zod ", "filename", "load zinatures from sdb file",
|
||||
"zoz ", "filename", "load zinagures from gzip file",
|
||||
NULL};
|
||||
r_core_cmd_help (core, help_msg);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
eprintf ("usage: zo[dz] [args]\n");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static int zignSpace(void *data, const char *input) {
|
||||
RCore *core = (RCore *)data;
|
||||
RCore *core = (RCore *) data;
|
||||
RSpaces *zs = &core->anal->zign_spaces;
|
||||
|
||||
switch (*input) {
|
||||
@ -351,14 +349,13 @@ static int zignSpace(void *data, const char *input) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static int zignFlirt(void *data, const char *input) {
|
||||
RCore *core = (RCore *)data;
|
||||
RCore *core = (RCore *) data;
|
||||
|
||||
switch (*input) {
|
||||
case '?':
|
||||
@ -373,39 +370,129 @@ static int zignFlirt(void *data, const char *input) {
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
eprintf ("usage: zf[dsz] filename\n");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
struct ctxDoSearchCB {
|
||||
RCore *core;
|
||||
ut64 from;
|
||||
ut64 to;
|
||||
bool rad;
|
||||
};
|
||||
|
||||
int zignDoSearchCB(void *user, RSignItem *it) {
|
||||
struct ctxDoSearchCB *ctx = (struct ctxDoSearchCB *) user;
|
||||
|
||||
eprintf ("name=%s from=0x%08"PFMT64x" to=0x%08"PFMT64x" rad=%d\n",
|
||||
it->name, ctx->from, ctx->to, ctx->rad);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static bool zignDoSearch(RCore *core, ut64 from, ut64 to, bool rad) {
|
||||
RList *list;
|
||||
RListIter *iter;
|
||||
RIOMap *map;
|
||||
struct ctxDoSearchCB ctx = { core, from, to, rad };
|
||||
bool search_all = false;
|
||||
bool retval = true;
|
||||
|
||||
if (from == 0 && to == 0) {
|
||||
search_all = true;
|
||||
} else if (to <= from) {
|
||||
eprintf ("error: invalid rage 0x%08"PFMT64x"-0x%08"PFMT64x"\n", from, to);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (search_all) {
|
||||
eprintf ("[+] searching all maps\n");
|
||||
|
||||
list = r_core_get_boundaries_ok (core);
|
||||
if (!list) {
|
||||
eprintf ("Invalid boundaries\n");
|
||||
return false;
|
||||
}
|
||||
r_list_foreach (list, iter, map) {
|
||||
ctx.from = map->from;
|
||||
ctx.to = map->to;
|
||||
r_sign_foreach (core->anal, zignDoSearchCB, &ctx);
|
||||
}
|
||||
r_list_free (list);
|
||||
} else {
|
||||
eprintf ("[+] searching from = 0x%08"PFMT64x" to = 0x%08"PFMT64x"\n", from, to);
|
||||
r_sign_foreach (core->anal, zignDoSearchCB, &ctx);
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
static int zignSearch(void *data, const char *input) {
|
||||
RCore *core = (RCore *)data;
|
||||
RCore *core = (RCore *) data;
|
||||
|
||||
switch (*input) {
|
||||
case '\x00':
|
||||
case ' ':
|
||||
break;
|
||||
case '*':
|
||||
{
|
||||
ut64 from = 0, to = 0;
|
||||
char *args = NULL;
|
||||
int n = 0;
|
||||
bool retval = true;
|
||||
|
||||
if (input[0]) {
|
||||
args = r_str_new (input + 1);
|
||||
n = r_str_word_set0(args);
|
||||
} else {
|
||||
n = 0;
|
||||
}
|
||||
|
||||
switch (n) {
|
||||
case 2:
|
||||
from = r_num_math (core->num, r_str_word_get0(args, 0));
|
||||
to = r_num_math (core->num, r_str_word_get0(args, 1));
|
||||
break;
|
||||
case 1:
|
||||
from = core->offset;
|
||||
to = r_num_math (core->num, r_str_word_get0(args, 0));
|
||||
break;
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
eprintf ("usage: z/ [from] [to]\n");
|
||||
retval = false;
|
||||
goto exit_case;
|
||||
}
|
||||
|
||||
retval = zignDoSearch(core, from, to, input[0] == '*');
|
||||
|
||||
exit_case:
|
||||
free (args);
|
||||
|
||||
return retval;
|
||||
}
|
||||
break;
|
||||
case '?':
|
||||
{
|
||||
const char *help_msg[] = {
|
||||
"Usage:", "z/[*] [ini] [end] ", "# Search signatures",
|
||||
"z/ ", "[ini] [end]", "search zignatures on range and flag matches",
|
||||
"z/* ", "[ini] [end]", "search zignatures on range and output radare commands",
|
||||
"Usage:", "z/[*] [from] [to] ", "# Search signatures",
|
||||
"z/ ", "[from] [to]", "search zignatures on range and flag matches",
|
||||
"z/* ", "[from] [to]", "search zignatures on range and output radare commands",
|
||||
NULL};
|
||||
r_core_cmd_help (core, help_msg);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
eprintf ("usage: z/[*] [from] [to]\n");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static int cmd_zign(void *data, const char *input) {
|
||||
RCore *core = (RCore *)data;
|
||||
RCore *core = (RCore *) data;
|
||||
|
||||
switch (*input) {
|
||||
case '\0':
|
||||
@ -450,7 +537,7 @@ static int cmd_zign(void *data, const char *input) {
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
eprintf ("usage: z[*j-aof/cs] [args]\n");
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -28,11 +28,14 @@ typedef struct r_sign_item_t {
|
||||
ut8 *mask;
|
||||
} RSignItem;
|
||||
|
||||
typedef int (*RSignForeachCallback)(void *user, RSignItem *it);
|
||||
|
||||
#ifdef R_API
|
||||
R_API bool r_sign_add(RAnal *a, int type, const char *name, ut64 size, const ut8 *bytes, const ut8 *mask);
|
||||
R_API bool r_sign_add_anal(RAnal *a, const char *name, ut64 size, const ut8 *bytes);
|
||||
R_API bool r_sign_delete(RAnal *a, const char *name);
|
||||
R_API void r_sign_list(RAnal *a, int format);
|
||||
R_API void r_sign_foreach(RAnal *a, RSignForeachCallback cb, void *user);
|
||||
R_API void r_sign_item_free(void *_item);
|
||||
#endif
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user