Coverage Report

Created: 2025-07-23 08:13

/src/pango/subprojects/glib/glib/gvariant-parser.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2009, 2010 Codethink Limited
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * Author: Ryan Lortie <desrt@desrt.ca>
20
 */
21
22
#include "config.h"
23
24
#include <stdlib.h>
25
#include <string.h>
26
#include <errno.h>
27
28
#include "gerror.h"
29
#include "gquark.h"
30
#include "gstring.h"
31
#include "gstrfuncs.h"
32
#include "gtestutils.h"
33
#include "gvariant.h"
34
#include "glib/gvariant-core.h"
35
#include "gvariant-internal.h"
36
#include "gvarianttype.h"
37
#include "gslice.h"
38
#include "gthread.h"
39
40
/*
41
 * two-pass algorithm
42
 * designed by ryan lortie and william hua
43
 * designed in itb-229 and at ghazi's, 2009.
44
 */
45
46
/**
47
 * G_VARIANT_PARSE_ERROR:
48
 *
49
 * Error domain for GVariant text format parsing.  Specific error codes
50
 * are not currently defined for this domain.  See #GError for
51
 * information on error domains.
52
 **/
53
/**
54
 * GVariantParseError:
55
 * @G_VARIANT_PARSE_ERROR_FAILED: generic error (unused)
56
 * @G_VARIANT_PARSE_ERROR_BASIC_TYPE_EXPECTED: a non-basic #GVariantType was given where a basic type was expected
57
 * @G_VARIANT_PARSE_ERROR_CANNOT_INFER_TYPE: cannot infer the #GVariantType
58
 * @G_VARIANT_PARSE_ERROR_DEFINITE_TYPE_EXPECTED: an indefinite #GVariantType was given where a definite type was expected
59
 * @G_VARIANT_PARSE_ERROR_INPUT_NOT_AT_END: extra data after parsing finished
60
 * @G_VARIANT_PARSE_ERROR_INVALID_CHARACTER: invalid character in number or unicode escape
61
 * @G_VARIANT_PARSE_ERROR_INVALID_FORMAT_STRING: not a valid #GVariant format string
62
 * @G_VARIANT_PARSE_ERROR_INVALID_OBJECT_PATH: not a valid object path
63
 * @G_VARIANT_PARSE_ERROR_INVALID_SIGNATURE: not a valid type signature
64
 * @G_VARIANT_PARSE_ERROR_INVALID_TYPE_STRING: not a valid #GVariant type string
65
 * @G_VARIANT_PARSE_ERROR_NO_COMMON_TYPE: could not find a common type for array entries
66
 * @G_VARIANT_PARSE_ERROR_NUMBER_OUT_OF_RANGE: the numerical value is out of range of the given type
67
 * @G_VARIANT_PARSE_ERROR_NUMBER_TOO_BIG: the numerical value is out of range for any type
68
 * @G_VARIANT_PARSE_ERROR_TYPE_ERROR: cannot parse as variant of the specified type
69
 * @G_VARIANT_PARSE_ERROR_UNEXPECTED_TOKEN: an unexpected token was encountered
70
 * @G_VARIANT_PARSE_ERROR_UNKNOWN_KEYWORD: an unknown keyword was encountered
71
 * @G_VARIANT_PARSE_ERROR_UNTERMINATED_STRING_CONSTANT: unterminated string constant
72
 * @G_VARIANT_PARSE_ERROR_VALUE_EXPECTED: no value given
73
 * @G_VARIANT_PARSE_ERROR_RECURSION: variant was too deeply nested; #GVariant is only guaranteed to handle nesting up to 64 levels (Since: 2.64)
74
 *
75
 * Error codes returned by parsing text-format GVariants.
76
 **/
77
G_DEFINE_QUARK (g-variant-parse-error-quark, g_variant_parse_error)
78
79
/**
80
 * g_variant_parser_get_error_quark:
81
 *
82
 * Same as g_variant_error_quark().
83
 *
84
 * Deprecated: Use g_variant_parse_error_quark() instead.
85
 */
86
GQuark
87
g_variant_parser_get_error_quark (void)
88
0
{
89
0
  return g_variant_parse_error_quark ();
90
0
}
91
92
typedef struct
93
{
94
  gint start, end;
95
} SourceRef;
96
97
G_GNUC_PRINTF(5, 0)
98
static void
99
parser_set_error_va (GError      **error,
100
                     SourceRef    *location,
101
                     SourceRef    *other,
102
                     gint          code,
103
                     const gchar  *format,
104
                     va_list       ap)
105
0
{
106
0
  GString *msg = g_string_new (NULL);
107
108
0
  if (location->start == location->end)
109
0
    g_string_append_printf (msg, "%d", location->start);
110
0
  else
111
0
    g_string_append_printf (msg, "%d-%d", location->start, location->end);
112
113
0
  if (other != NULL)
114
0
    {
115
0
      g_assert (other->start != other->end);
116
0
      g_string_append_printf (msg, ",%d-%d", other->start, other->end);
117
0
    }
118
0
  g_string_append_c (msg, ':');
119
120
0
  g_string_append_vprintf (msg, format, ap);
121
0
  g_set_error_literal (error, G_VARIANT_PARSE_ERROR, code, msg->str);
122
0
  g_string_free (msg, TRUE);
123
0
}
124
125
G_GNUC_PRINTF(5, 6)
126
static void
127
parser_set_error (GError      **error,
128
                  SourceRef    *location,
129
                  SourceRef    *other,
130
                  gint          code,
131
                  const gchar  *format,
132
                  ...)
133
0
{
134
0
  va_list ap;
135
136
0
  va_start (ap, format);
137
0
  parser_set_error_va (error, location, other, code, format, ap);
138
0
  va_end (ap);
139
0
}
140
141
typedef struct
142
{
143
  const gchar *start;
144
  const gchar *stream;
145
  const gchar *end;
146
147
  const gchar *this;
148
} TokenStream;
149
150
151
G_GNUC_PRINTF(5, 6)
152
static void
153
token_stream_set_error (TokenStream  *stream,
154
                        GError      **error,
155
                        gboolean      this_token,
156
                        gint          code,
157
                        const gchar  *format,
158
                        ...)
159
0
{
160
0
  SourceRef ref;
161
0
  va_list ap;
162
163
0
  ref.start = stream->this - stream->start;
164
165
0
  if (this_token)
166
0
    ref.end = stream->stream - stream->start;
167
0
  else
168
0
    ref.end = ref.start;
169
170
0
  va_start (ap, format);
171
0
  parser_set_error_va (error, &ref, NULL, code, format, ap);
172
0
  va_end (ap);
173
0
}
174
175
static gboolean
176
token_stream_prepare (TokenStream *stream)
177
0
{
178
0
  gint brackets = 0;
179
0
  const gchar *end;
180
181
0
  if (stream->this != NULL)
182
0
    return TRUE;
183
184
0
  while (stream->stream != stream->end && g_ascii_isspace (*stream->stream))
185
0
    stream->stream++;
186
187
0
  if (stream->stream == stream->end || *stream->stream == '\0')
188
0
    {
189
0
      stream->this = stream->stream;
190
0
      return FALSE;
191
0
    }
192
193
0
  switch (stream->stream[0])
194
0
    {
195
0
    case '-': case '+': case '.': case '0': case '1': case '2':
196
0
    case '3': case '4': case '5': case '6': case '7': case '8':
197
0
    case '9':
198
0
      for (end = stream->stream; end != stream->end; end++)
199
0
        if (!g_ascii_isalnum (*end) &&
200
0
            *end != '-' && *end != '+' && *end != '.')
201
0
          break;
202
0
      break;
203
204
0
    case 'b':
205
0
      if (stream->stream + 1 != stream->end &&
206
0
          (stream->stream[1] == '\'' || stream->stream[1] == '"'))
207
0
        {
208
0
          for (end = stream->stream + 2; end != stream->end; end++)
209
0
            if (*end == stream->stream[1] || *end == '\0' ||
210
0
                (*end == '\\' && (++end == stream->end || *end == '\0')))
211
0
              break;
212
213
0
          if (end != stream->end && *end)
214
0
            end++;
215
0
          break;
216
0
        }
217
218
0
      G_GNUC_FALLTHROUGH;
219
220
0
    case 'a': /* 'b' */ case 'c': case 'd': case 'e': case 'f':
221
0
    case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
222
0
    case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
223
0
    case 's': case 't': case 'u': case 'v': case 'w': case 'x':
224
0
    case 'y': case 'z':
225
0
      for (end = stream->stream; end != stream->end; end++)
226
0
        if (!g_ascii_isalnum (*end))
227
0
          break;
228
0
      break;
229
230
0
    case '\'': case '"':
231
0
      for (end = stream->stream + 1; end != stream->end; end++)
232
0
        if (*end == stream->stream[0] || *end == '\0' ||
233
0
            (*end == '\\' && (++end == stream->end || *end == '\0')))
234
0
          break;
235
236
0
      if (end != stream->end && *end)
237
0
        end++;
238
0
      break;
239
240
0
    case '@': case '%':
241
      /* stop at the first space, comma, colon or unmatched bracket.
242
       * deals nicely with cases like (%i, %i) or {%i: %i}.
243
       * Also: ] and > are never in format strings.
244
       */
245
0
      for (end = stream->stream + 1;
246
0
           end != stream->end && *end != '\0' && *end != ',' &&
247
0
           *end != ':' && *end != '>' && *end != ']' && !g_ascii_isspace (*end);
248
0
           end++)
249
250
0
        if (*end == '(' || *end == '{')
251
0
          brackets++;
252
253
0
        else if ((*end == ')' || *end == '}') && !brackets--)
254
0
          break;
255
256
0
      break;
257
258
0
    default:
259
0
      end = stream->stream + 1;
260
0
      break;
261
0
    }
262
263
0
  stream->this = stream->stream;
264
0
  stream->stream = end;
265
266
  /* We must have at least one byte in a token. */
267
0
  g_assert (stream->stream - stream->this >= 1);
268
269
0
  return TRUE;
270
0
}
271
272
static void
273
token_stream_next (TokenStream *stream)
274
0
{
275
0
  stream->this = NULL;
276
0
}
277
278
static gboolean
279
token_stream_peek (TokenStream *stream,
280
                   gchar        first_char)
281
0
{
282
0
  if (!token_stream_prepare (stream))
283
0
    return FALSE;
284
285
0
  return stream->stream - stream->this >= 1 &&
286
0
         stream->this[0] == first_char;
287
0
}
288
289
static gboolean
290
token_stream_peek2 (TokenStream *stream,
291
                    gchar        first_char,
292
                    gchar        second_char)
293
0
{
294
0
  if (!token_stream_prepare (stream))
295
0
    return FALSE;
296
297
0
  return stream->stream - stream->this >= 2 &&
298
0
         stream->this[0] == first_char &&
299
0
         stream->this[1] == second_char;
300
0
}
301
302
static gboolean
303
token_stream_is_keyword (TokenStream *stream)
304
0
{
305
0
  if (!token_stream_prepare (stream))
306
0
    return FALSE;
307
308
0
  return stream->stream - stream->this >= 2 &&
309
0
         g_ascii_isalpha (stream->this[0]) &&
310
0
         g_ascii_isalpha (stream->this[1]);
311
0
}
312
313
static gboolean
314
token_stream_is_numeric (TokenStream *stream)
315
0
{
316
0
  if (!token_stream_prepare (stream))
317
0
    return FALSE;
318
319
0
  return (stream->stream - stream->this >= 1 &&
320
0
          (g_ascii_isdigit (stream->this[0]) ||
321
0
           stream->this[0] == '-' ||
322
0
           stream->this[0] == '+' ||
323
0
           stream->this[0] == '.'));
324
0
}
325
326
static gboolean
327
token_stream_peek_string (TokenStream *stream,
328
                          const gchar *token)
329
0
{
330
0
  size_t length = strlen (token);
331
332
0
  return token_stream_prepare (stream) &&
333
0
         (size_t) (stream->stream - stream->this) == length &&
334
0
         memcmp (stream->this, token, length) == 0;
335
0
}
336
337
static gboolean
338
token_stream_consume (TokenStream *stream,
339
                      const gchar *token)
340
0
{
341
0
  if (!token_stream_peek_string (stream, token))
342
0
    return FALSE;
343
344
0
  token_stream_next (stream);
345
0
  return TRUE;
346
0
}
347
348
static gboolean
349
token_stream_require (TokenStream  *stream,
350
                      const gchar  *token,
351
                      const gchar  *purpose,
352
                      GError      **error)
353
0
{
354
355
0
  if (!token_stream_consume (stream, token))
356
0
    {
357
0
      token_stream_set_error (stream, error, FALSE,
358
0
                              G_VARIANT_PARSE_ERROR_UNEXPECTED_TOKEN,
359
0
                              "expected '%s'%s", token, purpose);
360
0
      return FALSE;
361
0
    }
362
363
0
  return TRUE;
364
0
}
365
366
static void
367
token_stream_assert (TokenStream *stream,
368
                     const gchar *token)
369
0
{
370
0
  gboolean correct_token G_GNUC_UNUSED  /* when compiling with G_DISABLE_ASSERT */;
371
372
0
  correct_token = token_stream_consume (stream, token);
373
0
  g_assert (correct_token);
374
0
}
375
376
static gchar *
377
token_stream_get (TokenStream *stream)
378
0
{
379
0
  gchar *result;
380
381
0
  if (!token_stream_prepare (stream))
382
0
    return NULL;
383
384
0
  result = g_strndup (stream->this, stream->stream - stream->this);
385
386
0
  return result;
387
0
}
388
389
static void
390
token_stream_start_ref (TokenStream *stream,
391
                        SourceRef   *ref)
392
0
{
393
0
  token_stream_prepare (stream);
394
0
  ref->start = stream->this - stream->start;
395
0
}
396
397
static void
398
token_stream_end_ref (TokenStream *stream,
399
                      SourceRef   *ref)
400
0
{
401
0
  ref->end = stream->stream - stream->start;
402
0
}
403
404
/* This is guaranteed to write exactly as many bytes to `out` as it consumes
405
 * from `in`. i.e. The `out` buffer doesn’t need to be any longer than `in`. */
406
static void
407
pattern_copy (gchar       **out,
408
              const gchar **in)
409
0
{
410
0
  gint brackets = 0;
411
412
0
  while (**in == 'a' || **in == 'm' || **in == 'M')
413
0
    *(*out)++ = *(*in)++;
414
415
0
  do
416
0
    {
417
0
      if (**in == '(' || **in == '{')
418
0
        brackets++;
419
420
0
      else if (**in == ')' || **in == '}')
421
0
        brackets--;
422
423
0
      *(*out)++ = *(*in)++;
424
0
    }
425
0
  while (brackets);
426
0
}
427
428
/* Returns the most general pattern that is subpattern of left and subpattern
429
 * of right, or NULL if there is no such pattern. */
430
static gchar *
431
pattern_coalesce (const gchar *left,
432
                  const gchar *right)
433
0
{
434
0
  gchar *result;
435
0
  gchar *out;
436
0
  size_t buflen;
437
0
  size_t left_len = strlen (left), right_len = strlen (right);
438
439
  /* the length of the output is loosely bound by the sum of the input
440
   * lengths, not simply the greater of the two lengths.
441
   *
442
   *   (*(iii)) + ((iii)*) = ((iii)(iii))
443
   *
444
   *      8     +    8     = 12
445
   *
446
   * This can be proven by the fact that `out` is never incremented by more
447
   * bytes than are consumed from `left` or `right` in each iteration.
448
   */
449
0
  g_assert (left_len < G_MAXSIZE - right_len);
450
0
  buflen = left_len + right_len + 1;
451
0
  out = result = g_malloc (buflen);
452
453
0
  while (*left && *right)
454
0
    {
455
0
      if (*left == *right)
456
0
        {
457
0
          *out++ = *left++;
458
0
          right++;
459
0
        }
460
461
0
      else
462
0
        {
463
0
          const gchar **one = &left, **the_other = &right;
464
465
0
         again:
466
0
          if (**one == '*' && **the_other != ')')
467
0
            {
468
0
              pattern_copy (&out, the_other);
469
0
              (*one)++;
470
0
            }
471
472
0
          else if (**one == 'M' && **the_other == 'm')
473
0
            {
474
0
              *out++ = *(*the_other)++;
475
0
            }
476
477
0
          else if (**one == 'M' && **the_other != 'm' && **the_other != '*')
478
0
            {
479
0
              (*one)++;
480
0
            }
481
482
0
          else if (**one == 'N' && strchr ("ynqiuxthd", **the_other))
483
0
            {
484
0
              *out++ = *(*the_other)++;
485
0
              (*one)++;
486
0
            }
487
488
0
          else if (**one == 'S' && strchr ("sog", **the_other))
489
0
            {
490
0
              *out++ = *(*the_other)++;
491
0
              (*one)++;
492
0
            }
493
494
0
          else if (one == &left)
495
0
            {
496
0
              one = &right, the_other = &left;
497
0
              goto again;
498
0
            }
499
500
0
          else
501
0
            break;
502
0
        }
503
0
    }
504
505
  /* Need at least one byte remaining for trailing nul. */
506
0
  g_assert (out < result + buflen);
507
508
0
  if (*left || *right)
509
0
    {
510
0
      g_free (result);
511
0
      result = NULL;
512
0
    }
513
0
  else
514
0
    *out++ = '\0';
515
516
0
  return result;
517
0
}
518
519
typedef struct _AST AST;
520
typedef gchar *    (*get_pattern_func)    (AST                 *ast,
521
                                           GError             **error);
522
typedef GVariant * (*get_value_func)      (AST                 *ast,
523
                                           const GVariantType  *type,
524
                                           GError             **error);
525
typedef GVariant * (*get_base_value_func) (AST                 *ast,
526
                                           const GVariantType  *type,
527
                                           GError             **error);
528
typedef void       (*free_func)           (AST                 *ast);
529
530
typedef struct
531
{
532
  gchar *    (* get_pattern)    (AST                 *ast,
533
                                 GError             **error);
534
  GVariant * (* get_value)      (AST                 *ast,
535
                                 const GVariantType  *type,
536
                                 GError             **error);
537
  GVariant * (* get_base_value) (AST                 *ast,
538
                                 const GVariantType  *type,
539
                                 GError             **error);
540
  void       (* free)           (AST                 *ast);
541
} ASTClass;
542
543
struct _AST
544
{
545
  const ASTClass *class;
546
  SourceRef source_ref;
547
};
548
549
static gchar *
550
ast_get_pattern (AST     *ast,
551
                 GError **error)
552
0
{
553
0
  return ast->class->get_pattern (ast, error);
554
0
}
555
556
static GVariant *
557
ast_get_value (AST                 *ast,
558
               const GVariantType  *type,
559
               GError             **error)
560
0
{
561
0
  return ast->class->get_value (ast, type, error);
562
0
}
563
564
static void
565
ast_free (AST *ast)
566
0
{
567
0
  ast->class->free (ast);
568
0
}
569
570
G_GNUC_PRINTF(5, 6)
571
static void
572
ast_set_error (AST          *ast,
573
               GError      **error,
574
               AST          *other_ast,
575
               gint          code,
576
               const gchar  *format,
577
               ...)
578
0
{
579
0
  va_list ap;
580
581
0
  va_start (ap, format);
582
0
  parser_set_error_va (error, &ast->source_ref,
583
0
                       other_ast ? & other_ast->source_ref : NULL,
584
0
                       code,
585
0
                       format, ap);
586
0
  va_end (ap);
587
0
}
588
589
static GVariant *
590
ast_type_error (AST                 *ast,
591
                const GVariantType  *type,
592
                GError             **error)
593
0
{
594
0
  gchar *typestr;
595
596
0
  typestr = g_variant_type_dup_string (type);
597
0
  ast_set_error (ast, error, NULL,
598
0
                 G_VARIANT_PARSE_ERROR_TYPE_ERROR,
599
0
                 "can not parse as value of type '%s'",
600
0
                 typestr);
601
0
  g_free (typestr);
602
603
0
  return NULL;
604
0
}
605
606
static GVariant *
607
ast_resolve (AST     *ast,
608
             GError **error)
609
0
{
610
0
  GVariant *value;
611
0
  gchar *pattern;
612
0
  gint i, j = 0;
613
614
0
  pattern = ast_get_pattern (ast, error);
615
616
0
  if (pattern == NULL)
617
0
    return NULL;
618
619
  /* choose reasonable defaults
620
   *
621
   *   1) favour non-maybe values where possible
622
   *   2) default type for strings is 's'
623
   *   3) default type for integers is 'i'
624
   */
625
0
  for (i = 0; pattern[i]; i++)
626
0
    switch (pattern[i])
627
0
      {
628
0
      case '*':
629
0
        ast_set_error (ast, error, NULL,
630
0
                       G_VARIANT_PARSE_ERROR_CANNOT_INFER_TYPE,
631
0
                       "unable to infer type");
632
0
        g_free (pattern);
633
0
        return NULL;
634
635
0
      case 'M':
636
0
        break;
637
638
0
      case 'S':
639
0
        pattern[j++] = 's';
640
0
        break;
641
642
0
      case 'N':
643
0
        pattern[j++] = 'i';
644
0
        break;
645
646
0
      default:
647
0
        pattern[j++] = pattern[i];
648
0
        break;
649
0
      }
650
0
  pattern[j++] = '\0';
651
652
0
  value = ast_get_value (ast, G_VARIANT_TYPE (pattern), error);
653
0
  g_free (pattern);
654
655
0
  return value;
656
0
}
657
658
659
static AST *parse (TokenStream  *stream,
660
                   guint         max_depth,
661
                   va_list      *app,
662
                   GError      **error);
663
664
static void
665
ast_array_append (AST  ***array,
666
                  gint   *n_items,
667
                  AST    *ast)
668
0
{
669
0
  if ((*n_items & (*n_items - 1)) == 0)
670
0
    *array = g_renew (AST *, *array, *n_items ? 2 ** n_items : 1);
671
672
0
  (*array)[(*n_items)++] = ast;
673
0
}
674
675
static void
676
ast_array_free (AST  **array,
677
                gint   n_items)
678
0
{
679
0
  gint i;
680
681
0
  for (i = 0; i < n_items; i++)
682
0
    ast_free (array[i]);
683
0
  g_free (array);
684
0
}
685
686
static gchar *
687
ast_array_get_pattern (AST    **array,
688
                       gint     n_items,
689
                       GError **error)
690
0
{
691
0
  gchar *pattern;
692
0
  gint i;
693
694
  /* Find the pattern which applies to all children in the array, by l-folding a
695
   * coalesce operation.
696
   */
697
0
  pattern = ast_get_pattern (array[0], error);
698
699
0
  if (pattern == NULL)
700
0
    return NULL;
701
702
0
  for (i = 1; i < n_items; i++)
703
0
    {
704
0
      gchar *tmp, *merged;
705
706
0
      tmp = ast_get_pattern (array[i], error);
707
708
0
      if (tmp == NULL)
709
0
        {
710
0
          g_free (pattern);
711
0
          return NULL;
712
0
        }
713
714
0
      merged = pattern_coalesce (pattern, tmp);
715
0
      g_free (pattern);
716
0
      pattern = merged;
717
718
0
      if (merged == NULL)
719
        /* set coalescence implies pairwise coalescence (i think).
720
         * we should therefore be able to trace the failure to a single
721
         * pair of values.
722
         */
723
0
        {
724
0
          int j = 0;
725
726
0
          while (TRUE)
727
0
            {
728
0
              gchar *tmp2;
729
0
              gchar *m;
730
731
              /* if 'j' reaches 'i' then we didn't find the pair that failed
732
               * to coalesce. This shouldn't happen (see above), but just in
733
               * case report an error:
734
               */
735
0
              if (j >= i)
736
0
                {
737
0
                  ast_set_error (array[i], error, NULL,
738
0
                                 G_VARIANT_PARSE_ERROR_NO_COMMON_TYPE,
739
0
                                 "unable to find a common type");
740
0
                  g_free (tmp);
741
0
                  return NULL;
742
0
                }
743
744
0
              tmp2 = ast_get_pattern (array[j], NULL);
745
0
              g_assert (tmp2 != NULL);
746
747
0
              m = pattern_coalesce (tmp, tmp2);
748
0
              g_free (tmp2);
749
0
              g_free (m);
750
751
0
              if (m == NULL)
752
0
                {
753
                  /* we found a conflict between 'i' and 'j'.
754
                   *
755
                   * report the error.  note: 'j' is first.
756
                   */
757
0
                  ast_set_error (array[j], error, array[i],
758
0
                                 G_VARIANT_PARSE_ERROR_NO_COMMON_TYPE,
759
0
                                 "unable to find a common type");
760
0
                  g_free (tmp);
761
0
                  return NULL;
762
0
                }
763
764
0
              j++;
765
0
            }
766
767
0
        }
768
769
0
      g_free (tmp);
770
0
    }
771
772
0
  return pattern;
773
0
}
774
775
typedef struct
776
{
777
  AST ast;
778
779
  AST *child;
780
} Maybe;
781
782
static gchar *
783
maybe_get_pattern (AST     *ast,
784
                   GError **error)
785
0
{
786
0
  Maybe *maybe = (Maybe *) ast;
787
788
0
  if (maybe->child != NULL)
789
0
    {
790
0
      gchar *child_pattern;
791
0
      gchar *pattern;
792
793
0
      child_pattern = ast_get_pattern (maybe->child, error);
794
795
0
      if (child_pattern == NULL)
796
0
        return NULL;
797
798
0
      pattern = g_strdup_printf ("m%s", child_pattern);
799
0
      g_free (child_pattern);
800
801
0
      return pattern;
802
0
    }
803
804
0
  return g_strdup ("m*");
805
0
}
806
807
static GVariant *
808
maybe_get_value (AST                 *ast,
809
                 const GVariantType  *type,
810
                 GError             **error)
811
0
{
812
0
  Maybe *maybe = (Maybe *) ast;
813
0
  GVariant *value;
814
815
0
  if (!g_variant_type_is_maybe (type))
816
0
    return ast_type_error (ast, type, error);
817
818
0
  type = g_variant_type_element (type);
819
820
0
  if (maybe->child)
821
0
    {
822
0
      value = ast_get_value (maybe->child, type, error);
823
824
0
      if (value == NULL)
825
0
        return NULL;
826
0
    }
827
0
  else
828
0
    value = NULL;
829
830
0
  return g_variant_new_maybe (type, value);
831
0
}
832
833
static void
834
maybe_free (AST *ast)
835
0
{
836
0
  Maybe *maybe = (Maybe *) ast;
837
838
0
  if (maybe->child != NULL)
839
0
    ast_free (maybe->child);
840
841
0
  g_slice_free (Maybe, maybe);
842
0
}
843
844
static AST *
845
maybe_parse (TokenStream  *stream,
846
             guint         max_depth,
847
             va_list      *app,
848
             GError      **error)
849
0
{
850
0
  static const ASTClass maybe_class = {
851
0
    maybe_get_pattern,
852
0
    maybe_get_value, NULL,
853
0
    maybe_free
854
0
  };
855
0
  AST *child = NULL;
856
0
  Maybe *maybe;
857
858
0
  if (token_stream_consume (stream, "just"))
859
0
    {
860
0
      child = parse (stream, max_depth - 1, app, error);
861
0
      if (child == NULL)
862
0
        return NULL;
863
0
    }
864
865
0
  else if (!token_stream_consume (stream, "nothing"))
866
0
    {
867
0
      token_stream_set_error (stream, error, TRUE,
868
0
                              G_VARIANT_PARSE_ERROR_UNKNOWN_KEYWORD,
869
0
                              "unknown keyword");
870
0
      return NULL;
871
0
    }
872
873
0
  maybe = g_slice_new (Maybe);
874
0
  maybe->ast.class = &maybe_class;
875
0
  maybe->child = child;
876
877
0
  return (AST *) maybe;
878
0
}
879
880
static GVariant *
881
maybe_wrapper (AST                 *ast,
882
               const GVariantType  *type,
883
               GError             **error)
884
0
{
885
0
  const GVariantType *base_type;
886
0
  GVariant *base_value;
887
0
  GVariant *value = NULL;
888
0
  unsigned int depth;
889
0
  gboolean trusted;
890
0
  GVariantTypeInfo *base_type_info = NULL;
891
0
  gsize base_serialised_fixed_size, base_serialised_size, serialised_size, n_suffix_zeros;
892
0
  guint8 *serialised = NULL;
893
0
  GBytes *bytes = NULL;
894
0
  gsize i;
895
896
0
  for (depth = 0, base_type = type;
897
0
       g_variant_type_is_maybe (base_type);
898
0
       depth++, base_type = g_variant_type_element (base_type));
899
900
0
  base_value = ast->class->get_base_value (ast, base_type, error);
901
902
0
  if (base_value == NULL || depth == 0)
903
0
    return g_steal_pointer (&base_value);
904
905
  /* This is the equivalent of calling g_variant_new_maybe() in a loop enough
906
   * times to match the number of nested maybe types in @type. It does the same
907
   * in a single `GVariant` allocation, though.
908
   *
909
   * This avoids maybe_wrapper() becoming an attack vector where a malicious
910
   * text-form variant can create a long array, and insert a typedecl for a
911
   * deeply nested maybe type on one of its elements. This is achievable with a
912
   * relatively short text form, but results in O(array length × typedecl depth)
913
   * allocations. This is a denial of service attack.
914
   *
915
   * Instead of constructing a tree of `GVariant`s in tree-form to match the
916
   * @ast, construct a single `GVariant` containing the serialised form of the
917
   * maybe-wrappers and the base value that they contain. This is relatively
918
   * straightforward: serialise the base value, and then append the correct
919
   * number of zero bytes for the maybe-wrappers.
920
   *
921
   * This is a bit of a layering violation, unfortunately.
922
   *
923
   * By doing this, the typedecl depth variable is reduced to O(1).
924
   */
925
0
  trusted = g_variant_is_trusted (base_value);
926
927
  /* See https://developer.gnome.org/documentation/specifications/gvariant-specification-1.0.html#maybes
928
   *
929
   * The serialised form of a `Just x` is the serialised form of `x` if `x` is
930
   * fixed-size, and the serialised form of `x` plus a trailing zero byte if `x`
931
   * is variable-size. A `Maybe` variant is always variable-size, even if its
932
   * child element is fixed-size, because it might be `Nothing`. This means that
933
   * all the maybe-wrappers which are not the innermost are always serialised
934
   * with one trailing zero byte each.
935
   *
936
   * The serialised form of a `Nothing` is an empty byte sequence, but that’s
937
   * already handled above in the `base_value == NULL` case.
938
   */
939
0
  base_type_info = g_variant_type_info_get (base_type);
940
0
  g_variant_type_info_query (base_type_info, NULL, &base_serialised_fixed_size);
941
0
  g_variant_type_info_unref (base_type_info);
942
943
0
  base_serialised_size = g_variant_get_size (base_value);
944
0
  n_suffix_zeros = (base_serialised_fixed_size > 0) ? depth - 1 : depth;
945
0
  g_assert (base_serialised_size <= G_MAXSIZE - n_suffix_zeros);
946
0
  serialised_size = base_serialised_size + n_suffix_zeros;
947
948
0
  g_assert (serialised_size >= base_serialised_size);
949
950
  /* Serialise the base value. */
951
0
  serialised = g_malloc (serialised_size);
952
0
  g_variant_store (base_value, serialised);
953
954
  /* Zero-out the suffix zeros to complete the serialisation of the maybe wrappers. */
955
0
  for (i = base_serialised_size; i < serialised_size; i++)
956
0
    serialised[i] = 0;
957
958
0
  bytes = g_bytes_new_take (g_steal_pointer (&serialised), serialised_size);
959
0
  value = g_variant_new_from_bytes (type, bytes, trusted);
960
0
  g_bytes_unref (bytes);
961
962
0
  g_variant_unref (base_value);
963
964
0
  return g_steal_pointer (&value);
965
0
}
966
967
typedef struct
968
{
969
  AST ast;
970
971
  AST **children;
972
  gint n_children;
973
} Array;
974
975
static gchar *
976
array_get_pattern (AST     *ast,
977
                   GError **error)
978
0
{
979
0
  Array *array = (Array *) ast;
980
0
  gchar *pattern;
981
0
  gchar *result;
982
983
0
  if (array->n_children == 0)
984
0
    return g_strdup ("Ma*");
985
986
0
  pattern = ast_array_get_pattern (array->children, array->n_children, error);
987
988
0
  if (pattern == NULL)
989
0
    return NULL;
990
991
0
  result = g_strdup_printf ("Ma%s", pattern);
992
0
  g_free (pattern);
993
994
0
  return result;
995
0
}
996
997
static GVariant *
998
array_get_value (AST                 *ast,
999
                 const GVariantType  *type,
1000
                 GError             **error)
1001
0
{
1002
0
  Array *array = (Array *) ast;
1003
0
  const GVariantType *childtype;
1004
0
  GVariantBuilder builder;
1005
0
  gint i;
1006
1007
0
  if (!g_variant_type_is_array (type))
1008
0
    return ast_type_error (ast, type, error);
1009
1010
0
  g_variant_builder_init_static (&builder, type);
1011
0
  childtype = g_variant_type_element (type);
1012
1013
0
  for (i = 0; i < array->n_children; i++)
1014
0
    {
1015
0
      GVariant *child;
1016
1017
0
      if (!(child = ast_get_value (array->children[i], childtype, error)))
1018
0
        {
1019
0
          g_variant_builder_clear (&builder);
1020
0
          return NULL;
1021
0
        }
1022
1023
0
      g_variant_builder_add_value (&builder, child);
1024
0
    }
1025
1026
0
  return g_variant_builder_end (&builder);
1027
0
}
1028
1029
static void
1030
array_free (AST *ast)
1031
0
{
1032
0
  Array *array = (Array *) ast;
1033
1034
0
  ast_array_free (array->children, array->n_children);
1035
0
  g_slice_free (Array, array);
1036
0
}
1037
1038
static AST *
1039
array_parse (TokenStream  *stream,
1040
             guint         max_depth,
1041
             va_list      *app,
1042
             GError      **error)
1043
0
{
1044
0
  static const ASTClass array_class = {
1045
0
    array_get_pattern,
1046
0
    maybe_wrapper, array_get_value,
1047
0
    array_free
1048
0
  };
1049
0
  gboolean need_comma = FALSE;
1050
0
  Array *array;
1051
1052
0
  array = g_slice_new (Array);
1053
0
  array->ast.class = &array_class;
1054
0
  array->children = NULL;
1055
0
  array->n_children = 0;
1056
1057
0
  token_stream_assert (stream, "[");
1058
0
  while (!token_stream_consume (stream, "]"))
1059
0
    {
1060
0
      AST *child;
1061
1062
0
      if (need_comma &&
1063
0
          !token_stream_require (stream, ",",
1064
0
                                 " or ']' to follow array element",
1065
0
                                 error))
1066
0
        goto error;
1067
1068
0
      child = parse (stream, max_depth - 1, app, error);
1069
1070
0
      if (!child)
1071
0
        goto error;
1072
1073
0
      ast_array_append (&array->children, &array->n_children, child);
1074
0
      need_comma = TRUE;
1075
0
    }
1076
1077
0
  return (AST *) array;
1078
1079
0
 error:
1080
0
  ast_array_free (array->children, array->n_children);
1081
0
  g_slice_free (Array, array);
1082
1083
0
  return NULL;
1084
0
}
1085
1086
typedef struct
1087
{
1088
  AST ast;
1089
1090
  AST **children;
1091
  gint n_children;
1092
} Tuple;
1093
1094
static gchar *
1095
tuple_get_pattern (AST     *ast,
1096
                   GError **error)
1097
0
{
1098
0
  Tuple *tuple = (Tuple *) ast;
1099
0
  gchar *result = NULL;
1100
0
  gchar **parts;
1101
0
  gint i;
1102
1103
0
  parts = g_new (gchar *, tuple->n_children + 4);
1104
0
  parts[tuple->n_children + 1] = (gchar *) ")";
1105
0
  parts[tuple->n_children + 2] = NULL;
1106
0
  parts[0] = (gchar *) "M(";
1107
1108
0
  for (i = 0; i < tuple->n_children; i++)
1109
0
    if (!(parts[i + 1] = ast_get_pattern (tuple->children[i], error)))
1110
0
      break;
1111
1112
0
  if (i == tuple->n_children)
1113
0
    result = g_strjoinv ("", parts);
1114
1115
  /* parts[0] should not be freed */
1116
0
  while (i)
1117
0
    g_free (parts[i--]);
1118
0
  g_free (parts);
1119
1120
0
  return result;
1121
0
}
1122
1123
static GVariant *
1124
tuple_get_value (AST                 *ast,
1125
                 const GVariantType  *type,
1126
                 GError             **error)
1127
0
{
1128
0
  Tuple *tuple = (Tuple *) ast;
1129
0
  const GVariantType *childtype;
1130
0
  GVariantBuilder builder;
1131
0
  gint i;
1132
1133
0
  if (!g_variant_type_is_tuple (type))
1134
0
    return ast_type_error (ast, type, error);
1135
1136
0
  g_variant_builder_init_static (&builder, type);
1137
0
  childtype = g_variant_type_first (type);
1138
1139
0
  for (i = 0; i < tuple->n_children; i++)
1140
0
    {
1141
0
      GVariant *child;
1142
1143
0
      if (childtype == NULL)
1144
0
        {
1145
0
          g_variant_builder_clear (&builder);
1146
0
          return ast_type_error (ast, type, error);
1147
0
        }
1148
1149
0
      if (!(child = ast_get_value (tuple->children[i], childtype, error)))
1150
0
        {
1151
0
          g_variant_builder_clear (&builder);
1152
0
          return FALSE;
1153
0
        }
1154
1155
0
      g_variant_builder_add_value (&builder, child);
1156
0
      childtype = g_variant_type_next (childtype);
1157
0
    }
1158
1159
0
  if (childtype != NULL)
1160
0
    {
1161
0
      g_variant_builder_clear (&builder);
1162
0
      return ast_type_error (ast, type, error);
1163
0
    }
1164
1165
0
  return g_variant_builder_end (&builder);
1166
0
}
1167
1168
static void
1169
tuple_free (AST *ast)
1170
0
{
1171
0
  Tuple *tuple = (Tuple *) ast;
1172
1173
0
  ast_array_free (tuple->children, tuple->n_children);
1174
0
  g_slice_free (Tuple, tuple);
1175
0
}
1176
1177
static AST *
1178
tuple_parse (TokenStream  *stream,
1179
             guint         max_depth,
1180
             va_list      *app,
1181
             GError      **error)
1182
0
{
1183
0
  static const ASTClass tuple_class = {
1184
0
    tuple_get_pattern,
1185
0
    maybe_wrapper, tuple_get_value,
1186
0
    tuple_free
1187
0
  };
1188
0
  gboolean need_comma = FALSE;
1189
0
  gboolean first = TRUE;
1190
0
  Tuple *tuple;
1191
1192
0
  tuple = g_slice_new (Tuple);
1193
0
  tuple->ast.class = &tuple_class;
1194
0
  tuple->children = NULL;
1195
0
  tuple->n_children = 0;
1196
1197
0
  token_stream_assert (stream, "(");
1198
0
  while (!token_stream_consume (stream, ")"))
1199
0
    {
1200
0
      AST *child;
1201
1202
0
      if (need_comma &&
1203
0
          !token_stream_require (stream, ",",
1204
0
                                 " or ')' to follow tuple element",
1205
0
                                 error))
1206
0
        goto error;
1207
1208
0
      child = parse (stream, max_depth - 1, app, error);
1209
1210
0
      if (!child)
1211
0
        goto error;
1212
1213
0
      ast_array_append (&tuple->children, &tuple->n_children, child);
1214
1215
      /* the first time, we absolutely require a comma, so grab it here
1216
       * and leave need_comma = FALSE so that the code above doesn't
1217
       * require a second comma.
1218
       *
1219
       * the second and remaining times, we set need_comma = TRUE.
1220
       */
1221
0
      if (first)
1222
0
        {
1223
0
          if (!token_stream_require (stream, ",",
1224
0
                                     " after first tuple element", error))
1225
0
            goto error;
1226
1227
0
          first = FALSE;
1228
0
        }
1229
0
      else
1230
0
        need_comma = TRUE;
1231
0
    }
1232
1233
0
  return (AST *) tuple;
1234
1235
0
 error:
1236
0
  ast_array_free (tuple->children, tuple->n_children);
1237
0
  g_slice_free (Tuple, tuple);
1238
1239
0
  return NULL;
1240
0
}
1241
1242
typedef struct
1243
{
1244
  AST ast;
1245
1246
  AST *value;
1247
} Variant;
1248
1249
static gchar *
1250
variant_get_pattern (AST     *ast,
1251
                     GError **error)
1252
0
{
1253
0
  return g_strdup ("Mv");
1254
0
}
1255
1256
static GVariant *
1257
variant_get_value (AST                 *ast,
1258
                   const GVariantType  *type,
1259
                   GError             **error)
1260
0
{
1261
0
  Variant *variant = (Variant *) ast;
1262
0
  GVariant *child;
1263
1264
0
  if (!g_variant_type_equal (type, G_VARIANT_TYPE_VARIANT))
1265
0
    return ast_type_error (ast, type, error);
1266
1267
0
  child = ast_resolve (variant->value, error);
1268
1269
0
  if (child == NULL)
1270
0
    return NULL;
1271
1272
0
  return g_variant_new_variant (child);
1273
0
}
1274
1275
static void
1276
variant_free (AST *ast)
1277
0
{
1278
0
  Variant *variant = (Variant *) ast;
1279
1280
0
  ast_free (variant->value);
1281
0
  g_slice_free (Variant, variant);
1282
0
}
1283
1284
static AST *
1285
variant_parse (TokenStream  *stream,
1286
               guint         max_depth,
1287
               va_list      *app,
1288
               GError      **error)
1289
0
{
1290
0
  static const ASTClass variant_class = {
1291
0
    variant_get_pattern,
1292
0
    maybe_wrapper, variant_get_value,
1293
0
    variant_free
1294
0
  };
1295
0
  Variant *variant;
1296
0
  AST *value;
1297
1298
0
  token_stream_assert (stream, "<");
1299
0
  value = parse (stream, max_depth - 1, app, error);
1300
1301
0
  if (!value)
1302
0
    return NULL;
1303
1304
0
  if (!token_stream_require (stream, ">", " to follow variant value", error))
1305
0
    {
1306
0
      ast_free (value);
1307
0
      return NULL;
1308
0
    }
1309
1310
0
  variant = g_slice_new (Variant);
1311
0
  variant->ast.class = &variant_class;
1312
0
  variant->value = value;
1313
1314
0
  return (AST *) variant;
1315
0
}
1316
1317
typedef struct
1318
{
1319
  AST ast;
1320
1321
  AST **keys;
1322
  AST **values;
1323
  gint n_children;
1324
} Dictionary;
1325
1326
static gchar *
1327
dictionary_get_pattern (AST     *ast,
1328
                        GError **error)
1329
0
{
1330
0
  Dictionary *dict = (Dictionary *) ast;
1331
0
  gchar *value_pattern;
1332
0
  gchar *key_pattern;
1333
0
  gchar key_char;
1334
0
  gchar *result;
1335
1336
0
  if (dict->n_children == 0)
1337
0
    return g_strdup ("Ma{**}");
1338
1339
0
  key_pattern = ast_array_get_pattern (dict->keys,
1340
0
                                       abs (dict->n_children),
1341
0
                                       error);
1342
1343
0
  if (key_pattern == NULL)
1344
0
    return NULL;
1345
1346
  /* we can not have maybe keys */
1347
0
  if (key_pattern[0] == 'M')
1348
0
    key_char = key_pattern[1];
1349
0
  else
1350
0
    key_char = key_pattern[0];
1351
1352
0
  g_free (key_pattern);
1353
1354
  /* the basic types,
1355
   * plus undetermined number type and undetermined string type.
1356
   */
1357
0
  if (!strchr ("bynqiuxthdsogNS", key_char))
1358
0
    {
1359
0
      ast_set_error (ast, error, NULL,
1360
0
                     G_VARIANT_PARSE_ERROR_BASIC_TYPE_EXPECTED,
1361
0
                     "dictionary keys must have basic types");
1362
0
      return NULL;
1363
0
    }
1364
1365
0
  value_pattern = ast_get_pattern (dict->values[0], error);
1366
1367
0
  if (value_pattern == NULL)
1368
0
    return NULL;
1369
1370
0
  result = g_strdup_printf ("M%s{%c%s}",
1371
0
                            dict->n_children > 0 ? "a" : "",
1372
0
                            key_char, value_pattern);
1373
0
  g_free (value_pattern);
1374
1375
0
  return result;
1376
0
}
1377
1378
static GVariant *
1379
dictionary_get_value (AST                 *ast,
1380
                      const GVariantType  *type,
1381
                      GError             **error)
1382
0
{
1383
0
  Dictionary *dict = (Dictionary *) ast;
1384
1385
0
  if (dict->n_children == -1)
1386
0
    {
1387
0
      const GVariantType *subtype;
1388
0
      GVariantBuilder builder;
1389
0
      GVariant *subvalue;
1390
1391
0
      if (!g_variant_type_is_dict_entry (type))
1392
0
        return ast_type_error (ast, type, error);
1393
1394
0
      g_variant_builder_init_static (&builder, type);
1395
1396
0
      subtype = g_variant_type_key (type);
1397
0
      if (!(subvalue = ast_get_value (dict->keys[0], subtype, error)))
1398
0
        {
1399
0
          g_variant_builder_clear (&builder);
1400
0
          return NULL;
1401
0
        }
1402
0
      g_variant_builder_add_value (&builder, subvalue);
1403
1404
0
      subtype = g_variant_type_value (type);
1405
0
      if (!(subvalue = ast_get_value (dict->values[0], subtype, error)))
1406
0
        {
1407
0
          g_variant_builder_clear (&builder);
1408
0
          return NULL;
1409
0
        }
1410
0
      g_variant_builder_add_value (&builder, subvalue);
1411
1412
0
      return g_variant_builder_end (&builder);
1413
0
    }
1414
0
  else
1415
0
    {
1416
0
      const GVariantType *entry, *key, *val;
1417
0
      GVariantBuilder builder;
1418
0
      gint i;
1419
1420
0
      if (!g_variant_type_is_subtype_of (type, G_VARIANT_TYPE_DICTIONARY))
1421
0
        return ast_type_error (ast, type, error);
1422
1423
0
      entry = g_variant_type_element (type);
1424
0
      key = g_variant_type_key (entry);
1425
0
      val = g_variant_type_value (entry);
1426
1427
0
      g_variant_builder_init_static (&builder, type);
1428
1429
0
      for (i = 0; i < dict->n_children; i++)
1430
0
        {
1431
0
          GVariant *subvalue;
1432
1433
0
          g_variant_builder_open (&builder, entry);
1434
1435
0
          if (!(subvalue = ast_get_value (dict->keys[i], key, error)))
1436
0
            {
1437
0
              g_variant_builder_clear (&builder);
1438
0
              return NULL;
1439
0
            }
1440
0
          g_variant_builder_add_value (&builder, subvalue);
1441
1442
0
          if (!(subvalue = ast_get_value (dict->values[i], val, error)))
1443
0
            {
1444
0
              g_variant_builder_clear (&builder);
1445
0
              return NULL;
1446
0
            }
1447
0
          g_variant_builder_add_value (&builder, subvalue);
1448
0
          g_variant_builder_close (&builder);
1449
0
        }
1450
1451
0
      return g_variant_builder_end (&builder);
1452
0
    }
1453
0
}
1454
1455
static void
1456
dictionary_free (AST *ast)
1457
0
{
1458
0
  Dictionary *dict = (Dictionary *) ast;
1459
0
  gint n_children;
1460
1461
0
  if (dict->n_children > -1)
1462
0
    n_children = dict->n_children;
1463
0
  else
1464
0
    n_children = 1;
1465
1466
0
  ast_array_free (dict->keys, n_children);
1467
0
  ast_array_free (dict->values, n_children);
1468
0
  g_slice_free (Dictionary, dict);
1469
0
}
1470
1471
static AST *
1472
dictionary_parse (TokenStream  *stream,
1473
                  guint         max_depth,
1474
                  va_list      *app,
1475
                  GError      **error)
1476
0
{
1477
0
  static const ASTClass dictionary_class = {
1478
0
    dictionary_get_pattern,
1479
0
    maybe_wrapper, dictionary_get_value,
1480
0
    dictionary_free
1481
0
  };
1482
0
  gint n_keys, n_values;
1483
0
  gboolean only_one;
1484
0
  Dictionary *dict;
1485
0
  AST *first;
1486
1487
0
  dict = g_slice_new (Dictionary);
1488
0
  dict->ast.class = &dictionary_class;
1489
0
  dict->keys = NULL;
1490
0
  dict->values = NULL;
1491
0
  n_keys = n_values = 0;
1492
1493
0
  token_stream_assert (stream, "{");
1494
1495
0
  if (token_stream_consume (stream, "}"))
1496
0
    {
1497
0
      dict->n_children = 0;
1498
0
      return (AST *) dict;
1499
0
    }
1500
1501
0
  if ((first = parse (stream, max_depth - 1, app, error)) == NULL)
1502
0
    goto error;
1503
1504
0
  ast_array_append (&dict->keys, &n_keys, first);
1505
1506
0
  only_one = token_stream_consume (stream, ",");
1507
0
  if (!only_one &&
1508
0
      !token_stream_require (stream, ":",
1509
0
                             " or ',' to follow dictionary entry key",
1510
0
                             error))
1511
0
    goto error;
1512
1513
0
  if ((first = parse (stream, max_depth - 1, app, error)) == NULL)
1514
0
    goto error;
1515
1516
0
  ast_array_append (&dict->values, &n_values, first);
1517
1518
0
  if (only_one)
1519
0
    {
1520
0
      if (!token_stream_require (stream, "}", " at end of dictionary entry",
1521
0
                                 error))
1522
0
        goto error;
1523
1524
0
      g_assert (n_keys == 1 && n_values == 1);
1525
0
      dict->n_children = -1;
1526
1527
0
      return (AST *) dict;
1528
0
    }
1529
1530
0
  while (!token_stream_consume (stream, "}"))
1531
0
    {
1532
0
      AST *child;
1533
1534
0
      if (!token_stream_require (stream, ",",
1535
0
                                 " or '}' to follow dictionary entry", error))
1536
0
        goto error;
1537
1538
0
      child = parse (stream, max_depth - 1, app, error);
1539
1540
0
      if (!child)
1541
0
        goto error;
1542
1543
0
      ast_array_append (&dict->keys, &n_keys, child);
1544
1545
0
      if (!token_stream_require (stream, ":",
1546
0
                                 " to follow dictionary entry key", error))
1547
0
        goto error;
1548
1549
0
      child = parse (stream, max_depth - 1, app, error);
1550
1551
0
      if (!child)
1552
0
        goto error;
1553
1554
0
      ast_array_append (&dict->values, &n_values, child);
1555
0
    }
1556
1557
0
  g_assert (n_keys == n_values);
1558
0
  dict->n_children = n_keys;
1559
1560
0
  return (AST *) dict;
1561
1562
0
 error:
1563
0
  ast_array_free (dict->keys, n_keys);
1564
0
  ast_array_free (dict->values, n_values);
1565
0
  g_slice_free (Dictionary, dict);
1566
1567
0
  return NULL;
1568
0
}
1569
1570
typedef struct
1571
{
1572
  AST ast;
1573
  gchar *string;
1574
} String;
1575
1576
static gchar *
1577
string_get_pattern (AST     *ast,
1578
                    GError **error)
1579
0
{
1580
0
  return g_strdup ("MS");
1581
0
}
1582
1583
static GVariant *
1584
string_get_value (AST                 *ast,
1585
                  const GVariantType  *type,
1586
                  GError             **error)
1587
0
{
1588
0
  String *string = (String *) ast;
1589
1590
0
  if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
1591
0
    return g_variant_new_string (string->string);
1592
1593
0
  else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
1594
0
    {
1595
0
      if (!g_variant_is_object_path (string->string))
1596
0
        {
1597
0
          ast_set_error (ast, error, NULL,
1598
0
                         G_VARIANT_PARSE_ERROR_INVALID_OBJECT_PATH,
1599
0
                         "not a valid object path");
1600
0
          return NULL;
1601
0
        }
1602
1603
0
      return g_variant_new_object_path (string->string);
1604
0
    }
1605
1606
0
  else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
1607
0
    {
1608
0
      if (!g_variant_is_signature (string->string))
1609
0
        {
1610
0
          ast_set_error (ast, error, NULL,
1611
0
                         G_VARIANT_PARSE_ERROR_INVALID_SIGNATURE,
1612
0
                         "not a valid signature");
1613
0
          return NULL;
1614
0
        }
1615
1616
0
      return g_variant_new_signature (string->string);
1617
0
    }
1618
1619
0
  else
1620
0
    return ast_type_error (ast, type, error);
1621
0
}
1622
1623
static void
1624
string_free (AST *ast)
1625
0
{
1626
0
  String *string = (String *) ast;
1627
1628
0
  g_free (string->string);
1629
0
  g_slice_free (String, string);
1630
0
}
1631
1632
/* Accepts exactly @length hexadecimal digits. No leading sign or `0x`/`0X` prefix allowed.
1633
 * No leading/trailing space allowed.
1634
 *
1635
 * It's OK to pass a length greater than the actual length of the src buffer,
1636
 * provided src must be null-terminated.
1637
 */
1638
static gboolean
1639
unicode_unescape (const gchar  *src,
1640
                  gint         *src_ofs,
1641
                  gchar        *dest,
1642
                  gint         *dest_ofs,
1643
                  gsize         length,
1644
                  SourceRef    *ref,
1645
                  GError      **error)
1646
0
{
1647
0
  gchar buffer[9];
1648
0
  guint64 value = 0;
1649
0
  gchar *end = NULL;
1650
0
  gsize n_valid_chars;
1651
1652
0
  (*src_ofs)++;
1653
1654
0
  g_assert (length < sizeof (buffer));
1655
0
  strncpy (buffer, src + *src_ofs, length);
1656
0
  buffer[length] = '\0';
1657
1658
0
  for (n_valid_chars = 0; n_valid_chars < length; n_valid_chars++)
1659
0
    if (!g_ascii_isxdigit (buffer[n_valid_chars]))
1660
0
      break;
1661
1662
0
  if (n_valid_chars == length)
1663
0
    value = g_ascii_strtoull (buffer, &end, 0x10);
1664
1665
0
  if (value == 0 || end != buffer + length)
1666
0
    {
1667
0
      SourceRef escape_ref;
1668
1669
0
      escape_ref = *ref;
1670
0
      escape_ref.start += *src_ofs;
1671
0
      escape_ref.end = escape_ref.start + n_valid_chars;
1672
1673
0
      parser_set_error (error, &escape_ref, NULL,
1674
0
                        G_VARIANT_PARSE_ERROR_INVALID_CHARACTER,
1675
0
                        "invalid %" G_GSIZE_FORMAT "-character unicode escape", length);
1676
0
      return FALSE;
1677
0
    }
1678
1679
0
  g_assert (value <= G_MAXUINT32);
1680
1681
0
  *dest_ofs += g_unichar_to_utf8 (value, dest + *dest_ofs);
1682
0
  *src_ofs += length;
1683
1684
0
  return TRUE;
1685
0
}
1686
1687
static AST *
1688
string_parse (TokenStream  *stream,
1689
              va_list      *app,
1690
              GError      **error)
1691
0
{
1692
0
  static const ASTClass string_class = {
1693
0
    string_get_pattern,
1694
0
    maybe_wrapper, string_get_value,
1695
0
    string_free
1696
0
  };
1697
0
  String *string;
1698
0
  SourceRef ref;
1699
0
  gchar *token;
1700
0
  gsize length;
1701
0
  gchar quote;
1702
0
  gchar *str;
1703
0
  gint i, j;
1704
1705
0
  token_stream_start_ref (stream, &ref);
1706
0
  token = token_stream_get (stream);
1707
0
  token_stream_end_ref (stream, &ref);
1708
0
  length = strlen (token);
1709
0
  quote = token[0];
1710
1711
  /* The output will always be at least one byte smaller than the input,
1712
   * because we skip over the initial quote character.
1713
   */
1714
0
  str = g_malloc (length);
1715
0
  g_assert (quote == '"' || quote == '\'');
1716
0
  j = 0;
1717
0
  i = 1;
1718
0
  while (token[i] != quote)
1719
0
    switch (token[i])
1720
0
      {
1721
0
      case '\0':
1722
0
        parser_set_error (error, &ref, NULL,
1723
0
                          G_VARIANT_PARSE_ERROR_UNTERMINATED_STRING_CONSTANT,
1724
0
                          "unterminated string constant");
1725
0
        g_free (token);
1726
0
        g_free (str);
1727
0
        return NULL;
1728
1729
0
      case '\\':
1730
0
        switch (token[++i])
1731
0
          {
1732
0
          case '\0':
1733
0
            parser_set_error (error, &ref, NULL,
1734
0
                              G_VARIANT_PARSE_ERROR_UNTERMINATED_STRING_CONSTANT,
1735
0
                              "unterminated string constant");
1736
0
            g_free (token);
1737
0
            g_free (str);
1738
0
            return NULL;
1739
1740
0
          case 'u':
1741
0
            if (!unicode_unescape (token, &i, str, &j, 4, &ref, error))
1742
0
              {
1743
0
                g_free (token);
1744
0
                g_free (str);
1745
0
                return NULL;
1746
0
              }
1747
0
            continue;
1748
1749
0
          case 'U':
1750
0
            if (!unicode_unescape (token, &i, str, &j, 8, &ref, error))
1751
0
              {
1752
0
                g_free (token);
1753
0
                g_free (str);
1754
0
                return NULL;
1755
0
              }
1756
0
            continue;
1757
1758
0
          case 'a': str[j++] = '\a'; i++; continue;
1759
0
          case 'b': str[j++] = '\b'; i++; continue;
1760
0
          case 'f': str[j++] = '\f'; i++; continue;
1761
0
          case 'n': str[j++] = '\n'; i++; continue;
1762
0
          case 'r': str[j++] = '\r'; i++; continue;
1763
0
          case 't': str[j++] = '\t'; i++; continue;
1764
0
          case 'v': str[j++] = '\v'; i++; continue;
1765
0
          case '\n': i++; continue;
1766
0
          }
1767
1768
0
        G_GNUC_FALLTHROUGH;
1769
1770
0
      default:
1771
0
        str[j++] = token[i++];
1772
0
      }
1773
0
  str[j++] = '\0';
1774
0
  g_free (token);
1775
1776
0
  string = g_slice_new (String);
1777
0
  string->ast.class = &string_class;
1778
0
  string->string = str;
1779
1780
0
  token_stream_next (stream);
1781
1782
0
  return (AST *) string;
1783
0
}
1784
1785
typedef struct
1786
{
1787
  AST ast;
1788
  gchar *string;
1789
} ByteString;
1790
1791
static gchar *
1792
bytestring_get_pattern (AST     *ast,
1793
                        GError **error)
1794
0
{
1795
0
  return g_strdup ("May");
1796
0
}
1797
1798
static GVariant *
1799
bytestring_get_value (AST                 *ast,
1800
                      const GVariantType  *type,
1801
                      GError             **error)
1802
0
{
1803
0
  ByteString *string = (ByteString *) ast;
1804
1805
0
  if (!g_variant_type_equal (type, G_VARIANT_TYPE_BYTESTRING))
1806
0
    return ast_type_error (ast, type, error);
1807
1808
0
  return g_variant_new_bytestring (string->string);
1809
0
}
1810
1811
static void
1812
bytestring_free (AST *ast)
1813
0
{
1814
0
  ByteString *string = (ByteString *) ast;
1815
1816
0
  g_free (string->string);
1817
0
  g_slice_free (ByteString, string);
1818
0
}
1819
1820
static AST *
1821
bytestring_parse (TokenStream  *stream,
1822
                  va_list      *app,
1823
                  GError      **error)
1824
0
{
1825
0
  static const ASTClass bytestring_class = {
1826
0
    bytestring_get_pattern,
1827
0
    maybe_wrapper, bytestring_get_value,
1828
0
    bytestring_free
1829
0
  };
1830
0
  ByteString *string;
1831
0
  SourceRef ref;
1832
0
  gchar *token;
1833
0
  gsize length;
1834
0
  gchar quote;
1835
0
  gchar *str;
1836
0
  gint i, j;
1837
1838
0
  token_stream_start_ref (stream, &ref);
1839
0
  token = token_stream_get (stream);
1840
0
  token_stream_end_ref (stream, &ref);
1841
0
  g_assert (token[0] == 'b');
1842
0
  length = strlen (token);
1843
0
  quote = token[1];
1844
1845
  /* The output will always be smaller than the input, because we skip over the
1846
   * initial b and the quote character.
1847
   */
1848
0
  str = g_malloc (length);
1849
0
  g_assert (quote == '"' || quote == '\'');
1850
0
  j = 0;
1851
0
  i = 2;
1852
0
  while (token[i] != quote)
1853
0
    switch (token[i])
1854
0
      {
1855
0
      case '\0':
1856
0
        parser_set_error (error, &ref, NULL,
1857
0
                          G_VARIANT_PARSE_ERROR_UNTERMINATED_STRING_CONSTANT,
1858
0
                          "unterminated string constant");
1859
0
        g_free (str);
1860
0
        g_free (token);
1861
0
        return NULL;
1862
1863
0
      case '\\':
1864
0
        switch (token[++i])
1865
0
          {
1866
0
          case '\0':
1867
0
            parser_set_error (error, &ref, NULL,
1868
0
                              G_VARIANT_PARSE_ERROR_UNTERMINATED_STRING_CONSTANT,
1869
0
                              "unterminated string constant");
1870
0
            g_free (str);
1871
0
            g_free (token);
1872
0
            return NULL;
1873
1874
0
          case '0': case '1': case '2': case '3':
1875
0
          case '4': case '5': case '6': case '7':
1876
0
            {
1877
              /* up to 3 characters */
1878
0
              guchar val = token[i++] - '0';
1879
1880
0
              if ('0' <= token[i] && token[i] < '8')
1881
0
                val = (val << 3) | (token[i++] - '0');
1882
1883
0
              if ('0' <= token[i] && token[i] < '8')
1884
0
                val = (val << 3) | (token[i++] - '0');
1885
1886
0
              str[j++] = val;
1887
0
            }
1888
0
            continue;
1889
1890
0
          case 'a': str[j++] = '\a'; i++; continue;
1891
0
          case 'b': str[j++] = '\b'; i++; continue;
1892
0
          case 'f': str[j++] = '\f'; i++; continue;
1893
0
          case 'n': str[j++] = '\n'; i++; continue;
1894
0
          case 'r': str[j++] = '\r'; i++; continue;
1895
0
          case 't': str[j++] = '\t'; i++; continue;
1896
0
          case 'v': str[j++] = '\v'; i++; continue;
1897
0
          case '\n': i++; continue;
1898
0
          }
1899
1900
0
        G_GNUC_FALLTHROUGH;
1901
1902
0
      default:
1903
0
        str[j++] = token[i++];
1904
0
      }
1905
0
  str[j++] = '\0';
1906
0
  g_free (token);
1907
1908
0
  string = g_slice_new (ByteString);
1909
0
  string->ast.class = &bytestring_class;
1910
0
  string->string = str;
1911
1912
0
  token_stream_next (stream);
1913
1914
0
  return (AST *) string;
1915
0
}
1916
1917
typedef struct
1918
{
1919
  AST ast;
1920
1921
  gchar *token;
1922
} Number;
1923
1924
static gchar *
1925
number_get_pattern (AST     *ast,
1926
                    GError **error)
1927
0
{
1928
0
  Number *number = (Number *) ast;
1929
1930
0
  if (strchr (number->token, '.') ||
1931
0
      (!g_str_has_prefix (number->token, "0x") && strchr (number->token, 'e')) ||
1932
0
      strstr (number->token, "inf") ||
1933
0
      strstr (number->token, "nan"))
1934
0
    return g_strdup ("Md");
1935
1936
0
  return g_strdup ("MN");
1937
0
}
1938
1939
static GVariant *
1940
number_overflow (AST                 *ast,
1941
                 const GVariantType  *type,
1942
                 GError             **error)
1943
0
{
1944
0
  ast_set_error (ast, error, NULL,
1945
0
                 G_VARIANT_PARSE_ERROR_NUMBER_OUT_OF_RANGE,
1946
0
                 "number out of range for type '%c'",
1947
0
                 g_variant_type_peek_string (type)[0]);
1948
0
  return NULL;
1949
0
}
1950
1951
static GVariant *
1952
number_get_value (AST                 *ast,
1953
                  const GVariantType  *type,
1954
                  GError             **error)
1955
0
{
1956
0
  Number *number = (Number *) ast;
1957
0
  const gchar *token;
1958
0
  gboolean negative;
1959
0
  gboolean floating;
1960
0
  guint64 abs_val;
1961
0
  gdouble dbl_val;
1962
0
  gchar *end;
1963
1964
0
  token = number->token;
1965
1966
0
  if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
1967
0
    {
1968
0
      floating = TRUE;
1969
1970
0
      errno = 0;
1971
0
      dbl_val = g_ascii_strtod (token, &end);
1972
0
      if (dbl_val != 0.0 && errno == ERANGE)
1973
0
        {
1974
0
          ast_set_error (ast, error, NULL,
1975
0
                         G_VARIANT_PARSE_ERROR_NUMBER_TOO_BIG,
1976
0
                         "number too big for any type");
1977
0
          return NULL;
1978
0
        }
1979
1980
      /* silence uninitialised warnings... */
1981
0
      negative = FALSE;
1982
0
      abs_val = 0;
1983
0
    }
1984
0
  else
1985
0
    {
1986
0
      floating = FALSE;
1987
0
      negative = token[0] == '-';
1988
0
      if (token[0] == '-')
1989
0
        token++;
1990
1991
0
      errno = 0;
1992
0
      abs_val = g_ascii_strtoull (token, &end, 0);
1993
0
      if (abs_val == G_MAXUINT64 && errno == ERANGE)
1994
0
        {
1995
0
          ast_set_error (ast, error, NULL,
1996
0
                         G_VARIANT_PARSE_ERROR_NUMBER_TOO_BIG,
1997
0
                         "integer too big for any type");
1998
0
          return NULL;
1999
0
        }
2000
2001
0
      if (abs_val == 0)
2002
0
        negative = FALSE;
2003
2004
      /* silence uninitialised warning... */
2005
0
      dbl_val = 0.0;
2006
0
    }
2007
2008
0
  if (*end != '\0')
2009
0
    {
2010
0
      SourceRef ref;
2011
2012
0
      ref = ast->source_ref;
2013
0
      ref.start += end - number->token;
2014
0
      ref.end = ref.start + 1;
2015
2016
0
      parser_set_error (error, &ref, NULL,
2017
0
                        G_VARIANT_PARSE_ERROR_INVALID_CHARACTER,
2018
0
                        "invalid character in number");
2019
0
      return NULL;
2020
0
     }
2021
2022
0
  if (floating)
2023
0
    return g_variant_new_double (dbl_val);
2024
2025
0
  switch (*g_variant_type_peek_string (type))
2026
0
    {
2027
0
    case 'y':
2028
0
      if (negative || abs_val > G_MAXUINT8)
2029
0
        return number_overflow (ast, type, error);
2030
0
      return g_variant_new_byte (abs_val);
2031
2032
0
    case 'n':
2033
0
      if (abs_val - negative > G_MAXINT16)
2034
0
        return number_overflow (ast, type, error);
2035
0
      if (negative && abs_val > G_MAXINT16)
2036
0
        return g_variant_new_int16 (G_MININT16);
2037
0
      return g_variant_new_int16 (negative ?
2038
0
                                  -((gint16) abs_val) : ((gint16) abs_val));
2039
2040
0
    case 'q':
2041
0
      if (negative || abs_val > G_MAXUINT16)
2042
0
        return number_overflow (ast, type, error);
2043
0
      return g_variant_new_uint16 (abs_val);
2044
2045
0
    case 'i':
2046
0
      if (abs_val - negative > G_MAXINT32)
2047
0
        return number_overflow (ast, type, error);
2048
0
      if (negative && abs_val > G_MAXINT32)
2049
0
        return g_variant_new_int32 (G_MININT32);
2050
0
      return g_variant_new_int32 (negative ?
2051
0
                                  -((gint32) abs_val) : ((gint32) abs_val));
2052
2053
0
    case 'u':
2054
0
      if (negative || abs_val > G_MAXUINT32)
2055
0
        return number_overflow (ast, type, error);
2056
0
      return g_variant_new_uint32 (abs_val);
2057
2058
0
    case 'x':
2059
0
      if (abs_val - negative > G_MAXINT64)
2060
0
        return number_overflow (ast, type, error);
2061
0
      if (negative && abs_val > G_MAXINT64)
2062
0
        return g_variant_new_int64 (G_MININT64);
2063
0
      return g_variant_new_int64 (negative ?
2064
0
                                  -((gint64) abs_val) : ((gint64) abs_val));
2065
2066
0
    case 't':
2067
0
      if (negative)
2068
0
        return number_overflow (ast, type, error);
2069
0
      return g_variant_new_uint64 (abs_val);
2070
2071
0
    case 'h':
2072
0
      if (abs_val - negative > G_MAXINT32)
2073
0
        return number_overflow (ast, type, error);
2074
0
      if (negative && abs_val > G_MAXINT32)
2075
0
        return g_variant_new_handle (G_MININT32);
2076
0
      return g_variant_new_handle (negative ?
2077
0
                                   -((gint32) abs_val) : ((gint32) abs_val));
2078
2079
0
    default:
2080
0
      return ast_type_error (ast, type, error);
2081
0
    }
2082
0
}
2083
2084
static void
2085
number_free (AST *ast)
2086
0
{
2087
0
  Number *number = (Number *) ast;
2088
2089
0
  g_free (number->token);
2090
0
  g_slice_free (Number, number);
2091
0
}
2092
2093
static AST *
2094
number_parse (TokenStream  *stream,
2095
              va_list      *app,
2096
              GError      **error)
2097
0
{
2098
0
  static const ASTClass number_class = {
2099
0
    number_get_pattern,
2100
0
    maybe_wrapper, number_get_value,
2101
0
    number_free
2102
0
  };
2103
0
  Number *number;
2104
2105
0
  number = g_slice_new (Number);
2106
0
  number->ast.class = &number_class;
2107
0
  number->token = token_stream_get (stream);
2108
0
  token_stream_next (stream);
2109
2110
0
  return (AST *) number;
2111
0
}
2112
2113
typedef struct
2114
{
2115
  AST ast;
2116
  gboolean value;
2117
} Boolean;
2118
2119
static gchar *
2120
boolean_get_pattern (AST     *ast,
2121
                     GError **error)
2122
0
{
2123
0
  return g_strdup ("Mb");
2124
0
}
2125
2126
static GVariant *
2127
boolean_get_value (AST                 *ast,
2128
                   const GVariantType  *type,
2129
                   GError             **error)
2130
0
{
2131
0
  Boolean *boolean = (Boolean *) ast;
2132
2133
0
  if (!g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
2134
0
    return ast_type_error (ast, type, error);
2135
2136
0
  return g_variant_new_boolean (boolean->value);
2137
0
}
2138
2139
static void
2140
boolean_free (AST *ast)
2141
0
{
2142
0
  Boolean *boolean = (Boolean *) ast;
2143
2144
0
  g_slice_free (Boolean, boolean);
2145
0
}
2146
2147
static AST *
2148
boolean_new (gboolean value)
2149
0
{
2150
0
  static const ASTClass boolean_class = {
2151
0
    boolean_get_pattern,
2152
0
    maybe_wrapper, boolean_get_value,
2153
0
    boolean_free
2154
0
  };
2155
0
  Boolean *boolean;
2156
2157
0
  boolean = g_slice_new (Boolean);
2158
0
  boolean->ast.class = &boolean_class;
2159
0
  boolean->value = value;
2160
2161
0
  return (AST *) boolean;
2162
0
}
2163
2164
typedef struct
2165
{
2166
  AST ast;
2167
2168
  GVariant *value;
2169
} Positional;
2170
2171
static gchar *
2172
positional_get_pattern (AST     *ast,
2173
                        GError **error)
2174
0
{
2175
0
  Positional *positional = (Positional *) ast;
2176
2177
0
  return g_strdup (g_variant_get_type_string (positional->value));
2178
0
}
2179
2180
static GVariant *
2181
positional_get_value (AST                 *ast,
2182
                      const GVariantType  *type,
2183
                      GError             **error)
2184
0
{
2185
0
  Positional *positional = (Positional *) ast;
2186
0
  GVariant *value;
2187
2188
0
  g_assert (positional->value != NULL);
2189
2190
0
  if G_UNLIKELY (!g_variant_is_of_type (positional->value, type))
2191
0
    return ast_type_error (ast, type, error);
2192
2193
  /* NOTE: if _get is called more than once then
2194
   * things get messed up with respect to floating refs.
2195
   *
2196
   * fortunately, this function should only ever get called once.
2197
   */
2198
0
  g_assert (positional->value != NULL);
2199
0
  value = positional->value;
2200
0
  positional->value = NULL;
2201
2202
0
  return value;
2203
0
}
2204
2205
static void
2206
positional_free (AST *ast)
2207
0
{
2208
0
  Positional *positional = (Positional *) ast;
2209
2210
  /* if positional->value is set, just leave it.
2211
   * memory management doesn't matter in case of programmer error.
2212
   */
2213
0
  g_slice_free (Positional, positional);
2214
0
}
2215
2216
static AST *
2217
positional_parse (TokenStream  *stream,
2218
                  va_list      *app,
2219
                  GError      **error)
2220
0
{
2221
0
  static const ASTClass positional_class = {
2222
0
    positional_get_pattern,
2223
0
    positional_get_value, NULL,
2224
0
    positional_free
2225
0
  };
2226
0
  Positional *positional;
2227
0
  const gchar *endptr;
2228
0
  gchar *token;
2229
2230
0
  token = token_stream_get (stream);
2231
0
  g_assert (token[0] == '%');
2232
2233
0
  positional = g_slice_new (Positional);
2234
0
  positional->ast.class = &positional_class;
2235
0
  positional->value = g_variant_new_va (token + 1, &endptr, app);
2236
2237
0
  if (*endptr || positional->value == NULL)
2238
0
    {
2239
0
      token_stream_set_error (stream, error, TRUE,
2240
0
                              G_VARIANT_PARSE_ERROR_INVALID_FORMAT_STRING,
2241
0
                              "invalid GVariant format string");
2242
      /* memory management doesn't matter in case of programmer error. */
2243
0
      return NULL;
2244
0
    }
2245
2246
0
  token_stream_next (stream);
2247
0
  g_free (token);
2248
2249
0
  return (AST *) positional;
2250
0
}
2251
2252
typedef struct
2253
{
2254
  AST ast;
2255
2256
  GVariantType *type;
2257
  AST *child;
2258
} TypeDecl;
2259
2260
static gchar *
2261
typedecl_get_pattern (AST     *ast,
2262
                      GError **error)
2263
0
{
2264
0
  TypeDecl *decl = (TypeDecl *) ast;
2265
2266
0
  return g_variant_type_dup_string (decl->type);
2267
0
}
2268
2269
static GVariant *
2270
typedecl_get_value (AST                 *ast,
2271
                    const GVariantType  *type,
2272
                    GError             **error)
2273
0
{
2274
0
  TypeDecl *decl = (TypeDecl *) ast;
2275
2276
0
  return ast_get_value (decl->child, type, error);
2277
0
}
2278
2279
static void
2280
typedecl_free (AST *ast)
2281
0
{
2282
0
  TypeDecl *decl = (TypeDecl *) ast;
2283
2284
0
  ast_free (decl->child);
2285
0
  g_variant_type_free (decl->type);
2286
0
  g_slice_free (TypeDecl, decl);
2287
0
}
2288
2289
static AST *
2290
typedecl_parse (TokenStream  *stream,
2291
                guint         max_depth,
2292
                va_list      *app,
2293
                GError      **error)
2294
0
{
2295
0
  static const ASTClass typedecl_class = {
2296
0
    typedecl_get_pattern,
2297
0
    typedecl_get_value, NULL,
2298
0
    typedecl_free
2299
0
  };
2300
0
  GVariantType *type;
2301
0
  TypeDecl *decl;
2302
0
  AST *child;
2303
2304
0
  if (token_stream_peek (stream, '@'))
2305
0
    {
2306
0
      gchar *token;
2307
2308
0
      token = token_stream_get (stream);
2309
2310
0
      if (!g_variant_type_string_is_valid (token + 1))
2311
0
        {
2312
0
          token_stream_set_error (stream, error, TRUE,
2313
0
                                  G_VARIANT_PARSE_ERROR_INVALID_TYPE_STRING,
2314
0
                                  "invalid type declaration");
2315
0
          g_free (token);
2316
2317
0
          return NULL;
2318
0
        }
2319
2320
0
      if (g_variant_type_string_get_depth_ (token + 1) > max_depth)
2321
0
        {
2322
0
          token_stream_set_error (stream, error, TRUE,
2323
0
                                  G_VARIANT_PARSE_ERROR_RECURSION,
2324
0
                                  "type declaration recurses too deeply");
2325
0
          g_free (token);
2326
2327
0
          return NULL;
2328
0
        }
2329
2330
0
      type = g_variant_type_new (token + 1);
2331
2332
0
      if (!g_variant_type_is_definite (type))
2333
0
        {
2334
0
          token_stream_set_error (stream, error, TRUE,
2335
0
                                  G_VARIANT_PARSE_ERROR_DEFINITE_TYPE_EXPECTED,
2336
0
                                  "type declarations must be definite");
2337
0
          g_variant_type_free (type);
2338
0
          g_free (token);
2339
2340
0
          return NULL;
2341
0
        }
2342
2343
0
      token_stream_next (stream);
2344
0
      g_free (token);
2345
0
    }
2346
0
  else
2347
0
    {
2348
0
      if (token_stream_consume (stream, "boolean"))
2349
0
        type = g_variant_type_copy (G_VARIANT_TYPE_BOOLEAN);
2350
2351
0
      else if (token_stream_consume (stream, "byte"))
2352
0
        type = g_variant_type_copy (G_VARIANT_TYPE_BYTE);
2353
2354
0
      else if (token_stream_consume (stream, "int16"))
2355
0
        type = g_variant_type_copy (G_VARIANT_TYPE_INT16);
2356
2357
0
      else if (token_stream_consume (stream, "uint16"))
2358
0
        type = g_variant_type_copy (G_VARIANT_TYPE_UINT16);
2359
2360
0
      else if (token_stream_consume (stream, "int32"))
2361
0
        type = g_variant_type_copy (G_VARIANT_TYPE_INT32);
2362
2363
0
      else if (token_stream_consume (stream, "handle"))
2364
0
        type = g_variant_type_copy (G_VARIANT_TYPE_HANDLE);
2365
2366
0
      else if (token_stream_consume (stream, "uint32"))
2367
0
        type = g_variant_type_copy (G_VARIANT_TYPE_UINT32);
2368
2369
0
      else if (token_stream_consume (stream, "int64"))
2370
0
        type = g_variant_type_copy (G_VARIANT_TYPE_INT64);
2371
2372
0
      else if (token_stream_consume (stream, "uint64"))
2373
0
        type = g_variant_type_copy (G_VARIANT_TYPE_UINT64);
2374
2375
0
      else if (token_stream_consume (stream, "double"))
2376
0
        type = g_variant_type_copy (G_VARIANT_TYPE_DOUBLE);
2377
2378
0
      else if (token_stream_consume (stream, "string"))
2379
0
        type = g_variant_type_copy (G_VARIANT_TYPE_STRING);
2380
2381
0
      else if (token_stream_consume (stream, "objectpath"))
2382
0
        type = g_variant_type_copy (G_VARIANT_TYPE_OBJECT_PATH);
2383
2384
0
      else if (token_stream_consume (stream, "signature"))
2385
0
        type = g_variant_type_copy (G_VARIANT_TYPE_SIGNATURE);
2386
2387
0
      else
2388
0
        {
2389
0
          token_stream_set_error (stream, error, TRUE,
2390
0
                                  G_VARIANT_PARSE_ERROR_UNKNOWN_KEYWORD,
2391
0
                                  "unknown keyword");
2392
0
          return NULL;
2393
0
        }
2394
0
    }
2395
2396
0
  if ((child = parse (stream, max_depth - 1, app, error)) == NULL)
2397
0
    {
2398
0
      g_variant_type_free (type);
2399
0
      return NULL;
2400
0
    }
2401
2402
0
  decl = g_slice_new (TypeDecl);
2403
0
  decl->ast.class = &typedecl_class;
2404
0
  decl->type = type;
2405
0
  decl->child = child;
2406
2407
0
  return (AST *) decl;
2408
0
}
2409
2410
static AST *
2411
parse (TokenStream  *stream,
2412
       guint         max_depth,
2413
       va_list      *app,
2414
       GError      **error)
2415
0
{
2416
0
  SourceRef source_ref;
2417
0
  AST *result;
2418
2419
0
  if (max_depth == 0)
2420
0
    {
2421
0
      token_stream_set_error (stream, error, FALSE,
2422
0
                              G_VARIANT_PARSE_ERROR_RECURSION,
2423
0
                              "variant nested too deeply");
2424
0
      return NULL;
2425
0
    }
2426
2427
0
  token_stream_prepare (stream);
2428
0
  token_stream_start_ref (stream, &source_ref);
2429
2430
0
  if (token_stream_peek (stream, '['))
2431
0
    result = array_parse (stream, max_depth, app, error);
2432
2433
0
  else if (token_stream_peek (stream, '('))
2434
0
    result = tuple_parse (stream, max_depth, app, error);
2435
2436
0
  else if (token_stream_peek (stream, '<'))
2437
0
    result = variant_parse (stream, max_depth, app, error);
2438
2439
0
  else if (token_stream_peek (stream, '{'))
2440
0
    result = dictionary_parse (stream, max_depth, app, error);
2441
2442
0
  else if (app && token_stream_peek (stream, '%'))
2443
0
    result = positional_parse (stream, app, error);
2444
2445
0
  else if (token_stream_consume (stream, "true"))
2446
0
    result = boolean_new (TRUE);
2447
2448
0
  else if (token_stream_consume (stream, "false"))
2449
0
    result = boolean_new (FALSE);
2450
2451
0
  else if (token_stream_is_numeric (stream) ||
2452
0
           token_stream_peek_string (stream, "inf") ||
2453
0
           token_stream_peek_string (stream, "nan"))
2454
0
    result = number_parse (stream, app, error);
2455
2456
0
  else if (token_stream_peek (stream, 'n') ||
2457
0
           token_stream_peek (stream, 'j'))
2458
0
    result = maybe_parse (stream, max_depth, app, error);
2459
2460
0
  else if (token_stream_peek (stream, '@') ||
2461
0
           token_stream_is_keyword (stream))
2462
0
    result = typedecl_parse (stream, max_depth, app, error);
2463
2464
0
  else if (token_stream_peek (stream, '\'') ||
2465
0
           token_stream_peek (stream, '"'))
2466
0
    result = string_parse (stream, app, error);
2467
2468
0
  else if (token_stream_peek2 (stream, 'b', '\'') ||
2469
0
           token_stream_peek2 (stream, 'b', '"'))
2470
0
    result = bytestring_parse (stream, app, error);
2471
2472
0
  else
2473
0
    {
2474
0
      token_stream_set_error (stream, error, FALSE,
2475
0
                              G_VARIANT_PARSE_ERROR_VALUE_EXPECTED,
2476
0
                              "expected value");
2477
0
      return NULL;
2478
0
    }
2479
2480
0
  if (result != NULL)
2481
0
    {
2482
0
      token_stream_end_ref (stream, &source_ref);
2483
0
      result->source_ref = source_ref;
2484
0
    }
2485
2486
0
  return result;
2487
0
}
2488
2489
/**
2490
 * g_variant_parse:
2491
 * @type: (nullable): a #GVariantType, or %NULL
2492
 * @text: a string containing a GVariant in text form
2493
 * @limit: (nullable): a pointer to the end of @text, or %NULL
2494
 * @endptr: (nullable): a location to store the end pointer, or %NULL
2495
 * @error: (nullable): a pointer to a %NULL #GError pointer, or %NULL
2496
 *
2497
 * Parses a #GVariant from a text representation.
2498
 *
2499
 * A single #GVariant is parsed from the content of @text.
2500
 *
2501
 * The format is described [here](gvariant-text-format.html).
2502
 *
2503
 * The memory at @limit will never be accessed and the parser behaves as
2504
 * if the character at @limit is the nul terminator.  This has the
2505
 * effect of bounding @text.
2506
 *
2507
 * If @endptr is non-%NULL then @text is permitted to contain data
2508
 * following the value that this function parses and @endptr will be
2509
 * updated to point to the first character past the end of the text
2510
 * parsed by this function.  If @endptr is %NULL and there is extra data
2511
 * then an error is returned.
2512
 *
2513
 * If @type is non-%NULL then the value will be parsed to have that
2514
 * type.  This may result in additional parse errors (in the case that
2515
 * the parsed value doesn't fit the type) but may also result in fewer
2516
 * errors (in the case that the type would have been ambiguous, such as
2517
 * with empty arrays).
2518
 *
2519
 * In the event that the parsing is successful, the resulting #GVariant
2520
 * is returned. It is never floating, and must be freed with
2521
 * [method@GLib.Variant.unref].
2522
 *
2523
 * In case of any error, %NULL will be returned.  If @error is non-%NULL
2524
 * then it will be set to reflect the error that occurred.
2525
 *
2526
 * Officially, the language understood by the parser is “any string
2527
 * produced by [method@GLib.Variant.print]”. This explicitly includes
2528
 * `g_variant_print()`’s annotated types like `int64 -1000`.
2529
 *
2530
 * There may be implementation specific restrictions on deeply nested values,
2531
 * which would result in a %G_VARIANT_PARSE_ERROR_RECURSION error. #GVariant is
2532
 * guaranteed to handle nesting up to at least 64 levels.
2533
 *
2534
 * Returns: a non-floating reference to a #GVariant, or %NULL
2535
 **/
2536
GVariant *
2537
g_variant_parse (const GVariantType  *type,
2538
                 const gchar         *text,
2539
                 const gchar         *limit,
2540
                 const gchar        **endptr,
2541
                 GError             **error)
2542
0
{
2543
0
  TokenStream stream = { 0, };
2544
0
  GVariant *result = NULL;
2545
0
  AST *ast;
2546
2547
0
  g_return_val_if_fail (text != NULL, NULL);
2548
0
  g_return_val_if_fail (text == limit || text != NULL, NULL);
2549
2550
0
  stream.start = text;
2551
0
  stream.stream = text;
2552
0
  stream.end = limit;
2553
2554
0
  if ((ast = parse (&stream, G_VARIANT_MAX_RECURSION_DEPTH, NULL, error)))
2555
0
    {
2556
0
      if (type == NULL)
2557
0
        result = ast_resolve (ast, error);
2558
0
      else
2559
0
        result = ast_get_value (ast, type, error);
2560
2561
0
      if (result != NULL)
2562
0
        {
2563
0
          g_variant_ref_sink (result);
2564
2565
0
          if (endptr == NULL)
2566
0
            {
2567
0
              while (stream.stream != limit &&
2568
0
                     g_ascii_isspace (*stream.stream))
2569
0
                stream.stream++;
2570
2571
0
              if (stream.stream != limit && *stream.stream != '\0')
2572
0
                {
2573
0
                  SourceRef ref = { stream.stream - text,
2574
0
                                    stream.stream - text };
2575
2576
0
                  parser_set_error (error, &ref, NULL,
2577
0
                                    G_VARIANT_PARSE_ERROR_INPUT_NOT_AT_END,
2578
0
                                    "expected end of input");
2579
0
                  g_variant_unref (result);
2580
2581
0
                  result = NULL;
2582
0
                }
2583
0
            }
2584
0
          else
2585
0
            *endptr = stream.stream;
2586
0
        }
2587
2588
0
      ast_free (ast);
2589
0
    }
2590
2591
0
  return result;
2592
0
}
2593
2594
/**
2595
 * g_variant_new_parsed_va:
2596
 * @format: a text format #GVariant
2597
 * @app: a pointer to a #va_list
2598
 *
2599
 * Parses @format and returns the result.
2600
 *
2601
 * This is the version of g_variant_new_parsed() intended to be used
2602
 * from libraries.
2603
 *
2604
 * The return value will be floating if it was a newly created GVariant
2605
 * instance.  In the case that @format simply specified the collection
2606
 * of a #GVariant pointer (eg: @format was "%*") then the collected
2607
 * #GVariant pointer will be returned unmodified, without adding any
2608
 * additional references.
2609
 *
2610
 * Note that the arguments in @app must be of the correct width for their types
2611
 * specified in @format when collected into the #va_list. See
2612
 * the [GVariant varargs documentation](gvariant-format-strings.html#varargs).
2613
 *
2614
 * In order to behave correctly in all cases it is necessary for the
2615
 * calling function to g_variant_ref_sink() the return result before
2616
 * returning control to the user that originally provided the pointer.
2617
 * At this point, the caller will have their own full reference to the
2618
 * result.  This can also be done by adding the result to a container,
2619
 * or by passing it to another g_variant_new() call.
2620
 *
2621
 * Returns: a new, usually floating, #GVariant
2622
 **/
2623
GVariant *
2624
g_variant_new_parsed_va (const gchar *format,
2625
                         va_list     *app)
2626
0
{
2627
0
  TokenStream stream = { 0, };
2628
0
  GVariant *result = NULL;
2629
0
  GError *error = NULL;
2630
0
  AST *ast;
2631
2632
0
  g_return_val_if_fail (format != NULL, NULL);
2633
0
  g_return_val_if_fail (app != NULL, NULL);
2634
2635
0
  stream.start = format;
2636
0
  stream.stream = format;
2637
0
  stream.end = NULL;
2638
2639
0
  if ((ast = parse (&stream, G_VARIANT_MAX_RECURSION_DEPTH, app, &error)))
2640
0
    {
2641
0
      result = ast_resolve (ast, &error);
2642
0
      ast_free (ast);
2643
0
    }
2644
2645
0
  if (error != NULL)
2646
0
    g_error ("g_variant_new_parsed: %s", error->message);
2647
2648
0
  if (*stream.stream)
2649
0
    g_error ("g_variant_new_parsed: trailing text after value");
2650
2651
0
  g_clear_error (&error);
2652
2653
0
  return result;
2654
0
}
2655
2656
/**
2657
 * g_variant_new_parsed:
2658
 * @format: a text format #GVariant
2659
 * @...: arguments as per @format
2660
 *
2661
 * Parses @format and returns the result.
2662
 *
2663
 * @format must be a text format #GVariant with one extension: at any
2664
 * point that a value may appear in the text, a '%' character followed
2665
 * by a GVariant format string (as per g_variant_new()) may appear.  In
2666
 * that case, the same arguments are collected from the argument list as
2667
 * g_variant_new() would have collected.
2668
 *
2669
 * Note that the arguments must be of the correct width for their types
2670
 * specified in @format. This can be achieved by casting them. See
2671
 * the [GVariant varargs documentation](gvariant-format-strings.html#varargs).
2672
 *
2673
 * Consider this simple example:
2674
 * |[<!-- language="C" --> 
2675
 *  g_variant_new_parsed ("[('one', 1), ('two', %i), (%s, 3)]", 2, "three");
2676
 * ]|
2677
 *
2678
 * In the example, the variable argument parameters are collected and
2679
 * filled in as if they were part of the original string to produce the
2680
 * result of
2681
 * |[<!-- language="C" --> 
2682
 * [('one', 1), ('two', 2), ('three', 3)]
2683
 * ]|
2684
 *
2685
 * This function is intended only to be used with @format as a string
2686
 * literal.  Any parse error is fatal to the calling process.  If you
2687
 * want to parse data from untrusted sources, use g_variant_parse().
2688
 *
2689
 * You may not use this function to return, unmodified, a single
2690
 * #GVariant pointer from the argument list.  ie: @format may not solely
2691
 * be anything along the lines of "%*", "%?", "\%r", or anything starting
2692
 * with "%@".
2693
 *
2694
 * Returns: a new floating #GVariant instance
2695
 **/
2696
GVariant *
2697
g_variant_new_parsed (const gchar *format,
2698
                      ...)
2699
0
{
2700
0
  GVariant *result;
2701
0
  va_list ap;
2702
2703
0
  va_start (ap, format);
2704
0
  result = g_variant_new_parsed_va (format, &ap);
2705
0
  va_end (ap);
2706
2707
0
  return result;
2708
0
}
2709
2710
/**
2711
 * g_variant_builder_add_parsed:
2712
 * @builder: a #GVariantBuilder
2713
 * @format: a text format #GVariant
2714
 * @...: arguments as per @format
2715
 *
2716
 * Adds to a #GVariantBuilder.
2717
 *
2718
 * This call is a convenience wrapper that is exactly equivalent to
2719
 * calling g_variant_new_parsed() followed by
2720
 * g_variant_builder_add_value().
2721
 *
2722
 * Note that the arguments must be of the correct width for their types
2723
 * specified in @format_string. This can be achieved by casting them. See
2724
 * the [GVariant varargs documentation](gvariant-format-strings.html#varargs).
2725
 *
2726
 * This function might be used as follows:
2727
 *
2728
 * |[<!-- language="C" --> 
2729
 * GVariant *
2730
 * make_pointless_dictionary (void)
2731
 * {
2732
 *   GVariantBuilder builder;
2733
 *   int i;
2734
 *
2735
 *   g_variant_builder_init_static (&builder, G_VARIANT_TYPE_ARRAY);
2736
 *   g_variant_builder_add_parsed (&builder, "{'width', <%i>}", 600);
2737
 *   g_variant_builder_add_parsed (&builder, "{'title', <%s>}", "foo");
2738
 *   g_variant_builder_add_parsed (&builder, "{'transparency', <0.5>}");
2739
 *   return g_variant_builder_end (&builder);
2740
 * }
2741
 * ]|
2742
 *
2743
 * Since: 2.26
2744
 */
2745
void
2746
g_variant_builder_add_parsed (GVariantBuilder *builder,
2747
                              const gchar     *format,
2748
                              ...)
2749
0
{
2750
0
  va_list ap;
2751
2752
0
  va_start (ap, format);
2753
0
  g_variant_builder_add_value (builder, g_variant_new_parsed_va (format, &ap));
2754
0
  va_end (ap);
2755
0
}
2756
2757
static gboolean
2758
parse_num (const gchar *num,
2759
           const gchar *limit,
2760
           guint       *result)
2761
0
{
2762
0
  gchar *endptr;
2763
0
  gint64 bignum;
2764
2765
0
  bignum = g_ascii_strtoll (num, &endptr, 10);
2766
2767
0
  if (endptr != limit)
2768
0
    return FALSE;
2769
2770
0
  if (bignum < 0 || bignum > G_MAXINT)
2771
0
    return FALSE;
2772
2773
0
  *result = (guint) bignum;
2774
2775
0
  return TRUE;
2776
0
}
2777
2778
static void
2779
add_last_line (GString     *err,
2780
               const gchar *str)
2781
0
{
2782
0
  const gchar *last_nl;
2783
0
  gchar *chomped;
2784
0
  gint i;
2785
2786
  /* This is an error at the end of input.  If we have a file
2787
   * with newlines, that's probably the empty string after the
2788
   * last newline, which is not the most useful thing to show.
2789
   *
2790
   * Instead, show the last line of non-whitespace that we have
2791
   * and put the pointer at the end of it.
2792
   */
2793
0
  chomped = g_strchomp (g_strdup (str));
2794
0
  last_nl = strrchr (chomped, '\n');
2795
0
  if (last_nl == NULL)
2796
0
    last_nl = chomped;
2797
0
  else
2798
0
    last_nl++;
2799
2800
  /* Print the last line like so:
2801
   *
2802
   *   [1, 2, 3,
2803
   *            ^
2804
   */
2805
0
  g_string_append (err, "  ");
2806
0
  if (last_nl[0])
2807
0
    g_string_append (err, last_nl);
2808
0
  else
2809
0
    g_string_append (err, "(empty input)");
2810
0
  g_string_append (err, "\n  ");
2811
0
  for (i = 0; last_nl[i]; i++)
2812
0
    g_string_append_c (err, ' ');
2813
0
  g_string_append (err, "^\n");
2814
0
  g_free (chomped);
2815
0
}
2816
2817
static void
2818
add_lines_from_range (GString     *err,
2819
                      const gchar *str,
2820
                      const gchar *start1,
2821
                      const gchar *end1,
2822
                      const gchar *start2,
2823
                      const gchar *end2)
2824
0
{
2825
0
  while (str < end1 || str < end2)
2826
0
    {
2827
0
      const gchar *nl;
2828
2829
0
      nl = str + strcspn (str, "\n");
2830
2831
0
      if ((start1 < nl && str < end1) || (start2 < nl && str < end2))
2832
0
        {
2833
0
          const gchar *s;
2834
2835
          /* We're going to print this line */
2836
0
          g_string_append (err, "  ");
2837
0
          g_string_append_len (err, str, nl - str);
2838
0
          g_string_append (err, "\n  ");
2839
2840
          /* And add underlines... */
2841
0
          for (s = str; s < nl; s++)
2842
0
            {
2843
0
              if ((start1 <= s && s < end1) || (start2 <= s && s < end2))
2844
0
                g_string_append_c (err, '^');
2845
0
              else
2846
0
                g_string_append_c (err, ' ');
2847
0
            }
2848
0
          g_string_append_c (err, '\n');
2849
0
        }
2850
2851
0
      if (!*nl)
2852
0
        break;
2853
2854
0
      str = nl + 1;
2855
0
    }
2856
0
}
2857
2858
/**
2859
 * g_variant_parse_error_print_context:
2860
 * @error: a #GError from the #GVariantParseError domain
2861
 * @source_str: the string that was given to the parser
2862
 *
2863
 * Pretty-prints a message showing the context of a #GVariant parse
2864
 * error within the string for which parsing was attempted.
2865
 *
2866
 * The resulting string is suitable for output to the console or other
2867
 * monospace media where newlines are treated in the usual way.
2868
 *
2869
 * The message will typically look something like one of the following:
2870
 *
2871
 * |[
2872
 * unterminated string constant:
2873
 *   (1, 2, 3, 'abc
2874
 *             ^^^^
2875
 * ]|
2876
 *
2877
 * or
2878
 *
2879
 * |[
2880
 * unable to find a common type:
2881
 *   [1, 2, 3, 'str']
2882
 *    ^        ^^^^^
2883
 * ]|
2884
 *
2885
 * The format of the message may change in a future version.
2886
 *
2887
 * @error must have come from a failed attempt to g_variant_parse() and
2888
 * @source_str must be exactly the same string that caused the error.
2889
 * If @source_str was not nul-terminated when you passed it to
2890
 * g_variant_parse() then you must add nul termination before using this
2891
 * function.
2892
 *
2893
 * Returns: (transfer full): the printed message
2894
 *
2895
 * Since: 2.40
2896
 **/
2897
gchar *
2898
g_variant_parse_error_print_context (GError      *error,
2899
                                     const gchar *source_str)
2900
0
{
2901
0
  const gchar *colon, *dash, *comma;
2902
0
  gboolean success = FALSE;
2903
0
  GString *err;
2904
2905
0
  g_return_val_if_fail (error->domain == G_VARIANT_PARSE_ERROR, FALSE);
2906
2907
  /* We can only have a limited number of possible types of ranges
2908
   * emitted from the parser:
2909
   *
2910
   *  - a:          -- usually errors from the tokeniser (eof, invalid char, etc.)
2911
   *  - a-b:        -- usually errors from handling one single token
2912
   *  - a-b,c-d:    -- errors involving two tokens (ie: type inferencing)
2913
   *
2914
   * We never see, for example "a,c".
2915
   */
2916
2917
0
  colon = strchr (error->message, ':');
2918
0
  dash = strchr (error->message, '-');
2919
0
  comma = strchr (error->message, ',');
2920
2921
0
  if (!colon)
2922
0
    return NULL;
2923
2924
0
  err = g_string_new (colon + 1);
2925
0
  g_string_append (err, ":\n");
2926
2927
0
  if (dash == NULL || colon < dash)
2928
0
    {
2929
0
      guint point;
2930
2931
      /* we have a single point */
2932
0
      if (!parse_num (error->message, colon, &point))
2933
0
        goto out;
2934
2935
0
      if (point >= strlen (source_str))
2936
        /* the error is at the end of the input */
2937
0
        add_last_line (err, source_str);
2938
0
      else
2939
        /* otherwise just treat it as an error at a thin range */
2940
0
        add_lines_from_range (err, source_str, source_str + point, source_str + point + 1, NULL, NULL);
2941
0
    }
2942
0
  else
2943
0
    {
2944
      /* We have one or two ranges... */
2945
0
      if (comma && comma < colon)
2946
0
        {
2947
0
          guint start1, end1, start2, end2;
2948
0
          const gchar *dash2;
2949
2950
          /* Two ranges */
2951
0
          dash2 = strchr (comma, '-');
2952
2953
0
          if (!parse_num (error->message, dash, &start1) || !parse_num (dash + 1, comma, &end1) ||
2954
0
              !parse_num (comma + 1, dash2, &start2) || !parse_num (dash2 + 1, colon, &end2))
2955
0
            goto out;
2956
2957
0
          add_lines_from_range (err, source_str,
2958
0
                                source_str + start1, source_str + end1,
2959
0
                                source_str + start2, source_str + end2);
2960
0
        }
2961
0
      else
2962
0
        {
2963
0
          guint start, end;
2964
2965
          /* One range */
2966
0
          if (!parse_num (error->message, dash, &start) || !parse_num (dash + 1, colon, &end))
2967
0
            goto out;
2968
2969
0
          add_lines_from_range (err, source_str, source_str + start, source_str + end, NULL, NULL);
2970
0
        }
2971
0
    }
2972
2973
0
  success = TRUE;
2974
2975
0
out:
2976
0
  return g_string_free (err, !success);
2977
0
}