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 * @see dbus_message_unref
1510 dbus_message_ref (DBusMessage *message)
1512 dbus_int32_t old_refcount;
1514 _dbus_return_if_fail (message != NULL);
1516 old_refcount = _dbus_atomic_inc (&message->refcount);
1517 _dbus_assert (old_refcount >= 1);
1521 free_size_counter (void *element,
1524 DBusCounter *counter = element;
1525 DBusMessage *message = data;
1527 _dbus_counter_adjust (counter, - message->size_counter_delta);
1529 _dbus_counter_unref (counter);
1533 * Decrements the reference count of a DBusMessage.
1535 * @param message The message
1536 * @see dbus_message_ref
1539 dbus_message_unref (DBusMessage *message)
1541 dbus_int32_t old_refcount;
1543 _dbus_return_if_fail (message != NULL);
1545 old_refcount = _dbus_atomic_dec (&message->refcount);
1547 _dbus_assert (old_refcount >= 0);
1549 if (old_refcount == 1)
1551 /* This calls application callbacks! */
1552 _dbus_data_slot_list_free (&message->slot_list);
1554 _dbus_list_foreach (&message->size_counters,
1555 free_size_counter, message);
1556 _dbus_list_clear (&message->size_counters);
1558 _dbus_string_free (&message->header);
1559 _dbus_string_free (&message->body);
1560 _dbus_string_free (&message->signature);
1562 dbus_free (message);
1567 * Gets the type of a message. Types include
1568 * DBUS_MESSAGE_TYPE_METHOD_CALL, DBUS_MESSAGE_TYPE_METHOD_RETURN,
1569 * DBUS_MESSAGE_TYPE_ERROR, DBUS_MESSAGE_TYPE_SIGNAL, but other types
1570 * are allowed and all code must silently ignore messages of unknown
1571 * type. DBUS_MESSAGE_TYPE_INVALID will never be returned, however.
1574 * @param message the message
1575 * @returns the type of the message
1578 dbus_message_get_type (DBusMessage *message)
1582 type = _dbus_string_get_byte (&message->header, 1);
1583 _dbus_assert (type != DBUS_MESSAGE_TYPE_INVALID);
1589 * Sets the object path this message is being sent to (for
1590 * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
1591 * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
1593 * @param message the message
1594 * @param object_path the path
1595 * @returns #FALSE if not enough memory
1598 dbus_message_set_path (DBusMessage *message,
1599 const char *object_path)
1601 _dbus_return_val_if_fail (message != NULL, FALSE);
1602 _dbus_return_val_if_fail (!message->locked, FALSE);
1604 return set_string_field (message,
1605 DBUS_HEADER_FIELD_PATH,
1606 DBUS_TYPE_OBJECT_PATH,
1611 * Gets the object path this message is being sent to
1612 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
1613 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
1615 * @param message the message
1616 * @returns the path (should not be freed)
1619 dbus_message_get_path (DBusMessage *message)
1621 _dbus_return_val_if_fail (message != NULL, NULL);
1623 return get_string_field (message, DBUS_HEADER_FIELD_PATH, NULL);
1627 * Gets the object path this message is being sent to
1628 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
1629 * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
1630 * format (one array element per path component).
1631 * Free the returned array with dbus_free_string_array().
1633 * An empty but non-NULL path array means the path "/".
1634 * So the path "/foo/bar" becomes { "foo", "bar", NULL }
1635 * and the path "/" becomes { NULL }.
1637 * @param message the message
1638 * @param path place to store allocated array of path components; #NULL set here if no path field exists
1639 * @returns #FALSE if no memory to allocate the array
1642 dbus_message_get_path_decomposed (DBusMessage *message,
1645 _dbus_return_val_if_fail (message != NULL, FALSE);
1646 _dbus_return_val_if_fail (path != NULL, FALSE);
1648 return get_path_field_decomposed (message,
1649 DBUS_HEADER_FIELD_PATH,
1654 * Sets the interface this message is being sent to
1655 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
1656 * the interface a signal is being emitted from
1657 * (for DBUS_MESSAGE_TYPE_SIGNAL).
1659 * @param message the message
1660 * @param interface the interface
1661 * @returns #FALSE if not enough memory
1664 dbus_message_set_interface (DBusMessage *message,
1665 const char *interface)
1667 _dbus_return_val_if_fail (message != NULL, FALSE);
1668 _dbus_return_val_if_fail (!message->locked, FALSE);
1670 return set_string_field (message,
1671 DBUS_HEADER_FIELD_INTERFACE,
1677 * Gets the interface this message is being sent to
1678 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
1679 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
1680 * The interface name is fully-qualified (namespaced).
1682 * @param message the message
1683 * @returns the message interface (should not be freed)
1686 dbus_message_get_interface (DBusMessage *message)
1688 _dbus_return_val_if_fail (message != NULL, NULL);
1690 return get_string_field (message, DBUS_HEADER_FIELD_INTERFACE, NULL);
1694 * Sets the interface member being invoked
1695 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
1696 * (DBUS_MESSAGE_TYPE_SIGNAL).
1697 * The interface name is fully-qualified (namespaced).
1699 * @param message the message
1700 * @param member the member
1701 * @returns #FALSE if not enough memory
1704 dbus_message_set_member (DBusMessage *message,
1707 _dbus_return_val_if_fail (message != NULL, FALSE);
1708 _dbus_return_val_if_fail (!message->locked, FALSE);
1710 return set_string_field (message,
1711 DBUS_HEADER_FIELD_MEMBER,
1717 * Gets the interface member being invoked
1718 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
1719 * (DBUS_MESSAGE_TYPE_SIGNAL).
1721 * @param message the message
1722 * @returns the member name (should not be freed)
1725 dbus_message_get_member (DBusMessage *message)
1727 _dbus_return_val_if_fail (message != NULL, NULL);
1729 return get_string_field (message,
1730 DBUS_HEADER_FIELD_MEMBER,
1735 * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
1736 * The name is fully-qualified (namespaced).
1738 * @param message the message
1739 * @param error_name the name
1740 * @returns #FALSE if not enough memory
1743 dbus_message_set_error_name (DBusMessage *message,
1744 const char *error_name)
1746 _dbus_return_val_if_fail (message != NULL, FALSE);
1747 _dbus_return_val_if_fail (!message->locked, FALSE);
1749 return set_string_field (message,
1750 DBUS_HEADER_FIELD_ERROR_NAME,
1756 * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only).
1758 * @param message the message
1759 * @returns the error name (should not be freed)
1762 dbus_message_get_error_name (DBusMessage *message)
1764 _dbus_return_val_if_fail (message != NULL, NULL);
1766 return get_string_field (message,
1767 DBUS_HEADER_FIELD_ERROR_NAME,
1772 * Sets the message's destination service.
1774 * @param message the message
1775 * @param destination the destination service name
1776 * @returns #FALSE if not enough memory
1779 dbus_message_set_destination (DBusMessage *message,
1780 const char *destination)
1782 _dbus_return_val_if_fail (message != NULL, FALSE);
1783 _dbus_return_val_if_fail (!message->locked, FALSE);
1785 return set_string_field (message,
1786 DBUS_HEADER_FIELD_SERVICE,
1792 * Gets the destination service of a message.
1794 * @param message the message
1795 * @returns the message destination service (should not be freed)
1798 dbus_message_get_destination (DBusMessage *message)
1800 _dbus_return_val_if_fail (message != NULL, NULL);
1802 return get_string_field (message,
1803 DBUS_HEADER_FIELD_SERVICE,
1808 * Appends fields to a message given a variable argument list. The
1809 * variable argument list should contain the type of the argument
1810 * followed by the value to add. Array values are specified by an int
1811 * typecode followed by a pointer to the array followed by an int
1812 * giving the length of the array. The argument list must be
1813 * terminated with #DBUS_TYPE_INVALID.
1815 * This function doesn't support dicts or non-fundamental arrays.
1817 * This function supports #DBUS_TYPE_INT64 and #DBUS_TYPE_UINT64
1818 * only if #DBUS_HAVE_INT64 is defined.
1820 * @param message the message
1821 * @param first_arg_type type of the first argument
1822 * @param ... value of first argument, list of additional type-value pairs
1823 * @returns #TRUE on success
1826 dbus_message_append_args (DBusMessage *message,
1833 _dbus_return_val_if_fail (message != NULL, FALSE);
1835 va_start (var_args, first_arg_type);
1836 retval = dbus_message_append_args_valist (message,
1845 * This function takes a va_list for use by language bindings.
1846 * It's otherwise the same as dbus_message_append_args().
1848 * @todo: Shouldn't this function clean up the changes to the message
1849 * on failures? (Yes)
1851 * @see dbus_message_append_args.
1852 * @param message the message
1853 * @param first_arg_type type of first argument
1854 * @param var_args value of first argument, then list of type/value pairs
1855 * @returns #TRUE on success
1858 dbus_message_append_args_valist (DBusMessage *message,
1863 DBusMessageIter iter;
1865 _dbus_return_val_if_fail (message != NULL, FALSE);
1867 old_len = _dbus_string_get_length (&message->body);
1869 type = first_arg_type;
1871 dbus_message_append_iter_init (message, &iter);
1873 while (type != DBUS_TYPE_INVALID)
1878 if (!dbus_message_iter_append_nil (&iter))
1881 case DBUS_TYPE_BOOLEAN:
1882 if (!dbus_message_iter_append_boolean (&iter, va_arg (var_args, dbus_bool_t)))
1885 case DBUS_TYPE_INT32:
1886 if (!dbus_message_iter_append_int32 (&iter, va_arg (var_args, dbus_int32_t)))
1889 case DBUS_TYPE_UINT32:
1890 if (!dbus_message_iter_append_uint32 (&iter, va_arg (var_args, dbus_uint32_t)))
1893 #ifdef DBUS_HAVE_INT64
1894 case DBUS_TYPE_INT64:
1895 if (!dbus_message_iter_append_int64 (&iter, va_arg (var_args, dbus_int64_t)))
1898 case DBUS_TYPE_UINT64:
1899 if (!dbus_message_iter_append_uint64 (&iter, va_arg (var_args, dbus_uint64_t)))
1902 #endif /* DBUS_HAVE_INT64 */
1903 case DBUS_TYPE_DOUBLE:
1904 if (!dbus_message_iter_append_double (&iter, va_arg (var_args, double)))
1907 case DBUS_TYPE_STRING:
1908 if (!dbus_message_iter_append_string (&iter, va_arg (var_args, const char *)))
1911 case DBUS_TYPE_OBJECT_PATH:
1914 case DBUS_TYPE_CUSTOM:
1917 unsigned char *data;
1920 name = va_arg (var_args, const char *);
1921 data = va_arg (var_args, unsigned char *);
1922 len = va_arg (var_args, int);
1924 if (!dbus_message_iter_append_custom (&iter, name, data, len))
1928 case DBUS_TYPE_ARRAY:
1933 type = va_arg (var_args, int);
1934 data = va_arg (var_args, void *);
1935 len = va_arg (var_args, int);
1939 case DBUS_TYPE_BYTE:
1940 if (!dbus_message_iter_append_byte_array (&iter, (unsigned char *)data, len))
1943 case DBUS_TYPE_BOOLEAN:
1944 if (!dbus_message_iter_append_boolean_array (&iter, (unsigned char *)data, len))
1947 case DBUS_TYPE_INT32:
1948 if (!dbus_message_iter_append_int32_array (&iter, (dbus_int32_t *)data, len))
1951 case DBUS_TYPE_UINT32:
1952 if (!dbus_message_iter_append_uint32_array (&iter, (dbus_uint32_t *)data, len))
1955 #ifdef DBUS_HAVE_INT64
1956 case DBUS_TYPE_INT64:
1957 if (!dbus_message_iter_append_int64_array (&iter, (dbus_int64_t *)data, len))
1960 case DBUS_TYPE_UINT64:
1961 if (!dbus_message_iter_append_uint64_array (&iter, (dbus_uint64_t *)data, len))
1964 #endif /* DBUS_HAVE_INT64 */
1965 case DBUS_TYPE_DOUBLE:
1966 if (!dbus_message_iter_append_double_array (&iter, (double *)data, len))
1969 case DBUS_TYPE_STRING:
1970 if (!dbus_message_iter_append_string_array (&iter, (const char **)data, len))
1974 case DBUS_TYPE_ARRAY:
1975 case DBUS_TYPE_CUSTOM:
1976 case DBUS_TYPE_DICT:
1977 _dbus_warn ("dbus_message_append_args_valist doesn't support recursive arrays\n");
1980 _dbus_warn ("Unknown field type %d\n", type);
1986 case DBUS_TYPE_DICT:
1987 _dbus_warn ("dbus_message_append_args_valist doesn't support dicts\n");
1990 _dbus_warn ("Unknown field type %d\n", type);
1994 type = va_arg (var_args, int);
2005 * Gets arguments from a message given a variable argument list.
2006 * The variable argument list should contain the type of the
2007 * argumen followed by a pointer to where the value should be
2008 * stored. The list is terminated with #DBUS_TYPE_INVALID.
2010 * @param message the message
2011 * @param error error to be filled in on failure
2012 * @param first_arg_type the first argument type
2013 * @param ... location for first argument value, then list of type-location pairs
2014 * @returns #FALSE if the error was set
2017 dbus_message_get_args (DBusMessage *message,
2025 _dbus_return_val_if_fail (message != NULL, FALSE);
2026 _dbus_return_val_if_error_is_set (error, FALSE);
2028 va_start (var_args, first_arg_type);
2029 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
2036 * This function takes a va_list for use by language bindings
2038 * @todo We need to free the argument data when an error occurs.
2040 * @see dbus_message_get_args
2041 * @param message the message
2042 * @param error error to be filled in
2043 * @param first_arg_type type of the first argument
2044 * @param var_args return location for first argument, followed by list of type/location pairs
2045 * @returns #FALSE if error was set
2048 dbus_message_get_args_valist (DBusMessage *message,
2053 DBusMessageIter iter;
2055 _dbus_return_val_if_fail (message != NULL, FALSE);
2056 _dbus_return_val_if_error_is_set (error, FALSE);
2058 dbus_message_iter_init (message, &iter);
2059 return dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
2063 * Gets arguments from a message iterator given a variable argument list.
2064 * The variable argument list should contain the type of the
2065 * argumen followed by a pointer to where the value should be
2066 * stored. The list is terminated with 0.
2068 * @param iter the message iterator
2069 * @param error error to be filled in on failure
2070 * @param first_arg_type the first argument type
2071 * @param ... location for first argument value, then list of type-location pairs
2072 * @returns #FALSE if the error was set
2075 dbus_message_iter_get_args (DBusMessageIter *iter,
2083 _dbus_return_val_if_fail (iter != NULL, FALSE);
2084 _dbus_return_val_if_error_is_set (error, FALSE);
2086 va_start (var_args, first_arg_type);
2087 retval = dbus_message_iter_get_args_valist (iter, error, first_arg_type, var_args);
2094 * This function takes a va_list for use by language bindings
2096 * This function supports #DBUS_TYPE_INT64 and #DBUS_TYPE_UINT64
2097 * only if #DBUS_HAVE_INT64 is defined.
2099 * @todo this function (or some lower-level non-convenience function)
2100 * needs better error handling; should allow the application to
2101 * distinguish between out of memory, and bad data from the remote
2102 * app. It also needs to not leak a bunch of args when it gets
2103 * to the arg that's bad, as that would be a security hole
2104 * (allow one app to force another to leak memory)
2106 * @todo We need to free the argument data when an error occurs.
2108 * @see dbus_message_get_args
2109 * @param iter the message iter
2110 * @param error error to be filled in
2111 * @param first_arg_type type of the first argument
2112 * @param var_args return location for first argument, followed by list of type/location pairs
2113 * @returns #FALSE if error was set
2116 dbus_message_iter_get_args_valist (DBusMessageIter *iter,
2121 int spec_type, msg_type, i;
2124 _dbus_return_val_if_fail (iter != NULL, FALSE);
2125 _dbus_return_val_if_error_is_set (error, FALSE);
2129 spec_type = first_arg_type;
2132 while (spec_type != 0)
2134 msg_type = dbus_message_iter_get_arg_type (iter);
2136 if (msg_type != spec_type)
2138 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2139 "Argument %d is specified to be of type \"%s\", but "
2140 "is actually of type \"%s\"\n", i,
2141 _dbus_type_to_string (spec_type),
2142 _dbus_type_to_string (msg_type));
2151 case DBUS_TYPE_BYTE:
2155 ptr = va_arg (var_args, unsigned char *);
2157 *ptr = dbus_message_iter_get_byte (iter);
2160 case DBUS_TYPE_BOOLEAN:
2164 ptr = va_arg (var_args, dbus_bool_t *);
2166 *ptr = dbus_message_iter_get_boolean (iter);
2169 case DBUS_TYPE_INT32:
2173 ptr = va_arg (var_args, dbus_int32_t *);
2175 *ptr = dbus_message_iter_get_int32 (iter);
2178 case DBUS_TYPE_UINT32:
2182 ptr = va_arg (var_args, dbus_uint32_t *);
2184 *ptr = dbus_message_iter_get_uint32 (iter);
2187 #ifdef DBUS_HAVE_INT64
2188 case DBUS_TYPE_INT64:
2192 ptr = va_arg (var_args, dbus_int64_t *);
2194 *ptr = dbus_message_iter_get_int64 (iter);
2197 case DBUS_TYPE_UINT64:
2201 ptr = va_arg (var_args, dbus_uint64_t *);
2203 *ptr = dbus_message_iter_get_uint64 (iter);
2206 #endif /* DBUS_HAVE_INT64 */
2208 case DBUS_TYPE_DOUBLE:
2212 ptr = va_arg (var_args, double *);
2214 *ptr = dbus_message_iter_get_double (iter);
2218 case DBUS_TYPE_STRING:
2222 ptr = va_arg (var_args, char **);
2224 *ptr = dbus_message_iter_get_string (iter);
2228 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2235 case DBUS_TYPE_CUSTOM:
2238 unsigned char **data;
2241 name = va_arg (var_args, char **);
2242 data = va_arg (var_args, unsigned char **);
2243 len = va_arg (var_args, int *);
2245 if (!dbus_message_iter_get_custom (iter, name, data, len))
2247 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2252 case DBUS_TYPE_ARRAY:
2257 type = va_arg (var_args, int);
2258 data = va_arg (var_args, void *);
2259 len = va_arg (var_args, int *);
2261 _dbus_return_val_if_fail (data != NULL, FALSE);
2262 _dbus_return_val_if_fail (len != NULL, FALSE);
2264 if (dbus_message_iter_get_array_type (iter) != type)
2266 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2267 "Argument %d is specified to be of type \"array of %s\", but "
2268 "is actually of type \"array of %s\"\n", i,
2269 _dbus_type_to_string (type),
2270 _dbus_type_to_string (dbus_message_iter_get_array_type (iter)));
2276 case DBUS_TYPE_BYTE:
2277 if (!dbus_message_iter_get_byte_array (iter, (unsigned char **)data, len))
2279 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2283 case DBUS_TYPE_BOOLEAN:
2284 if (!dbus_message_iter_get_boolean_array (iter, (unsigned char **)data, len))
2286 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2290 case DBUS_TYPE_INT32:
2291 if (!dbus_message_iter_get_int32_array (iter, (dbus_int32_t **)data, len))
2293 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2297 case DBUS_TYPE_UINT32:
2298 if (!dbus_message_iter_get_uint32_array (iter, (dbus_uint32_t **)data, len))
2300 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2304 #ifdef DBUS_HAVE_INT64
2305 case DBUS_TYPE_INT64:
2306 if (!dbus_message_iter_get_int64_array (iter, (dbus_int64_t **)data, len))
2308 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2312 case DBUS_TYPE_UINT64:
2313 if (!dbus_message_iter_get_uint64_array (iter, (dbus_uint64_t **)data, len))
2315 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2319 #endif /* DBUS_HAVE_INT64 */
2320 case DBUS_TYPE_DOUBLE:
2321 if (!dbus_message_iter_get_double_array (iter, (double **)data, len))
2323 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2327 case DBUS_TYPE_STRING:
2328 if (!dbus_message_iter_get_string_array (iter, (char ***)data, len))
2330 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2335 case DBUS_TYPE_ARRAY:
2336 case DBUS_TYPE_CUSTOM:
2337 case DBUS_TYPE_DICT:
2338 _dbus_warn ("dbus_message_get_args_valist doesn't support recursive arrays\n");
2339 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2342 _dbus_warn ("Unknown field type %d\n", type);
2343 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2348 case DBUS_TYPE_DICT:
2349 _dbus_warn ("dbus_message_get_args_valist doesn't support dicts\n");
2350 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2353 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2354 _dbus_warn ("Unknown field type %d\n", spec_type);
2358 spec_type = va_arg (var_args, int);
2359 if (spec_type != 0 && !dbus_message_iter_next (iter))
2361 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2362 "Message has only %d arguments, but more were expected", i);
2378 * Initializes a DBusMessageIter representing the arguments of the
2379 * message passed in.
2381 * @param message the message
2382 * @param iter pointer to an iterator to initialize
2385 dbus_message_iter_init (DBusMessage *message,
2386 DBusMessageIter *iter)
2388 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2390 _dbus_return_if_fail (message != NULL);
2391 _dbus_return_if_fail (iter != NULL);
2393 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
2395 real->message = message;
2396 real->parent_iter = NULL;
2397 real->changed_stamp = message->changed_stamp;
2399 real->type = DBUS_MESSAGE_ITER_TYPE_MESSAGE;
2401 real->end = _dbus_string_get_length (&message->body);
2403 real->container_start = 0;
2404 real->container_length_pos = 0;
2405 real->wrote_dict_key = 0;
2406 real->array_type_pos = 0;
2409 #ifndef DBUS_DISABLE_CHECKS
2411 dbus_message_iter_check (DBusMessageRealIter *iter)
2415 _dbus_warn ("dbus iterator check failed: iterator is NULL\n");
2419 if (iter->changed_stamp != iter->message->changed_stamp)
2421 _dbus_warn ("dbus iterator check failed: invalid iterator, must re-initialize it after modifying the message\n");
2425 if (iter->pos < 0 || iter->pos > iter->end)
2427 _dbus_warn ("dbus iterator check failed: invalid position\n");
2433 #endif /* DBUS_DISABLE_CHECKS */
2436 skip_array_type (DBusMessageRealIter *iter, int pos)
2442 data = _dbus_string_get_const_data_len (&iter->message->body,
2445 while (*data == DBUS_TYPE_ARRAY);
2450 /* FIXME what are these _dbus_type_is_valid() checks for?
2451 * haven't we validated the message?
2454 dbus_message_iter_get_data_start (DBusMessageRealIter *iter, int *type)
2461 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
2462 data = _dbus_string_get_const_data_len (&iter->message->body,
2464 if (_dbus_type_is_valid (*data))
2467 *type = DBUS_TYPE_INVALID;
2469 return skip_array_type (iter, iter->pos);
2471 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
2472 data = _dbus_string_get_const_data_len (&iter->message->body,
2473 iter->array_type_pos, 1);
2474 if (_dbus_type_is_valid (*data))
2477 *type = DBUS_TYPE_INVALID;
2481 case DBUS_MESSAGE_ITER_TYPE_DICT:
2482 /* Get the length of the string */
2483 len = _dbus_demarshal_uint32 (&iter->message->body,
2484 iter->message->byte_order,
2486 pos = pos + len + 1;
2488 data = _dbus_string_get_const_data_len (&iter->message->body,
2490 if (_dbus_type_is_valid (*data))
2493 *type = DBUS_TYPE_INVALID;
2495 return skip_array_type (iter, pos);
2498 _dbus_assert_not_reached ("Invalid iter type");
2501 *type = DBUS_TYPE_INVALID;
2507 * Checks if an iterator has any more fields.
2509 * @param iter the message iter
2510 * @returns #TRUE if there are more fields
2514 dbus_message_iter_has_next (DBusMessageIter *iter)
2516 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2520 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2522 if (real->pos >= real->end)
2525 pos = dbus_message_iter_get_data_start (real, &type);
2527 if (!_dbus_marshal_get_arg_end_pos (&real->message->body,
2528 real->message->byte_order,
2529 type, pos, &end_pos))
2532 if (end_pos >= real->end)
2539 * Moves the iterator to the next field.
2541 * @param iter The message iter
2542 * @returns #TRUE if the iterator was moved to the next field
2545 dbus_message_iter_next (DBusMessageIter *iter)
2547 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2551 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2553 pos = dbus_message_iter_get_data_start (real, &type);
2555 if (!_dbus_marshal_get_arg_end_pos (&real->message->body,
2556 real->message->byte_order,
2557 type, pos, &end_pos))
2560 if (end_pos >= real->end)
2563 real->pos = end_pos;
2569 * Returns the argument type of the argument that the
2570 * message iterator points at.
2572 * @param iter the message iter
2573 * @returns the field type
2576 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
2578 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2581 _dbus_return_val_if_fail (dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2583 if (real->pos >= real->end)
2585 _dbus_verbose (" iterator at or beyond end of message\n");
2586 return DBUS_TYPE_INVALID;
2589 pos = dbus_message_iter_get_data_start (real, &type);
2594 /* FIXME why do we validate the typecode in here, hasn't the message
2595 * already been verified?
2598 iter_get_array_type (DBusMessageRealIter *iter, int *array_type_pos)
2601 int _array_type_pos;
2606 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
2607 _array_type_pos = iter->pos + 1;
2609 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
2610 _array_type_pos = iter->array_type_pos + 1;
2612 case DBUS_MESSAGE_ITER_TYPE_DICT:
2613 /* Get the length of the string */
2614 len = _dbus_demarshal_uint32 (&iter->message->body,
2615 iter->message->byte_order,
2617 pos = pos + len + 1;
2618 data = _dbus_string_get_const_data_len (&iter->message->body,
2620 _array_type_pos = pos + 1;
2623 _dbus_assert_not_reached ("wrong iter type");
2624 return DBUS_TYPE_INVALID;
2627 if (array_type_pos != NULL)
2628 *array_type_pos = _array_type_pos;
2630 data = _dbus_string_get_const_data_len (&iter->message->body,
2631 _array_type_pos, 1);
2632 if (_dbus_type_is_valid (*data))
2635 return DBUS_TYPE_INVALID;
2640 * Returns the element type of the array that the
2641 * message iterator points at. Note that you need
2642 * to check that the iterator points to an array
2643 * prior to using this function.
2645 * @param iter the message iter
2646 * @returns the field type
2649 dbus_message_iter_get_array_type (DBusMessageIter *iter)
2651 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2654 _dbus_return_val_if_fail (dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2656 if (real->pos >= real->end)
2657 return DBUS_TYPE_INVALID;
2659 pos = dbus_message_iter_get_data_start (real, &type);
2661 _dbus_assert (type == DBUS_TYPE_ARRAY);
2663 return iter_get_array_type (real, NULL);
2668 * Returns the string value that an iterator may point to.
2669 * Note that you need to check that the iterator points to
2670 * a string value before using this function.
2672 * @see dbus_message_iter_get_arg_type
2673 * @param iter the message iter
2674 * @returns the string
2677 dbus_message_iter_get_string (DBusMessageIter *iter)
2679 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2682 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
2684 pos = dbus_message_iter_get_data_start (real, &type);
2686 _dbus_assert (type == DBUS_TYPE_STRING);
2688 return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2694 * @todo FIXME to finish this _dbus_demarshal_object_path() needs
2695 * to not explode the path.
2697 * Returns the object path value that an iterator may point to.
2698 * Note that you need to check that the iterator points to
2699 * an object path value before using this function.
2701 * @see dbus_message_iter_get_arg_type
2702 * @param iter the message iter
2706 dbus_message_iter_get_object_path (DBusMessageIter *iter)
2708 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2711 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
2713 pos = dbus_message_iter_get_data_start (real, &type);
2715 _dbus_assert (type == DBUS_TYPE_OBJECT_PATH);
2717 return _dbus_demarshal_object_path (&real->message->body, real->message->byte_order,
2723 * Returns the name and data from a custom type that an iterator may
2724 * point to. Note that you need to check that the iterator points to a
2725 * custom type before using this function.
2727 * @see dbus_message_iter_get_arg_type
2728 * @param iter the message iter
2729 * @param name return location for the name of the custom type
2730 * @param value return location for data
2731 * @param len return location for length of data
2732 * @returns TRUE if get succeed
2736 dbus_message_iter_get_custom (DBusMessageIter *iter,
2738 unsigned char **value,
2741 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2745 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2747 pos = dbus_message_iter_get_data_start (real, &type);
2749 _dbus_assert (type == DBUS_TYPE_CUSTOM);
2751 _name = _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2757 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
2758 pos, NULL, value, len))
2770 * Returns the byte value that an iterator may point to.
2771 * Note that you need to check that the iterator points to
2772 * a byte value before using this function.
2774 * @see dbus_message_iter_get_arg_type
2775 * @param iter the message iter
2776 * @returns the byte value
2779 dbus_message_iter_get_byte (DBusMessageIter *iter)
2781 unsigned char value;
2782 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2785 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2787 pos = dbus_message_iter_get_data_start (real, &type);
2789 _dbus_assert (type == DBUS_TYPE_BYTE);
2791 value = _dbus_string_get_byte (&real->message->body, pos);
2798 * Returns the boolean value that an iterator may point to.
2799 * Note that you need to check that the iterator points to
2800 * a boolean value before using this function.
2802 * @see dbus_message_iter_get_arg_type
2803 * @param iter the message iter
2804 * @returns the boolean value
2807 dbus_message_iter_get_boolean (DBusMessageIter *iter)
2809 unsigned char value;
2810 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2813 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2815 pos = dbus_message_iter_get_data_start (real, &type);
2817 _dbus_assert (type == DBUS_TYPE_BOOLEAN);
2819 value = _dbus_string_get_byte (&real->message->body, pos);
2825 * Returns the 32 bit signed integer value that an iterator may point to.
2826 * Note that you need to check that the iterator points to
2827 * a 32-bit integer value before using this function.
2829 * @see dbus_message_iter_get_arg_type
2830 * @param iter the message iter
2831 * @returns the integer
2834 dbus_message_iter_get_int32 (DBusMessageIter *iter)
2836 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2839 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2841 pos = dbus_message_iter_get_data_start (real, &type);
2843 _dbus_assert (type == DBUS_TYPE_INT32);
2845 return _dbus_demarshal_int32 (&real->message->body, real->message->byte_order,
2850 * Returns the 32 bit unsigned integer value that an iterator may point to.
2851 * Note that you need to check that the iterator points to
2852 * a 32-bit unsigned integer value before using this function.
2854 * @see dbus_message_iter_get_arg_type
2855 * @param iter the message iter
2856 * @returns the integer
2859 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
2861 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2864 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2866 pos = dbus_message_iter_get_data_start (real, &type);
2868 _dbus_assert (type == DBUS_TYPE_UINT32);
2870 return _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
2874 #ifdef DBUS_HAVE_INT64
2877 * Returns the 64 bit signed integer value that an iterator may point
2878 * to. Note that you need to check that the iterator points to a
2879 * 64-bit integer value before using this function.
2881 * This function only exists if #DBUS_HAVE_INT64 is defined.
2883 * @see dbus_message_iter_get_arg_type
2884 * @param iter the message iter
2885 * @returns the integer
2888 dbus_message_iter_get_int64 (DBusMessageIter *iter)
2890 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2893 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2895 pos = dbus_message_iter_get_data_start (real, &type);
2897 _dbus_assert (type == DBUS_TYPE_INT64);
2899 return _dbus_demarshal_int64 (&real->message->body, real->message->byte_order,
2904 * Returns the 64 bit unsigned integer value that an iterator may point to.
2905 * Note that you need to check that the iterator points to
2906 * a 64-bit unsigned integer value before using this function.
2908 * This function only exists if #DBUS_HAVE_INT64 is defined.
2910 * @see dbus_message_iter_get_arg_type
2911 * @param iter the message iter
2912 * @returns the integer
2915 dbus_message_iter_get_uint64 (DBusMessageIter *iter)
2917 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2920 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2922 pos = dbus_message_iter_get_data_start (real, &type);
2924 _dbus_assert (type == DBUS_TYPE_UINT64);
2926 return _dbus_demarshal_uint64 (&real->message->body, real->message->byte_order,
2930 #endif /* DBUS_HAVE_INT64 */
2933 * Returns the double value that an iterator may point to.
2934 * Note that you need to check that the iterator points to
2935 * a string value before using this function.
2937 * @see dbus_message_iter_get_arg_type
2938 * @param iter the message iter
2939 * @returns the double
2942 dbus_message_iter_get_double (DBusMessageIter *iter)
2944 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2947 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0.0);
2949 pos = dbus_message_iter_get_data_start (real, &type);
2951 _dbus_assert (type == DBUS_TYPE_DOUBLE);
2953 return _dbus_demarshal_double (&real->message->body, real->message->byte_order,
2958 * Initializes an iterator for the array that the iterator
2959 * may point to. Note that you need to check that the iterator
2960 * points to an array prior to using this function.
2962 * The array element type is returned in array_type, and the array
2963 * iterator can only be used to get that type of data.
2965 * @param iter the iterator
2966 * @param array_iter pointer to an iterator to initialize
2967 * @param array_type gets set to the type of the array elements
2970 dbus_message_iter_init_array_iterator (DBusMessageIter *iter,
2971 DBusMessageIter *array_iter,
2974 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2975 DBusMessageRealIter *array_real = (DBusMessageRealIter *)array_iter;
2976 int type, pos, len_pos, len, array_type_pos;
2979 _dbus_return_if_fail (dbus_message_iter_check (real));
2981 pos = dbus_message_iter_get_data_start (real, &type);
2983 _dbus_assert (type == DBUS_TYPE_ARRAY);
2985 _array_type = iter_get_array_type (real, &array_type_pos);
2987 len_pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
2988 len = _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
2991 array_real->parent_iter = real;
2992 array_real->message = real->message;
2993 array_real->changed_stamp = real->message->changed_stamp;
2995 array_real->type = DBUS_MESSAGE_ITER_TYPE_ARRAY;
2996 array_real->pos = pos;
2997 array_real->end = pos + len;
2999 array_real->container_start = pos;
3000 array_real->container_length_pos = len_pos;
3001 array_real->wrote_dict_key = 0;
3002 array_real->array_type_pos = array_type_pos;
3003 array_real->array_type_done = TRUE;
3005 if (array_type != NULL)
3006 *array_type = _array_type;
3011 * Initializes an iterator for the dict that the iterator
3012 * may point to. Note that you need to check that the iterator
3013 * points to a dict prior to using this function.
3015 * @param iter the iterator
3016 * @param dict_iter pointer to an iterator to initialize
3019 dbus_message_iter_init_dict_iterator (DBusMessageIter *iter,
3020 DBusMessageIter *dict_iter)
3022 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3023 DBusMessageRealIter *dict_real = (DBusMessageRealIter *)dict_iter;
3024 int type, pos, len_pos, len;
3026 _dbus_return_if_fail (dbus_message_iter_check (real));
3028 pos = dbus_message_iter_get_data_start (real, &type);
3030 _dbus_assert (type == DBUS_TYPE_DICT);
3032 len_pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
3033 len = _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
3036 dict_real->parent_iter = real;
3037 dict_real->message = real->message;
3038 dict_real->changed_stamp = real->message->changed_stamp;
3040 dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
3041 dict_real->pos = pos;
3042 dict_real->end = pos + len;
3044 dict_real->container_start = pos;
3045 dict_real->container_length_pos = len_pos;
3046 dict_real->wrote_dict_key = 0;
3050 * Returns the byte array that the iterator may point to.
3051 * Note that you need to check that the iterator points
3052 * to a byte array prior to using this function.
3054 * @param iter the iterator
3055 * @param value return location for array values
3056 * @param len return location for length of byte array
3057 * @returns #TRUE on success
3060 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
3061 unsigned char **value,
3064 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3067 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3069 pos = dbus_message_iter_get_data_start (real, &type);
3071 _dbus_assert (type == DBUS_TYPE_ARRAY);
3073 type = iter_get_array_type (real, NULL);
3075 _dbus_assert (type == DBUS_TYPE_BYTE);
3077 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
3078 pos, NULL, value, len))
3085 * Returns the boolean array that the iterator may point to. Note that
3086 * you need to check that the iterator points to an array of the
3087 * correct type prior to using this function.
3089 * @param iter the iterator
3090 * @param value return location for the array
3091 * @param len return location for the array length
3092 * @returns #TRUE on success
3095 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
3096 unsigned char **value,
3099 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3102 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3104 pos = dbus_message_iter_get_data_start (real, &type);
3106 _dbus_assert (type == DBUS_TYPE_ARRAY);
3108 type = iter_get_array_type (real, NULL);
3110 _dbus_assert (type == DBUS_TYPE_BOOLEAN);
3112 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
3113 pos, NULL, value, len))
3120 * Returns the 32 bit signed integer array that the iterator may point
3121 * to. Note that you need to check that the iterator points to an
3122 * array of the correct type prior to using this function.
3124 * @param iter the iterator
3125 * @param value return location for the array
3126 * @param len return location for the array length
3127 * @returns #TRUE on success
3130 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
3131 dbus_int32_t **value,
3134 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3137 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3139 pos = dbus_message_iter_get_data_start (real, &type);
3141 _dbus_assert (type == DBUS_TYPE_ARRAY);
3143 type = iter_get_array_type (real, NULL);
3145 _dbus_assert (type == DBUS_TYPE_INT32);
3147 if (!_dbus_demarshal_int32_array (&real->message->body, real->message->byte_order,
3148 pos, NULL, value, len))
3155 * Returns the 32 bit unsigned integer array that the iterator may point
3156 * to. Note that you need to check that the iterator points to an
3157 * array of the correct type prior to using this function.
3159 * @param iter the iterator
3160 * @param value return location for the array
3161 * @param len return location for the array length
3162 * @returns #TRUE on success
3165 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
3166 dbus_uint32_t **value,
3169 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3172 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3174 pos = dbus_message_iter_get_data_start (real, &type);
3176 _dbus_assert (type == DBUS_TYPE_ARRAY);
3178 type = iter_get_array_type (real, NULL);
3179 _dbus_assert (type == DBUS_TYPE_UINT32);
3181 if (!_dbus_demarshal_uint32_array (&real->message->body, real->message->byte_order,
3182 pos, NULL, value, len))
3188 #ifdef DBUS_HAVE_INT64
3191 * Returns the 64 bit signed integer array that the iterator may point
3192 * to. Note that you need to check that the iterator points to an
3193 * array of the correct type prior to using this function.
3195 * This function only exists if #DBUS_HAVE_INT64 is defined.
3197 * @param iter the iterator
3198 * @param value return location for the array
3199 * @param len return location for the array length
3200 * @returns #TRUE on success
3203 dbus_message_iter_get_int64_array (DBusMessageIter *iter,
3204 dbus_int64_t **value,
3207 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3210 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3212 pos = dbus_message_iter_get_data_start (real, &type);
3214 _dbus_assert (type == DBUS_TYPE_ARRAY);
3216 type = iter_get_array_type (real, NULL);
3218 _dbus_assert (type == DBUS_TYPE_INT64);
3220 if (!_dbus_demarshal_int64_array (&real->message->body, real->message->byte_order,
3221 pos, NULL, value, len))
3228 * Returns the 64 bit unsigned integer array that the iterator may point
3229 * to. Note that you need to check that the iterator points to an
3230 * array of the correct type prior to using this function.
3232 * This function only exists if #DBUS_HAVE_INT64 is defined.
3234 * @param iter the iterator
3235 * @param value return location for the array
3236 * @param len return location for the array length
3237 * @returns #TRUE on success
3240 dbus_message_iter_get_uint64_array (DBusMessageIter *iter,
3241 dbus_uint64_t **value,
3244 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3247 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3249 pos = dbus_message_iter_get_data_start (real, &type);
3251 _dbus_assert (type == DBUS_TYPE_ARRAY);
3253 type = iter_get_array_type (real, NULL);
3254 _dbus_assert (type == DBUS_TYPE_UINT64);
3256 if (!_dbus_demarshal_uint64_array (&real->message->body, real->message->byte_order,
3257 pos, NULL, value, len))
3263 #endif /* DBUS_HAVE_INT64 */
3266 * Returns the double array that the iterator may point to. Note that
3267 * you need to check that the iterator points to an array of the
3268 * correct type prior to using this function.
3270 * @param iter the iterator
3271 * @param value return location for the array
3272 * @param len return location for the array length
3273 * @returns #TRUE on success
3276 dbus_message_iter_get_double_array (DBusMessageIter *iter,
3280 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3283 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3285 pos = dbus_message_iter_get_data_start (real, &type);
3287 _dbus_assert (type == DBUS_TYPE_ARRAY);
3289 type = iter_get_array_type (real, NULL);
3290 _dbus_assert (type == DBUS_TYPE_DOUBLE);
3292 if (!_dbus_demarshal_double_array (&real->message->body, real->message->byte_order,
3293 pos, NULL, value, len))
3300 * Returns the string array that the iterator may point to.
3301 * Note that you need to check that the iterator points
3302 * to a string array prior to using this function.
3304 * The returned value is a #NULL-terminated array of strings.
3305 * Each string is a separate malloc block, and the array
3306 * itself is a malloc block. You can free this type of
3307 * string array with dbus_free_string_array().
3309 * @param iter the iterator
3310 * @param value return location for string values
3311 * @param len return location for length of byte array
3312 * @returns #TRUE on success
3315 dbus_message_iter_get_string_array (DBusMessageIter *iter,
3319 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3322 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3324 pos = dbus_message_iter_get_data_start (real, &type);
3326 _dbus_assert (type == DBUS_TYPE_ARRAY);
3328 type = iter_get_array_type (real, NULL);
3329 _dbus_assert (type == DBUS_TYPE_STRING);
3331 if (!_dbus_demarshal_string_array (&real->message->body, real->message->byte_order,
3332 pos, NULL, value, len))
3340 * @todo FIXME to implement this _dbus_demarshal_object_path_array()
3341 * needs implementing
3343 * Returns the object path array that the iterator may point to.
3344 * Note that you need to check that the iterator points
3345 * to an object path array prior to using this function.
3347 * The returned value is a #NULL-terminated array of strings.
3348 * Each string is a separate malloc block, and the array
3349 * itself is a malloc block. You can free this type of
3350 * array with dbus_free_string_array().
3352 * @param iter the iterator
3353 * @param value return location for string values
3354 * @param len return location for length of byte array
3355 * @returns #TRUE on success
3358 dbus_message_iter_get_object_path_array (DBusMessageIter *iter,
3362 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3365 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3367 pos = dbus_message_iter_get_data_start (real, &type);
3369 _dbus_assert (type == DBUS_TYPE_ARRAY);
3371 type = iter_get_array_type (real, NULL);
3372 _dbus_assert (type == DBUS_TYPE_OBJECT_PATH);
3374 if (!_dbus_demarshal_object_path_array (&real->message->body, real->message->byte_order,
3375 pos, NULL, value, len))
3383 * Returns the key name fot the dict entry that an iterator
3384 * may point to. Note that you need to check that the iterator
3385 * points to a dict entry before using this function.
3387 * @see dbus_message_iter_init_dict_iterator
3388 * @param iter the message iter
3389 * @returns the key name
3392 dbus_message_iter_get_dict_key (DBusMessageIter *iter)
3394 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3396 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
3398 _dbus_assert (real->type == DBUS_MESSAGE_ITER_TYPE_DICT);
3400 return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
3405 * Initializes a DBusMessageIter pointing to the end of the
3406 * message. This iterator can be used to append data to the
3409 * @param message the message
3410 * @param iter pointer to an iterator to initialize
3413 dbus_message_append_iter_init (DBusMessage *message,
3414 DBusMessageIter *iter)
3416 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3418 _dbus_return_if_fail (message != NULL);
3419 _dbus_return_if_fail (iter != NULL);
3421 real->message = message;
3422 real->parent_iter = NULL;
3423 real->changed_stamp = message->changed_stamp;
3425 real->type = DBUS_MESSAGE_ITER_TYPE_MESSAGE;
3426 real->end = _dbus_string_get_length (&real->message->body);
3427 real->pos = real->end;
3429 real->container_length_pos = 0;
3430 real->wrote_dict_key = 0;
3433 #ifndef DBUS_DISABLE_CHECKS
3435 dbus_message_iter_append_check (DBusMessageRealIter *iter)
3439 _dbus_warn ("dbus iterator check failed: NULL iterator\n");
3443 if (iter->message->locked)
3445 _dbus_warn ("dbus iterator check failed: message is locked (has already been sent)\n");
3449 if (iter->changed_stamp != iter->message->changed_stamp)
3451 _dbus_warn ("dbus iterator check failed: invalid iterator, must re-initialize it after modifying the message");
3455 if (iter->pos != iter->end)
3457 _dbus_warn ("dbus iterator check failed: can only append at end of message");
3461 if (iter->pos != _dbus_string_get_length (&iter->message->body))
3463 _dbus_warn ("dbus iterator check failed: append pos not at end of message string");
3469 #endif /* DBUS_DISABLE_CHECKS */
3472 dbus_message_iter_append_type (DBusMessageRealIter *iter,
3478 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
3479 if (!_dbus_string_append_byte (&iter->message->signature, type))
3482 if (!_dbus_string_append_byte (&iter->message->body, type))
3484 _dbus_string_shorten (&iter->message->signature, 1);
3489 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
3490 data = _dbus_string_get_const_data_len (&iter->message->body,
3491 iter->array_type_pos, 1);
3494 _dbus_warn ("Appended element of wrong type for array\n");
3499 case DBUS_MESSAGE_ITER_TYPE_DICT:
3500 if (!iter->wrote_dict_key)
3502 _dbus_warn ("Appending dict data before key name\n");
3506 if (!_dbus_string_append_byte (&iter->message->body, type))
3512 _dbus_assert_not_reached ("Invalid iter type");
3520 dbus_message_iter_update_after_change (DBusMessageRealIter *iter)
3522 iter->changed_stamp = iter->message->changed_stamp;
3524 /* Set new end of iter */
3525 iter->end = _dbus_string_get_length (&iter->message->body);
3526 iter->pos = iter->end;
3528 /* Set container length */
3529 if (iter->type == DBUS_MESSAGE_ITER_TYPE_DICT ||
3530 (iter->type == DBUS_MESSAGE_ITER_TYPE_ARRAY && iter->array_type_done))
3531 _dbus_marshal_set_uint32 (&iter->message->body,
3532 iter->message->byte_order,
3533 iter->container_length_pos,
3534 iter->end - iter->container_start);
3536 if (iter->parent_iter)
3537 dbus_message_iter_update_after_change (iter->parent_iter);
3541 dbus_message_iter_append_done (DBusMessageRealIter *iter)
3543 iter->message->changed_stamp++;
3544 dbus_message_iter_update_after_change (iter);
3545 iter->wrote_dict_key = FALSE;
3549 * Appends a nil value to the message
3551 * @param iter an iterator pointing to the end of the message
3552 * @returns #TRUE on success
3555 dbus_message_iter_append_nil (DBusMessageIter *iter)
3557 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3559 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3561 if (!dbus_message_iter_append_type (real, DBUS_TYPE_NIL))
3564 dbus_message_iter_append_done (real);
3570 * Appends a boolean value to the message
3572 * @param iter an iterator pointing to the end of the message
3573 * @param value the boolean value
3574 * @returns #TRUE on success
3577 dbus_message_iter_append_boolean (DBusMessageIter *iter,
3580 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3582 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3584 if (!dbus_message_iter_append_type (real, DBUS_TYPE_BOOLEAN))
3587 if (!_dbus_string_append_byte (&real->message->body, (value != FALSE)))
3589 _dbus_string_set_length (&real->message->body, real->pos);
3593 dbus_message_iter_append_done (real);
3599 * Appends a byte to the message
3601 * @param iter an iterator pointing to the end of the message
3602 * @param value the byte value
3603 * @returns #TRUE on success
3606 dbus_message_iter_append_byte (DBusMessageIter *iter,
3607 unsigned char value)
3609 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3611 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3613 if (!dbus_message_iter_append_type (real, DBUS_TYPE_BYTE))
3616 if (!_dbus_string_append_byte (&real->message->body, value))
3618 _dbus_string_set_length (&real->message->body, real->pos);
3622 dbus_message_iter_append_done (real);
3629 * Appends a 32 bit signed integer to the message.
3631 * @param iter an iterator pointing to the end of the message
3632 * @param value the integer value
3633 * @returns #TRUE on success
3636 dbus_message_iter_append_int32 (DBusMessageIter *iter,
3639 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3641 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3643 if (!dbus_message_iter_append_type (real, DBUS_TYPE_INT32))
3646 if (!_dbus_marshal_int32 (&real->message->body, real->message->byte_order, value))
3648 _dbus_string_set_length (&real->message->body, real->pos);
3652 dbus_message_iter_append_done (real);
3658 * Appends a 32 bit unsigned integer to the message.
3660 * @param iter an iterator pointing to the end of the message
3661 * @param value the integer value
3662 * @returns #TRUE on success
3665 dbus_message_iter_append_uint32 (DBusMessageIter *iter,
3666 dbus_uint32_t value)
3668 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3670 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3672 if (!dbus_message_iter_append_type (real, DBUS_TYPE_UINT32))
3675 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, value))
3677 _dbus_string_set_length (&real->message->body, real->pos);
3681 dbus_message_iter_append_done (real);
3686 #ifdef DBUS_HAVE_INT64
3689 * Appends a 64 bit signed integer to the message.
3691 * This function only exists if #DBUS_HAVE_INT64 is defined.
3693 * @param iter an iterator pointing to the end of the message
3694 * @param value the integer value
3695 * @returns #TRUE on success
3698 dbus_message_iter_append_int64 (DBusMessageIter *iter,
3701 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3703 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3705 if (!dbus_message_iter_append_type (real, DBUS_TYPE_INT64))
3708 if (!_dbus_marshal_int64 (&real->message->body, real->message->byte_order, value))
3710 _dbus_string_set_length (&real->message->body, real->pos);
3714 dbus_message_iter_append_done (real);
3720 * Appends a 64 bit unsigned integer to the message.
3722 * This function only exists if #DBUS_HAVE_INT64 is defined.
3724 * @param iter an iterator pointing to the end of the message
3725 * @param value the integer value
3726 * @returns #TRUE on success
3729 dbus_message_iter_append_uint64 (DBusMessageIter *iter,
3730 dbus_uint64_t value)
3732 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3734 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3736 if (!dbus_message_iter_append_type (real, DBUS_TYPE_UINT64))
3739 if (!_dbus_marshal_uint64 (&real->message->body, real->message->byte_order, value))
3741 _dbus_string_set_length (&real->message->body, real->pos);
3745 dbus_message_iter_append_done (real);
3750 #endif /* DBUS_HAVE_INT64 */
3753 * Appends a double value to the message.
3755 * @param iter an iterator pointing to the end of the message
3756 * @param value the double value
3757 * @returns #TRUE on success
3760 dbus_message_iter_append_double (DBusMessageIter *iter,
3763 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3765 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3767 if (!dbus_message_iter_append_type (real, DBUS_TYPE_DOUBLE))
3770 if (!_dbus_marshal_double (&real->message->body, real->message->byte_order, value))
3772 _dbus_string_set_length (&real->message->body, real->pos);
3776 dbus_message_iter_append_done (real);
3782 * Appends a UTF-8 string to the message.
3784 * @todo add return_val_if_fail(UTF-8 is valid)
3786 * @param iter an iterator pointing to the end of the message
3787 * @param value the string
3788 * @returns #TRUE on success
3791 dbus_message_iter_append_string (DBusMessageIter *iter,
3794 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3796 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3798 if (!dbus_message_iter_append_type (real, DBUS_TYPE_STRING))
3801 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
3803 _dbus_string_set_length (&real->message->body, real->pos);
3807 dbus_message_iter_append_done (real);
3813 * Appends a custom type data chunk to the message. A custom
3814 * type is simply an arbitrary UTF-8 string used as a type
3815 * tag, plus an array of arbitrary bytes to be interpreted
3816 * according to the type tag.
3818 * @param iter an iterator pointing to the end of the message
3819 * @param name the name of the type
3820 * @param data the binary data used to store the value
3821 * @param len the length of the binary data in bytes
3822 * @returns #TRUE on success
3825 dbus_message_iter_append_custom (DBusMessageIter *iter,
3827 const unsigned char *data,
3830 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3832 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3834 if (!dbus_message_iter_append_type (real, DBUS_TYPE_CUSTOM))
3837 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, name))
3839 _dbus_string_set_length (&real->message->body, real->pos);
3843 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, data, len))
3845 _dbus_string_set_length (&real->message->body, real->pos);
3849 dbus_message_iter_append_done (real);
3856 * Appends a dict key name to the message. The iterator used
3857 * must point to a dict.
3859 * @param iter an iterator pointing to the end of the message
3860 * @param value the string
3861 * @returns #TRUE on success
3864 dbus_message_iter_append_dict_key (DBusMessageIter *iter,
3867 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3869 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3870 _dbus_assert (real->type == DBUS_MESSAGE_ITER_TYPE_DICT);
3872 if (real->wrote_dict_key)
3874 _dbus_warn ("Appendinging multiple dict key names\n");
3878 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
3883 dbus_message_iter_append_done (real);
3884 real->wrote_dict_key = TRUE;
3890 array_iter_type_mark_done (DBusMessageRealIter *iter)
3894 if (iter->type == DBUS_MESSAGE_ITER_TYPE_ARRAY)
3895 array_iter_type_mark_done (iter->parent_iter);
3899 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&iter->message->body),
3900 sizeof (dbus_uint32_t));
3902 /* Empty length for now, backfill later */
3903 if (!_dbus_marshal_uint32 (&iter->message->body, iter->message->byte_order, 0))
3905 _dbus_string_set_length (&iter->message->body, iter->pos);
3909 iter->container_start = _dbus_string_get_length (&iter->message->body);
3910 iter->container_length_pos = len_pos;
3911 iter->array_type_done = TRUE;
3917 append_array_type (DBusMessageRealIter *real,
3919 dbus_bool_t *array_type_done,
3920 int *array_type_pos)
3922 int existing_element_type;
3924 if (!dbus_message_iter_append_type (real, DBUS_TYPE_ARRAY))
3927 if (real->type == DBUS_MESSAGE_ITER_TYPE_ARRAY &&
3928 real->array_type_done)
3930 existing_element_type = iter_get_array_type (real, array_type_pos);
3931 if (existing_element_type != element_type)
3933 _dbus_warn ("Appending array of %s, when expecting array of %s\n",
3934 _dbus_type_to_string (element_type),
3935 _dbus_type_to_string (existing_element_type));
3936 _dbus_string_set_length (&real->message->body, real->pos);
3939 if (array_type_done != NULL)
3940 *array_type_done = TRUE;
3944 if (array_type_pos != NULL)
3945 *array_type_pos = _dbus_string_get_length (&real->message->body);
3948 if (!_dbus_string_append_byte (&real->message->signature, element_type))
3950 _dbus_string_set_length (&real->message->body, real->pos);
3954 /* Append element type */
3955 if (!_dbus_string_append_byte (&real->message->body, element_type))
3957 _dbus_string_shorten (&real->message->signature, 1);
3958 _dbus_string_set_length (&real->message->body, real->pos);
3962 if (array_type_done != NULL)
3963 *array_type_done = element_type != DBUS_TYPE_ARRAY;
3965 if (element_type != DBUS_TYPE_ARRAY &&
3966 !array_iter_type_mark_done (real))
3968 _dbus_string_shorten (&real->message->signature, 1);
3977 * Appends an array to the message and initializes an iterator that
3978 * can be used to append to the array.
3980 * @param iter an iterator pointing to the end of the message
3981 * @param array_iter pointer to an iter that will be initialized
3982 * @param element_type the type of the array elements
3983 * @returns #TRUE on success
3986 dbus_message_iter_append_array (DBusMessageIter *iter,
3987 DBusMessageIter *array_iter,
3990 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3991 DBusMessageRealIter *array_real = (DBusMessageRealIter *)array_iter;
3994 dbus_bool_t array_type_done;
3996 if (element_type == DBUS_TYPE_NIL)
3998 _dbus_warn ("Can't create NIL arrays\n");
4002 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4004 if (!append_array_type (real, element_type, &array_type_done, &array_type_pos))
4007 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
4009 if (array_type_done)
4011 /* Empty length for now, backfill later */
4012 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
4014 _dbus_string_set_length (&real->message->body, real->pos);
4019 array_real->parent_iter = real;
4020 array_real->message = real->message;
4021 array_real->changed_stamp = real->message->changed_stamp;
4023 array_real->type = DBUS_MESSAGE_ITER_TYPE_ARRAY;
4024 array_real->pos = _dbus_string_get_length (&real->message->body);
4025 array_real->end = array_real->end;
4027 array_real->container_start = array_real->pos;
4028 array_real->container_length_pos = len_pos;
4029 array_real->wrote_dict_key = 0;
4030 array_real->array_type_done = array_type_done;
4031 array_real->array_type_pos = array_type_pos;
4033 dbus_message_iter_append_done (array_real);
4039 * Appends a dict to the message and initializes an iterator that
4040 * can be used to append to the dict.
4042 * @param iter an iterator pointing to the end of the message
4043 * @param dict_iter pointer to an iter that will be initialized
4044 * @returns #TRUE on success
4047 dbus_message_iter_append_dict (DBusMessageIter *iter,
4048 DBusMessageIter *dict_iter)
4050 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4051 DBusMessageRealIter *dict_real = (DBusMessageRealIter *)dict_iter;
4054 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4056 if (!dbus_message_iter_append_type (real, DBUS_TYPE_DICT))
4059 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
4061 /* Empty length for now, backfill later */
4062 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
4064 _dbus_string_set_length (&real->message->body, real->pos);
4068 dict_real->parent_iter = real;
4069 dict_real->message = real->message;
4070 dict_real->changed_stamp = real->message->changed_stamp;
4072 dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
4073 dict_real->pos = _dbus_string_get_length (&real->message->body);
4074 dict_real->end = dict_real->end;
4076 dict_real->container_start = dict_real->pos;
4077 dict_real->container_length_pos = len_pos;
4078 dict_real->wrote_dict_key = 0;
4080 dbus_message_iter_append_done (dict_real);
4087 * Appends a boolean array to the message.
4089 * @param iter an iterator pointing to the end of the message
4090 * @param value the array
4091 * @param len the length of the array
4092 * @returns #TRUE on success
4095 dbus_message_iter_append_boolean_array (DBusMessageIter *iter,
4096 unsigned const char *value,
4099 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4101 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4103 if (!append_array_type (real, DBUS_TYPE_BOOLEAN, NULL, NULL))
4106 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, value, len))
4108 _dbus_string_set_length (&real->message->body, real->pos);
4112 dbus_message_iter_append_done (real);
4118 * Appends a 32 bit signed integer array to the message.
4120 * @param iter an iterator pointing to the end of the message
4121 * @param value the array
4122 * @param len the length of the array
4123 * @returns #TRUE on success
4126 dbus_message_iter_append_int32_array (DBusMessageIter *iter,
4127 const dbus_int32_t *value,
4130 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4132 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4134 if (!append_array_type (real, DBUS_TYPE_INT32, NULL, NULL))
4137 if (!_dbus_marshal_int32_array (&real->message->body, real->message->byte_order, value, len))
4139 _dbus_string_set_length (&real->message->body, real->pos);
4143 dbus_message_iter_append_done (real);
4149 * Appends a 32 bit unsigned integer array to the message.
4151 * @param iter an iterator pointing to the end of the message
4152 * @param value the array
4153 * @param len the length of the array
4154 * @returns #TRUE on success
4157 dbus_message_iter_append_uint32_array (DBusMessageIter *iter,
4158 const dbus_uint32_t *value,
4161 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4163 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4165 if (!append_array_type (real, DBUS_TYPE_UINT32, NULL, NULL))
4168 if (!_dbus_marshal_uint32_array (&real->message->body, real->message->byte_order, value, len))
4170 _dbus_string_set_length (&real->message->body, real->pos);
4174 dbus_message_iter_append_done (real);
4179 #ifdef DBUS_HAVE_INT64
4182 * Appends a 64 bit signed integer array to the message.
4184 * This function only exists if #DBUS_HAVE_INT64 is defined.
4186 * @param iter an iterator pointing to the end of the message
4187 * @param value the array
4188 * @param len the length of the array
4189 * @returns #TRUE on success
4192 dbus_message_iter_append_int64_array (DBusMessageIter *iter,
4193 const dbus_int64_t *value,
4196 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4198 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4200 if (!append_array_type (real, DBUS_TYPE_INT64, NULL, NULL))
4203 if (!_dbus_marshal_int64_array (&real->message->body, real->message->byte_order, value, len))
4205 _dbus_string_set_length (&real->message->body, real->pos);
4209 dbus_message_iter_append_done (real);
4215 * Appends a 64 bit unsigned integer array to the message.
4217 * This function only exists if #DBUS_HAVE_INT64 is defined.
4219 * @param iter an iterator pointing to the end of the message
4220 * @param value the array
4221 * @param len the length of the array
4222 * @returns #TRUE on success
4225 dbus_message_iter_append_uint64_array (DBusMessageIter *iter,
4226 const dbus_uint64_t *value,
4229 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4231 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4233 if (!append_array_type (real, DBUS_TYPE_UINT64, NULL, NULL))
4236 if (!_dbus_marshal_uint64_array (&real->message->body, real->message->byte_order, value, len))
4238 _dbus_string_set_length (&real->message->body, real->pos);
4242 dbus_message_iter_append_done (real);
4246 #endif /* DBUS_HAVE_INT64 */
4249 * Appends a double array to the message.
4251 * @param iter an iterator pointing to the end of the message
4252 * @param value the array
4253 * @param len the length of the array
4254 * @returns #TRUE on success
4257 dbus_message_iter_append_double_array (DBusMessageIter *iter,
4258 const double *value,
4261 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4263 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4265 if (!append_array_type (real, DBUS_TYPE_DOUBLE, NULL, NULL))
4268 if (!_dbus_marshal_double_array (&real->message->body, real->message->byte_order, value, len))
4270 _dbus_string_set_length (&real->message->body, real->pos);
4274 dbus_message_iter_append_done (real);
4280 * Appends a byte array to the message.
4282 * @param iter an iterator pointing to the end of the message
4283 * @param value the array
4284 * @param len the length of the array
4285 * @returns #TRUE on success
4288 dbus_message_iter_append_byte_array (DBusMessageIter *iter,
4289 unsigned const char *value,
4292 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4294 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4296 if (!append_array_type (real, DBUS_TYPE_BYTE, NULL, NULL))
4299 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, value, len))
4301 _dbus_string_set_length (&real->message->body, real->pos);
4305 dbus_message_iter_append_done (real);
4311 * Appends a string array to the message.
4313 * @param iter an iterator pointing to the end of the message
4314 * @param value the array
4315 * @param len the length of the array
4316 * @returns #TRUE on success
4319 dbus_message_iter_append_string_array (DBusMessageIter *iter,
4323 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4325 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4327 if (!append_array_type (real, DBUS_TYPE_STRING, NULL, NULL))
4330 if (!_dbus_marshal_string_array (&real->message->body, real->message->byte_order, value, len))
4332 _dbus_string_set_length (&real->message->body, real->pos);
4336 dbus_message_iter_append_done (real);
4342 * Sets the message sender.
4344 * @param message the message
4345 * @param sender the sender
4346 * @returns #FALSE if not enough memory
4349 dbus_message_set_sender (DBusMessage *message,
4352 _dbus_return_val_if_fail (message != NULL, FALSE);
4353 _dbus_return_val_if_fail (!message->locked, FALSE);
4355 return set_string_field (message,
4356 DBUS_HEADER_FIELD_SENDER_SERVICE,
4362 * Sets a flag indicating that the message does not want a reply; if
4363 * this flag is set, the other end of the connection may (but is not
4364 * required to) optimize by not sending method return or error
4365 * replies. If this flag is set, there is no way to know whether the
4366 * message successfully arrived at the remote end.
4368 * @param message the message
4369 * @param no_reply #TRUE if no reply is desired
4372 dbus_message_set_no_reply (DBusMessage *message,
4373 dbus_bool_t no_reply)
4377 _dbus_return_if_fail (message != NULL);
4378 _dbus_return_if_fail (!message->locked);
4380 header = _dbus_string_get_data_len (&message->header, FLAGS_OFFSET, 1);
4383 *header |= DBUS_HEADER_FLAG_NO_REPLY_EXPECTED;
4385 *header &= ~DBUS_HEADER_FLAG_NO_REPLY_EXPECTED;
4389 * Returns #TRUE if the message does not expect
4392 * @param message the message
4393 * @returns #TRUE if the message sender isn't waiting for a reply
4396 dbus_message_get_no_reply (DBusMessage *message)
4400 _dbus_return_val_if_fail (message != NULL, FALSE);
4402 header = _dbus_string_get_const_data_len (&message->header, FLAGS_OFFSET, 1);
4404 return (*header & DBUS_HEADER_FLAG_NO_REPLY_EXPECTED) != 0;
4408 * Gets the service which originated this message,
4409 * or #NULL if unknown or inapplicable.
4411 * @param message the message
4412 * @returns the service name or #NULL
4415 dbus_message_get_sender (DBusMessage *message)
4417 _dbus_return_val_if_fail (message != NULL, NULL);
4419 return get_string_field (message,
4420 DBUS_HEADER_FIELD_SENDER_SERVICE,
4425 * Gets the type signature of the message, i.e. the arguments in the
4426 * message payload. The signature includes only "in" arguments for
4427 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
4428 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
4429 * what you might expect (it does not include the signature of the
4430 * entire C++-style method).
4432 * The signature is a string made up of type codes such
4433 * as #DBUS_TYPE_STRING. The string is terminated with nul
4434 * (nul is also the value of #DBUS_TYPE_INVALID).
4436 * @param message the message
4437 * @returns the type signature
4440 dbus_message_get_signature (DBusMessage *message)
4442 _dbus_return_val_if_fail (message != NULL, NULL);
4444 return _dbus_string_get_const_data (&message->signature);
4448 _dbus_message_has_type_interface_member (DBusMessage *message,
4450 const char *interface,
4455 _dbus_assert (message != NULL);
4456 _dbus_assert (interface != NULL);
4457 _dbus_assert (method != NULL);
4459 if (dbus_message_get_type (message) != type)
4462 /* Optimize by checking the short method name first
4463 * instead of the longer interface name
4466 n = dbus_message_get_member (message);
4468 if (n && strcmp (n, method) == 0)
4470 n = dbus_message_get_interface (message);
4472 if (n && strcmp (n, interface) == 0)
4480 * Checks whether the message is a method call with the given
4481 * interface and member fields. If the message is not
4482 * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or member field,
4485 * @param message the message
4486 * @param interface the name to check (must not be #NULL)
4487 * @param method the name to check (must not be #NULL)
4489 * @returns #TRUE if the message is the specified method call
4492 dbus_message_is_method_call (DBusMessage *message,
4493 const char *interface,
4496 _dbus_return_val_if_fail (message != NULL, FALSE);
4497 _dbus_return_val_if_fail (interface != NULL, FALSE);
4498 _dbus_return_val_if_fail (method != NULL, FALSE);
4500 return _dbus_message_has_type_interface_member (message,
4501 DBUS_MESSAGE_TYPE_METHOD_CALL,
4506 * Checks whether the message is a signal with the given
4507 * interface and member fields. If the message is not
4508 * #DBUS_MESSAGE_TYPE_SIGNAL, or has a different interface or member field,
4511 * @param message the message
4512 * @param interface the name to check (must not be #NULL)
4513 * @param signal_name the name to check (must not be #NULL)
4515 * @returns #TRUE if the message is the specified signal
4518 dbus_message_is_signal (DBusMessage *message,
4519 const char *interface,
4520 const char *signal_name)
4522 _dbus_return_val_if_fail (message != NULL, FALSE);
4523 _dbus_return_val_if_fail (interface != NULL, FALSE);
4524 _dbus_return_val_if_fail (signal_name != NULL, FALSE);
4526 return _dbus_message_has_type_interface_member (message,
4527 DBUS_MESSAGE_TYPE_SIGNAL,
4528 interface, signal_name);
4532 * Checks whether the message is an error reply with the given error
4533 * name. If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
4534 * different name, returns #FALSE.
4536 * @param message the message
4537 * @param error_name the name to check (must not be #NULL)
4539 * @returns #TRUE if the message is the specified error
4542 dbus_message_is_error (DBusMessage *message,
4543 const char *error_name)
4547 _dbus_return_val_if_fail (message != NULL, FALSE);
4548 _dbus_return_val_if_fail (error_name != NULL, FALSE);
4550 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
4553 n = dbus_message_get_error_name (message);
4555 if (n && strcmp (n, error_name) == 0)
4562 * Checks whether the message was sent to the given service. If the
4563 * message has no service specified or has a different name, returns
4566 * @param message the message
4567 * @param service the service to check (must not be #NULL)
4569 * @returns #TRUE if the message has the given destination service
4572 dbus_message_has_destination (DBusMessage *message,
4573 const char *service)
4577 _dbus_return_val_if_fail (message != NULL, FALSE);
4578 _dbus_return_val_if_fail (service != NULL, FALSE);
4580 s = dbus_message_get_destination (message);
4582 if (s && strcmp (s, service) == 0)
4589 * Checks whether the message has the given service as its sender. If
4590 * the message has no sender specified or has a different sender,
4591 * returns #FALSE. Note that if a peer application owns multiple
4592 * services, its messages will have only one of those services as the
4593 * sender (usually the base service). So you can't use this
4594 * function to prove the sender didn't own service Foo, you can
4595 * only use it to prove that it did.
4597 * @param message the message
4598 * @param service the service to check (must not be #NULL)
4600 * @returns #TRUE if the message has the given origin service
4603 dbus_message_has_sender (DBusMessage *message,
4604 const char *service)
4608 _dbus_return_val_if_fail (message != NULL, FALSE);
4609 _dbus_return_val_if_fail (service != NULL, FALSE);
4611 s = dbus_message_get_sender (message);
4613 if (s && strcmp (s, service) == 0)
4620 * Checks whether the message has the given signature;
4621 * see dbus_message_get_signature() for more details on
4622 * what the signature looks like.
4624 * @param message the message
4625 * @param signature typecode array
4626 * @returns #TRUE if message has the given signature
4629 dbus_message_has_signature (DBusMessage *message,
4630 const char *signature)
4632 _dbus_return_val_if_fail (message != NULL, FALSE);
4633 _dbus_return_val_if_fail (signature != NULL, FALSE);
4635 return _dbus_string_equal_c_str (&message->signature, signature);
4639 * Sets a #DBusError based on the contents of the given
4640 * message. The error is only set if the message
4641 * is an error message, as in DBUS_MESSAGE_TYPE_ERROR.
4642 * The name of the error is set to the name of the message,
4643 * and the error message is set to the first argument
4644 * if the argument exists and is a string.
4646 * The return value indicates whether the error was set (the error is
4647 * set if and only if the message is an error message).
4648 * So you can check for an error reply and convert it to DBusError
4651 * @param error the error to set
4652 * @param message the message to set it from
4653 * @returns #TRUE if dbus_message_get_is_error() returns #TRUE for the message
4656 dbus_set_error_from_message (DBusError *error,
4657 DBusMessage *message)
4661 _dbus_return_val_if_fail (message != NULL, FALSE);
4662 _dbus_return_val_if_error_is_set (error, FALSE);
4664 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
4668 dbus_message_get_args (message, NULL,
4669 DBUS_TYPE_STRING, &str,
4672 dbus_set_error (error, dbus_message_get_error_name (message),
4673 str ? "%s" : NULL, str);
4683 * @addtogroup DBusMessageInternals
4688 * @typedef DBusMessageLoader
4690 * The DBusMessageLoader object encapsulates the process of converting
4691 * a byte stream into a series of DBusMessage. It buffers the incoming
4692 * bytes as efficiently as possible, and generates a queue of
4693 * messages. DBusMessageLoader is typically used as part of a
4694 * DBusTransport implementation. The DBusTransport then hands off
4695 * the loaded messages to a DBusConnection, making the messages
4696 * visible to the application.
4698 * @todo write tests for break-loader that a) randomly delete header
4699 * fields and b) set string fields to zero-length and other funky
4704 /* we definitely use signed ints for sizes, so don't exceed
4705 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
4706 * over 128M is pretty nuts anyhow.
4710 * The maximum sane message size.
4712 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
4715 * Implementation details of DBusMessageLoader.
4716 * All members are private.
4718 struct DBusMessageLoader
4720 int refcount; /**< Reference count. */
4722 DBusString data; /**< Buffered data */
4724 DBusList *messages; /**< Complete messages. */
4726 long max_message_size; /**< Maximum size of a message */
4728 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
4730 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
4734 * The initial buffer size of the message loader.
4736 * @todo this should be based on min header size plus some average
4737 * body size, or something. Or rather, the min header size only, if we
4738 * want to try to read only the header, store that in a DBusMessage,
4739 * then read only the body and store that, etc., depends on
4740 * how we optimize _dbus_message_loader_get_buffer() and what
4741 * the exact message format is.
4743 #define INITIAL_LOADER_DATA_LEN 32
4746 * Creates a new message loader. Returns #NULL if memory can't
4749 * @returns new loader, or #NULL.
4752 _dbus_message_loader_new (void)
4754 DBusMessageLoader *loader;
4756 loader = dbus_new0 (DBusMessageLoader, 1);
4760 loader->refcount = 1;
4762 /* Try to cap message size at something that won't *totally* hose
4763 * the system if we have a couple of them.
4765 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
4767 if (!_dbus_string_init (&loader->data))
4773 /* preallocate the buffer for speed, ignore failure */
4774 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
4775 _dbus_string_set_length (&loader->data, 0);
4781 * Increments the reference count of the loader.
4783 * @param loader the loader.
4786 _dbus_message_loader_ref (DBusMessageLoader *loader)
4788 loader->refcount += 1;
4792 * Decrements the reference count of the loader and finalizes the
4793 * loader when the count reaches zero.
4795 * @param loader the loader.
4798 _dbus_message_loader_unref (DBusMessageLoader *loader)
4800 loader->refcount -= 1;
4801 if (loader->refcount == 0)
4803 _dbus_list_foreach (&loader->messages,
4804 (DBusForeachFunction) dbus_message_unref,
4806 _dbus_list_clear (&loader->messages);
4807 _dbus_string_free (&loader->data);
4813 * Gets the buffer to use for reading data from the network. Network
4814 * data is read directly into an allocated buffer, which is then used
4815 * in the DBusMessage, to avoid as many extra memcpy's as possible.
4816 * The buffer must always be returned immediately using
4817 * _dbus_message_loader_return_buffer(), even if no bytes are
4818 * successfully read.
4820 * @todo this function can be a lot more clever. For example
4821 * it can probably always return a buffer size to read exactly
4822 * the body of the next message, thus avoiding any memory wastage
4825 * @todo we need to enforce a max length on strings in header fields.
4827 * @param loader the message loader.
4828 * @param buffer the buffer
4831 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
4832 DBusString **buffer)
4834 _dbus_assert (!loader->buffer_outstanding);
4836 *buffer = &loader->data;
4838 loader->buffer_outstanding = TRUE;
4842 * The smallest header size that can occur.
4843 * (It won't be valid)
4845 #define DBUS_MINIMUM_HEADER_SIZE 16
4848 decode_string_field (const DBusString *data,
4850 HeaderField *header_field,
4851 DBusString *field_data,
4855 int string_data_pos;
4857 _dbus_assert (header_field != NULL);
4858 _dbus_assert (field_data != NULL);
4860 if (header_field->name_offset >= 0)
4862 _dbus_verbose ("%s field provided twice\n",
4863 _dbus_header_field_to_string (field));
4867 if (type != DBUS_TYPE_STRING)
4869 _dbus_verbose ("%s field has wrong type %s\n",
4870 _dbus_header_field_to_string (field),
4871 _dbus_type_to_string (type));
4875 /* skip padding after typecode, skip string length;
4876 * we assume that the string arg has already been validated
4877 * for sanity and UTF-8
4879 string_data_pos = _DBUS_ALIGN_VALUE (pos, 4) + 4;
4880 _dbus_assert (string_data_pos < _dbus_string_get_length (data));
4882 _dbus_string_init_const (field_data,
4883 _dbus_string_get_const_data (data) + string_data_pos);
4885 header_field->name_offset = pos;
4886 header_field->value_offset = _DBUS_ALIGN_VALUE (pos, 4);
4889 _dbus_verbose ("Found field %s at offset %d\n",
4890 _dbus_header_field_to_string (field),
4891 header_field->value_offset);
4897 /* FIXME because the service/interface/member/error names are already
4898 * validated to be in the particular ASCII subset, UTF-8 validating
4899 * them could be skipped as a probably-interesting optimization.
4900 * The UTF-8 validation definitely shows up in profiles.
4903 decode_header_data (const DBusString *data,
4907 HeaderField fields[DBUS_HEADER_FIELD_LAST + 1],
4908 int *message_padding)
4910 DBusString field_data;
4916 if (header_len < 16)
4918 _dbus_verbose ("Header length %d is too short\n", header_len);
4923 while (i <= DBUS_HEADER_FIELD_LAST)
4925 fields[i].name_offset = -1;
4926 fields[i].value_offset = -1;
4931 while (pos < header_len)
4933 field = _dbus_string_get_byte (data, pos);
4934 if (field == DBUS_HEADER_FIELD_INVALID)
4935 break; /* Must be padding */
4938 if (!_dbus_marshal_validate_type (data, pos, &type, &pos))
4940 _dbus_verbose ("Failed to validate type of named header field pos = %d\n",
4945 if (!_dbus_marshal_validate_arg (data, byte_order, 0, type, -1, pos, &new_pos))
4947 _dbus_verbose ("Failed to validate argument to named header field pos = %d\n",
4952 if (new_pos > header_len)
4954 _dbus_verbose ("Named header field tries to extend beyond header length\n");
4960 case DBUS_HEADER_FIELD_SERVICE:
4961 if (!decode_string_field (data, field, &fields[field],
4962 &field_data, pos, type))
4965 if (!_dbus_string_validate_service (&field_data, 0,
4966 _dbus_string_get_length (&field_data)))
4968 _dbus_verbose ("service field has invalid content \"%s\"\n",
4969 _dbus_string_get_const_data (&field_data));
4974 case DBUS_HEADER_FIELD_INTERFACE:
4975 if (!decode_string_field (data, field, &fields[field],
4976 &field_data, pos, type))
4979 if (!_dbus_string_validate_interface (&field_data, 0,
4980 _dbus_string_get_length (&field_data)))
4982 _dbus_verbose ("interface field has invalid content \"%s\"\n",
4983 _dbus_string_get_const_data (&field_data));
4987 if (_dbus_string_equal_c_str (&field_data,
4988 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL))
4990 _dbus_verbose ("Message is on the local interface\n");
4995 case DBUS_HEADER_FIELD_MEMBER:
4996 if (!decode_string_field (data, field, &fields[field],
4997 &field_data, pos, type))
5000 if (!_dbus_string_validate_member (&field_data, 0,
5001 _dbus_string_get_length (&field_data)))
5003 _dbus_verbose ("member field has invalid content \"%s\"\n",
5004 _dbus_string_get_const_data (&field_data));
5009 case DBUS_HEADER_FIELD_ERROR_NAME:
5010 if (!decode_string_field (data, field, &fields[field],
5011 &field_data, pos, type))
5014 if (!_dbus_string_validate_error_name (&field_data, 0,
5015 _dbus_string_get_length (&field_data)))
5017 _dbus_verbose ("error-name field has invalid content \"%s\"\n",
5018 _dbus_string_get_const_data (&field_data));
5023 case DBUS_HEADER_FIELD_SENDER_SERVICE:
5024 if (!decode_string_field (data, field, &fields[field],
5025 &field_data, pos, type))
5028 if (!_dbus_string_validate_service (&field_data, 0,
5029 _dbus_string_get_length (&field_data)))
5031 _dbus_verbose ("sender-service field has invalid content \"%s\"\n",
5032 _dbus_string_get_const_data (&field_data));
5037 case DBUS_HEADER_FIELD_PATH:
5039 /* Path was already validated as part of standard
5040 * type validation, since there's an OBJECT_PATH
5044 if (fields[field].name_offset >= 0)
5046 _dbus_verbose ("path field provided twice\n");
5049 if (type != DBUS_TYPE_OBJECT_PATH)
5051 _dbus_verbose ("path field has wrong type\n");
5055 fields[field].name_offset = pos;
5056 fields[field].value_offset = _DBUS_ALIGN_VALUE (pos, 4);
5058 /* No forging signals from the local path */
5061 s = _dbus_string_get_const_data_len (data,
5062 fields[field].value_offset,
5063 _dbus_string_get_length (data) -
5064 fields[field].value_offset);
5065 if (strcmp (s, DBUS_PATH_ORG_FREEDESKTOP_LOCAL) == 0)
5067 _dbus_verbose ("Message is on the local path\n");
5072 _dbus_verbose ("Found path at offset %d\n",
5073 fields[field].value_offset);
5076 case DBUS_HEADER_FIELD_REPLY_SERIAL:
5077 if (fields[field].name_offset >= 0)
5079 _dbus_verbose ("reply field provided twice\n");
5083 if (type != DBUS_TYPE_UINT32)
5085 _dbus_verbose ("reply field has wrong type\n");
5089 fields[field].name_offset = pos;
5090 fields[field].value_offset = _DBUS_ALIGN_VALUE (pos, 4);
5092 _dbus_verbose ("Found reply serial %u at offset %d\n",
5093 _dbus_demarshal_uint32 (data,
5095 fields[field].value_offset,
5097 fields[field].value_offset);
5101 _dbus_verbose ("Ignoring an unknown header field: %d at offset %d\n",
5108 if (pos < header_len)
5110 /* Alignment padding, verify that it's nul */
5111 if ((header_len - pos) >= 8)
5113 _dbus_verbose ("too much header alignment padding\n");
5117 if (!_dbus_string_validate_nul (data,
5118 pos, (header_len - pos)))
5120 _dbus_verbose ("header alignment padding is not nul\n");
5125 /* Depending on message type, enforce presence of certain fields. */
5126 switch (message_type)
5128 case DBUS_MESSAGE_TYPE_SIGNAL:
5129 case DBUS_MESSAGE_TYPE_METHOD_CALL:
5130 if (fields[DBUS_HEADER_FIELD_PATH].value_offset < 0)
5132 _dbus_verbose ("No path field provided\n");
5135 /* FIXME make this optional, only for method calls */
5136 if (fields[DBUS_HEADER_FIELD_INTERFACE].value_offset < 0)
5138 _dbus_verbose ("No interface field provided\n");
5141 if (fields[DBUS_HEADER_FIELD_MEMBER].value_offset < 0)
5143 _dbus_verbose ("No member field provided\n");
5147 case DBUS_MESSAGE_TYPE_ERROR:
5148 if (fields[DBUS_HEADER_FIELD_ERROR_NAME].value_offset < 0)
5150 _dbus_verbose ("No error-name field provided\n");
5153 if (fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset < 0)
5155 _dbus_verbose ("No reply serial field provided in error\n");
5159 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
5160 if (fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset < 0)
5162 _dbus_verbose ("No reply serial field provided in method return\n");
5167 /* An unknown type, spec requires us to ignore it */
5171 if (message_padding)
5172 *message_padding = header_len - pos;
5178 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
5179 * indicating to the loader how many bytes of the buffer were filled
5180 * in. This function must always be called, even if no bytes were
5181 * successfully read.
5183 * @param loader the loader.
5184 * @param buffer the buffer.
5185 * @param bytes_read number of bytes that were read into the buffer.
5188 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
5192 _dbus_assert (loader->buffer_outstanding);
5193 _dbus_assert (buffer == &loader->data);
5195 loader->buffer_outstanding = FALSE;
5199 load_one_message (DBusMessageLoader *loader,
5205 DBusMessage *message;
5206 HeaderField fields[DBUS_HEADER_FIELD_LAST + 1];
5216 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
5219 if (!decode_header_data (&loader->data,
5220 header_len, byte_order,
5222 fields, &header_padding))
5224 _dbus_verbose ("Header was invalid\n");
5225 loader->corrupted = TRUE;
5229 next_arg = header_len;
5230 while (next_arg < (header_len + body_len))
5233 int prev = next_arg;
5235 if (!_dbus_marshal_validate_type (&loader->data, next_arg,
5238 _dbus_verbose ("invalid typecode at offset %d\n", prev);
5239 loader->corrupted = TRUE;
5243 if (!_dbus_marshal_validate_arg (&loader->data,
5250 _dbus_verbose ("invalid type data at %d, next_arg\n", next_arg);
5251 loader->corrupted = TRUE;
5255 _dbus_assert (next_arg > prev);
5258 if (next_arg > (header_len + body_len))
5260 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
5261 next_arg, header_len, body_len,
5262 header_len + body_len);
5263 loader->corrupted = TRUE;
5267 message = dbus_message_new_empty_header ();
5268 if (message == NULL)
5270 _dbus_verbose ("Failed to allocate empty message\n");
5275 message->byte_order = byte_order;
5276 message->header_padding = header_padding;
5278 /* Copy in the offsets we found */
5280 while (i <= DBUS_HEADER_FIELD_LAST)
5282 message->header_fields[i] = fields[i];
5286 if (!_dbus_list_append (&loader->messages, message))
5288 _dbus_verbose ("Failed to append new message to loader queue\n");
5293 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
5294 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
5296 _dbus_assert (_dbus_string_get_length (&loader->data) >=
5297 (header_len + body_len));
5299 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
5301 _dbus_verbose ("Failed to move header into new message\n");
5306 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
5308 _dbus_verbose ("Failed to move body into new message\n");
5314 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
5315 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
5317 /* Fill in caches (we checked the types of these fields
5320 message->reply_serial = get_uint_field (message,
5321 DBUS_HEADER_FIELD_REPLY_SERIAL);
5323 message->client_serial = _dbus_demarshal_uint32 (&message->header,
5324 message->byte_order,
5325 CLIENT_SERIAL_OFFSET,
5327 if (message->client_serial == 0 ||
5328 (message->header_fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset >= 0 && message->reply_serial == 0))
5330 _dbus_verbose ("client_serial = %d reply_serial = %d, one of these no good\n",
5331 message->client_serial,
5332 message->reply_serial);
5334 loader->corrupted = TRUE;
5338 /* Fill in signature (FIXME should do this during validation,
5339 * but I didn't want to spend time on it since we want to change
5340 * the wire format to contain the signature anyway)
5343 DBusMessageIter iter;
5345 dbus_message_iter_init (message, &iter);
5351 t = dbus_message_iter_get_arg_type (&iter);
5352 if (t == DBUS_TYPE_INVALID)
5355 if (!_dbus_string_append_byte (&message->signature,
5358 _dbus_verbose ("failed to append type byte to signature\n");
5363 if (t == DBUS_TYPE_ARRAY)
5365 DBusMessageIter child_iter;
5370 while (array_type == DBUS_TYPE_ARRAY)
5372 DBusMessageIter parent_iter = child_iter;
5373 dbus_message_iter_init_array_iterator (&parent_iter,
5377 if (!_dbus_string_append_byte (&message->signature,
5380 _dbus_verbose ("failed to append array type byte to signature\n");
5388 while (dbus_message_iter_next (&iter));
5391 _dbus_verbose ("Loaded message %p\n", message);
5393 _dbus_assert (!oom);
5394 _dbus_assert (!loader->corrupted);
5402 if (message != NULL)
5404 /* Put the data back so we can try again later if it was an OOM issue */
5405 if (_dbus_string_get_length (&message->body) > 0)
5409 result = _dbus_string_copy_len (&message->body, 0, body_len,
5412 _dbus_assert (result); /* because DBusString never reallocs smaller */
5415 if (_dbus_string_get_length (&message->header) > 0)
5419 result = _dbus_string_copy_len (&message->header, 0, header_len,
5422 _dbus_assert (result); /* because DBusString never reallocs smaller */
5425 /* does nothing if the message isn't in the list */
5426 _dbus_list_remove_last (&loader->messages, message);
5428 dbus_message_unref (message);
5436 * Converts buffered data into messages.
5438 * @todo we need to check that the proper named header fields exist
5439 * for each message type.
5441 * @todo If a message has unknown type, we should probably eat it
5442 * right here rather than passing it out to applications. However
5443 * it's not an error to see messages of unknown type.
5445 * @param loader the loader.
5446 * @returns #TRUE if we had enough memory to finish.
5449 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
5451 while (!loader->corrupted && _dbus_string_get_length (&loader->data) >= 16)
5453 const char *header_data;
5454 int byte_order, message_type, header_len, body_len;
5455 dbus_uint32_t header_len_unsigned, body_len_unsigned;
5457 header_data = _dbus_string_get_const_data_len (&loader->data, 0, 16);
5459 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
5461 if (header_data[VERSION_OFFSET] != DBUS_MAJOR_PROTOCOL_VERSION)
5463 _dbus_verbose ("Message has protocol version %d ours is %d\n",
5464 (int) header_data[VERSION_OFFSET], DBUS_MAJOR_PROTOCOL_VERSION);
5465 loader->corrupted = TRUE;
5469 byte_order = header_data[BYTE_ORDER_OFFSET];
5471 if (byte_order != DBUS_LITTLE_ENDIAN &&
5472 byte_order != DBUS_BIG_ENDIAN)
5474 _dbus_verbose ("Message with bad byte order '%c' received\n",
5476 loader->corrupted = TRUE;
5480 /* Unknown types are ignored, but INVALID is
5483 message_type = header_data[TYPE_OFFSET];
5484 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
5486 _dbus_verbose ("Message with bad type '%d' received\n",
5488 loader->corrupted = TRUE;
5492 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
5493 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
5495 if (header_len_unsigned < 16)
5497 _dbus_verbose ("Message had broken too-small header length %u\n",
5498 header_len_unsigned);
5499 loader->corrupted = TRUE;
5503 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
5504 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
5506 _dbus_verbose ("Header or body length too large (%u %u)\n",
5507 header_len_unsigned,
5509 loader->corrupted = TRUE;
5513 /* Now that we know the values are in signed range, get
5514 * rid of stupid unsigned, just causes bugs
5516 header_len = header_len_unsigned;
5517 body_len = body_len_unsigned;
5519 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
5522 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
5524 loader->corrupted = TRUE;
5528 if (header_len + body_len > loader->max_message_size)
5530 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %ld\n",
5531 header_len, body_len, loader->max_message_size);
5532 loader->corrupted = TRUE;
5536 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
5538 if (!load_one_message (loader, byte_order, message_type,
5539 header_len, body_len))
5550 * Peeks at first loaded message, returns #NULL if no messages have
5553 * @param loader the loader.
5554 * @returns the next message, or #NULL if none.
5557 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
5559 if (loader->messages)
5560 return loader->messages->data;
5566 * Pops a loaded message (passing ownership of the message
5567 * to the caller). Returns #NULL if no messages have been
5570 * @param loader the loader.
5571 * @returns the next message, or #NULL if none.
5574 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
5576 return _dbus_list_pop_first (&loader->messages);
5580 * Pops a loaded message inside a list link (passing ownership of the
5581 * message and link to the caller). Returns #NULL if no messages have
5584 * @param loader the loader.
5585 * @returns the next message link, or #NULL if none.
5588 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
5590 return _dbus_list_pop_first_link (&loader->messages);
5594 * Returns a popped message link, used to undo a pop.
5596 * @param loader the loader
5597 * @param link the link with a message in it
5600 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
5603 _dbus_list_prepend_link (&loader->messages, link);
5607 * Checks whether the loader is confused due to bad data.
5608 * If messages are received that are invalid, the
5609 * loader gets confused and gives up permanently.
5610 * This state is called "corrupted."
5612 * @param loader the loader
5613 * @returns #TRUE if the loader is hosed.
5616 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
5618 return loader->corrupted;
5622 * Sets the maximum size message we allow.
5624 * @param loader the loader
5625 * @param size the max message size in bytes
5628 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
5631 if (size > MAX_SANE_MESSAGE_SIZE)
5633 _dbus_verbose ("clamping requested max message size %ld to %d\n",
5634 size, MAX_SANE_MESSAGE_SIZE);
5635 size = MAX_SANE_MESSAGE_SIZE;
5637 loader->max_message_size = size;
5641 * Gets the maximum allowed message size in bytes.
5643 * @param loader the loader
5644 * @returns max size in bytes
5647 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
5649 return loader->max_message_size;
5652 static DBusDataSlotAllocator slot_allocator;
5653 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
5656 * Allocates an integer ID to be used for storing application-specific
5657 * data on any DBusMessage. The allocated ID may then be used
5658 * with dbus_message_set_data() and dbus_message_get_data().
5659 * The passed-in slot must be initialized to -1, and is filled in
5660 * with the slot ID. If the passed-in slot is not -1, it's assumed
5661 * to be already allocated, and its refcount is incremented.
5663 * The allocated slot is global, i.e. all DBusMessage objects will
5664 * have a slot with the given integer ID reserved.
5666 * @param slot_p address of a global variable storing the slot
5667 * @returns #FALSE on failure (no memory)
5670 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
5672 return _dbus_data_slot_allocator_alloc (&slot_allocator,
5673 _DBUS_LOCK_NAME (message_slots),
5678 * Deallocates a global ID for message data slots.
5679 * dbus_message_get_data() and dbus_message_set_data() may no
5680 * longer be used with this slot. Existing data stored on existing
5681 * DBusMessage objects will be freed when the message is
5682 * finalized, but may not be retrieved (and may only be replaced if
5683 * someone else reallocates the slot). When the refcount on the
5684 * passed-in slot reaches 0, it is set to -1.
5686 * @param slot_p address storing the slot to deallocate
5689 dbus_message_free_data_slot (dbus_int32_t *slot_p)
5691 _dbus_return_if_fail (*slot_p >= 0);
5693 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
5697 * Stores a pointer on a DBusMessage, along
5698 * with an optional function to be used for freeing
5699 * the data when the data is set again, or when
5700 * the message is finalized. The slot number
5701 * must have been allocated with dbus_message_allocate_data_slot().
5703 * @param message the message
5704 * @param slot the slot number
5705 * @param data the data to store
5706 * @param free_data_func finalizer function for the data
5707 * @returns #TRUE if there was enough memory to store the data
5710 dbus_message_set_data (DBusMessage *message,
5713 DBusFreeFunction free_data_func)
5715 DBusFreeFunction old_free_func;
5719 _dbus_return_val_if_fail (message != NULL, FALSE);
5720 _dbus_return_val_if_fail (slot >= 0, FALSE);
5722 retval = _dbus_data_slot_list_set (&slot_allocator,
5723 &message->slot_list,
5724 slot, data, free_data_func,
5725 &old_free_func, &old_data);
5729 /* Do the actual free outside the message lock */
5731 (* old_free_func) (old_data);
5738 * Retrieves data previously set with dbus_message_set_data().
5739 * The slot must still be allocated (must not have been freed).
5741 * @param message the message
5742 * @param slot the slot to get data from
5743 * @returns the data, or #NULL if not found
5746 dbus_message_get_data (DBusMessage *message,
5751 _dbus_return_val_if_fail (message != NULL, NULL);
5753 res = _dbus_data_slot_list_get (&slot_allocator,
5754 &message->slot_list,
5761 * Utility function to convert a machine-readable (not translated)
5762 * string into a D-BUS message type.
5765 * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL
5766 * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN
5767 * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL
5768 * "error" -> DBUS_MESSAGE_TYPE_ERROR
5769 * anything else -> DBUS_MESSAGE_TYPE_INVALID
5774 dbus_message_type_from_string (const char *type_str)
5776 if (strcmp (type_str, "method_call") == 0)
5777 return DBUS_MESSAGE_TYPE_METHOD_CALL;
5778 if (strcmp (type_str, "method_return") == 0)
5779 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
5780 else if (strcmp (type_str, "signal") == 0)
5781 return DBUS_MESSAGE_TYPE_SIGNAL;
5782 else if (strcmp (type_str, "error") == 0)
5783 return DBUS_MESSAGE_TYPE_ERROR;
5785 return DBUS_MESSAGE_TYPE_INVALID;
5789 #ifdef DBUS_BUILD_TESTS
5790 #include "dbus-test.h"
5795 message_iter_test (DBusMessage *message)
5797 DBusMessageIter iter, dict, array, array2;
5799 unsigned char *data;
5800 dbus_int32_t *our_int_array;
5803 dbus_message_iter_init (message, &iter);
5806 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
5807 _dbus_assert_not_reached ("Argument type isn't string");
5809 str = dbus_message_iter_get_string (&iter);
5810 if (strcmp (str, "Test string") != 0)
5811 _dbus_assert_not_reached ("Strings differ");
5814 if (!dbus_message_iter_next (&iter))
5815 _dbus_assert_not_reached ("Reached end of arguments");
5817 /* Signed integer tests */
5818 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32)
5819 _dbus_assert_not_reached ("Argument type isn't int32");
5821 if (dbus_message_iter_get_int32 (&iter) != -0x12345678)
5822 _dbus_assert_not_reached ("Signed integers differ");
5824 if (!dbus_message_iter_next (&iter))
5825 _dbus_assert_not_reached ("Reached end of fields");
5827 /* Unsigned integer tests */
5828 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
5829 _dbus_assert_not_reached ("Argument type isn't int32");
5831 if (dbus_message_iter_get_uint32 (&iter) != 0xedd1e)
5832 _dbus_assert_not_reached ("Unsigned integers differ");
5834 if (!dbus_message_iter_next (&iter))
5835 _dbus_assert_not_reached ("Reached end of arguments");
5838 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DOUBLE)
5839 _dbus_assert_not_reached ("Argument type isn't double");
5841 if (dbus_message_iter_get_double (&iter) != 3.14159)
5842 _dbus_assert_not_reached ("Doubles differ");
5844 if (!dbus_message_iter_next (&iter))
5845 _dbus_assert_not_reached ("Reached end of arguments");
5847 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
5848 _dbus_assert_not_reached ("Argument type not an array");
5850 if (dbus_message_iter_get_array_type (&iter) != DBUS_TYPE_DOUBLE)
5851 _dbus_assert_not_reached ("Array type not double");
5854 dbus_message_iter_init_array_iterator (&iter, &array, NULL);
5856 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_DOUBLE)
5857 _dbus_assert_not_reached ("Argument type isn't double");
5859 if (dbus_message_iter_get_double (&array) != 1.5)
5860 _dbus_assert_not_reached ("Unsigned integers differ");
5862 if (!dbus_message_iter_next (&array))
5863 _dbus_assert_not_reached ("Reached end of arguments");
5865 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_DOUBLE)
5866 _dbus_assert_not_reached ("Argument type isn't double");
5868 if (dbus_message_iter_get_double (&array) != 2.5)
5869 _dbus_assert_not_reached ("Unsigned integers differ");
5871 if (dbus_message_iter_next (&array))
5872 _dbus_assert_not_reached ("Didn't reach end of arguments");
5874 if (!dbus_message_iter_next (&iter))
5875 _dbus_assert_not_reached ("Reached end of arguments");
5880 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
5881 _dbus_assert_not_reached ("not dict type");
5883 dbus_message_iter_init_dict_iterator (&iter, &dict);
5885 str = dbus_message_iter_get_dict_key (&dict);
5886 if (str == NULL || strcmp (str, "test") != 0)
5887 _dbus_assert_not_reached ("wrong dict key");
5890 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_UINT32)
5891 _dbus_assert_not_reached ("wrong dict entry type");
5893 if (dbus_message_iter_get_uint32 (&dict) != 0xDEADBEEF)
5894 _dbus_assert_not_reached ("wrong dict entry value");
5896 if (!dbus_message_iter_next (&dict))
5897 _dbus_assert_not_reached ("reached end of dict");
5899 /* array of array of int32 (in dict) */
5901 str = dbus_message_iter_get_dict_key (&dict);
5902 if (str == NULL || strcmp (str, "array") != 0)
5903 _dbus_assert_not_reached ("wrong dict key");
5906 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_ARRAY)
5907 _dbus_assert_not_reached ("Argument type not an array");
5909 if (dbus_message_iter_get_array_type (&dict) != DBUS_TYPE_ARRAY)
5910 _dbus_assert_not_reached ("Array type not array");
5912 dbus_message_iter_init_array_iterator (&dict, &array, NULL);
5914 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_ARRAY)
5915 _dbus_assert_not_reached ("Argument type isn't array");
5917 if (dbus_message_iter_get_array_type (&array) != DBUS_TYPE_INT32)
5918 _dbus_assert_not_reached ("Array type not int32");
5920 dbus_message_iter_init_array_iterator (&array, &array2, NULL);
5922 if (dbus_message_iter_get_arg_type (&array2) != DBUS_TYPE_INT32)
5923 _dbus_assert_not_reached ("Argument type isn't int32");
5925 if (dbus_message_iter_get_int32 (&array2) != 0x12345678)
5926 _dbus_assert_not_reached ("Signed integers differ");
5928 if (!dbus_message_iter_next (&array2))
5929 _dbus_assert_not_reached ("Reached end of arguments");
5931 if (dbus_message_iter_get_int32 (&array2) != 0x23456781)
5932 _dbus_assert_not_reached ("Signed integers differ");
5934 if (dbus_message_iter_next (&array2))
5935 _dbus_assert_not_reached ("Didn't reached end of arguments");
5937 if (!dbus_message_iter_next (&array))
5938 _dbus_assert_not_reached ("Reached end of arguments");
5940 if (dbus_message_iter_get_array_type (&array) != DBUS_TYPE_INT32)
5941 _dbus_assert_not_reached ("Array type not int32");
5943 if (!dbus_message_iter_get_int32_array (&array,
5946 _dbus_assert_not_reached ("couldn't get int32 array");
5948 _dbus_assert (len == 3);
5949 _dbus_assert (our_int_array[0] == 0x34567812 &&
5950 our_int_array[1] == 0x45678123 &&
5951 our_int_array[2] == 0x56781234);
5952 dbus_free (our_int_array);
5954 if (dbus_message_iter_next (&array))
5955 _dbus_assert_not_reached ("Didn't reach end of array");
5957 if (dbus_message_iter_next (&dict))
5958 _dbus_assert_not_reached ("Didn't reach end of dict");
5960 if (!dbus_message_iter_next (&iter))
5961 _dbus_assert_not_reached ("Reached end of arguments");
5963 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_BYTE)
5965 _dbus_warn ("type was: %d\n", dbus_message_iter_get_arg_type (&iter));
5966 _dbus_assert_not_reached ("wrong type after dict (should be byte)");
5969 if (dbus_message_iter_get_byte (&iter) != 0xF0)
5970 _dbus_assert_not_reached ("wrong value after dict");
5973 if (!dbus_message_iter_next (&iter))
5974 _dbus_assert_not_reached ("Reached end of arguments");
5976 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_NIL)
5977 _dbus_assert_not_reached ("not a nil type");
5979 if (!dbus_message_iter_next (&iter))
5980 _dbus_assert_not_reached ("Reached end of arguments");
5982 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_CUSTOM)
5983 _dbus_assert_not_reached ("wrong type after dict");
5985 if (!dbus_message_iter_get_custom (&iter, &str, &data, &len))
5986 _dbus_assert_not_reached ("failed to get custom type");
5988 _dbus_assert (strcmp (str, "MyTypeName")==0);
5989 _dbus_assert (len == 5);
5990 _dbus_assert (strcmp (data, "data")==0);
5994 if (dbus_message_iter_next (&iter))
5995 _dbus_assert_not_reached ("Didn't reach end of arguments");
6000 check_message_handling_type (DBusMessageIter *iter,
6003 DBusMessageIter child_iter;
6009 case DBUS_TYPE_BYTE:
6010 dbus_message_iter_get_byte (iter);
6012 case DBUS_TYPE_BOOLEAN:
6013 dbus_message_iter_get_boolean (iter);
6015 case DBUS_TYPE_INT32:
6016 dbus_message_iter_get_int32 (iter);
6018 case DBUS_TYPE_UINT32:
6019 dbus_message_iter_get_uint32 (iter);
6021 case DBUS_TYPE_INT64:
6022 #ifdef DBUS_HAVE_INT64
6023 dbus_message_iter_get_int64 (iter);
6026 case DBUS_TYPE_UINT64:
6027 #ifdef DBUS_HAVE_INT64
6028 dbus_message_iter_get_uint64 (iter);
6031 case DBUS_TYPE_DOUBLE:
6032 dbus_message_iter_get_double (iter);
6034 case DBUS_TYPE_STRING:
6037 str = dbus_message_iter_get_string (iter);
6040 _dbus_warn ("NULL string in message\n");
6046 case DBUS_TYPE_CUSTOM:
6049 unsigned char *data;
6052 if (!dbus_message_iter_get_custom (iter, &name, &data, &len))
6054 _dbus_warn ("error reading name from custom type\n");
6061 case DBUS_TYPE_ARRAY:
6065 dbus_message_iter_init_array_iterator (iter, &child_iter, &array_type);
6067 while (dbus_message_iter_has_next (&child_iter))
6069 if (!check_message_handling_type (&child_iter, array_type))
6071 _dbus_warn ("error in array element\n");
6075 if (!dbus_message_iter_next (&child_iter))
6080 case DBUS_TYPE_DICT:
6085 dbus_message_iter_init_dict_iterator (iter, &child_iter);
6087 while ((entry_type = dbus_message_iter_get_arg_type (&child_iter)) != DBUS_TYPE_INVALID)
6089 key = dbus_message_iter_get_dict_key (&child_iter);
6092 _dbus_warn ("error reading dict key\n");
6097 if (!check_message_handling_type (&child_iter, entry_type))
6099 _dbus_warn ("error in dict value\n");
6103 if (!dbus_message_iter_next (&child_iter))
6110 _dbus_warn ("unknown type %d\n", type);
6119 check_message_handling (DBusMessage *message)
6121 DBusMessageIter iter;
6124 dbus_uint32_t client_serial;
6128 client_serial = dbus_message_get_serial (message);
6130 /* can't use set_serial due to the assertions at the start of it */
6131 _dbus_marshal_set_uint32 (&message->header,
6132 message->byte_order,
6133 CLIENT_SERIAL_OFFSET,
6136 if (client_serial != dbus_message_get_serial (message))
6138 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
6142 /* If we implement message_set_arg (message, n, value)
6143 * then we would want to test it here
6146 dbus_message_iter_init (message, &iter);
6147 while ((type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
6149 if (!check_message_handling_type (&iter, type))
6152 if (!dbus_message_iter_next (&iter))
6163 check_have_valid_message (DBusMessageLoader *loader)
6165 DBusMessage *message;
6171 if (!_dbus_message_loader_queue_messages (loader))
6172 _dbus_assert_not_reached ("no memory to queue messages");
6174 if (_dbus_message_loader_get_is_corrupted (loader))
6176 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
6180 message = _dbus_message_loader_pop_message (loader);
6181 if (message == NULL)
6183 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
6187 if (_dbus_string_get_length (&loader->data) > 0)
6189 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
6193 /* Verify that we're able to properly deal with the message.
6194 * For example, this would detect improper handling of messages
6195 * in nonstandard byte order.
6197 if (!check_message_handling (message))
6204 dbus_message_unref (message);
6210 check_invalid_message (DBusMessageLoader *loader)
6216 if (!_dbus_message_loader_queue_messages (loader))
6217 _dbus_assert_not_reached ("no memory to queue messages");
6219 if (!_dbus_message_loader_get_is_corrupted (loader))
6221 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
6232 check_incomplete_message (DBusMessageLoader *loader)
6234 DBusMessage *message;
6240 if (!_dbus_message_loader_queue_messages (loader))
6241 _dbus_assert_not_reached ("no memory to queue messages");
6243 if (_dbus_message_loader_get_is_corrupted (loader))
6245 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
6249 message = _dbus_message_loader_pop_message (loader);
6250 if (message != NULL)
6252 _dbus_warn ("loaded message that was expected to be incomplete\n");
6260 dbus_message_unref (message);
6265 check_loader_results (DBusMessageLoader *loader,
6266 DBusMessageValidity validity)
6268 if (!_dbus_message_loader_queue_messages (loader))
6269 _dbus_assert_not_reached ("no memory to queue messages");
6273 case _DBUS_MESSAGE_VALID:
6274 return check_have_valid_message (loader);
6275 case _DBUS_MESSAGE_INVALID:
6276 return check_invalid_message (loader);
6277 case _DBUS_MESSAGE_INCOMPLETE:
6278 return check_incomplete_message (loader);
6279 case _DBUS_MESSAGE_UNKNOWN:
6283 _dbus_assert_not_reached ("bad DBusMessageValidity");
6289 * Loads the message in the given message file.
6291 * @param filename filename to load
6292 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
6293 * @param data string to load message into
6294 * @returns #TRUE if the message was loaded
6297 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
6309 _dbus_verbose ("Loading raw %s\n", _dbus_string_get_const_data (filename));
6310 dbus_error_init (&error);
6311 if (!_dbus_file_get_contents (data, filename, &error))
6313 _dbus_warn ("Could not load message file %s: %s\n",
6314 _dbus_string_get_const_data (filename),
6316 dbus_error_free (&error);
6322 if (!_dbus_message_data_load (data, filename))
6324 _dbus_warn ("Could not load message file %s\n",
6325 _dbus_string_get_const_data (filename));
6338 * Tries loading the message in the given message file
6339 * and verifies that DBusMessageLoader can handle it.
6341 * @param filename filename to load
6342 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
6343 * @param expected_validity what the message has to be like to return #TRUE
6344 * @returns #TRUE if the message has the expected validity
6347 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
6349 DBusMessageValidity expected_validity)
6356 if (!_dbus_string_init (&data))
6357 _dbus_assert_not_reached ("could not allocate string\n");
6359 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
6363 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
6369 if (_dbus_string_get_length (&data) > 0)
6370 _dbus_verbose_bytes_of_string (&data, 0,
6371 _dbus_string_get_length (&data));
6373 _dbus_warn ("Failed message loader test on %s\n",
6374 _dbus_string_get_const_data (filename));
6377 _dbus_string_free (&data);
6383 * Tries loading the given message data.
6386 * @param data the message data
6387 * @param expected_validity what the message has to be like to return #TRUE
6388 * @returns #TRUE if the message has the expected validity
6391 dbus_internal_do_not_use_try_message_data (const DBusString *data,
6392 DBusMessageValidity expected_validity)
6394 DBusMessageLoader *loader;
6402 /* Write the data one byte at a time */
6404 loader = _dbus_message_loader_new ();
6406 /* check some trivial loader functions */
6407 _dbus_message_loader_ref (loader);
6408 _dbus_message_loader_unref (loader);
6409 _dbus_message_loader_get_max_message_size (loader);
6411 len = _dbus_string_get_length (data);
6412 for (i = 0; i < len; i++)
6416 _dbus_message_loader_get_buffer (loader, &buffer);
6417 _dbus_string_append_byte (buffer,
6418 _dbus_string_get_byte (data, i));
6419 _dbus_message_loader_return_buffer (loader, buffer, 1);
6422 if (!check_loader_results (loader, expected_validity))
6425 _dbus_message_loader_unref (loader);
6428 /* Write the data all at once */
6430 loader = _dbus_message_loader_new ();
6435 _dbus_message_loader_get_buffer (loader, &buffer);
6436 _dbus_string_copy (data, 0, buffer,
6437 _dbus_string_get_length (buffer));
6438 _dbus_message_loader_return_buffer (loader, buffer, 1);
6441 if (!check_loader_results (loader, expected_validity))
6444 _dbus_message_loader_unref (loader);
6447 /* Write the data 2 bytes at a time */
6449 loader = _dbus_message_loader_new ();
6451 len = _dbus_string_get_length (data);
6452 for (i = 0; i < len; i += 2)
6456 _dbus_message_loader_get_buffer (loader, &buffer);
6457 _dbus_string_append_byte (buffer,
6458 _dbus_string_get_byte (data, i));
6460 _dbus_string_append_byte (buffer,
6461 _dbus_string_get_byte (data, i+1));
6462 _dbus_message_loader_return_buffer (loader, buffer, 1);
6465 if (!check_loader_results (loader, expected_validity))
6468 _dbus_message_loader_unref (loader);
6476 _dbus_message_loader_unref (loader);
6482 process_test_subdir (const DBusString *test_base_dir,
6484 DBusMessageValidity validity,
6485 DBusForeachMessageFileFunc function,
6488 DBusString test_directory;
6489 DBusString filename;
6497 if (!_dbus_string_init (&test_directory))
6498 _dbus_assert_not_reached ("didn't allocate test_directory\n");
6500 _dbus_string_init_const (&filename, subdir);
6502 if (!_dbus_string_copy (test_base_dir, 0,
6503 &test_directory, 0))
6504 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
6506 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
6507 _dbus_assert_not_reached ("couldn't allocate full path");
6509 _dbus_string_free (&filename);
6510 if (!_dbus_string_init (&filename))
6511 _dbus_assert_not_reached ("didn't allocate filename string\n");
6513 dbus_error_init (&error);
6514 dir = _dbus_directory_open (&test_directory, &error);
6517 _dbus_warn ("Could not open %s: %s\n",
6518 _dbus_string_get_const_data (&test_directory),
6520 dbus_error_free (&error);
6524 printf ("Testing %s:\n", subdir);
6527 while (_dbus_directory_get_next_file (dir, &filename, &error))
6529 DBusString full_path;
6532 if (!_dbus_string_init (&full_path))
6533 _dbus_assert_not_reached ("couldn't init string");
6535 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
6536 _dbus_assert_not_reached ("couldn't copy dir to full_path");
6538 if (!_dbus_concat_dir_and_file (&full_path, &filename))
6539 _dbus_assert_not_reached ("couldn't concat file to dir");
6541 if (_dbus_string_ends_with_c_str (&filename, ".message"))
6543 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
6547 _dbus_verbose ("Skipping non-.message file %s\n",
6548 _dbus_string_get_const_data (&filename));
6549 _dbus_string_free (&full_path);
6554 _dbus_string_get_const_data (&filename));
6556 _dbus_verbose (" expecting %s for %s\n",
6557 validity == _DBUS_MESSAGE_VALID ? "valid" :
6558 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
6559 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")),
6560 _dbus_string_get_const_data (&filename));
6562 if (! (*function) (&full_path, is_raw, validity, user_data))
6564 _dbus_string_free (&full_path);
6568 _dbus_string_free (&full_path);
6571 if (dbus_error_is_set (&error))
6573 _dbus_warn ("Could not get next file in %s: %s\n",
6574 _dbus_string_get_const_data (&test_directory),
6576 dbus_error_free (&error);
6585 _dbus_directory_close (dir);
6586 _dbus_string_free (&test_directory);
6587 _dbus_string_free (&filename);
6593 * Runs the given function on every message file in the test suite.
6594 * The function should return #FALSE on test failure or fatal error.
6596 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
6597 * @param func the function to run
6598 * @param user_data data for function
6599 * @returns #FALSE if there's a failure
6602 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
6603 DBusForeachMessageFileFunc func,
6606 DBusString test_directory;
6611 _dbus_string_init_const (&test_directory, test_data_dir);
6613 if (!process_test_subdir (&test_directory, "valid-messages",
6614 _DBUS_MESSAGE_VALID, func, user_data))
6617 if (!process_test_subdir (&test_directory, "invalid-messages",
6618 _DBUS_MESSAGE_INVALID, func, user_data))
6621 if (!process_test_subdir (&test_directory, "incomplete-messages",
6622 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
6629 _dbus_string_free (&test_directory);
6635 verify_test_message (DBusMessage *message)
6637 DBusMessageIter iter, dict;
6639 dbus_int32_t our_int;
6642 dbus_bool_t our_bool;
6643 dbus_uint32_t our_uint32;
6644 dbus_int32_t *our_uint32_array;
6645 int our_uint32_array_len;
6646 dbus_int32_t *our_int32_array;
6647 int our_int32_array_len;
6648 char **our_string_array;
6649 int our_string_array_len;
6650 #ifdef DBUS_HAVE_INT64
6651 dbus_int64_t our_int64;
6652 dbus_uint64_t our_uint64;
6653 dbus_int64_t *our_uint64_array;
6654 int our_uint64_array_len;
6655 dbus_int64_t *our_int64_array;
6656 int our_int64_array_len;
6658 double *our_double_array;
6659 int our_double_array_len;
6660 unsigned char *our_byte_array;
6661 int our_byte_array_len;
6662 unsigned char *our_boolean_array;
6663 int our_boolean_array_len;
6665 dbus_message_iter_init (message, &iter);
6667 dbus_error_init (&error);
6668 if (!dbus_message_iter_get_args (&iter, &error,
6669 DBUS_TYPE_INT32, &our_int,
6670 #ifdef DBUS_HAVE_INT64
6671 DBUS_TYPE_INT64, &our_int64,
6672 DBUS_TYPE_UINT64, &our_uint64,
6674 DBUS_TYPE_STRING, &our_str,
6675 DBUS_TYPE_DOUBLE, &our_double,
6676 DBUS_TYPE_BOOLEAN, &our_bool,
6677 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32,
6678 &our_uint32_array, &our_uint32_array_len,
6679 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32,
6680 &our_int32_array, &our_int32_array_len,
6681 #ifdef DBUS_HAVE_INT64
6682 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64,
6683 &our_uint64_array, &our_uint64_array_len,
6684 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64,
6685 &our_int64_array, &our_int64_array_len,
6687 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
6688 &our_string_array, &our_string_array_len,
6689 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE,
6690 &our_double_array, &our_double_array_len,
6691 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
6692 &our_byte_array, &our_byte_array_len,
6693 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN,
6694 &our_boolean_array, &our_boolean_array_len,
6697 _dbus_warn ("error: %s - %s\n", error.name,
6698 (error.message != NULL) ? error.message : "no message");
6699 _dbus_assert_not_reached ("Could not get arguments");
6702 if (our_int != -0x12345678)
6703 _dbus_assert_not_reached ("integers differ!");
6705 #ifdef DBUS_HAVE_INT64
6706 if (our_int64 != DBUS_INT64_CONSTANT (-0x123456789abcd))
6707 _dbus_assert_not_reached ("64-bit integers differ!");
6708 if (our_uint64 != DBUS_UINT64_CONSTANT (0x123456789abcd))
6709 _dbus_assert_not_reached ("64-bit unsigned integers differ!");
6712 if (our_double != 3.14159)
6713 _dbus_assert_not_reached ("doubles differ!");
6715 if (strcmp (our_str, "Test string") != 0)
6716 _dbus_assert_not_reached ("strings differ!");
6717 dbus_free (our_str);
6720 _dbus_assert_not_reached ("booleans differ");
6722 if (our_uint32_array_len != 4 ||
6723 our_uint32_array[0] != 0x12345678 ||
6724 our_uint32_array[1] != 0x23456781 ||
6725 our_uint32_array[2] != 0x34567812 ||
6726 our_uint32_array[3] != 0x45678123)
6727 _dbus_assert_not_reached ("uint array differs");
6728 dbus_free (our_uint32_array);
6730 if (our_int32_array_len != 4 ||
6731 our_int32_array[0] != 0x12345678 ||
6732 our_int32_array[1] != -0x23456781 ||
6733 our_int32_array[2] != 0x34567812 ||
6734 our_int32_array[3] != -0x45678123)
6735 _dbus_assert_not_reached ("int array differs");
6736 dbus_free (our_int32_array);
6738 #ifdef DBUS_HAVE_INT64
6739 if (our_uint64_array_len != 4 ||
6740 our_uint64_array[0] != 0x12345678 ||
6741 our_uint64_array[1] != 0x23456781 ||
6742 our_uint64_array[2] != 0x34567812 ||
6743 our_uint64_array[3] != 0x45678123)
6744 _dbus_assert_not_reached ("uint64 array differs");
6745 dbus_free (our_uint64_array);
6747 if (our_int64_array_len != 4 ||
6748 our_int64_array[0] != 0x12345678 ||
6749 our_int64_array[1] != -0x23456781 ||
6750 our_int64_array[2] != 0x34567812 ||
6751 our_int64_array[3] != -0x45678123)
6752 _dbus_assert_not_reached ("int64 array differs");
6753 dbus_free (our_int64_array);
6754 #endif /* DBUS_HAVE_INT64 */
6756 if (our_string_array_len != 4)
6757 _dbus_assert_not_reached ("string array has wrong length");
6759 if (strcmp (our_string_array[0], "Foo") != 0 ||
6760 strcmp (our_string_array[1], "bar") != 0 ||
6761 strcmp (our_string_array[2], "") != 0 ||
6762 strcmp (our_string_array[3], "woo woo woo woo") != 0)
6763 _dbus_assert_not_reached ("string array differs");
6765 dbus_free_string_array (our_string_array);
6767 if (our_double_array_len != 3)
6768 _dbus_assert_not_reached ("double array had wrong length");
6770 /* On all IEEE machines (i.e. everything sane) exact equality
6771 * should be preserved over the wire
6773 if (our_double_array[0] != 0.1234 ||
6774 our_double_array[1] != 9876.54321 ||
6775 our_double_array[2] != -300.0)
6776 _dbus_assert_not_reached ("double array had wrong values");
6778 dbus_free (our_double_array);
6780 if (our_byte_array_len != 4)
6781 _dbus_assert_not_reached ("byte array had wrong length");
6783 if (our_byte_array[0] != 'a' ||
6784 our_byte_array[1] != 'b' ||
6785 our_byte_array[2] != 'c' ||
6786 our_byte_array[3] != 234)
6787 _dbus_assert_not_reached ("byte array had wrong values");
6789 dbus_free (our_byte_array);
6791 if (our_boolean_array_len != 5)
6792 _dbus_assert_not_reached ("bool array had wrong length");
6794 if (our_boolean_array[0] != TRUE ||
6795 our_boolean_array[1] != FALSE ||
6796 our_boolean_array[2] != TRUE ||
6797 our_boolean_array[3] != TRUE ||
6798 our_boolean_array[4] != FALSE)
6799 _dbus_assert_not_reached ("bool array had wrong values");
6801 dbus_free (our_boolean_array);
6803 if (!dbus_message_iter_next (&iter))
6804 _dbus_assert_not_reached ("Reached end of arguments");
6806 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
6807 _dbus_assert_not_reached ("not dict type");
6809 dbus_message_iter_init_dict_iterator (&iter, &dict);
6811 our_str = dbus_message_iter_get_dict_key (&dict);
6812 if (our_str == NULL || strcmp (our_str, "test") != 0)
6813 _dbus_assert_not_reached ("wrong dict key");
6814 dbus_free (our_str);
6816 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_UINT32)
6818 _dbus_verbose ("dict entry type: %d\n", dbus_message_iter_get_arg_type (&dict));
6819 _dbus_assert_not_reached ("wrong dict entry type");
6822 if ((our_uint32 = dbus_message_iter_get_uint32 (&dict)) != 0xDEADBEEF)
6824 _dbus_verbose ("dict entry val: %x\n", our_uint32);
6825 _dbus_assert_not_reached ("wrong dict entry value");
6828 if (dbus_message_iter_next (&dict))
6829 _dbus_assert_not_reached ("Didn't reach end of dict");
6831 if (!dbus_message_iter_next (&iter))
6832 _dbus_assert_not_reached ("Reached end of arguments");
6834 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
6835 _dbus_assert_not_reached ("wrong type after dict");
6837 if (dbus_message_iter_get_uint32 (&iter) != 0xCAFEBABE)
6838 _dbus_assert_not_reached ("wrong value after dict");
6840 if (dbus_message_iter_next (&iter))
6841 _dbus_assert_not_reached ("Didn't reach end of arguments");
6845 * @ingroup DBusMessageInternals
6846 * Unit test for DBusMessage.
6848 * @returns #TRUE on success.
6851 _dbus_message_test (const char *test_data_dir)
6853 DBusMessage *message;
6854 DBusMessageLoader *loader;
6855 DBusMessageIter iter, child_iter, child_iter2, child_iter3;
6861 const dbus_uint32_t our_uint32_array[] =
6862 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
6863 const dbus_uint32_t our_int32_array[] =
6864 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
6865 #ifdef DBUS_HAVE_INT64
6866 const dbus_uint64_t our_uint64_array[] =
6867 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
6868 const dbus_uint64_t our_int64_array[] =
6869 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
6871 const char *our_string_array[] = { "Foo", "bar", "", "woo woo woo woo" };
6872 const double our_double_array[] = { 0.1234, 9876.54321, -300.0 };
6873 const unsigned char our_byte_array[] = { 'a', 'b', 'c', 234 };
6874 const unsigned char our_boolean_array[] = { TRUE, FALSE, TRUE, TRUE, FALSE };
6880 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
6882 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
6883 "/org/freedesktop/TestPath",
6884 "Foo.TestInterface",
6886 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
6887 _dbus_assert (dbus_message_is_method_call (message, "Foo.TestInterface",
6889 _dbus_assert (strcmp (dbus_message_get_path (message),
6890 "/org/freedesktop/TestPath") == 0);
6891 _dbus_message_set_serial (message, 1234);
6892 /* string length including nul byte not a multiple of 4 */
6893 if (!dbus_message_set_sender (message, "org.foo.bar1"))
6894 _dbus_assert_not_reached ("out of memory");
6895 _dbus_assert (dbus_message_has_sender (message, "org.foo.bar1"));
6896 dbus_message_set_reply_serial (message, 5678);
6897 if (!dbus_message_set_sender (message, NULL))
6898 _dbus_assert_not_reached ("out of memory");
6899 _dbus_assert (!dbus_message_has_sender (message, "org.foo.bar1"));
6900 _dbus_assert (dbus_message_get_serial (message) == 1234);
6901 _dbus_assert (dbus_message_get_reply_serial (message) == 5678);
6902 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
6904 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
6905 dbus_message_set_no_reply (message, TRUE);
6906 _dbus_assert (dbus_message_get_no_reply (message) == TRUE);
6907 dbus_message_set_no_reply (message, FALSE);
6908 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
6910 /* Set/get some header fields */
6912 if (!dbus_message_set_path (message, "/foo"))
6913 _dbus_assert_not_reached ("out of memory");
6914 _dbus_assert (strcmp (dbus_message_get_path (message),
6917 if (!dbus_message_set_interface (message, "org.Foo"))
6918 _dbus_assert_not_reached ("out of memory");
6919 _dbus_assert (strcmp (dbus_message_get_interface (message),
6922 if (!dbus_message_set_member (message, "Bar"))
6923 _dbus_assert_not_reached ("out of memory");
6924 _dbus_assert (strcmp (dbus_message_get_member (message),
6927 /* Set/get them with longer values */
6928 if (!dbus_message_set_path (message, "/foo/bar"))
6929 _dbus_assert_not_reached ("out of memory");
6930 _dbus_assert (strcmp (dbus_message_get_path (message),
6933 if (!dbus_message_set_interface (message, "org.Foo.Bar"))
6934 _dbus_assert_not_reached ("out of memory");
6935 _dbus_assert (strcmp (dbus_message_get_interface (message),
6936 "org.Foo.Bar") == 0);
6938 if (!dbus_message_set_member (message, "BarFoo"))
6939 _dbus_assert_not_reached ("out of memory");
6940 _dbus_assert (strcmp (dbus_message_get_member (message),
6943 /* Realloc shorter again */
6945 if (!dbus_message_set_path (message, "/foo"))
6946 _dbus_assert_not_reached ("out of memory");
6947 _dbus_assert (strcmp (dbus_message_get_path (message),
6950 if (!dbus_message_set_interface (message, "org.Foo"))
6951 _dbus_assert_not_reached ("out of memory");
6952 _dbus_assert (strcmp (dbus_message_get_interface (message),
6955 if (!dbus_message_set_member (message, "Bar"))
6956 _dbus_assert_not_reached ("out of memory");
6957 _dbus_assert (strcmp (dbus_message_get_member (message),
6960 dbus_message_unref (message);
6962 /* Test the vararg functions */
6963 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
6964 "/org/freedesktop/TestPath",
6965 "Foo.TestInterface",
6967 _dbus_message_set_serial (message, 1);
6968 dbus_message_append_args (message,
6969 DBUS_TYPE_INT32, -0x12345678,
6970 #ifdef DBUS_HAVE_INT64
6971 DBUS_TYPE_INT64, DBUS_INT64_CONSTANT (-0x123456789abcd),
6972 DBUS_TYPE_UINT64, DBUS_UINT64_CONSTANT (0x123456789abcd),
6974 DBUS_TYPE_STRING, "Test string",
6975 DBUS_TYPE_DOUBLE, 3.14159,
6976 DBUS_TYPE_BOOLEAN, TRUE,
6977 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, our_uint32_array,
6978 _DBUS_N_ELEMENTS (our_uint32_array),
6979 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, our_int32_array,
6980 _DBUS_N_ELEMENTS (our_int32_array),
6981 #ifdef DBUS_HAVE_INT64
6982 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64, our_uint64_array,
6983 _DBUS_N_ELEMENTS (our_uint64_array),
6984 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64, our_int64_array,
6985 _DBUS_N_ELEMENTS (our_int64_array),
6987 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, our_string_array,
6988 _DBUS_N_ELEMENTS (our_string_array),
6989 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, our_double_array,
6990 _DBUS_N_ELEMENTS (our_double_array),
6991 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, our_byte_array,
6992 _DBUS_N_ELEMENTS (our_byte_array),
6993 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN, our_boolean_array,
6994 _DBUS_N_ELEMENTS (our_boolean_array),
6997 dbus_message_append_iter_init (message, &iter);
6998 dbus_message_iter_append_dict (&iter, &child_iter);
6999 dbus_message_iter_append_dict_key (&child_iter, "test");
7000 dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
7001 dbus_message_iter_append_uint32 (&iter, 0xCAFEBABE);
7004 sig[i++] = DBUS_TYPE_INT32;
7005 #ifdef DBUS_HAVE_INT64
7006 sig[i++] = DBUS_TYPE_INT64;
7007 sig[i++] = DBUS_TYPE_UINT64;
7009 sig[i++] = DBUS_TYPE_STRING;
7010 sig[i++] = DBUS_TYPE_DOUBLE;
7011 sig[i++] = DBUS_TYPE_BOOLEAN;
7012 sig[i++] = DBUS_TYPE_ARRAY;
7013 sig[i++] = DBUS_TYPE_UINT32;
7014 sig[i++] = DBUS_TYPE_ARRAY;
7015 sig[i++] = DBUS_TYPE_INT32;
7016 #ifdef DBUS_HAVE_INT64
7017 sig[i++] = DBUS_TYPE_ARRAY;
7018 sig[i++] = DBUS_TYPE_UINT64;
7019 sig[i++] = DBUS_TYPE_ARRAY;
7020 sig[i++] = DBUS_TYPE_INT64;
7022 sig[i++] = DBUS_TYPE_ARRAY;
7023 sig[i++] = DBUS_TYPE_STRING;
7024 sig[i++] = DBUS_TYPE_ARRAY;
7025 sig[i++] = DBUS_TYPE_DOUBLE;
7026 sig[i++] = DBUS_TYPE_ARRAY;
7027 sig[i++] = DBUS_TYPE_BYTE;
7028 sig[i++] = DBUS_TYPE_ARRAY;
7029 sig[i++] = DBUS_TYPE_BOOLEAN;
7030 sig[i++] = DBUS_TYPE_DICT;
7031 sig[i++] = DBUS_TYPE_UINT32;
7032 sig[i++] = DBUS_TYPE_INVALID;
7034 _dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
7036 _dbus_verbose_bytes_of_string (&message->header, 0,
7037 _dbus_string_get_length (&message->header));
7038 _dbus_verbose_bytes_of_string (&message->body, 0,
7039 _dbus_string_get_length (&message->body));
7041 _dbus_verbose ("Signature expected \"%s\" actual \"%s\"\n",
7042 sig, dbus_message_get_signature (message));
7044 s = dbus_message_get_signature (message);
7046 _dbus_assert (dbus_message_has_signature (message, sig));
7047 _dbus_assert (strcmp (s, sig) == 0);
7049 verify_test_message (message);
7051 copy = dbus_message_copy (message);
7053 _dbus_assert (message->client_serial == copy->client_serial);
7054 _dbus_assert (message->reply_serial == copy->reply_serial);
7055 _dbus_assert (message->header_padding == copy->header_padding);
7057 _dbus_assert (_dbus_string_get_length (&message->header) ==
7058 _dbus_string_get_length (©->header));
7060 _dbus_assert (_dbus_string_get_length (&message->body) ==
7061 _dbus_string_get_length (©->body));
7063 _dbus_assert (_dbus_string_get_length (&message->signature) ==
7064 _dbus_string_get_length (©->signature));
7066 verify_test_message (copy);
7068 name1 = dbus_message_get_interface (message);
7069 name2 = dbus_message_get_interface (copy);
7071 _dbus_assert (strcmp (name1, name2) == 0);
7073 name1 = dbus_message_get_member (message);
7074 name2 = dbus_message_get_member (copy);
7076 _dbus_assert (strcmp (name1, name2) == 0);
7078 dbus_message_unref (message);
7079 dbus_message_unref (copy);
7081 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
7082 "/org/freedesktop/TestPath",
7083 "Foo.TestInterface",
7086 _dbus_message_set_serial (message, 1);
7087 dbus_message_set_reply_serial (message, 0x12345678);
7089 dbus_message_append_iter_init (message, &iter);
7090 dbus_message_iter_append_string (&iter, "Test string");
7091 dbus_message_iter_append_int32 (&iter, -0x12345678);
7092 dbus_message_iter_append_uint32 (&iter, 0xedd1e);
7093 dbus_message_iter_append_double (&iter, 3.14159);
7095 dbus_message_iter_append_array (&iter, &child_iter, DBUS_TYPE_DOUBLE);
7096 dbus_message_iter_append_double (&child_iter, 1.5);
7097 dbus_message_iter_append_double (&child_iter, 2.5);
7100 dbus_message_iter_append_dict (&iter, &child_iter);
7101 dbus_message_iter_append_dict_key (&child_iter, "test");
7102 dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
7104 /* array of array of int32 (in dict) */
7105 dbus_message_iter_append_dict_key (&child_iter, "array");
7106 dbus_message_iter_append_array (&child_iter, &child_iter2, DBUS_TYPE_ARRAY);
7107 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
7108 dbus_message_iter_append_int32 (&child_iter3, 0x12345678);
7109 dbus_message_iter_append_int32 (&child_iter3, 0x23456781);
7110 _dbus_warn ("next call expected to fail with wrong array type\n");
7111 _dbus_assert (!dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_UINT32));
7112 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
7113 dbus_message_iter_append_int32 (&child_iter3, 0x34567812);
7114 dbus_message_iter_append_int32 (&child_iter3, 0x45678123);
7115 dbus_message_iter_append_int32 (&child_iter3, 0x56781234);
7117 dbus_message_iter_append_byte (&iter, 0xF0);
7119 dbus_message_iter_append_nil (&iter);
7121 dbus_message_iter_append_custom (&iter, "MyTypeName",
7124 message_iter_test (message);
7126 /* Message loader test */
7127 _dbus_message_lock (message);
7128 loader = _dbus_message_loader_new ();
7130 /* check ref/unref */
7131 _dbus_message_loader_ref (loader);
7132 _dbus_message_loader_unref (loader);
7134 /* Write the header data one byte at a time */
7135 data = _dbus_string_get_const_data (&message->header);
7136 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
7140 _dbus_message_loader_get_buffer (loader, &buffer);
7141 _dbus_string_append_byte (buffer, data[i]);
7142 _dbus_message_loader_return_buffer (loader, buffer, 1);
7145 /* Write the body data one byte at a time */
7146 data = _dbus_string_get_const_data (&message->body);
7147 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
7151 _dbus_message_loader_get_buffer (loader, &buffer);
7152 _dbus_string_append_byte (buffer, data[i]);
7153 _dbus_message_loader_return_buffer (loader, buffer, 1);
7156 copy = dbus_message_copy (message); /* save for tests below */
7157 dbus_message_unref (message);
7159 /* Now pop back the message */
7160 if (!_dbus_message_loader_queue_messages (loader))
7161 _dbus_assert_not_reached ("no memory to queue messages");
7163 if (_dbus_message_loader_get_is_corrupted (loader))
7164 _dbus_assert_not_reached ("message loader corrupted");
7166 message = _dbus_message_loader_pop_message (loader);
7168 _dbus_assert_not_reached ("received a NULL message");
7170 if (dbus_message_get_reply_serial (message) != 0x12345678)
7171 _dbus_assert_not_reached ("reply serial fields differ");
7173 message_iter_test (message);
7175 dbus_message_unref (message);
7176 _dbus_message_loader_unref (loader);
7178 message = dbus_message_new_method_return (copy);
7179 if (message == NULL)
7180 _dbus_assert_not_reached ("out of memory\n");
7181 dbus_message_unref (copy);
7183 if (!dbus_message_append_args (message,
7184 DBUS_TYPE_STRING, "hello",
7186 _dbus_assert_not_reached ("no memory");
7188 if (!dbus_message_has_signature (message, "s"))
7189 _dbus_assert_not_reached ("method return has wrong signature");
7191 dbus_error_init (&error);
7192 if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING,
7193 &t, DBUS_TYPE_INVALID))
7196 _dbus_warn ("Failed to get expected string arg: %s\n", error.message);
7201 dbus_message_unref (message);
7203 /* Now load every message in test_data_dir if we have one */
7204 if (test_data_dir == NULL)
7207 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
7208 (DBusForeachMessageFileFunc)
7209 dbus_internal_do_not_use_try_message_file,
7213 #endif /* DBUS_BUILD_TESTS */