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 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;
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 struct DBusMessageRealIter DBusMessageRealIter;
121 * @brief Internals of DBusMessageIter
123 * Object representing a position in a message. All fields are internal.
125 struct DBusMessageRealIter
127 DBusMessageRealIter *parent_iter; /**< parent iter, or NULL */
128 DBusMessage *message; /**< Message used */
129 dbus_uint32_t changed_stamp; /**< stamp to detect invalid iters */
131 /* This is an int instead of an enum to get a guaranteed size for the dummy: */
132 int type; /**< type of iter */
134 int pos; /**< Current position in the string */
135 int end; /**< position right after the container */
136 int container_start; /**< offset of the start of the container */
137 int container_length_pos; /**< offset of the length of the container */
139 int wrote_dict_key; /**< whether we wrote the dict key for the current dict element */
141 int array_type_pos; /**< pointer to the position of the array element type */
142 int array_type_done; /**< TRUE if the array type is fully finished */
146 * Gets the data to be sent over the network for this message.
147 * The header and then the body should be written out.
148 * This function is guaranteed to always return the same
149 * data once a message is locked (with _dbus_message_lock()).
151 * @param message the message.
152 * @param header return location for message header data.
153 * @param body return location for message body data.
156 _dbus_message_get_network_data (DBusMessage *message,
157 const DBusString **header,
158 const DBusString **body)
160 _dbus_assert (message->locked);
162 *header = &message->header;
163 *body = &message->body;
167 clear_header_padding (DBusMessage *message)
169 _dbus_string_shorten (&message->header,
170 message->header_padding);
171 message->header_padding = 0;
175 append_header_padding (DBusMessage *message)
178 old_len = _dbus_string_get_length (&message->header);
179 if (!_dbus_string_align_length (&message->header, 8))
182 message->header_padding = _dbus_string_get_length (&message->header) - old_len;
188 adjust_field_offsets (DBusMessage *message,
198 while (i < FIELD_LAST)
200 if (message->header_fields[i].offset > offsets_after)
201 message->header_fields[i].offset += delta;
207 #ifdef DBUS_BUILD_TESTS
208 /* tests-only until it's actually used */
210 get_int_field (DBusMessage *message,
215 _dbus_assert (field < FIELD_LAST);
217 offset = message->header_fields[field].offset;
220 return -1; /* useless if -1 is a valid value of course */
222 return _dbus_demarshal_int32 (&message->header,
230 get_uint_field (DBusMessage *message,
235 _dbus_assert (field < FIELD_LAST);
237 offset = message->header_fields[field].offset;
240 return -1; /* useless if -1 is a valid value of course */
242 return _dbus_demarshal_uint32 (&message->header,
249 get_string_field (DBusMessage *message,
256 offset = message->header_fields[field].offset;
258 _dbus_assert (field < FIELD_LAST);
263 /* offset points to string length, string data follows it */
264 /* FIXME _dbus_demarshal_const_string() that returned
265 * a reference to the string plus its len might be nice.
269 *len = _dbus_demarshal_uint32 (&message->header,
274 data = _dbus_string_get_const_data (&message->header);
276 return data + (offset + 4);
279 #ifdef DBUS_BUILD_TESTS
281 append_int_field (DBusMessage *message,
288 _dbus_assert (!message->locked);
290 clear_header_padding (message);
292 orig_len = _dbus_string_get_length (&message->header);
294 if (!_dbus_string_align_length (&message->header, 4))
297 if (!_dbus_string_append_len (&message->header, name, 4))
300 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_INT32))
303 if (!_dbus_string_align_length (&message->header, 4))
306 message->header_fields[FIELD_REPLY_SERIAL].offset =
307 _dbus_string_get_length (&message->header);
309 if (!_dbus_marshal_int32 (&message->header, message->byte_order,
313 if (!append_header_padding (message))
319 message->header_fields[field].offset = -1;
320 _dbus_string_set_length (&message->header, orig_len);
322 /* this must succeed because it was allocated on function entry and
323 * DBusString doesn't ever realloc smaller
325 if (!append_header_padding (message))
326 _dbus_assert_not_reached ("failed to reappend header padding");
332 append_uint_field (DBusMessage *message,
339 _dbus_assert (!message->locked);
341 clear_header_padding (message);
343 orig_len = _dbus_string_get_length (&message->header);
345 if (!_dbus_string_align_length (&message->header, 4))
348 if (!_dbus_string_append_len (&message->header, name, 4))
351 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_UINT32))
354 if (!_dbus_string_align_length (&message->header, 4))
357 message->header_fields[FIELD_REPLY_SERIAL].offset =
358 _dbus_string_get_length (&message->header);
360 if (!_dbus_marshal_uint32 (&message->header, message->byte_order,
364 if (!append_header_padding (message))
370 message->header_fields[field].offset = -1;
371 _dbus_string_set_length (&message->header, orig_len);
373 /* this must succeed because it was allocated on function entry and
374 * DBusString doesn't ever realloc smaller
376 if (!append_header_padding (message))
377 _dbus_assert_not_reached ("failed to reappend header padding");
382 append_string_field (DBusMessage *message,
389 _dbus_assert (!message->locked);
391 clear_header_padding (message);
393 orig_len = _dbus_string_get_length (&message->header);
395 if (!_dbus_string_align_length (&message->header, 4))
398 if (!_dbus_string_append_len (&message->header, name, 4))
401 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_STRING))
404 if (!_dbus_string_align_length (&message->header, 4))
407 message->header_fields[field].offset =
408 _dbus_string_get_length (&message->header);
410 if (!_dbus_marshal_string (&message->header, message->byte_order,
414 if (!append_header_padding (message))
420 message->header_fields[field].offset = -1;
421 _dbus_string_set_length (&message->header, orig_len);
423 /* this must succeed because it was allocated on function entry and
424 * DBusString doesn't ever realloc smaller
426 if (!append_header_padding (message))
427 _dbus_assert_not_reached ("failed to reappend header padding");
432 #ifdef DBUS_BUILD_TESTS
433 /* This isn't used, but building it when tests are enabled just to
434 * keep it compiling if we need it in future
437 delete_int_or_uint_field (DBusMessage *message,
440 int offset = message->header_fields[field].offset;
442 _dbus_assert (!message->locked);
443 _dbus_assert (field_is_named[field]);
448 clear_header_padding (message);
450 /* The field typecode and name take up 8 bytes */
451 _dbus_string_delete (&message->header,
455 message->header_fields[field].offset = -1;
457 adjust_field_offsets (message,
461 append_header_padding (message);
466 delete_string_field (DBusMessage *message,
469 int offset = message->header_fields[field].offset;
473 _dbus_assert (!message->locked);
474 _dbus_assert (field_is_named[field]);
479 clear_header_padding (message);
481 get_string_field (message, field, &len);
483 /* The field typecode and name take up 8 bytes, and the nul
484 * termination is 1 bytes, string length integer is 4 bytes
486 delete_len = 8 + 4 + 1 + len;
488 _dbus_string_delete (&message->header,
492 message->header_fields[field].offset = -1;
494 adjust_field_offsets (message,
498 append_header_padding (message);
501 #ifdef DBUS_BUILD_TESTS
503 set_int_field (DBusMessage *message,
507 int offset = message->header_fields[field].offset;
509 _dbus_assert (!message->locked);
513 /* need to append the field */
518 _dbus_assert_not_reached ("appending an int field we don't support appending");
524 _dbus_marshal_set_int32 (&message->header,
534 set_uint_field (DBusMessage *message,
538 int offset = message->header_fields[field].offset;
540 _dbus_assert (!message->locked);
544 /* need to append the field */
548 case FIELD_REPLY_SERIAL:
549 return append_uint_field (message, field,
550 DBUS_HEADER_FIELD_REPLY,
553 _dbus_assert_not_reached ("appending a uint field we don't support appending");
559 _dbus_marshal_set_uint32 (&message->header,
568 set_string_field (DBusMessage *message,
572 int offset = message->header_fields[field].offset;
574 _dbus_assert (!message->locked);
575 _dbus_assert (value != NULL);
579 /* need to append the field */
584 return append_string_field (message, field,
585 DBUS_HEADER_FIELD_SENDER,
588 _dbus_assert_not_reached ("appending a string field we don't support appending");
599 clear_header_padding (message);
601 old_len = _dbus_string_get_length (&message->header);
603 len = strlen (value);
605 _dbus_string_init_const_len (&v, value,
606 len + 1); /* include nul */
607 if (!_dbus_marshal_set_string (&message->header,
613 new_len = _dbus_string_get_length (&message->header);
615 adjust_field_offsets (message,
619 if (!append_header_padding (message))
625 /* this must succeed because it was allocated on function entry and
626 * DBusString doesn't ever realloc smaller
628 if (!append_header_padding (message))
629 _dbus_assert_not_reached ("failed to reappend header padding");
636 * Sets the serial number of a message.
637 * This can only be done once on a message.
639 * @param message the message
640 * @param serial the serial
643 _dbus_message_set_serial (DBusMessage *message,
646 _dbus_assert (!message->locked);
647 _dbus_assert (dbus_message_get_serial (message) == 0);
649 set_uint_field (message, FIELD_CLIENT_SERIAL,
651 message->client_serial = serial;
655 * Sets the reply serial of a message (the client serial
656 * of the message this is a reply to).
658 * @param message the message
659 * @param reply_serial the client serial
660 * @returns #FALSE if not enough memory
663 dbus_message_set_reply_serial (DBusMessage *message,
664 dbus_uint32_t reply_serial)
666 _dbus_assert (!message->locked);
668 if (set_uint_field (message, FIELD_REPLY_SERIAL,
671 message->reply_serial = reply_serial;
679 * Returns the serial of a message or -1 if none has been specified.
680 * The message's serial number is provided by the application sending
681 * the message and is used to identify replies to this message.
683 * @param message the message
684 * @returns the client serial
687 dbus_message_get_serial (DBusMessage *message)
689 return message->client_serial;
693 * Returns the serial that the message is
694 * a reply to or 0 if none.
696 * @param message the message
697 * @returns the reply serial
700 dbus_message_get_reply_serial (DBusMessage *message)
702 return message->reply_serial;
706 * Adds a counter to be incremented immediately with the
707 * size of this message, and decremented by the size
708 * of this message when this message if finalized.
709 * The link contains a counter with its refcount already
710 * incremented, but the counter itself not incremented.
711 * Ownership of link and counter refcount is passed to
714 * @param message the message
715 * @param link link with counter as data
718 _dbus_message_add_size_counter_link (DBusMessage *message,
721 /* right now we don't recompute the delta when message
722 * size changes, and that's OK for current purposes
723 * I think, but could be important to change later.
724 * Do recompute it whenever there are no outstanding counters,
725 * since it's basically free.
727 if (message->size_counters == NULL)
729 message->size_counter_delta =
730 _dbus_string_get_length (&message->header) +
731 _dbus_string_get_length (&message->body);
734 _dbus_verbose ("message has size %ld\n",
735 message->size_counter_delta);
739 _dbus_list_append_link (&message->size_counters, link);
741 _dbus_counter_adjust (link->data, message->size_counter_delta);
745 * Adds a counter to be incremented immediately with the
746 * size of this message, and decremented by the size
747 * of this message when this message if finalized.
749 * @param message the message
750 * @param counter the counter
751 * @returns #FALSE if no memory
754 _dbus_message_add_size_counter (DBusMessage *message,
755 DBusCounter *counter)
759 link = _dbus_list_alloc_link (counter);
763 _dbus_counter_ref (counter);
764 _dbus_message_add_size_counter_link (message, link);
770 * Removes a counter tracking the size of this message, and decrements
771 * the counter by the size of this message.
773 * @param message the message
774 * @param counter the counter
777 _dbus_message_remove_size_counter (DBusMessage *message,
778 DBusCounter *counter)
780 if (!_dbus_list_remove_last (&message->size_counters,
782 _dbus_assert_not_reached ("Removed a message size counter that was not added");
784 _dbus_counter_adjust (counter, message->size_counter_delta);
786 _dbus_counter_unref (counter);
790 dbus_message_create_header (DBusMessage *message,
796 if (!_dbus_string_append_byte (&message->header, message->byte_order))
800 if (!_dbus_string_append_byte (&message->header, flags))
803 if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
806 if (!_dbus_string_append_byte (&message->header, 0))
809 message->header_fields[FIELD_HEADER_LENGTH].offset = 4;
810 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
813 message->header_fields[FIELD_BODY_LENGTH].offset = 8;
814 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
817 message->header_fields[FIELD_CLIENT_SERIAL].offset = 12;
818 if (!_dbus_marshal_int32 (&message->header, message->byte_order, -1))
821 /* Marshal message service */
824 if (!append_string_field (message,
826 DBUS_HEADER_FIELD_SERVICE,
831 _dbus_assert (name != NULL);
832 if (!append_string_field (message,
834 DBUS_HEADER_FIELD_NAME,
842 * Locks a message. Allows checking that applications don't keep a
843 * reference to a message in the outgoing queue and change it
844 * underneath us. Messages are locked when they enter the outgoing
845 * queue (dbus_connection_send_message()), and the library complains
846 * if the message is modified while locked.
848 * @param message the message to lock.
851 _dbus_message_lock (DBusMessage *message)
853 if (!message->locked)
855 /* Fill in our lengths */
856 set_uint_field (message,
858 _dbus_string_get_length (&message->header));
860 set_uint_field (message,
862 _dbus_string_get_length (&message->body));
864 message->locked = TRUE;
871 * @defgroup DBusMessage DBusMessage
873 * @brief Message to be sent or received over a DBusConnection.
875 * A DBusMessage is the most basic unit of communication over a
876 * DBusConnection. A DBusConnection represents a stream of messages
877 * received from a remote application, and a stream of messages
878 * sent to a remote application.
884 * @typedef DBusMessage
886 * Opaque data type representing a message received from or to be
887 * sent to another application.
891 dbus_message_new_empty_header (void)
893 DBusMessage *message;
896 message = dbus_new0 (DBusMessage, 1);
900 message->refcount = 1;
901 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
902 message->client_serial = 0;
903 message->reply_serial = 0;
906 while (i < FIELD_LAST)
908 message->header_fields[i].offset = -1;
912 if (!_dbus_string_init (&message->header))
918 if (!_dbus_string_init (&message->body))
920 _dbus_string_free (&message->header);
930 * Constructs a new message. Returns #NULL if memory can't be
931 * allocated for the message. The service may be #NULL in which case
932 * no service is set; this is appropriate when using D-BUS in a
933 * peer-to-peer context (no message bus).
935 * @todo reverse the arguments, first 'name' then 'service'
936 * as 'name' is more fundamental
938 * @param service service that the message should be sent to or #NULL
939 * @param name name of the message
940 * @returns a new DBusMessage, free with dbus_message_unref()
941 * @see dbus_message_unref()
944 dbus_message_new (const char *service,
947 DBusMessage *message;
949 message = dbus_message_new_empty_header ();
953 if (!dbus_message_create_header (message, service, name))
955 dbus_message_unref (message);
963 * Constructs a message that is a reply to some other
964 * message. Returns #NULL if memory can't be allocated
967 * @param original_message the message which the created
968 * message is a reply to.
969 * @returns a new DBusMessage, free with dbus_message_unref()
970 * @see dbus_message_new(), dbus_message_unref()
973 dbus_message_new_reply (DBusMessage *original_message)
975 DBusMessage *message;
976 const char *sender, *name;
978 sender = get_string_field (original_message,
980 name = get_string_field (original_message,
983 /* sender is allowed to be null here in peer-to-peer case */
985 message = dbus_message_new (sender, name);
990 if (!dbus_message_set_reply_serial (message,
991 dbus_message_get_serial (original_message)))
993 dbus_message_unref (message);
1001 * Creates a new message that is an error reply to a certain message.
1003 * @param original_message the original message
1004 * @param error_name the error name
1005 * @param error_message the error message string
1006 * @returns a new error message
1009 dbus_message_new_error_reply (DBusMessage *original_message,
1010 const char *error_name,
1011 const char *error_message)
1013 DBusMessage *message;
1015 DBusMessageIter iter;
1017 sender = get_string_field (original_message,
1018 FIELD_SENDER, NULL);
1020 _dbus_assert (sender != NULL);
1022 message = dbus_message_new (sender, error_name);
1024 if (message == NULL)
1027 if (!dbus_message_set_reply_serial (message,
1028 dbus_message_get_serial (original_message)))
1030 dbus_message_unref (message);
1034 dbus_message_append_iter_init (message, &iter);
1035 if (!dbus_message_iter_append_string (&iter, error_message))
1037 dbus_message_unref (message);
1041 dbus_message_set_is_error (message, TRUE);
1047 * Creates a new message that is an exact replica of the message
1048 * specified, except that its refcount is set to 1.
1050 * @param message the message.
1051 * @returns the new message.
1054 dbus_message_copy (const DBusMessage *message)
1056 DBusMessage *retval;
1059 retval = dbus_new0 (DBusMessage, 1);
1063 retval->refcount = 1;
1064 retval->byte_order = message->byte_order;
1065 retval->client_serial = message->client_serial;
1066 retval->reply_serial = message->reply_serial;
1067 retval->header_padding = message->header_padding;
1068 retval->locked = FALSE;
1070 if (!_dbus_string_init (&retval->header))
1076 if (!_dbus_string_init (&retval->body))
1078 _dbus_string_free (&retval->header);
1083 if (!_dbus_string_copy (&message->header, 0,
1084 &retval->header, 0))
1086 _dbus_string_free (&retval->header);
1087 _dbus_string_free (&retval->body);
1093 if (!_dbus_string_copy (&message->body, 0,
1096 _dbus_string_free (&retval->header);
1097 _dbus_string_free (&retval->body);
1103 for (i = 0; i < FIELD_LAST; i++)
1105 retval->header_fields[i].offset = message->header_fields[i].offset;
1113 * Increments the reference count of a DBusMessage.
1115 * @param message The message
1116 * @see dbus_message_unref
1119 dbus_message_ref (DBusMessage *message)
1121 dbus_atomic_t refcount;
1123 refcount = _dbus_atomic_inc (&message->refcount);
1124 _dbus_assert (refcount > 1);
1128 free_size_counter (void *element,
1131 DBusCounter *counter = element;
1132 DBusMessage *message = data;
1134 _dbus_counter_adjust (counter, - message->size_counter_delta);
1136 _dbus_counter_unref (counter);
1140 * Decrements the reference count of a DBusMessage.
1142 * @param message The message
1143 * @see dbus_message_ref
1146 dbus_message_unref (DBusMessage *message)
1148 dbus_atomic_t refcount;
1150 refcount = _dbus_atomic_dec (&message->refcount);
1152 _dbus_assert (refcount >= 0);
1156 _dbus_list_foreach (&message->size_counters,
1157 free_size_counter, message);
1158 _dbus_list_clear (&message->size_counters);
1160 _dbus_string_free (&message->header);
1161 _dbus_string_free (&message->body);
1163 dbus_free (message);
1168 * Gets the name of a message.
1170 * @param message the message
1171 * @returns the message name (should not be freed)
1174 dbus_message_get_name (DBusMessage *message)
1176 return get_string_field (message, FIELD_NAME, NULL);
1180 * Gets the destination service of a message.
1182 * @todo I think if we have set_sender/get_sender,
1183 * this function might be better named set_destination/
1184 * get_destination for clarity, as the sender
1185 * is also a service name.
1187 * @param message the message
1188 * @returns the message destination service (should not be freed)
1191 dbus_message_get_service (DBusMessage *message)
1193 return get_string_field (message, FIELD_SERVICE, NULL);
1197 * Appends fields to a message given a variable argument
1198 * list. The variable argument list should contain the type
1199 * of the argument followed by the value to add.
1200 * Array values are specified by a int typecode followed by a pointer
1201 * to the array followed by an int giving the length of the array.
1202 * The argument list must be terminated with 0.
1204 * This function doesn't support dicts or non-fundamental arrays.
1206 * @param message the message
1207 * @param first_arg_type type of the first argument
1208 * @param ... value of first argument, list of additional type-value pairs
1209 * @returns #TRUE on success
1212 dbus_message_append_args (DBusMessage *message,
1219 va_start (var_args, first_arg_type);
1220 retval = dbus_message_append_args_valist (message,
1229 * This function takes a va_list for use by language bindings
1231 * @todo: Shouldn't this function clean up the changes to the message
1234 * @see dbus_message_append_args.
1235 * @param message the message
1236 * @param first_arg_type type of first argument
1237 * @param var_args value of first argument, then list of type/value pairs
1238 * @returns #TRUE on success
1241 dbus_message_append_args_valist (DBusMessage *message,
1246 DBusMessageIter iter;
1248 old_len = _dbus_string_get_length (&message->body);
1250 type = first_arg_type;
1252 dbus_message_append_iter_init (message, &iter);
1259 if (!dbus_message_iter_append_nil (&iter))
1262 case DBUS_TYPE_BOOLEAN:
1263 if (!dbus_message_iter_append_boolean (&iter, va_arg (var_args, dbus_bool_t)))
1266 case DBUS_TYPE_INT32:
1267 if (!dbus_message_iter_append_int32 (&iter, va_arg (var_args, dbus_int32_t)))
1270 case DBUS_TYPE_UINT32:
1271 if (!dbus_message_iter_append_uint32 (&iter, va_arg (var_args, dbus_uint32_t)))
1274 case DBUS_TYPE_DOUBLE:
1275 if (!dbus_message_iter_append_double (&iter, va_arg (var_args, double)))
1278 case DBUS_TYPE_STRING:
1279 if (!dbus_message_iter_append_string (&iter, va_arg (var_args, const char *)))
1282 case DBUS_TYPE_NAMED:
1285 unsigned char *data;
1288 name = va_arg (var_args, const char *);
1289 data = va_arg (var_args, unsigned char *);
1290 len = va_arg (var_args, int);
1292 if (!dbus_message_iter_append_named (&iter, name, data, len))
1296 case DBUS_TYPE_ARRAY:
1301 type = va_arg (var_args, int);
1302 data = va_arg (var_args, void *);
1303 len = va_arg (var_args, int);
1307 case DBUS_TYPE_BYTE:
1308 if (!dbus_message_iter_append_byte_array (&iter, (unsigned char *)data, len))
1311 case DBUS_TYPE_BOOLEAN:
1312 if (!dbus_message_iter_append_boolean_array (&iter, (unsigned char *)data, len))
1315 case DBUS_TYPE_INT32:
1316 if (!dbus_message_iter_append_int32_array (&iter, (dbus_int32_t *)data, len))
1319 case DBUS_TYPE_UINT32:
1320 if (!dbus_message_iter_append_uint32_array (&iter, (dbus_uint32_t *)data, len))
1323 case DBUS_TYPE_DOUBLE:
1324 if (!dbus_message_iter_append_double_array (&iter, (double *)data, len))
1327 case DBUS_TYPE_STRING:
1328 if (!dbus_message_iter_append_string_array (&iter, (const char **)data, len))
1332 case DBUS_TYPE_ARRAY:
1333 case DBUS_TYPE_NAMED:
1334 case DBUS_TYPE_DICT:
1335 _dbus_warn ("dbus_message_append_args_valist doesn't support recursive arrays\n");
1338 _dbus_warn ("Unknown field type %d\n", type);
1344 case DBUS_TYPE_DICT:
1345 _dbus_warn ("dbus_message_append_args_valist doesn't support dicts\n");
1348 _dbus_warn ("Unknown field type %d\n", type);
1352 type = va_arg (var_args, int);
1363 * Gets arguments from a message given a variable argument list.
1364 * The variable argument list should contain the type of the
1365 * argumen followed by a pointer to where the value should be
1366 * stored. The list is terminated with 0.
1368 * @param message the message
1369 * @param error error to be filled in on failure
1370 * @param first_arg_type the first argument type
1371 * @param ... location for first argument value, then list of type-location pairs
1372 * @returns #FALSE if the error was set
1375 dbus_message_get_args (DBusMessage *message,
1383 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1385 va_start (var_args, first_arg_type);
1386 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1393 * This function takes a va_list for use by language bindings
1395 * @todo We need to free the argument data when an error occurs.
1397 * @see dbus_message_get_args
1398 * @param message the message
1399 * @param error error to be filled in
1400 * @param first_arg_type type of the first argument
1401 * @param var_args return location for first argument, followed by list of type/location pairs
1402 * @returns #FALSE if error was set
1405 dbus_message_get_args_valist (DBusMessage *message,
1410 DBusMessageIter iter;
1412 dbus_message_iter_init (message, &iter);
1413 return dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
1417 * Gets arguments from a message iterator given a variable argument list.
1418 * The variable argument list should contain the type of the
1419 * argumen followed by a pointer to where the value should be
1420 * stored. The list is terminated with 0.
1422 * @param iter the message iterator
1423 * @param error error to be filled in on failure
1424 * @param first_arg_type the first argument type
1425 * @param ... location for first argument value, then list of type-location pairs
1426 * @returns #FALSE if the error was set
1429 dbus_message_iter_get_args (DBusMessageIter *iter,
1437 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1439 va_start (var_args, first_arg_type);
1440 retval = dbus_message_iter_get_args_valist (iter, error, first_arg_type, var_args);
1447 * This function takes a va_list for use by language bindings
1449 * @todo this function (or some lower-level non-convenience function)
1450 * needs better error handling; should allow the application to
1451 * distinguish between out of memory, and bad data from the remote
1452 * app. It also needs to not leak a bunch of args when it gets
1453 * to the arg that's bad, as that would be a security hole
1454 * (allow one app to force another to leak memory)
1456 * @todo We need to free the argument data when an error occurs.
1458 * @see dbus_message_get_args
1459 * @param iter the message iter
1460 * @param error error to be filled in
1461 * @param first_arg_type type of the first argument
1462 * @param var_args return location for first argument, followed by list of type/location pairs
1463 * @returns #FALSE if error was set
1466 dbus_message_iter_get_args_valist (DBusMessageIter *iter,
1471 int spec_type, msg_type, i;
1474 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1478 spec_type = first_arg_type;
1481 while (spec_type != 0)
1483 msg_type = dbus_message_iter_get_arg_type (iter);
1485 if (msg_type != spec_type)
1487 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1488 "Argument %d is specified to be of type \"%s\", but "
1489 "is actually of type \"%s\"\n", i,
1490 _dbus_type_to_string (spec_type),
1491 _dbus_type_to_string (msg_type));
1500 case DBUS_TYPE_BYTE:
1504 ptr = va_arg (var_args, unsigned char *);
1506 *ptr = dbus_message_iter_get_byte (iter);
1509 case DBUS_TYPE_BOOLEAN:
1513 ptr = va_arg (var_args, dbus_bool_t *);
1515 *ptr = dbus_message_iter_get_boolean (iter);
1518 case DBUS_TYPE_INT32:
1522 ptr = va_arg (var_args, dbus_int32_t *);
1524 *ptr = dbus_message_iter_get_int32 (iter);
1527 case DBUS_TYPE_UINT32:
1531 ptr = va_arg (var_args, dbus_uint32_t *);
1533 *ptr = dbus_message_iter_get_uint32 (iter);
1537 case DBUS_TYPE_DOUBLE:
1541 ptr = va_arg (var_args, double *);
1543 *ptr = dbus_message_iter_get_double (iter);
1547 case DBUS_TYPE_STRING:
1551 ptr = va_arg (var_args, char **);
1553 *ptr = dbus_message_iter_get_string (iter);
1557 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1564 case DBUS_TYPE_NAMED:
1567 unsigned char **data;
1570 name = va_arg (var_args, char **);
1571 data = va_arg (var_args, unsigned char **);
1572 len = va_arg (var_args, int *);
1574 if (!dbus_message_iter_get_named (iter, name, data, len))
1576 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1581 case DBUS_TYPE_ARRAY:
1586 type = va_arg (var_args, int);
1587 data = va_arg (var_args, void *);
1588 len = va_arg (var_args, int *);
1590 if (dbus_message_iter_get_array_type (iter) != type)
1592 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1593 "Argument %d is specified to be of type \"array of %s\", but "
1594 "is actually of type \"array of %s\"\n", i,
1595 _dbus_type_to_string (type),
1596 _dbus_type_to_string (dbus_message_iter_get_array_type (iter)));
1602 case DBUS_TYPE_BYTE:
1603 if (!dbus_message_iter_get_byte_array (iter, (unsigned char **)data, len))
1605 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1609 case DBUS_TYPE_BOOLEAN:
1610 if (!dbus_message_iter_get_boolean_array (iter, (unsigned char **)data, len))
1612 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1616 case DBUS_TYPE_INT32:
1617 if (!dbus_message_iter_get_int32_array (iter, (dbus_int32_t **)data, len))
1619 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1623 case DBUS_TYPE_UINT32:
1624 if (!dbus_message_iter_get_uint32_array (iter, (dbus_uint32_t **)data, len))
1626 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1630 case DBUS_TYPE_DOUBLE:
1631 if (!dbus_message_iter_get_double_array (iter, (double **)data, len))
1633 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1637 case DBUS_TYPE_STRING:
1638 if (!dbus_message_iter_get_string_array (iter, (char ***)data, len))
1640 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1645 case DBUS_TYPE_ARRAY:
1646 case DBUS_TYPE_NAMED:
1647 case DBUS_TYPE_DICT:
1648 _dbus_warn ("dbus_message_get_args_valist doesn't support recursive arrays\n");
1649 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
1652 _dbus_warn ("Unknown field type %d\n", type);
1653 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
1658 case DBUS_TYPE_DICT:
1659 _dbus_warn ("dbus_message_get_args_valist doesn't support dicts\n");
1660 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
1663 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
1664 _dbus_warn ("Unknown field type %d\n", spec_type);
1668 spec_type = va_arg (var_args, int);
1669 if (spec_type != 0 && !dbus_message_iter_next (iter))
1671 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1672 "Message has only %d arguments, but more were expected", i);
1688 * Initializes a DBusMessageIter representing the arguments of the
1689 * message passed in.
1691 * @param message the message
1692 * @param _iter pointer to an iterator to initialize
1695 dbus_message_iter_init (DBusMessage *message,
1696 DBusMessageIter *iter)
1698 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1700 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
1702 real->message = message;
1703 real->parent_iter = NULL;
1704 real->changed_stamp = message->changed_stamp;
1706 real->type = DBUS_MESSAGE_ITER_TYPE_MESSAGE;
1708 real->end = _dbus_string_get_length (&message->body);
1710 real->container_start = 0;
1711 real->container_length_pos = 0;
1712 real->wrote_dict_key = 0;
1713 real->array_type_pos = 0;
1717 dbus_message_iter_check (DBusMessageRealIter *iter)
1719 if (iter->changed_stamp != iter->message->changed_stamp)
1720 _dbus_warn ("dbus iterator check failed: invalid iterator\n");
1721 if (iter->pos < 0 || iter->pos > iter->end)
1722 _dbus_warn ("dbus iterator check failed: invalid position\n");
1726 skip_array_type (DBusMessageRealIter *iter, int pos)
1732 data = _dbus_string_get_const_data_len (&iter->message->body,
1735 while (*data == DBUS_TYPE_ARRAY);
1741 dbus_message_iter_get_data_start (DBusMessageRealIter *iter, int *type)
1748 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
1749 data = _dbus_string_get_const_data_len (&iter->message->body,
1751 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
1754 *type = DBUS_TYPE_INVALID;
1756 return skip_array_type (iter, iter->pos);
1758 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
1759 data = _dbus_string_get_const_data_len (&iter->message->body,
1760 iter->array_type_pos, 1);
1761 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
1764 *type = DBUS_TYPE_INVALID;
1768 case DBUS_MESSAGE_ITER_TYPE_DICT:
1769 /* Get the length of the string */
1770 len = _dbus_demarshal_uint32 (&iter->message->body,
1771 iter->message->byte_order,
1773 pos = pos + len + 1;
1775 data = _dbus_string_get_const_data_len (&iter->message->body,
1777 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
1780 *type = DBUS_TYPE_INVALID;
1782 return skip_array_type (iter, pos);
1785 _dbus_assert_not_reached ("Invalid iter type");
1788 *type = DBUS_TYPE_INVALID;
1794 * Checks if an iterator has any more fields.
1796 * @param iter the message iter
1797 * @returns #TRUE if there are more fields
1801 dbus_message_iter_has_next (DBusMessageIter *iter)
1803 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1807 dbus_message_iter_check (real);
1809 if (real->pos >= real->end)
1812 pos = dbus_message_iter_get_data_start (real, &type);
1814 if (!_dbus_marshal_get_arg_end_pos (&real->message->body,
1815 real->message->byte_order,
1816 type, pos, &end_pos))
1819 if (end_pos >= real->end)
1826 * Moves the iterator to the next field.
1828 * @param _iter The message iter
1829 * @returns #TRUE if the iterator was moved to the next field
1832 dbus_message_iter_next (DBusMessageIter *iter)
1834 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1838 dbus_message_iter_check (real);
1840 pos = dbus_message_iter_get_data_start (real, &type);
1842 if (!_dbus_marshal_get_arg_end_pos (&real->message->body,
1843 real->message->byte_order,
1844 type, pos, &end_pos))
1847 if (end_pos >= real->end)
1850 real->pos = end_pos;
1856 * Returns the argument type of the argument that the
1857 * message iterator points at.
1859 * @param iter the message iter
1860 * @returns the field type
1863 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1865 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1868 dbus_message_iter_check (real);
1870 if (real->pos >= real->end)
1871 return DBUS_TYPE_INVALID;
1873 pos = dbus_message_iter_get_data_start (real, &type);
1879 iter_get_array_type (DBusMessageRealIter *iter, int *array_type_pos)
1882 int _array_type_pos;
1887 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
1888 _array_type_pos = iter->pos + 1;
1890 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
1891 _array_type_pos = iter->array_type_pos + 1;
1893 case DBUS_MESSAGE_ITER_TYPE_DICT:
1894 /* Get the length of the string */
1895 len = _dbus_demarshal_uint32 (&iter->message->body,
1896 iter->message->byte_order,
1898 pos = pos + len + 1;
1899 data = _dbus_string_get_const_data_len (&iter->message->body,
1901 _array_type_pos = pos + 1;
1904 _dbus_assert_not_reached ("wrong iter type");
1905 return DBUS_TYPE_INVALID;
1908 if (array_type_pos != NULL)
1909 *array_type_pos = _array_type_pos;
1911 data = _dbus_string_get_const_data_len (&iter->message->body,
1912 _array_type_pos, 1);
1913 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
1916 return DBUS_TYPE_INVALID;
1921 * Returns the element type of the array that the
1922 * message iterator points at. Note that you need
1923 * to check that the iterator points to an array
1924 * prior to using this function.
1926 * @param iter the message iter
1927 * @returns the field type
1930 dbus_message_iter_get_array_type (DBusMessageIter *iter)
1932 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1935 dbus_message_iter_check (real);
1937 if (real->pos >= real->end)
1938 return DBUS_TYPE_INVALID;
1940 pos = dbus_message_iter_get_data_start (real, &type);
1942 _dbus_assert (type == DBUS_TYPE_ARRAY);
1944 return iter_get_array_type (real, NULL);
1949 * Returns the string value that an iterator may point to.
1950 * Note that you need to check that the iterator points to
1951 * a string value before using this function.
1953 * @see dbus_message_iter_get_arg_type
1954 * @param iter the message iter
1955 * @returns the string
1958 dbus_message_iter_get_string (DBusMessageIter *iter)
1960 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1963 dbus_message_iter_check (real);
1965 pos = dbus_message_iter_get_data_start (real, &type);
1967 _dbus_assert (type == DBUS_TYPE_STRING);
1969 return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
1974 * Returns the name and data from a named type that an
1975 * iterator may point to. Note that you need to check that
1976 * the iterator points to a named type before using this
1979 * @see dbus_message_iter_get_arg_type
1980 * @param iter the message iter
1981 * @param name return location for the name
1982 * @param value return location for data
1983 * @param len return location for length of data
1984 * @returns TRUE if get succeed
1988 dbus_message_iter_get_named (DBusMessageIter *iter,
1990 unsigned char **value,
1993 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
1997 dbus_message_iter_check (real);
1999 pos = dbus_message_iter_get_data_start (real, &type);
2001 _dbus_assert (type == DBUS_TYPE_NAMED);
2003 _name = _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2009 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
2010 pos + 1, NULL, value, len))
2022 * Returns the byte value that an iterator may point to.
2023 * Note that you need to check that the iterator points to
2024 * a byte value before using this function.
2026 * @see dbus_message_iter_get_arg_type
2027 * @param iter the message iter
2028 * @returns the byte value
2031 dbus_message_iter_get_byte (DBusMessageIter *iter)
2033 unsigned char value;
2034 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2037 dbus_message_iter_check (real);
2039 pos = dbus_message_iter_get_data_start (real, &type);
2041 _dbus_assert (type == DBUS_TYPE_BYTE);
2043 value = _dbus_string_get_byte (&real->message->body, pos);
2050 * Returns the boolean value that an iterator may point to.
2051 * Note that you need to check that the iterator points to
2052 * a boolean value before using this function.
2054 * @see dbus_message_iter_get_arg_type
2055 * @param iter the message iter
2056 * @returns the boolean value
2059 dbus_message_iter_get_boolean (DBusMessageIter *iter)
2061 unsigned char value;
2062 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2065 dbus_message_iter_check (real);
2067 pos = dbus_message_iter_get_data_start (real, &type);
2069 _dbus_assert (type == DBUS_TYPE_BOOLEAN);
2071 value = _dbus_string_get_byte (&real->message->body, pos);
2077 * Returns the 32 bit signed integer value that an iterator may point to.
2078 * Note that you need to check that the iterator points to
2079 * an integer value before using this function.
2081 * @see dbus_message_iter_get_arg_type
2082 * @param iter the message iter
2083 * @returns the integer
2086 dbus_message_iter_get_int32 (DBusMessageIter *iter)
2088 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2091 dbus_message_iter_check (real);
2093 pos = dbus_message_iter_get_data_start (real, &type);
2095 _dbus_assert (type == DBUS_TYPE_INT32);
2097 return _dbus_demarshal_int32 (&real->message->body, real->message->byte_order,
2102 * Returns the 32 bit unsigned integer value that an iterator may point to.
2103 * Note that you need to check that the iterator points to
2104 * an unsigned integer value before using this function.
2106 * @see dbus_message_iter_get_arg_type
2107 * @param iter the message iter
2108 * @returns the integer
2111 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
2113 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2116 dbus_message_iter_check (real);
2118 pos = dbus_message_iter_get_data_start (real, &type);
2120 _dbus_assert (type == DBUS_TYPE_UINT32);
2122 return _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
2127 * Returns the double value that an iterator may point to.
2128 * Note that you need to check that the iterator points to
2129 * a string value before using this function.
2131 * @see dbus_message_iter_get_arg_type
2132 * @param iter the message iter
2133 * @returns the double
2136 dbus_message_iter_get_double (DBusMessageIter *iter)
2138 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2141 dbus_message_iter_check (real);
2143 pos = dbus_message_iter_get_data_start (real, &type);
2145 _dbus_assert (type == DBUS_TYPE_DOUBLE);
2147 return _dbus_demarshal_double (&real->message->body, real->message->byte_order,
2152 * Initializes an iterator for the array that the iterator
2153 * may point to. Note that you need to check that the iterator
2154 * points to an array prior to using this function.
2156 * The array element type is returned in array_type, and the array
2157 * iterator can only be used to get that type of data.
2159 * @param iter the iterator
2160 * @param array_iter pointer to an iterator to initialize
2161 * @param array_type gets set to the type of the array elements
2162 * @returns #TRUE on success
2165 dbus_message_iter_init_array_iterator (DBusMessageIter *iter,
2166 DBusMessageIter *array_iter,
2169 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2170 DBusMessageRealIter *array_real = (DBusMessageRealIter *)array_iter;
2171 int type, pos, len_pos, len, array_type_pos;
2174 dbus_message_iter_check (real);
2176 pos = dbus_message_iter_get_data_start (real, &type);
2178 _dbus_assert (type == DBUS_TYPE_ARRAY);
2180 _array_type = iter_get_array_type (real, &array_type_pos);
2182 len_pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
2183 len = _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
2186 array_real->parent_iter = real;
2187 array_real->message = real->message;
2188 array_real->changed_stamp = real->message->changed_stamp;
2190 array_real->type = DBUS_MESSAGE_ITER_TYPE_ARRAY;
2191 array_real->pos = pos;
2192 array_real->end = pos + len;
2194 array_real->container_start = pos;
2195 array_real->container_length_pos = len_pos;
2196 array_real->wrote_dict_key = 0;
2197 array_real->array_type_pos = array_type_pos;
2198 array_real->array_type_done = TRUE;
2200 if (array_type != NULL)
2201 *array_type = _array_type;
2208 * Initializes an iterator for the dict that the iterator
2209 * may point to. Note that you need to check that the iterator
2210 * points to a dict prior to using this function.
2212 * @param iter the iterator
2213 * @param dict_iter pointer to an iterator to initialize
2214 * @returns #TRUE on success
2217 dbus_message_iter_init_dict_iterator (DBusMessageIter *iter,
2218 DBusMessageIter *dict_iter)
2220 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2221 DBusMessageRealIter *dict_real = (DBusMessageRealIter *)dict_iter;
2222 int type, pos, len_pos, len;
2224 dbus_message_iter_check (real);
2226 pos = dbus_message_iter_get_data_start (real, &type);
2228 _dbus_assert (type == DBUS_TYPE_DICT);
2230 len_pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
2231 len = _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
2234 dict_real->parent_iter = real;
2235 dict_real->message = real->message;
2236 dict_real->changed_stamp = real->message->changed_stamp;
2238 dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
2239 dict_real->pos = pos;
2240 dict_real->end = pos + len;
2242 dict_real->container_start = pos;
2243 dict_real->container_length_pos = len_pos;
2244 dict_real->wrote_dict_key = 0;
2250 * Returns the byte array that the iterator may point to.
2251 * Note that you need to check that the iterator points
2252 * to a byte array prior to using this function.
2254 * @param iter the iterator
2255 * @param value return location for array values
2256 * @param len return location for length of byte array
2257 * @returns #TRUE on success
2260 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
2261 unsigned char **value,
2264 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2267 dbus_message_iter_check (real);
2269 pos = dbus_message_iter_get_data_start (real, &type);
2271 _dbus_assert (type == DBUS_TYPE_ARRAY);
2273 type = iter_get_array_type (real, NULL);
2275 _dbus_assert (type == DBUS_TYPE_BYTE);
2277 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
2278 pos, NULL, value, len))
2285 * Returns the boolean array that the iterator may point to. Note that
2286 * you need to check that the iterator points to an array of the
2287 * correct type prior to using this function.
2289 * @param iter the iterator
2290 * @param value return location for the array
2291 * @param len return location for the array length
2292 * @returns #TRUE on success
2295 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
2296 unsigned char **value,
2299 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2302 dbus_message_iter_check (real);
2304 pos = dbus_message_iter_get_data_start (real, &type);
2306 _dbus_assert (type == DBUS_TYPE_ARRAY);
2308 type = iter_get_array_type (real, NULL);
2310 _dbus_assert (type == DBUS_TYPE_BOOLEAN);
2312 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
2313 pos, NULL, value, len))
2320 * Returns the 32 bit signed integer array that the iterator may point
2321 * to. Note that you need to check that the iterator points to an
2322 * array of the correct type prior to using this function.
2324 * @param iter the iterator
2325 * @param value return location for the array
2326 * @param len return location for the array length
2327 * @returns #TRUE on success
2330 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
2331 dbus_int32_t **value,
2334 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2337 dbus_message_iter_check (real);
2339 pos = dbus_message_iter_get_data_start (real, &type);
2341 _dbus_assert (type == DBUS_TYPE_ARRAY);
2343 type = iter_get_array_type (real, NULL);
2345 _dbus_assert (type == DBUS_TYPE_INT32);
2347 if (!_dbus_demarshal_int32_array (&real->message->body, real->message->byte_order,
2348 pos, NULL, value, len))
2355 * Returns the 32 bit unsigned integer array that the iterator may point
2356 * to. Note that you need to check that the iterator points to an
2357 * array of the correct type prior to using this function.
2359 * @param iter the iterator
2360 * @param value return location for the array
2361 * @param len return location for the array length
2362 * @returns #TRUE on success
2365 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
2366 dbus_uint32_t **value,
2369 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2372 dbus_message_iter_check (real);
2374 pos = dbus_message_iter_get_data_start (real, &type);
2376 _dbus_assert (type == DBUS_TYPE_ARRAY);
2378 type = iter_get_array_type (real, NULL);
2379 _dbus_assert (type == DBUS_TYPE_UINT32);
2381 if (!_dbus_demarshal_uint32_array (&real->message->body, real->message->byte_order,
2382 pos, NULL, value, len))
2389 * Returns the double array that the iterator may point to. Note that
2390 * you need to check that the iterator points to an array of the
2391 * correct type prior to using this function.
2393 * @param iter the iterator
2394 * @param value return location for the array
2395 * @param len return location for the array length
2396 * @returns #TRUE on success
2399 dbus_message_iter_get_double_array (DBusMessageIter *iter,
2403 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2406 dbus_message_iter_check (real);
2408 pos = dbus_message_iter_get_data_start (real, &type);
2410 _dbus_assert (type == DBUS_TYPE_ARRAY);
2412 type = iter_get_array_type (real, NULL);
2413 _dbus_assert (type == DBUS_TYPE_DOUBLE);
2415 if (!_dbus_demarshal_double_array (&real->message->body, real->message->byte_order,
2416 pos, NULL, value, len))
2423 * Returns the string array that the iterator may point to.
2424 * Note that you need to check that the iterator points
2425 * to a byte array prior to using this function.
2427 * The returned value is a #NULL-terminated array of strings.
2428 * Each string is a separate malloc block, and the array
2429 * itself is a malloc block. You can free this type of
2430 * string array with dbus_free_string_array().
2432 * @param iter the iterator
2433 * @param value return location for string values
2434 * @param len return location for length of byte array
2435 * @returns #TRUE on success
2438 dbus_message_iter_get_string_array (DBusMessageIter *iter,
2442 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2445 dbus_message_iter_check (real);
2447 pos = dbus_message_iter_get_data_start (real, &type);
2449 _dbus_assert (type == DBUS_TYPE_ARRAY);
2451 type = iter_get_array_type (real, NULL);
2452 _dbus_assert (type == DBUS_TYPE_STRING);
2454 if (!_dbus_demarshal_string_array (&real->message->body, real->message->byte_order,
2455 pos, NULL, value, len))
2462 * Returns the key name fot the dict entry that an iterator
2463 * may point to. Note that you need to check that the iterator
2464 * points to a dict entry before using this function.
2466 * @see dbus_message_iter_init_dict_iterator
2467 * @param iter the message iter
2468 * @returns the key name
2471 dbus_message_iter_get_dict_key (DBusMessageIter *iter)
2473 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2475 dbus_message_iter_check (real);
2477 _dbus_assert (real->type == DBUS_MESSAGE_ITER_TYPE_DICT);
2479 return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2484 * Initializes a DBusMessageIter pointing to the end of the
2485 * message. This iterator can be used to append data to the
2488 * @param message the message
2489 * @param _iter pointer to an iterator to initialize
2492 dbus_message_append_iter_init (DBusMessage *message,
2493 DBusMessageIter *iter)
2495 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2497 real->message = message;
2498 real->parent_iter = NULL;
2499 real->changed_stamp = message->changed_stamp;
2501 real->type = DBUS_MESSAGE_ITER_TYPE_MESSAGE;
2502 real->end = _dbus_string_get_length (&real->message->body);
2503 real->pos = real->end;
2505 real->container_length_pos = 0;
2506 real->wrote_dict_key = 0;
2510 dbus_message_iter_append_check (DBusMessageRealIter *iter)
2512 _dbus_assert (!iter->message->locked);
2514 if (iter->changed_stamp != iter->message->changed_stamp)
2515 _dbus_warn ("dbus iterator check failed: invalid iterator");
2517 if (iter->pos != iter->end)
2518 _dbus_warn ("dbus iterator check failed: can only append at end of message");
2520 if (iter->pos != _dbus_string_get_length (&iter->message->body))
2521 _dbus_warn ("dbus iterator check failed: append pos not at end of message string");
2525 dbus_message_iter_append_type (DBusMessageRealIter *iter,
2531 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
2532 if (!_dbus_string_append_byte (&iter->message->body, type))
2536 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
2537 data = _dbus_string_get_const_data_len (&iter->message->body,
2538 iter->array_type_pos, 1);
2541 _dbus_warn ("Appended element of wrong type for array\n");
2546 case DBUS_MESSAGE_ITER_TYPE_DICT:
2547 if (!iter->wrote_dict_key)
2549 _dbus_warn ("Appending dict data before key name\n");
2553 if (!_dbus_string_append_byte (&iter->message->body, type))
2559 _dbus_assert_not_reached ("Invalid iter type");
2567 dbus_message_iter_update_after_change (DBusMessageRealIter *iter)
2569 iter->changed_stamp = iter->message->changed_stamp;
2571 /* Set new end of iter */
2572 iter->end = _dbus_string_get_length (&iter->message->body);
2573 iter->pos = iter->end;
2575 /* Set container length */
2576 if (iter->type == DBUS_MESSAGE_ITER_TYPE_DICT ||
2577 (iter->type == DBUS_MESSAGE_ITER_TYPE_ARRAY && iter->array_type_done))
2578 _dbus_marshal_set_uint32 (&iter->message->body,
2579 iter->message->byte_order,
2580 iter->container_length_pos,
2581 iter->end - iter->container_start);
2583 if (iter->parent_iter)
2584 dbus_message_iter_update_after_change (iter->parent_iter);
2588 dbus_message_iter_append_done (DBusMessageRealIter *iter)
2590 iter->message->changed_stamp++;
2591 dbus_message_iter_update_after_change (iter);
2592 iter->wrote_dict_key = FALSE;
2596 * Appends a nil value to the message
2598 * @param iter an iterator pointing to the end of the message
2599 * @returns #TRUE on success
2602 dbus_message_iter_append_nil (DBusMessageIter *iter)
2604 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2606 dbus_message_iter_append_check (real);
2608 if (!dbus_message_iter_append_type (real, DBUS_TYPE_NIL))
2611 dbus_message_iter_append_done (real);
2617 * Appends a boolean value to the message
2619 * @param iter an iterator pointing to the end of the message
2620 * @param value the boolean value
2621 * @returns #TRUE on success
2624 dbus_message_iter_append_boolean (DBusMessageIter *iter,
2627 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2629 dbus_message_iter_append_check (real);
2631 if (!dbus_message_iter_append_type (real, DBUS_TYPE_BOOLEAN))
2634 if (!_dbus_string_append_byte (&real->message->body, (value != FALSE)))
2636 _dbus_string_set_length (&real->message->body, real->pos);
2640 dbus_message_iter_append_done (real);
2646 * Appends a byte to the message
2648 * @param iter an iterator pointing to the end of the message
2649 * @param value the byte value
2650 * @returns #TRUE on success
2653 dbus_message_iter_append_byte (DBusMessageIter *iter,
2654 unsigned char value)
2656 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2658 dbus_message_iter_append_check (real);
2660 if (!dbus_message_iter_append_type (real, DBUS_TYPE_BYTE))
2663 if (!_dbus_string_append_byte (&real->message->body, value))
2665 _dbus_string_set_length (&real->message->body, real->pos);
2669 dbus_message_iter_append_done (real);
2676 * Appends a 32 bit signed integer to the message.
2678 * @param iter an iterator pointing to the end of the message
2679 * @param value the integer value
2680 * @returns #TRUE on success
2683 dbus_message_iter_append_int32 (DBusMessageIter *iter,
2686 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2688 dbus_message_iter_append_check (real);
2690 if (!dbus_message_iter_append_type (real, DBUS_TYPE_INT32))
2693 if (!_dbus_marshal_int32 (&real->message->body, real->message->byte_order, value))
2695 _dbus_string_set_length (&real->message->body, real->pos);
2699 dbus_message_iter_append_done (real);
2705 * Appends a 32 bit unsigned integer to the message.
2707 * @param iter an iterator pointing to the end of the message
2708 * @param value the integer value
2709 * @returns #TRUE on success
2712 dbus_message_iter_append_uint32 (DBusMessageIter *iter,
2713 dbus_uint32_t value)
2715 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2717 dbus_message_iter_append_check (real);
2719 if (!dbus_message_iter_append_type (real, DBUS_TYPE_UINT32))
2722 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, value))
2724 _dbus_string_set_length (&real->message->body, real->pos);
2728 dbus_message_iter_append_done (real);
2734 * Appends a double value to the message.
2736 * @param iter an iterator pointing to the end of the message
2737 * @param value the double value
2738 * @returns #TRUE on success
2741 dbus_message_iter_append_double (DBusMessageIter *iter,
2744 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2746 dbus_message_iter_append_check (real);
2748 if (!dbus_message_iter_append_type (real, DBUS_TYPE_DOUBLE))
2751 if (!_dbus_marshal_double (&real->message->body, real->message->byte_order, value))
2753 _dbus_string_set_length (&real->message->body, real->pos);
2757 dbus_message_iter_append_done (real);
2763 * Appends a UTF-8 string to the message.
2765 * @param iter an iterator pointing to the end of the message
2766 * @param value the string
2767 * @returns #TRUE on success
2770 dbus_message_iter_append_string (DBusMessageIter *iter,
2773 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2775 dbus_message_iter_append_check (real);
2777 if (!dbus_message_iter_append_type (real, DBUS_TYPE_STRING))
2780 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
2782 _dbus_string_set_length (&real->message->body, real->pos);
2786 dbus_message_iter_append_done (real);
2792 * Appends a named type data chunk to the message.
2794 * @param iter an iterator pointing to the end of the message
2795 * @param name the name of the type
2797 * @returns #TRUE on success
2800 dbus_message_iter_append_named (DBusMessageIter *iter,
2802 const unsigned char *data,
2805 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2807 dbus_message_iter_append_check (real);
2809 if (!dbus_message_iter_append_type (real, DBUS_TYPE_NAMED))
2812 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, name))
2814 _dbus_string_set_length (&real->message->body, real->pos);
2818 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, data, len))
2820 _dbus_string_set_length (&real->message->body, real->pos);
2824 dbus_message_iter_append_done (real);
2831 * Appends a dict key name to the message. The iterator used
2832 * must point to a dict.
2834 * @param iter an iterator pointing to the end of the message
2835 * @param value the string
2836 * @returns #TRUE on success
2839 dbus_message_iter_append_dict_key (DBusMessageIter *iter,
2842 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2844 dbus_message_iter_append_check (real);
2845 _dbus_assert (real->type == DBUS_MESSAGE_ITER_TYPE_DICT);
2847 if (real->wrote_dict_key)
2849 _dbus_warn ("Appendinging multiple dict key names\n");
2853 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
2858 dbus_message_iter_append_done (real);
2859 real->wrote_dict_key = TRUE;
2865 array_iter_type_mark_done (DBusMessageRealIter *iter)
2869 if (iter->type == DBUS_MESSAGE_ITER_TYPE_ARRAY)
2870 array_iter_type_mark_done (iter->parent_iter);
2874 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&iter->message->body),
2875 sizeof (dbus_uint32_t));
2877 /* Empty length for now, backfill later */
2878 if (!_dbus_marshal_uint32 (&iter->message->body, iter->message->byte_order, 0))
2880 _dbus_string_set_length (&iter->message->body, iter->pos);
2884 iter->container_start = _dbus_string_get_length (&iter->message->body);
2885 iter->container_length_pos = len_pos;
2886 iter->array_type_done = TRUE;
2892 append_array_type (DBusMessageRealIter *real,
2894 dbus_bool_t *array_type_done,
2895 int *array_type_pos)
2897 int existing_element_type;
2899 if (!dbus_message_iter_append_type (real, DBUS_TYPE_ARRAY))
2902 if (real->type == DBUS_MESSAGE_ITER_TYPE_ARRAY &&
2903 real->array_type_done)
2905 existing_element_type = iter_get_array_type (real, array_type_pos);
2906 if (existing_element_type != element_type)
2908 _dbus_warn ("Appending array of %d, when expecting array of %d\n",
2909 element_type, existing_element_type);
2910 _dbus_string_set_length (&real->message->body, real->pos);
2913 if (array_type_done != NULL)
2914 *array_type_done = TRUE;
2918 if (array_type_pos != NULL)
2919 *array_type_pos = _dbus_string_get_length (&real->message->body);
2921 /* Append element type */
2922 if (!_dbus_string_append_byte (&real->message->body, element_type))
2924 _dbus_string_set_length (&real->message->body, real->pos);
2928 if (array_type_done != NULL)
2929 *array_type_done = element_type != DBUS_TYPE_ARRAY;
2931 if (element_type != DBUS_TYPE_ARRAY &&
2932 !array_iter_type_mark_done (real))
2940 * Appends an array to the message and initializes an iterator that
2941 * can be used to append to the array.
2943 * @param iter an iterator pointing to the end of the message
2944 * @param array_iter pointer to an iter that will be initialized
2945 * @param element_type the type of the array elements
2946 * @returns #TRUE on success
2949 dbus_message_iter_append_array (DBusMessageIter *iter,
2950 DBusMessageIter *array_iter,
2953 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2954 DBusMessageRealIter *array_real = (DBusMessageRealIter *)array_iter;
2957 dbus_bool_t array_type_done;
2959 if (element_type == DBUS_TYPE_NIL)
2961 _dbus_warn ("Can't create NIL arrays\n");
2965 dbus_message_iter_append_check (real);
2967 if (!append_array_type (real, element_type, &array_type_done, &array_type_pos))
2970 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
2972 if (array_type_done)
2974 /* Empty length for now, backfill later */
2975 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
2977 _dbus_string_set_length (&real->message->body, real->pos);
2982 array_real->parent_iter = real;
2983 array_real->message = real->message;
2984 array_real->changed_stamp = real->message->changed_stamp;
2986 array_real->type = DBUS_MESSAGE_ITER_TYPE_ARRAY;
2987 array_real->pos = _dbus_string_get_length (&real->message->body);
2988 array_real->end = array_real->end;
2990 array_real->container_start = array_real->pos;
2991 array_real->container_length_pos = len_pos;
2992 array_real->wrote_dict_key = 0;
2993 array_real->array_type_done = array_type_done;
2994 array_real->array_type_pos = array_type_pos;
2996 dbus_message_iter_append_done (array_real);
3002 * Appends a dict to the message and initializes an iterator that
3003 * can be used to append to the dict.
3005 * @param iter an iterator pointing to the end of the message
3006 * @param array_iter pointer to an iter that will be initialized
3007 * @param element_type the type of the array elements
3008 * @returns #TRUE on success
3011 dbus_message_iter_append_dict (DBusMessageIter *iter,
3012 DBusMessageIter *dict_iter)
3014 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3015 DBusMessageRealIter *dict_real = (DBusMessageRealIter *)dict_iter;
3018 dbus_message_iter_append_check (real);
3020 if (!dbus_message_iter_append_type (real, DBUS_TYPE_DICT))
3023 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
3025 /* Empty length for now, backfill later */
3026 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
3028 _dbus_string_set_length (&real->message->body, real->pos);
3032 dict_real->parent_iter = real;
3033 dict_real->message = real->message;
3034 dict_real->changed_stamp = real->message->changed_stamp;
3036 dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
3037 dict_real->pos = _dbus_string_get_length (&real->message->body);
3038 dict_real->end = dict_real->end;
3040 dict_real->container_start = dict_real->pos;
3041 dict_real->container_length_pos = len_pos;
3042 dict_real->wrote_dict_key = 0;
3044 dbus_message_iter_append_done (dict_real);
3051 * Appends a boolean array to the message.
3053 * @param iter an iterator pointing to the end of the message
3054 * @param value the array
3055 * @param len the length of the array
3056 * @returns #TRUE on success
3059 dbus_message_iter_append_boolean_array (DBusMessageIter *iter,
3060 unsigned const char *value,
3063 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3065 dbus_message_iter_append_check (real);
3067 if (!append_array_type (real, DBUS_TYPE_BOOLEAN, NULL, NULL))
3070 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, value, len))
3072 _dbus_string_set_length (&real->message->body, real->pos);
3076 dbus_message_iter_append_done (real);
3082 * Appends a 32 bit signed integer array to the message.
3084 * @param iter an iterator pointing to the end of the message
3085 * @param value the array
3086 * @param len the length of the array
3087 * @returns #TRUE on success
3090 dbus_message_iter_append_int32_array (DBusMessageIter *iter,
3091 const dbus_int32_t *value,
3094 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3096 dbus_message_iter_append_check (real);
3098 if (!append_array_type (real, DBUS_TYPE_INT32, NULL, NULL))
3101 if (!_dbus_marshal_int32_array (&real->message->body, real->message->byte_order, value, len))
3103 _dbus_string_set_length (&real->message->body, real->pos);
3107 dbus_message_iter_append_done (real);
3113 * Appends a 32 bit unsigned integer array to the message.
3115 * @param iter an iterator pointing to the end of the message
3116 * @param value the array
3117 * @param len the length of the array
3118 * @returns #TRUE on success
3121 dbus_message_iter_append_uint32_array (DBusMessageIter *iter,
3122 const dbus_uint32_t *value,
3125 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3127 dbus_message_iter_append_check (real);
3129 if (!append_array_type (real, DBUS_TYPE_UINT32, NULL, NULL))
3132 if (!_dbus_marshal_uint32_array (&real->message->body, real->message->byte_order, value, len))
3134 _dbus_string_set_length (&real->message->body, real->pos);
3138 dbus_message_iter_append_done (real);
3144 * Appends a double array to the message.
3146 * @param iter an iterator pointing to the end of the message
3147 * @param value the array
3148 * @param len the length of the array
3149 * @returns #TRUE on success
3152 dbus_message_iter_append_double_array (DBusMessageIter *iter,
3153 const double *value,
3156 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3158 dbus_message_iter_append_check (real);
3160 if (!append_array_type (real, DBUS_TYPE_DOUBLE, NULL, NULL))
3163 if (!_dbus_marshal_double_array (&real->message->body, real->message->byte_order, value, len))
3165 _dbus_string_set_length (&real->message->body, real->pos);
3169 dbus_message_iter_append_done (real);
3175 * Appends a byte array to the message.
3177 * @param iter an iterator pointing to the end of the message
3178 * @param value the array
3179 * @param len the length of the array
3180 * @returns #TRUE on success
3183 dbus_message_iter_append_byte_array (DBusMessageIter *iter,
3184 unsigned const char *value,
3187 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3189 dbus_message_iter_append_check (real);
3191 if (!append_array_type (real, DBUS_TYPE_BYTE, NULL, NULL))
3194 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, value, len))
3196 _dbus_string_set_length (&real->message->body, real->pos);
3200 dbus_message_iter_append_done (real);
3206 * Appends a string array to the message.
3208 * @param iter an iterator pointing to the end of the message
3209 * @param value the array
3210 * @param len the length of the array
3211 * @returns #TRUE on success
3214 dbus_message_iter_append_string_array (DBusMessageIter *iter,
3218 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3220 dbus_message_iter_append_check (real);
3222 if (!append_array_type (real, DBUS_TYPE_STRING, NULL, NULL))
3225 if (!_dbus_marshal_string_array (&real->message->body, real->message->byte_order, value, len))
3227 _dbus_string_set_length (&real->message->body, real->pos);
3231 dbus_message_iter_append_done (real);
3237 * Sets the message sender.
3239 * @param message the message
3240 * @param sender the sender
3241 * @returns #FALSE if not enough memory
3244 dbus_message_set_sender (DBusMessage *message,
3247 _dbus_assert (!message->locked);
3251 delete_string_field (message, FIELD_SENDER);
3256 return set_string_field (message,
3263 * Sets a flag indicating that the message is an error reply
3264 * message, i.e. an "exception" rather than a normal response.
3266 * @param message the message
3267 * @param is_error_reply #TRUE if this is an error message.
3270 dbus_message_set_is_error (DBusMessage *message,
3271 dbus_bool_t is_error_reply)
3275 _dbus_assert (!message->locked);
3277 header = _dbus_string_get_data_len (&message->header, 1, 1);
3280 *header |= DBUS_HEADER_FLAG_ERROR;
3282 *header &= ~DBUS_HEADER_FLAG_ERROR;
3286 * Returns #TRUE if the message is an error
3287 * reply to some previous message we sent.
3289 * @param message the message
3290 * @returns #TRUE if the message is an error
3293 dbus_message_get_is_error (DBusMessage *message)
3297 header = _dbus_string_get_const_data_len (&message->header, 1, 1);
3299 return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
3303 * Gets the service which originated this message,
3304 * or #NULL if unknown or inapplicable.
3306 * @param message the message
3307 * @returns the service name or #NULL
3310 dbus_message_get_sender (DBusMessage *message)
3312 return get_string_field (message, FIELD_SENDER, NULL);
3316 * Checks whether the message has the given name.
3317 * If the message has no name or has a different
3318 * name, returns #FALSE.
3320 * @param message the message
3321 * @param name the name to check (must not be #NULL)
3323 * @returns #TRUE if the message has the given name
3326 dbus_message_name_is (DBusMessage *message,
3331 _dbus_assert (name != NULL);
3333 n = dbus_message_get_name (message);
3335 if (n && strcmp (n, name) == 0)
3342 * Checks whether the message was sent to the given service. If the
3343 * message has no service specified or has a different name, returns
3346 * @param message the message
3347 * @param service the service to check (must not be #NULL)
3349 * @returns #TRUE if the message has the given destination service
3352 dbus_message_service_is (DBusMessage *message,
3353 const char *service)
3357 _dbus_assert (service != NULL);
3359 s = dbus_message_get_service (message);
3361 if (s && strcmp (s, service) == 0)
3368 * Checks whether the message has the given service as its sender. If
3369 * the message has no sender specified or has a different sender,
3370 * returns #FALSE. Note that if a peer application owns multiple
3371 * services, its messages will have only one of those services as the
3372 * sender (usually the base service). So you can't use this
3373 * function to prove the sender didn't own service Foo, you can
3374 * only use it to prove that it did.
3376 * @param message the message
3377 * @param service the service to check (must not be #NULL)
3379 * @returns #TRUE if the message has the given origin service
3382 dbus_message_sender_is (DBusMessage *message,
3383 const char *service)
3387 _dbus_assert (service != NULL);
3389 s = dbus_message_get_sender (message);
3391 if (s && strcmp (s, service) == 0)
3398 * Sets a #DBusError based on the contents of the given
3399 * message. The error is only set if the message
3400 * is an error message, as in dbus_message_get_is_error().
3401 * The name of the error is set to the name of the message,
3402 * and the error message is set to the first argument
3403 * if the argument exists and is a string.
3405 * The return value indicates whether the error was set (the error is
3406 * set if and only if the message is an error message).
3407 * So you can check for an error reply and convert it to DBusError
3410 * @param error the error to set
3411 * @param message the message to set it from
3412 * @returns #TRUE if dbus_message_get_is_error() returns #TRUE for the message
3415 dbus_set_error_from_message (DBusError *error,
3416 DBusMessage *message)
3420 if (!dbus_message_get_is_error (message))
3424 dbus_message_get_args (message, NULL,
3425 DBUS_TYPE_STRING, &str,
3428 dbus_set_error (error, dbus_message_get_name (message),
3429 str ? "%s" : NULL, str);
3439 * @addtogroup DBusMessageInternals
3444 * @typedef DBusMessageLoader
3446 * The DBusMessageLoader object encapsulates the process of converting
3447 * a byte stream into a series of DBusMessage. It buffers the incoming
3448 * bytes as efficiently as possible, and generates a queue of
3449 * messages. DBusMessageLoader is typically used as part of a
3450 * DBusTransport implementation. The DBusTransport then hands off
3451 * the loaded messages to a DBusConnection, making the messages
3452 * visible to the application.
3454 * @todo write tests for break-loader that a) randomly delete header
3455 * fields and b) set string fields to zero-length and other funky
3460 /* we definitely use signed ints for sizes, so don't exceed
3461 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
3462 * over 128M is pretty nuts anyhow.
3466 * The maximum sane message size.
3468 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
3471 * Implementation details of DBusMessageLoader.
3472 * All members are private.
3474 struct DBusMessageLoader
3476 int refcount; /**< Reference count. */
3478 DBusString data; /**< Buffered data */
3480 DBusList *messages; /**< Complete messages. */
3482 long max_message_size; /**< Maximum size of a message */
3484 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
3486 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
3490 * The initial buffer size of the message loader.
3492 * @todo this should be based on min header size plus some average
3493 * body size, or something. Or rather, the min header size only, if we
3494 * want to try to read only the header, store that in a DBusMessage,
3495 * then read only the body and store that, etc., depends on
3496 * how we optimize _dbus_message_loader_get_buffer() and what
3497 * the exact message format is.
3499 #define INITIAL_LOADER_DATA_LEN 32
3502 * Creates a new message loader. Returns #NULL if memory can't
3505 * @returns new loader, or #NULL.
3508 _dbus_message_loader_new (void)
3510 DBusMessageLoader *loader;
3512 loader = dbus_new0 (DBusMessageLoader, 1);
3516 loader->refcount = 1;
3518 /* Try to cap message size at something that won't *totally* hose
3519 * the system if we have a couple of them.
3521 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
3523 if (!_dbus_string_init (&loader->data))
3529 /* preallocate the buffer for speed, ignore failure */
3530 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
3531 _dbus_string_set_length (&loader->data, 0);
3537 * Increments the reference count of the loader.
3539 * @param loader the loader.
3542 _dbus_message_loader_ref (DBusMessageLoader *loader)
3544 loader->refcount += 1;
3548 * Decrements the reference count of the loader and finalizes the
3549 * loader when the count reaches zero.
3551 * @param loader the loader.
3554 _dbus_message_loader_unref (DBusMessageLoader *loader)
3556 loader->refcount -= 1;
3557 if (loader->refcount == 0)
3559 _dbus_list_foreach (&loader->messages,
3560 (DBusForeachFunction) dbus_message_unref,
3562 _dbus_list_clear (&loader->messages);
3563 _dbus_string_free (&loader->data);
3569 * Gets the buffer to use for reading data from the network. Network
3570 * data is read directly into an allocated buffer, which is then used
3571 * in the DBusMessage, to avoid as many extra memcpy's as possible.
3572 * The buffer must always be returned immediately using
3573 * _dbus_message_loader_return_buffer(), even if no bytes are
3574 * successfully read.
3576 * @todo this function can be a lot more clever. For example
3577 * it can probably always return a buffer size to read exactly
3578 * the body of the next message, thus avoiding any memory wastage
3581 * @todo we need to enforce a max length on strings in header fields.
3583 * @param loader the message loader.
3584 * @param buffer the buffer
3587 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
3588 DBusString **buffer)
3590 _dbus_assert (!loader->buffer_outstanding);
3592 *buffer = &loader->data;
3594 loader->buffer_outstanding = TRUE;
3598 * The smallest header size that can occur.
3599 * (It won't be valid)
3601 #define DBUS_MINIMUM_HEADER_SIZE 16
3603 /** Pack four characters as in "abcd" into a uint32 */
3604 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
3605 ((((dbus_uint32_t)a) << 24) | \
3606 (((dbus_uint32_t)b) << 16) | \
3607 (((dbus_uint32_t)c) << 8) | \
3610 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
3611 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
3612 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
3614 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
3615 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
3616 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
3618 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
3619 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
3620 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
3622 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
3623 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
3624 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
3626 /* FIXME impose max length on name, srvc, sndr */
3628 decode_header_data (const DBusString *data,
3631 HeaderField fields[FIELD_LAST],
3632 int *message_padding)
3639 if (header_len < 16)
3643 while (i < FIELD_LAST)
3645 fields[i].offset = -1;
3649 fields[FIELD_HEADER_LENGTH].offset = 4;
3650 fields[FIELD_BODY_LENGTH].offset = 8;
3651 fields[FIELD_CLIENT_SERIAL].offset = 12;
3653 /* Now handle the named fields. A real named field is at least 4
3654 * bytes for the name, plus a type code (1 byte) plus padding. So
3655 * if we have less than 8 bytes left, it must be alignment padding,
3656 * not a field. While >= 8 bytes can't be entirely alignment
3660 while ((pos + 7) < header_len)
3662 pos = _DBUS_ALIGN_VALUE (pos, 4);
3664 if ((pos + 4) > header_len)
3667 field =_dbus_string_get_const_data_len (data, pos, 4);
3670 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
3672 switch (DBUS_UINT32_FROM_BE (*(int*)field))
3674 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
3675 if (fields[FIELD_SERVICE].offset >= 0)
3677 _dbus_verbose ("%s field provided twice\n",
3678 DBUS_HEADER_FIELD_SERVICE);
3682 fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
3684 _dbus_verbose ("Found service name at offset %d\n",
3685 fields[FIELD_SERVICE].offset);
3689 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
3690 if (fields[FIELD_NAME].offset >= 0)
3692 _dbus_verbose ("%s field provided twice\n",
3693 DBUS_HEADER_FIELD_NAME);
3697 fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
3700 _dbus_verbose ("Found message name at offset %d\n",
3701 fields[FIELD_NAME].offset);
3704 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
3705 if (fields[FIELD_SENDER].offset >= 0)
3707 _dbus_verbose ("%s field provided twice\n",
3708 DBUS_HEADER_FIELD_SENDER);
3712 fields[FIELD_SENDER].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
3714 _dbus_verbose ("Found sender name at offset %d\n",
3715 fields[FIELD_NAME].offset);
3718 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
3719 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
3721 _dbus_verbose ("%s field provided twice\n",
3722 DBUS_HEADER_FIELD_REPLY);
3726 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
3728 _dbus_verbose ("Found reply serial at offset %d\n",
3729 fields[FIELD_REPLY_SERIAL].offset);
3733 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c at offset %d\n",
3734 field[0], field[1], field[2], field[3], pos);
3737 if (!_dbus_marshal_validate_type (data, pos, &type, &pos))
3739 _dbus_verbose ("Failed to validate type of named header field\n");
3743 if (!_dbus_marshal_validate_arg (data, byte_order, 0, type, -1, pos, &new_pos))
3745 _dbus_verbose ("Failed to validate argument to named header field\n");
3749 if (new_pos > header_len)
3751 _dbus_verbose ("Named header field tries to extend beyond header length\n");
3758 if (pos < header_len)
3760 /* Alignment padding, verify that it's nul */
3761 _dbus_assert ((header_len - pos) < 8);
3763 if (!_dbus_string_validate_nul (data,
3764 pos, (header_len - pos)))
3766 _dbus_verbose ("header alignment padding is not nul\n");
3771 if (fields[FIELD_NAME].offset < 0)
3773 _dbus_verbose ("No %s field provided\n",
3774 DBUS_HEADER_FIELD_NAME);
3778 if (message_padding)
3779 *message_padding = header_len - pos;
3785 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
3786 * indicating to the loader how many bytes of the buffer were filled
3787 * in. This function must always be called, even if no bytes were
3788 * successfully read.
3790 * @param loader the loader.
3791 * @param buffer the buffer.
3792 * @param bytes_read number of bytes that were read into the buffer.
3795 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
3799 _dbus_assert (loader->buffer_outstanding);
3800 _dbus_assert (buffer == &loader->data);
3802 loader->buffer_outstanding = FALSE;
3806 * Converts buffered data into messages.
3808 * @param loader the loader.
3809 * @returns #TRUE if we had enough memory to finish.
3812 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
3814 if (loader->corrupted)
3817 while (_dbus_string_get_length (&loader->data) >= 16)
3819 DBusMessage *message;
3820 const char *header_data;
3821 int byte_order, header_len, body_len, header_padding;
3822 dbus_uint32_t header_len_unsigned, body_len_unsigned;
3824 header_data = _dbus_string_get_const_data_len (&loader->data, 0, 16);
3826 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
3828 if (header_data[2] != DBUS_MAJOR_PROTOCOL_VERSION)
3830 _dbus_verbose ("Message has protocol version %d ours is %d\n",
3831 (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
3832 loader->corrupted = TRUE;
3836 byte_order = header_data[0];
3838 if (byte_order != DBUS_LITTLE_ENDIAN &&
3839 byte_order != DBUS_BIG_ENDIAN)
3841 _dbus_verbose ("Message with bad byte order '%c' received\n",
3843 loader->corrupted = TRUE;
3847 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
3848 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
3850 if (header_len_unsigned < 16)
3852 _dbus_verbose ("Message had broken too-small header length %u\n",
3853 header_len_unsigned);
3854 loader->corrupted = TRUE;
3858 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
3859 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
3861 _dbus_verbose ("Header or body length too large (%u %u)\n",
3862 header_len_unsigned,
3864 loader->corrupted = TRUE;
3868 /* Now that we know the values are in signed range, get
3869 * rid of stupid unsigned, just causes bugs
3871 header_len = header_len_unsigned;
3872 body_len = body_len_unsigned;
3874 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
3877 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
3879 loader->corrupted = TRUE;
3883 if (header_len + body_len > loader->max_message_size)
3885 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %ld\n",
3886 header_len, body_len, loader->max_message_size);
3887 loader->corrupted = TRUE;
3891 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
3893 HeaderField fields[FIELD_LAST];
3898 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len + body_len);
3900 if (!decode_header_data (&loader->data, header_len, byte_order,
3901 fields, &header_padding))
3903 _dbus_verbose ("Header was invalid\n");
3904 loader->corrupted = TRUE;
3908 next_arg = header_len;
3909 while (next_arg < (header_len + body_len))
3912 int prev = next_arg;
3914 if (!_dbus_marshal_validate_type (&loader->data, next_arg,
3917 _dbus_verbose ("invalid typecode at offset %d\n", prev);
3918 loader->corrupted = TRUE;
3922 if (!_dbus_marshal_validate_arg (&loader->data,
3929 _dbus_verbose ("invalid type data at %d, next_arg\n", next_arg);
3930 loader->corrupted = TRUE;
3934 _dbus_assert (next_arg > prev);
3937 if (next_arg > (header_len + body_len))
3939 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
3940 next_arg, header_len, body_len,
3941 header_len + body_len);
3942 loader->corrupted = TRUE;
3946 message = dbus_message_new_empty_header ();
3947 if (message == NULL)
3949 _dbus_verbose ("Failed to allocate empty message\n");
3953 message->byte_order = byte_order;
3954 message->header_padding = header_padding;
3956 /* Copy in the offsets we found */
3958 while (i < FIELD_LAST)
3960 message->header_fields[i] = fields[i];
3964 if (!_dbus_list_append (&loader->messages, message))
3966 _dbus_verbose ("Failed to append new message to loader queue\n");
3967 dbus_message_unref (message);
3971 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
3972 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
3974 _dbus_assert (_dbus_string_get_length (&loader->data) >=
3975 (header_len + body_len));
3977 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
3979 _dbus_verbose ("Failed to move header into new message\n");
3980 _dbus_list_remove_last (&loader->messages, message);
3981 dbus_message_unref (message);
3985 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
3989 _dbus_verbose ("Failed to move body into new message\n");
3991 /* put the header back, we'll try again later */
3992 result = _dbus_string_copy_len (&message->header, 0, header_len,
3994 _dbus_assert (result); /* because DBusString never reallocs smaller */
3996 _dbus_list_remove_last (&loader->messages, message);
3997 dbus_message_unref (message);
4001 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
4002 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
4004 /* Fill in caches */
4005 /* FIXME there's no type check if the field has the wrong type */
4006 message->reply_serial = get_uint_field (message,
4007 FIELD_REPLY_SERIAL);
4008 message->client_serial = get_uint_field (message,
4009 FIELD_CLIENT_SERIAL);
4011 _dbus_verbose ("Loaded message %p\n", message);
4021 * Peeks at first loaded message, returns #NULL if no messages have
4024 * @param loader the loader.
4025 * @returns the next message, or #NULL if none.
4028 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
4030 if (loader->messages)
4031 return loader->messages->data;
4037 * Pops a loaded message (passing ownership of the message
4038 * to the caller). Returns #NULL if no messages have been
4041 * @param loader the loader.
4042 * @returns the next message, or #NULL if none.
4045 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
4047 return _dbus_list_pop_first (&loader->messages);
4051 * Pops a loaded message inside a list link (passing ownership of the
4052 * message and link to the caller). Returns #NULL if no messages have
4055 * @param loader the loader.
4056 * @returns the next message link, or #NULL if none.
4059 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
4061 return _dbus_list_pop_first_link (&loader->messages);
4065 * Returns a popped message link, used to undo a pop.
4067 * @param loader the loader
4068 * @param link the link with a message in it
4071 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
4074 _dbus_list_prepend_link (&loader->messages, link);
4078 * Checks whether the loader is confused due to bad data.
4079 * If messages are received that are invalid, the
4080 * loader gets confused and gives up permanently.
4081 * This state is called "corrupted."
4083 * @param loader the loader
4084 * @returns #TRUE if the loader is hosed.
4087 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
4089 return loader->corrupted;
4093 * Sets the maximum size message we allow.
4095 * @param loader the loader
4096 * @param size the max message size in bytes
4099 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
4102 if (size > MAX_SANE_MESSAGE_SIZE)
4104 _dbus_verbose ("clamping requested max message size %ld to %d\n",
4105 size, MAX_SANE_MESSAGE_SIZE);
4106 size = MAX_SANE_MESSAGE_SIZE;
4108 loader->max_message_size = size;
4112 * Gets the maximum allowed message size in bytes.
4114 * @param loader the loader
4115 * @returns max size in bytes
4118 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
4120 return loader->max_message_size;
4124 #ifdef DBUS_BUILD_TESTS
4125 #include "dbus-test.h"
4129 message_iter_test (DBusMessage *message)
4131 DBusMessageIter iter, dict, array, array2;
4133 unsigned char *data;
4134 dbus_int32_t *our_int_array;
4137 dbus_message_iter_init (message, &iter);
4140 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
4141 _dbus_assert_not_reached ("Argument type isn't string");
4143 str = dbus_message_iter_get_string (&iter);
4144 if (strcmp (str, "Test string") != 0)
4145 _dbus_assert_not_reached ("Strings differ");
4148 if (!dbus_message_iter_next (&iter))
4149 _dbus_assert_not_reached ("Reached end of arguments");
4151 /* Signed integer tests */
4152 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32)
4153 _dbus_assert_not_reached ("Argument type isn't int32");
4155 if (dbus_message_iter_get_int32 (&iter) != -0x12345678)
4156 _dbus_assert_not_reached ("Signed integers differ");
4158 if (!dbus_message_iter_next (&iter))
4159 _dbus_assert_not_reached ("Reached end of fields");
4161 /* Unsigned integer tests */
4162 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
4163 _dbus_assert_not_reached ("Argument type isn't int32");
4165 if (dbus_message_iter_get_uint32 (&iter) != 0xedd1e)
4166 _dbus_assert_not_reached ("Unsigned integers differ");
4168 if (!dbus_message_iter_next (&iter))
4169 _dbus_assert_not_reached ("Reached end of arguments");
4172 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DOUBLE)
4173 _dbus_assert_not_reached ("Argument type isn't double");
4175 if (dbus_message_iter_get_double (&iter) != 3.14159)
4176 _dbus_assert_not_reached ("Doubles differ");
4178 if (!dbus_message_iter_next (&iter))
4179 _dbus_assert_not_reached ("Reached end of arguments");
4181 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
4182 _dbus_assert_not_reached ("Argument type not an array");
4184 if (dbus_message_iter_get_array_type (&iter) != DBUS_TYPE_DOUBLE)
4185 _dbus_assert_not_reached ("Array type not double");
4188 if (!dbus_message_iter_init_array_iterator (&iter, &array, NULL))
4189 _dbus_assert_not_reached ("Array init failed");
4191 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_DOUBLE)
4192 _dbus_assert_not_reached ("Argument type isn't double");
4194 if (dbus_message_iter_get_double (&array) != 1.5)
4195 _dbus_assert_not_reached ("Unsigned integers differ");
4197 if (!dbus_message_iter_next (&array))
4198 _dbus_assert_not_reached ("Reached end of arguments");
4200 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_DOUBLE)
4201 _dbus_assert_not_reached ("Argument type isn't double");
4203 if (dbus_message_iter_get_double (&array) != 2.5)
4204 _dbus_assert_not_reached ("Unsigned integers differ");
4206 if (dbus_message_iter_next (&array))
4207 _dbus_assert_not_reached ("Didn't reach end of arguments");
4209 if (!dbus_message_iter_next (&iter))
4210 _dbus_assert_not_reached ("Reached end of arguments");
4215 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
4216 _dbus_assert_not_reached ("not dict type");
4218 if (!dbus_message_iter_init_dict_iterator (&iter, &dict))
4219 _dbus_assert_not_reached ("dict iter failed");
4221 str = dbus_message_iter_get_dict_key (&dict);
4222 if (str == NULL || strcmp (str, "test") != 0)
4223 _dbus_assert_not_reached ("wrong dict key");
4226 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_UINT32)
4227 _dbus_assert_not_reached ("wrong dict entry type");
4229 if (dbus_message_iter_get_uint32 (&dict) != 0xDEADBEEF)
4230 _dbus_assert_not_reached ("wrong dict entry value");
4232 if (!dbus_message_iter_next (&dict))
4233 _dbus_assert_not_reached ("reached end of dict");
4235 /* array of array of int32 (in dict) */
4237 str = dbus_message_iter_get_dict_key (&dict);
4238 if (str == NULL || strcmp (str, "array") != 0)
4239 _dbus_assert_not_reached ("wrong dict key");
4242 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_ARRAY)
4243 _dbus_assert_not_reached ("Argument type not an array");
4245 if (dbus_message_iter_get_array_type (&dict) != DBUS_TYPE_ARRAY)
4246 _dbus_assert_not_reached ("Array type not array");
4248 if (!dbus_message_iter_init_array_iterator (&dict, &array, NULL))
4249 _dbus_assert_not_reached ("Array init failed");
4251 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_ARRAY)
4252 _dbus_assert_not_reached ("Argument type isn't array");
4254 if (dbus_message_iter_get_array_type (&array) != DBUS_TYPE_INT32)
4255 _dbus_assert_not_reached ("Array type not int32");
4257 if (!dbus_message_iter_init_array_iterator (&array, &array2, NULL))
4258 _dbus_assert_not_reached ("Array init failed");
4260 if (dbus_message_iter_get_arg_type (&array2) != DBUS_TYPE_INT32)
4261 _dbus_assert_not_reached ("Argument type isn't int32");
4263 if (dbus_message_iter_get_int32 (&array2) != 0x12345678)
4264 _dbus_assert_not_reached ("Signed integers differ");
4266 if (!dbus_message_iter_next (&array2))
4267 _dbus_assert_not_reached ("Reached end of arguments");
4269 if (dbus_message_iter_get_int32 (&array2) != 0x23456781)
4270 _dbus_assert_not_reached ("Signed integers differ");
4272 if (dbus_message_iter_next (&array2))
4273 _dbus_assert_not_reached ("Didn't reached end of arguments");
4275 if (!dbus_message_iter_next (&array))
4276 _dbus_assert_not_reached ("Reached end of arguments");
4278 if (dbus_message_iter_get_array_type (&array) != DBUS_TYPE_INT32)
4279 _dbus_assert_not_reached ("Array type not int32");
4281 if (!dbus_message_iter_get_int32_array (&array,
4284 _dbus_assert_not_reached ("couldn't get int32 array");
4286 _dbus_assert (len == 3);
4287 _dbus_assert (our_int_array[0] == 0x34567812 &&
4288 our_int_array[1] == 0x45678123 &&
4289 our_int_array[2] == 0x56781234);
4290 dbus_free (our_int_array);
4292 if (dbus_message_iter_next (&array))
4293 _dbus_assert_not_reached ("Didn't reach end of array");
4295 if (dbus_message_iter_next (&dict))
4296 _dbus_assert_not_reached ("Didn't reach end of dict");
4298 if (!dbus_message_iter_next (&iter))
4299 _dbus_assert_not_reached ("Reached end of arguments");
4301 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_BYTE)
4303 _dbus_warn ("type was: %d\n", dbus_message_iter_get_arg_type (&iter));
4304 _dbus_assert_not_reached ("wrong type after dict (should be byte)");
4307 if (dbus_message_iter_get_byte (&iter) != 0xF0)
4308 _dbus_assert_not_reached ("wrong value after dict");
4311 if (!dbus_message_iter_next (&iter))
4312 _dbus_assert_not_reached ("Reached end of arguments");
4314 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_NIL)
4315 _dbus_assert_not_reached ("not a nil type");
4317 if (!dbus_message_iter_next (&iter))
4318 _dbus_assert_not_reached ("Reached end of arguments");
4320 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_NAMED)
4321 _dbus_assert_not_reached ("wrong type after dict");
4323 if (!dbus_message_iter_get_named (&iter, &str, &data, &len))
4324 _dbus_assert_not_reached ("failed to get named");
4326 _dbus_assert (strcmp (str, "named")==0);
4327 _dbus_assert (len == 5);
4328 _dbus_assert (strcmp (data, "data")==0);
4332 if (dbus_message_iter_next (&iter))
4333 _dbus_assert_not_reached ("Didn't reach end of arguments");
4338 check_message_handling_type (DBusMessageIter *iter,
4341 DBusMessageIter child_iter;
4347 case DBUS_TYPE_BYTE:
4348 dbus_message_iter_get_byte (iter);
4350 case DBUS_TYPE_BOOLEAN:
4351 dbus_message_iter_get_boolean (iter);
4353 case DBUS_TYPE_INT32:
4354 dbus_message_iter_get_int32 (iter);
4356 case DBUS_TYPE_UINT32:
4357 dbus_message_iter_get_uint32 (iter);
4359 case DBUS_TYPE_DOUBLE:
4360 dbus_message_iter_get_double (iter);
4362 case DBUS_TYPE_STRING:
4365 str = dbus_message_iter_get_string (iter);
4368 _dbus_warn ("NULL string in message\n");
4374 case DBUS_TYPE_NAMED:
4377 unsigned char *data;
4380 if (!dbus_message_iter_get_named (iter, &name, &data, &len))
4382 _dbus_warn ("error reading name from named type\n");
4389 case DBUS_TYPE_ARRAY:
4393 if (!dbus_message_iter_init_array_iterator (iter, &child_iter, &array_type))
4395 _dbus_warn ("Failed to init array iterator\n");
4399 while (dbus_message_iter_has_next (&child_iter))
4401 if (!check_message_handling_type (&child_iter, array_type))
4403 _dbus_warn ("error in array element\n");
4407 if (!dbus_message_iter_next (&child_iter))
4412 case DBUS_TYPE_DICT:
4417 if (!dbus_message_iter_init_dict_iterator (iter, &child_iter))
4419 _dbus_warn ("Failed to init dict iterator\n");
4423 while ((entry_type = dbus_message_iter_get_arg_type (&child_iter)) != DBUS_TYPE_INVALID)
4425 key = dbus_message_iter_get_dict_key (&child_iter);
4428 _dbus_warn ("error reading dict key\n");
4433 if (!check_message_handling_type (&child_iter, entry_type))
4435 _dbus_warn ("error in dict value\n");
4439 if (!dbus_message_iter_next (&child_iter))
4446 _dbus_warn ("unknown type %d\n", type);
4455 check_message_handling (DBusMessage *message)
4457 DBusMessageIter iter;
4460 dbus_uint32_t client_serial;
4464 client_serial = dbus_message_get_serial (message);
4466 /* can't use set_serial due to the assertions at the start of it */
4467 set_uint_field (message, FIELD_CLIENT_SERIAL,
4470 if (client_serial != dbus_message_get_serial (message))
4472 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
4476 /* If we implement message_set_arg (message, n, value)
4477 * then we would want to test it here
4480 dbus_message_iter_init (message, &iter);
4481 while ((type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
4483 if (!check_message_handling_type (&iter, type))
4486 if (!dbus_message_iter_next (&iter))
4497 check_have_valid_message (DBusMessageLoader *loader)
4499 DBusMessage *message;
4505 if (!_dbus_message_loader_queue_messages (loader))
4506 _dbus_assert_not_reached ("no memory to queue messages");
4508 if (_dbus_message_loader_get_is_corrupted (loader))
4510 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
4514 message = _dbus_message_loader_pop_message (loader);
4515 if (message == NULL)
4517 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
4521 if (_dbus_string_get_length (&loader->data) > 0)
4523 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
4527 /* Verify that we're able to properly deal with the message.
4528 * For example, this would detect improper handling of messages
4529 * in nonstandard byte order.
4531 if (!check_message_handling (message))
4538 dbus_message_unref (message);
4544 check_invalid_message (DBusMessageLoader *loader)
4550 if (!_dbus_message_loader_queue_messages (loader))
4551 _dbus_assert_not_reached ("no memory to queue messages");
4553 if (!_dbus_message_loader_get_is_corrupted (loader))
4555 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
4566 check_incomplete_message (DBusMessageLoader *loader)
4568 DBusMessage *message;
4574 if (!_dbus_message_loader_queue_messages (loader))
4575 _dbus_assert_not_reached ("no memory to queue messages");
4577 if (_dbus_message_loader_get_is_corrupted (loader))
4579 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
4583 message = _dbus_message_loader_pop_message (loader);
4584 if (message != NULL)
4586 _dbus_warn ("loaded message that was expected to be incomplete\n");
4594 dbus_message_unref (message);
4599 check_loader_results (DBusMessageLoader *loader,
4600 DBusMessageValidity validity)
4602 if (!_dbus_message_loader_queue_messages (loader))
4603 _dbus_assert_not_reached ("no memory to queue messages");
4607 case _DBUS_MESSAGE_VALID:
4608 return check_have_valid_message (loader);
4609 case _DBUS_MESSAGE_INVALID:
4610 return check_invalid_message (loader);
4611 case _DBUS_MESSAGE_INCOMPLETE:
4612 return check_incomplete_message (loader);
4613 case _DBUS_MESSAGE_UNKNOWN:
4617 _dbus_assert_not_reached ("bad DBusMessageValidity");
4623 * Loads the message in the given message file.
4625 * @param filename filename to load
4626 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
4627 * @param data string to load message into
4628 * @returns #TRUE if the message was loaded
4631 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
4643 _dbus_verbose ("Loading raw %s\n", _dbus_string_get_const_data (filename));
4644 dbus_error_init (&error);
4645 if (!_dbus_file_get_contents (data, filename, &error))
4647 _dbus_warn ("Could not load message file %s: %s\n",
4648 _dbus_string_get_const_data (filename),
4650 dbus_error_free (&error);
4656 if (!_dbus_message_data_load (data, filename))
4658 _dbus_warn ("Could not load message file %s\n",
4659 _dbus_string_get_const_data (filename));
4672 * Tries loading the message in the given message file
4673 * and verifies that DBusMessageLoader can handle it.
4675 * @param filename filename to load
4676 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
4677 * @param expected_validity what the message has to be like to return #TRUE
4678 * @returns #TRUE if the message has the expected validity
4681 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
4683 DBusMessageValidity expected_validity)
4690 if (!_dbus_string_init (&data))
4691 _dbus_assert_not_reached ("could not allocate string\n");
4693 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
4697 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
4703 if (_dbus_string_get_length (&data) > 0)
4704 _dbus_verbose_bytes_of_string (&data, 0,
4705 _dbus_string_get_length (&data));
4707 _dbus_warn ("Failed message loader test on %s\n",
4708 _dbus_string_get_const_data (filename));
4711 _dbus_string_free (&data);
4717 * Tries loading the given message data.
4720 * @param data the message data
4721 * @param expected_validity what the message has to be like to return #TRUE
4722 * @returns #TRUE if the message has the expected validity
4725 dbus_internal_do_not_use_try_message_data (const DBusString *data,
4726 DBusMessageValidity expected_validity)
4728 DBusMessageLoader *loader;
4736 /* Write the data one byte at a time */
4738 loader = _dbus_message_loader_new ();
4740 len = _dbus_string_get_length (data);
4741 for (i = 0; i < len; i++)
4745 _dbus_message_loader_get_buffer (loader, &buffer);
4746 _dbus_string_append_byte (buffer,
4747 _dbus_string_get_byte (data, i));
4748 _dbus_message_loader_return_buffer (loader, buffer, 1);
4751 if (!check_loader_results (loader, expected_validity))
4754 _dbus_message_loader_unref (loader);
4757 /* Write the data all at once */
4759 loader = _dbus_message_loader_new ();
4764 _dbus_message_loader_get_buffer (loader, &buffer);
4765 _dbus_string_copy (data, 0, buffer,
4766 _dbus_string_get_length (buffer));
4767 _dbus_message_loader_return_buffer (loader, buffer, 1);
4770 if (!check_loader_results (loader, expected_validity))
4773 _dbus_message_loader_unref (loader);
4776 /* Write the data 2 bytes at a time */
4778 loader = _dbus_message_loader_new ();
4780 len = _dbus_string_get_length (data);
4781 for (i = 0; i < len; i += 2)
4785 _dbus_message_loader_get_buffer (loader, &buffer);
4786 _dbus_string_append_byte (buffer,
4787 _dbus_string_get_byte (data, i));
4789 _dbus_string_append_byte (buffer,
4790 _dbus_string_get_byte (data, i+1));
4791 _dbus_message_loader_return_buffer (loader, buffer, 1);
4794 if (!check_loader_results (loader, expected_validity))
4797 _dbus_message_loader_unref (loader);
4805 _dbus_message_loader_unref (loader);
4811 process_test_subdir (const DBusString *test_base_dir,
4813 DBusMessageValidity validity,
4814 DBusForeachMessageFileFunc function,
4817 DBusString test_directory;
4818 DBusString filename;
4826 if (!_dbus_string_init (&test_directory))
4827 _dbus_assert_not_reached ("didn't allocate test_directory\n");
4829 _dbus_string_init_const (&filename, subdir);
4831 if (!_dbus_string_copy (test_base_dir, 0,
4832 &test_directory, 0))
4833 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
4835 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
4836 _dbus_assert_not_reached ("couldn't allocate full path");
4838 _dbus_string_free (&filename);
4839 if (!_dbus_string_init (&filename))
4840 _dbus_assert_not_reached ("didn't allocate filename string\n");
4842 dbus_error_init (&error);
4843 dir = _dbus_directory_open (&test_directory, &error);
4846 _dbus_warn ("Could not open %s: %s\n",
4847 _dbus_string_get_const_data (&test_directory),
4849 dbus_error_free (&error);
4853 printf ("Testing:\n");
4856 while (_dbus_directory_get_next_file (dir, &filename, &error))
4858 DBusString full_path;
4861 if (!_dbus_string_init (&full_path))
4862 _dbus_assert_not_reached ("couldn't init string");
4864 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
4865 _dbus_assert_not_reached ("couldn't copy dir to full_path");
4867 if (!_dbus_concat_dir_and_file (&full_path, &filename))
4868 _dbus_assert_not_reached ("couldn't concat file to dir");
4870 if (_dbus_string_ends_with_c_str (&filename, ".message"))
4872 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
4876 _dbus_verbose ("Skipping non-.message file %s\n",
4877 _dbus_string_get_const_data (&filename));
4878 _dbus_string_free (&full_path);
4883 _dbus_string_get_const_data (&filename));
4885 _dbus_verbose (" expecting %s\n",
4886 validity == _DBUS_MESSAGE_VALID ? "valid" :
4887 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
4888 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
4890 if (! (*function) (&full_path, is_raw, validity, user_data))
4892 _dbus_string_free (&full_path);
4896 _dbus_string_free (&full_path);
4899 if (dbus_error_is_set (&error))
4901 _dbus_warn ("Could not get next file in %s: %s\n",
4902 _dbus_string_get_const_data (&test_directory),
4904 dbus_error_free (&error);
4913 _dbus_directory_close (dir);
4914 _dbus_string_free (&test_directory);
4915 _dbus_string_free (&filename);
4921 * Runs the given function on every message file in the test suite.
4922 * The function should return #FALSE on test failure or fatal error.
4924 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
4925 * @param func the function to run
4926 * @param user_data data for function
4927 * @returns #FALSE if there's a failure
4930 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
4931 DBusForeachMessageFileFunc func,
4934 DBusString test_directory;
4939 _dbus_string_init_const (&test_directory, test_data_dir);
4941 if (!process_test_subdir (&test_directory, "valid-messages",
4942 _DBUS_MESSAGE_VALID, func, user_data))
4945 if (!process_test_subdir (&test_directory, "invalid-messages",
4946 _DBUS_MESSAGE_INVALID, func, user_data))
4949 if (!process_test_subdir (&test_directory, "incomplete-messages",
4950 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
4957 _dbus_string_free (&test_directory);
4963 verify_test_message (DBusMessage *message)
4965 dbus_int32_t our_int;
4968 dbus_bool_t our_bool;
4969 dbus_int32_t *our_int_array;
4970 dbus_uint32_t our_uint32;
4971 int our_int_array_len;
4972 DBusMessageIter iter, dict;
4975 dbus_message_iter_init (message, &iter);
4977 dbus_error_init (&error);
4978 if (!dbus_message_iter_get_args (&iter, &error,
4979 DBUS_TYPE_INT32, &our_int,
4980 DBUS_TYPE_STRING, &our_str,
4981 DBUS_TYPE_DOUBLE, &our_double,
4982 DBUS_TYPE_BOOLEAN, &our_bool,
4983 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &our_int_array, &our_int_array_len,
4986 _dbus_verbose ("error: %s - %s\n", error.name, (error.message != NULL)?error.message: "no message");
4987 _dbus_assert_not_reached ("Could not get arguments");
4990 if (our_int != -0x12345678)
4991 _dbus_assert_not_reached ("integers differ!");
4993 if (our_double != 3.14159)
4994 _dbus_assert_not_reached ("doubles differ!");
4996 if (strcmp (our_str, "Test string") != 0)
4997 _dbus_assert_not_reached ("strings differ!");
4998 dbus_free (our_str);
5001 _dbus_assert_not_reached ("booleans differ");
5003 if (our_int_array_len != 4 ||
5004 our_int_array[0] != 0x12345678 ||
5005 our_int_array[1] != 0x23456781 ||
5006 our_int_array[2] != 0x34567812 ||
5007 our_int_array[3] != 0x45678123)
5008 _dbus_assert_not_reached ("array differ");
5009 dbus_free (our_int_array);
5011 if (!dbus_message_iter_next (&iter))
5012 _dbus_assert_not_reached ("Reached end of arguments");
5014 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
5015 _dbus_assert_not_reached ("not dict type");
5017 if (!dbus_message_iter_init_dict_iterator (&iter, &dict))
5018 _dbus_assert_not_reached ("dict iter failed");
5020 our_str = dbus_message_iter_get_dict_key (&dict);
5021 if (our_str == NULL || strcmp (our_str, "test") != 0)
5022 _dbus_assert_not_reached ("wrong dict key");
5023 dbus_free (our_str);
5025 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_UINT32)
5027 _dbus_verbose ("dict entry type: %d\n", dbus_message_iter_get_arg_type (&dict));
5028 _dbus_assert_not_reached ("wrong dict entry type");
5031 if ((our_uint32 = dbus_message_iter_get_uint32 (&dict)) != 0xDEADBEEF)
5033 _dbus_verbose ("dict entry val: %x\n", our_uint32);
5034 _dbus_assert_not_reached ("wrong dict entry value");
5037 if (dbus_message_iter_next (&dict))
5038 _dbus_assert_not_reached ("Didn't reach end of dict");
5040 if (!dbus_message_iter_next (&iter))
5041 _dbus_assert_not_reached ("Reached end of arguments");
5043 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
5044 _dbus_assert_not_reached ("wrong type after dict");
5046 if (dbus_message_iter_get_uint32 (&iter) != 0xCAFEBABE)
5047 _dbus_assert_not_reached ("wrong value after dict");
5049 if (dbus_message_iter_next (&iter))
5050 _dbus_assert_not_reached ("Didn't reach end of arguments");
5054 * @ingroup DBusMessageInternals
5055 * Unit test for DBusMessage.
5057 * @returns #TRUE on success.
5060 _dbus_message_test (const char *test_data_dir)
5062 DBusMessage *message;
5063 DBusMessageLoader *loader;
5064 DBusMessageIter iter, child_iter, child_iter2, child_iter3;
5070 const dbus_uint32_t our_int32_array[] = { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
5072 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
5074 /* Test the vararg functions */
5075 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
5076 _dbus_message_set_serial (message, 1);
5077 dbus_message_append_args (message,
5078 DBUS_TYPE_INT32, -0x12345678,
5079 DBUS_TYPE_STRING, "Test string",
5080 DBUS_TYPE_DOUBLE, 3.14159,
5081 DBUS_TYPE_BOOLEAN, TRUE,
5082 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, our_int32_array, 4,
5085 dbus_message_append_iter_init (message, &iter);
5086 dbus_message_iter_append_dict (&iter, &child_iter);
5087 dbus_message_iter_append_dict_key (&child_iter, "test");
5088 dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
5089 dbus_message_iter_append_uint32 (&iter, 0xCAFEBABE);
5091 _dbus_verbose_bytes_of_string (&message->header, 0,
5092 _dbus_string_get_length (&message->header));
5093 _dbus_verbose_bytes_of_string (&message->body, 0,
5094 _dbus_string_get_length (&message->body));
5096 verify_test_message (message);
5098 copy = dbus_message_copy (message);
5100 _dbus_assert (message->client_serial == copy->client_serial);
5101 _dbus_assert (message->reply_serial == copy->reply_serial);
5102 _dbus_assert (message->header_padding == copy->header_padding);
5104 _dbus_assert (_dbus_string_get_length (&message->header) ==
5105 _dbus_string_get_length (©->header));
5107 _dbus_assert (_dbus_string_get_length (&message->body) ==
5108 _dbus_string_get_length (©->body));
5110 verify_test_message (copy);
5112 name1 = dbus_message_get_name (message);
5113 name2 = dbus_message_get_name (copy);
5115 _dbus_assert (strcmp (name1, name2) == 0);
5117 dbus_message_unref (message);
5118 dbus_message_unref (copy);
5120 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
5121 _dbus_message_set_serial (message, 1);
5122 dbus_message_set_reply_serial (message, 0x12345678);
5124 dbus_message_append_iter_init (message, &iter);
5125 dbus_message_iter_append_string (&iter, "Test string");
5126 dbus_message_iter_append_int32 (&iter, -0x12345678);
5127 dbus_message_iter_append_uint32 (&iter, 0xedd1e);
5128 dbus_message_iter_append_double (&iter, 3.14159);
5130 dbus_message_iter_append_array (&iter, &child_iter, DBUS_TYPE_DOUBLE);
5131 dbus_message_iter_append_double (&child_iter, 1.5);
5132 dbus_message_iter_append_double (&child_iter, 2.5);
5135 dbus_message_iter_append_dict (&iter, &child_iter);
5136 dbus_message_iter_append_dict_key (&child_iter, "test");
5137 dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
5139 /* array of array of int32 (in dict) */
5140 dbus_message_iter_append_dict_key (&child_iter, "array");
5141 dbus_message_iter_append_array (&child_iter, &child_iter2, DBUS_TYPE_ARRAY);
5142 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
5143 dbus_message_iter_append_int32 (&child_iter3, 0x12345678);
5144 dbus_message_iter_append_int32 (&child_iter3, 0x23456781);
5145 _dbus_warn ("next call expected to fail with wrong array type\n");
5146 _dbus_assert (!dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_UINT32));
5147 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
5148 dbus_message_iter_append_int32 (&child_iter3, 0x34567812);
5149 dbus_message_iter_append_int32 (&child_iter3, 0x45678123);
5150 dbus_message_iter_append_int32 (&child_iter3, 0x56781234);
5152 dbus_message_iter_append_byte (&iter, 0xF0);
5154 dbus_message_iter_append_nil (&iter);
5156 dbus_message_iter_append_named (&iter, "named",
5159 message_iter_test (message);
5161 /* Message loader test */
5162 _dbus_message_lock (message);
5163 loader = _dbus_message_loader_new ();
5165 /* Write the header data one byte at a time */
5166 data = _dbus_string_get_const_data (&message->header);
5167 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
5171 _dbus_message_loader_get_buffer (loader, &buffer);
5172 _dbus_string_append_byte (buffer, data[i]);
5173 _dbus_message_loader_return_buffer (loader, buffer, 1);
5176 /* Write the body data one byte at a time */
5177 data = _dbus_string_get_const_data (&message->body);
5178 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
5182 _dbus_message_loader_get_buffer (loader, &buffer);
5183 _dbus_string_append_byte (buffer, data[i]);
5184 _dbus_message_loader_return_buffer (loader, buffer, 1);
5187 dbus_message_unref (message);
5189 /* Now pop back the message */
5190 if (!_dbus_message_loader_queue_messages (loader))
5191 _dbus_assert_not_reached ("no memory to queue messages");
5193 if (_dbus_message_loader_get_is_corrupted (loader))
5194 _dbus_assert_not_reached ("message loader corrupted");
5196 message = _dbus_message_loader_pop_message (loader);
5198 _dbus_assert_not_reached ("received a NULL message");
5200 if (dbus_message_get_reply_serial (message) != 0x12345678)
5201 _dbus_assert_not_reached ("reply serial fields differ");
5203 message_iter_test (message);
5205 dbus_message_unref (message);
5206 _dbus_message_loader_unref (loader);
5208 /* Now load every message in test_data_dir if we have one */
5209 if (test_data_dir == NULL)
5212 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
5213 (DBusForeachMessageFileFunc)
5214 dbus_internal_do_not_use_try_message_file,
5218 #endif /* DBUS_BUILD_TESTS */