]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/bus-label.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Lennart Poettering
8 #include "alloc-util.h"
10 #include "hexdecoct.h"
13 char *bus_label_escape(const char *s
) {
17 assert_return(s
, NULL
);
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. */
26 r
= new(char, strlen(s
)*3 + 1);
30 for (f
= s
, t
= r
; *f
; f
++) {
32 /* Escape everything that is not a-zA-Z0-9. We also
33 * escape 0-9 if it's the first character */
35 if (!(*f
>= 'A' && *f
<= 'Z') &&
36 !(*f
>= 'a' && *f
<= 'z') &&
37 !(f
> s
&& *f
>= '0' && *f
<= '9')) {
39 *(t
++) = hexchar(*f
>> 4);
50 char *bus_label_unescape_n(const char *f
, size_t l
) {
54 assert_return(f
, NULL
);
56 /* Special case for the empty string */
57 if (l
== 1 && *f
== '_')
64 for (i
= 0, t
= r
; i
< l
; ++i
) {
69 (a
= unhexchar(f
[i
+ 1])) < 0 ||
70 (b
= unhexchar(f
[i
+ 2])) < 0) {
71 /* Invalid escape code, let's take it literal then */
74 *(t
++) = (char) ((a
<< 4) | b
);