[PATCH] libosmocore[master]: Introduce osmo_identifier_valid() function to check validity...

Harald Welte gerrit-no-reply at lists.osmocom.org
Tue Oct 3 10:21:43 UTC 2017


Review at  https://gerrit.osmocom.org/4128

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
---
M include/osmocom/core/utils.h
M src/utils.c
2 files changed, 30 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/28/4128/1

diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index 855e653..1127217 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -20,6 +20,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>
 
@@ -89,4 +90,6 @@
 
 size_t osmo_strlcpy(char *dst, const char *src, size_t siz);
 
+bool osmo_identifier_valid(const char *str);
+
 /*! @} */
diff --git a/src/utils.c b/src/utils.c
index 1c176f8..2ed5b16 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>
@@ -375,4 +376,30 @@
 	return ret;
 }
 
+/*! 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;
+}
+
 /*! @} */

-- 
To view, visit https://gerrit.osmocom.org/4128
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I96a8d345c5a69238a12d040f39b70c485a5c421c
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>


More information about the gerrit-log mailing list