25 #include "dbus-internals.h"
26 #include "dbus-marshal-validate.h"
27 #include "dbus-marshal-recursive.h"
28 #include "dbus-marshal-basic.h"
29 #include "dbus-signature.h"
30 #include "dbus-string.h"
55 const unsigned char *p;
56 const unsigned char *end;
67 element_count_stack =
NULL;
82 result = DBUS_INVALID_SIGNATURE_TOO_LONG;
86 p = _dbus_string_get_const_data_len (type_str, type_pos, 0);
88 end = _dbus_string_get_const_data_len (type_str, type_pos + len, 0);
118 result = DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION;
128 result = DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION;
142 if (struct_depth == 0)
144 result = DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED;
150 result = DBUS_INVALID_STRUCT_HAS_NO_FIELDS;
162 result = DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY;
166 dict_entry_depth += 1;
170 result = DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION;
184 if (dict_entry_depth == 0)
186 result = DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED;
190 dict_entry_depth -= 1;
195 if (element_count != 2)
197 if (element_count == 0)
198 result = DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS;
199 else if (element_count == 1)
200 result = DBUS_INVALID_DICT_ENTRY_HAS_ONLY_ONE_FIELD;
202 result = DBUS_INVALID_DICT_ENTRY_HAS_TOO_MANY_FIELDS;
211 result = DBUS_INVALID_UNKNOWN_TYPECODE;
241 result = DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
255 result = DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE;
267 result = DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
271 if (struct_depth > 0)
273 result = DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED;
277 if (dict_entry_depth > 0)
279 result = DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED;
302 const unsigned char *p,
303 const unsigned char *end,
304 const unsigned char **new_p)
315 return DBUS_INVALID_NESTED_TOO_DEEPLY;
320 const unsigned char *a;
324 _dbus_verbose (
" validating value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
331 return DBUS_INVALID_NOT_ENOUGH_DATA;
333 switch (current_type)
349 a = _DBUS_ALIGN_ADDRESS (p, alignment);
351 return DBUS_INVALID_NOT_ENOUGH_DATA;
355 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
363 if (!(v == 0 || v == 1))
364 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
376 a = _DBUS_ALIGN_ADDRESS (p, 4);
378 return DBUS_INVALID_NOT_ENOUGH_DATA;
382 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
398 return DBUS_INVALID_UNKNOWN_TYPECODE;
403 a = _DBUS_ALIGN_ADDRESS (p, alignment);
407 return DBUS_INVALID_NOT_ENOUGH_DATA;
412 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
417 if (claimed_len > (
unsigned long) (end - p))
418 return DBUS_INVALID_LENGTH_OUT_OF_BOUNDS;
425 _dbus_string_get_length (&str)))
426 return DBUS_INVALID_BAD_PATH;
435 _dbus_string_get_length (&str)))
436 return DBUS_INVALID_BAD_UTF8_IN_STRING;
444 const unsigned char *array_end;
448 return DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM;
456 array_end = p + claimed_len;
473 while (p < array_end)
477 if (!(v == 0 || v == 1))
478 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
492 while (p < array_end)
494 validity = validate_body_helper (&sub, byte_order,
FALSE,
503 return DBUS_INVALID_ARRAY_LENGTH_INCORRECT;
510 return DBUS_INVALID_NOT_ENOUGH_DATA;
513 return DBUS_INVALID_STRING_MISSING_NUL;
529 if (claimed_len + 1 > (
unsigned long) (end - p))
530 return DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
535 _dbus_string_get_length (&str));
544 return DBUS_INVALID_SIGNATURE_MISSING_NUL;
548 _dbus_verbose (
"p = %p end = %p claimed_len %u\n", p, end, claimed_len);
565 int contained_alignment;
573 if (claimed_len + 1 > (
unsigned long) (end - p))
574 return DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
578 _dbus_string_get_length (&sig));
584 return DBUS_INVALID_VARIANT_SIGNATURE_BAD;
590 return DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL;
595 return DBUS_INVALID_VARIANT_SIGNATURE_EMPTY;
599 a = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
601 return DBUS_INVALID_NOT_ENOUGH_DATA;
605 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
613 validity = validate_body_helper (&sub, byte_order,
FALSE,
620 return DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES;
632 a = _DBUS_ALIGN_ADDRESS (p, 8);
634 return DBUS_INVALID_NOT_ENOUGH_DATA;
638 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
644 validity = validate_body_helper (&sub, byte_order,
TRUE,
658 _dbus_verbose (
" validated value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
665 _dbus_verbose (
"not enough data!!! p = %p end = %p end-p = %d\n",
666 p, end, (
int) (end - p));
667 return DBUS_INVALID_NOT_ENOUGH_DATA;
670 if (walk_reader_to_end)
704 int expected_signature_start,
706 int *bytes_remaining,
712 const unsigned char *p;
713 const unsigned char *end;
718 _dbus_assert (value_pos <= _dbus_string_get_length (value_str) - len);
720 _dbus_verbose (
"validating body from pos %d len %d sig '%s'\n",
721 value_pos, len, _dbus_string_get_const_data_len (expected_signature,
722 expected_signature_start,
726 expected_signature, expected_signature_start);
728 p = _dbus_string_get_const_data_len (value_str, value_pos, len);
731 validity = validate_body_helper (&reader, byte_order,
TRUE, 0, p, end, &p);
737 *bytes_remaining = end - p;
753 #define VALID_INITIAL_NAME_CHARACTER(c) \
754 ( ((c) >= 'A' && (c) <= 'Z') || \
755 ((c) >= 'a' && (c) <= 'z') || \
762 #define VALID_NAME_CHARACTER(c) \
763 ( ((c) >= '0' && (c) <= '9') || \
764 ((c) >= 'A' && (c) <= 'Z') || \
765 ((c) >= 'a' && (c) <= 'z') || \
789 const unsigned char *s;
790 const unsigned char *end;
791 const unsigned char *last_slash;
797 if (len > _dbus_string_get_length (str) - start)
803 s = _dbus_string_get_const_data (str) + start;
815 if ((s - last_slash) < 2)
829 if ((end - last_slash) < 2 &&
842 case DBUS_INVALID_FOR_UNKNOWN_REASON:
return "Unknown reason";
843 case DBUS_VALID_BUT_INCOMPLETE:
return "Valid but incomplete";
844 case DBUS_VALIDITY_UNKNOWN:
return "Validity unknown";
846 case DBUS_INVALID_UNKNOWN_TYPECODE:
return "Unknown typecode";
847 case DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE:
return "Missing array element type";
848 case DBUS_INVALID_SIGNATURE_TOO_LONG:
return "Signature is too long";
849 case DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION:
return "Exceeded maximum array recursion";
850 case DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION:
return "Exceeded maximum struct recursion";
851 case DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED:
return "Struct ended but not started";
852 case DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED:
return "Struct started but not ended";
853 case DBUS_INVALID_STRUCT_HAS_NO_FIELDS:
return "Struct has no fields";
854 case DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL:
return "Alignment padding not null";
855 case DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE:
return "Boolean is not zero or one";
856 case DBUS_INVALID_NOT_ENOUGH_DATA:
return "Not enough data";
858 case DBUS_INVALID_BAD_BYTE_ORDER:
return "Bad byte order";
859 case DBUS_INVALID_BAD_PROTOCOL_VERSION:
return "Bad protocol version";
860 case DBUS_INVALID_BAD_MESSAGE_TYPE:
return "Bad message type";
861 case DBUS_INVALID_BAD_SERIAL:
return "Bad serial";
862 case DBUS_INVALID_INSANE_FIELDS_ARRAY_LENGTH:
return "Insane fields array length";
863 case DBUS_INVALID_INSANE_BODY_LENGTH:
return "Insane body length";
864 case DBUS_INVALID_MESSAGE_TOO_LONG:
return "Message too long";
865 case DBUS_INVALID_HEADER_FIELD_CODE:
return "Header field code";
866 case DBUS_INVALID_HEADER_FIELD_HAS_WRONG_TYPE:
return "Header field has wrong type";
867 case DBUS_INVALID_USES_LOCAL_INTERFACE:
return "Uses local interface";
868 case DBUS_INVALID_USES_LOCAL_PATH:
return "Uses local path";
869 case DBUS_INVALID_HEADER_FIELD_APPEARS_TWICE:
return "Header field appears twice";
870 case DBUS_INVALID_BAD_DESTINATION:
return "Bad destination";
871 case DBUS_INVALID_BAD_INTERFACE:
return "Bad interface";
872 case DBUS_INVALID_BAD_MEMBER:
return "Bad member";
873 case DBUS_INVALID_BAD_ERROR_NAME:
return "Bad error name";
874 case DBUS_INVALID_BAD_SENDER:
return "Bad sender";
875 case DBUS_INVALID_MISSING_PATH:
return "Missing path";
876 case DBUS_INVALID_MISSING_INTERFACE:
return "Missing interface";
877 case DBUS_INVALID_MISSING_MEMBER:
return "Missing member";
878 case DBUS_INVALID_MISSING_ERROR_NAME:
return "Missing error name";
879 case DBUS_INVALID_MISSING_REPLY_SERIAL:
return "Missing reply serial";
880 case DBUS_INVALID_LENGTH_OUT_OF_BOUNDS:
return "Length out of bounds";
881 case DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM:
return "Array length exceeds maximum";
882 case DBUS_INVALID_BAD_PATH:
return "Bad path";
883 case DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS:
return "Signature length out of bounds";
884 case DBUS_INVALID_BAD_UTF8_IN_STRING:
return "Bad utf8 in string";
885 case DBUS_INVALID_ARRAY_LENGTH_INCORRECT:
return "Array length incorrect";
886 case DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS:
return "Variant signature length out of bounds";
887 case DBUS_INVALID_VARIANT_SIGNATURE_BAD:
return "Variant signature bad";
888 case DBUS_INVALID_VARIANT_SIGNATURE_EMPTY:
return "Variant signature empty";
889 case DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES:
return "Variant signature specifies multiple values";
890 case DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL:
return "Variant signature missing nul";
891 case DBUS_INVALID_STRING_MISSING_NUL:
return "String missing nul";
892 case DBUS_INVALID_SIGNATURE_MISSING_NUL:
return "Signature missing nul";
893 case DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION:
return "Exceeded maximum dict entry recursion";
894 case DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED:
return "Dict entry ended but not started";
895 case DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED:
return "Dict entry started but not ended";
896 case DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS:
return "Dict entry has no fields";
897 case DBUS_INVALID_DICT_ENTRY_HAS_ONLY_ONE_FIELD:
return "Dict entry has only one field";
898 case DBUS_INVALID_DICT_ENTRY_HAS_TOO_MANY_FIELDS:
return "Dict entry has too many fields";
899 case DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY:
return "Dict entry not inside array";
900 case DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE:
return "Dict key must be basic type";
901 case DBUS_INVALID_NESTED_TOO_DEEPLY:
return "Variants cannot be used to create a hugely recursive tree of values";
925 const unsigned char *s;
926 const unsigned char *end;
927 const unsigned char *iface;
928 const unsigned char *last_dot;
934 if (len > _dbus_string_get_length (str) - start)
944 iface = _dbus_string_get_const_data (str) + start;
951 if (_DBUS_UNLIKELY (*s ==
'.'))
962 if (_DBUS_UNLIKELY ((s + 1) == end))
977 if (_DBUS_UNLIKELY (last_dot ==
NULL))
1001 const unsigned char *s;
1002 const unsigned char *end;
1003 const unsigned char *member;
1009 if (len > _dbus_string_get_length (str) - start)
1018 member = _dbus_string_get_const_data (str) + start;
1070 #define VALID_INITIAL_BUS_NAME_CHARACTER(c) \
1071 ( ((c) >= 'A' && (c) <= 'Z') || \
1072 ((c) >= 'a' && (c) <= 'z') || \
1073 ((c) == '_') || ((c) == '-'))
1079 #define VALID_BUS_NAME_CHARACTER(c) \
1080 ( ((c) >= '0' && (c) <= '9') || \
1081 ((c) >= 'A' && (c) <= 'Z') || \
1082 ((c) >= 'a' && (c) <= 'z') || \
1083 ((c) == '_') || ((c) == '-'))
1086 _dbus_validate_bus_name_full (
const DBusString *str,
1091 const unsigned char *s;
1092 const unsigned char *end;
1093 const unsigned char *iface;
1094 const unsigned char *last_dot;
1100 if (len > _dbus_string_get_length (str) - start)
1110 iface = _dbus_string_get_const_data (str) + start;
1125 if (_DBUS_UNLIKELY ((s + 1) == end))
1141 else if (_DBUS_UNLIKELY (*s ==
'.'))
1152 if (_DBUS_UNLIKELY ((s + 1) == end))
1167 if (!is_namespace && _DBUS_UNLIKELY (last_dot ==
NULL))
1191 return _dbus_validate_bus_name_full (str, start, len,
FALSE);
1212 return _dbus_validate_bus_name_full (str, start, len,
TRUE);
1236 if (len > _dbus_string_get_length (str) - start)
dbus_bool_t dbus_type_is_fixed(int typecode)
Tells you whether values of this type can change length if you set them to some other value...
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
#define DBUS_TYPE_UINT16
Type code marking a 16-bit unsigned integer.
#define NULL
A null pointer, defined appropriately for C or C++.
#define DBUS_MAXIMUM_NAME_LENGTH
Max length in bytes of a bus name, interface, or member (not object path, paths are unlimited)...
trailing junk makes it invalid
void * _dbus_list_pop_last(DBusList **list)
Removes the last value in the list and returns it.
int _dbus_first_type_in_signature(const DBusString *str, int pos)
Get the first type in the signature.
dbus_bool_t _dbus_validate_bus_name(const DBusString *str, int start, int len)
Checks that the given range of the string is a valid bus name in the D-Bus protocol.
void _dbus_type_reader_recurse(DBusTypeReader *reader, DBusTypeReader *sub)
Initialize a new reader pointing to the first type and corresponding value that's a child of the curr...
#define DBUS_TYPE_STRUCT
STRUCT and DICT_ENTRY are sort of special since their codes can't appear in a type string...
#define DBUS_MAXIMUM_TYPE_RECURSION_DEPTH
Depth of recursion in the type tree.
#define DBUS_TYPE_DICT_ENTRY
Type code used to represent a dict entry; however, this type code does not appear in type signatures...
#define DBUS_TYPE_STRING
Type code marking a UTF-8 encoded, nul-terminated Unicode string.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
int type_pos
current position in signature
#define _DBUS_INT_TO_POINTER(integer)
Safely stuffs an integer into a pointer, to be extracted later with _DBUS_POINTER_TO_INT.
#define DBUS_TYPE_BYTE
Type code marking an 8-bit unsigned integer.
dbus_bool_t _dbus_validate_bus_namespace(const DBusString *str, int start, int len)
Checks that the given range of the string is a prefix of a valid bus name in the D-Bus protocol...
dbus_bool_t _dbus_validate_interface(const DBusString *str, int start, int len)
Checks that the given range of the string is a valid interface name in the D-Bus protocol.
void _dbus_type_reader_init_types_only(DBusTypeReader *reader, const DBusString *type_str, int type_pos)
Like _dbus_type_reader_init() but the iteration is over the signature, not over values.
#define DBUS_DICT_ENTRY_BEGIN_CHAR
Code marking the start of a dict entry type in a type signature.
DBusValidity
This is primarily used in unit testing, so we can verify that each invalid message is invalid for the...
#define DBUS_STRUCT_END_CHAR
Code marking the end of a struct type in a type signature.
dbus_bool_t _dbus_validate_error_name(const DBusString *str, int start, int len)
Checks that the given range of the string is a valid error name in the D-Bus protocol.
#define DBUS_TYPE_DOUBLE
Type code marking an 8-byte double in IEEE 754 format.
dbus_bool_t _dbus_validate_signature(const DBusString *str, int start, int len)
Checks that the given range of the string is a valid message type signature in the D-Bus protocol...
#define DBUS_TYPE_ARRAY
Type code marking a D-Bus array type.
#define DBUS_TYPE_INT64
Type code marking a 64-bit signed integer.
dbus_bool_t _dbus_validate_member(const DBusString *str, int start, int len)
Checks that the given range of the string is a valid member name in the D-Bus protocol.
dbus_uint32_t _dbus_unpack_uint32(int byte_order, const unsigned char *data)
Unpacks a 32 bit unsigned integer from a data pointer.
dbus_bool_t _dbus_validate_path(const DBusString *str, int start, int len)
Checks that the given range of the string is a valid object path name in the D-Bus protocol...
dbus_bool_t dbus_type_is_basic(int typecode)
A "basic type" is a somewhat arbitrary concept, but the intent is to include those types that are ful...
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
can't determine validity due to OOM
#define VALID_BUS_NAME_CHARACTER(c)
Determine wether the given character is valid as a second or later character in a bus name...
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
#define DBUS_TYPE_INT32
Type code marking a 32-bit signed integer.
#define DBUS_MAXIMUM_SIGNATURE_LENGTH
This one is 255 so it fits in a byte.
dbus_bool_t _dbus_type_reader_next(DBusTypeReader *reader)
Skip to the next value on this "level".
dbus_bool_t _dbus_string_validate_utf8(const DBusString *str, int start, int len)
Checks that the given range of the string is valid UTF-8.
#define _DBUS_POINTER_TO_INT(pointer)
Safely casts a void* to an integer; should only be used on void* that actually contain integers...
#define DBUS_TYPE_SIGNATURE
Type code marking a D-Bus type signature.
#define DBUS_TYPE_UINT64
Type code marking a 64-bit unsigned integer.
#define DBUS_STRUCT_BEGIN_CHAR
Code marking the start of a struct type in a type signature.
#define DBUS_TYPE_VARIANT
Type code marking a D-Bus variant type.
#define DEFINE_DBUS_NAME_CHECK(what)
Define a name check to be used in _dbus_return_if_fail() statements.
#define DBUS_TYPE_UINT32
Type code marking a 32-bit unsigned integer.
#define VALID_INITIAL_NAME_CHARACTER(c)
Determine wether the given character is valid as the first character in a name.
DBusValidity _dbus_validate_body_with_reason(const DBusString *expected_signature, int expected_signature_start, int byte_order, int *bytes_remaining, const DBusString *value_str, int value_pos, int len)
Verifies that the range of value_str from value_pos to value_end is a legitimate value of type expect...
#define VALID_NAME_CHARACTER(c)
Determine wether the given character is valid as a second or later character in a name...
The type reader is an iterator for reading values from a block of values.
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define DBUS_TYPE_OBJECT_PATH
Type code marking a D-Bus object path.
#define DBUS_TYPE_UNIX_FD
Type code marking a unix file descriptor.
#define DBUS_TYPE_INVALID
Type code that is never equal to a legitimate type code.
#define DBUS_MAXIMUM_ARRAY_LENGTH
Max length of a marshaled array in bytes (64M, 2^26) We use signed int for lengths so must be INT_MAX...
#define DBUS_TYPE_INT16
Type code marking a 16-bit signed integer.
#define DBUS_TYPE_BOOLEAN
Type code marking a boolean.
int _dbus_type_reader_get_element_type(const DBusTypeReader *reader)
Gets the type of an element of the array the reader is currently pointing to.
#define _DBUS_INT32_MAX
Maximum value of type "int32".
int _dbus_type_reader_get_current_type(const DBusTypeReader *reader)
Gets the type of the value the reader is currently pointing to; or for a types-only reader gets the t...
#define FALSE
Expands to "0".
#define DBUS_DICT_ENTRY_END_CHAR
Code marking the end of a dict entry type in a type signature.
const char * _dbus_type_to_string(int typecode)
Returns a string describing the given type.
DBusValidity _dbus_validate_signature_with_reason(const DBusString *type_str, int type_pos, int len)
Verifies that the range of type_str from type_pos to type_end is a valid signature.
int _dbus_type_get_alignment(int typecode)
Gets the alignment requirement for the given type; will be 1, 4, or 8.
void _dbus_string_init_const_len(DBusString *str, const char *value, int len)
Initializes a constant string with a length.
#define VALID_INITIAL_BUS_NAME_CHARACTER(c)
Determine wether the given character is valid as the first character in a bus name.
dbus_bool_t dbus_type_is_valid(int typecode)
Return TRUE if the argument is a valid typecode.
void _dbus_list_clear(DBusList **list)
Frees all links in the list and sets the list head to NULL.