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"
35 * @defgroup DBusMessageInternals DBusMessage implementation details
36 * @ingroup DBusInternals
37 * @brief DBusMessage private implementation details.
39 * The guts of DBusMessage and its methods.
57 static dbus_bool_t field_is_named[FIELD_LAST] =
59 FALSE, /* FIELD_HEADER_LENGTH */
60 FALSE, /* FIELD_BODY_LENGTH */
61 FALSE, /* FIELD_CLIENT_SERIAL */
62 TRUE, /* FIELD_NAME */
63 TRUE, /* FIELD_SERVICE */
64 TRUE, /* FIELD_SENDER */
65 TRUE /* FIELD_REPLY_SERIAL */
70 int offset; /**< Offset to start of field (location of name of field
76 * @brief Internals of DBusMessage
78 * Object representing a message received from or to be sent to
79 * another application. This is an opaque object, all members
84 volatile dbus_atomic_t refcount; /**< Reference count */
86 DBusString header; /**< Header network data, stored
87 * separately from body so we can
88 * independently realloc it.
91 HeaderField header_fields[FIELD_LAST]; /**< Track the location
92 * of each field in "header"
95 dbus_uint32_t client_serial; /**< Cached client serial value for speed */
96 dbus_uint32_t reply_serial; /**< Cached reply serial value for speed */
98 int header_padding; /**< bytes of alignment in header */
100 DBusString body; /**< Body network data. */
102 char byte_order; /**< Message byte order. */
104 DBusList *size_counters; /**< 0-N DBusCounter used to track message size. */
105 long size_counter_delta; /**< Size we incremented the size counters by. */
107 dbus_uint32_t changed_stamp; /**< Incremented when iterators are invalidated. */
109 unsigned int locked : 1; /**< Message being sent, no modifications allowed. */
113 DBUS_MESSAGE_ITER_TYPE_MESSAGE,
114 DBUS_MESSAGE_ITER_TYPE_ARRAY,
115 DBUS_MESSAGE_ITER_TYPE_DICT
118 /** typedef for internals of message iterator */
119 typedef struct DBusMessageRealIter DBusMessageRealIter;
122 * @brief Internals of DBusMessageIter
124 * Object representing a position in a message. All fields are internal.
126 struct DBusMessageRealIter
128 DBusMessageRealIter *parent_iter; /**< parent iter, or NULL */
129 DBusMessage *message; /**< Message used */
130 dbus_uint32_t changed_stamp; /**< stamp to detect invalid iters */
132 /* This is an int instead of an enum to get a guaranteed size for the dummy: */
133 int type; /**< type of iter */
135 int pos; /**< Current position in the string */
136 int end; /**< position right after the container */
137 int container_start; /**< offset of the start of the container */
138 int container_length_pos; /**< offset of the length of the container */
140 int wrote_dict_key; /**< whether we wrote the dict key for the current dict element */
142 int array_type_pos; /**< pointer to the position of the array element type */
143 int array_type_done; /**< TRUE if the array type is fully finished */
147 * Gets the data to be sent over the network for this message.
148 * The header and then the body should be written out.
149 * This function is guaranteed to always return the same
150 * data once a message is locked (with _dbus_message_lock()).
152 * @param message the message.
153 * @param header return location for message header data.
154 * @param body return location for message body data.
157 _dbus_message_get_network_data (DBusMessage *message,
158 const DBusString **header,
159 const DBusString **body)
161 _dbus_assert (message->locked);
163 *header = &message->header;
164 *body = &message->body;
168 clear_header_padding (DBusMessage *message)
170 _dbus_string_shorten (&message->header,
171 message->header_padding);
172 message->header_padding = 0;
176 append_header_padding (DBusMessage *message)
179 old_len = _dbus_string_get_length (&message->header);
180 if (!_dbus_string_align_length (&message->header, 8))
183 message->header_padding = _dbus_string_get_length (&message->header) - old_len;
189 adjust_field_offsets (DBusMessage *message,
199 while (i < FIELD_LAST)
201 if (message->header_fields[i].offset > offsets_after)
202 message->header_fields[i].offset += delta;
208 #ifdef DBUS_BUILD_TESTS
209 /* tests-only until it's actually used */
211 get_int_field (DBusMessage *message,
216 _dbus_assert (field < FIELD_LAST);
218 offset = message->header_fields[field].offset;
221 return -1; /* useless if -1 is a valid value of course */
223 return _dbus_demarshal_int32 (&message->header,
231 get_uint_field (DBusMessage *message,
236 _dbus_assert (field < FIELD_LAST);
238 offset = message->header_fields[field].offset;
241 return -1; /* useless if -1 is a valid value of course */
243 return _dbus_demarshal_uint32 (&message->header,
250 get_string_field (DBusMessage *message,
257 offset = message->header_fields[field].offset;
259 _dbus_assert (field < FIELD_LAST);
264 /* offset points to string length, string data follows it */
265 /* FIXME _dbus_demarshal_const_string() that returned
266 * a reference to the string plus its len might be nice.
270 *len = _dbus_demarshal_uint32 (&message->header,
275 data = _dbus_string_get_const_data (&message->header);
277 return data + (offset + 4);
280 #ifdef DBUS_BUILD_TESTS
282 append_int_field (DBusMessage *message,
289 _dbus_assert (!message->locked);
291 clear_header_padding (message);
293 orig_len = _dbus_string_get_length (&message->header);
295 if (!_dbus_string_align_length (&message->header, 4))
298 if (!_dbus_string_append_len (&message->header, name, 4))
301 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_INT32))
304 if (!_dbus_string_align_length (&message->header, 4))
307 message->header_fields[FIELD_REPLY_SERIAL].offset =
308 _dbus_string_get_length (&message->header);
310 if (!_dbus_marshal_int32 (&message->header, message->byte_order,
314 if (!append_header_padding (message))
320 message->header_fields[field].offset = -1;
321 _dbus_string_set_length (&message->header, orig_len);
323 /* this must succeed because it was allocated on function entry and
324 * DBusString doesn't ever realloc smaller
326 if (!append_header_padding (message))
327 _dbus_assert_not_reached ("failed to reappend header padding");
333 append_uint_field (DBusMessage *message,
340 _dbus_assert (!message->locked);
342 clear_header_padding (message);
344 orig_len = _dbus_string_get_length (&message->header);
346 if (!_dbus_string_align_length (&message->header, 4))
349 if (!_dbus_string_append_len (&message->header, name, 4))
352 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_UINT32))
355 if (!_dbus_string_align_length (&message->header, 4))
358 message->header_fields[FIELD_REPLY_SERIAL].offset =
359 _dbus_string_get_length (&message->header);
361 if (!_dbus_marshal_uint32 (&message->header, message->byte_order,
365 if (!append_header_padding (message))
371 message->header_fields[field].offset = -1;
372 _dbus_string_set_length (&message->header, orig_len);
374 /* this must succeed because it was allocated on function entry and
375 * DBusString doesn't ever realloc smaller
377 if (!append_header_padding (message))
378 _dbus_assert_not_reached ("failed to reappend header padding");
383 append_string_field (DBusMessage *message,
390 _dbus_assert (!message->locked);
392 clear_header_padding (message);
394 orig_len = _dbus_string_get_length (&message->header);
396 if (!_dbus_string_align_length (&message->header, 4))
399 if (!_dbus_string_append_len (&message->header, name, 4))
402 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_STRING))
405 if (!_dbus_string_align_length (&message->header, 4))
408 message->header_fields[field].offset =
409 _dbus_string_get_length (&message->header);
411 if (!_dbus_marshal_string (&message->header, message->byte_order,
415 if (!append_header_padding (message))
421 message->header_fields[field].offset = -1;
422 _dbus_string_set_length (&message->header, orig_len);
424 /* this must succeed because it was allocated on function entry and
425 * DBusString doesn't ever realloc smaller
427 if (!append_header_padding (message))
428 _dbus_assert_not_reached ("failed to reappend header padding");
433 #ifdef DBUS_BUILD_TESTS
434 /* This isn't used, but building it when tests are enabled just to
435 * keep it compiling if we need it in future
438 delete_int_or_uint_field (DBusMessage *message,
441 int offset = message->header_fields[field].offset;
443 _dbus_assert (!message->locked);
444 _dbus_assert (field_is_named[field]);
449 clear_header_padding (message);
451 /* The field typecode and name take up 8 bytes */
452 _dbus_string_delete (&message->header,
456 message->header_fields[field].offset = -1;
458 adjust_field_offsets (message,
462 append_header_padding (message);
467 delete_string_field (DBusMessage *message,
470 int offset = message->header_fields[field].offset;
474 _dbus_assert (!message->locked);
475 _dbus_assert (field_is_named[field]);
480 clear_header_padding (message);
482 get_string_field (message, field, &len);
484 /* The field typecode and name take up 8 bytes, and the nul
485 * termination is 1 bytes, string length integer is 4 bytes
487 delete_len = 8 + 4 + 1 + len;
489 _dbus_string_delete (&message->header,
493 message->header_fields[field].offset = -1;
495 adjust_field_offsets (message,
499 append_header_padding (message);
502 #ifdef DBUS_BUILD_TESTS
504 set_int_field (DBusMessage *message,
508 int offset = message->header_fields[field].offset;
510 _dbus_assert (!message->locked);
514 /* need to append the field */
519 _dbus_assert_not_reached ("appending an int field we don't support appending");
525 _dbus_marshal_set_int32 (&message->header,
535 set_uint_field (DBusMessage *message,
539 int offset = message->header_fields[field].offset;
541 _dbus_assert (!message->locked);
545 /* need to append the field */
549 case FIELD_REPLY_SERIAL:
550 return append_uint_field (message, field,
551 DBUS_HEADER_FIELD_REPLY,
554 _dbus_assert_not_reached ("appending a uint field we don't support appending");
560 _dbus_marshal_set_uint32 (&message->header,
569 set_string_field (DBusMessage *message,
573 int offset = message->header_fields[field].offset;
575 _dbus_assert (!message->locked);
576 _dbus_assert (value != NULL);
580 /* need to append the field */
585 return append_string_field (message, field,
586 DBUS_HEADER_FIELD_SENDER,
589 _dbus_assert_not_reached ("appending a string field we don't support appending");
600 clear_header_padding (message);
602 old_len = _dbus_string_get_length (&message->header);
604 len = strlen (value);
606 _dbus_string_init_const_len (&v, value,
607 len + 1); /* include nul */
608 if (!_dbus_marshal_set_string (&message->header,
614 new_len = _dbus_string_get_length (&message->header);
616 adjust_field_offsets (message,
620 if (!append_header_padding (message))
626 /* this must succeed because it was allocated on function entry and
627 * DBusString doesn't ever realloc smaller
629 if (!append_header_padding (message))
630 _dbus_assert_not_reached ("failed to reappend header padding");
637 * Sets the serial number of a message.
638 * This can only be done once on a message.
640 * @param message the message
641 * @param serial the serial
644 _dbus_message_set_serial (DBusMessage *message,
647 _dbus_assert (!message->locked);
648 _dbus_assert (dbus_message_get_serial (message) == 0);
650 set_uint_field (message, FIELD_CLIENT_SERIAL,
652 message->client_serial = serial;
656 * Sets the reply serial of a message (the client serial
657 * of the message this is a reply to).
659 * @param message the message
660 * @param reply_serial the client serial
661 * @returns #FALSE if not enough memory
664 dbus_message_set_reply_serial (DBusMessage *message,
665 dbus_uint32_t reply_serial)
667 _dbus_assert (!message->locked);
669 if (set_uint_field (message, FIELD_REPLY_SERIAL,
672 message->reply_serial = reply_serial;
680 * Returns the serial of a message or -1 if none has been specified.
681 * The message's serial number is provided by the application sending
682 * the message and is used to identify replies to this message.
684 * @param message the message
685 * @returns the client serial
688 dbus_message_get_serial (DBusMessage *message)
690 return message->client_serial;
694 * Returns the serial that the message is
695 * a reply to or 0 if none.
697 * @param message the message
698 * @returns the reply serial
701 dbus_message_get_reply_serial (DBusMessage *message)
703 return message->reply_serial;
707 * Adds a counter to be incremented immediately with the
708 * size of this message, and decremented by the size
709 * of this message when this message if finalized.
710 * The link contains a counter with its refcount already
711 * incremented, but the counter itself not incremented.
712 * Ownership of link and counter refcount is passed to
715 * @param message the message
716 * @param link link with counter as data
719 _dbus_message_add_size_counter_link (DBusMessage *message,
722 /* right now we don't recompute the delta when message
723 * size changes, and that's OK for current purposes
724 * I think, but could be important to change later.
725 * Do recompute it whenever there are no outstanding counters,
726 * since it's basically free.
728 if (message->size_counters == NULL)
730 message->size_counter_delta =
731 _dbus_string_get_length (&message->header) +
732 _dbus_string_get_length (&message->body);
735 _dbus_verbose ("message has size %ld\n",
736 message->size_counter_delta);
740 _dbus_list_append_link (&message->size_counters, link);
742 _dbus_counter_adjust (link->data, message->size_counter_delta);
746 * Adds a counter to be incremented immediately with the
747 * size of this message, and decremented by the size
748 * of this message when this message if finalized.
750 * @param message the message
751 * @param counter the counter
752 * @returns #FALSE if no memory
755 _dbus_message_add_size_counter (DBusMessage *message,
756 DBusCounter *counter)
760 link = _dbus_list_alloc_link (counter);
764 _dbus_counter_ref (counter);
765 _dbus_message_add_size_counter_link (message, link);
771 * Removes a counter tracking the size of this message, and decrements
772 * the counter by the size of this message.
774 * @param message the message
775 * @param counter the counter
778 _dbus_message_remove_size_counter (DBusMessage *message,
779 DBusCounter *counter)
781 if (!_dbus_list_remove_last (&message->size_counters,
783 _dbus_assert_not_reached ("Removed a message size counter that was not added");
785 _dbus_counter_adjust (counter, message->size_counter_delta);
787 _dbus_counter_unref (counter);
791 dbus_message_create_header (DBusMessage *message,
797 if (!_dbus_string_append_byte (&message->header, message->byte_order))
801 if (!_dbus_string_append_byte (&message->header, flags))
804 if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
807 if (!_dbus_string_append_byte (&message->header, 0))
810 message->header_fields[FIELD_HEADER_LENGTH].offset = 4;
811 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
814 message->header_fields[FIELD_BODY_LENGTH].offset = 8;
815 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
818 message->header_fields[FIELD_CLIENT_SERIAL].offset = 12;
819 if (!_dbus_marshal_int32 (&message->header, message->byte_order, -1))
822 /* Marshal message service */
825 if (!append_string_field (message,
827 DBUS_HEADER_FIELD_SERVICE,
832 _dbus_assert (name != NULL);
833 if (!append_string_field (message,
835 DBUS_HEADER_FIELD_NAME,
843 * Locks a message. Allows checking that applications don't keep a
844 * reference to a message in the outgoing queue and change it
845 * underneath us. Messages are locked when they enter the outgoing
846 * queue (dbus_connection_send_message()), and the library complains
847 * if the message is modified while locked.
849 * @param message the message to lock.
852 _dbus_message_lock (DBusMessage *message)
854 if (!message->locked)
856 /* Fill in our lengths */
857 set_uint_field (message,
859 _dbus_string_get_length (&message->header));
861 set_uint_field (message,
863 _dbus_string_get_length (&message->body));
865 message->locked = TRUE;
872 * @defgroup DBusMessage DBusMessage
874 * @brief Message to be sent or received over a DBusConnection.
876 * A DBusMessage is the most basic unit of communication over a
877 * DBusConnection. A DBusConnection represents a stream of messages
878 * received from a remote application, and a stream of messages
879 * sent to a remote application.
885 * @typedef DBusMessage
887 * Opaque data type representing a message received from or to be
888 * sent to another application.
892 dbus_message_new_empty_header (void)
894 DBusMessage *message;
897 message = dbus_new0 (DBusMessage, 1);
901 message->refcount = 1;
902 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
903 message->client_serial = 0;
904 message->reply_serial = 0;
907 while (i < FIELD_LAST)
909 message->header_fields[i].offset = -1;
913 if (!_dbus_string_init (&message->header))
919 if (!_dbus_string_init (&message->body))
921 _dbus_string_free (&message->header);
931 * Constructs a new message. Returns #NULL if memory can't be
932 * allocated for the message. The service may be #NULL in which case
933 * no service is set; this is appropriate when using D-BUS in a
934 * peer-to-peer context (no message bus).
936 * @todo reverse the arguments, first 'name' then 'service'
937 * as 'name' is more fundamental
939 * @param name name of the message
940 * @param destination_service service that the message should be sent to or #NULL
941 * @returns a new DBusMessage, free with dbus_message_unref()
942 * @see dbus_message_unref()
945 dbus_message_new (const char *name,
946 const char *destination_service)
948 DBusMessage *message;
950 _dbus_return_val_if_fail (name != NULL, NULL);
952 message = dbus_message_new_empty_header ();
956 if (!dbus_message_create_header (message, name, destination_service))
958 dbus_message_unref (message);
966 * Constructs a message that is a reply to some other
967 * message. Returns #NULL if memory can't be allocated
970 * @param original_message the message which the created
971 * message is a reply to.
972 * @returns a new DBusMessage, free with dbus_message_unref()
973 * @see dbus_message_new(), dbus_message_unref()
976 dbus_message_new_reply (DBusMessage *original_message)
978 DBusMessage *message;
979 const char *sender, *name;
981 _dbus_return_val_if_fail (original_message != NULL, NULL);
983 sender = get_string_field (original_message,
985 name = get_string_field (original_message,
988 /* sender is allowed to be null here in peer-to-peer case */
990 message = dbus_message_new (name, sender);
995 if (!dbus_message_set_reply_serial (message,
996 dbus_message_get_serial (original_message)))
998 dbus_message_unref (message);
1006 * Creates a new message that is an error reply to a certain message.
1008 * @param original_message the original message
1009 * @param error_name the error name
1010 * @param error_message the error message string or #NULL for none
1011 * @returns a new error message
1014 dbus_message_new_error_reply (DBusMessage *original_message,
1015 const char *error_name,
1016 const char *error_message)
1018 DBusMessage *message;
1020 DBusMessageIter iter;
1022 _dbus_return_val_if_fail (original_message != NULL, NULL);
1023 _dbus_return_val_if_fail (error_name != NULL, NULL);
1025 sender = get_string_field (original_message,
1026 FIELD_SENDER, NULL);
1028 /* sender may be NULL for non-message-bus case or
1029 * when the message bus is dealing with an unregistered
1033 message = dbus_message_new (error_name, sender);
1035 if (message == NULL)
1038 if (!dbus_message_set_reply_serial (message,
1039 dbus_message_get_serial (original_message)))
1041 dbus_message_unref (message);
1045 if (error_message != NULL)
1047 dbus_message_append_iter_init (message, &iter);
1048 if (!dbus_message_iter_append_string (&iter, error_message))
1050 dbus_message_unref (message);
1055 dbus_message_set_is_error (message, TRUE);
1061 * Creates a new message that is an exact replica of the message
1062 * specified, except that its refcount is set to 1.
1064 * @param message the message.
1065 * @returns the new message.
1068 dbus_message_copy (const DBusMessage *message)
1070 DBusMessage *retval;
1073 _dbus_return_val_if_fail (message != NULL, NULL);
1075 retval = dbus_new0 (DBusMessage, 1);
1079 retval->refcount = 1;
1080 retval->byte_order = message->byte_order;
1081 retval->client_serial = message->client_serial;
1082 retval->reply_serial = message->reply_serial;
1083 retval->header_padding = message->header_padding;
1084 retval->locked = FALSE;
1086 if (!_dbus_string_init (&retval->header))
1092 if (!_dbus_string_init (&retval->body))
1094 _dbus_string_free (&retval->header);
1099 if (!_dbus_string_copy (&message->header, 0,
1100 &retval->header, 0))
1102 _dbus_string_free (&retval->header);
1103 _dbus_string_free (&retval->body);
1109 if (!_dbus_string_copy (&message->body, 0,
1112 _dbus_string_free (&retval->header);
1113 _dbus_string_free (&retval->body);
1119 for (i = 0; i < FIELD_LAST; i++)
1121 retval->header_fields[i].offset = message->header_fields[i].offset;
1129 * Increments the reference count of a DBusMessage.
1131 * @param message The message
1132 * @see dbus_message_unref
1135 dbus_message_ref (DBusMessage *message)
1137 volatile dbus_atomic_t refcount;
1139 _dbus_return_if_fail (message != NULL);
1141 refcount = _dbus_atomic_inc (&message->refcount);
1142 _dbus_assert (refcount > 1);
1146 free_size_counter (void *element,
1149 DBusCounter *counter = element;
1150 DBusMessage *message = data;
1152 _dbus_counter_adjust (counter, - message->size_counter_delta);
1154 _dbus_counter_unref (counter);
1158 * Decrements the reference count of a DBusMessage.
1160 * @param message The message
1161 * @see dbus_message_ref
1164 dbus_message_unref (DBusMessage *message)
1166 volatile dbus_atomic_t refcount;
1168 _dbus_return_if_fail (message != NULL);
1170 refcount = _dbus_atomic_dec (&message->refcount);
1172 _dbus_assert (refcount >= 0);
1176 _dbus_list_foreach (&message->size_counters,
1177 free_size_counter, message);
1178 _dbus_list_clear (&message->size_counters);
1180 _dbus_string_free (&message->header);
1181 _dbus_string_free (&message->body);
1183 dbus_free (message);
1188 * Gets the name of a message.
1190 * @param message the message
1191 * @returns the message name (should not be freed)
1194 dbus_message_get_name (DBusMessage *message)
1196 _dbus_return_val_if_fail (message != NULL, NULL);
1198 return get_string_field (message, FIELD_NAME, NULL);
1202 * Gets the destination service of a message.
1204 * @param message the message
1205 * @returns the message destination service (should not be freed)
1208 dbus_message_get_destination (DBusMessage *message)
1210 _dbus_return_val_if_fail (message != NULL, NULL);
1212 return get_string_field (message, FIELD_SERVICE, NULL);
1216 * Appends fields to a message given a variable argument list. The
1217 * variable argument list should contain the type of the argument
1218 * followed by the value to add. Array values are specified by an int
1219 * typecode followed by a pointer to the array followed by an int
1220 * giving the length of the array. The argument list must be
1221 * terminated with DBUS_TYPE_INVALID.
1223 * This function doesn't support dicts or non-fundamental arrays.
1225 * This function supports #DBUS_TYPE_INT64 and #DBUS_TYPE_UINT64
1226 * only if #DBUS_HAVE_INT64 is defined.
1228 * @param message the message
1229 * @param first_arg_type type of the first argument
1230 * @param ... value of first argument, list of additional type-value pairs
1231 * @returns #TRUE on success
1234 dbus_message_append_args (DBusMessage *message,
1241 _dbus_return_val_if_fail (message != NULL, FALSE);
1243 va_start (var_args, first_arg_type);
1244 retval = dbus_message_append_args_valist (message,
1253 * This function takes a va_list for use by language bindings.
1254 * It's otherwise the same as dbus_message_append_args().
1256 * @todo: Shouldn't this function clean up the changes to the message
1257 * on failures? (Yes)
1259 * @see dbus_message_append_args.
1260 * @param message the message
1261 * @param first_arg_type type of first argument
1262 * @param var_args value of first argument, then list of type/value pairs
1263 * @returns #TRUE on success
1266 dbus_message_append_args_valist (DBusMessage *message,
1271 DBusMessageIter iter;
1273 _dbus_return_val_if_fail (message != NULL, FALSE);
1275 old_len = _dbus_string_get_length (&message->body);
1277 type = first_arg_type;
1279 dbus_message_append_iter_init (message, &iter);
1286 if (!dbus_message_iter_append_nil (&iter))
1289 case DBUS_TYPE_BOOLEAN:
1290 if (!dbus_message_iter_append_boolean (&iter, va_arg (var_args, dbus_bool_t)))
1293 case DBUS_TYPE_INT32:
1294 if (!dbus_message_iter_append_int32 (&iter, va_arg (var_args, dbus_int32_t)))
1297 case DBUS_TYPE_UINT32:
1298 if (!dbus_message_iter_append_uint32 (&iter, va_arg (var_args, dbus_uint32_t)))
1301 #ifdef DBUS_HAVE_INT64
1302 case DBUS_TYPE_INT64:
1303 if (!dbus_message_iter_append_int64 (&iter, va_arg (var_args, dbus_int64_t)))
1306 case DBUS_TYPE_UINT64:
1307 if (!dbus_message_iter_append_uint64 (&iter, va_arg (var_args, dbus_uint64_t)))
1310 #endif /* DBUS_HAVE_INT64 */
1311 case DBUS_TYPE_DOUBLE:
1312 if (!dbus_message_iter_append_double (&iter, va_arg (var_args, double)))
1315 case DBUS_TYPE_STRING:
1316 if (!dbus_message_iter_append_string (&iter, va_arg (var_args, const char *)))
1319 case DBUS_TYPE_NAMED:
1322 unsigned char *data;
1325 name = va_arg (var_args, const char *);
1326 data = va_arg (var_args, unsigned char *);
1327 len = va_arg (var_args, int);
1329 if (!dbus_message_iter_append_named (&iter, name, data, len))
1333 case DBUS_TYPE_ARRAY:
1338 type = va_arg (var_args, int);
1339 data = va_arg (var_args, void *);
1340 len = va_arg (var_args, int);
1344 case DBUS_TYPE_BYTE:
1345 if (!dbus_message_iter_append_byte_array (&iter, (unsigned char *)data, len))
1348 case DBUS_TYPE_BOOLEAN:
1349 if (!dbus_message_iter_append_boolean_array (&iter, (unsigned char *)data, len))
1352 case DBUS_TYPE_INT32:
1353 if (!dbus_message_iter_append_int32_array (&iter, (dbus_int32_t *)data, len))
1356 case DBUS_TYPE_UINT32:
1357 if (!dbus_message_iter_append_uint32_array (&iter, (dbus_uint32_t *)data, len))
1360 #ifdef DBUS_HAVE_INT64
1361 case DBUS_TYPE_INT64:
1362 if (!dbus_message_iter_append_int64_array (&iter, (dbus_int64_t *)data, len))
1365 case DBUS_TYPE_UINT64:
1366 if (!dbus_message_iter_append_uint64_array (&iter, (dbus_uint64_t *)data, len))
1369 #endif /* DBUS_HAVE_INT64 */
1370 case DBUS_TYPE_DOUBLE:
1371 if (!dbus_message_iter_append_double_array (&iter, (double *)data, len))
1374 case DBUS_TYPE_STRING:
1375 if (!dbus_message_iter_append_string_array (&iter, (const char **)data, len))
1379 case DBUS_TYPE_ARRAY:
1380 case DBUS_TYPE_NAMED:
1381 case DBUS_TYPE_DICT:
1382 _dbus_warn ("dbus_message_append_args_valist doesn't support recursive arrays\n");
1385 _dbus_warn ("Unknown field type %d\n", type);
1391 case DBUS_TYPE_DICT:
1392 _dbus_warn ("dbus_message_append_args_valist doesn't support dicts\n");
1395 _dbus_warn ("Unknown field type %d\n", type);
1399 type = va_arg (var_args, int);
1410 * Gets arguments from a message given a variable argument list.
1411 * The variable argument list should contain the type of the
1412 * argumen followed by a pointer to where the value should be
1413 * stored. The list is terminated with 0.
1415 * @param message the message
1416 * @param error error to be filled in on failure
1417 * @param first_arg_type the first argument type
1418 * @param ... location for first argument value, then list of type-location pairs
1419 * @returns #FALSE if the error was set
1422 dbus_message_get_args (DBusMessage *message,
1430 _dbus_return_val_if_fail (message != NULL, FALSE);
1431 _dbus_return_val_if_error_is_set (error, FALSE);
1433 va_start (var_args, first_arg_type);
1434 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1441 * This function takes a va_list for use by language bindings
1443 * @todo We need to free the argument data when an error occurs.
1445 * @see dbus_message_get_args
1446 * @param message the message
1447 * @param error error to be filled in
1448 * @param first_arg_type type of the first argument
1449 * @param var_args return location for first argument, followed by list of type/location pairs
1450 * @returns #FALSE if error was set
1453 dbus_message_get_args_valist (DBusMessage *message,
1458 DBusMessageIter iter;
1460 _dbus_return_val_if_fail (message != NULL, FALSE);
1461 _dbus_return_val_if_error_is_set (error, FALSE);
1463 dbus_message_iter_init (message, &iter);
1464 return dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
1468 * Gets arguments from a message iterator given a variable argument list.
1469 * The variable argument list should contain the type of the
1470 * argumen followed by a pointer to where the value should be
1471 * stored. The list is terminated with 0.
1473 * @param iter the message iterator
1474 * @param error error to be filled in on failure
1475 * @param first_arg_type the first argument type
1476 * @param ... location for first argument value, then list of type-location pairs
1477 * @returns #FALSE if the error was set
1480 dbus_message_iter_get_args (DBusMessageIter *iter,
1488 _dbus_return_val_if_fail (iter != NULL, FALSE);
1489 _dbus_return_val_if_error_is_set (error, FALSE);
1491 va_start (var_args, first_arg_type);
1492 retval = dbus_message_iter_get_args_valist (iter, error, first_arg_type, var_args);
1499 * This function takes a va_list for use by language bindings
1501 * This function supports #DBUS_TYPE_INT64 and #DBUS_TYPE_UINT64
1502 * only if #DBUS_HAVE_INT64 is defined.
1504 * @todo this function (or some lower-level non-convenience function)
1505 * needs better error handling; should allow the application to
1506 * distinguish between out of memory, and bad data from the remote
1507 * app. It also needs to not leak a bunch of args when it gets
1508 * to the arg that's bad, as that would be a security hole
1509 * (allow one app to force another to leak memory)
1511 * @todo We need to free the argument data when an error occurs.
1513 * @see dbus_message_get_args
1514 * @param iter the message iter
1515 * @param error error to be filled in
1516 * @param first_arg_type type of the first argument
1517 * @param var_args return location for first argument, followed by list of type/location pairs
1518 * @returns #FALSE if error was set
1521 dbus_message_iter_get_args_valist (DBusMessageIter *iter,
1526 int spec_type, msg_type, i;
1529 _dbus_return_val_if_fail (iter != NULL, FALSE);
1530 _dbus_return_val_if_error_is_set (error, FALSE);
1534 spec_type = first_arg_type;
1537 while (spec_type != 0)
1539 msg_type = dbus_message_iter_get_arg_type (iter);
1541 if (msg_type != spec_type)
1543 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1544 "Argument %d is specified to be of type \"%s\", but "
1545 "is actually of type \"%s\"\n", i,
1546 _dbus_type_to_string (spec_type),
1547 _dbus_type_to_string (msg_type));
1556 case DBUS_TYPE_BYTE:
1560 ptr = va_arg (var_args, unsigned char *);
1562 *ptr = dbus_message_iter_get_byte (iter);
1565 case DBUS_TYPE_BOOLEAN:
1569 ptr = va_arg (var_args, dbus_bool_t *);
1571 *ptr = dbus_message_iter_get_boolean (iter);
1574 case DBUS_TYPE_INT32:
1578 ptr = va_arg (var_args, dbus_int32_t *);
1580 *ptr = dbus_message_iter_get_int32 (iter);
1583 case DBUS_TYPE_UINT32:
1587 ptr = va_arg (var_args, dbus_uint32_t *);
1589 *ptr = dbus_message_iter_get_uint32 (iter);
1592 #ifdef DBUS_HAVE_INT64
1593 case DBUS_TYPE_INT64:
1597 ptr = va_arg (var_args, dbus_int64_t *);
1599 *ptr = dbus_message_iter_get_int64 (iter);
1602 case DBUS_TYPE_UINT64:
1606 ptr = va_arg (var_args, dbus_uint64_t *);
1608 *ptr = dbus_message_iter_get_uint64 (iter);
1611 #endif /* DBUS_HAVE_INT64 */
1613 case DBUS_TYPE_DOUBLE:
1617 ptr = va_arg (var_args, double *);
1619 *ptr = dbus_message_iter_get_double (iter);
1623 case DBUS_TYPE_STRING:
1627 ptr = va_arg (var_args, char **);
1629 *ptr = dbus_message_iter_get_string (iter);
1633 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1640 case DBUS_TYPE_NAMED:
1643 unsigned char **data;
1646 name = va_arg (var_args, char **);
1647 data = va_arg (var_args, unsigned char **);
1648 len = va_arg (var_args, int *);
1650 if (!dbus_message_iter_get_named (iter, name, data, len))
1652 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1657 case DBUS_TYPE_ARRAY:
1662 type = va_arg (var_args, int);
1663 data = va_arg (var_args, void *);
1664 len = va_arg (var_args, int *);
1666 if (dbus_message_iter_get_array_type (iter) != type)
1668 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1669 "Argument %d is specified to be of type \"array of %s\", but "
1670 "is actually of type \"array of %s\"\n", i,
1671 _dbus_type_to_string (type),
1672 _dbus_type_to_string (dbus_message_iter_get_array_type (iter)));
1678 case DBUS_TYPE_BYTE:
1679 if (!dbus_message_iter_get_byte_array (iter, (unsigned char **)data, len))
1681 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1685 case DBUS_TYPE_BOOLEAN:
1686 if (!dbus_message_iter_get_boolean_array (iter, (unsigned char **)data, len))
1688 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1692 case DBUS_TYPE_INT32:
1693 if (!dbus_message_iter_get_int32_array (iter, (dbus_int32_t **)data, len))
1695 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1699 case DBUS_TYPE_UINT32:
1700 if (!dbus_message_iter_get_uint32_array (iter, (dbus_uint32_t **)data, len))
1702 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1706 #ifdef DBUS_HAVE_INT64
1707 case DBUS_TYPE_INT64:
1708 if (!dbus_message_iter_get_int64_array (iter, (dbus_int64_t **)data, len))
1710 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1714 case DBUS_TYPE_UINT64:
1715 if (!dbus_message_iter_get_uint64_array (iter, (dbus_uint64_t **)data, len))
1717 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1721 #endif /* DBUS_HAVE_INT64 */
1722 case DBUS_TYPE_DOUBLE:
1723 if (!dbus_message_iter_get_double_array (iter, (double **)data, len))
1725 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1729 case DBUS_TYPE_STRING:
1730 if (!dbus_message_iter_get_string_array (iter, (char ***)data, len))
1732 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1737 case DBUS_TYPE_ARRAY:
1738 case DBUS_TYPE_NAMED:
1739 case DBUS_TYPE_DICT:
1740 _dbus_warn ("dbus_message_get_args_valist doesn't support recursive arrays\n");
1741 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
1744 _dbus_warn ("Unknown field type %d\n", type);
1745 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
1750 case DBUS_TYPE_DICT:
1751 _dbus_warn ("dbus_message_get_args_valist doesn't support dicts\n");
1752 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
1755 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
1756 _dbus_warn ("Unknown field type %d\n", spec_type);
1760 spec_type = va_arg (var_args, int);
1761 if (spec_type != 0 && !dbus_message_iter_next (iter))
1763 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1764 "Message has only %d arguments, but more were expected", i);
1780 * Initializes a DBusMessageIter representing the arguments of the
1781 * message passed in.
1783 * @param message the message
1784 * @param iter pointer to an iterator to initialize
1787 dbus_message_iter_init (DBusMessage *message,
1788 DBusMessageIter *iter)
1790 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1792 _dbus_return_if_fail (message != NULL);
1793 _dbus_return_if_fail (iter != NULL);
1795 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
1797 real->message = message;
1798 real->parent_iter = NULL;
1799 real->changed_stamp = message->changed_stamp;
1801 real->type = DBUS_MESSAGE_ITER_TYPE_MESSAGE;
1803 real->end = _dbus_string_get_length (&message->body);
1805 real->container_start = 0;
1806 real->container_length_pos = 0;
1807 real->wrote_dict_key = 0;
1808 real->array_type_pos = 0;
1811 #ifndef DBUS_DISABLE_CHECKS
1813 dbus_message_iter_check (DBusMessageRealIter *iter)
1817 _dbus_warn ("dbus iterator check failed: iterator is NULL\n");
1821 if (iter->changed_stamp != iter->message->changed_stamp)
1823 _dbus_warn ("dbus iterator check failed: invalid iterator, must re-initialize it after modifying the message\n");
1827 if (iter->pos < 0 || iter->pos > iter->end)
1829 _dbus_warn ("dbus iterator check failed: invalid position\n");
1835 #endif /* DBUS_DISABLE_CHECKS */
1838 skip_array_type (DBusMessageRealIter *iter, int pos)
1844 data = _dbus_string_get_const_data_len (&iter->message->body,
1847 while (*data == DBUS_TYPE_ARRAY);
1853 dbus_message_iter_get_data_start (DBusMessageRealIter *iter, int *type)
1860 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
1861 data = _dbus_string_get_const_data_len (&iter->message->body,
1863 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
1866 *type = DBUS_TYPE_INVALID;
1868 return skip_array_type (iter, iter->pos);
1870 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
1871 data = _dbus_string_get_const_data_len (&iter->message->body,
1872 iter->array_type_pos, 1);
1873 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
1876 *type = DBUS_TYPE_INVALID;
1880 case DBUS_MESSAGE_ITER_TYPE_DICT:
1881 /* Get the length of the string */
1882 len = _dbus_demarshal_uint32 (&iter->message->body,
1883 iter->message->byte_order,
1885 pos = pos + len + 1;
1887 data = _dbus_string_get_const_data_len (&iter->message->body,
1889 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
1892 *type = DBUS_TYPE_INVALID;
1894 return skip_array_type (iter, pos);
1897 _dbus_assert_not_reached ("Invalid iter type");
1900 *type = DBUS_TYPE_INVALID;
1906 * Checks if an iterator has any more fields.
1908 * @param iter the message iter
1909 * @returns #TRUE if there are more fields
1913 dbus_message_iter_has_next (DBusMessageIter *iter)
1915 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1919 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
1921 if (real->pos >= real->end)
1924 pos = dbus_message_iter_get_data_start (real, &type);
1926 if (!_dbus_marshal_get_arg_end_pos (&real->message->body,
1927 real->message->byte_order,
1928 type, pos, &end_pos))
1931 if (end_pos >= real->end)
1938 * Moves the iterator to the next field.
1940 * @param iter The message iter
1941 * @returns #TRUE if the iterator was moved to the next field
1944 dbus_message_iter_next (DBusMessageIter *iter)
1946 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1950 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
1952 pos = dbus_message_iter_get_data_start (real, &type);
1954 if (!_dbus_marshal_get_arg_end_pos (&real->message->body,
1955 real->message->byte_order,
1956 type, pos, &end_pos))
1959 if (end_pos >= real->end)
1962 real->pos = end_pos;
1968 * Returns the argument type of the argument that the
1969 * message iterator points at.
1971 * @param iter the message iter
1972 * @returns the field type
1975 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1977 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1980 _dbus_return_val_if_fail (dbus_message_iter_check (real), DBUS_TYPE_INVALID);
1982 if (real->pos >= real->end)
1983 return DBUS_TYPE_INVALID;
1985 pos = dbus_message_iter_get_data_start (real, &type);
1991 iter_get_array_type (DBusMessageRealIter *iter, int *array_type_pos)
1994 int _array_type_pos;
1999 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
2000 _array_type_pos = iter->pos + 1;
2002 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
2003 _array_type_pos = iter->array_type_pos + 1;
2005 case DBUS_MESSAGE_ITER_TYPE_DICT:
2006 /* Get the length of the string */
2007 len = _dbus_demarshal_uint32 (&iter->message->body,
2008 iter->message->byte_order,
2010 pos = pos + len + 1;
2011 data = _dbus_string_get_const_data_len (&iter->message->body,
2013 _array_type_pos = pos + 1;
2016 _dbus_assert_not_reached ("wrong iter type");
2017 return DBUS_TYPE_INVALID;
2020 if (array_type_pos != NULL)
2021 *array_type_pos = _array_type_pos;
2023 data = _dbus_string_get_const_data_len (&iter->message->body,
2024 _array_type_pos, 1);
2025 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
2028 return DBUS_TYPE_INVALID;
2033 * Returns the element type of the array that the
2034 * message iterator points at. Note that you need
2035 * to check that the iterator points to an array
2036 * prior to using this function.
2038 * @param iter the message iter
2039 * @returns the field type
2042 dbus_message_iter_get_array_type (DBusMessageIter *iter)
2044 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2047 _dbus_return_val_if_fail (dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2049 if (real->pos >= real->end)
2050 return DBUS_TYPE_INVALID;
2052 pos = dbus_message_iter_get_data_start (real, &type);
2054 _dbus_assert (type == DBUS_TYPE_ARRAY);
2056 return iter_get_array_type (real, NULL);
2061 * Returns the string value that an iterator may point to.
2062 * Note that you need to check that the iterator points to
2063 * a string value before using this function.
2065 * @see dbus_message_iter_get_arg_type
2066 * @param iter the message iter
2067 * @returns the string
2070 dbus_message_iter_get_string (DBusMessageIter *iter)
2072 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2075 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
2077 pos = dbus_message_iter_get_data_start (real, &type);
2079 _dbus_assert (type == DBUS_TYPE_STRING);
2081 return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2086 * Returns the name and data from a named type that an
2087 * iterator may point to. Note that you need to check that
2088 * the iterator points to a named type before using this
2091 * @see dbus_message_iter_get_arg_type
2092 * @param iter the message iter
2093 * @param name return location for the name
2094 * @param value return location for data
2095 * @param len return location for length of data
2096 * @returns TRUE if get succeed
2100 dbus_message_iter_get_named (DBusMessageIter *iter,
2102 unsigned char **value,
2105 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2109 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2111 pos = dbus_message_iter_get_data_start (real, &type);
2113 _dbus_assert (type == DBUS_TYPE_NAMED);
2115 _name = _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2121 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
2122 pos + 1, NULL, value, len))
2134 * Returns the byte value that an iterator may point to.
2135 * Note that you need to check that the iterator points to
2136 * a byte value before using this function.
2138 * @see dbus_message_iter_get_arg_type
2139 * @param iter the message iter
2140 * @returns the byte value
2143 dbus_message_iter_get_byte (DBusMessageIter *iter)
2145 unsigned char value;
2146 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2149 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2151 pos = dbus_message_iter_get_data_start (real, &type);
2153 _dbus_assert (type == DBUS_TYPE_BYTE);
2155 value = _dbus_string_get_byte (&real->message->body, pos);
2162 * Returns the boolean value that an iterator may point to.
2163 * Note that you need to check that the iterator points to
2164 * a boolean value before using this function.
2166 * @see dbus_message_iter_get_arg_type
2167 * @param iter the message iter
2168 * @returns the boolean value
2171 dbus_message_iter_get_boolean (DBusMessageIter *iter)
2173 unsigned char value;
2174 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2177 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2179 pos = dbus_message_iter_get_data_start (real, &type);
2181 _dbus_assert (type == DBUS_TYPE_BOOLEAN);
2183 value = _dbus_string_get_byte (&real->message->body, pos);
2189 * Returns the 32 bit signed integer value that an iterator may point to.
2190 * Note that you need to check that the iterator points to
2191 * a 32-bit integer value before using this function.
2193 * @see dbus_message_iter_get_arg_type
2194 * @param iter the message iter
2195 * @returns the integer
2198 dbus_message_iter_get_int32 (DBusMessageIter *iter)
2200 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2203 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2205 pos = dbus_message_iter_get_data_start (real, &type);
2207 _dbus_assert (type == DBUS_TYPE_INT32);
2209 return _dbus_demarshal_int32 (&real->message->body, real->message->byte_order,
2214 * Returns the 32 bit unsigned integer value that an iterator may point to.
2215 * Note that you need to check that the iterator points to
2216 * a 32-bit unsigned integer value before using this function.
2218 * @see dbus_message_iter_get_arg_type
2219 * @param iter the message iter
2220 * @returns the integer
2223 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
2225 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2228 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2230 pos = dbus_message_iter_get_data_start (real, &type);
2232 _dbus_assert (type == DBUS_TYPE_UINT32);
2234 return _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
2238 #ifdef DBUS_HAVE_INT64
2241 * Returns the 64 bit signed integer value that an iterator may point
2242 * to. Note that you need to check that the iterator points to a
2243 * 64-bit integer value before using this function.
2245 * This function only exists if #DBUS_HAVE_INT64 is defined.
2247 * @see dbus_message_iter_get_arg_type
2248 * @param iter the message iter
2249 * @returns the integer
2252 dbus_message_iter_get_int64 (DBusMessageIter *iter)
2254 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2257 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2259 pos = dbus_message_iter_get_data_start (real, &type);
2261 _dbus_assert (type == DBUS_TYPE_INT64);
2263 return _dbus_demarshal_int64 (&real->message->body, real->message->byte_order,
2268 * Returns the 64 bit unsigned integer value that an iterator may point to.
2269 * Note that you need to check that the iterator points to
2270 * a 64-bit unsigned integer value before using this function.
2272 * This function only exists if #DBUS_HAVE_INT64 is defined.
2274 * @see dbus_message_iter_get_arg_type
2275 * @param iter the message iter
2276 * @returns the integer
2279 dbus_message_iter_get_uint64 (DBusMessageIter *iter)
2281 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2284 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
2286 pos = dbus_message_iter_get_data_start (real, &type);
2288 _dbus_assert (type == DBUS_TYPE_UINT64);
2290 return _dbus_demarshal_uint64 (&real->message->body, real->message->byte_order,
2294 #endif /* DBUS_HAVE_INT64 */
2297 * Returns the double value that an iterator may point to.
2298 * Note that you need to check that the iterator points to
2299 * a string value before using this function.
2301 * @see dbus_message_iter_get_arg_type
2302 * @param iter the message iter
2303 * @returns the double
2306 dbus_message_iter_get_double (DBusMessageIter *iter)
2308 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2311 _dbus_return_val_if_fail (dbus_message_iter_check (real), 0.0);
2313 pos = dbus_message_iter_get_data_start (real, &type);
2315 _dbus_assert (type == DBUS_TYPE_DOUBLE);
2317 return _dbus_demarshal_double (&real->message->body, real->message->byte_order,
2322 * Initializes an iterator for the array that the iterator
2323 * may point to. Note that you need to check that the iterator
2324 * points to an array prior to using this function.
2326 * The array element type is returned in array_type, and the array
2327 * iterator can only be used to get that type of data.
2329 * @param iter the iterator
2330 * @param array_iter pointer to an iterator to initialize
2331 * @param array_type gets set to the type of the array elements
2332 * @returns #TRUE on success
2335 dbus_message_iter_init_array_iterator (DBusMessageIter *iter,
2336 DBusMessageIter *array_iter,
2339 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2340 DBusMessageRealIter *array_real = (DBusMessageRealIter *)array_iter;
2341 int type, pos, len_pos, len, array_type_pos;
2344 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2346 pos = dbus_message_iter_get_data_start (real, &type);
2348 _dbus_assert (type == DBUS_TYPE_ARRAY);
2350 _array_type = iter_get_array_type (real, &array_type_pos);
2352 len_pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
2353 len = _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
2356 array_real->parent_iter = real;
2357 array_real->message = real->message;
2358 array_real->changed_stamp = real->message->changed_stamp;
2360 array_real->type = DBUS_MESSAGE_ITER_TYPE_ARRAY;
2361 array_real->pos = pos;
2362 array_real->end = pos + len;
2364 array_real->container_start = pos;
2365 array_real->container_length_pos = len_pos;
2366 array_real->wrote_dict_key = 0;
2367 array_real->array_type_pos = array_type_pos;
2368 array_real->array_type_done = TRUE;
2370 if (array_type != NULL)
2371 *array_type = _array_type;
2378 * Initializes an iterator for the dict that the iterator
2379 * may point to. Note that you need to check that the iterator
2380 * points to a dict prior to using this function.
2382 * @param iter the iterator
2383 * @param dict_iter pointer to an iterator to initialize
2384 * @returns #TRUE on success
2387 dbus_message_iter_init_dict_iterator (DBusMessageIter *iter,
2388 DBusMessageIter *dict_iter)
2390 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2391 DBusMessageRealIter *dict_real = (DBusMessageRealIter *)dict_iter;
2392 int type, pos, len_pos, len;
2394 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2396 pos = dbus_message_iter_get_data_start (real, &type);
2398 _dbus_assert (type == DBUS_TYPE_DICT);
2400 len_pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
2401 len = _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
2404 dict_real->parent_iter = real;
2405 dict_real->message = real->message;
2406 dict_real->changed_stamp = real->message->changed_stamp;
2408 dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
2409 dict_real->pos = pos;
2410 dict_real->end = pos + len;
2412 dict_real->container_start = pos;
2413 dict_real->container_length_pos = len_pos;
2414 dict_real->wrote_dict_key = 0;
2420 * Returns the byte array that the iterator may point to.
2421 * Note that you need to check that the iterator points
2422 * to a byte array prior to using this function.
2424 * @param iter the iterator
2425 * @param value return location for array values
2426 * @param len return location for length of byte array
2427 * @returns #TRUE on success
2430 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
2431 unsigned char **value,
2434 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2437 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2439 pos = dbus_message_iter_get_data_start (real, &type);
2441 _dbus_assert (type == DBUS_TYPE_ARRAY);
2443 type = iter_get_array_type (real, NULL);
2445 _dbus_assert (type == DBUS_TYPE_BYTE);
2447 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
2448 pos, NULL, value, len))
2455 * Returns the boolean array that the iterator may point to. Note that
2456 * you need to check that the iterator points to an array of the
2457 * correct type prior to using this function.
2459 * @param iter the iterator
2460 * @param value return location for the array
2461 * @param len return location for the array length
2462 * @returns #TRUE on success
2465 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
2466 unsigned char **value,
2469 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2472 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2474 pos = dbus_message_iter_get_data_start (real, &type);
2476 _dbus_assert (type == DBUS_TYPE_ARRAY);
2478 type = iter_get_array_type (real, NULL);
2480 _dbus_assert (type == DBUS_TYPE_BOOLEAN);
2482 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
2483 pos, NULL, value, len))
2490 * Returns the 32 bit signed integer array that the iterator may point
2491 * to. Note that you need to check that the iterator points to an
2492 * array of the correct type prior to using this function.
2494 * @param iter the iterator
2495 * @param value return location for the array
2496 * @param len return location for the array length
2497 * @returns #TRUE on success
2500 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
2501 dbus_int32_t **value,
2504 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2507 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2509 pos = dbus_message_iter_get_data_start (real, &type);
2511 _dbus_assert (type == DBUS_TYPE_ARRAY);
2513 type = iter_get_array_type (real, NULL);
2515 _dbus_assert (type == DBUS_TYPE_INT32);
2517 if (!_dbus_demarshal_int32_array (&real->message->body, real->message->byte_order,
2518 pos, NULL, value, len))
2525 * Returns the 32 bit unsigned integer array that the iterator may point
2526 * to. Note that you need to check that the iterator points to an
2527 * array of the correct type prior to using this function.
2529 * @param iter the iterator
2530 * @param value return location for the array
2531 * @param len return location for the array length
2532 * @returns #TRUE on success
2535 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
2536 dbus_uint32_t **value,
2539 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2542 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2544 pos = dbus_message_iter_get_data_start (real, &type);
2546 _dbus_assert (type == DBUS_TYPE_ARRAY);
2548 type = iter_get_array_type (real, NULL);
2549 _dbus_assert (type == DBUS_TYPE_UINT32);
2551 if (!_dbus_demarshal_uint32_array (&real->message->body, real->message->byte_order,
2552 pos, NULL, value, len))
2558 #ifdef DBUS_HAVE_INT64
2561 * Returns the 64 bit signed integer array that the iterator may point
2562 * to. Note that you need to check that the iterator points to an
2563 * array of the correct type prior to using this function.
2565 * This function only exists if #DBUS_HAVE_INT64 is defined.
2567 * @param iter the iterator
2568 * @param value return location for the array
2569 * @param len return location for the array length
2570 * @returns #TRUE on success
2573 dbus_message_iter_get_int64_array (DBusMessageIter *iter,
2574 dbus_int64_t **value,
2577 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2580 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2582 pos = dbus_message_iter_get_data_start (real, &type);
2584 _dbus_assert (type == DBUS_TYPE_ARRAY);
2586 type = iter_get_array_type (real, NULL);
2588 _dbus_assert (type == DBUS_TYPE_INT64);
2590 if (!_dbus_demarshal_int64_array (&real->message->body, real->message->byte_order,
2591 pos, NULL, value, len))
2598 * Returns the 64 bit unsigned integer array that the iterator may point
2599 * to. Note that you need to check that the iterator points to an
2600 * array of the correct type prior to using this function.
2602 * This function only exists if #DBUS_HAVE_INT64 is defined.
2604 * @param iter the iterator
2605 * @param value return location for the array
2606 * @param len return location for the array length
2607 * @returns #TRUE on success
2610 dbus_message_iter_get_uint64_array (DBusMessageIter *iter,
2611 dbus_uint64_t **value,
2614 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2617 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2619 pos = dbus_message_iter_get_data_start (real, &type);
2621 _dbus_assert (type == DBUS_TYPE_ARRAY);
2623 type = iter_get_array_type (real, NULL);
2624 _dbus_assert (type == DBUS_TYPE_UINT64);
2626 if (!_dbus_demarshal_uint64_array (&real->message->body, real->message->byte_order,
2627 pos, NULL, value, len))
2633 #endif /* DBUS_HAVE_INT64 */
2636 * Returns the double array that the iterator may point to. Note that
2637 * you need to check that the iterator points to an array of the
2638 * correct type prior to using this function.
2640 * @param iter the iterator
2641 * @param value return location for the array
2642 * @param len return location for the array length
2643 * @returns #TRUE on success
2646 dbus_message_iter_get_double_array (DBusMessageIter *iter,
2650 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2653 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2655 pos = dbus_message_iter_get_data_start (real, &type);
2657 _dbus_assert (type == DBUS_TYPE_ARRAY);
2659 type = iter_get_array_type (real, NULL);
2660 _dbus_assert (type == DBUS_TYPE_DOUBLE);
2662 if (!_dbus_demarshal_double_array (&real->message->body, real->message->byte_order,
2663 pos, NULL, value, len))
2670 * Returns the string array that the iterator may point to.
2671 * Note that you need to check that the iterator points
2672 * to a byte array prior to using this function.
2674 * The returned value is a #NULL-terminated array of strings.
2675 * Each string is a separate malloc block, and the array
2676 * itself is a malloc block. You can free this type of
2677 * string array with dbus_free_string_array().
2679 * @param iter the iterator
2680 * @param value return location for string values
2681 * @param len return location for length of byte array
2682 * @returns #TRUE on success
2685 dbus_message_iter_get_string_array (DBusMessageIter *iter,
2689 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2692 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2694 pos = dbus_message_iter_get_data_start (real, &type);
2696 _dbus_assert (type == DBUS_TYPE_ARRAY);
2698 type = iter_get_array_type (real, NULL);
2699 _dbus_assert (type == DBUS_TYPE_STRING);
2701 if (!_dbus_demarshal_string_array (&real->message->body, real->message->byte_order,
2702 pos, NULL, value, len))
2709 * Returns the key name fot the dict entry that an iterator
2710 * may point to. Note that you need to check that the iterator
2711 * points to a dict entry before using this function.
2713 * @see dbus_message_iter_init_dict_iterator
2714 * @param iter the message iter
2715 * @returns the key name
2718 dbus_message_iter_get_dict_key (DBusMessageIter *iter)
2720 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2722 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
2724 _dbus_assert (real->type == DBUS_MESSAGE_ITER_TYPE_DICT);
2726 return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2731 * Initializes a DBusMessageIter pointing to the end of the
2732 * message. This iterator can be used to append data to the
2735 * @param message the message
2736 * @param iter pointer to an iterator to initialize
2739 dbus_message_append_iter_init (DBusMessage *message,
2740 DBusMessageIter *iter)
2742 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2744 _dbus_return_if_fail (message != NULL);
2745 _dbus_return_if_fail (iter != NULL);
2747 real->message = message;
2748 real->parent_iter = NULL;
2749 real->changed_stamp = message->changed_stamp;
2751 real->type = DBUS_MESSAGE_ITER_TYPE_MESSAGE;
2752 real->end = _dbus_string_get_length (&real->message->body);
2753 real->pos = real->end;
2755 real->container_length_pos = 0;
2756 real->wrote_dict_key = 0;
2759 #ifndef DBUS_DISABLE_CHECKS
2761 dbus_message_iter_append_check (DBusMessageRealIter *iter)
2765 _dbus_warn ("dbus iterator check failed: NULL iterator\n");
2769 if (iter->message->locked)
2771 _dbus_warn ("dbus iterator check failed: message is locked (has already been sent)\n");
2775 if (iter->changed_stamp != iter->message->changed_stamp)
2777 _dbus_warn ("dbus iterator check failed: invalid iterator, must re-initialize it after modifying the message");
2781 if (iter->pos != iter->end)
2783 _dbus_warn ("dbus iterator check failed: can only append at end of message");
2787 if (iter->pos != _dbus_string_get_length (&iter->message->body))
2789 _dbus_warn ("dbus iterator check failed: append pos not at end of message string");
2795 #endif /* DBUS_DISABLE_CHECKS */
2798 dbus_message_iter_append_type (DBusMessageRealIter *iter,
2804 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
2805 if (!_dbus_string_append_byte (&iter->message->body, type))
2809 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
2810 data = _dbus_string_get_const_data_len (&iter->message->body,
2811 iter->array_type_pos, 1);
2814 _dbus_warn ("Appended element of wrong type for array\n");
2819 case DBUS_MESSAGE_ITER_TYPE_DICT:
2820 if (!iter->wrote_dict_key)
2822 _dbus_warn ("Appending dict data before key name\n");
2826 if (!_dbus_string_append_byte (&iter->message->body, type))
2832 _dbus_assert_not_reached ("Invalid iter type");
2840 dbus_message_iter_update_after_change (DBusMessageRealIter *iter)
2842 iter->changed_stamp = iter->message->changed_stamp;
2844 /* Set new end of iter */
2845 iter->end = _dbus_string_get_length (&iter->message->body);
2846 iter->pos = iter->end;
2848 /* Set container length */
2849 if (iter->type == DBUS_MESSAGE_ITER_TYPE_DICT ||
2850 (iter->type == DBUS_MESSAGE_ITER_TYPE_ARRAY && iter->array_type_done))
2851 _dbus_marshal_set_uint32 (&iter->message->body,
2852 iter->message->byte_order,
2853 iter->container_length_pos,
2854 iter->end - iter->container_start);
2856 if (iter->parent_iter)
2857 dbus_message_iter_update_after_change (iter->parent_iter);
2861 dbus_message_iter_append_done (DBusMessageRealIter *iter)
2863 iter->message->changed_stamp++;
2864 dbus_message_iter_update_after_change (iter);
2865 iter->wrote_dict_key = FALSE;
2869 * Appends a nil value to the message
2871 * @param iter an iterator pointing to the end of the message
2872 * @returns #TRUE on success
2875 dbus_message_iter_append_nil (DBusMessageIter *iter)
2877 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2879 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
2881 if (!dbus_message_iter_append_type (real, DBUS_TYPE_NIL))
2884 dbus_message_iter_append_done (real);
2890 * Appends a boolean value to the message
2892 * @param iter an iterator pointing to the end of the message
2893 * @param value the boolean value
2894 * @returns #TRUE on success
2897 dbus_message_iter_append_boolean (DBusMessageIter *iter,
2900 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2902 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
2904 if (!dbus_message_iter_append_type (real, DBUS_TYPE_BOOLEAN))
2907 if (!_dbus_string_append_byte (&real->message->body, (value != FALSE)))
2909 _dbus_string_set_length (&real->message->body, real->pos);
2913 dbus_message_iter_append_done (real);
2919 * Appends a byte to the message
2921 * @param iter an iterator pointing to the end of the message
2922 * @param value the byte value
2923 * @returns #TRUE on success
2926 dbus_message_iter_append_byte (DBusMessageIter *iter,
2927 unsigned char value)
2929 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2931 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
2933 if (!dbus_message_iter_append_type (real, DBUS_TYPE_BYTE))
2936 if (!_dbus_string_append_byte (&real->message->body, value))
2938 _dbus_string_set_length (&real->message->body, real->pos);
2942 dbus_message_iter_append_done (real);
2949 * Appends a 32 bit signed integer to the message.
2951 * @param iter an iterator pointing to the end of the message
2952 * @param value the integer value
2953 * @returns #TRUE on success
2956 dbus_message_iter_append_int32 (DBusMessageIter *iter,
2959 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2961 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
2963 if (!dbus_message_iter_append_type (real, DBUS_TYPE_INT32))
2966 if (!_dbus_marshal_int32 (&real->message->body, real->message->byte_order, value))
2968 _dbus_string_set_length (&real->message->body, real->pos);
2972 dbus_message_iter_append_done (real);
2978 * Appends a 32 bit unsigned integer to the message.
2980 * @param iter an iterator pointing to the end of the message
2981 * @param value the integer value
2982 * @returns #TRUE on success
2985 dbus_message_iter_append_uint32 (DBusMessageIter *iter,
2986 dbus_uint32_t value)
2988 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2990 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
2992 if (!dbus_message_iter_append_type (real, DBUS_TYPE_UINT32))
2995 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, value))
2997 _dbus_string_set_length (&real->message->body, real->pos);
3001 dbus_message_iter_append_done (real);
3006 #ifdef DBUS_HAVE_INT64
3009 * Appends a 64 bit signed integer to the message.
3011 * This function only exists if #DBUS_HAVE_INT64 is defined.
3013 * @param iter an iterator pointing to the end of the message
3014 * @param value the integer value
3015 * @returns #TRUE on success
3018 dbus_message_iter_append_int64 (DBusMessageIter *iter,
3021 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3023 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3025 if (!dbus_message_iter_append_type (real, DBUS_TYPE_INT64))
3028 if (!_dbus_marshal_int64 (&real->message->body, real->message->byte_order, value))
3030 _dbus_string_set_length (&real->message->body, real->pos);
3034 dbus_message_iter_append_done (real);
3040 * Appends a 64 bit unsigned integer to the message.
3042 * This function only exists if #DBUS_HAVE_INT64 is defined.
3044 * @param iter an iterator pointing to the end of the message
3045 * @param value the integer value
3046 * @returns #TRUE on success
3049 dbus_message_iter_append_uint64 (DBusMessageIter *iter,
3050 dbus_uint64_t value)
3052 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3054 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3056 if (!dbus_message_iter_append_type (real, DBUS_TYPE_UINT64))
3059 if (!_dbus_marshal_uint64 (&real->message->body, real->message->byte_order, value))
3061 _dbus_string_set_length (&real->message->body, real->pos);
3065 dbus_message_iter_append_done (real);
3070 #endif /* DBUS_HAVE_INT64 */
3073 * Appends a double value to the message.
3075 * @param iter an iterator pointing to the end of the message
3076 * @param value the double value
3077 * @returns #TRUE on success
3080 dbus_message_iter_append_double (DBusMessageIter *iter,
3083 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3085 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3087 if (!dbus_message_iter_append_type (real, DBUS_TYPE_DOUBLE))
3090 if (!_dbus_marshal_double (&real->message->body, real->message->byte_order, value))
3092 _dbus_string_set_length (&real->message->body, real->pos);
3096 dbus_message_iter_append_done (real);
3102 * Appends a UTF-8 string to the message.
3104 * @param iter an iterator pointing to the end of the message
3105 * @param value the string
3106 * @returns #TRUE on success
3109 dbus_message_iter_append_string (DBusMessageIter *iter,
3112 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3114 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3116 if (!dbus_message_iter_append_type (real, DBUS_TYPE_STRING))
3119 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
3121 _dbus_string_set_length (&real->message->body, real->pos);
3125 dbus_message_iter_append_done (real);
3131 * Appends a named type data chunk to the message. A named
3132 * type is simply an arbitrary UTF-8 string used as a type
3133 * tag, plus an array of arbitrary bytes to be interpreted
3134 * according to the type tag.
3136 * @param iter an iterator pointing to the end of the message
3137 * @param name the name of the type
3138 * @param data the binary data used to store the value
3139 * @param len the length of the binary data in bytes
3140 * @returns #TRUE on success
3143 dbus_message_iter_append_named (DBusMessageIter *iter,
3145 const unsigned char *data,
3148 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3150 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3152 if (!dbus_message_iter_append_type (real, DBUS_TYPE_NAMED))
3155 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, name))
3157 _dbus_string_set_length (&real->message->body, real->pos);
3161 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, data, len))
3163 _dbus_string_set_length (&real->message->body, real->pos);
3167 dbus_message_iter_append_done (real);
3174 * Appends a dict key name to the message. The iterator used
3175 * must point to a dict.
3177 * @param iter an iterator pointing to the end of the message
3178 * @param value the string
3179 * @returns #TRUE on success
3182 dbus_message_iter_append_dict_key (DBusMessageIter *iter,
3185 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3187 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3188 _dbus_assert (real->type == DBUS_MESSAGE_ITER_TYPE_DICT);
3190 if (real->wrote_dict_key)
3192 _dbus_warn ("Appendinging multiple dict key names\n");
3196 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
3201 dbus_message_iter_append_done (real);
3202 real->wrote_dict_key = TRUE;
3208 array_iter_type_mark_done (DBusMessageRealIter *iter)
3212 if (iter->type == DBUS_MESSAGE_ITER_TYPE_ARRAY)
3213 array_iter_type_mark_done (iter->parent_iter);
3217 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&iter->message->body),
3218 sizeof (dbus_uint32_t));
3220 /* Empty length for now, backfill later */
3221 if (!_dbus_marshal_uint32 (&iter->message->body, iter->message->byte_order, 0))
3223 _dbus_string_set_length (&iter->message->body, iter->pos);
3227 iter->container_start = _dbus_string_get_length (&iter->message->body);
3228 iter->container_length_pos = len_pos;
3229 iter->array_type_done = TRUE;
3235 append_array_type (DBusMessageRealIter *real,
3237 dbus_bool_t *array_type_done,
3238 int *array_type_pos)
3240 int existing_element_type;
3242 if (!dbus_message_iter_append_type (real, DBUS_TYPE_ARRAY))
3245 if (real->type == DBUS_MESSAGE_ITER_TYPE_ARRAY &&
3246 real->array_type_done)
3248 existing_element_type = iter_get_array_type (real, array_type_pos);
3249 if (existing_element_type != element_type)
3251 _dbus_warn ("Appending array of %s, when expecting array of %s\n",
3252 _dbus_type_to_string (element_type),
3253 _dbus_type_to_string (existing_element_type));
3254 _dbus_string_set_length (&real->message->body, real->pos);
3257 if (array_type_done != NULL)
3258 *array_type_done = TRUE;
3262 if (array_type_pos != NULL)
3263 *array_type_pos = _dbus_string_get_length (&real->message->body);
3265 /* Append element type */
3266 if (!_dbus_string_append_byte (&real->message->body, element_type))
3268 _dbus_string_set_length (&real->message->body, real->pos);
3272 if (array_type_done != NULL)
3273 *array_type_done = element_type != DBUS_TYPE_ARRAY;
3275 if (element_type != DBUS_TYPE_ARRAY &&
3276 !array_iter_type_mark_done (real))
3284 * Appends an array to the message and initializes an iterator that
3285 * can be used to append to the array.
3287 * @param iter an iterator pointing to the end of the message
3288 * @param array_iter pointer to an iter that will be initialized
3289 * @param element_type the type of the array elements
3290 * @returns #TRUE on success
3293 dbus_message_iter_append_array (DBusMessageIter *iter,
3294 DBusMessageIter *array_iter,
3297 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3298 DBusMessageRealIter *array_real = (DBusMessageRealIter *)array_iter;
3301 dbus_bool_t array_type_done;
3303 if (element_type == DBUS_TYPE_NIL)
3305 _dbus_warn ("Can't create NIL arrays\n");
3309 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3311 if (!append_array_type (real, element_type, &array_type_done, &array_type_pos))
3314 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
3316 if (array_type_done)
3318 /* Empty length for now, backfill later */
3319 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
3321 _dbus_string_set_length (&real->message->body, real->pos);
3326 array_real->parent_iter = real;
3327 array_real->message = real->message;
3328 array_real->changed_stamp = real->message->changed_stamp;
3330 array_real->type = DBUS_MESSAGE_ITER_TYPE_ARRAY;
3331 array_real->pos = _dbus_string_get_length (&real->message->body);
3332 array_real->end = array_real->end;
3334 array_real->container_start = array_real->pos;
3335 array_real->container_length_pos = len_pos;
3336 array_real->wrote_dict_key = 0;
3337 array_real->array_type_done = array_type_done;
3338 array_real->array_type_pos = array_type_pos;
3340 dbus_message_iter_append_done (array_real);
3346 * Appends a dict to the message and initializes an iterator that
3347 * can be used to append to the dict.
3349 * @param iter an iterator pointing to the end of the message
3350 * @param dict_iter pointer to an iter that will be initialized
3351 * @returns #TRUE on success
3354 dbus_message_iter_append_dict (DBusMessageIter *iter,
3355 DBusMessageIter *dict_iter)
3357 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3358 DBusMessageRealIter *dict_real = (DBusMessageRealIter *)dict_iter;
3361 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3363 if (!dbus_message_iter_append_type (real, DBUS_TYPE_DICT))
3366 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
3368 /* Empty length for now, backfill later */
3369 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
3371 _dbus_string_set_length (&real->message->body, real->pos);
3375 dict_real->parent_iter = real;
3376 dict_real->message = real->message;
3377 dict_real->changed_stamp = real->message->changed_stamp;
3379 dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
3380 dict_real->pos = _dbus_string_get_length (&real->message->body);
3381 dict_real->end = dict_real->end;
3383 dict_real->container_start = dict_real->pos;
3384 dict_real->container_length_pos = len_pos;
3385 dict_real->wrote_dict_key = 0;
3387 dbus_message_iter_append_done (dict_real);
3394 * Appends a boolean array to the message.
3396 * @param iter an iterator pointing to the end of the message
3397 * @param value the array
3398 * @param len the length of the array
3399 * @returns #TRUE on success
3402 dbus_message_iter_append_boolean_array (DBusMessageIter *iter,
3403 unsigned const char *value,
3406 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3408 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3410 if (!append_array_type (real, DBUS_TYPE_BOOLEAN, NULL, NULL))
3413 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, value, len))
3415 _dbus_string_set_length (&real->message->body, real->pos);
3419 dbus_message_iter_append_done (real);
3425 * Appends a 32 bit signed integer array to the message.
3427 * @param iter an iterator pointing to the end of the message
3428 * @param value the array
3429 * @param len the length of the array
3430 * @returns #TRUE on success
3433 dbus_message_iter_append_int32_array (DBusMessageIter *iter,
3434 const dbus_int32_t *value,
3437 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3439 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3441 if (!append_array_type (real, DBUS_TYPE_INT32, NULL, NULL))
3444 if (!_dbus_marshal_int32_array (&real->message->body, real->message->byte_order, value, len))
3446 _dbus_string_set_length (&real->message->body, real->pos);
3450 dbus_message_iter_append_done (real);
3456 * Appends a 32 bit unsigned integer array to the message.
3458 * @param iter an iterator pointing to the end of the message
3459 * @param value the array
3460 * @param len the length of the array
3461 * @returns #TRUE on success
3464 dbus_message_iter_append_uint32_array (DBusMessageIter *iter,
3465 const dbus_uint32_t *value,
3468 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3470 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3472 if (!append_array_type (real, DBUS_TYPE_UINT32, NULL, NULL))
3475 if (!_dbus_marshal_uint32_array (&real->message->body, real->message->byte_order, value, len))
3477 _dbus_string_set_length (&real->message->body, real->pos);
3481 dbus_message_iter_append_done (real);
3486 #ifdef DBUS_HAVE_INT64
3489 * Appends a 64 bit signed integer array to the message.
3491 * This function only exists if #DBUS_HAVE_INT64 is defined.
3493 * @param iter an iterator pointing to the end of the message
3494 * @param value the array
3495 * @param len the length of the array
3496 * @returns #TRUE on success
3499 dbus_message_iter_append_int64_array (DBusMessageIter *iter,
3500 const dbus_int64_t *value,
3503 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3505 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3507 if (!append_array_type (real, DBUS_TYPE_INT64, NULL, NULL))
3510 if (!_dbus_marshal_int64_array (&real->message->body, real->message->byte_order, value, len))
3512 _dbus_string_set_length (&real->message->body, real->pos);
3516 dbus_message_iter_append_done (real);
3522 * Appends a 64 bit unsigned integer array to the message.
3524 * This function only exists if #DBUS_HAVE_INT64 is defined.
3526 * @param iter an iterator pointing to the end of the message
3527 * @param value the array
3528 * @param len the length of the array
3529 * @returns #TRUE on success
3532 dbus_message_iter_append_uint64_array (DBusMessageIter *iter,
3533 const dbus_uint64_t *value,
3536 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3538 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3540 if (!append_array_type (real, DBUS_TYPE_UINT64, NULL, NULL))
3543 if (!_dbus_marshal_uint64_array (&real->message->body, real->message->byte_order, value, len))
3545 _dbus_string_set_length (&real->message->body, real->pos);
3549 dbus_message_iter_append_done (real);
3553 #endif /* DBUS_HAVE_INT64 */
3556 * Appends a double array to the message.
3558 * @param iter an iterator pointing to the end of the message
3559 * @param value the array
3560 * @param len the length of the array
3561 * @returns #TRUE on success
3564 dbus_message_iter_append_double_array (DBusMessageIter *iter,
3565 const double *value,
3568 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3570 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3572 if (!append_array_type (real, DBUS_TYPE_DOUBLE, NULL, NULL))
3575 if (!_dbus_marshal_double_array (&real->message->body, real->message->byte_order, value, len))
3577 _dbus_string_set_length (&real->message->body, real->pos);
3581 dbus_message_iter_append_done (real);
3587 * Appends a byte array to the message.
3589 * @param iter an iterator pointing to the end of the message
3590 * @param value the array
3591 * @param len the length of the array
3592 * @returns #TRUE on success
3595 dbus_message_iter_append_byte_array (DBusMessageIter *iter,
3596 unsigned const char *value,
3599 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3601 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3603 if (!append_array_type (real, DBUS_TYPE_BYTE, NULL, NULL))
3606 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, value, len))
3608 _dbus_string_set_length (&real->message->body, real->pos);
3612 dbus_message_iter_append_done (real);
3618 * Appends a string array to the message.
3620 * @param iter an iterator pointing to the end of the message
3621 * @param value the array
3622 * @param len the length of the array
3623 * @returns #TRUE on success
3626 dbus_message_iter_append_string_array (DBusMessageIter *iter,
3630 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3632 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3634 if (!append_array_type (real, DBUS_TYPE_STRING, NULL, NULL))
3637 if (!_dbus_marshal_string_array (&real->message->body, real->message->byte_order, value, len))
3639 _dbus_string_set_length (&real->message->body, real->pos);
3643 dbus_message_iter_append_done (real);
3649 * Sets the message sender.
3651 * @param message the message
3652 * @param sender the sender
3653 * @returns #FALSE if not enough memory
3656 dbus_message_set_sender (DBusMessage *message,
3659 _dbus_return_val_if_fail (message != NULL, FALSE);
3660 _dbus_return_val_if_fail (!message->locked, FALSE);
3664 delete_string_field (message, FIELD_SENDER);
3669 return set_string_field (message,
3676 * Sets a flag indicating that the message is an error reply
3677 * message, i.e. an "exception" rather than a normal response.
3679 * @param message the message
3680 * @param is_error_reply #TRUE if this is an error message.
3683 dbus_message_set_is_error (DBusMessage *message,
3684 dbus_bool_t is_error_reply)
3688 _dbus_return_if_fail (message != NULL);
3689 _dbus_return_if_fail (!message->locked);
3691 header = _dbus_string_get_data_len (&message->header, 1, 1);
3694 *header |= DBUS_HEADER_FLAG_ERROR;
3696 *header &= ~DBUS_HEADER_FLAG_ERROR;
3700 * Returns #TRUE if the message is an error
3701 * reply to some previous message we sent.
3703 * @param message the message
3704 * @returns #TRUE if the message is an error
3707 dbus_message_get_is_error (DBusMessage *message)
3711 _dbus_return_val_if_fail (message != NULL, FALSE);
3713 header = _dbus_string_get_const_data_len (&message->header, 1, 1);
3715 return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
3719 * Gets the service which originated this message,
3720 * or #NULL if unknown or inapplicable.
3722 * @param message the message
3723 * @returns the service name or #NULL
3726 dbus_message_get_sender (DBusMessage *message)
3728 _dbus_return_val_if_fail (message != NULL, NULL);
3730 return get_string_field (message, FIELD_SENDER, NULL);
3734 * Checks whether the message has the given name.
3735 * If the message has no name or has a different
3736 * name, returns #FALSE.
3738 * @param message the message
3739 * @param name the name to check (must not be #NULL)
3741 * @returns #TRUE if the message has the given name
3744 dbus_message_has_name (DBusMessage *message,
3749 _dbus_return_val_if_fail (message != NULL, FALSE);
3750 _dbus_return_val_if_fail (name != NULL, FALSE);
3752 n = dbus_message_get_name (message);
3754 if (n && strcmp (n, name) == 0)
3761 * Checks whether the message was sent to the given service. If the
3762 * message has no service specified or has a different name, returns
3765 * @param message the message
3766 * @param service the service to check (must not be #NULL)
3768 * @returns #TRUE if the message has the given destination service
3771 dbus_message_has_destination (DBusMessage *message,
3772 const char *service)
3776 _dbus_return_val_if_fail (message != NULL, FALSE);
3777 _dbus_return_val_if_fail (service != NULL, FALSE);
3779 s = dbus_message_get_destination (message);
3781 if (s && strcmp (s, service) == 0)
3788 * Checks whether the message has the given service as its sender. If
3789 * the message has no sender specified or has a different sender,
3790 * returns #FALSE. Note that if a peer application owns multiple
3791 * services, its messages will have only one of those services as the
3792 * sender (usually the base service). So you can't use this
3793 * function to prove the sender didn't own service Foo, you can
3794 * only use it to prove that it did.
3796 * @param message the message
3797 * @param service the service to check (must not be #NULL)
3799 * @returns #TRUE if the message has the given origin service
3802 dbus_message_has_sender (DBusMessage *message,
3803 const char *service)
3807 _dbus_assert (service != NULL);
3809 s = dbus_message_get_sender (message);
3811 if (s && strcmp (s, service) == 0)
3818 * Sets a #DBusError based on the contents of the given
3819 * message. The error is only set if the message
3820 * is an error message, as in dbus_message_get_is_error().
3821 * The name of the error is set to the name of the message,
3822 * and the error message is set to the first argument
3823 * if the argument exists and is a string.
3825 * The return value indicates whether the error was set (the error is
3826 * set if and only if the message is an error message).
3827 * So you can check for an error reply and convert it to DBusError
3830 * @param error the error to set
3831 * @param message the message to set it from
3832 * @returns #TRUE if dbus_message_get_is_error() returns #TRUE for the message
3835 dbus_set_error_from_message (DBusError *error,
3836 DBusMessage *message)
3840 _dbus_return_val_if_fail (message != NULL, FALSE);
3841 _dbus_return_val_if_error_is_set (error, FALSE);
3843 if (!dbus_message_get_is_error (message))
3847 dbus_message_get_args (message, NULL,
3848 DBUS_TYPE_STRING, &str,
3851 dbus_set_error (error, dbus_message_get_name (message),
3852 str ? "%s" : NULL, str);
3862 * @addtogroup DBusMessageInternals
3867 * @typedef DBusMessageLoader
3869 * The DBusMessageLoader object encapsulates the process of converting
3870 * a byte stream into a series of DBusMessage. It buffers the incoming
3871 * bytes as efficiently as possible, and generates a queue of
3872 * messages. DBusMessageLoader is typically used as part of a
3873 * DBusTransport implementation. The DBusTransport then hands off
3874 * the loaded messages to a DBusConnection, making the messages
3875 * visible to the application.
3877 * @todo write tests for break-loader that a) randomly delete header
3878 * fields and b) set string fields to zero-length and other funky
3883 /* we definitely use signed ints for sizes, so don't exceed
3884 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
3885 * over 128M is pretty nuts anyhow.
3889 * The maximum sane message size.
3891 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
3894 * Implementation details of DBusMessageLoader.
3895 * All members are private.
3897 struct DBusMessageLoader
3899 int refcount; /**< Reference count. */
3901 DBusString data; /**< Buffered data */
3903 DBusList *messages; /**< Complete messages. */
3905 long max_message_size; /**< Maximum size of a message */
3907 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
3909 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
3913 * The initial buffer size of the message loader.
3915 * @todo this should be based on min header size plus some average
3916 * body size, or something. Or rather, the min header size only, if we
3917 * want to try to read only the header, store that in a DBusMessage,
3918 * then read only the body and store that, etc., depends on
3919 * how we optimize _dbus_message_loader_get_buffer() and what
3920 * the exact message format is.
3922 #define INITIAL_LOADER_DATA_LEN 32
3925 * Creates a new message loader. Returns #NULL if memory can't
3928 * @returns new loader, or #NULL.
3931 _dbus_message_loader_new (void)
3933 DBusMessageLoader *loader;
3935 loader = dbus_new0 (DBusMessageLoader, 1);
3939 loader->refcount = 1;
3941 /* Try to cap message size at something that won't *totally* hose
3942 * the system if we have a couple of them.
3944 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
3946 if (!_dbus_string_init (&loader->data))
3952 /* preallocate the buffer for speed, ignore failure */
3953 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
3954 _dbus_string_set_length (&loader->data, 0);
3960 * Increments the reference count of the loader.
3962 * @param loader the loader.
3965 _dbus_message_loader_ref (DBusMessageLoader *loader)
3967 loader->refcount += 1;
3971 * Decrements the reference count of the loader and finalizes the
3972 * loader when the count reaches zero.
3974 * @param loader the loader.
3977 _dbus_message_loader_unref (DBusMessageLoader *loader)
3979 loader->refcount -= 1;
3980 if (loader->refcount == 0)
3982 _dbus_list_foreach (&loader->messages,
3983 (DBusForeachFunction) dbus_message_unref,
3985 _dbus_list_clear (&loader->messages);
3986 _dbus_string_free (&loader->data);
3992 * Gets the buffer to use for reading data from the network. Network
3993 * data is read directly into an allocated buffer, which is then used
3994 * in the DBusMessage, to avoid as many extra memcpy's as possible.
3995 * The buffer must always be returned immediately using
3996 * _dbus_message_loader_return_buffer(), even if no bytes are
3997 * successfully read.
3999 * @todo this function can be a lot more clever. For example
4000 * it can probably always return a buffer size to read exactly
4001 * the body of the next message, thus avoiding any memory wastage
4004 * @todo we need to enforce a max length on strings in header fields.
4006 * @param loader the message loader.
4007 * @param buffer the buffer
4010 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
4011 DBusString **buffer)
4013 _dbus_assert (!loader->buffer_outstanding);
4015 *buffer = &loader->data;
4017 loader->buffer_outstanding = TRUE;
4021 * The smallest header size that can occur.
4022 * (It won't be valid)
4024 #define DBUS_MINIMUM_HEADER_SIZE 16
4026 /** Pack four characters as in "abcd" into a uint32 */
4027 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
4028 ((((dbus_uint32_t)a) << 24) | \
4029 (((dbus_uint32_t)b) << 16) | \
4030 (((dbus_uint32_t)c) << 8) | \
4033 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
4034 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
4035 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
4037 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
4038 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
4039 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
4041 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
4042 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
4043 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
4045 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
4046 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
4047 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
4050 decode_string_field (const DBusString *data,
4051 HeaderField fields[FIELD_LAST],
4055 const char *field_name)
4058 int string_data_pos;
4060 if (fields[field].offset >= 0)
4062 _dbus_verbose ("%s field provided twice\n",
4067 if (type != DBUS_TYPE_STRING)
4069 _dbus_verbose ("%s field has wrong type %s\n",
4070 field_name, _dbus_type_to_string (type));
4074 /* skip padding after typecode, skip string length;
4075 * we assume that the string arg has already been validated
4076 * for sanity and UTF-8
4078 string_data_pos = _DBUS_ALIGN_VALUE (pos, 4) + 4;
4079 _dbus_assert (string_data_pos < _dbus_string_get_length (data));
4081 _dbus_string_init_const (&tmp,
4082 _dbus_string_get_const_data (data) + string_data_pos);
4084 if (field == FIELD_NAME)
4086 if (!_dbus_string_validate_name (&tmp, 0, _dbus_string_get_length (&tmp)))
4088 _dbus_verbose ("%s field has invalid content \"%s\"\n",
4089 field_name, _dbus_string_get_const_data (&tmp));
4093 if (_dbus_string_starts_with_c_str (&tmp,
4094 DBUS_NAMESPACE_LOCAL_MESSAGE))
4096 _dbus_verbose ("Message is in the local namespace\n");
4102 if (!_dbus_string_validate_service (&tmp, 0, _dbus_string_get_length (&tmp)))
4104 _dbus_verbose ("%s field has invalid content \"%s\"\n",
4105 field_name, _dbus_string_get_const_data (&tmp));
4110 fields[field].offset = _DBUS_ALIGN_VALUE (pos, 4);
4113 _dbus_verbose ("Found field %s name at offset %d\n",
4114 field_name, fields[field].offset);
4121 decode_header_data (const DBusString *data,
4124 HeaderField fields[FIELD_LAST],
4125 int *message_padding)
4132 if (header_len < 16)
4136 while (i < FIELD_LAST)
4138 fields[i].offset = -1;
4142 fields[FIELD_HEADER_LENGTH].offset = 4;
4143 fields[FIELD_BODY_LENGTH].offset = 8;
4144 fields[FIELD_CLIENT_SERIAL].offset = 12;
4146 /* Now handle the named fields. A real named field is at least 4
4147 * bytes for the name, plus a type code (1 byte) plus padding. So
4148 * if we have less than 8 bytes left, it must be alignment padding,
4149 * not a field. While >= 8 bytes can't be entirely alignment
4153 while ((pos + 7) < header_len)
4155 pos = _DBUS_ALIGN_VALUE (pos, 4);
4157 if ((pos + 4) > header_len)
4160 field =_dbus_string_get_const_data_len (data, pos, 4);
4163 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
4165 if (!_dbus_marshal_validate_type (data, pos, &type, &pos))
4167 _dbus_verbose ("Failed to validate type of named header field\n");
4171 if (!_dbus_marshal_validate_arg (data, byte_order, 0, type, -1, pos, &new_pos))
4173 _dbus_verbose ("Failed to validate argument to named header field\n");
4177 if (new_pos > header_len)
4179 _dbus_verbose ("Named header field tries to extend beyond header length\n");
4183 switch (DBUS_UINT32_FROM_BE (*(int*)field))
4185 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
4186 if (!decode_string_field (data, fields, pos, type,
4188 DBUS_HEADER_FIELD_SERVICE))
4192 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
4193 if (!decode_string_field (data, fields, pos, type,
4195 DBUS_HEADER_FIELD_NAME))
4199 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
4200 if (!decode_string_field (data, fields, pos, type,
4202 DBUS_HEADER_FIELD_SENDER))
4206 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
4207 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
4209 _dbus_verbose ("%s field provided twice\n",
4210 DBUS_HEADER_FIELD_REPLY);
4214 if (type != DBUS_TYPE_UINT32)
4216 _dbus_verbose ("%s field has wrong type\n", DBUS_HEADER_FIELD_REPLY);
4220 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos, 4);
4222 _dbus_verbose ("Found reply serial at offset %d\n",
4223 fields[FIELD_REPLY_SERIAL].offset);
4227 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c at offset %d\n",
4228 field[0], field[1], field[2], field[3], pos);
4234 if (pos < header_len)
4236 /* Alignment padding, verify that it's nul */
4237 _dbus_assert ((header_len - pos) < 8);
4239 if (!_dbus_string_validate_nul (data,
4240 pos, (header_len - pos)))
4242 _dbus_verbose ("header alignment padding is not nul\n");
4247 /* Name field is mandatory */
4248 if (fields[FIELD_NAME].offset < 0)
4250 _dbus_verbose ("No %s field provided\n",
4251 DBUS_HEADER_FIELD_NAME);
4255 if (message_padding)
4256 *message_padding = header_len - pos;
4262 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
4263 * indicating to the loader how many bytes of the buffer were filled
4264 * in. This function must always be called, even if no bytes were
4265 * successfully read.
4267 * @param loader the loader.
4268 * @param buffer the buffer.
4269 * @param bytes_read number of bytes that were read into the buffer.
4272 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
4276 _dbus_assert (loader->buffer_outstanding);
4277 _dbus_assert (buffer == &loader->data);
4279 loader->buffer_outstanding = FALSE;
4283 * Converts buffered data into messages.
4285 * @param loader the loader.
4286 * @returns #TRUE if we had enough memory to finish.
4289 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
4291 if (loader->corrupted)
4294 while (_dbus_string_get_length (&loader->data) >= 16)
4296 DBusMessage *message;
4297 const char *header_data;
4298 int byte_order, header_len, body_len, header_padding;
4299 dbus_uint32_t header_len_unsigned, body_len_unsigned;
4301 header_data = _dbus_string_get_const_data_len (&loader->data, 0, 16);
4303 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
4305 if (header_data[2] != DBUS_MAJOR_PROTOCOL_VERSION)
4307 _dbus_verbose ("Message has protocol version %d ours is %d\n",
4308 (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
4309 loader->corrupted = TRUE;
4313 byte_order = header_data[0];
4315 if (byte_order != DBUS_LITTLE_ENDIAN &&
4316 byte_order != DBUS_BIG_ENDIAN)
4318 _dbus_verbose ("Message with bad byte order '%c' received\n",
4320 loader->corrupted = TRUE;
4324 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
4325 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
4327 if (header_len_unsigned < 16)
4329 _dbus_verbose ("Message had broken too-small header length %u\n",
4330 header_len_unsigned);
4331 loader->corrupted = TRUE;
4335 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
4336 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
4338 _dbus_verbose ("Header or body length too large (%u %u)\n",
4339 header_len_unsigned,
4341 loader->corrupted = TRUE;
4345 /* Now that we know the values are in signed range, get
4346 * rid of stupid unsigned, just causes bugs
4348 header_len = header_len_unsigned;
4349 body_len = body_len_unsigned;
4351 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
4354 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
4356 loader->corrupted = TRUE;
4360 if (header_len + body_len > loader->max_message_size)
4362 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %ld\n",
4363 header_len, body_len, loader->max_message_size);
4364 loader->corrupted = TRUE;
4368 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
4370 HeaderField fields[FIELD_LAST];
4375 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len + body_len);
4377 if (!decode_header_data (&loader->data, header_len, byte_order,
4378 fields, &header_padding))
4380 _dbus_verbose ("Header was invalid\n");
4381 loader->corrupted = TRUE;
4385 next_arg = header_len;
4386 while (next_arg < (header_len + body_len))
4389 int prev = next_arg;
4391 if (!_dbus_marshal_validate_type (&loader->data, next_arg,
4394 _dbus_verbose ("invalid typecode at offset %d\n", prev);
4395 loader->corrupted = TRUE;
4399 if (!_dbus_marshal_validate_arg (&loader->data,
4406 _dbus_verbose ("invalid type data at %d, next_arg\n", next_arg);
4407 loader->corrupted = TRUE;
4411 _dbus_assert (next_arg > prev);
4414 if (next_arg > (header_len + body_len))
4416 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
4417 next_arg, header_len, body_len,
4418 header_len + body_len);
4419 loader->corrupted = TRUE;
4423 message = dbus_message_new_empty_header ();
4424 if (message == NULL)
4426 _dbus_verbose ("Failed to allocate empty message\n");
4430 message->byte_order = byte_order;
4431 message->header_padding = header_padding;
4433 /* Copy in the offsets we found */
4435 while (i < FIELD_LAST)
4437 message->header_fields[i] = fields[i];
4441 if (!_dbus_list_append (&loader->messages, message))
4443 _dbus_verbose ("Failed to append new message to loader queue\n");
4444 dbus_message_unref (message);
4448 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
4449 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
4451 _dbus_assert (_dbus_string_get_length (&loader->data) >=
4452 (header_len + body_len));
4454 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
4456 _dbus_verbose ("Failed to move header into new message\n");
4457 _dbus_list_remove_last (&loader->messages, message);
4458 dbus_message_unref (message);
4462 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
4466 _dbus_verbose ("Failed to move body into new message\n");
4468 /* put the header back, we'll try again later */
4469 result = _dbus_string_copy_len (&message->header, 0, header_len,
4471 _dbus_assert (result); /* because DBusString never reallocs smaller */
4473 _dbus_list_remove_last (&loader->messages, message);
4474 dbus_message_unref (message);
4478 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
4479 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
4481 /* Fill in caches (we checked the types of these fields
4484 message->reply_serial = get_uint_field (message,
4485 FIELD_REPLY_SERIAL);
4486 message->client_serial = get_uint_field (message,
4487 FIELD_CLIENT_SERIAL);
4489 _dbus_verbose ("Loaded message %p\n", message);
4499 * Peeks at first loaded message, returns #NULL if no messages have
4502 * @param loader the loader.
4503 * @returns the next message, or #NULL if none.
4506 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
4508 if (loader->messages)
4509 return loader->messages->data;
4515 * Pops a loaded message (passing ownership of the message
4516 * to the caller). Returns #NULL if no messages have been
4519 * @param loader the loader.
4520 * @returns the next message, or #NULL if none.
4523 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
4525 return _dbus_list_pop_first (&loader->messages);
4529 * Pops a loaded message inside a list link (passing ownership of the
4530 * message and link to the caller). Returns #NULL if no messages have
4533 * @param loader the loader.
4534 * @returns the next message link, or #NULL if none.
4537 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
4539 return _dbus_list_pop_first_link (&loader->messages);
4543 * Returns a popped message link, used to undo a pop.
4545 * @param loader the loader
4546 * @param link the link with a message in it
4549 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
4552 _dbus_list_prepend_link (&loader->messages, link);
4556 * Checks whether the loader is confused due to bad data.
4557 * If messages are received that are invalid, the
4558 * loader gets confused and gives up permanently.
4559 * This state is called "corrupted."
4561 * @param loader the loader
4562 * @returns #TRUE if the loader is hosed.
4565 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
4567 return loader->corrupted;
4571 * Sets the maximum size message we allow.
4573 * @param loader the loader
4574 * @param size the max message size in bytes
4577 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
4580 if (size > MAX_SANE_MESSAGE_SIZE)
4582 _dbus_verbose ("clamping requested max message size %ld to %d\n",
4583 size, MAX_SANE_MESSAGE_SIZE);
4584 size = MAX_SANE_MESSAGE_SIZE;
4586 loader->max_message_size = size;
4590 * Gets the maximum allowed message size in bytes.
4592 * @param loader the loader
4593 * @returns max size in bytes
4596 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
4598 return loader->max_message_size;
4602 #ifdef DBUS_BUILD_TESTS
4603 #include "dbus-test.h"
4607 message_iter_test (DBusMessage *message)
4609 DBusMessageIter iter, dict, array, array2;
4611 unsigned char *data;
4612 dbus_int32_t *our_int_array;
4615 dbus_message_iter_init (message, &iter);
4618 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
4619 _dbus_assert_not_reached ("Argument type isn't string");
4621 str = dbus_message_iter_get_string (&iter);
4622 if (strcmp (str, "Test string") != 0)
4623 _dbus_assert_not_reached ("Strings differ");
4626 if (!dbus_message_iter_next (&iter))
4627 _dbus_assert_not_reached ("Reached end of arguments");
4629 /* Signed integer tests */
4630 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32)
4631 _dbus_assert_not_reached ("Argument type isn't int32");
4633 if (dbus_message_iter_get_int32 (&iter) != -0x12345678)
4634 _dbus_assert_not_reached ("Signed integers differ");
4636 if (!dbus_message_iter_next (&iter))
4637 _dbus_assert_not_reached ("Reached end of fields");
4639 /* Unsigned integer tests */
4640 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
4641 _dbus_assert_not_reached ("Argument type isn't int32");
4643 if (dbus_message_iter_get_uint32 (&iter) != 0xedd1e)
4644 _dbus_assert_not_reached ("Unsigned integers differ");
4646 if (!dbus_message_iter_next (&iter))
4647 _dbus_assert_not_reached ("Reached end of arguments");
4650 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DOUBLE)
4651 _dbus_assert_not_reached ("Argument type isn't double");
4653 if (dbus_message_iter_get_double (&iter) != 3.14159)
4654 _dbus_assert_not_reached ("Doubles differ");
4656 if (!dbus_message_iter_next (&iter))
4657 _dbus_assert_not_reached ("Reached end of arguments");
4659 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
4660 _dbus_assert_not_reached ("Argument type not an array");
4662 if (dbus_message_iter_get_array_type (&iter) != DBUS_TYPE_DOUBLE)
4663 _dbus_assert_not_reached ("Array type not double");
4666 if (!dbus_message_iter_init_array_iterator (&iter, &array, NULL))
4667 _dbus_assert_not_reached ("Array init failed");
4669 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_DOUBLE)
4670 _dbus_assert_not_reached ("Argument type isn't double");
4672 if (dbus_message_iter_get_double (&array) != 1.5)
4673 _dbus_assert_not_reached ("Unsigned integers differ");
4675 if (!dbus_message_iter_next (&array))
4676 _dbus_assert_not_reached ("Reached end of arguments");
4678 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_DOUBLE)
4679 _dbus_assert_not_reached ("Argument type isn't double");
4681 if (dbus_message_iter_get_double (&array) != 2.5)
4682 _dbus_assert_not_reached ("Unsigned integers differ");
4684 if (dbus_message_iter_next (&array))
4685 _dbus_assert_not_reached ("Didn't reach end of arguments");
4687 if (!dbus_message_iter_next (&iter))
4688 _dbus_assert_not_reached ("Reached end of arguments");
4693 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
4694 _dbus_assert_not_reached ("not dict type");
4696 if (!dbus_message_iter_init_dict_iterator (&iter, &dict))
4697 _dbus_assert_not_reached ("dict iter failed");
4699 str = dbus_message_iter_get_dict_key (&dict);
4700 if (str == NULL || strcmp (str, "test") != 0)
4701 _dbus_assert_not_reached ("wrong dict key");
4704 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_UINT32)
4705 _dbus_assert_not_reached ("wrong dict entry type");
4707 if (dbus_message_iter_get_uint32 (&dict) != 0xDEADBEEF)
4708 _dbus_assert_not_reached ("wrong dict entry value");
4710 if (!dbus_message_iter_next (&dict))
4711 _dbus_assert_not_reached ("reached end of dict");
4713 /* array of array of int32 (in dict) */
4715 str = dbus_message_iter_get_dict_key (&dict);
4716 if (str == NULL || strcmp (str, "array") != 0)
4717 _dbus_assert_not_reached ("wrong dict key");
4720 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_ARRAY)
4721 _dbus_assert_not_reached ("Argument type not an array");
4723 if (dbus_message_iter_get_array_type (&dict) != DBUS_TYPE_ARRAY)
4724 _dbus_assert_not_reached ("Array type not array");
4726 if (!dbus_message_iter_init_array_iterator (&dict, &array, NULL))
4727 _dbus_assert_not_reached ("Array init failed");
4729 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_ARRAY)
4730 _dbus_assert_not_reached ("Argument type isn't array");
4732 if (dbus_message_iter_get_array_type (&array) != DBUS_TYPE_INT32)
4733 _dbus_assert_not_reached ("Array type not int32");
4735 if (!dbus_message_iter_init_array_iterator (&array, &array2, NULL))
4736 _dbus_assert_not_reached ("Array init failed");
4738 if (dbus_message_iter_get_arg_type (&array2) != DBUS_TYPE_INT32)
4739 _dbus_assert_not_reached ("Argument type isn't int32");
4741 if (dbus_message_iter_get_int32 (&array2) != 0x12345678)
4742 _dbus_assert_not_reached ("Signed integers differ");
4744 if (!dbus_message_iter_next (&array2))
4745 _dbus_assert_not_reached ("Reached end of arguments");
4747 if (dbus_message_iter_get_int32 (&array2) != 0x23456781)
4748 _dbus_assert_not_reached ("Signed integers differ");
4750 if (dbus_message_iter_next (&array2))
4751 _dbus_assert_not_reached ("Didn't reached end of arguments");
4753 if (!dbus_message_iter_next (&array))
4754 _dbus_assert_not_reached ("Reached end of arguments");
4756 if (dbus_message_iter_get_array_type (&array) != DBUS_TYPE_INT32)
4757 _dbus_assert_not_reached ("Array type not int32");
4759 if (!dbus_message_iter_get_int32_array (&array,
4762 _dbus_assert_not_reached ("couldn't get int32 array");
4764 _dbus_assert (len == 3);
4765 _dbus_assert (our_int_array[0] == 0x34567812 &&
4766 our_int_array[1] == 0x45678123 &&
4767 our_int_array[2] == 0x56781234);
4768 dbus_free (our_int_array);
4770 if (dbus_message_iter_next (&array))
4771 _dbus_assert_not_reached ("Didn't reach end of array");
4773 if (dbus_message_iter_next (&dict))
4774 _dbus_assert_not_reached ("Didn't reach end of dict");
4776 if (!dbus_message_iter_next (&iter))
4777 _dbus_assert_not_reached ("Reached end of arguments");
4779 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_BYTE)
4781 _dbus_warn ("type was: %d\n", dbus_message_iter_get_arg_type (&iter));
4782 _dbus_assert_not_reached ("wrong type after dict (should be byte)");
4785 if (dbus_message_iter_get_byte (&iter) != 0xF0)
4786 _dbus_assert_not_reached ("wrong value after dict");
4789 if (!dbus_message_iter_next (&iter))
4790 _dbus_assert_not_reached ("Reached end of arguments");
4792 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_NIL)
4793 _dbus_assert_not_reached ("not a nil type");
4795 if (!dbus_message_iter_next (&iter))
4796 _dbus_assert_not_reached ("Reached end of arguments");
4798 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_NAMED)
4799 _dbus_assert_not_reached ("wrong type after dict");
4801 if (!dbus_message_iter_get_named (&iter, &str, &data, &len))
4802 _dbus_assert_not_reached ("failed to get named");
4804 _dbus_assert (strcmp (str, "named")==0);
4805 _dbus_assert (len == 5);
4806 _dbus_assert (strcmp (data, "data")==0);
4810 if (dbus_message_iter_next (&iter))
4811 _dbus_assert_not_reached ("Didn't reach end of arguments");
4816 check_message_handling_type (DBusMessageIter *iter,
4819 DBusMessageIter child_iter;
4825 case DBUS_TYPE_BYTE:
4826 dbus_message_iter_get_byte (iter);
4828 case DBUS_TYPE_BOOLEAN:
4829 dbus_message_iter_get_boolean (iter);
4831 case DBUS_TYPE_INT32:
4832 dbus_message_iter_get_int32 (iter);
4834 case DBUS_TYPE_UINT32:
4835 dbus_message_iter_get_uint32 (iter);
4837 case DBUS_TYPE_INT64:
4838 #ifdef DBUS_HAVE_INT64
4839 dbus_message_iter_get_int64 (iter);
4842 case DBUS_TYPE_UINT64:
4843 #ifdef DBUS_HAVE_INT64
4844 dbus_message_iter_get_uint64 (iter);
4847 case DBUS_TYPE_DOUBLE:
4848 dbus_message_iter_get_double (iter);
4850 case DBUS_TYPE_STRING:
4853 str = dbus_message_iter_get_string (iter);
4856 _dbus_warn ("NULL string in message\n");
4862 case DBUS_TYPE_NAMED:
4865 unsigned char *data;
4868 if (!dbus_message_iter_get_named (iter, &name, &data, &len))
4870 _dbus_warn ("error reading name from named type\n");
4877 case DBUS_TYPE_ARRAY:
4881 if (!dbus_message_iter_init_array_iterator (iter, &child_iter, &array_type))
4883 _dbus_warn ("Failed to init array iterator\n");
4887 while (dbus_message_iter_has_next (&child_iter))
4889 if (!check_message_handling_type (&child_iter, array_type))
4891 _dbus_warn ("error in array element\n");
4895 if (!dbus_message_iter_next (&child_iter))
4900 case DBUS_TYPE_DICT:
4905 if (!dbus_message_iter_init_dict_iterator (iter, &child_iter))
4907 _dbus_warn ("Failed to init dict iterator\n");
4911 while ((entry_type = dbus_message_iter_get_arg_type (&child_iter)) != DBUS_TYPE_INVALID)
4913 key = dbus_message_iter_get_dict_key (&child_iter);
4916 _dbus_warn ("error reading dict key\n");
4921 if (!check_message_handling_type (&child_iter, entry_type))
4923 _dbus_warn ("error in dict value\n");
4927 if (!dbus_message_iter_next (&child_iter))
4934 _dbus_warn ("unknown type %d\n", type);
4943 check_message_handling (DBusMessage *message)
4945 DBusMessageIter iter;
4948 dbus_uint32_t client_serial;
4952 client_serial = dbus_message_get_serial (message);
4954 /* can't use set_serial due to the assertions at the start of it */
4955 set_uint_field (message, FIELD_CLIENT_SERIAL,
4958 if (client_serial != dbus_message_get_serial (message))
4960 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
4964 /* If we implement message_set_arg (message, n, value)
4965 * then we would want to test it here
4968 dbus_message_iter_init (message, &iter);
4969 while ((type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
4971 if (!check_message_handling_type (&iter, type))
4974 if (!dbus_message_iter_next (&iter))
4985 check_have_valid_message (DBusMessageLoader *loader)
4987 DBusMessage *message;
4993 if (!_dbus_message_loader_queue_messages (loader))
4994 _dbus_assert_not_reached ("no memory to queue messages");
4996 if (_dbus_message_loader_get_is_corrupted (loader))
4998 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
5002 message = _dbus_message_loader_pop_message (loader);
5003 if (message == NULL)
5005 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
5009 if (_dbus_string_get_length (&loader->data) > 0)
5011 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
5015 /* Verify that we're able to properly deal with the message.
5016 * For example, this would detect improper handling of messages
5017 * in nonstandard byte order.
5019 if (!check_message_handling (message))
5026 dbus_message_unref (message);
5032 check_invalid_message (DBusMessageLoader *loader)
5038 if (!_dbus_message_loader_queue_messages (loader))
5039 _dbus_assert_not_reached ("no memory to queue messages");
5041 if (!_dbus_message_loader_get_is_corrupted (loader))
5043 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
5054 check_incomplete_message (DBusMessageLoader *loader)
5056 DBusMessage *message;
5062 if (!_dbus_message_loader_queue_messages (loader))
5063 _dbus_assert_not_reached ("no memory to queue messages");
5065 if (_dbus_message_loader_get_is_corrupted (loader))
5067 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
5071 message = _dbus_message_loader_pop_message (loader);
5072 if (message != NULL)
5074 _dbus_warn ("loaded message that was expected to be incomplete\n");
5082 dbus_message_unref (message);
5087 check_loader_results (DBusMessageLoader *loader,
5088 DBusMessageValidity validity)
5090 if (!_dbus_message_loader_queue_messages (loader))
5091 _dbus_assert_not_reached ("no memory to queue messages");
5095 case _DBUS_MESSAGE_VALID:
5096 return check_have_valid_message (loader);
5097 case _DBUS_MESSAGE_INVALID:
5098 return check_invalid_message (loader);
5099 case _DBUS_MESSAGE_INCOMPLETE:
5100 return check_incomplete_message (loader);
5101 case _DBUS_MESSAGE_UNKNOWN:
5105 _dbus_assert_not_reached ("bad DBusMessageValidity");
5111 * Loads the message in the given message file.
5113 * @param filename filename to load
5114 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
5115 * @param data string to load message into
5116 * @returns #TRUE if the message was loaded
5119 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
5131 _dbus_verbose ("Loading raw %s\n", _dbus_string_get_const_data (filename));
5132 dbus_error_init (&error);
5133 if (!_dbus_file_get_contents (data, filename, &error))
5135 _dbus_warn ("Could not load message file %s: %s\n",
5136 _dbus_string_get_const_data (filename),
5138 dbus_error_free (&error);
5144 if (!_dbus_message_data_load (data, filename))
5146 _dbus_warn ("Could not load message file %s\n",
5147 _dbus_string_get_const_data (filename));
5160 * Tries loading the message in the given message file
5161 * and verifies that DBusMessageLoader can handle it.
5163 * @param filename filename to load
5164 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
5165 * @param expected_validity what the message has to be like to return #TRUE
5166 * @returns #TRUE if the message has the expected validity
5169 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
5171 DBusMessageValidity expected_validity)
5178 if (!_dbus_string_init (&data))
5179 _dbus_assert_not_reached ("could not allocate string\n");
5181 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
5185 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
5191 if (_dbus_string_get_length (&data) > 0)
5192 _dbus_verbose_bytes_of_string (&data, 0,
5193 _dbus_string_get_length (&data));
5195 _dbus_warn ("Failed message loader test on %s\n",
5196 _dbus_string_get_const_data (filename));
5199 _dbus_string_free (&data);
5205 * Tries loading the given message data.
5208 * @param data the message data
5209 * @param expected_validity what the message has to be like to return #TRUE
5210 * @returns #TRUE if the message has the expected validity
5213 dbus_internal_do_not_use_try_message_data (const DBusString *data,
5214 DBusMessageValidity expected_validity)
5216 DBusMessageLoader *loader;
5224 /* Write the data one byte at a time */
5226 loader = _dbus_message_loader_new ();
5228 /* check some trivial loader functions */
5229 _dbus_message_loader_ref (loader);
5230 _dbus_message_loader_unref (loader);
5231 _dbus_message_loader_get_max_message_size (loader);
5233 len = _dbus_string_get_length (data);
5234 for (i = 0; i < len; i++)
5238 _dbus_message_loader_get_buffer (loader, &buffer);
5239 _dbus_string_append_byte (buffer,
5240 _dbus_string_get_byte (data, i));
5241 _dbus_message_loader_return_buffer (loader, buffer, 1);
5244 if (!check_loader_results (loader, expected_validity))
5247 _dbus_message_loader_unref (loader);
5250 /* Write the data all at once */
5252 loader = _dbus_message_loader_new ();
5257 _dbus_message_loader_get_buffer (loader, &buffer);
5258 _dbus_string_copy (data, 0, buffer,
5259 _dbus_string_get_length (buffer));
5260 _dbus_message_loader_return_buffer (loader, buffer, 1);
5263 if (!check_loader_results (loader, expected_validity))
5266 _dbus_message_loader_unref (loader);
5269 /* Write the data 2 bytes at a time */
5271 loader = _dbus_message_loader_new ();
5273 len = _dbus_string_get_length (data);
5274 for (i = 0; i < len; i += 2)
5278 _dbus_message_loader_get_buffer (loader, &buffer);
5279 _dbus_string_append_byte (buffer,
5280 _dbus_string_get_byte (data, i));
5282 _dbus_string_append_byte (buffer,
5283 _dbus_string_get_byte (data, i+1));
5284 _dbus_message_loader_return_buffer (loader, buffer, 1);
5287 if (!check_loader_results (loader, expected_validity))
5290 _dbus_message_loader_unref (loader);
5298 _dbus_message_loader_unref (loader);
5304 process_test_subdir (const DBusString *test_base_dir,
5306 DBusMessageValidity validity,
5307 DBusForeachMessageFileFunc function,
5310 DBusString test_directory;
5311 DBusString filename;
5319 if (!_dbus_string_init (&test_directory))
5320 _dbus_assert_not_reached ("didn't allocate test_directory\n");
5322 _dbus_string_init_const (&filename, subdir);
5324 if (!_dbus_string_copy (test_base_dir, 0,
5325 &test_directory, 0))
5326 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
5328 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
5329 _dbus_assert_not_reached ("couldn't allocate full path");
5331 _dbus_string_free (&filename);
5332 if (!_dbus_string_init (&filename))
5333 _dbus_assert_not_reached ("didn't allocate filename string\n");
5335 dbus_error_init (&error);
5336 dir = _dbus_directory_open (&test_directory, &error);
5339 _dbus_warn ("Could not open %s: %s\n",
5340 _dbus_string_get_const_data (&test_directory),
5342 dbus_error_free (&error);
5346 printf ("Testing:\n");
5349 while (_dbus_directory_get_next_file (dir, &filename, &error))
5351 DBusString full_path;
5354 if (!_dbus_string_init (&full_path))
5355 _dbus_assert_not_reached ("couldn't init string");
5357 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
5358 _dbus_assert_not_reached ("couldn't copy dir to full_path");
5360 if (!_dbus_concat_dir_and_file (&full_path, &filename))
5361 _dbus_assert_not_reached ("couldn't concat file to dir");
5363 if (_dbus_string_ends_with_c_str (&filename, ".message"))
5365 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
5369 _dbus_verbose ("Skipping non-.message file %s\n",
5370 _dbus_string_get_const_data (&filename));
5371 _dbus_string_free (&full_path);
5376 _dbus_string_get_const_data (&filename));
5378 _dbus_verbose (" expecting %s\n",
5379 validity == _DBUS_MESSAGE_VALID ? "valid" :
5380 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
5381 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
5383 if (! (*function) (&full_path, is_raw, validity, user_data))
5385 _dbus_string_free (&full_path);
5389 _dbus_string_free (&full_path);
5392 if (dbus_error_is_set (&error))
5394 _dbus_warn ("Could not get next file in %s: %s\n",
5395 _dbus_string_get_const_data (&test_directory),
5397 dbus_error_free (&error);
5406 _dbus_directory_close (dir);
5407 _dbus_string_free (&test_directory);
5408 _dbus_string_free (&filename);
5414 * Runs the given function on every message file in the test suite.
5415 * The function should return #FALSE on test failure or fatal error.
5417 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
5418 * @param func the function to run
5419 * @param user_data data for function
5420 * @returns #FALSE if there's a failure
5423 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
5424 DBusForeachMessageFileFunc func,
5427 DBusString test_directory;
5432 _dbus_string_init_const (&test_directory, test_data_dir);
5434 if (!process_test_subdir (&test_directory, "valid-messages",
5435 _DBUS_MESSAGE_VALID, func, user_data))
5438 if (!process_test_subdir (&test_directory, "invalid-messages",
5439 _DBUS_MESSAGE_INVALID, func, user_data))
5442 if (!process_test_subdir (&test_directory, "incomplete-messages",
5443 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
5450 _dbus_string_free (&test_directory);
5456 verify_test_message (DBusMessage *message)
5458 DBusMessageIter iter, dict;
5460 dbus_int32_t our_int;
5463 dbus_bool_t our_bool;
5464 dbus_uint32_t our_uint32;
5465 dbus_int32_t *our_uint32_array;
5466 int our_uint32_array_len;
5467 dbus_int32_t *our_int32_array;
5468 int our_int32_array_len;
5469 char **our_string_array;
5470 int our_string_array_len;
5471 #ifdef DBUS_HAVE_INT64
5472 dbus_int64_t our_int64;
5473 dbus_uint64_t our_uint64;
5474 dbus_int64_t *our_uint64_array;
5475 int our_uint64_array_len;
5476 dbus_int64_t *our_int64_array;
5477 int our_int64_array_len;
5479 double *our_double_array;
5480 int our_double_array_len;
5481 unsigned char *our_byte_array;
5482 int our_byte_array_len;
5483 unsigned char *our_boolean_array;
5484 int our_boolean_array_len;
5486 dbus_message_iter_init (message, &iter);
5488 dbus_error_init (&error);
5489 if (!dbus_message_iter_get_args (&iter, &error,
5490 DBUS_TYPE_INT32, &our_int,
5491 #ifdef DBUS_HAVE_INT64
5492 DBUS_TYPE_INT64, &our_int64,
5493 DBUS_TYPE_UINT64, &our_uint64,
5495 DBUS_TYPE_STRING, &our_str,
5496 DBUS_TYPE_DOUBLE, &our_double,
5497 DBUS_TYPE_BOOLEAN, &our_bool,
5498 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32,
5499 &our_uint32_array, &our_uint32_array_len,
5500 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32,
5501 &our_int32_array, &our_int32_array_len,
5502 #ifdef DBUS_HAVE_INT64
5503 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64,
5504 &our_uint64_array, &our_uint64_array_len,
5505 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64,
5506 &our_int64_array, &our_int64_array_len,
5508 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
5509 &our_string_array, &our_string_array_len,
5510 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE,
5511 &our_double_array, &our_double_array_len,
5512 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
5513 &our_byte_array, &our_byte_array_len,
5514 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN,
5515 &our_boolean_array, &our_boolean_array_len,
5518 _dbus_warn ("error: %s - %s\n", error.name,
5519 (error.message != NULL) ? error.message : "no message");
5520 _dbus_assert_not_reached ("Could not get arguments");
5523 if (our_int != -0x12345678)
5524 _dbus_assert_not_reached ("integers differ!");
5526 #ifdef DBUS_HAVE_INT64
5527 if (our_int64 != -0x123456789abcd)
5528 _dbus_assert_not_reached ("64-bit integers differ!");
5529 if (our_uint64 != 0x123456789abcd)
5530 _dbus_assert_not_reached ("64-bit unsigned integers differ!");
5533 if (our_double != 3.14159)
5534 _dbus_assert_not_reached ("doubles differ!");
5536 if (strcmp (our_str, "Test string") != 0)
5537 _dbus_assert_not_reached ("strings differ!");
5538 dbus_free (our_str);
5541 _dbus_assert_not_reached ("booleans differ");
5543 if (our_uint32_array_len != 4 ||
5544 our_uint32_array[0] != 0x12345678 ||
5545 our_uint32_array[1] != 0x23456781 ||
5546 our_uint32_array[2] != 0x34567812 ||
5547 our_uint32_array[3] != 0x45678123)
5548 _dbus_assert_not_reached ("uint array differs");
5549 dbus_free (our_uint32_array);
5551 if (our_int32_array_len != 4 ||
5552 our_int32_array[0] != 0x12345678 ||
5553 our_int32_array[1] != -0x23456781 ||
5554 our_int32_array[2] != 0x34567812 ||
5555 our_int32_array[3] != -0x45678123)
5556 _dbus_assert_not_reached ("int array differs");
5557 dbus_free (our_int32_array);
5559 #ifdef DBUS_HAVE_INT64
5560 if (our_uint64_array_len != 4 ||
5561 our_uint64_array[0] != 0x12345678 ||
5562 our_uint64_array[1] != 0x23456781 ||
5563 our_uint64_array[2] != 0x34567812 ||
5564 our_uint64_array[3] != 0x45678123)
5565 _dbus_assert_not_reached ("uint64 array differs");
5566 dbus_free (our_uint64_array);
5568 if (our_int64_array_len != 4 ||
5569 our_int64_array[0] != 0x12345678 ||
5570 our_int64_array[1] != -0x23456781 ||
5571 our_int64_array[2] != 0x34567812 ||
5572 our_int64_array[3] != -0x45678123)
5573 _dbus_assert_not_reached ("int64 array differs");
5574 dbus_free (our_int64_array);
5575 #endif /* DBUS_HAVE_INT64 */
5577 if (our_string_array_len != 4)
5578 _dbus_assert_not_reached ("string array has wrong length");
5580 if (strcmp (our_string_array[0], "Foo") != 0 ||
5581 strcmp (our_string_array[1], "bar") != 0 ||
5582 strcmp (our_string_array[2], "") != 0 ||
5583 strcmp (our_string_array[3], "woo woo woo woo") != 0)
5584 _dbus_assert_not_reached ("string array differs");
5586 dbus_free_string_array (our_string_array);
5588 if (our_double_array_len != 3)
5589 _dbus_assert_not_reached ("double array had wrong length");
5591 /* On all IEEE machines (i.e. everything sane) exact equality
5592 * should be preserved over the wire
5594 if (our_double_array[0] != 0.1234 ||
5595 our_double_array[1] != 9876.54321 ||
5596 our_double_array[2] != -300.0)
5597 _dbus_assert_not_reached ("double array had wrong values");
5599 dbus_free (our_double_array);
5601 if (our_byte_array_len != 4)
5602 _dbus_assert_not_reached ("byte array had wrong length");
5604 if (our_byte_array[0] != 'a' ||
5605 our_byte_array[1] != 'b' ||
5606 our_byte_array[2] != 'c' ||
5607 our_byte_array[3] != 234)
5608 _dbus_assert_not_reached ("byte array had wrong values");
5610 dbus_free (our_byte_array);
5612 if (our_boolean_array_len != 5)
5613 _dbus_assert_not_reached ("bool array had wrong length");
5615 if (our_boolean_array[0] != TRUE ||
5616 our_boolean_array[1] != FALSE ||
5617 our_boolean_array[2] != TRUE ||
5618 our_boolean_array[3] != TRUE ||
5619 our_boolean_array[4] != FALSE)
5620 _dbus_assert_not_reached ("bool array had wrong values");
5622 dbus_free (our_boolean_array);
5624 if (!dbus_message_iter_next (&iter))
5625 _dbus_assert_not_reached ("Reached end of arguments");
5627 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
5628 _dbus_assert_not_reached ("not dict type");
5630 if (!dbus_message_iter_init_dict_iterator (&iter, &dict))
5631 _dbus_assert_not_reached ("dict iter failed");
5633 our_str = dbus_message_iter_get_dict_key (&dict);
5634 if (our_str == NULL || strcmp (our_str, "test") != 0)
5635 _dbus_assert_not_reached ("wrong dict key");
5636 dbus_free (our_str);
5638 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_UINT32)
5640 _dbus_verbose ("dict entry type: %d\n", dbus_message_iter_get_arg_type (&dict));
5641 _dbus_assert_not_reached ("wrong dict entry type");
5644 if ((our_uint32 = dbus_message_iter_get_uint32 (&dict)) != 0xDEADBEEF)
5646 _dbus_verbose ("dict entry val: %x\n", our_uint32);
5647 _dbus_assert_not_reached ("wrong dict entry value");
5650 if (dbus_message_iter_next (&dict))
5651 _dbus_assert_not_reached ("Didn't reach end of dict");
5653 if (!dbus_message_iter_next (&iter))
5654 _dbus_assert_not_reached ("Reached end of arguments");
5656 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
5657 _dbus_assert_not_reached ("wrong type after dict");
5659 if (dbus_message_iter_get_uint32 (&iter) != 0xCAFEBABE)
5660 _dbus_assert_not_reached ("wrong value after dict");
5662 if (dbus_message_iter_next (&iter))
5663 _dbus_assert_not_reached ("Didn't reach end of arguments");
5667 * @ingroup DBusMessageInternals
5668 * Unit test for DBusMessage.
5670 * @returns #TRUE on success.
5673 _dbus_message_test (const char *test_data_dir)
5675 DBusMessage *message;
5676 DBusMessageLoader *loader;
5677 DBusMessageIter iter, child_iter, child_iter2, child_iter3;
5683 const dbus_uint32_t our_uint32_array[] =
5684 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
5685 const dbus_uint32_t our_int32_array[] =
5686 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
5687 #ifdef DBUS_HAVE_INT64
5688 const dbus_uint64_t our_uint64_array[] =
5689 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
5690 const dbus_uint64_t our_int64_array[] =
5691 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
5693 const char *our_string_array[] = { "Foo", "bar", "", "woo woo woo woo" };
5694 const double our_double_array[] = { 0.1234, 9876.54321, -300.0 };
5695 const unsigned char our_byte_array[] = { 'a', 'b', 'c', 234 };
5696 const unsigned char our_boolean_array[] = { TRUE, FALSE, TRUE, TRUE, FALSE };
5698 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
5700 message = dbus_message_new ("test.Message", "org.freedesktop.DBus.Test");
5701 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.Test"));
5702 _dbus_message_set_serial (message, 1234);
5703 dbus_message_set_sender (message, "org.foo.bar");
5704 _dbus_assert (dbus_message_has_sender (message, "org.foo.bar"));
5705 dbus_message_set_sender (message, NULL);
5706 _dbus_assert (!dbus_message_has_sender (message, "org.foo.bar"));
5707 _dbus_assert (dbus_message_get_serial (message) == 1234);
5708 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.Test"));
5710 _dbus_assert (dbus_message_get_is_error (message) == FALSE);
5711 dbus_message_set_is_error (message, TRUE);
5712 _dbus_assert (dbus_message_get_is_error (message) == TRUE);
5713 dbus_message_set_is_error (message, FALSE);
5714 _dbus_assert (dbus_message_get_is_error (message) == FALSE);
5716 dbus_message_unref (message);
5718 /* Test the vararg functions */
5719 message = dbus_message_new ("test.Message", "org.freedesktop.DBus.Test");
5720 _dbus_message_set_serial (message, 1);
5721 dbus_message_append_args (message,
5722 DBUS_TYPE_INT32, -0x12345678,
5723 #ifdef DBUS_HAVE_INT64
5724 DBUS_TYPE_INT64, -0x123456789abcd,
5725 DBUS_TYPE_UINT64, 0x123456789abcd,
5727 DBUS_TYPE_STRING, "Test string",
5728 DBUS_TYPE_DOUBLE, 3.14159,
5729 DBUS_TYPE_BOOLEAN, TRUE,
5730 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, our_uint32_array,
5731 _DBUS_N_ELEMENTS (our_uint32_array),
5732 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, our_int32_array,
5733 _DBUS_N_ELEMENTS (our_int32_array),
5734 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64, our_uint64_array,
5735 _DBUS_N_ELEMENTS (our_uint64_array),
5736 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64, our_int64_array,
5737 _DBUS_N_ELEMENTS (our_int64_array),
5738 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, our_string_array,
5739 _DBUS_N_ELEMENTS (our_string_array),
5740 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, our_double_array,
5741 _DBUS_N_ELEMENTS (our_double_array),
5742 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, our_byte_array,
5743 _DBUS_N_ELEMENTS (our_byte_array),
5744 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN, our_boolean_array,
5745 _DBUS_N_ELEMENTS (our_boolean_array),
5748 dbus_message_append_iter_init (message, &iter);
5749 dbus_message_iter_append_dict (&iter, &child_iter);
5750 dbus_message_iter_append_dict_key (&child_iter, "test");
5751 dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
5752 dbus_message_iter_append_uint32 (&iter, 0xCAFEBABE);
5754 _dbus_verbose_bytes_of_string (&message->header, 0,
5755 _dbus_string_get_length (&message->header));
5756 _dbus_verbose_bytes_of_string (&message->body, 0,
5757 _dbus_string_get_length (&message->body));
5759 verify_test_message (message);
5761 copy = dbus_message_copy (message);
5763 _dbus_assert (message->client_serial == copy->client_serial);
5764 _dbus_assert (message->reply_serial == copy->reply_serial);
5765 _dbus_assert (message->header_padding == copy->header_padding);
5767 _dbus_assert (_dbus_string_get_length (&message->header) ==
5768 _dbus_string_get_length (©->header));
5770 _dbus_assert (_dbus_string_get_length (&message->body) ==
5771 _dbus_string_get_length (©->body));
5773 verify_test_message (copy);
5775 name1 = dbus_message_get_name (message);
5776 name2 = dbus_message_get_name (copy);
5778 _dbus_assert (strcmp (name1, name2) == 0);
5780 dbus_message_unref (message);
5781 dbus_message_unref (copy);
5783 message = dbus_message_new ("test.Message", "org.freedesktop.DBus.Test");
5784 _dbus_message_set_serial (message, 1);
5785 dbus_message_set_reply_serial (message, 0x12345678);
5787 dbus_message_append_iter_init (message, &iter);
5788 dbus_message_iter_append_string (&iter, "Test string");
5789 dbus_message_iter_append_int32 (&iter, -0x12345678);
5790 dbus_message_iter_append_uint32 (&iter, 0xedd1e);
5791 dbus_message_iter_append_double (&iter, 3.14159);
5793 dbus_message_iter_append_array (&iter, &child_iter, DBUS_TYPE_DOUBLE);
5794 dbus_message_iter_append_double (&child_iter, 1.5);
5795 dbus_message_iter_append_double (&child_iter, 2.5);
5798 dbus_message_iter_append_dict (&iter, &child_iter);
5799 dbus_message_iter_append_dict_key (&child_iter, "test");
5800 dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
5802 /* array of array of int32 (in dict) */
5803 dbus_message_iter_append_dict_key (&child_iter, "array");
5804 dbus_message_iter_append_array (&child_iter, &child_iter2, DBUS_TYPE_ARRAY);
5805 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
5806 dbus_message_iter_append_int32 (&child_iter3, 0x12345678);
5807 dbus_message_iter_append_int32 (&child_iter3, 0x23456781);
5808 _dbus_warn ("next call expected to fail with wrong array type\n");
5809 _dbus_assert (!dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_UINT32));
5810 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
5811 dbus_message_iter_append_int32 (&child_iter3, 0x34567812);
5812 dbus_message_iter_append_int32 (&child_iter3, 0x45678123);
5813 dbus_message_iter_append_int32 (&child_iter3, 0x56781234);
5815 dbus_message_iter_append_byte (&iter, 0xF0);
5817 dbus_message_iter_append_nil (&iter);
5819 dbus_message_iter_append_named (&iter, "named",
5822 message_iter_test (message);
5824 /* Message loader test */
5825 _dbus_message_lock (message);
5826 loader = _dbus_message_loader_new ();
5828 /* check ref/unref */
5829 _dbus_message_loader_ref (loader);
5830 _dbus_message_loader_unref (loader);
5832 /* Write the header data one byte at a time */
5833 data = _dbus_string_get_const_data (&message->header);
5834 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
5838 _dbus_message_loader_get_buffer (loader, &buffer);
5839 _dbus_string_append_byte (buffer, data[i]);
5840 _dbus_message_loader_return_buffer (loader, buffer, 1);
5843 /* Write the body data one byte at a time */
5844 data = _dbus_string_get_const_data (&message->body);
5845 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
5849 _dbus_message_loader_get_buffer (loader, &buffer);
5850 _dbus_string_append_byte (buffer, data[i]);
5851 _dbus_message_loader_return_buffer (loader, buffer, 1);
5854 dbus_message_unref (message);
5856 /* Now pop back the message */
5857 if (!_dbus_message_loader_queue_messages (loader))
5858 _dbus_assert_not_reached ("no memory to queue messages");
5860 if (_dbus_message_loader_get_is_corrupted (loader))
5861 _dbus_assert_not_reached ("message loader corrupted");
5863 message = _dbus_message_loader_pop_message (loader);
5865 _dbus_assert_not_reached ("received a NULL message");
5867 if (dbus_message_get_reply_serial (message) != 0x12345678)
5868 _dbus_assert_not_reached ("reply serial fields differ");
5870 message_iter_test (message);
5872 dbus_message_unref (message);
5873 _dbus_message_loader_unref (loader);
5875 /* Now load every message in test_data_dir if we have one */
5876 if (test_data_dir == NULL)
5879 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
5880 (DBusForeachMessageFileFunc)
5881 dbus_internal_do_not_use_try_message_file,
5885 #endif /* DBUS_BUILD_TESTS */