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