2003-03-31 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-message.c
index 66c6bd5..39ed394 100644 (file)
@@ -81,7 +81,7 @@ typedef struct
  */
 struct DBusMessage
 {
-  int refcount; /**< Reference count */
+  dbus_atomic_t refcount; /**< Reference count */
 
   DBusString header; /**< Header network data, stored
                       * separately from body so we can
@@ -91,6 +91,10 @@ struct DBusMessage
   HeaderField header_fields[FIELD_LAST]; /**< Track the location
                                            * of each field in "header"
                                            */
+
+  dbus_int32_t client_serial; /**< Cached client serial value for speed */
+  dbus_int32_t reply_serial;  /**< Cached reply serial value for speed */
+  
   int header_padding; /**< bytes of alignment in header */
   
   DBusString body;   /**< Body network data. */
@@ -144,7 +148,7 @@ clear_header_padding (DBusMessage *message)
   _dbus_string_shorten (&message->header,
                         message->header_padding);
   message->header_padding = 0;
-}                      
+}              
 
 static dbus_bool_t
 append_header_padding (DBusMessage *message)
@@ -184,8 +188,12 @@ get_string_field (DBusMessage *message,
                   int          field,
                   int         *len)
 {
-  int offset = message->header_fields[field].offset;
+  int offset;
   const char *data;
+
+  offset = message->header_fields[field].offset;
+
+  _dbus_assert (field < FIELD_LAST);
   
   if (offset < 0)
     return NULL;
@@ -201,21 +209,24 @@ get_string_field (DBusMessage *message,
                                    offset,
                                    NULL);
 
-  _dbus_string_get_const_data (&message->header,
-                               &data);
+  data = _dbus_string_get_const_data (&message->header);
   
   return data + (offset + 4); 
 }
 
 static dbus_int32_t
 get_int_field (DBusMessage *message,
-                     int          field)
+               int          field)
 {
-  int offset = message->header_fields[field].offset;
+  int offset;
+
+  _dbus_assert (field < FIELD_LAST);
+  
+  offset = message->header_fields[field].offset;
   
   if (offset < 0)
     return -1; /* useless if -1 is a valid value of course */
-
+  
   return _dbus_demarshal_int32 (&message->header,
                                 message->byte_order,
                                 offset,
@@ -481,15 +492,21 @@ set_string_field (DBusMessage *message,
       DBusString v;
       int old_len;
       int new_len;
+      int len;
+      
+      clear_header_padding (message);
       
       old_len = _dbus_string_get_length (&message->header);
 
+      len = strlen (value);
+      
       _dbus_string_init_const_len (&v, value,
-                                   strlen (value) + 1); /* include nul */
+                                  len + 1); /* include nul */
       if (!_dbus_marshal_set_string (&message->header,
                                      message->byte_order,
-                                     offset, &v))
-        return FALSE;
+                                     offset, &v,
+                                    len))
+        goto failed;
       
       new_len = _dbus_string_get_length (&message->header);
 
@@ -497,32 +514,39 @@ set_string_field (DBusMessage *message,
                             offset,
                             new_len - old_len);
 
+      if (!append_header_padding (message))
+       goto failed;
+      
       return TRUE;
+
+    failed:
+      /* this must succeed because it was allocated on function entry and
+       * DBusString doesn't ever realloc smaller
+       */
+      if (!append_header_padding (message))
+       _dbus_assert_not_reached ("failed to reappend header padding");
+
+      return FALSE;
     }
 }
 
 /**
- * Sets the client serial of a message. 
+ * Sets the serial number of a message. 
  * This can only be done once on a message.
- *
- * @todo client_serial should be called simply
- * "serial"; it's in outgoing messages for both
- * the client and the server, it's only client-specific
- * in the message bus case. It's more like origin_serial
- * or something.
  * 
  * @param message the message
- * @param client_serial the client serial
+ * @param serial the serial
  */
 void
-_dbus_message_set_client_serial (DBusMessage  *message,
-                                dbus_int32_t  client_serial)
+_dbus_message_set_serial (DBusMessage  *message,
+                          dbus_int32_t  serial)
 {
   _dbus_assert (!message->locked);
-  _dbus_assert (_dbus_message_get_client_serial (message) < 0);
-
+  _dbus_assert (dbus_message_get_serial (message) < 0);
+  
   set_int_field (message, FIELD_CLIENT_SERIAL,
-                 client_serial);
+                 serial);
+  message->client_serial = serial;
 }
 
 /**
@@ -534,31 +558,33 @@ _dbus_message_set_client_serial (DBusMessage  *message,
  * @returns #FALSE if not enough memory
  */
 dbus_bool_t
-_dbus_message_set_reply_serial (DBusMessage  *message,
+dbus_message_set_reply_serial (DBusMessage  *message,
                                 dbus_int32_t  reply_serial)
 {
   _dbus_assert (!message->locked);
 
-  return set_int_field (message, FIELD_REPLY_SERIAL,
-                        reply_serial);
+  if (set_int_field (message, FIELD_REPLY_SERIAL,
+                     reply_serial))
+    {
+      message->reply_serial = reply_serial;
+      return TRUE;
+    }
+  else
+    return FALSE;
 }
 
 /**
- * Returns the client serial of a message or
- * -1 if none has been specified.
- *
- * @todo see note in _dbus_message_set_client_serial()
- * about how client_serial is a misnomer
- *
- * @todo this function should be public, after renaming it.
+ * Returns the serial of a message or -1 if none has been specified.
+ * The message's serial number is provided by the application sending
+ * the message and is used to identify replies to this message.
  *
  * @param message the message
  * @returns the client serial
  */
 dbus_int32_t
-_dbus_message_get_client_serial (DBusMessage *message)
+dbus_message_get_serial (DBusMessage *message)
 {
-  return get_int_field (message, FIELD_CLIENT_SERIAL);
+  return message->client_serial;
 }
 
 /**
@@ -569,9 +595,9 @@ _dbus_message_get_client_serial (DBusMessage *message)
  * @returns the reply serial
  */
 dbus_int32_t
-_dbus_message_get_reply_serial  (DBusMessage *message)
+dbus_message_get_reply_serial  (DBusMessage *message)
 {
-  return get_int_field (message, FIELD_REPLY_SERIAL);
+  return message->reply_serial;
 }
 
 /**
@@ -601,8 +627,10 @@ _dbus_message_add_size_counter (DBusMessage *message,
     _dbus_string_get_length (&message->header) +
     _dbus_string_get_length (&message->body);
 
+#if 0
   _dbus_verbose ("message has size %ld\n",
                  message->size_counter_delta);
+#endif
   
   _dbus_counter_adjust (message->size_counter, message->size_counter_delta);
 }
@@ -720,6 +748,8 @@ dbus_message_new_empty_header (void)
   
   message->refcount = 1;
   message->byte_order = DBUS_COMPILER_BYTE_ORDER;
+  message->client_serial = -1;
+  message->reply_serial = -1;
   
   i = 0;
   while (i < FIELD_LAST)
@@ -728,13 +758,13 @@ dbus_message_new_empty_header (void)
       ++i;
     }
   
-  if (!_dbus_string_init (&message->header, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&message->header))
     {
       dbus_free (message);
       return NULL;
     }
   
-  if (!_dbus_string_init (&message->body, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&message->body))
     {
       _dbus_string_free (&message->header);
       dbus_free (message);
@@ -746,12 +776,15 @@ dbus_message_new_empty_header (void)
 
 
 /**
- * Constructs a new message. Returns #NULL if memory
- * can't be allocated for the message.
+ * Constructs a new message. Returns #NULL if memory can't be
+ * allocated for the message. The service may be #NULL in which case
+ * no service is set; this is appropriate when using D-BUS in a
+ * peer-to-peer context (no message bus).
  *
- * @todo use DBusString internally to store service and name.
+ * @todo reverse the arguments, first 'name' then 'service'
+ * as 'name' is more fundamental
  *
- * @param service service that the message should be sent to
+ * @param service service that the message should be sent to or #NULL
  * @param name name of the message
  * @returns a new DBusMessage, free with dbus_message_unref()
  * @see dbus_message_unref()
@@ -780,36 +813,80 @@ dbus_message_new (const char *service,
  * message. Returns #NULL if memory can't be allocated
  * for the message.
  *
- * @param name the name of the message
  * @param original_message the message which the created
  * message is a reply to.
  * @returns a new DBusMessage, free with dbus_message_unref()
  * @see dbus_message_new(), dbus_message_unref()
  */ 
 DBusMessage*
-dbus_message_new_reply (const char  *name,
-                       DBusMessage *original_message)
+dbus_message_new_reply (DBusMessage *original_message)
 {
   DBusMessage *message;
-  const char *sender;
+  const char *sender, *name;
 
   sender = get_string_field (original_message,
                              FIELD_SENDER, NULL);
+  name = get_string_field (original_message,
+                          FIELD_NAME, NULL);
 
-  _dbus_assert (sender != NULL);
+  /* sender is allowed to be null here in peer-to-peer case */
   
   message = dbus_message_new (sender, name);
   
   if (message == NULL)
     return NULL;
 
-  if (!_dbus_message_set_reply_serial (message,
-                                       _dbus_message_get_client_serial (original_message)))
+  if (!dbus_message_set_reply_serial (message,
+                                      dbus_message_get_serial (original_message)))
+    {
+      dbus_message_unref (message);
+      return NULL;
+    }
+
+  return message;
+}
+
+/**
+ * Creates a new message that is an error reply to a certain message.
+ *
+ * @param original_message the original message
+ * @param error_name the error name
+ * @param error_message the error message string
+ * @returns a new error message
+ */
+DBusMessage*
+dbus_message_new_error_reply (DBusMessage *original_message,
+                             const char  *error_name,
+                             const char  *error_message)
+{
+  DBusMessage *message;
+  const char *sender;
+
+  sender = get_string_field (original_message,
+                             FIELD_SENDER, NULL);
+  
+  _dbus_assert (sender != NULL);
+  
+  message = dbus_message_new (sender, error_name);
+  
+  if (message == NULL)
+    return NULL;
+
+  if (!dbus_message_set_reply_serial (message,
+                                      dbus_message_get_serial (original_message)))
+    {
+      dbus_message_unref (message);
+      return NULL;
+    }
+
+  if (!dbus_message_append_string (message, error_message))
     {
       dbus_message_unref (message);
       return NULL;
     }
 
+  dbus_message_set_is_error (message, TRUE);
+  
   return message;
 }
 
@@ -821,7 +898,7 @@ dbus_message_new_reply (const char  *name,
  * @returns the new message.
  */
 DBusMessage *
-dbus_message_new_from_message (const DBusMessage *message)
+dbus_message_copy (const DBusMessage *message)
 {
   DBusMessage *retval;
   int i;
@@ -832,14 +909,18 @@ dbus_message_new_from_message (const DBusMessage *message)
   
   retval->refcount = 1;
   retval->byte_order = message->byte_order;
-
-  if (!_dbus_string_init (&retval->header, _DBUS_INT_MAX))
+  retval->client_serial = message->client_serial;
+  retval->reply_serial = message->reply_serial;
+  retval->header_padding = message->header_padding;
+  retval->locked = FALSE;
+  
+  if (!_dbus_string_init (&retval->header))
     {
       dbus_free (retval);
       return NULL;
     }
   
-  if (!_dbus_string_init (&retval->body, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&retval->body))
     {
       _dbus_string_free (&retval->header);
       dbus_free (retval);
@@ -884,9 +965,10 @@ dbus_message_new_from_message (const DBusMessage *message)
 void
 dbus_message_ref (DBusMessage *message)
 {
-  _dbus_assert (message->refcount > 0);
-  
-  message->refcount += 1;
+  dbus_atomic_t refcount;
+
+  refcount = _dbus_atomic_inc (&message->refcount);
+  _dbus_assert (refcount > 1);
 }
 
 /**
@@ -898,10 +980,13 @@ dbus_message_ref (DBusMessage *message)
 void
 dbus_message_unref (DBusMessage *message)
 {
-  _dbus_assert (message->refcount > 0);
+  dbus_atomic_t refcount;
 
-  message->refcount -= 1;
-  if (message->refcount == 0)
+  refcount = _dbus_atomic_dec (&message->refcount);
+  
+  _dbus_assert (refcount >= 0);
+
+  if (refcount == 0)
     {
       if (message->size_counter != NULL)
         {
@@ -932,6 +1017,11 @@ dbus_message_get_name (DBusMessage *message)
 /**
  * Gets the destination service of a message.
  *
+ * @todo I think if we have set_sender/get_sender,
+ * this function might be better named set_destination/
+ * get_destination for clarity, as the sender
+ * is also a service name.
+ * 
  * @param message the message
  * @returns the message destination service (should not be freed)
  */
@@ -944,26 +1034,28 @@ dbus_message_get_service (DBusMessage *message)
 /**
  * Appends fields to a message given a variable argument
  * list. The variable argument list should contain the type
- * of the field followed by the value to add.
- * The list is terminated with 0.
+ * of the argument followed by the value to add. Array values
+ * are specified by a pointer to the array followed by an int
+ * giving the length of the array. The list is terminated
+ * with 0.
  *
  * @param message the message
- * @param first_field_type type of the first field
- * @param ... value of first field, list of additional type-value pairs
+ * @param first_arg_type type of the first argument
+ * @param ... value of first argument, list of additional type-value pairs
  * @returns #TRUE on success
  */
 dbus_bool_t
-dbus_message_append_fields (DBusMessage *message,
-                            int first_field_type,
-                           ...)
+dbus_message_append_args (DBusMessage *message,
+                         int first_arg_type,
+                         ...)
 {
   dbus_bool_t retval;
   va_list var_args;
 
-  va_start (var_args, first_field_type);
-  retval = dbus_message_append_fields_valist (message,
-                                              first_field_type,
-                                              var_args);
+  va_start (var_args, first_arg_type);
+  retval = dbus_message_append_args_valist (message,
+                                           first_arg_type,
+                                           var_args);
   va_end (var_args);
 
   return retval;
@@ -972,27 +1064,34 @@ dbus_message_append_fields (DBusMessage *message,
 /**
  * This function takes a va_list for use by language bindings
  *
- * @see dbus_message_append_fields.  
+ * @see dbus_message_append_args.  
  * @param message the message
- * @param first_field_type type of first field
- * @param var_args value of first field, then list of type/value pairs
+ * @param first_arg_type type of first argument
+ * @param var_args value of first argument, then list of type/value pairs
  * @returns #TRUE on success
  */
 dbus_bool_t
-dbus_message_append_fields_valist (DBusMessage *message,
-                                   int          first_field_type,
-                                  va_list      var_args)
+dbus_message_append_args_valist (DBusMessage *message,
+                                int          first_arg_type,
+                                va_list      var_args)
 {
   int type, old_len;
 
   old_len = _dbus_string_get_length (&message->body);
   
-  type = first_field_type;
+  type = first_arg_type;
 
   while (type != 0)
     {
       switch (type)
        {
+       case DBUS_TYPE_NIL:
+         if (!dbus_message_append_nil (message))
+           goto enomem;
+       case DBUS_TYPE_BOOLEAN:
+         if (!dbus_message_append_boolean (message, va_arg (var_args, dbus_bool_t)))
+           goto enomem;
+         break;
        case DBUS_TYPE_INT32:
          if (!dbus_message_append_int32 (message, va_arg (var_args, dbus_int32_t)))
            goto enomem;
@@ -1009,6 +1108,54 @@ dbus_message_append_fields_valist (DBusMessage *message,
          if (!dbus_message_append_string (message, va_arg (var_args, const char *)))
            goto enomem;
          break;
+       case DBUS_TYPE_BOOLEAN_ARRAY:
+         {
+           int len;
+           unsigned char *data;
+
+           data = va_arg (var_args, unsigned char *);
+           len = va_arg (var_args, int);
+
+           if (!dbus_message_append_boolean_array (message, data, len))
+             goto enomem;
+         }
+         break;
+       case DBUS_TYPE_INT32_ARRAY:
+         {
+           int len;
+           dbus_int32_t *data;
+
+           data = va_arg (var_args, dbus_int32_t *);
+           len = va_arg (var_args, int);
+
+           if (!dbus_message_append_int32_array (message, data, len))
+             goto enomem;
+         }
+         break;
+       case DBUS_TYPE_UINT32_ARRAY:
+         {
+           int len;
+           dbus_uint32_t *data;
+
+           data = va_arg (var_args, dbus_uint32_t *);
+           len = va_arg (var_args, int);
+
+           if (!dbus_message_append_uint32_array (message, data, len))
+             goto enomem;
+         }
+         break;
+       case DBUS_TYPE_DOUBLE_ARRAY:
+         {
+           int len;
+           double *data;
+
+           data = va_arg (var_args, double *);
+           len = va_arg (var_args, int);
+
+           if (!dbus_message_append_double_array (message, data, len))
+             goto enomem;
+         }
+         break;
        case DBUS_TYPE_BYTE_ARRAY:
          {
            int len;
@@ -1033,7 +1180,16 @@ dbus_message_append_fields_valist (DBusMessage *message,
              goto enomem;
          }
          break;
-         
+       case DBUS_TYPE_DICT:
+         {
+           DBusDict *dict;
+
+           dict = va_arg (var_args, DBusDict *);
+
+           if (!dbus_message_append_dict (message, dict))
+             goto enomem;
+           break;
+         }
        default:
          _dbus_warn ("Unknown field type %d\n", type);
        }
@@ -1044,11 +1200,52 @@ dbus_message_append_fields_valist (DBusMessage *message,
   return TRUE;
 
  enomem:
-  _dbus_string_set_length (&message->body, old_len);
   return FALSE;
 }
 
 /**
+ * Appends a nil value to the message
+ *
+ * @param message the message
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_append_nil (DBusMessage *message)
+{
+  _dbus_assert (!message->locked);
+
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_NIL))
+      return FALSE;
+  else
+    return TRUE;
+}
+
+/**
+ * Appends a boolean value to the message
+ *
+ * @param message the message
+ * @param value the boolean value
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_append_boolean (DBusMessage  *message,
+                            dbus_bool_t   value)
+{
+  _dbus_assert (!message->locked);
+  
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN))
+    return FALSE;
+
+  if (!_dbus_string_append_byte (&message->body, (value != FALSE)))
+    {
+      _dbus_string_shorten (&message->body, 1);
+      return FALSE;
+    }
+
+  return TRUE;
+}
+
+/**
  * Appends a 32 bit signed integer to the message.
  *
  * @param message the message
@@ -1062,13 +1259,15 @@ dbus_message_append_int32 (DBusMessage  *message,
   _dbus_assert (!message->locked);
 
   if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32))
+      return FALSE;
+  
+  if (!_dbus_marshal_int32 (&message->body, message->byte_order, value))
     {
       _dbus_string_shorten (&message->body, 1);
       return FALSE;
     }
-  
-  return _dbus_marshal_int32 (&message->body,
-                             message->byte_order, value);
+
+  return TRUE;
 }
 
 /**
@@ -1085,13 +1284,15 @@ dbus_message_append_uint32 (DBusMessage   *message,
   _dbus_assert (!message->locked);
 
   if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32))
+      return FALSE;
+  
+  if (!_dbus_marshal_uint32 (&message->body, message->byte_order, value))
     {
       _dbus_string_shorten (&message->body, 1);
       return FALSE;
     }
-  
-  return _dbus_marshal_uint32 (&message->body,
-                              message->byte_order, value);
+
+  return TRUE;      
 }
 
 /**
@@ -1108,13 +1309,15 @@ dbus_message_append_double (DBusMessage *message,
   _dbus_assert (!message->locked);
 
   if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE))
+    return FALSE;
+
+  if (!_dbus_marshal_double (&message->body, message->byte_order, value))
     {
       _dbus_string_shorten (&message->body, 1);
       return FALSE;
     }
   
-  return _dbus_marshal_double (&message->body,
-                              message->byte_order, value);
+  return TRUE;
 }
 
 /**
@@ -1131,17 +1334,19 @@ dbus_message_append_string (DBusMessage *message,
   _dbus_assert (!message->locked);
 
   if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING))
+      return FALSE;
+  
+  if (!_dbus_marshal_string (&message->body, message->byte_order, value))
     {
       _dbus_string_shorten (&message->body, 1);
-      return FALSE;
+      return FALSE;      
     }
-  
-  return _dbus_marshal_string (&message->body,
-                              message->byte_order, value);
+
+  return TRUE;
 }
 
 /**
- * Appends a byte array to the message.
+ * Appends a boolean array to the message.
  *
  * @param message the message
  * @param value the array
@@ -1149,24 +1354,26 @@ dbus_message_append_string (DBusMessage *message,
  * @returns #TRUE on success
  */
 dbus_bool_t
-dbus_message_append_byte_array (DBusMessage         *message,
-                               unsigned const char *value,
-                               int                 len)
+dbus_message_append_boolean_array (DBusMessage         *message,
+                                  unsigned const char *value,
+                                  int                  len)
 {
   _dbus_assert (!message->locked);
 
-  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY))
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BOOLEAN_ARRAY))
+    return FALSE;
+
+  if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
     {
       _dbus_string_shorten (&message->body, 1);
       return FALSE;
     }
-  
-  return _dbus_marshal_byte_array (&message->body,
-                                  message->byte_order, value, len);
+
+  return TRUE;
 }
 
 /**
- * Appends a string array to the message.
+ * Appends a 32 bit signed integer array to the message.
  *
  * @param message the message
  * @param value the array
@@ -1174,111 +1381,271 @@ dbus_message_append_byte_array (DBusMessage         *message,
  * @returns #TRUE on success
  */
 dbus_bool_t
-dbus_message_append_string_array (DBusMessage *message,
-                                 const char **value,
-                                 int          len)
+dbus_message_append_int32_array (DBusMessage        *message,
+                                const dbus_int32_t *value,
+                                int                 len)
 {
   _dbus_assert (!message->locked);
 
-  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING_ARRAY))
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32_ARRAY))
+    return FALSE;
+
+  if (!_dbus_marshal_int32_array (&message->body, message->byte_order,
+                                 value, len))
     {
       _dbus_string_shorten (&message->body, 1);
       return FALSE;
     }
-  
-  return _dbus_marshal_string_array (&message->body,
-                                    message->byte_order, value, len);
+
+  return TRUE;
 }
 
 /**
- * Gets fields from a message given a variable argument list.
- * The variable argument list should contain the type of the
- * field followed by a pointer to where the value should be
- * stored. The list is terminated with 0.
+ * Appends a 32 bit unsigned integer array to the message.
  *
- *  @todo rename get_args to avoid confusion with header fields
- * 
  * @param message the message
- * @param first_field_type the first field type
- * @param ... location for first field value, then list of type-location pairs
- * @returns result code
+ * @param value the array
+ * @param len the length of the array
+ * @returns #TRUE on success
  */
-DBusResultCode
-dbus_message_get_fields (DBusMessage *message,
-                         int          first_field_type,
-                        ...)
+dbus_bool_t
+dbus_message_append_uint32_array (DBusMessage         *message,
+                                 const dbus_uint32_t *value,
+                                 int                  len)
 {
-  DBusResultCode retval;
-  va_list var_args;
+  _dbus_assert (!message->locked);
 
-  va_start (var_args, first_field_type);
-  retval = dbus_message_get_fields_valist (message, first_field_type, var_args);
-  va_end (var_args);
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32_ARRAY))
+    return FALSE;
 
-  return retval;
+  if (!_dbus_marshal_uint32_array (&message->body, message->byte_order,
+                                 value, len))
+    {
+      _dbus_string_shorten (&message->body, 1);
+      return FALSE;
+    }
+
+  return TRUE;
 }
 
 /**
- * This function takes a va_list for use by language bindings
- *
- * @todo this function (or some lower-level non-convenience function)
- * needs better error handling; should allow the application to
- * distinguish between out of memory, and bad data from the remote
- * app. It also needs to not leak a bunch of args when it gets
- * to the arg that's bad, as that would be a security hole
- * (allow one app to force another to leak memory)
- *
- * @todo We need to free the field data when an error occurs.
+ * Appends a double array to the message.
  *
- * @todo rename get_args_valist to avoid confusion with header fields
- *
- * @see dbus_message_get_fields
  * @param message the message
- * @param first_field_type type of the first field
- * @param var_args return location for first field, followed by list of type/location pairs
- * @returns result code
+ * @param value the array
+ * @param len the length of the array
+ * @returns #TRUE on success
  */
-DBusResultCode
-dbus_message_get_fields_valist (DBusMessage *message,
-                                int          first_field_type,
-                               va_list      var_args)
+dbus_bool_t
+dbus_message_append_double_array (DBusMessage  *message,
+                                 const double *value,
+                                 int           len)
 {
-  int spec_type, msg_type, i;
-  DBusMessageIter *iter;
+  _dbus_assert (!message->locked);
 
-  iter = dbus_message_get_fields_iter (message);
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DOUBLE_ARRAY))
+    return FALSE;
 
-  if (iter == NULL)
-    return DBUS_RESULT_NO_MEMORY;
-  
-  spec_type = first_field_type;
-  i = 0;
-  
-  while (spec_type != 0)
+  if (!_dbus_marshal_double_array (&message->body, message->byte_order,
+                                  value, len))
     {
-      msg_type = dbus_message_iter_get_field_type (iter);      
-      
-      if (msg_type != spec_type)
-       {
-         _dbus_verbose ("Field %d is specified to be of type \"%s\", but "
-                        "is actually of type \"%s\"\n", i,
-                        _dbus_type_to_string (spec_type),
-                        _dbus_type_to_string (msg_type));
-         dbus_message_iter_unref (iter);
-
-         return DBUS_RESULT_INVALID_FIELDS;
-       }
+      _dbus_string_shorten (&message->body, 1);
+      return FALSE;
+    }
 
-      switch (spec_type)
-       {
-       case DBUS_TYPE_INT32:
-         {
-           dbus_int32_t *ptr;
+  return TRUE;
+}
 
-           ptr = va_arg (var_args, dbus_int32_t *);
+/**
+ * Appends a byte array to the message.
+ *
+ * @param message the message
+ * @param value the array
+ * @param len the length of the array
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_append_byte_array (DBusMessage         *message,
+                               unsigned const char *value,
+                               int                 len)
+{
+  _dbus_assert (!message->locked);
 
-           *ptr = dbus_message_iter_get_int32 (iter);
-           break;
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY))
+    return FALSE;
+  
+  if (!_dbus_marshal_byte_array (&message->body, message->byte_order, value, len))
+    {
+      _dbus_string_shorten (&message->body, 1);
+      return FALSE;
+    }
+      
+  return TRUE;
+}
+
+/**
+ * Appends a string array to the message.
+ *
+ * @param message the message
+ * @param value the array
+ * @param len the length of the array
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_append_string_array (DBusMessage  *message,
+                                 const char  **value,
+                                 int           len)
+{
+  _dbus_assert (!message->locked);
+
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_STRING_ARRAY))
+    return FALSE;
+
+  if (!_dbus_marshal_string_array (&message->body, message->byte_order,
+                                  (const char **)value, len))
+    {
+      _dbus_string_shorten (&message->body, 1);
+      return FALSE;
+    }
+
+  return TRUE;
+}
+
+/**
+ * Appends a dict to the message.
+ *
+ * @param message the message
+ * @param dict the dict
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_append_dict (DBusMessage *message,
+                         DBusDict    *dict)
+{
+  _dbus_assert (!message->locked);
+
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_DICT))
+    return FALSE;
+
+  if (!_dbus_marshal_dict (&message->body, message->byte_order, dict))
+    {
+      _dbus_string_shorten (&message->body, 1);
+      return FALSE;
+    }
+
+  return TRUE;
+}
+
+/**
+ * Gets arguments from a message given a variable argument list.
+ * The variable argument list should contain the type of the
+ * argumen followed by a pointer to where the value should be
+ * stored. The list is terminated with 0.
+ *
+ * @param message the message
+ * @param error error to be filled in on failure
+ * @param first_arg_type the first argument type
+ * @param ... location for first argument value, then list of type-location pairs
+ * @returns #FALSE if the error was set
+ */
+dbus_bool_t
+dbus_message_get_args (DBusMessage *message,
+                       DBusError   *error,
+                      int          first_arg_type,
+                      ...)
+{
+  dbus_bool_t retval;
+  va_list var_args;
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+  
+  va_start (var_args, first_arg_type);
+  retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
+  va_end (var_args);
+
+  return retval;
+}
+
+/**
+ * This function takes a va_list for use by language bindings
+ *
+ * @todo this function (or some lower-level non-convenience function)
+ * needs better error handling; should allow the application to
+ * distinguish between out of memory, and bad data from the remote
+ * app. It also needs to not leak a bunch of args when it gets
+ * to the arg that's bad, as that would be a security hole
+ * (allow one app to force another to leak memory)
+ *
+ * @todo We need to free the argument data when an error occurs.
+ *
+ * @see dbus_message_get_args
+ * @param message the message
+ * @param error error to be filled in
+ * @param first_arg_type type of the first argument
+ * @param var_args return location for first argument, followed by list of type/location pairs
+ * @returns #FALSE if error was set
+ */
+dbus_bool_t
+dbus_message_get_args_valist (DBusMessage *message,
+                              DBusError   *error,
+                             int          first_arg_type,
+                             va_list      var_args)
+{
+  int spec_type, msg_type, i;
+  DBusMessageIter *iter;
+  dbus_bool_t retval;
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+  
+  iter = dbus_message_get_args_iter (message);
+
+  if (iter == NULL)
+    {
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      return FALSE;
+    }
+
+  retval = FALSE;
+  
+  spec_type = first_arg_type;
+  i = 0;
+  
+  while (spec_type != 0)
+    {
+      msg_type = dbus_message_iter_get_arg_type (iter);      
+      
+      if (msg_type != spec_type)
+       {
+          dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
+                          "Argument %d is specified to be of type \"%s\", but "
+                          "is actually of type \"%s\"\n", i,
+                          _dbus_type_to_string (spec_type),
+                          _dbus_type_to_string (msg_type));
+
+          goto out;
+       }
+
+      switch (spec_type)
+       {
+       case DBUS_TYPE_NIL:
+         break;
+       case DBUS_TYPE_BOOLEAN:
+         {
+           dbus_bool_t *ptr;
+
+           ptr = va_arg (var_args, dbus_bool_t *);
+
+           *ptr = dbus_message_iter_get_boolean (iter);
+           break;
+         }
+       case DBUS_TYPE_INT32:
+         {
+           dbus_int32_t *ptr;
+
+           ptr = va_arg (var_args, dbus_int32_t *);
+
+           *ptr = dbus_message_iter_get_int32 (iter);
+           break;
          }
        case DBUS_TYPE_UINT32:
          {
@@ -1309,12 +1676,15 @@ dbus_message_get_fields_valist (DBusMessage *message,
            *ptr = dbus_message_iter_get_string (iter);
 
            if (!*ptr)
-             return DBUS_RESULT_NO_MEMORY;
+              {
+                dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+                goto out;
+              }
            
            break;
          }
 
-       case DBUS_TYPE_BYTE_ARRAY:
+       case DBUS_TYPE_BOOLEAN_ARRAY:
          {
            unsigned char **ptr;
            int *len;
@@ -1322,13 +1692,79 @@ dbus_message_get_fields_valist (DBusMessage *message,
            ptr = va_arg (var_args, unsigned char **);
            len = va_arg (var_args, int *);
 
-           *ptr = dbus_message_iter_get_byte_array (iter, len);
+           if (!dbus_message_iter_get_boolean_array (iter, ptr, len))
+              {
+                dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+                goto out;
+              }
+           break;
+         }
+         
+       case DBUS_TYPE_INT32_ARRAY:
+         {
+           dbus_int32_t **ptr;
+           int *len;
 
-           if (!*ptr)
-             return DBUS_RESULT_NO_MEMORY;
+           ptr = va_arg (var_args, dbus_int32_t **);
+           len = va_arg (var_args, int *);
+
+           if (!dbus_message_iter_get_int32_array (iter, ptr, len))
+              {
+                dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+                goto out;
+              }
            
            break;
          }
+
+       case DBUS_TYPE_UINT32_ARRAY:
+         {
+           dbus_uint32_t **ptr;
+           int *len;
+
+           ptr = va_arg (var_args, dbus_uint32_t **);
+           len = va_arg (var_args, int *);
+
+           if (!dbus_message_iter_get_uint32_array (iter, ptr, len))
+              {
+                dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+                goto out;
+              }
+           
+           break;
+         }
+
+       case DBUS_TYPE_DOUBLE_ARRAY:
+         {
+           double **ptr;
+           int *len;
+
+           ptr = va_arg (var_args, double **);
+           len = va_arg (var_args, int *);
+
+           if (!dbus_message_iter_get_double_array (iter, ptr, len))
+              {
+                dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+                goto out;
+              }
+           break;
+         }
+         
+       case DBUS_TYPE_BYTE_ARRAY:
+         {
+           unsigned char **ptr;
+           int *len;
+
+           ptr = va_arg (var_args, unsigned char **);
+           len = va_arg (var_args, int *);
+
+           if (!dbus_message_iter_get_byte_array (iter, ptr, len))
+              {
+                dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+                goto out;
+              }
+           break;
+         }
        case DBUS_TYPE_STRING_ARRAY:
          {
            char ***ptr;
@@ -1337,11 +1773,24 @@ dbus_message_get_fields_valist (DBusMessage *message,
            ptr = va_arg (var_args, char ***);
            len = va_arg (var_args, int *);
 
-           *ptr = dbus_message_iter_get_string_array (iter, len);
-           
-           if (!*ptr)
-             return DBUS_RESULT_NO_MEMORY;
-           
+           if (!dbus_message_iter_get_string_array (iter, ptr, len))
+              {
+                dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+                goto out;
+              }
+           break;
+         }
+       case DBUS_TYPE_DICT:
+         {
+           DBusDict **dict;
+
+           dict = va_arg (var_args, DBusDict **);
+
+           if (!dbus_message_iter_get_dict (iter, dict))
+              {
+                dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+                goto out;
+              }
            break;
          }
        default:          
@@ -1350,21 +1799,24 @@ dbus_message_get_fields_valist (DBusMessage *message,
       
       spec_type = va_arg (var_args, int);
       if (spec_type != 0 && !dbus_message_iter_next (iter))
-       {
-         _dbus_verbose ("More fields than exist in the message were specified or field is corrupt\n");
+        {
+          dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
+                          "Message has only %d arguments, but more were expected", i);
+          goto out;
+        }
 
-         dbus_message_iter_unref (iter);  
-         return DBUS_RESULT_INVALID_FIELDS;
-       }
       i++;
     }
-
+  
+  retval = TRUE;
+  
+ out:
   dbus_message_iter_unref (iter);
-  return DBUS_RESULT_SUCCESS;
+  return retval;
 }
 
 /**
- * Returns a DBusMessageIter representing the fields of the
+ * Returns a DBusMessageIter representing the arguments of the
  * message passed in.
  *
  * @todo IMO the message iter should follow the GtkTextIter pattern,
@@ -1373,24 +1825,29 @@ dbus_message_get_fields_valist (DBusMessage *message,
  * ref/unref is kind of annoying to deal with, and slower too.
  * This implies not ref'ing the message from the iter.
  *
- * @todo rename get_args_iter to avoid confusion with header fields
- * 
+ * @todo I'd also name this dbus_message_iter_new() or
+ * for the static object dbus_message_iter_init() rather
+ * than making it a method on the message
+ *
  * @param message the message
  * @returns a new iter.
  */
 DBusMessageIter *
-dbus_message_get_fields_iter (DBusMessage *message)
+dbus_message_get_args_iter (DBusMessage *message)
 {
   DBusMessageIter *iter;
   
   iter = dbus_new (DBusMessageIter, 1);
 
-  dbus_message_ref (message);
+  if (iter != NULL)
+    {
+      dbus_message_ref (message);
+  
+      iter->refcount = 1;
+      iter->message = message;
+      iter->pos = 0;
+    }
   
-  iter->refcount = 1;
-  iter->message = message;
-  iter->pos = 0;
-
   return iter;
 }
 
@@ -1477,23 +1934,23 @@ dbus_message_iter_next (DBusMessageIter *iter)
 }
 
 /**
- * Returns the field type of the field that the
+ * Returns the argument type of the argument that the
  * message iterator points at.
  *
  * @param iter the message iter
  * @returns the field type
  */
 int
-dbus_message_iter_get_field_type (DBusMessageIter *iter)
+dbus_message_iter_get_arg_type (DBusMessageIter *iter)
 {
   const char *data;
 
   if (iter->pos >= _dbus_string_get_length (&iter->message->body))
     return DBUS_TYPE_INVALID;
 
-  _dbus_string_get_const_data_len (&iter->message->body, &data, iter->pos, 1);
+  data = _dbus_string_get_const_data_len (&iter->message->body, iter->pos, 1);
 
-  if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_STRING_ARRAY)
+  if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_DICT)
     return *data;
 
   return DBUS_TYPE_INVALID;
@@ -1504,31 +1961,54 @@ dbus_message_iter_get_field_type (DBusMessageIter *iter)
  * Note that you need to check that the iterator points to
  * a string value before using this function.
  *
- * @see dbus_message_iter_get_field_type
+ * @see dbus_message_iter_get_arg_type
  * @param iter the message iter
  * @returns the string
  */
 char *
 dbus_message_iter_get_string (DBusMessageIter *iter)
 {
-  _dbus_assert (dbus_message_iter_get_field_type (iter) == DBUS_TYPE_STRING);
+  _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING);
 
   return _dbus_demarshal_string (&iter->message->body, iter->message->byte_order,
                                  iter->pos + 1, NULL);
 }
 
 /**
+ * Returns the boolean value that an iterator may point to.
+ * Note that you need to check that the iterator points to
+ * a boolean value before using this function.
+ *
+ * @see dbus_message_iter_get_arg_type
+ * @param iter the message iter
+ * @returns the string
+ */
+dbus_bool_t
+dbus_message_iter_get_boolean (DBusMessageIter *iter)
+{
+  unsigned char value;
+  
+  _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN);
+
+  value = _dbus_string_get_byte (&iter->message->body, iter->pos + 1);
+  
+  return value;
+}
+
+/**
  * Returns the 32 bit signed integer value that an iterator may point to.
  * Note that you need to check that the iterator points to
- * a string value before using this function.
+ * an integer value before using this function.
  *
- * @see dbus_message_iter_get_field_type
+ * @see dbus_message_iter_get_arg_type
  * @param iter the message iter
  * @returns the integer
  */
 int
 dbus_message_iter_get_int32 (DBusMessageIter *iter)
 {
+  _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32);
+  
   return _dbus_demarshal_int32 (&iter->message->body, iter->message->byte_order,
                                iter->pos + 1, NULL);
 }
@@ -1536,15 +2016,17 @@ dbus_message_iter_get_int32 (DBusMessageIter *iter)
 /**
  * Returns the 32 bit unsigned integer value that an iterator may point to.
  * Note that you need to check that the iterator points to
- * a string value before using this function.
+ * an unsigned integer value before using this function.
  *
- * @see dbus_message_iter_get_field_type
+ * @see dbus_message_iter_get_arg_type
  * @param iter the message iter
  * @returns the integer
  */
 int
 dbus_message_iter_get_uint32 (DBusMessageIter *iter)
 {
+  _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32);
+  
   return _dbus_demarshal_uint32 (&iter->message->body, iter->message->byte_order,
                                 iter->pos + 1, NULL);
 }
@@ -1554,37 +2036,137 @@ dbus_message_iter_get_uint32 (DBusMessageIter *iter)
  * Note that you need to check that the iterator points to
  * a string value before using this function.
  *
- * @see dbus_message_iter_get_field_type
+ * @see dbus_message_iter_get_arg_type
  * @param iter the message iter
  * @returns the double
  */
 double
 dbus_message_iter_get_double (DBusMessageIter *iter)
 {
+  _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE);
+  
   return _dbus_demarshal_double (&iter->message->body, iter->message->byte_order,
                                 iter->pos + 1, NULL);
 }
 
 /**
+ * Returns the boolean array that the iterator may point to. Note that
+ * you need to check that the iterator points to an array of the
+ * correct type prior to using this function.
+ *
+ * @param iter the iterator
+ * @param value return location for the array
+ * @param len return location for the array length
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_iter_get_boolean_array (DBusMessageIter   *iter,
+                                    unsigned char    **value,
+                                    int               *len)
+{
+  _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BOOLEAN_ARRAY);
+
+  if (!_dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
+                                  iter->pos + 1, NULL, value, len))
+    return FALSE;
+  else
+    return TRUE;
+}
+
+/**
+ * Returns the 32 bit signed integer array that the iterator may point
+ * to. Note that you need to check that the iterator points to an
+ * array of the correct type prior to using this function.
+ *
+ * @param iter the iterator
+ * @param value return location for the array
+ * @param len return location for the array length
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_iter_get_int32_array  (DBusMessageIter *iter,
+                                   dbus_int32_t   **value,
+                                   int             *len)
+{
+  _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32_ARRAY);
+
+  if (!_dbus_demarshal_int32_array (&iter->message->body, iter->message->byte_order,
+                                   iter->pos + 1, NULL, value, len))
+    return FALSE;
+  else
+    return TRUE;
+}
+
+/**
+ * Returns the 32 bit unsigned integer array that the iterator may point
+ * to. Note that you need to check that the iterator points to an
+ * array of the correct type prior to using this function.
+ *
+ * @param iter the iterator
+ * @param value return location for the array
+ * @param len return location for the array length
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_iter_get_uint32_array  (DBusMessageIter *iter,
+                                    dbus_uint32_t  **value,
+                                    int             *len)
+{
+  _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UINT32_ARRAY);
+
+  if (!_dbus_demarshal_uint32_array (&iter->message->body, iter->message->byte_order,
+                                    iter->pos + 1, NULL, value, len))
+    return FALSE;
+  else
+    return TRUE;
+}
+
+/**
+ * Returns the double array that the iterator may point to. Note that
+ * you need to check that the iterator points to an array of the
+ * correct type prior to using this function.
+ *
+ * @param iter the iterator
+ * @param value return location for the array
+ * @param len return location for the array length
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_iter_get_double_array  (DBusMessageIter *iter,
+                                    double         **value,
+                                    int             *len)
+{
+  _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DOUBLE_ARRAY);
+
+  if (!_dbus_demarshal_double_array (&iter->message->body, iter->message->byte_order,
+                                    iter->pos + 1, NULL, value, len))
+    return FALSE;
+  else
+    return TRUE;
+}
+
+/**
  * Returns the byte array that the iterator may point to.
  * Note that you need to check that the iterator points
  * to a byte array prior to using this function.
  *
- * @todo this function should probably take "unsigned char **" as
- * an out param argument, and return boolean or result code.
- *
  * @param iter the iterator
+ * @param value return location for array values
  * @param len return location for length of byte array
- * @returns the byte array
+ * @returns #TRUE on success
  */
-unsigned char *
-dbus_message_iter_get_byte_array (DBusMessageIter *iter,
-                                  int             *len)
+dbus_bool_t
+dbus_message_iter_get_byte_array (DBusMessageIter  *iter,
+                                 unsigned char   **value,
+                                  int              *len)
 {
-  _dbus_assert (dbus_message_iter_get_field_type (iter) == DBUS_TYPE_BYTE_ARRAY);
+  _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_BYTE_ARRAY);
 
-  return _dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
-                                    iter->pos + 1, NULL, len);
+  if (!_dbus_demarshal_byte_array (&iter->message->body, iter->message->byte_order,
+                                  iter->pos + 1, NULL, value, len))
+    return FALSE;
+  else
+    return TRUE;
 }
 
 /**
@@ -1592,21 +2174,50 @@ dbus_message_iter_get_byte_array (DBusMessageIter *iter,
  * Note that you need to check that the iterator points
  * to a byte array prior to using this function.
  *
- * @todo this function should probably take "char **" as
- * an out param argument, and return boolean or result code.
+ * The returned value is a #NULL-terminated array of strings.
+ * Each string is a separate malloc block, and the array
+ * itself is a malloc block. You can free this type of
+ * string array with dbus_free_string_array().
  *
  * @param iter the iterator
+ * @param value return location for string values
  * @param len return location for length of byte array
- * @returns the byte array
+ * @returns #TRUE on success
  */
-char **
+dbus_bool_t
 dbus_message_iter_get_string_array (DBusMessageIter *iter,
+                                   char          ***value,
                                    int             *len)
 {
-  _dbus_assert (dbus_message_iter_get_field_type (iter) == DBUS_TYPE_STRING_ARRAY);
+  _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING_ARRAY);
+
+  if (!_dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
+                                    iter->pos + 1, NULL, value, len))
+    return FALSE;
+  else
+    return TRUE;
+}
+
+/**
+ * Returns the dict that the iterator may point to.
+ * Note that you need to check that the iterator points
+ * to a dict prior to using this function.
+ *
+ * @param iter the iterator
+ * @param dict return location for dict
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_iter_get_dict (DBusMessageIter *iter,
+                           DBusDict       **dict)
+{
+  _dbus_assert (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_DICT);
 
-  return _dbus_demarshal_string_array (&iter->message->body, iter->message->byte_order,
-                                      iter->pos + 1, NULL, len);
+  if (!_dbus_demarshal_dict (&iter->message->body, iter->message->byte_order,
+                            iter->pos + 1, NULL, dict))
+    return FALSE;
+  else
+    return TRUE;
 }
 
 /**
@@ -1650,7 +2261,7 @@ dbus_message_set_is_error (DBusMessage *message,
   
   _dbus_assert (!message->locked);
   
-  _dbus_string_get_data_len (&message->header, &header, 1, 1);
+  header = _dbus_string_get_data_len (&message->header, 1, 1);
   
   if (is_error_reply)
     *header |= DBUS_HEADER_FLAG_ERROR;
@@ -1670,7 +2281,7 @@ dbus_message_get_is_error (DBusMessage *message)
 {
   const char *header;
 
-  _dbus_string_get_const_data_len (&message->header, &header, 1, 1);
+  header = _dbus_string_get_const_data_len (&message->header, 1, 1);
 
   return (*header & DBUS_HEADER_FLAG_ERROR) != 0;
 }
@@ -1688,6 +2299,88 @@ dbus_message_get_sender (DBusMessage *message)
   return get_string_field (message, FIELD_SENDER, NULL);
 }
 
+/**
+ * Checks whether the message has the given name.
+ * If the message has no name or has a different
+ * name, returns #FALSE.
+ *
+ * @param message the message
+ * @param name the name to check (must not be #NULL)
+ * 
+ * @returns #TRUE if the message has the given name
+ */
+dbus_bool_t
+dbus_message_name_is (DBusMessage *message,
+                     const char  *name)
+{
+  const char *n;
+
+  _dbus_assert (name != NULL);
+  
+  n = dbus_message_get_name (message);
+
+  if (n && strcmp (n, name) == 0)
+    return TRUE;
+  else
+    return FALSE;
+}
+
+/**
+ * Checks whether the message was sent to the given service.  If the
+ * message has no service specified or has a different name, returns
+ * #FALSE.
+ *
+ * @param message the message
+ * @param service the service to check (must not be #NULL)
+ * 
+ * @returns #TRUE if the message has the given destination service
+ */
+dbus_bool_t
+dbus_message_service_is (DBusMessage  *message,
+                         const char   *service)
+{
+  const char *s;
+
+  _dbus_assert (service != NULL);
+  
+  s = dbus_message_get_service (message);
+
+  if (s && strcmp (s, service) == 0)
+    return TRUE;
+  else
+    return FALSE;
+}
+
+/**
+ * Checks whether the message has the given service as its sender.  If
+ * the message has no sender specified or has a different sender,
+ * returns #FALSE. Note that if a peer application owns multiple
+ * services, its messages will have only one of those services as the
+ * sender (usually the base service). So you can't use this
+ * function to prove the sender didn't own service Foo, you can
+ * only use it to prove that it did.
+ *
+ * @param message the message
+ * @param service the service to check (must not be #NULL)
+ * 
+ * @returns #TRUE if the message has the given origin service
+ */
+dbus_bool_t
+dbus_message_sender_is (DBusMessage  *message,
+                        const char   *service)
+{
+  const char *s;
+
+  _dbus_assert (service != NULL);
+  
+  s = dbus_message_get_sender (message);
+
+  if (s && strcmp (s, service) == 0)
+    return TRUE;
+  else
+    return FALSE;
+}
+
 /** @} */
 
 /**
@@ -1705,6 +2398,10 @@ dbus_message_get_sender (DBusMessage *message)
  * DBusTransport implementation. The DBusTransport then hands off
  * the loaded messages to a DBusConnection, making the messages
  * visible to the application.
+ *
+ * @todo write tests for break-loader that a) randomly delete header
+ * fields and b) set string fields to zero-length and other funky
+ * values.
  * 
  */
 
@@ -1771,7 +2468,7 @@ _dbus_message_loader_new (void)
    */
   loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
   
-  if (!_dbus_string_init (&loader->data, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&loader->data))
     {
       dbus_free (loader);
       return NULL;
@@ -1828,6 +2525,8 @@ _dbus_message_loader_unref (DBusMessageLoader *loader)
  * it can probably always return a buffer size to read exactly
  * the body of the next message, thus avoiding any memory wastage
  * or reallocs.
+ *
+ * @todo we need to enforce a max length on strings in header fields.
  * 
  * @param loader the message loader.
  * @param buffer the buffer
@@ -1872,11 +2571,13 @@ _dbus_message_loader_get_buffer (DBusMessageLoader  *loader,
 #define DBUS_HEADER_FIELD_SENDER_AS_UINT32  \
   FOUR_CHARS_TO_UINT32 ('s', 'n', 'd', 'r')
 
+/* FIXME impose max length on name, srvc, sndr */
 static dbus_bool_t
 decode_header_data (const DBusString   *data,
                    int                 header_len,
                    int                 byte_order,
-                    HeaderField         fields[FIELD_LAST])
+                    HeaderField         fields[FIELD_LAST],
+                   int                *message_padding)
 {
   const char *field;
   int pos, new_pos;
@@ -1910,7 +2611,7 @@ decode_header_data (const DBusString   *data,
       if ((pos + 4) > header_len)
         return FALSE;      
       
-      _dbus_string_get_const_data_len (data, &field, pos, 4);
+      field =_dbus_string_get_const_data_len (data, pos, 4);
       pos += 4;
 
       _dbus_assert (_DBUS_ALIGN_ADDRESS (field, 4) == field);
@@ -1929,13 +2630,15 @@ decode_header_data (const DBusString   *data,
             }
           
           fields[FIELD_SERVICE].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
+#if 0
           _dbus_verbose ("Found service name at offset %d\n",
                          fields[FIELD_SERVICE].offset);
+#endif
           break;
 
         case DBUS_HEADER_FIELD_NAME_AS_UINT32:
           if (fields[FIELD_NAME].offset >= 0)
-            {
+            {              
               _dbus_verbose ("%s field provided twice\n",
                              DBUS_HEADER_FIELD_NAME);
               return FALSE;
@@ -1943,8 +2646,10 @@ decode_header_data (const DBusString   *data,
           
           fields[FIELD_NAME].offset = _DBUS_ALIGN_VALUE (pos + 1, 4);
 
+#if 0
           _dbus_verbose ("Found message name at offset %d\n",
                          fields[FIELD_NAME].offset);
+#endif
           break;
        case DBUS_HEADER_FIELD_SENDER_AS_UINT32:
           if (fields[FIELD_SENDER].offset >= 0)
@@ -1975,15 +2680,21 @@ decode_header_data (const DBusString   *data,
          break;
 
         default:
-         _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c\n",
-                        field[0], field[1], field[2], field[3]);
+         _dbus_verbose ("Ignoring an unknown header field: %c%c%c%c at offset %d\n",
+                        field[0], field[1], field[2], field[3], pos);
        }
 
       if (!_dbus_marshal_validate_arg (data, byte_order, pos, &new_pos))
-        return FALSE;
+        {
+          _dbus_verbose ("Failed to validate argument to named header field\n");
+          return FALSE;
+        }
 
       if (new_pos > header_len)
-       return FALSE;
+        {
+          _dbus_verbose ("Named header field tries to extend beyond header length\n");
+          return FALSE;
+        }
       
       pos = new_pos;
     }
@@ -2000,6 +2711,16 @@ decode_header_data (const DBusString   *data,
           return FALSE;
         }
     }
+
+ if (fields[FIELD_NAME].offset < 0)
+   {
+     _dbus_verbose ("No %s field provided\n",
+                    DBUS_HEADER_FIELD_NAME);
+     return FALSE;
+   }
+  
+  if (message_padding)
+    *message_padding = header_len - pos;  
   
   return TRUE;
 }
@@ -2023,18 +2744,28 @@ _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
   _dbus_assert (buffer == &loader->data);
 
   loader->buffer_outstanding = FALSE;
+}
 
+/**
+ * Converts buffered data into messages.
+ *
+ * @param loader the loader.
+ * @returns #TRUE if we had enough memory to finish.
+ */
+dbus_bool_t
+_dbus_message_loader_queue_messages (DBusMessageLoader *loader)
+{
   if (loader->corrupted)
-    return;
+    return TRUE;
 
   while (_dbus_string_get_length (&loader->data) >= 16)
     {
       DBusMessage *message;      
       const char *header_data;
-      int byte_order, header_len, body_len;
+      int byte_order, header_len, body_len, header_padding;
       dbus_uint32_t header_len_unsigned, body_len_unsigned;
       
-      _dbus_string_get_const_data_len (&loader->data, &header_data, 0, 16);
+      header_data = _dbus_string_get_const_data_len (&loader->data, 0, 16);
 
       _dbus_assert (_DBUS_ALIGN_ADDRESS (header_data, 4) == header_data);
 
@@ -2043,7 +2774,7 @@ _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
           _dbus_verbose ("Message has protocol version %d ours is %d\n",
                          (int) header_data[2], DBUS_MAJOR_PROTOCOL_VERSION);
           loader->corrupted = TRUE;
-          return;
+          return TRUE;
         }
       
       byte_order = header_data[0];
@@ -2054,7 +2785,7 @@ _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
           _dbus_verbose ("Message with bad byte order '%c' received\n",
                          byte_order);
          loader->corrupted = TRUE;
-         return;
+         return TRUE;
        }
 
       header_len_unsigned = _dbus_unpack_uint32 (byte_order, header_data + 4);
@@ -2065,7 +2796,7 @@ _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
           _dbus_verbose ("Message had broken too-small header length %u\n",
                          header_len_unsigned);
           loader->corrupted = TRUE;
-          return;
+          return TRUE;
         }
 
       if (header_len_unsigned > (unsigned) MAX_SANE_MESSAGE_SIZE ||
@@ -2075,29 +2806,30 @@ _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
                          header_len_unsigned,
                          body_len_unsigned);
           loader->corrupted = TRUE;
-          return;
-        }      
+          return TRUE;
+        }
 
       /* Now that we know the values are in signed range, get
        * rid of stupid unsigned, just causes bugs
        */
       header_len = header_len_unsigned;
       body_len = body_len_unsigned;
-      
+
       if (_DBUS_ALIGN_VALUE (header_len, 8) != header_len_unsigned)
         {
+         
           _dbus_verbose ("header length %d is not aligned to 8 bytes\n",
                          header_len);
           loader->corrupted = TRUE;
-          return;
+          return TRUE;
         }
       
       if (header_len + body_len > loader->max_message_size)
        {
-          _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %d\n",
+          _dbus_verbose ("Message claimed length header = %d body = %d exceeds max message length %ld\n",
                          header_len, body_len, loader->max_message_size);
          loader->corrupted = TRUE;
-         return;
+         return TRUE;
        }
 
       if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
@@ -2106,11 +2838,15 @@ _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
           int i;
           int next_arg;          
 
+#if 0
+         _dbus_verbose_bytes_of_string (&loader->data, 0, header_len + body_len);
+#endif   
          if (!decode_header_data (&loader->data, header_len, byte_order,
-                                   fields))
+                                   fields, &header_padding))
            {
+              _dbus_verbose ("Header was invalid\n");
              loader->corrupted = TRUE;
-             return;
+             return TRUE;
            }
           
           next_arg = header_len;
@@ -2124,7 +2860,7 @@ _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
                                                &next_arg))
                 {
                   loader->corrupted = TRUE;
-                  return;
+                  return TRUE;
                 }
 
               _dbus_assert (next_arg > prev);
@@ -2136,15 +2872,16 @@ _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
                              next_arg, header_len, body_len,
                              header_len + body_len);
               loader->corrupted = TRUE;
-              return;
+              return TRUE;
             }
 
          message = dbus_message_new_empty_header ();
          if (message == NULL)
-            break; /* ugh, postpone this I guess. */
+            return FALSE;
 
           message->byte_order = byte_order;
-          
+          message->header_padding = header_padding;
+         
           /* Copy in the offsets we found */
           i = 0;
           while (i < FIELD_LAST)
@@ -2156,7 +2893,7 @@ _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
          if (!_dbus_list_append (&loader->messages, message))
             {
               dbus_message_unref (message);
-              break;
+              return FALSE;
             }
 
           _dbus_assert (_dbus_string_get_length (&message->header) == 0);
@@ -2169,7 +2906,7 @@ _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
             {
               _dbus_list_remove_last (&loader->messages, message);
               dbus_message_unref (message);
-              break;
+              return FALSE;
             }
           
          if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
@@ -2183,23 +2920,47 @@ _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
 
               _dbus_list_remove_last (&loader->messages, message);
               dbus_message_unref (message);
-              break;
+              return FALSE;
             }
 
           _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
           _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
 
+          /* Fill in caches */
+          message->reply_serial = get_int_field (message,
+                                                 FIELD_REPLY_SERIAL);
+          message->client_serial = get_int_field (message,
+                                                  FIELD_CLIENT_SERIAL);
+          
          _dbus_verbose ("Loaded message %p\n", message);
        }
       else
-       break;
+        return TRUE;
     }
+
+  return TRUE;
+}
+
+/**
+ * Peeks at first loaded message, returns #NULL if no messages have
+ * been queued.
+ *
+ * @param loader the loader.
+ * @returns the next message, or #NULL if none.
+ */
+DBusMessage*
+_dbus_message_loader_peek_message (DBusMessageLoader *loader)
+{
+  if (loader->messages)
+    return loader->messages->data;
+  else
+    return NULL;
 }
 
 /**
  * Pops a loaded message (passing ownership of the message
  * to the caller). Returns #NULL if no messages have been
- * loaded.
+ * queued.
  *
  * @param loader the loader.
  * @returns the next message, or #NULL if none.
@@ -2210,6 +2971,19 @@ _dbus_message_loader_pop_message (DBusMessageLoader *loader)
   return _dbus_list_pop_first (&loader->messages);
 }
 
+/**
+ * Pops a loaded message inside a list link (passing ownership of the
+ * message and link to the caller). Returns #NULL if no messages have
+ * been loaded.
+ *
+ * @param loader the loader.
+ * @returns the next message link, or #NULL if none.
+ */
+DBusList*
+_dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
+{
+  return _dbus_list_pop_first_link (&loader->messages);
+}
 
 /**
  * Checks whether the loader is confused due to bad data.
@@ -2268,11 +3042,11 @@ message_iter_test (DBusMessage *message)
   DBusMessageIter *iter;
   char *str;
   
-  iter = dbus_message_get_fields_iter (message);
+  iter = dbus_message_get_args_iter (message);
 
   /* String tests */
-  if (dbus_message_iter_get_field_type (iter) != DBUS_TYPE_STRING)
-    _dbus_assert_not_reached ("Field type isn't string");
+  if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_STRING)
+    _dbus_assert_not_reached ("Argument type isn't string");
 
   str = dbus_message_iter_get_string (iter);
   if (strcmp (str, "Test string") != 0)
@@ -2280,11 +3054,11 @@ message_iter_test (DBusMessage *message)
   dbus_free (str);
 
   if (!dbus_message_iter_next (iter))
-    _dbus_assert_not_reached ("Reached end of fields");
+    _dbus_assert_not_reached ("Reached end of arguments");
 
   /* Signed integer tests */
-  if (dbus_message_iter_get_field_type (iter) != DBUS_TYPE_INT32)
-    _dbus_assert_not_reached ("Field type isn't int32");
+  if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_INT32)
+    _dbus_assert_not_reached ("Argument type isn't int32");
 
   if (dbus_message_iter_get_int32 (iter) != -0x12345678)
     _dbus_assert_not_reached ("Signed integers differ");
@@ -2293,24 +3067,24 @@ message_iter_test (DBusMessage *message)
     _dbus_assert_not_reached ("Reached end of fields");
   
   /* Unsigned integer tests */
-  if (dbus_message_iter_get_field_type (iter) != DBUS_TYPE_UINT32)
-    _dbus_assert_not_reached ("Field type isn't int32");
+  if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_UINT32)
+    _dbus_assert_not_reached ("Argument type isn't int32");
 
-  if (dbus_message_iter_get_int32 (iter) != 0xedd1e)
+  if (dbus_message_iter_get_uint32 (iter) != 0xedd1e)
     _dbus_assert_not_reached ("Unsigned integers differ");
 
   if (!dbus_message_iter_next (iter))
-    _dbus_assert_not_reached ("Reached end of fields");
+    _dbus_assert_not_reached ("Reached end of arguments");
 
   /* Double tests */
-  if (dbus_message_iter_get_field_type (iter) != DBUS_TYPE_DOUBLE)
-    _dbus_assert_not_reached ("Field type isn't double");
+  if (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_DOUBLE)
+    _dbus_assert_not_reached ("Argument type isn't double");
 
   if (dbus_message_iter_get_double (iter) != 3.14159)
     _dbus_assert_not_reached ("Doubles differ");
 
   if (dbus_message_iter_next (iter))
-    _dbus_assert_not_reached ("Didn't reach end of fields");
+    _dbus_assert_not_reached ("Didn't reach end of arguments");
   
   dbus_message_iter_unref (iter);
 }
@@ -2326,27 +3100,38 @@ check_message_handling (DBusMessage *message)
   retval = FALSE;
   iter = NULL;
   
-  client_serial = _dbus_message_get_client_serial (message);
+  client_serial = dbus_message_get_serial (message);
 
-  /* can't use set_client_serial due to the assertions at the start of it */
+  /* can't use set_serial due to the assertions at the start of it */
   set_int_field (message, FIELD_CLIENT_SERIAL,
                  client_serial);
-
-  if (client_serial != _dbus_message_get_client_serial (message))
+  
+  if (client_serial != dbus_message_get_serial (message))
     {
       _dbus_warn ("get/set cycle for client_serial did not succeed\n");
       goto failed;
     }
   
-  /* If we implement message_set_field (message, n, value)
+  /* If we implement message_set_arg (message, n, value)
    * then we would want to test it here
    */
-  
-  iter = dbus_message_get_fields_iter (message);
-  while ((type = dbus_message_iter_get_field_type (iter)) != DBUS_TYPE_INVALID)
+
+  iter = dbus_message_get_args_iter (message);
+  while ((type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID)
     {
       switch (type)
         {
+       case DBUS_TYPE_NIL:
+         break;
+       case DBUS_TYPE_INT32:
+         dbus_message_iter_get_int32 (iter);
+         break;
+       case DBUS_TYPE_UINT32:
+         dbus_message_iter_get_uint32 (iter);
+         break;
+       case DBUS_TYPE_DOUBLE:
+         dbus_message_iter_get_double (iter);
+         break;
         case DBUS_TYPE_STRING:
           {
             char *str;
@@ -2354,6 +3139,74 @@ check_message_handling (DBusMessage *message)
             dbus_free (str);
           }
           break;
+        case DBUS_TYPE_BOOLEAN_ARRAY:
+          {
+           unsigned char *values;
+           int len;
+           
+            if (!dbus_message_iter_get_boolean_array (iter, &values, &len))
+             return FALSE;
+
+           dbus_free (values);
+          }
+          break;
+        case DBUS_TYPE_INT32_ARRAY:
+          {
+           dbus_int32_t *values;
+           int len;
+           
+            if (!dbus_message_iter_get_int32_array (iter, &values, &len))
+             return FALSE;
+
+           dbus_free (values);
+          }
+          break;
+        case DBUS_TYPE_UINT32_ARRAY:
+          {
+           dbus_uint32_t *values;
+           int len;
+           
+            if (!dbus_message_iter_get_uint32_array (iter, &values, &len))
+             return FALSE;
+
+           dbus_free (values);
+          }
+          break;
+        case DBUS_TYPE_DOUBLE_ARRAY:
+          {
+           double *values;
+           int len;
+           
+            if (!dbus_message_iter_get_double_array (iter, &values, &len))
+             return FALSE;
+
+           dbus_free (values);
+          }
+         break;
+       case DBUS_TYPE_STRING_ARRAY:
+          {
+           char **values;
+           int len;
+           
+            if (!dbus_message_iter_get_string_array (iter, &values, &len))
+             return FALSE;
+
+           dbus_free_string_array (values);
+          }
+          break;
+
+       case DBUS_TYPE_DICT:
+         {
+           DBusDict *dict;
+
+           if (!dbus_message_iter_get_dict (iter, &dict))
+             return FALSE;
+           dbus_dict_unref (dict);
+         }
+         break;
+
+       default:
+         break;
         }
       
       if (!dbus_message_iter_next (iter))
@@ -2377,6 +3230,9 @@ check_have_valid_message (DBusMessageLoader *loader)
 
   message = NULL;
   retval = FALSE;
+
+  if (!_dbus_message_loader_queue_messages (loader))
+    _dbus_assert_not_reached ("no memory to queue messages");
   
   if (_dbus_message_loader_get_is_corrupted (loader))
     {
@@ -2409,6 +3265,7 @@ check_have_valid_message (DBusMessageLoader *loader)
  failed:
   if (message)
     dbus_message_unref (message);
+
   return retval;
 }
 
@@ -2418,6 +3275,9 @@ check_invalid_message (DBusMessageLoader *loader)
   dbus_bool_t retval;
 
   retval = FALSE;
+
+  if (!_dbus_message_loader_queue_messages (loader))
+    _dbus_assert_not_reached ("no memory to queue messages");
   
   if (!_dbus_message_loader_get_is_corrupted (loader))
     {
@@ -2439,6 +3299,9 @@ check_incomplete_message (DBusMessageLoader *loader)
 
   message = NULL;
   retval = FALSE;
+
+  if (!_dbus_message_loader_queue_messages (loader))
+    _dbus_assert_not_reached ("no memory to queue messages");
   
   if (_dbus_message_loader_get_is_corrupted (loader))
     {
@@ -2461,91 +3324,159 @@ check_incomplete_message (DBusMessageLoader *loader)
   return retval;
 }
 
-typedef enum
-{
-  MESSAGE_VALID,
-  MESSAGE_INVALID,
-  MESSAGE_INCOMPLETE
-} ExpectedMessageValidity;
-
 static dbus_bool_t
 check_loader_results (DBusMessageLoader      *loader,
-                      ExpectedMessageValidity validity)
+                      DBusMessageValidity     validity)
 {
+  if (!_dbus_message_loader_queue_messages (loader))
+    _dbus_assert_not_reached ("no memory to queue messages");
+  
   switch (validity)
     {
-    case MESSAGE_VALID:
+    case _DBUS_MESSAGE_VALID:
       return check_have_valid_message (loader);
-    case MESSAGE_INVALID:
+    case _DBUS_MESSAGE_INVALID:
       return check_invalid_message (loader);
-    case MESSAGE_INCOMPLETE:
+    case _DBUS_MESSAGE_INCOMPLETE:
       return check_incomplete_message (loader);
+    case _DBUS_MESSAGE_UNKNOWN:
+      return TRUE;
     }
 
-  _dbus_assert_not_reached ("bad ExpectedMessageValidity");
+  _dbus_assert_not_reached ("bad DBusMessageValidity");
   return FALSE;
 }
 
-static dbus_bool_t
-try_message (const DBusString       *filename,
-             ExpectedMessageValidity validity)
+
+/**
+ * Loads the message in the given message file.
+ *
+ * @param filename filename to load
+ * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
+ * @param data string to load message into
+ * @returns #TRUE if the message was loaded
+ */
+dbus_bool_t
+dbus_internal_do_not_use_load_message_file (const DBusString    *filename,
+                                            dbus_bool_t          is_raw,
+                                            DBusString          *data)
 {
-  DBusString data;
-  DBusMessageLoader *loader;
   dbus_bool_t retval;
-  int len;
-  int i;
-  const char *filename_c;
 
-  _dbus_string_get_const_data (filename, &filename_c);
-  
-  if (!_dbus_string_ends_with_c_str (filename, ".message"))
+  retval = FALSE;  
+
+  if (is_raw)
     {
-      _dbus_verbose ("Skipping non-.message file %s\n",
-                     filename_c);
-      return TRUE;
+      DBusError error;
+
+      dbus_error_init (&error);
+      if (!_dbus_file_get_contents (data, filename, &error))
+        {
+          _dbus_warn ("Could not load message file %s: %s\n",
+                      _dbus_string_get_const_data (filename),
+                      error.message);
+          dbus_error_free (&error);
+          goto failed;
+        }
+    }
+  else
+    {
+      if (!_dbus_message_data_load (data, filename))
+        {
+          _dbus_warn ("Could not load message file %s\n",
+                      _dbus_string_get_const_data (filename));
+          goto failed;
+        }
     }
 
-  {
-    const char *s;
-    _dbus_string_get_const_data (filename, &s);
-    printf ("    %s\n", s);
-  }
+  retval = TRUE;
   
-  _dbus_verbose (" expecting %s\n",
-                 validity == MESSAGE_VALID ? "valid" :
-                 (validity == MESSAGE_INVALID ? "invalid" : "incomplete"));
+ failed:
+
+  return retval;
+}
+
+/**
+ * Tries loading the message in the given message file
+ * and verifies that DBusMessageLoader can handle it.
+ *
+ * @param filename filename to load
+ * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
+ * @param expected_validity what the message has to be like to return #TRUE
+ * @returns #TRUE if the message has the expected validity
+ */
+dbus_bool_t
+dbus_internal_do_not_use_try_message_file (const DBusString    *filename,
+                                           dbus_bool_t          is_raw,
+                                           DBusMessageValidity  expected_validity)
+{
+  DBusString data;
+  dbus_bool_t retval;
 
-  loader = NULL;
   retval = FALSE;
   
-  if (!_dbus_string_init (&data, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&data))
     _dbus_assert_not_reached ("could not allocate string\n");
-  
-  if (!_dbus_message_data_load (&data, filename))
+
+  if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
+                                                   &data))
+    goto failed;
+
+  retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
+
+ failed:
+
+  if (!retval)
     {
-      const char *s;      
-      _dbus_string_get_const_data (filename, &s);
-      _dbus_warn ("Could not load message file %s\n", s);
-      goto failed;
+      if (_dbus_string_get_length (&data) > 0)
+        _dbus_verbose_bytes_of_string (&data, 0,
+                                       _dbus_string_get_length (&data));
+      
+      _dbus_warn ("Failed message loader test on %s\n",
+                  _dbus_string_get_const_data (filename));
     }
+  
+  _dbus_string_free (&data);
+
+  return retval;
+}
+
+/**
+ * Tries loading the given message data.
+ *
+ *
+ * @param data the message data
+ * @param expected_validity what the message has to be like to return #TRUE
+ * @returns #TRUE if the message has the expected validity
+ */
+dbus_bool_t
+dbus_internal_do_not_use_try_message_data (const DBusString    *data,
+                                           DBusMessageValidity  expected_validity)
+{
+  DBusMessageLoader *loader;
+  dbus_bool_t retval;
+  int len;
+  int i;
+
+  loader = NULL;
+  retval = FALSE;
 
   /* Write the data one byte at a time */
   
   loader = _dbus_message_loader_new ();
 
-  len = _dbus_string_get_length (&data);
+  len = _dbus_string_get_length (data);
   for (i = 0; i < len; i++)
     {
       DBusString *buffer;
 
       _dbus_message_loader_get_buffer (loader, &buffer);
       _dbus_string_append_byte (buffer,
-                                _dbus_string_get_byte (&data, i));
+                                _dbus_string_get_byte (data, i));
       _dbus_message_loader_return_buffer (loader, buffer, 1);
     }
   
-  if (!check_loader_results (loader, validity))
+  if (!check_loader_results (loader, expected_validity))
     goto failed;
 
   _dbus_message_loader_unref (loader);
@@ -2559,12 +3490,12 @@ try_message (const DBusString       *filename,
     DBusString *buffer;
     
     _dbus_message_loader_get_buffer (loader, &buffer);
-    _dbus_string_copy (&data, 0, buffer,
+    _dbus_string_copy (data, 0, buffer,
                        _dbus_string_get_length (buffer));
     _dbus_message_loader_return_buffer (loader, buffer, 1);
   }
   
-  if (!check_loader_results (loader, validity))
+  if (!check_loader_results (loader, expected_validity))
     goto failed;
 
   _dbus_message_loader_unref (loader);
@@ -2574,21 +3505,21 @@ try_message (const DBusString       *filename,
   
   loader = _dbus_message_loader_new ();
 
-  len = _dbus_string_get_length (&data);
+  len = _dbus_string_get_length (data);
   for (i = 0; i < len; i += 2)
     {
       DBusString *buffer;
 
       _dbus_message_loader_get_buffer (loader, &buffer);
       _dbus_string_append_byte (buffer,
-                                _dbus_string_get_byte (&data, i));
+                                _dbus_string_get_byte (data, i));
       if ((i+1) < len)
         _dbus_string_append_byte (buffer,
-                                  _dbus_string_get_byte (&data, i+1));
+                                  _dbus_string_get_byte (data, i+1));
       _dbus_message_loader_return_buffer (loader, buffer, 1);
     }
   
-  if (!check_loader_results (loader, validity))
+  if (!check_loader_results (loader, expected_validity))
     goto failed;
 
   _dbus_message_loader_unref (loader);
@@ -2597,41 +3528,30 @@ try_message (const DBusString       *filename,
   retval = TRUE;
   
  failed:
-  if (!retval)
-    {
-      const char *s;
-
-      if (_dbus_string_get_length (&data) > 0)
-        _dbus_verbose_bytes_of_string (&data, 0,
-                                       _dbus_string_get_length (&data));
-      
-      _dbus_string_get_const_data (filename, &s);
-      _dbus_warn ("Failed message loader test on %s\n",
-                  s);
-    }
   
   if (loader)
     _dbus_message_loader_unref (loader);
-  _dbus_string_free (&data);
   
   return retval;
 }
 
 static dbus_bool_t
-process_test_subdir (const DBusString       *test_base_dir,
-                     const char             *subdir,
-                     ExpectedMessageValidity validity)
+process_test_subdir (const DBusString          *test_base_dir,
+                     const char                *subdir,
+                     DBusMessageValidity        validity,
+                     DBusForeachMessageFileFunc function,
+                     void                      *user_data)
 {
   DBusString test_directory;
   DBusString filename;
   DBusDirIter *dir;
   dbus_bool_t retval;
-  DBusResultCode result;
+  DBusError error;
 
   retval = FALSE;
   dir = NULL;
   
-  if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&test_directory))
     _dbus_assert_not_reached ("didn't allocate test_directory\n");
 
   _dbus_string_init_const (&filename, subdir);
@@ -2641,30 +3561,32 @@ process_test_subdir (const DBusString       *test_base_dir,
     _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
   
   if (!_dbus_concat_dir_and_file (&test_directory, &filename))    
-    _dbus_assert_not_reached ("could't allocate full path");
+    _dbus_assert_not_reached ("couldn't allocate full path");
 
   _dbus_string_free (&filename);
-  if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&filename))
     _dbus_assert_not_reached ("didn't allocate filename string\n");
-  
-  dir = _dbus_directory_open (&test_directory, &result);
+
+  dbus_error_init (&error);
+  dir = _dbus_directory_open (&test_directory, &error);
   if (dir == NULL)
     {
-      const char *s;
-      _dbus_string_get_const_data (&test_directory, &s);
-      _dbus_warn ("Could not open %s: %s\n", s,
-                  dbus_result_to_string (result));
+      _dbus_warn ("Could not open %s: %s\n",
+                  _dbus_string_get_const_data (&test_directory),
+                  error.message);
+      dbus_error_free (&error);
       goto failed;
     }
 
   printf ("Testing:\n");
   
-  result = DBUS_RESULT_SUCCESS;
-  while (_dbus_directory_get_next_file (dir, &filename, &result))
+ next:
+  while (_dbus_directory_get_next_file (dir, &filename, &error))
     {
       DBusString full_path;
-
-      if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
+      dbus_bool_t is_raw;
+      
+      if (!_dbus_string_init (&full_path))
         _dbus_assert_not_reached ("couldn't init string");
 
       if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
@@ -2672,8 +3594,28 @@ process_test_subdir (const DBusString       *test_base_dir,
 
       if (!_dbus_concat_dir_and_file (&full_path, &filename))
         _dbus_assert_not_reached ("couldn't concat file to dir");
+
+      if (_dbus_string_ends_with_c_str (&filename, ".message"))
+        is_raw = FALSE;
+      else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
+        is_raw = TRUE;
+      else
+        {
+          _dbus_verbose ("Skipping non-.message file %s\n",
+                         _dbus_string_get_const_data (&filename));
+         _dbus_string_free (&full_path);
+          goto next;
+        }
+
+      printf ("    %s\n",
+              _dbus_string_get_const_data (&filename));
       
-      if (!try_message (&full_path, validity))
+      _dbus_verbose (" expecting %s\n",
+                     validity == _DBUS_MESSAGE_VALID ? "valid" :
+                     (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
+                      (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")));
+      
+      if (! (*function) (&full_path, is_raw, validity, user_data))
         {
           _dbus_string_free (&full_path);
           goto failed;
@@ -2682,12 +3624,12 @@ process_test_subdir (const DBusString       *test_base_dir,
         _dbus_string_free (&full_path);
     }
 
-  if (result != DBUS_RESULT_SUCCESS)
+  if (dbus_error_is_set (&error))
     {
-      const char *s;
-      _dbus_string_get_const_data (&test_directory, &s);
       _dbus_warn ("Could not get next file in %s: %s\n",
-                  s, dbus_result_to_string (result));
+                  _dbus_string_get_const_data (&test_directory),
+                  error.message);
+      dbus_error_free (&error);
       goto failed;
     }
     
@@ -2703,6 +3645,78 @@ process_test_subdir (const DBusString       *test_base_dir,
   return retval;
 }
                      
+/**
+ * Runs the given function on every message file in the test suite.
+ * The function should return #FALSE on test failure or fatal error.
+ *
+ * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
+ * @param func the function to run
+ * @param user_data data for function
+ * @returns #FALSE if there's a failure
+ */
+dbus_bool_t
+dbus_internal_do_not_use_foreach_message_file (const char                *test_data_dir,
+                                               DBusForeachMessageFileFunc func,
+                                               void                      *user_data)
+{
+  DBusString test_directory;
+  dbus_bool_t retval;
+
+  retval = FALSE;
+  
+  _dbus_string_init_const (&test_directory, test_data_dir);
+
+  if (!process_test_subdir (&test_directory, "valid-messages",
+                            _DBUS_MESSAGE_VALID, func, user_data))
+    goto failed;
+
+  if (!process_test_subdir (&test_directory, "invalid-messages",
+                            _DBUS_MESSAGE_INVALID, func, user_data))
+    goto failed;
+  
+  if (!process_test_subdir (&test_directory, "incomplete-messages",
+                            _DBUS_MESSAGE_INCOMPLETE, func, user_data))
+    goto failed;
+
+  retval = TRUE;
+  
+ failed:
+
+  _dbus_string_free (&test_directory);
+  
+  return retval;
+}
+
+static void
+verify_test_message (DBusMessage *message)
+{
+  dbus_int32_t our_int;
+  char *our_str;
+  double our_double;
+  dbus_bool_t our_bool;
+  
+  if (!dbus_message_get_args (message, NULL,
+                              DBUS_TYPE_INT32, &our_int,
+                              DBUS_TYPE_STRING, &our_str,
+                              DBUS_TYPE_DOUBLE, &our_double,
+                              DBUS_TYPE_BOOLEAN, &our_bool,
+                              0))
+    _dbus_assert_not_reached ("Could not get arguments");
+
+  if (our_int != -0x12345678)
+    _dbus_assert_not_reached ("integers differ!");
+
+  if (our_double != 3.14159)
+    _dbus_assert_not_reached ("doubles differ!");
+
+  if (strcmp (our_str, "Test string") != 0)
+    _dbus_assert_not_reached ("strings differ!");
+
+  if (!our_bool)
+    _dbus_assert_not_reached ("booleans differ");
+  
+  dbus_free (our_str);
+}
 
 /**
  * @ingroup DBusMessageInternals
@@ -2717,47 +3731,51 @@ _dbus_message_test (const char *test_data_dir)
   DBusMessageLoader *loader;
   int i;
   const char *data;
-  dbus_int32_t our_int;
-  char *our_str;
-  double our_double;
-  DBusString test_directory;
-  dbus_bool_t retval;
+  DBusMessage *copy;
+  const char *name1;
+  const char *name2;
   
   /* Test the vararg functions */
   message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
-  _dbus_message_set_client_serial (message, 1);
-  dbus_message_append_fields (message,
-                             DBUS_TYPE_INT32, -0x12345678,
-                             DBUS_TYPE_STRING, "Test string",
-                             DBUS_TYPE_DOUBLE, 3.14159,
-                             0);
+  _dbus_message_set_serial (message, 1);
+  dbus_message_append_args (message,
+                           DBUS_TYPE_INT32, -0x12345678,
+                           DBUS_TYPE_STRING, "Test string",
+                           DBUS_TYPE_DOUBLE, 3.14159,
+                           DBUS_TYPE_BOOLEAN, TRUE,
+                           0);
   _dbus_verbose_bytes_of_string (&message->header, 0,
                                  _dbus_string_get_length (&message->header));
   _dbus_verbose_bytes_of_string (&message->body, 0,
                                  _dbus_string_get_length (&message->body));
+
+  verify_test_message (message);
+
+  copy = dbus_message_copy (message);
+  
+  _dbus_assert (message->client_serial == copy->client_serial);
+  _dbus_assert (message->reply_serial == copy->reply_serial);
+  _dbus_assert (message->header_padding == copy->header_padding);
   
-  if (dbus_message_get_fields (message,
-                              DBUS_TYPE_INT32, &our_int,
-                              DBUS_TYPE_STRING, &our_str,
-                              DBUS_TYPE_DOUBLE, &our_double,
-                              0) != DBUS_RESULT_SUCCESS)
-    _dbus_assert_not_reached ("Could not get fields");
+  _dbus_assert (_dbus_string_get_length (&message->header) ==
+                _dbus_string_get_length (&copy->header));
 
-  if (our_int != -0x12345678)
-    _dbus_assert_not_reached ("integers differ!");
+  _dbus_assert (_dbus_string_get_length (&message->body) ==
+                _dbus_string_get_length (&copy->body));
 
-  if (our_double != 3.14159)
-    _dbus_assert_not_reached ("doubles differ!");
+  verify_test_message (copy);
 
-  if (strcmp (our_str, "Test string") != 0)
-    _dbus_assert_not_reached ("strings differ!");
+  name1 = dbus_message_get_name (message);
+  name2 = dbus_message_get_name (copy);
 
-  dbus_free (our_str);
+  _dbus_assert (strcmp (name1, name2) == 0);
+  
   dbus_message_unref (message);
+  dbus_message_unref (copy);
   
   message = dbus_message_new ("org.freedesktop.DBus.Test", "testMessage");
-  _dbus_message_set_client_serial (message, 1);
-  _dbus_message_set_reply_serial (message, 0x12345678);
+  _dbus_message_set_serial (message, 1);
+  dbus_message_set_reply_serial (message, 0x12345678);
 
   dbus_message_append_string (message, "Test string");
   dbus_message_append_int32 (message, -0x12345678);
@@ -2771,7 +3789,7 @@ _dbus_message_test (const char *test_data_dir)
   loader = _dbus_message_loader_new ();
 
   /* Write the header data one byte at a time */
-  _dbus_string_get_const_data (&message->header, &data);
+  data = _dbus_string_get_const_data (&message->header);
   for (i = 0; i < _dbus_string_get_length (&message->header); i++)
     {
       DBusString *buffer;
@@ -2782,7 +3800,7 @@ _dbus_message_test (const char *test_data_dir)
     }
 
   /* Write the body data one byte at a time */
-  _dbus_string_get_const_data (&message->body, &data);
+  data = _dbus_string_get_const_data (&message->body);
   for (i = 0; i < _dbus_string_get_length (&message->body); i++)
     {
       DBusString *buffer;
@@ -2795,6 +3813,9 @@ _dbus_message_test (const char *test_data_dir)
   dbus_message_unref (message);
 
   /* Now pop back the message */
+  if (!_dbus_message_loader_queue_messages (loader))
+    _dbus_assert_not_reached ("no memory to queue messages");
+  
   if (_dbus_message_loader_get_is_corrupted (loader))
     _dbus_assert_not_reached ("message loader corrupted");
   
@@ -2802,7 +3823,7 @@ _dbus_message_test (const char *test_data_dir)
   if (!message)
     _dbus_assert_not_reached ("received a NULL message");
 
-  if (_dbus_message_get_reply_serial (message) != 0x12345678)
+  if (dbus_message_get_reply_serial (message) != 0x12345678)
     _dbus_assert_not_reached ("reply serial fields differ");
   
   message_iter_test (message);
@@ -2814,28 +3835,10 @@ _dbus_message_test (const char *test_data_dir)
   if (test_data_dir == NULL)
     return TRUE;
 
-  retval = FALSE;
-  
-  _dbus_string_init_const (&test_directory, test_data_dir);
-
-  if (!process_test_subdir (&test_directory, "valid-messages",
-                            MESSAGE_VALID))
-    goto failed;
-  if (!process_test_subdir (&test_directory, "invalid-messages",
-                            MESSAGE_INVALID))
-    goto failed;
-  
-  if (!process_test_subdir (&test_directory, "incomplete-messages",
-                            MESSAGE_INCOMPLETE))
-    goto failed;
-
-  retval = TRUE;
-  
- failed:
-
-  _dbus_string_free (&test_directory);
-  
-  return retval;
+  return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
+                                                        (DBusForeachMessageFileFunc)
+                                                        dbus_internal_do_not_use_try_message_file,
+                                                        NULL);
 }
 
 #endif /* DBUS_BUILD_TESTS */