Improve r_buf_buffer and remove r_buf_get_at (#13676)

* Make r_buf_buffer return a ut8 and the associated size of the buffer
* Remove r_buf_get_at uses and make it static
This commit is contained in:
Riccardo Schirone 2019-04-15 13:24:15 +02:00 committed by GitHub
parent 3d6f20bb67
commit 820e3dbe73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
49 changed files with 450 additions and 298 deletions

View File

@ -37,7 +37,9 @@ R_API bool r_bin_wr_output(RBin *bin, const char *filename) {
if (!filename || !binfile || !binfile->buf) {
return false;
}
return r_file_dump (filename, r_buf_buffer (binfile->buf), r_buf_size (binfile->buf), 0);
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (binfile->buf, &tmpsz);
return r_file_dump (filename, tmp, tmpsz, 0);
}
R_API bool r_bin_wr_entry(RBin *bin, ut64 addr) {

View File

@ -40,10 +40,10 @@
#define READWORD(x, i) READ32 (x, i)
#endif
#define BREAD8(x, i) r_read_ble8(r_buf_get_at (x, (i), NULL)); (i) += 1
#define BREAD16(x, i) r_read_ble16(r_buf_get_at (x, (i), NULL), bin->endian); (i) += 2
#define BREAD32(x, i) r_read_ble32(r_buf_get_at (x, (i), NULL), bin->endian); (i) += 4
#define BREAD64(x, i) r_read_ble64(r_buf_get_at (x, (i), NULL), bin->endian); (i) += 8
#define BREAD8(x, i) r_buf_read_ble8_at(x, i); (i) += 1
#define BREAD16(x, i) r_buf_read_ble16_at(x, i, bin->endian); (i) += 2
#define BREAD32(x, i) r_buf_read_ble32_at(x, i, bin->endian); (i) += 4
#define BREAD64(x, i) r_buf_read_ble64_at(x, i, bin->endian); (i) += 8
#if R_BIN_ELF64
static inline int UTX_MUL(ut64 *r, ut64 a, ut64 b) {

View File

@ -2751,10 +2751,13 @@ void MACH0_(mach_headerfields)(RBinFile *file) {
#endif
}
break;
case LC_ID_DYLIB: // install_name_tool
case LC_ID_DYLIB: { // install_name_tool
char *id = r_buf_get_string (buf, addr + 20);
cb_printf ("0x%08"PFMT64x" id %s\n",
addr + 20, r_buf_get_at (buf, addr + 20, NULL));
addr + 20, id? id: "");
free (id);
break;
}
case LC_UUID:
{
ut8 i, uuid[16];
@ -2776,17 +2779,24 @@ void MACH0_(mach_headerfields)(RBinFile *file) {
}
break;
case LC_LOAD_DYLIB:
case LC_LOAD_WEAK_DYLIB:
case LC_LOAD_WEAK_DYLIB: {
char *load_dylib = r_buf_get_string (buf, addr + 16);
cb_printf ("0x%08"PFMT64x" load_dylib %s\n",
addr + 16, r_buf_get_at (buf, addr + 16, NULL));
addr + 16, load_dylib? load_dylib: "");
free (load_dylib);
break;
case LC_RPATH:
cb_printf ("0x%08"PFMT64x" rpath %s\n",
addr + 4, r_buf_get_at (buf, addr + 4, NULL));
}
case LC_RPATH: {
char *rpath = r_buf_get_string (buf, addr + 4);
cb_printf ("0x%08" PFMT64x " rpath %s\n",
addr + 4, rpath ? rpath : "");
free (rpath);
break;
}
case LC_CODE_SIGNATURE:
{
ut32 *words = (ut32*)r_buf_get_at (buf, addr, NULL);
ut32 words[2];
r_buf_read_at (buf, addr, (ut8 *)words, sizeof (words));
cb_printf ("0x%08"PFMT64x" dataoff 0x%08x\n", addr, words[0]);
cb_printf ("0x%08"PFMT64x" datasize %d\n", addr + 4, words[1]);
cb_printf ("# wtf mach0.sign %d @ 0x%x\n", words[1], words[0]);

View File

@ -934,9 +934,12 @@ static bool r_bin_mdmp_init_pe_bins(struct r_bin_mdmp_obj *obj) {
if (!(paddr = r_bin_mdmp_get_paddr (obj, module->base_of_image))) {
continue;
}
int left = 0;
const ut8 *b = r_buf_get_at (obj->b, paddr, &left);
buf = r_buf_new_with_bytes (b, R_MIN (left, module->size_of_image));
ut8 *b = R_NEWS (ut8, module->size_of_image);
if (!b) {
continue;
}
int r = r_buf_read_at (obj->b, paddr, b, module->size_of_image);
buf = r_buf_new_with_bytes (b, r);
dup = false;
if (check_pe32_buf (buf, module->size_of_image)) {
r_list_foreach(obj->pe32_bins, it_dup, pe32_dup) {

View File

@ -88,8 +88,7 @@ RList *r_bin_mz_get_segments (const struct r_bin_mz_obj_t *bin) {
for (i = 0; i < num_relocs; i++) {
RBinSection c;
ut64 laddr, paddr, section_laddr;
ut16 *curr_seg;
int left;
ut16 curr_seg;
laddr = r_bin_mz_va_to_la (relocs[i].segment, relocs[i].offset);
if ((laddr + 2) >= bin->load_module_size) {
@ -97,12 +96,12 @@ RList *r_bin_mz_get_segments (const struct r_bin_mz_obj_t *bin) {
}
paddr = r_bin_mz_la_to_pa (bin, laddr);
curr_seg = (ut16 *)r_buf_get_at (bin->b, paddr, &left);
if (left < 2) {
if (r_buf_size (bin->b) < paddr + 2) {
continue;
}
curr_seg = r_buf_read_le16_at (bin->b, paddr);
section_laddr = r_bin_mz_va_to_la (r_read_le16 (curr_seg), 0);
section_laddr = r_bin_mz_va_to_la (curr_seg, 0);
if (section_laddr > bin->load_module_size) {
continue;
}

View File

@ -15,14 +15,10 @@ ut32 readLE32(RBuffer *buf, int off) {
ut32 num = 0;
(void)r_buf_read_at (buf, off, (ut8 *)&num, 4);
return num;
//const ut8 *data = r_buf_get_at (buf, off, &left);
//return left > 3? r_read_le32 (data): 0;
}
ut64 readLE64(RBuffer *buf, int off) {
int left = 0;
const ut8 *data = r_buf_get_at (buf, off, &left);
return left > 7? r_read_le64 (data): 0;
return r_buf_size (buf) >= off + 8? r_buf_read_le64_at (buf, off): 0;
}
static char *readString(RBuffer *buf, int off) {

View File

@ -110,7 +110,9 @@ static RList *patch_relocs(RBin *b) {
}
R_FREE (bin->reloc_table);
}
b->iob.write_at (b->iob.io, 0, r_buf_buffer (bin->b), r_buf_size (bin->b));
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (bin->b, &tmpsz);
b->iob.write_at (b->iob.io, 0, tmp, tmpsz);
return list;
}

View File

@ -60,16 +60,16 @@ static char *getstr(RBinDexObj *bin, int idx) {
if (bin->strings[idx] + uleblen >= bin->strings[idx] + bin->header.strings_size) {
return NULL;
}
char* ptr = (char*) r_buf_get_at (bin->b, bin->strings[idx] + uleblen, NULL);
ut8 *ptr = R_NEWS (ut8, len + 1);
if (!ptr) {
return NULL;
}
if (len != r_utf8_strlen ((const ut8*)ptr)) {
r_buf_read_at (bin->b, bin->strings[idx] + uleblen, ptr, len + 1);
if (len != r_utf8_strlen (ptr)) {
// eprintf ("WARNING: Invalid string for index %d\n", idx);
return NULL;
}
return ptr;
return (char *)ptr;
}
static int countOnes(ut32 val) {
@ -796,7 +796,9 @@ static RBinInfo *info(RBinFile *bf) {
ret->rclass = strdup ("class");
ret->os = strdup ("linux");
const char *kw = "Landroid/support/wearable/view";
if (r_mem_mem (r_buf_buffer (bf->buf), r_buf_size (bf->buf), (const ut8*)kw, strlen (kw))) {
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (bf->buf, &tmpsz);
if (r_mem_mem (tmp, tmpsz, (const ut8 *)kw, strlen (kw))) {
ret->subsystem = strdup ("android-wear");
} else {
ret->subsystem = strdup ("android");
@ -820,7 +822,9 @@ static RBinInfo *info(RBinFile *bf) {
r_buf_read_at (bf->buf, 8, h->buf, 4);
{
ut32 fc = r_buf_read_le32_at (bf->buf, 8);
ut32 cc = __adler32 (r_buf_buffer (bf->buf) + 12, r_buf_size (bf->buf) - 12);
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (bf->buf, &tmpsz);
ut32 cc = __adler32 (tmp + 12, tmpsz - 12);
if (fc != cc) {
eprintf ("# adler32 checksum doesn't match. Type this to fix it:\n");
eprintf ("wx `ph sha1 $s-32 @32` @12 ; wx `ph adler32 $s-12 @12` @8\n");
@ -844,7 +848,7 @@ static RList *strings(RBinFile *bf) {
if (!bf || !bf->o) {
return NULL;
}
bin = (struct r_bin_dex_obj_t *) bf->o->bin_obj;
bin = (struct r_bin_dex_obj_t *)bf->o->bin_obj;
if (!bin || !bin->strings) {
return NULL;
}
@ -1102,7 +1106,7 @@ static const ut8 *parse_dex_class_method(RBinFile *binfile, RBinDexObj *bin,
bool is_direct, const ut8 *bufbuf) {
struct r_bin_t *rbin = binfile->rbin;
bool bin_dbginfo = rbin->want_dbginfo;
int i, left;
int i;
ut64 omi = 0;
bool catchAll;
ut16 regsz = 0, ins_size = 0, outs_size = 0, tries_size = 0;
@ -1154,19 +1158,19 @@ static const ut8 *parse_dex_class_method(RBinFile *binfile, RBinDexObj *bin,
R_FREE (signature);
continue;
}
const ut8 *ff2 = r_buf_get_at (binfile->buf, MC, &left);
if (!ff2 || left < 16) {
ut64 bufsz = r_buf_size (binfile->buf);
if (bufsz < MC || bufsz < MC + 16) {
//R_FREE (method_name);
R_FREE (flag_name);
R_FREE (signature);
continue;
}
regsz = r_read_le16 (ff2);
ins_size = r_read_le16 (ff2 + 2);
outs_size = r_read_le16 (ff2 + 4);
tries_size = r_read_le16 (ff2 + 6);
debug_info_off = r_read_le32 (ff2 + 8);
insns_size = r_read_le32 (ff2 + 12);
regsz = r_buf_read_le16_at (binfile->buf, MC);
ins_size = r_buf_read_le16_at (binfile->buf, MC + 2);
outs_size = r_buf_read_le16_at (binfile->buf, MC + 4);
tries_size = r_buf_read_le16_at (binfile->buf, MC + 6);
debug_info_off = r_buf_read_le32_at (binfile->buf, MC + 8);
insns_size = r_buf_read_le32_at (binfile->buf, MC + 12);
int padd = 0;
if (tries_size > 0 && insns_size % 2) {
padd = 2;
@ -1205,14 +1209,14 @@ static const ut8 *parse_dex_class_method(RBinFile *binfile, RBinDexObj *bin,
R_FREE (signature);
break;
}
const ut8 *ptr = r_buf_get_at (binfile->buf, offset, &left);
if (!ptr || left < 8) {
ut64 bufsz = r_buf_size (binfile->buf);
if (bufsz < offset || bufsz < offset + 8) {
R_FREE (signature);
break;
}
start_addr = r_read_le32 (ptr);
insn_count = r_read_le16 (ptr + 4);
handler_off = r_read_le16 (ptr + 6);
start_addr = r_buf_read_le32_at (binfile->buf, offset);
insn_count = r_buf_read_le16_at (binfile->buf, offset + 4);
handler_off = r_buf_read_le16_at (binfile->buf, offset + 6);
char* s = NULL;
if (dexdump) {
@ -1227,9 +1231,12 @@ static const ut8 *parse_dex_class_method(RBinFile *binfile, RBinDexObj *bin,
break;
}
// TODO: catch left instead of null
const ut8 *p3 = r_buf_get_at (binfile->buf, off, NULL);
const ut8 *p3_end = p3 + r_buf_size (binfile->buf) - off;
st64 size = r_sleb128 (&p3, p3_end);
st64 size;
r_buf_seek (binfile->buf, off, 0);
int r = r_buf_sleb128 (binfile->buf, &size);
if (r <= 0) {
break;
}
if (size <= 0) {
catchAll = true;
@ -1239,9 +1246,15 @@ static const ut8 *parse_dex_class_method(RBinFile *binfile, RBinDexObj *bin,
catchAll = false;
}
for (m = 0; m < size && p3 < p3_end; m++) {
p3 = r_uleb128 (p3, p3_end - p3, &handler_type);
p3 = r_uleb128 (p3, p3_end - p3, &handler_addr);
for (m = 0; m < size; m++) {
r = r_buf_uleb128 (binfile->buf, &handler_type);
if (r <= 0) {
break;
}
r = r_buf_uleb128 (binfile->buf, &handler_addr);
if (r <= 0) {
break;
}
if (handler_type > 0 && handler_type < bin->header.types_size) {
s = getstr (bin, bin->types[handler_type].descriptor_id);
@ -1259,7 +1272,10 @@ static const ut8 *parse_dex_class_method(RBinFile *binfile, RBinDexObj *bin,
}
}
if (catchAll) {
p3 = r_uleb128 (p3, p3_end - p3, &v2);
r = r_buf_uleb128 (binfile->buf, &v2);
if (r <= 0) {
break;
}
if (dexdump) {
rbin->cb_printf (" <any> -> 0x%04"PFMT64x"\n", v2);
}
@ -1307,19 +1323,14 @@ static const ut8 *parse_dex_class_method(RBinFile *binfile, RBinDexObj *bin,
sym->ordinal = (*sym_count)++;
if (MC > 0) {
const ut8 *ff2 = r_buf_get_at (binfile->buf, MC, &left);
if (!ff2 || left < 16) {
// if (r_buf_read_at (binfile->buf, binfile->buf->base + MC, ff2, 16) < 1) {
ut64 bufsz = r_buf_size (binfile->buf);
if (bufsz < MC || bufsz < MC + 16) {
R_FREE (sym);
R_FREE (signature);
continue;
}
//ut16 regsz = r_read_le16 (ff2);
//ut16 ins_size = r_read_le16 (ff2 + 2);
//ut16 outs_size = r_read_le16 (ff2 + 4);
ut16 tries_size = r_read_le16 (ff2 + 6);
//ut32 debug_info_off = r_read_le32 (ff2 + 8);
ut32 insns_size = r_read_le32 (ff2 + 12);
ut16 tries_size = r_buf_read_le16_at (binfile->buf, MC + 6);
ut32 insns_size = r_buf_read_le32_at (binfile->buf, MC + 12);
ut64 prolog_size = 2 + 2 + 2 + 2 + 4 + 4;
if (tries_size > 0) {
//prolog_size += 2 + 8*tries_size; // we need to parse all so the catch info...
@ -1496,12 +1507,7 @@ static void parse_class(RBinFile *binfile, RBinDexObj *bin, RBinDexClass *c,
bin->header.data_offset < c->interfaces_offset &&
c->interfaces_offset <
bin->header.data_offset + bin->header.data_size) {
int left;
p = r_buf_get_at (binfile->buf, c->interfaces_offset, &left);
if (left < 4) {
return;
}
int types_list_size = r_read_le32 (p);
int types_list_size = r_buf_read_le32_at (binfile->buf, c->interfaces_offset);
if (types_list_size < 0 || types_list_size >= bin->header.types_size ) {
return;
}
@ -1539,13 +1545,14 @@ static void parse_class(RBinFile *binfile, RBinDexObj *bin, RBinDexClass *c,
return;
}
const ut8 *bufbuf = r_buf_buffer (binfile->buf);
ut64 bufbufsz;
const ut8 *bufbuf = r_buf_buffer (binfile->buf, &bufbufsz);
p = bufbuf + c->class_data_offset;
// XXX may overflow
if (r_buf_size (binfile->buf) < c->class_data_offset) {
if (bufbufsz < c->class_data_offset) {
return;
}
ut32 p_size = (r_buf_size (binfile->buf) - c->class_data_offset);
ut32 p_size = (bufbufsz - c->class_data_offset);
p_end = p + p_size;
//XXX check for NULL!!
c->class_data = (struct dex_class_data_item_t *)malloc (

View File

@ -2,14 +2,13 @@
static void headers32(RBinFile *bf) {
#define p bf->rbin->cb_printf
const ut8 *buf = r_buf_get_at (bf->buf, 0, NULL);
p ("0x00000000 ELF MAGIC 0x%08x\n", r_read_le32 (buf));
p ("0x00000010 Type 0x%04x\n", r_read_le16 (buf + 0x10));
p ("0x00000012 Machine 0x%04x\n", r_read_le16 (buf + 0x12));
p ("0x00000014 Version 0x%08x\n", r_read_le32 (buf + 0x14));
p ("0x00000018 Entrypoint 0x%08x\n", r_read_le32 (buf + 0x18));
p ("0x0000001c PhOff 0x%08x\n", r_read_le32 (buf + 0x1c));
p ("0x00000020 ShOff 0x%08x\n", r_read_le32 (buf + 0x20));
p ("0x00000000 ELF MAGIC 0x%08x\n", r_buf_read_le32_at (bf->buf, 0));
p ("0x00000010 Type 0x%04x\n", r_buf_read_le16_at (bf->buf, 0x10));
p ("0x00000012 Machine 0x%04x\n", r_buf_read_le16_at (bf->buf, 0x12));
p ("0x00000014 Version 0x%08x\n", r_buf_read_le32_at (bf->buf, 0x14));
p ("0x00000018 Entrypoint 0x%08x\n", r_buf_read_le32_at (bf->buf, 0x18));
p ("0x0000001c PhOff 0x%08x\n", r_buf_read_le32_at (bf->buf, 0x1c));
p ("0x00000020 ShOff 0x%08x\n", r_buf_read_le32_at (bf->buf, 0x20));
}
static bool check_bytes(const ut8 *buf, ut64 length) {

View File

@ -1088,58 +1088,39 @@ static RBinInfo* info(RBinFile *bf) {
}
static RList* fields(RBinFile *bf) {
int left = 0;
RList *ret = NULL;
const ut8 *buf = NULL;
if (!(ret = r_list_new ())) {
RList *ret = r_list_newf ((RListFree)free);
if (!ret) {
return NULL;
}
ret->free = free;
if (!(buf = r_buf_get_at (bf->buf, 0, &left))) {
RBinField *ptr = NULL;
struct r_bin_elf_field_t *field = NULL;
int i;
if (!(field = Elf_(r_bin_elf_get_fields) (bf->o->bin_obj))) {
#define ROW(nam, siz, val, fmt) \
r_list_append (ret, r_bin_field_new (addr, addr, siz, nam, sdb_fmt ("0x%08x", val), fmt));
if (r_buf_size (bf->buf) < sizeof (Elf_ (Ehdr))) {
return ret;
}
for (i = 0; !field[i].last; i++) {
if (!(ptr = R_NEW0 (RBinField))) {
break;
}
ptr->name = strdup (field[i].name);
ptr->comment = NULL;
ptr->vaddr = field[i].offset;
ptr->paddr = field[i].offset;
r_list_append (ret, ptr);
}
free (field);
} else {
#define ROW(nam,siz,val,fmt) \
r_list_append (ret, r_bin_field_new (addr, addr, siz, nam, sdb_fmt ("0x%08x", val), fmt));
if (left < sizeof (Elf_(Ehdr))) {
return ret;
}
ut64 addr = 0;
ROW ("ELF", 4, r_read_le32 (buf), "x"); addr+=0x10;
ROW ("Type", 2, r_read_le16 (buf + addr), "x"); addr+=0x2;
ROW ("Machine", 2, r_read_le16 (buf + addr), "x"); addr+=0x2;
ROW ("Version", 4, r_read_le32 (buf + addr), "x"); addr+=0x4;
if (r_read_le8 (buf + 0x04) == 1) {
ROW ("Entry point", 4, r_read_le32 (buf + addr), "x"); addr+=0x4;
ROW ("PhOff", 4, r_read_le32 (buf + addr), "x"); addr+=0x4;
ROW ("ShOff", 4, r_read_le32 (buf + addr), "x");
} else {
ROW ("Entry point", 8, r_read_le64 (buf + addr), "x"); addr+=0x8;
ROW ("PhOff", 8, r_read_le64 (buf + addr), "x"); addr+=0x8;
ROW ("ShOff", 8, r_read_le64 (buf + addr), "x");
}
}
ut64 addr = 0;
ROW ("ELF", 4, r_buf_read_le32_at (bf->buf, addr), "x");
addr += 0x10;
ROW ("Type", 2, r_buf_read_le16_at (bf->buf, addr), "x");
addr += 0x2;
ROW ("Machine", 2, r_buf_read_le16_at (bf->buf, addr), "x");
addr += 0x2;
ROW ("Version", 4, r_buf_read_le32_at (bf->buf, addr), "x");
addr += 0x4;
if (r_buf_read8_at (bf->buf, 0x04) == 1) {
ROW ("Entry point", 4, r_buf_read_le32_at (bf->buf, addr), "x");
addr += 0x4;
ROW ("PhOff", 4, r_buf_read_le32_at (bf->buf, addr), "x");
addr += 0x4;
ROW ("ShOff", 4, r_buf_read_le32_at (bf->buf, addr), "x");
} else {
ROW ("Entry point", 8, r_buf_read_le64_at (bf->buf, addr), "x");
addr += 0x8;
ROW ("PhOff", 8, r_buf_read_le64_at (bf->buf, addr), "x");
addr += 0x8;
ROW ("ShOff", 8, r_buf_read_le64_at (bf->buf, addr), "x");
}
return ret;
}

View File

@ -23,14 +23,13 @@ static ut64 get_elf_vaddr64 (RBinFile *bf, ut64 baddr, ut64 paddr, ut64 vaddr) {
static void headers64(RBinFile *bf) {
#define p bf->rbin->cb_printf
const ut8 *buf = r_buf_get_at (bf->buf, 0, NULL);
p ("0x00000000 ELF64 0x%08x\n", r_read_le32 (buf));
p ("0x00000010 Type 0x%04x\n", r_read_le16 (buf + 0x10));
p ("0x00000012 Machine 0x%04x\n", r_read_le16 (buf + 0x12));
p ("0x00000014 Version 0x%08x\n", r_read_le32 (buf + 0x14));
p ("0x00000018 Entrypoint 0x%08"PFMT64x"\n", r_read_le64 (buf + 0x18));
p ("0x00000020 PhOff 0x%08"PFMT64x"\n", r_read_le64 (buf + 0x20));
p ("0x00000028 ShOff 0x%08"PFMT64x"\n", r_read_le64 (buf + 0x28));
p ("0x00000000 ELF64 0x%08x\n", r_buf_read_le32_at (bf->buf, 0));
p ("0x00000010 Type 0x%04x\n", r_buf_read_le16_at (bf->buf, 0x10));
p ("0x00000012 Machine 0x%04x\n", r_buf_read_le16_at (bf->buf, 0x12));
p ("0x00000014 Version 0x%08x\n", r_buf_read_le32_at (bf->buf, 0x14));
p ("0x00000018 Entrypoint 0x%08"PFMT64x"\n", r_buf_read_le64_at (bf->buf, 0x18));
p ("0x00000020 PhOff 0x%08"PFMT64x"\n", r_buf_read_le64_at (bf->buf, 0x20));
p ("0x00000028 ShOff 0x%08"PFMT64x"\n", r_buf_read_le64_at (bf->buf, 0x28));
}
static RBuffer* create(RBin* bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RBinArchOptions *opt) {

View File

@ -58,10 +58,13 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut
}
static bool load(RBinFile *bf) {
const ut8 *bytes = bf ? r_buf_buffer (bf->buf) : NULL;
ut64 sz = bf ? r_buf_size (bf->buf): 0;
ut64 la = (bf && bf->o) ? bf->o->loadaddr: 0;
return load_bytes (bf, bf? &bf->o->bin_obj: NULL, bytes, sz, la, bf? bf->sdb: NULL);
if (!bf || !bf->o) {
return false;
}
ut64 sz;
const ut8 *bytes = r_buf_buffer (bf->buf, &sz);
ut64 la = bf->o->loadaddr;
return load_bytes (bf, &bf->o->bin_obj, bytes, sz, la, bf->sdb);
}
static int destroy(RBinFile *bf) {
@ -86,11 +89,10 @@ static RBinInfo* info(RBinFile *bf) {
if (!bf) {
return NULL;
}
bytes = r_buf_buffer (bf->buf);
bytes = r_buf_buffer (bf->buf, &sz);
if (!bytes) {
return NULL;
}
sz = bf->buf ? r_buf_size (bf->buf): 0;
if (!(ret = R_NEW0 (RBinInfo))) {
return NULL;
}

View File

@ -93,16 +93,16 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut
}
static bool load(RBinFile *bf) {
int result = false;
const ut8 *bytes = bf? r_buf_buffer (bf->buf): NULL;
ut64 sz = bf? r_buf_size (bf->buf): 0;
struct r_bin_java_obj_t *bin_obj = NULL;
if (!bf || !bf->o) {
return false;
}
load_bytes (bf, (void **) &bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);
int result = false;
ut64 sz;
const ut8 *bytes = r_buf_buffer (bf->buf, &sz);
struct r_bin_java_obj_t *bin_obj = NULL;
load_bytes (bf, (void **)&bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);
if (bin_obj) {
if (!bf->o->kv) {

View File

@ -77,14 +77,14 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut
static bool load(RBinFile *bf) {
if (bf && bf->buf) {
const ut8 *bytes = r_buf_buffer (bf->buf);
ut64 sz = r_buf_size (bf->buf);
ut64 sz;
const ut8 *bytes = r_buf_buffer (bf->buf, &sz);
return load_bytes (bf, &bf->o->bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);
}
return false;
}
static int destroy (RBinFile *bf) {
static int destroy(RBinFile *bf) {
return true;
}

View File

@ -221,13 +221,12 @@ static void *load_buffer(RBinFile *bf, RBuffer *buf, ut64 loadaddr, Sdb *sdb) {
}
static bool load(RBinFile *bf) {
const ut8 *bytes = bf? r_buf_buffer (bf->buf): NULL;
ut64 sz = bf? r_buf_size (bf->buf): 0;
if (!bf || !bf->o) {
return false;
}
ut64 sz;
const ut8 *bytes = r_buf_buffer (bf->buf, &sz);
return load_bytes (bf, &bf->o->bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);
}

View File

@ -67,9 +67,9 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut
}
static bool load(RBinFile *bf) {
const ut8 *bytes = bf ? r_buf_buffer (bf->buf) : NULL;
ut64 sz = bf ? r_buf_size (bf->buf): 0;
ut64 la = (bf && bf->o) ? bf->o->loadaddr: 0;
ut64 sz;
const ut8 *bytes = r_buf_buffer (bf->buf, &sz);
ut64 la = (bf && bf->o)? bf->o->loadaddr: 0;
return load_bytes (bf, bf? &bf->o->bin_obj: NULL, bytes, sz, la, bf? bf->sdb: NULL);
}
@ -77,7 +77,7 @@ static ut64 baddr(RBinFile *bf) {
return 0; // 0x800000;
}
static ut64 menuetEntry (const ut8 *buf, int buf_size) {
static ut64 menuetEntry(const ut8 *buf, int buf_size) {
switch (MENUET_VERSION(buf)) {
case '0': return r_read_ble32 (buf + 12, false);
case '1': return r_read_ble32 (buf + 12, false);

View File

@ -16,7 +16,9 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut
}
static void *load_buffer(RBinFile *bf, RBuffer *buf, ut64 loadaddr, Sdb *sdb) {
if (!check_bytes (r_buf_get_at (buf, 0, NULL), r_buf_size (buf))) {
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (buf, &tmpsz);
if (!check_bytes (tmp, tmpsz)) {
return NULL;
}
return r_buf_new ();

View File

@ -22,11 +22,11 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut
}
static bool load(RBinFile *bf) {
const ut8 *bytes = bf? r_buf_buffer (bf->buf): NULL;
ut64 sz = bf? r_buf_size (bf->buf): 0;
if (!bf || !bf->o) {
return false;
}
ut64 sz;
const ut8 *bytes = r_buf_buffer (bf->buf, &sz);
load_bytes (bf, &bf->o->bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);
return check_bytes (bytes, sz);
}

View File

@ -33,11 +33,11 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut
}
static bool load(RBinFile *bf) {
const ut8 *bytes = bf? r_buf_buffer (bf->buf): NULL;
ut64 sz = bf? r_buf_size (bf->buf): 0;
if (!bf || !bf->o) {
return false;
}
ut64 sz;
const ut8 *bytes = r_buf_buffer (bf->buf, &sz);
load_bytes (bf, &bf->o->bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);
return check_bytes (bytes, sz);
}

View File

@ -21,12 +21,12 @@ static bool check_bytes(const ut8 *buf, ut64 length) {
}
static bool load(RBinFile *bf) {
const ut8 *bytes = bf ? r_buf_buffer (bf->buf) : NULL;
ut64 sz = bf ? r_buf_size (bf->buf): 0;
ut64 la = (bf && bf->o) ? bf->o->loadaddr: 0;
if (!bf || !bf->o) {
return false;
}
ut64 sz;
const ut8 *bytes = r_buf_buffer (bf->buf, &sz);
ut64 la = bf->o->loadaddr;
load_bytes (bf, &bf->o->bin_obj, bytes, sz, la, bf->sdb);
return check_bytes (bytes, sz);
}

View File

@ -17,11 +17,11 @@ static bool check_bytes(const ut8 *buf, ut64 length) {
}
static bool load(RBinFile *bf) {
const ut8 *bytes = bf? r_buf_buffer (bf->buf): NULL;
ut64 sz = bf? r_buf_size (bf->buf): 0;
if (!bf || !bf->o) {
return false;
}
ut64 sz;
const ut8 *bytes = r_buf_buffer (bf->buf, &sz);
bf->rbin->maxstrbuf = 0x20000000;
return check_bytes (bytes, sz);
}

View File

@ -56,9 +56,9 @@ static bool load(RBinFile *bf) {
if (!bf || !bf->buf || !bf->o) {
return false;
}
const ut64 sz = r_buf_size (bf->buf);
ut64 sz;
const ut64 la = bf->o->loadaddr;
const ut8 *bytes = r_buf_buffer (bf->buf);
const ut8 *bytes = r_buf_buffer (bf->buf, &sz);
load_bytes (bf, &bf->o->bin_obj, bytes, sz, la, bf->sdb);
return bf->o->bin_obj != NULL;
}

View File

@ -73,26 +73,49 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut
ut64 total_size = tsize + rosize + dsize;
RBuffer *newbuf = r_buf_new_empty (total_size);
ut64 ba = baddr (bf);
ut8 *tmp = NULL;
if (rbin->iob.io && !(rbin->iob.io->cached & R_PERM_W)) {
eprintf ("Please add \'-e io.cache=true\' option to r2 command. This is required to decompress the code.\n");
goto fail;
}
/* Decompress each sections */
if (decompress (buf + toff, r_buf_get_at (newbuf, 0, NULL), rooff - toff, tsize) != tsize) {
tmp = R_NEWS (ut8, tsize);
if (!tmp) {
goto fail;
}
if (decompress (buf + toff, tmp, rooff - toff, tsize) != tsize) {
eprintf ("decompression failure\n");
goto fail;
}
if (decompress (buf + rooff, r_buf_get_at (newbuf, tsize, NULL), doff - rooff, rosize) != rosize) {
r_buf_write_at (newbuf, 0, tmp, tsize);
R_FREE (tmp);
tmp = R_NEWS (ut8, rosize);
if (!tmp) {
goto fail;
}
if (decompress (buf + rooff, tmp, doff - rooff, rosize) != rosize) {
eprintf ("decompression2 failure\n");
goto fail;
}
if (decompress (buf + doff, r_buf_get_at (newbuf, tsize + rosize, NULL), r_buf_size (bf->buf) - doff, dsize) != dsize) {
r_buf_write_at (newbuf, tsize, tmp, rosize);
R_FREE (tmp);
tmp = R_NEWS (ut8, dsize);
if (!tmp) {
goto fail;
}
if (decompress (buf + doff, tmp, r_buf_size (bf->buf) - doff, dsize) != dsize) {
eprintf ("decompression3 failure\n");
goto fail;
}
r_buf_write_at (newbuf, tsize + rosize, tmp, dsize);
R_FREE (tmp);
/* Load unpacked binary */
r_io_write_at (rbin->iob.io, ba, r_buf_get_at (newbuf, 0, NULL), total_size);
const ut8 *tmpbuf = r_buf_buffer (newbuf, &total_size);
r_io_write_at (rbin->iob.io, ba, tmpbuf, total_size);
ut32 modoff = readLE32 (newbuf, NSO_OFFSET_MODMEMOFF);
RBinNXOObj *bin = nso_new ();
eprintf ("MOD Offset = 0x%"PFMT64x"\n", (ut64)modoff);
@ -101,6 +124,7 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut
*bin_obj = bin;
return true;
fail:
free (tmp);
r_buf_free (newbuf);
*bin_obj = NULL;
return false;
@ -231,7 +255,13 @@ static RBinInfo *info(RBinFile *bf) {
if (!ret) {
return NULL;
}
const char *ft = fileType (r_buf_get_at (bf->buf, NSO_OFF (magic), NULL));
ut8 magic[4];
if (r_buf_read_at (bf->buf, NSO_OFF (magic), magic, sizeof (magic)) != sizeof (magic)) {
free (ret);
return NULL;
}
const char *ft = fileType (magic);
if (!ft) {
ft = "nso";
}

View File

@ -15,11 +15,11 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 size,
}
static bool load(RBinFile *bf) {
const ut8 *byte = bf? r_buf_buffer (bf->buf): NULL;
ut64 size = bf? r_buf_size (bf->buf): 0;
if (!bf || !bf->o) {
return false;
}
ut64 size;
const ut8 *byte = r_buf_buffer (bf->buf, &size);
return load_bytes (bf, &bf->o->bin_obj, byte, size, bf->o->loadaddr, bf->sdb);
}

View File

@ -118,10 +118,8 @@ static char *signature (RBinFile *bf, bool json) {
}
static RList *fields(RBinFile *bf) {
const ut8 *buf = bf ? r_buf_buffer (bf->buf) : NULL;
RList *ret = r_list_new ();
if (!buf || !ret) {
if (!ret) {
return NULL;
}

View File

@ -53,8 +53,7 @@ static bool load(RBinFile *bf) {
if (!bf || !bf->o) {
return false;
}
bytes = r_buf_buffer (bf->buf);
sz = r_buf_size (bf->buf);
bytes = r_buf_buffer (bf->buf, &sz);
return load_bytes (bf, &bf->o->bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);
}
@ -447,11 +446,10 @@ static int haschr(const RBinFile* bf, ut16 dllCharacteristic) {
if (!bf) {
return false;
}
buf = r_buf_buffer (bf->buf);
buf = r_buf_buffer (bf->buf, &sz);
if (!buf) {
return false;
}
sz = r_buf_size (bf->buf);
idx = (buf[0x3c] | (buf[0x3d]<<8));
if (idx + 0x5E + 1 >= sz ) {
return false;

View File

@ -44,8 +44,8 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut
}
static bool load(RBinFile *bf) {
const ut8 *bytes = bf ? r_buf_buffer (bf->buf) : NULL;
ut64 sz = bf ? r_buf_size (bf->buf): 0;
ut64 sz;
const ut8 *bytes = r_buf_buffer (bf->buf, &sz);
return check_bytes (bytes, sz);
}

View File

@ -35,12 +35,12 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut
}
static bool load(RBinFile *bf) {
const ut8 *bytes = bf? r_buf_buffer (bf->buf): NULL;
ut64 sz = bf? r_buf_size (bf->buf): 0;
if (!bf || !bf->o) {
return false;
}
ut64 sz;
const ut8 *bytes = r_buf_buffer (bf->buf, &sz);
load_bytes (bf, &bf->o->bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);
return bf->o->bin_obj? true: false;
}

View File

@ -17,11 +17,11 @@ static bool check_bytes(const ut8 *buf, ut64 size) {
}
static bool load(RBinFile *bf) {
r_bin_xbe_obj_t *obj = NULL;
const ut8 *bytes = bf? r_buf_buffer (bf->buf): NULL;
if (!bf || !bf->o) {
return false;
}
r_bin_xbe_obj_t *obj = NULL;
const ut8 *bytes = r_buf_buffer (bf->buf, NULL);
bf->o->bin_obj = malloc (sizeof (r_bin_plugin_xbe));
obj = bf->o->bin_obj;
if (obj) {

View File

@ -78,7 +78,7 @@ static bool check_bytes (const ut8 *buf, ut64 length) {
}
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
if (check_bytes (r_buf_buffer (bf->buf), sz)) {
if (check_bytes (buf, sz)) {
*bin_obj = memcpy (&n64_header, buf, sizeof (N64Header));
return true;
}
@ -86,11 +86,11 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut
}
static bool load(RBinFile *bf) {
const ut8 *bytes = bf ? r_buf_buffer (bf->buf) : NULL;
ut64 sz = bf ? r_buf_size (bf->buf) : 0;
if (!bf || !bf->o) {
return false;
}
ut64 sz;
const ut8 *bytes = r_buf_buffer (bf->buf, &sz);
load_bytes (bf, &bf->o->bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);
return check_bytes (bytes, sz);
}

View File

@ -31,11 +31,11 @@ static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 size,
}
static bool load(RBinFile *bf) {
const ut8 *bytes = bf? r_buf_buffer (bf->buf): NULL;
ut64 size = bf? r_buf_size (bf->buf): 0;
if (!bf || !bf->o) {
return false;
}
ut64 size;
const ut8 *bytes = r_buf_buffer (bf->buf, &size);
return load_bytes (bf, &bf->o->bin_obj, bytes, size, bf->o->loadaddr, bf->sdb);
}

View File

@ -106,7 +106,8 @@ R_API bool r_core_dump(RCore *core, const char *file, ut64 addr, ut64 size, int
}
R_API int r_core_write_op(RCore *core, const char *arg, char op) {
int i, j, len, ret = false;
int i, j, ret = false;
ut64 len;
char *str = NULL;
ut8 *buf;
@ -134,12 +135,8 @@ R_API int r_core_write_op(RCore *core, const char *arg, char op) {
goto beach;
}
} else { // use clipboard as key
len = r_buf_size (core->yank_buf);
if (len <= 0) {
eprintf ("Clipboard is empty and no value argument(s) given\n");
goto beach;
}
str = r_mem_dup (r_buf_buffer (core->yank_buf), len);
const ut8 *tmp = r_buf_buffer (core->yank_buf, &len);
str = r_mem_dup (tmp, len);
if (!str) {
goto beach;
}

View File

@ -661,8 +661,10 @@ static int cmd_yank(void *data, const char *input) {
break;
case 't': // "wt"
if (input[1] == 'f') { // "wtf"
ut64 tmpsz;
const char *file = r_str_trim_ro (input + 2);
if (!r_file_dump (file, r_buf_buffer (core->yank_buf), r_buf_size (core->yank_buf), false)) {
const ut8 *tmp = r_buf_buffer (core->yank_buf, &tmpsz);
if (!r_file_dump (file, tmp, tmpsz, false)) {
eprintf ("Cannot dump to '%s'\n", file);
}
} else if (input[1] == ' ') {

View File

@ -5052,7 +5052,9 @@ static int cmd_debug(void *data, const char *input) {
b = r_egg_get_bin (egg);
r_asm_set_pc (core->assembler, core->offset);
r_reg_arena_push (core->dbg->reg);
r_debug_execute (core->dbg, r_buf_buffer (b), r_buf_size (b), 0);
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (b, &tmpsz);
r_debug_execute (core->dbg, tmp, tmpsz, 0);
r_reg_arena_pop (core->dbg->reg);
break;
}

View File

@ -4080,7 +4080,7 @@ static const char* bits_to_c_code_fmtstr(int bits) {
}
}
static void print_c_code(RPrint *p, ut64 addr, ut8 *buf, int len, int ws, int w) {
static void print_c_code(RPrint *p, ut64 addr, const ut8 *buf, int len, int ws, int w) {
const char *fmtstr;
int i, bits;
@ -4113,7 +4113,7 @@ static void print_c_code(RPrint *p, ut64 addr, ut8 *buf, int len, int ws, int w)
p->cb_printf ("\n};\n");
}
R_API void r_print_code(RPrint *p, ut64 addr, ut8 *buf, int len, char lang) {
R_API void r_print_code(RPrint *p, ut64 addr, const ut8 *buf, int len, char lang) {
int i, w = p->cols * 0.7;
if (w < 1) {
w = 1;

View File

@ -98,7 +98,9 @@ static int __core_patch_bracket(RCore *core, const char *str, ut64 *noff) {
if (strcmp (off, "+")) {
*noff = r_num_math (core->num, off);
}
r_core_write_at (core, *noff, r_buf_buffer (b), r_buf_size (b));
ut64 tmpsz;
const ut8 *tmpbuf = r_buf_buffer (b, &tmpsz);
r_core_write_at (core, *noff, tmpbuf, tmpsz);
*noff += r_buf_size (b);
free (off);
return 1;

View File

@ -1121,7 +1121,9 @@ static RDebugMap* linux_map_alloc (RDebug *dbg, ut64 addr, int size) {
ut64 map_addr;
r_reg_arena_push (dbg->reg);
map_addr = r_debug_execute (dbg, r_buf_buffer (buf), r_buf_size (buf), 1);
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (buf, &tmpsz);
map_addr = r_debug_execute (dbg, tmp, tmpsz, 1);
r_reg_arena_pop (dbg->reg);
if (map_addr != (ut64)-1) {
r_debug_map_sync (dbg);
@ -1145,8 +1147,9 @@ static int linux_map_dealloc(RDebug *dbg, ut64 addr, int size) {
snprintf (code, sizeof (code),
"sc_munmap@syscall(%d);\n"
"main@naked(0) { .rarg0 = sc_munmap(0x%08"PFMT64x",%d);break;\n"
"}\n", num, addr, size);
"main@naked(0) { .rarg0 = sc_munmap(0x%08" PFMT64x ",%d);break;\n"
"}\n",
num, addr, size);
r_egg_reset (dbg->egg);
r_egg_setup (dbg->egg, dbg->arch, 8 * dbg->bits, 0, 0);
r_egg_load (dbg->egg, code, 0);
@ -1161,7 +1164,9 @@ static int linux_map_dealloc(RDebug *dbg, ut64 addr, int size) {
buf = r_egg_get_bin (dbg->egg);
if (buf) {
r_reg_arena_push (dbg->reg);
ret = r_debug_execute (dbg, r_buf_buffer (buf), r_buf_size (buf), 1) == 0;
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (buf, &tmpsz);
ret = r_debug_execute (dbg, tmp, tmpsz, 1) == 0;
r_reg_arena_pop (dbg->reg);
}
err_linux_map_dealloc:
@ -2008,7 +2013,9 @@ static int r_debug_native_map_protect (RDebug *dbg, ut64 addr, int size, int per
buf = r_egg_get_bin (dbg->egg);
if (buf) {
r_reg_arena_push (dbg->reg);
r_debug_execute (dbg, r_buf_buffer (buf), r_buf_size (buf), 1);
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (buf, &tmpsz);
r_debug_execute (dbg, tmp, tmpsz, 1);
r_reg_arena_pop (dbg->reg);
return true;
}

View File

@ -378,7 +378,10 @@ R_API void r_egg_append(REgg *egg, const char *src) {
/* JIT : TODO: accept arguments here */
R_API int r_egg_run(REgg *egg) {
return r_sys_run (r_buf_buffer (egg->bin), r_buf_size (egg->bin));
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (egg->bin, &tmpsz);
bool res = r_sys_run (tmp, tmpsz);
return res;
}
#define R_EGG_FILL_TYPE_TRAP
@ -402,7 +405,7 @@ R_API int r_egg_padding(REgg *egg, const char *pad) {
ut8 *buf, padding_byte;
char *p, *o = strdup (pad);
for (p = o; *p; ) { // parse pad string
for (p = o; *p;) { // parse pad string
const char f = *p++;
number = strtol (p, NULL, 10);
@ -472,8 +475,9 @@ R_API int r_egg_shellcode(REgg *egg, const char *name) {
eprintf ("%s Shellcode has failed\n", p->name);
return false;
}
r_egg_raw (egg, r_buf_buffer (b), r_buf_size (b));
r_buf_free (b);
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (b, &tmpsz);
r_egg_raw (egg, tmp, tmpsz);
return true;
}
}
@ -522,12 +526,12 @@ R_API void r_egg_finalize(REgg *egg) {
}
r_list_foreach (egg->patches, iter, ep) {
if (ep->off < 0) {
ut64 sz = r_buf_size (ep->b);
const ut8 *buf = r_buf_buffer (ep->b);
ut64 sz;
const ut8 *buf = r_buf_buffer (ep->b, &sz);
r_egg_append_bytes (egg, buf, sz);
} else {
ut64 sz = r_buf_size (ep->b);
const ut8 *buf = r_buf_buffer (ep->b);
ut64 sz;
const ut8 *buf = r_buf_buffer (ep->b, &sz);
int r = r_buf_write_at (egg->bin, ep->off, buf, sz);
if (r < sz) {
eprintf ("Cannot patch outside\n");

View File

@ -6,6 +6,8 @@
extern "C" {
#endif
// TODO: choose whether the _at operations should preserve the current seek or not
#define R_BUF_CUR UT64_MAX
typedef struct r_buf_t {
@ -67,7 +69,7 @@ R_API bool r_buf_append_ut64(RBuffer *b, ut64 n);
R_API bool r_buf_append_ut16(RBuffer *b, ut16 n);
R_API bool r_buf_prepend_bytes(RBuffer *b, const ut8 *buf, int length);
R_API char *r_buf_to_string(RBuffer *b);
R_API ut8 *r_buf_get_at(RBuffer *b, ut64 addr, int *len);
R_API char *r_buf_get_string(RBuffer *b, ut64 addr);
#define r_buf_read(a,b,c) r_buf_read_at(a,R_BUF_CUR,b,c)
#define r_buf_write(a,b,c) r_buf_write_at(a,R_BUF_CUR,b,c)
#define r_buf_read8(b) r_buf_read8_at(b,R_BUF_CUR)
@ -81,7 +83,7 @@ R_API int r_buf_fwrite_at(RBuffer *b, ut64 addr, ut8 *buf, const char *fmt, int
R_API void r_buf_free(RBuffer *b);
R_API bool r_buf_fini(RBuffer *b);
R_API char *r_buf_free_to_string(RBuffer *b);
R_API const ut8 *r_buf_buffer(RBuffer *b);
R_API const ut8 *r_buf_buffer(RBuffer *b, ut64 *size);
R_API ut64 r_buf_size(RBuffer *b);
R_API bool r_buf_resize(RBuffer *b, ut64 newsize);
R_API RList *r_buf_nonempty_list(RBuffer *b);
@ -176,6 +178,18 @@ static inline ut64 r_buf_read_ble64_at(RBuffer *b, ut64 addr, bool big_endian) {
return r == sizeof (buf)? r_read_ble64 (buf, big_endian): UT64_MAX;
}
R_API int r_buf_uleb128(RBuffer *b, ut64 *v);
R_API int r_buf_sleb128(RBuffer *b, st64 *v);
static inline int r_buf_uleb128_at(RBuffer *b, ut64 addr, ut64 *v) {
r_buf_seek (b, addr, 0);
return r_buf_uleb128 (b, v);
}
static inline int r_buf_sleb128_at(RBuffer *b, ut64 addr, st64 *v) {
r_buf_seek (b, addr, 0);
return r_buf_sleb128(b, v);
}
#ifdef __cplusplus
}
#endif

View File

@ -156,7 +156,7 @@ R_API void r_print_cursor(RPrint *p, int cur, int len, int set);
R_API void r_print_cursor_range(RPrint *p, int cur, int to, int set);
R_API int r_print_get_cursor(RPrint *p);
R_API void r_print_set_cursor(RPrint *p, int curset, int ocursor, int cursor);
R_API void r_print_code(RPrint *p, ut64 addr, ut8 *buf, int len, char lang);
R_API void r_print_code(RPrint *p, ut64 addr, const ut8 *buf, int len, char lang);
#define SEEFLAG -2
#define JSONOUTPUT -3

View File

@ -7,7 +7,7 @@
R_API const ut8 *r_uleb128(const ut8 *data, int datalen, ut64 *v);
R_API const ut8 *r_uleb128_decode(const ut8 *data, int *datalen, ut64 *v);
R_API const ut8 *r_uleb128_encode(const ut64 s, int *len);
R_API ut8 *r_uleb128_encode(const ut64 s, int *len);
R_API const ut8 *r_leb128(const ut8 *data, st64 *v);
R_API st64 r_sleb128(const ut8 **data, const ut8 *end);
R_API size_t read_u32_leb128(const ut8 *p, const ut8 *max, ut32 *out_val);

View File

@ -121,11 +121,12 @@ R_API void r_io_free(RIO *io) {
}
R_API RIODesc *r_io_open_buffer(RIO *io, RBuffer *b, int perm, int mode) {
const int bufSize = r_buf_size (b);
ut64 bufSize = r_buf_size (b);
char *uri = r_str_newf ("malloc://%d", bufSize);
RIODesc *desc = r_io_open_nomap (io, uri, perm, mode);
if (desc) {
r_io_desc_write (desc, r_buf_get_at (b, 0, NULL), bufSize);
const ut8 *tmp = r_buf_buffer (b, &bufSize);
r_io_desc_write (desc, tmp, bufSize);
}
return desc;
}

View File

@ -93,19 +93,21 @@ static ut8 gprobe_checksum_i2c (const ut8 *p, unsigned int size, ut8 initial) {
return res;
}
static void gprobe_frame_i2c (RBuffer *frame) {
static void gprobe_frame_i2c(RBuffer *frame) {
ut8 size = r_buf_size (frame) + 1;
ut8 header[] = {0x51, 0x80 + size + 3, 0xc2, 0x00, 0x00};
r_buf_prepend_bytes (frame, &size, 1);
r_buf_prepend_bytes (frame, header, sizeof (header));
ut8 checksum = gprobe_checksum_i2c (r_buf_buffer (frame), r_buf_size (frame), GPROBE_I2C_ADDR);
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (frame, &tmpsz);
ut8 checksum = gprobe_checksum_i2c (tmp, tmpsz, GPROBE_I2C_ADDR);
r_buf_append_bytes (frame, &checksum, 1);
}
static int gprobe_get_reply_i2c (struct gport *port, ut8 cmd, RBuffer *reply) {
static int gprobe_get_reply_i2c(struct gport *port, ut8 cmd, RBuffer *reply) {
ut8 buf[131];
int count;
int ddc2bi3_len;
@ -144,14 +146,16 @@ static int gprobe_get_reply_i2c (struct gport *port, ut8 cmd, RBuffer *reply) {
return 0;
}
static int gprobe_send_request_i2c (struct gport *port, RBuffer *request) {
if (write (port->fd, r_buf_buffer (request), r_buf_size (request)) != r_buf_size (request)) {
static int gprobe_send_request_i2c(struct gport *port, RBuffer *request) {
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (request, &tmpsz);
if (write (port->fd, tmp, tmpsz) != r_buf_size (request)) {
return -1;
}
return 0;
}
static int i2c_open (struct gport *port) {
static int i2c_open(struct gport *port) {
char *end, filename[32];
int i2cbus = strtol (port->name + 4, &end, 0);
@ -180,7 +184,7 @@ static int i2c_open (struct gport *port) {
}
#endif
static int sp_close (struct gport *port) {
static int sp_close(struct gport *port) {
#if __WINDOWS__
/* Returns non-zero upon success, 0 upon failure. */
if (CloseHandle (port->hdl) == 0){
@ -192,7 +196,7 @@ static int sp_close (struct gport *port) {
#define CLOSE_OVERLAPPED(ovl) \
do { \
if (port->ovl.hEvent != INVALID_HANDLE_VALUE && \
CloseHandle (port->ovl.hEvent) == 0) \
CloseHandle (port->ovl.hEvent) == 0) \
return -1; \
} while (0)
CLOSE_OVERLAPPED (read_ovl);
@ -653,17 +657,19 @@ static ut8 gprobe_checksum (const ut8 *p, unsigned int size) {
return res;
}
static void gprobe_frame_sp (RBuffer *frame) {
ut8 size = r_buf_size (frame) + 2;
static void gprobe_frame_sp(RBuffer *frame) {
ut64 size;
const ut8 *tmp = r_buf_buffer (frame, &size);
size += 2;
ut8 checksum;
r_buf_prepend_bytes (frame, &size, 1);
checksum = gprobe_checksum (r_buf_buffer (frame), size - 1);
r_buf_prepend_bytes (frame, (const ut8 *)&size, 1);
checksum = gprobe_checksum (tmp, size - 1);
r_buf_append_bytes (frame, &checksum, 1);
}
static int gprobe_get_reply_sp (struct gport *port, ut8 cmd, RBuffer *reply) {
static int gprobe_get_reply_sp(struct gport *port, ut8 cmd, RBuffer *reply) {
ut8 buf[256];
int count = sp_blocking_read (port, buf, 2, 50);
@ -697,18 +703,19 @@ static int gprobe_get_reply_sp (struct gport *port, ut8 cmd, RBuffer *reply) {
return 0;
}
static int gprobe_send_request_sp (struct gport *port, RBuffer *request) {
static int gprobe_send_request_sp(struct gport *port, RBuffer *request) {
sp_flush (port);
if (sp_blocking_write (port, r_buf_buffer (request), r_buf_size (request),
100) != r_buf_size (request)) {
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (request, &tmpsz);
if (sp_blocking_write (port, tmp, tmpsz, 100) != tmpsz) {
return -1;
}
return 0;
}
static int gprobe_read (struct gport *port, ut32 addr, ut8 *buf, ut32 count) {
static int gprobe_read(struct gport *port, ut32 addr, ut8 *buf, ut32 count) {
RBuffer *request = r_buf_new ();
RBuffer *reply = r_buf_new ();
const ut8 cmd = GPROBE_RAM_READ_2;
@ -1001,7 +1008,9 @@ static int gprobe_getinformation (struct gport *port) {
goto fail;
}
r_print_hexdump (NULL, 0, r_buf_buffer (reply), r_buf_size (reply), 16, 1, 1);
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (reply, &tmpsz);
r_print_hexdump (NULL, 0, tmp, tmpsz, 16, 1, 1);
r_buf_free (request);
r_buf_free (reply);
@ -1014,7 +1023,7 @@ fail:
return -1;
}
static int __write (RIO *io, RIODesc *fd, const ut8 *buf, int count) {
static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
RIOGprobe *gprobe;
int res;
int has_written = 0;

View File

@ -196,8 +196,9 @@ int r_io_zip_flush_file(RIOZipFileObj *zfo) {
return res;
}
struct zip_source *s = zip_source_buffer (zipArch, r_buf_buffer (zfo->b),
r_buf_size (zfo->b), 0);
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (zfo->b, &tmpsz);
struct zip_source *s = zip_source_buffer (zipArch, tmp, tmpsz, 0);
if (s && zfo->entry != -1) {
if (zip_replace(zipArch, zfo->entry, s) == 0) {
res = true;

View File

@ -907,9 +907,11 @@ R_API int r_main_rabin2(int argc, char **argv) {
r_bin_arch_options_init (&opts, arch, bits);
b = r_bin_create (bin, create, code, codelen, data, datalen, &opts);
if (b) {
if (r_file_dump (file, r_buf_buffer (b), r_buf_size (b), 0)) {
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (b, &tmpsz);
if (r_file_dump (file, tmp, tmpsz, 0)) {
eprintf ("Dumped %" PFMT64d " bytes in '%s'\n",
r_buf_size (b), file);
tmpsz, file);
r_file_chmod (file, "+x", 0);
} else {
eprintf ("Error dumping into a.out\n");

View File

@ -72,8 +72,9 @@ static int create(const char *format, const char *arch, int bits, const ut8 *cod
r_bin_arch_options_init (&opts, arch, bits);
b = r_bin_create (bin, format, code, codelen, NULL, 0, &opts);
if (b) {
size_t blen = r_buf_size (b);
if (write (1, r_buf_buffer (b), blen) != blen) {
ut64 blen;
const ut8 *tmp = r_buf_buffer (b, &blen);
if (write (1, tmp, blen) != blen) {
eprintf ("Failed to write buffer\n");
}
r_buf_free (b);
@ -131,7 +132,7 @@ R_API int r_main_ragg2(int argc, char **argv) {
char *shellcode = NULL;
char *encoder = NULL;
char *sequence = NULL;
int bits = (R_SYS_BITS & R_SYS_BITS_64) ? 64 : 32;
int bits = (R_SYS_BITS & R_SYS_BITS_64)? 64: 32;
int fmt = 0;
const char *ofile = NULL;
int ofileauto = 0;
@ -525,8 +526,9 @@ R_API int r_main_ragg2(int argc, char **argv) {
}
b = r_egg_get_bin (egg);
if (show_raw) {
size_t blen = r_buf_size (b);
if (write (1, r_buf_buffer (b), blen) != blen) {
ut64 blen;
const ut8 *tmp = r_buf_buffer (b, &blen);
if (write (1, tmp, blen) != blen) {
eprintf ("Failed to write buffer\n");
goto fail;
}
@ -536,39 +538,38 @@ R_API int r_main_ragg2(int argc, char **argv) {
goto fail;
}
RPrint *p = r_print_new ();
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (b, &tmpsz);
switch (*format) {
case 'c':
r_print_code (p, 0, (ut8 *)r_buf_buffer (b), r_buf_size (b), 'c');
r_print_code (p, 0, tmp, tmpsz, 'c');
break;
case 'j': // JavaScript
r_print_code (p, 0, (ut8 *)r_buf_buffer (b), r_buf_size (b), 'j');
r_print_code (p, 0, tmp, tmpsz, 'j');
break;
case 'r':
if (show_str) {
printf ("\"");
r_buf_seek (b, 0, 0);
for (i = 0; i < r_buf_size (b); i++) {
printf ("\\x%02x", r_buf_read8 (b));
for (i = 0; i < tmpsz; i++) {
printf ("\\x%02x", tmp[i]);
}
printf ("\"\n");
} else if (show_hex) {
r_buf_seek (b, 0, 0);
for (i = 0; i < r_buf_size (b); i++) {
printf ("%02x", r_buf_read8 (b));
for (i = 0; i < tmpsz; i++) {
printf ("%02x", tmp[i]);
}
printf ("\n");
} // else show_raw is_above()
break;
case 'p': // PE
if (strlen(format) >= 2 && format[1] == 'y') { // Python
r_print_code (p, 0, (ut8 *)r_buf_buffer (b),
r_buf_size (b), 'p');
r_print_code (p, 0, tmp, tmpsz, 'p');
}
break;
case 'e': // ELF
case 'm': // MACH0
create (format, arch, bits, r_buf_buffer (b),
r_buf_size (b));
create (format, arch, bits, tmp, tmpsz);
break;
default:
eprintf ("unknown executable format (%s)\n", format);

View File

@ -4,6 +4,37 @@
#include <r_util.h>
#include <r_io.h>
static ut8 *r_buf_get_at(RBuffer *b, ut64 addr, int *left) {
if (b->empty_priv) {
return NULL;
}
if (b->iob) {
if (b->fd_priv != -1) {
eprintf ("r_buf_get_at not supported for r_buf_new_file\n");
return NULL;
}
static ut8 buf[8];
r_buf_read_at (b, addr, buf, sizeof (buf));
if (left) {
*left = 8;
}
return buf;
}
if (addr == R_BUF_CUR) {
addr = b->cur_priv;
} else {
addr = addr - b->base_priv + b->offset_priv;
}
ut64 effective_size = r_buf_size (b);
if (addr == UT64_MAX || addr - b->offset_priv > effective_size) {
return NULL;
}
if (left) {
*left = effective_size - addr + b->offset_priv;
}
return b->buf_priv + addr;
}
// TODO: Optimize to use memcpy when buffers are not in range..
// check buf boundaries and offsets and use memcpy or memmove
@ -415,8 +446,11 @@ R_API RBuffer *r_buf_new() {
return b;
}
R_API const ut8 *r_buf_buffer(RBuffer *b) {
R_API const ut8 *r_buf_buffer(RBuffer *b, ut64 *size) {
if (b && !b->sparse_priv && b->fd_priv == -1 && !b->mmap_priv) {
if (size) {
*size = r_buf_size (b);
}
return b->buf_priv;
}
r_return_val_if_fail (false, NULL);
@ -498,7 +532,9 @@ R_API bool r_buf_dump(RBuffer *b, const char *file) {
if (!b || !file) {
return false;
}
return r_file_dump (file, r_buf_get_at (b, 0, NULL), r_buf_size (b), 0);
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (b, &tmpsz);
return r_file_dump (file, tmp, tmpsz, 0);
}
R_API ut64 r_buf_tell(RBuffer *b) {
@ -739,35 +775,37 @@ R_API bool r_buf_append_buf_slice(RBuffer *b, RBuffer *a, ut64 offset, ut64 size
return false;
}
R_API ut8 *r_buf_get_at(RBuffer *b, ut64 addr, int *left) {
if (b->empty_priv) {
// return an heap-allocated string read from the RBuffer b at address addr. The
// length depends on the first '\0' found in the buffer.
R_API char *r_buf_get_string(RBuffer *b, ut64 addr) {
const ut8 *needle = NULL;
ut8 tmp[16];
ut64 sz = 0;
int r = r_buf_read_at (b, addr + sz, tmp, sizeof (tmp));
while (r >= 0) {
needle = r_mem_mem (tmp, r, (ut8 *)"\x00", 1);
if (needle) {
sz += (needle - tmp);
break;
}
sz += r;
r = r_buf_read_at (b, addr + sz, tmp, sizeof (tmp));
}
if (r < 0) {
return NULL;
}
if (b->iob) {
if (b->fd_priv != -1) {
eprintf ("r_buf_get_at not supported for r_buf_new_file\n");
return NULL;
}
static ut8 buf[8];
r_buf_read_at (b, addr, buf, sizeof (buf));
if (left) {
*left = 8;
}
return buf;
}
if (addr == R_BUF_CUR) {
addr = b->cur_priv;
} else {
addr = addr - b->base_priv + b->offset_priv;
}
ut64 effective_size = r_buf_size (b);
if (addr == UT64_MAX || addr - b->offset_priv > effective_size) {
char *res = R_NEWS (char, sz + 1);
if (!res) {
return NULL;
}
if (left) {
*left = effective_size - addr + b->offset_priv;
r = r_buf_read_at (b, addr + 20, (ut8 *)res, sz);
if (r < 0) {
free (res);
return NULL;
}
return b->buf_priv + addr;
res[sz] = '\0';
return res;
}
R_API ut8 r_buf_read8_at(RBuffer *b, ut64 addr) {
@ -1005,3 +1043,47 @@ R_API RList *r_buf_nonempty_list(RBuffer *b) {
}
return r_list_clone (b->sparse_priv);
}
R_API int r_buf_uleb128(RBuffer *b, ut64 *v) {
ut8 c = 0xff;
ut64 s = 0, sum = 0, l = 0;
do {
ut8 data;
int r = r_buf_read (b, &data, sizeof (data));
if (r <= 0) {
return -1;
}
c = data & 0xff;
sum |= ((ut64) (c & 0x7f) << s);
s += 7;
l++;
} while (c & 0x80);
if (v) {
*v = sum;
}
return l;
}
R_API int r_buf_sleb128(RBuffer *b, st64 *v) {
st64 result = 0;
int offset = 0;
ut8 value;
do {
st64 chunk;
int r = r_buf_read (b, &value, sizeof (value));
if (r != sizeof (value)) {
return -1;
}
chunk = value & 0x7f;
result |= (chunk << offset);
offset += 7;
} while (value & 0x80);
if ((value & 0x40) != 0) {
result |= ~0UL << offset;
}
if (v) {
*v = result;
}
return offset / 7;
}

View File

@ -23,10 +23,10 @@ R_API const ut8 *r_uleb128(const ut8 *data, int datalen, ut64 *v) {
if (*data) {
for (s = 0; data < data_end; s += 7) {
c = *(data++) & 0xff;
if (s > 31) {
if (s > 63) {
eprintf ("r_uleb128: undefined behaviour in %d shift on ut32\n", (int)s);
} else {
sum |= ((ut32) (c & 0x7f) << s);
sum |= ((ut64) (c & 0x7f) << s);
}
if (!(c & 0x80)) {
break;
@ -51,7 +51,7 @@ R_API const ut8 *r_uleb128_decode(const ut8 *data, int *datalen, ut64 *v) {
ut64 s = 0, sum = 0, l = 0;
do {
c = *(data++) & 0xff;
sum |= ((ut32) (c&0x7f) << s);
sum |= ((ut64) (c&0x7f) << s);
s += 7;
l++;
} while (c & 0x80);
@ -64,7 +64,7 @@ R_API const ut8 *r_uleb128_decode(const ut8 *data, int *datalen, ut64 *v) {
return data;
}
R_API const ut8 *r_uleb128_encode(const ut64 s, int *len) {
R_API ut8 *r_uleb128_encode(const ut64 s, int *len) {
ut8 c = 0;
int l = 0;
ut8 *otarget = NULL, *target = NULL, *tmptarget = NULL;
@ -127,8 +127,7 @@ R_API st64 r_sleb128(const ut8 **data, const ut8 *end) {
chunk = value & 0x7f;
result |= (chunk << offset);
offset += 7;
}
while (cond = *p & 0x80 && p + 1 < end, p++, cond);
} while (cond = *p & 0x80 && p + 1 < end, p++, cond);
if ((value & 0x40) != 0) {
result |= ~0UL << offset;

View File

@ -3110,7 +3110,9 @@ R_API RBinJavaObj *r_bin_java_new_buf(RBuffer *buf, ut64 loadaddr, Sdb *kv) {
if (!bin) {
return NULL;
}
if (!r_bin_java_new_bin (bin, loadaddr, kv, r_buf_buffer (buf), r_buf_size (buf))) {
ut64 tmpsz;
const ut8 *tmp = r_buf_buffer (buf, &tmpsz);
if (!r_bin_java_new_bin (bin, loadaddr, kv, tmp, tmpsz)) {
return r_bin_java_free (bin);
}
return bin;