From febe83c4243520ccc64544026d5f496ef945a9aa Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 3 Oct 2017 17:41:59 +0800 Subject: Introduce osmo_identifier_valid() function to check validity of identifier We define the notion of an 'osmocom identifier' which is basically a 7-bit US-ASCII without any special characters beyond "-_:@". We introduce a function to verify if an identifier consists only of the permitted characters. Change-Id: I96a8d345c5a69238a12d040f39b70c485a5c421c --- src/utils.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src') diff --git a/src/utils.c b/src/utils.c index 534916b6..ef8e916e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -22,6 +22,7 @@ */ +#include #include #include #include @@ -411,6 +412,33 @@ bool osmo_is_hexstr(const char *str, int min_digits, int max_digits, return false; if (require_even && (len & 1)) return false; + + return true; +} + +/*! Determine if a given identifier is valid, i.e. doesn't contain illegal chars + * \param[in] str String to validate + * \returns true in case string contains valid identifier, false otherwise + */ +bool osmo_identifier_valid(const char *str) +{ + /* characters that are illegal in names */ + static const char illegal_chars[] = "., {}[]()<>|~\\^`'\"?=;/+*&%$#!"; + unsigned int i; + + /* an empty string is not a valid identifier */ + if (!str || strlen(str) == 0) + return false; + + for (i = 0; i < strlen(str); i++) { + /* check for 7-bit ASCII */ + if (str[i] & 0x80) + return false; + /* check for some explicit reserved control characters */ + if (strchr(illegal_chars, str[i])) + return false; + } + return true; } -- cgit v1.2.3