Change LGPL-2.1+ to LGPL-2.1-or-later
[platform/upstream/glib.git] / gio / gdbusutils.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
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.
11  *
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.
16  *
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/>.
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  * g_dbus_is_error_name:
275  * @string: The string to check.
276  *
277  * Check whether @string is a valid D-Bus error name.
278  *
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.
282  *
283  * Returns: %TRUE if valid, %FALSE otherwise.
284  *
285  * Since: 2.70
286  */
287 gboolean
288 g_dbus_is_error_name (const gchar *string)
289 {
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);
293 }
294
295 /* ---------------------------------------------------------------------------------------------------- */
296
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.
299  */
300
301 /**
302  * g_dbus_generate_guid:
303  *
304  * Generate a D-Bus GUID that can be used with
305  * e.g. g_dbus_connection_new().
306  *
307  * See the
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.
312  *
313  * Note that D-Bus GUIDs do not follow
314  * [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122).
315  *
316  * Returns: A valid D-Bus GUID. Free with g_free().
317  *
318  * Since: 2.26
319  */
320 gchar *
321 g_dbus_generate_guid (void)
322 {
323   GString *s;
324   guint32 r1;
325   guint32 r2;
326   guint32 r3;
327   gint64 now_us;
328
329   s = g_string_new (NULL);
330
331   r1 = g_random_int ();
332   r2 = g_random_int ();
333   r3 = g_random_int ();
334   now_us = g_get_real_time ();
335
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));
340
341   return g_string_free (s, FALSE);
342 }
343
344 /**
345  * g_dbus_is_guid:
346  * @string: The string to check.
347  *
348  * Checks if @string is a D-Bus GUID.
349  *
350  * See the documentation for g_dbus_generate_guid() for more information about
351  * the format of a GUID.
352  *
353  * Returns: %TRUE if @string is a GUID, %FALSE otherwise.
354  *
355  * Since: 2.26
356  */
357 gboolean
358 g_dbus_is_guid (const gchar *string)
359 {
360   gboolean ret;
361   guint n;
362
363   g_return_val_if_fail (string != NULL, FALSE);
364
365   ret = FALSE;
366
367   for (n = 0; n < 32; n++)
368     {
369       if (!g_ascii_isxdigit (string[n]))
370         goto out;
371     }
372   if (string[32] != '\0')
373     goto out;
374
375   ret = TRUE;
376
377  out:
378   return ret;
379 }
380
381 /* ---------------------------------------------------------------------------------------------------- */
382
383 /**
384  * g_dbus_gvariant_to_gvalue:
385  * @value: A #GVariant.
386  * @out_gvalue: (out): Return location pointing to a zero-filled (uninitialized) #GValue.
387  *
388  * Converts a #GVariant to a #GValue. If @value is floating, it is consumed.
389  *
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
395  * #GVariant.
396  *
397  * The conversion never fails - a valid #GValue is always returned in
398  * @out_gvalue.
399  *
400  * Since: 2.30
401  */
402 void
403 g_dbus_gvariant_to_gvalue (GVariant  *value,
404                            GValue    *out_gvalue)
405 {
406   const GVariantType *type;
407   gchar **array;
408
409   g_return_if_fail (value != NULL);
410   g_return_if_fail (out_gvalue != NULL);
411
412   memset (out_gvalue, '\0', sizeof (GValue));
413
414   switch (g_variant_classify (value))
415     {
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));
419       break;
420
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));
424       break;
425
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));
429       break;
430
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));
434       break;
435
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));
439       break;
440
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));
444       break;
445
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));
449       break;
450
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));
454       break;
455
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));
459       break;
460
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));
464       break;
465
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));
469       break;
470
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));
474       break;
475
476     case G_VARIANT_CLASS_ARRAY:
477       type = g_variant_get_type (value);
478       switch (g_variant_type_peek_string (type)[1])
479         {
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));
483           break;
484
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);
489           break;
490
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);
495           break;
496
497         case G_VARIANT_CLASS_ARRAY:
498           switch (g_variant_type_peek_string (type)[2])
499             {
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);
504               break;
505
506             default:
507               g_value_init (out_gvalue, G_TYPE_VARIANT);
508               g_value_set_variant (out_gvalue, value);
509               break;
510             }
511           break;
512
513         default:
514           g_value_init (out_gvalue, G_TYPE_VARIANT);
515           g_value_set_variant (out_gvalue, value);
516           break;
517         }
518       break;
519
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);
527       break;
528     }
529 }
530
531
532 /**
533  * g_dbus_gvalue_to_gvariant:
534  * @gvalue: A #GValue to convert to a #GVariant
535  * @type: A #GVariantType
536  *
537  * Converts a #GValue to a #GVariant of the type indicated by the @type
538  * parameter.
539  *
540  * The conversion is using the following rules:
541  *
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
552  *
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.
557  *
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).
562  *
563  * See the g_dbus_gvariant_to_gvalue() function for how to convert a
564  * #GVariant to a #GValue.
565  *
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().
569  *
570  * Since: 2.30
571  */
572 GVariant *
573 g_dbus_gvalue_to_gvariant (const GValue       *gvalue,
574                            const GVariantType *type)
575 {
576   GVariant *ret;
577   const gchar *s;
578   const gchar * const *as;
579   const gchar *empty_strv[1] = {NULL};
580
581   g_return_val_if_fail (gvalue != NULL, NULL);
582   g_return_val_if_fail (type != NULL, NULL);
583
584   ret = NULL;
585
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.
589    */
590   if (G_VALUE_TYPE (gvalue) == G_TYPE_VARIANT)
591     {
592       ret = g_value_dup_variant (gvalue);
593     }
594   else
595     {
596       switch (g_variant_type_peek_string (type)[0])
597         {
598         case G_VARIANT_CLASS_BOOLEAN:
599           ret = g_variant_ref_sink (g_variant_new_boolean (g_value_get_boolean (gvalue)));
600           break;
601
602         case G_VARIANT_CLASS_BYTE:
603           ret = g_variant_ref_sink (g_variant_new_byte (g_value_get_uchar (gvalue)));
604           break;
605
606         case G_VARIANT_CLASS_INT16:
607           ret = g_variant_ref_sink (g_variant_new_int16 (g_value_get_int (gvalue)));
608           break;
609
610         case G_VARIANT_CLASS_UINT16:
611           ret = g_variant_ref_sink (g_variant_new_uint16 (g_value_get_uint (gvalue)));
612           break;
613
614         case G_VARIANT_CLASS_INT32:
615           ret = g_variant_ref_sink (g_variant_new_int32 (g_value_get_int (gvalue)));
616           break;
617
618         case G_VARIANT_CLASS_UINT32:
619           ret = g_variant_ref_sink (g_variant_new_uint32 (g_value_get_uint (gvalue)));
620           break;
621
622         case G_VARIANT_CLASS_INT64:
623           ret = g_variant_ref_sink (g_variant_new_int64 (g_value_get_int64 (gvalue)));
624           break;
625
626         case G_VARIANT_CLASS_UINT64:
627           ret = g_variant_ref_sink (g_variant_new_uint64 (g_value_get_uint64 (gvalue)));
628           break;
629
630         case G_VARIANT_CLASS_DOUBLE:
631           ret = g_variant_ref_sink (g_variant_new_double (g_value_get_double (gvalue)));
632           break;
633
634         case G_VARIANT_CLASS_STRING:
635           s = g_value_get_string (gvalue);
636           if (s == NULL)
637             s = "";
638           ret = g_variant_ref_sink (g_variant_new_string (s));
639           break;
640
641         case G_VARIANT_CLASS_OBJECT_PATH:
642           s = g_value_get_string (gvalue);
643           if (s == NULL)
644             s = "/";
645           ret = g_variant_ref_sink (g_variant_new_object_path (s));
646           break;
647
648         case G_VARIANT_CLASS_SIGNATURE:
649           s = g_value_get_string (gvalue);
650           if (s == NULL)
651             s = "";
652           ret = g_variant_ref_sink (g_variant_new_signature (s));
653           break;
654
655         case G_VARIANT_CLASS_ARRAY:
656           switch (g_variant_type_peek_string (type)[1])
657             {
658             case G_VARIANT_CLASS_BYTE:
659               s = g_value_get_string (gvalue);
660               if (s == NULL)
661                 s = "";
662               ret = g_variant_ref_sink (g_variant_new_bytestring (s));
663               break;
664
665             case G_VARIANT_CLASS_STRING:
666               as = g_value_get_boxed (gvalue);
667               if (as == NULL)
668                 as = empty_strv;
669               ret = g_variant_ref_sink (g_variant_new_strv (as, -1));
670               break;
671
672             case G_VARIANT_CLASS_OBJECT_PATH:
673               as = g_value_get_boxed (gvalue);
674               if (as == NULL)
675                 as = empty_strv;
676               ret = g_variant_ref_sink (g_variant_new_objv (as, -1));
677               break;
678
679             case G_VARIANT_CLASS_ARRAY:
680               switch (g_variant_type_peek_string (type)[2])
681                 {
682                 case G_VARIANT_CLASS_BYTE:
683                   as = g_value_get_boxed (gvalue);
684                   if (as == NULL)
685                     as = empty_strv;
686                   ret = g_variant_ref_sink (g_variant_new_bytestring_array (as, -1));
687                   break;
688
689                 default:
690                   ret = g_value_dup_variant (gvalue);
691                   break;
692                 }
693               break;
694
695             default:
696               ret = g_value_dup_variant (gvalue);
697               break;
698             }
699           break;
700
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);
707           break;
708         }
709     }
710
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
713    */
714   if (ret == NULL)
715     {
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);
720     }
721
722   g_assert (!g_variant_is_floating (ret));
723
724   return ret;
725 }
726
727 /**
728  * g_dbus_escape_object_path_bytestring:
729  * @bytes: (array zero-terminated=1) (element-type guint8): the string of bytes to escape
730  *
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.
734  *
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`.
739  *
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.
745  *
746  * This can be reversed with g_dbus_unescape_object_path().
747  *
748  * Returns: an escaped version of @bytes. Free with g_free().
749  *
750  * Since: 2.68
751  *
752  */
753 gchar *
754 g_dbus_escape_object_path_bytestring (const guint8 *bytes)
755 {
756   GString *escaped;
757   const guint8 *p;
758
759   g_return_val_if_fail (bytes != NULL, NULL);
760
761   if (*bytes == '\0')
762     return g_strdup ("_");
763
764   escaped = g_string_new (NULL);
765   for (p = bytes; *p; p++)
766     {
767       if (g_ascii_isalnum (*p))
768         g_string_append_c (escaped, *p);
769       else
770         g_string_append_printf (escaped, "_%02x", *p);
771     }
772
773   return g_string_free (escaped, FALSE);
774 }
775
776 /**
777  * g_dbus_escape_object_path:
778  * @s: the string to escape
779  *
780  * This is a language binding friendly version of g_dbus_escape_object_path_bytestring().
781  *
782  * Returns: an escaped version of @s. Free with g_free().
783  *
784  * Since: 2.68
785  */
786 gchar *
787 g_dbus_escape_object_path (const gchar *s)
788 {
789   return (gchar *) g_dbus_escape_object_path_bytestring ((const guint8 *) s);
790 }
791
792 /**
793  * g_dbus_unescape_object_path:
794  * @s: the string to unescape
795  *
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
799  * returns %NULL.
800  *
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).
804  *
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().
808  *
809  * Since: 2.68
810  */
811 guint8 *
812 g_dbus_unescape_object_path (const gchar *s)
813 {
814   GString *unescaped;
815   const gchar *p;
816
817   g_return_val_if_fail (s != NULL, NULL);
818
819   if (g_str_equal (s, "_"))
820     return (guint8 *) g_strdup ("");
821
822   unescaped = g_string_new (NULL);
823   for (p = s; *p; p++)
824     {
825       gint hi, lo;
826
827       if (g_ascii_isalnum (*p))
828         {
829           g_string_append_c (unescaped, *p);
830         }
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 */
836         {
837           g_string_append_c (unescaped, (hi << 4) | lo);
838           p += 2;
839         }
840       else
841         {
842           /* the string was not encoded correctly */
843           g_string_free (unescaped, TRUE);
844           return NULL;
845         }
846     }
847
848   return (guint8 *) g_string_free (unescaped, FALSE);
849 }