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