1 /* Implementation of various validation functions for use in dbus-python.
3 * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
5 * Licensed under the Academic Free License version 2.1
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus_bindings-internal.h"
28 dbus_py_validate_bus_name(const char *name
,
29 dbus_bool_t may_be_unique
,
30 dbus_bool_t may_be_not_unique
)
32 dbus_bool_t dot
= FALSE
;
37 if (name
[0] == '\0') {
38 PyErr_SetString(PyExc_ValueError
, "Invalid bus name: "
42 unique
= (name
[0] == ':');
43 if (unique
&& !may_be_unique
) {
44 PyErr_Format(PyExc_ValueError
, "Invalid well-known bus name '%s':"
45 "only unique names may start with ':'", name
);
48 if (!unique
&& !may_be_not_unique
) {
49 PyErr_Format(PyExc_ValueError
, "Invalid unique bus name '%s': "
50 "unique names must start with ':'", name
);
53 if (strlen(name
) > 255) {
54 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': "
55 "too long (> 255 characters)", name
);
59 for (ptr
= name
+ (unique
? 1 : 0); *ptr
; ptr
++) {
63 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': "
64 "contains substring '..'", name
);
67 else if (last
== '\0') {
68 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': "
69 "must not start with '.'", name
);
73 else if (*ptr
>= '0' && *ptr
<= '9') {
76 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': "
77 "a digit may not follow '.' except in a "
78 "unique name starting with ':'", name
);
81 else if (last
== '\0') {
82 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': "
83 "must not start with a digit", name
);
88 else if ((*ptr
< 'a' || *ptr
> 'z') &&
89 (*ptr
< 'A' || *ptr
> 'Z') && *ptr
!= '_' && *ptr
!= '-') {
90 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': "
91 "contains invalid character '%c'", name
, *ptr
);
97 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': must "
98 "not end with '.'", name
);
102 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': must "
103 "contain '.'", name
);
110 dbus_py_validate_member_name(const char *name
)
114 if (name
[0] == '\0') {
115 PyErr_SetString(PyExc_ValueError
, "Invalid member name: may not "
119 if (strlen(name
) > 255) {
120 PyErr_Format(PyExc_ValueError
, "Invalid member name '%s': "
121 "too long (> 255 characters)", name
);
124 for (ptr
= name
; *ptr
; ptr
++) {
125 if (*ptr
>= '0' && *ptr
<= '9') {
127 PyErr_Format(PyExc_ValueError
, "Invalid member name '%s': "
128 "must not start with a digit", name
);
132 else if ((*ptr
< 'a' || *ptr
> 'z') &&
133 (*ptr
< 'A' || *ptr
> 'Z') && *ptr
!= '_') {
134 PyErr_Format(PyExc_ValueError
, "Invalid member name '%s': "
135 "contains invalid character '%c'", name
, *ptr
);
143 dbus_py_validate_interface_name(const char *name
)
145 dbus_bool_t dot
= FALSE
;
149 if (name
[0] == '\0') {
150 PyErr_SetString(PyExc_ValueError
, "Invalid interface or error name: "
154 if (strlen(name
) > 255) {
155 PyErr_Format(PyExc_ValueError
, "Invalid interface or error name '%s': "
156 "too long (> 255 characters)", name
);
160 for (ptr
= name
; *ptr
; ptr
++) {
164 PyErr_Format(PyExc_ValueError
, "Invalid interface or "
165 "error name '%s': contains substring '..'", name
);
168 else if (last
== '\0') {
169 PyErr_Format(PyExc_ValueError
, "Invalid interface or error "
170 "name '%s': must not start with '.'", name
);
174 else if (*ptr
>= '0' && *ptr
<= '9') {
176 PyErr_Format(PyExc_ValueError
, "Invalid interface or error "
177 "name '%s': a digit may not follow '.'", name
);
180 else if (last
== '\0') {
181 PyErr_Format(PyExc_ValueError
, "Invalid interface or error "
182 "name '%s': must not start with a digit", name
);
186 else if ((*ptr
< 'a' || *ptr
> 'z') &&
187 (*ptr
< 'A' || *ptr
> 'Z') && *ptr
!= '_') {
188 PyErr_Format(PyExc_ValueError
, "Invalid interface or error "
189 "name '%s': contains invalid character '%c'",
196 PyErr_Format(PyExc_ValueError
, "Invalid interface or error name "
197 "'%s': must not end with '.'", name
);
201 PyErr_Format(PyExc_ValueError
, "Invalid interface or error name "
202 "'%s': must contain '.'", name
);
210 dbus_py_validate_object_path(const char *path
)
214 if (path
[0] != '/') {
215 PyErr_Format(PyExc_ValueError
, "Invalid object path '%s': does not "
216 "start with '/'", path
);
219 if (path
[1] == '\0') return TRUE
;
220 for (ptr
= path
+ 1; *ptr
; ptr
++) {
222 if (ptr
[-1] == '/') {
223 PyErr_Format(PyExc_ValueError
, "Invalid object path '%s': "
224 "contains substring '//'", path
);
228 else if ((*ptr
< 'a' || *ptr
> 'z') &&
229 (*ptr
< 'A' || *ptr
> 'Z') &&
230 (*ptr
< '0' || *ptr
> '9') && *ptr
!= '_') {
231 PyErr_Format(PyExc_ValueError
, "Invalid object path '%s': "
232 "contains invalid character '%c'", path
, *ptr
);
236 if (ptr
[-1] == '/') {
237 PyErr_Format(PyExc_ValueError
, "Invalid object path '%s': ends "
238 "with '/' and is not just '/'", path
);
244 /* vim:set ft=c cino< sw=4 sts=4 et: */