chiark / gitweb /
fee38b5055cec95931f578431dfc5a0592de8b02
[elogind.git] / src / basic / bus-label.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2013 Lennart Poettering
6 ***/
7
8 #include <stdlib.h>
9
10 #include "alloc-util.h"
11 #include "bus-label.h"
12 #include "hexdecoct.h"
13 #include "macro.h"
14
15 char *bus_label_escape(const char *s) {
16         char *r, *t;
17         const char *f;
18
19         assert_return(s, NULL);
20
21         /* Escapes all chars that D-Bus' object path cannot deal
22          * with. Can be reversed with bus_path_unescape(). We special
23          * case the empty string. */
24
25         if (*s == 0)
26                 return strdup("_");
27
28         r = new(char, strlen(s)*3 + 1);
29         if (!r)
30                 return NULL;
31
32         for (f = s, t = r; *f; f++) {
33
34                 /* Escape everything that is not a-zA-Z0-9. We also
35                  * escape 0-9 if it's the first character */
36
37                 if (!(*f >= 'A' && *f <= 'Z') &&
38                     !(*f >= 'a' && *f <= 'z') &&
39                     !(f > s && *f >= '0' && *f <= '9')) {
40                         *(t++) = '_';
41                         *(t++) = hexchar(*f >> 4);
42                         *(t++) = hexchar(*f);
43                 } else
44                         *(t++) = *f;
45         }
46
47         *t = 0;
48
49         return r;
50 }
51
52 char *bus_label_unescape_n(const char *f, size_t l) {
53         char *r, *t;
54         size_t i;
55
56         assert_return(f, NULL);
57
58         /* Special case for the empty string */
59         if (l == 1 && *f == '_')
60                 return strdup("");
61
62         r = new(char, l + 1);
63         if (!r)
64                 return NULL;
65
66         for (i = 0, t = r; i < l; ++i) {
67                 if (f[i] == '_') {
68                         int a, b;
69
70                         if (l - i < 3 ||
71                             (a = unhexchar(f[i + 1])) < 0 ||
72                             (b = unhexchar(f[i + 2])) < 0) {
73                                 /* Invalid escape code, let's take it literal then */
74                                 *(t++) = '_';
75                         } else {
76                                 *(t++) = (char) ((a << 4) | b);
77                                 i += 2;
78                         }
79                 } else
80                         *(t++) = f[i];
81         }
82
83         *t = 0;
84
85         return r;
86 }