2018-04-11 08:25:22 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2018 - pancake */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-10-24 22:57:03 +00:00
|
|
|
#include <r_util.h>
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2015-01-18 01:17:55 +00:00
|
|
|
R_API int r_name_validate_char(const char ch) {
|
2018-04-11 08:25:22 +00:00
|
|
|
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (IS_DIGIT(ch))) {
|
2016-07-12 20:15:19 +00:00
|
|
|
return true;
|
2018-04-11 08:25:22 +00:00
|
|
|
}
|
2010-08-16 12:58:10 +00:00
|
|
|
switch (ch) {
|
2015-01-02 01:10:20 +00:00
|
|
|
case ':':
|
2014-11-09 17:11:03 +00:00
|
|
|
case '.':
|
|
|
|
case '_':
|
2016-07-12 20:15:19 +00:00
|
|
|
return true;
|
2013-04-15 23:48:03 +00:00
|
|
|
}
|
2016-07-12 20:15:19 +00:00
|
|
|
return false;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2011-06-04 03:27:26 +00:00
|
|
|
R_API int r_name_check(const char *name) {
|
2015-09-25 08:49:27 +00:00
|
|
|
/* Cannot start by number */
|
2018-04-11 08:25:22 +00:00
|
|
|
if (!name || !*name || IS_DIGIT (*name)) {
|
2016-07-12 20:15:19 +00:00
|
|
|
return false;
|
2018-04-11 08:25:22 +00:00
|
|
|
}
|
2015-09-25 08:49:27 +00:00
|
|
|
/* Cannot contain non-alphanumeric chars + [:._] */
|
2018-04-11 08:25:22 +00:00
|
|
|
for (; *name != '\0'; name++) {
|
|
|
|
if (!r_name_validate_char (*name)) {
|
2016-07-12 20:15:19 +00:00
|
|
|
return false;
|
2018-04-11 08:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-12 20:15:19 +00:00
|
|
|
return true;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 13:57:32 +00:00
|
|
|
static inline bool is_special_char (char *name) {
|
|
|
|
const char n = *name;
|
2018-03-11 15:10:54 +00:00
|
|
|
return (n == 'b' || n == 'f' || n == 'n' || n == 'r' || n == 't' || n == 'v' || n == 'a');
|
2017-12-19 13:57:32 +00:00
|
|
|
}
|
|
|
|
|
2011-06-04 03:27:26 +00:00
|
|
|
R_API int r_name_filter(char *name, int maxlen) {
|
2017-12-20 10:29:51 +00:00
|
|
|
int i;
|
|
|
|
size_t len;
|
2009-02-05 21:08:46 +00:00
|
|
|
char *oname;
|
2018-04-11 08:25:22 +00:00
|
|
|
if (!name) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-12-08 12:24:21 +00:00
|
|
|
if (maxlen < 0) {
|
2015-10-19 11:21:12 +00:00
|
|
|
maxlen = strlen (name);
|
|
|
|
}
|
2014-06-23 23:12:54 +00:00
|
|
|
name = oname = r_str_trim_head_tail (name);
|
2015-12-08 12:24:21 +00:00
|
|
|
for (i = 0; *name; name++, i++) {
|
|
|
|
if (maxlen && i > maxlen) {
|
2011-09-14 02:31:22 +00:00
|
|
|
*name = '\0';
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-12-19 13:57:32 +00:00
|
|
|
if (!r_name_validate_char (*name) && *name != '\\') {
|
2013-11-13 01:30:00 +00:00
|
|
|
*name = '_';
|
2015-12-08 12:24:21 +00:00
|
|
|
// r_str_ccpy (name, name+1, 0);
|
2013-11-13 01:30:00 +00:00
|
|
|
//name--;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-19 13:57:32 +00:00
|
|
|
while (i > 0) {
|
|
|
|
if (*(name - 1) == '\\' && is_special_char (name)) {
|
|
|
|
*name = '_';
|
|
|
|
*(name - 1) = '_';
|
|
|
|
}
|
|
|
|
if (*name == '\\') {
|
|
|
|
*name = '_';
|
|
|
|
}
|
|
|
|
name--;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
if (*name == '\\') {
|
|
|
|
*name = '_';
|
|
|
|
}
|
|
|
|
// trimming trailing and leading underscores
|
|
|
|
len = strlen (name);
|
2017-12-20 10:29:51 +00:00
|
|
|
for (; len > 0 && *(name + len - 1) == '_'; len--);
|
2017-12-19 14:18:00 +00:00
|
|
|
if (!len) { // name consists only of underscores
|
2017-12-19 13:57:32 +00:00
|
|
|
return r_name_check (oname);
|
|
|
|
}
|
|
|
|
for (i = 0; *(name + i) == '_'; i++, len--);
|
|
|
|
memmove (name, name + i, len);
|
|
|
|
*(name + len) = '\0';
|
2011-06-04 03:27:26 +00:00
|
|
|
return r_name_check (oname);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2014-11-09 17:11:03 +00:00
|
|
|
|
|
|
|
R_API char *r_name_filter2(const char *name) {
|
|
|
|
int i;
|
|
|
|
char *res;
|
2018-04-11 08:25:22 +00:00
|
|
|
while (!IS_PRINTABLE (*name)) {
|
2014-11-09 17:11:03 +00:00
|
|
|
name++;
|
2018-04-11 08:25:22 +00:00
|
|
|
}
|
2014-11-09 17:11:03 +00:00
|
|
|
res = strdup (name);
|
2015-12-08 12:24:21 +00:00
|
|
|
for (i = 0; res[i]; i++) {
|
2014-11-09 17:11:03 +00:00
|
|
|
if (!r_name_validate_char (res[i])) {
|
|
|
|
res[i] = '_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|