static char *mnemonics(RAsm *a, int id, bool json) { int i; a->cur->disassemble (a, NULL, NULL, -1); if (id != -1) { const char *name = cs_insn_name (cd, id); if (json) { return name? r_str_newf ("[\"%s\"]\n", name): NULL; } return name? r_str_newf ("%s", name): NULL; } RStrBuf *buf = r_strbuf_new (""); if (json) { r_strbuf_append (buf, "["); } for (i = 1; ; i++) { const char *op = cs_insn_name (cd, i); if (!op) { break; } if (json) { r_strbuf_append (buf, "\""); } r_strbuf_append (buf, op); if (json) { if (cs_insn_name (cd, i + 1)) { r_strbuf_append (buf, "\","); } else { r_strbuf_append (buf, "\"]\n"); } } else { r_strbuf_append (buf, "\n"); } } return r_strbuf_drain (buf); }
// TODO: move into r_util/str R_API char *r_cons_html_filter(const char *ptr, int *newlen) { const char *str = ptr; int esc = 0; int len = 0; int inv = 0; int tmp; bool tag_font = false; if (!ptr) { return NULL; } RStrBuf *res = r_strbuf_new (""); if (!res) { return NULL; } for (; ptr[0]; ptr = ptr + 1) { if (ptr[0] == '\n') { tmp = (int) (size_t) (ptr - str); r_strbuf_append_n (res, str, tmp); if (!ptr[1]) { // write new line if it's the end of the output r_strbuf_append (res, "\n"); } else { r_strbuf_append (res, "<br />"); } str = ptr + 1; continue; } else if (ptr[0] == '<') { tmp = (int) (size_t) (ptr - str); r_strbuf_append_n (res, str, tmp); r_strbuf_append (res, "<"); str = ptr + 1; continue; } else if (ptr[0] == '>') { tmp = (int) (size_t) (ptr - str); r_strbuf_append_n (res, str, tmp); r_strbuf_append (res, ">"); str = ptr + 1; continue; } else if (ptr[0] == ' ') { tmp = (int) (size_t) (ptr - str); r_strbuf_append_n (res, str, tmp); r_strbuf_append (res, " "); str = ptr + 1; continue; } if (ptr[0] == 0x1b) { esc = 1; tmp = (int) (size_t) (ptr - str); r_strbuf_append_n (res, str, tmp); if (tag_font) { r_strbuf_append (res, "</font>"); tag_font = false; } str = ptr + 1; continue; } if (esc == 1) { // \x1b[2J if (ptr[0] != '[') { eprintf ("Oops invalid escape char\n"); esc = 0; str = ptr + 1; continue; } esc = 2; continue; } else if (esc == 2) { // TODO: use dword comparison here if (ptr[0] == '2' && ptr[1] == 'J') { r_strbuf_append (res, "<hr />"); ptr++; esc = 0; str = ptr; continue; } else if (!strncmp (ptr, "48;5;", 5) || !strncmp (ptr, "48;2;", 5)) { char *end = strchr (ptr, 'm'); r_strbuf_appendf (res, "<font style='background-color:%s'>", gethtmlrgb (ptr)); tag_font = true; ptr = end; str = ptr + 1; esc = 0; } else if (!strncmp (ptr, "38;5;", 5) || !strncmp (ptr, "38;2;", 5)) { char *end = strchr (ptr, 'm'); r_strbuf_appendf (res, "<font color='%s'>", gethtmlrgb (ptr)); tag_font = true; ptr = end; str = ptr + 1; esc = 0; } else if (ptr[0] == '0' && ptr[1] == ';' && ptr[2] == '0') { // wtf ? r_cons_gotoxy (0, 0); ptr += 4; esc = 0; str = ptr; continue; } else if (ptr[0] == '0' && ptr[1] == 'm') { str = (++ptr) + 1; esc = inv = 0; continue; // reset color } else if (ptr[0] == '7' && ptr[1] == 'm') { str = (++ptr) + 1; inv = 128; esc = 0; continue; // reset color } else if (ptr[0] == '3' && ptr[2] == 'm') { r_strbuf_appendf (res, "<font color='%s'>", gethtmlcolor (ptr[1], inv? "#fff": "#000")); tag_font = true; ptr = ptr + 1; str = ptr + 2; esc = 0; continue; } else if (ptr[0] == '4' && ptr[2] == 'm') { r_strbuf_appendf (res, "<font style='background-color:%s'>", gethtmlcolor (ptr[1], inv? "#000": "#fff")); tag_font = true; ptr = ptr + 1; str = ptr + 2; esc = 0; continue; } } len++; } if (tag_font) { r_strbuf_append (res, "</font>"); tag_font = false; } r_strbuf_append_n (res, str, ptr - str); if (newlen) { *newlen = res->len; } return r_strbuf_drain (res); }
R_API char *pj_drain(PJ *pj) { char *res = r_strbuf_drain (pj->sb); pj->sb = NULL; free (pj); return res; }