2012-12-14 09:51:25 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2012 - 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
|
|
|
|
|
|
|
#define IS_PRINTABLE(x) (x>=' '&&x<='~')
|
|
|
|
|
2013-04-15 23:48:03 +00:00
|
|
|
/* TODO: use a whitelist :) */
|
2011-06-04 03:27:26 +00:00
|
|
|
static int r_name_validate_char(const char ch) {
|
2013-04-15 23:48:03 +00:00
|
|
|
if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
|
|
|
|
return R_TRUE;
|
2010-08-16 12:58:10 +00:00
|
|
|
switch (ch) {
|
2014-11-09 17:11:03 +00:00
|
|
|
case '.':
|
|
|
|
case '_':
|
|
|
|
return R_TRUE;
|
2013-04-15 23:48:03 +00:00
|
|
|
}
|
|
|
|
return R_FALSE;
|
|
|
|
#if 0
|
|
|
|
switch (ch) {
|
|
|
|
case '!': case ':': case '{': case '}': case '$': case '=': case '*':
|
|
|
|
case '/': case '+': case '|': case '&': case ';': case '~': case '"':
|
|
|
|
case '>': case '<': case '#': case '%': case '(': case ')': case '`':
|
|
|
|
case '\'': case '-': case ' ': case '\n': case '\t': case '[': case ']':
|
2009-02-05 21:08:46 +00:00
|
|
|
case '@':
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
if (((ch >= '0') && (ch <= '9')))
|
2011-03-18 08:35:02 +00:00
|
|
|
return R_TRUE;
|
2010-03-08 11:45:22 +00:00
|
|
|
if (!IS_PRINTABLE (ch))
|
2011-03-18 08:35:02 +00:00
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2011-03-18 08:35:02 +00:00
|
|
|
return R_TRUE;
|
2013-04-15 23:48:03 +00:00
|
|
|
#endif
|
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) {
|
2013-04-15 23:48:03 +00:00
|
|
|
if (!name || !*name)
|
2011-03-18 08:35:02 +00:00
|
|
|
return R_FALSE;
|
2013-09-07 00:39:26 +00:00
|
|
|
if (*name>='0' && *name<='9')
|
|
|
|
return R_FALSE;
|
2010-04-09 11:24:40 +00:00
|
|
|
for (;*name!='\0'; name++)
|
2011-06-04 03:27:26 +00:00
|
|
|
if (!r_name_validate_char (*name))
|
2011-03-18 08:35:02 +00:00
|
|
|
return R_FALSE;
|
|
|
|
return R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2011-06-04 03:27:26 +00:00
|
|
|
R_API int r_name_filter(char *name, int maxlen) {
|
2009-02-05 21:08:46 +00:00
|
|
|
int i;
|
|
|
|
char *oname;
|
2014-06-23 23:12:54 +00:00
|
|
|
name = oname = r_str_trim_head_tail (name);
|
2014-11-09 17:11:03 +00:00
|
|
|
for (i=0; *name; name++, i++) {
|
2011-07-19 16:03:31 +00:00
|
|
|
if (maxlen && i>maxlen) {
|
2011-09-14 02:31:22 +00:00
|
|
|
*name = '\0';
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-06-04 03:27:26 +00:00
|
|
|
if (!r_name_validate_char (*name)) {
|
2013-11-13 01:30:00 +00:00
|
|
|
*name = '_';
|
2014-11-09 17:28:10 +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
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
while (!IS_PRINTABLE (*name))
|
|
|
|
name++;
|
|
|
|
res = strdup (name);
|
|
|
|
for (i=0; res[i]; i++) {
|
|
|
|
if (!r_name_validate_char (res[i])) {
|
|
|
|
res[i] = '_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|