chiark / gitweb /
Get rid of some more unused defines and dirs
[elogind.git] / src / shared / bus-label.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <stdlib.h>
23
24 #include "util.h"
25 #include "macro.h"
26
27 #include "bus-label.h"
28
29 char *bus_label_escape(const char *s) {
30         char *r, *t;
31         const char *f;
32
33         assert_return(s, NULL);
34
35         /* Escapes all chars that D-Bus' object path cannot deal
36          * with. Can be reversed with bus_path_unescape(). We special
37          * case the empty string. */
38
39         if (*s == 0)
40                 return strdup("_");
41
42         r = new(char, strlen(s)*3 + 1);
43         if (!r)
44                 return NULL;
45
46         for (f = s, t = r; *f; f++) {
47
48                 /* Escape everything that is not a-zA-Z0-9. We also
49                  * escape 0-9 if it's the first character */
50
51                 if (!(*f >= 'A' && *f <= 'Z') &&
52                     !(*f >= 'a' && *f <= 'z') &&
53                     !(f > s && *f >= '0' && *f <= '9')) {
54                         *(t++) = '_';
55                         *(t++) = hexchar(*f >> 4);
56                         *(t++) = hexchar(*f);
57                 } else
58                         *(t++) = *f;
59         }
60
61         *t = 0;
62
63         return r;
64 }
65
66 char *bus_label_unescape(const char *f) {
67         char *r, *t;
68
69         assert_return(f, NULL);
70
71         /* Special case for the empty string */
72         if (streq(f, "_"))
73                 return strdup("");
74
75         r = new(char, strlen(f) + 1);
76         if (!r)
77                 return NULL;
78
79         for (t = r; *f; f++) {
80
81                 if (*f == '_') {
82                         int a, b;
83
84                         if ((a = unhexchar(f[1])) < 0 ||
85                             (b = unhexchar(f[2])) < 0) {
86                                 /* Invalid escape code, let's take it literal then */
87                                 *(t++) = '_';
88                         } else {
89                                 *(t++) = (char) ((a << 4) | b);
90                                 f += 2;
91                         }
92                 } else
93                         *(t++) = *f;
94         }
95
96         *t = 0;
97
98         return r;
99 }