// XXX buffers_static doesnt constructs the correct string in this callback static int tostring(RDiff *d, void *user, RDiffOp *op) { RDiffUser *u = (RDiffUser*)user; if (op->a_len > 0) { char *a_str = r_str_ndup ((const char *)op->a_buf + op->a_off, op->a_len); u->str = r_str_appendf (u->str, "+(%s)", a_str); #if 0 char *bufasm = r_str_prefix_all (a_str, "- "); u->str = r_str_appendf (u->str, "-(%s)", bufasm); free (bufasm); #endif free (a_str); } if (op->b_len > 0) { char *b_str = r_str_ndup ((const char *)op->b_buf + op->b_off, op->b_len); u->str = r_str_appendf (u->str, "+(%s)", b_str); #if 0 char *bufasm = r_str_prefix_all (b_str, "+ "); u->str = r_str_appendf (u->str, "+(%s)", bufasm); free (bufasm); #endif free (b_str); } if (op->a_len == op->b_len) { char *b_str = r_str_ndup ((const char *)op->a_buf + op->a_off, op->a_len); // char *bufasm = r_str_prefix_all (b_str, " "); u->str = r_str_appendf (u->str, "%s", b_str); // free (bufasm); free (b_str); } return 1; }
static int bootimg_header_load(BootImage *bi, RBuffer *buf, Sdb *db) { char *n; int i; if (r_buf_size (buf) < sizeof (BootImage)) { return false; } // TODO make it endian-safe (void)r_buf_fread_at (buf, 0, (ut8*)bi, "IIiiiiiiiiiiii", 1); (void)r_buf_read_at (buf, 0, (ut8*)bi, sizeof (BootImage)); if ((n = r_str_ndup ((char*)bi->name, BOOT_NAME_SIZE))) { sdb_set (db, "name", n, 0); free (n); } if ((n = r_str_ndup ((char*)bi->cmdline, BOOT_ARGS_SIZE))) { sdb_set (db, "cmdline", n, 0); free (n); } for (i=0; i<8; i++) { sdb_num_set (db, "id", (ut64)bi->id[i], 0); } if ((n = r_str_ndup ((char*)bi->extra_cmdline, BOOT_EXTRA_ARGS_SIZE))) { sdb_set (db, "extra_cmdline", n, 0); free (n); } return true; }
static RBinInfo *info(RBinFile *arch) { ut8 rom_info[16]; RBinInfo *ret = R_NEW0 (RBinInfo); if (!ret) { return NULL; } if (!arch || !arch->buf) { free (ret); return NULL; } ret->lang = NULL; r_buf_read_at (arch->buf, 0xa0, rom_info, 16); ret->file = r_str_ndup ((const char *) rom_info, 12); ret->type = r_str_ndup ((char *) &rom_info[12], 4); ret->machine = strdup ("GameBoy Advance"); ret->os = strdup ("any"); ret->arch = strdup ("arm"); ret->has_va = 1; ret->bits = 32; ret->big_endian = 0; ret->dbg_info = 0; return ret; }
static RBinInfo* info(RBinFile *arch) { RBinInfo *ret = NULL; PebbleAppInfo pai; memset (&pai, 0, sizeof (pai)); int reat = r_buf_read_at (arch->buf, 0, (ut8*)&pai, sizeof (pai)); if (reat != sizeof (pai)) { eprintf ("Truncated Header\n"); return NULL; } if (!(ret = R_NEW0 (RBinInfo))) return NULL; ret->lang = NULL; ret->file = strdup (arch->file); ret->type = strdup ("pebble"); ret->bclass = r_str_ndup (pai.name, 32); ret->rclass = r_str_ndup (pai.company, 32); ret->os = strdup ("rtos"); ret->subsystem = strdup ("pebble"); ret->machine = strdup ("watch"); ret->arch = strdup ("arm"); // thumb only ret->has_va = 1; ret->bits = 16; ret->big_endian = 0; ret->dbg_info = 0; return ret; }
static void color_line(const char *line, RStrpool *p, RList *ml){ int m_len, offset = 0; char *m_addr; RListIter *it; RRegexMatch *m; char *inv[2] = { R_CONS_INVERT (true, true), R_CONS_INVERT (false, true) }; int linv[2] = { strlen (inv[0]), strlen(inv[1]) }; r_strpool_empty (p); r_list_foreach (ml, it, m) { /* highlight a match */ r_strpool_memcat (p, line + offset, m->rm_so - offset); r_strpool_memcat (p, inv[0], linv[0]); m_len = m->rm_eo - m->rm_so; if (m_len<0) m_len = 0; m_addr = r_str_ndup (line + m->rm_so, m_len); if (m_addr) { /* in case there's a CSI in the middle of this match*/ m_len = r_str_ansi_filter (m_addr, NULL, NULL, m_len); if (m_len<0) m_len = 0; r_strpool_memcat (p, m_addr, m_len); r_strpool_memcat (p, inv[1], linv[1]); offset = m->rm_eo; free(m_addr); } }
static RList *parseSegments(RBuffer *buf, int off, int count) { ut8 *b = calloc (count, 32); (void)r_buf_read_at (buf, off, b, count * 32); int x = off; int X = 0; int i; RList *segments = r_list_newf ((RListFree)r_bin_section_free); if (!segments) { return NULL; } // eprintf ("Segments: %d\n", count); for (i = 0; i < count; i++) { int A = r_read_le32 (b + X + 16); int B = r_read_le32 (b + X + 16 + 8); // eprintf ("0x%08x segment 0x%08x 0x%08x %s\n", // x, A, A + B, b + X); const char *cname = (const char *)(b + X); char *name = r_str_ndup (cname, r_str_nlen (cname, 16)); RBinSection *section = newSection (name, A, A + B, true); free (name); r_list_append (segments, section); x += 32; X += 32; } return segments; }
static int cmd_colon(void *_core, const char *cmd) { // RCore *core = (RCore*)_core; if (*cmd == '{') { // parse json here, and retrieve arguments return 1; } if (!*cmd) { return 1; } if (*cmd == '?') { eprintf ("Usage: :<command> <arguments\n"); eprintf ("Usage: :{json-goes-here}\n"); eprintf ("See: T command to save/replay/. long commands\n"); eprintf ("See: e http.colon=true\n"); return 1; } const char *space = strchr (cmd, ' '); if (space) { int len = space - cmd; char *action = r_str_ndup (cmd, len); r_cons_printf ("-> %s\n", action); free (action); return 1; } eprintf ("Invalid command\n"); // Use hashtable return 0; }
R_API char *r_hex_from_py_array(char *out, const char *code) { const char abc[] = "0123456789abcdef"; if (*code != '[' || !strchr (code, ']')) { return NULL; } code++; for (; *code; code++) { char *comma = strchr (code, ','); if (!comma) { comma = strchr (code, ']'); } if (!comma) { break; } char * _word = r_str_ndup (code, comma - code); const char *word = _word; while (*word == ' ' || *word == '\t' || *word == '\n') { word++; word = skip_comment_py (word); } if (IS_DIGIT (*word)) { ut8 n = (ut8)r_num_math (NULL, word); *out++ = abc[(n >> 4) & 0xf]; *out++ = abc[n & 0xf]; } free (_word); code = comma; if (*code == ']') { break; } }
static void handle_friend_message( Tox *tox, uint32_t friend_number, TOX_MESSAGE_TYPE type, const uint8_t *message, size_t length, void *user_data) { TOX_ERR_FRIEND_SEND_MESSAGE err_send; eprintf ("<%d> %s\n", friend_number, message); if (*message != ':') { return; } tox_self_set_typing(tox, friend_number, true, NULL); char *cmd = r_str_ndup (message, length); char *res = r_core_cmd_str (core, cmd + 1); if (res && *res) { const int MAXK = 1280; int bak = 0; int k = 0; char *r = res; while (1) { if (strlen (r) > MAXK) { k = MAXK; bak = r[k]; r[k] = 0; char *nl = r_str_rchr (r, NULL, '\n'); if (nl) { r[k] = bak; bak = *nl; *nl = 0; k = nl - r; } } else { bak = 0; } tox_friend_send_message (tox, friend_number, type, r, strlen (r), &err_send); // tox_friend_send_message (tox, friend_number, type, message, length, &err_send); if (err_send != TOX_ERR_FRIEND_SEND_MESSAGE_OK) { fprintf(stderr, "unable to send message back to friend %d: %d\n", friend_number, err_send); } if (bak) { r[k] = bak; r += k; } else { break; } } } else { eprintf ("Invalid command?\n"); } free (res); free (cmd); tox_self_set_typing(tox, friend_number, false, NULL); }
static RBinInfo* info(RBinFile *arch) { RBinInfo *ret = NULL; if (!(ret = R_NEW0 (RBinInfo))) return NULL; ret->file = strdup (arch->file); ret->type = strdup ("ROM"); ret->machine = strdup ("Sega Megadrive"); ret->bclass = r_str_ndup ((char*)arch->buf->buf + 0x100, 32); ret->os = strdup ("smd"); ret->arch = strdup ("m68k"); ret->bits = 16; ret->has_va = 1; return ret; }
static const char *set_attr(RConsCanvas *c, const char *s) { const char *p; char *color; /* check for ANSI sequences and use them as attr */ if (is_ansi_seq(s)) { p = s + 2; for (p = s; *p && *p != 'J' && *p != 'm' && *p != 'H'; ++p); p++; color = r_str_ndup(s, p - s); c->attr = color; s = p; } return s; }
/* check for ANSI sequences and use them as attr */ static const char *set_attr(RConsCanvas *c, const char *s) { const char *p = s; char *color; while (is_ansi_seq (p)) { p += 2; while (*p && *p != 'J' && *p != 'm' && *p != 'H') { p++; } p++; } if (p != s) { color = r_str_ndup (s, p - s); c->attr = color; } return p; }
static RBinInfo* info(RBinFile *bf) { ut8 rom_header[76]; RBinInfo *ret = R_NEW0 (RBinInfo); if (!ret || !bf || !bf->buf) { free (ret); return NULL; } r_buf_read_at (bf->buf, 0x104, rom_header, 76); ret->file = r_str_ndup ((const char*)&rom_header[48], 16); ret->type = malloc (128); ret->type[0] = 0; gb_get_gbtype (ret->type, rom_header[66], rom_header[63]); gb_add_cardtype (ret->type, rom_header[67]); // XXX ret->machine = strdup ("Gameboy"); ret->os = strdup ("any"); ret->arch = strdup ("gb"); ret->has_va = true; ret->bits = 16; ret->big_endian = 0; ret->dbg_info = 0; return ret; }
static void color_line(const char *line, RStrpool *p, RRegexMatch *ms){ int i, m_len; int offset = 0; char *m_addr; char *inv[2] = {R_CONS_INVERT(R_TRUE, R_TRUE), R_CONS_INVERT(R_FALSE, R_TRUE)}; int linv[2] = {strlen(inv[0]), strlen(inv[1])}; r_strpool_empty(p); for (i = 0; i < NMATCHES; i++) { if (ms[i].rm_eo && (i < NMATCHES - 1)) { /* highlight a match */ r_strpool_memcat (p, line + offset, ms[i].rm_so - offset); r_strpool_memcat (p, inv[0], linv[0]); m_len = ms[i].rm_eo - ms[i].rm_so; m_addr = r_str_ndup (line + ms[i].rm_so, m_len); if (m_addr) { if(r_str_ansi_chrn (m_addr, m_len) - m_addr < m_len ){ /* there's a CSI in the middle of this match*/ m_len = r_str_ansi_filter(m_addr, NULL, NULL, m_len); } r_strpool_memcat (p, m_addr, m_len); r_strpool_memcat (p, inv[1], linv[1]); offset = ms[i].rm_eo; free(m_addr); } } else { /* append final part of string w/o matches */ r_strpool_append(p, line + offset); break; } } }
static int cmd_meta_lineinfo(RCore *core, const char *input) { int ret; ut64 offset = UT64_MAX; // use this as error value int remove = R_FALSE; int all = R_FALSE; const char *p = input; char *colon, *space, *file_line = 0; if (*p == '?') { eprintf ("Usage: CL[-][*] [file:line] [addr]"); return 0; } if (*p == '-') { p++; remove = R_TRUE; } if (*p == '*') { p++; all = R_TRUE; } if (all) { if (remove) { sdb_reset (core->bin->cur->sdb_addrinfo); } else { sdb_foreach (core->bin->cur->sdb_addrinfo, print_addrinfo, NULL); } return 0; } while (*p == ' ') { p++; } if (*p) { offset = r_num_math (core->num, p); if (!offset) offset = core->offset; } else offset = core->offset; colon = strchr (p, ':'); if (colon) { space = strchr (p, ' '); if (!space) { file_line = strdup (p); } else if (space > colon) { file_line = r_str_ndup (p, space - p); } else { goto error; } colon = strchr (file_line, ':'); if (!colon) goto error; *colon = '|'; while (*p != ' ') p++; while (*p == ' ') p++; if (*p != '\0') { ret = sscanf (p, "0x%"PFMT64x, &offset); if (ret != 1) { eprintf ("Failed to parse addr at %s\n", p); goto error; } ret = cmd_meta_add_fileline (core->bin->cur->sdb_addrinfo, file_line, offset); goto error; } if (!file_line) return -1; if (remove) { remove_meta_fileline (core, file_line); } else { print_meta_fileline (core, file_line); } free (file_line); return 0; } offset = core->offset; if (offset != UT64_MAX) { if (remove) { remove_meta_offset (core, offset); } else { print_meta_offset (core, offset); } } else { goto error; } return 0; error: free (file_line); return -1; }
/* convert: * char *foo = "\x41\x23\x42\x1b"; * into: * 4123421b */ R_API char *r_hex_from_c(const char *code) { const char abc[] = "0123456789abcdefABCDEF"; bool parse_on = false; int parse_mode = 0; char *out, *ret; int is_hexa = 0; if (code) { ret = malloc (strlen (code) * 3); } else { ret = malloc (3); } *ret = 0; out = ret; if (code) { for (;*code; code++) { if (!strncmp (code, "/*", 2)) { /* skip comments */ char *end = strstr (code, "*/"); if (end) { code = end + 1; } else { eprintf ("Missing closing comment\n"); } continue; } if (!strncmp (code, "//", 2)) { char *end = strchr (code, '\n'); if (end) { code = end; } continue; } if (parse_on) { if (*code == '}' || *code == '"') { parse_on = false; // stop parsing after the first string statement break; } } else { if (*code == '{') { parse_mode = *code; for (code++;*code; code++) { if (IS_WHITESPACE (*code)) continue; if (IS_NUMBER (*code)) { parse_on = true; break; } else { parse_on = false; parse_mode = 0; break; } } } } if (parse_on && parse_mode == '{') { char *comma = strchr (code, ','); if (!comma) comma = strchr (code, '}'); if (comma) { char *word = r_str_ndup (code, comma - code); if (IS_NUMBER (*word)) { ut8 n = (ut8)r_num_math (NULL, word); *out++ = abc[(n >> 4) & 0xf]; *out++ = abc[n & 0xf]; } else { parse_on = false; } code = comma; free (word); } } else if (*code == '"') {
static void showstr(const char *str, const ut8 *s, int len) { char *msg = r_str_ndup ((const char*)s, len); eprintf ("%s: %s\n", str, msg); free (msg); }