summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/osmocom/core/utils.h3
-rw-r--r--src/utils.c28
2 files changed, 31 insertions, 0 deletions
diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index 4b083f6c..c5cc138b 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -22,6 +22,7 @@
/*! Number of bytes necessary to store given BITS */
#define OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8)
+#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@@ -112,4 +113,6 @@ size_t osmo_strlcpy(char *dst, const char *src, size_t siz);
bool osmo_is_hexstr(const char *str, int min_digits, int max_digits,
bool require_even);
+bool osmo_identifier_valid(const char *str);
+
/*! @} */
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 <stdbool.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
@@ -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;
}