2005-01-28 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-marshal-recursive.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal-recursive.c  Marshalling routines for recursive types
3  *
4  * Copyright (C) 2004, 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "dbus-marshal-recursive.h"
25 #include "dbus-marshal-basic.h"
26 #include "dbus-internals.h"
27
28 /**
29  * @addtogroup DBusMarshal
30  * @{
31  */
32
33 /** turn this on to get deluged in TypeReader verbose spam */
34 #define RECURSIVE_MARSHAL_READ_TRACE  0
35
36 /** turn this on to get deluged in TypeWriter verbose spam */
37 #define RECURSIVE_MARSHAL_WRITE_TRACE 0
38
39 static void
40 free_fixups (DBusList **fixups)
41 {
42   DBusList *link;
43
44   link = _dbus_list_get_first_link (fixups);
45   while (link != NULL)
46     {
47       DBusList *next;
48
49       next = _dbus_list_get_next_link (fixups, link);
50
51       dbus_free (link->data);
52       _dbus_list_free_link (link);
53
54       link = next;
55     }
56
57   *fixups = NULL;
58 }
59
60 static void
61 apply_and_free_fixups (DBusList      **fixups,
62                        DBusTypeReader *reader)
63 {
64   DBusList *link;
65
66 #if RECURSIVE_MARSHAL_WRITE_TRACE
67   if (*fixups)
68     _dbus_verbose (" %d FIXUPS to apply\n",
69                    _dbus_list_get_length (fixups));
70 #endif
71
72   link = _dbus_list_get_first_link (fixups);
73   while (link != NULL)
74     {
75       DBusList *next;
76
77       next = _dbus_list_get_next_link (fixups, link);
78
79       if (reader)
80         {
81           DBusArrayLenFixup *f;
82
83           f = link->data;
84
85 #if RECURSIVE_MARSHAL_WRITE_TRACE
86           _dbus_verbose (" applying FIXUP to reader %p at pos %d new_len = %d old len %d\n",
87                          reader, f->len_pos_in_reader, f->new_len,
88                          _dbus_marshal_read_uint32 (reader->value_str,
89                                                     f->len_pos_in_reader,
90                                                     reader->byte_order, NULL));
91 #endif
92
93           _dbus_marshal_set_uint32 ((DBusString*) reader->value_str,
94                                     f->len_pos_in_reader,
95                                     f->new_len,
96                                     reader->byte_order);
97         }
98
99       dbus_free (link->data);
100       _dbus_list_free_link (link);
101
102       link = next;
103     }
104
105   *fixups = NULL;
106 }
107
108 /**
109  * Virtual table for a type reader.
110  */
111 struct DBusTypeReaderClass
112 {
113   const char *name;       /**< name for debugging */
114   int         id;         /**< index in all_reader_classes */
115   dbus_bool_t types_only; /**< only iterates over types, not values */
116   void        (* recurse)          (DBusTypeReader        *sub,
117                                     DBusTypeReader        *parent); /**< recurse with this reader as sub */
118   dbus_bool_t (* check_finished)   (const DBusTypeReader  *reader); /**< check whether reader is at the end */
119   void        (* next)             (DBusTypeReader        *reader,
120                                     int                    current_type); /**< go to the next value */
121   void        (* init_from_mark)   (DBusTypeReader        *reader,
122                                     const DBusTypeMark    *mark);  /**< uncompress from a mark */
123 };
124
125 static int
126 element_type_get_alignment (const DBusString *str,
127                             int               pos)
128 {
129   return _dbus_type_get_alignment (_dbus_first_type_in_signature (str, pos));
130 }
131
132 static void
133 reader_init (DBusTypeReader    *reader,
134              int                byte_order,
135              const DBusString  *type_str,
136              int                type_pos,
137              const DBusString  *value_str,
138              int                value_pos)
139 {
140   reader->byte_order = byte_order;
141   reader->finished = FALSE;
142   reader->type_str = type_str;
143   reader->type_pos = type_pos;
144   reader->value_str = value_str;
145   reader->value_pos = value_pos;
146 }
147
148 static void
149 base_reader_recurse (DBusTypeReader *sub,
150                      DBusTypeReader *parent)
151 {
152   /* point subreader at the same place as parent */
153   reader_init (sub,
154                parent->byte_order,
155                parent->type_str,
156                parent->type_pos,
157                parent->value_str,
158                parent->value_pos);
159 }
160
161 static void
162 struct_or_dict_entry_types_only_reader_recurse (DBusTypeReader *sub,
163                                                 DBusTypeReader *parent)
164 {
165   base_reader_recurse (sub, parent);
166   
167   _dbus_assert (_dbus_string_get_byte (sub->type_str,
168                                        sub->type_pos) == DBUS_STRUCT_BEGIN_CHAR ||
169                 _dbus_string_get_byte (sub->type_str,
170                                        sub->type_pos) == DBUS_DICT_ENTRY_BEGIN_CHAR);
171
172   sub->type_pos += 1;
173 }
174
175 static void
176 struct_or_dict_entry_reader_recurse (DBusTypeReader *sub,
177                                      DBusTypeReader *parent)
178 {
179   struct_or_dict_entry_types_only_reader_recurse (sub, parent);
180
181   /* struct and dict entry have 8 byte alignment */
182   sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
183 }
184
185 static void
186 array_types_only_reader_recurse (DBusTypeReader *sub,
187                                  DBusTypeReader *parent)
188 {
189   base_reader_recurse (sub, parent);
190
191   /* point type_pos at the array element type */
192   sub->type_pos += 1;
193
194   /* Init with values likely to crash things if misused */
195   sub->u.array.start_pos = _DBUS_INT_MAX;
196   sub->array_len_offset = 7;
197 }
198
199 /** compute position of array length given array_len_offset, which is
200     the offset back from start_pos to end of the len */
201 #define ARRAY_READER_LEN_POS(reader) \
202   ((reader)->u.array.start_pos - ((int)(reader)->array_len_offset) - 4)
203
204 static int
205 array_reader_get_array_len (const DBusTypeReader *reader)
206 {
207   dbus_uint32_t array_len;
208   int len_pos;
209
210   len_pos = ARRAY_READER_LEN_POS (reader);
211
212   _dbus_assert (_DBUS_ALIGN_VALUE (len_pos, 4) == (unsigned) len_pos);
213   array_len = _dbus_unpack_uint32 (reader->byte_order,
214                                    _dbus_string_get_const_data_len (reader->value_str, len_pos, 4));
215
216 #if RECURSIVE_MARSHAL_READ_TRACE
217   _dbus_verbose ("   reader %p len_pos %d array len %u len_offset %d\n",
218                  reader, len_pos, array_len, reader->array_len_offset);
219 #endif
220
221   _dbus_assert (reader->u.array.start_pos - len_pos - 4 < 8);
222
223   return array_len;
224 }
225
226 static void
227 array_reader_recurse (DBusTypeReader *sub,
228                       DBusTypeReader *parent)
229 {
230   int alignment;
231   int len_pos;
232
233   array_types_only_reader_recurse (sub, parent);
234
235   sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
236
237   len_pos = sub->value_pos;
238
239   sub->value_pos += 4; /* for the length */
240
241   alignment = element_type_get_alignment (sub->type_str,
242                                           sub->type_pos);
243
244   sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
245
246   sub->u.array.start_pos = sub->value_pos;
247   _dbus_assert ((sub->u.array.start_pos - (len_pos + 4)) < 8); /* only 3 bits in array_len_offset */
248   sub->array_len_offset = sub->u.array.start_pos - (len_pos + 4);
249
250 #if RECURSIVE_MARSHAL_READ_TRACE
251   _dbus_verbose ("    type reader %p array start = %d len_offset = %d array len = %d array element type = %s\n",
252                  sub,
253                  sub->u.array.start_pos,
254                  sub->array_len_offset,
255                  array_reader_get_array_len (sub),
256                  _dbus_type_to_string (_dbus_first_type_in_signature (sub->type_str,
257                                                                 sub->type_pos)));
258 #endif
259 }
260
261 static void
262 variant_reader_recurse (DBusTypeReader *sub,
263                         DBusTypeReader *parent)
264 {
265   int sig_len;
266   int contained_alignment;
267
268   base_reader_recurse (sub, parent);
269
270   /* Variant is 1 byte sig length (without nul), signature with nul,
271    * padding to 8-boundary, then values
272    */
273
274   sig_len = _dbus_string_get_byte (sub->value_str, sub->value_pos);
275
276   sub->type_str = sub->value_str;
277   sub->type_pos = sub->value_pos + 1;
278
279   sub->value_pos = sub->type_pos + sig_len + 1;
280
281   contained_alignment = _dbus_type_get_alignment (_dbus_first_type_in_signature (sub->type_str,
282                                                                            sub->type_pos));
283   
284   sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, contained_alignment);
285
286 #if RECURSIVE_MARSHAL_READ_TRACE
287   _dbus_verbose ("    type reader %p variant containing '%s'\n",
288                  sub,
289                  _dbus_string_get_const_data_len (sub->type_str,
290                                                   sub->type_pos, 0));
291 #endif
292 }
293
294 static dbus_bool_t
295 array_reader_check_finished (const DBusTypeReader *reader)
296 {
297   int end_pos;
298
299   /* return the array element type if elements remain, and
300    * TYPE_INVALID otherwise
301    */
302
303   end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
304
305   _dbus_assert (reader->value_pos <= end_pos);
306   _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
307
308   return reader->value_pos == end_pos;
309 }
310
311 /* this is written a little oddly to try and overoptimize */
312 static void
313 skip_one_complete_type (const DBusString *type_str,
314                         int              *type_pos)
315 {
316   const unsigned char *p;
317   const unsigned char *start;
318
319   _dbus_assert (type_str != NULL);
320   _dbus_assert (type_pos != NULL);
321   
322   start = _dbus_string_get_const_data (type_str);
323   p = start + *type_pos;
324
325   _dbus_assert (*p != DBUS_STRUCT_END_CHAR);
326   _dbus_assert (*p != DBUS_DICT_ENTRY_END_CHAR);
327   
328   while (*p == DBUS_TYPE_ARRAY)
329     ++p;
330
331   _dbus_assert (*p != DBUS_STRUCT_END_CHAR);
332   _dbus_assert (*p != DBUS_DICT_ENTRY_END_CHAR);
333   
334   if (*p == DBUS_STRUCT_BEGIN_CHAR)
335     {
336       int depth;
337
338       depth = 1;
339
340       while (TRUE)
341         {
342           _dbus_assert (*p != DBUS_TYPE_INVALID);
343
344           ++p;
345
346           _dbus_assert (*p != DBUS_TYPE_INVALID);
347
348           if (*p == DBUS_STRUCT_BEGIN_CHAR)
349             depth += 1;
350           else if (*p == DBUS_STRUCT_END_CHAR)
351             {
352               depth -= 1;
353               if (depth == 0)
354                 {
355                   ++p;
356                   break;
357                 }
358             }
359         }
360     }
361   else if (*p == DBUS_DICT_ENTRY_BEGIN_CHAR)
362     {
363       int depth;
364
365       depth = 1;
366
367       while (TRUE)
368         {
369           _dbus_assert (*p != DBUS_TYPE_INVALID);
370
371           ++p;
372
373           _dbus_assert (*p != DBUS_TYPE_INVALID);
374
375           if (*p == DBUS_DICT_ENTRY_BEGIN_CHAR)
376             depth += 1;
377           else if (*p == DBUS_DICT_ENTRY_END_CHAR)
378             {
379               depth -= 1;
380               if (depth == 0)
381                 {
382                   ++p;
383                   break;
384                 }
385             }
386         }
387     }
388   else
389     {
390       ++p;
391     }
392
393   *type_pos = (int) (p - start);
394 }
395
396 static int
397 find_len_of_complete_type (const DBusString *type_str,
398                            int               type_pos)
399 {
400   int end;
401
402   end = type_pos;
403
404   skip_one_complete_type (type_str, &end);
405
406   return end - type_pos;
407 }
408
409 static void
410 base_reader_next (DBusTypeReader *reader,
411                   int             current_type)
412 {
413   switch (current_type)
414     {
415     case DBUS_TYPE_DICT_ENTRY:
416     case DBUS_TYPE_STRUCT:
417     case DBUS_TYPE_VARIANT:
418       /* Scan forward over the entire container contents */
419       {
420         DBusTypeReader sub;
421
422         if (reader->klass->types_only && current_type == DBUS_TYPE_VARIANT)
423           ;
424         else
425           {
426             /* Recurse into the struct or variant */
427             _dbus_type_reader_recurse (reader, &sub);
428
429             /* Skip everything in this subreader */
430             while (_dbus_type_reader_next (&sub))
431               {
432                 /* nothing */;
433               }
434           }
435         if (!reader->klass->types_only)
436           reader->value_pos = sub.value_pos;
437
438         /* Now we are at the end of this container; for variants, the
439          * subreader's type_pos is totally inapplicable (it's in the
440          * value string) but we know that we increment by one past the
441          * DBUS_TYPE_VARIANT
442          */
443         if (current_type == DBUS_TYPE_VARIANT)
444           reader->type_pos += 1;
445         else
446           reader->type_pos = sub.type_pos;
447       }
448       break;
449
450     case DBUS_TYPE_ARRAY:
451       {
452         if (!reader->klass->types_only)
453           _dbus_marshal_skip_array (reader->value_str,
454                                     _dbus_first_type_in_signature (reader->type_str,
455                                                                    reader->type_pos + 1),
456                                     reader->byte_order,
457                                     &reader->value_pos);
458
459         skip_one_complete_type (reader->type_str, &reader->type_pos);
460       }
461       break;
462
463     default:
464       if (!reader->klass->types_only)
465         _dbus_marshal_skip_basic (reader->value_str,
466                                   current_type, reader->byte_order,
467                                   &reader->value_pos);
468
469       reader->type_pos += 1;
470       break;
471     }
472 }
473
474 static void
475 struct_reader_next (DBusTypeReader *reader,
476                     int             current_type)
477 {
478   int t;
479
480   base_reader_next (reader, current_type);
481
482   /* for STRUCT containers we return FALSE at the end of the struct,
483    * for INVALID we return FALSE at the end of the signature.
484    * In both cases we arrange for get_current_type() to return INVALID
485    * which is defined to happen iff we're at the end (no more next())
486    */
487   t = _dbus_string_get_byte (reader->type_str, reader->type_pos);
488   if (t == DBUS_STRUCT_END_CHAR)
489     {
490       reader->type_pos += 1;
491       reader->finished = TRUE;
492     }
493 }
494
495 static void
496 dict_entry_reader_next (DBusTypeReader *reader,
497                         int             current_type)
498 {
499   int t;
500
501   base_reader_next (reader, current_type);
502
503   /* for STRUCT containers we return FALSE at the end of the struct,
504    * for INVALID we return FALSE at the end of the signature.
505    * In both cases we arrange for get_current_type() to return INVALID
506    * which is defined to happen iff we're at the end (no more next())
507    */
508   t = _dbus_string_get_byte (reader->type_str, reader->type_pos);
509   if (t == DBUS_DICT_ENTRY_END_CHAR)
510     {
511       reader->type_pos += 1;
512       reader->finished = TRUE;
513     }
514 }
515
516 static void
517 array_types_only_reader_next (DBusTypeReader *reader,
518                               int             current_type)
519 {
520   /* We have one "element" to be iterated over
521    * in each array, which is its element type.
522    * So the finished flag indicates whether we've
523    * iterated over it yet or not.
524    */
525   reader->finished = TRUE;
526 }
527
528 static void
529 array_reader_next (DBusTypeReader *reader,
530                    int             current_type)
531 {
532   /* Skip one array element */
533   int end_pos;
534
535   end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
536
537 #if RECURSIVE_MARSHAL_READ_TRACE
538   _dbus_verbose ("  reader %p array next START start_pos = %d end_pos = %d value_pos = %d current_type = %s\n",
539                  reader,
540                  reader->u.array.start_pos,
541                  end_pos, reader->value_pos,
542                  _dbus_type_to_string (current_type));
543 #endif
544
545   _dbus_assert (reader->value_pos < end_pos);
546   _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
547
548   switch (_dbus_first_type_in_signature (reader->type_str,
549                                    reader->type_pos))
550     {
551     case DBUS_TYPE_DICT_ENTRY:
552     case DBUS_TYPE_STRUCT:
553     case DBUS_TYPE_VARIANT:
554       {
555         DBusTypeReader sub;
556
557         /* Recurse into the struct or variant */
558         _dbus_type_reader_recurse (reader, &sub);
559
560         /* Skip everything in this element */
561         while (_dbus_type_reader_next (&sub))
562           {
563             /* nothing */;
564           }
565
566         /* Now we are at the end of this element */
567         reader->value_pos = sub.value_pos;
568       }
569       break;
570
571     case DBUS_TYPE_ARRAY:
572       {
573         _dbus_marshal_skip_array (reader->value_str,
574                                   _dbus_first_type_in_signature (reader->type_str,
575                                                            reader->type_pos + 1),
576                                   reader->byte_order,
577                                   &reader->value_pos);
578       }
579       break;
580
581     default:
582       {
583         _dbus_marshal_skip_basic (reader->value_str,
584                                   current_type, reader->byte_order,
585                                   &reader->value_pos);
586       }
587       break;
588     }
589
590 #if RECURSIVE_MARSHAL_READ_TRACE
591   _dbus_verbose ("  reader %p array next END start_pos = %d end_pos = %d value_pos = %d current_type = %s\n",
592                  reader,
593                  reader->u.array.start_pos,
594                  end_pos, reader->value_pos,
595                  _dbus_type_to_string (current_type));
596 #endif
597
598   _dbus_assert (reader->value_pos <= end_pos);
599
600   if (reader->value_pos == end_pos)
601     {
602       skip_one_complete_type (reader->type_str,
603                               &reader->type_pos);
604     }
605 }
606
607 static void
608 array_init_from_mark (DBusTypeReader     *reader,
609                       const DBusTypeMark *mark)
610 {
611   /* Fill in the array-specific fields from the mark. The general
612    * fields are already filled in.
613    */
614   reader->u.array.start_pos = mark->array_start_pos;
615   reader->array_len_offset = mark->array_len_offset;
616 }
617
618 static const DBusTypeReaderClass body_reader_class = {
619   "body", 0,
620   FALSE,
621   NULL, /* body is always toplevel, so doesn't get recursed into */
622   NULL,
623   base_reader_next,
624   NULL
625 };
626
627 static const DBusTypeReaderClass body_types_only_reader_class = {
628   "body types", 1,
629   TRUE,
630   NULL, /* body is always toplevel, so doesn't get recursed into */
631   NULL,
632   base_reader_next,
633   NULL
634 };
635
636 static const DBusTypeReaderClass struct_reader_class = {
637   "struct", 2,
638   FALSE,
639   struct_or_dict_entry_reader_recurse,
640   NULL,
641   struct_reader_next,
642   NULL
643 };
644
645 static const DBusTypeReaderClass struct_types_only_reader_class = {
646   "struct types", 3,
647   TRUE,
648   struct_or_dict_entry_types_only_reader_recurse,
649   NULL,
650   struct_reader_next,
651   NULL
652 };
653
654 static const DBusTypeReaderClass dict_entry_reader_class = {
655   "dict_entry", 4,
656   FALSE,
657   struct_or_dict_entry_reader_recurse,
658   NULL,
659   dict_entry_reader_next,
660   NULL
661 };
662
663 static const DBusTypeReaderClass dict_entry_types_only_reader_class = {
664   "dict_entry types", 5,
665   TRUE,
666   struct_or_dict_entry_types_only_reader_recurse,
667   NULL,
668   dict_entry_reader_next,
669   NULL
670 };
671
672 static const DBusTypeReaderClass array_reader_class = {
673   "array", 6,
674   FALSE,
675   array_reader_recurse,
676   array_reader_check_finished,
677   array_reader_next,
678   array_init_from_mark
679 };
680
681 static const DBusTypeReaderClass array_types_only_reader_class = {
682   "array types", 7,
683   TRUE,
684   array_types_only_reader_recurse,
685   NULL,
686   array_types_only_reader_next,
687   NULL
688 };
689
690 static const DBusTypeReaderClass variant_reader_class = {
691   "variant", 8,
692   FALSE,
693   variant_reader_recurse,
694   NULL,
695   base_reader_next,
696   NULL
697 };
698
699 static const DBusTypeReaderClass const *
700 all_reader_classes[] = {
701   &body_reader_class,
702   &body_types_only_reader_class,
703   &struct_reader_class,
704   &struct_types_only_reader_class,
705   &dict_entry_reader_class,
706   &dict_entry_types_only_reader_class,
707   &array_reader_class,
708   &array_types_only_reader_class,
709   &variant_reader_class
710 };
711
712 /**
713  * Initializes a type reader.
714  *
715  * @param reader the reader
716  * @param byte_order the byte order of the block to read
717  * @param type_str the signature of the block to read
718  * @param type_pos location of signature
719  * @param value_str the string containing values block
720  * @param value_pos start of values block
721  */
722 void
723 _dbus_type_reader_init (DBusTypeReader    *reader,
724                         int                byte_order,
725                         const DBusString  *type_str,
726                         int                type_pos,
727                         const DBusString  *value_str,
728                         int                value_pos)
729 {
730   reader->klass = &body_reader_class;
731
732   reader_init (reader, byte_order, type_str, type_pos,
733                value_str, value_pos);
734
735 #if RECURSIVE_MARSHAL_READ_TRACE
736   _dbus_verbose ("  type reader %p init type_pos = %d value_pos = %d remaining sig '%s'\n",
737                  reader, reader->type_pos, reader->value_pos,
738                  _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
739 #endif
740 }
741
742 /**
743  * Initializes a type reader that's been compressed into a
744  * DBusTypeMark.  The args have to be the same as those passed in to
745  * create the original #DBusTypeReader.
746  *
747  * @param reader the reader
748  * @param byte_order the byte order of the value block
749  * @param type_str string containing the type signature
750  * @param value_str string containing the values block
751  * @param mark the mark to decompress from
752  */
753 void
754 _dbus_type_reader_init_from_mark (DBusTypeReader     *reader,
755                                   int                 byte_order,
756                                   const DBusString   *type_str,
757                                   const DBusString   *value_str,
758                                   const DBusTypeMark *mark)
759 {
760   reader->klass = all_reader_classes[mark->container_type];
761
762   reader_init (reader, byte_order,
763                mark->type_pos_in_value_str ? value_str : type_str,
764                mark->type_pos,
765                value_str, mark->value_pos);
766
767   if (reader->klass->init_from_mark)
768     (* reader->klass->init_from_mark) (reader, mark);
769
770 #if RECURSIVE_MARSHAL_READ_TRACE
771   _dbus_verbose ("  type reader %p init from mark type_pos = %d value_pos = %d remaining sig '%s'\n",
772                  reader, reader->type_pos, reader->value_pos,
773                  _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
774 #endif
775 }
776
777 /**
778  * Like _dbus_type_reader_init() but the iteration is over the
779  * signature, not over values.
780  *
781  * @param reader the reader
782  * @param type_str the signature string
783  * @param type_pos location in the signature string
784  */
785 void
786 _dbus_type_reader_init_types_only (DBusTypeReader    *reader,
787                                    const DBusString  *type_str,
788                                    int                type_pos)
789 {
790   reader->klass = &body_types_only_reader_class;
791
792   reader_init (reader, DBUS_COMPILER_BYTE_ORDER /* irrelevant */,
793                type_str, type_pos, NULL, _DBUS_INT_MAX /* crashes if we screw up */);
794
795 #if RECURSIVE_MARSHAL_READ_TRACE
796   _dbus_verbose ("  type reader %p init types only type_pos = %d remaining sig '%s'\n",
797                  reader, reader->type_pos,
798                  _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
799 #endif
800 }
801
802 /**
803  * Like _dbus_type_reader_init_from_mark() but only iterates over
804  * the signature, not the values.
805  *
806  * @param reader the reader
807  * @param type_str the signature string
808  * @param mark the mark to decompress from
809  */
810 void
811 _dbus_type_reader_init_types_only_from_mark (DBusTypeReader     *reader,
812                                              const DBusString   *type_str,
813                                              const DBusTypeMark *mark)
814 {
815   reader->klass = all_reader_classes[mark->container_type];
816   _dbus_assert (reader->klass->types_only);
817   _dbus_assert (!mark->type_pos_in_value_str);
818
819   reader_init (reader, DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
820                type_str, mark->type_pos,
821                NULL, _DBUS_INT_MAX /* crashes if we screw up */);
822
823   if (reader->klass->init_from_mark)
824     (* reader->klass->init_from_mark) (reader, mark);
825
826 #if RECURSIVE_MARSHAL_READ_TRACE
827   _dbus_verbose ("  type reader %p init types only from mark type_pos = %d remaining sig '%s'\n",
828                  reader, reader->type_pos,
829                  _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
830 #endif
831 }
832
833 /**
834  * Compresses a type reader into a #DBusTypeMark, useful for example
835  * if you want to cache a bunch of positions in a block of values.
836  *
837  * @param reader the reader
838  * @param mark the mark to init
839  */
840 void
841 _dbus_type_reader_save_mark (const DBusTypeReader *reader,
842                              DBusTypeMark         *mark)
843 {
844   mark->type_pos_in_value_str = (reader->type_str == reader->value_str);
845   mark->container_type = reader->klass->id;
846   _dbus_assert (all_reader_classes[reader->klass->id] == reader->klass);
847
848   mark->type_pos = reader->type_pos;
849   mark->value_pos = reader->value_pos;
850
851   /* these are just junk if the reader isn't really an array of course */
852   mark->array_len_offset = reader->array_len_offset;
853   mark->array_start_pos = reader->u.array.start_pos;
854 }
855
856 /**
857  * Gets the type of the value the reader is currently pointing to;
858  * or for a types-only reader gets the type it's currently pointing to.
859  * If the reader is at the end of a block or end of a container such
860  * as an array, returns #DBUS_TYPE_INVALID.
861  *
862  * @param reader the reader
863  */
864 int
865 _dbus_type_reader_get_current_type (const DBusTypeReader *reader)
866 {
867   int t;
868
869   if (reader->finished ||
870       (reader->klass->check_finished &&
871        (* reader->klass->check_finished) (reader)))
872     t = DBUS_TYPE_INVALID;
873   else
874     t = _dbus_first_type_in_signature (reader->type_str,
875                                        reader->type_pos);
876
877   _dbus_assert (t != DBUS_STRUCT_END_CHAR);
878   _dbus_assert (t != DBUS_STRUCT_BEGIN_CHAR);
879   _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
880   _dbus_assert (t != DBUS_DICT_ENTRY_BEGIN_CHAR);
881   
882 #if 0
883   _dbus_verbose ("  type reader %p current type_pos = %d type = %s\n",
884                  reader, reader->type_pos,
885                  _dbus_type_to_string (t));
886 #endif
887
888   return t;
889 }
890
891 /**
892  * Gets the type of an element of the array the reader is currently
893  * pointing to. It's an error to call this if
894  * _dbus_type_reader_get_current_type() doesn't return #DBUS_TYPE_ARRAY
895  * for this reader.
896  *
897  * @param reader the reader
898  */
899 int
900 _dbus_type_reader_get_element_type (const DBusTypeReader  *reader)
901 {
902   int element_type;
903
904   _dbus_assert (_dbus_type_reader_get_current_type (reader) == DBUS_TYPE_ARRAY);
905
906   element_type = _dbus_first_type_in_signature (reader->type_str,
907                                           reader->type_pos + 1);
908
909   return element_type;
910 }
911
912 /**
913  * Gets the current position in the value block
914  * @param reader the reader
915  */
916 int
917 _dbus_type_reader_get_value_pos (const DBusTypeReader  *reader)
918 {
919   return reader->value_pos;
920 }
921
922 /**
923  * Get the address of the marshaled value in the data being read.  The
924  * address may not be aligned; you have to align it to the type of the
925  * value you want to read. Most of the demarshal routines do this for
926  * you.
927  *
928  * @param reader the reader
929  * @param value_location the address of the marshaled value
930  */
931 void
932 _dbus_type_reader_read_raw (const DBusTypeReader  *reader,
933                             const unsigned char  **value_location)
934 {
935   _dbus_assert (!reader->klass->types_only);
936
937   *value_location = _dbus_string_get_const_data_len (reader->value_str,
938                                                      reader->value_pos,
939                                                      0);
940 }
941
942 /**
943  * Reads a basic-typed value, as with _dbus_marshal_read_basic().
944  *
945  * @param reader the reader
946  * @param value the address of the value
947  */
948 void
949 _dbus_type_reader_read_basic (const DBusTypeReader    *reader,
950                               void                    *value)
951 {
952   int t;
953
954   _dbus_assert (!reader->klass->types_only);
955
956   t = _dbus_type_reader_get_current_type (reader);
957
958   _dbus_marshal_read_basic (reader->value_str,
959                             reader->value_pos,
960                             t, value,
961                             reader->byte_order,
962                             NULL);
963
964
965 #if RECURSIVE_MARSHAL_READ_TRACE
966   _dbus_verbose ("  type reader %p read basic type_pos = %d value_pos = %d remaining sig '%s'\n",
967                  reader, reader->type_pos, reader->value_pos,
968                  _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
969 #endif
970 }
971
972 /**
973  * Reads a block of fixed-length basic values, from the current point
974  * in an array to the end of the array.  Does not work for arrays of
975  * string or container types.
976  *
977  * This function returns the array in-place; it does not make a copy,
978  * and it does not swap the bytes.
979  *
980  * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
981  * and the "value" argument should be a "const double**" and so on.
982  *
983  * @param reader the reader to read from
984  * @param value place to return the array values
985  * @param n_elements place to return number of array elements
986  */
987 void
988 _dbus_type_reader_read_fixed_multi (const DBusTypeReader  *reader,
989                                     void                  *value,
990                                     int                   *n_elements)
991 {
992   int element_type;
993   int end_pos;
994   int remaining_len;
995   int alignment;
996   int total_len;
997
998   _dbus_assert (!reader->klass->types_only);
999   _dbus_assert (reader->klass == &array_reader_class);
1000
1001   element_type = _dbus_first_type_in_signature (reader->type_str,
1002                                                 reader->type_pos);
1003
1004   _dbus_assert (element_type != DBUS_TYPE_INVALID); /* why we don't use get_current_type() */
1005   _dbus_assert (_dbus_type_is_fixed (element_type));
1006
1007   alignment = _dbus_type_get_alignment (element_type);
1008
1009   _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
1010
1011   total_len = array_reader_get_array_len (reader);
1012   end_pos = reader->u.array.start_pos + total_len;
1013   remaining_len = end_pos - reader->value_pos;
1014
1015 #if RECURSIVE_MARSHAL_READ_TRACE
1016   _dbus_verbose ("end_pos %d total_len %d remaining_len %d value_pos %d\n",
1017                  end_pos, total_len, remaining_len, reader->value_pos);
1018 #endif
1019
1020   _dbus_assert (remaining_len <= total_len);
1021
1022   if (remaining_len == 0)
1023     *(const DBusBasicValue**) value = NULL;
1024   else
1025     *(const DBusBasicValue**) value =
1026       (void*) _dbus_string_get_const_data_len (reader->value_str,
1027                                                reader->value_pos,
1028                                                remaining_len);
1029
1030   *n_elements = remaining_len / alignment;
1031   _dbus_assert ((remaining_len % alignment) == 0);
1032
1033 #if RECURSIVE_MARSHAL_READ_TRACE
1034   _dbus_verbose ("  type reader %p read fixed array type_pos = %d value_pos = %d remaining sig '%s'\n",
1035                  reader, reader->type_pos, reader->value_pos,
1036                  _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
1037 #endif
1038 }
1039
1040 /**
1041  * Initialize a new reader pointing to the first type and
1042  * corresponding value that's a child of the current container. It's
1043  * an error to call this if the current type is a non-container.
1044  *
1045  * Note that DBusTypeReader traverses values, not types. So if you
1046  * have an empty array of array of int, you can't recurse into it. You
1047  * can only recurse into each element.
1048  *
1049  * @param reader the reader
1050  * @param sub a reader to init pointing to the first child
1051  */
1052 void
1053 _dbus_type_reader_recurse (DBusTypeReader *reader,
1054                            DBusTypeReader *sub)
1055 {
1056   int t;
1057
1058   t = _dbus_first_type_in_signature (reader->type_str, reader->type_pos);
1059
1060   switch (t)
1061     {
1062     case DBUS_TYPE_STRUCT:
1063       if (reader->klass->types_only)
1064         sub->klass = &struct_types_only_reader_class;
1065       else
1066         sub->klass = &struct_reader_class;
1067       break;
1068     case DBUS_TYPE_DICT_ENTRY:
1069       if (reader->klass->types_only)
1070         sub->klass = &dict_entry_types_only_reader_class;
1071       else
1072         sub->klass = &dict_entry_reader_class;
1073       break;
1074     case DBUS_TYPE_ARRAY:
1075       if (reader->klass->types_only)
1076         sub->klass = &array_types_only_reader_class;
1077       else
1078         sub->klass = &array_reader_class;
1079       break;
1080     case DBUS_TYPE_VARIANT:
1081       if (reader->klass->types_only)
1082         _dbus_assert_not_reached ("can't recurse into variant typecode");
1083       else
1084         sub->klass = &variant_reader_class;
1085       break;
1086     default:
1087       _dbus_verbose ("recursing into type %s\n", _dbus_type_to_string (t));
1088 #ifndef DBUS_DISABLE_CHECKS
1089       if (t == DBUS_TYPE_INVALID)
1090         _dbus_warn ("You can't recurse into an empty array or off the end of a message body\n");
1091 #endif /* DBUS_DISABLE_CHECKS */
1092
1093       _dbus_assert_not_reached ("don't yet handle recursing into this type");
1094     }
1095
1096   _dbus_assert (sub->klass == all_reader_classes[sub->klass->id]);
1097
1098   (* sub->klass->recurse) (sub, reader);
1099
1100 #if RECURSIVE_MARSHAL_READ_TRACE
1101   _dbus_verbose ("  type reader %p RECURSED type_pos = %d value_pos = %d remaining sig '%s'\n",
1102                  sub, sub->type_pos, sub->value_pos,
1103                  _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0));
1104 #endif
1105 }
1106
1107 /**
1108  * Skip to the next value on this "level". e.g. the next field in a
1109  * struct, the next value in an array. Returns FALSE at the end of the
1110  * current container.
1111  *
1112  * @param reader the reader
1113  * @returns FALSE if nothing more to read at or below this level
1114  */
1115 dbus_bool_t
1116 _dbus_type_reader_next (DBusTypeReader *reader)
1117 {
1118   int t;
1119
1120   t = _dbus_type_reader_get_current_type (reader);
1121
1122 #if RECURSIVE_MARSHAL_READ_TRACE
1123   _dbus_verbose ("  type reader %p START next() { type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
1124                  reader, reader->type_pos, reader->value_pos,
1125                  _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1126                  _dbus_type_to_string (t));
1127 #endif
1128
1129   if (t == DBUS_TYPE_INVALID)
1130     return FALSE;
1131
1132   (* reader->klass->next) (reader, t);
1133
1134 #if RECURSIVE_MARSHAL_READ_TRACE
1135   _dbus_verbose ("  type reader %p END next() type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
1136                  reader, reader->type_pos, reader->value_pos,
1137                  _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1138                  _dbus_type_to_string (_dbus_type_reader_get_current_type (reader)));
1139 #endif
1140
1141   return _dbus_type_reader_get_current_type (reader) != DBUS_TYPE_INVALID;
1142 }
1143
1144 /**
1145  * Check whether there's another value on this "level". e.g. the next
1146  * field in a struct, the next value in an array. Returns FALSE at the
1147  * end of the current container.
1148  *
1149  * You probably don't want to use this; it makes for an awkward for/while
1150  * loop. A nicer one is "while ((current_type = get_current_type()) != INVALID)"
1151  *
1152  * @param reader the reader
1153  * @returns FALSE if nothing more to read at or below this level
1154  */
1155 dbus_bool_t
1156 _dbus_type_reader_has_next (const DBusTypeReader *reader)
1157 {
1158   /* Not efficient but works for now. */
1159   DBusTypeReader copy;
1160
1161   copy = *reader;
1162   return _dbus_type_reader_next (&copy);
1163 }
1164
1165 /**
1166  * Gets the string and range of said string containing the signature
1167  * of the current value. Essentially a more complete version of
1168  * _dbus_type_reader_get_current_type() (returns the full type
1169  * rather than only the outside of the onion).
1170  *
1171  * Note though that the first byte in a struct signature is
1172  * #DBUS_STRUCT_BEGIN_CHAR while the current type will be
1173  * #DBUS_TYPE_STRUCT so it isn't true that the first byte of the
1174  * signature is always the same as the current type. Another
1175  * difference is that this function will still return a signature when
1176  * inside an empty array; say you recurse into empty array of int32,
1177  * the signature is "i" but the current type will always be
1178  * #DBUS_TYPE_INVALID since there are no elements to be currently
1179  * pointing to.
1180  *
1181  * @param reader the reader
1182  * @param str_p place to return the string with the type in it
1183  * @param start_p place to return start of the type
1184  * @param len_p place to return the length of the type
1185  */
1186 void
1187 _dbus_type_reader_get_signature (const DBusTypeReader  *reader,
1188                                  const DBusString     **str_p,
1189                                  int                   *start_p,
1190                                  int                   *len_p)
1191 {
1192   *str_p = reader->type_str;
1193   *start_p = reader->type_pos;
1194   *len_p = find_len_of_complete_type (reader->type_str, reader->type_pos);
1195 }
1196
1197 typedef struct
1198 {
1199   DBusString replacement;
1200   int padding;
1201 } ReplacementBlock;
1202
1203 static dbus_bool_t
1204 replacement_block_init (ReplacementBlock *block,
1205                         DBusTypeReader   *reader)
1206 {
1207   if (!_dbus_string_init (&block->replacement))
1208     return FALSE;
1209
1210   /* % 8 is the padding to have the same align properties in
1211    * our replacement string as we do at the position being replaced
1212    */
1213   block->padding = reader->value_pos % 8;
1214
1215   if (!_dbus_string_lengthen (&block->replacement, block->padding))
1216     goto oom;
1217
1218   return TRUE;
1219
1220  oom:
1221   _dbus_string_free (&block->replacement);
1222   return FALSE;
1223 }
1224
1225 static dbus_bool_t
1226 replacement_block_replace (ReplacementBlock     *block,
1227                            DBusTypeReader       *reader,
1228                            const DBusTypeReader *realign_root)
1229 {
1230   DBusTypeWriter writer;
1231   DBusTypeReader realign_reader;
1232   DBusList *fixups;
1233   int orig_len;
1234
1235   _dbus_assert (realign_root != NULL);
1236
1237   orig_len = _dbus_string_get_length (&block->replacement);
1238
1239   realign_reader = *realign_root;
1240
1241 #if RECURSIVE_MARSHAL_WRITE_TRACE
1242   _dbus_verbose ("INITIALIZING replacement block writer %p at value_pos %d\n",
1243                  &writer, _dbus_string_get_length (&block->replacement));
1244 #endif
1245   _dbus_type_writer_init_values_only (&writer,
1246                                       realign_reader.byte_order,
1247                                       realign_reader.type_str,
1248                                       realign_reader.type_pos,
1249                                       &block->replacement,
1250                                       _dbus_string_get_length (&block->replacement));
1251
1252   _dbus_assert (realign_reader.value_pos <= reader->value_pos);
1253
1254 #if RECURSIVE_MARSHAL_WRITE_TRACE
1255   _dbus_verbose ("COPYING from reader at value_pos %d to writer %p starting after value_pos %d\n",
1256                  realign_reader.value_pos, &writer, reader->value_pos);
1257 #endif
1258   fixups = NULL;
1259   if (!_dbus_type_writer_write_reader_partial (&writer,
1260                                                &realign_reader,
1261                                                reader,
1262                                                block->padding,
1263                                                _dbus_string_get_length (&block->replacement) - block->padding,
1264                                                &fixups))
1265     goto oom;
1266
1267 #if RECURSIVE_MARSHAL_WRITE_TRACE
1268   _dbus_verbose ("REPLACEMENT at padding %d len %d\n", block->padding,
1269                  _dbus_string_get_length (&block->replacement) - block->padding);
1270   _dbus_verbose_bytes_of_string (&block->replacement, block->padding,
1271                                  _dbus_string_get_length (&block->replacement) - block->padding);
1272   _dbus_verbose ("TO BE REPLACED at value_pos = %d (align pad %d) len %d realign_reader.value_pos %d\n",
1273                  reader->value_pos, reader->value_pos % 8,
1274                  realign_reader.value_pos - reader->value_pos,
1275                  realign_reader.value_pos);
1276   _dbus_verbose_bytes_of_string (reader->value_str,
1277                                  reader->value_pos,
1278                                  realign_reader.value_pos - reader->value_pos);
1279 #endif
1280
1281   /* Move the replacement into position
1282    * (realign_reader should now be at the end of the block to be replaced)
1283    */
1284   if (!_dbus_string_replace_len (&block->replacement, block->padding,
1285                                  _dbus_string_get_length (&block->replacement) - block->padding,
1286                                  (DBusString*) reader->value_str,
1287                                  reader->value_pos,
1288                                  realign_reader.value_pos - reader->value_pos))
1289     goto oom;
1290
1291   /* Process our fixups now that we can't have an OOM error */
1292   apply_and_free_fixups (&fixups, reader);
1293
1294   return TRUE;
1295
1296  oom:
1297   _dbus_string_set_length (&block->replacement, orig_len);
1298   free_fixups (&fixups);
1299   return FALSE;
1300 }
1301
1302 static void
1303 replacement_block_free (ReplacementBlock *block)
1304 {
1305   _dbus_string_free (&block->replacement);
1306 }
1307
1308 /* In the variable-length case, we have to fix alignment after we insert.
1309  * The strategy is as follows:
1310  *
1311  *  - pad a new string to have the same alignment as the
1312  *    start of the current basic value
1313  *  - write the new basic value
1314  *  - copy from the original reader to the new string,
1315  *    which will fix the alignment of types following
1316  *    the new value
1317  *    - this copy has to start at realign_root,
1318  *      but not really write anything until it
1319  *      passes the value being set
1320  *    - as an optimization, we can stop copying
1321  *      when the source and dest values are both
1322  *      on an 8-boundary, since we know all following
1323  *      padding and alignment will be identical
1324  *  - copy the new string back to the original
1325  *    string, replacing the relevant part of the
1326  *    original string
1327  *  - now any arrays in the original string that
1328  *    contained the replaced string may have the
1329  *    wrong length; so we have to fix that
1330  */
1331 static dbus_bool_t
1332 reader_set_basic_variable_length (DBusTypeReader       *reader,
1333                                   int                   current_type,
1334                                   const void           *value,
1335                                   const DBusTypeReader *realign_root)
1336 {
1337   dbus_bool_t retval;
1338   ReplacementBlock block;
1339   DBusTypeWriter writer;
1340
1341   _dbus_assert (realign_root != NULL);
1342
1343   retval = FALSE;
1344
1345   if (!replacement_block_init (&block, reader))
1346     return FALSE;
1347
1348   /* Write the new basic value */
1349 #if RECURSIVE_MARSHAL_WRITE_TRACE
1350   _dbus_verbose ("INITIALIZING writer %p to write basic value at value_pos %d of replacement string\n",
1351                  &writer, _dbus_string_get_length (&block.replacement));
1352 #endif
1353   _dbus_type_writer_init_values_only (&writer,
1354                                       reader->byte_order,
1355                                       reader->type_str,
1356                                       reader->type_pos,
1357                                       &block.replacement,
1358                                       _dbus_string_get_length (&block.replacement));
1359 #if RECURSIVE_MARSHAL_WRITE_TRACE
1360   _dbus_verbose ("WRITING basic value to writer %p (replacement string)\n", &writer);
1361 #endif
1362   if (!_dbus_type_writer_write_basic (&writer, current_type, value))
1363     goto out;
1364
1365   if (!replacement_block_replace (&block,
1366                                   reader,
1367                                   realign_root))
1368     goto out;
1369
1370   retval = TRUE;
1371
1372  out:
1373   replacement_block_free (&block);
1374   return retval;
1375 }
1376
1377 static void
1378 reader_set_basic_fixed_length (DBusTypeReader *reader,
1379                                int             current_type,
1380                                const void     *value)
1381 {
1382   _dbus_marshal_set_basic ((DBusString*) reader->value_str,
1383                            reader->value_pos,
1384                            current_type,
1385                            value,
1386                            reader->byte_order,
1387                            NULL, NULL);
1388 }
1389
1390 /**
1391  * Sets a new value for the basic type value pointed to by the reader,
1392  * leaving the reader valid to continue reading. Any other readers
1393  * will be invalidated if you set a variable-length type such as a
1394  * string.
1395  *
1396  * The provided realign_root is the reader to start from when
1397  * realigning the data that follows the newly-set value. The reader
1398  * parameter must point to a value below the realign_root parameter.
1399  * If the type being set is fixed-length, then realign_root may be
1400  * #NULL. Only values reachable from realign_root will be realigned,
1401  * so if your string contains other values you will need to deal with
1402  * those somehow yourself. It is OK if realign_root is the same
1403  * reader as the reader parameter, though if you aren't setting the
1404  * root it may not be such a good idea.
1405  *
1406  * @todo DBusTypeReader currently takes "const" versions of the type
1407  * and value strings, and this function modifies those strings by
1408  * casting away the const, which is of course bad if we want to get
1409  * picky. (To be truly clean you'd have an object which contained the
1410  * type and value strings and set_basic would be a method on that
1411  * object... this would also make DBusTypeReader the same thing as
1412  * DBusTypeMark. But since DBusMessage is effectively that object for
1413  * D-BUS it doesn't seem worth creating some random object.)
1414  *
1415  * @todo optimize this by only rewriting until the old and new values
1416  * are at the same alignment. Frequently this should result in only
1417  * replacing the value that's immediately at hand.
1418  *
1419  * @param reader reader indicating where to set a new value
1420  * @param value address of the value to set
1421  * @param realign_root realign from here
1422  * @returns #FALSE if not enough memory
1423  */
1424 dbus_bool_t
1425 _dbus_type_reader_set_basic (DBusTypeReader       *reader,
1426                              const void           *value,
1427                              const DBusTypeReader *realign_root)
1428 {
1429   int current_type;
1430
1431   _dbus_assert (!reader->klass->types_only);
1432   _dbus_assert (reader->value_str == realign_root->value_str);
1433   _dbus_assert (reader->value_pos >= realign_root->value_pos);
1434
1435   current_type = _dbus_type_reader_get_current_type (reader);
1436
1437 #if RECURSIVE_MARSHAL_WRITE_TRACE
1438   _dbus_verbose ("  SET BASIC type reader %p type_pos = %d value_pos = %d remaining sig '%s' realign_root = %p with value_pos %d current_type = %s\n",
1439                  reader, reader->type_pos, reader->value_pos,
1440                  _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1441                  realign_root,
1442                  realign_root ? realign_root->value_pos : -1,
1443                  _dbus_type_to_string (current_type));
1444   _dbus_verbose_bytes_of_string (realign_root->value_str, realign_root->value_pos,
1445                                  _dbus_string_get_length (realign_root->value_str) -
1446                                  realign_root->value_pos);
1447 #endif
1448
1449   _dbus_assert (_dbus_type_is_basic (current_type));
1450
1451   if (_dbus_type_is_fixed (current_type))
1452     {
1453       reader_set_basic_fixed_length (reader, current_type, value);
1454       return TRUE;
1455     }
1456   else
1457     {
1458       _dbus_assert (realign_root != NULL);
1459       return reader_set_basic_variable_length (reader, current_type,
1460                                                value, realign_root);
1461     }
1462 }
1463
1464 /**
1465  * Recursively deletes any value pointed to by the reader, leaving the
1466  * reader valid to continue reading. Any other readers will be
1467  * invalidated.
1468  *
1469  * The provided realign_root is the reader to start from when
1470  * realigning the data that follows the newly-set value.
1471  * See _dbus_type_reader_set_basic() for more details on the
1472  * realign_root paramter.
1473  *
1474  * @todo for now this does not delete the typecodes associated with
1475  * the value, so this function should only be used for array elements.
1476  *
1477  * @param reader reader indicating where to delete a value
1478  * @param realign_root realign from here
1479  * @returns #FALSE if not enough memory
1480  */
1481 dbus_bool_t
1482 _dbus_type_reader_delete (DBusTypeReader        *reader,
1483                           const DBusTypeReader  *realign_root)
1484 {
1485   dbus_bool_t retval;
1486   ReplacementBlock block;
1487
1488   _dbus_assert (realign_root != NULL);
1489   _dbus_assert (reader->klass == &array_reader_class);
1490
1491   retval = FALSE;
1492
1493   if (!replacement_block_init (&block, reader))
1494     return FALSE;
1495
1496   if (!replacement_block_replace (&block,
1497                                   reader,
1498                                   realign_root))
1499     goto out;
1500
1501   retval = TRUE;
1502
1503  out:
1504   replacement_block_free (&block);
1505   return retval;
1506 }
1507
1508 /**
1509  * Compares two readers, which must be iterating over the same value data.
1510  * Returns #TRUE if the first parameter is further along than the second parameter.
1511  *
1512  * @param lhs left-hand-side (first) parameter
1513  * @param rhs left-hand-side (first) parameter
1514  * @returns whether lhs is greater than rhs
1515  */
1516 dbus_bool_t
1517 _dbus_type_reader_greater_than (const DBusTypeReader  *lhs,
1518                                 const DBusTypeReader  *rhs)
1519 {
1520   _dbus_assert (lhs->value_str == rhs->value_str);
1521
1522   return lhs->value_pos > rhs->value_pos;
1523 }
1524
1525 /*
1526  *
1527  *
1528  *         DBusTypeWriter
1529  *
1530  *
1531  *
1532  */
1533
1534 /**
1535  * Initialize a write iterator, which is used to write out values in
1536  * serialized D-BUS format.
1537  *
1538  * The type_pos passed in is expected to be inside an already-valid,
1539  * though potentially empty, type signature. This means that the byte
1540  * after type_pos must be either #DBUS_TYPE_INVALID (aka nul) or some
1541  * other valid type. #DBusTypeWriter won't enforce that the signature
1542  * is already valid (you can append the nul byte at the end if you
1543  * like), but just be aware that you need the nul byte eventually and
1544  * #DBusTypeWriter isn't going to write it for you.
1545  *
1546  * @param writer the writer to init
1547  * @param byte_order the byte order to marshal into
1548  * @param type_str the string to write typecodes into
1549  * @param type_pos where to insert typecodes
1550  * @param value_str the string to write values into
1551  * @param value_pos where to insert values
1552  *
1553  */
1554 void
1555 _dbus_type_writer_init (DBusTypeWriter *writer,
1556                         int             byte_order,
1557                         DBusString     *type_str,
1558                         int             type_pos,
1559                         DBusString     *value_str,
1560                         int             value_pos)
1561 {
1562   writer->byte_order = byte_order;
1563   writer->type_str = type_str;
1564   writer->type_pos = type_pos;
1565   writer->value_str = value_str;
1566   writer->value_pos = value_pos;
1567   writer->container_type = DBUS_TYPE_INVALID;
1568   writer->type_pos_is_expectation = FALSE;
1569   writer->enabled = TRUE;
1570
1571 #if RECURSIVE_MARSHAL_WRITE_TRACE
1572   _dbus_verbose ("writer %p init remaining sig '%s'\n", writer,
1573                  writer->type_str ?
1574                  _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1575                  "unknown");
1576 #endif
1577 }
1578
1579 /**
1580  * Initialize a write iterator, with the signature to be provided
1581  * later.
1582  *
1583  * @param writer the writer to init
1584  * @param byte_order the byte order to marshal into
1585  * @param value_str the string to write values into
1586  * @param value_pos where to insert values
1587  *
1588  */
1589 void
1590 _dbus_type_writer_init_types_delayed (DBusTypeWriter *writer,
1591                                       int             byte_order,
1592                                       DBusString     *value_str,
1593                                       int             value_pos)
1594 {
1595   _dbus_type_writer_init (writer, byte_order,
1596                           NULL, 0, value_str, value_pos);
1597 }
1598
1599 /**
1600  * Adds type string to the writer, if it had none.
1601  *
1602  * @param writer the writer to init
1603  * @param type_str type string to add
1604  * @param type_pos type position
1605  *
1606  */
1607 void
1608 _dbus_type_writer_add_types (DBusTypeWriter *writer,
1609                              DBusString     *type_str,
1610                              int             type_pos)
1611 {
1612   if (writer->type_str == NULL) /* keeps us from using this as setter */
1613     {
1614       writer->type_str = type_str;
1615       writer->type_pos = type_pos;
1616     }
1617 }
1618
1619 /**
1620  * Removes type string from the writer.
1621  *
1622  * @param writer the writer to remove from
1623  */
1624 void
1625 _dbus_type_writer_remove_types (DBusTypeWriter *writer)
1626 {
1627   writer->type_str = NULL;
1628   writer->type_pos = -1;
1629 }
1630
1631 /**
1632  * Like _dbus_type_writer_init(), except the type string
1633  * passed in should correspond to an existing signature that
1634  * matches what you're going to write out. The writer will
1635  * check what you write vs. this existing signature.
1636  *
1637  * @param writer the writer to init
1638  * @param byte_order the byte order to marshal into
1639  * @param type_str the string with signature
1640  * @param type_pos start of signature
1641  * @param value_str the string to write values into
1642  * @param value_pos where to insert values
1643  *
1644  */
1645 void
1646 _dbus_type_writer_init_values_only (DBusTypeWriter   *writer,
1647                                     int               byte_order,
1648                                     const DBusString *type_str,
1649                                     int               type_pos,
1650                                     DBusString       *value_str,
1651                                     int               value_pos)
1652 {
1653   _dbus_type_writer_init (writer, byte_order,
1654                           (DBusString*)type_str, type_pos,
1655                           value_str, value_pos);
1656
1657   writer->type_pos_is_expectation = TRUE;
1658 }
1659
1660 static dbus_bool_t
1661 _dbus_type_writer_write_basic_no_typecode (DBusTypeWriter *writer,
1662                                            int             type,
1663                                            const void     *value)
1664 {
1665   if (writer->enabled)
1666     return _dbus_marshal_write_basic (writer->value_str,
1667                                       writer->value_pos,
1668                                       type,
1669                                       value,
1670                                       writer->byte_order,
1671                                       &writer->value_pos);
1672   else
1673     return TRUE;
1674 }
1675
1676 /* If our parent is an array, things are a little bit complicated.
1677  *
1678  * The parent must have a complete element type, such as
1679  * "i" or "aai" or "(ii)" or "a(ii)". There can't be
1680  * unclosed parens, or an "a" with no following type.
1681  *
1682  * To recurse, the only allowed operation is to recurse into the
1683  * first type in the element type. So for "i" you can't recurse, for
1684  * "ai" you can recurse into the array, for "(ii)" you can recurse
1685  * into the struct.
1686  *
1687  * If you recurse into the array for "ai", then you must specify
1688  * "i" for the element type of the array you recurse into.
1689  *
1690  * While inside an array at any level, we need to avoid writing to
1691  * type_str, since the type only appears once for the whole array,
1692  * it does not appear for each array element.
1693  *
1694  * While inside an array type_pos points to the expected next
1695  * typecode, rather than the next place we could write a typecode.
1696  */
1697 static void
1698 writer_recurse_init_and_check (DBusTypeWriter *writer,
1699                                int             container_type,
1700                                DBusTypeWriter *sub)
1701 {
1702   _dbus_type_writer_init (sub,
1703                           writer->byte_order,
1704                           writer->type_str,
1705                           writer->type_pos,
1706                           writer->value_str,
1707                           writer->value_pos);
1708
1709   sub->container_type = container_type;
1710
1711   if (writer->type_pos_is_expectation ||
1712       (sub->container_type == DBUS_TYPE_ARRAY || sub->container_type == DBUS_TYPE_VARIANT))
1713     sub->type_pos_is_expectation = TRUE;
1714   else
1715     sub->type_pos_is_expectation = FALSE;
1716
1717   sub->enabled = writer->enabled;
1718
1719 #ifndef DBUS_DISABLE_CHECKS
1720   if (writer->type_pos_is_expectation && writer->type_str)
1721     {
1722       int expected;
1723
1724       expected = _dbus_first_type_in_signature (writer->type_str, writer->type_pos);
1725
1726       if (expected != sub->container_type)
1727         {
1728           _dbus_warn ("Writing an element of type %s, but the expected type here is %s\n",
1729                       _dbus_type_to_string (sub->container_type),
1730                       _dbus_type_to_string (expected));
1731           _dbus_assert_not_reached ("bad array element or variant content written");
1732         }
1733     }
1734 #endif /* DBUS_DISABLE_CHECKS */
1735
1736 #if RECURSIVE_MARSHAL_WRITE_TRACE
1737   _dbus_verbose ("  type writer %p recurse parent %s type_pos = %d value_pos = %d is_expectation = %d remaining sig '%s' enabled = %d\n",
1738                  writer,
1739                  _dbus_type_to_string (writer->container_type),
1740                  writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
1741                  writer->type_str ?
1742                  _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1743                  "unknown",
1744                  writer->enabled);
1745   _dbus_verbose ("  type writer %p recurse sub %s   type_pos = %d value_pos = %d is_expectation = %d enabled = %d\n",
1746                  sub,
1747                  _dbus_type_to_string (sub->container_type),
1748                  sub->type_pos, sub->value_pos,
1749                  sub->type_pos_is_expectation,
1750                  sub->enabled);
1751 #endif
1752 }
1753
1754 static dbus_bool_t
1755 write_or_verify_typecode (DBusTypeWriter *writer,
1756                           int             typecode)
1757 {
1758   /* A subwriter inside an array or variant will have type_pos
1759    * pointing to the expected typecode; a writer not inside an array
1760    * or variant has type_pos pointing to the next place to insert a
1761    * typecode.
1762    */
1763 #if RECURSIVE_MARSHAL_WRITE_TRACE
1764   _dbus_verbose ("  type writer %p write_or_verify start type_pos = %d remaining sig '%s' enabled = %d\n",
1765                  writer, writer->type_pos,
1766                  writer->type_str ?
1767                  _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1768                  "unknown",
1769                  writer->enabled);
1770 #endif
1771
1772   if (writer->type_str == NULL)
1773     return TRUE;
1774
1775   if (writer->type_pos_is_expectation)
1776     {
1777 #ifndef DBUS_DISABLE_CHECKS
1778       {
1779         int expected;
1780
1781         expected = _dbus_string_get_byte (writer->type_str, writer->type_pos);
1782
1783         if (expected != typecode)
1784           {
1785             _dbus_warn ("Array or variant type requires that type %s be written, but %s was written\n",
1786                         _dbus_type_to_string (expected), _dbus_type_to_string (typecode));
1787             _dbus_assert_not_reached ("bad type inserted somewhere inside an array or variant");
1788           }
1789       }
1790 #endif /* DBUS_DISABLE_CHECKS */
1791
1792       /* if immediately inside an array we'd always be appending an element,
1793        * so the expected type doesn't change; if inside a struct or something
1794        * below an array, we need to move through said struct or something.
1795        */
1796       if (writer->container_type != DBUS_TYPE_ARRAY)
1797         writer->type_pos += 1;
1798     }
1799   else
1800     {
1801       if (!_dbus_string_insert_byte (writer->type_str,
1802                                      writer->type_pos,
1803                                      typecode))
1804         return FALSE;
1805
1806       writer->type_pos += 1;
1807     }
1808
1809 #if RECURSIVE_MARSHAL_WRITE_TRACE
1810   _dbus_verbose ("  type writer %p write_or_verify end type_pos = %d remaining sig '%s'\n",
1811                  writer, writer->type_pos,
1812                  _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0));
1813 #endif
1814
1815   return TRUE;
1816 }
1817
1818 static dbus_bool_t
1819 writer_recurse_struct_or_dict_entry (DBusTypeWriter   *writer,
1820                                      int               begin_char,
1821                                      const DBusString *contained_type,
1822                                      int               contained_type_start,
1823                                      int               contained_type_len,
1824                                      DBusTypeWriter   *sub)
1825 {
1826   /* FIXME right now contained_type is ignored; we could probably
1827    * almost trivially fix the code so if it's present we
1828    * write it out and then set type_pos_is_expectation
1829    */
1830
1831   /* Ensure that we'll be able to add alignment padding and the typecode */
1832   if (writer->enabled)
1833     {
1834       if (!_dbus_string_alloc_space (sub->value_str, 8))
1835         return FALSE;
1836     }
1837
1838   if (!write_or_verify_typecode (sub, begin_char))
1839     _dbus_assert_not_reached ("failed to insert struct typecode after prealloc");
1840
1841   if (writer->enabled)
1842     {
1843       if (!_dbus_string_insert_bytes (sub->value_str,
1844                                       sub->value_pos,
1845                                       _DBUS_ALIGN_VALUE (sub->value_pos, 8) - sub->value_pos,
1846                                       '\0'))
1847         _dbus_assert_not_reached ("should not have failed to insert alignment padding for struct");
1848       sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
1849     }
1850
1851   return TRUE;
1852 }
1853
1854
1855 static dbus_bool_t
1856 writer_recurse_array (DBusTypeWriter   *writer,
1857                       const DBusString *contained_type,
1858                       int               contained_type_start,
1859                       int               contained_type_len,
1860                       DBusTypeWriter   *sub,
1861                       dbus_bool_t       is_array_append)
1862 {
1863   dbus_uint32_t value = 0;
1864   int alignment;
1865   int aligned;
1866
1867 #ifndef DBUS_DISABLE_CHECKS
1868   if (writer->container_type == DBUS_TYPE_ARRAY &&
1869       writer->type_str)
1870     {
1871       if (!_dbus_string_equal_substring (contained_type,
1872                                          contained_type_start,
1873                                          contained_type_len,
1874                                          writer->type_str,
1875                                          writer->u.array.element_type_pos + 1))
1876         {
1877           _dbus_warn ("Writing an array of '%s' but this is incompatible with the expected type of elements in the parent array\n",
1878                       _dbus_string_get_const_data_len (contained_type,
1879                                                        contained_type_start,
1880                                                        contained_type_len));
1881           _dbus_assert_not_reached ("incompatible type for child array");
1882         }
1883     }
1884 #endif /* DBUS_DISABLE_CHECKS */
1885
1886   if (writer->enabled && !is_array_append)
1887     {
1888       /* 3 pad + 4 bytes for the array length, and 4 bytes possible padding
1889        * before array values
1890        */
1891       if (!_dbus_string_alloc_space (sub->value_str, 3 + 4 + 4))
1892         return FALSE;
1893     }
1894
1895   if (writer->type_str != NULL)
1896     {
1897       sub->type_pos += 1; /* move to point to the element type, since type_pos
1898                            * should be the expected type for further writes
1899                            */
1900       sub->u.array.element_type_pos = sub->type_pos;
1901     }
1902
1903   if (!writer->type_pos_is_expectation)
1904     {
1905       /* sub is a toplevel/outermost array so we need to write the type data */
1906
1907       /* alloc space for array typecode, element signature */
1908       if (!_dbus_string_alloc_space (writer->type_str, 1 + contained_type_len))
1909         return FALSE;
1910
1911       if (!_dbus_string_insert_byte (writer->type_str,
1912                                      writer->type_pos,
1913                                      DBUS_TYPE_ARRAY))
1914         _dbus_assert_not_reached ("failed to insert array typecode after prealloc");
1915
1916       if (!_dbus_string_copy_len (contained_type,
1917                                   contained_type_start, contained_type_len,
1918                                   sub->type_str,
1919                                   sub->u.array.element_type_pos))
1920         _dbus_assert_not_reached ("should not have failed to insert array element typecodes");
1921     }
1922
1923   if (writer->type_str != NULL)
1924     {
1925       /* If the parent is an array, we hold type_pos pointing at the array element type;
1926        * otherwise advance it to reflect the array value we just recursed into
1927        */
1928       if (writer->container_type != DBUS_TYPE_ARRAY)
1929         writer->type_pos += 1 + contained_type_len;
1930       else
1931         _dbus_assert (writer->type_pos_is_expectation); /* because it's an array */
1932     }
1933
1934   if (writer->enabled)
1935     {
1936       /* Write (or jump over, if is_array_append) the length */
1937       sub->u.array.len_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
1938
1939       if (is_array_append)
1940         {
1941           sub->value_pos += 4;
1942         }
1943       else
1944         {
1945           if (!_dbus_type_writer_write_basic_no_typecode (sub, DBUS_TYPE_UINT32,
1946                                                           &value))
1947             _dbus_assert_not_reached ("should not have failed to insert array len");
1948         }
1949
1950       _dbus_assert (sub->u.array.len_pos == sub->value_pos - 4);
1951
1952       /* Write alignment padding for array elements
1953        * Note that we write the padding *even for empty arrays*
1954        * to avoid wonky special cases
1955        */
1956       alignment = element_type_get_alignment (contained_type, contained_type_start);
1957
1958       aligned = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
1959       if (aligned != sub->value_pos)
1960         {
1961           if (!is_array_append)
1962             {
1963               if (!_dbus_string_insert_bytes (sub->value_str,
1964                                               sub->value_pos,
1965                                               aligned - sub->value_pos,
1966                                               '\0'))
1967                 _dbus_assert_not_reached ("should not have failed to insert alignment padding");
1968             }
1969
1970           sub->value_pos = aligned;
1971         }
1972
1973       sub->u.array.start_pos = sub->value_pos;
1974
1975       if (is_array_append)
1976         {
1977           dbus_uint32_t len;
1978
1979           _dbus_assert (_DBUS_ALIGN_VALUE (sub->u.array.len_pos, 4) ==
1980                         (unsigned) sub->u.array.len_pos);
1981           len = _dbus_unpack_uint32 (sub->byte_order,
1982                                      _dbus_string_get_const_data_len (sub->value_str,
1983                                                                       sub->u.array.len_pos,
1984                                                                       4));
1985
1986           sub->value_pos += len;
1987         }
1988     }
1989   else
1990     {
1991       /* not enabled, so we won't write the len_pos; set it to -1 to so indicate */
1992       sub->u.array.len_pos = -1;
1993       sub->u.array.start_pos = sub->value_pos;
1994     }
1995
1996   _dbus_assert (sub->u.array.len_pos < sub->u.array.start_pos);
1997   _dbus_assert (is_array_append || sub->u.array.start_pos == sub->value_pos);
1998
1999 #if RECURSIVE_MARSHAL_WRITE_TRACE
2000       _dbus_verbose ("  type writer %p recurse array done remaining sig '%s' array start_pos = %d len_pos = %d value_pos = %d\n", sub,
2001                      sub->type_str ?
2002                      _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0) :
2003                      "unknown",
2004                      sub->u.array.start_pos, sub->u.array.len_pos, sub->value_pos);
2005 #endif
2006
2007   return TRUE;
2008 }
2009
2010 /* Variant value will normally have:
2011  *   1 byte signature length not including nul
2012  *   signature typecodes (nul terminated)
2013  *   padding to alignment of contained type
2014  *   body according to signature
2015  *
2016  * The signature string can only have a single type
2017  * in it but that type may be complex/recursive.
2018  *
2019  * So a typical variant type with the integer 3 will have these
2020  * octets:
2021  *   0x1 'i' '\0' [1 byte padding to alignment boundary] 0x0 0x0 0x0 0x3
2022  *
2023  * The main world of hurt for writing out a variant is that the type
2024  * string is the same string as the value string. Which means
2025  * inserting to the type string will move the value_pos; and it means
2026  * that inserting to the type string could break type alignment.
2027  */
2028 static dbus_bool_t
2029 writer_recurse_variant (DBusTypeWriter   *writer,
2030                         const DBusString *contained_type,
2031                         int               contained_type_start,
2032                         int               contained_type_len,
2033                         DBusTypeWriter   *sub)
2034 {
2035   int contained_alignment;
2036   
2037   if (writer->enabled)
2038     {
2039       /* Allocate space for the worst case, which is 1 byte sig
2040        * length, nul byte at end of sig, and 7 bytes padding to
2041        * 8-boundary.
2042        */
2043       if (!_dbus_string_alloc_space (sub->value_str, contained_type_len + 9))
2044         return FALSE;
2045     }
2046
2047   /* write VARIANT typecode to the parent's type string */
2048   if (!write_or_verify_typecode (writer, DBUS_TYPE_VARIANT))
2049     return FALSE;
2050
2051   /* If not enabled, mark that we have no type_str anymore ... */
2052
2053   if (!writer->enabled)
2054     {
2055       sub->type_str = NULL;
2056       sub->type_pos = -1;
2057
2058       return TRUE;
2059     }
2060
2061   /* If we're enabled then continue ... */
2062
2063   if (!_dbus_string_insert_byte (sub->value_str,
2064                                  sub->value_pos,
2065                                  contained_type_len))
2066     _dbus_assert_not_reached ("should not have failed to insert variant type sig len");
2067
2068   sub->value_pos += 1;
2069
2070   /* Here we switch over to the expected type sig we're about to write */
2071   sub->type_str = sub->value_str;
2072   sub->type_pos = sub->value_pos;
2073
2074   if (!_dbus_string_copy_len (contained_type, contained_type_start, contained_type_len,
2075                               sub->value_str, sub->value_pos))
2076     _dbus_assert_not_reached ("should not have failed to insert variant type sig");
2077
2078   sub->value_pos += contained_type_len;
2079
2080   if (!_dbus_string_insert_byte (sub->value_str,
2081                                  sub->value_pos,
2082                                  DBUS_TYPE_INVALID))
2083     _dbus_assert_not_reached ("should not have failed to insert variant type nul termination");
2084
2085   sub->value_pos += 1;
2086
2087   contained_alignment = _dbus_type_get_alignment (_dbus_first_type_in_signature (contained_type, contained_type_start));
2088   
2089   if (!_dbus_string_insert_bytes (sub->value_str,
2090                                   sub->value_pos,
2091                                   _DBUS_ALIGN_VALUE (sub->value_pos, contained_alignment) - sub->value_pos,
2092                                   '\0'))
2093     _dbus_assert_not_reached ("should not have failed to insert alignment padding for variant body");
2094   sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, contained_alignment);
2095
2096   return TRUE;
2097 }
2098
2099 static dbus_bool_t
2100 _dbus_type_writer_recurse_contained_len (DBusTypeWriter   *writer,
2101                                          int               container_type,
2102                                          const DBusString *contained_type,
2103                                          int               contained_type_start,
2104                                          int               contained_type_len,
2105                                          DBusTypeWriter   *sub,
2106                                          dbus_bool_t       is_array_append)
2107 {
2108   writer_recurse_init_and_check (writer, container_type, sub);
2109
2110   switch (container_type)
2111     {
2112     case DBUS_TYPE_STRUCT:
2113       return writer_recurse_struct_or_dict_entry (writer,
2114                                                   DBUS_STRUCT_BEGIN_CHAR,
2115                                                   contained_type,
2116                                                   contained_type_start, contained_type_len,
2117                                                   sub);
2118       break;
2119     case DBUS_TYPE_DICT_ENTRY:
2120       return writer_recurse_struct_or_dict_entry (writer,
2121                                                   DBUS_DICT_ENTRY_BEGIN_CHAR,
2122                                                   contained_type,
2123                                                   contained_type_start, contained_type_len,
2124                                                   sub);
2125       break;
2126     case DBUS_TYPE_ARRAY:
2127       return writer_recurse_array (writer,
2128                                    contained_type, contained_type_start, contained_type_len,
2129                                    sub, is_array_append);
2130       break;
2131     case DBUS_TYPE_VARIANT:
2132       return writer_recurse_variant (writer,
2133                                      contained_type, contained_type_start, contained_type_len,
2134                                      sub);
2135       break;
2136     default:
2137       _dbus_assert_not_reached ("tried to recurse into type that doesn't support that");
2138       return FALSE;
2139       break;
2140     }
2141 }
2142
2143 /**
2144  * Opens a new container and writes out the initial information for that container.
2145  *
2146  * @param writer the writer
2147  * @param container_type the type of the container to open
2148  * @param contained_type the array element type or variant content type
2149  * @param contained_type_start position to look for the type
2150  * @param sub the new sub-writer to write container contents
2151  * @returns #FALSE if no memory
2152  */
2153 dbus_bool_t
2154 _dbus_type_writer_recurse (DBusTypeWriter   *writer,
2155                            int               container_type,
2156                            const DBusString *contained_type,
2157                            int               contained_type_start,
2158                            DBusTypeWriter   *sub)
2159 {
2160   int contained_type_len;
2161
2162   if (contained_type)
2163     contained_type_len = find_len_of_complete_type (contained_type, contained_type_start);
2164   else
2165     contained_type_len = 0;
2166
2167   return _dbus_type_writer_recurse_contained_len (writer, container_type,
2168                                                   contained_type,
2169                                                   contained_type_start,
2170                                                   contained_type_len,
2171                                                   sub,
2172                                                   FALSE);
2173 }
2174
2175 /**
2176  * Append to an existing array. Essentially, the writer will read an
2177  * existing length at the write location; jump over that length; and
2178  * write new fields. On unrecurse(), the existing length will be
2179  * updated.
2180  *
2181  * @param writer the writer
2182  * @param contained_type element type
2183  * @param contained_type_start position of element type
2184  * @param sub the subwriter to init
2185  * @returns #FALSE if no memory
2186  */
2187 dbus_bool_t
2188 _dbus_type_writer_append_array (DBusTypeWriter   *writer,
2189                                 const DBusString *contained_type,
2190                                 int               contained_type_start,
2191                                 DBusTypeWriter   *sub)
2192 {
2193   int contained_type_len;
2194
2195   if (contained_type)
2196     contained_type_len = find_len_of_complete_type (contained_type, contained_type_start);
2197   else
2198     contained_type_len = 0;
2199
2200   return _dbus_type_writer_recurse_contained_len (writer, DBUS_TYPE_ARRAY,
2201                                                   contained_type,
2202                                                   contained_type_start,
2203                                                   contained_type_len,
2204                                                   sub,
2205                                                   TRUE);
2206 }
2207
2208 static int
2209 writer_get_array_len (DBusTypeWriter *writer)
2210 {
2211   _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY);
2212   return writer->value_pos - writer->u.array.start_pos;
2213 }
2214
2215 /**
2216  * Closes a container created by _dbus_type_writer_recurse()
2217  * and writes any additional information to the values block.
2218  *
2219  * @param writer the writer
2220  * @param sub the sub-writer created by _dbus_type_writer_recurse()
2221  * @returns #FALSE if no memory
2222  */
2223 dbus_bool_t
2224 _dbus_type_writer_unrecurse (DBusTypeWriter *writer,
2225                              DBusTypeWriter *sub)
2226 {
2227   /* type_pos_is_expectation never gets unset once set, or we'd get all hosed */
2228   _dbus_assert (!writer->type_pos_is_expectation ||
2229                 (writer->type_pos_is_expectation && sub->type_pos_is_expectation));
2230
2231 #if RECURSIVE_MARSHAL_WRITE_TRACE
2232   _dbus_verbose ("  type writer %p unrecurse type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
2233                  writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
2234                  _dbus_type_to_string (writer->container_type));
2235   _dbus_verbose ("  type writer %p unrecurse sub type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
2236                  sub, sub->type_pos, sub->value_pos,
2237                  sub->type_pos_is_expectation,
2238                  _dbus_type_to_string (sub->container_type));
2239 #endif
2240
2241   if (sub->container_type == DBUS_TYPE_STRUCT)
2242     {
2243       if (!write_or_verify_typecode (sub, DBUS_STRUCT_END_CHAR))
2244         return FALSE;
2245     }
2246   else if (sub->container_type == DBUS_TYPE_DICT_ENTRY)
2247     {
2248       if (!write_or_verify_typecode (sub, DBUS_DICT_ENTRY_END_CHAR))
2249         return FALSE;
2250     }
2251   else if (sub->container_type == DBUS_TYPE_ARRAY)
2252     {
2253       if (sub->u.array.len_pos >= 0) /* len_pos == -1 if we weren't enabled when we passed it */
2254         {
2255           dbus_uint32_t len;
2256
2257           /* Set the array length */
2258           len = writer_get_array_len (sub);
2259           _dbus_marshal_set_uint32 (sub->value_str,
2260                                     sub->u.array.len_pos,
2261                                     len,
2262                                     sub->byte_order);
2263 #if RECURSIVE_MARSHAL_WRITE_TRACE
2264           _dbus_verbose ("    filled in sub array len to %u at len_pos %d\n",
2265                          len, sub->u.array.len_pos);
2266 #endif
2267         }
2268 #if RECURSIVE_MARSHAL_WRITE_TRACE
2269       else
2270         {
2271           _dbus_verbose ("    not filling in sub array len because we were disabled when we passed the len\n");
2272         }
2273 #endif
2274     }
2275
2276   /* Now get type_pos right for the parent writer. Here are the cases:
2277    *
2278    * Cases !writer->type_pos_is_expectation:
2279    *   (in these cases we want to update to the new insertion point)
2280    *
2281    * - if we recursed into a STRUCT then we didn't know in advance
2282    *   what the types in the struct would be; so we have to fill in
2283    *   that information now.
2284    *       writer->type_pos = sub->type_pos
2285    *
2286    * - if we recursed into anything else, we knew the full array
2287    *   type, or knew the single typecode marking VARIANT, so
2288    *   writer->type_pos is already correct.
2289    *       writer->type_pos should remain as-is
2290    *
2291    * - note that the parent is never an ARRAY or VARIANT, if it were
2292    *   then type_pos_is_expectation would be TRUE. The parent
2293    *   is thus known to be a toplevel or STRUCT.
2294    *
2295    * Cases where writer->type_pos_is_expectation:
2296    *   (in these cases we want to update to next expected type to write)
2297    *
2298    * - we recursed from STRUCT into STRUCT and we didn't increment
2299    *   type_pos in the parent just to stay consistent with the
2300    *   !writer->type_pos_is_expectation case (though we could
2301    *   special-case this in recurse_struct instead if we wanted)
2302    *       writer->type_pos = sub->type_pos
2303    *
2304    * - we recursed from STRUCT into ARRAY or VARIANT and type_pos
2305    *   for parent should have been incremented already
2306    *       writer->type_pos should remain as-is
2307    *
2308    * - we recursed from ARRAY into a sub-element, so type_pos in the
2309    *   parent is the element type and should remain the element type
2310    *   for the benefit of the next child element
2311    *       writer->type_pos should remain as-is
2312    *
2313    * - we recursed from VARIANT into its value, so type_pos in the
2314    *   parent makes no difference since there's only one value
2315    *   and we just finished writing it and won't use type_pos again
2316    *       writer->type_pos should remain as-is
2317    *
2318    *
2319    * For all these, DICT_ENTRY is the same as STRUCT
2320    */
2321   if (writer->type_str != NULL)
2322     {
2323       if ((sub->container_type == DBUS_TYPE_STRUCT ||
2324            sub->container_type == DBUS_TYPE_DICT_ENTRY) &&
2325           (writer->container_type == DBUS_TYPE_STRUCT ||
2326            writer->container_type == DBUS_TYPE_DICT_ENTRY ||
2327            writer->container_type == DBUS_TYPE_INVALID))
2328         {
2329           /* Advance the parent to the next struct field */
2330           writer->type_pos = sub->type_pos;
2331         }
2332     }
2333
2334   writer->value_pos = sub->value_pos;
2335
2336 #if RECURSIVE_MARSHAL_WRITE_TRACE
2337   _dbus_verbose ("  type writer %p unrecursed type_pos = %d value_pos = %d remaining sig '%s'\n",
2338                  writer, writer->type_pos, writer->value_pos,
2339                  writer->type_str ?
2340                  _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
2341                  "unknown");
2342 #endif
2343
2344   return TRUE;
2345 }
2346
2347 /**
2348  * Writes out a basic type.
2349  *
2350  * @param writer the writer
2351  * @param type the type to write
2352  * @param value the address of the value to write
2353  * @returns #FALSE if no memory
2354  */
2355 dbus_bool_t
2356 _dbus_type_writer_write_basic (DBusTypeWriter *writer,
2357                                int             type,
2358                                const void     *value)
2359 {
2360   dbus_bool_t retval;
2361
2362   /* First ensure that our type realloc will succeed */
2363   if (!writer->type_pos_is_expectation && writer->type_str != NULL)
2364     {
2365       if (!_dbus_string_alloc_space (writer->type_str, 1))
2366         return FALSE;
2367     }
2368
2369   retval = FALSE;
2370
2371   if (!_dbus_type_writer_write_basic_no_typecode (writer, type, value))
2372     goto out;
2373
2374   if (!write_or_verify_typecode (writer, type))
2375     _dbus_assert_not_reached ("failed to write typecode after prealloc");
2376
2377   retval = TRUE;
2378
2379  out:
2380 #if RECURSIVE_MARSHAL_WRITE_TRACE
2381   _dbus_verbose ("  type writer %p basic type_pos = %d value_pos = %d is_expectation = %d enabled = %d\n",
2382                  writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
2383                  writer->enabled);
2384 #endif
2385
2386   return retval;
2387 }
2388
2389 /**
2390  * Writes a block of fixed-length basic values, i.e. those that are
2391  * both _dbus_type_is_fixed() and _dbus_type_is_basic(). The block
2392  * must be written inside an array.
2393  *
2394  * The value parameter should be the address of said array of values,
2395  * so e.g. if it's an array of double, pass in "const double**"
2396  *
2397  * @param writer the writer
2398  * @param element_type type of stuff in the array
2399  * @param value address of the array
2400  * @param n_elements number of elements in the array
2401  * @returns #FALSE if no memory
2402  */
2403 dbus_bool_t
2404 _dbus_type_writer_write_fixed_multi (DBusTypeWriter        *writer,
2405                                      int                    element_type,
2406                                      const void            *value,
2407                                      int                    n_elements)
2408 {
2409   _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY);
2410   _dbus_assert (_dbus_type_is_fixed (element_type));
2411   _dbus_assert (writer->type_pos_is_expectation);
2412   _dbus_assert (n_elements >= 0);
2413
2414 #if RECURSIVE_MARSHAL_WRITE_TRACE
2415   _dbus_verbose ("  type writer %p entering fixed multi type_pos = %d value_pos = %d n_elements %d\n",
2416                  writer, writer->type_pos, writer->value_pos, n_elements);
2417 #endif
2418
2419   if (!write_or_verify_typecode (writer, element_type))
2420     _dbus_assert_not_reached ("OOM should not happen if only verifying typecode");
2421
2422   if (writer->enabled)
2423     {
2424       if (!_dbus_marshal_write_fixed_multi (writer->value_str,
2425                                             writer->value_pos,
2426                                             element_type,
2427                                             value,
2428                                             n_elements,
2429                                             writer->byte_order,
2430                                             &writer->value_pos))
2431         return FALSE;
2432     }
2433
2434 #if RECURSIVE_MARSHAL_WRITE_TRACE
2435   _dbus_verbose ("  type writer %p fixed multi written new type_pos = %d new value_pos = %d n_elements %d\n",
2436                  writer, writer->type_pos, writer->value_pos, n_elements);
2437 #endif
2438
2439   return TRUE;
2440 }
2441
2442 static void
2443 enable_if_after (DBusTypeWriter       *writer,
2444                  DBusTypeReader       *reader,
2445                  const DBusTypeReader *start_after)
2446 {
2447   if (start_after)
2448     {
2449       if (!writer->enabled && _dbus_type_reader_greater_than (reader, start_after))
2450         {
2451           _dbus_type_writer_set_enabled (writer, TRUE);
2452 #if RECURSIVE_MARSHAL_WRITE_TRACE
2453           _dbus_verbose ("ENABLING writer %p at %d because reader at value_pos %d is after reader at value_pos %d\n",
2454                          writer, writer->value_pos, reader->value_pos, start_after->value_pos);
2455 #endif
2456         }
2457
2458       _dbus_assert ((!writer->enabled && !_dbus_type_reader_greater_than (reader, start_after)) ||
2459                     (writer->enabled && _dbus_type_reader_greater_than (reader, start_after)));
2460     }
2461 }
2462
2463 static dbus_bool_t
2464 append_fixup (DBusList               **fixups,
2465               const DBusArrayLenFixup *fixup)
2466 {
2467   DBusArrayLenFixup *f;
2468
2469   f = dbus_new (DBusArrayLenFixup, 1);
2470   if (f == NULL)
2471     return FALSE;
2472
2473   *f = *fixup;
2474
2475   if (!_dbus_list_append (fixups, f))
2476     {
2477       dbus_free (f);
2478       return FALSE;
2479     }
2480
2481   _dbus_assert (f->len_pos_in_reader == fixup->len_pos_in_reader);
2482   _dbus_assert (f->new_len == fixup->new_len);
2483
2484   return TRUE;
2485 }
2486
2487 /* This loop is trivial if you ignore all the start_after nonsense,
2488  * so if you're trying to figure it out, start by ignoring that
2489  */
2490 static dbus_bool_t
2491 writer_write_reader_helper (DBusTypeWriter       *writer,
2492                             DBusTypeReader       *reader,
2493                             const DBusTypeReader *start_after,
2494                             int                   start_after_new_pos,
2495                             int                   start_after_new_len,
2496                             DBusList            **fixups,
2497                             dbus_bool_t           inside_start_after)
2498 {
2499   int current_type;
2500
2501   while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
2502     {
2503       if (_dbus_type_is_container (current_type))
2504         {
2505           DBusTypeReader subreader;
2506           DBusTypeWriter subwriter;
2507           const DBusString *sig_str;
2508           int sig_start;
2509           int sig_len;
2510           dbus_bool_t enabled_at_recurse;
2511           dbus_bool_t past_start_after;
2512           int reader_array_len_pos;
2513           int reader_array_start_pos;
2514           dbus_bool_t this_is_start_after;
2515
2516           /* type_pos is checked since e.g. in a struct the struct
2517            * and its first field have the same value_pos.
2518            * type_str will differ in reader/start_after for variants
2519            * where type_str is inside the value_str
2520            */
2521           if (!inside_start_after && start_after &&
2522               reader->value_pos == start_after->value_pos &&
2523               reader->type_str == start_after->type_str &&
2524               reader->type_pos == start_after->type_pos)
2525             this_is_start_after = TRUE;
2526           else
2527             this_is_start_after = FALSE;
2528
2529           _dbus_type_reader_recurse (reader, &subreader);
2530
2531           if (current_type == DBUS_TYPE_ARRAY)
2532             {
2533               reader_array_len_pos = ARRAY_READER_LEN_POS (&subreader);
2534               reader_array_start_pos = subreader.u.array.start_pos;
2535             }
2536           else
2537             {
2538               /* quiet gcc */
2539               reader_array_len_pos = -1;
2540               reader_array_start_pos = -1;
2541             }
2542
2543           _dbus_type_reader_get_signature (&subreader, &sig_str,
2544                                            &sig_start, &sig_len);
2545
2546 #if RECURSIVE_MARSHAL_WRITE_TRACE
2547           _dbus_verbose ("about to recurse into %s reader at %d subreader at %d writer at %d start_after reader at %d write target len %d inside_start_after = %d this_is_start_after = %d\n",
2548                          _dbus_type_to_string (current_type),
2549                          reader->value_pos,
2550                          subreader.value_pos,
2551                          writer->value_pos,
2552                          start_after ? start_after->value_pos : -1,
2553                          _dbus_string_get_length (writer->value_str),
2554                          inside_start_after, this_is_start_after);
2555 #endif
2556
2557           if (!inside_start_after && !this_is_start_after)
2558             enable_if_after (writer, &subreader, start_after);
2559           enabled_at_recurse = writer->enabled;
2560           if (!_dbus_type_writer_recurse_contained_len (writer, current_type,
2561                                                         sig_str, sig_start, sig_len,
2562                                                         &subwriter, FALSE))
2563             goto oom;
2564
2565 #if RECURSIVE_MARSHAL_WRITE_TRACE
2566           _dbus_verbose ("recursed into subwriter at %d write target len %d\n",
2567                          subwriter.value_pos,
2568                          _dbus_string_get_length (subwriter.value_str));
2569 #endif
2570
2571           if (!writer_write_reader_helper (&subwriter, &subreader, start_after,
2572                                            start_after_new_pos, start_after_new_len,
2573                                            fixups,
2574                                            inside_start_after ||
2575                                            this_is_start_after))
2576             goto oom;
2577
2578 #if RECURSIVE_MARSHAL_WRITE_TRACE
2579           _dbus_verbose ("about to unrecurse from %s subreader at %d writer at %d subwriter at %d  write target len %d\n",
2580                          _dbus_type_to_string (current_type),
2581                          subreader.value_pos,
2582                          writer->value_pos,
2583                          subwriter.value_pos,
2584                          _dbus_string_get_length (writer->value_str));
2585 #endif
2586
2587           if (!inside_start_after && !this_is_start_after)
2588             enable_if_after (writer, &subreader, start_after);
2589           past_start_after = writer->enabled;
2590           if (!_dbus_type_writer_unrecurse (writer, &subwriter))
2591             goto oom;
2592
2593           /* If we weren't enabled when we recursed, we didn't
2594            * write an array len; if we passed start_after
2595            * somewhere inside the array, then we need to generate
2596            * a fixup.
2597            */
2598           if (start_after != NULL &&
2599               !enabled_at_recurse && past_start_after &&
2600               current_type == DBUS_TYPE_ARRAY &&
2601               fixups != NULL)
2602             {
2603               DBusArrayLenFixup fixup;
2604               int bytes_written_after_start_after;
2605               int bytes_before_start_after;
2606               int old_len;
2607
2608               /* this subwriter access is moderately unkosher since we
2609                * already unrecursed, but it works as long as unrecurse
2610                * doesn't break us on purpose
2611                */
2612               bytes_written_after_start_after = writer_get_array_len (&subwriter);
2613
2614               bytes_before_start_after =
2615                 start_after->value_pos - reader_array_start_pos;
2616
2617               fixup.len_pos_in_reader = reader_array_len_pos;
2618               fixup.new_len =
2619                 bytes_before_start_after +
2620                 start_after_new_len +
2621                 bytes_written_after_start_after;
2622
2623               _dbus_assert (_DBUS_ALIGN_VALUE (fixup.len_pos_in_reader, 4) ==
2624                             (unsigned) fixup.len_pos_in_reader);
2625
2626               old_len = _dbus_unpack_uint32 (reader->byte_order,
2627                                              _dbus_string_get_const_data_len (reader->value_str,
2628                                                                               fixup.len_pos_in_reader, 4));
2629
2630               if (old_len != fixup.new_len && !append_fixup (fixups, &fixup))
2631                 goto oom;
2632
2633 #if RECURSIVE_MARSHAL_WRITE_TRACE
2634               _dbus_verbose ("Generated fixup len_pos_in_reader = %d new_len = %d reader_array_start_pos = %d start_after->value_pos = %d bytes_before_start_after = %d start_after_new_len = %d bytes_written_after_start_after = %d\n",
2635                              fixup.len_pos_in_reader,
2636                              fixup.new_len,
2637                              reader_array_start_pos,
2638                              start_after->value_pos,
2639                              bytes_before_start_after,
2640                              start_after_new_len,
2641                              bytes_written_after_start_after);
2642 #endif
2643             }
2644         }
2645       else
2646         {
2647           DBusBasicValue val;
2648
2649           _dbus_assert (_dbus_type_is_basic (current_type));
2650
2651 #if RECURSIVE_MARSHAL_WRITE_TRACE
2652           _dbus_verbose ("Reading basic value %s at %d\n",
2653                          _dbus_type_to_string (current_type),
2654                          reader->value_pos);
2655 #endif
2656
2657           _dbus_type_reader_read_basic (reader, &val);
2658
2659 #if RECURSIVE_MARSHAL_WRITE_TRACE
2660           _dbus_verbose ("Writing basic value %s at %d write target len %d inside_start_after = %d\n",
2661                          _dbus_type_to_string (current_type),
2662                          writer->value_pos,
2663                          _dbus_string_get_length (writer->value_str),
2664                          inside_start_after);
2665 #endif
2666           if (!inside_start_after)
2667             enable_if_after (writer, reader, start_after);
2668           if (!_dbus_type_writer_write_basic (writer, current_type, &val))
2669             goto oom;
2670 #if RECURSIVE_MARSHAL_WRITE_TRACE
2671           _dbus_verbose ("Wrote basic value %s, new value_pos %d write target len %d\n",
2672                          _dbus_type_to_string (current_type),
2673                          writer->value_pos,
2674                          _dbus_string_get_length (writer->value_str));
2675 #endif
2676         }
2677
2678       _dbus_type_reader_next (reader);
2679     }
2680
2681   return TRUE;
2682
2683  oom:
2684   if (fixups)
2685     apply_and_free_fixups (fixups, NULL); /* NULL for reader to apply to */
2686
2687   return FALSE;
2688 }
2689
2690 /**
2691  * Iterate through all values in the given reader, writing a copy of
2692  * each value to the writer.  The reader will be moved forward to its
2693  * end position.
2694  *
2695  * If a reader start_after is provided, it should be a reader for the
2696  * same data as the reader to be written. Only values occurring after
2697  * the value pointed to by start_after will be written to the writer.
2698  *
2699  * If start_after is provided, then the copy of the reader will be
2700  * partial. This means that array lengths will not have been copied.
2701  * The assumption is that you wrote a new version of the value at
2702  * start_after to the writer. You have to pass in the start position
2703  * and length of the new value. (If you are deleting the value
2704  * at start_after, pass in 0 for the length.)
2705  *
2706  * If the fixups parameter is non-#NULL, then any array length that
2707  * was read but not written due to start_after will be provided
2708  * as a #DBusArrayLenFixup. The fixup contains the position of the
2709  * array length in the source data, and the correct array length
2710  * assuming you combine the source data before start_after with
2711  * the written data at start_after and beyond.
2712  *
2713  * @param writer the writer to copy to
2714  * @param reader the reader to copy from
2715  * @param start_after #NULL or a reader showing where to start
2716  * @param start_after_new_pos the position of start_after equivalent in the target data
2717  * @param start_after_new_len the length of start_after equivalent in the target data
2718  * @param fixups list to append #DBusArrayLenFixup if the write was partial
2719  * @returns #FALSE if no memory
2720  */
2721 dbus_bool_t
2722 _dbus_type_writer_write_reader_partial (DBusTypeWriter       *writer,
2723                                         DBusTypeReader       *reader,
2724                                         const DBusTypeReader *start_after,
2725                                         int                   start_after_new_pos,
2726                                         int                   start_after_new_len,
2727                                         DBusList            **fixups)
2728 {
2729   DBusTypeWriter orig;
2730   int orig_type_len;
2731   int orig_value_len;
2732   int new_bytes;
2733   int orig_enabled;
2734
2735   orig = *writer;
2736   orig_type_len = _dbus_string_get_length (writer->type_str);
2737   orig_value_len = _dbus_string_get_length (writer->value_str);
2738   orig_enabled = writer->enabled;
2739
2740   if (start_after)
2741     _dbus_type_writer_set_enabled (writer, FALSE);
2742
2743   if (!writer_write_reader_helper (writer, reader, start_after,
2744                                    start_after_new_pos,
2745                                    start_after_new_len,
2746                                    fixups, FALSE))
2747     goto oom;
2748
2749   _dbus_type_writer_set_enabled (writer, orig_enabled);
2750   return TRUE;
2751
2752  oom:
2753   if (!writer->type_pos_is_expectation)
2754     {
2755       new_bytes = _dbus_string_get_length (writer->type_str) - orig_type_len;
2756       _dbus_string_delete (writer->type_str, orig.type_pos, new_bytes);
2757     }
2758   new_bytes = _dbus_string_get_length (writer->value_str) - orig_value_len;
2759   _dbus_string_delete (writer->value_str, orig.value_pos, new_bytes);
2760
2761   *writer = orig;
2762
2763   return FALSE;
2764 }
2765
2766 /**
2767  * Iterate through all values in the given reader, writing a copy of
2768  * each value to the writer.  The reader will be moved forward to its
2769  * end position.
2770  *
2771  * @param writer the writer to copy to
2772  * @param reader the reader to copy from
2773  * @returns #FALSE if no memory
2774  */
2775 dbus_bool_t
2776 _dbus_type_writer_write_reader (DBusTypeWriter       *writer,
2777                                 DBusTypeReader       *reader)
2778 {
2779   return _dbus_type_writer_write_reader_partial (writer, reader, NULL, 0, 0, NULL);
2780 }
2781
2782 /**
2783  * If disabled, a writer can still be iterated forward and recursed/unrecursed
2784  * but won't write any values. Types will still be written unless the
2785  * writer is a "values only" writer, because the writer needs access to
2786  * a valid signature to be able to iterate.
2787  *
2788  * @param writer the type writer
2789  * @param enabled #TRUE if values should be written
2790  */
2791 void
2792 _dbus_type_writer_set_enabled (DBusTypeWriter   *writer,
2793                                dbus_bool_t       enabled)
2794 {
2795   writer->enabled = enabled != FALSE;
2796 }
2797
2798 /** @} */ /* end of DBusMarshal group */
2799
2800 /* tests in dbus-marshal-recursive-util.c */