1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal-recursive.c Marshalling routines for recursive types
4 * Copyright (C) 2004, 2005 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-marshal-recursive.h"
25 #include "dbus-marshal-basic.h"
26 #include "dbus-internals.h"
29 * @addtogroup DBusMarshal
33 /** turn this on to get deluged in TypeReader verbose spam */
34 #define RECURSIVE_MARSHAL_READ_TRACE 0
36 /** turn this on to get deluged in TypeWriter verbose spam */
37 #define RECURSIVE_MARSHAL_WRITE_TRACE 0
40 free_fixups (DBusList **fixups)
44 link = _dbus_list_get_first_link (fixups);
49 next = _dbus_list_get_next_link (fixups, link);
51 dbus_free (link->data);
52 _dbus_list_free_link (link);
61 apply_and_free_fixups (DBusList **fixups,
62 DBusTypeReader *reader)
66 #if RECURSIVE_MARSHAL_WRITE_TRACE
68 _dbus_verbose (" %d FIXUPS to apply\n",
69 _dbus_list_get_length (fixups));
72 link = _dbus_list_get_first_link (fixups);
77 next = _dbus_list_get_next_link (fixups, link);
85 #if RECURSIVE_MARSHAL_WRITE_TRACE
86 _dbus_verbose (" applying FIXUP to reader %p at pos %d new_len = %d old len %d\n",
87 reader, f->len_pos_in_reader, f->new_len,
88 _dbus_marshal_read_uint32 (reader->value_str,
90 reader->byte_order, NULL));
93 _dbus_marshal_set_uint32 ((DBusString*) reader->value_str,
99 dbus_free (link->data);
100 _dbus_list_free_link (link);
109 * Virtual table for a type reader.
111 struct DBusTypeReaderClass
113 const char *name; /**< name for debugging */
114 int id; /**< index in all_reader_classes */
115 dbus_bool_t types_only; /**< only iterates over types, not values */
116 void (* recurse) (DBusTypeReader *sub,
117 DBusTypeReader *parent); /**< recurse with this reader as sub */
118 dbus_bool_t (* check_finished) (const DBusTypeReader *reader); /**< check whether reader is at the end */
119 void (* next) (DBusTypeReader *reader,
120 int current_type); /**< go to the next value */
121 void (* init_from_mark) (DBusTypeReader *reader,
122 const DBusTypeMark *mark); /**< uncompress from a mark */
126 first_type_in_signature (const DBusString *str,
131 t = _dbus_string_get_byte (str, pos);
133 if (t == DBUS_STRUCT_BEGIN_CHAR)
134 return DBUS_TYPE_STRUCT;
140 element_type_get_alignment (const DBusString *str,
143 return _dbus_type_get_alignment (first_type_in_signature (str, pos));
147 reader_init (DBusTypeReader *reader,
149 const DBusString *type_str,
151 const DBusString *value_str,
154 reader->byte_order = byte_order;
155 reader->finished = FALSE;
156 reader->type_str = type_str;
157 reader->type_pos = type_pos;
158 reader->value_str = value_str;
159 reader->value_pos = value_pos;
163 base_reader_recurse (DBusTypeReader *sub,
164 DBusTypeReader *parent)
166 /* point subreader at the same place as parent */
176 struct_types_only_reader_recurse (DBusTypeReader *sub,
177 DBusTypeReader *parent)
179 base_reader_recurse (sub, parent);
181 _dbus_assert (_dbus_string_get_byte (sub->type_str,
182 sub->type_pos) == DBUS_STRUCT_BEGIN_CHAR);
188 struct_reader_recurse (DBusTypeReader *sub,
189 DBusTypeReader *parent)
191 struct_types_only_reader_recurse (sub, parent);
193 /* struct has 8 byte alignment */
194 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
198 array_types_only_reader_recurse (DBusTypeReader *sub,
199 DBusTypeReader *parent)
201 base_reader_recurse (sub, parent);
203 /* point type_pos at the array element type */
206 /* Init with values likely to crash things if misused */
207 sub->u.array.start_pos = _DBUS_INT_MAX;
208 sub->array_len_offset = 7;
211 /** compute position of array length given array_len_offset, which is
212 the offset back from start_pos to end of the len */
213 #define ARRAY_READER_LEN_POS(reader) \
214 ((reader)->u.array.start_pos - ((int)(reader)->array_len_offset) - 4)
217 array_reader_get_array_len (const DBusTypeReader *reader)
219 dbus_uint32_t array_len;
222 len_pos = ARRAY_READER_LEN_POS (reader);
224 _dbus_assert (_DBUS_ALIGN_VALUE (len_pos, 4) == (unsigned) len_pos);
225 array_len = _dbus_unpack_uint32 (reader->byte_order,
226 _dbus_string_get_const_data_len (reader->value_str, len_pos, 4));
228 #if RECURSIVE_MARSHAL_READ_TRACE
229 _dbus_verbose (" reader %p len_pos %d array len %u len_offset %d\n",
230 reader, len_pos, array_len, reader->array_len_offset);
233 _dbus_assert (reader->u.array.start_pos - len_pos - 4 < 8);
239 array_reader_recurse (DBusTypeReader *sub,
240 DBusTypeReader *parent)
245 array_types_only_reader_recurse (sub, parent);
247 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
249 len_pos = sub->value_pos;
251 sub->value_pos += 4; /* for the length */
253 alignment = element_type_get_alignment (sub->type_str,
256 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
258 sub->u.array.start_pos = sub->value_pos;
259 _dbus_assert ((sub->u.array.start_pos - (len_pos + 4)) < 8); /* only 3 bits in array_len_offset */
260 sub->array_len_offset = sub->u.array.start_pos - (len_pos + 4);
262 #if RECURSIVE_MARSHAL_READ_TRACE
263 _dbus_verbose (" type reader %p array start = %d len_offset = %d array len = %d array element type = %s\n",
265 sub->u.array.start_pos,
266 sub->array_len_offset,
267 array_reader_get_array_len (sub),
268 _dbus_type_to_string (first_type_in_signature (sub->type_str,
274 variant_reader_recurse (DBusTypeReader *sub,
275 DBusTypeReader *parent)
279 base_reader_recurse (sub, parent);
281 /* Variant is 1 byte sig length (without nul), signature with nul,
282 * padding to 8-boundary, then values
285 sig_len = _dbus_string_get_byte (sub->value_str, sub->value_pos);
287 sub->type_str = sub->value_str;
288 sub->type_pos = sub->value_pos + 1;
290 sub->value_pos = sub->type_pos + sig_len + 1;
292 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
294 #if RECURSIVE_MARSHAL_READ_TRACE
295 _dbus_verbose (" type reader %p variant containing '%s'\n",
297 _dbus_string_get_const_data_len (sub->type_str,
303 array_reader_check_finished (const DBusTypeReader *reader)
307 /* return the array element type if elements remain, and
308 * TYPE_INVALID otherwise
311 end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
313 _dbus_assert (reader->value_pos <= end_pos);
314 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
316 return reader->value_pos == end_pos;
319 /* this is written a little oddly to try and overoptimize */
321 skip_one_complete_type (const DBusString *type_str,
324 const unsigned char *p;
325 const unsigned char *start;
327 _dbus_assert (type_str != NULL);
328 _dbus_assert (type_pos != NULL);
330 start = _dbus_string_get_const_data (type_str);
331 p = start + *type_pos;
333 _dbus_assert (*p != DBUS_STRUCT_END_CHAR);
335 while (*p == DBUS_TYPE_ARRAY)
338 _dbus_assert (*p != DBUS_STRUCT_END_CHAR);
340 if (*p == DBUS_STRUCT_BEGIN_CHAR)
348 _dbus_assert (*p != DBUS_TYPE_INVALID);
352 _dbus_assert (*p != DBUS_TYPE_INVALID);
354 if (*p == DBUS_STRUCT_BEGIN_CHAR)
356 else if (*p == DBUS_STRUCT_END_CHAR)
372 *type_pos = (int) (p - start);
376 find_len_of_complete_type (const DBusString *type_str,
383 skip_one_complete_type (type_str, &end);
385 return end - type_pos;
389 base_reader_next (DBusTypeReader *reader,
392 switch (current_type)
394 case DBUS_TYPE_STRUCT:
395 case DBUS_TYPE_VARIANT:
396 /* Scan forward over the entire container contents */
400 if (reader->klass->types_only && current_type == DBUS_TYPE_VARIANT)
404 /* Recurse into the struct or variant */
405 _dbus_type_reader_recurse (reader, &sub);
407 /* Skip everything in this subreader */
408 while (_dbus_type_reader_next (&sub))
413 if (!reader->klass->types_only)
414 reader->value_pos = sub.value_pos;
416 /* Now we are at the end of this container; for variants, the
417 * subreader's type_pos is totally inapplicable (it's in the
418 * value string) but we know that we increment by one past the
421 if (current_type == DBUS_TYPE_VARIANT)
422 reader->type_pos += 1;
424 reader->type_pos = sub.type_pos;
428 case DBUS_TYPE_ARRAY:
430 if (!reader->klass->types_only)
431 _dbus_marshal_skip_array (reader->value_str,
432 first_type_in_signature (reader->type_str,
433 reader->type_pos + 1),
437 skip_one_complete_type (reader->type_str, &reader->type_pos);
442 if (!reader->klass->types_only)
443 _dbus_marshal_skip_basic (reader->value_str,
444 current_type, reader->byte_order,
447 reader->type_pos += 1;
453 struct_reader_next (DBusTypeReader *reader,
458 base_reader_next (reader, current_type);
460 /* for STRUCT containers we return FALSE at the end of the struct,
461 * for INVALID we return FALSE at the end of the signature.
462 * In both cases we arrange for get_current_type() to return INVALID
463 * which is defined to happen iff we're at the end (no more next())
465 t = _dbus_string_get_byte (reader->type_str, reader->type_pos);
466 if (t == DBUS_STRUCT_END_CHAR)
468 reader->type_pos += 1;
469 reader->finished = TRUE;
474 array_types_only_reader_next (DBusTypeReader *reader,
477 /* We have one "element" to be iterated over
478 * in each array, which is its element type.
479 * So the finished flag indicates whether we've
480 * iterated over it yet or not.
482 reader->finished = TRUE;
486 array_reader_next (DBusTypeReader *reader,
489 /* Skip one array element */
492 end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
494 #if RECURSIVE_MARSHAL_READ_TRACE
495 _dbus_verbose (" reader %p array next START start_pos = %d end_pos = %d value_pos = %d current_type = %s\n",
497 reader->u.array.start_pos,
498 end_pos, reader->value_pos,
499 _dbus_type_to_string (current_type));
502 _dbus_assert (reader->value_pos < end_pos);
503 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
505 switch (first_type_in_signature (reader->type_str,
508 case DBUS_TYPE_STRUCT:
509 case DBUS_TYPE_VARIANT:
513 /* Recurse into the struct or variant */
514 _dbus_type_reader_recurse (reader, &sub);
516 /* Skip everything in this element */
517 while (_dbus_type_reader_next (&sub))
522 /* Now we are at the end of this element */
523 reader->value_pos = sub.value_pos;
527 case DBUS_TYPE_ARRAY:
529 _dbus_marshal_skip_array (reader->value_str,
530 first_type_in_signature (reader->type_str,
531 reader->type_pos + 1),
539 _dbus_marshal_skip_basic (reader->value_str,
540 current_type, reader->byte_order,
546 #if RECURSIVE_MARSHAL_READ_TRACE
547 _dbus_verbose (" reader %p array next END start_pos = %d end_pos = %d value_pos = %d current_type = %s\n",
549 reader->u.array.start_pos,
550 end_pos, reader->value_pos,
551 _dbus_type_to_string (current_type));
554 _dbus_assert (reader->value_pos <= end_pos);
556 if (reader->value_pos == end_pos)
558 skip_one_complete_type (reader->type_str,
564 array_init_from_mark (DBusTypeReader *reader,
565 const DBusTypeMark *mark)
567 /* Fill in the array-specific fields from the mark. The general
568 * fields are already filled in.
570 reader->u.array.start_pos = mark->array_start_pos;
571 reader->array_len_offset = mark->array_len_offset;
574 static const DBusTypeReaderClass body_reader_class = {
577 NULL, /* body is always toplevel, so doesn't get recursed into */
583 static const DBusTypeReaderClass body_types_only_reader_class = {
586 NULL, /* body is always toplevel, so doesn't get recursed into */
592 static const DBusTypeReaderClass struct_reader_class = {
595 struct_reader_recurse,
601 static const DBusTypeReaderClass struct_types_only_reader_class = {
604 struct_types_only_reader_recurse,
610 static const DBusTypeReaderClass array_reader_class = {
613 array_reader_recurse,
614 array_reader_check_finished,
619 static const DBusTypeReaderClass array_types_only_reader_class = {
622 array_types_only_reader_recurse,
624 array_types_only_reader_next,
628 static const DBusTypeReaderClass variant_reader_class = {
631 variant_reader_recurse,
637 static const DBusTypeReaderClass const *
638 all_reader_classes[] = {
640 &body_types_only_reader_class,
641 &struct_reader_class,
642 &struct_types_only_reader_class,
644 &array_types_only_reader_class,
645 &variant_reader_class
649 * Initializes a type reader.
651 * @param reader the reader
652 * @param byte_order the byte order of the block to read
653 * @param type_str the signature of the block to read
654 * @param type_pos location of signature
655 * @param value_str the string containing values block
656 * @param value_pos start of values block
659 _dbus_type_reader_init (DBusTypeReader *reader,
661 const DBusString *type_str,
663 const DBusString *value_str,
666 reader->klass = &body_reader_class;
668 reader_init (reader, byte_order, type_str, type_pos,
669 value_str, value_pos);
671 #if RECURSIVE_MARSHAL_READ_TRACE
672 _dbus_verbose (" type reader %p init type_pos = %d value_pos = %d remaining sig '%s'\n",
673 reader, reader->type_pos, reader->value_pos,
674 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
679 * Initializes a type reader that's been compressed into a
680 * DBusTypeMark. The args have to be the same as those passed in to
681 * create the original #DBusTypeReader.
683 * @param reader the reader
684 * @param byte_order the byte order of the value block
685 * @param type_str string containing the type signature
686 * @param value_str string containing the values block
687 * @param mark the mark to decompress from
690 _dbus_type_reader_init_from_mark (DBusTypeReader *reader,
692 const DBusString *type_str,
693 const DBusString *value_str,
694 const DBusTypeMark *mark)
696 reader->klass = all_reader_classes[mark->container_type];
698 reader_init (reader, byte_order,
699 mark->type_pos_in_value_str ? value_str : type_str,
701 value_str, mark->value_pos);
703 if (reader->klass->init_from_mark)
704 (* reader->klass->init_from_mark) (reader, mark);
706 #if RECURSIVE_MARSHAL_READ_TRACE
707 _dbus_verbose (" type reader %p init from mark type_pos = %d value_pos = %d remaining sig '%s'\n",
708 reader, reader->type_pos, reader->value_pos,
709 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
714 * Like _dbus_type_reader_init() but the iteration is over the
715 * signature, not over values.
717 * @param reader the reader
718 * @param type_str the signature string
719 * @param type_pos location in the signature string
722 _dbus_type_reader_init_types_only (DBusTypeReader *reader,
723 const DBusString *type_str,
726 reader->klass = &body_types_only_reader_class;
728 reader_init (reader, DBUS_COMPILER_BYTE_ORDER /* irrelevant */,
729 type_str, type_pos, NULL, _DBUS_INT_MAX /* crashes if we screw up */);
731 #if RECURSIVE_MARSHAL_READ_TRACE
732 _dbus_verbose (" type reader %p init types only type_pos = %d remaining sig '%s'\n",
733 reader, reader->type_pos,
734 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
739 * Like _dbus_type_reader_init_from_mark() but only iterates over
740 * the signature, not the values.
742 * @param reader the reader
743 * @param type_str the signature string
744 * @param mark the mark to decompress from
747 _dbus_type_reader_init_types_only_from_mark (DBusTypeReader *reader,
748 const DBusString *type_str,
749 const DBusTypeMark *mark)
751 reader->klass = all_reader_classes[mark->container_type];
752 _dbus_assert (reader->klass->types_only);
753 _dbus_assert (!mark->type_pos_in_value_str);
755 reader_init (reader, DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
756 type_str, mark->type_pos,
757 NULL, _DBUS_INT_MAX /* crashes if we screw up */);
759 if (reader->klass->init_from_mark)
760 (* reader->klass->init_from_mark) (reader, mark);
762 #if RECURSIVE_MARSHAL_READ_TRACE
763 _dbus_verbose (" type reader %p init types only from mark type_pos = %d remaining sig '%s'\n",
764 reader, reader->type_pos,
765 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
770 * Compresses a type reader into a #DBusTypeMark, useful for example
771 * if you want to cache a bunch of positions in a block of values.
773 * @param reader the reader
774 * @param mark the mark to init
777 _dbus_type_reader_save_mark (const DBusTypeReader *reader,
780 mark->type_pos_in_value_str = (reader->type_str == reader->value_str);
781 mark->container_type = reader->klass->id;
782 _dbus_assert (all_reader_classes[reader->klass->id] == reader->klass);
784 mark->type_pos = reader->type_pos;
785 mark->value_pos = reader->value_pos;
787 /* these are just junk if the reader isn't really an array of course */
788 mark->array_len_offset = reader->array_len_offset;
789 mark->array_start_pos = reader->u.array.start_pos;
793 * Gets the type of the value the reader is currently pointing to;
794 * or for a types-only reader gets the type it's currently pointing to.
795 * If the reader is at the end of a block or end of a container such
796 * as an array, returns #DBUS_TYPE_INVALID.
798 * @param reader the reader
801 _dbus_type_reader_get_current_type (const DBusTypeReader *reader)
805 if (reader->finished ||
806 (reader->klass->check_finished &&
807 (* reader->klass->check_finished) (reader)))
808 t = DBUS_TYPE_INVALID;
810 t = first_type_in_signature (reader->type_str,
813 _dbus_assert (t != DBUS_STRUCT_END_CHAR);
814 _dbus_assert (t != DBUS_STRUCT_BEGIN_CHAR);
817 _dbus_verbose (" type reader %p current type_pos = %d type = %s\n",
818 reader, reader->type_pos,
819 _dbus_type_to_string (t));
826 * Gets the type of an element of the array the reader is currently
827 * pointing to. It's an error to call this if
828 * _dbus_type_reader_get_current_type() doesn't return #DBUS_TYPE_ARRAY
831 * @param reader the reader
834 _dbus_type_reader_get_element_type (const DBusTypeReader *reader)
838 _dbus_assert (_dbus_type_reader_get_current_type (reader) == DBUS_TYPE_ARRAY);
840 element_type = first_type_in_signature (reader->type_str,
841 reader->type_pos + 1);
847 * Gets the current position in the value block
848 * @param reader the reader
851 _dbus_type_reader_get_value_pos (const DBusTypeReader *reader)
853 return reader->value_pos;
857 * Get the address of the marshaled value in the data being read. The
858 * address may not be aligned; you have to align it to the type of the
859 * value you want to read. Most of the demarshal routines do this for
862 * @param reader the reader
863 * @param value_location the address of the marshaled value
866 _dbus_type_reader_read_raw (const DBusTypeReader *reader,
867 const unsigned char **value_location)
869 _dbus_assert (!reader->klass->types_only);
871 *value_location = _dbus_string_get_const_data_len (reader->value_str,
877 * Reads a basic-typed value, as with _dbus_marshal_read_basic().
879 * @param reader the reader
880 * @param value the address of the value
883 _dbus_type_reader_read_basic (const DBusTypeReader *reader,
888 _dbus_assert (!reader->klass->types_only);
890 t = _dbus_type_reader_get_current_type (reader);
892 _dbus_marshal_read_basic (reader->value_str,
899 #if RECURSIVE_MARSHAL_READ_TRACE
900 _dbus_verbose (" type reader %p read basic type_pos = %d value_pos = %d remaining sig '%s'\n",
901 reader, reader->type_pos, reader->value_pos,
902 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
907 * Reads a block of fixed-length basic values, from the current point
908 * in an array to the end of the array. Does not work for arrays of
909 * string or container types.
911 * This function returns the array in-place; it does not make a copy,
912 * and it does not swap the bytes.
914 * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
915 * and the "value" argument should be a "const double**" and so on.
917 * @param reader the reader to read from
918 * @param value place to return the array values
919 * @param n_elements place to return number of array elements
922 _dbus_type_reader_read_fixed_multi (const DBusTypeReader *reader,
932 _dbus_assert (!reader->klass->types_only);
933 _dbus_assert (reader->klass == &array_reader_class);
935 element_type = first_type_in_signature (reader->type_str,
938 _dbus_assert (element_type != DBUS_TYPE_INVALID); /* why we don't use get_current_type() */
939 _dbus_assert (_dbus_type_is_fixed (element_type));
941 alignment = _dbus_type_get_alignment (element_type);
943 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
945 total_len = array_reader_get_array_len (reader);
946 end_pos = reader->u.array.start_pos + total_len;
947 remaining_len = end_pos - reader->value_pos;
949 #if RECURSIVE_MARSHAL_READ_TRACE
950 _dbus_verbose ("end_pos %d total_len %d remaining_len %d value_pos %d\n",
951 end_pos, total_len, remaining_len, reader->value_pos);
954 _dbus_assert (remaining_len <= total_len);
956 if (remaining_len == 0)
957 *(const DBusBasicValue**) value = NULL;
959 *(const DBusBasicValue**) value =
960 (void*) _dbus_string_get_const_data_len (reader->value_str,
964 *n_elements = remaining_len / alignment;
965 _dbus_assert ((remaining_len % alignment) == 0);
967 #if RECURSIVE_MARSHAL_READ_TRACE
968 _dbus_verbose (" type reader %p read fixed array type_pos = %d value_pos = %d remaining sig '%s'\n",
969 reader, reader->type_pos, reader->value_pos,
970 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
975 * Initialize a new reader pointing to the first type and
976 * corresponding value that's a child of the current container. It's
977 * an error to call this if the current type is a non-container.
979 * Note that DBusTypeReader traverses values, not types. So if you
980 * have an empty array of array of int, you can't recurse into it. You
981 * can only recurse into each element.
983 * @param reader the reader
984 * @param sub a reader to init pointing to the first child
987 _dbus_type_reader_recurse (DBusTypeReader *reader,
992 t = first_type_in_signature (reader->type_str, reader->type_pos);
996 case DBUS_TYPE_STRUCT:
997 if (reader->klass->types_only)
998 sub->klass = &struct_types_only_reader_class;
1000 sub->klass = &struct_reader_class;
1002 case DBUS_TYPE_ARRAY:
1003 if (reader->klass->types_only)
1004 sub->klass = &array_types_only_reader_class;
1006 sub->klass = &array_reader_class;
1008 case DBUS_TYPE_VARIANT:
1009 if (reader->klass->types_only)
1010 _dbus_assert_not_reached ("can't recurse into variant typecode");
1012 sub->klass = &variant_reader_class;
1015 _dbus_verbose ("recursing into type %s\n", _dbus_type_to_string (t));
1016 #ifndef DBUS_DISABLE_CHECKS
1017 if (t == DBUS_TYPE_INVALID)
1018 _dbus_warn ("You can't recurse into an empty array or off the end of a message body\n");
1019 #endif /* DBUS_DISABLE_CHECKS */
1021 _dbus_assert_not_reached ("don't yet handle recursing into this type");
1024 _dbus_assert (sub->klass == all_reader_classes[sub->klass->id]);
1026 (* sub->klass->recurse) (sub, reader);
1028 #if RECURSIVE_MARSHAL_READ_TRACE
1029 _dbus_verbose (" type reader %p RECURSED type_pos = %d value_pos = %d remaining sig '%s'\n",
1030 sub, sub->type_pos, sub->value_pos,
1031 _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0));
1036 * Skip to the next value on this "level". e.g. the next field in a
1037 * struct, the next value in an array. Returns FALSE at the end of the
1038 * current container.
1040 * @param reader the reader
1041 * @returns FALSE if nothing more to read at or below this level
1044 _dbus_type_reader_next (DBusTypeReader *reader)
1048 t = _dbus_type_reader_get_current_type (reader);
1050 #if RECURSIVE_MARSHAL_READ_TRACE
1051 _dbus_verbose (" type reader %p START next() { type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
1052 reader, reader->type_pos, reader->value_pos,
1053 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1054 _dbus_type_to_string (t));
1057 if (t == DBUS_TYPE_INVALID)
1060 (* reader->klass->next) (reader, t);
1062 #if RECURSIVE_MARSHAL_READ_TRACE
1063 _dbus_verbose (" type reader %p END next() type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
1064 reader, reader->type_pos, reader->value_pos,
1065 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1066 _dbus_type_to_string (_dbus_type_reader_get_current_type (reader)));
1069 return _dbus_type_reader_get_current_type (reader) != DBUS_TYPE_INVALID;
1073 * Check whether there's another value on this "level". e.g. the next
1074 * field in a struct, the next value in an array. Returns FALSE at the
1075 * end of the current container.
1077 * You probably don't want to use this; it makes for an awkward for/while
1078 * loop. A nicer one is "while ((current_type = get_current_type()) != INVALID)"
1080 * @param reader the reader
1081 * @returns FALSE if nothing more to read at or below this level
1084 _dbus_type_reader_has_next (const DBusTypeReader *reader)
1086 /* Not efficient but works for now. */
1087 DBusTypeReader copy;
1090 return _dbus_type_reader_next (©);
1094 * Gets the string and range of said string containing the signature
1095 * of the current value. Essentially a more complete version of
1096 * _dbus_type_reader_get_current_type() (returns the full type
1097 * rather than only the outside of the onion).
1099 * Note though that the first byte in a struct signature is
1100 * #DBUS_STRUCT_BEGIN_CHAR while the current type will be
1101 * #DBUS_TYPE_STRUCT so it isn't true that the first byte of the
1102 * signature is always the same as the current type. Another
1103 * difference is that this function will still return a signature when
1104 * inside an empty array; say you recurse into empty array of int32,
1105 * the signature is "i" but the current type will always be
1106 * #DBUS_TYPE_INVALID since there are no elements to be currently
1109 * @param reader the reader
1110 * @param str_p place to return the string with the type in it
1111 * @param start_p place to return start of the type
1112 * @param len_p place to return the length of the type
1115 _dbus_type_reader_get_signature (const DBusTypeReader *reader,
1116 const DBusString **str_p,
1120 *str_p = reader->type_str;
1121 *start_p = reader->type_pos;
1122 *len_p = find_len_of_complete_type (reader->type_str, reader->type_pos);
1127 DBusString replacement;
1132 replacement_block_init (ReplacementBlock *block,
1133 DBusTypeReader *reader)
1135 if (!_dbus_string_init (&block->replacement))
1138 /* % 8 is the padding to have the same align properties in
1139 * our replacement string as we do at the position being replaced
1141 block->padding = reader->value_pos % 8;
1143 if (!_dbus_string_lengthen (&block->replacement, block->padding))
1149 _dbus_string_free (&block->replacement);
1154 replacement_block_replace (ReplacementBlock *block,
1155 DBusTypeReader *reader,
1156 const DBusTypeReader *realign_root)
1158 DBusTypeWriter writer;
1159 DBusTypeReader realign_reader;
1163 _dbus_assert (realign_root != NULL);
1165 orig_len = _dbus_string_get_length (&block->replacement);
1167 realign_reader = *realign_root;
1169 #if RECURSIVE_MARSHAL_WRITE_TRACE
1170 _dbus_verbose ("INITIALIZING replacement block writer %p at value_pos %d\n",
1171 &writer, _dbus_string_get_length (&block->replacement));
1173 _dbus_type_writer_init_values_only (&writer,
1174 realign_reader.byte_order,
1175 realign_reader.type_str,
1176 realign_reader.type_pos,
1177 &block->replacement,
1178 _dbus_string_get_length (&block->replacement));
1180 _dbus_assert (realign_reader.value_pos <= reader->value_pos);
1182 #if RECURSIVE_MARSHAL_WRITE_TRACE
1183 _dbus_verbose ("COPYING from reader at value_pos %d to writer %p starting after value_pos %d\n",
1184 realign_reader.value_pos, &writer, reader->value_pos);
1187 if (!_dbus_type_writer_write_reader_partial (&writer,
1191 _dbus_string_get_length (&block->replacement) - block->padding,
1195 #if RECURSIVE_MARSHAL_WRITE_TRACE
1196 _dbus_verbose ("REPLACEMENT at padding %d len %d\n", block->padding,
1197 _dbus_string_get_length (&block->replacement) - block->padding);
1198 _dbus_verbose_bytes_of_string (&block->replacement, block->padding,
1199 _dbus_string_get_length (&block->replacement) - block->padding);
1200 _dbus_verbose ("TO BE REPLACED at value_pos = %d (align pad %d) len %d realign_reader.value_pos %d\n",
1201 reader->value_pos, reader->value_pos % 8,
1202 realign_reader.value_pos - reader->value_pos,
1203 realign_reader.value_pos);
1204 _dbus_verbose_bytes_of_string (reader->value_str,
1206 realign_reader.value_pos - reader->value_pos);
1209 /* Move the replacement into position
1210 * (realign_reader should now be at the end of the block to be replaced)
1212 if (!_dbus_string_replace_len (&block->replacement, block->padding,
1213 _dbus_string_get_length (&block->replacement) - block->padding,
1214 (DBusString*) reader->value_str,
1216 realign_reader.value_pos - reader->value_pos))
1219 /* Process our fixups now that we can't have an OOM error */
1220 apply_and_free_fixups (&fixups, reader);
1225 _dbus_string_set_length (&block->replacement, orig_len);
1226 free_fixups (&fixups);
1231 replacement_block_free (ReplacementBlock *block)
1233 _dbus_string_free (&block->replacement);
1236 /* In the variable-length case, we have to fix alignment after we insert.
1237 * The strategy is as follows:
1239 * - pad a new string to have the same alignment as the
1240 * start of the current basic value
1241 * - write the new basic value
1242 * - copy from the original reader to the new string,
1243 * which will fix the alignment of types following
1245 * - this copy has to start at realign_root,
1246 * but not really write anything until it
1247 * passes the value being set
1248 * - as an optimization, we can stop copying
1249 * when the source and dest values are both
1250 * on an 8-boundary, since we know all following
1251 * padding and alignment will be identical
1252 * - copy the new string back to the original
1253 * string, replacing the relevant part of the
1255 * - now any arrays in the original string that
1256 * contained the replaced string may have the
1257 * wrong length; so we have to fix that
1260 reader_set_basic_variable_length (DBusTypeReader *reader,
1263 const DBusTypeReader *realign_root)
1266 ReplacementBlock block;
1267 DBusTypeWriter writer;
1269 _dbus_assert (realign_root != NULL);
1273 if (!replacement_block_init (&block, reader))
1276 /* Write the new basic value */
1277 #if RECURSIVE_MARSHAL_WRITE_TRACE
1278 _dbus_verbose ("INITIALIZING writer %p to write basic value at value_pos %d of replacement string\n",
1279 &writer, _dbus_string_get_length (&block.replacement));
1281 _dbus_type_writer_init_values_only (&writer,
1286 _dbus_string_get_length (&block.replacement));
1287 #if RECURSIVE_MARSHAL_WRITE_TRACE
1288 _dbus_verbose ("WRITING basic value to writer %p (replacement string)\n", &writer);
1290 if (!_dbus_type_writer_write_basic (&writer, current_type, value))
1293 if (!replacement_block_replace (&block,
1301 replacement_block_free (&block);
1306 reader_set_basic_fixed_length (DBusTypeReader *reader,
1310 _dbus_marshal_set_basic ((DBusString*) reader->value_str,
1319 * Sets a new value for the basic type value pointed to by the reader,
1320 * leaving the reader valid to continue reading. Any other readers
1321 * will be invalidated if you set a variable-length type such as a
1324 * The provided realign_root is the reader to start from when
1325 * realigning the data that follows the newly-set value. The reader
1326 * parameter must point to a value below the realign_root parameter.
1327 * If the type being set is fixed-length, then realign_root may be
1328 * #NULL. Only values reachable from realign_root will be realigned,
1329 * so if your string contains other values you will need to deal with
1330 * those somehow yourself. It is OK if realign_root is the same
1331 * reader as the reader parameter, though if you aren't setting the
1332 * root it may not be such a good idea.
1334 * @todo DBusTypeReader currently takes "const" versions of the type
1335 * and value strings, and this function modifies those strings by
1336 * casting away the const, which is of course bad if we want to get
1337 * picky. (To be truly clean you'd have an object which contained the
1338 * type and value strings and set_basic would be a method on that
1339 * object... this would also make DBusTypeReader the same thing as
1340 * DBusTypeMark. But since DBusMessage is effectively that object for
1341 * D-BUS it doesn't seem worth creating some random object.)
1343 * @todo optimize this by only rewriting until the old and new values
1344 * are at the same alignment. Frequently this should result in only
1345 * replacing the value that's immediately at hand.
1347 * @param reader reader indicating where to set a new value
1348 * @param value address of the value to set
1349 * @param realign_root realign from here
1350 * @returns #FALSE if not enough memory
1353 _dbus_type_reader_set_basic (DBusTypeReader *reader,
1355 const DBusTypeReader *realign_root)
1359 _dbus_assert (!reader->klass->types_only);
1360 _dbus_assert (reader->value_str == realign_root->value_str);
1361 _dbus_assert (reader->value_pos >= realign_root->value_pos);
1363 current_type = _dbus_type_reader_get_current_type (reader);
1365 #if RECURSIVE_MARSHAL_WRITE_TRACE
1366 _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",
1367 reader, reader->type_pos, reader->value_pos,
1368 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1370 realign_root ? realign_root->value_pos : -1,
1371 _dbus_type_to_string (current_type));
1372 _dbus_verbose_bytes_of_string (realign_root->value_str, realign_root->value_pos,
1373 _dbus_string_get_length (realign_root->value_str) -
1374 realign_root->value_pos);
1377 _dbus_assert (_dbus_type_is_basic (current_type));
1379 if (_dbus_type_is_fixed (current_type))
1381 reader_set_basic_fixed_length (reader, current_type, value);
1386 _dbus_assert (realign_root != NULL);
1387 return reader_set_basic_variable_length (reader, current_type,
1388 value, realign_root);
1393 * Recursively deletes any value pointed to by the reader, leaving the
1394 * reader valid to continue reading. Any other readers will be
1397 * The provided realign_root is the reader to start from when
1398 * realigning the data that follows the newly-set value.
1399 * See _dbus_type_reader_set_basic() for more details on the
1400 * realign_root paramter.
1402 * @todo for now this does not delete the typecodes associated with
1403 * the value, so this function should only be used for array elements.
1405 * @param reader reader indicating where to delete a value
1406 * @param realign_root realign from here
1407 * @returns #FALSE if not enough memory
1410 _dbus_type_reader_delete (DBusTypeReader *reader,
1411 const DBusTypeReader *realign_root)
1414 ReplacementBlock block;
1416 _dbus_assert (realign_root != NULL);
1417 _dbus_assert (reader->klass == &array_reader_class);
1421 if (!replacement_block_init (&block, reader))
1424 if (!replacement_block_replace (&block,
1432 replacement_block_free (&block);
1437 * Compares two readers, which must be iterating over the same value data.
1438 * Returns #TRUE if the first parameter is further along than the second parameter.
1440 * @param lhs left-hand-side (first) parameter
1441 * @param rhs left-hand-side (first) parameter
1442 * @returns whether lhs is greater than rhs
1445 _dbus_type_reader_greater_than (const DBusTypeReader *lhs,
1446 const DBusTypeReader *rhs)
1448 _dbus_assert (lhs->value_str == rhs->value_str);
1450 return lhs->value_pos > rhs->value_pos;
1463 * Initialize a write iterator, which is used to write out values in
1464 * serialized D-BUS format.
1466 * The type_pos passed in is expected to be inside an already-valid,
1467 * though potentially empty, type signature. This means that the byte
1468 * after type_pos must be either #DBUS_TYPE_INVALID (aka nul) or some
1469 * other valid type. #DBusTypeWriter won't enforce that the signature
1470 * is already valid (you can append the nul byte at the end if you
1471 * like), but just be aware that you need the nul byte eventually and
1472 * #DBusTypeWriter isn't going to write it for you.
1474 * @param writer the writer to init
1475 * @param byte_order the byte order to marshal into
1476 * @param type_str the string to write typecodes into
1477 * @param type_pos where to insert typecodes
1478 * @param value_str the string to write values into
1479 * @param value_pos where to insert values
1483 _dbus_type_writer_init (DBusTypeWriter *writer,
1485 DBusString *type_str,
1487 DBusString *value_str,
1490 writer->byte_order = byte_order;
1491 writer->type_str = type_str;
1492 writer->type_pos = type_pos;
1493 writer->value_str = value_str;
1494 writer->value_pos = value_pos;
1495 writer->container_type = DBUS_TYPE_INVALID;
1496 writer->type_pos_is_expectation = FALSE;
1497 writer->enabled = TRUE;
1499 #if RECURSIVE_MARSHAL_WRITE_TRACE
1500 _dbus_verbose ("writer %p init remaining sig '%s'\n", writer,
1502 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1508 * Initialize a write iterator, with the signature to be provided
1511 * @param writer the writer to init
1512 * @param byte_order the byte order to marshal into
1513 * @param value_str the string to write values into
1514 * @param value_pos where to insert values
1518 _dbus_type_writer_init_types_delayed (DBusTypeWriter *writer,
1520 DBusString *value_str,
1523 _dbus_type_writer_init (writer, byte_order,
1524 NULL, 0, value_str, value_pos);
1528 * Adds type string to the writer, if it had none.
1530 * @param writer the writer to init
1531 * @param type_str type string to add
1532 * @param type_pos type position
1536 _dbus_type_writer_add_types (DBusTypeWriter *writer,
1537 DBusString *type_str,
1540 if (writer->type_str == NULL) /* keeps us from using this as setter */
1542 writer->type_str = type_str;
1543 writer->type_pos = type_pos;
1548 * Removes type string from the writer.
1550 * @param writer the writer to remove from
1553 _dbus_type_writer_remove_types (DBusTypeWriter *writer)
1555 writer->type_str = NULL;
1556 writer->type_pos = -1;
1560 * Like _dbus_type_writer_init(), except the type string
1561 * passed in should correspond to an existing signature that
1562 * matches what you're going to write out. The writer will
1563 * check what you write vs. this existing signature.
1565 * @param writer the writer to init
1566 * @param byte_order the byte order to marshal into
1567 * @param type_str the string with signature
1568 * @param type_pos start of signature
1569 * @param value_str the string to write values into
1570 * @param value_pos where to insert values
1574 _dbus_type_writer_init_values_only (DBusTypeWriter *writer,
1576 const DBusString *type_str,
1578 DBusString *value_str,
1581 _dbus_type_writer_init (writer, byte_order,
1582 (DBusString*)type_str, type_pos,
1583 value_str, value_pos);
1585 writer->type_pos_is_expectation = TRUE;
1589 _dbus_type_writer_write_basic_no_typecode (DBusTypeWriter *writer,
1593 if (writer->enabled)
1594 return _dbus_marshal_write_basic (writer->value_str,
1599 &writer->value_pos);
1604 /* If our parent is an array, things are a little bit complicated.
1606 * The parent must have a complete element type, such as
1607 * "i" or "aai" or "(ii)" or "a(ii)". There can't be
1608 * unclosed parens, or an "a" with no following type.
1610 * To recurse, the only allowed operation is to recurse into the
1611 * first type in the element type. So for "i" you can't recurse, for
1612 * "ai" you can recurse into the array, for "(ii)" you can recurse
1615 * If you recurse into the array for "ai", then you must specify
1616 * "i" for the element type of the array you recurse into.
1618 * While inside an array at any level, we need to avoid writing to
1619 * type_str, since the type only appears once for the whole array,
1620 * it does not appear for each array element.
1622 * While inside an array type_pos points to the expected next
1623 * typecode, rather than the next place we could write a typecode.
1626 writer_recurse_init_and_check (DBusTypeWriter *writer,
1628 DBusTypeWriter *sub)
1630 _dbus_type_writer_init (sub,
1637 sub->container_type = container_type;
1639 if (writer->type_pos_is_expectation ||
1640 (sub->container_type == DBUS_TYPE_ARRAY || sub->container_type == DBUS_TYPE_VARIANT))
1641 sub->type_pos_is_expectation = TRUE;
1643 sub->type_pos_is_expectation = FALSE;
1645 sub->enabled = writer->enabled;
1647 #ifndef DBUS_DISABLE_CHECKS
1648 if (writer->type_pos_is_expectation && writer->type_str)
1652 expected = first_type_in_signature (writer->type_str, writer->type_pos);
1654 if (expected != sub->container_type)
1656 _dbus_warn ("Writing an element of type %s, but the expected type here is %s\n",
1657 _dbus_type_to_string (sub->container_type),
1658 _dbus_type_to_string (expected));
1659 _dbus_assert_not_reached ("bad array element or variant content written");
1662 #endif /* DBUS_DISABLE_CHECKS */
1664 #if RECURSIVE_MARSHAL_WRITE_TRACE
1665 _dbus_verbose (" type writer %p recurse parent %s type_pos = %d value_pos = %d is_expectation = %d remaining sig '%s' enabled = %d\n",
1667 _dbus_type_to_string (writer->container_type),
1668 writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
1670 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1673 _dbus_verbose (" type writer %p recurse sub %s type_pos = %d value_pos = %d is_expectation = %d enabled = %d\n",
1675 _dbus_type_to_string (sub->container_type),
1676 sub->type_pos, sub->value_pos,
1677 sub->type_pos_is_expectation,
1683 write_or_verify_typecode (DBusTypeWriter *writer,
1686 /* A subwriter inside an array or variant will have type_pos
1687 * pointing to the expected typecode; a writer not inside an array
1688 * or variant has type_pos pointing to the next place to insert a
1691 #if RECURSIVE_MARSHAL_WRITE_TRACE
1692 _dbus_verbose (" type writer %p write_or_verify start type_pos = %d remaining sig '%s' enabled = %d\n",
1693 writer, writer->type_pos,
1695 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1700 if (writer->type_str == NULL)
1703 if (writer->type_pos_is_expectation)
1705 #ifndef DBUS_DISABLE_CHECKS
1709 expected = _dbus_string_get_byte (writer->type_str, writer->type_pos);
1711 if (expected != typecode)
1713 _dbus_warn ("Array or variant type requires that type %s be written, but %s was written\n",
1714 _dbus_type_to_string (expected), _dbus_type_to_string (typecode));
1715 _dbus_assert_not_reached ("bad type inserted somewhere inside an array or variant");
1718 #endif /* DBUS_DISABLE_CHECKS */
1720 /* if immediately inside an array we'd always be appending an element,
1721 * so the expected type doesn't change; if inside a struct or something
1722 * below an array, we need to move through said struct or something.
1724 if (writer->container_type != DBUS_TYPE_ARRAY)
1725 writer->type_pos += 1;
1729 if (!_dbus_string_insert_byte (writer->type_str,
1734 writer->type_pos += 1;
1737 #if RECURSIVE_MARSHAL_WRITE_TRACE
1738 _dbus_verbose (" type writer %p write_or_verify end type_pos = %d remaining sig '%s'\n",
1739 writer, writer->type_pos,
1740 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0));
1747 writer_recurse_struct (DBusTypeWriter *writer,
1748 const DBusString *contained_type,
1749 int contained_type_start,
1750 int contained_type_len,
1751 DBusTypeWriter *sub)
1753 /* FIXME right now contained_type is ignored; we could probably
1754 * almost trivially fix the code so if it's present we
1755 * write it out and then set type_pos_is_expectation
1758 /* Ensure that we'll be able to add alignment padding and the typecode */
1759 if (writer->enabled)
1761 if (!_dbus_string_alloc_space (sub->value_str, 8))
1765 if (!write_or_verify_typecode (sub, DBUS_STRUCT_BEGIN_CHAR))
1766 _dbus_assert_not_reached ("failed to insert struct typecode after prealloc");
1768 if (writer->enabled)
1770 if (!_dbus_string_insert_bytes (sub->value_str,
1772 _DBUS_ALIGN_VALUE (sub->value_pos, 8) - sub->value_pos,
1774 _dbus_assert_not_reached ("should not have failed to insert alignment padding for struct");
1775 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
1783 writer_recurse_array (DBusTypeWriter *writer,
1784 const DBusString *contained_type,
1785 int contained_type_start,
1786 int contained_type_len,
1787 DBusTypeWriter *sub,
1788 dbus_bool_t is_array_append)
1790 dbus_uint32_t value = 0;
1794 #ifndef DBUS_DISABLE_CHECKS
1795 if (writer->container_type == DBUS_TYPE_ARRAY &&
1798 if (!_dbus_string_equal_substring (contained_type,
1799 contained_type_start,
1802 writer->u.array.element_type_pos + 1))
1804 _dbus_warn ("Writing an array of '%s' but this is incompatible with the expected type of elements in the parent array\n",
1805 _dbus_string_get_const_data_len (contained_type,
1806 contained_type_start,
1807 contained_type_len));
1808 _dbus_assert_not_reached ("incompatible type for child array");
1811 #endif /* DBUS_DISABLE_CHECKS */
1813 if (writer->enabled && !is_array_append)
1815 /* 3 pad + 4 bytes for the array length, and 4 bytes possible padding
1816 * before array values
1818 if (!_dbus_string_alloc_space (sub->value_str, 3 + 4 + 4))
1822 if (writer->type_str != NULL)
1824 sub->type_pos += 1; /* move to point to the element type, since type_pos
1825 * should be the expected type for further writes
1827 sub->u.array.element_type_pos = sub->type_pos;
1830 if (!writer->type_pos_is_expectation)
1832 /* sub is a toplevel/outermost array so we need to write the type data */
1834 /* alloc space for array typecode, element signature */
1835 if (!_dbus_string_alloc_space (writer->type_str, 1 + contained_type_len))
1838 if (!_dbus_string_insert_byte (writer->type_str,
1841 _dbus_assert_not_reached ("failed to insert array typecode after prealloc");
1843 if (!_dbus_string_copy_len (contained_type,
1844 contained_type_start, contained_type_len,
1846 sub->u.array.element_type_pos))
1847 _dbus_assert_not_reached ("should not have failed to insert array element typecodes");
1850 if (writer->type_str != NULL)
1852 /* If the parent is an array, we hold type_pos pointing at the array element type;
1853 * otherwise advance it to reflect the array value we just recursed into
1855 if (writer->container_type != DBUS_TYPE_ARRAY)
1856 writer->type_pos += 1 + contained_type_len;
1858 _dbus_assert (writer->type_pos_is_expectation); /* because it's an array */
1861 if (writer->enabled)
1863 /* Write (or jump over, if is_array_append) the length */
1864 sub->u.array.len_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
1866 if (is_array_append)
1868 sub->value_pos += 4;
1872 if (!_dbus_type_writer_write_basic_no_typecode (sub, DBUS_TYPE_UINT32,
1874 _dbus_assert_not_reached ("should not have failed to insert array len");
1877 _dbus_assert (sub->u.array.len_pos == sub->value_pos - 4);
1879 /* Write alignment padding for array elements
1880 * Note that we write the padding *even for empty arrays*
1881 * to avoid wonky special cases
1883 alignment = element_type_get_alignment (contained_type, contained_type_start);
1885 aligned = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
1886 if (aligned != sub->value_pos)
1888 if (!is_array_append)
1890 if (!_dbus_string_insert_bytes (sub->value_str,
1892 aligned - sub->value_pos,
1894 _dbus_assert_not_reached ("should not have failed to insert alignment padding");
1897 sub->value_pos = aligned;
1900 sub->u.array.start_pos = sub->value_pos;
1902 if (is_array_append)
1906 _dbus_assert (_DBUS_ALIGN_VALUE (sub->u.array.len_pos, 4) ==
1907 (unsigned) sub->u.array.len_pos);
1908 len = _dbus_unpack_uint32 (sub->byte_order,
1909 _dbus_string_get_const_data_len (sub->value_str,
1910 sub->u.array.len_pos,
1913 sub->value_pos += len;
1918 /* not enabled, so we won't write the len_pos; set it to -1 to so indicate */
1919 sub->u.array.len_pos = -1;
1920 sub->u.array.start_pos = sub->value_pos;
1923 _dbus_assert (sub->u.array.len_pos < sub->u.array.start_pos);
1924 _dbus_assert (is_array_append || sub->u.array.start_pos == sub->value_pos);
1926 #if RECURSIVE_MARSHAL_WRITE_TRACE
1927 _dbus_verbose (" type writer %p recurse array done remaining sig '%s' array start_pos = %d len_pos = %d value_pos = %d\n", sub,
1929 _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0) :
1931 sub->u.array.start_pos, sub->u.array.len_pos, sub->value_pos);
1937 /* Variant value will normally have:
1938 * 1 byte signature length not including nul
1939 * signature typecodes (nul terminated)
1940 * padding to 8-boundary
1941 * body according to signature
1943 * The signature string can only have a single type
1944 * in it but that type may be complex/recursive.
1946 * So a typical variant type with the integer 3 will have these
1948 * 0x1 'i' '\0' [padding to 8-boundary] 0x0 0x0 0x0 0x3
1950 * For an array of 4-byte types stuffed into variants, the padding to
1951 * 8-boundary is only the 1 byte that is required for the 4-boundary
1952 * anyhow for all array elements after the first one. And for single
1953 * variants in isolation, wasting a few bytes is hardly a big deal.
1955 * The main world of hurt for writing out a variant is that the type
1956 * string is the same string as the value string. Which means
1957 * inserting to the type string will move the value_pos; and it means
1958 * that inserting to the type string could break type alignment.
1960 * This type alignment issue is why the body of the variant is always
1961 * 8-aligned. Then we know that re-8-aligning the start of the body
1962 * will always correctly align the full contents of the variant type.
1965 writer_recurse_variant (DBusTypeWriter *writer,
1966 const DBusString *contained_type,
1967 int contained_type_start,
1968 int contained_type_len,
1969 DBusTypeWriter *sub)
1971 if (writer->enabled)
1973 /* Allocate space for the worst case, which is 1 byte sig
1974 * length, nul byte at end of sig, and 7 bytes padding to
1977 if (!_dbus_string_alloc_space (sub->value_str, contained_type_len + 9))
1981 /* write VARIANT typecode to the parent's type string */
1982 if (!write_or_verify_typecode (writer, DBUS_TYPE_VARIANT))
1985 /* If not enabled, mark that we have no type_str anymore ... */
1987 if (!writer->enabled)
1989 sub->type_str = NULL;
1995 /* If we're enabled then continue ... */
1997 if (!_dbus_string_insert_byte (sub->value_str,
1999 contained_type_len))
2000 _dbus_assert_not_reached ("should not have failed to insert variant type sig len");
2002 sub->value_pos += 1;
2004 /* Here we switch over to the expected type sig we're about to write */
2005 sub->type_str = sub->value_str;
2006 sub->type_pos = sub->value_pos;
2008 if (!_dbus_string_copy_len (contained_type, contained_type_start, contained_type_len,
2009 sub->value_str, sub->value_pos))
2010 _dbus_assert_not_reached ("should not have failed to insert variant type sig");
2012 sub->value_pos += contained_type_len;
2014 if (!_dbus_string_insert_byte (sub->value_str,
2017 _dbus_assert_not_reached ("should not have failed to insert variant type nul termination");
2019 sub->value_pos += 1;
2021 if (!_dbus_string_insert_bytes (sub->value_str,
2023 _DBUS_ALIGN_VALUE (sub->value_pos, 8) - sub->value_pos,
2025 _dbus_assert_not_reached ("should not have failed to insert alignment padding for variant body");
2026 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
2032 _dbus_type_writer_recurse_contained_len (DBusTypeWriter *writer,
2034 const DBusString *contained_type,
2035 int contained_type_start,
2036 int contained_type_len,
2037 DBusTypeWriter *sub,
2038 dbus_bool_t is_array_append)
2040 writer_recurse_init_and_check (writer, container_type, sub);
2042 switch (container_type)
2044 case DBUS_TYPE_STRUCT:
2045 return writer_recurse_struct (writer,
2046 contained_type, contained_type_start, contained_type_len,
2049 case DBUS_TYPE_ARRAY:
2050 return writer_recurse_array (writer,
2051 contained_type, contained_type_start, contained_type_len,
2052 sub, is_array_append);
2054 case DBUS_TYPE_VARIANT:
2055 return writer_recurse_variant (writer,
2056 contained_type, contained_type_start, contained_type_len,
2060 _dbus_assert_not_reached ("tried to recurse into type that doesn't support that");
2067 * Opens a new container and writes out the initial information for that container.
2069 * @param writer the writer
2070 * @param container_type the type of the container to open
2071 * @param contained_type the array element type or variant content type
2072 * @param contained_type_start position to look for the type
2073 * @param sub the new sub-writer to write container contents
2074 * @returns #FALSE if no memory
2077 _dbus_type_writer_recurse (DBusTypeWriter *writer,
2079 const DBusString *contained_type,
2080 int contained_type_start,
2081 DBusTypeWriter *sub)
2083 int contained_type_len;
2086 contained_type_len = find_len_of_complete_type (contained_type, contained_type_start);
2088 contained_type_len = 0;
2090 return _dbus_type_writer_recurse_contained_len (writer, container_type,
2092 contained_type_start,
2099 * Append to an existing array. Essentially, the writer will read an
2100 * existing length at the write location; jump over that length; and
2101 * write new fields. On unrecurse(), the existing length will be
2104 * @param writer the writer
2105 * @param contained_type element type
2106 * @param contained_type_start position of element type
2107 * @param sub the subwriter to init
2108 * @returns #FALSE if no memory
2111 _dbus_type_writer_append_array (DBusTypeWriter *writer,
2112 const DBusString *contained_type,
2113 int contained_type_start,
2114 DBusTypeWriter *sub)
2116 int contained_type_len;
2119 contained_type_len = find_len_of_complete_type (contained_type, contained_type_start);
2121 contained_type_len = 0;
2123 return _dbus_type_writer_recurse_contained_len (writer, DBUS_TYPE_ARRAY,
2125 contained_type_start,
2132 writer_get_array_len (DBusTypeWriter *writer)
2134 _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY);
2135 return writer->value_pos - writer->u.array.start_pos;
2139 * Closes a container created by _dbus_type_writer_recurse()
2140 * and writes any additional information to the values block.
2142 * @param writer the writer
2143 * @param sub the sub-writer created by _dbus_type_writer_recurse()
2144 * @returns #FALSE if no memory
2147 _dbus_type_writer_unrecurse (DBusTypeWriter *writer,
2148 DBusTypeWriter *sub)
2150 /* type_pos_is_expectation never gets unset once set, or we'd get all hosed */
2151 _dbus_assert (!writer->type_pos_is_expectation ||
2152 (writer->type_pos_is_expectation && sub->type_pos_is_expectation));
2154 #if RECURSIVE_MARSHAL_WRITE_TRACE
2155 _dbus_verbose (" type writer %p unrecurse type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
2156 writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
2157 _dbus_type_to_string (writer->container_type));
2158 _dbus_verbose (" type writer %p unrecurse sub type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
2159 sub, sub->type_pos, sub->value_pos,
2160 sub->type_pos_is_expectation,
2161 _dbus_type_to_string (sub->container_type));
2164 if (sub->container_type == DBUS_TYPE_STRUCT)
2166 if (!write_or_verify_typecode (sub, DBUS_STRUCT_END_CHAR))
2169 else if (sub->container_type == DBUS_TYPE_ARRAY)
2171 if (sub->u.array.len_pos >= 0) /* len_pos == -1 if we weren't enabled when we passed it */
2175 /* Set the array length */
2176 len = writer_get_array_len (sub);
2177 _dbus_marshal_set_uint32 (sub->value_str,
2178 sub->u.array.len_pos,
2181 #if RECURSIVE_MARSHAL_WRITE_TRACE
2182 _dbus_verbose (" filled in sub array len to %u at len_pos %d\n",
2183 len, sub->u.array.len_pos);
2186 #if RECURSIVE_MARSHAL_WRITE_TRACE
2189 _dbus_verbose (" not filling in sub array len because we were disabled when we passed the len\n");
2194 /* Now get type_pos right for the parent writer. Here are the cases:
2196 * Cases !writer->type_pos_is_expectation:
2197 * (in these cases we want to update to the new insertion point)
2199 * - if we recursed into a STRUCT then we didn't know in advance
2200 * what the types in the struct would be; so we have to fill in
2201 * that information now.
2202 * writer->type_pos = sub->type_pos
2204 * - if we recursed into anything else, we knew the full array
2205 * type, or knew the single typecode marking VARIANT, so
2206 * writer->type_pos is already correct.
2207 * writer->type_pos should remain as-is
2209 * - note that the parent is never an ARRAY or VARIANT, if it were
2210 * then type_pos_is_expectation would be TRUE. The parent
2211 * is thus known to be a toplevel or STRUCT.
2213 * Cases where writer->type_pos_is_expectation:
2214 * (in these cases we want to update to next expected type to write)
2216 * - we recursed from STRUCT into STRUCT and we didn't increment
2217 * type_pos in the parent just to stay consistent with the
2218 * !writer->type_pos_is_expectation case (though we could
2219 * special-case this in recurse_struct instead if we wanted)
2220 * writer->type_pos = sub->type_pos
2222 * - we recursed from STRUCT into ARRAY or VARIANT and type_pos
2223 * for parent should have been incremented already
2224 * writer->type_pos should remain as-is
2226 * - we recursed from ARRAY into a sub-element, so type_pos in the
2227 * parent is the element type and should remain the element type
2228 * for the benefit of the next child element
2229 * writer->type_pos should remain as-is
2231 * - we recursed from VARIANT into its value, so type_pos in the
2232 * parent makes no difference since there's only one value
2233 * and we just finished writing it and won't use type_pos again
2234 * writer->type_pos should remain as-is
2236 if (writer->type_str != NULL)
2238 if (sub->container_type == DBUS_TYPE_STRUCT &&
2239 (writer->container_type == DBUS_TYPE_STRUCT ||
2240 writer->container_type == DBUS_TYPE_INVALID))
2242 /* Advance the parent to the next struct field */
2243 writer->type_pos = sub->type_pos;
2247 writer->value_pos = sub->value_pos;
2249 #if RECURSIVE_MARSHAL_WRITE_TRACE
2250 _dbus_verbose (" type writer %p unrecursed type_pos = %d value_pos = %d remaining sig '%s'\n",
2251 writer, writer->type_pos, writer->value_pos,
2253 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
2261 * Writes out a basic type.
2263 * @param writer the writer
2264 * @param type the type to write
2265 * @param value the address of the value to write
2266 * @returns #FALSE if no memory
2269 _dbus_type_writer_write_basic (DBusTypeWriter *writer,
2275 /* First ensure that our type realloc will succeed */
2276 if (!writer->type_pos_is_expectation && writer->type_str != NULL)
2278 if (!_dbus_string_alloc_space (writer->type_str, 1))
2284 if (!_dbus_type_writer_write_basic_no_typecode (writer, type, value))
2287 if (!write_or_verify_typecode (writer, type))
2288 _dbus_assert_not_reached ("failed to write typecode after prealloc");
2293 #if RECURSIVE_MARSHAL_WRITE_TRACE
2294 _dbus_verbose (" type writer %p basic type_pos = %d value_pos = %d is_expectation = %d enabled = %d\n",
2295 writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
2303 * Writes a block of fixed-length basic values, i.e. those that are
2304 * both _dbus_type_is_fixed() and _dbus_type_is_basic(). The block
2305 * must be written inside an array.
2307 * The value parameter should be the address of said array of values,
2308 * so e.g. if it's an array of double, pass in "const double**"
2310 * @param writer the writer
2311 * @param element_type type of stuff in the array
2312 * @param value address of the array
2313 * @param n_elements number of elements in the array
2314 * @returns #FALSE if no memory
2317 _dbus_type_writer_write_fixed_multi (DBusTypeWriter *writer,
2322 _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY);
2323 _dbus_assert (_dbus_type_is_fixed (element_type));
2324 _dbus_assert (writer->type_pos_is_expectation);
2325 _dbus_assert (n_elements >= 0);
2327 #if RECURSIVE_MARSHAL_WRITE_TRACE
2328 _dbus_verbose (" type writer %p entering fixed multi type_pos = %d value_pos = %d n_elements %d\n",
2329 writer, writer->type_pos, writer->value_pos, n_elements);
2332 if (!write_or_verify_typecode (writer, element_type))
2333 _dbus_assert_not_reached ("OOM should not happen if only verifying typecode");
2335 if (writer->enabled)
2337 if (!_dbus_marshal_write_fixed_multi (writer->value_str,
2343 &writer->value_pos))
2347 #if RECURSIVE_MARSHAL_WRITE_TRACE
2348 _dbus_verbose (" type writer %p fixed multi written new type_pos = %d new value_pos = %d n_elements %d\n",
2349 writer, writer->type_pos, writer->value_pos, n_elements);
2356 enable_if_after (DBusTypeWriter *writer,
2357 DBusTypeReader *reader,
2358 const DBusTypeReader *start_after)
2362 if (!writer->enabled && _dbus_type_reader_greater_than (reader, start_after))
2364 _dbus_type_writer_set_enabled (writer, TRUE);
2365 #if RECURSIVE_MARSHAL_WRITE_TRACE
2366 _dbus_verbose ("ENABLING writer %p at %d because reader at value_pos %d is after reader at value_pos %d\n",
2367 writer, writer->value_pos, reader->value_pos, start_after->value_pos);
2371 _dbus_assert ((!writer->enabled && !_dbus_type_reader_greater_than (reader, start_after)) ||
2372 (writer->enabled && _dbus_type_reader_greater_than (reader, start_after)));
2377 append_fixup (DBusList **fixups,
2378 const DBusArrayLenFixup *fixup)
2380 DBusArrayLenFixup *f;
2382 f = dbus_new (DBusArrayLenFixup, 1);
2388 if (!_dbus_list_append (fixups, f))
2394 _dbus_assert (f->len_pos_in_reader == fixup->len_pos_in_reader);
2395 _dbus_assert (f->new_len == fixup->new_len);
2400 /* This loop is trivial if you ignore all the start_after nonsense,
2401 * so if you're trying to figure it out, start by ignoring that
2404 writer_write_reader_helper (DBusTypeWriter *writer,
2405 DBusTypeReader *reader,
2406 const DBusTypeReader *start_after,
2407 int start_after_new_pos,
2408 int start_after_new_len,
2410 dbus_bool_t inside_start_after)
2414 while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
2416 if (_dbus_type_is_container (current_type))
2418 DBusTypeReader subreader;
2419 DBusTypeWriter subwriter;
2420 const DBusString *sig_str;
2423 dbus_bool_t enabled_at_recurse;
2424 dbus_bool_t past_start_after;
2425 int reader_array_len_pos;
2426 int reader_array_start_pos;
2427 dbus_bool_t this_is_start_after;
2429 /* type_pos is checked since e.g. in a struct the struct
2430 * and its first field have the same value_pos.
2431 * type_str will differ in reader/start_after for variants
2432 * where type_str is inside the value_str
2434 if (!inside_start_after && start_after &&
2435 reader->value_pos == start_after->value_pos &&
2436 reader->type_str == start_after->type_str &&
2437 reader->type_pos == start_after->type_pos)
2438 this_is_start_after = TRUE;
2440 this_is_start_after = FALSE;
2442 _dbus_type_reader_recurse (reader, &subreader);
2444 if (current_type == DBUS_TYPE_ARRAY)
2446 reader_array_len_pos = ARRAY_READER_LEN_POS (&subreader);
2447 reader_array_start_pos = subreader.u.array.start_pos;
2452 reader_array_len_pos = -1;
2453 reader_array_start_pos = -1;
2456 _dbus_type_reader_get_signature (&subreader, &sig_str,
2457 &sig_start, &sig_len);
2459 #if RECURSIVE_MARSHAL_WRITE_TRACE
2460 _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",
2461 _dbus_type_to_string (current_type),
2463 subreader.value_pos,
2465 start_after ? start_after->value_pos : -1,
2466 _dbus_string_get_length (writer->value_str),
2467 inside_start_after, this_is_start_after);
2470 if (!inside_start_after && !this_is_start_after)
2471 enable_if_after (writer, &subreader, start_after);
2472 enabled_at_recurse = writer->enabled;
2473 if (!_dbus_type_writer_recurse_contained_len (writer, current_type,
2474 sig_str, sig_start, sig_len,
2478 #if RECURSIVE_MARSHAL_WRITE_TRACE
2479 _dbus_verbose ("recursed into subwriter at %d write target len %d\n",
2480 subwriter.value_pos,
2481 _dbus_string_get_length (subwriter.value_str));
2484 if (!writer_write_reader_helper (&subwriter, &subreader, start_after,
2485 start_after_new_pos, start_after_new_len,
2487 inside_start_after ||
2488 this_is_start_after))
2491 #if RECURSIVE_MARSHAL_WRITE_TRACE
2492 _dbus_verbose ("about to unrecurse from %s subreader at %d writer at %d subwriter at %d write target len %d\n",
2493 _dbus_type_to_string (current_type),
2494 subreader.value_pos,
2496 subwriter.value_pos,
2497 _dbus_string_get_length (writer->value_str));
2500 if (!inside_start_after && !this_is_start_after)
2501 enable_if_after (writer, &subreader, start_after);
2502 past_start_after = writer->enabled;
2503 if (!_dbus_type_writer_unrecurse (writer, &subwriter))
2506 /* If we weren't enabled when we recursed, we didn't
2507 * write an array len; if we passed start_after
2508 * somewhere inside the array, then we need to generate
2511 if (start_after != NULL &&
2512 !enabled_at_recurse && past_start_after &&
2513 current_type == DBUS_TYPE_ARRAY &&
2516 DBusArrayLenFixup fixup;
2517 int bytes_written_after_start_after;
2518 int bytes_before_start_after;
2521 /* this subwriter access is moderately unkosher since we
2522 * already unrecursed, but it works as long as unrecurse
2523 * doesn't break us on purpose
2525 bytes_written_after_start_after = writer_get_array_len (&subwriter);
2527 bytes_before_start_after =
2528 start_after->value_pos - reader_array_start_pos;
2530 fixup.len_pos_in_reader = reader_array_len_pos;
2532 bytes_before_start_after +
2533 start_after_new_len +
2534 bytes_written_after_start_after;
2536 _dbus_assert (_DBUS_ALIGN_VALUE (fixup.len_pos_in_reader, 4) ==
2537 (unsigned) fixup.len_pos_in_reader);
2539 old_len = _dbus_unpack_uint32 (reader->byte_order,
2540 _dbus_string_get_const_data_len (reader->value_str,
2541 fixup.len_pos_in_reader, 4));
2543 if (old_len != fixup.new_len && !append_fixup (fixups, &fixup))
2546 #if RECURSIVE_MARSHAL_WRITE_TRACE
2547 _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",
2548 fixup.len_pos_in_reader,
2550 reader_array_start_pos,
2551 start_after->value_pos,
2552 bytes_before_start_after,
2553 start_after_new_len,
2554 bytes_written_after_start_after);
2562 _dbus_assert (_dbus_type_is_basic (current_type));
2564 #if RECURSIVE_MARSHAL_WRITE_TRACE
2565 _dbus_verbose ("Reading basic value %s at %d\n",
2566 _dbus_type_to_string (current_type),
2570 _dbus_type_reader_read_basic (reader, &val);
2572 #if RECURSIVE_MARSHAL_WRITE_TRACE
2573 _dbus_verbose ("Writing basic value %s at %d write target len %d inside_start_after = %d\n",
2574 _dbus_type_to_string (current_type),
2576 _dbus_string_get_length (writer->value_str),
2577 inside_start_after);
2579 if (!inside_start_after)
2580 enable_if_after (writer, reader, start_after);
2581 if (!_dbus_type_writer_write_basic (writer, current_type, &val))
2583 #if RECURSIVE_MARSHAL_WRITE_TRACE
2584 _dbus_verbose ("Wrote basic value %s, new value_pos %d write target len %d\n",
2585 _dbus_type_to_string (current_type),
2587 _dbus_string_get_length (writer->value_str));
2591 _dbus_type_reader_next (reader);
2598 apply_and_free_fixups (fixups, NULL); /* NULL for reader to apply to */
2604 * Iterate through all values in the given reader, writing a copy of
2605 * each value to the writer. The reader will be moved forward to its
2608 * If a reader start_after is provided, it should be a reader for the
2609 * same data as the reader to be written. Only values occurring after
2610 * the value pointed to by start_after will be written to the writer.
2612 * If start_after is provided, then the copy of the reader will be
2613 * partial. This means that array lengths will not have been copied.
2614 * The assumption is that you wrote a new version of the value at
2615 * start_after to the writer. You have to pass in the start position
2616 * and length of the new value. (If you are deleting the value
2617 * at start_after, pass in 0 for the length.)
2619 * If the fixups parameter is non-#NULL, then any array length that
2620 * was read but not written due to start_after will be provided
2621 * as a #DBusArrayLenFixup. The fixup contains the position of the
2622 * array length in the source data, and the correct array length
2623 * assuming you combine the source data before start_after with
2624 * the written data at start_after and beyond.
2626 * @param writer the writer to copy to
2627 * @param reader the reader to copy from
2628 * @param start_after #NULL or a reader showing where to start
2629 * @param start_after_new_pos the position of start_after equivalent in the target data
2630 * @param start_after_new_len the length of start_after equivalent in the target data
2631 * @param fixups list to append #DBusArrayLenFixup if the write was partial
2632 * @returns #FALSE if no memory
2635 _dbus_type_writer_write_reader_partial (DBusTypeWriter *writer,
2636 DBusTypeReader *reader,
2637 const DBusTypeReader *start_after,
2638 int start_after_new_pos,
2639 int start_after_new_len,
2642 DBusTypeWriter orig;
2649 orig_type_len = _dbus_string_get_length (writer->type_str);
2650 orig_value_len = _dbus_string_get_length (writer->value_str);
2651 orig_enabled = writer->enabled;
2654 _dbus_type_writer_set_enabled (writer, FALSE);
2656 if (!writer_write_reader_helper (writer, reader, start_after,
2657 start_after_new_pos,
2658 start_after_new_len,
2662 _dbus_type_writer_set_enabled (writer, orig_enabled);
2666 if (!writer->type_pos_is_expectation)
2668 new_bytes = _dbus_string_get_length (writer->type_str) - orig_type_len;
2669 _dbus_string_delete (writer->type_str, orig.type_pos, new_bytes);
2671 new_bytes = _dbus_string_get_length (writer->value_str) - orig_value_len;
2672 _dbus_string_delete (writer->value_str, orig.value_pos, new_bytes);
2680 * Iterate through all values in the given reader, writing a copy of
2681 * each value to the writer. The reader will be moved forward to its
2684 * @param writer the writer to copy to
2685 * @param reader the reader to copy from
2686 * @returns #FALSE if no memory
2689 _dbus_type_writer_write_reader (DBusTypeWriter *writer,
2690 DBusTypeReader *reader)
2692 return _dbus_type_writer_write_reader_partial (writer, reader, NULL, 0, 0, NULL);
2696 * If disabled, a writer can still be iterated forward and recursed/unrecursed
2697 * but won't write any values. Types will still be written unless the
2698 * writer is a "values only" writer, because the writer needs access to
2699 * a valid signature to be able to iterate.
2701 * @param writer the type writer
2702 * @param enabled #TRUE if values should be written
2705 _dbus_type_writer_set_enabled (DBusTypeWriter *writer,
2706 dbus_bool_t enabled)
2708 writer->enabled = enabled != FALSE;
2711 /** @} */ /* end of DBusMarshal group */
2713 /* tests in dbus-marshal-recursive-util.c */