R_API RAnalState * r_anal_state_new(ut64 start, ut8* buffer, ut64 len) { RAnalState *state = R_NEW0 (RAnalState); if (!state) { return NULL; } state->start = start; state->end = start + len; state->buffer = buffer; state->len = len; state->current_op = NULL; state->current_bb = NULL; state->current_fcn = NULL; state->ht = r_hashtable64_new(); state->ht->free = (RHashFree)r_anal_bb_free; state->ht_sz = 512; state->bbs = r_list_newf ((RListFree)r_anal_bb_free); state->max_depth = 50; state->current_depth = 0; return state; }
R_API RThread *r_th_new(R_TH_FUNCTION(fun), void *user, int delay) { RThread *th = R_NEW0 (RThread); if (th) { th->lock = r_th_lock_new (false); th->running = false; th->fun = fun; th->user = user; th->delay = delay; th->breaked = false; th->ready = false; #if HAVE_PTHREAD pthread_cond_init (&th->_cond, NULL); pthread_mutex_init (&th->_mutex, NULL); pthread_create (&th->tid, NULL, _r_th_launcher, th); #elif __WINDOWS__ && !defined(__CYGWIN__) th->tid = CreateThread (NULL, 0, _r_th_launcher, th, 0, 0); #endif } return th; }
static RList *sections(RBinFile *bf) { RList /*<RBinSection>*/ *ret = r_list_new (); if (!ret) { return NULL; } RBinSection *text = R_NEW0 (RBinSection); if (!text) { r_list_free (ret); return NULL; } strncpy (text->name, "text", R_BIN_SIZEOF_STRINGS); text->size = bf->buf->length - N64_ROM_START; text->vsize = text->size; text->paddr = N64_ROM_START; text->vaddr = baddr (bf); text->perm = R_PERM_RX; text->add = true; r_list_append (ret, text); return ret; }
static RBinInfo* info(RBinFile *arch) { RBinInfo *ret = NULL; ines_hdr ihdr; memset (&ihdr, 0, INES_HDR_SIZE); int reat = r_buf_read_at (arch->buf, 0, (ut8*)&ihdr, INES_HDR_SIZE); if (reat != INES_HDR_SIZE) { eprintf ("Truncated Header\n"); return NULL; } if (!(ret = R_NEW0 (RBinInfo))) return NULL; ret->file = strdup (arch->file); ret->type = strdup ("ROM"); ret->machine = strdup ("Nintendo NES"); ret->os = strdup ("nes"); ret->arch = strdup ("6502"); ret->bits = 8; ret->has_va = 1; return ret; }
static RList *entries(RBinFile *bf) { BootImageObj *bio = bf->o->bin_obj; RBinAddr *ptr = NULL; if (!bio) { return NULL; } BootImage *bi = &bio->bi; RList *ret; if (!(ret = r_list_newf (free))) { return NULL; } if (!(ptr = R_NEW0 (RBinAddr))) { return ret; } ptr->paddr = bi->page_size; ptr->vaddr = bi->kernel_addr; r_list_append (ret, ptr); return ret; }
static RList* sections(RBinFile *arch) { RList *ret = NULL; RBinSection *ptr = NULL; if (!(ret = r_list_new ())) return NULL; ret->free = free; // program headers is another section if (!(ptr = R_NEW0 (RBinSection))) return ret; strcpy (ptr->name, "bootblk"); ptr->vsize = ptr->size = 0x10000; //printf ("SIZE %d\n", ptr->size); ptr->paddr = arch->buf->length - ptr->size; ptr->vaddr = 0xf0000; ptr->srwx = R_BIN_SCN_READABLE | R_BIN_SCN_WRITABLE | R_BIN_SCN_EXECUTABLE | R_BIN_SCN_MAP; r_list_append (ret, ptr); return ret; }
static RBinInfo *info(RBinFile *arch) { RBinInfo *ret; if(!(ret = R_NEW0 (RBinInfo))) return NULL; ret->file = strdup (arch->file); ret->bclass = strdup ("OMF"); ret->rclass = strdup ("omf"); // the "E" is here to made rva return the same value for 16 bit en 32 bits files ret->type = strdup ("E OMF (Relocatable Object Module Format)"); ret->os = strdup ("any"); ret->machine = strdup ("i386"); ret->arch = strdup ("x86"); ret->big_endian = false; ret->has_va = true; ret->bits = r_bin_omf_get_bits (arch->o->bin_obj); ret->dbg_info = 0; ret->has_nx = false; return ret; }
static RList *sections(RBinFile *arch) { RList *ret = NULL; RBinSection *s = R_NEW0 (RBinSection); ut64 sz = r_buf_size (arch->buf); if (!(ret = r_list_new ())) { free (s); return NULL; } strcpy (s->name, "ROM"); s->paddr = 0; s->vaddr = 0x8000000; s->size = sz; s->vsize = 0x2000000; s->srwx = R_BIN_SCN_READABLE | R_BIN_SCN_EXECUTABLE | R_BIN_SCN_MAP; s->add = true; r_list_append (ret, s); return ret; }
static RList *entries(RBinFile *bf) { const r_bin_xbe_obj_t *obj; RList *ret; RBinAddr *ptr = R_NEW0 (RBinAddr); if (!bf || !bf->buf || !bf->o->bin_obj || !ptr) { free (ptr); return NULL; } ret = r_list_new (); if (!ret) { free (ptr); return NULL; } ret->free = free; obj = bf->o->bin_obj; ptr->vaddr = obj->header->ep ^ obj->ep_key; ptr->paddr = ptr->vaddr - obj->header->base; r_list_append (ret, ptr); return ret; }
static RBinInfo* info(RBinFile *arch) { RBinInfo *ret = NULL; bool big_endian = 0; if ((ret = R_NEW0 (RBinInfo)) == NULL) return NULL; ret->file = strdup (arch->file); ret->bclass = strdup ("dyldcache"); ret->rclass = strdup ("ios"); ret->os = strdup ("iOS"); ret->arch = strdup ("arm"); ret->machine = strdup (ret->arch); ret->subsystem = strdup ("xnu"); ret->type = strdup ("LIBRARY CACHE"); ret->bits = dyld64? 64: 32; ret->has_va = true; ret->big_endian = big_endian; ret->dbg_info = 0; ret->dbg_info = 0; return ret; }
static RBinInfo* info(RBinFile *arch) { RBinInfo *ret = NULL; _3DSX_hdr _3dsxhdr; memset (&_3dsxhdr, 0, _3DSX_HDR_SIZE); int reat = r_buf_read_at (arch->buf, 0, (ut8*)&_3dsxhdr, _3DSX_HDR_SIZE); if (reat != _3DSX_HDR_SIZE) { eprintf ("Truncated Header\n"); return NULL; } if (!(ret = R_NEW0 (RBinInfo))) return NULL; ret->file = strdup (arch->file); ret->type = strdup ("Sound File Data"); ret->machine = strdup ("3DSX"); ret->os = strdup ("3DS"); ret->arch = strdup ("arm"); ret->bits = 32; ret->has_va = 1; return ret; }
/* Try to get a valid entrypoint using the methods outlined in * http://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_mono/ld.html#SEC24 */ RBinAddr *r_coff_get_entry(struct r_bin_coff_obj *obj) { RBinAddr *addr = R_NEW0 (RBinAddr); int i; if (!addr) return -1; /* Simplest case, the header provides the entrypoint address */ if (obj->hdr.f_opthdr) { addr->paddr = obj->opt_hdr.entry; return addr; } /* No help from the header eh? Use the address of the symbols '_start' * or 'main' if present */ if (obj->symbols) for (i = 0; i < obj->hdr.f_nsyms; i++) { if ((!strcmp (obj->symbols[i].n_name, "_start") || !strcmp (obj->symbols[i].n_name, "start")) && r_coff_rebase_sym (obj, addr, &obj->symbols[i])) return addr; } if (obj->symbols) for (i = 0; i < obj->hdr.f_nsyms; i++) { if ((!strcmp (obj->symbols[i].n_name, "_main") || !strcmp (obj->symbols[i].n_name, "main")) && r_coff_rebase_sym (obj, addr, &obj->symbols[i])) return addr; } /* Still clueless ? Let's just use the address of .text */ if (obj->scn_hdrs) for (i = 0; i < obj->hdr.f_nscns; i++) { if (!strcmp (obj->scn_hdrs[i].s_name, ".text")) { addr->paddr = obj->scn_hdrs[i].s_scnptr; return addr; } } return addr; }
R_API R2Pipe *r2p_open(const char *cmd) { R2Pipe *r2p = R_NEW0 (R2Pipe); r2p->magic = R2P_MAGIC; if (!cmd) { r2p->child = -1; return r2p_open_spawn (r2p, cmd); } #if __WINDOWS__ && !defined(__CYGWIN__) w32_createPipe (r2p, cmd); r2p->child = (int)(r2p->pipe); #else pipe (r2p->input); pipe (r2p->output); r2p->child = fork (); if (r2p->child == -1) { r2p_close (r2p); return NULL; } env ("R2PIPE_IN", r2p->input[0]); env ("R2PIPE_OUT", r2p->output[1]); if (r2p->child) { eprintf ("Child is %d\n", r2p->child); char ch; read (r2p->output[0], &ch, 1); } else { int rc = 0; if (cmd && *cmd) { close (0); close (1); dup2 (r2p->input[0], 0); dup2 (r2p->output[1], 1); rc = r_sandbox_system (cmd, 0); } r2p_close (r2p); exit (rc); return NULL; } #endif return r2p; }
static RIODesc *__open(RIO *io, const char *file, int rw, int mode) { if (__plugin_open (io, file, 0)) { char *pidpath; RIODesc *ret; RIOW32Dbg *dbg = R_NEW0 (RIOW32Dbg); if (!dbg) { return NULL; } dbg->pid = atoi (file + 9); if (__open_proc (dbg, !strncmp (file, "attach://", 9)) == -1) { free (dbg); return NULL; } pidpath = r_sys_pid_to_path (dbg->pid); ret = r_io_desc_new (io, &r_io_plugin_w32dbg, file, rw | R_PERM_X, mode, dbg); ret->name = pidpath; return ret; } return NULL; }
RIOMMapFileObj *r_io_def_mmap_create_new_file(RIO *io, const char *filename, int mode, int flags) { RIOMMapFileObj *mmo = NULL; if (!io) return NULL; mmo = R_NEW0 (RIOMMapFileObj); if (!mmo) return NULL; mmo->filename = strdup (filename); mmo->fd = r_num_rand (0xFFFF); // XXX: Use r_io_fd api mmo->mode = mode; mmo->flags = flags; mmo->io_backref = io; if (!r_io_def_mmap_refresh_def_mmap_buf (mmo)) { r_io_def_mmap_free (mmo); mmo = NULL; } return mmo; }
static RBinInfo* info(RBinFile* bf) { RBinInfo* ret = NULL; psxexe_header psxheader; if (r_buf_read_at (bf->buf, 0, (ut8*)&psxheader, sizeof(psxexe_header)) < sizeof(psxexe_header)) { eprintf ("Truncated Header\n"); return NULL; } if (!(ret = R_NEW0 (RBinInfo))) return NULL; ret->file = strdup (bf->file); ret->type = strdup ("Sony PlayStation 1 Executable"); ret->machine = strdup ("Sony PlayStation 1"); ret->os = strdup ("psx"); ret->arch = strdup ("mips"); ret->bits = 32; ret->has_va = true; return ret; }
static RList *entries(RBinFile *bf) { RBinWasmObj *bin = bf && bf->o ? bf->o->bin_obj : NULL; // TODO RList *ret; RBinAddr *ptr = NULL; ut64 addr = 0x0; if (!(ret = r_list_newf ((RListFree)free))) { return NULL; } if (!(addr = (ut64) r_bin_wasm_get_entrypoint (bin))) { r_list_free (ret); return NULL; } if ((ptr = R_NEW0 (RBinAddr))) { ptr->paddr = addr; ptr->vaddr = addr; r_list_append (ret, ptr); } return ret; }
static RList *r_debug_wind_pids (int pid) { RList *ret, *pids; RListIter *it; WindProc *p; ret = r_list_newf (free); if (!ret) return NULL; pids = wind_list_process(wctx); if (!pids) return ret; r_list_foreach(pids, it, p) { RDebugPid *newpid = R_NEW0 (RDebugPid); newpid->path = strdup (p->name); newpid->pid = p->uniqueid; newpid->status = 's'; newpid->runnable = R_TRUE; r_list_append (ret, newpid); }
static RBinInfo *info(RBinFile *arch) { RBinInfo *ret = NULL; if (!(ret = R_NEW0 (RBinInfo))) { return NULL; } ret->file = strdup (arch->file); ret->bclass = strdup ("module"); ret->rclass = strdup ("wasm"); ret->os = strdup ("Wasm"); ret->arch = strdup ("wasm"); ret->machine = strdup (ret->arch); ret->subsystem = strdup ("wasm"); ret->type = strdup ("EXEC"); ret->bits = 32; ret->has_va = true; ret->big_endian = false; ret->dbg_info = 0; ret->dbg_info = 0; return ret; }
static RBinInfo *info(RBinFile *bf) { RBinInfo *ret = R_NEW0 (RBinInfo); if (!ret) { return NULL; } if (!bf || !bf->buf) { free (ret); return NULL; } char *filepath = r_str_newf ("%.12s - %.4s", loaded_header.title, loaded_header.gamecode); ret->file = filepath; ret->type = strdup ("ROM"); ret->machine = strdup ("Nintendo DS"); ret->os = strdup ("nds"); ret->arch = strdup ("arm"); ret->has_va = true; ret->bits = 32; return ret; }
static RBinInfo* info(RBinFile *arch) { RBinInfo *ret = R_NEW0 (RBinInfo); if (!ret) return NULL; ret->lang = ""; ret->file = arch->file? strdup (arch->file): NULL; ret->type = get_filetype (arch); ret->has_pi = 0; ret->has_canary = 0; if (R_SYS_BITS & R_SYS_BITS_64) { ret->bits = 64; } else { ret->bits = 32; } ret->big_endian = 0; ret->has_va = 0; ret->has_nx = 0; ret->dbg_info = 0; ret->dbg_info = 0; ret->dbg_info = 0; return ret; }
static RBinInfo* info(RBinFile *bf) { RBinInfo *ret = NULL; spc_hdr spchdr; memset (&spchdr, 0, SPC_HDR_SIZE); int reat = r_buf_read_at (bf->buf, 0, (ut8*)&spchdr, SPC_HDR_SIZE); if (reat != SPC_HDR_SIZE) { eprintf ("Truncated Header\n"); return NULL; } if (!(ret = R_NEW0 (RBinInfo))) { return NULL; } ret->file = strdup (bf->file); ret->type = strdup ("Sound File Data"); ret->machine = strdup ("SPC700"); ret->os = strdup ("spc700"); ret->arch = strdup ("spc700"); ret->bits = 16; ret->has_va = 1; return ret; }
R_API RAnalData *r_anal_data_new_string (ut64 addr, const char *p, int len, int type) { RAnalData *ad = R_NEW0 (RAnalData); ad->str = NULL; ad->addr = addr; ad->type = type; if (len == 0) len = strlen (p); if (type == R_ANAL_DATA_TYPE_WIDE_STRING) { /* TODO: add support for wide strings */ //eprintf ("r_anal_data_new_string: wide string not supported yet\n"); } else { ad->str = malloc (len+1); memcpy (ad->str, p, len); ad->str[len] = 0; ad->buf = malloc (len+1); memcpy (ad->buf, ad->str, len+1); ad->len = len+1; // string length + \x00 } ad->ptr = 0L; return ad; }
R_API RAnalOp *r_anal_op_copy (RAnalOp *op) { RAnalOp *nop = R_NEW0 (RAnalOp); if (!nop) return NULL; *nop = *op; if (op->mnemonic) { nop->mnemonic = strdup (op->mnemonic); if (!nop->mnemonic) { free (nop); return NULL; } } else { nop->mnemonic = NULL; } nop->src[0] = r_anal_value_copy (op->src[0]); nop->src[1] = r_anal_value_copy (op->src[1]); nop->src[2] = r_anal_value_copy (op->src[2]); nop->dst = r_anal_value_copy (op->dst); r_strbuf_init (&nop->esil); r_strbuf_set (&nop->esil, r_strbuf_get (&op->esil)); return nop; }
static RList *sections(RBinFile *arch) { const char *coffname; size_t i; RList *ret = NULL; RBinSection *ptr = NULL; struct r_bin_coff_obj *obj = (struct r_bin_coff_obj*)arch->o->bin_obj; ret = r_list_new(); if (!ret) return NULL; if (obj && obj->scn_hdrs) for (i = 0; i < obj->hdr.f_nscns; i++) { coffname = r_coff_symbol_name (obj, &obj->scn_hdrs[i]); if (!coffname) { r_list_free (ret); return NULL; } ptr = R_NEW0 (RBinSection); strncpy (ptr->name, coffname, R_BIN_SIZEOF_STRINGS); ptr->size = obj->scn_hdrs[i].s_size; ptr->vsize = obj->scn_hdrs[i].s_size; ptr->paddr = obj->scn_hdrs[i].s_scnptr; ptr->srwx = R_BIN_SCN_MAP; if (obj->scn_hdrs[i].s_flags&COFF_SCN_MEM_READ) ptr->srwx |= R_BIN_SCN_READABLE; if (obj->scn_hdrs[i].s_flags&COFF_SCN_MEM_WRITE) ptr->srwx |= R_BIN_SCN_WRITABLE; if (obj->scn_hdrs[i].s_flags&COFF_SCN_MEM_EXECUTE) ptr->srwx |= R_BIN_SCN_EXECUTABLE; r_list_append (ret, ptr); } return ret; }
R_API RSocket *r_socket_accept(RSocket *s) { RSocket *sock; socklen_t salen = sizeof (s->sa); if (!s) { return NULL; } sock = R_NEW0 (RSocket); if (!sock) { return NULL; } //signal (SIGPIPE, SIG_DFL); sock->fd = accept (s->fd, (struct sockaddr *)&s->sa, &salen); if (sock->fd == -1) { r_sys_perror ("accept"); free (sock); return NULL; } #if HAVE_LIB_SSL sock->is_ssl = s->is_ssl; if (sock->is_ssl) { sock->sfd = NULL; sock->ctx = NULL; sock->bio = NULL; BIO *sbio = BIO_new_socket (sock->fd, BIO_NOCLOSE); sock->sfd = SSL_new (s->ctx); SSL_set_bio (sock->sfd, sbio, sbio); if (SSL_accept (sock->sfd) <= 0) { r_socket_free (sock); return NULL; } sock->bio = BIO_new (BIO_f_buffer ()); sbio = BIO_new (BIO_f_ssl ()); BIO_set_ssl (sbio, sock->sfd, BIO_CLOSE); BIO_push (sock->bio, sbio); } #else sock->is_ssl = 0; #endif return sock; }
R_API RListIter *r_list_prepend(RList *list, void *data) { if (list) { RListIter *item = R_NEW0 (RListIter); if (!item) { return NULL; } if (list->head) { list->head->p = item; } item->data = data; item->n = list->head; item->p = NULL; list->head = item; if (!list->tail) { list->tail = item; } list->length++; list->sorted = true; return item; } return NULL; }
static RBinInfo *info(RBinFile *bf) { RBinJavaObj *jo = bf->o->bin_obj; RBinInfo *ret = R_NEW0 (RBinInfo); if (!ret) { return NULL; } ret->lang = (jo && jo->lang) ? jo->lang : "java"; ret->file = strdup (bf->file); ret->type = strdup ("JAVA CLASS"); ret->bclass = r_bin_java_get_version (bf->o->bin_obj); ret->has_va = 0; // ret->has_lit = true; ret->rclass = strdup ("class"); ret->os = strdup ("any"); ret->subsystem = strdup ("any"); ret->machine = strdup ("jvm"); ret->arch = strdup ("java"); ret->bits = 32; ret->big_endian = 0; ret->dbg_info = 4 | 8; /* LineNums | Syms */ return ret; }
static RBinInfo* info(RBinFile *arch) { RBinInfo *ret = NULL; const int bits = 16; if ((ret = R_NEW0 (RBinInfo)) == NULL) return NULL; ret->file = strdup (arch->file); ret->bclass = strdup ("bootloader"); ret->rclass = strdup ("mbn"); ret->os = strdup ("MBN"); ret->arch = strdup ("arm"); ret->machine = strdup (ret->arch); ret->subsystem = strdup ("mbn"); ret->type = strdup ("sbl"); // secondary boot loader ret->bits = bits; ret->has_va = true; ret->has_crypto = true; // must be false if there' no sign or cert sections ret->has_pi = false; ret->has_nx = false; ret->big_endian = false; ret->dbg_info = false; return ret; }
R_API RPrint *r_print_new() { RPrint *p = R_NEW (RPrint); if (!p) return NULL; strcpy (p->datefmt, "%d:%m:%Y %H:%M:%S %z"); p->user = NULL; r_io_bind_init (p->iob); p->printf = printf; p->interrupt = 0; p->bigendian = 0; p->col = 0; p->width = 78; p->cols = 16; p->cur_enabled = R_FALSE; p->cur = p->ocur = -1; p->addrmod = 4; p->flags = \ R_PRINT_FLAGS_COLOR | R_PRINT_FLAGS_HEADER | R_PRINT_FLAGS_ADDRMOD; p->zoom = R_NEW0 (RPrintZoom); return p; }