chiark / gitweb /
tree-wide: drop 'This file is part of systemd' blurb
[elogind.git] / src / basic / bus-label.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   Copyright 2013 Lennart Poettering
4 ***/
5
6 #include <stdlib.h>
7
8 #include "alloc-util.h"
9 #include "bus-label.h"
10 #include "hexdecoct.h"
11 #include "macro.h"
12
13 char *bus_label_escape(const char *s) {
14         char *r, *t;
15         const char *f;
16
17         assert_return(s, NULL);
18
19         /* Escapes all chars that D-Bus' object path cannot deal
20          * with. Can be reversed with bus_path_unescape(). We special
21          * case the empty string. */
22
23         if (*s == 0)
24                 return strdup("_");
25
26         r = new(char, strlen(s)*3 + 1);
27         if (!r)
28                 return NULL;
29
30         for (f = s, t = r; *f; f++) {
31
32                 /* Escape everything that is not a-zA-Z0-9. We also
33                  * escape 0-9 if it's the first character */
34
35                 if (!(*f >= 'A' && *f <= 'Z') &&
36                     !(*f >= 'a' && *f <= 'z') &&
37                     !(f > s && *f >= '0' && *f <= '9')) {
38                         *(t++) = '_';
39                         *(t++) = hexchar(*f >> 4);
40                         *(t++) = hexchar(*f);
41                 } else
42                         *(t++) = *f;
43         }
44
45         *t = 0;
46
47         return r;
48 }
49
50 char *bus_label_unescape_n(const char *f, size_t l) {
51         char *r, *t;
52         size_t i;
53
54         assert_return(f, NULL);
55
56         /* Special case for the empty string */
57         if (l == 1 && *f == '_')
58                 return strdup("");
59
60         r = new(char, l + 1);
61         if (!r)
62                 return NULL;
63
64         for (i = 0, t = r; i < l; ++i) {
65                 if (f[i] == '_') {
66                         int a, b;
67
68                         if (l - i < 3 ||
69                             (a = unhexchar(f[i + 1])) < 0 ||
70                             (b = unhexchar(f[i + 2])) < 0) {
71                                 /* Invalid escape code, let's take it literal then */
72                                 *(t++) = '_';
73                         } else {
74                                 *(t++) = (char) ((a << 4) | b);
75                                 i += 2;
76                         }
77                 } else
78                         *(t++) = f[i];
79         }
80
81         *t = 0;
82
83         return r;
84 }