2005-02-05 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-marshal-validate.h
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal-validate.h  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 #ifndef DBUS_MARSHAL_VALIDATE_H
25 #define DBUS_MARSHAL_VALIDATE_H
26
27 #include <config.h>
28
29 #ifndef PACKAGE
30 #error "config.h not included here"
31 #endif
32
33 /**
34  * This is used rather than a bool for high visibility
35  */
36 typedef enum
37 {
38   DBUS_VALIDATION_MODE_WE_TRUST_THIS_DATA_ABSOLUTELY,
39   DBUS_VALIDATION_MODE_DATA_IS_UNTRUSTED
40 } DBusValidationMode;
41
42 /**
43  * This is primarily used in unit testing, so we can verify that each
44  * invalid message is invalid for the expected reasons. Thus we really
45  * want a distinct enum value for every codepath leaving the validator
46  * functions. Enum values are specified manually for ease of debugging
47  * (so you can see the enum value given a printf)
48  */
49 typedef enum
50 {
51 #define _DBUS_NEGATIVE_VALIDITY_COUNT 3
52   DBUS_INVALID_FOR_UNKNOWN_REASON = -3,
53   DBUS_VALID_BUT_INCOMPLETE = -2,
54   DBUS_VALIDITY_UNKNOWN = -1,
55   DBUS_VALID = 0,
56   DBUS_INVALID_UNKNOWN_TYPECODE = 1,
57   DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE = 2,
58   DBUS_INVALID_SIGNATURE_TOO_LONG = 3,
59   DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION = 4,
60   DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION = 5,
61   DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED = 6,
62   DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED = 7,
63   DBUS_INVALID_STRUCT_HAS_NO_FIELDS = 8,
64   DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL = 9,
65   DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE = 10,
66   DBUS_INVALID_NOT_ENOUGH_DATA = 11,
67   DBUS_INVALID_TOO_MUCH_DATA = 12,
68   DBUS_INVALID_BAD_BYTE_ORDER = 13,
69   DBUS_INVALID_BAD_PROTOCOL_VERSION = 14,
70   DBUS_INVALID_BAD_MESSAGE_TYPE = 15,
71   DBUS_INVALID_BAD_SERIAL = 16,
72   DBUS_INVALID_INSANE_FIELDS_ARRAY_LENGTH = 17,
73   DBUS_INVALID_INSANE_BODY_LENGTH = 18,
74   DBUS_INVALID_MESSAGE_TOO_LONG = 19,
75   DBUS_INVALID_HEADER_FIELD_CODE = 20,
76   DBUS_INVALID_HEADER_FIELD_HAS_WRONG_TYPE = 21,
77   DBUS_INVALID_USES_LOCAL_INTERFACE = 22,
78   DBUS_INVALID_USES_LOCAL_PATH = 23,
79   DBUS_INVALID_HEADER_FIELD_APPEARS_TWICE = 24,
80   DBUS_INVALID_BAD_DESTINATION = 25,
81   DBUS_INVALID_BAD_INTERFACE = 26,
82   DBUS_INVALID_BAD_MEMBER = 27,
83   DBUS_INVALID_BAD_ERROR_NAME = 28,
84   DBUS_INVALID_BAD_SENDER = 29,
85   DBUS_INVALID_MISSING_PATH = 30,
86   DBUS_INVALID_MISSING_INTERFACE = 31,
87   DBUS_INVALID_MISSING_MEMBER = 32,
88   DBUS_INVALID_MISSING_ERROR_NAME = 33,
89   DBUS_INVALID_MISSING_REPLY_SERIAL = 34,
90   DBUS_INVALID_STRING_LENGTH_OUT_OF_BOUNDS = 35,
91   DBUS_INVALID_ARRAY_LENGTH_OUT_OF_BOUNDS = 36,
92   DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM = 37,
93   DBUS_INVALID_BAD_PATH = 38,
94   DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS = 39,
95   DBUS_INVALID_BAD_UTF8_IN_STRING = 40,
96   DBUS_INVALID_ARRAY_LENGTH_INCORRECT = 41,
97   DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS = 42,
98   DBUS_INVALID_VARIANT_SIGNATURE_BAD = 43,
99   DBUS_INVALID_VARIANT_SIGNATURE_EMPTY = 44,
100   DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES = 45,
101   DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL = 46,
102   DBUS_INVALID_STRING_MISSING_NUL = 47,
103   DBUS_INVALID_SIGNATURE_MISSING_NUL = 48,
104   DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION = 49,
105   DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED = 50,
106   DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED = 51,
107   DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS = 52,
108   DBUS_INVALID_DICT_ENTRY_HAS_ONLY_ONE_FIELD = 53,
109   DBUS_INVALID_DICT_ENTRY_HAS_TOO_MANY_FIELDS = 54,
110   DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY = 55,
111   DBUS_VALIDITY_LAST
112 } DBusValidity;
113
114 DBusValidity _dbus_validate_signature_with_reason (const DBusString *type_str,
115                                                    int               type_pos,
116                                                    int               len);
117 DBusValidity _dbus_validate_body_with_reason      (const DBusString *expected_signature,
118                                                    int               expected_signature_start,
119                                                    int               byte_order,
120                                                    int              *bytes_remaining,
121                                                    const DBusString *value_str,
122                                                    int               value_pos,
123                                                    int               len);
124
125 dbus_bool_t _dbus_validate_path       (const DBusString *str,
126                                        int               start,
127                                        int               len);
128 dbus_bool_t _dbus_validate_interface  (const DBusString *str,
129                                        int               start,
130                                        int               len);
131 dbus_bool_t _dbus_validate_member     (const DBusString *str,
132                                        int               start,
133                                        int               len);
134 dbus_bool_t _dbus_validate_error_name (const DBusString *str,
135                                        int               start,
136                                        int               len);
137 dbus_bool_t _dbus_validate_bus_name   (const DBusString *str,
138                                        int               start,
139                                        int               len);
140 dbus_bool_t _dbus_validate_signature  (const DBusString *str,
141                                        int               start,
142                                        int               len);
143
144 #ifdef DBUS_DISABLE_CHECKS
145
146 /* Be sure they don't exist, since we don't want to use them outside of checks
147  * and so we want the compile failure.
148  */
149 #define DECLARE_DBUS_NAME_CHECK(what)
150 #define DEFINE_DBUS_NAME_CHECK(what)
151
152 #else /* !DBUS_DISABLE_CHECKS */
153
154 /** A name check is used in _dbus_return_if_fail(), it's not suitable
155  * for validating untrusted data. use _dbus_validate_##what for that.
156  */
157 #define DECLARE_DBUS_NAME_CHECK(what) \
158 dbus_bool_t _dbus_check_is_valid_##what (const char *name)
159
160 /** Define a name check to be used in _dbus_return_if_fail() statements.
161  */
162 #define DEFINE_DBUS_NAME_CHECK(what)                                    \
163 dbus_bool_t                                                             \
164 _dbus_check_is_valid_##what (const char *name)                          \
165 {                                                                       \
166   DBusString str;                                                       \
167                                                                         \
168   if (name == NULL)                                                     \
169     return FALSE;                                                       \
170                                                                         \
171   _dbus_string_init_const (&str, name);                                 \
172   return _dbus_validate_##what (&str, 0,                                \
173                                 _dbus_string_get_length (&str));        \
174 }
175 #endif /* !DBUS_DISABLE_CHECKS */
176
177 DECLARE_DBUS_NAME_CHECK(path);
178 DECLARE_DBUS_NAME_CHECK(interface);
179 DECLARE_DBUS_NAME_CHECK(member);
180 DECLARE_DBUS_NAME_CHECK(error_name);
181 DECLARE_DBUS_NAME_CHECK(bus_name);
182 DECLARE_DBUS_NAME_CHECK(signature);
183
184 #endif /* DBUS_MARSHAL_VALIDATE_H */