1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-message.c DBusMessage object
4 * Copyright (C) 2002, 2003 Red Hat Inc.
5 * Copyright (C) 2002, 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 1.2
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-internals.h"
26 #include "dbus-marshal.h"
27 #include "dbus-message.h"
28 #include "dbus-message-internal.h"
29 #include "dbus-memory.h"
30 #include "dbus-list.h"
31 #include "dbus-message-builder.h"
32 #include "dbus-dataslot.h"
36 * @defgroup DBusMessageInternals DBusMessage implementation details
37 * @ingroup DBusInternals
38 * @brief DBusMessage private implementation details.
40 * The guts of DBusMessage and its methods.
46 * Cached information about a header field in the message
50 int name_offset; /**< Offset to name of field */
51 int value_offset; /**< Offset to value of field */
54 /** Offset to byte order from start of header */
55 #define BYTE_ORDER_OFFSET 0
56 /** Offset to type from start of header */
58 /** Offset to flags from start of header */
59 #define FLAGS_OFFSET 2
60 /** Offset to version from start of header */
61 #define VERSION_OFFSET 3
62 /** Offset to header length from start of header */
63 #define HEADER_LENGTH_OFFSET 4
64 /** Offset to body length from start of header */
65 #define BODY_LENGTH_OFFSET 8
66 /** Offset to client serial from start of header */
67 #define CLIENT_SERIAL_OFFSET 12
71 * @brief Internals of DBusMessage
73 * Object representing a message received from or to be sent to
74 * another application. This is an opaque object, all members
79 DBusAtomic refcount; /**< Reference count */
81 DBusString header; /**< Header network data, stored
82 * separately from body so we can
83 * independently realloc it.
86 HeaderField header_fields[DBUS_HEADER_FIELD_LAST + 1]; /**< Track the location
87 * of each field in "header"
90 dbus_uint32_t client_serial; /**< Cached client serial value for speed */
91 dbus_uint32_t reply_serial; /**< Cached reply serial value for speed */
93 int header_padding; /**< bytes of alignment in header */
95 DBusString body; /**< Body network data. */
97 char byte_order; /**< Message byte order. */
99 DBusList *size_counters; /**< 0-N DBusCounter used to track message size. */
100 long size_counter_delta; /**< Size we incremented the size counters by. */
102 dbus_uint32_t changed_stamp; /**< Incremented when iterators are invalidated. */
104 unsigned int locked : 1; /**< Message being sent, no modifications allowed. */
106 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
108 DBusString signature; /**< Signature */
112 DBUS_MESSAGE_ITER_TYPE_MESSAGE,
113 DBUS_MESSAGE_ITER_TYPE_ARRAY,
114 DBUS_MESSAGE_ITER_TYPE_DICT
117 /** typedef for internals of message iterator */
118 typedef struct DBusMessageRealIter DBusMessageRealIter;
121 * @brief Internals of DBusMessageIter
123 * Object representing a position in a message. All fields are internal.
125 struct DBusMessageRealIter
127 DBusMessageRealIter *parent_iter; /**< parent iter, or NULL */
128 DBusMessage *message; /**< Message used */
129 dbus_uint32_t changed_stamp; /**< stamp to detect invalid iters */
131 /* This is an int instead of an enum to get a guaranteed size for the dummy: */
132 int type; /**< type of iter */
134 int pos; /**< Current position in the string */
135 int end; /**< position right after the container */
136 int container_start; /**< offset of the start of the container */
137 int container_length_pos; /**< offset of the length of the container */
139 int wrote_dict_key; /**< whether we wrote the dict key for the current dict element */
141 int array_type_pos; /**< pointer to the position of the array element type */
142 int array_type_done; /**< TRUE if the array type is fully finished */
146 * Gets the data to be sent over the network for this message.
147 * The header and then the body should be written out.
148 * This function is guaranteed to always return the same
149 * data once a message is locked (with _dbus_message_lock()).
151 * @param message the message.
152 * @param header return location for message header data.
153 * @param body return location for message body data.
156 _dbus_message_get_network_data (DBusMessage *message,
157 const DBusString **header,
158 const DBusString **body)
160 _dbus_assert (message->locked);
162 *header = &message->header;
163 *body = &message->body;
167 clear_header_padding (DBusMessage *message)
169 _dbus_string_shorten (&message->header,
170 message->header_padding);
171 message->header_padding = 0;
175 append_header_padding (DBusMessage *message)
178 old_len = _dbus_string_get_length (&message->header);
179 if (!_dbus_string_align_length (&message->header, 8))
182 message->header_padding = _dbus_string_get_length (&message->header) - old_len;
187 #ifdef DBUS_BUILD_TESTS
188 /* tests-only until it's actually used */
190 get_int_field (DBusMessage *message,
195 _dbus_assert (field <= DBUS_HEADER_FIELD_LAST);
197 offset = message->header_fields[field].value_offset;
200 return -1; /* useless if -1 is a valid value of course */
202 return _dbus_demarshal_int32 (&message->header,
210 get_uint_field (DBusMessage *message,
215 _dbus_assert (field <= DBUS_HEADER_FIELD_LAST);
217 offset = message->header_fields[field].value_offset;
220 return 0; /* useless if 0 is a valid value of course */
222 return _dbus_demarshal_uint32 (&message->header,
229 get_string_field (DBusMessage *message,
236 offset = message->header_fields[field].value_offset;
238 _dbus_assert (field <= DBUS_HEADER_FIELD_LAST);
243 /* offset points to string length, string data follows it */
244 /* FIXME _dbus_demarshal_const_string() that returned
245 * a reference to the string plus its len might be nice.
249 *len = _dbus_demarshal_uint32 (&message->header,
254 data = _dbus_string_get_const_data (&message->header);
256 return data + (offset + 4);
259 /* returns FALSE if no memory, TRUE with NULL path if no field */
261 get_path_field_decomposed (DBusMessage *message,
267 offset = message->header_fields[field].value_offset;
269 _dbus_assert (field <= DBUS_HEADER_FIELD_LAST);
277 return _dbus_demarshal_object_path (&message->header,
284 #ifdef DBUS_BUILD_TESTS
286 append_int_field (DBusMessage *message,
290 _dbus_assert (!message->locked);
292 clear_header_padding (message);
294 message->header_fields[field].name_offset =
295 _dbus_string_get_length (&message->header);
297 if (!_dbus_string_append_byte (&message->header, field))
300 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_INT32))
303 if (!_dbus_string_align_length (&message->header, 4))
306 message->header_fields[field].value_offset =
307 _dbus_string_get_length (&message->header);
309 if (!_dbus_marshal_int32 (&message->header, message->byte_order,
313 if (!append_header_padding (message))
319 _dbus_string_set_length (&message->header,
320 message->header_fields[field].name_offset);
321 message->header_fields[field].name_offset = -1;
322 message->header_fields[field].value_offset = -1;
324 /* this must succeed because it was allocated on function entry and
325 * DBusString doesn't ever realloc smaller
327 if (!append_header_padding (message))
328 _dbus_assert_not_reached ("failed to reappend header padding");
334 append_uint_field (DBusMessage *message,
338 _dbus_assert (!message->locked);
340 clear_header_padding (message);
342 message->header_fields[field].name_offset =
343 _dbus_string_get_length (&message->header);
345 if (!_dbus_string_append_byte (&message->header, field))
348 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_UINT32))
351 if (!_dbus_string_align_length (&message->header, 4))
354 message->header_fields[field].value_offset =
355 _dbus_string_get_length (&message->header);
357 if (!_dbus_marshal_uint32 (&message->header, message->byte_order,
361 if (!append_header_padding (message))
367 _dbus_string_set_length (&message->header,
368 message->header_fields[field].name_offset);
369 message->header_fields[field].name_offset = -1;
370 message->header_fields[field].value_offset = -1;
372 /* this must succeed because it was allocated on function entry and
373 * DBusString doesn't ever realloc smaller
375 if (!append_header_padding (message))
376 _dbus_assert_not_reached ("failed to reappend header padding");
380 #define MAX_BYTES_OVERHEAD_TO_APPEND_A_STRING (1 + 1 + 3 + 1 + 8)
382 append_string_field (DBusMessage *message,
387 _dbus_assert (!message->locked);
389 clear_header_padding (message);
391 message->header_fields[field].name_offset =
392 _dbus_string_get_length (&message->header);
394 if (!_dbus_string_append_byte (&message->header, field))
397 if (!_dbus_string_append_byte (&message->header, type))
400 if (!_dbus_string_align_length (&message->header, 4))
403 message->header_fields[field].value_offset =
404 _dbus_string_get_length (&message->header);
406 if (!_dbus_marshal_string (&message->header, message->byte_order,
410 if (!append_header_padding (message))
416 _dbus_string_set_length (&message->header,
417 message->header_fields[field].name_offset);
418 message->header_fields[field].name_offset = -1;
419 message->header_fields[field].value_offset = -1;
421 /* this must succeed because it was allocated on function entry and
422 * DBusString doesn't ever realloc smaller
424 if (!append_header_padding (message))
425 _dbus_assert_not_reached ("failed to reappend header padding");
431 get_next_field (DBusMessage *message,
439 offset = message->header_fields[field].name_offset;
440 retval = DBUS_HEADER_FIELD_INVALID;
443 closest = _DBUS_INT_MAX;
444 while (i < DBUS_HEADER_FIELD_LAST)
446 if (message->header_fields[i].name_offset > offset &&
447 message->header_fields[i].name_offset < closest)
449 closest = message->header_fields[i].name_offset;
459 get_type_alignment (int type)
467 case DBUS_TYPE_BOOLEAN:
471 case DBUS_TYPE_INT32:
472 case DBUS_TYPE_UINT32:
473 case DBUS_TYPE_STRING:
474 case DBUS_TYPE_OBJECT_PATH:
475 /* These are aligned 4 because they have a length as the
478 case DBUS_TYPE_CUSTOM:
483 case DBUS_TYPE_INT64:
484 case DBUS_TYPE_UINT64:
485 case DBUS_TYPE_DOUBLE:
489 case DBUS_TYPE_ARRAY:
490 _dbus_assert_not_reached ("passed an ARRAY type to get_type_alignment()");
493 case DBUS_TYPE_INVALID:
495 _dbus_assert_not_reached ("passed an invalid or unknown type to get_type_alignment()");
503 iterate_one_field (const DBusString *str,
508 DBusString *append_copy_to,
509 int *copy_name_offset_p,
510 int *copy_value_offset_p)
512 int name, type, array_type;
521 _dbus_verbose ("%s: name_offset=%d, append to %p\n",
522 _DBUS_FUNCTION_NAME, name_offset, append_copy_to);
526 name = _dbus_string_get_byte (str, name_offset);
530 type = _dbus_string_get_byte (str, type_pos);
535 /* find out the type of our array */
536 while (array_type == DBUS_TYPE_ARRAY)
540 array_type = _dbus_string_get_byte (str, pos);
543 _dbus_verbose ("%s: name %d, type '%c' %d at %d len %d, array type '%c' %d\n",
545 name, type, type, type_pos, type_len, array_type, array_type);
547 #ifndef DBUS_DISABLE_ASSERT
548 if (!_dbus_type_is_valid (array_type))
550 _dbus_warn ("type '%c' %d is not valid in %s\n",
551 array_type, array_type, _DBUS_FUNCTION_NAME);
552 _dbus_assert_not_reached ("invalid type");
556 alignment = get_type_alignment (array_type);
559 pos = _DBUS_ALIGN_VALUE (pos, alignment);
561 _dbus_verbose ("%s: alignment %d value at pos %d\n",
562 _DBUS_FUNCTION_NAME, alignment, pos);
564 /* pos now points to our value */
565 if (!_dbus_marshal_get_arg_end_pos (str, byte_order,
566 type, pos, &value_end))
567 _dbus_assert_not_reached ("failed to get the byte after this header");
570 value_len = value_end - value_pos;
572 _dbus_verbose ("%s: value_pos %d value_len %d value_end %d\n",
573 _DBUS_FUNCTION_NAME, value_pos, value_len, value_end);
576 *next_offset_p = pos + value_len;
579 *field_name_p = name;
585 orig_len = _dbus_string_get_length (append_copy_to);
587 if (copy_name_offset_p)
588 *copy_name_offset_p = orig_len;
590 if (!_dbus_string_append_byte (append_copy_to, name))
593 if (!_dbus_string_copy_len (str, type_pos, type_len,
595 _dbus_string_get_length (append_copy_to)))
598 if (!_dbus_string_align_length (append_copy_to, alignment))
601 if (copy_value_offset_p)
602 *copy_value_offset_p = _dbus_string_get_length (append_copy_to);
604 if (!_dbus_string_copy_len (str, value_pos, value_len,
606 _dbus_string_get_length (append_copy_to)))
612 _dbus_verbose ("%s: Failed copying old fields to new string\n",
613 _DBUS_FUNCTION_NAME);
614 _dbus_string_set_length (append_copy_to, orig_len);
621 #ifndef DBUS_DISABLE_ASSERT
623 verify_header_fields (DBusMessage *message)
627 while (i < DBUS_HEADER_FIELD_LAST)
629 if (message->header_fields[i].name_offset >= 0)
630 _dbus_assert (_dbus_string_get_byte (&message->header,
631 message->header_fields[i].name_offset) ==
636 #else /* DBUS_DISABLE_ASSERT */
637 #define verify_header_fields(x)
638 #endif /* DBUS_DISABLE_ASSERT */
640 /* In this function we delete one field and re-align all the fields
644 delete_one_and_re_align (DBusMessage *message,
645 int name_offset_to_delete)
648 int new_fields_front_padding;
652 HeaderField new_header_fields[DBUS_HEADER_FIELD_LAST];
654 _dbus_assert (name_offset_to_delete < _dbus_string_get_length (&message->header));
655 verify_header_fields (message);
657 _dbus_verbose ("%s: Deleting one field at offset %d\n",
659 name_offset_to_delete);
663 clear_header_padding (message);
665 if (!_dbus_string_init_preallocated (©,
666 _dbus_string_get_length (&message->header) -
667 name_offset_to_delete + 8))
669 _dbus_verbose ("%s: Failed to init string to hold copy of fields\n",
670 _DBUS_FUNCTION_NAME);
674 /* Align the name offset of the first field in the same way it's
675 * aligned in the real header
677 new_fields_front_padding = name_offset_to_delete % 8;
679 if (!_dbus_string_insert_bytes (©, 0, new_fields_front_padding,
681 _dbus_assert_not_reached ("Should not have failed to insert bytes into preallocated string\n");
683 memcpy (new_header_fields, message->header_fields,
684 sizeof (new_header_fields));
686 /* Now just re-marshal each field in the header to our temporary
687 * buffer, skipping the first one. The tricky part is that the
688 * fields are padded as if for previous_name_offset, but are in fact
689 * at unaligned_name_offset
692 if (!iterate_one_field (&message->header,
694 name_offset_to_delete,
696 &field_name, NULL, NULL, NULL))
697 _dbus_assert_not_reached ("shouldn't have failed to alloc memory to skip the deleted field");
699 if (field_name < DBUS_HEADER_FIELD_LAST)
701 new_header_fields[field_name].name_offset = -1;
702 new_header_fields[field_name].value_offset = -1;
705 while (next_offset < _dbus_string_get_length (&message->header))
707 int copy_name_offset;
708 int copy_value_offset;
710 if (!iterate_one_field (&message->header,
719 _dbus_verbose ("%s: OOM iterating one field\n",
720 _DBUS_FUNCTION_NAME);
724 if (field_name < DBUS_HEADER_FIELD_LAST)
726 new_header_fields[field_name].name_offset = copy_name_offset - new_fields_front_padding + name_offset_to_delete;
727 new_header_fields[field_name].value_offset = copy_value_offset - new_fields_front_padding + name_offset_to_delete;
731 if (!_dbus_string_replace_len (©,
732 new_fields_front_padding,
733 _dbus_string_get_length (©) - new_fields_front_padding,
735 name_offset_to_delete,
736 _dbus_string_get_length (&message->header) - name_offset_to_delete))
738 _dbus_verbose ("%s: OOM moving copy back into header\n",
739 _DBUS_FUNCTION_NAME);
743 memcpy (message->header_fields, new_header_fields,
744 sizeof (new_header_fields));
745 verify_header_fields (message);
750 _dbus_string_free (©);
753 if (!append_header_padding (message))
754 _dbus_assert_not_reached ("Failed to re-append header padding in re_align_field_recurse()");
760 delete_field (DBusMessage *message,
762 int prealloc_header_space)
766 _dbus_assert (!message->locked);
769 if (!_dbus_string_lengthen (&message->header, prealloc_header_space))
771 _dbus_verbose ("failed to prealloc %d bytes header space\n",
772 prealloc_header_space);
775 _dbus_string_shorten (&message->header, prealloc_header_space);
778 offset = message->header_fields[field].name_offset;
781 _dbus_verbose ("header field didn't exist, no need to delete\n");
782 return TRUE; /* field didn't exist */
785 return delete_one_and_re_align (message, offset);
788 #ifdef DBUS_BUILD_TESTS
790 set_int_field (DBusMessage *message,
794 int offset = message->header_fields[field].value_offset;
796 _dbus_assert (!message->locked);
800 /* need to append the field */
801 return append_int_field (message, field, value);
805 _dbus_marshal_set_int32 (&message->header,
815 set_uint_field (DBusMessage *message,
819 int offset = message->header_fields[field].value_offset;
821 _dbus_assert (!message->locked);
825 /* need to append the field */
826 return append_uint_field (message, field, value);
830 _dbus_marshal_set_uint32 (&message->header,
839 set_string_field (DBusMessage *message,
846 _dbus_assert (!message->locked);
848 /* the prealloc is so the append_string_field()
849 * below won't fail, leaving us in inconsistent state
851 prealloc = (value ? strlen (value) : 0) + MAX_BYTES_OVERHEAD_TO_APPEND_A_STRING;
853 _dbus_verbose ("set_string_field() field %d prealloc %d\n",
856 if (!delete_field (message, field, prealloc))
861 /* need to append the field */
862 if (!append_string_field (message, field, type, value))
863 _dbus_assert_not_reached ("Appending string field shouldn't have failed, due to preallocation");
870 * Sets the serial number of a message.
871 * This can only be done once on a message.
873 * @param message the message
874 * @param serial the serial
877 _dbus_message_set_serial (DBusMessage *message,
880 _dbus_assert (!message->locked);
881 _dbus_assert (dbus_message_get_serial (message) == 0);
883 _dbus_marshal_set_uint32 (&message->header,
885 CLIENT_SERIAL_OFFSET,
888 message->client_serial = serial;
892 * Sets the reply serial of a message (the client serial
893 * of the message this is a reply to).
895 * @param message the message
896 * @param reply_serial the client serial
897 * @returns #FALSE if not enough memory
900 dbus_message_set_reply_serial (DBusMessage *message,
901 dbus_uint32_t reply_serial)
903 _dbus_assert (!message->locked);
905 if (set_uint_field (message,
906 DBUS_HEADER_FIELD_REPLY_SERIAL,
909 message->reply_serial = reply_serial;
917 * Returns the serial of a message or 0 if none has been specified.
918 * The message's serial number is provided by the application sending
919 * the message and is used to identify replies to this message. All
920 * messages received on a connection will have a serial, but messages
921 * you haven't sent yet may return 0.
923 * @param message the message
924 * @returns the client serial
927 dbus_message_get_serial (DBusMessage *message)
929 return message->client_serial;
933 * Returns the serial that the message is a reply to or 0 if none.
935 * @param message the message
936 * @returns the reply serial
939 dbus_message_get_reply_serial (DBusMessage *message)
941 return message->reply_serial;
945 * Adds a counter to be incremented immediately with the
946 * size of this message, and decremented by the size
947 * of this message when this message if finalized.
948 * The link contains a counter with its refcount already
949 * incremented, but the counter itself not incremented.
950 * Ownership of link and counter refcount is passed to
953 * @param message the message
954 * @param link link with counter as data
957 _dbus_message_add_size_counter_link (DBusMessage *message,
960 /* right now we don't recompute the delta when message
961 * size changes, and that's OK for current purposes
962 * I think, but could be important to change later.
963 * Do recompute it whenever there are no outstanding counters,
964 * since it's basically free.
966 if (message->size_counters == NULL)
968 message->size_counter_delta =
969 _dbus_string_get_length (&message->header) +
970 _dbus_string_get_length (&message->body);
973 _dbus_verbose ("message has size %ld\n",
974 message->size_counter_delta);
978 _dbus_list_append_link (&message->size_counters, link);
980 _dbus_counter_adjust (link->data, message->size_counter_delta);
984 * Adds a counter to be incremented immediately with the
985 * size of this message, and decremented by the size
986 * of this message when this message if finalized.
988 * @param message the message
989 * @param counter the counter
990 * @returns #FALSE if no memory
993 _dbus_message_add_size_counter (DBusMessage *message,
994 DBusCounter *counter)
998 link = _dbus_list_alloc_link (counter);
1002 _dbus_counter_ref (counter);
1003 _dbus_message_add_size_counter_link (message, link);
1009 * Removes a counter tracking the size of this message, and decrements
1010 * the counter by the size of this message.
1012 * @param message the message
1013 * @param link_return return the link used
1014 * @param counter the counter
1017 _dbus_message_remove_size_counter (DBusMessage *message,
1018 DBusCounter *counter,
1019 DBusList **link_return)
1023 link = _dbus_list_find_last (&message->size_counters,
1025 _dbus_assert (link != NULL);
1027 _dbus_list_unlink (&message->size_counters,
1030 *link_return = link;
1032 _dbus_list_free_link (link);
1034 _dbus_counter_adjust (counter, message->size_counter_delta);
1036 _dbus_counter_unref (counter);
1040 dbus_message_create_header (DBusMessage *message,
1042 const char *service,
1044 const char *interface,
1046 const char *error_name)
1050 _dbus_assert ((interface && member) ||
1052 !(interface || member || error_name));
1054 if (!_dbus_string_append_byte (&message->header, message->byte_order))
1057 if (!_dbus_string_append_byte (&message->header, type))
1061 if (!_dbus_string_append_byte (&message->header, flags))
1064 if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
1068 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
1072 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
1076 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
1079 /* Marshal all the fields (Marshall Fields?) */
1083 if (!append_string_field (message,
1084 DBUS_HEADER_FIELD_PATH,
1085 DBUS_TYPE_OBJECT_PATH,
1090 if (service != NULL)
1092 if (!append_string_field (message,
1093 DBUS_HEADER_FIELD_SERVICE,
1099 if (interface != NULL)
1101 if (!append_string_field (message,
1102 DBUS_HEADER_FIELD_INTERFACE,
1110 if (!append_string_field (message,
1111 DBUS_HEADER_FIELD_MEMBER,
1117 if (error_name != NULL)
1119 if (!append_string_field (message,
1120 DBUS_HEADER_FIELD_ERROR_NAME,
1130 * Locks a message. Allows checking that applications don't keep a
1131 * reference to a message in the outgoing queue and change it
1132 * underneath us. Messages are locked when they enter the outgoing
1133 * queue (dbus_connection_send_message()), and the library complains
1134 * if the message is modified while locked.
1136 * @param message the message to lock.
1139 _dbus_message_lock (DBusMessage *message)
1141 if (!message->locked)
1143 /* Fill in our lengths */
1144 _dbus_marshal_set_uint32 (&message->header,
1145 message->byte_order,
1146 HEADER_LENGTH_OFFSET,
1147 _dbus_string_get_length (&message->header));
1149 _dbus_marshal_set_uint32 (&message->header,
1150 message->byte_order,
1152 _dbus_string_get_length (&message->body));
1154 message->locked = TRUE;
1161 * @defgroup DBusMessage DBusMessage
1163 * @brief Message to be sent or received over a DBusConnection.
1165 * A DBusMessage is the most basic unit of communication over a
1166 * DBusConnection. A DBusConnection represents a stream of messages
1167 * received from a remote application, and a stream of messages
1168 * sent to a remote application.
1174 * @typedef DBusMessage
1176 * Opaque data type representing a message received from or to be
1177 * sent to another application.
1181 dbus_message_new_empty_header (void)
1183 DBusMessage *message;
1186 message = dbus_new0 (DBusMessage, 1);
1187 if (message == NULL)
1190 message->refcount.value = 1;
1191 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
1192 message->client_serial = 0;
1193 message->reply_serial = 0;
1195 _dbus_data_slot_list_init (&message->slot_list);
1198 while (i <= DBUS_HEADER_FIELD_LAST)
1200 message->header_fields[i].name_offset = -1;
1201 message->header_fields[i].value_offset = -1;
1205 if (!_dbus_string_init_preallocated (&message->header, 64))
1207 dbus_free (message);
1211 if (!_dbus_string_init_preallocated (&message->body, 64))
1213 _dbus_string_free (&message->header);
1214 dbus_free (message);
1218 if (!_dbus_string_init_preallocated (&message->signature, 4))
1220 _dbus_string_free (&message->header);
1221 _dbus_string_free (&message->body);
1222 dbus_free (message);
1230 * Constructs a new message of the given message type.
1231 * Types include #DBUS_MESSAGE_TYPE_METHOD_CALL,
1232 * #DBUS_MESSAGE_TYPE_SIGNAL, and so forth.
1234 * @param message_type type of message
1235 * @returns new message or #NULL If no memory
1238 dbus_message_new (int message_type)
1240 DBusMessage *message;
1242 _dbus_return_val_if_fail (message_type != DBUS_MESSAGE_TYPE_INVALID, NULL);
1244 message = dbus_message_new_empty_header ();
1245 if (message == NULL)
1248 if (!dbus_message_create_header (message,
1250 NULL, NULL, NULL, NULL, NULL))
1252 dbus_message_unref (message);
1260 * Constructs a new message to invoke a method on a remote
1261 * object. Returns #NULL if memory can't be allocated for the
1262 * message. The service may be #NULL in which case no service is set;
1263 * this is appropriate when using D-BUS in a peer-to-peer context (no
1264 * message bus). The interface may be #NULL, which means that
1265 * if multiple methods with the given name exist it is undefined
1266 * which one will be invoked.
1268 * @param service service that the message should be sent to or #NULL
1269 * @param path object path the message should be sent to
1270 * @param interface interface to invoke method on
1271 * @param method method to invoke
1273 * @returns a new DBusMessage, free with dbus_message_unref()
1274 * @see dbus_message_unref()
1277 dbus_message_new_method_call (const char *service,
1279 const char *interface,
1282 DBusMessage *message;
1284 _dbus_return_val_if_fail (path != NULL, NULL);
1285 _dbus_return_val_if_fail (method != NULL, NULL);
1287 message = dbus_message_new_empty_header ();
1288 if (message == NULL)
1291 if (!dbus_message_create_header (message,
1292 DBUS_MESSAGE_TYPE_METHOD_CALL,
1293 service, path, interface, method, NULL))
1295 dbus_message_unref (message);
1303 * Constructs a message that is a reply to a method call. Returns
1304 * #NULL if memory can't be allocated for the message.
1306 * @param method_call the message which the created
1307 * message is a reply to.
1308 * @returns a new DBusMessage, free with dbus_message_unref()
1309 * @see dbus_message_new_method_call(), dbus_message_unref()
1312 dbus_message_new_method_return (DBusMessage *method_call)
1314 DBusMessage *message;
1317 _dbus_return_val_if_fail (method_call != NULL, NULL);
1319 sender = get_string_field (method_call,
1320 DBUS_HEADER_FIELD_SENDER_SERVICE,
1323 /* sender is allowed to be null here in peer-to-peer case */
1325 message = dbus_message_new_empty_header ();
1326 if (message == NULL)
1329 if (!dbus_message_create_header (message,
1330 DBUS_MESSAGE_TYPE_METHOD_RETURN,
1331 sender, NULL, NULL, NULL, NULL))
1333 dbus_message_unref (message);
1337 dbus_message_set_no_reply (message, TRUE);
1339 if (!dbus_message_set_reply_serial (message,
1340 dbus_message_get_serial (method_call)))
1342 dbus_message_unref (message);
1350 * Constructs a new message representing a signal emission. Returns
1351 * #NULL if memory can't be allocated for the message.
1352 * A signal is identified by its originating interface, and
1353 * the name of the signal.
1355 * @param path the path to the object emitting the signal
1356 * @param interface the interface the signal is emitted from
1357 * @param name name of the signal
1358 * @returns a new DBusMessage, free with dbus_message_unref()
1359 * @see dbus_message_unref()
1362 dbus_message_new_signal (const char *path,
1363 const char *interface,
1366 DBusMessage *message;
1368 _dbus_return_val_if_fail (path != NULL, NULL);
1369 _dbus_return_val_if_fail (interface != NULL, NULL);
1370 _dbus_return_val_if_fail (name != NULL, NULL);
1372 message = dbus_message_new_empty_header ();
1373 if (message == NULL)
1376 if (!dbus_message_create_header (message,
1377 DBUS_MESSAGE_TYPE_SIGNAL,
1378 NULL, path, interface, name, NULL))
1380 dbus_message_unref (message);
1384 dbus_message_set_no_reply (message, TRUE);
1390 * Creates a new message that is an error reply to a certain message.
1391 * Error replies are possible in response to method calls primarily.
1393 * @param reply_to the original message
1394 * @param error_name the error name
1395 * @param error_message the error message string or #NULL for none
1396 * @returns a new error message
1399 dbus_message_new_error (DBusMessage *reply_to,
1400 const char *error_name,
1401 const char *error_message)
1403 DBusMessage *message;
1405 DBusMessageIter iter;
1407 _dbus_return_val_if_fail (reply_to != NULL, NULL);
1408 _dbus_return_val_if_fail (error_name != NULL, NULL);
1410 sender = get_string_field (reply_to,
1411 DBUS_HEADER_FIELD_SENDER_SERVICE,
1414 /* sender may be NULL for non-message-bus case or
1415 * when the message bus is dealing with an unregistered
1418 message = dbus_message_new_empty_header ();
1419 if (message == NULL)
1422 if (!dbus_message_create_header (message,
1423 DBUS_MESSAGE_TYPE_ERROR,
1424 sender, NULL, NULL, NULL, error_name))
1426 dbus_message_unref (message);
1430 dbus_message_set_no_reply (message, TRUE);
1432 if (!dbus_message_set_reply_serial (message,
1433 dbus_message_get_serial (reply_to)))
1435 dbus_message_unref (message);
1439 if (error_message != NULL)
1441 dbus_message_append_iter_init (message, &iter);
1442 if (!dbus_message_iter_append_string (&iter, error_message))
1444 dbus_message_unref (message);
1453 * Creates a new message that is an exact replica of the message
1454 * specified, except that its refcount is set to 1.
1456 * @param message the message.
1457 * @returns the new message.
1460 dbus_message_copy (const DBusMessage *message)
1462 DBusMessage *retval;
1465 _dbus_return_val_if_fail (message != NULL, NULL);
1467 retval = dbus_new0 (DBusMessage, 1);
1471 retval->refcount.value = 1;
1472 retval->byte_order = message->byte_order;
1473 retval->client_serial = message->client_serial;
1474 retval->reply_serial = message->reply_serial;
1475 retval->header_padding = message->header_padding;
1476 retval->locked = FALSE;
1478 if (!_dbus_string_init_preallocated (&retval->header,
1479 _dbus_string_get_length (&message->header)))
1485 if (!_dbus_string_init_preallocated (&retval->body,
1486 _dbus_string_get_length (&message->body)))
1488 _dbus_string_free (&retval->header);
1493 if (!_dbus_string_init_preallocated (&retval->signature,
1494 _dbus_string_get_length (&message->signature)))
1496 _dbus_string_free (&retval->header);
1497 _dbus_string_free (&retval->body);
1502 if (!_dbus_string_copy (&message->header, 0,
1503 &retval->header, 0))
1506 if (!_dbus_string_copy (&message->body, 0,
1510 if (!_dbus_string_copy (&message->signature, 0,
1511 &retval->signature, 0))
1514 for (i = 0; i <= DBUS_HEADER_FIELD_LAST; i++)
1516 retval->header_fields[i] = message->header_fields[i];
1522 _dbus_string_free (&retval->header);
1523 _dbus_string_free (&retval->body);
1524 _dbus_string_free (&retval->signature);
1532 * Increments the reference count of a DBusMessage.
1534 * @param message The message
1535 * @see dbus_message_unref
1538 dbus_message_ref (DBusMessage *message)
1540 dbus_int32_t old_refcount;
1542 _dbus_return_if_fail (message != NULL);
1544 old_refcount = _dbus_atomic_inc (&message->refcount);
1545 _dbus_assert (old_refcount >= 1);
1549 free_size_counter (void *element,
1552 DBusCounter *counter = element;
1553 DBusMessage *message = data;
1555 _dbus_counter_adjust (counter, - message->size_counter_delta);
1557 _dbus_counter_unref (counter);
1561 * Decrements the reference count of a DBusMessage.
1563 * @param message The message
1564 * @see dbus_message_ref
1567 dbus_message_unref (DBusMessage *message)
1569 dbus_int32_t old_refcount;
1571 _dbus_return_if_fail (message != NULL);
1573 old_refcount = _dbus_atomic_dec (&message->refcount);
1575 _dbus_assert (old_refcount >= 0);
1577 if (old_refcount == 1)
1579 /* This calls application callbacks! */
1580 _dbus_data_slot_list_free (&message->slot_list);
1582 _dbus_list_foreach (&message->size_counters,
1583 free_size_counter, message);
1584 _dbus_list_clear (&message->size_counters);
1586 _dbus_string_free (&message->header);
1587 _dbus_string_free (&message->body);
1588 _dbus_string_free (&message->signature);
1590 dbus_free (message);
1595 * Gets the type of a message. Types include
1596 * DBUS_MESSAGE_TYPE_METHOD_CALL, DBUS_MESSAGE_TYPE_METHOD_RETURN,
1597 * DBUS_MESSAGE_TYPE_ERROR, DBUS_MESSAGE_TYPE_SIGNAL, but other types
1598 * are allowed and all code must silently ignore messages of unknown
1599 * type. DBUS_MESSAGE_TYPE_INVALID will never be returned, however.
1602 * @param message the message
1603 * @returns the type of the message
1606 dbus_message_get_type (DBusMessage *message)
1610 type = _dbus_string_get_byte (&message->header, 1);
1611 _dbus_assert (type != DBUS_MESSAGE_TYPE_INVALID);
1617 * Sets the object path this message is being sent to (for
1618 * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
1619 * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
1621 * @param message the message
1622 * @param object_path the path
1623 * @returns #FALSE if not enough memory
1626 dbus_message_set_path (DBusMessage *message,
1627 const char *object_path)
1629 _dbus_return_val_if_fail (message != NULL, FALSE);
1630 _dbus_return_val_if_fail (!message->locked, FALSE);
1632 return set_string_field (message,
1633 DBUS_HEADER_FIELD_PATH,
1634 DBUS_TYPE_OBJECT_PATH,
1639 * Gets the object path this message is being sent to
1640 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
1641 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
1643 * @param message the message
1644 * @returns the path (should not be freed)
1647 dbus_message_get_path (DBusMessage *message)
1649 _dbus_return_val_if_fail (message != NULL, NULL);
1651 return get_string_field (message, DBUS_HEADER_FIELD_PATH, NULL);
1655 * Gets the object path this message is being sent to
1656 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
1657 * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
1658 * format (one array element per path component).
1659 * Free the returned array with dbus_free_string_array().
1661 * An empty but non-NULL path array means the path "/".
1662 * So the path "/foo/bar" becomes { "foo", "bar", NULL }
1663 * and the path "/" becomes { NULL }.
1665 * @param message the message
1666 * @param path place to store allocated array of path components; #NULL set here if no path field exists
1667 * @returns #FALSE if no memory to allocate the array
1670 dbus_message_get_path_decomposed (DBusMessage *message,
1673 _dbus_return_val_if_fail (message != NULL, FALSE);
1674 _dbus_return_val_if_fail (path != NULL, FALSE);
1676 return get_path_field_decomposed (message,
1677 DBUS_HEADER_FIELD_PATH,
1682 * Sets the interface this message is being sent to
1683 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
1684 * the interface a signal is being emitted from
1685 * (for DBUS_MESSAGE_TYPE_SIGNAL).
1687 * @param message the message
1688 * @param interface the interface
1689 * @returns #FALSE if not enough memory
1692 dbus_message_set_interface (DBusMessage *message,
1693 const char *interface)
1695 _dbus_return_val_if_fail (message != NULL, FALSE);
1696 _dbus_return_val_if_fail (!message->locked, FALSE);
1698 return set_string_field (message,
1699 DBUS_HEADER_FIELD_INTERFACE,
1705 * Gets the interface this message is being sent to
1706 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
1707 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
1708 * The interface name is fully-qualified (namespaced).
1710 * @param message the message
1711 * @returns the message interface (should not be freed)
1714 dbus_message_get_interface (DBusMessage *message)
1716 _dbus_return_val_if_fail (message != NULL, NULL);
1718 return get_string_field (message, DBUS_HEADER_FIELD_INTERFACE, NULL);
1722 * Sets the interface member being invoked
1723 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
1724 * (DBUS_MESSAGE_TYPE_SIGNAL).
1725 * The interface name is fully-qualified (namespaced).
1727 * @param message the message
1728 * @param member the member
1729 * @returns #FALSE if not enough memory
1732 dbus_message_set_member (DBusMessage *message,
1735 _dbus_return_val_if_fail (message != NULL, FALSE);
1736 _dbus_return_val_if_fail (!message->locked, FALSE);
1738 return set_string_field (message,
1739 DBUS_HEADER_FIELD_MEMBER,
1745 * Gets the interface member being invoked
1746 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
1747 * (DBUS_MESSAGE_TYPE_SIGNAL).
1749 * @param message the message
1750 * @returns the member name (should not be freed)
1753 dbus_message_get_member (DBusMessage *message)
1755 _dbus_return_val_if_fail (message != NULL, NULL);
1757 return get_string_field (message,
1758 DBUS_HEADER_FIELD_MEMBER,
1763 * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
1764 * The name is fully-qualified (namespaced).
1766 * @param message the message
1767 * @param error_name the name
1768 * @returns #FALSE if not enough memory
1771 dbus_message_set_error_name (DBusMessage *message,
1772 const char *error_name)
1774 _dbus_return_val_if_fail (message != NULL, FALSE);
1775 _dbus_return_val_if_fail (!message->locked, FALSE);
1777 return set_string_field (message,
1778 DBUS_HEADER_FIELD_ERROR_NAME,
1784 * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only).
1786 * @param message the message
1787 * @returns the error name (should not be freed)
1790 dbus_message_get_error_name (DBusMessage *message)
1792 _dbus_return_val_if_fail (message != NULL, NULL);
1794 return get_string_field (message,
1795 DBUS_HEADER_FIELD_ERROR_NAME,
1800 * Sets the message's destination service.
1802 * @param message the message
1803 * @param destination the destination service name
1804 * @returns #FALSE if not enough memory
1807 dbus_message_set_destination (DBusMessage *message,
1808 const char *destination)
1810 _dbus_return_val_if_fail (message != NULL, FALSE);
1811 _dbus_return_val_if_fail (!message->locked, FALSE);
1813 return set_string_field (message,
1814 DBUS_HEADER_FIELD_SERVICE,
1820 * Gets the destination service of a message.
1822 * @param message the message
1823 * @returns the message destination service (should not be freed)
1826 dbus_message_get_destination (DBusMessage *message)
1828 _dbus_return_val_if_fail (message != NULL, NULL);
1830 return get_string_field (message,
1831 DBUS_HEADER_FIELD_SERVICE,
1836 * Appends fields to a message given a variable argument list. The
1837 * variable argument list should contain the type of the argument
1838 * followed by the value to add. Array values are specified by an int
1839 * typecode followed by a pointer to the array followed by an int
1840 * giving the length of the array. The argument list must be
1841 * terminated with #DBUS_TYPE_INVALID.
1843 * This function doesn't support dicts or non-fundamental arrays.
1845 * This function supports #DBUS_TYPE_INT64 and #DBUS_TYPE_UINT64
1846 * only if #DBUS_HAVE_INT64 is defined.
1848 * @param message the message
1849 * @param first_arg_type type of the first argument
1850 * @param ... value of first argument, list of additional type-value pairs
1851 * @returns #TRUE on success
1854 dbus_message_append_args (DBusMessage *message,
1861 _dbus_return_val_if_fail (message != NULL, FALSE);
1863 va_start (var_args, first_arg_type);
1864 retval = dbus_message_append_args_valist (message,
1873 * This function takes a va_list for use by language bindings.
1874 * It's otherwise the same as dbus_message_append_args().
1876 * @todo: Shouldn't this function clean up the changes to the message
1877 * on failures? (Yes)
1879 * @see dbus_message_append_args.
1880 * @param message the message
1881 * @param first_arg_type type of first argument
1882 * @param var_args value of first argument, then list of type/value pairs
1883 * @returns #TRUE on success
1886 dbus_message_append_args_valist (DBusMessage *message,
1891 DBusMessageIter iter;
1893 _dbus_return_val_if_fail (message != NULL, FALSE);
1895 old_len = _dbus_string_get_length (&message->body);
1897 type = first_arg_type;
1899 dbus_message_append_iter_init (message, &iter);
1901 while (type != DBUS_TYPE_INVALID)
1906 if (!dbus_message_iter_append_nil (&iter))
1909 case DBUS_TYPE_BOOLEAN:
1910 if (!dbus_message_iter_append_boolean (&iter, va_arg (var_args, dbus_bool_t)))
1913 case DBUS_TYPE_INT32:
1914 if (!dbus_message_iter_append_int32 (&iter, va_arg (var_args, dbus_int32_t)))
1917 case DBUS_TYPE_UINT32:
1918 if (!dbus_message_iter_append_uint32 (&iter, va_arg (var_args, dbus_uint32_t)))
1921 #ifdef DBUS_HAVE_INT64
1922 case DBUS_TYPE_INT64:
1923 if (!dbus_message_iter_append_int64 (&iter, va_arg (var_args, dbus_int64_t)))
1926 case DBUS_TYPE_UINT64:
1927 if (!dbus_message_iter_append_uint64 (&iter, va_arg (var_args, dbus_uint64_t)))
1930 #endif /* DBUS_HAVE_INT64 */
1931 case DBUS_TYPE_DOUBLE:
1932 if (!dbus_message_iter_append_double (&iter, va_arg (var_args, double)))
1935 case DBUS_TYPE_STRING:
1936 if (!dbus_message_iter_append_string (&iter, va_arg (var_args, const char *)))
1939 case DBUS_TYPE_OBJECT_PATH:
1942 case DBUS_TYPE_CUSTOM:
1945 unsigned char *data;
1948 name = va_arg (var_args, const char *);
1949 data = va_arg (var_args, unsigned char *);
1950 len = va_arg (var_args, int);
1952 if (!dbus_message_iter_append_custom (&iter, name, data, len))
1956 case DBUS_TYPE_ARRAY:
1961 type = va_arg (var_args, int);
1962 data = va_arg (var_args, void *);
1963 len = va_arg (var_args, int);
1967 case DBUS_TYPE_BYTE:
1968 if (!dbus_message_iter_append_byte_array (&iter, (unsigned char *)data, len))
1971 case DBUS_TYPE_BOOLEAN:
1972 if (!dbus_message_iter_append_boolean_array (&iter, (unsigned char *)data, len))
1975 case DBUS_TYPE_INT32:
1976 if (!dbus_message_iter_append_int32_array (&iter, (dbus_int32_t *)data, len))
1979 case DBUS_TYPE_UINT32:
1980 if (!dbus_message_iter_append_uint32_array (&iter, (dbus_uint32_t *)data, len))
1983 #ifdef DBUS_HAVE_INT64
1984 case DBUS_TYPE_INT64:
1985 if (!dbus_message_iter_append_int64_array (&iter, (dbus_int64_t *)data, len))
1988 case DBUS_TYPE_UINT64:
1989 if (!dbus_message_iter_append_uint64_array (&iter, (dbus_uint64_t *)data, len))
1992 #endif /* DBUS_HAVE_INT64 */
1993 case DBUS_TYPE_DOUBLE:
1994 if (!dbus_message_iter_append_double_array (&iter, (double *)data, len))
1997 case DBUS_TYPE_STRING:
1998 if (!dbus_message_iter_append_string_array (&iter, (const char **)data, len))
2002 case DBUS_TYPE_ARRAY:
2003 case DBUS_TYPE_CUSTOM:
2004 case DBUS_TYPE_DICT:
2005 _dbus_warn ("dbus_message_append_args_valist doesn't support recursive arrays\n");
2008 _dbus_warn ("Unknown field type %d\n", type);
2014 case DBUS_TYPE_DICT:
2015 _dbus_warn ("dbus_message_append_args_valist doesn't support dicts\n");
2018 _dbus_warn ("Unknown field type %d\n", type);
2022 type = va_arg (var_args, int);
2033 * Gets arguments from a message given a variable argument list.
2034 * The variable argument list should contain the type of the
2035 * argumen followed by a pointer to where the value should be
2036 * stored. The list is terminated with #DBUS_TYPE_INVALID.
2038 * @param message the message
2039 * @param error error to be filled in on failure
2040 * @param first_arg_type the first argument type
2041 * @param ... location for first argument value, then list of type-location pairs
2042 * @returns #FALSE if the error was set
2045 dbus_message_get_args (DBusMessage *message,
2053 _dbus_return_val_if_fail (message != NULL, FALSE);
2054 _dbus_return_val_if_error_is_set (error, FALSE);
2056 va_start (var_args, first_arg_type);
2057 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
2064 * This function takes a va_list for use by language bindings
2066 * @todo We need to free the argument data when an error occurs.
2068 * @see dbus_message_get_args
2069 * @param message the message
2070 * @param error error to be filled in
2071 * @param first_arg_type type of the first argument
2072 * @param var_args return location for first argument, followed by list of type/location pairs
2073 * @returns #FALSE if error was set
2076 dbus_message_get_args_valist (DBusMessage *message,
2081 DBusMessageIter iter;
2083 _dbus_return_val_if_fail (message != NULL, FALSE);
2084 _dbus_return_val_if_error_is_set (error, FALSE);
2086 dbus_message_iter_init (message, &iter);
2087 return dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
2091 * Gets arguments from a message iterator given a variable argument list.
2092 * The variable argument list should contain the type of the
2093 * argumen followed by a pointer to where the value should be
2094 * stored. The list is terminated with 0.
2096 * @param iter the message iterator
2097 * @param error error to be filled in on failure
2098 * @param first_arg_type the first argument type
2099 * @param ... location for first argument value, then list of type-location pairs
2100 * @returns #FALSE if the error was set
2103 dbus_message_iter_get_args (DBusMessageIter *iter,
2111 _dbus_return_val_if_fail (iter != NULL, FALSE);
2112 _dbus_return_val_if_error_is_set (error, FALSE);
2114 va_start (var_args, first_arg_type);
2115 retval = dbus_message_iter_get_args_valist (iter, error, first_arg_type, var_args);
2122 * This function takes a va_list for use by language bindings
2124 * This function supports #DBUS_TYPE_INT64 and #DBUS_TYPE_UINT64
2125 * only if #DBUS_HAVE_INT64 is defined.
2127 * @todo this function (or some lower-level non-convenience function)
2128 * needs better error handling; should allow the application to
2129 * distinguish between out of memory, and bad data from the remote
2130 * app. It also needs to not leak a bunch of args when it gets
2131 * to the arg that's bad, as that would be a security hole
2132 * (allow one app to force another to leak memory)
2134 * @todo We need to free the argument data when an error occurs.
2136 * @see dbus_message_get_args
2137 * @param iter the message iter
2138 * @param error error to be filled in
2139 * @param first_arg_type type of the first argument
2140 * @param var_args return location for first argument, followed by list of type/location pairs
2141 * @returns #FALSE if error was set
2144 dbus_message_iter_get_args_valist (DBusMessageIter *iter,
2149 int spec_type, msg_type, i;
2152 _dbus_return_val_if_fail (iter != NULL, FALSE);
2153 _dbus_return_val_if_error_is_set (error, FALSE);
2157 spec_type = first_arg_type;
2160 while (spec_type != 0)
2162 msg_type = dbus_message_iter_get_arg_type (iter);
2164 if (msg_type != spec_type)
2166 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2167 "Argument %d is specified to be of type \"%s\", but "
2168 "is actually of type \"%s\"\n", i,
2169 _dbus_type_to_string (spec_type),
2170 _dbus_type_to_string (msg_type));
2179 case DBUS_TYPE_BYTE:
2183 ptr = va_arg (var_args, unsigned char *);
2185 *ptr = dbus_message_iter_get_byte (iter);
2188 case DBUS_TYPE_BOOLEAN:
2192 ptr = va_arg (var_args, dbus_bool_t *);
2194 *ptr = dbus_message_iter_get_boolean (iter);
2197 case DBUS_TYPE_INT32:
2201 ptr = va_arg (var_args, dbus_int32_t *);
2203 *ptr = dbus_message_iter_get_int32 (iter);
2206 case DBUS_TYPE_UINT32:
2210 ptr = va_arg (var_args, dbus_uint32_t *);
2212 *ptr = dbus_message_iter_get_uint32 (iter);
2215 #ifdef DBUS_HAVE_INT64
2216 case DBUS_TYPE_INT64:
2220 ptr = va_arg (var_args, dbus_int64_t *);
2222 *ptr = dbus_message_iter_get_int64 (iter);
2225 case DBUS_TYPE_UINT64:
2229 ptr = va_arg (var_args, dbus_uint64_t *);
2231 *ptr = dbus_message_iter_get_uint64 (iter);
2234 #endif /* DBUS_HAVE_INT64 */
2236 case DBUS_TYPE_DOUBLE:
2240 ptr = va_arg (var_args, double *);
2242 *ptr = dbus_message_iter_get_double (iter);
2246 case DBUS_TYPE_STRING:
2250 ptr = va_arg (var_args, char **);
2252 *ptr = dbus_message_iter_get_string (iter);
2256 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2263 case DBUS_TYPE_CUSTOM:
2266 unsigned char **data;
2269 name = va_arg (var_args, char **);
2270 data = va_arg (var_args, unsigned char **);
2271 len = va_arg (var_args, int *);
2273 if (!dbus_message_iter_get_custom (iter, name, data, len))
2275 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2280 case DBUS_TYPE_ARRAY:
2285 type = va_arg (var_args, int);
2286 data = va_arg (var_args, void *);
2287 len = va_arg (var_args, int *);
2289 _dbus_return_val_if_fail (data != NULL, FALSE);
2290 _dbus_return_val_if_fail (len != NULL, FALSE);
2292 if (dbus_message_iter_get_array_type (iter) != type)
2294 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2295 "Argument %d is specified to be of type \"array of %s\", but "
2296 "is actually of type \"array of %s\"\n", i,
2297 _dbus_type_to_string (type),
2298 _dbus_type_to_string (dbus_message_iter_get_array_type (iter)));
2304 case DBUS_TYPE_BYTE:
2305 if (!dbus_message_iter_get_byte_array (iter, (unsigned char **)data, len))
2307 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2311 case DBUS_TYPE_BOOLEAN:
2312 if (!dbus_message_iter_get_boolean_array (iter, (unsigned char **)data, len))
2314 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2318 case DBUS_TYPE_INT32:
2319 if (!dbus_message_iter_get_int32_array (iter, (dbus_int32_t **)data, len))
2321 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2325 case DBUS_TYPE_UINT32:
2326 if (!dbus_message_iter_get_uint32_array (iter, (dbus_uint32_t **)data, len))
2328 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2332 #ifdef DBUS_HAVE_INT64
2333 case DBUS_TYPE_INT64:
2334 if (!dbus_message_iter_get_int64_array (iter, (dbus_int64_t **)data, len))
2336 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2340 case DBUS_TYPE_UINT64:
2341 if (!dbus_message_iter_get_uint64_array (iter, (dbus_uint64_t **)data, len))
2343 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2347 #endif /* DBUS_HAVE_INT64 */
2348 case DBUS_TYPE_DOUBLE:
2349 if (!dbus_message_iter_get_double_array (iter, (double **)data, len))
2351 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2355 case DBUS_TYPE_STRING:
2356 if (!dbus_message_iter_get_string_array (iter, (char ***)data, len))
2358 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2363 case DBUS_TYPE_ARRAY:
2364 case DBUS_TYPE_CUSTOM:
2365 case DBUS_TYPE_DICT:
2366 _dbus_warn ("dbus_message_get_args_valist doesn't support recursive arrays\n");
2367 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2370 _dbus_warn ("Unknown field type %d\n", type);
2371 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2376 case DBUS_TYPE_DICT:
2377 _dbus_warn ("dbus_message_get_args_valist doesn't support dicts\n");
2378 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2381 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2382 _dbus_warn ("Unknown field type %d\n", spec_type);
2386 spec_type = va_arg (var_args, int);
2387 if (spec_type != 0 && !dbus_message_iter_next (iter))
2389 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2390 "Message has only %d arguments, but more were expected", i);
2406 * Initializes a DBusMessageIter representing the arguments of the
2407 * message passed in.
2409 * @param message the message
2410 * @param iter pointer to an iterator to initialize
2413 dbus_message_iter_init (DBusMessage *message,
2414 DBusMessageIter *iter)
2416 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2418 _dbus_return_if_fail (message != NULL);
2419 _dbus_return_if_fail (iter != NULL);
2421 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
2423 real->message = message;
2424 real->parent_iter = NULL;
2425 real->changed_stamp = message->changed_stamp;
2427 real->type = DBUS_MESSAGE_ITER_TYPE_MESSAGE;
2429 real->end = _dbus_string_get_length (&message->body);
2431 real->container_start = 0;
2432 real->container_length_pos = 0;
2433 real->wrote_dict_key = 0;
2434 real->array_type_pos = 0;
2437 #ifndef DBUS_DISABLE_CHECKS
2439 dbus_message_iter_check (DBusMessageRealIter *iter)
2443 _dbus_warn ("dbus iterator check failed: iterator is NULL\n");
2447 if (iter->changed_stamp != iter->message->changed_stamp)
2449 _dbus_warn ("dbus iterator check failed: invalid iterator, must re-initialize it after modifying the message\n");
2453 if (iter->pos < 0 || iter->pos > iter->end)
2455 _dbus_warn ("dbus iterator check failed: invalid position\n");
2461 #endif /* DBUS_DISABLE_CHECKS */
2464 skip_array_type (DBusMessageRealIter *iter, int pos)
2470 data = _dbus_string_get_const_data_len (&iter->message->body,
2473 while (*data == DBUS_TYPE_ARRAY);
2478 /* FIXME what are these _dbus_type_is_valid() checks for?
2479 * haven't we validated the message?
2482 dbus_message_iter_get_data_start (DBusMessageRealIter *iter, int *type)
2489 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
2490 data = _dbus_string_get_const_data_len (&iter->message->body,
2492 if (_dbus_type_is_valid (*data))
2495 *type = DBUS_TYPE_INVALID;
2497 return skip_array_type (iter, iter->pos);
2499 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
2500 data = _dbus_string_get_const_data_len (&iter->message->body,
2501 iter->array_type_pos, 1);
2502 if (_dbus_type_is_valid (*data))
2505 *type = DBUS_TYPE_INVALID;
2509 case DBUS_MESSAGE_ITER_TYPE_DICT:
2510 /* Get the length of the string */
2511 len = _dbus_demarshal_uint32 (&iter->message->body,
2512 iter->message->byte_order,
2514 pos = pos + len + 1;
2516 data = _dbus_string_get_const_data_len (&iter->message->body,
2518 if (_dbus_type_is_valid (*data))
2521 *type = DBUS_TYPE_INVALID;
2523 return skip_array_type (iter, pos);
2526 _dbus_assert_not_reached ("Invalid iter type");
2529 *type = DBUS_TYPE_INVALID;
2535 * Checks if an iterator has any more fields.
2537 * @param iter the message iter
2538 * @returns #TRUE if there are more fields
2542 dbus_message_iter_has_next (DBusMessageIter *iter)
2544 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2548 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2550 if (real->pos >= real->end)
2553 pos = dbus_message_iter_get_data_start (real, &type);
2555 if (!_dbus_marshal_get_arg_end_pos (&real->message->body,
2556 real->message->byte_order,
2557 type, pos, &end_pos))
2560 if (end_pos >= real->end)
2567 * Moves the iterator to the next field.
2569 * @param iter The message iter
2570 * @returns #TRUE if the iterator was moved to the next field
2573 dbus_message_iter_next (DBusMessageIter *iter)
2575 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2579 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2581 pos = dbus_message_iter_get_data_start (real, &type);
2583 if (!_dbus_marshal_get_arg_end_pos (&real->message->body,
2584 real->message->byte_order,
2585 type, pos, &end_pos))
2588 if (end_pos >= real->end)
2591 real->pos = end_pos;
2597 * Returns the argument type of the argument that the
2598 * message iterator points at.
2600 * @param iter the message iter
2601 * @returns the field type
2604 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
2606 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2609 _dbus_return_val_if_fail (dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2611 if (real->pos >= real->end)
2613 _dbus_verbose (" iterator at or beyond end of message\n");
2614 return DBUS_TYPE_INVALID;
2617 pos = dbus_message_iter_get_data_start (real, &type);
2622 /* FIXME why do we validate the typecode in here, hasn't the message
2623 * already been verified?
2626 iter_get_array_type (DBusMessageRealIter *iter, int *array_type_pos)
2629 int _array_type_pos;
2634 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
2635 _array_type_pos = iter->pos + 1;
2637 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
2638 _array_type_pos = iter->array_type_pos + 1;
2640 case DBUS_MESSAGE_ITER_TYPE_DICT:
2641 /* Get the length of the string */
2642 len = _dbus_demarshal_uint32 (&iter->message->body,
2643 iter->message->byte_order,
2645 pos = pos + len + 1;
2646 data = _dbus_string_get_const_data_len (&iter->message->body,
2648 _array_type_pos = pos + 1;
2651 _dbus_assert_not_reached ("wrong iter type");
2652 return DBUS_TYPE_INVALID;
2655 if (array_type_pos != NULL)
2656 *array_type_pos = _array_type_pos;
2658 data = _dbus_string_get_const_data_len (&iter->message->body,
2659 _array_type_pos, 1);
2660 if (_dbus_type_is_valid (*data))
2663 return DBUS_TYPE_INVALID;
2668 * Returns the element type of the array that the
2669 * message iterator points at. Note that you need
2670 * to check that the iterator points to an array
2671 * prior to using this function.
2673 * @param iter the message iter
2674 * @returns the field type
2677 dbus_message_iter_get_array_type (DBusMessageIter *iter)
2679 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2682 _dbus_return_val_if_fail (dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2684 if (real->pos >= real->end)
2685 return DBUS_TYPE_INVALID;
2687 pos = dbus_message_iter_get_data_start (real, &type);
2689 _dbus_assert (type == DBUS_TYPE_ARRAY);
2691 return iter_get_array_type (real, NULL);
2696 * Returns the string value that an iterator may point to.
2697 * Note that you need to check that the iterator points to
2698 * a string value before using this function.
2700 * @see dbus_message_iter_get_arg_type
2701 * @param iter the message iter
2702 * @returns the string
2705 dbus_message_iter_get_string (DBusMessageIter *iter)
2707 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2710 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
2712 pos = dbus_message_iter_get_data_start (real, &type);
2714 _dbus_assert (type == DBUS_TYPE_STRING);
2716 return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2722 * @todo FIXME to finish this _dbus_demarshal_object_path() needs
2723 * to not explode the path.
2725 * Returns the object path value that an iterator may point to.
2726 * Note that you need to check that the iterator points to
2727 * an object path value before using this function.
2729 * @see dbus_message_iter_get_arg_type
2730 * @param iter the message iter
2734 dbus_message_iter_get_object_path (DBusMessageIter *iter)
2736 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2739 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
2741 pos = dbus_message_iter_get_data_start (real, &type);
2743 _dbus_assert (type == DBUS_TYPE_OBJECT_PATH);
2745 return _dbus_demarshal_object_path (&real->message->body, real->message->byte_order,
2751 * Returns the name and data from a custom type that an iterator may
2752 * point to. Note that you need to check that the iterator points to a
2753 * custom type before using this function.
2755 * @see dbus_message_iter_get_arg_type
2756 * @param iter the message iter
2757 * @param name return location for the name of the custom type
2758 * @param value return location for data
2759 * @param len return location for length of data
2760 * @returns TRUE if get succeed
2764 dbus_message_iter_get_custom (DBusMessageIter *iter,
2766 unsigned char **value,
2769 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2773 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2775 pos = dbus_message_iter_get_data_start (real, &type);
2777 _dbus_assert (type == DBUS_TYPE_CUSTOM);
2779 _name = _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2785 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
2786 pos, NULL, value, len))
2798 * Returns the byte value that an iterator may point to.
2799 * Note that you need to check that the iterator points to
2800 * a byte value before using this function.
2802 * @see dbus_message_iter_get_arg_type
2803 * @param iter the message iter
2804 * @returns the byte value
2807 dbus_message_iter_get_byte (DBusMessageIter *iter)
2809 unsigned char value;
2810 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2813 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2815 pos = dbus_message_iter_get_data_start (real, &type);
2817 _dbus_assert (type == DBUS_TYPE_BYTE);
2819 value = _dbus_string_get_byte (&real->message->body, pos);
2826 * Returns the boolean value that an iterator may point to.
2827 * Note that you need to check that the iterator points to
2828 * a boolean value before using this function.
2830 * @see dbus_message_iter_get_arg_type
2831 * @param iter the message iter
2832 * @returns the boolean value
2835 dbus_message_iter_get_boolean (DBusMessageIter *iter)
2837 unsigned char value;
2838 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2841 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2843 pos = dbus_message_iter_get_data_start (real, &type);
2845 _dbus_assert (type == DBUS_TYPE_BOOLEAN);
2847 value = _dbus_string_get_byte (&real->message->body, pos);
2853 * Returns the 32 bit signed integer value that an iterator may point to.
2854 * Note that you need to check that the iterator points to
2855 * a 32-bit integer value before using this function.
2857 * @see dbus_message_iter_get_arg_type
2858 * @param iter the message iter
2859 * @returns the integer
2862 dbus_message_iter_get_int32 (DBusMessageIter *iter)
2864 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2867 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2869 pos = dbus_message_iter_get_data_start (real, &type);
2871 _dbus_assert (type == DBUS_TYPE_INT32);
2873 return _dbus_demarshal_int32 (&real->message->body, real->message->byte_order,
2878 * Returns the 32 bit unsigned integer value that an iterator may point to.
2879 * Note that you need to check that the iterator points to
2880 * a 32-bit unsigned integer value before using this function.
2882 * @see dbus_message_iter_get_arg_type
2883 * @param iter the message iter
2884 * @returns the integer
2887 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
2889 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2892 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2894 pos = dbus_message_iter_get_data_start (real, &type);
2896 _dbus_assert (type == DBUS_TYPE_UINT32);
2898 return _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
2902 #ifdef DBUS_HAVE_INT64
2905 * Returns the 64 bit signed integer value that an iterator may point
2906 * to. Note that you need to check that the iterator points to a
2907 * 64-bit integer value before using this function.
2909 * This function only exists if #DBUS_HAVE_INT64 is defined.
2911 * @see dbus_message_iter_get_arg_type
2912 * @param iter the message iter
2913 * @returns the integer
2916 dbus_message_iter_get_int64 (DBusMessageIter *iter)
2918 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2921 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2923 pos = dbus_message_iter_get_data_start (real, &type);
2925 _dbus_assert (type == DBUS_TYPE_INT64);
2927 return _dbus_demarshal_int64 (&real->message->body, real->message->byte_order,
2932 * Returns the 64 bit unsigned integer value that an iterator may point to.
2933 * Note that you need to check that the iterator points to
2934 * a 64-bit unsigned integer value before using this function.
2936 * This function only exists if #DBUS_HAVE_INT64 is defined.
2938 * @see dbus_message_iter_get_arg_type
2939 * @param iter the message iter
2940 * @returns the integer
2943 dbus_message_iter_get_uint64 (DBusMessageIter *iter)
2945 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2948 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2950 pos = dbus_message_iter_get_data_start (real, &type);
2952 _dbus_assert (type == DBUS_TYPE_UINT64);
2954 return _dbus_demarshal_uint64 (&real->message->body, real->message->byte_order,
2958 #endif /* DBUS_HAVE_INT64 */
2961 * Returns the double value that an iterator may point to.
2962 * Note that you need to check that the iterator points to
2963 * a string value before using this function.
2965 * @see dbus_message_iter_get_arg_type
2966 * @param iter the message iter
2967 * @returns the double
2970 dbus_message_iter_get_double (DBusMessageIter *iter)
2972 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2975 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0.0);
2977 pos = dbus_message_iter_get_data_start (real, &type);
2979 _dbus_assert (type == DBUS_TYPE_DOUBLE);
2981 return _dbus_demarshal_double (&real->message->body, real->message->byte_order,
2986 * Initializes an iterator for the array that the iterator
2987 * may point to. Note that you need to check that the iterator
2988 * points to an array prior to using this function.
2990 * The array element type is returned in array_type, and the array
2991 * iterator can only be used to get that type of data.
2993 * @param iter the iterator
2994 * @param array_iter pointer to an iterator to initialize
2995 * @param array_type gets set to the type of the array elements
2998 dbus_message_iter_init_array_iterator (DBusMessageIter *iter,
2999 DBusMessageIter *array_iter,
3002 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3003 DBusMessageRealIter *array_real = (DBusMessageRealIter *)array_iter;
3004 int type, pos, len_pos, len, array_type_pos;
3007 _dbus_return_if_fail (dbus_message_iter_check (real));
3009 pos = dbus_message_iter_get_data_start (real, &type);
3011 _dbus_assert (type == DBUS_TYPE_ARRAY);
3013 _array_type = iter_get_array_type (real, &array_type_pos);
3015 len_pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
3016 len = _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
3019 array_real->parent_iter = real;
3020 array_real->message = real->message;
3021 array_real->changed_stamp = real->message->changed_stamp;
3023 array_real->type = DBUS_MESSAGE_ITER_TYPE_ARRAY;
3024 array_real->pos = pos;
3025 array_real->end = pos + len;
3027 array_real->container_start = pos;
3028 array_real->container_length_pos = len_pos;
3029 array_real->wrote_dict_key = 0;
3030 array_real->array_type_pos = array_type_pos;
3031 array_real->array_type_done = TRUE;
3033 if (array_type != NULL)
3034 *array_type = _array_type;
3039 * Initializes an iterator for the dict that the iterator
3040 * may point to. Note that you need to check that the iterator
3041 * points to a dict prior to using this function.
3043 * @param iter the iterator
3044 * @param dict_iter pointer to an iterator to initialize
3047 dbus_message_iter_init_dict_iterator (DBusMessageIter *iter,
3048 DBusMessageIter *dict_iter)
3050 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3051 DBusMessageRealIter *dict_real = (DBusMessageRealIter *)dict_iter;
3052 int type, pos, len_pos, len;
3054 _dbus_return_if_fail (dbus_message_iter_check (real));
3056 pos = dbus_message_iter_get_data_start (real, &type);
3058 _dbus_assert (type == DBUS_TYPE_DICT);
3060 len_pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
3061 len = _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
3064 dict_real->parent_iter = real;
3065 dict_real->message = real->message;
3066 dict_real->changed_stamp = real->message->changed_stamp;
3068 dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
3069 dict_real->pos = pos;
3070 dict_real->end = pos + len;
3072 dict_real->container_start = pos;
3073 dict_real->container_length_pos = len_pos;
3074 dict_real->wrote_dict_key = 0;
3078 * Returns the byte array that the iterator may point to.
3079 * Note that you need to check that the iterator points
3080 * to a byte array prior to using this function.
3082 * @param iter the iterator
3083 * @param value return location for array values
3084 * @param len return location for length of byte array
3085 * @returns #TRUE on success
3088 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
3089 unsigned char **value,
3092 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3095 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3097 pos = dbus_message_iter_get_data_start (real, &type);
3099 _dbus_assert (type == DBUS_TYPE_ARRAY);
3101 type = iter_get_array_type (real, NULL);
3103 _dbus_assert (type == DBUS_TYPE_BYTE);
3105 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
3106 pos, NULL, value, len))
3113 * Returns the boolean array that the iterator may point to. Note that
3114 * you need to check that the iterator points to an array of the
3115 * correct type prior to using this function.
3117 * @param iter the iterator
3118 * @param value return location for the array
3119 * @param len return location for the array length
3120 * @returns #TRUE on success
3123 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
3124 unsigned char **value,
3127 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3130 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3132 pos = dbus_message_iter_get_data_start (real, &type);
3134 _dbus_assert (type == DBUS_TYPE_ARRAY);
3136 type = iter_get_array_type (real, NULL);
3138 _dbus_assert (type == DBUS_TYPE_BOOLEAN);
3140 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
3141 pos, NULL, value, len))
3148 * Returns the 32 bit signed integer array that the iterator may point
3149 * to. Note that you need to check that the iterator points to an
3150 * array of the correct type prior to using this function.
3152 * @param iter the iterator
3153 * @param value return location for the array
3154 * @param len return location for the array length
3155 * @returns #TRUE on success
3158 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
3159 dbus_int32_t **value,
3162 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3165 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3167 pos = dbus_message_iter_get_data_start (real, &type);
3169 _dbus_assert (type == DBUS_TYPE_ARRAY);
3171 type = iter_get_array_type (real, NULL);
3173 _dbus_assert (type == DBUS_TYPE_INT32);
3175 if (!_dbus_demarshal_int32_array (&real->message->body, real->message->byte_order,
3176 pos, NULL, value, len))
3183 * Returns the 32 bit unsigned integer array that the iterator may point
3184 * to. Note that you need to check that the iterator points to an
3185 * array of the correct type prior to using this function.
3187 * @param iter the iterator
3188 * @param value return location for the array
3189 * @param len return location for the array length
3190 * @returns #TRUE on success
3193 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
3194 dbus_uint32_t **value,
3197 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3200 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3202 pos = dbus_message_iter_get_data_start (real, &type);
3204 _dbus_assert (type == DBUS_TYPE_ARRAY);
3206 type = iter_get_array_type (real, NULL);
3207 _dbus_assert (type == DBUS_TYPE_UINT32);
3209 if (!_dbus_demarshal_uint32_array (&real->message->body, real->message->byte_order,
3210 pos, NULL, value, len))
3216 #ifdef DBUS_HAVE_INT64
3219 * Returns the 64 bit signed integer array that the iterator may point
3220 * to. Note that you need to check that the iterator points to an
3221 * array of the correct type prior to using this function.
3223 * This function only exists if #DBUS_HAVE_INT64 is defined.
3225 * @param iter the iterator
3226 * @param value return location for the array
3227 * @param len return location for the array length
3228 * @returns #TRUE on success
3231 dbus_message_iter_get_int64_array (DBusMessageIter *iter,
3232 dbus_int64_t **value,
3235 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3238 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3240 pos = dbus_message_iter_get_data_start (real, &type);
3242 _dbus_assert (type == DBUS_TYPE_ARRAY);
3244 type = iter_get_array_type (real, NULL);
3246 _dbus_assert (type == DBUS_TYPE_INT64);
3248 if (!_dbus_demarshal_int64_array (&real->message->body, real->message->byte_order,
3249 pos, NULL, value, len))
3256 * Returns the 64 bit unsigned integer array that the iterator may point
3257 * to. Note that you need to check that the iterator points to an
3258 * array of the correct type prior to using this function.
3260 * This function only exists if #DBUS_HAVE_INT64 is defined.
3262 * @param iter the iterator
3263 * @param value return location for the array
3264 * @param len return location for the array length
3265 * @returns #TRUE on success
3268 dbus_message_iter_get_uint64_array (DBusMessageIter *iter,
3269 dbus_uint64_t **value,
3272 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3275 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3277 pos = dbus_message_iter_get_data_start (real, &type);
3279 _dbus_assert (type == DBUS_TYPE_ARRAY);
3281 type = iter_get_array_type (real, NULL);
3282 _dbus_assert (type == DBUS_TYPE_UINT64);
3284 if (!_dbus_demarshal_uint64_array (&real->message->body, real->message->byte_order,
3285 pos, NULL, value, len))
3291 #endif /* DBUS_HAVE_INT64 */
3294 * Returns the double array that the iterator may point to. Note that
3295 * you need to check that the iterator points to an array of the
3296 * correct type prior to using this function.
3298 * @param iter the iterator
3299 * @param value return location for the array
3300 * @param len return location for the array length
3301 * @returns #TRUE on success
3304 dbus_message_iter_get_double_array (DBusMessageIter *iter,
3308 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3311 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3313 pos = dbus_message_iter_get_data_start (real, &type);
3315 _dbus_assert (type == DBUS_TYPE_ARRAY);
3317 type = iter_get_array_type (real, NULL);
3318 _dbus_assert (type == DBUS_TYPE_DOUBLE);
3320 if (!_dbus_demarshal_double_array (&real->message->body, real->message->byte_order,
3321 pos, NULL, value, len))
3328 * Returns the string array that the iterator may point to.
3329 * Note that you need to check that the iterator points
3330 * to a string array prior to using this function.
3332 * The returned value is a #NULL-terminated array of strings.
3333 * Each string is a separate malloc block, and the array
3334 * itself is a malloc block. You can free this type of
3335 * string array with dbus_free_string_array().
3337 * @param iter the iterator
3338 * @param value return location for string values
3339 * @param len return location for length of byte array
3340 * @returns #TRUE on success
3343 dbus_message_iter_get_string_array (DBusMessageIter *iter,
3347 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3350 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3352 pos = dbus_message_iter_get_data_start (real, &type);
3354 _dbus_assert (type == DBUS_TYPE_ARRAY);
3356 type = iter_get_array_type (real, NULL);
3357 _dbus_assert (type == DBUS_TYPE_STRING);
3359 if (!_dbus_demarshal_string_array (&real->message->body, real->message->byte_order,
3360 pos, NULL, value, len))
3368 * @todo FIXME to implement this _dbus_demarshal_object_path_array()
3369 * needs implementing
3371 * Returns the object path array that the iterator may point to.
3372 * Note that you need to check that the iterator points
3373 * to an object path array prior to using this function.
3375 * The returned value is a #NULL-terminated array of strings.
3376 * Each string is a separate malloc block, and the array
3377 * itself is a malloc block. You can free this type of
3378 * array with dbus_free_string_array().
3380 * @param iter the iterator
3381 * @param value return location for string values
3382 * @param len return location for length of byte array
3383 * @returns #TRUE on success
3386 dbus_message_iter_get_object_path_array (DBusMessageIter *iter,
3390 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3393 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3395 pos = dbus_message_iter_get_data_start (real, &type);
3397 _dbus_assert (type == DBUS_TYPE_ARRAY);
3399 type = iter_get_array_type (real, NULL);
3400 _dbus_assert (type == DBUS_TYPE_OBJECT_PATH);
3402 if (!_dbus_demarshal_object_path_array (&real->message->body, real->message->byte_order,
3403 pos, NULL, value, len))
3411 * Returns the key name fot the dict entry that an iterator
3412 * may point to. Note that you need to check that the iterator
3413 * points to a dict entry before using this function.
3415 * @see dbus_message_iter_init_dict_iterator
3416 * @param iter the message iter
3417 * @returns the key name
3420 dbus_message_iter_get_dict_key (DBusMessageIter *iter)
3422 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3424 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
3426 _dbus_assert (real->type == DBUS_MESSAGE_ITER_TYPE_DICT);
3428 return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
3433 * Initializes a DBusMessageIter pointing to the end of the
3434 * message. This iterator can be used to append data to the
3437 * @param message the message
3438 * @param iter pointer to an iterator to initialize
3441 dbus_message_append_iter_init (DBusMessage *message,
3442 DBusMessageIter *iter)
3444 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3446 _dbus_return_if_fail (message != NULL);
3447 _dbus_return_if_fail (iter != NULL);
3449 real->message = message;
3450 real->parent_iter = NULL;
3451 real->changed_stamp = message->changed_stamp;
3453 real->type = DBUS_MESSAGE_ITER_TYPE_MESSAGE;
3454 real->end = _dbus_string_get_length (&real->message->body);
3455 real->pos = real->end;
3457 real->container_length_pos = 0;
3458 real->wrote_dict_key = 0;
3461 #ifndef DBUS_DISABLE_CHECKS
3463 dbus_message_iter_append_check (DBusMessageRealIter *iter)
3467 _dbus_warn ("dbus iterator check failed: NULL iterator\n");
3471 if (iter->message->locked)
3473 _dbus_warn ("dbus iterator check failed: message is locked (has already been sent)\n");
3477 if (iter->changed_stamp != iter->message->changed_stamp)
3479 _dbus_warn ("dbus iterator check failed: invalid iterator, must re-initialize it after modifying the message");
3483 if (iter->pos != iter->end)
3485 _dbus_warn ("dbus iterator check failed: can only append at end of message");
3489 if (iter->pos != _dbus_string_get_length (&iter->message->body))
3491 _dbus_warn ("dbus iterator check failed: append pos not at end of message string");
3497 #endif /* DBUS_DISABLE_CHECKS */
3500 dbus_message_iter_append_type (DBusMessageRealIter *iter,
3506 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
3507 if (!_dbus_string_append_byte (&iter->message->signature, type))
3510 if (!_dbus_string_append_byte (&iter->message->body, type))
3512 _dbus_string_shorten (&iter->message->signature, 1);
3517 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
3518 data = _dbus_string_get_const_data_len (&iter->message->body,
3519 iter->array_type_pos, 1);
3522 _dbus_warn ("Appended element of wrong type for array\n");
3527 case DBUS_MESSAGE_ITER_TYPE_DICT:
3528 if (!iter->wrote_dict_key)
3530 _dbus_warn ("Appending dict data before key name\n");
3534 if (!_dbus_string_append_byte (&iter->message->body, type))
3540 _dbus_assert_not_reached ("Invalid iter type");
3548 dbus_message_iter_update_after_change (DBusMessageRealIter *iter)
3550 iter->changed_stamp = iter->message->changed_stamp;
3552 /* Set new end of iter */
3553 iter->end = _dbus_string_get_length (&iter->message->body);
3554 iter->pos = iter->end;
3556 /* Set container length */
3557 if (iter->type == DBUS_MESSAGE_ITER_TYPE_DICT ||
3558 (iter->type == DBUS_MESSAGE_ITER_TYPE_ARRAY && iter->array_type_done))
3559 _dbus_marshal_set_uint32 (&iter->message->body,
3560 iter->message->byte_order,
3561 iter->container_length_pos,
3562 iter->end - iter->container_start);
3564 if (iter->parent_iter)
3565 dbus_message_iter_update_after_change (iter->parent_iter);
3569 dbus_message_iter_append_done (DBusMessageRealIter *iter)
3571 iter->message->changed_stamp++;
3572 dbus_message_iter_update_after_change (iter);
3573 iter->wrote_dict_key = FALSE;
3577 * Appends a nil value to the message
3579 * @param iter an iterator pointing to the end of the message
3580 * @returns #TRUE on success
3583 dbus_message_iter_append_nil (DBusMessageIter *iter)
3585 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3587 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3589 if (!dbus_message_iter_append_type (real, DBUS_TYPE_NIL))
3592 dbus_message_iter_append_done (real);
3598 * Appends a boolean value to the message
3600 * @param iter an iterator pointing to the end of the message
3601 * @param value the boolean value
3602 * @returns #TRUE on success
3605 dbus_message_iter_append_boolean (DBusMessageIter *iter,
3608 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3610 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3612 if (!dbus_message_iter_append_type (real, DBUS_TYPE_BOOLEAN))
3615 if (!_dbus_string_append_byte (&real->message->body, (value != FALSE)))
3617 _dbus_string_set_length (&real->message->body, real->pos);
3621 dbus_message_iter_append_done (real);
3627 * Appends a byte to the message
3629 * @param iter an iterator pointing to the end of the message
3630 * @param value the byte value
3631 * @returns #TRUE on success
3634 dbus_message_iter_append_byte (DBusMessageIter *iter,
3635 unsigned char value)
3637 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3639 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3641 if (!dbus_message_iter_append_type (real, DBUS_TYPE_BYTE))
3644 if (!_dbus_string_append_byte (&real->message->body, value))
3646 _dbus_string_set_length (&real->message->body, real->pos);
3650 dbus_message_iter_append_done (real);
3657 * Appends a 32 bit signed integer to the message.
3659 * @param iter an iterator pointing to the end of the message
3660 * @param value the integer value
3661 * @returns #TRUE on success
3664 dbus_message_iter_append_int32 (DBusMessageIter *iter,
3667 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3669 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3671 if (!dbus_message_iter_append_type (real, DBUS_TYPE_INT32))
3674 if (!_dbus_marshal_int32 (&real->message->body, real->message->byte_order, value))
3676 _dbus_string_set_length (&real->message->body, real->pos);
3680 dbus_message_iter_append_done (real);
3686 * Appends a 32 bit unsigned integer to the message.
3688 * @param iter an iterator pointing to the end of the message
3689 * @param value the integer value
3690 * @returns #TRUE on success
3693 dbus_message_iter_append_uint32 (DBusMessageIter *iter,
3694 dbus_uint32_t value)
3696 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3698 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3700 if (!dbus_message_iter_append_type (real, DBUS_TYPE_UINT32))
3703 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, value))
3705 _dbus_string_set_length (&real->message->body, real->pos);
3709 dbus_message_iter_append_done (real);
3714 #ifdef DBUS_HAVE_INT64
3717 * Appends a 64 bit signed integer to the message.
3719 * This function only exists if #DBUS_HAVE_INT64 is defined.
3721 * @param iter an iterator pointing to the end of the message
3722 * @param value the integer value
3723 * @returns #TRUE on success
3726 dbus_message_iter_append_int64 (DBusMessageIter *iter,
3729 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3731 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3733 if (!dbus_message_iter_append_type (real, DBUS_TYPE_INT64))
3736 if (!_dbus_marshal_int64 (&real->message->body, real->message->byte_order, value))
3738 _dbus_string_set_length (&real->message->body, real->pos);
3742 dbus_message_iter_append_done (real);
3748 * Appends a 64 bit unsigned integer to the message.
3750 * This function only exists if #DBUS_HAVE_INT64 is defined.
3752 * @param iter an iterator pointing to the end of the message
3753 * @param value the integer value
3754 * @returns #TRUE on success
3757 dbus_message_iter_append_uint64 (DBusMessageIter *iter,
3758 dbus_uint64_t value)
3760 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3762 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3764 if (!dbus_message_iter_append_type (real, DBUS_TYPE_UINT64))
3767 if (!_dbus_marshal_uint64 (&real->message->body, real->message->byte_order, value))
3769 _dbus_string_set_length (&real->message->body, real->pos);
3773 dbus_message_iter_append_done (real);
3778 #endif /* DBUS_HAVE_INT64 */
3781 * Appends a double value to the message.
3783 * @param iter an iterator pointing to the end of the message
3784 * @param value the double value
3785 * @returns #TRUE on success
3788 dbus_message_iter_append_double (DBusMessageIter *iter,
3791 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3793 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3795 if (!dbus_message_iter_append_type (real, DBUS_TYPE_DOUBLE))
3798 if (!_dbus_marshal_double (&real->message->body, real->message->byte_order, value))
3800 _dbus_string_set_length (&real->message->body, real->pos);
3804 dbus_message_iter_append_done (real);
3810 * Appends a UTF-8 string to the message.
3812 * @todo add return_val_if_fail(UTF-8 is valid)
3814 * @param iter an iterator pointing to the end of the message
3815 * @param value the string
3816 * @returns #TRUE on success
3819 dbus_message_iter_append_string (DBusMessageIter *iter,
3822 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3824 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3826 if (!dbus_message_iter_append_type (real, DBUS_TYPE_STRING))
3829 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
3831 _dbus_string_set_length (&real->message->body, real->pos);
3835 dbus_message_iter_append_done (real);
3841 * Appends a custom type data chunk to the message. A custom
3842 * type is simply an arbitrary UTF-8 string used as a type
3843 * tag, plus an array of arbitrary bytes to be interpreted
3844 * according to the type tag.
3846 * @param iter an iterator pointing to the end of the message
3847 * @param name the name of the type
3848 * @param data the binary data used to store the value
3849 * @param len the length of the binary data in bytes
3850 * @returns #TRUE on success
3853 dbus_message_iter_append_custom (DBusMessageIter *iter,
3855 const unsigned char *data,
3858 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3860 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3862 if (!dbus_message_iter_append_type (real, DBUS_TYPE_CUSTOM))
3865 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, name))
3867 _dbus_string_set_length (&real->message->body, real->pos);
3871 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, data, len))
3873 _dbus_string_set_length (&real->message->body, real->pos);
3877 dbus_message_iter_append_done (real);
3884 * Appends a dict key name to the message. The iterator used
3885 * must point to a dict.
3887 * @param iter an iterator pointing to the end of the message
3888 * @param value the string
3889 * @returns #TRUE on success
3892 dbus_message_iter_append_dict_key (DBusMessageIter *iter,
3895 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3897 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3898 _dbus_assert (real->type == DBUS_MESSAGE_ITER_TYPE_DICT);
3900 if (real->wrote_dict_key)
3902 _dbus_warn ("Appendinging multiple dict key names\n");
3906 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
3911 dbus_message_iter_append_done (real);
3912 real->wrote_dict_key = TRUE;
3918 array_iter_type_mark_done (DBusMessageRealIter *iter)
3922 if (iter->type == DBUS_MESSAGE_ITER_TYPE_ARRAY)
3923 array_iter_type_mark_done (iter->parent_iter);
3927 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&iter->message->body),
3928 sizeof (dbus_uint32_t));
3930 /* Empty length for now, backfill later */
3931 if (!_dbus_marshal_uint32 (&iter->message->body, iter->message->byte_order, 0))
3933 _dbus_string_set_length (&iter->message->body, iter->pos);
3937 iter->container_start = _dbus_string_get_length (&iter->message->body);
3938 iter->container_length_pos = len_pos;
3939 iter->array_type_done = TRUE;
3945 append_array_type (DBusMessageRealIter *real,
3947 dbus_bool_t *array_type_done,
3948 int *array_type_pos)
3950 int existing_element_type;
3952 if (!dbus_message_iter_append_type (real, DBUS_TYPE_ARRAY))
3955 if (real->type == DBUS_MESSAGE_ITER_TYPE_ARRAY &&
3956 real->array_type_done)
3958 existing_element_type = iter_get_array_type (real, array_type_pos);
3959 if (existing_element_type != element_type)
3961 _dbus_warn ("Appending array of %s, when expecting array of %s\n",
3962 _dbus_type_to_string (element_type),
3963 _dbus_type_to_string (existing_element_type));
3964 _dbus_string_set_length (&real->message->body, real->pos);
3967 if (array_type_done != NULL)
3968 *array_type_done = TRUE;
3972 if (array_type_pos != NULL)
3973 *array_type_pos = _dbus_string_get_length (&real->message->body);
3976 if (!_dbus_string_append_byte (&real->message->signature, element_type))
3978 _dbus_string_set_length (&real->message->body, real->pos);
3982 /* Append element type */
3983 if (!_dbus_string_append_byte (&real->message->body, element_type))
3985 _dbus_string_shorten (&real->message->signature, 1);
3986 _dbus_string_set_length (&real->message->body, real->pos);
3990 if (array_type_done != NULL)
3991 *array_type_done = element_type != DBUS_TYPE_ARRAY;
3993 if (element_type != DBUS_TYPE_ARRAY &&
3994 !array_iter_type_mark_done (real))
3996 _dbus_string_shorten (&real->message->signature, 1);
4005 * Appends an array to the message and initializes an iterator that
4006 * can be used to append to the array.
4008 * @param iter an iterator pointing to the end of the message
4009 * @param array_iter pointer to an iter that will be initialized
4010 * @param element_type the type of the array elements
4011 * @returns #TRUE on success
4014 dbus_message_iter_append_array (DBusMessageIter *iter,
4015 DBusMessageIter *array_iter,
4018 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4019 DBusMessageRealIter *array_real = (DBusMessageRealIter *)array_iter;
4022 dbus_bool_t array_type_done;
4024 if (element_type == DBUS_TYPE_NIL)
4026 _dbus_warn ("Can't create NIL arrays\n");
4030 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4032 if (!append_array_type (real, element_type, &array_type_done, &array_type_pos))
4035 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
4037 if (array_type_done)
4039 /* Empty length for now, backfill later */
4040 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
4042 _dbus_string_set_length (&real->message->body, real->pos);
4047 array_real->parent_iter = real;
4048 array_real->message = real->message;
4049 array_real->changed_stamp = real->message->changed_stamp;
4051 array_real->type = DBUS_MESSAGE_ITER_TYPE_ARRAY;
4052 array_real->pos = _dbus_string_get_length (&real->message->body);
4053 array_real->end = array_real->end;
4055 array_real->container_start = array_real->pos;
4056 array_real->container_length_pos = len_pos;
4057 array_real->wrote_dict_key = 0;
4058 array_real->array_type_done = array_type_done;
4059 array_real->array_type_pos = array_type_pos;
4061 dbus_message_iter_append_done (array_real);
4067 * Appends a dict to the message and initializes an iterator that
4068 * can be used to append to the dict.
4070 * @param iter an iterator pointing to the end of the message
4071 * @param dict_iter pointer to an iter that will be initialized
4072 * @returns #TRUE on success
4075 dbus_message_iter_append_dict (DBusMessageIter *iter,
4076 DBusMessageIter *dict_iter)
4078 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4079 DBusMessageRealIter *dict_real = (DBusMessageRealIter *)dict_iter;
4082 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4084 if (!dbus_message_iter_append_type (real, DBUS_TYPE_DICT))
4087 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
4089 /* Empty length for now, backfill later */
4090 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
4092 _dbus_string_set_length (&real->message->body, real->pos);
4096 dict_real->parent_iter = real;
4097 dict_real->message = real->message;
4098 dict_real->changed_stamp = real->message->changed_stamp;
4100 dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
4101 dict_real->pos = _dbus_string_get_length (&real->message->body);
4102 dict_real->end = dict_real->end;
4104 dict_real->container_start = dict_real->pos;
4105 dict_real->container_length_pos = len_pos;
4106 dict_real->wrote_dict_key = 0;
4108 dbus_message_iter_append_done (dict_real);
4115 * Appends a boolean array to the message.
4117 * @param iter an iterator pointing to the end of the message
4118 * @param value the array
4119 * @param len the length of the array
4120 * @returns #TRUE on success
4123 dbus_message_iter_append_boolean_array (DBusMessageIter *iter,
4124 unsigned const char *value,
4127 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4129 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4131 if (!append_array_type (real, DBUS_TYPE_BOOLEAN, NULL, NULL))
4134 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, value, len))
4136 _dbus_string_set_length (&real->message->body, real->pos);
4140 dbus_message_iter_append_done (real);
4146 * Appends a 32 bit signed integer array to the message.
4148 * @param iter an iterator pointing to the end of the message
4149 * @param value the array
4150 * @param len the length of the array
4151 * @returns #TRUE on success
4154 dbus_message_iter_append_int32_array (DBusMessageIter *iter,
4155 const dbus_int32_t *value,
4158 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4160 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4162 if (!append_array_type (real, DBUS_TYPE_INT32, NULL, NULL))
4165 if (!_dbus_marshal_int32_array (&real->message->body, real->message->byte_order, value, len))
4167 _dbus_string_set_length (&real->message->body, real->pos);
4171 dbus_message_iter_append_done (real);
4177 * Appends a 32 bit unsigned integer array to the message.
4179 * @param iter an iterator pointing to the end of the message
4180 * @param value the array
4181 * @param len the length of the array
4182 * @returns #TRUE on success
4185 dbus_message_iter_append_uint32_array (DBusMessageIter *iter,
4186 const dbus_uint32_t *value,
4189 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4191 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4193 if (!append_array_type (real, DBUS_TYPE_UINT32, NULL, NULL))
4196 if (!_dbus_marshal_uint32_array (&real->message->body, real->message->byte_order, value, len))
4198 _dbus_string_set_length (&real->message->body, real->pos);
4202 dbus_message_iter_append_done (real);
4207 #ifdef DBUS_HAVE_INT64
4210 * Appends a 64 bit signed integer array to the message.
4212 * This function only exists if #DBUS_HAVE_INT64 is defined.
4214 * @param iter an iterator pointing to the end of the message
4215 * @param value the array
4216 * @param len the length of the array
4217 * @returns #TRUE on success
4220 dbus_message_iter_append_int64_array (DBusMessageIter *iter,
4221 const dbus_int64_t *value,
4224 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4226 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4228 if (!append_array_type (real, DBUS_TYPE_INT64, NULL, NULL))
4231 if (!_dbus_marshal_int64_array (&real->message->body, real->message->byte_order, value, len))
4233 _dbus_string_set_length (&real->message->body, real->pos);
4237 dbus_message_iter_append_done (real);
4243 * Appends a 64 bit unsigned integer array to the message.
4245 * This function only exists if #DBUS_HAVE_INT64 is defined.
4247 * @param iter an iterator pointing to the end of the message
4248 * @param value the array
4249 * @param len the length of the array
4250 * @returns #TRUE on success
4253 dbus_message_iter_append_uint64_array (DBusMessageIter *iter,
4254 const dbus_uint64_t *value,
4257 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4259 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4261 if (!append_array_type (real, DBUS_TYPE_UINT64, NULL, NULL))
4264 if (!_dbus_marshal_uint64_array (&real->message->body, real->message->byte_order, value, len))
4266 _dbus_string_set_length (&real->message->body, real->pos);
4270 dbus_message_iter_append_done (real);
4274 #endif /* DBUS_HAVE_INT64 */
4277 * Appends a double array to the message.
4279 * @param iter an iterator pointing to the end of the message
4280 * @param value the array
4281 * @param len the length of the array
4282 * @returns #TRUE on success
4285 dbus_message_iter_append_double_array (DBusMessageIter *iter,
4286 const double *value,
4289 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4291 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4293 if (!append_array_type (real, DBUS_TYPE_DOUBLE, NULL, NULL))
4296 if (!_dbus_marshal_double_array (&real->message->body, real->message->byte_order, value, len))
4298 _dbus_string_set_length (&real->message->body, real->pos);
4302 dbus_message_iter_append_done (real);
4308 * Appends a byte array to the message.
4310 * @param iter an iterator pointing to the end of the message
4311 * @param value the array
4312 * @param len the length of the array
4313 * @returns #TRUE on success
4316 dbus_message_iter_append_byte_array (DBusMessageIter *iter,
4317 unsigned const char *value,
4320 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4322 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4324 if (!append_array_type (real, DBUS_TYPE_BYTE, NULL, NULL))
4327 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, value, len))
4329 _dbus_string_set_length (&real->message->body, real->pos);
4333 dbus_message_iter_append_done (real);
4339 * Appends a string array to the message.
4341 * @param iter an iterator pointing to the end of the message
4342 * @param value the array
4343 * @param len the length of the array
4344 * @returns #TRUE on success
4347 dbus_message_iter_append_string_array (DBusMessageIter *iter,
4351 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4353 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4355 if (!append_array_type (real, DBUS_TYPE_STRING, NULL, NULL))
4358 if (!_dbus_marshal_string_array (&real->message->body, real->message->byte_order, value, len))
4360 _dbus_string_set_length (&real->message->body, real->pos);
4364 dbus_message_iter_append_done (real);
4370 * Sets the message sender.
4372 * @param message the message
4373 * @param sender the sender
4374 * @returns #FALSE if not enough memory
4377 dbus_message_set_sender (DBusMessage *message,
4380 _dbus_return_val_if_fail (message != NULL, FALSE);
4381 _dbus_return_val_if_fail (!message->locked, FALSE);
4383 return set_string_field (message,
4384 DBUS_HEADER_FIELD_SENDER_SERVICE,
4390 * Sets a flag indicating that the message does not want a reply; if
4391 * this flag is set, the other end of the connection may (but is not
4392 * required to) optimize by not sending method return or error
4393 * replies. If this flag is set, there is no way to know whether the
4394 * message successfully arrived at the remote end.
4396 * @param message the message
4397 * @param no_reply #TRUE if no reply is desired
4400 dbus_message_set_no_reply (DBusMessage *message,
4401 dbus_bool_t no_reply)
4405 _dbus_return_if_fail (message != NULL);
4406 _dbus_return_if_fail (!message->locked);
4408 header = _dbus_string_get_data_len (&message->header, FLAGS_OFFSET, 1);
4411 *header |= DBUS_HEADER_FLAG_NO_REPLY_EXPECTED;
4413 *header &= ~DBUS_HEADER_FLAG_NO_REPLY_EXPECTED;
4417 * Returns #TRUE if the message does not expect
4420 * @param message the message
4421 * @returns #TRUE if the message sender isn't waiting for a reply
4424 dbus_message_get_no_reply (DBusMessage *message)
4428 _dbus_return_val_if_fail (message != NULL, FALSE);
4430 header = _dbus_string_get_const_data_len (&message->header, FLAGS_OFFSET, 1);
4432 return (*header & DBUS_HEADER_FLAG_NO_REPLY_EXPECTED) != 0;
4436 * Gets the service which originated this message,
4437 * or #NULL if unknown or inapplicable.
4439 * @param message the message
4440 * @returns the service name or #NULL
4443 dbus_message_get_sender (DBusMessage *message)
4445 _dbus_return_val_if_fail (message != NULL, NULL);
4447 return get_string_field (message,
4448 DBUS_HEADER_FIELD_SENDER_SERVICE,
4453 * Gets the type signature of the message, i.e. the arguments in the
4454 * message payload. The signature includes only "in" arguments for
4455 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
4456 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
4457 * what you might expect (it does not include the signature of the
4458 * entire C++-style method).
4460 * The signature is a string made up of type codes such
4461 * as #DBUS_TYPE_STRING. The string is terminated with nul
4462 * (nul is also the value of #DBUS_TYPE_INVALID).
4464 * @param message the message
4465 * @returns the type signature
4468 dbus_message_get_signature (DBusMessage *message)
4470 _dbus_return_val_if_fail (message != NULL, NULL);
4472 return _dbus_string_get_const_data (&message->signature);
4476 _dbus_message_has_type_interface_member (DBusMessage *message,
4478 const char *interface,
4483 _dbus_assert (message != NULL);
4484 _dbus_assert (interface != NULL);
4485 _dbus_assert (method != NULL);
4487 if (dbus_message_get_type (message) != type)
4490 /* Optimize by checking the short method name first
4491 * instead of the longer interface name
4494 n = dbus_message_get_member (message);
4496 if (n && strcmp (n, method) == 0)
4498 n = dbus_message_get_interface (message);
4500 if (n && strcmp (n, interface) == 0)
4508 * Checks whether the message is a method call with the given
4509 * interface and member fields. If the message is not
4510 * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or member field,
4513 * @param message the message
4514 * @param interface the name to check (must not be #NULL)
4515 * @param method the name to check (must not be #NULL)
4517 * @returns #TRUE if the message is the specified method call
4520 dbus_message_is_method_call (DBusMessage *message,
4521 const char *interface,
4524 _dbus_return_val_if_fail (message != NULL, FALSE);
4525 _dbus_return_val_if_fail (interface != NULL, FALSE);
4526 _dbus_return_val_if_fail (method != NULL, FALSE);
4528 return _dbus_message_has_type_interface_member (message,
4529 DBUS_MESSAGE_TYPE_METHOD_CALL,
4534 * Checks whether the message is a signal with the given
4535 * interface and member fields. If the message is not
4536 * #DBUS_MESSAGE_TYPE_SIGNAL, or has a different interface or member field,
4539 * @param message the message
4540 * @param interface the name to check (must not be #NULL)
4541 * @param signal_name the name to check (must not be #NULL)
4543 * @returns #TRUE if the message is the specified signal
4546 dbus_message_is_signal (DBusMessage *message,
4547 const char *interface,
4548 const char *signal_name)
4550 _dbus_return_val_if_fail (message != NULL, FALSE);
4551 _dbus_return_val_if_fail (interface != NULL, FALSE);
4552 _dbus_return_val_if_fail (signal_name != NULL, FALSE);
4554 return _dbus_message_has_type_interface_member (message,
4555 DBUS_MESSAGE_TYPE_SIGNAL,
4556 interface, signal_name);
4560 * Checks whether the message is an error reply with the given error
4561 * name. If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
4562 * different name, returns #FALSE.
4564 * @param message the message
4565 * @param error_name the name to check (must not be #NULL)
4567 * @returns #TRUE if the message is the specified error
4570 dbus_message_is_error (DBusMessage *message,
4571 const char *error_name)
4575 _dbus_return_val_if_fail (message != NULL, FALSE);
4576 _dbus_return_val_if_fail (error_name != NULL, FALSE);
4578 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
4581 n = dbus_message_get_error_name (message);
4583 if (n && strcmp (n, error_name) == 0)
4590 * Checks whether the message was sent to the given service. If the
4591 * message has no service specified or has a different name, returns
4594 * @param message the message
4595 * @param service the service to check (must not be #NULL)
4597 * @returns #TRUE if the message has the given destination service
4600 dbus_message_has_destination (DBusMessage *message,
4601 const char *service)
4605 _dbus_return_val_if_fail (message != NULL, FALSE);
4606 _dbus_return_val_if_fail (service != NULL, FALSE);
4608 s = dbus_message_get_destination (message);
4610 if (s && strcmp (s, service) == 0)
4617 * Checks whether the message has the given service as its sender. If
4618 * the message has no sender specified or has a different sender,
4619 * returns #FALSE. Note that if a peer application owns multiple
4620 * services, its messages will have only one of those services as the
4621 * sender (usually the base service). So you can't use this
4622 * function to prove the sender didn't own service Foo, you can
4623 * only use it to prove that it did.
4625 * @param message the message
4626 * @param service the service to check (must not be #NULL)
4628 * @returns #TRUE if the message has the given origin service
4631 dbus_message_has_sender (DBusMessage *message,
4632 const char *service)
4636 _dbus_return_val_if_fail (message != NULL, FALSE);
4637 _dbus_return_val_if_fail (service != NULL, FALSE);
4639 s = dbus_message_get_sender (message);
4641 if (s && strcmp (s, service) == 0)
4648 * Checks whether the message has the given signature;
4649 * see dbus_message_get_signature() for more details on
4650 * what the signature looks like.
4652 * @param message the message
4653 * @param signature typecode array
4654 * @returns #TRUE if message has the given signature
4657 dbus_message_has_signature (DBusMessage *message,
4658 const char *signature)
4660 _dbus_return_val_if_fail (message != NULL, FALSE);
4661 _dbus_return_val_if_fail (signature != NULL, FALSE);
4663 return _dbus_string_equal_c_str (&message->signature, signature);
4667 * Sets a #DBusError based on the contents of the given
4668 * message. The error is only set if the message
4669 * is an error message, as in DBUS_MESSAGE_TYPE_ERROR.
4670 * The name of the error is set to the name of the message,
4671 * and the error message is set to the first argument
4672 * if the argument exists and is a string.
4674 * The return value indicates whether the error was set (the error is
4675 * set if and only if the message is an error message).
4676 * So you can check for an error reply and convert it to DBusError
4679 * @param error the error to set
4680 * @param message the message to set it from
4681 * @returns #TRUE if dbus_message_get_is_error() returns #TRUE for the message
4684 dbus_set_error_from_message (DBusError *error,
4685 DBusMessage *message)
4689 _dbus_return_val_if_fail (message != NULL, FALSE);
4690 _dbus_return_val_if_error_is_set (error, FALSE);
4692 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
4696 dbus_message_get_args (message, NULL,
4697 DBUS_TYPE_STRING, &str,
4700 dbus_set_error (error, dbus_message_get_error_name (message),
4701 str ? "%s" : NULL, str);
4711 * @addtogroup DBusMessageInternals
4716 * @typedef DBusMessageLoader
4718 * The DBusMessageLoader object encapsulates the process of converting
4719 * a byte stream into a series of DBusMessage. It buffers the incoming
4720 * bytes as efficiently as possible, and generates a queue of
4721 * messages. DBusMessageLoader is typically used as part of a
4722 * DBusTransport implementation. The DBusTransport then hands off
4723 * the loaded messages to a DBusConnection, making the messages
4724 * visible to the application.
4726 * @todo write tests for break-loader that a) randomly delete header
4727 * fields and b) set string fields to zero-length and other funky
4732 /* we definitely use signed ints for sizes, so don't exceed
4733 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
4734 * over 128M is pretty nuts anyhow.
4738 * The maximum sane message size.
4740 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
4743 * Implementation details of DBusMessageLoader.
4744 * All members are private.
4746 struct DBusMessageLoader
4748 int refcount; /**< Reference count. */
4750 DBusString data; /**< Buffered data */
4752 DBusList *messages; /**< Complete messages. */
4754 long max_message_size; /**< Maximum size of a message */
4756 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
4758 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
4762 * The initial buffer size of the message loader.
4764 * @todo this should be based on min header size plus some average
4765 * body size, or something. Or rather, the min header size only, if we
4766 * want to try to read only the header, store that in a DBusMessage,
4767 * then read only the body and store that, etc., depends on
4768 * how we optimize _dbus_message_loader_get_buffer() and what
4769 * the exact message format is.
4771 #define INITIAL_LOADER_DATA_LEN 32
4774 * Creates a new message loader. Returns #NULL if memory can't
4777 * @returns new loader, or #NULL.
4780 _dbus_message_loader_new (void)
4782 DBusMessageLoader *loader;
4784 loader = dbus_new0 (DBusMessageLoader, 1);
4788 loader->refcount = 1;
4790 /* Try to cap message size at something that won't *totally* hose
4791 * the system if we have a couple of them.
4793 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
4795 if (!_dbus_string_init (&loader->data))
4801 /* preallocate the buffer for speed, ignore failure */
4802 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
4803 _dbus_string_set_length (&loader->data, 0);
4809 * Increments the reference count of the loader.
4811 * @param loader the loader.
4814 _dbus_message_loader_ref (DBusMessageLoader *loader)
4816 loader->refcount += 1;
4820 * Decrements the reference count of the loader and finalizes the
4821 * loader when the count reaches zero.
4823 * @param loader the loader.
4826 _dbus_message_loader_unref (DBusMessageLoader *loader)
4828 loader->refcount -= 1;
4829 if (loader->refcount == 0)
4831 _dbus_list_foreach (&loader->messages,
4832 (DBusForeachFunction) dbus_message_unref,
4834 _dbus_list_clear (&loader->messages);
4835 _dbus_string_free (&loader->data);
4841 * Gets the buffer to use for reading data from the network. Network
4842 * data is read directly into an allocated buffer, which is then used
4843 * in the DBusMessage, to avoid as many extra memcpy's as possible.
4844 * The buffer must always be returned immediately using
4845 * _dbus_message_loader_return_buffer(), even if no bytes are
4846 * successfully read.
4848 * @todo this function can be a lot more clever. For example
4849 * it can probably always return a buffer size to read exactly
4850 * the body of the next message, thus avoiding any memory wastage
4853 * @todo we need to enforce a max length on strings in header fields.
4855 * @param loader the message loader.
4856 * @param buffer the buffer
4859 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
4860 DBusString **buffer)
4862 _dbus_assert (!loader->buffer_outstanding);
4864 *buffer = &loader->data;
4866 loader->buffer_outstanding = TRUE;
4870 * The smallest header size that can occur.
4871 * (It won't be valid)
4873 #define DBUS_MINIMUM_HEADER_SIZE 16
4876 decode_string_field (const DBusString *data,
4878 HeaderField *header_field,
4879 DBusString *field_data,
4883 int string_data_pos;
4885 _dbus_assert (header_field != NULL);
4886 _dbus_assert (field_data != NULL);
4888 if (header_field->name_offset >= 0)
4890 _dbus_verbose ("%s field provided twice\n",
4891 _dbus_header_field_to_string (field));
4895 if (type != DBUS_TYPE_STRING)
4897 _dbus_verbose ("%s field has wrong type %s\n",
4898 _dbus_header_field_to_string (field),
4899 _dbus_type_to_string (type));
4903 /* skip padding after typecode, skip string length;
4904 * we assume that the string arg has already been validated
4905 * for sanity and UTF-8
4907 string_data_pos = _DBUS_ALIGN_VALUE (pos, 4) + 4;
4908 _dbus_assert (string_data_pos < _dbus_string_get_length (data));
4910 _dbus_string_init_const (field_data,
4911 _dbus_string_get_const_data (data) + string_data_pos);
4913 header_field->name_offset = pos;
4914 header_field->value_offset = _DBUS_ALIGN_VALUE (pos, 4);
4917 _dbus_verbose ("Found field %s at offset %d\n",
4918 _dbus_header_field_to_string (field),
4919 header_field->value_offset);
4925 /* FIXME because the service/interface/member/error names are already
4926 * validated to be in the particular ASCII subset, UTF-8 validating
4927 * them could be skipped as a probably-interesting optimization.
4928 * The UTF-8 validation definitely shows up in profiles.
4931 decode_header_data (const DBusString *data,
4935 HeaderField fields[DBUS_HEADER_FIELD_LAST + 1],
4936 int *message_padding)
4938 DBusString field_data;
4944 if (header_len < 16)
4946 _dbus_verbose ("Header length %d is too short\n", header_len);
4951 while (i <= DBUS_HEADER_FIELD_LAST)
4953 fields[i].name_offset = -1;
4954 fields[i].value_offset = -1;
4959 while (pos < header_len)
4961 field = _dbus_string_get_byte (data, pos);
4962 if (field == DBUS_HEADER_FIELD_INVALID)
4963 break; /* Must be padding */
4966 if (!_dbus_marshal_validate_type (data, pos, &type, &pos))
4968 _dbus_verbose ("Failed to validate type of named header field pos = %d\n",
4973 if (!_dbus_marshal_validate_arg (data, byte_order, 0, type, -1, pos, &new_pos))
4975 _dbus_verbose ("Failed to validate argument to named header field pos = %d\n",
4980 if (new_pos > header_len)
4982 _dbus_verbose ("Named header field tries to extend beyond header length\n");
4988 case DBUS_HEADER_FIELD_SERVICE:
4989 if (!decode_string_field (data, field, &fields[field],
4990 &field_data, pos, type))
4993 if (!_dbus_string_validate_service (&field_data, 0,
4994 _dbus_string_get_length (&field_data)))
4996 _dbus_verbose ("service field has invalid content \"%s\"\n",
4997 _dbus_string_get_const_data (&field_data));
5002 case DBUS_HEADER_FIELD_INTERFACE:
5003 if (!decode_string_field (data, field, &fields[field],
5004 &field_data, pos, type))
5007 if (!_dbus_string_validate_interface (&field_data, 0,
5008 _dbus_string_get_length (&field_data)))
5010 _dbus_verbose ("interface field has invalid content \"%s\"\n",
5011 _dbus_string_get_const_data (&field_data));
5015 if (_dbus_string_equal_c_str (&field_data,
5016 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL))
5018 _dbus_verbose ("Message is on the local interface\n");
5023 case DBUS_HEADER_FIELD_MEMBER:
5024 if (!decode_string_field (data, field, &fields[field],
5025 &field_data, pos, type))
5028 if (!_dbus_string_validate_member (&field_data, 0,
5029 _dbus_string_get_length (&field_data)))
5031 _dbus_verbose ("member field has invalid content \"%s\"\n",
5032 _dbus_string_get_const_data (&field_data));
5037 case DBUS_HEADER_FIELD_ERROR_NAME:
5038 if (!decode_string_field (data, field, &fields[field],
5039 &field_data, pos, type))
5042 if (!_dbus_string_validate_error_name (&field_data, 0,
5043 _dbus_string_get_length (&field_data)))
5045 _dbus_verbose ("error-name field has invalid content \"%s\"\n",
5046 _dbus_string_get_const_data (&field_data));
5051 case DBUS_HEADER_FIELD_SENDER_SERVICE:
5052 if (!decode_string_field (data, field, &fields[field],
5053 &field_data, pos, type))
5056 if (!_dbus_string_validate_service (&field_data, 0,
5057 _dbus_string_get_length (&field_data)))
5059 _dbus_verbose ("sender-service field has invalid content \"%s\"\n",
5060 _dbus_string_get_const_data (&field_data));
5065 case DBUS_HEADER_FIELD_PATH:
5067 /* Path was already validated as part of standard
5068 * type validation, since there's an OBJECT_PATH
5072 if (fields[field].name_offset >= 0)
5074 _dbus_verbose ("path field provided twice\n");
5077 if (type != DBUS_TYPE_OBJECT_PATH)
5079 _dbus_verbose ("path field has wrong type\n");
5083 fields[field].name_offset = pos;
5084 fields[field].value_offset = _DBUS_ALIGN_VALUE (pos, 4);
5086 /* No forging signals from the local path */
5089 s = _dbus_string_get_const_data_len (data,
5090 fields[field].value_offset,
5091 _dbus_string_get_length (data) -
5092 fields[field].value_offset);
5093 if (strcmp (s, DBUS_PATH_ORG_FREEDESKTOP_LOCAL) == 0)
5095 _dbus_verbose ("Message is on the local path\n");
5100 _dbus_verbose ("Found path at offset %d\n",
5101 fields[field].value_offset);
5104 case DBUS_HEADER_FIELD_REPLY_SERIAL:
5105 if (fields[field].name_offset >= 0)
5107 _dbus_verbose ("reply field provided twice\n");
5111 if (type != DBUS_TYPE_UINT32)
5113 _dbus_verbose ("reply field has wrong type\n");
5117 fields[field].name_offset = pos;
5118 fields[field].value_offset = _DBUS_ALIGN_VALUE (pos, 4);
5120 _dbus_verbose ("Found reply serial %u at offset %d\n",
5121 _dbus_demarshal_uint32 (data,
5123 fields[field].value_offset,
5125 fields[field].value_offset);
5129 _dbus_verbose ("Ignoring an unknown header field: %d at offset %d\n",
5136 if (pos < header_len)
5138 /* Alignment padding, verify that it's nul */
5139 if ((header_len - pos) >= 8)
5141 _dbus_verbose ("too much header alignment padding\n");
5145 if (!_dbus_string_validate_nul (data,
5146 pos, (header_len - pos)))
5148 _dbus_verbose ("header alignment padding is not nul\n");
5153 /* Depending on message type, enforce presence of certain fields. */
5154 switch (message_type)
5156 case DBUS_MESSAGE_TYPE_SIGNAL:
5157 case DBUS_MESSAGE_TYPE_METHOD_CALL:
5158 if (fields[DBUS_HEADER_FIELD_PATH].value_offset < 0)
5160 _dbus_verbose ("No path field provided\n");
5163 /* FIXME make this optional, only for method calls */
5164 if (fields[DBUS_HEADER_FIELD_INTERFACE].value_offset < 0)
5166 _dbus_verbose ("No interface field provided\n");
5169 if (fields[DBUS_HEADER_FIELD_MEMBER].value_offset < 0)
5171 _dbus_verbose ("No member field provided\n");
5175 case DBUS_MESSAGE_TYPE_ERROR:
5176 if (fields[DBUS_HEADER_FIELD_ERROR_NAME].value_offset < 0)
5178 _dbus_verbose ("No error-name field provided\n");
5181 if (fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset < 0)
5183 _dbus_verbose ("No reply serial field provided in error\n");
5187 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
5188 if (fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset < 0)
5190 _dbus_verbose ("No reply serial field provided in method return\n");
5195 /* An unknown type, spec requires us to ignore it */
5199 if (message_padding)
5200 *message_padding = header_len - pos;
5206 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
5207 * indicating to the loader how many bytes of the buffer were filled
5208 * in. This function must always be called, even if no bytes were
5209 * successfully read.
5211 * @param loader the loader.
5212 * @param buffer the buffer.
5213 * @param bytes_read number of bytes that were read into the buffer.
5216 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
5220 _dbus_assert (loader->buffer_outstanding);
5221 _dbus_assert (buffer == &loader->data);
5223 loader->buffer_outstanding = FALSE;
5227 load_one_message (DBusMessageLoader *loader,
5233 DBusMessage *message;
5234 HeaderField fields[DBUS_HEADER_FIELD_LAST + 1];
5244 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
5247 if (!decode_header_data (&loader->data,
5248 header_len, byte_order,
5250 fields, &header_padding))
5252 _dbus_verbose ("Header was invalid\n");
5253 loader->corrupted = TRUE;
5257 next_arg = header_len;
5258 while (next_arg < (header_len + body_len))
5261 int prev = next_arg;
5263 if (!_dbus_marshal_validate_type (&loader->data, next_arg,
5266 _dbus_verbose ("invalid typecode at offset %d\n", prev);
5267 loader->corrupted = TRUE;
5271 if (!_dbus_marshal_validate_arg (&loader->data,
5278 _dbus_verbose ("invalid type data at %d, next_arg\n", next_arg);
5279 loader->corrupted = TRUE;
5283 _dbus_assert (next_arg > prev);
5286 if (next_arg > (header_len + body_len))
5288 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
5289 next_arg, header_len, body_len,
5290 header_len + body_len);
5291 loader->corrupted = TRUE;
5295 message = dbus_message_new_empty_header ();
5296 if (message == NULL)
5298 _dbus_verbose ("Failed to allocate empty message\n");
5303 message->byte_order = byte_order;
5304 message->header_padding = header_padding;
5306 /* Copy in the offsets we found */
5308 while (i <= DBUS_HEADER_FIELD_LAST)
5310 message->header_fields[i] = fields[i];
5314 if (!_dbus_list_append (&loader->messages, message))
5316 _dbus_verbose ("Failed to append new message to loader queue\n");
5321 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
5322 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
5324 _dbus_assert (_dbus_string_get_length (&loader->data) >=
5325 (header_len + body_len));
5327 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
5329 _dbus_verbose ("Failed to move header into new message\n");
5334 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
5336 _dbus_verbose ("Failed to move body into new message\n");
5342 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
5343 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
5345 /* Fill in caches (we checked the types of these fields
5348 message->reply_serial = get_uint_field (message,
5349 DBUS_HEADER_FIELD_REPLY_SERIAL);
5351 message->client_serial = _dbus_demarshal_uint32 (&message->header,
5352 message->byte_order,
5353 CLIENT_SERIAL_OFFSET,
5355 if (message->client_serial == 0 ||
5356 (message->header_fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset >= 0 && message->reply_serial == 0))
5358 _dbus_verbose ("client_serial = %d reply_serial = %d, one of these no good\n",
5359 message->client_serial,
5360 message->reply_serial);
5362 loader->corrupted = TRUE;
5366 /* Fill in signature (FIXME should do this during validation,
5367 * but I didn't want to spend time on it since we want to change
5368 * the wire format to contain the signature anyway)
5371 DBusMessageIter iter;
5373 dbus_message_iter_init (message, &iter);
5379 t = dbus_message_iter_get_arg_type (&iter);
5380 if (t == DBUS_TYPE_INVALID)
5383 if (!_dbus_string_append_byte (&message->signature,
5386 _dbus_verbose ("failed to append type byte to signature\n");
5391 if (t == DBUS_TYPE_ARRAY)
5393 DBusMessageIter child_iter;
5398 while (array_type == DBUS_TYPE_ARRAY)
5400 DBusMessageIter parent_iter = child_iter;
5401 dbus_message_iter_init_array_iterator (&parent_iter,
5405 if (!_dbus_string_append_byte (&message->signature,
5408 _dbus_verbose ("failed to append array type byte to signature\n");
5416 while (dbus_message_iter_next (&iter));
5419 _dbus_verbose ("Loaded message %p\n", message);
5421 _dbus_assert (!oom);
5422 _dbus_assert (!loader->corrupted);
5430 if (message != NULL)
5432 /* Put the data back so we can try again later if it was an OOM issue */
5433 if (_dbus_string_get_length (&message->body) > 0)
5437 result = _dbus_string_copy_len (&message->body, 0, body_len,
5440 _dbus_assert (result); /* because DBusString never reallocs smaller */
5443 if (_dbus_string_get_length (&message->header) > 0)
5447 result = _dbus_string_copy_len (&message->header, 0, header_len,
5450 _dbus_assert (result); /* because DBusString never reallocs smaller */
5453 /* does nothing if the message isn't in the list */
5454 _dbus_list_remove_last (&loader->messages, message);
5456 dbus_message_unref (message);
5464 * Converts buffered data into messages.
5466 * @todo we need to check that the proper named header fields exist
5467 * for each message type.
5469 * @todo If a message has unknown type, we should probably eat it
5470 * right here rather than passing it out to applications. However
5471 * it's not an error to see messages of unknown type.
5473 * @param loader the loader.
5474 * @returns #TRUE if we had enough memory to finish.
5477 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
5479 while (!loader->corrupted && _dbus_string_get_length (&loader->data) >= 16)
5481 const char *header_data;
5482 int byte_order, message_type, header_len, body_len;
5483 dbus_uint32_t header_len_unsigned, body_len_unsigned;
5485 header_data = _dbus_string_get_const_data_len (&loader->data, 0, 16);
5487 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
5489 if (header_data[VERSION_OFFSET] != DBUS_MAJOR_PROTOCOL_VERSION)
5491 _dbus_verbose ("Message has protocol version %d ours is %d\n",
5492 (int) header_data[VERSION_OFFSET], DBUS_MAJOR_PROTOCOL_VERSION);
5493 loader->corrupted = TRUE;
5497 byte_order = header_data[BYTE_ORDER_OFFSET];
5499 if (byte_order != DBUS_LITTLE_ENDIAN &&
5500 byte_order != DBUS_BIG_ENDIAN)
5502 _dbus_verbose ("Message with bad byte order '%c' received\n",
5504 loader->corrupted = TRUE;
5508 /* Unknown types are ignored, but INVALID is
5511 message_type = header_data[TYPE_OFFSET];
5512 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
5514 _dbus_verbose ("Message with bad type '%d' received\n",
5516 loader->corrupted = TRUE;
5520 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
5521 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
5523 if (header_len_unsigned < 16)
5525 _dbus_verbose ("Message had broken too-small header length %u\n",
5526 header_len_unsigned);
5527 loader->corrupted = TRUE;
5531 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
5532 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
5534 _dbus_verbose ("Header or body length too large (%u %u)\n",
5535 header_len_unsigned,
5537 loader->corrupted = TRUE;
5541 /* Now that we know the values are in signed range, get
5542 * rid of stupid unsigned, just causes bugs
5544 header_len = header_len_unsigned;
5545 body_len = body_len_unsigned;
5547 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
5550 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
5552 loader->corrupted = TRUE;
5556 if (header_len + body_len > loader->max_message_size)
5558 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %ld\n",
5559 header_len, body_len, loader->max_message_size);
5560 loader->corrupted = TRUE;
5564 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
5566 if (!load_one_message (loader, byte_order, message_type,
5567 header_len, body_len))
5578 * Peeks at first loaded message, returns #NULL if no messages have
5581 * @param loader the loader.
5582 * @returns the next message, or #NULL if none.
5585 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
5587 if (loader->messages)
5588 return loader->messages->data;
5594 * Pops a loaded message (passing ownership of the message
5595 * to the caller). Returns #NULL if no messages have been
5598 * @param loader the loader.
5599 * @returns the next message, or #NULL if none.
5602 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
5604 return _dbus_list_pop_first (&loader->messages);
5608 * Pops a loaded message inside a list link (passing ownership of the
5609 * message and link to the caller). Returns #NULL if no messages have
5612 * @param loader the loader.
5613 * @returns the next message link, or #NULL if none.
5616 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
5618 return _dbus_list_pop_first_link (&loader->messages);
5622 * Returns a popped message link, used to undo a pop.
5624 * @param loader the loader
5625 * @param link the link with a message in it
5628 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
5631 _dbus_list_prepend_link (&loader->messages, link);
5635 * Checks whether the loader is confused due to bad data.
5636 * If messages are received that are invalid, the
5637 * loader gets confused and gives up permanently.
5638 * This state is called "corrupted."
5640 * @param loader the loader
5641 * @returns #TRUE if the loader is hosed.
5644 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
5646 return loader->corrupted;
5650 * Sets the maximum size message we allow.
5652 * @param loader the loader
5653 * @param size the max message size in bytes
5656 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
5659 if (size > MAX_SANE_MESSAGE_SIZE)
5661 _dbus_verbose ("clamping requested max message size %ld to %d\n",
5662 size, MAX_SANE_MESSAGE_SIZE);
5663 size = MAX_SANE_MESSAGE_SIZE;
5665 loader->max_message_size = size;
5669 * Gets the maximum allowed message size in bytes.
5671 * @param loader the loader
5672 * @returns max size in bytes
5675 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
5677 return loader->max_message_size;
5680 static DBusDataSlotAllocator slot_allocator;
5681 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
5684 * Allocates an integer ID to be used for storing application-specific
5685 * data on any DBusMessage. The allocated ID may then be used
5686 * with dbus_message_set_data() and dbus_message_get_data().
5687 * The passed-in slot must be initialized to -1, and is filled in
5688 * with the slot ID. If the passed-in slot is not -1, it's assumed
5689 * to be already allocated, and its refcount is incremented.
5691 * The allocated slot is global, i.e. all DBusMessage objects will
5692 * have a slot with the given integer ID reserved.
5694 * @param slot_p address of a global variable storing the slot
5695 * @returns #FALSE on failure (no memory)
5698 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
5700 return _dbus_data_slot_allocator_alloc (&slot_allocator,
5701 _DBUS_LOCK_NAME (message_slots),
5706 * Deallocates a global ID for message data slots.
5707 * dbus_message_get_data() and dbus_message_set_data() may no
5708 * longer be used with this slot. Existing data stored on existing
5709 * DBusMessage objects will be freed when the message is
5710 * finalized, but may not be retrieved (and may only be replaced if
5711 * someone else reallocates the slot). When the refcount on the
5712 * passed-in slot reaches 0, it is set to -1.
5714 * @param slot_p address storing the slot to deallocate
5717 dbus_message_free_data_slot (dbus_int32_t *slot_p)
5719 _dbus_return_if_fail (*slot_p >= 0);
5721 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
5725 * Stores a pointer on a DBusMessage, along
5726 * with an optional function to be used for freeing
5727 * the data when the data is set again, or when
5728 * the message is finalized. The slot number
5729 * must have been allocated with dbus_message_allocate_data_slot().
5731 * @param message the message
5732 * @param slot the slot number
5733 * @param data the data to store
5734 * @param free_data_func finalizer function for the data
5735 * @returns #TRUE if there was enough memory to store the data
5738 dbus_message_set_data (DBusMessage *message,
5741 DBusFreeFunction free_data_func)
5743 DBusFreeFunction old_free_func;
5747 _dbus_return_val_if_fail (message != NULL, FALSE);
5748 _dbus_return_val_if_fail (slot >= 0, FALSE);
5750 retval = _dbus_data_slot_list_set (&slot_allocator,
5751 &message->slot_list,
5752 slot, data, free_data_func,
5753 &old_free_func, &old_data);
5757 /* Do the actual free outside the message lock */
5759 (* old_free_func) (old_data);
5766 * Retrieves data previously set with dbus_message_set_data().
5767 * The slot must still be allocated (must not have been freed).
5769 * @param message the message
5770 * @param slot the slot to get data from
5771 * @returns the data, or #NULL if not found
5774 dbus_message_get_data (DBusMessage *message,
5779 _dbus_return_val_if_fail (message != NULL, NULL);
5781 res = _dbus_data_slot_list_get (&slot_allocator,
5782 &message->slot_list,
5789 * Utility function to convert a machine-readable (not translated)
5790 * string into a D-BUS message type.
5793 * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL
5794 * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN
5795 * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL
5796 * "error" -> DBUS_MESSAGE_TYPE_ERROR
5797 * anything else -> DBUS_MESSAGE_TYPE_INVALID
5802 dbus_message_type_from_string (const char *type_str)
5804 if (strcmp (type_str, "method_call") == 0)
5805 return DBUS_MESSAGE_TYPE_METHOD_CALL;
5806 if (strcmp (type_str, "method_return") == 0)
5807 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
5808 else if (strcmp (type_str, "signal") == 0)
5809 return DBUS_MESSAGE_TYPE_SIGNAL;
5810 else if (strcmp (type_str, "error") == 0)
5811 return DBUS_MESSAGE_TYPE_ERROR;
5813 return DBUS_MESSAGE_TYPE_INVALID;
5817 #ifdef DBUS_BUILD_TESTS
5818 #include "dbus-test.h"
5823 message_iter_test (DBusMessage *message)
5825 DBusMessageIter iter, dict, array, array2;
5827 unsigned char *data;
5828 dbus_int32_t *our_int_array;
5831 dbus_message_iter_init (message, &iter);
5834 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
5835 _dbus_assert_not_reached ("Argument type isn't string");
5837 str = dbus_message_iter_get_string (&iter);
5838 if (strcmp (str, "Test string") != 0)
5839 _dbus_assert_not_reached ("Strings differ");
5842 if (!dbus_message_iter_next (&iter))
5843 _dbus_assert_not_reached ("Reached end of arguments");
5845 /* Signed integer tests */
5846 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32)
5847 _dbus_assert_not_reached ("Argument type isn't int32");
5849 if (dbus_message_iter_get_int32 (&iter) != -0x12345678)
5850 _dbus_assert_not_reached ("Signed integers differ");
5852 if (!dbus_message_iter_next (&iter))
5853 _dbus_assert_not_reached ("Reached end of fields");
5855 /* Unsigned integer tests */
5856 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
5857 _dbus_assert_not_reached ("Argument type isn't int32");
5859 if (dbus_message_iter_get_uint32 (&iter) != 0xedd1e)
5860 _dbus_assert_not_reached ("Unsigned integers differ");
5862 if (!dbus_message_iter_next (&iter))
5863 _dbus_assert_not_reached ("Reached end of arguments");
5866 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DOUBLE)
5867 _dbus_assert_not_reached ("Argument type isn't double");
5869 if (dbus_message_iter_get_double (&iter) != 3.14159)
5870 _dbus_assert_not_reached ("Doubles differ");
5872 if (!dbus_message_iter_next (&iter))
5873 _dbus_assert_not_reached ("Reached end of arguments");
5875 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
5876 _dbus_assert_not_reached ("Argument type not an array");
5878 if (dbus_message_iter_get_array_type (&iter) != DBUS_TYPE_DOUBLE)
5879 _dbus_assert_not_reached ("Array type not double");
5882 dbus_message_iter_init_array_iterator (&iter, &array, NULL);
5884 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_DOUBLE)
5885 _dbus_assert_not_reached ("Argument type isn't double");
5887 if (dbus_message_iter_get_double (&array) != 1.5)
5888 _dbus_assert_not_reached ("Unsigned integers differ");
5890 if (!dbus_message_iter_next (&array))
5891 _dbus_assert_not_reached ("Reached end of arguments");
5893 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_DOUBLE)
5894 _dbus_assert_not_reached ("Argument type isn't double");
5896 if (dbus_message_iter_get_double (&array) != 2.5)
5897 _dbus_assert_not_reached ("Unsigned integers differ");
5899 if (dbus_message_iter_next (&array))
5900 _dbus_assert_not_reached ("Didn't reach end of arguments");
5902 if (!dbus_message_iter_next (&iter))
5903 _dbus_assert_not_reached ("Reached end of arguments");
5908 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
5909 _dbus_assert_not_reached ("not dict type");
5911 dbus_message_iter_init_dict_iterator (&iter, &dict);
5913 str = dbus_message_iter_get_dict_key (&dict);
5914 if (str == NULL || strcmp (str, "test") != 0)
5915 _dbus_assert_not_reached ("wrong dict key");
5918 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_UINT32)
5919 _dbus_assert_not_reached ("wrong dict entry type");
5921 if (dbus_message_iter_get_uint32 (&dict) != 0xDEADBEEF)
5922 _dbus_assert_not_reached ("wrong dict entry value");
5924 if (!dbus_message_iter_next (&dict))
5925 _dbus_assert_not_reached ("reached end of dict");
5927 /* array of array of int32 (in dict) */
5929 str = dbus_message_iter_get_dict_key (&dict);
5930 if (str == NULL || strcmp (str, "array") != 0)
5931 _dbus_assert_not_reached ("wrong dict key");
5934 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_ARRAY)
5935 _dbus_assert_not_reached ("Argument type not an array");
5937 if (dbus_message_iter_get_array_type (&dict) != DBUS_TYPE_ARRAY)
5938 _dbus_assert_not_reached ("Array type not array");
5940 dbus_message_iter_init_array_iterator (&dict, &array, NULL);
5942 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_ARRAY)
5943 _dbus_assert_not_reached ("Argument type isn't array");
5945 if (dbus_message_iter_get_array_type (&array) != DBUS_TYPE_INT32)
5946 _dbus_assert_not_reached ("Array type not int32");
5948 dbus_message_iter_init_array_iterator (&array, &array2, NULL);
5950 if (dbus_message_iter_get_arg_type (&array2) != DBUS_TYPE_INT32)
5951 _dbus_assert_not_reached ("Argument type isn't int32");
5953 if (dbus_message_iter_get_int32 (&array2) != 0x12345678)
5954 _dbus_assert_not_reached ("Signed integers differ");
5956 if (!dbus_message_iter_next (&array2))
5957 _dbus_assert_not_reached ("Reached end of arguments");
5959 if (dbus_message_iter_get_int32 (&array2) != 0x23456781)
5960 _dbus_assert_not_reached ("Signed integers differ");
5962 if (dbus_message_iter_next (&array2))
5963 _dbus_assert_not_reached ("Didn't reached end of arguments");
5965 if (!dbus_message_iter_next (&array))
5966 _dbus_assert_not_reached ("Reached end of arguments");
5968 if (dbus_message_iter_get_array_type (&array) != DBUS_TYPE_INT32)
5969 _dbus_assert_not_reached ("Array type not int32");
5971 if (!dbus_message_iter_get_int32_array (&array,
5974 _dbus_assert_not_reached ("couldn't get int32 array");
5976 _dbus_assert (len == 3);
5977 _dbus_assert (our_int_array[0] == 0x34567812 &&
5978 our_int_array[1] == 0x45678123 &&
5979 our_int_array[2] == 0x56781234);
5980 dbus_free (our_int_array);
5982 if (dbus_message_iter_next (&array))
5983 _dbus_assert_not_reached ("Didn't reach end of array");
5985 if (dbus_message_iter_next (&dict))
5986 _dbus_assert_not_reached ("Didn't reach end of dict");
5988 if (!dbus_message_iter_next (&iter))
5989 _dbus_assert_not_reached ("Reached end of arguments");
5991 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_BYTE)
5993 _dbus_warn ("type was: %d\n", dbus_message_iter_get_arg_type (&iter));
5994 _dbus_assert_not_reached ("wrong type after dict (should be byte)");
5997 if (dbus_message_iter_get_byte (&iter) != 0xF0)
5998 _dbus_assert_not_reached ("wrong value after dict");
6001 if (!dbus_message_iter_next (&iter))
6002 _dbus_assert_not_reached ("Reached end of arguments");
6004 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_NIL)
6005 _dbus_assert_not_reached ("not a nil type");
6007 if (!dbus_message_iter_next (&iter))
6008 _dbus_assert_not_reached ("Reached end of arguments");
6010 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_CUSTOM)
6011 _dbus_assert_not_reached ("wrong type after dict");
6013 if (!dbus_message_iter_get_custom (&iter, &str, &data, &len))
6014 _dbus_assert_not_reached ("failed to get custom type");
6016 _dbus_assert (strcmp (str, "MyTypeName")==0);
6017 _dbus_assert (len == 5);
6018 _dbus_assert (strcmp (data, "data")==0);
6022 if (dbus_message_iter_next (&iter))
6023 _dbus_assert_not_reached ("Didn't reach end of arguments");
6028 check_message_handling_type (DBusMessageIter *iter,
6031 DBusMessageIter child_iter;
6037 case DBUS_TYPE_BYTE:
6038 dbus_message_iter_get_byte (iter);
6040 case DBUS_TYPE_BOOLEAN:
6041 dbus_message_iter_get_boolean (iter);
6043 case DBUS_TYPE_INT32:
6044 dbus_message_iter_get_int32 (iter);
6046 case DBUS_TYPE_UINT32:
6047 dbus_message_iter_get_uint32 (iter);
6049 case DBUS_TYPE_INT64:
6050 #ifdef DBUS_HAVE_INT64
6051 dbus_message_iter_get_int64 (iter);
6054 case DBUS_TYPE_UINT64:
6055 #ifdef DBUS_HAVE_INT64
6056 dbus_message_iter_get_uint64 (iter);
6059 case DBUS_TYPE_DOUBLE:
6060 dbus_message_iter_get_double (iter);
6062 case DBUS_TYPE_STRING:
6065 str = dbus_message_iter_get_string (iter);
6068 _dbus_warn ("NULL string in message\n");
6074 case DBUS_TYPE_CUSTOM:
6077 unsigned char *data;
6080 if (!dbus_message_iter_get_custom (iter, &name, &data, &len))
6082 _dbus_warn ("error reading name from custom type\n");
6089 case DBUS_TYPE_ARRAY:
6093 dbus_message_iter_init_array_iterator (iter, &child_iter, &array_type);
6095 while (dbus_message_iter_has_next (&child_iter))
6097 if (!check_message_handling_type (&child_iter, array_type))
6099 _dbus_warn ("error in array element\n");
6103 if (!dbus_message_iter_next (&child_iter))
6108 case DBUS_TYPE_DICT:
6113 dbus_message_iter_init_dict_iterator (iter, &child_iter);
6115 while ((entry_type = dbus_message_iter_get_arg_type (&child_iter)) != DBUS_TYPE_INVALID)
6117 key = dbus_message_iter_get_dict_key (&child_iter);
6120 _dbus_warn ("error reading dict key\n");
6125 if (!check_message_handling_type (&child_iter, entry_type))
6127 _dbus_warn ("error in dict value\n");
6131 if (!dbus_message_iter_next (&child_iter))
6138 _dbus_warn ("unknown type %d\n", type);
6147 check_message_handling (DBusMessage *message)
6149 DBusMessageIter iter;
6152 dbus_uint32_t client_serial;
6156 client_serial = dbus_message_get_serial (message);
6158 /* can't use set_serial due to the assertions at the start of it */
6159 _dbus_marshal_set_uint32 (&message->header,
6160 message->byte_order,
6161 CLIENT_SERIAL_OFFSET,
6164 if (client_serial != dbus_message_get_serial (message))
6166 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
6170 /* If we implement message_set_arg (message, n, value)
6171 * then we would want to test it here
6174 dbus_message_iter_init (message, &iter);
6175 while ((type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
6177 if (!check_message_handling_type (&iter, type))
6180 if (!dbus_message_iter_next (&iter))
6191 check_have_valid_message (DBusMessageLoader *loader)
6193 DBusMessage *message;
6199 if (!_dbus_message_loader_queue_messages (loader))
6200 _dbus_assert_not_reached ("no memory to queue messages");
6202 if (_dbus_message_loader_get_is_corrupted (loader))
6204 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
6208 message = _dbus_message_loader_pop_message (loader);
6209 if (message == NULL)
6211 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
6215 if (_dbus_string_get_length (&loader->data) > 0)
6217 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
6221 /* Verify that we're able to properly deal with the message.
6222 * For example, this would detect improper handling of messages
6223 * in nonstandard byte order.
6225 if (!check_message_handling (message))
6232 dbus_message_unref (message);
6238 check_invalid_message (DBusMessageLoader *loader)
6244 if (!_dbus_message_loader_queue_messages (loader))
6245 _dbus_assert_not_reached ("no memory to queue messages");
6247 if (!_dbus_message_loader_get_is_corrupted (loader))
6249 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
6260 check_incomplete_message (DBusMessageLoader *loader)
6262 DBusMessage *message;
6268 if (!_dbus_message_loader_queue_messages (loader))
6269 _dbus_assert_not_reached ("no memory to queue messages");
6271 if (_dbus_message_loader_get_is_corrupted (loader))
6273 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
6277 message = _dbus_message_loader_pop_message (loader);
6278 if (message != NULL)
6280 _dbus_warn ("loaded message that was expected to be incomplete\n");
6288 dbus_message_unref (message);
6293 check_loader_results (DBusMessageLoader *loader,
6294 DBusMessageValidity validity)
6296 if (!_dbus_message_loader_queue_messages (loader))
6297 _dbus_assert_not_reached ("no memory to queue messages");
6301 case _DBUS_MESSAGE_VALID:
6302 return check_have_valid_message (loader);
6303 case _DBUS_MESSAGE_INVALID:
6304 return check_invalid_message (loader);
6305 case _DBUS_MESSAGE_INCOMPLETE:
6306 return check_incomplete_message (loader);
6307 case _DBUS_MESSAGE_UNKNOWN:
6311 _dbus_assert_not_reached ("bad DBusMessageValidity");
6317 * Loads the message in the given message file.
6319 * @param filename filename to load
6320 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
6321 * @param data string to load message into
6322 * @returns #TRUE if the message was loaded
6325 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
6337 _dbus_verbose ("Loading raw %s\n", _dbus_string_get_const_data (filename));
6338 dbus_error_init (&error);
6339 if (!_dbus_file_get_contents (data, filename, &error))
6341 _dbus_warn ("Could not load message file %s: %s\n",
6342 _dbus_string_get_const_data (filename),
6344 dbus_error_free (&error);
6350 if (!_dbus_message_data_load (data, filename))
6352 _dbus_warn ("Could not load message file %s\n",
6353 _dbus_string_get_const_data (filename));
6366 * Tries loading the message in the given message file
6367 * and verifies that DBusMessageLoader can handle it.
6369 * @param filename filename to load
6370 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
6371 * @param expected_validity what the message has to be like to return #TRUE
6372 * @returns #TRUE if the message has the expected validity
6375 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
6377 DBusMessageValidity expected_validity)
6384 if (!_dbus_string_init (&data))
6385 _dbus_assert_not_reached ("could not allocate string\n");
6387 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
6391 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
6397 if (_dbus_string_get_length (&data) > 0)
6398 _dbus_verbose_bytes_of_string (&data, 0,
6399 _dbus_string_get_length (&data));
6401 _dbus_warn ("Failed message loader test on %s\n",
6402 _dbus_string_get_const_data (filename));
6405 _dbus_string_free (&data);
6411 * Tries loading the given message data.
6414 * @param data the message data
6415 * @param expected_validity what the message has to be like to return #TRUE
6416 * @returns #TRUE if the message has the expected validity
6419 dbus_internal_do_not_use_try_message_data (const DBusString *data,
6420 DBusMessageValidity expected_validity)
6422 DBusMessageLoader *loader;
6430 /* Write the data one byte at a time */
6432 loader = _dbus_message_loader_new ();
6434 /* check some trivial loader functions */
6435 _dbus_message_loader_ref (loader);
6436 _dbus_message_loader_unref (loader);
6437 _dbus_message_loader_get_max_message_size (loader);
6439 len = _dbus_string_get_length (data);
6440 for (i = 0; i < len; i++)
6444 _dbus_message_loader_get_buffer (loader, &buffer);
6445 _dbus_string_append_byte (buffer,
6446 _dbus_string_get_byte (data, i));
6447 _dbus_message_loader_return_buffer (loader, buffer, 1);
6450 if (!check_loader_results (loader, expected_validity))
6453 _dbus_message_loader_unref (loader);
6456 /* Write the data all at once */
6458 loader = _dbus_message_loader_new ();
6463 _dbus_message_loader_get_buffer (loader, &buffer);
6464 _dbus_string_copy (data, 0, buffer,
6465 _dbus_string_get_length (buffer));
6466 _dbus_message_loader_return_buffer (loader, buffer, 1);
6469 if (!check_loader_results (loader, expected_validity))
6472 _dbus_message_loader_unref (loader);
6475 /* Write the data 2 bytes at a time */
6477 loader = _dbus_message_loader_new ();
6479 len = _dbus_string_get_length (data);
6480 for (i = 0; i < len; i += 2)
6484 _dbus_message_loader_get_buffer (loader, &buffer);
6485 _dbus_string_append_byte (buffer,
6486 _dbus_string_get_byte (data, i));
6488 _dbus_string_append_byte (buffer,
6489 _dbus_string_get_byte (data, i+1));
6490 _dbus_message_loader_return_buffer (loader, buffer, 1);
6493 if (!check_loader_results (loader, expected_validity))
6496 _dbus_message_loader_unref (loader);
6504 _dbus_message_loader_unref (loader);
6510 process_test_subdir (const DBusString *test_base_dir,
6512 DBusMessageValidity validity,
6513 DBusForeachMessageFileFunc function,
6516 DBusString test_directory;
6517 DBusString filename;
6525 if (!_dbus_string_init (&test_directory))
6526 _dbus_assert_not_reached ("didn't allocate test_directory\n");
6528 _dbus_string_init_const (&filename, subdir);
6530 if (!_dbus_string_copy (test_base_dir, 0,
6531 &test_directory, 0))
6532 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
6534 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
6535 _dbus_assert_not_reached ("couldn't allocate full path");
6537 _dbus_string_free (&filename);
6538 if (!_dbus_string_init (&filename))
6539 _dbus_assert_not_reached ("didn't allocate filename string\n");
6541 dbus_error_init (&error);
6542 dir = _dbus_directory_open (&test_directory, &error);
6545 _dbus_warn ("Could not open %s: %s\n",
6546 _dbus_string_get_const_data (&test_directory),
6548 dbus_error_free (&error);
6552 printf ("Testing %s:\n", subdir);
6555 while (_dbus_directory_get_next_file (dir, &filename, &error))
6557 DBusString full_path;
6560 if (!_dbus_string_init (&full_path))
6561 _dbus_assert_not_reached ("couldn't init string");
6563 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
6564 _dbus_assert_not_reached ("couldn't copy dir to full_path");
6566 if (!_dbus_concat_dir_and_file (&full_path, &filename))
6567 _dbus_assert_not_reached ("couldn't concat file to dir");
6569 if (_dbus_string_ends_with_c_str (&filename, ".message"))
6571 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
6575 _dbus_verbose ("Skipping non-.message file %s\n",
6576 _dbus_string_get_const_data (&filename));
6577 _dbus_string_free (&full_path);
6582 _dbus_string_get_const_data (&filename));
6584 _dbus_verbose (" expecting %s for %s\n",
6585 validity == _DBUS_MESSAGE_VALID ? "valid" :
6586 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
6587 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")),
6588 _dbus_string_get_const_data (&filename));
6590 if (! (*function) (&full_path, is_raw, validity, user_data))
6592 _dbus_string_free (&full_path);
6596 _dbus_string_free (&full_path);
6599 if (dbus_error_is_set (&error))
6601 _dbus_warn ("Could not get next file in %s: %s\n",
6602 _dbus_string_get_const_data (&test_directory),
6604 dbus_error_free (&error);
6613 _dbus_directory_close (dir);
6614 _dbus_string_free (&test_directory);
6615 _dbus_string_free (&filename);
6621 * Runs the given function on every message file in the test suite.
6622 * The function should return #FALSE on test failure or fatal error.
6624 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
6625 * @param func the function to run
6626 * @param user_data data for function
6627 * @returns #FALSE if there's a failure
6630 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
6631 DBusForeachMessageFileFunc func,
6634 DBusString test_directory;
6639 _dbus_string_init_const (&test_directory, test_data_dir);
6641 if (!process_test_subdir (&test_directory, "valid-messages",
6642 _DBUS_MESSAGE_VALID, func, user_data))
6645 if (!process_test_subdir (&test_directory, "invalid-messages",
6646 _DBUS_MESSAGE_INVALID, func, user_data))
6649 if (!process_test_subdir (&test_directory, "incomplete-messages",
6650 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
6657 _dbus_string_free (&test_directory);
6663 verify_test_message (DBusMessage *message)
6665 DBusMessageIter iter, dict;
6667 dbus_int32_t our_int;
6670 dbus_bool_t our_bool;
6671 dbus_uint32_t our_uint32;
6672 dbus_int32_t *our_uint32_array;
6673 int our_uint32_array_len;
6674 dbus_int32_t *our_int32_array;
6675 int our_int32_array_len;
6676 char **our_string_array;
6677 int our_string_array_len;
6678 #ifdef DBUS_HAVE_INT64
6679 dbus_int64_t our_int64;
6680 dbus_uint64_t our_uint64;
6681 dbus_int64_t *our_uint64_array;
6682 int our_uint64_array_len;
6683 dbus_int64_t *our_int64_array;
6684 int our_int64_array_len;
6686 double *our_double_array;
6687 int our_double_array_len;
6688 unsigned char *our_byte_array;
6689 int our_byte_array_len;
6690 unsigned char *our_boolean_array;
6691 int our_boolean_array_len;
6693 dbus_message_iter_init (message, &iter);
6695 dbus_error_init (&error);
6696 if (!dbus_message_iter_get_args (&iter, &error,
6697 DBUS_TYPE_INT32, &our_int,
6698 #ifdef DBUS_HAVE_INT64
6699 DBUS_TYPE_INT64, &our_int64,
6700 DBUS_TYPE_UINT64, &our_uint64,
6702 DBUS_TYPE_STRING, &our_str,
6703 DBUS_TYPE_DOUBLE, &our_double,
6704 DBUS_TYPE_BOOLEAN, &our_bool,
6705 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32,
6706 &our_uint32_array, &our_uint32_array_len,
6707 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32,
6708 &our_int32_array, &our_int32_array_len,
6709 #ifdef DBUS_HAVE_INT64
6710 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64,
6711 &our_uint64_array, &our_uint64_array_len,
6712 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64,
6713 &our_int64_array, &our_int64_array_len,
6715 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
6716 &our_string_array, &our_string_array_len,
6717 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE,
6718 &our_double_array, &our_double_array_len,
6719 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
6720 &our_byte_array, &our_byte_array_len,
6721 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN,
6722 &our_boolean_array, &our_boolean_array_len,
6725 _dbus_warn ("error: %s - %s\n", error.name,
6726 (error.message != NULL) ? error.message : "no message");
6727 _dbus_assert_not_reached ("Could not get arguments");
6730 if (our_int != -0x12345678)
6731 _dbus_assert_not_reached ("integers differ!");
6733 #ifdef DBUS_HAVE_INT64
6734 if (our_int64 != DBUS_INT64_CONSTANT (-0x123456789abcd))
6735 _dbus_assert_not_reached ("64-bit integers differ!");
6736 if (our_uint64 != DBUS_UINT64_CONSTANT (0x123456789abcd))
6737 _dbus_assert_not_reached ("64-bit unsigned integers differ!");
6740 if (our_double != 3.14159)
6741 _dbus_assert_not_reached ("doubles differ!");
6743 if (strcmp (our_str, "Test string") != 0)
6744 _dbus_assert_not_reached ("strings differ!");
6745 dbus_free (our_str);
6748 _dbus_assert_not_reached ("booleans differ");
6750 if (our_uint32_array_len != 4 ||
6751 our_uint32_array[0] != 0x12345678 ||
6752 our_uint32_array[1] != 0x23456781 ||
6753 our_uint32_array[2] != 0x34567812 ||
6754 our_uint32_array[3] != 0x45678123)
6755 _dbus_assert_not_reached ("uint array differs");
6756 dbus_free (our_uint32_array);
6758 if (our_int32_array_len != 4 ||
6759 our_int32_array[0] != 0x12345678 ||
6760 our_int32_array[1] != -0x23456781 ||
6761 our_int32_array[2] != 0x34567812 ||
6762 our_int32_array[3] != -0x45678123)
6763 _dbus_assert_not_reached ("int array differs");
6764 dbus_free (our_int32_array);
6766 #ifdef DBUS_HAVE_INT64
6767 if (our_uint64_array_len != 4 ||
6768 our_uint64_array[0] != 0x12345678 ||
6769 our_uint64_array[1] != 0x23456781 ||
6770 our_uint64_array[2] != 0x34567812 ||
6771 our_uint64_array[3] != 0x45678123)
6772 _dbus_assert_not_reached ("uint64 array differs");
6773 dbus_free (our_uint64_array);
6775 if (our_int64_array_len != 4 ||
6776 our_int64_array[0] != 0x12345678 ||
6777 our_int64_array[1] != -0x23456781 ||
6778 our_int64_array[2] != 0x34567812 ||
6779 our_int64_array[3] != -0x45678123)
6780 _dbus_assert_not_reached ("int64 array differs");
6781 dbus_free (our_int64_array);
6782 #endif /* DBUS_HAVE_INT64 */
6784 if (our_string_array_len != 4)
6785 _dbus_assert_not_reached ("string array has wrong length");
6787 if (strcmp (our_string_array[0], "Foo") != 0 ||
6788 strcmp (our_string_array[1], "bar") != 0 ||
6789 strcmp (our_string_array[2], "") != 0 ||
6790 strcmp (our_string_array[3], "woo woo woo woo") != 0)
6791 _dbus_assert_not_reached ("string array differs");
6793 dbus_free_string_array (our_string_array);
6795 if (our_double_array_len != 3)
6796 _dbus_assert_not_reached ("double array had wrong length");
6798 /* On all IEEE machines (i.e. everything sane) exact equality
6799 * should be preserved over the wire
6801 if (our_double_array[0] != 0.1234 ||
6802 our_double_array[1] != 9876.54321 ||
6803 our_double_array[2] != -300.0)
6804 _dbus_assert_not_reached ("double array had wrong values");
6806 dbus_free (our_double_array);
6808 if (our_byte_array_len != 4)
6809 _dbus_assert_not_reached ("byte array had wrong length");
6811 if (our_byte_array[0] != 'a' ||
6812 our_byte_array[1] != 'b' ||
6813 our_byte_array[2] != 'c' ||
6814 our_byte_array[3] != 234)
6815 _dbus_assert_not_reached ("byte array had wrong values");
6817 dbus_free (our_byte_array);
6819 if (our_boolean_array_len != 5)
6820 _dbus_assert_not_reached ("bool array had wrong length");
6822 if (our_boolean_array[0] != TRUE ||
6823 our_boolean_array[1] != FALSE ||
6824 our_boolean_array[2] != TRUE ||
6825 our_boolean_array[3] != TRUE ||
6826 our_boolean_array[4] != FALSE)
6827 _dbus_assert_not_reached ("bool array had wrong values");
6829 dbus_free (our_boolean_array);
6831 if (!dbus_message_iter_next (&iter))
6832 _dbus_assert_not_reached ("Reached end of arguments");
6834 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
6835 _dbus_assert_not_reached ("not dict type");
6837 dbus_message_iter_init_dict_iterator (&iter, &dict);
6839 our_str = dbus_message_iter_get_dict_key (&dict);
6840 if (our_str == NULL || strcmp (our_str, "test") != 0)
6841 _dbus_assert_not_reached ("wrong dict key");
6842 dbus_free (our_str);
6844 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_UINT32)
6846 _dbus_verbose ("dict entry type: %d\n", dbus_message_iter_get_arg_type (&dict));
6847 _dbus_assert_not_reached ("wrong dict entry type");
6850 if ((our_uint32 = dbus_message_iter_get_uint32 (&dict)) != 0xDEADBEEF)
6852 _dbus_verbose ("dict entry val: %x\n", our_uint32);
6853 _dbus_assert_not_reached ("wrong dict entry value");
6856 if (dbus_message_iter_next (&dict))
6857 _dbus_assert_not_reached ("Didn't reach end of dict");
6859 if (!dbus_message_iter_next (&iter))
6860 _dbus_assert_not_reached ("Reached end of arguments");
6862 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
6863 _dbus_assert_not_reached ("wrong type after dict");
6865 if (dbus_message_iter_get_uint32 (&iter) != 0xCAFEBABE)
6866 _dbus_assert_not_reached ("wrong value after dict");
6868 if (dbus_message_iter_next (&iter))
6869 _dbus_assert_not_reached ("Didn't reach end of arguments");
6873 * @ingroup DBusMessageInternals
6874 * Unit test for DBusMessage.
6876 * @returns #TRUE on success.
6879 _dbus_message_test (const char *test_data_dir)
6881 DBusMessage *message;
6882 DBusMessageLoader *loader;
6883 DBusMessageIter iter, child_iter, child_iter2, child_iter3;
6889 const dbus_uint32_t our_uint32_array[] =
6890 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
6891 const dbus_uint32_t our_int32_array[] =
6892 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
6893 #ifdef DBUS_HAVE_INT64
6894 const dbus_uint64_t our_uint64_array[] =
6895 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
6896 const dbus_uint64_t our_int64_array[] =
6897 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
6899 const char *our_string_array[] = { "Foo", "bar", "", "woo woo woo woo" };
6900 const double our_double_array[] = { 0.1234, 9876.54321, -300.0 };
6901 const unsigned char our_byte_array[] = { 'a', 'b', 'c', 234 };
6902 const unsigned char our_boolean_array[] = { TRUE, FALSE, TRUE, TRUE, FALSE };
6908 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
6910 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
6911 "/org/freedesktop/TestPath",
6912 "Foo.TestInterface",
6914 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
6915 _dbus_assert (dbus_message_is_method_call (message, "Foo.TestInterface",
6917 _dbus_assert (strcmp (dbus_message_get_path (message),
6918 "/org/freedesktop/TestPath") == 0);
6919 _dbus_message_set_serial (message, 1234);
6920 /* string length including nul byte not a multiple of 4 */
6921 if (!dbus_message_set_sender (message, "org.foo.bar1"))
6922 _dbus_assert_not_reached ("out of memory");
6923 _dbus_assert (dbus_message_has_sender (message, "org.foo.bar1"));
6924 dbus_message_set_reply_serial (message, 5678);
6925 if (!dbus_message_set_sender (message, NULL))
6926 _dbus_assert_not_reached ("out of memory");
6927 _dbus_assert (!dbus_message_has_sender (message, "org.foo.bar1"));
6928 _dbus_assert (dbus_message_get_serial (message) == 1234);
6929 _dbus_assert (dbus_message_get_reply_serial (message) == 5678);
6930 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
6932 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
6933 dbus_message_set_no_reply (message, TRUE);
6934 _dbus_assert (dbus_message_get_no_reply (message) == TRUE);
6935 dbus_message_set_no_reply (message, FALSE);
6936 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
6938 /* Set/get some header fields */
6940 if (!dbus_message_set_path (message, "/foo"))
6941 _dbus_assert_not_reached ("out of memory");
6942 _dbus_assert (strcmp (dbus_message_get_path (message),
6945 if (!dbus_message_set_interface (message, "org.Foo"))
6946 _dbus_assert_not_reached ("out of memory");
6947 _dbus_assert (strcmp (dbus_message_get_interface (message),
6950 if (!dbus_message_set_member (message, "Bar"))
6951 _dbus_assert_not_reached ("out of memory");
6952 _dbus_assert (strcmp (dbus_message_get_member (message),
6955 /* Set/get them with longer values */
6956 if (!dbus_message_set_path (message, "/foo/bar"))
6957 _dbus_assert_not_reached ("out of memory");
6958 _dbus_assert (strcmp (dbus_message_get_path (message),
6961 if (!dbus_message_set_interface (message, "org.Foo.Bar"))
6962 _dbus_assert_not_reached ("out of memory");
6963 _dbus_assert (strcmp (dbus_message_get_interface (message),
6964 "org.Foo.Bar") == 0);
6966 if (!dbus_message_set_member (message, "BarFoo"))
6967 _dbus_assert_not_reached ("out of memory");
6968 _dbus_assert (strcmp (dbus_message_get_member (message),
6971 /* Realloc shorter again */
6973 if (!dbus_message_set_path (message, "/foo"))
6974 _dbus_assert_not_reached ("out of memory");
6975 _dbus_assert (strcmp (dbus_message_get_path (message),
6978 if (!dbus_message_set_interface (message, "org.Foo"))
6979 _dbus_assert_not_reached ("out of memory");
6980 _dbus_assert (strcmp (dbus_message_get_interface (message),
6983 if (!dbus_message_set_member (message, "Bar"))
6984 _dbus_assert_not_reached ("out of memory");
6985 _dbus_assert (strcmp (dbus_message_get_member (message),
6988 dbus_message_unref (message);
6990 /* Test the vararg functions */
6991 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
6992 "/org/freedesktop/TestPath",
6993 "Foo.TestInterface",
6995 _dbus_message_set_serial (message, 1);
6996 dbus_message_append_args (message,
6997 DBUS_TYPE_INT32, -0x12345678,
6998 #ifdef DBUS_HAVE_INT64
6999 DBUS_TYPE_INT64, DBUS_INT64_CONSTANT (-0x123456789abcd),
7000 DBUS_TYPE_UINT64, DBUS_UINT64_CONSTANT (0x123456789abcd),
7002 DBUS_TYPE_STRING, "Test string",
7003 DBUS_TYPE_DOUBLE, 3.14159,
7004 DBUS_TYPE_BOOLEAN, TRUE,
7005 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, our_uint32_array,
7006 _DBUS_N_ELEMENTS (our_uint32_array),
7007 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, our_int32_array,
7008 _DBUS_N_ELEMENTS (our_int32_array),
7009 #ifdef DBUS_HAVE_INT64
7010 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64, our_uint64_array,
7011 _DBUS_N_ELEMENTS (our_uint64_array),
7012 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64, our_int64_array,
7013 _DBUS_N_ELEMENTS (our_int64_array),
7015 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, our_string_array,
7016 _DBUS_N_ELEMENTS (our_string_array),
7017 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, our_double_array,
7018 _DBUS_N_ELEMENTS (our_double_array),
7019 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, our_byte_array,
7020 _DBUS_N_ELEMENTS (our_byte_array),
7021 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN, our_boolean_array,
7022 _DBUS_N_ELEMENTS (our_boolean_array),
7025 dbus_message_append_iter_init (message, &iter);
7026 dbus_message_iter_append_dict (&iter, &child_iter);
7027 dbus_message_iter_append_dict_key (&child_iter, "test");
7028 dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
7029 dbus_message_iter_append_uint32 (&iter, 0xCAFEBABE);
7032 sig[i++] = DBUS_TYPE_INT32;
7033 #ifdef DBUS_HAVE_INT64
7034 sig[i++] = DBUS_TYPE_INT64;
7035 sig[i++] = DBUS_TYPE_UINT64;
7037 sig[i++] = DBUS_TYPE_STRING;
7038 sig[i++] = DBUS_TYPE_DOUBLE;
7039 sig[i++] = DBUS_TYPE_BOOLEAN;
7040 sig[i++] = DBUS_TYPE_ARRAY;
7041 sig[i++] = DBUS_TYPE_UINT32;
7042 sig[i++] = DBUS_TYPE_ARRAY;
7043 sig[i++] = DBUS_TYPE_INT32;
7044 #ifdef DBUS_HAVE_INT64
7045 sig[i++] = DBUS_TYPE_ARRAY;
7046 sig[i++] = DBUS_TYPE_UINT64;
7047 sig[i++] = DBUS_TYPE_ARRAY;
7048 sig[i++] = DBUS_TYPE_INT64;
7050 sig[i++] = DBUS_TYPE_ARRAY;
7051 sig[i++] = DBUS_TYPE_STRING;
7052 sig[i++] = DBUS_TYPE_ARRAY;
7053 sig[i++] = DBUS_TYPE_DOUBLE;
7054 sig[i++] = DBUS_TYPE_ARRAY;
7055 sig[i++] = DBUS_TYPE_BYTE;
7056 sig[i++] = DBUS_TYPE_ARRAY;
7057 sig[i++] = DBUS_TYPE_BOOLEAN;
7058 sig[i++] = DBUS_TYPE_DICT;
7059 sig[i++] = DBUS_TYPE_UINT32;
7060 sig[i++] = DBUS_TYPE_INVALID;
7062 _dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
7064 _dbus_verbose_bytes_of_string (&message->header, 0,
7065 _dbus_string_get_length (&message->header));
7066 _dbus_verbose_bytes_of_string (&message->body, 0,
7067 _dbus_string_get_length (&message->body));
7069 _dbus_verbose ("Signature expected \"%s\" actual \"%s\"\n",
7070 sig, dbus_message_get_signature (message));
7072 s = dbus_message_get_signature (message);
7074 _dbus_assert (dbus_message_has_signature (message, sig));
7075 _dbus_assert (strcmp (s, sig) == 0);
7077 verify_test_message (message);
7079 copy = dbus_message_copy (message);
7081 _dbus_assert (message->client_serial == copy->client_serial);
7082 _dbus_assert (message->reply_serial == copy->reply_serial);
7083 _dbus_assert (message->header_padding == copy->header_padding);
7085 _dbus_assert (_dbus_string_get_length (&message->header) ==
7086 _dbus_string_get_length (©->header));
7088 _dbus_assert (_dbus_string_get_length (&message->body) ==
7089 _dbus_string_get_length (©->body));
7091 _dbus_assert (_dbus_string_get_length (&message->signature) ==
7092 _dbus_string_get_length (©->signature));
7094 verify_test_message (copy);
7096 name1 = dbus_message_get_interface (message);
7097 name2 = dbus_message_get_interface (copy);
7099 _dbus_assert (strcmp (name1, name2) == 0);
7101 name1 = dbus_message_get_member (message);
7102 name2 = dbus_message_get_member (copy);
7104 _dbus_assert (strcmp (name1, name2) == 0);
7106 dbus_message_unref (message);
7107 dbus_message_unref (copy);
7109 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
7110 "/org/freedesktop/TestPath",
7111 "Foo.TestInterface",
7114 _dbus_message_set_serial (message, 1);
7115 dbus_message_set_reply_serial (message, 0x12345678);
7117 dbus_message_append_iter_init (message, &iter);
7118 dbus_message_iter_append_string (&iter, "Test string");
7119 dbus_message_iter_append_int32 (&iter, -0x12345678);
7120 dbus_message_iter_append_uint32 (&iter, 0xedd1e);
7121 dbus_message_iter_append_double (&iter, 3.14159);
7123 dbus_message_iter_append_array (&iter, &child_iter, DBUS_TYPE_DOUBLE);
7124 dbus_message_iter_append_double (&child_iter, 1.5);
7125 dbus_message_iter_append_double (&child_iter, 2.5);
7128 dbus_message_iter_append_dict (&iter, &child_iter);
7129 dbus_message_iter_append_dict_key (&child_iter, "test");
7130 dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
7132 /* array of array of int32 (in dict) */
7133 dbus_message_iter_append_dict_key (&child_iter, "array");
7134 dbus_message_iter_append_array (&child_iter, &child_iter2, DBUS_TYPE_ARRAY);
7135 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
7136 dbus_message_iter_append_int32 (&child_iter3, 0x12345678);
7137 dbus_message_iter_append_int32 (&child_iter3, 0x23456781);
7138 _dbus_warn ("next call expected to fail with wrong array type\n");
7139 _dbus_assert (!dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_UINT32));
7140 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
7141 dbus_message_iter_append_int32 (&child_iter3, 0x34567812);
7142 dbus_message_iter_append_int32 (&child_iter3, 0x45678123);
7143 dbus_message_iter_append_int32 (&child_iter3, 0x56781234);
7145 dbus_message_iter_append_byte (&iter, 0xF0);
7147 dbus_message_iter_append_nil (&iter);
7149 dbus_message_iter_append_custom (&iter, "MyTypeName",
7152 message_iter_test (message);
7154 /* Message loader test */
7155 _dbus_message_lock (message);
7156 loader = _dbus_message_loader_new ();
7158 /* check ref/unref */
7159 _dbus_message_loader_ref (loader);
7160 _dbus_message_loader_unref (loader);
7162 /* Write the header data one byte at a time */
7163 data = _dbus_string_get_const_data (&message->header);
7164 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
7168 _dbus_message_loader_get_buffer (loader, &buffer);
7169 _dbus_string_append_byte (buffer, data[i]);
7170 _dbus_message_loader_return_buffer (loader, buffer, 1);
7173 /* Write the body data one byte at a time */
7174 data = _dbus_string_get_const_data (&message->body);
7175 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
7179 _dbus_message_loader_get_buffer (loader, &buffer);
7180 _dbus_string_append_byte (buffer, data[i]);
7181 _dbus_message_loader_return_buffer (loader, buffer, 1);
7184 copy = dbus_message_copy (message); /* save for tests below */
7185 dbus_message_unref (message);
7187 /* Now pop back the message */
7188 if (!_dbus_message_loader_queue_messages (loader))
7189 _dbus_assert_not_reached ("no memory to queue messages");
7191 if (_dbus_message_loader_get_is_corrupted (loader))
7192 _dbus_assert_not_reached ("message loader corrupted");
7194 message = _dbus_message_loader_pop_message (loader);
7196 _dbus_assert_not_reached ("received a NULL message");
7198 if (dbus_message_get_reply_serial (message) != 0x12345678)
7199 _dbus_assert_not_reached ("reply serial fields differ");
7201 message_iter_test (message);
7203 dbus_message_unref (message);
7204 _dbus_message_loader_unref (loader);
7206 message = dbus_message_new_method_return (copy);
7207 if (message == NULL)
7208 _dbus_assert_not_reached ("out of memory\n");
7209 dbus_message_unref (copy);
7211 if (!dbus_message_append_args (message,
7212 DBUS_TYPE_STRING, "hello",
7214 _dbus_assert_not_reached ("no memory");
7216 if (!dbus_message_has_signature (message, "s"))
7217 _dbus_assert_not_reached ("method return has wrong signature");
7219 dbus_error_init (&error);
7220 if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING,
7221 &t, DBUS_TYPE_INVALID))
7224 _dbus_warn ("Failed to get expected string arg: %s\n", error.message);
7229 dbus_message_unref (message);
7231 /* Now load every message in test_data_dir if we have one */
7232 if (test_data_dir == NULL)
7235 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
7236 (DBusForeachMessageFileFunc)
7237 dbus_internal_do_not_use_try_message_file,
7241 #endif /* DBUS_BUILD_TESTS */