chiark / gitweb /
bus: add basic implementation of a native bus client library
[elogind.git] / src / libsystemd-bus / bus-signature.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 <util.h>
23
24 #include "bus-signature.h"
25 #include "bus-type.h"
26
27 static int signature_element_length_internal(
28                 const char *s,
29                 bool allow_dict_entry,
30                 size_t *l) {
31
32         int r;
33
34         assert(s);
35
36         if (bus_type_is_basic(*s) || *s == SD_BUS_TYPE_VARIANT) {
37                 *l = 1;
38                 return 0;
39         }
40
41         if (*s == SD_BUS_TYPE_ARRAY) {
42                 size_t t;
43
44                 r = signature_element_length_internal(s + 1, true, &t);
45                 if (r < 0)
46                         return r;
47
48                 *l = t + 1;
49                 return 0;
50         }
51
52         if (*s == SD_BUS_TYPE_STRUCT_BEGIN) {
53                 const char *p = s + 1;
54
55                 while (*p != SD_BUS_TYPE_STRUCT_END) {
56                         size_t t;
57
58                         r = signature_element_length_internal(p, false, &t);
59                         if (r < 0)
60                                 return r;
61
62                         p += t;
63                 }
64
65                 *l = p - s + 1;
66                 return 0;
67         }
68
69         if (*s == SD_BUS_TYPE_DICT_ENTRY_BEGIN && allow_dict_entry) {
70                 const char *p = s + 1;
71                 unsigned n = 0;
72
73                 while (*p != SD_BUS_TYPE_DICT_ENTRY_END) {
74                         size_t t;
75
76                         if (n == 0 && !bus_type_is_basic(*p))
77                                 return -EINVAL;
78
79                         r = signature_element_length_internal(p, false, &t);
80                         if (r < 0)
81                                 return r;
82
83                         p += t;
84                         n++;
85                 }
86
87                 if (n != 2)
88                         return -EINVAL;
89
90                 *l = p - s + 1;
91                 return 0;
92         }
93
94         return -EINVAL;
95 }
96
97 bool signature_is_single(const char *s) {
98         int r;
99         size_t t;
100
101         assert(s);
102
103         r = signature_element_length(s, &t);
104         if (r < 0)
105                 return false;
106
107         return s[t] == 0;
108 }
109
110 bool signature_is_pair(const char *s) {
111         assert(s);
112
113         if (!bus_type_is_basic(*s))
114                 return false;
115
116         return signature_is_single(s + 1);
117 }
118
119 bool signature_is_valid(const char *s, bool allow_dict_entry) {
120         const char *p;
121         int r;
122
123         assert(s);
124
125         p = s;
126         while (*p) {
127                 size_t t;
128
129                 r = signature_element_length_internal(p, allow_dict_entry, &t);
130                 if (r < 0)
131                         return false;
132
133                 p += t;
134         }
135
136         return p - s <= 255;
137 }
138
139 int signature_element_length(const char *s, size_t *l) {
140         return signature_element_length_internal(s, true, l);
141 }