diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-11-21 01:03:26 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-11-21 01:03:26 +0100 |
commit | 28383ba18963cdedd98ced271b3425f7321119b7 (patch) | |
tree | 5d6254dbdd2dd07476cfaf7b4fd9a72c9048e9c2 /src/libsystemd-bus | |
parent | 850516e012f0e2c96cecb63c1e8997e13912fcd6 (diff) |
bus: add API calls to escape string components of objects paths
Diffstat (limited to 'src/libsystemd-bus')
-rw-r--r-- | src/libsystemd-bus/libsystemd-bus.sym | 4 | ||||
-rw-r--r-- | src/libsystemd-bus/sd-bus.c | 72 | ||||
-rw-r--r-- | src/libsystemd-bus/test-bus-marshal.c | 25 |
3 files changed, 101 insertions, 0 deletions
diff --git a/src/libsystemd-bus/libsystemd-bus.sym b/src/libsystemd-bus/libsystemd-bus.sym index 8fa2cdec6d..a8df513009 100644 --- a/src/libsystemd-bus/libsystemd-bus.sym +++ b/src/libsystemd-bus/libsystemd-bus.sym @@ -187,6 +187,10 @@ global: sd_bus_error_is_set; sd_bus_error_has_name; + /* Escaping */ + sd_bus_label_escape; + sd_bus_label_unescape; + /* sd-memfd functions */ sd_memfd_new; sd_memfd_make; diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 72d04d28f1..0ae52563e7 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -2712,3 +2712,75 @@ _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) { return -ENXIO; } + +_public_ char *sd_bus_label_escape(const char *s) { + char *r, *t; + const char *f; + + assert_return(s, NULL); + + /* Escapes all chars that D-Bus' object path cannot deal + * with. Can be reversed with bus_path_unescape(). We special + * case the empty string. */ + + if (*s == 0) + return strdup("_"); + + r = new(char, strlen(s)*3 + 1); + if (!r) + return NULL; + + for (f = s, t = r; *f; f++) { + + /* Escape everything that is not a-zA-Z0-9. We also + * escape 0-9 if it's the first character */ + + if (!(*f >= 'A' && *f <= 'Z') && + !(*f >= 'a' && *f <= 'z') && + !(f > s && *f >= '0' && *f <= '9')) { + *(t++) = '_'; + *(t++) = hexchar(*f >> 4); + *(t++) = hexchar(*f); + } else + *(t++) = *f; + } + + *t = 0; + + return r; +} + +_public_ char *sd_bus_label_unescape(const char *f) { + char *r, *t; + + assert_return(f, NULL); + + /* Special case for the empty string */ + if (streq(f, "_")) + return strdup(""); + + r = new(char, strlen(f) + 1); + if (!r) + return NULL; + + for (t = r; *f; f++) { + + if (*f == '_') { + int a, b; + + if ((a = unhexchar(f[1])) < 0 || + (b = unhexchar(f[2])) < 0) { + /* Invalid escape code, let's take it literal then */ + *(t++) = '_'; + } else { + *(t++) = (char) ((a << 4) | b); + f += 2; + } + } else + *(t++) = *f; + } + + *t = 0; + + return r; +} diff --git a/src/libsystemd-bus/test-bus-marshal.c b/src/libsystemd-bus/test-bus-marshal.c index b7606d7708..98737b6811 100644 --- a/src/libsystemd-bus/test-bus-marshal.c +++ b/src/libsystemd-bus/test-bus-marshal.c @@ -39,6 +39,29 @@ #include "bus-util.h" #include "bus-dump.h" +static void test_bus_label_escape_one(const char *a, const char *b) { + _cleanup_free_ char *t = NULL, *x = NULL, *y = NULL; + + assert_se(t = sd_bus_label_escape(a)); + assert_se(streq(t, b)); + + assert_se(x = sd_bus_label_unescape(t)); + assert_se(streq(a, x)); + + assert_se(y = sd_bus_label_unescape(b)); + assert_se(streq(a, y)); +} + +static void test_bus_label_escape(void) { + test_bus_label_escape_one("foo123bar", "foo123bar"); + test_bus_label_escape_one("foo.bar", "foo_2ebar"); + test_bus_label_escape_one("foo_2ebar", "foo_5f2ebar"); + test_bus_label_escape_one("", "_"); + test_bus_label_escape_one("_", "_5f"); + test_bus_label_escape_one("1", "_31"); + test_bus_label_escape_one(":1", "_3a1"); +} + int main(int argc, char *argv[]) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *copy = NULL; int r, boolean; @@ -294,5 +317,7 @@ int main(int argc, char *argv[]) { assert_se(streq(c, "ccc")); assert_se(streq(d, "3")); + test_bus_label_escape(); + return 0; } |