Skip to content

Commit

Permalink
Use r_str_escape for str flag cmts (radareorg#7483)
Browse files Browse the repository at this point in the history
* Using r_str_escape for str flag cmts

* Escape '"' and '\'

* Escape ESC

* Properly escape Windows UTF-16 2-byte chars
  • Loading branch information
kazarmy authored and radare committed May 14, 2017
1 parent b0b1c7f commit a4ab7cd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
39 changes: 27 additions & 12 deletions libr/core/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2759,10 +2759,14 @@ static void ds_print_ptr(RDisasmState *ds, int len, int idx) {
f = r_flag_get_i (core->flags, refaddr);
if (f) {
if (strlen (msg) != 1) {
r_str_filter (msg, 0);
}
if (!strncmp (msg, "UH..", 4)) {
*msg = 0;
char *msg2 = r_str_new (msg);
if (msg2) {
r_str_filter (msg2, 0);
if (!strncmp (msg2, "UH..", 4)) {
*msg = 0;
}
free (msg2);
}
}
if (*msg) {
if (strlen (msg) == 1) {
Expand All @@ -2774,19 +2778,30 @@ static void ds_print_ptr(RDisasmState *ds, int len, int idx) {
if (!msg[i]) {
break;
}
if (IS_PRINTABLE (msg[i])) {
ds_comment (ds, false, "%c", msg[i]);
} else {
ds_comment (ds, false, "\\x%02x", msg[i]);
}
if (!msg[i+1]) {
i++;
if (IS_PRINTABLE (msg[i])
&& msg[i] != '"' && msg[i] != '\\') {
ds_comment (ds, false, "%c", msg[i]);
} else {
char *escchar = r_str_escape_all (&msg[i]);
if (escchar) {
ds_comment (ds, false, "%s", escchar);
free (escchar);
}
}
} else {
ds_comment (ds, false, "\\u%02x%02x", msg[i+1], msg[i]);
}
i++;
}
ds_comment (ds, false, "\"%s", nl);
} else {
ALIGN;
ds_comment (ds, true, "; \"%s\"%s", msg, nl);
char *escstr = r_str_escape_all (msg);
if (escstr) {
ALIGN;
ds_comment (ds, true, "; \"%s\"%s", escstr, nl);
free (escstr);
}
}
}
} else {
Expand Down
1 change: 1 addition & 0 deletions libr/include/r_util/r_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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_all(const char *buf);
R_API void r_str_uri_decode(char *buf);
R_API char *r_str_uri_encode(const char *buf);
R_API char *r_str_utf16_decode(const ut8 *s, int len);
Expand Down
38 changes: 22 additions & 16 deletions libr/util/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ R_API void r_str_sanitize(char *c) {

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

Expand Down Expand Up @@ -1240,27 +1240,29 @@ static char *r_str_escape_(const char *buf, const int dot_nl) {
*q++ = 'b';
break;
case 0x1b: // ESC
p++;
/* Parse the ANSI code (only the graphic mode
* set ones are supported) */
if (*p == '\0') {
goto out;
}
if (*p == '[') {
for (p++; *p != 'm'; p++) {
if (*p == '\0') {
goto out;
if (ign_esc_seq) {
p++;
/* Parse the ANSI code (only the graphic mode
* set ones are supported) */
if (*p == '\0') {
goto out;
}
if (*p == '[') {
for (p++; *p != 'm'; p++) {
if (*p == '\0') {
goto out;
}
}
}
break;
}
break;
default:
/* Outside the ASCII printable range */
if (!IS_PRINTABLE (*p)) {
*q++ = '\\';
*q++ = 'x';
*q++ = '0'+((*p)>>4);
*q++ = '0'+((*p)&0xf);
*q++ = "0123456789abcdef"[*p >> 4];
*q++ = "0123456789abcdef"[*p & 0xf];
} else {
*q++ = *p;
}
Expand All @@ -1273,11 +1275,15 @@ static char *r_str_escape_(const char *buf, const int dot_nl) {
}

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

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

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

/* ansi helpers */
Expand Down

0 comments on commit a4ab7cd

Please sign in to comment.