summaryrefslogtreecommitdiff
path: root/dbind/dbind-any.c
blob: bcbe39118b3c3332f6e75b91516540bbbc92a701 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/*
 * Copyright 2008-2011 Novell, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/* type driven marshalling */
#include <glib.h>
#include <stdio.h>

#include "config.h"
#include "dbind-any.h"

#undef DEBUG

/*  Align a value upward to a boundary, expressed as a number of bytes.
 *  E.g. align to an 8-byte boundary with argument of 8.
 *
 *   (this + boundary - 1)
 *          &
 *    ~(boundary - 1)
 */
#define ALIGN_VALUE(this, boundary) \
  ((((gulong) (this)) + (((gulong) (boundary)) - 1)) & (~(((gulong) (boundary)) - 1)))

#define ALIGN_ADDRESS(this, boundary) \
  ((gpointer) ALIGN_VALUE (this, boundary))

#define PTR_PLUS(ptr, offset) \
  ((gpointer) (((guchar *) (ptr)) + (offset)))

#define DBIND_POD_CASES   \
  DBUS_TYPE_BYTE:         \
  case DBUS_TYPE_INT16:   \
  case DBUS_TYPE_UINT16:  \
  case DBUS_TYPE_INT32:   \
  case DBUS_TYPE_UINT32:  \
  case DBUS_TYPE_BOOLEAN: \
  case DBUS_TYPE_INT64:   \
  case DBUS_TYPE_UINT64:  \
  case DBUS_TYPE_DOUBLE

/*---------------------------------------------------------------------------*/

static void
warn_braces ()
{
  fprintf (stderr, "Error: dbus flags structures & dicts with braces rather than "
                   " an explicit type member of 'struct'\n");
}

/*---------------------------------------------------------------------------*/

static unsigned int
dbind_find_c_alignment_r (const char **type)
{
  unsigned int retval = 1;

  char t = **type;
  (*type)++;

#ifdef DEBUG
  fprintf (stderr, "\tfind align for %c (0x%x)\n", t, t);
#endif

  switch (t)
    {
    case DBUS_TYPE_BYTE:
      return ALIGNOF_CHAR;
    case DBUS_TYPE_BOOLEAN:
      return ALIGNOF_DBUS_BOOL_T;
    case DBUS_TYPE_INT16:
    case DBUS_TYPE_UINT16:
      return ALIGNOF_DBUS_INT16_T;
    case DBUS_TYPE_INT32:
    case DBUS_TYPE_UINT32:
      return ALIGNOF_DBUS_INT32_T;
    case DBUS_TYPE_INT64:
    case DBUS_TYPE_UINT64:
      return ALIGNOF_DBUS_INT64_T;
    case DBUS_TYPE_DOUBLE:
      return ALIGNOF_DOUBLE;
    /* ptr types */
    case DBUS_TYPE_STRING:
    case DBUS_TYPE_OBJECT_PATH:
    case DBUS_TYPE_SIGNATURE:
    case DBUS_TYPE_ARRAY:
      return ALIGNOF_DBIND_POINTER;
    case DBUS_STRUCT_BEGIN_CHAR:
      /* TODO: I think this would break with a nested struct */
#if ALIGNOF_DBIND_STRUCT > 1
      retval = MAX (retval, ALIGNOF_DBIND_STRUCT);
#endif
      while (**type != DBUS_STRUCT_END_CHAR)
        {
          int elem_align = dbind_find_c_alignment_r (type);
          retval = MAX (retval, elem_align);
        }
      (*type)++;
      return retval;
    case DBUS_DICT_ENTRY_BEGIN_CHAR:
#if ALIGNOF_DBIND_STRUCT > 1
      retval = MAX (retval, ALIGNOF_DBIND_STRUCT);
#endif
      while (**type != DBUS_DICT_ENTRY_END_CHAR)
        {
          int elem_align = dbind_find_c_alignment_r (type);
          retval = MAX (retval, elem_align);
        }
      (*type)++;
      return retval;
    case DBUS_TYPE_STRUCT:
    case DBUS_TYPE_DICT_ENTRY:
      warn_braces ();
      return ALIGNOF_DBIND_POINTER;
    case '\0':
      g_assert_not_reached ();
      break;
    default:
      return 1;
    }
}

/*---------------------------------------------------------------------------*/

/* gather immediate allocation information for this type */
static size_t
dbind_gather_alloc_info_r (const char **type)
{
  char t = **type;
  (*type)++;
  if (t == DBUS_TYPE_ARRAY)
    {
      switch (**type)
        {
        case DBUS_STRUCT_BEGIN_CHAR:
          while (**type != DBUS_STRUCT_END_CHAR && **type != '\0')
            (*type)++;
          if (**type != '\0')
            (*type)++;
          break;
        case DBUS_DICT_ENTRY_BEGIN_CHAR:
          while (**type != DBUS_DICT_ENTRY_END_CHAR && **type != '\0')
            (*type)++;
          if (**type != '\0')
            (*type)++;
          break;
        case '\0':
          break;
        default:
          (*type)++;
          break;
        }
    }

  switch (t)
    {
    case DBUS_TYPE_BYTE:
      return sizeof (char);
    case DBUS_TYPE_BOOLEAN:
      return sizeof (dbus_bool_t);
    case DBUS_TYPE_INT16:
    case DBUS_TYPE_UINT16:
      return sizeof (dbus_int16_t);
    case DBUS_TYPE_INT32:
    case DBUS_TYPE_UINT32:
      return sizeof (dbus_int32_t);
    case DBUS_TYPE_INT64:
    case DBUS_TYPE_UINT64:
      return sizeof (dbus_int64_t);
    case DBUS_TYPE_DOUBLE:
      return sizeof (double);
    /* ptr types */
    case DBUS_TYPE_STRING:
    case DBUS_TYPE_OBJECT_PATH:
    case DBUS_TYPE_SIGNATURE:
    case DBUS_TYPE_ARRAY:
      return sizeof (void *);
    case DBUS_STRUCT_BEGIN_CHAR:
      {
        int sum = 0, stralign;

        stralign = dbind_find_c_alignment (*type - 1);

        while (**type != DBUS_STRUCT_END_CHAR)
          {
            sum = ALIGN_VALUE (sum, dbind_find_c_alignment (*type));
            sum += dbind_gather_alloc_info_r (type);
          }
        sum = ALIGN_VALUE (sum, stralign);

        g_assert (**type == DBUS_STRUCT_END_CHAR);
        (*type)++;

        return sum;
      }
    case DBUS_DICT_ENTRY_BEGIN_CHAR:
      {
        int sum = 0, stralign;

        stralign = dbind_find_c_alignment (*type - 1);

        while (**type != DBUS_DICT_ENTRY_END_CHAR)
          {
            sum = ALIGN_VALUE (sum, dbind_find_c_alignment (*type));
            sum += dbind_gather_alloc_info_r (type);
          }
        sum = ALIGN_VALUE (sum, stralign);

        g_assert (**type == DBUS_DICT_ENTRY_END_CHAR);
        (*type)++;

        return sum;
      }
    case DBUS_TYPE_STRUCT:
    case DBUS_TYPE_DICT_ENTRY:
      warn_braces ();
    default:
      return 0;
    }
}

static size_t
dbind_gather_alloc_info (const char *type)
{
  return dbind_gather_alloc_info_r (&type);
}

/*---------------------------------------------------------------------------*/

static void
dbind_any_free_r (const char **type, void **data)
{
#ifdef DEBUG
  fprintf (stderr, "any free '%c' to %p\n", **type, *data);
#endif

  switch (**type)
    {
    case DBIND_POD_CASES:
      *data = ((guchar *) *data) + dbind_gather_alloc_info (*type);
      (*type)++;
      break;
    case DBUS_TYPE_STRING:
    case DBUS_TYPE_OBJECT_PATH:
    case DBUS_TYPE_SIGNATURE:
#ifdef DEBUG
      fprintf (stderr, "string free %p\n", **(void ***) data);
#endif
      g_free (**(void ***) data);
      *data = ((guchar *) *data) + dbind_gather_alloc_info (*type);
      (*type)++;
      break;
    case DBUS_TYPE_ARRAY:
      {
        int i;
        GArray *vals = **(void ***) data;
        size_t elem_size, elem_align;
        const char *saved_child_type;

        (*type)++;
        saved_child_type = *type;

        elem_size = dbind_gather_alloc_info (*type);
        elem_align = dbind_find_c_alignment_r (type);

        for (i = 0; i < vals->len; i++)
          {
            void *ptr = vals->data + elem_size * i;
            *type = saved_child_type; /* rewind type info */
            ptr = ALIGN_ADDRESS (ptr, elem_align);
            dbind_any_free_r (type, &ptr);
          }
        g_array_free (vals, TRUE);
        break;
      }
    case DBUS_STRUCT_BEGIN_CHAR:
      {
        gconstpointer data0 = *data;
        int offset = 0, stralign;

        stralign = dbind_find_c_alignment (*type);
        (*type)++;

        offset = 0;
        while (**type != DBUS_STRUCT_END_CHAR)
          {
            const char *subt = *type;
            offset = ALIGN_VALUE (offset, dbind_find_c_alignment (*type));
            *data = PTR_PLUS (data0, offset);
            dbind_any_free_r (type, data);
            offset += dbind_gather_alloc_info (subt);
          }

        offset = ALIGN_VALUE (offset, stralign);
        *data = PTR_PLUS (data0, offset);

        g_assert (**type == DBUS_STRUCT_END_CHAR);
        (*type)++;

        break;
      }
    case DBUS_DICT_ENTRY_BEGIN_CHAR:
      {
        gconstpointer data0 = *data;
        int offset = 0, stralign;

        stralign = dbind_find_c_alignment (*type);
        (*type)++;

        offset = 0;
        while (**type != DBUS_DICT_ENTRY_END_CHAR)
          {
            const char *subt = *type;
            offset = ALIGN_VALUE (offset, dbind_find_c_alignment (*type));
            *data = PTR_PLUS (data0, offset);
            dbind_any_free_r (type, data);
            offset += dbind_gather_alloc_info (subt);
          }

        offset = ALIGN_VALUE (offset, stralign);
        *data = PTR_PLUS (data0, offset);

        g_assert (**type == DBUS_DICT_ENTRY_END_CHAR);
        (*type)++;

        break;
      }
    case DBUS_TYPE_STRUCT:
    case DBUS_TYPE_DICT_ENTRY:
      warn_braces ();
      break;
    }
}

/*---------------------------------------------------------------------------*/

void
dbind_any_marshal (DBusMessageIter *iter,
                   const char **type,
                   void **data)
{
  size_t len;

#ifdef DEBUG
  fprintf (stderr, "any marshal '%c' to %p\n", **type, *data);
#endif

  switch (**type)
    {
    case DBIND_POD_CASES:
    case DBUS_TYPE_STRING:
    case DBUS_TYPE_OBJECT_PATH:
    case DBUS_TYPE_SIGNATURE:
      len = dbind_gather_alloc_info (*type);
      dbus_message_iter_append_basic (iter, **type, *data);
      *data = ((guchar *) *data) + len;
      (*type)++;
      break;
    case DBUS_TYPE_ARRAY:
      {
        int i;
        GArray *vals = **(void ***) data;
        size_t elem_size, elem_align;
        DBusMessageIter sub;
        const char *saved_child_type;
        char *child_type_string;

        (*type)++;
        saved_child_type = *type;

        elem_size = dbind_gather_alloc_info (*type);
        elem_align = dbind_find_c_alignment_r (type);

        /* wow this part of the API sucks too ... */
        child_type_string = g_strndup (saved_child_type, *type - saved_child_type);
        /* fprintf (stderr, "array child type '%s'\n", child_type_string); */
        dbus_message_iter_open_container (iter, DBUS_TYPE_ARRAY,
                                          child_type_string, &sub);
        for (i = 0; i < vals->len; i++)
          {
            void *ptr = vals->data + elem_size * i;
            *type = saved_child_type; /* rewind type info */
            ptr = ALIGN_ADDRESS (ptr, elem_align);
            dbind_any_marshal (&sub, type, &ptr);
          }

        dbus_message_iter_close_container (iter, &sub);
        g_free (child_type_string);
        break;
      }
    case DBUS_STRUCT_BEGIN_CHAR:
      {
        gconstpointer data0 = *data;
        int offset = 0, stralign;
        DBusMessageIter sub;

        stralign = dbind_find_c_alignment (*type);

        (*type)++;

        dbus_message_iter_open_container (iter, DBUS_TYPE_STRUCT, NULL, &sub);

        offset = 0;
        while (**type != DBUS_STRUCT_END_CHAR)
          {
            const char *subt = *type;
            offset = ALIGN_VALUE (offset, dbind_find_c_alignment (*type));
            *data = PTR_PLUS (data0, offset);
            dbind_any_marshal (&sub, type, data);
            offset += dbind_gather_alloc_info (subt);
          }

        offset = ALIGN_VALUE (offset, stralign);
        *data = PTR_PLUS (data0, offset);

        dbus_message_iter_close_container (iter, &sub);

        g_assert (**type == DBUS_STRUCT_END_CHAR);
        (*type)++;

        break;
      }
    case DBUS_DICT_ENTRY_BEGIN_CHAR:
      {
        gconstpointer data0 = *data;
        int offset = 0, stralign;
        DBusMessageIter sub;

        stralign = dbind_find_c_alignment (*type);

        (*type)++;

        dbus_message_iter_open_container (iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub);

        offset = 0;
        while (**type != DBUS_DICT_ENTRY_END_CHAR)
          {
            const char *subt = *type;
            offset = ALIGN_VALUE (offset, dbind_find_c_alignment (*type));
            *data = PTR_PLUS (data0, offset);
            dbind_any_marshal (&sub, type, data);
            offset += dbind_gather_alloc_info (subt);
          }

        offset = ALIGN_VALUE (offset, stralign);
        *data = PTR_PLUS (data0, offset);

        dbus_message_iter_close_container (iter, &sub);

        g_assert (**type == DBUS_DICT_ENTRY_END_CHAR);
        (*type)++;

        break;
      }
    case DBUS_TYPE_STRUCT:
    case DBUS_TYPE_DICT_ENTRY:
      warn_braces ();
      break;
    }
}

/*---------------------------------------------------------------------------*/

void
dbind_any_marshal_va (DBusMessageIter *iter,
                      const char **arg_types,
                      va_list args)
{
  const char *p = *arg_types;

  /* Guard against null arg types
     Fix for - http://bugs.freedesktop.org/show_bug.cgi?id=23027
   */
  if (p == NULL)
    p = "";

  {
    /* special case base-types since we need to walk the stack worse-luck */
    for (; *p != '\0' && *p != '=';)
      {
        int intarg;
        void *ptrarg;
        double doublearg;
        dbus_int64_t int64arg;
        void *arg = NULL;

        switch (*p)
          {
          case DBUS_TYPE_BYTE:
          case DBUS_TYPE_BOOLEAN:
          case DBUS_TYPE_INT16:
          case DBUS_TYPE_UINT16:
          case DBUS_TYPE_INT32:
          case DBUS_TYPE_UINT32:
            intarg = va_arg (args, int);
            arg = &intarg;
            break;
          case DBUS_TYPE_INT64:
          case DBUS_TYPE_UINT64:
            int64arg = va_arg (args, dbus_int64_t);
            arg = &int64arg;
            break;
          case DBUS_TYPE_DOUBLE:
            doublearg = va_arg (args, double);
            arg = &doublearg;
            break;
          /* ptr types */
          case DBUS_TYPE_STRING:
          case DBUS_TYPE_OBJECT_PATH:
          case DBUS_TYPE_SIGNATURE:
          case DBUS_TYPE_ARRAY:
          case DBUS_TYPE_DICT_ENTRY:
            ptrarg = va_arg (args, void *);
            arg = &ptrarg;
            break;
          case DBUS_STRUCT_BEGIN_CHAR:
            ptrarg = va_arg (args, void *);
            arg = ptrarg;
            break;
          case DBUS_DICT_ENTRY_BEGIN_CHAR:
            ptrarg = va_arg (args, void *);
            arg = ptrarg;
            break;

          case DBUS_TYPE_VARIANT:
            fprintf (stderr, "No variant support yet - very toolkit specific\n");
            ptrarg = va_arg (args, void *);
            arg = &ptrarg;
            break;
          default:
            fprintf (stderr, "Unknown / invalid arg type %c\n", *p);
            break;
          }
        if (arg != NULL)
          dbind_any_marshal (iter, &p, &arg);
      }
    if (*arg_types)
      *arg_types = p;
  }
}

/*---------------------------------------------------------------------------*/

void
dbind_any_demarshal (DBusMessageIter *iter,
                     const char **type,
                     void **data)
{
  size_t len;

#ifdef DEBUG
  fprintf (stderr, "any demarshal '%c' to %p\n", **type, *data);
#endif

  switch (**type)
    {
    case DBIND_POD_CASES:
      len = dbind_gather_alloc_info (*type);
      dbus_message_iter_get_basic (iter, *data);
      *data = ((guchar *) *data) + len;
      (*type)++;
      break;
    case DBUS_TYPE_STRING:
    case DBUS_TYPE_OBJECT_PATH:
    case DBUS_TYPE_SIGNATURE:
      len = dbind_gather_alloc_info (*type);
      dbus_message_iter_get_basic (iter, *data);
#ifdef DEBUG
      fprintf (stderr, "dup string '%s' (%p)\n", **(void ***) data, **(void ***) data);
#endif
      **(void ***) data = g_strdup (**(void ***) data);
      *data = ((guchar *) *data) + len;
      (*type)++;
      break;
    case DBUS_TYPE_ARRAY:
      {
        GArray *vals;
        DBusMessageIter child;
        size_t elem_size, elem_align;
        const char *stored_child_type;
        int i;

        (*type)++;
        stored_child_type = *type;

        elem_size = dbind_gather_alloc_info (*type);
        elem_align = dbind_find_c_alignment_r (type);
        vals = g_array_new (FALSE, FALSE, elem_size);
        (**(void ***) data) = vals;
        *data = ((guchar *) *data) + sizeof (void *);

        i = 0;
        dbus_message_iter_recurse (iter, &child);
        while (dbus_message_iter_get_arg_type (&child) != DBUS_TYPE_INVALID)
          {
            void *ptr;
            const char *subt = stored_child_type;
            g_array_set_size (vals, i + 1);
            ptr = vals->data + elem_size * i;
            ptr = ALIGN_ADDRESS (ptr, elem_align);
            dbind_any_demarshal (&child, &subt, &ptr);
            i++;
          };
        break;
      }
    case DBUS_STRUCT_BEGIN_CHAR:
      {
        gconstpointer data0 = *data;
        int offset = 0, stralign;
        DBusMessageIter child;

        stralign = dbind_find_c_alignment (*type);

        (*type)++;

        dbus_message_iter_recurse (iter, &child);

        while (**type != DBUS_STRUCT_END_CHAR)
          {
            const char *subt = *type;
            offset = ALIGN_VALUE (offset, dbind_find_c_alignment (*type));
            *data = PTR_PLUS (data0, offset);
            dbind_any_demarshal (&child, type, data);
            offset += dbind_gather_alloc_info (subt);
          }

        offset = ALIGN_VALUE (offset, stralign);
        *data = PTR_PLUS (data0, offset);

        g_assert (**type == DBUS_STRUCT_END_CHAR);
        (*type)++;

        break;
      }
    case DBUS_DICT_ENTRY_BEGIN_CHAR:
      {
        gconstpointer data0 = *data;
        int offset = 0, stralign;
        DBusMessageIter child;

        stralign = dbind_find_c_alignment (*type);

        (*type)++;

        dbus_message_iter_recurse (iter, &child);

        while (**type != DBUS_DICT_ENTRY_END_CHAR)
          {
            const char *subt = *type;
            offset = ALIGN_VALUE (offset, dbind_find_c_alignment (*type));
            *data = PTR_PLUS (data0, offset);
            dbind_any_demarshal (&child, type, data);
            offset += dbind_gather_alloc_info (subt);
          }

        offset = ALIGN_VALUE (offset, stralign);
        *data = PTR_PLUS (data0, offset);

        g_assert (**type == DBUS_DICT_ENTRY_END_CHAR);
        (*type)++;

        break;
      case DBUS_TYPE_VARIANT:
        /* skip; unimplemented for now */
        (*type)++;
        break;
      }
    case DBUS_TYPE_STRUCT:
    case DBUS_TYPE_DICT_ENTRY:
      warn_braces ();
      break;
    }
  dbus_message_iter_next (iter);
}

static const char *
pass_complex_arg (const char *p, char begin, char end)
{
  int level = 1;

  p++;
  while (*p && level > 0)
    {
      if (*p == begin)
        level++;
      else if (*p == end)
        level--;
      p++;
    }
  if (*p == end)
    p++;
  return p;
}

static const char *
pass_arg (const char *p)
{
  switch (*p)
    {
    case '(':
      return pass_complex_arg (p, '(', ')');
    case '{':
      return pass_complex_arg (p, '{', '}');
    case 'a':
      return pass_arg (p + 1);
    default:
      return p + 1;
    }
}

/*---------------------------------------------------------------------------*/

void
dbind_any_demarshal_va (DBusMessageIter *iter,
                        const char **arg_types,
                        va_list args)
{
  const char *p = *arg_types;

  /* Just consume the in args without doing anything to them */
  for (; *p != '\0' && *p != '=';)
    {
      switch (*p)
        {
        case DBUS_TYPE_BYTE:
        case DBUS_TYPE_BOOLEAN:
        case DBUS_TYPE_INT16:
        case DBUS_TYPE_UINT16:
        case DBUS_TYPE_INT32:
        case DBUS_TYPE_UINT32:
          va_arg (args, int);
          break;
        case DBUS_TYPE_INT64:
        case DBUS_TYPE_UINT64:
          va_arg (args, dbus_int64_t);
          break;
        case DBUS_TYPE_DOUBLE:
          va_arg (args, double);
          break;
        /* ptr types */
        case DBUS_TYPE_STRING:
        case DBUS_TYPE_OBJECT_PATH:
        case DBUS_TYPE_SIGNATURE:
        case DBUS_TYPE_ARRAY:
        case DBUS_TYPE_DICT_ENTRY:
          va_arg (args, void *);
          break;
        case DBUS_STRUCT_BEGIN_CHAR:
          va_arg (args, void *);
          break;
        case DBUS_DICT_ENTRY_BEGIN_CHAR:
          va_arg (args, void *);
          break;

        case DBUS_TYPE_VARIANT:
          fprintf (stderr, "No variant support yet - very toolkit specific\n");
          va_arg (args, void *);
          break;
        default:
          fprintf (stderr, "Unknown / invalid arg type %c\n", *p);
          break;
        }
      p = pass_arg (p);
    }

  if (p[0] == '=' && p[1] == '>')
    p += 2;

  for (; *p != '\0';)
    {
      void *arg = va_arg (args, void *);
      dbind_any_demarshal (iter, &p, &arg);
    }
}

/*---------------------------------------------------------------------------*/

/* nice deep free ... */
void
dbind_any_free (const char *type,
                void *ptr)
{
  dbind_any_free_r (&type, &ptr);
}

/* should this be the default normalization ? */
void
dbind_any_free_ptr (const char *type, void *ptr)
{
  dbind_any_free (type, &ptr);
}

/*---------------------------------------------------------------------------*/

unsigned int
dbind_find_c_alignment (const char *type)
{
  return dbind_find_c_alignment_r (&type);
}

/*END------------------------------------------------------------------------*/