R_API char *r_core_project_info(RCore *core, const char *prjfile) { char buf[256], *file = NULL, *prj = r_core_project_file (core, prjfile); FILE *fd = prj? r_sandbox_fopen (prj, "r"): NULL; for (;fd;) { fgets (buf, sizeof (buf), fd); if (feof (fd)) break; if (!memcmp (buf, "\"e file.path = ", 15)) { buf[strlen(buf)-2]=0; file = r_str_new (buf+15); break; } // TODO: deprecate before 1.0 if (!memcmp (buf, "e file.path = ", 14)) { buf[strlen(buf)-1]=0; file = r_str_new (buf+14); break; } } if (fd) fclose (fd); r_cons_printf ("%s\n", prj); if (file) r_cons_printf ("FilePath: %s\n", file); free (prj); return file; }
// Returns a new heap-allocated string that matches the format-string // specification. R_API char *r_str_newf(const char *fmt, ...) { int ret, ret2; char *p, string[1024]; va_list ap, ap2; va_start (ap, fmt); va_start (ap2, fmt); if (!strchr (fmt, '%')) { va_end (ap2); va_end (ap); return strdup (fmt); } ret = vsnprintf (string, sizeof (string) - 1, fmt, ap); if (ret < 1 || ret >= sizeof (string)) { p = malloc (ret + 2); if (!p) { va_end (ap2); va_end (ap); return NULL; } ret2 = vsnprintf (p, ret + 1, fmt, ap2); if (ret2 < 1 || ret2 > ret + 1) { free (p); va_end (ap2); va_end (ap); return NULL; } fmt = r_str_new (p); free (p); } else { fmt = r_str_new (string); } va_end (ap2); va_end (ap); return (char*)fmt; }
R_API char *r_anal_value_to_string (RAnalValue *value) { char *out = NULL; if (value) { out = r_str_new (""); if (!value->base && !value->reg) { if (value->imm != -1LL) out = r_str_concatf (out, "0x%"PFMT64x, value->imm); else out = r_str_concat (out, "-1"); } else { if (value->memref) { switch (value->memref) { case 1: out = r_str_concat (out, "(char)"); break; case 2: out = r_str_concat (out, "(short)"); break; case 4: out = r_str_concat (out, "(word)"); break; case 8: out = r_str_concat (out, "(dword)"); break; } out = r_str_concat (out, "["); } if (value->mul) out = r_str_concatf (out, "%d*", value->mul); if (value->reg) out = r_str_concatf (out, "%s", value->reg->name); if (value->regdelta) out = r_str_concatf (out, "+%s", value->regdelta->name); if (value->base!=0) out = r_str_concatf (out, "0x%"PFMT64x, value->base); if (value->delta>0) out = r_str_concatf (out, "+0x%"PFMT64x, value->delta); else if (value->delta<0) out = r_str_concatf (out, "-0x%"PFMT64x, -value->delta); if (value->memref) out = r_str_concat (out, "]"); } } return out; }
static bool addBytes(RAnal *a, const char *name, ut64 size, const ut8 *bytes, const ut8 *mask) { RSignItem *it = R_NEW0 (RSignItem); bool retval = true; if (r_mem_is_zero (mask, size)) { eprintf ("error: zero mask\n"); retval = false; goto exit_function; } it->name = r_str_new (name); it->space = a->zign_spaces.space_idx; it->bytes = R_NEW0 (RSignBytes); it->bytes->size = size; it->bytes->bytes = malloc (size); memcpy (it->bytes->bytes, bytes, size); it->bytes->mask = malloc (size); memcpy (it->bytes->mask, mask, size); retval = addItem (a, it); exit_function: r_sign_item_free (it); return retval; }
static int lang_lib_file_run (RLang *user, const char *file) { char *libpath; void *lib; if (!(libpath = r_str_new (file))) { return -1; } if (!r_str_startswith (libpath, "/") && !r_str_startswith (libpath, "./")) { libpath = r_str_prefix (libpath, "./"); } if (!r_file_exists (libpath)) { if (!r_str_endswith (libpath, R_LIB_EXT)) { libpath = r_str_appendf (libpath, ".%s", R_LIB_EXT); } } if (!r_file_exists (libpath)) { free (libpath); return -1; } lib = r_lib_dl_open (libpath); if (lib) { void (*fcn)(RCore *); fcn = r_lib_dl_sym (lib, "entry"); if (fcn) { fcn (user->user); } else { eprintf ("Cannot find 'entry' symbol in library\n"); } r_lib_dl_close (lib); } free (libpath); return 0; }
R_API char *r_str_newf(const char *fmt, ...) { char string[1024]; va_list ap; va_start (ap, fmt); vsnprintf (string, 1023, fmt, ap); fmt = r_str_new (string); va_end (ap); return (char*)fmt; }
R_API bool r_sign_add_graph(RAnal *a, const char *name, RSignGraph graph) { RSignItem *it = R_NEW0 (RSignItem); bool retval = true; it->name = r_str_new (name); it->space = a->zign_spaces.space_idx; it->graph = R_NEW0 (RSignGraph); *it->graph = graph; retval = addItem (a, it); r_sign_item_free (it); return retval; }
R_API char *r_core_project_info(RCore *core, const char *prjfile) { char buf[256], *file = NULL; char *prj = r_core_project_file (prjfile); FILE *fd; if (prj && (fd = r_sandbox_fopen (prj, "r"))) { for (;;) { fgets (buf, sizeof (buf), fd); if (feof (fd)) break; if (!memcmp (buf, "e file.path = ", 14)) { buf[strlen(buf)-1]=0; file = r_str_new (buf+14); break; } } fclose (fd); } r_cons_printf ("Project : %s\n", prj); if (file) r_cons_printf ("FilePath: %s\n", file); free (prj); return file; }
static bool deserialize(RAnal *a, RSignItem *it, const char *k, const char *v) { char *k2 = NULL, *v2 = NULL, *ptr = NULL, *token = NULL; int i = 0, size = 0; bool retval = true; k2 = r_str_new (k); v2 = r_str_new (v); // Deserialize key: zign|space|name for (ptr = k2, i = 0;; ptr = NULL, i++) { token = strtok (ptr, "|"); if (!token) { break; } switch (i) { case 0: // Const "zign" string break; case 1: it->space = r_space_add (&a->zign_spaces, token); break; case 2: it->name = r_str_new (token); break; } } // Deserialize val: size|bytes|mask|graph for (ptr = v2, i = 0;; ptr = NULL, i++) { token = strtok (ptr, "|"); if (!token) { break; } switch (i) { case 0: size = sdb_atoi (token); if (size > 0) { it->bytes = R_NEW0 (RSignBytes); it->bytes->size = size; } break; case 1: if (it->bytes) { if (strlen (token) != 2 * it->bytes->size) { retval = false; goto exit_function; } it->bytes->bytes = malloc (it->bytes->size); r_hex_str2bin (token, it->bytes->bytes); } break; case 2: if (it->bytes) { if (strlen (token) != 2 * it->bytes->size) { retval = false; goto exit_function; } it->bytes->mask = malloc (it->bytes->size); r_hex_str2bin (token, it->bytes->mask); } break; case 3: if (strlen (token) == 2 * sizeof (RSignGraph)) { it->graph = R_NEW0 (RSignGraph); r_hex_str2bin (token, (ut8 *) it->graph); } break; default: retval = false; goto exit_function; } } exit_function: free (k2); free (v2); return retval; }