r_str_unescape: Support all izz esc seqs (#15770) ##bin

* Rewrite r_str_unescape() using switch
This commit is contained in:
Khairul Azhar Kasmiran 2020-01-07 23:41:56 +08:00 committed by radare
parent 46e0a8d169
commit 5a752e4d1c
2 changed files with 57 additions and 23 deletions

View File

@ -1153,19 +1153,36 @@ R_API int r_str_unescape(char *buf) {
if (buf[i] != '\\') {
continue;
}
if (buf[i+1] == 'e') {
int esc_seq_len = 2;
switch (buf[i + 1]) {
case 'e':
buf[i] = 0x1b;
memmove (buf + i + 1, buf + i + 2, strlen (buf + i + 2) + 1);
} else if (buf[i + 1] == '\\') {
break;
case '\\':
buf[i] = '\\';
memmove (buf + i + 1, buf + i + 2, strlen (buf + i + 2) + 1);
} else if (buf[i+1] == 'r') {
break;
case 'r':
buf[i] = 0x0d;
memmove (buf + i + 1, buf + i + 2, strlen (buf + i + 2) + 1);
} else if (buf[i+1] == 'n') {
break;
case 'n':
buf[i] = 0x0a;
memmove (buf + i + 1, buf + i + 2, strlen (buf + i + 2) + 1);
} else if (buf[i + 1] == 'x') {
break;
case 'a':
buf[i] = 0x07;
break;
case 'b':
buf[i] = 0x08;
break;
case 't':
buf[i] = 0x09;
break;
case 'v':
buf[i] = 0x0b;
break;
case 'f':
buf[i] = 0x0c;
break;
case 'x':
err = ch2 = ch = 0;
if (!buf[i + 2] || !buf[i + 3]) {
eprintf ("Unexpected end of string.\n");
@ -1178,24 +1195,27 @@ R_API int r_str_unescape(char *buf) {
return 0; // -1?
}
buf[i] = (ch << 4) + ch2;
memmove (buf + i + 1, buf + i + 4, strlen (buf + i + 4) + 1);
} else if (IS_OCTAL (buf[i + 1])) {
int num_digits = 1;
buf[i] = buf[i + 1] - '0';
if (IS_OCTAL (buf[i + 2])) {
num_digits++;
buf[i] = (ut8)buf[i] * 8 + (buf[i + 2] - '0');
if (IS_OCTAL (buf[i + 3])) {
esc_seq_len = 4;
break;
default:
if (IS_OCTAL (buf[i + 1])) {
int num_digits = 1;
buf[i] = buf[i + 1] - '0';
if (IS_OCTAL (buf[i + 2])) {
num_digits++;
buf[i] = (ut8)buf[i] * 8 + (buf[i + 3] - '0');
buf[i] = (ut8)buf[i] * 8 + (buf[i + 2] - '0');
if (IS_OCTAL (buf[i + 3])) {
num_digits++;
buf[i] = (ut8)buf[i] * 8 + (buf[i + 3] - '0');
}
}
esc_seq_len = 1 + num_digits;
} else {
eprintf ("Error: Unknown escape sequence.\n");
return 0; // -1?
}
memmove (buf + i + 1, buf + i + 1 + num_digits,
strlen (buf + i + 1 + num_digits) + 1);
} else {
eprintf ("'\\x' expected.\n");
return 0; // -1?
}
memmove (buf + i + 1, buf + i + esc_seq_len, strlen (buf + i + esc_seq_len) + 1);
}
return i;
}

View File

@ -396,3 +396,17 @@ EXPECT=<<EOF
0
EOF
RUN
NAME=wz esc seqs
FILE=-
CMDS=<<EOF
e str.escbslash=true
wz \a\b\t\n\v\f\r\e\\\x40\7\15\176
izz~0x0
px 16~0x0
EOF
EXPECT=<<EOF
0 0x00000000 0x00000000 13 14 ascii \a\b\t\n\v\f\r\e\\@\a\r~
0x00000000 0708 090a 0b0c 0d1b 5c40 070d 7e00 0000 ........\@..~...
EOF
RUN