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_data_len (type_str, type_pos, 0);
88 end = _dbus_string_get_const_data_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)
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)
361 dbus_uint32_t v = _dbus_unpack_uint32 (byte_order,
363 if (!(v == 0 || v == 1))
364 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
370 case DBUS_TYPE_ARRAY:
371 case DBUS_TYPE_STRING:
372 case DBUS_TYPE_OBJECT_PATH:
374 dbus_uint32_t claimed_len;
376 a = _DBUS_ALIGN_ADDRESS (p, 4);
378 return DBUS_INVALID_NOT_ENOUGH_DATA;
382 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
386 claimed_len = _dbus_unpack_uint32 (byte_order, p);
389 /* p may now be == end */
390 _dbus_assert (p <= end);
392 if (current_type == DBUS_TYPE_ARRAY)
394 int array_elem_type = _dbus_type_reader_get_element_type (reader);
396 if (!dbus_type_is_valid (array_elem_type))
398 return DBUS_INVALID_UNKNOWN_TYPECODE;
401 alignment = _dbus_type_get_alignment (array_elem_type);
403 a = _DBUS_ALIGN_ADDRESS (p, alignment);
405 /* a may now be == end */
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;
420 if (current_type == DBUS_TYPE_OBJECT_PATH)
423 _dbus_string_init_const_len (&str, p, claimed_len);
424 if (!_dbus_validate_path (&str, 0,
425 _dbus_string_get_length (&str)))
426 return DBUS_INVALID_BAD_PATH;
430 else if (current_type == DBUS_TYPE_STRING)
433 _dbus_string_init_const_len (&str, p, claimed_len);
434 if (!_dbus_string_validate_utf8 (&str, 0,
435 _dbus_string_get_length (&str)))
436 return DBUS_INVALID_BAD_UTF8_IN_STRING;
440 else if (current_type == DBUS_TYPE_ARRAY && claimed_len > 0)
443 DBusValidity validity;
444 const unsigned char *array_end;
447 if (claimed_len > DBUS_MAXIMUM_ARRAY_LENGTH)
448 return DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM;
450 /* Remember that the reader is types only, so we can't
451 * use it to iterate over elements. It stays the same
454 _dbus_type_reader_recurse (reader, &sub);
456 array_end = p + claimed_len;
458 array_elem_type = _dbus_type_reader_get_element_type (reader);
460 /* avoid recursive call to validate_body_helper if this is an array
461 * of fixed-size elements
463 if (dbus_type_is_fixed (array_elem_type))
465 /* bools need to be handled differently, because they can
466 * have an invalid value
468 if (array_elem_type == DBUS_TYPE_BOOLEAN)
471 alignment = _dbus_type_get_alignment (array_elem_type);
473 while (p < array_end)
475 v = _dbus_unpack_uint32 (byte_order, p);
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,
497 if (validity != DBUS_VALID)
503 return DBUS_INVALID_ARRAY_LENGTH_INCORRECT;
506 /* check nul termination */
507 if (current_type != DBUS_TYPE_ARRAY)
510 return DBUS_INVALID_NOT_ENOUGH_DATA;
513 return DBUS_INVALID_STRING_MISSING_NUL;
519 case DBUS_TYPE_SIGNATURE:
521 dbus_uint32_t claimed_len;
523 DBusValidity validity;
528 /* 1 is for nul termination */
529 if (claimed_len + 1 > (unsigned long) (end - p))
530 return DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
532 _dbus_string_init_const_len (&str, p, claimed_len);
534 _dbus_validate_signature_with_reason (&str, 0,
535 _dbus_string_get_length (&str));
537 if (validity != DBUS_VALID)
542 _dbus_assert (p < end);
543 if (*p != DBUS_TYPE_INVALID)
544 return DBUS_INVALID_SIGNATURE_MISSING_NUL;
548 _dbus_verbose ("p = %p end = %p claimed_len %u\n", p, end, claimed_len);
552 case DBUS_TYPE_VARIANT:
554 /* 1 byte sig len, sig typecodes, align to
555 * contained-type-boundary, values.
558 /* In addition to normal signature validation, we need to be sure
559 * the signature contains only a single (possibly container) type.
561 dbus_uint32_t claimed_len;
564 DBusValidity validity;
565 int contained_alignment;
573 if (claimed_len + 1 > (unsigned long) (end - p))
574 return DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
576 _dbus_string_init_const_len (&sig, p, claimed_len);
577 reason = _dbus_validate_signature_with_reason (&sig, 0,
578 _dbus_string_get_length (&sig));
579 if (!(reason == DBUS_VALID))
581 if (reason == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
584 return DBUS_INVALID_VARIANT_SIGNATURE_BAD;
589 if (*p != DBUS_TYPE_INVALID)
590 return DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL;
593 contained_type = _dbus_first_type_in_signature (&sig, 0);
594 if (contained_type == DBUS_TYPE_INVALID)
595 return DBUS_INVALID_VARIANT_SIGNATURE_EMPTY;
597 contained_alignment = _dbus_type_get_alignment (contained_type);
599 a = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
601 return DBUS_INVALID_NOT_ENOUGH_DATA;
605 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
609 _dbus_type_reader_init_types_only (&sub, &sig, 0);
611 _dbus_assert (_dbus_type_reader_get_current_type (&sub) != DBUS_TYPE_INVALID);
613 validity = validate_body_helper (&sub, byte_order, FALSE,
616 if (validity != DBUS_VALID)
619 if (_dbus_type_reader_next (&sub))
620 return DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES;
622 _dbus_assert (_dbus_type_reader_get_current_type (&sub) == DBUS_TYPE_INVALID);
626 case DBUS_TYPE_DICT_ENTRY:
627 case DBUS_TYPE_STRUCT:
630 DBusValidity validity;
632 a = _DBUS_ALIGN_ADDRESS (p, 8);
634 return DBUS_INVALID_NOT_ENOUGH_DATA;
638 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
642 _dbus_type_reader_recurse (reader, &sub);
644 validity = validate_body_helper (&sub, byte_order, TRUE,
647 if (validity != DBUS_VALID)
653 _dbus_assert_not_reached ("invalid typecode in supposedly-validated signature");
658 _dbus_verbose (" validated value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
659 _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
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)
671 _dbus_type_reader_next (reader);
683 * Verifies that the range of value_str from value_pos to value_end is
684 * a legitimate value of type expected_signature. If this function
685 * returns #TRUE, it will be safe to iterate over the values with
686 * #DBusTypeReader. The signature is assumed to be already valid.
688 * If bytes_remaining is not #NULL, then leftover bytes will be stored
689 * there and #DBUS_VALID returned. If it is #NULL, then
690 * #DBUS_INVALID_TOO_MUCH_DATA will be returned if bytes are left
693 * @param expected_signature the expected types in the value_str
694 * @param expected_signature_start where in expected_signature is the signature
695 * @param byte_order the byte order
696 * @param bytes_remaining place to store leftover bytes
697 * @param value_str the string containing the body
698 * @param value_pos where the values start
699 * @param len length of values after value_pos
700 * @returns #DBUS_VALID if valid, reason why invalid otherwise
703 _dbus_validate_body_with_reason (const DBusString *expected_signature,
704 int expected_signature_start,
706 int *bytes_remaining,
707 const DBusString *value_str,
711 DBusTypeReader reader;
712 const unsigned char *p;
713 const unsigned char *end;
714 DBusValidity validity;
716 _dbus_assert (len >= 0);
717 _dbus_assert (value_pos >= 0);
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,
725 _dbus_type_reader_init_types_only (&reader,
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);
732 if (validity != DBUS_VALID)
737 *bytes_remaining = end - p;
741 return DBUS_INVALID_TOO_MUCH_DATA;
744 _dbus_assert (p == end);
750 * Determine wether the given character is valid as the first character
753 #define VALID_INITIAL_NAME_CHARACTER(c) \
754 ( ((c) >= 'A' && (c) <= 'Z') || \
755 ((c) >= 'a' && (c) <= 'z') || \
759 * Determine wether the given character is valid as a second or later
760 * character in a name
762 #define VALID_NAME_CHARACTER(c) \
763 ( ((c) >= '0' && (c) <= '9') || \
764 ((c) >= 'A' && (c) <= 'Z') || \
765 ((c) >= 'a' && (c) <= 'z') || \
769 * Checks that the given range of the string is a valid object path
770 * name in the D-Bus protocol. Part of the validation ensures that
771 * the object path contains only ASCII.
773 * @todo this is inconsistent with most of DBusString in that
774 * it allows a start,len range that extends past the string end.
776 * @todo change spec to disallow more things, such as spaces in the
779 * @param str the string
780 * @param start first byte index to check
781 * @param len number of bytes to check
782 * @returns #TRUE if the byte range exists and is a valid name
785 _dbus_validate_path (const DBusString *str,
789 const unsigned char *s;
790 const unsigned char *end;
791 const unsigned char *last_slash;
793 _dbus_assert (start >= 0);
794 _dbus_assert (len >= 0);
795 _dbus_assert (start <= _dbus_string_get_length (str));
797 if (len > _dbus_string_get_length (str) - start)
803 s = _dbus_string_get_const_data (str) + start;
815 if ((s - last_slash) < 2)
816 return FALSE; /* no empty path components allowed */
822 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
829 if ((end - last_slash) < 2 &&
831 return FALSE; /* trailing slash not allowed unless the string is "/" */
837 _dbus_validity_to_error_message (DBusValidity validity)
841 case DBUS_VALIDITY_UNKNOWN_OOM_ERROR: return "Out of memory";
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";
845 case DBUS_VALID: return "Valid";
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";
857 case DBUS_INVALID_TOO_MUCH_DATA: return "Too much 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";
908 * Checks that the given range of the string is a valid interface name
909 * in the D-Bus protocol. This includes a length restriction and an
910 * ASCII subset, see the specification.
912 * @todo this is inconsistent with most of DBusString in that
913 * it allows a start,len range that extends past the string end.
915 * @param str the string
916 * @param start first byte index to check
917 * @param len number of bytes to check
918 * @returns #TRUE if the byte range exists and is a valid name
921 _dbus_validate_interface (const DBusString *str,
925 const unsigned char *s;
926 const unsigned char *end;
927 const unsigned char *iface;
928 const unsigned char *last_dot;
930 _dbus_assert (start >= 0);
931 _dbus_assert (len >= 0);
932 _dbus_assert (start <= _dbus_string_get_length (str));
934 if (len > _dbus_string_get_length (str) - start)
937 if (len > DBUS_MAXIMUM_NAME_LENGTH)
944 iface = _dbus_string_get_const_data (str) + start;
948 /* check special cases of first char so it doesn't have to be done
949 * in the loop. Note we know len > 0
951 if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
953 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
962 if (_DBUS_UNLIKELY ((s + 1) == end))
964 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
967 ++s; /* we just validated the next char, so skip two */
969 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
977 if (_DBUS_UNLIKELY (last_dot == NULL))
984 * Checks that the given range of the string is a valid member name
985 * in the D-Bus protocol. This includes a length restriction, etc.,
986 * see the specification.
988 * @todo this is inconsistent with most of DBusString in that
989 * it allows a start,len range that extends past the string end.
991 * @param str the string
992 * @param start first byte index to check
993 * @param len number of bytes to check
994 * @returns #TRUE if the byte range exists and is a valid name
997 _dbus_validate_member (const DBusString *str,
1001 const unsigned char *s;
1002 const unsigned char *end;
1003 const unsigned char *member;
1005 _dbus_assert (start >= 0);
1006 _dbus_assert (len >= 0);
1007 _dbus_assert (start <= _dbus_string_get_length (str));
1009 if (len > _dbus_string_get_length (str) - start)
1012 if (len > DBUS_MAXIMUM_NAME_LENGTH)
1018 member = _dbus_string_get_const_data (str) + start;
1022 /* check special cases of first char so it doesn't have to be done
1023 * in the loop. Note we know len > 0
1026 if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
1033 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
1045 * Checks that the given range of the string is a valid error name
1046 * in the D-Bus protocol. This includes a length restriction, etc.,
1047 * see the specification.
1049 * @todo this is inconsistent with most of DBusString in that
1050 * it allows a start,len range that extends past the string end.
1052 * @param str the string
1053 * @param start first byte index to check
1054 * @param len number of bytes to check
1055 * @returns #TRUE if the byte range exists and is a valid name
1058 _dbus_validate_error_name (const DBusString *str,
1062 /* Same restrictions as interface name at the moment */
1063 return _dbus_validate_interface (str, start, len);
1067 * Determine wether the given character is valid as the first character
1070 #define VALID_INITIAL_BUS_NAME_CHARACTER(c) \
1071 ( ((c) >= 'A' && (c) <= 'Z') || \
1072 ((c) >= 'a' && (c) <= 'z') || \
1073 ((c) == '_') || ((c) == '-'))
1076 * Determine wether the given character is valid as a second or later
1077 * character in a bus name
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,
1089 dbus_bool_t is_namespace)
1091 const unsigned char *s;
1092 const unsigned char *end;
1093 const unsigned char *iface;
1094 const unsigned char *last_dot;
1096 _dbus_assert (start >= 0);
1097 _dbus_assert (len >= 0);
1098 _dbus_assert (start <= _dbus_string_get_length (str));
1100 if (len > _dbus_string_get_length (str) - start)
1103 if (len > DBUS_MAXIMUM_NAME_LENGTH)
1110 iface = _dbus_string_get_const_data (str) + start;
1114 /* check special cases of first char so it doesn't have to be done
1115 * in the loop. Note we know len > 0
1125 if (_DBUS_UNLIKELY ((s + 1) == end))
1127 if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*(s + 1))))
1129 ++s; /* we just validated the next char, so skip two */
1131 else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1141 else if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
1143 else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*s)))
1152 if (_DBUS_UNLIKELY ((s + 1) == end))
1154 else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*(s + 1))))
1157 ++s; /* we just validated the next char, so skip two */
1159 else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1167 if (!is_namespace && _DBUS_UNLIKELY (last_dot == NULL))
1174 * Checks that the given range of the string is a valid bus name in
1175 * the D-Bus protocol. This includes a length restriction, etc., see
1176 * the specification.
1178 * @todo this is inconsistent with most of DBusString in that
1179 * it allows a start,len range that extends past the string end.
1181 * @param str the string
1182 * @param start first byte index to check
1183 * @param len number of bytes to check
1184 * @returns #TRUE if the byte range exists and is a valid name
1187 _dbus_validate_bus_name (const DBusString *str,
1191 return _dbus_validate_bus_name_full (str, start, len, FALSE);
1195 * Checks that the given range of the string is a prefix of a valid bus name in
1196 * the D-Bus protocol. Unlike _dbus_validate_bus_name(), this accepts strings
1197 * with only one period-separated component.
1199 * @todo this is inconsistent with most of DBusString in that
1200 * it allows a start,len range that extends past the string end.
1202 * @param str the string
1203 * @param start first byte index to check
1204 * @param len number of bytes to check
1205 * @returns #TRUE if the byte range exists and is a valid name
1208 _dbus_validate_bus_namespace (const DBusString *str,
1212 return _dbus_validate_bus_name_full (str, start, len, TRUE);
1216 * Checks that the given range of the string is a valid message type
1217 * signature in the D-Bus protocol.
1219 * @todo this is inconsistent with most of DBusString in that
1220 * it allows a start,len range that extends past the string end.
1222 * @param str the string
1223 * @param start first byte index to check
1224 * @param len number of bytes to check
1225 * @returns #TRUE if the byte range exists and is a valid signature
1228 _dbus_validate_signature (const DBusString *str,
1232 _dbus_assert (start >= 0);
1233 _dbus_assert (start <= _dbus_string_get_length (str));
1234 _dbus_assert (len >= 0);
1236 if (len > _dbus_string_get_length (str) - start)
1239 return _dbus_validate_signature_with_reason (str, start, len) == DBUS_VALID;
1242 /** define _dbus_check_is_valid_path() */
1243 DEFINE_DBUS_NAME_CHECK(path)
1244 /** define _dbus_check_is_valid_interface() */
1245 DEFINE_DBUS_NAME_CHECK(interface)
1246 /** define _dbus_check_is_valid_member() */
1247 DEFINE_DBUS_NAME_CHECK(member)
1248 /** define _dbus_check_is_valid_error_name() */
1249 DEFINE_DBUS_NAME_CHECK(error_name)
1250 /** define _dbus_check_is_valid_bus_name() */
1251 DEFINE_DBUS_NAME_CHECK(bus_name)
1252 /** define _dbus_check_is_valid_signature() */
1253 DEFINE_DBUS_NAME_CHECK(signature)
1254 /** define _dbus_check_is_valid_utf8() */
1255 DEFINE_DBUS_NAME_CHECK(utf8)
1259 /* tests in dbus-marshal-validate-util.c */