Esempio n. 1
0
static int reil_if(RAnalEsil *esil) {
	RAnalReilInst *ins;
	RAnalReilArg *op2, *op1;

	op2 = reil_pop_arg (esil);
	if (!op2) return false;
	op1 = reil_pop_arg (esil);
	if (!op1) {
		R_FREE (op2);
		return false;
	}

	ins = R_NEW0 (RAnalReilInst);
	if (!ins) {
		R_FREE (op2);
		R_FREE (op1);
		return false;
	}
	ins->opcode = REIL_JCC;
	ins->arg[0] = op1;
	ins->arg[2] = op2;
	ins->arg[1] = R_NEW0 (RAnalReilArg);
	if (!ins->arg[1]) {
		reil_free_inst (ins);
		return false;
	}
	reil_make_arg (esil, ins->arg[1], " ");
	reil_print_inst (esil, ins);
	reil_free_inst (ins);
	return true;
}
Esempio n. 2
0
// n = 8, 4, 2, 1
static int reil_peekn(RAnalEsil *esil, ut8 n) {
	RAnalReilArg *op2;
	RAnalReilArg *op1 = reil_pop_arg (esil);
	if (!op1) return false;

	reil_push_arg (esil, op1);
	reil_peek (esil);
	// No need to cast if n = 0
	if (n == 0) {
		R_FREE (op1);
		return true;
	}

	R_FREE (op1);
	op1 = reil_pop_arg (esil);
	if (!op1) return false;

	op2 = R_NEW0 (RAnalReilArg);
	if (!op2) {
		R_FREE (op1);
		return false;
	}
	op2->size = n * 8;
	op2->type = ARG_TEMP;
	get_next_temp_reg (esil, op2->name);
	reil_cast_size (esil, op1, op2);
	esil->Reil->lastsz = 8 * n;

	R_FREE (op2);
	return true;
}
Esempio n. 3
0
// TODO: Add in a Coverity modelling file
char *strcat_dup(char *s1, char *s2, st32 n_free) {
    char *res;
    ut32 len_s1 = s1? strlen (s1) : 0;
    ut32 len_s2 = s2? strlen (s2) : 0;

    if (!(res = (char *)malloc (len_s1 + len_s2 + 1))) {
        return NULL;
    }
    if (len_s1 > 0) {
        memcpy (res, s1, len_s1);
    }
    if (len_s2 > 0) {
        memcpy (res + len_s1, s2, len_s2);
    }
    res[len_s1 + len_s2] = '\0';
    if (n_free == 1) {
        R_FREE (s1);
    } else if (n_free == 2) {
        R_FREE (s2);
    } else if (n_free == 3) {
        R_FREE (s1);
        R_FREE (s2);
    }
    return res;
}
Esempio n. 4
0
R_API bool r_anal_op_fini(RAnalOp *op) {
	if (!op) {
		return false;
	}
	if (((ut64)(size_t)op) == UT64_MAX) {
		return false;
	}
	if (((ut64)(size_t)op->mnemonic) == UT64_MAX) {
		return false;
	}
	r_anal_var_free (op->var);
	r_anal_value_free (op->src[0]);
	r_anal_value_free (op->src[1]);
	r_anal_value_free (op->src[2]);
	r_anal_value_free (op->dst);
	r_strbuf_fini (&op->esil);
	r_anal_switch_op_free (op->switch_op);
	op->src[0] = NULL;
	op->src[1] = NULL;
	op->src[2] = NULL;
	op->dst = NULL;
	op->var = NULL;
	op->switch_op = NULL;
	R_FREE (op->mnemonic);
	R_FREE (op->reg);
	return true;
}
Esempio n. 5
0
void r_bin_bflt_free(struct r_bin_bflt_obj *obj) {
	if (obj) {
		R_FREE (obj->hdr);
		R_FREE (obj->b);
		R_FREE (obj);
	}
}
Esempio n. 6
0
// Free ins and all its arguments
void reil_free_inst(RAnalReilInst *ins) {
	if (!ins) return;
	if (ins->arg[0]) R_FREE(ins->arg[0]);
	if (ins->arg[1]) R_FREE(ins->arg[1]);
	if (ins->arg[2]) R_FREE(ins->arg[2]);
	R_FREE(ins);
}
Esempio n. 7
0
File: lang.c Progetto: tkzv/radare2
static void rcc_element(REgg *egg, char *str) {
	REggEmit *e = egg->remit;
	char *p = strrchr (str, ',');
	int num, num2;

	if (CTX) {
		nargs = 0;
		if (mode == GOTO)
			mode = NORMAL; // XXX
		while (p) {
			*p = '\0';
			p = (char *)skipspaces (p+1);
			rcc_pusharg (egg, p);
			p = strrchr (str, ',');
		}
		if (callname)
			rcc_pusharg (egg, str);
		else
		if (mode == NORMAL) {
			if (!atoi (str)) {
				if (dstvar == NULL) /* return string */
					dstvar = strdup (".fix0");
				rcc_pushstr (egg, str, 1);
			}
		}
	} else {
		switch (mode) {
		case ALIAS:
			e->equ (egg, dstvar, str);
			R_FREE (dstvar);
			mode = NORMAL;
			break;
		case SYSCALL:
			syscalls[nsyscalls].name = strdup (dstvar);
			syscalls[nsyscalls].arg = strdup (str);
			nsyscalls++;
			R_FREE (dstvar);
			break;
		case GOTO:
			elem[elem_n] = 0;
			e->jmp (egg, elem, 0);
			break;
		default:
			p = strchr (str, ',');
			if (p) {
				*p='\0';
				num2 = atoi (p+1);
			} else num2 = 0;
			num = atoi (str) + num2;
			stackframe = num;
			stackfixed = num2;
			if (mode != NAKED)
				e->frame (egg, stackframe+stackfixed);
		}
		elem[0] = 0;
		elem_n = 0;
	}
}
Esempio n. 8
0
int get_template(char *buf, SStrInfo *str_info)
{
	int len = 0;
	unsigned int i = 0;
	char *str_type_code = 0;
	char *tmp = strstr(buf, "@");
	STypeCodeStr type_code_str;
	RListIter *it = 0;
	RList *saved_abbr_names = abbr_names;	// save current abbr names, this

	if (!tmp) {
		goto get_template_err;
	}
	if (!init_type_code_str_struct(&type_code_str)) {
		goto get_template_err;
	}
	abbr_names = r_list_new ();

	// get/copy template len/name
	len += (tmp - buf + 1);
	copy_string(&type_code_str, buf, len - 1);
	buf += len;

	if (*buf != '@') {
		copy_string(&type_code_str, "<", 0);
	}

	// get identifier
	while (*buf != '@') {
		if (i) {
			copy_string (&type_code_str, ", ", 0);
		}
		if (get_type_code_string (buf, &i, &str_type_code) != eDemanglerErrOK) {
			len = 0;
			goto get_template_err;
		}
		copy_string (&type_code_str, str_type_code, 0);
		buf += i;
		len += i;
		R_FREE (str_type_code);
	}
	if (*buf != '@') {
		len = 0;
		goto get_template_err;
	}
	copy_string (&type_code_str, ">", 0);
	buf++;
	len++;

	str_info->str_ptr = type_code_str.type_str;
	str_info->len = type_code_str.curr_pos;

get_template_err:
#if 0
	it = r_list_iterator (abbr_names);
	r_list_foreach (abbr_names, it, tmp) {
		R_FREE (tmp);
	}
Esempio n. 9
0
static int parsedatachar(REgg *egg, char c) {
	static int inlinectr = 0;
	char *str;
	int i, j;

	if (!dstval) {
		return 0;
	}
	/* skip until '{' */
	if (c == '{') { /* XXX: repeated code!! */
		rcc_context (egg, 1);
		if (++inlinectr==1)
			return (ndstval = 0);
	} else if (inlinectr == 0) {
		/* capture value between parenthesis foo@data(NNN) { ... } */
		if (c==')') {
			stackframe = atoi (dstval);
			ndstval = 0;
		} else {
			dstval[ndstval++] = c;
		}
		return 0;
	}
	/* capture body */
	if (c == '}') { /* XXX: repeated code!! */
		if (CTX< 2) {
			inlinectr = 0;
			rcc_context (egg, -1);
			slurp = 0;
			mode = NORMAL;
			/* register */
			if (dstval != NULL && dstvar != NULL) {
				dstval[ndstval]='\0';
				egg->remit->comment (egg, "data (%s)(%s)size=(%d)\n",
					dstvar, dstval, stackframe);
				r_egg_printf (egg, ".data\n");
				for (str=dstval; is_space (*str); str++);
				j = (stackframe)? stackframe: 1;
				/* emit label */
				r_egg_printf (egg, "%s:\n", dstvar);
				for (i=1; i<=j; i++) {
					if (*str=='"')
						r_egg_printf (egg, ".ascii %s%s\n", dstval, (i==j)?"\"\\x00\"":"");
					else r_egg_printf (egg, ".long %s\n", dstval);
				}
				r_egg_printf (egg, ".text\n");
				R_FREE (dstvar);
				R_FREE (dstval);
				ndstval = 0;
				CTX = 0;
				return 1;
			}
		}
	}
	dstval[ndstval++] = c;
	return 0;
}
Esempio n. 10
0
static int __close(RIODesc *fd) {
	RIOSparse *riom;
	if (!fd || !fd->data) {
		return -1;
	}
	riom = fd->data;
	R_FREE (riom->buf);
	R_FREE (fd->data);
	return 0;
}
Esempio n. 11
0
static int __close(RIODesc *fd) {
	RIOBfdbg *riom;
	if (!fd || !fd->data) {
		return -1;
	}
	riom = fd->data;
	bfvm_free (riom->bfvm);
	R_FREE (riom->buf);
	R_FREE (fd->data);
	return 0;
}
Esempio n. 12
0
int init_pdb_parser(R_PDB *pdb, const char *filename) {
	char *signature = NULL;
	int bytes_read = 0;

	if (!pdb) {
		eprintf ("struct R_PDB is not correct\n");
		goto error;
	}
	if (!pdb->cb_printf)
		pdb->cb_printf = (PrintfCallback)printf;

	pdb->buf = r_buf_file(filename);
//	pdb->fp = r_sandbox_fopen (filename, "rb");
//	if (!pdb->fp) {
//		eprintf ("file %s can not be open\n", filename);
//		goto error;
//	}

	signature = (char *)calloc (1, PDB7_SIGNATURE_LEN);
	if (!signature) {
		eprintf ("memory allocation error\n");
		goto error;
	}

	bytes_read = r_buf_read(pdb->buf, (unsigned char *)signature, PDB7_SIGNATURE_LEN);
	if (bytes_read != PDB7_SIGNATURE_LEN) {
		eprintf ("file reading error\n");
		goto error;
	}

	r_buf_seek(pdb->buf, 0, 0);

	if (!memcmp (signature, PDB7_SIGNATURE, PDB7_SIGNATURE_LEN)) {
		pdb->pdb_parse = pdb7_parse;
	} else {
		goto error;
	}

	R_FREE (signature);

	pdb->pdb_streams = r_list_new ();
	pdb->stream_map = 0;
	pdb->finish_pdb_parse = finish_pdb_parse;
	pdb->print_types = print_types;
	pdb->print_gvars = print_gvars;
//	printf("init_pdb_parser() finish with success\n");
	return 1;

error:
	R_FREE (signature);

	return 0;
}
Esempio n. 13
0
R_API RPrint *r_print_free(RPrint *p) {
	if (!p) return NULL;
	r_strht_free (p->formats);
	p->formats = NULL;
	if (p->zoom) {
		free (p->zoom->buf);
		free (p->zoom);
		p->zoom = NULL;
	}
	R_FREE (p->lines_cache);
	R_FREE (p->row_offsets);
	free (p);
	return NULL;
}
Esempio n. 14
0
R_API void r_cons_pal_free () {
	int i;
	RCons *cons = r_cons_singleton ();
	for (i = 0; i < R_CONS_PALETTE_LIST_SIZE; i++) {
		if (cons->pal.list[i]) R_FREE (cons->pal.list[i]);
	}
}
Esempio n. 15
0
R_API void r_cons_pal_random () {
	RCons *cons = r_cons_singleton ();
	ut8 r, g, b;
	char val[32];
	const char *k;
	int i;
	for (i = 0; ; i++) {
		k = r_cons_pal_get_i (i);
		if (!k) break;
		if (cons->truecolor > 0) {
			r = r_num_rand (0xff);
			g = r_num_rand (0xff);
			b = r_num_rand (0xff);
			sprintf (val, "rgb:%02x%02x%02x", r, g, b);
			r_cons_pal_set (k, val);
		} else {
			char *s = r_cons_color_random_string (0);
			if (s) {
				r_cons_pal_set (k, s);
				free (s);
			} else {
				r_cons_pal_set (k, "red");
			}
		}
	}
	for (i = 0; i < R_CONS_PALETTE_LIST_SIZE; i++) {
		if (cons->pal.list[i]) R_FREE (cons->pal.list[i]);
		cons->pal.list[i] = r_cons_color_random (0);
	}
}
Esempio n. 16
0
static pyc_object *get_tuple_object_generic(RBuffer *buffer, ut32 size) {
	pyc_object *ret = R_NEW0 (pyc_object);
	if (!ret)
		return NULL;
	ret->type = TYPE_TUPLE;
	ret->data = r_list_new ();
	if (!ret->data) {
		free (ret);
		return NULL;
	}
	for (ut32 i = 0; i < size; ++i) {
		pyc_object *tmp = get_object (buffer);
		if (!tmp) {
			r_list_free (ret->data);
			R_FREE (ret);
			break;
		}
		if (!r_list_append (ret->data, tmp)) {
			free (tmp);
			r_list_free (ret->data);
			break;
		}
	}
	return ret;
}
Esempio n. 17
0
R_API RList *r_core_list_themes(RCore *core) {
	RList *files = NULL;
	RListIter *iter;
	const char *fn;
	char *home = r_str_home (".config/radare2/cons/");

	RList *list = r_list_new ();
	getNext = false;
	if (home) {
		files = r_sys_dir (home);
		r_list_foreach (files, iter, fn) {
			if (*fn && *fn != '.') {
				r_list_append (list, strdup (fn));
			}
		}
		r_list_free (files);
		R_FREE (home);
	}
	files = r_sys_dir (R2_DATDIR"/radare2/"R2_VERSION"/cons/");
	r_list_foreach (files, iter, fn) {
		if (*fn && *fn != '.') {
			r_list_append (list, strdup (fn));
		}
	}
	r_list_free (files);
	files = NULL;
	return list;
}
Esempio n. 18
0
static int reil_neg(RAnalEsil *esil) {
	char tmp_buf[REGBUFSZ];
	RAnalReilInst *ins;
	RAnalReilArg *op = reil_pop_arg (esil);
	if (!op) return false;

	ins = R_NEW0 (RAnalReilInst);
	if (!ins) {
		R_FREE (op);
		return false;
	}
	ins->opcode = REIL_EQ;
	ins->arg[0] = op;
	r_anal_esil_pushnum (esil, 0);
	ins->arg[1] = reil_pop_arg(esil);
	if (!ins->arg[1]) {
		reil_free_inst (ins);
		return false;
	}
	ins->arg[2] = R_NEW0 (RAnalReilArg);
	if (!ins->arg[2]) {
		reil_free_inst (ins);
		return false;
	}
	get_next_temp_reg (esil, tmp_buf);
	reil_make_arg(esil, ins->arg[2], tmp_buf);
	if (ins->arg[0]->size < ins->arg[1]->size)
		ins->arg[1]->size = ins->arg[0]->size;

	ins->arg[2]->size = 1;
	reil_print_inst (esil, ins);
	reil_push_arg (esil, ins->arg[2]);
	reil_free_inst (ins);
	return true;
}
Esempio n. 19
0
R_API R2Pipe *r2p_open(const char *cmd) {
	R2Pipe *r2p = R_NEW0 (R2Pipe);
	r2p->magic = R2P_MAGIC;
	if (cmd == NULL) {
		r2p->child = -1;
#if __UNIX__
		 {
			char *out = r_sys_getenv ("R2PIPE_IN");
			char *in = r_sys_getenv ("R2PIPE_OUT");
			int done = R_FALSE;
			if (in && out) {
				int i_in = atoi (in);
				int i_out = atoi (out);
				if (i_in>=0 && i_out>=0) {
					r2p->input[0] = r2p->input[1] = i_in;
					r2p->output[0] = r2p->output[1] = i_out;
					done = R_TRUE;
				}
			}
			if (!done) {
				eprintf ("Cannot find R2PIPE_IN or R2PIPE_OUT environment\n");
				R_FREE (r2p);
			}
			free (in);
			free (out);
		 }
		return r2p;
#else
		eprintf ("r2p_open(NULL) not supported on windows\n");
		return NULL;
#endif
	}
#if __WINDOWS__
	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);
	} else {
		int rc;
		if (cmd && *cmd) {
			rc = r_sandbox_system (cmd, 1);
		} else rc = 0;
		r2p_close (r2p);
		exit (0);
		return NULL;
	}
#endif
	return r2p;
}
Esempio n. 20
0
static int reil_peek(RAnalEsil *esil) {
	RAnalReilInst *ins;
	char tmp_buf[REGBUFSZ];
	RAnalReilArg *op1 = reil_pop_arg(esil);
	if (!op1) return false;

	ins = R_NEW0 (RAnalReilInst);
	if (!ins) {
		R_FREE (op1);
		return false;
	}
	ins->opcode = REIL_LDM;
	ins->arg[0] = op1;
	ins->arg[1] = R_NEW0(RAnalReilArg);
	if (!ins->arg[1]) {
		reil_free_inst (ins);
		return false;
	}
	ins->arg[2] = R_NEW0(RAnalReilArg);
	if (!ins->arg[2]) {
		reil_free_inst (ins);
		return false;
	}
	reil_make_arg(esil, ins->arg[1], " ");
	get_next_temp_reg(esil, tmp_buf);
	reil_make_arg(esil, ins->arg[2], tmp_buf);
	ins->arg[2]->size = ins->arg[0]->size;
	reil_print_inst(esil, ins);
	reil_push_arg(esil, ins->arg[2]);
	reil_free_inst(ins);
	return true;
}
Esempio n. 21
0
static char *projectScriptPath(RCore *core, const char *file) {
	const char *magic = "# r2 rdb project file";
	char *data, *prjfile;
	if (r_file_is_abspath (file)) {
		prjfile = strdup (file);
	} else {
		if (!is_valid_project_name (file)) {
			return NULL;
		}
		prjfile = r_file_abspath (r_config_get (core->config, "dir.projects"));
		prjfile = r_str_append (prjfile, R_SYS_DIR);
		prjfile = r_str_append (prjfile, file);
		if (!r_file_exists (prjfile) || r_file_is_directory (prjfile)) {
			prjfile = r_str_append (prjfile, R_SYS_DIR "rc");
		}
	}
	data = r_file_slurp (prjfile, NULL);
	if (data) {
		if (strncmp (data, magic, strlen (magic))) {
			R_FREE (prjfile);
		}
	}
	free (data);
	return prjfile;
}
Esempio n. 22
0
R_API ut8* r_socket_slurp(RSocket *s, int *len) {
	int blockSize = 4096;
	ut8 *ptr, *buf = malloc (blockSize);
	int copied = 0;
	if (len) {
		*len = 0;
	}
	for (;;) {
		int rc = r_socket_read (s, buf + copied, blockSize);
		if (rc > 0) {
			copied += rc;
		}
		ptr = realloc (buf, copied + blockSize);
		if (ptr) {
			buf = ptr;
		} else {
			break;
		}
		if (rc < 1) {
			break;
		}
	}
	if (copied == 0) {
		R_FREE (buf);
	}
	if (len) {
		*len = copied;
	}
	return buf;
}
Esempio n. 23
0
R_API int r_strbuf_append(RStrBuf *sb, const char *s) {
	int l = strlen (s);
	if (l<1) return false;
	if ((sb->len + l + 1) < sizeof (sb->buf)) {
		memcpy (sb->buf + sb->len, s, l + 1);
		R_FREE (sb->ptr);
	} else {
		int newlen = sb->len + l + 128;
		char *p = sb->ptr;
		bool allocated = true;
		if (!sb->ptr) {
			p = malloc (newlen);
			if (p && sb->len > 0) {
				memcpy (p, sb->buf, sb->len);
			}
		} else if (sb->len + l + 1 > sb->ptrlen) {
			p = realloc (sb->ptr, newlen);
		} else {
			allocated = false;
		}
		if (allocated) {
			if (!p) return false;
			sb->ptr = p;
			sb->ptrlen = newlen;
		}
		memcpy (p + sb->len, s, l + 1);
	}
	sb->len += l;
	return true;
}
Esempio n. 24
0
R_API void r_id_storage_delete(RIDStorage* storage, ut32 id) {
	if (!storage || !storage->data || (storage->size <= id)) {
		return;
	}
	storage->data[id] = NULL;
	if (id == storage->top_id) {
		while (storage->top_id && !storage->data[storage->top_id]) {
			storage->top_id--;
		}
		if (!storage->top_id) {
			if (storage->data[storage->top_id]) {
				id_storage_reallocate (storage, 2);
			} else {
				RIDPool* pool = r_id_pool_new (storage->pool->start_id, storage->pool->last_id);
				R_FREE (storage->data);
				storage->size = 0;
				r_id_pool_free (storage->pool);
				storage->pool = pool;
				return;
			}
		} else if ((storage->top_id + 1) < (storage->size / 4)) {
			id_storage_reallocate (storage, storage->size / 2);
		}
	}
	r_id_pool_kick_id (storage->pool, id);
}
Esempio n. 25
0
SDB_API bool sdb_disk_create(Sdb* s) {
	int nlen;
	char *str;
	const char *dir;
	if (!s || s->fdump >= 0) {
		return false; // cannot re-create
	}
	if (!s->dir && s->name) {
		s->dir = strdup (s->name);
	}
	dir = s->dir ? s->dir : "./";
	R_FREE (s->ndump);
	nlen = strlen (dir);
	str = malloc (nlen + 5);
	if (!str) {
		return false;
	}
	memcpy (str, dir, nlen + 1);
	r_sys_mkdirp (str);
	memcpy (str + nlen, ".tmp", 5);
	if (s->fdump != -1) {
		close (s->fdump);
	}
	s->fdump = open (str, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, SDB_MODE);
	if (s->fdump == -1) {
		eprintf ("sdb: Cannot open '%s' for writing.\n", str);
		free (str);
		return false;
	}
	cdb_make_start (&s->m, s->fdump);
	s->ndump = str;
	return true;
}
Esempio n. 26
0
static R2Pipe* r2pipe_open_spawn(R2Pipe* r2pipe) {
#if __UNIX__ || defined(__CYGWIN__)
	char *out = r_sys_getenv ("R2PIPE_IN");
	char *in = r_sys_getenv ("R2PIPE_OUT");
	int done = false;
	if (in && out) {
		int i_in = atoi (in);
		int i_out = atoi (out);
		if (i_in >= 0 && i_out >= 0) {
			r2pipe->input[0] = r2pipe->input[1] = i_in;
			r2pipe->output[0] = r2pipe->output[1] = i_out;
			done = true;
		}
	}
	if (!done) {
		eprintf ("Cannot find R2PIPE_IN or R2PIPE_OUT environment\n");
		R_FREE (r2pipe);
	}
	free (in);
	free (out);
	return r2pipe;
#else
	eprintf ("r2pipe_open(NULL) not supported on windows\n");
	return NULL;
#endif
}
Esempio n. 27
0
static ut8 *slurp(RCore **c, const char *file, int *sz) {
	RIODesc *d;
	RIO *io;
	if (c && file && strstr (file, "://")) {
		ut8 *data = NULL;
		ut64 size;
		if (!*c) {
			*c = opencore (NULL);
		}
		io = (*c)->io;
		d = r_io_open (io, file, 0, 0);
		if (!d) {
			return NULL;
		}
		size = r_io_size (io);
		if (size > 0 || size < ST32_MAX) {
			data = calloc (1, size);
			if (r_io_read_at (io, 0, data, size) == size) {
				if (sz) {
					*sz = size;
				}
			} else {
				eprintf ("slurp: read error\n");
				R_FREE (data);
			}
		} else {
			eprintf ("slurp: File is too big\n");
		}
		r_io_close (io, d);
		return data;
	}
	return (ut8*)r_file_slurp (file, sz);
}
Esempio n. 28
0
static int reil_cmp(RAnalEsil *esil) {
	RAnalReilInst *ins;
	char tmp_buf[REGBUFSZ];
	RAnalReilArg *op2, *op1;

	op2 = reil_pop_arg(esil);
	if (!op2) return false;
	op1 = reil_pop_arg(esil);
	if (!op1) {
		R_FREE (op2);
		return false;
	}

	ins = R_NEW0 (RAnalReilInst);
	if (!ins) {
		R_FREE (op1);
		R_FREE (op2);
		return false;
	}
	ins->opcode = REIL_EQ;
	ins->arg[0] = op2;
	ins->arg[1] = op1;
	ins->arg[2] = R_NEW0(RAnalReilArg);
	if (!ins->arg[2]) {
		reil_free_inst (ins);
		return false;
	}
	get_next_temp_reg(esil, tmp_buf);
	reil_make_arg(esil, ins->arg[2], tmp_buf);
	ins->arg[2]->size = 1;
	reil_print_inst(esil, ins);
	// Set vars needed to determine flags.
	snprintf(esil->Reil->cur, sizeof(esil->Reil->old) - 1, "%s:%d",
			ins->arg[2]->name, ins->arg[2]->size);
	snprintf(esil->Reil->old, sizeof(esil->Reil->cur) - 1, "%s:%d", op2->name,
			op2->size);

	if (r_reg_get(esil->anal->reg, op2->name, -1)) {
		esil->Reil->lastsz = op2->size;
	} else if (r_reg_get(esil->anal->reg, op1->name, -1)) {
		esil->Reil->lastsz = op1->size;
	}

	reil_push_arg(esil, ins->arg[2]);
	reil_free_inst(ins);
	return true;
}
Esempio n. 29
0
// General function for operations that take 2 operands
static int reil_binop(RAnalEsil *esil, RAnalReilOpcode opcode) {
	RAnalReilInst *ins;
	char tmp_buf[REGBUFSZ];
	ut8 dst_size;
	RAnalReilArg *op2, *op1;

	op2 = reil_pop_arg(esil);
	if (!op2) return false;
	op1 = reil_pop_arg(esil);
	if (!op1) {
		R_FREE (op2);
		return false;
	}

	ins = R_NEW0 (RAnalReilInst);
	if (!ins) {
		R_FREE (op1);
		R_FREE (op2);
		return false;
	}
	ins->opcode = opcode;
	ins->arg[0] = op2;
	ins->arg[1] = op1;
	if (!ins->arg[1]) {
		reil_free_inst (ins);
		return false;
	}
	ins->arg[2] = R_NEW0(RAnalReilArg);
	if (!ins->arg[2])  {
		reil_free_inst (ins);
		return false;
	}
	get_next_temp_reg(esil, tmp_buf);
	reil_make_arg(esil, ins->arg[2], tmp_buf);
	// Choose the larger of the two sizes as the size of dst
	dst_size = ins->arg[0]->size;
	if (dst_size < ins->arg[1]->size)
		dst_size = ins->arg[1]->size;
	// REIL_LT has a dst_size of 1.
	if (opcode == REIL_LT)
		dst_size = 1;
	ins->arg[2]->size = dst_size;
	reil_print_inst(esil, ins);
	reil_push_arg(esil, ins->arg[2]);
	reil_free_inst(ins);
	return true;
}
Esempio n. 30
0
R_API int r_core_lines_initcache(RCore *core, ut64 start_addr, ut64 end_addr) {
	int i, line_count;
	int bsz = core->blocksize;
	char *buf;
	ut64 off = start_addr;
	ut64 baddr;
	if (start_addr == UT64_MAX || end_addr == UT64_MAX) {
		return -1;
	}

	free (core->print->lines_cache);
	core->print->lines_cache = R_NEWS0 (ut64, bsz);
	if (!core->print->lines_cache) {
		return -1;
	}

	{
		RIOSection *s = r_io_section_mget_in (core->io, core->offset);
		baddr = s? s->paddr: r_config_get_i (core->config, "bin.baddr");
	}

	line_count = start_addr? 0: 1;
	core->print->lines_cache[0] = start_addr? 0: baddr;
	buf = malloc (bsz);
	if (!buf) {
		return -1;
	}
	r_cons_break_push (NULL, NULL);
	while (off < end_addr) {
		if (r_cons_is_breaked ()) {
			break;
		}
		r_io_read_at (core->io, off, (ut8 *) buf, bsz);
		for (i = 0; i < bsz; i++) {
			if (buf[i] == '\n') {
				core->print->lines_cache[line_count] = start_addr? off + i + 1: off + i + 1 + baddr;
				line_count++;
				if (line_count % bsz == 0) {
					ut64 *tmp = realloc (core->print->lines_cache,
						(line_count + bsz) * sizeof (ut64));
					if (tmp) {
						core->print->lines_cache = tmp;
					} else {
						R_FREE (core->print->lines_cache);
						goto beach;
					}
				}
			}
		}
		off += bsz;
	}
	free (buf);
	r_cons_break_pop ();
	return line_count;
beach:
	free (buf);
	r_cons_break_pop ();
	return -1;
}