예제 #1
0
파일: io_debug.c 프로젝트: leberus/radare2
static char *get_and_escape_path (char *str)
{
	char *path_bin = strdup (str);
	char *final = NULL;

	if (path_bin) {
		char *p = (char*) r_str_lchr (str, '/');
		char *pp = (char*) r_str_tok (p, ' ', -1);
		char *args;

		if (!pp) {
			// There is nothing more to parse
			free (path_bin);
			return str;
		}

		path_bin[pp - str] = '\0';
		if (strstr (path_bin, "\\ ")) {
		        path_bin = r_str_replace (path_bin, "\\ ", " ", true);
		}
		args = path_bin + (pp - str) + 1;

		char *path_bin_escaped = r_str_arg_escape (path_bin);
		int len = strlen (path_bin_escaped);

		path_bin_escaped[len] = ' ';
		path_bin_escaped[len + 1] = '\0';

		final = r_str_append (path_bin_escaped, args);
		free (path_bin);
       }
예제 #2
0
파일: cmd_eval.c 프로젝트: XVilka/radare2
static bool nextpal_item(RCore *core, int mode, const char *file, int ctr) {
	const char *fn = r_str_lchr (file, '/');
	if (!fn) fn = file;
	switch (mode) {
	case 'j': // json
		r_cons_printf ("%s\"%s\"", ctr?",":"", fn);
		break;
	case 'l': // list
		r_cons_println (fn);
		break;
	case 'p': // previous
		// TODO: move logic here
		break;
	case 'n': // next
		if (getNext) {
			curtheme = r_str_dup (curtheme, fn);
			getNext = false;
			return false;
		} else if (curtheme) {
			if (!strcmp (curtheme, fn)) {
				getNext = true;
			}
		} else {
			curtheme = r_str_dup (curtheme, fn);
			return false;
		}
		break;
	}
	return true;
}
예제 #3
0
파일: c.c 프로젝트: libcrack/radare2
static int lang_c_file(RLang *lang, const char *file) {
    void *lib;
    char *cc, *p, name[512], buf[512];
    const char *libpath, *libname;

    if (strlen (file) > (sizeof(name)-10))
        return R_FALSE;
    if (!strstr (file, ".c"))
        sprintf (name, "%s.c", file);
    else strcpy (name, file);
    if (!r_file_exists (name)) {
        eprintf ("file not found (%s)\n", name);
        return R_FALSE;
    }

    {
        char *a = (char*)r_str_lchr (name, '/');
        if (a) {
            *a = 0;
            libpath = name;
            libname = a+1;
        } else {
            libpath = ".";
            libname = name;
        }
    }
    p = strstr (name, ".c");
    if (p) *p=0;
    cc = r_sys_getenv ("CC");
    if (!cc || !*cc)
        cc = strdup ("gcc");
    snprintf (buf, sizeof (buf), "%s -fPIC -shared %s -o %s/lib%s."R_LIB_EXT
              " $(pkg-config --cflags --libs r_core)", cc, file, libpath, libname);
    free (cc);
    if (r_sandbox_system (buf, 1) != 0)
        return R_FALSE;

    snprintf (buf, sizeof (buf), "%s/lib%s."R_LIB_EXT, libpath, libname);
    lib = r_lib_dl_open (buf);
    if (lib!= NULL) {
        void (*fcn)(RCore *);
        fcn = r_lib_dl_sym (lib, "entry");
        if (fcn) fcn (lang->user);
        else eprintf ("Cannot find 'entry' symbol in library\n");
        r_lib_dl_close (lib);
    } else eprintf ("Cannot open library\n");
    r_file_rm (buf); // remove lib
    return 0;
}
예제 #4
0
파일: cpipe.c 프로젝트: aronsky/radare2
static int lang_cpipe_file(RLang *lang, const char *file) {
	char *a, *cc, *p, name[512];
	const char *libpath, *libname;

	if (strlen (file) > (sizeof (name)-10))
		return false;
	if (!strstr (file, ".c"))
		sprintf (name, "%s.c", file);
	else strcpy (name, file);
	if (!r_file_exists (name)) {
		eprintf ("file not found (%s)\n", name);
		return false;
	}

	a = (char*)r_str_lchr (name, '/');
	if (a) {
		*a = 0;
		libpath = name;
		libname = a + 1;
	} else {
		libpath = ".";
		libname = name;
	}
	r_sys_setenv ("PKG_CONFIG_PATH", R2_LIBDIR"/pkgconfig");
	p = strstr (name, ".c");
	if (p) *p = 0;
	cc = r_sys_getenv ("CC");
	if (!cc || !*cc) {
		free (cc);
		cc = strdup ("gcc");
	}
	char *buf = r_str_newf ("%s %s -o %s/bin%s"
		" $(pkg-config --cflags --libs r_socket)",
		cc, file, libpath, libname);
	free (cc);
	if (r_sandbox_system (buf, 1) == 0) {
		char *binfile = r_str_newf ("%s/bin%s", libpath, libname);
		lang_pipe_run (lang, binfile, -1);
		r_file_rm (binfile);
		free (binfile);
	}
	free (buf);
	return 0;
}
예제 #5
0
파일: bin_java.c 프로젝트: 0xroot/radare2
static RList* classes(RBinArch *arch) {
	char *p;
	RBinClass *c;
	RList *ret = r_list_new ();
	if (!ret) return NULL;
	
	// TODO: add proper support for inner classes in Java
	c = R_NEW0 (RBinClass);
	c->visibility = R_BIN_CLASS_PUBLIC;
	c->name = strdup (arch->file);
	p = strchr (c->name, '.');
	if (p) *p = 0;
	p = r_str_lchr (c->name, '/');
	if (p) strcpy (c->name, p+1);
	c->super = strdup ("Object"); //XXX
	r_list_append (ret, c);

	return ret;
}
예제 #6
0
static bool nextpal_item(RCore *core, int mode, const char *file) {
	const char *fn = r_str_lchr (file, '/');
	if (!fn) fn = file;
	switch (mode) {
	case 'l': // list
		r_cons_printf ("%s\n", fn);
		break;
	case 'n': // next
		if (getNext) {
			curtheme = r_str_dup (curtheme, fn);
			getNext = false;
			return false;
		} else if (curtheme) {
			if (!strcmp (curtheme, fn)) {
				getNext = true;
			}
		} else {
			curtheme = r_str_dup (curtheme, fn);
			return false;
		}
		break;
	}
	return true;
}
예제 #7
0
파일: str.c 프로젝트: djpohly/radare2
R_API const char *r_str_lastbut (const char *s, char ch, const char *but) {
	int idx, _b = 0;
	ut8 *b = (ut8*)&_b;
	const char *isbut, *p, *lp = NULL;
	const int bsz = sizeof (_b);
	if (!but)
		return r_str_lchr (s, ch);
	if (strlen (but) >= bsz) {
		eprintf ("r_str_lastbut: but string too long\n");
		return NULL;
	}
	for (p=s; *p; p++) {
		isbut = but? strchr (but, *p): NULL;
		if (isbut) {
			idx = (int)(size_t)(isbut-but);
			_b = R_BIT_CHK (b, idx)?
				R_BIT_UNSET (b, idx):
				R_BIT_SET (b, idx);
			continue;
		}
		if (*p == ch && !_b) lp = p;
	}
	return lp;
}
예제 #8
0
static int parse(RParse *p, const char *data, char *str) {
	int i, n;
	char w0[32];
	char w1[32];
	char w2[32];
	char w3[32];
	char *buf, *ptr, *optr, *num;

	// malloc can be slow here :?
	buf = strdup (data);
	r_str_trim_head (buf);

	ptr = strchr (buf, '#');
	if (ptr) {
		*ptr = 0;
		r_str_chop (buf);
	}
	if (*buf == '.' || buf[strlen(buf)-1] == ':') {
		free (buf);
		strcpy (str, data);
		return R_TRUE;
	}
	r_str_replace_char (buf, '$', 0);
	r_str_replace_char (buf, '%', 0);
	r_str_replace_char (buf, '\t', ' ');
	r_str_replace_char (buf, '(', '[');
	r_str_replace_char (buf, ')', ']');
	ptr = strchr (buf, '[');
	if (ptr) {
		*ptr = 0;
		num = (char*)r_str_lchr (buf, ' ');
		if (!num)
			num = (char*)r_str_lchr (buf, ',');
		if (num) {
			n = atoi (num+1);
			*ptr = '[';
			memmove (num+1, ptr, strlen (ptr)+1);
			ptr = (char*)r_str_lchr (buf, ']');
			if (n && ptr) {
				char *rest = strdup (ptr+1);
				if(n>0) sprintf (ptr, "+%d]%s", n, rest);
				else sprintf (ptr, "%d]%s", n, rest);
				free (rest);
			}
		} else *ptr = '[';
	}

	if (*buf) {
		*w0 = *w1 = *w2 = *w3 = 0;
		ptr = strchr (buf, ' ');
		if (ptr == NULL)
			ptr = strchr (buf, '\t');
		if (ptr) {
			*ptr = '\0';
			for (++ptr; *ptr==' '; ptr++);
			strncpy (w0, buf, sizeof(w0) - 1);
			strncpy (w1, ptr, sizeof(w1) - 1);

			optr = ptr;
			ptr = strchr (ptr, ',');
			if (ptr) {
				*ptr = '\0';
				for (++ptr; *ptr==' '; ptr++);
				strncpy (w1, optr, sizeof(w1)-1);
				strncpy (w2, ptr, sizeof(w2)-1);
				ptr = strchr (ptr, ',');
				if (ptr) {
					*ptr = '\0';
					for (++ptr; *ptr==' '; ptr++);
					strncpy (w2, optr, sizeof(w2)-1);
					strncpy (w3, ptr, sizeof(w3)-1);
				}
			}
		}
		{
			const char *wa[] = { w0, w1, w2, w3 };
			int nw = 0;
			for (i=0; i<4; i++) {
				if (wa[i][0] != '\0')
				nw++;
			}
			replace (nw, wa, str);
		}
	}
	free (buf);
	return R_TRUE;
}
예제 #9
0
파일: vala.c 프로젝트: P4N74/radare2
static int lang_vala_file(RLang *lang, const char *file, bool silent) {
	void *lib;
	char *p, name[512], buf[512];
	char *vapidir, *srcdir, *libname;

	if (strlen (file)>500)
		return false;
	if (!strstr (file, ".vala"))
		sprintf (name, "%s.vala", file);
	else strcpy (name, file);
	if (!r_file_exists (name)) {
		eprintf ("file not found (%s)\n", name);
		return false;
	}

	srcdir = strdup (file);
	p = (char*)r_str_lchr (srcdir, '/');
	if (p) {
		*p = 0;
		libname = strdup (p+1);
		if (*file!='/') {
			strcpy (srcdir, ".");
		}
	} else {
		libname = strdup (file);
		strcpy (srcdir, ".");
	}
	r_sys_setenv ("PKG_CONFIG_PATH", R2_LIBDIR"/pkgconfig");
	vapidir = r_sys_getenv ("VAPIDIR");
	char *tail = silent?  " > /dev/null 2>&1": "";
	if (vapidir) {
		if (*vapidir) {
			snprintf (buf, sizeof (buf)-1, "valac -d %s --vapidir=%s --pkg r_core -C %s %s",
				srcdir, vapidir, name, tail);
		}
		free (vapidir);
	} else {
		snprintf (buf, sizeof (buf) - 1, "valac -d %s --pkg r_core -C %s %s", srcdir, name, tail);
	}
	free (srcdir);
	if (r_sandbox_system (buf, 1) != 0) {
		free (libname);
		return false;
	}
	p = strstr (name, ".vala"); if (p) *p=0;
	p = strstr (name, ".gs"); if (p) *p=0;
	// TODO: use CC environ if possible
	snprintf (buf, sizeof (buf), "gcc -fPIC -shared %s.c -o lib%s."R_LIB_EXT
		" $(pkg-config --cflags --libs r_core gobject-2.0)", name, libname);
	if (r_sandbox_system (buf, 1) != 0) {
		free (libname);
		return false;
	}

	snprintf (buf, sizeof (buf), "./lib%s."R_LIB_EXT, libname);
	free (libname);
	lib = r_lib_dl_open (buf);
	if (lib != NULL) {
		void (*fcn)(RCore *);
		fcn = r_lib_dl_sym (lib, "entry");
		if (fcn) fcn (lang->user);
		else eprintf ("Cannot find 'entry' symbol in library\n");
		r_lib_dl_close (lib);
	} else eprintf ("Cannot open library\n");
	r_file_rm (buf); // remove lib
	sprintf (buf, "%s.c", name); // remove .c
	r_file_rm (buf);
	return 0;
}
예제 #10
0
bool test_r_str_lchr(void) {
	const char* test = "radare2";
	const char* out = r_str_lchr (test, 'r');
	mu_assert_streq (out, "re2", "pointer to last r in radare2");
	mu_end;
}