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