1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-marshal-validate.c Validation routines for marshaled data
4 * Copyright (C) 2005 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-marshal-validate.h"
26 #include "dbus-marshal-recursive.h"
27 #include "dbus-marshal-basic.h"
28 #include "dbus-signature.h"
29 #include "dbus-string.h"
32 * @addtogroup DBusMarshal
38 * Verifies that the range of type_str from type_pos to type_end is a
39 * valid signature. If this function returns #TRUE, it will be safe
40 * to iterate over the signature with a types-only #DBusTypeReader.
41 * The range passed in should NOT include the terminating
42 * nul/DBUS_TYPE_INVALID.
44 * @param type_str the string
45 * @param type_pos where the typecodes start
46 * @param len length of typecodes
47 * @returns #DBUS_VALID if valid, reason why invalid otherwise
50 _dbus_validate_signature_with_reason (const DBusString *type_str,
54 const unsigned char *p;
55 const unsigned char *end;
63 DBusList *element_count_stack;
66 element_count_stack = NULL;
68 if (!_dbus_list_append (&element_count_stack, _DBUS_INT_TO_POINTER (0)))
70 result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
74 _dbus_assert (type_str != NULL);
75 _dbus_assert (type_pos < _DBUS_INT32_MAX - len);
76 _dbus_assert (len >= 0);
77 _dbus_assert (type_pos >= 0);
79 if (len > DBUS_MAXIMUM_SIGNATURE_LENGTH)
81 result = DBUS_INVALID_SIGNATURE_TOO_LONG;
85 p = _dbus_string_get_const_data_len (type_str, type_pos, 0);
87 end = _dbus_string_get_const_data_len (type_str, type_pos + len, 0);
91 last = DBUS_TYPE_INVALID;
98 case DBUS_TYPE_BOOLEAN:
100 case DBUS_TYPE_UINT16:
101 case DBUS_TYPE_INT32:
102 case DBUS_TYPE_UINT32:
103 case DBUS_TYPE_INT64:
104 case DBUS_TYPE_UINT64:
105 case DBUS_TYPE_DOUBLE:
106 case DBUS_TYPE_STRING:
107 case DBUS_TYPE_OBJECT_PATH:
108 case DBUS_TYPE_SIGNATURE:
109 case DBUS_TYPE_VARIANT:
112 case DBUS_TYPE_ARRAY:
114 if (array_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
116 result = DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION;
121 case DBUS_STRUCT_BEGIN_CHAR:
124 if (struct_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
126 result = DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION;
130 if (!_dbus_list_append (&element_count_stack,
131 _DBUS_INT_TO_POINTER (0)))
133 result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
139 case DBUS_STRUCT_END_CHAR:
140 if (struct_depth == 0)
142 result = DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED;
146 if (last == DBUS_STRUCT_BEGIN_CHAR)
148 result = DBUS_INVALID_STRUCT_HAS_NO_FIELDS;
152 _dbus_list_pop_last (&element_count_stack);
157 case DBUS_DICT_ENTRY_BEGIN_CHAR:
158 if (last != DBUS_TYPE_ARRAY)
160 result = DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY;
164 dict_entry_depth += 1;
166 if (dict_entry_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
168 result = DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION;
172 if (!_dbus_list_append (&element_count_stack,
173 _DBUS_INT_TO_POINTER (0)))
175 result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
181 case DBUS_DICT_ENTRY_END_CHAR:
182 if (dict_entry_depth == 0)
184 result = DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED;
188 dict_entry_depth -= 1;
191 _DBUS_POINTER_TO_INT (_dbus_list_pop_last (&element_count_stack));
193 if (element_count != 2)
195 if (element_count == 0)
196 result = DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS;
197 else if (element_count == 1)
198 result = DBUS_INVALID_DICT_ENTRY_HAS_ONLY_ONE_FIELD;
200 result = DBUS_INVALID_DICT_ENTRY_HAS_TOO_MANY_FIELDS;
206 case DBUS_TYPE_STRUCT: /* doesn't appear in signatures */
207 case DBUS_TYPE_DICT_ENTRY: /* ditto */
209 result = DBUS_INVALID_UNKNOWN_TYPECODE;
213 if (*p != DBUS_TYPE_ARRAY &&
214 *p != DBUS_DICT_ENTRY_BEGIN_CHAR &&
215 *p != DBUS_STRUCT_BEGIN_CHAR)
218 _DBUS_POINTER_TO_INT (_dbus_list_pop_last (&element_count_stack));
222 if (!_dbus_list_append (&element_count_stack,
223 _DBUS_INT_TO_POINTER (element_count)))
225 result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
232 if (*p == DBUS_TYPE_ARRAY && p != end)
236 if (*p1 == DBUS_STRUCT_END_CHAR ||
237 *p1 == DBUS_DICT_ENTRY_END_CHAR)
239 result = DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
249 if (last == DBUS_DICT_ENTRY_BEGIN_CHAR &&
250 _dbus_type_is_valid (*p) &&
251 !dbus_type_is_basic (*p))
253 result = DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE;
264 result = DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
268 if (struct_depth > 0)
270 result = DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED;
274 if (dict_entry_depth > 0)
276 result = DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED;
280 _dbus_assert (last != DBUS_TYPE_ARRAY);
281 _dbus_assert (last != DBUS_STRUCT_BEGIN_CHAR);
282 _dbus_assert (last != DBUS_DICT_ENTRY_BEGIN_CHAR);
287 _dbus_list_clear (&element_count_stack);
292 validate_body_helper (DBusTypeReader *reader,
294 dbus_bool_t walk_reader_to_end,
295 const unsigned char *p,
296 const unsigned char *end,
297 const unsigned char **new_p)
301 while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
303 const unsigned char *a;
307 _dbus_verbose (" validating value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
308 _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
312 /* Guarantee that p has one byte to look at */
314 return DBUS_INVALID_NOT_ENOUGH_DATA;
316 switch (current_type)
322 case DBUS_TYPE_BOOLEAN:
323 case DBUS_TYPE_INT16:
324 case DBUS_TYPE_UINT16:
325 case DBUS_TYPE_INT32:
326 case DBUS_TYPE_UINT32:
327 case DBUS_TYPE_INT64:
328 case DBUS_TYPE_UINT64:
329 case DBUS_TYPE_DOUBLE:
330 alignment = _dbus_type_get_alignment (current_type);
331 a = _DBUS_ALIGN_ADDRESS (p, alignment);
333 return DBUS_INVALID_NOT_ENOUGH_DATA;
337 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
341 if (current_type == DBUS_TYPE_BOOLEAN)
343 dbus_uint32_t v = _dbus_unpack_uint32 (byte_order,
345 if (!(v == 0 || v == 1))
346 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
352 case DBUS_TYPE_ARRAY:
353 case DBUS_TYPE_STRING:
354 case DBUS_TYPE_OBJECT_PATH:
356 dbus_uint32_t claimed_len;
358 a = _DBUS_ALIGN_ADDRESS (p, 4);
360 return DBUS_INVALID_NOT_ENOUGH_DATA;
364 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
368 claimed_len = _dbus_unpack_uint32 (byte_order, p);
371 /* p may now be == end */
372 _dbus_assert (p <= end);
374 if (current_type == DBUS_TYPE_ARRAY)
376 int array_elem_type = _dbus_type_reader_get_element_type (reader);
378 if (!_dbus_type_is_valid (array_elem_type))
380 return DBUS_INVALID_UNKNOWN_TYPECODE;
383 alignment = _dbus_type_get_alignment (array_elem_type);
385 a = _DBUS_ALIGN_ADDRESS (p, alignment);
387 /* a may now be == end */
389 return DBUS_INVALID_NOT_ENOUGH_DATA;
394 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
399 if (claimed_len > (unsigned long) (end - p))
400 return DBUS_INVALID_LENGTH_OUT_OF_BOUNDS;
402 if (current_type == DBUS_TYPE_OBJECT_PATH)
405 _dbus_string_init_const_len (&str, p, claimed_len);
406 if (!_dbus_validate_path (&str, 0,
407 _dbus_string_get_length (&str)))
408 return DBUS_INVALID_BAD_PATH;
412 else if (current_type == DBUS_TYPE_STRING)
415 _dbus_string_init_const_len (&str, p, claimed_len);
416 if (!_dbus_string_validate_utf8 (&str, 0,
417 _dbus_string_get_length (&str)))
418 return DBUS_INVALID_BAD_UTF8_IN_STRING;
422 else if (current_type == DBUS_TYPE_ARRAY && claimed_len > 0)
425 DBusValidity validity;
426 const unsigned char *array_end;
429 if (claimed_len > DBUS_MAXIMUM_ARRAY_LENGTH)
430 return DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM;
432 /* Remember that the reader is types only, so we can't
433 * use it to iterate over elements. It stays the same
436 _dbus_type_reader_recurse (reader, &sub);
438 array_end = p + claimed_len;
440 array_elem_type = _dbus_type_reader_get_element_type (reader);
442 /* avoid recursive call to validate_body_helper if this is an array
443 * of fixed-size elements
445 if (dbus_type_is_fixed (array_elem_type))
447 /* bools need to be handled differently, because they can
448 * have an invalid value
450 if (array_elem_type == DBUS_TYPE_BOOLEAN)
453 alignment = _dbus_type_get_alignment (array_elem_type);
455 while (p < array_end)
457 v = _dbus_unpack_uint32 (byte_order, p);
459 if (!(v == 0 || v == 1))
460 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
474 while (p < array_end)
476 validity = validate_body_helper (&sub, byte_order, FALSE, p, end, &p);
477 if (validity != DBUS_VALID)
483 return DBUS_INVALID_ARRAY_LENGTH_INCORRECT;
486 /* check nul termination */
487 if (current_type != DBUS_TYPE_ARRAY)
490 return DBUS_INVALID_NOT_ENOUGH_DATA;
493 return DBUS_INVALID_STRING_MISSING_NUL;
499 case DBUS_TYPE_SIGNATURE:
501 dbus_uint32_t claimed_len;
503 DBusValidity validity;
508 /* 1 is for nul termination */
509 if (claimed_len + 1 > (unsigned long) (end - p))
510 return DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
512 _dbus_string_init_const_len (&str, p, claimed_len);
514 _dbus_validate_signature_with_reason (&str, 0,
515 _dbus_string_get_length (&str));
517 if (validity != DBUS_VALID)
522 _dbus_assert (p < end);
523 if (*p != DBUS_TYPE_INVALID)
524 return DBUS_INVALID_SIGNATURE_MISSING_NUL;
528 _dbus_verbose ("p = %p end = %p claimed_len %u\n", p, end, claimed_len);
532 case DBUS_TYPE_VARIANT:
534 /* 1 byte sig len, sig typecodes, align to
535 * contained-type-boundary, values.
538 /* In addition to normal signature validation, we need to be sure
539 * the signature contains only a single (possibly container) type.
541 dbus_uint32_t claimed_len;
544 DBusValidity validity;
545 int contained_alignment;
553 if (claimed_len + 1 > (unsigned long) (end - p))
554 return DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
556 _dbus_string_init_const_len (&sig, p, claimed_len);
557 reason = _dbus_validate_signature_with_reason (&sig, 0,
558 _dbus_string_get_length (&sig));
559 if (!(reason == DBUS_VALID))
561 if (reason == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
564 return DBUS_INVALID_VARIANT_SIGNATURE_BAD;
569 if (*p != DBUS_TYPE_INVALID)
570 return DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL;
573 contained_type = _dbus_first_type_in_signature (&sig, 0);
574 if (contained_type == DBUS_TYPE_INVALID)
575 return DBUS_INVALID_VARIANT_SIGNATURE_EMPTY;
577 contained_alignment = _dbus_type_get_alignment (contained_type);
579 a = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
581 return DBUS_INVALID_NOT_ENOUGH_DATA;
585 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
589 _dbus_type_reader_init_types_only (&sub, &sig, 0);
591 _dbus_assert (_dbus_type_reader_get_current_type (&sub) != DBUS_TYPE_INVALID);
593 validity = validate_body_helper (&sub, byte_order, FALSE, p, end, &p);
594 if (validity != DBUS_VALID)
597 if (_dbus_type_reader_next (&sub))
598 return DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES;
600 _dbus_assert (_dbus_type_reader_get_current_type (&sub) == DBUS_TYPE_INVALID);
604 case DBUS_TYPE_DICT_ENTRY:
605 case DBUS_TYPE_STRUCT:
608 DBusValidity validity;
610 a = _DBUS_ALIGN_ADDRESS (p, 8);
612 return DBUS_INVALID_NOT_ENOUGH_DATA;
616 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
620 _dbus_type_reader_recurse (reader, &sub);
622 validity = validate_body_helper (&sub, byte_order, TRUE, p, end, &p);
623 if (validity != DBUS_VALID)
629 _dbus_assert_not_reached ("invalid typecode in supposedly-validated signature");
634 _dbus_verbose (" validated value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
635 _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
641 _dbus_verbose ("not enough data!!! p = %p end = %p end-p = %d\n",
642 p, end, (int) (end - p));
643 return DBUS_INVALID_NOT_ENOUGH_DATA;
646 if (walk_reader_to_end)
647 _dbus_type_reader_next (reader);
659 * Verifies that the range of value_str from value_pos to value_end is
660 * a legitimate value of type expected_signature. If this function
661 * returns #TRUE, it will be safe to iterate over the values with
662 * #DBusTypeReader. The signature is assumed to be already valid.
664 * If bytes_remaining is not #NULL, then leftover bytes will be stored
665 * there and #DBUS_VALID returned. If it is #NULL, then
666 * #DBUS_INVALID_TOO_MUCH_DATA will be returned if bytes are left
669 * @param expected_signature the expected types in the value_str
670 * @param expected_signature_start where in expected_signature is the signature
671 * @param byte_order the byte order
672 * @param bytes_remaining place to store leftover bytes
673 * @param value_str the string containing the body
674 * @param value_pos where the values start
675 * @param len length of values after value_pos
676 * @returns #DBUS_VALID if valid, reason why invalid otherwise
679 _dbus_validate_body_with_reason (const DBusString *expected_signature,
680 int expected_signature_start,
682 int *bytes_remaining,
683 const DBusString *value_str,
687 DBusTypeReader reader;
688 const unsigned char *p;
689 const unsigned char *end;
690 DBusValidity validity;
692 _dbus_assert (len >= 0);
693 _dbus_assert (value_pos >= 0);
694 _dbus_assert (value_pos <= _dbus_string_get_length (value_str) - len);
696 _dbus_verbose ("validating body from pos %d len %d sig '%s'\n",
697 value_pos, len, _dbus_string_get_const_data_len (expected_signature,
698 expected_signature_start,
701 _dbus_type_reader_init_types_only (&reader,
702 expected_signature, expected_signature_start);
704 p = _dbus_string_get_const_data_len (value_str, value_pos, len);
707 validity = validate_body_helper (&reader, byte_order, TRUE, p, end, &p);
708 if (validity != DBUS_VALID)
713 *bytes_remaining = end - p;
717 return DBUS_INVALID_TOO_MUCH_DATA;
720 _dbus_assert (p == end);
726 * Determine wether the given character is valid as the first character
729 #define VALID_INITIAL_NAME_CHARACTER(c) \
730 ( ((c) >= 'A' && (c) <= 'Z') || \
731 ((c) >= 'a' && (c) <= 'z') || \
735 * Determine wether the given character is valid as a second or later
736 * character in a name
738 #define VALID_NAME_CHARACTER(c) \
739 ( ((c) >= '0' && (c) <= '9') || \
740 ((c) >= 'A' && (c) <= 'Z') || \
741 ((c) >= 'a' && (c) <= 'z') || \
745 * Checks that the given range of the string is a valid object path
746 * name in the D-Bus protocol. Part of the validation ensures that
747 * the object path contains only ASCII.
749 * @todo this is inconsistent with most of DBusString in that
750 * it allows a start,len range that extends past the string end.
752 * @todo change spec to disallow more things, such as spaces in the
755 * @param str the string
756 * @param start first byte index to check
757 * @param len number of bytes to check
758 * @returns #TRUE if the byte range exists and is a valid name
761 _dbus_validate_path (const DBusString *str,
765 const unsigned char *s;
766 const unsigned char *end;
767 const unsigned char *last_slash;
769 _dbus_assert (start >= 0);
770 _dbus_assert (len >= 0);
771 _dbus_assert (start <= _dbus_string_get_length (str));
773 if (len > _dbus_string_get_length (str) - start)
779 s = _dbus_string_get_const_data (str) + start;
791 if ((s - last_slash) < 2)
792 return FALSE; /* no empty path components allowed */
798 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
805 if ((end - last_slash) < 2 &&
807 return FALSE; /* trailing slash not allowed unless the string is "/" */
813 * Checks that the given range of the string is a valid interface name
814 * in the D-Bus protocol. This includes a length restriction and an
815 * ASCII subset, see the specification.
817 * @todo this is inconsistent with most of DBusString in that
818 * it allows a start,len range that extends past the string end.
820 * @param str the string
821 * @param start first byte index to check
822 * @param len number of bytes to check
823 * @returns #TRUE if the byte range exists and is a valid name
826 _dbus_validate_interface (const DBusString *str,
830 const unsigned char *s;
831 const unsigned char *end;
832 const unsigned char *iface;
833 const unsigned char *last_dot;
835 _dbus_assert (start >= 0);
836 _dbus_assert (len >= 0);
837 _dbus_assert (start <= _dbus_string_get_length (str));
839 if (len > _dbus_string_get_length (str) - start)
842 if (len > DBUS_MAXIMUM_NAME_LENGTH)
849 iface = _dbus_string_get_const_data (str) + start;
853 /* check special cases of first char so it doesn't have to be done
854 * in the loop. Note we know len > 0
856 if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
858 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
867 if (_DBUS_UNLIKELY ((s + 1) == end))
869 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
872 ++s; /* we just validated the next char, so skip two */
874 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
882 if (_DBUS_UNLIKELY (last_dot == NULL))
889 * Checks that the given range of the string is a valid member name
890 * in the D-Bus protocol. This includes a length restriction, etc.,
891 * see the specification.
893 * @todo this is inconsistent with most of DBusString in that
894 * it allows a start,len range that extends past the string end.
896 * @param str the string
897 * @param start first byte index to check
898 * @param len number of bytes to check
899 * @returns #TRUE if the byte range exists and is a valid name
902 _dbus_validate_member (const DBusString *str,
906 const unsigned char *s;
907 const unsigned char *end;
908 const unsigned char *member;
910 _dbus_assert (start >= 0);
911 _dbus_assert (len >= 0);
912 _dbus_assert (start <= _dbus_string_get_length (str));
914 if (len > _dbus_string_get_length (str) - start)
917 if (len > DBUS_MAXIMUM_NAME_LENGTH)
923 member = _dbus_string_get_const_data (str) + start;
927 /* check special cases of first char so it doesn't have to be done
928 * in the loop. Note we know len > 0
931 if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
938 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
950 * Checks that the given range of the string is a valid error name
951 * in the D-Bus protocol. This includes a length restriction, etc.,
952 * see the specification.
954 * @todo this is inconsistent with most of DBusString in that
955 * it allows a start,len range that extends past the string end.
957 * @param str the string
958 * @param start first byte index to check
959 * @param len number of bytes to check
960 * @returns #TRUE if the byte range exists and is a valid name
963 _dbus_validate_error_name (const DBusString *str,
967 /* Same restrictions as interface name at the moment */
968 return _dbus_validate_interface (str, start, len);
972 * Determine wether the given character is valid as the first character
975 #define VALID_INITIAL_BUS_NAME_CHARACTER(c) \
976 ( ((c) >= 'A' && (c) <= 'Z') || \
977 ((c) >= 'a' && (c) <= 'z') || \
978 ((c) == '_') || ((c) == '-'))
981 * Determine wether the given character is valid as a second or later
982 * character in a bus name
984 #define VALID_BUS_NAME_CHARACTER(c) \
985 ( ((c) >= '0' && (c) <= '9') || \
986 ((c) >= 'A' && (c) <= 'Z') || \
987 ((c) >= 'a' && (c) <= 'z') || \
988 ((c) == '_') || ((c) == '-'))
991 * Checks that the given range of the string is a valid bus name in
992 * the D-Bus protocol. This includes a length restriction, etc., see
995 * @todo this is inconsistent with most of DBusString in that
996 * it allows a start,len range that extends past the string end.
998 * @param str the string
999 * @param start first byte index to check
1000 * @param len number of bytes to check
1001 * @returns #TRUE if the byte range exists and is a valid name
1004 _dbus_validate_bus_name (const DBusString *str,
1008 const unsigned char *s;
1009 const unsigned char *end;
1010 const unsigned char *iface;
1011 const unsigned char *last_dot;
1013 _dbus_assert (start >= 0);
1014 _dbus_assert (len >= 0);
1015 _dbus_assert (start <= _dbus_string_get_length (str));
1017 if (len > _dbus_string_get_length (str) - start)
1020 if (len > DBUS_MAXIMUM_NAME_LENGTH)
1027 iface = _dbus_string_get_const_data (str) + start;
1031 /* check special cases of first char so it doesn't have to be done
1032 * in the loop. Note we know len > 0
1042 if (_DBUS_UNLIKELY ((s + 1) == end))
1044 if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*(s + 1))))
1046 ++s; /* we just validated the next char, so skip two */
1048 else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1058 else if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
1060 else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*s)))
1069 if (_DBUS_UNLIKELY ((s + 1) == end))
1071 else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*(s + 1))))
1074 ++s; /* we just validated the next char, so skip two */
1076 else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1084 if (_DBUS_UNLIKELY (last_dot == NULL))
1091 * Checks that the given range of the string is a valid message type
1092 * signature in the D-Bus protocol.
1094 * @todo this is inconsistent with most of DBusString in that
1095 * it allows a start,len range that extends past the string end.
1097 * @param str the string
1098 * @param start first byte index to check
1099 * @param len number of bytes to check
1100 * @returns #TRUE if the byte range exists and is a valid signature
1103 _dbus_validate_signature (const DBusString *str,
1107 _dbus_assert (start >= 0);
1108 _dbus_assert (start <= _dbus_string_get_length (str));
1109 _dbus_assert (len >= 0);
1111 if (len > _dbus_string_get_length (str) - start)
1114 return _dbus_validate_signature_with_reason (str, start, len) == DBUS_VALID;
1117 /** define _dbus_check_is_valid_path() */
1118 DEFINE_DBUS_NAME_CHECK(path)
1119 /** define _dbus_check_is_valid_interface() */
1120 DEFINE_DBUS_NAME_CHECK(interface)
1121 /** define _dbus_check_is_valid_member() */
1122 DEFINE_DBUS_NAME_CHECK(member)
1123 /** define _dbus_check_is_valid_error_name() */
1124 DEFINE_DBUS_NAME_CHECK(error_name)
1125 /** define _dbus_check_is_valid_bus_name() */
1126 DEFINE_DBUS_NAME_CHECK(bus_name)
1127 /** define _dbus_check_is_valid_signature() */
1128 DEFINE_DBUS_NAME_CHECK(signature)
1132 /* tests in dbus-marshal-validate-util.c */