1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-message.c DBusMessage object
4 * Copyright (C) 2002, 2003, 2004 Red Hat Inc.
5 * Copyright (C) 2002, 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 2.0
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-internals.h"
26 #include "dbus-marshal.h"
27 #include "dbus-message.h"
28 #include "dbus-message-internal.h"
29 #include "dbus-memory.h"
30 #include "dbus-list.h"
31 #include "dbus-message-builder.h"
32 #include "dbus-dataslot.h"
36 * @defgroup DBusMessageInternals DBusMessage implementation details
37 * @ingroup DBusInternals
38 * @brief DBusMessage private implementation details.
40 * The guts of DBusMessage and its methods.
46 * Cached information about a header field in the message
50 int name_offset; /**< Offset to name of field */
51 int value_offset; /**< Offset to value of field */
54 /** Offset to byte order from start of header */
55 #define BYTE_ORDER_OFFSET 0
56 /** Offset to type from start of header */
58 /** Offset to flags from start of header */
59 #define FLAGS_OFFSET 2
60 /** Offset to version from start of header */
61 #define VERSION_OFFSET 3
62 /** Offset to header length from start of header */
63 #define HEADER_LENGTH_OFFSET 4
64 /** Offset to body length from start of header */
65 #define BODY_LENGTH_OFFSET 8
66 /** Offset to client serial from start of header */
67 #define CLIENT_SERIAL_OFFSET 12
71 * @brief Internals of DBusMessage
73 * Object representing a message received from or to be sent to
74 * another application. This is an opaque object, all members
79 DBusAtomic refcount; /**< Reference count */
81 DBusString header; /**< Header network data, stored
82 * separately from body so we can
83 * independently realloc it.
86 HeaderField header_fields[DBUS_HEADER_FIELD_LAST + 1]; /**< Track the location
87 * of each field in "header"
90 dbus_uint32_t client_serial; /**< Cached client serial value for speed */
91 dbus_uint32_t reply_serial; /**< Cached reply serial value for speed */
93 int header_padding; /**< bytes of alignment in header */
95 DBusString body; /**< Body network data. */
97 char byte_order; /**< Message byte order. */
99 DBusList *size_counters; /**< 0-N DBusCounter used to track message size. */
100 long size_counter_delta; /**< Size we incremented the size counters by. */
102 dbus_uint32_t changed_stamp; /**< Incremented when iterators are invalidated. */
104 unsigned int locked : 1; /**< Message being sent, no modifications allowed. */
106 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
110 DBUS_MESSAGE_ITER_TYPE_MESSAGE,
111 DBUS_MESSAGE_ITER_TYPE_ARRAY,
112 DBUS_MESSAGE_ITER_TYPE_DICT
115 /** typedef for internals of message iterator */
116 typedef struct DBusMessageRealIter DBusMessageRealIter;
119 * @brief Internals of DBusMessageIter
121 * Object representing a position in a message. All fields are internal.
123 struct DBusMessageRealIter
125 DBusMessageRealIter *parent_iter; /**< parent iter, or NULL */
126 DBusMessage *message; /**< Message used */
127 dbus_uint32_t changed_stamp; /**< stamp to detect invalid iters */
129 /* This is an int instead of an enum to get a guaranteed size for the dummy: */
130 int type; /**< type of iter */
132 int pos; /**< Current position in the string */
133 int end; /**< position right after the container */
134 int container_start; /**< offset of the start of the container */
135 int container_length_pos; /**< offset of the length of the container */
137 int wrote_dict_key; /**< whether we wrote the dict key for the current dict element */
139 int array_type_pos; /**< pointer to the position of the array element type */
140 int array_type_done; /**< TRUE if the array type is fully finished */
144 * Gets the data to be sent over the network for this message.
145 * The header and then the body should be written out.
146 * This function is guaranteed to always return the same
147 * data once a message is locked (with _dbus_message_lock()).
149 * @param message the message.
150 * @param header return location for message header data.
151 * @param body return location for message body data.
154 _dbus_message_get_network_data (DBusMessage *message,
155 const DBusString **header,
156 const DBusString **body)
158 _dbus_assert (message->locked);
160 *header = &message->header;
161 *body = &message->body;
165 clear_header_padding (DBusMessage *message)
167 _dbus_string_shorten (&message->header,
168 message->header_padding);
169 message->header_padding = 0;
172 #ifdef DBUS_DISABLE_CHECKS
173 #define is_valid_error_name(x) TRUE
176 is_valid_error_name (const char *error_name)
178 DBusString the_error_name;
180 if (error_name == NULL)
183 _dbus_string_init_const (&the_error_name, error_name);
184 return _dbus_string_validate_error_name (&the_error_name, 0,
185 _dbus_string_get_length (&the_error_name));
190 append_header_padding (DBusMessage *message)
193 old_len = _dbus_string_get_length (&message->header);
194 if (!_dbus_string_align_length (&message->header, 8))
197 message->header_padding = _dbus_string_get_length (&message->header) - old_len;
202 #ifdef DBUS_BUILD_TESTS
203 /* tests-only until it's actually used */
205 get_int_field (DBusMessage *message,
210 _dbus_assert (field <= DBUS_HEADER_FIELD_LAST);
212 offset = message->header_fields[field].value_offset;
215 return -1; /* useless if -1 is a valid value of course */
217 return _dbus_demarshal_int32 (&message->header,
225 get_uint_field (DBusMessage *message,
230 _dbus_assert (field <= DBUS_HEADER_FIELD_LAST);
232 offset = message->header_fields[field].value_offset;
235 return 0; /* useless if 0 is a valid value of course */
237 return _dbus_demarshal_uint32 (&message->header,
244 get_string_field (DBusMessage *message,
251 offset = message->header_fields[field].value_offset;
253 _dbus_assert (field <= DBUS_HEADER_FIELD_LAST);
258 /* offset points to string length, string data follows it */
259 /* FIXME _dbus_demarshal_const_string() that returned
260 * a reference to the string plus its len might be nice.
264 *len = _dbus_demarshal_uint32 (&message->header,
269 data = _dbus_string_get_const_data (&message->header);
271 return data + (offset + 4);
274 /* returns FALSE if no memory, TRUE with NULL path if no field */
276 get_path_field_decomposed (DBusMessage *message,
282 offset = message->header_fields[field].value_offset;
284 _dbus_assert (field <= DBUS_HEADER_FIELD_LAST);
292 return _dbus_demarshal_object_path (&message->header,
299 #ifdef DBUS_BUILD_TESTS
301 append_int_field (DBusMessage *message,
305 _dbus_assert (!message->locked);
307 clear_header_padding (message);
309 message->header_fields[field].name_offset =
310 _dbus_string_get_length (&message->header);
312 if (!_dbus_string_append_byte (&message->header, field))
315 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_INT32))
318 if (!_dbus_string_align_length (&message->header, 4))
321 message->header_fields[field].value_offset =
322 _dbus_string_get_length (&message->header);
324 if (!_dbus_marshal_int32 (&message->header, message->byte_order,
328 if (!append_header_padding (message))
334 _dbus_string_set_length (&message->header,
335 message->header_fields[field].name_offset);
336 message->header_fields[field].name_offset = -1;
337 message->header_fields[field].value_offset = -1;
339 /* this must succeed because it was allocated on function entry and
340 * DBusString doesn't ever realloc smaller
342 if (!append_header_padding (message))
343 _dbus_assert_not_reached ("failed to reappend header padding");
349 append_uint_field (DBusMessage *message,
353 _dbus_assert (!message->locked);
355 clear_header_padding (message);
357 message->header_fields[field].name_offset =
358 _dbus_string_get_length (&message->header);
360 if (!_dbus_string_append_byte (&message->header, field))
363 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_UINT32))
366 if (!_dbus_string_align_length (&message->header, 4))
369 message->header_fields[field].value_offset =
370 _dbus_string_get_length (&message->header);
372 if (!_dbus_marshal_uint32 (&message->header, message->byte_order,
376 if (!append_header_padding (message))
382 _dbus_string_set_length (&message->header,
383 message->header_fields[field].name_offset);
384 message->header_fields[field].name_offset = -1;
385 message->header_fields[field].value_offset = -1;
387 /* this must succeed because it was allocated on function entry and
388 * DBusString doesn't ever realloc smaller
390 if (!append_header_padding (message))
391 _dbus_assert_not_reached ("failed to reappend header padding");
395 /** The maximum number of bytes of overhead to append to a string
396 * (fieldcode + typecode + alignment + length + nul + headerpadding)
398 #define MAX_BYTES_OVERHEAD_TO_APPEND_A_STRING (1 + 1 + 3 + 4 + 1 + 8)
401 append_string_field_len (DBusMessage *message,
407 _dbus_assert (!message->locked);
409 clear_header_padding (message);
411 message->header_fields[field].name_offset =
412 _dbus_string_get_length (&message->header);
414 if (!_dbus_string_append_byte (&message->header, field))
417 if (!_dbus_string_append_byte (&message->header, type))
420 if (!_dbus_string_align_length (&message->header, 4))
423 message->header_fields[field].value_offset =
424 _dbus_string_get_length (&message->header);
426 if (!_dbus_marshal_string_len (&message->header, message->byte_order,
430 if (!append_header_padding (message))
436 _dbus_string_set_length (&message->header,
437 message->header_fields[field].name_offset);
438 message->header_fields[field].name_offset = -1;
439 message->header_fields[field].value_offset = -1;
441 /* this must succeed because it was allocated on function entry and
442 * DBusString doesn't ever realloc smaller
444 if (!append_header_padding (message))
445 _dbus_assert_not_reached ("failed to reappend header padding");
451 append_string_field (DBusMessage *message,
458 value_len = strlen (value);
460 return append_string_field_len (message, field, type, value, value_len);
464 get_type_alignment (int type)
472 case DBUS_TYPE_BOOLEAN:
476 case DBUS_TYPE_INT32:
477 case DBUS_TYPE_UINT32:
478 case DBUS_TYPE_STRING:
479 case DBUS_TYPE_OBJECT_PATH:
480 /* These are aligned 4 because they have a length as the
483 case DBUS_TYPE_CUSTOM:
488 case DBUS_TYPE_INT64:
489 case DBUS_TYPE_UINT64:
490 case DBUS_TYPE_DOUBLE:
494 case DBUS_TYPE_ARRAY:
495 _dbus_assert_not_reached ("passed an ARRAY type to get_type_alignment()");
498 case DBUS_TYPE_INVALID:
500 _dbus_assert_not_reached ("passed an invalid or unknown type to get_type_alignment()");
508 iterate_one_field (const DBusString *str,
513 DBusString *append_copy_to,
514 int *copy_name_offset_p,
515 int *copy_value_offset_p)
517 int name, type, array_type;
526 _dbus_verbose ("%s: name_offset=%d, append to %p\n",
527 _DBUS_FUNCTION_NAME, name_offset, append_copy_to);
531 name = _dbus_string_get_byte (str, name_offset);
535 type = _dbus_string_get_byte (str, type_pos);
540 /* find out the type of our array */
541 while (array_type == DBUS_TYPE_ARRAY)
545 array_type = _dbus_string_get_byte (str, pos);
548 _dbus_verbose ("%s: name %d, type '%c' %d at %d len %d, array type '%c' %d\n",
550 name, type, type, type_pos, type_len, array_type, array_type);
552 #ifndef DBUS_DISABLE_ASSERT
553 if (!_dbus_type_is_valid (array_type))
555 _dbus_warn ("type '%c' %d is not valid in %s\n",
556 array_type, array_type, _DBUS_FUNCTION_NAME);
557 _dbus_assert_not_reached ("invalid type");
561 alignment = get_type_alignment (array_type);
564 pos = _DBUS_ALIGN_VALUE (pos, alignment);
566 _dbus_verbose ("%s: alignment %d value at pos %d\n",
567 _DBUS_FUNCTION_NAME, alignment, pos);
569 /* pos now points to our value */
570 if (!_dbus_marshal_get_arg_end_pos (str, byte_order,
571 type, pos, &value_end))
572 _dbus_assert_not_reached ("failed to get the byte after this header");
575 value_len = value_end - value_pos;
577 _dbus_verbose ("%s: value_pos %d value_len %d value_end %d\n",
578 _DBUS_FUNCTION_NAME, value_pos, value_len, value_end);
581 *next_offset_p = pos + value_len;
584 *field_name_p = name;
590 orig_len = _dbus_string_get_length (append_copy_to);
592 if (copy_name_offset_p)
593 *copy_name_offset_p = orig_len;
595 if (!_dbus_string_append_byte (append_copy_to, name))
598 if (!_dbus_string_copy_len (str, type_pos, type_len,
600 _dbus_string_get_length (append_copy_to)))
603 if (!_dbus_string_align_length (append_copy_to, alignment))
606 if (copy_value_offset_p)
607 *copy_value_offset_p = _dbus_string_get_length (append_copy_to);
609 if (!_dbus_string_copy_len (str, value_pos, value_len,
611 _dbus_string_get_length (append_copy_to)))
617 _dbus_verbose ("%s: Failed copying old fields to new string\n",
618 _DBUS_FUNCTION_NAME);
619 _dbus_string_set_length (append_copy_to, orig_len);
626 #ifndef DBUS_DISABLE_ASSERT
628 verify_header_fields (DBusMessage *message)
632 while (i < DBUS_HEADER_FIELD_LAST)
634 if (message->header_fields[i].name_offset >= 0)
635 _dbus_assert (_dbus_string_get_byte (&message->header,
636 message->header_fields[i].name_offset) ==
641 #else /* DBUS_DISABLE_ASSERT */
642 #define verify_header_fields(x)
643 #endif /* DBUS_DISABLE_ASSERT */
645 /* In this function we delete one field and re-align all the fields
649 delete_one_and_re_align (DBusMessage *message,
650 int name_offset_to_delete)
653 int new_fields_front_padding;
657 HeaderField new_header_fields[DBUS_HEADER_FIELD_LAST];
659 _dbus_assert (name_offset_to_delete < _dbus_string_get_length (&message->header));
660 verify_header_fields (message);
662 _dbus_verbose ("%s: Deleting one field at offset %d\n",
664 name_offset_to_delete);
668 clear_header_padding (message);
670 if (!_dbus_string_init_preallocated (©,
671 _dbus_string_get_length (&message->header) -
672 name_offset_to_delete + 8))
674 _dbus_verbose ("%s: Failed to init string to hold copy of fields\n",
675 _DBUS_FUNCTION_NAME);
679 /* Align the name offset of the first field in the same way it's
680 * aligned in the real header
682 new_fields_front_padding = name_offset_to_delete % 8;
684 if (!_dbus_string_insert_bytes (©, 0, new_fields_front_padding,
686 _dbus_assert_not_reached ("Should not have failed to insert bytes into preallocated string\n");
688 memcpy (new_header_fields, message->header_fields,
689 sizeof (new_header_fields));
691 /* Now just re-marshal each field in the header to our temporary
692 * buffer, skipping the first one. The tricky part is that the
693 * fields are padded as if for previous_name_offset, but are in fact
694 * at unaligned_name_offset
697 if (!iterate_one_field (&message->header,
699 name_offset_to_delete,
701 &field_name, NULL, NULL, NULL))
702 _dbus_assert_not_reached ("shouldn't have failed to alloc memory to skip the deleted field");
704 if (field_name < DBUS_HEADER_FIELD_LAST)
706 new_header_fields[field_name].name_offset = -1;
707 new_header_fields[field_name].value_offset = -1;
710 while (next_offset < _dbus_string_get_length (&message->header))
712 int copy_name_offset;
713 int copy_value_offset;
715 if (!iterate_one_field (&message->header,
724 _dbus_verbose ("%s: OOM iterating one field\n",
725 _DBUS_FUNCTION_NAME);
729 if (field_name < DBUS_HEADER_FIELD_LAST)
731 new_header_fields[field_name].name_offset = copy_name_offset - new_fields_front_padding + name_offset_to_delete;
732 new_header_fields[field_name].value_offset = copy_value_offset - new_fields_front_padding + name_offset_to_delete;
736 if (!_dbus_string_replace_len (©,
737 new_fields_front_padding,
738 _dbus_string_get_length (©) - new_fields_front_padding,
740 name_offset_to_delete,
741 _dbus_string_get_length (&message->header) - name_offset_to_delete))
743 _dbus_verbose ("%s: OOM moving copy back into header\n",
744 _DBUS_FUNCTION_NAME);
748 memcpy (message->header_fields, new_header_fields,
749 sizeof (new_header_fields));
750 verify_header_fields (message);
755 _dbus_string_free (©);
758 if (!append_header_padding (message))
759 _dbus_assert_not_reached ("Failed to re-append header padding in re_align_field_recurse()");
765 delete_field (DBusMessage *message,
767 int prealloc_header_space)
771 _dbus_assert (!message->locked);
774 if (!_dbus_string_lengthen (&message->header, prealloc_header_space))
776 _dbus_verbose ("failed to prealloc %d bytes header space\n",
777 prealloc_header_space);
780 _dbus_string_shorten (&message->header, prealloc_header_space);
783 offset = message->header_fields[field].name_offset;
786 _dbus_verbose ("header field didn't exist, no need to delete\n");
787 return TRUE; /* field didn't exist */
790 return delete_one_and_re_align (message, offset);
793 #ifdef DBUS_BUILD_TESTS
795 set_int_field (DBusMessage *message,
799 int offset = message->header_fields[field].value_offset;
801 _dbus_assert (!message->locked);
805 /* need to append the field */
806 return append_int_field (message, field, value);
810 _dbus_marshal_set_int32 (&message->header,
820 set_uint_field (DBusMessage *message,
824 int offset = message->header_fields[field].value_offset;
826 _dbus_assert (!message->locked);
830 /* need to append the field */
831 return append_uint_field (message, field, value);
835 _dbus_marshal_set_uint32 (&message->header,
844 set_string_field (DBusMessage *message,
852 _dbus_assert (!message->locked);
854 value_len = value ? strlen (value) : 0;
856 /* the prealloc is so the append_string_field()
857 * below won't fail, leaving us in inconsistent state
859 prealloc = value_len + MAX_BYTES_OVERHEAD_TO_APPEND_A_STRING;
861 _dbus_verbose ("set_string_field() field %d prealloc %d\n",
864 if (!delete_field (message, field, prealloc))
869 /* need to append the field */
870 if (!append_string_field_len (message, field, type, value, value_len))
871 _dbus_assert_not_reached ("Appending string field shouldn't have failed, due to preallocation");
878 * Sets the serial number of a message.
879 * This can only be done once on a message.
881 * @param message the message
882 * @param serial the serial
885 _dbus_message_set_serial (DBusMessage *message,
888 _dbus_assert (!message->locked);
889 _dbus_assert (dbus_message_get_serial (message) == 0);
891 _dbus_marshal_set_uint32 (&message->header,
893 CLIENT_SERIAL_OFFSET,
896 message->client_serial = serial;
900 * Sets the reply serial of a message (the client serial
901 * of the message this is a reply to).
903 * @param message the message
904 * @param reply_serial the client serial
905 * @returns #FALSE if not enough memory
908 dbus_message_set_reply_serial (DBusMessage *message,
909 dbus_uint32_t reply_serial)
911 _dbus_assert (!message->locked);
913 if (set_uint_field (message,
914 DBUS_HEADER_FIELD_REPLY_SERIAL,
917 message->reply_serial = reply_serial;
925 * Returns the serial of a message or 0 if none has been specified.
926 * The message's serial number is provided by the application sending
927 * the message and is used to identify replies to this message. All
928 * messages received on a connection will have a serial, but messages
929 * you haven't sent yet may return 0.
931 * @param message the message
932 * @returns the client serial
935 dbus_message_get_serial (DBusMessage *message)
937 return message->client_serial;
941 * Returns the serial that the message is a reply to or 0 if none.
943 * @param message the message
944 * @returns the reply serial
947 dbus_message_get_reply_serial (DBusMessage *message)
949 return message->reply_serial;
953 * Adds a counter to be incremented immediately with the
954 * size of this message, and decremented by the size
955 * of this message when this message if finalized.
956 * The link contains a counter with its refcount already
957 * incremented, but the counter itself not incremented.
958 * Ownership of link and counter refcount is passed to
961 * @param message the message
962 * @param link link with counter as data
965 _dbus_message_add_size_counter_link (DBusMessage *message,
968 /* right now we don't recompute the delta when message
969 * size changes, and that's OK for current purposes
970 * I think, but could be important to change later.
971 * Do recompute it whenever there are no outstanding counters,
972 * since it's basically free.
974 if (message->size_counters == NULL)
976 message->size_counter_delta =
977 _dbus_string_get_length (&message->header) +
978 _dbus_string_get_length (&message->body);
981 _dbus_verbose ("message has size %ld\n",
982 message->size_counter_delta);
986 _dbus_list_append_link (&message->size_counters, link);
988 _dbus_counter_adjust (link->data, message->size_counter_delta);
992 * Adds a counter to be incremented immediately with the
993 * size of this message, and decremented by the size
994 * of this message when this message if finalized.
996 * @param message the message
997 * @param counter the counter
998 * @returns #FALSE if no memory
1001 _dbus_message_add_size_counter (DBusMessage *message,
1002 DBusCounter *counter)
1006 link = _dbus_list_alloc_link (counter);
1010 _dbus_counter_ref (counter);
1011 _dbus_message_add_size_counter_link (message, link);
1017 * Removes a counter tracking the size of this message, and decrements
1018 * the counter by the size of this message.
1020 * @param message the message
1021 * @param link_return return the link used
1022 * @param counter the counter
1025 _dbus_message_remove_size_counter (DBusMessage *message,
1026 DBusCounter *counter,
1027 DBusList **link_return)
1031 link = _dbus_list_find_last (&message->size_counters,
1033 _dbus_assert (link != NULL);
1035 _dbus_list_unlink (&message->size_counters,
1038 *link_return = link;
1040 _dbus_list_free_link (link);
1042 _dbus_counter_adjust (counter, - message->size_counter_delta);
1044 _dbus_counter_unref (counter);
1048 dbus_message_create_header (DBusMessage *message,
1050 const char *service,
1052 const char *interface,
1054 const char *error_name)
1058 _dbus_assert ((interface && member) ||
1060 !(interface || member || error_name));
1061 _dbus_assert (error_name == NULL || is_valid_error_name (error_name));
1063 if (!_dbus_string_append_byte (&message->header, message->byte_order))
1066 if (!_dbus_string_append_byte (&message->header, type))
1070 if (!_dbus_string_append_byte (&message->header, flags))
1073 if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
1077 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
1081 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
1085 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
1088 /* Marshal all the fields (Marshall Fields?) */
1092 if (!append_string_field (message,
1093 DBUS_HEADER_FIELD_PATH,
1094 DBUS_TYPE_OBJECT_PATH,
1099 if (service != NULL)
1101 if (!append_string_field (message,
1102 DBUS_HEADER_FIELD_DESTINATION,
1108 if (interface != NULL)
1110 if (!append_string_field (message,
1111 DBUS_HEADER_FIELD_INTERFACE,
1119 if (!append_string_field (message,
1120 DBUS_HEADER_FIELD_MEMBER,
1126 if (error_name != NULL)
1128 if (!append_string_field (message,
1129 DBUS_HEADER_FIELD_ERROR_NAME,
1135 /* @todo if we make signature optional when body is empty, we don't
1136 * need to do this here.
1138 if (!append_string_field (message,
1139 DBUS_HEADER_FIELD_SIGNATURE,
1148 * Locks a message. Allows checking that applications don't keep a
1149 * reference to a message in the outgoing queue and change it
1150 * underneath us. Messages are locked when they enter the outgoing
1151 * queue (dbus_connection_send_message()), and the library complains
1152 * if the message is modified while locked.
1154 * @param message the message to lock.
1157 _dbus_message_lock (DBusMessage *message)
1159 if (!message->locked)
1161 /* Fill in our lengths */
1162 _dbus_marshal_set_uint32 (&message->header,
1163 message->byte_order,
1164 HEADER_LENGTH_OFFSET,
1165 _dbus_string_get_length (&message->header));
1167 _dbus_marshal_set_uint32 (&message->header,
1168 message->byte_order,
1170 _dbus_string_get_length (&message->body));
1172 message->locked = TRUE;
1179 * @defgroup DBusMessage DBusMessage
1181 * @brief Message to be sent or received over a DBusConnection.
1183 * A DBusMessage is the most basic unit of communication over a
1184 * DBusConnection. A DBusConnection represents a stream of messages
1185 * received from a remote application, and a stream of messages
1186 * sent to a remote application.
1192 * Sets the signature of the message, i.e. the arguments in the
1193 * message payload. The signature includes only "in" arguments for
1194 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
1195 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
1196 * what you might expect (it does not include the signature of the
1197 * entire C++-style method).
1199 * The signature is a string made up of type codes such
1200 * as #DBUS_TYPE_STRING. The string is terminated with nul
1201 * (nul is also the value of #DBUS_TYPE_INVALID).
1203 * @param message the message
1204 * @param signature the type signature or #NULL to unset
1205 * @returns #FALSE if no memory
1208 dbus_message_set_signature (DBusMessage *message,
1209 const char *signature)
1211 _dbus_return_val_if_fail (message != NULL, FALSE);
1212 _dbus_return_val_if_fail (!message->locked, FALSE);
1214 return set_string_field (message,
1215 DBUS_HEADER_FIELD_SIGNATURE,
1221 * Appends to the signature of the message.
1222 * (currently a static function, maybe should be public?)
1224 * @param message the message
1225 * @param append_bytes nul-terminated bytes to append to the type signature
1226 * @returns #FALSE if no memory
1229 dbus_message_append_to_signature (DBusMessage *message,
1230 const char *append_bytes)
1232 const char *signature;
1233 DBusString append_str;
1236 _dbus_return_val_if_fail (append_bytes != NULL, FALSE);
1237 _dbus_return_val_if_fail (message != NULL, FALSE);
1238 _dbus_return_val_if_fail (!message->locked, FALSE);
1242 /* FIXME Just really inefficient for the moment; later we could
1243 * speed it up a lot by poking more directly at the header data
1245 signature = dbus_message_get_signature (message);
1247 if (!_dbus_string_init (&append_str))
1250 if (signature && !_dbus_string_append (&append_str, signature))
1253 if (!_dbus_string_append (&append_str, append_bytes))
1256 if (!set_string_field (message,
1257 DBUS_HEADER_FIELD_SIGNATURE,
1259 _dbus_string_get_const_data (&append_str)))
1266 _dbus_string_free (&append_str);
1272 * Appends one byte to the signature of the message.
1273 * Internal function.
1275 * @param message the message
1276 * @param append_byte the byte
1277 * @returns #FALSE if no memory
1280 _dbus_message_append_byte_to_signature (DBusMessage *message,
1281 unsigned char append_byte)
1285 _dbus_return_val_if_fail (message != NULL, FALSE);
1286 _dbus_return_val_if_fail (!message->locked, FALSE);
1288 buf[0] = append_byte;
1291 return dbus_message_append_to_signature (message, buf);
1295 * Removes the last byte from the signature of the message.
1296 * Internal function.
1298 * @param message the message
1301 _dbus_message_remove_byte_from_signature (DBusMessage *message)
1303 const char *signature;
1305 _dbus_return_if_fail (message != NULL);
1306 _dbus_return_if_fail (!message->locked);
1308 signature = dbus_message_get_signature (message);
1310 _dbus_return_if_fail (signature != NULL);
1312 if (!delete_field (message,
1313 DBUS_HEADER_FIELD_SIGNATURE,
1315 _dbus_assert_not_reached ("failed to delete signature field");
1317 /* reappend one shorter (could this be any less efficient? the code will
1318 * go away later anyhow)
1320 if (!append_string_field_len (message, DBUS_HEADER_FIELD_SIGNATURE,
1321 DBUS_TYPE_STRING, signature,
1322 strlen (signature) - 1))
1323 _dbus_assert_not_reached ("reappending shorter signature shouldn't have failed");
1327 * @typedef DBusMessage
1329 * Opaque data type representing a message received from or to be
1330 * sent to another application.
1334 dbus_message_new_empty_header (void)
1336 DBusMessage *message;
1339 message = dbus_new0 (DBusMessage, 1);
1340 if (message == NULL)
1343 message->refcount.value = 1;
1344 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
1345 message->client_serial = 0;
1346 message->reply_serial = 0;
1348 _dbus_data_slot_list_init (&message->slot_list);
1351 while (i <= DBUS_HEADER_FIELD_LAST)
1353 message->header_fields[i].name_offset = -1;
1354 message->header_fields[i].value_offset = -1;
1358 if (!_dbus_string_init_preallocated (&message->header, 64))
1360 dbus_free (message);
1364 if (!_dbus_string_init_preallocated (&message->body, 64))
1366 _dbus_string_free (&message->header);
1367 dbus_free (message);
1375 * Constructs a new message of the given message type.
1376 * Types include #DBUS_MESSAGE_TYPE_METHOD_CALL,
1377 * #DBUS_MESSAGE_TYPE_SIGNAL, and so forth.
1379 * @param message_type type of message
1380 * @returns new message or #NULL If no memory
1383 dbus_message_new (int message_type)
1385 DBusMessage *message;
1387 _dbus_return_val_if_fail (message_type != DBUS_MESSAGE_TYPE_INVALID, NULL);
1389 message = dbus_message_new_empty_header ();
1390 if (message == NULL)
1393 if (!dbus_message_create_header (message,
1395 NULL, NULL, NULL, NULL, NULL))
1397 dbus_message_unref (message);
1405 * Constructs a new message to invoke a method on a remote
1406 * object. Returns #NULL if memory can't be allocated for the
1407 * message. The service may be #NULL in which case no service is set;
1408 * this is appropriate when using D-BUS in a peer-to-peer context (no
1409 * message bus). The interface may be #NULL, which means that
1410 * if multiple methods with the given name exist it is undefined
1411 * which one will be invoked.
1413 * @param service service that the message should be sent to or #NULL
1414 * @param path object path the message should be sent to
1415 * @param interface interface to invoke method on
1416 * @param method method to invoke
1418 * @returns a new DBusMessage, free with dbus_message_unref()
1419 * @see dbus_message_unref()
1422 dbus_message_new_method_call (const char *service,
1424 const char *interface,
1427 DBusMessage *message;
1429 _dbus_return_val_if_fail (path != NULL, NULL);
1430 _dbus_return_val_if_fail (method != NULL, NULL);
1432 message = dbus_message_new_empty_header ();
1433 if (message == NULL)
1436 if (!dbus_message_create_header (message,
1437 DBUS_MESSAGE_TYPE_METHOD_CALL,
1438 service, path, interface, method, NULL))
1440 dbus_message_unref (message);
1448 * Constructs a message that is a reply to a method call. Returns
1449 * #NULL if memory can't be allocated for the message.
1451 * @param method_call the message which the created
1452 * message is a reply to.
1453 * @returns a new DBusMessage, free with dbus_message_unref()
1454 * @see dbus_message_new_method_call(), dbus_message_unref()
1457 dbus_message_new_method_return (DBusMessage *method_call)
1459 DBusMessage *message;
1462 _dbus_return_val_if_fail (method_call != NULL, NULL);
1464 sender = get_string_field (method_call,
1465 DBUS_HEADER_FIELD_SENDER,
1468 /* sender is allowed to be null here in peer-to-peer case */
1470 message = dbus_message_new_empty_header ();
1471 if (message == NULL)
1474 if (!dbus_message_create_header (message,
1475 DBUS_MESSAGE_TYPE_METHOD_RETURN,
1476 sender, NULL, NULL, NULL, NULL))
1478 dbus_message_unref (message);
1482 dbus_message_set_no_reply (message, TRUE);
1484 if (!dbus_message_set_reply_serial (message,
1485 dbus_message_get_serial (method_call)))
1487 dbus_message_unref (message);
1495 * Constructs a new message representing a signal emission. Returns
1496 * #NULL if memory can't be allocated for the message.
1497 * A signal is identified by its originating interface, and
1498 * the name of the signal.
1500 * @param path the path to the object emitting the signal
1501 * @param interface the interface the signal is emitted from
1502 * @param name name of the signal
1503 * @returns a new DBusMessage, free with dbus_message_unref()
1504 * @see dbus_message_unref()
1507 dbus_message_new_signal (const char *path,
1508 const char *interface,
1511 DBusMessage *message;
1513 _dbus_return_val_if_fail (path != NULL, NULL);
1514 _dbus_return_val_if_fail (interface != NULL, NULL);
1515 _dbus_return_val_if_fail (name != NULL, NULL);
1517 message = dbus_message_new_empty_header ();
1518 if (message == NULL)
1521 if (!dbus_message_create_header (message,
1522 DBUS_MESSAGE_TYPE_SIGNAL,
1523 NULL, path, interface, name, NULL))
1525 dbus_message_unref (message);
1529 dbus_message_set_no_reply (message, TRUE);
1535 * Creates a new message that is an error reply to a certain message.
1536 * Error replies are possible in response to method calls primarily.
1538 * @param reply_to the original message
1539 * @param error_name the error name
1540 * @param error_message the error message string or #NULL for none
1541 * @returns a new error message
1544 dbus_message_new_error (DBusMessage *reply_to,
1545 const char *error_name,
1546 const char *error_message)
1548 DBusMessage *message;
1550 DBusMessageIter iter;
1552 _dbus_return_val_if_fail (reply_to != NULL, NULL);
1553 _dbus_return_val_if_fail (error_name != NULL, NULL);
1554 _dbus_return_val_if_fail (is_valid_error_name (error_name), NULL);
1556 sender = get_string_field (reply_to,
1557 DBUS_HEADER_FIELD_SENDER,
1560 /* sender may be NULL for non-message-bus case or
1561 * when the message bus is dealing with an unregistered
1564 message = dbus_message_new_empty_header ();
1565 if (message == NULL)
1568 if (!dbus_message_create_header (message,
1569 DBUS_MESSAGE_TYPE_ERROR,
1570 sender, NULL, NULL, NULL, error_name))
1572 dbus_message_unref (message);
1576 dbus_message_set_no_reply (message, TRUE);
1578 if (!dbus_message_set_reply_serial (message,
1579 dbus_message_get_serial (reply_to)))
1581 dbus_message_unref (message);
1585 if (error_message != NULL)
1587 dbus_message_append_iter_init (message, &iter);
1588 if (!dbus_message_iter_append_string (&iter, error_message))
1590 dbus_message_unref (message);
1599 * Creates a new message that is an error reply to a certain message.
1600 * Error replies are possible in response to method calls primarily.
1602 * @param reply_to the original message
1603 * @param error_name the error name
1604 * @param error_format the error message string to be printed
1605 * @param ... value of first argument, list of additional values to print
1606 * @returns a new error message
1609 dbus_message_new_error_printf (DBusMessage *reply_to,
1610 const char *error_name,
1611 const char *error_format,
1616 DBusMessage *message;
1618 _dbus_return_val_if_fail (reply_to != NULL, NULL);
1619 _dbus_return_val_if_fail (error_name != NULL, NULL);
1620 _dbus_return_val_if_fail (is_valid_error_name (error_name), NULL);
1622 if (!_dbus_string_init (&str))
1625 va_start (args, error_format);
1627 if (_dbus_string_append_printf_valist (&str, error_format, args))
1628 message = dbus_message_new_error (reply_to, error_name,
1629 _dbus_string_get_const_data (&str));
1633 _dbus_string_free (&str);
1642 * Creates a new message that is an exact replica of the message
1643 * specified, except that its refcount is set to 1.
1645 * @param message the message.
1646 * @returns the new message.
1649 dbus_message_copy (const DBusMessage *message)
1651 DBusMessage *retval;
1654 _dbus_return_val_if_fail (message != NULL, NULL);
1656 retval = dbus_new0 (DBusMessage, 1);
1660 retval->refcount.value = 1;
1661 retval->byte_order = message->byte_order;
1662 retval->client_serial = message->client_serial;
1663 retval->reply_serial = message->reply_serial;
1664 retval->header_padding = message->header_padding;
1665 retval->locked = FALSE;
1667 if (!_dbus_string_init_preallocated (&retval->header,
1668 _dbus_string_get_length (&message->header)))
1674 if (!_dbus_string_init_preallocated (&retval->body,
1675 _dbus_string_get_length (&message->body)))
1677 _dbus_string_free (&retval->header);
1682 if (!_dbus_string_copy (&message->header, 0,
1683 &retval->header, 0))
1686 if (!_dbus_string_copy (&message->body, 0,
1690 for (i = 0; i <= DBUS_HEADER_FIELD_LAST; i++)
1692 retval->header_fields[i] = message->header_fields[i];
1698 _dbus_string_free (&retval->header);
1699 _dbus_string_free (&retval->body);
1707 * Increments the reference count of a DBusMessage.
1709 * @param message The message
1710 * @returns the message
1711 * @see dbus_message_unref
1714 dbus_message_ref (DBusMessage *message)
1716 dbus_int32_t old_refcount;
1718 _dbus_return_val_if_fail (message != NULL, NULL);
1720 old_refcount = _dbus_atomic_inc (&message->refcount);
1721 _dbus_assert (old_refcount >= 1);
1727 free_size_counter (void *element,
1730 DBusCounter *counter = element;
1731 DBusMessage *message = data;
1733 _dbus_counter_adjust (counter, - message->size_counter_delta);
1735 _dbus_counter_unref (counter);
1739 * Decrements the reference count of a DBusMessage.
1741 * @param message The message
1742 * @see dbus_message_ref
1745 dbus_message_unref (DBusMessage *message)
1747 dbus_int32_t old_refcount;
1749 _dbus_return_if_fail (message != NULL);
1751 old_refcount = _dbus_atomic_dec (&message->refcount);
1753 _dbus_assert (old_refcount >= 0);
1755 if (old_refcount == 1)
1757 /* This calls application callbacks! */
1758 _dbus_data_slot_list_free (&message->slot_list);
1760 _dbus_list_foreach (&message->size_counters,
1761 free_size_counter, message);
1762 _dbus_list_clear (&message->size_counters);
1764 _dbus_string_free (&message->header);
1765 _dbus_string_free (&message->body);
1767 dbus_free (message);
1772 * Gets the type of a message. Types include
1773 * DBUS_MESSAGE_TYPE_METHOD_CALL, DBUS_MESSAGE_TYPE_METHOD_RETURN,
1774 * DBUS_MESSAGE_TYPE_ERROR, DBUS_MESSAGE_TYPE_SIGNAL, but other types
1775 * are allowed and all code must silently ignore messages of unknown
1776 * type. DBUS_MESSAGE_TYPE_INVALID will never be returned, however.
1779 * @param message the message
1780 * @returns the type of the message
1783 dbus_message_get_type (DBusMessage *message)
1787 type = _dbus_string_get_byte (&message->header, 1);
1788 _dbus_assert (type != DBUS_MESSAGE_TYPE_INVALID);
1794 * Sets the object path this message is being sent to (for
1795 * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
1796 * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
1798 * @param message the message
1799 * @param object_path the path or #NULL to unset
1800 * @returns #FALSE if not enough memory
1803 dbus_message_set_path (DBusMessage *message,
1804 const char *object_path)
1806 _dbus_return_val_if_fail (message != NULL, FALSE);
1807 _dbus_return_val_if_fail (!message->locked, FALSE);
1809 return set_string_field (message,
1810 DBUS_HEADER_FIELD_PATH,
1811 DBUS_TYPE_OBJECT_PATH,
1816 * Gets the object path this message is being sent to
1817 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
1818 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
1820 * @param message the message
1821 * @returns the path (should not be freed)
1824 dbus_message_get_path (DBusMessage *message)
1826 _dbus_return_val_if_fail (message != NULL, NULL);
1828 return get_string_field (message, DBUS_HEADER_FIELD_PATH, NULL);
1832 * Gets the object path this message is being sent to
1833 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
1834 * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
1835 * format (one array element per path component).
1836 * Free the returned array with dbus_free_string_array().
1838 * An empty but non-NULL path array means the path "/".
1839 * So the path "/foo/bar" becomes { "foo", "bar", NULL }
1840 * and the path "/" becomes { NULL }.
1842 * @param message the message
1843 * @param path place to store allocated array of path components; #NULL set here if no path field exists
1844 * @returns #FALSE if no memory to allocate the array
1847 dbus_message_get_path_decomposed (DBusMessage *message,
1850 _dbus_return_val_if_fail (message != NULL, FALSE);
1851 _dbus_return_val_if_fail (path != NULL, FALSE);
1853 return get_path_field_decomposed (message,
1854 DBUS_HEADER_FIELD_PATH,
1859 * Sets the interface this message is being sent to
1860 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
1861 * the interface a signal is being emitted from
1862 * (for DBUS_MESSAGE_TYPE_SIGNAL).
1864 * @param message the message
1865 * @param interface the interface or #NULL to unset
1866 * @returns #FALSE if not enough memory
1869 dbus_message_set_interface (DBusMessage *message,
1870 const char *interface)
1872 _dbus_return_val_if_fail (message != NULL, FALSE);
1873 _dbus_return_val_if_fail (!message->locked, FALSE);
1875 return set_string_field (message,
1876 DBUS_HEADER_FIELD_INTERFACE,
1882 * Gets the interface this message is being sent to
1883 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
1884 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
1885 * The interface name is fully-qualified (namespaced).
1887 * @param message the message
1888 * @returns the message interface (should not be freed)
1891 dbus_message_get_interface (DBusMessage *message)
1893 _dbus_return_val_if_fail (message != NULL, NULL);
1895 return get_string_field (message, DBUS_HEADER_FIELD_INTERFACE, NULL);
1899 * Sets the interface member being invoked
1900 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
1901 * (DBUS_MESSAGE_TYPE_SIGNAL).
1902 * The interface name is fully-qualified (namespaced).
1904 * @param message the message
1905 * @param member the member or #NULL to unset
1906 * @returns #FALSE if not enough memory
1909 dbus_message_set_member (DBusMessage *message,
1912 _dbus_return_val_if_fail (message != NULL, FALSE);
1913 _dbus_return_val_if_fail (!message->locked, FALSE);
1915 return set_string_field (message,
1916 DBUS_HEADER_FIELD_MEMBER,
1922 * Gets the interface member being invoked
1923 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
1924 * (DBUS_MESSAGE_TYPE_SIGNAL).
1926 * @param message the message
1927 * @returns the member name (should not be freed)
1930 dbus_message_get_member (DBusMessage *message)
1932 _dbus_return_val_if_fail (message != NULL, NULL);
1934 return get_string_field (message,
1935 DBUS_HEADER_FIELD_MEMBER,
1940 * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
1941 * The name is fully-qualified (namespaced).
1943 * @param message the message
1944 * @param error_name the name or #NULL to unset
1945 * @returns #FALSE if not enough memory
1948 dbus_message_set_error_name (DBusMessage *message,
1949 const char *error_name)
1951 _dbus_return_val_if_fail (message != NULL, FALSE);
1952 _dbus_return_val_if_fail (!message->locked, FALSE);
1953 _dbus_return_val_if_fail (error_name == NULL || is_valid_error_name (error_name), FALSE);
1955 return set_string_field (message,
1956 DBUS_HEADER_FIELD_ERROR_NAME,
1962 * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only).
1964 * @param message the message
1965 * @returns the error name (should not be freed)
1968 dbus_message_get_error_name (DBusMessage *message)
1970 _dbus_return_val_if_fail (message != NULL, NULL);
1972 return get_string_field (message,
1973 DBUS_HEADER_FIELD_ERROR_NAME,
1978 * Sets the message's destination service.
1980 * @param message the message
1981 * @param destination the destination service name or #NULL to unset
1982 * @returns #FALSE if not enough memory
1985 dbus_message_set_destination (DBusMessage *message,
1986 const char *destination)
1988 _dbus_return_val_if_fail (message != NULL, FALSE);
1989 _dbus_return_val_if_fail (!message->locked, FALSE);
1991 return set_string_field (message,
1992 DBUS_HEADER_FIELD_DESTINATION,
1998 * Gets the destination service of a message.
2000 * @param message the message
2001 * @returns the message destination service (should not be freed)
2004 dbus_message_get_destination (DBusMessage *message)
2006 _dbus_return_val_if_fail (message != NULL, NULL);
2008 return get_string_field (message,
2009 DBUS_HEADER_FIELD_DESTINATION,
2014 * Appends fields to a message given a variable argument list. The
2015 * variable argument list should contain the type of the argument
2016 * followed by the value to add. Array values are specified by an int
2017 * typecode followed by a pointer to the array followed by an int
2018 * giving the length of the array. The argument list must be
2019 * terminated with #DBUS_TYPE_INVALID.
2021 * This function doesn't support dicts or non-fundamental arrays.
2023 * This function supports #DBUS_TYPE_INT64 and #DBUS_TYPE_UINT64
2024 * only if #DBUS_HAVE_INT64 is defined.
2026 * @param message the message
2027 * @param first_arg_type type of the first argument
2028 * @param ... value of first argument, list of additional type-value pairs
2029 * @returns #TRUE on success
2032 dbus_message_append_args (DBusMessage *message,
2039 _dbus_return_val_if_fail (message != NULL, FALSE);
2041 va_start (var_args, first_arg_type);
2042 retval = dbus_message_append_args_valist (message,
2051 * Gets arguments from a message given a variable argument list.
2052 * The variable argument list should contain the type of the
2053 * argumen followed by a pointer to where the value should be
2054 * stored. The list is terminated with #DBUS_TYPE_INVALID.
2056 * @param message the message
2057 * @param error error to be filled in on failure
2058 * @param first_arg_type the first argument type
2059 * @param ... location for first argument value, then list of type-location pairs
2060 * @returns #FALSE if the error was set
2063 dbus_message_get_args (DBusMessage *message,
2071 _dbus_return_val_if_fail (message != NULL, FALSE);
2072 _dbus_return_val_if_error_is_set (error, FALSE);
2074 va_start (var_args, first_arg_type);
2075 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
2082 * This function takes a va_list for use by language bindings
2084 * @todo We need to free the argument data when an error occurs.
2086 * @see dbus_message_get_args
2087 * @param message the message
2088 * @param error error to be filled in
2089 * @param first_arg_type type of the first argument
2090 * @param var_args return location for first argument, followed by list of type/location pairs
2091 * @returns #FALSE if error was set
2094 dbus_message_get_args_valist (DBusMessage *message,
2099 DBusMessageIter iter;
2101 _dbus_return_val_if_fail (message != NULL, FALSE);
2102 _dbus_return_val_if_error_is_set (error, FALSE);
2104 dbus_message_iter_init (message, &iter);
2105 return dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
2109 * Gets arguments from a message iterator given a variable argument list.
2110 * The variable argument list should contain the type of the
2111 * argumen followed by a pointer to where the value should be
2112 * stored. The list is terminated with 0.
2114 * @param iter the message iterator
2115 * @param error error to be filled in on failure
2116 * @param first_arg_type the first argument type
2117 * @param ... location for first argument value, then list of type-location pairs
2118 * @returns #FALSE if the error was set
2121 dbus_message_iter_get_args (DBusMessageIter *iter,
2129 _dbus_return_val_if_fail (iter != NULL, FALSE);
2130 _dbus_return_val_if_error_is_set (error, FALSE);
2132 va_start (var_args, first_arg_type);
2133 retval = dbus_message_iter_get_args_valist (iter, error, first_arg_type, var_args);
2140 * Initializes a DBusMessageIter representing the arguments of the
2141 * message passed in.
2143 * @param message the message
2144 * @param iter pointer to an iterator to initialize
2145 * @returns #FALSE if the message has no arguments
2148 dbus_message_iter_init (DBusMessage *message,
2149 DBusMessageIter *iter)
2151 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2153 _dbus_return_val_if_fail (message != NULL, FALSE);
2154 _dbus_return_val_if_fail (iter != NULL, FALSE);
2156 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
2158 real->message = message;
2159 real->parent_iter = NULL;
2160 real->changed_stamp = message->changed_stamp;
2162 real->type = DBUS_MESSAGE_ITER_TYPE_MESSAGE;
2164 real->end = _dbus_string_get_length (&message->body);
2166 real->container_start = 0;
2167 real->container_length_pos = 0;
2168 real->wrote_dict_key = 0;
2169 real->array_type_pos = 0;
2171 return real->end > real->pos;
2174 #ifndef DBUS_DISABLE_CHECKS
2176 dbus_message_iter_check (DBusMessageRealIter *iter)
2180 _dbus_warn ("dbus iterator check failed: iterator is NULL\n");
2184 if (iter->changed_stamp != iter->message->changed_stamp)
2186 _dbus_warn ("dbus iterator check failed: invalid iterator, must re-initialize it after modifying the message\n");
2190 if (iter->pos < 0 || iter->pos > iter->end)
2192 _dbus_warn ("dbus iterator check failed: invalid position\n");
2198 #endif /* DBUS_DISABLE_CHECKS */
2201 skip_array_type (DBusMessageRealIter *iter, int pos)
2207 data = _dbus_string_get_const_data_len (&iter->message->body,
2210 while (*data == DBUS_TYPE_ARRAY);
2215 /* FIXME what are these _dbus_type_is_valid() checks for?
2216 * haven't we validated the message?
2219 dbus_message_iter_get_data_start (DBusMessageRealIter *iter, int *type)
2226 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
2227 data = _dbus_string_get_const_data_len (&iter->message->body,
2229 if (_dbus_type_is_valid (*data))
2232 *type = DBUS_TYPE_INVALID;
2234 return skip_array_type (iter, iter->pos);
2236 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
2237 data = _dbus_string_get_const_data_len (&iter->message->body,
2238 iter->array_type_pos, 1);
2239 if (_dbus_type_is_valid (*data))
2242 *type = DBUS_TYPE_INVALID;
2246 case DBUS_MESSAGE_ITER_TYPE_DICT:
2247 /* Get the length of the string */
2248 len = _dbus_demarshal_uint32 (&iter->message->body,
2249 iter->message->byte_order,
2251 pos = pos + len + 1;
2253 data = _dbus_string_get_const_data_len (&iter->message->body,
2255 if (_dbus_type_is_valid (*data))
2258 *type = DBUS_TYPE_INVALID;
2260 return skip_array_type (iter, pos);
2263 _dbus_assert_not_reached ("Invalid iter type");
2266 *type = DBUS_TYPE_INVALID;
2272 * Checks if an iterator has any more fields.
2274 * @param iter the message iter
2275 * @returns #TRUE if there are more fields
2279 dbus_message_iter_has_next (DBusMessageIter *iter)
2281 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2285 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2287 if (real->pos >= real->end)
2290 pos = dbus_message_iter_get_data_start (real, &type);
2292 if (!_dbus_marshal_get_arg_end_pos (&real->message->body,
2293 real->message->byte_order,
2294 type, pos, &end_pos))
2297 if (end_pos >= real->end)
2304 * Moves the iterator to the next field.
2306 * @param iter The message iter
2307 * @returns #TRUE if the iterator was moved to the next field
2310 dbus_message_iter_next (DBusMessageIter *iter)
2312 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2316 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2318 pos = dbus_message_iter_get_data_start (real, &type);
2320 if (!_dbus_marshal_get_arg_end_pos (&real->message->body,
2321 real->message->byte_order,
2322 type, pos, &end_pos))
2325 if (end_pos >= real->end)
2328 real->pos = end_pos;
2334 * Returns the argument type of the argument that the
2335 * message iterator points at.
2337 * @param iter the message iter
2338 * @returns the field type
2341 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
2343 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2346 _dbus_return_val_if_fail (dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2348 if (real->pos >= real->end)
2350 _dbus_verbose (" iterator at or beyond end of message\n");
2351 return DBUS_TYPE_INVALID;
2354 pos = dbus_message_iter_get_data_start (real, &type);
2359 /* FIXME why do we validate the typecode in here, hasn't the message
2360 * already been verified?
2363 iter_get_array_type (DBusMessageRealIter *iter, int *array_type_pos)
2366 int _array_type_pos;
2371 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
2372 _array_type_pos = iter->pos + 1;
2374 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
2375 _array_type_pos = iter->array_type_pos + 1;
2377 case DBUS_MESSAGE_ITER_TYPE_DICT:
2378 /* Get the length of the string */
2379 len = _dbus_demarshal_uint32 (&iter->message->body,
2380 iter->message->byte_order,
2382 pos = pos + len + 1;
2383 data = _dbus_string_get_const_data_len (&iter->message->body,
2385 _array_type_pos = pos + 1;
2388 _dbus_assert_not_reached ("wrong iter type");
2389 return DBUS_TYPE_INVALID;
2392 if (array_type_pos != NULL)
2393 *array_type_pos = _array_type_pos;
2395 data = _dbus_string_get_const_data_len (&iter->message->body,
2396 _array_type_pos, 1);
2397 if (_dbus_type_is_valid (*data))
2400 return DBUS_TYPE_INVALID;
2405 * Returns the element type of the array that the
2406 * message iterator points at. Note that you need
2407 * to check that the iterator points to an array
2408 * prior to using this function.
2410 * @param iter the message iter
2411 * @returns the field type
2414 dbus_message_iter_get_array_type (DBusMessageIter *iter)
2416 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2419 _dbus_return_val_if_fail (dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2421 if (real->pos >= real->end)
2422 return DBUS_TYPE_INVALID;
2424 pos = dbus_message_iter_get_data_start (real, &type);
2426 _dbus_assert (type == DBUS_TYPE_ARRAY);
2428 return iter_get_array_type (real, NULL);
2433 * Returns the string value that an iterator may point to.
2434 * Note that you need to check that the iterator points to
2435 * a string value before using this function.
2437 * @see dbus_message_iter_get_arg_type
2438 * @param iter the message iter
2439 * @returns the string
2442 dbus_message_iter_get_string (DBusMessageIter *iter)
2444 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2447 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
2448 pos = dbus_message_iter_get_data_start (real, &type);
2450 _dbus_assert (type == DBUS_TYPE_STRING);
2452 return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2457 * Returns the object path value that an iterator may point to.
2458 * Note that you need to check that the iterator points to
2459 * an object path value before using this function.
2461 * @see dbus_message_iter_get_arg_type
2462 * @param iter the message iter
2466 dbus_message_iter_get_object_path (DBusMessageIter *iter)
2468 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2471 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
2473 pos = dbus_message_iter_get_data_start (real, &type);
2475 _dbus_assert (type == DBUS_TYPE_OBJECT_PATH);
2477 return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2482 * Returns the name and data from a custom type that an iterator may
2483 * point to. Note that you need to check that the iterator points to a
2484 * custom type before using this function.
2486 * @see dbus_message_iter_get_arg_type
2487 * @param iter the message iter
2488 * @param name return location for the name of the custom type
2489 * @param value return location for data
2490 * @param len return location for length of data
2491 * @returns TRUE if get succeed
2495 dbus_message_iter_get_custom (DBusMessageIter *iter,
2497 unsigned char **value,
2500 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2504 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2506 pos = dbus_message_iter_get_data_start (real, &type);
2508 _dbus_assert (type == DBUS_TYPE_CUSTOM);
2510 _name = _dbus_demarshal_string (&real->message->body, real->message->byte_order,
2516 if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
2517 pos, NULL, value, len))
2529 _dbus_message_iter_get_basic_type (DBusMessageIter *iter,
2533 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2536 _dbus_return_if_fail (dbus_message_iter_check (real));
2538 pos = dbus_message_iter_get_data_start (real, &item_type);
2540 _dbus_assert (type == item_type);
2542 _dbus_demarshal_basic_type (&real->message->body,
2544 real->message->byte_order,
2550 * This function takes a va_list for use by language bindings
2552 * This function supports #DBUS_TYPE_INT64 and #DBUS_TYPE_UINT64
2553 * only if #DBUS_HAVE_INT64 is defined.
2555 * @todo this function (or some lower-level non-convenience function)
2556 * needs better error handling; should allow the application to
2557 * distinguish between out of memory, and bad data from the remote
2558 * app. It also needs to not leak a bunch of args when it gets
2559 * to the arg that's bad, as that would be a security hole
2560 * (allow one app to force another to leak memory)
2562 * @todo We need to free the argument data when an error occurs.
2564 * @see dbus_message_get_args
2565 * @param iter the message iter
2566 * @param error error to be filled in
2567 * @param first_arg_type type of the first argument
2568 * @param var_args return location for first argument, followed by list of type/location pairs
2569 * @returns #FALSE if error was set
2572 dbus_message_iter_get_args_valist (DBusMessageIter *iter,
2577 int spec_type, msg_type, i;
2580 _dbus_return_val_if_fail (iter != NULL, FALSE);
2581 _dbus_return_val_if_error_is_set (error, FALSE);
2585 spec_type = first_arg_type;
2588 while (spec_type != DBUS_TYPE_INVALID)
2590 msg_type = dbus_message_iter_get_arg_type (iter);
2592 if (msg_type != spec_type)
2594 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2595 "Argument %d is specified to be of type \"%s\", but "
2596 "is actually of type \"%s\"\n", i,
2597 _dbus_type_to_string (spec_type),
2598 _dbus_type_to_string (msg_type));
2607 case DBUS_TYPE_BOOLEAN:
2611 ptr = va_arg (var_args, dbus_bool_t *);
2613 *ptr = dbus_message_iter_get_boolean (iter);
2616 case DBUS_TYPE_BYTE:
2617 case DBUS_TYPE_INT32:
2618 case DBUS_TYPE_UINT32:
2619 #ifdef DBUS_HAVE_INT64
2620 case DBUS_TYPE_INT64:
2621 case DBUS_TYPE_UINT64:
2622 #endif /* DBUS_HAVE_INT64 */
2623 case DBUS_TYPE_DOUBLE:
2625 void *ptr = va_arg (var_args, void *);
2626 _dbus_message_iter_get_basic_type (iter, spec_type, ptr);
2630 case DBUS_TYPE_STRING:
2634 ptr = va_arg (var_args, char **);
2636 *ptr = dbus_message_iter_get_string (iter);
2640 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2647 case DBUS_TYPE_OBJECT_PATH:
2651 ptr = va_arg (var_args, char **);
2653 *ptr = dbus_message_iter_get_object_path (iter);
2657 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2664 case DBUS_TYPE_CUSTOM:
2667 unsigned char **data;
2670 name = va_arg (var_args, char **);
2671 data = va_arg (var_args, unsigned char **);
2672 len = va_arg (var_args, int *);
2674 if (!dbus_message_iter_get_custom (iter, name, data, len))
2676 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2681 case DBUS_TYPE_ARRAY:
2685 dbus_bool_t err = FALSE;
2687 type = va_arg (var_args, int);
2688 data = va_arg (var_args, void *);
2689 len = va_arg (var_args, int *);
2691 _dbus_return_val_if_fail (data != NULL, FALSE);
2692 _dbus_return_val_if_fail (len != NULL, FALSE);
2694 if (dbus_message_iter_get_array_type (iter) != type)
2696 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2697 "Argument %d is specified to be of type \"array of %s\", but "
2698 "is actually of type \"array of %s\"\n", i,
2699 _dbus_type_to_string (type),
2700 _dbus_type_to_string (dbus_message_iter_get_array_type (iter)));
2706 case DBUS_TYPE_BYTE:
2707 err = !dbus_message_iter_get_byte_array (iter, (unsigned char **)data, len);
2709 case DBUS_TYPE_BOOLEAN:
2710 err = !dbus_message_iter_get_boolean_array (iter, (unsigned char **)data, len);
2712 case DBUS_TYPE_INT32:
2713 err = !dbus_message_iter_get_int32_array (iter, (dbus_int32_t **)data, len);
2715 case DBUS_TYPE_UINT32:
2716 err = !dbus_message_iter_get_uint32_array (iter, (dbus_uint32_t **)data, len);
2718 #ifdef DBUS_HAVE_INT64
2719 case DBUS_TYPE_INT64:
2720 err = !dbus_message_iter_get_int64_array (iter, (dbus_int64_t **)data, len);
2722 case DBUS_TYPE_UINT64:
2723 err = !dbus_message_iter_get_uint64_array (iter, (dbus_uint64_t **)data, len);
2725 #endif /* DBUS_HAVE_INT64 */
2726 case DBUS_TYPE_DOUBLE:
2727 err = !dbus_message_iter_get_double_array (iter, (double **)data, len);
2729 case DBUS_TYPE_STRING:
2730 err = !dbus_message_iter_get_string_array (iter, (char ***)data, len);
2732 case DBUS_TYPE_OBJECT_PATH:
2733 err = !dbus_message_iter_get_object_path_array (iter, (char ***)data, len);
2737 case DBUS_TYPE_ARRAY:
2738 case DBUS_TYPE_CUSTOM:
2739 case DBUS_TYPE_DICT:
2740 _dbus_warn ("dbus_message_get_args_valist doesn't support recursive arrays\n");
2741 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2744 _dbus_warn ("Unknown field type %d\n", type);
2745 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2750 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2755 case DBUS_TYPE_DICT:
2756 _dbus_warn ("dbus_message_get_args_valist doesn't support dicts\n");
2757 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2760 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
2761 _dbus_warn ("Unknown field type %d\n", spec_type);
2765 spec_type = va_arg (var_args, int);
2766 if (!dbus_message_iter_next (iter) && spec_type != DBUS_TYPE_INVALID)
2768 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2769 "Message has only %d arguments, but more were expected", i);
2784 * Returns the byte value that an iterator may point to.
2785 * Note that you need to check that the iterator points to
2786 * a byte value before using this function.
2788 * @see dbus_message_iter_get_arg_type
2789 * @param iter the message iter
2790 * @returns the byte value
2793 dbus_message_iter_get_byte (DBusMessageIter *iter)
2795 unsigned char value = 0;
2797 _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_BYTE, &value);
2803 * Returns the boolean value that an iterator may point to.
2804 * Note that you need to check that the iterator points to
2805 * a boolean value before using this function.
2807 * @see dbus_message_iter_get_arg_type
2808 * @param iter the message iter
2809 * @returns the boolean value
2812 dbus_message_iter_get_boolean (DBusMessageIter *iter)
2814 unsigned char value = 0;
2816 _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_BOOLEAN, &value);
2818 return (value != FALSE);
2822 * Returns the 32 bit signed integer value that an iterator may point to.
2823 * Note that you need to check that the iterator points to
2824 * a 32-bit integer value before using this function.
2826 * @see dbus_message_iter_get_arg_type
2827 * @param iter the message iter
2828 * @returns the integer
2831 dbus_message_iter_get_int32 (DBusMessageIter *iter)
2833 dbus_int32_t value = 0;
2835 _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_INT32, &value);
2841 * Returns the 32 bit unsigned integer value that an iterator may point to.
2842 * Note that you need to check that the iterator points to
2843 * a 32-bit unsigned integer value before using this function.
2845 * @see dbus_message_iter_get_arg_type
2846 * @param iter the message iter
2847 * @returns the integer
2850 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
2852 dbus_int32_t value = 0;
2854 _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_UINT32, &value);
2859 #ifdef DBUS_HAVE_INT64
2862 * Returns the 64 bit signed integer value that an iterator may point
2863 * to. Note that you need to check that the iterator points to a
2864 * 64-bit integer value before using this function.
2866 * This function only exists if #DBUS_HAVE_INT64 is defined.
2868 * @see dbus_message_iter_get_arg_type
2869 * @param iter the message iter
2870 * @returns the integer
2873 dbus_message_iter_get_int64 (DBusMessageIter *iter)
2875 dbus_int64_t value = 0;
2877 _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_INT64, &value);
2883 * Returns the 64 bit unsigned integer value that an iterator may point to.
2884 * Note that you need to check that the iterator points to
2885 * a 64-bit unsigned integer value before using this function.
2887 * This function only exists if #DBUS_HAVE_INT64 is defined.
2889 * @see dbus_message_iter_get_arg_type
2890 * @param iter the message iter
2891 * @returns the integer
2894 dbus_message_iter_get_uint64 (DBusMessageIter *iter)
2896 dbus_uint64_t value = 0;
2898 _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_UINT64, &value);
2903 #endif /* DBUS_HAVE_INT64 */
2906 * Returns the double value that an iterator may point to.
2907 * Note that you need to check that the iterator points to
2908 * a string value before using this function.
2910 * @see dbus_message_iter_get_arg_type
2911 * @param iter the message iter
2912 * @returns the double
2915 dbus_message_iter_get_double (DBusMessageIter *iter)
2919 _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_DOUBLE, &value);
2925 * Initializes an iterator for the array that the iterator
2926 * may point to. Note that you need to check that the iterator
2927 * points to an array prior to using this function.
2929 * The array element type is returned in array_type, and the array
2930 * iterator can only be used to get that type of data.
2932 * @param iter the iterator
2933 * @param array_iter pointer to an iterator to initialize
2934 * @param array_type gets set to the type of the array elements
2935 * @returns #FALSE if the array is empty
2938 dbus_message_iter_init_array_iterator (DBusMessageIter *iter,
2939 DBusMessageIter *array_iter,
2942 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2943 DBusMessageRealIter *array_real = (DBusMessageRealIter *)array_iter;
2944 int type, pos, len_pos, len, array_type_pos;
2947 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2949 pos = dbus_message_iter_get_data_start (real, &type);
2951 _dbus_assert (type == DBUS_TYPE_ARRAY);
2953 _array_type = iter_get_array_type (real, &array_type_pos);
2955 len_pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
2956 len = _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
2959 array_real->parent_iter = real;
2960 array_real->message = real->message;
2961 array_real->changed_stamp = real->message->changed_stamp;
2963 array_real->type = DBUS_MESSAGE_ITER_TYPE_ARRAY;
2964 array_real->pos = pos;
2965 array_real->end = pos + len;
2967 array_real->container_start = pos;
2968 array_real->container_length_pos = len_pos;
2969 array_real->wrote_dict_key = 0;
2970 array_real->array_type_pos = array_type_pos;
2971 array_real->array_type_done = TRUE;
2973 if (array_type != NULL)
2974 *array_type = _array_type;
2981 * Initializes an iterator for the dict that the iterator
2982 * may point to. Note that you need to check that the iterator
2983 * points to a dict prior to using this function.
2985 * @param iter the iterator
2986 * @param dict_iter pointer to an iterator to initialize
2987 * @returns #FALSE if the dict is empty
2990 dbus_message_iter_init_dict_iterator (DBusMessageIter *iter,
2991 DBusMessageIter *dict_iter)
2993 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2994 DBusMessageRealIter *dict_real = (DBusMessageRealIter *)dict_iter;
2995 int type, pos, len_pos, len;
2997 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
2999 pos = dbus_message_iter_get_data_start (real, &type);
3001 _dbus_assert (type == DBUS_TYPE_DICT);
3003 len_pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
3004 len = _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
3007 dict_real->parent_iter = real;
3008 dict_real->message = real->message;
3009 dict_real->changed_stamp = real->message->changed_stamp;
3011 dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
3012 dict_real->pos = pos;
3013 dict_real->end = pos + len;
3015 dict_real->container_start = pos;
3016 dict_real->container_length_pos = len_pos;
3017 dict_real->wrote_dict_key = 0;
3023 _dbus_message_iter_get_basic_type_array (DBusMessageIter *iter,
3028 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3031 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3033 pos = dbus_message_iter_get_data_start (real, &item_type);
3035 _dbus_assert (item_type == DBUS_TYPE_ARRAY);
3037 item_type = iter_get_array_type (real, NULL);
3039 _dbus_assert (type == item_type);
3041 return _dbus_demarshal_basic_type_array (&real->message->body,
3042 item_type, array, array_len,
3043 real->message->byte_order, &pos);
3047 * Returns the byte array that the iterator may point to.
3048 * Note that you need to check that the iterator points
3049 * to a byte array prior to using this function.
3051 * @param iter the iterator
3052 * @param value return location for array values
3053 * @param len return location for length of byte array
3054 * @returns #TRUE on success
3057 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
3058 unsigned char **value,
3061 return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_BYTE,
3062 (void **) value, len);
3066 * Returns the boolean array that the iterator may point to. Note that
3067 * you need to check that the iterator points to an array of the
3068 * correct type prior to using this function.
3070 * @param iter the iterator
3071 * @param value return location for the array
3072 * @param len return location for the array length
3073 * @returns #TRUE on success
3076 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
3077 unsigned char **value,
3080 return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_BOOLEAN,
3081 (void **) value, len);
3085 * Returns the 32 bit signed integer array that the iterator may point
3086 * to. Note that you need to check that the iterator points to an
3087 * array of the correct type prior to using this function.
3089 * @param iter the iterator
3090 * @param value return location for the array
3091 * @param len return location for the array length
3092 * @returns #TRUE on success
3095 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
3096 dbus_int32_t **value,
3099 return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_INT32,
3100 (void **) value, len);
3104 * Returns the 32 bit unsigned integer array that the iterator may point
3105 * to. Note that you need to check that the iterator points to an
3106 * array of the correct type prior to using this function.
3108 * @param iter the iterator
3109 * @param value return location for the array
3110 * @param len return location for the array length
3111 * @returns #TRUE on success
3114 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
3115 dbus_uint32_t **value,
3118 return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_UINT32,
3119 (void **) value, len);
3122 #ifdef DBUS_HAVE_INT64
3125 * Returns the 64 bit signed integer array that the iterator may point
3126 * to. Note that you need to check that the iterator points to an
3127 * array of the correct type prior to using this function.
3129 * This function only exists if #DBUS_HAVE_INT64 is defined.
3131 * @param iter the iterator
3132 * @param value return location for the array
3133 * @param len return location for the array length
3134 * @returns #TRUE on success
3137 dbus_message_iter_get_int64_array (DBusMessageIter *iter,
3138 dbus_int64_t **value,
3141 return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_INT64,
3142 (void **) value, len);
3146 * Returns the 64 bit unsigned integer array that the iterator may point
3147 * to. Note that you need to check that the iterator points to an
3148 * array of the correct type prior to using this function.
3150 * This function only exists if #DBUS_HAVE_INT64 is defined.
3152 * @param iter the iterator
3153 * @param value return location for the array
3154 * @param len return location for the array length
3155 * @returns #TRUE on success
3158 dbus_message_iter_get_uint64_array (DBusMessageIter *iter,
3159 dbus_uint64_t **value,
3162 return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_UINT64,
3163 (void **) value, len);
3166 #endif /* DBUS_HAVE_INT64 */
3169 * Returns the double array that the iterator may point to. Note that
3170 * you need to check that the iterator points to an array of the
3171 * correct type prior to using this function.
3173 * @param iter the iterator
3174 * @param value return location for the array
3175 * @param len return location for the array length
3176 * @returns #TRUE on success
3179 dbus_message_iter_get_double_array (DBusMessageIter *iter,
3183 return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_DOUBLE,
3184 (void **) value, len);
3188 * Returns the string array that the iterator may point to.
3189 * Note that you need to check that the iterator points
3190 * to a string array prior to using this function.
3192 * The returned value is a #NULL-terminated array of strings.
3193 * Each string is a separate malloc block, and the array
3194 * itself is a malloc block. You can free this type of
3195 * string array with dbus_free_string_array().
3197 * @param iter the iterator
3198 * @param value return location for string values
3199 * @param len return location for length of byte array
3200 * @returns #TRUE on success
3203 dbus_message_iter_get_string_array (DBusMessageIter *iter,
3207 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3210 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3212 pos = dbus_message_iter_get_data_start (real, &type);
3214 _dbus_assert (type == DBUS_TYPE_ARRAY);
3216 type = iter_get_array_type (real, NULL);
3217 _dbus_assert (type == DBUS_TYPE_STRING);
3219 if (!_dbus_demarshal_string_array (&real->message->body, real->message->byte_order,
3220 pos, NULL, value, len))
3227 * Returns the object path array that the iterator may point to.
3228 * Note that you need to check that the iterator points
3229 * to an object path array prior to using this function.
3231 * The returned value is a #NULL-terminated array of strings.
3232 * Each string is a separate malloc block, and the array
3233 * itself is a malloc block. You can free this type of
3234 * array with dbus_free_string_array().
3236 * @param iter the iterator
3237 * @param value return location for string values
3238 * @param len return location for length of byte array
3239 * @returns #TRUE on success
3242 dbus_message_iter_get_object_path_array (DBusMessageIter *iter,
3246 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3249 _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
3251 pos = dbus_message_iter_get_data_start (real, &type);
3253 _dbus_assert (type == DBUS_TYPE_ARRAY);
3255 type = iter_get_array_type (real, NULL);
3256 _dbus_assert (type == DBUS_TYPE_OBJECT_PATH);
3258 if (!_dbus_demarshal_string_array (&real->message->body, real->message->byte_order,
3259 pos, NULL, value, len))
3266 * Returns the key name fot the dict entry that an iterator
3267 * may point to. Note that you need to check that the iterator
3268 * points to a dict entry before using this function.
3270 * @see dbus_message_iter_init_dict_iterator
3271 * @param iter the message iter
3272 * @returns the key name
3275 dbus_message_iter_get_dict_key (DBusMessageIter *iter)
3277 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3279 _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
3281 _dbus_assert (real->type == DBUS_MESSAGE_ITER_TYPE_DICT);
3283 return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
3288 * Initializes a DBusMessageIter pointing to the end of the
3289 * message. This iterator can be used to append data to the
3292 * @param message the message
3293 * @param iter pointer to an iterator to initialize
3296 dbus_message_append_iter_init (DBusMessage *message,
3297 DBusMessageIter *iter)
3299 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3301 _dbus_return_if_fail (message != NULL);
3302 _dbus_return_if_fail (iter != NULL);
3304 real->message = message;
3305 real->parent_iter = NULL;
3306 real->changed_stamp = message->changed_stamp;
3308 real->type = DBUS_MESSAGE_ITER_TYPE_MESSAGE;
3309 real->end = _dbus_string_get_length (&real->message->body);
3310 real->pos = real->end;
3312 real->container_length_pos = 0;
3313 real->wrote_dict_key = 0;
3316 #ifndef DBUS_DISABLE_CHECKS
3318 dbus_message_iter_append_check (DBusMessageRealIter *iter)
3322 _dbus_warn ("dbus iterator check failed: NULL iterator\n");
3326 if (iter->message->locked)
3328 _dbus_warn ("dbus iterator check failed: message is locked (has already been sent)\n");
3332 if (iter->changed_stamp != iter->message->changed_stamp)
3334 _dbus_warn ("dbus iterator check failed: invalid iterator, must re-initialize it after modifying the message");
3338 if (iter->pos != iter->end)
3340 _dbus_warn ("dbus iterator check failed: can only append at end of message");
3344 if (iter->pos != _dbus_string_get_length (&iter->message->body))
3346 _dbus_warn ("dbus iterator check failed: append pos not at end of message string");
3352 #endif /* DBUS_DISABLE_CHECKS */
3355 dbus_message_iter_append_type (DBusMessageRealIter *iter,
3362 case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
3363 if (!_dbus_string_append_byte (&iter->message->body, type))
3366 if (!_dbus_message_append_byte_to_signature (iter->message, type))
3368 _dbus_string_shorten (&iter->message->body, 1);
3373 case DBUS_MESSAGE_ITER_TYPE_ARRAY:
3374 data = _dbus_string_get_const_data_len (&iter->message->body,
3375 iter->array_type_pos, 1);
3378 _dbus_warn ("Appended element of wrong type for array\n");
3383 case DBUS_MESSAGE_ITER_TYPE_DICT:
3384 if (!iter->wrote_dict_key)
3386 _dbus_warn ("Appending dict data before key name\n");
3390 if (!_dbus_string_append_byte (&iter->message->body, type))
3396 _dbus_assert_not_reached ("Invalid iter type");
3404 dbus_message_iter_update_after_change (DBusMessageRealIter *iter)
3406 iter->changed_stamp = iter->message->changed_stamp;
3408 /* Set new end of iter */
3409 iter->end = _dbus_string_get_length (&iter->message->body);
3410 iter->pos = iter->end;
3412 /* Set container length */
3413 if (iter->type == DBUS_MESSAGE_ITER_TYPE_DICT ||
3414 (iter->type == DBUS_MESSAGE_ITER_TYPE_ARRAY && iter->array_type_done))
3415 _dbus_marshal_set_uint32 (&iter->message->body,
3416 iter->message->byte_order,
3417 iter->container_length_pos,
3418 iter->end - iter->container_start);
3420 if (iter->parent_iter)
3421 dbus_message_iter_update_after_change (iter->parent_iter);
3425 dbus_message_iter_append_done (DBusMessageRealIter *iter)
3427 iter->message->changed_stamp++;
3428 dbus_message_iter_update_after_change (iter);
3429 iter->wrote_dict_key = FALSE;
3433 * Appends a nil value to the message
3435 * @param iter an iterator pointing to the end of the message
3436 * @returns #TRUE on success
3439 dbus_message_iter_append_nil (DBusMessageIter *iter)
3441 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3443 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3445 if (!dbus_message_iter_append_type (real, DBUS_TYPE_NIL))
3448 dbus_message_iter_append_done (real);
3454 dbus_message_iter_append_basic (DBusMessageIter *iter,
3458 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3460 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3462 if (!dbus_message_iter_append_type (real, type))
3465 if (!_dbus_marshal_basic_type (&real->message->body,
3467 real->message->byte_order))
3469 _dbus_string_set_length (&real->message->body, real->pos);
3473 dbus_message_iter_append_done (real);
3479 * Appends a boolean value to the message
3481 * @param iter an iterator pointing to the end of the message
3482 * @param value the boolean value
3483 * @returns #TRUE on success
3486 dbus_message_iter_append_boolean (DBusMessageIter *iter,
3489 unsigned char val = (value != FALSE);
3490 return dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &val);
3494 * Appends a byte to the message
3496 * @param iter an iterator pointing to the end of the message
3497 * @param value the byte value
3498 * @returns #TRUE on success
3501 dbus_message_iter_append_byte (DBusMessageIter *iter,
3502 unsigned char value)
3504 return dbus_message_iter_append_basic (iter, DBUS_TYPE_BYTE, &value);
3508 * Appends a 32 bit signed integer to the message.
3510 * @param iter an iterator pointing to the end of the message
3511 * @param value the integer value
3512 * @returns #TRUE on success
3515 dbus_message_iter_append_int32 (DBusMessageIter *iter,
3518 return dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &value);
3522 * Appends a 32 bit unsigned integer to the message.
3524 * @param iter an iterator pointing to the end of the message
3525 * @param value the integer value
3526 * @returns #TRUE on success
3529 dbus_message_iter_append_uint32 (DBusMessageIter *iter,
3530 dbus_uint32_t value)
3532 return dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT32, &value);
3535 #ifdef DBUS_HAVE_INT64
3538 * Appends a 64 bit signed integer to the message.
3540 * This function only exists if #DBUS_HAVE_INT64 is defined.
3542 * @param iter an iterator pointing to the end of the message
3543 * @param value the integer value
3544 * @returns #TRUE on success
3547 dbus_message_iter_append_int64 (DBusMessageIter *iter,
3550 return dbus_message_iter_append_basic (iter, DBUS_TYPE_INT64, &value);
3554 * Appends a 64 bit unsigned integer to the message.
3556 * This function only exists if #DBUS_HAVE_INT64 is defined.
3558 * @param iter an iterator pointing to the end of the message
3559 * @param value the integer value
3560 * @returns #TRUE on success
3563 dbus_message_iter_append_uint64 (DBusMessageIter *iter,
3564 dbus_uint64_t value)
3566 return dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT64, &value);
3569 #endif /* DBUS_HAVE_INT64 */
3572 * Appends a double value to the message.
3574 * @param iter an iterator pointing to the end of the message
3575 * @param value the double value
3576 * @returns #TRUE on success
3579 dbus_message_iter_append_double (DBusMessageIter *iter,
3582 return dbus_message_iter_append_basic (iter, DBUS_TYPE_DOUBLE, &value);
3586 * Appends a UTF-8 string to the message.
3588 * @todo add return_val_if_fail(UTF-8 is valid)
3590 * @param iter an iterator pointing to the end of the message
3591 * @param value the string
3592 * @returns #TRUE on success
3595 dbus_message_iter_append_string (DBusMessageIter *iter,
3598 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3600 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3602 if (!dbus_message_iter_append_type (real, DBUS_TYPE_STRING))
3605 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
3607 _dbus_string_set_length (&real->message->body, real->pos);
3611 dbus_message_iter_append_done (real);
3617 * Appends an object path to the message.
3619 * @todo add return_val_if_fail(UTF-8 is valid)
3621 * @param iter an iterator pointing to the end of the message
3622 * @param value the object path
3623 * @returns #TRUE on success
3626 dbus_message_iter_append_object_path (DBusMessageIter *iter,
3629 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3631 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3633 if (!dbus_message_iter_append_type (real, DBUS_TYPE_OBJECT_PATH))
3636 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
3638 _dbus_string_set_length (&real->message->body, real->pos);
3642 dbus_message_iter_append_done (real);
3648 * Appends a custom type data chunk to the message. A custom
3649 * type is simply an arbitrary UTF-8 string used as a type
3650 * tag, plus an array of arbitrary bytes to be interpreted
3651 * according to the type tag.
3653 * @param iter an iterator pointing to the end of the message
3654 * @param name the name of the type
3655 * @param data the binary data used to store the value
3656 * @param len the length of the binary data in bytes
3657 * @returns #TRUE on success
3660 dbus_message_iter_append_custom (DBusMessageIter *iter,
3662 const unsigned char *data,
3665 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3667 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3669 if (!dbus_message_iter_append_type (real, DBUS_TYPE_CUSTOM))
3672 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, name))
3674 _dbus_string_set_length (&real->message->body, real->pos);
3678 if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, data, len))
3680 _dbus_string_set_length (&real->message->body, real->pos);
3684 dbus_message_iter_append_done (real);
3691 * Appends a dict key name to the message. The iterator used
3692 * must point to a dict.
3694 * @param iter an iterator pointing to the end of the message
3695 * @param value the string
3696 * @returns #TRUE on success
3699 dbus_message_iter_append_dict_key (DBusMessageIter *iter,
3702 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3704 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3705 _dbus_assert (real->type == DBUS_MESSAGE_ITER_TYPE_DICT);
3707 if (real->wrote_dict_key)
3709 _dbus_warn ("Appending multiple dict key names\n");
3713 if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
3718 dbus_message_iter_append_done (real);
3719 real->wrote_dict_key = TRUE;
3725 array_iter_type_mark_done (DBusMessageRealIter *iter)
3729 if (iter->type == DBUS_MESSAGE_ITER_TYPE_ARRAY)
3730 array_iter_type_mark_done (iter->parent_iter);
3734 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&iter->message->body),
3735 sizeof (dbus_uint32_t));
3737 /* Empty length for now, backfill later */
3738 if (!_dbus_marshal_uint32 (&iter->message->body, iter->message->byte_order, 0))
3740 _dbus_string_set_length (&iter->message->body, iter->pos);
3744 iter->container_start = _dbus_string_get_length (&iter->message->body);
3745 iter->container_length_pos = len_pos;
3746 iter->array_type_done = TRUE;
3752 append_array_type (DBusMessageRealIter *real,
3754 dbus_bool_t *array_type_done,
3755 int *array_type_pos)
3757 int existing_element_type;
3759 if (!dbus_message_iter_append_type (real, DBUS_TYPE_ARRAY))
3762 if (real->type == DBUS_MESSAGE_ITER_TYPE_ARRAY &&
3763 real->array_type_done)
3765 existing_element_type = iter_get_array_type (real, array_type_pos);
3766 if (existing_element_type != element_type)
3768 _dbus_warn ("Appending array of %s, when expecting array of %s\n",
3769 _dbus_type_to_string (element_type),
3770 _dbus_type_to_string (existing_element_type));
3771 _dbus_string_set_length (&real->message->body, real->pos);
3774 if (array_type_done != NULL)
3775 *array_type_done = TRUE;
3779 if (array_type_pos != NULL)
3780 *array_type_pos = _dbus_string_get_length (&real->message->body);
3783 if (!_dbus_message_append_byte_to_signature (real->message, element_type))
3785 _dbus_string_set_length (&real->message->body, real->pos);
3789 /* Append element type */
3790 if (!_dbus_string_append_byte (&real->message->body, element_type))
3792 _dbus_message_remove_byte_from_signature (real->message);
3793 _dbus_string_set_length (&real->message->body, real->pos);
3797 if (array_type_done != NULL)
3798 *array_type_done = element_type != DBUS_TYPE_ARRAY;
3800 if (element_type != DBUS_TYPE_ARRAY &&
3801 !array_iter_type_mark_done (real))
3803 _dbus_message_remove_byte_from_signature (real->message);
3812 * Appends an array to the message and initializes an iterator that
3813 * can be used to append to the array.
3815 * @param iter an iterator pointing to the end of the message
3816 * @param array_iter pointer to an iter that will be initialized
3817 * @param element_type the type of the array elements
3818 * @returns #TRUE on success
3821 dbus_message_iter_append_array (DBusMessageIter *iter,
3822 DBusMessageIter *array_iter,
3825 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3826 DBusMessageRealIter *array_real = (DBusMessageRealIter *)array_iter;
3829 dbus_bool_t array_type_done;
3831 if (element_type == DBUS_TYPE_NIL)
3833 _dbus_warn ("Can't create NIL arrays\n");
3837 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3839 if (!append_array_type (real, element_type, &array_type_done, &array_type_pos))
3842 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
3844 if (array_type_done)
3846 /* Empty length for now, backfill later */
3847 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
3849 _dbus_string_set_length (&real->message->body, real->pos);
3854 array_real->parent_iter = real;
3855 array_real->message = real->message;
3856 array_real->changed_stamp = real->message->changed_stamp;
3858 array_real->type = DBUS_MESSAGE_ITER_TYPE_ARRAY;
3859 array_real->pos = _dbus_string_get_length (&real->message->body);
3860 array_real->end = array_real->end;
3862 array_real->container_start = array_real->pos;
3863 array_real->container_length_pos = len_pos;
3864 array_real->wrote_dict_key = 0;
3865 array_real->array_type_done = array_type_done;
3866 array_real->array_type_pos = array_type_pos;
3868 dbus_message_iter_append_done (array_real);
3874 * Appends a dict to the message and initializes an iterator that
3875 * can be used to append to the dict.
3877 * @param iter an iterator pointing to the end of the message
3878 * @param dict_iter pointer to an iter that will be initialized
3879 * @returns #TRUE on success
3882 dbus_message_iter_append_dict (DBusMessageIter *iter,
3883 DBusMessageIter *dict_iter)
3885 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3886 DBusMessageRealIter *dict_real = (DBusMessageRealIter *)dict_iter;
3889 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3891 if (!dbus_message_iter_append_type (real, DBUS_TYPE_DICT))
3894 len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
3896 /* Empty length for now, backfill later */
3897 if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
3899 _dbus_string_set_length (&real->message->body, real->pos);
3903 dict_real->parent_iter = real;
3904 dict_real->message = real->message;
3905 dict_real->changed_stamp = real->message->changed_stamp;
3907 dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
3908 dict_real->pos = _dbus_string_get_length (&real->message->body);
3909 dict_real->end = dict_real->end;
3911 dict_real->container_start = dict_real->pos;
3912 dict_real->container_length_pos = len_pos;
3913 dict_real->wrote_dict_key = 0;
3915 dbus_message_iter_append_done (dict_real);
3917 real->wrote_dict_key = FALSE;
3923 _dbus_message_iter_append_basic_array (DBusMessageIter *iter,
3928 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3930 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
3932 if (!append_array_type (real, type, NULL, NULL))
3935 if (!_dbus_marshal_basic_type_array (&real->message->body,
3937 real->message->byte_order))
3939 _dbus_string_set_length (&real->message->body, real->pos);
3943 dbus_message_iter_append_done (real);
3950 * This function takes a va_list for use by language bindings.
3951 * It's otherwise the same as dbus_message_append_args().
3953 * @todo: Shouldn't this function clean up the changes to the message
3954 * on failures? (Yes)
3956 * @see dbus_message_append_args.
3957 * @param message the message
3958 * @param first_arg_type type of first argument
3959 * @param var_args value of first argument, then list of type/value pairs
3960 * @returns #TRUE on success
3963 dbus_message_append_args_valist (DBusMessage *message,
3968 DBusMessageIter iter;
3970 _dbus_return_val_if_fail (message != NULL, FALSE);
3972 old_len = _dbus_string_get_length (&message->body);
3974 type = first_arg_type;
3976 dbus_message_append_iter_init (message, &iter);
3978 while (type != DBUS_TYPE_INVALID)
3983 if (!dbus_message_iter_append_nil (&iter))
3986 case DBUS_TYPE_BYTE:
3987 /* Read an int from varargs, because the original unsigned
3988 * char has been promoted to int. */
3989 if (!dbus_message_iter_append_byte (&iter, va_arg (var_args, int)))
3992 case DBUS_TYPE_BOOLEAN:
3993 if (!dbus_message_iter_append_boolean (&iter, va_arg (var_args, dbus_bool_t)))
3996 case DBUS_TYPE_INT32:
3997 /* FIXME this is probably wrong, because an int passed in probably gets
3998 * converted to plain "int" not necessarily 32-bit.
4000 if (!dbus_message_iter_append_int32 (&iter, va_arg (var_args, dbus_int32_t)))
4003 case DBUS_TYPE_UINT32:
4004 /* FIXME this is probably wrong, because an int passed in probably gets
4005 * converted to plain "int" not necessarily 32-bit.
4007 if (!dbus_message_iter_append_uint32 (&iter, va_arg (var_args, dbus_uint32_t)))
4010 #ifdef DBUS_HAVE_INT64
4011 case DBUS_TYPE_INT64:
4012 if (!dbus_message_iter_append_int64 (&iter, va_arg (var_args, dbus_int64_t)))
4015 case DBUS_TYPE_UINT64:
4016 if (!dbus_message_iter_append_uint64 (&iter, va_arg (var_args, dbus_uint64_t)))
4019 #endif /* DBUS_HAVE_INT64 */
4020 case DBUS_TYPE_DOUBLE:
4021 if (!dbus_message_iter_append_double (&iter, va_arg (var_args, double)))
4024 case DBUS_TYPE_STRING:
4025 if (!dbus_message_iter_append_string (&iter, va_arg (var_args, const char *)))
4028 case DBUS_TYPE_OBJECT_PATH:
4029 if (!dbus_message_iter_append_object_path (&iter, va_arg (var_args, const char*)))
4032 case DBUS_TYPE_CUSTOM:
4035 unsigned char *data;
4038 name = va_arg (var_args, const char *);
4039 data = va_arg (var_args, unsigned char *);
4040 len = va_arg (var_args, int);
4042 if (!dbus_message_iter_append_custom (&iter, name, data, len))
4046 case DBUS_TYPE_ARRAY:
4051 type = va_arg (var_args, int);
4052 data = va_arg (var_args, void *);
4053 len = va_arg (var_args, int);
4057 case DBUS_TYPE_BYTE:
4058 case DBUS_TYPE_BOOLEAN:
4059 case DBUS_TYPE_INT32:
4060 case DBUS_TYPE_UINT32:
4061 #ifdef DBUS_HAVE_INT64
4062 case DBUS_TYPE_INT64:
4063 case DBUS_TYPE_UINT64:
4064 #endif /* DBUS_HAVE_INT64 */
4065 case DBUS_TYPE_DOUBLE:
4066 if (!_dbus_message_iter_append_basic_array (&iter, type, data, len))
4069 case DBUS_TYPE_STRING:
4070 if (!dbus_message_iter_append_string_array (&iter, (const char **)data, len))
4073 case DBUS_TYPE_OBJECT_PATH:
4074 if (!dbus_message_iter_append_object_path_array (&iter, (const char **)data, len))
4078 case DBUS_TYPE_ARRAY:
4079 case DBUS_TYPE_CUSTOM:
4080 case DBUS_TYPE_DICT:
4081 _dbus_warn ("dbus_message_append_args_valist doesn't support recursive arrays\n");
4084 _dbus_warn ("Unknown field type %d\n", type);
4090 case DBUS_TYPE_DICT:
4091 _dbus_warn ("dbus_message_append_args_valist doesn't support dicts\n");
4094 _dbus_warn ("Unknown field type %d\n", type);
4098 type = va_arg (var_args, int);
4108 * Appends a boolean array to the message.
4110 * @param iter an iterator pointing to the end of the message
4111 * @param value the array
4112 * @param len the length of the array
4113 * @returns #TRUE on success
4116 dbus_message_iter_append_boolean_array (DBusMessageIter *iter,
4117 unsigned const char *value,
4120 return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_BOOLEAN,
4125 * Appends a 32 bit signed integer array to the message.
4127 * @param iter an iterator pointing to the end of the message
4128 * @param value the array
4129 * @param len the length of the array
4130 * @returns #TRUE on success
4133 dbus_message_iter_append_int32_array (DBusMessageIter *iter,
4134 const dbus_int32_t *value,
4137 return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_INT32,
4142 * Appends a 32 bit unsigned integer array to the message.
4144 * @param iter an iterator pointing to the end of the message
4145 * @param value the array
4146 * @param len the length of the array
4147 * @returns #TRUE on success
4150 dbus_message_iter_append_uint32_array (DBusMessageIter *iter,
4151 const dbus_uint32_t *value,
4154 return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_UINT32,
4158 #ifdef DBUS_HAVE_INT64
4161 * Appends a 64 bit signed integer array to the message.
4163 * This function only exists if #DBUS_HAVE_INT64 is defined.
4165 * @param iter an iterator pointing to the end of the message
4166 * @param value the array
4167 * @param len the length of the array
4168 * @returns #TRUE on success
4171 dbus_message_iter_append_int64_array (DBusMessageIter *iter,
4172 const dbus_int64_t *value,
4175 return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_INT64,
4180 * Appends a 64 bit unsigned integer array to the message.
4182 * This function only exists if #DBUS_HAVE_INT64 is defined.
4184 * @param iter an iterator pointing to the end of the message
4185 * @param value the array
4186 * @param len the length of the array
4187 * @returns #TRUE on success
4190 dbus_message_iter_append_uint64_array (DBusMessageIter *iter,
4191 const dbus_uint64_t *value,
4194 return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_UINT64,
4197 #endif /* DBUS_HAVE_INT64 */
4200 * Appends a double array to the message.
4202 * @param iter an iterator pointing to the end of the message
4203 * @param value the array
4204 * @param len the length of the array
4205 * @returns #TRUE on success
4208 dbus_message_iter_append_double_array (DBusMessageIter *iter,
4209 const double *value,
4212 return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_DOUBLE,
4217 * Appends a byte array to the message.
4219 * @param iter an iterator pointing to the end of the message
4220 * @param value the array
4221 * @param len the length of the array
4222 * @returns #TRUE on success
4225 dbus_message_iter_append_byte_array (DBusMessageIter *iter,
4226 unsigned const char *value,
4229 return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_BYTE,
4234 * Appends a string array to the message.
4236 * @param iter an iterator pointing to the end of the message
4237 * @param value the array
4238 * @param len the length of the array
4239 * @returns #TRUE on success
4242 dbus_message_iter_append_string_array (DBusMessageIter *iter,
4246 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4248 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4250 if (!append_array_type (real, DBUS_TYPE_STRING, NULL, NULL))
4253 if (!_dbus_marshal_string_array (&real->message->body, real->message->byte_order, value, len))
4255 _dbus_string_set_length (&real->message->body, real->pos);
4259 dbus_message_iter_append_done (real);
4265 * Appends an object path array to the message.
4267 * @param iter an iterator pointing to the end of the message
4268 * @param value the array
4269 * @param len the length of the array
4270 * @returns #TRUE on success
4273 dbus_message_iter_append_object_path_array (DBusMessageIter *iter,
4277 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
4279 _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
4281 if (!append_array_type (real, DBUS_TYPE_OBJECT_PATH, NULL, NULL))
4284 if (!_dbus_marshal_string_array (&real->message->body, real->message->byte_order, value, len))
4286 _dbus_string_set_length (&real->message->body, real->pos);
4290 dbus_message_iter_append_done (real);
4296 * Sets the message sender.
4298 * @param message the message
4299 * @param sender the sender or #NULL to unset
4300 * @returns #FALSE if not enough memory
4303 dbus_message_set_sender (DBusMessage *message,
4306 _dbus_return_val_if_fail (message != NULL, FALSE);
4307 _dbus_return_val_if_fail (!message->locked, FALSE);
4309 return set_string_field (message,
4310 DBUS_HEADER_FIELD_SENDER,
4316 * Sets a flag indicating that the message does not want a reply; if
4317 * this flag is set, the other end of the connection may (but is not
4318 * required to) optimize by not sending method return or error
4319 * replies. If this flag is set, there is no way to know whether the
4320 * message successfully arrived at the remote end.
4322 * @param message the message
4323 * @param no_reply #TRUE if no reply is desired
4326 dbus_message_set_no_reply (DBusMessage *message,
4327 dbus_bool_t no_reply)
4331 _dbus_return_if_fail (message != NULL);
4332 _dbus_return_if_fail (!message->locked);
4334 header = _dbus_string_get_data_len (&message->header, FLAGS_OFFSET, 1);
4337 *header |= DBUS_HEADER_FLAG_NO_REPLY_EXPECTED;
4339 *header &= ~DBUS_HEADER_FLAG_NO_REPLY_EXPECTED;
4343 * Returns #TRUE if the message does not expect
4346 * @param message the message
4347 * @returns #TRUE if the message sender isn't waiting for a reply
4350 dbus_message_get_no_reply (DBusMessage *message)
4354 _dbus_return_val_if_fail (message != NULL, FALSE);
4356 header = _dbus_string_get_const_data_len (&message->header, FLAGS_OFFSET, 1);
4358 return (*header & DBUS_HEADER_FLAG_NO_REPLY_EXPECTED) != 0;
4363 * Sets a flag indicating that the addressed service will be auto-activated
4364 * before the message is delivered. When this flag is set, the message is held
4365 * until the service is succesfully activated or fail to activate. In case of
4366 * failure, the reply will be an activation error.
4368 * @param message the message
4369 * @param auto_activation #TRUE if auto-activation is desired
4372 dbus_message_set_auto_activation (DBusMessage *message,
4373 dbus_bool_t auto_activation)
4377 _dbus_return_if_fail (message != NULL);
4378 _dbus_return_if_fail (!message->locked);
4380 header = _dbus_string_get_data_len (&message->header, FLAGS_OFFSET, 1);
4382 if (auto_activation)
4383 *header |= DBUS_HEADER_FLAG_AUTO_ACTIVATION;
4385 *header &= ~DBUS_HEADER_FLAG_AUTO_ACTIVATION;
4389 * Returns #TRUE if the message will cause the addressed service to be
4392 * @param message the message
4393 * @returns #TRUE if the message will use auto-activation
4396 dbus_message_get_auto_activation (DBusMessage *message)
4400 _dbus_return_val_if_fail (message != NULL, FALSE);
4402 header = _dbus_string_get_const_data_len (&message->header, FLAGS_OFFSET, 1);
4404 return (*header & DBUS_HEADER_FLAG_AUTO_ACTIVATION) != 0;
4408 * Gets the service which originated this message,
4409 * or #NULL if unknown or inapplicable.
4411 * @param message the message
4412 * @returns the service name or #NULL
4415 dbus_message_get_sender (DBusMessage *message)
4417 _dbus_return_val_if_fail (message != NULL, NULL);
4419 return get_string_field (message,
4420 DBUS_HEADER_FIELD_SENDER,
4425 * Gets the type signature of the message, i.e. the arguments in the
4426 * message payload. The signature includes only "in" arguments for
4427 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
4428 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
4429 * what you might expect (it does not include the signature of the
4430 * entire C++-style method).
4432 * The signature is a string made up of type codes such
4433 * as #DBUS_TYPE_STRING. The string is terminated with nul
4434 * (nul is also the value of #DBUS_TYPE_INVALID).
4436 * @param message the message
4437 * @returns the type signature
4440 dbus_message_get_signature (DBusMessage *message)
4442 _dbus_return_val_if_fail (message != NULL, NULL);
4444 return get_string_field (message,
4445 DBUS_HEADER_FIELD_SIGNATURE,
4450 _dbus_message_has_type_interface_member (DBusMessage *message,
4452 const char *interface,
4457 _dbus_assert (message != NULL);
4458 _dbus_assert (interface != NULL);
4459 _dbus_assert (method != NULL);
4461 if (dbus_message_get_type (message) != type)
4464 /* Optimize by checking the short method name first
4465 * instead of the longer interface name
4468 n = dbus_message_get_member (message);
4470 if (n && strcmp (n, method) == 0)
4472 n = dbus_message_get_interface (message);
4474 if (n && strcmp (n, interface) == 0)
4482 * Checks whether the message is a method call with the given
4483 * interface and member fields. If the message is not
4484 * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or member field,
4487 * @param message the message
4488 * @param interface the name to check (must not be #NULL)
4489 * @param method the name to check (must not be #NULL)
4491 * @returns #TRUE if the message is the specified method call
4494 dbus_message_is_method_call (DBusMessage *message,
4495 const char *interface,
4498 _dbus_return_val_if_fail (message != NULL, FALSE);
4499 _dbus_return_val_if_fail (interface != NULL, FALSE);
4500 _dbus_return_val_if_fail (method != NULL, FALSE);
4502 return _dbus_message_has_type_interface_member (message,
4503 DBUS_MESSAGE_TYPE_METHOD_CALL,
4508 * Checks whether the message is a signal with the given
4509 * interface and member fields. If the message is not
4510 * #DBUS_MESSAGE_TYPE_SIGNAL, or has a different interface or member field,
4513 * @param message the message
4514 * @param interface the name to check (must not be #NULL)
4515 * @param signal_name the name to check (must not be #NULL)
4517 * @returns #TRUE if the message is the specified signal
4520 dbus_message_is_signal (DBusMessage *message,
4521 const char *interface,
4522 const char *signal_name)
4524 _dbus_return_val_if_fail (message != NULL, FALSE);
4525 _dbus_return_val_if_fail (interface != NULL, FALSE);
4526 _dbus_return_val_if_fail (signal_name != NULL, FALSE);
4528 return _dbus_message_has_type_interface_member (message,
4529 DBUS_MESSAGE_TYPE_SIGNAL,
4530 interface, signal_name);
4534 * Checks whether the message is an error reply with the given error
4535 * name. If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
4536 * different name, returns #FALSE.
4538 * @param message the message
4539 * @param error_name the name to check (must not be #NULL)
4541 * @returns #TRUE if the message is the specified error
4544 dbus_message_is_error (DBusMessage *message,
4545 const char *error_name)
4549 _dbus_return_val_if_fail (message != NULL, FALSE);
4550 _dbus_return_val_if_fail (error_name != NULL, FALSE);
4551 _dbus_return_val_if_fail (is_valid_error_name (error_name), FALSE);
4553 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
4556 n = dbus_message_get_error_name (message);
4558 if (n && strcmp (n, error_name) == 0)
4565 * Checks whether the message was sent to the given service. If the
4566 * message has no service specified or has a different name, returns
4569 * @param message the message
4570 * @param service the service to check (must not be #NULL)
4572 * @returns #TRUE if the message has the given destination service
4575 dbus_message_has_destination (DBusMessage *message,
4576 const char *service)
4580 _dbus_return_val_if_fail (message != NULL, FALSE);
4581 _dbus_return_val_if_fail (service != NULL, FALSE);
4583 s = dbus_message_get_destination (message);
4585 if (s && strcmp (s, service) == 0)
4592 * Checks whether the message has the given service as its sender. If
4593 * the message has no sender specified or has a different sender,
4594 * returns #FALSE. Note that if a peer application owns multiple
4595 * services, its messages will have only one of those services as the
4596 * sender (usually the base service). So you can't use this
4597 * function to prove the sender didn't own service Foo, you can
4598 * only use it to prove that it did.
4600 * @param message the message
4601 * @param service the service to check (must not be #NULL)
4603 * @returns #TRUE if the message has the given origin service
4606 dbus_message_has_sender (DBusMessage *message,
4607 const char *service)
4611 _dbus_return_val_if_fail (message != NULL, FALSE);
4612 _dbus_return_val_if_fail (service != NULL, FALSE);
4614 s = dbus_message_get_sender (message);
4616 if (s && strcmp (s, service) == 0)
4623 * Checks whether the message has the given signature;
4624 * see dbus_message_get_signature() for more details on
4625 * what the signature looks like.
4627 * @param message the message
4628 * @param signature typecode array
4629 * @returns #TRUE if message has the given signature
4632 dbus_message_has_signature (DBusMessage *message,
4633 const char *signature)
4637 _dbus_return_val_if_fail (message != NULL, FALSE);
4638 _dbus_return_val_if_fail (signature != NULL, FALSE);
4640 s = dbus_message_get_signature (message);
4642 if (s && strcmp (s, signature) == 0)
4649 * Sets a #DBusError based on the contents of the given
4650 * message. The error is only set if the message
4651 * is an error message, as in DBUS_MESSAGE_TYPE_ERROR.
4652 * The name of the error is set to the name of the message,
4653 * and the error message is set to the first argument
4654 * if the argument exists and is a string.
4656 * The return value indicates whether the error was set (the error is
4657 * set if and only if the message is an error message).
4658 * So you can check for an error reply and convert it to DBusError
4661 * @param error the error to set
4662 * @param message the message to set it from
4663 * @returns #TRUE if dbus_message_get_is_error() returns #TRUE for the message
4666 dbus_set_error_from_message (DBusError *error,
4667 DBusMessage *message)
4671 _dbus_return_val_if_fail (message != NULL, FALSE);
4672 _dbus_return_val_if_error_is_set (error, FALSE);
4674 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
4678 dbus_message_get_args (message, NULL,
4679 DBUS_TYPE_STRING, &str,
4682 dbus_set_error (error, dbus_message_get_error_name (message),
4683 str ? "%s" : NULL, str);
4693 * @addtogroup DBusMessageInternals
4698 * @typedef DBusMessageLoader
4700 * The DBusMessageLoader object encapsulates the process of converting
4701 * a byte stream into a series of DBusMessage. It buffers the incoming
4702 * bytes as efficiently as possible, and generates a queue of
4703 * messages. DBusMessageLoader is typically used as part of a
4704 * DBusTransport implementation. The DBusTransport then hands off
4705 * the loaded messages to a DBusConnection, making the messages
4706 * visible to the application.
4708 * @todo write tests for break-loader that a) randomly delete header
4709 * fields and b) set string fields to zero-length and other funky
4714 /* we definitely use signed ints for sizes, so don't exceed
4715 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
4716 * over 128M is pretty nuts anyhow.
4720 * The maximum sane message size.
4722 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
4725 * Implementation details of DBusMessageLoader.
4726 * All members are private.
4728 struct DBusMessageLoader
4730 int refcount; /**< Reference count. */
4732 DBusString data; /**< Buffered data */
4734 DBusList *messages; /**< Complete messages. */
4736 long max_message_size; /**< Maximum size of a message */
4738 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
4740 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
4744 * The initial buffer size of the message loader.
4746 * @todo this should be based on min header size plus some average
4747 * body size, or something. Or rather, the min header size only, if we
4748 * want to try to read only the header, store that in a DBusMessage,
4749 * then read only the body and store that, etc., depends on
4750 * how we optimize _dbus_message_loader_get_buffer() and what
4751 * the exact message format is.
4753 #define INITIAL_LOADER_DATA_LEN 32
4756 * Creates a new message loader. Returns #NULL if memory can't
4759 * @returns new loader, or #NULL.
4762 _dbus_message_loader_new (void)
4764 DBusMessageLoader *loader;
4766 loader = dbus_new0 (DBusMessageLoader, 1);
4770 loader->refcount = 1;
4772 /* Try to cap message size at something that won't *totally* hose
4773 * the system if we have a couple of them.
4775 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
4777 if (!_dbus_string_init (&loader->data))
4783 /* preallocate the buffer for speed, ignore failure */
4784 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
4785 _dbus_string_set_length (&loader->data, 0);
4791 * Increments the reference count of the loader.
4793 * @param loader the loader.
4794 * @returns the loader
4797 _dbus_message_loader_ref (DBusMessageLoader *loader)
4799 loader->refcount += 1;
4805 * Decrements the reference count of the loader and finalizes the
4806 * loader when the count reaches zero.
4808 * @param loader the loader.
4811 _dbus_message_loader_unref (DBusMessageLoader *loader)
4813 loader->refcount -= 1;
4814 if (loader->refcount == 0)
4816 _dbus_list_foreach (&loader->messages,
4817 (DBusForeachFunction) dbus_message_unref,
4819 _dbus_list_clear (&loader->messages);
4820 _dbus_string_free (&loader->data);
4826 * Gets the buffer to use for reading data from the network. Network
4827 * data is read directly into an allocated buffer, which is then used
4828 * in the DBusMessage, to avoid as many extra memcpy's as possible.
4829 * The buffer must always be returned immediately using
4830 * _dbus_message_loader_return_buffer(), even if no bytes are
4831 * successfully read.
4833 * @todo this function can be a lot more clever. For example
4834 * it can probably always return a buffer size to read exactly
4835 * the body of the next message, thus avoiding any memory wastage
4838 * @todo we need to enforce a max length on strings in header fields.
4840 * @param loader the message loader.
4841 * @param buffer the buffer
4844 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
4845 DBusString **buffer)
4847 _dbus_assert (!loader->buffer_outstanding);
4849 *buffer = &loader->data;
4851 loader->buffer_outstanding = TRUE;
4855 * The smallest header size that can occur.
4856 * (It won't be valid)
4858 #define DBUS_MINIMUM_HEADER_SIZE 16
4861 decode_string_field (const DBusString *data,
4863 HeaderField *header_field,
4864 DBusString *field_data,
4868 int string_data_pos;
4870 _dbus_assert (header_field != NULL);
4871 _dbus_assert (field_data != NULL);
4873 if (header_field->name_offset >= 0)
4875 _dbus_verbose ("%s field provided twice\n",
4876 _dbus_header_field_to_string (field));
4880 if (type != DBUS_TYPE_STRING)
4882 _dbus_verbose ("%s field has wrong type %s\n",
4883 _dbus_header_field_to_string (field),
4884 _dbus_type_to_string (type));
4888 /* skip padding after typecode, skip string length;
4889 * we assume that the string arg has already been validated
4890 * for sanity and UTF-8
4892 string_data_pos = _DBUS_ALIGN_VALUE (pos, 4) + 4;
4893 _dbus_assert (string_data_pos < _dbus_string_get_length (data));
4895 _dbus_string_init_const (field_data,
4896 _dbus_string_get_const_data (data) + string_data_pos);
4898 header_field->name_offset = pos - 2;
4899 header_field->value_offset = _DBUS_ALIGN_VALUE (pos, 4);
4902 _dbus_verbose ("Found field %s at offset %d\n",
4903 _dbus_header_field_to_string (field),
4904 header_field->value_offset);
4910 /* FIXME because the service/interface/member/error names are already
4911 * validated to be in the particular ASCII subset, UTF-8 validating
4912 * them could be skipped as a probably-interesting optimization.
4913 * The UTF-8 validation definitely shows up in profiles.
4916 decode_header_data (const DBusString *data,
4920 HeaderField fields[DBUS_HEADER_FIELD_LAST + 1],
4921 int *message_padding)
4923 DBusString field_data;
4928 dbus_bool_t signature_required;
4930 if (header_len < 16)
4932 _dbus_verbose ("Header length %d is too short\n", header_len);
4937 while (i <= DBUS_HEADER_FIELD_LAST)
4939 fields[i].name_offset = -1;
4940 fields[i].value_offset = -1;
4945 while (pos < header_len)
4947 field = _dbus_string_get_byte (data, pos);
4948 if (field == DBUS_HEADER_FIELD_INVALID)
4949 break; /* Must be padding */
4952 if (!_dbus_marshal_validate_type (data, pos, &type, &pos))
4954 _dbus_verbose ("Failed to validate type of named header field pos = %d\n",
4959 if (!_dbus_marshal_validate_arg (data, byte_order, 0, type, -1, pos, &new_pos))
4961 _dbus_verbose ("Failed to validate argument to named header field pos = %d\n",
4966 if (new_pos > header_len)
4968 _dbus_verbose ("Named header field tries to extend beyond header length\n");
4974 case DBUS_HEADER_FIELD_DESTINATION:
4975 if (!decode_string_field (data, field, &fields[field],
4976 &field_data, pos, type))
4979 if (!_dbus_string_validate_service (&field_data, 0,
4980 _dbus_string_get_length (&field_data)))
4982 _dbus_verbose ("service field has invalid content \"%s\"\n",
4983 _dbus_string_get_const_data (&field_data));
4988 case DBUS_HEADER_FIELD_INTERFACE:
4989 if (!decode_string_field (data, field, &fields[field],
4990 &field_data, pos, type))
4993 if (!_dbus_string_validate_interface (&field_data, 0,
4994 _dbus_string_get_length (&field_data)))
4996 _dbus_verbose ("interface field has invalid content \"%s\"\n",
4997 _dbus_string_get_const_data (&field_data));
5001 if (_dbus_string_equal_c_str (&field_data,
5002 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL))
5004 _dbus_verbose ("Message is on the local interface\n");
5009 case DBUS_HEADER_FIELD_MEMBER:
5010 if (!decode_string_field (data, field, &fields[field],
5011 &field_data, pos, type))
5014 if (!_dbus_string_validate_member (&field_data, 0,
5015 _dbus_string_get_length (&field_data)))
5017 _dbus_verbose ("member field has invalid content \"%s\"\n",
5018 _dbus_string_get_const_data (&field_data));
5023 case DBUS_HEADER_FIELD_ERROR_NAME:
5024 if (!decode_string_field (data, field, &fields[field],
5025 &field_data, pos, type))
5028 if (!_dbus_string_validate_error_name (&field_data, 0,
5029 _dbus_string_get_length (&field_data)))
5031 _dbus_verbose ("error-name field has invalid content \"%s\"\n",
5032 _dbus_string_get_const_data (&field_data));
5037 case DBUS_HEADER_FIELD_SENDER:
5038 if (!decode_string_field (data, field, &fields[field],
5039 &field_data, pos, type))
5042 if (!_dbus_string_validate_service (&field_data, 0,
5043 _dbus_string_get_length (&field_data)))
5045 _dbus_verbose ("sender-service field has invalid content \"%s\"\n",
5046 _dbus_string_get_const_data (&field_data));
5051 case DBUS_HEADER_FIELD_PATH:
5053 /* Path was already validated as part of standard
5054 * type validation, since there's an OBJECT_PATH
5058 if (fields[field].name_offset >= 0)
5060 _dbus_verbose ("path field provided twice\n");
5063 if (type != DBUS_TYPE_OBJECT_PATH)
5065 _dbus_verbose ("path field has wrong type\n");
5069 fields[field].name_offset = pos - 2;
5070 fields[field].value_offset = _DBUS_ALIGN_VALUE (pos, 4);
5072 /* No forging signals from the local path */
5075 s = _dbus_string_get_const_data_len (data,
5076 fields[field].value_offset,
5077 _dbus_string_get_length (data) -
5078 fields[field].value_offset);
5079 if (strcmp (s, DBUS_PATH_ORG_FREEDESKTOP_LOCAL) == 0)
5081 _dbus_verbose ("Message is on the local path\n");
5086 _dbus_verbose ("Found path at offset %d\n",
5087 fields[field].value_offset);
5090 case DBUS_HEADER_FIELD_REPLY_SERIAL:
5091 if (fields[field].name_offset >= 0)
5093 _dbus_verbose ("reply field provided twice\n");
5097 if (type != DBUS_TYPE_UINT32)
5099 _dbus_verbose ("reply field has wrong type\n");
5103 fields[field].name_offset = pos - 2;
5104 fields[field].value_offset = _DBUS_ALIGN_VALUE (pos, 4);
5106 _dbus_verbose ("Found reply serial %u at offset %d\n",
5107 _dbus_demarshal_uint32 (data,
5109 fields[field].value_offset,
5111 fields[field].value_offset);
5114 case DBUS_HEADER_FIELD_SIGNATURE:
5115 if (!decode_string_field (data, field, &fields[field],
5116 &field_data, pos, type))
5121 if (!_dbus_string_validate_signature (&field_data, 0,
5122 _dbus_string_get_length (&field_data)))
5124 _dbus_verbose ("signature field has invalid content \"%s\"\n",
5125 _dbus_string_get_const_data (&field_data));
5132 _dbus_verbose ("Ignoring an unknown header field: %d at offset %d\n",
5139 if (pos < header_len)
5141 /* Alignment padding, verify that it's nul */
5142 if ((header_len - pos) >= 8)
5144 _dbus_verbose ("too much header alignment padding\n");
5148 if (!_dbus_string_validate_nul (data,
5149 pos, (header_len - pos)))
5151 _dbus_verbose ("header alignment padding is not nul\n");
5156 /* Depending on message type, enforce presence of certain fields. */
5157 signature_required = TRUE;
5159 switch (message_type)
5161 case DBUS_MESSAGE_TYPE_SIGNAL:
5162 case DBUS_MESSAGE_TYPE_METHOD_CALL:
5163 if (fields[DBUS_HEADER_FIELD_PATH].value_offset < 0)
5165 _dbus_verbose ("No path field provided\n");
5168 /* FIXME make this optional, only for method calls */
5169 if (fields[DBUS_HEADER_FIELD_INTERFACE].value_offset < 0)
5171 _dbus_verbose ("No interface field provided\n");
5174 if (fields[DBUS_HEADER_FIELD_MEMBER].value_offset < 0)
5176 _dbus_verbose ("No member field provided\n");
5180 case DBUS_MESSAGE_TYPE_ERROR:
5181 if (fields[DBUS_HEADER_FIELD_ERROR_NAME].value_offset < 0)
5183 _dbus_verbose ("No error-name field provided\n");
5186 if (fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset < 0)
5188 _dbus_verbose ("No reply serial field provided in error\n");
5192 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
5193 if (fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset < 0)
5195 _dbus_verbose ("No reply serial field provided in method return\n");
5200 /* An unknown type, spec requires us to ignore it */
5201 signature_required = FALSE;
5205 /* FIXME allow omitting signature field for a message with no arguments? */
5206 if (signature_required)
5208 if (fields[DBUS_HEADER_FIELD_SIGNATURE].value_offset < 0)
5210 _dbus_verbose ("No signature field provided\n");
5215 if (message_padding)
5216 *message_padding = header_len - pos;
5222 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
5223 * indicating to the loader how many bytes of the buffer were filled
5224 * in. This function must always be called, even if no bytes were
5225 * successfully read.
5227 * @param loader the loader.
5228 * @param buffer the buffer.
5229 * @param bytes_read number of bytes that were read into the buffer.
5232 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
5236 _dbus_assert (loader->buffer_outstanding);
5237 _dbus_assert (buffer == &loader->data);
5239 loader->buffer_outstanding = FALSE;
5243 load_one_message (DBusMessageLoader *loader,
5249 DBusMessage *message;
5250 HeaderField fields[DBUS_HEADER_FIELD_LAST + 1];
5260 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
5263 if (!decode_header_data (&loader->data,
5264 header_len, byte_order,
5266 fields, &header_padding))
5268 _dbus_verbose ("Header was invalid\n");
5269 loader->corrupted = TRUE;
5273 next_arg = header_len;
5274 while (next_arg < (header_len + body_len))
5277 int prev = next_arg;
5279 if (!_dbus_marshal_validate_type (&loader->data, next_arg,
5282 _dbus_verbose ("invalid typecode at offset %d\n", prev);
5283 loader->corrupted = TRUE;
5287 if (!_dbus_marshal_validate_arg (&loader->data,
5294 _dbus_verbose ("invalid type data at %d, next_arg\n", next_arg);
5295 loader->corrupted = TRUE;
5299 _dbus_assert (next_arg > prev);
5302 if (next_arg > (header_len + body_len))
5304 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
5305 next_arg, header_len, body_len,
5306 header_len + body_len);
5307 loader->corrupted = TRUE;
5311 message = dbus_message_new_empty_header ();
5312 if (message == NULL)
5314 _dbus_verbose ("Failed to allocate empty message\n");
5319 message->byte_order = byte_order;
5320 message->header_padding = header_padding;
5322 /* Copy in the offsets we found */
5324 while (i <= DBUS_HEADER_FIELD_LAST)
5326 message->header_fields[i] = fields[i];
5330 if (!_dbus_list_append (&loader->messages, message))
5332 _dbus_verbose ("Failed to append new message to loader queue\n");
5337 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
5338 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
5340 _dbus_assert (_dbus_string_get_length (&loader->data) >=
5341 (header_len + body_len));
5343 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
5345 _dbus_verbose ("Failed to move header into new message\n");
5350 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
5352 _dbus_verbose ("Failed to move body into new message\n");
5358 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
5359 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
5361 /* Fill in caches (we checked the types of these fields
5364 message->reply_serial = get_uint_field (message,
5365 DBUS_HEADER_FIELD_REPLY_SERIAL);
5367 message->client_serial = _dbus_demarshal_uint32 (&message->header,
5368 message->byte_order,
5369 CLIENT_SERIAL_OFFSET,
5371 if (message->client_serial == 0 ||
5372 (message->header_fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset >= 0 && message->reply_serial == 0))
5374 _dbus_verbose ("client_serial = %d reply_serial = %d, one of these no good\n",
5375 message->client_serial,
5376 message->reply_serial);
5378 loader->corrupted = TRUE;
5382 _dbus_verbose ("Loaded message %p\n", message);
5384 _dbus_assert (!oom);
5385 _dbus_assert (!loader->corrupted);
5393 if (message != NULL)
5395 /* Put the data back so we can try again later if it was an OOM issue */
5396 if (_dbus_string_get_length (&message->body) > 0)
5400 result = _dbus_string_copy_len (&message->body, 0, body_len,
5403 _dbus_assert (result); /* because DBusString never reallocs smaller */
5406 if (_dbus_string_get_length (&message->header) > 0)
5410 result = _dbus_string_copy_len (&message->header, 0, header_len,
5413 _dbus_assert (result); /* because DBusString never reallocs smaller */
5416 /* does nothing if the message isn't in the list */
5417 _dbus_list_remove_last (&loader->messages, message);
5419 dbus_message_unref (message);
5427 * Converts buffered data into messages.
5429 * @todo we need to check that the proper named header fields exist
5430 * for each message type.
5432 * @todo If a message has unknown type, we should probably eat it
5433 * right here rather than passing it out to applications. However
5434 * it's not an error to see messages of unknown type.
5436 * @param loader the loader.
5437 * @returns #TRUE if we had enough memory to finish.
5440 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
5442 while (!loader->corrupted && _dbus_string_get_length (&loader->data) >= 16)
5444 const char *header_data;
5445 int byte_order, message_type, header_len, body_len;
5446 dbus_uint32_t header_len_unsigned, body_len_unsigned;
5448 header_data = _dbus_string_get_const_data_len (&loader->data, 0, 16);
5450 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
5452 if (header_data[VERSION_OFFSET] != DBUS_MAJOR_PROTOCOL_VERSION)
5454 _dbus_verbose ("Message has protocol version %d ours is %d\n",
5455 (int) header_data[VERSION_OFFSET], DBUS_MAJOR_PROTOCOL_VERSION);
5456 loader->corrupted = TRUE;
5460 byte_order = header_data[BYTE_ORDER_OFFSET];
5462 if (byte_order != DBUS_LITTLE_ENDIAN &&
5463 byte_order != DBUS_BIG_ENDIAN)
5465 _dbus_verbose ("Message with bad byte order '%c' received\n",
5467 loader->corrupted = TRUE;
5471 /* Unknown types are ignored, but INVALID is
5474 message_type = header_data[TYPE_OFFSET];
5475 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
5477 _dbus_verbose ("Message with bad type '%d' received\n",
5479 loader->corrupted = TRUE;
5483 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
5484 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
5486 if (header_len_unsigned < 16)
5488 _dbus_verbose ("Message had broken too-small header length %u\n",
5489 header_len_unsigned);
5490 loader->corrupted = TRUE;
5494 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
5495 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
5497 _dbus_verbose ("Header or body length too large (%u %u)\n",
5498 header_len_unsigned,
5500 loader->corrupted = TRUE;
5504 /* Now that we know the values are in signed range, get
5505 * rid of stupid unsigned, just causes bugs
5507 header_len = header_len_unsigned;
5508 body_len = body_len_unsigned;
5510 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
5513 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
5515 loader->corrupted = TRUE;
5519 if (header_len + body_len > loader->max_message_size)
5521 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %ld\n",
5522 header_len, body_len, loader->max_message_size);
5523 loader->corrupted = TRUE;
5527 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
5529 if (!load_one_message (loader, byte_order, message_type,
5530 header_len, body_len))
5541 * Peeks at first loaded message, returns #NULL if no messages have
5544 * @param loader the loader.
5545 * @returns the next message, or #NULL if none.
5548 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
5550 if (loader->messages)
5551 return loader->messages->data;
5557 * Pops a loaded message (passing ownership of the message
5558 * to the caller). Returns #NULL if no messages have been
5561 * @param loader the loader.
5562 * @returns the next message, or #NULL if none.
5565 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
5567 return _dbus_list_pop_first (&loader->messages);
5571 * Pops a loaded message inside a list link (passing ownership of the
5572 * message and link to the caller). Returns #NULL if no messages have
5575 * @param loader the loader.
5576 * @returns the next message link, or #NULL if none.
5579 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
5581 return _dbus_list_pop_first_link (&loader->messages);
5585 * Returns a popped message link, used to undo a pop.
5587 * @param loader the loader
5588 * @param link the link with a message in it
5591 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
5594 _dbus_list_prepend_link (&loader->messages, link);
5598 * Checks whether the loader is confused due to bad data.
5599 * If messages are received that are invalid, the
5600 * loader gets confused and gives up permanently.
5601 * This state is called "corrupted."
5603 * @param loader the loader
5604 * @returns #TRUE if the loader is hosed.
5607 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
5609 return loader->corrupted;
5613 * Sets the maximum size message we allow.
5615 * @param loader the loader
5616 * @param size the max message size in bytes
5619 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
5622 if (size > MAX_SANE_MESSAGE_SIZE)
5624 _dbus_verbose ("clamping requested max message size %ld to %d\n",
5625 size, MAX_SANE_MESSAGE_SIZE);
5626 size = MAX_SANE_MESSAGE_SIZE;
5628 loader->max_message_size = size;
5632 * Gets the maximum allowed message size in bytes.
5634 * @param loader the loader
5635 * @returns max size in bytes
5638 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
5640 return loader->max_message_size;
5643 static DBusDataSlotAllocator slot_allocator;
5644 _DBUS_DEFINE_GLOBAL_LOCK (message_slots);
5647 * Allocates an integer ID to be used for storing application-specific
5648 * data on any DBusMessage. The allocated ID may then be used
5649 * with dbus_message_set_data() and dbus_message_get_data().
5650 * The passed-in slot must be initialized to -1, and is filled in
5651 * with the slot ID. If the passed-in slot is not -1, it's assumed
5652 * to be already allocated, and its refcount is incremented.
5654 * The allocated slot is global, i.e. all DBusMessage objects will
5655 * have a slot with the given integer ID reserved.
5657 * @param slot_p address of a global variable storing the slot
5658 * @returns #FALSE on failure (no memory)
5661 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
5663 return _dbus_data_slot_allocator_alloc (&slot_allocator,
5664 _DBUS_LOCK_NAME (message_slots),
5669 * Deallocates a global ID for message data slots.
5670 * dbus_message_get_data() and dbus_message_set_data() may no
5671 * longer be used with this slot. Existing data stored on existing
5672 * DBusMessage objects will be freed when the message is
5673 * finalized, but may not be retrieved (and may only be replaced if
5674 * someone else reallocates the slot). When the refcount on the
5675 * passed-in slot reaches 0, it is set to -1.
5677 * @param slot_p address storing the slot to deallocate
5680 dbus_message_free_data_slot (dbus_int32_t *slot_p)
5682 _dbus_return_if_fail (*slot_p >= 0);
5684 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
5688 * Stores a pointer on a DBusMessage, along
5689 * with an optional function to be used for freeing
5690 * the data when the data is set again, or when
5691 * the message is finalized. The slot number
5692 * must have been allocated with dbus_message_allocate_data_slot().
5694 * @param message the message
5695 * @param slot the slot number
5696 * @param data the data to store
5697 * @param free_data_func finalizer function for the data
5698 * @returns #TRUE if there was enough memory to store the data
5701 dbus_message_set_data (DBusMessage *message,
5704 DBusFreeFunction free_data_func)
5706 DBusFreeFunction old_free_func;
5710 _dbus_return_val_if_fail (message != NULL, FALSE);
5711 _dbus_return_val_if_fail (slot >= 0, FALSE);
5713 retval = _dbus_data_slot_list_set (&slot_allocator,
5714 &message->slot_list,
5715 slot, data, free_data_func,
5716 &old_free_func, &old_data);
5720 /* Do the actual free outside the message lock */
5722 (* old_free_func) (old_data);
5729 * Retrieves data previously set with dbus_message_set_data().
5730 * The slot must still be allocated (must not have been freed).
5732 * @param message the message
5733 * @param slot the slot to get data from
5734 * @returns the data, or #NULL if not found
5737 dbus_message_get_data (DBusMessage *message,
5742 _dbus_return_val_if_fail (message != NULL, NULL);
5744 res = _dbus_data_slot_list_get (&slot_allocator,
5745 &message->slot_list,
5752 * Utility function to convert a machine-readable (not translated)
5753 * string into a D-BUS message type.
5756 * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL
5757 * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN
5758 * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL
5759 * "error" -> DBUS_MESSAGE_TYPE_ERROR
5760 * anything else -> DBUS_MESSAGE_TYPE_INVALID
5765 dbus_message_type_from_string (const char *type_str)
5767 if (strcmp (type_str, "method_call") == 0)
5768 return DBUS_MESSAGE_TYPE_METHOD_CALL;
5769 if (strcmp (type_str, "method_return") == 0)
5770 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
5771 else if (strcmp (type_str, "signal") == 0)
5772 return DBUS_MESSAGE_TYPE_SIGNAL;
5773 else if (strcmp (type_str, "error") == 0)
5774 return DBUS_MESSAGE_TYPE_ERROR;
5776 return DBUS_MESSAGE_TYPE_INVALID;
5780 #ifdef DBUS_BUILD_TESTS
5781 #include "dbus-test.h"
5786 message_iter_test (DBusMessage *message)
5788 DBusMessageIter iter, dict, dict2, array, array2;
5790 unsigned char *data;
5791 dbus_int32_t *our_int_array;
5794 dbus_message_iter_init (message, &iter);
5797 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
5798 _dbus_assert_not_reached ("Argument type isn't string");
5800 str = dbus_message_iter_get_string (&iter);
5801 if (strcmp (str, "Test string") != 0)
5802 _dbus_assert_not_reached ("Strings differ");
5805 if (!dbus_message_iter_next (&iter))
5806 _dbus_assert_not_reached ("Reached end of arguments");
5808 /* Signed integer tests */
5809 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32)
5810 _dbus_assert_not_reached ("Argument type isn't int32");
5812 if (dbus_message_iter_get_int32 (&iter) != -0x12345678)
5813 _dbus_assert_not_reached ("Signed integers differ");
5815 if (!dbus_message_iter_next (&iter))
5816 _dbus_assert_not_reached ("Reached end of fields");
5818 /* Unsigned integer tests */
5819 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
5820 _dbus_assert_not_reached ("Argument type isn't int32");
5822 if (dbus_message_iter_get_uint32 (&iter) != 0xedd1e)
5823 _dbus_assert_not_reached ("Unsigned integers differ");
5825 if (!dbus_message_iter_next (&iter))
5826 _dbus_assert_not_reached ("Reached end of arguments");
5829 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DOUBLE)
5830 _dbus_assert_not_reached ("Argument type isn't double");
5832 if (dbus_message_iter_get_double (&iter) != 3.14159)
5833 _dbus_assert_not_reached ("Doubles differ");
5835 if (!dbus_message_iter_next (&iter))
5836 _dbus_assert_not_reached ("Reached end of arguments");
5838 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
5839 _dbus_assert_not_reached ("Argument type not an array");
5841 if (dbus_message_iter_get_array_type (&iter) != DBUS_TYPE_DOUBLE)
5842 _dbus_assert_not_reached ("Array type not double");
5845 dbus_message_iter_init_array_iterator (&iter, &array, NULL);
5847 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_DOUBLE)
5848 _dbus_assert_not_reached ("Argument type isn't double");
5850 if (dbus_message_iter_get_double (&array) != 1.5)
5851 _dbus_assert_not_reached ("Unsigned integers differ");
5853 if (!dbus_message_iter_next (&array))
5854 _dbus_assert_not_reached ("Reached end of arguments");
5856 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_DOUBLE)
5857 _dbus_assert_not_reached ("Argument type isn't double");
5859 if (dbus_message_iter_get_double (&array) != 2.5)
5860 _dbus_assert_not_reached ("Unsigned integers differ");
5862 if (dbus_message_iter_next (&array))
5863 _dbus_assert_not_reached ("Didn't reach end of arguments");
5865 if (!dbus_message_iter_next (&iter))
5866 _dbus_assert_not_reached ("Reached end of arguments");
5871 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
5872 _dbus_assert_not_reached ("not dict type");
5874 dbus_message_iter_init_dict_iterator (&iter, &dict);
5876 str = dbus_message_iter_get_dict_key (&dict);
5877 if (str == NULL || strcmp (str, "test") != 0)
5878 _dbus_assert_not_reached ("wrong dict key");
5881 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_UINT32)
5882 _dbus_assert_not_reached ("wrong dict entry type");
5884 if (dbus_message_iter_get_uint32 (&dict) != 0xDEADBEEF)
5885 _dbus_assert_not_reached ("wrong dict entry value");
5887 /* dict (in dict) */
5889 if (!dbus_message_iter_next (&dict))
5890 _dbus_assert_not_reached ("reached end of dict");
5892 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_DICT)
5893 _dbus_assert_not_reached ("not dict type");
5895 dbus_message_iter_init_dict_iterator (&dict, &dict2);
5897 str = dbus_message_iter_get_dict_key (&dict2);
5898 if (str == NULL || strcmp (str, "dictkey") != 0)
5899 _dbus_assert_not_reached ("wrong dict key");
5902 if (dbus_message_iter_get_arg_type (&dict2) != DBUS_TYPE_STRING)
5903 _dbus_assert_not_reached ("wrong dict entry type");
5905 str = dbus_message_iter_get_string (&dict2);
5906 if (str == NULL || strcmp (str, "dictvalue") != 0)
5907 _dbus_assert_not_reached ("wrong dict entry value");
5910 if (dbus_message_iter_next (&dict2))
5911 _dbus_assert_not_reached ("didn't reach end of dict");
5913 if (!dbus_message_iter_next (&dict))
5914 _dbus_assert_not_reached ("reached end of dict");
5916 /* array of array of int32 (in dict) */
5918 str = dbus_message_iter_get_dict_key (&dict);
5919 if (str == NULL || strcmp (str, "array") != 0)
5920 _dbus_assert_not_reached ("wrong dict key");
5923 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_ARRAY)
5924 _dbus_assert_not_reached ("Argument type not an array");
5926 if (dbus_message_iter_get_array_type (&dict) != DBUS_TYPE_ARRAY)
5927 _dbus_assert_not_reached ("Array type not array");
5929 dbus_message_iter_init_array_iterator (&dict, &array, NULL);
5931 if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_ARRAY)
5932 _dbus_assert_not_reached ("Argument type isn't array");
5934 if (dbus_message_iter_get_array_type (&array) != DBUS_TYPE_INT32)
5935 _dbus_assert_not_reached ("Array type not int32");
5937 dbus_message_iter_init_array_iterator (&array, &array2, NULL);
5939 if (dbus_message_iter_get_arg_type (&array2) != DBUS_TYPE_INT32)
5940 _dbus_assert_not_reached ("Argument type isn't int32");
5942 if (dbus_message_iter_get_int32 (&array2) != 0x12345678)
5943 _dbus_assert_not_reached ("Signed integers differ");
5945 if (!dbus_message_iter_next (&array2))
5946 _dbus_assert_not_reached ("Reached end of arguments");
5948 if (dbus_message_iter_get_int32 (&array2) != 0x23456781)
5949 _dbus_assert_not_reached ("Signed integers differ");
5951 if (dbus_message_iter_next (&array2))
5952 _dbus_assert_not_reached ("Didn't reached end of arguments");
5954 if (!dbus_message_iter_next (&array))
5955 _dbus_assert_not_reached ("Reached end of arguments");
5957 if (dbus_message_iter_get_array_type (&array) != DBUS_TYPE_INT32)
5958 _dbus_assert_not_reached ("Array type not int32");
5960 if (!dbus_message_iter_get_int32_array (&array,
5963 _dbus_assert_not_reached ("couldn't get int32 array");
5965 _dbus_assert (len == 3);
5966 _dbus_assert (our_int_array[0] == 0x34567812 &&
5967 our_int_array[1] == 0x45678123 &&
5968 our_int_array[2] == 0x56781234);
5969 dbus_free (our_int_array);
5971 if (dbus_message_iter_next (&array))
5972 _dbus_assert_not_reached ("Didn't reach end of array");
5974 if (dbus_message_iter_next (&dict))
5975 _dbus_assert_not_reached ("Didn't reach end of dict");
5977 if (!dbus_message_iter_next (&iter))
5978 _dbus_assert_not_reached ("Reached end of arguments");
5980 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_BYTE)
5982 _dbus_warn ("type was: %d\n", dbus_message_iter_get_arg_type (&iter));
5983 _dbus_assert_not_reached ("wrong type after dict (should be byte)");
5986 if (dbus_message_iter_get_byte (&iter) != 0xF0)
5987 _dbus_assert_not_reached ("wrong value after dict");
5990 if (!dbus_message_iter_next (&iter))
5991 _dbus_assert_not_reached ("Reached end of arguments");
5993 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_NIL)
5994 _dbus_assert_not_reached ("not a nil type");
5996 if (!dbus_message_iter_next (&iter))
5997 _dbus_assert_not_reached ("Reached end of arguments");
5999 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_CUSTOM)
6000 _dbus_assert_not_reached ("wrong type after dict");
6002 if (!dbus_message_iter_get_custom (&iter, &str, &data, &len))
6003 _dbus_assert_not_reached ("failed to get custom type");
6005 _dbus_assert (strcmp (str, "MyTypeName")==0);
6006 _dbus_assert (len == 5);
6007 _dbus_assert (strcmp (data, "data")==0);
6011 if (!dbus_message_iter_next (&iter))
6012 _dbus_assert_not_reached ("Reached end of arguments");
6014 if (dbus_message_iter_get_byte (&iter) != 0xF0)
6015 _dbus_assert_not_reached ("wrong value after custom");
6017 if (!dbus_message_iter_next (&iter))
6018 _dbus_assert_not_reached ("Reached end of arguments");
6020 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
6021 _dbus_assert_not_reached ("no array");
6023 if (dbus_message_iter_get_array_type (&iter) != DBUS_TYPE_INT32)
6024 _dbus_assert_not_reached ("Array type not int32");
6026 if (dbus_message_iter_init_array_iterator (&iter, &array, NULL))
6027 _dbus_assert_not_reached ("non empty array");
6029 if (!dbus_message_iter_next (&iter))
6030 _dbus_assert_not_reached ("Reached end of arguments");
6032 if (dbus_message_iter_get_byte (&iter) != 0xF0)
6033 _dbus_assert_not_reached ("wrong value after empty array");
6035 if (!dbus_message_iter_next (&iter))
6036 _dbus_assert_not_reached ("Reached end of arguments");
6038 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
6039 _dbus_assert_not_reached ("non dict");
6041 if (dbus_message_iter_init_dict_iterator (&iter, &dict))
6042 _dbus_assert_not_reached ("non empty dict");
6044 if (!dbus_message_iter_next (&iter))
6045 _dbus_assert_not_reached ("Reached end of arguments");
6047 if (dbus_message_iter_get_byte (&iter) != 0xF0)
6048 _dbus_assert_not_reached ("wrong value after empty dict");
6050 if (dbus_message_iter_next (&iter))
6051 _dbus_assert_not_reached ("Didn't reach end of arguments");
6056 check_message_handling_type (DBusMessageIter *iter,
6059 DBusMessageIter child_iter;
6065 case DBUS_TYPE_BYTE:
6066 dbus_message_iter_get_byte (iter);
6068 case DBUS_TYPE_BOOLEAN:
6069 dbus_message_iter_get_boolean (iter);
6071 case DBUS_TYPE_INT32:
6072 dbus_message_iter_get_int32 (iter);
6074 case DBUS_TYPE_UINT32:
6075 dbus_message_iter_get_uint32 (iter);
6077 case DBUS_TYPE_INT64:
6078 #ifdef DBUS_HAVE_INT64
6079 dbus_message_iter_get_int64 (iter);
6082 case DBUS_TYPE_UINT64:
6083 #ifdef DBUS_HAVE_INT64
6084 dbus_message_iter_get_uint64 (iter);
6087 case DBUS_TYPE_DOUBLE:
6088 dbus_message_iter_get_double (iter);
6090 case DBUS_TYPE_STRING:
6093 str = dbus_message_iter_get_string (iter);
6096 _dbus_warn ("NULL string in message\n");
6102 case DBUS_TYPE_CUSTOM:
6105 unsigned char *data;
6108 if (!dbus_message_iter_get_custom (iter, &name, &data, &len))
6110 _dbus_warn ("error reading name from custom type\n");
6117 case DBUS_TYPE_ARRAY:
6121 dbus_message_iter_init_array_iterator (iter, &child_iter, &array_type);
6123 while (dbus_message_iter_has_next (&child_iter))
6125 if (!check_message_handling_type (&child_iter, array_type))
6127 _dbus_warn ("error in array element\n");
6131 if (!dbus_message_iter_next (&child_iter))
6136 case DBUS_TYPE_DICT:
6141 dbus_message_iter_init_dict_iterator (iter, &child_iter);
6143 while ((entry_type = dbus_message_iter_get_arg_type (&child_iter)) != DBUS_TYPE_INVALID)
6145 key = dbus_message_iter_get_dict_key (&child_iter);
6148 _dbus_warn ("error reading dict key\n");
6153 if (!check_message_handling_type (&child_iter, entry_type))
6155 _dbus_warn ("error in dict value\n");
6159 if (!dbus_message_iter_next (&child_iter))
6166 _dbus_warn ("unknown type %d\n", type);
6175 check_message_handling (DBusMessage *message)
6177 DBusMessageIter iter;
6180 dbus_uint32_t client_serial;
6184 client_serial = dbus_message_get_serial (message);
6186 /* can't use set_serial due to the assertions at the start of it */
6187 _dbus_marshal_set_uint32 (&message->header,
6188 message->byte_order,
6189 CLIENT_SERIAL_OFFSET,
6192 if (client_serial != dbus_message_get_serial (message))
6194 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
6198 /* If we implement message_set_arg (message, n, value)
6199 * then we would want to test it here
6202 dbus_message_iter_init (message, &iter);
6203 while ((type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
6205 if (!check_message_handling_type (&iter, type))
6208 if (!dbus_message_iter_next (&iter))
6219 check_have_valid_message (DBusMessageLoader *loader)
6221 DBusMessage *message;
6227 if (!_dbus_message_loader_queue_messages (loader))
6228 _dbus_assert_not_reached ("no memory to queue messages");
6230 if (_dbus_message_loader_get_is_corrupted (loader))
6232 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
6236 message = _dbus_message_loader_pop_message (loader);
6237 if (message == NULL)
6239 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
6243 if (_dbus_string_get_length (&loader->data) > 0)
6245 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
6249 /* Verify that we're able to properly deal with the message.
6250 * For example, this would detect improper handling of messages
6251 * in nonstandard byte order.
6253 if (!check_message_handling (message))
6260 dbus_message_unref (message);
6266 check_invalid_message (DBusMessageLoader *loader)
6272 if (!_dbus_message_loader_queue_messages (loader))
6273 _dbus_assert_not_reached ("no memory to queue messages");
6275 if (!_dbus_message_loader_get_is_corrupted (loader))
6277 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
6288 check_incomplete_message (DBusMessageLoader *loader)
6290 DBusMessage *message;
6296 if (!_dbus_message_loader_queue_messages (loader))
6297 _dbus_assert_not_reached ("no memory to queue messages");
6299 if (_dbus_message_loader_get_is_corrupted (loader))
6301 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
6305 message = _dbus_message_loader_pop_message (loader);
6306 if (message != NULL)
6308 _dbus_warn ("loaded message that was expected to be incomplete\n");
6316 dbus_message_unref (message);
6321 check_loader_results (DBusMessageLoader *loader,
6322 DBusMessageValidity validity)
6324 if (!_dbus_message_loader_queue_messages (loader))
6325 _dbus_assert_not_reached ("no memory to queue messages");
6329 case _DBUS_MESSAGE_VALID:
6330 return check_have_valid_message (loader);
6331 case _DBUS_MESSAGE_INVALID:
6332 return check_invalid_message (loader);
6333 case _DBUS_MESSAGE_INCOMPLETE:
6334 return check_incomplete_message (loader);
6335 case _DBUS_MESSAGE_UNKNOWN:
6339 _dbus_assert_not_reached ("bad DBusMessageValidity");
6345 * Loads the message in the given message file.
6347 * @param filename filename to load
6348 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
6349 * @param data string to load message into
6350 * @returns #TRUE if the message was loaded
6353 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
6365 _dbus_verbose ("Loading raw %s\n", _dbus_string_get_const_data (filename));
6366 dbus_error_init (&error);
6367 if (!_dbus_file_get_contents (data, filename, &error))
6369 _dbus_warn ("Could not load message file %s: %s\n",
6370 _dbus_string_get_const_data (filename),
6372 dbus_error_free (&error);
6378 if (!_dbus_message_data_load (data, filename))
6380 _dbus_warn ("Could not load message file %s\n",
6381 _dbus_string_get_const_data (filename));
6394 * Tries loading the message in the given message file
6395 * and verifies that DBusMessageLoader can handle it.
6397 * @param filename filename to load
6398 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
6399 * @param expected_validity what the message has to be like to return #TRUE
6400 * @returns #TRUE if the message has the expected validity
6403 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
6405 DBusMessageValidity expected_validity)
6412 if (!_dbus_string_init (&data))
6413 _dbus_assert_not_reached ("could not allocate string\n");
6415 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
6419 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
6425 if (_dbus_string_get_length (&data) > 0)
6426 _dbus_verbose_bytes_of_string (&data, 0,
6427 _dbus_string_get_length (&data));
6429 _dbus_warn ("Failed message loader test on %s\n",
6430 _dbus_string_get_const_data (filename));
6433 _dbus_string_free (&data);
6439 * Tries loading the given message data.
6442 * @param data the message data
6443 * @param expected_validity what the message has to be like to return #TRUE
6444 * @returns #TRUE if the message has the expected validity
6447 dbus_internal_do_not_use_try_message_data (const DBusString *data,
6448 DBusMessageValidity expected_validity)
6450 DBusMessageLoader *loader;
6458 /* Write the data one byte at a time */
6460 loader = _dbus_message_loader_new ();
6462 /* check some trivial loader functions */
6463 _dbus_message_loader_ref (loader);
6464 _dbus_message_loader_unref (loader);
6465 _dbus_message_loader_get_max_message_size (loader);
6467 len = _dbus_string_get_length (data);
6468 for (i = 0; i < len; i++)
6472 _dbus_message_loader_get_buffer (loader, &buffer);
6473 _dbus_string_append_byte (buffer,
6474 _dbus_string_get_byte (data, i));
6475 _dbus_message_loader_return_buffer (loader, buffer, 1);
6478 if (!check_loader_results (loader, expected_validity))
6481 _dbus_message_loader_unref (loader);
6484 /* Write the data all at once */
6486 loader = _dbus_message_loader_new ();
6491 _dbus_message_loader_get_buffer (loader, &buffer);
6492 _dbus_string_copy (data, 0, buffer,
6493 _dbus_string_get_length (buffer));
6494 _dbus_message_loader_return_buffer (loader, buffer, 1);
6497 if (!check_loader_results (loader, expected_validity))
6500 _dbus_message_loader_unref (loader);
6503 /* Write the data 2 bytes at a time */
6505 loader = _dbus_message_loader_new ();
6507 len = _dbus_string_get_length (data);
6508 for (i = 0; i < len; i += 2)
6512 _dbus_message_loader_get_buffer (loader, &buffer);
6513 _dbus_string_append_byte (buffer,
6514 _dbus_string_get_byte (data, i));
6516 _dbus_string_append_byte (buffer,
6517 _dbus_string_get_byte (data, i+1));
6518 _dbus_message_loader_return_buffer (loader, buffer, 1);
6521 if (!check_loader_results (loader, expected_validity))
6524 _dbus_message_loader_unref (loader);
6532 _dbus_message_loader_unref (loader);
6538 process_test_subdir (const DBusString *test_base_dir,
6540 DBusMessageValidity validity,
6541 DBusForeachMessageFileFunc function,
6544 DBusString test_directory;
6545 DBusString filename;
6553 if (!_dbus_string_init (&test_directory))
6554 _dbus_assert_not_reached ("didn't allocate test_directory\n");
6556 _dbus_string_init_const (&filename, subdir);
6558 if (!_dbus_string_copy (test_base_dir, 0,
6559 &test_directory, 0))
6560 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
6562 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
6563 _dbus_assert_not_reached ("couldn't allocate full path");
6565 _dbus_string_free (&filename);
6566 if (!_dbus_string_init (&filename))
6567 _dbus_assert_not_reached ("didn't allocate filename string\n");
6569 dbus_error_init (&error);
6570 dir = _dbus_directory_open (&test_directory, &error);
6573 _dbus_warn ("Could not open %s: %s\n",
6574 _dbus_string_get_const_data (&test_directory),
6576 dbus_error_free (&error);
6580 printf ("Testing %s:\n", subdir);
6583 while (_dbus_directory_get_next_file (dir, &filename, &error))
6585 DBusString full_path;
6588 if (!_dbus_string_init (&full_path))
6589 _dbus_assert_not_reached ("couldn't init string");
6591 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
6592 _dbus_assert_not_reached ("couldn't copy dir to full_path");
6594 if (!_dbus_concat_dir_and_file (&full_path, &filename))
6595 _dbus_assert_not_reached ("couldn't concat file to dir");
6597 if (_dbus_string_ends_with_c_str (&filename, ".message"))
6599 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
6603 _dbus_verbose ("Skipping non-.message file %s\n",
6604 _dbus_string_get_const_data (&filename));
6605 _dbus_string_free (&full_path);
6610 _dbus_string_get_const_data (&filename));
6612 _dbus_verbose (" expecting %s for %s\n",
6613 validity == _DBUS_MESSAGE_VALID ? "valid" :
6614 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
6615 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")),
6616 _dbus_string_get_const_data (&filename));
6618 if (! (*function) (&full_path, is_raw, validity, user_data))
6620 _dbus_string_free (&full_path);
6624 _dbus_string_free (&full_path);
6627 if (dbus_error_is_set (&error))
6629 _dbus_warn ("Could not get next file in %s: %s\n",
6630 _dbus_string_get_const_data (&test_directory),
6632 dbus_error_free (&error);
6641 _dbus_directory_close (dir);
6642 _dbus_string_free (&test_directory);
6643 _dbus_string_free (&filename);
6649 * Runs the given function on every message file in the test suite.
6650 * The function should return #FALSE on test failure or fatal error.
6652 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
6653 * @param func the function to run
6654 * @param user_data data for function
6655 * @returns #FALSE if there's a failure
6658 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
6659 DBusForeachMessageFileFunc func,
6662 DBusString test_directory;
6667 _dbus_string_init_const (&test_directory, test_data_dir);
6669 if (!process_test_subdir (&test_directory, "valid-messages",
6670 _DBUS_MESSAGE_VALID, func, user_data))
6673 if (!process_test_subdir (&test_directory, "invalid-messages",
6674 _DBUS_MESSAGE_INVALID, func, user_data))
6677 if (!process_test_subdir (&test_directory, "incomplete-messages",
6678 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
6685 _dbus_string_free (&test_directory);
6691 verify_test_message (DBusMessage *message)
6693 DBusMessageIter iter, dict;
6695 dbus_int32_t our_int;
6698 dbus_bool_t our_bool;
6699 dbus_uint32_t our_uint32;
6700 dbus_int32_t *our_uint32_array;
6701 int our_uint32_array_len;
6702 dbus_int32_t *our_int32_array;
6703 int our_int32_array_len;
6704 char **our_string_array;
6705 int our_string_array_len;
6706 #ifdef DBUS_HAVE_INT64
6707 dbus_int64_t our_int64;
6708 dbus_uint64_t our_uint64;
6709 dbus_int64_t *our_uint64_array;
6710 int our_uint64_array_len;
6711 dbus_int64_t *our_int64_array;
6712 int our_int64_array_len;
6714 double *our_double_array;
6715 int our_double_array_len;
6716 unsigned char *our_byte_array;
6717 int our_byte_array_len;
6718 unsigned char *our_boolean_array;
6719 int our_boolean_array_len;
6721 dbus_message_iter_init (message, &iter);
6723 dbus_error_init (&error);
6724 if (!dbus_message_iter_get_args (&iter, &error,
6725 DBUS_TYPE_INT32, &our_int,
6726 #ifdef DBUS_HAVE_INT64
6727 DBUS_TYPE_INT64, &our_int64,
6728 DBUS_TYPE_UINT64, &our_uint64,
6730 DBUS_TYPE_STRING, &our_str,
6731 DBUS_TYPE_DOUBLE, &our_double,
6732 DBUS_TYPE_BOOLEAN, &our_bool,
6733 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32,
6734 &our_uint32_array, &our_uint32_array_len,
6735 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32,
6736 &our_int32_array, &our_int32_array_len,
6737 #ifdef DBUS_HAVE_INT64
6738 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64,
6739 &our_uint64_array, &our_uint64_array_len,
6740 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64,
6741 &our_int64_array, &our_int64_array_len,
6743 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
6744 &our_string_array, &our_string_array_len,
6745 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE,
6746 &our_double_array, &our_double_array_len,
6747 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
6748 &our_byte_array, &our_byte_array_len,
6749 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN,
6750 &our_boolean_array, &our_boolean_array_len,
6753 _dbus_warn ("error: %s - %s\n", error.name,
6754 (error.message != NULL) ? error.message : "no message");
6755 _dbus_assert_not_reached ("Could not get arguments");
6758 if (our_int != -0x12345678)
6759 _dbus_assert_not_reached ("integers differ!");
6761 #ifdef DBUS_HAVE_INT64
6762 if (our_int64 != DBUS_INT64_CONSTANT (-0x123456789abcd))
6763 _dbus_assert_not_reached ("64-bit integers differ!");
6764 if (our_uint64 != DBUS_UINT64_CONSTANT (0x123456789abcd))
6765 _dbus_assert_not_reached ("64-bit unsigned integers differ!");
6768 if (our_double != 3.14159)
6769 _dbus_assert_not_reached ("doubles differ!");
6771 if (strcmp (our_str, "Test string") != 0)
6772 _dbus_assert_not_reached ("strings differ!");
6773 dbus_free (our_str);
6776 _dbus_assert_not_reached ("booleans differ");
6778 if (our_uint32_array_len != 4 ||
6779 our_uint32_array[0] != 0x12345678 ||
6780 our_uint32_array[1] != 0x23456781 ||
6781 our_uint32_array[2] != 0x34567812 ||
6782 our_uint32_array[3] != 0x45678123)
6783 _dbus_assert_not_reached ("uint array differs");
6784 dbus_free (our_uint32_array);
6786 if (our_int32_array_len != 4 ||
6787 our_int32_array[0] != 0x12345678 ||
6788 our_int32_array[1] != -0x23456781 ||
6789 our_int32_array[2] != 0x34567812 ||
6790 our_int32_array[3] != -0x45678123)
6791 _dbus_assert_not_reached ("int array differs");
6792 dbus_free (our_int32_array);
6794 #ifdef DBUS_HAVE_INT64
6795 if (our_uint64_array_len != 4 ||
6796 our_uint64_array[0] != 0x12345678 ||
6797 our_uint64_array[1] != 0x23456781 ||
6798 our_uint64_array[2] != 0x34567812 ||
6799 our_uint64_array[3] != 0x45678123)
6800 _dbus_assert_not_reached ("uint64 array differs");
6801 dbus_free (our_uint64_array);
6803 if (our_int64_array_len != 4 ||
6804 our_int64_array[0] != 0x12345678 ||
6805 our_int64_array[1] != -0x23456781 ||
6806 our_int64_array[2] != 0x34567812 ||
6807 our_int64_array[3] != -0x45678123)
6808 _dbus_assert_not_reached ("int64 array differs");
6809 dbus_free (our_int64_array);
6810 #endif /* DBUS_HAVE_INT64 */
6812 if (our_string_array_len != 4)
6813 _dbus_assert_not_reached ("string array has wrong length");
6815 if (strcmp (our_string_array[0], "Foo") != 0 ||
6816 strcmp (our_string_array[1], "bar") != 0 ||
6817 strcmp (our_string_array[2], "") != 0 ||
6818 strcmp (our_string_array[3], "woo woo woo woo") != 0)
6819 _dbus_assert_not_reached ("string array differs");
6821 dbus_free_string_array (our_string_array);
6823 if (our_double_array_len != 3)
6824 _dbus_assert_not_reached ("double array had wrong length");
6826 /* On all IEEE machines (i.e. everything sane) exact equality
6827 * should be preserved over the wire
6829 if (our_double_array[0] != 0.1234 ||
6830 our_double_array[1] != 9876.54321 ||
6831 our_double_array[2] != -300.0)
6832 _dbus_assert_not_reached ("double array had wrong values");
6834 dbus_free (our_double_array);
6836 if (our_byte_array_len != 4)
6837 _dbus_assert_not_reached ("byte array had wrong length");
6839 if (our_byte_array[0] != 'a' ||
6840 our_byte_array[1] != 'b' ||
6841 our_byte_array[2] != 'c' ||
6842 our_byte_array[3] != 234)
6843 _dbus_assert_not_reached ("byte array had wrong values");
6845 dbus_free (our_byte_array);
6847 if (our_boolean_array_len != 5)
6848 _dbus_assert_not_reached ("bool array had wrong length");
6850 if (our_boolean_array[0] != TRUE ||
6851 our_boolean_array[1] != FALSE ||
6852 our_boolean_array[2] != TRUE ||
6853 our_boolean_array[3] != TRUE ||
6854 our_boolean_array[4] != FALSE)
6855 _dbus_assert_not_reached ("bool array had wrong values");
6857 dbus_free (our_boolean_array);
6859 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
6860 _dbus_assert_not_reached ("not dict type");
6862 dbus_message_iter_init_dict_iterator (&iter, &dict);
6864 our_str = dbus_message_iter_get_dict_key (&dict);
6865 if (our_str == NULL || strcmp (our_str, "test") != 0)
6866 _dbus_assert_not_reached ("wrong dict key");
6867 dbus_free (our_str);
6869 if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_UINT32)
6871 _dbus_verbose ("dict entry type: %d\n", dbus_message_iter_get_arg_type (&dict));
6872 _dbus_assert_not_reached ("wrong dict entry type");
6875 if ((our_uint32 = dbus_message_iter_get_uint32 (&dict)) != 0xDEADBEEF)
6877 _dbus_verbose ("dict entry val: %x\n", our_uint32);
6878 _dbus_assert_not_reached ("wrong dict entry value");
6881 if (dbus_message_iter_next (&dict))
6882 _dbus_assert_not_reached ("Didn't reach end of dict");
6884 if (!dbus_message_iter_next (&iter))
6885 _dbus_assert_not_reached ("Reached end of arguments");
6887 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
6888 _dbus_assert_not_reached ("wrong type after dict");
6890 if (dbus_message_iter_get_uint32 (&iter) != 0xCAFEBABE)
6891 _dbus_assert_not_reached ("wrong value after dict");
6893 if (dbus_message_iter_next (&iter))
6894 _dbus_assert_not_reached ("Didn't reach end of arguments");
6898 * @ingroup DBusMessageInternals
6899 * Unit test for DBusMessage.
6901 * @returns #TRUE on success.
6904 _dbus_message_test (const char *test_data_dir)
6906 DBusMessage *message;
6907 DBusMessageLoader *loader;
6908 DBusMessageIter iter, child_iter, child_iter2, child_iter3;
6914 const dbus_uint32_t our_uint32_array[] =
6915 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
6916 const dbus_uint32_t our_int32_array[] =
6917 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
6918 #ifdef DBUS_HAVE_INT64
6919 const dbus_uint64_t our_uint64_array[] =
6920 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
6921 const dbus_uint64_t our_int64_array[] =
6922 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
6924 const char *our_string_array[] = { "Foo", "bar", "", "woo woo woo woo" };
6925 const double our_double_array[] = { 0.1234, 9876.54321, -300.0 };
6926 const unsigned char our_byte_array[] = { 'a', 'b', 'c', 234 };
6927 const unsigned char our_boolean_array[] = { TRUE, FALSE, TRUE, TRUE, FALSE };
6933 _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
6935 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
6936 "/org/freedesktop/TestPath",
6937 "Foo.TestInterface",
6939 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
6940 _dbus_assert (dbus_message_is_method_call (message, "Foo.TestInterface",
6942 _dbus_assert (strcmp (dbus_message_get_path (message),
6943 "/org/freedesktop/TestPath") == 0);
6944 _dbus_message_set_serial (message, 1234);
6945 /* string length including nul byte not a multiple of 4 */
6946 if (!dbus_message_set_sender (message, "org.foo.bar1"))
6947 _dbus_assert_not_reached ("out of memory");
6948 _dbus_assert (dbus_message_has_sender (message, "org.foo.bar1"));
6949 dbus_message_set_reply_serial (message, 5678);
6950 if (!dbus_message_set_sender (message, NULL))
6951 _dbus_assert_not_reached ("out of memory");
6952 _dbus_assert (!dbus_message_has_sender (message, "org.foo.bar1"));
6953 _dbus_assert (dbus_message_get_serial (message) == 1234);
6954 _dbus_assert (dbus_message_get_reply_serial (message) == 5678);
6955 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
6957 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
6958 dbus_message_set_no_reply (message, TRUE);
6959 _dbus_assert (dbus_message_get_no_reply (message) == TRUE);
6960 dbus_message_set_no_reply (message, FALSE);
6961 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
6963 /* Set/get some header fields */
6965 if (!dbus_message_set_path (message, "/foo"))
6966 _dbus_assert_not_reached ("out of memory");
6967 _dbus_assert (strcmp (dbus_message_get_path (message),
6970 if (!dbus_message_set_interface (message, "org.Foo"))
6971 _dbus_assert_not_reached ("out of memory");
6972 _dbus_assert (strcmp (dbus_message_get_interface (message),
6975 if (!dbus_message_set_member (message, "Bar"))
6976 _dbus_assert_not_reached ("out of memory");
6977 _dbus_assert (strcmp (dbus_message_get_member (message),
6980 /* Set/get them with longer values */
6981 if (!dbus_message_set_path (message, "/foo/bar"))
6982 _dbus_assert_not_reached ("out of memory");
6983 _dbus_assert (strcmp (dbus_message_get_path (message),
6986 if (!dbus_message_set_interface (message, "org.Foo.Bar"))
6987 _dbus_assert_not_reached ("out of memory");
6988 _dbus_assert (strcmp (dbus_message_get_interface (message),
6989 "org.Foo.Bar") == 0);
6991 if (!dbus_message_set_member (message, "BarFoo"))
6992 _dbus_assert_not_reached ("out of memory");
6993 _dbus_assert (strcmp (dbus_message_get_member (message),
6996 /* Realloc shorter again */
6998 if (!dbus_message_set_path (message, "/foo"))
6999 _dbus_assert_not_reached ("out of memory");
7000 _dbus_assert (strcmp (dbus_message_get_path (message),
7003 if (!dbus_message_set_interface (message, "org.Foo"))
7004 _dbus_assert_not_reached ("out of memory");
7005 _dbus_assert (strcmp (dbus_message_get_interface (message),
7008 if (!dbus_message_set_member (message, "Bar"))
7009 _dbus_assert_not_reached ("out of memory");
7010 _dbus_assert (strcmp (dbus_message_get_member (message),
7013 dbus_message_unref (message);
7015 /* Test the vararg functions */
7016 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
7017 "/org/freedesktop/TestPath",
7018 "Foo.TestInterface",
7020 _dbus_message_set_serial (message, 1);
7021 dbus_message_append_args (message,
7022 DBUS_TYPE_INT32, -0x12345678,
7023 #ifdef DBUS_HAVE_INT64
7024 DBUS_TYPE_INT64, DBUS_INT64_CONSTANT (-0x123456789abcd),
7025 DBUS_TYPE_UINT64, DBUS_UINT64_CONSTANT (0x123456789abcd),
7027 DBUS_TYPE_STRING, "Test string",
7028 DBUS_TYPE_DOUBLE, 3.14159,
7029 DBUS_TYPE_BOOLEAN, TRUE,
7030 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, our_uint32_array,
7031 _DBUS_N_ELEMENTS (our_uint32_array),
7032 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, our_int32_array,
7033 _DBUS_N_ELEMENTS (our_int32_array),
7034 #ifdef DBUS_HAVE_INT64
7035 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64, our_uint64_array,
7036 _DBUS_N_ELEMENTS (our_uint64_array),
7037 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64, our_int64_array,
7038 _DBUS_N_ELEMENTS (our_int64_array),
7040 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, our_string_array,
7041 _DBUS_N_ELEMENTS (our_string_array),
7042 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, our_double_array,
7043 _DBUS_N_ELEMENTS (our_double_array),
7044 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, our_byte_array,
7045 _DBUS_N_ELEMENTS (our_byte_array),
7046 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN, our_boolean_array,
7047 _DBUS_N_ELEMENTS (our_boolean_array),
7050 dbus_message_append_iter_init (message, &iter);
7051 dbus_message_iter_append_dict (&iter, &child_iter);
7052 dbus_message_iter_append_dict_key (&child_iter, "test");
7053 dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
7054 dbus_message_iter_append_uint32 (&iter, 0xCAFEBABE);
7057 sig[i++] = DBUS_TYPE_INT32;
7058 #ifdef DBUS_HAVE_INT64
7059 sig[i++] = DBUS_TYPE_INT64;
7060 sig[i++] = DBUS_TYPE_UINT64;
7062 sig[i++] = DBUS_TYPE_STRING;
7063 sig[i++] = DBUS_TYPE_DOUBLE;
7064 sig[i++] = DBUS_TYPE_BOOLEAN;
7065 sig[i++] = DBUS_TYPE_ARRAY;
7066 sig[i++] = DBUS_TYPE_UINT32;
7067 sig[i++] = DBUS_TYPE_ARRAY;
7068 sig[i++] = DBUS_TYPE_INT32;
7069 #ifdef DBUS_HAVE_INT64
7070 sig[i++] = DBUS_TYPE_ARRAY;
7071 sig[i++] = DBUS_TYPE_UINT64;
7072 sig[i++] = DBUS_TYPE_ARRAY;
7073 sig[i++] = DBUS_TYPE_INT64;
7075 sig[i++] = DBUS_TYPE_ARRAY;
7076 sig[i++] = DBUS_TYPE_STRING;
7077 sig[i++] = DBUS_TYPE_ARRAY;
7078 sig[i++] = DBUS_TYPE_DOUBLE;
7079 sig[i++] = DBUS_TYPE_ARRAY;
7080 sig[i++] = DBUS_TYPE_BYTE;
7081 sig[i++] = DBUS_TYPE_ARRAY;
7082 sig[i++] = DBUS_TYPE_BOOLEAN;
7083 sig[i++] = DBUS_TYPE_DICT;
7084 sig[i++] = DBUS_TYPE_UINT32;
7085 sig[i++] = DBUS_TYPE_INVALID;
7087 _dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
7089 _dbus_verbose_bytes_of_string (&message->header, 0,
7090 _dbus_string_get_length (&message->header));
7091 _dbus_verbose_bytes_of_string (&message->body, 0,
7092 _dbus_string_get_length (&message->body));
7094 _dbus_verbose ("Signature expected \"%s\" actual \"%s\"\n",
7095 sig, dbus_message_get_signature (message));
7097 s = dbus_message_get_signature (message);
7099 _dbus_assert (dbus_message_has_signature (message, sig));
7100 _dbus_assert (strcmp (s, sig) == 0);
7102 verify_test_message (message);
7104 copy = dbus_message_copy (message);
7106 _dbus_assert (message->client_serial == copy->client_serial);
7107 _dbus_assert (message->reply_serial == copy->reply_serial);
7108 _dbus_assert (message->header_padding == copy->header_padding);
7110 _dbus_assert (_dbus_string_get_length (&message->header) ==
7111 _dbus_string_get_length (©->header));
7113 _dbus_assert (_dbus_string_get_length (&message->body) ==
7114 _dbus_string_get_length (©->body));
7116 verify_test_message (copy);
7118 name1 = dbus_message_get_interface (message);
7119 name2 = dbus_message_get_interface (copy);
7121 _dbus_assert (strcmp (name1, name2) == 0);
7123 name1 = dbus_message_get_member (message);
7124 name2 = dbus_message_get_member (copy);
7126 _dbus_assert (strcmp (name1, name2) == 0);
7128 dbus_message_unref (message);
7129 dbus_message_unref (copy);
7131 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
7132 "/org/freedesktop/TestPath",
7133 "Foo.TestInterface",
7136 _dbus_message_set_serial (message, 1);
7137 dbus_message_set_reply_serial (message, 0x12345678);
7139 dbus_message_append_iter_init (message, &iter);
7140 dbus_message_iter_append_string (&iter, "Test string");
7141 dbus_message_iter_append_int32 (&iter, -0x12345678);
7142 dbus_message_iter_append_uint32 (&iter, 0xedd1e);
7143 dbus_message_iter_append_double (&iter, 3.14159);
7145 dbus_message_iter_append_array (&iter, &child_iter, DBUS_TYPE_DOUBLE);
7146 dbus_message_iter_append_double (&child_iter, 1.5);
7147 dbus_message_iter_append_double (&child_iter, 2.5);
7150 dbus_message_iter_append_dict (&iter, &child_iter);
7151 dbus_message_iter_append_dict_key (&child_iter, "test");
7152 dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
7154 /* dict (in dict) */
7155 dbus_message_iter_append_dict_key (&child_iter, "testdict");
7156 dbus_message_iter_append_dict (&child_iter, &child_iter2);
7158 dbus_message_iter_append_dict_key (&child_iter2, "dictkey");
7159 dbus_message_iter_append_string (&child_iter2, "dictvalue");
7161 /* array of array of int32 (in dict) */
7162 dbus_message_iter_append_dict_key (&child_iter, "array");
7163 dbus_message_iter_append_array (&child_iter, &child_iter2, DBUS_TYPE_ARRAY);
7164 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
7165 dbus_message_iter_append_int32 (&child_iter3, 0x12345678);
7166 dbus_message_iter_append_int32 (&child_iter3, 0x23456781);
7167 _dbus_warn ("next call expected to fail with wrong array type\n");
7168 _dbus_assert (!dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_UINT32));
7169 dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
7170 dbus_message_iter_append_int32 (&child_iter3, 0x34567812);
7171 dbus_message_iter_append_int32 (&child_iter3, 0x45678123);
7172 dbus_message_iter_append_int32 (&child_iter3, 0x56781234);
7174 dbus_message_iter_append_byte (&iter, 0xF0);
7176 dbus_message_iter_append_nil (&iter);
7178 dbus_message_iter_append_custom (&iter, "MyTypeName",
7181 dbus_message_iter_append_byte (&iter, 0xF0);
7183 dbus_message_iter_append_array (&iter, &child_iter, DBUS_TYPE_INT32);
7185 dbus_message_iter_append_byte (&iter, 0xF0);
7187 dbus_message_iter_append_dict (&iter, &child_iter);
7189 dbus_message_iter_append_byte (&iter, 0xF0);
7191 message_iter_test (message);
7193 /* Message loader test */
7194 _dbus_message_lock (message);
7195 loader = _dbus_message_loader_new ();
7197 /* check ref/unref */
7198 _dbus_message_loader_ref (loader);
7199 _dbus_message_loader_unref (loader);
7201 /* Write the header data one byte at a time */
7202 data = _dbus_string_get_const_data (&message->header);
7203 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
7207 _dbus_message_loader_get_buffer (loader, &buffer);
7208 _dbus_string_append_byte (buffer, data[i]);
7209 _dbus_message_loader_return_buffer (loader, buffer, 1);
7212 /* Write the body data one byte at a time */
7213 data = _dbus_string_get_const_data (&message->body);
7214 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
7218 _dbus_message_loader_get_buffer (loader, &buffer);
7219 _dbus_string_append_byte (buffer, data[i]);
7220 _dbus_message_loader_return_buffer (loader, buffer, 1);
7223 copy = dbus_message_copy (message); /* save for tests below */
7224 dbus_message_unref (message);
7226 /* Now pop back the message */
7227 if (!_dbus_message_loader_queue_messages (loader))
7228 _dbus_assert_not_reached ("no memory to queue messages");
7230 if (_dbus_message_loader_get_is_corrupted (loader))
7231 _dbus_assert_not_reached ("message loader corrupted");
7233 message = _dbus_message_loader_pop_message (loader);
7235 _dbus_assert_not_reached ("received a NULL message");
7237 if (dbus_message_get_reply_serial (message) != 0x12345678)
7238 _dbus_assert_not_reached ("reply serial fields differ");
7240 message_iter_test (message);
7242 dbus_message_unref (message);
7243 _dbus_message_loader_unref (loader);
7245 message = dbus_message_new_method_return (copy);
7246 if (message == NULL)
7247 _dbus_assert_not_reached ("out of memory\n");
7248 dbus_message_unref (copy);
7250 if (!dbus_message_append_args (message,
7251 DBUS_TYPE_STRING, "hello",
7253 _dbus_assert_not_reached ("no memory");
7255 if (!dbus_message_has_signature (message, "s"))
7256 _dbus_assert_not_reached ("method return has wrong signature");
7258 dbus_error_init (&error);
7259 if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING,
7260 &t, DBUS_TYPE_INVALID))
7263 _dbus_warn ("Failed to get expected string arg: %s\n", error.message);
7268 dbus_message_unref (message);
7270 /* Now load every message in test_data_dir if we have one */
7271 if (test_data_dir == NULL)
7274 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
7275 (DBusForeachMessageFileFunc)
7276 dbus_internal_do_not_use_try_message_file,
7280 #endif /* DBUS_BUILD_TESTS */