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 library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus_bindings-internal.h"
26 dbus_py_validate_bus_name(const char *name
,
27 dbus_bool_t may_be_unique
,
28 dbus_bool_t may_be_not_unique
)
30 dbus_bool_t dot
= FALSE
;
35 if (name
[0] == '\0') {
36 PyErr_SetString(PyExc_ValueError
, "Invalid bus name: "
40 unique
= (name
[0] == ':');
41 if (unique
&& !may_be_unique
) {
42 PyErr_Format(PyExc_ValueError
, "Invalid well-known bus name '%s':"
43 "only unique names may start with ':'", name
);
46 if (!unique
&& !may_be_not_unique
) {
47 PyErr_Format(PyExc_ValueError
, "Invalid unique bus name '%s': "
48 "unique names must start with ':'", name
);
51 if (strlen(name
) > 255) {
52 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': "
53 "too long (> 255 characters)", name
);
57 for (ptr
= name
+ (unique
? 1 : 0); *ptr
; ptr
++) {
61 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': "
62 "contains substring '..'", name
);
65 else if (last
== '\0') {
66 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': "
67 "must not start with '.'", name
);
71 else if (*ptr
>= '0' && *ptr
<= '9') {
74 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': "
75 "a digit may not follow '.' except in a "
76 "unique name starting with ':'", name
);
79 else if (last
== '\0') {
80 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': "
81 "must not start with a digit", name
);
86 else if ((*ptr
< 'a' || *ptr
> 'z') &&
87 (*ptr
< 'A' || *ptr
> 'Z') && *ptr
!= '_' && *ptr
!= '-') {
88 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': "
89 "contains invalid character '%c'", name
, *ptr
);
95 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': must "
96 "not end with '.'", name
);
100 PyErr_Format(PyExc_ValueError
, "Invalid bus name '%s': must "
101 "contain '.'", name
);
108 dbus_py_validate_member_name(const char *name
)
112 if (name
[0] == '\0') {
113 PyErr_SetString(PyExc_ValueError
, "Invalid member name: may not "
117 if (strlen(name
) > 255) {
118 PyErr_Format(PyExc_ValueError
, "Invalid member name '%s': "
119 "too long (> 255 characters)", name
);
122 for (ptr
= name
; *ptr
; ptr
++) {
123 if (*ptr
>= '0' && *ptr
<= '9') {
125 PyErr_Format(PyExc_ValueError
, "Invalid member name '%s': "
126 "must not start with a digit", name
);
130 else if ((*ptr
< 'a' || *ptr
> 'z') &&
131 (*ptr
< 'A' || *ptr
> 'Z') && *ptr
!= '_') {
132 PyErr_Format(PyExc_ValueError
, "Invalid member name '%s': "
133 "contains invalid character '%c'", name
, *ptr
);
141 dbus_py_validate_interface_name(const char *name
)
143 dbus_bool_t dot
= FALSE
;
147 if (name
[0] == '\0') {
148 PyErr_SetString(PyExc_ValueError
, "Invalid interface or error name: "
152 if (strlen(name
) > 255) {
153 PyErr_Format(PyExc_ValueError
, "Invalid interface or error name '%s': "
154 "too long (> 255 characters)", name
);
158 for (ptr
= name
; *ptr
; ptr
++) {
162 PyErr_Format(PyExc_ValueError
, "Invalid interface or "
163 "error name '%s': contains substring '..'", name
);
166 else if (last
== '\0') {
167 PyErr_Format(PyExc_ValueError
, "Invalid interface or error "
168 "name '%s': must not start with '.'", name
);
172 else if (*ptr
>= '0' && *ptr
<= '9') {
174 PyErr_Format(PyExc_ValueError
, "Invalid interface or error "
175 "name '%s': a digit may not follow '.'", name
);
178 else if (last
== '\0') {
179 PyErr_Format(PyExc_ValueError
, "Invalid interface or error "
180 "name '%s': must not start with a digit", name
);
184 else if ((*ptr
< 'a' || *ptr
> 'z') &&
185 (*ptr
< 'A' || *ptr
> 'Z') && *ptr
!= '_') {
186 PyErr_Format(PyExc_ValueError
, "Invalid interface or error "
187 "name '%s': contains invalid character '%c'",
194 PyErr_Format(PyExc_ValueError
, "Invalid interface or error name "
195 "'%s': must not end with '.'", name
);
199 PyErr_Format(PyExc_ValueError
, "Invalid interface or error name "
200 "'%s': must contain '.'", name
);
208 dbus_py_validate_object_path(const char *path
)
212 if (path
[0] != '/') {
213 PyErr_Format(PyExc_ValueError
, "Invalid object path '%s': does not "
214 "start with '/'", path
);
217 if (path
[1] == '\0') return TRUE
;
218 for (ptr
= path
+ 1; *ptr
; ptr
++) {
220 if (ptr
[-1] == '/') {
221 PyErr_Format(PyExc_ValueError
, "Invalid object path '%s': "
222 "contains substring '//'", path
);
226 else if ((*ptr
< 'a' || *ptr
> 'z') &&
227 (*ptr
< 'A' || *ptr
> 'Z') &&
228 (*ptr
< '0' || *ptr
> '9') && *ptr
!= '_') {
229 PyErr_Format(PyExc_ValueError
, "Invalid object path '%s': "
230 "contains invalid character '%c'", path
, *ptr
);
234 if (ptr
[-1] == '/') {
235 PyErr_Format(PyExc_ValueError
, "Invalid object path '%s': ends "
236 "with '/' and is not just '/'", path
);
242 /* vim:set ft=c cino< sw=4 sts=4 et: */