/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-message.c DBusMessage object
*
- * Copyright (C) 2002, 2003 Red Hat Inc.
+ * Copyright (C) 2002, 2003, 2004 Red Hat Inc.
* Copyright (C) 2002, 2003 CodeFactory AB
*
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.0
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
unsigned int locked : 1; /**< Message being sent, no modifications allowed. */
- DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
+ DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
};
enum {
message->header_padding = 0;
}
+#ifdef DBUS_DISABLE_CHECKS
+#define is_valid_error_name(x) TRUE
+#else
+static dbus_bool_t
+is_valid_error_name (const char *error_name)
+{
+ DBusString the_error_name;
+
+ if (error_name == NULL)
+ return FALSE;
+
+ _dbus_string_init_const (&the_error_name, error_name);
+ return _dbus_string_validate_error_name (&the_error_name, 0,
+ _dbus_string_get_length (&the_error_name));
+}
+#endif
+
static dbus_bool_t
append_header_padding (DBusMessage *message)
{
offset = message->header_fields[field].value_offset;
if (offset < 0)
- return -1; /* useless if -1 is a valid value of course */
+ return 0; /* useless if 0 is a valid value of course */
return _dbus_demarshal_uint32 (&message->header,
message->byte_order,
return FALSE;
}
+/** The maximum number of bytes of overhead to append to a string
+ * (fieldcode + typecode + alignment + length + nul + headerpadding)
+ */
+#define MAX_BYTES_OVERHEAD_TO_APPEND_A_STRING (1 + 1 + 3 + 4 + 1 + 8)
+
static dbus_bool_t
-append_string_field (DBusMessage *message,
- int field,
- int type,
- const char *value)
+append_string_field_len (DBusMessage *message,
+ int field,
+ int type,
+ const char *value,
+ int value_len)
{
_dbus_assert (!message->locked);
message->header_fields[field].value_offset =
_dbus_string_get_length (&message->header);
- if (!_dbus_marshal_string (&message->header, message->byte_order,
- value))
+ if (!_dbus_marshal_string_len (&message->header, message->byte_order,
+ value, value_len))
goto failed;
if (!append_header_padding (message))
return FALSE;
}
-static int
-get_next_field (DBusMessage *message,
- int field)
+static dbus_bool_t
+append_string_field (DBusMessage *message,
+ int field,
+ int type,
+ const char *value)
{
- int offset = message->header_fields[field].name_offset;
- int closest;
- int i;
- int retval = DBUS_HEADER_FIELD_INVALID;
+ int value_len;
- i = 0;
- closest = _DBUS_INT_MAX;
- while (i < DBUS_HEADER_FIELD_LAST)
- {
- if (message->header_fields[i].name_offset > offset &&
- message->header_fields[i].name_offset < closest)
- {
- closest = message->header_fields[i].name_offset;
- retval = i;
- }
- ++i;
- }
+ value_len = strlen (value);
- return retval;
+ return append_string_field_len (message, field, type, value, value_len);
}
-static dbus_bool_t
-re_align_field_recurse (DBusMessage *message,
- int field,
- int offset)
+static int
+get_type_alignment (int type)
{
- int old_name_offset = message->header_fields[field].name_offset;
- int old_value_offset = message->header_fields[field].value_offset;
- int prev_padding, padding, delta;
- int type;
- int next_field;
- int pos = offset;
-
- /* padding between the typecode byte and the value itself */
- prev_padding = old_value_offset - old_name_offset + 2;
-
- pos++;
- type = _dbus_string_get_byte (&message->header, pos);
-
- pos++;
+ int alignment;
+
switch (type)
{
case DBUS_TYPE_NIL:
case DBUS_TYPE_BYTE:
case DBUS_TYPE_BOOLEAN:
- padding = 0;
+ alignment = 0;
break;
+
case DBUS_TYPE_INT32:
case DBUS_TYPE_UINT32:
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
- padding = _DBUS_ALIGN_VALUE (pos, 4) - pos;
+ /* These are aligned 4 because they have a length as the
+ * first field;
+ */
+ case DBUS_TYPE_CUSTOM:
+ case DBUS_TYPE_DICT:
+ alignment = 4;
break;
+
case DBUS_TYPE_INT64:
case DBUS_TYPE_UINT64:
case DBUS_TYPE_DOUBLE:
- padding = _DBUS_ALIGN_VALUE (pos, 8) - pos;
+ alignment = 8;
break;
- case DBUS_TYPE_NAMED:
+
case DBUS_TYPE_ARRAY:
- case DBUS_TYPE_DICT:
- /* FIXME This is no good; we have to handle undefined header fields
- * also. SECURITY and spec compliance issue.
- */
- _dbus_assert_not_reached ("no defined header fields may contain a named, array or dict value");
+ _dbus_assert_not_reached ("passed an ARRAY type to get_type_alignment()");
break;
+
case DBUS_TYPE_INVALID:
default:
- _dbus_assert_not_reached ("invalid type in marshalled header");
+ _dbus_assert_not_reached ("passed an invalid or unknown type to get_type_alignment()");
break;
}
- delta = padding - prev_padding;
- if (delta > 0)
+ return alignment;
+}
+
+static dbus_bool_t
+iterate_one_field (const DBusString *str,
+ int byte_order,
+ int name_offset,
+ int *next_offset_p,
+ int *field_name_p,
+ DBusString *append_copy_to,
+ int *copy_name_offset_p,
+ int *copy_value_offset_p)
+{
+ int name, type, array_type;
+ int alignment;
+ int type_len;
+ int type_pos;
+ int value_pos;
+ int value_len;
+ int value_end;
+ int pos;
+
+ _dbus_verbose ("%s: name_offset=%d, append to %p\n",
+ _DBUS_FUNCTION_NAME, name_offset, append_copy_to);
+
+ pos = name_offset;
+
+ name = _dbus_string_get_byte (str, name_offset);
+ pos++;
+
+ type_pos = pos;
+ type = _dbus_string_get_byte (str, type_pos);
+ pos++;
+ type_len = 1;
+
+ array_type = type;
+ /* find out the type of our array */
+ while (array_type == DBUS_TYPE_ARRAY)
{
- if (!_dbus_string_insert_bytes (&message->header, pos, delta, 0))
- return FALSE;
+ pos++;
+ type_len++;
+ array_type = _dbus_string_get_byte (str, pos);
}
- else if (delta < 0)
+
+ _dbus_verbose ("%s: name %d, type '%c' %d at %d len %d, array type '%c' %d\n",
+ _DBUS_FUNCTION_NAME,
+ name, type, type, type_pos, type_len, array_type, array_type);
+
+#ifndef DBUS_DISABLE_ASSERT
+ if (!_dbus_type_is_valid (array_type))
{
- _dbus_string_delete (&message->header, pos, -delta);
+ _dbus_warn ("type '%c' %d is not valid in %s\n",
+ array_type, array_type, _DBUS_FUNCTION_NAME);
+ _dbus_assert_not_reached ("invalid type");
}
+#endif
+
+ alignment = get_type_alignment (array_type);
+
+ if (alignment > 0)
+ pos = _DBUS_ALIGN_VALUE (pos, alignment);
- next_field = get_next_field (message, field);
- if (next_field != DBUS_HEADER_FIELD_INVALID)
- {
- int next_offset = message->header_fields[next_field].name_offset;
+ _dbus_verbose ("%s: alignment %d value at pos %d\n",
+ _DBUS_FUNCTION_NAME, alignment, pos);
+
+ /* pos now points to our value */
+ if (!_dbus_marshal_get_arg_end_pos (str, byte_order,
+ type, pos, &value_end))
+ _dbus_assert_not_reached ("failed to get the byte after this header");
- _dbus_assert (next_offset > 0);
+ value_pos = pos;
+ value_len = value_end - value_pos;
- if (!re_align_field_recurse (message, field,
- pos + padding + (next_offset - old_value_offset)))
- goto failed;
- }
- else
+ _dbus_verbose ("%s: value_pos %d value_len %d value_end %d\n",
+ _DBUS_FUNCTION_NAME, value_pos, value_len, value_end);
+
+ if (next_offset_p)
+ *next_offset_p = pos + value_len;
+
+ if (field_name_p)
+ *field_name_p = name;
+
+ if (append_copy_to)
{
- if (!append_header_padding (message))
- goto failed;
- }
+ int orig_len;
- message->header_fields[field].name_offset = offset;
- message->header_fields[field].value_offset = pos + padding;
+ orig_len = _dbus_string_get_length (append_copy_to);
- return TRUE;
+ if (copy_name_offset_p)
+ *copy_name_offset_p = orig_len;
+
+ if (!_dbus_string_append_byte (append_copy_to, name))
+ goto failed_copy;
- failed:
- if (delta > 0)
- {
- _dbus_string_delete (&message->header, pos, delta);
+ if (!_dbus_string_copy_len (str, type_pos, type_len,
+ append_copy_to,
+ _dbus_string_get_length (append_copy_to)))
+ goto failed_copy;
+
+ if (!_dbus_string_align_length (append_copy_to, alignment))
+ goto failed_copy;
+
+ if (copy_value_offset_p)
+ *copy_value_offset_p = _dbus_string_get_length (append_copy_to);
+
+ if (!_dbus_string_copy_len (str, value_pos, value_len,
+ append_copy_to,
+ _dbus_string_get_length (append_copy_to)))
+ goto failed_copy;
+
+ return TRUE;
+
+ failed_copy:
+ _dbus_verbose ("%s: Failed copying old fields to new string\n",
+ _DBUS_FUNCTION_NAME);
+ _dbus_string_set_length (append_copy_to, orig_len);
+ return FALSE;
}
- else if (delta < 0)
+ else
+ return TRUE;
+}
+
+#ifndef DBUS_DISABLE_ASSERT
+static void
+verify_header_fields (DBusMessage *message)
+{
+ int i;
+ i = 0;
+ while (i < DBUS_HEADER_FIELD_LAST)
{
- /* this must succeed because it was allocated on function entry and
- * DBusString doesn't ever realloc smaller
- */
- _dbus_string_insert_bytes (&message->header, pos, -delta, 0);
+ if (message->header_fields[i].name_offset >= 0)
+ _dbus_assert (_dbus_string_get_byte (&message->header,
+ message->header_fields[i].name_offset) ==
+ i);
+ ++i;
}
-
- return FALSE;
}
+#else /* DBUS_DISABLE_ASSERT */
+#define verify_header_fields(x)
+#endif /* DBUS_DISABLE_ASSERT */
+/* In this function we delete one field and re-align all the fields
+ * following it.
+ */
static dbus_bool_t
-delete_field (DBusMessage *message,
- int field)
+delete_one_and_re_align (DBusMessage *message,
+ int name_offset_to_delete)
{
- int offset = message->header_fields[field].name_offset;
- int next_field;
+ DBusString copy;
+ int new_fields_front_padding;
+ int next_offset;
+ int field_name;
+ dbus_bool_t retval;
+ HeaderField new_header_fields[DBUS_HEADER_FIELD_LAST];
+
+ _dbus_assert (name_offset_to_delete < _dbus_string_get_length (&message->header));
+ verify_header_fields (message);
- _dbus_assert (!message->locked);
+ _dbus_verbose ("%s: Deleting one field at offset %d\n",
+ _DBUS_FUNCTION_NAME,
+ name_offset_to_delete);
- if (offset < 0)
- return FALSE;
+ retval = FALSE;
clear_header_padding (message);
-
- next_field = get_next_field (message, field);
- if (next_field == DBUS_HEADER_FIELD_INVALID)
+
+ if (!_dbus_string_init_preallocated (©,
+ _dbus_string_get_length (&message->header) -
+ name_offset_to_delete + 8))
{
- _dbus_string_set_length (&message->header, offset);
-
- message->header_fields[field].name_offset = -1;
- message->header_fields[field].value_offset = -1;
-
- /* 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 TRUE;
+ _dbus_verbose ("%s: Failed to init string to hold copy of fields\n",
+ _DBUS_FUNCTION_NAME);
+ goto out_0;
}
- else
- {
- DBusString deleted;
- int next_offset = message->header_fields[next_field].name_offset;
+
+ /* Align the name offset of the first field in the same way it's
+ * aligned in the real header
+ */
+ new_fields_front_padding = name_offset_to_delete % 8;
- _dbus_assert (next_offset > 0);
+ if (!_dbus_string_insert_bytes (©, 0, new_fields_front_padding,
+ '\0'))
+ _dbus_assert_not_reached ("Should not have failed to insert bytes into preallocated string\n");
- if (!_dbus_string_init (&deleted))
- goto failed;
+ memcpy (new_header_fields, message->header_fields,
+ sizeof (new_header_fields));
+
+ /* Now just re-marshal each field in the header to our temporary
+ * buffer, skipping the first one. The tricky part is that the
+ * fields are padded as if for previous_name_offset, but are in fact
+ * at unaligned_name_offset
+ */
- if (!_dbus_string_move_len (&message->header,
- offset, next_offset - offset,
- &deleted, 0))
- {
- _dbus_string_free (&deleted);
- goto failed;
- }
+ if (!iterate_one_field (&message->header,
+ message->byte_order,
+ name_offset_to_delete,
+ &next_offset,
+ &field_name, NULL, NULL, NULL))
+ _dbus_assert_not_reached ("shouldn't have failed to alloc memory to skip the deleted field");
- /* appends the header padding */
- if (!re_align_field_recurse (message, next_field, offset))
- {
- /* this must succeed because it was allocated on function entry and
- * DBusString doesn't ever realloc smaller
- */
- if (!_dbus_string_copy (&deleted, 0, &message->header, offset))
- _dbus_assert_not_reached ("failed to revert to original field");
-
- _dbus_string_free (&deleted);
- goto failed;
- }
+ if (field_name < DBUS_HEADER_FIELD_LAST)
+ {
+ new_header_fields[field_name].name_offset = -1;
+ new_header_fields[field_name].value_offset = -1;
+ }
+
+ while (next_offset < _dbus_string_get_length (&message->header))
+ {
+ int copy_name_offset;
+ int copy_value_offset;
- _dbus_string_free (&deleted);
+ if (!iterate_one_field (&message->header,
+ message->byte_order,
+ next_offset,
+ &next_offset,
+ &field_name,
+ ©,
+ ©_name_offset,
+ ©_value_offset))
+ {
+ _dbus_verbose ("%s: OOM iterating one field\n",
+ _DBUS_FUNCTION_NAME);
+ goto out_1;
+ }
- message->header_fields[field].name_offset = -1;
- message->header_fields[field].value_offset = -1;
+ if (field_name < DBUS_HEADER_FIELD_LAST)
+ {
+ new_header_fields[field_name].name_offset = copy_name_offset - new_fields_front_padding + name_offset_to_delete;
+ new_header_fields[field_name].value_offset = copy_value_offset - new_fields_front_padding + name_offset_to_delete;
+ }
+ }
- return TRUE;
+ if (!_dbus_string_replace_len (©,
+ new_fields_front_padding,
+ _dbus_string_get_length (©) - new_fields_front_padding,
+ &message->header,
+ name_offset_to_delete,
+ _dbus_string_get_length (&message->header) - name_offset_to_delete))
+ {
+ _dbus_verbose ("%s: OOM moving copy back into header\n",
+ _DBUS_FUNCTION_NAME);
+ goto out_1;
+ }
+
+ memcpy (message->header_fields, new_header_fields,
+ sizeof (new_header_fields));
+ verify_header_fields (message);
+
+ retval = TRUE;
+
+ out_1:
+ _dbus_string_free (©);
+
+ out_0:
+ if (!append_header_padding (message))
+ _dbus_assert_not_reached ("Failed to re-append header padding in re_align_field_recurse()");
+
+ return retval;
+}
- 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");
+static dbus_bool_t
+delete_field (DBusMessage *message,
+ int field,
+ int prealloc_header_space)
+{
+ int offset;
+ _dbus_assert (!message->locked);
+
+ /* Prealloc */
+ if (!_dbus_string_lengthen (&message->header, prealloc_header_space))
+ {
+ _dbus_verbose ("failed to prealloc %d bytes header space\n",
+ prealloc_header_space);
return FALSE;
}
+ _dbus_string_shorten (&message->header, prealloc_header_space);
+
+ /* Delete */
+ offset = message->header_fields[field].name_offset;
+ if (offset < 0)
+ {
+ _dbus_verbose ("header field didn't exist, no need to delete\n");
+ return TRUE; /* field didn't exist */
+ }
+
+ return delete_one_and_re_align (message, offset);
}
#ifdef DBUS_BUILD_TESTS
int type,
const char *value)
{
- int offset = message->header_fields[field].value_offset;
-
- _dbus_assert (!message->locked);
- _dbus_assert (value != NULL);
+ int prealloc;
+ int value_len;
- if (offset < 0)
- {
- /* need to append the field */
- return append_string_field (message, field, type, value);
- }
- else
- {
- DBusString v;
- char *old_value;
- int next_field;
- int next_offset;
- int len;
-
- clear_header_padding (message);
-
- old_value = _dbus_demarshal_string (&message->header,
- message->byte_order,
- offset,
- &next_offset);
- if (!old_value)
- goto failed;
-
- len = strlen (value);
-
- _dbus_string_init_const_len (&v, value,
- len + 1); /* include nul */
- if (!_dbus_marshal_set_string (&message->header,
- message->byte_order,
- offset, &v, len))
- {
- dbus_free (old_value);
- goto failed;
- }
-
- next_field = get_next_field (message, field);
- if (next_field != DBUS_HEADER_FIELD_INVALID)
- {
- /* re-appends the header padding */
- if (!re_align_field_recurse (message, next_field, next_offset))
- {
- len = strlen (old_value);
-
- _dbus_string_init_const_len (&v, old_value,
- len + 1); /* include nul */
- if (!_dbus_marshal_set_string (&message->header,
- message->byte_order,
- offset, &v, len))
- _dbus_assert_not_reached ("failed to revert to original string");
-
- dbus_free (old_value);
- goto failed;
- }
- }
+ _dbus_assert (!message->locked);
- dbus_free (old_value);
-
- return TRUE;
+ value_len = value ? strlen (value) : 0;
+
+ /* the prealloc is so the append_string_field()
+ * below won't fail, leaving us in inconsistent state
+ */
+ prealloc = value_len + MAX_BYTES_OVERHEAD_TO_APPEND_A_STRING;
- 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");
+ _dbus_verbose ("set_string_field() field %d prealloc %d\n",
+ field, prealloc);
+
+ if (!delete_field (message, field, prealloc))
+ return FALSE;
- return FALSE;
+ if (value != NULL)
+ {
+ /* need to append the field */
+ if (!append_string_field_len (message, field, type, value, value_len))
+ _dbus_assert_not_reached ("Appending string field shouldn't have failed, due to preallocation");
}
+
+ return TRUE;
}
/**
}
/**
- * Returns the serial of a message or -1 if none has been specified.
+ * Returns the serial of a message or 0 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.
+ * the message and is used to identify replies to this message. All
+ * messages received on a connection will have a serial, but messages
+ * you haven't sent yet may return 0.
*
* @param message the message
* @returns the client serial
}
/**
- * Returns the serial that the message is
- * a reply to or 0 if none.
+ * Returns the serial that the message is a reply to or 0 if none.
*
* @param message the message
* @returns the reply serial
else
_dbus_list_free_link (link);
- _dbus_counter_adjust (counter, message->size_counter_delta);
+ _dbus_counter_adjust (counter, - message->size_counter_delta);
_dbus_counter_unref (counter);
}
_dbus_assert ((interface && member) ||
(error_name) ||
!(interface || member || error_name));
+ _dbus_assert (error_name == NULL || is_valid_error_name (error_name));
if (!_dbus_string_append_byte (&message->header, message->byte_order))
return FALSE;
if (!_dbus_string_append_byte (&message->header, DBUS_MAJOR_PROTOCOL_VERSION))
return FALSE;
+ /* header length */
if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
return FALSE;
+ /* body length */
if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
return FALSE;
- if (!_dbus_marshal_int32 (&message->header, message->byte_order, -1))
+ /* serial */
+ if (!_dbus_marshal_uint32 (&message->header, message->byte_order, 0))
return FALSE;
/* Marshal all the fields (Marshall Fields?) */
if (service != NULL)
{
if (!append_string_field (message,
- DBUS_HEADER_FIELD_SERVICE,
+ DBUS_HEADER_FIELD_DESTINATION,
DBUS_TYPE_STRING,
service))
return FALSE;
error_name))
return FALSE;
}
+
+ /* @todo if we make signature optional when body is empty, we don't
+ * need to do this here.
+ */
+ if (!append_string_field (message,
+ DBUS_HEADER_FIELD_SIGNATURE,
+ DBUS_TYPE_STRING,
+ ""))
+ return FALSE;
return TRUE;
}
*/
/**
- * @typedef DBusMessage
- *
- * Opaque data type representing a message received from or to be
- * sent to another application.
+ * Sets the signature of the message, i.e. the arguments in the
+ * message payload. The signature includes only "in" arguments for
+ * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
+ * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
+ * what you might expect (it does not include the signature of the
+ * entire C++-style method).
+ *
+ * The signature is a string made up of type codes such
+ * as #DBUS_TYPE_STRING. The string is terminated with nul
+ * (nul is also the value of #DBUS_TYPE_INVALID).
+ *
+ * @param message the message
+ * @param signature the type signature or #NULL to unset
+ * @returns #FALSE if no memory
*/
+static dbus_bool_t
+dbus_message_set_signature (DBusMessage *message,
+ const char *signature)
+{
+ _dbus_return_val_if_fail (message != NULL, FALSE);
+ _dbus_return_val_if_fail (!message->locked, FALSE);
+
+ return set_string_field (message,
+ DBUS_HEADER_FIELD_SIGNATURE,
+ DBUS_TYPE_STRING,
+ signature);
+}
-static DBusMessage*
-dbus_message_new_empty_header (void)
+/**
+ * Appends to the signature of the message.
+ * (currently a static function, maybe should be public?)
+ *
+ * @param message the message
+ * @param append_bytes nul-terminated bytes to append to the type signature
+ * @returns #FALSE if no memory
+ */
+static dbus_bool_t
+dbus_message_append_to_signature (DBusMessage *message,
+ const char *append_bytes)
{
- DBusMessage *message;
- int i;
+ const char *signature;
+ DBusString append_str;
+ dbus_bool_t retval;
- message = dbus_new0 (DBusMessage, 1);
- if (message == NULL)
- return NULL;
+ _dbus_return_val_if_fail (append_bytes != NULL, FALSE);
+ _dbus_return_val_if_fail (message != NULL, FALSE);
+ _dbus_return_val_if_fail (!message->locked, FALSE);
+
+ retval = FALSE;
- message->refcount.value = 1;
- message->byte_order = DBUS_COMPILER_BYTE_ORDER;
- message->client_serial = 0;
- message->reply_serial = 0;
+ /* FIXME Just really inefficient for the moment; later we could
+ * speed it up a lot by poking more directly at the header data
+ */
+ signature = dbus_message_get_signature (message);
- _dbus_data_slot_list_init (&message->slot_list);
+ if (!_dbus_string_init (&append_str))
+ return FALSE;
+
+ if (signature && !_dbus_string_append (&append_str, signature))
+ goto out;
+
+ if (!_dbus_string_append (&append_str, append_bytes))
+ goto out;
- i = 0;
- while (i <= DBUS_HEADER_FIELD_LAST)
- {
- message->header_fields[i].name_offset = -1;
- message->header_fields[i].value_offset = -1;
- ++i;
- }
+ if (!set_string_field (message,
+ DBUS_HEADER_FIELD_SIGNATURE,
+ DBUS_TYPE_STRING,
+ _dbus_string_get_const_data (&append_str)))
+ goto out;
+
+ retval = TRUE;
- if (!_dbus_string_init_preallocated (&message->header, 64))
- {
- dbus_free (message);
- return NULL;
- }
+ out:
- if (!_dbus_string_init_preallocated (&message->body, 64))
- {
- _dbus_string_free (&message->header);
- dbus_free (message);
- return NULL;
+ _dbus_string_free (&append_str);
+
+ return retval;
+}
+
+/**
+ * Appends one byte to the signature of the message.
+ * Internal function.
+ *
+ * @param message the message
+ * @param append_byte the byte
+ * @returns #FALSE if no memory
+ */
+static dbus_bool_t
+_dbus_message_append_byte_to_signature (DBusMessage *message,
+ unsigned char append_byte)
+{
+ char buf[2];
+
+ _dbus_return_val_if_fail (message != NULL, FALSE);
+ _dbus_return_val_if_fail (!message->locked, FALSE);
+
+ buf[0] = append_byte;
+ buf[1] = '\0';
+
+ return dbus_message_append_to_signature (message, buf);
+}
+
+/**
+ * Removes the last byte from the signature of the message.
+ * Internal function.
+ *
+ * @param message the message
+ */
+static void
+_dbus_message_remove_byte_from_signature (DBusMessage *message)
+{
+ const char *signature;
+
+ _dbus_return_if_fail (message != NULL);
+ _dbus_return_if_fail (!message->locked);
+
+ signature = dbus_message_get_signature (message);
+
+ _dbus_return_if_fail (signature != NULL);
+
+ if (!delete_field (message,
+ DBUS_HEADER_FIELD_SIGNATURE,
+ 0))
+ _dbus_assert_not_reached ("failed to delete signature field");
+
+ /* reappend one shorter (could this be any less efficient? the code will
+ * go away later anyhow)
+ */
+ if (!append_string_field_len (message, DBUS_HEADER_FIELD_SIGNATURE,
+ DBUS_TYPE_STRING, signature,
+ strlen (signature) - 1))
+ _dbus_assert_not_reached ("reappending shorter signature shouldn't have failed");
+}
+
+/**
+ * @typedef DBusMessage
+ *
+ * Opaque data type representing a message received from or to be
+ * sent to another application.
+ */
+
+static DBusMessage*
+dbus_message_new_empty_header (void)
+{
+ DBusMessage *message;
+ int i;
+
+ message = dbus_new0 (DBusMessage, 1);
+ if (message == NULL)
+ return NULL;
+
+ message->refcount.value = 1;
+ message->byte_order = DBUS_COMPILER_BYTE_ORDER;
+ message->client_serial = 0;
+ message->reply_serial = 0;
+
+ _dbus_data_slot_list_init (&message->slot_list);
+
+ i = 0;
+ while (i <= DBUS_HEADER_FIELD_LAST)
+ {
+ message->header_fields[i].name_offset = -1;
+ message->header_fields[i].value_offset = -1;
+ ++i;
+ }
+
+ if (!_dbus_string_init_preallocated (&message->header, 64))
+ {
+ dbus_free (message);
+ return NULL;
+ }
+
+ if (!_dbus_string_init_preallocated (&message->body, 64))
+ {
+ _dbus_string_free (&message->header);
+ dbus_free (message);
+ return NULL;
}
return message;
_dbus_return_val_if_fail (method_call != NULL, NULL);
sender = get_string_field (method_call,
- DBUS_HEADER_FIELD_SENDER_SERVICE,
+ DBUS_HEADER_FIELD_SENDER,
NULL);
/* sender is allowed to be null here in peer-to-peer case */
return NULL;
}
+ dbus_message_set_no_reply (message, TRUE);
+
if (!dbus_message_set_reply_serial (message,
dbus_message_get_serial (method_call)))
{
dbus_message_unref (message);
return NULL;
}
+
+ dbus_message_set_no_reply (message, TRUE);
return message;
}
_dbus_return_val_if_fail (reply_to != NULL, NULL);
_dbus_return_val_if_fail (error_name != NULL, NULL);
+ _dbus_return_val_if_fail (is_valid_error_name (error_name), NULL);
sender = get_string_field (reply_to,
- DBUS_HEADER_FIELD_SENDER_SERVICE,
+ DBUS_HEADER_FIELD_SENDER,
NULL);
/* sender may be NULL for non-message-bus case or
return NULL;
}
+ dbus_message_set_no_reply (message, TRUE);
+
if (!dbus_message_set_reply_serial (message,
dbus_message_get_serial (reply_to)))
{
}
/**
+ * Creates a new message that is an error reply to a certain message.
+ * Error replies are possible in response to method calls primarily.
+ *
+ * @param reply_to the original message
+ * @param error_name the error name
+ * @param error_format the error message string to be printed
+ * @param ... value of first argument, list of additional values to print
+ * @returns a new error message
+ */
+DBusMessage*
+dbus_message_new_error_printf (DBusMessage *reply_to,
+ const char *error_name,
+ const char *error_format,
+ ...)
+{
+ va_list args;
+ DBusString str;
+ DBusMessage *message;
+
+ _dbus_return_val_if_fail (reply_to != NULL, NULL);
+ _dbus_return_val_if_fail (error_name != NULL, NULL);
+ _dbus_return_val_if_fail (is_valid_error_name (error_name), NULL);
+
+ if (!_dbus_string_init (&str))
+ return NULL;
+
+ va_start (args, error_format);
+
+ if (_dbus_string_append_printf_valist (&str, error_format, args))
+ message = dbus_message_new_error (reply_to, error_name,
+ _dbus_string_get_const_data (&str));
+ else
+ message = NULL;
+
+ _dbus_string_free (&str);
+
+ va_end (args);
+
+ return message;
+}
+
+
+/**
* Creates a new message that is an exact replica of the message
* specified, except that its refcount is set to 1.
*
retval->header_padding = message->header_padding;
retval->locked = FALSE;
- if (!_dbus_string_init (&retval->header))
+ if (!_dbus_string_init_preallocated (&retval->header,
+ _dbus_string_get_length (&message->header)))
{
dbus_free (retval);
return NULL;
}
- if (!_dbus_string_init (&retval->body))
+ if (!_dbus_string_init_preallocated (&retval->body,
+ _dbus_string_get_length (&message->body)))
{
_dbus_string_free (&retval->header);
dbus_free (retval);
return NULL;
}
-
+
if (!_dbus_string_copy (&message->header, 0,
&retval->header, 0))
- {
- _dbus_string_free (&retval->header);
- _dbus_string_free (&retval->body);
- dbus_free (retval);
-
- return NULL;
- }
+ goto failed_copy;
if (!_dbus_string_copy (&message->body, 0,
&retval->body, 0))
- {
- _dbus_string_free (&retval->header);
- _dbus_string_free (&retval->body);
- dbus_free (retval);
-
- return NULL;
- }
-
+ goto failed_copy;
+
for (i = 0; i <= DBUS_HEADER_FIELD_LAST; i++)
{
retval->header_fields[i] = message->header_fields[i];
}
return retval;
+
+ failed_copy:
+ _dbus_string_free (&retval->header);
+ _dbus_string_free (&retval->body);
+ dbus_free (retval);
+
+ return NULL;
}
* Increments the reference count of a DBusMessage.
*
* @param message The message
+ * @returns the message
* @see dbus_message_unref
*/
-void
+DBusMessage *
dbus_message_ref (DBusMessage *message)
{
dbus_int32_t old_refcount;
- _dbus_return_if_fail (message != NULL);
+ _dbus_return_val_if_fail (message != NULL, NULL);
old_refcount = _dbus_atomic_inc (&message->refcount);
_dbus_assert (old_refcount >= 1);
+
+ return message;
}
static void
* emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
*
* @param message the message
- * @param object_path the path
+ * @param object_path the path or #NULL to unset
* @returns #FALSE if not enough memory
*/
dbus_bool_t
{
_dbus_return_val_if_fail (message != NULL, FALSE);
_dbus_return_val_if_fail (!message->locked, FALSE);
-
- if (object_path == NULL)
- {
- delete_field (message, DBUS_HEADER_FIELD_PATH);
- return TRUE;
- }
- else
- {
- return set_string_field (message,
- DBUS_HEADER_FIELD_PATH,
- DBUS_TYPE_OBJECT_PATH,
- object_path);
- }
+
+ return set_string_field (message,
+ DBUS_HEADER_FIELD_PATH,
+ DBUS_TYPE_OBJECT_PATH,
+ object_path);
}
/**
* (for DBUS_MESSAGE_TYPE_SIGNAL).
*
* @param message the message
- * @param interface the interface
+ * @param interface the interface or #NULL to unset
* @returns #FALSE if not enough memory
*/
dbus_bool_t
{
_dbus_return_val_if_fail (message != NULL, FALSE);
_dbus_return_val_if_fail (!message->locked, FALSE);
-
- if (interface == NULL)
- {
- delete_field (message, DBUS_HEADER_FIELD_INTERFACE);
- return TRUE;
- }
- else
- {
- return set_string_field (message,
- DBUS_HEADER_FIELD_INTERFACE,
- DBUS_TYPE_STRING,
- interface);
- }
+
+ return set_string_field (message,
+ DBUS_HEADER_FIELD_INTERFACE,
+ DBUS_TYPE_STRING,
+ interface);
}
/**
* The interface name is fully-qualified (namespaced).
*
* @param message the message
- * @param member the member
+ * @param member the member or #NULL to unset
* @returns #FALSE if not enough memory
*/
dbus_bool_t
{
_dbus_return_val_if_fail (message != NULL, FALSE);
_dbus_return_val_if_fail (!message->locked, FALSE);
-
- if (member == NULL)
- {
- delete_field (message, DBUS_HEADER_FIELD_MEMBER);
- return TRUE;
- }
- else
- {
- return set_string_field (message,
- DBUS_HEADER_FIELD_MEMBER,
- DBUS_TYPE_STRING,
- member);
- }
+
+ return set_string_field (message,
+ DBUS_HEADER_FIELD_MEMBER,
+ DBUS_TYPE_STRING,
+ member);
}
/**
* The name is fully-qualified (namespaced).
*
* @param message the message
- * @param error_name the name
+ * @param error_name the name or #NULL to unset
* @returns #FALSE if not enough memory
*/
dbus_bool_t
{
_dbus_return_val_if_fail (message != NULL, FALSE);
_dbus_return_val_if_fail (!message->locked, FALSE);
+ _dbus_return_val_if_fail (error_name == NULL || is_valid_error_name (error_name), FALSE);
- if (error_name == NULL)
- {
- delete_field (message, DBUS_HEADER_FIELD_ERROR_NAME);
- return TRUE;
- }
- else
- {
- return set_string_field (message,
- DBUS_HEADER_FIELD_ERROR_NAME,
- DBUS_TYPE_STRING,
- error_name);
- }
+ return set_string_field (message,
+ DBUS_HEADER_FIELD_ERROR_NAME,
+ DBUS_TYPE_STRING,
+ error_name);
}
/**
* Sets the message's destination service.
*
* @param message the message
- * @param destination the destination service name
+ * @param destination the destination service name or #NULL to unset
* @returns #FALSE if not enough memory
*/
dbus_bool_t
{
_dbus_return_val_if_fail (message != NULL, FALSE);
_dbus_return_val_if_fail (!message->locked, FALSE);
-
- if (destination == NULL)
- {
- delete_field (message, DBUS_HEADER_FIELD_SERVICE);
- return TRUE;
- }
- else
- {
- return set_string_field (message,
- DBUS_HEADER_FIELD_SERVICE,
- DBUS_TYPE_STRING,
- destination);
- }
+
+ return set_string_field (message,
+ DBUS_HEADER_FIELD_DESTINATION,
+ DBUS_TYPE_STRING,
+ destination);
}
/**
_dbus_return_val_if_fail (message != NULL, NULL);
return get_string_field (message,
- DBUS_HEADER_FIELD_SERVICE,
+ DBUS_HEADER_FIELD_DESTINATION,
NULL);
}
}
/**
- * This function takes a va_list for use by language bindings.
- * It's otherwise the same as dbus_message_append_args().
+ * 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 #DBUS_TYPE_INVALID.
*
- * @todo: Shouldn't this function clean up the changes to the message
- * on failures? (Yes)
+ * @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_return_val_if_fail (message != NULL, FALSE);
+ _dbus_return_val_if_error_is_set (error, FALSE);
- * @see dbus_message_append_args.
+ 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 We need to free the argument data when an error occurs.
+ *
+ * @see dbus_message_get_args
* @param message the message
- * @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
+ * @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_append_args_valist (DBusMessage *message,
- int first_arg_type,
- va_list var_args)
+dbus_message_get_args_valist (DBusMessage *message,
+ DBusError *error,
+ int first_arg_type,
+ va_list var_args)
{
- int type, old_len;
DBusMessageIter iter;
_dbus_return_val_if_fail (message != NULL, FALSE);
+ _dbus_return_val_if_error_is_set (error, FALSE);
- old_len = _dbus_string_get_length (&message->body);
-
- type = first_arg_type;
-
- dbus_message_append_iter_init (message, &iter);
-
- while (type != 0)
- {
- switch (type)
- {
- case DBUS_TYPE_NIL:
- if (!dbus_message_iter_append_nil (&iter))
- goto errorout;
- break;
- case DBUS_TYPE_BOOLEAN:
- if (!dbus_message_iter_append_boolean (&iter, va_arg (var_args, dbus_bool_t)))
- goto errorout;
- break;
- case DBUS_TYPE_INT32:
- if (!dbus_message_iter_append_int32 (&iter, va_arg (var_args, dbus_int32_t)))
- goto errorout;
- break;
- case DBUS_TYPE_UINT32:
- if (!dbus_message_iter_append_uint32 (&iter, va_arg (var_args, dbus_uint32_t)))
- goto errorout;
- break;
-#ifdef DBUS_HAVE_INT64
- case DBUS_TYPE_INT64:
- if (!dbus_message_iter_append_int64 (&iter, va_arg (var_args, dbus_int64_t)))
- goto errorout;
- break;
- case DBUS_TYPE_UINT64:
- if (!dbus_message_iter_append_uint64 (&iter, va_arg (var_args, dbus_uint64_t)))
- goto errorout;
- break;
-#endif /* DBUS_HAVE_INT64 */
- case DBUS_TYPE_DOUBLE:
- if (!dbus_message_iter_append_double (&iter, va_arg (var_args, double)))
- goto errorout;
- break;
- case DBUS_TYPE_STRING:
- if (!dbus_message_iter_append_string (&iter, va_arg (var_args, const char *)))
- goto errorout;
- break;
- case DBUS_TYPE_NAMED:
- {
- const char *name;
- unsigned char *data;
- int len;
-
- name = va_arg (var_args, const char *);
- data = va_arg (var_args, unsigned char *);
- len = va_arg (var_args, int);
-
- if (!dbus_message_iter_append_named (&iter, name, data, len))
- goto errorout;
- break;
- }
- case DBUS_TYPE_ARRAY:
- {
- void *data;
- int len, type;
-
- type = va_arg (var_args, int);
- data = va_arg (var_args, void *);
- len = va_arg (var_args, int);
-
- switch (type)
- {
- case DBUS_TYPE_BYTE:
- if (!dbus_message_iter_append_byte_array (&iter, (unsigned char *)data, len))
- goto errorout;
- break;
- case DBUS_TYPE_BOOLEAN:
- if (!dbus_message_iter_append_boolean_array (&iter, (unsigned char *)data, len))
- goto errorout;
- break;
- case DBUS_TYPE_INT32:
- if (!dbus_message_iter_append_int32_array (&iter, (dbus_int32_t *)data, len))
- goto errorout;
- break;
- case DBUS_TYPE_UINT32:
- if (!dbus_message_iter_append_uint32_array (&iter, (dbus_uint32_t *)data, len))
- goto errorout;
- break;
-#ifdef DBUS_HAVE_INT64
- case DBUS_TYPE_INT64:
- if (!dbus_message_iter_append_int64_array (&iter, (dbus_int64_t *)data, len))
- goto errorout;
- break;
- case DBUS_TYPE_UINT64:
- if (!dbus_message_iter_append_uint64_array (&iter, (dbus_uint64_t *)data, len))
- goto errorout;
- break;
-#endif /* DBUS_HAVE_INT64 */
- case DBUS_TYPE_DOUBLE:
- if (!dbus_message_iter_append_double_array (&iter, (double *)data, len))
- goto errorout;
- break;
- case DBUS_TYPE_STRING:
- if (!dbus_message_iter_append_string_array (&iter, (const char **)data, len))
- goto errorout;
- break;
- case DBUS_TYPE_NIL:
- case DBUS_TYPE_ARRAY:
- case DBUS_TYPE_NAMED:
- case DBUS_TYPE_DICT:
- _dbus_warn ("dbus_message_append_args_valist doesn't support recursive arrays\n");
- goto errorout;
- default:
- _dbus_warn ("Unknown field type %d\n", type);
- goto errorout;
- }
- }
- break;
-
- case DBUS_TYPE_DICT:
- _dbus_warn ("dbus_message_append_args_valist doesn't support dicts\n");
- goto errorout;
- default:
- _dbus_warn ("Unknown field type %d\n", type);
- goto errorout;
- }
-
- type = va_arg (var_args, int);
- }
-
- return TRUE;
-
- errorout:
- return FALSE;
-}
-
-
-/**
- * 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 #DBUS_TYPE_INVALID.
- *
- * @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_return_val_if_fail (message != NULL, FALSE);
- _dbus_return_val_if_error_is_set (error, FALSE);
-
- 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 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)
-{
- DBusMessageIter iter;
-
- _dbus_return_val_if_fail (message != NULL, FALSE);
- _dbus_return_val_if_error_is_set (error, FALSE);
-
- dbus_message_iter_init (message, &iter);
- return dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
-}
+ dbus_message_iter_init (message, &iter);
+ return dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
+}
/**
* Gets arguments from a message iterator given a variable argument list.
*/
dbus_bool_t
dbus_message_iter_get_args (DBusMessageIter *iter,
- DBusError *error,
- int first_arg_type,
- ...)
-{
- dbus_bool_t retval;
- va_list var_args;
-
- _dbus_return_val_if_fail (iter != NULL, FALSE);
- _dbus_return_val_if_error_is_set (error, FALSE);
-
- va_start (var_args, first_arg_type);
- retval = dbus_message_iter_get_args_valist (iter, error, first_arg_type, var_args);
- va_end (var_args);
-
- return retval;
-}
-
-/**
- * This function takes a va_list for use by language bindings
- *
- * This function supports #DBUS_TYPE_INT64 and #DBUS_TYPE_UINT64
- * only if #DBUS_HAVE_INT64 is defined.
- *
- * @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 iter the message iter
- * @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_iter_get_args_valist (DBusMessageIter *iter,
- DBusError *error,
- int first_arg_type,
- va_list var_args)
-{
- int spec_type, msg_type, i;
- dbus_bool_t retval;
-
- _dbus_return_val_if_fail (iter != NULL, FALSE);
- _dbus_return_val_if_error_is_set (error, 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_BYTE:
- {
- unsigned char *ptr;
-
- ptr = va_arg (var_args, unsigned char *);
-
- *ptr = dbus_message_iter_get_byte (iter);
- 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:
- {
- dbus_uint32_t *ptr;
-
- ptr = va_arg (var_args, dbus_uint32_t *);
-
- *ptr = dbus_message_iter_get_uint32 (iter);
- break;
- }
-#ifdef DBUS_HAVE_INT64
- case DBUS_TYPE_INT64:
- {
- dbus_int64_t *ptr;
-
- ptr = va_arg (var_args, dbus_int64_t *);
-
- *ptr = dbus_message_iter_get_int64 (iter);
- break;
- }
- case DBUS_TYPE_UINT64:
- {
- dbus_uint64_t *ptr;
-
- ptr = va_arg (var_args, dbus_uint64_t *);
-
- *ptr = dbus_message_iter_get_uint64 (iter);
- break;
- }
-#endif /* DBUS_HAVE_INT64 */
-
- case DBUS_TYPE_DOUBLE:
- {
- double *ptr;
-
- ptr = va_arg (var_args, double *);
-
- *ptr = dbus_message_iter_get_double (iter);
- break;
- }
-
- case DBUS_TYPE_STRING:
- {
- char **ptr;
-
- ptr = va_arg (var_args, char **);
-
- *ptr = dbus_message_iter_get_string (iter);
-
- if (!*ptr)
- {
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
-
- break;
- }
-
- case DBUS_TYPE_NAMED:
- {
- char **name;
- unsigned char **data;
- int *len;
-
- name = va_arg (var_args, char **);
- data = va_arg (var_args, unsigned char **);
- len = va_arg (var_args, int *);
-
- if (!dbus_message_iter_get_named (iter, name, data, len))
- {
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
- }
- break;
- case DBUS_TYPE_ARRAY:
- {
- void **data;
- int *len, type;
-
- type = va_arg (var_args, int);
- data = va_arg (var_args, void *);
- len = va_arg (var_args, int *);
-
- if (dbus_message_iter_get_array_type (iter) != type)
- {
- dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
- "Argument %d is specified to be of type \"array of %s\", but "
- "is actually of type \"array of %s\"\n", i,
- _dbus_type_to_string (type),
- _dbus_type_to_string (dbus_message_iter_get_array_type (iter)));
- goto out;
- }
-
- switch (type)
- {
- case DBUS_TYPE_BYTE:
- if (!dbus_message_iter_get_byte_array (iter, (unsigned char **)data, len))
- {
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
- break;
- case DBUS_TYPE_BOOLEAN:
- if (!dbus_message_iter_get_boolean_array (iter, (unsigned char **)data, len))
- {
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
- break;
- case DBUS_TYPE_INT32:
- if (!dbus_message_iter_get_int32_array (iter, (dbus_int32_t **)data, len))
- {
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
- break;
- case DBUS_TYPE_UINT32:
- if (!dbus_message_iter_get_uint32_array (iter, (dbus_uint32_t **)data, len))
- {
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
- break;
-#ifdef DBUS_HAVE_INT64
- case DBUS_TYPE_INT64:
- if (!dbus_message_iter_get_int64_array (iter, (dbus_int64_t **)data, len))
- {
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
- break;
- case DBUS_TYPE_UINT64:
- if (!dbus_message_iter_get_uint64_array (iter, (dbus_uint64_t **)data, len))
- {
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
- break;
-#endif /* DBUS_HAVE_INT64 */
- case DBUS_TYPE_DOUBLE:
- if (!dbus_message_iter_get_double_array (iter, (double **)data, len))
- {
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
- break;
- case DBUS_TYPE_STRING:
- if (!dbus_message_iter_get_string_array (iter, (char ***)data, len))
- {
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
- break;
- case DBUS_TYPE_NIL:
- case DBUS_TYPE_ARRAY:
- case DBUS_TYPE_NAMED:
- case DBUS_TYPE_DICT:
- _dbus_warn ("dbus_message_get_args_valist doesn't support recursive arrays\n");
- dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
- goto out;
- default:
- _dbus_warn ("Unknown field type %d\n", type);
- dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
- goto out;
- }
- }
- break;
- case DBUS_TYPE_DICT:
- _dbus_warn ("dbus_message_get_args_valist doesn't support dicts\n");
- dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
- goto out;
- default:
- dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
- _dbus_warn ("Unknown field type %d\n", spec_type);
- goto out;
- }
-
- spec_type = va_arg (var_args, int);
- if (spec_type != 0 && !dbus_message_iter_next (iter))
- {
- dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
- "Message has only %d arguments, but more were expected", i);
- goto out;
- }
+ DBusError *error,
+ int first_arg_type,
+ ...)
+{
+ dbus_bool_t retval;
+ va_list var_args;
- i++;
- }
-
- retval = TRUE;
-
- out:
+ _dbus_return_val_if_fail (iter != NULL, FALSE);
+ _dbus_return_val_if_error_is_set (error, FALSE);
+ va_start (var_args, first_arg_type);
+ retval = dbus_message_iter_get_args_valist (iter, error, first_arg_type, var_args);
+ va_end (var_args);
+
return retval;
}
-
/**
* Initializes a DBusMessageIter representing the arguments of the
* message passed in.
*
* @param message the message
* @param iter pointer to an iterator to initialize
+ * @returns #FALSE if the message has no arguments
*/
-void
+dbus_bool_t
dbus_message_iter_init (DBusMessage *message,
DBusMessageIter *iter)
{
DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- _dbus_return_if_fail (message != NULL);
- _dbus_return_if_fail (iter != NULL);
+ _dbus_return_val_if_fail (message != NULL, FALSE);
+ _dbus_return_val_if_fail (iter != NULL, FALSE);
_dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
real->container_length_pos = 0;
real->wrote_dict_key = 0;
real->array_type_pos = 0;
+
+ return real->end > real->pos;
}
#ifndef DBUS_DISABLE_CHECKS
return pos;
}
+/* FIXME what are these _dbus_type_is_valid() checks for?
+ * haven't we validated the message?
+ */
static int
dbus_message_iter_get_data_start (DBusMessageRealIter *iter, int *type)
{
case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
data = _dbus_string_get_const_data_len (&iter->message->body,
iter->pos, 1);
- if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
+ if (_dbus_type_is_valid (*data))
*type = *data;
else
*type = DBUS_TYPE_INVALID;
case DBUS_MESSAGE_ITER_TYPE_ARRAY:
data = _dbus_string_get_const_data_len (&iter->message->body,
iter->array_type_pos, 1);
- if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
+ if (_dbus_type_is_valid (*data))
*type = *data;
else
*type = DBUS_TYPE_INVALID;
data = _dbus_string_get_const_data_len (&iter->message->body,
pos, 1);
- if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
+ if (_dbus_type_is_valid (*data))
*type = *data;
else
*type = DBUS_TYPE_INVALID;
_dbus_return_val_if_fail (dbus_message_iter_check (real), DBUS_TYPE_INVALID);
if (real->pos >= real->end)
- return DBUS_TYPE_INVALID;
+ {
+ _dbus_verbose (" iterator at or beyond end of message\n");
+ return DBUS_TYPE_INVALID;
+ }
pos = dbus_message_iter_get_data_start (real, &type);
return type;
}
+/* FIXME why do we validate the typecode in here, hasn't the message
+ * already been verified?
+ */
static int
iter_get_array_type (DBusMessageRealIter *iter, int *array_type_pos)
{
data = _dbus_string_get_const_data_len (&iter->message->body,
_array_type_pos, 1);
- if (*data > DBUS_TYPE_INVALID && *data <= DBUS_TYPE_LAST)
+ if (_dbus_type_is_valid (*data))
return *data;
return DBUS_TYPE_INVALID;
int type, pos;
_dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
-
pos = dbus_message_iter_get_data_start (real, &type);
_dbus_assert (type == DBUS_TYPE_STRING);
}
/**
- * Returns the name and data from a named type that an
- * iterator may point to. Note that you need to check that
- * the iterator points to a named type before using this
- * function.
+ * Returns the object path value that an iterator may point to.
+ * Note that you need to check that the iterator points to
+ * an object path value before using this function.
+ *
+ * @see dbus_message_iter_get_arg_type
+ * @param iter the message iter
+ * @returns the path
+ */
+char *
+dbus_message_iter_get_object_path (DBusMessageIter *iter)
+{
+ DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
+ int type, pos;
+
+ _dbus_return_val_if_fail (dbus_message_iter_check (real), NULL);
+
+ pos = dbus_message_iter_get_data_start (real, &type);
+
+ _dbus_assert (type == DBUS_TYPE_OBJECT_PATH);
+
+ return _dbus_demarshal_string (&real->message->body, real->message->byte_order,
+ pos, NULL);
+}
+
+/**
+ * Returns the name and data from a custom type that an iterator may
+ * point to. Note that you need to check that the iterator points to a
+ * custom type before using this function.
*
* @see dbus_message_iter_get_arg_type
* @param iter the message iter
- * @param name return location for the name
+ * @param name return location for the name of the custom type
* @param value return location for data
* @param len return location for length of data
* @returns TRUE if get succeed
*
*/
dbus_bool_t
-dbus_message_iter_get_named (DBusMessageIter *iter,
- char **name,
- unsigned char **value,
- int *len)
+dbus_message_iter_get_custom (DBusMessageIter *iter,
+ char **name,
+ unsigned char **value,
+ int *len)
{
DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
int type, pos;
char *_name;
- _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
+ _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
+
+ pos = dbus_message_iter_get_data_start (real, &type);
+
+ _dbus_assert (type == DBUS_TYPE_CUSTOM);
+
+ _name = _dbus_demarshal_string (&real->message->body, real->message->byte_order,
+ pos, &pos);
+
+ if (_name == NULL)
+ return FALSE;
+
+ if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
+ pos, NULL, value, len))
+ {
+ dbus_free (_name);
+ return FALSE;
+ }
+
+ *name = _name;
+
+ return TRUE;
+}
+
+static void
+_dbus_message_iter_get_basic_type (DBusMessageIter *iter,
+ char type,
+ void *value)
+{
+ DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
+ int item_type, pos;
+
+ _dbus_return_if_fail (dbus_message_iter_check (real));
+
+ pos = dbus_message_iter_get_data_start (real, &item_type);
+
+ _dbus_assert (type == item_type);
+
+ _dbus_demarshal_basic_type (&real->message->body,
+ type, value,
+ real->message->byte_order,
+ &pos);
+}
+
+
+/**
+ * This function takes a va_list for use by language bindings
+ *
+ * This function supports #DBUS_TYPE_INT64 and #DBUS_TYPE_UINT64
+ * only if #DBUS_HAVE_INT64 is defined.
+ *
+ * @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 iter the message iter
+ * @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_iter_get_args_valist (DBusMessageIter *iter,
+ DBusError *error,
+ int first_arg_type,
+ va_list var_args)
+{
+ int spec_type, msg_type, i;
+ dbus_bool_t retval;
+
+ _dbus_return_val_if_fail (iter != NULL, FALSE);
+ _dbus_return_val_if_error_is_set (error, FALSE);
+
+ retval = FALSE;
+
+ spec_type = first_arg_type;
+ i = 0;
+
+ while (spec_type != DBUS_TYPE_INVALID)
+ {
+ 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_BYTE:
+ case DBUS_TYPE_INT32:
+ case DBUS_TYPE_UINT32:
+#ifdef DBUS_HAVE_INT64
+ case DBUS_TYPE_INT64:
+ case DBUS_TYPE_UINT64:
+#endif /* DBUS_HAVE_INT64 */
+ case DBUS_TYPE_DOUBLE:
+ {
+ void *ptr = va_arg (var_args, void *);
+ _dbus_message_iter_get_basic_type (iter, spec_type, ptr);
+ break;
+ }
+
+ case DBUS_TYPE_STRING:
+ {
+ char **ptr;
+
+ ptr = va_arg (var_args, char **);
+
+ *ptr = dbus_message_iter_get_string (iter);
+
+ if (!*ptr)
+ {
+ dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+ goto out;
+ }
+
+ break;
+ }
+
+ case DBUS_TYPE_OBJECT_PATH:
+ {
+ char **ptr;
+
+ ptr = va_arg (var_args, char **);
+
+ *ptr = dbus_message_iter_get_object_path (iter);
+
+ if (!*ptr)
+ {
+ dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+ goto out;
+ }
+
+ break;
+ }
+
+ case DBUS_TYPE_CUSTOM:
+ {
+ char **name;
+ unsigned char **data;
+ int *len;
+
+ name = va_arg (var_args, char **);
+ data = va_arg (var_args, unsigned char **);
+ len = va_arg (var_args, int *);
+
+ if (!dbus_message_iter_get_custom (iter, name, data, len))
+ {
+ dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+ goto out;
+ }
+ }
+ break;
+ case DBUS_TYPE_ARRAY:
+ {
+ void **data;
+ int *len, type;
+ dbus_bool_t err = FALSE;
+
+ type = va_arg (var_args, int);
+ data = va_arg (var_args, void *);
+ len = va_arg (var_args, int *);
+
+ _dbus_return_val_if_fail (data != NULL, FALSE);
+ _dbus_return_val_if_fail (len != NULL, FALSE);
+
+ if (dbus_message_iter_get_array_type (iter) != type)
+ {
+ dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
+ "Argument %d is specified to be of type \"array of %s\", but "
+ "is actually of type \"array of %s\"\n", i,
+ _dbus_type_to_string (type),
+ _dbus_type_to_string (dbus_message_iter_get_array_type (iter)));
+ goto out;
+ }
+
+ switch (type)
+ {
+ case DBUS_TYPE_BYTE:
+ err = !dbus_message_iter_get_byte_array (iter, (unsigned char **)data, len);
+ break;
+ case DBUS_TYPE_BOOLEAN:
+ err = !dbus_message_iter_get_boolean_array (iter, (unsigned char **)data, len);
+ break;
+ case DBUS_TYPE_INT32:
+ err = !dbus_message_iter_get_int32_array (iter, (dbus_int32_t **)data, len);
+ break;
+ case DBUS_TYPE_UINT32:
+ err = !dbus_message_iter_get_uint32_array (iter, (dbus_uint32_t **)data, len);
+ break;
+#ifdef DBUS_HAVE_INT64
+ case DBUS_TYPE_INT64:
+ err = !dbus_message_iter_get_int64_array (iter, (dbus_int64_t **)data, len);
+ break;
+ case DBUS_TYPE_UINT64:
+ err = !dbus_message_iter_get_uint64_array (iter, (dbus_uint64_t **)data, len);
+ break;
+#endif /* DBUS_HAVE_INT64 */
+ case DBUS_TYPE_DOUBLE:
+ err = !dbus_message_iter_get_double_array (iter, (double **)data, len);
+ break;
+ case DBUS_TYPE_STRING:
+ err = !dbus_message_iter_get_string_array (iter, (char ***)data, len);
+ break;
+ case DBUS_TYPE_OBJECT_PATH:
+ err = !dbus_message_iter_get_object_path_array (iter, (char ***)data, len);
+ break;
+
+ case DBUS_TYPE_NIL:
+ case DBUS_TYPE_ARRAY:
+ case DBUS_TYPE_CUSTOM:
+ case DBUS_TYPE_DICT:
+ _dbus_warn ("dbus_message_get_args_valist doesn't support recursive arrays\n");
+ dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
+ goto out;
+ default:
+ _dbus_warn ("Unknown field type %d\n", type);
+ dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
+ goto out;
+ }
+ if (err)
+ {
+ dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+ goto out;
+ }
+ }
+ break;
+ case DBUS_TYPE_DICT:
+ _dbus_warn ("dbus_message_get_args_valist doesn't support dicts\n");
+ dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
+ goto out;
+ default:
+ dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, NULL);
+ _dbus_warn ("Unknown field type %d\n", spec_type);
+ goto out;
+ }
+
+ spec_type = va_arg (var_args, int);
+ if (!dbus_message_iter_next (iter) && spec_type != DBUS_TYPE_INVALID)
+ {
+ dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
+ "Message has only %d arguments, but more were expected", i);
+ goto out;
+ }
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_NAMED);
+ i++;
+ }
- _name = _dbus_demarshal_string (&real->message->body, real->message->byte_order,
- pos, &pos);
-
- if (_name == NULL)
- return FALSE;
+ retval = TRUE;
- if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
- pos, NULL, value, len))
- {
- dbus_free (_name);
- return FALSE;
- }
-
- *name = _name;
+ out:
- return TRUE;
+ return retval;
}
/**
unsigned char
dbus_message_iter_get_byte (DBusMessageIter *iter)
{
- unsigned char value;
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- int type, pos;
-
- _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
+ unsigned char value = 0;
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_BYTE);
+ _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_BYTE, &value);
- value = _dbus_string_get_byte (&real->message->body, pos);
-
return value;
}
-
/**
* Returns the boolean value that an iterator may point to.
* Note that you need to check that the iterator points to
dbus_bool_t
dbus_message_iter_get_boolean (DBusMessageIter *iter)
{
- unsigned char value;
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- int type, pos;
+ unsigned char value = 0;
- _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
-
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_BOOLEAN);
+ _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_BOOLEAN, &value);
- value = _dbus_string_get_byte (&real->message->body, pos);
-
- return value;
+ return (value != FALSE);
}
/**
dbus_int32_t
dbus_message_iter_get_int32 (DBusMessageIter *iter)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- int type, pos;
+ dbus_int32_t value = 0;
- _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
+ _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_INT32, &value);
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_INT32);
-
- return _dbus_demarshal_int32 (&real->message->body, real->message->byte_order,
- pos, NULL);
+ return value;
}
/**
dbus_uint32_t
dbus_message_iter_get_uint32 (DBusMessageIter *iter)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- int type, pos;
+ dbus_int32_t value = 0;
- _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
+ _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_UINT32, &value);
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_UINT32);
-
- return _dbus_demarshal_uint32 (&real->message->body, real->message->byte_order,
- pos, NULL);
+ return value;
}
#ifdef DBUS_HAVE_INT64
dbus_int64_t
dbus_message_iter_get_int64 (DBusMessageIter *iter)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- int type, pos;
+ dbus_int64_t value = 0;
- _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
+ _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_INT64, &value);
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_INT64);
-
- return _dbus_demarshal_int64 (&real->message->body, real->message->byte_order,
- pos, NULL);
+ return value;
}
/**
dbus_uint64_t
dbus_message_iter_get_uint64 (DBusMessageIter *iter)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- int type, pos;
+ dbus_uint64_t value = 0;
- _dbus_return_val_if_fail (dbus_message_iter_check (real), 0);
+ _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_UINT64, &value);
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_UINT64);
-
- return _dbus_demarshal_uint64 (&real->message->body, real->message->byte_order,
- pos, NULL);
+ return value;
}
#endif /* DBUS_HAVE_INT64 */
double
dbus_message_iter_get_double (DBusMessageIter *iter)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- int type, pos;
+ double value = 0.0;
- _dbus_return_val_if_fail (dbus_message_iter_check (real), 0.0);
+ _dbus_message_iter_get_basic_type (iter, DBUS_TYPE_DOUBLE, &value);
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_DOUBLE);
-
- return _dbus_demarshal_double (&real->message->body, real->message->byte_order,
- pos, NULL);
+ return value;
}
/**
* @param iter the iterator
* @param array_iter pointer to an iterator to initialize
* @param array_type gets set to the type of the array elements
- * @returns #TRUE on success
+ * @returns #FALSE if the array is empty
*/
dbus_bool_t
dbus_message_iter_init_array_iterator (DBusMessageIter *iter,
if (array_type != NULL)
*array_type = _array_type;
-
- return TRUE;
+
+ return len > 0;
}
*
* @param iter the iterator
* @param dict_iter pointer to an iterator to initialize
- * @returns #TRUE on success
+ * @returns #FALSE if the dict is empty
*/
dbus_bool_t
dbus_message_iter_init_dict_iterator (DBusMessageIter *iter,
dict_real->container_length_pos = len_pos;
dict_real->wrote_dict_key = 0;
- return TRUE;
+ return len > 0;
+}
+
+static dbus_bool_t
+_dbus_message_iter_get_basic_type_array (DBusMessageIter *iter,
+ char type,
+ void **array,
+ int *array_len)
+{
+ DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
+ int item_type, pos;
+
+ _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
+
+ pos = dbus_message_iter_get_data_start (real, &item_type);
+
+ _dbus_assert (item_type == DBUS_TYPE_ARRAY);
+
+ item_type = iter_get_array_type (real, NULL);
+
+ _dbus_assert (type == item_type);
+
+ return _dbus_demarshal_basic_type_array (&real->message->body,
+ item_type, array, array_len,
+ real->message->byte_order, &pos);
}
/**
unsigned char **value,
int *len)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- int type, pos;
-
- _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
-
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_ARRAY);
-
- type = iter_get_array_type (real, NULL);
-
- _dbus_assert (type == DBUS_TYPE_BYTE);
-
- if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
- pos, NULL, value, len))
- return FALSE;
- else
- return TRUE;
+ return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_BYTE,
+ (void **) value, len);
}
/**
unsigned char **value,
int *len)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- int type, pos;
-
- _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
-
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_ARRAY);
-
- type = iter_get_array_type (real, NULL);
-
- _dbus_assert (type == DBUS_TYPE_BOOLEAN);
-
- if (!_dbus_demarshal_byte_array (&real->message->body, real->message->byte_order,
- pos, NULL, value, len))
- return FALSE;
- else
- return TRUE;
+ return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_BOOLEAN,
+ (void **) value, len);
}
/**
dbus_int32_t **value,
int *len)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- int type, pos;
-
- _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
-
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_ARRAY);
-
- type = iter_get_array_type (real, NULL);
-
- _dbus_assert (type == DBUS_TYPE_INT32);
-
- if (!_dbus_demarshal_int32_array (&real->message->body, real->message->byte_order,
- pos, NULL, value, len))
- return FALSE;
- else
- return TRUE;
+ return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_INT32,
+ (void **) value, len);
}
/**
dbus_uint32_t **value,
int *len)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- int type, pos;
-
- _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
-
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_ARRAY);
-
- type = iter_get_array_type (real, NULL);
- _dbus_assert (type == DBUS_TYPE_UINT32);
-
- if (!_dbus_demarshal_uint32_array (&real->message->body, real->message->byte_order,
- pos, NULL, value, len))
- return FALSE;
- else
- return TRUE;
+ return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_UINT32,
+ (void **) value, len);
}
#ifdef DBUS_HAVE_INT64
dbus_int64_t **value,
int *len)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- int type, pos;
-
- _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
-
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_ARRAY);
-
- type = iter_get_array_type (real, NULL);
-
- _dbus_assert (type == DBUS_TYPE_INT64);
-
- if (!_dbus_demarshal_int64_array (&real->message->body, real->message->byte_order,
- pos, NULL, value, len))
- return FALSE;
- else
- return TRUE;
+ return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_INT64,
+ (void **) value, len);
}
/**
dbus_uint64_t **value,
int *len)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
- int type, pos;
-
- _dbus_return_val_if_fail (dbus_message_iter_check (real), FALSE);
-
- pos = dbus_message_iter_get_data_start (real, &type);
-
- _dbus_assert (type == DBUS_TYPE_ARRAY);
-
- type = iter_get_array_type (real, NULL);
- _dbus_assert (type == DBUS_TYPE_UINT64);
-
- if (!_dbus_demarshal_uint64_array (&real->message->body, real->message->byte_order,
- pos, NULL, value, len))
- return FALSE;
- else
- return TRUE;
+ return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_UINT64,
+ (void **) value, len);
}
#endif /* DBUS_HAVE_INT64 */
double **value,
int *len)
{
+ return _dbus_message_iter_get_basic_type_array (iter, DBUS_TYPE_DOUBLE,
+ (void **) value, len);
+}
+
+/**
+ * Returns the string array that the iterator may point to.
+ * Note that you need to check that the iterator points
+ * to a string array prior to using this function.
+ *
+ * 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 #TRUE on success
+ */
+dbus_bool_t
+dbus_message_iter_get_string_array (DBusMessageIter *iter,
+ char ***value,
+ int *len)
+{
DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
int type, pos;
_dbus_assert (type == DBUS_TYPE_ARRAY);
type = iter_get_array_type (real, NULL);
- _dbus_assert (type == DBUS_TYPE_DOUBLE);
+ _dbus_assert (type == DBUS_TYPE_STRING);
- if (!_dbus_demarshal_double_array (&real->message->body, real->message->byte_order,
+ if (!_dbus_demarshal_string_array (&real->message->body, real->message->byte_order,
pos, NULL, value, len))
return FALSE;
else
}
/**
- * Returns the string array that the iterator may point to.
+ * Returns the object path 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.
+ * to an object path array prior to using this function.
*
* 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().
+ * array with dbus_free_string_array().
*
* @param iter the iterator
* @param value return location for string values
* @returns #TRUE on success
*/
dbus_bool_t
-dbus_message_iter_get_string_array (DBusMessageIter *iter,
- char ***value,
- int *len)
+dbus_message_iter_get_object_path_array (DBusMessageIter *iter,
+ char ***value,
+ int *len)
{
DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
int type, pos;
_dbus_assert (type == DBUS_TYPE_ARRAY);
type = iter_get_array_type (real, NULL);
- _dbus_assert (type == DBUS_TYPE_STRING);
+ _dbus_assert (type == DBUS_TYPE_OBJECT_PATH);
if (!_dbus_demarshal_string_array (&real->message->body, real->message->byte_order,
pos, NULL, value, len))
int type)
{
const char *data;
+
switch (iter->type)
{
case DBUS_MESSAGE_ITER_TYPE_MESSAGE:
if (!_dbus_string_append_byte (&iter->message->body, type))
- return FALSE;
+ return FALSE;
+
+ if (!_dbus_message_append_byte_to_signature (iter->message, type))
+ {
+ _dbus_string_shorten (&iter->message->body, 1);
+ return FALSE;
+ }
break;
case DBUS_MESSAGE_ITER_TYPE_ARRAY:
}
if (!_dbus_string_append_byte (&iter->message->body, type))
- return FALSE;
+ return FALSE;
break;
return TRUE;
}
+static dbus_bool_t
+dbus_message_iter_append_basic (DBusMessageIter *iter,
+ char type,
+ void *value)
+{
+ DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
+
+ _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
+
+ if (!dbus_message_iter_append_type (real, type))
+ return FALSE;
+
+ if (!_dbus_marshal_basic_type (&real->message->body,
+ type, value,
+ real->message->byte_order))
+ {
+ _dbus_string_set_length (&real->message->body, real->pos);
+ return FALSE;
+ }
+
+ dbus_message_iter_append_done (real);
+
+ return TRUE;
+}
+
/**
* Appends a boolean value to the message
*
dbus_message_iter_append_boolean (DBusMessageIter *iter,
dbus_bool_t value)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
-
- _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!dbus_message_iter_append_type (real, DBUS_TYPE_BOOLEAN))
- return FALSE;
-
- if (!_dbus_string_append_byte (&real->message->body, (value != FALSE)))
- {
- _dbus_string_set_length (&real->message->body, real->pos);
- return FALSE;
- }
-
- dbus_message_iter_append_done (real);
-
- return TRUE;
+ unsigned char val = (value != FALSE);
+ return dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &val);
}
/**
dbus_message_iter_append_byte (DBusMessageIter *iter,
unsigned char value)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
-
- _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!dbus_message_iter_append_type (real, DBUS_TYPE_BYTE))
- return FALSE;
-
- if (!_dbus_string_append_byte (&real->message->body, value))
- {
- _dbus_string_set_length (&real->message->body, real->pos);
- return FALSE;
- }
-
- dbus_message_iter_append_done (real);
-
- return TRUE;
+ return dbus_message_iter_append_basic (iter, DBUS_TYPE_BYTE, &value);
}
-
/**
* Appends a 32 bit signed integer to the message.
*
dbus_message_iter_append_int32 (DBusMessageIter *iter,
dbus_int32_t value)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
-
- _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!dbus_message_iter_append_type (real, DBUS_TYPE_INT32))
- return FALSE;
-
- if (!_dbus_marshal_int32 (&real->message->body, real->message->byte_order, value))
- {
- _dbus_string_set_length (&real->message->body, real->pos);
- return FALSE;
- }
-
- dbus_message_iter_append_done (real);
-
- return TRUE;
+ return dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &value);
}
/**
dbus_message_iter_append_uint32 (DBusMessageIter *iter,
dbus_uint32_t value)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
-
- _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!dbus_message_iter_append_type (real, DBUS_TYPE_UINT32))
- return FALSE;
-
- if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, value))
- {
- _dbus_string_set_length (&real->message->body, real->pos);
- return FALSE;
- }
-
- dbus_message_iter_append_done (real);
-
- return TRUE;
+ return dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT32, &value);
}
#ifdef DBUS_HAVE_INT64
dbus_message_iter_append_int64 (DBusMessageIter *iter,
dbus_int64_t value)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
-
- _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!dbus_message_iter_append_type (real, DBUS_TYPE_INT64))
- return FALSE;
-
- if (!_dbus_marshal_int64 (&real->message->body, real->message->byte_order, value))
- {
- _dbus_string_set_length (&real->message->body, real->pos);
- return FALSE;
- }
-
- dbus_message_iter_append_done (real);
-
- return TRUE;
+ return dbus_message_iter_append_basic (iter, DBUS_TYPE_INT64, &value);
}
/**
dbus_message_iter_append_uint64 (DBusMessageIter *iter,
dbus_uint64_t value)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
-
- _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!dbus_message_iter_append_type (real, DBUS_TYPE_UINT64))
- return FALSE;
-
- if (!_dbus_marshal_uint64 (&real->message->body, real->message->byte_order, value))
- {
- _dbus_string_set_length (&real->message->body, real->pos);
- return FALSE;
- }
-
- dbus_message_iter_append_done (real);
-
- return TRUE;
+ return dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT64, &value);
}
#endif /* DBUS_HAVE_INT64 */
dbus_message_iter_append_double (DBusMessageIter *iter,
double value)
{
+ return dbus_message_iter_append_basic (iter, DBUS_TYPE_DOUBLE, &value);
+}
+
+/**
+ * Appends a UTF-8 string to the message.
+ *
+ * @todo add return_val_if_fail(UTF-8 is valid)
+ *
+ * @param iter an iterator pointing to the end of the message
+ * @param value the string
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_iter_append_string (DBusMessageIter *iter,
+ const char *value)
+{
DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
_dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!dbus_message_iter_append_type (real, DBUS_TYPE_DOUBLE))
+
+ if (!dbus_message_iter_append_type (real, DBUS_TYPE_STRING))
return FALSE;
- if (!_dbus_marshal_double (&real->message->body, real->message->byte_order, value))
+ if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
{
_dbus_string_set_length (&real->message->body, real->pos);
return FALSE;
}
/**
- * Appends a UTF-8 string to the message.
+ * Appends an object path to the message.
+ *
+ * @todo add return_val_if_fail(UTF-8 is valid)
*
* @param iter an iterator pointing to the end of the message
- * @param value the string
+ * @param value the object path
* @returns #TRUE on success
*/
dbus_bool_t
-dbus_message_iter_append_string (DBusMessageIter *iter,
- const char *value)
+dbus_message_iter_append_object_path (DBusMessageIter *iter,
+ const char *value)
{
DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
_dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!dbus_message_iter_append_type (real, DBUS_TYPE_STRING))
+
+ if (!dbus_message_iter_append_type (real, DBUS_TYPE_OBJECT_PATH))
return FALSE;
if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, value))
}
/**
- * Appends a named type data chunk to the message. A named
+ * Appends a custom type data chunk to the message. A custom
* type is simply an arbitrary UTF-8 string used as a type
* tag, plus an array of arbitrary bytes to be interpreted
* according to the type tag.
* @returns #TRUE on success
*/
dbus_bool_t
-dbus_message_iter_append_named (DBusMessageIter *iter,
- const char *name,
- const unsigned char *data,
- int len)
+dbus_message_iter_append_custom (DBusMessageIter *iter,
+ const char *name,
+ const unsigned char *data,
+ int len)
{
DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
_dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
- if (!dbus_message_iter_append_type (real, DBUS_TYPE_NAMED))
+ if (!dbus_message_iter_append_type (real, DBUS_TYPE_CUSTOM))
return FALSE;
if (!_dbus_marshal_string (&real->message->body, real->message->byte_order, name))
if (real->wrote_dict_key)
{
- _dbus_warn ("Appendinging multiple dict key names\n");
+ _dbus_warn ("Appending multiple dict key names\n");
return FALSE;
}
{
if (array_type_pos != NULL)
*array_type_pos = _dbus_string_get_length (&real->message->body);
+
+
+ if (!_dbus_message_append_byte_to_signature (real->message, element_type))
+ {
+ _dbus_string_set_length (&real->message->body, real->pos);
+ return FALSE;
+ }
/* Append element type */
if (!_dbus_string_append_byte (&real->message->body, element_type))
{
+ _dbus_message_remove_byte_from_signature (real->message);
_dbus_string_set_length (&real->message->body, real->pos);
return FALSE;
}
if (element_type != DBUS_TYPE_ARRAY &&
!array_iter_type_mark_done (real))
- return FALSE;
+ {
+ _dbus_message_remove_byte_from_signature (real->message);
+ return FALSE;
+ }
}
return TRUE;
int len_pos;
_dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
+
if (!dbus_message_iter_append_type (real, DBUS_TYPE_DICT))
+ return FALSE;
+
+ len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
+
+ /* Empty length for now, backfill later */
+ if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
+ {
+ _dbus_string_set_length (&real->message->body, real->pos);
+ return FALSE;
+ }
+
+ dict_real->parent_iter = real;
+ dict_real->message = real->message;
+ dict_real->changed_stamp = real->message->changed_stamp;
+
+ dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
+ dict_real->pos = _dbus_string_get_length (&real->message->body);
+ dict_real->end = dict_real->end;
+
+ dict_real->container_start = dict_real->pos;
+ dict_real->container_length_pos = len_pos;
+ dict_real->wrote_dict_key = 0;
+
+ dbus_message_iter_append_done (dict_real);
+
+ real->wrote_dict_key = FALSE;
+
+ return TRUE;
+}
+
+static dbus_bool_t
+_dbus_message_iter_append_basic_array (DBusMessageIter *iter,
+ char type,
+ const void *value,
+ int len)
+{
+ DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
+
+ _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
+
+ if (!append_array_type (real, type, NULL, NULL))
return FALSE;
+
+ if (!_dbus_marshal_basic_type_array (&real->message->body,
+ type, value, len,
+ real->message->byte_order))
+ {
+ _dbus_string_set_length (&real->message->body, real->pos);
+ return FALSE;
+ }
+
+ dbus_message_iter_append_done (real);
+
+ return TRUE;
+}
+
+
+/**
+ * This function takes a va_list for use by language bindings.
+ * It's otherwise the same as dbus_message_append_args().
+ *
+ * @todo: Shouldn't this function clean up the changes to the message
+ * on failures? (Yes)
+
+ * @see dbus_message_append_args.
+ * @param message the message
+ * @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_args_valist (DBusMessage *message,
+ int first_arg_type,
+ va_list var_args)
+{
+ int type, old_len;
+ DBusMessageIter iter;
+
+ _dbus_return_val_if_fail (message != NULL, FALSE);
+
+ old_len = _dbus_string_get_length (&message->body);
+
+ type = first_arg_type;
+
+ dbus_message_append_iter_init (message, &iter);
+
+ while (type != DBUS_TYPE_INVALID)
+ {
+ switch (type)
+ {
+ case DBUS_TYPE_NIL:
+ if (!dbus_message_iter_append_nil (&iter))
+ goto errorout;
+ break;
+ case DBUS_TYPE_BYTE:
+ /* FIXME if you pass an unsigned char to varargs it gets promoted to int,
+ * so probably we should read an int here.
+ */
+ if (!dbus_message_iter_append_byte (&iter, va_arg (var_args, unsigned char)))
+ goto errorout;
+ break;
+ case DBUS_TYPE_BOOLEAN:
+ if (!dbus_message_iter_append_boolean (&iter, va_arg (var_args, dbus_bool_t)))
+ goto errorout;
+ break;
+ case DBUS_TYPE_INT32:
+ /* FIXME this is probably wrong, because an int passed in probably gets
+ * converted to plain "int" not necessarily 32-bit.
+ */
+ if (!dbus_message_iter_append_int32 (&iter, va_arg (var_args, dbus_int32_t)))
+ goto errorout;
+ break;
+ case DBUS_TYPE_UINT32:
+ /* FIXME this is probably wrong, because an int passed in probably gets
+ * converted to plain "int" not necessarily 32-bit.
+ */
+ if (!dbus_message_iter_append_uint32 (&iter, va_arg (var_args, dbus_uint32_t)))
+ goto errorout;
+ break;
+#ifdef DBUS_HAVE_INT64
+ case DBUS_TYPE_INT64:
+ if (!dbus_message_iter_append_int64 (&iter, va_arg (var_args, dbus_int64_t)))
+ goto errorout;
+ break;
+ case DBUS_TYPE_UINT64:
+ if (!dbus_message_iter_append_uint64 (&iter, va_arg (var_args, dbus_uint64_t)))
+ goto errorout;
+ break;
+#endif /* DBUS_HAVE_INT64 */
+ case DBUS_TYPE_DOUBLE:
+ if (!dbus_message_iter_append_double (&iter, va_arg (var_args, double)))
+ goto errorout;
+ break;
+ case DBUS_TYPE_STRING:
+ if (!dbus_message_iter_append_string (&iter, va_arg (var_args, const char *)))
+ goto errorout;
+ break;
+ case DBUS_TYPE_OBJECT_PATH:
+ if (!dbus_message_iter_append_object_path (&iter, va_arg (var_args, const char*)))
+ goto errorout;
+ break;
+ case DBUS_TYPE_CUSTOM:
+ {
+ const char *name;
+ unsigned char *data;
+ int len;
+
+ name = va_arg (var_args, const char *);
+ data = va_arg (var_args, unsigned char *);
+ len = va_arg (var_args, int);
+
+ if (!dbus_message_iter_append_custom (&iter, name, data, len))
+ goto errorout;
+ break;
+ }
+ case DBUS_TYPE_ARRAY:
+ {
+ void *data;
+ int len, type;
+
+ type = va_arg (var_args, int);
+ data = va_arg (var_args, void *);
+ len = va_arg (var_args, int);
- len_pos = _DBUS_ALIGN_VALUE (_dbus_string_get_length (&real->message->body), sizeof (dbus_uint32_t));
+ switch (type)
+ {
+ case DBUS_TYPE_BYTE:
+ case DBUS_TYPE_BOOLEAN:
+ case DBUS_TYPE_INT32:
+ case DBUS_TYPE_UINT32:
+#ifdef DBUS_HAVE_INT64
+ case DBUS_TYPE_INT64:
+ case DBUS_TYPE_UINT64:
+#endif /* DBUS_HAVE_INT64 */
+ case DBUS_TYPE_DOUBLE:
+ if (!_dbus_message_iter_append_basic_array (&iter, type, data, len))
+ goto errorout;
+ break;
+ case DBUS_TYPE_STRING:
+ if (!dbus_message_iter_append_string_array (&iter, (const char **)data, len))
+ goto errorout;
+ break;
+ case DBUS_TYPE_OBJECT_PATH:
+ if (!dbus_message_iter_append_object_path_array (&iter, (const char **)data, len))
+ goto errorout;
+ break;
+ case DBUS_TYPE_NIL:
+ case DBUS_TYPE_ARRAY:
+ case DBUS_TYPE_CUSTOM:
+ case DBUS_TYPE_DICT:
+ _dbus_warn ("dbus_message_append_args_valist doesn't support recursive arrays\n");
+ goto errorout;
+ default:
+ _dbus_warn ("Unknown field type %d\n", type);
+ goto errorout;
+ }
+ }
+ break;
+
+ case DBUS_TYPE_DICT:
+ _dbus_warn ("dbus_message_append_args_valist doesn't support dicts\n");
+ goto errorout;
+ default:
+ _dbus_warn ("Unknown field type %d\n", type);
+ goto errorout;
+ }
- /* Empty length for now, backfill later */
- if (!_dbus_marshal_uint32 (&real->message->body, real->message->byte_order, 0))
- {
- _dbus_string_set_length (&real->message->body, real->pos);
- return FALSE;
+ type = va_arg (var_args, int);
}
-
- dict_real->parent_iter = real;
- dict_real->message = real->message;
- dict_real->changed_stamp = real->message->changed_stamp;
-
- dict_real->type = DBUS_MESSAGE_ITER_TYPE_DICT;
- dict_real->pos = _dbus_string_get_length (&real->message->body);
- dict_real->end = dict_real->end;
-
- dict_real->container_start = dict_real->pos;
- dict_real->container_length_pos = len_pos;
- dict_real->wrote_dict_key = 0;
- dbus_message_iter_append_done (dict_real);
-
return TRUE;
-}
+ errorout:
+ return FALSE;
+}
/**
* Appends a boolean array to the message.
unsigned const char *value,
int len)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
-
- _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!append_array_type (real, DBUS_TYPE_BOOLEAN, NULL, NULL))
- return FALSE;
-
- if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, value, len))
- {
- _dbus_string_set_length (&real->message->body, real->pos);
- return FALSE;
- }
-
- dbus_message_iter_append_done (real);
-
- return TRUE;
+ return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_BOOLEAN,
+ value, len);
}
/**
const dbus_int32_t *value,
int len)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
-
- _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!append_array_type (real, DBUS_TYPE_INT32, NULL, NULL))
- return FALSE;
-
- if (!_dbus_marshal_int32_array (&real->message->body, real->message->byte_order, value, len))
- {
- _dbus_string_set_length (&real->message->body, real->pos);
- return FALSE;
- }
-
- dbus_message_iter_append_done (real);
-
- return TRUE;
+ return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_INT32,
+ value, len);
}
/**
const dbus_uint32_t *value,
int len)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
-
- _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!append_array_type (real, DBUS_TYPE_UINT32, NULL, NULL))
- return FALSE;
-
- if (!_dbus_marshal_uint32_array (&real->message->body, real->message->byte_order, value, len))
- {
- _dbus_string_set_length (&real->message->body, real->pos);
- return FALSE;
- }
-
- dbus_message_iter_append_done (real);
-
- return TRUE;
+ return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_UINT32,
+ value, len);
}
#ifdef DBUS_HAVE_INT64
const dbus_int64_t *value,
int len)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
-
- _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!append_array_type (real, DBUS_TYPE_INT64, NULL, NULL))
- return FALSE;
-
- if (!_dbus_marshal_int64_array (&real->message->body, real->message->byte_order, value, len))
- {
- _dbus_string_set_length (&real->message->body, real->pos);
- return FALSE;
- }
-
- dbus_message_iter_append_done (real);
-
- return TRUE;
+ return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_INT64,
+ value, len);
}
/**
const dbus_uint64_t *value,
int len)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
-
- _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!append_array_type (real, DBUS_TYPE_UINT64, NULL, NULL))
- return FALSE;
-
- if (!_dbus_marshal_uint64_array (&real->message->body, real->message->byte_order, value, len))
- {
- _dbus_string_set_length (&real->message->body, real->pos);
- return FALSE;
- }
-
- dbus_message_iter_append_done (real);
-
- return TRUE;
+ return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_UINT64,
+ value, len);
}
#endif /* DBUS_HAVE_INT64 */
const double *value,
int len)
{
- DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
-
- _dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
-
- if (!append_array_type (real, DBUS_TYPE_DOUBLE, NULL, NULL))
- return FALSE;
-
- if (!_dbus_marshal_double_array (&real->message->body, real->message->byte_order, value, len))
- {
- _dbus_string_set_length (&real->message->body, real->pos);
- return FALSE;
- }
-
- dbus_message_iter_append_done (real);
-
- return TRUE;
+ return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_DOUBLE,
+ value, len);
}
/**
unsigned const char *value,
int len)
{
+ return _dbus_message_iter_append_basic_array (iter, DBUS_TYPE_BYTE,
+ value, len);
+}
+
+/**
+ * Appends a string array to the message.
+ *
+ * @param iter an iterator pointing to the end of the message
+ * @param value the array
+ * @param len the length of the array
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_iter_append_string_array (DBusMessageIter *iter,
+ const char **value,
+ int len)
+{
DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
_dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
- if (!append_array_type (real, DBUS_TYPE_BYTE, NULL, NULL))
+ if (!append_array_type (real, DBUS_TYPE_STRING, NULL, NULL))
return FALSE;
- if (!_dbus_marshal_byte_array (&real->message->body, real->message->byte_order, value, len))
+ if (!_dbus_marshal_string_array (&real->message->body, real->message->byte_order, value, len))
{
_dbus_string_set_length (&real->message->body, real->pos);
return FALSE;
}
/**
- * Appends a string array to the message.
+ * Appends an object path array to the message.
*
* @param iter an iterator pointing to the end of the message
* @param value the array
* @returns #TRUE on success
*/
dbus_bool_t
-dbus_message_iter_append_string_array (DBusMessageIter *iter,
- const char **value,
- int len)
+dbus_message_iter_append_object_path_array (DBusMessageIter *iter,
+ const char **value,
+ int len)
{
DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
_dbus_return_val_if_fail (dbus_message_iter_append_check (real), FALSE);
- if (!append_array_type (real, DBUS_TYPE_STRING, NULL, NULL))
+ if (!append_array_type (real, DBUS_TYPE_OBJECT_PATH, NULL, NULL))
return FALSE;
if (!_dbus_marshal_string_array (&real->message->body, real->message->byte_order, value, len))
* Sets the message sender.
*
* @param message the message
- * @param sender the sender
+ * @param sender the sender or #NULL to unset
* @returns #FALSE if not enough memory
*/
dbus_bool_t
_dbus_return_val_if_fail (message != NULL, FALSE);
_dbus_return_val_if_fail (!message->locked, FALSE);
- if (sender == NULL)
- {
- delete_field (message, DBUS_HEADER_FIELD_SENDER_SERVICE);
- return TRUE;
- }
- else
- {
- return set_string_field (message,
- DBUS_HEADER_FIELD_SENDER_SERVICE,
- DBUS_TYPE_STRING,
- sender);
- }
+ return set_string_field (message,
+ DBUS_HEADER_FIELD_SENDER,
+ DBUS_TYPE_STRING,
+ sender);
}
/**
return (*header & DBUS_HEADER_FLAG_NO_REPLY_EXPECTED) != 0;
}
+
+/**
+ * Sets a flag indicating that the addressed service will be auto-activated
+ * before the message is delivered. When this flag is set, the message is held
+ * until the service is succesfully activated or fail to activate. In case of
+ * failure, the reply will be an activation error.
+ *
+ * @param message the message
+ * @param auto_activation #TRUE if auto-activation is desired
+ */
+void
+dbus_message_set_auto_activation (DBusMessage *message,
+ dbus_bool_t auto_activation)
+{
+ char *header;
+
+ _dbus_return_if_fail (message != NULL);
+ _dbus_return_if_fail (!message->locked);
+
+ header = _dbus_string_get_data_len (&message->header, FLAGS_OFFSET, 1);
+
+ if (auto_activation)
+ *header |= DBUS_HEADER_FLAG_AUTO_ACTIVATION;
+ else
+ *header &= ~DBUS_HEADER_FLAG_AUTO_ACTIVATION;
+}
+
+/**
+ * Returns #TRUE if the message will cause the addressed service to be
+ * auto-activated.
+ *
+ * @param message the message
+ * @returns #TRUE if the message will use auto-activation
+ */
+dbus_bool_t
+dbus_message_get_auto_activation (DBusMessage *message)
+{
+ const char *header;
+
+ _dbus_return_val_if_fail (message != NULL, FALSE);
+
+ header = _dbus_string_get_const_data_len (&message->header, FLAGS_OFFSET, 1);
+
+ return (*header & DBUS_HEADER_FLAG_AUTO_ACTIVATION) != 0;
+}
+
/**
* Gets the service which originated this message,
* or #NULL if unknown or inapplicable.
_dbus_return_val_if_fail (message != NULL, NULL);
return get_string_field (message,
- DBUS_HEADER_FIELD_SENDER_SERVICE,
+ DBUS_HEADER_FIELD_SENDER,
+ NULL);
+}
+
+/**
+ * Gets the type signature of the message, i.e. the arguments in the
+ * message payload. The signature includes only "in" arguments for
+ * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
+ * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
+ * what you might expect (it does not include the signature of the
+ * entire C++-style method).
+ *
+ * The signature is a string made up of type codes such
+ * as #DBUS_TYPE_STRING. The string is terminated with nul
+ * (nul is also the value of #DBUS_TYPE_INVALID).
+ *
+ * @param message the message
+ * @returns the type signature
+ */
+const char*
+dbus_message_get_signature (DBusMessage *message)
+{
+ _dbus_return_val_if_fail (message != NULL, NULL);
+
+ return get_string_field (message,
+ DBUS_HEADER_FIELD_SIGNATURE,
NULL);
}
*/
dbus_bool_t
dbus_message_is_error (DBusMessage *message,
- const char *error_name)
+ const char *error_name)
{
const char *n;
_dbus_return_val_if_fail (message != NULL, FALSE);
_dbus_return_val_if_fail (error_name != NULL, FALSE);
-
+ _dbus_return_val_if_fail (is_valid_error_name (error_name), FALSE);
+
if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
return FALSE;
{
const char *s;
- _dbus_assert (service != NULL);
+ _dbus_return_val_if_fail (message != NULL, FALSE);
+ _dbus_return_val_if_fail (service != NULL, FALSE);
s = dbus_message_get_sender (message);
}
/**
+ * Checks whether the message has the given signature;
+ * see dbus_message_get_signature() for more details on
+ * what the signature looks like.
+ *
+ * @param message the message
+ * @param signature typecode array
+ * @returns #TRUE if message has the given signature
+*/
+dbus_bool_t
+dbus_message_has_signature (DBusMessage *message,
+ const char *signature)
+{
+ const char *s;
+
+ _dbus_return_val_if_fail (message != NULL, FALSE);
+ _dbus_return_val_if_fail (signature != NULL, FALSE);
+
+ s = dbus_message_get_signature (message);
+
+ if (s && strcmp (s, signature) == 0)
+ return TRUE;
+ else
+ return FALSE;
+}
+
+/**
* Sets a #DBusError based on the contents of the given
* message. The error is only set if the message
* is an error message, as in DBUS_MESSAGE_TYPE_ERROR.
* Increments the reference count of the loader.
*
* @param loader the loader.
+ * @returns the loader
*/
-void
+DBusMessageLoader *
_dbus_message_loader_ref (DBusMessageLoader *loader)
{
loader->refcount += 1;
+
+ return loader;
}
/**
_dbus_string_init_const (field_data,
_dbus_string_get_const_data (data) + string_data_pos);
- header_field->name_offset = pos;
+ header_field->name_offset = pos - 2;
header_field->value_offset = _DBUS_ALIGN_VALUE (pos, 4);
#if 0
return TRUE;
}
+/* FIXME because the service/interface/member/error names are already
+ * validated to be in the particular ASCII subset, UTF-8 validating
+ * them could be skipped as a probably-interesting optimization.
+ * The UTF-8 validation definitely shows up in profiles.
+ */
static dbus_bool_t
decode_header_data (const DBusString *data,
int header_len,
int i;
int field;
int type;
+ dbus_bool_t signature_required;
if (header_len < 16)
{
if (!_dbus_marshal_validate_type (data, pos, &type, &pos))
{
- _dbus_verbose ("Failed to validate type of named header field\n");
+ _dbus_verbose ("Failed to validate type of named header field pos = %d\n",
+ pos);
return FALSE;
}
if (!_dbus_marshal_validate_arg (data, byte_order, 0, type, -1, pos, &new_pos))
{
- _dbus_verbose ("Failed to validate argument to named header field\n");
+ _dbus_verbose ("Failed to validate argument to named header field pos = %d\n",
+ pos);
return FALSE;
}
switch (field)
{
- case DBUS_HEADER_FIELD_SERVICE:
+ case DBUS_HEADER_FIELD_DESTINATION:
if (!decode_string_field (data, field, &fields[field],
&field_data, pos, type))
return FALSE;
}
break;
- case DBUS_HEADER_FIELD_SENDER_SERVICE:
+ case DBUS_HEADER_FIELD_SENDER:
if (!decode_string_field (data, field, &fields[field],
&field_data, pos, type))
return FALSE;
return FALSE;
}
- fields[field].name_offset = pos;
+ fields[field].name_offset = pos - 2;
fields[field].value_offset = _DBUS_ALIGN_VALUE (pos, 4);
/* No forging signals from the local path */
return FALSE;
}
- fields[field].name_offset = pos;
+ fields[field].name_offset = pos - 2;
fields[field].value_offset = _DBUS_ALIGN_VALUE (pos, 4);
- _dbus_verbose ("Found reply serial at offset %d\n",
- fields[field].value_offset);
+ _dbus_verbose ("Found reply serial %u at offset %d\n",
+ _dbus_demarshal_uint32 (data,
+ byte_order,
+ fields[field].value_offset,
+ NULL),
+ fields[field].value_offset);
+ break;
+
+ case DBUS_HEADER_FIELD_SIGNATURE:
+ if (!decode_string_field (data, field, &fields[field],
+ &field_data, pos, type))
+ return FALSE;
+
+#if 0
+ /* FIXME */
+ if (!_dbus_string_validate_signature (&field_data, 0,
+ _dbus_string_get_length (&field_data)))
+ {
+ _dbus_verbose ("signature field has invalid content \"%s\"\n",
+ _dbus_string_get_const_data (&field_data));
+ return FALSE;
+ }
+#endif
break;
-
+
default:
_dbus_verbose ("Ignoring an unknown header field: %d at offset %d\n",
field, pos);
}
/* Depending on message type, enforce presence of certain fields. */
+ signature_required = TRUE;
+
switch (message_type)
{
case DBUS_MESSAGE_TYPE_SIGNAL:
_dbus_verbose ("No error-name field provided\n");
return FALSE;
}
+ if (fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset < 0)
+ {
+ _dbus_verbose ("No reply serial field provided in error\n");
+ return FALSE;
+ }
break;
case DBUS_MESSAGE_TYPE_METHOD_RETURN:
+ if (fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset < 0)
+ {
+ _dbus_verbose ("No reply serial field provided in method return\n");
+ return FALSE;
+ }
break;
default:
/* An unknown type, spec requires us to ignore it */
+ signature_required = FALSE;
break;
}
+
+ /* FIXME allow omitting signature field for a message with no arguments? */
+ if (signature_required)
+ {
+ if (fields[DBUS_HEADER_FIELD_SIGNATURE].value_offset < 0)
+ {
+ _dbus_verbose ("No signature field provided\n");
+ return FALSE;
+ }
+ }
if (message_padding)
*message_padding = header_len - pos;
loader->buffer_outstanding = FALSE;
}
+static dbus_bool_t
+load_one_message (DBusMessageLoader *loader,
+ int byte_order,
+ int message_type,
+ int header_len,
+ int body_len)
+{
+ DBusMessage *message;
+ HeaderField fields[DBUS_HEADER_FIELD_LAST + 1];
+ int i;
+ int next_arg;
+ dbus_bool_t oom;
+ int header_padding;
+
+ message = NULL;
+ oom = FALSE;
+
+#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,
+ message_type,
+ fields, &header_padding))
+ {
+ _dbus_verbose ("Header was invalid\n");
+ loader->corrupted = TRUE;
+ goto failed;
+ }
+
+ next_arg = header_len;
+ while (next_arg < (header_len + body_len))
+ {
+ int type;
+ int prev = next_arg;
+
+ if (!_dbus_marshal_validate_type (&loader->data, next_arg,
+ &type, &next_arg))
+ {
+ _dbus_verbose ("invalid typecode at offset %d\n", prev);
+ loader->corrupted = TRUE;
+ goto failed;
+ }
+
+ if (!_dbus_marshal_validate_arg (&loader->data,
+ byte_order,
+ 0,
+ type, -1,
+ next_arg,
+ &next_arg))
+ {
+ _dbus_verbose ("invalid type data at %d, next_arg\n", next_arg);
+ loader->corrupted = TRUE;
+ goto failed;
+ }
+
+ _dbus_assert (next_arg > prev);
+ }
+
+ if (next_arg > (header_len + body_len))
+ {
+ _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
+ next_arg, header_len, body_len,
+ header_len + body_len);
+ loader->corrupted = TRUE;
+ goto failed;
+ }
+
+ message = dbus_message_new_empty_header ();
+ if (message == NULL)
+ {
+ _dbus_verbose ("Failed to allocate empty message\n");
+ oom = TRUE;
+ goto failed;
+ }
+
+ message->byte_order = byte_order;
+ message->header_padding = header_padding;
+
+ /* Copy in the offsets we found */
+ i = 0;
+ while (i <= DBUS_HEADER_FIELD_LAST)
+ {
+ message->header_fields[i] = fields[i];
+ ++i;
+ }
+
+ if (!_dbus_list_append (&loader->messages, message))
+ {
+ _dbus_verbose ("Failed to append new message to loader queue\n");
+ oom = TRUE;
+ goto failed;
+ }
+
+ _dbus_assert (_dbus_string_get_length (&message->header) == 0);
+ _dbus_assert (_dbus_string_get_length (&message->body) == 0);
+
+ _dbus_assert (_dbus_string_get_length (&loader->data) >=
+ (header_len + body_len));
+
+ if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
+ {
+ _dbus_verbose ("Failed to move header into new message\n");
+ oom = TRUE;
+ goto failed;
+ }
+
+ if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
+ {
+ _dbus_verbose ("Failed to move body into new message\n");
+
+ oom = TRUE;
+ goto failed;
+ }
+
+ _dbus_assert (_dbus_string_get_length (&message->header) == header_len);
+ _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
+
+ /* Fill in caches (we checked the types of these fields
+ * earlier)
+ */
+ message->reply_serial = get_uint_field (message,
+ DBUS_HEADER_FIELD_REPLY_SERIAL);
+
+ message->client_serial = _dbus_demarshal_uint32 (&message->header,
+ message->byte_order,
+ CLIENT_SERIAL_OFFSET,
+ NULL);
+ if (message->client_serial == 0 ||
+ (message->header_fields[DBUS_HEADER_FIELD_REPLY_SERIAL].value_offset >= 0 && message->reply_serial == 0))
+ {
+ _dbus_verbose ("client_serial = %d reply_serial = %d, one of these no good\n",
+ message->client_serial,
+ message->reply_serial);
+
+ loader->corrupted = TRUE;
+ goto failed;
+ }
+
+ _dbus_verbose ("Loaded message %p\n", message);
+
+ _dbus_assert (!oom);
+ _dbus_assert (!loader->corrupted);
+
+ return TRUE;
+
+ failed:
+
+ /* Clean up */
+
+ if (message != NULL)
+ {
+ /* Put the data back so we can try again later if it was an OOM issue */
+ if (_dbus_string_get_length (&message->body) > 0)
+ {
+ dbus_bool_t result;
+
+ result = _dbus_string_copy_len (&message->body, 0, body_len,
+ &loader->data, 0);
+
+ _dbus_assert (result); /* because DBusString never reallocs smaller */
+ }
+
+ if (_dbus_string_get_length (&message->header) > 0)
+ {
+ dbus_bool_t result;
+
+ result = _dbus_string_copy_len (&message->header, 0, header_len,
+ &loader->data, 0);
+
+ _dbus_assert (result); /* because DBusString never reallocs smaller */
+ }
+
+ /* does nothing if the message isn't in the list */
+ _dbus_list_remove_last (&loader->messages, message);
+
+ dbus_message_unref (message);
+ }
+
+
+ return !oom;
+}
+
/**
* Converts buffered data into messages.
*
dbus_bool_t
_dbus_message_loader_queue_messages (DBusMessageLoader *loader)
{
- if (loader->corrupted)
- return TRUE;
-
- while (_dbus_string_get_length (&loader->data) >= 16)
+ while (!loader->corrupted && _dbus_string_get_length (&loader->data) >= 16)
{
- DBusMessage *message;
const char *header_data;
- int byte_order, message_type, header_len, body_len, header_padding;
+ int byte_order, message_type, header_len, body_len;
dbus_uint32_t header_len_unsigned, body_len_unsigned;
header_data = _dbus_string_get_const_data_len (&loader->data, 0, 16);
if (_dbus_string_get_length (&loader->data) >= (header_len + body_len))
{
- HeaderField fields[DBUS_HEADER_FIELD_LAST + 1];
- 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,
- message_type,
- fields, &header_padding))
- {
- _dbus_verbose ("Header was invalid\n");
- loader->corrupted = TRUE;
- return TRUE;
- }
-
- next_arg = header_len;
- while (next_arg < (header_len + body_len))
- {
- int type;
- int prev = next_arg;
-
- if (!_dbus_marshal_validate_type (&loader->data, next_arg,
- &type, &next_arg))
- {
- _dbus_verbose ("invalid typecode at offset %d\n", prev);
- loader->corrupted = TRUE;
- return TRUE;
- }
-
- if (!_dbus_marshal_validate_arg (&loader->data,
- byte_order,
- 0,
- type, -1,
- next_arg,
- &next_arg))
- {
- _dbus_verbose ("invalid type data at %d, next_arg\n", next_arg);
- loader->corrupted = TRUE;
- return TRUE;
- }
-
- _dbus_assert (next_arg > prev);
- }
-
- if (next_arg > (header_len + body_len))
- {
- _dbus_verbose ("end of last arg at %d but message has len %d+%d=%d\n",
- next_arg, header_len, body_len,
- header_len + body_len);
- loader->corrupted = TRUE;
- return TRUE;
- }
-
- message = dbus_message_new_empty_header ();
- if (message == NULL)
- {
- _dbus_verbose ("Failed to allocate empty message\n");
- return FALSE;
- }
-
- message->byte_order = byte_order;
- message->header_padding = header_padding;
-
- /* Copy in the offsets we found */
- i = 0;
- while (i <= DBUS_HEADER_FIELD_LAST)
- {
- message->header_fields[i] = fields[i];
- ++i;
- }
-
- if (!_dbus_list_append (&loader->messages, message))
- {
- _dbus_verbose ("Failed to append new message to loader queue\n");
- dbus_message_unref (message);
- return FALSE;
- }
-
- _dbus_assert (_dbus_string_get_length (&message->header) == 0);
- _dbus_assert (_dbus_string_get_length (&message->body) == 0);
-
- _dbus_assert (_dbus_string_get_length (&loader->data) >=
- (header_len + body_len));
-
- if (!_dbus_string_move_len (&loader->data, 0, header_len, &message->header, 0))
- {
- _dbus_verbose ("Failed to move header into new message\n");
- _dbus_list_remove_last (&loader->messages, message);
- dbus_message_unref (message);
- return FALSE;
- }
-
- if (!_dbus_string_move_len (&loader->data, 0, body_len, &message->body, 0))
- {
- dbus_bool_t result;
-
- _dbus_verbose ("Failed to move body into new message\n");
-
- /* put the header back, we'll try again later */
- result = _dbus_string_copy_len (&message->header, 0, header_len,
- &loader->data, 0);
- _dbus_assert (result); /* because DBusString never reallocs smaller */
-
- _dbus_list_remove_last (&loader->messages, message);
- dbus_message_unref (message);
- 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 (we checked the types of these fields
- * earlier)
- */
- message->reply_serial = get_uint_field (message,
- DBUS_HEADER_FIELD_REPLY_SERIAL);
-
- message->client_serial = _dbus_demarshal_uint32 (&message->header,
- message->byte_order,
- CLIENT_SERIAL_OFFSET,
- NULL);
-
- _dbus_verbose ("Loaded message %p\n", message);
+ if (!load_one_message (loader, byte_order, message_type,
+ header_len, body_len))
+ return FALSE;
}
else
return TRUE;
#ifdef DBUS_BUILD_TESTS
#include "dbus-test.h"
#include <stdio.h>
+#include <stdlib.h>
static void
message_iter_test (DBusMessage *message)
{
- DBusMessageIter iter, dict, array, array2;
+ DBusMessageIter iter, dict, dict2, array, array2;
char *str;
unsigned char *data;
dbus_int32_t *our_int_array;
_dbus_assert_not_reached ("Array type not double");
- if (!dbus_message_iter_init_array_iterator (&iter, &array, NULL))
- _dbus_assert_not_reached ("Array init failed");
+ dbus_message_iter_init_array_iterator (&iter, &array, NULL);
if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_DOUBLE)
_dbus_assert_not_reached ("Argument type isn't double");
if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
_dbus_assert_not_reached ("not dict type");
- if (!dbus_message_iter_init_dict_iterator (&iter, &dict))
- _dbus_assert_not_reached ("dict iter failed");
+ dbus_message_iter_init_dict_iterator (&iter, &dict);
str = dbus_message_iter_get_dict_key (&dict);
if (str == NULL || strcmp (str, "test") != 0)
if (dbus_message_iter_get_uint32 (&dict) != 0xDEADBEEF)
_dbus_assert_not_reached ("wrong dict entry value");
+ /* dict (in dict) */
+
+ if (!dbus_message_iter_next (&dict))
+ _dbus_assert_not_reached ("reached end of dict");
+
+ if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_DICT)
+ _dbus_assert_not_reached ("not dict type");
+
+ dbus_message_iter_init_dict_iterator (&dict, &dict2);
+
+ str = dbus_message_iter_get_dict_key (&dict2);
+ if (str == NULL || strcmp (str, "dictkey") != 0)
+ _dbus_assert_not_reached ("wrong dict key");
+ dbus_free (str);
+
+ if (dbus_message_iter_get_arg_type (&dict2) != DBUS_TYPE_STRING)
+ _dbus_assert_not_reached ("wrong dict entry type");
+
+ str = dbus_message_iter_get_string (&dict2);
+ if (str == NULL || strcmp (str, "dictvalue") != 0)
+ _dbus_assert_not_reached ("wrong dict entry value");
+ dbus_free (str);
+
+ if (dbus_message_iter_next (&dict2))
+ _dbus_assert_not_reached ("didn't reach end of dict");
+
if (!dbus_message_iter_next (&dict))
_dbus_assert_not_reached ("reached end of dict");
if (dbus_message_iter_get_array_type (&dict) != DBUS_TYPE_ARRAY)
_dbus_assert_not_reached ("Array type not array");
- if (!dbus_message_iter_init_array_iterator (&dict, &array, NULL))
- _dbus_assert_not_reached ("Array init failed");
+ dbus_message_iter_init_array_iterator (&dict, &array, NULL);
if (dbus_message_iter_get_arg_type (&array) != DBUS_TYPE_ARRAY)
_dbus_assert_not_reached ("Argument type isn't array");
if (dbus_message_iter_get_array_type (&array) != DBUS_TYPE_INT32)
_dbus_assert_not_reached ("Array type not int32");
- if (!dbus_message_iter_init_array_iterator (&array, &array2, NULL))
- _dbus_assert_not_reached ("Array init failed");
+ dbus_message_iter_init_array_iterator (&array, &array2, NULL);
if (dbus_message_iter_get_arg_type (&array2) != DBUS_TYPE_INT32)
_dbus_assert_not_reached ("Argument type isn't int32");
if (!dbus_message_iter_next (&iter))
_dbus_assert_not_reached ("Reached end of arguments");
- if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_NAMED)
+ if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_CUSTOM)
_dbus_assert_not_reached ("wrong type after dict");
- if (!dbus_message_iter_get_named (&iter, &str, &data, &len))
- _dbus_assert_not_reached ("failed to get named");
+ if (!dbus_message_iter_get_custom (&iter, &str, &data, &len))
+ _dbus_assert_not_reached ("failed to get custom type");
- _dbus_assert (strcmp (str, "named")==0);
+ _dbus_assert (strcmp (str, "MyTypeName")==0);
_dbus_assert (len == 5);
_dbus_assert (strcmp (data, "data")==0);
dbus_free (str);
dbus_free (data);
+ if (!dbus_message_iter_next (&iter))
+ _dbus_assert_not_reached ("Reached end of arguments");
+
+ if (dbus_message_iter_get_byte (&iter) != 0xF0)
+ _dbus_assert_not_reached ("wrong value after custom");
+
+ if (!dbus_message_iter_next (&iter))
+ _dbus_assert_not_reached ("Reached end of arguments");
+
+ if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
+ _dbus_assert_not_reached ("no array");
+
+ if (dbus_message_iter_get_array_type (&iter) != DBUS_TYPE_INT32)
+ _dbus_assert_not_reached ("Array type not int32");
+
+ if (dbus_message_iter_init_array_iterator (&iter, &array, NULL))
+ _dbus_assert_not_reached ("non empty array");
+
+ if (!dbus_message_iter_next (&iter))
+ _dbus_assert_not_reached ("Reached end of arguments");
+
+ if (dbus_message_iter_get_byte (&iter) != 0xF0)
+ _dbus_assert_not_reached ("wrong value after empty array");
+
+ if (!dbus_message_iter_next (&iter))
+ _dbus_assert_not_reached ("Reached end of arguments");
+
+ if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
+ _dbus_assert_not_reached ("non dict");
+
+ if (dbus_message_iter_init_dict_iterator (&iter, &dict))
+ _dbus_assert_not_reached ("non empty dict");
+
+ if (!dbus_message_iter_next (&iter))
+ _dbus_assert_not_reached ("Reached end of arguments");
+
+ if (dbus_message_iter_get_byte (&iter) != 0xF0)
+ _dbus_assert_not_reached ("wrong value after empty dict");
+
if (dbus_message_iter_next (&iter))
_dbus_assert_not_reached ("Didn't reach end of arguments");
}
dbus_free (str);
}
break;
- case DBUS_TYPE_NAMED:
+ case DBUS_TYPE_CUSTOM:
{
char *name;
unsigned char *data;
int len;
- if (!dbus_message_iter_get_named (iter, &name, &data, &len))
+ if (!dbus_message_iter_get_custom (iter, &name, &data, &len))
{
- _dbus_warn ("error reading name from named type\n");
+ _dbus_warn ("error reading name from custom type\n");
return FALSE;
}
dbus_free (data);
{
int array_type;
- if (!dbus_message_iter_init_array_iterator (iter, &child_iter, &array_type))
- {
- _dbus_warn ("Failed to init array iterator\n");
- return FALSE;
- }
+ dbus_message_iter_init_array_iterator (iter, &child_iter, &array_type);
while (dbus_message_iter_has_next (&child_iter))
{
int entry_type;
char *key;
- if (!dbus_message_iter_init_dict_iterator (iter, &child_iter))
- {
- _dbus_warn ("Failed to init dict iterator\n");
- return FALSE;
- }
+ dbus_message_iter_init_dict_iterator (iter, &child_iter);
while ((entry_type = dbus_message_iter_get_arg_type (&child_iter)) != DBUS_TYPE_INVALID)
{
_dbus_assert_not_reached ("integers differ!");
#ifdef DBUS_HAVE_INT64
- if (our_int64 != -0x123456789abcd)
+ if (our_int64 != DBUS_INT64_CONSTANT (-0x123456789abcd))
_dbus_assert_not_reached ("64-bit integers differ!");
- if (our_uint64 != 0x123456789abcd)
+ if (our_uint64 != DBUS_UINT64_CONSTANT (0x123456789abcd))
_dbus_assert_not_reached ("64-bit unsigned integers differ!");
#endif
_dbus_assert_not_reached ("bool array had wrong values");
dbus_free (our_boolean_array);
-
- if (!dbus_message_iter_next (&iter))
- _dbus_assert_not_reached ("Reached end of arguments");
if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_DICT)
_dbus_assert_not_reached ("not dict type");
- if (!dbus_message_iter_init_dict_iterator (&iter, &dict))
- _dbus_assert_not_reached ("dict iter failed");
+ dbus_message_iter_init_dict_iterator (&iter, &dict);
our_str = dbus_message_iter_get_dict_key (&dict);
if (our_str == NULL || strcmp (our_str, "test") != 0)
const double our_double_array[] = { 0.1234, 9876.54321, -300.0 };
const unsigned char our_byte_array[] = { 'a', 'b', 'c', 234 };
const unsigned char our_boolean_array[] = { TRUE, FALSE, TRUE, TRUE, FALSE };
+ char sig[64];
+ const char *s;
+ char *t;
+ DBusError error;
_dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
dbus_message_set_no_reply (message, FALSE);
_dbus_assert (dbus_message_get_no_reply (message) == FALSE);
-#if 0
/* Set/get some header fields */
if (!dbus_message_set_path (message, "/foo"))
_dbus_assert_not_reached ("out of memory");
_dbus_assert (strcmp (dbus_message_get_member (message),
"Bar") == 0);
-#endif
dbus_message_unref (message);
dbus_message_append_args (message,
DBUS_TYPE_INT32, -0x12345678,
#ifdef DBUS_HAVE_INT64
- DBUS_TYPE_INT64, -0x123456789abcd,
- DBUS_TYPE_UINT64, 0x123456789abcd,
+ DBUS_TYPE_INT64, DBUS_INT64_CONSTANT (-0x123456789abcd),
+ DBUS_TYPE_UINT64, DBUS_UINT64_CONSTANT (0x123456789abcd),
#endif
DBUS_TYPE_STRING, "Test string",
DBUS_TYPE_DOUBLE, 3.14159,
dbus_message_iter_append_dict_key (&child_iter, "test");
dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
dbus_message_iter_append_uint32 (&iter, 0xCAFEBABE);
+
+ i = 0;
+ sig[i++] = DBUS_TYPE_INT32;
+#ifdef DBUS_HAVE_INT64
+ sig[i++] = DBUS_TYPE_INT64;
+ sig[i++] = DBUS_TYPE_UINT64;
+#endif
+ sig[i++] = DBUS_TYPE_STRING;
+ sig[i++] = DBUS_TYPE_DOUBLE;
+ sig[i++] = DBUS_TYPE_BOOLEAN;
+ sig[i++] = DBUS_TYPE_ARRAY;
+ sig[i++] = DBUS_TYPE_UINT32;
+ sig[i++] = DBUS_TYPE_ARRAY;
+ sig[i++] = DBUS_TYPE_INT32;
+#ifdef DBUS_HAVE_INT64
+ sig[i++] = DBUS_TYPE_ARRAY;
+ sig[i++] = DBUS_TYPE_UINT64;
+ sig[i++] = DBUS_TYPE_ARRAY;
+ sig[i++] = DBUS_TYPE_INT64;
+#endif
+ sig[i++] = DBUS_TYPE_ARRAY;
+ sig[i++] = DBUS_TYPE_STRING;
+ sig[i++] = DBUS_TYPE_ARRAY;
+ sig[i++] = DBUS_TYPE_DOUBLE;
+ sig[i++] = DBUS_TYPE_ARRAY;
+ sig[i++] = DBUS_TYPE_BYTE;
+ sig[i++] = DBUS_TYPE_ARRAY;
+ sig[i++] = DBUS_TYPE_BOOLEAN;
+ sig[i++] = DBUS_TYPE_DICT;
+ sig[i++] = DBUS_TYPE_UINT32;
+ sig[i++] = DBUS_TYPE_INVALID;
+
+ _dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
_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));
-
+
+ _dbus_verbose ("Signature expected \"%s\" actual \"%s\"\n",
+ sig, dbus_message_get_signature (message));
+
+ s = dbus_message_get_signature (message);
+
+ _dbus_assert (dbus_message_has_signature (message, sig));
+ _dbus_assert (strcmp (s, sig) == 0);
+
verify_test_message (message);
copy = dbus_message_copy (message);
_dbus_assert (_dbus_string_get_length (&message->body) ==
_dbus_string_get_length (©->body));
-
+
verify_test_message (copy);
name1 = dbus_message_get_interface (message);
_dbus_assert (strcmp (name1, name2) == 0);
- dbus_message_unref (message);
+ dbus_message_unref (message);
dbus_message_unref (copy);
message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
dbus_message_iter_append_dict_key (&child_iter, "test");
dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
+ /* dict (in dict) */
+ dbus_message_iter_append_dict_key (&child_iter, "testdict");
+ dbus_message_iter_append_dict (&child_iter, &child_iter2);
+
+ dbus_message_iter_append_dict_key (&child_iter2, "dictkey");
+ dbus_message_iter_append_string (&child_iter2, "dictvalue");
+
/* array of array of int32 (in dict) */
dbus_message_iter_append_dict_key (&child_iter, "array");
dbus_message_iter_append_array (&child_iter, &child_iter2, DBUS_TYPE_ARRAY);
dbus_message_iter_append_nil (&iter);
- dbus_message_iter_append_named (&iter, "named",
- "data", 5);
+ dbus_message_iter_append_custom (&iter, "MyTypeName",
+ "data", 5);
- message_iter_test (message);
+ dbus_message_iter_append_byte (&iter, 0xF0);
+
+ dbus_message_iter_append_array (&iter, &child_iter, DBUS_TYPE_INT32);
+
+ dbus_message_iter_append_byte (&iter, 0xF0);
+
+ dbus_message_iter_append_dict (&iter, &child_iter);
+ dbus_message_iter_append_byte (&iter, 0xF0);
+
+ message_iter_test (message);
+
/* Message loader test */
_dbus_message_lock (message);
loader = _dbus_message_loader_new ();
_dbus_message_loader_return_buffer (loader, buffer, 1);
}
+ copy = dbus_message_copy (message); /* save for tests below */
dbus_message_unref (message);
/* Now pop back the message */
dbus_message_unref (message);
_dbus_message_loader_unref (loader);
+ message = dbus_message_new_method_return (copy);
+ if (message == NULL)
+ _dbus_assert_not_reached ("out of memory\n");
+ dbus_message_unref (copy);
+
+ if (!dbus_message_append_args (message,
+ DBUS_TYPE_STRING, "hello",
+ DBUS_TYPE_INVALID))
+ _dbus_assert_not_reached ("no memory");
+
+ if (!dbus_message_has_signature (message, "s"))
+ _dbus_assert_not_reached ("method return has wrong signature");
+
+ dbus_error_init (&error);
+ if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING,
+ &t, DBUS_TYPE_INVALID))
+
+ {
+ _dbus_warn ("Failed to get expected string arg: %s\n", error.message);
+ exit (1);
+ }
+ dbus_free (t);
+
+ dbus_message_unref (message);
+
/* Now load every message in test_data_dir if we have one */
if (test_data_dir == NULL)
return TRUE;