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