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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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"
33 * @addtogroup DBusMarshal
39 * Verifies that the range of type_str from type_pos to type_end is a
40 * valid signature. If this function returns #TRUE, it will be safe
41 * to iterate over the signature with a types-only #DBusTypeReader.
42 * The range passed in should NOT include the terminating
43 * nul/DBUS_TYPE_INVALID.
45 * @param type_str the string
46 * @param type_pos where the typecodes start
47 * @param len length of typecodes
48 * @returns #DBUS_VALID if valid, reason why invalid otherwise
51 _dbus_validate_signature_with_reason (const DBusString *type_str,
55 const unsigned char *p;
56 const unsigned char *end;
64 DBusList *element_count_stack;
67 element_count_stack = NULL;
69 if (!_dbus_list_append (&element_count_stack, _DBUS_INT_TO_POINTER (0)))
71 result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
75 _dbus_assert (type_str != NULL);
76 _dbus_assert (type_pos < _DBUS_INT32_MAX - len);
77 _dbus_assert (len >= 0);
78 _dbus_assert (type_pos >= 0);
80 if (len > DBUS_MAXIMUM_SIGNATURE_LENGTH)
82 result = DBUS_INVALID_SIGNATURE_TOO_LONG;
86 p = _dbus_string_get_const_udata_len (type_str, type_pos, 0);
88 end = _dbus_string_get_const_udata_len (type_str, type_pos + len, 0);
92 last = DBUS_TYPE_INVALID;
99 case DBUS_TYPE_BOOLEAN:
100 case DBUS_TYPE_INT16:
101 case DBUS_TYPE_UINT16:
102 case DBUS_TYPE_INT32:
103 case DBUS_TYPE_UINT32:
104 case DBUS_TYPE_UNIX_FD:
105 case DBUS_TYPE_INT64:
106 case DBUS_TYPE_UINT64:
107 case DBUS_TYPE_DOUBLE:
108 case DBUS_TYPE_STRING:
109 case DBUS_TYPE_OBJECT_PATH:
110 case DBUS_TYPE_SIGNATURE:
111 case DBUS_TYPE_VARIANT:
114 case DBUS_TYPE_ARRAY:
116 if (array_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
118 result = DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION;
123 case DBUS_STRUCT_BEGIN_CHAR:
126 if (struct_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
128 result = DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION;
132 if (!_dbus_list_append (&element_count_stack,
133 _DBUS_INT_TO_POINTER (0)))
135 result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
141 case DBUS_STRUCT_END_CHAR:
142 if (struct_depth == 0)
144 result = DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED;
148 if (last == DBUS_STRUCT_BEGIN_CHAR)
150 result = DBUS_INVALID_STRUCT_HAS_NO_FIELDS;
154 _dbus_list_pop_last (&element_count_stack);
159 case DBUS_DICT_ENTRY_BEGIN_CHAR:
160 if (last != DBUS_TYPE_ARRAY)
162 result = DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY;
166 dict_entry_depth += 1;
168 if (dict_entry_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
170 result = DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION;
174 if (!_dbus_list_append (&element_count_stack,
175 _DBUS_INT_TO_POINTER (0)))
177 result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
183 case DBUS_DICT_ENTRY_END_CHAR:
184 if (dict_entry_depth == 0)
186 result = DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED;
190 dict_entry_depth -= 1;
193 _DBUS_POINTER_TO_INT (_dbus_list_pop_last (&element_count_stack));
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;
208 case DBUS_TYPE_STRUCT: /* doesn't appear in signatures */
209 case DBUS_TYPE_DICT_ENTRY: /* ditto */
211 result = DBUS_INVALID_UNKNOWN_TYPECODE;
215 if (*p != DBUS_TYPE_ARRAY &&
216 *p != DBUS_DICT_ENTRY_BEGIN_CHAR &&
217 *p != DBUS_STRUCT_BEGIN_CHAR)
220 _DBUS_POINTER_TO_INT (_dbus_list_pop_last (&element_count_stack));
224 if (!_dbus_list_append (&element_count_stack,
225 _DBUS_INT_TO_POINTER (element_count)))
227 result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
234 if (*p == DBUS_TYPE_ARRAY && p != end)
236 const unsigned char *p1;
238 if (*p1 == DBUS_STRUCT_END_CHAR ||
239 *p1 == DBUS_DICT_ENTRY_END_CHAR)
241 result = DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
251 if (last == DBUS_DICT_ENTRY_BEGIN_CHAR)
253 if (!(dbus_type_is_valid (*p) && dbus_type_is_basic (*p)))
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;
283 _dbus_assert (last != DBUS_TYPE_ARRAY);
284 _dbus_assert (last != DBUS_STRUCT_BEGIN_CHAR);
285 _dbus_assert (last != DBUS_DICT_ENTRY_BEGIN_CHAR);
290 _dbus_list_clear (&element_count_stack);
294 /* note: this function is also used to validate the header's values,
295 * since the header is a valid body with a particular signature.
298 validate_body_helper (DBusTypeReader *reader,
300 dbus_bool_t walk_reader_to_end,
302 const unsigned char *p,
303 const unsigned char *end,
304 const unsigned char **new_p)
308 /* The spec allows arrays and structs to each nest 32, for total
309 * nesting of 2*32. We want to impose the same limit on "dynamic"
310 * value nesting (not visible in the signature) which is introduced
311 * by DBUS_TYPE_VARIANT.
313 if (total_depth > (DBUS_MAXIMUM_TYPE_RECURSION_DEPTH * 2))
315 return DBUS_INVALID_NESTED_TOO_DEEPLY;
318 while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
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",
325 _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
329 /* Guarantee that p has one byte to look at */
331 return DBUS_INVALID_NOT_ENOUGH_DATA;
333 switch (current_type)
339 case DBUS_TYPE_BOOLEAN:
340 case DBUS_TYPE_INT16:
341 case DBUS_TYPE_UINT16:
342 case DBUS_TYPE_INT32:
343 case DBUS_TYPE_UINT32:
344 case DBUS_TYPE_UNIX_FD:
345 case DBUS_TYPE_INT64:
346 case DBUS_TYPE_UINT64:
347 case DBUS_TYPE_DOUBLE:
348 alignment = _dbus_type_get_alignment (current_type);
349 a = _DBUS_ALIGN_ADDRESS (p, alignment);
351 return DBUS_INVALID_NOT_ENOUGH_DATA;
355 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
359 if (current_type == DBUS_TYPE_BOOLEAN)
364 return DBUS_INVALID_NOT_ENOUGH_DATA;
366 v = _dbus_unpack_uint32 (byte_order, p);
368 if (!(v == 0 || v == 1))
369 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
375 case DBUS_TYPE_ARRAY:
376 case DBUS_TYPE_STRING:
377 case DBUS_TYPE_OBJECT_PATH:
379 dbus_uint32_t claimed_len;
381 a = _DBUS_ALIGN_ADDRESS (p, 4);
383 return DBUS_INVALID_NOT_ENOUGH_DATA;
387 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
391 claimed_len = _dbus_unpack_uint32 (byte_order, p);
394 /* p may now be == end */
395 _dbus_assert (p <= end);
397 if (current_type == DBUS_TYPE_ARRAY)
399 int array_elem_type = _dbus_type_reader_get_element_type (reader);
401 if (!dbus_type_is_valid (array_elem_type))
403 return DBUS_INVALID_UNKNOWN_TYPECODE;
406 alignment = _dbus_type_get_alignment (array_elem_type);
408 a = _DBUS_ALIGN_ADDRESS (p, alignment);
410 /* a may now be == end */
412 return DBUS_INVALID_NOT_ENOUGH_DATA;
417 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
422 if (claimed_len > (unsigned long) (end - p))
423 return DBUS_INVALID_LENGTH_OUT_OF_BOUNDS;
425 if (current_type == DBUS_TYPE_OBJECT_PATH)
428 _dbus_string_init_const_len (&str, (const char *) p, claimed_len);
429 if (!_dbus_validate_path (&str, 0,
430 _dbus_string_get_length (&str)))
431 return DBUS_INVALID_BAD_PATH;
435 else if (current_type == DBUS_TYPE_STRING)
438 _dbus_string_init_const_len (&str, (const char *) p, claimed_len);
439 if (!_dbus_string_validate_utf8 (&str, 0,
440 _dbus_string_get_length (&str)))
441 return DBUS_INVALID_BAD_UTF8_IN_STRING;
445 else if (current_type == DBUS_TYPE_ARRAY && claimed_len > 0)
448 DBusValidity validity;
449 const unsigned char *array_end;
452 if (claimed_len > DBUS_MAXIMUM_ARRAY_LENGTH)
453 return DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM;
455 /* Remember that the reader is types only, so we can't
456 * use it to iterate over elements. It stays the same
459 _dbus_type_reader_recurse (reader, &sub);
461 array_end = p + claimed_len;
463 array_elem_type = _dbus_type_reader_get_element_type (reader);
465 /* avoid recursive call to validate_body_helper if this is an array
466 * of fixed-size elements
468 if (dbus_type_is_fixed (array_elem_type))
470 /* bools need to be handled differently, because they can
471 * have an invalid value
473 if (array_elem_type == DBUS_TYPE_BOOLEAN)
476 alignment = _dbus_type_get_alignment (array_elem_type);
478 while (p < array_end)
480 v = _dbus_unpack_uint32 (byte_order, p);
482 if (!(v == 0 || v == 1))
483 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
497 while (p < array_end)
499 validity = validate_body_helper (&sub, byte_order, FALSE,
502 if (validity != DBUS_VALID)
508 return DBUS_INVALID_ARRAY_LENGTH_INCORRECT;
511 /* check nul termination */
512 if (current_type != DBUS_TYPE_ARRAY)
515 return DBUS_INVALID_NOT_ENOUGH_DATA;
518 return DBUS_INVALID_STRING_MISSING_NUL;
524 case DBUS_TYPE_SIGNATURE:
526 dbus_uint32_t claimed_len;
528 DBusValidity validity;
533 /* 1 is for nul termination */
534 if (claimed_len + 1 > (unsigned long) (end - p))
535 return DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
537 _dbus_string_init_const_len (&str, (const char *) p, claimed_len);
539 _dbus_validate_signature_with_reason (&str, 0,
540 _dbus_string_get_length (&str));
542 if (validity != DBUS_VALID)
547 _dbus_assert (p < end);
548 if (*p != DBUS_TYPE_INVALID)
549 return DBUS_INVALID_SIGNATURE_MISSING_NUL;
553 _dbus_verbose ("p = %p end = %p claimed_len %u\n", p, end, claimed_len);
557 case DBUS_TYPE_VARIANT:
559 /* 1 byte sig len, sig typecodes, align to
560 * contained-type-boundary, values.
563 /* In addition to normal signature validation, we need to be sure
564 * the signature contains only a single (possibly container) type.
566 dbus_uint32_t claimed_len;
569 DBusValidity validity;
570 int contained_alignment;
578 if (claimed_len + 1 > (unsigned long) (end - p))
579 return DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
581 _dbus_string_init_const_len (&sig, (const char *) p, claimed_len);
582 reason = _dbus_validate_signature_with_reason (&sig, 0,
583 _dbus_string_get_length (&sig));
584 if (!(reason == DBUS_VALID))
586 if (reason == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
589 return DBUS_INVALID_VARIANT_SIGNATURE_BAD;
594 if (*p != DBUS_TYPE_INVALID)
595 return DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL;
598 contained_type = _dbus_first_type_in_signature (&sig, 0);
599 if (contained_type == DBUS_TYPE_INVALID)
600 return DBUS_INVALID_VARIANT_SIGNATURE_EMPTY;
602 contained_alignment = _dbus_type_get_alignment (contained_type);
604 a = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
606 return DBUS_INVALID_NOT_ENOUGH_DATA;
610 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
614 _dbus_type_reader_init_types_only (&sub, &sig, 0);
616 _dbus_assert (_dbus_type_reader_get_current_type (&sub) != DBUS_TYPE_INVALID);
618 validity = validate_body_helper (&sub, byte_order, FALSE,
621 if (validity != DBUS_VALID)
624 if (_dbus_type_reader_next (&sub))
625 return DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES;
627 _dbus_assert (_dbus_type_reader_get_current_type (&sub) == DBUS_TYPE_INVALID);
631 case DBUS_TYPE_DICT_ENTRY:
632 case DBUS_TYPE_STRUCT:
635 DBusValidity validity;
637 a = _DBUS_ALIGN_ADDRESS (p, 8);
639 return DBUS_INVALID_NOT_ENOUGH_DATA;
643 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
647 _dbus_type_reader_recurse (reader, &sub);
649 validity = validate_body_helper (&sub, byte_order, TRUE,
652 if (validity != DBUS_VALID)
658 _dbus_assert_not_reached ("invalid typecode in supposedly-validated signature");
663 _dbus_verbose (" validated value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
664 _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
670 _dbus_verbose ("not enough data!!! p = %p end = %p end-p = %d\n",
671 p, end, (int) (end - p));
672 return DBUS_INVALID_NOT_ENOUGH_DATA;
675 if (walk_reader_to_end)
676 _dbus_type_reader_next (reader);
688 * Verifies that the range of value_str from value_pos to value_end is
689 * a legitimate value of type expected_signature. If this function
690 * returns #TRUE, it will be safe to iterate over the values with
691 * #DBusTypeReader. The signature is assumed to be already valid.
693 * If bytes_remaining is not #NULL, then leftover bytes will be stored
694 * there and #DBUS_VALID returned. If it is #NULL, then
695 * #DBUS_INVALID_TOO_MUCH_DATA will be returned if bytes are left
698 * @param expected_signature the expected types in the value_str
699 * @param expected_signature_start where in expected_signature is the signature
700 * @param byte_order the byte order
701 * @param bytes_remaining place to store leftover bytes
702 * @param value_str the string containing the body
703 * @param value_pos where the values start
704 * @param len length of values after value_pos
705 * @returns #DBUS_VALID if valid, reason why invalid otherwise
708 _dbus_validate_body_with_reason (const DBusString *expected_signature,
709 int expected_signature_start,
711 int *bytes_remaining,
712 const DBusString *value_str,
716 DBusTypeReader reader;
717 const unsigned char *p;
718 const unsigned char *end;
719 DBusValidity validity;
721 _dbus_assert (len >= 0);
722 _dbus_assert (value_pos >= 0);
723 _dbus_assert (value_pos <= _dbus_string_get_length (value_str) - len);
725 _dbus_verbose ("validating body from pos %d len %d sig '%s'\n",
726 value_pos, len, _dbus_string_get_const_data_len (expected_signature,
727 expected_signature_start,
730 _dbus_type_reader_init_types_only (&reader,
731 expected_signature, expected_signature_start);
733 p = _dbus_string_get_const_udata_len (value_str, value_pos, len);
736 validity = validate_body_helper (&reader, byte_order, TRUE, 0, p, end, &p);
737 if (validity != DBUS_VALID)
742 *bytes_remaining = end - p;
746 return DBUS_INVALID_TOO_MUCH_DATA;
749 _dbus_assert (p == end);
755 * Determine wether the given character is valid as the first character
758 #define VALID_INITIAL_NAME_CHARACTER(c) \
759 ( ((c) >= 'A' && (c) <= 'Z') || \
760 ((c) >= 'a' && (c) <= 'z') || \
764 * Determine wether the given character is valid as a second or later
765 * character in a name
767 #define VALID_NAME_CHARACTER(c) \
768 ( ((c) >= '0' && (c) <= '9') || \
769 ((c) >= 'A' && (c) <= 'Z') || \
770 ((c) >= 'a' && (c) <= 'z') || \
774 * Checks that the given range of the string is a valid object path
775 * name in the D-Bus protocol. Part of the validation ensures that
776 * the object path contains only ASCII.
778 * @todo this is inconsistent with most of DBusString in that
779 * it allows a start,len range that extends past the string end.
781 * @todo change spec to disallow more things, such as spaces in the
784 * @param str the string
785 * @param start first byte index to check
786 * @param len number of bytes to check
787 * @returns #TRUE if the byte range exists and is a valid name
790 _dbus_validate_path (const DBusString *str,
794 const unsigned char *s;
795 const unsigned char *end;
796 const unsigned char *last_slash;
798 _dbus_assert (start >= 0);
799 _dbus_assert (len >= 0);
800 _dbus_assert (start <= _dbus_string_get_length (str));
802 if (len > _dbus_string_get_length (str) - start)
808 s = _dbus_string_get_const_udata (str) + start;
820 if ((s - last_slash) < 2)
821 return FALSE; /* no empty path components allowed */
827 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
834 if ((end - last_slash) < 2 &&
836 return FALSE; /* trailing slash not allowed unless the string is "/" */
842 _dbus_validity_to_error_message (DBusValidity validity)
846 case DBUS_VALIDITY_UNKNOWN_OOM_ERROR: return "Out of memory";
847 case DBUS_INVALID_FOR_UNKNOWN_REASON: return "Unknown reason";
848 case DBUS_VALID_BUT_INCOMPLETE: return "Valid but incomplete";
849 case DBUS_VALIDITY_UNKNOWN: return "Validity unknown";
850 case DBUS_VALID: return "Valid";
851 case DBUS_INVALID_UNKNOWN_TYPECODE: return "Unknown typecode";
852 case DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE: return "Missing array element type";
853 case DBUS_INVALID_SIGNATURE_TOO_LONG: return "Signature is too long";
854 case DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION: return "Exceeded maximum array recursion";
855 case DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION: return "Exceeded maximum struct recursion";
856 case DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED: return "Struct ended but not started";
857 case DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED: return "Struct started but not ended";
858 case DBUS_INVALID_STRUCT_HAS_NO_FIELDS: return "Struct has no fields";
859 case DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL: return "Alignment padding not null";
860 case DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE: return "Boolean is not zero or one";
861 case DBUS_INVALID_NOT_ENOUGH_DATA: return "Not enough data";
862 case DBUS_INVALID_TOO_MUCH_DATA: return "Too much data";
863 case DBUS_INVALID_BAD_BYTE_ORDER: return "Bad byte order";
864 case DBUS_INVALID_BAD_PROTOCOL_VERSION: return "Bad protocol version";
865 case DBUS_INVALID_BAD_MESSAGE_TYPE: return "Bad message type";
866 case DBUS_INVALID_BAD_SERIAL: return "Bad serial";
867 case DBUS_INVALID_INSANE_FIELDS_ARRAY_LENGTH: return "Insane fields array length";
868 case DBUS_INVALID_INSANE_BODY_LENGTH: return "Insane body length";
869 case DBUS_INVALID_MESSAGE_TOO_LONG: return "Message too long";
870 case DBUS_INVALID_HEADER_FIELD_CODE: return "Header field code";
871 case DBUS_INVALID_HEADER_FIELD_HAS_WRONG_TYPE: return "Header field has wrong type";
872 case DBUS_INVALID_USES_LOCAL_INTERFACE: return "Uses local interface";
873 case DBUS_INVALID_USES_LOCAL_PATH: return "Uses local path";
874 case DBUS_INVALID_HEADER_FIELD_APPEARS_TWICE: return "Header field appears twice";
875 case DBUS_INVALID_BAD_DESTINATION: return "Bad destination";
876 case DBUS_INVALID_BAD_INTERFACE: return "Bad interface";
877 case DBUS_INVALID_BAD_MEMBER: return "Bad member";
878 case DBUS_INVALID_BAD_ERROR_NAME: return "Bad error name";
879 case DBUS_INVALID_BAD_SENDER: return "Bad sender";
880 case DBUS_INVALID_MISSING_PATH: return "Missing path";
881 case DBUS_INVALID_MISSING_INTERFACE: return "Missing interface";
882 case DBUS_INVALID_MISSING_MEMBER: return "Missing member";
883 case DBUS_INVALID_MISSING_ERROR_NAME: return "Missing error name";
884 case DBUS_INVALID_MISSING_REPLY_SERIAL: return "Missing reply serial";
885 case DBUS_INVALID_LENGTH_OUT_OF_BOUNDS: return "Length out of bounds";
886 case DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM: return "Array length exceeds maximum";
887 case DBUS_INVALID_BAD_PATH: return "Bad path";
888 case DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS: return "Signature length out of bounds";
889 case DBUS_INVALID_BAD_UTF8_IN_STRING: return "Bad utf8 in string";
890 case DBUS_INVALID_ARRAY_LENGTH_INCORRECT: return "Array length incorrect";
891 case DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS: return "Variant signature length out of bounds";
892 case DBUS_INVALID_VARIANT_SIGNATURE_BAD: return "Variant signature bad";
893 case DBUS_INVALID_VARIANT_SIGNATURE_EMPTY: return "Variant signature empty";
894 case DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES: return "Variant signature specifies multiple values";
895 case DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL: return "Variant signature missing nul";
896 case DBUS_INVALID_STRING_MISSING_NUL: return "String missing nul";
897 case DBUS_INVALID_SIGNATURE_MISSING_NUL: return "Signature missing nul";
898 case DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION: return "Exceeded maximum dict entry recursion";
899 case DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED: return "Dict entry ended but not started";
900 case DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED: return "Dict entry started but not ended";
901 case DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS: return "Dict entry has no fields";
902 case DBUS_INVALID_DICT_ENTRY_HAS_ONLY_ONE_FIELD: return "Dict entry has only one field";
903 case DBUS_INVALID_DICT_ENTRY_HAS_TOO_MANY_FIELDS: return "Dict entry has too many fields";
904 case DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY: return "Dict entry not inside array";
905 case DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE: return "Dict key must be basic type";
906 case DBUS_INVALID_MISSING_UNIX_FDS: return "Unix file descriptor missing";
907 case DBUS_INVALID_NESTED_TOO_DEEPLY: return "Variants cannot be used to create a hugely recursive tree of values";
908 case DBUS_VALIDITY_LAST:
915 * Checks that the given range of the string is a valid interface name
916 * in the D-Bus protocol. This includes a length restriction and an
917 * ASCII subset, see the specification.
919 * @todo this is inconsistent with most of DBusString in that
920 * it allows a start,len range that extends past the string end.
922 * @param str the string
923 * @param start first byte index to check
924 * @param len number of bytes to check
925 * @returns #TRUE if the byte range exists and is a valid name
928 _dbus_validate_interface (const DBusString *str,
932 const unsigned char *s;
933 const unsigned char *end;
934 const unsigned char *iface;
935 const unsigned char *last_dot;
937 _dbus_assert (start >= 0);
938 _dbus_assert (len >= 0);
939 _dbus_assert (start <= _dbus_string_get_length (str));
941 if (len > _dbus_string_get_length (str) - start)
944 if (len > DBUS_MAXIMUM_NAME_LENGTH)
951 iface = _dbus_string_get_const_udata (str) + start;
955 /* check special cases of first char so it doesn't have to be done
956 * in the loop. Note we know len > 0
958 if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
960 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
969 if (_DBUS_UNLIKELY ((s + 1) == end))
971 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
974 ++s; /* we just validated the next char, so skip two */
976 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
984 if (_DBUS_UNLIKELY (last_dot == NULL))
991 * Checks that the given range of the string is a valid member name
992 * in the D-Bus protocol. This includes a length restriction, etc.,
993 * see the specification.
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_member (const DBusString *str,
1008 const unsigned char *s;
1009 const unsigned char *end;
1010 const unsigned char *member;
1012 _dbus_assert (start >= 0);
1013 _dbus_assert (len >= 0);
1014 _dbus_assert (start <= _dbus_string_get_length (str));
1016 if (len > _dbus_string_get_length (str) - start)
1019 if (len > DBUS_MAXIMUM_NAME_LENGTH)
1025 member = _dbus_string_get_const_udata (str) + start;
1029 /* check special cases of first char so it doesn't have to be done
1030 * in the loop. Note we know len > 0
1033 if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
1040 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
1052 * Checks that the given range of the string is a valid error name
1053 * in the D-Bus protocol. This includes a length restriction, etc.,
1054 * see the specification.
1056 * @todo this is inconsistent with most of DBusString in that
1057 * it allows a start,len range that extends past the string end.
1059 * @param str the string
1060 * @param start first byte index to check
1061 * @param len number of bytes to check
1062 * @returns #TRUE if the byte range exists and is a valid name
1065 _dbus_validate_error_name (const DBusString *str,
1069 /* Same restrictions as interface name at the moment */
1070 return _dbus_validate_interface (str, start, len);
1074 * Determine wether the given character is valid as the first character
1077 #define VALID_INITIAL_BUS_NAME_CHARACTER(c) \
1078 ( ((c) >= 'A' && (c) <= 'Z') || \
1079 ((c) >= 'a' && (c) <= 'z') || \
1080 ((c) == '_') || ((c) == '-'))
1083 * Determine wether the given character is valid as a second or later
1084 * character in a bus name
1086 #define VALID_BUS_NAME_CHARACTER(c) \
1087 ( ((c) >= '0' && (c) <= '9') || \
1088 ((c) >= 'A' && (c) <= 'Z') || \
1089 ((c) >= 'a' && (c) <= 'z') || \
1090 ((c) == '_') || ((c) == '-'))
1093 _dbus_validate_bus_name_full (const DBusString *str,
1096 dbus_bool_t is_namespace)
1098 const unsigned char *s;
1099 const unsigned char *end;
1100 const unsigned char *iface;
1101 const unsigned char *last_dot;
1103 _dbus_assert (start >= 0);
1104 _dbus_assert (len >= 0);
1105 _dbus_assert (start <= _dbus_string_get_length (str));
1107 if (len > _dbus_string_get_length (str) - start)
1110 if (len > DBUS_MAXIMUM_NAME_LENGTH)
1117 iface = _dbus_string_get_const_udata (str) + start;
1121 /* check special cases of first char so it doesn't have to be done
1122 * in the loop. Note we know len > 0
1132 if (_DBUS_UNLIKELY ((s + 1) == end))
1134 if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*(s + 1))))
1136 ++s; /* we just validated the next char, so skip two */
1138 else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1148 else if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
1150 else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*s)))
1159 if (_DBUS_UNLIKELY ((s + 1) == end))
1161 else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*(s + 1))))
1164 ++s; /* we just validated the next char, so skip two */
1166 else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1174 if (!is_namespace && _DBUS_UNLIKELY (last_dot == NULL))
1181 * Checks that the given range of the string is a valid bus name in
1182 * the D-Bus protocol. This includes a length restriction, etc., see
1183 * the specification.
1185 * @todo this is inconsistent with most of DBusString in that
1186 * it allows a start,len range that extends past the string end.
1188 * @param str the string
1189 * @param start first byte index to check
1190 * @param len number of bytes to check
1191 * @returns #TRUE if the byte range exists and is a valid name
1194 _dbus_validate_bus_name (const DBusString *str,
1198 return _dbus_validate_bus_name_full (str, start, len, FALSE);
1202 * Checks that the given range of the string is a prefix of a valid bus name in
1203 * the D-Bus protocol. Unlike _dbus_validate_bus_name(), this accepts strings
1204 * with only one period-separated component.
1206 * @todo this is inconsistent with most of DBusString in that
1207 * it allows a start,len range that extends past the string end.
1209 * @param str the string
1210 * @param start first byte index to check
1211 * @param len number of bytes to check
1212 * @returns #TRUE if the byte range exists and is a valid name
1215 _dbus_validate_bus_namespace (const DBusString *str,
1219 return _dbus_validate_bus_name_full (str, start, len, TRUE);
1222 /** define _dbus_check_is_valid_path() */
1223 DEFINE_DBUS_NAME_CHECK(path)
1224 /** define _dbus_check_is_valid_interface() */
1225 DEFINE_DBUS_NAME_CHECK(interface)
1226 /** define _dbus_check_is_valid_member() */
1227 DEFINE_DBUS_NAME_CHECK(member)
1228 /** define _dbus_check_is_valid_error_name() */
1229 DEFINE_DBUS_NAME_CHECK(error_name)
1230 /** define _dbus_check_is_valid_bus_name() */
1231 DEFINE_DBUS_NAME_CHECK(bus_name)
1232 /** define _dbus_check_is_valid_utf8() */
1233 DEFINE_DBUS_NAME_CHECK(utf8)
1237 /* tests in dbus-marshal-validate-util.c */