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

utils: add osmo_separated_identifiers_valid()

For validating CTRL input, we want to verify that an input variable is a series
of valid osmo_identifier_valid() separated by dots. Allow validating any
additional chars with identifiers, for CTRL vars will be just ".".

Change-Id: I13dfd02c8c870620f937d789873ad84c6b1c45de
---
M include/osmocom/core/utils.h
M src/utils.c
2 files changed, 18 insertions(+), 4 deletions(-)


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

diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index 5c660cd..0973b4c 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -118,5 +118,6 @@
                    bool require_even);
 
 bool osmo_identifier_valid(const char *str);
+bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars);
 
 /*! @} */
diff --git a/src/utils.c b/src/utils.c
index 6d9e9f1..d64bb57 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -428,19 +428,23 @@
 
 /*! 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
+ *  \param[in] sep_chars Permitted separation characters between identifiers.
+ *  \returns true in case \a str contains only valid identifiers and 
sep_chars, false otherwise
  */
-bool osmo_identifier_valid(const char *str)
+bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars)
 {
        /* characters that are illegal in names */
        static const char illegal_chars[] = "., {}[]()<>|~\\^`'\"?=;/+*&%$#!";
        unsigned int i;
+       size_t len;
 
        /* an empty string is not a valid identifier */
-       if (!str || strlen(str) == 0)
+       if (!str || (len = strlen(str)) == 0)
                return false;
 
-       for (i = 0; i < strlen(str); i++) {
+       for (i = 0; i < len; i++) {
+               if (sep_chars && strchr(sep_chars, str[i]))
+                       continue;
                /* check for 7-bit ASCII */
                if (str[i] & 0x80)
                        return false;
@@ -454,4 +458,13 @@
        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 \a str contains valid identifier, false otherwise
+ */
+bool osmo_identifier_valid(const char *str)
+{
+       return osmo_separated_identifiers_valid(str, NULL);
+}
+
 /*! @} */

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I13dfd02c8c870620f937d789873ad84c6b1c45de
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <[email protected]>

Reply via email to