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_type_alignment (int type)
439 case DBUS_TYPE_BOOLEAN:
443 case DBUS_TYPE_INT32:
444 case DBUS_TYPE_UINT32:
445 case DBUS_TYPE_STRING:
446 case DBUS_TYPE_OBJECT_PATH:
447 /* These are aligned 4 because they have a length as the
450 case DBUS_TYPE_CUSTOM:
455 case DBUS_TYPE_INT64:
456 case DBUS_TYPE_UINT64:
457 case DBUS_TYPE_DOUBLE:
461 case DBUS_TYPE_ARRAY:
462 _dbus_assert_not_reached ("passed an ARRAY type to get_type_alignment()");
465 case DBUS_TYPE_INVALID:
467 _dbus_assert_not_reached ("passed an invalid or unknown type to get_type_alignment()");
475 iterate_one_field (const DBusString *str,
480 DBusString *append_copy_to,
481 int *copy_name_offset_p,
482 int *copy_value_offset_p)
484 int name, type, array_type;
493 _dbus_verbose ("%s: name_offset=%d, append to %p\n",
494 _DBUS_FUNCTION_NAME, name_offset, append_copy_to);
498 name = _dbus_string_get_byte (str, name_offset);
502 type = _dbus_string_get_byte (str, type_pos);
507 /* find out the type of our array */
508 while (array_type == DBUS_TYPE_ARRAY)
512 array_type = _dbus_string_get_byte (str, pos);
515 _dbus_verbose ("%s: name %d, type '%c' %d at %d len %d, array type '%c' %d\n",
517 name, type, type, type_pos, type_len, array_type, array_type);
519 #ifndef DBUS_DISABLE_ASSERT
520 if (!_dbus_type_is_valid (array_type))
522 _dbus_warn ("type '%c' %d is not valid in %s\n",
523 array_type, array_type, _DBUS_FUNCTION_NAME);
524 _dbus_assert_not_reached ("invalid type");
528 alignment = get_type_alignment (array_type);
531 pos = _DBUS_ALIGN_VALUE (pos, alignment);
533 _dbus_verbose ("%s: alignment %d value at pos %d\n",
534 _DBUS_FUNCTION_NAME, alignment, pos);
536 /* pos now points to our value */
537 if (!_dbus_marshal_get_arg_end_pos (str, byte_order,
538 type, pos, &value_end))
539 _dbus_assert_not_reached ("failed to get the byte after this header");
542 value_len = value_end - value_pos;
544 _dbus_verbose ("%s: value_pos %d value_len %d value_end %d\n",
545 _DBUS_FUNCTION_NAME, value_pos, value_len, value_end);
548 *next_offset_p = pos + value_len;
551 *field_name_p = name;
557 orig_len = _dbus_string_get_length (append_copy_to);
559 if (copy_name_offset_p)
560 *copy_name_offset_p = orig_len;
562 if (!_dbus_string_append_byte (append_copy_to, name))
565 if (!_dbus_string_copy_len (str, type_pos, type_len,
567 _dbus_string_get_length (append_copy_to)))
570 if (!_dbus_string_align_length (append_copy_to, alignment))
573 if (copy_value_offset_p)
574 *copy_value_offset_p = _dbus_string_get_length (append_copy_to);
576 if (!_dbus_string_copy_len (str, value_pos, value_len,
578 _dbus_string_get_length (append_copy_to)))
584 _dbus_verbose ("%s: Failed copying old fields to new string\n",
585 _DBUS_FUNCTION_NAME);
586 _dbus_string_set_length (append_copy_to, orig_len);
593 #ifndef DBUS_DISABLE_ASSERT
595 verify_header_fields (DBusMessage *message)
599 while (i < DBUS_HEADER_FIELD_LAST)
601 if (message->header_fields[i].name_offset >= 0)
602 _dbus_assert (_dbus_string_get_byte (&message->header,
603 message->header_fields[i].name_offset) ==
608 #else /* DBUS_DISABLE_ASSERT */
609 #define verify_header_fields(x)
610 #endif /* DBUS_DISABLE_ASSERT */
612 /* In this function we delete one field and re-align all the fields
616 delete_one_and_re_align (DBusMessage *message,
617 int name_offset_to_delete)
620 int new_fields_front_padding;
624 HeaderField new_header_fields[DBUS_HEADER_FIELD_LAST];
626 _dbus_assert (name_offset_to_delete < _dbus_string_get_length (&message->header));
627 verify_header_fields (message);
629 _dbus_verbose ("%s: Deleting one field at offset %d\n",
631 name_offset_to_delete);
635 clear_header_padding (message);
637 if (!_dbus_string_init_preallocated (©,
638 _dbus_string_get_length (&message->header) -
639 name_offset_to_delete + 8))
641 _dbus_verbose ("%s: Failed to init string to hold copy of fields\n",
642 _DBUS_FUNCTION_NAME);
646 /* Align the name offset of the first field in the same way it's
647 * aligned in the real header
649 new_fields_front_padding = name_offset_to_delete % 8;
651 if (!_dbus_string_insert_bytes (©, 0, new_fields_front_padding,
653 _dbus_assert_not_reached ("Should not have failed to insert bytes into preallocated string\n");
655 memcpy (new_header_fields, message->header_fields,
656 sizeof (new_header_fields));
658 /* Now just re-marshal each field in the header to our temporary
659 * buffer, skipping the first one. The tricky part is that the
660 * fields are padded as if for previous_name_offset, but are in fact
661 * at unaligned_name_offset
664 if (!iterate_one_field (&message->header,
666 name_offset_to_delete,
668 &field_name, NULL, NULL, NULL))
669 _dbus_assert_not_reached ("shouldn't have failed to alloc memory to skip the deleted field");
671 if (field_name < DBUS_HEADER_FIELD_LAST)
673 new_header_fields[field_name].name_offset = -1;
674 new_header_fields[field_name].value_offset = -1;
677 while (next_offset < _dbus_string_get_length (&message->header))
679 int copy_name_offset;
680 int copy_value_offset;
682 if (!iterate_one_field (&message->header,
691 _dbus_verbose ("%s: OOM iterating one field\n",
692 _DBUS_FUNCTION_NAME);
696 if (field_name < DBUS_HEADER_FIELD_LAST)
698 new_header_fields[field_name].name_offset = copy_name_offset - new_fields_front_padding + name_offset_to_delete;
699 new_header_fields[field_name].value_offset = copy_value_offset - new_fields_front_padding + name_offset_to_delete;
703 if (!_dbus_string_replace_len (©,
704 new_fields_front_padding,
705 _dbus_string_get_length (©) - new_fields_front_padding,
707 name_offset_to_delete,
708 _dbus_string_get_length (&message->header) - name_offset_to_delete))
710 _dbus_verbose ("%s: OOM moving copy back into header\n",
711 _DBUS_FUNCTION_NAME);
715 memcpy (message->header_fields, new_header_fields,
716 sizeof (new_header_fields));
717 verify_header_fields (message);
722 _dbus_string_free (©);
725 if (!append_header_padding (message))
726 _dbus_assert_not_reached ("Failed to re-append header padding in re_align_field_recurse()");
732 delete_field (DBusMessage *message,
734 int prealloc_header_space)
738 _dbus_assert (!message->locked);
741 if (!_dbus_string_lengthen (&message->header, prealloc_header_space))
743 _dbus_verbose ("failed to prealloc %d bytes header space\n",
744 prealloc_header_space);
747 _dbus_string_shorten (&message->header, prealloc_header_space);
750 offset = message->header_fields[field].name_offset;
753 _dbus_verbose ("header field didn't exist, no need to delete\n");
754 return TRUE; /* field didn't exist */
757 return delete_one_and_re_align (message, offset);
760 #ifdef DBUS_BUILD_TESTS
762 set_int_field (DBusMessage *message,
766 int offset = message->header_fields[field].value_offset;
768 _dbus_assert (!message->locked);
772 /* need to append the field */
773 return append_int_field (message, field, value);
777 _dbus_marshal_set_int32 (&message->header,
787 set_uint_field (DBusMessage *message,
791 int offset = message->header_fields[field].value_offset;
793 _dbus_assert (!message->locked);
797 /* need to append the field */
798 return append_uint_field (message, field, value);
802 _dbus_marshal_set_uint32 (&message->header,
811 set_string_field (DBusMessage *message,
818 _dbus_assert (!message->locked);
820 /* the prealloc is so the append_string_field()
821 * below won't fail, leaving us in inconsistent state
823 prealloc = (value ? strlen (value) : 0) + MAX_BYTES_OVERHEAD_TO_APPEND_A_STRING;
825 _dbus_verbose ("set_string_field() field %d prealloc %d\n",
828 if (!delete_field (message, field, prealloc))
833 /* need to append the field */
834 if (!append_string_field (message, field, type, value))
835 _dbus_assert_not_reached ("Appending string field shouldn't have failed, due to preallocation");
842 * Sets the serial number of a message.
843 * This can only be done once on a message.
845 * @param message the message
846 * @param serial the serial
849 _dbus_message_set_serial (DBusMessage *message,
852 _dbus_assert (!message->locked);
853 _dbus_assert (dbus_message_get_serial (message) == 0);
855 _dbus_marshal_set_uint32 (&message->header,
857 CLIENT_SERIAL_OFFSET,
860 message->client_serial = serial;
864 * Sets the reply serial of a message (the client serial
865 * of the message this is a reply to).
867 * @param message the message
868 * @param reply_serial the client serial
869 * @returns #FALSE if not enough memory
872 dbus_message_set_reply_serial (DBusMessage *message,
873 dbus_uint32_t reply_serial)
875 _dbus_assert (!message->locked);
877 if (set_uint_field (message,
878 DBUS_HEADER_FIELD_REPLY_SERIAL,
881 message->reply_serial = reply_serial;
889 * Returns the serial of a message or 0 if none has been specified.
890 * The message's serial number is provided by the application sending
891 * the message and is used to identify replies to this message. All
892 * messages received on a connection will have a serial, but messages
893 * you haven't sent yet may return 0.
895 * @param message the message
896 * @returns the client serial
899 dbus_message_get_serial (DBusMessage *message)
901 return message->client_serial;
905 * Returns the serial that the message is a reply to or 0 if none.
907 * @param message the message
908 * @returns the reply serial
911 dbus_message_get_reply_serial (DBusMessage *message)
913 return message->reply_serial;
917 * Adds a counter to be incremented immediately with the
918 * size of this message, and decremented by the size
919 * of this message when this message if finalized.
920 * The link contains a counter with its refcount already
921 * incremented, but the counter itself not incremented.
922 * Ownership of link and counter refcount is passed to
925 * @param message the message
926 * @param link link with counter as data
929 _dbus_message_add_size_counter_link (DBusMessage *message,
932 /* right now we don't recompute the delta when message
933 * size changes, and that's OK for current purposes
934 * I think, but could be important to change later.
935 * Do recompute it whenever there are no outstanding counters,
936 * since it's basically free.
938 if (message->size_counters == NULL)
940 message->size_counter_delta =
941 _dbus_string_get_length (&message->header) +
942 _dbus_string_get_length (&message->body);
945 _dbus_verbose ("message has size %ld\n",
946 message->size_counter_delta);
950 _dbus_list_append_link (&message->size_counters, link);
952 _dbus_counter_adjust (link->data, message->size_counter_delta);
956 * Adds a counter to be incremented immediately with the
957 * size of this message, and decremented by the size
958 * of this message when this message if finalized.
960 * @param message the message
961 * @param counter the counter
962 * @returns #FALSE if no memory
965 _dbus_message_add_size_counter (DBusMessage *message,
966 DBusCounter *counter)
970 link = _dbus_list_alloc_link (counter);
974 _dbus_counter_ref (counter);
975 _dbus_message_add_size_counter_link (message, link);
981 * Removes a counter tracking the size of this message, and decrements
982 * the counter by the size of this message.
984 * @param message the message
985 * @param link_return return the link used
986 * @param counter the counter
989 _dbus_message_remove_size_counter (DBusMessage *message,
990 DBusCounter *counter,
991 DBusList **link_return)
995 link = _dbus_list_find_last (&message->size_counters,
997 _dbus_assert (link != NULL);
999 _dbus_list_unlink (&message->size_counters,
1002 *link_return = link;
1004 _dbus_list_free_link (link);
1006 _dbus_counter_adjust (counter, message->size_counter_delta);
1008 _dbus_counter_unref (counter);
1012 dbus_message_create_header (DBusMessage *message,
1014 const char *service,
1016 const char *interface,
1018 const char *error_name)
1022 _dbus_assert ((interface && member) ||
1024 !(interface || member || error_name));
1026 if (!_dbus_string_append_byte (&message->header, message->byte_order))
1029 if (!_dbus_string_append_byte (&message->header, type))
1033 if (!_dbus_string_append_byte (&message->header, flags))
1036 if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
1040 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
1044 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
1048 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
1051 /* Marshal all the fields (Marshall Fields?) */
1055 if (!append_string_field (message,
1056 DBUS_HEADER_FIELD_PATH,
1057 DBUS_TYPE_OBJECT_PATH,
1062 if (service != NULL)
1064 if (!append_string_field (message,
1065 DBUS_HEADER_FIELD_SERVICE,
1071 if (interface != NULL)
1073 if (!append_string_field (message,
1074 DBUS_HEADER_FIELD_INTERFACE,
1082 if (!append_string_field (message,
1083 DBUS_HEADER_FIELD_MEMBER,
1089 if (error_name != NULL)
1091 if (!append_string_field (message,
1092 DBUS_HEADER_FIELD_ERROR_NAME,
1102 * Locks a message. Allows checking that applications don't keep a
1103 * reference to a message in the outgoing queue and change it
1104 * underneath us. Messages are locked when they enter the outgoing
1105 * queue (dbus_connection_send_message()), and the library complains
1106 * if the message is modified while locked.
1108 * @param message the message to lock.
1111 _dbus_message_lock (DBusMessage *message)
1113 if (!message->locked)
1115 /* Fill in our lengths */
1116 _dbus_marshal_set_uint32 (&message->header,
1117 message->byte_order,
1118 HEADER_LENGTH_OFFSET,
1119 _dbus_string_get_length (&message->header));
1121 _dbus_marshal_set_uint32 (&message->header,
1122 message->byte_order,
1124 _dbus_string_get_length (&message->body));
1126 message->locked = TRUE;
1133 * @defgroup DBusMessage DBusMessage
1135 * @brief Message to be sent or received over a DBusConnection.
1137 * A DBusMessage is the most basic unit of communication over a
1138 * DBusConnection. A DBusConnection represents a stream of messages
1139 * received from a remote application, and a stream of messages
1140 * sent to a remote application.
1146 * @typedef DBusMessage
1148 * Opaque data type representing a message received from or to be
1149 * sent to another application.
1153 dbus_message_new_empty_header (void)
1155 DBusMessage *message;
1158 message = dbus_new0 (DBusMessage, 1);
1159 if (message == NULL)
1162 message->refcount.value = 1;
1163 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
1164 message->client_serial = 0;
1165 message->reply_serial = 0;
1167 _dbus_data_slot_list_init (&message->slot_list);
1170 while (i <= DBUS_HEADER_FIELD_LAST)
1172 message->header_fields[i].name_offset = -1;
1173 message->header_fields[i].value_offset = -1;
1177 if (!_dbus_string_init_preallocated (&message->header, 64))
1179 dbus_free (message);
1183 if (!_dbus_string_init_preallocated (&message->body, 64))
1185 _dbus_string_free (&message->header);
1186 dbus_free (message);
1190 if (!_dbus_string_init_preallocated (&message->signature, 4))
1192 _dbus_string_free (&message->header);
1193 _dbus_string_free (&message->body);
1194 dbus_free (message);
1202 * Constructs a new message of the given message type.
1203 * Types include #DBUS_MESSAGE_TYPE_METHOD_CALL,
1204 * #DBUS_MESSAGE_TYPE_SIGNAL, and so forth.
1206 * @param message_type type of message
1207 * @returns new message or #NULL If no memory
1210 dbus_message_new (int message_type)
1212 DBusMessage *message;
1214 _dbus_return_val_if_fail (message_type != DBUS_MESSAGE_TYPE_INVALID, NULL);
1216 message = dbus_message_new_empty_header ();
1217 if (message == NULL)
1220 if (!dbus_message_create_header (message,
1222 NULL, NULL, NULL, NULL, NULL))
1224 dbus_message_unref (message);
1232 * Constructs a new message to invoke a method on a remote
1233 * object. Returns #NULL if memory can't be allocated for the
1234 * message. The service may be #NULL in which case no service is set;
1235 * this is appropriate when using D-BUS in a peer-to-peer context (no
1236 * message bus). The interface may be #NULL, which means that
1237 * if multiple methods with the given name exist it is undefined
1238 * which one will be invoked.
1240 * @param service service that the message should be sent to or #NULL
1241 * @param path object path the message should be sent to
1242 * @param interface interface to invoke method on
1243 * @param method method to invoke
1245 * @returns a new DBusMessage, free with dbus_message_unref()
1246 * @see dbus_message_unref()
1249 dbus_message_new_method_call (const char *service,
1251 const char *interface,
1254 DBusMessage *message;
1256 _dbus_return_val_if_fail (path != NULL, NULL);
1257 _dbus_return_val_if_fail (method != NULL, NULL);
1259 message = dbus_message_new_empty_header ();
1260 if (message == NULL)
1263 if (!dbus_message_create_header (message,
1264 DBUS_MESSAGE_TYPE_METHOD_CALL,
1265 service, path, interface, method, NULL))
1267 dbus_message_unref (message);
1275 * Constructs a message that is a reply to a method call. Returns
1276 * #NULL if memory can't be allocated for the message.
1278 * @param method_call the message which the created
1279 * message is a reply to.
1280 * @returns a new DBusMessage, free with dbus_message_unref()
1281 * @see dbus_message_new_method_call(), dbus_message_unref()
1284 dbus_message_new_method_return (DBusMessage *method_call)
1286 DBusMessage *message;
1289 _dbus_return_val_if_fail (method_call != NULL, NULL);
1291 sender = get_string_field (method_call,
1292 DBUS_HEADER_FIELD_SENDER_SERVICE,
1295 /* sender is allowed to be null here in peer-to-peer case */
1297 message = dbus_message_new_empty_header ();
1298 if (message == NULL)
1301 if (!dbus_message_create_header (message,
1302 DBUS_MESSAGE_TYPE_METHOD_RETURN,
1303 sender, NULL, NULL, NULL, NULL))
1305 dbus_message_unref (message);
1309 dbus_message_set_no_reply (message, TRUE);
1311 if (!dbus_message_set_reply_serial (message,
1312 dbus_message_get_serial (method_call)))
1314 dbus_message_unref (message);
1322 * Constructs a new message representing a signal emission. Returns
1323 * #NULL if memory can't be allocated for the message.
1324 * A signal is identified by its originating interface, and
1325 * the name of the signal.
1327 * @param path the path to the object emitting the signal
1328 * @param interface the interface the signal is emitted from
1329 * @param name name of the signal
1330 * @returns a new DBusMessage, free with dbus_message_unref()
1331 * @see dbus_message_unref()
1334 dbus_message_new_signal (const char *path,
1335 const char *interface,
1338 DBusMessage *message;
1340 _dbus_return_val_if_fail (path != NULL, NULL);
1341 _dbus_return_val_if_fail (interface != NULL, NULL);
1342 _dbus_return_val_if_fail (name != NULL, NULL);
1344 message = dbus_message_new_empty_header ();
1345 if (message == NULL)
1348 if (!dbus_message_create_header (message,
1349 DBUS_MESSAGE_TYPE_SIGNAL,
1350 NULL, path, interface, name, NULL))
1352 dbus_message_unref (message);
1356 dbus_message_set_no_reply (message, TRUE);
1362 * Creates a new message that is an error reply to a certain message.
1363 * Error replies are possible in response to method calls primarily.
1365 * @param reply_to the original message
1366 * @param error_name the error name
1367 * @param error_message the error message string or #NULL for none
1368 * @returns a new error message
1371 dbus_message_new_error (DBusMessage *reply_to,
1372 const char *error_name,
1373 const char *error_message)
1375 DBusMessage *message;
1377 DBusMessageIter iter;
1379 _dbus_return_val_if_fail (reply_to != NULL, NULL);
1380 _dbus_return_val_if_fail (error_name != NULL, NULL);
1382 sender = get_string_field (reply_to,
1383 DBUS_HEADER_FIELD_SENDER_SERVICE,
1386 /* sender may be NULL for non-message-bus case or
1387 * when the message bus is dealing with an unregistered
1390 message = dbus_message_new_empty_header ();
1391 if (message == NULL)
1394 if (!dbus_message_create_header (message,
1395 DBUS_MESSAGE_TYPE_ERROR,
1396 sender, NULL, NULL, NULL, error_name))
1398 dbus_message_unref (message);
1402 dbus_message_set_no_reply (message, TRUE);
1404 if (!dbus_message_set_reply_serial (message,
1405 dbus_message_get_serial (reply_to)))
1407 dbus_message_unref (message);
1411 if (error_message != NULL)
1413 dbus_message_append_iter_init (message, &iter);
1414 if (!dbus_message_iter_append_string (&iter, error_message))
1416 dbus_message_unref (message);
1425 * Creates a new message that is an exact replica of the message
1426 * specified, except that its refcount is set to 1.
1428 * @param message the message.
1429 * @returns the new message.
1432 dbus_message_copy (const DBusMessage *message)
1434 DBusMessage *retval;
1437 _dbus_return_val_if_fail (message != NULL, NULL);
1439 retval = dbus_new0 (DBusMessage, 1);
1443 retval->refcount.value = 1;
1444 retval->byte_order = message->byte_order;
1445 retval->client_serial = message->client_serial;
1446 retval->reply_serial = message->reply_serial;
1447 retval->header_padding = message->header_padding;
1448 retval->locked = FALSE;
1450 if (!_dbus_string_init_preallocated (&retval->header,
1451 _dbus_string_get_length (&message->header)))
1457 if (!_dbus_string_init_preallocated (&retval->body,
1458 _dbus_string_get_length (&message->body)))
1460 _dbus_string_free (&retval->header);
1465 if (!_dbus_string_init_preallocated (&retval->signature,
1466 _dbus_string_get_length (&message->signature)))
1468 _dbus_string_free (&retval->header);
1469 _dbus_string_free (&retval->body);
1474 if (!_dbus_string_copy (&message->header, 0,
1475 &retval->header, 0))
1478 if (!_dbus_string_copy (&message->body, 0,
1482 if (!_dbus_string_copy (&message->signature, 0,
1483 &retval->signature, 0))
1486 for (i = 0; i <= DBUS_HEADER_FIELD_LAST; i++)
1488 retval->header_fields[i] = message->header_fields[i];
1494 _dbus_string_free (&retval->header);
1495 _dbus_string_free (&retval->body);
1496 _dbus_string_free (&retval->signature);
1504 * Increments the reference count of a DBusMessage.
1506 * @param message The message
1507 * @returns the message
1508 * @see dbus_message_unref
1511 dbus_message_ref (DBusMessage *message)
1513 dbus_int32_t old_refcount;
1515 _dbus_return_val_if_fail (message != NULL, NULL);
1517 old_refcount = _dbus_atomic_inc (&message->refcount);
1518 _dbus_assert (old_refcount >= 1);
1524 free_size_counter (void *element,
1527 DBusCounter *counter = element;
1528 DBusMessage *message = data;
1530 _dbus_counter_adjust (counter, - message->size_counter_delta);
1532 _dbus_counter_unref (counter);
1536 * Decrements the reference count of a DBusMessage.
1538 * @param message The message
1539 * @see dbus_message_ref
1542 dbus_message_unref (DBusMessage *message)
1544 dbus_int32_t old_refcount;
1546 _dbus_return_if_fail (message != NULL);
1548 old_refcount = _dbus_atomic_dec (&message->refcount);
1550 _dbus_assert (old_refcount >= 0);
1552 if (old_refcount == 1)
1554 /* This calls application callbacks! */
1555 _dbus_data_slot_list_free (&message->slot_list);
1557 _dbus_list_foreach (&message->size_counters,
1558 free_size_counter, message);
1559 _dbus_list_clear (&message->size_counters);
1561 _dbus_string_free (&message->header);
1562 _dbus_string_free (&message->body);
1563 _dbus_string_free (&message->signature);
1565 dbus_free (message);
1570 * Gets the type of a message. Types include
1571 * DBUS_MESSAGE_TYPE_METHOD_CALL, DBUS_MESSAGE_TYPE_METHOD_RETURN,
1572 * DBUS_MESSAGE_TYPE_ERROR, DBUS_MESSAGE_TYPE_SIGNAL, but other types
1573 * are allowed and all code must silently ignore messages of unknown
1574 * type. DBUS_MESSAGE_TYPE_INVALID will never be returned, however.
1577 * @param message the message
1578 * @returns the type of the message
1581 dbus_message_get_type (DBusMessage *message)
1585 type = _dbus_string_get_byte (&message->header, 1);
1586 _dbus_assert (type != DBUS_MESSAGE_TYPE_INVALID);
1592 * Sets the object path this message is being sent to (for
1593 * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
1594 * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
1596 * @param message the message
1597 * @param object_path the path
1598 * @returns #FALSE if not enough memory
1601 dbus_message_set_path (DBusMessage *message,
1602 const char *object_path)
1604 _dbus_return_val_if_fail (message != NULL, FALSE);
1605 _dbus_return_val_if_fail (!message->locked, FALSE);
1607 return set_string_field (message,
1608 DBUS_HEADER_FIELD_PATH,
1609 DBUS_TYPE_OBJECT_PATH,
1614 * Gets the object path this message is being sent to
1615 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
1616 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
1618 * @param message the message
1619 * @returns the path (should not be freed)
1622 dbus_message_get_path (DBusMessage *message)
1624 _dbus_return_val_if_fail (message != NULL, NULL);
1626 return get_string_field (message, DBUS_HEADER_FIELD_PATH, NULL);
1630 * Gets the object path this message is being sent to
1631 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
1632 * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
1633 * format (one array element per path component).
1634 * Free the returned array with dbus_free_string_array().
1636 * An empty but non-NULL path array means the path "/".
1637 * So the path "/foo/bar" becomes { "foo", "bar", NULL }
1638 * and the path "/" becomes { NULL }.
1640 * @param message the message
1641 * @param path place to store allocated array of path components; #NULL set here if no path field exists
1642 * @returns #FALSE if no memory to allocate the array
1645 dbus_message_get_path_decomposed (DBusMessage *message,
1648 _dbus_return_val_if_fail (message != NULL, FALSE);
1649 _dbus_return_val_if_fail (path != NULL, FALSE);
1651 return get_path_field_decomposed (message,
1652 DBUS_HEADER_FIELD_PATH,
1657 * Sets the interface this message is being sent to
1658 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
1659 * the interface a signal is being emitted from
1660 * (for DBUS_MESSAGE_TYPE_SIGNAL).
1662 * @param message the message
1663 * @param interface the interface
1664 * @returns #FALSE if not enough memory
1667 dbus_message_set_interface (DBusMessage *message,
1668 const char *interface)
1670 _dbus_return_val_if_fail (message != NULL, FALSE);
1671 _dbus_return_val_if_fail (!message->locked, FALSE);
1673 return set_string_field (message,
1674 DBUS_HEADER_FIELD_INTERFACE,
1680 * Gets the interface this message is being sent to
1681 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
1682 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
1683 * The interface name is fully-qualified (namespaced).
1685 * @param message the message
1686 * @returns the message interface (should not be freed)
1689 dbus_message_get_interface (DBusMessage *message)
1691 _dbus_return_val_if_fail (message != NULL, NULL);
1693 return get_string_field (message, DBUS_HEADER_FIELD_INTERFACE, NULL);
1697 * Sets the interface member being invoked
1698 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
1699 * (DBUS_MESSAGE_TYPE_SIGNAL).
1700 * The interface name is fully-qualified (namespaced).
1702 * @param message the message
1703 * @param member the member
1704 * @returns #FALSE if not enough memory
1707 dbus_message_set_member (DBusMessage *message,
1710 _dbus_return_val_if_fail (message != NULL, FALSE);
1711 _dbus_return_val_if_fail (!message->locked, FALSE);
1713 return set_string_field (message,
1714 DBUS_HEADER_FIELD_MEMBER,
1720 * Gets the interface member being invoked
1721 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
1722 * (DBUS_MESSAGE_TYPE_SIGNAL).
1724 * @param message the message
1725 * @returns the member name (should not be freed)
1728 dbus_message_get_member (DBusMessage *message)
1730 _dbus_return_val_if_fail (message != NULL, NULL);
1732 return get_string_field (message,
1733 DBUS_HEADER_FIELD_MEMBER,
1738 * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
1739 * The name is fully-qualified (namespaced).
1741 * @param message the message
1742 * @param error_name the name
1743 * @returns #FALSE if not enough memory
1746 dbus_message_set_error_name (DBusMessage *message,
1747 const char *error_name)
1749 _dbus_return_val_if_fail (message != NULL, FALSE);
1750 _dbus_return_val_if_fail (!message->locked, FALSE);
1752 return set_string_field (message,
1753 DBUS_HEADER_FIELD_ERROR_NAME,
1759 * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only).
1761 * @param message the message
1762 * @returns the error name (should not be freed)
1765 dbus_message_get_error_name (DBusMessage *message)
1767 _dbus_return_val_if_fail (message != NULL, NULL);
1769 return get_string_field (message,
1770 DBUS_HEADER_FIELD_ERROR_NAME,
1775 * Sets the message's destination service.
1777 * @param message the message
1778 * @param destination the destination service name
1779 * @returns #FALSE if not enough memory
1782 dbus_message_set_destination (DBusMessage *message,
1783 const char *destination)
1785 _dbus_return_val_if_fail (message != NULL, FALSE);
1786 _dbus_return_val_if_fail (!message->locked, FALSE);
1788 return set_string_field (message,
1789 DBUS_HEADER_FIELD_SERVICE,
1795 * Gets the destination service of a message.
1797 * @param message the message
1798 * @returns the message destination service (should not be freed)
1801 dbus_message_get_destination (DBusMessage *message)
1803 _dbus_return_val_if_fail (message != NULL, NULL);
1805 return get_string_field (message,
1806 DBUS_HEADER_FIELD_SERVICE,
1811 * Appends fields to a message given a variable argument list. The
1812 * variable argument list should contain the type of the argument
1813 * followed by the value to add. Array values are specified by an int
1814 * typecode followed by a pointer to the array followed by an int
1815 * giving the length of the array. The argument list must be
1816 * terminated with #DBUS_TYPE_INVALID.
1818 * This function doesn't support dicts or non-fundamental arrays.
1820 * This function supports #DBUS_TYPE_INT64 and #DBUS_TYPE_UINT64
1821 * only if #DBUS_HAVE_INT64 is defined.
1823 * @param message the message
1824 * @param first_arg_type type of the first argument
1825 * @param ... value of first argument, list of additional type-value pairs
1826 * @returns #TRUE on success
1829 dbus_message_append_args (DBusMessage *message,
1836 _dbus_return_val_if_fail (message != NULL, FALSE);
1838 va_start (var_args, first_arg_type);
1839 retval = dbus_message_append_args_valist (message,
1848 * This function takes a va_list for use by language bindings.
1849 * It's otherwise the same as dbus_message_append_args().
1851 * @todo: Shouldn't this function clean up the changes to the message
1852 * on failures? (Yes)
1854 * @see dbus_message_append_args.
1855 * @param message the message
1856 * @param first_arg_type type of first argument
1857 * @param var_args value of first argument, then list of type/value pairs
1858 * @returns #TRUE on success
1861 dbus_message_append_args_valist (DBusMessage *message,
1866 DBusMessageIter iter;
1868 _dbus_return_val_if_fail (message != NULL, FALSE);
1870 old_len = _dbus_string_get_length (&message->body);
1872 type = first_arg_type;
1874 dbus_message_append_iter_init (message, &iter);
1876 while (type != DBUS_TYPE_INVALID)
1881 if (!dbus_message_iter_append_nil (&iter))
1884 case DBUS_TYPE_BOOLEAN:
1885 if (!dbus_message_iter_append_boolean (&iter, va_arg (var_args, dbus_bool_t)))
1888 case DBUS_TYPE_INT32:
1889 if (!dbus_message_iter_append_int32 (&iter, va_arg (var_args, dbus_int32_t)))
1892 case DBUS_TYPE_UINT32:
1893 if (!dbus_message_iter_append_uint32 (&iter, va_arg (var_args, dbus_uint32_t)))
1896 #ifdef DBUS_HAVE_INT64
1897 case DBUS_TYPE_INT64:
1898 if (!dbus_message_iter_append_int64 (&iter, va_arg (var_args, dbus_int64_t)))
1901 case DBUS_TYPE_UINT64:
1902 if (!dbus_message_iter_append_uint64 (&iter, va_arg (var_args, dbus_uint64_t)))
1905 #endif /* DBUS_HAVE_INT64 */
1906 case DBUS_TYPE_DOUBLE:
1907 if (!dbus_message_iter_append_double (&iter, va_arg (var_args, double)))
1910 case DBUS_TYPE_STRING:
1911 if (!dbus_message_iter_append_string (&iter, va_arg (var_args, const char *)))
1914 case DBUS_TYPE_OBJECT_PATH:
1917 case DBUS_TYPE_CUSTOM:
1920 unsigned char *data;
1923 name = va_arg (var_args, const char *);
1924 data = va_arg (var_args, unsigned char *);
1925 len = va_arg (var_args, int);
1927 if (!dbus_message_iter_append_custom (&iter, name, data, len))
1931 case DBUS_TYPE_ARRAY:
1936 type = va_arg (var_args, int);
1937 data = va_arg (var_args, void *);
1938 len = va_arg (var_args, int);
1942 case DBUS_TYPE_BYTE:
1943 if (!dbus_message_iter_append_byte_array (&iter, (unsigned char *)data, len))
1946 case DBUS_TYPE_BOOLEAN:
1947 if (!dbus_message_iter_append_boolean_array (&iter, (unsigned char *)data, len))
1950 case DBUS_TYPE_INT32:
1951 if (!dbus_message_iter_append_int32_array (&iter, (dbus_int32_t *)data, len))
1954 case DBUS_TYPE_UINT32:
1955 if (!dbus_message_iter_append_uint32_array (&iter, (dbus_uint32_t *)data, len))
1958 #ifdef DBUS_HAVE_INT64
1959 case DBUS_TYPE_INT64:
1960 if (!dbus_message_iter_append_int64_array (&iter, (dbus_int64_t *)data, len))
1963 case DBUS_TYPE_UINT64:
1964 if (!dbus_message_iter_append_uint64_array (&iter, (dbus_uint64_t *)data, len))
1967 #endif /* DBUS_HAVE_INT64 */
1968 case DBUS_TYPE_DOUBLE:
1969 if (!dbus_message_iter_append_double_array (&iter, (double *)data, len))
1972 case DBUS_TYPE_STRING:
1973 if (!dbus_message_iter_append_string_array (&iter, (const char **)data, len))
1977 case DBUS_TYPE_ARRAY:
1978 case DBUS_TYPE_CUSTOM:
1979 case DBUS_TYPE_DICT:
1980 _dbus_warn ("dbus_message_append_args_valist doesn't support recursive arrays\n");
1983 _dbus_warn ("Unknown field type %d\n", type);
1989 case DBUS_TYPE_DICT:
1990 _dbus_warn ("dbus_message_append_args_valist doesn't support dicts\n");
1993 _dbus_warn ("Unknown field type %d\n", type);
1997 type = va_arg (var_args, int);
2008 * Gets arguments from a message given a variable argument list.
2009 * The variable argument list should contain the type of the
2010 * argumen followed by a pointer to where the value should be
2011 * stored. The list is terminated with #DBUS_TYPE_INVALID.
2013 * @param message the message
2014 * @param error error to be filled in on failure
2015 * @param first_arg_type the first argument type
2016 * @param ... location for first argument value, then list of type-location pairs
2017 * @returns #FALSE if the error was set
2020 dbus_message_get_args (DBusMessage *message,
2028 _dbus_return_val_if_fail (message != NULL, FALSE);
2029 _dbus_return_val_if_error_is_set (error, FALSE);
2031 va_start (var_args, first_arg_type);
2032 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
2039 * This function takes a va_list for use by language bindings
2041 * @todo We need to free the argument data when an error occurs.
2043 * @see dbus_message_get_args
2044 * @param message the message
2045 * @param error error to be filled in
2046 * @param first_arg_type type of the first argument
2047 * @param var_args return location for first argument, followed by list of type/location pairs
2048 * @returns #FALSE if error was set
2051 dbus_message_get_args_valist (DBusMessage *message,
2056 DBusMessageIter iter;
2058 _dbus_return_val_if_fail (message != NULL, FALSE);
2059 _dbus_return_val_if_error_is_set (error, FALSE);
2061 dbus_message_iter_init (message, &iter);
2062 return dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
2066 * Gets arguments from a message iterator given a variable argument list.
2067 * The variable argument list should contain the type of the
2068 * argumen followed by a pointer to where the value should be
2069 * stored. The list is terminated with 0.
2071 * @param iter the message iterator
2072 * @param error error to be filled in on failure
2073 * @param first_arg_type the first argument type
2074 * @param ... location for first argument value, then list of type-location pairs
2075 * @returns #FALSE if the error was set
2078 dbus_message_iter_get_args (DBusMessageIter *iter,
2086 _dbus_return_val_if_fail (iter != NULL, FALSE);
2087 _dbus_return_val_if_error_is_set (error, FALSE);
2089 va_start (var_args, first_arg_type);
2090 retval = dbus_message_iter_get_args_valist (iter, error, first_arg_type, var_args);
2097 * This function takes a va_list for use by language bindings
2099 * This function supports #DBUS_TYPE_INT64 and #DBUS_TYPE_UINT64
2100 * only if #DBUS_HAVE_INT64 is defined.
2102 * @todo this function (or some lower-level non-convenience function)
2103 * needs better error handling; should allow the application to
2104 * distinguish between out of memory, and bad data from the remote
2105 * app. It also needs to not leak a bunch of args when it gets
2106 * to the arg that's bad, as that would be a security hole
2107 * (allow one app to force another to leak memory)
2109 * @todo We need to free the argument data when an error occurs.
2111 * @see dbus_message_get_args
2112 * @param iter the message iter
2113 * @param error error to be filled in
2114 * @param first_arg_type type of the first argument
2115 * @param var_args return location for first argument, followed by list of type/location pairs
2116 * @returns #FALSE if error was set
2119 dbus_message_iter_get_args_valist (DBusMessageIter *iter,
2124 int spec_type, msg_type, i;
2127 _dbus_return_val_if_fail (iter != NULL, FALSE);
2128 _dbus_return_val_if_error_is_set (error, FALSE);
2132 spec_type = first_arg_type;
2135 while (spec_type != 0)
2137 msg_type = dbus_message_iter_get_arg_type (iter);
2139 if (msg_type != spec_type)
2141 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2142 "Argument %d is specified to be of type \"%s\", but "
2143 "is actually of type \"%s\"\n", i,
2144 _dbus_type_to_string (spec_type),
2145 _dbus_type_to_string (msg_type));
2154 case DBUS_TYPE_BYTE:
2158 ptr = va_arg (var_args, unsigned char *);
2160 *ptr = dbus_message_iter_get_byte (iter);
2163 case DBUS_TYPE_BOOLEAN:
2167 ptr = va_arg (var_args, dbus_bool_t *);
2169 *ptr = dbus_message_iter_get_boolean (iter);
2172 case DBUS_TYPE_INT32:
2176 ptr = va_arg (var_args, dbus_int32_t *);
2178 *ptr = dbus_message_iter_get_int32 (iter);
2181 case DBUS_TYPE_UINT32:
2185 ptr = va_arg (var_args, dbus_uint32_t *);
2187 *ptr = dbus_message_iter_get_uint32 (iter);
2190 #ifdef DBUS_HAVE_INT64
2191 case DBUS_TYPE_INT64:
2195 ptr = va_arg (var_args, dbus_int64_t *);
2197 *ptr = dbus_message_iter_get_int64 (iter);
2200 case DBUS_TYPE_UINT64:
2204 ptr = va_arg (var_args, dbus_uint64_t *);
2206 *ptr = dbus_message_iter_get_uint64 (iter);
2209 #endif /* DBUS_HAVE_INT64 */
2211 case DBUS_TYPE_DOUBLE:
2215 ptr = va_arg (var_args, double *);
2217 *ptr = dbus_message_iter_get_double (iter);
2221 case DBUS_TYPE_STRING:
2225 ptr = va_arg (var_args, char **);
2227 *ptr = dbus_message_iter_get_string (iter);
2231 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2238 case DBUS_TYPE_CUSTOM:
2241 unsigned char **data;
2244 name = va_arg (var_args, char **);
2245 data = va_arg (var_args, unsigned char **);
2246 len = va_arg (var_args, int *);
2248 if (!dbus_message_iter_get_custom (iter, name, data, len))
2250 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2255 case DBUS_TYPE_ARRAY:
2260 type = va_arg (var_args, int);
2261 data = va_arg (var_args, void *);
2262 len = va_arg (var_args, int *);
2264 _dbus_return_val_if_fail (data != NULL, FALSE);
2265 _dbus_return_val_if_fail (len != NULL, FALSE);
2267 if (dbus_message_iter_get_array_type (iter) != type)
2269 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2270 "Argument %d is specified to be of type \"array of %s\", but "
2271 "is actually of type \"array of %s\"\n", i,
2272 _dbus_type_to_string (type),
2273 _dbus_type_to_string (dbus_message_iter_get_array_type (iter)));
2279 case DBUS_TYPE_BYTE:
2280 if (!dbus_message_iter_get_byte_array (iter, (unsigned char **)data, len))
2282 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2286 case DBUS_TYPE_BOOLEAN:
2287 if (!dbus_message_iter_get_boolean_array (iter, (unsigned char **)data, len))
2289 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2293 case DBUS_TYPE_INT32:
2294 if (!dbus_message_iter_get_int32_array (iter, (dbus_int32_t **)data, len))
2296 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2300 case DBUS_TYPE_UINT32:
2301 if (!dbus_message_iter_get_uint32_array (iter, (dbus_uint32_t **)data, len))
2303 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2307 #ifdef DBUS_HAVE_INT64
2308 case DBUS_TYPE_INT64:
2309 if (!dbus_message_iter_get_int64_array (iter, (dbus_int64_t **)data, len))
2311 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2315 case DBUS_TYPE_UINT64:
2316 if (!dbus_message_iter_get_uint64_array (iter, (dbus_uint64_t **)data, len))
2318 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2322 #endif /* DBUS_HAVE_INT64 */
2323 case DBUS_TYPE_DOUBLE:
2324 if (!dbus_message_iter_get_double_array (iter, (double **)data, len))
2326 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2330 case DBUS_TYPE_STRING:
2331 if (!dbus_message_iter_get_string_array (iter, (char ***)data, len))
2333 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2338 case DBUS_TYPE_ARRAY:
2339 case DBUS_TYPE_CUSTOM:
2340 case DBUS_TYPE_DICT:
2341 _dbus_warn ("dbus_message_get_args_valist doesn't support recursive arrays\n");
2342 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2345 _dbus_warn ("Unknown field type %d\n", type);
2346 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2351 case DBUS_TYPE_DICT:
2352 _dbus_warn ("dbus_message_get_args_valist doesn't support dicts\n");
2353 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2356 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2357 _dbus_warn ("Unknown field type %d\n", spec_type);
2361 spec_type = va_arg (var_args, int);
2362 if (spec_type != 0 && !dbus_message_iter_next (iter))
2364 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2365 "Message has only %d arguments, but more were expected", i);
2381 * Initializes a DBusMessageIter representing the arguments of the
2382 * message passed in.
2384 * @param message the message
2385 * @param iter pointer to an iterator to initialize
2388 dbus_message_iter_init (DBusMessage *message,
2389 DBusMessageIter *iter)
2391 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2393 _dbus_return_if_fail (message != NULL);
2394 _dbus_return_if_fail (iter != NULL);
2396 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
2398 real->message = message;
2399 real->parent_iter = NULL;
2400 real->changed_stamp = message->changed_stamp;
2402 real->type = DBUS_MESSAGE_ITER_TYPE_MESSAGE;
2404 real->end = _dbus_string_get_length (&message->body);
2406 real->container_start = 0;
2407 real->container_length_pos = 0;
2408 real->wrote_dict_key = 0;
2409 real->array_type_pos = 0;
2412 #ifndef DBUS_DISABLE_CHECKS
2414 dbus_message_iter_check (DBusMessageRealIter *iter)
2418 _dbus_warn ("dbus iterator check failed: iterator is NULL\n");
2422 if (iter->changed_stamp != iter->message->changed_stamp)
2424 _dbus_warn ("dbus iterator check failed: invalid iterator, must re-initialize it after modifying the message\n");
2428 if (iter->pos < 0 || iter->pos > iter->end)
2430 _dbus_warn ("dbus iterator check failed: invalid position\n");
2436 #endif /* DBUS_DISABLE_CHECKS */
2439 skip_array_type (DBusMessageRealIter *iter, int pos)
2445 data = _dbus_string_get_const_data_len (&iter->message->body,
2448 while (*data == DBUS_TYPE_ARRAY);
2453 /* FIXME what are these _dbus_type_is_valid() checks for?
2454 * haven't we validated the message?
2457 dbus_message_iter_get_data_start (DBusMessageRealIter *iter, int *type)
2464 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
2465 data = _dbus_string_get_const_data_len (&iter->message->body,
2467 if (_dbus_type_is_valid (*data))
2470 *type = DBUS_TYPE_INVALID;
2472 return skip_array_type (iter, iter->pos);
2474 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
2475 data = _dbus_string_get_const_data_len (&iter->message->body,
2476 iter->array_type_pos, 1);
2477 if (_dbus_type_is_valid (*data))
2480 *type = DBUS_TYPE_INVALID;
2484 case DBUS_MESSAGE_ITER_TYPE_DICT:
2485 /* Get the length of the string */
2486 len = _dbus_demarshal_uint32 (&iter->message->body,
2487 iter->message->byte_order,
2489 pos = pos + len + 1;
2491 data = _dbus_string_get_const_data_len (&iter->message->body,
2493 if (_dbus_type_is_valid (*data))
2496 *type = DBUS_TYPE_INVALID;
2498 return skip_array_type (iter, pos);
2501 _dbus_assert_not_reached ("Invalid iter type");
2504 *type = DBUS_TYPE_INVALID;
2510 * Checks if an iterator has any more fields.
2512 * @param iter the message iter
2513 * @returns #TRUE if there are more fields
2517 dbus_message_iter_has_next (DBusMessageIter *iter)
2519 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2523 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2525 if (real->pos >= real->end)
2528 pos = dbus_message_iter_get_data_start (real, &type);
2530 if (!_dbus_marshal_get_arg_end_pos (&real->message->body,
2531 real->message->byte_order,
2532 type, pos, &end_pos))
2535 if (end_pos >= real->end)
2542 * Moves the iterator to the next field.
2544 * @param iter The message iter
2545 * @returns #TRUE if the iterator was moved to the next field
2548 dbus_message_iter_next (DBusMessageIter *iter)
2550 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2554 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2556 pos = dbus_message_iter_get_data_start (real, &type);
2558 if (!_dbus_marshal_get_arg_end_pos (&real->message->body,
2559 real->message->byte_order,
2560 type, pos, &end_pos))
2563 if (end_pos >= real->end)
2566 real->pos = end_pos;
2572 * Returns the argument type of the argument that the
2573 * message iterator points at.
2575 * @param iter the message iter
2576 * @returns the field type
2579 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
2581 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2584 _dbus_return_val_if_fail (dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2586 if (real->pos >= real->end)
2588 _dbus_verbose (" iterator at or beyond end of message\n");
2589 return DBUS_TYPE_INVALID;
2592 pos = dbus_message_iter_get_data_start (real, &type);
2597 /* FIXME why do we validate the typecode in here, hasn't the message
2598 * already been verified?
2601 iter_get_array_type (DBusMessageRealIter *iter, int *array_type_pos)
2604 int _array_type_pos;
2609 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
2610 _array_type_pos = iter->pos + 1;
2612 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
2613 _array_type_pos = iter->array_type_pos + 1;
2615 case DBUS_MESSAGE_ITER_TYPE_DICT:
2616 /* Get the length of the string */
2617 len = _dbus_demarshal_uint32 (&iter->message->body,
2618 iter->message->byte_order,
2620 pos = pos + len + 1;
2621 data = _dbus_string_get_const_data_len (&iter->message->body,
2623 _array_type_pos = pos + 1;
2626 _dbus_assert_not_reached ("wrong iter type");
2627 return DBUS_TYPE_INVALID;
2630 if (array_type_pos != NULL)
2631 *array_type_pos = _array_type_pos;
2633 data = _dbus_string_get_const_data_len (&iter->message->body,
2634 _array_type_pos, 1);
2635 if (_dbus_type_is_valid (*data))
2638 return DBUS_TYPE_INVALID;
2643 * Returns the element type of the array that the
2644 * message iterator points at. Note that you need
2645 * to check that the iterator points to an array
2646 * prior to using this function.
2648 * @param iter the message iter
2649 * @returns the field type
2652 dbus_message_iter_get_array_type (DBusMessageIter *iter)
2654 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2657 _dbus_return_val_if_fail (dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2659 if (real->pos >= real->end)
2660 return DBUS_TYPE_INVALID;
2662 pos = dbus_message_iter_get_data_start (real, &type);
2664 _dbus_assert (type == DBUS_TYPE_ARRAY);
2666 return iter_get_array_type (real, NULL);
2671 * Returns the string value that an iterator may point to.
2672 * Note that you need to check that the iterator points to
2673 * a string value before using this function.
2675 * @see dbus_message_iter_get_arg_type
2676 * @param iter the message iter
2677 * @returns the string
2680 dbus_message_iter_get_string (DBusMessageIter *iter)
2682 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2685 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
2687 pos = dbus_message_iter_get_data_start (real, &type);
2689 _dbus_assert (type == DBUS_TYPE_STRING);
2691 return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2697 * @todo FIXME to finish this _dbus_demarshal_object_path() needs
2698 * to not explode the path.
2700 * Returns the object path value that an iterator may point to.
2701 * Note that you need to check that the iterator points to
2702 * an object path value before using this function.
2704 * @see dbus_message_iter_get_arg_type
2705 * @param iter the message iter
2709 dbus_message_iter_get_object_path (DBusMessageIter *iter)
2711 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2714 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
2716 pos = dbus_message_iter_get_data_start (real, &type);
2718 _dbus_assert (type == DBUS_TYPE_OBJECT_PATH);
2720 return _dbus_demarshal_object_path (&real->message->body, real->message->byte_order,
2726 * Returns the name and data from a custom type that an iterator may
2727 * point to. Note that you need to check that the iterator points to a
2728 * custom type before using this function.
2730 * @see dbus_message_iter_get_arg_type
2731 * @param iter the message iter
2732 * @param name return location for the name of the custom type
2733 * @param value return location for data
2734 * @param len return location for length of data
2735 * @returns TRUE if get succeed
2739 dbus_message_iter_get_custom (DBusMessageIter *iter,
2741 unsigned char **value,
2744 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2748 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2750 pos = dbus_message_iter_get_data_start (real, &type);
2752 _dbus_assert (type == DBUS_TYPE_CUSTOM);
2754 _name = _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2760 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
2761 pos, NULL, value, len))
2773 * Returns the byte value that an iterator may point to.
2774 * Note that you need to check that the iterator points to
2775 * a byte value before using this function.
2777 * @see dbus_message_iter_get_arg_type
2778 * @param iter the message iter
2779 * @returns the byte value
2782 dbus_message_iter_get_byte (DBusMessageIter *iter)
2784 unsigned char value;
2785 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2788 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2790 pos = dbus_message_iter_get_data_start (real, &type);
2792 _dbus_assert (type == DBUS_TYPE_BYTE);
2794 value = _dbus_string_get_byte (&real->message->body, pos);
2801 * Returns the boolean value that an iterator may point to.
2802 * Note that you need to check that the iterator points to
2803 * a boolean value before using this function.
2805 * @see dbus_message_iter_get_arg_type
2806 * @param iter the message iter
2807 * @returns the boolean value
2810 dbus_message_iter_get_boolean (DBusMessageIter *iter)
2812 unsigned char value;
2813 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2816 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2818 pos = dbus_message_iter_get_data_start (real, &type);
2820 _dbus_assert (type == DBUS_TYPE_BOOLEAN);
2822 value = _dbus_string_get_byte (&real->message->body, pos);
2828 * Returns the 32 bit signed integer value that an iterator may point to.
2829 * Note that you need to check that the iterator points to
2830 * a 32-bit integer value before using this function.
2832 * @see dbus_message_iter_get_arg_type
2833 * @param iter the message iter
2834 * @returns the integer
2837 dbus_message_iter_get_int32 (DBusMessageIter *iter)
2839 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2842 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2844 pos = dbus_message_iter_get_data_start (real, &type);
2846 _dbus_assert (type == DBUS_TYPE_INT32);
2848 return _dbus_demarshal_int32 (&real->message->body, real->message->byte_order,
2853 * Returns the 32 bit unsigned integer value that an iterator may point to.
2854 * Note that you need to check that the iterator points to
2855 * a 32-bit unsigned 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_uint32 (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_UINT32);
2873 return _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
2877 #ifdef DBUS_HAVE_INT64
2880 * Returns the 64 bit signed integer value that an iterator may point
2881 * to. Note that you need to check that the iterator points to a
2882 * 64-bit integer value before using this function.
2884 * This function only exists if #DBUS_HAVE_INT64 is defined.
2886 * @see dbus_message_iter_get_arg_type
2887 * @param iter the message iter
2888 * @returns the integer
2891 dbus_message_iter_get_int64 (DBusMessageIter *iter)
2893 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2896 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2898 pos = dbus_message_iter_get_data_start (real, &type);
2900 _dbus_assert (type == DBUS_TYPE_INT64);
2902 return _dbus_demarshal_int64 (&real->message->body, real->message->byte_order,
2907 * Returns the 64 bit unsigned integer value that an iterator may point to.
2908 * Note that you need to check that the iterator points to
2909 * a 64-bit unsigned integer value before using this function.
2911 * This function only exists if #DBUS_HAVE_INT64 is defined.
2913 * @see dbus_message_iter_get_arg_type
2914 * @param iter the message iter
2915 * @returns the integer
2918 dbus_message_iter_get_uint64 (DBusMessageIter *iter)
2920 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2923 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2925 pos = dbus_message_iter_get_data_start (real, &type);
2927 _dbus_assert (type == DBUS_TYPE_UINT64);
2929 return _dbus_demarshal_uint64 (&real->message->body, real->message->byte_order,
2933 #endif /* DBUS_HAVE_INT64 */
2936 * Returns the double value that an iterator may point to.
2937 * Note that you need to check that the iterator points to
2938 * a string value before using this function.
2940 * @see dbus_message_iter_get_arg_type
2941 * @param iter the message iter
2942 * @returns the double
2945 dbus_message_iter_get_double (DBusMessageIter *iter)
2947 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2950 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0.0);
2952 pos = dbus_message_iter_get_data_start (real, &type);
2954 _dbus_assert (type == DBUS_TYPE_DOUBLE);
2956 return _dbus_demarshal_double (&real->message->body, real->message->byte_order,
2961 * Initializes an iterator for the array that the iterator
2962 * may point to. Note that you need to check that the iterator
2963 * points to an array prior to using this function.
2965 * The array element type is returned in array_type, and the array
2966 * iterator can only be used to get that type of data.
2968 * @param iter the iterator
2969 * @param array_iter pointer to an iterator to initialize
2970 * @param array_type gets set to the type of the array elements
2973 dbus_message_iter_init_array_iterator (DBusMessageIter *iter,
2974 DBusMessageIter *array_iter,
2977 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2978 DBusMessageRealIter *array_real = (DBusMessageRealIter *)array_iter;
2979 int type, pos, len_pos, len, array_type_pos;
2982 _dbus_return_if_fail (dbus_message_iter_check (real));
2984 pos = dbus_message_iter_get_data_start (real, &type);
2986 _dbus_assert (type == DBUS_TYPE_ARRAY);
2988 _array_type = iter_get_array_type (real, &array_type_pos);
2990 len_pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
2991 len = _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
2994 array_real->parent_iter = real;
2995 array_real->message = real->message;
2996 array_real->changed_stamp = real->message->changed_stamp;
2998 array_real->type = DBUS_MESSAGE_ITER_TYPE_ARRAY;
2999 array_real->pos = pos;
3000 array_real->end = pos + len;
3002 array_real->container_start = pos;
3003 array_real->container_length_pos = len_pos;
3004 array_real->wrote_dict_key = 0;
3005 array_real->array_type_pos = array_type_pos;
3006 array_real->array_type_done = TRUE;
3008 if (array_type != NULL)
3009 *array_type = _array_type;
3014 * Initializes an iterator for the dict that the iterator
3015 * may point to. Note that you need to check that the iterator
3016 * points to a dict prior to using this function.
3018 * @param iter the iterator
3019 * @param dict_iter pointer to an iterator to initialize
3022 dbus_message_iter_init_dict_iterator (DBusMessageIter *iter,
3023 DBusMessageIter *dict_iter)
3025 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3026 DBusMessageRealIter *dict_real = (DBusMessageRealIter *)dict_iter;
3027 int type, pos, len_pos, len;
3029 _dbus_return_if_fail (dbus_message_iter_check (real));
3031 pos = dbus_message_iter_get_data_start (real, &type);
3033 _dbus_assert (type == DBUS_TYPE_DICT);
3035 len_pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
3036 len = _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
3039 dict_real->parent_iter = real;
3040 dict_real->message = real->message;
3041 dict_real->changed_stamp = real->message->changed_stamp;
3043 dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
3044 dict_real->pos = pos;
3045 dict_real->end = pos + len;
3047 dict_real->container_start = pos;
3048 dict_real->container_length_pos = len_pos;
3049 dict_real->wrote_dict_key = 0;
3053 * Returns the byte array that the iterator may point to.
3054 * Note that you need to check that the iterator points
3055 * to a byte array prior to using this function.
3057 * @param iter the iterator
3058 * @param value return location for array values
3059 * @param len return location for length of byte array
3060 * @returns #TRUE on success
3063 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
3064 unsigned char **value,
3067 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3070 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3072 pos = dbus_message_iter_get_data_start (real, &type);
3074 _dbus_assert (type == DBUS_TYPE_ARRAY);
3076 type = iter_get_array_type (real, NULL);
3078 _dbus_assert (type == DBUS_TYPE_BYTE);
3080 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
3081 pos, NULL, value, len))
3088 * Returns the boolean array that the iterator may point to. Note that
3089 * you need to check that the iterator points to an array of the
3090 * correct type prior to using this function.
3092 * @param iter the iterator
3093 * @param value return location for the array
3094 * @param len return location for the array length
3095 * @returns #TRUE on success
3098 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
3099 unsigned char **value,
3102 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3105 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3107 pos = dbus_message_iter_get_data_start (real, &type);
3109 _dbus_assert (type == DBUS_TYPE_ARRAY);
3111 type = iter_get_array_type (real, NULL);
3113 _dbus_assert (type == DBUS_TYPE_BOOLEAN);
3115 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
3116 pos, NULL, value, len))
3123 * Returns the 32 bit signed integer array that the iterator may point
3124 * to. Note that you need to check that the iterator points to an
3125 * array of the correct type prior to using this function.
3127 * @param iter the iterator
3128 * @param value return location for the array
3129 * @param len return location for the array length
3130 * @returns #TRUE on success
3133 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
3134 dbus_int32_t **value,
3137 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3140 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3142 pos = dbus_message_iter_get_data_start (real, &type);
3144 _dbus_assert (type == DBUS_TYPE_ARRAY);
3146 type = iter_get_array_type (real, NULL);
3148 _dbus_assert (type == DBUS_TYPE_INT32);
3150 if (!_dbus_demarshal_int32_array (&real->message->body, real->message->byte_order,
3151 pos, NULL, value, len))
3158 * Returns the 32 bit unsigned integer array that the iterator may point
3159 * to. Note that you need to check that the iterator points to an
3160 * array of the correct type prior to using this function.
3162 * @param iter the iterator
3163 * @param value return location for the array
3164 * @param len return location for the array length
3165 * @returns #TRUE on success
3168 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
3169 dbus_uint32_t **value,
3172 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3175 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3177 pos = dbus_message_iter_get_data_start (real, &type);
3179 _dbus_assert (type == DBUS_TYPE_ARRAY);
3181 type = iter_get_array_type (real, NULL);
3182 _dbus_assert (type == DBUS_TYPE_UINT32);
3184 if (!_dbus_demarshal_uint32_array (&real->message->body, real->message->byte_order,
3185 pos, NULL, value, len))
3191 #ifdef DBUS_HAVE_INT64
3194 * Returns the 64 bit signed integer array that the iterator may point
3195 * to. Note that you need to check that the iterator points to an
3196 * array of the correct type prior to using this function.
3198 * This function only exists if #DBUS_HAVE_INT64 is defined.
3200 * @param iter the iterator
3201 * @param value return location for the array
3202 * @param len return location for the array length
3203 * @returns #TRUE on success
3206 dbus_message_iter_get_int64_array (DBusMessageIter *iter,
3207 dbus_int64_t **value,
3210 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3213 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3215 pos = dbus_message_iter_get_data_start (real, &type);
3217 _dbus_assert (type == DBUS_TYPE_ARRAY);
3219 type = iter_get_array_type (real, NULL);
3221 _dbus_assert (type == DBUS_TYPE_INT64);
3223 if (!_dbus_demarshal_int64_array (&real->message->body, real->message->byte_order,
3224 pos, NULL, value, len))
3231 * Returns the 64 bit unsigned integer array that the iterator may point
3232 * to. Note that you need to check that the iterator points to an
3233 * array of the correct type prior to using this function.
3235 * This function only exists if #DBUS_HAVE_INT64 is defined.
3237 * @param iter the iterator
3238 * @param value return location for the array
3239 * @param len return location for the array length
3240 * @returns #TRUE on success
3243 dbus_message_iter_get_uint64_array (DBusMessageIter *iter,
3244 dbus_uint64_t **value,
3247 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3250 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3252 pos = dbus_message_iter_get_data_start (real, &type);
3254 _dbus_assert (type == DBUS_TYPE_ARRAY);
3256 type = iter_get_array_type (real, NULL);
3257 _dbus_assert (type == DBUS_TYPE_UINT64);
3259 if (!_dbus_demarshal_uint64_array (&real->message->body, real->message->byte_order,
3260 pos, NULL, value, len))
3266 #endif /* DBUS_HAVE_INT64 */
3269 * Returns the double array that the iterator may point to. Note that
3270 * you need to check that the iterator points to an array of the
3271 * correct type prior to using this function.
3273 * @param iter the iterator
3274 * @param value return location for the array
3275 * @param len return location for the array length
3276 * @returns #TRUE on success
3279 dbus_message_iter_get_double_array (DBusMessageIter *iter,
3283 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3286 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3288 pos = dbus_message_iter_get_data_start (real, &type);
3290 _dbus_assert (type == DBUS_TYPE_ARRAY);
3292 type = iter_get_array_type (real, NULL);
3293 _dbus_assert (type == DBUS_TYPE_DOUBLE);
3295 if (!_dbus_demarshal_double_array (&real->message->body, real->message->byte_order,
3296 pos, NULL, value, len))
3303 * Returns the string array that the iterator may point to.
3304 * Note that you need to check that the iterator points
3305 * to a string array prior to using this function.
3307 * The returned value is a #NULL-terminated array of strings.
3308 * Each string is a separate malloc block, and the array
3309 * itself is a malloc block. You can free this type of
3310 * string array with dbus_free_string_array().
3312 * @param iter the iterator
3313 * @param value return location for string values
3314 * @param len return location for length of byte array
3315 * @returns #TRUE on success
3318 dbus_message_iter_get_string_array (DBusMessageIter *iter,
3322 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3325 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3327 pos = dbus_message_iter_get_data_start (real, &type);
3329 _dbus_assert (type == DBUS_TYPE_ARRAY);
3331 type = iter_get_array_type (real, NULL);
3332 _dbus_assert (type == DBUS_TYPE_STRING);
3334 if (!_dbus_demarshal_string_array (&real->message->body, real->message->byte_order,
3335 pos, NULL, value, len))
3343 * @todo FIXME to implement this _dbus_demarshal_object_path_array()
3344 * needs implementing
3346 * Returns the object path array that the iterator may point to.
3347 * Note that you need to check that the iterator points
3348 * to an object path array prior to using this function.
3350 * The returned value is a #NULL-terminated array of strings.
3351 * Each string is a separate malloc block, and the array
3352 * itself is a malloc block. You can free this type of
3353 * array with dbus_free_string_array().
3355 * @param iter the iterator
3356 * @param value return location for string values
3357 * @param len return location for length of byte array
3358 * @returns #TRUE on success
3361 dbus_message_iter_get_object_path_array (DBusMessageIter *iter,
3365 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3368 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3370 pos = dbus_message_iter_get_data_start (real, &type);
3372 _dbus_assert (type == DBUS_TYPE_ARRAY);
3374 type = iter_get_array_type (real, NULL);
3375 _dbus_assert (type == DBUS_TYPE_OBJECT_PATH);
3377 if (!_dbus_demarshal_object_path_array (&real->message->body, real->message->byte_order,
3378 pos, NULL, value, len))
3386 * Returns the key name fot the dict entry that an iterator
3387 * may point to. Note that you need to check that the iterator
3388 * points to a dict entry before using this function.
3390 * @see dbus_message_iter_init_dict_iterator
3391 * @param iter the message iter
3392 * @returns the key name
3395 dbus_message_iter_get_dict_key (DBusMessageIter *iter)
3397 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3399 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
3401 _dbus_assert (real->type == DBUS_MESSAGE_ITER_TYPE_DICT);
3403 return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
3408 * Initializes a DBusMessageIter pointing to the end of the
3409 * message. This iterator can be used to append data to the
3412 * @param message the message
3413 * @param iter pointer to an iterator to initialize
3416 dbus_message_append_iter_init (DBusMessage *message,
3417 DBusMessageIter *iter)
3419 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3421 _dbus_return_if_fail (message != NULL);
3422 _dbus_return_if_fail (iter != NULL);
3424 real->message = message;
3425 real->parent_iter = NULL;
3426 real->changed_stamp = message->changed_stamp;
3428 real->type = DBUS_MESSAGE_ITER_TYPE_MESSAGE;
3429 real->end = _dbus_string_get_length (&real->message->body);
3430 real->pos = real->end;
3432 real->container_length_pos = 0;
3433 real->wrote_dict_key = 0;
3436 #ifndef DBUS_DISABLE_CHECKS
3438 dbus_message_iter_append_check (DBusMessageRealIter *iter)
3442 _dbus_warn ("dbus iterator check failed: NULL iterator\n");
3446 if (iter->message->locked)
3448 _dbus_warn ("dbus iterator check failed: message is locked (has already been sent)\n");
3452 if (iter->changed_stamp != iter->message->changed_stamp)
3454 _dbus_warn ("dbus iterator check failed: invalid iterator, must re-initialize it after modifying the message");
3458 if (iter->pos != iter->end)
3460 _dbus_warn ("dbus iterator check failed: can only append at end of message");
3464 if (iter->pos != _dbus_string_get_length (&iter->message->body))
3466 _dbus_warn ("dbus iterator check failed: append pos not at end of message string");
3472 #endif /* DBUS_DISABLE_CHECKS */
3475 dbus_message_iter_append_type (DBusMessageRealIter *iter,
3481 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
3482 if (!_dbus_string_append_byte (&iter->message->signature, type))
3485 if (!_dbus_string_append_byte (&iter->message->body, type))
3487 _dbus_string_shorten (&iter->message->signature, 1);
3492 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
3493 data = _dbus_string_get_const_data_len (&iter->message->body,
3494 iter->array_type_pos, 1);
3497 _dbus_warn ("Appended element of wrong type for array\n");
3502 case DBUS_MESSAGE_ITER_TYPE_DICT:
3503 if (!iter->wrote_dict_key)
3505 _dbus_warn ("Appending dict data before key name\n");
3509 if (!_dbus_string_append_byte (&iter->message->body, type))
3515 _dbus_assert_not_reached ("Invalid iter type");
3523 dbus_message_iter_update_after_change (DBusMessageRealIter *iter)
3525 iter->changed_stamp = iter->message->changed_stamp;
3527 /* Set new end of iter */
3528 iter->end = _dbus_string_get_length (&iter->message->body);
3529 iter->pos = iter->end;
3531 /* Set container length */
3532 if (iter->type == DBUS_MESSAGE_ITER_TYPE_DICT ||
3533 (iter->type == DBUS_MESSAGE_ITER_TYPE_ARRAY && iter->array_type_done))
3534 _dbus_marshal_set_uint32 (&iter->message->body,
3535 iter->message->byte_order,
3536 iter->container_length_pos,
3537 iter->end - iter->container_start);
3539 if (iter->parent_iter)
3540 dbus_message_iter_update_after_change (iter->parent_iter);
3544 dbus_message_iter_append_done (DBusMessageRealIter *iter)
3546 iter->message->changed_stamp++;
3547 dbus_message_iter_update_after_change (iter);
3548 iter->wrote_dict_key = FALSE;
3552 * Appends a nil value to the message
3554 * @param iter an iterator pointing to the end of the message
3555 * @returns #TRUE on success
3558 dbus_message_iter_append_nil (DBusMessageIter *iter)
3560 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3562 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3564 if (!dbus_message_iter_append_type (real, DBUS_TYPE_NIL))
3567 dbus_message_iter_append_done (real);
3573 * Appends a boolean value to the message
3575 * @param iter an iterator pointing to the end of the message
3576 * @param value the boolean value
3577 * @returns #TRUE on success
3580 dbus_message_iter_append_boolean (DBusMessageIter *iter,
3583 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3585 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3587 if (!dbus_message_iter_append_type (real, DBUS_TYPE_BOOLEAN))
3590 if (!_dbus_string_append_byte (&real->message->body, (value != FALSE)))
3592 _dbus_string_set_length (&real->message->body, real->pos);
3596 dbus_message_iter_append_done (real);
3602 * Appends a byte to the message
3604 * @param iter an iterator pointing to the end of the message
3605 * @param value the byte value
3606 * @returns #TRUE on success
3609 dbus_message_iter_append_byte (DBusMessageIter *iter,
3610 unsigned char value)
3612 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3614 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3616 if (!dbus_message_iter_append_type (real, DBUS_TYPE_BYTE))
3619 if (!_dbus_string_append_byte (&real->message->body, value))
3621 _dbus_string_set_length (&real->message->body, real->pos);
3625 dbus_message_iter_append_done (real);
3632 * Appends a 32 bit signed integer to the message.
3634 * @param iter an iterator pointing to the end of the message
3635 * @param value the integer value
3636 * @returns #TRUE on success
3639 dbus_message_iter_append_int32 (DBusMessageIter *iter,
3642 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3644 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3646 if (!dbus_message_iter_append_type (real, DBUS_TYPE_INT32))
3649 if (!_dbus_marshal_int32 (&real->message->body, real->message->byte_order, value))
3651 _dbus_string_set_length (&real->message->body, real->pos);
3655 dbus_message_iter_append_done (real);
3661 * Appends a 32 bit unsigned integer to the message.
3663 * @param iter an iterator pointing to the end of the message
3664 * @param value the integer value
3665 * @returns #TRUE on success
3668 dbus_message_iter_append_uint32 (DBusMessageIter *iter,
3669 dbus_uint32_t value)
3671 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3673 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3675 if (!dbus_message_iter_append_type (real, DBUS_TYPE_UINT32))
3678 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, value))
3680 _dbus_string_set_length (&real->message->body, real->pos);
3684 dbus_message_iter_append_done (real);
3689 #ifdef DBUS_HAVE_INT64
3692 * Appends a 64 bit signed integer to the message.
3694 * This function only exists if #DBUS_HAVE_INT64 is defined.
3696 * @param iter an iterator pointing to the end of the message
3697 * @param value the integer value
3698 * @returns #TRUE on success
3701 dbus_message_iter_append_int64 (DBusMessageIter *iter,
3704 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3706 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3708 if (!dbus_message_iter_append_type (real, DBUS_TYPE_INT64))
3711 if (!_dbus_marshal_int64 (&real->message->body, real->message->byte_order, value))
3713 _dbus_string_set_length (&real->message->body, real->pos);
3717 dbus_message_iter_append_done (real);
3723 * Appends a 64 bit unsigned integer to the message.
3725 * This function only exists if #DBUS_HAVE_INT64 is defined.
3727 * @param iter an iterator pointing to the end of the message
3728 * @param value the integer value
3729 * @returns #TRUE on success
3732 dbus_message_iter_append_uint64 (DBusMessageIter *iter,
3733 dbus_uint64_t value)
3735 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3737 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3739 if (!dbus_message_iter_append_type (real, DBUS_TYPE_UINT64))
3742 if (!_dbus_marshal_uint64 (&real->message->body, real->message->byte_order, value))
3744 _dbus_string_set_length (&real->message->body, real->pos);
3748 dbus_message_iter_append_done (real);
3753 #endif /* DBUS_HAVE_INT64 */
3756 * Appends a double value to the message.
3758 * @param iter an iterator pointing to the end of the message
3759 * @param value the double value
3760 * @returns #TRUE on success
3763 dbus_message_iter_append_double (DBusMessageIter *iter,
3766 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3768 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3770 if (!dbus_message_iter_append_type (real, DBUS_TYPE_DOUBLE))
3773 if (!_dbus_marshal_double (&real->message->body, real->message->byte_order, value))
3775 _dbus_string_set_length (&real->message->body, real->pos);
3779 dbus_message_iter_append_done (real);
3785 * Appends a UTF-8 string to the message.
3787 * @todo add return_val_if_fail(UTF-8 is valid)
3789 * @param iter an iterator pointing to the end of the message
3790 * @param value the string
3791 * @returns #TRUE on success
3794 dbus_message_iter_append_string (DBusMessageIter *iter,
3797 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3799 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3801 if (!dbus_message_iter_append_type (real, DBUS_TYPE_STRING))
3804 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
3806 _dbus_string_set_length (&real->message->body, real->pos);
3810 dbus_message_iter_append_done (real);
3816 * Appends a custom type data chunk to the message. A custom
3817 * type is simply an arbitrary UTF-8 string used as a type
3818 * tag, plus an array of arbitrary bytes to be interpreted
3819 * according to the type tag.
3821 * @param iter an iterator pointing to the end of the message
3822 * @param name the name of the type
3823 * @param data the binary data used to store the value
3824 * @param len the length of the binary data in bytes
3825 * @returns #TRUE on success
3828 dbus_message_iter_append_custom (DBusMessageIter *iter,
3830 const unsigned char *data,
3833 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3835 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3837 if (!dbus_message_iter_append_type (real, DBUS_TYPE_CUSTOM))
3840 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, name))
3842 _dbus_string_set_length (&real->message->body, real->pos);
3846 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, data, len))
3848 _dbus_string_set_length (&real->message->body, real->pos);
3852 dbus_message_iter_append_done (real);
3859 * Appends a dict key name to the message. The iterator used
3860 * must point to a dict.
3862 * @param iter an iterator pointing to the end of the message
3863 * @param value the string
3864 * @returns #TRUE on success
3867 dbus_message_iter_append_dict_key (DBusMessageIter *iter,
3870 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3872 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3873 _dbus_assert (real->type == DBUS_MESSAGE_ITER_TYPE_DICT);
3875 if (real->wrote_dict_key)
3877 _dbus_warn ("Appending multiple dict key names\n");
3881 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
3886 dbus_message_iter_append_done (real);
3887 real->wrote_dict_key = TRUE;
3893 array_iter_type_mark_done (DBusMessageRealIter *iter)
3897 if (iter->type == DBUS_MESSAGE_ITER_TYPE_ARRAY)
3898 array_iter_type_mark_done (iter->parent_iter);
3902 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&iter->message->body),
3903 sizeof (dbus_uint32_t));
3905 /* Empty length for now, backfill later */
3906 if (!_dbus_marshal_uint32 (&iter->message->body, iter->message->byte_order, 0))
3908 _dbus_string_set_length (&iter->message->body, iter->pos);
3912 iter->container_start = _dbus_string_get_length (&iter->message->body);
3913 iter->container_length_pos = len_pos;
3914 iter->array_type_done = TRUE;
3920 append_array_type (DBusMessageRealIter *real,
3922 dbus_bool_t *array_type_done,
3923 int *array_type_pos)
3925 int existing_element_type;
3927 if (!dbus_message_iter_append_type (real, DBUS_TYPE_ARRAY))
3930 if (real->type == DBUS_MESSAGE_ITER_TYPE_ARRAY &&
3931 real->array_type_done)
3933 existing_element_type = iter_get_array_type (real, array_type_pos);
3934 if (existing_element_type != element_type)
3936 _dbus_warn ("Appending array of %s, when expecting array of %s\n",
3937 _dbus_type_to_string (element_type),
3938 _dbus_type_to_string (existing_element_type));
3939 _dbus_string_set_length (&real->message->body, real->pos);
3942 if (array_type_done != NULL)
3943 *array_type_done = TRUE;
3947 if (array_type_pos != NULL)
3948 *array_type_pos = _dbus_string_get_length (&real->message->body);
3951 if (!_dbus_string_append_byte (&real->message->signature, element_type))
3953 _dbus_string_set_length (&real->message->body, real->pos);
3957 /* Append element type */
3958 if (!_dbus_string_append_byte (&real->message->body, element_type))
3960 _dbus_string_shorten (&real->message->signature, 1);
3961 _dbus_string_set_length (&real->message->body, real->pos);
3965 if (array_type_done != NULL)
3966 *array_type_done = element_type != DBUS_TYPE_ARRAY;
3968 if (element_type != DBUS_TYPE_ARRAY &&
3969 !array_iter_type_mark_done (real))
3971 _dbus_string_shorten (&real->message->signature, 1);
3980 * Appends an array to the message and initializes an iterator that
3981 * can be used to append to the array.
3983 * @param iter an iterator pointing to the end of the message
3984 * @param array_iter pointer to an iter that will be initialized
3985 * @param element_type the type of the array elements
3986 * @returns #TRUE on success
3989 dbus_message_iter_append_array (DBusMessageIter *iter,
3990 DBusMessageIter *array_iter,
3993 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3994 DBusMessageRealIter *array_real = (DBusMessageRealIter *)array_iter;
3997 dbus_bool_t array_type_done;
3999 if (element_type == DBUS_TYPE_NIL)
4001 _dbus_warn ("Can't create NIL arrays\n");
4005 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4007 if (!append_array_type (real, element_type, &array_type_done, &array_type_pos))
4010 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
4012 if (array_type_done)
4014 /* Empty length for now, backfill later */
4015 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
4017 _dbus_string_set_length (&real->message->body, real->pos);
4022 array_real->parent_iter = real;
4023 array_real->message = real->message;
4024 array_real->changed_stamp = real->message->changed_stamp;
4026 array_real->type = DBUS_MESSAGE_ITER_TYPE_ARRAY;
4027 array_real->pos = _dbus_string_get_length (&real->message->body);
4028 array_real->end = array_real->end;
4030 array_real->container_start = array_real->pos;
4031 array_real->container_length_pos = len_pos;
4032 array_real->wrote_dict_key = 0;
4033 array_real->array_type_done = array_type_done;
4034 array_real->array_type_pos = array_type_pos;
4036 dbus_message_iter_append_done (array_real);
4042 * Appends a dict to the message and initializes an iterator that
4043 * can be used to append to the dict.
4045 * @param iter an iterator pointing to the end of the message
4046 * @param dict_iter pointer to an iter that will be initialized
4047 * @returns #TRUE on success
4050 dbus_message_iter_append_dict (DBusMessageIter *iter,
4051 DBusMessageIter *dict_iter)
4053 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4054 DBusMessageRealIter *dict_real = (DBusMessageRealIter *)dict_iter;
4057 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4059 if (!dbus_message_iter_append_type (real, DBUS_TYPE_DICT))
4062 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
4064 /* Empty length for now, backfill later */
4065 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
4067 _dbus_string_set_length (&real->message->body, real->pos);
4071 dict_real->parent_iter = real;
4072 dict_real->message = real->message;
4073 dict_real->changed_stamp = real->message->changed_stamp;
4075 dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
4076 dict_real->pos = _dbus_string_get_length (&real->message->body);
4077 dict_real->end = dict_real->end;
4079 dict_real->container_start = dict_real->pos;
4080 dict_real->container_length_pos = len_pos;
4081 dict_real->wrote_dict_key = 0;
4083 dbus_message_iter_append_done (dict_real);
4085 real->wrote_dict_key = FALSE;
4092 * Appends a boolean array to the message.
4094 * @param iter an iterator pointing to the end of the message
4095 * @param value the array
4096 * @param len the length of the array
4097 * @returns #TRUE on success
4100 dbus_message_iter_append_boolean_array (DBusMessageIter *iter,
4101 unsigned const char *value,
4104 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4106 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4108 if (!append_array_type (real, DBUS_TYPE_BOOLEAN, NULL, NULL))
4111 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, value, len))
4113 _dbus_string_set_length (&real->message->body, real->pos);
4117 dbus_message_iter_append_done (real);
4123 * Appends a 32 bit signed integer array to the message.
4125 * @param iter an iterator pointing to the end of the message
4126 * @param value the array
4127 * @param len the length of the array
4128 * @returns #TRUE on success
4131 dbus_message_iter_append_int32_array (DBusMessageIter *iter,
4132 const dbus_int32_t *value,
4135 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4137 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4139 if (!append_array_type (real, DBUS_TYPE_INT32, NULL, NULL))
4142 if (!_dbus_marshal_int32_array (&real->message->body, real->message->byte_order, value, len))
4144 _dbus_string_set_length (&real->message->body, real->pos);
4148 dbus_message_iter_append_done (real);
4154 * Appends a 32 bit unsigned integer array to the message.
4156 * @param iter an iterator pointing to the end of the message
4157 * @param value the array
4158 * @param len the length of the array
4159 * @returns #TRUE on success
4162 dbus_message_iter_append_uint32_array (DBusMessageIter *iter,
4163 const dbus_uint32_t *value,
4166 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4168 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4170 if (!append_array_type (real, DBUS_TYPE_UINT32, NULL, NULL))
4173 if (!_dbus_marshal_uint32_array (&real->message->body, real->message->byte_order, value, len))
4175 _dbus_string_set_length (&real->message->body, real->pos);
4179 dbus_message_iter_append_done (real);
4184 #ifdef DBUS_HAVE_INT64
4187 * Appends a 64 bit signed integer array to the message.
4189 * This function only exists if #DBUS_HAVE_INT64 is defined.
4191 * @param iter an iterator pointing to the end of the message
4192 * @param value the array
4193 * @param len the length of the array
4194 * @returns #TRUE on success
4197 dbus_message_iter_append_int64_array (DBusMessageIter *iter,
4198 const dbus_int64_t *value,
4201 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4203 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4205 if (!append_array_type (real, DBUS_TYPE_INT64, NULL, NULL))
4208 if (!_dbus_marshal_int64_array (&real->message->body, real->message->byte_order, value, len))
4210 _dbus_string_set_length (&real->message->body, real->pos);
4214 dbus_message_iter_append_done (real);
4220 * Appends a 64 bit unsigned integer array to the message.
4222 * This function only exists if #DBUS_HAVE_INT64 is defined.
4224 * @param iter an iterator pointing to the end of the message
4225 * @param value the array
4226 * @param len the length of the array
4227 * @returns #TRUE on success
4230 dbus_message_iter_append_uint64_array (DBusMessageIter *iter,
4231 const dbus_uint64_t *value,
4234 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4236 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4238 if (!append_array_type (real, DBUS_TYPE_UINT64, NULL, NULL))
4241 if (!_dbus_marshal_uint64_array (&real->message->body, real->message->byte_order, value, len))
4243 _dbus_string_set_length (&real->message->body, real->pos);
4247 dbus_message_iter_append_done (real);
4251 #endif /* DBUS_HAVE_INT64 */
4254 * Appends a double array to the message.
4256 * @param iter an iterator pointing to the end of the message
4257 * @param value the array
4258 * @param len the length of the array
4259 * @returns #TRUE on success
4262 dbus_message_iter_append_double_array (DBusMessageIter *iter,
4263 const double *value,
4266 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4268 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4270 if (!append_array_type (real, DBUS_TYPE_DOUBLE, NULL, NULL))
4273 if (!_dbus_marshal_double_array (&real->message->body, real->message->byte_order, value, len))
4275 _dbus_string_set_length (&real->message->body, real->pos);
4279 dbus_message_iter_append_done (real);
4285 * Appends a byte array to the message.
4287 * @param iter an iterator pointing to the end of the message
4288 * @param value the array
4289 * @param len the length of the array
4290 * @returns #TRUE on success
4293 dbus_message_iter_append_byte_array (DBusMessageIter *iter,
4294 unsigned const char *value,
4297 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4299 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4301 if (!append_array_type (real, DBUS_TYPE_BYTE, NULL, NULL))
4304 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, value, len))
4306 _dbus_string_set_length (&real->message->body, real->pos);
4310 dbus_message_iter_append_done (real);
4316 * Appends a string array to the message.
4318 * @param iter an iterator pointing to the end of the message
4319 * @param value the array
4320 * @param len the length of the array
4321 * @returns #TRUE on success
4324 dbus_message_iter_append_string_array (DBusMessageIter *iter,
4328 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4330 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4332 if (!append_array_type (real, DBUS_TYPE_STRING, NULL, NULL))
4335 if (!_dbus_marshal_string_array (&real->message->body, real->message->byte_order, value, len))
4337 _dbus_string_set_length (&real->message->body, real->pos);
4341 dbus_message_iter_append_done (real);
4347 * Sets the message sender.
4349 * @param message the message
4350 * @param sender the sender
4351 * @returns #FALSE if not enough memory
4354 dbus_message_set_sender (DBusMessage *message,
4357 _dbus_return_val_if_fail (message != NULL, FALSE);
4358 _dbus_return_val_if_fail (!message->locked, FALSE);
4360 return set_string_field (message,
4361 DBUS_HEADER_FIELD_SENDER_SERVICE,
4367 * Sets a flag indicating that the message does not want a reply; if
4368 * this flag is set, the other end of the connection may (but is not
4369 * required to) optimize by not sending method return or error
4370 * replies. If this flag is set, there is no way to know whether the
4371 * message successfully arrived at the remote end.
4373 * @param message the message
4374 * @param no_reply #TRUE if no reply is desired
4377 dbus_message_set_no_reply (DBusMessage *message,
4378 dbus_bool_t no_reply)
4382 _dbus_return_if_fail (message != NULL);
4383 _dbus_return_if_fail (!message->locked);
4385 header = _dbus_string_get_data_len (&message->header, FLAGS_OFFSET, 1);
4388 *header |= DBUS_HEADER_FLAG_NO_REPLY_EXPECTED;
4390 *header &= ~DBUS_HEADER_FLAG_NO_REPLY_EXPECTED;
4394 * Returns #TRUE if the message does not expect
4397 * @param message the message
4398 * @returns #TRUE if the message sender isn't waiting for a reply
4401 dbus_message_get_no_reply (DBusMessage *message)
4405 _dbus_return_val_if_fail (message != NULL, FALSE);
4407 header = _dbus_string_get_const_data_len (&message->header, FLAGS_OFFSET, 1);
4409 return (*header & DBUS_HEADER_FLAG_NO_REPLY_EXPECTED) != 0;
4413 * Gets the service which originated this message,
4414 * or #NULL if unknown or inapplicable.
4416 * @param message the message
4417 * @returns the service name or #NULL
4420 dbus_message_get_sender (DBusMessage *message)
4422 _dbus_return_val_if_fail (message != NULL, NULL);
4424 return get_string_field (message,
4425 DBUS_HEADER_FIELD_SENDER_SERVICE,
4430 * Gets the type signature of the message, i.e. the arguments in the
4431 * message payload. The signature includes only "in" arguments for
4432 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
4433 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
4434 * what you might expect (it does not include the signature of the
4435 * entire C++-style method).
4437 * The signature is a string made up of type codes such
4438 * as #DBUS_TYPE_STRING. The string is terminated with nul
4439 * (nul is also the value of #DBUS_TYPE_INVALID).
4441 * @param message the message
4442 * @returns the type signature
4445 dbus_message_get_signature (DBusMessage *message)
4447 _dbus_return_val_if_fail (message != NULL, NULL);
4449 return _dbus_string_get_const_data (&message->signature);
4453 _dbus_message_has_type_interface_member (DBusMessage *message,
4455 const char *interface,
4460 _dbus_assert (message != NULL);
4461 _dbus_assert (interface != NULL);
4462 _dbus_assert (method != NULL);
4464 if (dbus_message_get_type (message) != type)
4467 /* Optimize by checking the short method name first
4468 * instead of the longer interface name
4471 n = dbus_message_get_member (message);
4473 if (n && strcmp (n, method) == 0)
4475 n = dbus_message_get_interface (message);
4477 if (n && strcmp (n, interface) == 0)
4485 * Checks whether the message is a method call with the given
4486 * interface and member fields. If the message is not
4487 * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or member field,
4490 * @param message the message
4491 * @param interface the name to check (must not be #NULL)
4492 * @param method the name to check (must not be #NULL)
4494 * @returns #TRUE if the message is the specified method call
4497 dbus_message_is_method_call (DBusMessage *message,
4498 const char *interface,
4501 _dbus_return_val_if_fail (message != NULL, FALSE);
4502 _dbus_return_val_if_fail (interface != NULL, FALSE);
4503 _dbus_return_val_if_fail (method != NULL, FALSE);
4505 return _dbus_message_has_type_interface_member (message,
4506 DBUS_MESSAGE_TYPE_METHOD_CALL,
4511 * Checks whether the message is a signal with the given
4512 * interface and member fields. If the message is not
4513 * #DBUS_MESSAGE_TYPE_SIGNAL, or has a different interface or member field,
4516 * @param message the message
4517 * @param interface the name to check (must not be #NULL)
4518 * @param signal_name the name to check (must not be #NULL)
4520 * @returns #TRUE if the message is the specified signal
4523 dbus_message_is_signal (DBusMessage *message,
4524 const char *interface,
4525 const char *signal_name)
4527 _dbus_return_val_if_fail (message != NULL, FALSE);
4528 _dbus_return_val_if_fail (interface != NULL, FALSE);
4529 _dbus_return_val_if_fail (signal_name != NULL, FALSE);
4531 return _dbus_message_has_type_interface_member (message,
4532 DBUS_MESSAGE_TYPE_SIGNAL,
4533 interface, signal_name);
4537 * Checks whether the message is an error reply with the given error
4538 * name. If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
4539 * different name, returns #FALSE.
4541 * @param message the message
4542 * @param error_name the name to check (must not be #NULL)
4544 * @returns #TRUE if the message is the specified error
4547 dbus_message_is_error (DBusMessage *message,
4548 const char *error_name)
4552 _dbus_return_val_if_fail (message != NULL, FALSE);
4553 _dbus_return_val_if_fail (error_name != NULL, FALSE);
4555 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
4558 n = dbus_message_get_error_name (message);
4560 if (n && strcmp (n, error_name) == 0)
4567 * Checks whether the message was sent to the given service. If the
4568 * message has no service specified or has a different name, returns
4571 * @param message the message
4572 * @param service the service to check (must not be #NULL)
4574 * @returns #TRUE if the message has the given destination service
4577 dbus_message_has_destination (DBusMessage *message,
4578 const char *service)
4582 _dbus_return_val_if_fail (message != NULL, FALSE);
4583 _dbus_return_val_if_fail (service != NULL, FALSE);
4585 s = dbus_message_get_destination (message);
4587 if (s && strcmp (s, service) == 0)
4594 * Checks whether the message has the given service as its sender. If
4595 * the message has no sender specified or has a different sender,
4596 * returns #FALSE. Note that if a peer application owns multiple
4597 * services, its messages will have only one of those services as the
4598 * sender (usually the base service). So you can't use this
4599 * function to prove the sender didn't own service Foo, you can
4600 * only use it to prove that it did.
4602 * @param message the message
4603 * @param service the service to check (must not be #NULL)
4605 * @returns #TRUE if the message has the given origin service
4608 dbus_message_has_sender (DBusMessage *message,
4609 const char *service)
4613 _dbus_return_val_if_fail (message != NULL, FALSE);
4614 _dbus_return_val_if_fail (service != NULL, FALSE);
4616 s = dbus_message_get_sender (message);
4618 if (s && strcmp (s, service) == 0)
4625 * Checks whether the message has the given signature;
4626 * see dbus_message_get_signature() for more details on
4627 * what the signature looks like.
4629 * @param message the message
4630 * @param signature typecode array
4631 * @returns #TRUE if message has the given signature
4634 dbus_message_has_signature (DBusMessage *message,
4635 const char *signature)
4637 _dbus_return_val_if_fail (message != NULL, FALSE);
4638 _dbus_return_val_if_fail (signature != NULL, FALSE);
4640 return _dbus_string_equal_c_str (&message->signature, signature);
4644 * Sets a #DBusError based on the contents of the given
4645 * message. The error is only set if the message
4646 * is an error message, as in DBUS_MESSAGE_TYPE_ERROR.
4647 * The name of the error is set to the name of the message,
4648 * and the error message is set to the first argument
4649 * if the argument exists and is a string.
4651 * The return value indicates whether the error was set (the error is
4652 * set if and only if the message is an error message).
4653 * So you can check for an error reply and convert it to DBusError
4656 * @param error the error to set
4657 * @param message the message to set it from
4658 * @returns #TRUE if dbus_message_get_is_error() returns #TRUE for the message
4661 dbus_set_error_from_message (DBusError *error,
4662 DBusMessage *message)
4666 _dbus_return_val_if_fail (message != NULL, FALSE);
4667 _dbus_return_val_if_error_is_set (error, FALSE);
4669 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
4673 dbus_message_get_args (message, NULL,
4674 DBUS_TYPE_STRING, &str,
4677 dbus_set_error (error, dbus_message_get_error_name (message),
4678 str ? "%s" : NULL, str);
4688 * @addtogroup DBusMessageInternals
4693 * @typedef DBusMessageLoader
4695 * The DBusMessageLoader object encapsulates the process of converting
4696 * a byte stream into a series of DBusMessage. It buffers the incoming
4697 * bytes as efficiently as possible, and generates a queue of
4698 * messages. DBusMessageLoader is typically used as part of a
4699 * DBusTransport implementation. The DBusTransport then hands off
4700 * the loaded messages to a DBusConnection, making the messages
4701 * visible to the application.
4703 * @todo write tests for break-loader that a) randomly delete header
4704 * fields and b) set string fields to zero-length and other funky
4709 /* we definitely use signed ints for sizes, so don't exceed
4710 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
4711 * over 128M is pretty nuts anyhow.
4715 * The maximum sane message size.
4717 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
4720 * Implementation details of DBusMessageLoader.
4721 * All members are private.
4723 struct DBusMessageLoader
4725 int refcount; /**< Reference count. */
4727 DBusString data; /**< Buffered data */
4729 DBusList *messages; /**< Complete messages. */
4731 long max_message_size; /**< Maximum size of a message */
4733 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
4735 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
4739 * The initial buffer size of the message loader.
4741 * @todo this should be based on min header size plus some average
4742 * body size, or something. Or rather, the min header size only, if we
4743 * want to try to read only the header, store that in a DBusMessage,
4744 * then read only the body and store that, etc., depends on
4745 * how we optimize _dbus_message_loader_get_buffer() and what
4746 * the exact message format is.
4748 #define INITIAL_LOADER_DATA_LEN 32
4751 * Creates a new message loader. Returns #NULL if memory can't
4754 * @returns new loader, or #NULL.
4757 _dbus_message_loader_new (void)
4759 DBusMessageLoader *loader;
4761 loader = dbus_new0 (DBusMessageLoader, 1);
4765 loader->refcount = 1;
4767 /* Try to cap message size at something that won't *totally* hose
4768 * the system if we have a couple of them.
4770 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
4772 if (!_dbus_string_init (&loader->data))
4778 /* preallocate the buffer for speed, ignore failure */
4779 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
4780 _dbus_string_set_length (&loader->data, 0);
4786 * Increments the reference count of the loader.
4788 * @param loader the loader.
4789 * @returns the loader
4792 _dbus_message_loader_ref (DBusMessageLoader *loader)
4794 loader->refcount += 1;
4800 * Decrements the reference count of the loader and finalizes the
4801 * loader when the count reaches zero.
4803 * @param loader the loader.
4806 _dbus_message_loader_unref (DBusMessageLoader *loader)
4808 loader->refcount -= 1;
4809 if (loader->refcount == 0)
4811 _dbus_list_foreach (&loader->messages,
4812 (DBusForeachFunction) dbus_message_unref,
4814 _dbus_list_clear (&loader->messages);
4815 _dbus_string_free (&loader->data);
4821 * Gets the buffer to use for reading data from the network. Network
4822 * data is read directly into an allocated buffer, which is then used
4823 * in the DBusMessage, to avoid as many extra memcpy's as possible.
4824 * The buffer must always be returned immediately using
4825 * _dbus_message_loader_return_buffer(), even if no bytes are
4826 * successfully read.
4828 * @todo this function can be a lot more clever. For example
4829 * it can probably always return a buffer size to read exactly
4830 * the body of the next message, thus avoiding any memory wastage
4833 * @todo we need to enforce a max length on strings in header fields.
4835 * @param loader the message loader.
4836 * @param buffer the buffer
4839 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
4840 DBusString **buffer)
4842 _dbus_assert (!loader->buffer_outstanding);
4844 *buffer = &loader->data;
4846 loader->buffer_outstanding = TRUE;
4850 * The smallest header size that can occur.
4851 * (It won't be valid)
4853 #define DBUS_MINIMUM_HEADER_SIZE 16
4856 decode_string_field (const DBusString *data,
4858 HeaderField *header_field,
4859 DBusString *field_data,
4863 int string_data_pos;
4865 _dbus_assert (header_field != NULL);
4866 _dbus_assert (field_data != NULL);
4868 if (header_field->name_offset >= 0)
4870 _dbus_verbose ("%s field provided twice\n",
4871 _dbus_header_field_to_string (field));
4875 if (type != DBUS_TYPE_STRING)
4877 _dbus_verbose ("%s field has wrong type %s\n",
4878 _dbus_header_field_to_string (field),
4879 _dbus_type_to_string (type));
4883 /* skip padding after typecode, skip string length;
4884 * we assume that the string arg has already been validated
4885 * for sanity and UTF-8
4887 string_data_pos = _DBUS_ALIGN_VALUE (pos, 4) + 4;
4888 _dbus_assert (string_data_pos < _dbus_string_get_length (data));
4890 _dbus_string_init_const (field_data,
4891 _dbus_string_get_const_data (data) + string_data_pos);
4893 header_field->name_offset = pos;
4894 header_field->value_offset = _DBUS_ALIGN_VALUE (pos, 4);
4897 _dbus_verbose ("Found field %s at offset %d\n",
4898 _dbus_header_field_to_string (field),
4899 header_field->value_offset);
4905 /* FIXME because the service/interface/member/error names are already
4906 * validated to be in the particular ASCII subset, UTF-8 validating
4907 * them could be skipped as a probably-interesting optimization.
4908 * The UTF-8 validation definitely shows up in profiles.
4911 decode_header_data (const DBusString *data,
4915 HeaderField fields[DBUS_HEADER_FIELD_LAST + 1],
4916 int *message_padding)
4918 DBusString field_data;
4924 if (header_len < 16)
4926 _dbus_verbose ("Header length %d is too short\n", header_len);
4931 while (i <= DBUS_HEADER_FIELD_LAST)
4933 fields[i].name_offset = -1;
4934 fields[i].value_offset = -1;
4939 while (pos < header_len)
4941 field = _dbus_string_get_byte (data, pos);
4942 if (field == DBUS_HEADER_FIELD_INVALID)
4943 break; /* Must be padding */
4946 if (!_dbus_marshal_validate_type (data, pos, &type, &pos))
4948 _dbus_verbose ("Failed to validate type of named header field pos = %d\n",
4953 if (!_dbus_marshal_validate_arg (data, byte_order, 0, type, -1, pos, &new_pos))
4955 _dbus_verbose ("Failed to validate argument to named header field pos = %d\n",
4960 if (new_pos > header_len)
4962 _dbus_verbose ("Named header field tries to extend beyond header length\n");
4968 case DBUS_HEADER_FIELD_SERVICE:
4969 if (!decode_string_field (data, field, &fields[field],
4970 &field_data, pos, type))
4973 if (!_dbus_string_validate_service (&field_data, 0,
4974 _dbus_string_get_length (&field_data)))
4976 _dbus_verbose ("service field has invalid content \"%s\"\n",
4977 _dbus_string_get_const_data (&field_data));
4982 case DBUS_HEADER_FIELD_INTERFACE:
4983 if (!decode_string_field (data, field, &fields[field],
4984 &field_data, pos, type))
4987 if (!_dbus_string_validate_interface (&field_data, 0,
4988 _dbus_string_get_length (&field_data)))
4990 _dbus_verbose ("interface field has invalid content \"%s\"\n",
4991 _dbus_string_get_const_data (&field_data));
4995 if (_dbus_string_equal_c_str (&field_data,
4996 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL))
4998 _dbus_verbose ("Message is on the local interface\n");
5003 case DBUS_HEADER_FIELD_MEMBER:
5004 if (!decode_string_field (data, field, &fields[field],
5005 &field_data, pos, type))
5008 if (!_dbus_string_validate_member (&field_data, 0,
5009 _dbus_string_get_length (&field_data)))
5011 _dbus_verbose ("member field has invalid content \"%s\"\n",
5012 _dbus_string_get_const_data (&field_data));
5017 case DBUS_HEADER_FIELD_ERROR_NAME:
5018 if (!decode_string_field (data, field, &fields[field],
5019 &field_data, pos, type))
5022 if (!_dbus_string_validate_error_name (&field_data, 0,
5023 _dbus_string_get_length (&field_data)))
5025 _dbus_verbose ("error-name field has invalid content \"%s\"\n",
5026 _dbus_string_get_const_data (&field_data));
5031 case DBUS_HEADER_FIELD_SENDER_SERVICE:
5032 if (!decode_string_field (data, field, &fields[field],
5033 &field_data, pos, type))
5036 if (!_dbus_string_validate_service (&field_data, 0,
5037 _dbus_string_get_length (&field_data)))
5039 _dbus_verbose ("sender-service field has invalid content \"%s\"\n",
5040 _dbus_string_get_const_data (&field_data));
5045 case DBUS_HEADER_FIELD_PATH:
5047 /* Path was already validated as part of standard
5048 * type validation, since there's an OBJECT_PATH
5052 if (fields[field].name_offset >= 0)
5054 _dbus_verbose ("path field provided twice\n");
5057 if (type != DBUS_TYPE_OBJECT_PATH)
5059 _dbus_verbose ("path field has wrong type\n");
5063 fields[field].name_offset = pos;
5064 fields[field].value_offset = _DBUS_ALIGN_VALUE (pos, 4);
5066 /* No forging signals from the local path */
5069 s = _dbus_string_get_const_data_len (data,
5070 fields[field].value_offset,
5071 _dbus_string_get_length (data) -
5072 fields[field].value_offset);
5073 if (strcmp (s, DBUS_PATH_ORG_FREEDESKTOP_LOCAL) == 0)
5075 _dbus_verbose ("Message is on the local path\n");
5080 _dbus_verbose ("Found path at offset %d\n",
5081 fields[field].value_offset);
5084 case DBUS_HEADER_FIELD_REPLY_SERIAL:
5085 if (fields[field].name_offset >= 0)
5087 _dbus_verbose ("reply field provided twice\n");
5091 if (type != DBUS_TYPE_UINT32)
5093 _dbus_verbose ("reply field has wrong type\n");
5097 fields[field].name_offset = pos;
5098 fields[field].value_offset = _DBUS_ALIGN_VALUE (pos, 4);
5100 _dbus_verbose ("Found reply serial %u at offset %d\n",
5101 _dbus_demarshal_uint32 (data,
5103 fields[field].value_offset,
5105 fields[field].value_offset);
5109 _dbus_verbose ("Ignoring an unknown header field: %d at offset %d\n",
5116 if (pos < header_len)
5118 /* Alignment padding, verify that it's nul */
5119 if ((header_len - pos) >= 8)
5121 _dbus_verbose ("too much header alignment padding\n");
5125 if (!_dbus_string_validate_nul (data,
5126 pos, (header_len - pos)))
5128 _dbus_verbose ("header alignment padding is not nul\n");
5133 /* Depending on message type, enforce presence of certain fields. */
5134 switch (message_type)
5136 case DBUS_MESSAGE_TYPE_SIGNAL:
5137 case DBUS_MESSAGE_TYPE_METHOD_CALL:
5138 if (fields[DBUS_HEADER_FIELD_PATH].value_offset < 0)
5140 _dbus_verbose ("No path field provided\n");
5143 /* FIXME make this optional, only for method calls */
5144 if (fields[DBUS_HEADER_FIELD_INTERFACE].value_offset < 0)
5146 _dbus_verbose ("No interface field provided\n");
5149 if (fields[DBUS_HEADER_FIELD_MEMBER].value_offset < 0)
5151 _dbus_verbose ("No member field provided\n");
5155 case DBUS_MESSAGE_TYPE_ERROR:
5156 if (fields[DBUS_HEADER_FIELD_ERROR_NAME].value_offset < 0)
5158 _dbus_verbose ("No error-name field provided\n");
5161 if (fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset < 0)
5163 _dbus_verbose ("No reply serial field provided in error\n");
5167 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
5168 if (fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset < 0)
5170 _dbus_verbose ("No reply serial field provided in method return\n");
5175 /* An unknown type, spec requires us to ignore it */
5179 if (message_padding)
5180 *message_padding = header_len - pos;
5186 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
5187 * indicating to the loader how many bytes of the buffer were filled
5188 * in. This function must always be called, even if no bytes were
5189 * successfully read.
5191 * @param loader the loader.
5192 * @param buffer the buffer.
5193 * @param bytes_read number of bytes that were read into the buffer.
5196 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
5200 _dbus_assert (loader->buffer_outstanding);
5201 _dbus_assert (buffer == &loader->data);
5203 loader->buffer_outstanding = FALSE;
5207 load_one_message (DBusMessageLoader *loader,
5213 DBusMessage *message;
5214 HeaderField fields[DBUS_HEADER_FIELD_LAST + 1];
5224 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
5227 if (!decode_header_data (&loader->data,
5228 header_len, byte_order,
5230 fields, &header_padding))
5232 _dbus_verbose ("Header was invalid\n");
5233 loader->corrupted = TRUE;
5237 next_arg = header_len;
5238 while (next_arg < (header_len + body_len))
5241 int prev = next_arg;
5243 if (!_dbus_marshal_validate_type (&loader->data, next_arg,
5246 _dbus_verbose ("invalid typecode at offset %d\n", prev);
5247 loader->corrupted = TRUE;
5251 if (!_dbus_marshal_validate_arg (&loader->data,
5258 _dbus_verbose ("invalid type data at %d, next_arg\n", next_arg);
5259 loader->corrupted = TRUE;
5263 _dbus_assert (next_arg > prev);
5266 if (next_arg > (header_len + body_len))
5268 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
5269 next_arg, header_len, body_len,
5270 header_len + body_len);
5271 loader->corrupted = TRUE;
5275 message = dbus_message_new_empty_header ();
5276 if (message == NULL)
5278 _dbus_verbose ("Failed to allocate empty message\n");
5283 message->byte_order = byte_order;
5284 message->header_padding = header_padding;
5286 /* Copy in the offsets we found */
5288 while (i <= DBUS_HEADER_FIELD_LAST)
5290 message->header_fields[i] = fields[i];
5294 if (!_dbus_list_append (&loader->messages, message))
5296 _dbus_verbose ("Failed to append new message to loader queue\n");
5301 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
5302 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
5304 _dbus_assert (_dbus_string_get_length (&loader->data) >=
5305 (header_len + body_len));
5307 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
5309 _dbus_verbose ("Failed to move header into new message\n");
5314 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
5316 _dbus_verbose ("Failed to move body into new message\n");
5322 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
5323 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
5325 /* Fill in caches (we checked the types of these fields
5328 message->reply_serial = get_uint_field (message,
5329 DBUS_HEADER_FIELD_REPLY_SERIAL);
5331 message->client_serial = _dbus_demarshal_uint32 (&message->header,
5332 message->byte_order,
5333 CLIENT_SERIAL_OFFSET,
5335 if (message->client_serial == 0 ||
5336 (message->header_fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset >= 0 && message->reply_serial == 0))
5338 _dbus_verbose ("client_serial = %d reply_serial = %d, one of these no good\n",
5339 message->client_serial,
5340 message->reply_serial);
5342 loader->corrupted = TRUE;
5346 /* Fill in signature (FIXME should do this during validation,
5347 * but I didn't want to spend time on it since we want to change
5348 * the wire format to contain the signature anyway)
5351 DBusMessageIter iter;
5353 dbus_message_iter_init (message, &iter);
5359 t = dbus_message_iter_get_arg_type (&iter);
5360 if (t == DBUS_TYPE_INVALID)
5363 if (!_dbus_string_append_byte (&message->signature,
5366 _dbus_verbose ("failed to append type byte to signature\n");
5371 if (t == DBUS_TYPE_ARRAY)
5373 DBusMessageIter child_iter;
5378 while (array_type == DBUS_TYPE_ARRAY)
5380 DBusMessageIter parent_iter = child_iter;
5381 dbus_message_iter_init_array_iterator (&parent_iter,
5385 if (!_dbus_string_append_byte (&message->signature,
5388 _dbus_verbose ("failed to append array type byte to signature\n");
5396 while (dbus_message_iter_next (&iter));
5399 _dbus_verbose ("Loaded message %p\n", message);
5401 _dbus_assert (!oom);
5402 _dbus_assert (!loader->corrupted);
5410 if (message != NULL)
5412 /* Put the data back so we can try again later if it was an OOM issue */
5413 if (_dbus_string_get_length (&message->body) > 0)
5417 result = _dbus_string_copy_len (&message->body, 0, body_len,
5420 _dbus_assert (result); /* because DBusString never reallocs smaller */
5423 if (_dbus_string_get_length (&message->header) > 0)
5427 result = _dbus_string_copy_len (&message->header, 0, header_len,
5430 _dbus_assert (result); /* because DBusString never reallocs smaller */
5433 /* does nothing if the message isn't in the list */
5434 _dbus_list_remove_last (&loader->messages, message);
5436 dbus_message_unref (message);
5444 * Converts buffered data into messages.
5446 * @todo we need to check that the proper named header fields exist
5447 * for each message type.
5449 * @todo If a message has unknown type, we should probably eat it
5450 * right here rather than passing it out to applications. However
5451 * it's not an error to see messages of unknown type.
5453 * @param loader the loader.
5454 * @returns #TRUE if we had enough memory to finish.
5457 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
5459 while (!loader->corrupted && _dbus_string_get_length (&loader->data) >= 16)
5461 const char *header_data;
5462 int byte_order, message_type, header_len, body_len;
5463 dbus_uint32_t header_len_unsigned, body_len_unsigned;
5465 header_data = _dbus_string_get_const_data_len (&loader->data, 0, 16);
5467 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
5469 if (header_data[VERSION_OFFSET] != DBUS_MAJOR_PROTOCOL_VERSION)
5471 _dbus_verbose ("Message has protocol version %d ours is %d\n",
5472 (int) header_data[VERSION_OFFSET], DBUS_MAJOR_PROTOCOL_VERSION);
5473 loader->corrupted = TRUE;
5477 byte_order = header_data[BYTE_ORDER_OFFSET];
5479 if (byte_order != DBUS_LITTLE_ENDIAN &&
5480 byte_order != DBUS_BIG_ENDIAN)
5482 _dbus_verbose ("Message with bad byte order '%c' received\n",
5484 loader->corrupted = TRUE;
5488 /* Unknown types are ignored, but INVALID is
5491 message_type = header_data[TYPE_OFFSET];
5492 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
5494 _dbus_verbose ("Message with bad type '%d' received\n",
5496 loader->corrupted = TRUE;
5500 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
5501 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
5503 if (header_len_unsigned < 16)
5505 _dbus_verbose ("Message had broken too-small header length %u\n",
5506 header_len_unsigned);
5507 loader->corrupted = TRUE;
5511 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
5512 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
5514 _dbus_verbose ("Header or body length too large (%u %u)\n",
5515 header_len_unsigned,
5517 loader->corrupted = TRUE;
5521 /* Now that we know the values are in signed range, get
5522 * rid of stupid unsigned, just causes bugs
5524 header_len = header_len_unsigned;
5525 body_len = body_len_unsigned;
5527 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
5530 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
5532 loader->corrupted = TRUE;
5536 if (header_len + body_len > loader->max_message_size)
5538 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %ld\n",
5539 header_len, body_len, loader->max_message_size);
5540 loader->corrupted = TRUE;
5544 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
5546 if (!load_one_message (loader, byte_order, message_type,
5547 header_len, body_len))
5558 * Peeks at first loaded message, returns #NULL if no messages have
5561 * @param loader the loader.
5562 * @returns the next message, or #NULL if none.
5565 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
5567 if (loader->messages)
5568 return loader->messages->data;
5574 * Pops a loaded message (passing ownership of the message
5575 * to the caller). Returns #NULL if no messages have been
5578 * @param loader the loader.
5579 * @returns the next message, or #NULL if none.
5582 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
5584 return _dbus_list_pop_first (&loader->messages);
5588 * Pops a loaded message inside a list link (passing ownership of the
5589 * message and link to the caller). Returns #NULL if no messages have
5592 * @param loader the loader.
5593 * @returns the next message link, or #NULL if none.
5596 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
5598 return _dbus_list_pop_first_link (&loader->messages);
5602 * Returns a popped message link, used to undo a pop.
5604 * @param loader the loader
5605 * @param link the link with a message in it
5608 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
5611 _dbus_list_prepend_link (&loader->messages, link);
5615 * Checks whether the loader is confused due to bad data.
5616 * If messages are received that are invalid, the
5617 * loader gets confused and gives up permanently.
5618 * This state is called "corrupted."
5620 * @param loader the loader
5621 * @returns #TRUE if the loader is hosed.
5624 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
5626 return loader->corrupted;
5630 * Sets the maximum size message we allow.
5632 * @param loader the loader
5633 * @param size the max message size in bytes
5636 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
5639 if (size > MAX_SANE_MESSAGE_SIZE)
5641 _dbus_verbose ("clamping requested max message size %ld to %d\n",
5642 size, MAX_SANE_MESSAGE_SIZE);
5643 size = MAX_SANE_MESSAGE_SIZE;
5645 loader->max_message_size = size;
5649 * Gets the maximum allowed message size in bytes.
5651 * @param loader the loader
5652 * @returns max size in bytes
5655 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
5657 return loader->max_message_size;
5660 static DBusDataSlotAllocator slot_allocator;
5661 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
5664 * Allocates an integer ID to be used for storing application-specific
5665 * data on any DBusMessage. The allocated ID may then be used
5666 * with dbus_message_set_data() and dbus_message_get_data().
5667 * The passed-in slot must be initialized to -1, and is filled in
5668 * with the slot ID. If the passed-in slot is not -1, it's assumed
5669 * to be already allocated, and its refcount is incremented.
5671 * The allocated slot is global, i.e. all DBusMessage objects will
5672 * have a slot with the given integer ID reserved.
5674 * @param slot_p address of a global variable storing the slot
5675 * @returns #FALSE on failure (no memory)
5678 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
5680 return _dbus_data_slot_allocator_alloc (&slot_allocator,
5681 _DBUS_LOCK_NAME (message_slots),
5686 * Deallocates a global ID for message data slots.
5687 * dbus_message_get_data() and dbus_message_set_data() may no
5688 * longer be used with this slot. Existing data stored on existing
5689 * DBusMessage objects will be freed when the message is
5690 * finalized, but may not be retrieved (and may only be replaced if
5691 * someone else reallocates the slot). When the refcount on the
5692 * passed-in slot reaches 0, it is set to -1.
5694 * @param slot_p address storing the slot to deallocate
5697 dbus_message_free_data_slot (dbus_int32_t *slot_p)
5699 _dbus_return_if_fail (*slot_p >= 0);
5701 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
5705 * Stores a pointer on a DBusMessage, along
5706 * with an optional function to be used for freeing
5707 * the data when the data is set again, or when
5708 * the message is finalized. The slot number
5709 * must have been allocated with dbus_message_allocate_data_slot().
5711 * @param message the message
5712 * @param slot the slot number
5713 * @param data the data to store
5714 * @param free_data_func finalizer function for the data
5715 * @returns #TRUE if there was enough memory to store the data
5718 dbus_message_set_data (DBusMessage *message,
5721 DBusFreeFunction free_data_func)
5723 DBusFreeFunction old_free_func;
5727 _dbus_return_val_if_fail (message != NULL, FALSE);
5728 _dbus_return_val_if_fail (slot >= 0, FALSE);
5730 retval = _dbus_data_slot_list_set (&slot_allocator,
5731 &message->slot_list,
5732 slot, data, free_data_func,
5733 &old_free_func, &old_data);
5737 /* Do the actual free outside the message lock */
5739 (* old_free_func) (old_data);
5746 * Retrieves data previously set with dbus_message_set_data().
5747 * The slot must still be allocated (must not have been freed).
5749 * @param message the message
5750 * @param slot the slot to get data from
5751 * @returns the data, or #NULL if not found
5754 dbus_message_get_data (DBusMessage *message,
5759 _dbus_return_val_if_fail (message != NULL, NULL);
5761 res = _dbus_data_slot_list_get (&slot_allocator,
5762 &message->slot_list,
5769 * Utility function to convert a machine-readable (not translated)
5770 * string into a D-BUS message type.
5773 * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL
5774 * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN
5775 * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL
5776 * "error" -> DBUS_MESSAGE_TYPE_ERROR
5777 * anything else -> DBUS_MESSAGE_TYPE_INVALID
5782 dbus_message_type_from_string (const char *type_str)
5784 if (strcmp (type_str, "method_call") == 0)
5785 return DBUS_MESSAGE_TYPE_METHOD_CALL;
5786 if (strcmp (type_str, "method_return") == 0)
5787 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
5788 else if (strcmp (type_str, "signal") == 0)
5789 return DBUS_MESSAGE_TYPE_SIGNAL;
5790 else if (strcmp (type_str, "error") == 0)
5791 return DBUS_MESSAGE_TYPE_ERROR;
5793 return DBUS_MESSAGE_TYPE_INVALID;
5797 #ifdef DBUS_BUILD_TESTS
5798 #include "dbus-test.h"
5803 message_iter_test (DBusMessage *message)
5805 DBusMessageIter iter, dict, dict2, array, array2;
5807 unsigned char *data;
5808 dbus_int32_t *our_int_array;
5811 dbus_message_iter_init (message, &iter);
5814 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
5815 _dbus_assert_not_reached ("Argument type isn't string");
5817 str = dbus_message_iter_get_string (&iter);
5818 if (strcmp (str, "Test string") != 0)
5819 _dbus_assert_not_reached ("Strings differ");
5822 if (!dbus_message_iter_next (&iter))
5823 _dbus_assert_not_reached ("Reached end of arguments");
5825 /* Signed integer tests */
5826 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32)
5827 _dbus_assert_not_reached ("Argument type isn't int32");
5829 if (dbus_message_iter_get_int32 (&iter) != -0x12345678)
5830 _dbus_assert_not_reached ("Signed integers differ");
5832 if (!dbus_message_iter_next (&iter))
5833 _dbus_assert_not_reached ("Reached end of fields");
5835 /* Unsigned integer tests */
5836 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
5837 _dbus_assert_not_reached ("Argument type isn't int32");
5839 if (dbus_message_iter_get_uint32 (&iter) != 0xedd1e)
5840 _dbus_assert_not_reached ("Unsigned integers differ");
5842 if (!dbus_message_iter_next (&iter))
5843 _dbus_assert_not_reached ("Reached end of arguments");
5846 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DOUBLE)
5847 _dbus_assert_not_reached ("Argument type isn't double");
5849 if (dbus_message_iter_get_double (&iter) != 3.14159)
5850 _dbus_assert_not_reached ("Doubles differ");
5852 if (!dbus_message_iter_next (&iter))
5853 _dbus_assert_not_reached ("Reached end of arguments");
5855 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
5856 _dbus_assert_not_reached ("Argument type not an array");
5858 if (dbus_message_iter_get_array_type (&iter) != DBUS_TYPE_DOUBLE)
5859 _dbus_assert_not_reached ("Array type not double");
5862 dbus_message_iter_init_array_iterator (&iter, &array, NULL);
5864 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_DOUBLE)
5865 _dbus_assert_not_reached ("Argument type isn't double");
5867 if (dbus_message_iter_get_double (&array) != 1.5)
5868 _dbus_assert_not_reached ("Unsigned integers differ");
5870 if (!dbus_message_iter_next (&array))
5871 _dbus_assert_not_reached ("Reached end of arguments");
5873 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_DOUBLE)
5874 _dbus_assert_not_reached ("Argument type isn't double");
5876 if (dbus_message_iter_get_double (&array) != 2.5)
5877 _dbus_assert_not_reached ("Unsigned integers differ");
5879 if (dbus_message_iter_next (&array))
5880 _dbus_assert_not_reached ("Didn't reach end of arguments");
5882 if (!dbus_message_iter_next (&iter))
5883 _dbus_assert_not_reached ("Reached end of arguments");
5888 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
5889 _dbus_assert_not_reached ("not dict type");
5891 dbus_message_iter_init_dict_iterator (&iter, &dict);
5893 str = dbus_message_iter_get_dict_key (&dict);
5894 if (str == NULL || strcmp (str, "test") != 0)
5895 _dbus_assert_not_reached ("wrong dict key");
5898 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_UINT32)
5899 _dbus_assert_not_reached ("wrong dict entry type");
5901 if (dbus_message_iter_get_uint32 (&dict) != 0xDEADBEEF)
5902 _dbus_assert_not_reached ("wrong dict entry value");
5904 /* dict (in dict) */
5906 if (!dbus_message_iter_next (&dict))
5907 _dbus_assert_not_reached ("reached end of dict");
5909 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_DICT)
5910 _dbus_assert_not_reached ("not dict type");
5912 dbus_message_iter_init_dict_iterator (&dict, &dict2);
5914 str = dbus_message_iter_get_dict_key (&dict2);
5915 if (str == NULL || strcmp (str, "dictkey") != 0)
5916 _dbus_assert_not_reached ("wrong dict key");
5919 if (dbus_message_iter_get_arg_type (&dict2) != DBUS_TYPE_STRING)
5920 _dbus_assert_not_reached ("wrong dict entry type");
5922 str = dbus_message_iter_get_string (&dict2);
5923 if (str == NULL || strcmp (str, "dictvalue") != 0)
5924 _dbus_assert_not_reached ("wrong dict entry value");
5927 if (dbus_message_iter_next (&dict2))
5928 _dbus_assert_not_reached ("didn't reach end of dict");
5930 if (!dbus_message_iter_next (&dict))
5931 _dbus_assert_not_reached ("reached end of dict");
5933 /* array of array of int32 (in dict) */
5935 str = dbus_message_iter_get_dict_key (&dict);
5936 if (str == NULL || strcmp (str, "array") != 0)
5937 _dbus_assert_not_reached ("wrong dict key");
5940 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_ARRAY)
5941 _dbus_assert_not_reached ("Argument type not an array");
5943 if (dbus_message_iter_get_array_type (&dict) != DBUS_TYPE_ARRAY)
5944 _dbus_assert_not_reached ("Array type not array");
5946 dbus_message_iter_init_array_iterator (&dict, &array, NULL);
5948 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_ARRAY)
5949 _dbus_assert_not_reached ("Argument type isn't array");
5951 if (dbus_message_iter_get_array_type (&array) != DBUS_TYPE_INT32)
5952 _dbus_assert_not_reached ("Array type not int32");
5954 dbus_message_iter_init_array_iterator (&array, &array2, NULL);
5956 if (dbus_message_iter_get_arg_type (&array2) != DBUS_TYPE_INT32)
5957 _dbus_assert_not_reached ("Argument type isn't int32");
5959 if (dbus_message_iter_get_int32 (&array2) != 0x12345678)
5960 _dbus_assert_not_reached ("Signed integers differ");
5962 if (!dbus_message_iter_next (&array2))
5963 _dbus_assert_not_reached ("Reached end of arguments");
5965 if (dbus_message_iter_get_int32 (&array2) != 0x23456781)
5966 _dbus_assert_not_reached ("Signed integers differ");
5968 if (dbus_message_iter_next (&array2))
5969 _dbus_assert_not_reached ("Didn't reached end of arguments");
5971 if (!dbus_message_iter_next (&array))
5972 _dbus_assert_not_reached ("Reached end of arguments");
5974 if (dbus_message_iter_get_array_type (&array) != DBUS_TYPE_INT32)
5975 _dbus_assert_not_reached ("Array type not int32");
5977 if (!dbus_message_iter_get_int32_array (&array,
5980 _dbus_assert_not_reached ("couldn't get int32 array");
5982 _dbus_assert (len == 3);
5983 _dbus_assert (our_int_array[0] == 0x34567812 &&
5984 our_int_array[1] == 0x45678123 &&
5985 our_int_array[2] == 0x56781234);
5986 dbus_free (our_int_array);
5988 if (dbus_message_iter_next (&array))
5989 _dbus_assert_not_reached ("Didn't reach end of array");
5991 if (dbus_message_iter_next (&dict))
5992 _dbus_assert_not_reached ("Didn't reach end of dict");
5994 if (!dbus_message_iter_next (&iter))
5995 _dbus_assert_not_reached ("Reached end of arguments");
5997 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_BYTE)
5999 _dbus_warn ("type was: %d\n", dbus_message_iter_get_arg_type (&iter));
6000 _dbus_assert_not_reached ("wrong type after dict (should be byte)");
6003 if (dbus_message_iter_get_byte (&iter) != 0xF0)
6004 _dbus_assert_not_reached ("wrong value after dict");
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_NIL)
6011 _dbus_assert_not_reached ("not a nil type");
6013 if (!dbus_message_iter_next (&iter))
6014 _dbus_assert_not_reached ("Reached end of arguments");
6016 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_CUSTOM)
6017 _dbus_assert_not_reached ("wrong type after dict");
6019 if (!dbus_message_iter_get_custom (&iter, &str, &data, &len))
6020 _dbus_assert_not_reached ("failed to get custom type");
6022 _dbus_assert (strcmp (str, "MyTypeName")==0);
6023 _dbus_assert (len == 5);
6024 _dbus_assert (strcmp (data, "data")==0);
6028 if (dbus_message_iter_next (&iter))
6029 _dbus_assert_not_reached ("Didn't reach end of arguments");
6034 check_message_handling_type (DBusMessageIter *iter,
6037 DBusMessageIter child_iter;
6043 case DBUS_TYPE_BYTE:
6044 dbus_message_iter_get_byte (iter);
6046 case DBUS_TYPE_BOOLEAN:
6047 dbus_message_iter_get_boolean (iter);
6049 case DBUS_TYPE_INT32:
6050 dbus_message_iter_get_int32 (iter);
6052 case DBUS_TYPE_UINT32:
6053 dbus_message_iter_get_uint32 (iter);
6055 case DBUS_TYPE_INT64:
6056 #ifdef DBUS_HAVE_INT64
6057 dbus_message_iter_get_int64 (iter);
6060 case DBUS_TYPE_UINT64:
6061 #ifdef DBUS_HAVE_INT64
6062 dbus_message_iter_get_uint64 (iter);
6065 case DBUS_TYPE_DOUBLE:
6066 dbus_message_iter_get_double (iter);
6068 case DBUS_TYPE_STRING:
6071 str = dbus_message_iter_get_string (iter);
6074 _dbus_warn ("NULL string in message\n");
6080 case DBUS_TYPE_CUSTOM:
6083 unsigned char *data;
6086 if (!dbus_message_iter_get_custom (iter, &name, &data, &len))
6088 _dbus_warn ("error reading name from custom type\n");
6095 case DBUS_TYPE_ARRAY:
6099 dbus_message_iter_init_array_iterator (iter, &child_iter, &array_type);
6101 while (dbus_message_iter_has_next (&child_iter))
6103 if (!check_message_handling_type (&child_iter, array_type))
6105 _dbus_warn ("error in array element\n");
6109 if (!dbus_message_iter_next (&child_iter))
6114 case DBUS_TYPE_DICT:
6119 dbus_message_iter_init_dict_iterator (iter, &child_iter);
6121 while ((entry_type = dbus_message_iter_get_arg_type (&child_iter)) != DBUS_TYPE_INVALID)
6123 key = dbus_message_iter_get_dict_key (&child_iter);
6126 _dbus_warn ("error reading dict key\n");
6131 if (!check_message_handling_type (&child_iter, entry_type))
6133 _dbus_warn ("error in dict value\n");
6137 if (!dbus_message_iter_next (&child_iter))
6144 _dbus_warn ("unknown type %d\n", type);
6153 check_message_handling (DBusMessage *message)
6155 DBusMessageIter iter;
6158 dbus_uint32_t client_serial;
6162 client_serial = dbus_message_get_serial (message);
6164 /* can't use set_serial due to the assertions at the start of it */
6165 _dbus_marshal_set_uint32 (&message->header,
6166 message->byte_order,
6167 CLIENT_SERIAL_OFFSET,
6170 if (client_serial != dbus_message_get_serial (message))
6172 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
6176 /* If we implement message_set_arg (message, n, value)
6177 * then we would want to test it here
6180 dbus_message_iter_init (message, &iter);
6181 while ((type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
6183 if (!check_message_handling_type (&iter, type))
6186 if (!dbus_message_iter_next (&iter))
6197 check_have_valid_message (DBusMessageLoader *loader)
6199 DBusMessage *message;
6205 if (!_dbus_message_loader_queue_messages (loader))
6206 _dbus_assert_not_reached ("no memory to queue messages");
6208 if (_dbus_message_loader_get_is_corrupted (loader))
6210 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
6214 message = _dbus_message_loader_pop_message (loader);
6215 if (message == NULL)
6217 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
6221 if (_dbus_string_get_length (&loader->data) > 0)
6223 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
6227 /* Verify that we're able to properly deal with the message.
6228 * For example, this would detect improper handling of messages
6229 * in nonstandard byte order.
6231 if (!check_message_handling (message))
6238 dbus_message_unref (message);
6244 check_invalid_message (DBusMessageLoader *loader)
6250 if (!_dbus_message_loader_queue_messages (loader))
6251 _dbus_assert_not_reached ("no memory to queue messages");
6253 if (!_dbus_message_loader_get_is_corrupted (loader))
6255 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
6266 check_incomplete_message (DBusMessageLoader *loader)
6268 DBusMessage *message;
6274 if (!_dbus_message_loader_queue_messages (loader))
6275 _dbus_assert_not_reached ("no memory to queue messages");
6277 if (_dbus_message_loader_get_is_corrupted (loader))
6279 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
6283 message = _dbus_message_loader_pop_message (loader);
6284 if (message != NULL)
6286 _dbus_warn ("loaded message that was expected to be incomplete\n");
6294 dbus_message_unref (message);
6299 check_loader_results (DBusMessageLoader *loader,
6300 DBusMessageValidity validity)
6302 if (!_dbus_message_loader_queue_messages (loader))
6303 _dbus_assert_not_reached ("no memory to queue messages");
6307 case _DBUS_MESSAGE_VALID:
6308 return check_have_valid_message (loader);
6309 case _DBUS_MESSAGE_INVALID:
6310 return check_invalid_message (loader);
6311 case _DBUS_MESSAGE_INCOMPLETE:
6312 return check_incomplete_message (loader);
6313 case _DBUS_MESSAGE_UNKNOWN:
6317 _dbus_assert_not_reached ("bad DBusMessageValidity");
6323 * Loads the message in the given message file.
6325 * @param filename filename to load
6326 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
6327 * @param data string to load message into
6328 * @returns #TRUE if the message was loaded
6331 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
6343 _dbus_verbose ("Loading raw %s\n", _dbus_string_get_const_data (filename));
6344 dbus_error_init (&error);
6345 if (!_dbus_file_get_contents (data, filename, &error))
6347 _dbus_warn ("Could not load message file %s: %s\n",
6348 _dbus_string_get_const_data (filename),
6350 dbus_error_free (&error);
6356 if (!_dbus_message_data_load (data, filename))
6358 _dbus_warn ("Could not load message file %s\n",
6359 _dbus_string_get_const_data (filename));
6372 * Tries loading the message in the given message file
6373 * and verifies that DBusMessageLoader can handle it.
6375 * @param filename filename to load
6376 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
6377 * @param expected_validity what the message has to be like to return #TRUE
6378 * @returns #TRUE if the message has the expected validity
6381 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
6383 DBusMessageValidity expected_validity)
6390 if (!_dbus_string_init (&data))
6391 _dbus_assert_not_reached ("could not allocate string\n");
6393 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
6397 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
6403 if (_dbus_string_get_length (&data) > 0)
6404 _dbus_verbose_bytes_of_string (&data, 0,
6405 _dbus_string_get_length (&data));
6407 _dbus_warn ("Failed message loader test on %s\n",
6408 _dbus_string_get_const_data (filename));
6411 _dbus_string_free (&data);
6417 * Tries loading the given message data.
6420 * @param data the message data
6421 * @param expected_validity what the message has to be like to return #TRUE
6422 * @returns #TRUE if the message has the expected validity
6425 dbus_internal_do_not_use_try_message_data (const DBusString *data,
6426 DBusMessageValidity expected_validity)
6428 DBusMessageLoader *loader;
6436 /* Write the data one byte at a time */
6438 loader = _dbus_message_loader_new ();
6440 /* check some trivial loader functions */
6441 _dbus_message_loader_ref (loader);
6442 _dbus_message_loader_unref (loader);
6443 _dbus_message_loader_get_max_message_size (loader);
6445 len = _dbus_string_get_length (data);
6446 for (i = 0; i < len; i++)
6450 _dbus_message_loader_get_buffer (loader, &buffer);
6451 _dbus_string_append_byte (buffer,
6452 _dbus_string_get_byte (data, i));
6453 _dbus_message_loader_return_buffer (loader, buffer, 1);
6456 if (!check_loader_results (loader, expected_validity))
6459 _dbus_message_loader_unref (loader);
6462 /* Write the data all at once */
6464 loader = _dbus_message_loader_new ();
6469 _dbus_message_loader_get_buffer (loader, &buffer);
6470 _dbus_string_copy (data, 0, buffer,
6471 _dbus_string_get_length (buffer));
6472 _dbus_message_loader_return_buffer (loader, buffer, 1);
6475 if (!check_loader_results (loader, expected_validity))
6478 _dbus_message_loader_unref (loader);
6481 /* Write the data 2 bytes at a time */
6483 loader = _dbus_message_loader_new ();
6485 len = _dbus_string_get_length (data);
6486 for (i = 0; i < len; i += 2)
6490 _dbus_message_loader_get_buffer (loader, &buffer);
6491 _dbus_string_append_byte (buffer,
6492 _dbus_string_get_byte (data, i));
6494 _dbus_string_append_byte (buffer,
6495 _dbus_string_get_byte (data, i+1));
6496 _dbus_message_loader_return_buffer (loader, buffer, 1);
6499 if (!check_loader_results (loader, expected_validity))
6502 _dbus_message_loader_unref (loader);
6510 _dbus_message_loader_unref (loader);
6516 process_test_subdir (const DBusString *test_base_dir,
6518 DBusMessageValidity validity,
6519 DBusForeachMessageFileFunc function,
6522 DBusString test_directory;
6523 DBusString filename;
6531 if (!_dbus_string_init (&test_directory))
6532 _dbus_assert_not_reached ("didn't allocate test_directory\n");
6534 _dbus_string_init_const (&filename, subdir);
6536 if (!_dbus_string_copy (test_base_dir, 0,
6537 &test_directory, 0))
6538 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
6540 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
6541 _dbus_assert_not_reached ("couldn't allocate full path");
6543 _dbus_string_free (&filename);
6544 if (!_dbus_string_init (&filename))
6545 _dbus_assert_not_reached ("didn't allocate filename string\n");
6547 dbus_error_init (&error);
6548 dir = _dbus_directory_open (&test_directory, &error);
6551 _dbus_warn ("Could not open %s: %s\n",
6552 _dbus_string_get_const_data (&test_directory),
6554 dbus_error_free (&error);
6558 printf ("Testing %s:\n", subdir);
6561 while (_dbus_directory_get_next_file (dir, &filename, &error))
6563 DBusString full_path;
6566 if (!_dbus_string_init (&full_path))
6567 _dbus_assert_not_reached ("couldn't init string");
6569 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
6570 _dbus_assert_not_reached ("couldn't copy dir to full_path");
6572 if (!_dbus_concat_dir_and_file (&full_path, &filename))
6573 _dbus_assert_not_reached ("couldn't concat file to dir");
6575 if (_dbus_string_ends_with_c_str (&filename, ".message"))
6577 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
6581 _dbus_verbose ("Skipping non-.message file %s\n",
6582 _dbus_string_get_const_data (&filename));
6583 _dbus_string_free (&full_path);
6588 _dbus_string_get_const_data (&filename));
6590 _dbus_verbose (" expecting %s for %s\n",
6591 validity == _DBUS_MESSAGE_VALID ? "valid" :
6592 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
6593 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")),
6594 _dbus_string_get_const_data (&filename));
6596 if (! (*function) (&full_path, is_raw, validity, user_data))
6598 _dbus_string_free (&full_path);
6602 _dbus_string_free (&full_path);
6605 if (dbus_error_is_set (&error))
6607 _dbus_warn ("Could not get next file in %s: %s\n",
6608 _dbus_string_get_const_data (&test_directory),
6610 dbus_error_free (&error);
6619 _dbus_directory_close (dir);
6620 _dbus_string_free (&test_directory);
6621 _dbus_string_free (&filename);
6627 * Runs the given function on every message file in the test suite.
6628 * The function should return #FALSE on test failure or fatal error.
6630 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
6631 * @param func the function to run
6632 * @param user_data data for function
6633 * @returns #FALSE if there's a failure
6636 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
6637 DBusForeachMessageFileFunc func,
6640 DBusString test_directory;
6645 _dbus_string_init_const (&test_directory, test_data_dir);
6647 if (!process_test_subdir (&test_directory, "valid-messages",
6648 _DBUS_MESSAGE_VALID, func, user_data))
6651 if (!process_test_subdir (&test_directory, "invalid-messages",
6652 _DBUS_MESSAGE_INVALID, func, user_data))
6655 if (!process_test_subdir (&test_directory, "incomplete-messages",
6656 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
6663 _dbus_string_free (&test_directory);
6669 verify_test_message (DBusMessage *message)
6671 DBusMessageIter iter, dict;
6673 dbus_int32_t our_int;
6676 dbus_bool_t our_bool;
6677 dbus_uint32_t our_uint32;
6678 dbus_int32_t *our_uint32_array;
6679 int our_uint32_array_len;
6680 dbus_int32_t *our_int32_array;
6681 int our_int32_array_len;
6682 char **our_string_array;
6683 int our_string_array_len;
6684 #ifdef DBUS_HAVE_INT64
6685 dbus_int64_t our_int64;
6686 dbus_uint64_t our_uint64;
6687 dbus_int64_t *our_uint64_array;
6688 int our_uint64_array_len;
6689 dbus_int64_t *our_int64_array;
6690 int our_int64_array_len;
6692 double *our_double_array;
6693 int our_double_array_len;
6694 unsigned char *our_byte_array;
6695 int our_byte_array_len;
6696 unsigned char *our_boolean_array;
6697 int our_boolean_array_len;
6699 dbus_message_iter_init (message, &iter);
6701 dbus_error_init (&error);
6702 if (!dbus_message_iter_get_args (&iter, &error,
6703 DBUS_TYPE_INT32, &our_int,
6704 #ifdef DBUS_HAVE_INT64
6705 DBUS_TYPE_INT64, &our_int64,
6706 DBUS_TYPE_UINT64, &our_uint64,
6708 DBUS_TYPE_STRING, &our_str,
6709 DBUS_TYPE_DOUBLE, &our_double,
6710 DBUS_TYPE_BOOLEAN, &our_bool,
6711 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32,
6712 &our_uint32_array, &our_uint32_array_len,
6713 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32,
6714 &our_int32_array, &our_int32_array_len,
6715 #ifdef DBUS_HAVE_INT64
6716 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64,
6717 &our_uint64_array, &our_uint64_array_len,
6718 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64,
6719 &our_int64_array, &our_int64_array_len,
6721 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
6722 &our_string_array, &our_string_array_len,
6723 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE,
6724 &our_double_array, &our_double_array_len,
6725 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
6726 &our_byte_array, &our_byte_array_len,
6727 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN,
6728 &our_boolean_array, &our_boolean_array_len,
6731 _dbus_warn ("error: %s - %s\n", error.name,
6732 (error.message != NULL) ? error.message : "no message");
6733 _dbus_assert_not_reached ("Could not get arguments");
6736 if (our_int != -0x12345678)
6737 _dbus_assert_not_reached ("integers differ!");
6739 #ifdef DBUS_HAVE_INT64
6740 if (our_int64 != DBUS_INT64_CONSTANT (-0x123456789abcd))
6741 _dbus_assert_not_reached ("64-bit integers differ!");
6742 if (our_uint64 != DBUS_UINT64_CONSTANT (0x123456789abcd))
6743 _dbus_assert_not_reached ("64-bit unsigned integers differ!");
6746 if (our_double != 3.14159)
6747 _dbus_assert_not_reached ("doubles differ!");
6749 if (strcmp (our_str, "Test string") != 0)
6750 _dbus_assert_not_reached ("strings differ!");
6751 dbus_free (our_str);
6754 _dbus_assert_not_reached ("booleans differ");
6756 if (our_uint32_array_len != 4 ||
6757 our_uint32_array[0] != 0x12345678 ||
6758 our_uint32_array[1] != 0x23456781 ||
6759 our_uint32_array[2] != 0x34567812 ||
6760 our_uint32_array[3] != 0x45678123)
6761 _dbus_assert_not_reached ("uint array differs");
6762 dbus_free (our_uint32_array);
6764 if (our_int32_array_len != 4 ||
6765 our_int32_array[0] != 0x12345678 ||
6766 our_int32_array[1] != -0x23456781 ||
6767 our_int32_array[2] != 0x34567812 ||
6768 our_int32_array[3] != -0x45678123)
6769 _dbus_assert_not_reached ("int array differs");
6770 dbus_free (our_int32_array);
6772 #ifdef DBUS_HAVE_INT64
6773 if (our_uint64_array_len != 4 ||
6774 our_uint64_array[0] != 0x12345678 ||
6775 our_uint64_array[1] != 0x23456781 ||
6776 our_uint64_array[2] != 0x34567812 ||
6777 our_uint64_array[3] != 0x45678123)
6778 _dbus_assert_not_reached ("uint64 array differs");
6779 dbus_free (our_uint64_array);
6781 if (our_int64_array_len != 4 ||
6782 our_int64_array[0] != 0x12345678 ||
6783 our_int64_array[1] != -0x23456781 ||
6784 our_int64_array[2] != 0x34567812 ||
6785 our_int64_array[3] != -0x45678123)
6786 _dbus_assert_not_reached ("int64 array differs");
6787 dbus_free (our_int64_array);
6788 #endif /* DBUS_HAVE_INT64 */
6790 if (our_string_array_len != 4)
6791 _dbus_assert_not_reached ("string array has wrong length");
6793 if (strcmp (our_string_array[0], "Foo") != 0 ||
6794 strcmp (our_string_array[1], "bar") != 0 ||
6795 strcmp (our_string_array[2], "") != 0 ||
6796 strcmp (our_string_array[3], "woo woo woo woo") != 0)
6797 _dbus_assert_not_reached ("string array differs");
6799 dbus_free_string_array (our_string_array);
6801 if (our_double_array_len != 3)
6802 _dbus_assert_not_reached ("double array had wrong length");
6804 /* On all IEEE machines (i.e. everything sane) exact equality
6805 * should be preserved over the wire
6807 if (our_double_array[0] != 0.1234 ||
6808 our_double_array[1] != 9876.54321 ||
6809 our_double_array[2] != -300.0)
6810 _dbus_assert_not_reached ("double array had wrong values");
6812 dbus_free (our_double_array);
6814 if (our_byte_array_len != 4)
6815 _dbus_assert_not_reached ("byte array had wrong length");
6817 if (our_byte_array[0] != 'a' ||
6818 our_byte_array[1] != 'b' ||
6819 our_byte_array[2] != 'c' ||
6820 our_byte_array[3] != 234)
6821 _dbus_assert_not_reached ("byte array had wrong values");
6823 dbus_free (our_byte_array);
6825 if (our_boolean_array_len != 5)
6826 _dbus_assert_not_reached ("bool array had wrong length");
6828 if (our_boolean_array[0] != TRUE ||
6829 our_boolean_array[1] != FALSE ||
6830 our_boolean_array[2] != TRUE ||
6831 our_boolean_array[3] != TRUE ||
6832 our_boolean_array[4] != FALSE)
6833 _dbus_assert_not_reached ("bool array had wrong values");
6835 dbus_free (our_boolean_array);
6837 if (!dbus_message_iter_next (&iter))
6838 _dbus_assert_not_reached ("Reached end of arguments");
6840 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
6841 _dbus_assert_not_reached ("not dict type");
6843 dbus_message_iter_init_dict_iterator (&iter, &dict);
6845 our_str = dbus_message_iter_get_dict_key (&dict);
6846 if (our_str == NULL || strcmp (our_str, "test") != 0)
6847 _dbus_assert_not_reached ("wrong dict key");
6848 dbus_free (our_str);
6850 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_UINT32)
6852 _dbus_verbose ("dict entry type: %d\n", dbus_message_iter_get_arg_type (&dict));
6853 _dbus_assert_not_reached ("wrong dict entry type");
6856 if ((our_uint32 = dbus_message_iter_get_uint32 (&dict)) != 0xDEADBEEF)
6858 _dbus_verbose ("dict entry val: %x\n", our_uint32);
6859 _dbus_assert_not_reached ("wrong dict entry value");
6862 if (dbus_message_iter_next (&dict))
6863 _dbus_assert_not_reached ("Didn't reach end of dict");
6865 if (!dbus_message_iter_next (&iter))
6866 _dbus_assert_not_reached ("Reached end of arguments");
6868 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
6869 _dbus_assert_not_reached ("wrong type after dict");
6871 if (dbus_message_iter_get_uint32 (&iter) != 0xCAFEBABE)
6872 _dbus_assert_not_reached ("wrong value after dict");
6874 if (dbus_message_iter_next (&iter))
6875 _dbus_assert_not_reached ("Didn't reach end of arguments");
6879 * @ingroup DBusMessageInternals
6880 * Unit test for DBusMessage.
6882 * @returns #TRUE on success.
6885 _dbus_message_test (const char *test_data_dir)
6887 DBusMessage *message;
6888 DBusMessageLoader *loader;
6889 DBusMessageIter iter, child_iter, child_iter2, child_iter3;
6895 const dbus_uint32_t our_uint32_array[] =
6896 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
6897 const dbus_uint32_t our_int32_array[] =
6898 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
6899 #ifdef DBUS_HAVE_INT64
6900 const dbus_uint64_t our_uint64_array[] =
6901 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
6902 const dbus_uint64_t our_int64_array[] =
6903 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
6905 const char *our_string_array[] = { "Foo", "bar", "", "woo woo woo woo" };
6906 const double our_double_array[] = { 0.1234, 9876.54321, -300.0 };
6907 const unsigned char our_byte_array[] = { 'a', 'b', 'c', 234 };
6908 const unsigned char our_boolean_array[] = { TRUE, FALSE, TRUE, TRUE, FALSE };
6914 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
6916 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
6917 "/org/freedesktop/TestPath",
6918 "Foo.TestInterface",
6920 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
6921 _dbus_assert (dbus_message_is_method_call (message, "Foo.TestInterface",
6923 _dbus_assert (strcmp (dbus_message_get_path (message),
6924 "/org/freedesktop/TestPath") == 0);
6925 _dbus_message_set_serial (message, 1234);
6926 /* string length including nul byte not a multiple of 4 */
6927 if (!dbus_message_set_sender (message, "org.foo.bar1"))
6928 _dbus_assert_not_reached ("out of memory");
6929 _dbus_assert (dbus_message_has_sender (message, "org.foo.bar1"));
6930 dbus_message_set_reply_serial (message, 5678);
6931 if (!dbus_message_set_sender (message, NULL))
6932 _dbus_assert_not_reached ("out of memory");
6933 _dbus_assert (!dbus_message_has_sender (message, "org.foo.bar1"));
6934 _dbus_assert (dbus_message_get_serial (message) == 1234);
6935 _dbus_assert (dbus_message_get_reply_serial (message) == 5678);
6936 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
6938 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
6939 dbus_message_set_no_reply (message, TRUE);
6940 _dbus_assert (dbus_message_get_no_reply (message) == TRUE);
6941 dbus_message_set_no_reply (message, FALSE);
6942 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
6944 /* Set/get some header fields */
6946 if (!dbus_message_set_path (message, "/foo"))
6947 _dbus_assert_not_reached ("out of memory");
6948 _dbus_assert (strcmp (dbus_message_get_path (message),
6951 if (!dbus_message_set_interface (message, "org.Foo"))
6952 _dbus_assert_not_reached ("out of memory");
6953 _dbus_assert (strcmp (dbus_message_get_interface (message),
6956 if (!dbus_message_set_member (message, "Bar"))
6957 _dbus_assert_not_reached ("out of memory");
6958 _dbus_assert (strcmp (dbus_message_get_member (message),
6961 /* Set/get them with longer values */
6962 if (!dbus_message_set_path (message, "/foo/bar"))
6963 _dbus_assert_not_reached ("out of memory");
6964 _dbus_assert (strcmp (dbus_message_get_path (message),
6967 if (!dbus_message_set_interface (message, "org.Foo.Bar"))
6968 _dbus_assert_not_reached ("out of memory");
6969 _dbus_assert (strcmp (dbus_message_get_interface (message),
6970 "org.Foo.Bar") == 0);
6972 if (!dbus_message_set_member (message, "BarFoo"))
6973 _dbus_assert_not_reached ("out of memory");
6974 _dbus_assert (strcmp (dbus_message_get_member (message),
6977 /* Realloc shorter again */
6979 if (!dbus_message_set_path (message, "/foo"))
6980 _dbus_assert_not_reached ("out of memory");
6981 _dbus_assert (strcmp (dbus_message_get_path (message),
6984 if (!dbus_message_set_interface (message, "org.Foo"))
6985 _dbus_assert_not_reached ("out of memory");
6986 _dbus_assert (strcmp (dbus_message_get_interface (message),
6989 if (!dbus_message_set_member (message, "Bar"))
6990 _dbus_assert_not_reached ("out of memory");
6991 _dbus_assert (strcmp (dbus_message_get_member (message),
6994 dbus_message_unref (message);
6996 /* Test the vararg functions */
6997 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
6998 "/org/freedesktop/TestPath",
6999 "Foo.TestInterface",
7001 _dbus_message_set_serial (message, 1);
7002 dbus_message_append_args (message,
7003 DBUS_TYPE_INT32, -0x12345678,
7004 #ifdef DBUS_HAVE_INT64
7005 DBUS_TYPE_INT64, DBUS_INT64_CONSTANT (-0x123456789abcd),
7006 DBUS_TYPE_UINT64, DBUS_UINT64_CONSTANT (0x123456789abcd),
7008 DBUS_TYPE_STRING, "Test string",
7009 DBUS_TYPE_DOUBLE, 3.14159,
7010 DBUS_TYPE_BOOLEAN, TRUE,
7011 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, our_uint32_array,
7012 _DBUS_N_ELEMENTS (our_uint32_array),
7013 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, our_int32_array,
7014 _DBUS_N_ELEMENTS (our_int32_array),
7015 #ifdef DBUS_HAVE_INT64
7016 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64, our_uint64_array,
7017 _DBUS_N_ELEMENTS (our_uint64_array),
7018 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64, our_int64_array,
7019 _DBUS_N_ELEMENTS (our_int64_array),
7021 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, our_string_array,
7022 _DBUS_N_ELEMENTS (our_string_array),
7023 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, our_double_array,
7024 _DBUS_N_ELEMENTS (our_double_array),
7025 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, our_byte_array,
7026 _DBUS_N_ELEMENTS (our_byte_array),
7027 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN, our_boolean_array,
7028 _DBUS_N_ELEMENTS (our_boolean_array),
7031 dbus_message_append_iter_init (message, &iter);
7032 dbus_message_iter_append_dict (&iter, &child_iter);
7033 dbus_message_iter_append_dict_key (&child_iter, "test");
7034 dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
7035 dbus_message_iter_append_uint32 (&iter, 0xCAFEBABE);
7038 sig[i++] = DBUS_TYPE_INT32;
7039 #ifdef DBUS_HAVE_INT64
7040 sig[i++] = DBUS_TYPE_INT64;
7041 sig[i++] = DBUS_TYPE_UINT64;
7043 sig[i++] = DBUS_TYPE_STRING;
7044 sig[i++] = DBUS_TYPE_DOUBLE;
7045 sig[i++] = DBUS_TYPE_BOOLEAN;
7046 sig[i++] = DBUS_TYPE_ARRAY;
7047 sig[i++] = DBUS_TYPE_UINT32;
7048 sig[i++] = DBUS_TYPE_ARRAY;
7049 sig[i++] = DBUS_TYPE_INT32;
7050 #ifdef DBUS_HAVE_INT64
7051 sig[i++] = DBUS_TYPE_ARRAY;
7052 sig[i++] = DBUS_TYPE_UINT64;
7053 sig[i++] = DBUS_TYPE_ARRAY;
7054 sig[i++] = DBUS_TYPE_INT64;
7056 sig[i++] = DBUS_TYPE_ARRAY;
7057 sig[i++] = DBUS_TYPE_STRING;
7058 sig[i++] = DBUS_TYPE_ARRAY;
7059 sig[i++] = DBUS_TYPE_DOUBLE;
7060 sig[i++] = DBUS_TYPE_ARRAY;
7061 sig[i++] = DBUS_TYPE_BYTE;
7062 sig[i++] = DBUS_TYPE_ARRAY;
7063 sig[i++] = DBUS_TYPE_BOOLEAN;
7064 sig[i++] = DBUS_TYPE_DICT;
7065 sig[i++] = DBUS_TYPE_UINT32;
7066 sig[i++] = DBUS_TYPE_INVALID;
7068 _dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
7070 _dbus_verbose_bytes_of_string (&message->header, 0,
7071 _dbus_string_get_length (&message->header));
7072 _dbus_verbose_bytes_of_string (&message->body, 0,
7073 _dbus_string_get_length (&message->body));
7075 _dbus_verbose ("Signature expected \"%s\" actual \"%s\"\n",
7076 sig, dbus_message_get_signature (message));
7078 s = dbus_message_get_signature (message);
7080 _dbus_assert (dbus_message_has_signature (message, sig));
7081 _dbus_assert (strcmp (s, sig) == 0);
7083 verify_test_message (message);
7085 copy = dbus_message_copy (message);
7087 _dbus_assert (message->client_serial == copy->client_serial);
7088 _dbus_assert (message->reply_serial == copy->reply_serial);
7089 _dbus_assert (message->header_padding == copy->header_padding);
7091 _dbus_assert (_dbus_string_get_length (&message->header) ==
7092 _dbus_string_get_length (©->header));
7094 _dbus_assert (_dbus_string_get_length (&message->body) ==
7095 _dbus_string_get_length (©->body));
7097 _dbus_assert (_dbus_string_get_length (&message->signature) ==
7098 _dbus_string_get_length (©->signature));
7100 verify_test_message (copy);
7102 name1 = dbus_message_get_interface (message);
7103 name2 = dbus_message_get_interface (copy);
7105 _dbus_assert (strcmp (name1, name2) == 0);
7107 name1 = dbus_message_get_member (message);
7108 name2 = dbus_message_get_member (copy);
7110 _dbus_assert (strcmp (name1, name2) == 0);
7112 dbus_message_unref (message);
7113 dbus_message_unref (copy);
7115 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
7116 "/org/freedesktop/TestPath",
7117 "Foo.TestInterface",
7120 _dbus_message_set_serial (message, 1);
7121 dbus_message_set_reply_serial (message, 0x12345678);
7123 dbus_message_append_iter_init (message, &iter);
7124 dbus_message_iter_append_string (&iter, "Test string");
7125 dbus_message_iter_append_int32 (&iter, -0x12345678);
7126 dbus_message_iter_append_uint32 (&iter, 0xedd1e);
7127 dbus_message_iter_append_double (&iter, 3.14159);
7129 dbus_message_iter_append_array (&iter, &child_iter, DBUS_TYPE_DOUBLE);
7130 dbus_message_iter_append_double (&child_iter, 1.5);
7131 dbus_message_iter_append_double (&child_iter, 2.5);
7134 dbus_message_iter_append_dict (&iter, &child_iter);
7135 dbus_message_iter_append_dict_key (&child_iter, "test");
7136 dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
7138 /* dict (in dict) */
7139 dbus_message_iter_append_dict_key (&child_iter, "testdict");
7140 dbus_message_iter_append_dict (&child_iter, &child_iter2);
7142 dbus_message_iter_append_dict_key (&child_iter2, "dictkey");
7143 dbus_message_iter_append_string (&child_iter2, "dictvalue");
7145 /* array of array of int32 (in dict) */
7146 dbus_message_iter_append_dict_key (&child_iter, "array");
7147 dbus_message_iter_append_array (&child_iter, &child_iter2, DBUS_TYPE_ARRAY);
7148 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
7149 dbus_message_iter_append_int32 (&child_iter3, 0x12345678);
7150 dbus_message_iter_append_int32 (&child_iter3, 0x23456781);
7151 _dbus_warn ("next call expected to fail with wrong array type\n");
7152 _dbus_assert (!dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_UINT32));
7153 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
7154 dbus_message_iter_append_int32 (&child_iter3, 0x34567812);
7155 dbus_message_iter_append_int32 (&child_iter3, 0x45678123);
7156 dbus_message_iter_append_int32 (&child_iter3, 0x56781234);
7158 dbus_message_iter_append_byte (&iter, 0xF0);
7160 dbus_message_iter_append_nil (&iter);
7162 dbus_message_iter_append_custom (&iter, "MyTypeName",
7165 message_iter_test (message);
7167 /* Message loader test */
7168 _dbus_message_lock (message);
7169 loader = _dbus_message_loader_new ();
7171 /* check ref/unref */
7172 _dbus_message_loader_ref (loader);
7173 _dbus_message_loader_unref (loader);
7175 /* Write the header data one byte at a time */
7176 data = _dbus_string_get_const_data (&message->header);
7177 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
7181 _dbus_message_loader_get_buffer (loader, &buffer);
7182 _dbus_string_append_byte (buffer, data[i]);
7183 _dbus_message_loader_return_buffer (loader, buffer, 1);
7186 /* Write the body data one byte at a time */
7187 data = _dbus_string_get_const_data (&message->body);
7188 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
7192 _dbus_message_loader_get_buffer (loader, &buffer);
7193 _dbus_string_append_byte (buffer, data[i]);
7194 _dbus_message_loader_return_buffer (loader, buffer, 1);
7197 copy = dbus_message_copy (message); /* save for tests below */
7198 dbus_message_unref (message);
7200 /* Now pop back the message */
7201 if (!_dbus_message_loader_queue_messages (loader))
7202 _dbus_assert_not_reached ("no memory to queue messages");
7204 if (_dbus_message_loader_get_is_corrupted (loader))
7205 _dbus_assert_not_reached ("message loader corrupted");
7207 message = _dbus_message_loader_pop_message (loader);
7209 _dbus_assert_not_reached ("received a NULL message");
7211 if (dbus_message_get_reply_serial (message) != 0x12345678)
7212 _dbus_assert_not_reached ("reply serial fields differ");
7214 message_iter_test (message);
7216 dbus_message_unref (message);
7217 _dbus_message_loader_unref (loader);
7219 message = dbus_message_new_method_return (copy);
7220 if (message == NULL)
7221 _dbus_assert_not_reached ("out of memory\n");
7222 dbus_message_unref (copy);
7224 if (!dbus_message_append_args (message,
7225 DBUS_TYPE_STRING, "hello",
7227 _dbus_assert_not_reached ("no memory");
7229 if (!dbus_message_has_signature (message, "s"))
7230 _dbus_assert_not_reached ("method return has wrong signature");
7232 dbus_error_init (&error);
7233 if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING,
7234 &t, DBUS_TYPE_INVALID))
7237 _dbus_warn ("Failed to get expected string arg: %s\n", error.message);
7242 dbus_message_unref (message);
7244 /* Now load every message in test_data_dir if we have one */
7245 if (test_data_dir == NULL)
7248 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
7249 (DBusForeachMessageFileFunc)
7250 dbus_internal_do_not_use_try_message_file,
7254 #endif /* DBUS_BUILD_TESTS */