R_API char *r_cons_editor (const char *file) { char *line; _n = 0; free (path); if (file) { path = strdup (file); lines = r_file_slurp (file, &bytes); nlines = r_str_split (lines, '\n'); eprintf ("Loaded %d lines on %d bytes\n", nlines-1, bytes); } else path = NULL; r_cons_new (); I->line->hist_up = up; I->line->hist_down = down; I->line->contents = I->line->buffer.data; for (;;) { setnewline (_n); snprintf (prompt, sizeof (prompt), "%d: ", _n); r_line_set_prompt (prompt); line = r_line_readline (); saveline (_n, line); _n++; if (!line) break; } filesave (); return NULL; }
static bool r_egg_Cfile_parseCompiled(const char *file) { char *fileExt = r_str_newf ("%s.tmp", file); char *buffer = r_file_slurp (fileExt, NULL); buffer = r_str_replace (buffer, "rdata", "text", false); buffer = r_str_replace (buffer, "rodata", "text", false); buffer = r_str_replace (buffer, "get_pc_thunk.bx", "__getesp__", true); const char *words[] = {".cstring", "size", "___main", "section", "__alloca", "zero", "cfi"}; size_t i; for (i = 0; i < 7; i++) { r_str_stripLine (buffer, words[i]); } free (fileExt); fileExt = r_str_newf ("%s.s", file); if (!r_file_dump (fileExt, (const ut8*) buffer, strlen (buffer), true)) { eprintf ("Error while opening %s.s\n", file); goto fail; } free (buffer); free (fileExt); return true; fail: free (buffer); free (fileExt); return false; }
static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) { char *out; int rlen; if (__plugin_open (io, pathname, 0)) { RIOBind iob; RIOBfdbg *mal = R_NEW0 (RIOBfdbg); r_io_bind (io, &iob); mal->fd = getmalfd (mal); mal->bfvm = bfvm_new (&iob); out = r_file_slurp (pathname+8, &rlen); if (!out || rlen < 1) { free (mal); free (out); return NULL; } mal->size = rlen; mal->buf = malloc (mal->size+1); if (mal->buf != NULL) { memcpy (mal->buf, out, rlen); free (out); return r_io_desc_new (&r_io_plugin_bfdbg, mal->fd, pathname, rw, mode, mal); } eprintf ("Cannot allocate (%s) %d bytes\n", pathname+9, mal->size); free (mal); free (out); } return NULL; }
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; }
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); }
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 */ }
static char *r_debug_rap_reg_profile(RDebug *dbg) { char *out, *tf = r_file_temp ("/tmp/rap.XXXXXX"); int fd = r_cons_pipe_open (tf, 1, 0); r_io_system (dbg->iob.io, "drp"); r_cons_pipe_close (fd); out = r_file_slurp (tf, NULL); r_file_rm (tf); return out; }
// Display the content of a file in the hud R_API char *r_cons_hud_file(const char *f) { char *s = r_file_slurp (f, NULL); if (s) { char *ret = r_cons_hud_string (s); free (s); return ret; } return NULL; }
R_API int r_run_parsefile(RRunProfile *p, const char *b) { char *s = r_file_slurp (b, NULL); if (s) { int ret = r_run_parse (p, s); free (s); return ret; } return 0; }
// Display the content of a file in the hud R_API char *r_cons_hud_file(const char *f, const bool usecolor) { char *s = r_file_slurp (f, NULL); if (s) { char *ret = r_cons_hud_string (s, usecolor); free (s); if (!ret) ret = strdup (""); return ret; } return NULL; }
R_API int r_reg_set_profile(RReg *reg, const char *profile) { int ret; char *base, *file; char *str = r_file_slurp (profile, NULL); if (!str) { base = r_sys_getenv (R_LIB_ENV); if (base) { file = r_str_append (base, profile); str = r_file_slurp (file, NULL); free (file); } } if (!str) { eprintf ("r_reg_set_profile: Cannot find '%s'\n", profile); return false; } ret = r_reg_set_profile_string (reg, str); free (str); return ret; }
// 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 char *getstr(const char *src) { int len; char *ret = NULL; switch (*src) { case '\'': ret = strdup (src+1); if (ret) { len = strlen (ret); if (len>0) { len--; if (ret[len]=='\'') { ret[len] = 0; return ret; } else eprintf ("Missing \"\n"); } free (ret); } return NULL; case '"': ret = strdup (src+1); if (ret) { len = strlen (ret); if (len>0) { len--; if (ret[len]=='"') { ret[len] = 0; r_str_unescape (ret); return ret; } else eprintf ("Missing \"\n"); } free (ret); } return NULL; case '@': // slurp file return r_file_slurp (src+1, NULL); case ':': // hexpairs ret = strdup (src); len = r_hex_str2bin (src+1, (ut8*)ret); if (len>0) { ret[len] = 0; return ret; } else { eprintf ("Invalid hexpair string\n"); free (ret); return NULL; } } r_str_unescape ((ret = strdup (src))); return ret; }
R_API int r_core_project_cat(RCore *core, const char *name) { char *path = r_core_project_file (core, name); if (path) { char *data = r_file_slurp (path, NULL); if (data) { r_cons_println (data); free (data); } } free (path); return 0; }
R_API int r_reg_set_profile(RReg *reg, const char *profile) { int ret; char *base, *file; char *str = r_file_slurp (profile, NULL); if (!str) { // XXX we must define this varname in r_lib.h /compiletime/ base = r_sys_getenv ("LIBR_PLUGINS"); if (base) { file = r_str_concat (base, profile); str = r_file_slurp (file, NULL); free (file); } } if (!str) { eprintf ("r_reg_set_profile: Cannot find '%s'\n", profile); return false; } ret = r_reg_set_profile_string (reg, str); free (str); return ret; }
RBinJavaObj* r_bin_java_new(const char* file) { ut8 *buf; RBinJavaObj *bin = R_NEW0 (RBinJavaObj); bin->file = file; if (!(buf = (ut8*)r_file_slurp (file, &bin->size))) return r_bin_java_free (bin); bin->b = r_buf_new (); if (!r_buf_set_bytes (bin->b, buf, bin->size)) return r_bin_java_free (bin); free (buf); if (!javasm_init (bin)) return r_bin_java_free (bin); return bin; }
struct r_bin_te_obj_t* r_bin_te_new(const char* file) { ut8 *buf; struct r_bin_te_obj_t *bin = R_NEW0 (struct r_bin_te_obj_t); if (!bin) return NULL; bin->file = file; if (!(buf = (ut8*)r_file_slurp(file, &bin->size))) return r_bin_te_free(bin); bin->b = r_buf_new (); if (!r_buf_set_bytes (bin->b, buf, bin->size)) return r_bin_te_free(bin); free (buf); if (!r_bin_te_init(bin)) return r_bin_te_free(bin); return bin; }
static bool cmd_wff(RCore *core, const char *input) { ut8 *buf; int size; // XXX: file names cannot contain spaces const char *arg = input + ((input[1] == ' ') ? 2 : 1); int wseek = r_config_get_i (core->config, "cfg.wseek"); char *p, *a = r_str_trim (strdup (arg)); p = strchr (a, ' '); if (p) { *p++ = 0; } if (*arg =='?' || !*arg) { eprintf ("Usage: wf [file] ([size] ([offset]))\n"); } if (!strcmp (arg, "-")) { char *out = r_core_editor (core, NULL, NULL); if (out) { r_io_write_at (core->io, core->offset, (ut8*)out, strlen (out)); r_core_block_read (core); free (out); } } if ((buf = (ut8*) r_file_slurp (a, &size))) { int u_size = size; int u_offset = 0; u_size = r_num_math (core->num, p); if (u_size < 1) u_size = size; if (p) { *p++ = 0; u_offset = r_num_math (core->num, p); if (u_offset > size) { eprintf ("Invalid offset\n"); free (buf); return false; } } r_io_use_fd (core->io, core->file->fd); r_io_write_at (core->io, core->offset, buf + u_offset, u_size); WSEEK (core, size); free (buf); r_core_block_read (core); } else { eprintf ("Cannot open file '%s'\n", arg); } return true; }
struct r_bin_dyldcache_obj_t* r_bin_dyldcache_new(const char* file) { struct r_bin_dyldcache_obj_t *bin; ut8 *buf; if (!(bin = malloc (sizeof (struct r_bin_dyldcache_obj_t)))) return NULL; memset (bin, 0, sizeof (struct r_bin_dyldcache_obj_t)); bin->file = file; if (!(buf = (ut8*)r_file_slurp(file, &bin->size))) return r_bin_dyldcache_free(bin); bin->b = r_buf_new(); if (!r_buf_set_bytes(bin->b, buf, bin->size)) return r_bin_dyldcache_free(bin); free (buf); if (!r_bin_dyldcache_init(bin)) return r_bin_dyldcache_free(bin); return bin; }
static int lang_duktape_file(RLang *lang, const char *file) { int ret = -1; char *code = r_file_slurp (file, NULL); if (code) { register_helpers (lang); duk_push_lstring (ctx, code, strlen (code)); duk_push_string (ctx,file); free (code); ret = duk_safe_call (ctx, wrapped_compile_execute, 2, 1); if (ret != DUK_EXEC_SUCCESS) { print_error(ctx, stderr); eprintf ("duktape error"); } else { duk_pop (ctx); } } return ret; }
static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) { if (__plugin_open (io, pathname, 0)) { RIOGzip *mal = R_NEW0 (RIOGzip); if (!mal) return NULL; int len; ut8 *data = (ut8*)r_file_slurp (pathname+7, &len); mal->buf = r_inflate (data, len, NULL, &mal->size); if (mal->buf) { RETURN_IO_DESC_NEW (&r_io_plugin_malloc, mal->fd, pathname, rw, mode, mal); } free (data); eprintf ("Cannot allocate (%s) %d bytes\n", pathname+9, mal->size); free (mal); } return NULL; }
struct r_bin_mz_obj_t* r_bin_mz_new(const char* file) { const ut8 *buf; struct r_bin_mz_obj_t *bin = R_NEW0 (struct r_bin_mz_obj_t); if (!bin) { return NULL; } bin->file = file; if (!(buf = (ut8*)r_file_slurp (file, &bin->size))) { return r_bin_mz_free (bin); } bin->b = r_buf_new (); if (!r_buf_set_bytes (bin->b, buf, bin->size)) { free ((void *)buf); return r_bin_mz_free (bin); } free ((void *)buf); if (!r_bin_mz_init (bin)) { return r_bin_mz_free (bin); } return bin; }
R_API void r_core_fortune_list(RCore *core) { // TODO: use file.fortunes // can be dangerous in sandbox mode const char *types = (char *)r_config_get (core->config, "cfg.fortunes.type"); int i, j; for (i = 0; i < R_ARRAY_SIZE (fortunes); i++) { if (strstr (types, fortunes[i])) { char *file = getFortuneFile(core, fortunes[i]); char *str = r_file_slurp (file, NULL); for (j = 0; str[j]; j++) { if (str[j] == '\n') { if (i < j) { str[j] = '\0'; r_cons_printf ("%s\n", str + i); } i = j + 1; } } free (str); free (file); } } }
static char *r_core_project_file(RCore *core, const char *file) { const char *magic = "# r2 rdb project file"; char *data, *prjfile; //if (*file != R_SYS_DIR[0]) { 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_concat (prjfile, R_SYS_DIR); prjfile = r_str_concat (prjfile, file); } data = r_file_slurp (prjfile, NULL); if (data) { if (strncmp (data, magic, strlen (magic))) { R_FREE (prjfile); } } free (data); return prjfile; }
int main(int argc, char **argv) { const char *addr = NULL; RCore *c, *c2; RDiff *d; char *file, *file2; ut8 *bufa, *bufb; int o, sza, szb, rad = 0, delta = 0; int mode = MODE_DIFF; int diffops = 0; int threshold = -1; double sim; while ((o = getopt (argc, argv, "Cpg:Orhcdsvxt:")) != -1) { switch (o) { case 'p': useva = R_FALSE; break; case 'r': rad = 1; break; case 'g': mode = MODE_GRAPH; addr = optarg; break; case 'c': showcount = 1; break; case 'C': mode = MODE_CODE; break; case 'O': diffops = 1; break; case 't': threshold = atoi (optarg); break; case 'd': delta = 1; break; case 'h': return show_help (1); case 's': mode = MODE_DIST; break; case 'x': mode = MODE_COLS; break; // case 'l': // mode = MODE_LOCS; // break; case 'v': printf ("radiff2 v"R2_VERSION"\n"); return 0; default: return show_help (0); } } if (argc<3 || optind+2>argc) return show_help (0); file = argv[optind]; file2 = argv[optind+1]; switch (mode) { case MODE_GRAPH: case MODE_CODE: c = opencore (file); if (!c) eprintf ("Cannot open '%s'\n", file); c2 = opencore (file2); if (!c||!c2) { eprintf ("Cannot open '%s'\n", file2); return 1; } r_anal_diff_setup_i (c->anal, diffops, threshold, threshold); r_anal_diff_setup_i (c2->anal, diffops, threshold, threshold); r_core_gdiff (c, c2); if (mode == MODE_GRAPH) diff_graph (c, c2, addr); else r_core_diff_show (c, c2); return 0; } bufa = (ut8*)r_file_slurp (file, &sza); bufb = (ut8*)r_file_slurp (file2, &szb); if (bufa == NULL || bufb == NULL) { eprintf ("radiff2: Cannot open: %s\n", bufa? file2: file); return 1; } //delta = 0; switch (mode) { case MODE_COLS: dump_cols (bufa, sza, bufb, szb); break; case MODE_DIFF: d = r_diff_new (0LL, 0LL); r_diff_set_delta (d, delta); r_diff_set_callback (d, &cb, (void *)(size_t)rad); r_diff_buffers (d, bufa, sza, bufb, szb); r_diff_free (d); break; case MODE_DIST: r_diff_buffers_distance (NULL, bufa, sza, bufb, szb, &count, &sim); printf ("similarity: %.2f\n", sim); printf ("distance: %d\n", count); break; // case MODE_LOCS: // count = r_diff_lines(file, (char*)bufa, sza, file2, (char*)bufb, szb); // break; /* TODO: DEPRECATE */ case MODE_GRAPH: eprintf ("TODO: Use ragdiff2\n"); break; } if (showcount) printf ("%d\n", count); return 0; }
static char *getstr(const char *src) { int len; char *ret = NULL; switch (*src) { case '\'': ret = strdup (src+1); if (ret) { len = strlen (ret); if (len>0) { len--; if (ret[len]=='\'') { ret[len] = 0; return ret; } else eprintf ("Missing \"\n"); } free (ret); } return NULL; case '"': ret = strdup (src+1); if (ret) { len = strlen (ret); if (len>0) { len--; if (ret[len]=='"') { ret[len] = 0; r_str_unescape (ret); return ret; } else eprintf ("Missing \"\n"); } free (ret); } return NULL; case '@': { char *pat = strchr (src+1, '@'); if (pat) { *pat++ = 0; int i, rep = atoi (src+1); int len = strlen (pat); if (rep>0) { char *buf = malloc (rep); for(i=0;i<rep;i++) { buf[i] = pat[i%len]; } return buf; } } // slurp file return r_file_slurp (src+1, NULL); } case '!': return r_str_trim_tail (r_sys_cmd_str (src+1, NULL, NULL)); case ':': if (src[1]=='!') { ret = r_str_trim_tail (r_sys_cmd_str (src+1, NULL, NULL)); } else { ret = strdup (src); } len = r_hex_str2bin (src+1, (ut8*)ret); if (len>0) { ret[len] = 0; return ret; } else { eprintf ("Invalid hexpair string\n"); free (ret); return NULL; } } r_str_unescape ((ret = strdup (src))); return ret; }
static int cmd_project(void *data, const char *input) { RCore *core = (RCore *) data; const char *file, *arg = (input && *input)? input + 1: NULL; const char *fileproject = r_config_get (core->config, "prj.name"); char *str = NULL; if (!input) { return false; } str = strdup (fileproject); arg = strchr (input, ' '); if (arg) { arg++; } else { if (*input) { arg = input + 1; if (*arg == '&') { arg++; } } } file = arg; switch (input[0]) { case 'c': if (input[1] == ' ') { r_core_project_cat (core, input + 2); } else { eprintf ("Usage: Pc [prjname]\n"); } break; case 'o': // if (r_file_is_regular (file)) if (input[1] == '&') { r_core_project_open (core, file, true); } else if (input[1]) { r_core_project_open (core, file, false); } else { if (file && *file) { r_cons_println (file); } } break; case 'l': r_core_project_list (core, input[1]); break; case 'd': case '-': r_core_project_delete (core, file); break; case 's': if (!file || !file[0]) { /* if no argument specified use current project */ file = str; } if (r_core_project_save (core, file)) { r_config_set (core->config, "prj.name", file); r_cons_println (file); } break; case 'S': if (input[1] == ' ') { r_core_project_save_rdb (core, input + 2, R_CORE_PRJ_ALL); } else { eprintf ("Usage: PS [file]\n"); } break; case 'n': if (!fileproject || !*fileproject) { eprintf ("No project\n"); } else { switch (input[1]) { case '-': /* remove lines containing specific words */ { FILE *fd = r_sandbox_fopen (str, "w"); if (!fd) { eprintf ("Cannot open %s\n", str); } else { char *str = r_core_project_notes_file (core, fileproject); char *data = r_file_slurp (str, NULL); int del = 0; if (data) { char *ptr, *nl; for (ptr = data; ptr; ptr = nl) { nl = strchr (ptr, '\n'); if (nl) { *nl++ = 0; if (strstr (ptr, input + 2)) { del++; } else { fprintf (fd, "%s\n", ptr); } } } free (data); } if (del > 0) { eprintf ("Deleted %d lines\n", del); } free (str); fclose (fd); } } break; case ' ': if (input[2] == '-') { char *str = r_core_project_notes_file (core, fileproject); // edit with cfg.editor const char *editor = r_config_get (core->config, "cfg.editor"); if (str && *str && editor && *editor) { r_sys_cmdf ("%s %s", editor, str); } else { eprintf ("No cfg.editor configured\n"); } free (str); } else { //char *str = r_core_project_notes_file (core, fileproject); // append line to project notes char *str = r_core_project_notes_file (core, fileproject); char *data = r_file_slurp (str, NULL); FILE *fd = r_sandbox_fopen (str, "a"); if (fd) { fprintf (fd, "%s\n", input + 2); fclose (fd); } free (str); free (data); } break; case 'j': if (!input[2]) { int len = 0; /* get base64 string */ char *str = r_core_project_notes_file (core, fileproject); if (str) { char *data = r_file_slurp (str, &len); char *res = r_base64_encode_dyn (data, len); if (res) { r_cons_println (res); free (res); } free (data); free (str); } } else if (input[2] == ' ') { /* set base64 string */ ut8 *data = r_base64_decode_dyn (input + 3, -1); if (data) { char *str = r_core_project_notes_file (core, fileproject); if (str) { r_file_dump (str, data, strlen ((const char *) data), 0); free (str); } free (data); } } else { eprintf ("Usage: `Pnj` or `Pnj ...`\n"); } break; case 'x': r_core_project_execute_cmds (core, fileproject); break; case 0: { char *str = r_core_project_notes_file (core, fileproject); char *data = r_file_slurp (str, NULL); if (data) { r_cons_println (data); free (data); } free (str); } break; case '?': { const char *help_msg[] = { "Usage:", "Pn[j-?] [...]", "Project Notes", "Pn", "", "show project notes", "Pn", " -", "edit notes with cfg.editor", "Pn-", "", "delete notes", "Pn-", "str", "delete lines matching /str/ in notes", "Pnx", "", "run project note commands", "Pnj", "", "show notes in base64", "Pnj", " [base64]", "set notes in base64", NULL }; r_core_cmd_help (core, help_msg); } break; } } break; case 'i': if (file && *file) { char *prjName = r_core_project_info (core, file); r_cons_println (prjName); free (prjName); } break; default: { const char *help_msg[] = { "Usage:", "P[?osi] [file]", "Project management", "Pc", " [file]", "show project script to console", "Pd", " [file]", "delete project", "Pi", " [file]", "show project information", "Pl", "", "list all projects", "Pn", "[j]", "show project notes (Pnj for json)", "Pn", " [base64]", "set notes text", "Pn", " -", "edit notes with cfg.editor", "Po", " [file]", "open project", "Ps", " [file]", "save project", "PS", " [file]", "save script file", "P-", " [file]", "delete project (alias for Pd)", "NOTE:", "", "See 'e??prj.'", "NOTE:", "", "project are stored in ~/.config/radare2/projects", NULL }; r_core_cmd_help (core, help_msg); } break; } free (str); return true; }
/* TODO: split this function into several ones..quite long fun */ static void rcc_next(REgg *egg) { const char *ocn; REggEmit *e = egg->remit; char *str = NULL, *p, *ptr, buf[64]; int i; if (setenviron) { elem[elem_n - 1] = 0; r_sys_setenv (setenviron, elem); R_FREE (setenviron); return; } if (includefile) { char *p, *q, *path; // TODO: add support for directories elem[elem_n-1] = 0; path = find_include (elem, includefile); if (!path) { eprintf ("Cannot find include file '%s'\n", elem); return; } free (includefile); includefile = NULL; rcc_reset_callname (); p = q = r_file_slurp (path, NULL); if (p) { int oline = ++line; elem[0] = 0; // TODO: this must be a separate function elem_n = 0; line = 0; for (; *p; p++) r_egg_lang_parsechar (egg, *p); free (q); line = oline; } else { eprintf ("Cannot find '%s'\n", path); } free (path); return; } docall = 1; if (callname) { if (!strcmp (callname, "goto")) { if (nargs != 1) { eprintf ("Invalid number of arguments for goto()\n"); return; } e->jmp (egg, ctxpush[CTX], 0); rcc_reset_callname (); return; } if (!strcmp (callname, "break")) { e->trap (egg); rcc_reset_callname (); return; } ptr = strchr (callname, '='); if (ptr) { *ptr = '\0'; //ocn = ptr+1; // what is the point of this? } ocn = skipspaces (callname); if (!ocn) return; str = r_egg_mkvar (egg, buf, ocn, 0); if (!str) { eprintf ("Cannot mkvar\n"); return; } if (*ocn=='.') e->call (egg, str, 1); if (!strcmp (str, "while")) { char var[128]; if (lastctxdelta >= 0) exit (eprintf ("ERROR: Unsupported while syntax\n")); sprintf (var, "__begin_%d_%d_%d\n", nfunctions, CTX, nestedi[CTX-1]); e->while_end (egg, var); //get_frame_label (1)); #if 0 eprintf ("------------------------------------------ lastctx: %d\n", lastctxdelta); // TODO: the pushvar is required for the if(){}while(); constructions //char *pushvar = ctxpush[context+nbrackets-1]; /* TODO: support to compare more than one expression (LOGICAL OR) */ rcc_printf (" pop %%eax\n"); rcc_printf (" cmp $0, %%eax\n"); // XXX MUST SUPPORT != 0 COMPARE HERE /* TODO : Simplify!! */ //if (pushvar) // printf(" push %s /* wihle push */\n", pushvar); if (lastctxdelta<0) rcc_printf (" jnz %s\n", get_frame_label (1)); else rcc_printf (" jnz %s\n", get_frame_label (0)); //if (pushvar) // printf(" pop %%"R_AX" /* while pop */\n"); #endif nargs = 0; } else { for (i = 0; i < nsyscalls; i++) { if (!strcmp (str, syscalls[i].name)) { p = syscallbody; e->comment (egg, "set syscall args"); e->syscall_args (egg, nargs); docall = 0; e->comment (egg, "syscall"); r_egg_lang_parsechar (egg, '\n'); /* FIX parsing issue */ if (p) { for (; *p; p++) r_egg_lang_parsechar (egg, *p); } else { char *q, *s = e->syscall (egg, nargs); if (s) { for (q=s; *q; q++) r_egg_lang_parsechar (egg, *q); free (s); } else eprintf ("Cannot get @syscall payload\n"); } docall = 0; break; } } if (docall) for (i = 0; i < ninlines; i++) { if (!strcmp (str, inlines[i].name)) { p = inlines[i].body; docall = 0; e->comment (egg, "inline"); r_egg_lang_parsechar (egg, '\n'); /* FIX parsing issue */ for (; *p; p++) r_egg_lang_parsechar (egg, *p); docall = 0; break; } } if (docall) { e->comment (egg, "call in mode %d", mode); e->call (egg, str, 0); } } if (nargs > 0) e->restore_stack (egg, nargs*e->size); if (ocn) { // Used to call .var0() /* XXX: Probably buggy and wrong */ *buf = 0; free (str); str = r_egg_mkvar (egg, buf, ocn, 0); if (*buf) e->get_result (egg, buf); //else { eprintf("external symbol %s\n", ocn); } } /* store result of call */ if (dstvar) { if (mode != NAKED) { *buf = 0; free (str); str = r_egg_mkvar (egg, buf, dstvar, 0); if (*buf == 0) eprintf ("Cannot resolve variable '%s'\n", dstvar); else e->get_result (egg, buf); } R_FREE (dstvar); } rcc_reset_callname (); } else { int vs = 'l'; char type, *eq, *ptr = elem; elem[elem_n] = '\0'; ptr = (char*)skipspaces (ptr); if (*ptr) { eq = strchr (ptr, '='); if (eq) { char str2[64], *p, ch = *(eq-1); *eq = '\0'; eq = (char*) skipspaces (eq+1); p = r_egg_mkvar (egg, str2, ptr, 0); vs = varsize; if (is_var (eq)) { eq = r_egg_mkvar (egg, buf, eq, 0); if (varxs=='*') e->load (egg, eq, varsize); else /* XXX this is a hack .. must be integrated with pusharg */ if (varxs=='&') e->load_ptr (egg, eq); if (eq) { free (eq); eq = NULL; } type = ' '; } else type = '$'; vs = 'l'; // XXX: add support for != 'l' size e->mathop (egg, ch, vs, type, eq, p); free(p); } else { if (!strcmp (ptr, "break")) { // handle 'break;' e->trap (egg); rcc_reset_callname (); } else { e->mathop (egg, '=', vs, '$', ptr, NULL); } } } } free (str); }
int main(int argc, char **argv) { int i, ret, c, rad = 0, bsize = 0, numblocks = 0, ule = 0, b64mode = 0; const char *algo = "sha256"; /* default hashing algorithm */ const char *seed = NULL; char *hashstr = NULL; int hashstr_len = 0; int hashstr_hex = 0; ut64 algobit; RHash *ctx; RIO *io; while ((c = getopt (argc, argv, "jdDrvea:i:S:s:x:b:nBhf:t:kLq")) != -1) { switch (c) { case 'q': quiet = 1; break; case 'i': iterations = atoi (optarg); if (iterations<0) { eprintf ("error: -i argument must be positive\n"); return 1; } break; case 'j': rad = 'j'; break; case 'S': seed = optarg; break; case 'n': numblocks = 1; break; case 'd': b64mode = 1; break; case 'D': b64mode = 2; break; case 'L': algolist (); return 0; case 'e': ule = 1; break; case 'r': rad = 1; break; case 'k': rad = 2; break; case 'a': algo = optarg; break; case 'B': incremental = 0; break; case 'b': bsize = (int)r_num_math (NULL, optarg); break; case 'f': from = r_num_math (NULL, optarg); break; case 't': to = 1+r_num_math (NULL, optarg); break; case 'v': return blob_version ("rahash2"); case 'h': return do_help (0); case 's': setHashString (optarg, 0); break; case 'x': setHashString (optarg, 1); break; break; default: eprintf ("rahash2: Unknown flag\n"); return 1; } } if ((st64)from>=0 && (st64)to<0) { to = 0; // end of file } if (from || to) { if (to && from>=to) { eprintf ("Invalid -f or -t offsets\n"); return 1; } } do_hash_seed (seed); if (hashstr) { #define INSIZE 32768 if (!strcmp (hashstr, "-")) { int res = 0; hashstr = malloc (INSIZE); if (!hashstr) return 1; res = fread ((void*)hashstr, 1, INSIZE-1, stdin); if (res<1) res = 0; hashstr[res] = '\0'; hashstr_len = res; } if (hashstr_hex) { ut8 *out = malloc ((strlen (hashstr)+1)*2); hashstr_len = r_hex_str2bin (hashstr, out); if (hashstr_len<1) { eprintf ("Invalid hex string\n"); free (out); } hashstr = (char *)out; /* out memleaks here, hashstr can't be freed */ } else { hashstr_len = strlen (hashstr); } if (from) { if (from>=hashstr_len) { eprintf ("Invalid -f.\n"); return 1; } } if (to) { if (to>hashstr_len) { eprintf ("Invalid -t.\n"); return 1; } } else { to = hashstr_len; } hashstr = hashstr+from; hashstr_len = to-from; hashstr[hashstr_len] = '\0'; hashstr_len = r_str_unescape (hashstr); switch (b64mode) { case 1: // encode { char *out = malloc (((hashstr_len+1)*4)/3); if (out) { r_base64_encode (out, (const ut8*)hashstr, hashstr_len); printf ("%s\n", out); fflush (stdout); free (out); } } break; case 2: // decode { ut8 *out = malloc (INSIZE); if (out) { int outlen = r_base64_decode (out, (const char *)hashstr, hashstr_len); write (1, out, outlen); free (out); } } break; default: { char *str = (char *)hashstr; int strsz = hashstr_len; if (_s) { // alloc/concat/resize str = malloc (strsz + s.len); if (s.prefix) { memcpy (str, s.buf, s.len); memcpy (str+s.len, hashstr, hashstr_len); } else { memcpy (str, hashstr, hashstr_len); memcpy (str+strsz, s.buf, s.len); } strsz += s.len; str[strsz] = 0; } algobit = r_hash_name_to_bits (algo); for (i=1; i<0x800000; i<<=1) { if (algobit & i) { int hashbit = i & algobit; ctx = r_hash_new (R_TRUE, hashbit); from = 0; to = strsz; do_hash_internal (ctx, hashbit, (const ut8*)str, strsz, rad, 1, ule); r_hash_free (ctx); } } if (_s) { free (str); free (s.buf); } } } return 0; } if (optind>=argc) return do_help (1); if (numblocks) { bsize = -bsize; } else if (bsize<0) { eprintf ("rahash2: Invalid block size\n"); return 1; } io = r_io_new (); for (ret=0, i=optind; i<argc; i++) { switch (b64mode) { case 1: // encode { int binlen; char *out; ut8 *bin = (ut8*)r_file_slurp (argv[i], &binlen); if (!bin) { eprintf ("Cannot open file\n"); continue; } out = malloc (((binlen+1)*4)/3); if (out) { r_base64_encode (out, bin, binlen); printf ("%s\n", out); fflush (stdout); free (out); } free (bin); } break; case 2: // decode { int binlen, outlen; ut8 *out, *bin = (ut8*)r_file_slurp (argv[i], &binlen); if (!bin) { eprintf ("Cannot open file\n"); continue; } out = malloc (binlen+1); if (out) { outlen = r_base64_decode (out, (const char*)bin, binlen); write (1, out, outlen); free (out); } free (bin); } break; default: if (r_file_is_directory (argv[i])) { eprintf ("rahash2: Cannot hash directories\n"); return 1; } if (!r_io_open_nomap (io, argv[i], 0, 0)) { eprintf ("rahash2: Cannot open '%s'\n", argv[i]); return 1; } ret |= do_hash (argv[i], algo, io, bsize, rad, ule); } } free (hashstr); r_io_free (io); return ret; }
main (int argc, char **argv) { char *s = r_file_slurp (argv[1], NULL); r_cons_new (); r_cons_less (s); }