2019-09-02 22:15:56 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2019 - 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
|
|
|
|
2020-04-07 12:07:40 +00:00
|
|
|
R_API bool 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
|
|
|
}
|
|
|
|
|
2020-04-07 12:07:40 +00:00
|
|
|
R_API bool 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
|
|
|
}
|
|
|
|
|
2020-04-07 12:07:40 +00:00
|
|
|
R_API bool r_name_filter(char *name, int maxlen) {
|
|
|
|
size_t i, len;
|
2018-04-11 08:25:22 +00:00
|
|
|
if (!name) {
|
2020-04-07 12:07:40 +00:00
|
|
|
return false;
|
2018-04-11 08:25:22 +00:00
|
|
|
}
|
2015-12-08 12:24:21 +00:00
|
|
|
if (maxlen < 0) {
|
2015-10-19 11:21:12 +00:00
|
|
|
maxlen = strlen (name);
|
|
|
|
}
|
2020-03-02 20:39:37 +00:00
|
|
|
r_str_trim (name);
|
|
|
|
char *oname = 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);
|
2018-09-13 08:17:26 +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);
|
|
|
|
}
|
2018-09-13 08:17:26 +00:00
|
|
|
for (i = 0; *(name + i) == '_'; i++, len--) {
|
|
|
|
;
|
|
|
|
}
|
2017-12-19 13:57:32 +00:00
|
|
|
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) {
|
2020-04-07 12:07:40 +00:00
|
|
|
size_t i;
|
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
|
|
|
}
|
2018-10-30 03:36:02 +00:00
|
|
|
char *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;
|
|
|
|
}
|