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