D-Bus: dbus-signature.c Source File

D-Bus 1.13.18

dbus-signature.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-signature.c Routines for reading recursive type signatures
3 *
4 * Copyright (C) 2005 Red Hat, Inc.
5 *
6 * Licensed under the Academic Free License version 2.1
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24 #include <config.h>
25
26 #include "dbus-signature.h"
27 #include "dbus-marshal-recursive.h"
28 #include "dbus-marshal-basic.h"
29 #include "dbus-internals.h"
30 #include "dbus-test.h"
31 #include <dbus/dbus-test-tap.h>
32
36 typedef struct
37 {
38 const char *pos;
39 unsigned int finished : 1;
40 unsigned int in_array : 1;
41 } DBusSignatureRealIter;
42
43 _DBUS_STATIC_ASSERT (sizeof (DBusSignatureIter) >= sizeof (DBusSignatureRealIter));
44
46 #define TYPE_IS_CONTAINER(typecode) \
47 ((typecode) == DBUS_TYPE_STRUCT || \
48 (typecode) == DBUS_TYPE_DICT_ENTRY || \
49 (typecode) == DBUS_TYPE_VARIANT || \
50 (typecode) == DBUS_TYPE_ARRAY)
51
52
69 void
70 dbus_signature_iter_init (DBusSignatureIter *iter,
71 const char *signature)
72 {
73 DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
74
75 real_iter->pos = signature;
76 real_iter->finished = FALSE;
77 real_iter->in_array = FALSE;
78 }
79
94 int
95 dbus_signature_iter_get_current_type (const DBusSignatureIter *iter)
96 {
97 DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
98
99 return _dbus_first_type_in_signature_c_str (real_iter->pos, 0);
100 }
101
114 char *
115 dbus_signature_iter_get_signature (const DBusSignatureIter *iter)
116 {
117 DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
118 DBusString str;
119 char *ret;
120 int pos;
121
122 if (!_dbus_string_init (&str))
123 return NULL;
124
125 pos = 0;
126 _dbus_type_signature_next (real_iter->pos, &pos);
127
128 if (!_dbus_string_append_len (&str, real_iter->pos, pos))
129 return NULL;
130 if (!_dbus_string_steal_data (&str, &ret))
131 ret = NULL;
132 _dbus_string_free (&str);
133
134 return ret;
135 }
136
148 int
149 dbus_signature_iter_get_element_type (const DBusSignatureIter *iter)
150 {
151 DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
152
153 _dbus_return_val_if_fail (dbus_signature_iter_get_current_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
154
155 return _dbus_first_type_in_signature_c_str (real_iter->pos, 1);
156 }
157
166 dbus_bool_t
167 dbus_signature_iter_next (DBusSignatureIter *iter)
168 {
169 DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
170
171 if (real_iter->finished)
172 return FALSE;
173 else
174 {
175 int pos;
176
177 if (real_iter->in_array)
178 {
179 real_iter->finished = TRUE;
180 return FALSE;
181 }
182
183 pos = 0;
184 _dbus_type_signature_next (real_iter->pos, &pos);
185 real_iter->pos += pos;
186
187 if (*real_iter->pos == DBUS_STRUCT_END_CHAR
188 || *real_iter->pos == DBUS_DICT_ENTRY_END_CHAR)
189 {
190 real_iter->finished = TRUE;
191 return FALSE;
192 }
193
194 return *real_iter->pos != DBUS_TYPE_INVALID;
195 }
196 }
197
209 void
210 dbus_signature_iter_recurse (const DBusSignatureIter *iter,
211 DBusSignatureIter *subiter)
212 {
213 DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
214 DBusSignatureRealIter *real_sub_iter = (DBusSignatureRealIter *) subiter;
215
216 _dbus_return_if_fail (dbus_type_is_container (dbus_signature_iter_get_current_type (iter)));
217
218 *real_sub_iter = *real_iter;
219 real_sub_iter->in_array = FALSE;
220 real_sub_iter->pos++;
221
222 if (dbus_signature_iter_get_current_type (iter) == DBUS_TYPE_ARRAY)
223 real_sub_iter->in_array = TRUE;
224 }
225
235 dbus_bool_t
236 dbus_signature_validate (const char *signature,
237 DBusError *error)
238
239 {
240 DBusString str;
241 DBusValidity reason;
242
243 _dbus_string_init_const (&str, signature);
244 reason = _dbus_validate_signature_with_reason (&str, 0, _dbus_string_get_length (&str));
245
246 if (reason == DBUS_VALID)
247 return TRUE;
248 else
249 {
250 dbus_set_error (error, DBUS_ERROR_INVALID_SIGNATURE, "%s",
251 _dbus_validity_to_error_message (reason));
252 return FALSE;
253 }
254 }
255
267 dbus_bool_t
268 dbus_signature_validate_single (const char *signature,
269 DBusError *error)
270 {
271 DBusSignatureIter iter;
272
273 if (!dbus_signature_validate (signature, error))
274 return FALSE;
275
276 dbus_signature_iter_init (&iter, signature);
277 if (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_INVALID)
278 goto lose;
279 if (!dbus_signature_iter_next (&iter))
280 return TRUE;
281 lose:
282 dbus_set_error (error, DBUS_ERROR_INVALID_SIGNATURE, "Exactly one complete type required in signature");
283 return FALSE;
284 }
285
297 dbus_bool_t
298 dbus_type_is_container (int typecode)
299 {
300 /* only reasonable (non-line-noise) typecodes are allowed */
301 _dbus_return_val_if_fail (dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
302 FALSE);
303 return TYPE_IS_CONTAINER (typecode);
304 }
305
321 dbus_bool_t
322 dbus_type_is_basic (int typecode)
323 {
324 /* only reasonable (non-line-noise) typecodes are allowed */
325 _dbus_return_val_if_fail (dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
326 FALSE);
327
328 /* everything that isn't invalid or a container */
329 return !(typecode == DBUS_TYPE_INVALID || TYPE_IS_CONTAINER (typecode));
330 }
331
352 dbus_bool_t
353 dbus_type_is_fixed (int typecode)
354 {
355 /* only reasonable (non-line-noise) typecodes are allowed */
356 _dbus_return_val_if_fail (dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
357 FALSE);
358
359 switch (typecode)
360 {
361 case DBUS_TYPE_BYTE:
362 case DBUS_TYPE_BOOLEAN:
363 case DBUS_TYPE_INT16:
364 case DBUS_TYPE_UINT16:
365 case DBUS_TYPE_INT32:
366 case DBUS_TYPE_UINT32:
367 case DBUS_TYPE_INT64:
368 case DBUS_TYPE_UINT64:
369 case DBUS_TYPE_DOUBLE:
370 case DBUS_TYPE_UNIX_FD:
371 return TRUE;
372 default:
373 return FALSE;
374 }
375 }
376
386 dbus_bool_t
387 dbus_type_is_valid (int typecode)
388 {
389 switch (typecode)
390 {
391 case DBUS_TYPE_BYTE:
392 case DBUS_TYPE_BOOLEAN:
393 case DBUS_TYPE_INT16:
394 case DBUS_TYPE_UINT16:
395 case DBUS_TYPE_INT32:
396 case DBUS_TYPE_UINT32:
397 case DBUS_TYPE_INT64:
398 case DBUS_TYPE_UINT64:
399 case DBUS_TYPE_DOUBLE:
400 case DBUS_TYPE_STRING:
401 case DBUS_TYPE_OBJECT_PATH:
402 case DBUS_TYPE_SIGNATURE:
403 case DBUS_TYPE_ARRAY:
404 case DBUS_TYPE_STRUCT:
405 case DBUS_TYPE_DICT_ENTRY:
406 case DBUS_TYPE_VARIANT:
407 case DBUS_TYPE_UNIX_FD:
408 return TRUE;
409
410 default:
411 return FALSE;
412 }
413 }
414 /* end of DBusSignature group */
DBUS_TYPE_ARRAY
#define DBUS_TYPE_ARRAY
Type code marking a D-Bus array type.
Definition: dbus-protocol.h:120
_dbus_type_signature_next
void _dbus_type_signature_next(const char *type_str, int *type_pos)
Skips to the next "complete" type inside a type signature.
Definition: dbus-marshal-recursive.c:341
DBUS_TYPE_INT64
#define DBUS_TYPE_INT64
Type code marking a 64-bit signed integer.
Definition: dbus-protocol.h:90
DBUS_TYPE_STRING
#define DBUS_TYPE_STRING
Type code marking a UTF-8 encoded, nul-terminated Unicode string.
Definition: dbus-protocol.h:102
DBUS_TYPE_INVALID
#define DBUS_TYPE_INVALID
Type code that is never equal to a legitimate type code.
Definition: dbus-protocol.h:60
DBUS_VALID
@ DBUS_VALID
the data is valid
Definition: dbus-marshal-validate.h:58
DBusSignatureRealIter::pos
const char * pos
current position in the signature string
Definition: dbus-signature.c:38
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:271
DBusSignatureRealIter::finished
unsigned int finished
true if we are at the end iter
Definition: dbus-signature.c:39
dbus_signature_validate_single
dbus_bool_t dbus_signature_validate_single(const char *signature, DBusError *error)
Check that a type signature is both valid and contains exactly one complete type.
Definition: dbus-signature.c:268
DBUS_TYPE_UNIX_FD
#define DBUS_TYPE_UNIX_FD
Type code marking a unix file descriptor.
Definition: dbus-protocol.h:114
DBUS_TYPE_INT32
#define DBUS_TYPE_INT32
Type code marking a 32-bit signed integer.
Definition: dbus-protocol.h:82
dbus_signature_iter_next
dbus_bool_t dbus_signature_iter_next(DBusSignatureIter *iter)
Skip to the next value on this "level".
Definition: dbus-signature.c:167
_dbus_first_type_in_signature_c_str
int _dbus_first_type_in_signature_c_str(const char *str, int pos)
Similar to _dbus_first_type_in_signature, but operates on a C string buffer.
Definition: dbus-marshal-basic.c:1492
_dbus_string_append_len
dbus_bool_t _dbus_string_append_len(DBusString *str, const char *buffer, int len)
Appends block of bytes with the given length to a DBusString.
Definition: dbus-string.c:1161
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
TRUE
#define TRUE
dbus_signature_iter_recurse
void dbus_signature_iter_recurse(const DBusSignatureIter *iter, DBusSignatureIter *subiter)
Initialize a new iterator pointing to the first type in the current container.
Definition: dbus-signature.c:210
DBusSignatureRealIter
Implementation details of DBusSignatureIter, all fields are private.
Definition: dbus-signature.c:37
DBUS_TYPE_SIGNATURE
#define DBUS_TYPE_SIGNATURE
Type code marking a D-Bus type signature.
Definition: dbus-protocol.h:110
DBUS_TYPE_UINT16
#define DBUS_TYPE_UINT16
Type code marking a 16-bit unsigned integer.
Definition: dbus-protocol.h:78
dbus_signature_iter_get_current_type
int dbus_signature_iter_get_current_type(const DBusSignatureIter *iter)
Returns the current type pointed to by the iterator.
Definition: dbus-signature.c:95
DBUS_STRUCT_END_CHAR
#define DBUS_STRUCT_END_CHAR
Code marking the end of a struct type in a type signature.
Definition: dbus-protocol.h:160
DBusString
Definition: dbus-string.h:43
DBUS_TYPE_STRUCT
#define DBUS_TYPE_STRUCT
STRUCT and DICT_ENTRY are sort of special since their codes can't appear in a type string,...
Definition: dbus-protocol.h:136
DBUS_TYPE_BYTE
#define DBUS_TYPE_BYTE
Type code marking an 8-bit unsigned integer.
Definition: dbus-protocol.h:66
DBUS_TYPE_DICT_ENTRY
#define DBUS_TYPE_DICT_ENTRY
Type code used to represent a dict entry; however, this type code does not appear in type signatures,...
Definition: dbus-protocol.h:143
FALSE
#define FALSE
DBUS_TYPE_INT16
#define DBUS_TYPE_INT16
Type code marking a 16-bit signed integer.
Definition: dbus-protocol.h:74
DBUS_TYPE_DOUBLE
#define DBUS_TYPE_DOUBLE
Type code marking an 8-byte double in IEEE 754 format.
Definition: dbus-protocol.h:98
dbus_type_is_container
dbus_bool_t dbus_type_is_container(int typecode)
A "container type" can contain basic types, or nested container types.
Definition: dbus-signature.c:298
DBUS_TYPE_UINT64
#define DBUS_TYPE_UINT64
Type code marking a 64-bit unsigned integer.
Definition: dbus-protocol.h:94
dbus_type_is_fixed
dbus_bool_t dbus_type_is_fixed(int typecode)
Tells you whether values of this type can change length if you set them to some other value.
Definition: dbus-signature.c:353
dbus_signature_iter_get_element_type
int dbus_signature_iter_get_element_type(const DBusSignatureIter *iter)
Convenience function for returning the element type of an array; This function allows you to avoid in...
Definition: dbus-signature.c:149
DBusSignatureRealIter::in_array
unsigned int in_array
true if we are a subiterator pointing to an array's element type
Definition: dbus-signature.c:40
DBUS_DICT_ENTRY_END_CHAR
#define DBUS_DICT_ENTRY_END_CHAR
Code marking the end of a dict entry type in a type signature.
Definition: dbus-protocol.h:168
dbus_signature_iter_get_signature
char * dbus_signature_iter_get_signature(const DBusSignatureIter *iter)
Returns the signature of the single complete type starting at the given iterator.
Definition: dbus-signature.c:115
DBUS_TYPE_BOOLEAN
#define DBUS_TYPE_BOOLEAN
Type code marking a boolean.
Definition: dbus-protocol.h:70
DBusSignatureIter
DBusSignatureIter struct; contains no public fields.
Definition: dbus-signature.h:45
dbus_type_is_valid
dbus_bool_t dbus_type_is_valid(int typecode)
Return TRUE if the argument is a valid typecode.
Definition: dbus-signature.c:387
DBUS_TYPE_OBJECT_PATH
#define DBUS_TYPE_OBJECT_PATH
Type code marking a D-Bus object path.
Definition: dbus-protocol.h:106
DBUS_TYPE_VARIANT
#define DBUS_TYPE_VARIANT
Type code marking a D-Bus variant type.
Definition: dbus-protocol.h:124
DBusError
Object representing an exception.
Definition: dbus-errors.h:49
dbus_signature_validate
dbus_bool_t dbus_signature_validate(const char *signature, DBusError *error)
Check a type signature for validity.
Definition: dbus-signature.c:236
_dbus_validate_signature_with_reason
DBusValidity _dbus_validate_signature_with_reason(const DBusString *type_str, int type_pos, int len)
Verifies that the range of type_str from type_pos to type_end is a valid signature.
Definition: dbus-marshal-validate.c:51
dbus_set_error
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
dbus_type_is_basic
dbus_bool_t dbus_type_is_basic(int typecode)
A "basic type" is a somewhat arbitrary concept, but the intent is to include those types that are ful...
Definition: dbus-signature.c:322
_dbus_string_steal_data
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
Definition: dbus-string.c:665
DBUS_ERROR_INVALID_SIGNATURE
#define DBUS_ERROR_INVALID_SIGNATURE
A type signature is not valid.
Definition: dbus-protocol.h:446
_dbus_string_init_const
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:197
DBUS_TYPE_UINT32
#define DBUS_TYPE_UINT32
Type code marking a 32-bit unsigned integer.
Definition: dbus-protocol.h:86
dbus_signature_iter_init
void dbus_signature_iter_init(DBusSignatureIter *iter, const char *signature)
Initializes a DBusSignatureIter for reading a type signature.
Definition: dbus-signature.c:70
dbus_bool_t
dbus_uint32_t dbus_bool_t
Definition: dbus-types.h:35
DBusValidity
DBusValidity
This is primarily used in unit testing, so we can verify that each invalid message is invalid for the...
Definition: dbus-marshal-validate.h:52
NULL
#define NULL