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)
251 if (!(_dbus_type_is_valid (*p) && dbus_type_is_basic (*p)))
253 result = DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE;
265 result = DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
269 if (struct_depth > 0)
271 result = DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED;
275 if (dict_entry_depth > 0)
277 result = DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED;
281 _dbus_assert (last != DBUS_TYPE_ARRAY);
282 _dbus_assert (last != DBUS_STRUCT_BEGIN_CHAR);
283 _dbus_assert (last != DBUS_DICT_ENTRY_BEGIN_CHAR);
288 _dbus_list_clear (&element_count_stack);
293 validate_body_helper (DBusTypeReader *reader,
295 dbus_bool_t walk_reader_to_end,
296 const unsigned char *p,
297 const unsigned char *end,
298 const unsigned char **new_p)
302 while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
304 const unsigned char *a;
308 _dbus_verbose (" validating value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
309 _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
313 /* Guarantee that p has one byte to look at */
315 return DBUS_INVALID_NOT_ENOUGH_DATA;
317 switch (current_type)
323 case DBUS_TYPE_BOOLEAN:
324 case DBUS_TYPE_INT16:
325 case DBUS_TYPE_UINT16:
326 case DBUS_TYPE_INT32:
327 case DBUS_TYPE_UINT32:
328 case DBUS_TYPE_INT64:
329 case DBUS_TYPE_UINT64:
330 case DBUS_TYPE_DOUBLE:
331 alignment = _dbus_type_get_alignment (current_type);
332 a = _DBUS_ALIGN_ADDRESS (p, alignment);
334 return DBUS_INVALID_NOT_ENOUGH_DATA;
338 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
342 if (current_type == DBUS_TYPE_BOOLEAN)
344 dbus_uint32_t v = _dbus_unpack_uint32 (byte_order,
346 if (!(v == 0 || v == 1))
347 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
353 case DBUS_TYPE_ARRAY:
354 case DBUS_TYPE_STRING:
355 case DBUS_TYPE_OBJECT_PATH:
357 dbus_uint32_t claimed_len;
359 a = _DBUS_ALIGN_ADDRESS (p, 4);
361 return DBUS_INVALID_NOT_ENOUGH_DATA;
365 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
369 claimed_len = _dbus_unpack_uint32 (byte_order, p);
372 /* p may now be == end */
373 _dbus_assert (p <= end);
375 if (current_type == DBUS_TYPE_ARRAY)
377 int array_elem_type = _dbus_type_reader_get_element_type (reader);
379 if (!_dbus_type_is_valid (array_elem_type))
381 return DBUS_INVALID_UNKNOWN_TYPECODE;
384 alignment = _dbus_type_get_alignment (array_elem_type);
386 a = _DBUS_ALIGN_ADDRESS (p, alignment);
388 /* a may now be == end */
390 return DBUS_INVALID_NOT_ENOUGH_DATA;
395 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
400 if (claimed_len > (unsigned long) (end - p))
401 return DBUS_INVALID_LENGTH_OUT_OF_BOUNDS;
403 if (current_type == DBUS_TYPE_OBJECT_PATH)
406 _dbus_string_init_const_len (&str, p, claimed_len);
407 if (!_dbus_validate_path (&str, 0,
408 _dbus_string_get_length (&str)))
409 return DBUS_INVALID_BAD_PATH;
413 else if (current_type == DBUS_TYPE_STRING)
416 _dbus_string_init_const_len (&str, p, claimed_len);
417 if (!_dbus_string_validate_utf8 (&str, 0,
418 _dbus_string_get_length (&str)))
419 return DBUS_INVALID_BAD_UTF8_IN_STRING;
423 else if (current_type == DBUS_TYPE_ARRAY && claimed_len > 0)
426 DBusValidity validity;
427 const unsigned char *array_end;
430 if (claimed_len > DBUS_MAXIMUM_ARRAY_LENGTH)
431 return DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM;
433 /* Remember that the reader is types only, so we can't
434 * use it to iterate over elements. It stays the same
437 _dbus_type_reader_recurse (reader, &sub);
439 array_end = p + claimed_len;
441 array_elem_type = _dbus_type_reader_get_element_type (reader);
443 /* avoid recursive call to validate_body_helper if this is an array
444 * of fixed-size elements
446 if (dbus_type_is_fixed (array_elem_type))
448 /* bools need to be handled differently, because they can
449 * have an invalid value
451 if (array_elem_type == DBUS_TYPE_BOOLEAN)
454 alignment = _dbus_type_get_alignment (array_elem_type);
456 while (p < array_end)
458 v = _dbus_unpack_uint32 (byte_order, p);
460 if (!(v == 0 || v == 1))
461 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
475 while (p < array_end)
477 validity = validate_body_helper (&sub, byte_order, FALSE, p, end, &p);
478 if (validity != DBUS_VALID)
484 return DBUS_INVALID_ARRAY_LENGTH_INCORRECT;
487 /* check nul termination */
488 if (current_type != DBUS_TYPE_ARRAY)
491 return DBUS_INVALID_NOT_ENOUGH_DATA;
494 return DBUS_INVALID_STRING_MISSING_NUL;
500 case DBUS_TYPE_SIGNATURE:
502 dbus_uint32_t claimed_len;
504 DBusValidity validity;
509 /* 1 is for nul termination */
510 if (claimed_len + 1 > (unsigned long) (end - p))
511 return DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
513 _dbus_string_init_const_len (&str, p, claimed_len);
515 _dbus_validate_signature_with_reason (&str, 0,
516 _dbus_string_get_length (&str));
518 if (validity != DBUS_VALID)
523 _dbus_assert (p < end);
524 if (*p != DBUS_TYPE_INVALID)
525 return DBUS_INVALID_SIGNATURE_MISSING_NUL;
529 _dbus_verbose ("p = %p end = %p claimed_len %u\n", p, end, claimed_len);
533 case DBUS_TYPE_VARIANT:
535 /* 1 byte sig len, sig typecodes, align to
536 * contained-type-boundary, values.
539 /* In addition to normal signature validation, we need to be sure
540 * the signature contains only a single (possibly container) type.
542 dbus_uint32_t claimed_len;
545 DBusValidity validity;
546 int contained_alignment;
554 if (claimed_len + 1 > (unsigned long) (end - p))
555 return DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
557 _dbus_string_init_const_len (&sig, p, claimed_len);
558 reason = _dbus_validate_signature_with_reason (&sig, 0,
559 _dbus_string_get_length (&sig));
560 if (!(reason == DBUS_VALID))
562 if (reason == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
565 return DBUS_INVALID_VARIANT_SIGNATURE_BAD;
570 if (*p != DBUS_TYPE_INVALID)
571 return DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL;
574 contained_type = _dbus_first_type_in_signature (&sig, 0);
575 if (contained_type == DBUS_TYPE_INVALID)
576 return DBUS_INVALID_VARIANT_SIGNATURE_EMPTY;
578 contained_alignment = _dbus_type_get_alignment (contained_type);
580 a = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
582 return DBUS_INVALID_NOT_ENOUGH_DATA;
586 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
590 _dbus_type_reader_init_types_only (&sub, &sig, 0);
592 _dbus_assert (_dbus_type_reader_get_current_type (&sub) != DBUS_TYPE_INVALID);
594 validity = validate_body_helper (&sub, byte_order, FALSE, p, end, &p);
595 if (validity != DBUS_VALID)
598 if (_dbus_type_reader_next (&sub))
599 return DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES;
601 _dbus_assert (_dbus_type_reader_get_current_type (&sub) == DBUS_TYPE_INVALID);
605 case DBUS_TYPE_DICT_ENTRY:
606 case DBUS_TYPE_STRUCT:
609 DBusValidity validity;
611 a = _DBUS_ALIGN_ADDRESS (p, 8);
613 return DBUS_INVALID_NOT_ENOUGH_DATA;
617 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
621 _dbus_type_reader_recurse (reader, &sub);
623 validity = validate_body_helper (&sub, byte_order, TRUE, p, end, &p);
624 if (validity != DBUS_VALID)
630 _dbus_assert_not_reached ("invalid typecode in supposedly-validated signature");
635 _dbus_verbose (" validated value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
636 _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
642 _dbus_verbose ("not enough data!!! p = %p end = %p end-p = %d\n",
643 p, end, (int) (end - p));
644 return DBUS_INVALID_NOT_ENOUGH_DATA;
647 if (walk_reader_to_end)
648 _dbus_type_reader_next (reader);
660 * Verifies that the range of value_str from value_pos to value_end is
661 * a legitimate value of type expected_signature. If this function
662 * returns #TRUE, it will be safe to iterate over the values with
663 * #DBusTypeReader. The signature is assumed to be already valid.
665 * If bytes_remaining is not #NULL, then leftover bytes will be stored
666 * there and #DBUS_VALID returned. If it is #NULL, then
667 * #DBUS_INVALID_TOO_MUCH_DATA will be returned if bytes are left
670 * @param expected_signature the expected types in the value_str
671 * @param expected_signature_start where in expected_signature is the signature
672 * @param byte_order the byte order
673 * @param bytes_remaining place to store leftover bytes
674 * @param value_str the string containing the body
675 * @param value_pos where the values start
676 * @param len length of values after value_pos
677 * @returns #DBUS_VALID if valid, reason why invalid otherwise
680 _dbus_validate_body_with_reason (const DBusString *expected_signature,
681 int expected_signature_start,
683 int *bytes_remaining,
684 const DBusString *value_str,
688 DBusTypeReader reader;
689 const unsigned char *p;
690 const unsigned char *end;
691 DBusValidity validity;
693 _dbus_assert (len >= 0);
694 _dbus_assert (value_pos >= 0);
695 _dbus_assert (value_pos <= _dbus_string_get_length (value_str) - len);
697 _dbus_verbose ("validating body from pos %d len %d sig '%s'\n",
698 value_pos, len, _dbus_string_get_const_data_len (expected_signature,
699 expected_signature_start,
702 _dbus_type_reader_init_types_only (&reader,
703 expected_signature, expected_signature_start);
705 p = _dbus_string_get_const_data_len (value_str, value_pos, len);
708 validity = validate_body_helper (&reader, byte_order, TRUE, p, end, &p);
709 if (validity != DBUS_VALID)
714 *bytes_remaining = end - p;
718 return DBUS_INVALID_TOO_MUCH_DATA;
721 _dbus_assert (p == end);
727 * Determine wether the given character is valid as the first character
730 #define VALID_INITIAL_NAME_CHARACTER(c) \
731 ( ((c) >= 'A' && (c) <= 'Z') || \
732 ((c) >= 'a' && (c) <= 'z') || \
736 * Determine wether the given character is valid as a second or later
737 * character in a name
739 #define VALID_NAME_CHARACTER(c) \
740 ( ((c) >= '0' && (c) <= '9') || \
741 ((c) >= 'A' && (c) <= 'Z') || \
742 ((c) >= 'a' && (c) <= 'z') || \
746 * Checks that the given range of the string is a valid object path
747 * name in the D-Bus protocol. Part of the validation ensures that
748 * the object path contains only ASCII.
750 * @todo this is inconsistent with most of DBusString in that
751 * it allows a start,len range that extends past the string end.
753 * @todo change spec to disallow more things, such as spaces in the
756 * @param str the string
757 * @param start first byte index to check
758 * @param len number of bytes to check
759 * @returns #TRUE if the byte range exists and is a valid name
762 _dbus_validate_path (const DBusString *str,
766 const unsigned char *s;
767 const unsigned char *end;
768 const unsigned char *last_slash;
770 _dbus_assert (start >= 0);
771 _dbus_assert (len >= 0);
772 _dbus_assert (start <= _dbus_string_get_length (str));
774 if (len > _dbus_string_get_length (str) - start)
780 s = _dbus_string_get_const_data (str) + start;
792 if ((s - last_slash) < 2)
793 return FALSE; /* no empty path components allowed */
799 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
806 if ((end - last_slash) < 2 &&
808 return FALSE; /* trailing slash not allowed unless the string is "/" */
814 _dbus_validity_to_error_message (DBusValidity validity)
818 case DBUS_VALIDITY_UNKNOWN_OOM_ERROR: return "Out of memory";
819 case DBUS_INVALID_FOR_UNKNOWN_REASON: return "Unknown reason";
820 case DBUS_VALID_BUT_INCOMPLETE: return "Valid but incomplete";
821 case DBUS_VALIDITY_UNKNOWN: return "Validity unknown";
822 case DBUS_VALID: return "Valid";
823 case DBUS_INVALID_UNKNOWN_TYPECODE: return "Unknown typecode";
824 case DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE: return "Missing array element type";
825 case DBUS_INVALID_SIGNATURE_TOO_LONG: return "Signature is too long";
826 case DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION: return "Exceeded maximum array recursion";
827 case DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION: return "Exceeded maximum struct recursion";
828 case DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED: return "Struct ended but not started";
829 case DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED: return "Struct started but not ended";
830 case DBUS_INVALID_STRUCT_HAS_NO_FIELDS: return "Struct has no fields";
831 case DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL: return "Alignment padding not null";
832 case DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE: return "Boolean is not zero or one";
833 case DBUS_INVALID_NOT_ENOUGH_DATA: return "Not enough data";
834 case DBUS_INVALID_TOO_MUCH_DATA: return "Too much data";
835 case DBUS_INVALID_BAD_BYTE_ORDER: return "Bad byte order";
836 case DBUS_INVALID_BAD_PROTOCOL_VERSION: return "Bad protocol version";
837 case DBUS_INVALID_BAD_MESSAGE_TYPE: return "Bad message type";
838 case DBUS_INVALID_BAD_SERIAL: return "Bad serial";
839 case DBUS_INVALID_INSANE_FIELDS_ARRAY_LENGTH: return "Insane fields array length";
840 case DBUS_INVALID_INSANE_BODY_LENGTH: return "Insane body length";
841 case DBUS_INVALID_MESSAGE_TOO_LONG: return "Message too long";
842 case DBUS_INVALID_HEADER_FIELD_CODE: return "Header field code";
843 case DBUS_INVALID_HEADER_FIELD_HAS_WRONG_TYPE: return "Header field has wrong type";
844 case DBUS_INVALID_USES_LOCAL_INTERFACE: return "Uses local interface";
845 case DBUS_INVALID_USES_LOCAL_PATH: return "Uses local path";
846 case DBUS_INVALID_HEADER_FIELD_APPEARS_TWICE: return "Header field appears twice";
847 case DBUS_INVALID_BAD_DESTINATION: return "Bad destination";
848 case DBUS_INVALID_BAD_INTERFACE: return "Bad interface";
849 case DBUS_INVALID_BAD_MEMBER: return "Bad member";
850 case DBUS_INVALID_BAD_ERROR_NAME: return "Bad error name";
851 case DBUS_INVALID_BAD_SENDER: return "Bad sender";
852 case DBUS_INVALID_MISSING_PATH: return "Missing path";
853 case DBUS_INVALID_MISSING_INTERFACE: return "Missing interface";
854 case DBUS_INVALID_MISSING_MEMBER: return "Missing member";
855 case DBUS_INVALID_MISSING_ERROR_NAME: return "Missing error name";
856 case DBUS_INVALID_MISSING_REPLY_SERIAL: return "Missing reply serial";
857 case DBUS_INVALID_LENGTH_OUT_OF_BOUNDS: return "Length out of bounds";
858 case DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM: return "Array length exceeds maximum";
859 case DBUS_INVALID_BAD_PATH: return "Bad path";
860 case DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS: return "Signature length out of bounds";
861 case DBUS_INVALID_BAD_UTF8_IN_STRING: return "Bad utf8 in string";
862 case DBUS_INVALID_ARRAY_LENGTH_INCORRECT: return "Array length incorrect";
863 case DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS: return "Variant signature length out of bounds";
864 case DBUS_INVALID_VARIANT_SIGNATURE_BAD: return "Variant signature bad";
865 case DBUS_INVALID_VARIANT_SIGNATURE_EMPTY: return "Variant signature empty";
866 case DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES: return "Variant signature specifies multiple values";
867 case DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL: return "Variant signature missing nul";
868 case DBUS_INVALID_STRING_MISSING_NUL: return "String missing nul";
869 case DBUS_INVALID_SIGNATURE_MISSING_NUL: return "Signature missing nul";
870 case DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION: return "Exceeded maximum dict entry recursion";
871 case DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED: return "Dict entry ended but not started";
872 case DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED: return "Dict entry started but not ended";
873 case DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS: return "Dict entry has no fields";
874 case DBUS_INVALID_DICT_ENTRY_HAS_ONLY_ONE_FIELD: return "Dict entry has only one field";
875 case DBUS_INVALID_DICT_ENTRY_HAS_TOO_MANY_FIELDS: return "Dict entry has too many fields";
876 case DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY: return "Dict entry not inside array";
877 case DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE: return "Dict key must be basic type";
885 * Checks that the given range of the string is a valid interface name
886 * in the D-Bus protocol. This includes a length restriction and an
887 * ASCII subset, see the specification.
889 * @todo this is inconsistent with most of DBusString in that
890 * it allows a start,len range that extends past the string end.
892 * @param str the string
893 * @param start first byte index to check
894 * @param len number of bytes to check
895 * @returns #TRUE if the byte range exists and is a valid name
898 _dbus_validate_interface (const DBusString *str,
902 const unsigned char *s;
903 const unsigned char *end;
904 const unsigned char *iface;
905 const unsigned char *last_dot;
907 _dbus_assert (start >= 0);
908 _dbus_assert (len >= 0);
909 _dbus_assert (start <= _dbus_string_get_length (str));
911 if (len > _dbus_string_get_length (str) - start)
914 if (len > DBUS_MAXIMUM_NAME_LENGTH)
921 iface = _dbus_string_get_const_data (str) + start;
925 /* check special cases of first char so it doesn't have to be done
926 * in the loop. Note we know len > 0
928 if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
930 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
939 if (_DBUS_UNLIKELY ((s + 1) == end))
941 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
944 ++s; /* we just validated the next char, so skip two */
946 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
954 if (_DBUS_UNLIKELY (last_dot == NULL))
961 * Checks that the given range of the string is a valid member name
962 * in the D-Bus protocol. This includes a length restriction, etc.,
963 * see the specification.
965 * @todo this is inconsistent with most of DBusString in that
966 * it allows a start,len range that extends past the string end.
968 * @param str the string
969 * @param start first byte index to check
970 * @param len number of bytes to check
971 * @returns #TRUE if the byte range exists and is a valid name
974 _dbus_validate_member (const DBusString *str,
978 const unsigned char *s;
979 const unsigned char *end;
980 const unsigned char *member;
982 _dbus_assert (start >= 0);
983 _dbus_assert (len >= 0);
984 _dbus_assert (start <= _dbus_string_get_length (str));
986 if (len > _dbus_string_get_length (str) - start)
989 if (len > DBUS_MAXIMUM_NAME_LENGTH)
995 member = _dbus_string_get_const_data (str) + start;
999 /* check special cases of first char so it doesn't have to be done
1000 * in the loop. Note we know len > 0
1003 if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
1010 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
1022 * Checks that the given range of the string is a valid error name
1023 * in the D-Bus protocol. This includes a length restriction, etc.,
1024 * see the specification.
1026 * @todo this is inconsistent with most of DBusString in that
1027 * it allows a start,len range that extends past the string end.
1029 * @param str the string
1030 * @param start first byte index to check
1031 * @param len number of bytes to check
1032 * @returns #TRUE if the byte range exists and is a valid name
1035 _dbus_validate_error_name (const DBusString *str,
1039 /* Same restrictions as interface name at the moment */
1040 return _dbus_validate_interface (str, start, len);
1044 * Determine wether the given character is valid as the first character
1047 #define VALID_INITIAL_BUS_NAME_CHARACTER(c) \
1048 ( ((c) >= 'A' && (c) <= 'Z') || \
1049 ((c) >= 'a' && (c) <= 'z') || \
1050 ((c) == '_') || ((c) == '-'))
1053 * Determine wether the given character is valid as a second or later
1054 * character in a bus name
1056 #define VALID_BUS_NAME_CHARACTER(c) \
1057 ( ((c) >= '0' && (c) <= '9') || \
1058 ((c) >= 'A' && (c) <= 'Z') || \
1059 ((c) >= 'a' && (c) <= 'z') || \
1060 ((c) == '_') || ((c) == '-'))
1063 * Checks that the given range of the string is a valid bus name in
1064 * the D-Bus protocol. This includes a length restriction, etc., see
1065 * the specification.
1067 * @todo this is inconsistent with most of DBusString in that
1068 * it allows a start,len range that extends past the string end.
1070 * @param str the string
1071 * @param start first byte index to check
1072 * @param len number of bytes to check
1073 * @returns #TRUE if the byte range exists and is a valid name
1076 _dbus_validate_bus_name (const DBusString *str,
1080 const unsigned char *s;
1081 const unsigned char *end;
1082 const unsigned char *iface;
1083 const unsigned char *last_dot;
1085 _dbus_assert (start >= 0);
1086 _dbus_assert (len >= 0);
1087 _dbus_assert (start <= _dbus_string_get_length (str));
1089 if (len > _dbus_string_get_length (str) - start)
1092 if (len > DBUS_MAXIMUM_NAME_LENGTH)
1099 iface = _dbus_string_get_const_data (str) + start;
1103 /* check special cases of first char so it doesn't have to be done
1104 * in the loop. Note we know len > 0
1114 if (_DBUS_UNLIKELY ((s + 1) == end))
1116 if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*(s + 1))))
1118 ++s; /* we just validated the next char, so skip two */
1120 else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1130 else if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
1132 else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*s)))
1141 if (_DBUS_UNLIKELY ((s + 1) == end))
1143 else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*(s + 1))))
1146 ++s; /* we just validated the next char, so skip two */
1148 else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1156 if (_DBUS_UNLIKELY (last_dot == NULL))
1163 * Checks that the given range of the string is a valid message type
1164 * signature in the D-Bus protocol.
1166 * @todo this is inconsistent with most of DBusString in that
1167 * it allows a start,len range that extends past the string end.
1169 * @param str the string
1170 * @param start first byte index to check
1171 * @param len number of bytes to check
1172 * @returns #TRUE if the byte range exists and is a valid signature
1175 _dbus_validate_signature (const DBusString *str,
1179 _dbus_assert (start >= 0);
1180 _dbus_assert (start <= _dbus_string_get_length (str));
1181 _dbus_assert (len >= 0);
1183 if (len > _dbus_string_get_length (str) - start)
1186 return _dbus_validate_signature_with_reason (str, start, len) == DBUS_VALID;
1189 /** define _dbus_check_is_valid_path() */
1190 DEFINE_DBUS_NAME_CHECK(path)
1191 /** define _dbus_check_is_valid_interface() */
1192 DEFINE_DBUS_NAME_CHECK(interface)
1193 /** define _dbus_check_is_valid_member() */
1194 DEFINE_DBUS_NAME_CHECK(member)
1195 /** define _dbus_check_is_valid_error_name() */
1196 DEFINE_DBUS_NAME_CHECK(error_name)
1197 /** define _dbus_check_is_valid_bus_name() */
1198 DEFINE_DBUS_NAME_CHECK(bus_name)
1199 /** define _dbus_check_is_valid_signature() */
1200 DEFINE_DBUS_NAME_CHECK(signature)
1204 /* tests in dbus-marshal-validate-util.c */