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,
190 offset = message->header_fields[field].offset;
192 _dbus_assert (field < FIELD_LAST);
197 /* offset points to string length, string data follows it */
198 /* FIXME _dbus_demarshal_const_string() that returned
199 * a reference to the string plus its len might be nice.
203 *len = _dbus_demarshal_uint32 (&message->header,
208 _dbus_string_get_const_data (&message->header,
211 return data + (offset + 4);
215 get_int_field (DBusMessage *message,
220 _dbus_assert (field < FIELD_LAST);
222 offset = message->header_fields[field].offset;
225 return -1; /* useless if -1 is a valid value of course */
227 return _dbus_demarshal_int32 (&message->header,
234 append_int_field (DBusMessage *message,
241 _dbus_assert (!message->locked);
243 clear_header_padding (message);
245 orig_len = _dbus_string_get_length (&message->header);
247 if (!_dbus_string_align_length (&message->header, 4))
250 if (!_dbus_string_append_len (&message->header, name, 4))
253 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_INT32))
256 if (!_dbus_string_align_length (&message->header, 4))
259 message->header_fields[FIELD_REPLY_SERIAL].offset =
260 _dbus_string_get_length (&message->header);
262 if (!_dbus_marshal_int32 (&message->header, message->byte_order,
266 if (!append_header_padding (message))
272 message->header_fields[field].offset = -1;
273 _dbus_string_set_length (&message->header, orig_len);
275 /* this must succeed because it was allocated on function entry and
276 * DBusString doesn't ever realloc smaller
278 if (!append_header_padding (message))
279 _dbus_assert_not_reached ("failed to reappend header padding");
284 append_string_field (DBusMessage *message,
291 _dbus_assert (!message->locked);
293 clear_header_padding (message);
295 orig_len = _dbus_string_get_length (&message->header);
297 if (!_dbus_string_align_length (&message->header, 4))
300 if (!_dbus_string_append_len (&message->header, name, 4))
303 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_STRING))
306 if (!_dbus_string_align_length (&message->header, 4))
309 message->header_fields[field].offset =
310 _dbus_string_get_length (&message->header);
312 if (!_dbus_marshal_string (&message->header, message->byte_order,
316 if (!append_header_padding (message))
322 message->header_fields[field].offset = -1;
323 _dbus_string_set_length (&message->header, orig_len);
325 /* this must succeed because it was allocated on function entry and
326 * DBusString doesn't ever realloc smaller
328 if (!append_header_padding (message))
329 _dbus_assert_not_reached ("failed to reappend header padding");
335 delete_int_field (DBusMessage *message,
338 int offset = message->header_fields[field].offset;
340 _dbus_assert (!message->locked);
341 _dbus_assert (field_is_named[field]);
346 clear_header_padding (message);
348 /* The field typecode and name take up 8 bytes */
349 _dbus_string_delete (&message->header,
353 message->header_fields[field].offset = -1;
355 adjust_field_offsets (message,
359 append_header_padding (message);
363 delete_string_field (DBusMessage *message,
366 int offset = message->header_fields[field].offset;
370 _dbus_assert (!message->locked);
371 _dbus_assert (field_is_named[field]);
376 clear_header_padding (message);
378 get_string_field (message, field, &len);
380 /* The field typecode and name take up 8 bytes, and the nul
381 * termination is 1 bytes, string length integer is 4 bytes
383 delete_len = 8 + 4 + 1 + len;
385 _dbus_string_delete (&message->header,
389 message->header_fields[field].offset = -1;
391 adjust_field_offsets (message,
395 append_header_padding (message);
399 set_int_field (DBusMessage *message,
403 int offset = message->header_fields[field].offset;
405 _dbus_assert (!message->locked);
409 /* need to append the field */
413 case FIELD_REPLY_SERIAL:
414 return append_int_field (message, field,
415 DBUS_HEADER_FIELD_REPLY,
418 _dbus_assert_not_reached ("appending an int field we don't support appending");
424 _dbus_marshal_set_int32 (&message->header,
433 set_uint_field (DBusMessage *message,
437 int offset = message->header_fields[field].offset;
439 _dbus_assert (!message->locked);
443 /* need to append the field */
448 _dbus_assert_not_reached ("appending a uint field we don't support appending");
454 _dbus_marshal_set_uint32 (&message->header,
463 set_string_field (DBusMessage *message,
467 int offset = message->header_fields[field].offset;
469 _dbus_assert (!message->locked);
470 _dbus_assert (value != NULL);
474 /* need to append the field */
479 return append_string_field (message, field,
480 DBUS_HEADER_FIELD_SENDER,
483 _dbus_assert_not_reached ("appending a string field we don't support appending");
493 old_len = _dbus_string_get_length (&message->header);
495 _dbus_string_init_const_len (&v, value,
496 strlen (value) + 1); /* include nul */
497 if (!_dbus_marshal_set_string (&message->header,
502 new_len = _dbus_string_get_length (&message->header);
504 adjust_field_offsets (message,
513 * Sets the client serial of a message.
514 * This can only be done once on a message.
516 * @todo client_serial should be called simply
517 * "serial"; it's in outgoing messages for both
518 * the client and the server, it's only client-specific
519 * in the message bus case. It's more like origin_serial
522 * @param message the message
523 * @param client_serial the client serial
526 _dbus_message_set_client_serial (DBusMessage *message,
527 dbus_int32_t client_serial)
529 _dbus_assert (!message->locked);
530 _dbus_assert (_dbus_message_get_client_serial (message) < 0);
532 set_int_field (message, FIELD_CLIENT_SERIAL,
537 * Sets the reply serial of a message (the client serial
538 * of the message this is a reply to).
540 * @param message the message
541 * @param reply_serial the client serial
542 * @returns #FALSE if not enough memory
545 _dbus_message_set_reply_serial (DBusMessage *message,
546 dbus_int32_t reply_serial)
548 _dbus_assert (!message->locked);
550 return set_int_field (message, FIELD_REPLY_SERIAL,
555 * Returns the client serial of a message or
556 * -1 if none has been specified.
558 * @todo see note in _dbus_message_set_client_serial()
559 * about how client_serial is a misnomer
561 * @todo this function should be public, after renaming it.
563 * @param message the message
564 * @returns the client serial
567 _dbus_message_get_client_serial (DBusMessage *message)
569 return get_int_field (message, FIELD_CLIENT_SERIAL);
573 * Returns the serial that the message is
574 * a reply to or -1 if none.
576 * @param message the message
577 * @returns the reply serial
580 _dbus_message_get_reply_serial (DBusMessage *message)
582 return get_int_field (message, FIELD_REPLY_SERIAL);
586 * Adds a counter to be incremented immediately with the
587 * size of this message, and decremented by the size
588 * of this message when this message if finalized.
590 * @param message the message
591 * @param counter the counter
594 _dbus_message_add_size_counter (DBusMessage *message,
595 DBusCounter *counter)
597 _dbus_assert (message->size_counter == NULL); /* If this fails we may need to keep a list of
598 * counters instead of just one
601 message->size_counter = counter;
602 _dbus_counter_ref (message->size_counter);
604 /* When we can change message size, we may want to
605 * update this each time we do so, or we may want to
606 * just KISS like this.
608 message->size_counter_delta =
609 _dbus_string_get_length (&message->header) +
610 _dbus_string_get_length (&message->body);
612 _dbus_verbose ("message has size %ld\n",
613 message->size_counter_delta);
615 _dbus_counter_adjust (message->size_counter, message->size_counter_delta);
619 dbus_message_create_header (DBusMessage *message,
625 if (!_dbus_string_append_byte (&message->header, message->byte_order))
629 if (!_dbus_string_append_byte (&message->header, flags))
632 if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
635 if (!_dbus_string_append_byte (&message->header, 0))
638 message->header_fields[FIELD_HEADER_LENGTH].offset = 4;
639 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
642 message->header_fields[FIELD_BODY_LENGTH].offset = 8;
643 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
646 message->header_fields[FIELD_CLIENT_SERIAL].offset = 12;
647 if (!_dbus_marshal_int32 (&message->header, message->byte_order, -1))
650 /* Marshal message service */
653 if (!append_string_field (message,
655 DBUS_HEADER_FIELD_SERVICE,
660 _dbus_assert (name != NULL);
661 if (!append_string_field (message,
663 DBUS_HEADER_FIELD_NAME,
671 * Locks a message. Allows checking that applications don't keep a
672 * reference to a message in the outgoing queue and change it
673 * underneath us. Messages are locked when they enter the outgoing
674 * queue (dbus_connection_send_message()), and the library complains
675 * if the message is modified while locked.
677 * @param message the message to lock.
680 _dbus_message_lock (DBusMessage *message)
682 if (!message->locked)
684 /* Fill in our lengths */
685 set_uint_field (message,
687 _dbus_string_get_length (&message->header));
689 set_uint_field (message,
691 _dbus_string_get_length (&message->body));
693 message->locked = TRUE;
700 * @defgroup DBusMessage DBusMessage
702 * @brief Message to be sent or received over a DBusConnection.
704 * A DBusMessage is the most basic unit of communication over a
705 * DBusConnection. A DBusConnection represents a stream of messages
706 * received from a remote application, and a stream of messages
707 * sent to a remote application.
713 * @typedef DBusMessage
715 * Opaque data type representing a message received from or to be
716 * sent to another application.
720 dbus_message_new_empty_header (void)
722 DBusMessage *message;
725 message = dbus_new0 (DBusMessage, 1);
729 message->refcount = 1;
730 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
733 while (i < FIELD_LAST)
735 message->header_fields[i].offset = -1;
739 if (!_dbus_string_init (&message->header, _DBUS_INT_MAX))
745 if (!_dbus_string_init (&message->body, _DBUS_INT_MAX))
747 _dbus_string_free (&message->header);
757 * Constructs a new message. Returns #NULL if memory can't be
758 * allocated for the message. The service may be #NULL in which case
759 * no service is set; this is appropriate when using D-BUS in a
760 * peer-to-peer context (no message bus).
762 * @todo reverse the arguments, first 'name' then 'service'
763 * as 'name' is more fundamental
765 * @param service service that the message should be sent to or #NULL
766 * @param name name of the message
767 * @returns a new DBusMessage, free with dbus_message_unref()
768 * @see dbus_message_unref()
771 dbus_message_new (const char *service,
774 DBusMessage *message;
776 message = dbus_message_new_empty_header ();
780 if (!dbus_message_create_header (message, service, name))
782 dbus_message_unref (message);
790 * Constructs a message that is a reply to some other
791 * message. Returns #NULL if memory can't be allocated
794 * @param original_message the message which the created
795 * message is a reply to.
796 * @returns a new DBusMessage, free with dbus_message_unref()
797 * @see dbus_message_new(), dbus_message_unref()
800 dbus_message_new_reply (DBusMessage *original_message)
802 DBusMessage *message;
803 const char *sender, *name;
805 sender = get_string_field (original_message,
807 name = get_string_field (original_message,
810 /* sender is allowed to be null here in peer-to-peer case */
812 message = dbus_message_new (sender, name);
817 if (!_dbus_message_set_reply_serial (message,
818 _dbus_message_get_client_serial (original_message)))
820 dbus_message_unref (message);
828 * Creates a new message that is an error reply to a certain message.
830 * @param original_message the original message
831 * @param error_name the error name
832 * @param error_message the error message string
833 * @returns a new error message
836 dbus_message_new_error_reply (DBusMessage *original_message,
837 const char *error_name,
838 const char *error_message)
840 DBusMessage *message;
843 sender = get_string_field (original_message,
846 _dbus_assert (sender != NULL);
848 message = dbus_message_new (sender, error_name);
853 if (!_dbus_message_set_reply_serial (message,
854 _dbus_message_get_client_serial (original_message)))
856 dbus_message_unref (message);
860 if (!dbus_message_append_string (message, error_message))
862 dbus_message_unref (message);
866 dbus_message_set_is_error (message, TRUE);
872 * Creates a new message that is an exact replica of the message
873 * specified, except that its refcount is set to 1.
875 * @param message the message.
876 * @returns the new message.
879 dbus_message_new_from_message (const DBusMessage *message)
884 retval = dbus_new0 (DBusMessage, 1);
888 retval->refcount = 1;
889 retval->byte_order = message->byte_order;
891 if (!_dbus_string_init (&retval->header, _DBUS_INT_MAX))
897 if (!_dbus_string_init (&retval->body, _DBUS_INT_MAX))
899 _dbus_string_free (&retval->header);
904 if (!_dbus_string_copy (&message->header, 0,
907 _dbus_string_free (&retval->header);
908 _dbus_string_free (&retval->body);
914 if (!_dbus_string_copy (&message->body, 0,
917 _dbus_string_free (&retval->header);
918 _dbus_string_free (&retval->body);
924 for (i = 0; i < FIELD_LAST; i++)
926 retval->header_fields[i].offset = message->header_fields[i].offset;
934 * Increments the reference count of a DBusMessage.
936 * @param message The message
937 * @see dbus_message_unref
940 dbus_message_ref (DBusMessage *message)
942 dbus_atomic_t refcount;
944 refcount = _dbus_atomic_inc (&message->refcount);
945 _dbus_assert (refcount > 1);
949 * Decrements the reference count of a DBusMessage.
951 * @param message The message
952 * @see dbus_message_ref
955 dbus_message_unref (DBusMessage *message)
957 dbus_atomic_t refcount;
959 refcount = _dbus_atomic_dec (&message->refcount);
961 _dbus_assert (refcount >= 0);
965 if (message->size_counter != NULL)
967 _dbus_counter_adjust (message->size_counter,
968 - message->size_counter_delta);
969 _dbus_counter_unref (message->size_counter);
972 _dbus_string_free (&message->header);
973 _dbus_string_free (&message->body);
980 * Gets the name of a message.
982 * @param message the message
983 * @returns the message name (should not be freed)
986 dbus_message_get_name (DBusMessage *message)
988 return get_string_field (message, FIELD_NAME, NULL);
992 * Gets the destination service of a message.
994 * @param message the message
995 * @returns the message destination service (should not be freed)
998 dbus_message_get_service (DBusMessage *message)
1000 return get_string_field (message, FIELD_SERVICE, NULL);
1004 * Appends fields to a message given a variable argument
1005 * list. The variable argument list should contain the type
1006 * of the argument followed by the value to add. Array values
1007 * are specified by a pointer to the array followed by an int
1008 * giving the length of the array. The list is terminated
1011 * @param message the message
1012 * @param first_arg_type type of the first argument
1013 * @param ... value of first argument, list of additional type-value pairs
1014 * @returns #TRUE on success
1017 dbus_message_append_args (DBusMessage *message,
1024 va_start (var_args, first_arg_type);
1025 retval = dbus_message_append_args_valist (message,
1034 * This function takes a va_list for use by language bindings
1036 * @see dbus_message_append_args.
1037 * @param message the message
1038 * @param first_arg_type type of first argument
1039 * @param var_args value of first argument, then list of type/value pairs
1040 * @returns #TRUE on success
1043 dbus_message_append_args_valist (DBusMessage *message,
1049 old_len = _dbus_string_get_length (&message->body);
1051 type = first_arg_type;
1058 if (!dbus_message_append_nil (message))
1060 case DBUS_TYPE_BOOLEAN:
1061 if (!dbus_message_append_boolean (message, va_arg (var_args, dbus_bool_t)))
1063 case DBUS_TYPE_INT32:
1064 if (!dbus_message_append_int32 (message, va_arg (var_args, dbus_int32_t)))
1067 case DBUS_TYPE_UINT32:
1068 if (!dbus_message_append_uint32 (message, va_arg (var_args, dbus_uint32_t)))
1071 case DBUS_TYPE_DOUBLE:
1072 if (!dbus_message_append_double (message, va_arg (var_args, double)))
1075 case DBUS_TYPE_STRING:
1076 if (!dbus_message_append_string (message, va_arg (var_args, const char *)))
1079 case DBUS_TYPE_BOOLEAN_ARRAY:
1082 unsigned char *data;
1084 data = va_arg (var_args, unsigned char *);
1085 len = va_arg (var_args, int);
1087 if (!dbus_message_append_boolean_array (message, data, len))
1091 case DBUS_TYPE_INT32_ARRAY:
1096 data = va_arg (var_args, dbus_int32_t *);
1097 len = va_arg (var_args, int);
1099 if (!dbus_message_append_int32_array (message, data, len))
1103 case DBUS_TYPE_UINT32_ARRAY:
1106 dbus_uint32_t *data;
1108 data = va_arg (var_args, dbus_uint32_t *);
1109 len = va_arg (var_args, int);
1111 if (!dbus_message_append_uint32_array (message, data, len))
1115 case DBUS_TYPE_DOUBLE_ARRAY:
1120 data = va_arg (var_args, double *);
1121 len = va_arg (var_args, int);
1123 if (!dbus_message_append_double_array (message, data, len))
1127 case DBUS_TYPE_BYTE_ARRAY:
1130 unsigned char *data;
1132 data = va_arg (var_args, unsigned char *);
1133 len = va_arg (var_args, int);
1135 if (!dbus_message_append_byte_array (message, data, len))
1139 case DBUS_TYPE_STRING_ARRAY:
1144 data = va_arg (var_args, const char **);
1145 len = va_arg (var_args, int);
1147 if (!dbus_message_append_string_array (message, data, len))
1153 _dbus_warn ("Unknown field type %d\n", type);
1156 type = va_arg (var_args, int);
1166 * Appends a nil value to the message
1168 * @param message the message
1169 * @returns #TRUE on success
1172 dbus_message_append_nil (DBusMessage *message)
1174 _dbus_assert (!message->locked);
1176 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_NIL))
1183 * Appends a boolean value to the message
1185 * @param message the message
1186 * @param value the boolean value
1187 * @returns #TRUE on success
1190 dbus_message_append_boolean (DBusMessage *message,
1193 _dbus_assert (!message->locked);
1195 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN))
1198 if (!_dbus_string_append_byte (&message->body, (value != FALSE)))
1200 _dbus_string_shorten (&message->body, 1);
1208 * Appends a 32 bit signed integer to the message.
1210 * @param message the message
1211 * @param value the integer value
1212 * @returns #TRUE on success
1215 dbus_message_append_int32 (DBusMessage *message,
1218 _dbus_assert (!message->locked);
1220 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32))
1223 if (!_dbus_marshal_int32 (&message->body, message->byte_order, value))
1225 _dbus_string_shorten (&message->body, 1);
1233 * Appends a 32 bit unsigned integer to the message.
1235 * @param message the message
1236 * @param value the integer value
1237 * @returns #TRUE on success
1240 dbus_message_append_uint32 (DBusMessage *message,
1241 dbus_uint32_t value)
1243 _dbus_assert (!message->locked);
1245 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32))
1248 if (!_dbus_marshal_uint32 (&message->body, message->byte_order, value))
1250 _dbus_string_shorten (&message->body, 1);
1258 * Appends a double value to the message.
1260 * @param message the message
1261 * @param value the double value
1262 * @returns #TRUE on success
1265 dbus_message_append_double (DBusMessage *message,
1268 _dbus_assert (!message->locked);
1270 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE))
1273 if (!_dbus_marshal_double (&message->body, message->byte_order, value))
1275 _dbus_string_shorten (&message->body, 1);
1283 * Appends a UTF-8 string to the message.
1285 * @param message the message
1286 * @param value the string
1287 * @returns #TRUE on success
1290 dbus_message_append_string (DBusMessage *message,
1293 _dbus_assert (!message->locked);
1295 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING))
1298 if (!_dbus_marshal_string (&message->body, message->byte_order, value))
1300 _dbus_string_shorten (&message->body, 1);
1308 * Appends a boolean array to the message.
1310 * @param message the message
1311 * @param value the array
1312 * @param len the length of the array
1313 * @returns #TRUE on success
1316 dbus_message_append_boolean_array (DBusMessage *message,
1317 unsigned const char *value,
1320 _dbus_assert (!message->locked);
1322 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN_ARRAY))
1325 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1327 _dbus_string_shorten (&message->body, 1);
1335 * Appends a 32 bit signed integer array to the message.
1337 * @param message the message
1338 * @param value the array
1339 * @param len the length of the array
1340 * @returns #TRUE on success
1343 dbus_message_append_int32_array (DBusMessage *message,
1344 const dbus_int32_t *value,
1347 _dbus_assert (!message->locked);
1349 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32_ARRAY))
1352 if (!_dbus_marshal_int32_array (&message->body, message->byte_order,
1355 _dbus_string_shorten (&message->body, 1);
1363 * Appends a 32 bit unsigned integer array to the message.
1365 * @param message the message
1366 * @param value the array
1367 * @param len the length of the array
1368 * @returns #TRUE on success
1371 dbus_message_append_uint32_array (DBusMessage *message,
1372 const dbus_uint32_t *value,
1375 _dbus_assert (!message->locked);
1377 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32_ARRAY))
1380 if (!_dbus_marshal_uint32_array (&message->body, message->byte_order,
1383 _dbus_string_shorten (&message->body, 1);
1391 * Appends a double array to the message.
1393 * @param message the message
1394 * @param value the array
1395 * @param len the length of the array
1396 * @returns #TRUE on success
1399 dbus_message_append_double_array (DBusMessage *message,
1400 const double *value,
1403 _dbus_assert (!message->locked);
1405 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE_ARRAY))
1408 if (!_dbus_marshal_double_array (&message->body, message->byte_order,
1411 _dbus_string_shorten (&message->body, 1);
1419 * Appends a byte array to the message.
1421 * @param message the message
1422 * @param value the array
1423 * @param len the length of the array
1424 * @returns #TRUE on success
1427 dbus_message_append_byte_array (DBusMessage *message,
1428 unsigned const char *value,
1431 _dbus_assert (!message->locked);
1433 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY))
1436 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1438 _dbus_string_shorten (&message->body, 1);
1446 * Appends a string array to the message.
1448 * @param message the message
1449 * @param value the array
1450 * @param len the length of the array
1451 * @returns #TRUE on success
1454 dbus_message_append_string_array (DBusMessage *message,
1458 _dbus_assert (!message->locked);
1460 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING_ARRAY))
1463 if (!_dbus_marshal_string_array (&message->body, message->byte_order,
1466 _dbus_string_shorten (&message->body, 1);
1474 * Gets arguments from a message given a variable argument list.
1475 * The variable argument list should contain the type of the
1476 * argumen followed by a pointer to where the value should be
1477 * stored. The list is terminated with 0.
1479 * @param message the message
1480 * @param first_arg_type the first argument type
1481 * @param ... location for first argument value, then list of type-location pairs
1482 * @returns result code
1485 dbus_message_get_args (DBusMessage *message,
1489 DBusResultCode retval;
1492 va_start (var_args, first_arg_type);
1493 retval = dbus_message_get_args_valist (message, first_arg_type, var_args);
1500 * This function takes a va_list for use by language bindings
1502 * @todo this function (or some lower-level non-convenience function)
1503 * needs better error handling; should allow the application to
1504 * distinguish between out of memory, and bad data from the remote
1505 * app. It also needs to not leak a bunch of args when it gets
1506 * to the arg that's bad, as that would be a security hole
1507 * (allow one app to force another to leak memory)
1509 * @todo We need to free the argument data when an error occurs.
1511 * @see dbus_message_get_args
1512 * @param message the message
1513 * @param first_arg_type type of the first argument
1514 * @param var_args return location for first argument, followed by list of type/location pairs
1515 * @returns result code
1518 dbus_message_get_args_valist (DBusMessage *message,
1522 int spec_type, msg_type, i;
1523 DBusMessageIter *iter;
1525 iter = dbus_message_get_args_iter (message);
1528 return DBUS_RESULT_NO_MEMORY;
1530 spec_type = first_arg_type;
1533 while (spec_type != 0)
1535 msg_type = dbus_message_iter_get_arg_type (iter);
1537 if (msg_type != spec_type)
1539 _dbus_verbose ("Argument %d is specified to be of type \"%s\", but "
1540 "is actually of type \"%s\"\n", i,
1541 _dbus_type_to_string (spec_type),
1542 _dbus_type_to_string (msg_type));
1543 dbus_message_iter_unref (iter);
1545 return DBUS_RESULT_INVALID_ARGS;
1552 case DBUS_TYPE_BOOLEAN:
1556 ptr = va_arg (var_args, dbus_bool_t *);
1558 *ptr = dbus_message_iter_get_boolean (iter);
1561 case DBUS_TYPE_INT32:
1565 ptr = va_arg (var_args, dbus_int32_t *);
1567 *ptr = dbus_message_iter_get_int32 (iter);
1570 case DBUS_TYPE_UINT32:
1574 ptr = va_arg (var_args, dbus_uint32_t *);
1576 *ptr = dbus_message_iter_get_uint32 (iter);
1580 case DBUS_TYPE_DOUBLE:
1584 ptr = va_arg (var_args, double *);
1586 *ptr = dbus_message_iter_get_double (iter);
1590 case DBUS_TYPE_STRING:
1594 ptr = va_arg (var_args, char **);
1596 *ptr = dbus_message_iter_get_string (iter);
1599 return DBUS_RESULT_NO_MEMORY;
1604 case DBUS_TYPE_BOOLEAN_ARRAY:
1606 unsigned char **ptr;
1609 ptr = va_arg (var_args, unsigned char **);
1610 len = va_arg (var_args, int *);
1612 if (!dbus_message_iter_get_boolean_array (iter, ptr, len))
1613 return DBUS_RESULT_NO_MEMORY;
1618 case DBUS_TYPE_INT32_ARRAY:
1623 ptr = va_arg (var_args, dbus_int32_t **);
1624 len = va_arg (var_args, int *);
1626 if (!dbus_message_iter_get_int32_array (iter, ptr, len))
1627 return DBUS_RESULT_NO_MEMORY;
1632 case DBUS_TYPE_UINT32_ARRAY:
1634 dbus_uint32_t **ptr;
1637 ptr = va_arg (var_args, dbus_uint32_t **);
1638 len = va_arg (var_args, int *);
1640 if (!dbus_message_iter_get_uint32_array (iter, ptr, len))
1641 return DBUS_RESULT_NO_MEMORY;
1646 case DBUS_TYPE_DOUBLE_ARRAY:
1651 ptr = va_arg (var_args, double **);
1652 len = va_arg (var_args, int *);
1654 if (!dbus_message_iter_get_double_array (iter, ptr, len))
1655 return DBUS_RESULT_NO_MEMORY;
1660 case DBUS_TYPE_BYTE_ARRAY:
1662 unsigned char **ptr;
1665 ptr = va_arg (var_args, unsigned char **);
1666 len = va_arg (var_args, int *);
1668 if (!dbus_message_iter_get_byte_array (iter, ptr, len))
1669 return DBUS_RESULT_NO_MEMORY;
1673 case DBUS_TYPE_STRING_ARRAY:
1678 ptr = va_arg (var_args, char ***);
1679 len = va_arg (var_args, int *);
1681 if (!dbus_message_iter_get_string_array (iter, ptr, len))
1682 return DBUS_RESULT_NO_MEMORY;
1686 _dbus_warn ("Unknown field type %d\n", spec_type);
1689 spec_type = va_arg (var_args, int);
1690 if (spec_type != 0 && !dbus_message_iter_next (iter))
1692 _dbus_verbose ("More fields than exist in the message were specified or field is corrupt\n");
1694 dbus_message_iter_unref (iter);
1695 return DBUS_RESULT_INVALID_ARGS;
1700 dbus_message_iter_unref (iter);
1701 return DBUS_RESULT_SUCCESS;
1705 * Returns a DBusMessageIter representing the arguments of the
1706 * message passed in.
1708 * @todo IMO the message iter should follow the GtkTextIter pattern,
1709 * a static object with a "stamp" value used to detect invalid
1710 * iter uses (uninitialized or after changing the message).
1711 * ref/unref is kind of annoying to deal with, and slower too.
1712 * This implies not ref'ing the message from the iter.
1714 * @todo I'd also name this dbus_message_iter_new() or
1715 * for the static object dbus_message_iter_init() rather
1716 * than making it a method on the message
1718 * @param message the message
1719 * @returns a new iter.
1722 dbus_message_get_args_iter (DBusMessage *message)
1724 DBusMessageIter *iter;
1726 iter = dbus_new (DBusMessageIter, 1);
1730 dbus_message_ref (message);
1733 iter->message = message;
1741 * Increments the reference count of a DBusMessageIter.
1743 * @param iter the message iter
1744 * @see dbus_message_iter_unref
1747 dbus_message_iter_ref (DBusMessageIter *iter)
1749 _dbus_assert (iter->refcount > 0);
1751 iter->refcount += 1;
1755 * Decrements the reference count of a DBusMessageIter.
1757 * @param iter The message iter
1758 * @see dbus_message_iter_ref
1761 dbus_message_iter_unref (DBusMessageIter *iter)
1763 _dbus_assert (iter->refcount > 0);
1765 iter->refcount -= 1;
1767 if (iter->refcount == 0)
1769 dbus_message_unref (iter->message);
1776 * Checks if an iterator has any more fields.
1778 * @param iter the message iter
1779 * @returns #TRUE if there are more fields
1783 dbus_message_iter_has_next (DBusMessageIter *iter)
1787 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1788 iter->message->byte_order,
1789 iter->pos, &end_pos))
1792 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1799 * Moves the iterator to the next field.
1801 * @param iter The message iter
1802 * @returns #TRUE if the iterator was moved to the next field
1805 dbus_message_iter_next (DBusMessageIter *iter)
1809 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1810 iter->message->byte_order,
1811 iter->pos, &end_pos))
1814 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1817 iter->pos = end_pos;
1823 * Returns the argument type of the argument that the
1824 * message iterator points at.
1826 * @param iter the message iter
1827 * @returns the field type
1830 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1834 if (iter->pos >= _dbus_string_get_length (&iter->message->body))
1835 return DBUS_TYPE_INVALID;
1837 _dbus_string_get_const_data_len (&iter->message->body, &data, iter->pos, 1);
1839 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_STRING_ARRAY)
1842 return DBUS_TYPE_INVALID;
1846 * Returns the string value that an iterator may point to.
1847 * Note that you need to check that the iterator points to
1848 * a string value before using this function.
1850 * @see dbus_message_iter_get_arg_type
1851 * @param iter the message iter
1852 * @returns the string
1855 dbus_message_iter_get_string (DBusMessageIter *iter)
1857 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING);
1859 return _dbus_demarshal_string (&iter->message->body, iter->message->byte_order,
1860 iter->pos + 1, NULL);
1864 * Returns the boolean value that an iterator may point to.
1865 * Note that you need to check that the iterator points to
1866 * a boolean value before using this function.
1868 * @see dbus_message_iter_get_arg_type
1869 * @param iter the message iter
1870 * @returns the string
1873 dbus_message_iter_get_boolean (DBusMessageIter *iter)
1875 unsigned char value;
1877 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN);
1879 value = _dbus_string_get_byte (&iter->message->body, iter->pos + 1);
1886 * Returns the 32 bit signed integer value that an iterator may point to.
1887 * Note that you need to check that the iterator points to
1888 * an integer value before using this function.
1890 * @see dbus_message_iter_get_arg_type
1891 * @param iter the message iter
1892 * @returns the integer
1895 dbus_message_iter_get_int32 (DBusMessageIter *iter)
1897 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32);
1899 return _dbus_demarshal_int32 (&iter->message->body, iter->message->byte_order,
1900 iter->pos + 1, NULL);
1904 * Returns the 32 bit unsigned integer value that an iterator may point to.
1905 * Note that you need to check that the iterator points to
1906 * an unsigned integer value before using this function.
1908 * @see dbus_message_iter_get_arg_type
1909 * @param iter the message iter
1910 * @returns the integer
1913 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
1915 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32);
1917 return _dbus_demarshal_uint32 (&iter->message->body, iter->message->byte_order,
1918 iter->pos + 1, NULL);
1922 * Returns the double value that an iterator may point to.
1923 * Note that you need to check that the iterator points to
1924 * a string value before using this function.
1926 * @see dbus_message_iter_get_arg_type
1927 * @param iter the message iter
1928 * @returns the double
1931 dbus_message_iter_get_double (DBusMessageIter *iter)
1933 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE);
1935 return _dbus_demarshal_double (&iter->message->body, iter->message->byte_order,
1936 iter->pos + 1, NULL);
1940 * Returns the boolean array that the iterator may point to. Note that
1941 * you need to check that the iterator points to an array of the
1942 * correct type prior to using this function.
1944 * @param iter the iterator
1945 * @param value return location for the array
1946 * @param len return location for the array length
1947 * @returns #TRUE on success
1950 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
1951 unsigned char **value,
1954 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN_ARRAY);
1956 *value = _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
1957 iter->pos + 1, NULL, len);
1966 * Returns the 32 bit signed integer array that the iterator may point
1967 * to. Note that you need to check that the iterator points to an
1968 * array of the correct type prior to using this function.
1970 * @param iter the iterator
1971 * @param value return location for the array
1972 * @param len return location for the array length
1973 * @returns #TRUE on success
1976 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
1977 dbus_int32_t **value,
1980 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32_ARRAY);
1982 *value = _dbus_demarshal_int32_array (&iter->message->body, iter->message->byte_order,
1983 iter->pos + 1, NULL, len);
1992 * Returns the 32 bit unsigned integer array that the iterator may point
1993 * to. Note that you need to check that the iterator points to an
1994 * array of the correct type prior to using this function.
1996 * @param iter the iterator
1997 * @param value return location for the array
1998 * @param len return location for the array length
1999 * @returns #TRUE on success
2002 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
2003 dbus_uint32_t **value,
2006 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32_ARRAY);
2008 *value = _dbus_demarshal_uint32_array (&iter->message->body, iter->message->byte_order,
2009 iter->pos + 1, NULL, len);
2018 * Returns the double array that the iterator may point to. Note that
2019 * you need to check that the iterator points to an array of the
2020 * correct type prior to using this function.
2022 * @param iter the iterator
2023 * @param value return location for the array
2024 * @param len return location for the array length
2025 * @returns #TRUE on success
2028 dbus_message_iter_get_double_array (DBusMessageIter *iter,
2032 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE_ARRAY);
2034 *value = _dbus_demarshal_double_array (&iter->message->body, iter->message->byte_order,
2035 iter->pos + 1, NULL, len);
2044 * Returns the byte array that the iterator may point to.
2045 * Note that you need to check that the iterator points
2046 * to a byte array prior to using this function.
2048 * @param iter the iterator
2049 * @param value return location for array values
2050 * @param len return location for length of byte array
2051 * @returns the byte array
2054 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
2055 unsigned char **value,
2058 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BYTE_ARRAY);
2060 *value = _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2061 iter->pos + 1, NULL, len);
2070 * Returns the string array that the iterator may point to.
2071 * Note that you need to check that the iterator points
2072 * to a byte array prior to using this function.
2074 * The returned value is a #NULL-terminated array of strings.
2075 * Each string is a separate malloc block, and the array
2076 * itself is a malloc block. You can free this type of
2077 * string array with dbus_free_string_array().
2079 * @param iter the iterator
2080 * @param value return location for string values
2081 * @param len return location for length of byte array
2082 * @returns the byte array
2085 dbus_message_iter_get_string_array (DBusMessageIter *iter,
2089 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING_ARRAY);
2091 *value = _dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
2092 iter->pos + 1, NULL, len);
2101 * Sets the message sender.
2103 * @param message the message
2104 * @param sender the sender
2105 * @returns #FALSE if not enough memory
2108 dbus_message_set_sender (DBusMessage *message,
2111 _dbus_assert (!message->locked);
2115 delete_string_field (message, FIELD_SENDER);
2120 return set_string_field (message,
2127 * Sets a flag indicating that the message is an error reply
2128 * message, i.e. an "exception" rather than a normal response.
2130 * @param message the message
2131 * @param is_error_reply #TRUE if this is an error message.
2134 dbus_message_set_is_error (DBusMessage *message,
2135 dbus_bool_t is_error_reply)
2139 _dbus_assert (!message->locked);
2141 _dbus_string_get_data_len (&message->header, &header, 1, 1);
2144 *header |= DBUS_HEADER_FLAG_ERROR;
2146 *header &= ~DBUS_HEADER_FLAG_ERROR;
2150 * Returns #TRUE if the message is an error
2151 * reply to some previous message we sent.
2153 * @param message the message
2154 * @returns #TRUE if the message is an error
2157 dbus_message_get_is_error (DBusMessage *message)
2161 _dbus_string_get_const_data_len (&message->header, &header, 1, 1);
2163 return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
2167 * Gets the service which originated this message,
2168 * or #NULL if unknown or inapplicable.
2170 * @param message the message
2171 * @returns the service name or #NULL
2174 dbus_message_get_sender (DBusMessage *message)
2176 return get_string_field (message, FIELD_SENDER, NULL);
2180 * Checks whether the message has the given name.
2181 * If the message has no name or has a different
2182 * name, returns #FALSE.
2184 * @param message the message
2185 * @param name the name to check (must not be #NULL)
2187 * @returns #TRUE if the message has the given name
2190 dbus_message_name_is (DBusMessage *message,
2195 _dbus_assert (name != NULL);
2197 n = dbus_message_get_name (message);
2199 if (n && strcmp (n, name) == 0)
2208 * @addtogroup DBusMessageInternals
2213 * @typedef DBusMessageLoader
2215 * The DBusMessageLoader object encapsulates the process of converting
2216 * a byte stream into a series of DBusMessage. It buffers the incoming
2217 * bytes as efficiently as possible, and generates a queue of
2218 * messages. DBusMessageLoader is typically used as part of a
2219 * DBusTransport implementation. The DBusTransport then hands off
2220 * the loaded messages to a DBusConnection, making the messages
2221 * visible to the application.
2225 /* we definitely use signed ints for sizes, so don't exceed
2226 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
2227 * over 128M is pretty nuts anyhow.
2231 * The maximum sane message size.
2233 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
2236 * Implementation details of DBusMessageLoader.
2237 * All members are private.
2239 struct DBusMessageLoader
2241 int refcount; /**< Reference count. */
2243 DBusString data; /**< Buffered data */
2245 DBusList *messages; /**< Complete messages. */
2247 long max_message_size; /**< Maximum size of a message */
2249 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
2251 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
2255 * The initial buffer size of the message loader.
2257 * @todo this should be based on min header size plus some average
2258 * body size, or something. Or rather, the min header size only, if we
2259 * want to try to read only the header, store that in a DBusMessage,
2260 * then read only the body and store that, etc., depends on
2261 * how we optimize _dbus_message_loader_get_buffer() and what
2262 * the exact message format is.
2264 #define INITIAL_LOADER_DATA_LEN 32
2267 * Creates a new message loader. Returns #NULL if memory can't
2270 * @returns new loader, or #NULL.
2273 _dbus_message_loader_new (void)
2275 DBusMessageLoader *loader;
2277 loader = dbus_new0 (DBusMessageLoader, 1);
2281 loader->refcount = 1;
2283 /* Try to cap message size at something that won't *totally* hose
2284 * the system if we have a couple of them.
2286 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
2288 if (!_dbus_string_init (&loader->data, _DBUS_INT_MAX))
2294 /* preallocate the buffer for speed, ignore failure */
2295 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
2296 _dbus_string_set_length (&loader->data, 0);
2302 * Increments the reference count of the loader.
2304 * @param loader the loader.
2307 _dbus_message_loader_ref (DBusMessageLoader *loader)
2309 loader->refcount += 1;
2313 * Decrements the reference count of the loader and finalizes the
2314 * loader when the count reaches zero.
2316 * @param loader the loader.
2319 _dbus_message_loader_unref (DBusMessageLoader *loader)
2321 loader->refcount -= 1;
2322 if (loader->refcount == 0)
2324 _dbus_list_foreach (&loader->messages,
2325 (DBusForeachFunction) dbus_message_unref,
2327 _dbus_list_clear (&loader->messages);
2328 _dbus_string_free (&loader->data);
2334 * Gets the buffer to use for reading data from the network. Network
2335 * data is read directly into an allocated buffer, which is then used
2336 * in the DBusMessage, to avoid as many extra memcpy's as possible.
2337 * The buffer must always be returned immediately using
2338 * _dbus_message_loader_return_buffer(), even if no bytes are
2339 * successfully read.
2341 * @todo this function can be a lot more clever. For example
2342 * it can probably always return a buffer size to read exactly
2343 * the body of the next message, thus avoiding any memory wastage
2346 * @param loader the message loader.
2347 * @param buffer the buffer
2350 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
2351 DBusString **buffer)
2353 _dbus_assert (!loader->buffer_outstanding);
2355 *buffer = &loader->data;
2357 loader->buffer_outstanding = TRUE;
2361 * The smallest header size that can occur.
2362 * (It won't be valid)
2364 #define DBUS_MINIMUM_HEADER_SIZE 16
2366 /** Pack four characters as in "abcd" into a uint32 */
2367 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
2368 ((((dbus_uint32_t)a) << 24) | \
2369 (((dbus_uint32_t)b) << 16) | \
2370 (((dbus_uint32_t)c) << 8) | \
2373 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
2374 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
2375 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
2377 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
2378 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
2379 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
2381 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
2382 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
2383 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
2385 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
2386 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
2387 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
2390 decode_header_data (const DBusString *data,
2393 HeaderField fields[FIELD_LAST],
2394 int *message_padding)
2400 if (header_len < 16)
2404 while (i < FIELD_LAST)
2406 fields[i].offset = -1;
2410 fields[FIELD_HEADER_LENGTH].offset = 4;
2411 fields[FIELD_BODY_LENGTH].offset = 8;
2412 fields[FIELD_CLIENT_SERIAL].offset = 12;
2414 /* Now handle the named fields. A real named field is at least 4
2415 * bytes for the name, plus a type code (1 byte) plus padding. So
2416 * if we have less than 8 bytes left, it must be alignment padding,
2417 * not a field. While >= 8 bytes can't be entirely alignment
2421 while ((pos + 7) < header_len)
2423 pos = _DBUS_ALIGN_VALUE (pos, 4);
2425 if ((pos + 4) > header_len)
2428 _dbus_string_get_const_data_len (data, &field, pos, 4);
2431 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
2433 /* I believe FROM_BE is right, but if not we'll find out
2436 switch (DBUS_UINT32_FROM_BE (*(int*)field))
2438 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
2439 if (fields[FIELD_SERVICE].offset >= 0)
2441 _dbus_verbose ("%s field provided twice\n",
2442 DBUS_HEADER_FIELD_SERVICE);
2446 fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2447 _dbus_verbose ("Found service name at offset %d\n",
2448 fields[FIELD_SERVICE].offset);
2451 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
2452 if (fields[FIELD_NAME].offset >= 0)
2454 _dbus_verbose ("%s field provided twice\n",
2455 DBUS_HEADER_FIELD_NAME);
2459 fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2461 _dbus_verbose ("Found message name at offset %d\n",
2462 fields[FIELD_NAME].offset);
2464 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
2465 if (fields[FIELD_SENDER].offset >= 0)
2467 _dbus_verbose ("%s field provided twice\n",
2468 DBUS_HEADER_FIELD_SENDER);
2472 fields[FIELD_SENDER].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2474 _dbus_verbose ("Found sender name at offset %d\n",
2475 fields[FIELD_NAME].offset);
2478 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
2479 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
2481 _dbus_verbose ("%s field provided twice\n",
2482 DBUS_HEADER_FIELD_REPLY);
2486 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2488 _dbus_verbose ("Found reply serial at offset %d\n",
2489 fields[FIELD_REPLY_SERIAL].offset);
2493 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c at offset %d\n",
2494 field[0], field[1], field[2], field[3], pos);
2497 if (!_dbus_marshal_validate_arg (data, byte_order, pos, &new_pos))
2499 _dbus_verbose ("Failed to validate argument to named header field\n");
2503 if (new_pos > header_len)
2505 _dbus_verbose ("Named header field tries to extend beyond header length\n");
2512 if (pos < header_len)
2514 /* Alignment padding, verify that it's nul */
2515 _dbus_assert ((header_len - pos) < 8);
2517 if (!_dbus_string_validate_nul (data,
2518 pos, (header_len - pos)))
2520 _dbus_verbose ("header alignment padding is not nul\n");
2524 if (message_padding)
2525 *message_padding = header_len - pos;
2532 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
2533 * indicating to the loader how many bytes of the buffer were filled
2534 * in. This function must always be called, even if no bytes were
2535 * successfully read.
2537 * @todo if we run out of memory in here, we offer no way for calling
2538 * code to handle it, i.e. they can't re-run the message parsing
2539 * attempt. Perhaps much of this code could be moved to pop_message()?
2540 * But then that may need to distinguish NULL return for no messages
2541 * from NULL return for errors.
2543 * @param loader the loader.
2544 * @param buffer the buffer.
2545 * @param bytes_read number of bytes that were read into the buffer.
2548 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
2552 _dbus_assert (loader->buffer_outstanding);
2553 _dbus_assert (buffer == &loader->data);
2555 loader->buffer_outstanding = FALSE;
2557 if (loader->corrupted)
2560 while (_dbus_string_get_length (&loader->data) >= 16)
2562 DBusMessage *message;
2563 const char *header_data;
2564 int byte_order, header_len, body_len, header_padding;
2565 dbus_uint32_t header_len_unsigned, body_len_unsigned;
2567 _dbus_string_get_const_data_len (&loader->data, &header_data, 0, 16);
2569 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
2571 if (header_data[2] != DBUS_MAJOR_PROTOCOL_VERSION)
2573 _dbus_verbose ("Message has protocol version %d ours is %d\n",
2574 (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
2575 loader->corrupted = TRUE;
2579 byte_order = header_data[0];
2581 if (byte_order != DBUS_LITTLE_ENDIAN &&
2582 byte_order != DBUS_BIG_ENDIAN)
2584 _dbus_verbose ("Message with bad byte order '%c' received\n",
2586 loader->corrupted = TRUE;
2590 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
2591 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
2593 if (header_len_unsigned < 16)
2595 _dbus_verbose ("Message had broken too-small header length %u\n",
2596 header_len_unsigned);
2597 loader->corrupted = TRUE;
2601 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
2602 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
2604 _dbus_verbose ("Header or body length too large (%u %u)\n",
2605 header_len_unsigned,
2607 loader->corrupted = TRUE;
2611 /* Now that we know the values are in signed range, get
2612 * rid of stupid unsigned, just causes bugs
2614 header_len = header_len_unsigned;
2615 body_len = body_len_unsigned;
2617 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
2619 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
2621 loader->corrupted = TRUE;
2625 if (header_len + body_len > loader->max_message_size)
2627 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %d\n",
2628 header_len, body_len, loader->max_message_size);
2629 loader->corrupted = TRUE;
2633 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
2635 HeaderField fields[FIELD_LAST];
2639 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len);
2640 if (!decode_header_data (&loader->data, header_len, byte_order,
2641 fields, &header_padding))
2643 _dbus_verbose ("Header was invalid\n");
2644 loader->corrupted = TRUE;
2648 next_arg = header_len;
2649 while (next_arg < (header_len + body_len))
2651 int prev = next_arg;
2653 if (!_dbus_marshal_validate_arg (&loader->data,
2658 loader->corrupted = TRUE;
2662 _dbus_assert (next_arg > prev);
2665 if (next_arg > (header_len + body_len))
2667 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
2668 next_arg, header_len, body_len,
2669 header_len + body_len);
2670 loader->corrupted = TRUE;
2674 message = dbus_message_new_empty_header ();
2675 if (message == NULL)
2676 break; /* ugh, postpone this I guess. */
2678 message->byte_order = byte_order;
2679 message->header_padding = header_padding;
2681 /* Copy in the offsets we found */
2683 while (i < FIELD_LAST)
2685 message->header_fields[i] = fields[i];
2689 if (!_dbus_list_append (&loader->messages, message))
2691 dbus_message_unref (message);
2695 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
2696 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
2698 _dbus_assert (_dbus_string_get_length (&loader->data) >=
2699 (header_len + body_len));
2701 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
2703 _dbus_list_remove_last (&loader->messages, message);
2704 dbus_message_unref (message);
2708 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
2712 /* put the header back, we'll try again later */
2713 result = _dbus_string_copy_len (&message->header, 0, header_len,
2715 _dbus_assert (result); /* because DBusString never reallocs smaller */
2717 _dbus_list_remove_last (&loader->messages, message);
2718 dbus_message_unref (message);
2722 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
2723 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
2725 _dbus_verbose ("Loaded message %p\n", message);
2733 * Pops a loaded message (passing ownership of the message
2734 * to the caller). Returns #NULL if no messages have been
2737 * @param loader the loader.
2738 * @returns the next message, or #NULL if none.
2741 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
2743 return _dbus_list_pop_first (&loader->messages);
2748 * Checks whether the loader is confused due to bad data.
2749 * If messages are received that are invalid, the
2750 * loader gets confused and gives up permanently.
2751 * This state is called "corrupted."
2753 * @param loader the loader
2754 * @returns #TRUE if the loader is hosed.
2757 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
2759 return loader->corrupted;
2763 * Sets the maximum size message we allow.
2765 * @param loader the loader
2766 * @param size the max message size in bytes
2769 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
2772 if (size > MAX_SANE_MESSAGE_SIZE)
2774 _dbus_verbose ("clamping requested max message size %ld to %d\n",
2775 size, MAX_SANE_MESSAGE_SIZE);
2776 size = MAX_SANE_MESSAGE_SIZE;
2778 loader->max_message_size = size;
2782 * Gets the maximum allowed message size in bytes.
2784 * @param loader the loader
2785 * @returns max size in bytes
2788 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
2790 return loader->max_message_size;
2794 #ifdef DBUS_BUILD_TESTS
2795 #include "dbus-test.h"
2799 message_iter_test (DBusMessage *message)
2801 DBusMessageIter *iter;
2804 iter = dbus_message_get_args_iter (message);
2807 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_STRING)
2808 _dbus_assert_not_reached ("Argument type isn't string");
2810 str = dbus_message_iter_get_string (iter);
2811 if (strcmp (str, "Test string") != 0)
2812 _dbus_assert_not_reached ("Strings differ");
2815 if (!dbus_message_iter_next (iter))
2816 _dbus_assert_not_reached ("Reached end of arguments");
2818 /* Signed integer tests */
2819 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_INT32)
2820 _dbus_assert_not_reached ("Argument type isn't int32");
2822 if (dbus_message_iter_get_int32 (iter) != -0x12345678)
2823 _dbus_assert_not_reached ("Signed integers differ");
2825 if (!dbus_message_iter_next (iter))
2826 _dbus_assert_not_reached ("Reached end of fields");
2828 /* Unsigned integer tests */
2829 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_UINT32)
2830 _dbus_assert_not_reached ("Argument type isn't int32");
2832 if (dbus_message_iter_get_uint32 (iter) != 0xedd1e)
2833 _dbus_assert_not_reached ("Unsigned integers differ");
2835 if (!dbus_message_iter_next (iter))
2836 _dbus_assert_not_reached ("Reached end of arguments");
2839 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_DOUBLE)
2840 _dbus_assert_not_reached ("Argument type isn't double");
2842 if (dbus_message_iter_get_double (iter) != 3.14159)
2843 _dbus_assert_not_reached ("Doubles differ");
2845 if (dbus_message_iter_next (iter))
2846 _dbus_assert_not_reached ("Didn't reach end of arguments");
2848 dbus_message_iter_unref (iter);
2852 check_message_handling (DBusMessage *message)
2854 DBusMessageIter *iter;
2857 dbus_int32_t client_serial;
2862 client_serial = _dbus_message_get_client_serial (message);
2864 /* can't use set_client_serial due to the assertions at the start of it */
2865 set_int_field (message, FIELD_CLIENT_SERIAL,
2868 if (client_serial != _dbus_message_get_client_serial (message))
2870 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
2874 /* If we implement message_set_arg (message, n, value)
2875 * then we would want to test it here
2878 iter = dbus_message_get_args_iter (message);
2879 while ((type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID)
2885 case DBUS_TYPE_INT32:
2886 dbus_message_iter_get_int32 (iter);
2888 case DBUS_TYPE_UINT32:
2889 dbus_message_iter_get_uint32 (iter);
2891 case DBUS_TYPE_DOUBLE:
2892 dbus_message_iter_get_double (iter);
2894 case DBUS_TYPE_STRING:
2897 str = dbus_message_iter_get_string (iter);
2901 case DBUS_TYPE_INT32_ARRAY:
2903 dbus_int32_t *values;
2906 if (!dbus_message_iter_get_int32_array (iter, &values, &len))
2912 case DBUS_TYPE_UINT32_ARRAY:
2914 dbus_uint32_t *values;
2917 if (!dbus_message_iter_get_uint32_array (iter, &values, &len))
2923 case DBUS_TYPE_DOUBLE_ARRAY:
2928 if (!dbus_message_iter_get_double_array (iter, &values, &len))
2934 case DBUS_TYPE_STRING_ARRAY:
2939 if (!dbus_message_iter_get_string_array (iter, &values, &len))
2942 dbus_free_string_array (values);
2950 if (!dbus_message_iter_next (iter))
2958 dbus_message_iter_unref (iter);
2964 check_have_valid_message (DBusMessageLoader *loader)
2966 DBusMessage *message;
2972 if (_dbus_message_loader_get_is_corrupted (loader))
2974 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
2978 message = _dbus_message_loader_pop_message (loader);
2979 if (message == NULL)
2981 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
2985 if (_dbus_string_get_length (&loader->data) > 0)
2987 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
2991 /* Verify that we're able to properly deal with the message.
2992 * For example, this would detect improper handling of messages
2993 * in nonstandard byte order.
2995 if (!check_message_handling (message))
3002 dbus_message_unref (message);
3007 check_invalid_message (DBusMessageLoader *loader)
3013 if (!_dbus_message_loader_get_is_corrupted (loader))
3015 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
3026 check_incomplete_message (DBusMessageLoader *loader)
3028 DBusMessage *message;
3034 if (_dbus_message_loader_get_is_corrupted (loader))
3036 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
3040 message = _dbus_message_loader_pop_message (loader);
3041 if (message != NULL)
3043 _dbus_warn ("loaded message that was expected to be incomplete\n");
3051 dbus_message_unref (message);
3056 check_loader_results (DBusMessageLoader *loader,
3057 DBusMessageValidity validity)
3061 case _DBUS_MESSAGE_VALID:
3062 return check_have_valid_message (loader);
3063 case _DBUS_MESSAGE_INVALID:
3064 return check_invalid_message (loader);
3065 case _DBUS_MESSAGE_INCOMPLETE:
3066 return check_incomplete_message (loader);
3067 case _DBUS_MESSAGE_UNKNOWN:
3071 _dbus_assert_not_reached ("bad DBusMessageValidity");
3077 * Loads the message in the given message file.
3079 * @param filename filename to load
3080 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3081 * @param data string to load message into
3082 * @returns #TRUE if the message was loaded
3085 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
3095 DBusResultCode result;
3097 result = _dbus_file_get_contents (data, filename);
3098 if (result != DBUS_RESULT_SUCCESS)
3101 _dbus_string_get_const_data (filename, &s);
3102 _dbus_warn ("Could not load message file %s\n", s);
3108 if (!_dbus_message_data_load (data, filename))
3111 _dbus_string_get_const_data (filename, &s);
3112 _dbus_warn ("Could not load message file %s\n", s);
3125 * Tries loading the message in the given message file
3126 * and verifies that DBusMessageLoader can handle it.
3128 * @param filename filename to load
3129 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3130 * @param expected_validity what the message has to be like to return #TRUE
3131 * @returns #TRUE if the message has the expected validity
3134 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
3136 DBusMessageValidity expected_validity)
3143 if (!_dbus_string_init (&data, _DBUS_INT_MAX))
3144 _dbus_assert_not_reached ("could not allocate string\n");
3146 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
3150 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
3158 if (_dbus_string_get_length (&data) > 0)
3159 _dbus_verbose_bytes_of_string (&data, 0,
3160 _dbus_string_get_length (&data));
3162 _dbus_string_get_const_data (filename, &s);
3163 _dbus_warn ("Failed message loader test on %s\n",
3167 _dbus_string_free (&data);
3173 * Tries loading the given message data.
3176 * @param data the message data
3177 * @param expected_validity what the message has to be like to return #TRUE
3178 * @returns #TRUE if the message has the expected validity
3181 dbus_internal_do_not_use_try_message_data (const DBusString *data,
3182 DBusMessageValidity expected_validity)
3184 DBusMessageLoader *loader;
3192 /* Write the data one byte at a time */
3194 loader = _dbus_message_loader_new ();
3196 len = _dbus_string_get_length (data);
3197 for (i = 0; i < len; i++)
3201 _dbus_message_loader_get_buffer (loader, &buffer);
3202 _dbus_string_append_byte (buffer,
3203 _dbus_string_get_byte (data, i));
3204 _dbus_message_loader_return_buffer (loader, buffer, 1);
3207 if (!check_loader_results (loader, expected_validity))
3210 _dbus_message_loader_unref (loader);
3213 /* Write the data all at once */
3215 loader = _dbus_message_loader_new ();
3220 _dbus_message_loader_get_buffer (loader, &buffer);
3221 _dbus_string_copy (data, 0, buffer,
3222 _dbus_string_get_length (buffer));
3223 _dbus_message_loader_return_buffer (loader, buffer, 1);
3226 if (!check_loader_results (loader, expected_validity))
3229 _dbus_message_loader_unref (loader);
3232 /* Write the data 2 bytes at a time */
3234 loader = _dbus_message_loader_new ();
3236 len = _dbus_string_get_length (data);
3237 for (i = 0; i < len; i += 2)
3241 _dbus_message_loader_get_buffer (loader, &buffer);
3242 _dbus_string_append_byte (buffer,
3243 _dbus_string_get_byte (data, i));
3245 _dbus_string_append_byte (buffer,
3246 _dbus_string_get_byte (data, i+1));
3247 _dbus_message_loader_return_buffer (loader, buffer, 1);
3250 if (!check_loader_results (loader, expected_validity))
3253 _dbus_message_loader_unref (loader);
3261 _dbus_message_loader_unref (loader);
3267 process_test_subdir (const DBusString *test_base_dir,
3269 DBusMessageValidity validity,
3270 DBusForeachMessageFileFunc function,
3273 DBusString test_directory;
3274 DBusString filename;
3277 DBusResultCode result;
3282 if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
3283 _dbus_assert_not_reached ("didn't allocate test_directory\n");
3285 _dbus_string_init_const (&filename, subdir);
3287 if (!_dbus_string_copy (test_base_dir, 0,
3288 &test_directory, 0))
3289 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3291 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3292 _dbus_assert_not_reached ("couldn't allocate full path");
3294 _dbus_string_free (&filename);
3295 if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
3296 _dbus_assert_not_reached ("didn't allocate filename string\n");
3298 dir = _dbus_directory_open (&test_directory, &result);
3302 _dbus_string_get_const_data (&test_directory, &s);
3303 _dbus_warn ("Could not open %s: %s\n", s,
3304 dbus_result_to_string (result));
3308 printf ("Testing:\n");
3310 result = DBUS_RESULT_SUCCESS;
3312 while (_dbus_directory_get_next_file (dir, &filename, &result))
3314 DBusString full_path;
3317 if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
3318 _dbus_assert_not_reached ("couldn't init string");
3320 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3321 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3323 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3324 _dbus_assert_not_reached ("couldn't concat file to dir");
3326 if (_dbus_string_ends_with_c_str (&filename, ".message"))
3328 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
3332 const char *filename_c;
3333 _dbus_string_get_const_data (&filename, &filename_c);
3334 _dbus_verbose ("Skipping non-.message file %s\n",
3336 _dbus_string_free (&full_path);
3342 _dbus_string_get_const_data (&filename, &s);
3343 printf (" %s\n", s);
3346 _dbus_verbose (" expecting %s\n",
3347 validity == _DBUS_MESSAGE_VALID ? "valid" :
3348 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
3349 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
3351 if (! (*function) (&full_path, is_raw, validity, user_data))
3353 _dbus_string_free (&full_path);
3357 _dbus_string_free (&full_path);
3360 if (result != DBUS_RESULT_SUCCESS)
3363 _dbus_string_get_const_data (&test_directory, &s);
3364 _dbus_warn ("Could not get next file in %s: %s\n",
3365 s, dbus_result_to_string (result));
3374 _dbus_directory_close (dir);
3375 _dbus_string_free (&test_directory);
3376 _dbus_string_free (&filename);
3382 * Runs the given function on every message file in the test suite.
3383 * The function should return #FALSE on test failure or fatal error.
3385 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
3386 * @param func the function to run
3387 * @param user_data data for function
3388 * @returns #FALSE if there's a failure
3391 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
3392 DBusForeachMessageFileFunc func,
3395 DBusString test_directory;
3400 _dbus_string_init_const (&test_directory, test_data_dir);
3402 if (!process_test_subdir (&test_directory, "valid-messages",
3403 _DBUS_MESSAGE_VALID, func, user_data))
3406 if (!process_test_subdir (&test_directory, "invalid-messages",
3407 _DBUS_MESSAGE_INVALID, func, user_data))
3410 if (!process_test_subdir (&test_directory, "incomplete-messages",
3411 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
3418 _dbus_string_free (&test_directory);
3424 * @ingroup DBusMessageInternals
3425 * Unit test for DBusMessage.
3427 * @returns #TRUE on success.
3430 _dbus_message_test (const char *test_data_dir)
3432 DBusMessage *message;
3433 DBusMessageLoader *loader;
3436 dbus_int32_t our_int;
3439 dbus_bool_t our_bool;
3441 /* Test the vararg functions */
3442 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3443 _dbus_message_set_client_serial (message, 1);
3444 dbus_message_append_args (message,
3445 DBUS_TYPE_INT32, -0x12345678,
3446 DBUS_TYPE_STRING, "Test string",
3447 DBUS_TYPE_DOUBLE, 3.14159,
3448 DBUS_TYPE_BOOLEAN, TRUE,
3450 _dbus_verbose_bytes_of_string (&message->header, 0,
3451 _dbus_string_get_length (&message->header));
3452 _dbus_verbose_bytes_of_string (&message->body, 0,
3453 _dbus_string_get_length (&message->body));
3455 if (dbus_message_get_args (message,
3456 DBUS_TYPE_INT32, &our_int,
3457 DBUS_TYPE_STRING, &our_str,
3458 DBUS_TYPE_DOUBLE, &our_double,
3459 DBUS_TYPE_BOOLEAN, &our_bool,
3460 0) != DBUS_RESULT_SUCCESS)
3461 _dbus_assert_not_reached ("Could not get arguments");
3463 if (our_int != -0x12345678)
3464 _dbus_assert_not_reached ("integers differ!");
3466 if (our_double != 3.14159)
3467 _dbus_assert_not_reached ("doubles differ!");
3469 if (strcmp (our_str, "Test string") != 0)
3470 _dbus_assert_not_reached ("strings differ!");
3473 _dbus_assert_not_reached ("booleans differ");
3475 dbus_free (our_str);
3476 dbus_message_unref (message);
3478 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3479 _dbus_message_set_client_serial (message, 1);
3480 _dbus_message_set_reply_serial (message, 0x12345678);
3482 dbus_message_append_string (message, "Test string");
3483 dbus_message_append_int32 (message, -0x12345678);
3484 dbus_message_append_uint32 (message, 0xedd1e);
3485 dbus_message_append_double (message, 3.14159);
3487 message_iter_test (message);
3489 /* Message loader test */
3490 _dbus_message_lock (message);
3491 loader = _dbus_message_loader_new ();
3493 /* Write the header data one byte at a time */
3494 _dbus_string_get_const_data (&message->header, &data);
3495 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
3499 _dbus_message_loader_get_buffer (loader, &buffer);
3500 _dbus_string_append_byte (buffer, data[i]);
3501 _dbus_message_loader_return_buffer (loader, buffer, 1);
3504 /* Write the body data one byte at a time */
3505 _dbus_string_get_const_data (&message->body, &data);
3506 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
3510 _dbus_message_loader_get_buffer (loader, &buffer);
3511 _dbus_string_append_byte (buffer, data[i]);
3512 _dbus_message_loader_return_buffer (loader, buffer, 1);
3515 dbus_message_unref (message);
3517 /* Now pop back the message */
3518 if (_dbus_message_loader_get_is_corrupted (loader))
3519 _dbus_assert_not_reached ("message loader corrupted");
3521 message = _dbus_message_loader_pop_message (loader);
3523 _dbus_assert_not_reached ("received a NULL message");
3525 if (_dbus_message_get_reply_serial (message) != 0x12345678)
3526 _dbus_assert_not_reached ("reply serial fields differ");
3528 message_iter_test (message);
3530 dbus_message_unref (message);
3531 _dbus_message_loader_unref (loader);
3533 /* Now load every message in test_data_dir if we have one */
3534 if (test_data_dir == NULL)
3537 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
3538 (DBusForeachMessageFileFunc)
3539 dbus_internal_do_not_use_try_message_file,
3543 #endif /* DBUS_BUILD_TESTS */