Fix compilation on MSVC, which doesn't understand "inline" with its C99 meaning.
[platform/upstream/dbus.git] / dbus / 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
32 /**
33  * Implementation details of #DBusSignatureIter, all fields are private
34  */
35 typedef struct
36
37   const char *pos;           /**< current position in the signature string */
38   unsigned int finished : 1; /**< true if we are at the end iter */
39   unsigned int in_array : 1; /**< true if we are a subiterator pointing to an array's element type */
40 } DBusSignatureRealIter;
41
42 /** macro that checks whether a typecode is a container type */
43 #define TYPE_IS_CONTAINER(typecode)             \
44     ((typecode) == DBUS_TYPE_STRUCT ||          \
45      (typecode) == DBUS_TYPE_DICT_ENTRY ||      \
46      (typecode) == DBUS_TYPE_VARIANT ||         \
47      (typecode) == DBUS_TYPE_ARRAY)
48
49
50 /**
51  * @defgroup DBusSignature Type signature parsing
52  * @ingroup  DBus
53  * @brief Parsing D-Bus type signatures
54  * @{
55  */
56
57 /**
58  * Initializes a #DBusSignatureIter for reading a type signature.  This
59  * function is not safe to use on invalid signatures; be sure to
60  * validate potentially invalid signatures with dbus_signature_validate
61  * before using this function.
62  *
63  * @param iter pointer to an iterator to initialize
64  * @param signature the type signature
65  */
66 void
67 dbus_signature_iter_init (DBusSignatureIter *iter,
68                           const char        *signature)
69 {
70   DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
71
72   real_iter->pos = signature;
73   real_iter->finished = FALSE;
74   real_iter->in_array = FALSE;
75 }
76
77 /**
78  * Returns the current type pointed to by the iterator.
79  * If the iterator is pointing at a type code such as 's', then
80  * it will be returned directly.
81  *
82  * However, when the parser encounters a container type start
83  * character such as '(' for a structure, the corresponding type for
84  * the container will be returned, e.g.  DBUS_TYPE_STRUCT, not '('.
85  * In this case, you should initialize a sub-iterator with
86  * dbus_signature_iter_recurse() to parse the container type.
87  *
88  * @param iter pointer to an iterator 
89  * @returns current type (e.g. #DBUS_TYPE_STRING, #DBUS_TYPE_ARRAY)
90  */
91 int
92 dbus_signature_iter_get_current_type (const DBusSignatureIter *iter)
93 {
94   DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
95
96   return _dbus_first_type_in_signature_c_str (real_iter->pos, 0);
97 }
98
99 /**
100  * Returns the signature of the single complete type starting at the
101  * given iterator.
102  *
103  * For example, if the iterator is pointing at the start of "(ii)ii"
104  * (which is "a struct of two ints, followed by an int, followed by an
105  * int"), then "(ii)" would be returned. If the iterator is pointing at
106  * one of the "i" then just that "i" would be returned.
107  *
108  * @param iter pointer to an iterator 
109  * @returns current signature; or #NULL if no memory.  Should be freed with dbus_free()
110  */
111 char *
112 dbus_signature_iter_get_signature (const DBusSignatureIter *iter)
113 {
114   DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
115   DBusString str;
116   char *ret;
117   int pos;
118   
119   if (!_dbus_string_init (&str))
120     return NULL;
121
122   pos = 0;
123   _dbus_type_signature_next (real_iter->pos, &pos);
124
125   if (!_dbus_string_append_len (&str, real_iter->pos, pos))
126     return NULL;
127   if (!_dbus_string_steal_data (&str, &ret))
128     ret = NULL;
129   _dbus_string_free (&str);
130
131   return ret; 
132 }
133
134 /**
135  * Convenience function for returning the element type of an array;
136  * This function allows you to avoid initializing a sub-iterator and
137  * getting its current type.
138  *
139  * Undefined behavior results if you invoke this function when the
140  * current type of the iterator is not #DBUS_TYPE_ARRAY.
141  *
142  * @param iter pointer to an iterator 
143  * @returns current array element type
144  */
145 int
146 dbus_signature_iter_get_element_type (const DBusSignatureIter *iter)
147 {
148   DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
149
150   _dbus_return_val_if_fail (dbus_signature_iter_get_current_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
151
152   return _dbus_first_type_in_signature_c_str (real_iter->pos, 1);
153 }
154
155 /**
156  * Skip to the next value on this "level". e.g. the next field in a
157  * struct, the next value in an array. Returns #FALSE at the end of the
158  * current container.
159  *
160  * @param iter the iterator
161  * @returns FALSE if nothing more to read at or below this level
162  */
163 dbus_bool_t
164 dbus_signature_iter_next (DBusSignatureIter *iter)
165 {
166   DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
167
168   if (real_iter->finished)
169     return FALSE;
170   else
171     {
172       int pos;
173
174       if (real_iter->in_array)
175         {
176           real_iter->finished = TRUE;
177           return FALSE;
178         }
179
180       pos = 0;
181       _dbus_type_signature_next (real_iter->pos, &pos);
182       real_iter->pos += pos;
183
184       if (*real_iter->pos == DBUS_STRUCT_END_CHAR
185           || *real_iter->pos == DBUS_DICT_ENTRY_END_CHAR)
186         {
187           real_iter->finished = TRUE;
188           return FALSE;
189         }
190
191       return *real_iter->pos != DBUS_TYPE_INVALID;
192     }
193 }
194
195 /**
196  * Initialize a new iterator pointing to the first type in the current
197  * container.
198  * 
199  * The results are undefined when calling this if the current type is
200  * a non-container (i.e. if dbus_type_is_container() returns #FALSE
201  * for the result of dbus_signature_iter_get_current_type()).
202  *
203  * @param iter the current interator
204  * @param subiter an iterator to initialize pointing to the first child
205  */
206 void
207 dbus_signature_iter_recurse (const DBusSignatureIter *iter,
208                              DBusSignatureIter       *subiter)
209 {
210   DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
211   DBusSignatureRealIter *real_sub_iter = (DBusSignatureRealIter *) subiter;
212
213   _dbus_return_if_fail (dbus_type_is_container (dbus_signature_iter_get_current_type (iter)));
214
215   *real_sub_iter = *real_iter;
216   real_sub_iter->in_array = FALSE;
217   real_sub_iter->pos++;
218
219   if (dbus_signature_iter_get_current_type (iter) == DBUS_TYPE_ARRAY)
220     real_sub_iter->in_array = TRUE;
221 }
222
223 /**
224  * Check a type signature for validity. Remember that #NULL can always
225  * be passed instead of a DBusError*, if you don't care about having
226  * an error name and message.
227  *
228  * @param signature a potentially invalid type signature
229  * @param error error return
230  * @returns #TRUE if signature is valid or #FALSE if an error is set
231  */
232 dbus_bool_t
233 dbus_signature_validate (const char       *signature,
234                          DBusError        *error)
235                          
236 {
237   DBusString str;
238   DBusValidity reason;
239
240   _dbus_string_init_const (&str, signature);
241   reason = _dbus_validate_signature_with_reason (&str, 0, _dbus_string_get_length (&str));
242
243   if (reason == DBUS_VALID)
244     return TRUE;
245   else
246     {
247       dbus_set_error (error, DBUS_ERROR_INVALID_SIGNATURE, _dbus_validity_to_error_message (reason));
248       return FALSE;
249     }
250 }
251
252 /**
253  * Check that a type signature is both valid and contains exactly one
254  * complete type. "One complete type" means a single basic type,
255  * array, struct, or dictionary, though the struct or array may be
256  * arbitrarily recursive and complex. More than one complete type
257  * would mean for example "ii" or two integers in sequence.
258  *
259  * @param signature a potentially invalid type signature
260  * @param error error return
261  * @returns #TRUE if signature is valid and has exactly one complete type
262  */
263 dbus_bool_t
264 dbus_signature_validate_single (const char       *signature,
265                                 DBusError        *error)
266 {
267   DBusSignatureIter iter;
268
269   if (!dbus_signature_validate (signature, error))
270     return FALSE;
271
272   dbus_signature_iter_init (&iter, signature);
273   if (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_INVALID)
274     goto lose;
275   if (!dbus_signature_iter_next (&iter))
276     return TRUE;
277  lose:
278   dbus_set_error (error, DBUS_ERROR_INVALID_SIGNATURE, "Exactly one complete type required in signature");
279   return FALSE;
280 }
281
282 /**
283  * A "container type" can contain basic types, or nested
284  * container types. #DBUS_TYPE_INVALID is not a container type.
285  *
286  * It is an error to pass an invalid type-code, other than DBUS_TYPE_INVALID,
287  * to this function. The valid type-codes are defined by dbus-protocol.h.
288  *
289  * @param typecode either a valid type-code or DBUS_TYPE_INVALID
290  * @returns #TRUE if type is a container
291  */
292 dbus_bool_t
293 dbus_type_is_container (int typecode)
294 {
295   /* only reasonable (non-line-noise) typecodes are allowed */
296   _dbus_return_val_if_fail (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
297                             FALSE);
298   return TYPE_IS_CONTAINER (typecode);
299 }
300
301 /**
302  * A "basic type" is a somewhat arbitrary concept, but the intent is
303  * to include those types that are fully-specified by a single
304  * typecode, with no additional type information or nested values. So
305  * all numbers and strings are basic types and structs, arrays, and
306  * variants are not basic types.  #DBUS_TYPE_INVALID is not a basic
307  * type.
308  *
309  * It is an error to pass an invalid type-code, other than DBUS_TYPE_INVALID,
310  * to this function. The valid type-codes are defined by dbus-protocol.h.
311  *
312  * @param typecode either a valid type-code or DBUS_TYPE_INVALID
313  * @returns #TRUE if type is basic
314  */
315 dbus_bool_t
316 dbus_type_is_basic (int typecode)
317 {
318   /* only reasonable (non-line-noise) typecodes are allowed */
319   _dbus_return_val_if_fail (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
320                             FALSE);
321
322   /* everything that isn't invalid or a container */
323   return !(typecode == DBUS_TYPE_INVALID || TYPE_IS_CONTAINER (typecode));
324 }
325
326 /**
327  * Tells you whether values of this type can change length if you set
328  * them to some other value. For this purpose, you assume that the
329  * first byte of the old and new value would be in the same location,
330  * so alignment padding is not a factor.
331  *
332  * This function is useful to determine whether
333  * dbus_message_iter_get_fixed_array() may be used.
334  *
335  * Some structs are fixed-size (if they contain only fixed-size types)
336  * but struct is not considered a fixed type for purposes of this
337  * function.
338  *
339  * It is an error to pass an invalid type-code, other than DBUS_TYPE_INVALID,
340  * to this function. The valid type-codes are defined by dbus-protocol.h.
341  *
342  * @param typecode either a valid type-code or DBUS_TYPE_INVALID
343  * @returns #FALSE if the type can occupy different lengths
344  */
345 dbus_bool_t
346 dbus_type_is_fixed (int typecode)
347 {
348   /* only reasonable (non-line-noise) typecodes are allowed */
349   _dbus_return_val_if_fail (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
350                             FALSE);
351   
352   switch (typecode)
353     {
354     case DBUS_TYPE_BYTE:
355     case DBUS_TYPE_BOOLEAN:
356     case DBUS_TYPE_INT16:
357     case DBUS_TYPE_UINT16:
358     case DBUS_TYPE_INT32:
359     case DBUS_TYPE_UINT32:
360     case DBUS_TYPE_INT64:
361     case DBUS_TYPE_UINT64:
362     case DBUS_TYPE_DOUBLE:
363     case DBUS_TYPE_UNIX_FD:
364       return TRUE;
365     default:
366       return FALSE;
367     }
368 }
369
370 /** @} */ /* end of DBusSignature group */
371
372 #ifdef DBUS_BUILD_TESTS
373
374 /**
375  * @ingroup DBusSignatureInternals
376  * Unit test for DBusSignature.
377  *
378  * @returns #TRUE on success.
379  */
380 dbus_bool_t
381 _dbus_signature_test (void)
382 {
383   DBusSignatureIter iter;
384   DBusSignatureIter subiter;
385   DBusSignatureIter subsubiter;
386   DBusSignatureIter subsubsubiter;
387   const char *sig;
388   dbus_bool_t boolres;
389
390   _dbus_assert (sizeof (DBusSignatureIter) >= sizeof (DBusSignatureRealIter));
391
392   sig = "";
393   _dbus_assert (dbus_signature_validate (sig, NULL));
394   _dbus_assert (!dbus_signature_validate_single (sig, NULL));
395   dbus_signature_iter_init (&iter, sig);
396   _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_INVALID);
397
398   sig = DBUS_TYPE_STRING_AS_STRING;
399   _dbus_assert (dbus_signature_validate (sig, NULL));
400   _dbus_assert (dbus_signature_validate_single (sig, NULL));
401   dbus_signature_iter_init (&iter, sig);
402   _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_STRING);
403
404   sig = DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_BYTE_AS_STRING;
405   _dbus_assert (dbus_signature_validate (sig, NULL));
406   dbus_signature_iter_init (&iter, sig);
407   _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_STRING);
408   boolres = dbus_signature_iter_next (&iter);
409   _dbus_assert (boolres);
410   _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_BYTE);
411
412   sig = DBUS_TYPE_UINT16_AS_STRING
413     DBUS_STRUCT_BEGIN_CHAR_AS_STRING
414     DBUS_TYPE_STRING_AS_STRING
415     DBUS_TYPE_UINT32_AS_STRING
416     DBUS_TYPE_VARIANT_AS_STRING
417     DBUS_TYPE_DOUBLE_AS_STRING
418     DBUS_STRUCT_END_CHAR_AS_STRING;
419   _dbus_assert (dbus_signature_validate (sig, NULL));
420   dbus_signature_iter_init (&iter, sig);
421   _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_UINT16);
422   boolres = dbus_signature_iter_next (&iter);
423   _dbus_assert (boolres);
424   _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_STRUCT);
425   dbus_signature_iter_recurse (&iter, &subiter);
426   _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_STRING);
427   boolres = dbus_signature_iter_next (&subiter);
428   _dbus_assert (boolres);
429   _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_UINT32);
430   boolres = dbus_signature_iter_next (&subiter);
431   _dbus_assert (boolres);
432   _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_VARIANT);
433   boolres = dbus_signature_iter_next (&subiter);
434   _dbus_assert (boolres);
435   _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_DOUBLE);
436
437   sig = DBUS_TYPE_UINT16_AS_STRING
438     DBUS_STRUCT_BEGIN_CHAR_AS_STRING
439     DBUS_TYPE_UINT32_AS_STRING
440     DBUS_TYPE_BYTE_AS_STRING
441     DBUS_TYPE_ARRAY_AS_STRING
442     DBUS_TYPE_ARRAY_AS_STRING
443     DBUS_TYPE_DOUBLE_AS_STRING
444     DBUS_STRUCT_BEGIN_CHAR_AS_STRING
445     DBUS_TYPE_BYTE_AS_STRING
446     DBUS_STRUCT_END_CHAR_AS_STRING
447     DBUS_STRUCT_END_CHAR_AS_STRING;
448   _dbus_assert (dbus_signature_validate (sig, NULL));
449   dbus_signature_iter_init (&iter, sig);
450   _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_UINT16);
451   boolres = dbus_signature_iter_next (&iter);
452   _dbus_assert (boolres);
453   _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_STRUCT);
454   dbus_signature_iter_recurse (&iter, &subiter);
455   _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_UINT32);
456   boolres = dbus_signature_iter_next (&subiter);
457   _dbus_assert (boolres);
458   _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_BYTE);
459   boolres = dbus_signature_iter_next (&subiter);
460   _dbus_assert (boolres);
461   _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_ARRAY);
462   _dbus_assert (dbus_signature_iter_get_element_type (&subiter) == DBUS_TYPE_ARRAY);
463
464   dbus_signature_iter_recurse (&subiter, &subsubiter);
465   _dbus_assert (dbus_signature_iter_get_current_type (&subsubiter) == DBUS_TYPE_ARRAY);
466   _dbus_assert (dbus_signature_iter_get_element_type (&subsubiter) == DBUS_TYPE_DOUBLE);
467
468   dbus_signature_iter_recurse (&subsubiter, &subsubsubiter);
469   _dbus_assert (dbus_signature_iter_get_current_type (&subsubsubiter) == DBUS_TYPE_DOUBLE);
470   boolres = dbus_signature_iter_next (&subiter);
471   _dbus_assert (boolres);
472   _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_STRUCT);
473   dbus_signature_iter_recurse (&subiter, &subsubiter);
474   _dbus_assert (dbus_signature_iter_get_current_type (&subsubiter) == DBUS_TYPE_BYTE);
475
476   sig = DBUS_TYPE_ARRAY_AS_STRING
477     DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
478     DBUS_TYPE_INT16_AS_STRING
479     DBUS_TYPE_STRING_AS_STRING
480     DBUS_DICT_ENTRY_END_CHAR_AS_STRING
481     DBUS_TYPE_VARIANT_AS_STRING;
482   _dbus_assert (dbus_signature_validate (sig, NULL));
483   _dbus_assert (!dbus_signature_validate_single (sig, NULL));
484   dbus_signature_iter_init (&iter, sig);
485   _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_ARRAY);
486   _dbus_assert (dbus_signature_iter_get_element_type (&iter) == DBUS_TYPE_DICT_ENTRY);
487
488   dbus_signature_iter_recurse (&iter, &subiter);
489   dbus_signature_iter_recurse (&subiter, &subsubiter);
490   _dbus_assert (dbus_signature_iter_get_current_type (&subsubiter) == DBUS_TYPE_INT16);
491   boolres = dbus_signature_iter_next (&subsubiter);
492   _dbus_assert (boolres);
493   _dbus_assert (dbus_signature_iter_get_current_type (&subsubiter) == DBUS_TYPE_STRING);
494   boolres = dbus_signature_iter_next (&subsubiter);
495   _dbus_assert (!boolres);
496
497   boolres = dbus_signature_iter_next (&iter);
498   _dbus_assert (boolres);
499   _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_VARIANT);
500   boolres = dbus_signature_iter_next (&iter);
501   _dbus_assert (!boolres);
502
503   sig = DBUS_TYPE_DICT_ENTRY_AS_STRING;
504   _dbus_assert (!dbus_signature_validate (sig, NULL));
505
506   sig = DBUS_TYPE_ARRAY_AS_STRING;
507   _dbus_assert (!dbus_signature_validate (sig, NULL));
508
509   sig = DBUS_TYPE_UINT32_AS_STRING
510     DBUS_TYPE_ARRAY_AS_STRING;
511   _dbus_assert (!dbus_signature_validate (sig, NULL));
512
513   sig = DBUS_TYPE_ARRAY_AS_STRING
514     DBUS_TYPE_DICT_ENTRY_AS_STRING;
515   _dbus_assert (!dbus_signature_validate (sig, NULL));
516
517   sig = DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING;
518   _dbus_assert (!dbus_signature_validate (sig, NULL));
519
520   sig = DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
521   _dbus_assert (!dbus_signature_validate (sig, NULL));
522
523   sig = DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
524     DBUS_TYPE_INT32_AS_STRING;
525   _dbus_assert (!dbus_signature_validate (sig, NULL));
526
527   sig = DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
528     DBUS_TYPE_INT32_AS_STRING
529     DBUS_TYPE_STRING_AS_STRING;
530   _dbus_assert (!dbus_signature_validate (sig, NULL));
531
532   sig = DBUS_STRUCT_END_CHAR_AS_STRING
533     DBUS_STRUCT_BEGIN_CHAR_AS_STRING;
534   _dbus_assert (!dbus_signature_validate (sig, NULL));
535
536   sig = DBUS_STRUCT_BEGIN_CHAR_AS_STRING
537     DBUS_TYPE_BOOLEAN_AS_STRING;
538   _dbus_assert (!dbus_signature_validate (sig, NULL));
539   return TRUE;
540 #if 0
541  oom:
542   _dbus_assert_not_reached ("out of memory");
543   return FALSE;
544 #endif
545 }
546
547 #endif
548