]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/bus-label.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
10 char *bus_label_escape(const char *s
) {
14 assert_return(s
, NULL
);
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. */
23 r
= new(char, strlen(s
)*3 + 1);
27 for (f
= s
, t
= r
; *f
; f
++) {
29 /* Escape everything that is not a-zA-Z0-9. We also
30 * escape 0-9 if it's the first character */
32 if (!(*f
>= 'A' && *f
<= 'Z') &&
33 !(*f
>= 'a' && *f
<= 'z') &&
34 !(f
> s
&& *f
>= '0' && *f
<= '9')) {
36 *(t
++) = hexchar(*f
>> 4);
47 char *bus_label_unescape_n(const char *f
, size_t l
) {
51 assert_return(f
, NULL
);
53 /* Special case for the empty string */
54 if (l
== 1 && *f
== '_')
61 for (i
= 0, t
= r
; i
< l
; ++i
) {
66 (a
= unhexchar(f
[i
+ 1])) < 0 ||
67 (b
= unhexchar(f
[i
+ 2])) < 0) {
68 /* Invalid escape code, let's take it literal then */
71 *(t
++) = (char) ((a
<< 4) | b
);