]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-bus/bus-signature.c
tests: fix size_t in format string
[thirdparty/systemd.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 unsigned array_depth,
31 unsigned struct_depth,
32 size_t *l) {
33
34 int r;
35
36 assert(s);
37
38 if (bus_type_is_basic(*s) || *s == SD_BUS_TYPE_VARIANT) {
39 *l = 1;
40 return 0;
41 }
42
43 if (*s == SD_BUS_TYPE_ARRAY) {
44 size_t t;
45
46 if (array_depth >= 32)
47 return -EINVAL;
48
49 r = signature_element_length_internal(s + 1, true, array_depth+1, struct_depth, &t);
50 if (r < 0)
51 return r;
52
53 *l = t + 1;
54 return 0;
55 }
56
57 if (*s == SD_BUS_TYPE_STRUCT_BEGIN) {
58 const char *p = s + 1;
59
60 if (struct_depth >= 32)
61 return -EINVAL;
62
63 while (*p != SD_BUS_TYPE_STRUCT_END) {
64 size_t t;
65
66 r = signature_element_length_internal(p, false, array_depth, struct_depth+1, &t);
67 if (r < 0)
68 return r;
69
70 p += t;
71 }
72
73 *l = p - s + 1;
74 return 0;
75 }
76
77 if (*s == SD_BUS_TYPE_DICT_ENTRY_BEGIN && allow_dict_entry) {
78 const char *p = s + 1;
79 unsigned n = 0;
80
81 if (struct_depth >= 32)
82 return -EINVAL;
83
84 while (*p != SD_BUS_TYPE_DICT_ENTRY_END) {
85 size_t t;
86
87 if (n == 0 && !bus_type_is_basic(*p))
88 return -EINVAL;
89
90 r = signature_element_length_internal(p, false, array_depth, struct_depth+1, &t);
91 if (r < 0)
92 return r;
93
94 p += t;
95 n++;
96 }
97
98 if (n != 2)
99 return -EINVAL;
100
101 *l = p - s + 1;
102 return 0;
103 }
104
105 return -EINVAL;
106 }
107
108
109 int signature_element_length(const char *s, size_t *l) {
110 return signature_element_length_internal(s, true, 0, 0, l);
111 }
112
113 bool signature_is_single(const char *s) {
114 int r;
115 size_t t;
116
117 assert(s);
118
119 r = signature_element_length(s, &t);
120 if (r < 0)
121 return false;
122
123 return s[t] == 0;
124 }
125
126 bool signature_is_pair(const char *s) {
127 assert(s);
128
129 if (!bus_type_is_basic(*s))
130 return false;
131
132 return signature_is_single(s + 1);
133 }
134
135 bool signature_is_valid(const char *s, bool allow_dict_entry) {
136 const char *p;
137 int r;
138
139 assert(s);
140
141 p = s;
142 while (*p) {
143 size_t t;
144
145 r = signature_element_length_internal(p, allow_dict_entry, 0, 0, &t);
146 if (r < 0)
147 return false;
148
149 p += t;
150 }
151
152 return p - s <= 255;
153 }