2005-01-26 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-marshal-validate.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal-validate.c Validation routines for marshaled data
3  *
4  * Copyright (C) 2005 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include "dbus-internals.h"
25 #include "dbus-marshal-validate.h"
26 #include "dbus-marshal-recursive.h"
27
28 /**
29  * @addtogroup DBusMarshal
30  *
31  * @{
32  */
33
34 /**
35  * Verifies that the range of type_str from type_pos to type_end is a
36  * valid signature.  If this function returns #TRUE, it will be safe
37  * to iterate over the signature with a types-only #DBusTypeReader.
38  * The range passed in should NOT include the terminating
39  * nul/DBUS_TYPE_INVALID.
40  *
41  * @param type_str the string
42  * @param type_pos where the typecodes start
43  * @param len length of typecodes
44  * @returns #DBUS_VALID if valid, reason why invalid otherwise
45  */
46 DBusValidity
47 _dbus_validate_signature_with_reason (const DBusString *type_str,
48                                       int               type_pos,
49                                       int               len)
50 {
51   const unsigned char *p;
52   const unsigned char *end;
53   int last;
54   int struct_depth;
55   int array_depth;
56
57   _dbus_assert (type_str != NULL);
58   _dbus_assert (type_pos < _DBUS_INT_MAX - len);
59   _dbus_assert (len >= 0);
60   _dbus_assert (type_pos >= 0);
61
62   if (len > DBUS_MAXIMUM_SIGNATURE_LENGTH)
63     return DBUS_INVALID_SIGNATURE_TOO_LONG;
64
65   p = _dbus_string_get_const_data_len (type_str, type_pos, 0);
66   end = _dbus_string_get_const_data_len (type_str, type_pos + len, 0);
67   struct_depth = 0;
68   array_depth = 0;
69   last = DBUS_TYPE_INVALID;
70
71   while (p != end)
72     {
73       switch (*p)
74         {
75         case DBUS_TYPE_BYTE:
76         case DBUS_TYPE_BOOLEAN:
77         case DBUS_TYPE_INT32:
78         case DBUS_TYPE_UINT32:
79         case DBUS_TYPE_INT64:
80         case DBUS_TYPE_UINT64:
81         case DBUS_TYPE_DOUBLE:
82         case DBUS_TYPE_STRING:
83         case DBUS_TYPE_OBJECT_PATH:
84         case DBUS_TYPE_SIGNATURE:
85         case DBUS_TYPE_VARIANT:
86           break;
87
88         case DBUS_TYPE_ARRAY:
89           array_depth += 1;
90           if (array_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
91             return DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION;
92           break;
93
94         case DBUS_STRUCT_BEGIN_CHAR:
95           struct_depth += 1;
96
97           if (struct_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
98             return DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION;
99           break;
100
101         case DBUS_STRUCT_END_CHAR:
102           if (struct_depth == 0)
103             return DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED;
104
105           if (last == DBUS_STRUCT_BEGIN_CHAR)
106             return DBUS_INVALID_STRUCT_HAS_NO_FIELDS;
107
108           struct_depth -= 1;
109           break;
110
111         case DBUS_TYPE_STRUCT: /* doesn't appear in signatures */
112         default:
113           return DBUS_INVALID_UNKNOWN_TYPECODE;
114         }
115
116       if (*p != DBUS_TYPE_ARRAY)
117         array_depth = 0;
118
119       last = *p;
120       ++p;
121     }
122
123   if (array_depth > 0)
124     return DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
125
126   if (struct_depth > 0)
127     return DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED;
128
129   return DBUS_VALID;
130 }
131
132 static DBusValidity
133 validate_body_helper (DBusTypeReader       *reader,
134                       int                   byte_order,
135                       dbus_bool_t           walk_reader_to_end,
136                       const unsigned char  *p,
137                       const unsigned char  *end,
138                       const unsigned char **new_p)
139 {
140   int current_type;
141
142   while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
143     {
144       const unsigned char *a;
145       int alignment;
146
147       _dbus_verbose ("   validating value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
148                      _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
149                      (int) (end - p));
150
151       /* Guarantee that p has one byte to look at */
152       if (p == end)
153         return DBUS_INVALID_NOT_ENOUGH_DATA;
154
155       switch (current_type)
156         {
157         case DBUS_TYPE_BYTE:
158           ++p;
159           break;
160
161         case DBUS_TYPE_BOOLEAN:
162         case DBUS_TYPE_INT32:
163         case DBUS_TYPE_UINT32:
164         case DBUS_TYPE_INT64:
165         case DBUS_TYPE_UINT64:
166         case DBUS_TYPE_DOUBLE:
167           alignment = _dbus_type_get_alignment (current_type);
168           a = _DBUS_ALIGN_ADDRESS (p, alignment);
169           if (a >= end)
170             return DBUS_INVALID_NOT_ENOUGH_DATA;
171           while (p != a)
172             {
173               if (*p != '\0')
174                 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
175               ++p;
176             }
177           
178           if (current_type == DBUS_TYPE_BOOLEAN)
179             {
180               dbus_uint32_t v = _dbus_unpack_uint32 (byte_order,
181                                                      p);
182               if (!(v == 0 || v == 1))
183                 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
184             }
185           
186           p += alignment;
187           break;
188
189         case DBUS_TYPE_ARRAY:
190         case DBUS_TYPE_STRING:
191         case DBUS_TYPE_OBJECT_PATH:
192           {
193             dbus_uint32_t claimed_len;
194
195             a = _DBUS_ALIGN_ADDRESS (p, 4);
196             if (a + 4 > end)
197               return DBUS_INVALID_NOT_ENOUGH_DATA;
198             while (p != a)
199               {
200                 if (*p != '\0')
201                   return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
202                 ++p;
203               }
204
205             claimed_len = _dbus_unpack_uint32 (byte_order, p);
206             p += 4;
207
208             /* p may now be == end */
209             _dbus_assert (p <= end);
210             
211             if (current_type == DBUS_TYPE_ARRAY)
212               {
213                 int array_elem_type = _dbus_type_reader_get_element_type (reader);
214                 alignment = _dbus_type_get_alignment (array_elem_type);
215                 p = _DBUS_ALIGN_ADDRESS (p, alignment);
216               }
217
218             if (claimed_len > (unsigned long) (end - p))
219               return DBUS_INVALID_STRING_LENGTH_OUT_OF_BOUNDS;
220
221             if (current_type == DBUS_TYPE_OBJECT_PATH)
222               {
223                 DBusString str;
224                 _dbus_string_init_const_len (&str, p, claimed_len);
225                 if (!_dbus_validate_path (&str, 0,
226                                           _dbus_string_get_length (&str)))
227                   return DBUS_INVALID_BAD_PATH;
228
229                 p += claimed_len;
230               }
231             else if (current_type == DBUS_TYPE_STRING)
232               {
233                 DBusString str;
234                 _dbus_string_init_const_len (&str, p, claimed_len);
235                 if (!_dbus_string_validate_utf8 (&str, 0,
236                                                  _dbus_string_get_length (&str)))
237                   return DBUS_INVALID_BAD_UTF8_IN_STRING;
238
239                 p += claimed_len;
240               }
241             else if (current_type == DBUS_TYPE_ARRAY && claimed_len > 0)
242               {
243                 DBusTypeReader sub;
244                 DBusValidity validity;
245                 const unsigned char *array_end;
246
247                 /* Remember that the reader is types only, so we can't
248                  * use it to iterate over elements. It stays the same
249                  * for all elements.
250                  */
251                 _dbus_type_reader_recurse (reader, &sub);
252
253                 array_end = p + claimed_len;
254
255                 while (p < array_end)
256                   {
257                     validity = validate_body_helper (&sub, byte_order, FALSE, p, end, &p);
258                     if (validity != DBUS_VALID)
259                       return validity;
260                   }
261
262                 if (p != array_end)
263                   return DBUS_INVALID_ARRAY_LENGTH_INCORRECT;
264               }
265
266             /* check nul termination */
267             if (current_type != DBUS_TYPE_ARRAY)
268               {
269                 if (p == end)
270                   return DBUS_INVALID_NOT_ENOUGH_DATA;
271
272                 if (*p != '\0')
273                   return DBUS_INVALID_STRING_MISSING_NUL;
274                 ++p;
275               }
276           }
277           break;
278
279         case DBUS_TYPE_SIGNATURE:
280           {
281             dbus_uint32_t claimed_len;
282             DBusString str;
283
284             claimed_len = *p;
285             ++p;
286
287             /* 1 is for nul termination */
288             if (claimed_len + 1 > (unsigned long) (end - p))
289               return DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
290
291             _dbus_string_init_const_len (&str, p, claimed_len);
292             if (!_dbus_validate_signature (&str, 0,
293                                            _dbus_string_get_length (&str)))
294               return DBUS_INVALID_BAD_SIGNATURE;
295
296             p += claimed_len;
297
298             _dbus_assert (p < end);
299             if (*p != DBUS_TYPE_INVALID)
300               return DBUS_INVALID_SIGNATURE_MISSING_NUL;
301
302             ++p;
303
304             _dbus_verbose ("p = %p end = %p claimed_len %u\n", p, end, claimed_len);
305           }
306           break;
307
308         case DBUS_TYPE_VARIANT:
309           {
310             /* 1 byte sig len, sig typecodes, align to
311              * contained-type-boundary, values.
312              */
313
314             /* In addition to normal signature validation, we need to be sure
315              * the signature contains only a single (possibly container) type.
316              */
317             dbus_uint32_t claimed_len;
318             DBusString sig;
319             DBusTypeReader sub;
320             DBusValidity validity;
321             int contained_alignment;
322
323             claimed_len = *p;
324             ++p;
325
326             /* + 1 for nul */
327             if (claimed_len + 1 > (unsigned long) (end - p))
328               return DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
329
330             _dbus_string_init_const_len (&sig, p, claimed_len);
331             if (!_dbus_validate_signature (&sig, 0,
332                                            _dbus_string_get_length (&sig)))
333               return DBUS_INVALID_VARIANT_SIGNATURE_BAD;
334
335             p += claimed_len;
336
337             if (*p != DBUS_TYPE_INVALID)
338               return DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL;
339             ++p;
340
341             contained_alignment = _dbus_type_get_alignment (_dbus_first_type_in_signature (&sig, 0));
342             
343             a = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
344             if (a > end)
345               return DBUS_INVALID_NOT_ENOUGH_DATA;
346             while (p != a)
347               {
348                 if (*p != '\0')
349                   return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
350                 ++p;
351               }
352
353             _dbus_type_reader_init_types_only (&sub, &sig, 0);
354
355             if (_dbus_type_reader_get_current_type (&sub) == DBUS_TYPE_INVALID)
356               return DBUS_INVALID_VARIANT_SIGNATURE_EMPTY;
357
358             validity = validate_body_helper (&sub, byte_order, FALSE, p, end, &p);
359             if (validity != DBUS_VALID)
360               return validity;
361
362             if (_dbus_type_reader_next (&sub))
363               return DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES;
364
365             _dbus_assert (_dbus_type_reader_get_current_type (&sub) == DBUS_TYPE_INVALID);
366           }
367           break;
368
369         case DBUS_TYPE_STRUCT:
370           {
371             DBusTypeReader sub;
372             DBusValidity validity;
373
374             a = _DBUS_ALIGN_ADDRESS (p, 8);
375             if (a > end)
376               return DBUS_INVALID_NOT_ENOUGH_DATA;
377             while (p != a)
378               {
379                 if (*p != '\0')
380                   return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
381                 ++p;
382               }
383
384             _dbus_type_reader_recurse (reader, &sub);
385
386             validity = validate_body_helper (&sub, byte_order, TRUE, p, end, &p);
387             if (validity != DBUS_VALID)
388               return validity;
389           }
390           break;
391
392         default:
393           _dbus_assert_not_reached ("invalid typecode in supposedly-validated signature");
394           break;
395         }
396
397       _dbus_verbose ("   validated value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
398                      _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
399                      (int) (end - p));
400
401       if (p > end)
402         {
403           _dbus_verbose ("not enough data!!! p = %p end = %p end-p = %d\n",
404                          p, end, (int) (end - p));
405           return DBUS_INVALID_NOT_ENOUGH_DATA;
406         }
407
408       if (walk_reader_to_end)
409         _dbus_type_reader_next (reader);
410       else
411         break;
412     }
413
414   if (new_p)
415     *new_p = p;
416
417   return DBUS_VALID;
418 }
419
420 /**
421  * Verifies that the range of value_str from value_pos to value_end is
422  * a legitimate value of type expected_signature.  If this function
423  * returns #TRUE, it will be safe to iterate over the values with
424  * #DBusTypeReader. The signature is assumed to be already valid.
425  *
426  * If bytes_remaining is not #NULL, then leftover bytes will be stored
427  * there and #DBUS_VALID returned. If it is #NULL, then
428  * #DBUS_INVALID_TOO_MUCH_DATA will be returned if bytes are left
429  * over.
430  *
431  * @param expected_signature the expected types in the value_str
432  * @param expected_signature_start where in expected_signature is the signature
433  * @param byte_order the byte order
434  * @param bytes_remaining place to store leftover bytes
435  * @param value_str the string containing the body
436  * @param value_pos where the values start
437  * @param len length of values after value_pos
438  * @returns #DBUS_VALID if valid, reason why invalid otherwise
439  */
440 DBusValidity
441 _dbus_validate_body_with_reason (const DBusString *expected_signature,
442                                  int               expected_signature_start,
443                                  int               byte_order,
444                                  int              *bytes_remaining,
445                                  const DBusString *value_str,
446                                  int               value_pos,
447                                  int               len)
448 {
449   DBusTypeReader reader;
450   const unsigned char *p;
451   const unsigned char *end;
452   DBusValidity validity;
453
454   _dbus_assert (len >= 0);
455   _dbus_assert (value_pos >= 0);
456   _dbus_assert (value_pos <= _dbus_string_get_length (value_str) - len);
457
458   _dbus_verbose ("validating body from pos %d len %d sig '%s'\n",
459                  value_pos, len, _dbus_string_get_const_data_len (expected_signature,
460                                                                   expected_signature_start,
461                                                                   0));
462
463   _dbus_type_reader_init_types_only (&reader,
464                                      expected_signature, expected_signature_start);
465
466   p = _dbus_string_get_const_data_len (value_str, value_pos, len);
467   end = p + len;
468
469   validity = validate_body_helper (&reader, byte_order, TRUE, p, end, &p);
470   if (validity != DBUS_VALID)
471     return validity;
472   
473   if (bytes_remaining)
474     {
475       *bytes_remaining = end - p;
476       return DBUS_VALID;
477     }
478   else if (p < end)
479     return DBUS_INVALID_TOO_MUCH_DATA;
480   else
481     {
482       _dbus_assert (p == end);
483       return DBUS_VALID;
484     }
485 }
486
487 /**
488  * Determine wether the given charater is valid as the first charater
489  * in a name.
490  */
491 #define VALID_INITIAL_NAME_CHARACTER(c)         \
492   ( ((c) >= 'A' && (c) <= 'Z') ||               \
493     ((c) >= 'a' && (c) <= 'z') ||               \
494     ((c) == '_') )
495
496 /**
497  * Determine wether the given charater is valid as a second or later
498  * character in a name
499  */
500 #define VALID_NAME_CHARACTER(c)                 \
501   ( ((c) >= '0' && (c) <= '9') ||               \
502     ((c) >= 'A' && (c) <= 'Z') ||               \
503     ((c) >= 'a' && (c) <= 'z') ||               \
504     ((c) == '_') )
505
506 /**
507  * Checks that the given range of the string is a valid object path
508  * name in the D-BUS protocol. Part of the validation ensures that
509  * the object path contains only ASCII.
510  *
511  * @todo this is inconsistent with most of DBusString in that
512  * it allows a start,len range that extends past the string end.
513  *
514  * @todo change spec to disallow more things, such as spaces in the
515  * path name
516  *
517  * @param str the string
518  * @param start first byte index to check
519  * @param len number of bytes to check
520  * @returns #TRUE if the byte range exists and is a valid name
521  */
522 dbus_bool_t
523 _dbus_validate_path (const DBusString  *str,
524                      int                start,
525                      int                len)
526 {
527   const unsigned char *s;
528   const unsigned char *end;
529   const unsigned char *last_slash;
530
531   _dbus_assert (start >= 0);
532   _dbus_assert (len >= 0);
533   _dbus_assert (start <= _dbus_string_get_length (str));
534
535   if (len > _dbus_string_get_length (str) - start)
536     return FALSE;
537
538   if (len == 0)
539     return FALSE;
540
541   s = _dbus_string_get_const_data (str) + start;
542   end = s + len;
543
544   if (*s != '/')
545     return FALSE;
546   last_slash = s;
547   ++s;
548
549   while (s != end)
550     {
551       if (*s == '/')
552         {
553           if ((s - last_slash) < 2)
554             return FALSE; /* no empty path components allowed */
555
556           last_slash = s;
557         }
558       else
559         {
560           if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
561             return FALSE;
562         }
563
564       ++s;
565     }
566
567   if ((end - last_slash) < 2 &&
568       len > 1)
569     return FALSE; /* trailing slash not allowed unless the string is "/" */
570
571   return TRUE;
572 }
573
574 /**
575  * Checks that the given range of the string is a valid interface name
576  * in the D-BUS protocol. This includes a length restriction and an
577  * ASCII subset, see the specification.
578  *
579  * @todo this is inconsistent with most of DBusString in that
580  * it allows a start,len range that extends past the string end.
581  *
582  * @param str the string
583  * @param start first byte index to check
584  * @param len number of bytes to check
585  * @returns #TRUE if the byte range exists and is a valid name
586  */
587 dbus_bool_t
588 _dbus_validate_interface (const DBusString  *str,
589                           int                start,
590                           int                len)
591 {
592   const unsigned char *s;
593   const unsigned char *end;
594   const unsigned char *iface;
595   const unsigned char *last_dot;
596
597   _dbus_assert (start >= 0);
598   _dbus_assert (len >= 0);
599   _dbus_assert (start <= _dbus_string_get_length (str));
600
601   if (len > _dbus_string_get_length (str) - start)
602     return FALSE;
603
604   if (len > DBUS_MAXIMUM_NAME_LENGTH)
605     return FALSE;
606
607   if (len == 0)
608     return FALSE;
609
610   last_dot = NULL;
611   iface = _dbus_string_get_const_data (str) + start;
612   end = iface + len;
613   s = iface;
614
615   /* check special cases of first char so it doesn't have to be done
616    * in the loop. Note we know len > 0
617    */
618   if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
619     return FALSE;
620   else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
621     return FALSE;
622   else
623     ++s;
624
625   while (s != end)
626     {
627       if (*s == '.')
628         {
629           if (_DBUS_UNLIKELY ((s + 1) == end))
630             return FALSE;
631           else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
632             return FALSE;
633           last_dot = s;
634           ++s; /* we just validated the next char, so skip two */
635         }
636       else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
637         {
638           return FALSE;
639         }
640
641       ++s;
642     }
643
644   if (_DBUS_UNLIKELY (last_dot == NULL))
645     return FALSE;
646
647   return TRUE;
648 }
649
650 /**
651  * Checks that the given range of the string is a valid member name
652  * in the D-BUS protocol. This includes a length restriction, etc.,
653  * see the specification.
654  *
655  * @todo this is inconsistent with most of DBusString in that
656  * it allows a start,len range that extends past the string end.
657  *
658  * @param str the string
659  * @param start first byte index to check
660  * @param len number of bytes to check
661  * @returns #TRUE if the byte range exists and is a valid name
662  */
663 dbus_bool_t
664 _dbus_validate_member (const DBusString  *str,
665                        int                start,
666                        int                len)
667 {
668   const unsigned char *s;
669   const unsigned char *end;
670   const unsigned char *member;
671
672   _dbus_assert (start >= 0);
673   _dbus_assert (len >= 0);
674   _dbus_assert (start <= _dbus_string_get_length (str));
675
676   if (len > _dbus_string_get_length (str) - start)
677     return FALSE;
678
679   if (len > DBUS_MAXIMUM_NAME_LENGTH)
680     return FALSE;
681
682   if (len == 0)
683     return FALSE;
684
685   member = _dbus_string_get_const_data (str) + start;
686   end = member + len;
687   s = member;
688
689   /* check special cases of first char so it doesn't have to be done
690    * in the loop. Note we know len > 0
691    */
692
693   if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
694     return FALSE;
695   else
696     ++s;
697
698   while (s != end)
699     {
700       if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
701         {
702           return FALSE;
703         }
704
705       ++s;
706     }
707
708   return TRUE;
709 }
710
711 /**
712  * Checks that the given range of the string is a valid error name
713  * in the D-BUS protocol. This includes a length restriction, etc.,
714  * see the specification.
715  *
716  * @todo this is inconsistent with most of DBusString in that
717  * it allows a start,len range that extends past the string end.
718  *
719  * @param str the string
720  * @param start first byte index to check
721  * @param len number of bytes to check
722  * @returns #TRUE if the byte range exists and is a valid name
723  */
724 dbus_bool_t
725 _dbus_validate_error_name (const DBusString  *str,
726                            int                start,
727                            int                len)
728 {
729   /* Same restrictions as interface name at the moment */
730   return _dbus_validate_interface (str, start, len);
731 }
732
733 /* This assumes the first char exists and is ':' */
734 static dbus_bool_t
735 _dbus_validate_unique_name (const DBusString  *str,
736                             int                start,
737                             int                len)
738 {
739   const unsigned char *s;
740   const unsigned char *end;
741   const unsigned char *name;
742
743   _dbus_assert (start >= 0);
744   _dbus_assert (len >= 0);
745   _dbus_assert (start <= _dbus_string_get_length (str));
746
747   if (len > _dbus_string_get_length (str) - start)
748     return FALSE;
749
750   if (len > DBUS_MAXIMUM_NAME_LENGTH)
751     return FALSE;
752
753   _dbus_assert (len > 0);
754
755   name = _dbus_string_get_const_data (str) + start;
756   end = name + len;
757   _dbus_assert (*name == ':');
758   s = name + 1;
759
760   while (s != end)
761     {
762       if (*s == '.')
763         {
764           if (_DBUS_UNLIKELY ((s + 1) == end))
765             return FALSE;
766           if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*(s + 1))))
767             return FALSE;
768           ++s; /* we just validated the next char, so skip two */
769         }
770       else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
771         {
772           return FALSE;
773         }
774
775       ++s;
776     }
777
778   return TRUE;
779 }
780
781 /**
782  * Checks that the given range of the string is a valid bus name in
783  * the D-BUS protocol. This includes a length restriction, etc., see
784  * the specification.
785  *
786  * @todo this is inconsistent with most of DBusString in that
787  * it allows a start,len range that extends past the string end.
788  *
789  * @param str the string
790  * @param start first byte index to check
791  * @param len number of bytes to check
792  * @returns #TRUE if the byte range exists and is a valid name
793  */
794 dbus_bool_t
795 _dbus_validate_bus_name (const DBusString  *str,
796                          int                start,
797                          int                len)
798 {
799   if (_DBUS_UNLIKELY (len == 0))
800     return FALSE;
801   if (_dbus_string_get_byte (str, start) == ':')
802     return _dbus_validate_unique_name (str, start, len);
803   else
804     return _dbus_validate_interface (str, start, len);
805 }
806
807 /**
808  * Checks that the given range of the string is a valid message type
809  * signature in the D-BUS protocol.
810  *
811  * @todo this is inconsistent with most of DBusString in that
812  * it allows a start,len range that extends past the string end.
813  *
814  * @param str the string
815  * @param start first byte index to check
816  * @param len number of bytes to check
817  * @returns #TRUE if the byte range exists and is a valid signature
818  */
819 dbus_bool_t
820 _dbus_validate_signature (const DBusString  *str,
821                           int                start,
822                           int                len)
823 {
824   _dbus_assert (start >= 0);
825   _dbus_assert (start <= _dbus_string_get_length (str));
826   _dbus_assert (len >= 0);
827
828   if (len > _dbus_string_get_length (str) - start)
829     return FALSE;
830
831   return _dbus_validate_signature_with_reason (str, start, len) == DBUS_VALID;
832 }
833
834 /** define _dbus_check_is_valid_path() */
835 DEFINE_DBUS_NAME_CHECK(path);
836 /** define _dbus_check_is_valid_interface() */
837 DEFINE_DBUS_NAME_CHECK(interface);
838 /** define _dbus_check_is_valid_member() */
839 DEFINE_DBUS_NAME_CHECK(member);
840 /** define _dbus_check_is_valid_error_name() */
841 DEFINE_DBUS_NAME_CHECK(error_name);
842 /** define _dbus_check_is_valid_bus_name() */
843 DEFINE_DBUS_NAME_CHECK(bus_name);
844 /** define _dbus_check_is_valid_signature() */
845 DEFINE_DBUS_NAME_CHECK(signature);
846
847 /** @} */
848
849 /* tests in dbus-marshal-validate-util.c */