Coverage Report

Created: 2025-07-23 08:13

/src/pango/subprojects/glib/gio/gdbusutils.c
Line
Count
Source (jump to first uncovered line)
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
static gboolean
33
is_valid_bus_name_character (gint c,
34
                             gboolean allow_hyphen)
35
0
{
36
0
  return
37
0
    (c >= '0' && c <= '9') ||
38
0
    (c >= 'A' && c <= 'Z') ||
39
0
    (c >= 'a' && c <= 'z') ||
40
0
    (c == '_') ||
41
0
    (allow_hyphen && c == '-');
42
0
}
43
44
static gboolean
45
is_valid_initial_bus_name_character (gint c,
46
                                     gboolean allow_initial_digit,
47
                                     gboolean allow_hyphen)
48
0
{
49
0
  if (allow_initial_digit)
50
0
    return is_valid_bus_name_character (c, allow_hyphen);
51
0
  else
52
0
    return
53
0
      (c >= 'A' && c <= 'Z') ||
54
0
      (c >= 'a' && c <= 'z') ||
55
0
      (c == '_') ||
56
0
      (allow_hyphen && c == '-');
57
0
}
58
59
static gboolean
60
is_valid_name (const gchar *start,
61
               guint len,
62
               gboolean allow_initial_digit,
63
               gboolean allow_hyphen)
64
0
{
65
0
  gboolean ret;
66
0
  const gchar *s;
67
0
  const gchar *end;
68
0
  gboolean has_dot;
69
70
0
  ret = FALSE;
71
72
0
  if (len == 0)
73
0
    goto out;
74
75
0
  s = start;
76
0
  end = s + len;
77
0
  has_dot = FALSE;
78
0
  while (s != end)
79
0
    {
80
0
      if (*s == '.')
81
0
        {
82
0
          s += 1;
83
0
          if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, allow_initial_digit, allow_hyphen)))
84
0
            goto out;
85
0
          has_dot = TRUE;
86
0
        }
87
0
      else if (G_UNLIKELY (!is_valid_bus_name_character (*s, allow_hyphen)))
88
0
        {
89
0
          goto out;
90
0
        }
91
0
      s += 1;
92
0
    }
93
94
0
  if (G_UNLIKELY (!has_dot))
95
0
    goto out;
96
97
0
  ret = TRUE;
98
99
0
 out:
100
0
  return ret;
101
0
}
102
103
/**
104
 * g_dbus_is_name:
105
 * @string: The string to check.
106
 *
107
 * Checks if @string is a valid D-Bus bus name (either unique or well-known).
108
 *
109
 * Returns: %TRUE if valid, %FALSE otherwise.
110
 *
111
 * Since: 2.26
112
 */
113
gboolean
114
g_dbus_is_name (const gchar *string)
115
0
{
116
0
  size_t len;
117
0
  gboolean ret;
118
0
  const gchar *s;
119
120
0
  g_return_val_if_fail (string != NULL, FALSE);
121
122
0
  ret = FALSE;
123
124
0
  len = strlen (string);
125
0
  if (G_UNLIKELY (len == 0 || len > 255))
126
0
    goto out;
127
128
0
  s = string;
129
0
  if (*s == ':')
130
0
    {
131
      /* handle unique name */
132
0
      if (!is_valid_name (s + 1, len - 1, TRUE, TRUE))
133
0
        goto out;
134
0
      ret = TRUE;
135
0
      goto out;
136
0
    }
137
0
  else if (G_UNLIKELY (*s == '.'))
138
0
    {
139
      /* can't start with a . */
140
0
      goto out;
141
0
    }
142
0
  else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, TRUE)))
143
0
    goto out;
144
145
0
  ret = is_valid_name (s + 1, len - 1, FALSE, TRUE);
146
147
0
 out:
148
0
  return ret;
149
0
}
150
151
/**
152
 * g_dbus_is_unique_name:
153
 * @string: The string to check.
154
 *
155
 * Checks if @string is a valid D-Bus unique bus name.
156
 *
157
 * Returns: %TRUE if valid, %FALSE otherwise.
158
 *
159
 * Since: 2.26
160
 */
161
gboolean
162
g_dbus_is_unique_name (const gchar *string)
163
0
{
164
0
  gboolean ret;
165
0
  guint len;
166
167
0
  g_return_val_if_fail (string != NULL, FALSE);
168
169
0
  ret = FALSE;
170
171
0
  len = strlen (string);
172
0
  if (G_UNLIKELY (len == 0 || len > 255))
173
0
    goto out;
174
175
0
  if (G_UNLIKELY (*string != ':'))
176
0
    goto out;
177
178
0
  if (G_UNLIKELY (!is_valid_name (string + 1, len - 1, TRUE, TRUE)))
179
0
    goto out;
180
181
0
  ret = TRUE;
182
183
0
 out:
184
0
  return ret;
185
0
}
186
187
/**
188
 * g_dbus_is_member_name:
189
 * @string: The string to check.
190
 *
191
 * Checks if @string is a valid D-Bus member (e.g. signal or method) name.
192
 *
193
 * Returns: %TRUE if valid, %FALSE otherwise.
194
 *
195
 * Since: 2.26
196
 */
197
gboolean
198
g_dbus_is_member_name (const gchar *string)
199
0
{
200
0
  gboolean ret;
201
0
  guint n;
202
203
0
  ret = FALSE;
204
0
  if (G_UNLIKELY (string == NULL))
205
0
    goto out;
206
207
0
  if (G_UNLIKELY (!is_valid_initial_bus_name_character (string[0], FALSE, FALSE)))
208
0
    goto out;
209
210
0
  for (n = 1; string[n] != '\0'; n++)
211
0
    {
212
0
      if (G_UNLIKELY (!is_valid_bus_name_character (string[n], FALSE)))
213
0
        {
214
0
          goto out;
215
0
        }
216
0
    }
217
218
0
  ret = TRUE;
219
220
0
 out:
221
0
  return ret;
222
0
}
223
224
/**
225
 * g_dbus_is_interface_name:
226
 * @string: The string to check.
227
 *
228
 * Checks if @string is a valid D-Bus interface name.
229
 *
230
 * Returns: %TRUE if valid, %FALSE otherwise.
231
 *
232
 * Since: 2.26
233
 */
234
gboolean
235
g_dbus_is_interface_name (const gchar *string)
236
0
{
237
0
  guint len;
238
0
  gboolean ret;
239
0
  const gchar *s;
240
241
0
  g_return_val_if_fail (string != NULL, FALSE);
242
243
0
  ret = FALSE;
244
245
0
  len = strlen (string);
246
0
  if (G_UNLIKELY (len == 0 || len > 255))
247
0
    goto out;
248
249
0
  s = string;
250
0
  if (G_UNLIKELY (*s == '.'))
251
0
    {
252
      /* can't start with a . */
253
0
      goto out;
254
0
    }
255
0
  else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, FALSE)))
256
0
    goto out;
257
258
0
  ret = is_valid_name (s + 1, len - 1, FALSE, FALSE);
259
260
0
 out:
261
0
  return ret;
262
0
}
263
264
/**
265
 * g_dbus_is_error_name:
266
 * @string: The string to check.
267
 *
268
 * Check whether @string is a valid D-Bus error name.
269
 *
270
 * This function returns the same result as g_dbus_is_interface_name(),
271
 * because D-Bus error names are defined to have exactly the
272
 * same syntax as interface names.
273
 *
274
 * Returns: %TRUE if valid, %FALSE otherwise.
275
 *
276
 * Since: 2.70
277
 */
278
gboolean
279
g_dbus_is_error_name (const gchar *string)
280
0
{
281
  /* Error names are the same syntax as interface names.
282
   * See https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-error */
283
0
  return g_dbus_is_interface_name (string);
284
0
}
285
286
/* ---------------------------------------------------------------------------------------------------- */
287
288
/* TODO: maybe move to glib? if so, it should conform to http://en.wikipedia.org/wiki/Guid and/or
289
 *       http://tools.ietf.org/html/rfc4122 - specifically it should have hyphens then.
290
 */
291
292
/**
293
 * g_dbus_generate_guid:
294
 *
295
 * Generate a D-Bus GUID that can be used with
296
 * e.g. g_dbus_connection_new().
297
 *
298
 * See the
299
 * [D-Bus specification](https://dbus.freedesktop.org/doc/dbus-specification.html#uuids)
300
 * regarding what strings are valid D-Bus GUIDs. The specification refers to
301
 * these as ‘UUIDs’ whereas GLib (for historical reasons) refers to them as
302
 * ‘GUIDs’. The terms are interchangeable.
303
 *
304
 * Note that D-Bus GUIDs do not follow
305
 * [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122).
306
 *
307
 * Returns: A valid D-Bus GUID. Free with g_free().
308
 *
309
 * Since: 2.26
310
 */
311
gchar *
312
g_dbus_generate_guid (void)
313
0
{
314
0
  GString *s;
315
0
  guint32 r1;
316
0
  guint32 r2;
317
0
  guint32 r3;
318
0
  gint64 now_us;
319
320
0
  s = g_string_new (NULL);
321
322
0
  r1 = g_random_int ();
323
0
  r2 = g_random_int ();
324
0
  r3 = g_random_int ();
325
0
  now_us = g_get_real_time ();
326
327
0
  g_string_append_printf (s, "%08x", r1);
328
0
  g_string_append_printf (s, "%08x", r2);
329
0
  g_string_append_printf (s, "%08x", r3);
330
0
  g_string_append_printf (s, "%08x", (guint32) (now_us / G_USEC_PER_SEC));
331
332
0
  return g_string_free (s, FALSE);
333
0
}
334
335
/**
336
 * g_dbus_is_guid:
337
 * @string: The string to check.
338
 *
339
 * Checks if @string is a D-Bus GUID.
340
 *
341
 * See the documentation for g_dbus_generate_guid() for more information about
342
 * the format of a GUID.
343
 *
344
 * Returns: %TRUE if @string is a GUID, %FALSE otherwise.
345
 *
346
 * Since: 2.26
347
 */
348
gboolean
349
g_dbus_is_guid (const gchar *string)
350
0
{
351
0
  gboolean ret;
352
0
  guint n;
353
354
0
  g_return_val_if_fail (string != NULL, FALSE);
355
356
0
  ret = FALSE;
357
358
0
  for (n = 0; n < 32; n++)
359
0
    {
360
0
      if (!g_ascii_isxdigit (string[n]))
361
0
        goto out;
362
0
    }
363
0
  if (string[32] != '\0')
364
0
    goto out;
365
366
0
  ret = TRUE;
367
368
0
 out:
369
0
  return ret;
370
0
}
371
372
/* ---------------------------------------------------------------------------------------------------- */
373
374
/**
375
 * g_dbus_gvariant_to_gvalue:
376
 * @value: A #GVariant.
377
 * @out_gvalue: (out): Return location pointing to a zero-filled (uninitialized) #GValue.
378
 *
379
 * Converts a #GVariant to a #GValue. If @value is floating, it is consumed.
380
 *
381
 * The rules specified in the g_dbus_gvalue_to_gvariant() function are
382
 * used - this function is essentially its reverse form. So, a #GVariant
383
 * containing any basic or string array type will be converted to a #GValue
384
 * containing a basic value or string array. Any other #GVariant (handle,
385
 * variant, tuple, dict entry) will be converted to a #GValue containing that
386
 * #GVariant.
387
 *
388
 * The conversion never fails - a valid #GValue is always returned in
389
 * @out_gvalue.
390
 *
391
 * Since: 2.30
392
 */
393
void
394
g_dbus_gvariant_to_gvalue (GVariant  *value,
395
                           GValue    *out_gvalue)
396
0
{
397
0
  const GVariantType *type;
398
0
  gchar **array;
399
400
0
  g_return_if_fail (value != NULL);
401
0
  g_return_if_fail (out_gvalue != NULL);
402
403
0
  memset (out_gvalue, '\0', sizeof (GValue));
404
405
0
  switch (g_variant_classify (value))
406
0
    {
407
0
    case G_VARIANT_CLASS_BOOLEAN:
408
0
      g_value_init (out_gvalue, G_TYPE_BOOLEAN);
409
0
      g_value_set_boolean (out_gvalue, g_variant_get_boolean (value));
410
0
      break;
411
412
0
    case G_VARIANT_CLASS_BYTE:
413
0
      g_value_init (out_gvalue, G_TYPE_UCHAR);
414
0
      g_value_set_uchar (out_gvalue, g_variant_get_byte (value));
415
0
      break;
416
417
0
    case G_VARIANT_CLASS_INT16:
418
0
      g_value_init (out_gvalue, G_TYPE_INT);
419
0
      g_value_set_int (out_gvalue, g_variant_get_int16 (value));
420
0
      break;
421
422
0
    case G_VARIANT_CLASS_UINT16:
423
0
      g_value_init (out_gvalue, G_TYPE_UINT);
424
0
      g_value_set_uint (out_gvalue, g_variant_get_uint16 (value));
425
0
      break;
426
427
0
    case G_VARIANT_CLASS_INT32:
428
0
      g_value_init (out_gvalue, G_TYPE_INT);
429
0
      g_value_set_int (out_gvalue, g_variant_get_int32 (value));
430
0
      break;
431
432
0
    case G_VARIANT_CLASS_UINT32:
433
0
      g_value_init (out_gvalue, G_TYPE_UINT);
434
0
      g_value_set_uint (out_gvalue, g_variant_get_uint32 (value));
435
0
      break;
436
437
0
    case G_VARIANT_CLASS_INT64:
438
0
      g_value_init (out_gvalue, G_TYPE_INT64);
439
0
      g_value_set_int64 (out_gvalue, g_variant_get_int64 (value));
440
0
      break;
441
442
0
    case G_VARIANT_CLASS_UINT64:
443
0
      g_value_init (out_gvalue, G_TYPE_UINT64);
444
0
      g_value_set_uint64 (out_gvalue, g_variant_get_uint64 (value));
445
0
      break;
446
447
0
    case G_VARIANT_CLASS_DOUBLE:
448
0
      g_value_init (out_gvalue, G_TYPE_DOUBLE);
449
0
      g_value_set_double (out_gvalue, g_variant_get_double (value));
450
0
      break;
451
452
0
    case G_VARIANT_CLASS_STRING:
453
0
      g_value_init (out_gvalue, G_TYPE_STRING);
454
0
      g_value_set_string (out_gvalue, g_variant_get_string (value, NULL));
455
0
      break;
456
457
0
    case G_VARIANT_CLASS_OBJECT_PATH:
458
0
      g_value_init (out_gvalue, G_TYPE_STRING);
459
0
      g_value_set_string (out_gvalue, g_variant_get_string (value, NULL));
460
0
      break;
461
462
0
    case G_VARIANT_CLASS_SIGNATURE:
463
0
      g_value_init (out_gvalue, G_TYPE_STRING);
464
0
      g_value_set_string (out_gvalue, g_variant_get_string (value, NULL));
465
0
      break;
466
467
0
    case G_VARIANT_CLASS_ARRAY:
468
0
      type = g_variant_get_type (value);
469
0
      switch (g_variant_type_peek_string (type)[1])
470
0
        {
471
0
        case G_VARIANT_CLASS_BYTE:
472
0
          g_value_init (out_gvalue, G_TYPE_STRING);
473
0
          g_value_set_string (out_gvalue, g_variant_get_bytestring (value));
474
0
          break;
475
476
0
        case G_VARIANT_CLASS_STRING:
477
0
          g_value_init (out_gvalue, G_TYPE_STRV);
478
0
          array = g_variant_dup_strv (value, NULL);
479
0
          g_value_take_boxed (out_gvalue, array);
480
0
          break;
481
482
0
        case G_VARIANT_CLASS_OBJECT_PATH:
483
0
          g_value_init (out_gvalue, G_TYPE_STRV);
484
0
          array = g_variant_dup_objv (value, NULL);
485
0
          g_value_take_boxed (out_gvalue, array);
486
0
          break;
487
488
0
        case G_VARIANT_CLASS_ARRAY:
489
0
          switch (g_variant_type_peek_string (type)[2])
490
0
            {
491
0
            case G_VARIANT_CLASS_BYTE:
492
0
              g_value_init (out_gvalue, G_TYPE_STRV);
493
0
              array = g_variant_dup_bytestring_array (value, NULL);
494
0
              g_value_take_boxed (out_gvalue, array);
495
0
              break;
496
497
0
            default:
498
0
              g_value_init (out_gvalue, G_TYPE_VARIANT);
499
0
              g_value_set_variant (out_gvalue, value);
500
0
              break;
501
0
            }
502
0
          break;
503
504
0
        default:
505
0
          g_value_init (out_gvalue, G_TYPE_VARIANT);
506
0
          g_value_set_variant (out_gvalue, value);
507
0
          break;
508
0
        }
509
0
      break;
510
511
0
    case G_VARIANT_CLASS_HANDLE:
512
0
    case G_VARIANT_CLASS_VARIANT:
513
0
    case G_VARIANT_CLASS_MAYBE:
514
0
    case G_VARIANT_CLASS_TUPLE:
515
0
    case G_VARIANT_CLASS_DICT_ENTRY:
516
0
      g_value_init (out_gvalue, G_TYPE_VARIANT);
517
0
      g_value_set_variant (out_gvalue, value);
518
0
      break;
519
0
    }
520
0
}
521
522
523
/**
524
 * g_dbus_gvalue_to_gvariant:
525
 * @gvalue: A #GValue to convert to a #GVariant
526
 * @type: A #GVariantType
527
 *
528
 * Converts a #GValue to a #GVariant of the type indicated by the @type
529
 * parameter.
530
 *
531
 * The conversion is using the following rules:
532
 *
533
 * - `G_TYPE_STRING`: 's', 'o', 'g' or 'ay'
534
 * - `G_TYPE_STRV`: 'as', 'ao' or 'aay'
535
 * - `G_TYPE_BOOLEAN`: 'b'
536
 * - `G_TYPE_UCHAR`: 'y'
537
 * - `G_TYPE_INT`: 'i', 'n'
538
 * - `G_TYPE_UINT`: 'u', 'q'
539
 * - `G_TYPE_INT64`: 'x'
540
 * - `G_TYPE_UINT64`: 't'
541
 * - `G_TYPE_DOUBLE`: 'd'
542
 * - `G_TYPE_VARIANT`: Any #GVariantType
543
 *
544
 * This can fail if e.g. @gvalue is of type %G_TYPE_STRING and @type
545
 * is 'i', i.e. %G_VARIANT_TYPE_INT32. It will also fail for any #GType
546
 * (including e.g. %G_TYPE_OBJECT and %G_TYPE_BOXED derived-types) not
547
 * in the table above.
548
 *
549
 * Note that if @gvalue is of type %G_TYPE_VARIANT and its value is
550
 * %NULL, the empty #GVariant instance (never %NULL) for @type is
551
 * returned (e.g. 0 for scalar types, the empty string for string types,
552
 * '/' for object path types, the empty array for any array type and so on).
553
 *
554
 * See the g_dbus_gvariant_to_gvalue() function for how to convert a
555
 * #GVariant to a #GValue.
556
 *
557
 * Returns: (transfer full): A #GVariant (never floating) of
558
 *     #GVariantType @type holding the data from @gvalue or an empty #GVariant
559
 *     in case of failure. Free with g_variant_unref().
560
 *
561
 * Since: 2.30
562
 */
563
GVariant *
564
g_dbus_gvalue_to_gvariant (const GValue       *gvalue,
565
                           const GVariantType *type)
566
0
{
567
0
  GVariant *ret;
568
0
  const gchar *s;
569
0
  const gchar * const *as;
570
0
  const gchar *empty_strv[1] = {NULL};
571
572
0
  g_return_val_if_fail (gvalue != NULL, NULL);
573
0
  g_return_val_if_fail (type != NULL, NULL);
574
575
0
  ret = NULL;
576
577
  /* @type can easily be e.g. "s" with the GValue holding a GVariant - for example this
578
   * can happen when using the org.gtk.GDBus.C.ForceGVariant annotation with the
579
   * gdbus-codegen(1) tool.
580
   */
581
0
  if (G_VALUE_TYPE (gvalue) == G_TYPE_VARIANT)
582
0
    {
583
0
      ret = g_value_dup_variant (gvalue);
584
0
    }
585
0
  else
586
0
    {
587
0
      switch (g_variant_type_peek_string (type)[0])
588
0
        {
589
0
        case G_VARIANT_CLASS_BOOLEAN:
590
0
          ret = g_variant_ref_sink (g_variant_new_boolean (g_value_get_boolean (gvalue)));
591
0
          break;
592
593
0
        case G_VARIANT_CLASS_BYTE:
594
0
          ret = g_variant_ref_sink (g_variant_new_byte (g_value_get_uchar (gvalue)));
595
0
          break;
596
597
0
        case G_VARIANT_CLASS_INT16:
598
0
          ret = g_variant_ref_sink (g_variant_new_int16 (g_value_get_int (gvalue)));
599
0
          break;
600
601
0
        case G_VARIANT_CLASS_UINT16:
602
0
          ret = g_variant_ref_sink (g_variant_new_uint16 (g_value_get_uint (gvalue)));
603
0
          break;
604
605
0
        case G_VARIANT_CLASS_INT32:
606
0
          ret = g_variant_ref_sink (g_variant_new_int32 (g_value_get_int (gvalue)));
607
0
          break;
608
609
0
        case G_VARIANT_CLASS_UINT32:
610
0
          ret = g_variant_ref_sink (g_variant_new_uint32 (g_value_get_uint (gvalue)));
611
0
          break;
612
613
0
        case G_VARIANT_CLASS_INT64:
614
0
          ret = g_variant_ref_sink (g_variant_new_int64 (g_value_get_int64 (gvalue)));
615
0
          break;
616
617
0
        case G_VARIANT_CLASS_UINT64:
618
0
          ret = g_variant_ref_sink (g_variant_new_uint64 (g_value_get_uint64 (gvalue)));
619
0
          break;
620
621
0
        case G_VARIANT_CLASS_DOUBLE:
622
0
          ret = g_variant_ref_sink (g_variant_new_double (g_value_get_double (gvalue)));
623
0
          break;
624
625
0
        case G_VARIANT_CLASS_STRING:
626
0
          s = g_value_get_string (gvalue);
627
0
          if (s == NULL)
628
0
            s = "";
629
0
          ret = g_variant_ref_sink (g_variant_new_string (s));
630
0
          break;
631
632
0
        case G_VARIANT_CLASS_OBJECT_PATH:
633
0
          s = g_value_get_string (gvalue);
634
0
          if (s == NULL)
635
0
            s = "/";
636
0
          ret = g_variant_ref_sink (g_variant_new_object_path (s));
637
0
          break;
638
639
0
        case G_VARIANT_CLASS_SIGNATURE:
640
0
          s = g_value_get_string (gvalue);
641
0
          if (s == NULL)
642
0
            s = "";
643
0
          ret = g_variant_ref_sink (g_variant_new_signature (s));
644
0
          break;
645
646
0
        case G_VARIANT_CLASS_ARRAY:
647
0
          switch (g_variant_type_peek_string (type)[1])
648
0
            {
649
0
            case G_VARIANT_CLASS_BYTE:
650
0
              s = g_value_get_string (gvalue);
651
0
              if (s == NULL)
652
0
                s = "";
653
0
              ret = g_variant_ref_sink (g_variant_new_bytestring (s));
654
0
              break;
655
656
0
            case G_VARIANT_CLASS_STRING:
657
0
              as = g_value_get_boxed (gvalue);
658
0
              if (as == NULL)
659
0
                as = empty_strv;
660
0
              ret = g_variant_ref_sink (g_variant_new_strv (as, -1));
661
0
              break;
662
663
0
            case G_VARIANT_CLASS_OBJECT_PATH:
664
0
              as = g_value_get_boxed (gvalue);
665
0
              if (as == NULL)
666
0
                as = empty_strv;
667
0
              ret = g_variant_ref_sink (g_variant_new_objv (as, -1));
668
0
              break;
669
670
0
            case G_VARIANT_CLASS_ARRAY:
671
0
              switch (g_variant_type_peek_string (type)[2])
672
0
                {
673
0
                case G_VARIANT_CLASS_BYTE:
674
0
                  as = g_value_get_boxed (gvalue);
675
0
                  if (as == NULL)
676
0
                    as = empty_strv;
677
0
                  ret = g_variant_ref_sink (g_variant_new_bytestring_array (as, -1));
678
0
                  break;
679
680
0
                default:
681
0
                  ret = g_value_dup_variant (gvalue);
682
0
                  break;
683
0
                }
684
0
              break;
685
686
0
            default:
687
0
              ret = g_value_dup_variant (gvalue);
688
0
              break;
689
0
            }
690
0
          break;
691
692
0
        case G_VARIANT_CLASS_HANDLE:
693
0
        case G_VARIANT_CLASS_VARIANT:
694
0
        case G_VARIANT_CLASS_MAYBE:
695
0
        case G_VARIANT_CLASS_TUPLE:
696
0
        case G_VARIANT_CLASS_DICT_ENTRY:
697
0
          ret = g_value_dup_variant (gvalue);
698
0
          break;
699
0
        }
700
0
    }
701
702
  /* Could be that the GValue is holding a NULL GVariant - in that case,
703
   * we return an "empty" GVariant instead of a NULL GVariant
704
   */
705
0
  if (ret == NULL)
706
0
    {
707
0
      GVariant *untrusted_empty;
708
0
      untrusted_empty = g_variant_new_from_data (type, NULL, 0, FALSE, NULL, NULL);
709
0
      ret = g_variant_take_ref (g_variant_get_normal_form (untrusted_empty));
710
0
      g_variant_unref (untrusted_empty);
711
0
    }
712
713
0
  g_assert (!g_variant_is_floating (ret));
714
715
0
  return ret;
716
0
}
717
718
/**
719
 * g_dbus_escape_object_path_bytestring:
720
 * @bytes: (array zero-terminated=1) (element-type guint8): the string of bytes to escape
721
 *
722
 * Escapes @bytes for use in a D-Bus object path component.
723
 * @bytes is an array of zero or more nonzero bytes in an
724
 * unspecified encoding, followed by a single zero byte.
725
 *
726
 * The escaping method consists of replacing all non-alphanumeric
727
 * characters (see g_ascii_isalnum()) with their hexadecimal value
728
 * preceded by an underscore (`_`). For example:
729
 * `foo.bar.baz` will become `foo_2ebar_2ebaz`.
730
 *
731
 * This method is appropriate to use when the input is nearly
732
 * a valid object path component but is not when your input
733
 * is far from being a valid object path component.
734
 * Other escaping algorithms are also valid to use with
735
 * D-Bus object paths.
736
 *
737
 * This can be reversed with g_dbus_unescape_object_path().
738
 *
739
 * Returns: an escaped version of @bytes. Free with g_free().
740
 *
741
 * Since: 2.68
742
 *
743
 */
744
gchar *
745
g_dbus_escape_object_path_bytestring (const guint8 *bytes)
746
0
{
747
0
  GString *escaped;
748
0
  const guint8 *p;
749
750
0
  g_return_val_if_fail (bytes != NULL, NULL);
751
752
0
  if (*bytes == '\0')
753
0
    return g_strdup ("_");
754
755
0
  escaped = g_string_new (NULL);
756
0
  for (p = bytes; *p; p++)
757
0
    {
758
0
      if (g_ascii_isalnum (*p))
759
0
        g_string_append_c (escaped, *p);
760
0
      else
761
0
        g_string_append_printf (escaped, "_%02x", *p);
762
0
    }
763
764
0
  return g_string_free (escaped, FALSE);
765
0
}
766
767
/**
768
 * g_dbus_escape_object_path:
769
 * @s: the string to escape
770
 *
771
 * This is a language binding friendly version of g_dbus_escape_object_path_bytestring().
772
 *
773
 * Returns: an escaped version of @s. Free with g_free().
774
 *
775
 * Since: 2.68
776
 */
777
gchar *
778
g_dbus_escape_object_path (const gchar *s)
779
0
{
780
0
  return (gchar *) g_dbus_escape_object_path_bytestring ((const guint8 *) s);
781
0
}
782
783
/**
784
 * g_dbus_unescape_object_path:
785
 * @s: the string to unescape
786
 *
787
 * Unescapes an string that was previously escaped with
788
 * g_dbus_escape_object_path(). If the string is in a format that could
789
 * not have been returned by g_dbus_escape_object_path(), this function
790
 * returns %NULL.
791
 *
792
 * Encoding alphanumeric characters which do not need to be
793
 * encoded is not allowed (e.g `_63` is not valid, the string
794
 * should contain `c` instead).
795
 *
796
 * Returns: (array zero-terminated=1) (element-type guint8) (nullable): an
797
 *   unescaped version of @s, or %NULL if @s is not a string returned
798
 *   from g_dbus_escape_object_path(). Free with g_free().
799
 *
800
 * Since: 2.68
801
 */
802
guint8 *
803
g_dbus_unescape_object_path (const gchar *s)
804
0
{
805
0
  GString *unescaped;
806
0
  const gchar *p;
807
808
0
  g_return_val_if_fail (s != NULL, NULL);
809
810
0
  if (g_str_equal (s, "_"))
811
0
    return (guint8 *) g_strdup ("");
812
813
0
  unescaped = g_string_new (NULL);
814
0
  for (p = s; *p; p++)
815
0
    {
816
0
      gint hi, lo;
817
818
0
      if (g_ascii_isalnum (*p))
819
0
        {
820
0
          g_string_append_c (unescaped, *p);
821
0
        }
822
0
      else if (*p == '_' &&
823
0
               ((hi = g_ascii_xdigit_value (p[1])) >= 0) &&
824
0
               ((lo = g_ascii_xdigit_value (p[2])) >= 0) &&
825
0
               (hi || lo) &&                      /* \0 is not allowed */
826
0
               !g_ascii_isalnum ((hi << 4) | lo)) /* alnums must not be encoded */
827
0
        {
828
0
          g_string_append_c (unescaped, (hi << 4) | lo);
829
0
          p += 2;
830
0
        }
831
0
      else
832
0
        {
833
          /* the string was not encoded correctly */
834
0
          g_string_free (unescaped, TRUE);
835
0
          return NULL;
836
0
        }
837
0
    }
838
839
0
  return (guint8 *) g_string_free (unescaped, FALSE);
840
0
}