* Some code cleanup and add some more checks on r_bin (-15LOC)
* Added new type R_TRUFAE
This commit is contained in:
parent
d1db55e261
commit
57415c2107
4 changed files with 75 additions and 86 deletions
119
libr/bin/bin.c
119
libr/bin/bin.c
|
|
@ -31,6 +31,10 @@ static struct r_bin_string_t *get_strings(struct r_bin_t *bin, int min)
|
|||
max_str = (ut64)(len/min);
|
||||
|
||||
ret = malloc(max_str*sizeof(struct r_bin_string_t));
|
||||
if (ret == NULL) {
|
||||
fprintf(stderr, "Error allocating file\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buf = malloc(len);
|
||||
if (buf == NULL) {
|
||||
|
|
@ -60,9 +64,7 @@ static struct r_bin_string_t *get_strings(struct r_bin_t *bin, int min)
|
|||
matches = 0;
|
||||
}
|
||||
}
|
||||
|
||||
ret[ctr].last = 1;
|
||||
|
||||
free(buf);
|
||||
|
||||
return ret;
|
||||
|
|
@ -75,18 +77,17 @@ struct r_bin_t *r_bin_new()
|
|||
return bin;
|
||||
}
|
||||
|
||||
void r_bin_free(struct r_bin_t *bin)
|
||||
void *r_bin_free(struct r_bin_t *bin)
|
||||
{
|
||||
free(bin);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int r_bin_init(struct r_bin_t *bin)
|
||||
{
|
||||
int i;
|
||||
bin->cur = NULL;
|
||||
bin->user = NULL;
|
||||
bin->file = NULL;
|
||||
bin->rw = 0;
|
||||
bin->cur = bin->user = bin->file = NULL;
|
||||
INIT_LIST_HEAD(&bin->bins);
|
||||
for(i=0;bin_static_plugins[i];i++)
|
||||
r_bin_add(bin, bin_static_plugins[i]);
|
||||
|
|
@ -125,9 +126,10 @@ int r_bin_list(struct r_bin_t *bin)
|
|||
|
||||
int r_bin_open(struct r_bin_t *bin, const char *file, int rw, char *plugin_name)
|
||||
{
|
||||
if (file != NULL)
|
||||
bin->file = file;
|
||||
else return -1;
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
bin->file = file;
|
||||
bin->rw = rw;
|
||||
|
||||
struct list_head *pos;
|
||||
|
|
@ -137,7 +139,6 @@ int r_bin_open(struct r_bin_t *bin, const char *file, int rw, char *plugin_name)
|
|||
(h->check && h->check(bin)))
|
||||
bin->cur = h;
|
||||
}
|
||||
|
||||
if (bin->cur && bin->cur->open)
|
||||
return bin->cur->open(bin);
|
||||
if (plugin_name && !strcmp(plugin_name, "bin_dummy"))
|
||||
|
|
@ -149,134 +150,118 @@ int r_bin_close(struct r_bin_t *bin)
|
|||
{
|
||||
if (bin->cur && bin->cur->close)
|
||||
return bin->cur->close(bin);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
ut64 r_bin_get_baddr(struct r_bin_t *bin)
|
||||
R_API ut64 r_bin_get_baddr(struct r_bin_t *bin)
|
||||
{
|
||||
if (bin->cur && bin->cur->baddr)
|
||||
return bin->cur->baddr(bin);
|
||||
|
||||
return -1;
|
||||
return UT64_MAX;
|
||||
}
|
||||
|
||||
struct r_bin_entry_t* r_bin_get_entry(struct r_bin_t *bin)
|
||||
R_API struct r_bin_entry_t* r_bin_get_entry(struct r_bin_t *bin)
|
||||
{
|
||||
if (bin->cur && bin->cur->entry)
|
||||
return bin->cur->entry(bin);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct r_bin_section_t* r_bin_get_sections(struct r_bin_t *bin)
|
||||
R_API struct r_bin_section_t* r_bin_get_sections(struct r_bin_t *bin)
|
||||
{
|
||||
if (bin->cur && bin->cur->sections)
|
||||
return bin->cur->sections(bin);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct r_bin_symbol_t* r_bin_get_symbols(struct r_bin_t *bin)
|
||||
R_API struct r_bin_symbol_t* r_bin_get_symbols(struct r_bin_t *bin)
|
||||
{
|
||||
if (bin->cur && bin->cur->symbols)
|
||||
return bin->cur->symbols(bin);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct r_bin_import_t* r_bin_get_imports(struct r_bin_t *bin)
|
||||
R_API struct r_bin_import_t* r_bin_get_imports(struct r_bin_t *bin)
|
||||
{
|
||||
if (bin->cur && bin->cur->imports)
|
||||
return bin->cur->imports(bin);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct r_bin_string_t* r_bin_get_strings(struct r_bin_t *bin)
|
||||
R_API struct r_bin_string_t* r_bin_get_strings(struct r_bin_t *bin)
|
||||
{
|
||||
if (bin->cur && bin->cur->strings)
|
||||
return bin->cur->strings(bin);
|
||||
else return get_strings(bin, 5);
|
||||
|
||||
return NULL;
|
||||
return get_strings(bin, 5);
|
||||
}
|
||||
|
||||
struct r_bin_info_t* r_bin_get_info(struct r_bin_t *bin)
|
||||
R_API struct r_bin_info_t* r_bin_get_info(struct r_bin_t *bin)
|
||||
{
|
||||
if (bin->cur && bin->cur->info)
|
||||
return bin->cur->info(bin);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct r_bin_field_t* r_bin_get_fields(struct r_bin_t *bin)
|
||||
R_API struct r_bin_field_t* r_bin_get_fields(struct r_bin_t *bin)
|
||||
{
|
||||
if (bin->cur && bin->cur->fields)
|
||||
return bin->cur->fields(bin);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ut64 r_bin_get_section_offset(struct r_bin_t *bin, char *name)
|
||||
R_API ut64 r_bin_get_section_offset(struct r_bin_t *bin, char *name)
|
||||
{
|
||||
struct r_bin_section_t *sections;
|
||||
ut64 ret = -1;
|
||||
ut64 ret = UT64_MAX;
|
||||
int i;
|
||||
|
||||
if (!(sections = r_bin_get_sections(bin)))
|
||||
return R_FALSE;
|
||||
|
||||
for (i = 0; !sections[i].last; i++)
|
||||
if (!strcmp(sections[i].name, name)) {
|
||||
ret = sections[i].offset;
|
||||
break;
|
||||
}
|
||||
|
||||
free(sections);
|
||||
|
||||
sections = r_bin_get_sections(bin);
|
||||
if (sections) {
|
||||
for (i = 0; !sections[i].last; i++)
|
||||
if (!strcmp(sections[i].name, name)) {
|
||||
ret = sections[i].offset;
|
||||
break;
|
||||
}
|
||||
free(sections);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ut64 r_bin_get_section_rva(struct r_bin_t *bin, char *name)
|
||||
R_API ut64 r_bin_get_section_rva(struct r_bin_t *bin, char *name)
|
||||
{
|
||||
struct r_bin_section_t *sections;
|
||||
ut64 ret = -1;
|
||||
ut64 ret = UT64_MAX;
|
||||
int i;
|
||||
|
||||
if (!(sections = r_bin_get_sections(bin)))
|
||||
return R_FALSE;
|
||||
|
||||
for (i = 0; !sections[i].last; i++) {
|
||||
if (!strcmp(sections[i].name, name)) {
|
||||
ret = sections[i].rva;
|
||||
break;
|
||||
sections = r_bin_get_sections(bin);
|
||||
if (sections) {
|
||||
for (i=0; !sections[i].last; i++) {
|
||||
if (!strcmp(sections[i].name, name)) {
|
||||
ret = sections[i].rva;
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(sections);
|
||||
}
|
||||
|
||||
free(sections);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
ut64 r_bin_get_section_size(struct r_bin_t *bin, char *name)
|
||||
R_API ut64 r_bin_get_section_size(struct r_bin_t *bin, char *name)
|
||||
{
|
||||
struct r_bin_section_t *sections;
|
||||
ut64 ret = -1;
|
||||
ut64 ret = UT64_MAX;
|
||||
int i;
|
||||
|
||||
if (!(sections = r_bin_get_sections(bin)))
|
||||
return R_FALSE;
|
||||
|
||||
for (i = 0; !sections[i].last; i++) {
|
||||
if (!strcmp(sections[i].name, name)) {
|
||||
ret = sections[i].size;
|
||||
break;
|
||||
sections = r_bin_get_sections(bin);
|
||||
if (sections) {
|
||||
for (i=0; !sections[i].last; i++) {
|
||||
if (!strcmp(sections[i].name, name)) {
|
||||
ret = sections[i].size;
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(sections);
|
||||
}
|
||||
|
||||
free(sections);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -243,19 +243,19 @@ static int cmd_help(void *data, const char *input)
|
|||
break;
|
||||
case '+':
|
||||
if (input[1]) {
|
||||
if (core->num.value & U64_GT0)
|
||||
if (core->num.value & UT64_GT0)
|
||||
r_core_cmd(core, input+1, 0);
|
||||
} else r_cons_printf("0x%llx\n", core->num.value);
|
||||
break;
|
||||
case '-':
|
||||
if (input[1]) {
|
||||
if (core->num.value & U64_LT0)
|
||||
if (core->num.value & UT64_LT0)
|
||||
r_core_cmd(core, input+1, 0);
|
||||
} else r_cons_printf("0x%llx\n", core->num.value);
|
||||
break;
|
||||
case '!': // ??
|
||||
if (input[1]) {
|
||||
if (core->num.value != U64_MIN)
|
||||
if (core->num.value != UT64_MIN)
|
||||
r_core_cmd(core, input+1, 0);
|
||||
} else r_cons_printf("0x%llx\n", core->num.value);
|
||||
break;
|
||||
|
|
@ -301,7 +301,7 @@ static int cmd_help(void *data, const char *input)
|
|||
return 0;
|
||||
} else
|
||||
if (input[1]) {
|
||||
if (core->num.value == U64_MIN)
|
||||
if (core->num.value == UT64_MIN)
|
||||
r_core_cmd(core, input+1, 0);
|
||||
} else r_cons_printf("0x%llx\n", core->num.value);
|
||||
break;
|
||||
|
|
@ -771,7 +771,7 @@ static int cmd_write(void *data, const char *input)
|
|||
{
|
||||
ut64 off = r_num_math(&core->num, input+1);
|
||||
r_io_lseek(&core->io, core->file->fd, core->seek, R_IO_SEEK_SET);
|
||||
if (off&U64_32U) {
|
||||
if (off&UT64_32U) {
|
||||
/* 8 byte addr */
|
||||
ut64 addr8;
|
||||
memcpy((ut8*)&addr8, (ut8*)&off, 8); // XXX needs endian here
|
||||
|
|
|
|||
|
|
@ -99,15 +99,15 @@ static inline int ERR(char *str, ...)
|
|||
#define eprintf(x,y...) fprintf(stderr,x,##y)
|
||||
|
||||
/* limits */
|
||||
#define U64_MAX 0xFFFFFFFFFFFFFFFFLL
|
||||
#define U64_GT0 0x8000000000000000LL
|
||||
#define U64_LT0 0x7FFFFFFFFFFFFFFFLL
|
||||
#define U64_MIN 0LL
|
||||
#define U64_32U 0xFFFFFFFF00000000LL
|
||||
#define U32_MIN 0
|
||||
#define U32_GT0 0x80000000
|
||||
#define U32_LT0 0x7FFFFFFF
|
||||
#define U32_MAX 0xFFFFFFFF
|
||||
#define UT64_MAX 0xFFFFFFFFFFFFFFFFLL
|
||||
#define UT64_GT0 0x8000000000000000LL
|
||||
#define UT64_LT0 0x7FFFFFFFFFFFFFFFLL
|
||||
#define UT64_MIN 0LL
|
||||
#define UT64_32U 0xFFFFFFFF00000000LL
|
||||
#define UT32_MIN 0
|
||||
#define UT32_GT0 0x80000000
|
||||
#define UT32_LT0 0x7FFFFFFF
|
||||
#define UT32_MAX 0xFFFFFFFF
|
||||
|
||||
#define R_MAX(x,y) (x>y)?x:y
|
||||
#define R_MIN(x,y) (x>y)?y:x
|
||||
|
|
@ -115,6 +115,7 @@ static inline int ERR(char *str, ...)
|
|||
|
||||
#define R_FALSE 0
|
||||
#define R_TRUE 1
|
||||
#define R_TRUFAE 2
|
||||
|
||||
#define R_FREE(x) { free(x); x = NULL; }
|
||||
|
||||
|
|
|
|||
|
|
@ -136,7 +136,7 @@ int r_trace_add(struct r_trace_t *tr, ut64 addr, int opsize)
|
|||
if (tr->dup) {
|
||||
if (tr->tag != -1)
|
||||
t->tags |= tr->tag;
|
||||
++(t->times);
|
||||
t->times++;
|
||||
} else {
|
||||
t->count = ++tr->num;
|
||||
gettimeofday(&(t->tm), NULL);
|
||||
|
|
@ -148,6 +148,10 @@ int r_trace_add(struct r_trace_t *tr, ut64 addr, int opsize)
|
|||
}
|
||||
|
||||
t = MALLOC_STRUCT(struct r_trace_item_t);
|
||||
if (t == NULL) {
|
||||
printf("Cannot alloc\n");
|
||||
return -1;
|
||||
}
|
||||
memset(t,'\0',sizeof(struct r_trace_item_t));
|
||||
t->addr = addr;
|
||||
t->times = 1;
|
||||
|
|
@ -184,7 +188,7 @@ ut64 r_trace_range(struct r_trace_t *t, ut64 from, int tag)
|
|||
|
||||
ut64 r_trace_next(struct r_trace_t *tr, ut64 from, int tag)
|
||||
{
|
||||
ut64 next = U64_MAX;
|
||||
ut64 next = UT64_MAX;
|
||||
struct list_head *pos;
|
||||
struct r_trace_item_t *h;
|
||||
|
||||
|
|
@ -195,9 +199,8 @@ ut64 r_trace_next(struct r_trace_t *tr, ut64 from, int tag)
|
|||
if (h->addr > from && h->addr < next)
|
||||
next = h->addr;
|
||||
}
|
||||
|
||||
if (next == U64_MAX)
|
||||
return U64_MIN;
|
||||
if (next == UT64_MAX)
|
||||
return UT64_MIN;
|
||||
return next;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue