From 3b3f83ed4044d09f756a348c9ee09c030b8c4858 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Wed, 5 May 2010 13:00:47 +0200 Subject: [PATCH 1/5] Internal _DBUS_TYPE_IS_ARRAYLIKE for array checks Implementation-wise, Maybe is like an array and it saves a lot of code replication to treat it as such. --- dbus/dbus-internals.h | 4 ++++ dbus/dbus-marshal-byteswap.c | 2 +- dbus/dbus-marshal-header.c | 2 +- dbus/dbus-marshal-recursive.c | 22 +++++++++++----------- dbus/dbus-marshal-validate.c | 14 +++++++------- dbus/dbus-message.c | 8 ++++---- dbus/dbus-signature.c | 6 +++--- 7 files changed, 31 insertions(+), 27 deletions(-) diff --git a/dbus/dbus-internals.h b/dbus/dbus-internals.h index dcef7d7..02ad54c 100644 --- a/dbus/dbus-internals.h +++ b/dbus/dbus-internals.h @@ -246,6 +246,10 @@ char** _dbus_dup_string_array (const char **array); #define _DBUS_ISASCII(c) ((c) != '\0' && (((c) & ~0x7f) == 0)) +#define _DBUS_TYPE_IS_ARRAYLIKE(typeliteral) \ + (typeliteral == DBUS_TYPE_ARRAY \ + || typeliteral == DBUS_TYPE_MAYBE) + typedef void (* DBusForeachFunction) (void *element, void *data); diff --git a/dbus/dbus-marshal-byteswap.c b/dbus/dbus-marshal-byteswap.c index 22d7e22..82758d0 100644 --- a/dbus/dbus-marshal-byteswap.c +++ b/dbus/dbus-marshal-byteswap.c @@ -95,7 +95,7 @@ byteswap_body_helper (DBusTypeReader *reader, *((dbus_uint32_t*)p) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)p)); p += 4; - if (current_type == DBUS_TYPE_ARRAY) + if (_DBUS_TYPE_IS_ARRAYLIKE (current_type)) { int elem_type; int alignment; diff --git a/dbus/dbus-marshal-header.c b/dbus/dbus-marshal-header.c index 3f31d7a..b4d67e5 100644 --- a/dbus/dbus-marshal-header.c +++ b/dbus/dbus-marshal-header.c @@ -1099,7 +1099,7 @@ _dbus_header_load (DBusHeader *header, goto invalid; } - _dbus_assert (_dbus_type_reader_get_current_type (&reader) == DBUS_TYPE_ARRAY); + _dbus_assert (_DBUS_TYPE_IS_ARRAYLIKE (_dbus_type_reader_get_current_type (&reader))); _dbus_assert (_dbus_type_reader_get_value_pos (&reader) == FIELDS_ARRAY_LENGTH_OFFSET); _dbus_type_reader_recurse (&reader, &array_reader); diff --git a/dbus/dbus-marshal-recursive.c b/dbus/dbus-marshal-recursive.c index cb10854..493864a 100644 --- a/dbus/dbus-marshal-recursive.c +++ b/dbus/dbus-marshal-recursive.c @@ -340,7 +340,7 @@ _dbus_type_signature_next (const char *type_str, _dbus_assert (*p != DBUS_STRUCT_END_CHAR); _dbus_assert (*p != DBUS_DICT_ENTRY_END_CHAR); - while (*p == DBUS_TYPE_ARRAY) + while (_DBUS_TYPE_IS_ARRAYLIKE (*p)) ++p; _dbus_assert (*p != DBUS_STRUCT_END_CHAR); @@ -809,7 +809,7 @@ _dbus_type_reader_get_element_type (const DBusTypeReader *reader) { int element_type; - _dbus_assert (_dbus_type_reader_get_current_type (reader) == DBUS_TYPE_ARRAY); + _dbus_assert (_DBUS_TYPE_IS_ARRAYLIKE (_dbus_type_reader_get_current_type (reader))); element_type = _dbus_first_type_in_signature (reader->type_str, reader->type_pos + 1); @@ -1632,7 +1632,7 @@ writer_recurse_init_and_check (DBusTypeWriter *writer, sub->container_type = container_type; if (writer->type_pos_is_expectation || - (sub->container_type == DBUS_TYPE_ARRAY || sub->container_type == DBUS_TYPE_VARIANT)) + (_DBUS_TYPE_IS_ARRAYLIKE (sub->container_type) || sub->container_type == DBUS_TYPE_VARIANT)) sub->type_pos_is_expectation = TRUE; else sub->type_pos_is_expectation = FALSE; @@ -1733,7 +1733,7 @@ write_or_verify_typecode (DBusTypeWriter *writer, * so the expected type doesn't change; if inside a struct or something * below an array, we need to move through said struct or something. */ - if (writer->container_type != DBUS_TYPE_ARRAY) + if (!_DBUS_TYPE_IS_ARRAYLIKE (writer->container_type)) writer->type_pos += 1; } else @@ -1805,7 +1805,7 @@ writer_recurse_array (DBusTypeWriter *writer, int aligned; #ifndef DBUS_DISABLE_CHECKS - if (writer->container_type == DBUS_TYPE_ARRAY && + if (_DBUS_TYPE_IS_ARRAYLIKE (writer->container_type) && writer->type_str) { if (!_dbus_string_equal_substring (contained_type, @@ -1865,7 +1865,7 @@ writer_recurse_array (DBusTypeWriter *writer, /* If the parent is an array, we hold type_pos pointing at the array element type; * otherwise advance it to reflect the array value we just recursed into */ - if (writer->container_type != DBUS_TYPE_ARRAY) + if (!_DBUS_TYPE_IS_ARRAYLIKE (writer->container_type)) writer->type_pos += 1 + contained_type_len; else _dbus_assert (writer->type_pos_is_expectation); /* because it's an array */ @@ -2148,7 +2148,7 @@ _dbus_type_writer_append_array (DBusTypeWriter *writer, static int writer_get_array_len (DBusTypeWriter *writer) { - _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY); + _dbus_assert (_DBUS_TYPE_IS_ARRAYLIKE (writer->container_type)); return writer->value_pos - writer->u.array.start_pos; } @@ -2188,7 +2188,7 @@ _dbus_type_writer_unrecurse (DBusTypeWriter *writer, if (!write_or_verify_typecode (sub, DBUS_DICT_ENTRY_END_CHAR)) return FALSE; } - else if (sub->container_type == DBUS_TYPE_ARRAY) + else if (_DBUS_TYPE_IS_ARRAYLIKE (sub->container_type)) { if (sub->u.array.len_pos >= 0) /* len_pos == -1 if we weren't enabled when we passed it */ { @@ -2346,7 +2346,7 @@ _dbus_type_writer_write_fixed_multi (DBusTypeWriter *writer, const void *value, int n_elements) { - _dbus_assert (writer->container_type == DBUS_TYPE_ARRAY); + _dbus_assert (_DBUS_TYPE_IS_ARRAYLIKE (writer->container_type)); _dbus_assert (dbus_type_is_fixed (element_type)); _dbus_assert (writer->type_pos_is_expectation); _dbus_assert (n_elements >= 0); @@ -2468,7 +2468,7 @@ writer_write_reader_helper (DBusTypeWriter *writer, _dbus_type_reader_recurse (reader, &subreader); - if (current_type == DBUS_TYPE_ARRAY) + if (_DBUS_TYPE_IS_ARRAYLIKE (current_type)) { reader_array_len_pos = ARRAY_READER_LEN_POS (&subreader); reader_array_start_pos = subreader.u.array.start_pos; @@ -2537,7 +2537,7 @@ writer_write_reader_helper (DBusTypeWriter *writer, */ if (start_after != NULL && !enabled_at_recurse && past_start_after && - current_type == DBUS_TYPE_ARRAY && + _DBUS_TYPE_IS_ARRAYLIKE (current_type) && fixups != NULL) { DBusArrayLenFixup fixup; diff --git a/dbus/dbus-marshal-validate.c b/dbus/dbus-marshal-validate.c index aa470fc..36ba266 100644 --- a/dbus/dbus-marshal-validate.c +++ b/dbus/dbus-marshal-validate.c @@ -212,8 +212,8 @@ _dbus_validate_signature_with_reason (const DBusString *type_str, goto out; } - if (*p != DBUS_TYPE_ARRAY && - *p != DBUS_DICT_ENTRY_BEGIN_CHAR && + if (!_DBUS_TYPE_IS_ARRAYLIKE (*p) && + *p != DBUS_DICT_ENTRY_BEGIN_CHAR && *p != DBUS_STRUCT_BEGIN_CHAR) { element_count = @@ -231,7 +231,7 @@ _dbus_validate_signature_with_reason (const DBusString *type_str, if (array_depth > 0) { - if (*p == DBUS_TYPE_ARRAY && p != end) + if (_DBUS_TYPE_IS_ARRAYLIKE (*p) && p != end) { const char *p1; p1 = p + 1; @@ -280,7 +280,7 @@ _dbus_validate_signature_with_reason (const DBusString *type_str, goto out; } - _dbus_assert (last != DBUS_TYPE_ARRAY); + _dbus_assert (!_DBUS_TYPE_IS_ARRAYLIKE (last)); _dbus_assert (last != DBUS_STRUCT_BEGIN_CHAR); _dbus_assert (last != DBUS_DICT_ENTRY_BEGIN_CHAR); @@ -375,7 +375,7 @@ validate_body_helper (DBusTypeReader *reader, /* p may now be == end */ _dbus_assert (p <= end); - if (current_type == DBUS_TYPE_ARRAY) + if (_DBUS_TYPE_IS_ARRAYLIKE (current_type)) { int array_elem_type = _dbus_type_reader_get_element_type (reader); @@ -423,7 +423,7 @@ validate_body_helper (DBusTypeReader *reader, p += claimed_len; } - else if (current_type == DBUS_TYPE_ARRAY && claimed_len > 0) + else if (_DBUS_TYPE_IS_ARRAYLIKE (current_type) && claimed_len > 0) { DBusTypeReader sub; DBusValidity validity; @@ -488,7 +488,7 @@ validate_body_helper (DBusTypeReader *reader, } /* check nul termination */ - if (current_type != DBUS_TYPE_ARRAY) + if (!_DBUS_TYPE_IS_ARRAYLIKE (current_type)) { if (p == end) return DBUS_INVALID_NOT_ENOUGH_DATA; diff --git a/dbus/dbus-message.c b/dbus/dbus-message.c index b19697e..540cf0f 100644 --- a/dbus/dbus-message.c +++ b/dbus/dbus-message.c @@ -791,7 +791,7 @@ _dbus_message_iter_get_args_valist (DBusMessageIter *iter, _dbus_type_reader_read_basic (&real->u.reader, ptr); } - else if (spec_type == DBUS_TYPE_ARRAY) + else if (_DBUS_TYPE_IS_ARRAYLIKE (spec_type)) { int element_type; int spec_element_type; @@ -1696,7 +1696,7 @@ dbus_message_append_args_valist (DBusMessage *message, value)) goto failed; } - else if (type == DBUS_TYPE_ARRAY) + else if (_DBUS_TYPE_IS_ARRAYLIKE (type)) { int element_type; DBusMessageIter array; @@ -2013,7 +2013,7 @@ dbus_message_iter_get_element_type (DBusMessageIter *iter) _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID); _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, DBUS_TYPE_INVALID); - _dbus_return_val_if_fail (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID); + _dbus_return_val_if_fail (_DBUS_TYPE_IS_ARRAYLIKE (dbus_message_iter_get_arg_type (iter)), DBUS_TYPE_INVALID); return _dbus_type_reader_get_element_type (&real->u.reader); } @@ -2673,7 +2673,7 @@ dbus_message_iter_open_container (DBusMessageIter *iter, contained_signature == NULL) || (type == DBUS_TYPE_VARIANT && contained_signature != NULL) || - (type == DBUS_TYPE_ARRAY && + (_DBUS_TYPE_IS_ARRAYLIKE (type) && contained_signature != NULL), FALSE); /* this would fail if the contained_signature is a dict entry, since diff --git a/dbus/dbus-signature.c b/dbus/dbus-signature.c index ddc0bcc..7f159b5 100644 --- a/dbus/dbus-signature.c +++ b/dbus/dbus-signature.c @@ -44,7 +44,7 @@ typedef struct ((typecode) == DBUS_TYPE_STRUCT || \ (typecode) == DBUS_TYPE_DICT_ENTRY || \ (typecode) == DBUS_TYPE_VARIANT || \ - (typecode) == DBUS_TYPE_ARRAY) + _DBUS_TYPE_IS_ARRAYLIKE (typecode)) /** @@ -147,7 +147,7 @@ dbus_signature_iter_get_element_type (const DBusSignatureIter *iter) { DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter; - _dbus_return_val_if_fail (dbus_signature_iter_get_current_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID); + _dbus_return_val_if_fail (_DBUS_TYPE_IS_ARRAYLIKE (dbus_signature_iter_get_current_type (iter)), DBUS_TYPE_INVALID); return _dbus_first_type_in_signature_c_str (real_iter->pos, 1); } @@ -216,7 +216,7 @@ dbus_signature_iter_recurse (const DBusSignatureIter *iter, real_sub_iter->in_array = FALSE; real_sub_iter->pos++; - if (dbus_signature_iter_get_current_type (iter) == DBUS_TYPE_ARRAY) + if (_DBUS_TYPE_IS_ARRAYLIKE (dbus_signature_iter_get_current_type (iter))) real_sub_iter->in_array = TRUE; } -- 1.7.3.2