]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
de1c301e LP |
2 | |
3 | #include <util.h> | |
4 | ||
dccca82b LP |
5 | #include "sd-bus.h" |
6 | ||
de1c301e LP |
7 | #include "bus-signature.h" |
8 | #include "bus-type.h" | |
9 | ||
10 | static int signature_element_length_internal( | |
11 | const char *s, | |
12 | bool allow_dict_entry, | |
813a4f93 LP |
13 | unsigned array_depth, |
14 | unsigned struct_depth, | |
de1c301e LP |
15 | size_t *l) { |
16 | ||
17 | int r; | |
18 | ||
35460afc LP |
19 | if (!s) |
20 | return -EINVAL; | |
21 | ||
718db961 | 22 | assert(l); |
de1c301e LP |
23 | |
24 | if (bus_type_is_basic(*s) || *s == SD_BUS_TYPE_VARIANT) { | |
25 | *l = 1; | |
26 | return 0; | |
27 | } | |
28 | ||
29 | if (*s == SD_BUS_TYPE_ARRAY) { | |
30 | size_t t; | |
31 | ||
813a4f93 LP |
32 | if (array_depth >= 32) |
33 | return -EINVAL; | |
34 | ||
35 | r = signature_element_length_internal(s + 1, true, array_depth+1, struct_depth, &t); | |
de1c301e LP |
36 | if (r < 0) |
37 | return r; | |
38 | ||
39 | *l = t + 1; | |
40 | return 0; | |
41 | } | |
42 | ||
43 | if (*s == SD_BUS_TYPE_STRUCT_BEGIN) { | |
44 | const char *p = s + 1; | |
45 | ||
813a4f93 LP |
46 | if (struct_depth >= 32) |
47 | return -EINVAL; | |
48 | ||
de1c301e LP |
49 | while (*p != SD_BUS_TYPE_STRUCT_END) { |
50 | size_t t; | |
51 | ||
813a4f93 | 52 | r = signature_element_length_internal(p, false, array_depth, struct_depth+1, &t); |
de1c301e LP |
53 | if (r < 0) |
54 | return r; | |
55 | ||
56 | p += t; | |
57 | } | |
58 | ||
ec6bda56 ZJS |
59 | if (p - s < 2) |
60 | /* D-Bus spec: Empty structures are not allowed; there | |
61 | * must be at least one type code between the parentheses. | |
62 | */ | |
63 | return -EINVAL; | |
64 | ||
de1c301e LP |
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 | ||
813a4f93 LP |
73 | if (struct_depth >= 32) |
74 | return -EINVAL; | |
75 | ||
de1c301e LP |
76 | while (*p != SD_BUS_TYPE_DICT_ENTRY_END) { |
77 | size_t t; | |
78 | ||
79 | if (n == 0 && !bus_type_is_basic(*p)) | |
80 | return -EINVAL; | |
81 | ||
813a4f93 | 82 | r = signature_element_length_internal(p, false, array_depth, struct_depth+1, &t); |
de1c301e LP |
83 | if (r < 0) |
84 | return r; | |
85 | ||
86 | p += t; | |
87 | n++; | |
88 | } | |
89 | ||
90 | if (n != 2) | |
91 | return -EINVAL; | |
92 | ||
93 | *l = p - s + 1; | |
94 | return 0; | |
95 | } | |
96 | ||
97 | return -EINVAL; | |
98 | } | |
99 | ||
813a4f93 LP |
100 | int signature_element_length(const char *s, size_t *l) { |
101 | return signature_element_length_internal(s, true, 0, 0, l); | |
102 | } | |
103 | ||
29ddb38f | 104 | bool signature_is_single(const char *s, bool allow_dict_entry) { |
de1c301e LP |
105 | int r; |
106 | size_t t; | |
107 | ||
35460afc LP |
108 | if (!s) |
109 | return false; | |
de1c301e | 110 | |
29ddb38f | 111 | r = signature_element_length_internal(s, allow_dict_entry, 0, 0, &t); |
de1c301e LP |
112 | if (r < 0) |
113 | return false; | |
114 | ||
115 | return s[t] == 0; | |
116 | } | |
117 | ||
118 | bool signature_is_pair(const char *s) { | |
718db961 | 119 | |
35460afc LP |
120 | if (!s) |
121 | return false; | |
de1c301e LP |
122 | |
123 | if (!bus_type_is_basic(*s)) | |
124 | return false; | |
125 | ||
29ddb38f | 126 | return signature_is_single(s + 1, false); |
de1c301e LP |
127 | } |
128 | ||
129 | bool signature_is_valid(const char *s, bool allow_dict_entry) { | |
130 | const char *p; | |
131 | int r; | |
132 | ||
35460afc LP |
133 | if (!s) |
134 | return false; | |
de1c301e LP |
135 | |
136 | p = s; | |
137 | while (*p) { | |
138 | size_t t; | |
139 | ||
813a4f93 | 140 | r = signature_element_length_internal(p, allow_dict_entry, 0, 0, &t); |
de1c301e LP |
141 | if (r < 0) |
142 | return false; | |
143 | ||
144 | p += t; | |
145 | } | |
146 | ||
147 | return p - s <= 255; | |
148 | } |