1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-message.c DBusMessage object
4 * Copyright (C) 2002, 2003 Red Hat Inc.
5 * Copyright (C) 2002, 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 1.2
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-internals.h"
26 #include "dbus-marshal.h"
27 #include "dbus-message.h"
28 #include "dbus-message-internal.h"
29 #include "dbus-memory.h"
30 #include "dbus-list.h"
31 #include "dbus-message-builder.h"
35 * @defgroup DBusMessageInternals DBusMessage implementation details
36 * @ingroup DBusInternals
37 * @brief DBusMessage private implementation details.
39 * The guts of DBusMessage and its methods.
57 static dbus_bool_t field_is_named[FIELD_LAST] =
59 FALSE, /* FIELD_HEADER_LENGTH */
60 FALSE, /* FIELD_BODY_LENGTH */
61 FALSE, /* FIELD_CLIENT_SERIAL */
62 TRUE, /* FIELD_NAME */
63 TRUE, /* FIELD_SERVICE */
64 TRUE, /* FIELD_SENDER */
65 TRUE /* FIELD_REPLY_SERIAL */
70 int offset; /**< Offset to start of field (location of name of field
76 * @brief Internals of DBusMessage
78 * Object representing a message received from or to be sent to
79 * another application. This is an opaque object, all members
84 dbus_atomic_t refcount; /**< Reference count */
86 DBusString header; /**< Header network data, stored
87 * separately from body so we can
88 * independently realloc it.
91 HeaderField header_fields[FIELD_LAST]; /**< Track the location
92 * of each field in "header"
94 int header_padding; /**< bytes of alignment in header */
96 DBusString body; /**< Body network data. */
98 char byte_order; /**< Message byte order. */
100 DBusCounter *size_counter; /**< Counter for the size of the message, or #NULL */
101 long size_counter_delta; /**< Size we incremented the size counter by. */
103 unsigned int locked : 1; /**< Message being sent, no modifications allowed. */
107 * @brief Internals of DBusMessageIter
109 * Object representing a position in a message. All fields are internal.
111 struct DBusMessageIter
113 int refcount; /**< Reference count */
115 int pos; /**< Current position in the string */
117 DBusMessage *message; /**< Message used */
121 * Gets the data to be sent over the network for this message.
122 * The header and then the body should be written out.
123 * This function is guaranteed to always return the same
124 * data once a message is locked (with _dbus_message_lock()).
126 * @param message the message.
127 * @param header return location for message header data.
128 * @param body return location for message body data.
131 _dbus_message_get_network_data (DBusMessage *message,
132 const DBusString **header,
133 const DBusString **body)
135 _dbus_assert (message->locked);
137 *header = &message->header;
138 *body = &message->body;
142 clear_header_padding (DBusMessage *message)
144 _dbus_string_shorten (&message->header,
145 message->header_padding);
146 message->header_padding = 0;
150 append_header_padding (DBusMessage *message)
153 old_len = _dbus_string_get_length (&message->header);
154 if (!_dbus_string_align_length (&message->header, 8))
157 message->header_padding = _dbus_string_get_length (&message->header) - old_len;
163 adjust_field_offsets (DBusMessage *message,
173 while (i < FIELD_LAST)
175 if (message->header_fields[i].offset > offsets_after)
176 message->header_fields[i].offset += delta;
183 get_string_field (DBusMessage *message,
187 int offset = message->header_fields[field].offset;
193 /* offset points to string length, string data follows it */
194 /* FIXME _dbus_demarshal_const_string() that returned
195 * a reference to the string plus its len might be nice.
199 *len = _dbus_demarshal_uint32 (&message->header,
204 _dbus_string_get_const_data (&message->header,
207 return data + (offset + 4);
211 get_int_field (DBusMessage *message,
214 int offset = message->header_fields[field].offset;
217 return -1; /* useless if -1 is a valid value of course */
219 return _dbus_demarshal_int32 (&message->header,
226 append_int_field (DBusMessage *message,
233 _dbus_assert (!message->locked);
235 clear_header_padding (message);
237 orig_len = _dbus_string_get_length (&message->header);
239 if (!_dbus_string_align_length (&message->header, 4))
242 if (!_dbus_string_append_len (&message->header, name, 4))
245 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_INT32))
248 if (!_dbus_string_align_length (&message->header, 4))
251 message->header_fields[FIELD_REPLY_SERIAL].offset =
252 _dbus_string_get_length (&message->header);
254 if (!_dbus_marshal_int32 (&message->header, message->byte_order,
258 if (!append_header_padding (message))
264 message->header_fields[field].offset = -1;
265 _dbus_string_set_length (&message->header, orig_len);
267 /* this must succeed because it was allocated on function entry and
268 * DBusString doesn't ever realloc smaller
270 if (!append_header_padding (message))
271 _dbus_assert_not_reached ("failed to reappend header padding");
276 append_string_field (DBusMessage *message,
283 _dbus_assert (!message->locked);
285 clear_header_padding (message);
287 orig_len = _dbus_string_get_length (&message->header);
289 if (!_dbus_string_align_length (&message->header, 4))
292 if (!_dbus_string_append_len (&message->header, name, 4))
295 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_STRING))
298 if (!_dbus_string_align_length (&message->header, 4))
301 message->header_fields[field].offset =
302 _dbus_string_get_length (&message->header);
304 if (!_dbus_marshal_string (&message->header, message->byte_order,
308 if (!append_header_padding (message))
314 message->header_fields[field].offset = -1;
315 _dbus_string_set_length (&message->header, orig_len);
317 /* this must succeed because it was allocated on function entry and
318 * DBusString doesn't ever realloc smaller
320 if (!append_header_padding (message))
321 _dbus_assert_not_reached ("failed to reappend header padding");
327 delete_int_field (DBusMessage *message,
330 int offset = message->header_fields[field].offset;
332 _dbus_assert (!message->locked);
333 _dbus_assert (field_is_named[field]);
338 clear_header_padding (message);
340 /* The field typecode and name take up 8 bytes */
341 _dbus_string_delete (&message->header,
345 message->header_fields[field].offset = -1;
347 adjust_field_offsets (message,
351 append_header_padding (message);
355 delete_string_field (DBusMessage *message,
358 int offset = message->header_fields[field].offset;
362 _dbus_assert (!message->locked);
363 _dbus_assert (field_is_named[field]);
368 clear_header_padding (message);
370 get_string_field (message, field, &len);
372 /* The field typecode and name take up 8 bytes, and the nul
373 * termination is 1 bytes, string length integer is 4 bytes
375 delete_len = 8 + 4 + 1 + len;
377 _dbus_string_delete (&message->header,
381 message->header_fields[field].offset = -1;
383 adjust_field_offsets (message,
387 append_header_padding (message);
391 set_int_field (DBusMessage *message,
395 int offset = message->header_fields[field].offset;
397 _dbus_assert (!message->locked);
401 /* need to append the field */
405 case FIELD_REPLY_SERIAL:
406 return append_int_field (message, field,
407 DBUS_HEADER_FIELD_REPLY,
410 _dbus_assert_not_reached ("appending an int field we don't support appending");
416 _dbus_marshal_set_int32 (&message->header,
425 set_uint_field (DBusMessage *message,
429 int offset = message->header_fields[field].offset;
431 _dbus_assert (!message->locked);
435 /* need to append the field */
440 _dbus_assert_not_reached ("appending a uint field we don't support appending");
446 _dbus_marshal_set_uint32 (&message->header,
455 set_string_field (DBusMessage *message,
459 int offset = message->header_fields[field].offset;
461 _dbus_assert (!message->locked);
462 _dbus_assert (value != NULL);
466 /* need to append the field */
471 return append_string_field (message, field,
472 DBUS_HEADER_FIELD_SENDER,
475 _dbus_assert_not_reached ("appending a string field we don't support appending");
485 old_len = _dbus_string_get_length (&message->header);
487 _dbus_string_init_const_len (&v, value,
488 strlen (value) + 1); /* include nul */
489 if (!_dbus_marshal_set_string (&message->header,
494 new_len = _dbus_string_get_length (&message->header);
496 adjust_field_offsets (message,
505 * Sets the client serial of a message.
506 * This can only be done once on a message.
508 * @todo client_serial should be called simply
509 * "serial"; it's in outgoing messages for both
510 * the client and the server, it's only client-specific
511 * in the message bus case. It's more like origin_serial
514 * @param message the message
515 * @param client_serial the client serial
518 _dbus_message_set_client_serial (DBusMessage *message,
519 dbus_int32_t client_serial)
521 _dbus_assert (!message->locked);
522 _dbus_assert (_dbus_message_get_client_serial (message) < 0);
524 set_int_field (message, FIELD_CLIENT_SERIAL,
529 * Sets the reply serial of a message (the client serial
530 * of the message this is a reply to).
532 * @param message the message
533 * @param reply_serial the client serial
534 * @returns #FALSE if not enough memory
537 _dbus_message_set_reply_serial (DBusMessage *message,
538 dbus_int32_t reply_serial)
540 _dbus_assert (!message->locked);
542 return set_int_field (message, FIELD_REPLY_SERIAL,
547 * Returns the client serial of a message or
548 * -1 if none has been specified.
550 * @todo see note in _dbus_message_set_client_serial()
551 * about how client_serial is a misnomer
553 * @todo this function should be public, after renaming it.
555 * @param message the message
556 * @returns the client serial
559 _dbus_message_get_client_serial (DBusMessage *message)
561 return get_int_field (message, FIELD_CLIENT_SERIAL);
565 * Returns the serial that the message is
566 * a reply to or -1 if none.
568 * @param message the message
569 * @returns the reply serial
572 _dbus_message_get_reply_serial (DBusMessage *message)
574 return get_int_field (message, FIELD_REPLY_SERIAL);
578 * Adds a counter to be incremented immediately with the
579 * size of this message, and decremented by the size
580 * of this message when this message if finalized.
582 * @param message the message
583 * @param counter the counter
586 _dbus_message_add_size_counter (DBusMessage *message,
587 DBusCounter *counter)
589 _dbus_assert (message->size_counter == NULL); /* If this fails we may need to keep a list of
590 * counters instead of just one
593 message->size_counter = counter;
594 _dbus_counter_ref (message->size_counter);
596 /* When we can change message size, we may want to
597 * update this each time we do so, or we may want to
598 * just KISS like this.
600 message->size_counter_delta =
601 _dbus_string_get_length (&message->header) +
602 _dbus_string_get_length (&message->body);
604 _dbus_verbose ("message has size %ld\n",
605 message->size_counter_delta);
607 _dbus_counter_adjust (message->size_counter, message->size_counter_delta);
611 dbus_message_create_header (DBusMessage *message,
617 if (!_dbus_string_append_byte (&message->header, message->byte_order))
621 if (!_dbus_string_append_byte (&message->header, flags))
624 if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
627 if (!_dbus_string_append_byte (&message->header, 0))
630 message->header_fields[FIELD_HEADER_LENGTH].offset = 4;
631 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
634 message->header_fields[FIELD_BODY_LENGTH].offset = 8;
635 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
638 message->header_fields[FIELD_CLIENT_SERIAL].offset = 12;
639 if (!_dbus_marshal_int32 (&message->header, message->byte_order, -1))
642 /* Marshal message service */
645 if (!append_string_field (message,
647 DBUS_HEADER_FIELD_SERVICE,
652 _dbus_assert (name != NULL);
653 if (!append_string_field (message,
655 DBUS_HEADER_FIELD_NAME,
663 * Locks a message. Allows checking that applications don't keep a
664 * reference to a message in the outgoing queue and change it
665 * underneath us. Messages are locked when they enter the outgoing
666 * queue (dbus_connection_send_message()), and the library complains
667 * if the message is modified while locked.
669 * @param message the message to lock.
672 _dbus_message_lock (DBusMessage *message)
674 if (!message->locked)
676 /* Fill in our lengths */
677 set_uint_field (message,
679 _dbus_string_get_length (&message->header));
681 set_uint_field (message,
683 _dbus_string_get_length (&message->body));
685 message->locked = TRUE;
692 * @defgroup DBusMessage DBusMessage
694 * @brief Message to be sent or received over a DBusConnection.
696 * A DBusMessage is the most basic unit of communication over a
697 * DBusConnection. A DBusConnection represents a stream of messages
698 * received from a remote application, and a stream of messages
699 * sent to a remote application.
705 * @typedef DBusMessage
707 * Opaque data type representing a message received from or to be
708 * sent to another application.
712 dbus_message_new_empty_header (void)
714 DBusMessage *message;
717 message = dbus_new0 (DBusMessage, 1);
721 message->refcount = 1;
722 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
725 while (i < FIELD_LAST)
727 message->header_fields[i].offset = -1;
731 if (!_dbus_string_init (&message->header, _DBUS_INT_MAX))
737 if (!_dbus_string_init (&message->body, _DBUS_INT_MAX))
739 _dbus_string_free (&message->header);
749 * Constructs a new message. Returns #NULL if memory can't be
750 * allocated for the message. The service may be #NULL in which case
751 * no service is set; this is appropriate when using D-BUS in a
752 * peer-to-peer context (no message bus).
754 * @todo reverse the arguments, first 'name' then 'service'
755 * as 'name' is more fundamental
757 * @param service service that the message should be sent to or #NULL
758 * @param name name of the message
759 * @returns a new DBusMessage, free with dbus_message_unref()
760 * @see dbus_message_unref()
763 dbus_message_new (const char *service,
766 DBusMessage *message;
768 message = dbus_message_new_empty_header ();
772 if (!dbus_message_create_header (message, service, name))
774 dbus_message_unref (message);
782 * Constructs a message that is a reply to some other
783 * message. Returns #NULL if memory can't be allocated
786 * @param original_message the message which the created
787 * message is a reply to.
788 * @returns a new DBusMessage, free with dbus_message_unref()
789 * @see dbus_message_new(), dbus_message_unref()
792 dbus_message_new_reply (DBusMessage *original_message)
794 DBusMessage *message;
795 const char *sender, *name;
797 sender = get_string_field (original_message,
799 name = get_string_field (original_message,
802 _dbus_assert (sender != NULL);
804 message = dbus_message_new (sender, name);
809 if (!_dbus_message_set_reply_serial (message,
810 _dbus_message_get_client_serial (original_message)))
812 dbus_message_unref (message);
820 * Creates a new message that is an error reply to a certain message.
822 * @param original_message the original message
823 * @param error_name the error name
824 * @param error_message the error message string
825 * @returns a new error message
828 dbus_message_new_error_reply (DBusMessage *original_message,
829 const char *error_name,
830 const char *error_message)
832 DBusMessage *message;
835 sender = get_string_field (original_message,
838 _dbus_assert (sender != NULL);
840 message = dbus_message_new (sender, error_name);
845 if (!_dbus_message_set_reply_serial (message,
846 _dbus_message_get_client_serial (original_message)))
848 dbus_message_unref (message);
852 if (!dbus_message_append_string (message, error_message))
854 dbus_message_unref (message);
858 dbus_message_set_is_error (message, TRUE);
864 * Creates a new message that is an exact replica of the message
865 * specified, except that its refcount is set to 1.
867 * @param message the message.
868 * @returns the new message.
871 dbus_message_new_from_message (const DBusMessage *message)
876 retval = dbus_new0 (DBusMessage, 1);
880 retval->refcount = 1;
881 retval->byte_order = message->byte_order;
883 if (!_dbus_string_init (&retval->header, _DBUS_INT_MAX))
889 if (!_dbus_string_init (&retval->body, _DBUS_INT_MAX))
891 _dbus_string_free (&retval->header);
896 if (!_dbus_string_copy (&message->header, 0,
899 _dbus_string_free (&retval->header);
900 _dbus_string_free (&retval->body);
906 if (!_dbus_string_copy (&message->body, 0,
909 _dbus_string_free (&retval->header);
910 _dbus_string_free (&retval->body);
916 for (i = 0; i < FIELD_LAST; i++)
918 retval->header_fields[i].offset = message->header_fields[i].offset;
926 * Increments the reference count of a DBusMessage.
928 * @param message The message
929 * @see dbus_message_unref
932 dbus_message_ref (DBusMessage *message)
934 dbus_atomic_t refcount;
936 refcount = _dbus_atomic_inc (&message->refcount);
937 _dbus_assert (refcount > 1);
941 * Decrements the reference count of a DBusMessage.
943 * @param message The message
944 * @see dbus_message_ref
947 dbus_message_unref (DBusMessage *message)
949 dbus_atomic_t refcount;
951 refcount = _dbus_atomic_dec (&message->refcount);
953 _dbus_assert (refcount >= 0);
957 if (message->size_counter != NULL)
959 _dbus_counter_adjust (message->size_counter,
960 - message->size_counter_delta);
961 _dbus_counter_unref (message->size_counter);
964 _dbus_string_free (&message->header);
965 _dbus_string_free (&message->body);
972 * Gets the name of a message.
974 * @param message the message
975 * @returns the message name (should not be freed)
978 dbus_message_get_name (DBusMessage *message)
980 return get_string_field (message, FIELD_NAME, NULL);
984 * Gets the destination service of a message.
986 * @param message the message
987 * @returns the message destination service (should not be freed)
990 dbus_message_get_service (DBusMessage *message)
992 return get_string_field (message, FIELD_SERVICE, NULL);
996 * Appends fields to a message given a variable argument
997 * list. The variable argument list should contain the type
998 * of the argument followed by the value to add. Array values
999 * are specified by a pointer to the array followed by an int
1000 * giving the length of the array. The list is terminated
1003 * @param message the message
1004 * @param first_arg_type type of the first argument
1005 * @param ... value of first argument, list of additional type-value pairs
1006 * @returns #TRUE on success
1009 dbus_message_append_args (DBusMessage *message,
1016 va_start (var_args, first_arg_type);
1017 retval = dbus_message_append_args_valist (message,
1026 * This function takes a va_list for use by language bindings
1028 * @see dbus_message_append_args.
1029 * @param message the message
1030 * @param first_arg_type type of first argument
1031 * @param var_args value of first argument, then list of type/value pairs
1032 * @returns #TRUE on success
1035 dbus_message_append_args_valist (DBusMessage *message,
1041 old_len = _dbus_string_get_length (&message->body);
1043 type = first_arg_type;
1050 if (!dbus_message_append_nil (message))
1052 case DBUS_TYPE_BOOLEAN:
1053 if (!dbus_message_append_boolean (message, va_arg (var_args, dbus_bool_t)))
1055 case DBUS_TYPE_INT32:
1056 if (!dbus_message_append_int32 (message, va_arg (var_args, dbus_int32_t)))
1059 case DBUS_TYPE_UINT32:
1060 if (!dbus_message_append_uint32 (message, va_arg (var_args, dbus_uint32_t)))
1063 case DBUS_TYPE_DOUBLE:
1064 if (!dbus_message_append_double (message, va_arg (var_args, double)))
1067 case DBUS_TYPE_STRING:
1068 if (!dbus_message_append_string (message, va_arg (var_args, const char *)))
1071 case DBUS_TYPE_BOOLEAN_ARRAY:
1074 unsigned char *data;
1076 data = va_arg (var_args, unsigned char *);
1077 len = va_arg (var_args, int);
1079 if (!dbus_message_append_boolean_array (message, data, len))
1083 case DBUS_TYPE_INT32_ARRAY:
1088 data = va_arg (var_args, dbus_int32_t *);
1089 len = va_arg (var_args, int);
1091 if (!dbus_message_append_int32_array (message, data, len))
1095 case DBUS_TYPE_UINT32_ARRAY:
1098 dbus_uint32_t *data;
1100 data = va_arg (var_args, dbus_uint32_t *);
1101 len = va_arg (var_args, int);
1103 if (!dbus_message_append_uint32_array (message, data, len))
1107 case DBUS_TYPE_DOUBLE_ARRAY:
1112 data = va_arg (var_args, double *);
1113 len = va_arg (var_args, int);
1115 if (!dbus_message_append_double_array (message, data, len))
1119 case DBUS_TYPE_BYTE_ARRAY:
1122 unsigned char *data;
1124 data = va_arg (var_args, unsigned char *);
1125 len = va_arg (var_args, int);
1127 if (!dbus_message_append_byte_array (message, data, len))
1131 case DBUS_TYPE_STRING_ARRAY:
1136 data = va_arg (var_args, const char **);
1137 len = va_arg (var_args, int);
1139 if (!dbus_message_append_string_array (message, data, len))
1145 _dbus_warn ("Unknown field type %d\n", type);
1148 type = va_arg (var_args, int);
1158 * Appends a nil value to the message
1160 * @param message the message
1161 * @returns #TRUE on success
1164 dbus_message_append_nil (DBusMessage *message)
1166 _dbus_assert (!message->locked);
1168 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_NIL))
1175 * Appends a boolean value to the message
1177 * @param message the message
1178 * @param value the boolean value
1179 * @returns #TRUE on success
1182 dbus_message_append_boolean (DBusMessage *message,
1185 _dbus_assert (!message->locked);
1187 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN))
1190 if (!_dbus_string_append_byte (&message->body, (value != FALSE)))
1192 _dbus_string_shorten (&message->body, 1);
1200 * Appends a 32 bit signed integer to the message.
1202 * @param message the message
1203 * @param value the integer value
1204 * @returns #TRUE on success
1207 dbus_message_append_int32 (DBusMessage *message,
1210 _dbus_assert (!message->locked);
1212 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32))
1215 if (!_dbus_marshal_int32 (&message->body, message->byte_order, value))
1217 _dbus_string_shorten (&message->body, 1);
1225 * Appends a 32 bit unsigned integer to the message.
1227 * @param message the message
1228 * @param value the integer value
1229 * @returns #TRUE on success
1232 dbus_message_append_uint32 (DBusMessage *message,
1233 dbus_uint32_t value)
1235 _dbus_assert (!message->locked);
1237 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32))
1240 if (!_dbus_marshal_uint32 (&message->body, message->byte_order, value))
1242 _dbus_string_shorten (&message->body, 1);
1250 * Appends a double value to the message.
1252 * @param message the message
1253 * @param value the double value
1254 * @returns #TRUE on success
1257 dbus_message_append_double (DBusMessage *message,
1260 _dbus_assert (!message->locked);
1262 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE))
1265 if (!_dbus_marshal_double (&message->body, message->byte_order, value))
1267 _dbus_string_shorten (&message->body, 1);
1275 * Appends a UTF-8 string to the message.
1277 * @param message the message
1278 * @param value the string
1279 * @returns #TRUE on success
1282 dbus_message_append_string (DBusMessage *message,
1285 _dbus_assert (!message->locked);
1287 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING))
1290 if (!_dbus_marshal_string (&message->body, message->byte_order, value))
1292 _dbus_string_shorten (&message->body, 1);
1300 * Appends a boolean array to the message.
1302 * @param message the message
1303 * @param value the array
1304 * @param len the length of the array
1305 * @returns #TRUE on success
1308 dbus_message_append_boolean_array (DBusMessage *message,
1309 unsigned const char *value,
1312 _dbus_assert (!message->locked);
1314 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN_ARRAY))
1317 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1319 _dbus_string_shorten (&message->body, 1);
1327 * Appends a 32 bit signed integer array to the message.
1329 * @param message the message
1330 * @param value the array
1331 * @param len the length of the array
1332 * @returns #TRUE on success
1335 dbus_message_append_int32_array (DBusMessage *message,
1336 const dbus_int32_t *value,
1339 _dbus_assert (!message->locked);
1341 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32_ARRAY))
1344 if (!_dbus_marshal_int32_array (&message->body, message->byte_order,
1347 _dbus_string_shorten (&message->body, 1);
1355 * Appends a 32 bit unsigned integer array to the message.
1357 * @param message the message
1358 * @param value the array
1359 * @param len the length of the array
1360 * @returns #TRUE on success
1363 dbus_message_append_uint32_array (DBusMessage *message,
1364 const dbus_uint32_t *value,
1367 _dbus_assert (!message->locked);
1369 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32_ARRAY))
1372 if (!_dbus_marshal_uint32_array (&message->body, message->byte_order,
1375 _dbus_string_shorten (&message->body, 1);
1383 * Appends a double array to the message.
1385 * @param message the message
1386 * @param value the array
1387 * @param len the length of the array
1388 * @returns #TRUE on success
1391 dbus_message_append_double_array (DBusMessage *message,
1392 const double *value,
1395 _dbus_assert (!message->locked);
1397 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE_ARRAY))
1400 if (!_dbus_marshal_double_array (&message->body, message->byte_order,
1403 _dbus_string_shorten (&message->body, 1);
1411 * Appends a byte array to the message.
1413 * @param message the message
1414 * @param value the array
1415 * @param len the length of the array
1416 * @returns #TRUE on success
1419 dbus_message_append_byte_array (DBusMessage *message,
1420 unsigned const char *value,
1423 _dbus_assert (!message->locked);
1425 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY))
1428 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1430 _dbus_string_shorten (&message->body, 1);
1438 * Appends a string array to the message.
1440 * @param message the message
1441 * @param value the array
1442 * @param len the length of the array
1443 * @returns #TRUE on success
1446 dbus_message_append_string_array (DBusMessage *message,
1450 _dbus_assert (!message->locked);
1452 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING_ARRAY))
1455 if (!_dbus_marshal_string_array (&message->body, message->byte_order,
1458 _dbus_string_shorten (&message->body, 1);
1466 * Gets arguments from a message given a variable argument list.
1467 * The variable argument list should contain the type of the
1468 * argumen followed by a pointer to where the value should be
1469 * stored. The list is terminated with 0.
1471 * @param message the message
1472 * @param first_arg_type the first argument type
1473 * @param ... location for first argument value, then list of type-location pairs
1474 * @returns result code
1477 dbus_message_get_args (DBusMessage *message,
1481 DBusResultCode retval;
1484 va_start (var_args, first_arg_type);
1485 retval = dbus_message_get_args_valist (message, first_arg_type, var_args);
1492 * This function takes a va_list for use by language bindings
1494 * @todo this function (or some lower-level non-convenience function)
1495 * needs better error handling; should allow the application to
1496 * distinguish between out of memory, and bad data from the remote
1497 * app. It also needs to not leak a bunch of args when it gets
1498 * to the arg that's bad, as that would be a security hole
1499 * (allow one app to force another to leak memory)
1501 * @todo We need to free the argument data when an error occurs.
1503 * @see dbus_message_get_args
1504 * @param message the message
1505 * @param first_arg_type type of the first argument
1506 * @param var_args return location for first argument, followed by list of type/location pairs
1507 * @returns result code
1510 dbus_message_get_args_valist (DBusMessage *message,
1514 int spec_type, msg_type, i;
1515 DBusMessageIter *iter;
1517 iter = dbus_message_get_args_iter (message);
1520 return DBUS_RESULT_NO_MEMORY;
1522 spec_type = first_arg_type;
1525 while (spec_type != 0)
1527 msg_type = dbus_message_iter_get_arg_type (iter);
1529 if (msg_type != spec_type)
1531 _dbus_verbose ("Argument %d is specified to be of type \"%s\", but "
1532 "is actually of type \"%s\"\n", i,
1533 _dbus_type_to_string (spec_type),
1534 _dbus_type_to_string (msg_type));
1535 dbus_message_iter_unref (iter);
1537 return DBUS_RESULT_INVALID_ARGS;
1544 case DBUS_TYPE_BOOLEAN:
1548 ptr = va_arg (var_args, dbus_bool_t *);
1550 *ptr = dbus_message_iter_get_boolean (iter);
1553 case DBUS_TYPE_INT32:
1557 ptr = va_arg (var_args, dbus_int32_t *);
1559 *ptr = dbus_message_iter_get_int32 (iter);
1562 case DBUS_TYPE_UINT32:
1566 ptr = va_arg (var_args, dbus_uint32_t *);
1568 *ptr = dbus_message_iter_get_uint32 (iter);
1572 case DBUS_TYPE_DOUBLE:
1576 ptr = va_arg (var_args, double *);
1578 *ptr = dbus_message_iter_get_double (iter);
1582 case DBUS_TYPE_STRING:
1586 ptr = va_arg (var_args, char **);
1588 *ptr = dbus_message_iter_get_string (iter);
1591 return DBUS_RESULT_NO_MEMORY;
1596 case DBUS_TYPE_BOOLEAN_ARRAY:
1598 unsigned char **ptr;
1601 ptr = va_arg (var_args, unsigned char **);
1602 len = va_arg (var_args, int *);
1604 if (!dbus_message_iter_get_boolean_array (iter, ptr, len))
1605 return DBUS_RESULT_NO_MEMORY;
1610 case DBUS_TYPE_INT32_ARRAY:
1615 ptr = va_arg (var_args, dbus_int32_t **);
1616 len = va_arg (var_args, int *);
1618 if (!dbus_message_iter_get_int32_array (iter, ptr, len))
1619 return DBUS_RESULT_NO_MEMORY;
1624 case DBUS_TYPE_UINT32_ARRAY:
1626 dbus_uint32_t **ptr;
1629 ptr = va_arg (var_args, dbus_uint32_t **);
1630 len = va_arg (var_args, int *);
1632 if (!dbus_message_iter_get_uint32_array (iter, ptr, len))
1633 return DBUS_RESULT_NO_MEMORY;
1638 case DBUS_TYPE_DOUBLE_ARRAY:
1643 ptr = va_arg (var_args, double **);
1644 len = va_arg (var_args, int *);
1646 if (!dbus_message_iter_get_double_array (iter, ptr, len))
1647 return DBUS_RESULT_NO_MEMORY;
1652 case DBUS_TYPE_BYTE_ARRAY:
1654 unsigned char **ptr;
1657 ptr = va_arg (var_args, unsigned char **);
1658 len = va_arg (var_args, int *);
1660 if (!dbus_message_iter_get_byte_array (iter, ptr, len))
1661 return DBUS_RESULT_NO_MEMORY;
1665 case DBUS_TYPE_STRING_ARRAY:
1670 ptr = va_arg (var_args, char ***);
1671 len = va_arg (var_args, int *);
1673 if (!dbus_message_iter_get_string_array (iter, ptr, len))
1674 return DBUS_RESULT_NO_MEMORY;
1678 _dbus_warn ("Unknown field type %d\n", spec_type);
1681 spec_type = va_arg (var_args, int);
1682 if (spec_type != 0 && !dbus_message_iter_next (iter))
1684 _dbus_verbose ("More fields than exist in the message were specified or field is corrupt\n");
1686 dbus_message_iter_unref (iter);
1687 return DBUS_RESULT_INVALID_ARGS;
1692 dbus_message_iter_unref (iter);
1693 return DBUS_RESULT_SUCCESS;
1697 * Returns a DBusMessageIter representing the arguments of the
1698 * message passed in.
1700 * @todo IMO the message iter should follow the GtkTextIter pattern,
1701 * a static object with a "stamp" value used to detect invalid
1702 * iter uses (uninitialized or after changing the message).
1703 * ref/unref is kind of annoying to deal with, and slower too.
1704 * This implies not ref'ing the message from the iter.
1706 * @param message the message
1707 * @returns a new iter.
1710 dbus_message_get_args_iter (DBusMessage *message)
1712 DBusMessageIter *iter;
1714 iter = dbus_new (DBusMessageIter, 1);
1716 dbus_message_ref (message);
1719 iter->message = message;
1726 * Increments the reference count of a DBusMessageIter.
1728 * @param iter the message iter
1729 * @see dbus_message_iter_unref
1732 dbus_message_iter_ref (DBusMessageIter *iter)
1734 _dbus_assert (iter->refcount > 0);
1736 iter->refcount += 1;
1740 * Decrements the reference count of a DBusMessageIter.
1742 * @param iter The message iter
1743 * @see dbus_message_iter_ref
1746 dbus_message_iter_unref (DBusMessageIter *iter)
1748 _dbus_assert (iter->refcount > 0);
1750 iter->refcount -= 1;
1752 if (iter->refcount == 0)
1754 dbus_message_unref (iter->message);
1761 * Checks if an iterator has any more fields.
1763 * @param iter the message iter
1764 * @returns #TRUE if there are more fields
1768 dbus_message_iter_has_next (DBusMessageIter *iter)
1772 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1773 iter->message->byte_order,
1774 iter->pos, &end_pos))
1777 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1784 * Moves the iterator to the next field.
1786 * @param iter The message iter
1787 * @returns #TRUE if the iterator was moved to the next field
1790 dbus_message_iter_next (DBusMessageIter *iter)
1794 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1795 iter->message->byte_order,
1796 iter->pos, &end_pos))
1799 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1802 iter->pos = end_pos;
1808 * Returns the argument type of the argument that the
1809 * message iterator points at.
1811 * @param iter the message iter
1812 * @returns the field type
1815 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1819 if (iter->pos >= _dbus_string_get_length (&iter->message->body))
1820 return DBUS_TYPE_INVALID;
1822 _dbus_string_get_const_data_len (&iter->message->body, &data, iter->pos, 1);
1824 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_STRING_ARRAY)
1827 return DBUS_TYPE_INVALID;
1831 * Returns the string value that an iterator may point to.
1832 * Note that you need to check that the iterator points to
1833 * a string value before using this function.
1835 * @see dbus_message_iter_get_arg_type
1836 * @param iter the message iter
1837 * @returns the string
1840 dbus_message_iter_get_string (DBusMessageIter *iter)
1842 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING);
1844 return _dbus_demarshal_string (&iter->message->body, iter->message->byte_order,
1845 iter->pos + 1, NULL);
1849 * Returns the boolean value that an iterator may point to.
1850 * Note that you need to check that the iterator points to
1851 * a boolean value before using this function.
1853 * @see dbus_message_iter_get_arg_type
1854 * @param iter the message iter
1855 * @returns the string
1858 dbus_message_iter_get_boolean (DBusMessageIter *iter)
1860 unsigned char value;
1862 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN);
1864 value = _dbus_string_get_byte (&iter->message->body, iter->pos + 1);
1871 * Returns the 32 bit signed integer value that an iterator may point to.
1872 * Note that you need to check that the iterator points to
1873 * an integer value before using this function.
1875 * @see dbus_message_iter_get_arg_type
1876 * @param iter the message iter
1877 * @returns the integer
1880 dbus_message_iter_get_int32 (DBusMessageIter *iter)
1882 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32);
1884 return _dbus_demarshal_int32 (&iter->message->body, iter->message->byte_order,
1885 iter->pos + 1, NULL);
1889 * Returns the 32 bit unsigned integer value that an iterator may point to.
1890 * Note that you need to check that the iterator points to
1891 * an unsigned integer value before using this function.
1893 * @see dbus_message_iter_get_arg_type
1894 * @param iter the message iter
1895 * @returns the integer
1898 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
1900 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32);
1902 return _dbus_demarshal_uint32 (&iter->message->body, iter->message->byte_order,
1903 iter->pos + 1, NULL);
1907 * Returns the double value that an iterator may point to.
1908 * Note that you need to check that the iterator points to
1909 * a string value before using this function.
1911 * @see dbus_message_iter_get_arg_type
1912 * @param iter the message iter
1913 * @returns the double
1916 dbus_message_iter_get_double (DBusMessageIter *iter)
1918 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE);
1920 return _dbus_demarshal_double (&iter->message->body, iter->message->byte_order,
1921 iter->pos + 1, NULL);
1925 * Returns the boolean array that the iterator may point to. Note that
1926 * you need to check that the iterator points to an array of the
1927 * correct type prior to using this function.
1929 * @param iter the iterator
1930 * @param value return location for the array
1931 * @param len return location for the array length
1932 * @returns #TRUE on success
1935 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
1936 unsigned char **value,
1939 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN_ARRAY);
1941 *value = _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
1942 iter->pos + 1, NULL, len);
1951 * Returns the 32 bit signed integer array that the iterator may point
1952 * to. Note that you need to check that the iterator points to an
1953 * array of the correct type prior to using this function.
1955 * @param iter the iterator
1956 * @param value return location for the array
1957 * @param len return location for the array length
1958 * @returns #TRUE on success
1961 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
1962 dbus_int32_t **value,
1965 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32_ARRAY);
1967 *value = _dbus_demarshal_int32_array (&iter->message->body, iter->message->byte_order,
1968 iter->pos + 1, NULL, len);
1977 * Returns the 32 bit unsigned integer array that the iterator may point
1978 * to. Note that you need to check that the iterator points to an
1979 * array of the correct type prior to using this function.
1981 * @param iter the iterator
1982 * @param value return location for the array
1983 * @param len return location for the array length
1984 * @returns #TRUE on success
1987 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
1988 dbus_uint32_t **value,
1991 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32_ARRAY);
1993 *value = _dbus_demarshal_uint32_array (&iter->message->body, iter->message->byte_order,
1994 iter->pos + 1, NULL, len);
2003 * Returns the double array that the iterator may point to. Note that
2004 * you need to check that the iterator points to an array of the
2005 * correct type prior to using this function.
2007 * @param iter the iterator
2008 * @param value return location for the array
2009 * @param len return location for the array length
2010 * @returns #TRUE on success
2013 dbus_message_iter_get_double_array (DBusMessageIter *iter,
2017 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE_ARRAY);
2019 *value = _dbus_demarshal_double_array (&iter->message->body, iter->message->byte_order,
2020 iter->pos + 1, NULL, len);
2029 * Returns the byte array that the iterator may point to.
2030 * Note that you need to check that the iterator points
2031 * to a byte array prior to using this function.
2033 * @param iter the iterator
2034 * @param value return location for array values
2035 * @param len return location for length of byte array
2036 * @returns the byte array
2039 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
2040 unsigned char **value,
2043 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BYTE_ARRAY);
2045 *value = _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2046 iter->pos + 1, NULL, len);
2055 * Returns the string array that the iterator may point to.
2056 * Note that you need to check that the iterator points
2057 * to a byte array prior to using this function.
2059 * The returned value is a #NULL-terminated array of strings.
2060 * Each string is a separate malloc block, and the array
2061 * itself is a malloc block. You can free this type of
2062 * string array with dbus_free_string_array().
2064 * @param iter the iterator
2065 * @param value return location for string values
2066 * @param len return location for length of byte array
2067 * @returns the byte array
2070 dbus_message_iter_get_string_array (DBusMessageIter *iter,
2074 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING_ARRAY);
2076 *value = _dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
2077 iter->pos + 1, NULL, len);
2086 * Sets the message sender.
2088 * @param message the message
2089 * @param sender the sender
2090 * @returns #FALSE if not enough memory
2093 dbus_message_set_sender (DBusMessage *message,
2096 _dbus_assert (!message->locked);
2100 delete_string_field (message, FIELD_SENDER);
2105 return set_string_field (message,
2112 * Sets a flag indicating that the message is an error reply
2113 * message, i.e. an "exception" rather than a normal response.
2115 * @param message the message
2116 * @param is_error_reply #TRUE if this is an error message.
2119 dbus_message_set_is_error (DBusMessage *message,
2120 dbus_bool_t is_error_reply)
2124 _dbus_assert (!message->locked);
2126 _dbus_string_get_data_len (&message->header, &header, 1, 1);
2129 *header |= DBUS_HEADER_FLAG_ERROR;
2131 *header &= ~DBUS_HEADER_FLAG_ERROR;
2135 * Returns #TRUE if the message is an error
2136 * reply to some previous message we sent.
2138 * @param message the message
2139 * @returns #TRUE if the message is an error
2142 dbus_message_get_is_error (DBusMessage *message)
2146 _dbus_string_get_const_data_len (&message->header, &header, 1, 1);
2148 return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
2152 * Gets the service which originated this message,
2153 * or #NULL if unknown or inapplicable.
2155 * @param message the message
2156 * @returns the service name or #NULL
2159 dbus_message_get_sender (DBusMessage *message)
2161 return get_string_field (message, FIELD_SENDER, NULL);
2165 * Checks whether the message has the given name.
2166 * If the message has no name or has a different
2167 * name, returns #FALSE.
2169 * @param message the message
2170 * @param name the name to check (must not be #NULL)
2172 * @returns #TRUE if the message has the given name
2175 dbus_message_name_is (DBusMessage *message,
2180 _dbus_assert (name != NULL);
2182 n = dbus_message_get_name (message);
2184 if (n && strcmp (n, name) == 0)
2193 * @addtogroup DBusMessageInternals
2198 * @typedef DBusMessageLoader
2200 * The DBusMessageLoader object encapsulates the process of converting
2201 * a byte stream into a series of DBusMessage. It buffers the incoming
2202 * bytes as efficiently as possible, and generates a queue of
2203 * messages. DBusMessageLoader is typically used as part of a
2204 * DBusTransport implementation. The DBusTransport then hands off
2205 * the loaded messages to a DBusConnection, making the messages
2206 * visible to the application.
2210 /* we definitely use signed ints for sizes, so don't exceed
2211 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
2212 * over 128M is pretty nuts anyhow.
2216 * The maximum sane message size.
2218 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
2221 * Implementation details of DBusMessageLoader.
2222 * All members are private.
2224 struct DBusMessageLoader
2226 int refcount; /**< Reference count. */
2228 DBusString data; /**< Buffered data */
2230 DBusList *messages; /**< Complete messages. */
2232 long max_message_size; /**< Maximum size of a message */
2234 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
2236 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
2240 * The initial buffer size of the message loader.
2242 * @todo this should be based on min header size plus some average
2243 * body size, or something. Or rather, the min header size only, if we
2244 * want to try to read only the header, store that in a DBusMessage,
2245 * then read only the body and store that, etc., depends on
2246 * how we optimize _dbus_message_loader_get_buffer() and what
2247 * the exact message format is.
2249 #define INITIAL_LOADER_DATA_LEN 32
2252 * Creates a new message loader. Returns #NULL if memory can't
2255 * @returns new loader, or #NULL.
2258 _dbus_message_loader_new (void)
2260 DBusMessageLoader *loader;
2262 loader = dbus_new0 (DBusMessageLoader, 1);
2266 loader->refcount = 1;
2268 /* Try to cap message size at something that won't *totally* hose
2269 * the system if we have a couple of them.
2271 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
2273 if (!_dbus_string_init (&loader->data, _DBUS_INT_MAX))
2279 /* preallocate the buffer for speed, ignore failure */
2280 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
2281 _dbus_string_set_length (&loader->data, 0);
2287 * Increments the reference count of the loader.
2289 * @param loader the loader.
2292 _dbus_message_loader_ref (DBusMessageLoader *loader)
2294 loader->refcount += 1;
2298 * Decrements the reference count of the loader and finalizes the
2299 * loader when the count reaches zero.
2301 * @param loader the loader.
2304 _dbus_message_loader_unref (DBusMessageLoader *loader)
2306 loader->refcount -= 1;
2307 if (loader->refcount == 0)
2309 _dbus_list_foreach (&loader->messages,
2310 (DBusForeachFunction) dbus_message_unref,
2312 _dbus_list_clear (&loader->messages);
2313 _dbus_string_free (&loader->data);
2319 * Gets the buffer to use for reading data from the network. Network
2320 * data is read directly into an allocated buffer, which is then used
2321 * in the DBusMessage, to avoid as many extra memcpy's as possible.
2322 * The buffer must always be returned immediately using
2323 * _dbus_message_loader_return_buffer(), even if no bytes are
2324 * successfully read.
2326 * @todo this function can be a lot more clever. For example
2327 * it can probably always return a buffer size to read exactly
2328 * the body of the next message, thus avoiding any memory wastage
2331 * @param loader the message loader.
2332 * @param buffer the buffer
2335 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
2336 DBusString **buffer)
2338 _dbus_assert (!loader->buffer_outstanding);
2340 *buffer = &loader->data;
2342 loader->buffer_outstanding = TRUE;
2346 * The smallest header size that can occur.
2347 * (It won't be valid)
2349 #define DBUS_MINIMUM_HEADER_SIZE 16
2351 /** Pack four characters as in "abcd" into a uint32 */
2352 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
2353 ((((dbus_uint32_t)a) << 24) | \
2354 (((dbus_uint32_t)b) << 16) | \
2355 (((dbus_uint32_t)c) << 8) | \
2358 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
2359 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
2360 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
2362 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
2363 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
2364 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
2366 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
2367 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
2368 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
2370 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
2371 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
2372 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
2375 decode_header_data (const DBusString *data,
2378 HeaderField fields[FIELD_LAST],
2379 int *message_padding)
2385 if (header_len < 16)
2389 while (i < FIELD_LAST)
2391 fields[i].offset = -1;
2395 fields[FIELD_HEADER_LENGTH].offset = 4;
2396 fields[FIELD_BODY_LENGTH].offset = 8;
2397 fields[FIELD_CLIENT_SERIAL].offset = 12;
2399 /* Now handle the named fields. A real named field is at least 4
2400 * bytes for the name, plus a type code (1 byte) plus padding. So
2401 * if we have less than 8 bytes left, it must be alignment padding,
2402 * not a field. While >= 8 bytes can't be entirely alignment
2406 while ((pos + 7) < header_len)
2408 pos = _DBUS_ALIGN_VALUE (pos, 4);
2410 if ((pos + 4) > header_len)
2413 _dbus_string_get_const_data_len (data, &field, pos, 4);
2416 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
2418 /* I believe FROM_BE is right, but if not we'll find out
2421 switch (DBUS_UINT32_FROM_BE (*(int*)field))
2423 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
2424 if (fields[FIELD_SERVICE].offset >= 0)
2426 _dbus_verbose ("%s field provided twice\n",
2427 DBUS_HEADER_FIELD_SERVICE);
2431 fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2432 _dbus_verbose ("Found service name at offset %d\n",
2433 fields[FIELD_SERVICE].offset);
2436 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
2437 if (fields[FIELD_NAME].offset >= 0)
2439 _dbus_verbose ("%s field provided twice\n",
2440 DBUS_HEADER_FIELD_NAME);
2444 fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2446 _dbus_verbose ("Found message name at offset %d\n",
2447 fields[FIELD_NAME].offset);
2449 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
2450 if (fields[FIELD_SENDER].offset >= 0)
2452 _dbus_verbose ("%s field provided twice\n",
2453 DBUS_HEADER_FIELD_SENDER);
2457 fields[FIELD_SENDER].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2459 _dbus_verbose ("Found sender name at offset %d\n",
2460 fields[FIELD_NAME].offset);
2463 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
2464 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
2466 _dbus_verbose ("%s field provided twice\n",
2467 DBUS_HEADER_FIELD_REPLY);
2471 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2473 _dbus_verbose ("Found reply serial at offset %d\n",
2474 fields[FIELD_REPLY_SERIAL].offset);
2478 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c at offset %d\n",
2479 field[0], field[1], field[2], field[3], pos);
2482 if (!_dbus_marshal_validate_arg (data, byte_order, pos, &new_pos))
2484 _dbus_verbose ("Failed to validate argument to named header field\n");
2488 if (new_pos > header_len)
2490 _dbus_verbose ("Named header field tries to extend beyond header length\n");
2497 if (pos < header_len)
2499 /* Alignment padding, verify that it's nul */
2500 _dbus_assert ((header_len - pos) < 8);
2502 if (!_dbus_string_validate_nul (data,
2503 pos, (header_len - pos)))
2505 _dbus_verbose ("header alignment padding is not nul\n");
2509 if (message_padding)
2510 *message_padding = header_len - pos;
2517 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
2518 * indicating to the loader how many bytes of the buffer were filled
2519 * in. This function must always be called, even if no bytes were
2520 * successfully read.
2522 * @todo if we run out of memory in here, we offer no way for calling
2523 * code to handle it, i.e. they can't re-run the message parsing
2524 * attempt. Perhaps much of this code could be moved to pop_message()?
2525 * But then that may need to distinguish NULL return for no messages
2526 * from NULL return for errors.
2528 * @param loader the loader.
2529 * @param buffer the buffer.
2530 * @param bytes_read number of bytes that were read into the buffer.
2533 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
2537 _dbus_assert (loader->buffer_outstanding);
2538 _dbus_assert (buffer == &loader->data);
2540 loader->buffer_outstanding = FALSE;
2542 if (loader->corrupted)
2545 while (_dbus_string_get_length (&loader->data) >= 16)
2547 DBusMessage *message;
2548 const char *header_data;
2549 int byte_order, header_len, body_len, header_padding;
2550 dbus_uint32_t header_len_unsigned, body_len_unsigned;
2552 _dbus_string_get_const_data_len (&loader->data, &header_data, 0, 16);
2554 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
2556 if (header_data[2] != DBUS_MAJOR_PROTOCOL_VERSION)
2558 _dbus_verbose ("Message has protocol version %d ours is %d\n",
2559 (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
2560 loader->corrupted = TRUE;
2564 byte_order = header_data[0];
2566 if (byte_order != DBUS_LITTLE_ENDIAN &&
2567 byte_order != DBUS_BIG_ENDIAN)
2569 _dbus_verbose ("Message with bad byte order '%c' received\n",
2571 loader->corrupted = TRUE;
2575 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
2576 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
2578 if (header_len_unsigned < 16)
2580 _dbus_verbose ("Message had broken too-small header length %u\n",
2581 header_len_unsigned);
2582 loader->corrupted = TRUE;
2586 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
2587 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
2589 _dbus_verbose ("Header or body length too large (%u %u)\n",
2590 header_len_unsigned,
2592 loader->corrupted = TRUE;
2596 /* Now that we know the values are in signed range, get
2597 * rid of stupid unsigned, just causes bugs
2599 header_len = header_len_unsigned;
2600 body_len = body_len_unsigned;
2602 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
2604 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
2606 loader->corrupted = TRUE;
2610 if (header_len + body_len > loader->max_message_size)
2612 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %d\n",
2613 header_len, body_len, loader->max_message_size);
2614 loader->corrupted = TRUE;
2618 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
2620 HeaderField fields[FIELD_LAST];
2624 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len);
2625 if (!decode_header_data (&loader->data, header_len, byte_order,
2626 fields, &header_padding))
2628 _dbus_verbose ("Header was invalid\n");
2629 loader->corrupted = TRUE;
2633 next_arg = header_len;
2634 while (next_arg < (header_len + body_len))
2636 int prev = next_arg;
2638 if (!_dbus_marshal_validate_arg (&loader->data,
2643 loader->corrupted = TRUE;
2647 _dbus_assert (next_arg > prev);
2650 if (next_arg > (header_len + body_len))
2652 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
2653 next_arg, header_len, body_len,
2654 header_len + body_len);
2655 loader->corrupted = TRUE;
2659 message = dbus_message_new_empty_header ();
2660 if (message == NULL)
2661 break; /* ugh, postpone this I guess. */
2663 message->byte_order = byte_order;
2664 message->header_padding = header_padding;
2666 /* Copy in the offsets we found */
2668 while (i < FIELD_LAST)
2670 message->header_fields[i] = fields[i];
2674 if (!_dbus_list_append (&loader->messages, message))
2676 dbus_message_unref (message);
2680 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
2681 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
2683 _dbus_assert (_dbus_string_get_length (&loader->data) >=
2684 (header_len + body_len));
2686 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
2688 _dbus_list_remove_last (&loader->messages, message);
2689 dbus_message_unref (message);
2693 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
2697 /* put the header back, we'll try again later */
2698 result = _dbus_string_copy_len (&message->header, 0, header_len,
2700 _dbus_assert (result); /* because DBusString never reallocs smaller */
2702 _dbus_list_remove_last (&loader->messages, message);
2703 dbus_message_unref (message);
2707 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
2708 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
2710 _dbus_verbose ("Loaded message %p\n", message);
2718 * Pops a loaded message (passing ownership of the message
2719 * to the caller). Returns #NULL if no messages have been
2722 * @param loader the loader.
2723 * @returns the next message, or #NULL if none.
2726 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
2728 return _dbus_list_pop_first (&loader->messages);
2733 * Checks whether the loader is confused due to bad data.
2734 * If messages are received that are invalid, the
2735 * loader gets confused and gives up permanently.
2736 * This state is called "corrupted."
2738 * @param loader the loader
2739 * @returns #TRUE if the loader is hosed.
2742 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
2744 return loader->corrupted;
2748 * Sets the maximum size message we allow.
2750 * @param loader the loader
2751 * @param size the max message size in bytes
2754 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
2757 if (size > MAX_SANE_MESSAGE_SIZE)
2759 _dbus_verbose ("clamping requested max message size %ld to %d\n",
2760 size, MAX_SANE_MESSAGE_SIZE);
2761 size = MAX_SANE_MESSAGE_SIZE;
2763 loader->max_message_size = size;
2767 * Gets the maximum allowed message size in bytes.
2769 * @param loader the loader
2770 * @returns max size in bytes
2773 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
2775 return loader->max_message_size;
2779 #ifdef DBUS_BUILD_TESTS
2780 #include "dbus-test.h"
2784 message_iter_test (DBusMessage *message)
2786 DBusMessageIter *iter;
2789 iter = dbus_message_get_args_iter (message);
2792 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_STRING)
2793 _dbus_assert_not_reached ("Argument type isn't string");
2795 str = dbus_message_iter_get_string (iter);
2796 if (strcmp (str, "Test string") != 0)
2797 _dbus_assert_not_reached ("Strings differ");
2800 if (!dbus_message_iter_next (iter))
2801 _dbus_assert_not_reached ("Reached end of arguments");
2803 /* Signed integer tests */
2804 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_INT32)
2805 _dbus_assert_not_reached ("Argument type isn't int32");
2807 if (dbus_message_iter_get_int32 (iter) != -0x12345678)
2808 _dbus_assert_not_reached ("Signed integers differ");
2810 if (!dbus_message_iter_next (iter))
2811 _dbus_assert_not_reached ("Reached end of fields");
2813 /* Unsigned integer tests */
2814 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_UINT32)
2815 _dbus_assert_not_reached ("Argument type isn't int32");
2817 if (dbus_message_iter_get_uint32 (iter) != 0xedd1e)
2818 _dbus_assert_not_reached ("Unsigned integers differ");
2820 if (!dbus_message_iter_next (iter))
2821 _dbus_assert_not_reached ("Reached end of arguments");
2824 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_DOUBLE)
2825 _dbus_assert_not_reached ("Argument type isn't double");
2827 if (dbus_message_iter_get_double (iter) != 3.14159)
2828 _dbus_assert_not_reached ("Doubles differ");
2830 if (dbus_message_iter_next (iter))
2831 _dbus_assert_not_reached ("Didn't reach end of arguments");
2833 dbus_message_iter_unref (iter);
2837 check_message_handling (DBusMessage *message)
2839 DBusMessageIter *iter;
2842 dbus_int32_t client_serial;
2847 client_serial = _dbus_message_get_client_serial (message);
2849 /* can't use set_client_serial due to the assertions at the start of it */
2850 set_int_field (message, FIELD_CLIENT_SERIAL,
2853 if (client_serial != _dbus_message_get_client_serial (message))
2855 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
2859 /* If we implement message_set_arg (message, n, value)
2860 * then we would want to test it here
2863 iter = dbus_message_get_args_iter (message);
2864 while ((type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID)
2870 case DBUS_TYPE_INT32:
2871 dbus_message_iter_get_int32 (iter);
2873 case DBUS_TYPE_UINT32:
2874 dbus_message_iter_get_uint32 (iter);
2876 case DBUS_TYPE_DOUBLE:
2877 dbus_message_iter_get_double (iter);
2879 case DBUS_TYPE_STRING:
2882 str = dbus_message_iter_get_string (iter);
2886 case DBUS_TYPE_INT32_ARRAY:
2888 dbus_int32_t *values;
2891 if (!dbus_message_iter_get_int32_array (iter, &values, &len))
2897 case DBUS_TYPE_UINT32_ARRAY:
2899 dbus_uint32_t *values;
2902 if (!dbus_message_iter_get_uint32_array (iter, &values, &len))
2908 case DBUS_TYPE_DOUBLE_ARRAY:
2913 if (!dbus_message_iter_get_double_array (iter, &values, &len))
2919 case DBUS_TYPE_STRING_ARRAY:
2924 if (!dbus_message_iter_get_string_array (iter, &values, &len))
2927 dbus_free_string_array (values);
2935 if (!dbus_message_iter_next (iter))
2943 dbus_message_iter_unref (iter);
2949 check_have_valid_message (DBusMessageLoader *loader)
2951 DBusMessage *message;
2957 if (_dbus_message_loader_get_is_corrupted (loader))
2959 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
2963 message = _dbus_message_loader_pop_message (loader);
2964 if (message == NULL)
2966 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
2970 if (_dbus_string_get_length (&loader->data) > 0)
2972 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
2976 /* Verify that we're able to properly deal with the message.
2977 * For example, this would detect improper handling of messages
2978 * in nonstandard byte order.
2980 if (!check_message_handling (message))
2987 dbus_message_unref (message);
2992 check_invalid_message (DBusMessageLoader *loader)
2998 if (!_dbus_message_loader_get_is_corrupted (loader))
3000 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
3011 check_incomplete_message (DBusMessageLoader *loader)
3013 DBusMessage *message;
3019 if (_dbus_message_loader_get_is_corrupted (loader))
3021 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
3025 message = _dbus_message_loader_pop_message (loader);
3026 if (message != NULL)
3028 _dbus_warn ("loaded message that was expected to be incomplete\n");
3036 dbus_message_unref (message);
3041 check_loader_results (DBusMessageLoader *loader,
3042 DBusMessageValidity validity)
3046 case _DBUS_MESSAGE_VALID:
3047 return check_have_valid_message (loader);
3048 case _DBUS_MESSAGE_INVALID:
3049 return check_invalid_message (loader);
3050 case _DBUS_MESSAGE_INCOMPLETE:
3051 return check_incomplete_message (loader);
3052 case _DBUS_MESSAGE_UNKNOWN:
3056 _dbus_assert_not_reached ("bad DBusMessageValidity");
3062 * Loads the message in the given message file.
3064 * @param filename filename to load
3065 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3066 * @param data string to load message into
3067 * @returns #TRUE if the message was loaded
3070 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
3080 DBusResultCode result;
3082 result = _dbus_file_get_contents (data, filename);
3083 if (result != DBUS_RESULT_SUCCESS)
3086 _dbus_string_get_const_data (filename, &s);
3087 _dbus_warn ("Could not load message file %s\n", s);
3093 if (!_dbus_message_data_load (data, filename))
3096 _dbus_string_get_const_data (filename, &s);
3097 _dbus_warn ("Could not load message file %s\n", s);
3110 * Tries loading the message in the given message file
3111 * and verifies that DBusMessageLoader can handle it.
3113 * @param filename filename to load
3114 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3115 * @param expected_validity what the message has to be like to return #TRUE
3116 * @returns #TRUE if the message has the expected validity
3119 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
3121 DBusMessageValidity expected_validity)
3128 if (!_dbus_string_init (&data, _DBUS_INT_MAX))
3129 _dbus_assert_not_reached ("could not allocate string\n");
3131 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
3135 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
3143 if (_dbus_string_get_length (&data) > 0)
3144 _dbus_verbose_bytes_of_string (&data, 0,
3145 _dbus_string_get_length (&data));
3147 _dbus_string_get_const_data (filename, &s);
3148 _dbus_warn ("Failed message loader test on %s\n",
3152 _dbus_string_free (&data);
3158 * Tries loading the given message data.
3161 * @param data the message data
3162 * @param expected_validity what the message has to be like to return #TRUE
3163 * @returns #TRUE if the message has the expected validity
3166 dbus_internal_do_not_use_try_message_data (const DBusString *data,
3167 DBusMessageValidity expected_validity)
3169 DBusMessageLoader *loader;
3177 /* Write the data one byte at a time */
3179 loader = _dbus_message_loader_new ();
3181 len = _dbus_string_get_length (data);
3182 for (i = 0; i < len; i++)
3186 _dbus_message_loader_get_buffer (loader, &buffer);
3187 _dbus_string_append_byte (buffer,
3188 _dbus_string_get_byte (data, i));
3189 _dbus_message_loader_return_buffer (loader, buffer, 1);
3192 if (!check_loader_results (loader, expected_validity))
3195 _dbus_message_loader_unref (loader);
3198 /* Write the data all at once */
3200 loader = _dbus_message_loader_new ();
3205 _dbus_message_loader_get_buffer (loader, &buffer);
3206 _dbus_string_copy (data, 0, buffer,
3207 _dbus_string_get_length (buffer));
3208 _dbus_message_loader_return_buffer (loader, buffer, 1);
3211 if (!check_loader_results (loader, expected_validity))
3214 _dbus_message_loader_unref (loader);
3217 /* Write the data 2 bytes at a time */
3219 loader = _dbus_message_loader_new ();
3221 len = _dbus_string_get_length (data);
3222 for (i = 0; i < len; i += 2)
3226 _dbus_message_loader_get_buffer (loader, &buffer);
3227 _dbus_string_append_byte (buffer,
3228 _dbus_string_get_byte (data, i));
3230 _dbus_string_append_byte (buffer,
3231 _dbus_string_get_byte (data, i+1));
3232 _dbus_message_loader_return_buffer (loader, buffer, 1);
3235 if (!check_loader_results (loader, expected_validity))
3238 _dbus_message_loader_unref (loader);
3246 _dbus_message_loader_unref (loader);
3252 process_test_subdir (const DBusString *test_base_dir,
3254 DBusMessageValidity validity,
3255 DBusForeachMessageFileFunc function,
3258 DBusString test_directory;
3259 DBusString filename;
3262 DBusResultCode result;
3267 if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
3268 _dbus_assert_not_reached ("didn't allocate test_directory\n");
3270 _dbus_string_init_const (&filename, subdir);
3272 if (!_dbus_string_copy (test_base_dir, 0,
3273 &test_directory, 0))
3274 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3276 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3277 _dbus_assert_not_reached ("couldn't allocate full path");
3279 _dbus_string_free (&filename);
3280 if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
3281 _dbus_assert_not_reached ("didn't allocate filename string\n");
3283 dir = _dbus_directory_open (&test_directory, &result);
3287 _dbus_string_get_const_data (&test_directory, &s);
3288 _dbus_warn ("Could not open %s: %s\n", s,
3289 dbus_result_to_string (result));
3293 printf ("Testing:\n");
3295 result = DBUS_RESULT_SUCCESS;
3297 while (_dbus_directory_get_next_file (dir, &filename, &result))
3299 DBusString full_path;
3302 if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
3303 _dbus_assert_not_reached ("couldn't init string");
3305 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3306 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3308 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3309 _dbus_assert_not_reached ("couldn't concat file to dir");
3311 if (_dbus_string_ends_with_c_str (&filename, ".message"))
3313 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
3317 const char *filename_c;
3318 _dbus_string_get_const_data (&filename, &filename_c);
3319 _dbus_verbose ("Skipping non-.message file %s\n",
3321 _dbus_string_free (&full_path);
3327 _dbus_string_get_const_data (&filename, &s);
3328 printf (" %s\n", s);
3331 _dbus_verbose (" expecting %s\n",
3332 validity == _DBUS_MESSAGE_VALID ? "valid" :
3333 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
3334 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
3336 if (! (*function) (&full_path, is_raw, validity, user_data))
3338 _dbus_string_free (&full_path);
3342 _dbus_string_free (&full_path);
3345 if (result != DBUS_RESULT_SUCCESS)
3348 _dbus_string_get_const_data (&test_directory, &s);
3349 _dbus_warn ("Could not get next file in %s: %s\n",
3350 s, dbus_result_to_string (result));
3359 _dbus_directory_close (dir);
3360 _dbus_string_free (&test_directory);
3361 _dbus_string_free (&filename);
3367 * Runs the given function on every message file in the test suite.
3368 * The function should return #FALSE on test failure or fatal error.
3370 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
3371 * @param func the function to run
3372 * @param user_data data for function
3373 * @returns #FALSE if there's a failure
3376 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
3377 DBusForeachMessageFileFunc func,
3380 DBusString test_directory;
3385 _dbus_string_init_const (&test_directory, test_data_dir);
3387 if (!process_test_subdir (&test_directory, "valid-messages",
3388 _DBUS_MESSAGE_VALID, func, user_data))
3391 if (!process_test_subdir (&test_directory, "invalid-messages",
3392 _DBUS_MESSAGE_INVALID, func, user_data))
3395 if (!process_test_subdir (&test_directory, "incomplete-messages",
3396 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
3403 _dbus_string_free (&test_directory);
3409 * @ingroup DBusMessageInternals
3410 * Unit test for DBusMessage.
3412 * @returns #TRUE on success.
3415 _dbus_message_test (const char *test_data_dir)
3417 DBusMessage *message;
3418 DBusMessageLoader *loader;
3421 dbus_int32_t our_int;
3424 dbus_bool_t our_bool;
3426 /* Test the vararg functions */
3427 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3428 _dbus_message_set_client_serial (message, 1);
3429 dbus_message_append_args (message,
3430 DBUS_TYPE_INT32, -0x12345678,
3431 DBUS_TYPE_STRING, "Test string",
3432 DBUS_TYPE_DOUBLE, 3.14159,
3433 DBUS_TYPE_BOOLEAN, TRUE,
3435 _dbus_verbose_bytes_of_string (&message->header, 0,
3436 _dbus_string_get_length (&message->header));
3437 _dbus_verbose_bytes_of_string (&message->body, 0,
3438 _dbus_string_get_length (&message->body));
3440 if (dbus_message_get_args (message,
3441 DBUS_TYPE_INT32, &our_int,
3442 DBUS_TYPE_STRING, &our_str,
3443 DBUS_TYPE_DOUBLE, &our_double,
3444 DBUS_TYPE_BOOLEAN, &our_bool,
3445 0) != DBUS_RESULT_SUCCESS)
3446 _dbus_assert_not_reached ("Could not get arguments");
3448 if (our_int != -0x12345678)
3449 _dbus_assert_not_reached ("integers differ!");
3451 if (our_double != 3.14159)
3452 _dbus_assert_not_reached ("doubles differ!");
3454 if (strcmp (our_str, "Test string") != 0)
3455 _dbus_assert_not_reached ("strings differ!");
3458 _dbus_assert_not_reached ("booleans differ");
3460 dbus_free (our_str);
3461 dbus_message_unref (message);
3463 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3464 _dbus_message_set_client_serial (message, 1);
3465 _dbus_message_set_reply_serial (message, 0x12345678);
3467 dbus_message_append_string (message, "Test string");
3468 dbus_message_append_int32 (message, -0x12345678);
3469 dbus_message_append_uint32 (message, 0xedd1e);
3470 dbus_message_append_double (message, 3.14159);
3472 message_iter_test (message);
3474 /* Message loader test */
3475 _dbus_message_lock (message);
3476 loader = _dbus_message_loader_new ();
3478 /* Write the header data one byte at a time */
3479 _dbus_string_get_const_data (&message->header, &data);
3480 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
3484 _dbus_message_loader_get_buffer (loader, &buffer);
3485 _dbus_string_append_byte (buffer, data[i]);
3486 _dbus_message_loader_return_buffer (loader, buffer, 1);
3489 /* Write the body data one byte at a time */
3490 _dbus_string_get_const_data (&message->body, &data);
3491 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
3495 _dbus_message_loader_get_buffer (loader, &buffer);
3496 _dbus_string_append_byte (buffer, data[i]);
3497 _dbus_message_loader_return_buffer (loader, buffer, 1);
3500 dbus_message_unref (message);
3502 /* Now pop back the message */
3503 if (_dbus_message_loader_get_is_corrupted (loader))
3504 _dbus_assert_not_reached ("message loader corrupted");
3506 message = _dbus_message_loader_pop_message (loader);
3508 _dbus_assert_not_reached ("received a NULL message");
3510 if (_dbus_message_get_reply_serial (message) != 0x12345678)
3511 _dbus_assert_not_reached ("reply serial fields differ");
3513 message_iter_test (message);
3515 dbus_message_unref (message);
3516 _dbus_message_loader_unref (loader);
3518 /* Now load every message in test_data_dir if we have one */
3519 if (test_data_dir == NULL)
3522 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
3523 (DBusForeachMessageFileFunc)
3524 dbus_internal_do_not_use_try_message_file,
3528 #endif /* DBUS_BUILD_TESTS */