1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4
5 #include "sd-bus.h"
6
7 #include "bus-internal.h"
8 #include "bus-type.h"
9
bus_type_is_valid(char c)10 bool bus_type_is_valid(char c) {
11 static const char valid[] = {
12 SD_BUS_TYPE_BYTE,
13 SD_BUS_TYPE_BOOLEAN,
14 SD_BUS_TYPE_INT16,
15 SD_BUS_TYPE_UINT16,
16 SD_BUS_TYPE_INT32,
17 SD_BUS_TYPE_UINT32,
18 SD_BUS_TYPE_INT64,
19 SD_BUS_TYPE_UINT64,
20 SD_BUS_TYPE_DOUBLE,
21 SD_BUS_TYPE_STRING,
22 SD_BUS_TYPE_OBJECT_PATH,
23 SD_BUS_TYPE_SIGNATURE,
24 SD_BUS_TYPE_ARRAY,
25 SD_BUS_TYPE_VARIANT,
26 SD_BUS_TYPE_STRUCT,
27 SD_BUS_TYPE_DICT_ENTRY,
28 SD_BUS_TYPE_UNIX_FD
29 };
30
31 return !!memchr(valid, c, sizeof(valid));
32 }
33
bus_type_is_basic(char c)34 bool bus_type_is_basic(char c) {
35 static const char valid[] = {
36 SD_BUS_TYPE_BYTE,
37 SD_BUS_TYPE_BOOLEAN,
38 SD_BUS_TYPE_INT16,
39 SD_BUS_TYPE_UINT16,
40 SD_BUS_TYPE_INT32,
41 SD_BUS_TYPE_UINT32,
42 SD_BUS_TYPE_INT64,
43 SD_BUS_TYPE_UINT64,
44 SD_BUS_TYPE_DOUBLE,
45 SD_BUS_TYPE_STRING,
46 SD_BUS_TYPE_OBJECT_PATH,
47 SD_BUS_TYPE_SIGNATURE,
48 SD_BUS_TYPE_UNIX_FD
49 };
50
51 return !!memchr(valid, c, sizeof(valid));
52 }
53
bus_type_is_trivial(char c)54 bool bus_type_is_trivial(char c) {
55 static const char valid[] = {
56 SD_BUS_TYPE_BYTE,
57 SD_BUS_TYPE_BOOLEAN,
58 SD_BUS_TYPE_INT16,
59 SD_BUS_TYPE_UINT16,
60 SD_BUS_TYPE_INT32,
61 SD_BUS_TYPE_UINT32,
62 SD_BUS_TYPE_INT64,
63 SD_BUS_TYPE_UINT64,
64 SD_BUS_TYPE_DOUBLE
65 };
66
67 return !!memchr(valid, c, sizeof(valid));
68 }
69
bus_type_is_container(char c)70 bool bus_type_is_container(char c) {
71 static const char valid[] = {
72 SD_BUS_TYPE_ARRAY,
73 SD_BUS_TYPE_VARIANT,
74 SD_BUS_TYPE_STRUCT,
75 SD_BUS_TYPE_DICT_ENTRY
76 };
77
78 return !!memchr(valid, c, sizeof(valid));
79 }
80
bus_type_get_alignment(char c)81 int bus_type_get_alignment(char c) {
82
83 switch (c) {
84 case SD_BUS_TYPE_BYTE:
85 case SD_BUS_TYPE_SIGNATURE:
86 case SD_BUS_TYPE_VARIANT:
87 return 1;
88
89 case SD_BUS_TYPE_INT16:
90 case SD_BUS_TYPE_UINT16:
91 return 2;
92
93 case SD_BUS_TYPE_BOOLEAN:
94 case SD_BUS_TYPE_INT32:
95 case SD_BUS_TYPE_UINT32:
96 case SD_BUS_TYPE_STRING:
97 case SD_BUS_TYPE_OBJECT_PATH:
98 case SD_BUS_TYPE_ARRAY:
99 case SD_BUS_TYPE_UNIX_FD:
100 return 4;
101
102 case SD_BUS_TYPE_INT64:
103 case SD_BUS_TYPE_UINT64:
104 case SD_BUS_TYPE_DOUBLE:
105 case SD_BUS_TYPE_STRUCT:
106 case SD_BUS_TYPE_STRUCT_BEGIN:
107 case SD_BUS_TYPE_DICT_ENTRY:
108 case SD_BUS_TYPE_DICT_ENTRY_BEGIN:
109 return 8;
110 }
111
112 return -EINVAL;
113 }
114
bus_type_get_size(char c)115 int bus_type_get_size(char c) {
116
117 switch (c) {
118 case SD_BUS_TYPE_BYTE:
119 return 1;
120
121 case SD_BUS_TYPE_INT16:
122 case SD_BUS_TYPE_UINT16:
123 return 2;
124
125 case SD_BUS_TYPE_BOOLEAN:
126 case SD_BUS_TYPE_INT32:
127 case SD_BUS_TYPE_UINT32:
128 case SD_BUS_TYPE_UNIX_FD:
129 return 4;
130
131 case SD_BUS_TYPE_INT64:
132 case SD_BUS_TYPE_UINT64:
133 case SD_BUS_TYPE_DOUBLE:
134 return 8;
135 }
136
137 return -EINVAL;
138 }
139
sd_bus_interface_name_is_valid(const char * p)140 _public_ int sd_bus_interface_name_is_valid(const char *p) {
141 assert_return(p, -EINVAL);
142
143 return interface_name_is_valid(p);
144 }
145
sd_bus_service_name_is_valid(const char * p)146 _public_ int sd_bus_service_name_is_valid(const char *p) {
147 assert_return(p, -EINVAL);
148
149 return service_name_is_valid(p);
150 }
151
sd_bus_member_name_is_valid(const char * p)152 _public_ int sd_bus_member_name_is_valid(const char *p) {
153 assert_return(p, -EINVAL);
154
155 return member_name_is_valid(p);
156 }
157
sd_bus_object_path_is_valid(const char * p)158 _public_ int sd_bus_object_path_is_valid(const char *p) {
159 assert_return(p, -EINVAL);
160
161 return object_path_is_valid(p);
162 }
163