D-Bus: dbus-syntax.c Source File

D-Bus 1.14.10

dbus-syntax.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-syntax.c - utility functions for strings with special syntax
3 *
4 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
5 * Copyright © 2011 Nokia Corporation
6 *
7 * Licensed under the Academic Free License version 2.1
8 *
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.
13 *
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.
18 *
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
22 *
23 */
24
25#include <config.h>
26#include "dbus-syntax.h"
27
28#include "dbus-internals.h"
29#include "dbus-marshal-validate.h"
30#include "dbus-shared.h"
31
53dbus_bool_t
54dbus_validate_path (const char *path,
55 DBusError *error)
56{
57 DBusString str;
58 int len;
59
60 _dbus_return_val_if_fail (path != NULL, FALSE);
61
62 _dbus_string_init_const (&str, path);
63 len = _dbus_string_get_length (&str);
64
65 /* In general, it ought to be valid... */
66 if (_DBUS_LIKELY (_dbus_validate_path (&str, 0, len)))
67 return TRUE;
68
69 /* slow path: string is invalid, find out why */
70
71 if (!_dbus_string_validate_utf8 (&str, 0, len))
72 {
73 /* don't quote the actual string here, since a DBusError also needs to
74 * be valid UTF-8 */
75 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
76 "Object path was not valid UTF-8");
77 return FALSE;
78 }
79
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);
83 return FALSE;
84}
85
100dbus_bool_t
101dbus_validate_interface (const char *name,
102 DBusError *error)
103{
104 DBusString str;
105 int len;
106
107 _dbus_return_val_if_fail (name != NULL, FALSE);
108
109 _dbus_string_init_const (&str, name);
110 len = _dbus_string_get_length (&str);
111
112 /* In general, it ought to be valid... */
113 if (_DBUS_LIKELY (_dbus_validate_interface (&str, 0, len)))
114 return TRUE;
115
116 /* slow path: string is invalid, find out why */
117
118 if (!_dbus_string_validate_utf8 (&str, 0, len))
119 {
120 /* don't quote the actual string here, since a DBusError also needs to
121 * be valid UTF-8 */
122 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
123 "Interface name was not valid UTF-8");
124 return FALSE;
125 }
126
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);
130 return FALSE;
131}
132
147dbus_bool_t
148dbus_validate_member (const char *name,
149 DBusError *error)
150{
151 DBusString str;
152 int len;
153
154 _dbus_return_val_if_fail (name != NULL, FALSE);
155
156 _dbus_string_init_const (&str, name);
157 len = _dbus_string_get_length (&str);
158
159 /* In general, it ought to be valid... */
160 if (_DBUS_LIKELY (_dbus_validate_member (&str, 0, len)))
161 return TRUE;
162
163 /* slow path: string is invalid, find out why */
164
165 if (!_dbus_string_validate_utf8 (&str, 0, len))
166 {
167 /* don't quote the actual string here, since a DBusError also needs to
168 * be valid UTF-8 */
169 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
170 "Member name was not valid UTF-8");
171 return FALSE;
172 }
173
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);
177 return FALSE;
178}
179
194dbus_bool_t
195dbus_validate_error_name (const char *name,
196 DBusError *error)
197{
198 DBusString str;
199 int len;
200
201 _dbus_return_val_if_fail (name != NULL, FALSE);
202
203 _dbus_string_init_const (&str, name);
204 len = _dbus_string_get_length (&str);
205
206 /* In general, it ought to be valid... */
207 if (_DBUS_LIKELY (_dbus_validate_error_name (&str, 0, len)))
208 return TRUE;
209
210 /* slow path: string is invalid, find out why */
211
212 if (!_dbus_string_validate_utf8 (&str, 0, len))
213 {
214 /* don't quote the actual string here, since a DBusError also needs to
215 * be valid UTF-8 */
216 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
217 "Error name was not valid UTF-8");
218 return FALSE;
219 }
220
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);
224 return FALSE;
225}
226
241dbus_bool_t
242dbus_validate_bus_name (const char *name,
243 DBusError *error)
244{
245 DBusString str;
246 int len;
247
248 _dbus_return_val_if_fail (name != NULL, FALSE);
249
250 _dbus_string_init_const (&str, name);
251 len = _dbus_string_get_length (&str);
252
253 /* In general, it ought to be valid... */
254 if (_DBUS_LIKELY (_dbus_validate_bus_name (&str, 0, len)))
255 return TRUE;
256
257 /* slow path: string is invalid, find out why */
258
259 if (!_dbus_string_validate_utf8 (&str, 0, len))
260 {
261 /* don't quote the actual string here, since a DBusError also needs to
262 * be valid UTF-8 */
263 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
264 "Bus name was not valid UTF-8");
265 return FALSE;
266 }
267
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);
271 return FALSE;
272}
273
288dbus_bool_t
289dbus_validate_utf8 (const char *alleged_utf8,
290 DBusError *error)
291{
292 DBusString str;
293
294 _dbus_return_val_if_fail (alleged_utf8 != NULL, FALSE);
295
296 _dbus_string_init_const (&str, alleged_utf8);
297
298 if (_DBUS_LIKELY (_dbus_string_validate_utf8 (&str, 0,
299 _dbus_string_get_length (&str))))
300 return TRUE;
301
302 /* don't quote the actual string here, since a DBusError also needs to
303 * be valid UTF-8 */
304 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
305 "String was not valid UTF-8");
306 return FALSE;
307}
308 /* end of group */
dbus_set_error
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition dbus-errors.c:354
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:49
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:39
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:42
_dbus_validate_error_name
dbus_bool_t _dbus_validate_error_name(const DBusString *str, int start, int len)
Checks that the given range of the string is a valid error name in the D-Bus protocol.
Definition dbus-marshal-validate.c:1112
_dbus_validate_member
dbus_bool_t _dbus_validate_member(const DBusString *str, int start, int len)
Checks that the given range of the string is a valid member name in the D-Bus protocol.
Definition dbus-marshal-validate.c:1051
_dbus_validate_path
dbus_bool_t _dbus_validate_path(const DBusString *str, int start, int len)
Checks that the given range of the string is a valid object path name in the D-Bus protocol.
Definition dbus-marshal-validate.c:837
_dbus_validate_interface
dbus_bool_t _dbus_validate_interface(const DBusString *str, int start, int len)
Checks that the given range of the string is a valid interface name in the D-Bus protocol.
Definition dbus-marshal-validate.c:975
_dbus_validate_bus_name
dbus_bool_t _dbus_validate_bus_name(const DBusString *str, int start, int len)
Checks that the given range of the string is a valid bus name in the D-Bus protocol.
Definition dbus-marshal-validate.c:1241
DBUS_ERROR_INVALID_ARGS
#define DBUS_ERROR_INVALID_ARGS
Invalid arguments passed to a method call.
Definition dbus-protocol.h:395
_dbus_string_init_const
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition dbus-string.c:197
_dbus_string_validate_utf8
dbus_bool_t _dbus_string_validate_utf8(const DBusString *str, int start, int len)
Checks that the given range of the string is valid UTF-8.
Definition dbus-string.c:2641
dbus_validate_interface
dbus_bool_t dbus_validate_interface(const char *name, DBusError *error)
Check an interface name for validity.
Definition dbus-syntax.c:101
dbus_validate_path
dbus_bool_t dbus_validate_path(const char *path, DBusError *error)
Check an object path for validity.
Definition dbus-syntax.c:54
dbus_validate_utf8
dbus_bool_t dbus_validate_utf8(const char *alleged_utf8, DBusError *error)
Check a string for validity.
Definition dbus-syntax.c:289
dbus_validate_member
dbus_bool_t dbus_validate_member(const char *name, DBusError *error)
Check a member (method/signal) name for validity.
Definition dbus-syntax.c:148
dbus_validate_bus_name
dbus_bool_t dbus_validate_bus_name(const char *name, DBusError *error)
Check a bus name for validity.
Definition dbus-syntax.c:242
dbus_validate_error_name
dbus_bool_t dbus_validate_error_name(const char *name, DBusError *error)
Check an error name for validity.
Definition dbus-syntax.c:195
DBusError
Object representing an exception.
Definition dbus-errors.h:49
DBusString
Definition dbus-string.h:43