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.
5 * Copyright (C) 2015 Samsung Electronics
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "dbus-marshal-recursive.h"
27 #include "dbus-marshal-basic.h"
28 #include "dbus-signature.h"
29 #include "dbus-internals.h"
30 #include "dbus-marshal-gvariant.h"
33 * @addtogroup DBusMarshal
37 static dbus_bool_t _dbus_type_reader_greater_than (const DBusTypeReader *lhs,
38 const DBusTypeReader *rhs);
40 static void _dbus_type_writer_set_enabled (DBusTypeWriter *writer,
42 static dbus_bool_t _dbus_type_writer_write_reader_partial (DBusTypeWriter *writer,
43 DBusTypeReader *reader,
44 const DBusTypeReader *start_after,
45 int start_after_new_pos,
46 int start_after_new_len,
49 /** turn this on to get deluged in TypeReader verbose spam */
50 #define RECURSIVE_MARSHAL_READ_TRACE 0
52 /** turn this on to get deluged in TypeWriter verbose spam */
53 #define RECURSIVE_MARSHAL_WRITE_TRACE 0
56 free_fixups (DBusList **fixups)
60 link = _dbus_list_get_first_link (fixups);
65 next = _dbus_list_get_next_link (fixups, link);
67 dbus_free (link->data);
68 _dbus_list_free_link (link);
77 apply_and_free_fixups (DBusList **fixups,
78 DBusTypeReader *reader)
82 #if RECURSIVE_MARSHAL_WRITE_TRACE
84 _dbus_verbose (" %d FIXUPS to apply\n",
85 _dbus_list_get_length (fixups));
88 link = _dbus_list_get_first_link (fixups);
93 next = _dbus_list_get_next_link (fixups, link);
101 #if RECURSIVE_MARSHAL_WRITE_TRACE
102 _dbus_verbose (" applying FIXUP to reader %p at pos %d new_len = %d old len %d\n",
103 reader, f->len_pos_in_reader, f->new_len,
104 _dbus_marshal_read_uint32 (reader->value_str,
105 f->len_pos_in_reader,
106 reader->byte_order, NULL));
109 _dbus_marshal_set_uint32 ((DBusString*) reader->value_str,
110 f->len_pos_in_reader,
115 dbus_free (link->data);
116 _dbus_list_free_link (link);
125 * Virtual table for a type reader.
127 struct DBusTypeReaderClass
129 const char *name; /**< name for debugging */
130 int id; /**< index in all_reader_classes */
131 dbus_bool_t types_only; /**< only iterates over types, not values */
132 void (* recurse) (DBusTypeReader *sub,
133 DBusTypeReader *parent); /**< recurse with this reader as sub */
134 dbus_bool_t (* check_finished) (const DBusTypeReader *reader); /**< check whether reader is at the end */
135 void (* next) (DBusTypeReader *reader,
136 int current_type); /**< go to the next value */
140 element_type_get_alignment (const DBusString *str,
143 return _dbus_type_get_alignment (_dbus_first_type_in_signature (str, pos));
147 reader_init (DBusTypeReader *reader,
149 const DBusString *type_str,
151 const DBusString *value_str,
153 dbus_bool_t gvariant)
155 _DBUS_ZERO (*reader);
156 reader->byte_order = byte_order;
157 reader->finished = FALSE;
158 reader->type_str = type_str;
159 reader->type_pos = type_pos;
160 reader->value_str = value_str;
161 reader->value_pos = value_pos;
162 reader->value_start = value_pos;
163 reader->gvariant = gvariant;
164 reader->variable_index = 0;
165 reader->offsets_from_back = TRUE;
166 reader->is_variant = FALSE;
170 base_reader_recurse (DBusTypeReader *sub,
171 DBusTypeReader *parent)
173 /* point subreader at the same place as parent */
184 struct_or_dict_entry_types_only_reader_recurse (DBusTypeReader *sub,
185 DBusTypeReader *parent)
187 base_reader_recurse (sub, parent);
189 _dbus_assert (_dbus_string_get_byte (sub->type_str,
190 sub->type_pos) == DBUS_STRUCT_BEGIN_CHAR ||
191 _dbus_string_get_byte (sub->type_str,
192 sub->type_pos) == DBUS_DICT_ENTRY_BEGIN_CHAR);
198 struct_or_dict_entry_reader_recurse (DBusTypeReader *sub,
199 DBusTypeReader *parent)
201 struct_or_dict_entry_types_only_reader_recurse (sub, parent);
206 /* check if current type is fixed or variable */
208 int size = _dbus_reader_get_type_fixed_size (parent, &alignment);
209 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, alignment); /* adjust alignment */
210 sub->value_start = sub->value_pos;
211 sub->n_offsets = _dbus_reader_count_offsets (sub);
212 sub->offsets_from_back = TRUE;
216 sub->value_end = _dbus_reader_get_offset_of_end_of_variable (parent);
220 sub->value_end = sub->value_pos + size;
225 /* struct and dict entry have 8 byte alignment */
226 sub->value_pos = sub->value_start = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
231 array_types_only_reader_recurse (DBusTypeReader *sub,
232 DBusTypeReader *parent)
234 base_reader_recurse (sub, parent);
236 /* point type_pos at the array element type */
239 /* Init with values likely to crash things if misused */
240 sub->u.array.start_pos = _DBUS_INT_MAX;
241 sub->array_len_offset = 7;
244 /** compute position of array length given array_len_offset, which is
245 the offset back from start_pos to end of the len */
246 #define ARRAY_READER_LEN_POS(reader) \
247 ((reader)->u.array.start_pos - ((int)(reader)->array_len_offset) - 4)
250 array_reader_get_array_len (const DBusTypeReader *reader)
252 dbus_uint32_t array_len;
255 if (reader->gvariant)
256 return reader->value_end - reader->value_start;
258 len_pos = ARRAY_READER_LEN_POS (reader);
260 _dbus_assert (_DBUS_ALIGN_VALUE (len_pos, 4) == (unsigned) len_pos);
261 array_len = _dbus_unpack_uint32 (reader->byte_order,
262 _dbus_string_get_const_data_len (reader->value_str, len_pos, 4));
264 #if RECURSIVE_MARSHAL_READ_TRACE
265 _dbus_verbose (" reader %p len_pos %d array len %u len_offset %d\n",
266 reader, len_pos, array_len, reader->array_len_offset);
269 _dbus_assert (reader->u.array.start_pos - len_pos - 4 < 8);
275 array_reader_recurse (DBusTypeReader *sub,
276 DBusTypeReader *parent)
281 array_types_only_reader_recurse (sub, parent);
285 int size = _dbus_reader_get_type_fixed_size (sub, &alignment);
286 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
287 sub->value_start = sub->value_pos;
288 sub->offsets_from_back = FALSE;
289 sub->value_end = _dbus_reader_get_offset_of_end_of_variable (parent);
290 sub->variable_index = 0;
292 sub->n_offsets = _dbus_reader_count_array_elems (sub);
295 sub->u.array.start_pos = sub->value_start;
296 sub->finished = (sub->value_end == sub->value_start);
300 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
302 len_pos = sub->value_pos;
304 sub->value_pos += 4; /* for the length */
306 alignment = element_type_get_alignment (sub->type_str,
309 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
311 sub->u.array.start_pos = sub->value_pos;
312 _dbus_assert ((sub->u.array.start_pos - (len_pos + 4)) < 8); /* only 3 bits in array_len_offset */
313 sub->array_len_offset = sub->u.array.start_pos - (len_pos + 4);
316 #if RECURSIVE_MARSHAL_READ_TRACE
317 _dbus_verbose (" type reader %p array start = %d len_offset = %d array len = %d array element type = %s\n",
319 sub->u.array.start_pos,
320 sub->array_len_offset,
321 array_reader_get_array_len (sub),
322 _dbus_type_to_string (_dbus_first_type_in_signature (sub->type_str,
328 variant_reader_recurse (DBusTypeReader *sub,
329 DBusTypeReader *parent)
332 int contained_alignment;
334 base_reader_recurse (sub, parent);
338 /* GVariant's Variant is values, then nul byte, then signature.
339 * Variant's alignment is 8.
341 sub->value_pos = sub->value_start = _DBUS_ALIGN_VALUE (sub->value_pos, 8); /* adjust alignment */
342 sub->value_end = _dbus_reader_get_offset_of_end_of_variable (parent);
344 /* find beginning of signature in variant */
345 sub->type_str = sub->value_str;
346 sub->type_pos = sub->value_end - 1;
348 while (sub->type_pos > 0 && _dbus_string_get_byte (sub->type_str, sub->type_pos) != 0)
351 if (_dbus_string_get_byte (sub->type_str, sub->type_pos) == 0)
354 /* set the end of variant's value to the zero byte before signature */
355 sub->value_end = sub->type_pos - 1;
356 sub->is_variant = TRUE;
360 /* Variant is 1 byte sig length (without nul), signature with nul,
361 * padding to 8-boundary, then values
364 sig_len = _dbus_string_get_byte (sub->value_str, sub->value_pos);
366 sub->type_str = sub->value_str;
367 sub->type_pos = sub->value_pos + 1;
369 sub->value_pos = sub->type_pos + sig_len + 1;
371 contained_alignment = _dbus_type_get_alignment (_dbus_first_type_in_signature (sub->type_str,
374 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, contained_alignment);
377 #if RECURSIVE_MARSHAL_READ_TRACE
378 _dbus_verbose (" type reader %p variant containing '%s'\n",
380 _dbus_string_get_const_data_len (sub->type_str,
386 array_reader_check_finished (const DBusTypeReader *reader)
390 /* return the array element type if elements remain, and
391 * TYPE_INVALID otherwise
394 end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
396 _dbus_assert (reader->value_pos <= end_pos);
397 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
399 return reader->value_pos == end_pos;
403 skip_one_complete_type (const DBusString *type_str,
406 _dbus_type_signature_next (_dbus_string_get_const_data (type_str),
411 * Skips to the next "complete" type inside a type signature.
412 * The signature is read starting at type_pos, and the next
413 * type position is stored in the same variable.
415 * @param type_str a type signature (must be valid)
416 * @param type_pos an integer position in the type signature (in and out)
419 _dbus_type_signature_next (const char *type_str,
422 const unsigned char *p;
423 const unsigned char *start;
425 _dbus_assert (type_str != NULL);
426 _dbus_assert (type_pos != NULL);
429 p = start + *type_pos;
431 _dbus_assert (*p != DBUS_STRUCT_END_CHAR);
432 _dbus_assert (*p != DBUS_DICT_ENTRY_END_CHAR);
434 while (*p == DBUS_TYPE_ARRAY)
437 _dbus_assert (*p != DBUS_STRUCT_END_CHAR);
438 _dbus_assert (*p != DBUS_DICT_ENTRY_END_CHAR);
440 if (*p == DBUS_STRUCT_BEGIN_CHAR)
448 _dbus_assert (*p != DBUS_TYPE_INVALID);
452 _dbus_assert (*p != DBUS_TYPE_INVALID);
454 if (*p == DBUS_STRUCT_BEGIN_CHAR)
456 else if (*p == DBUS_STRUCT_END_CHAR)
467 else if (*p == DBUS_DICT_ENTRY_BEGIN_CHAR)
475 _dbus_assert (*p != DBUS_TYPE_INVALID);
479 _dbus_assert (*p != DBUS_TYPE_INVALID);
481 if (*p == DBUS_DICT_ENTRY_BEGIN_CHAR)
483 else if (*p == DBUS_DICT_ENTRY_END_CHAR)
499 *type_pos = (int) (p - start);
503 find_len_of_complete_type (const DBusString *type_str,
510 skip_one_complete_type (type_str, &end);
512 return end - type_pos;
516 base_reader_next (DBusTypeReader *reader,
519 switch (current_type)
521 case DBUS_TYPE_DICT_ENTRY:
522 case DBUS_TYPE_STRUCT:
523 case DBUS_TYPE_VARIANT:
524 /* Scan forward over the entire container contents */
525 /* FIXME for GVariant - use offsets */
529 if (reader->klass->types_only && current_type == DBUS_TYPE_VARIANT)
533 /* Recurse into the struct or variant */
534 _dbus_type_reader_recurse (reader, &sub);
536 /* Skip everything in this subreader */
537 while (_dbus_type_reader_next (&sub))
542 if (!reader->klass->types_only)
543 reader->value_pos = sub.value_pos;
545 /* Now we are at the end of this container; for variants, the
546 * subreader's type_pos is totally inapplicable (it's in the
547 * value string) but we know that we increment by one past the
550 if (current_type == DBUS_TYPE_VARIANT)
551 reader->type_pos += 1;
553 reader->type_pos = sub.type_pos;
557 case DBUS_TYPE_ARRAY:
559 if (!reader->klass->types_only)
560 _dbus_marshal_skip_array (reader->value_str,
561 _dbus_first_type_in_signature (reader->type_str,
562 reader->type_pos + 1),
566 skip_one_complete_type (reader->type_str, &reader->type_pos);
571 if (!reader->klass->types_only)
572 (reader->gvariant ? _dbus_marshal_skip_gvariant_basic : _dbus_marshal_skip_basic) (
574 current_type, reader->byte_order,
577 reader->type_pos += 1;
583 struct_or_dict_entry_reader_next (DBusTypeReader *reader,
586 if (reader->gvariant)
589 int size = _dbus_reader_get_type_fixed_size (reader, &alignment);
592 /* variable size - use offsets*/
593 reader->value_pos = _dbus_reader_get_offset_of_end_of_variable (reader);
594 reader->variable_index++;
598 /* just move, but consider alignment */
599 reader->value_pos = _DBUS_ALIGN_VALUE(reader->value_pos, alignment) + size;
602 skip_one_complete_type (reader->type_str, &reader->type_pos);
606 base_reader_next (reader, current_type);
611 struct_reader_next (DBusTypeReader *reader,
616 struct_or_dict_entry_reader_next (reader, current_type);
618 /* for STRUCT containers we return FALSE at the end of the struct,
619 * for INVALID we return FALSE at the end of the signature.
620 * In both cases we arrange for get_current_type() to return INVALID
621 * which is defined to happen iff we're at the end (no more next())
623 t = _dbus_string_get_byte (reader->type_str, reader->type_pos);
624 if (t == DBUS_STRUCT_END_CHAR)
626 reader->type_pos += 1;
627 reader->finished = TRUE;
632 body_reader_next (DBusTypeReader *reader,
635 if (reader->gvariant)
636 struct_reader_next (reader, current_type);
638 base_reader_next (reader, current_type);
642 dict_entry_reader_next (DBusTypeReader *reader,
647 struct_or_dict_entry_reader_next (reader, current_type);
649 /* for STRUCT containers we return FALSE at the end of the struct,
650 * for INVALID we return FALSE at the end of the signature.
651 * In both cases we arrange for get_current_type() to return INVALID
652 * which is defined to happen iff we're at the end (no more next())
654 t = _dbus_string_get_byte (reader->type_str, reader->type_pos);
655 if (t == DBUS_DICT_ENTRY_END_CHAR)
657 reader->type_pos += 1;
658 reader->finished = TRUE;
663 array_types_only_reader_next (DBusTypeReader *reader,
666 /* We have one "element" to be iterated over
667 * in each array, which is its element type.
668 * So the finished flag indicates whether we've
669 * iterated over it yet or not.
671 reader->finished = TRUE;
675 array_reader_next (DBusTypeReader *reader,
678 /* Skip one array element */
681 if (reader->gvariant)
684 int size = _dbus_reader_get_type_fixed_size (reader, &alignment);
687 /* variable size - use offsets*/
688 reader->value_pos = _dbus_reader_get_offset_of_end_of_variable (reader);
689 reader->variable_index++;
690 reader->finished = (reader->variable_index >= reader->n_offsets);
694 /* fixed size - move on; consider alignment */
695 reader->value_pos = _DBUS_ALIGN_VALUE(reader->value_pos, alignment) + size;
696 reader->finished = ((size_t)reader->value_pos >= reader->value_end);
701 end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
703 #if RECURSIVE_MARSHAL_READ_TRACE
704 _dbus_verbose (" reader %p array next START start_pos = %d end_pos = %d value_pos = %d current_type = %s\n",
706 reader->u.array.start_pos,
707 end_pos, reader->value_pos,
708 _dbus_type_to_string (current_type));
711 _dbus_assert (reader->value_pos < end_pos);
712 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
714 switch (_dbus_first_type_in_signature (reader->type_str,
717 case DBUS_TYPE_DICT_ENTRY:
718 case DBUS_TYPE_STRUCT:
719 case DBUS_TYPE_VARIANT:
723 /* Recurse into the struct or variant */
724 _dbus_type_reader_recurse (reader, &sub);
726 /* Skip everything in this element */
727 while (_dbus_type_reader_next (&sub))
732 /* Now we are at the end of this element */
733 reader->value_pos = sub.value_pos;
737 case DBUS_TYPE_ARRAY:
739 _dbus_marshal_skip_array (reader->value_str,
740 _dbus_first_type_in_signature (reader->type_str,
741 reader->type_pos + 1),
749 _dbus_marshal_skip_basic (reader->value_str,
750 current_type, reader->byte_order,
756 #if RECURSIVE_MARSHAL_READ_TRACE
757 _dbus_verbose (" reader %p array next END start_pos = %d end_pos = %d value_pos = %d current_type = %s\n",
759 reader->u.array.start_pos,
760 end_pos, reader->value_pos,
761 _dbus_type_to_string (current_type));
764 _dbus_assert (reader->value_pos <= end_pos);
766 if (reader->value_pos == end_pos)
768 skip_one_complete_type (reader->type_str,
774 variant_reader_next (DBusTypeReader *reader,
777 if (reader->gvariant)
779 if (!reader->klass->types_only)
780 reader->value_pos = reader->value_end;
782 reader->type_pos += 1;
784 reader->finished = TRUE;
786 reader->variable_index++;
790 base_reader_next (reader, current_type);
794 static const DBusTypeReaderClass body_reader_class = {
797 NULL, /* body is always toplevel, so doesn't get recursed into */
802 static const DBusTypeReaderClass body_types_only_reader_class = {
805 NULL, /* body is always toplevel, so doesn't get recursed into */
810 static const DBusTypeReaderClass struct_reader_class = {
813 struct_or_dict_entry_reader_recurse,
818 static const DBusTypeReaderClass struct_types_only_reader_class = {
821 struct_or_dict_entry_types_only_reader_recurse,
826 static const DBusTypeReaderClass dict_entry_reader_class = {
829 struct_or_dict_entry_reader_recurse,
831 dict_entry_reader_next
834 static const DBusTypeReaderClass dict_entry_types_only_reader_class = {
835 "dict_entry types", 5,
837 struct_or_dict_entry_types_only_reader_recurse,
839 dict_entry_reader_next
842 static const DBusTypeReaderClass array_reader_class = {
845 array_reader_recurse,
846 array_reader_check_finished,
850 static const DBusTypeReaderClass array_types_only_reader_class = {
853 array_types_only_reader_recurse,
855 array_types_only_reader_next
858 static const DBusTypeReaderClass variant_reader_class = {
861 variant_reader_recurse,
866 #ifndef DBUS_DISABLE_ASSERT
867 static const DBusTypeReaderClass * const
868 all_reader_classes[] = {
870 &body_types_only_reader_class,
871 &struct_reader_class,
872 &struct_types_only_reader_class,
873 &dict_entry_reader_class,
874 &dict_entry_types_only_reader_class,
876 &array_types_only_reader_class,
877 &variant_reader_class
882 * Initializes a type reader.
884 * @param reader the reader
885 * @param byte_order the byte order of the block to read
886 * @param type_str the signature of the block to read
887 * @param type_pos location of signature
888 * @param value_str the string containing values block
889 * @param value_pos start of values block
892 _dbus_type_reader_init (DBusTypeReader *reader,
894 const DBusString *type_str,
896 const DBusString *value_str,
899 reader_init (reader, byte_order, type_str, type_pos,
900 value_str, value_pos, FALSE);
902 reader->klass = &body_reader_class;
904 #if RECURSIVE_MARSHAL_READ_TRACE
905 _dbus_verbose (" type reader %p init type_pos = %d value_pos = %d remaining sig '%s'\n",
906 reader, reader->type_pos, reader->value_pos,
907 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
912 * Like _dbus_type_reader_init() but the iteration is over the
913 * signature, not over values.
915 * @param reader the reader
916 * @param type_str the signature string
917 * @param type_pos location in the signature string
920 _dbus_type_reader_init_types_only (DBusTypeReader *reader,
921 const DBusString *type_str,
924 reader_init (reader, DBUS_COMPILER_BYTE_ORDER /* irrelevant */,
925 type_str, type_pos, NULL, _DBUS_INT_MAX /* crashes if we screw up */,
928 reader->klass = &body_types_only_reader_class;
930 #if RECURSIVE_MARSHAL_READ_TRACE
931 _dbus_verbose (" type reader %p init types only type_pos = %d remaining sig '%s'\n",
932 reader, reader->type_pos,
933 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
938 * Gets the type of the value the reader is currently pointing to;
939 * or for a types-only reader gets the type it's currently pointing to.
940 * If the reader is at the end of a block or end of a container such
941 * as an array, returns #DBUS_TYPE_INVALID.
943 * @param reader the reader
946 _dbus_type_reader_get_current_type (const DBusTypeReader *reader)
950 if (reader->finished ||
951 (reader->klass->check_finished &&
952 (* reader->klass->check_finished) (reader)))
953 t = DBUS_TYPE_INVALID;
955 t = _dbus_first_type_in_signature (reader->type_str,
958 _dbus_assert (t != DBUS_STRUCT_END_CHAR);
959 _dbus_assert (t != DBUS_STRUCT_BEGIN_CHAR);
960 _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
961 _dbus_assert (t != DBUS_DICT_ENTRY_BEGIN_CHAR);
964 _dbus_verbose (" type reader %p current type_pos = %d type = %s\n",
965 reader, reader->type_pos,
966 _dbus_type_to_string (t));
973 * Gets the type of an element of the array the reader is currently
974 * pointing to. It's an error to call this if
975 * _dbus_type_reader_get_current_type() doesn't return #DBUS_TYPE_ARRAY
978 * @param reader the reader
981 _dbus_type_reader_get_element_type (const DBusTypeReader *reader)
985 _dbus_assert (_dbus_type_reader_get_current_type (reader) == DBUS_TYPE_ARRAY);
987 element_type = _dbus_first_type_in_signature (reader->type_str,
988 reader->type_pos + 1);
994 * Gets the current position in the value block
995 * @param reader the reader
998 _dbus_type_reader_get_value_pos (const DBusTypeReader *reader)
1000 return reader->value_pos;
1004 * Get the address of the marshaled value in the data being read. The
1005 * address may not be aligned; you have to align it to the type of the
1006 * value you want to read. Most of the demarshal routines do this for
1009 * @param reader the reader
1010 * @param value_location the address of the marshaled value
1013 _dbus_type_reader_read_raw (const DBusTypeReader *reader,
1014 const unsigned char **value_location)
1016 _dbus_assert (!reader->klass->types_only);
1018 *value_location = _dbus_string_get_const_data_len (reader->value_str,
1024 * Reads a basic-typed value, as with _dbus_marshal_read_basic().
1026 * @param reader the reader
1027 * @param value the address of the value
1030 _dbus_type_reader_read_basic (const DBusTypeReader *reader,
1035 _dbus_assert (!reader->klass->types_only);
1037 t = _dbus_type_reader_get_current_type (reader);
1039 (reader->gvariant ? _dbus_marshal_read_gvariant_basic : _dbus_marshal_read_basic) (
1047 #if RECURSIVE_MARSHAL_READ_TRACE
1048 _dbus_verbose (" type reader %p read basic type_pos = %d value_pos = %d remaining sig '%s'\n",
1049 reader, reader->type_pos, reader->value_pos,
1050 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
1055 * Returns the number of bytes in the array.
1057 * @param reader the reader to read from
1058 * @returns the number of bytes in the array
1061 _dbus_type_reader_get_array_length (const DBusTypeReader *reader)
1063 _dbus_assert (!reader->klass->types_only);
1064 _dbus_assert (reader->klass == &array_reader_class);
1066 return array_reader_get_array_len (reader);
1070 * Reads a block of fixed-length basic values, from the current point
1071 * in an array to the end of the array. Does not work for arrays of
1072 * string or container types.
1074 * This function returns the array in-place; it does not make a copy,
1075 * and it does not swap the bytes.
1077 * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
1078 * and the "value" argument should be a "const double**" and so on.
1080 * @param reader the reader to read from
1081 * @param value place to return the array values
1082 * @param n_elements place to return number of array elements
1085 _dbus_type_reader_read_fixed_multi (const DBusTypeReader *reader,
1095 _dbus_assert (!reader->klass->types_only);
1096 _dbus_assert (reader->klass == &array_reader_class);
1098 element_type = _dbus_first_type_in_signature (reader->type_str,
1101 _dbus_assert (element_type != DBUS_TYPE_INVALID); /* why we don't use get_current_type() */
1102 _dbus_assert (dbus_type_is_fixed (element_type));
1104 alignment = _dbus_type_get_alignment (element_type);
1106 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
1108 total_len = array_reader_get_array_len (reader);
1109 end_pos = reader->u.array.start_pos + total_len;
1110 remaining_len = end_pos - reader->value_pos;
1112 #if RECURSIVE_MARSHAL_READ_TRACE
1113 _dbus_verbose ("end_pos %d total_len %d remaining_len %d value_pos %d\n",
1114 end_pos, total_len, remaining_len, reader->value_pos);
1117 _dbus_assert (remaining_len <= total_len);
1119 if (remaining_len == 0)
1120 *(const DBusBasicValue**) value = NULL;
1122 *(const DBusBasicValue**) value =
1123 (void*) _dbus_string_get_const_data_len (reader->value_str,
1127 *n_elements = remaining_len / alignment;
1128 _dbus_assert ((remaining_len % alignment) == 0);
1130 #if RECURSIVE_MARSHAL_READ_TRACE
1131 _dbus_verbose (" type reader %p read fixed array type_pos = %d value_pos = %d remaining sig '%s'\n",
1132 reader, reader->type_pos, reader->value_pos,
1133 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
1138 * Initialize a new reader pointing to the first type and
1139 * corresponding value that's a child of the current container. It's
1140 * an error to call this if the current type is a non-container.
1142 * Note that DBusTypeReader traverses values, not types. So if you
1143 * have an empty array of array of int, you can't recurse into it. You
1144 * can only recurse into each element.
1146 * @param reader the reader
1147 * @param sub a reader to init pointing to the first child
1150 _dbus_type_reader_recurse (DBusTypeReader *reader,
1151 DBusTypeReader *sub)
1153 const DBusTypeReaderClass *klass = NULL;
1156 t = _dbus_first_type_in_signature (reader->type_str, reader->type_pos);
1160 case DBUS_TYPE_STRUCT:
1161 if (reader->klass->types_only)
1162 klass = &struct_types_only_reader_class;
1164 klass = &struct_reader_class;
1166 case DBUS_TYPE_DICT_ENTRY:
1167 if (reader->klass->types_only)
1168 klass = &dict_entry_types_only_reader_class;
1170 klass = &dict_entry_reader_class;
1172 case DBUS_TYPE_ARRAY:
1173 if (reader->klass->types_only)
1174 klass = &array_types_only_reader_class;
1176 klass = &array_reader_class;
1178 case DBUS_TYPE_VARIANT:
1179 if (reader->klass->types_only)
1180 _dbus_assert_not_reached ("can't recurse into variant typecode");
1182 klass = &variant_reader_class;
1185 _dbus_verbose ("recursing into type %s\n", _dbus_type_to_string (t));
1186 #ifndef DBUS_DISABLE_CHECKS
1187 if (t == DBUS_TYPE_INVALID)
1188 _dbus_warn_check_failed ("You can't recurse into an empty array or off the end of a message body\n");
1189 #endif /* DBUS_DISABLE_CHECKS */
1191 _dbus_assert_not_reached ("don't yet handle recursing into this type");
1194 _dbus_assert (klass == all_reader_classes[klass->id]);
1196 (* klass->recurse) (sub, reader);
1199 #if RECURSIVE_MARSHAL_READ_TRACE
1200 _dbus_verbose (" type reader %p RECURSED type_pos = %d value_pos = %d remaining sig '%s'\n",
1201 sub, sub->type_pos, sub->value_pos,
1202 _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0));
1207 * Skip to the next value on this "level". e.g. the next field in a
1208 * struct, the next value in an array. Returns FALSE at the end of the
1209 * current container.
1211 * @param reader the reader
1212 * @returns FALSE if nothing more to read at or below this level
1215 _dbus_type_reader_next (DBusTypeReader *reader)
1219 t = _dbus_type_reader_get_current_type (reader);
1221 #if RECURSIVE_MARSHAL_READ_TRACE
1222 _dbus_verbose (" type reader %p START next() { type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
1223 reader, reader->type_pos, reader->value_pos,
1224 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1225 _dbus_type_to_string (t));
1228 if (t == DBUS_TYPE_INVALID)
1231 (* reader->klass->next) (reader, t);
1233 #if RECURSIVE_MARSHAL_READ_TRACE
1234 _dbus_verbose (" type reader %p END next() type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
1235 reader, reader->type_pos, reader->value_pos,
1236 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1237 _dbus_type_to_string (_dbus_type_reader_get_current_type (reader)));
1240 return _dbus_type_reader_get_current_type (reader) != DBUS_TYPE_INVALID;
1244 * Check whether there's another value on this "level". e.g. the next
1245 * field in a struct, the next value in an array. Returns FALSE at the
1246 * end of the current container.
1248 * You probably don't want to use this; it makes for an awkward for/while
1249 * loop. A nicer one is "while ((current_type = get_current_type()) != INVALID)"
1251 * @param reader the reader
1252 * @returns FALSE if nothing more to read at or below this level
1255 _dbus_type_reader_has_next (const DBusTypeReader *reader)
1257 /* Not efficient but works for now. */
1258 DBusTypeReader copy;
1261 return _dbus_type_reader_next (©);
1265 * Gets the string and range of said string containing the signature
1266 * of the current value. Essentially a more complete version of
1267 * _dbus_type_reader_get_current_type() (returns the full type
1268 * rather than only the outside of the onion).
1270 * Note though that the first byte in a struct signature is
1271 * #DBUS_STRUCT_BEGIN_CHAR while the current type will be
1272 * #DBUS_TYPE_STRUCT so it isn't true that the first byte of the
1273 * signature is always the same as the current type. Another
1274 * difference is that this function will still return a signature when
1275 * inside an empty array; say you recurse into empty array of int32,
1276 * the signature is "i" but the current type will always be
1277 * #DBUS_TYPE_INVALID since there are no elements to be currently
1280 * @param reader the reader
1281 * @param str_p place to return the string with the type in it
1282 * @param start_p place to return start of the type
1283 * @param len_p place to return the length of the type
1286 _dbus_type_reader_get_signature (const DBusTypeReader *reader,
1287 const DBusString **str_p,
1291 *str_p = reader->type_str;
1292 *start_p = reader->type_pos;
1293 *len_p = find_len_of_complete_type (reader->type_str, reader->type_pos);
1298 DBusString replacement; /**< Marshaled value including alignment padding */
1299 int padding; /**< How much of the replacement block is padding */
1303 replacement_block_init (ReplacementBlock *block,
1304 DBusTypeReader *reader)
1306 if (!_dbus_string_init (&block->replacement))
1309 /* % 8 is the padding to have the same align properties in
1310 * our replacement string as we do at the position being replaced
1312 block->padding = reader->value_pos % 8;
1314 if (!_dbus_string_lengthen (&block->replacement, block->padding))
1320 _dbus_string_free (&block->replacement);
1325 replacement_block_replace (ReplacementBlock *block,
1326 DBusTypeReader *reader,
1327 const DBusTypeReader *realign_root)
1329 DBusTypeWriter writer;
1330 DBusTypeReader realign_reader;
1334 _dbus_assert (realign_root != NULL);
1336 orig_len = _dbus_string_get_length (&block->replacement);
1338 realign_reader = *realign_root;
1340 #if RECURSIVE_MARSHAL_WRITE_TRACE
1341 _dbus_verbose ("INITIALIZING replacement block writer %p at value_pos %d\n",
1342 &writer, _dbus_string_get_length (&block->replacement));
1344 _dbus_type_writer_init_values_only (&writer,
1345 realign_reader.byte_order,
1346 realign_reader.type_str,
1347 realign_reader.type_pos,
1348 &block->replacement,
1349 _dbus_string_get_length (&block->replacement));
1351 _dbus_assert (realign_reader.value_pos <= reader->value_pos);
1353 #if RECURSIVE_MARSHAL_WRITE_TRACE
1354 _dbus_verbose ("COPYING from reader at value_pos %d to writer %p starting after value_pos %d\n",
1355 realign_reader.value_pos, &writer, reader->value_pos);
1358 if (!_dbus_type_writer_write_reader_partial (&writer,
1362 _dbus_string_get_length (&block->replacement) - block->padding,
1366 #if RECURSIVE_MARSHAL_WRITE_TRACE
1367 _dbus_verbose ("REPLACEMENT at padding %d len %d\n", block->padding,
1368 _dbus_string_get_length (&block->replacement) - block->padding);
1369 _dbus_verbose_bytes_of_string (&block->replacement, block->padding,
1370 _dbus_string_get_length (&block->replacement) - block->padding);
1371 _dbus_verbose ("TO BE REPLACED at value_pos = %d (align pad %d) len %d realign_reader.value_pos %d\n",
1372 reader->value_pos, reader->value_pos % 8,
1373 realign_reader.value_pos - reader->value_pos,
1374 realign_reader.value_pos);
1375 _dbus_verbose_bytes_of_string (reader->value_str,
1377 realign_reader.value_pos - reader->value_pos);
1380 /* Move the replacement into position
1381 * (realign_reader should now be at the end of the block to be replaced)
1383 if (!_dbus_string_replace_len (&block->replacement, block->padding,
1384 _dbus_string_get_length (&block->replacement) - block->padding,
1385 (DBusString*) reader->value_str,
1387 realign_reader.value_pos - reader->value_pos))
1390 /* Process our fixups now that we can't have an OOM error */
1391 apply_and_free_fixups (&fixups, reader);
1396 _dbus_string_set_length (&block->replacement, orig_len);
1397 free_fixups (&fixups);
1402 replacement_block_free (ReplacementBlock *block)
1404 _dbus_string_free (&block->replacement);
1407 /* In the variable-length case, we have to fix alignment after we insert.
1408 * The strategy is as follows:
1410 * - pad a new string to have the same alignment as the
1411 * start of the current basic value
1412 * - write the new basic value
1413 * - copy from the original reader to the new string,
1414 * which will fix the alignment of types following
1416 * - this copy has to start at realign_root,
1417 * but not really write anything until it
1418 * passes the value being set
1419 * - as an optimization, we can stop copying
1420 * when the source and dest values are both
1421 * on an 8-boundary, since we know all following
1422 * padding and alignment will be identical
1423 * - copy the new string back to the original
1424 * string, replacing the relevant part of the
1426 * - now any arrays in the original string that
1427 * contained the replaced string may have the
1428 * wrong length; so we have to fix that
1431 reader_set_basic_variable_length (DBusTypeReader *reader,
1434 const DBusTypeReader *realign_root)
1437 ReplacementBlock block;
1438 DBusTypeWriter writer;
1440 _dbus_assert (realign_root != NULL);
1444 if (!replacement_block_init (&block, reader))
1447 /* Write the new basic value */
1448 #if RECURSIVE_MARSHAL_WRITE_TRACE
1449 _dbus_verbose ("INITIALIZING writer %p to write basic value at value_pos %d of replacement string\n",
1450 &writer, _dbus_string_get_length (&block.replacement));
1452 _dbus_type_writer_init_values_only (&writer,
1457 _dbus_string_get_length (&block.replacement));
1458 #if RECURSIVE_MARSHAL_WRITE_TRACE
1459 _dbus_verbose ("WRITING basic value to writer %p (replacement string)\n", &writer);
1461 if (!_dbus_type_writer_write_basic (&writer, current_type, value))
1464 if (!replacement_block_replace (&block,
1472 replacement_block_free (&block);
1477 reader_set_basic_fixed_length (DBusTypeReader *reader,
1481 _dbus_marshal_set_basic ((DBusString*) reader->value_str,
1490 * Sets a new value for the basic type value pointed to by the reader,
1491 * leaving the reader valid to continue reading. Any other readers
1492 * will be invalidated if you set a variable-length type such as a
1495 * The provided realign_root is the reader to start from when
1496 * realigning the data that follows the newly-set value. The reader
1497 * parameter must point to a value below the realign_root parameter.
1498 * If the type being set is fixed-length, then realign_root may be
1499 * #NULL. Only values reachable from realign_root will be realigned,
1500 * so if your string contains other values you will need to deal with
1501 * those somehow yourself. It is OK if realign_root is the same
1502 * reader as the reader parameter, though if you aren't setting the
1503 * root it may not be such a good idea.
1505 * @todo DBusTypeReader currently takes "const" versions of the type
1506 * and value strings, and this function modifies those strings by
1507 * casting away the const, which is of course bad if we want to get
1508 * picky. (To be truly clean you'd have an object which contained the
1509 * type and value strings and set_basic would be a method on that
1510 * object... this would also make DBusTypeReader the same thing as
1511 * DBusTypeMark. But since DBusMessage is effectively that object for
1512 * D-Bus it doesn't seem worth creating some random object.)
1514 * @todo optimize this by only rewriting until the old and new values
1515 * are at the same alignment. Frequently this should result in only
1516 * replacing the value that's immediately at hand.
1518 * @param reader reader indicating where to set a new value
1519 * @param value address of the value to set
1520 * @param realign_root realign from here
1521 * @returns #FALSE if not enough memory
1524 _dbus_type_reader_set_basic (DBusTypeReader *reader,
1526 const DBusTypeReader *realign_root)
1530 _dbus_assert (!reader->klass->types_only);
1531 _dbus_assert (reader->value_str == realign_root->value_str);
1532 _dbus_assert (reader->value_pos >= realign_root->value_pos);
1534 current_type = _dbus_type_reader_get_current_type (reader);
1536 #if RECURSIVE_MARSHAL_WRITE_TRACE
1537 _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",
1538 reader, reader->type_pos, reader->value_pos,
1539 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1541 realign_root ? realign_root->value_pos : -1,
1542 _dbus_type_to_string (current_type));
1543 _dbus_verbose_bytes_of_string (realign_root->value_str, realign_root->value_pos,
1544 _dbus_string_get_length (realign_root->value_str) -
1545 realign_root->value_pos);
1548 _dbus_assert (dbus_type_is_basic (current_type));
1550 if (dbus_type_is_fixed (current_type))
1552 reader_set_basic_fixed_length (reader, current_type, value);
1557 _dbus_assert (realign_root != NULL);
1558 return reader_set_basic_variable_length (reader, current_type,
1559 value, realign_root);
1564 * Recursively deletes any value pointed to by the reader, leaving the
1565 * reader valid to continue reading. Any other readers will be
1568 * The provided realign_root is the reader to start from when
1569 * realigning the data that follows the newly-set value.
1570 * See _dbus_type_reader_set_basic() for more details on the
1571 * realign_root paramter.
1573 * @todo for now this does not delete the typecodes associated with
1574 * the value, so this function should only be used for array elements.
1576 * @param reader reader indicating where to delete a value
1577 * @param realign_root realign from here
1578 * @returns #FALSE if not enough memory
1581 _dbus_type_reader_delete (DBusTypeReader *reader,
1582 const DBusTypeReader *realign_root)
1585 ReplacementBlock block;
1587 _dbus_assert (realign_root != NULL);
1588 _dbus_assert (reader->klass == &array_reader_class);
1592 if (!replacement_block_init (&block, reader))
1595 if (!replacement_block_replace (&block,
1603 replacement_block_free (&block);
1608 * Compares two readers, which must be iterating over the same value data.
1609 * Returns #TRUE if the first parameter is further along than the second parameter.
1611 * @param lhs left-hand-side (first) parameter
1612 * @param rhs left-hand-side (first) parameter
1613 * @returns whether lhs is greater than rhs
1616 _dbus_type_reader_greater_than (const DBusTypeReader *lhs,
1617 const DBusTypeReader *rhs)
1619 _dbus_assert (lhs->value_str == rhs->value_str);
1621 return lhs->value_pos > rhs->value_pos;
1634 * Initialize a write iterator, which is used to write out values in
1635 * serialized D-Bus format.
1637 * The type_pos passed in is expected to be inside an already-valid,
1638 * though potentially empty, type signature. This means that the byte
1639 * after type_pos must be either #DBUS_TYPE_INVALID (aka nul) or some
1640 * other valid type. #DBusTypeWriter won't enforce that the signature
1641 * is already valid (you can append the nul byte at the end if you
1642 * like), but just be aware that you need the nul byte eventually and
1643 * #DBusTypeWriter isn't going to write it for you.
1645 * @param writer the writer to init
1646 * @param byte_order the byte order to marshal into
1647 * @param type_str the string to write typecodes into
1648 * @param type_pos where to insert typecodes
1649 * @param value_str the string to write values into
1650 * @param value_pos where to insert values
1654 _dbus_type_writer_init (DBusTypeWriter *writer,
1656 DBusString *type_str,
1658 DBusString *value_str,
1661 writer->byte_order = byte_order;
1662 writer->type_str = type_str;
1663 writer->type_pos = type_pos;
1664 writer->value_str = value_str;
1665 writer->value_pos = value_pos;
1666 writer->value_start = value_pos;
1667 writer->container_type = DBUS_TYPE_INVALID;
1668 writer->type_pos_is_expectation = FALSE;
1669 writer->enabled = TRUE;
1670 writer->gvariant = FALSE;
1671 writer->body_container = FALSE;
1672 writer->is_fixed = TRUE;
1673 writer->alignment = 1;
1675 #if RECURSIVE_MARSHAL_WRITE_TRACE
1676 _dbus_verbose ("writer %p init remaining sig '%s'\n", writer,
1678 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1684 * Initialize a write iterator, with the signature to be provided
1687 * @param writer the writer to init
1688 * @param byte_order the byte order to marshal into
1689 * @param value_str the string to write values into
1690 * @param value_pos where to insert values
1694 _dbus_type_writer_init_types_delayed (DBusTypeWriter *writer,
1696 DBusString *value_str,
1699 _dbus_type_writer_init (writer, byte_order,
1700 NULL, 0, value_str, value_pos);
1704 * Initialize a write iterator, with the signature to be provided
1705 * later. Supports GVariant
1707 * @param writer the writer to init
1708 * @param byte_order the byte order to marshal into
1709 * @param value_str the string to write values into
1710 * @param value_pos where to insert values
1711 * @param gvariant TRUE if append values with GVariant marshalling
1712 * @param last_offset pointer to root level offset of last variable-size value
1713 * @param last_pos pointer to root level position of offsets
1716 _dbus_type_writer_gvariant_init_types_delayed (DBusTypeWriter *writer,
1718 DBusString *value_str,
1720 dbus_bool_t gvariant,
1721 size_t *last_offset,
1724 _dbus_type_writer_init (writer, byte_order,
1725 NULL, 0, value_str, value_pos);
1726 writer->gvariant = gvariant;
1727 writer->body_container = TRUE;
1728 writer->is_fixed = TRUE;
1729 writer->alignment = 8;
1730 writer->u.root.last_offset = last_offset;
1731 writer->u.root.last_pos = last_pos;
1732 writer->offsets_size = 1;
1733 writer->offsets = NULL;
1737 writer->value_pos = *last_pos;
1738 writer->value_start = 0;
1743 * Adds type string to the writer, if it had none.
1745 * @param writer the writer to init
1746 * @param type_str type string to add
1747 * @param type_pos type position
1751 _dbus_type_writer_add_types (DBusTypeWriter *writer,
1752 DBusString *type_str,
1755 if (writer->type_str == NULL) /* keeps us from using this as setter */
1757 writer->type_str = type_str;
1758 writer->type_pos = type_pos;
1763 * Removes type string from the writer.
1765 * @param writer the writer to remove from
1768 _dbus_type_writer_remove_types (DBusTypeWriter *writer)
1770 writer->type_str = NULL;
1771 writer->type_pos = -1;
1775 * Like _dbus_type_writer_init(), except the type string
1776 * passed in should correspond to an existing signature that
1777 * matches what you're going to write out. The writer will
1778 * check what you write vs. this existing signature.
1780 * @param writer the writer to init
1781 * @param byte_order the byte order to marshal into
1782 * @param type_str the string with signature
1783 * @param type_pos start of signature
1784 * @param value_str the string to write values into
1785 * @param value_pos where to insert values
1789 _dbus_type_writer_init_values_only (DBusTypeWriter *writer,
1791 const DBusString *type_str,
1793 DBusString *value_str,
1796 _dbus_type_writer_init (writer, byte_order,
1797 (DBusString*)type_str, type_pos,
1798 value_str, value_pos);
1800 writer->type_pos_is_expectation = TRUE;
1804 _dbus_type_writer_write_basic_no_typecode (DBusTypeWriter *writer,
1808 if (writer->enabled)
1810 if (writer->gvariant)
1812 return _dbus_type_writer_gvariant_write_basic_no_typecode (writer, type, value);
1815 return _dbus_marshal_write_basic (writer->value_str,
1820 &writer->value_pos);
1826 /* If our parent is an array, things are a little bit complicated.
1828 * The parent must have a complete element type, such as
1829 * "i" or "aai" or "(ii)" or "a(ii)". There can't be
1830 * unclosed parens, or an "a" with no following type.
1832 * To recurse, the only allowed operation is to recurse into the
1833 * first type in the element type. So for "i" you can't recurse, for
1834 * "ai" you can recurse into the array, for "(ii)" you can recurse
1837 * If you recurse into the array for "ai", then you must specify
1838 * "i" for the element type of the array you recurse into.
1840 * While inside an array at any level, we need to avoid writing to
1841 * type_str, since the type only appears once for the whole array,
1842 * it does not appear for each array element.
1844 * While inside an array type_pos points to the expected next
1845 * typecode, rather than the next place we could write a typecode.
1848 writer_recurse_init_and_check (DBusTypeWriter *writer,
1850 DBusTypeWriter *sub)
1852 _dbus_type_writer_init (sub,
1859 sub->container_type = container_type;
1860 sub->gvariant = writer->gvariant;
1862 if (writer->type_pos_is_expectation ||
1863 (sub->container_type == DBUS_TYPE_ARRAY || sub->container_type == DBUS_TYPE_VARIANT))
1864 sub->type_pos_is_expectation = TRUE;
1866 sub->type_pos_is_expectation = FALSE;
1868 sub->enabled = writer->enabled;
1870 #ifndef DBUS_DISABLE_CHECKS
1871 if (writer->type_pos_is_expectation && writer->type_str)
1875 expected = _dbus_first_type_in_signature (writer->type_str, writer->type_pos);
1877 if (expected != sub->container_type)
1879 if (expected != DBUS_TYPE_INVALID)
1880 _dbus_warn_check_failed ("Writing an element of type %s, but the expected type here is %s\n"
1881 "The overall signature expected here was '%s' and we are on byte %d of that signature.\n",
1882 _dbus_type_to_string (sub->container_type),
1883 _dbus_type_to_string (expected),
1884 _dbus_string_get_const_data (writer->type_str), writer->type_pos);
1886 _dbus_warn_check_failed ("Writing an element of type %s, but no value is expected here\n"
1887 "The overall signature expected here was '%s' and we are on byte %d of that signature.\n",
1888 _dbus_type_to_string (sub->container_type),
1889 _dbus_string_get_const_data (writer->type_str), writer->type_pos);
1891 _dbus_assert_not_reached ("bad array element or variant content written");
1894 #endif /* DBUS_DISABLE_CHECKS */
1896 #if RECURSIVE_MARSHAL_WRITE_TRACE
1897 _dbus_verbose (" type writer %p recurse parent %s type_pos = %d value_pos = %d is_expectation = %d remaining sig '%s' enabled = %d\n",
1899 _dbus_type_to_string (writer->container_type),
1900 writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
1902 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1905 _dbus_verbose (" type writer %p recurse sub %s type_pos = %d value_pos = %d is_expectation = %d enabled = %d\n",
1907 _dbus_type_to_string (sub->container_type),
1908 sub->type_pos, sub->value_pos,
1909 sub->type_pos_is_expectation,
1915 write_or_verify_typecode (DBusTypeWriter *writer,
1918 /* A subwriter inside an array or variant will have type_pos
1919 * pointing to the expected typecode; a writer not inside an array
1920 * or variant has type_pos pointing to the next place to insert a
1923 #if RECURSIVE_MARSHAL_WRITE_TRACE
1924 _dbus_verbose (" type writer %p write_or_verify start type_pos = %d remaining sig '%s' enabled = %d\n",
1925 writer, writer->type_pos,
1927 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1932 if (writer->type_str == NULL)
1935 if (writer->type_pos_is_expectation)
1937 #ifndef DBUS_DISABLE_CHECKS
1941 expected = _dbus_string_get_byte (writer->type_str, writer->type_pos);
1943 if (expected != typecode)
1945 if (expected != DBUS_TYPE_INVALID)
1946 _dbus_warn_check_failed ("Array or variant type requires that type %s be written, but %s was written.\n"
1947 "The overall signature expected here was '%s' and we are on byte %d of that signature.\n",
1948 _dbus_type_to_string (expected), _dbus_type_to_string (typecode),
1949 _dbus_string_get_const_data (writer->type_str), writer->type_pos);
1951 _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"
1952 "The overall signature expected here was '%s' and we are on byte %d of that signature.\n",
1953 _dbus_type_to_string (typecode),
1954 _dbus_string_get_const_data (writer->type_str), writer->type_pos);
1955 _dbus_assert_not_reached ("bad type inserted somewhere inside an array or variant");
1958 #endif /* DBUS_DISABLE_CHECKS */
1960 /* if immediately inside an array we'd always be appending an element,
1961 * so the expected type doesn't change; if inside a struct or something
1962 * below an array, we need to move through said struct or something.
1964 if (writer->container_type != DBUS_TYPE_ARRAY)
1965 writer->type_pos += 1;
1969 if (!_dbus_string_insert_byte (writer->type_str,
1974 writer->type_pos += 1;
1977 #if RECURSIVE_MARSHAL_WRITE_TRACE
1978 _dbus_verbose (" type writer %p write_or_verify end type_pos = %d remaining sig '%s'\n",
1979 writer, writer->type_pos,
1980 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0));
1987 writer_recurse_struct_or_dict_entry (DBusTypeWriter *writer,
1989 const DBusString *contained_type,
1990 int contained_type_start,
1991 int contained_type_len,
1992 DBusTypeWriter *sub)
1994 /* FIXME right now contained_type is ignored; we could probably
1995 * almost trivially fix the code so if it's present we
1996 * write it out and then set type_pos_is_expectation
1999 /* Ensure that we'll be able to add alignment padding and the typecode */
2000 if (writer->enabled)
2002 if (!_dbus_string_alloc_space (sub->value_str, 8))
2006 if (!write_or_verify_typecode (sub, begin_char))
2007 _dbus_assert_not_reached ("failed to insert struct typecode after prealloc");
2009 if (writer->enabled)
2011 if (writer->gvariant)
2014 sub->value_str = dbus_new (DBusString, 1);
2015 if (NULL == sub->value_str || !_dbus_string_init (sub->value_str))
2017 sub->value_start = sub->value_pos = 0;
2018 sub->u.struct_or_dict.last_offset = GVARIANT_LAST_OFFSET_NOT_SET;
2019 sub->offsets_size = 1;
2020 sub->is_fixed = TRUE;
2021 sub->offsets = dbus_new (DBusString, 1);
2022 _dbus_string_init (sub->offsets);
2026 if (!_dbus_string_insert_bytes (sub->value_str,
2028 _DBUS_ALIGN_VALUE (sub->value_pos, 8) - sub->value_pos,
2030 _dbus_assert_not_reached ("should not have failed to insert alignment padding for struct");
2032 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
2041 writer_recurse_array (DBusTypeWriter *writer,
2042 const DBusString *contained_type,
2043 int contained_type_start,
2044 int contained_type_len,
2045 DBusTypeWriter *sub,
2046 dbus_bool_t is_array_append)
2048 dbus_uint32_t value = 0;
2052 #ifndef DBUS_DISABLE_CHECKS
2053 if (writer->container_type == DBUS_TYPE_ARRAY &&
2056 if (!_dbus_string_equal_substring (contained_type,
2057 contained_type_start,
2060 writer->u.array.element_type_pos + 1))
2062 _dbus_warn_check_failed ("Writing an array of '%s' but this is incompatible with the expected type of elements in the parent array\n",
2063 _dbus_string_get_const_data_len (contained_type,
2064 contained_type_start,
2065 contained_type_len));
2066 _dbus_assert_not_reached ("incompatible type for child array");
2069 #endif /* DBUS_DISABLE_CHECKS */
2071 if (writer->enabled && !is_array_append)
2073 /* 3 pad + 4 bytes for the array length, and 4 bytes possible padding
2074 * before array values
2076 if (!_dbus_string_alloc_space (sub->value_str, 3 + 4 + 4))
2080 if (writer->type_str != NULL)
2082 sub->type_pos += 1; /* move to point to the element type, since type_pos
2083 * should be the expected type for further writes
2085 sub->u.array.element_type_pos = sub->type_pos;
2088 if (!writer->type_pos_is_expectation)
2090 /* sub is a toplevel/outermost array so we need to write the type data */
2092 /* alloc space for array typecode, element signature */
2093 if (!_dbus_string_alloc_space (writer->type_str, 1 + contained_type_len))
2096 if (!_dbus_string_insert_byte (writer->type_str,
2099 _dbus_assert_not_reached ("failed to insert array typecode after prealloc");
2101 if (!_dbus_string_copy_len (contained_type,
2102 contained_type_start, contained_type_len,
2104 sub->u.array.element_type_pos))
2105 _dbus_assert_not_reached ("should not have failed to insert array element typecodes");
2108 if (writer->type_str != NULL)
2110 /* If the parent is an array, we hold type_pos pointing at the array element type;
2111 * otherwise advance it to reflect the array value we just recursed into
2113 if (writer->container_type != DBUS_TYPE_ARRAY)
2114 writer->type_pos += 1 + contained_type_len;
2116 _dbus_assert (writer->type_pos_is_expectation); /* because it's an array */
2119 if (writer->enabled)
2121 if (!writer->gvariant)
2123 /* Write (or jump over, if is_array_append) the length */
2124 sub->u.array.len_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
2126 if (is_array_append)
2128 sub->value_pos += 4;
2132 if (!_dbus_type_writer_write_basic_no_typecode (sub, DBUS_TYPE_UINT32, &value))
2133 _dbus_assert_not_reached ("should not have failed to insert array len");
2136 _dbus_assert (sub->u.array.len_pos == sub->value_pos - 4);
2139 /* Write alignment padding for array elements
2140 * Note that we write the padding *even for empty arrays*
2141 * to avoid wonky special cases
2143 if (writer->gvariant)
2145 int size = _dbus_type_gvariant_get_fixed_size (contained_type, contained_type_start, &alignment);
2148 sub->offsets_size = 1;
2149 sub->offsets = dbus_new (DBusString, 1);
2150 _dbus_string_init (sub->offsets);
2154 sub->offsets_size = 0;
2155 sub->offsets = NULL;
2160 alignment = element_type_get_alignment (contained_type, contained_type_start);
2163 aligned = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
2164 if (aligned != sub->value_pos)
2166 if (!is_array_append)
2168 if (!_dbus_string_insert_bytes (sub->value_str,
2170 aligned - sub->value_pos,
2172 _dbus_assert_not_reached ("should not have failed to insert alignment padding");
2175 sub->value_pos = aligned;
2178 sub->u.array.start_pos = sub->value_pos;
2180 if (is_array_append && !writer->gvariant)
2184 _dbus_assert (_DBUS_ALIGN_VALUE (sub->u.array.len_pos, 4) ==
2185 (unsigned) sub->u.array.len_pos);
2186 len = _dbus_unpack_uint32 (sub->byte_order,
2187 _dbus_string_get_const_data_len (sub->value_str,
2188 sub->u.array.len_pos,
2191 sub->value_pos += len;
2193 if (writer->gvariant)
2195 sub->alignment = alignment;
2196 sub->is_fixed = FALSE;
2197 sub->value_start = sub->value_pos;
2202 /* not enabled, so we won't write the len_pos; set it to -1 to so indicate */
2203 sub->u.array.len_pos = -1;
2204 sub->u.array.start_pos = sub->value_pos;
2207 _dbus_assert (sub->gvariant || sub->u.array.len_pos < sub->u.array.start_pos);
2208 _dbus_assert (is_array_append || sub->u.array.start_pos == sub->value_pos);
2210 #if RECURSIVE_MARSHAL_WRITE_TRACE
2211 _dbus_verbose (" type writer %p recurse array done remaining sig '%s' array start_pos = %d len_pos = %d value_pos = %d\n", sub,
2213 _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0) :
2215 sub->u.array.start_pos, sub->u.array.len_pos, sub->value_pos);
2221 /* Variant value will normally have:
2222 * 1 byte signature length not including nul
2223 * signature typecodes (nul terminated)
2224 * padding to alignment of contained type
2225 * body according to signature
2227 * The signature string can only have a single type
2228 * in it but that type may be complex/recursive.
2230 * So a typical variant type with the integer 3 will have these
2232 * 0x1 'i' '\0' [1 byte padding to alignment boundary] 0x0 0x0 0x0 0x3
2234 * The main world of hurt for writing out a variant is that the type
2235 * string is the same string as the value string. Which means
2236 * inserting to the type string will move the value_pos; and it means
2237 * that inserting to the type string could break type alignment.
2240 writer_recurse_variant (DBusTypeWriter *writer,
2241 const DBusString *contained_type,
2242 int contained_type_start,
2243 int contained_type_len,
2244 DBusTypeWriter *sub)
2246 int contained_alignment;
2248 if (writer->enabled)
2250 /* Allocate space for the worst case, which is 1 byte sig
2251 * length, nul byte at end of sig, and 7 bytes padding to
2254 if (!_dbus_string_alloc_space (sub->value_str, contained_type_len + 9))
2258 /* write VARIANT typecode to the parent's type string */
2259 if (!write_or_verify_typecode (writer, DBUS_TYPE_VARIANT))
2262 /* If not enabled, mark that we have no type_str anymore ... */
2264 if (!writer->enabled)
2266 sub->type_str = NULL;
2272 /* If we're enabled then continue ... */
2274 if (writer->gvariant)
2277 * contents, then nul byte, then signature without nul byte.
2278 * The alignment is always 8.
2280 * Signature is at the end of a variant. So, the easiest way is to write it down
2281 * when unrecursing. So, we need to copy it to a new string.
2283 contained_alignment = 8;
2285 sub->type_str = dbus_new (DBusString, 1); /* to be deallocated on unrecurse */
2287 sub->is_fixed = FALSE;
2288 _dbus_string_init_preallocated (sub->type_str, contained_type_len);
2290 if (!_dbus_string_copy_len (contained_type, contained_type_start, contained_type_len,
2291 sub->type_str, sub->type_pos))
2292 _dbus_assert_not_reached ("should not have failed to insert variant type sig");
2297 * length, signature with nul byte, then contents
2298 * alignment depends on contents.
2300 if (!_dbus_string_insert_byte (sub->value_str,
2302 contained_type_len))
2303 _dbus_assert_not_reached ("should not have failed to insert variant type sig len");
2305 sub->value_pos += 1;
2307 /* Here we switch over to the expected type sig we're about to write */
2308 sub->type_str = sub->value_str;
2309 sub->type_pos = sub->value_pos;
2311 if (!_dbus_string_copy_len (contained_type, contained_type_start, contained_type_len,
2312 sub->value_str, sub->value_pos))
2313 _dbus_assert_not_reached ("should not have failed to insert variant type sig");
2315 sub->value_pos += contained_type_len;
2317 if (!_dbus_string_insert_byte (sub->value_str,
2320 _dbus_assert_not_reached ("should not have failed to insert variant type nul termination");
2322 sub->value_pos += 1;
2324 contained_alignment = _dbus_type_get_alignment (_dbus_first_type_in_signature (contained_type, contained_type_start));
2327 if (!_dbus_string_insert_bytes (sub->value_str,
2329 _DBUS_ALIGN_VALUE (sub->value_pos, contained_alignment) - sub->value_pos,
2331 _dbus_assert_not_reached ("should not have failed to insert alignment padding for variant body");
2332 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, contained_alignment);
2333 sub->value_start = sub->value_pos;
2339 _dbus_type_writer_recurse_contained_len (DBusTypeWriter *writer,
2341 const DBusString *contained_type,
2342 int contained_type_start,
2343 int contained_type_len,
2344 DBusTypeWriter *sub,
2345 dbus_bool_t is_array_append)
2347 writer_recurse_init_and_check (writer, container_type, sub);
2349 switch (container_type)
2351 case DBUS_TYPE_STRUCT:
2352 return writer_recurse_struct_or_dict_entry (writer,
2353 DBUS_STRUCT_BEGIN_CHAR,
2355 contained_type_start, contained_type_len,
2358 case DBUS_TYPE_DICT_ENTRY:
2359 return writer_recurse_struct_or_dict_entry (writer,
2360 DBUS_DICT_ENTRY_BEGIN_CHAR,
2362 contained_type_start, contained_type_len,
2365 case DBUS_TYPE_ARRAY:
2366 return writer_recurse_array (writer,
2367 contained_type, contained_type_start, contained_type_len,
2368 sub, is_array_append);
2370 case DBUS_TYPE_VARIANT:
2371 return writer_recurse_variant (writer,
2372 contained_type, contained_type_start, contained_type_len,
2376 _dbus_assert_not_reached ("tried to recurse into type that doesn't support that");
2383 * Opens a new container and writes out the initial information for that container.
2385 * @param writer the writer
2386 * @param container_type the type of the container to open
2387 * @param contained_type the array element type or variant content type
2388 * @param contained_type_start position to look for the type
2389 * @param sub the new sub-writer to write container contents
2390 * @returns #FALSE if no memory
2393 _dbus_type_writer_recurse (DBusTypeWriter *writer,
2395 const DBusString *contained_type,
2396 int contained_type_start,
2397 DBusTypeWriter *sub)
2399 int contained_type_len;
2402 contained_type_len = find_len_of_complete_type (contained_type, contained_type_start);
2404 contained_type_len = 0;
2406 sub->body_container = FALSE;
2408 return _dbus_type_writer_recurse_contained_len (writer, container_type,
2410 contained_type_start,
2417 * Append to an existing array. Essentially, the writer will read an
2418 * existing length at the write location; jump over that length; and
2419 * write new fields. On unrecurse(), the existing length will be
2422 * @param writer the writer
2423 * @param contained_type element type
2424 * @param contained_type_start position of element type
2425 * @param sub the subwriter to init
2426 * @returns #FALSE if no memory
2429 _dbus_type_writer_append_array (DBusTypeWriter *writer,
2430 const DBusString *contained_type,
2431 int contained_type_start,
2432 DBusTypeWriter *sub)
2434 int contained_type_len;
2437 contained_type_len = find_len_of_complete_type (contained_type, contained_type_start);
2439 contained_type_len = 0;
2441 return _dbus_type_writer_recurse_contained_len (writer, DBUS_TYPE_ARRAY,
2443 contained_type_start,
2450 writer_get_array_len (DBusTypeWriter *writer)
2452 _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY);
2453 return writer->value_pos - writer->u.array.start_pos;
2457 _dbus_type_writer_unrecurse_write (DBusTypeWriter *writer,
2458 DBusTypeWriter *sub)
2460 if (sub->container_type == DBUS_TYPE_STRUCT)
2462 if (!write_or_verify_typecode (sub, DBUS_STRUCT_END_CHAR))
2465 else if (sub->container_type == DBUS_TYPE_DICT_ENTRY)
2467 if (!write_or_verify_typecode (sub, DBUS_DICT_ENTRY_END_CHAR))
2470 else if (sub->container_type == DBUS_TYPE_ARRAY && !sub->gvariant)
2472 if (sub->u.array.len_pos >= 0) /* len_pos == -1 if we weren't enabled when we passed it */
2476 /* Set the array length */
2477 len = writer_get_array_len (sub);
2478 _dbus_marshal_set_uint32 (sub->value_str,
2479 sub->u.array.len_pos,
2482 #if RECURSIVE_MARSHAL_WRITE_TRACE
2483 _dbus_verbose (" filled in sub array len to %u at len_pos %d\n",
2484 len, sub->u.array.len_pos);
2487 #if RECURSIVE_MARSHAL_WRITE_TRACE
2490 _dbus_verbose (" not filling in sub array len because we were disabled when we passed the len\n");
2498 * Closes a container created by _dbus_type_writer_recurse()
2499 * and writes any additional information to the values block.
2501 * @param writer the writer
2502 * @param sub the sub-writer created by _dbus_type_writer_recurse()
2503 * @returns #FALSE if no memory
2506 _dbus_type_writer_unrecurse (DBusTypeWriter *writer,
2507 DBusTypeWriter *sub)
2509 /* type_pos_is_expectation never gets unset once set, or we'd get all hosed */
2510 _dbus_assert (!writer->type_pos_is_expectation ||
2511 (writer->type_pos_is_expectation && sub->type_pos_is_expectation));
2513 #if RECURSIVE_MARSHAL_WRITE_TRACE
2514 _dbus_verbose (" type writer %p unrecurse type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
2515 writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
2516 _dbus_type_to_string (writer->container_type));
2517 _dbus_verbose (" type writer %p unrecurse sub type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
2518 sub, sub->type_pos, sub->value_pos,
2519 sub->type_pos_is_expectation,
2520 _dbus_type_to_string (sub->container_type));
2523 if (!_dbus_type_writer_unrecurse_write (writer, sub))
2526 if (writer->gvariant)
2528 if (!_dbus_writer_unrecurse_gvariant_write (writer, sub))
2532 writer->value_pos = sub->value_pos;
2535 /* Now get type_pos right for the parent writer. Here are the cases:
2537 * Cases !writer->type_pos_is_expectation:
2538 * (in these cases we want to update to the new insertion point)
2540 * - if we recursed into a STRUCT then we didn't know in advance
2541 * what the types in the struct would be; so we have to fill in
2542 * that information now.
2543 * writer->type_pos = sub->type_pos
2545 * - if we recursed into anything else, we knew the full array
2546 * type, or knew the single typecode marking VARIANT, so
2547 * writer->type_pos is already correct.
2548 * writer->type_pos should remain as-is
2550 * - note that the parent is never an ARRAY or VARIANT, if it were
2551 * then type_pos_is_expectation would be TRUE. The parent
2552 * is thus known to be a toplevel or STRUCT.
2554 * Cases where writer->type_pos_is_expectation:
2555 * (in these cases we want to update to next expected type to write)
2557 * - we recursed from STRUCT into STRUCT and we didn't increment
2558 * type_pos in the parent just to stay consistent with the
2559 * !writer->type_pos_is_expectation case (though we could
2560 * special-case this in recurse_struct instead if we wanted)
2561 * writer->type_pos = sub->type_pos
2563 * - we recursed from STRUCT into ARRAY or VARIANT and type_pos
2564 * for parent should have been incremented already
2565 * writer->type_pos should remain as-is
2567 * - we recursed from ARRAY into a sub-element, so type_pos in the
2568 * parent is the element type and should remain the element type
2569 * for the benefit of the next child element
2570 * writer->type_pos should remain as-is
2572 * - we recursed from VARIANT into its value, so type_pos in the
2573 * parent makes no difference since there's only one value
2574 * and we just finished writing it and won't use type_pos again
2575 * writer->type_pos should remain as-is
2578 * For all these, DICT_ENTRY is the same as STRUCT
2580 if (writer->type_str != NULL)
2582 if ((sub->container_type == DBUS_TYPE_STRUCT ||
2583 sub->container_type == DBUS_TYPE_DICT_ENTRY) &&
2584 (writer->container_type == DBUS_TYPE_STRUCT ||
2585 writer->container_type == DBUS_TYPE_DICT_ENTRY ||
2586 writer->container_type == DBUS_TYPE_INVALID))
2588 /* Advance the parent to the next struct field */
2589 writer->type_pos = sub->type_pos;
2593 #if RECURSIVE_MARSHAL_WRITE_TRACE
2594 _dbus_verbose (" type writer %p unrecursed type_pos = %d value_pos = %d remaining sig '%s'\n",
2595 writer, writer->type_pos, writer->value_pos,
2597 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
2605 * Writes out a basic type.
2607 * @param writer the writer
2608 * @param type the type to write
2609 * @param value the address of the value to write
2610 * @returns #FALSE if no memory
2613 _dbus_type_writer_write_basic (DBusTypeWriter *writer,
2619 /* First ensure that our type realloc will succeed */
2620 if (!writer->type_pos_is_expectation && writer->type_str != NULL)
2622 if (!_dbus_string_alloc_space (writer->type_str, 1))
2628 if (!_dbus_type_writer_write_basic_no_typecode (writer, type, value))
2631 if (!write_or_verify_typecode (writer, type))
2632 _dbus_assert_not_reached ("failed to write typecode after prealloc");
2637 #if RECURSIVE_MARSHAL_WRITE_TRACE
2638 _dbus_verbose (" type writer %p basic type_pos = %d value_pos = %d is_expectation = %d enabled = %d\n",
2639 writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
2647 * Writes a block of fixed-length basic values, i.e. those that are
2648 * both dbus_type_is_fixed() and _dbus_type_is_basic(). The block
2649 * must be written inside an array.
2651 * The value parameter should be the address of said array of values,
2652 * so e.g. if it's an array of double, pass in "const double**"
2654 * @param writer the writer
2655 * @param element_type type of stuff in the array
2656 * @param value address of the array
2657 * @param n_elements number of elements in the array
2658 * @returns #FALSE if no memory
2661 _dbus_type_writer_write_fixed_multi (DBusTypeWriter *writer,
2666 _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY);
2667 _dbus_assert (dbus_type_is_fixed (element_type));
2668 _dbus_assert (writer->type_pos_is_expectation);
2669 _dbus_assert (n_elements >= 0);
2671 #if RECURSIVE_MARSHAL_WRITE_TRACE
2672 _dbus_verbose (" type writer %p entering fixed multi type_pos = %d value_pos = %d n_elements %d\n",
2673 writer, writer->type_pos, writer->value_pos, n_elements);
2676 if (!write_or_verify_typecode (writer, element_type))
2677 _dbus_assert_not_reached ("OOM should not happen if only verifying typecode");
2679 if (writer->enabled)
2681 if (!_dbus_marshal_write_fixed_multi (writer->value_str,
2687 &writer->value_pos))
2691 #if RECURSIVE_MARSHAL_WRITE_TRACE
2692 _dbus_verbose (" type writer %p fixed multi written new type_pos = %d new value_pos = %d n_elements %d\n",
2693 writer, writer->type_pos, writer->value_pos, n_elements);
2700 enable_if_after (DBusTypeWriter *writer,
2701 DBusTypeReader *reader,
2702 const DBusTypeReader *start_after)
2706 if (!writer->enabled && _dbus_type_reader_greater_than (reader, start_after))
2708 _dbus_type_writer_set_enabled (writer, TRUE);
2709 #if RECURSIVE_MARSHAL_WRITE_TRACE
2710 _dbus_verbose ("ENABLING writer %p at %d because reader at value_pos %d is after reader at value_pos %d\n",
2711 writer, writer->value_pos, reader->value_pos, start_after->value_pos);
2715 _dbus_assert ((!writer->enabled && !_dbus_type_reader_greater_than (reader, start_after)) ||
2716 (writer->enabled && _dbus_type_reader_greater_than (reader, start_after)));
2721 append_fixup (DBusList **fixups,
2722 const DBusArrayLenFixup *fixup)
2724 DBusArrayLenFixup *f;
2726 f = dbus_new (DBusArrayLenFixup, 1);
2732 if (!_dbus_list_append (fixups, f))
2738 _dbus_assert (f->len_pos_in_reader == fixup->len_pos_in_reader);
2739 _dbus_assert (f->new_len == fixup->new_len);
2744 /* This loop is trivial if you ignore all the start_after nonsense,
2745 * so if you're trying to figure it out, start by ignoring that
2748 writer_write_reader_helper (DBusTypeWriter *writer,
2749 DBusTypeReader *reader,
2750 const DBusTypeReader *start_after,
2751 int start_after_new_pos,
2752 int start_after_new_len,
2754 dbus_bool_t inside_start_after)
2758 while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
2760 if (dbus_type_is_container (current_type))
2762 DBusTypeReader subreader;
2763 DBusTypeWriter subwriter;
2764 const DBusString *sig_str;
2767 dbus_bool_t enabled_at_recurse;
2768 dbus_bool_t past_start_after;
2769 int reader_array_len_pos;
2770 int reader_array_start_pos;
2771 dbus_bool_t this_is_start_after;
2773 /* type_pos is checked since e.g. in a struct the struct
2774 * and its first field have the same value_pos.
2775 * type_str will differ in reader/start_after for variants
2776 * where type_str is inside the value_str
2778 if (!inside_start_after && start_after &&
2779 reader->value_pos == start_after->value_pos &&
2780 reader->type_str == start_after->type_str &&
2781 reader->type_pos == start_after->type_pos)
2782 this_is_start_after = TRUE;
2784 this_is_start_after = FALSE;
2786 _dbus_type_reader_recurse (reader, &subreader);
2788 if (current_type == DBUS_TYPE_ARRAY)
2790 reader_array_len_pos = ARRAY_READER_LEN_POS (&subreader);
2791 reader_array_start_pos = subreader.u.array.start_pos;
2796 reader_array_len_pos = -1;
2797 reader_array_start_pos = -1;
2800 _dbus_type_reader_get_signature (&subreader, &sig_str,
2801 &sig_start, &sig_len);
2803 #if RECURSIVE_MARSHAL_WRITE_TRACE
2804 _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",
2805 _dbus_type_to_string (current_type),
2807 subreader.value_pos,
2809 start_after ? start_after->value_pos : -1,
2810 _dbus_string_get_length (writer->value_str),
2811 inside_start_after, this_is_start_after);
2814 if (!inside_start_after && !this_is_start_after)
2815 enable_if_after (writer, &subreader, start_after);
2816 enabled_at_recurse = writer->enabled;
2817 if (!_dbus_type_writer_recurse_contained_len (writer, current_type,
2818 sig_str, sig_start, sig_len,
2822 #if RECURSIVE_MARSHAL_WRITE_TRACE
2823 _dbus_verbose ("recursed into subwriter at %d write target len %d\n",
2824 subwriter.value_pos,
2825 _dbus_string_get_length (subwriter.value_str));
2828 if (!writer_write_reader_helper (&subwriter, &subreader, start_after,
2829 start_after_new_pos, start_after_new_len,
2831 inside_start_after ||
2832 this_is_start_after))
2835 #if RECURSIVE_MARSHAL_WRITE_TRACE
2836 _dbus_verbose ("about to unrecurse from %s subreader at %d writer at %d subwriter at %d write target len %d\n",
2837 _dbus_type_to_string (current_type),
2838 subreader.value_pos,
2840 subwriter.value_pos,
2841 _dbus_string_get_length (writer->value_str));
2844 if (!inside_start_after && !this_is_start_after)
2845 enable_if_after (writer, &subreader, start_after);
2846 past_start_after = writer->enabled;
2847 if (!_dbus_type_writer_unrecurse (writer, &subwriter))
2850 /* If we weren't enabled when we recursed, we didn't
2851 * write an array len; if we passed start_after
2852 * somewhere inside the array, then we need to generate
2855 if (start_after != NULL &&
2856 !enabled_at_recurse && past_start_after &&
2857 current_type == DBUS_TYPE_ARRAY &&
2860 DBusArrayLenFixup fixup;
2861 int bytes_written_after_start_after;
2862 int bytes_before_start_after;
2865 /* this subwriter access is moderately unkosher since we
2866 * already unrecursed, but it works as long as unrecurse
2867 * doesn't break us on purpose
2869 bytes_written_after_start_after = writer_get_array_len (&subwriter);
2871 bytes_before_start_after =
2872 start_after->value_pos - reader_array_start_pos;
2874 fixup.len_pos_in_reader = reader_array_len_pos;
2876 bytes_before_start_after +
2877 start_after_new_len +
2878 bytes_written_after_start_after;
2880 _dbus_assert (_DBUS_ALIGN_VALUE (fixup.len_pos_in_reader, 4) ==
2881 (unsigned) fixup.len_pos_in_reader);
2883 old_len = _dbus_unpack_uint32 (reader->byte_order,
2884 _dbus_string_get_const_data_len (reader->value_str,
2885 fixup.len_pos_in_reader, 4));
2887 if (old_len != fixup.new_len && !append_fixup (fixups, &fixup))
2890 #if RECURSIVE_MARSHAL_WRITE_TRACE
2891 _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",
2892 fixup.len_pos_in_reader,
2894 reader_array_start_pos,
2895 start_after->value_pos,
2896 bytes_before_start_after,
2897 start_after_new_len,
2898 bytes_written_after_start_after);
2906 _dbus_assert (dbus_type_is_basic (current_type));
2908 #if RECURSIVE_MARSHAL_WRITE_TRACE
2909 _dbus_verbose ("Reading basic value %s at %d\n",
2910 _dbus_type_to_string (current_type),
2914 _dbus_type_reader_read_basic (reader, &val);
2916 #if RECURSIVE_MARSHAL_WRITE_TRACE
2917 _dbus_verbose ("Writing basic value %s at %d write target len %d inside_start_after = %d\n",
2918 _dbus_type_to_string (current_type),
2920 _dbus_string_get_length (writer->value_str),
2921 inside_start_after);
2923 if (!inside_start_after)
2924 enable_if_after (writer, reader, start_after);
2925 if (!_dbus_type_writer_write_basic (writer, current_type, &val))
2927 #if RECURSIVE_MARSHAL_WRITE_TRACE
2928 _dbus_verbose ("Wrote basic value %s, new value_pos %d write target len %d\n",
2929 _dbus_type_to_string (current_type),
2931 _dbus_string_get_length (writer->value_str));
2935 _dbus_type_reader_next (reader);
2942 apply_and_free_fixups (fixups, NULL); /* NULL for reader to apply to */
2948 * Iterate through all values in the given reader, writing a copy of
2949 * each value to the writer. The reader will be moved forward to its
2952 * If a reader start_after is provided, it should be a reader for the
2953 * same data as the reader to be written. Only values occurring after
2954 * the value pointed to by start_after will be written to the writer.
2956 * If start_after is provided, then the copy of the reader will be
2957 * partial. This means that array lengths will not have been copied.
2958 * The assumption is that you wrote a new version of the value at
2959 * start_after to the writer. You have to pass in the start position
2960 * and length of the new value. (If you are deleting the value
2961 * at start_after, pass in 0 for the length.)
2963 * If the fixups parameter is non-#NULL, then any array length that
2964 * was read but not written due to start_after will be provided
2965 * as a #DBusArrayLenFixup. The fixup contains the position of the
2966 * array length in the source data, and the correct array length
2967 * assuming you combine the source data before start_after with
2968 * the written data at start_after and beyond.
2970 * @param writer the writer to copy to
2971 * @param reader the reader to copy from
2972 * @param start_after #NULL or a reader showing where to start
2973 * @param start_after_new_pos the position of start_after equivalent in the target data
2974 * @param start_after_new_len the length of start_after equivalent in the target data
2975 * @param fixups list to append #DBusArrayLenFixup if the write was partial
2976 * @returns #FALSE if no memory
2979 _dbus_type_writer_write_reader_partial (DBusTypeWriter *writer,
2980 DBusTypeReader *reader,
2981 const DBusTypeReader *start_after,
2982 int start_after_new_pos,
2983 int start_after_new_len,
2986 DBusTypeWriter orig;
2993 orig_type_len = _dbus_string_get_length (writer->type_str);
2994 orig_value_len = _dbus_string_get_length (writer->value_str);
2995 orig_enabled = writer->enabled;
2998 _dbus_type_writer_set_enabled (writer, FALSE);
3000 if (!writer_write_reader_helper (writer, reader, start_after,
3001 start_after_new_pos,
3002 start_after_new_len,
3006 _dbus_type_writer_set_enabled (writer, orig_enabled);
3010 if (!writer->type_pos_is_expectation)
3012 new_bytes = _dbus_string_get_length (writer->type_str) - orig_type_len;
3013 _dbus_string_delete (writer->type_str, orig.type_pos, new_bytes);
3015 new_bytes = _dbus_string_get_length (writer->value_str) - orig_value_len;
3016 _dbus_string_delete (writer->value_str, orig.value_pos, new_bytes);
3024 * Iterate through all values in the given reader, writing a copy of
3025 * each value to the writer. The reader will be moved forward to its
3028 * @param writer the writer to copy to
3029 * @param reader the reader to copy from
3030 * @returns #FALSE if no memory
3033 _dbus_type_writer_write_reader (DBusTypeWriter *writer,
3034 DBusTypeReader *reader)
3036 return _dbus_type_writer_write_reader_partial (writer, reader, NULL, 0, 0, NULL);
3040 * If disabled, a writer can still be iterated forward and recursed/unrecursed
3041 * but won't write any values. Types will still be written unless the
3042 * writer is a "values only" writer, because the writer needs access to
3043 * a valid signature to be able to iterate.
3045 * @param writer the type writer
3046 * @param enabled #TRUE if values should be written
3049 _dbus_type_writer_set_enabled (DBusTypeWriter *writer,
3050 dbus_bool_t enabled)
3052 writer->enabled = enabled != FALSE;
3055 /** @} */ /* end of DBusMarshal group */
3057 /* tests in dbus-marshal-recursive-util.c */