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