diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-04-29 18:39:12 -0300 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-04-30 08:36:01 -0300 |
commit | aa96c6cb44a6eeccc506ae055aae2519a7f914e1 (patch) | |
tree | 7ddc766397769008a014b1777661bbaf94311de8 /src/shared/util.c | |
parent | 6886b0449dbf264f6b7db2a93a1cfee0e4d4080a (diff) |
id128: when taking user input for a 128bit ID, validate syntax
Also, always accept both our simple hexdump syntax and UUID syntax.
Diffstat (limited to 'src/shared/util.c')
-rw-r--r-- | src/shared/util.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/shared/util.c b/src/shared/util.c index 38ee493a81..f9ec14aff4 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -5854,3 +5854,44 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need) { *allocated = a; return q; } + +bool id128_is_valid(const char *s) { + size_t i, l; + + l = strlen(s); + if (l == 32) { + + /* Simple formatted 128bit hex string */ + + for (i = 0; i < l; i++) { + char c = s[i]; + + if (!(c >= '0' && c <= '9') && + !(c >= 'a' && c <= 'z') && + !(c >= 'A' && c <= 'Z')) + return false; + } + + } else if (l == 36) { + + /* Formatted UUID */ + + for (i = 0; i < l; i++) { + char c = s[i]; + + if ((i == 8 || i == 13 || i == 18 || i == 23)) { + if (c != '-') + return false; + } else { + if (!(c >= '0' && c <= '9') && + !(c >= 'a' && c <= 'z') && + !(c >= 'A' && c <= 'Z')) + return false; + } + } + + } else + return false; + + return true; +} |