1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal-recursive.c Marshalling routines for recursive types
4 * Copyright (C) 2004, 2005 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-marshal-recursive.h"
25 #include "dbus-marshal-basic.h"
26 #include "dbus-internals.h"
29 * @addtogroup DBusMarshal
32 #define RECURSIVE_MARSHAL_READ_TRACE 0
33 #define RECURSIVE_MARSHAL_WRITE_TRACE 0
36 free_fixups (DBusList **fixups)
40 link = _dbus_list_get_first_link (fixups);
45 next = _dbus_list_get_next_link (fixups, link);
47 dbus_free (link->data);
48 _dbus_list_free_link (link);
57 apply_and_free_fixups (DBusList **fixups,
58 DBusTypeReader *reader)
62 #if RECURSIVE_MARSHAL_WRITE_TRACE
64 _dbus_verbose (" %d FIXUPS to apply\n",
65 _dbus_list_get_length (fixups));
68 link = _dbus_list_get_first_link (fixups);
73 next = _dbus_list_get_next_link (fixups, link);
81 #if RECURSIVE_MARSHAL_WRITE_TRACE
82 _dbus_verbose (" applying FIXUP to reader %p at pos %d new_len = %d old len %d\n",
83 reader, f->len_pos_in_reader, f->new_len,
84 _dbus_marshal_read_uint32 (reader->value_str,
86 reader->byte_order, NULL));
89 _dbus_marshal_set_uint32 ((DBusString*) reader->value_str,
95 dbus_free (link->data);
96 _dbus_list_free_link (link);
104 struct DBusTypeReaderClass
107 int id; /* index in all_reader_classes */
108 dbus_bool_t types_only; /* only iterates over types, not values */
109 void (* recurse) (DBusTypeReader *sub,
110 DBusTypeReader *parent);
111 dbus_bool_t (* check_finished) (const DBusTypeReader *reader);
112 void (* next) (DBusTypeReader *reader,
114 void (* init_from_mark) (DBusTypeReader *reader,
115 const DBusTypeMark *mark);
119 first_type_in_signature (const DBusString *str,
124 t = _dbus_string_get_byte (str, pos);
126 if (t == DBUS_STRUCT_BEGIN_CHAR)
127 return DBUS_TYPE_STRUCT;
133 element_type_get_alignment (const DBusString *str,
136 return _dbus_type_get_alignment (first_type_in_signature (str, pos));
140 reader_init (DBusTypeReader *reader,
142 const DBusString *type_str,
144 const DBusString *value_str,
147 reader->byte_order = byte_order;
148 reader->finished = FALSE;
149 reader->type_str = type_str;
150 reader->type_pos = type_pos;
151 reader->value_str = value_str;
152 reader->value_pos = value_pos;
156 base_reader_recurse (DBusTypeReader *sub,
157 DBusTypeReader *parent)
159 /* point subreader at the same place as parent */
169 struct_types_only_reader_recurse (DBusTypeReader *sub,
170 DBusTypeReader *parent)
172 base_reader_recurse (sub, parent);
174 _dbus_assert (_dbus_string_get_byte (sub->type_str,
175 sub->type_pos) == DBUS_STRUCT_BEGIN_CHAR);
181 struct_reader_recurse (DBusTypeReader *sub,
182 DBusTypeReader *parent)
184 struct_types_only_reader_recurse (sub, parent);
186 /* struct has 8 byte alignment */
187 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
191 array_types_only_reader_recurse (DBusTypeReader *sub,
192 DBusTypeReader *parent)
194 base_reader_recurse (sub, parent);
196 /* point type_pos at the array element type */
199 /* Init with values likely to crash things if misused */
200 sub->u.array.start_pos = _DBUS_INT_MAX;
201 sub->array_len_offset = 7;
204 /* array_len_offset is the offset back from start_pos to end of the len */
205 #define ARRAY_READER_LEN_POS(reader) \
206 ((reader)->u.array.start_pos - ((int)(reader)->array_len_offset) - 4)
209 array_reader_get_array_len (const DBusTypeReader *reader)
211 dbus_uint32_t array_len;
214 len_pos = ARRAY_READER_LEN_POS (reader);
216 _dbus_marshal_read_basic (reader->value_str,
223 #if RECURSIVE_MARSHAL_READ_TRACE
224 _dbus_verbose (" reader %p len_pos %d array len %u len_offset %d\n",
225 reader, len_pos, array_len, reader->array_len_offset);
228 _dbus_assert (reader->u.array.start_pos - len_pos - 4 < 8);
234 array_reader_recurse (DBusTypeReader *sub,
235 DBusTypeReader *parent)
240 array_types_only_reader_recurse (sub, parent);
242 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
244 len_pos = sub->value_pos;
246 sub->value_pos += 4; /* for the length */
248 alignment = element_type_get_alignment (sub->type_str,
251 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
253 sub->u.array.start_pos = sub->value_pos;
254 _dbus_assert ((sub->u.array.start_pos - (len_pos + 4)) < 8); /* only 3 bits in array_len_offset */
255 sub->array_len_offset = sub->u.array.start_pos - (len_pos + 4);
257 #if RECURSIVE_MARSHAL_READ_TRACE
258 _dbus_verbose (" type reader %p array start = %d len_offset = %d array len = %d array element type = %s\n",
260 sub->u.array.start_pos,
261 sub->array_len_offset,
262 array_reader_get_array_len (sub),
263 _dbus_type_to_string (first_type_in_signature (sub->type_str,
269 variant_reader_recurse (DBusTypeReader *sub,
270 DBusTypeReader *parent)
274 base_reader_recurse (sub, parent);
276 /* Variant is 1 byte sig length (without nul), signature with nul,
277 * padding to 8-boundary, then values
280 sig_len = _dbus_string_get_byte (sub->value_str, sub->value_pos);
282 sub->type_str = sub->value_str;
283 sub->type_pos = sub->value_pos + 1;
285 sub->value_pos = sub->type_pos + sig_len + 1;
287 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
289 #if RECURSIVE_MARSHAL_READ_TRACE
290 _dbus_verbose (" type reader %p variant containing '%s'\n",
292 _dbus_string_get_const_data_len (sub->type_str,
298 array_reader_check_finished (const DBusTypeReader *reader)
302 /* return the array element type if elements remain, and
303 * TYPE_INVALID otherwise
306 end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
308 _dbus_assert (reader->value_pos <= end_pos);
309 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
311 return reader->value_pos == end_pos;
315 skip_one_complete_type (const DBusString *type_str,
318 while (_dbus_string_get_byte (type_str, *type_pos) == DBUS_TYPE_ARRAY)
321 if (_dbus_string_get_byte (type_str, *type_pos) == DBUS_STRUCT_BEGIN_CHAR)
328 switch (_dbus_string_get_byte (type_str, *type_pos))
330 case DBUS_STRUCT_BEGIN_CHAR:
333 case DBUS_STRUCT_END_CHAR:
336 case DBUS_TYPE_INVALID:
337 _dbus_assert_not_reached ("unbalanced parens in signature");
348 find_len_of_complete_type (const DBusString *type_str,
355 skip_one_complete_type (type_str, &end);
357 return end - type_pos;
361 base_reader_next (DBusTypeReader *reader,
364 switch (current_type)
366 case DBUS_TYPE_STRUCT:
367 case DBUS_TYPE_VARIANT:
368 /* Scan forward over the entire container contents */
372 if (reader->klass->types_only && current_type == DBUS_TYPE_VARIANT)
376 /* Recurse into the struct or variant */
377 _dbus_type_reader_recurse (reader, &sub);
379 /* Skip everything in this subreader */
380 while (_dbus_type_reader_next (&sub))
385 if (!reader->klass->types_only)
386 reader->value_pos = sub.value_pos;
388 /* Now we are at the end of this container; for variants, the
389 * subreader's type_pos is totally inapplicable (it's in the
390 * value string) but we know that we increment by one past the
393 if (current_type == DBUS_TYPE_VARIANT)
394 reader->type_pos += 1;
396 reader->type_pos = sub.type_pos;
400 case DBUS_TYPE_ARRAY:
402 if (!reader->klass->types_only)
403 _dbus_marshal_skip_array (reader->value_str,
404 first_type_in_signature (reader->type_str,
405 reader->type_pos + 1),
409 skip_one_complete_type (reader->type_str, &reader->type_pos);
414 if (!reader->klass->types_only)
415 _dbus_marshal_skip_basic (reader->value_str,
416 current_type, reader->byte_order,
419 reader->type_pos += 1;
425 struct_reader_next (DBusTypeReader *reader,
430 base_reader_next (reader, current_type);
432 /* for STRUCT containers we return FALSE at the end of the struct,
433 * for INVALID we return FALSE at the end of the signature.
434 * In both cases we arrange for get_current_type() to return INVALID
435 * which is defined to happen iff we're at the end (no more next())
437 t = _dbus_string_get_byte (reader->type_str, reader->type_pos);
438 if (t == DBUS_STRUCT_END_CHAR)
440 reader->type_pos += 1;
441 reader->finished = TRUE;
446 array_types_only_reader_next (DBusTypeReader *reader,
449 /* We have one "element" to be iterated over
450 * in each array, which is its element type.
451 * So the finished flag indicates whether we've
452 * iterated over it yet or not.
454 reader->finished = TRUE;
458 array_reader_next (DBusTypeReader *reader,
461 /* Skip one array element */
464 end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
466 #if RECURSIVE_MARSHAL_READ_TRACE
467 _dbus_verbose (" reader %p array next START start_pos = %d end_pos = %d value_pos = %d current_type = %s\n",
469 reader->u.array.start_pos,
470 end_pos, reader->value_pos,
471 _dbus_type_to_string (current_type));
474 _dbus_assert (reader->value_pos < end_pos);
475 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
477 switch (first_type_in_signature (reader->type_str,
480 case DBUS_TYPE_STRUCT:
481 case DBUS_TYPE_VARIANT:
485 /* Recurse into the struct or variant */
486 _dbus_type_reader_recurse (reader, &sub);
488 /* Skip everything in this element */
489 while (_dbus_type_reader_next (&sub))
494 /* Now we are at the end of this element */
495 reader->value_pos = sub.value_pos;
499 case DBUS_TYPE_ARRAY:
501 _dbus_marshal_skip_array (reader->value_str,
502 first_type_in_signature (reader->type_str,
503 reader->type_pos + 1),
511 _dbus_marshal_skip_basic (reader->value_str,
512 current_type, reader->byte_order,
518 #if RECURSIVE_MARSHAL_READ_TRACE
519 _dbus_verbose (" reader %p array next END start_pos = %d end_pos = %d value_pos = %d current_type = %s\n",
521 reader->u.array.start_pos,
522 end_pos, reader->value_pos,
523 _dbus_type_to_string (current_type));
526 _dbus_assert (reader->value_pos <= end_pos);
528 if (reader->value_pos == end_pos)
530 skip_one_complete_type (reader->type_str,
536 array_init_from_mark (DBusTypeReader *reader,
537 const DBusTypeMark *mark)
539 /* Fill in the array-specific fields from the mark. The general
540 * fields are already filled in.
542 reader->u.array.start_pos = mark->array_start_pos;
543 reader->array_len_offset = mark->array_len_offset;
546 static const DBusTypeReaderClass body_reader_class = {
549 NULL, /* body is always toplevel, so doesn't get recursed into */
555 static const DBusTypeReaderClass body_types_only_reader_class = {
558 NULL, /* body is always toplevel, so doesn't get recursed into */
564 static const DBusTypeReaderClass struct_reader_class = {
567 struct_reader_recurse,
573 static const DBusTypeReaderClass struct_types_only_reader_class = {
576 struct_types_only_reader_recurse,
582 static const DBusTypeReaderClass array_reader_class = {
585 array_reader_recurse,
586 array_reader_check_finished,
591 static const DBusTypeReaderClass array_types_only_reader_class = {
594 array_types_only_reader_recurse,
596 array_types_only_reader_next,
600 static const DBusTypeReaderClass variant_reader_class = {
603 variant_reader_recurse,
609 static const DBusTypeReaderClass const *
610 all_reader_classes[] = {
612 &body_types_only_reader_class,
613 &struct_reader_class,
614 &struct_types_only_reader_class,
616 &array_types_only_reader_class,
617 &variant_reader_class
621 _dbus_type_reader_init (DBusTypeReader *reader,
623 const DBusString *type_str,
625 const DBusString *value_str,
628 reader->klass = &body_reader_class;
630 reader_init (reader, byte_order, type_str, type_pos,
631 value_str, value_pos);
633 #if RECURSIVE_MARSHAL_READ_TRACE
634 _dbus_verbose (" type reader %p init type_pos = %d value_pos = %d remaining sig '%s'\n",
635 reader, reader->type_pos, reader->value_pos,
636 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
641 _dbus_type_reader_init_from_mark (DBusTypeReader *reader,
643 const DBusString *type_str,
644 const DBusString *value_str,
645 const DBusTypeMark *mark)
647 reader->klass = all_reader_classes[mark->container_type];
649 reader_init (reader, byte_order,
650 mark->type_pos_in_value_str ? value_str : type_str,
652 value_str, mark->value_pos);
654 if (reader->klass->init_from_mark)
655 (* reader->klass->init_from_mark) (reader, mark);
657 #if RECURSIVE_MARSHAL_READ_TRACE
658 _dbus_verbose (" type reader %p init from mark type_pos = %d value_pos = %d remaining sig '%s'\n",
659 reader, reader->type_pos, reader->value_pos,
660 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
665 _dbus_type_reader_init_types_only (DBusTypeReader *reader,
666 const DBusString *type_str,
669 reader->klass = &body_types_only_reader_class;
671 reader_init (reader, DBUS_COMPILER_BYTE_ORDER /* irrelevant */,
672 type_str, type_pos, NULL, _DBUS_INT_MAX /* crashes if we screw up */);
674 #if RECURSIVE_MARSHAL_READ_TRACE
675 _dbus_verbose (" type reader %p init types only type_pos = %d remaining sig '%s'\n",
676 reader, reader->type_pos,
677 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
682 _dbus_type_reader_init_types_only_from_mark (DBusTypeReader *reader,
683 const DBusString *type_str,
684 const DBusTypeMark *mark)
686 reader->klass = all_reader_classes[mark->container_type];
687 _dbus_assert (reader->klass->types_only);
688 _dbus_assert (!mark->type_pos_in_value_str);
690 reader_init (reader, DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
691 type_str, mark->type_pos,
692 NULL, _DBUS_INT_MAX /* crashes if we screw up */);
694 if (reader->klass->init_from_mark)
695 (* reader->klass->init_from_mark) (reader, mark);
697 #if RECURSIVE_MARSHAL_READ_TRACE
698 _dbus_verbose (" type reader %p init types only from mark type_pos = %d remaining sig '%s'\n",
699 reader, reader->type_pos,
700 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
705 _dbus_type_reader_save_mark (const DBusTypeReader *reader,
708 mark->type_pos_in_value_str = (reader->type_str == reader->value_str);
709 mark->container_type = reader->klass->id;
710 _dbus_assert (all_reader_classes[reader->klass->id] == reader->klass);
712 mark->type_pos = reader->type_pos;
713 mark->value_pos = reader->value_pos;
715 /* these are just junk if the reader isn't really an array of course */
716 mark->array_len_offset = reader->array_len_offset;
717 mark->array_start_pos = reader->u.array.start_pos;
721 _dbus_type_reader_get_current_type (const DBusTypeReader *reader)
725 if (reader->finished ||
726 (reader->klass->check_finished &&
727 (* reader->klass->check_finished) (reader)))
728 t = DBUS_TYPE_INVALID;
730 t = first_type_in_signature (reader->type_str,
733 _dbus_assert (t != DBUS_STRUCT_END_CHAR);
734 _dbus_assert (t != DBUS_STRUCT_BEGIN_CHAR);
737 _dbus_verbose (" type reader %p current type_pos = %d type = %s\n",
738 reader, reader->type_pos,
739 _dbus_type_to_string (t));
746 _dbus_type_reader_get_array_type (const DBusTypeReader *reader)
750 _dbus_assert (_dbus_type_reader_get_current_type (reader) == DBUS_TYPE_ARRAY);
752 element_type = first_type_in_signature (reader->type_str,
753 reader->type_pos + 1);
759 _dbus_type_reader_get_value_pos (const DBusTypeReader *reader)
761 return reader->value_pos;
765 _dbus_type_reader_array_is_empty (const DBusTypeReader *reader)
767 dbus_uint32_t array_len;
769 _dbus_assert (_dbus_type_reader_get_current_type (reader) == DBUS_TYPE_ARRAY);
770 _dbus_assert (!reader->klass->types_only);
772 /* reader is supposed to be at an array child */
773 #if RECURSIVE_MARSHAL_READ_TRACE
774 _dbus_verbose ("checking array len at %d\n", reader->value_pos);
777 _dbus_marshal_read_basic (reader->value_str,
783 #if RECURSIVE_MARSHAL_READ_TRACE
784 _dbus_verbose (" ... array len = %d\n", array_len);
787 return array_len == 0;
791 * Get the address of the marshaled value in the data being read. The
792 * address may not be aligned; you have to align it to the type of the
793 * value you want to read. Most of the demarshal routines do this for
796 * @param reader the reader
797 * @param value_location the address of the marshaled value
800 _dbus_type_reader_read_raw (const DBusTypeReader *reader,
801 const unsigned char **value_location)
803 _dbus_assert (!reader->klass->types_only);
805 *value_location = _dbus_string_get_const_data_len (reader->value_str,
811 _dbus_type_reader_read_basic (const DBusTypeReader *reader,
816 _dbus_assert (!reader->klass->types_only);
818 t = _dbus_type_reader_get_current_type (reader);
820 _dbus_marshal_read_basic (reader->value_str,
827 #if RECURSIVE_MARSHAL_READ_TRACE
828 _dbus_verbose (" type reader %p read basic type_pos = %d value_pos = %d remaining sig '%s'\n",
829 reader, reader->type_pos, reader->value_pos,
830 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
835 * Reads a block of fixed-length basic values, from the current point
836 * in an array to the end of the array. Does not work for arrays of
837 * string or container types.
839 * This function returns the array in-place; it does not make a copy,
840 * and it does not swap the bytes.
842 * If you ask for #DBUS_TYPE_DOUBLE you will get a "const double*" back
843 * and the "value" argument should be a "const double**" and so on.
845 * @param reader the reader to read from
846 * @param value place to return the array values
847 * @param n_elements place to return number of array elements
850 _dbus_type_reader_read_fixed_multi (const DBusTypeReader *reader,
860 _dbus_assert (!reader->klass->types_only);
861 _dbus_assert (reader->klass == &array_reader_class);
863 element_type = first_type_in_signature (reader->type_str,
866 _dbus_assert (element_type != DBUS_TYPE_INVALID); /* why we don't use get_current_type() */
867 _dbus_assert (_dbus_type_is_fixed (element_type));
869 alignment = _dbus_type_get_alignment (element_type);
871 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
873 total_len = array_reader_get_array_len (reader);
874 end_pos = reader->u.array.start_pos + total_len;
875 remaining_len = end_pos - reader->value_pos;
877 #if RECURSIVE_MARSHAL_READ_TRACE
878 _dbus_verbose ("end_pos %d total_len %d remaining_len %d value_pos %d\n",
879 end_pos, total_len, remaining_len, reader->value_pos);
882 _dbus_assert (remaining_len <= total_len);
884 if (remaining_len == 0)
885 *(const DBusBasicValue**) value = NULL;
887 *(const DBusBasicValue**) value =
888 (void*) _dbus_string_get_const_data_len (reader->value_str,
892 *n_elements = remaining_len / alignment;
893 _dbus_assert ((remaining_len % alignment) == 0);
895 #if RECURSIVE_MARSHAL_READ_TRACE
896 _dbus_verbose (" type reader %p read fixed array type_pos = %d value_pos = %d remaining sig '%s'\n",
897 reader, reader->type_pos, reader->value_pos,
898 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
903 * Initialize a new reader pointing to the first type and
904 * corresponding value that's a child of the current container. It's
905 * an error to call this if the current type is a non-container.
907 * Note that DBusTypeReader traverses values, not types. So if you
908 * have an empty array of array of int, you can't recurse into it. You
909 * can only recurse into each element.
911 * @param reader the reader
912 * @param sub a reader to init pointing to the first child
915 _dbus_type_reader_recurse (DBusTypeReader *reader,
920 t = first_type_in_signature (reader->type_str, reader->type_pos);
924 case DBUS_TYPE_STRUCT:
925 if (reader->klass->types_only)
926 sub->klass = &struct_types_only_reader_class;
928 sub->klass = &struct_reader_class;
930 case DBUS_TYPE_ARRAY:
931 if (reader->klass->types_only)
932 sub->klass = &array_types_only_reader_class;
934 sub->klass = &array_reader_class;
936 case DBUS_TYPE_VARIANT:
937 if (reader->klass->types_only)
938 _dbus_assert_not_reached ("can't recurse into variant typecode");
940 sub->klass = &variant_reader_class;
943 _dbus_verbose ("recursing into type %s\n", _dbus_type_to_string (t));
944 #ifndef DBUS_DISABLE_CHECKS
945 if (t == DBUS_TYPE_INVALID)
946 _dbus_warn ("You can't recurse into an empty array or off the end of a message body\n");
947 #endif /* DBUS_DISABLE_CHECKS */
949 _dbus_assert_not_reached ("don't yet handle recursing into this type");
952 _dbus_assert (sub->klass == all_reader_classes[sub->klass->id]);
954 (* sub->klass->recurse) (sub, reader);
956 #if RECURSIVE_MARSHAL_READ_TRACE
957 _dbus_verbose (" type reader %p RECURSED type_pos = %d value_pos = %d remaining sig '%s'\n",
958 sub, sub->type_pos, sub->value_pos,
959 _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0));
964 * Skip to the next value on this "level". e.g. the next field in a
965 * struct, the next value in an array. Returns FALSE at the end of the
968 * @param reader the reader
969 * @returns FALSE if nothing more to read at or below this level
972 _dbus_type_reader_next (DBusTypeReader *reader)
976 t = _dbus_type_reader_get_current_type (reader);
978 #if RECURSIVE_MARSHAL_READ_TRACE
979 _dbus_verbose (" type reader %p START next() { type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
980 reader, reader->type_pos, reader->value_pos,
981 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
982 _dbus_type_to_string (t));
985 if (t == DBUS_TYPE_INVALID)
988 (* reader->klass->next) (reader, t);
990 #if RECURSIVE_MARSHAL_READ_TRACE
991 _dbus_verbose (" type reader %p END next() type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
992 reader, reader->type_pos, reader->value_pos,
993 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
994 _dbus_type_to_string (_dbus_type_reader_get_current_type (reader)));
997 return _dbus_type_reader_get_current_type (reader) != DBUS_TYPE_INVALID;
1001 * Check whether there's another value on this "level". e.g. the next
1002 * field in a struct, the next value in an array. Returns FALSE at the
1003 * end of the current container.
1005 * You probably don't want to use this; it makes for an awkward for/while
1006 * loop. A nicer one is "while ((current_type = get_current_type()) != INVALID)"
1008 * @param reader the reader
1009 * @returns FALSE if nothing more to read at or below this level
1012 _dbus_type_reader_has_next (const DBusTypeReader *reader)
1014 /* Not efficient but works for now. */
1015 DBusTypeReader copy;
1018 return _dbus_type_reader_next (©);
1022 * Gets the string and range of said string containing the signature
1023 * of the current value. Essentially a more complete version of
1024 * _dbus_type_reader_get_current_type() (returns the full type
1025 * rather than only the outside of the onion).
1027 * Note though that the first byte in a struct signature is
1028 * #DBUS_STRUCT_BEGIN_CHAR while the current type will be
1029 * #DBUS_TYPE_STRUCT so it isn't true that the first byte of the
1030 * signature is always the same as the current type. Another
1031 * difference is that this function will still return a signature when
1032 * inside an empty array; say you recurse into empty array of int32,
1033 * the signature is "i" but the current type will always be
1034 * #DBUS_TYPE_INVALID since there are no elements to be currently
1037 * @param reader the reader
1038 * @param str_p place to return the string with the type in it
1039 * @param start_p place to return start of the type
1040 * @param len_p place to return the length of the type
1043 _dbus_type_reader_get_signature (const DBusTypeReader *reader,
1044 const DBusString **str_p,
1048 *str_p = reader->type_str;
1049 *start_p = reader->type_pos;
1050 *len_p = find_len_of_complete_type (reader->type_str, reader->type_pos);
1055 DBusString replacement;
1060 replacement_block_init (ReplacementBlock *block,
1061 DBusTypeReader *reader)
1063 if (!_dbus_string_init (&block->replacement))
1066 /* % 8 is the padding to have the same align properties in
1067 * our replacement string as we do at the position being replaced
1069 block->padding = reader->value_pos % 8;
1071 if (!_dbus_string_lengthen (&block->replacement, block->padding))
1077 _dbus_string_free (&block->replacement);
1082 replacement_block_replace (ReplacementBlock *block,
1083 DBusTypeReader *reader,
1084 const DBusTypeReader *realign_root)
1086 DBusTypeWriter writer;
1087 DBusTypeReader realign_reader;
1091 _dbus_assert (realign_root != NULL);
1093 orig_len = _dbus_string_get_length (&block->replacement);
1095 realign_reader = *realign_root;
1097 #if RECURSIVE_MARSHAL_WRITE_TRACE
1098 _dbus_verbose ("INITIALIZING replacement block writer %p at value_pos %d\n",
1099 &writer, _dbus_string_get_length (&block->replacement));
1101 _dbus_type_writer_init_values_only (&writer,
1102 realign_reader.byte_order,
1103 realign_reader.type_str,
1104 realign_reader.type_pos,
1105 &block->replacement,
1106 _dbus_string_get_length (&block->replacement));
1108 _dbus_assert (realign_reader.value_pos <= reader->value_pos);
1110 #if RECURSIVE_MARSHAL_WRITE_TRACE
1111 _dbus_verbose ("COPYING from reader at value_pos %d to writer %p starting after value_pos %d\n",
1112 realign_reader.value_pos, &writer, reader->value_pos);
1115 if (!_dbus_type_writer_write_reader_partial (&writer,
1119 _dbus_string_get_length (&block->replacement) - block->padding,
1123 #if RECURSIVE_MARSHAL_WRITE_TRACE
1124 _dbus_verbose ("REPLACEMENT at padding %d len %d\n", block->padding,
1125 _dbus_string_get_length (&block->replacement) - block->padding);
1126 _dbus_verbose_bytes_of_string (&block->replacement, block->padding,
1127 _dbus_string_get_length (&block->replacement) - block->padding);
1128 _dbus_verbose ("TO BE REPLACED at value_pos = %d (align pad %d) len %d realign_reader.value_pos %d\n",
1129 reader->value_pos, reader->value_pos % 8,
1130 realign_reader.value_pos - reader->value_pos,
1131 realign_reader.value_pos);
1132 _dbus_verbose_bytes_of_string (reader->value_str,
1134 realign_reader.value_pos - reader->value_pos);
1137 /* Move the replacement into position
1138 * (realign_reader should now be at the end of the block to be replaced)
1140 if (!_dbus_string_replace_len (&block->replacement, block->padding,
1141 _dbus_string_get_length (&block->replacement) - block->padding,
1142 (DBusString*) reader->value_str,
1144 realign_reader.value_pos - reader->value_pos))
1147 /* Process our fixups now that we can't have an OOM error */
1148 apply_and_free_fixups (&fixups, reader);
1153 _dbus_string_set_length (&block->replacement, orig_len);
1154 free_fixups (&fixups);
1159 replacement_block_free (ReplacementBlock *block)
1161 _dbus_string_free (&block->replacement);
1164 /* In the variable-length case, we have to fix alignment after we insert.
1165 * The strategy is as follows:
1167 * - pad a new string to have the same alignment as the
1168 * start of the current basic value
1169 * - write the new basic value
1170 * - copy from the original reader to the new string,
1171 * which will fix the alignment of types following
1173 * - this copy has to start at realign_root,
1174 * but not really write anything until it
1175 * passes the value being set
1176 * - as an optimization, we can stop copying
1177 * when the source and dest values are both
1178 * on an 8-boundary, since we know all following
1179 * padding and alignment will be identical
1180 * - copy the new string back to the original
1181 * string, replacing the relevant part of the
1183 * - now any arrays in the original string that
1184 * contained the replaced string may have the
1185 * wrong length; so we have to fix that
1188 reader_set_basic_variable_length (DBusTypeReader *reader,
1191 const DBusTypeReader *realign_root)
1194 ReplacementBlock block;
1195 DBusTypeWriter writer;
1197 _dbus_assert (realign_root != NULL);
1201 if (!replacement_block_init (&block, reader))
1204 /* Write the new basic value */
1205 #if RECURSIVE_MARSHAL_WRITE_TRACE
1206 _dbus_verbose ("INITIALIZING writer %p to write basic value at value_pos %d of replacement string\n",
1207 &writer, _dbus_string_get_length (&block.replacement));
1209 _dbus_type_writer_init_values_only (&writer,
1214 _dbus_string_get_length (&block.replacement));
1215 #if RECURSIVE_MARSHAL_WRITE_TRACE
1216 _dbus_verbose ("WRITING basic value to writer %p (replacement string)\n", &writer);
1218 if (!_dbus_type_writer_write_basic (&writer, current_type, value))
1221 if (!replacement_block_replace (&block,
1229 replacement_block_free (&block);
1234 reader_set_basic_fixed_length (DBusTypeReader *reader,
1238 _dbus_marshal_set_basic ((DBusString*) reader->value_str,
1247 * Sets a new value for the basic type value pointed to by the reader,
1248 * leaving the reader valid to continue reading. Any other readers
1249 * will be invalidated if you set a variable-length type such as a
1252 * The provided realign_root is the reader to start from when
1253 * realigning the data that follows the newly-set value. The reader
1254 * parameter must point to a value below the realign_root parameter.
1255 * If the type being set is fixed-length, then realign_root may be
1256 * #NULL. Only values reachable from realign_root will be realigned,
1257 * so if your string contains other values you will need to deal with
1258 * those somehow yourself. It is OK if realign_root is the same
1259 * reader as the reader parameter, though if you aren't setting the
1260 * root it may not be such a good idea.
1262 * @todo DBusTypeReader currently takes "const" versions of the type
1263 * and value strings, and this function modifies those strings by
1264 * casting away the const, which is of course bad if we want to get
1265 * picky. (To be truly clean you'd have an object which contained the
1266 * type and value strings and set_basic would be a method on that
1267 * object... this would also make DBusTypeReader the same thing as
1268 * DBusTypeMark. But since DBusMessage is effectively that object for
1269 * D-BUS it doesn't seem worth creating some random object.)
1271 * @todo optimize this by only rewriting until the old and new values
1272 * are at the same alignment. Frequently this should result in only
1273 * replacing the value that's immediately at hand.
1275 * @param reader reader indicating where to set a new value
1276 * @param value address of the value to set
1277 * @param realign_root realign from here
1278 * @returns #FALSE if not enough memory
1281 _dbus_type_reader_set_basic (DBusTypeReader *reader,
1283 const DBusTypeReader *realign_root)
1287 _dbus_assert (!reader->klass->types_only);
1288 _dbus_assert (reader->value_str == realign_root->value_str);
1289 _dbus_assert (reader->value_pos >= realign_root->value_pos);
1291 current_type = _dbus_type_reader_get_current_type (reader);
1293 #if RECURSIVE_MARSHAL_WRITE_TRACE
1294 _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",
1295 reader, reader->type_pos, reader->value_pos,
1296 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
1298 realign_root ? realign_root->value_pos : -1,
1299 _dbus_type_to_string (current_type));
1300 _dbus_verbose_bytes_of_string (realign_root->value_str, realign_root->value_pos,
1301 _dbus_string_get_length (realign_root->value_str) -
1302 realign_root->value_pos);
1305 _dbus_assert (_dbus_type_is_basic (current_type));
1307 if (_dbus_type_is_fixed (current_type))
1309 reader_set_basic_fixed_length (reader, current_type, value);
1314 _dbus_assert (realign_root != NULL);
1315 return reader_set_basic_variable_length (reader, current_type,
1316 value, realign_root);
1321 * Recursively deletes any value pointed to by the reader, leaving the
1322 * reader valid to continue reading. Any other readers will be
1325 * The provided realign_root is the reader to start from when
1326 * realigning the data that follows the newly-set value.
1327 * See _dbus_type_reader_set_basic() for more details on the
1328 * realign_root paramter.
1330 * @todo for now this does not delete the typecodes associated with
1331 * the value, so this function should only be used for array elements.
1333 * @param reader reader indicating where to delete a value
1334 * @param realign_root realign from here
1335 * @returns #FALSE if not enough memory
1338 _dbus_type_reader_delete (DBusTypeReader *reader,
1339 const DBusTypeReader *realign_root)
1342 ReplacementBlock block;
1344 _dbus_assert (realign_root != NULL);
1345 _dbus_assert (reader->klass == &array_reader_class);
1349 if (!replacement_block_init (&block, reader))
1352 if (!replacement_block_replace (&block,
1360 replacement_block_free (&block);
1365 * Compares two readers, which must be iterating over the same value data.
1366 * Returns #TRUE if the first parameter is further along than the second parameter.
1368 * @param lhs left-hand-side (first) parameter
1369 * @param rhs left-hand-side (first) parameter
1370 * @returns whether lhs is greater than rhs
1373 _dbus_type_reader_greater_than (const DBusTypeReader *lhs,
1374 const DBusTypeReader *rhs)
1376 _dbus_assert (lhs->value_str == rhs->value_str);
1378 return lhs->value_pos > rhs->value_pos;
1391 * Initialize a write iterator, which is used to write out values in
1392 * serialized D-BUS format.
1394 * The type_pos passed in is expected to be inside an already-valid,
1395 * though potentially empty, type signature. This means that the byte
1396 * after type_pos must be either #DBUS_TYPE_INVALID (aka nul) or some
1397 * other valid type. #DBusTypeWriter won't enforce that the signature
1398 * is already valid (you can append the nul byte at the end if you
1399 * like), but just be aware that you need the nul byte eventually and
1400 * #DBusTypeWriter isn't going to write it for you.
1402 * @param writer the writer to init
1403 * @param byte_order the byte order to marshal into
1404 * @param type_str the string to write typecodes into
1405 * @param type_pos where to insert typecodes
1406 * @param value_str the string to write values into
1407 * @param value_pos where to insert values
1411 _dbus_type_writer_init (DBusTypeWriter *writer,
1413 DBusString *type_str,
1415 DBusString *value_str,
1418 writer->byte_order = byte_order;
1419 writer->type_str = type_str;
1420 writer->type_pos = type_pos;
1421 writer->value_str = value_str;
1422 writer->value_pos = value_pos;
1423 writer->container_type = DBUS_TYPE_INVALID;
1424 writer->type_pos_is_expectation = FALSE;
1425 writer->enabled = TRUE;
1427 #if RECURSIVE_MARSHAL_WRITE_TRACE
1428 _dbus_verbose ("writer %p init remaining sig '%s'\n", writer,
1430 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1436 * Initialize a write iterator, with the signature to be provided
1439 * @param writer the writer to init
1440 * @param byte_order the byte order to marshal into
1441 * @param value_str the string to write values into
1442 * @param value_pos where to insert values
1446 _dbus_type_writer_init_types_delayed (DBusTypeWriter *writer,
1448 DBusString *value_str,
1451 _dbus_type_writer_init (writer, byte_order,
1452 NULL, 0, value_str, value_pos);
1456 * Adds type string to the writer, if it had none.
1458 * @param writer the writer to init
1459 * @param type_str type string to add
1460 * @param type_pos type position
1464 _dbus_type_writer_add_types (DBusTypeWriter *writer,
1465 DBusString *type_str,
1468 if (writer->type_str == NULL) /* keeps us from using this as setter */
1470 writer->type_str = type_str;
1471 writer->type_pos = type_pos;
1476 * Removes type string from the writer.
1478 * @param writer the writer to remove from
1481 _dbus_type_writer_remove_types (DBusTypeWriter *writer)
1483 writer->type_str = NULL;
1484 writer->type_pos = -1;
1488 * Like _dbus_type_writer_init(), except the type string
1489 * passed in should correspond to an existing signature that
1490 * matches what you're going to write out. The writer will
1491 * check what you write vs. this existing signature.
1493 * @param writer the writer to init
1494 * @param byte_order the byte order to marshal into
1495 * @param type_str the string with signature
1496 * @param type_pos start of signature
1497 * @param value_str the string to write values into
1498 * @param value_pos where to insert values
1502 _dbus_type_writer_init_values_only (DBusTypeWriter *writer,
1504 const DBusString *type_str,
1506 DBusString *value_str,
1509 _dbus_type_writer_init (writer, byte_order,
1510 (DBusString*)type_str, type_pos,
1511 value_str, value_pos);
1513 writer->type_pos_is_expectation = TRUE;
1517 _dbus_type_writer_write_basic_no_typecode (DBusTypeWriter *writer,
1521 if (writer->enabled)
1522 return _dbus_marshal_write_basic (writer->value_str,
1527 &writer->value_pos);
1532 /* If our parent is an array, things are a little bit complicated.
1534 * The parent must have a complete element type, such as
1535 * "i" or "aai" or "(ii)" or "a(ii)". There can't be
1536 * unclosed parens, or an "a" with no following type.
1538 * To recurse, the only allowed operation is to recurse into the
1539 * first type in the element type. So for "i" you can't recurse, for
1540 * "ai" you can recurse into the array, for "(ii)" you can recurse
1543 * If you recurse into the array for "ai", then you must specify
1544 * "i" for the element type of the array you recurse into.
1546 * While inside an array at any level, we need to avoid writing to
1547 * type_str, since the type only appears once for the whole array,
1548 * it does not appear for each array element.
1550 * While inside an array type_pos points to the expected next
1551 * typecode, rather than the next place we could write a typecode.
1554 writer_recurse_init_and_check (DBusTypeWriter *writer,
1556 DBusTypeWriter *sub)
1558 _dbus_type_writer_init (sub,
1565 sub->container_type = container_type;
1567 if (writer->type_pos_is_expectation ||
1568 (sub->container_type == DBUS_TYPE_ARRAY || sub->container_type == DBUS_TYPE_VARIANT))
1569 sub->type_pos_is_expectation = TRUE;
1571 sub->type_pos_is_expectation = FALSE;
1573 sub->enabled = writer->enabled;
1575 #ifndef DBUS_DISABLE_CHECKS
1576 if (writer->type_pos_is_expectation && writer->type_str)
1580 expected = first_type_in_signature (writer->type_str, writer->type_pos);
1582 if (expected != sub->container_type)
1584 _dbus_warn ("Writing an element of type %s, but the expected type here is %s\n",
1585 _dbus_type_to_string (sub->container_type),
1586 _dbus_type_to_string (expected));
1587 _dbus_assert_not_reached ("bad array element or variant content written");
1590 #endif /* DBUS_DISABLE_CHECKS */
1592 #if RECURSIVE_MARSHAL_WRITE_TRACE
1593 _dbus_verbose (" type writer %p recurse parent %s type_pos = %d value_pos = %d is_expectation = %d remaining sig '%s' enabled = %d\n",
1595 _dbus_type_to_string (writer->container_type),
1596 writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
1598 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1601 _dbus_verbose (" type writer %p recurse sub %s type_pos = %d value_pos = %d is_expectation = %d enabled = %d\n",
1603 _dbus_type_to_string (sub->container_type),
1604 sub->type_pos, sub->value_pos,
1605 sub->type_pos_is_expectation,
1611 write_or_verify_typecode (DBusTypeWriter *writer,
1614 /* A subwriter inside an array or variant will have type_pos
1615 * pointing to the expected typecode; a writer not inside an array
1616 * or variant has type_pos pointing to the next place to insert a
1619 #if RECURSIVE_MARSHAL_WRITE_TRACE
1620 _dbus_verbose (" type writer %p write_or_verify start type_pos = %d remaining sig '%s' enabled = %d\n",
1621 writer, writer->type_pos,
1623 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
1628 if (writer->type_str == NULL)
1631 if (writer->type_pos_is_expectation)
1633 #ifndef DBUS_DISABLE_CHECKS
1637 expected = _dbus_string_get_byte (writer->type_str, writer->type_pos);
1639 if (expected != typecode)
1641 _dbus_warn ("Array or variant type requires that type %s be written, but %s was written\n",
1642 _dbus_type_to_string (expected), _dbus_type_to_string (typecode));
1643 _dbus_assert_not_reached ("bad type inserted somewhere inside an array or variant");
1646 #endif /* DBUS_DISABLE_CHECKS */
1648 /* if immediately inside an array we'd always be appending an element,
1649 * so the expected type doesn't change; if inside a struct or something
1650 * below an array, we need to move through said struct or something.
1652 if (writer->container_type != DBUS_TYPE_ARRAY)
1653 writer->type_pos += 1;
1657 if (!_dbus_string_insert_byte (writer->type_str,
1662 writer->type_pos += 1;
1665 #if RECURSIVE_MARSHAL_WRITE_TRACE
1666 _dbus_verbose (" type writer %p write_or_verify end type_pos = %d remaining sig '%s'\n",
1667 writer, writer->type_pos,
1668 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0));
1675 writer_recurse_struct (DBusTypeWriter *writer,
1676 const DBusString *contained_type,
1677 int contained_type_start,
1678 int contained_type_len,
1679 DBusTypeWriter *sub)
1681 /* FIXME right now contained_type is ignored; we could probably
1682 * almost trivially fix the code so if it's present we
1683 * write it out and then set type_pos_is_expectation
1686 /* Ensure that we'll be able to add alignment padding and the typecode */
1687 if (writer->enabled)
1689 if (!_dbus_string_alloc_space (sub->value_str, 8))
1693 if (!write_or_verify_typecode (sub, DBUS_STRUCT_BEGIN_CHAR))
1694 _dbus_assert_not_reached ("failed to insert struct typecode after prealloc");
1696 if (writer->enabled)
1698 if (!_dbus_string_insert_bytes (sub->value_str,
1700 _DBUS_ALIGN_VALUE (sub->value_pos, 8) - sub->value_pos,
1702 _dbus_assert_not_reached ("should not have failed to insert alignment padding for struct");
1703 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
1711 writer_recurse_array (DBusTypeWriter *writer,
1712 const DBusString *contained_type,
1713 int contained_type_start,
1714 int contained_type_len,
1715 DBusTypeWriter *sub,
1716 dbus_bool_t is_array_append)
1718 dbus_uint32_t value = 0;
1722 #ifndef DBUS_DISABLE_CHECKS
1723 if (writer->container_type == DBUS_TYPE_ARRAY &&
1726 if (!_dbus_string_equal_substring (contained_type,
1727 contained_type_start,
1730 writer->u.array.element_type_pos + 1))
1732 _dbus_warn ("Writing an array of '%s' but this is incompatible with the expected type of elements in the parent array\n",
1733 _dbus_string_get_const_data_len (contained_type,
1734 contained_type_start,
1735 contained_type_len));
1736 _dbus_assert_not_reached ("incompatible type for child array");
1739 #endif /* DBUS_DISABLE_CHECKS */
1741 if (writer->enabled && !is_array_append)
1743 /* 3 pad + 4 bytes for the array length, and 4 bytes possible padding
1744 * before array values
1746 if (!_dbus_string_alloc_space (sub->value_str, 3 + 4 + 4))
1750 if (writer->type_str != NULL)
1752 sub->type_pos += 1; /* move to point to the element type, since type_pos
1753 * should be the expected type for further writes
1755 sub->u.array.element_type_pos = sub->type_pos;
1758 if (!writer->type_pos_is_expectation)
1760 /* sub is a toplevel/outermost array so we need to write the type data */
1762 /* alloc space for array typecode, element signature */
1763 if (!_dbus_string_alloc_space (writer->type_str, 1 + contained_type_len))
1766 if (!_dbus_string_insert_byte (writer->type_str,
1769 _dbus_assert_not_reached ("failed to insert array typecode after prealloc");
1771 if (!_dbus_string_copy_len (contained_type,
1772 contained_type_start, contained_type_len,
1774 sub->u.array.element_type_pos))
1775 _dbus_assert_not_reached ("should not have failed to insert array element typecodes");
1778 if (writer->type_str != NULL)
1780 /* If the parent is an array, we hold type_pos pointing at the array element type;
1781 * otherwise advance it to reflect the array value we just recursed into
1783 if (writer->container_type != DBUS_TYPE_ARRAY)
1784 writer->type_pos += 1 + contained_type_len;
1786 _dbus_assert (writer->type_pos_is_expectation); /* because it's an array */
1789 if (writer->enabled)
1791 /* Write (or jump over, if is_array_append) the length */
1792 sub->u.array.len_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
1794 if (is_array_append)
1796 sub->value_pos += 4;
1800 if (!_dbus_type_writer_write_basic_no_typecode (sub, DBUS_TYPE_UINT32,
1802 _dbus_assert_not_reached ("should not have failed to insert array len");
1805 _dbus_assert (sub->u.array.len_pos == sub->value_pos - 4);
1807 /* Write alignment padding for array elements
1808 * Note that we write the padding *even for empty arrays*
1809 * to avoid wonky special cases
1811 alignment = element_type_get_alignment (contained_type, contained_type_start);
1813 aligned = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
1814 if (aligned != sub->value_pos)
1816 if (!is_array_append)
1818 if (!_dbus_string_insert_bytes (sub->value_str,
1820 aligned - sub->value_pos,
1822 _dbus_assert_not_reached ("should not have failed to insert alignment padding");
1825 sub->value_pos = aligned;
1828 sub->u.array.start_pos = sub->value_pos;
1830 if (is_array_append)
1834 len = _dbus_marshal_read_uint32 (sub->value_str,
1835 sub->u.array.len_pos,
1836 sub->byte_order, NULL);
1838 sub->value_pos += len;
1843 /* not enabled, so we won't write the len_pos; set it to -1 to so indicate */
1844 sub->u.array.len_pos = -1;
1845 sub->u.array.start_pos = sub->value_pos;
1848 _dbus_assert (sub->u.array.len_pos < sub->u.array.start_pos);
1849 _dbus_assert (is_array_append || sub->u.array.start_pos == sub->value_pos);
1851 #if RECURSIVE_MARSHAL_WRITE_TRACE
1852 _dbus_verbose (" type writer %p recurse array done remaining sig '%s' array start_pos = %d len_pos = %d value_pos = %d\n", sub,
1854 _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0) :
1856 sub->u.array.start_pos, sub->u.array.len_pos, sub->value_pos);
1862 /* Variant value will normally have:
1863 * 1 byte signature length not including nul
1864 * signature typecodes (nul terminated)
1865 * padding to 8-boundary
1866 * body according to signature
1868 * The signature string can only have a single type
1869 * in it but that type may be complex/recursive.
1871 * So a typical variant type with the integer 3 will have these
1873 * 0x1 'i' '\0' [padding to 8-boundary] 0x0 0x0 0x0 0x3
1875 * For an array of 4-byte types stuffed into variants, the padding to
1876 * 8-boundary is only the 1 byte that is required for the 4-boundary
1877 * anyhow for all array elements after the first one. And for single
1878 * variants in isolation, wasting a few bytes is hardly a big deal.
1880 * The main world of hurt for writing out a variant is that the type
1881 * string is the same string as the value string. Which means
1882 * inserting to the type string will move the value_pos; and it means
1883 * that inserting to the type string could break type alignment.
1885 * This type alignment issue is why the body of the variant is always
1886 * 8-aligned. Then we know that re-8-aligning the start of the body
1887 * will always correctly align the full contents of the variant type.
1890 writer_recurse_variant (DBusTypeWriter *writer,
1891 const DBusString *contained_type,
1892 int contained_type_start,
1893 int contained_type_len,
1894 DBusTypeWriter *sub)
1896 if (writer->enabled)
1898 /* Allocate space for the worst case, which is 1 byte sig
1899 * length, nul byte at end of sig, and 7 bytes padding to
1902 if (!_dbus_string_alloc_space (sub->value_str, contained_type_len + 9))
1906 /* write VARIANT typecode to the parent's type string */
1907 if (!write_or_verify_typecode (writer, DBUS_TYPE_VARIANT))
1910 /* If not enabled, mark that we have no type_str anymore ... */
1912 if (!writer->enabled)
1914 sub->type_str = NULL;
1920 /* If we're enabled then continue ... */
1922 if (!_dbus_string_insert_byte (sub->value_str,
1924 contained_type_len))
1925 _dbus_assert_not_reached ("should not have failed to insert variant type sig len");
1927 sub->value_pos += 1;
1929 /* Here we switch over to the expected type sig we're about to write */
1930 sub->type_str = sub->value_str;
1931 sub->type_pos = sub->value_pos;
1933 if (!_dbus_string_copy_len (contained_type, contained_type_start, contained_type_len,
1934 sub->value_str, sub->value_pos))
1935 _dbus_assert_not_reached ("should not have failed to insert variant type sig");
1937 sub->value_pos += contained_type_len;
1939 if (!_dbus_string_insert_byte (sub->value_str,
1942 _dbus_assert_not_reached ("should not have failed to insert variant type nul termination");
1944 sub->value_pos += 1;
1946 if (!_dbus_string_insert_bytes (sub->value_str,
1948 _DBUS_ALIGN_VALUE (sub->value_pos, 8) - sub->value_pos,
1950 _dbus_assert_not_reached ("should not have failed to insert alignment padding for variant body");
1951 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
1957 _dbus_type_writer_recurse_contained_len (DBusTypeWriter *writer,
1959 const DBusString *contained_type,
1960 int contained_type_start,
1961 int contained_type_len,
1962 DBusTypeWriter *sub,
1963 dbus_bool_t is_array_append)
1965 writer_recurse_init_and_check (writer, container_type, sub);
1967 switch (container_type)
1969 case DBUS_TYPE_STRUCT:
1970 return writer_recurse_struct (writer,
1971 contained_type, contained_type_start, contained_type_len,
1974 case DBUS_TYPE_ARRAY:
1975 return writer_recurse_array (writer,
1976 contained_type, contained_type_start, contained_type_len,
1977 sub, is_array_append);
1979 case DBUS_TYPE_VARIANT:
1980 return writer_recurse_variant (writer,
1981 contained_type, contained_type_start, contained_type_len,
1985 _dbus_assert_not_reached ("tried to recurse into type that doesn't support that");
1992 _dbus_type_writer_recurse (DBusTypeWriter *writer,
1994 const DBusString *contained_type,
1995 int contained_type_start,
1996 DBusTypeWriter *sub)
1998 int contained_type_len;
2001 contained_type_len = find_len_of_complete_type (contained_type, contained_type_start);
2003 contained_type_len = 0;
2005 return _dbus_type_writer_recurse_contained_len (writer, container_type,
2007 contained_type_start,
2014 * Append to an existing array. Essentially, the writer will read an
2015 * existing length at the write location; jump over that length; and
2016 * write new fields. On unrecurse(), the existing length will be
2019 * @param writer the writer
2020 * @param contained_type element type
2021 * @param contained_type_start position of element type
2022 * @param sub the subwriter to init
2023 * @returns #FALSE if no memory
2026 _dbus_type_writer_append_array (DBusTypeWriter *writer,
2027 const DBusString *contained_type,
2028 int contained_type_start,
2029 DBusTypeWriter *sub)
2031 int contained_type_len;
2034 contained_type_len = find_len_of_complete_type (contained_type, contained_type_start);
2036 contained_type_len = 0;
2038 return _dbus_type_writer_recurse_contained_len (writer, DBUS_TYPE_ARRAY,
2040 contained_type_start,
2047 writer_get_array_len (DBusTypeWriter *writer)
2049 _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY);
2050 return writer->value_pos - writer->u.array.start_pos;
2054 _dbus_type_writer_unrecurse (DBusTypeWriter *writer,
2055 DBusTypeWriter *sub)
2057 /* type_pos_is_expectation never gets unset once set, or we'd get all hosed */
2058 _dbus_assert (!writer->type_pos_is_expectation ||
2059 (writer->type_pos_is_expectation && sub->type_pos_is_expectation));
2061 #if RECURSIVE_MARSHAL_WRITE_TRACE
2062 _dbus_verbose (" type writer %p unrecurse type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
2063 writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
2064 _dbus_type_to_string (writer->container_type));
2065 _dbus_verbose (" type writer %p unrecurse sub type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
2066 sub, sub->type_pos, sub->value_pos,
2067 sub->type_pos_is_expectation,
2068 _dbus_type_to_string (sub->container_type));
2071 if (sub->container_type == DBUS_TYPE_STRUCT)
2073 if (!write_or_verify_typecode (sub, DBUS_STRUCT_END_CHAR))
2076 else if (sub->container_type == DBUS_TYPE_ARRAY)
2078 if (sub->u.array.len_pos >= 0) /* len_pos == -1 if we weren't enabled when we passed it */
2082 /* Set the array length */
2083 len = writer_get_array_len (sub);
2084 _dbus_marshal_set_uint32 (sub->value_str,
2085 sub->u.array.len_pos,
2088 #if RECURSIVE_MARSHAL_WRITE_TRACE
2089 _dbus_verbose (" filled in sub array len to %u at len_pos %d\n",
2090 len, sub->u.array.len_pos);
2093 #if RECURSIVE_MARSHAL_WRITE_TRACE
2096 _dbus_verbose (" not filling in sub array len because we were disabled when we passed the len\n");
2101 /* Now get type_pos right for the parent writer. Here are the cases:
2103 * Cases !writer->type_pos_is_expectation:
2104 * (in these cases we want to update to the new insertion point)
2106 * - if we recursed into a STRUCT then we didn't know in advance
2107 * what the types in the struct would be; so we have to fill in
2108 * that information now.
2109 * writer->type_pos = sub->type_pos
2111 * - if we recursed into anything else, we knew the full array
2112 * type, or knew the single typecode marking VARIANT, so
2113 * writer->type_pos is already correct.
2114 * writer->type_pos should remain as-is
2116 * - note that the parent is never an ARRAY or VARIANT, if it were
2117 * then type_pos_is_expectation would be TRUE. The parent
2118 * is thus known to be a toplevel or STRUCT.
2120 * Cases where writer->type_pos_is_expectation:
2121 * (in these cases we want to update to next expected type to write)
2123 * - we recursed from STRUCT into STRUCT and we didn't increment
2124 * type_pos in the parent just to stay consistent with the
2125 * !writer->type_pos_is_expectation case (though we could
2126 * special-case this in recurse_struct instead if we wanted)
2127 * writer->type_pos = sub->type_pos
2129 * - we recursed from STRUCT into ARRAY or VARIANT and type_pos
2130 * for parent should have been incremented already
2131 * writer->type_pos should remain as-is
2133 * - we recursed from ARRAY into a sub-element, so type_pos in the
2134 * parent is the element type and should remain the element type
2135 * for the benefit of the next child element
2136 * writer->type_pos should remain as-is
2138 * - we recursed from VARIANT into its value, so type_pos in the
2139 * parent makes no difference since there's only one value
2140 * and we just finished writing it and won't use type_pos again
2141 * writer->type_pos should remain as-is
2143 if (writer->type_str != NULL)
2145 if (sub->container_type == DBUS_TYPE_STRUCT &&
2146 (writer->container_type == DBUS_TYPE_STRUCT ||
2147 writer->container_type == DBUS_TYPE_INVALID))
2149 /* Advance the parent to the next struct field */
2150 writer->type_pos = sub->type_pos;
2154 writer->value_pos = sub->value_pos;
2156 #if RECURSIVE_MARSHAL_WRITE_TRACE
2157 _dbus_verbose (" type writer %p unrecursed type_pos = %d value_pos = %d remaining sig '%s'\n",
2158 writer, writer->type_pos, writer->value_pos,
2160 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0) :
2168 _dbus_type_writer_write_basic (DBusTypeWriter *writer,
2174 /* First ensure that our type realloc will succeed */
2175 if (!writer->type_pos_is_expectation && writer->type_str != NULL)
2177 if (!_dbus_string_alloc_space (writer->type_str, 1))
2183 if (!_dbus_type_writer_write_basic_no_typecode (writer, type, value))
2186 if (!write_or_verify_typecode (writer, type))
2187 _dbus_assert_not_reached ("failed to write typecode after prealloc");
2192 #if RECURSIVE_MARSHAL_WRITE_TRACE
2193 _dbus_verbose (" type writer %p basic type_pos = %d value_pos = %d is_expectation = %d enabled = %d\n",
2194 writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
2202 * Writes a block of fixed-length basic values, i.e. those that are
2203 * both _dbus_type_is_fixed() and _dbus_type_is_basic(). The block
2204 * must be written inside an array.
2206 * The value parameter should be the address of said array of values,
2207 * so e.g. if it's an array of double, pass in "const double**"
2209 * @param writer the writer
2210 * @param element_type type of stuff in the array
2211 * @param value address of the array
2212 * @param n_elements number of elements in the array
2213 * @returns #FALSE if no memory
2216 _dbus_type_writer_write_fixed_multi (DBusTypeWriter *writer,
2221 _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY);
2222 _dbus_assert (_dbus_type_is_fixed (element_type));
2223 _dbus_assert (writer->type_pos_is_expectation);
2224 _dbus_assert (n_elements >= 0);
2226 #if RECURSIVE_MARSHAL_WRITE_TRACE
2227 _dbus_verbose (" type writer %p entering fixed multi type_pos = %d value_pos = %d n_elements %d\n",
2228 writer, writer->type_pos, writer->value_pos, n_elements);
2231 if (!write_or_verify_typecode (writer, element_type))
2232 _dbus_assert_not_reached ("OOM should not happen if only verifying typecode");
2234 if (writer->enabled)
2236 if (!_dbus_marshal_write_fixed_multi (writer->value_str,
2242 &writer->value_pos))
2246 #if RECURSIVE_MARSHAL_WRITE_TRACE
2247 _dbus_verbose (" type writer %p fixed multi written new type_pos = %d new value_pos = %d n_elements %d\n",
2248 writer, writer->type_pos, writer->value_pos, n_elements);
2255 enable_if_after (DBusTypeWriter *writer,
2256 DBusTypeReader *reader,
2257 const DBusTypeReader *start_after)
2261 if (!writer->enabled && _dbus_type_reader_greater_than (reader, start_after))
2263 _dbus_type_writer_set_enabled (writer, TRUE);
2264 #if RECURSIVE_MARSHAL_WRITE_TRACE
2265 _dbus_verbose ("ENABLING writer %p at %d because reader at value_pos %d is after reader at value_pos %d\n",
2266 writer, writer->value_pos, reader->value_pos, start_after->value_pos);
2270 _dbus_assert ((!writer->enabled && !_dbus_type_reader_greater_than (reader, start_after)) ||
2271 (writer->enabled && _dbus_type_reader_greater_than (reader, start_after)));
2276 append_fixup (DBusList **fixups,
2277 const DBusArrayLenFixup *fixup)
2279 DBusArrayLenFixup *f;
2281 f = dbus_new (DBusArrayLenFixup, 1);
2287 if (!_dbus_list_append (fixups, f))
2293 _dbus_assert (f->len_pos_in_reader == fixup->len_pos_in_reader);
2294 _dbus_assert (f->new_len == fixup->new_len);
2299 /* This loop is trivial if you ignore all the start_after nonsense,
2300 * so if you're trying to figure it out, start by ignoring that
2303 writer_write_reader_helper (DBusTypeWriter *writer,
2304 DBusTypeReader *reader,
2305 const DBusTypeReader *start_after,
2306 int start_after_new_pos,
2307 int start_after_new_len,
2309 dbus_bool_t inside_start_after)
2313 while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
2315 if (_dbus_type_is_container (current_type))
2317 DBusTypeReader subreader;
2318 DBusTypeWriter subwriter;
2319 const DBusString *sig_str;
2322 dbus_bool_t enabled_at_recurse;
2323 dbus_bool_t past_start_after;
2324 int reader_array_len_pos;
2325 int reader_array_start_pos;
2326 dbus_bool_t this_is_start_after;
2328 /* type_pos is checked since e.g. in a struct the struct
2329 * and its first field have the same value_pos.
2330 * type_str will differ in reader/start_after for variants
2331 * where type_str is inside the value_str
2333 if (!inside_start_after && start_after &&
2334 reader->value_pos == start_after->value_pos &&
2335 reader->type_str == start_after->type_str &&
2336 reader->type_pos == start_after->type_pos)
2337 this_is_start_after = TRUE;
2339 this_is_start_after = FALSE;
2341 _dbus_type_reader_recurse (reader, &subreader);
2343 if (current_type == DBUS_TYPE_ARRAY)
2345 reader_array_len_pos = ARRAY_READER_LEN_POS (&subreader);
2346 reader_array_start_pos = subreader.u.array.start_pos;
2351 reader_array_len_pos = -1;
2352 reader_array_start_pos = -1;
2355 _dbus_type_reader_get_signature (&subreader, &sig_str,
2356 &sig_start, &sig_len);
2358 #if RECURSIVE_MARSHAL_WRITE_TRACE
2359 _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",
2360 _dbus_type_to_string (current_type),
2362 subreader.value_pos,
2364 start_after ? start_after->value_pos : -1,
2365 _dbus_string_get_length (writer->value_str),
2366 inside_start_after, this_is_start_after);
2369 if (!inside_start_after && !this_is_start_after)
2370 enable_if_after (writer, &subreader, start_after);
2371 enabled_at_recurse = writer->enabled;
2372 if (!_dbus_type_writer_recurse_contained_len (writer, current_type,
2373 sig_str, sig_start, sig_len,
2377 #if RECURSIVE_MARSHAL_WRITE_TRACE
2378 _dbus_verbose ("recursed into subwriter at %d write target len %d\n",
2379 subwriter.value_pos,
2380 _dbus_string_get_length (subwriter.value_str));
2383 if (!writer_write_reader_helper (&subwriter, &subreader, start_after,
2384 start_after_new_pos, start_after_new_len,
2386 inside_start_after ||
2387 this_is_start_after))
2390 #if RECURSIVE_MARSHAL_WRITE_TRACE
2391 _dbus_verbose ("about to unrecurse from %s subreader at %d writer at %d subwriter at %d write target len %d\n",
2392 _dbus_type_to_string (current_type),
2393 subreader.value_pos,
2395 subwriter.value_pos,
2396 _dbus_string_get_length (writer->value_str));
2399 if (!inside_start_after && !this_is_start_after)
2400 enable_if_after (writer, &subreader, start_after);
2401 past_start_after = writer->enabled;
2402 if (!_dbus_type_writer_unrecurse (writer, &subwriter))
2405 /* If we weren't enabled when we recursed, we didn't
2406 * write an array len; if we passed start_after
2407 * somewhere inside the array, then we need to generate
2410 if (start_after != NULL &&
2411 !enabled_at_recurse && past_start_after &&
2412 current_type == DBUS_TYPE_ARRAY &&
2415 DBusArrayLenFixup fixup;
2416 int bytes_written_after_start_after;
2417 int bytes_before_start_after;
2420 /* this subwriter access is moderately unkosher since we
2421 * already unrecursed, but it works as long as unrecurse
2422 * doesn't break us on purpose
2424 bytes_written_after_start_after = writer_get_array_len (&subwriter);
2426 bytes_before_start_after =
2427 start_after->value_pos - reader_array_start_pos;
2429 fixup.len_pos_in_reader = reader_array_len_pos;
2431 bytes_before_start_after +
2432 start_after_new_len +
2433 bytes_written_after_start_after;
2435 old_len = _dbus_marshal_read_uint32 (reader->value_str,
2436 fixup.len_pos_in_reader,
2437 reader->byte_order, NULL);
2439 if (old_len != fixup.new_len && !append_fixup (fixups, &fixup))
2442 #if RECURSIVE_MARSHAL_WRITE_TRACE
2443 _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",
2444 fixup.len_pos_in_reader,
2446 reader_array_start_pos,
2447 start_after->value_pos,
2448 bytes_before_start_after,
2449 start_after_new_len,
2450 bytes_written_after_start_after);
2458 _dbus_assert (_dbus_type_is_basic (current_type));
2460 #if RECURSIVE_MARSHAL_WRITE_TRACE
2461 _dbus_verbose ("Reading basic value %s at %d\n",
2462 _dbus_type_to_string (current_type),
2466 _dbus_type_reader_read_basic (reader, &val);
2468 #if RECURSIVE_MARSHAL_WRITE_TRACE
2469 _dbus_verbose ("Writing basic value %s at %d write target len %d inside_start_after = %d\n",
2470 _dbus_type_to_string (current_type),
2472 _dbus_string_get_length (writer->value_str),
2473 inside_start_after);
2475 if (!inside_start_after)
2476 enable_if_after (writer, reader, start_after);
2477 if (!_dbus_type_writer_write_basic (writer, current_type, &val))
2479 #if RECURSIVE_MARSHAL_WRITE_TRACE
2480 _dbus_verbose ("Wrote basic value %s, new value_pos %d write target len %d\n",
2481 _dbus_type_to_string (current_type),
2483 _dbus_string_get_length (writer->value_str));
2487 _dbus_type_reader_next (reader);
2494 apply_and_free_fixups (fixups, NULL); /* NULL for reader to apply to */
2500 * Iterate through all values in the given reader, writing a copy of
2501 * each value to the writer. The reader will be moved forward to its
2504 * If a reader start_after is provided, it should be a reader for the
2505 * same data as the reader to be written. Only values occurring after
2506 * the value pointed to by start_after will be written to the writer.
2508 * If start_after is provided, then the copy of the reader will be
2509 * partial. This means that array lengths will not have been copied.
2510 * The assumption is that you wrote a new version of the value at
2511 * start_after to the writer. You have to pass in the start position
2512 * and length of the new value. (If you are deleting the value
2513 * at start_after, pass in 0 for the length.)
2515 * If the fixups parameter is non-#NULL, then any array length that
2516 * was read but not written due to start_after will be provided
2517 * as a #DBusArrayLenFixup. The fixup contains the position of the
2518 * array length in the source data, and the correct array length
2519 * assuming you combine the source data before start_after with
2520 * the written data at start_after and beyond.
2522 * @param writer the writer to copy to
2523 * @param reader the reader to copy from
2524 * @param start_after #NULL or a reader showing where to start
2525 * @param start_after_new_pos the position of start_after equivalent in the target data
2526 * @param start_after_new_len the length of start_after equivalent in the target data
2527 * @param fixups list to append #DBusArrayLenFixup if the write was partial
2528 * @returns #FALSE if no memory
2531 _dbus_type_writer_write_reader_partial (DBusTypeWriter *writer,
2532 DBusTypeReader *reader,
2533 const DBusTypeReader *start_after,
2534 int start_after_new_pos,
2535 int start_after_new_len,
2538 DBusTypeWriter orig;
2545 orig_type_len = _dbus_string_get_length (writer->type_str);
2546 orig_value_len = _dbus_string_get_length (writer->value_str);
2547 orig_enabled = writer->enabled;
2550 _dbus_type_writer_set_enabled (writer, FALSE);
2552 if (!writer_write_reader_helper (writer, reader, start_after,
2553 start_after_new_pos,
2554 start_after_new_len,
2558 _dbus_type_writer_set_enabled (writer, orig_enabled);
2562 if (!writer->type_pos_is_expectation)
2564 new_bytes = _dbus_string_get_length (writer->type_str) - orig_type_len;
2565 _dbus_string_delete (writer->type_str, orig.type_pos, new_bytes);
2567 new_bytes = _dbus_string_get_length (writer->value_str) - orig_value_len;
2568 _dbus_string_delete (writer->value_str, orig.value_pos, new_bytes);
2576 * Iterate through all values in the given reader, writing a copy of
2577 * each value to the writer. The reader will be moved forward to its
2580 * @param writer the writer to copy to
2581 * @param reader the reader to copy from
2582 * @returns #FALSE if no memory
2585 _dbus_type_writer_write_reader (DBusTypeWriter *writer,
2586 DBusTypeReader *reader)
2588 return _dbus_type_writer_write_reader_partial (writer, reader, NULL, 0, 0, NULL);
2592 * If disabled, a writer can still be iterated forward and recursed/unrecursed
2593 * but won't write any values. Types will still be written unless the
2594 * writer is a "values only" writer, because the writer needs access to
2595 * a valid signature to be able to iterate.
2597 * @param writer the type writer
2598 * @param enabled #TRUE if values should be written
2601 _dbus_type_writer_set_enabled (DBusTypeWriter *writer,
2602 dbus_bool_t enabled)
2604 writer->enabled = enabled != FALSE;
2607 /** @} */ /* end of DBusMarshal group */
2609 #ifdef DBUS_BUILD_TESTS
2610 #include "dbus-test.h"
2611 #include "dbus-list.h"
2615 /* Whether to do the OOM stuff (only with other expensive tests) */
2616 #define TEST_OOM_HANDLING 0
2617 /* We do start offset 0 through 9, to get various alignment cases. Still this
2618 * obviously makes the test suite run 10x as slow.
2620 #define MAX_INITIAL_OFFSET 9
2622 /* Largest iteration count to test copying, realignment,
2623 * etc. with. i.e. we only test this stuff with some of the smaller
2626 #define MAX_ITERATIONS_FOR_EXPENSIVE_TESTS 1000
2632 DBusString signature;
2642 #define N_FENCE_BYTES 5
2643 #define FENCE_BYTES_STR "abcde"
2644 #define INITIAL_PADDING_BYTE '\0'
2647 data_block_init (DataBlock *block,
2651 if (!_dbus_string_init (&block->signature))
2654 if (!_dbus_string_init (&block->body))
2656 _dbus_string_free (&block->signature);
2660 if (!_dbus_string_insert_bytes (&block->signature, 0, initial_offset,
2661 INITIAL_PADDING_BYTE) ||
2662 !_dbus_string_insert_bytes (&block->body, 0, initial_offset,
2663 INITIAL_PADDING_BYTE) ||
2664 !_dbus_string_append (&block->signature, FENCE_BYTES_STR) ||
2665 !_dbus_string_append (&block->body, FENCE_BYTES_STR))
2667 _dbus_string_free (&block->signature);
2668 _dbus_string_free (&block->body);
2672 block->byte_order = byte_order;
2673 block->initial_offset = initial_offset;
2679 data_block_save (DataBlock *block,
2680 DataBlockState *state)
2682 state->saved_sig_len = _dbus_string_get_length (&block->signature) - N_FENCE_BYTES;
2683 state->saved_body_len = _dbus_string_get_length (&block->body) - N_FENCE_BYTES;
2687 data_block_restore (DataBlock *block,
2688 DataBlockState *state)
2690 _dbus_string_delete (&block->signature,
2691 state->saved_sig_len,
2692 _dbus_string_get_length (&block->signature) - state->saved_sig_len - N_FENCE_BYTES);
2693 _dbus_string_delete (&block->body,
2694 state->saved_body_len,
2695 _dbus_string_get_length (&block->body) - state->saved_body_len - N_FENCE_BYTES);
2699 data_block_verify (DataBlock *block)
2701 if (!_dbus_string_ends_with_c_str (&block->signature,
2706 offset = _dbus_string_get_length (&block->signature) - N_FENCE_BYTES - 8;
2710 _dbus_verbose_bytes_of_string (&block->signature,
2712 _dbus_string_get_length (&block->signature) - offset);
2713 _dbus_assert_not_reached ("block did not verify: bad bytes at end of signature");
2715 if (!_dbus_string_ends_with_c_str (&block->body,
2720 offset = _dbus_string_get_length (&block->body) - N_FENCE_BYTES - 8;
2724 _dbus_verbose_bytes_of_string (&block->body,
2726 _dbus_string_get_length (&block->body) - offset);
2727 _dbus_assert_not_reached ("block did not verify: bad bytes at end of body");
2730 _dbus_assert (_dbus_string_validate_nul (&block->signature,
2731 0, block->initial_offset));
2732 _dbus_assert (_dbus_string_validate_nul (&block->body,
2733 0, block->initial_offset));
2737 data_block_free (DataBlock *block)
2739 data_block_verify (block);
2741 _dbus_string_free (&block->signature);
2742 _dbus_string_free (&block->body);
2746 data_block_reset (DataBlock *block)
2748 data_block_verify (block);
2750 _dbus_string_delete (&block->signature,
2751 block->initial_offset,
2752 _dbus_string_get_length (&block->signature) - N_FENCE_BYTES - block->initial_offset);
2753 _dbus_string_delete (&block->body,
2754 block->initial_offset,
2755 _dbus_string_get_length (&block->body) - N_FENCE_BYTES - block->initial_offset);
2757 data_block_verify (block);
2761 data_block_init_reader_writer (DataBlock *block,
2762 DBusTypeReader *reader,
2763 DBusTypeWriter *writer)
2766 _dbus_type_reader_init (reader,
2769 block->initial_offset,
2771 block->initial_offset);
2774 _dbus_type_writer_init (writer,
2777 _dbus_string_get_length (&block->signature) - N_FENCE_BYTES,
2779 _dbus_string_get_length (&block->body) - N_FENCE_BYTES);
2783 real_check_expected_type (DBusTypeReader *reader,
2785 const char *funcname,
2790 t = _dbus_type_reader_get_current_type (reader);
2794 _dbus_warn ("Read type %s while expecting %s at %s line %d\n",
2795 _dbus_type_to_string (t),
2796 _dbus_type_to_string (expected),
2799 _dbus_assert_not_reached ("read wrong type");
2803 #define check_expected_type(reader, expected) real_check_expected_type (reader, expected, _DBUS_FUNCTION_NAME, __LINE__)
2805 #define NEXT_EXPECTING_TRUE(reader) do { if (!_dbus_type_reader_next (reader)) \
2807 _dbus_warn ("_dbus_type_reader_next() should have returned TRUE at %s %d\n", \
2808 _DBUS_FUNCTION_NAME, __LINE__); \
2809 _dbus_assert_not_reached ("test failed"); \
2813 #define NEXT_EXPECTING_FALSE(reader) do { if (_dbus_type_reader_next (reader)) \
2815 _dbus_warn ("_dbus_type_reader_next() should have returned FALSE at %s %d\n", \
2816 _DBUS_FUNCTION_NAME, __LINE__); \
2817 _dbus_assert_not_reached ("test failed"); \
2819 check_expected_type (reader, DBUS_TYPE_INVALID); \
2822 typedef struct TestTypeNode TestTypeNode;
2823 typedef struct TestTypeNodeClass TestTypeNodeClass;
2824 typedef struct TestTypeNodeContainer TestTypeNodeContainer;
2825 typedef struct TestTypeNodeContainerClass TestTypeNodeContainerClass;
2829 const TestTypeNodeClass *klass;
2832 struct TestTypeNodeContainer
2838 struct TestTypeNodeClass
2844 int subclass_detail; /* a bad hack to avoid a bunch of subclass casting */
2846 dbus_bool_t (* construct) (TestTypeNode *node);
2847 void (* destroy) (TestTypeNode *node);
2849 dbus_bool_t (* write_value) (TestTypeNode *node,
2851 DBusTypeWriter *writer,
2853 dbus_bool_t (* read_value) (TestTypeNode *node,
2854 DBusTypeReader *reader,
2856 dbus_bool_t (* set_value) (TestTypeNode *node,
2857 DBusTypeReader *reader,
2858 DBusTypeReader *realign_root,
2860 dbus_bool_t (* build_signature) (TestTypeNode *node,
2862 dbus_bool_t (* write_multi) (TestTypeNode *node,
2864 DBusTypeWriter *writer,
2867 dbus_bool_t (* read_multi) (TestTypeNode *node,
2868 DBusTypeReader *reader,
2873 struct TestTypeNodeContainerClass
2875 TestTypeNodeClass base;
2878 /* FIXME this could be chilled out substantially by unifying
2879 * the basic types into basic_write_value/basic_read_value
2880 * and by merging read_value and set_value into one function
2881 * taking a flag argument.
2883 static dbus_bool_t int32_write_value (TestTypeNode *node,
2885 DBusTypeWriter *writer,
2887 static dbus_bool_t int32_read_value (TestTypeNode *node,
2888 DBusTypeReader *reader,
2890 static dbus_bool_t int32_set_value (TestTypeNode *node,
2891 DBusTypeReader *reader,
2892 DBusTypeReader *realign_root,
2894 static dbus_bool_t int32_write_multi (TestTypeNode *node,
2896 DBusTypeWriter *writer,
2899 static dbus_bool_t int32_read_multi (TestTypeNode *node,
2900 DBusTypeReader *reader,
2903 static dbus_bool_t int64_write_value (TestTypeNode *node,
2905 DBusTypeWriter *writer,
2907 static dbus_bool_t int64_read_value (TestTypeNode *node,
2908 DBusTypeReader *reader,
2910 static dbus_bool_t int64_set_value (TestTypeNode *node,
2911 DBusTypeReader *reader,
2912 DBusTypeReader *realign_root,
2914 static dbus_bool_t string_write_value (TestTypeNode *node,
2916 DBusTypeWriter *writer,
2918 static dbus_bool_t string_read_value (TestTypeNode *node,
2919 DBusTypeReader *reader,
2921 static dbus_bool_t string_set_value (TestTypeNode *node,
2922 DBusTypeReader *reader,
2923 DBusTypeReader *realign_root,
2925 static dbus_bool_t bool_write_value (TestTypeNode *node,
2927 DBusTypeWriter *writer,
2929 static dbus_bool_t bool_read_value (TestTypeNode *node,
2930 DBusTypeReader *reader,
2932 static dbus_bool_t bool_set_value (TestTypeNode *node,
2933 DBusTypeReader *reader,
2934 DBusTypeReader *realign_root,
2936 static dbus_bool_t byte_write_value (TestTypeNode *node,
2938 DBusTypeWriter *writer,
2940 static dbus_bool_t byte_read_value (TestTypeNode *node,
2941 DBusTypeReader *reader,
2943 static dbus_bool_t byte_set_value (TestTypeNode *node,
2944 DBusTypeReader *reader,
2945 DBusTypeReader *realign_root,
2947 static dbus_bool_t double_write_value (TestTypeNode *node,
2949 DBusTypeWriter *writer,
2951 static dbus_bool_t double_read_value (TestTypeNode *node,
2952 DBusTypeReader *reader,
2954 static dbus_bool_t double_set_value (TestTypeNode *node,
2955 DBusTypeReader *reader,
2956 DBusTypeReader *realign_root,
2958 static dbus_bool_t object_path_write_value (TestTypeNode *node,
2960 DBusTypeWriter *writer,
2962 static dbus_bool_t object_path_read_value (TestTypeNode *node,
2963 DBusTypeReader *reader,
2965 static dbus_bool_t object_path_set_value (TestTypeNode *node,
2966 DBusTypeReader *reader,
2967 DBusTypeReader *realign_root,
2969 static dbus_bool_t signature_write_value (TestTypeNode *node,
2971 DBusTypeWriter *writer,
2973 static dbus_bool_t signature_read_value (TestTypeNode *node,
2974 DBusTypeReader *reader,
2976 static dbus_bool_t signature_set_value (TestTypeNode *node,
2977 DBusTypeReader *reader,
2978 DBusTypeReader *realign_root,
2980 static dbus_bool_t struct_write_value (TestTypeNode *node,
2982 DBusTypeWriter *writer,
2984 static dbus_bool_t struct_read_value (TestTypeNode *node,
2985 DBusTypeReader *reader,
2987 static dbus_bool_t struct_set_value (TestTypeNode *node,
2988 DBusTypeReader *reader,
2989 DBusTypeReader *realign_root,
2991 static dbus_bool_t struct_build_signature (TestTypeNode *node,
2993 static dbus_bool_t array_write_value (TestTypeNode *node,
2995 DBusTypeWriter *writer,
2997 static dbus_bool_t array_read_value (TestTypeNode *node,
2998 DBusTypeReader *reader,
3000 static dbus_bool_t array_set_value (TestTypeNode *node,
3001 DBusTypeReader *reader,
3002 DBusTypeReader *realign_root,
3004 static dbus_bool_t array_build_signature (TestTypeNode *node,
3006 static dbus_bool_t variant_write_value (TestTypeNode *node,
3008 DBusTypeWriter *writer,
3010 static dbus_bool_t variant_read_value (TestTypeNode *node,
3011 DBusTypeReader *reader,
3013 static dbus_bool_t variant_set_value (TestTypeNode *node,
3014 DBusTypeReader *reader,
3015 DBusTypeReader *realign_root,
3017 static void container_destroy (TestTypeNode *node);
3020 static const TestTypeNodeClass int32_class = {
3022 sizeof (TestTypeNode),
3034 static const TestTypeNodeClass uint32_class = {
3036 sizeof (TestTypeNode),
3040 int32_write_value, /* recycle from int32 */
3041 int32_read_value, /* recycle from int32 */
3042 int32_set_value, /* recycle from int32 */
3044 int32_write_multi, /* recycle from int32 */
3045 int32_read_multi /* recycle from int32 */
3048 static const TestTypeNodeClass int64_class = {
3050 sizeof (TestTypeNode),
3062 static const TestTypeNodeClass uint64_class = {
3064 sizeof (TestTypeNode),
3068 int64_write_value, /* recycle from int64 */
3069 int64_read_value, /* recycle from int64 */
3070 int64_set_value, /* recycle from int64 */
3076 static const TestTypeNodeClass string_0_class = {
3078 sizeof (TestTypeNode),
3079 0, /* string length */
3090 static const TestTypeNodeClass string_1_class = {
3092 sizeof (TestTypeNode),
3093 1, /* string length */
3104 /* with nul, a len 3 string should fill 4 bytes and thus is "special" */
3105 static const TestTypeNodeClass string_3_class = {
3107 sizeof (TestTypeNode),
3108 3, /* string length */
3119 /* with nul, a len 8 string should fill 9 bytes and thus is "special" (far-fetched I suppose) */
3120 static const TestTypeNodeClass string_8_class = {
3122 sizeof (TestTypeNode),
3123 8, /* string length */
3134 static const TestTypeNodeClass bool_class = {
3136 sizeof (TestTypeNode),
3148 static const TestTypeNodeClass byte_class = {
3150 sizeof (TestTypeNode),
3162 static const TestTypeNodeClass double_class = {
3164 sizeof (TestTypeNode),
3176 static const TestTypeNodeClass object_path_class = {
3177 DBUS_TYPE_OBJECT_PATH,
3178 sizeof (TestTypeNode),
3182 object_path_write_value,
3183 object_path_read_value,
3184 object_path_set_value,
3190 static const TestTypeNodeClass signature_class = {
3191 DBUS_TYPE_SIGNATURE,
3192 sizeof (TestTypeNode),
3196 signature_write_value,
3197 signature_read_value,
3198 signature_set_value,
3204 static const TestTypeNodeClass struct_1_class = {
3206 sizeof (TestTypeNodeContainer),
3207 1, /* number of times children appear as fields */
3213 struct_build_signature,
3218 static const TestTypeNodeClass struct_2_class = {
3220 sizeof (TestTypeNodeContainer),
3221 2, /* number of times children appear as fields */
3227 struct_build_signature,
3232 static dbus_bool_t arrays_write_fixed_in_blocks = FALSE;
3234 static const TestTypeNodeClass array_0_class = {
3236 sizeof (TestTypeNodeContainer),
3237 0, /* number of array elements */
3243 array_build_signature,
3248 static const TestTypeNodeClass array_1_class = {
3250 sizeof (TestTypeNodeContainer),
3251 1, /* number of array elements */
3257 array_build_signature,
3262 static const TestTypeNodeClass array_2_class = {
3264 sizeof (TestTypeNodeContainer),
3265 2, /* number of array elements */
3271 array_build_signature,
3276 static const TestTypeNodeClass array_9_class = {
3278 sizeof (TestTypeNodeContainer),
3279 9, /* number of array elements */
3285 array_build_signature,
3290 static const TestTypeNodeClass variant_class = {
3292 sizeof (TestTypeNodeContainer),
3296 variant_write_value,
3304 static const TestTypeNodeClass* const
3320 #define N_BASICS (_DBUS_N_ELEMENTS (basic_nodes))
3322 static const TestTypeNodeClass* const
3323 container_nodes[] = {
3330 /* array_9_class is omitted on purpose, it's too slow;
3331 * we only use it in one hardcoded test below
3334 #define N_CONTAINERS (_DBUS_N_ELEMENTS (container_nodes))
3336 static TestTypeNode*
3337 node_new (const TestTypeNodeClass *klass)
3341 node = dbus_malloc0 (klass->instance_size);
3345 node->klass = klass;
3347 if (klass->construct)
3349 if (!(* klass->construct) (node))
3360 node_destroy (TestTypeNode *node)
3362 if (node->klass->destroy)
3363 (* node->klass->destroy) (node);
3368 node_write_value (TestTypeNode *node,
3370 DBusTypeWriter *writer,
3375 retval = (* node->klass->write_value) (node, block, writer, seed);
3378 /* Handy to see where things break, but too expensive to do all the time */
3379 data_block_verify (block);
3386 node_read_value (TestTypeNode *node,
3387 DBusTypeReader *reader,
3391 DBusTypeReader restored;
3393 _dbus_type_reader_save_mark (reader, &mark);
3395 if (!(* node->klass->read_value) (node, reader, seed))
3398 _dbus_type_reader_init_from_mark (&restored,
3404 if (!(* node->klass->read_value) (node, &restored, seed))
3410 /* Warning: if this one fails due to OOM, it has side effects (can
3411 * modify only some of the sub-values). OK in a test suite, but we
3412 * never do this in real code.
3415 node_set_value (TestTypeNode *node,
3416 DBusTypeReader *reader,
3417 DBusTypeReader *realign_root,
3420 if (!(* node->klass->set_value) (node, reader, realign_root, seed))
3427 node_build_signature (TestTypeNode *node,
3430 if (node->klass->build_signature)
3431 return (* node->klass->build_signature) (node, str);
3433 return _dbus_string_append_byte (str, node->klass->typecode);
3437 node_append_child (TestTypeNode *node,
3438 TestTypeNode *child)
3440 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3442 _dbus_assert (node->klass->instance_size >= (int) sizeof (TestTypeNodeContainer));
3444 if (!_dbus_list_append (&container->children, child))
3445 _dbus_assert_not_reached ("no memory"); /* we never check the return value on node_append_child anyhow - it's run from outside the malloc-failure test code */
3451 node_write_multi (TestTypeNode *node,
3453 DBusTypeWriter *writer,
3459 _dbus_assert (node->klass->write_multi != NULL);
3460 retval = (* node->klass->write_multi) (node, block, writer, seed, n_copies);
3463 /* Handy to see where things break, but too expensive to do all the time */
3464 data_block_verify (block);
3471 node_read_multi (TestTypeNode *node,
3472 DBusTypeReader *reader,
3476 _dbus_assert (node->klass->read_multi != NULL);
3478 if (!(* node->klass->read_multi) (node, reader, seed, n_copies))
3484 static int n_iterations_completed_total = 0;
3485 static int n_iterations_completed_this_test = 0;
3486 static int n_iterations_expected_this_test = 0;
3490 const DBusString *signature;
3493 TestTypeNode **nodes;
3495 } NodeIterationData;
3499 run_test_copy (NodeIterationData *nid)
3504 DBusTypeReader reader;
3505 DBusTypeWriter writer;
3507 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
3513 if (!data_block_init (&dest, src->byte_order, src->initial_offset))
3516 data_block_init_reader_writer (src, &reader, NULL);
3517 data_block_init_reader_writer (&dest, NULL, &writer);
3519 /* DBusTypeWriter assumes it's writing into an existing signature,
3520 * so doesn't add nul on its own. We have to do that.
3522 if (!_dbus_string_insert_byte (&dest.signature,
3523 dest.initial_offset, '\0'))
3526 if (!_dbus_type_writer_write_reader (&writer, &reader))
3529 /* Data blocks should now be identical */
3530 if (!_dbus_string_equal (&src->signature, &dest.signature))
3532 _dbus_verbose ("SOURCE\n");
3533 _dbus_verbose_bytes_of_string (&src->signature, 0,
3534 _dbus_string_get_length (&src->signature));
3535 _dbus_verbose ("DEST\n");
3536 _dbus_verbose_bytes_of_string (&dest.signature, 0,
3537 _dbus_string_get_length (&dest.signature));
3538 _dbus_assert_not_reached ("signatures did not match");
3541 if (!_dbus_string_equal (&src->body, &dest.body))
3543 _dbus_verbose ("SOURCE\n");
3544 _dbus_verbose_bytes_of_string (&src->body, 0,
3545 _dbus_string_get_length (&src->body));
3546 _dbus_verbose ("DEST\n");
3547 _dbus_verbose_bytes_of_string (&dest.body, 0,
3548 _dbus_string_get_length (&dest.body));
3549 _dbus_assert_not_reached ("bodies did not match");
3556 data_block_free (&dest);
3562 run_test_values_only_write (NodeIterationData *nid)
3564 DBusTypeReader reader;
3565 DBusTypeWriter writer;
3570 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
3574 data_block_reset (nid->block);
3576 sig_len = _dbus_string_get_length (nid->signature);
3578 _dbus_type_writer_init_values_only (&writer,
3579 nid->block->byte_order,
3582 _dbus_string_get_length (&nid->block->body) - N_FENCE_BYTES);
3583 _dbus_type_reader_init (&reader,
3584 nid->block->byte_order,
3587 nid->block->initial_offset);
3590 while (i < nid->n_nodes)
3592 if (!node_write_value (nid->nodes[i], nid->block, &writer, i))
3598 /* if we wrote any typecodes then this would fail */
3599 _dbus_assert (sig_len == _dbus_string_get_length (nid->signature));
3601 /* But be sure we wrote out the values correctly */
3603 while (i < nid->n_nodes)
3605 if (!node_read_value (nid->nodes[i], &reader, i))
3608 if (i + 1 == nid->n_nodes)
3609 NEXT_EXPECTING_FALSE (&reader);
3611 NEXT_EXPECTING_TRUE (&reader);
3619 data_block_reset (nid->block);
3623 /* offset the seed for setting, so we set different numbers than
3624 * we originally wrote. Don't offset by a huge number since in
3625 * some cases it's value = possibilities[seed % n_possibilities]
3626 * and we don't want to wrap around. bool_from_seed
3627 * is just seed % 2 even.
3631 run_test_set_values (NodeIterationData *nid)
3633 DBusTypeReader reader;
3634 DBusTypeReader realign_root;
3638 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
3642 data_block_init_reader_writer (nid->block,
3645 realign_root = reader;
3648 while (i < nid->n_nodes)
3650 if (!node_set_value (nid->nodes[i],
3651 &reader, &realign_root,
3655 if (i + 1 == nid->n_nodes)
3656 NEXT_EXPECTING_FALSE (&reader);
3658 NEXT_EXPECTING_TRUE (&reader);
3663 /* Check that the new values were set */
3665 reader = realign_root;
3668 while (i < nid->n_nodes)
3670 if (!node_read_value (nid->nodes[i], &reader,
3674 if (i + 1 == nid->n_nodes)
3675 NEXT_EXPECTING_FALSE (&reader);
3677 NEXT_EXPECTING_TRUE (&reader);
3689 run_test_delete_values (NodeIterationData *nid)
3691 DBusTypeReader reader;
3695 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
3699 data_block_init_reader_writer (nid->block,
3702 while ((t = _dbus_type_reader_get_current_type (&reader)) != DBUS_TYPE_INVALID)
3704 /* Right now, deleting only works on array elements. We delete
3705 * all array elements, and then verify that there aren't any
3708 if (t == DBUS_TYPE_ARRAY)
3710 DBusTypeReader array;
3714 _dbus_type_reader_recurse (&reader, &array);
3716 while (_dbus_type_reader_get_current_type (&array) != DBUS_TYPE_INVALID)
3719 _dbus_type_reader_next (&array);
3722 /* reset to start of array */
3723 _dbus_type_reader_recurse (&reader, &array);
3724 _dbus_verbose ("recursing into deletion loop reader.value_pos = %d array.value_pos = %d array.u.start_pos = %d\n",
3725 reader.value_pos, array.value_pos, array.u.array.start_pos);
3726 while ((elem_type = _dbus_type_reader_get_current_type (&array)) != DBUS_TYPE_INVALID)
3728 /* We don't want to always delete from the same part of the array. */
3729 static int cycle = 0;
3732 _dbus_assert (n_elements > 0);
3733 _dbus_assert (!_dbus_type_reader_array_is_empty (&reader));
3736 if (elem == 3 || elem >= n_elements) /* end of array */
3737 elem = n_elements - 1;
3739 _dbus_verbose ("deleting array element %d of %d type %s cycle %d reader pos %d elem pos %d\n",
3740 elem, n_elements, _dbus_type_to_string (elem_type),
3741 cycle, reader.value_pos, array.value_pos);
3744 if (!_dbus_type_reader_next (&array))
3745 _dbus_assert_not_reached ("should have had another element\n");
3749 if (!_dbus_type_reader_delete (&array, &reader))
3755 _dbus_type_reader_recurse (&reader, &array);
3763 _dbus_type_reader_next (&reader);
3766 /* Check that there are no array elements left */
3767 data_block_init_reader_writer (nid->block,
3770 while ((t = _dbus_type_reader_get_current_type (&reader)) != DBUS_TYPE_INVALID)
3772 if (t == DBUS_TYPE_ARRAY)
3773 _dbus_assert (_dbus_type_reader_array_is_empty (&reader));
3775 _dbus_type_reader_next (&reader);
3785 run_test_nodes_iteration (void *data)
3787 NodeIterationData *nid = data;
3788 DBusTypeReader reader;
3789 DBusTypeWriter writer;
3794 * 1. write the value
3795 * 2. strcmp-compare with the signature we built
3797 * 4. type-iterate the signature and the value and see if they are the same type-wise
3801 data_block_init_reader_writer (nid->block,
3804 /* DBusTypeWriter assumes it's writing into an existing signature,
3805 * so doesn't add nul on its own. We have to do that.
3807 if (!_dbus_string_insert_byte (&nid->block->signature,
3808 nid->type_offset, '\0'))
3812 while (i < nid->n_nodes)
3814 if (!node_write_value (nid->nodes[i], nid->block, &writer, i))
3820 if (!_dbus_string_equal_substring (nid->signature, 0, _dbus_string_get_length (nid->signature),
3821 &nid->block->signature, nid->type_offset))
3823 _dbus_warn ("Expected signature '%s' and got '%s' with initial offset %d\n",
3824 _dbus_string_get_const_data (nid->signature),
3825 _dbus_string_get_const_data_len (&nid->block->signature, nid->type_offset, 0),
3827 _dbus_assert_not_reached ("wrong signature");
3831 while (i < nid->n_nodes)
3833 if (!node_read_value (nid->nodes[i], &reader, i))
3836 if (i + 1 == nid->n_nodes)
3837 NEXT_EXPECTING_FALSE (&reader);
3839 NEXT_EXPECTING_TRUE (&reader);
3844 if (n_iterations_expected_this_test <= MAX_ITERATIONS_FOR_EXPENSIVE_TESTS)
3846 /* this set values test uses code from copy and
3847 * values_only_write so would ideally be last so you get a
3848 * simpler test case for problems with copying or values_only
3849 * writing; but it also needs an already-written DataBlock so it
3850 * has to go first. Comment it out if it breaks, and see if the
3851 * later tests also break - debug them first if so.
3853 if (!run_test_set_values (nid))
3856 if (!run_test_delete_values (nid))
3859 if (!run_test_copy (nid))
3862 if (!run_test_values_only_write (nid))
3866 /* FIXME type-iterate both signature and value and compare the resulting
3867 * tree to the node tree perhaps
3874 data_block_reset (nid->block);
3880 run_test_nodes_in_one_configuration (TestTypeNode **nodes,
3882 const DBusString *signature,
3887 NodeIterationData nid;
3889 if (!data_block_init (&block, byte_order, initial_offset))
3890 _dbus_assert_not_reached ("no memory");
3892 nid.signature = signature;
3894 nid.type_offset = initial_offset;
3896 nid.n_nodes = n_nodes;
3898 if (TEST_OOM_HANDLING &&
3899 n_iterations_expected_this_test <= MAX_ITERATIONS_FOR_EXPENSIVE_TESTS)
3901 _dbus_test_oom_handling ("running test node",
3902 run_test_nodes_iteration,
3907 if (!run_test_nodes_iteration (&nid))
3908 _dbus_assert_not_reached ("no memory");
3911 data_block_free (&block);
3915 run_test_nodes (TestTypeNode **nodes,
3919 DBusString signature;
3921 if (!_dbus_string_init (&signature))
3922 _dbus_assert_not_reached ("no memory");
3927 if (! node_build_signature (nodes[i], &signature))
3928 _dbus_assert_not_reached ("no memory");
3933 _dbus_verbose (">>> test nodes with signature '%s'\n",
3934 _dbus_string_get_const_data (&signature));
3937 while (i <= MAX_INITIAL_OFFSET)
3939 run_test_nodes_in_one_configuration (nodes, n_nodes, &signature,
3940 DBUS_LITTLE_ENDIAN, i);
3941 run_test_nodes_in_one_configuration (nodes, n_nodes, &signature,
3942 DBUS_BIG_ENDIAN, i);
3947 n_iterations_completed_this_test += 1;
3948 n_iterations_completed_total += 1;
3950 if (n_iterations_completed_this_test == n_iterations_expected_this_test)
3952 fprintf (stderr, " 100%% %d this test (%d cumulative)\n",
3953 n_iterations_completed_this_test,
3954 n_iterations_completed_total);
3956 /* this happens to turn out well with mod == 1 */
3957 else if ((n_iterations_completed_this_test %
3958 (int)(n_iterations_expected_this_test / 10.0)) == 1)
3960 fprintf (stderr, " %d%% ", (int) (n_iterations_completed_this_test / (double) n_iterations_expected_this_test * 100));
3963 _dbus_string_free (&signature);
3966 #define N_VALUES (N_BASICS * N_CONTAINERS + N_BASICS)
3968 static TestTypeNode*
3969 value_generator (int *ip)
3972 const TestTypeNodeClass *child_klass;
3973 const TestTypeNodeClass *container_klass;
3974 TestTypeNode *child;
3977 _dbus_assert (i <= N_VALUES);
3983 else if (i < N_BASICS)
3985 node = node_new (basic_nodes[i]);
3989 /* imagine an array:
3990 * container 0 of basic 0
3991 * container 0 of basic 1
3992 * container 0 of basic 2
3993 * container 1 of basic 0
3994 * container 1 of basic 1
3995 * container 1 of basic 2
3999 container_klass = container_nodes[i / N_BASICS];
4000 child_klass = basic_nodes[i % N_BASICS];
4002 node = node_new (container_klass);
4003 child = node_new (child_klass);
4005 node_append_child (node, child);
4008 *ip += 1; /* increment the generator */
4014 make_and_run_values_inside_container (const TestTypeNodeClass *container_klass,
4018 TestTypeNode *container;
4019 TestTypeNode *child;
4022 root = node_new (container_klass);
4024 for (i = 1; i < n_nested; i++)
4026 child = node_new (container_klass);
4027 node_append_child (container, child);
4031 /* container should now be the most-nested container */
4034 while ((child = value_generator (&i)))
4036 node_append_child (container, child);
4038 run_test_nodes (&root, 1);
4040 _dbus_list_clear (&((TestTypeNodeContainer*)container)->children);
4041 node_destroy (child);
4044 node_destroy (root);
4048 start_next_test (const char *format,
4051 n_iterations_completed_this_test = 0;
4052 n_iterations_expected_this_test = expected;
4054 fprintf (stderr, ">>> >>> ");
4055 fprintf (stderr, format,
4056 n_iterations_expected_this_test);
4060 make_and_run_test_nodes (void)
4064 /* We try to do this in order of "complicatedness" so that test
4065 * failures tend to show up in the simplest test case that
4066 * demonstrates the failure. There are also some tests that run
4067 * more than once for this reason, first while going through simple
4068 * cases, second while going through a broader range of complex
4071 /* Each basic node. The basic nodes should include:
4073 * - each fixed-size type (in such a way that it has different values each time,
4074 * so we can tell if we mix two of them up)
4075 * - strings of various lengths
4079 /* Each container node. The container nodes should include:
4081 * struct with 1 and 2 copies of the contained item
4082 * array with 0, 1, 2 copies of the contained item
4085 /* Let a "value" be a basic node, or a container containing a single basic node.
4086 * Let n_values be the number of such values i.e. (n_container * n_basic + n_basic)
4087 * When iterating through all values to make combinations, do the basic types
4088 * first and the containers second.
4090 /* Each item is shown with its number of iterations to complete so
4091 * we can keep a handle on this unit test
4094 /* FIXME test just an empty body, no types at all */
4096 start_next_test ("Each value by itself %d iterations\n", N_VALUES);
4100 while ((node = value_generator (&i)))
4102 run_test_nodes (&node, 1);
4104 node_destroy (node);
4108 start_next_test ("Each value by itself with arrays as blocks %d iterations\n", N_VALUES);
4109 arrays_write_fixed_in_blocks = TRUE;
4113 while ((node = value_generator (&i)))
4115 run_test_nodes (&node, 1);
4117 node_destroy (node);
4120 arrays_write_fixed_in_blocks = FALSE;
4122 start_next_test ("All values in one big toplevel %d iteration\n", 1);
4124 TestTypeNode *nodes[N_VALUES];
4127 while ((nodes[i] = value_generator (&i)))
4130 run_test_nodes (nodes, N_VALUES);
4132 for (i = 0; i < N_VALUES; i++)
4133 node_destroy (nodes[i]);
4136 start_next_test ("Each value,value pair combination as toplevel, in both orders %d iterations\n",
4137 N_VALUES * N_VALUES);
4139 TestTypeNode *nodes[2];
4142 while ((nodes[0] = value_generator (&i)))
4145 while ((nodes[1] = value_generator (&j)))
4147 run_test_nodes (nodes, 2);
4149 node_destroy (nodes[1]);
4152 node_destroy (nodes[0]);
4156 start_next_test ("Each container containing each value %d iterations\n",
4157 N_CONTAINERS * N_VALUES);
4158 for (i = 0; i < N_CONTAINERS; i++)
4160 const TestTypeNodeClass *container_klass = container_nodes[i];
4162 make_and_run_values_inside_container (container_klass, 1);
4165 start_next_test ("Each container containing each value with arrays as blocks %d iterations\n",
4166 N_CONTAINERS * N_VALUES);
4167 arrays_write_fixed_in_blocks = TRUE;
4168 for (i = 0; i < N_CONTAINERS; i++)
4170 const TestTypeNodeClass *container_klass = container_nodes[i];
4172 make_and_run_values_inside_container (container_klass, 1);
4174 arrays_write_fixed_in_blocks = FALSE;
4176 start_next_test ("Each container of same container of each value %d iterations\n",
4177 N_CONTAINERS * N_VALUES);
4178 for (i = 0; i < N_CONTAINERS; i++)
4180 const TestTypeNodeClass *container_klass = container_nodes[i];
4182 make_and_run_values_inside_container (container_klass, 2);
4185 start_next_test ("Each container of same container of same container of each value %d iterations\n",
4186 N_CONTAINERS * N_VALUES);
4187 for (i = 0; i < N_CONTAINERS; i++)
4189 const TestTypeNodeClass *container_klass = container_nodes[i];
4191 make_and_run_values_inside_container (container_klass, 3);
4194 start_next_test ("Each value,value pair inside a struct %d iterations\n",
4195 N_VALUES * N_VALUES);
4197 TestTypeNode *val1, *val2;
4200 node = node_new (&struct_1_class);
4203 while ((val1 = value_generator (&i)))
4206 while ((val2 = value_generator (&j)))
4208 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
4210 node_append_child (node, val1);
4211 node_append_child (node, val2);
4213 run_test_nodes (&node, 1);
4215 _dbus_list_clear (&container->children);
4216 node_destroy (val2);
4218 node_destroy (val1);
4220 node_destroy (node);
4223 start_next_test ("All values in one big struct %d iteration\n",
4227 TestTypeNode *child;
4229 node = node_new (&struct_1_class);
4232 while ((child = value_generator (&i)))
4233 node_append_child (node, child);
4235 run_test_nodes (&node, 1);
4237 node_destroy (node);
4240 start_next_test ("Each value in a large array %d iterations\n",
4246 node = node_new (&array_9_class);
4249 while ((val = value_generator (&i)))
4251 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
4253 node_append_child (node, val);
4255 run_test_nodes (&node, 1);
4257 _dbus_list_clear (&container->children);
4261 node_destroy (node);
4264 start_next_test ("Each container of each container of each value %d iterations\n",
4265 N_CONTAINERS * N_CONTAINERS * N_VALUES);
4266 for (i = 0; i < N_CONTAINERS; i++)
4268 const TestTypeNodeClass *outer_container_klass = container_nodes[i];
4269 TestTypeNode *outer_container = node_new (outer_container_klass);
4271 for (j = 0; j < N_CONTAINERS; j++)
4273 TestTypeNode *child;
4274 const TestTypeNodeClass *inner_container_klass = container_nodes[j];
4275 TestTypeNode *inner_container = node_new (inner_container_klass);
4277 node_append_child (outer_container, inner_container);
4280 while ((child = value_generator (&m)))
4282 node_append_child (inner_container, child);
4284 run_test_nodes (&outer_container, 1);
4286 _dbus_list_clear (&((TestTypeNodeContainer*)inner_container)->children);
4287 node_destroy (child);
4289 _dbus_list_clear (&((TestTypeNodeContainer*)outer_container)->children);
4290 node_destroy (inner_container);
4292 node_destroy (outer_container);
4295 start_next_test ("Each container of each container of each container of each value %d iterations\n",
4296 N_CONTAINERS * N_CONTAINERS * N_CONTAINERS * N_VALUES);
4297 for (i = 0; i < N_CONTAINERS; i++)
4299 const TestTypeNodeClass *outer_container_klass = container_nodes[i];
4300 TestTypeNode *outer_container = node_new (outer_container_klass);
4302 for (j = 0; j < N_CONTAINERS; j++)
4304 const TestTypeNodeClass *inner_container_klass = container_nodes[j];
4305 TestTypeNode *inner_container = node_new (inner_container_klass);
4307 node_append_child (outer_container, inner_container);
4309 for (k = 0; k < N_CONTAINERS; k++)
4311 TestTypeNode *child;
4312 const TestTypeNodeClass *center_container_klass = container_nodes[k];
4313 TestTypeNode *center_container = node_new (center_container_klass);
4315 node_append_child (inner_container, center_container);
4318 while ((child = value_generator (&m)))
4320 node_append_child (center_container, child);
4322 run_test_nodes (&outer_container, 1);
4324 _dbus_list_clear (&((TestTypeNodeContainer*)center_container)->children);
4325 node_destroy (child);
4327 _dbus_list_clear (&((TestTypeNodeContainer*)inner_container)->children);
4328 node_destroy (center_container);
4330 _dbus_list_clear (&((TestTypeNodeContainer*)outer_container)->children);
4331 node_destroy (inner_container);
4333 node_destroy (outer_container);
4337 /* This one takes a really long time, so comment it out for now */
4338 start_next_test ("Each value,value,value triplet combination as toplevel, in all orders %d iterations\n",
4339 N_VALUES * N_VALUES * N_VALUES);
4341 TestTypeNode *nodes[3];
4344 while ((nodes[0] = value_generator (&i)))
4347 while ((nodes[1] = value_generator (&j)))
4350 while ((nodes[2] = value_generator (&k)))
4352 run_test_nodes (nodes, 3);
4354 node_destroy (nodes[2]);
4356 node_destroy (nodes[1]);
4358 node_destroy (nodes[0]);
4361 #endif /* #if 0 expensive test */
4363 fprintf (stderr, "%d total iterations of recursive marshaling tests\n",
4364 n_iterations_completed_total);
4365 fprintf (stderr, "each iteration ran at initial offsets 0 through %d in both big and little endian\n",
4366 MAX_INITIAL_OFFSET);
4367 fprintf (stderr, "out of memory handling %s tested\n",
4368 TEST_OOM_HANDLING ? "was" : "was not");
4372 _dbus_marshal_recursive_test (void)
4374 make_and_run_test_nodes ();
4382 * Implementations of each type node class
4387 #define MAX_MULTI_COUNT 5
4390 #define SAMPLE_INT32 12345678
4391 #define SAMPLE_INT32_ALTERNATE 53781429
4393 int32_from_seed (int seed)
4395 /* Generate an integer value that's predictable from seed. We could
4396 * just use seed itself, but that would only ever touch one byte of
4397 * the int so would miss some kinds of bug.
4401 v = 42; /* just to quiet compiler afaik */
4408 v = SAMPLE_INT32_ALTERNATE;
4422 v *= seed; /* wraps around eventually, which is fine */
4428 int32_write_value (TestTypeNode *node,
4430 DBusTypeWriter *writer,
4433 /* also used for uint32 */
4436 v = int32_from_seed (seed);
4438 return _dbus_type_writer_write_basic (writer,
4439 node->klass->typecode,
4444 int32_read_value (TestTypeNode *node,
4445 DBusTypeReader *reader,
4448 /* also used for uint32 */
4451 check_expected_type (reader, node->klass->typecode);
4453 _dbus_type_reader_read_basic (reader,
4454 (dbus_int32_t*) &v);
4456 _dbus_assert (v == int32_from_seed (seed));
4462 int32_set_value (TestTypeNode *node,
4463 DBusTypeReader *reader,
4464 DBusTypeReader *realign_root,
4467 /* also used for uint32 */
4470 v = int32_from_seed (seed);
4472 return _dbus_type_reader_set_basic (reader,
4478 int32_write_multi (TestTypeNode *node,
4480 DBusTypeWriter *writer,
4484 /* also used for uint32 */
4485 dbus_int32_t values[MAX_MULTI_COUNT];
4486 dbus_int32_t *v_ARRAY_INT32 = values;
4489 for (i = 0; i < count; ++i)
4490 values[i] = int32_from_seed (seed + i);
4492 return _dbus_type_writer_write_fixed_multi (writer,
4493 node->klass->typecode,
4494 &v_ARRAY_INT32, count);
4498 int32_read_multi (TestTypeNode *node,
4499 DBusTypeReader *reader,
4503 /* also used for uint32 */
4504 dbus_int32_t *values;
4508 check_expected_type (reader, node->klass->typecode);
4510 _dbus_type_reader_read_fixed_multi (reader,
4514 if (n_elements != count)
4515 _dbus_warn ("got %d elements expected %d\n", n_elements, count);
4516 _dbus_assert (n_elements == count);
4518 for (i = 0; i < count; i++)
4519 _dbus_assert (_dbus_unpack_int32 (reader->byte_order,
4520 (const unsigned char*)values + (i * 4)) ==
4521 int32_from_seed (seed + i));
4526 #ifdef DBUS_HAVE_INT64
4528 int64_from_seed (int seed)
4533 v32 = int32_from_seed (seed);
4535 v = - (dbus_int32_t) ~ v32;
4536 v |= (((dbus_int64_t)v32) << 32);
4543 int64_write_value (TestTypeNode *node,
4545 DBusTypeWriter *writer,
4548 #ifdef DBUS_HAVE_INT64
4549 /* also used for uint64 */
4552 v = int64_from_seed (seed);
4554 return _dbus_type_writer_write_basic (writer,
4555 node->klass->typecode,
4563 int64_read_value (TestTypeNode *node,
4564 DBusTypeReader *reader,
4567 #ifdef DBUS_HAVE_INT64
4568 /* also used for uint64 */
4571 check_expected_type (reader, node->klass->typecode);
4573 _dbus_type_reader_read_basic (reader,
4574 (dbus_int64_t*) &v);
4576 _dbus_assert (v == int64_from_seed (seed));
4585 int64_set_value (TestTypeNode *node,
4586 DBusTypeReader *reader,
4587 DBusTypeReader *realign_root,
4590 #ifdef DBUS_HAVE_INT64
4591 /* also used for uint64 */
4594 v = int64_from_seed (seed);
4596 return _dbus_type_reader_set_basic (reader,
4604 #define MAX_SAMPLE_STRING_LEN 10
4606 string_from_seed (char *buf,
4613 _dbus_assert (len < MAX_SAMPLE_STRING_LEN);
4615 /* vary the length slightly, though we also have multiple string
4616 * value types for this, varying it here tests the set_value code
4630 v = (unsigned char) ('A' + seed);
4635 if (v < 'A' || v > 'z')
4648 string_write_value (TestTypeNode *node,
4650 DBusTypeWriter *writer,
4653 char buf[MAX_SAMPLE_STRING_LEN];
4654 const char *v_string = buf;
4656 string_from_seed (buf, node->klass->subclass_detail,
4659 return _dbus_type_writer_write_basic (writer,
4660 node->klass->typecode,
4665 string_read_value (TestTypeNode *node,
4666 DBusTypeReader *reader,
4670 char buf[MAX_SAMPLE_STRING_LEN];
4672 check_expected_type (reader, node->klass->typecode);
4674 _dbus_type_reader_read_basic (reader,
4675 (const char **) &v);
4677 string_from_seed (buf, node->klass->subclass_detail,
4680 if (strcmp (buf, v) != 0)
4682 _dbus_warn ("read string '%s' expected '%s'\n",
4684 _dbus_assert_not_reached ("test failed");
4691 string_set_value (TestTypeNode *node,
4692 DBusTypeReader *reader,
4693 DBusTypeReader *realign_root,
4696 char buf[MAX_SAMPLE_STRING_LEN];
4697 const char *v_string = buf;
4699 string_from_seed (buf, node->klass->subclass_detail,
4702 #if RECURSIVE_MARSHAL_WRITE_TRACE
4705 _dbus_type_reader_read_basic (reader, &old);
4706 _dbus_verbose ("SETTING new string '%s' len %d in place of '%s' len %d\n",
4707 v_string, strlen (v_string), old, strlen (old));
4711 return _dbus_type_reader_set_basic (reader,
4716 #define BOOL_FROM_SEED(seed) (seed % 2)
4719 bool_write_value (TestTypeNode *node,
4721 DBusTypeWriter *writer,
4726 v = BOOL_FROM_SEED (seed);
4728 return _dbus_type_writer_write_basic (writer,
4729 node->klass->typecode,
4734 bool_read_value (TestTypeNode *node,
4735 DBusTypeReader *reader,
4740 check_expected_type (reader, node->klass->typecode);
4742 _dbus_type_reader_read_basic (reader,
4743 (unsigned char*) &v);
4745 _dbus_assert (v == BOOL_FROM_SEED (seed));
4751 bool_set_value (TestTypeNode *node,
4752 DBusTypeReader *reader,
4753 DBusTypeReader *realign_root,
4758 v = BOOL_FROM_SEED (seed);
4760 return _dbus_type_reader_set_basic (reader,
4765 #define BYTE_FROM_SEED(seed) ((unsigned char) int32_from_seed (seed))
4768 byte_write_value (TestTypeNode *node,
4770 DBusTypeWriter *writer,
4775 v = BYTE_FROM_SEED (seed);
4777 return _dbus_type_writer_write_basic (writer,
4778 node->klass->typecode,
4783 byte_read_value (TestTypeNode *node,
4784 DBusTypeReader *reader,
4789 check_expected_type (reader, node->klass->typecode);
4791 _dbus_type_reader_read_basic (reader,
4792 (unsigned char*) &v);
4794 _dbus_assert (v == BYTE_FROM_SEED (seed));
4801 byte_set_value (TestTypeNode *node,
4802 DBusTypeReader *reader,
4803 DBusTypeReader *realign_root,
4808 v = BYTE_FROM_SEED (seed);
4810 return _dbus_type_reader_set_basic (reader,
4816 double_from_seed (int seed)
4818 return SAMPLE_INT32 * (double) seed + 0.3;
4822 double_write_value (TestTypeNode *node,
4824 DBusTypeWriter *writer,
4829 v = double_from_seed (seed);
4831 return _dbus_type_writer_write_basic (writer,
4832 node->klass->typecode,
4837 double_read_value (TestTypeNode *node,
4838 DBusTypeReader *reader,
4844 check_expected_type (reader, node->klass->typecode);
4846 _dbus_type_reader_read_basic (reader,
4849 expected = double_from_seed (seed);
4851 if (!_DBUS_DOUBLES_BITWISE_EQUAL (v, expected))
4853 #ifdef DBUS_HAVE_INT64
4854 _dbus_warn ("Expected double %g got %g\n bits = 0x%llx vs.\n bits = 0x%llx)\n",
4856 *(dbus_uint64_t*)(char*)&expected,
4857 *(dbus_uint64_t*)(char*)&v);
4859 _dbus_assert_not_reached ("test failed");
4866 double_set_value (TestTypeNode *node,
4867 DBusTypeReader *reader,
4868 DBusTypeReader *realign_root,
4873 v = double_from_seed (seed);
4875 return _dbus_type_reader_set_basic (reader,
4880 #define MAX_SAMPLE_OBJECT_PATH_LEN 10
4882 object_path_from_seed (char *buf,
4890 _dbus_assert (len < MAX_SAMPLE_OBJECT_PATH_LEN);
4892 v = (unsigned char) ('A' + seed);
4897 if (v < 'A' || v > 'z')
4912 object_path_write_value (TestTypeNode *node,
4914 DBusTypeWriter *writer,
4917 char buf[MAX_SAMPLE_OBJECT_PATH_LEN];
4918 const char *v_string = buf;
4920 object_path_from_seed (buf, seed);
4922 return _dbus_type_writer_write_basic (writer,
4923 node->klass->typecode,
4928 object_path_read_value (TestTypeNode *node,
4929 DBusTypeReader *reader,
4933 char buf[MAX_SAMPLE_OBJECT_PATH_LEN];
4935 check_expected_type (reader, node->klass->typecode);
4937 _dbus_type_reader_read_basic (reader,
4938 (const char **) &v);
4940 object_path_from_seed (buf, seed);
4942 if (strcmp (buf, v) != 0)
4944 _dbus_warn ("read object path '%s' expected '%s'\n",
4946 _dbus_assert_not_reached ("test failed");
4953 object_path_set_value (TestTypeNode *node,
4954 DBusTypeReader *reader,
4955 DBusTypeReader *realign_root,
4958 char buf[MAX_SAMPLE_OBJECT_PATH_LEN];
4959 const char *v_string = buf;
4961 object_path_from_seed (buf, seed);
4963 return _dbus_type_reader_set_basic (reader,
4968 #define MAX_SAMPLE_SIGNATURE_LEN 10
4970 signature_from_seed (char *buf,
4975 /* try to avoid ascending, descending, or alternating length to help find bugs */
4976 const char *sample_signatures[] = {
4985 s = sample_signatures[seed % _DBUS_N_ELEMENTS(sample_signatures)];
4987 for (i = 0; s[i]; i++)
4995 signature_write_value (TestTypeNode *node,
4997 DBusTypeWriter *writer,
5000 char buf[MAX_SAMPLE_SIGNATURE_LEN];
5001 const char *v_string = buf;
5003 signature_from_seed (buf, seed);
5005 return _dbus_type_writer_write_basic (writer,
5006 node->klass->typecode,
5011 signature_read_value (TestTypeNode *node,
5012 DBusTypeReader *reader,
5016 char buf[MAX_SAMPLE_SIGNATURE_LEN];
5018 check_expected_type (reader, node->klass->typecode);
5020 _dbus_type_reader_read_basic (reader,
5021 (const char **) &v);
5023 signature_from_seed (buf, seed);
5025 if (strcmp (buf, v) != 0)
5027 _dbus_warn ("read signature value '%s' expected '%s'\n",
5029 _dbus_assert_not_reached ("test failed");
5037 signature_set_value (TestTypeNode *node,
5038 DBusTypeReader *reader,
5039 DBusTypeReader *realign_root,
5042 char buf[MAX_SAMPLE_SIGNATURE_LEN];
5043 const char *v_string = buf;
5045 signature_from_seed (buf, seed);
5047 return _dbus_type_reader_set_basic (reader,
5053 struct_write_value (TestTypeNode *node,
5055 DBusTypeWriter *writer,
5058 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
5059 DataBlockState saved;
5064 n_copies = node->klass->subclass_detail;
5066 _dbus_assert (container->children != NULL);
5068 data_block_save (block, &saved);
5070 if (!_dbus_type_writer_recurse (writer, DBUS_TYPE_STRUCT,
5076 while (i < n_copies)
5080 link = _dbus_list_get_first_link (&container->children);
5081 while (link != NULL)
5083 TestTypeNode *child = link->data;
5084 DBusList *next = _dbus_list_get_next_link (&container->children, link);
5086 if (!node_write_value (child, block, &sub, seed + i))
5088 data_block_restore (block, &saved);
5098 if (!_dbus_type_writer_unrecurse (writer, &sub))
5100 data_block_restore (block, &saved);
5108 struct_read_or_set_value (TestTypeNode *node,
5109 DBusTypeReader *reader,
5110 DBusTypeReader *realign_root,
5113 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
5118 n_copies = node->klass->subclass_detail;
5120 check_expected_type (reader, DBUS_TYPE_STRUCT);
5122 _dbus_type_reader_recurse (reader, &sub);
5125 while (i < n_copies)
5129 link = _dbus_list_get_first_link (&container->children);
5130 while (link != NULL)
5132 TestTypeNode *child = link->data;
5133 DBusList *next = _dbus_list_get_next_link (&container->children, link);
5135 if (realign_root == NULL)
5137 if (!node_read_value (child, &sub, seed + i))
5142 if (!node_set_value (child, &sub, realign_root, seed + i))
5146 if (i == (n_copies - 1) && next == NULL)
5147 NEXT_EXPECTING_FALSE (&sub);
5149 NEXT_EXPECTING_TRUE (&sub);
5161 struct_read_value (TestTypeNode *node,
5162 DBusTypeReader *reader,
5165 return struct_read_or_set_value (node, reader, NULL, seed);
5169 struct_set_value (TestTypeNode *node,
5170 DBusTypeReader *reader,
5171 DBusTypeReader *realign_root,
5174 return struct_read_or_set_value (node, reader, realign_root, seed);
5178 struct_build_signature (TestTypeNode *node,
5181 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
5186 n_copies = node->klass->subclass_detail;
5188 orig_len = _dbus_string_get_length (str);
5190 if (!_dbus_string_append_byte (str, DBUS_STRUCT_BEGIN_CHAR))
5194 while (i < n_copies)
5198 link = _dbus_list_get_first_link (&container->children);
5199 while (link != NULL)
5201 TestTypeNode *child = link->data;
5202 DBusList *next = _dbus_list_get_next_link (&container->children, link);
5204 if (!node_build_signature (child, str))
5213 if (!_dbus_string_append_byte (str, DBUS_STRUCT_END_CHAR))
5219 _dbus_string_set_length (str, orig_len);
5224 array_write_value (TestTypeNode *node,
5226 DBusTypeWriter *writer,
5229 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
5230 DataBlockState saved;
5232 DBusString element_signature;
5236 TestTypeNode *child;
5238 n_copies = node->klass->subclass_detail;
5240 _dbus_assert (container->children != NULL);
5242 data_block_save (block, &saved);
5244 if (!_dbus_string_init (&element_signature))
5247 child = _dbus_list_get_first (&container->children);
5249 if (!node_build_signature (child,
5250 &element_signature))
5253 element_type = first_type_in_signature (&element_signature, 0);
5255 if (!_dbus_type_writer_recurse (writer, DBUS_TYPE_ARRAY,
5256 &element_signature, 0,
5260 if (arrays_write_fixed_in_blocks &&
5261 _dbus_type_is_fixed (element_type) &&
5262 child->klass->write_multi)
5264 if (!node_write_multi (child, block, &sub, seed, n_copies))
5270 while (i < n_copies)
5274 link = _dbus_list_get_first_link (&container->children);
5275 while (link != NULL)
5277 TestTypeNode *child = link->data;
5278 DBusList *next = _dbus_list_get_next_link (&container->children, link);
5280 if (!node_write_value (child, block, &sub, seed + i))
5290 if (!_dbus_type_writer_unrecurse (writer, &sub))
5293 _dbus_string_free (&element_signature);
5297 data_block_restore (block, &saved);
5298 _dbus_string_free (&element_signature);
5303 array_read_or_set_value (TestTypeNode *node,
5304 DBusTypeReader *reader,
5305 DBusTypeReader *realign_root,
5308 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
5312 TestTypeNode *child;
5314 n_copies = node->klass->subclass_detail;
5316 check_expected_type (reader, DBUS_TYPE_ARRAY);
5318 child = _dbus_list_get_first (&container->children);
5322 _dbus_assert (!_dbus_type_reader_array_is_empty (reader));
5324 _dbus_type_reader_recurse (reader, &sub);
5326 if (realign_root == NULL && arrays_write_fixed_in_blocks &&
5327 _dbus_type_is_fixed (_dbus_type_reader_get_array_type (reader)) &&
5328 child->klass->read_multi)
5330 if (!node_read_multi (child, &sub, seed, n_copies))
5336 while (i < n_copies)
5340 link = _dbus_list_get_first_link (&container->children);
5341 while (link != NULL)
5343 TestTypeNode *child = link->data;
5344 DBusList *next = _dbus_list_get_next_link (&container->children, link);
5346 _dbus_assert (child->klass->typecode ==
5347 _dbus_type_reader_get_array_type (reader));
5349 if (realign_root == NULL)
5351 if (!node_read_value (child, &sub, seed + i))
5356 if (!node_set_value (child, &sub, realign_root, seed + i))
5360 if (i == (n_copies - 1) && next == NULL)
5361 NEXT_EXPECTING_FALSE (&sub);
5363 NEXT_EXPECTING_TRUE (&sub);
5374 _dbus_assert (_dbus_type_reader_array_is_empty (reader));
5381 array_read_value (TestTypeNode *node,
5382 DBusTypeReader *reader,
5385 return array_read_or_set_value (node, reader, NULL, seed);
5389 array_set_value (TestTypeNode *node,
5390 DBusTypeReader *reader,
5391 DBusTypeReader *realign_root,
5394 return array_read_or_set_value (node, reader, realign_root, seed);
5398 array_build_signature (TestTypeNode *node,
5401 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
5404 orig_len = _dbus_string_get_length (str);
5406 if (!_dbus_string_append_byte (str, DBUS_TYPE_ARRAY))
5409 if (!node_build_signature (_dbus_list_get_first (&container->children),
5416 _dbus_string_set_length (str, orig_len);
5420 /* 10 is random just to add another seed that we use in the suite */
5421 #define VARIANT_SEED 10
5424 variant_write_value (TestTypeNode *node,
5426 DBusTypeWriter *writer,
5429 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
5430 DataBlockState saved;
5432 DBusString content_signature;
5433 TestTypeNode *child;
5435 _dbus_assert (container->children != NULL);
5436 _dbus_assert (_dbus_list_length_is_one (&container->children));
5438 child = _dbus_list_get_first (&container->children);
5440 data_block_save (block, &saved);
5442 if (!_dbus_string_init (&content_signature))
5445 if (!node_build_signature (child,
5446 &content_signature))
5449 if (!_dbus_type_writer_recurse (writer, DBUS_TYPE_VARIANT,
5450 &content_signature, 0,
5454 if (!node_write_value (child, block, &sub, seed + VARIANT_SEED))
5457 if (!_dbus_type_writer_unrecurse (writer, &sub))
5460 _dbus_string_free (&content_signature);
5464 data_block_restore (block, &saved);
5465 _dbus_string_free (&content_signature);
5470 variant_read_or_set_value (TestTypeNode *node,
5471 DBusTypeReader *reader,
5472 DBusTypeReader *realign_root,
5475 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
5477 TestTypeNode *child;
5479 _dbus_assert (container->children != NULL);
5480 _dbus_assert (_dbus_list_length_is_one (&container->children));
5482 child = _dbus_list_get_first (&container->children);
5484 check_expected_type (reader, DBUS_TYPE_VARIANT);
5486 _dbus_type_reader_recurse (reader, &sub);
5488 if (realign_root == NULL)
5490 if (!node_read_value (child, &sub, seed + VARIANT_SEED))
5495 if (!node_set_value (child, &sub, realign_root, seed + VARIANT_SEED))
5499 NEXT_EXPECTING_FALSE (&sub);
5505 variant_read_value (TestTypeNode *node,
5506 DBusTypeReader *reader,
5509 return variant_read_or_set_value (node, reader, NULL, seed);
5513 variant_set_value (TestTypeNode *node,
5514 DBusTypeReader *reader,
5515 DBusTypeReader *realign_root,
5518 return variant_read_or_set_value (node, reader, realign_root, seed);
5522 container_destroy (TestTypeNode *node)
5524 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
5527 link = _dbus_list_get_first_link (&container->children);
5528 while (link != NULL)
5530 TestTypeNode *child = link->data;
5531 DBusList *next = _dbus_list_get_next_link (&container->children, link);
5533 node_destroy (child);
5535 _dbus_list_free_link (link);
5541 #endif /* DBUS_BUILD_TESTS */