chiark / gitweb /
path-util: Add hidden suffixes for ucf (#3131)
[elogind.git] / src / basic / bus-label.c
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
20 #include <stdlib.h>
21
22 #include "alloc-util.h"
23 #include "bus-label.h"
24 #include "hexdecoct.h"
25 #include "macro.h"
26
27 char *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
64 char *bus_label_unescape_n(const char *f, size_t l) {
65         char *r, *t;
66         size_t i;
67
68         assert_return(f, NULL);
69
70         /* Special case for the empty string */
71         if (l == 1 && *f == '_')
72                 return strdup("");
73
74         r = new(char, l + 1);
75         if (!r)
76                 return NULL;
77
78         for (i = 0, t = r; i < l; ++i) {
79                 if (f[i] == '_') {
80                         int a, b;
81
82                         if (l - i < 3 ||
83                             (a = unhexchar(f[i + 1])) < 0 ||
84                             (b = unhexchar(f[i + 2])) < 0) {
85                                 /* Invalid escape code, let's take it literal then */
86                                 *(t++) = '_';
87                         } else {
88                                 *(t++) = (char) ((a << 4) | b);
89                                 i += 2;
90                         }
91                 } else
92                         *(t++) = f[i];
93         }
94
95         *t = 0;
96
97         return r;
98 }