1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.
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.
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.
20 * Author: David Zeuthen <davidz@redhat.com>
28 #include "gdbusutils.h"
35 * @title: D-Bus Utilities
36 * @short_description: Various utilities related to D-Bus.
39 * Various utility routines related to D-Bus.
43 is_valid_bus_name_character (gint c,
44 gboolean allow_hyphen)
47 (c >= '0' && c <= '9') ||
48 (c >= 'A' && c <= 'Z') ||
49 (c >= 'a' && c <= 'z') ||
51 (allow_hyphen && c == '-');
55 is_valid_initial_bus_name_character (gint c,
56 gboolean allow_initial_digit,
57 gboolean allow_hyphen)
59 if (allow_initial_digit)
60 return is_valid_bus_name_character (c, allow_hyphen);
63 (c >= 'A' && c <= 'Z') ||
64 (c >= 'a' && c <= 'z') ||
66 (allow_hyphen && c == '-');
70 is_valid_name (const gchar *start,
72 gboolean allow_initial_digit,
73 gboolean allow_hyphen)
93 if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, allow_initial_digit, allow_hyphen)))
97 else if (G_UNLIKELY (!is_valid_bus_name_character (*s, allow_hyphen)))
104 if (G_UNLIKELY (!has_dot))
115 * @string: The string to check.
117 * Checks if @string is a valid D-Bus bus name (either unique or well-known).
119 * Returns: %TRUE if valid, %FALSE otherwise.
124 g_dbus_is_name (const gchar *string)
131 g_return_val_if_fail (string != NULL, FALSE);
135 len = strlen (string);
136 if (G_UNLIKELY (len == 0 || len > 255))
143 /* handle unique name */
144 if (!is_valid_name (s + 1, len - 1, TRUE, TRUE))
149 else if (G_UNLIKELY (*s == '.'))
151 /* can't start with a . */
154 else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, TRUE)))
157 ret = is_valid_name (s + 1, len - 1, FALSE, TRUE);
164 * g_dbus_is_unique_name:
165 * @string: The string to check.
167 * Checks if @string is a valid D-Bus unique bus name.
169 * Returns: %TRUE if valid, %FALSE otherwise.
174 g_dbus_is_unique_name (const gchar *string)
179 g_return_val_if_fail (string != NULL, FALSE);
183 len = strlen (string);
184 if (G_UNLIKELY (len == 0 || len > 255))
187 if (G_UNLIKELY (*string != ':'))
190 if (G_UNLIKELY (!is_valid_name (string + 1, len - 1, TRUE, TRUE)))
200 * g_dbus_is_member_name:
201 * @string: The string to check.
203 * Checks if @string is a valid D-Bus member (e.g. signal or method) name.
205 * Returns: %TRUE if valid, %FALSE otherwise.
210 g_dbus_is_member_name (const gchar *string)
216 if (G_UNLIKELY (string == NULL))
219 if (G_UNLIKELY (!is_valid_initial_bus_name_character (string[0], FALSE, FALSE)))
222 for (n = 1; string[n] != '\0'; n++)
224 if (G_UNLIKELY (!is_valid_bus_name_character (string[n], FALSE)))
237 * g_dbus_is_interface_name:
238 * @string: The string to check.
240 * Checks if @string is a valid D-Bus interface name.
242 * Returns: %TRUE if valid, %FALSE otherwise.
247 g_dbus_is_interface_name (const gchar *string)
254 g_return_val_if_fail (string != NULL, FALSE);
258 len = strlen (string);
259 if (G_UNLIKELY (len == 0 || len > 255))
264 if (G_UNLIKELY (*s == '.'))
266 /* can't start with a . */
269 else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, FALSE)))
272 ret = is_valid_name (s + 1, len - 1, FALSE, FALSE);
278 /* ---------------------------------------------------------------------------------------------------- */
280 /* TODO: maybe move to glib? if so, it should conform to http://en.wikipedia.org/wiki/Guid and/or
281 * http://tools.ietf.org/html/rfc4122 - specifically it should have hyphens then.
285 * g_dbus_generate_guid:
287 * Generate a D-Bus GUID that can be used with
288 * e.g. g_dbus_connection_new().
290 * See the D-Bus specification regarding what strings are valid D-Bus
291 * GUID (for example, D-Bus GUIDs are not RFC-4122 compliant).
293 * Returns: A valid D-Bus GUID. Free with g_free().
298 g_dbus_generate_guid (void)
306 s = g_string_new (NULL);
308 r1 = g_random_int ();
309 r2 = g_random_int ();
310 r3 = g_random_int ();
311 g_get_current_time (&now);
313 g_string_append_printf (s, "%08x", r1);
314 g_string_append_printf (s, "%08x", r2);
315 g_string_append_printf (s, "%08x", r3);
316 g_string_append_printf (s, "%08x", (guint32) now.tv_sec);
318 return g_string_free (s, FALSE);
323 * @string: The string to check.
325 * Checks if @string is a D-Bus GUID.
327 * See the D-Bus specification regarding what strings are valid D-Bus
328 * GUID (for example, D-Bus GUIDs are not RFC-4122 compliant).
330 * Returns: %TRUE if @string is a guid, %FALSE otherwise.
335 g_dbus_is_guid (const gchar *string)
340 g_return_val_if_fail (string != NULL, FALSE);
344 for (n = 0; n < 32; n++)
346 if (!g_ascii_isxdigit (string[n]))
349 if (string[32] != '\0')
358 /* ---------------------------------------------------------------------------------------------------- */
360 #define __G_DBUS_UTILS_C__
361 #include "gioaliasdef.c"