2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
23 #include "bus-label.h"
24 #include "hexdecoct.h"
27 char *bus_label_escape(const char *s) {
31 assert_return(s, NULL);
33 /* Escapes all chars that D-Bus' object path cannot deal
34 * with. Can be reversed with bus_path_unescape(). We special
35 * case the empty string. */
40 r = new(char, strlen(s)*3 + 1);
44 for (f = s, t = r; *f; f++) {
46 /* Escape everything that is not a-zA-Z0-9. We also
47 * escape 0-9 if it's the first character */
49 if (!(*f >= 'A' && *f <= 'Z') &&
50 !(*f >= 'a' && *f <= 'z') &&
51 !(f > s && *f >= '0' && *f <= '9')) {
53 *(t++) = hexchar(*f >> 4);
64 char *bus_label_unescape_n(const char *f, size_t l) {
68 assert_return(f, NULL);
70 /* Special case for the empty string */
71 if (l == 1 && *f == '_')
78 for (i = 0, t = r; i < l; ++i) {
83 (a = unhexchar(f[i + 1])) < 0 ||
84 (b = unhexchar(f[i + 2])) < 0) {
85 /* Invalid escape code, let's take it literal then */
88 *(t++) = (char) ((a << 4) | b);