1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #include "bus-label.h"
32 char *bus_label_escape(const char *s) {
36 assert_return(s, NULL);
38 /* Escapes all chars that D-Bus' object path cannot deal
39 * with. Can be reversed with bus_path_unescape(). We special
40 * case the empty string. */
45 r = new(char, strlen(s)*3 + 1);
49 for (f = s, t = r; *f; f++) {
51 /* Escape everything that is not a-zA-Z0-9. We also
52 * escape 0-9 if it's the first character */
54 if (!(*f >= 'A' && *f <= 'Z') &&
55 !(*f >= 'a' && *f <= 'z') &&
56 !(f > s && *f >= '0' && *f <= '9')) {
58 *(t++) = hexchar(*f >> 4);
69 char *bus_label_unescape(const char *f) {
72 assert_return(f, NULL);
74 /* Special case for the empty string */
78 r = new(char, strlen(f) + 1);
82 for (t = r; *f; f++) {
87 if ((a = unhexchar(f[1])) < 0 ||
88 (b = unhexchar(f[2])) < 0) {
89 /* Invalid escape code, let's take it literal then */
92 *(t++) = (char) ((a << 4) | b);