1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-syntax.c - utility functions for strings with special syntax
4 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
5 * Copyright © 2011 Nokia Corporation
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "dbus-syntax.h"
28 #include "dbus-internals.h"
29 #include "dbus-marshal-validate.h"
30 #include "dbus-shared.h"
33 * @defgroup DBusSyntax Utility functions for strings with special syntax
35 * @brief Parsing D-Bus type signatures
40 * Check an object path for validity. Remember that #NULL can always
41 * be passed instead of a DBusError *, if you don't care about having
42 * an error name and message.
44 * This function is suitable for validating C strings, but is not suitable
45 * for validating untrusted data from a network unless the string's length
46 * is also checked, since it assumes that the string ends at the first zero
47 * byte according to normal C conventions.
49 * @param path a potentially invalid object path, which must not be #NULL
50 * @param error error return
51 * @returns #TRUE if path is valid
54 dbus_validate_path (const char *path,
60 _dbus_return_val_if_fail (path != NULL, FALSE);
62 _dbus_string_init_const (&str, path);
63 len = _dbus_string_get_length (&str);
65 /* In general, it ought to be valid... */
66 if (_DBUS_LIKELY (_dbus_validate_path (&str, 0, len)))
69 /* slow path: string is invalid, find out why */
71 if (!_dbus_string_validate_utf8 (&str, 0, len))
73 /* don't quote the actual string here, since a DBusError also needs to
75 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
76 "Object path was not valid UTF-8");
80 /* FIXME: later, diagnose exactly how it was invalid */
81 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
82 "Object path was not valid: '%s'", path);
87 * Check an interface name for validity. Remember that #NULL can always
88 * be passed instead of a DBusError *, if you don't care about having
89 * an error name and message.
91 * This function is suitable for validating C strings, but is not suitable
92 * for validating untrusted data from a network unless the string's length
93 * is also checked, since it assumes that the string ends at the first zero
94 * byte according to normal C conventions.
96 * @param path a potentially invalid interface name, which must not be #NULL
97 * @param error error return
98 * @returns #TRUE if name is valid
101 dbus_validate_interface (const char *name,
107 _dbus_return_val_if_fail (name != NULL, FALSE);
109 _dbus_string_init_const (&str, name);
110 len = _dbus_string_get_length (&str);
112 /* In general, it ought to be valid... */
113 if (_DBUS_LIKELY (_dbus_validate_interface (&str, 0, len)))
116 /* slow path: string is invalid, find out why */
118 if (!_dbus_string_validate_utf8 (&str, 0, len))
120 /* don't quote the actual string here, since a DBusError also needs to
122 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
123 "Interface name was not valid UTF-8");
127 /* FIXME: later, diagnose exactly how it was invalid */
128 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
129 "Interface name was not valid: '%s'", name);
134 * Check a member (method/signal) name for validity. Remember that #NULL
135 * can always be passed instead of a DBusError *, if you don't care about
136 * having an error name and message.
138 * This function is suitable for validating C strings, but is not suitable
139 * for validating untrusted data from a network unless the string's length
140 * is also checked, since it assumes that the string ends at the first zero
141 * byte according to normal C conventions.
143 * @param path a potentially invalid member name, which must not be #NULL
144 * @param error error return
145 * @returns #TRUE if name is valid
148 dbus_validate_member (const char *name,
154 _dbus_return_val_if_fail (name != NULL, FALSE);
156 _dbus_string_init_const (&str, name);
157 len = _dbus_string_get_length (&str);
159 /* In general, it ought to be valid... */
160 if (_DBUS_LIKELY (_dbus_validate_member (&str, 0, len)))
163 /* slow path: string is invalid, find out why */
165 if (!_dbus_string_validate_utf8 (&str, 0, len))
167 /* don't quote the actual string here, since a DBusError also needs to
169 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
170 "Member name was not valid UTF-8");
174 /* FIXME: later, diagnose exactly how it was invalid */
175 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
176 "Member name was not valid: '%s'", name);
181 * Check an error name for validity. Remember that #NULL
182 * can always be passed instead of a DBusError *, if you don't care about
183 * having an error name and message.
185 * This function is suitable for validating C strings, but is not suitable
186 * for validating untrusted data from a network unless the string's length
187 * is also checked, since it assumes that the string ends at the first zero
188 * byte according to normal C conventions.
190 * @param path a potentially invalid error name, which must not be #NULL
191 * @param error error return
192 * @returns #TRUE if name is valid
195 dbus_validate_error_name (const char *name,
201 _dbus_return_val_if_fail (name != NULL, FALSE);
203 _dbus_string_init_const (&str, name);
204 len = _dbus_string_get_length (&str);
206 /* In general, it ought to be valid... */
207 if (_DBUS_LIKELY (_dbus_validate_error_name (&str, 0, len)))
210 /* slow path: string is invalid, find out why */
212 if (!_dbus_string_validate_utf8 (&str, 0, len))
214 /* don't quote the actual string here, since a DBusError also needs to
216 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
217 "Error name was not valid UTF-8");
221 /* FIXME: later, diagnose exactly how it was invalid */
222 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
223 "Error name was not valid: '%s'", name);
228 * Check a bus name for validity. Remember that #NULL
229 * can always be passed instead of a DBusError *, if you don't care about
230 * having an error name and message.
232 * This function is suitable for validating C strings, but is not suitable
233 * for validating untrusted data from a network unless the string's length
234 * is also checked, since it assumes that the string ends at the first zero
235 * byte according to normal C conventions.
237 * @param path a potentially invalid bus name, which must not be #NULL
238 * @param error error return
239 * @returns #TRUE if name is valid
242 dbus_validate_bus_name (const char *name,
248 _dbus_return_val_if_fail (name != NULL, FALSE);
250 _dbus_string_init_const (&str, name);
251 len = _dbus_string_get_length (&str);
253 /* In general, it ought to be valid... */
254 if (_DBUS_LIKELY (_dbus_validate_bus_name (&str, 0, len)))
257 /* slow path: string is invalid, find out why */
259 if (!_dbus_string_validate_utf8 (&str, 0, len))
261 /* don't quote the actual string here, since a DBusError also needs to
263 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
264 "Bus name was not valid UTF-8");
268 /* FIXME: later, diagnose exactly how it was invalid */
269 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
270 "Bus name was not valid: '%s'", name);
275 * Check a string for validity. Strings on D-Bus must be valid UTF-8.
276 * Remember that #NULL can always be passed instead of a DBusError *,
277 * if you don't care about having an error name and message.
279 * This function is suitable for validating C strings, but is not suitable
280 * for validating untrusted data from a network unless the string's length
281 * is also checked, since it assumes that the string ends at the first zero
282 * byte according to normal C conventions.
284 * @param alleged_utf8 a string to be checked, which must not be #NULL
285 * @param error error return
286 * @returns #TRUE if alleged_utf8 is valid UTF-8
289 dbus_validate_utf8 (const char *alleged_utf8,
294 _dbus_return_val_if_fail (alleged_utf8 != NULL, FALSE);
296 _dbus_string_init_const (&str, alleged_utf8);
298 if (_DBUS_LIKELY (_dbus_string_validate_utf8 (&str, 0,
299 _dbus_string_get_length (&str))))
302 /* don't quote the actual string here, since a DBusError also needs to
304 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
305 "String was not valid UTF-8");
309 /** @} */ /* end of group */