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