chiark / gitweb /
bus: parse uid/gid/pid/tid meta data from kdbus messages
[elogind.git] / src / libsystemd-bus / bus-type.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 #include "bus-type.h"
24
25 bool bus_type_is_valid(char c) {
26         static const char valid[] = {
27                 SD_BUS_TYPE_BYTE,
28                 SD_BUS_TYPE_BOOLEAN,
29                 SD_BUS_TYPE_INT16,
30                 SD_BUS_TYPE_UINT16,
31                 SD_BUS_TYPE_INT32,
32                 SD_BUS_TYPE_UINT32,
33                 SD_BUS_TYPE_INT64,
34                 SD_BUS_TYPE_UINT64,
35                 SD_BUS_TYPE_DOUBLE,
36                 SD_BUS_TYPE_STRING,
37                 SD_BUS_TYPE_OBJECT_PATH,
38                 SD_BUS_TYPE_SIGNATURE,
39                 SD_BUS_TYPE_ARRAY,
40                 SD_BUS_TYPE_VARIANT,
41                 SD_BUS_TYPE_STRUCT,
42                 SD_BUS_TYPE_DICT_ENTRY,
43                 SD_BUS_TYPE_UNIX_FD
44         };
45
46         return !!memchr(valid, c, sizeof(valid));
47 }
48
49 bool bus_type_is_valid_in_signature(char c) {
50         static const char valid[] = {
51                 SD_BUS_TYPE_BYTE,
52                 SD_BUS_TYPE_BOOLEAN,
53                 SD_BUS_TYPE_INT16,
54                 SD_BUS_TYPE_UINT16,
55                 SD_BUS_TYPE_INT32,
56                 SD_BUS_TYPE_UINT32,
57                 SD_BUS_TYPE_INT64,
58                 SD_BUS_TYPE_UINT64,
59                 SD_BUS_TYPE_DOUBLE,
60                 SD_BUS_TYPE_STRING,
61                 SD_BUS_TYPE_OBJECT_PATH,
62                 SD_BUS_TYPE_SIGNATURE,
63                 SD_BUS_TYPE_ARRAY,
64                 SD_BUS_TYPE_VARIANT,
65                 SD_BUS_TYPE_STRUCT_BEGIN,
66                 SD_BUS_TYPE_STRUCT_END,
67                 SD_BUS_TYPE_DICT_ENTRY_BEGIN,
68                 SD_BUS_TYPE_DICT_ENTRY_END,
69                 SD_BUS_TYPE_UNIX_FD
70         };
71
72         return !!memchr(valid, c, sizeof(valid));
73 }
74
75 bool bus_type_is_basic(char c) {
76         static const char valid[] = {
77                 SD_BUS_TYPE_BYTE,
78                 SD_BUS_TYPE_BOOLEAN,
79                 SD_BUS_TYPE_INT16,
80                 SD_BUS_TYPE_UINT16,
81                 SD_BUS_TYPE_INT32,
82                 SD_BUS_TYPE_UINT32,
83                 SD_BUS_TYPE_INT64,
84                 SD_BUS_TYPE_UINT64,
85                 SD_BUS_TYPE_DOUBLE,
86                 SD_BUS_TYPE_STRING,
87                 SD_BUS_TYPE_OBJECT_PATH,
88                 SD_BUS_TYPE_SIGNATURE,
89                 SD_BUS_TYPE_UNIX_FD
90         };
91
92         return !!memchr(valid, c, sizeof(valid));
93 }
94
95 bool bus_type_is_container(char c) {
96         static const char valid[] = {
97                 SD_BUS_TYPE_ARRAY,
98                 SD_BUS_TYPE_VARIANT,
99                 SD_BUS_TYPE_STRUCT,
100                 SD_BUS_TYPE_DICT_ENTRY
101         };
102
103         return !!memchr(valid, c, sizeof(valid));
104 }
105
106 int bus_type_get_alignment(char c) {
107
108         switch (c) {
109         case SD_BUS_TYPE_BYTE:
110         case SD_BUS_TYPE_SIGNATURE:
111         case SD_BUS_TYPE_VARIANT:
112                 return 1;
113
114         case SD_BUS_TYPE_INT16:
115         case SD_BUS_TYPE_UINT16:
116                 return 2;
117
118         case SD_BUS_TYPE_BOOLEAN:
119         case SD_BUS_TYPE_INT32:
120         case SD_BUS_TYPE_UINT32:
121         case SD_BUS_TYPE_STRING:
122         case SD_BUS_TYPE_OBJECT_PATH:
123         case SD_BUS_TYPE_ARRAY:
124         case SD_BUS_TYPE_UNIX_FD:
125                 return 4;
126
127         case SD_BUS_TYPE_INT64:
128         case SD_BUS_TYPE_UINT64:
129         case SD_BUS_TYPE_DOUBLE:
130         case SD_BUS_TYPE_STRUCT:
131         case SD_BUS_TYPE_STRUCT_BEGIN:
132         case SD_BUS_TYPE_DICT_ENTRY:
133         case SD_BUS_TYPE_DICT_ENTRY_BEGIN:
134                 return 8;
135         }
136
137         return -EINVAL;
138 }
139
140 int bus_type_get_size(char c) {
141
142         switch (c) {
143         case SD_BUS_TYPE_BYTE:
144                 return 1;
145
146         case SD_BUS_TYPE_INT16:
147         case SD_BUS_TYPE_UINT16:
148                 return 2;
149
150         case SD_BUS_TYPE_BOOLEAN:
151         case SD_BUS_TYPE_INT32:
152         case SD_BUS_TYPE_UINT32:
153         case SD_BUS_TYPE_UNIX_FD:
154                 return 4;
155
156         case SD_BUS_TYPE_INT64:
157         case SD_BUS_TYPE_UINT64:
158         case SD_BUS_TYPE_DOUBLE:
159                 return 8;
160         }
161
162         return -EINVAL;
163 }