]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f01de965 | 2 | |
f01de965 | 3 | #include <stdlib.h> |
f01de965 | 4 | |
b5efdb8a | 5 | #include "alloc-util.h" |
f01de965 | 6 | #include "bus-label.h" |
e4e73a63 | 7 | #include "hexdecoct.h" |
b5efdb8a | 8 | #include "macro.h" |
f01de965 KS |
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 | ||
ad922737 | 47 | char *bus_label_unescape_n(const char *f, size_t l) { |
f01de965 | 48 | char *r, *t; |
ad922737 | 49 | size_t i; |
f01de965 KS |
50 | |
51 | assert_return(f, NULL); | |
52 | ||
53 | /* Special case for the empty string */ | |
ad922737 | 54 | if (l == 1 && *f == '_') |
f01de965 KS |
55 | return strdup(""); |
56 | ||
ad922737 | 57 | r = new(char, l + 1); |
f01de965 KS |
58 | if (!r) |
59 | return NULL; | |
60 | ||
ad922737 DH |
61 | for (i = 0, t = r; i < l; ++i) { |
62 | if (f[i] == '_') { | |
f01de965 KS |
63 | int a, b; |
64 | ||
ad922737 DH |
65 | if (l - i < 3 || |
66 | (a = unhexchar(f[i + 1])) < 0 || | |
67 | (b = unhexchar(f[i + 2])) < 0) { | |
f01de965 KS |
68 | /* Invalid escape code, let's take it literal then */ |
69 | *(t++) = '_'; | |
70 | } else { | |
71 | *(t++) = (char) ((a << 4) | b); | |
ad922737 | 72 | i += 2; |
f01de965 KS |
73 | } |
74 | } else | |
ad922737 | 75 | *(t++) = f[i]; |
f01de965 KS |
76 | } |
77 | ||
78 | *t = 0; | |
79 | ||
80 | return r; | |
81 | } |