static int __core_patch_bracket(RCore *core, const char *str, ut64 *noff) { char tmp[128]; char *s, *p, *q, *off; RBuffer *b = r_buf_new (); if (!b) { return 0; } p = off = strdup (str); if (!p) { r_buf_free (b); return 0; } for (;*p;) { if (*p=='\n') { *p++ = 0; } else { p++; continue; } if (*str == '}') break; if ((q = strstr (str, "${"))) { char *end = strchr (q+2,'}'); if (end) { *q = *end = 0; *noff = r_num_math (core->num, q+2); r_buf_append_bytes (b, (const ut8*)str, strlen (str)); snprintf (tmp, sizeof (tmp), "0x%08"PFMT64x, *noff); r_buf_append_bytes (b, (const ut8*)tmp, strlen (tmp)); r_buf_append_bytes (b, (const ut8*)end+1, strlen (end+1)); } } else { r_buf_append_bytes (b, (const ut8*)str, strlen (str)); } str = p; } s = r_buf_to_string (b); r_egg_load (core->egg, s, 0); free (s); r_egg_compile (core->egg); r_egg_assemble (core->egg); r_buf_free (b); b = r_egg_get_bin (core->egg); if (strcmp (off, "+")) { *noff = r_num_math (core->num, off); } r_core_write_at (core, *noff, b->buf, b->length); *noff += b->length; free (off); return 1; }
static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) { if (__plugin_open (io, pathname,0)) { RIOSparse *mal = R_NEW0 (RIOSparse); mal->fd = -2; /* causes r_io_desc_new() to set the correct fd */ int size = (int)r_num_math (NULL, pathname+9); mal->buf = r_buf_new_sparse (); if (size>0) { ut8 *data = malloc (size); if (!data) { eprintf ("Cannot allocate (%s) %d bytes\n", pathname+9, size); mal->offset = 0; } else { memset (data, 0x00, size); r_buf_write_at (mal->buf, 0, data, size); free (data); } } if (mal->buf) { RETURN_IO_DESC_NEW (&r_io_plugin_sparse, mal->fd, pathname, rw, mode, mal); } r_buf_free (mal->buf); free (mal); } return NULL; }
static void r_io_def_mmap_free (RIOMMapFileObj *mmo) { free (mmo->filename); r_buf_free (mmo->buf); close (mmo->fd); memset (mmo, 0, sizeof (RIOMMapFileObj)); free (mmo); }
static int destroy(RBinFile *arch) { free(arch->o->bin_obj); r_buf_free (arch->buf); arch->buf = NULL; arch->o->bin_obj = NULL; return true; }
static int r_io_def_mmap_refresh_def_mmap_buf(RIOMMapFileObj *mmo) { RIO* io = mmo->io_backref; ut64 cur; if (mmo->buf) { cur = mmo->buf->cur; r_buf_free (mmo->buf); mmo->buf = NULL; } else { cur = 0; } st64 sz = r_file_size (mmo->filename); if (sz == 0 || sz > ST32_MAX) { // Do not use mmap if the file is huge mmo->rawio = 1; } if (mmo->rawio) { mmo->fd = __io_posix_open (mmo->filename, mmo->flags, mmo->mode); return (mmo->fd != -1); } mmo->buf = r_buf_mmap (mmo->filename, mmo->flags); if (mmo->buf) { r_io_def_mmap_seek (io, mmo, cur, SEEK_SET); return true; } else { mmo->rawio = 1; mmo->fd = __io_posix_open (mmo->filename, mmo->flags, mmo->mode); return (mmo->fd != -1); } return false; }
static int destroy(RBinFile *bf) { free (bf->o->bin_obj); r_buf_free (bf->buf); bf->buf = NULL; bf->o->bin_obj = NULL; return true; }
static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) { if (__plugin_open (io, pathname,0)) { RIOSparse *mal = R_NEW0 (RIOSparse); int size = (int)r_num_math (NULL, pathname + 9); mal->buf = r_buf_new_sparse (io->Oxff); if (!mal->buf) { free (mal); return NULL; } if (size > 0) { ut8 *data = malloc (size); if (!data) { eprintf ("Cannot allocate (%s) %d byte(s)\n", pathname+9, size); mal->offset = 0; } else { memset (data, 0x00, size); r_buf_write_at (mal->buf, 0, data, size); free (data); } } if (mal->buf) { return r_io_desc_new (io, &r_io_plugin_sparse, pathname, rw, mode, mal); } r_buf_free (mal->buf); free (mal); } return NULL; }
void* r_bin_dyldcache_free(struct r_bin_dyldcache_obj_t* bin) { if (!bin) return NULL; if (bin->b) r_buf_free (bin->b); free(bin); return NULL; }
void* r_bin_te_free(struct r_bin_te_obj_t* bin) { if (!bin) return NULL; free (bin->header); free (bin->section_header); r_buf_free (bin->b); free (bin); return NULL; }
R_API RBuffer *r_buf_file (const char *file) { RBuffer *b = r_buf_new (); if (!b) return NULL; b->buf = (ut8*)r_file_slurp (file, &b->length); if (b->buf) return b; r_buf_free (b); return NULL; /* we just freed b, don't return it */ }
void r_io_zip_free_zipfileobj(RIOZipFileObj *zfo) { if (!zfo) return; if (zfo->modified) r_io_zip_flush_file (zfo); free (zfo->name); free (zfo->password); r_buf_free (zfo->b); free (zfo); }
void* r_bin_java_free(RBinJavaObj* bin) { if (!bin) return NULL; if (bin->cp_items) free (bin->cp_items); if (bin->fields) free (bin->fields); if (bin->methods) free (bin->methods); if (bin->b) r_buf_free (bin->b); free (bin); return NULL; }
void *r_bin_mz_free(struct r_bin_mz_obj_t* bin) { if (!bin) return NULL; free ((void *)bin->dos_header); free ((void *)bin->dos_extended_header); free ((void *)bin->relocation_entries); r_buf_free (bin->b); bin->b = NULL; free (bin); return NULL; }
static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) { if (!buf || !sz || sz == UT64_MAX) { return NULL; } RBuffer *tbuf = r_buf_new(); r_buf_set_bytes (tbuf, buf, sz); void *res = r_bin_coff_new_buf (tbuf, arch->rbin->verbose); r_buf_free (tbuf); return res; }
static void * load_bytes(const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){ void *res = NULL; RBuffer *tbuf = NULL; if (!buf || sz == 0 || sz == UT64_MAX) return NULL; tbuf = r_buf_new (); r_buf_set_bytes (tbuf, buf, sz); res = r_bin_dex_new_buf (tbuf); r_buf_free (tbuf); return res; }
static int r_io_def_mmap_refresh_def_mmap_buf(RIOMMapFileObj *mmo) { RIO* io = mmo->io_backref; ut64 cur = mmo->buf ? mmo->buf->cur : 0; if (mmo->buf) { r_buf_free (mmo->buf); mmo->buf = NULL; } mmo->buf = r_buf_mmap (mmo->filename, mmo->flags); if (mmo->buf) r_io_def_mmap_seek (io, mmo, cur, SEEK_SET); return (mmo->buf ? R_TRUE : R_FALSE); }
R_API RBuffer *r_buf_mmap (const char *file, int rw) { RBuffer *b = r_buf_new (); b->mmap = r_file_mmap (file, rw); if (b->mmap && b->mmap->len>0) { b->buf = b->mmap->buf; b->length = b->mmap->len; } else { r_buf_free (b); return NULL; /* we just freed b, don't return it */ } return b; }
// TODO: rename to new_from_file ? R_API RBuffer *r_buf_new_slurp(const char *file) { int len; RBuffer *b = r_buf_new (); if (!b) return NULL; b->buf = (ut8*)r_file_slurp (file, &len); b->length = len; if (b->buf) { return b; } r_buf_free (b); return NULL; /* we just freed b, don't return it */ }
static void * load_bytes(const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){ void *res = NULL; RBuffer *tbuf = NULL; struct r_bin_java_obj_t* bin_obj = NULL; if (!buf || sz == 0 || sz == UT64_MAX) return NULL; tbuf = r_buf_new(); r_buf_set_bytes (tbuf, buf, sz); res = bin_obj = r_bin_java_new_buf (tbuf, loadaddr, sdb); add_bin_obj_to_sdb (bin_obj); r_buf_free (tbuf); return res; }
static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) { const struct r_bin_mz_obj_t *res = NULL; RBuffer *tbuf = NULL; if (!buf || sz == 0 || sz == UT64_MAX) return NULL; tbuf = r_buf_new (); r_buf_set_bytes (tbuf, buf, sz); res = r_bin_mz_new_buf (tbuf); if (res) sdb_ns_set (sdb, "info", res->kv); r_buf_free (tbuf); return (void *)res; }
static int r_bin_coff_init(struct r_bin_coff_obj *obj, RBuffer *buf) { obj->b = r_buf_new (); obj->size = buf->length; if (!r_buf_set_bytes (obj->b, buf->buf, obj->size)){ r_buf_free (obj->b); return false; } r_bin_coff_init_hdr(obj); r_bin_coff_init_opt_hdr(obj); r_bin_coff_init_scn_hdr(obj); r_bin_coff_init_symtable(obj); return true; }
R_API RBuffer *r_buf_mmap (const char *file, int flags) { int rw = flags&R_IO_WRITE ? R_TRUE : R_FALSE; RBuffer *b = r_buf_new (); if (!b) return NULL; b->mmap = r_file_mmap (file, rw, 0); if (b->mmap) { b->buf = b->mmap->buf; b->length = b->mmap->len; if (b->length == 0) b->empty = 1; return b; } r_buf_free (b); return NULL; /* we just freed b, don't return it */ }
static RBinXtrData * extract(RBin* bin, int idx) { int narch; RBinXtrData * res = NULL; struct r_bin_fatmach0_obj_t *fb = bin->cur->xtr_obj; struct r_bin_fatmach0_arch_t *arch; arch = r_bin_fatmach0_extract (fb, idx, &narch); if (!arch) return res; res = r_bin_xtrdata_new (NULL, NULL, arch->b, arch->offset, arch->size, narch); r_buf_free (arch->b); free (arch); return res; }
void r_bin_mdmp_free(struct r_bin_mdmp_obj *obj) { if (!obj) return; r_bin_mdmp_destroy_lists(obj); obj->system_info = NULL; obj->hdr = NULL; if (obj->kv) { sdb_free (obj->kv); obj->kv = NULL; } if (obj->b) { r_buf_free (obj->b); obj->b = NULL; } free (obj); }
static void *load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){ struct r_bin_java_obj_t *bin_obj = NULL; RBuffer *tbuf = NULL; void *res = NULL; if (!buf || sz == 0 || sz == UT64_MAX) { return NULL; } tbuf = r_buf_new (); r_buf_set_bytes (tbuf, buf, sz); res = bin_obj = r_bin_java_new_buf (tbuf, loadaddr, sdb); add_bin_obj_to_sdb (bin_obj); if (bf && bf->file) { bin_obj->file = strdup (bf->file); } r_buf_free (tbuf); return res; }
void r_io_zip_free_zipfileobj(RIOZipFileObj *zipFileObj) { if (zipFileObj) { if (zipFileObj->modified) { r_io_zip_flush_file(zipFileObj); } if (zipFileObj->name) { free(zipFileObj->name); } if (zipFileObj->password) { free(zipFileObj->password); } if (zipFileObj->b) { r_buf_free(zipFileObj->b); } free(zipFileObj); } }
R_API char *r_buf_free_to_string(RBuffer *b) { char *p; if (!b) { return NULL; } if (b->mmap) { p = r_buf_to_string (b); } else { r_buf_append_bytes (b, (const ut8*)"", 1); p = malloc (b->length + 1); if (!p) { return NULL; } memmove (p, b->buf, b->length); p[b->length] = 0; } r_buf_free (b); return p; }
static RBinXtrData * oneshot(RBin *bin, const ut8 *buf, ut64 size, int idx) { void *xtr_obj = r_bin_fatmach0_from_bytes_new (buf, size); struct r_bin_fatmach0_obj_t *fb = xtr_obj; struct r_bin_fatmach0_arch_t *arch; RBinXtrData *res = NULL; int narch; arch = r_bin_fatmach0_extract (fb, idx, &narch); if (!arch) { free_xtr (xtr_obj); return res; } res = r_bin_xtrdata_new (xtr_obj, free_xtr, arch->b, arch->offset, arch->size, narch); r_buf_free (arch->b); free (arch); return res; }
struct r_bin_dex_obj_t* r_bin_dex_new_buf(RBuffer *buf) { struct r_bin_dex_obj_t *bin = R_NEW0 (struct r_bin_dex_obj_t);; if (!bin) return NULL; bin->size = buf->length; bin->b = r_buf_new (); if (!r_buf_set_bytes (bin->b, buf->buf, bin->size)){ r_buf_free (bin->b); free (bin); return NULL; } // XXX: use r_buf_getc() // XXX: this is not endian safe /* header */ r_buf_read_at (bin->b, 0, (ut8*)&bin->header, sizeof (struct dex_header_t)); /* strings */ bin->strings = (ut32 *) malloc (bin->header.strings_size * sizeof (ut32) + 1); r_buf_read_at (bin->b, bin->header.strings_offset, (ut8*)bin->strings, bin->header.strings_size * sizeof (ut32)); /* classes */ bin->classes = (struct dex_class_t *) malloc (bin->header.class_size * sizeof (struct dex_class_t) + 1); r_buf_read_at (bin->b, bin->header.class_offset, (ut8*)bin->classes, bin->header.class_size * sizeof (struct dex_class_t)); //{ ut8 *b = (ut8*)&bin->methods; eprintf ("CLASS %02x %02x %02x %02x\n", b[0], b[1], b[2], b[3]); } /* methods */ bin->methods = (struct dex_method_t *) calloc (bin->header.method_size * sizeof (struct dex_method_t) + 1, 1); r_buf_read_at (bin->b, bin->header.method_offset, (ut8*)bin->methods, bin->header.method_size * sizeof (struct dex_method_t)); /* types */ bin->types = (struct dex_type_t *) calloc (bin->header.types_size * sizeof (struct dex_type_t) + 1, 1); r_buf_read_at (bin->b, bin->header.types_offset, (ut8*)bin->types, bin->header.types_size * sizeof (struct dex_type_t)); /* fields */ bin->fields = (struct dex_field_t *) calloc (bin->header.fields_size * sizeof (struct dex_field_t) + 1, 1); r_buf_read_at (bin->b, bin->header.fields_offset, (ut8*)bin->fields, bin->header.fields_size * sizeof (struct dex_field_t)); return bin; }
struct r_bin_zimg_obj_t* r_bin_zimg_new_buf(RBuffer *buf) { struct r_bin_zimg_obj_t *bin = R_NEW0 (struct r_bin_zimg_obj_t); if (!bin) { goto fail; } bin->size = r_buf_size (buf); bin->b = r_buf_ref (buf); if (r_buf_size (bin->b) < sizeof (struct zimg_header_t)) { goto fail; } r_buf_read_at (bin->b, 0, (ut8 *)&bin->header, sizeof (bin->header)); return bin; fail: if (bin) { r_buf_free (bin->b); free (bin); } return NULL; }