Fix build and implement basic, print_hexdump works with null instance

This commit is contained in:
pancake 2014-02-16 03:11:41 +01:00
parent 36518d2ae6
commit c406144aa7
4 changed files with 158 additions and 65 deletions

View File

@ -1,5 +1,6 @@
OBJ_JAVA=anal_java.o
SHARED_JAVA+=../../shlr/java/ops.o
SHARED_JAVA+=../../shlr/java/code.o
SHARED_JAVA+=../../shlr/java/class.o
SHARED2_JAVA=$(addprefix ../,${SHARED_JAVA})
OBJ_JAVA+=${SHARED2_JAVA}

View File

@ -148,7 +148,8 @@ static int PE_(r_bin_pe_init_hdr)(struct PE_(r_bin_pe_obj_t)* bin) {
}
static int PE_(r_bin_pe_init_sections)(struct PE_(r_bin_pe_obj_t)* bin) {
int sections_size = sizeof(PE_(image_section_header)) * bin->nt_headers->file_header.NumberOfSections;
int sections_size = sizeof (PE_(image_section_header)) *
bin->nt_headers->file_header.NumberOfSections;
if (sections_size > bin->size) {
eprintf ("Invalid NumberOfSections value\n");
return R_FALSE;
@ -187,7 +188,7 @@ static int PE_(r_bin_pe_init_imports)(struct PE_(r_bin_pe_obj_t) *bin) {
perror("malloc (import directory)");
return R_FALSE;
}
if (r_buf_read_at(bin->b, import_dir_offset, (ut8*)bin->import_directory, import_dir_size) == -1) {
if (r_buf_read_at (bin->b, import_dir_offset, (ut8*)bin->import_directory, import_dir_size) == -1) {
eprintf("Error: read (import directory)\n");
return R_FALSE;
}
@ -197,9 +198,9 @@ static int PE_(r_bin_pe_init_imports)(struct PE_(r_bin_pe_obj_t) *bin) {
perror("malloc (delay import directory)");
return R_FALSE;
}
if (r_buf_read_at(bin->b, delay_import_dir_offset,
if (r_buf_read_at (bin->b, delay_import_dir_offset,
(ut8*)bin->delay_import_directory, delay_import_dir_size) == -1) {
eprintf("Error: read (delay import directory)\n");
eprintf ("Error: read (delay import directory)\n");
return R_FALSE;
}
}
@ -207,12 +208,89 @@ static int PE_(r_bin_pe_init_imports)(struct PE_(r_bin_pe_obj_t) *bin) {
}
static int PE_(r_bin_pe_init_exports)(struct PE_(r_bin_pe_obj_t) *bin) {
int i;
PE_(image_data_directory) *data_dir_export = \
&bin->nt_headers->optional_header.DataDirectory[PE_IMAGE_DIRECTORY_ENTRY_EXPORT];
PE_DWord export_dir_offset = PE_(r_bin_pe_rva_to_offset)(bin, data_dir_export->VirtualAddress);
#if 0
// STAB PARSER
{
ut8 *stab = NULL;
int stab_sz = 0;
ut8 *stabst = NULL;
int n, stabst_sz = 0;
struct r_bin_pe_section_t* sections = PE_(r_bin_pe_get_sections)(bin);
for (i = 0; i < bin->nt_headers->file_header.NumberOfSections; i++) {
if (!strcmp (sections[i].name, ".stab")) {
stab = malloc ( ( stab_sz = sections[i].size ) );
r_buf_read_at (bin->b, sections[i].offset, stab, stab_sz);
}
if (!strcmp (sections[i].name, ".stabst")) {
stabst_sz = sections[i].size;
eprintf ("Stab String Table found\n");
stabst = malloc (sections[i].size);
r_buf_read_at (bin->b, sections[i].offset, stabst, stabst_sz);
}
}
if (stab && stabst) {
__attribute__ ((packed))
struct stab_item {
#if R_BIN_PE64
ut64 n_strx; /* index into string table of name */
#else
ut32 n_strx; /* index into string table of name */
#endif
ut8 n_type; /* type of symbol */
ut8 n_other; /* misc info (usually empty) */
ut16 n_desc; /* description field */
#if R_BIN_PE64
ut64 n_value; /* value of symbol (bfd_vma) */
#else
ut32 n_value; /* value of symbol (bfd_vma) */
#endif
};
eprintf ("SIZE IS %d\n", sizeof (struct stab_item));
ut8 *p = stab;
struct stab_item *si = p;
#if 0
struct internal_nlist {
ut32 n_strx; /* index into string table of name */
ut8 n_type; /* type of symbol */
ut8 n_other; /* misc info (usually empty) */
ut16 n_desc; /* description field */
ut32 n_value; /* value of symbol (bfd_vma) */
};
#endif
n = 0;
i = 0;
#define getstring(x) (x<stabst_sz)?stabst+x:"???"
while (i<stab_sz) {
printf ("%d vs %d\n", i, stab_sz);
if (si->n_strx>0) {
printf ("%d stridx = 0x%x\n", n, si->n_strx);
printf ("%d string = %s\n", n, getstring (si->n_strx));
printf ("%d type = 0x%x\n", n, si->n_type);
printf ("%d value = 0x%llx\n", n, (ut64)si->n_value);
}
i += 12; //sizeof (struct stab_item);
si = stab + i;
n++;
}
// TODO : iterate over all stab elements
} else {
// you failed //
}
free (stab);
free (stabst);
free (sections);
}
#endif
if (export_dir_offset == 0) {
eprintf ("Warning: Cannot find the offset of the export directory\n");
// eprintf ("Warning: Cannot find the offset of the export directory\n");
return R_FALSE;
}
//sdb_setn (DB, "hdr.exports_directory", export_dir_offset);
@ -631,7 +709,7 @@ struct r_bin_pe_section_t* PE_(r_bin_pe_get_sections)(struct PE_(r_bin_pe_obj_t)
PE_(image_section_header) *shdr = bin->section_header;
int i, sections_count = bin->nt_headers->file_header.NumberOfSections;
if ((sections = malloc((sections_count + 1) * sizeof(struct r_bin_pe_section_t))) == NULL) {
if ((sections = malloc ((sections_count + 1) * sizeof (struct r_bin_pe_section_t))) == NULL) {
perror ("malloc (sections)");
return NULL;
}

View File

@ -223,9 +223,7 @@ R_API void r_buf_deinit(RBuffer *b) {
if (b->mmap) {
r_file_mmap_free (b->mmap);
b->mmap = NULL;
} else {
free (b->buf);
}
} else free (b->buf);
}
R_API void r_buf_free(struct r_buf_t *b) {

View File

@ -73,6 +73,7 @@ R_API void r_print_unset_flags(RPrint *p, int flags) {
}
R_API void r_print_set_cursor(RPrint *p, int enable, int ocursor, int cursor) {
if (!p) return;
p->cur_enabled = enable;
p->ocur = ocursor;
if (cursor<0) cursor = 0;
@ -80,7 +81,7 @@ R_API void r_print_set_cursor(RPrint *p, int enable, int ocursor, int cursor) {
}
R_API void r_print_cursor(RPrint *p, int cur, int set) {
if (!p->cur_enabled)
if (!p || !p->cur_enabled)
return;
if (p->ocur != -1) {
int from = p->ocur;
@ -93,25 +94,28 @@ R_API void r_print_cursor(RPrint *p, int cur, int set) {
p->printf ("%s", R_CONS_INVERT (set, 1)); //r_cons_invert (set, 1); //p->flags&R_PRINT_FLAGS_COLOR);
}
#define P(x) (p->cons &&p->cons->pal.x)?p->cons->pal.x
R_API void r_print_addr(RPrint *p, ut64 addr) {
int mod = p->flags & R_PRINT_FLAGS_ADDRMOD;
char ch = (p->addrmod&&mod)?((addr%p->addrmod)?' ':','):' ';
if (p->flags & R_PRINT_FLAGS_SEGOFF) {
#define PREOFF(x) (p && p->cons &&p->cons->pal.x)?p->cons->pal.x
PrintfCallback printfmt = (PrintfCallback) (p? p->printf: printf);
int mod = p? (p->flags & R_PRINT_FLAGS_ADDRMOD): 0;
char ch = p? ((p->addrmod&&mod)?((addr%p->addrmod)?' ':','):' '): ' ';
int use_color = p? (p->flags & R_PRINT_FLAGS_COLOR): 0;
int use_segoff = p? (p->flags & R_PRINT_FLAGS_SEGOFF): 0;
if (use_segoff) {
ut32 s, a;
a = addr & 0xffff;
s = (addr-a)>>4;
if (p->flags & R_PRINT_FLAGS_COLOR) {
const char *pre = P(offset): Color_GREEN;
if (use_color) {
const char *pre = PREOFF(offset): Color_GREEN;
const char *fin = Color_RESET;
p->printf ("%s%04x:%04x%c%s", pre, s & 0xffff, a & 0xffff, ch, fin);
} else p->printf ("%04x:%04x%c", s & 0xffff, a & 0xffff, ch);
printfmt ("%s%04x:%04x%c%s", pre, s & 0xffff, a & 0xffff, ch, fin);
} else printfmt ("%04x:%04x%c", s & 0xffff, a & 0xffff, ch);
} else {
if (p->flags & R_PRINT_FLAGS_COLOR) {
const char *pre = P(offset): Color_GREEN;
if (use_color) {
const char *pre = PREOFF(offset): Color_GREEN;
const char *fin = Color_RESET;
p->printf ("%s0x%08"PFMT64x"%c%s", pre, addr, ch, fin);
} else p->printf ("0x%08"PFMT64x"%c", addr, ch);
printfmt ("%s0x%08"PFMT64x"%c%s", pre, addr, ch, fin);
} else printfmt ("0x%08"PFMT64x"%c", addr, ch);
}
}
@ -181,12 +185,13 @@ R_API char *r_print_hexpair(RPrint *p, const char *str, int n) {
}
R_API void r_print_byte(RPrint *p, const char *fmt, int idx, ut8 ch) {
PrintfCallback printfmt = (PrintfCallback) (p? p->printf: printf);
ut8 rch = ch;
if (!IS_PRINTABLE (ch) && fmt[0]=='%'&&fmt[1]=='c')
rch = '.';
r_print_cursor (p, idx, 1);
//if (p->flags & R_PRINT_FLAGS_CURSOR && idx == p->cur) {
if (p->flags & R_PRINT_FLAGS_COLOR) {
if (p && p->flags & R_PRINT_FLAGS_COLOR) {
#define P(x) (p->cons &&p->cons->pal.x)?p->cons->pal.x
char *color_0x00 = P(b0x00): Color_GREEN;
char *color_0x7f = P(b0x7f): Color_YELLOW;
@ -203,10 +208,10 @@ R_API void r_print_byte(RPrint *p, const char *fmt, int idx, ut8 ch) {
pre = color_text;
else pre = color_other;
}
if (pre) p->printf (pre);
p->printf (fmt, rch);
if (pre) p->printf (Color_RESET);
} else p->printf (fmt, rch);
if (pre) printfmt (pre);
printfmt (fmt, rch);
if (pre) printfmt (Color_RESET);
} else printfmt (fmt, rch);
r_print_cursor (p, idx, 0);
}
@ -359,13 +364,29 @@ static int check_sparse (const ut8 *p, int len, int ch) {
// XXX: step is borken
R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int base, int step) {
int i, j, k, inc;
int i, j, k, inc = 16;
int sparse_char = 0;
int use_sparse = p->flags & R_PRINT_FLAGS_SPARSE;
int stride = 0;
int col = 0; // selected column (0=none, 1=hex, 2=ascii)
int use_sparse = 0;
int use_header = 1;
int use_offset = 1;
int use_segoff = 0;
const char *fmt = "%02x";
const char *pre = "";
int last_sparse = 0;
PrintfCallback printfmt = (PrintfCallback) printf;
if (p) {
use_sparse = p->flags & R_PRINT_FLAGS_SPARSE;
use_header = p->flags & R_PRINT_FLAGS_HEADER;
use_segoff = p->flags & R_PRINT_FLAGS_SEGOFF;
use_offset = p->flags & R_PRINT_FLAGS_OFFSET;
inc = p->cols;
col = p->col;
printfmt = (PrintfCallback) p->printf;
stride = p->stride;
}
if (step<1) step = 1;
switch (base) {
@ -376,51 +397,46 @@ R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int ba
}
// TODO: Use base to change %03o and so on
if (p == NULL) {
// TODO: use defaults r_print_t (static one)
eprintf ("TODO: r_print_hexdump does not supports NULL as arg0\n");
return;
}
inc = p->cols;
if ((base<32) && (p->flags & R_PRINT_FLAGS_HEADER)) {
if ((base<32) && use_header) {
ut32 opad = (ut32)(addr >> 32);
{ // XXX: use r_print_addr_header
int i, delta;
char soff[32];
if (p->flags & R_PRINT_FLAGS_SEGOFF) {
if (use_segoff) {
ut32 s, a;
a = addr & 0xffff;
s = ((addr-a)>>4 ) &0xffff;
snprintf (soff, sizeof (soff), "%04x:%04x ", s, a);
p->printf ("- offset -");
printfmt ("- offset -");
} else {
p->printf ("- offset - ");
printfmt ("- offset - ");
snprintf (soff, sizeof (soff), "0x%08"PFMT64x, addr);
}
delta = strlen (soff) - 10;
for (i=0; i<delta; i++)
p->printf (i+1==delta?" ":" ");
printfmt (i+1==delta?" ":" ");
}
p->printf (p->col==1?"|":" ");
printfmt (col==1?"|":" ");
opad >>= 4;
k = 0; // TODO: ??? SURE??? config.seek & 0xF;
/* extra padding for offsets > 8 digits */
for (i=0; i<inc; i++) {
p->printf (pre);
p->printf (" %c", hex[(i+k)%16]);
printfmt (pre);
printfmt (" %c", hex[(i+k)%16]);
if (i&1)
p->printf (p->col!=1?" ":((i+1)<inc)?" ":"|");
printfmt (col!=1?" ":((i+1)<inc)?" ":"|");
}
p->printf ((p->col==2)? "|": " ");
printfmt ((col==2)? "|": " ");
for (i=0; i<inc; i++)
p->printf ("%c", hex[(i+k)%16]);
p->printf (p->col==2?"|\n":"\n");
printfmt ("%c", hex[(i+k)%16]);
printfmt (col==2?"|\n":"\n");
}
p->interrupt = 0;
for (i=j=0; !p->interrupt && i<len; i+=(p->stride?p->stride:inc), j+=(p->stride?p->stride:0)) {
if (p) p->interrupt = 0;
//for (i=j=0; (p&&!p->interrupt) && i<len; i+=(stride?stride:inc), j+=(stride?stride:0)) {
for (i=j=0; i<len; i+=(stride?stride:inc), j+=(stride?stride:0)) {
if (use_sparse) {
if (check_sparse (buf+i, inc, sparse_char)) {
if (i+inc>=len || check_sparse (buf+i+inc, inc, sparse_char)) {
@ -428,7 +444,7 @@ R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int ba
sparse_char = buf[j];
last_sparse++;
if (last_sparse==2) {
p->printf (" ...\n");
printfmt (" ...\n");
continue;
}
if (last_sparse>2) continue;
@ -436,23 +452,23 @@ R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int ba
}
} else last_sparse = 0;
}
if (p->flags & R_PRINT_FLAGS_OFFSET)
if (use_offset)
r_print_addr (p, addr+j);
p->printf ((p->col==1)? "|": " ");
printfmt ((col==1)? "|": " ");
for (j=i; j<i+inc; j++) {
if (j>=len) {
if (p->col==1) {
if (col==1) {
if (j+1>=inc+i)
p->printf (j%2?" |":"| ");
else p->printf (j%2?" ":" ");
} else p->printf (j%2?" ":" ");
printfmt (j%2?" |":"| ");
else printfmt (j%2?" ":" ");
} else printfmt (j%2?" ":" ");
continue;
}
if (base==32) {
ut32 n;
memcpy (&n, buf+j, sizeof (n));
r_print_cursor (p, j, 1);
p->printf ("0x%08x ", n);
printfmt ("0x%08x ", n);
r_print_cursor (p, j, 0);
j += 3;
} else
@ -464,26 +480,26 @@ R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int ba
memcpy (&a, buf+j, 4);
memcpy (&b, buf+j+4, 4);
r_print_cursor (p, j, 1);
p->printf ("0x%08x%08x ", b, a); //n<<32, n&0xffffff);
printfmt ("0x%08x%08x ", b, a); //n<<32, n&0xffffff);
r_print_cursor (p, j, 0);
j += 7;
} else {
r_print_byte (p, fmt, j, buf[j]);
if (j%2) {
if (p->col==1) {
if (col==1) {
if (j+1<inc+i)
p->printf (" ");
else p->printf ("|");
} else p->printf (" ");
printfmt (" ");
else printfmt ("|");
} else printfmt (" ");
}
}
}
p->printf ((p->col==2)? "|":" ");
printfmt ((col==2)? "|":" ");
for (j=i; j<i+inc; j++) {
if (j >= len) p->printf (" ");
if (j >= len) printfmt (" ");
else r_print_byte (p, "%c", j, buf[j]);
}
p->printf (p->col==2?"|\n":"\n");
printfmt (col==2?"|\n":"\n");
}
}