summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2005-02-24 16:03:56 +0000
committerColin Walters <walters@verbum.org>2005-02-24 16:03:56 +0000
commit54a2e9f7961b4b8afff94bb0c5b756f986965be6 (patch)
tree475800623e70c6bf14e7b4b2af2be8cc14d217ba
parentd3f1b8bc89631cd9ef1cf2a806ce80f056b940fb (diff)
2005-02-24 Colin Walters <walters@verbum.org>
* dbus/dbus-signature.c: New file; implements various functions related to type signatures. Includes an interator for parsing, validation functions. (dbus_type_is_basic): Moved here from dbus-marshal-basic.c:_dbus_type_is_basic. (dbus_type_is_container): Moved here from dbus-marshal-basic.c:_dbus_type_is_container. All callers of _dbus_type_is_container and _dbus_type_is_basic updated, and include dbus-signature.h. * dbus/dbus-signature.h: New file; prototypes for the above. * dbus/Makefile.am (DBUS_LIB_SOURCES): Add dbus-signature.c, dbus-signature.h. * dbus/dbus-marshal-basic.c (map_type_char_to_type): New utility function factored out of _dbus_first_type_in_signature. (_dbus_first_type_in_signature_c_str): New function; returns first type code for a type signature character. * dbus/dbus-marshal-basic.h: Prototype _dbus_first_type_in_signature_c_str, handle function moves. * dbus/dbus-marshal-recursive.h: Export _dbus_type_signature_next. * dbus/dbus-marshal-recursive.c (_dbus_type_signature_next): New function; skips to next complete type in type signature. Implemented using previous skip_one_complete_type. Now skip_one_complete_type just delegates to _dbus_type_signature_next. * dbus/dbus-marshal-basic.c (_dbus_type_is_basic): Moved to dbus-signature.c (_dbus_type_is_container): Ditto. * doc/dbus-specification.xml: Update introspection sample to use real type signatures. * dbus/dbus-test.h: Prototype signature test. * dbus/dbus-test.c (dbus_internal_do_not_use_run_tests): Run signature tests. * dbus/dbus-protocol.h (DBUS_ERROR_INVALID_SIGNATURE): New error.
-rw-r--r--ChangeLog48
-rw-r--r--dbus/Makefile.am2
-rw-r--r--dbus/dbus-marshal-basic.c100
-rw-r--r--dbus/dbus-marshal-basic.h6
-rw-r--r--dbus/dbus-marshal-recursive-util.c3
-rw-r--r--dbus/dbus-marshal-recursive.c26
-rw-r--r--dbus/dbus-marshal-recursive.h3
-rw-r--r--dbus/dbus-marshal-validate.c3
-rw-r--r--dbus/dbus-message.c9
-rw-r--r--dbus/dbus-protocol.h1
-rw-r--r--dbus/dbus-signature.c430
-rw-r--r--dbus/dbus-signature.h71
-rw-r--r--dbus/dbus-test.c2
-rw-r--r--dbus/dbus-test.h1
-rw-r--r--doc/dbus-specification.xml24
15 files changed, 638 insertions, 91 deletions
diff --git a/ChangeLog b/ChangeLog
index 5b7b4bcc..5359bf51 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,51 @@
+2005-02-24 Colin Walters <walters@verbum.org>
+
+ * dbus/dbus-signature.c: New file; implements various functions
+ related to type signatures. Includes an interator for parsing,
+ validation functions.
+ (dbus_type_is_basic): Moved here from
+ dbus-marshal-basic.c:_dbus_type_is_basic.
+ (dbus_type_is_container): Moved here from
+ dbus-marshal-basic.c:_dbus_type_is_container.
+
+ All callers of _dbus_type_is_container and _dbus_type_is_basic
+ updated, and include dbus-signature.h.
+
+ * dbus/dbus-signature.h: New file; prototypes for the above.
+
+ * dbus/Makefile.am (DBUS_LIB_SOURCES): Add dbus-signature.c,
+ dbus-signature.h.
+
+ * dbus/dbus-marshal-basic.c (map_type_char_to_type): New utility
+ function factored out of _dbus_first_type_in_signature.
+ (_dbus_first_type_in_signature_c_str): New function; returns first
+ type code for a type signature character.
+
+ * dbus/dbus-marshal-basic.h: Prototype _dbus_first_type_in_signature_c_str,
+ handle function moves.
+
+ * dbus/dbus-marshal-recursive.h: Export _dbus_type_signature_next.
+
+ * dbus/dbus-marshal-recursive.c (_dbus_type_signature_next): New
+ function; skips to next complete type in type signature.
+ Implemented using previous skip_one_complete_type. Now
+ skip_one_complete_type just delegates to
+ _dbus_type_signature_next.
+
+ * dbus/dbus-marshal-basic.c (_dbus_type_is_basic): Moved
+ to dbus-signature.c
+ (_dbus_type_is_container): Ditto.
+
+ * doc/dbus-specification.xml: Update introspection sample to
+ use real type signatures.
+
+ * dbus/dbus-test.h: Prototype signature test.
+
+ * dbus/dbus-test.c (dbus_internal_do_not_use_run_tests): Run
+ signature tests.
+
+ * dbus/dbus-protocol.h (DBUS_ERROR_INVALID_SIGNATURE): New error.
+
2005-02-23 John (J5) Palmieri <johnp@redhat.com>
* python/dbus_bindings.pyx.in (PendingCall::get_reply):
diff --git a/dbus/Makefile.am b/dbus/Makefile.am
index 2d08be6c..24d99107 100644
--- a/dbus/Makefile.am
+++ b/dbus/Makefile.am
@@ -78,6 +78,8 @@ DBUS_LIB_SOURCES= \
dbus-server-unix.h \
dbus-sha.c \
dbus-sha.h \
+ dbus-signature.c \
+ dbus-signature.h \
dbus-timeout.c \
dbus-timeout.h \
dbus-threads.c \
diff --git a/dbus/dbus-marshal-basic.c b/dbus/dbus-marshal-basic.c
index a443ea35..fde58d51 100644
--- a/dbus/dbus-marshal-basic.c
+++ b/dbus/dbus-marshal-basic.c
@@ -24,6 +24,7 @@
#include "dbus-internals.h"
#include "dbus-marshal-basic.h"
+#include "dbus-signature.h"
#include <string.h>
@@ -509,7 +510,7 @@ _dbus_marshal_read_basic (const DBusString *str,
const char *str_data;
DBusBasicValue *vp;
- _dbus_assert (_dbus_type_is_basic (type));
+ _dbus_assert (dbus_type_is_basic (type));
str_data = _dbus_string_get_const_data (str);
vp = value;
@@ -620,7 +621,7 @@ _dbus_marshal_read_fixed_multi (const DBusString *str,
int alignment;
_dbus_assert (_dbus_type_is_fixed (element_type));
- _dbus_assert (_dbus_type_is_basic (element_type));
+ _dbus_assert (dbus_type_is_basic (element_type));
#if 0
_dbus_verbose ("reading %d elements of %s\n",
@@ -849,7 +850,7 @@ _dbus_marshal_write_basic (DBusString *str,
{
const DBusBasicValue *vp;
- _dbus_assert (_dbus_type_is_basic (type));
+ _dbus_assert (dbus_type_is_basic (type));
vp = value;
@@ -1297,56 +1298,6 @@ _dbus_type_is_valid (int typecode)
}
}
-/** macro that checks whether a typecode is a container type */
-#define TYPE_IS_CONTAINER(typecode) \
- ((typecode) == DBUS_TYPE_STRUCT || \
- (typecode) == DBUS_TYPE_DICT_ENTRY || \
- (typecode) == DBUS_TYPE_VARIANT || \
- (typecode) == DBUS_TYPE_ARRAY)
-
-/**
- * A "container type" can contain basic types, or nested
- * container types. #DBUS_TYPE_INVALID is not a container type.
- * This function will crash if passed a typecode that isn't
- * in dbus-protocol.h
- *
- * @returns #TRUE if type is a container
- */
-dbus_bool_t
-_dbus_type_is_container (int typecode)
-{
- /* only reasonable (non-line-noise) typecodes are allowed */
- _dbus_assert (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID);
- return TYPE_IS_CONTAINER (typecode);
-}
-
-/**
- * A "basic type" is a somewhat arbitrary concept, but the intent
- * is to include those types that are fully-specified by a single
- * typecode, with no additional type information or nested
- * values. So all numbers and strings are basic types and
- * structs, arrays, and variants are not basic types.
- * #DBUS_TYPE_INVALID is not a basic type.
- *
- * This function is defined to return #TRUE for exactly those
- * types that can be written with _dbus_marshal_basic_type()
- * and read with _dbus_marshal_read_basic().
- *
- * This function will crash if passed a typecode that isn't
- * in dbus-protocol.h
- *
- * @returns #TRUE if type is basic
- */
-dbus_bool_t
-_dbus_type_is_basic (int typecode)
-{
- /* only reasonable (non-line-noise) typecodes are allowed */
- _dbus_assert (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID);
-
- /* everything that isn't invalid or a container */
- return !(typecode == DBUS_TYPE_INVALID || TYPE_IS_CONTAINER (typecode));
-}
-
/**
* Tells you whether values of this type can change length if you set
* them to some other value. For this purpose, you assume that the
@@ -1552,6 +1503,21 @@ _dbus_verbose_bytes_of_string (const DBusString *str,
_dbus_verbose_bytes (d, len, start);
}
+static int
+map_type_char_to_type (int t)
+{
+ if (t == DBUS_STRUCT_BEGIN_CHAR)
+ return DBUS_TYPE_STRUCT;
+ else if (t == DBUS_DICT_ENTRY_BEGIN_CHAR)
+ return DBUS_TYPE_DICT_ENTRY;
+ else
+ {
+ _dbus_assert (t != DBUS_STRUCT_END_CHAR);
+ _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
+ return t;
+ }
+}
+
/**
* Get the first type in the signature. The difference between this
* and just getting the first byte of the signature is that you won't
@@ -1566,20 +1532,22 @@ int
_dbus_first_type_in_signature (const DBusString *str,
int pos)
{
- unsigned char t;
-
- t = _dbus_string_get_byte (str, pos);
+ return map_type_char_to_type (_dbus_string_get_byte (str, pos));
+}
- if (t == DBUS_STRUCT_BEGIN_CHAR)
- return DBUS_TYPE_STRUCT;
- else if (t == DBUS_DICT_ENTRY_BEGIN_CHAR)
- return DBUS_TYPE_DICT_ENTRY;
- else
- {
- _dbus_assert (t != DBUS_STRUCT_END_CHAR);
- _dbus_assert (t != DBUS_DICT_ENTRY_END_CHAR);
- return t;
- }
+/**
+ * Similar to #_dbus_first_type_in_signature, but operates
+ * on a C string buffer.
+ *
+ * @param str a C string buffer
+ * @param pos where the signature starts
+ * @returns the first type in the signature
+ */
+int
+_dbus_first_type_in_signature_c_str (const char *str,
+ int pos)
+{
+ return map_type_char_to_type (str[pos]);
}
/** @} */
diff --git a/dbus/dbus-marshal-basic.h b/dbus/dbus-marshal-basic.h
index f25d38c2..b474ace2 100644
--- a/dbus/dbus-marshal-basic.h
+++ b/dbus/dbus-marshal-basic.h
@@ -244,14 +244,16 @@ dbus_uint32_t _dbus_marshal_read_uint32 (const DBusString *str,
int *new_pos);
dbus_bool_t _dbus_type_is_valid (int typecode);
int _dbus_type_get_alignment (int typecode);
-dbus_bool_t _dbus_type_is_basic (int typecode);
-dbus_bool_t _dbus_type_is_container (int typecode);
dbus_bool_t _dbus_type_is_fixed (int typecode);
+int _dbus_type_get_alignment (int typecode);
const char* _dbus_type_to_string (int typecode);
int _dbus_first_type_in_signature (const DBusString *str,
int pos);
+int _dbus_first_type_in_signature_c_str (const char *str,
+ int pos);
+
void _dbus_swap_array (unsigned char *data,
int n_elements,
int alignment);
diff --git a/dbus/dbus-marshal-recursive-util.c b/dbus/dbus-marshal-recursive-util.c
index 6e8af18a..e1b00e58 100644
--- a/dbus/dbus-marshal-recursive-util.c
+++ b/dbus/dbus-marshal-recursive-util.c
@@ -27,6 +27,7 @@
#include "dbus-marshal-recursive.h"
#include "dbus-marshal-basic.h"
+#include "dbus-signature.h"
#include "dbus-internals.h"
#include <string.h>
@@ -80,7 +81,7 @@ equal_values_helper (DBusTypeReader *lhs,
if (lhs_type == DBUS_TYPE_INVALID)
return TRUE;
- if (_dbus_type_is_basic (lhs_type))
+ if (dbus_type_is_basic (lhs_type))
{
DBusBasicValue lhs_value;
DBusBasicValue rhs_value;
diff --git a/dbus/dbus-marshal-recursive.c b/dbus/dbus-marshal-recursive.c
index 738edd91..432e6f0b 100644
--- a/dbus/dbus-marshal-recursive.c
+++ b/dbus/dbus-marshal-recursive.c
@@ -23,6 +23,7 @@
#include "dbus-marshal-recursive.h"
#include "dbus-marshal-basic.h"
+#include "dbus-signature.h"
#include "dbus-internals.h"
/**
@@ -308,18 +309,33 @@ array_reader_check_finished (const DBusTypeReader *reader)
return reader->value_pos == end_pos;
}
-/* this is written a little oddly to try and overoptimize */
static void
skip_one_complete_type (const DBusString *type_str,
int *type_pos)
{
+ _dbus_type_signature_next (_dbus_string_get_const_data (type_str),
+ type_pos);
+}
+
+/**
+ * Skips to the next "complete" type inside a type signature.
+ * The signature is read starting at type_pos, and the next
+ * type position is stored in the same variable.
+ *
+ * @param type_str a type signature (must be valid)
+ * @param type_pos an integer position in the type signtaure (in and out)
+ */
+void
+_dbus_type_signature_next (const char *type_str,
+ int *type_pos)
+{
const unsigned char *p;
const unsigned char *start;
_dbus_assert (type_str != NULL);
_dbus_assert (type_pos != NULL);
- start = _dbus_string_get_const_data (type_str);
+ start = type_str;
p = start + *type_pos;
_dbus_assert (*p != DBUS_STRUCT_END_CHAR);
@@ -1446,7 +1462,7 @@ _dbus_type_reader_set_basic (DBusTypeReader *reader,
realign_root->value_pos);
#endif
- _dbus_assert (_dbus_type_is_basic (current_type));
+ _dbus_assert (dbus_type_is_basic (current_type));
if (_dbus_type_is_fixed (current_type))
{
@@ -2500,7 +2516,7 @@ writer_write_reader_helper (DBusTypeWriter *writer,
while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
{
- if (_dbus_type_is_container (current_type))
+ if (dbus_type_is_container (current_type))
{
DBusTypeReader subreader;
DBusTypeWriter subwriter;
@@ -2646,7 +2662,7 @@ writer_write_reader_helper (DBusTypeWriter *writer,
{
DBusBasicValue val;
- _dbus_assert (_dbus_type_is_basic (current_type));
+ _dbus_assert (dbus_type_is_basic (current_type));
#if RECURSIVE_MARSHAL_WRITE_TRACE
_dbus_verbose ("Reading basic value %s at %d\n",
diff --git a/dbus/dbus-marshal-recursive.h b/dbus/dbus-marshal-recursive.h
index 45deeb5c..779e449a 100644
--- a/dbus/dbus-marshal-recursive.h
+++ b/dbus/dbus-marshal-recursive.h
@@ -170,6 +170,9 @@ dbus_bool_t _dbus_type_reader_greater_than (const DBusTypeReader *
dbus_bool_t _dbus_type_reader_equal_values (const DBusTypeReader *lhs,
const DBusTypeReader *rhs);
+void _dbus_type_signature_next (const char *signature,
+ int *type_pos);
+
void _dbus_type_writer_init (DBusTypeWriter *writer,
int byte_order,
DBusString *type_str,
diff --git a/dbus/dbus-marshal-validate.c b/dbus/dbus-marshal-validate.c
index 8315bcce..dbe2fa54 100644
--- a/dbus/dbus-marshal-validate.c
+++ b/dbus/dbus-marshal-validate.c
@@ -25,6 +25,7 @@
#include "dbus-marshal-validate.h"
#include "dbus-marshal-recursive.h"
#include "dbus-marshal-basic.h"
+#include "dbus-signature.h"
#include "dbus-string.h"
/**
@@ -156,7 +157,7 @@ _dbus_validate_signature_with_reason (const DBusString *type_str,
}
if (last == DBUS_DICT_ENTRY_BEGIN_CHAR &&
- !_dbus_type_is_basic (*p))
+ !dbus_type_is_basic (*p))
return DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE;
last = *p;
diff --git a/dbus/dbus-message.c b/dbus/dbus-message.c
index dfe3b02b..7ff5762c 100644
--- a/dbus/dbus-message.c
+++ b/dbus/dbus-message.c
@@ -27,6 +27,7 @@
#include "dbus-marshal-validate.h"
#include "dbus-marshal-byteswap.h"
#include "dbus-marshal-header.h"
+#include "dbus-signature.h"
#include "dbus-message-private.h"
#include "dbus-object-tree.h"
#include "dbus-memory.h"
@@ -1242,7 +1243,7 @@ dbus_message_append_args_valist (DBusMessage *message,
while (type != DBUS_TYPE_INVALID)
{
- if (_dbus_type_is_basic (type))
+ if (dbus_type_is_basic (type))
{
const DBusBasicValue *value;
value = va_arg (var_args, const DBusBasicValue*);
@@ -1742,7 +1743,7 @@ _dbus_message_iter_get_args_valist (DBusMessageIter *iter,
goto out;
}
- if (_dbus_type_is_basic (spec_type))
+ if (dbus_type_is_basic (spec_type))
{
DBusBasicValue *ptr;
@@ -2076,7 +2077,7 @@ dbus_message_iter_append_basic (DBusMessageIter *iter,
_dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
_dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
- _dbus_return_val_if_fail (_dbus_type_is_basic (type), FALSE);
+ _dbus_return_val_if_fail (dbus_type_is_basic (type), FALSE);
_dbus_return_val_if_fail (value != NULL, FALSE);
if (!_dbus_message_iter_open_signature (real))
@@ -2182,7 +2183,7 @@ dbus_message_iter_open_container (DBusMessageIter *iter,
_dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
_dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
- _dbus_return_val_if_fail (_dbus_type_is_container (type), FALSE);
+ _dbus_return_val_if_fail (dbus_type_is_container (type), FALSE);
_dbus_return_val_if_fail (sub != NULL, FALSE);
_dbus_return_val_if_fail ((type == DBUS_TYPE_STRUCT &&
contained_signature == NULL) ||
diff --git a/dbus/dbus-protocol.h b/dbus/dbus-protocol.h
index d48c3e84..df5bfedc 100644
--- a/dbus/dbus-protocol.h
+++ b/dbus/dbus-protocol.h
@@ -271,6 +271,7 @@ extern "C" {
#define DBUS_ERROR_SPAWN_CHILD_SIGNALED "org.freedesktop.DBus.Error.Spawn.ChildSignaled"
#define DBUS_ERROR_SPAWN_FAILED "org.freedesktop.DBus.Error.Spawn.Failed"
#define DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN "org.freedesktop.DBus.Error.UnixProcessIdUnknown"
+#define DBUS_ERROR_INVALID_SIGNATURE "org.freedesktop.DBus.Error.InvalidSignature"
#define DBUS_INTROSPECT_1_0_XML_NAMESPACE "http://www.freedesktop.org/standards/dbus"
#define DBUS_INTROSPECT_1_0_XML_PUBLIC_IDENTIFIER "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
diff --git a/dbus/dbus-signature.c b/dbus/dbus-signature.c
new file mode 100644
index 00000000..c984bcfd
--- /dev/null
+++ b/dbus/dbus-signature.c
@@ -0,0 +1,430 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* dbus-signature.c Routines for reading recursive type signatures
+ *
+ * Copyright (C) 2005 Red Hat, Inc.
+ *
+ * Licensed under the Academic Free License version 2.1
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include "dbus-signature.h"
+#include "dbus-marshal-recursive.h"
+#include "dbus-marshal-basic.h"
+#include "dbus-internals.h"
+#include "dbus-test.h"
+
+typedef struct
+{
+ const char *pos;
+ unsigned int finished : 1;
+ unsigned int in_array : 1;
+} DBusSignatureRealIter;
+
+/**
+ * @addtogroup DBusSignature
+ * @{
+ */
+
+/**
+ * Initializes a #DBusSignatureIter for reading a type signature. This
+ * function is not safe to use on invalid signatures; be sure to
+ * validate potentially invalid signatures with dbus_signature_validate
+ * before using this function.
+ *
+ * @param iter pointer to an iterator to initialize
+ * @param signature the type signature
+ */
+void
+dbus_signature_iter_init (DBusSignatureIter *iter,
+ const char *signature)
+{
+ DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
+
+ real_iter->pos = signature;
+ real_iter->finished = FALSE;
+ real_iter->in_array = FALSE;
+}
+
+/**
+ * Returns the current type pointed to by the iterator.
+ * If the iterator is pointing at a type code such as 's', then
+ * it will be returned directly.
+ *
+ * However, when the parser encounters a container type start
+ * character such as '(' for a structure, the corresponding type for
+ * the container will be returned, e.g. DBUS_TYPE_STRUCT, not '('.
+ * In this case, you should initialize a sub-iterator with
+ * dbus_signature_iter_recurse to parse the container type.
+ *
+ * @param iter pointer to an iterator
+ * @returns current type (e.g. DBUS_TYPE_STRING, DBUS_TYPE_ARRAY)
+ */
+int
+dbus_signature_iter_get_current_type (const DBusSignatureIter *iter)
+{
+ DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
+
+ return _dbus_first_type_in_signature_c_str (real_iter->pos, 0);
+}
+
+/**
+ * Convenience function for returning the element type of an array;
+ * This function allows you to avoid initializing a sub-iterator and
+ * getting its current type.
+ *
+ * It is an error to invoke this function if the current type of the
+ * iterator is not DBUS_TYPE_ARRAY.
+ *
+ * @param iter pointer to an iterator
+ * @returns current array element type
+ */
+int
+dbus_signature_iter_get_element_type (const DBusSignatureIter *iter)
+{
+ DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
+
+ _dbus_retur(dbus_signature_iter_get_current_type (iter) == DBUS_TYPE_ARRAY);
+
+ return _dbus_first_type_in_signature_c_str (real_iter->pos, 1);
+}
+
+/**
+ * Skip to the next value on this "level". e.g. the next field in a
+ * struct, the next value in an array. Returns FALSE at the end of the
+ * current container.
+ *
+ * @param iter the iterator
+ * @returns FALSE if nothing more to read at or below this level
+ */
+dbus_bool_t
+dbus_signature_iter_next (DBusSignatureIter *iter)
+{
+ DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
+
+ if (real_iter->finished)
+ return FALSE;
+ else
+ {
+ int pos;
+
+ if (real_iter->in_array)
+ {
+ real_iter->finished = TRUE;
+ return FALSE;
+ }
+
+ pos = 0;
+ _dbus_type_signature_next (real_iter->pos, &pos);
+ real_iter->pos += pos;
+
+ if (*real_iter->pos == DBUS_STRUCT_END_CHAR
+ || *real_iter->pos == DBUS_DICT_ENTRY_END_CHAR)
+ {
+ real_iter->finished = TRUE;
+ return FALSE;
+ }
+
+ return *real_iter->pos != DBUS_TYPE_INVALID;
+ }
+}
+
+/**
+ * Initialize a new iterator pointing to the first type current
+ * container. It's an error to call this if the current type is a
+ * non-container (i.e. if dbus_type_is_container returns FALSE).
+ *
+ * @param iter the current interator
+ * @param subiter an iterator to initialize pointing to the first child
+ */
+void
+dbus_signature_iter_recurse (const DBusSignatureIter *iter,
+ DBusSignatureIter *subiter)
+{
+ DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
+ DBusSignatureRealIter *real_sub_iter = (DBusSignatureRealIter *) subiter;
+
+ _dbus_return_if_fail (dbus_type_is_container (dbus_signature_iter_get_current_type (iter)));
+
+ *real_sub_iter = *real_iter;
+ real_sub_iter->pos++;
+
+ if (dbus_signature_iter_get_current_type (subiter) == DBUS_TYPE_ARRAY)
+ real_sub_iter->in_array = TRUE;
+}
+
+/**
+ * Check a type signature for validity.
+ *
+ * @param signature a potentially invalid type signature
+ * @error error return
+ * @returns TRUE iif signature is valid
+ */
+dbus_bool_t
+dbus_signature_validate (const char *signature,
+ DBusError *error)
+
+{
+ DBusString str;
+
+ _dbus_string_init_const (&str, signature);
+ if (_dbus_validate_signature (&str, 0, _dbus_string_get_length (&str)))
+ return TRUE;
+ dbus_set_error (error, DBUS_ERROR_INVALID_SIGNATURE, "Corrupt type signature");
+ return FALSE;
+}
+
+/**
+ * Check that a type signature is both valid and contains exactly
+ * one complete type.
+ *
+ * @param signature a potentially invalid type signature
+ * @error error return
+ * @returns TRUE iif signature is valid and has exactly one complete type
+ */
+dbus_bool_t
+dbus_signature_validate_single (const char *signature,
+ DBusError *error)
+{
+ DBusSignatureIter iter;
+
+ if (!dbus_signature_validate (signature, error))
+ return FALSE;
+
+ dbus_signature_iter_init (&iter, signature);
+ if (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_INVALID)
+ goto lose;
+ if (!dbus_signature_iter_next (&iter))
+ return TRUE;
+ lose:
+ dbus_set_error (error, DBUS_ERROR_INVALID_SIGNATURE, "Exactly one complete type required in signature");
+ return FALSE;
+}
+
+/** macro that checks whether a typecode is a container type */
+#define TYPE_IS_CONTAINER(typecode) \
+ ((typecode) == DBUS_TYPE_STRUCT || \
+ (typecode) == DBUS_TYPE_DICT_ENTRY || \
+ (typecode) == DBUS_TYPE_VARIANT || \
+ (typecode) == DBUS_TYPE_ARRAY)
+
+/**
+ * A "container type" can contain basic types, or nested
+ * container types. #DBUS_TYPE_INVALID is not a container type.
+ * This function will crash if passed a typecode that isn't
+ * in dbus-protocol.h
+ *
+ * @returns #TRUE if type is a container
+ */
+dbus_bool_t
+dbus_type_is_container (int typecode)
+{
+ /* only reasonable (non-line-noise) typecodes are allowed */
+ _dbus_return_val_if_fail (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
+ FALSE);
+ return TYPE_IS_CONTAINER (typecode);
+}
+
+/**
+ * A "basic type" is a somewhat arbitrary concept, but the intent
+ * is to include those types that are fully-specified by a single
+ * typecode, with no additional type information or nested
+ * values. So all numbers and strings are basic types and
+ * structs, arrays, and variants are not basic types.
+ * #DBUS_TYPE_INVALID is not a basic type.
+ *
+ * This function will crash if passed a typecode that isn't
+ * in dbus-protocol.h
+ *
+ * @returns #TRUE if type is basic
+ */
+dbus_bool_t
+dbus_type_is_basic (int typecode)
+{
+ /* only reasonable (non-line-noise) typecodes are allowed */
+ _dbus_return_val_if_fail (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
+ FALSE);
+
+ /* everything that isn't invalid or a container */
+ return !(typecode == DBUS_TYPE_INVALID || TYPE_IS_CONTAINER (typecode));
+}
+
+
+#ifdef DBUS_BUILD_TESTS
+
+/**
+ * @ingroup DBusSignatureInternals
+ * Unit test for DBusSignature.
+ *
+ * @returns #TRUE on success.
+ */
+dbus_bool_t
+_dbus_signature_test (void)
+{
+ DBusSignatureIter iter;
+ DBusSignatureIter subiter;
+ DBusSignatureIter subsubiter;
+ DBusSignatureIter subsubsubiter;
+ const char *sig;
+
+ _dbus_assert (sizeof (DBusSignatureIter) >= sizeof (DBusSignatureRealIter));
+
+ sig = "";
+ _dbus_assert (dbus_signature_validate (sig, NULL));
+ _dbus_assert (!dbus_signature_validate_single (sig, NULL));
+ dbus_signature_iter_init (&iter, sig);
+ _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_INVALID);
+
+ sig = DBUS_TYPE_STRING_AS_STRING;
+ _dbus_assert (dbus_signature_validate (sig, NULL));
+ _dbus_assert (dbus_signature_validate_single (sig, NULL));
+ dbus_signature_iter_init (&iter, sig);
+ _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_STRING);
+
+ sig = DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_BYTE_AS_STRING;
+ _dbus_assert (dbus_signature_validate (sig, NULL));
+ dbus_signature_iter_init (&iter, sig);
+ _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_STRING);
+ _dbus_assert (dbus_signature_iter_next (&iter));
+ _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_BYTE);
+
+ sig = DBUS_TYPE_UINT16_AS_STRING
+ DBUS_STRUCT_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_STRING_AS_STRING
+ DBUS_TYPE_UINT32_AS_STRING
+ DBUS_TYPE_VARIANT_AS_STRING
+ DBUS_TYPE_DOUBLE_AS_STRING
+ DBUS_STRUCT_END_CHAR_AS_STRING;
+ _dbus_assert (dbus_signature_validate (sig, NULL));
+ dbus_signature_iter_init (&iter, sig);
+ _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_UINT16);
+ _dbus_assert (dbus_signature_iter_next (&iter));
+ _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_STRUCT);
+ dbus_signature_iter_recurse (&iter, &subiter);
+ _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_STRING);
+ _dbus_assert (dbus_signature_iter_next (&subiter));
+ _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_UINT32);
+ _dbus_assert (dbus_signature_iter_next (&subiter));
+ _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_VARIANT);
+ _dbus_assert (dbus_signature_iter_next (&subiter));
+ _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_DOUBLE);
+
+ sig = DBUS_TYPE_UINT16_AS_STRING
+ DBUS_STRUCT_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_UINT32_AS_STRING
+ DBUS_TYPE_BYTE_AS_STRING
+ DBUS_TYPE_ARRAY_AS_STRING
+ DBUS_TYPE_ARRAY_AS_STRING
+ DBUS_TYPE_DOUBLE_AS_STRING
+ DBUS_STRUCT_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_BYTE_AS_STRING
+ DBUS_STRUCT_END_CHAR_AS_STRING
+ DBUS_STRUCT_END_CHAR_AS_STRING;
+ _dbus_assert (dbus_signature_validate (sig, NULL));
+ dbus_signature_iter_init (&iter, sig);
+ _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_UINT16);
+ _dbus_assert (dbus_signature_iter_next (&iter));
+ _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_STRUCT);
+ dbus_signature_iter_recurse (&iter, &subiter);
+ _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_UINT32);
+ _dbus_assert (dbus_signature_iter_next (&subiter));
+ _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_BYTE);
+ _dbus_assert (dbus_signature_iter_next (&subiter));
+ _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_ARRAY);
+ _dbus_assert (dbus_signature_iter_get_element_type (&subiter) == DBUS_TYPE_ARRAY);
+
+ dbus_signature_iter_recurse (&subiter, &subsubiter);
+ _dbus_assert (dbus_signature_iter_get_current_type (&subsubiter) == DBUS_TYPE_ARRAY);
+ _dbus_assert (dbus_signature_iter_get_element_type (&subsubiter) == DBUS_TYPE_DOUBLE);
+
+ dbus_signature_iter_recurse (&subsubiter, &subsubsubiter);
+ _dbus_assert (dbus_signature_iter_get_current_type (&subsubsubiter) == DBUS_TYPE_DOUBLE);
+ _dbus_assert (dbus_signature_iter_next (&subiter));
+ _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_STRUCT);
+ dbus_signature_iter_recurse (&subiter, &subsubiter);
+ _dbus_assert (dbus_signature_iter_get_current_type (&subsubiter) == DBUS_TYPE_BYTE);
+
+ sig = DBUS_TYPE_ARRAY_AS_STRING
+ DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_INT16_AS_STRING
+ DBUS_TYPE_STRING_AS_STRING
+ DBUS_DICT_ENTRY_END_CHAR_AS_STRING
+ DBUS_TYPE_VARIANT_AS_STRING;
+ _dbus_assert (dbus_signature_validate (sig, NULL));
+ _dbus_assert (!dbus_signature_validate_single (sig, NULL));
+ dbus_signature_iter_init (&iter, sig);
+ _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_ARRAY);
+ _dbus_assert (dbus_signature_iter_get_element_type (&iter) == DBUS_TYPE_DICT_ENTRY);
+
+ dbus_signature_iter_recurse (&iter, &subiter);
+ dbus_signature_iter_recurse (&subiter, &subsubiter);
+ _dbus_assert (dbus_signature_iter_get_current_type (&subsubiter) == DBUS_TYPE_INT16);
+ _dbus_assert (dbus_signature_iter_next (&subsubiter));
+ _dbus_assert (dbus_signature_iter_get_current_type (&subsubiter) == DBUS_TYPE_STRING);
+ _dbus_assert (!dbus_signature_iter_next (&subsubiter));
+
+ _dbus_assert (dbus_signature_iter_next (&iter));
+ _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_VARIANT);
+ _dbus_assert (!dbus_signature_iter_next (&iter));
+
+ sig = DBUS_TYPE_DICT_ENTRY_AS_STRING;
+ _dbus_assert (!dbus_signature_validate (sig, NULL));
+
+ sig = DBUS_TYPE_ARRAY_AS_STRING;
+ _dbus_assert (!dbus_signature_validate (sig, NULL));
+
+ sig = DBUS_TYPE_UINT32_AS_STRING
+ DBUS_TYPE_ARRAY_AS_STRING;
+ _dbus_assert (!dbus_signature_validate (sig, NULL));
+
+ sig = DBUS_TYPE_ARRAY_AS_STRING
+ DBUS_TYPE_DICT_ENTRY_AS_STRING;
+ _dbus_assert (!dbus_signature_validate (sig, NULL));
+
+ sig = DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING;
+ _dbus_assert (!dbus_signature_validate (sig, NULL));
+
+ sig = DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
+ _dbus_assert (!dbus_signature_validate (sig, NULL));
+
+ sig = DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_INT32_AS_STRING;
+ _dbus_assert (!dbus_signature_validate (sig, NULL));
+
+ sig = DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_INT32_AS_STRING
+ DBUS_TYPE_STRING_AS_STRING;
+ _dbus_assert (!dbus_signature_validate (sig, NULL));
+
+ sig = DBUS_STRUCT_END_CHAR_AS_STRING
+ DBUS_STRUCT_BEGIN_CHAR_AS_STRING;
+ _dbus_assert (!dbus_signature_validate (sig, NULL));
+
+ sig = DBUS_STRUCT_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_BOOLEAN_AS_STRING;
+ _dbus_assert (!dbus_signature_validate (sig, NULL));
+ return TRUE;
+#if 0
+ oom:
+ _dbus_assert_not_reached ("out of memory");
+ return FALSE;
+#endif
+}
+
+#endif
+
+/** @} */ /* end of DBusSignature group */
+
diff --git a/dbus/dbus-signature.h b/dbus/dbus-signature.h
new file mode 100644
index 00000000..3c50795e
--- /dev/null
+++ b/dbus/dbus-signature.h
@@ -0,0 +1,71 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* dbus-signatures.h utility functions for D-BUS types
+ *
+ * Copyright (C) 2005 Red Hat Inc.
+ *
+ * Licensed under the Academic Free License version 2.1
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+#if !defined (DBUS_INSIDE_DBUS_H) && !defined (DBUS_COMPILATION)
+#error "Only <dbus/dbus.h> can be included directly, this file may disappear or change contents."
+#endif
+
+#ifndef DBUS_SIGNATURES_H
+#define DBUS_SIGNATURES_H
+
+#include <dbus/dbus-macros.h>
+#include <dbus/dbus-types.h>
+#include <dbus/dbus-errors.h>
+
+DBUS_BEGIN_DECLS
+
+/**
+ * DBusSignatureIter struct; contains no public fields
+ */
+typedef struct
+{
+ void *dummy1; /**< Don't use this */
+ void *dummy2; /**< Don't use this */
+ dbus_uint32_t dummy8; /**< Don't use this */
+ int dummy12; /**< Don't use this */
+ int dummy17; /**< Don't use this */
+} DBusSignatureIter;
+
+void dbus_signature_iter_init (DBusSignatureIter *iter,
+ const char *signature);
+
+int dbus_signature_iter_get_current_type (const DBusSignatureIter *iter);
+
+int dbus_signature_iter_get_element_type (const DBusSignatureIter *iter);
+
+dbus_bool_t dbus_signature_iter_next (DBusSignatureIter *iter);
+
+void dbus_signature_iter_recurse (const DBusSignatureIter *iter,
+ DBusSignatureIter *subiter);
+
+dbus_bool_t dbus_signature_validate (const char *signature,
+ DBusError *error);
+
+dbus_bool_t dbus_signature_validate_single (const char *signature,
+ DBusError *error);
+
+dbus_bool_t dbus_type_is_basic (int typecode);
+dbus_bool_t dbus_type_is_container (int typecode);
+
+DBUS_END_DECLS
+
+#endif /* DBUS_MESSAGE_H */
diff --git a/dbus/dbus-test.c b/dbus/dbus-test.c
index 2f909881..658d2d8a 100644
--- a/dbus/dbus-test.c
+++ b/dbus/dbus-test.c
@@ -121,6 +121,8 @@ dbus_internal_do_not_use_run_tests (const char *test_data_dir, const char *speci
run_test ("server", specific_test, _dbus_server_test);
run_test ("object-tree", specific_test, _dbus_object_tree_test);
+
+ run_test ("signature", specific_test, _dbus_signature_test);
run_test ("marshalling", specific_test, _dbus_marshal_test);
diff --git a/dbus/dbus-test.h b/dbus/dbus-test.h
index 983ae577..9e37fe74 100644
--- a/dbus/dbus-test.h
+++ b/dbus/dbus-test.h
@@ -36,6 +36,7 @@ dbus_bool_t _dbus_marshal_recursive_test (void);
dbus_bool_t _dbus_marshal_byteswap_test (void);
dbus_bool_t _dbus_marshal_header_test (void);
dbus_bool_t _dbus_marshal_validate_test (void);
+dbus_bool_t _dbus_signature_test (void);
dbus_bool_t _dbus_mem_pool_test (void);
dbus_bool_t _dbus_string_test (void);
dbus_bool_t _dbus_address_test (void);
diff --git a/doc/dbus-specification.xml b/doc/dbus-specification.xml
index 74df3b33..1d47733c 100644
--- a/doc/dbus-specification.xml
+++ b/doc/dbus-specification.xml
@@ -2294,14 +2294,22 @@
&lt;node name="/org/freedesktop/sample_object"&gt;
&lt;interface name="org.freedesktop.SampleInterface"&gt;
&lt;method name="Frobate"&gt;
- &lt;arg name="foo" type="int32" direction="in"/&gt;
- &lt;arg name="bar" type="string" direction="out"/&gt;
+ &lt;arg name="foo" type="i" direction="in"/&gt;
+ &lt;arg name="bar" type="s" direction="out"/&gt;
+ &lt;arg name="baz" type="a{us}" direction="out"/&gt;
&lt;annotation name="org.freedesktop.DBus.Deprecated" value="true"/&gt;
&lt;/method&gt;
+ &lt;method name="Bazify"&gt;
+ &lt;arg name="bar" type="(iiu)" direction="in"/&gt;
+ &lt;arg name="bar" type="v" direction="out"/&gt;
+ &lt;/method&gt;
+ &lt;method name="Mogrify"&gt;
+ &lt;arg name="bar" type="(iiav)" direction="in"/&gt;
+ &lt;/method&gt;
&lt;signal name="Changed"&gt;
- &lt;arg name="new_value" type="boolean"/&gt;
+ &lt;arg name="new_value" type="b"/&gt;
&lt;/signal&gt;
- &lt;property name="Bar" type="byte" access="readwrite"/&gt;
+ &lt;property name="Bar" type="y" access="readwrite"/&gt;
&lt;/interface&gt;
&lt;node name="child_of_sample_object"/&gt;
&lt;node name="another_child_of_sample_object"/&gt;
@@ -2354,14 +2362,6 @@
</listitem>
<listitem>
<para>
- The current type="uint32" stuff is totally broken,
- instead we have to do full signatures.
- However, then this format will suck for human readability.
- So, some thinking to do here.
- </para>
- </listitem>
- <listitem>
- <para>
Multiple interfaces can of course be listed for
one &lt;node&gt;.
</para>