1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dbus-marshal-recursive.h"
26 #include "dbus-marshal-basic.h"
27 #include "dbus-signature.h"
28 #include "dbus-internals.h"
31 * @addtogroup DBusMarshal
35 static dbus_bool_t _dbus_type_reader_greater_than (const DBusTypeReader *lhs,
36 const DBusTypeReader *rhs);
38 static void _dbus_type_writer_set_enabled (DBusTypeWriter *writer,
40 static dbus_bool_t _dbus_type_writer_write_reader_partial (DBusTypeWriter *writer,
41 DBusTypeReader *reader,
42 const DBusTypeReader *start_after,
43 int start_after_new_pos,
44 int start_after_new_len,
47 /** turn this on to get deluged in TypeReader verbose spam */
48 #define RECURSIVE_MARSHAL_READ_TRACE 0
50 /** turn this on to get deluged in TypeWriter verbose spam */
51 #define RECURSIVE_MARSHAL_WRITE_TRACE 0
54 free_fixups (DBusList **fixups)
58 link = _dbus_list_get_first_link (fixups);
63 next = _dbus_list_get_next_link (fixups, link);
65 dbus_free (link->data);
66 _dbus_list_free_link (link);
75 apply_and_free_fixups (DBusList **fixups,
76 DBusTypeReader *reader)
80 #if RECURSIVE_MARSHAL_WRITE_TRACE
82 _dbus_verbose (" %d FIXUPS to apply\n",
83 _dbus_list_get_length (fixups));
86 link = _dbus_list_get_first_link (fixups);
91 next = _dbus_list_get_next_link (fixups, link);
99 #if RECURSIVE_MARSHAL_WRITE_TRACE
100 _dbus_verbose (" applying FIXUP to reader %p at pos %d new_len = %d old len %d\n",
101 reader, f->len_pos_in_reader, f->new_len,
102 _dbus_marshal_read_uint32 (reader->value_str,
103 f->len_pos_in_reader,
104 reader->byte_order, NULL));
107 _dbus_marshal_set_uint32 ((DBusString*) reader->value_str,
108 f->len_pos_in_reader,
113 dbus_free (link->data);
114 _dbus_list_free_link (link);
123 * Virtual table for a type reader.
125 struct DBusTypeReaderClass
127 const char *name; /**< name for debugging */
128 int id; /**< index in all_reader_classes */
129 dbus_bool_t types_only; /**< only iterates over types, not values */
130 void (* recurse) (DBusTypeReader *sub,
131 DBusTypeReader *parent); /**< recurse with this reader as sub */
132 dbus_bool_t (* check_finished) (const DBusTypeReader *reader); /**< check whether reader is at the end */
133 void (* next) (DBusTypeReader *reader,
134 int current_type); /**< go to the next value */
138 element_type_get_alignment (const DBusString *str,
141 return _dbus_type_get_alignment (_dbus_first_type_in_signature (str, pos));
145 reader_init (DBusTypeReader *reader,
147 const DBusString *type_str,
149 const DBusString *value_str,
152 _DBUS_ZERO (*reader);
153 reader->byte_order = byte_order;
154 reader->finished = FALSE;
155 reader->type_str = type_str;
156 reader->type_pos = type_pos;
157 reader->value_str = value_str;
158 reader->value_pos = value_pos;
162 base_reader_recurse (DBusTypeReader *sub,
163 DBusTypeReader *parent)
165 /* point subreader at the same place as parent */
175 struct_or_dict_entry_types_only_reader_recurse (DBusTypeReader *sub,
176 DBusTypeReader *parent)
178 base_reader_recurse (sub, parent);
180 _dbus_assert (_dbus_string_get_byte (sub->type_str,
181 sub->type_pos) == DBUS_STRUCT_BEGIN_CHAR ||
182 _dbus_string_get_byte (sub->type_str,
183 sub->type_pos) == DBUS_DICT_ENTRY_BEGIN_CHAR);
189 struct_or_dict_entry_reader_recurse (DBusTypeReader *sub,
190 DBusTypeReader *parent)
192 struct_or_dict_entry_types_only_reader_recurse (sub, parent);
194 /* struct and dict entry have 8 byte alignment */
195 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
199 array_types_only_reader_recurse (DBusTypeReader *sub,
200 DBusTypeReader *parent)
202 base_reader_recurse (sub, parent);
204 /* point type_pos at the array element type */
207 /* Init with values likely to crash things if misused */
208 sub->u.array.start_pos = _DBUS_INT_MAX;
209 sub->array_len_offset = 7;
212 /** compute position of array length given array_len_offset, which is
213 the offset back from start_pos to end of the len */
214 #define ARRAY_READER_LEN_POS(reader) \
215 ((reader)->u.array.start_pos - ((int)(reader)->array_len_offset) - 4)
218 array_reader_get_array_len (const DBusTypeReader *reader)
220 dbus_uint32_t array_len;
223 len_pos = ARRAY_READER_LEN_POS (reader);
225 _dbus_assert (_DBUS_ALIGN_VALUE (len_pos, 4) == (unsigned) len_pos);
226 array_len = _dbus_unpack_uint32 (reader->byte_order,
227 _dbus_string_get_const_udata_len (reader->value_str, len_pos, 4));
229 #if RECURSIVE_MARSHAL_READ_TRACE
230 _dbus_verbose (" reader %p len_pos %d array len %u len_offset %d\n",
231 reader, len_pos, array_len, reader->array_len_offset);
234 _dbus_assert (reader->u.array.start_pos - len_pos - 4 < 8);
240 array_reader_recurse (DBusTypeReader *sub,
241 DBusTypeReader *parent)
246 array_types_only_reader_recurse (sub, parent);
248 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
250 len_pos = sub->value_pos;
252 sub->value_pos += 4; /* for the length */
254 alignment = element_type_get_alignment (sub->type_str,
257 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
259 sub->u.array.start_pos = sub->value_pos;
260 _dbus_assert ((sub->u.array.start_pos - (len_pos + 4)) < 8); /* only 3 bits in array_len_offset */
261 sub->array_len_offset = sub->u.array.start_pos - (len_pos + 4);
263 #if RECURSIVE_MARSHAL_READ_TRACE
264 _dbus_verbose (" type reader %p array start = %d len_offset = %d array len = %d array element type = %s\n",
266 sub->u.array.start_pos,
267 sub->array_len_offset,
268 array_reader_get_array_len (sub),
269 _dbus_type_to_string (_dbus_first_type_in_signature (sub->type_str,
275 variant_reader_recurse (DBusTypeReader *sub,
276 DBusTypeReader *parent)
279 int contained_alignment;
281 base_reader_recurse (sub, parent);
283 /* Variant is 1 byte sig length (without nul), signature with nul,
284 * padding to 8-boundary, then values
287 sig_len = _dbus_string_get_byte (sub->value_str, sub->value_pos);
289 sub->type_str = sub->value_str;
290 sub->type_pos = sub->value_pos + 1;
292 sub->value_pos = sub->type_pos + sig_len + 1;
294 contained_alignment = _dbus_type_get_alignment (_dbus_first_type_in_signature (sub->type_str,
297 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, contained_alignment);
299 #if RECURSIVE_MARSHAL_READ_TRACE
300 _dbus_verbose (" type reader %p variant containing '%s'\n",
302 _dbus_string_get_const_data_len (sub->type_str,
308 array_reader_check_finished (const DBusTypeReader *reader)
312 /* return the array element type if elements remain, and
313 * TYPE_INVALID otherwise
316 end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
318 _dbus_assert (reader->value_pos <= end_pos);
319 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
321 return reader->value_pos == end_pos;
325 skip_one_complete_type (const DBusString *type_str,
328 _dbus_type_signature_next (_dbus_string_get_const_data (type_str),
333 * Skips to the next "complete" type inside a type signature.
334 * The signature is read starting at type_pos, and the next
335 * type position is stored in the same variable.
337 * @param type_str a type signature (must be valid)
338 * @param type_pos an integer position in the type signature (in and out)
341 _dbus_type_signature_next (const char *type_str,
344 const unsigned char *p;
345 const unsigned char *start;
347 _dbus_assert (type_str != NULL);
348 _dbus_assert (type_pos != NULL);
350 start = (const unsigned char *)type_str;
351 p = start + *type_pos;
353 _dbus_assert (*p != DBUS_STRUCT_END_CHAR);
354 _dbus_assert (*p != DBUS_DICT_ENTRY_END_CHAR);
356 while (*p == DBUS_TYPE_ARRAY)
359 _dbus_assert (*p != DBUS_STRUCT_END_CHAR);
360 _dbus_assert (*p != DBUS_DICT_ENTRY_END_CHAR);
362 if (*p == DBUS_STRUCT_BEGIN_CHAR)
370 _dbus_assert (*p != DBUS_TYPE_INVALID);
374 _dbus_assert (*p != DBUS_TYPE_INVALID);
376 if (*p == DBUS_STRUCT_BEGIN_CHAR)
378 else if (*p == DBUS_STRUCT_END_CHAR)
389 else if (*p == DBUS_DICT_ENTRY_BEGIN_CHAR)
397 _dbus_assert (*p != DBUS_TYPE_INVALID);
401 _dbus_assert (*p != DBUS_TYPE_INVALID);
403 if (*p == DBUS_DICT_ENTRY_BEGIN_CHAR)
405 else if (*p == DBUS_DICT_ENTRY_END_CHAR)
421 *type_pos = (int) (p - start);
425 find_len_of_complete_type (const DBusString *type_str,
432 skip_one_complete_type (type_str, &end);
434 return end - type_pos;
438 base_reader_next (DBusTypeReader *reader,
441 switch (current_type)
443 case DBUS_TYPE_DICT_ENTRY:
444 case DBUS_TYPE_STRUCT:
445 case DBUS_TYPE_VARIANT:
446 /* Scan forward over the entire container contents */
450 if (reader->klass->types_only && current_type == DBUS_TYPE_VARIANT)
454 /* Recurse into the struct or variant */
455 _dbus_type_reader_recurse (reader, &sub);
457 /* Skip everything in this subreader */
458 while (_dbus_type_reader_next (&sub))
463 if (!reader->klass->types_only)
464 reader->value_pos = sub.value_pos;
466 /* Now we are at the end of this container; for variants, the
467 * subreader's type_pos is totally inapplicable (it's in the
468 * value string) but we know that we increment by one past the
471 if (current_type == DBUS_TYPE_VARIANT)
472 reader->type_pos += 1;
474 reader->type_pos = sub.type_pos;
478 case DBUS_TYPE_ARRAY:
480 if (!reader->klass->types_only)
481 _dbus_marshal_skip_array (reader->value_str,
482 _dbus_first_type_in_signature (reader->type_str,
483 reader->type_pos + 1),
487 skip_one_complete_type (reader->type_str, &reader->type_pos);
492 if (!reader->klass->types_only)
493 _dbus_marshal_skip_basic (reader->value_str,
494 current_type, reader->byte_order,
497 reader->type_pos += 1;
503 struct_reader_next (DBusTypeReader *reader,
508 base_reader_next (reader, current_type);
510 /* for STRUCT containers we return FALSE at the end of the struct,
511 * for INVALID we return FALSE at the end of the signature.
512 * In both cases we arrange for get_current_type() to return INVALID
513 * which is defined to happen iff we're at the end (no more next())
515 t = _dbus_string_get_byte (reader->type_str, reader->type_pos);
516 if (t == DBUS_STRUCT_END_CHAR)
518 reader->type_pos += 1;
519 reader->finished = TRUE;
524 dict_entry_reader_next (DBusTypeReader *reader,
529 base_reader_next (reader, current_type);
531 /* for STRUCT containers we return FALSE at the end of the struct,
532 * for INVALID we return FALSE at the end of the signature.
533 * In both cases we arrange for get_current_type() to return INVALID
534 * which is defined to happen iff we're at the end (no more next())
536 t = _dbus_string_get_byte (reader->type_str, reader->type_pos);
537 if (t == DBUS_DICT_ENTRY_END_CHAR)
539 reader->type_pos += 1;
540 reader->finished = TRUE;
545 array_types_only_reader_next (DBusTypeReader *reader,
548 /* We have one "element" to be iterated over
549 * in each array, which is its element type.
550 * So the finished flag indicates whether we've
551 * iterated over it yet or not.
553 reader->finished = TRUE;
557 array_reader_next (DBusTypeReader *reader,
560 /* Skip one array element */
563 end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
565 #if RECURSIVE_MARSHAL_READ_TRACE
566 _dbus_verbose (" reader %p array next START start_pos = %d end_pos = %d value_pos = %d current_type = %s\n",
568 reader->u.array.start_pos,
569 end_pos, reader->value_pos,
570 _dbus_type_to_string (current_type));
573 _dbus_assert (reader->value_pos < end_pos);
574 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
576 switch (_dbus_first_type_in_signature (reader->type_str,
579 case DBUS_TYPE_DICT_ENTRY:
580 case DBUS_TYPE_STRUCT:
581 case DBUS_TYPE_VARIANT:
585 /* Recurse into the struct or variant */
586 _dbus_type_reader_recurse (reader, &sub);
588 /* Skip everything in this element */
589 while (_dbus_type_reader_next (&sub))
594 /* Now we are at the end of this element */
595 reader->value_pos = sub.value_pos;
599 case DBUS_TYPE_ARRAY:
601 _dbus_marshal_skip_array (reader->value_str,
602 _dbus_first_type_in_signature (reader->type_str,
603 reader->type_pos + 1),
611 _dbus_marshal_skip_basic (reader->value_str,
612 current_type, reader->byte_order,
618 #if RECURSIVE_MARSHAL_READ_TRACE
619 _dbus_verbose (" reader %p array next END start_pos = %d end_pos = %d value_pos = %d current_type = %s\n",
621 reader->u.array.start_pos,
622 end_pos, reader->value_pos,
623 _dbus_type_to_string (current_type));
626 _dbus_assert (reader->value_pos <= end_pos);
628 if (reader->value_pos == end_pos)
630 skip_one_complete_type (reader->type_str,
635 static const DBusTypeReaderClass body_reader_class = {
638 NULL, /* body is always toplevel, so doesn't get recursed into */
643 static const DBusTypeReaderClass body_types_only_reader_class = {
646 NULL, /* body is always toplevel, so doesn't get recursed into */
651 static const DBusTypeReaderClass struct_reader_class = {
654 struct_or_dict_entry_reader_recurse,
659 static const DBusTypeReaderClass struct_types_only_reader_class = {
662 struct_or_dict_entry_types_only_reader_recurse,
667 static const DBusTypeReaderClass dict_entry_reader_class = {
670 struct_or_dict_entry_reader_recurse,
672 dict_entry_reader_next
675 static const DBusTypeReaderClass dict_entry_types_only_reader_class = {
676 "dict_entry types", 5,
678 struct_or_dict_entry_types_only_reader_recurse,
680 dict_entry_reader_next
683 static const DBusTypeReaderClass array_reader_class = {
686 array_reader_recurse,
687 array_reader_check_finished,
691 static const DBusTypeReaderClass array_types_only_reader_class = {
694 array_types_only_reader_recurse,
696 array_types_only_reader_next
699 static const DBusTypeReaderClass variant_reader_class = {
702 variant_reader_recurse,
707 #ifndef DBUS_DISABLE_ASSERT
708 static const DBusTypeReaderClass * const
709 all_reader_classes[] = {
711 &body_types_only_reader_class,
712 &struct_reader_class,
713 &struct_types_only_reader_class,
714 &dict_entry_reader_class,
715 &dict_entry_types_only_reader_class,
717 &array_types_only_reader_class,
718 &variant_reader_class
723 * Initializes a type reader.
725 * @param reader the reader
726 * @param byte_order the byte order of the block to read
727 * @param type_str the signature of the block to read
728 * @param type_pos location of signature
729 * @param value_str the string containing values block
730 * @param value_pos start of values block
733 _dbus_type_reader_init (DBusTypeReader *reader,
735 const DBusString *type_str,
737 const DBusString *value_str,
740 reader_init (reader, byte_order, type_str, type_pos,
741 value_str, value_pos);
743 reader->klass = &body_reader_class;
745 #if RECURSIVE_MARSHAL_READ_TRACE
746 _dbus_verbose (" type reader %p init type_pos = %d value_pos = %d remaining sig '%s'\n",
747 reader, reader->type_pos, reader->value_pos,
748 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
753 * Like _dbus_type_reader_init() but the iteration is over the
754 * signature, not over values.
756 * @param reader the reader
757 * @param type_str the signature string
758 * @param type_pos location in the signature string
761 _dbus_type_reader_init_types_only (DBusTypeReader *reader,
762 const DBusString *type_str,
765 reader_init (reader, DBUS_COMPILER_BYTE_ORDER /* irrelevant */,
766 type_str, type_pos, NULL, _DBUS_INT_MAX /* crashes if we screw up */);
768 reader->klass = &body_types_only_reader_class;
770 #if RECURSIVE_MARSHAL_READ_TRACE
771 _dbus_verbose (" type reader %p init types only type_pos = %d remaining sig '%s'\n",
772 reader, reader->type_pos,
773 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
778 * Gets the type of the value the reader is currently pointing to;
779 * or for a types-only reader gets the type it's currently pointing to.
780 * If the reader is at the end of a block or end of a container such
781 * as an array, returns #DBUS_TYPE_INVALID.
783 * @param reader the reader
786 _dbus_type_reader_get_current_type (const DBusTypeReader *reader)
790 if (reader->finished ||
791 (reader->klass->check_finished &&
792 (* reader->klass->check_finished) (reader)))
793 t = DBUS_TYPE_INVALID;
795 t = _dbus_first_type_in_signature (reader->type_str,
798 _dbus_assert (t != DBUS_STRUCT_END_CHAR);
799 _dbus_assert (t != DBUS_STRUCT_BEGIN_CHAR);
800 _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
801 _dbus_assert (t != DBUS_DICT_ENTRY_BEGIN_CHAR);
804 _dbus_verbose (" type reader %p current type_pos = %d type = %s\n",
805 reader, reader->type_pos,
806 _dbus_type_to_string (t));
813 * Gets the type of an element of the array the reader is currently
814 * pointing to. It's an error to call this if
815 * _dbus_type_reader_get_current_type() doesn't return #DBUS_TYPE_ARRAY
818 * @param reader the reader
821 _dbus_type_reader_get_element_type (const DBusTypeReader *reader)
825 _dbus_assert (_dbus_type_reader_get_current_type (reader) == DBUS_TYPE_ARRAY);
827 element_type = _dbus_first_type_in_signature (reader->type_str,
828 reader->type_pos + 1);
834 * Gets the current position in the value block
835 * @param reader the reader
838 _dbus_type_reader_get_value_pos (const DBusTypeReader *reader)
840 return reader->value_pos;
844 * Get the address of the marshaled value in the data being read. The
845 * address may not be aligned; you have to align it to the type of the
846 * value you want to read. Most of the demarshal routines do this for
849 * @param reader the reader
850 * @param value_location the address of the marshaled value
853 _dbus_type_reader_read_raw (const DBusTypeReader *reader,
854 const unsigned char **value_location)
856 _dbus_assert (!reader->klass->types_only);
858 *value_location = _dbus_string_get_const_udata_len (reader->value_str,
864 * Reads a basic-typed value, as with _dbus_marshal_read_basic().
866 * @param reader the reader
867 * @param value the address of the value
870 _dbus_type_reader_read_basic (const DBusTypeReader *reader,
875 _dbus_assert (!reader->klass->types_only);
877 t = _dbus_type_reader_get_current_type (reader);
879 _dbus_marshal_read_basic (reader->value_str,
886 #if RECURSIVE_MARSHAL_READ_TRACE
887 _dbus_verbose (" type reader %p read basic type_pos = %d value_pos = %d remaining sig '%s'\n",
888 reader, reader->type_pos, reader->value_pos,
889 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
894 * Returns the number of bytes in the array.
896 * @param reader the reader to read from
897 * @returns the number of bytes in the array
900 _dbus_type_reader_get_array_length (const DBusTypeReader *reader)
902 _dbus_assert (!reader->klass->types_only);
903 _dbus_assert (reader->klass == &array_reader_class);
905 return array_reader_get_array_len (reader);
909 * Reads a block of fixed-length basic values, from the current point
910 * in an array to the end of the array. Does not work for arrays of
911 * string or container types.
913 * This function returns the array in-place; it does not make a copy,
914 * and it does not swap the bytes.
916 * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
917 * and the "value" argument should be a "const double**" and so on.
919 * @param reader the reader to read from
920 * @param value place to return the array values
921 * @param n_elements place to return number of array elements
924 _dbus_type_reader_read_fixed_multi (const DBusTypeReader *reader,
934 _dbus_assert (!reader->klass->types_only);
935 _dbus_assert (reader->klass == &array_reader_class);
937 element_type = _dbus_first_type_in_signature (reader->type_str,
940 _dbus_assert (element_type != DBUS_TYPE_INVALID); /* why we don't use get_current_type() */
941 _dbus_assert (dbus_type_is_fixed (element_type));
943 alignment = _dbus_type_get_alignment (element_type);
945 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
947 total_len = array_reader_get_array_len (reader);
948 end_pos = reader->u.array.start_pos + total_len;
949 remaining_len = end_pos - reader->value_pos;
951 #if RECURSIVE_MARSHAL_READ_TRACE
952 _dbus_verbose ("end_pos %d total_len %d remaining_len %d value_pos %d\n",
953 end_pos, total_len, remaining_len, reader->value_pos);
956 _dbus_assert (remaining_len <= total_len);
958 if (remaining_len == 0)
959 *(const DBusBasicValue**) value = NULL;
961 *(const DBusBasicValue**) value =
962 (void*) _dbus_string_get_const_data_len (reader->value_str,
966 *n_elements = remaining_len / alignment;
967 _dbus_assert ((remaining_len % alignment) == 0);
969 #if RECURSIVE_MARSHAL_READ_TRACE
970 _dbus_verbose (" type reader %p read fixed array type_pos = %d value_pos = %d remaining sig '%s'\n",
971 reader, reader->type_pos, reader->value_pos,
972 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
977 * Initialize a new reader pointing to the first type and
978 * corresponding value that's a child of the current container. It's
979 * an error to call this if the current type is a non-container.
981 * Note that DBusTypeReader traverses values, not types. So if you
982 * have an empty array of array of int, you can't recurse into it. You
983 * can only recurse into each element.
985 * @param reader the reader
986 * @param sub a reader to init pointing to the first child
989 _dbus_type_reader_recurse (DBusTypeReader *reader,
992 const DBusTypeReaderClass *klass;
995 t = _dbus_first_type_in_signature (reader->type_str, reader->type_pos);
999 case DBUS_TYPE_STRUCT:
1000 if (reader->klass->types_only)
1001 klass = &struct_types_only_reader_class;
1003 klass = &struct_reader_class;
1005 case DBUS_TYPE_DICT_ENTRY:
1006 if (reader->klass->types_only)
1007 klass = &dict_entry_types_only_reader_class;
1009 klass = &dict_entry_reader_class;
1011 case DBUS_TYPE_ARRAY:
1012 if (reader->klass->types_only)
1013 klass = &array_types_only_reader_class;
1015 klass = &array_reader_class;
1017 case DBUS_TYPE_VARIANT:
1018 if (reader->klass->types_only)
1019 _dbus_assert_not_reached ("can't recurse into variant typecode");
1021 klass = &variant_reader_class;
1024 _dbus_verbose ("recursing into type %s\n", _dbus_type_to_string (t));
1025 #ifndef DBUS_DISABLE_CHECKS
1026 if (t == DBUS_TYPE_INVALID)
1027 _dbus_warn_check_failed ("You can't recurse into an empty array or off the end of a message body\n");
1028 #endif /* DBUS_DISABLE_CHECKS */
1030 _dbus_assert_not_reached ("don't yet handle recursing into this type");
1033 _dbus_assert (klass == all_reader_classes[klass->id]);
1035 (* klass->recurse) (sub, reader);
1038 #if RECURSIVE_MARSHAL_READ_TRACE
1039 _dbus_verbose (" type reader %p RECURSED type_pos = %d value_pos = %d remaining sig '%s'\n",
1040 sub, sub->type_pos, sub->value_pos,
1041 _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0));
1046 * Skip to the next value on this "level". e.g. the next field in a
1047 * struct, the next value in an array. Returns FALSE at the end of the
1048 * current container.
1050 * @param reader the reader
1051 * @returns FALSE if nothing more to read at or below this level
1054 _dbus_type_reader_next (DBusTypeReader *reader)
1058 t = _dbus_type_reader_get_current_type (reader);
1060 #if RECURSIVE_MARSHAL_READ_TRACE
1061 _dbus_verbose (" type reader %p START next() { type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
1062 reader, reader->type_pos, reader->value_pos,
1063 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1064 _dbus_type_to_string (t));
1067 if (t == DBUS_TYPE_INVALID)
1070 (* reader->klass->next) (reader, t);
1072 #if RECURSIVE_MARSHAL_READ_TRACE
1073 _dbus_verbose (" type reader %p END next() type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
1074 reader, reader->type_pos, reader->value_pos,
1075 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1076 _dbus_type_to_string (_dbus_type_reader_get_current_type (reader)));
1079 return _dbus_type_reader_get_current_type (reader) != DBUS_TYPE_INVALID;
1083 * Check whether there's another value on this "level". e.g. the next
1084 * field in a struct, the next value in an array. Returns FALSE at the
1085 * end of the current container.
1087 * You probably don't want to use this; it makes for an awkward for/while
1088 * loop. A nicer one is "while ((current_type = get_current_type()) != INVALID)"
1090 * @param reader the reader
1091 * @returns FALSE if nothing more to read at or below this level
1094 _dbus_type_reader_has_next (const DBusTypeReader *reader)
1096 /* Not efficient but works for now. */
1097 DBusTypeReader copy;
1100 return _dbus_type_reader_next (©);
1104 * Gets the string and range of said string containing the signature
1105 * of the current value. Essentially a more complete version of
1106 * _dbus_type_reader_get_current_type() (returns the full type
1107 * rather than only the outside of the onion).
1109 * Note though that the first byte in a struct signature is
1110 * #DBUS_STRUCT_BEGIN_CHAR while the current type will be
1111 * #DBUS_TYPE_STRUCT so it isn't true that the first byte of the
1112 * signature is always the same as the current type. Another
1113 * difference is that this function will still return a signature when
1114 * inside an empty array; say you recurse into empty array of int32,
1115 * the signature is "i" but the current type will always be
1116 * #DBUS_TYPE_INVALID since there are no elements to be currently
1119 * @param reader the reader
1120 * @param str_p place to return the string with the type in it
1121 * @param start_p place to return start of the type
1122 * @param len_p place to return the length of the type
1125 _dbus_type_reader_get_signature (const DBusTypeReader *reader,
1126 const DBusString **str_p,
1130 *str_p = reader->type_str;
1131 *start_p = reader->type_pos;
1132 *len_p = find_len_of_complete_type (reader->type_str, reader->type_pos);
1137 DBusString replacement; /**< Marshaled value including alignment padding */
1138 int padding; /**< How much of the replacement block is padding */
1142 replacement_block_init (ReplacementBlock *block,
1143 DBusTypeReader *reader)
1145 if (!_dbus_string_init (&block->replacement))
1148 /* % 8 is the padding to have the same align properties in
1149 * our replacement string as we do at the position being replaced
1151 block->padding = reader->value_pos % 8;
1153 if (!_dbus_string_lengthen (&block->replacement, block->padding))
1159 _dbus_string_free (&block->replacement);
1164 replacement_block_replace (ReplacementBlock *block,
1165 DBusTypeReader *reader,
1166 const DBusTypeReader *realign_root)
1168 DBusTypeWriter writer;
1169 DBusTypeReader realign_reader;
1173 _dbus_assert (realign_root != NULL);
1175 orig_len = _dbus_string_get_length (&block->replacement);
1177 realign_reader = *realign_root;
1179 #if RECURSIVE_MARSHAL_WRITE_TRACE
1180 _dbus_verbose ("INITIALIZING replacement block writer %p at value_pos %d\n",
1181 &writer, _dbus_string_get_length (&block->replacement));
1183 _dbus_type_writer_init_values_only (&writer,
1184 realign_reader.byte_order,
1185 realign_reader.type_str,
1186 realign_reader.type_pos,
1187 &block->replacement,
1188 _dbus_string_get_length (&block->replacement));
1190 _dbus_assert (realign_reader.value_pos <= reader->value_pos);
1192 #if RECURSIVE_MARSHAL_WRITE_TRACE
1193 _dbus_verbose ("COPYING from reader at value_pos %d to writer %p starting after value_pos %d\n",
1194 realign_reader.value_pos, &writer, reader->value_pos);
1197 if (!_dbus_type_writer_write_reader_partial (&writer,
1201 _dbus_string_get_length (&block->replacement) - block->padding,
1205 #if RECURSIVE_MARSHAL_WRITE_TRACE
1206 _dbus_verbose ("REPLACEMENT at padding %d len %d\n", block->padding,
1207 _dbus_string_get_length (&block->replacement) - block->padding);
1208 _dbus_verbose_bytes_of_string (&block->replacement, block->padding,
1209 _dbus_string_get_length (&block->replacement) - block->padding);
1210 _dbus_verbose ("TO BE REPLACED at value_pos = %d (align pad %d) len %d realign_reader.value_pos %d\n",
1211 reader->value_pos, reader->value_pos % 8,
1212 realign_reader.value_pos - reader->value_pos,
1213 realign_reader.value_pos);
1214 _dbus_verbose_bytes_of_string (reader->value_str,
1216 realign_reader.value_pos - reader->value_pos);
1219 /* Move the replacement into position
1220 * (realign_reader should now be at the end of the block to be replaced)
1222 if (!_dbus_string_replace_len (&block->replacement, block->padding,
1223 _dbus_string_get_length (&block->replacement) - block->padding,
1224 (DBusString*) reader->value_str,
1226 realign_reader.value_pos - reader->value_pos))
1229 /* Process our fixups now that we can't have an OOM error */
1230 apply_and_free_fixups (&fixups, reader);
1235 _dbus_string_set_length (&block->replacement, orig_len);
1236 free_fixups (&fixups);
1241 replacement_block_free (ReplacementBlock *block)
1243 _dbus_string_free (&block->replacement);
1246 /* In the variable-length case, we have to fix alignment after we insert.
1247 * The strategy is as follows:
1249 * - pad a new string to have the same alignment as the
1250 * start of the current basic value
1251 * - write the new basic value
1252 * - copy from the original reader to the new string,
1253 * which will fix the alignment of types following
1255 * - this copy has to start at realign_root,
1256 * but not really write anything until it
1257 * passes the value being set
1258 * - as an optimization, we can stop copying
1259 * when the source and dest values are both
1260 * on an 8-boundary, since we know all following
1261 * padding and alignment will be identical
1262 * - copy the new string back to the original
1263 * string, replacing the relevant part of the
1265 * - now any arrays in the original string that
1266 * contained the replaced string may have the
1267 * wrong length; so we have to fix that
1270 reader_set_basic_variable_length (DBusTypeReader *reader,
1273 const DBusTypeReader *realign_root)
1276 ReplacementBlock block;
1277 DBusTypeWriter writer;
1279 _dbus_assert (realign_root != NULL);
1283 if (!replacement_block_init (&block, reader))
1286 /* Write the new basic value */
1287 #if RECURSIVE_MARSHAL_WRITE_TRACE
1288 _dbus_verbose ("INITIALIZING writer %p to write basic value at value_pos %d of replacement string\n",
1289 &writer, _dbus_string_get_length (&block.replacement));
1291 _dbus_type_writer_init_values_only (&writer,
1296 _dbus_string_get_length (&block.replacement));
1297 #if RECURSIVE_MARSHAL_WRITE_TRACE
1298 _dbus_verbose ("WRITING basic value to writer %p (replacement string)\n", &writer);
1300 if (!_dbus_type_writer_write_basic (&writer, current_type, value))
1303 if (!replacement_block_replace (&block,
1311 replacement_block_free (&block);
1316 reader_set_basic_fixed_length (DBusTypeReader *reader,
1320 _dbus_marshal_set_basic ((DBusString*) reader->value_str,
1329 * Sets a new value for the basic type value pointed to by the reader,
1330 * leaving the reader valid to continue reading. Any other readers
1331 * will be invalidated if you set a variable-length type such as a
1334 * The provided realign_root is the reader to start from when
1335 * realigning the data that follows the newly-set value. The reader
1336 * parameter must point to a value below the realign_root parameter.
1337 * If the type being set is fixed-length, then realign_root may be
1338 * #NULL. Only values reachable from realign_root will be realigned,
1339 * so if your string contains other values you will need to deal with
1340 * those somehow yourself. It is OK if realign_root is the same
1341 * reader as the reader parameter, though if you aren't setting the
1342 * root it may not be such a good idea.
1344 * @todo DBusTypeReader currently takes "const" versions of the type
1345 * and value strings, and this function modifies those strings by
1346 * casting away the const, which is of course bad if we want to get
1347 * picky. (To be truly clean you'd have an object which contained the
1348 * type and value strings and set_basic would be a method on that
1349 * object... this would also make DBusTypeReader the same thing as
1350 * DBusTypeMark. But since DBusMessage is effectively that object for
1351 * D-Bus it doesn't seem worth creating some random object.)
1353 * @todo optimize this by only rewriting until the old and new values
1354 * are at the same alignment. Frequently this should result in only
1355 * replacing the value that's immediately at hand.
1357 * @param reader reader indicating where to set a new value
1358 * @param value address of the value to set
1359 * @param realign_root realign from here
1360 * @returns #FALSE if not enough memory
1363 _dbus_type_reader_set_basic (DBusTypeReader *reader,
1365 const DBusTypeReader *realign_root)
1369 _dbus_assert (!reader->klass->types_only);
1370 _dbus_assert (reader->value_str == realign_root->value_str);
1371 _dbus_assert (reader->value_pos >= realign_root->value_pos);
1373 current_type = _dbus_type_reader_get_current_type (reader);
1375 #if RECURSIVE_MARSHAL_WRITE_TRACE
1376 _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",
1377 reader, reader->type_pos, reader->value_pos,
1378 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1380 realign_root ? realign_root->value_pos : -1,
1381 _dbus_type_to_string (current_type));
1382 _dbus_verbose_bytes_of_string (realign_root->value_str, realign_root->value_pos,
1383 _dbus_string_get_length (realign_root->value_str) -
1384 realign_root->value_pos);
1387 _dbus_assert (dbus_type_is_basic (current_type));
1389 if (dbus_type_is_fixed (current_type))
1391 reader_set_basic_fixed_length (reader, current_type, value);
1396 _dbus_assert (realign_root != NULL);
1397 return reader_set_basic_variable_length (reader, current_type,
1398 value, realign_root);
1403 * Recursively deletes any value pointed to by the reader, leaving the
1404 * reader valid to continue reading. Any other readers will be
1407 * The provided realign_root is the reader to start from when
1408 * realigning the data that follows the newly-set value.
1409 * See _dbus_type_reader_set_basic() for more details on the
1410 * realign_root paramter.
1412 * @todo for now this does not delete the typecodes associated with
1413 * the value, so this function should only be used for array elements.
1415 * @param reader reader indicating where to delete a value
1416 * @param realign_root realign from here
1417 * @returns #FALSE if not enough memory
1420 _dbus_type_reader_delete (DBusTypeReader *reader,
1421 const DBusTypeReader *realign_root)
1424 ReplacementBlock block;
1426 _dbus_assert (realign_root != NULL);
1427 _dbus_assert (reader->klass == &array_reader_class);
1431 if (!replacement_block_init (&block, reader))
1434 if (!replacement_block_replace (&block,
1442 replacement_block_free (&block);
1447 * Compares two readers, which must be iterating over the same value data.
1448 * Returns #TRUE if the first parameter is further along than the second parameter.
1450 * @param lhs left-hand-side (first) parameter
1451 * @param rhs left-hand-side (first) parameter
1452 * @returns whether lhs is greater than rhs
1455 _dbus_type_reader_greater_than (const DBusTypeReader *lhs,
1456 const DBusTypeReader *rhs)
1458 _dbus_assert (lhs->value_str == rhs->value_str);
1460 return lhs->value_pos > rhs->value_pos;
1473 * Initialize a write iterator, which is used to write out values in
1474 * serialized D-Bus format.
1476 * The type_pos passed in is expected to be inside an already-valid,
1477 * though potentially empty, type signature. This means that the byte
1478 * after type_pos must be either #DBUS_TYPE_INVALID (aka nul) or some
1479 * other valid type. #DBusTypeWriter won't enforce that the signature
1480 * is already valid (you can append the nul byte at the end if you
1481 * like), but just be aware that you need the nul byte eventually and
1482 * #DBusTypeWriter isn't going to write it for you.
1484 * @param writer the writer to init
1485 * @param byte_order the byte order to marshal into
1486 * @param type_str the string to write typecodes into
1487 * @param type_pos where to insert typecodes
1488 * @param value_str the string to write values into
1489 * @param value_pos where to insert values
1493 _dbus_type_writer_init (DBusTypeWriter *writer,
1495 DBusString *type_str,
1497 DBusString *value_str,
1500 writer->byte_order = byte_order;
1501 writer->type_str = type_str;
1502 writer->type_pos = type_pos;
1503 writer->value_str = value_str;
1504 writer->value_pos = value_pos;
1505 writer->container_type = DBUS_TYPE_INVALID;
1506 writer->type_pos_is_expectation = FALSE;
1507 writer->enabled = TRUE;
1509 #if RECURSIVE_MARSHAL_WRITE_TRACE
1510 _dbus_verbose ("writer %p init remaining sig '%s'\n", writer,
1512 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1518 * Initialize a write iterator, with the signature to be provided
1521 * @param writer the writer to init
1522 * @param byte_order the byte order to marshal into
1523 * @param value_str the string to write values into
1524 * @param value_pos where to insert values
1528 _dbus_type_writer_init_types_delayed (DBusTypeWriter *writer,
1530 DBusString *value_str,
1533 _dbus_type_writer_init (writer, byte_order,
1534 NULL, 0, value_str, value_pos);
1538 * Adds type string to the writer, if it had none.
1540 * @param writer the writer to init
1541 * @param type_str type string to add
1542 * @param type_pos type position
1546 _dbus_type_writer_add_types (DBusTypeWriter *writer,
1547 DBusString *type_str,
1550 if (writer->type_str == NULL) /* keeps us from using this as setter */
1552 writer->type_str = type_str;
1553 writer->type_pos = type_pos;
1558 * Removes type string from the writer.
1560 * @param writer the writer to remove from
1563 _dbus_type_writer_remove_types (DBusTypeWriter *writer)
1565 writer->type_str = NULL;
1566 writer->type_pos = -1;
1570 * Like _dbus_type_writer_init(), except the type string
1571 * passed in should correspond to an existing signature that
1572 * matches what you're going to write out. The writer will
1573 * check what you write vs. this existing signature.
1575 * @param writer the writer to init
1576 * @param byte_order the byte order to marshal into
1577 * @param type_str the string with signature
1578 * @param type_pos start of signature
1579 * @param value_str the string to write values into
1580 * @param value_pos where to insert values
1584 _dbus_type_writer_init_values_only (DBusTypeWriter *writer,
1586 const DBusString *type_str,
1588 DBusString *value_str,
1591 _dbus_type_writer_init (writer, byte_order,
1592 (DBusString*)type_str, type_pos,
1593 value_str, value_pos);
1595 writer->type_pos_is_expectation = TRUE;
1599 _dbus_type_writer_write_basic_no_typecode (DBusTypeWriter *writer,
1603 if (writer->enabled)
1604 return _dbus_marshal_write_basic (writer->value_str,
1609 &writer->value_pos);
1614 /* If our parent is an array, things are a little bit complicated.
1616 * The parent must have a complete element type, such as
1617 * "i" or "aai" or "(ii)" or "a(ii)". There can't be
1618 * unclosed parens, or an "a" with no following type.
1620 * To recurse, the only allowed operation is to recurse into the
1621 * first type in the element type. So for "i" you can't recurse, for
1622 * "ai" you can recurse into the array, for "(ii)" you can recurse
1625 * If you recurse into the array for "ai", then you must specify
1626 * "i" for the element type of the array you recurse into.
1628 * While inside an array at any level, we need to avoid writing to
1629 * type_str, since the type only appears once for the whole array,
1630 * it does not appear for each array element.
1632 * While inside an array type_pos points to the expected next
1633 * typecode, rather than the next place we could write a typecode.
1636 writer_recurse_init_and_check (DBusTypeWriter *writer,
1638 DBusTypeWriter *sub)
1640 _dbus_type_writer_init (sub,
1647 sub->container_type = container_type;
1649 if (writer->type_pos_is_expectation ||
1650 (sub->container_type == DBUS_TYPE_ARRAY || sub->container_type == DBUS_TYPE_VARIANT))
1651 sub->type_pos_is_expectation = TRUE;
1653 sub->type_pos_is_expectation = FALSE;
1655 sub->enabled = writer->enabled;
1657 #ifndef DBUS_DISABLE_CHECKS
1658 if (writer->type_pos_is_expectation && writer->type_str)
1662 expected = _dbus_first_type_in_signature (writer->type_str, writer->type_pos);
1664 if (expected != sub->container_type)
1666 if (expected != DBUS_TYPE_INVALID)
1667 _dbus_warn_check_failed ("Writing an element of type %s, but the expected type here is %s\n"
1668 "The overall signature expected here was '%s' and we are on byte %d of that signature.\n",
1669 _dbus_type_to_string (sub->container_type),
1670 _dbus_type_to_string (expected),
1671 _dbus_string_get_const_data (writer->type_str), writer->type_pos);
1673 _dbus_warn_check_failed ("Writing an element of type %s, but no value is expected here\n"
1674 "The overall signature expected here was '%s' and we are on byte %d of that signature.\n",
1675 _dbus_type_to_string (sub->container_type),
1676 _dbus_string_get_const_data (writer->type_str), writer->type_pos);
1678 _dbus_assert_not_reached ("bad array element or variant content written");
1681 #endif /* DBUS_DISABLE_CHECKS */
1683 #if RECURSIVE_MARSHAL_WRITE_TRACE
1684 _dbus_verbose (" type writer %p recurse parent %s type_pos = %d value_pos = %d is_expectation = %d remaining sig '%s' enabled = %d\n",
1686 _dbus_type_to_string (writer->container_type),
1687 writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
1689 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1692 _dbus_verbose (" type writer %p recurse sub %s type_pos = %d value_pos = %d is_expectation = %d enabled = %d\n",
1694 _dbus_type_to_string (sub->container_type),
1695 sub->type_pos, sub->value_pos,
1696 sub->type_pos_is_expectation,
1702 write_or_verify_typecode (DBusTypeWriter *writer,
1705 /* A subwriter inside an array or variant will have type_pos
1706 * pointing to the expected typecode; a writer not inside an array
1707 * or variant has type_pos pointing to the next place to insert a
1710 #if RECURSIVE_MARSHAL_WRITE_TRACE
1711 _dbus_verbose (" type writer %p write_or_verify start type_pos = %d remaining sig '%s' enabled = %d\n",
1712 writer, writer->type_pos,
1714 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1719 if (writer->type_str == NULL)
1722 if (writer->type_pos_is_expectation)
1724 #ifndef DBUS_DISABLE_CHECKS
1728 expected = _dbus_string_get_byte (writer->type_str, writer->type_pos);
1730 if (expected != typecode)
1732 if (expected != DBUS_TYPE_INVALID)
1733 _dbus_warn_check_failed ("Array or variant type requires that type %s be written, but %s was written.\n"
1734 "The overall signature expected here was '%s' and we are on byte %d of that signature.\n",
1735 _dbus_type_to_string (expected), _dbus_type_to_string (typecode),
1736 _dbus_string_get_const_data (writer->type_str), writer->type_pos);
1738 _dbus_warn_check_failed ("Array or variant type wasn't expecting any more values to be written into it, but a value %s was written.\n"
1739 "The overall signature expected here was '%s' and we are on byte %d of that signature.\n",
1740 _dbus_type_to_string (typecode),
1741 _dbus_string_get_const_data (writer->type_str), writer->type_pos);
1742 _dbus_assert_not_reached ("bad type inserted somewhere inside an array or variant");
1745 #endif /* DBUS_DISABLE_CHECKS */
1747 /* if immediately inside an array we'd always be appending an element,
1748 * so the expected type doesn't change; if inside a struct or something
1749 * below an array, we need to move through said struct or something.
1751 if (writer->container_type != DBUS_TYPE_ARRAY)
1752 writer->type_pos += 1;
1756 if (!_dbus_string_insert_byte (writer->type_str,
1761 writer->type_pos += 1;
1764 #if RECURSIVE_MARSHAL_WRITE_TRACE
1765 _dbus_verbose (" type writer %p write_or_verify end type_pos = %d remaining sig '%s'\n",
1766 writer, writer->type_pos,
1767 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0));
1774 writer_recurse_struct_or_dict_entry (DBusTypeWriter *writer,
1776 const DBusString *contained_type,
1777 int contained_type_start,
1778 int contained_type_len,
1779 DBusTypeWriter *sub)
1781 /* FIXME right now contained_type is ignored; we could probably
1782 * almost trivially fix the code so if it's present we
1783 * write it out and then set type_pos_is_expectation
1786 /* Ensure that we'll be able to add alignment padding and the typecode */
1787 if (writer->enabled)
1789 if (!_dbus_string_alloc_space (sub->value_str, 8))
1793 if (!write_or_verify_typecode (sub, begin_char))
1794 _dbus_assert_not_reached ("failed to insert struct typecode after prealloc");
1796 if (writer->enabled)
1798 if (!_dbus_string_insert_bytes (sub->value_str,
1800 _DBUS_ALIGN_VALUE (sub->value_pos, 8) - sub->value_pos,
1802 _dbus_assert_not_reached ("should not have failed to insert alignment padding for struct");
1803 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
1811 writer_recurse_array (DBusTypeWriter *writer,
1812 const DBusString *contained_type,
1813 int contained_type_start,
1814 int contained_type_len,
1815 DBusTypeWriter *sub,
1816 dbus_bool_t is_array_append)
1818 dbus_uint32_t value = 0;
1822 #ifndef DBUS_DISABLE_CHECKS
1823 if (writer->container_type == DBUS_TYPE_ARRAY &&
1826 if (!_dbus_string_equal_substring (contained_type,
1827 contained_type_start,
1830 writer->u.array.element_type_pos + 1))
1832 _dbus_warn_check_failed ("Writing an array of '%s' but this is incompatible with the expected type of elements in the parent array\n",
1833 _dbus_string_get_const_data_len (contained_type,
1834 contained_type_start,
1835 contained_type_len));
1836 _dbus_assert_not_reached ("incompatible type for child array");
1839 #endif /* DBUS_DISABLE_CHECKS */
1841 if (writer->enabled && !is_array_append)
1843 /* 3 pad + 4 bytes for the array length, and 4 bytes possible padding
1844 * before array values
1846 if (!_dbus_string_alloc_space (sub->value_str, 3 + 4 + 4))
1850 if (writer->type_str != NULL)
1852 sub->type_pos += 1; /* move to point to the element type, since type_pos
1853 * should be the expected type for further writes
1855 sub->u.array.element_type_pos = sub->type_pos;
1858 if (!writer->type_pos_is_expectation)
1860 /* sub is a toplevel/outermost array so we need to write the type data */
1862 /* alloc space for array typecode, element signature */
1863 if (!_dbus_string_alloc_space (writer->type_str, 1 + contained_type_len))
1866 if (!_dbus_string_insert_byte (writer->type_str,
1869 _dbus_assert_not_reached ("failed to insert array typecode after prealloc");
1871 if (!_dbus_string_copy_len (contained_type,
1872 contained_type_start, contained_type_len,
1874 sub->u.array.element_type_pos))
1875 _dbus_assert_not_reached ("should not have failed to insert array element typecodes");
1878 if (writer->type_str != NULL)
1880 /* If the parent is an array, we hold type_pos pointing at the array element type;
1881 * otherwise advance it to reflect the array value we just recursed into
1883 if (writer->container_type != DBUS_TYPE_ARRAY)
1884 writer->type_pos += 1 + contained_type_len;
1886 _dbus_assert (writer->type_pos_is_expectation); /* because it's an array */
1889 if (writer->enabled)
1891 /* Write (or jump over, if is_array_append) the length */
1892 sub->u.array.len_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
1894 if (is_array_append)
1896 sub->value_pos += 4;
1900 if (!_dbus_type_writer_write_basic_no_typecode (sub, DBUS_TYPE_UINT32,
1902 _dbus_assert_not_reached ("should not have failed to insert array len");
1905 _dbus_assert (sub->u.array.len_pos == sub->value_pos - 4);
1907 /* Write alignment padding for array elements
1908 * Note that we write the padding *even for empty arrays*
1909 * to avoid wonky special cases
1911 alignment = element_type_get_alignment (contained_type, contained_type_start);
1913 aligned = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
1914 if (aligned != sub->value_pos)
1916 if (!is_array_append)
1918 if (!_dbus_string_insert_bytes (sub->value_str,
1920 aligned - sub->value_pos,
1922 _dbus_assert_not_reached ("should not have failed to insert alignment padding");
1925 sub->value_pos = aligned;
1928 sub->u.array.start_pos = sub->value_pos;
1930 if (is_array_append)
1934 _dbus_assert (_DBUS_ALIGN_VALUE (sub->u.array.len_pos, 4) ==
1935 (unsigned) sub->u.array.len_pos);
1936 len = _dbus_unpack_uint32 (sub->byte_order,
1937 _dbus_string_get_const_udata_len (sub->value_str,
1938 sub->u.array.len_pos,
1941 sub->value_pos += len;
1946 /* not enabled, so we won't write the len_pos; set it to -1 to so indicate */
1947 sub->u.array.len_pos = -1;
1948 sub->u.array.start_pos = sub->value_pos;
1951 _dbus_assert (sub->u.array.len_pos < sub->u.array.start_pos);
1952 _dbus_assert (is_array_append || sub->u.array.start_pos == sub->value_pos);
1954 #if RECURSIVE_MARSHAL_WRITE_TRACE
1955 _dbus_verbose (" type writer %p recurse array done remaining sig '%s' array start_pos = %d len_pos = %d value_pos = %d\n", sub,
1957 _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0) :
1959 sub->u.array.start_pos, sub->u.array.len_pos, sub->value_pos);
1965 /* Variant value will normally have:
1966 * 1 byte signature length not including nul
1967 * signature typecodes (nul terminated)
1968 * padding to alignment of contained type
1969 * body according to signature
1971 * The signature string can only have a single type
1972 * in it but that type may be complex/recursive.
1974 * So a typical variant type with the integer 3 will have these
1976 * 0x1 'i' '\0' [1 byte padding to alignment boundary] 0x0 0x0 0x0 0x3
1978 * The main world of hurt for writing out a variant is that the type
1979 * string is the same string as the value string. Which means
1980 * inserting to the type string will move the value_pos; and it means
1981 * that inserting to the type string could break type alignment.
1984 writer_recurse_variant (DBusTypeWriter *writer,
1985 const DBusString *contained_type,
1986 int contained_type_start,
1987 int contained_type_len,
1988 DBusTypeWriter *sub)
1990 int contained_alignment;
1992 if (writer->enabled)
1994 /* Allocate space for the worst case, which is 1 byte sig
1995 * length, nul byte at end of sig, and 7 bytes padding to
1998 if (!_dbus_string_alloc_space (sub->value_str, contained_type_len + 9))
2002 /* write VARIANT typecode to the parent's type string */
2003 if (!write_or_verify_typecode (writer, DBUS_TYPE_VARIANT))
2006 /* If not enabled, mark that we have no type_str anymore ... */
2008 if (!writer->enabled)
2010 sub->type_str = NULL;
2016 /* If we're enabled then continue ... */
2018 if (!_dbus_string_insert_byte (sub->value_str,
2020 contained_type_len))
2021 _dbus_assert_not_reached ("should not have failed to insert variant type sig len");
2023 sub->value_pos += 1;
2025 /* Here we switch over to the expected type sig we're about to write */
2026 sub->type_str = sub->value_str;
2027 sub->type_pos = sub->value_pos;
2029 if (!_dbus_string_copy_len (contained_type, contained_type_start, contained_type_len,
2030 sub->value_str, sub->value_pos))
2031 _dbus_assert_not_reached ("should not have failed to insert variant type sig");
2033 sub->value_pos += contained_type_len;
2035 if (!_dbus_string_insert_byte (sub->value_str,
2038 _dbus_assert_not_reached ("should not have failed to insert variant type nul termination");
2040 sub->value_pos += 1;
2042 contained_alignment = _dbus_type_get_alignment (_dbus_first_type_in_signature (contained_type, contained_type_start));
2044 if (!_dbus_string_insert_bytes (sub->value_str,
2046 _DBUS_ALIGN_VALUE (sub->value_pos, contained_alignment) - sub->value_pos,
2048 _dbus_assert_not_reached ("should not have failed to insert alignment padding for variant body");
2049 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, contained_alignment);
2055 _dbus_type_writer_recurse_contained_len (DBusTypeWriter *writer,
2057 const DBusString *contained_type,
2058 int contained_type_start,
2059 int contained_type_len,
2060 DBusTypeWriter *sub,
2061 dbus_bool_t is_array_append)
2063 writer_recurse_init_and_check (writer, container_type, sub);
2065 switch (container_type)
2067 case DBUS_TYPE_STRUCT:
2068 return writer_recurse_struct_or_dict_entry (writer,
2069 DBUS_STRUCT_BEGIN_CHAR,
2071 contained_type_start, contained_type_len,
2074 case DBUS_TYPE_DICT_ENTRY:
2075 return writer_recurse_struct_or_dict_entry (writer,
2076 DBUS_DICT_ENTRY_BEGIN_CHAR,
2078 contained_type_start, contained_type_len,
2081 case DBUS_TYPE_ARRAY:
2082 return writer_recurse_array (writer,
2083 contained_type, contained_type_start, contained_type_len,
2084 sub, is_array_append);
2086 case DBUS_TYPE_VARIANT:
2087 return writer_recurse_variant (writer,
2088 contained_type, contained_type_start, contained_type_len,
2092 _dbus_assert_not_reached ("tried to recurse into type that doesn't support that");
2099 * Opens a new container and writes out the initial information for that container.
2101 * @param writer the writer
2102 * @param container_type the type of the container to open
2103 * @param contained_type the array element type or variant content type
2104 * @param contained_type_start position to look for the type
2105 * @param sub the new sub-writer to write container contents
2106 * @returns #FALSE if no memory
2109 _dbus_type_writer_recurse (DBusTypeWriter *writer,
2111 const DBusString *contained_type,
2112 int contained_type_start,
2113 DBusTypeWriter *sub)
2115 int contained_type_len;
2118 contained_type_len = find_len_of_complete_type (contained_type, contained_type_start);
2120 contained_type_len = 0;
2122 return _dbus_type_writer_recurse_contained_len (writer, container_type,
2124 contained_type_start,
2131 * Append to an existing array. Essentially, the writer will read an
2132 * existing length at the write location; jump over that length; and
2133 * write new fields. On unrecurse(), the existing length will be
2136 * @param writer the writer
2137 * @param contained_type element type
2138 * @param contained_type_start position of element type
2139 * @param sub the subwriter to init
2140 * @returns #FALSE if no memory
2143 _dbus_type_writer_append_array (DBusTypeWriter *writer,
2144 const DBusString *contained_type,
2145 int contained_type_start,
2146 DBusTypeWriter *sub)
2148 int contained_type_len;
2151 contained_type_len = find_len_of_complete_type (contained_type, contained_type_start);
2153 contained_type_len = 0;
2155 return _dbus_type_writer_recurse_contained_len (writer, DBUS_TYPE_ARRAY,
2157 contained_type_start,
2164 writer_get_array_len (DBusTypeWriter *writer)
2166 _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY);
2167 return writer->value_pos - writer->u.array.start_pos;
2171 * Closes a container created by _dbus_type_writer_recurse()
2172 * and writes any additional information to the values block.
2174 * @param writer the writer
2175 * @param sub the sub-writer created by _dbus_type_writer_recurse()
2176 * @returns #FALSE if no memory
2179 _dbus_type_writer_unrecurse (DBusTypeWriter *writer,
2180 DBusTypeWriter *sub)
2182 /* type_pos_is_expectation never gets unset once set, or we'd get all hosed */
2183 _dbus_assert (!writer->type_pos_is_expectation ||
2184 (writer->type_pos_is_expectation && sub->type_pos_is_expectation));
2186 #if RECURSIVE_MARSHAL_WRITE_TRACE
2187 _dbus_verbose (" type writer %p unrecurse type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
2188 writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
2189 _dbus_type_to_string (writer->container_type));
2190 _dbus_verbose (" type writer %p unrecurse sub type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
2191 sub, sub->type_pos, sub->value_pos,
2192 sub->type_pos_is_expectation,
2193 _dbus_type_to_string (sub->container_type));
2196 if (sub->container_type == DBUS_TYPE_STRUCT)
2198 if (!write_or_verify_typecode (sub, DBUS_STRUCT_END_CHAR))
2201 else if (sub->container_type == DBUS_TYPE_DICT_ENTRY)
2203 if (!write_or_verify_typecode (sub, DBUS_DICT_ENTRY_END_CHAR))
2206 else if (sub->container_type == DBUS_TYPE_ARRAY)
2208 if (sub->u.array.len_pos >= 0) /* len_pos == -1 if we weren't enabled when we passed it */
2212 /* Set the array length */
2213 len = writer_get_array_len (sub);
2214 _dbus_marshal_set_uint32 (sub->value_str,
2215 sub->u.array.len_pos,
2218 #if RECURSIVE_MARSHAL_WRITE_TRACE
2219 _dbus_verbose (" filled in sub array len to %u at len_pos %d\n",
2220 len, sub->u.array.len_pos);
2223 #if RECURSIVE_MARSHAL_WRITE_TRACE
2226 _dbus_verbose (" not filling in sub array len because we were disabled when we passed the len\n");
2231 /* Now get type_pos right for the parent writer. Here are the cases:
2233 * Cases !writer->type_pos_is_expectation:
2234 * (in these cases we want to update to the new insertion point)
2236 * - if we recursed into a STRUCT then we didn't know in advance
2237 * what the types in the struct would be; so we have to fill in
2238 * that information now.
2239 * writer->type_pos = sub->type_pos
2241 * - if we recursed into anything else, we knew the full array
2242 * type, or knew the single typecode marking VARIANT, so
2243 * writer->type_pos is already correct.
2244 * writer->type_pos should remain as-is
2246 * - note that the parent is never an ARRAY or VARIANT, if it were
2247 * then type_pos_is_expectation would be TRUE. The parent
2248 * is thus known to be a toplevel or STRUCT.
2250 * Cases where writer->type_pos_is_expectation:
2251 * (in these cases we want to update to next expected type to write)
2253 * - we recursed from STRUCT into STRUCT and we didn't increment
2254 * type_pos in the parent just to stay consistent with the
2255 * !writer->type_pos_is_expectation case (though we could
2256 * special-case this in recurse_struct instead if we wanted)
2257 * writer->type_pos = sub->type_pos
2259 * - we recursed from STRUCT into ARRAY or VARIANT and type_pos
2260 * for parent should have been incremented already
2261 * writer->type_pos should remain as-is
2263 * - we recursed from ARRAY into a sub-element, so type_pos in the
2264 * parent is the element type and should remain the element type
2265 * for the benefit of the next child element
2266 * writer->type_pos should remain as-is
2268 * - we recursed from VARIANT into its value, so type_pos in the
2269 * parent makes no difference since there's only one value
2270 * and we just finished writing it and won't use type_pos again
2271 * writer->type_pos should remain as-is
2274 * For all these, DICT_ENTRY is the same as STRUCT
2276 if (writer->type_str != NULL)
2278 if ((sub->container_type == DBUS_TYPE_STRUCT ||
2279 sub->container_type == DBUS_TYPE_DICT_ENTRY) &&
2280 (writer->container_type == DBUS_TYPE_STRUCT ||
2281 writer->container_type == DBUS_TYPE_DICT_ENTRY ||
2282 writer->container_type == DBUS_TYPE_INVALID))
2284 /* Advance the parent to the next struct field */
2285 writer->type_pos = sub->type_pos;
2289 writer->value_pos = sub->value_pos;
2291 #if RECURSIVE_MARSHAL_WRITE_TRACE
2292 _dbus_verbose (" type writer %p unrecursed type_pos = %d value_pos = %d remaining sig '%s'\n",
2293 writer, writer->type_pos, writer->value_pos,
2295 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
2303 * Writes out a basic type.
2305 * @param writer the writer
2306 * @param type the type to write
2307 * @param value the address of the value to write
2308 * @returns #FALSE if no memory
2311 _dbus_type_writer_write_basic (DBusTypeWriter *writer,
2317 /* First ensure that our type realloc will succeed */
2318 if (!writer->type_pos_is_expectation && writer->type_str != NULL)
2320 if (!_dbus_string_alloc_space (writer->type_str, 1))
2326 if (!_dbus_type_writer_write_basic_no_typecode (writer, type, value))
2329 if (!write_or_verify_typecode (writer, type))
2330 _dbus_assert_not_reached ("failed to write typecode after prealloc");
2335 #if RECURSIVE_MARSHAL_WRITE_TRACE
2336 _dbus_verbose (" type writer %p basic type_pos = %d value_pos = %d is_expectation = %d enabled = %d\n",
2337 writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
2345 * Writes a block of fixed-length basic values, i.e. those that are
2346 * both dbus_type_is_fixed() and _dbus_type_is_basic(). The block
2347 * must be written inside an array.
2349 * The value parameter should be the address of said array of values,
2350 * so e.g. if it's an array of double, pass in "const double**"
2352 * @param writer the writer
2353 * @param element_type type of stuff in the array
2354 * @param value address of the array
2355 * @param n_elements number of elements in the array
2356 * @returns #FALSE if no memory
2359 _dbus_type_writer_write_fixed_multi (DBusTypeWriter *writer,
2364 _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY);
2365 _dbus_assert (dbus_type_is_fixed (element_type));
2366 _dbus_assert (writer->type_pos_is_expectation);
2367 _dbus_assert (n_elements >= 0);
2369 #if RECURSIVE_MARSHAL_WRITE_TRACE
2370 _dbus_verbose (" type writer %p entering fixed multi type_pos = %d value_pos = %d n_elements %d\n",
2371 writer, writer->type_pos, writer->value_pos, n_elements);
2374 if (!write_or_verify_typecode (writer, element_type))
2375 _dbus_assert_not_reached ("OOM should not happen if only verifying typecode");
2377 if (writer->enabled)
2379 if (!_dbus_marshal_write_fixed_multi (writer->value_str,
2385 &writer->value_pos))
2389 #if RECURSIVE_MARSHAL_WRITE_TRACE
2390 _dbus_verbose (" type writer %p fixed multi written new type_pos = %d new value_pos = %d n_elements %d\n",
2391 writer, writer->type_pos, writer->value_pos, n_elements);
2398 enable_if_after (DBusTypeWriter *writer,
2399 DBusTypeReader *reader,
2400 const DBusTypeReader *start_after)
2404 if (!writer->enabled && _dbus_type_reader_greater_than (reader, start_after))
2406 _dbus_type_writer_set_enabled (writer, TRUE);
2407 #if RECURSIVE_MARSHAL_WRITE_TRACE
2408 _dbus_verbose ("ENABLING writer %p at %d because reader at value_pos %d is after reader at value_pos %d\n",
2409 writer, writer->value_pos, reader->value_pos, start_after->value_pos);
2413 _dbus_assert ((!writer->enabled && !_dbus_type_reader_greater_than (reader, start_after)) ||
2414 (writer->enabled && _dbus_type_reader_greater_than (reader, start_after)));
2419 append_fixup (DBusList **fixups,
2420 const DBusArrayLenFixup *fixup)
2422 DBusArrayLenFixup *f;
2424 f = dbus_new (DBusArrayLenFixup, 1);
2430 if (!_dbus_list_append (fixups, f))
2436 _dbus_assert (f->len_pos_in_reader == fixup->len_pos_in_reader);
2437 _dbus_assert (f->new_len == fixup->new_len);
2442 /* This loop is trivial if you ignore all the start_after nonsense,
2443 * so if you're trying to figure it out, start by ignoring that
2446 writer_write_reader_helper (DBusTypeWriter *writer,
2447 DBusTypeReader *reader,
2448 const DBusTypeReader *start_after,
2449 int start_after_new_pos,
2450 int start_after_new_len,
2452 dbus_bool_t inside_start_after)
2456 while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
2458 if (dbus_type_is_container (current_type))
2460 DBusTypeReader subreader;
2461 DBusTypeWriter subwriter;
2462 const DBusString *sig_str;
2465 dbus_bool_t enabled_at_recurse;
2466 dbus_bool_t past_start_after;
2467 int reader_array_len_pos;
2468 int reader_array_start_pos;
2469 dbus_bool_t this_is_start_after;
2471 /* type_pos is checked since e.g. in a struct the struct
2472 * and its first field have the same value_pos.
2473 * type_str will differ in reader/start_after for variants
2474 * where type_str is inside the value_str
2476 if (!inside_start_after && start_after &&
2477 reader->value_pos == start_after->value_pos &&
2478 reader->type_str == start_after->type_str &&
2479 reader->type_pos == start_after->type_pos)
2480 this_is_start_after = TRUE;
2482 this_is_start_after = FALSE;
2484 _dbus_type_reader_recurse (reader, &subreader);
2486 if (current_type == DBUS_TYPE_ARRAY)
2488 reader_array_len_pos = ARRAY_READER_LEN_POS (&subreader);
2489 reader_array_start_pos = subreader.u.array.start_pos;
2494 reader_array_len_pos = -1;
2495 reader_array_start_pos = -1;
2498 _dbus_type_reader_get_signature (&subreader, &sig_str,
2499 &sig_start, &sig_len);
2501 #if RECURSIVE_MARSHAL_WRITE_TRACE
2502 _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",
2503 _dbus_type_to_string (current_type),
2505 subreader.value_pos,
2507 start_after ? start_after->value_pos : -1,
2508 _dbus_string_get_length (writer->value_str),
2509 inside_start_after, this_is_start_after);
2512 if (!inside_start_after && !this_is_start_after)
2513 enable_if_after (writer, &subreader, start_after);
2514 enabled_at_recurse = writer->enabled;
2515 if (!_dbus_type_writer_recurse_contained_len (writer, current_type,
2516 sig_str, sig_start, sig_len,
2520 #if RECURSIVE_MARSHAL_WRITE_TRACE
2521 _dbus_verbose ("recursed into subwriter at %d write target len %d\n",
2522 subwriter.value_pos,
2523 _dbus_string_get_length (subwriter.value_str));
2526 if (!writer_write_reader_helper (&subwriter, &subreader, start_after,
2527 start_after_new_pos, start_after_new_len,
2529 inside_start_after ||
2530 this_is_start_after))
2533 #if RECURSIVE_MARSHAL_WRITE_TRACE
2534 _dbus_verbose ("about to unrecurse from %s subreader at %d writer at %d subwriter at %d write target len %d\n",
2535 _dbus_type_to_string (current_type),
2536 subreader.value_pos,
2538 subwriter.value_pos,
2539 _dbus_string_get_length (writer->value_str));
2542 if (!inside_start_after && !this_is_start_after)
2543 enable_if_after (writer, &subreader, start_after);
2544 past_start_after = writer->enabled;
2545 if (!_dbus_type_writer_unrecurse (writer, &subwriter))
2548 /* If we weren't enabled when we recursed, we didn't
2549 * write an array len; if we passed start_after
2550 * somewhere inside the array, then we need to generate
2553 if (start_after != NULL &&
2554 !enabled_at_recurse && past_start_after &&
2555 current_type == DBUS_TYPE_ARRAY &&
2558 DBusArrayLenFixup fixup;
2559 int bytes_written_after_start_after;
2560 int bytes_before_start_after;
2563 /* this subwriter access is moderately unkosher since we
2564 * already unrecursed, but it works as long as unrecurse
2565 * doesn't break us on purpose
2567 bytes_written_after_start_after = writer_get_array_len (&subwriter);
2569 bytes_before_start_after =
2570 start_after->value_pos - reader_array_start_pos;
2572 fixup.len_pos_in_reader = reader_array_len_pos;
2574 bytes_before_start_after +
2575 start_after_new_len +
2576 bytes_written_after_start_after;
2578 _dbus_assert (_DBUS_ALIGN_VALUE (fixup.len_pos_in_reader, 4) ==
2579 (unsigned) fixup.len_pos_in_reader);
2581 old_len = _dbus_unpack_uint32 (reader->byte_order,
2582 _dbus_string_get_const_udata_len (reader->value_str,
2583 fixup.len_pos_in_reader, 4));
2585 if (old_len != fixup.new_len && !append_fixup (fixups, &fixup))
2588 #if RECURSIVE_MARSHAL_WRITE_TRACE
2589 _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",
2590 fixup.len_pos_in_reader,
2592 reader_array_start_pos,
2593 start_after->value_pos,
2594 bytes_before_start_after,
2595 start_after_new_len,
2596 bytes_written_after_start_after);
2604 _dbus_assert (dbus_type_is_basic (current_type));
2606 #if RECURSIVE_MARSHAL_WRITE_TRACE
2607 _dbus_verbose ("Reading basic value %s at %d\n",
2608 _dbus_type_to_string (current_type),
2612 _dbus_type_reader_read_basic (reader, &val);
2614 #if RECURSIVE_MARSHAL_WRITE_TRACE
2615 _dbus_verbose ("Writing basic value %s at %d write target len %d inside_start_after = %d\n",
2616 _dbus_type_to_string (current_type),
2618 _dbus_string_get_length (writer->value_str),
2619 inside_start_after);
2621 if (!inside_start_after)
2622 enable_if_after (writer, reader, start_after);
2623 if (!_dbus_type_writer_write_basic (writer, current_type, &val))
2625 #if RECURSIVE_MARSHAL_WRITE_TRACE
2626 _dbus_verbose ("Wrote basic value %s, new value_pos %d write target len %d\n",
2627 _dbus_type_to_string (current_type),
2629 _dbus_string_get_length (writer->value_str));
2633 _dbus_type_reader_next (reader);
2640 apply_and_free_fixups (fixups, NULL); /* NULL for reader to apply to */
2646 * Iterate through all values in the given reader, writing a copy of
2647 * each value to the writer. The reader will be moved forward to its
2650 * If a reader start_after is provided, it should be a reader for the
2651 * same data as the reader to be written. Only values occurring after
2652 * the value pointed to by start_after will be written to the writer.
2654 * If start_after is provided, then the copy of the reader will be
2655 * partial. This means that array lengths will not have been copied.
2656 * The assumption is that you wrote a new version of the value at
2657 * start_after to the writer. You have to pass in the start position
2658 * and length of the new value. (If you are deleting the value
2659 * at start_after, pass in 0 for the length.)
2661 * If the fixups parameter is non-#NULL, then any array length that
2662 * was read but not written due to start_after will be provided
2663 * as a #DBusArrayLenFixup. The fixup contains the position of the
2664 * array length in the source data, and the correct array length
2665 * assuming you combine the source data before start_after with
2666 * the written data at start_after and beyond.
2668 * @param writer the writer to copy to
2669 * @param reader the reader to copy from
2670 * @param start_after #NULL or a reader showing where to start
2671 * @param start_after_new_pos the position of start_after equivalent in the target data
2672 * @param start_after_new_len the length of start_after equivalent in the target data
2673 * @param fixups list to append #DBusArrayLenFixup if the write was partial
2674 * @returns #FALSE if no memory
2677 _dbus_type_writer_write_reader_partial (DBusTypeWriter *writer,
2678 DBusTypeReader *reader,
2679 const DBusTypeReader *start_after,
2680 int start_after_new_pos,
2681 int start_after_new_len,
2684 DBusTypeWriter orig;
2691 orig_type_len = _dbus_string_get_length (writer->type_str);
2692 orig_value_len = _dbus_string_get_length (writer->value_str);
2693 orig_enabled = writer->enabled;
2696 _dbus_type_writer_set_enabled (writer, FALSE);
2698 if (!writer_write_reader_helper (writer, reader, start_after,
2699 start_after_new_pos,
2700 start_after_new_len,
2704 _dbus_type_writer_set_enabled (writer, orig_enabled);
2708 if (!writer->type_pos_is_expectation)
2710 new_bytes = _dbus_string_get_length (writer->type_str) - orig_type_len;
2711 _dbus_string_delete (writer->type_str, orig.type_pos, new_bytes);
2713 new_bytes = _dbus_string_get_length (writer->value_str) - orig_value_len;
2714 _dbus_string_delete (writer->value_str, orig.value_pos, new_bytes);
2722 * Iterate through all values in the given reader, writing a copy of
2723 * each value to the writer. The reader will be moved forward to its
2726 * @param writer the writer to copy to
2727 * @param reader the reader to copy from
2728 * @returns #FALSE if no memory
2731 _dbus_type_writer_write_reader (DBusTypeWriter *writer,
2732 DBusTypeReader *reader)
2734 return _dbus_type_writer_write_reader_partial (writer, reader, NULL, 0, 0, NULL);
2738 * If disabled, a writer can still be iterated forward and recursed/unrecursed
2739 * but won't write any values. Types will still be written unless the
2740 * writer is a "values only" writer, because the writer needs access to
2741 * a valid signature to be able to iterate.
2743 * @param writer the type writer
2744 * @param enabled #TRUE if values should be written
2747 _dbus_type_writer_set_enabled (DBusTypeWriter *writer,
2748 dbus_bool_t enabled)
2750 writer->enabled = enabled != FALSE;
2753 /** @} */ /* end of DBusMarshal group */
2755 /* tests in dbus-marshal-recursive-util.c */