From: Lennart Poettering Date: Thu, 19 Nov 2009 01:50:52 +0000 (+0100) Subject: name: add simple name string validator X-Git-Tag: 0.git+20100605+dfd8ee-1~457 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=07232470c869976ae2d7a36799bc6b0126d7e820;p=systemd name: add simple name string validator --- diff --git a/name.c b/name.c index 4b4b0b87..15e324c5 100644 --- a/name.c +++ b/name.c @@ -2,6 +2,7 @@ #include #include +#include #include "set.h" #include "name.h" @@ -30,6 +31,32 @@ NameType name_type_from_string(const char *n) { return _NAME_TYPE_INVALID; } +#define VALID_CHARS \ + "0123456789" \ + "abcdefghijklmnopqrstuvwxyz" \ + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ + "-_" + +bool name_is_valid(const char *n) { + NameType t; + const char *e, *i; + + assert(n); + + t = name_type_from_string(n); + if (t < 0 || t >= _NAME_TYPE_MAX) + return false; + + if (!(e = strrchr(n, '.'))) + return false; + + for (i = n; i < e; i++) + if (!strchr(VALID_CHARS, *i)) + return false; + + return true; +} + Name *name_new(Manager *m) { Name *n; diff --git a/name.h b/name.h index 3b364f00..062c95aa 100644 --- a/name.h +++ b/name.h @@ -260,6 +260,7 @@ DEFINE_CAST(SNAPSHOT, Snapshot, snapshot); bool name_is_ready(Name *name); NameType name_type_from_string(const char *n); +bool name_is_valid(const char *n); Name *name_new(Manager *m); void name_free(Name *name);