Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: color ascii escapes in json output #10898

Merged
merged 1 commit into from Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion libr/anal/meta.c
Expand Up @@ -479,7 +479,7 @@ R_API void r_meta_print(RAnal *a, RAnalMetaItem *d, int rad, bool show_full) {
if (!d->subtype) { /* temporary legacy workaround */
esc_bslash = false;
}
str = r_str_escape_latin1 (d->str, false, esc_bslash);
str = r_str_escape_latin1 (d->str, false, esc_bslash, false);
}
} else {
str = r_str_escape (d->str);
Expand Down
2 changes: 1 addition & 1 deletion libr/core/cmd_meta.c
Expand Up @@ -629,7 +629,7 @@ static int cmd_meta_others(RCore *core, const char *input) {
case 0: /* temporary legacy workaround */
esc_bslash = false;
default:
esc_str = r_str_escape_latin1 (mi.str, false, esc_bslash);
esc_str = r_str_escape_latin1 (mi.str, false, esc_bslash, false);
}
if (esc_str) {
r_cons_printf ("\"%s\"\n", esc_str);
Expand Down
26 changes: 13 additions & 13 deletions libr/core/disasm.c
Expand Up @@ -312,7 +312,7 @@ static void ds_print_as_string(RDisasmState *ds);
static void ds_print_core_vmode(RDisasmState *ds, int pos);
static void ds_print_dwarf(RDisasmState *ds);
static void ds_print_asmop_payload(RDisasmState *ds, const ut8 *buf);
static char *ds_esc_str(RDisasmState *ds, const char *str, int len, const char **prefix_out);
static char *ds_esc_str(RDisasmState *ds, const char *str, int len, const char **prefix_out, bool is_comment);
static void ds_print_comments_right(RDisasmState *ds);
static void ds_print_ptr(RDisasmState *ds, int len, int idx);
static void ds_print_str(RDisasmState *ds, const char *str, int len, ut64 refaddr);
Expand Down Expand Up @@ -472,7 +472,7 @@ static void ds_comment(RDisasmState *ds, bool align, const char *format, ...) {
} else {
char buffer[4096];
vsnprintf (buffer, sizeof(buffer), format, ap);
char *escstr = r_str_escape_latin1 (buffer, false, true);
char *escstr = r_str_escape_latin1 (buffer, false, true, false);
if (escstr) {
r_cons_printf ("%s", escstr);
free (escstr);
Expand Down Expand Up @@ -1695,7 +1695,7 @@ static void ds_show_functions(RDisasmState *ds) {
if (comment) {
char *comment_esc = NULL;
if (ds->use_json) {
comment = comment_esc = ds_esc_str (ds, comment, (int)strlen (comment), NULL);
comment = comment_esc = ds_esc_str (ds, comment, (int)strlen (comment), NULL, true);
}

if (comment) {
Expand Down Expand Up @@ -2635,7 +2635,7 @@ static int ds_print_meta_infos(RDisasmState *ds, ut8* buf, int len, int idx) {
esc_bslash = false;
/* fallthrough */
default:
out = r_str_escape_latin1 (mi->str, false, esc_bslash);
out = r_str_escape_latin1 (mi->str, false, esc_bslash, false);
}
if (!out) {
break;
Expand Down Expand Up @@ -3351,14 +3351,14 @@ static void ds_print_asmop_payload(RDisasmState *ds, const ut8 *buf) {
}

/* Do not use this function for escaping JSON! */
static char *ds_esc_str(RDisasmState *ds, const char *str, int len, const char **prefix_out) {
static char *ds_esc_str(RDisasmState *ds, const char *str, int len, const char **prefix_out, bool is_comment) {
int str_len;
char *escstr = NULL;
const char *prefix = "";
bool esc_bslash = ds->core->print->esc_bslash;
switch (ds->strenc) {
case R_STRING_ENC_LATIN1:
escstr = r_str_escape_latin1 (str, ds->show_asciidot, esc_bslash);
escstr = r_str_escape_latin1 (str, ds->show_asciidot, esc_bslash, is_comment);
break;
case R_STRING_ENC_UTF8:
escstr = r_str_escape_utf8 (str, ds->show_asciidot, esc_bslash);
Expand Down Expand Up @@ -3395,7 +3395,7 @@ static char *ds_esc_str(RDisasmState *ds, const char *str, int len, const char *
escstr = r_str_escape_utf32le (str, len, ds->show_asciidot, esc_bslash);
prefix = "U";
} else {
escstr = r_str_escape_latin1 (str, ds->show_asciidot, esc_bslash);
escstr = r_str_escape_latin1 (str, ds->show_asciidot, esc_bslash, is_comment);
}
} else {
RStrEnc enc = R_STRING_ENC_LATIN1;
Expand All @@ -3407,8 +3407,8 @@ static char *ds_esc_str(RDisasmState *ds, const char *str, int len, const char *
}
}
escstr = (enc == R_STRING_ENC_UTF8 ?
r_str_escape_utf8 (str, ds->show_asciidot, esc_bslash) :
r_str_escape_latin1 (str, ds->show_asciidot, esc_bslash));
r_str_escape_utf8 (str, ds->show_asciidot, esc_bslash) :
r_str_escape_latin1 (str, ds->show_asciidot, esc_bslash, is_comment));
}
}
if (prefix_out) {
Expand All @@ -3422,7 +3422,7 @@ static void ds_print_str(RDisasmState *ds, const char *str, int len, ut64 refadd
return;
}
const char *prefix;
char *escstr = ds_esc_str (ds, str, len, &prefix);
char *escstr = ds_esc_str (ds, str, len, &prefix, false);
if (escstr) {
bool inv = ds->show_color && !ds->show_emu_strinv;
ds_begin_comment (ds);
Expand Down Expand Up @@ -3870,7 +3870,7 @@ static int myregwrite(RAnalEsil *esil, const char *name, ut64 *val) {
len = R_DISASM_MAX_STR;
}
#endif
char *escstr = ds_esc_str (ds, str, (int)len, &prefix);
char *escstr = ds_esc_str (ds, str, (int)len, &prefix, false);
const char *escquote = ds->use_json ? "\\\"" : "\"";
if (escstr) {
if (ds->show_color) {
Expand Down Expand Up @@ -4366,7 +4366,7 @@ static void ds_print_comments_right(RDisasmState *ds) {
char *c = comment + line_indexes[i];
char *escstr = NULL;
if (ds->use_json) {
c = escstr = ds_esc_str (ds, c, (int)strlen (c), NULL);
c = escstr = ds_esc_str (ds, c, (int)strlen (c), NULL, true);
}
ds_print_pre (ds);
r_cons_printf ("; %s", c);
Expand All @@ -4381,7 +4381,7 @@ static void ds_print_comments_right(RDisasmState *ds) {
} else {
char *escstr = NULL;
if (ds->use_json) {
comment = escstr = ds_esc_str (ds, comment, (int)strlen (comment), NULL);
comment = escstr = ds_esc_str (ds, comment, (int)strlen (comment), NULL, true);
}
if (comment) {
r_cons_printf ("; %s", comment);
Expand Down
2 changes: 1 addition & 1 deletion libr/include/r_util/r_str.h
Expand Up @@ -133,7 +133,7 @@ R_API int r_str_re_replace(const char *str, const char *reg, const char *sub);
R_API int r_str_unescape(char *buf);
R_API char *r_str_escape(const char *buf);
R_API char *r_str_escape_dot(const char *buf);
R_API char *r_str_escape_latin1(const char *buf, bool show_asciidot, bool esc_bslash);
R_API char *r_str_escape_latin1(const char *buf, bool show_asciidot, bool esc_bslash, bool colors);
R_API char *r_str_escape_utf8(const char *buf, bool show_asciidot, bool esc_bslash);
R_API char *r_str_escape_utf16le(const char *buf, int buf_size, bool show_asciidot, bool esc_bslash);
R_API char *r_str_escape_utf32le(const char *buf, int buf_size, bool show_asciidot, bool esc_bslash);
Expand Down
17 changes: 11 additions & 6 deletions libr/util/str.c
Expand Up @@ -1201,7 +1201,7 @@ static void r_str_byte_escape(const char *p, char **dst, int dot_nl, bool defaul

/* Internal function. dot_nl specifies wheter to convert \n into the
* graphiz-compatible newline \l */
static char *r_str_escape_(const char *buf, int dot_nl, bool ign_esc_seq, bool show_asciidot, bool esc_bslash) {
static char *r_str_escape_(const char *buf, int dot_nl, bool parse_esc_seq, bool ign_esc_seq, bool show_asciidot, bool esc_bslash) {
char *new_buf, *q;
const char *p;

Expand All @@ -1219,7 +1219,8 @@ static char *r_str_escape_(const char *buf, int dot_nl, bool ign_esc_seq, bool s
while (*p) {
switch (*p) {
case 0x1b: // ESC
if (ign_esc_seq) {
if (parse_esc_seq) {
const char *start_seq = p;
p++;
/* Parse the ANSI code (only the graphic mode
* set ones are supported) */
Expand All @@ -1232,6 +1233,10 @@ static char *r_str_escape_(const char *buf, int dot_nl, bool ign_esc_seq, bool s
goto out;
}
}
if (!ign_esc_seq) {
memcpy (q, start_seq, p - start_seq + 1);
q += (p - start_seq + 1);
}
}
break;
}
Expand All @@ -1246,15 +1251,15 @@ static char *r_str_escape_(const char *buf, int dot_nl, bool ign_esc_seq, bool s
}

R_API char *r_str_escape(const char *buf) {
return r_str_escape_ (buf, false, true, false, true);
return r_str_escape_ (buf, false, true, true, false, true);
}

R_API char *r_str_escape_dot(const char *buf) {
return r_str_escape_ (buf, true, true, false, true);
return r_str_escape_ (buf, true, true, true, false, true);
}

R_API char *r_str_escape_latin1(const char *buf, bool show_asciidot, bool esc_bslash) {
return r_str_escape_ (buf, false, false, show_asciidot, esc_bslash);
R_API char *r_str_escape_latin1(const char *buf, bool show_asciidot, bool esc_bslash, bool colors) {
return r_str_escape_ (buf, false, false, !colors, show_asciidot, esc_bslash);
}

static char *r_str_escape_utf(const char *buf, int buf_size, RStrEnc enc, bool show_asciidot, bool esc_bslash) {
Expand Down