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;
65 char opened_brackets[DBUS_MAXIMUM_TYPE_RECURSION_DEPTH * 2 + 1] = { '\0' };
69 element_count_stack = NULL;
71 if (!_dbus_list_append (&element_count_stack, _DBUS_INT_TO_POINTER (0)))
73 result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
77 _dbus_assert (type_str != NULL);
78 _dbus_assert (type_pos < _DBUS_INT32_MAX - len);
79 _dbus_assert (len >= 0);
80 _dbus_assert (type_pos >= 0);
82 if (len > DBUS_MAXIMUM_SIGNATURE_LENGTH)
84 result = DBUS_INVALID_SIGNATURE_TOO_LONG;
88 p = _dbus_string_get_const_udata_len (type_str, type_pos, 0);
90 end = _dbus_string_get_const_udata_len (type_str, type_pos + len, 0);
94 last = DBUS_TYPE_INVALID;
98 _dbus_assert (struct_depth + dict_entry_depth >= 0);
99 _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
100 _dbus_assert (opened_brackets[struct_depth + dict_entry_depth] == '\0');
105 case DBUS_TYPE_BOOLEAN:
106 case DBUS_TYPE_INT16:
107 case DBUS_TYPE_UINT16:
108 case DBUS_TYPE_INT32:
109 case DBUS_TYPE_UINT32:
110 case DBUS_TYPE_UNIX_FD:
111 case DBUS_TYPE_INT64:
112 case DBUS_TYPE_UINT64:
113 case DBUS_TYPE_DOUBLE:
114 case DBUS_TYPE_STRING:
115 case DBUS_TYPE_OBJECT_PATH:
116 case DBUS_TYPE_SIGNATURE:
117 case DBUS_TYPE_VARIANT:
120 case DBUS_TYPE_ARRAY:
122 if (array_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
124 result = DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION;
129 case DBUS_STRUCT_BEGIN_CHAR:
132 if (struct_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
134 result = DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION;
138 if (!_dbus_list_append (&element_count_stack,
139 _DBUS_INT_TO_POINTER (0)))
141 result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
145 _dbus_assert (struct_depth + dict_entry_depth >= 1);
146 _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
147 _dbus_assert (opened_brackets[struct_depth + dict_entry_depth - 1] == '\0');
148 opened_brackets[struct_depth + dict_entry_depth - 1] = DBUS_STRUCT_BEGIN_CHAR;
151 case DBUS_STRUCT_END_CHAR:
152 if (struct_depth == 0)
154 result = DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED;
158 if (last == DBUS_STRUCT_BEGIN_CHAR)
160 result = DBUS_INVALID_STRUCT_HAS_NO_FIELDS;
164 _dbus_assert (struct_depth + dict_entry_depth >= 1);
165 _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
166 last_bracket = opened_brackets[struct_depth + dict_entry_depth - 1];
168 if (last_bracket != DBUS_STRUCT_BEGIN_CHAR)
170 result = DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED;
174 _dbus_list_pop_last (&element_count_stack);
177 opened_brackets[struct_depth + dict_entry_depth] = '\0';
180 case DBUS_DICT_ENTRY_BEGIN_CHAR:
181 if (last != DBUS_TYPE_ARRAY)
183 result = DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY;
187 dict_entry_depth += 1;
189 if (dict_entry_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
191 result = DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION;
195 if (!_dbus_list_append (&element_count_stack,
196 _DBUS_INT_TO_POINTER (0)))
198 result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
202 _dbus_assert (struct_depth + dict_entry_depth >= 1);
203 _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
204 _dbus_assert (opened_brackets[struct_depth + dict_entry_depth - 1] == '\0');
205 opened_brackets[struct_depth + dict_entry_depth - 1] = DBUS_DICT_ENTRY_BEGIN_CHAR;
208 case DBUS_DICT_ENTRY_END_CHAR:
209 if (dict_entry_depth == 0)
211 result = DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED;
215 _dbus_assert (struct_depth + dict_entry_depth >= 1);
216 _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
217 last_bracket = opened_brackets[struct_depth + dict_entry_depth - 1];
219 if (last_bracket != DBUS_DICT_ENTRY_BEGIN_CHAR)
221 result = DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED;
225 dict_entry_depth -= 1;
226 opened_brackets[struct_depth + dict_entry_depth] = '\0';
229 _DBUS_POINTER_TO_INT (_dbus_list_pop_last (&element_count_stack));
231 if (element_count != 2)
233 if (element_count == 0)
234 result = DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS;
235 else if (element_count == 1)
236 result = DBUS_INVALID_DICT_ENTRY_HAS_ONLY_ONE_FIELD;
238 result = DBUS_INVALID_DICT_ENTRY_HAS_TOO_MANY_FIELDS;
244 case DBUS_TYPE_STRUCT: /* doesn't appear in signatures */
245 case DBUS_TYPE_DICT_ENTRY: /* ditto */
247 result = DBUS_INVALID_UNKNOWN_TYPECODE;
251 if (*p != DBUS_TYPE_ARRAY &&
252 *p != DBUS_DICT_ENTRY_BEGIN_CHAR &&
253 *p != DBUS_STRUCT_BEGIN_CHAR)
256 _DBUS_POINTER_TO_INT (_dbus_list_pop_last (&element_count_stack));
260 if (!_dbus_list_append (&element_count_stack,
261 _DBUS_INT_TO_POINTER (element_count)))
263 result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
270 if (*p == DBUS_TYPE_ARRAY && p != end)
272 const unsigned char *p1;
274 if (*p1 == DBUS_STRUCT_END_CHAR ||
275 *p1 == DBUS_DICT_ENTRY_END_CHAR)
277 result = DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
287 if (last == DBUS_DICT_ENTRY_BEGIN_CHAR)
289 if (!(dbus_type_is_valid (*p) && dbus_type_is_basic (*p)))
291 result = DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE;
303 result = DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
307 if (struct_depth > 0)
309 result = DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED;
313 if (dict_entry_depth > 0)
315 result = DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED;
319 _dbus_assert (last != DBUS_TYPE_ARRAY);
320 _dbus_assert (last != DBUS_STRUCT_BEGIN_CHAR);
321 _dbus_assert (last != DBUS_DICT_ENTRY_BEGIN_CHAR);
326 _dbus_list_clear (&element_count_stack);
330 /* note: this function is also used to validate the header's values,
331 * since the header is a valid body with a particular signature.
334 validate_body_helper (DBusTypeReader *reader,
336 dbus_bool_t walk_reader_to_end,
338 const unsigned char *p,
339 const unsigned char *end,
340 const unsigned char **new_p)
344 /* The spec allows arrays and structs to each nest 32, for total
345 * nesting of 2*32. We want to impose the same limit on "dynamic"
346 * value nesting (not visible in the signature) which is introduced
347 * by DBUS_TYPE_VARIANT.
349 if (total_depth > (DBUS_MAXIMUM_TYPE_RECURSION_DEPTH * 2))
351 return DBUS_INVALID_NESTED_TOO_DEEPLY;
354 while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
356 const unsigned char *a;
360 _dbus_verbose (" validating value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
361 _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
365 /* Guarantee that p has one byte to look at */
367 return DBUS_INVALID_NOT_ENOUGH_DATA;
369 switch (current_type)
375 case DBUS_TYPE_BOOLEAN:
376 case DBUS_TYPE_INT16:
377 case DBUS_TYPE_UINT16:
378 case DBUS_TYPE_INT32:
379 case DBUS_TYPE_UINT32:
380 case DBUS_TYPE_UNIX_FD:
381 case DBUS_TYPE_INT64:
382 case DBUS_TYPE_UINT64:
383 case DBUS_TYPE_DOUBLE:
384 alignment = _dbus_type_get_alignment (current_type);
385 a = _DBUS_ALIGN_ADDRESS (p, alignment);
387 return DBUS_INVALID_NOT_ENOUGH_DATA;
391 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
395 if (current_type == DBUS_TYPE_BOOLEAN)
400 return DBUS_INVALID_NOT_ENOUGH_DATA;
402 v = _dbus_unpack_uint32 (byte_order, p);
404 if (!(v == 0 || v == 1))
405 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
411 case DBUS_TYPE_ARRAY:
412 case DBUS_TYPE_STRING:
413 case DBUS_TYPE_OBJECT_PATH:
415 dbus_uint32_t claimed_len;
417 a = _DBUS_ALIGN_ADDRESS (p, 4);
419 return DBUS_INVALID_NOT_ENOUGH_DATA;
423 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
427 claimed_len = _dbus_unpack_uint32 (byte_order, p);
430 /* p may now be == end */
431 _dbus_assert (p <= end);
433 if (current_type == DBUS_TYPE_ARRAY)
435 int array_elem_type = _dbus_type_reader_get_element_type (reader);
437 if (!dbus_type_is_valid (array_elem_type))
439 return DBUS_INVALID_UNKNOWN_TYPECODE;
442 alignment = _dbus_type_get_alignment (array_elem_type);
444 a = _DBUS_ALIGN_ADDRESS (p, alignment);
446 /* a may now be == end */
448 return DBUS_INVALID_NOT_ENOUGH_DATA;
453 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
458 if (claimed_len > (unsigned long) (end - p))
459 return DBUS_INVALID_LENGTH_OUT_OF_BOUNDS;
461 if (current_type == DBUS_TYPE_OBJECT_PATH)
464 _dbus_string_init_const_len (&str, (const char *) p, claimed_len);
465 if (!_dbus_validate_path (&str, 0,
466 _dbus_string_get_length (&str)))
467 return DBUS_INVALID_BAD_PATH;
471 else if (current_type == DBUS_TYPE_STRING)
474 _dbus_string_init_const_len (&str, (const char *) p, claimed_len);
475 if (!_dbus_string_validate_utf8 (&str, 0,
476 _dbus_string_get_length (&str)))
477 return DBUS_INVALID_BAD_UTF8_IN_STRING;
481 else if (current_type == DBUS_TYPE_ARRAY && claimed_len > 0)
484 DBusValidity validity;
485 const unsigned char *array_end;
488 if (claimed_len > DBUS_MAXIMUM_ARRAY_LENGTH)
489 return DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM;
491 /* Remember that the reader is types only, so we can't
492 * use it to iterate over elements. It stays the same
495 _dbus_type_reader_recurse (reader, &sub);
497 array_end = p + claimed_len;
499 array_elem_type = _dbus_type_reader_get_element_type (reader);
501 /* avoid recursive call to validate_body_helper if this is an array
502 * of fixed-size elements
504 if (dbus_type_is_fixed (array_elem_type))
506 /* bools need to be handled differently, because they can
507 * have an invalid value
509 if (array_elem_type == DBUS_TYPE_BOOLEAN)
512 alignment = _dbus_type_get_alignment (array_elem_type);
514 while (p < array_end)
516 v = _dbus_unpack_uint32 (byte_order, p);
518 if (!(v == 0 || v == 1))
519 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
533 while (p < array_end)
535 validity = validate_body_helper (&sub, byte_order, FALSE,
538 if (validity != DBUS_VALID)
544 return DBUS_INVALID_ARRAY_LENGTH_INCORRECT;
547 /* check nul termination */
548 if (current_type != DBUS_TYPE_ARRAY)
551 return DBUS_INVALID_NOT_ENOUGH_DATA;
554 return DBUS_INVALID_STRING_MISSING_NUL;
560 case DBUS_TYPE_SIGNATURE:
562 dbus_uint32_t claimed_len;
564 DBusValidity validity;
569 /* 1 is for nul termination */
570 if (claimed_len + 1 > (unsigned long) (end - p))
571 return DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
573 _dbus_string_init_const_len (&str, (const char *) p, claimed_len);
575 _dbus_validate_signature_with_reason (&str, 0,
576 _dbus_string_get_length (&str));
578 if (validity != DBUS_VALID)
583 _dbus_assert (p < end);
584 if (*p != DBUS_TYPE_INVALID)
585 return DBUS_INVALID_SIGNATURE_MISSING_NUL;
589 _dbus_verbose ("p = %p end = %p claimed_len %u\n", p, end, claimed_len);
593 case DBUS_TYPE_VARIANT:
595 /* 1 byte sig len, sig typecodes, align to
596 * contained-type-boundary, values.
599 /* In addition to normal signature validation, we need to be sure
600 * the signature contains only a single (possibly container) type.
602 dbus_uint32_t claimed_len;
605 DBusValidity validity;
606 int contained_alignment;
614 if (claimed_len + 1 > (unsigned long) (end - p))
615 return DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
617 _dbus_string_init_const_len (&sig, (const char *) p, claimed_len);
618 reason = _dbus_validate_signature_with_reason (&sig, 0,
619 _dbus_string_get_length (&sig));
620 if (!(reason == DBUS_VALID))
622 if (reason == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
625 return DBUS_INVALID_VARIANT_SIGNATURE_BAD;
630 if (*p != DBUS_TYPE_INVALID)
631 return DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL;
634 contained_type = _dbus_first_type_in_signature (&sig, 0);
635 if (contained_type == DBUS_TYPE_INVALID)
636 return DBUS_INVALID_VARIANT_SIGNATURE_EMPTY;
638 contained_alignment = _dbus_type_get_alignment (contained_type);
640 a = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
642 return DBUS_INVALID_NOT_ENOUGH_DATA;
646 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
650 _dbus_type_reader_init_types_only (&sub, &sig, 0);
652 _dbus_assert (_dbus_type_reader_get_current_type (&sub) != DBUS_TYPE_INVALID);
654 validity = validate_body_helper (&sub, byte_order, FALSE,
657 if (validity != DBUS_VALID)
660 if (_dbus_type_reader_next (&sub))
661 return DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES;
663 _dbus_assert (_dbus_type_reader_get_current_type (&sub) == DBUS_TYPE_INVALID);
667 case DBUS_TYPE_DICT_ENTRY:
668 case DBUS_TYPE_STRUCT:
671 DBusValidity validity;
673 a = _DBUS_ALIGN_ADDRESS (p, 8);
675 return DBUS_INVALID_NOT_ENOUGH_DATA;
679 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
683 _dbus_type_reader_recurse (reader, &sub);
685 validity = validate_body_helper (&sub, byte_order, TRUE,
688 if (validity != DBUS_VALID)
694 _dbus_assert_not_reached ("invalid typecode in supposedly-validated signature");
699 _dbus_verbose (" validated value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
700 _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
706 _dbus_verbose ("not enough data!!! p = %p end = %p end-p = %d\n",
707 p, end, (int) (end - p));
708 return DBUS_INVALID_NOT_ENOUGH_DATA;
711 if (walk_reader_to_end)
712 _dbus_type_reader_next (reader);
724 * Verifies that the range of value_str from value_pos to value_end is
725 * a legitimate value of type expected_signature. If this function
726 * returns #TRUE, it will be safe to iterate over the values with
727 * #DBusTypeReader. The signature is assumed to be already valid.
729 * If bytes_remaining is not #NULL, then leftover bytes will be stored
730 * there and #DBUS_VALID returned. If it is #NULL, then
731 * #DBUS_INVALID_TOO_MUCH_DATA will be returned if bytes are left
734 * @param expected_signature the expected types in the value_str
735 * @param expected_signature_start where in expected_signature is the signature
736 * @param byte_order the byte order
737 * @param bytes_remaining place to store leftover bytes
738 * @param value_str the string containing the body
739 * @param value_pos where the values start
740 * @param len length of values after value_pos
741 * @returns #DBUS_VALID if valid, reason why invalid otherwise
744 _dbus_validate_body_with_reason (const DBusString *expected_signature,
745 int expected_signature_start,
747 int *bytes_remaining,
748 const DBusString *value_str,
752 DBusTypeReader reader;
753 const unsigned char *p;
754 const unsigned char *end;
755 DBusValidity validity;
757 _dbus_assert (len >= 0);
758 _dbus_assert (value_pos >= 0);
759 _dbus_assert (value_pos <= _dbus_string_get_length (value_str) - len);
761 _dbus_verbose ("validating body from pos %d len %d sig '%s'\n",
762 value_pos, len, _dbus_string_get_const_data_len (expected_signature,
763 expected_signature_start,
766 _dbus_type_reader_init_types_only (&reader,
767 expected_signature, expected_signature_start);
769 p = _dbus_string_get_const_udata_len (value_str, value_pos, len);
772 validity = validate_body_helper (&reader, byte_order, TRUE, 0, p, end, &p);
773 if (validity != DBUS_VALID)
778 *bytes_remaining = end - p;
782 return DBUS_INVALID_TOO_MUCH_DATA;
785 _dbus_assert (p == end);
791 * Determine wether the given character is valid as the first character
794 #define VALID_INITIAL_NAME_CHARACTER(c) \
795 ( ((c) >= 'A' && (c) <= 'Z') || \
796 ((c) >= 'a' && (c) <= 'z') || \
800 * Determine wether the given character is valid as a second or later
801 * character in a name
803 #define VALID_NAME_CHARACTER(c) \
804 ( ((c) >= '0' && (c) <= '9') || \
805 ((c) >= 'A' && (c) <= 'Z') || \
806 ((c) >= 'a' && (c) <= 'z') || \
810 * Checks that the given range of the string is a valid object path
811 * name in the D-Bus protocol. Part of the validation ensures that
812 * the object path contains only ASCII.
814 * @todo this is inconsistent with most of DBusString in that
815 * it allows a start,len range that extends past the string end.
817 * @todo change spec to disallow more things, such as spaces in the
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_path (const DBusString *str,
830 const unsigned char *s;
831 const unsigned char *end;
832 const unsigned char *last_slash;
834 _dbus_assert (start >= 0);
835 _dbus_assert (len >= 0);
836 _dbus_assert (start <= _dbus_string_get_length (str));
838 if (len > _dbus_string_get_length (str) - start)
844 s = _dbus_string_get_const_udata (str) + start;
856 if ((s - last_slash) < 2)
857 return FALSE; /* no empty path components allowed */
863 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
870 if ((end - last_slash) < 2 &&
872 return FALSE; /* trailing slash not allowed unless the string is "/" */
878 _dbus_validity_to_error_message (DBusValidity validity)
882 case DBUS_VALIDITY_UNKNOWN_OOM_ERROR: return "Out of memory";
883 case DBUS_INVALID_FOR_UNKNOWN_REASON: return "Unknown reason";
884 case DBUS_VALID_BUT_INCOMPLETE: return "Valid but incomplete";
885 case DBUS_VALIDITY_UNKNOWN: return "Validity unknown";
886 case DBUS_VALID: return "Valid";
887 case DBUS_INVALID_UNKNOWN_TYPECODE: return "Unknown typecode";
888 case DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE: return "Missing array element type";
889 case DBUS_INVALID_SIGNATURE_TOO_LONG: return "Signature is too long";
890 case DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION: return "Exceeded maximum array recursion";
891 case DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION: return "Exceeded maximum struct recursion";
892 case DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED: return "Struct ended but not started";
893 case DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED: return "Struct started but not ended";
894 case DBUS_INVALID_STRUCT_HAS_NO_FIELDS: return "Struct has no fields";
895 case DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL: return "Alignment padding not null";
896 case DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE: return "Boolean is not zero or one";
897 case DBUS_INVALID_NOT_ENOUGH_DATA: return "Not enough data";
898 case DBUS_INVALID_TOO_MUCH_DATA: return "Too much data";
899 case DBUS_INVALID_BAD_BYTE_ORDER: return "Bad byte order";
900 case DBUS_INVALID_BAD_PROTOCOL_VERSION: return "Bad protocol version";
901 case DBUS_INVALID_BAD_MESSAGE_TYPE: return "Bad message type";
902 case DBUS_INVALID_BAD_SERIAL: return "Bad serial";
903 case DBUS_INVALID_INSANE_FIELDS_ARRAY_LENGTH: return "Insane fields array length";
904 case DBUS_INVALID_INSANE_BODY_LENGTH: return "Insane body length";
905 case DBUS_INVALID_MESSAGE_TOO_LONG: return "Message too long";
906 case DBUS_INVALID_HEADER_FIELD_CODE: return "Header field code";
907 case DBUS_INVALID_HEADER_FIELD_HAS_WRONG_TYPE: return "Header field has wrong type";
908 case DBUS_INVALID_USES_LOCAL_INTERFACE: return "Uses local interface";
909 case DBUS_INVALID_USES_LOCAL_PATH: return "Uses local path";
910 case DBUS_INVALID_HEADER_FIELD_APPEARS_TWICE: return "Header field appears twice";
911 case DBUS_INVALID_BAD_DESTINATION: return "Bad destination";
912 case DBUS_INVALID_BAD_INTERFACE: return "Bad interface";
913 case DBUS_INVALID_BAD_MEMBER: return "Bad member";
914 case DBUS_INVALID_BAD_ERROR_NAME: return "Bad error name";
915 case DBUS_INVALID_BAD_SENDER: return "Bad sender";
916 case DBUS_INVALID_MISSING_PATH: return "Missing path";
917 case DBUS_INVALID_MISSING_INTERFACE: return "Missing interface";
918 case DBUS_INVALID_MISSING_MEMBER: return "Missing member";
919 case DBUS_INVALID_MISSING_ERROR_NAME: return "Missing error name";
920 case DBUS_INVALID_MISSING_REPLY_SERIAL: return "Missing reply serial";
921 case DBUS_INVALID_LENGTH_OUT_OF_BOUNDS: return "Length out of bounds";
922 case DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM: return "Array length exceeds maximum";
923 case DBUS_INVALID_BAD_PATH: return "Bad path";
924 case DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS: return "Signature length out of bounds";
925 case DBUS_INVALID_BAD_UTF8_IN_STRING: return "Bad utf8 in string";
926 case DBUS_INVALID_ARRAY_LENGTH_INCORRECT: return "Array length incorrect";
927 case DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS: return "Variant signature length out of bounds";
928 case DBUS_INVALID_VARIANT_SIGNATURE_BAD: return "Variant signature bad";
929 case DBUS_INVALID_VARIANT_SIGNATURE_EMPTY: return "Variant signature empty";
930 case DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES: return "Variant signature specifies multiple values";
931 case DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL: return "Variant signature missing nul";
932 case DBUS_INVALID_STRING_MISSING_NUL: return "String missing nul";
933 case DBUS_INVALID_SIGNATURE_MISSING_NUL: return "Signature missing nul";
934 case DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION: return "Exceeded maximum dict entry recursion";
935 case DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED: return "Dict entry ended but not started";
936 case DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED: return "Dict entry started but not ended";
937 case DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS: return "Dict entry has no fields";
938 case DBUS_INVALID_DICT_ENTRY_HAS_ONLY_ONE_FIELD: return "Dict entry has only one field";
939 case DBUS_INVALID_DICT_ENTRY_HAS_TOO_MANY_FIELDS: return "Dict entry has too many fields";
940 case DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY: return "Dict entry not inside array";
941 case DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE: return "Dict key must be basic type";
942 case DBUS_INVALID_MISSING_UNIX_FDS: return "Unix file descriptor missing";
943 case DBUS_INVALID_NESTED_TOO_DEEPLY: return "Variants cannot be used to create a hugely recursive tree of values";
944 case DBUS_VALIDITY_LAST:
951 * Checks that the given range of the string is a valid interface name
952 * in the D-Bus protocol. This includes a length restriction and an
953 * ASCII subset, see the specification.
955 * @todo this is inconsistent with most of DBusString in that
956 * it allows a start,len range that extends past the string end.
958 * @param str the string
959 * @param start first byte index to check
960 * @param len number of bytes to check
961 * @returns #TRUE if the byte range exists and is a valid name
964 _dbus_validate_interface (const DBusString *str,
968 const unsigned char *s;
969 const unsigned char *end;
970 const unsigned char *iface;
971 const unsigned char *last_dot;
973 _dbus_assert (start >= 0);
974 _dbus_assert (len >= 0);
975 _dbus_assert (start <= _dbus_string_get_length (str));
977 if (len > _dbus_string_get_length (str) - start)
980 if (len > DBUS_MAXIMUM_NAME_LENGTH)
987 iface = _dbus_string_get_const_udata (str) + start;
991 /* check special cases of first char so it doesn't have to be done
992 * in the loop. Note we know len > 0
994 if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
996 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
1005 if (_DBUS_UNLIKELY ((s + 1) == end))
1007 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
1010 ++s; /* we just validated the next char, so skip two */
1012 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
1020 if (_DBUS_UNLIKELY (last_dot == NULL))
1027 * Checks that the given range of the string is a valid member name
1028 * in the D-Bus protocol. This includes a length restriction, etc.,
1029 * see the specification.
1031 * @todo this is inconsistent with most of DBusString in that
1032 * it allows a start,len range that extends past the string end.
1034 * @param str the string
1035 * @param start first byte index to check
1036 * @param len number of bytes to check
1037 * @returns #TRUE if the byte range exists and is a valid name
1040 _dbus_validate_member (const DBusString *str,
1044 const unsigned char *s;
1045 const unsigned char *end;
1046 const unsigned char *member;
1048 _dbus_assert (start >= 0);
1049 _dbus_assert (len >= 0);
1050 _dbus_assert (start <= _dbus_string_get_length (str));
1052 if (len > _dbus_string_get_length (str) - start)
1055 if (len > DBUS_MAXIMUM_NAME_LENGTH)
1061 member = _dbus_string_get_const_udata (str) + start;
1065 /* check special cases of first char so it doesn't have to be done
1066 * in the loop. Note we know len > 0
1069 if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
1076 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
1088 * Checks that the given range of the string is a valid error name
1089 * in the D-Bus protocol. This includes a length restriction, etc.,
1090 * see the specification.
1092 * @todo this is inconsistent with most of DBusString in that
1093 * it allows a start,len range that extends past the string end.
1095 * @param str the string
1096 * @param start first byte index to check
1097 * @param len number of bytes to check
1098 * @returns #TRUE if the byte range exists and is a valid name
1101 _dbus_validate_error_name (const DBusString *str,
1105 /* Same restrictions as interface name at the moment */
1106 return _dbus_validate_interface (str, start, len);
1110 * Determine wether the given character is valid as the first character
1113 #define VALID_INITIAL_BUS_NAME_CHARACTER(c) \
1114 ( ((c) >= 'A' && (c) <= 'Z') || \
1115 ((c) >= 'a' && (c) <= 'z') || \
1116 ((c) == '_') || ((c) == '-'))
1119 * Determine wether the given character is valid as a second or later
1120 * character in a bus name
1122 #define VALID_BUS_NAME_CHARACTER(c) \
1123 ( ((c) >= '0' && (c) <= '9') || \
1124 ((c) >= 'A' && (c) <= 'Z') || \
1125 ((c) >= 'a' && (c) <= 'z') || \
1126 ((c) == '_') || ((c) == '-'))
1129 _dbus_validate_bus_name_full (const DBusString *str,
1132 dbus_bool_t is_namespace)
1134 const unsigned char *s;
1135 const unsigned char *end;
1136 const unsigned char *iface;
1137 const unsigned char *last_dot;
1139 _dbus_assert (start >= 0);
1140 _dbus_assert (len >= 0);
1141 _dbus_assert (start <= _dbus_string_get_length (str));
1143 if (len > _dbus_string_get_length (str) - start)
1146 if (len > DBUS_MAXIMUM_NAME_LENGTH)
1153 iface = _dbus_string_get_const_udata (str) + start;
1157 /* check special cases of first char so it doesn't have to be done
1158 * in the loop. Note we know len > 0
1168 if (_DBUS_UNLIKELY ((s + 1) == end))
1170 if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*(s + 1))))
1172 ++s; /* we just validated the next char, so skip two */
1174 else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1184 else if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
1186 else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*s)))
1195 if (_DBUS_UNLIKELY ((s + 1) == end))
1197 else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*(s + 1))))
1200 ++s; /* we just validated the next char, so skip two */
1202 else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1210 if (!is_namespace && _DBUS_UNLIKELY (last_dot == NULL))
1217 * Checks that the given range of the string is a valid bus name in
1218 * the D-Bus protocol. This includes a length restriction, etc., see
1219 * the specification.
1221 * @todo this is inconsistent with most of DBusString in that
1222 * it allows a start,len range that extends past the string end.
1224 * @param str the string
1225 * @param start first byte index to check
1226 * @param len number of bytes to check
1227 * @returns #TRUE if the byte range exists and is a valid name
1230 _dbus_validate_bus_name (const DBusString *str,
1234 return _dbus_validate_bus_name_full (str, start, len, FALSE);
1238 * Checks that the given range of the string is a prefix of a valid bus name in
1239 * the D-Bus protocol. Unlike _dbus_validate_bus_name(), this accepts strings
1240 * with only one period-separated component.
1242 * @todo this is inconsistent with most of DBusString in that
1243 * it allows a start,len range that extends past the string end.
1245 * @param str the string
1246 * @param start first byte index to check
1247 * @param len number of bytes to check
1248 * @returns #TRUE if the byte range exists and is a valid name
1251 _dbus_validate_bus_namespace (const DBusString *str,
1255 return _dbus_validate_bus_name_full (str, start, len, TRUE);
1258 /** define _dbus_check_is_valid_path() */
1259 DEFINE_DBUS_NAME_CHECK(path)
1260 /** define _dbus_check_is_valid_interface() */
1261 DEFINE_DBUS_NAME_CHECK(interface)
1262 /** define _dbus_check_is_valid_member() */
1263 DEFINE_DBUS_NAME_CHECK(member)
1264 /** define _dbus_check_is_valid_error_name() */
1265 DEFINE_DBUS_NAME_CHECK(error_name)
1266 /** define _dbus_check_is_valid_bus_name() */
1267 DEFINE_DBUS_NAME_CHECK(bus_name)
1268 /** define _dbus_check_is_valid_utf8() */
1269 DEFINE_DBUS_NAME_CHECK(utf8)
1273 /* tests in dbus-marshal-validate-util.c */