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);
1728 dbus_message_ref (message);
1731 iter->message = message;
1738 * Increments the reference count of a DBusMessageIter.
1740 * @param iter the message iter
1741 * @see dbus_message_iter_unref
1744 dbus_message_iter_ref (DBusMessageIter *iter)
1746 _dbus_assert (iter->refcount > 0);
1748 iter->refcount += 1;
1752 * Decrements the reference count of a DBusMessageIter.
1754 * @param iter The message iter
1755 * @see dbus_message_iter_ref
1758 dbus_message_iter_unref (DBusMessageIter *iter)
1760 _dbus_assert (iter->refcount > 0);
1762 iter->refcount -= 1;
1764 if (iter->refcount == 0)
1766 dbus_message_unref (iter->message);
1773 * Checks if an iterator has any more fields.
1775 * @param iter the message iter
1776 * @returns #TRUE if there are more fields
1780 dbus_message_iter_has_next (DBusMessageIter *iter)
1784 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1785 iter->message->byte_order,
1786 iter->pos, &end_pos))
1789 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1796 * Moves the iterator to the next field.
1798 * @param iter The message iter
1799 * @returns #TRUE if the iterator was moved to the next field
1802 dbus_message_iter_next (DBusMessageIter *iter)
1806 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1807 iter->message->byte_order,
1808 iter->pos, &end_pos))
1811 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1814 iter->pos = end_pos;
1820 * Returns the argument type of the argument that the
1821 * message iterator points at.
1823 * @param iter the message iter
1824 * @returns the field type
1827 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1831 if (iter->pos >= _dbus_string_get_length (&iter->message->body))
1832 return DBUS_TYPE_INVALID;
1834 _dbus_string_get_const_data_len (&iter->message->body, &data, iter->pos, 1);
1836 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_STRING_ARRAY)
1839 return DBUS_TYPE_INVALID;
1843 * Returns the string value that an iterator may point to.
1844 * Note that you need to check that the iterator points to
1845 * a string value before using this function.
1847 * @see dbus_message_iter_get_arg_type
1848 * @param iter the message iter
1849 * @returns the string
1852 dbus_message_iter_get_string (DBusMessageIter *iter)
1854 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING);
1856 return _dbus_demarshal_string (&iter->message->body, iter->message->byte_order,
1857 iter->pos + 1, NULL);
1861 * Returns the boolean value that an iterator may point to.
1862 * Note that you need to check that the iterator points to
1863 * a boolean value before using this function.
1865 * @see dbus_message_iter_get_arg_type
1866 * @param iter the message iter
1867 * @returns the string
1870 dbus_message_iter_get_boolean (DBusMessageIter *iter)
1872 unsigned char value;
1874 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN);
1876 value = _dbus_string_get_byte (&iter->message->body, iter->pos + 1);
1883 * Returns the 32 bit signed integer value that an iterator may point to.
1884 * Note that you need to check that the iterator points to
1885 * an integer value before using this function.
1887 * @see dbus_message_iter_get_arg_type
1888 * @param iter the message iter
1889 * @returns the integer
1892 dbus_message_iter_get_int32 (DBusMessageIter *iter)
1894 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32);
1896 return _dbus_demarshal_int32 (&iter->message->body, iter->message->byte_order,
1897 iter->pos + 1, NULL);
1901 * Returns the 32 bit unsigned integer value that an iterator may point to.
1902 * Note that you need to check that the iterator points to
1903 * an unsigned integer value before using this function.
1905 * @see dbus_message_iter_get_arg_type
1906 * @param iter the message iter
1907 * @returns the integer
1910 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
1912 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32);
1914 return _dbus_demarshal_uint32 (&iter->message->body, iter->message->byte_order,
1915 iter->pos + 1, NULL);
1919 * Returns the double value that an iterator may point to.
1920 * Note that you need to check that the iterator points to
1921 * a string value before using this function.
1923 * @see dbus_message_iter_get_arg_type
1924 * @param iter the message iter
1925 * @returns the double
1928 dbus_message_iter_get_double (DBusMessageIter *iter)
1930 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE);
1932 return _dbus_demarshal_double (&iter->message->body, iter->message->byte_order,
1933 iter->pos + 1, NULL);
1937 * Returns the boolean array that the iterator may point to. Note that
1938 * you need to check that the iterator points to an array of the
1939 * correct type prior to using this function.
1941 * @param iter the iterator
1942 * @param value return location for the array
1943 * @param len return location for the array length
1944 * @returns #TRUE on success
1947 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
1948 unsigned char **value,
1951 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN_ARRAY);
1953 *value = _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
1954 iter->pos + 1, NULL, len);
1963 * Returns the 32 bit signed integer array that the iterator may point
1964 * to. Note that you need to check that the iterator points to an
1965 * array of the correct type prior to using this function.
1967 * @param iter the iterator
1968 * @param value return location for the array
1969 * @param len return location for the array length
1970 * @returns #TRUE on success
1973 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
1974 dbus_int32_t **value,
1977 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32_ARRAY);
1979 *value = _dbus_demarshal_int32_array (&iter->message->body, iter->message->byte_order,
1980 iter->pos + 1, NULL, len);
1989 * Returns the 32 bit unsigned integer array that the iterator may point
1990 * to. Note that you need to check that the iterator points to an
1991 * array of the correct type prior to using this function.
1993 * @param iter the iterator
1994 * @param value return location for the array
1995 * @param len return location for the array length
1996 * @returns #TRUE on success
1999 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
2000 dbus_uint32_t **value,
2003 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32_ARRAY);
2005 *value = _dbus_demarshal_uint32_array (&iter->message->body, iter->message->byte_order,
2006 iter->pos + 1, NULL, len);
2015 * Returns the double array that the iterator may point to. Note that
2016 * you need to check that the iterator points to an array of the
2017 * correct type prior to using this function.
2019 * @param iter the iterator
2020 * @param value return location for the array
2021 * @param len return location for the array length
2022 * @returns #TRUE on success
2025 dbus_message_iter_get_double_array (DBusMessageIter *iter,
2029 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE_ARRAY);
2031 *value = _dbus_demarshal_double_array (&iter->message->body, iter->message->byte_order,
2032 iter->pos + 1, NULL, len);
2041 * Returns the byte array that the iterator may point to.
2042 * Note that you need to check that the iterator points
2043 * to a byte array prior to using this function.
2045 * @param iter the iterator
2046 * @param value return location for array values
2047 * @param len return location for length of byte array
2048 * @returns the byte array
2051 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
2052 unsigned char **value,
2055 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BYTE_ARRAY);
2057 *value = _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2058 iter->pos + 1, NULL, len);
2067 * Returns the string array that the iterator may point to.
2068 * Note that you need to check that the iterator points
2069 * to a byte array prior to using this function.
2071 * The returned value is a #NULL-terminated array of strings.
2072 * Each string is a separate malloc block, and the array
2073 * itself is a malloc block. You can free this type of
2074 * string array with dbus_free_string_array().
2076 * @param iter the iterator
2077 * @param value return location for string values
2078 * @param len return location for length of byte array
2079 * @returns the byte array
2082 dbus_message_iter_get_string_array (DBusMessageIter *iter,
2086 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING_ARRAY);
2088 *value = _dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
2089 iter->pos + 1, NULL, len);
2098 * Sets the message sender.
2100 * @param message the message
2101 * @param sender the sender
2102 * @returns #FALSE if not enough memory
2105 dbus_message_set_sender (DBusMessage *message,
2108 _dbus_assert (!message->locked);
2112 delete_string_field (message, FIELD_SENDER);
2117 return set_string_field (message,
2124 * Sets a flag indicating that the message is an error reply
2125 * message, i.e. an "exception" rather than a normal response.
2127 * @param message the message
2128 * @param is_error_reply #TRUE if this is an error message.
2131 dbus_message_set_is_error (DBusMessage *message,
2132 dbus_bool_t is_error_reply)
2136 _dbus_assert (!message->locked);
2138 _dbus_string_get_data_len (&message->header, &header, 1, 1);
2141 *header |= DBUS_HEADER_FLAG_ERROR;
2143 *header &= ~DBUS_HEADER_FLAG_ERROR;
2147 * Returns #TRUE if the message is an error
2148 * reply to some previous message we sent.
2150 * @param message the message
2151 * @returns #TRUE if the message is an error
2154 dbus_message_get_is_error (DBusMessage *message)
2158 _dbus_string_get_const_data_len (&message->header, &header, 1, 1);
2160 return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
2164 * Gets the service which originated this message,
2165 * or #NULL if unknown or inapplicable.
2167 * @param message the message
2168 * @returns the service name or #NULL
2171 dbus_message_get_sender (DBusMessage *message)
2173 return get_string_field (message, FIELD_SENDER, NULL);
2177 * Checks whether the message has the given name.
2178 * If the message has no name or has a different
2179 * name, returns #FALSE.
2181 * @param message the message
2182 * @param name the name to check (must not be #NULL)
2184 * @returns #TRUE if the message has the given name
2187 dbus_message_name_is (DBusMessage *message,
2192 _dbus_assert (name != NULL);
2194 n = dbus_message_get_name (message);
2196 if (n && strcmp (n, name) == 0)
2205 * @addtogroup DBusMessageInternals
2210 * @typedef DBusMessageLoader
2212 * The DBusMessageLoader object encapsulates the process of converting
2213 * a byte stream into a series of DBusMessage. It buffers the incoming
2214 * bytes as efficiently as possible, and generates a queue of
2215 * messages. DBusMessageLoader is typically used as part of a
2216 * DBusTransport implementation. The DBusTransport then hands off
2217 * the loaded messages to a DBusConnection, making the messages
2218 * visible to the application.
2222 /* we definitely use signed ints for sizes, so don't exceed
2223 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
2224 * over 128M is pretty nuts anyhow.
2228 * The maximum sane message size.
2230 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
2233 * Implementation details of DBusMessageLoader.
2234 * All members are private.
2236 struct DBusMessageLoader
2238 int refcount; /**< Reference count. */
2240 DBusString data; /**< Buffered data */
2242 DBusList *messages; /**< Complete messages. */
2244 long max_message_size; /**< Maximum size of a message */
2246 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
2248 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
2252 * The initial buffer size of the message loader.
2254 * @todo this should be based on min header size plus some average
2255 * body size, or something. Or rather, the min header size only, if we
2256 * want to try to read only the header, store that in a DBusMessage,
2257 * then read only the body and store that, etc., depends on
2258 * how we optimize _dbus_message_loader_get_buffer() and what
2259 * the exact message format is.
2261 #define INITIAL_LOADER_DATA_LEN 32
2264 * Creates a new message loader. Returns #NULL if memory can't
2267 * @returns new loader, or #NULL.
2270 _dbus_message_loader_new (void)
2272 DBusMessageLoader *loader;
2274 loader = dbus_new0 (DBusMessageLoader, 1);
2278 loader->refcount = 1;
2280 /* Try to cap message size at something that won't *totally* hose
2281 * the system if we have a couple of them.
2283 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
2285 if (!_dbus_string_init (&loader->data, _DBUS_INT_MAX))
2291 /* preallocate the buffer for speed, ignore failure */
2292 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
2293 _dbus_string_set_length (&loader->data, 0);
2299 * Increments the reference count of the loader.
2301 * @param loader the loader.
2304 _dbus_message_loader_ref (DBusMessageLoader *loader)
2306 loader->refcount += 1;
2310 * Decrements the reference count of the loader and finalizes the
2311 * loader when the count reaches zero.
2313 * @param loader the loader.
2316 _dbus_message_loader_unref (DBusMessageLoader *loader)
2318 loader->refcount -= 1;
2319 if (loader->refcount == 0)
2321 _dbus_list_foreach (&loader->messages,
2322 (DBusForeachFunction) dbus_message_unref,
2324 _dbus_list_clear (&loader->messages);
2325 _dbus_string_free (&loader->data);
2331 * Gets the buffer to use for reading data from the network. Network
2332 * data is read directly into an allocated buffer, which is then used
2333 * in the DBusMessage, to avoid as many extra memcpy's as possible.
2334 * The buffer must always be returned immediately using
2335 * _dbus_message_loader_return_buffer(), even if no bytes are
2336 * successfully read.
2338 * @todo this function can be a lot more clever. For example
2339 * it can probably always return a buffer size to read exactly
2340 * the body of the next message, thus avoiding any memory wastage
2343 * @param loader the message loader.
2344 * @param buffer the buffer
2347 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
2348 DBusString **buffer)
2350 _dbus_assert (!loader->buffer_outstanding);
2352 *buffer = &loader->data;
2354 loader->buffer_outstanding = TRUE;
2358 * The smallest header size that can occur.
2359 * (It won't be valid)
2361 #define DBUS_MINIMUM_HEADER_SIZE 16
2363 /** Pack four characters as in "abcd" into a uint32 */
2364 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
2365 ((((dbus_uint32_t)a) << 24) | \
2366 (((dbus_uint32_t)b) << 16) | \
2367 (((dbus_uint32_t)c) << 8) | \
2370 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
2371 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
2372 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
2374 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
2375 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
2376 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
2378 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
2379 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
2380 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
2382 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
2383 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
2384 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
2387 decode_header_data (const DBusString *data,
2390 HeaderField fields[FIELD_LAST],
2391 int *message_padding)
2397 if (header_len < 16)
2401 while (i < FIELD_LAST)
2403 fields[i].offset = -1;
2407 fields[FIELD_HEADER_LENGTH].offset = 4;
2408 fields[FIELD_BODY_LENGTH].offset = 8;
2409 fields[FIELD_CLIENT_SERIAL].offset = 12;
2411 /* Now handle the named fields. A real named field is at least 4
2412 * bytes for the name, plus a type code (1 byte) plus padding. So
2413 * if we have less than 8 bytes left, it must be alignment padding,
2414 * not a field. While >= 8 bytes can't be entirely alignment
2418 while ((pos + 7) < header_len)
2420 pos = _DBUS_ALIGN_VALUE (pos, 4);
2422 if ((pos + 4) > header_len)
2425 _dbus_string_get_const_data_len (data, &field, pos, 4);
2428 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
2430 /* I believe FROM_BE is right, but if not we'll find out
2433 switch (DBUS_UINT32_FROM_BE (*(int*)field))
2435 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
2436 if (fields[FIELD_SERVICE].offset >= 0)
2438 _dbus_verbose ("%s field provided twice\n",
2439 DBUS_HEADER_FIELD_SERVICE);
2443 fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2444 _dbus_verbose ("Found service name at offset %d\n",
2445 fields[FIELD_SERVICE].offset);
2448 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
2449 if (fields[FIELD_NAME].offset >= 0)
2451 _dbus_verbose ("%s field provided twice\n",
2452 DBUS_HEADER_FIELD_NAME);
2456 fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2458 _dbus_verbose ("Found message name at offset %d\n",
2459 fields[FIELD_NAME].offset);
2461 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
2462 if (fields[FIELD_SENDER].offset >= 0)
2464 _dbus_verbose ("%s field provided twice\n",
2465 DBUS_HEADER_FIELD_SENDER);
2469 fields[FIELD_SENDER].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2471 _dbus_verbose ("Found sender name at offset %d\n",
2472 fields[FIELD_NAME].offset);
2475 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
2476 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
2478 _dbus_verbose ("%s field provided twice\n",
2479 DBUS_HEADER_FIELD_REPLY);
2483 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2485 _dbus_verbose ("Found reply serial at offset %d\n",
2486 fields[FIELD_REPLY_SERIAL].offset);
2490 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c at offset %d\n",
2491 field[0], field[1], field[2], field[3], pos);
2494 if (!_dbus_marshal_validate_arg (data, byte_order, pos, &new_pos))
2496 _dbus_verbose ("Failed to validate argument to named header field\n");
2500 if (new_pos > header_len)
2502 _dbus_verbose ("Named header field tries to extend beyond header length\n");
2509 if (pos < header_len)
2511 /* Alignment padding, verify that it's nul */
2512 _dbus_assert ((header_len - pos) < 8);
2514 if (!_dbus_string_validate_nul (data,
2515 pos, (header_len - pos)))
2517 _dbus_verbose ("header alignment padding is not nul\n");
2521 if (message_padding)
2522 *message_padding = header_len - pos;
2529 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
2530 * indicating to the loader how many bytes of the buffer were filled
2531 * in. This function must always be called, even if no bytes were
2532 * successfully read.
2534 * @todo if we run out of memory in here, we offer no way for calling
2535 * code to handle it, i.e. they can't re-run the message parsing
2536 * attempt. Perhaps much of this code could be moved to pop_message()?
2537 * But then that may need to distinguish NULL return for no messages
2538 * from NULL return for errors.
2540 * @param loader the loader.
2541 * @param buffer the buffer.
2542 * @param bytes_read number of bytes that were read into the buffer.
2545 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
2549 _dbus_assert (loader->buffer_outstanding);
2550 _dbus_assert (buffer == &loader->data);
2552 loader->buffer_outstanding = FALSE;
2554 if (loader->corrupted)
2557 while (_dbus_string_get_length (&loader->data) >= 16)
2559 DBusMessage *message;
2560 const char *header_data;
2561 int byte_order, header_len, body_len, header_padding;
2562 dbus_uint32_t header_len_unsigned, body_len_unsigned;
2564 _dbus_string_get_const_data_len (&loader->data, &header_data, 0, 16);
2566 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
2568 if (header_data[2] != DBUS_MAJOR_PROTOCOL_VERSION)
2570 _dbus_verbose ("Message has protocol version %d ours is %d\n",
2571 (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
2572 loader->corrupted = TRUE;
2576 byte_order = header_data[0];
2578 if (byte_order != DBUS_LITTLE_ENDIAN &&
2579 byte_order != DBUS_BIG_ENDIAN)
2581 _dbus_verbose ("Message with bad byte order '%c' received\n",
2583 loader->corrupted = TRUE;
2587 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
2588 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
2590 if (header_len_unsigned < 16)
2592 _dbus_verbose ("Message had broken too-small header length %u\n",
2593 header_len_unsigned);
2594 loader->corrupted = TRUE;
2598 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
2599 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
2601 _dbus_verbose ("Header or body length too large (%u %u)\n",
2602 header_len_unsigned,
2604 loader->corrupted = TRUE;
2608 /* Now that we know the values are in signed range, get
2609 * rid of stupid unsigned, just causes bugs
2611 header_len = header_len_unsigned;
2612 body_len = body_len_unsigned;
2614 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
2616 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
2618 loader->corrupted = TRUE;
2622 if (header_len + body_len > loader->max_message_size)
2624 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %d\n",
2625 header_len, body_len, loader->max_message_size);
2626 loader->corrupted = TRUE;
2630 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
2632 HeaderField fields[FIELD_LAST];
2636 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len);
2637 if (!decode_header_data (&loader->data, header_len, byte_order,
2638 fields, &header_padding))
2640 _dbus_verbose ("Header was invalid\n");
2641 loader->corrupted = TRUE;
2645 next_arg = header_len;
2646 while (next_arg < (header_len + body_len))
2648 int prev = next_arg;
2650 if (!_dbus_marshal_validate_arg (&loader->data,
2655 loader->corrupted = TRUE;
2659 _dbus_assert (next_arg > prev);
2662 if (next_arg > (header_len + body_len))
2664 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
2665 next_arg, header_len, body_len,
2666 header_len + body_len);
2667 loader->corrupted = TRUE;
2671 message = dbus_message_new_empty_header ();
2672 if (message == NULL)
2673 break; /* ugh, postpone this I guess. */
2675 message->byte_order = byte_order;
2676 message->header_padding = header_padding;
2678 /* Copy in the offsets we found */
2680 while (i < FIELD_LAST)
2682 message->header_fields[i] = fields[i];
2686 if (!_dbus_list_append (&loader->messages, message))
2688 dbus_message_unref (message);
2692 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
2693 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
2695 _dbus_assert (_dbus_string_get_length (&loader->data) >=
2696 (header_len + body_len));
2698 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
2700 _dbus_list_remove_last (&loader->messages, message);
2701 dbus_message_unref (message);
2705 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
2709 /* put the header back, we'll try again later */
2710 result = _dbus_string_copy_len (&message->header, 0, header_len,
2712 _dbus_assert (result); /* because DBusString never reallocs smaller */
2714 _dbus_list_remove_last (&loader->messages, message);
2715 dbus_message_unref (message);
2719 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
2720 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
2722 _dbus_verbose ("Loaded message %p\n", message);
2730 * Pops a loaded message (passing ownership of the message
2731 * to the caller). Returns #NULL if no messages have been
2734 * @param loader the loader.
2735 * @returns the next message, or #NULL if none.
2738 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
2740 return _dbus_list_pop_first (&loader->messages);
2745 * Checks whether the loader is confused due to bad data.
2746 * If messages are received that are invalid, the
2747 * loader gets confused and gives up permanently.
2748 * This state is called "corrupted."
2750 * @param loader the loader
2751 * @returns #TRUE if the loader is hosed.
2754 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
2756 return loader->corrupted;
2760 * Sets the maximum size message we allow.
2762 * @param loader the loader
2763 * @param size the max message size in bytes
2766 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
2769 if (size > MAX_SANE_MESSAGE_SIZE)
2771 _dbus_verbose ("clamping requested max message size %ld to %d\n",
2772 size, MAX_SANE_MESSAGE_SIZE);
2773 size = MAX_SANE_MESSAGE_SIZE;
2775 loader->max_message_size = size;
2779 * Gets the maximum allowed message size in bytes.
2781 * @param loader the loader
2782 * @returns max size in bytes
2785 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
2787 return loader->max_message_size;
2791 #ifdef DBUS_BUILD_TESTS
2792 #include "dbus-test.h"
2796 message_iter_test (DBusMessage *message)
2798 DBusMessageIter *iter;
2801 iter = dbus_message_get_args_iter (message);
2804 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_STRING)
2805 _dbus_assert_not_reached ("Argument type isn't string");
2807 str = dbus_message_iter_get_string (iter);
2808 if (strcmp (str, "Test string") != 0)
2809 _dbus_assert_not_reached ("Strings differ");
2812 if (!dbus_message_iter_next (iter))
2813 _dbus_assert_not_reached ("Reached end of arguments");
2815 /* Signed integer tests */
2816 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_INT32)
2817 _dbus_assert_not_reached ("Argument type isn't int32");
2819 if (dbus_message_iter_get_int32 (iter) != -0x12345678)
2820 _dbus_assert_not_reached ("Signed integers differ");
2822 if (!dbus_message_iter_next (iter))
2823 _dbus_assert_not_reached ("Reached end of fields");
2825 /* Unsigned integer tests */
2826 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_UINT32)
2827 _dbus_assert_not_reached ("Argument type isn't int32");
2829 if (dbus_message_iter_get_uint32 (iter) != 0xedd1e)
2830 _dbus_assert_not_reached ("Unsigned integers differ");
2832 if (!dbus_message_iter_next (iter))
2833 _dbus_assert_not_reached ("Reached end of arguments");
2836 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_DOUBLE)
2837 _dbus_assert_not_reached ("Argument type isn't double");
2839 if (dbus_message_iter_get_double (iter) != 3.14159)
2840 _dbus_assert_not_reached ("Doubles differ");
2842 if (dbus_message_iter_next (iter))
2843 _dbus_assert_not_reached ("Didn't reach end of arguments");
2845 dbus_message_iter_unref (iter);
2849 check_message_handling (DBusMessage *message)
2851 DBusMessageIter *iter;
2854 dbus_int32_t client_serial;
2859 client_serial = _dbus_message_get_client_serial (message);
2861 /* can't use set_client_serial due to the assertions at the start of it */
2862 set_int_field (message, FIELD_CLIENT_SERIAL,
2865 if (client_serial != _dbus_message_get_client_serial (message))
2867 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
2871 /* If we implement message_set_arg (message, n, value)
2872 * then we would want to test it here
2875 iter = dbus_message_get_args_iter (message);
2876 while ((type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID)
2882 case DBUS_TYPE_INT32:
2883 dbus_message_iter_get_int32 (iter);
2885 case DBUS_TYPE_UINT32:
2886 dbus_message_iter_get_uint32 (iter);
2888 case DBUS_TYPE_DOUBLE:
2889 dbus_message_iter_get_double (iter);
2891 case DBUS_TYPE_STRING:
2894 str = dbus_message_iter_get_string (iter);
2898 case DBUS_TYPE_INT32_ARRAY:
2900 dbus_int32_t *values;
2903 if (!dbus_message_iter_get_int32_array (iter, &values, &len))
2909 case DBUS_TYPE_UINT32_ARRAY:
2911 dbus_uint32_t *values;
2914 if (!dbus_message_iter_get_uint32_array (iter, &values, &len))
2920 case DBUS_TYPE_DOUBLE_ARRAY:
2925 if (!dbus_message_iter_get_double_array (iter, &values, &len))
2931 case DBUS_TYPE_STRING_ARRAY:
2936 if (!dbus_message_iter_get_string_array (iter, &values, &len))
2939 dbus_free_string_array (values);
2947 if (!dbus_message_iter_next (iter))
2955 dbus_message_iter_unref (iter);
2961 check_have_valid_message (DBusMessageLoader *loader)
2963 DBusMessage *message;
2969 if (_dbus_message_loader_get_is_corrupted (loader))
2971 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
2975 message = _dbus_message_loader_pop_message (loader);
2976 if (message == NULL)
2978 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
2982 if (_dbus_string_get_length (&loader->data) > 0)
2984 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
2988 /* Verify that we're able to properly deal with the message.
2989 * For example, this would detect improper handling of messages
2990 * in nonstandard byte order.
2992 if (!check_message_handling (message))
2999 dbus_message_unref (message);
3004 check_invalid_message (DBusMessageLoader *loader)
3010 if (!_dbus_message_loader_get_is_corrupted (loader))
3012 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
3023 check_incomplete_message (DBusMessageLoader *loader)
3025 DBusMessage *message;
3031 if (_dbus_message_loader_get_is_corrupted (loader))
3033 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
3037 message = _dbus_message_loader_pop_message (loader);
3038 if (message != NULL)
3040 _dbus_warn ("loaded message that was expected to be incomplete\n");
3048 dbus_message_unref (message);
3053 check_loader_results (DBusMessageLoader *loader,
3054 DBusMessageValidity validity)
3058 case _DBUS_MESSAGE_VALID:
3059 return check_have_valid_message (loader);
3060 case _DBUS_MESSAGE_INVALID:
3061 return check_invalid_message (loader);
3062 case _DBUS_MESSAGE_INCOMPLETE:
3063 return check_incomplete_message (loader);
3064 case _DBUS_MESSAGE_UNKNOWN:
3068 _dbus_assert_not_reached ("bad DBusMessageValidity");
3074 * Loads the message in the given message file.
3076 * @param filename filename to load
3077 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3078 * @param data string to load message into
3079 * @returns #TRUE if the message was loaded
3082 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
3092 DBusResultCode result;
3094 result = _dbus_file_get_contents (data, filename);
3095 if (result != DBUS_RESULT_SUCCESS)
3098 _dbus_string_get_const_data (filename, &s);
3099 _dbus_warn ("Could not load message file %s\n", s);
3105 if (!_dbus_message_data_load (data, filename))
3108 _dbus_string_get_const_data (filename, &s);
3109 _dbus_warn ("Could not load message file %s\n", s);
3122 * Tries loading the message in the given message file
3123 * and verifies that DBusMessageLoader can handle it.
3125 * @param filename filename to load
3126 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3127 * @param expected_validity what the message has to be like to return #TRUE
3128 * @returns #TRUE if the message has the expected validity
3131 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
3133 DBusMessageValidity expected_validity)
3140 if (!_dbus_string_init (&data, _DBUS_INT_MAX))
3141 _dbus_assert_not_reached ("could not allocate string\n");
3143 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
3147 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
3155 if (_dbus_string_get_length (&data) > 0)
3156 _dbus_verbose_bytes_of_string (&data, 0,
3157 _dbus_string_get_length (&data));
3159 _dbus_string_get_const_data (filename, &s);
3160 _dbus_warn ("Failed message loader test on %s\n",
3164 _dbus_string_free (&data);
3170 * Tries loading the given message data.
3173 * @param data the message data
3174 * @param expected_validity what the message has to be like to return #TRUE
3175 * @returns #TRUE if the message has the expected validity
3178 dbus_internal_do_not_use_try_message_data (const DBusString *data,
3179 DBusMessageValidity expected_validity)
3181 DBusMessageLoader *loader;
3189 /* Write the data one byte at a time */
3191 loader = _dbus_message_loader_new ();
3193 len = _dbus_string_get_length (data);
3194 for (i = 0; i < len; i++)
3198 _dbus_message_loader_get_buffer (loader, &buffer);
3199 _dbus_string_append_byte (buffer,
3200 _dbus_string_get_byte (data, i));
3201 _dbus_message_loader_return_buffer (loader, buffer, 1);
3204 if (!check_loader_results (loader, expected_validity))
3207 _dbus_message_loader_unref (loader);
3210 /* Write the data all at once */
3212 loader = _dbus_message_loader_new ();
3217 _dbus_message_loader_get_buffer (loader, &buffer);
3218 _dbus_string_copy (data, 0, buffer,
3219 _dbus_string_get_length (buffer));
3220 _dbus_message_loader_return_buffer (loader, buffer, 1);
3223 if (!check_loader_results (loader, expected_validity))
3226 _dbus_message_loader_unref (loader);
3229 /* Write the data 2 bytes at a time */
3231 loader = _dbus_message_loader_new ();
3233 len = _dbus_string_get_length (data);
3234 for (i = 0; i < len; i += 2)
3238 _dbus_message_loader_get_buffer (loader, &buffer);
3239 _dbus_string_append_byte (buffer,
3240 _dbus_string_get_byte (data, i));
3242 _dbus_string_append_byte (buffer,
3243 _dbus_string_get_byte (data, i+1));
3244 _dbus_message_loader_return_buffer (loader, buffer, 1);
3247 if (!check_loader_results (loader, expected_validity))
3250 _dbus_message_loader_unref (loader);
3258 _dbus_message_loader_unref (loader);
3264 process_test_subdir (const DBusString *test_base_dir,
3266 DBusMessageValidity validity,
3267 DBusForeachMessageFileFunc function,
3270 DBusString test_directory;
3271 DBusString filename;
3274 DBusResultCode result;
3279 if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
3280 _dbus_assert_not_reached ("didn't allocate test_directory\n");
3282 _dbus_string_init_const (&filename, subdir);
3284 if (!_dbus_string_copy (test_base_dir, 0,
3285 &test_directory, 0))
3286 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3288 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3289 _dbus_assert_not_reached ("couldn't allocate full path");
3291 _dbus_string_free (&filename);
3292 if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
3293 _dbus_assert_not_reached ("didn't allocate filename string\n");
3295 dir = _dbus_directory_open (&test_directory, &result);
3299 _dbus_string_get_const_data (&test_directory, &s);
3300 _dbus_warn ("Could not open %s: %s\n", s,
3301 dbus_result_to_string (result));
3305 printf ("Testing:\n");
3307 result = DBUS_RESULT_SUCCESS;
3309 while (_dbus_directory_get_next_file (dir, &filename, &result))
3311 DBusString full_path;
3314 if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
3315 _dbus_assert_not_reached ("couldn't init string");
3317 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3318 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3320 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3321 _dbus_assert_not_reached ("couldn't concat file to dir");
3323 if (_dbus_string_ends_with_c_str (&filename, ".message"))
3325 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
3329 const char *filename_c;
3330 _dbus_string_get_const_data (&filename, &filename_c);
3331 _dbus_verbose ("Skipping non-.message file %s\n",
3333 _dbus_string_free (&full_path);
3339 _dbus_string_get_const_data (&filename, &s);
3340 printf (" %s\n", s);
3343 _dbus_verbose (" expecting %s\n",
3344 validity == _DBUS_MESSAGE_VALID ? "valid" :
3345 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
3346 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
3348 if (! (*function) (&full_path, is_raw, validity, user_data))
3350 _dbus_string_free (&full_path);
3354 _dbus_string_free (&full_path);
3357 if (result != DBUS_RESULT_SUCCESS)
3360 _dbus_string_get_const_data (&test_directory, &s);
3361 _dbus_warn ("Could not get next file in %s: %s\n",
3362 s, dbus_result_to_string (result));
3371 _dbus_directory_close (dir);
3372 _dbus_string_free (&test_directory);
3373 _dbus_string_free (&filename);
3379 * Runs the given function on every message file in the test suite.
3380 * The function should return #FALSE on test failure or fatal error.
3382 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
3383 * @param func the function to run
3384 * @param user_data data for function
3385 * @returns #FALSE if there's a failure
3388 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
3389 DBusForeachMessageFileFunc func,
3392 DBusString test_directory;
3397 _dbus_string_init_const (&test_directory, test_data_dir);
3399 if (!process_test_subdir (&test_directory, "valid-messages",
3400 _DBUS_MESSAGE_VALID, func, user_data))
3403 if (!process_test_subdir (&test_directory, "invalid-messages",
3404 _DBUS_MESSAGE_INVALID, func, user_data))
3407 if (!process_test_subdir (&test_directory, "incomplete-messages",
3408 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
3415 _dbus_string_free (&test_directory);
3421 * @ingroup DBusMessageInternals
3422 * Unit test for DBusMessage.
3424 * @returns #TRUE on success.
3427 _dbus_message_test (const char *test_data_dir)
3429 DBusMessage *message;
3430 DBusMessageLoader *loader;
3433 dbus_int32_t our_int;
3436 dbus_bool_t our_bool;
3438 /* Test the vararg functions */
3439 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3440 _dbus_message_set_client_serial (message, 1);
3441 dbus_message_append_args (message,
3442 DBUS_TYPE_INT32, -0x12345678,
3443 DBUS_TYPE_STRING, "Test string",
3444 DBUS_TYPE_DOUBLE, 3.14159,
3445 DBUS_TYPE_BOOLEAN, TRUE,
3447 _dbus_verbose_bytes_of_string (&message->header, 0,
3448 _dbus_string_get_length (&message->header));
3449 _dbus_verbose_bytes_of_string (&message->body, 0,
3450 _dbus_string_get_length (&message->body));
3452 if (dbus_message_get_args (message,
3453 DBUS_TYPE_INT32, &our_int,
3454 DBUS_TYPE_STRING, &our_str,
3455 DBUS_TYPE_DOUBLE, &our_double,
3456 DBUS_TYPE_BOOLEAN, &our_bool,
3457 0) != DBUS_RESULT_SUCCESS)
3458 _dbus_assert_not_reached ("Could not get arguments");
3460 if (our_int != -0x12345678)
3461 _dbus_assert_not_reached ("integers differ!");
3463 if (our_double != 3.14159)
3464 _dbus_assert_not_reached ("doubles differ!");
3466 if (strcmp (our_str, "Test string") != 0)
3467 _dbus_assert_not_reached ("strings differ!");
3470 _dbus_assert_not_reached ("booleans differ");
3472 dbus_free (our_str);
3473 dbus_message_unref (message);
3475 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3476 _dbus_message_set_client_serial (message, 1);
3477 _dbus_message_set_reply_serial (message, 0x12345678);
3479 dbus_message_append_string (message, "Test string");
3480 dbus_message_append_int32 (message, -0x12345678);
3481 dbus_message_append_uint32 (message, 0xedd1e);
3482 dbus_message_append_double (message, 3.14159);
3484 message_iter_test (message);
3486 /* Message loader test */
3487 _dbus_message_lock (message);
3488 loader = _dbus_message_loader_new ();
3490 /* Write the header data one byte at a time */
3491 _dbus_string_get_const_data (&message->header, &data);
3492 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
3496 _dbus_message_loader_get_buffer (loader, &buffer);
3497 _dbus_string_append_byte (buffer, data[i]);
3498 _dbus_message_loader_return_buffer (loader, buffer, 1);
3501 /* Write the body data one byte at a time */
3502 _dbus_string_get_const_data (&message->body, &data);
3503 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
3507 _dbus_message_loader_get_buffer (loader, &buffer);
3508 _dbus_string_append_byte (buffer, data[i]);
3509 _dbus_message_loader_return_buffer (loader, buffer, 1);
3512 dbus_message_unref (message);
3514 /* Now pop back the message */
3515 if (_dbus_message_loader_get_is_corrupted (loader))
3516 _dbus_assert_not_reached ("message loader corrupted");
3518 message = _dbus_message_loader_pop_message (loader);
3520 _dbus_assert_not_reached ("received a NULL message");
3522 if (_dbus_message_get_reply_serial (message) != 0x12345678)
3523 _dbus_assert_not_reached ("reply serial fields differ");
3525 message_iter_test (message);
3527 dbus_message_unref (message);
3528 _dbus_message_loader_unref (loader);
3530 /* Now load every message in test_data_dir if we have one */
3531 if (test_data_dir == NULL)
3534 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
3535 (DBusForeachMessageFileFunc)
3536 dbus_internal_do_not_use_try_message_file,
3540 #endif /* DBUS_BUILD_TESTS */