Merge remote-tracking branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gdbusutils.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "gdbusutils.h"
29
30 #include "glibintl.h"
31
32 /**
33  * SECTION:gdbusutils
34  * @title: D-Bus Utilities
35  * @short_description: Various utilities related to D-Bus.
36  * @include: gio/gio.h
37  *
38  * Various utility routines related to D-Bus.
39  */
40
41 static gboolean
42 is_valid_bus_name_character (gint c,
43                              gboolean allow_hyphen)
44 {
45   return
46     (c >= '0' && c <= '9') ||
47     (c >= 'A' && c <= 'Z') ||
48     (c >= 'a' && c <= 'z') ||
49     (c == '_') ||
50     (allow_hyphen && c == '-');
51 }
52
53 static gboolean
54 is_valid_initial_bus_name_character (gint c,
55                                      gboolean allow_initial_digit,
56                                      gboolean allow_hyphen)
57 {
58   if (allow_initial_digit)
59     return is_valid_bus_name_character (c, allow_hyphen);
60   else
61     return
62       (c >= 'A' && c <= 'Z') ||
63       (c >= 'a' && c <= 'z') ||
64       (c == '_') ||
65       (allow_hyphen && c == '-');
66 }
67
68 static gboolean
69 is_valid_name (const gchar *start,
70                guint len,
71                gboolean allow_initial_digit,
72                gboolean allow_hyphen)
73 {
74   gboolean ret;
75   const gchar *s;
76   const gchar *end;
77   gboolean has_dot;
78
79   ret = FALSE;
80
81   if (len == 0)
82     goto out;
83
84   s = start;
85   end = s + len;
86   has_dot = FALSE;
87   while (s != end)
88     {
89       if (*s == '.')
90         {
91           s += 1;
92           if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, allow_initial_digit, allow_hyphen)))
93             goto out;
94           has_dot = TRUE;
95         }
96       else if (G_UNLIKELY (!is_valid_bus_name_character (*s, allow_hyphen)))
97         {
98           goto out;
99         }
100       s += 1;
101     }
102
103   if (G_UNLIKELY (!has_dot))
104     goto out;
105
106   ret = TRUE;
107
108  out:
109   return ret;
110 }
111
112 /**
113  * g_dbus_is_name:
114  * @string: The string to check.
115  *
116  * Checks if @string is a valid D-Bus bus name (either unique or well-known).
117  *
118  * Returns: %TRUE if valid, %FALSE otherwise.
119  *
120  * Since: 2.26
121  */
122 gboolean
123 g_dbus_is_name (const gchar *string)
124 {
125   guint len;
126   gboolean ret;
127   const gchar *s;
128
129   g_return_val_if_fail (string != NULL, FALSE);
130
131   ret = FALSE;
132
133   len = strlen (string);
134   if (G_UNLIKELY (len == 0 || len > 255))
135     goto out;
136
137   s = string;
138   if (*s == ':')
139     {
140       /* handle unique name */
141       if (!is_valid_name (s + 1, len - 1, TRUE, TRUE))
142         goto out;
143       ret = TRUE;
144       goto out;
145     }
146   else if (G_UNLIKELY (*s == '.'))
147     {
148       /* can't start with a . */
149       goto out;
150     }
151   else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, TRUE)))
152     goto out;
153
154   ret = is_valid_name (s + 1, len - 1, FALSE, TRUE);
155
156  out:
157   return ret;
158 }
159
160 /**
161  * g_dbus_is_unique_name:
162  * @string: The string to check.
163  *
164  * Checks if @string is a valid D-Bus unique bus name.
165  *
166  * Returns: %TRUE if valid, %FALSE otherwise.
167  *
168  * Since: 2.26
169  */
170 gboolean
171 g_dbus_is_unique_name (const gchar *string)
172 {
173   gboolean ret;
174   guint len;
175
176   g_return_val_if_fail (string != NULL, FALSE);
177
178   ret = FALSE;
179
180   len = strlen (string);
181   if (G_UNLIKELY (len == 0 || len > 255))
182     goto out;
183
184   if (G_UNLIKELY (*string != ':'))
185     goto out;
186
187   if (G_UNLIKELY (!is_valid_name (string + 1, len - 1, TRUE, TRUE)))
188     goto out;
189
190   ret = TRUE;
191
192  out:
193   return ret;
194 }
195
196 /**
197  * g_dbus_is_member_name:
198  * @string: The string to check.
199  *
200  * Checks if @string is a valid D-Bus member (e.g. signal or method) name.
201  *
202  * Returns: %TRUE if valid, %FALSE otherwise.
203  *
204  * Since: 2.26
205  */
206 gboolean
207 g_dbus_is_member_name (const gchar *string)
208 {
209   gboolean ret;
210   guint n;
211
212   ret = FALSE;
213   if (G_UNLIKELY (string == NULL))
214     goto out;
215
216   if (G_UNLIKELY (!is_valid_initial_bus_name_character (string[0], FALSE, FALSE)))
217     goto out;
218
219   for (n = 1; string[n] != '\0'; n++)
220     {
221       if (G_UNLIKELY (!is_valid_bus_name_character (string[n], FALSE)))
222         {
223           goto out;
224         }
225     }
226
227   ret = TRUE;
228
229  out:
230   return ret;
231 }
232
233 /**
234  * g_dbus_is_interface_name:
235  * @string: The string to check.
236  *
237  * Checks if @string is a valid D-Bus interface name.
238  *
239  * Returns: %TRUE if valid, %FALSE otherwise.
240  *
241  * Since: 2.26
242  */
243 gboolean
244 g_dbus_is_interface_name (const gchar *string)
245 {
246   guint len;
247   gboolean ret;
248   const gchar *s;
249
250   g_return_val_if_fail (string != NULL, FALSE);
251
252   ret = FALSE;
253
254   len = strlen (string);
255   if (G_UNLIKELY (len == 0 || len > 255))
256     goto out;
257
258   s = string;
259   if (G_UNLIKELY (*s == '.'))
260     {
261       /* can't start with a . */
262       goto out;
263     }
264   else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, FALSE)))
265     goto out;
266
267   ret = is_valid_name (s + 1, len - 1, FALSE, FALSE);
268
269  out:
270   return ret;
271 }
272
273 /* ---------------------------------------------------------------------------------------------------- */
274
275 /* TODO: maybe move to glib? if so, it should conform to http://en.wikipedia.org/wiki/Guid and/or
276  *       http://tools.ietf.org/html/rfc4122 - specifically it should have hyphens then.
277  */
278
279 /**
280  * g_dbus_generate_guid:
281  *
282  * Generate a D-Bus GUID that can be used with
283  * e.g. g_dbus_connection_new().
284  *
285  * See the D-Bus specification regarding what strings are valid D-Bus
286  * GUID (for example, D-Bus GUIDs are not RFC-4122 compliant).
287  *
288  * Returns: A valid D-Bus GUID. Free with g_free().
289  *
290  * Since: 2.26
291  */
292 gchar *
293 g_dbus_generate_guid (void)
294 {
295   GString *s;
296   GTimeVal now;
297   guint32 r1;
298   guint32 r2;
299   guint32 r3;
300
301   s = g_string_new (NULL);
302
303   r1 = g_random_int ();
304   r2 = g_random_int ();
305   r3 = g_random_int ();
306   g_get_current_time (&now);
307
308   g_string_append_printf (s, "%08x", r1);
309   g_string_append_printf (s, "%08x", r2);
310   g_string_append_printf (s, "%08x", r3);
311   g_string_append_printf (s, "%08x", (guint32) now.tv_sec);
312
313   return g_string_free (s, FALSE);
314 }
315
316 /**
317  * g_dbus_is_guid:
318  * @string: The string to check.
319  *
320  * Checks if @string is a D-Bus GUID.
321  *
322  * See the D-Bus specification regarding what strings are valid D-Bus
323  * GUID (for example, D-Bus GUIDs are not RFC-4122 compliant).
324  *
325  * Returns: %TRUE if @string is a guid, %FALSE otherwise.
326  *
327  * Since: 2.26
328  */
329 gboolean
330 g_dbus_is_guid (const gchar *string)
331 {
332   gboolean ret;
333   guint n;
334
335   g_return_val_if_fail (string != NULL, FALSE);
336
337   ret = FALSE;
338
339   for (n = 0; n < 32; n++)
340     {
341       if (!g_ascii_isxdigit (string[n]))
342         goto out;
343     }
344   if (string[32] != '\0')
345     goto out;
346
347   ret = TRUE;
348
349  out:
350   return ret;
351 }
352
353 /* ---------------------------------------------------------------------------------------------------- */