X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dbus%2Fdbus-signature.c;h=c130de5b93c994cfe432a6aac3b87dc9a8b991d3;hb=5cf4959a8fa2958dbc2215ccdadbb2e270719e34;hp=7118bd2596f894e0f9c376bcac1843456d0411db;hpb=61411a061c09def43687153e6c734ff27b7fd556;p=platform%2Fupstream%2Fdbus.git diff --git a/dbus/dbus-signature.c b/dbus/dbus-signature.c index 7118bd2..c130de5 100644 --- a/dbus/dbus-signature.c +++ b/dbus/dbus-signature.c @@ -1,4 +1,4 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ /* dbus-signature.c Routines for reading recursive type signatures * * Copyright (C) 2005 Red Hat, Inc. @@ -17,25 +17,40 @@ * * 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 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ +#include + #include "dbus-signature.h" #include "dbus-marshal-recursive.h" #include "dbus-marshal-basic.h" #include "dbus-internals.h" #include "dbus-test.h" +/** + * Implementation details of #DBusSignatureIter, all fields are private + */ typedef struct { - const char *pos; - unsigned int finished : 1; - unsigned int in_array : 1; + const char *pos; /**< current position in the signature string */ + unsigned int finished : 1; /**< true if we are at the end iter */ + unsigned int in_array : 1; /**< true if we are a subiterator pointing to an array's element type */ } DBusSignatureRealIter; +/** 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) + + /** - * @addtogroup DBusSignature + * @defgroup DBusSignature Type signature parsing + * @ingroup DBus + * @brief Parsing D-Bus type signatures * @{ */ @@ -68,10 +83,10 @@ dbus_signature_iter_init (DBusSignatureIter *iter, * 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. + * 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) + * @returns current type (e.g. #DBUS_TYPE_STRING, #DBUS_TYPE_ARRAY) */ int dbus_signature_iter_get_current_type (const DBusSignatureIter *iter) @@ -82,11 +97,16 @@ dbus_signature_iter_get_current_type (const DBusSignatureIter *iter) } /** - * Returns the full type signature represented by the current - * iterator as a C string. + * Returns the signature of the single complete type starting at the + * given iterator. + * + * For example, if the iterator is pointing at the start of "(ii)ii" + * (which is "a struct of two ints, followed by an int, followed by an + * int"), then "(ii)" would be returned. If the iterator is pointing at + * one of the "i" then just that "i" would be returned. * * @param iter pointer to an iterator - * @returns current signature; or NULL on OOM. Should be freed with #dbus_free + * @returns current signature; or #NULL if no memory. Should be freed with dbus_free() */ char * dbus_signature_iter_get_signature (const DBusSignatureIter *iter) @@ -116,8 +136,8 @@ dbus_signature_iter_get_signature (const DBusSignatureIter *iter) * 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. + * Undefined behavior results if you invoke this function when the + * current type of the iterator is not #DBUS_TYPE_ARRAY. * * @param iter pointer to an iterator * @returns current array element type @@ -134,7 +154,7 @@ dbus_signature_iter_get_element_type (const DBusSignatureIter *iter) /** * 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 + * struct, the next value in an array. Returns #FALSE at the end of the * current container. * * @param iter the iterator @@ -173,9 +193,12 @@ dbus_signature_iter_next (DBusSignatureIter *iter) } /** - * 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). + * Initialize a new iterator pointing to the first type in the current + * container. + * + * The results are undefined when calling this if the current type is + * a non-container (i.e. if dbus_type_is_container() returns #FALSE + * for the result of dbus_signature_iter_get_current_type()). * * @param iter the current interator * @param subiter an iterator to initialize pointing to the first child @@ -190,18 +213,21 @@ dbus_signature_iter_recurse (const DBusSignatureIter *iter, _dbus_return_if_fail (dbus_type_is_container (dbus_signature_iter_get_current_type (iter))); *real_sub_iter = *real_iter; + real_sub_iter->in_array = FALSE; real_sub_iter->pos++; - if (dbus_signature_iter_get_current_type (subiter) == DBUS_TYPE_ARRAY) + if (dbus_signature_iter_get_current_type (iter) == DBUS_TYPE_ARRAY) real_sub_iter->in_array = TRUE; } /** - * Check a type signature for validity. + * Check a type signature for validity. Remember that #NULL can always + * be passed instead of a DBusError*, if you don't care about having + * an error name and message. * * @param signature a potentially invalid type signature - * @error error return - * @returns TRUE iif signature is valid + * @param error error return + * @returns #TRUE if signature is valid or #FALSE if an error is set */ dbus_bool_t dbus_signature_validate (const char *signature, @@ -209,21 +235,30 @@ dbus_signature_validate (const char *signature, { DBusString str; + DBusValidity reason; _dbus_string_init_const (&str, signature); - if (_dbus_validate_signature (&str, 0, _dbus_string_get_length (&str))) + reason = _dbus_validate_signature_with_reason (&str, 0, _dbus_string_get_length (&str)); + + if (reason == DBUS_VALID) return TRUE; - dbus_set_error (error, DBUS_ERROR_INVALID_SIGNATURE, "Corrupt type signature"); - return FALSE; + else + { + dbus_set_error (error, DBUS_ERROR_INVALID_SIGNATURE, _dbus_validity_to_error_message (reason)); + return FALSE; + } } /** - * Check that a type signature is both valid and contains exactly - * one complete type. + * Check that a type signature is both valid and contains exactly one + * complete type. "One complete type" means a single basic type, + * array, struct, or dictionary, though the struct or array may be + * arbitrarily recursive and complex. More than one complete type + * would mean for example "ii" or two integers in sequence. * * @param signature a potentially invalid type signature - * @error error return - * @returns TRUE iif signature is valid and has exactly one complete type + * @param error error return + * @returns #TRUE if signature is valid and has exactly one complete type */ dbus_bool_t dbus_signature_validate_single (const char *signature, @@ -244,48 +279,46 @@ dbus_signature_validate_single (const char *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 * + * It is an error to pass an invalid type-code, other than DBUS_TYPE_INVALID, + * to this function. The valid type-codes are defined by dbus-protocol.h + * and can be checked with dbus_type_is_valid(). + * + * @param typecode either a valid type-code or DBUS_TYPE_INVALID * @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, + _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. + * 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 + * It is an error to pass an invalid type-code, other than DBUS_TYPE_INVALID, + * to this function. The valid type-codes are defined by dbus-protocol.h + * and can be checked with dbus_type_is_valid(). * + * @param typecode either a valid type-code or DBUS_TYPE_INVALID * @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, + _dbus_return_val_if_fail (dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID, FALSE); /* everything that isn't invalid or a container */ @@ -298,14 +331,57 @@ dbus_type_is_basic (int typecode) * first byte of the old and new value would be in the same location, * so alignment padding is not a factor. * - * This function is useful to determine whether #dbus_message_iter_get_fixed_array - * may be used. + * This function is useful to determine whether + * dbus_message_iter_get_fixed_array() may be used. * + * Some structs are fixed-size (if they contain only fixed-size types) + * but struct is not considered a fixed type for purposes of this + * function. + * + * It is an error to pass an invalid type-code, other than DBUS_TYPE_INVALID, + * to this function. The valid type-codes are defined by dbus-protocol.h + * and can be checked with dbus_type_is_valid(). + * + * @param typecode either a valid type-code or DBUS_TYPE_INVALID * @returns #FALSE if the type can occupy different lengths */ dbus_bool_t dbus_type_is_fixed (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); + + switch (typecode) + { + case DBUS_TYPE_BYTE: + case DBUS_TYPE_BOOLEAN: + case DBUS_TYPE_INT16: + case DBUS_TYPE_UINT16: + case DBUS_TYPE_INT32: + case DBUS_TYPE_UINT32: + case DBUS_TYPE_INT64: + case DBUS_TYPE_UINT64: + case DBUS_TYPE_DOUBLE: + case DBUS_TYPE_UNIX_FD: + return TRUE; + default: + return FALSE; + } +} + +/** + * Return #TRUE if the argument is a valid typecode. + * #DBUS_TYPE_INVALID surprisingly enough is not considered valid, and + * random unknown bytes aren't either. This function is safe with + * untrusted data. + * + * @param typecode a potential type-code + * @returns #TRUE if valid + */ +dbus_bool_t +dbus_type_is_valid (int typecode) +{ switch (typecode) { case DBUS_TYPE_BYTE: @@ -317,12 +393,23 @@ dbus_type_is_fixed (int typecode) case DBUS_TYPE_INT64: case DBUS_TYPE_UINT64: case DBUS_TYPE_DOUBLE: + case DBUS_TYPE_STRING: + case DBUS_TYPE_OBJECT_PATH: + case DBUS_TYPE_SIGNATURE: + case DBUS_TYPE_ARRAY: + case DBUS_TYPE_STRUCT: + case DBUS_TYPE_DICT_ENTRY: + case DBUS_TYPE_VARIANT: + case DBUS_TYPE_UNIX_FD: return TRUE; + default: return FALSE; } } +/** @} */ /* end of DBusSignature group */ + #ifdef DBUS_BUILD_TESTS /** @@ -339,6 +426,7 @@ _dbus_signature_test (void) DBusSignatureIter subsubiter; DBusSignatureIter subsubsubiter; const char *sig; + dbus_bool_t boolres; _dbus_assert (sizeof (DBusSignatureIter) >= sizeof (DBusSignatureRealIter)); @@ -358,7 +446,8 @@ _dbus_signature_test (void) _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)); + boolres = dbus_signature_iter_next (&iter); + _dbus_assert (boolres); _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_BYTE); sig = DBUS_TYPE_UINT16_AS_STRING @@ -371,15 +460,19 @@ _dbus_signature_test (void) _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)); + boolres = dbus_signature_iter_next (&iter); + _dbus_assert (boolres); _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)); + boolres = dbus_signature_iter_next (&subiter); + _dbus_assert (boolres); _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_UINT32); - _dbus_assert (dbus_signature_iter_next (&subiter)); + boolres = dbus_signature_iter_next (&subiter); + _dbus_assert (boolres); _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_VARIANT); - _dbus_assert (dbus_signature_iter_next (&subiter)); + boolres = dbus_signature_iter_next (&subiter); + _dbus_assert (boolres); _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_DOUBLE); sig = DBUS_TYPE_UINT16_AS_STRING @@ -396,13 +489,16 @@ _dbus_signature_test (void) _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)); + boolres = dbus_signature_iter_next (&iter); + _dbus_assert (boolres); _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)); + boolres = dbus_signature_iter_next (&subiter); + _dbus_assert (boolres); _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_BYTE); - _dbus_assert (dbus_signature_iter_next (&subiter)); + boolres = dbus_signature_iter_next (&subiter); + _dbus_assert (boolres); _dbus_assert (dbus_signature_iter_get_current_type (&subiter) == DBUS_TYPE_ARRAY); _dbus_assert (dbus_signature_iter_get_element_type (&subiter) == DBUS_TYPE_ARRAY); @@ -412,7 +508,8 @@ _dbus_signature_test (void) 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)); + boolres = dbus_signature_iter_next (&subiter); + _dbus_assert (boolres); _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); @@ -432,13 +529,17 @@ _dbus_signature_test (void) 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)); + boolres = dbus_signature_iter_next (&subsubiter); + _dbus_assert (boolres); _dbus_assert (dbus_signature_iter_get_current_type (&subsubiter) == DBUS_TYPE_STRING); - _dbus_assert (!dbus_signature_iter_next (&subsubiter)); + boolres = dbus_signature_iter_next (&subsubiter); + _dbus_assert (!boolres); - _dbus_assert (dbus_signature_iter_next (&iter)); + boolres = dbus_signature_iter_next (&iter); + _dbus_assert (boolres); _dbus_assert (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_VARIANT); - _dbus_assert (!dbus_signature_iter_next (&iter)); + boolres = dbus_signature_iter_next (&iter); + _dbus_assert (!boolres); sig = DBUS_TYPE_DICT_ENTRY_AS_STRING; _dbus_assert (!dbus_signature_validate (sig, NULL)); @@ -486,5 +587,3 @@ _dbus_signature_test (void) #endif -/** @} */ /* end of DBusSignature group */ -