1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-signature.c Routines for reading recursive type signatures
4 * Copyright (C) 2005 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-signature.h"
25 #include "dbus-marshal-recursive.h"
26 #include "dbus-marshal-basic.h"
27 #include "dbus-internals.h"
28 #include "dbus-test.h"
31 * Implementation details of #DBusSignatureIter, all fields are private
35 const char *pos; /**< current position in the signature string */
36 unsigned int finished : 1; /**< true if we are at the end iter */
37 unsigned int in_array : 1; /**< true if we are a subiterator pointing to an array's element type */
38 } DBusSignatureRealIter;
41 * @defgroup DBusSignature Type signature parsing
43 * @brief Parsing D-Bus type signatures
48 * Initializes a #DBusSignatureIter for reading a type signature. This
49 * function is not safe to use on invalid signatures; be sure to
50 * validate potentially invalid signatures with dbus_signature_validate
51 * before using this function.
53 * @param iter pointer to an iterator to initialize
54 * @param signature the type signature
57 dbus_signature_iter_init (DBusSignatureIter *iter,
58 const char *signature)
60 DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
62 real_iter->pos = signature;
63 real_iter->finished = FALSE;
64 real_iter->in_array = FALSE;
68 * Returns the current type pointed to by the iterator.
69 * If the iterator is pointing at a type code such as 's', then
70 * it will be returned directly.
72 * However, when the parser encounters a container type start
73 * character such as '(' for a structure, the corresponding type for
74 * the container will be returned, e.g. DBUS_TYPE_STRUCT, not '('.
75 * In this case, you should initialize a sub-iterator with
76 * dbus_signature_iter_recurse to parse the container type.
78 * @param iter pointer to an iterator
79 * @returns current type (e.g. DBUS_TYPE_STRING, DBUS_TYPE_ARRAY)
82 dbus_signature_iter_get_current_type (const DBusSignatureIter *iter)
84 DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
86 return _dbus_first_type_in_signature_c_str (real_iter->pos, 0);
90 * Returns the full type signature represented by the current
91 * iterator as a C string.
93 * @param iter pointer to an iterator
94 * @returns current signature; or NULL on OOM. Should be freed with #dbus_free
97 dbus_signature_iter_get_signature (const DBusSignatureIter *iter)
99 DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
104 if (!_dbus_string_init (&str))
108 _dbus_type_signature_next (real_iter->pos, &pos);
110 if (!_dbus_string_append_len (&str, real_iter->pos, pos))
112 if (!_dbus_string_steal_data (&str, &ret))
114 _dbus_string_free (&str);
120 * Convenience function for returning the element type of an array;
121 * This function allows you to avoid initializing a sub-iterator and
122 * getting its current type.
124 * It is an error to invoke this function if the current type of the
125 * iterator is not DBUS_TYPE_ARRAY.
127 * @param iter pointer to an iterator
128 * @returns current array element type
131 dbus_signature_iter_get_element_type (const DBusSignatureIter *iter)
133 DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
135 _dbus_return_val_if_fail (dbus_signature_iter_get_current_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
137 return _dbus_first_type_in_signature_c_str (real_iter->pos, 1);
141 * Skip to the next value on this "level". e.g. the next field in a
142 * struct, the next value in an array. Returns FALSE at the end of the
145 * @param iter the iterator
146 * @returns FALSE if nothing more to read at or below this level
149 dbus_signature_iter_next (DBusSignatureIter *iter)
151 DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
153 if (real_iter->finished)
159 if (real_iter->in_array)
161 real_iter->finished = TRUE;
166 _dbus_type_signature_next (real_iter->pos, &pos);
167 real_iter->pos += pos;
169 if (*real_iter->pos == DBUS_STRUCT_END_CHAR
170 || *real_iter->pos == DBUS_DICT_ENTRY_END_CHAR)
172 real_iter->finished = TRUE;
176 return *real_iter->pos != DBUS_TYPE_INVALID;
181 * Initialize a new iterator pointing to the first type current
182 * container. It's an error to call this if the current type is a
183 * non-container (i.e. if dbus_type_is_container returns FALSE).
185 * @param iter the current interator
186 * @param subiter an iterator to initialize pointing to the first child
189 dbus_signature_iter_recurse (const DBusSignatureIter *iter,
190 DBusSignatureIter *subiter)
192 DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
193 DBusSignatureRealIter *real_sub_iter = (DBusSignatureRealIter *) subiter;
195 _dbus_return_if_fail (dbus_type_is_container (dbus_signature_iter_get_current_type (iter)));
197 *real_sub_iter = *real_iter;
198 real_sub_iter->in_array = FALSE;
199 real_sub_iter->pos++;
201 if (dbus_signature_iter_get_current_type (iter) == DBUS_TYPE_ARRAY)
202 real_sub_iter->in_array = TRUE;
206 * Check a type signature for validity.
208 * @param signature a potentially invalid type signature
209 * @param error error return
210 * @returns TRUE iif signature is valid
213 dbus_signature_validate (const char *signature,
219 _dbus_string_init_const (&str, signature);
220 if (_dbus_validate_signature (&str, 0, _dbus_string_get_length (&str)))
222 dbus_set_error (error, DBUS_ERROR_INVALID_SIGNATURE, "Corrupt type signature");
227 * Check that a type signature is both valid and contains exactly
230 * @param signature a potentially invalid type signature
231 * @param error error return
232 * @returns TRUE iif signature is valid and has exactly one complete type
235 dbus_signature_validate_single (const char *signature,
238 DBusSignatureIter iter;
240 if (!dbus_signature_validate (signature, error))
243 dbus_signature_iter_init (&iter, signature);
244 if (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_INVALID)
246 if (!dbus_signature_iter_next (&iter))
249 dbus_set_error (error, DBUS_ERROR_INVALID_SIGNATURE, "Exactly one complete type required in signature");
253 /** macro that checks whether a typecode is a container type */
254 #define TYPE_IS_CONTAINER(typecode) \
255 ((typecode) == DBUS_TYPE_STRUCT || \
256 (typecode) == DBUS_TYPE_DICT_ENTRY || \
257 (typecode) == DBUS_TYPE_VARIANT || \
258 (typecode) == DBUS_TYPE_ARRAY)
261 * A "container type" can contain basic types, or nested
262 * container types. #DBUS_TYPE_INVALID is not a container type.
263 * This function will crash if passed a typecode that isn't
266 * @returns #TRUE if type is a container
269 dbus_type_is_container (int typecode)
271 /* only reasonable (non-line-noise) typecodes are allowed */
272 _dbus_return_val_if_fail (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
274 return TYPE_IS_CONTAINER (typecode);
278 * A "basic type" is a somewhat arbitrary concept, but the intent
279 * is to include those types that are fully-specified by a single
280 * typecode, with no additional type information or nested
281 * values. So all numbers and strings are basic types and
282 * structs, arrays, and variants are not basic types.
283 * #DBUS_TYPE_INVALID is not a basic type.
285 * This function will crash if passed a typecode that isn't
288 * @returns #TRUE if type is basic
291 dbus_type_is_basic (int typecode)
293 /* only reasonable (non-line-noise) typecodes are allowed */
294 _dbus_return_val_if_fail (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
297 /* everything that isn't invalid or a container */
298 return !(typecode == DBUS_TYPE_INVALID || TYPE_IS_CONTAINER (typecode));
302 * Tells you whether values of this type can change length if you set
303 * them to some other value. For this purpose, you assume that the
304 * first byte of the old and new value would be in the same location,
305 * so alignment padding is not a factor.
307 * This function is useful to determine whether #dbus_message_iter_get_fixed_array
310 * @returns #FALSE if the type can occupy different lengths
313 dbus_type_is_fixed (int typecode)
318 case DBUS_TYPE_BOOLEAN:
319 case DBUS_TYPE_INT16:
320 case DBUS_TYPE_UINT16:
321 case DBUS_TYPE_INT32:
322 case DBUS_TYPE_UINT32:
323 case DBUS_TYPE_INT64:
324 case DBUS_TYPE_UINT64:
325 case DBUS_TYPE_DOUBLE:
332 /** @} */ /* end of DBusSignature group */
334 #ifdef DBUS_BUILD_TESTS
337 * @ingroup DBusSignatureInternals
338 * Unit test for DBusSignature.
340 * @returns #TRUE on success.
343 _dbus_signature_test (void)
345 DBusSignatureIter iter;
346 DBusSignatureIter subiter;
347 DBusSignatureIter subsubiter;
348 DBusSignatureIter subsubsubiter;
352 _dbus_assert (sizeof (DBusSignatureIter) >= sizeof (DBusSignatureRealIter));
355 _dbus_assert (dbus_signature_validate (sig, NULL));
356 _dbus_assert (!dbus_signature_validate_single (sig, NULL));
357 dbus_signature_iter_init (&iter, sig);
358 _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_INVALID);
360 sig = DBUS_TYPE_STRING_AS_STRING;
361 _dbus_assert (dbus_signature_validate (sig, NULL));
362 _dbus_assert (dbus_signature_validate_single (sig, NULL));
363 dbus_signature_iter_init (&iter, sig);
364 _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_STRING);
366 sig = DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_BYTE_AS_STRING;
367 _dbus_assert (dbus_signature_validate (sig, NULL));
368 dbus_signature_iter_init (&iter, sig);
369 _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_STRING);
370 boolres = dbus_signature_iter_next (&iter);
371 _dbus_assert (boolres);
372 _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_BYTE);
374 sig = DBUS_TYPE_UINT16_AS_STRING
375 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
376 DBUS_TYPE_STRING_AS_STRING
377 DBUS_TYPE_UINT32_AS_STRING
378 DBUS_TYPE_VARIANT_AS_STRING
379 DBUS_TYPE_DOUBLE_AS_STRING
380 DBUS_STRUCT_END_CHAR_AS_STRING;
381 _dbus_assert (dbus_signature_validate (sig, NULL));
382 dbus_signature_iter_init (&iter, sig);
383 _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_UINT16);
384 boolres = dbus_signature_iter_next (&iter);
385 _dbus_assert (boolres);
386 _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_STRUCT);
387 dbus_signature_iter_recurse (&iter, &subiter);
388 _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_STRING);
389 boolres = dbus_signature_iter_next (&subiter);
390 _dbus_assert (boolres);
391 _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_UINT32);
392 boolres = dbus_signature_iter_next (&subiter);
393 _dbus_assert (boolres);
394 _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_VARIANT);
395 boolres = dbus_signature_iter_next (&subiter);
396 _dbus_assert (boolres);
397 _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_DOUBLE);
399 sig = DBUS_TYPE_UINT16_AS_STRING
400 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
401 DBUS_TYPE_UINT32_AS_STRING
402 DBUS_TYPE_BYTE_AS_STRING
403 DBUS_TYPE_ARRAY_AS_STRING
404 DBUS_TYPE_ARRAY_AS_STRING
405 DBUS_TYPE_DOUBLE_AS_STRING
406 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
407 DBUS_TYPE_BYTE_AS_STRING
408 DBUS_STRUCT_END_CHAR_AS_STRING
409 DBUS_STRUCT_END_CHAR_AS_STRING;
410 _dbus_assert (dbus_signature_validate (sig, NULL));
411 dbus_signature_iter_init (&iter, sig);
412 _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_UINT16);
413 boolres = dbus_signature_iter_next (&iter);
414 _dbus_assert (boolres);
415 _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_STRUCT);
416 dbus_signature_iter_recurse (&iter, &subiter);
417 _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_UINT32);
418 boolres = dbus_signature_iter_next (&subiter);
419 _dbus_assert (boolres);
420 _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_BYTE);
421 boolres = dbus_signature_iter_next (&subiter);
422 _dbus_assert (boolres);
423 _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_ARRAY);
424 _dbus_assert (dbus_signature_iter_get_element_type (&subiter) == DBUS_TYPE_ARRAY);
426 dbus_signature_iter_recurse (&subiter, &subsubiter);
427 _dbus_assert (dbus_signature_iter_get_current_type (&subsubiter) == DBUS_TYPE_ARRAY);
428 _dbus_assert (dbus_signature_iter_get_element_type (&subsubiter) == DBUS_TYPE_DOUBLE);
430 dbus_signature_iter_recurse (&subsubiter, &subsubsubiter);
431 _dbus_assert (dbus_signature_iter_get_current_type (&subsubsubiter) == DBUS_TYPE_DOUBLE);
432 boolres = dbus_signature_iter_next (&subiter);
433 _dbus_assert (boolres);
434 _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_STRUCT);
435 dbus_signature_iter_recurse (&subiter, &subsubiter);
436 _dbus_assert (dbus_signature_iter_get_current_type (&subsubiter) == DBUS_TYPE_BYTE);
438 sig = DBUS_TYPE_ARRAY_AS_STRING
439 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
440 DBUS_TYPE_INT16_AS_STRING
441 DBUS_TYPE_STRING_AS_STRING
442 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
443 DBUS_TYPE_VARIANT_AS_STRING;
444 _dbus_assert (dbus_signature_validate (sig, NULL));
445 _dbus_assert (!dbus_signature_validate_single (sig, NULL));
446 dbus_signature_iter_init (&iter, sig);
447 _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_ARRAY);
448 _dbus_assert (dbus_signature_iter_get_element_type (&iter) == DBUS_TYPE_DICT_ENTRY);
450 dbus_signature_iter_recurse (&iter, &subiter);
451 dbus_signature_iter_recurse (&subiter, &subsubiter);
452 _dbus_assert (dbus_signature_iter_get_current_type (&subsubiter) == DBUS_TYPE_INT16);
453 boolres = dbus_signature_iter_next (&subsubiter);
454 _dbus_assert (boolres);
455 _dbus_assert (dbus_signature_iter_get_current_type (&subsubiter) == DBUS_TYPE_STRING);
456 boolres = dbus_signature_iter_next (&subsubiter);
457 _dbus_assert (!boolres);
459 boolres = dbus_signature_iter_next (&iter);
460 _dbus_assert (boolres);
461 _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_VARIANT);
462 boolres = dbus_signature_iter_next (&iter);
463 _dbus_assert (!boolres);
465 sig = DBUS_TYPE_DICT_ENTRY_AS_STRING;
466 _dbus_assert (!dbus_signature_validate (sig, NULL));
468 sig = DBUS_TYPE_ARRAY_AS_STRING;
469 _dbus_assert (!dbus_signature_validate (sig, NULL));
471 sig = DBUS_TYPE_UINT32_AS_STRING
472 DBUS_TYPE_ARRAY_AS_STRING;
473 _dbus_assert (!dbus_signature_validate (sig, NULL));
475 sig = DBUS_TYPE_ARRAY_AS_STRING
476 DBUS_TYPE_DICT_ENTRY_AS_STRING;
477 _dbus_assert (!dbus_signature_validate (sig, NULL));
479 sig = DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING;
480 _dbus_assert (!dbus_signature_validate (sig, NULL));
482 sig = DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
483 _dbus_assert (!dbus_signature_validate (sig, NULL));
485 sig = DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
486 DBUS_TYPE_INT32_AS_STRING;
487 _dbus_assert (!dbus_signature_validate (sig, NULL));
489 sig = DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
490 DBUS_TYPE_INT32_AS_STRING
491 DBUS_TYPE_STRING_AS_STRING;
492 _dbus_assert (!dbus_signature_validate (sig, NULL));
494 sig = DBUS_STRUCT_END_CHAR_AS_STRING
495 DBUS_STRUCT_BEGIN_CHAR_AS_STRING;
496 _dbus_assert (!dbus_signature_validate (sig, NULL));
498 sig = DBUS_STRUCT_BEGIN_CHAR_AS_STRING
499 DBUS_TYPE_BOOLEAN_AS_STRING;
500 _dbus_assert (!dbus_signature_validate (sig, NULL));
504 _dbus_assert_not_reached ("out of memory");