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