]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/bus-label.c
in-addr-util: add new helper call in_addr_prefix_from_string_auto()
[thirdparty/systemd.git] / src / basic / bus-label.c
CommitLineData
f01de965
KS
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Lennart Poettering
5
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.
10
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.
15
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/>.
18***/
19
f01de965 20#include <stdlib.h>
f01de965 21
b5efdb8a 22#include "alloc-util.h"
f01de965 23#include "bus-label.h"
e4e73a63 24#include "hexdecoct.h"
b5efdb8a 25#include "macro.h"
f01de965
KS
26
27char *bus_label_escape(const char *s) {
28 char *r, *t;
29 const char *f;
30
31 assert_return(s, NULL);
32
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. */
36
37 if (*s == 0)
38 return strdup("_");
39
40 r = new(char, strlen(s)*3 + 1);
41 if (!r)
42 return NULL;
43
44 for (f = s, t = r; *f; f++) {
45
46 /* Escape everything that is not a-zA-Z0-9. We also
47 * escape 0-9 if it's the first character */
48
49 if (!(*f >= 'A' && *f <= 'Z') &&
50 !(*f >= 'a' && *f <= 'z') &&
51 !(f > s && *f >= '0' && *f <= '9')) {
52 *(t++) = '_';
53 *(t++) = hexchar(*f >> 4);
54 *(t++) = hexchar(*f);
55 } else
56 *(t++) = *f;
57 }
58
59 *t = 0;
60
61 return r;
62}
63
ad922737 64char *bus_label_unescape_n(const char *f, size_t l) {
f01de965 65 char *r, *t;
ad922737 66 size_t i;
f01de965
KS
67
68 assert_return(f, NULL);
69
70 /* Special case for the empty string */
ad922737 71 if (l == 1 && *f == '_')
f01de965
KS
72 return strdup("");
73
ad922737 74 r = new(char, l + 1);
f01de965
KS
75 if (!r)
76 return NULL;
77
ad922737
DH
78 for (i = 0, t = r; i < l; ++i) {
79 if (f[i] == '_') {
f01de965
KS
80 int a, b;
81
ad922737
DH
82 if (l - i < 3 ||
83 (a = unhexchar(f[i + 1])) < 0 ||
84 (b = unhexchar(f[i + 2])) < 0) {
f01de965
KS
85 /* Invalid escape code, let's take it literal then */
86 *(t++) = '_';
87 } else {
88 *(t++) = (char) ((a << 4) | b);
ad922737 89 i += 2;
f01de965
KS
90 }
91 } else
ad922737 92 *(t++) = f[i];
f01de965
KS
93 }
94
95 *t = 0;
96
97 return r;
98}