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_TRACE 0
34 struct DBusTypeReaderClass
37 int id; /* index in all_reader_classes */
38 dbus_bool_t types_only; /* only iterates over types, not values */
39 void (* recurse) (DBusTypeReader *sub,
40 DBusTypeReader *parent);
41 dbus_bool_t (* check_finished) (const DBusTypeReader *reader);
42 void (* next) (DBusTypeReader *reader,
44 void (* init_from_mark) (DBusTypeReader *reader,
45 const DBusTypeMark *mark);
49 first_type_in_signature (const DBusString *str,
54 t = _dbus_string_get_byte (str, pos);
56 if (t == DBUS_STRUCT_BEGIN_CHAR)
57 return DBUS_TYPE_STRUCT;
63 element_type_get_alignment (const DBusString *str,
66 return _dbus_type_get_alignment (first_type_in_signature (str, pos));
70 reader_init (DBusTypeReader *reader,
72 const DBusString *type_str,
74 const DBusString *value_str,
77 reader->byte_order = byte_order;
78 reader->finished = FALSE;
79 reader->type_str = type_str;
80 reader->type_pos = type_pos;
81 reader->value_str = value_str;
82 reader->value_pos = value_pos;
86 base_reader_recurse (DBusTypeReader *sub,
87 DBusTypeReader *parent)
89 /* point subreader at the same place as parent */
99 struct_types_only_reader_recurse (DBusTypeReader *sub,
100 DBusTypeReader *parent)
102 base_reader_recurse (sub, parent);
104 _dbus_assert (_dbus_string_get_byte (sub->type_str,
105 sub->type_pos) == DBUS_STRUCT_BEGIN_CHAR);
111 struct_reader_recurse (DBusTypeReader *sub,
112 DBusTypeReader *parent)
114 struct_types_only_reader_recurse (sub, parent);
116 /* struct has 8 byte alignment */
117 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
121 array_types_only_reader_recurse (DBusTypeReader *sub,
122 DBusTypeReader *parent)
124 base_reader_recurse (sub, parent);
126 /* point type_pos at the array element type */
129 /* Init with values likely to crash things if misused */
130 sub->u.array.start_pos = _DBUS_INT_MAX;
131 sub->array_len_offset = 7;
135 array_reader_get_array_len (const DBusTypeReader *reader)
137 dbus_uint32_t array_len;
140 /* array_len_offset is the offset back from start_pos to end of the len */
141 len_pos = reader->u.array.start_pos - ((int)reader->array_len_offset) - 4;
143 _dbus_marshal_read_basic (reader->value_str,
150 _dbus_verbose (" reader %p len_pos %d array len %u len_offset %d\n",
151 reader, len_pos, array_len, reader->array_len_offset);
153 _dbus_assert (reader->u.array.start_pos - len_pos - 4 < 8);
159 array_reader_recurse (DBusTypeReader *sub,
160 DBusTypeReader *parent)
165 array_types_only_reader_recurse (sub, parent);
167 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
169 len_pos = sub->value_pos;
171 sub->value_pos += 4; /* for the length */
173 alignment = element_type_get_alignment (sub->type_str,
176 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
178 sub->u.array.start_pos = sub->value_pos;
179 _dbus_assert ((sub->u.array.start_pos - (len_pos + 4)) < 8); /* only 3 bits in array_len_offset */
180 sub->array_len_offset = sub->u.array.start_pos - (len_pos + 4);
182 #if RECURSIVE_MARSHAL_TRACE
183 _dbus_verbose (" type reader %p array start = %d len_offset = %d array len = %d array element type = %s\n",
185 sub->u.array.start_pos,
186 sub->array_len_offset,
187 array_reader_get_array_len (sub),
188 _dbus_type_to_string (first_type_in_signature (sub->type_str,
194 variant_reader_recurse (DBusTypeReader *sub,
195 DBusTypeReader *parent)
199 base_reader_recurse (sub, parent);
201 /* Variant is 1 byte sig length (without nul), signature with nul,
202 * padding to 8-boundary, then values
205 sig_len = _dbus_string_get_byte (sub->value_str, sub->value_pos);
207 sub->type_str = sub->value_str;
208 sub->type_pos = sub->value_pos + 1;
210 sub->value_pos = sub->type_pos + sig_len + 1;
212 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
214 #if RECURSIVE_MARSHAL_TRACE
215 _dbus_verbose (" type reader %p variant containing '%s'\n",
217 _dbus_string_get_const_data_len (sub->type_str,
223 array_reader_check_finished (const DBusTypeReader *reader)
227 /* return the array element type if elements remain, and
228 * TYPE_INVALID otherwise
231 end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
233 _dbus_assert (reader->value_pos <= end_pos);
234 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
236 return reader->value_pos == end_pos;
240 skip_one_complete_type (const DBusString *type_str,
243 while (_dbus_string_get_byte (type_str, *type_pos) == DBUS_TYPE_ARRAY)
246 if (_dbus_string_get_byte (type_str, *type_pos) == DBUS_STRUCT_BEGIN_CHAR)
253 switch (_dbus_string_get_byte (type_str, *type_pos))
255 case DBUS_STRUCT_BEGIN_CHAR:
258 case DBUS_STRUCT_END_CHAR:
261 case DBUS_TYPE_INVALID:
262 _dbus_assert_not_reached ("unbalanced parens in signature");
273 find_len_of_complete_type (const DBusString *type_str,
280 skip_one_complete_type (type_str, &end);
282 return end - type_pos;
286 base_reader_next (DBusTypeReader *reader,
289 switch (current_type)
291 case DBUS_TYPE_STRUCT:
292 case DBUS_TYPE_VARIANT:
293 /* Scan forward over the entire container contents */
297 /* Recurse into the struct or variant */
298 _dbus_type_reader_recurse (reader, &sub);
300 /* Skip everything in this subreader */
301 while (_dbus_type_reader_next (&sub))
306 /* Now we are at the end of this container; for variants, the
307 * subreader's type_pos is totally inapplicable (it's in the
308 * value string) but we know that we increment by one past the
311 if (current_type == DBUS_TYPE_VARIANT)
312 reader->type_pos += 1;
314 reader->type_pos = sub.type_pos;
316 if (!reader->klass->types_only)
317 reader->value_pos = sub.value_pos;
321 case DBUS_TYPE_ARRAY:
323 if (!reader->klass->types_only)
324 _dbus_marshal_skip_array (reader->value_str,
325 first_type_in_signature (reader->type_str,
326 reader->type_pos + 1),
330 skip_one_complete_type (reader->type_str, &reader->type_pos);
335 if (!reader->klass->types_only)
336 _dbus_marshal_skip_basic (reader->value_str,
337 current_type, reader->byte_order,
340 reader->type_pos += 1;
346 struct_reader_next (DBusTypeReader *reader,
351 base_reader_next (reader, current_type);
353 /* for STRUCT containers we return FALSE at the end of the struct,
354 * for INVALID we return FALSE at the end of the signature.
355 * In both cases we arrange for get_current_type() to return INVALID
356 * which is defined to happen iff we're at the end (no more next())
358 t = _dbus_string_get_byte (reader->type_str, reader->type_pos);
359 if (t == DBUS_STRUCT_END_CHAR)
361 reader->type_pos += 1;
362 reader->finished = TRUE;
367 array_types_only_reader_next (DBusTypeReader *reader,
370 /* We have one "element" to be iterated over
371 * in each array, which is its element type.
372 * So the finished flag indicates whether we've
373 * iterated over it yet or not.
375 reader->finished = TRUE;
379 array_reader_next (DBusTypeReader *reader,
382 /* Skip one array element */
385 end_pos = reader->u.array.start_pos + array_reader_get_array_len (reader);
387 _dbus_assert (reader->value_pos < end_pos);
388 _dbus_assert (reader->value_pos >= reader->u.array.start_pos);
390 switch (first_type_in_signature (reader->type_str,
393 case DBUS_TYPE_STRUCT:
394 case DBUS_TYPE_VARIANT:
398 /* Recurse into the struct or variant */
399 _dbus_type_reader_recurse (reader, &sub);
401 /* Skip everything in this element */
402 while (_dbus_type_reader_next (&sub))
407 /* Now we are at the end of this element */
408 reader->value_pos = sub.value_pos;
412 case DBUS_TYPE_ARRAY:
414 _dbus_marshal_skip_array (reader->value_str,
415 first_type_in_signature (reader->type_str,
416 reader->type_pos + 1),
424 _dbus_marshal_skip_basic (reader->value_str,
425 current_type, reader->byte_order,
431 _dbus_assert (reader->value_pos <= end_pos);
433 if (reader->value_pos == end_pos)
435 skip_one_complete_type (reader->type_str,
441 array_init_from_mark (DBusTypeReader *reader,
442 const DBusTypeMark *mark)
444 /* Fill in the array-specific fields from the mark. The general
445 * fields are already filled in.
447 reader->u.array.start_pos = mark->array_start_pos;
448 reader->array_len_offset = mark->array_len_offset;
451 static const DBusTypeReaderClass body_reader_class = {
454 NULL, /* body is always toplevel, so doesn't get recursed into */
460 static const DBusTypeReaderClass body_types_only_reader_class = {
463 NULL, /* body is always toplevel, so doesn't get recursed into */
469 static const DBusTypeReaderClass struct_reader_class = {
472 struct_reader_recurse,
478 static const DBusTypeReaderClass struct_types_only_reader_class = {
481 struct_types_only_reader_recurse,
487 static const DBusTypeReaderClass array_reader_class = {
490 array_reader_recurse,
491 array_reader_check_finished,
496 static const DBusTypeReaderClass array_types_only_reader_class = {
499 array_types_only_reader_recurse,
501 array_types_only_reader_next,
505 static const DBusTypeReaderClass variant_reader_class = {
508 variant_reader_recurse,
514 static const DBusTypeReaderClass const *
515 all_reader_classes[] = {
517 &body_types_only_reader_class,
518 &struct_reader_class,
519 &struct_types_only_reader_class,
521 &array_types_only_reader_class,
522 &variant_reader_class
526 _dbus_type_reader_init (DBusTypeReader *reader,
528 const DBusString *type_str,
530 const DBusString *value_str,
533 reader->klass = &body_reader_class;
535 reader_init (reader, byte_order, type_str, type_pos,
536 value_str, value_pos);
538 #if RECURSIVE_MARSHAL_TRACE
539 _dbus_verbose (" type reader %p init type_pos = %d value_pos = %d remaining sig '%s'\n",
540 reader, reader->type_pos, reader->value_pos,
541 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
546 _dbus_type_reader_init_from_mark (DBusTypeReader *reader,
548 const DBusString *type_str,
549 const DBusString *value_str,
550 const DBusTypeMark *mark)
552 reader->klass = all_reader_classes[mark->container_type];
554 reader_init (reader, byte_order,
555 mark->type_pos_in_value_str ? value_str : type_str,
557 value_str, mark->value_pos);
559 if (reader->klass->init_from_mark)
560 (* reader->klass->init_from_mark) (reader, mark);
562 #if RECURSIVE_MARSHAL_TRACE
563 _dbus_verbose (" type reader %p init from mark type_pos = %d value_pos = %d remaining sig '%s'\n",
564 reader, reader->type_pos, reader->value_pos,
565 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
570 _dbus_type_reader_init_types_only (DBusTypeReader *reader,
571 const DBusString *type_str,
574 reader->klass = &body_types_only_reader_class;
576 reader_init (reader, DBUS_COMPILER_BYTE_ORDER /* irrelevant */,
577 type_str, type_pos, NULL, _DBUS_INT_MAX /* crashes if we screw up */);
579 #if RECURSIVE_MARSHAL_TRACE
580 _dbus_verbose (" type reader %p init types only type_pos = %d remaining sig '%s'\n",
581 reader, reader->type_pos,
582 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
587 _dbus_type_reader_init_types_only_from_mark (DBusTypeReader *reader,
588 const DBusString *type_str,
589 const DBusTypeMark *mark)
591 reader->klass = all_reader_classes[mark->container_type];
592 _dbus_assert (reader->klass->types_only);
593 _dbus_assert (!mark->type_pos_in_value_str);
595 reader_init (reader, DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
596 type_str, mark->type_pos,
597 NULL, _DBUS_INT_MAX /* crashes if we screw up */);
599 if (reader->klass->init_from_mark)
600 (* reader->klass->init_from_mark) (reader, mark);
602 #if RECURSIVE_MARSHAL_TRACE
603 _dbus_verbose (" type reader %p init types only from mark type_pos = %d remaining sig '%s'\n",
604 reader, reader->type_pos,
605 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
610 _dbus_type_reader_save_mark (const DBusTypeReader *reader,
613 mark->type_pos_in_value_str = (reader->type_str == reader->value_str);
614 mark->container_type = reader->klass->id;
615 _dbus_assert (all_reader_classes[reader->klass->id] == reader->klass);
617 mark->type_pos = reader->type_pos;
618 mark->value_pos = reader->value_pos;
620 /* these are just junk if the reader isn't really an array of course */
621 mark->array_len_offset = reader->array_len_offset;
622 mark->array_start_pos = reader->u.array.start_pos;
626 _dbus_type_reader_get_current_type (const DBusTypeReader *reader)
630 if (reader->finished ||
631 (reader->klass->check_finished &&
632 (* reader->klass->check_finished) (reader)))
633 t = DBUS_TYPE_INVALID;
635 t = first_type_in_signature (reader->type_str,
638 _dbus_assert (t != DBUS_STRUCT_END_CHAR);
639 _dbus_assert (t != DBUS_STRUCT_BEGIN_CHAR);
642 _dbus_verbose (" type reader %p current type_pos = %d type = %s\n",
643 reader, reader->type_pos,
644 _dbus_type_to_string (t));
651 _dbus_type_reader_array_is_empty (const DBusTypeReader *reader)
653 dbus_uint32_t array_len;
655 _dbus_assert (_dbus_type_reader_get_current_type (reader) == DBUS_TYPE_ARRAY);
656 _dbus_assert (!reader->klass->types_only);
658 /* reader is supposed to be at an array child */
659 #if RECURSIVE_MARSHAL_TRACE
660 _dbus_verbose ("checking array len at %d\n", reader->value_pos);
663 _dbus_marshal_read_basic (reader->value_str,
669 #if RECURSIVE_MARSHAL_TRACE
670 _dbus_verbose (" ... array len = %d\n", array_len);
673 return array_len == 0;
677 _dbus_type_reader_read_basic (const DBusTypeReader *reader,
682 _dbus_assert (!reader->klass->types_only);
684 t = _dbus_type_reader_get_current_type (reader);
686 _dbus_marshal_read_basic (reader->value_str,
693 #if RECURSIVE_MARSHAL_TRACE
694 _dbus_verbose (" type reader %p read basic type_pos = %d value_pos = %d remaining sig '%s'\n",
695 reader, reader->type_pos, reader->value_pos,
696 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0));
701 _dbus_type_reader_read_array_of_basic (const DBusTypeReader *reader,
706 _dbus_assert (!reader->klass->types_only);
711 * Initialize a new reader pointing to the first type and
712 * corresponding value that's a child of the current container. It's
713 * an error to call this if the current type is a non-container.
715 * Note that DBusTypeReader traverses values, not types. So if you
716 * have an empty array of array of int, you can't recurse into it. You
717 * can only recurse into each element.
719 * @param reader the reader
720 * @param sub a reader to init pointing to the first child
723 _dbus_type_reader_recurse (DBusTypeReader *reader,
728 t = first_type_in_signature (reader->type_str, reader->type_pos);
732 case DBUS_TYPE_STRUCT:
733 if (reader->klass->types_only)
734 sub->klass = &struct_types_only_reader_class;
736 sub->klass = &struct_reader_class;
738 case DBUS_TYPE_ARRAY:
739 if (reader->klass->types_only)
740 sub->klass = &array_types_only_reader_class;
742 sub->klass = &array_reader_class;
744 case DBUS_TYPE_VARIANT:
745 if (reader->klass->types_only)
746 _dbus_assert_not_reached ("can't recurse into variant typecode");
748 sub->klass = &variant_reader_class;
751 _dbus_verbose ("recursing into type %s\n", _dbus_type_to_string (t));
752 #ifndef DBUS_DISABLE_CHECKS
753 if (t == DBUS_TYPE_INVALID)
754 _dbus_warn ("You can't recurse into an empty array or off the end of a message body\n");
755 #endif /* DBUS_DISABLE_CHECKS */
757 _dbus_assert_not_reached ("don't yet handle recursing into this type");
760 _dbus_assert (sub->klass == all_reader_classes[sub->klass->id]);
762 (* sub->klass->recurse) (sub, reader);
764 #if RECURSIVE_MARSHAL_TRACE
765 _dbus_verbose (" type reader %p RECURSED type_pos = %d value_pos = %d remaining sig '%s'\n",
766 sub, sub->type_pos, sub->value_pos,
767 _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0));
772 * Skip to the next value on this "level". e.g. the next field in a
773 * struct, the next value in an array. Returns FALSE at the end of the
776 * @param reader the reader
777 * @returns FALSE if nothing more to read at or below this level
780 _dbus_type_reader_next (DBusTypeReader *reader)
784 t = _dbus_type_reader_get_current_type (reader);
786 #if RECURSIVE_MARSHAL_TRACE
787 _dbus_verbose (" type reader %p START next() { type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
788 reader, reader->type_pos, reader->value_pos,
789 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
790 _dbus_type_to_string (t));
793 if (t == DBUS_TYPE_INVALID)
796 (* reader->klass->next) (reader, t);
798 #if RECURSIVE_MARSHAL_TRACE
799 _dbus_verbose (" type reader %p END next() type_pos = %d value_pos = %d remaining sig '%s' current_type = %s\n",
800 reader, reader->type_pos, reader->value_pos,
801 _dbus_string_get_const_data_len (reader->type_str, reader->type_pos, 0),
802 _dbus_type_to_string (_dbus_type_reader_get_current_type (reader)));
805 return _dbus_type_reader_get_current_type (reader) != DBUS_TYPE_INVALID;
809 * Check whether there's another value on this "level". e.g. the next
810 * field in a struct, the next value in an array. Returns FALSE at the
811 * end of the current container.
813 * You probably don't want to use this; it makes for an awkward for/while
814 * loop. A nicer one is "while ((current_type = get_current_type()) != INVALID)"
816 * @param reader the reader
817 * @returns FALSE if nothing more to read at or below this level
820 _dbus_type_reader_has_next (const DBusTypeReader *reader)
822 /* Not efficient but works for now. */
826 return _dbus_type_reader_next (©);
830 * Gets the string and range of said string containing the signature
831 * of the current value. Essentially a more complete version of
832 * _dbus_type_reader_get_current_type() (returns the full type
833 * rather than only the outside of the onion).
835 * Note though that the first byte in a struct signature is
836 * #DBUS_STRUCT_BEGIN_CHAR while the current type will be
837 * #DBUS_TYPE_STRUCT so it isn't true that the first byte of the
838 * signature is always the same as the current type. Another
839 * difference is that this function will still return a signature when
840 * inside an empty array; say you recurse into empty array of int32,
841 * the signature is "i" but the current type will always be
842 * #DBUS_TYPE_INVALID since there are no elements to be currently
845 * @param reader the reader
846 * @param str_p place to return the string with the type in it
847 * @param start_p place to return start of the type
848 * @param len_p place to return the length of the type
851 _dbus_type_reader_get_signature (const DBusTypeReader *reader,
852 const DBusString **str_p,
856 *str_p = reader->type_str;
857 *start_p = reader->type_pos;
858 *len_p = find_len_of_complete_type (reader->type_str, reader->type_pos);
862 reader_set_basic_fixed_length (DBusTypeReader *reader,
866 _dbus_marshal_set_basic ((DBusString*) reader->value_str,
875 * Sets a new value for the basic type pointed to by the reader,
876 * leaving the reader valid to continue reading. Any other readers may
877 * of course be invalidated if you set a variable-length type such as
880 * The provided realign_root is the reader to start from when
881 * realigning the data that follows the newly-set value. The reader
882 * parameter must point to a value below the realign_root parameter.
883 * If the type being set is fixed-length, then realign_root may be
884 * #NULL. Only values reachable from realign_root will be realigned,
885 * so if your string contains other values you will need to deal with
886 * those somehow yourself. It is OK if realign_root is the same
887 * reader as the reader parameter, though if you aren't setting the
888 * root it may not be such a good idea.
890 * @todo DBusTypeReader currently takes "const" versions of the type
891 * and value strings, and this function modifies those strings by
892 * casting away the const, which is of course bad if we want to get
893 * picky. (To be truly clean you'd have an object which contained the
894 * type and value strings and set_basic would be a method on that
895 * object... this would also make DBusTypeReader the same thing as
896 * DBusTypeMark. But since DBusMessage is effectively that object for
897 * D-BUS it doesn't seem worth creating some random object.)
899 * @param reader reader indicating where to set a new value
900 * @param value address of the value to set
901 * @param realign_root realign from here
902 * @returns #FALSE if not enough memory
905 _dbus_type_reader_set_basic (DBusTypeReader *reader,
907 const DBusTypeReader *realign_root)
914 current_type = _dbus_type_reader_get_current_type (reader);
916 _dbus_assert (_dbus_type_is_basic (current_type));
918 if (!_dbus_type_length_varies (current_type))
920 reader_set_basic_fixed_length (reader, current_type, value);
924 _dbus_assert (realign_root != NULL);
926 /* In the harder case, we have to fix alignment after we insert.
927 * The basic strategy is as follows:
929 * - pad a new string to have the same alignment as the
930 * start of the current basic value
931 * - write the new basic value
932 * - copy from the original reader to the new string,
933 * which will fix the alignment of types following
935 * - this copy has to start at realign_root,
936 * but not really write anything until it
937 * passes the value being set
938 * - as an optimization, we can stop copying
939 * when the source and dest values are both
940 * on an 8-boundary, since we know all following
941 * padding and alignment will be identical
942 * - copy the new string back to the original
943 * string, replacing the relevant part of the
965 * Initialize a write iterator, which is used to write out values in
966 * serialized D-BUS format. #DBusTypeWriter is a value iterator; it
967 * writes out values. You can't use it to write out only types.
969 * The type_pos passed in is expected to be inside an already-valid,
970 * though potentially empty, type signature. This means that the byte
971 * after type_pos must be either #DBUS_TYPE_INVALID (aka nul) or some
972 * other valid type. #DBusTypeWriter won't enforce that the signature
973 * is already valid (you can append the nul byte at the end if you
974 * like), but just be aware that you need the nul byte eventually and
975 * #DBusTypeWriter isn't going to write it for you.
977 * @param writer the writer to init
978 * @param byte_order the byte order to marshal into
979 * @param type_str the string to write typecodes into
980 * @param type_pos where to insert typecodes
981 * @param value_str the string to write values into
982 * @param value_pos where to insert values
986 _dbus_type_writer_init (DBusTypeWriter *writer,
988 DBusString *type_str,
990 DBusString *value_str,
993 writer->byte_order = byte_order;
994 writer->type_str = type_str;
995 writer->type_pos = type_pos;
996 writer->value_str = value_str;
997 writer->value_pos = value_pos;
998 writer->container_type = DBUS_TYPE_INVALID;
999 writer->type_pos_is_expectation = FALSE;
1001 #if RECURSIVE_MARSHAL_TRACE
1002 _dbus_verbose ("writer %p init remaining sig '%s'\n", writer,
1003 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0));
1008 _dbus_type_writer_write_basic_no_typecode (DBusTypeWriter *writer,
1012 return _dbus_marshal_write_basic (writer->value_str,
1017 &writer->value_pos);
1020 /* If our parent is an array, things are a little bit complicated.
1022 * The parent must have a complete element type, such as
1023 * "i" or "aai" or "(ii)" or "a(ii)". There can't be
1024 * unclosed parens, or an "a" with no following type.
1026 * To recurse, the only allowed operation is to recurse into the
1027 * first type in the element type. So for "i" you can't recurse, for
1028 * "ai" you can recurse into the array, for "(ii)" you can recurse
1031 * If you recurse into the array for "ai", then you must specify
1032 * "i" for the element type of the array you recurse into.
1034 * While inside an array at any level, we need to avoid writing to
1035 * type_str, since the type only appears once for the whole array,
1036 * it does not appear for each array element.
1038 * While inside an array type_pos points to the expected next
1039 * typecode, rather than the next place we could write a typecode.
1042 writer_recurse_init_and_check (DBusTypeWriter *writer,
1044 DBusTypeWriter *sub)
1046 _dbus_type_writer_init (sub,
1053 sub->container_type = container_type;
1055 if (writer->type_pos_is_expectation ||
1056 (sub->container_type == DBUS_TYPE_ARRAY || sub->container_type == DBUS_TYPE_VARIANT))
1057 sub->type_pos_is_expectation = TRUE;
1059 sub->type_pos_is_expectation = FALSE;
1061 #ifndef DBUS_DISABLE_CHECKS
1062 if (writer->type_pos_is_expectation)
1066 expected = first_type_in_signature (writer->type_str, writer->type_pos);
1068 if (expected != sub->container_type)
1070 _dbus_warn ("Writing an element of type %s, but the expected type here is %s\n",
1071 _dbus_type_to_string (sub->container_type),
1072 _dbus_type_to_string (expected));
1073 _dbus_assert_not_reached ("bad array element or variant content written");
1076 #endif /* DBUS_DISABLE_CHECKS */
1078 #if RECURSIVE_MARSHAL_TRACE
1079 _dbus_verbose (" type writer %p recurse parent %s type_pos = %d value_pos = %d is_expectation = %d remaining sig '%s'\n",
1081 _dbus_type_to_string (writer->container_type),
1082 writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
1083 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0));
1084 _dbus_verbose (" type writer %p recurse sub %s type_pos = %d value_pos = %d is_expectation = %d\n",
1086 _dbus_type_to_string (sub->container_type),
1087 sub->type_pos, sub->value_pos,
1088 sub->type_pos_is_expectation);
1093 write_or_verify_typecode (DBusTypeWriter *writer,
1096 /* A subwriter inside an array or variant will have type_pos
1097 * pointing to the expected typecode; a writer not inside an array
1098 * or variant has type_pos pointing to the next place to insert a
1101 #if RECURSIVE_MARSHAL_TRACE
1102 _dbus_verbose (" type writer %p write_or_verify start type_pos = %d remaining sig '%s'\n",
1103 writer, writer->type_pos,
1104 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0));
1107 if (writer->type_pos_is_expectation)
1109 #ifndef DBUS_DISABLE_CHECKS
1113 expected = _dbus_string_get_byte (writer->type_str, writer->type_pos);
1115 if (expected != typecode)
1117 _dbus_warn ("Array or variant type requires that type %s be written, but %s was written\n",
1118 _dbus_type_to_string (expected), _dbus_type_to_string (typecode));
1119 _dbus_assert_not_reached ("bad type inserted somewhere inside an array or variant");
1122 #endif /* DBUS_DISABLE_CHECKS */
1124 /* if immediately inside an array we'd always be appending an element,
1125 * so the expected type doesn't change; if inside a struct or something
1126 * below an array, we need to move through said struct or something.
1128 if (writer->container_type != DBUS_TYPE_ARRAY)
1129 writer->type_pos += 1;
1133 if (!_dbus_string_insert_byte (writer->type_str,
1138 writer->type_pos += 1;
1141 #if RECURSIVE_MARSHAL_TRACE
1142 _dbus_verbose (" type writer %p write_or_verify end type_pos = %d remaining sig '%s'\n",
1143 writer, writer->type_pos,
1144 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0));
1151 writer_recurse_struct (DBusTypeWriter *writer,
1152 const DBusString *contained_type,
1153 int contained_type_start,
1154 int contained_type_len,
1155 DBusTypeWriter *sub)
1157 /* FIXME right now contained_type is ignored; we could probably
1158 * almost trivially fix the code so if it's present we
1159 * write it out and then set type_pos_is_expectation
1162 /* Ensure that we'll be able to add alignment padding and the typecode */
1163 if (!_dbus_string_alloc_space (sub->value_str, 8))
1166 if (!_dbus_string_alloc_space (sub->type_str, 1))
1169 if (!write_or_verify_typecode (sub, DBUS_STRUCT_BEGIN_CHAR))
1170 _dbus_assert_not_reached ("failed to insert struct typecode after prealloc");
1172 if (!_dbus_string_insert_bytes (sub->value_str,
1174 _DBUS_ALIGN_VALUE (sub->value_pos, 8) - sub->value_pos,
1176 _dbus_assert_not_reached ("should not have failed to insert alignment padding for struct");
1177 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
1184 writer_recurse_array (DBusTypeWriter *writer,
1185 const DBusString *contained_type,
1186 int contained_type_start,
1187 int contained_type_len,
1188 DBusTypeWriter *sub)
1190 dbus_uint32_t value = 0;
1194 #ifndef DBUS_DISABLE_CHECKS
1195 if (writer->container_type == DBUS_TYPE_ARRAY)
1197 if (!_dbus_string_equal_substring (contained_type,
1198 contained_type_start,
1201 writer->u.array.element_type_pos + 1))
1203 _dbus_warn ("Writing an array of '%s' but this is incompatible with the expected type of elements in the parent array\n",
1204 _dbus_string_get_const_data_len (contained_type,
1205 contained_type_start,
1206 contained_type_len));
1207 _dbus_assert_not_reached ("incompatible type for child array");
1210 #endif /* DBUS_DISABLE_CHECKS */
1212 /* 3 pad + 4 bytes for the array length, and 4 bytes possible padding
1213 * before array values
1215 if (!_dbus_string_alloc_space (sub->value_str, 3 + 4 + 4))
1218 sub->type_pos += 1; /* move to point to the element type, since type_pos
1219 * should be the expected type for further writes
1221 sub->u.array.element_type_pos = sub->type_pos;
1223 if (!writer->type_pos_is_expectation)
1225 /* sub is a toplevel/outermost array so we need to write the type data */
1227 /* alloc space for array typecode, element signature */
1228 if (!_dbus_string_alloc_space (writer->type_str, 1 + contained_type_len))
1231 if (!_dbus_string_insert_byte (writer->type_str,
1234 _dbus_assert_not_reached ("failed to insert array typecode after prealloc");
1236 if (!_dbus_string_copy_len (contained_type,
1237 contained_type_start, contained_type_len,
1239 sub->u.array.element_type_pos))
1240 _dbus_assert_not_reached ("should not have failed to insert array element typecodes");
1243 /* If the parent is an array, we hold type_pos pointing at the array element type;
1244 * otherwise advance it to reflect the array value we just recursed into
1246 if (writer->container_type != DBUS_TYPE_ARRAY)
1247 writer->type_pos += 1 + contained_type_len;
1249 _dbus_assert (writer->type_pos_is_expectation); /* because it's an array */
1251 /* Write the length */
1252 sub->u.array.len_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 4);
1254 if (!_dbus_type_writer_write_basic_no_typecode (sub, DBUS_TYPE_UINT32,
1256 _dbus_assert_not_reached ("should not have failed to insert array len");
1258 _dbus_assert (sub->u.array.len_pos == sub->value_pos - 4);
1260 /* Write alignment padding for array elements
1261 * Note that we write the padding *even for empty arrays*
1262 * to avoid wonky special cases
1264 alignment = element_type_get_alignment (contained_type, contained_type_start);
1266 aligned = _DBUS_ALIGN_VALUE (sub->value_pos, alignment);
1267 if (aligned != sub->value_pos)
1269 if (!_dbus_string_insert_bytes (sub->value_str,
1271 aligned - sub->value_pos,
1273 _dbus_assert_not_reached ("should not have failed to insert alignment padding");
1275 sub->value_pos = aligned;
1277 sub->u.array.start_pos = sub->value_pos;
1279 _dbus_assert (sub->u.array.start_pos == sub->value_pos);
1280 _dbus_assert (sub->u.array.len_pos < sub->u.array.start_pos);
1282 #if RECURSIVE_MARSHAL_TRACE
1283 _dbus_verbose (" type writer %p recurse array done remaining sig '%s' array start_pos = %d len_pos = %d\n", sub,
1284 _dbus_string_get_const_data_len (sub->type_str, sub->type_pos, 0),
1285 sub->u.array.start_pos, sub->u.array.len_pos);
1291 /* Variant value will normally have:
1292 * 1 byte signature length not including nul
1293 * signature typecodes (nul terminated)
1294 * padding to 8-boundary
1295 * body according to signature
1297 * The signature string can only have a single type
1298 * in it but that type may be complex/recursive.
1300 * So a typical variant type with the integer 3 will have these
1302 * 0x1 'i' '\0' [padding to 8-boundary] 0x0 0x0 0x0 0x3
1304 * For an array of 4-byte types stuffed into variants, the padding to
1305 * 8-boundary is only the 1 byte that is required for the 4-boundary
1306 * anyhow for all array elements after the first one. And for single
1307 * variants in isolation, wasting a few bytes is hardly a big deal.
1309 * The main world of hurt for writing out a variant is that the type
1310 * string is the same string as the value string. Which means
1311 * inserting to the type string will move the value_pos; and it means
1312 * that inserting to the type string could break type alignment.
1314 * This type alignment issue is why the body of the variant is always
1315 * 8-aligned. Then we know that re-8-aligning the start of the body
1316 * will always correctly align the full contents of the variant type.
1319 writer_recurse_variant (DBusTypeWriter *writer,
1320 const DBusString *contained_type,
1321 int contained_type_start,
1322 int contained_type_len,
1323 DBusTypeWriter *sub)
1325 /* Allocate space for the worst case, which is 1 byte sig
1326 * length, nul byte at end of sig, and 7 bytes padding to
1329 if (!_dbus_string_alloc_space (sub->value_str, contained_type_len + 9))
1332 /* write VARIANT typecode to the parent's type string */
1333 if (!write_or_verify_typecode (writer, DBUS_TYPE_VARIANT))
1336 if (!_dbus_string_insert_byte (sub->value_str,
1338 contained_type_len))
1339 _dbus_assert_not_reached ("should not have failed to insert variant type sig len");
1341 sub->value_pos += 1;
1343 /* Here we switch over to the expected type sig we're about to write */
1344 sub->type_str = sub->value_str;
1345 sub->type_pos = sub->value_pos;
1347 if (!_dbus_string_copy_len (contained_type, contained_type_start, contained_type_len,
1348 sub->value_str, sub->value_pos))
1349 _dbus_assert_not_reached ("should not have failed to insert variant type sig");
1351 sub->value_pos += contained_type_len;
1353 if (!_dbus_string_insert_byte (sub->value_str,
1356 _dbus_assert_not_reached ("should not have failed to insert variant type nul termination");
1358 sub->value_pos += 1;
1360 if (!_dbus_string_insert_bytes (sub->value_str,
1362 _DBUS_ALIGN_VALUE (sub->value_pos, 8) - sub->value_pos,
1364 _dbus_assert_not_reached ("should not have failed to insert alignment padding for variant body");
1365 sub->value_pos = _DBUS_ALIGN_VALUE (sub->value_pos, 8);
1371 _dbus_type_writer_recurse_contained_len (DBusTypeWriter *writer,
1373 const DBusString *contained_type,
1374 int contained_type_start,
1375 int contained_type_len,
1376 DBusTypeWriter *sub)
1378 writer_recurse_init_and_check (writer, container_type, sub);
1380 switch (container_type)
1382 case DBUS_TYPE_STRUCT:
1383 return writer_recurse_struct (writer,
1384 contained_type, contained_type_start, contained_type_len,
1387 case DBUS_TYPE_ARRAY:
1388 return writer_recurse_array (writer,
1389 contained_type, contained_type_start, contained_type_len,
1392 case DBUS_TYPE_VARIANT:
1393 return writer_recurse_variant (writer,
1394 contained_type, contained_type_start, contained_type_len,
1398 _dbus_assert_not_reached ("tried to recurse into type that doesn't support that");
1405 _dbus_type_writer_recurse (DBusTypeWriter *writer,
1407 const DBusString *contained_type,
1408 int contained_type_start,
1409 DBusTypeWriter *sub)
1411 int contained_type_len;
1414 contained_type_len = find_len_of_complete_type (contained_type, contained_type_start);
1416 contained_type_len = 0;
1418 return _dbus_type_writer_recurse_contained_len (writer, container_type,
1420 contained_type_start,
1426 _dbus_type_writer_unrecurse (DBusTypeWriter *writer,
1427 DBusTypeWriter *sub)
1429 _dbus_assert (sub->type_pos > 0); /* can't be recursed if this fails */
1431 /* type_pos_is_expectation never gets unset once set, or we'd get all hosed */
1432 _dbus_assert (!writer->type_pos_is_expectation ||
1433 (writer->type_pos_is_expectation && sub->type_pos_is_expectation));
1435 #if RECURSIVE_MARSHAL_TRACE
1436 _dbus_verbose (" type writer %p unrecurse type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
1437 writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation,
1438 _dbus_type_to_string (writer->container_type));
1439 _dbus_verbose (" type writer %p unrecurse sub type_pos = %d value_pos = %d is_expectation = %d container_type = %s\n",
1440 sub, sub->type_pos, sub->value_pos,
1441 sub->type_pos_is_expectation,
1442 _dbus_type_to_string (sub->container_type));
1445 if (sub->container_type == DBUS_TYPE_STRUCT)
1447 if (!write_or_verify_typecode (sub, DBUS_STRUCT_END_CHAR))
1450 else if (sub->container_type == DBUS_TYPE_ARRAY)
1454 /* Set the array length */
1455 len = sub->value_pos - sub->u.array.start_pos;
1456 _dbus_marshal_set_uint32 (sub->value_str,
1457 sub->u.array.len_pos,
1460 #if RECURSIVE_MARSHAL_TRACE
1461 _dbus_verbose (" filled in sub array len to %u at len_pos %d\n",
1462 len, sub->u.array.len_pos);
1466 /* Now get type_pos right for the parent writer. Here are the cases:
1468 * Cases !writer->type_pos_is_expectation:
1469 * (in these cases we want to update to the new insertion point)
1471 * - if we recursed into a STRUCT then we didn't know in advance
1472 * what the types in the struct would be; so we have to fill in
1473 * that information now.
1474 * writer->type_pos = sub->type_pos
1476 * - if we recursed into anything else, we knew the full array
1477 * type, or knew the single typecode marking VARIANT, so
1478 * writer->type_pos is already correct.
1479 * writer->type_pos should remain as-is
1481 * - note that the parent is never an ARRAY or VARIANT, if it were
1482 * then type_pos_is_expectation would be TRUE. The parent
1483 * is thus known to be a toplevel or STRUCT.
1485 * Cases where writer->type_pos_is_expectation:
1486 * (in these cases we want to update to next expected type to write)
1488 * - we recursed from STRUCT into STRUCT and we didn't increment
1489 * type_pos in the parent just to stay consistent with the
1490 * !writer->type_pos_is_expectation case (though we could
1491 * special-case this in recurse_struct instead if we wanted)
1492 * writer->type_pos = sub->type_pos
1494 * - we recursed from STRUCT into ARRAY or VARIANT and type_pos
1495 * for parent should have been incremented already
1496 * writer->type_pos should remain as-is
1498 * - we recursed from ARRAY into a sub-element, so type_pos in the
1499 * parent is the element type and should remain the element type
1500 * for the benefit of the next child element
1501 * writer->type_pos should remain as-is
1503 * - we recursed from VARIANT into its value, so type_pos in the
1504 * parent makes no difference since there's only one value
1505 * and we just finished writing it and won't use type_pos again
1506 * writer->type_pos should remain as-is
1508 if (sub->container_type == DBUS_TYPE_STRUCT &&
1509 (writer->container_type == DBUS_TYPE_STRUCT ||
1510 writer->container_type == DBUS_TYPE_INVALID))
1512 /* Advance the parent to the next struct field */
1513 writer->type_pos = sub->type_pos;
1516 writer->value_pos = sub->value_pos;
1518 #if RECURSIVE_MARSHAL_TRACE
1519 _dbus_verbose (" type writer %p unrecursed type_pos = %d value_pos = %d remaining sig '%s'\n",
1520 writer, writer->type_pos, writer->value_pos,
1521 _dbus_string_get_const_data_len (writer->type_str, writer->type_pos, 0));
1528 _dbus_type_writer_write_basic (DBusTypeWriter *writer,
1534 /* First ensure that our type realloc will succeed */
1535 if (!_dbus_string_alloc_space (writer->type_str, 1))
1540 if (!_dbus_type_writer_write_basic_no_typecode (writer, type, value))
1543 if (!write_or_verify_typecode (writer, type))
1544 _dbus_assert_not_reached ("failed to write typecode after prealloc");
1549 #if RECURSIVE_MARSHAL_TRACE
1550 _dbus_verbose (" type writer %p basic type_pos = %d value_pos = %d is_expectation = %d\n",
1551 writer, writer->type_pos, writer->value_pos, writer->type_pos_is_expectation);
1558 _dbus_type_writer_write_array (DBusTypeWriter *writer,
1568 * Iterate through all values in the given reader,
1569 * writing a copy of each value to the writer.
1570 * The reader will be moved forward to its end position.
1572 * @param writer the writer to copy to
1573 * @param reader the reader to copy from
1576 _dbus_type_writer_write_reader (DBusTypeWriter *writer,
1577 DBusTypeReader *reader)
1579 DBusTypeWriter orig;
1586 orig_type_len = _dbus_string_get_length (writer->type_str);
1587 orig_value_len = _dbus_string_get_length (writer->value_str);
1589 while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
1591 if (_dbus_type_is_container (current_type))
1593 DBusTypeReader subreader;
1594 DBusTypeWriter subwriter;
1595 const DBusString *sig_str;
1599 _dbus_type_reader_recurse (reader, &subreader);
1601 _dbus_type_reader_get_signature (&subreader, &sig_str,
1602 &sig_start, &sig_len);
1604 if (!_dbus_type_writer_recurse_contained_len (writer, current_type,
1605 sig_str, sig_start, sig_len,
1609 if (!_dbus_type_writer_write_reader (&subwriter, &subreader))
1612 if (!_dbus_type_writer_unrecurse (writer, &subwriter))
1619 _dbus_assert (_dbus_type_is_basic (current_type));
1621 _dbus_type_reader_read_basic (reader, &val);
1623 if (!_dbus_type_writer_write_basic (writer, current_type, &val))
1627 _dbus_type_reader_next (reader);
1633 if (!writer->type_pos_is_expectation)
1635 new_bytes = _dbus_string_get_length (writer->type_str) - orig_type_len;
1636 _dbus_string_delete (writer->type_str, orig.type_pos, new_bytes);
1638 new_bytes = _dbus_string_get_length (writer->value_str) - orig_value_len;
1639 _dbus_string_delete (writer->value_str, orig.value_pos, new_bytes);
1646 /** @} */ /* end of DBusMarshal group */
1648 #ifdef DBUS_BUILD_TESTS
1649 #include "dbus-test.h"
1650 #include "dbus-list.h"
1654 /* Whether to do the OOM stuff */
1655 #define TEST_OOM_HANDLING 0
1656 /* We do start offset 0 through 9, to get various alignment cases. Still this
1657 * obviously makes the test suite run 10x as slow.
1659 #define MAX_INITIAL_OFFSET 9
1660 /* Largest iteration count to test copying with. i.e. we only test copying with
1661 * some of the smaller data sets.
1663 #define MAX_ITERATIONS_TO_TEST_COPYING 100
1669 DBusString signature;
1679 #define N_FENCE_BYTES 5
1680 #define FENCE_BYTES_STR "abcde"
1681 #define INITIAL_PADDING_BYTE '\0'
1684 data_block_init (DataBlock *block,
1688 if (!_dbus_string_init (&block->signature))
1691 if (!_dbus_string_init (&block->body))
1693 _dbus_string_free (&block->signature);
1697 if (!_dbus_string_insert_bytes (&block->signature, 0, initial_offset,
1698 INITIAL_PADDING_BYTE) ||
1699 !_dbus_string_insert_bytes (&block->body, 0, initial_offset,
1700 INITIAL_PADDING_BYTE) ||
1701 !_dbus_string_append (&block->signature, FENCE_BYTES_STR) ||
1702 !_dbus_string_append (&block->body, FENCE_BYTES_STR))
1704 _dbus_string_free (&block->signature);
1705 _dbus_string_free (&block->body);
1709 block->byte_order = byte_order;
1710 block->initial_offset = initial_offset;
1716 data_block_save (DataBlock *block,
1717 DataBlockState *state)
1719 state->saved_sig_len = _dbus_string_get_length (&block->signature) - N_FENCE_BYTES;
1720 state->saved_body_len = _dbus_string_get_length (&block->body) - N_FENCE_BYTES;
1724 data_block_restore (DataBlock *block,
1725 DataBlockState *state)
1727 _dbus_string_delete (&block->signature,
1728 state->saved_sig_len,
1729 _dbus_string_get_length (&block->signature) - state->saved_sig_len - N_FENCE_BYTES);
1730 _dbus_string_delete (&block->body,
1731 state->saved_body_len,
1732 _dbus_string_get_length (&block->body) - state->saved_body_len - N_FENCE_BYTES);
1736 data_block_verify (DataBlock *block)
1738 if (!_dbus_string_ends_with_c_str (&block->signature,
1743 offset = _dbus_string_get_length (&block->signature) - N_FENCE_BYTES - 8;
1747 _dbus_verbose_bytes_of_string (&block->signature,
1749 _dbus_string_get_length (&block->signature) - offset);
1750 _dbus_assert_not_reached ("block did not verify: bad bytes at end of signature");
1752 if (!_dbus_string_ends_with_c_str (&block->body,
1757 offset = _dbus_string_get_length (&block->body) - N_FENCE_BYTES - 8;
1761 _dbus_verbose_bytes_of_string (&block->body,
1763 _dbus_string_get_length (&block->body) - offset);
1764 _dbus_assert_not_reached ("block did not verify: bad bytes at end of body");
1767 _dbus_assert (_dbus_string_validate_nul (&block->signature,
1768 0, block->initial_offset));
1769 _dbus_assert (_dbus_string_validate_nul (&block->body,
1770 0, block->initial_offset));
1774 data_block_free (DataBlock *block)
1776 data_block_verify (block);
1778 _dbus_string_free (&block->signature);
1779 _dbus_string_free (&block->body);
1783 data_block_reset (DataBlock *block)
1785 data_block_verify (block);
1787 _dbus_string_delete (&block->signature,
1788 block->initial_offset,
1789 _dbus_string_get_length (&block->signature) - N_FENCE_BYTES - block->initial_offset);
1790 _dbus_string_delete (&block->body,
1791 block->initial_offset,
1792 _dbus_string_get_length (&block->body) - N_FENCE_BYTES - block->initial_offset);
1794 data_block_verify (block);
1798 data_block_init_reader_writer (DataBlock *block,
1799 DBusTypeReader *reader,
1800 DBusTypeWriter *writer)
1803 _dbus_type_reader_init (reader,
1806 block->initial_offset,
1808 block->initial_offset);
1811 _dbus_type_writer_init (writer,
1814 _dbus_string_get_length (&block->signature) - N_FENCE_BYTES,
1816 _dbus_string_get_length (&block->body) - N_FENCE_BYTES);
1820 real_check_expected_type (DBusTypeReader *reader,
1822 const char *funcname,
1827 t = _dbus_type_reader_get_current_type (reader);
1831 _dbus_warn ("Read type %s while expecting %s at %s line %d\n",
1832 _dbus_type_to_string (t),
1833 _dbus_type_to_string (expected),
1840 #define check_expected_type(reader, expected) real_check_expected_type (reader, expected, _DBUS_FUNCTION_NAME, __LINE__)
1842 #define NEXT_EXPECTING_TRUE(reader) do { if (!_dbus_type_reader_next (reader)) \
1844 _dbus_warn ("_dbus_type_reader_next() should have returned TRUE at %s %d\n", \
1845 _DBUS_FUNCTION_NAME, __LINE__); \
1846 _dbus_assert_not_reached ("test failed"); \
1850 #define NEXT_EXPECTING_FALSE(reader) do { if (_dbus_type_reader_next (reader)) \
1852 _dbus_warn ("_dbus_type_reader_next() should have returned FALSE at %s %d\n", \
1853 _DBUS_FUNCTION_NAME, __LINE__); \
1854 _dbus_assert_not_reached ("test failed"); \
1856 check_expected_type (reader, DBUS_TYPE_INVALID); \
1859 typedef struct TestTypeNode TestTypeNode;
1860 typedef struct TestTypeNodeClass TestTypeNodeClass;
1861 typedef struct TestTypeNodeContainer TestTypeNodeContainer;
1862 typedef struct TestTypeNodeContainerClass TestTypeNodeContainerClass;
1866 const TestTypeNodeClass *klass;
1869 struct TestTypeNodeContainer
1875 struct TestTypeNodeClass
1881 int subclass_detail; /* a bad hack to avoid a bunch of subclass casting */
1883 dbus_bool_t (* construct) (TestTypeNode *node);
1884 void (* destroy) (TestTypeNode *node);
1886 dbus_bool_t (* write_value) (TestTypeNode *node,
1888 DBusTypeWriter *writer,
1890 dbus_bool_t (* read_value) (TestTypeNode *node,
1892 DBusTypeReader *reader,
1894 dbus_bool_t (* build_signature) (TestTypeNode *node,
1898 struct TestTypeNodeContainerClass
1900 TestTypeNodeClass base;
1903 static dbus_bool_t int32_write_value (TestTypeNode *node,
1905 DBusTypeWriter *writer,
1907 static dbus_bool_t int32_read_value (TestTypeNode *node,
1909 DBusTypeReader *reader,
1911 static dbus_bool_t int64_write_value (TestTypeNode *node,
1913 DBusTypeWriter *writer,
1915 static dbus_bool_t int64_read_value (TestTypeNode *node,
1917 DBusTypeReader *reader,
1919 static dbus_bool_t string_write_value (TestTypeNode *node,
1921 DBusTypeWriter *writer,
1923 static dbus_bool_t string_read_value (TestTypeNode *node,
1925 DBusTypeReader *reader,
1927 static dbus_bool_t bool_read_value (TestTypeNode *node,
1929 DBusTypeReader *reader,
1931 static dbus_bool_t bool_write_value (TestTypeNode *node,
1933 DBusTypeWriter *writer,
1935 static dbus_bool_t byte_read_value (TestTypeNode *node,
1937 DBusTypeReader *reader,
1939 static dbus_bool_t byte_write_value (TestTypeNode *node,
1941 DBusTypeWriter *writer,
1943 static dbus_bool_t double_read_value (TestTypeNode *node,
1945 DBusTypeReader *reader,
1947 static dbus_bool_t double_write_value (TestTypeNode *node,
1949 DBusTypeWriter *writer,
1951 static dbus_bool_t object_path_read_value (TestTypeNode *node,
1953 DBusTypeReader *reader,
1955 static dbus_bool_t object_path_write_value (TestTypeNode *node,
1957 DBusTypeWriter *writer,
1959 static dbus_bool_t signature_read_value (TestTypeNode *node,
1961 DBusTypeReader *reader,
1963 static dbus_bool_t signature_write_value (TestTypeNode *node,
1965 DBusTypeWriter *writer,
1967 static dbus_bool_t struct_write_value (TestTypeNode *node,
1969 DBusTypeWriter *writer,
1971 static dbus_bool_t struct_read_value (TestTypeNode *node,
1973 DBusTypeReader *reader,
1975 static dbus_bool_t struct_build_signature (TestTypeNode *node,
1977 static dbus_bool_t array_write_value (TestTypeNode *node,
1979 DBusTypeWriter *writer,
1981 static dbus_bool_t array_read_value (TestTypeNode *node,
1983 DBusTypeReader *reader,
1985 static dbus_bool_t array_build_signature (TestTypeNode *node,
1987 static dbus_bool_t variant_write_value (TestTypeNode *node,
1989 DBusTypeWriter *writer,
1991 static dbus_bool_t variant_read_value (TestTypeNode *node,
1993 DBusTypeReader *reader,
1995 static void container_destroy (TestTypeNode *node);
1998 static const TestTypeNodeClass int32_class = {
2000 sizeof (TestTypeNode),
2009 static const TestTypeNodeClass uint32_class = {
2011 sizeof (TestTypeNode),
2015 int32_write_value, /* recycle from int32 */
2016 int32_read_value, /* recycle from int32 */
2020 static const TestTypeNodeClass int64_class = {
2022 sizeof (TestTypeNode),
2031 static const TestTypeNodeClass uint64_class = {
2033 sizeof (TestTypeNode),
2037 int64_write_value, /* recycle from int64 */
2038 int64_read_value, /* recycle from int64 */
2042 static const TestTypeNodeClass string_0_class = {
2044 sizeof (TestTypeNode),
2045 0, /* string length */
2053 static const TestTypeNodeClass string_1_class = {
2055 sizeof (TestTypeNode),
2056 1, /* string length */
2064 /* with nul, a len 3 string should fill 4 bytes and thus is "special" */
2065 static const TestTypeNodeClass string_3_class = {
2067 sizeof (TestTypeNode),
2068 3, /* string length */
2076 /* with nul, a len 8 string should fill 9 bytes and thus is "special" (far-fetched I suppose) */
2077 static const TestTypeNodeClass string_8_class = {
2079 sizeof (TestTypeNode),
2080 8, /* string length */
2088 static const TestTypeNodeClass bool_class = {
2090 sizeof (TestTypeNode),
2099 static const TestTypeNodeClass byte_class = {
2101 sizeof (TestTypeNode),
2110 static const TestTypeNodeClass double_class = {
2112 sizeof (TestTypeNode),
2121 static const TestTypeNodeClass object_path_class = {
2122 DBUS_TYPE_OBJECT_PATH,
2123 sizeof (TestTypeNode),
2127 object_path_write_value,
2128 object_path_read_value,
2132 static const TestTypeNodeClass signature_class = {
2133 DBUS_TYPE_SIGNATURE,
2134 sizeof (TestTypeNode),
2138 signature_write_value,
2139 signature_read_value,
2143 static const TestTypeNodeClass struct_1_class = {
2145 sizeof (TestTypeNodeContainer),
2146 1, /* number of times children appear as fields */
2151 struct_build_signature
2154 static const TestTypeNodeClass struct_2_class = {
2156 sizeof (TestTypeNodeContainer),
2157 2, /* number of times children appear as fields */
2162 struct_build_signature
2165 static const TestTypeNodeClass array_0_class = {
2167 sizeof (TestTypeNodeContainer),
2168 0, /* number of array elements */
2173 array_build_signature
2176 static const TestTypeNodeClass array_1_class = {
2178 sizeof (TestTypeNodeContainer),
2179 1, /* number of array elements */
2184 array_build_signature
2187 static const TestTypeNodeClass array_2_class = {
2189 sizeof (TestTypeNodeContainer),
2190 2, /* number of array elements */
2195 array_build_signature
2198 static const TestTypeNodeClass array_9_class = {
2200 sizeof (TestTypeNodeContainer),
2201 9, /* number of array elements */
2206 array_build_signature
2209 static const TestTypeNodeClass variant_class = {
2211 sizeof (TestTypeNodeContainer),
2215 variant_write_value,
2220 static const TestTypeNodeClass* const
2236 #define N_BASICS (_DBUS_N_ELEMENTS (basic_nodes))
2238 static const TestTypeNodeClass* const
2239 container_nodes[] = {
2246 /* array_9_class is omitted on purpose, it's too slow;
2247 * we only use it in one hardcoded test below
2250 #define N_CONTAINERS (_DBUS_N_ELEMENTS (container_nodes))
2252 static TestTypeNode*
2253 node_new (const TestTypeNodeClass *klass)
2257 node = dbus_malloc0 (klass->instance_size);
2261 node->klass = klass;
2263 if (klass->construct)
2265 if (!(* klass->construct) (node))
2276 node_destroy (TestTypeNode *node)
2278 if (node->klass->destroy)
2279 (* node->klass->destroy) (node);
2284 node_write_value (TestTypeNode *node,
2286 DBusTypeWriter *writer,
2291 retval = (* node->klass->write_value) (node, block, writer, seed);
2294 /* Handy to see where things break, but too expensive to do all the time */
2295 data_block_verify (block);
2302 node_read_value (TestTypeNode *node,
2304 DBusTypeReader *reader,
2308 DBusTypeReader restored;
2310 _dbus_type_reader_save_mark (reader, &mark);
2312 if (!(* node->klass->read_value) (node, block, reader, seed))
2315 _dbus_type_reader_init_from_mark (&restored,
2316 reader->byte_order, /* a bit of a cheat,
2317 * since we didn't bother
2318 * to store this in DataBlock
2324 if (!(* node->klass->read_value) (node, block, &restored, seed))
2331 node_build_signature (TestTypeNode *node,
2334 if (node->klass->build_signature)
2335 return (* node->klass->build_signature) (node, str);
2337 return _dbus_string_append_byte (str, node->klass->typecode);
2341 node_append_child (TestTypeNode *node,
2342 TestTypeNode *child)
2344 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
2346 _dbus_assert (node->klass->instance_size >= (int) sizeof (TestTypeNodeContainer));
2348 if (!_dbus_list_append (&container->children, child))
2349 _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 */
2355 run_test_copy (DataBlock *src)
2359 DBusTypeReader reader;
2360 DBusTypeWriter writer;
2364 if (!data_block_init (&dest, src->byte_order, src->initial_offset))
2367 data_block_init_reader_writer (src, &reader, NULL);
2368 data_block_init_reader_writer (&dest, NULL, &writer);
2370 /* DBusTypeWriter assumes it's writing into an existing signature,
2371 * so doesn't add nul on its own. We have to do that.
2373 if (!_dbus_string_insert_byte (&dest.signature,
2374 dest.initial_offset, '\0'))
2377 if (!_dbus_type_writer_write_reader (&writer, &reader))
2380 /* Data blocks should now be identical */
2381 if (!_dbus_string_equal (&src->signature, &dest.signature))
2383 _dbus_verbose ("SOURCE\n");
2384 _dbus_verbose_bytes_of_string (&src->signature, 0,
2385 _dbus_string_get_length (&src->signature));
2386 _dbus_verbose ("DEST\n");
2387 _dbus_verbose_bytes_of_string (&dest.signature, 0,
2388 _dbus_string_get_length (&dest.signature));
2389 _dbus_assert_not_reached ("signatures did not match");
2392 if (!_dbus_string_equal (&src->body, &dest.body))
2394 _dbus_verbose ("SOURCE\n");
2395 _dbus_verbose_bytes_of_string (&src->body, 0,
2396 _dbus_string_get_length (&src->body));
2397 _dbus_verbose ("DEST\n");
2398 _dbus_verbose_bytes_of_string (&dest.body, 0,
2399 _dbus_string_get_length (&dest.body));
2400 _dbus_assert_not_reached ("bodies did not match");
2407 data_block_free (&dest);
2412 static int n_iterations_completed_total = 0;
2413 static int n_iterations_completed_this_test = 0;
2414 static int n_iterations_expected_this_test = 0;
2418 const DBusString *signature;
2421 TestTypeNode **nodes;
2423 } NodeIterationData;
2426 run_test_nodes_iteration (void *data)
2428 NodeIterationData *nid = data;
2429 DBusTypeReader reader;
2430 DBusTypeWriter writer;
2435 * 1. write the value
2436 * 2. strcmp-compare with the signature we built
2438 * 4. type-iterate the signature and the value and see if they are the same type-wise
2442 data_block_init_reader_writer (nid->block,
2445 /* DBusTypeWriter assumes it's writing into an existing signature,
2446 * so doesn't add nul on its own. We have to do that.
2448 if (!_dbus_string_insert_byte (&nid->block->signature,
2449 nid->type_offset, '\0'))
2453 while (i < nid->n_nodes)
2455 if (!node_write_value (nid->nodes[i], nid->block, &writer, i))
2461 if (!_dbus_string_equal_substring (nid->signature, 0, _dbus_string_get_length (nid->signature),
2462 &nid->block->signature, nid->type_offset))
2464 _dbus_warn ("Expected signature '%s' and got '%s' with initial offset %d\n",
2465 _dbus_string_get_const_data (nid->signature),
2466 _dbus_string_get_const_data_len (&nid->block->signature, nid->type_offset, 0),
2468 _dbus_assert_not_reached ("wrong signature");
2472 while (i < nid->n_nodes)
2474 if (!node_read_value (nid->nodes[i], nid->block, &reader, i))
2477 if (i + 1 == nid->n_nodes)
2478 NEXT_EXPECTING_FALSE (&reader);
2480 NEXT_EXPECTING_TRUE (&reader);
2485 if (n_iterations_expected_this_test <= MAX_ITERATIONS_TO_TEST_COPYING)
2486 run_test_copy (nid->block);
2488 /* FIXME type-iterate both signature and value and compare the resulting
2489 * tree to the node tree perhaps
2496 data_block_reset (nid->block);
2502 run_test_nodes_in_one_configuration (TestTypeNode **nodes,
2504 const DBusString *signature,
2509 NodeIterationData nid;
2511 if (!data_block_init (&block, byte_order, initial_offset))
2512 _dbus_assert_not_reached ("no memory");
2514 nid.signature = signature;
2516 nid.type_offset = initial_offset;
2518 nid.n_nodes = n_nodes;
2520 #if TEST_OOM_HANDLING
2521 _dbus_test_oom_handling ("running test node",
2522 run_test_nodes_iteration,
2525 if (!run_test_nodes_iteration (&nid))
2526 _dbus_assert_not_reached ("no memory");
2529 data_block_free (&block);
2533 run_test_nodes (TestTypeNode **nodes,
2537 DBusString signature;
2539 if (!_dbus_string_init (&signature))
2540 _dbus_assert_not_reached ("no memory");
2545 if (! node_build_signature (nodes[i], &signature))
2546 _dbus_assert_not_reached ("no memory");
2551 _dbus_verbose (">>> test nodes with signature '%s'\n",
2552 _dbus_string_get_const_data (&signature));
2555 while (i <= MAX_INITIAL_OFFSET)
2557 run_test_nodes_in_one_configuration (nodes, n_nodes, &signature,
2558 DBUS_LITTLE_ENDIAN, i);
2559 run_test_nodes_in_one_configuration (nodes, n_nodes, &signature,
2560 DBUS_BIG_ENDIAN, i);
2565 n_iterations_completed_this_test += 1;
2566 n_iterations_completed_total += 1;
2568 if (n_iterations_completed_this_test == n_iterations_expected_this_test)
2570 fprintf (stderr, " 100%% %d this test (%d cumulative)\n",
2571 n_iterations_completed_this_test,
2572 n_iterations_completed_total);
2574 /* this happens to turn out well with mod == 1 */
2575 else if ((n_iterations_completed_this_test %
2576 (int)(n_iterations_expected_this_test / 10.0)) == 1)
2578 fprintf (stderr, " %d%% ", (int) (n_iterations_completed_this_test / (double) n_iterations_expected_this_test * 100));
2581 _dbus_string_free (&signature);
2584 #define N_VALUES (N_BASICS * N_CONTAINERS + N_BASICS)
2586 static TestTypeNode*
2587 value_generator (int *ip)
2590 const TestTypeNodeClass *child_klass;
2591 const TestTypeNodeClass *container_klass;
2592 TestTypeNode *child;
2595 _dbus_assert (i <= N_VALUES);
2601 else if (i < N_BASICS)
2603 node = node_new (basic_nodes[i]);
2607 /* imagine an array:
2608 * container 0 of basic 0
2609 * container 0 of basic 1
2610 * container 0 of basic 2
2611 * container 1 of basic 0
2612 * container 1 of basic 1
2613 * container 1 of basic 2
2617 container_klass = container_nodes[i / N_BASICS];
2618 child_klass = basic_nodes[i % N_BASICS];
2620 node = node_new (container_klass);
2621 child = node_new (child_klass);
2623 node_append_child (node, child);
2626 *ip += 1; /* increment the generator */
2632 make_and_run_values_inside_container (const TestTypeNodeClass *container_klass,
2636 TestTypeNode *container;
2637 TestTypeNode *child;
2640 root = node_new (container_klass);
2642 for (i = 1; i < n_nested; i++)
2644 child = node_new (container_klass);
2645 node_append_child (container, child);
2649 /* container should now be the most-nested container */
2652 while ((child = value_generator (&i)))
2654 node_append_child (container, child);
2656 run_test_nodes (&root, 1);
2658 _dbus_list_clear (&((TestTypeNodeContainer*)container)->children);
2659 node_destroy (child);
2662 node_destroy (root);
2666 start_next_test (const char *format,
2669 n_iterations_completed_this_test = 0;
2670 n_iterations_expected_this_test = expected;
2672 fprintf (stderr, ">>> >>> ");
2673 fprintf (stderr, format,
2674 n_iterations_expected_this_test);
2678 make_and_run_test_nodes (void)
2682 /* We try to do this in order of "complicatedness" so that test
2683 * failures tend to show up in the simplest test case that
2684 * demonstrates the failure. There are also some tests that run
2685 * more than once for this reason, first while going through simple
2686 * cases, second while going through a broader range of complex
2689 /* Each basic node. The basic nodes should include:
2691 * - each fixed-size type (in such a way that it has different values each time,
2692 * so we can tell if we mix two of them up)
2693 * - strings of various lengths
2697 /* Each container node. The container nodes should include:
2699 * struct with 1 and 2 copies of the contained item
2700 * array with 0, 1, 2 copies of the contained item
2703 /* Let a "value" be a basic node, or a container containing a single basic node.
2704 * Let n_values be the number of such values i.e. (n_container * n_basic + n_basic)
2705 * When iterating through all values to make combinations, do the basic types
2706 * first and the containers second.
2708 /* Each item is shown with its number of iterations to complete so
2709 * we can keep a handle on this unit test
2712 /* FIXME test just an empty body, no types at all */
2714 start_next_test ("Each value by itself %d iterations\n", N_VALUES);
2718 while ((node = value_generator (&i)))
2720 run_test_nodes (&node, 1);
2722 node_destroy (node);
2726 start_next_test ("All values in one big toplevel %d iteration\n", 1);
2728 TestTypeNode *nodes[N_VALUES];
2731 while ((nodes[i] = value_generator (&i)))
2734 run_test_nodes (nodes, N_VALUES);
2736 for (i = 0; i < N_VALUES; i++)
2737 node_destroy (nodes[i]);
2740 start_next_test ("Each value,value pair combination as toplevel, in both orders %d iterations\n",
2741 N_VALUES * N_VALUES);
2743 TestTypeNode *nodes[2];
2746 while ((nodes[0] = value_generator (&i)))
2749 while ((nodes[1] = value_generator (&j)))
2751 run_test_nodes (nodes, 2);
2753 node_destroy (nodes[1]);
2756 node_destroy (nodes[0]);
2760 start_next_test ("Each container containing each value %d iterations\n",
2761 N_CONTAINERS * N_VALUES);
2762 for (i = 0; i < N_CONTAINERS; i++)
2764 const TestTypeNodeClass *container_klass = container_nodes[i];
2766 make_and_run_values_inside_container (container_klass, 1);
2769 n_iterations_completed_this_test = 0;
2770 n_iterations_expected_this_test = N_CONTAINERS * N_VALUES;
2771 _dbus_verbose (">>> >>> Each container of same container of each value %d iterations\n",
2772 n_iterations_completed_this_test);
2773 for (i = 0; i < N_CONTAINERS; i++)
2775 const TestTypeNodeClass *container_klass = container_nodes[i];
2777 make_and_run_values_inside_container (container_klass, 2);
2780 start_next_test ("Each container of same container of same container of each value %d iterations\n",
2781 N_CONTAINERS * N_VALUES);
2782 for (i = 0; i < N_CONTAINERS; i++)
2784 const TestTypeNodeClass *container_klass = container_nodes[i];
2786 make_and_run_values_inside_container (container_klass, 3);
2789 start_next_test ("Each value,value pair inside a struct %d iterations\n",
2790 N_VALUES * N_VALUES);
2792 TestTypeNode *val1, *val2;
2795 node = node_new (&struct_1_class);
2798 while ((val1 = value_generator (&i)))
2801 while ((val2 = value_generator (&j)))
2803 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
2805 node_append_child (node, val1);
2806 node_append_child (node, val2);
2808 run_test_nodes (&node, 1);
2810 _dbus_list_clear (&container->children);
2811 node_destroy (val2);
2813 node_destroy (val1);
2815 node_destroy (node);
2818 start_next_test ("All values in one big struct %d iteration\n",
2822 TestTypeNode *child;
2824 node = node_new (&struct_1_class);
2827 while ((child = value_generator (&i)))
2828 node_append_child (node, child);
2830 run_test_nodes (&node, 1);
2832 node_destroy (node);
2835 start_next_test ("Each value in a large array %d iterations\n",
2841 node = node_new (&array_9_class);
2844 while ((val = value_generator (&i)))
2846 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
2848 node_append_child (node, val);
2850 run_test_nodes (&node, 1);
2852 _dbus_list_clear (&container->children);
2856 node_destroy (node);
2859 start_next_test ("Each container of each container of each value %d iterations\n",
2860 N_CONTAINERS * N_CONTAINERS * N_VALUES);
2861 for (i = 0; i < N_CONTAINERS; i++)
2863 const TestTypeNodeClass *outer_container_klass = container_nodes[i];
2864 TestTypeNode *outer_container = node_new (outer_container_klass);
2866 for (j = 0; j < N_CONTAINERS; j++)
2868 TestTypeNode *child;
2869 const TestTypeNodeClass *inner_container_klass = container_nodes[j];
2870 TestTypeNode *inner_container = node_new (inner_container_klass);
2872 node_append_child (outer_container, inner_container);
2875 while ((child = value_generator (&m)))
2877 node_append_child (inner_container, child);
2879 run_test_nodes (&outer_container, 1);
2881 _dbus_list_clear (&((TestTypeNodeContainer*)inner_container)->children);
2882 node_destroy (child);
2884 _dbus_list_clear (&((TestTypeNodeContainer*)outer_container)->children);
2885 node_destroy (inner_container);
2887 node_destroy (outer_container);
2890 start_next_test ("Each container of each container of each container of each value %d iterations\n",
2891 N_CONTAINERS * N_CONTAINERS * N_CONTAINERS * N_VALUES);
2892 for (i = 0; i < N_CONTAINERS; i++)
2894 const TestTypeNodeClass *outer_container_klass = container_nodes[i];
2895 TestTypeNode *outer_container = node_new (outer_container_klass);
2897 for (j = 0; j < N_CONTAINERS; j++)
2899 const TestTypeNodeClass *inner_container_klass = container_nodes[j];
2900 TestTypeNode *inner_container = node_new (inner_container_klass);
2902 node_append_child (outer_container, inner_container);
2904 for (k = 0; k < N_CONTAINERS; k++)
2906 TestTypeNode *child;
2907 const TestTypeNodeClass *center_container_klass = container_nodes[k];
2908 TestTypeNode *center_container = node_new (center_container_klass);
2910 node_append_child (inner_container, center_container);
2913 while ((child = value_generator (&m)))
2915 node_append_child (center_container, child);
2917 run_test_nodes (&outer_container, 1);
2919 _dbus_list_clear (&((TestTypeNodeContainer*)center_container)->children);
2920 node_destroy (child);
2922 _dbus_list_clear (&((TestTypeNodeContainer*)inner_container)->children);
2923 node_destroy (center_container);
2925 _dbus_list_clear (&((TestTypeNodeContainer*)outer_container)->children);
2926 node_destroy (inner_container);
2928 node_destroy (outer_container);
2932 /* This one takes a really long time, so comment it out for now */
2933 start_next_test ("Each value,value,value triplet combination as toplevel, in all orders %d iterations\n",
2934 N_VALUES * N_VALUES * N_VALUES);
2936 TestTypeNode *nodes[3];
2939 while ((nodes[0] = value_generator (&i)))
2942 while ((nodes[1] = value_generator (&j)))
2945 while ((nodes[2] = value_generator (&k)))
2947 run_test_nodes (nodes, 3);
2949 node_destroy (nodes[2]);
2951 node_destroy (nodes[1]);
2953 node_destroy (nodes[0]);
2956 #endif /* #if 0 expensive test */
2958 fprintf (stderr, "%d total iterations of recursive marshaling tests\n",
2959 n_iterations_completed_total);
2960 fprintf (stderr, "each iteration ran at initial offsets 0 through %d in both big and little endian\n",
2961 MAX_INITIAL_OFFSET);
2962 fprintf (stderr, "out of memory handling %s tested\n",
2963 TEST_OOM_HANDLING ? "was" : "was not");
2966 dbus_bool_t _dbus_marshal_recursive_test (void);
2969 _dbus_marshal_recursive_test (void)
2971 make_and_run_test_nodes ();
2977 dbus_bool_t _dbus_marshal_test (void);
2979 main (int argc, char **argv)
2981 _dbus_marshal_test ();
2983 _dbus_marshal_recursive_test ();
2993 * Implementations of each type node class
3000 #define SAMPLE_INT32 12345678
3001 #define SAMPLE_INT32_ALTERNATE 53781429
3003 int32_from_seed (int seed)
3005 /* Generate an integer value that's predictable from seed. We could
3006 * just use seed itself, but that would only ever touch one byte of
3007 * the int so would miss some kinds of bug.
3011 v = 42; /* just to quiet compiler afaik */
3018 v = SAMPLE_INT32_ALTERNATE;
3032 v *= seed; /* wraps around eventually, which is fine */
3038 int32_write_value (TestTypeNode *node,
3040 DBusTypeWriter *writer,
3043 /* also used for uint32 */
3046 v = int32_from_seed (seed);
3048 return _dbus_type_writer_write_basic (writer,
3049 node->klass->typecode,
3054 int32_read_value (TestTypeNode *node,
3056 DBusTypeReader *reader,
3059 /* also used for uint32 */
3062 check_expected_type (reader, node->klass->typecode);
3064 _dbus_type_reader_read_basic (reader,
3065 (dbus_int32_t*) &v);
3067 _dbus_assert (v == int32_from_seed (seed));
3072 #ifdef DBUS_HAVE_INT64
3074 int64_from_seed (int seed)
3079 v32 = int32_from_seed (seed);
3081 v = - (dbus_int32_t) ~ v32;
3082 v |= (((dbus_int64_t)v32) << 32);
3089 int64_write_value (TestTypeNode *node,
3091 DBusTypeWriter *writer,
3094 #ifdef DBUS_HAVE_INT64
3095 /* also used for uint64 */
3098 v = int64_from_seed (seed);
3100 return _dbus_type_writer_write_basic (writer,
3101 node->klass->typecode,
3109 int64_read_value (TestTypeNode *node,
3111 DBusTypeReader *reader,
3114 #ifdef DBUS_HAVE_INT64
3115 /* also used for uint64 */
3118 check_expected_type (reader, node->klass->typecode);
3120 _dbus_type_reader_read_basic (reader,
3121 (dbus_int64_t*) &v);
3123 _dbus_assert (v == int64_from_seed (seed));
3131 #define MAX_SAMPLE_STRING_LEN 10
3133 string_from_seed (char *buf,
3140 _dbus_assert (len < MAX_SAMPLE_STRING_LEN);
3142 v = (unsigned char) ('A' + seed);
3147 if (v < 'A' || v > 'z')
3160 string_write_value (TestTypeNode *node,
3162 DBusTypeWriter *writer,
3165 char buf[MAX_SAMPLE_STRING_LEN];
3166 const char *v_string = buf;
3168 string_from_seed (buf, node->klass->subclass_detail,
3171 return _dbus_type_writer_write_basic (writer,
3172 node->klass->typecode,
3177 string_read_value (TestTypeNode *node,
3179 DBusTypeReader *reader,
3183 char buf[MAX_SAMPLE_STRING_LEN];
3185 check_expected_type (reader, node->klass->typecode);
3187 _dbus_type_reader_read_basic (reader,
3188 (const char **) &v);
3190 string_from_seed (buf, node->klass->subclass_detail,
3193 if (strcmp (buf, v) != 0)
3195 _dbus_warn ("read string '%s' expected '%s'\n",
3197 _dbus_assert_not_reached ("test failed");
3203 #define BOOL_FROM_SEED(seed) (seed % 2)
3206 bool_write_value (TestTypeNode *node,
3208 DBusTypeWriter *writer,
3213 v = BOOL_FROM_SEED (seed);
3215 return _dbus_type_writer_write_basic (writer,
3216 node->klass->typecode,
3221 bool_read_value (TestTypeNode *node,
3223 DBusTypeReader *reader,
3228 check_expected_type (reader, node->klass->typecode);
3230 _dbus_type_reader_read_basic (reader,
3231 (unsigned char*) &v);
3233 _dbus_assert (v == BOOL_FROM_SEED (seed));
3238 #define BYTE_FROM_SEED(seed) ((unsigned char) int32_from_seed (seed))
3241 byte_write_value (TestTypeNode *node,
3243 DBusTypeWriter *writer,
3248 v = BYTE_FROM_SEED (seed);
3250 return _dbus_type_writer_write_basic (writer,
3251 node->klass->typecode,
3256 byte_read_value (TestTypeNode *node,
3258 DBusTypeReader *reader,
3263 check_expected_type (reader, node->klass->typecode);
3265 _dbus_type_reader_read_basic (reader,
3266 (unsigned char*) &v);
3268 _dbus_assert (v == BYTE_FROM_SEED (seed));
3274 double_from_seed (int seed)
3276 return SAMPLE_INT32 * (double) seed + 0.3;
3280 double_write_value (TestTypeNode *node,
3282 DBusTypeWriter *writer,
3287 v = double_from_seed (seed);
3289 return _dbus_type_writer_write_basic (writer,
3290 node->klass->typecode,
3295 double_read_value (TestTypeNode *node,
3297 DBusTypeReader *reader,
3303 check_expected_type (reader, node->klass->typecode);
3305 _dbus_type_reader_read_basic (reader,
3308 expected = double_from_seed (seed);
3310 if (!_DBUS_DOUBLES_BITWISE_EQUAL (v, expected))
3312 #ifdef DBUS_HAVE_INT64
3313 _dbus_warn ("Expected double %g got %g\n bits = 0x%llx vs.\n bits = 0x%llx)\n",
3315 *(dbus_uint64_t*)&expected,
3316 *(dbus_uint64_t*)&v);
3318 _dbus_assert_not_reached ("test failed");
3325 #define MAX_SAMPLE_OBJECT_PATH_LEN 10
3327 object_path_from_seed (char *buf,
3333 v = (unsigned char) ('A' + seed);
3338 if (v < 'A' || v > 'z')
3353 object_path_write_value (TestTypeNode *node,
3355 DBusTypeWriter *writer,
3358 char buf[MAX_SAMPLE_OBJECT_PATH_LEN];
3359 const char *v_string = buf;
3361 object_path_from_seed (buf, seed);
3363 return _dbus_type_writer_write_basic (writer,
3364 node->klass->typecode,
3369 object_path_read_value (TestTypeNode *node,
3371 DBusTypeReader *reader,
3375 char buf[MAX_SAMPLE_OBJECT_PATH_LEN];
3377 check_expected_type (reader, node->klass->typecode);
3379 _dbus_type_reader_read_basic (reader,
3380 (const char **) &v);
3382 object_path_from_seed (buf, seed);
3384 if (strcmp (buf, v) != 0)
3386 _dbus_warn ("read object path '%s' expected '%s'\n",
3388 _dbus_assert_not_reached ("test failed");
3395 #define MAX_SAMPLE_SIGNATURE_LEN 10
3397 signature_from_seed (char *buf,
3402 const char *sample_signatures[] = {
3410 s = sample_signatures[seed % _DBUS_N_ELEMENTS(sample_signatures)];
3412 for (i = 0; s[i]; i++)
3420 signature_write_value (TestTypeNode *node,
3422 DBusTypeWriter *writer,
3425 char buf[MAX_SAMPLE_SIGNATURE_LEN];
3426 const char *v_string = buf;
3428 signature_from_seed (buf, seed);
3430 return _dbus_type_writer_write_basic (writer,
3431 node->klass->typecode,
3436 signature_read_value (TestTypeNode *node,
3438 DBusTypeReader *reader,
3442 char buf[MAX_SAMPLE_SIGNATURE_LEN];
3444 check_expected_type (reader, node->klass->typecode);
3446 _dbus_type_reader_read_basic (reader,
3447 (const char **) &v);
3449 signature_from_seed (buf, seed);
3451 if (strcmp (buf, v) != 0)
3453 _dbus_warn ("read signature value '%s' expected '%s'\n",
3455 _dbus_assert_not_reached ("test failed");
3462 struct_write_value (TestTypeNode *node,
3464 DBusTypeWriter *writer,
3467 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3468 DataBlockState saved;
3473 n_copies = node->klass->subclass_detail;
3475 _dbus_assert (container->children != NULL);
3477 data_block_save (block, &saved);
3479 if (!_dbus_type_writer_recurse (writer, DBUS_TYPE_STRUCT,
3485 while (i < n_copies)
3489 link = _dbus_list_get_first_link (&container->children);
3490 while (link != NULL)
3492 TestTypeNode *child = link->data;
3493 DBusList *next = _dbus_list_get_next_link (&container->children, link);
3495 if (!node_write_value (child, block, &sub, i))
3497 data_block_restore (block, &saved);
3507 if (!_dbus_type_writer_unrecurse (writer, &sub))
3509 data_block_restore (block, &saved);
3517 struct_read_value (TestTypeNode *node,
3519 DBusTypeReader *reader,
3522 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3527 n_copies = node->klass->subclass_detail;
3529 check_expected_type (reader, DBUS_TYPE_STRUCT);
3531 _dbus_type_reader_recurse (reader, &sub);
3534 while (i < n_copies)
3538 link = _dbus_list_get_first_link (&container->children);
3539 while (link != NULL)
3541 TestTypeNode *child = link->data;
3542 DBusList *next = _dbus_list_get_next_link (&container->children, link);
3544 if (!node_read_value (child, block, &sub, i))
3547 if (i == (n_copies - 1) && next == NULL)
3548 NEXT_EXPECTING_FALSE (&sub);
3550 NEXT_EXPECTING_TRUE (&sub);
3562 struct_build_signature (TestTypeNode *node,
3565 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3570 n_copies = node->klass->subclass_detail;
3572 orig_len = _dbus_string_get_length (str);
3574 if (!_dbus_string_append_byte (str, DBUS_STRUCT_BEGIN_CHAR))
3578 while (i < n_copies)
3582 link = _dbus_list_get_first_link (&container->children);
3583 while (link != NULL)
3585 TestTypeNode *child = link->data;
3586 DBusList *next = _dbus_list_get_next_link (&container->children, link);
3588 if (!node_build_signature (child, str))
3597 if (!_dbus_string_append_byte (str, DBUS_STRUCT_END_CHAR))
3603 _dbus_string_set_length (str, orig_len);
3608 array_write_value (TestTypeNode *node,
3610 DBusTypeWriter *writer,
3613 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3614 DataBlockState saved;
3616 DBusString element_signature;
3620 n_copies = node->klass->subclass_detail;
3622 _dbus_assert (container->children != NULL);
3624 data_block_save (block, &saved);
3626 if (!_dbus_string_init (&element_signature))
3629 if (!node_build_signature (_dbus_list_get_first (&container->children),
3630 &element_signature))
3633 if (!_dbus_type_writer_recurse (writer, DBUS_TYPE_ARRAY,
3634 &element_signature, 0,
3639 while (i < n_copies)
3643 link = _dbus_list_get_first_link (&container->children);
3644 while (link != NULL)
3646 TestTypeNode *child = link->data;
3647 DBusList *next = _dbus_list_get_next_link (&container->children, link);
3649 if (!node_write_value (child, block, &sub, i))
3658 if (!_dbus_type_writer_unrecurse (writer, &sub))
3661 _dbus_string_free (&element_signature);
3665 data_block_restore (block, &saved);
3666 _dbus_string_free (&element_signature);
3671 array_read_value (TestTypeNode *node,
3673 DBusTypeReader *reader,
3676 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3681 n_copies = node->klass->subclass_detail;
3683 check_expected_type (reader, DBUS_TYPE_ARRAY);
3687 _dbus_assert (!_dbus_type_reader_array_is_empty (reader));
3689 _dbus_type_reader_recurse (reader, &sub);
3692 while (i < n_copies)
3696 link = _dbus_list_get_first_link (&container->children);
3697 while (link != NULL)
3699 TestTypeNode *child = link->data;
3700 DBusList *next = _dbus_list_get_next_link (&container->children, link);
3702 if (!node_read_value (child, block, &sub, i))
3705 if (i == (n_copies - 1) && next == NULL)
3706 NEXT_EXPECTING_FALSE (&sub);
3708 NEXT_EXPECTING_TRUE (&sub);
3718 _dbus_assert (_dbus_type_reader_array_is_empty (reader));
3725 array_build_signature (TestTypeNode *node,
3728 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3731 orig_len = _dbus_string_get_length (str);
3733 if (!_dbus_string_append_byte (str, DBUS_TYPE_ARRAY))
3736 if (!node_build_signature (_dbus_list_get_first (&container->children),
3743 _dbus_string_set_length (str, orig_len);
3747 /* 10 is random just to add another seed that we use in the suite */
3748 #define VARIANT_SEED 10
3751 variant_write_value (TestTypeNode *node,
3753 DBusTypeWriter *writer,
3756 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3757 DataBlockState saved;
3759 DBusString content_signature;
3760 TestTypeNode *child;
3762 _dbus_assert (container->children != NULL);
3763 _dbus_assert (_dbus_list_length_is_one (&container->children));
3765 child = _dbus_list_get_first (&container->children);
3767 data_block_save (block, &saved);
3769 if (!_dbus_string_init (&content_signature))
3772 if (!node_build_signature (child,
3773 &content_signature))
3776 if (!_dbus_type_writer_recurse (writer, DBUS_TYPE_VARIANT,
3777 &content_signature, 0,
3781 if (!node_write_value (child, block, &sub, VARIANT_SEED))
3784 if (!_dbus_type_writer_unrecurse (writer, &sub))
3787 _dbus_string_free (&content_signature);
3791 data_block_restore (block, &saved);
3792 _dbus_string_free (&content_signature);
3797 variant_read_value (TestTypeNode *node,
3799 DBusTypeReader *reader,
3802 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3804 TestTypeNode *child;
3806 _dbus_assert (container->children != NULL);
3807 _dbus_assert (_dbus_list_length_is_one (&container->children));
3809 child = _dbus_list_get_first (&container->children);
3811 check_expected_type (reader, DBUS_TYPE_VARIANT);
3813 _dbus_type_reader_recurse (reader, &sub);
3815 if (!node_read_value (child, block, &sub, VARIANT_SEED))
3818 NEXT_EXPECTING_FALSE (&sub);
3824 container_destroy (TestTypeNode *node)
3826 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3829 link = _dbus_list_get_first_link (&container->children);
3830 while (link != NULL)
3832 TestTypeNode *child = link->data;
3833 DBusList *next = _dbus_list_get_next_link (&container->children, link);
3835 node_destroy (child);
3837 _dbus_list_free_link (link);
3843 #endif /* DBUS_BUILD_TESTS */