Coverage Report

Created: 2025-06-13 06:55

/src/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
/**
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
0
{
45
0
  return
46
0
    (c >= '0' && c <= '9') ||
47
0
    (c >= 'A' && c <= 'Z') ||
48
0
    (c >= 'a' && c <= 'z') ||
49
0
    (c == '_') ||
50
0
    (allow_hyphen && c == '-');
51
0
}
52
53
static gboolean
54
is_valid_initial_bus_name_character (gint c,
55
                                     gboolean allow_initial_digit,
56
                                     gboolean allow_hyphen)
57
0
{
58
0
  if (allow_initial_digit)
59
0
    return is_valid_bus_name_character (c, allow_hyphen);
60
0
  else
61
0
    return
62
0
      (c >= 'A' && c <= 'Z') ||
63
0
      (c >= 'a' && c <= 'z') ||
64
0
      (c == '_') ||
65
0
      (allow_hyphen && c == '-');
66
0
}
67
68
static gboolean
69
is_valid_name (const gchar *start,
70
               guint len,
71
               gboolean allow_initial_digit,
72
               gboolean allow_hyphen)
73
0
{
74
0
  gboolean ret;
75
0
  const gchar *s;
76
0
  const gchar *end;
77
0
  gboolean has_dot;
78
79
0
  ret = FALSE;
80
81
0
  if (len == 0)
82
0
    goto out;
83
84
0
  s = start;
85
0
  end = s + len;
86
0
  has_dot = FALSE;
87
0
  while (s != end)
88
0
    {
89
0
      if (*s == '.')
90
0
        {
91
0
          s += 1;
92
0
          if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, allow_initial_digit, allow_hyphen)))
93
0
            goto out;
94
0
          has_dot = TRUE;
95
0
        }
96
0
      else if (G_UNLIKELY (!is_valid_bus_name_character (*s, allow_hyphen)))
97
0
        {
98
0
          goto out;
99
0
        }
100
0
      s += 1;
101
0
    }
102
103
0
  if (G_UNLIKELY (!has_dot))
104
0
    goto out;
105
106
0
  ret = TRUE;
107
108
0
 out:
109
0
  return ret;
110
0
}
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
0
{
125
0
  guint len;
126
0
  gboolean ret;
127
0
  const gchar *s;
128
129
0
  g_return_val_if_fail (string != NULL, FALSE);
130
131
0
  ret = FALSE;
132
133
0
  len = strlen (string);
134
0
  if (G_UNLIKELY (len == 0 || len > 255))
135
0
    goto out;
136
137
0
  s = string;
138
0
  if (*s == ':')
139
0
    {
140
      /* handle unique name */
141
0
      if (!is_valid_name (s + 1, len - 1, TRUE, TRUE))
142
0
        goto out;
143
0
      ret = TRUE;
144
0
      goto out;
145
0
    }
146
0
  else if (G_UNLIKELY (*s == '.'))
147
0
    {
148
      /* can't start with a . */
149
0
      goto out;
150
0
    }
151
0
  else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, TRUE)))
152
0
    goto out;
153
154
0
  ret = is_valid_name (s + 1, len - 1, FALSE, TRUE);
155
156
0
 out:
157
0
  return ret;
158
0
}
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
0
{
173
0
  gboolean ret;
174
0
  guint len;
175
176
0
  g_return_val_if_fail (string != NULL, FALSE);
177
178
0
  ret = FALSE;
179
180
0
  len = strlen (string);
181
0
  if (G_UNLIKELY (len == 0 || len > 255))
182
0
    goto out;
183
184
0
  if (G_UNLIKELY (*string != ':'))
185
0
    goto out;
186
187
0
  if (G_UNLIKELY (!is_valid_name (string + 1, len - 1, TRUE, TRUE)))
188
0
    goto out;
189
190
0
  ret = TRUE;
191
192
0
 out:
193
0
  return ret;
194
0
}
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
0
{
209
0
  gboolean ret;
210
0
  guint n;
211
212
0
  ret = FALSE;
213
0
  if (G_UNLIKELY (string == NULL))
214
0
    goto out;
215
216
0
  if (G_UNLIKELY (!is_valid_initial_bus_name_character (string[0], FALSE, FALSE)))
217
0
    goto out;
218
219
0
  for (n = 1; string[n] != '\0'; n++)
220
0
    {
221
0
      if (G_UNLIKELY (!is_valid_bus_name_character (string[n], FALSE)))
222
0
        {
223
0
          goto out;
224
0
        }
225
0
    }
226
227
0
  ret = TRUE;
228
229
0
 out:
230
0
  return ret;
231
0
}
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
0
{
246
0
  guint len;
247
0
  gboolean ret;
248
0
  const gchar *s;
249
250
0
  g_return_val_if_fail (string != NULL, FALSE);
251
252
0
  ret = FALSE;
253
254
0
  len = strlen (string);
255
0
  if (G_UNLIKELY (len == 0 || len > 255))
256
0
    goto out;
257
258
0
  s = string;
259
0
  if (G_UNLIKELY (*s == '.'))
260
0
    {
261
      /* can't start with a . */
262
0
      goto out;
263
0
    }
264
0
  else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, FALSE)))
265
0
    goto out;
266
267
0
  ret = is_valid_name (s + 1, len - 1, FALSE, FALSE);
268
269
0
 out:
270
0
  return ret;
271
0
}
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
0
{
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
0
  return g_dbus_is_interface_name (string);
293
0
}
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
0
{
323
0
  GString *s;
324
0
  guint32 r1;
325
0
  guint32 r2;
326
0
  guint32 r3;
327
0
  gint64 now_us;
328
329
0
  s = g_string_new (NULL);
330
331
0
  r1 = g_random_int ();
332
0
  r2 = g_random_int ();
333
0
  r3 = g_random_int ();
334
0
  now_us = g_get_real_time ();
335
336
0
  g_string_append_printf (s, "%08x", r1);
337
0
  g_string_append_printf (s, "%08x", r2);
338
0
  g_string_append_printf (s, "%08x", r3);
339
0
  g_string_append_printf (s, "%08x", (guint32) (now_us / G_USEC_PER_SEC));
340
341
0
  return g_string_free (s, FALSE);
342
0
}
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
0
{
360
0
  gboolean ret;
361
0
  guint n;
362
363
0
  g_return_val_if_fail (string != NULL, FALSE);
364
365
0
  ret = FALSE;
366
367
0
  for (n = 0; n < 32; n++)
368
0
    {
369
0
      if (!g_ascii_isxdigit (string[n]))
370
0
        goto out;
371
0
    }
372
0
  if (string[32] != '\0')
373
0
    goto out;
374
375
0
  ret = TRUE;
376
377
0
 out:
378
0
  return ret;
379
0
}
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
0
{
406
0
  const GVariantType *type;
407
0
  gchar **array;
408
409
0
  g_return_if_fail (value != NULL);
410
0
  g_return_if_fail (out_gvalue != NULL);
411
412
0
  memset (out_gvalue, '\0', sizeof (GValue));
413
414
0
  switch (g_variant_classify (value))
415
0
    {
416
0
    case G_VARIANT_CLASS_BOOLEAN:
417
0
      g_value_init (out_gvalue, G_TYPE_BOOLEAN);
418
0
      g_value_set_boolean (out_gvalue, g_variant_get_boolean (value));
419
0
      break;
420
421
0
    case G_VARIANT_CLASS_BYTE:
422
0
      g_value_init (out_gvalue, G_TYPE_UCHAR);
423
0
      g_value_set_uchar (out_gvalue, g_variant_get_byte (value));
424
0
      break;
425
426
0
    case G_VARIANT_CLASS_INT16:
427
0
      g_value_init (out_gvalue, G_TYPE_INT);
428
0
      g_value_set_int (out_gvalue, g_variant_get_int16 (value));
429
0
      break;
430
431
0
    case G_VARIANT_CLASS_UINT16:
432
0
      g_value_init (out_gvalue, G_TYPE_UINT);
433
0
      g_value_set_uint (out_gvalue, g_variant_get_uint16 (value));
434
0
      break;
435
436
0
    case G_VARIANT_CLASS_INT32:
437
0
      g_value_init (out_gvalue, G_TYPE_INT);
438
0
      g_value_set_int (out_gvalue, g_variant_get_int32 (value));
439
0
      break;
440
441
0
    case G_VARIANT_CLASS_UINT32:
442
0
      g_value_init (out_gvalue, G_TYPE_UINT);
443
0
      g_value_set_uint (out_gvalue, g_variant_get_uint32 (value));
444
0
      break;
445
446
0
    case G_VARIANT_CLASS_INT64:
447
0
      g_value_init (out_gvalue, G_TYPE_INT64);
448
0
      g_value_set_int64 (out_gvalue, g_variant_get_int64 (value));
449
0
      break;
450
451
0
    case G_VARIANT_CLASS_UINT64:
452
0
      g_value_init (out_gvalue, G_TYPE_UINT64);
453
0
      g_value_set_uint64 (out_gvalue, g_variant_get_uint64 (value));
454
0
      break;
455
456
0
    case G_VARIANT_CLASS_DOUBLE:
457
0
      g_value_init (out_gvalue, G_TYPE_DOUBLE);
458
0
      g_value_set_double (out_gvalue, g_variant_get_double (value));
459
0
      break;
460
461
0
    case G_VARIANT_CLASS_STRING:
462
0
      g_value_init (out_gvalue, G_TYPE_STRING);
463
0
      g_value_set_string (out_gvalue, g_variant_get_string (value, NULL));
464
0
      break;
465
466
0
    case G_VARIANT_CLASS_OBJECT_PATH:
467
0
      g_value_init (out_gvalue, G_TYPE_STRING);
468
0
      g_value_set_string (out_gvalue, g_variant_get_string (value, NULL));
469
0
      break;
470
471
0
    case G_VARIANT_CLASS_SIGNATURE:
472
0
      g_value_init (out_gvalue, G_TYPE_STRING);
473
0
      g_value_set_string (out_gvalue, g_variant_get_string (value, NULL));
474
0
      break;
475
476
0
    case G_VARIANT_CLASS_ARRAY:
477
0
      type = g_variant_get_type (value);
478
0
      switch (g_variant_type_peek_string (type)[1])
479
0
        {
480
0
        case G_VARIANT_CLASS_BYTE:
481
0
          g_value_init (out_gvalue, G_TYPE_STRING);
482
0
          g_value_set_string (out_gvalue, g_variant_get_bytestring (value));
483
0
          break;
484
485
0
        case G_VARIANT_CLASS_STRING:
486
0
          g_value_init (out_gvalue, G_TYPE_STRV);
487
0
          array = g_variant_dup_strv (value, NULL);
488
0
          g_value_take_boxed (out_gvalue, array);
489
0
          break;
490
491
0
        case G_VARIANT_CLASS_OBJECT_PATH:
492
0
          g_value_init (out_gvalue, G_TYPE_STRV);
493
0
          array = g_variant_dup_objv (value, NULL);
494
0
          g_value_take_boxed (out_gvalue, array);
495
0
          break;
496
497
0
        case G_VARIANT_CLASS_ARRAY:
498
0
          switch (g_variant_type_peek_string (type)[2])
499
0
            {
500
0
            case G_VARIANT_CLASS_BYTE:
501
0
              g_value_init (out_gvalue, G_TYPE_STRV);
502
0
              array = g_variant_dup_bytestring_array (value, NULL);
503
0
              g_value_take_boxed (out_gvalue, array);
504
0
              break;
505
506
0
            default:
507
0
              g_value_init (out_gvalue, G_TYPE_VARIANT);
508
0
              g_value_set_variant (out_gvalue, value);
509
0
              break;
510
0
            }
511
0
          break;
512
513
0
        default:
514
0
          g_value_init (out_gvalue, G_TYPE_VARIANT);
515
0
          g_value_set_variant (out_gvalue, value);
516
0
          break;
517
0
        }
518
0
      break;
519
520
0
    case G_VARIANT_CLASS_HANDLE:
521
0
    case G_VARIANT_CLASS_VARIANT:
522
0
    case G_VARIANT_CLASS_MAYBE:
523
0
    case G_VARIANT_CLASS_TUPLE:
524
0
    case G_VARIANT_CLASS_DICT_ENTRY:
525
0
      g_value_init (out_gvalue, G_TYPE_VARIANT);
526
0
      g_value_set_variant (out_gvalue, value);
527
0
      break;
528
0
    }
529
0
}
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
0
{
576
0
  GVariant *ret;
577
0
  const gchar *s;
578
0
  const gchar * const *as;
579
0
  const gchar *empty_strv[1] = {NULL};
580
581
0
  g_return_val_if_fail (gvalue != NULL, NULL);
582
0
  g_return_val_if_fail (type != NULL, NULL);
583
584
0
  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
0
  if (G_VALUE_TYPE (gvalue) == G_TYPE_VARIANT)
591
0
    {
592
0
      ret = g_value_dup_variant (gvalue);
593
0
    }
594
0
  else
595
0
    {
596
0
      switch (g_variant_type_peek_string (type)[0])
597
0
        {
598
0
        case G_VARIANT_CLASS_BOOLEAN:
599
0
          ret = g_variant_ref_sink (g_variant_new_boolean (g_value_get_boolean (gvalue)));
600
0
          break;
601
602
0
        case G_VARIANT_CLASS_BYTE:
603
0
          ret = g_variant_ref_sink (g_variant_new_byte (g_value_get_uchar (gvalue)));
604
0
          break;
605
606
0
        case G_VARIANT_CLASS_INT16:
607
0
          ret = g_variant_ref_sink (g_variant_new_int16 (g_value_get_int (gvalue)));
608
0
          break;
609
610
0
        case G_VARIANT_CLASS_UINT16:
611
0
          ret = g_variant_ref_sink (g_variant_new_uint16 (g_value_get_uint (gvalue)));
612
0
          break;
613
614
0
        case G_VARIANT_CLASS_INT32:
615
0
          ret = g_variant_ref_sink (g_variant_new_int32 (g_value_get_int (gvalue)));
616
0
          break;
617
618
0
        case G_VARIANT_CLASS_UINT32:
619
0
          ret = g_variant_ref_sink (g_variant_new_uint32 (g_value_get_uint (gvalue)));
620
0
          break;
621
622
0
        case G_VARIANT_CLASS_INT64:
623
0
          ret = g_variant_ref_sink (g_variant_new_int64 (g_value_get_int64 (gvalue)));
624
0
          break;
625
626
0
        case G_VARIANT_CLASS_UINT64:
627
0
          ret = g_variant_ref_sink (g_variant_new_uint64 (g_value_get_uint64 (gvalue)));
628
0
          break;
629
630
0
        case G_VARIANT_CLASS_DOUBLE:
631
0
          ret = g_variant_ref_sink (g_variant_new_double (g_value_get_double (gvalue)));
632
0
          break;
633
634
0
        case G_VARIANT_CLASS_STRING:
635
0
          s = g_value_get_string (gvalue);
636
0
          if (s == NULL)
637
0
            s = "";
638
0
          ret = g_variant_ref_sink (g_variant_new_string (s));
639
0
          break;
640
641
0
        case G_VARIANT_CLASS_OBJECT_PATH:
642
0
          s = g_value_get_string (gvalue);
643
0
          if (s == NULL)
644
0
            s = "/";
645
0
          ret = g_variant_ref_sink (g_variant_new_object_path (s));
646
0
          break;
647
648
0
        case G_VARIANT_CLASS_SIGNATURE:
649
0
          s = g_value_get_string (gvalue);
650
0
          if (s == NULL)
651
0
            s = "";
652
0
          ret = g_variant_ref_sink (g_variant_new_signature (s));
653
0
          break;
654
655
0
        case G_VARIANT_CLASS_ARRAY:
656
0
          switch (g_variant_type_peek_string (type)[1])
657
0
            {
658
0
            case G_VARIANT_CLASS_BYTE:
659
0
              s = g_value_get_string (gvalue);
660
0
              if (s == NULL)
661
0
                s = "";
662
0
              ret = g_variant_ref_sink (g_variant_new_bytestring (s));
663
0
              break;
664
665
0
            case G_VARIANT_CLASS_STRING:
666
0
              as = g_value_get_boxed (gvalue);
667
0
              if (as == NULL)
668
0
                as = empty_strv;
669
0
              ret = g_variant_ref_sink (g_variant_new_strv (as, -1));
670
0
              break;
671
672
0
            case G_VARIANT_CLASS_OBJECT_PATH:
673
0
              as = g_value_get_boxed (gvalue);
674
0
              if (as == NULL)
675
0
                as = empty_strv;
676
0
              ret = g_variant_ref_sink (g_variant_new_objv (as, -1));
677
0
              break;
678
679
0
            case G_VARIANT_CLASS_ARRAY:
680
0
              switch (g_variant_type_peek_string (type)[2])
681
0
                {
682
0
                case G_VARIANT_CLASS_BYTE:
683
0
                  as = g_value_get_boxed (gvalue);
684
0
                  if (as == NULL)
685
0
                    as = empty_strv;
686
0
                  ret = g_variant_ref_sink (g_variant_new_bytestring_array (as, -1));
687
0
                  break;
688
689
0
                default:
690
0
                  ret = g_value_dup_variant (gvalue);
691
0
                  break;
692
0
                }
693
0
              break;
694
695
0
            default:
696
0
              ret = g_value_dup_variant (gvalue);
697
0
              break;
698
0
            }
699
0
          break;
700
701
0
        case G_VARIANT_CLASS_HANDLE:
702
0
        case G_VARIANT_CLASS_VARIANT:
703
0
        case G_VARIANT_CLASS_MAYBE:
704
0
        case G_VARIANT_CLASS_TUPLE:
705
0
        case G_VARIANT_CLASS_DICT_ENTRY:
706
0
          ret = g_value_dup_variant (gvalue);
707
0
          break;
708
0
        }
709
0
    }
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
0
  if (ret == NULL)
715
0
    {
716
0
      GVariant *untrusted_empty;
717
0
      untrusted_empty = g_variant_new_from_data (type, NULL, 0, FALSE, NULL, NULL);
718
0
      ret = g_variant_take_ref (g_variant_get_normal_form (untrusted_empty));
719
0
      g_variant_unref (untrusted_empty);
720
0
    }
721
722
0
  g_assert (!g_variant_is_floating (ret));
723
724
0
  return ret;
725
0
}
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
0
{
756
0
  GString *escaped;
757
0
  const guint8 *p;
758
759
0
  g_return_val_if_fail (bytes != NULL, NULL);
760
761
0
  if (*bytes == '\0')
762
0
    return g_strdup ("_");
763
764
0
  escaped = g_string_new (NULL);
765
0
  for (p = bytes; *p; p++)
766
0
    {
767
0
      if (g_ascii_isalnum (*p))
768
0
        g_string_append_c (escaped, *p);
769
0
      else
770
0
        g_string_append_printf (escaped, "_%02x", *p);
771
0
    }
772
773
0
  return g_string_free (escaped, FALSE);
774
0
}
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
0
{
789
0
  return (gchar *) g_dbus_escape_object_path_bytestring ((const guint8 *) s);
790
0
}
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
0
{
814
0
  GString *unescaped;
815
0
  const gchar *p;
816
817
0
  g_return_val_if_fail (s != NULL, NULL);
818
819
0
  if (g_str_equal (s, "_"))
820
0
    return (guint8 *) g_strdup ("");
821
822
0
  unescaped = g_string_new (NULL);
823
0
  for (p = s; *p; p++)
824
0
    {
825
0
      gint hi, lo;
826
827
0
      if (g_ascii_isalnum (*p))
828
0
        {
829
0
          g_string_append_c (unescaped, *p);
830
0
        }
831
0
      else if (*p == '_' &&
832
0
               ((hi = g_ascii_xdigit_value (p[1])) >= 0) &&
833
0
               ((lo = g_ascii_xdigit_value (p[2])) >= 0) &&
834
0
               (hi || lo) &&                      /* \0 is not allowed */
835
0
               !g_ascii_isalnum ((hi << 4) | lo)) /* alnums must not be encoded */
836
0
        {
837
0
          g_string_append_c (unescaped, (hi << 4) | lo);
838
0
          p += 2;
839
0
        }
840
0
      else
841
0
        {
842
          /* the string was not encoded correctly */
843
0
          g_string_free (unescaped, TRUE);
844
0
          return NULL;
845
0
        }
846
0
    }
847
848
0
  return (guint8 *) g_string_free (unescaped, FALSE);
849
0
}