1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (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 GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: David Zeuthen <davidz@redhat.com>
28 #include "gdbusutils.h"
34 * @title: D-Bus Utilities
35 * @short_description: Various utilities related to D-Bus
38 * Various utility routines related to D-Bus.
42 is_valid_bus_name_character (gint c,
43 gboolean allow_hyphen)
46 (c >= '0' && c <= '9') ||
47 (c >= 'A' && c <= 'Z') ||
48 (c >= 'a' && c <= 'z') ||
50 (allow_hyphen && c == '-');
54 is_valid_initial_bus_name_character (gint c,
55 gboolean allow_initial_digit,
56 gboolean allow_hyphen)
58 if (allow_initial_digit)
59 return is_valid_bus_name_character (c, allow_hyphen);
62 (c >= 'A' && c <= 'Z') ||
63 (c >= 'a' && c <= 'z') ||
65 (allow_hyphen && c == '-');
69 is_valid_name (const gchar *start,
71 gboolean allow_initial_digit,
72 gboolean allow_hyphen)
92 if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, allow_initial_digit, allow_hyphen)))
96 else if (G_UNLIKELY (!is_valid_bus_name_character (*s, allow_hyphen)))
103 if (G_UNLIKELY (!has_dot))
114 * @string: The string to check.
116 * Checks if @string is a valid D-Bus bus name (either unique or well-known).
118 * Returns: %TRUE if valid, %FALSE otherwise.
123 g_dbus_is_name (const gchar *string)
129 g_return_val_if_fail (string != NULL, FALSE);
133 len = strlen (string);
134 if (G_UNLIKELY (len == 0 || len > 255))
140 /* handle unique name */
141 if (!is_valid_name (s + 1, len - 1, TRUE, TRUE))
146 else if (G_UNLIKELY (*s == '.'))
148 /* can't start with a . */
151 else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, TRUE)))
154 ret = is_valid_name (s + 1, len - 1, FALSE, TRUE);
161 * g_dbus_is_unique_name:
162 * @string: The string to check.
164 * Checks if @string is a valid D-Bus unique bus name.
166 * Returns: %TRUE if valid, %FALSE otherwise.
171 g_dbus_is_unique_name (const gchar *string)
176 g_return_val_if_fail (string != NULL, FALSE);
180 len = strlen (string);
181 if (G_UNLIKELY (len == 0 || len > 255))
184 if (G_UNLIKELY (*string != ':'))
187 if (G_UNLIKELY (!is_valid_name (string + 1, len - 1, TRUE, TRUE)))
197 * g_dbus_is_member_name:
198 * @string: The string to check.
200 * Checks if @string is a valid D-Bus member (e.g. signal or method) name.
202 * Returns: %TRUE if valid, %FALSE otherwise.
207 g_dbus_is_member_name (const gchar *string)
213 if (G_UNLIKELY (string == NULL))
216 if (G_UNLIKELY (!is_valid_initial_bus_name_character (string[0], FALSE, FALSE)))
219 for (n = 1; string[n] != '\0'; n++)
221 if (G_UNLIKELY (!is_valid_bus_name_character (string[n], FALSE)))
234 * g_dbus_is_interface_name:
235 * @string: The string to check.
237 * Checks if @string is a valid D-Bus interface name.
239 * Returns: %TRUE if valid, %FALSE otherwise.
244 g_dbus_is_interface_name (const gchar *string)
250 g_return_val_if_fail (string != NULL, FALSE);
254 len = strlen (string);
255 if (G_UNLIKELY (len == 0 || len > 255))
259 if (G_UNLIKELY (*s == '.'))
261 /* can't start with a . */
264 else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, FALSE)))
267 ret = is_valid_name (s + 1, len - 1, FALSE, FALSE);
274 * g_dbus_is_error_name:
275 * @string: The string to check.
277 * Check whether @string is a valid D-Bus error name.
279 * This function returns the same result as g_dbus_is_interface_name(),
280 * because D-Bus error names are defined to have exactly the
281 * same syntax as interface names.
283 * Returns: %TRUE if valid, %FALSE otherwise.
288 g_dbus_is_error_name (const gchar *string)
290 /* Error names are the same syntax as interface names.
291 * See https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-error */
292 return g_dbus_is_interface_name (string);
295 /* ---------------------------------------------------------------------------------------------------- */
297 /* TODO: maybe move to glib? if so, it should conform to http://en.wikipedia.org/wiki/Guid and/or
298 * http://tools.ietf.org/html/rfc4122 - specifically it should have hyphens then.
302 * g_dbus_generate_guid:
304 * Generate a D-Bus GUID that can be used with
305 * e.g. g_dbus_connection_new().
308 * [D-Bus specification](https://dbus.freedesktop.org/doc/dbus-specification.html#uuids)
309 * regarding what strings are valid D-Bus GUIDs. The specification refers to
310 * these as ‘UUIDs’ whereas GLib (for historical reasons) refers to them as
311 * ‘GUIDs’. The terms are interchangeable.
313 * Note that D-Bus GUIDs do not follow
314 * [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122).
316 * Returns: A valid D-Bus GUID. Free with g_free().
321 g_dbus_generate_guid (void)
329 s = g_string_new (NULL);
331 r1 = g_random_int ();
332 r2 = g_random_int ();
333 r3 = g_random_int ();
334 now_us = g_get_real_time ();
336 g_string_append_printf (s, "%08x", r1);
337 g_string_append_printf (s, "%08x", r2);
338 g_string_append_printf (s, "%08x", r3);
339 g_string_append_printf (s, "%08x", (guint32) (now_us / G_USEC_PER_SEC));
341 return g_string_free (s, FALSE);
346 * @string: The string to check.
348 * Checks if @string is a D-Bus GUID.
350 * See the documentation for g_dbus_generate_guid() for more information about
351 * the format of a GUID.
353 * Returns: %TRUE if @string is a GUID, %FALSE otherwise.
358 g_dbus_is_guid (const gchar *string)
363 g_return_val_if_fail (string != NULL, FALSE);
367 for (n = 0; n < 32; n++)
369 if (!g_ascii_isxdigit (string[n]))
372 if (string[32] != '\0')
381 /* ---------------------------------------------------------------------------------------------------- */
384 * g_dbus_gvariant_to_gvalue:
385 * @value: A #GVariant.
386 * @out_gvalue: (out): Return location pointing to a zero-filled (uninitialized) #GValue.
388 * Converts a #GVariant to a #GValue. If @value is floating, it is consumed.
390 * The rules specified in the g_dbus_gvalue_to_gvariant() function are
391 * used - this function is essentially its reverse form. So, a #GVariant
392 * containing any basic or string array type will be converted to a #GValue
393 * containing a basic value or string array. Any other #GVariant (handle,
394 * variant, tuple, dict entry) will be converted to a #GValue containing that
397 * The conversion never fails - a valid #GValue is always returned in
403 g_dbus_gvariant_to_gvalue (GVariant *value,
406 const GVariantType *type;
409 g_return_if_fail (value != NULL);
410 g_return_if_fail (out_gvalue != NULL);
412 memset (out_gvalue, '\0', sizeof (GValue));
414 switch (g_variant_classify (value))
416 case G_VARIANT_CLASS_BOOLEAN:
417 g_value_init (out_gvalue, G_TYPE_BOOLEAN);
418 g_value_set_boolean (out_gvalue, g_variant_get_boolean (value));
421 case G_VARIANT_CLASS_BYTE:
422 g_value_init (out_gvalue, G_TYPE_UCHAR);
423 g_value_set_uchar (out_gvalue, g_variant_get_byte (value));
426 case G_VARIANT_CLASS_INT16:
427 g_value_init (out_gvalue, G_TYPE_INT);
428 g_value_set_int (out_gvalue, g_variant_get_int16 (value));
431 case G_VARIANT_CLASS_UINT16:
432 g_value_init (out_gvalue, G_TYPE_UINT);
433 g_value_set_uint (out_gvalue, g_variant_get_uint16 (value));
436 case G_VARIANT_CLASS_INT32:
437 g_value_init (out_gvalue, G_TYPE_INT);
438 g_value_set_int (out_gvalue, g_variant_get_int32 (value));
441 case G_VARIANT_CLASS_UINT32:
442 g_value_init (out_gvalue, G_TYPE_UINT);
443 g_value_set_uint (out_gvalue, g_variant_get_uint32 (value));
446 case G_VARIANT_CLASS_INT64:
447 g_value_init (out_gvalue, G_TYPE_INT64);
448 g_value_set_int64 (out_gvalue, g_variant_get_int64 (value));
451 case G_VARIANT_CLASS_UINT64:
452 g_value_init (out_gvalue, G_TYPE_UINT64);
453 g_value_set_uint64 (out_gvalue, g_variant_get_uint64 (value));
456 case G_VARIANT_CLASS_DOUBLE:
457 g_value_init (out_gvalue, G_TYPE_DOUBLE);
458 g_value_set_double (out_gvalue, g_variant_get_double (value));
461 case G_VARIANT_CLASS_STRING:
462 g_value_init (out_gvalue, G_TYPE_STRING);
463 g_value_set_string (out_gvalue, g_variant_get_string (value, NULL));
466 case G_VARIANT_CLASS_OBJECT_PATH:
467 g_value_init (out_gvalue, G_TYPE_STRING);
468 g_value_set_string (out_gvalue, g_variant_get_string (value, NULL));
471 case G_VARIANT_CLASS_SIGNATURE:
472 g_value_init (out_gvalue, G_TYPE_STRING);
473 g_value_set_string (out_gvalue, g_variant_get_string (value, NULL));
476 case G_VARIANT_CLASS_ARRAY:
477 type = g_variant_get_type (value);
478 switch (g_variant_type_peek_string (type)[1])
480 case G_VARIANT_CLASS_BYTE:
481 g_value_init (out_gvalue, G_TYPE_STRING);
482 g_value_set_string (out_gvalue, g_variant_get_bytestring (value));
485 case G_VARIANT_CLASS_STRING:
486 g_value_init (out_gvalue, G_TYPE_STRV);
487 array = g_variant_dup_strv (value, NULL);
488 g_value_take_boxed (out_gvalue, array);
491 case G_VARIANT_CLASS_OBJECT_PATH:
492 g_value_init (out_gvalue, G_TYPE_STRV);
493 array = g_variant_dup_objv (value, NULL);
494 g_value_take_boxed (out_gvalue, array);
497 case G_VARIANT_CLASS_ARRAY:
498 switch (g_variant_type_peek_string (type)[2])
500 case G_VARIANT_CLASS_BYTE:
501 g_value_init (out_gvalue, G_TYPE_STRV);
502 array = g_variant_dup_bytestring_array (value, NULL);
503 g_value_take_boxed (out_gvalue, array);
507 g_value_init (out_gvalue, G_TYPE_VARIANT);
508 g_value_set_variant (out_gvalue, value);
514 g_value_init (out_gvalue, G_TYPE_VARIANT);
515 g_value_set_variant (out_gvalue, value);
520 case G_VARIANT_CLASS_HANDLE:
521 case G_VARIANT_CLASS_VARIANT:
522 case G_VARIANT_CLASS_MAYBE:
523 case G_VARIANT_CLASS_TUPLE:
524 case G_VARIANT_CLASS_DICT_ENTRY:
525 g_value_init (out_gvalue, G_TYPE_VARIANT);
526 g_value_set_variant (out_gvalue, value);
533 * g_dbus_gvalue_to_gvariant:
534 * @gvalue: A #GValue to convert to a #GVariant
535 * @type: A #GVariantType
537 * Converts a #GValue to a #GVariant of the type indicated by the @type
540 * The conversion is using the following rules:
542 * - `G_TYPE_STRING`: 's', 'o', 'g' or 'ay'
543 * - `G_TYPE_STRV`: 'as', 'ao' or 'aay'
544 * - `G_TYPE_BOOLEAN`: 'b'
545 * - `G_TYPE_UCHAR`: 'y'
546 * - `G_TYPE_INT`: 'i', 'n'
547 * - `G_TYPE_UINT`: 'u', 'q'
548 * - `G_TYPE_INT64`: 'x'
549 * - `G_TYPE_UINT64`: 't'
550 * - `G_TYPE_DOUBLE`: 'd'
551 * - `G_TYPE_VARIANT`: Any #GVariantType
553 * This can fail if e.g. @gvalue is of type %G_TYPE_STRING and @type
554 * is 'i', i.e. %G_VARIANT_TYPE_INT32. It will also fail for any #GType
555 * (including e.g. %G_TYPE_OBJECT and %G_TYPE_BOXED derived-types) not
556 * in the table above.
558 * Note that if @gvalue is of type %G_TYPE_VARIANT and its value is
559 * %NULL, the empty #GVariant instance (never %NULL) for @type is
560 * returned (e.g. 0 for scalar types, the empty string for string types,
561 * '/' for object path types, the empty array for any array type and so on).
563 * See the g_dbus_gvariant_to_gvalue() function for how to convert a
564 * #GVariant to a #GValue.
566 * Returns: (transfer full): A #GVariant (never floating) of
567 * #GVariantType @type holding the data from @gvalue or an empty #GVariant
568 * in case of failure. Free with g_variant_unref().
573 g_dbus_gvalue_to_gvariant (const GValue *gvalue,
574 const GVariantType *type)
578 const gchar * const *as;
579 const gchar *empty_strv[1] = {NULL};
581 g_return_val_if_fail (gvalue != NULL, NULL);
582 g_return_val_if_fail (type != NULL, NULL);
586 /* @type can easily be e.g. "s" with the GValue holding a GVariant - for example this
587 * can happen when using the org.gtk.GDBus.C.ForceGVariant annotation with the
588 * gdbus-codegen(1) tool.
590 if (G_VALUE_TYPE (gvalue) == G_TYPE_VARIANT)
592 ret = g_value_dup_variant (gvalue);
596 switch (g_variant_type_peek_string (type)[0])
598 case G_VARIANT_CLASS_BOOLEAN:
599 ret = g_variant_ref_sink (g_variant_new_boolean (g_value_get_boolean (gvalue)));
602 case G_VARIANT_CLASS_BYTE:
603 ret = g_variant_ref_sink (g_variant_new_byte (g_value_get_uchar (gvalue)));
606 case G_VARIANT_CLASS_INT16:
607 ret = g_variant_ref_sink (g_variant_new_int16 (g_value_get_int (gvalue)));
610 case G_VARIANT_CLASS_UINT16:
611 ret = g_variant_ref_sink (g_variant_new_uint16 (g_value_get_uint (gvalue)));
614 case G_VARIANT_CLASS_INT32:
615 ret = g_variant_ref_sink (g_variant_new_int32 (g_value_get_int (gvalue)));
618 case G_VARIANT_CLASS_UINT32:
619 ret = g_variant_ref_sink (g_variant_new_uint32 (g_value_get_uint (gvalue)));
622 case G_VARIANT_CLASS_INT64:
623 ret = g_variant_ref_sink (g_variant_new_int64 (g_value_get_int64 (gvalue)));
626 case G_VARIANT_CLASS_UINT64:
627 ret = g_variant_ref_sink (g_variant_new_uint64 (g_value_get_uint64 (gvalue)));
630 case G_VARIANT_CLASS_DOUBLE:
631 ret = g_variant_ref_sink (g_variant_new_double (g_value_get_double (gvalue)));
634 case G_VARIANT_CLASS_STRING:
635 s = g_value_get_string (gvalue);
638 ret = g_variant_ref_sink (g_variant_new_string (s));
641 case G_VARIANT_CLASS_OBJECT_PATH:
642 s = g_value_get_string (gvalue);
645 ret = g_variant_ref_sink (g_variant_new_object_path (s));
648 case G_VARIANT_CLASS_SIGNATURE:
649 s = g_value_get_string (gvalue);
652 ret = g_variant_ref_sink (g_variant_new_signature (s));
655 case G_VARIANT_CLASS_ARRAY:
656 switch (g_variant_type_peek_string (type)[1])
658 case G_VARIANT_CLASS_BYTE:
659 s = g_value_get_string (gvalue);
662 ret = g_variant_ref_sink (g_variant_new_bytestring (s));
665 case G_VARIANT_CLASS_STRING:
666 as = g_value_get_boxed (gvalue);
669 ret = g_variant_ref_sink (g_variant_new_strv (as, -1));
672 case G_VARIANT_CLASS_OBJECT_PATH:
673 as = g_value_get_boxed (gvalue);
676 ret = g_variant_ref_sink (g_variant_new_objv (as, -1));
679 case G_VARIANT_CLASS_ARRAY:
680 switch (g_variant_type_peek_string (type)[2])
682 case G_VARIANT_CLASS_BYTE:
683 as = g_value_get_boxed (gvalue);
686 ret = g_variant_ref_sink (g_variant_new_bytestring_array (as, -1));
690 ret = g_value_dup_variant (gvalue);
696 ret = g_value_dup_variant (gvalue);
701 case G_VARIANT_CLASS_HANDLE:
702 case G_VARIANT_CLASS_VARIANT:
703 case G_VARIANT_CLASS_MAYBE:
704 case G_VARIANT_CLASS_TUPLE:
705 case G_VARIANT_CLASS_DICT_ENTRY:
706 ret = g_value_dup_variant (gvalue);
711 /* Could be that the GValue is holding a NULL GVariant - in that case,
712 * we return an "empty" GVariant instead of a NULL GVariant
716 GVariant *untrusted_empty;
717 untrusted_empty = g_variant_new_from_data (type, NULL, 0, FALSE, NULL, NULL);
718 ret = g_variant_take_ref (g_variant_get_normal_form (untrusted_empty));
719 g_variant_unref (untrusted_empty);
722 g_assert (!g_variant_is_floating (ret));
728 * g_dbus_escape_object_path_bytestring:
729 * @bytes: (array zero-terminated=1) (element-type guint8): the string of bytes to escape
731 * Escapes @bytes for use in a D-Bus object path component.
732 * @bytes is an array of zero or more nonzero bytes in an
733 * unspecified encoding, followed by a single zero byte.
735 * The escaping method consists of replacing all non-alphanumeric
736 * characters (see g_ascii_isalnum()) with their hexadecimal value
737 * preceded by an underscore (`_`). For example:
738 * `foo.bar.baz` will become `foo_2ebar_2ebaz`.
740 * This method is appropriate to use when the input is nearly
741 * a valid object path component but is not when your input
742 * is far from being a valid object path component.
743 * Other escaping algorithms are also valid to use with
744 * D-Bus object paths.
746 * This can be reversed with g_dbus_unescape_object_path().
748 * Returns: an escaped version of @bytes. Free with g_free().
754 g_dbus_escape_object_path_bytestring (const guint8 *bytes)
759 g_return_val_if_fail (bytes != NULL, NULL);
762 return g_strdup ("_");
764 escaped = g_string_new (NULL);
765 for (p = bytes; *p; p++)
767 if (g_ascii_isalnum (*p))
768 g_string_append_c (escaped, *p);
770 g_string_append_printf (escaped, "_%02x", *p);
773 return g_string_free (escaped, FALSE);
777 * g_dbus_escape_object_path:
778 * @s: the string to escape
780 * This is a language binding friendly version of g_dbus_escape_object_path_bytestring().
782 * Returns: an escaped version of @s. Free with g_free().
787 g_dbus_escape_object_path (const gchar *s)
789 return (gchar *) g_dbus_escape_object_path_bytestring ((const guint8 *) s);
793 * g_dbus_unescape_object_path:
794 * @s: the string to unescape
796 * Unescapes an string that was previously escaped with
797 * g_dbus_escape_object_path(). If the string is in a format that could
798 * not have been returned by g_dbus_escape_object_path(), this function
801 * Encoding alphanumeric characters which do not need to be
802 * encoded is not allowed (e.g `_63` is not valid, the string
803 * should contain `c` instead).
805 * Returns: (array zero-terminated=1) (element-type guint8) (nullable): an
806 * unescaped version of @s, or %NULL if @s is not a string returned
807 * from g_dbus_escape_object_path(). Free with g_free().
812 g_dbus_unescape_object_path (const gchar *s)
817 g_return_val_if_fail (s != NULL, NULL);
819 if (g_str_equal (s, "_"))
820 return (guint8 *) g_strdup ("");
822 unescaped = g_string_new (NULL);
827 if (g_ascii_isalnum (*p))
829 g_string_append_c (unescaped, *p);
831 else if (*p == '_' &&
832 ((hi = g_ascii_xdigit_value (p[1])) >= 0) &&
833 ((lo = g_ascii_xdigit_value (p[2])) >= 0) &&
834 (hi || lo) && /* \0 is not allowed */
835 !g_ascii_isalnum ((hi << 4) | lo)) /* alnums must not be encoded */
837 g_string_append_c (unescaped, (hi << 4) | lo);
842 /* the string was not encoded correctly */
843 g_string_free (unescaped, TRUE);
848 return (guint8 *) g_string_free (unescaped, FALSE);