1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-message.c DBusMessage object
4 * Copyright (C) 2002, 2003 Red Hat Inc.
5 * Copyright (C) 2002, 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 1.2
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-internals.h"
26 #include "dbus-marshal.h"
27 #include "dbus-message.h"
28 #include "dbus-message-internal.h"
29 #include "dbus-memory.h"
30 #include "dbus-list.h"
31 #include "dbus-message-builder.h"
35 * @defgroup DBusMessageInternals DBusMessage implementation details
36 * @ingroup DBusInternals
37 * @brief DBusMessage private implementation details.
39 * The guts of DBusMessage and its methods.
57 static dbus_bool_t field_is_named[FIELD_LAST] =
59 FALSE, /* FIELD_HEADER_LENGTH */
60 FALSE, /* FIELD_BODY_LENGTH */
61 FALSE, /* FIELD_CLIENT_SERIAL */
62 TRUE, /* FIELD_NAME */
63 TRUE, /* FIELD_SERVICE */
64 TRUE, /* FIELD_SENDER */
65 TRUE /* FIELD_REPLY_SERIAL */
70 int offset; /**< Offset to start of field (location of name of field
76 * @brief Internals of DBusMessage
78 * Object representing a message received from or to be sent to
79 * another application. This is an opaque object, all members
84 dbus_atomic_t refcount; /**< Reference count */
86 DBusString header; /**< Header network data, stored
87 * separately from body so we can
88 * independently realloc it.
91 HeaderField header_fields[FIELD_LAST]; /**< Track the location
92 * of each field in "header"
95 dbus_int32_t client_serial; /**< Cached client serial value for speed */
96 dbus_int32_t reply_serial; /**< Cached reply serial value for speed */
98 int header_padding; /**< bytes of alignment in header */
100 DBusString body; /**< Body network data. */
102 char byte_order; /**< Message byte order. */
104 DBusCounter *size_counter; /**< Counter for the size of the message, or #NULL */
105 long size_counter_delta; /**< Size we incremented the size counter by. */
107 unsigned int locked : 1; /**< Message being sent, no modifications allowed. */
111 * @brief Internals of DBusMessageIter
113 * Object representing a position in a message. All fields are internal.
115 struct DBusMessageIter
117 int refcount; /**< Reference count */
119 int pos; /**< Current position in the string */
121 DBusMessage *message; /**< Message used */
125 * Gets the data to be sent over the network for this message.
126 * The header and then the body should be written out.
127 * This function is guaranteed to always return the same
128 * data once a message is locked (with _dbus_message_lock()).
130 * @param message the message.
131 * @param header return location for message header data.
132 * @param body return location for message body data.
135 _dbus_message_get_network_data (DBusMessage *message,
136 const DBusString **header,
137 const DBusString **body)
139 _dbus_assert (message->locked);
141 *header = &message->header;
142 *body = &message->body;
146 clear_header_padding (DBusMessage *message)
148 _dbus_string_shorten (&message->header,
149 message->header_padding);
150 message->header_padding = 0;
154 append_header_padding (DBusMessage *message)
157 old_len = _dbus_string_get_length (&message->header);
158 if (!_dbus_string_align_length (&message->header, 8))
161 message->header_padding = _dbus_string_get_length (&message->header) - old_len;
167 adjust_field_offsets (DBusMessage *message,
177 while (i < FIELD_LAST)
179 if (message->header_fields[i].offset > offsets_after)
180 message->header_fields[i].offset += delta;
187 get_string_field (DBusMessage *message,
194 offset = message->header_fields[field].offset;
196 _dbus_assert (field < FIELD_LAST);
201 /* offset points to string length, string data follows it */
202 /* FIXME _dbus_demarshal_const_string() that returned
203 * a reference to the string plus its len might be nice.
207 *len = _dbus_demarshal_uint32 (&message->header,
212 _dbus_string_get_const_data (&message->header,
215 return data + (offset + 4);
219 get_int_field (DBusMessage *message,
224 _dbus_assert (field < FIELD_LAST);
226 offset = message->header_fields[field].offset;
229 return -1; /* useless if -1 is a valid value of course */
231 return _dbus_demarshal_int32 (&message->header,
238 append_int_field (DBusMessage *message,
245 _dbus_assert (!message->locked);
247 clear_header_padding (message);
249 orig_len = _dbus_string_get_length (&message->header);
251 if (!_dbus_string_align_length (&message->header, 4))
254 if (!_dbus_string_append_len (&message->header, name, 4))
257 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_INT32))
260 if (!_dbus_string_align_length (&message->header, 4))
263 message->header_fields[FIELD_REPLY_SERIAL].offset =
264 _dbus_string_get_length (&message->header);
266 if (!_dbus_marshal_int32 (&message->header, message->byte_order,
270 if (!append_header_padding (message))
276 message->header_fields[field].offset = -1;
277 _dbus_string_set_length (&message->header, orig_len);
279 /* this must succeed because it was allocated on function entry and
280 * DBusString doesn't ever realloc smaller
282 if (!append_header_padding (message))
283 _dbus_assert_not_reached ("failed to reappend header padding");
288 append_string_field (DBusMessage *message,
295 _dbus_assert (!message->locked);
297 clear_header_padding (message);
299 orig_len = _dbus_string_get_length (&message->header);
301 if (!_dbus_string_align_length (&message->header, 4))
304 if (!_dbus_string_append_len (&message->header, name, 4))
307 if (!_dbus_string_append_byte (&message->header, DBUS_TYPE_STRING))
310 if (!_dbus_string_align_length (&message->header, 4))
313 message->header_fields[field].offset =
314 _dbus_string_get_length (&message->header);
316 if (!_dbus_marshal_string (&message->header, message->byte_order,
320 if (!append_header_padding (message))
326 message->header_fields[field].offset = -1;
327 _dbus_string_set_length (&message->header, orig_len);
329 /* this must succeed because it was allocated on function entry and
330 * DBusString doesn't ever realloc smaller
332 if (!append_header_padding (message))
333 _dbus_assert_not_reached ("failed to reappend header padding");
339 delete_int_field (DBusMessage *message,
342 int offset = message->header_fields[field].offset;
344 _dbus_assert (!message->locked);
345 _dbus_assert (field_is_named[field]);
350 clear_header_padding (message);
352 /* The field typecode and name take up 8 bytes */
353 _dbus_string_delete (&message->header,
357 message->header_fields[field].offset = -1;
359 adjust_field_offsets (message,
363 append_header_padding (message);
367 delete_string_field (DBusMessage *message,
370 int offset = message->header_fields[field].offset;
374 _dbus_assert (!message->locked);
375 _dbus_assert (field_is_named[field]);
380 clear_header_padding (message);
382 get_string_field (message, field, &len);
384 /* The field typecode and name take up 8 bytes, and the nul
385 * termination is 1 bytes, string length integer is 4 bytes
387 delete_len = 8 + 4 + 1 + len;
389 _dbus_string_delete (&message->header,
393 message->header_fields[field].offset = -1;
395 adjust_field_offsets (message,
399 append_header_padding (message);
403 set_int_field (DBusMessage *message,
407 int offset = message->header_fields[field].offset;
409 _dbus_assert (!message->locked);
413 /* need to append the field */
417 case FIELD_REPLY_SERIAL:
418 return append_int_field (message, field,
419 DBUS_HEADER_FIELD_REPLY,
422 _dbus_assert_not_reached ("appending an int field we don't support appending");
428 _dbus_marshal_set_int32 (&message->header,
437 set_uint_field (DBusMessage *message,
441 int offset = message->header_fields[field].offset;
443 _dbus_assert (!message->locked);
447 /* need to append the field */
452 _dbus_assert_not_reached ("appending a uint field we don't support appending");
458 _dbus_marshal_set_uint32 (&message->header,
467 set_string_field (DBusMessage *message,
471 int offset = message->header_fields[field].offset;
473 _dbus_assert (!message->locked);
474 _dbus_assert (value != NULL);
478 /* need to append the field */
483 return append_string_field (message, field,
484 DBUS_HEADER_FIELD_SENDER,
487 _dbus_assert_not_reached ("appending a string field we don't support appending");
498 clear_header_padding (message);
500 old_len = _dbus_string_get_length (&message->header);
502 len = strlen (value);
504 _dbus_string_init_const_len (&v, value,
505 len + 1); /* include nul */
506 if (!_dbus_marshal_set_string (&message->header,
512 new_len = _dbus_string_get_length (&message->header);
514 adjust_field_offsets (message,
518 if (!append_header_padding (message))
524 /* this must succeed because it was allocated on function entry and
525 * DBusString doesn't ever realloc smaller
527 if (!append_header_padding (message))
528 _dbus_assert_not_reached ("failed to reappend header padding");
535 * Sets the serial number of a message.
536 * This can only be done once on a message.
538 * @param message the message
539 * @param serial the serial
542 _dbus_message_set_serial (DBusMessage *message,
545 _dbus_assert (!message->locked);
546 _dbus_assert (dbus_message_get_serial (message) < 0);
548 set_int_field (message, FIELD_CLIENT_SERIAL,
550 message->client_serial = serial;
554 * Sets the reply serial of a message (the client serial
555 * of the message this is a reply to).
557 * @param message the message
558 * @param reply_serial the client serial
559 * @returns #FALSE if not enough memory
562 dbus_message_set_reply_serial (DBusMessage *message,
563 dbus_int32_t reply_serial)
565 _dbus_assert (!message->locked);
567 if (set_int_field (message, FIELD_REPLY_SERIAL,
570 message->reply_serial = reply_serial;
578 * Returns the serial of a message or -1 if none has been specified.
579 * The message's serial number is provided by the application sending
580 * the message and is used to identify replies to this message.
582 * @param message the message
583 * @returns the client serial
586 dbus_message_get_serial (DBusMessage *message)
588 return message->client_serial;
592 * Returns the serial that the message is
593 * a reply to or -1 if none.
595 * @param message the message
596 * @returns the reply serial
599 dbus_message_get_reply_serial (DBusMessage *message)
601 return message->reply_serial;
605 * Adds a counter to be incremented immediately with the
606 * size of this message, and decremented by the size
607 * of this message when this message if finalized.
609 * @param message the message
610 * @param counter the counter
613 _dbus_message_add_size_counter (DBusMessage *message,
614 DBusCounter *counter)
616 _dbus_assert (message->size_counter == NULL); /* If this fails we may need to keep a list of
617 * counters instead of just one
620 message->size_counter = counter;
621 _dbus_counter_ref (message->size_counter);
623 /* When we can change message size, we may want to
624 * update this each time we do so, or we may want to
625 * just KISS like this.
627 message->size_counter_delta =
628 _dbus_string_get_length (&message->header) +
629 _dbus_string_get_length (&message->body);
632 _dbus_verbose ("message has size %ld\n",
633 message->size_counter_delta);
636 _dbus_counter_adjust (message->size_counter, message->size_counter_delta);
640 dbus_message_create_header (DBusMessage *message,
646 if (!_dbus_string_append_byte (&message->header, message->byte_order))
650 if (!_dbus_string_append_byte (&message->header, flags))
653 if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
656 if (!_dbus_string_append_byte (&message->header, 0))
659 message->header_fields[FIELD_HEADER_LENGTH].offset = 4;
660 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
663 message->header_fields[FIELD_BODY_LENGTH].offset = 8;
664 if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
667 message->header_fields[FIELD_CLIENT_SERIAL].offset = 12;
668 if (!_dbus_marshal_int32 (&message->header, message->byte_order, -1))
671 /* Marshal message service */
674 if (!append_string_field (message,
676 DBUS_HEADER_FIELD_SERVICE,
681 _dbus_assert (name != NULL);
682 if (!append_string_field (message,
684 DBUS_HEADER_FIELD_NAME,
692 * Locks a message. Allows checking that applications don't keep a
693 * reference to a message in the outgoing queue and change it
694 * underneath us. Messages are locked when they enter the outgoing
695 * queue (dbus_connection_send_message()), and the library complains
696 * if the message is modified while locked.
698 * @param message the message to lock.
701 _dbus_message_lock (DBusMessage *message)
703 if (!message->locked)
705 /* Fill in our lengths */
706 set_uint_field (message,
708 _dbus_string_get_length (&message->header));
710 set_uint_field (message,
712 _dbus_string_get_length (&message->body));
714 message->locked = TRUE;
721 * @defgroup DBusMessage DBusMessage
723 * @brief Message to be sent or received over a DBusConnection.
725 * A DBusMessage is the most basic unit of communication over a
726 * DBusConnection. A DBusConnection represents a stream of messages
727 * received from a remote application, and a stream of messages
728 * sent to a remote application.
734 * @typedef DBusMessage
736 * Opaque data type representing a message received from or to be
737 * sent to another application.
741 dbus_message_new_empty_header (void)
743 DBusMessage *message;
746 message = dbus_new0 (DBusMessage, 1);
750 message->refcount = 1;
751 message->byte_order = DBUS_COMPILER_BYTE_ORDER;
752 message->client_serial = -1;
753 message->reply_serial = -1;
756 while (i < FIELD_LAST)
758 message->header_fields[i].offset = -1;
762 if (!_dbus_string_init (&message->header, _DBUS_INT_MAX))
768 if (!_dbus_string_init (&message->body, _DBUS_INT_MAX))
770 _dbus_string_free (&message->header);
780 * Constructs a new message. Returns #NULL if memory can't be
781 * allocated for the message. The service may be #NULL in which case
782 * no service is set; this is appropriate when using D-BUS in a
783 * peer-to-peer context (no message bus).
785 * @todo reverse the arguments, first 'name' then 'service'
786 * as 'name' is more fundamental
788 * @param service service that the message should be sent to or #NULL
789 * @param name name of the message
790 * @returns a new DBusMessage, free with dbus_message_unref()
791 * @see dbus_message_unref()
794 dbus_message_new (const char *service,
797 DBusMessage *message;
799 message = dbus_message_new_empty_header ();
803 if (!dbus_message_create_header (message, service, name))
805 dbus_message_unref (message);
813 * Constructs a message that is a reply to some other
814 * message. Returns #NULL if memory can't be allocated
817 * @param original_message the message which the created
818 * message is a reply to.
819 * @returns a new DBusMessage, free with dbus_message_unref()
820 * @see dbus_message_new(), dbus_message_unref()
823 dbus_message_new_reply (DBusMessage *original_message)
825 DBusMessage *message;
826 const char *sender, *name;
828 sender = get_string_field (original_message,
830 name = get_string_field (original_message,
833 /* sender is allowed to be null here in peer-to-peer case */
835 message = dbus_message_new (sender, name);
840 if (!dbus_message_set_reply_serial (message,
841 dbus_message_get_serial (original_message)))
843 dbus_message_unref (message);
851 * Creates a new message that is an error reply to a certain message.
853 * @param original_message the original message
854 * @param error_name the error name
855 * @param error_message the error message string
856 * @returns a new error message
859 dbus_message_new_error_reply (DBusMessage *original_message,
860 const char *error_name,
861 const char *error_message)
863 DBusMessage *message;
866 sender = get_string_field (original_message,
869 _dbus_assert (sender != NULL);
871 message = dbus_message_new (sender, error_name);
876 if (!dbus_message_set_reply_serial (message,
877 dbus_message_get_serial (original_message)))
879 dbus_message_unref (message);
883 if (!dbus_message_append_string (message, error_message))
885 dbus_message_unref (message);
889 dbus_message_set_is_error (message, TRUE);
895 * Creates a new message that is an exact replica of the message
896 * specified, except that its refcount is set to 1.
898 * @param message the message.
899 * @returns the new message.
902 dbus_message_copy (const DBusMessage *message)
907 retval = dbus_new0 (DBusMessage, 1);
911 retval->refcount = 1;
912 retval->byte_order = message->byte_order;
913 retval->client_serial = message->client_serial;
914 retval->reply_serial = message->reply_serial;
915 retval->header_padding = message->header_padding;
916 retval->locked = FALSE;
918 if (!_dbus_string_init (&retval->header, _DBUS_INT_MAX))
924 if (!_dbus_string_init (&retval->body, _DBUS_INT_MAX))
926 _dbus_string_free (&retval->header);
931 if (!_dbus_string_copy (&message->header, 0,
934 _dbus_string_free (&retval->header);
935 _dbus_string_free (&retval->body);
941 if (!_dbus_string_copy (&message->body, 0,
944 _dbus_string_free (&retval->header);
945 _dbus_string_free (&retval->body);
951 for (i = 0; i < FIELD_LAST; i++)
953 retval->header_fields[i].offset = message->header_fields[i].offset;
961 * Increments the reference count of a DBusMessage.
963 * @param message The message
964 * @see dbus_message_unref
967 dbus_message_ref (DBusMessage *message)
969 dbus_atomic_t refcount;
971 refcount = _dbus_atomic_inc (&message->refcount);
972 _dbus_assert (refcount > 1);
976 * Decrements the reference count of a DBusMessage.
978 * @param message The message
979 * @see dbus_message_ref
982 dbus_message_unref (DBusMessage *message)
984 dbus_atomic_t refcount;
986 refcount = _dbus_atomic_dec (&message->refcount);
988 _dbus_assert (refcount >= 0);
992 if (message->size_counter != NULL)
994 _dbus_counter_adjust (message->size_counter,
995 - message->size_counter_delta);
996 _dbus_counter_unref (message->size_counter);
999 _dbus_string_free (&message->header);
1000 _dbus_string_free (&message->body);
1002 dbus_free (message);
1007 * Gets the name of a message.
1009 * @param message the message
1010 * @returns the message name (should not be freed)
1013 dbus_message_get_name (DBusMessage *message)
1015 return get_string_field (message, FIELD_NAME, NULL);
1019 * Gets the destination service of a message.
1021 * @param message the message
1022 * @returns the message destination service (should not be freed)
1025 dbus_message_get_service (DBusMessage *message)
1027 return get_string_field (message, FIELD_SERVICE, NULL);
1031 * Appends fields to a message given a variable argument
1032 * list. The variable argument list should contain the type
1033 * of the argument followed by the value to add. Array values
1034 * are specified by a pointer to the array followed by an int
1035 * giving the length of the array. The list is terminated
1038 * @param message the message
1039 * @param first_arg_type type of the first argument
1040 * @param ... value of first argument, list of additional type-value pairs
1041 * @returns #TRUE on success
1044 dbus_message_append_args (DBusMessage *message,
1051 va_start (var_args, first_arg_type);
1052 retval = dbus_message_append_args_valist (message,
1061 * This function takes a va_list for use by language bindings
1063 * @see dbus_message_append_args.
1064 * @param message the message
1065 * @param first_arg_type type of first argument
1066 * @param var_args value of first argument, then list of type/value pairs
1067 * @returns #TRUE on success
1070 dbus_message_append_args_valist (DBusMessage *message,
1076 old_len = _dbus_string_get_length (&message->body);
1078 type = first_arg_type;
1085 if (!dbus_message_append_nil (message))
1087 case DBUS_TYPE_BOOLEAN:
1088 if (!dbus_message_append_boolean (message, va_arg (var_args, dbus_bool_t)))
1091 case DBUS_TYPE_INT32:
1092 if (!dbus_message_append_int32 (message, va_arg (var_args, dbus_int32_t)))
1095 case DBUS_TYPE_UINT32:
1096 if (!dbus_message_append_uint32 (message, va_arg (var_args, dbus_uint32_t)))
1099 case DBUS_TYPE_DOUBLE:
1100 if (!dbus_message_append_double (message, va_arg (var_args, double)))
1103 case DBUS_TYPE_STRING:
1104 if (!dbus_message_append_string (message, va_arg (var_args, const char *)))
1107 case DBUS_TYPE_BOOLEAN_ARRAY:
1110 unsigned char *data;
1112 data = va_arg (var_args, unsigned char *);
1113 len = va_arg (var_args, int);
1115 if (!dbus_message_append_boolean_array (message, data, len))
1119 case DBUS_TYPE_INT32_ARRAY:
1124 data = va_arg (var_args, dbus_int32_t *);
1125 len = va_arg (var_args, int);
1127 if (!dbus_message_append_int32_array (message, data, len))
1131 case DBUS_TYPE_UINT32_ARRAY:
1134 dbus_uint32_t *data;
1136 data = va_arg (var_args, dbus_uint32_t *);
1137 len = va_arg (var_args, int);
1139 if (!dbus_message_append_uint32_array (message, data, len))
1143 case DBUS_TYPE_DOUBLE_ARRAY:
1148 data = va_arg (var_args, double *);
1149 len = va_arg (var_args, int);
1151 if (!dbus_message_append_double_array (message, data, len))
1155 case DBUS_TYPE_BYTE_ARRAY:
1158 unsigned char *data;
1160 data = va_arg (var_args, unsigned char *);
1161 len = va_arg (var_args, int);
1163 if (!dbus_message_append_byte_array (message, data, len))
1167 case DBUS_TYPE_STRING_ARRAY:
1172 data = va_arg (var_args, const char **);
1173 len = va_arg (var_args, int);
1175 if (!dbus_message_append_string_array (message, data, len))
1179 case DBUS_TYPE_DICT:
1183 dict = va_arg (var_args, DBusDict *);
1185 if (!dbus_message_append_dict (message, dict))
1190 _dbus_warn ("Unknown field type %d\n", type);
1193 type = va_arg (var_args, int);
1203 * Appends a nil value to the message
1205 * @param message the message
1206 * @returns #TRUE on success
1209 dbus_message_append_nil (DBusMessage *message)
1211 _dbus_assert (!message->locked);
1213 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_NIL))
1220 * Appends a boolean value to the message
1222 * @param message the message
1223 * @param value the boolean value
1224 * @returns #TRUE on success
1227 dbus_message_append_boolean (DBusMessage *message,
1230 _dbus_assert (!message->locked);
1232 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN))
1235 if (!_dbus_string_append_byte (&message->body, (value != FALSE)))
1237 _dbus_string_shorten (&message->body, 1);
1245 * Appends a 32 bit signed integer to the message.
1247 * @param message the message
1248 * @param value the integer value
1249 * @returns #TRUE on success
1252 dbus_message_append_int32 (DBusMessage *message,
1255 _dbus_assert (!message->locked);
1257 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32))
1260 if (!_dbus_marshal_int32 (&message->body, message->byte_order, value))
1262 _dbus_string_shorten (&message->body, 1);
1270 * Appends a 32 bit unsigned integer to the message.
1272 * @param message the message
1273 * @param value the integer value
1274 * @returns #TRUE on success
1277 dbus_message_append_uint32 (DBusMessage *message,
1278 dbus_uint32_t value)
1280 _dbus_assert (!message->locked);
1282 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32))
1285 if (!_dbus_marshal_uint32 (&message->body, message->byte_order, value))
1287 _dbus_string_shorten (&message->body, 1);
1295 * Appends a double value to the message.
1297 * @param message the message
1298 * @param value the double value
1299 * @returns #TRUE on success
1302 dbus_message_append_double (DBusMessage *message,
1305 _dbus_assert (!message->locked);
1307 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE))
1310 if (!_dbus_marshal_double (&message->body, message->byte_order, value))
1312 _dbus_string_shorten (&message->body, 1);
1320 * Appends a UTF-8 string to the message.
1322 * @param message the message
1323 * @param value the string
1324 * @returns #TRUE on success
1327 dbus_message_append_string (DBusMessage *message,
1330 _dbus_assert (!message->locked);
1332 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING))
1335 if (!_dbus_marshal_string (&message->body, message->byte_order, value))
1337 _dbus_string_shorten (&message->body, 1);
1345 * Appends a boolean array to the message.
1347 * @param message the message
1348 * @param value the array
1349 * @param len the length of the array
1350 * @returns #TRUE on success
1353 dbus_message_append_boolean_array (DBusMessage *message,
1354 unsigned const char *value,
1357 _dbus_assert (!message->locked);
1359 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN_ARRAY))
1362 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1364 _dbus_string_shorten (&message->body, 1);
1372 * Appends a 32 bit signed integer array to the message.
1374 * @param message the message
1375 * @param value the array
1376 * @param len the length of the array
1377 * @returns #TRUE on success
1380 dbus_message_append_int32_array (DBusMessage *message,
1381 const dbus_int32_t *value,
1384 _dbus_assert (!message->locked);
1386 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32_ARRAY))
1389 if (!_dbus_marshal_int32_array (&message->body, message->byte_order,
1392 _dbus_string_shorten (&message->body, 1);
1400 * Appends a 32 bit unsigned integer array to the message.
1402 * @param message the message
1403 * @param value the array
1404 * @param len the length of the array
1405 * @returns #TRUE on success
1408 dbus_message_append_uint32_array (DBusMessage *message,
1409 const dbus_uint32_t *value,
1412 _dbus_assert (!message->locked);
1414 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32_ARRAY))
1417 if (!_dbus_marshal_uint32_array (&message->body, message->byte_order,
1420 _dbus_string_shorten (&message->body, 1);
1428 * Appends a double array to the message.
1430 * @param message the message
1431 * @param value the array
1432 * @param len the length of the array
1433 * @returns #TRUE on success
1436 dbus_message_append_double_array (DBusMessage *message,
1437 const double *value,
1440 _dbus_assert (!message->locked);
1442 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE_ARRAY))
1445 if (!_dbus_marshal_double_array (&message->body, message->byte_order,
1448 _dbus_string_shorten (&message->body, 1);
1456 * Appends a byte array to the message.
1458 * @param message the message
1459 * @param value the array
1460 * @param len the length of the array
1461 * @returns #TRUE on success
1464 dbus_message_append_byte_array (DBusMessage *message,
1465 unsigned const char *value,
1468 _dbus_assert (!message->locked);
1470 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY))
1473 if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
1475 _dbus_string_shorten (&message->body, 1);
1483 * Appends a string array to the message.
1485 * @param message the message
1486 * @param value the array
1487 * @param len the length of the array
1488 * @returns #TRUE on success
1491 dbus_message_append_string_array (DBusMessage *message,
1495 _dbus_assert (!message->locked);
1497 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING_ARRAY))
1500 if (!_dbus_marshal_string_array (&message->body, message->byte_order,
1501 (const char **)value, len))
1503 _dbus_string_shorten (&message->body, 1);
1511 * Appends a dict to the message.
1513 * @param message the message
1514 * @param dict the dict
1515 * @returns #TRUE on success
1518 dbus_message_append_dict (DBusMessage *message,
1521 _dbus_assert (!message->locked);
1523 if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DICT))
1526 if (!_dbus_marshal_dict (&message->body, message->byte_order, dict))
1528 _dbus_string_shorten (&message->body, 1);
1536 * Gets arguments from a message given a variable argument list.
1537 * The variable argument list should contain the type of the
1538 * argumen followed by a pointer to where the value should be
1539 * stored. The list is terminated with 0.
1541 * @param message the message
1542 * @param error error to be filled in on failure
1543 * @param first_arg_type the first argument type
1544 * @param ... location for first argument value, then list of type-location pairs
1545 * @returns #FALSE if the error was set
1548 dbus_message_get_args (DBusMessage *message,
1553 DBusResultCode retval;
1556 va_start (var_args, first_arg_type);
1557 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
1564 * This function takes a va_list for use by language bindings
1566 * @todo this function (or some lower-level non-convenience function)
1567 * needs better error handling; should allow the application to
1568 * distinguish between out of memory, and bad data from the remote
1569 * app. It also needs to not leak a bunch of args when it gets
1570 * to the arg that's bad, as that would be a security hole
1571 * (allow one app to force another to leak memory)
1573 * @todo We need to free the argument data when an error occurs.
1575 * @see dbus_message_get_args
1576 * @param message the message
1577 * @param error error to be filled in
1578 * @param first_arg_type type of the first argument
1579 * @param var_args return location for first argument, followed by list of type/location pairs
1580 * @returns #FALSE if error was set
1583 dbus_message_get_args_valist (DBusMessage *message,
1588 int spec_type, msg_type, i;
1589 DBusMessageIter *iter;
1592 iter = dbus_message_get_args_iter (message);
1596 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1597 "No memory to get message arguments");
1603 spec_type = first_arg_type;
1606 while (spec_type != 0)
1608 msg_type = dbus_message_iter_get_arg_type (iter);
1610 if (msg_type != spec_type)
1612 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1613 "Argument %d is specified to be of type \"%s\", but "
1614 "is actually of type \"%s\"\n", i,
1615 _dbus_type_to_string (spec_type),
1616 _dbus_type_to_string (msg_type));
1625 case DBUS_TYPE_BOOLEAN:
1629 ptr = va_arg (var_args, dbus_bool_t *);
1631 *ptr = dbus_message_iter_get_boolean (iter);
1634 case DBUS_TYPE_INT32:
1638 ptr = va_arg (var_args, dbus_int32_t *);
1640 *ptr = dbus_message_iter_get_int32 (iter);
1643 case DBUS_TYPE_UINT32:
1647 ptr = va_arg (var_args, dbus_uint32_t *);
1649 *ptr = dbus_message_iter_get_uint32 (iter);
1653 case DBUS_TYPE_DOUBLE:
1657 ptr = va_arg (var_args, double *);
1659 *ptr = dbus_message_iter_get_double (iter);
1663 case DBUS_TYPE_STRING:
1667 ptr = va_arg (var_args, char **);
1669 *ptr = dbus_message_iter_get_string (iter);
1673 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1674 "No memory for argument %d", i);
1681 case DBUS_TYPE_BOOLEAN_ARRAY:
1683 unsigned char **ptr;
1686 ptr = va_arg (var_args, unsigned char **);
1687 len = va_arg (var_args, int *);
1689 if (!dbus_message_iter_get_boolean_array (iter, ptr, len))
1691 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1692 "No memory for argument %d", i);
1698 case DBUS_TYPE_INT32_ARRAY:
1703 ptr = va_arg (var_args, dbus_int32_t **);
1704 len = va_arg (var_args, int *);
1706 if (!dbus_message_iter_get_int32_array (iter, ptr, len))
1708 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1709 "No memory for argument %d", i);
1716 case DBUS_TYPE_UINT32_ARRAY:
1718 dbus_uint32_t **ptr;
1721 ptr = va_arg (var_args, dbus_uint32_t **);
1722 len = va_arg (var_args, int *);
1724 if (!dbus_message_iter_get_uint32_array (iter, ptr, len))
1726 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1727 "No memory for argument %d", i);
1734 case DBUS_TYPE_DOUBLE_ARRAY:
1739 ptr = va_arg (var_args, double **);
1740 len = va_arg (var_args, int *);
1742 if (!dbus_message_iter_get_double_array (iter, ptr, len))
1744 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1745 "No memory for argument %d", i);
1751 case DBUS_TYPE_BYTE_ARRAY:
1753 unsigned char **ptr;
1756 ptr = va_arg (var_args, unsigned char **);
1757 len = va_arg (var_args, int *);
1759 if (!dbus_message_iter_get_byte_array (iter, ptr, len))
1761 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1762 "No memory for argument %d", i);
1767 case DBUS_TYPE_STRING_ARRAY:
1772 ptr = va_arg (var_args, char ***);
1773 len = va_arg (var_args, int *);
1775 if (!dbus_message_iter_get_string_array (iter, ptr, len))
1777 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1778 "No memory for argument %d", i);
1783 case DBUS_TYPE_DICT:
1787 dict = va_arg (var_args, DBusDict **);
1789 if (!dbus_message_iter_get_dict (iter, dict))
1791 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1792 "No memory for argument %d", i);
1798 _dbus_warn ("Unknown field type %d\n", spec_type);
1801 spec_type = va_arg (var_args, int);
1802 if (spec_type != 0 && !dbus_message_iter_next (iter))
1804 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1805 "Message has only %d arguments, but more were expected", i);
1815 dbus_message_iter_unref (iter);
1820 * Returns a DBusMessageIter representing the arguments of the
1821 * message passed in.
1823 * @todo IMO the message iter should follow the GtkTextIter pattern,
1824 * a static object with a "stamp" value used to detect invalid
1825 * iter uses (uninitialized or after changing the message).
1826 * ref/unref is kind of annoying to deal with, and slower too.
1827 * This implies not ref'ing the message from the iter.
1829 * @todo I'd also name this dbus_message_iter_new() or
1830 * for the static object dbus_message_iter_init() rather
1831 * than making it a method on the message
1833 * @param message the message
1834 * @returns a new iter.
1837 dbus_message_get_args_iter (DBusMessage *message)
1839 DBusMessageIter *iter;
1841 iter = dbus_new (DBusMessageIter, 1);
1845 dbus_message_ref (message);
1848 iter->message = message;
1856 * Increments the reference count of a DBusMessageIter.
1858 * @param iter the message iter
1859 * @see dbus_message_iter_unref
1862 dbus_message_iter_ref (DBusMessageIter *iter)
1864 _dbus_assert (iter->refcount > 0);
1866 iter->refcount += 1;
1870 * Decrements the reference count of a DBusMessageIter.
1872 * @param iter The message iter
1873 * @see dbus_message_iter_ref
1876 dbus_message_iter_unref (DBusMessageIter *iter)
1878 _dbus_assert (iter->refcount > 0);
1880 iter->refcount -= 1;
1882 if (iter->refcount == 0)
1884 dbus_message_unref (iter->message);
1891 * Checks if an iterator has any more fields.
1893 * @param iter the message iter
1894 * @returns #TRUE if there are more fields
1898 dbus_message_iter_has_next (DBusMessageIter *iter)
1902 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1903 iter->message->byte_order,
1904 iter->pos, &end_pos))
1907 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1914 * Moves the iterator to the next field.
1916 * @param iter The message iter
1917 * @returns #TRUE if the iterator was moved to the next field
1920 dbus_message_iter_next (DBusMessageIter *iter)
1924 if (!_dbus_marshal_get_arg_end_pos (&iter->message->body,
1925 iter->message->byte_order,
1926 iter->pos, &end_pos))
1929 if (end_pos >= _dbus_string_get_length (&iter->message->body))
1932 iter->pos = end_pos;
1938 * Returns the argument type of the argument that the
1939 * message iterator points at.
1941 * @param iter the message iter
1942 * @returns the field type
1945 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
1949 if (iter->pos >= _dbus_string_get_length (&iter->message->body))
1950 return DBUS_TYPE_INVALID;
1952 _dbus_string_get_const_data_len (&iter->message->body, &data, iter->pos, 1);
1954 if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_DICT)
1957 return DBUS_TYPE_INVALID;
1961 * Returns the string value that an iterator may point to.
1962 * Note that you need to check that the iterator points to
1963 * a string value before using this function.
1965 * @see dbus_message_iter_get_arg_type
1966 * @param iter the message iter
1967 * @returns the string
1970 dbus_message_iter_get_string (DBusMessageIter *iter)
1972 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING);
1974 return _dbus_demarshal_string (&iter->message->body, iter->message->byte_order,
1975 iter->pos + 1, NULL);
1979 * Returns the boolean value that an iterator may point to.
1980 * Note that you need to check that the iterator points to
1981 * a boolean value before using this function.
1983 * @see dbus_message_iter_get_arg_type
1984 * @param iter the message iter
1985 * @returns the string
1988 dbus_message_iter_get_boolean (DBusMessageIter *iter)
1990 unsigned char value;
1992 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN);
1994 value = _dbus_string_get_byte (&iter->message->body, iter->pos + 1);
2000 * Returns the 32 bit signed integer value that an iterator may point to.
2001 * Note that you need to check that the iterator points to
2002 * an integer value before using this function.
2004 * @see dbus_message_iter_get_arg_type
2005 * @param iter the message iter
2006 * @returns the integer
2009 dbus_message_iter_get_int32 (DBusMessageIter *iter)
2011 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32);
2013 return _dbus_demarshal_int32 (&iter->message->body, iter->message->byte_order,
2014 iter->pos + 1, NULL);
2018 * Returns the 32 bit unsigned integer value that an iterator may point to.
2019 * Note that you need to check that the iterator points to
2020 * an unsigned integer value before using this function.
2022 * @see dbus_message_iter_get_arg_type
2023 * @param iter the message iter
2024 * @returns the integer
2027 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
2029 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32);
2031 return _dbus_demarshal_uint32 (&iter->message->body, iter->message->byte_order,
2032 iter->pos + 1, NULL);
2036 * Returns the double value that an iterator may point to.
2037 * Note that you need to check that the iterator points to
2038 * a string value before using this function.
2040 * @see dbus_message_iter_get_arg_type
2041 * @param iter the message iter
2042 * @returns the double
2045 dbus_message_iter_get_double (DBusMessageIter *iter)
2047 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE);
2049 return _dbus_demarshal_double (&iter->message->body, iter->message->byte_order,
2050 iter->pos + 1, NULL);
2054 * Returns the boolean array that the iterator may point to. Note that
2055 * you need to check that the iterator points to an array of the
2056 * correct type prior to using this function.
2058 * @param iter the iterator
2059 * @param value return location for the array
2060 * @param len return location for the array length
2061 * @returns #TRUE on success
2064 dbus_message_iter_get_boolean_array (DBusMessageIter *iter,
2065 unsigned char **value,
2068 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN_ARRAY);
2070 if (!_dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2071 iter->pos + 1, NULL, value, len))
2078 * Returns the 32 bit signed integer array that the iterator may point
2079 * to. Note that you need to check that the iterator points to an
2080 * array of the correct type prior to using this function.
2082 * @param iter the iterator
2083 * @param value return location for the array
2084 * @param len return location for the array length
2085 * @returns #TRUE on success
2088 dbus_message_iter_get_int32_array (DBusMessageIter *iter,
2089 dbus_int32_t **value,
2092 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32_ARRAY);
2094 if (!_dbus_demarshal_int32_array (&iter->message->body, iter->message->byte_order,
2095 iter->pos + 1, NULL, value, len))
2102 * Returns the 32 bit unsigned integer array that the iterator may point
2103 * to. Note that you need to check that the iterator points to an
2104 * array of the correct type prior to using this function.
2106 * @param iter the iterator
2107 * @param value return location for the array
2108 * @param len return location for the array length
2109 * @returns #TRUE on success
2112 dbus_message_iter_get_uint32_array (DBusMessageIter *iter,
2113 dbus_uint32_t **value,
2116 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32_ARRAY);
2118 if (!_dbus_demarshal_uint32_array (&iter->message->body, iter->message->byte_order,
2119 iter->pos + 1, NULL, value, len))
2126 * Returns the double array that the iterator may point to. Note that
2127 * you need to check that the iterator points to an array of the
2128 * correct type prior to using this function.
2130 * @param iter the iterator
2131 * @param value return location for the array
2132 * @param len return location for the array length
2133 * @returns #TRUE on success
2136 dbus_message_iter_get_double_array (DBusMessageIter *iter,
2140 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE_ARRAY);
2142 if (!_dbus_demarshal_double_array (&iter->message->body, iter->message->byte_order,
2143 iter->pos + 1, NULL, value, len))
2150 * Returns the byte array that the iterator may point to.
2151 * Note that you need to check that the iterator points
2152 * to a byte array prior to using this function.
2154 * @param iter the iterator
2155 * @param value return location for array values
2156 * @param len return location for length of byte array
2157 * @returns #TRUE on success
2160 dbus_message_iter_get_byte_array (DBusMessageIter *iter,
2161 unsigned char **value,
2164 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BYTE_ARRAY);
2166 if (!_dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
2167 iter->pos + 1, NULL, value, len))
2174 * Returns the string array that the iterator may point to.
2175 * Note that you need to check that the iterator points
2176 * to a byte array prior to using this function.
2178 * The returned value is a #NULL-terminated array of strings.
2179 * Each string is a separate malloc block, and the array
2180 * itself is a malloc block. You can free this type of
2181 * string array with dbus_free_string_array().
2183 * @param iter the iterator
2184 * @param value return location for string values
2185 * @param len return location for length of byte array
2186 * @returns #TRUE on success
2189 dbus_message_iter_get_string_array (DBusMessageIter *iter,
2193 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING_ARRAY);
2195 if (!_dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
2196 iter->pos + 1, NULL, value, len))
2203 * Returns the dict that the iterator may point to.
2204 * Note that you need to check that the iterator points
2205 * to a dict prior to using this function.
2207 * @param iter the iterator
2208 * @param dict return location for dict
2209 * @returns #TRUE on success
2212 dbus_message_iter_get_dict (DBusMessageIter *iter,
2215 _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DICT);
2217 if (!_dbus_demarshal_dict (&iter->message->body, iter->message->byte_order,
2218 iter->pos + 1, NULL, dict))
2225 * Sets the message sender.
2227 * @param message the message
2228 * @param sender the sender
2229 * @returns #FALSE if not enough memory
2232 dbus_message_set_sender (DBusMessage *message,
2235 _dbus_assert (!message->locked);
2239 delete_string_field (message, FIELD_SENDER);
2244 return set_string_field (message,
2251 * Sets a flag indicating that the message is an error reply
2252 * message, i.e. an "exception" rather than a normal response.
2254 * @param message the message
2255 * @param is_error_reply #TRUE if this is an error message.
2258 dbus_message_set_is_error (DBusMessage *message,
2259 dbus_bool_t is_error_reply)
2263 _dbus_assert (!message->locked);
2265 _dbus_string_get_data_len (&message->header, &header, 1, 1);
2268 *header |= DBUS_HEADER_FLAG_ERROR;
2270 *header &= ~DBUS_HEADER_FLAG_ERROR;
2274 * Returns #TRUE if the message is an error
2275 * reply to some previous message we sent.
2277 * @param message the message
2278 * @returns #TRUE if the message is an error
2281 dbus_message_get_is_error (DBusMessage *message)
2285 _dbus_string_get_const_data_len (&message->header, &header, 1, 1);
2287 return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
2291 * Gets the service which originated this message,
2292 * or #NULL if unknown or inapplicable.
2294 * @param message the message
2295 * @returns the service name or #NULL
2298 dbus_message_get_sender (DBusMessage *message)
2300 return get_string_field (message, FIELD_SENDER, NULL);
2304 * Checks whether the message has the given name.
2305 * If the message has no name or has a different
2306 * name, returns #FALSE.
2308 * @param message the message
2309 * @param name the name to check (must not be #NULL)
2311 * @returns #TRUE if the message has the given name
2314 dbus_message_name_is (DBusMessage *message,
2319 _dbus_assert (name != NULL);
2321 n = dbus_message_get_name (message);
2323 if (n && strcmp (n, name) == 0)
2332 * @addtogroup DBusMessageInternals
2337 * @typedef DBusMessageLoader
2339 * The DBusMessageLoader object encapsulates the process of converting
2340 * a byte stream into a series of DBusMessage. It buffers the incoming
2341 * bytes as efficiently as possible, and generates a queue of
2342 * messages. DBusMessageLoader is typically used as part of a
2343 * DBusTransport implementation. The DBusTransport then hands off
2344 * the loaded messages to a DBusConnection, making the messages
2345 * visible to the application.
2347 * @todo write tests for break-loader that a) randomly delete header
2348 * fields and b) set string fields to zero-length and other funky
2353 /* we definitely use signed ints for sizes, so don't exceed
2354 * _DBUS_INT_MAX; and add 16 for paranoia, since a message
2355 * over 128M is pretty nuts anyhow.
2359 * The maximum sane message size.
2361 #define MAX_SANE_MESSAGE_SIZE (_DBUS_INT_MAX/16)
2364 * Implementation details of DBusMessageLoader.
2365 * All members are private.
2367 struct DBusMessageLoader
2369 int refcount; /**< Reference count. */
2371 DBusString data; /**< Buffered data */
2373 DBusList *messages; /**< Complete messages. */
2375 long max_message_size; /**< Maximum size of a message */
2377 unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
2379 unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
2383 * The initial buffer size of the message loader.
2385 * @todo this should be based on min header size plus some average
2386 * body size, or something. Or rather, the min header size only, if we
2387 * want to try to read only the header, store that in a DBusMessage,
2388 * then read only the body and store that, etc., depends on
2389 * how we optimize _dbus_message_loader_get_buffer() and what
2390 * the exact message format is.
2392 #define INITIAL_LOADER_DATA_LEN 32
2395 * Creates a new message loader. Returns #NULL if memory can't
2398 * @returns new loader, or #NULL.
2401 _dbus_message_loader_new (void)
2403 DBusMessageLoader *loader;
2405 loader = dbus_new0 (DBusMessageLoader, 1);
2409 loader->refcount = 1;
2411 /* Try to cap message size at something that won't *totally* hose
2412 * the system if we have a couple of them.
2414 loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
2416 if (!_dbus_string_init (&loader->data, _DBUS_INT_MAX))
2422 /* preallocate the buffer for speed, ignore failure */
2423 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
2424 _dbus_string_set_length (&loader->data, 0);
2430 * Increments the reference count of the loader.
2432 * @param loader the loader.
2435 _dbus_message_loader_ref (DBusMessageLoader *loader)
2437 loader->refcount += 1;
2441 * Decrements the reference count of the loader and finalizes the
2442 * loader when the count reaches zero.
2444 * @param loader the loader.
2447 _dbus_message_loader_unref (DBusMessageLoader *loader)
2449 loader->refcount -= 1;
2450 if (loader->refcount == 0)
2452 _dbus_list_foreach (&loader->messages,
2453 (DBusForeachFunction) dbus_message_unref,
2455 _dbus_list_clear (&loader->messages);
2456 _dbus_string_free (&loader->data);
2462 * Gets the buffer to use for reading data from the network. Network
2463 * data is read directly into an allocated buffer, which is then used
2464 * in the DBusMessage, to avoid as many extra memcpy's as possible.
2465 * The buffer must always be returned immediately using
2466 * _dbus_message_loader_return_buffer(), even if no bytes are
2467 * successfully read.
2469 * @todo this function can be a lot more clever. For example
2470 * it can probably always return a buffer size to read exactly
2471 * the body of the next message, thus avoiding any memory wastage
2474 * @todo we need to enforce a max length on strings in header fields.
2476 * @param loader the message loader.
2477 * @param buffer the buffer
2480 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
2481 DBusString **buffer)
2483 _dbus_assert (!loader->buffer_outstanding);
2485 *buffer = &loader->data;
2487 loader->buffer_outstanding = TRUE;
2491 * The smallest header size that can occur.
2492 * (It won't be valid)
2494 #define DBUS_MINIMUM_HEADER_SIZE 16
2496 /** Pack four characters as in "abcd" into a uint32 */
2497 #define FOUR_CHARS_TO_UINT32(a, b, c, d) \
2498 ((((dbus_uint32_t)a) << 24) | \
2499 (((dbus_uint32_t)b) << 16) | \
2500 (((dbus_uint32_t)c) << 8) | \
2503 /** DBUS_HEADER_FIELD_NAME packed into a dbus_uint32_t */
2504 #define DBUS_HEADER_FIELD_NAME_AS_UINT32 \
2505 FOUR_CHARS_TO_UINT32 ('n', 'a', 'm', 'e')
2507 /** DBUS_HEADER_FIELD_SERVICE packed into a dbus_uint32_t */
2508 #define DBUS_HEADER_FIELD_SERVICE_AS_UINT32 \
2509 FOUR_CHARS_TO_UINT32 ('s', 'r', 'v', 'c')
2511 /** DBUS_HEADER_FIELD_REPLY packed into a dbus_uint32_t */
2512 #define DBUS_HEADER_FIELD_REPLY_AS_UINT32 \
2513 FOUR_CHARS_TO_UINT32 ('r', 'p', 'l', 'y')
2515 /** DBUS_HEADER_FIELD_SENDER Packed into a dbus_uint32_t */
2516 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32 \
2517 FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
2519 /* FIXME impose max length on name, srvc, sndr */
2521 decode_header_data (const DBusString *data,
2524 HeaderField fields[FIELD_LAST],
2525 int *message_padding)
2531 if (header_len < 16)
2535 while (i < FIELD_LAST)
2537 fields[i].offset = -1;
2541 fields[FIELD_HEADER_LENGTH].offset = 4;
2542 fields[FIELD_BODY_LENGTH].offset = 8;
2543 fields[FIELD_CLIENT_SERIAL].offset = 12;
2545 /* Now handle the named fields. A real named field is at least 4
2546 * bytes for the name, plus a type code (1 byte) plus padding. So
2547 * if we have less than 8 bytes left, it must be alignment padding,
2548 * not a field. While >= 8 bytes can't be entirely alignment
2552 while ((pos + 7) < header_len)
2554 pos = _DBUS_ALIGN_VALUE (pos, 4);
2556 if ((pos + 4) > header_len)
2559 _dbus_string_get_const_data_len (data, &field, pos, 4);
2562 _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
2564 /* I believe FROM_BE is right, but if not we'll find out
2567 switch (DBUS_UINT32_FROM_BE (*(int*)field))
2569 case DBUS_HEADER_FIELD_SERVICE_AS_UINT32:
2570 if (fields[FIELD_SERVICE].offset >= 0)
2572 _dbus_verbose ("%s field provided twice\n",
2573 DBUS_HEADER_FIELD_SERVICE);
2577 fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2579 _dbus_verbose ("Found service name at offset %d\n",
2580 fields[FIELD_SERVICE].offset);
2584 case DBUS_HEADER_FIELD_NAME_AS_UINT32:
2585 if (fields[FIELD_NAME].offset >= 0)
2587 _dbus_verbose ("%s field provided twice\n",
2588 DBUS_HEADER_FIELD_NAME);
2592 fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2595 _dbus_verbose ("Found message name at offset %d\n",
2596 fields[FIELD_NAME].offset);
2599 case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
2600 if (fields[FIELD_SENDER].offset >= 0)
2602 _dbus_verbose ("%s field provided twice\n",
2603 DBUS_HEADER_FIELD_SENDER);
2607 fields[FIELD_SENDER].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2609 _dbus_verbose ("Found sender name at offset %d\n",
2610 fields[FIELD_NAME].offset);
2613 case DBUS_HEADER_FIELD_REPLY_AS_UINT32:
2614 if (fields[FIELD_REPLY_SERIAL].offset >= 0)
2616 _dbus_verbose ("%s field provided twice\n",
2617 DBUS_HEADER_FIELD_REPLY);
2621 fields[FIELD_REPLY_SERIAL].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
2623 _dbus_verbose ("Found reply serial at offset %d\n",
2624 fields[FIELD_REPLY_SERIAL].offset);
2628 _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c at offset %d\n",
2629 field[0], field[1], field[2], field[3], pos);
2632 if (!_dbus_marshal_validate_arg (data, byte_order, pos, &new_pos))
2634 _dbus_verbose ("Failed to validate argument to named header field\n");
2638 if (new_pos > header_len)
2640 _dbus_verbose ("Named header field tries to extend beyond header length\n");
2647 if (pos < header_len)
2649 /* Alignment padding, verify that it's nul */
2650 _dbus_assert ((header_len - pos) < 8);
2652 if (!_dbus_string_validate_nul (data,
2653 pos, (header_len - pos)))
2655 _dbus_verbose ("header alignment padding is not nul\n");
2660 if (fields[FIELD_NAME].offset < 0)
2662 _dbus_verbose ("No %s field provided\n",
2663 DBUS_HEADER_FIELD_NAME);
2667 if (message_padding)
2668 *message_padding = header_len - pos;
2674 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
2675 * indicating to the loader how many bytes of the buffer were filled
2676 * in. This function must always be called, even if no bytes were
2677 * successfully read.
2679 * @todo if we run out of memory in here, we offer no way for calling
2680 * code to handle it, i.e. they can't re-run the message parsing
2681 * attempt. Perhaps much of this code could be moved to pop_message()?
2682 * But then that may need to distinguish NULL return for no messages
2683 * from NULL return for errors.
2685 * @param loader the loader.
2686 * @param buffer the buffer.
2687 * @param bytes_read number of bytes that were read into the buffer.
2690 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
2694 _dbus_assert (loader->buffer_outstanding);
2695 _dbus_assert (buffer == &loader->data);
2697 loader->buffer_outstanding = FALSE;
2699 if (loader->corrupted)
2702 while (_dbus_string_get_length (&loader->data) >= 16)
2704 DBusMessage *message;
2705 const char *header_data;
2706 int byte_order, header_len, body_len, header_padding;
2707 dbus_uint32_t header_len_unsigned, body_len_unsigned;
2709 _dbus_string_get_const_data_len (&loader->data, &header_data, 0, 16);
2711 _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
2713 if (header_data[2] != DBUS_MAJOR_PROTOCOL_VERSION)
2715 _dbus_verbose ("Message has protocol version %d ours is %d\n",
2716 (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
2717 loader->corrupted = TRUE;
2721 byte_order = header_data[0];
2723 if (byte_order != DBUS_LITTLE_ENDIAN &&
2724 byte_order != DBUS_BIG_ENDIAN)
2726 _dbus_verbose ("Message with bad byte order '%c' received\n",
2728 loader->corrupted = TRUE;
2732 header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
2733 body_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 8);
2735 if (header_len_unsigned < 16)
2737 _dbus_verbose ("Message had broken too-small header length %u\n",
2738 header_len_unsigned);
2739 loader->corrupted = TRUE;
2743 if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
2744 body_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE)
2746 _dbus_verbose ("Header or body length too large (%u %u)\n",
2747 header_len_unsigned,
2749 loader->corrupted = TRUE;
2753 /* Now that we know the values are in signed range, get
2754 * rid of stupid unsigned, just causes bugs
2756 header_len = header_len_unsigned;
2757 body_len = body_len_unsigned;
2759 if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
2762 _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
2764 loader->corrupted = TRUE;
2768 if (header_len + body_len > loader->max_message_size)
2770 _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %d\n",
2771 header_len, body_len, loader->max_message_size);
2772 loader->corrupted = TRUE;
2776 if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
2778 HeaderField fields[FIELD_LAST];
2783 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len + body_len);
2785 if (!decode_header_data (&loader->data, header_len, byte_order,
2786 fields, &header_padding))
2788 _dbus_verbose ("Header was invalid\n");
2789 loader->corrupted = TRUE;
2793 next_arg = header_len;
2794 while (next_arg < (header_len + body_len))
2796 int prev = next_arg;
2798 if (!_dbus_marshal_validate_arg (&loader->data,
2803 loader->corrupted = TRUE;
2807 _dbus_assert (next_arg > prev);
2810 if (next_arg > (header_len + body_len))
2812 _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
2813 next_arg, header_len, body_len,
2814 header_len + body_len);
2815 loader->corrupted = TRUE;
2819 message = dbus_message_new_empty_header ();
2820 if (message == NULL)
2821 break; /* ugh, postpone this I guess. */
2823 message->byte_order = byte_order;
2824 message->header_padding = header_padding;
2826 /* Copy in the offsets we found */
2828 while (i < FIELD_LAST)
2830 message->header_fields[i] = fields[i];
2834 if (!_dbus_list_append (&loader->messages, message))
2836 dbus_message_unref (message);
2840 _dbus_assert (_dbus_string_get_length (&message->header) == 0);
2841 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
2843 _dbus_assert (_dbus_string_get_length (&loader->data) >=
2844 (header_len + body_len));
2846 if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
2848 _dbus_list_remove_last (&loader->messages, message);
2849 dbus_message_unref (message);
2853 if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
2857 /* put the header back, we'll try again later */
2858 result = _dbus_string_copy_len (&message->header, 0, header_len,
2860 _dbus_assert (result); /* because DBusString never reallocs smaller */
2862 _dbus_list_remove_last (&loader->messages, message);
2863 dbus_message_unref (message);
2867 _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
2868 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
2870 /* Fill in caches */
2871 message->reply_serial = get_int_field (message,
2872 FIELD_REPLY_SERIAL);
2873 message->client_serial = get_int_field (message,
2874 FIELD_CLIENT_SERIAL);
2876 _dbus_verbose ("Loaded message %p\n", message);
2884 * Pops a loaded message (passing ownership of the message
2885 * to the caller). Returns #NULL if no messages have been
2888 * @param loader the loader.
2889 * @returns the next message, or #NULL if none.
2892 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
2894 return _dbus_list_pop_first (&loader->messages);
2898 * Pops a loaded message inside a list link (passing ownership of the
2899 * message and link to the caller). Returns #NULL if no messages have
2902 * @param loader the loader.
2903 * @returns the next message link, or #NULL if none.
2906 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
2908 return _dbus_list_pop_first_link (&loader->messages);
2912 * Checks whether the loader is confused due to bad data.
2913 * If messages are received that are invalid, the
2914 * loader gets confused and gives up permanently.
2915 * This state is called "corrupted."
2917 * @param loader the loader
2918 * @returns #TRUE if the loader is hosed.
2921 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
2923 return loader->corrupted;
2927 * Sets the maximum size message we allow.
2929 * @param loader the loader
2930 * @param size the max message size in bytes
2933 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
2936 if (size > MAX_SANE_MESSAGE_SIZE)
2938 _dbus_verbose ("clamping requested max message size %ld to %d\n",
2939 size, MAX_SANE_MESSAGE_SIZE);
2940 size = MAX_SANE_MESSAGE_SIZE;
2942 loader->max_message_size = size;
2946 * Gets the maximum allowed message size in bytes.
2948 * @param loader the loader
2949 * @returns max size in bytes
2952 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
2954 return loader->max_message_size;
2958 #ifdef DBUS_BUILD_TESTS
2959 #include "dbus-test.h"
2963 message_iter_test (DBusMessage *message)
2965 DBusMessageIter *iter;
2968 iter = dbus_message_get_args_iter (message);
2971 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_STRING)
2972 _dbus_assert_not_reached ("Argument type isn't string");
2974 str = dbus_message_iter_get_string (iter);
2975 if (strcmp (str, "Test string") != 0)
2976 _dbus_assert_not_reached ("Strings differ");
2979 if (!dbus_message_iter_next (iter))
2980 _dbus_assert_not_reached ("Reached end of arguments");
2982 /* Signed integer tests */
2983 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_INT32)
2984 _dbus_assert_not_reached ("Argument type isn't int32");
2986 if (dbus_message_iter_get_int32 (iter) != -0x12345678)
2987 _dbus_assert_not_reached ("Signed integers differ");
2989 if (!dbus_message_iter_next (iter))
2990 _dbus_assert_not_reached ("Reached end of fields");
2992 /* Unsigned integer tests */
2993 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_UINT32)
2994 _dbus_assert_not_reached ("Argument type isn't int32");
2996 if (dbus_message_iter_get_uint32 (iter) != 0xedd1e)
2997 _dbus_assert_not_reached ("Unsigned integers differ");
2999 if (!dbus_message_iter_next (iter))
3000 _dbus_assert_not_reached ("Reached end of arguments");
3003 if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_DOUBLE)
3004 _dbus_assert_not_reached ("Argument type isn't double");
3006 if (dbus_message_iter_get_double (iter) != 3.14159)
3007 _dbus_assert_not_reached ("Doubles differ");
3009 if (dbus_message_iter_next (iter))
3010 _dbus_assert_not_reached ("Didn't reach end of arguments");
3012 dbus_message_iter_unref (iter);
3016 check_message_handling (DBusMessage *message)
3018 DBusMessageIter *iter;
3021 dbus_int32_t client_serial;
3026 client_serial = dbus_message_get_serial (message);
3028 /* can't use set_serial due to the assertions at the start of it */
3029 set_int_field (message, FIELD_CLIENT_SERIAL,
3032 if (client_serial != dbus_message_get_serial (message))
3034 _dbus_warn ("get/set cycle for client_serial did not succeed\n");
3038 /* If we implement message_set_arg (message, n, value)
3039 * then we would want to test it here
3042 iter = dbus_message_get_args_iter (message);
3043 while ((type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID)
3049 case DBUS_TYPE_INT32:
3050 dbus_message_iter_get_int32 (iter);
3052 case DBUS_TYPE_UINT32:
3053 dbus_message_iter_get_uint32 (iter);
3055 case DBUS_TYPE_DOUBLE:
3056 dbus_message_iter_get_double (iter);
3058 case DBUS_TYPE_STRING:
3061 str = dbus_message_iter_get_string (iter);
3065 case DBUS_TYPE_BOOLEAN_ARRAY:
3067 unsigned char *values;
3070 if (!dbus_message_iter_get_boolean_array (iter, &values, &len))
3076 case DBUS_TYPE_INT32_ARRAY:
3078 dbus_int32_t *values;
3081 if (!dbus_message_iter_get_int32_array (iter, &values, &len))
3087 case DBUS_TYPE_UINT32_ARRAY:
3089 dbus_uint32_t *values;
3092 if (!dbus_message_iter_get_uint32_array (iter, &values, &len))
3098 case DBUS_TYPE_DOUBLE_ARRAY:
3103 if (!dbus_message_iter_get_double_array (iter, &values, &len))
3109 case DBUS_TYPE_STRING_ARRAY:
3114 if (!dbus_message_iter_get_string_array (iter, &values, &len))
3117 dbus_free_string_array (values);
3121 case DBUS_TYPE_DICT:
3125 if (!dbus_message_iter_get_dict (iter, &dict))
3127 dbus_dict_unref (dict);
3135 if (!dbus_message_iter_next (iter))
3143 dbus_message_iter_unref (iter);
3149 check_have_valid_message (DBusMessageLoader *loader)
3151 DBusMessage *message;
3157 if (_dbus_message_loader_get_is_corrupted (loader))
3159 _dbus_warn ("loader corrupted on message that was expected to be valid\n");
3163 message = _dbus_message_loader_pop_message (loader);
3164 if (message == NULL)
3166 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
3170 if (_dbus_string_get_length (&loader->data) > 0)
3172 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
3176 /* Verify that we're able to properly deal with the message.
3177 * For example, this would detect improper handling of messages
3178 * in nonstandard byte order.
3180 if (!check_message_handling (message))
3187 dbus_message_unref (message);
3193 check_invalid_message (DBusMessageLoader *loader)
3199 if (!_dbus_message_loader_get_is_corrupted (loader))
3201 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
3212 check_incomplete_message (DBusMessageLoader *loader)
3214 DBusMessage *message;
3220 if (_dbus_message_loader_get_is_corrupted (loader))
3222 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
3226 message = _dbus_message_loader_pop_message (loader);
3227 if (message != NULL)
3229 _dbus_warn ("loaded message that was expected to be incomplete\n");
3237 dbus_message_unref (message);
3242 check_loader_results (DBusMessageLoader *loader,
3243 DBusMessageValidity validity)
3247 case _DBUS_MESSAGE_VALID:
3248 return check_have_valid_message (loader);
3249 case _DBUS_MESSAGE_INVALID:
3250 return check_invalid_message (loader);
3251 case _DBUS_MESSAGE_INCOMPLETE:
3252 return check_incomplete_message (loader);
3253 case _DBUS_MESSAGE_UNKNOWN:
3257 _dbus_assert_not_reached ("bad DBusMessageValidity");
3263 * Loads the message in the given message file.
3265 * @param filename filename to load
3266 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3267 * @param data string to load message into
3268 * @returns #TRUE if the message was loaded
3271 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
3283 dbus_error_init (&error);
3284 if (!_dbus_file_get_contents (data, filename, &error))
3287 _dbus_string_get_const_data (filename, &s);
3288 _dbus_warn ("Could not load message file %s: %s\n", s, error.message);
3289 dbus_error_free (&error);
3295 if (!_dbus_message_data_load (data, filename))
3298 _dbus_string_get_const_data (filename, &s);
3299 _dbus_warn ("Could not load message file %s\n", s);
3312 * Tries loading the message in the given message file
3313 * and verifies that DBusMessageLoader can handle it.
3315 * @param filename filename to load
3316 * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
3317 * @param expected_validity what the message has to be like to return #TRUE
3318 * @returns #TRUE if the message has the expected validity
3321 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
3323 DBusMessageValidity expected_validity)
3330 if (!_dbus_string_init (&data, _DBUS_INT_MAX))
3331 _dbus_assert_not_reached ("could not allocate string\n");
3333 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
3337 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
3345 if (_dbus_string_get_length (&data) > 0)
3346 _dbus_verbose_bytes_of_string (&data, 0,
3347 _dbus_string_get_length (&data));
3349 _dbus_string_get_const_data (filename, &s);
3350 _dbus_warn ("Failed message loader test on %s\n",
3354 _dbus_string_free (&data);
3360 * Tries loading the given message data.
3363 * @param data the message data
3364 * @param expected_validity what the message has to be like to return #TRUE
3365 * @returns #TRUE if the message has the expected validity
3368 dbus_internal_do_not_use_try_message_data (const DBusString *data,
3369 DBusMessageValidity expected_validity)
3371 DBusMessageLoader *loader;
3379 /* Write the data one byte at a time */
3381 loader = _dbus_message_loader_new ();
3383 len = _dbus_string_get_length (data);
3384 for (i = 0; i < len; i++)
3388 _dbus_message_loader_get_buffer (loader, &buffer);
3389 _dbus_string_append_byte (buffer,
3390 _dbus_string_get_byte (data, i));
3391 _dbus_message_loader_return_buffer (loader, buffer, 1);
3394 if (!check_loader_results (loader, expected_validity))
3397 _dbus_message_loader_unref (loader);
3400 /* Write the data all at once */
3402 loader = _dbus_message_loader_new ();
3407 _dbus_message_loader_get_buffer (loader, &buffer);
3408 _dbus_string_copy (data, 0, buffer,
3409 _dbus_string_get_length (buffer));
3410 _dbus_message_loader_return_buffer (loader, buffer, 1);
3413 if (!check_loader_results (loader, expected_validity))
3416 _dbus_message_loader_unref (loader);
3419 /* Write the data 2 bytes at a time */
3421 loader = _dbus_message_loader_new ();
3423 len = _dbus_string_get_length (data);
3424 for (i = 0; i < len; i += 2)
3428 _dbus_message_loader_get_buffer (loader, &buffer);
3429 _dbus_string_append_byte (buffer,
3430 _dbus_string_get_byte (data, i));
3432 _dbus_string_append_byte (buffer,
3433 _dbus_string_get_byte (data, i+1));
3434 _dbus_message_loader_return_buffer (loader, buffer, 1);
3437 if (!check_loader_results (loader, expected_validity))
3440 _dbus_message_loader_unref (loader);
3448 _dbus_message_loader_unref (loader);
3454 process_test_subdir (const DBusString *test_base_dir,
3456 DBusMessageValidity validity,
3457 DBusForeachMessageFileFunc function,
3460 DBusString test_directory;
3461 DBusString filename;
3469 if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
3470 _dbus_assert_not_reached ("didn't allocate test_directory\n");
3472 _dbus_string_init_const (&filename, subdir);
3474 if (!_dbus_string_copy (test_base_dir, 0,
3475 &test_directory, 0))
3476 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3478 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3479 _dbus_assert_not_reached ("couldn't allocate full path");
3481 _dbus_string_free (&filename);
3482 if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
3483 _dbus_assert_not_reached ("didn't allocate filename string\n");
3485 dbus_error_init (&error);
3486 dir = _dbus_directory_open (&test_directory, &error);
3490 _dbus_string_get_const_data (&test_directory, &s);
3491 _dbus_warn ("Could not open %s: %s\n", s,
3493 dbus_error_free (&error);
3497 printf ("Testing:\n");
3500 while (_dbus_directory_get_next_file (dir, &filename, &error))
3502 DBusString full_path;
3505 if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
3506 _dbus_assert_not_reached ("couldn't init string");
3508 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3509 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3511 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3512 _dbus_assert_not_reached ("couldn't concat file to dir");
3514 if (_dbus_string_ends_with_c_str (&filename, ".message"))
3516 else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
3520 const char *filename_c;
3521 _dbus_string_get_const_data (&filename, &filename_c);
3522 _dbus_verbose ("Skipping non-.message file %s\n",
3524 _dbus_string_free (&full_path);
3530 _dbus_string_get_const_data (&filename, &s);
3531 printf (" %s\n", s);
3534 _dbus_verbose (" expecting %s\n",
3535 validity == _DBUS_MESSAGE_VALID ? "valid" :
3536 (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
3537 (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
3539 if (! (*function) (&full_path, is_raw, validity, user_data))
3541 _dbus_string_free (&full_path);
3545 _dbus_string_free (&full_path);
3548 if (dbus_error_is_set (&error))
3551 _dbus_string_get_const_data (&test_directory, &s);
3552 _dbus_warn ("Could not get next file in %s: %s\n",
3554 dbus_error_free (&error);
3563 _dbus_directory_close (dir);
3564 _dbus_string_free (&test_directory);
3565 _dbus_string_free (&filename);
3571 * Runs the given function on every message file in the test suite.
3572 * The function should return #FALSE on test failure or fatal error.
3574 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
3575 * @param func the function to run
3576 * @param user_data data for function
3577 * @returns #FALSE if there's a failure
3580 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
3581 DBusForeachMessageFileFunc func,
3584 DBusString test_directory;
3589 _dbus_string_init_const (&test_directory, test_data_dir);
3591 if (!process_test_subdir (&test_directory, "valid-messages",
3592 _DBUS_MESSAGE_VALID, func, user_data))
3595 if (!process_test_subdir (&test_directory, "invalid-messages",
3596 _DBUS_MESSAGE_INVALID, func, user_data))
3599 if (!process_test_subdir (&test_directory, "incomplete-messages",
3600 _DBUS_MESSAGE_INCOMPLETE, func, user_data))
3607 _dbus_string_free (&test_directory);
3613 verify_test_message (DBusMessage *message)
3615 dbus_int32_t our_int;
3618 dbus_bool_t our_bool;
3620 if (!dbus_message_get_args (message, NULL,
3621 DBUS_TYPE_INT32, &our_int,
3622 DBUS_TYPE_STRING, &our_str,
3623 DBUS_TYPE_DOUBLE, &our_double,
3624 DBUS_TYPE_BOOLEAN, &our_bool,
3626 _dbus_assert_not_reached ("Could not get arguments");
3628 if (our_int != -0x12345678)
3629 _dbus_assert_not_reached ("integers differ!");
3631 if (our_double != 3.14159)
3632 _dbus_assert_not_reached ("doubles differ!");
3634 if (strcmp (our_str, "Test string") != 0)
3635 _dbus_assert_not_reached ("strings differ!");
3638 _dbus_assert_not_reached ("booleans differ");
3640 dbus_free (our_str);
3644 * @ingroup DBusMessageInternals
3645 * Unit test for DBusMessage.
3647 * @returns #TRUE on success.
3650 _dbus_message_test (const char *test_data_dir)
3652 DBusMessage *message;
3653 DBusMessageLoader *loader;
3660 /* Test the vararg functions */
3661 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3662 _dbus_message_set_serial (message, 1);
3663 dbus_message_append_args (message,
3664 DBUS_TYPE_INT32, -0x12345678,
3665 DBUS_TYPE_STRING, "Test string",
3666 DBUS_TYPE_DOUBLE, 3.14159,
3667 DBUS_TYPE_BOOLEAN, TRUE,
3669 _dbus_verbose_bytes_of_string (&message->header, 0,
3670 _dbus_string_get_length (&message->header));
3671 _dbus_verbose_bytes_of_string (&message->body, 0,
3672 _dbus_string_get_length (&message->body));
3674 verify_test_message (message);
3676 copy = dbus_message_copy (message);
3678 _dbus_assert (message->client_serial == copy->client_serial);
3679 _dbus_assert (message->reply_serial == copy->reply_serial);
3680 _dbus_assert (message->header_padding == copy->header_padding);
3682 _dbus_assert (_dbus_string_get_length (&message->header) ==
3683 _dbus_string_get_length (©->header));
3685 _dbus_assert (_dbus_string_get_length (&message->body) ==
3686 _dbus_string_get_length (©->body));
3688 verify_test_message (copy);
3690 name1 = dbus_message_get_name (message);
3691 name2 = dbus_message_get_name (copy);
3693 _dbus_assert (strcmp (name1, name2) == 0);
3695 dbus_message_unref (message);
3696 dbus_message_unref (copy);
3698 message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
3699 _dbus_message_set_serial (message, 1);
3700 dbus_message_set_reply_serial (message, 0x12345678);
3702 dbus_message_append_string (message, "Test string");
3703 dbus_message_append_int32 (message, -0x12345678);
3704 dbus_message_append_uint32 (message, 0xedd1e);
3705 dbus_message_append_double (message, 3.14159);
3707 message_iter_test (message);
3709 /* Message loader test */
3710 _dbus_message_lock (message);
3711 loader = _dbus_message_loader_new ();
3713 /* Write the header data one byte at a time */
3714 _dbus_string_get_const_data (&message->header, &data);
3715 for (i = 0; i < _dbus_string_get_length (&message->header); i++)
3719 _dbus_message_loader_get_buffer (loader, &buffer);
3720 _dbus_string_append_byte (buffer, data[i]);
3721 _dbus_message_loader_return_buffer (loader, buffer, 1);
3724 /* Write the body data one byte at a time */
3725 _dbus_string_get_const_data (&message->body, &data);
3726 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
3730 _dbus_message_loader_get_buffer (loader, &buffer);
3731 _dbus_string_append_byte (buffer, data[i]);
3732 _dbus_message_loader_return_buffer (loader, buffer, 1);
3735 dbus_message_unref (message);
3737 /* Now pop back the message */
3738 if (_dbus_message_loader_get_is_corrupted (loader))
3739 _dbus_assert_not_reached ("message loader corrupted");
3741 message = _dbus_message_loader_pop_message (loader);
3743 _dbus_assert_not_reached ("received a NULL message");
3745 if (dbus_message_get_reply_serial (message) != 0x12345678)
3746 _dbus_assert_not_reached ("reply serial fields differ");
3748 message_iter_test (message);
3750 dbus_message_unref (message);
3751 _dbus_message_loader_unref (loader);
3753 /* Now load every message in test_data_dir if we have one */
3754 if (test_data_dir == NULL)
3757 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
3758 (DBusForeachMessageFileFunc)
3759 dbus_internal_do_not_use_try_message_file,
3763 #endif /* DBUS_BUILD_TESTS */