78d559412f7419ba0e94617fa9aecb50ea3a3714
[monky] / src / dbus / dbus-marshal-validate.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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-signature.h"
29 #include "dbus-string.h"
30
31 /**
32  * @addtogroup DBusMarshal
33  *
34  * @{
35  */
36
37 /**
38  * Verifies that the range of type_str from type_pos to type_end is a
39  * valid signature.  If this function returns #TRUE, it will be safe
40  * to iterate over the signature with a types-only #DBusTypeReader.
41  * The range passed in should NOT include the terminating
42  * nul/DBUS_TYPE_INVALID.
43  *
44  * @param type_str the string
45  * @param type_pos where the typecodes start
46  * @param len length of typecodes
47  * @returns #DBUS_VALID if valid, reason why invalid otherwise
48  */
49 DBusValidity
50 _dbus_validate_signature_with_reason (const DBusString *type_str,
51                                       int               type_pos,
52                                       int               len)
53 {
54   const unsigned char *p;
55   const unsigned char *end;
56   int last;
57   int struct_depth;
58   int array_depth;
59   int dict_entry_depth;
60   DBusValidity result;
61
62   int element_count;
63   DBusList *element_count_stack;
64
65   result = DBUS_VALID;
66   element_count_stack = NULL;
67
68   if (!_dbus_list_append (&element_count_stack, _DBUS_INT_TO_POINTER (0)))
69     {
70       result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
71       goto out;
72     }
73
74   _dbus_assert (type_str != NULL);
75   _dbus_assert (type_pos < _DBUS_INT32_MAX - len);
76   _dbus_assert (len >= 0);
77   _dbus_assert (type_pos >= 0);
78
79   if (len > DBUS_MAXIMUM_SIGNATURE_LENGTH)
80     {
81       result = DBUS_INVALID_SIGNATURE_TOO_LONG;
82       goto out;
83     }
84
85   p = _dbus_string_get_const_data_len (type_str, type_pos, 0);
86
87   end = _dbus_string_get_const_data_len (type_str, type_pos + len, 0);
88   struct_depth = 0;
89   array_depth = 0;
90   dict_entry_depth = 0;
91   last = DBUS_TYPE_INVALID;
92
93   while (p != end)
94     {
95       switch (*p)
96         {
97         case DBUS_TYPE_BYTE:
98         case DBUS_TYPE_BOOLEAN:
99         case DBUS_TYPE_INT16:
100         case DBUS_TYPE_UINT16:
101         case DBUS_TYPE_INT32:
102         case DBUS_TYPE_UINT32:
103         case DBUS_TYPE_INT64:
104         case DBUS_TYPE_UINT64:
105         case DBUS_TYPE_DOUBLE:
106         case DBUS_TYPE_STRING:
107         case DBUS_TYPE_OBJECT_PATH:
108         case DBUS_TYPE_SIGNATURE:
109         case DBUS_TYPE_VARIANT:
110           break;
111
112         case DBUS_TYPE_ARRAY:
113           array_depth += 1;
114           if (array_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
115             {
116               result = DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION;
117               goto out;
118             }
119           break;
120
121         case DBUS_STRUCT_BEGIN_CHAR:
122           struct_depth += 1;
123
124           if (struct_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
125             {
126               result = DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION;
127               goto out;
128             }
129           
130           if (!_dbus_list_append (&element_count_stack, 
131                              _DBUS_INT_TO_POINTER (0)))
132             {
133               result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
134               goto out;
135             }
136
137           break;
138
139         case DBUS_STRUCT_END_CHAR:
140           if (struct_depth == 0)
141             {
142               result = DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED;
143               goto out;
144             }
145
146           if (last == DBUS_STRUCT_BEGIN_CHAR)
147             {
148               result = DBUS_INVALID_STRUCT_HAS_NO_FIELDS;
149               goto out;
150             }
151
152           _dbus_list_pop_last (&element_count_stack);
153
154           struct_depth -= 1;
155           break;
156
157         case DBUS_DICT_ENTRY_BEGIN_CHAR:
158           if (last != DBUS_TYPE_ARRAY)
159             {
160               result = DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY;
161               goto out;
162             }
163             
164           dict_entry_depth += 1;
165
166           if (dict_entry_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
167             {
168               result = DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION;
169               goto out;
170             }
171
172           if (!_dbus_list_append (&element_count_stack, 
173                              _DBUS_INT_TO_POINTER (0)))
174             {
175               result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
176               goto out;
177             }
178
179           break;
180
181         case DBUS_DICT_ENTRY_END_CHAR:
182           if (dict_entry_depth == 0)
183             {
184               result = DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED;
185               goto out;
186             }
187             
188           dict_entry_depth -= 1;
189
190           element_count = 
191             _DBUS_POINTER_TO_INT (_dbus_list_pop_last (&element_count_stack));
192
193           if (element_count != 2)
194             {
195               if (element_count == 0)
196                 result = DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS;
197               else if (element_count == 1)
198                 result = DBUS_INVALID_DICT_ENTRY_HAS_ONLY_ONE_FIELD;
199               else
200                 result = DBUS_INVALID_DICT_ENTRY_HAS_TOO_MANY_FIELDS;
201               
202               goto out;
203             }
204           break;
205           
206         case DBUS_TYPE_STRUCT:     /* doesn't appear in signatures */
207         case DBUS_TYPE_DICT_ENTRY: /* ditto */
208         default:
209           result = DBUS_INVALID_UNKNOWN_TYPECODE;
210           goto out;
211         }
212
213       if (*p != DBUS_TYPE_ARRAY && 
214           *p != DBUS_DICT_ENTRY_BEGIN_CHAR && 
215           *p != DBUS_STRUCT_BEGIN_CHAR) 
216         {
217           element_count = 
218             _DBUS_POINTER_TO_INT (_dbus_list_pop_last (&element_count_stack));
219
220           ++element_count;
221
222           if (!_dbus_list_append (&element_count_stack, 
223                              _DBUS_INT_TO_POINTER (element_count)))
224             {
225               result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
226               goto out;
227             }
228         }
229       
230       if (array_depth > 0)
231         {
232           if (*p == DBUS_TYPE_ARRAY && p != end)
233             {
234                const char *p1;
235                p1 = p + 1;
236                if (*p1 == DBUS_STRUCT_END_CHAR ||
237                    *p1 == DBUS_DICT_ENTRY_END_CHAR)
238                  {
239                    result = DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
240                    goto out;
241                  }
242             }
243           else
244             {
245               array_depth = 0;
246             }
247         }
248
249       if (last == DBUS_DICT_ENTRY_BEGIN_CHAR)
250         {
251           if (!(_dbus_type_is_valid (*p) && dbus_type_is_basic (*p)))
252             {
253               result = DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE;
254               goto out;
255             }
256         }
257
258       last = *p;
259       ++p;
260     }
261
262
263   if (array_depth > 0)
264     {
265       result = DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
266       goto out;
267     }
268     
269   if (struct_depth > 0)
270     {
271        result = DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED;
272        goto out;
273     }
274     
275   if (dict_entry_depth > 0)
276     {
277       result =  DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED;
278       goto out;
279     }
280     
281   _dbus_assert (last != DBUS_TYPE_ARRAY);
282   _dbus_assert (last != DBUS_STRUCT_BEGIN_CHAR);
283   _dbus_assert (last != DBUS_DICT_ENTRY_BEGIN_CHAR);
284
285   result = DBUS_VALID;
286
287 out:
288   _dbus_list_clear (&element_count_stack);
289   return result;
290 }
291
292 static DBusValidity
293 validate_body_helper (DBusTypeReader       *reader,
294                       int                   byte_order,
295                       dbus_bool_t           walk_reader_to_end,
296                       const unsigned char  *p,
297                       const unsigned char  *end,
298                       const unsigned char **new_p)
299 {
300   int current_type;
301
302   while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
303     {
304       const unsigned char *a;
305       int alignment;
306
307 #if 0
308       _dbus_verbose ("   validating value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
309                      _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
310                      (int) (end - p));
311 #endif
312
313       /* Guarantee that p has one byte to look at */
314       if (p == end)
315         return DBUS_INVALID_NOT_ENOUGH_DATA;
316
317       switch (current_type)
318         {
319         case DBUS_TYPE_BYTE:
320           ++p;
321           break;
322           
323         case DBUS_TYPE_BOOLEAN:
324         case DBUS_TYPE_INT16:
325         case DBUS_TYPE_UINT16:
326         case DBUS_TYPE_INT32:
327         case DBUS_TYPE_UINT32:
328         case DBUS_TYPE_INT64:
329         case DBUS_TYPE_UINT64:
330         case DBUS_TYPE_DOUBLE:
331           alignment = _dbus_type_get_alignment (current_type);
332           a = _DBUS_ALIGN_ADDRESS (p, alignment);
333           if (a >= end)
334             return DBUS_INVALID_NOT_ENOUGH_DATA;
335           while (p != a)
336             {
337               if (*p != '\0')
338                 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
339               ++p;
340             }
341           
342           if (current_type == DBUS_TYPE_BOOLEAN)
343             {
344               dbus_uint32_t v = _dbus_unpack_uint32 (byte_order,
345                                                      p);
346               if (!(v == 0 || v == 1))
347                 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
348             }
349           
350           p += alignment;
351           break;
352
353         case DBUS_TYPE_ARRAY:
354         case DBUS_TYPE_STRING:
355         case DBUS_TYPE_OBJECT_PATH:
356           {
357             dbus_uint32_t claimed_len;
358
359             a = _DBUS_ALIGN_ADDRESS (p, 4);
360             if (a + 4 > end)
361               return DBUS_INVALID_NOT_ENOUGH_DATA;
362             while (p != a)
363               {
364                 if (*p != '\0')
365                   return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
366                 ++p;
367               }
368
369             claimed_len = _dbus_unpack_uint32 (byte_order, p);
370             p += 4;
371
372             /* p may now be == end */
373             _dbus_assert (p <= end);
374
375             if (current_type == DBUS_TYPE_ARRAY)
376               {
377                 int array_elem_type = _dbus_type_reader_get_element_type (reader);
378
379                 if (!_dbus_type_is_valid (array_elem_type))
380                   {
381                     return DBUS_INVALID_UNKNOWN_TYPECODE;
382                   }
383
384                 alignment = _dbus_type_get_alignment (array_elem_type);
385
386                 a = _DBUS_ALIGN_ADDRESS (p, alignment);
387
388                 /* a may now be == end */
389                 if (a > end)
390                   return DBUS_INVALID_NOT_ENOUGH_DATA;
391
392                 while (p != a)
393                   {
394                     if (*p != '\0')
395                       return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
396                     ++p;
397                   }
398               }
399
400             if (claimed_len > (unsigned long) (end - p))
401               return DBUS_INVALID_LENGTH_OUT_OF_BOUNDS;
402
403             if (current_type == DBUS_TYPE_OBJECT_PATH)
404               {
405                 DBusString str;
406                 _dbus_string_init_const_len (&str, p, claimed_len);
407                 if (!_dbus_validate_path (&str, 0,
408                                           _dbus_string_get_length (&str)))
409                   return DBUS_INVALID_BAD_PATH;
410
411                 p += claimed_len;
412               }
413             else if (current_type == DBUS_TYPE_STRING)
414               {
415                 DBusString str;
416                 _dbus_string_init_const_len (&str, p, claimed_len);
417                 if (!_dbus_string_validate_utf8 (&str, 0,
418                                                  _dbus_string_get_length (&str)))
419                   return DBUS_INVALID_BAD_UTF8_IN_STRING;
420
421                 p += claimed_len;
422               }
423             else if (current_type == DBUS_TYPE_ARRAY && claimed_len > 0)
424               {
425                 DBusTypeReader sub;
426                 DBusValidity validity;
427                 const unsigned char *array_end;
428                 int array_elem_type;
429
430                 if (claimed_len > DBUS_MAXIMUM_ARRAY_LENGTH)
431                   return DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM;
432                 
433                 /* Remember that the reader is types only, so we can't
434                  * use it to iterate over elements. It stays the same
435                  * for all elements.
436                  */
437                 _dbus_type_reader_recurse (reader, &sub);
438
439                 array_end = p + claimed_len;
440
441                 array_elem_type = _dbus_type_reader_get_element_type (reader);
442
443                 /* avoid recursive call to validate_body_helper if this is an array
444                  * of fixed-size elements
445                  */ 
446                 if (dbus_type_is_fixed (array_elem_type))
447                   {
448                     /* bools need to be handled differently, because they can
449                      * have an invalid value
450                      */
451                     if (array_elem_type == DBUS_TYPE_BOOLEAN)
452                       {
453                         dbus_uint32_t v;
454                         alignment = _dbus_type_get_alignment (array_elem_type);
455
456                         while (p < array_end)
457                           {
458                             v = _dbus_unpack_uint32 (byte_order, p);
459
460                             if (!(v == 0 || v == 1))
461                               return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
462
463                             p += alignment;
464                           }
465                       }
466
467                     else
468                       {
469                         p = array_end;
470                       }
471                   }
472
473                 else
474                   {
475                     while (p < array_end)
476                       {
477                         validity = validate_body_helper (&sub, byte_order, FALSE, p, end, &p);
478                         if (validity != DBUS_VALID)
479                           return validity;
480                       }
481                   }
482
483                 if (p != array_end)
484                   return DBUS_INVALID_ARRAY_LENGTH_INCORRECT;
485               }
486
487             /* check nul termination */
488             if (current_type != DBUS_TYPE_ARRAY)
489               {
490                 if (p == end)
491                   return DBUS_INVALID_NOT_ENOUGH_DATA;
492
493                 if (*p != '\0')
494                   return DBUS_INVALID_STRING_MISSING_NUL;
495                 ++p;
496               }
497           }
498           break;
499
500         case DBUS_TYPE_SIGNATURE:
501           {
502             dbus_uint32_t claimed_len;
503             DBusString str;
504             DBusValidity validity;
505
506             claimed_len = *p;
507             ++p;
508
509             /* 1 is for nul termination */
510             if (claimed_len + 1 > (unsigned long) (end - p))
511               return DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
512
513             _dbus_string_init_const_len (&str, p, claimed_len);
514             validity =
515               _dbus_validate_signature_with_reason (&str, 0,
516                                                     _dbus_string_get_length (&str));
517
518             if (validity != DBUS_VALID)
519               return validity;
520
521             p += claimed_len;
522
523             _dbus_assert (p < end);
524             if (*p != DBUS_TYPE_INVALID)
525               return DBUS_INVALID_SIGNATURE_MISSING_NUL;
526
527             ++p;
528
529             _dbus_verbose ("p = %p end = %p claimed_len %u\n", p, end, claimed_len);
530           }
531           break;
532
533         case DBUS_TYPE_VARIANT:
534           {
535             /* 1 byte sig len, sig typecodes, align to
536              * contained-type-boundary, values.
537              */
538
539             /* In addition to normal signature validation, we need to be sure
540              * the signature contains only a single (possibly container) type.
541              */
542             dbus_uint32_t claimed_len;
543             DBusString sig;
544             DBusTypeReader sub;
545             DBusValidity validity;
546             int contained_alignment;
547             int contained_type;
548             DBusValidity reason;
549
550             claimed_len = *p;
551             ++p;
552
553             /* + 1 for nul */
554             if (claimed_len + 1 > (unsigned long) (end - p))
555               return DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
556
557             _dbus_string_init_const_len (&sig, p, claimed_len);
558             reason = _dbus_validate_signature_with_reason (&sig, 0,
559                                            _dbus_string_get_length (&sig));
560             if (!(reason == DBUS_VALID))
561               {
562                 if (reason == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
563                   return reason;
564                 else 
565                   return DBUS_INVALID_VARIANT_SIGNATURE_BAD;
566               }
567
568             p += claimed_len;
569             
570             if (*p != DBUS_TYPE_INVALID)
571               return DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL;
572             ++p;
573
574             contained_type = _dbus_first_type_in_signature (&sig, 0);
575             if (contained_type == DBUS_TYPE_INVALID)
576               return DBUS_INVALID_VARIANT_SIGNATURE_EMPTY;
577             
578             contained_alignment = _dbus_type_get_alignment (contained_type);
579             
580             a = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
581             if (a > end)
582               return DBUS_INVALID_NOT_ENOUGH_DATA;
583             while (p != a)
584               {
585                 if (*p != '\0')
586                   return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
587                 ++p;
588               }
589
590             _dbus_type_reader_init_types_only (&sub, &sig, 0);
591
592             _dbus_assert (_dbus_type_reader_get_current_type (&sub) != DBUS_TYPE_INVALID);
593
594             validity = validate_body_helper (&sub, byte_order, FALSE, p, end, &p);
595             if (validity != DBUS_VALID)
596               return validity;
597
598             if (_dbus_type_reader_next (&sub))
599               return DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES;
600
601             _dbus_assert (_dbus_type_reader_get_current_type (&sub) == DBUS_TYPE_INVALID);
602           }
603           break;
604
605         case DBUS_TYPE_DICT_ENTRY:
606         case DBUS_TYPE_STRUCT:
607           {
608             DBusTypeReader sub;
609             DBusValidity validity;
610
611             a = _DBUS_ALIGN_ADDRESS (p, 8);
612             if (a > end)
613               return DBUS_INVALID_NOT_ENOUGH_DATA;
614             while (p != a)
615               {
616                 if (*p != '\0')
617                   return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
618                 ++p;
619               }
620
621             _dbus_type_reader_recurse (reader, &sub);
622
623             validity = validate_body_helper (&sub, byte_order, TRUE, p, end, &p);
624             if (validity != DBUS_VALID)
625               return validity;
626           }
627           break;
628
629         default:
630           _dbus_assert_not_reached ("invalid typecode in supposedly-validated signature");
631           break;
632         }
633
634 #if 0
635       _dbus_verbose ("   validated value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
636                      _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
637                      (int) (end - p));
638 #endif
639
640       if (p > end)
641         {
642           _dbus_verbose ("not enough data!!! p = %p end = %p end-p = %d\n",
643                          p, end, (int) (end - p));
644           return DBUS_INVALID_NOT_ENOUGH_DATA;
645         }
646
647       if (walk_reader_to_end)
648         _dbus_type_reader_next (reader);
649       else
650         break;
651     }
652
653   if (new_p)
654     *new_p = p;
655
656   return DBUS_VALID;
657 }
658
659 /**
660  * Verifies that the range of value_str from value_pos to value_end is
661  * a legitimate value of type expected_signature.  If this function
662  * returns #TRUE, it will be safe to iterate over the values with
663  * #DBusTypeReader. The signature is assumed to be already valid.
664  *
665  * If bytes_remaining is not #NULL, then leftover bytes will be stored
666  * there and #DBUS_VALID returned. If it is #NULL, then
667  * #DBUS_INVALID_TOO_MUCH_DATA will be returned if bytes are left
668  * over.
669  *
670  * @param expected_signature the expected types in the value_str
671  * @param expected_signature_start where in expected_signature is the signature
672  * @param byte_order the byte order
673  * @param bytes_remaining place to store leftover bytes
674  * @param value_str the string containing the body
675  * @param value_pos where the values start
676  * @param len length of values after value_pos
677  * @returns #DBUS_VALID if valid, reason why invalid otherwise
678  */
679 DBusValidity
680 _dbus_validate_body_with_reason (const DBusString *expected_signature,
681                                  int               expected_signature_start,
682                                  int               byte_order,
683                                  int              *bytes_remaining,
684                                  const DBusString *value_str,
685                                  int               value_pos,
686                                  int               len)
687 {
688   DBusTypeReader reader;
689   const unsigned char *p;
690   const unsigned char *end;
691   DBusValidity validity;
692
693   _dbus_assert (len >= 0);
694   _dbus_assert (value_pos >= 0);
695   _dbus_assert (value_pos <= _dbus_string_get_length (value_str) - len);
696
697   _dbus_verbose ("validating body from pos %d len %d sig '%s'\n",
698                  value_pos, len, _dbus_string_get_const_data_len (expected_signature,
699                                                                   expected_signature_start,
700                                                                   0));
701
702   _dbus_type_reader_init_types_only (&reader,
703                                      expected_signature, expected_signature_start);
704
705   p = _dbus_string_get_const_data_len (value_str, value_pos, len);
706   end = p + len;
707
708   validity = validate_body_helper (&reader, byte_order, TRUE, p, end, &p);
709   if (validity != DBUS_VALID)
710     return validity;
711   
712   if (bytes_remaining)
713     {
714       *bytes_remaining = end - p;
715       return DBUS_VALID;
716     }
717   else if (p < end)
718     return DBUS_INVALID_TOO_MUCH_DATA;
719   else
720     {
721       _dbus_assert (p == end);
722       return DBUS_VALID;
723     }
724 }
725
726 /**
727  * Determine wether the given character is valid as the first character
728  * in a name.
729  */
730 #define VALID_INITIAL_NAME_CHARACTER(c)         \
731   ( ((c) >= 'A' && (c) <= 'Z') ||               \
732     ((c) >= 'a' && (c) <= 'z') ||               \
733     ((c) == '_') )
734
735 /**
736  * Determine wether the given character is valid as a second or later
737  * character in a name
738  */
739 #define VALID_NAME_CHARACTER(c)                 \
740   ( ((c) >= '0' && (c) <= '9') ||               \
741     ((c) >= 'A' && (c) <= 'Z') ||               \
742     ((c) >= 'a' && (c) <= 'z') ||               \
743     ((c) == '_') )
744
745 /**
746  * Checks that the given range of the string is a valid object path
747  * name in the D-Bus protocol. Part of the validation ensures that
748  * the object path contains only ASCII.
749  *
750  * @todo this is inconsistent with most of DBusString in that
751  * it allows a start,len range that extends past the string end.
752  *
753  * @todo change spec to disallow more things, such as spaces in the
754  * path name
755  *
756  * @param str the string
757  * @param start first byte index to check
758  * @param len number of bytes to check
759  * @returns #TRUE if the byte range exists and is a valid name
760  */
761 dbus_bool_t
762 _dbus_validate_path (const DBusString  *str,
763                      int                start,
764                      int                len)
765 {
766   const unsigned char *s;
767   const unsigned char *end;
768   const unsigned char *last_slash;
769
770   _dbus_assert (start >= 0);
771   _dbus_assert (len >= 0);
772   _dbus_assert (start <= _dbus_string_get_length (str));
773   
774   if (len > _dbus_string_get_length (str) - start)
775     return FALSE;
776
777   if (len == 0)
778     return FALSE;
779
780   s = _dbus_string_get_const_data (str) + start;
781   end = s + len;
782
783   if (*s != '/')
784     return FALSE;
785   last_slash = s;
786   ++s;
787
788   while (s != end)
789     {
790       if (*s == '/')
791         {
792           if ((s - last_slash) < 2)
793             return FALSE; /* no empty path components allowed */
794
795           last_slash = s;
796         }
797       else
798         {
799           if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
800             return FALSE;
801         }
802
803       ++s;
804     }
805
806   if ((end - last_slash) < 2 &&
807       len > 1)
808     return FALSE; /* trailing slash not allowed unless the string is "/" */
809
810   return TRUE;
811 }
812
813 const char *
814 _dbus_validity_to_error_message (DBusValidity validity)
815 {
816   switch (validity)
817     {
818     case DBUS_VALIDITY_UNKNOWN_OOM_ERROR:                          return "Out of memory";
819     case DBUS_INVALID_FOR_UNKNOWN_REASON:                          return "Unknown reason";
820     case DBUS_VALID_BUT_INCOMPLETE:                                return "Valid but incomplete";
821     case DBUS_VALIDITY_UNKNOWN:                                    return "Validity unknown";
822     case DBUS_VALID:                                               return "Valid";
823     case DBUS_INVALID_UNKNOWN_TYPECODE:                            return "Unknown typecode";
824     case DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE:                  return "Missing array element type";
825     case DBUS_INVALID_SIGNATURE_TOO_LONG:                          return "Signature is too long";
826     case DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION:            return "Exceeded maximum array recursion";
827     case DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION:           return "Exceeded maximum struct recursion";
828     case DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED:                return "Struct ended but not started";
829     case DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED:                return "Struct started but not ended";
830     case DBUS_INVALID_STRUCT_HAS_NO_FIELDS:                        return "Struct has no fields";
831     case DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL:                   return "Alignment padding not null";
832     case DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE:                     return "Boolean is not zero or one";
833     case DBUS_INVALID_NOT_ENOUGH_DATA:                             return "Not enough data";
834     case DBUS_INVALID_TOO_MUCH_DATA:                               return "Too much data";
835     case DBUS_INVALID_BAD_BYTE_ORDER:                              return "Bad byte order";
836     case DBUS_INVALID_BAD_PROTOCOL_VERSION:                        return "Bad protocol version";
837     case DBUS_INVALID_BAD_MESSAGE_TYPE:                            return "Bad message type";
838     case DBUS_INVALID_BAD_SERIAL:                                  return "Bad serial";
839     case DBUS_INVALID_INSANE_FIELDS_ARRAY_LENGTH:                  return "Insane fields array length";
840     case DBUS_INVALID_INSANE_BODY_LENGTH:                          return "Insane body length";
841     case DBUS_INVALID_MESSAGE_TOO_LONG:                            return "Message too long";
842     case DBUS_INVALID_HEADER_FIELD_CODE:                           return "Header field code";
843     case DBUS_INVALID_HEADER_FIELD_HAS_WRONG_TYPE:                 return "Header field has wrong type";
844     case DBUS_INVALID_USES_LOCAL_INTERFACE:                        return "Uses local interface";
845     case DBUS_INVALID_USES_LOCAL_PATH:                             return "Uses local path";
846     case DBUS_INVALID_HEADER_FIELD_APPEARS_TWICE:                  return "Header field appears twice";
847     case DBUS_INVALID_BAD_DESTINATION:                             return "Bad destination";
848     case DBUS_INVALID_BAD_INTERFACE:                               return "Bad interface";
849     case DBUS_INVALID_BAD_MEMBER:                                  return "Bad member";
850     case DBUS_INVALID_BAD_ERROR_NAME:                              return "Bad error name";
851     case DBUS_INVALID_BAD_SENDER:                                  return "Bad sender";
852     case DBUS_INVALID_MISSING_PATH:                                return "Missing path";
853     case DBUS_INVALID_MISSING_INTERFACE:                           return "Missing interface";
854     case DBUS_INVALID_MISSING_MEMBER:                              return "Missing member";
855     case DBUS_INVALID_MISSING_ERROR_NAME:                          return "Missing error name";
856     case DBUS_INVALID_MISSING_REPLY_SERIAL:                        return "Missing reply serial";
857     case DBUS_INVALID_LENGTH_OUT_OF_BOUNDS:                        return "Length out of bounds";
858     case DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM:                return "Array length exceeds maximum";
859     case DBUS_INVALID_BAD_PATH:                                    return "Bad path";
860     case DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS:              return "Signature length out of bounds";
861     case DBUS_INVALID_BAD_UTF8_IN_STRING:                          return "Bad utf8 in string";
862     case DBUS_INVALID_ARRAY_LENGTH_INCORRECT:                      return "Array length incorrect";
863     case DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS:      return "Variant signature length out of bounds";
864     case DBUS_INVALID_VARIANT_SIGNATURE_BAD:                       return "Variant signature bad";
865     case DBUS_INVALID_VARIANT_SIGNATURE_EMPTY:                     return "Variant signature empty";
866     case DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES: return "Variant signature specifies multiple values";
867     case DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL:               return "Variant signature missing nul";
868     case DBUS_INVALID_STRING_MISSING_NUL:                          return "String missing nul";
869     case DBUS_INVALID_SIGNATURE_MISSING_NUL:                       return "Signature missing nul";
870     case DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION:       return "Exceeded maximum dict entry recursion";
871     case DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED:            return "Dict entry ended but not started";
872     case DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED:            return "Dict entry started but not ended";
873     case DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS:                    return "Dict entry has no fields";
874     case DBUS_INVALID_DICT_ENTRY_HAS_ONLY_ONE_FIELD:               return "Dict entry has only one field";
875     case DBUS_INVALID_DICT_ENTRY_HAS_TOO_MANY_FIELDS:              return "Dict entry has too many fields";
876     case DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY:                 return "Dict entry not inside array";
877     case DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE:                 return "Dict key must be basic type";
878
879     default:
880       return "Invalid";
881     }
882 }
883
884 /**
885  * Checks that the given range of the string is a valid interface name
886  * in the D-Bus protocol. This includes a length restriction and an
887  * ASCII subset, see the specification.
888  *
889  * @todo this is inconsistent with most of DBusString in that
890  * it allows a start,len range that extends past the string end.
891  *
892  * @param str the string
893  * @param start first byte index to check
894  * @param len number of bytes to check
895  * @returns #TRUE if the byte range exists and is a valid name
896  */
897 dbus_bool_t
898 _dbus_validate_interface (const DBusString  *str,
899                           int                start,
900                           int                len)
901 {
902   const unsigned char *s;
903   const unsigned char *end;
904   const unsigned char *iface;
905   const unsigned char *last_dot;
906
907   _dbus_assert (start >= 0);
908   _dbus_assert (len >= 0);
909   _dbus_assert (start <= _dbus_string_get_length (str));
910
911   if (len > _dbus_string_get_length (str) - start)
912     return FALSE;
913
914   if (len > DBUS_MAXIMUM_NAME_LENGTH)
915     return FALSE;
916
917   if (len == 0)
918     return FALSE;
919
920   last_dot = NULL;
921   iface = _dbus_string_get_const_data (str) + start;
922   end = iface + len;
923   s = iface;
924
925   /* check special cases of first char so it doesn't have to be done
926    * in the loop. Note we know len > 0
927    */
928   if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
929     return FALSE;
930   else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
931     return FALSE;
932   else
933     ++s;
934
935   while (s != end)
936     {
937       if (*s == '.')
938         {
939           if (_DBUS_UNLIKELY ((s + 1) == end))
940             return FALSE;
941           else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
942             return FALSE;
943           last_dot = s;
944           ++s; /* we just validated the next char, so skip two */
945         }
946       else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
947         {
948           return FALSE;
949         }
950
951       ++s;
952     }
953
954   if (_DBUS_UNLIKELY (last_dot == NULL))
955     return FALSE;
956
957   return TRUE;
958 }
959
960 /**
961  * Checks that the given range of the string is a valid member name
962  * in the D-Bus protocol. This includes a length restriction, etc.,
963  * see the specification.
964  *
965  * @todo this is inconsistent with most of DBusString in that
966  * it allows a start,len range that extends past the string end.
967  *
968  * @param str the string
969  * @param start first byte index to check
970  * @param len number of bytes to check
971  * @returns #TRUE if the byte range exists and is a valid name
972  */
973 dbus_bool_t
974 _dbus_validate_member (const DBusString  *str,
975                        int                start,
976                        int                len)
977 {
978   const unsigned char *s;
979   const unsigned char *end;
980   const unsigned char *member;
981
982   _dbus_assert (start >= 0);
983   _dbus_assert (len >= 0);
984   _dbus_assert (start <= _dbus_string_get_length (str));
985
986   if (len > _dbus_string_get_length (str) - start)
987     return FALSE;
988
989   if (len > DBUS_MAXIMUM_NAME_LENGTH)
990     return FALSE;
991
992   if (len == 0)
993     return FALSE;
994
995   member = _dbus_string_get_const_data (str) + start;
996   end = member + len;
997   s = member;
998
999   /* check special cases of first char so it doesn't have to be done
1000    * in the loop. Note we know len > 0
1001    */
1002
1003   if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
1004     return FALSE;
1005   else
1006     ++s;
1007
1008   while (s != end)
1009     {
1010       if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
1011         {
1012           return FALSE;
1013         }
1014
1015       ++s;
1016     }
1017
1018   return TRUE;
1019 }
1020
1021 /**
1022  * Checks that the given range of the string is a valid error name
1023  * in the D-Bus protocol. This includes a length restriction, etc.,
1024  * see the specification.
1025  *
1026  * @todo this is inconsistent with most of DBusString in that
1027  * it allows a start,len range that extends past the string end.
1028  *
1029  * @param str the string
1030  * @param start first byte index to check
1031  * @param len number of bytes to check
1032  * @returns #TRUE if the byte range exists and is a valid name
1033  */
1034 dbus_bool_t
1035 _dbus_validate_error_name (const DBusString  *str,
1036                            int                start,
1037                            int                len)
1038 {
1039   /* Same restrictions as interface name at the moment */
1040   return _dbus_validate_interface (str, start, len);
1041 }
1042
1043 /**
1044  * Determine wether the given character is valid as the first character
1045  * in a bus name.
1046  */
1047 #define VALID_INITIAL_BUS_NAME_CHARACTER(c)         \
1048   ( ((c) >= 'A' && (c) <= 'Z') ||               \
1049     ((c) >= 'a' && (c) <= 'z') ||               \
1050     ((c) == '_') || ((c) == '-'))
1051
1052 /**
1053  * Determine wether the given character is valid as a second or later
1054  * character in a bus name
1055  */
1056 #define VALID_BUS_NAME_CHARACTER(c)                 \
1057   ( ((c) >= '0' && (c) <= '9') ||               \
1058     ((c) >= 'A' && (c) <= 'Z') ||               \
1059     ((c) >= 'a' && (c) <= 'z') ||               \
1060     ((c) == '_') || ((c) == '-'))
1061
1062 /**
1063  * Checks that the given range of the string is a valid bus name in
1064  * the D-Bus protocol. This includes a length restriction, etc., see
1065  * the specification.
1066  *
1067  * @todo this is inconsistent with most of DBusString in that
1068  * it allows a start,len range that extends past the string end.
1069  *
1070  * @param str the string
1071  * @param start first byte index to check
1072  * @param len number of bytes to check
1073  * @returns #TRUE if the byte range exists and is a valid name
1074  */
1075 dbus_bool_t
1076 _dbus_validate_bus_name (const DBusString  *str,
1077                          int                start,
1078                          int                len)
1079 {
1080   const unsigned char *s;
1081   const unsigned char *end;
1082   const unsigned char *iface;
1083   const unsigned char *last_dot;
1084
1085   _dbus_assert (start >= 0);
1086   _dbus_assert (len >= 0);
1087   _dbus_assert (start <= _dbus_string_get_length (str));
1088
1089   if (len > _dbus_string_get_length (str) - start)
1090     return FALSE;
1091
1092   if (len > DBUS_MAXIMUM_NAME_LENGTH)
1093     return FALSE;
1094
1095   if (len == 0)
1096     return FALSE;
1097
1098   last_dot = NULL;
1099   iface = _dbus_string_get_const_data (str) + start;
1100   end = iface + len;
1101   s = iface;
1102
1103   /* check special cases of first char so it doesn't have to be done
1104    * in the loop. Note we know len > 0
1105    */
1106   if (*s == ':')
1107   {
1108     /* unique name */
1109     ++s;
1110     while (s != end)
1111       {
1112         if (*s == '.')
1113           {
1114             if (_DBUS_UNLIKELY ((s + 1) == end))
1115               return FALSE;
1116             if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*(s + 1))))
1117               return FALSE;
1118             ++s; /* we just validated the next char, so skip two */
1119           }
1120         else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1121           {
1122             return FALSE;
1123           }
1124
1125         ++s;
1126       }
1127
1128     return TRUE;
1129   }
1130   else if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
1131     return FALSE;
1132   else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*s)))
1133     return FALSE;
1134   else
1135     ++s;
1136
1137   while (s != end)
1138     {
1139       if (*s == '.')
1140         {
1141           if (_DBUS_UNLIKELY ((s + 1) == end))
1142             return FALSE;
1143           else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*(s + 1))))
1144             return FALSE;
1145           last_dot = s;
1146           ++s; /* we just validated the next char, so skip two */
1147         }
1148       else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1149         {
1150           return FALSE;
1151         }
1152
1153       ++s;
1154     }
1155
1156   if (_DBUS_UNLIKELY (last_dot == NULL))
1157     return FALSE;
1158
1159   return TRUE;
1160 }
1161
1162 /**
1163  * Checks that the given range of the string is a valid message type
1164  * signature in the D-Bus protocol.
1165  *
1166  * @todo this is inconsistent with most of DBusString in that
1167  * it allows a start,len range that extends past the string end.
1168  *
1169  * @param str the string
1170  * @param start first byte index to check
1171  * @param len number of bytes to check
1172  * @returns #TRUE if the byte range exists and is a valid signature
1173  */
1174 dbus_bool_t
1175 _dbus_validate_signature (const DBusString  *str,
1176                           int                start,
1177                           int                len)
1178 {
1179   _dbus_assert (start >= 0);
1180   _dbus_assert (start <= _dbus_string_get_length (str));
1181   _dbus_assert (len >= 0);
1182
1183   if (len > _dbus_string_get_length (str) - start)
1184     return FALSE;
1185
1186   return _dbus_validate_signature_with_reason (str, start, len) == DBUS_VALID;
1187 }
1188
1189 /** define _dbus_check_is_valid_path() */
1190 DEFINE_DBUS_NAME_CHECK(path)
1191 /** define _dbus_check_is_valid_interface() */
1192 DEFINE_DBUS_NAME_CHECK(interface)
1193 /** define _dbus_check_is_valid_member() */
1194 DEFINE_DBUS_NAME_CHECK(member)
1195 /** define _dbus_check_is_valid_error_name() */
1196 DEFINE_DBUS_NAME_CHECK(error_name)
1197 /** define _dbus_check_is_valid_bus_name() */
1198 DEFINE_DBUS_NAME_CHECK(bus_name)
1199 /** define _dbus_check_is_valid_signature() */
1200 DEFINE_DBUS_NAME_CHECK(signature)
1201
1202 /** @} */
1203
1204 /* tests in dbus-marshal-validate-util.c */