Coverage Report

Created: 2026-12-01 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libyaml/src/scanner.c
Line
Count
Source
1
2
/*
3
 * Introduction
4
 * ************
5
 *
6
 * The following notes assume that you are familiar with the YAML specification
7
 * (http://yaml.org/spec/cvs/current.html).  We mostly follow it, although in
8
 * some cases we are less restrictive that it requires.
9
 *
10
 * The process of transforming a YAML stream into a sequence of events is
11
 * divided on two steps: Scanning and Parsing.
12
 *
13
 * The Scanner transforms the input stream into a sequence of tokens, while the
14
 * parser transform the sequence of tokens produced by the Scanner into a
15
 * sequence of parsing events.
16
 *
17
 * The Scanner is rather clever and complicated. The Parser, on the contrary,
18
 * is a straightforward implementation of a recursive-descendant parser (or,
19
 * LL(1) parser, as it is usually called).
20
 *
21
 * Actually there are two issues of Scanning that might be called "clever", the
22
 * rest is quite straightforward.  The issues are "block collection start" and
23
 * "simple keys".  Both issues are explained below in details.
24
 *
25
 * Here the Scanning step is explained and implemented.  We start with the list
26
 * of all the tokens produced by the Scanner together with short descriptions.
27
 *
28
 * Now, tokens:
29
 *
30
 *      STREAM-START(encoding)          # The stream start.
31
 *      STREAM-END                      # The stream end.
32
 *      VERSION-DIRECTIVE(major,minor)  # The '%YAML' directive.
33
 *      TAG-DIRECTIVE(handle,prefix)    # The '%TAG' directive.
34
 *      DOCUMENT-START                  # '---'
35
 *      DOCUMENT-END                    # '...'
36
 *      BLOCK-SEQUENCE-START            # Indentation increase denoting a block
37
 *      BLOCK-MAPPING-START             # sequence or a block mapping.
38
 *      BLOCK-END                       # Indentation decrease.
39
 *      FLOW-SEQUENCE-START             # '['
40
 *      FLOW-SEQUENCE-END               # ']'
41
 *      FLOW-MAPPING-START              # '{'
42
 *      FLOW-MAPPING-END                # '}'
43
 *      BLOCK-ENTRY                     # '-'
44
 *      FLOW-ENTRY                      # ','
45
 *      KEY                             # '?' or nothing (simple keys).
46
 *      VALUE                           # ':'
47
 *      ALIAS(anchor)                   # '*anchor'
48
 *      ANCHOR(anchor)                  # '&anchor'
49
 *      TAG(handle,suffix)              # '!handle!suffix'
50
 *      SCALAR(value,style)             # A scalar.
51
 *
52
 * The following two tokens are "virtual" tokens denoting the beginning and the
53
 * end of the stream:
54
 *
55
 *      STREAM-START(encoding)
56
 *      STREAM-END
57
 *
58
 * We pass the information about the input stream encoding with the
59
 * STREAM-START token.
60
 *
61
 * The next two tokens are responsible for tags:
62
 *
63
 *      VERSION-DIRECTIVE(major,minor)
64
 *      TAG-DIRECTIVE(handle,prefix)
65
 *
66
 * Example:
67
 *
68
 *      %YAML   1.1
69
 *      %TAG    !   !foo
70
 *      %TAG    !yaml!  tag:yaml.org,2002:
71
 *      ---
72
 *
73
 * The corresponding sequence of tokens:
74
 *
75
 *      STREAM-START(utf-8)
76
 *      VERSION-DIRECTIVE(1,1)
77
 *      TAG-DIRECTIVE("!","!foo")
78
 *      TAG-DIRECTIVE("!yaml","tag:yaml.org,2002:")
79
 *      DOCUMENT-START
80
 *      STREAM-END
81
 *
82
 * Note that the VERSION-DIRECTIVE and TAG-DIRECTIVE tokens occupy a whole
83
 * line.
84
 *
85
 * The document start and end indicators are represented by:
86
 *
87
 *      DOCUMENT-START
88
 *      DOCUMENT-END
89
 *
90
 * Note that if a YAML stream contains an implicit document (without '---'
91
 * and '...' indicators), no DOCUMENT-START and DOCUMENT-END tokens will be
92
 * produced.
93
 *
94
 * In the following examples, we present whole documents together with the
95
 * produced tokens.
96
 *
97
 *      1. An implicit document:
98
 *
99
 *          'a scalar'
100
 *
101
 *      Tokens:
102
 *
103
 *          STREAM-START(utf-8)
104
 *          SCALAR("a scalar",single-quoted)
105
 *          STREAM-END
106
 *
107
 *      2. An explicit document:
108
 *
109
 *          ---
110
 *          'a scalar'
111
 *          ...
112
 *
113
 *      Tokens:
114
 *
115
 *          STREAM-START(utf-8)
116
 *          DOCUMENT-START
117
 *          SCALAR("a scalar",single-quoted)
118
 *          DOCUMENT-END
119
 *          STREAM-END
120
 *
121
 *      3. Several documents in a stream:
122
 *
123
 *          'a scalar'
124
 *          ---
125
 *          'another scalar'
126
 *          ---
127
 *          'yet another scalar'
128
 *
129
 *      Tokens:
130
 *
131
 *          STREAM-START(utf-8)
132
 *          SCALAR("a scalar",single-quoted)
133
 *          DOCUMENT-START
134
 *          SCALAR("another scalar",single-quoted)
135
 *          DOCUMENT-START
136
 *          SCALAR("yet another scalar",single-quoted)
137
 *          STREAM-END
138
 *
139
 * We have already introduced the SCALAR token above.  The following tokens are
140
 * used to describe aliases, anchors, tag, and scalars:
141
 *
142
 *      ALIAS(anchor)
143
 *      ANCHOR(anchor)
144
 *      TAG(handle,suffix)
145
 *      SCALAR(value,style)
146
 *
147
 * The following series of examples illustrate the usage of these tokens:
148
 *
149
 *      1. A recursive sequence:
150
 *
151
 *          &A [ *A ]
152
 *
153
 *      Tokens:
154
 *
155
 *          STREAM-START(utf-8)
156
 *          ANCHOR("A")
157
 *          FLOW-SEQUENCE-START
158
 *          ALIAS("A")
159
 *          FLOW-SEQUENCE-END
160
 *          STREAM-END
161
 *
162
 *      2. A tagged scalar:
163
 *
164
 *          !!float "3.14"  # A good approximation.
165
 *
166
 *      Tokens:
167
 *
168
 *          STREAM-START(utf-8)
169
 *          TAG("!!","float")
170
 *          SCALAR("3.14",double-quoted)
171
 *          STREAM-END
172
 *
173
 *      3. Various scalar styles:
174
 *
175
 *          --- # Implicit empty plain scalars do not produce tokens.
176
 *          --- a plain scalar
177
 *          --- 'a single-quoted scalar'
178
 *          --- "a double-quoted scalar"
179
 *          --- |-
180
 *            a literal scalar
181
 *          --- >-
182
 *            a folded
183
 *            scalar
184
 *
185
 *      Tokens:
186
 *
187
 *          STREAM-START(utf-8)
188
 *          DOCUMENT-START
189
 *          DOCUMENT-START
190
 *          SCALAR("a plain scalar",plain)
191
 *          DOCUMENT-START
192
 *          SCALAR("a single-quoted scalar",single-quoted)
193
 *          DOCUMENT-START
194
 *          SCALAR("a double-quoted scalar",double-quoted)
195
 *          DOCUMENT-START
196
 *          SCALAR("a literal scalar",literal)
197
 *          DOCUMENT-START
198
 *          SCALAR("a folded scalar",folded)
199
 *          STREAM-END
200
 *
201
 * Now it's time to review collection-related tokens. We will start with
202
 * flow collections:
203
 *
204
 *      FLOW-SEQUENCE-START
205
 *      FLOW-SEQUENCE-END
206
 *      FLOW-MAPPING-START
207
 *      FLOW-MAPPING-END
208
 *      FLOW-ENTRY
209
 *      KEY
210
 *      VALUE
211
 *
212
 * The tokens FLOW-SEQUENCE-START, FLOW-SEQUENCE-END, FLOW-MAPPING-START, and
213
 * FLOW-MAPPING-END represent the indicators '[', ']', '{', and '}'
214
 * correspondingly.  FLOW-ENTRY represent the ',' indicator.  Finally the
215
 * indicators '?' and ':', which are used for denoting mapping keys and values,
216
 * are represented by the KEY and VALUE tokens.
217
 *
218
 * The following examples show flow collections:
219
 *
220
 *      1. A flow sequence:
221
 *
222
 *          [item 1, item 2, item 3]
223
 *
224
 *      Tokens:
225
 *
226
 *          STREAM-START(utf-8)
227
 *          FLOW-SEQUENCE-START
228
 *          SCALAR("item 1",plain)
229
 *          FLOW-ENTRY
230
 *          SCALAR("item 2",plain)
231
 *          FLOW-ENTRY
232
 *          SCALAR("item 3",plain)
233
 *          FLOW-SEQUENCE-END
234
 *          STREAM-END
235
 *
236
 *      2. A flow mapping:
237
 *
238
 *          {
239
 *              a simple key: a value,  # Note that the KEY token is produced.
240
 *              ? a complex key: another value,
241
 *          }
242
 *
243
 *      Tokens:
244
 *
245
 *          STREAM-START(utf-8)
246
 *          FLOW-MAPPING-START
247
 *          KEY
248
 *          SCALAR("a simple key",plain)
249
 *          VALUE
250
 *          SCALAR("a value",plain)
251
 *          FLOW-ENTRY
252
 *          KEY
253
 *          SCALAR("a complex key",plain)
254
 *          VALUE
255
 *          SCALAR("another value",plain)
256
 *          FLOW-ENTRY
257
 *          FLOW-MAPPING-END
258
 *          STREAM-END
259
 *
260
 * A simple key is a key which is not denoted by the '?' indicator.  Note that
261
 * the Scanner still produce the KEY token whenever it encounters a simple key.
262
 *
263
 * For scanning block collections, the following tokens are used (note that we
264
 * repeat KEY and VALUE here):
265
 *
266
 *      BLOCK-SEQUENCE-START
267
 *      BLOCK-MAPPING-START
268
 *      BLOCK-END
269
 *      BLOCK-ENTRY
270
 *      KEY
271
 *      VALUE
272
 *
273
 * The tokens BLOCK-SEQUENCE-START and BLOCK-MAPPING-START denote indentation
274
 * increase that precedes a block collection (cf. the INDENT token in Python).
275
 * The token BLOCK-END denote indentation decrease that ends a block collection
276
 * (cf. the DEDENT token in Python).  However YAML has some syntax peculiarities
277
 * that makes detections of these tokens more complex.
278
 *
279
 * The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators
280
 * '-', '?', and ':' correspondingly.
281
 *
282
 * The following examples show how the tokens BLOCK-SEQUENCE-START,
283
 * BLOCK-MAPPING-START, and BLOCK-END are emitted by the Scanner:
284
 *
285
 *      1. Block sequences:
286
 *
287
 *          - item 1
288
 *          - item 2
289
 *          -
290
 *            - item 3.1
291
 *            - item 3.2
292
 *          -
293
 *            key 1: value 1
294
 *            key 2: value 2
295
 *
296
 *      Tokens:
297
 *
298
 *          STREAM-START(utf-8)
299
 *          BLOCK-SEQUENCE-START
300
 *          BLOCK-ENTRY
301
 *          SCALAR("item 1",plain)
302
 *          BLOCK-ENTRY
303
 *          SCALAR("item 2",plain)
304
 *          BLOCK-ENTRY
305
 *          BLOCK-SEQUENCE-START
306
 *          BLOCK-ENTRY
307
 *          SCALAR("item 3.1",plain)
308
 *          BLOCK-ENTRY
309
 *          SCALAR("item 3.2",plain)
310
 *          BLOCK-END
311
 *          BLOCK-ENTRY
312
 *          BLOCK-MAPPING-START
313
 *          KEY
314
 *          SCALAR("key 1",plain)
315
 *          VALUE
316
 *          SCALAR("value 1",plain)
317
 *          KEY
318
 *          SCALAR("key 2",plain)
319
 *          VALUE
320
 *          SCALAR("value 2",plain)
321
 *          BLOCK-END
322
 *          BLOCK-END
323
 *          STREAM-END
324
 *
325
 *      2. Block mappings:
326
 *
327
 *          a simple key: a value   # The KEY token is produced here.
328
 *          ? a complex key
329
 *          : another value
330
 *          a mapping:
331
 *            key 1: value 1
332
 *            key 2: value 2
333
 *          a sequence:
334
 *            - item 1
335
 *            - item 2
336
 *
337
 *      Tokens:
338
 *
339
 *          STREAM-START(utf-8)
340
 *          BLOCK-MAPPING-START
341
 *          KEY
342
 *          SCALAR("a simple key",plain)
343
 *          VALUE
344
 *          SCALAR("a value",plain)
345
 *          KEY
346
 *          SCALAR("a complex key",plain)
347
 *          VALUE
348
 *          SCALAR("another value",plain)
349
 *          KEY
350
 *          SCALAR("a mapping",plain)
351
 *          VALUE
352
 *          BLOCK-MAPPING-START
353
 *          KEY
354
 *          SCALAR("key 1",plain)
355
 *          VALUE
356
 *          SCALAR("value 1",plain)
357
 *          KEY
358
 *          SCALAR("key 2",plain)
359
 *          VALUE
360
 *          SCALAR("value 2",plain)
361
 *          BLOCK-END
362
 *          KEY
363
 *          SCALAR("a sequence",plain)
364
 *          VALUE
365
 *          BLOCK-SEQUENCE-START
366
 *          BLOCK-ENTRY
367
 *          SCALAR("item 1",plain)
368
 *          BLOCK-ENTRY
369
 *          SCALAR("item 2",plain)
370
 *          BLOCK-END
371
 *          BLOCK-END
372
 *          STREAM-END
373
 *
374
 * YAML does not always require to start a new block collection from a new
375
 * line.  If the current line contains only '-', '?', and ':' indicators, a new
376
 * block collection may start at the current line.  The following examples
377
 * illustrate this case:
378
 *
379
 *      1. Collections in a sequence:
380
 *
381
 *          - - item 1
382
 *            - item 2
383
 *          - key 1: value 1
384
 *            key 2: value 2
385
 *          - ? complex key
386
 *            : complex value
387
 *
388
 *      Tokens:
389
 *
390
 *          STREAM-START(utf-8)
391
 *          BLOCK-SEQUENCE-START
392
 *          BLOCK-ENTRY
393
 *          BLOCK-SEQUENCE-START
394
 *          BLOCK-ENTRY
395
 *          SCALAR("item 1",plain)
396
 *          BLOCK-ENTRY
397
 *          SCALAR("item 2",plain)
398
 *          BLOCK-END
399
 *          BLOCK-ENTRY
400
 *          BLOCK-MAPPING-START
401
 *          KEY
402
 *          SCALAR("key 1",plain)
403
 *          VALUE
404
 *          SCALAR("value 1",plain)
405
 *          KEY
406
 *          SCALAR("key 2",plain)
407
 *          VALUE
408
 *          SCALAR("value 2",plain)
409
 *          BLOCK-END
410
 *          BLOCK-ENTRY
411
 *          BLOCK-MAPPING-START
412
 *          KEY
413
 *          SCALAR("complex key")
414
 *          VALUE
415
 *          SCALAR("complex value")
416
 *          BLOCK-END
417
 *          BLOCK-END
418
 *          STREAM-END
419
 *
420
 *      2. Collections in a mapping:
421
 *
422
 *          ? a sequence
423
 *          : - item 1
424
 *            - item 2
425
 *          ? a mapping
426
 *          : key 1: value 1
427
 *            key 2: value 2
428
 *
429
 *      Tokens:
430
 *
431
 *          STREAM-START(utf-8)
432
 *          BLOCK-MAPPING-START
433
 *          KEY
434
 *          SCALAR("a sequence",plain)
435
 *          VALUE
436
 *          BLOCK-SEQUENCE-START
437
 *          BLOCK-ENTRY
438
 *          SCALAR("item 1",plain)
439
 *          BLOCK-ENTRY
440
 *          SCALAR("item 2",plain)
441
 *          BLOCK-END
442
 *          KEY
443
 *          SCALAR("a mapping",plain)
444
 *          VALUE
445
 *          BLOCK-MAPPING-START
446
 *          KEY
447
 *          SCALAR("key 1",plain)
448
 *          VALUE
449
 *          SCALAR("value 1",plain)
450
 *          KEY
451
 *          SCALAR("key 2",plain)
452
 *          VALUE
453
 *          SCALAR("value 2",plain)
454
 *          BLOCK-END
455
 *          BLOCK-END
456
 *          STREAM-END
457
 *
458
 * YAML also permits non-indented sequences if they are included into a block
459
 * mapping.  In this case, the token BLOCK-SEQUENCE-START is not produced:
460
 *
461
 *      key:
462
 *      - item 1    # BLOCK-SEQUENCE-START is NOT produced here.
463
 *      - item 2
464
 *
465
 * Tokens:
466
 *
467
 *      STREAM-START(utf-8)
468
 *      BLOCK-MAPPING-START
469
 *      KEY
470
 *      SCALAR("key",plain)
471
 *      VALUE
472
 *      BLOCK-ENTRY
473
 *      SCALAR("item 1",plain)
474
 *      BLOCK-ENTRY
475
 *      SCALAR("item 2",plain)
476
 *      BLOCK-END
477
 */
478
479
#include "yaml_private.h"
480
481
/*
482
 * Maximum nesting level (defined in parser.c).
483
 */
484
485
extern int MAX_NESTING_LEVEL;
486
487
/*
488
 * Ensure that the buffer contains the required number of characters.
489
 * Return 1 on success, 0 on failure (reader error or memory error).
490
 */
491
492
#define CACHE(parser,length)                                                    \
493
3.42G
    (parser->unread >= (length)                                                 \
494
3.42G
        ? 1                                                                     \
495
3.42G
        : yaml_parser_update_buffer(parser, (length)))
496
497
/*
498
 * Advance the buffer pointer.
499
 */
500
501
#define SKIP(parser)                                                            \
502
1.85G
     (parser->mark.index ++,                                                    \
503
1.85G
      parser->mark.column ++,                                                   \
504
1.85G
      parser->unread --,                                                        \
505
1.85G
      parser->buffer.pointer += WIDTH(parser->buffer))
506
507
#define SKIP_LINE(parser)                                                       \
508
47.7M
     (IS_CRLF(parser->buffer) ?                                                 \
509
47.7M
      (parser->mark.index += 2,                                                 \
510
6.41M
       parser->mark.column = 0,                                                 \
511
6.41M
       parser->mark.line ++,                                                    \
512
6.41M
       parser->unread -= 2,                                                     \
513
6.41M
       parser->buffer.pointer += 2) :                                           \
514
47.7M
      IS_BREAK(parser->buffer) ?                                                \
515
41.3M
      (parser->mark.index ++,                                                   \
516
41.3M
       parser->mark.column = 0,                                                 \
517
41.3M
       parser->mark.line ++,                                                    \
518
41.3M
       parser->unread --,                                                       \
519
41.3M
       parser->buffer.pointer += WIDTH(parser->buffer)) : 0)
520
521
/*
522
 * Copy a character to a string buffer and advance pointers.
523
 */
524
525
#define READ(parser,string)                                                     \
526
891M
     (STRING_EXTEND(parser,string) ?                                            \
527
891M
         (COPY(string,parser->buffer),                                          \
528
891M
          parser->mark.index ++,                                                \
529
891M
          parser->mark.column ++,                                               \
530
891M
          parser->unread --,                                                    \
531
891M
          1) : 0)
532
533
/*
534
 * Copy a line break character to a string buffer and advance pointers.
535
 */
536
537
#define READ_LINE(parser,string)                                                \
538
76.0M
    (STRING_EXTEND(parser,string) ?                                             \
539
76.0M
    (((CHECK_AT(parser->buffer,'\r',0)                                          \
540
76.0M
       && CHECK_AT(parser->buffer,'\n',1)) ?        /* CR LF -> LF */           \
541
76.0M
     (*((string).pointer++) = (yaml_char_t) '\n',                               \
542
5.67M
      parser->buffer.pointer += 2,                                              \
543
5.67M
      parser->mark.index += 2,                                                  \
544
5.67M
      parser->mark.column = 0,                                                  \
545
5.67M
      parser->mark.line ++,                                                     \
546
5.67M
      parser->unread -= 2) :                                                    \
547
76.0M
     (CHECK_AT(parser->buffer,'\r',0)                                           \
548
70.3M
      || CHECK_AT(parser->buffer,'\n',0)) ?         /* CR|LF -> LF */           \
549
70.3M
     (*((string).pointer++) = (yaml_char_t) '\n',                               \
550
68.5M
      parser->buffer.pointer ++,                                                \
551
68.5M
      parser->mark.index ++,                                                    \
552
68.5M
      parser->mark.column = 0,                                                  \
553
68.5M
      parser->mark.line ++,                                                     \
554
68.5M
      parser->unread --) :                                                      \
555
70.3M
     (CHECK_AT(parser->buffer,'\xC2',0)                                         \
556
1.78M
      && CHECK_AT(parser->buffer,'\x85',1)) ?       /* NEL -> LF */             \
557
1.78M
     (*((string).pointer++) = (yaml_char_t) '\n',                               \
558
33.6k
      parser->buffer.pointer += 2,                                              \
559
33.6k
      parser->mark.index ++,                                                    \
560
33.6k
      parser->mark.column = 0,                                                  \
561
33.6k
      parser->mark.line ++,                                                     \
562
33.6k
      parser->unread --) :                                                      \
563
1.78M
     (CHECK_AT(parser->buffer,'\xE2',0) &&                                      \
564
1.75M
      CHECK_AT(parser->buffer,'\x80',1) &&                                      \
565
1.75M
      (CHECK_AT(parser->buffer,'\xA8',2) ||                                     \
566
1.74M
       CHECK_AT(parser->buffer,'\xA9',2))) ?        /* LS|PS -> LS|PS */        \
567
1.75M
     (*((string).pointer++) = *(parser->buffer.pointer++),                      \
568
1.74M
      *((string).pointer++) = *(parser->buffer.pointer++),                      \
569
1.74M
      *((string).pointer++) = *(parser->buffer.pointer++),                      \
570
1.74M
      parser->mark.index ++,                                                    \
571
1.74M
      parser->mark.column = 0,                                                  \
572
1.74M
      parser->mark.line ++,                                                     \
573
1.74M
      parser->unread --) : 0),                                                  \
574
76.0M
    1) : 0)
575
576
/*
577
 * Public API declarations.
578
 */
579
580
YAML_DECLARE(int)
581
yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token);
582
583
/*
584
 * Error handling.
585
 */
586
587
static int
588
yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
589
        yaml_mark_t context_mark, const char *problem);
590
591
/*
592
 * High-level token API.
593
 */
594
595
YAML_DECLARE(int)
596
yaml_parser_fetch_more_tokens(yaml_parser_t *parser);
597
598
static int
599
yaml_parser_fetch_next_token(yaml_parser_t *parser);
600
601
/*
602
 * Potential simple keys.
603
 */
604
605
static int
606
yaml_parser_stale_simple_keys(yaml_parser_t *parser);
607
608
static int
609
yaml_parser_save_simple_key(yaml_parser_t *parser);
610
611
static int
612
yaml_parser_remove_simple_key(yaml_parser_t *parser);
613
614
static int
615
yaml_parser_increase_flow_level(yaml_parser_t *parser);
616
617
static int
618
yaml_parser_decrease_flow_level(yaml_parser_t *parser);
619
620
/*
621
 * Indentation treatment.
622
 */
623
624
static int
625
yaml_parser_roll_indent(yaml_parser_t *parser, ptrdiff_t column,
626
        ptrdiff_t number, yaml_token_type_t type, yaml_mark_t mark);
627
628
static int
629
yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column);
630
631
/*
632
 * Token fetchers.
633
 */
634
635
static int
636
yaml_parser_fetch_stream_start(yaml_parser_t *parser);
637
638
static int
639
yaml_parser_fetch_stream_end(yaml_parser_t *parser);
640
641
static int
642
yaml_parser_fetch_directive(yaml_parser_t *parser);
643
644
static int
645
yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
646
        yaml_token_type_t type);
647
648
static int
649
yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
650
        yaml_token_type_t type);
651
652
static int
653
yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
654
        yaml_token_type_t type);
655
656
static int
657
yaml_parser_fetch_flow_entry(yaml_parser_t *parser);
658
659
static int
660
yaml_parser_fetch_block_entry(yaml_parser_t *parser);
661
662
static int
663
yaml_parser_fetch_key(yaml_parser_t *parser);
664
665
static int
666
yaml_parser_fetch_value(yaml_parser_t *parser);
667
668
static int
669
yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type);
670
671
static int
672
yaml_parser_fetch_tag(yaml_parser_t *parser);
673
674
static int
675
yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal);
676
677
static int
678
yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single);
679
680
static int
681
yaml_parser_fetch_plain_scalar(yaml_parser_t *parser);
682
683
/*
684
 * Token scanners.
685
 */
686
687
static int
688
yaml_parser_scan_to_next_token(yaml_parser_t *parser);
689
690
static int
691
yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token);
692
693
static int
694
yaml_parser_scan_directive_name(yaml_parser_t *parser,
695
        yaml_mark_t start_mark, yaml_char_t **name);
696
697
static int
698
yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
699
        yaml_mark_t start_mark, int *major, int *minor);
700
701
static int
702
yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
703
        yaml_mark_t start_mark, int *number);
704
705
static int
706
yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
707
        yaml_mark_t mark, yaml_char_t **handle, yaml_char_t **prefix);
708
709
static int
710
yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
711
        yaml_token_type_t type);
712
713
static int
714
yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token);
715
716
static int
717
yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
718
        yaml_mark_t start_mark, yaml_char_t **handle);
719
720
static int
721
yaml_parser_scan_tag_uri(yaml_parser_t *parser, int uri_char, int directive,
722
        yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri);
723
724
static int
725
yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
726
        yaml_mark_t start_mark, yaml_string_t *string);
727
728
static int
729
yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
730
        int literal);
731
732
static int
733
yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
734
        int *indent, yaml_string_t *breaks,
735
        yaml_mark_t start_mark, yaml_mark_t *end_mark);
736
737
static int
738
yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
739
        int single);
740
741
static int
742
yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token);
743
744
/*
745
 * Get the next token.
746
 */
747
748
YAML_DECLARE(int)
749
yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token)
750
10.6M
{
751
10.6M
    assert(parser); /* Non-NULL parser object is expected. */
752
10.6M
    assert(token);  /* Non-NULL token object is expected. */
753
754
    /* Erase the token object. */
755
756
10.6M
    memset(token, 0, sizeof(yaml_token_t));
757
758
    /* No tokens after STREAM-END or error. */
759
760
10.6M
    if (parser->stream_end_produced || parser->error) {
761
0
        return 1;
762
0
    }
763
764
    /* Ensure that the tokens queue contains enough tokens. */
765
766
10.6M
    if (!parser->token_available) {
767
10.6M
        if (!yaml_parser_fetch_more_tokens(parser))
768
2.62k
            return 0;
769
10.6M
    }
770
771
    /* Fetch the next token from the queue. */
772
773
10.5M
    *token = DEQUEUE(parser, parser->tokens);
774
10.5M
    parser->token_available = 0;
775
10.5M
    parser->tokens_parsed ++;
776
777
10.5M
    if (token->type == YAML_STREAM_END_TOKEN) {
778
4.35k
        parser->stream_end_produced = 1;
779
4.35k
    }
780
781
10.5M
    return 1;
782
10.6M
}
783
784
/*
785
 * Set the scanner error and return 0.
786
 */
787
788
static int
789
yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
790
        yaml_mark_t context_mark, const char *problem)
791
19.4k
{
792
19.4k
    parser->error = YAML_SCANNER_ERROR;
793
19.4k
    parser->context = context;
794
19.4k
    parser->context_mark = context_mark;
795
19.4k
    parser->problem = problem;
796
19.4k
    parser->problem_mark = parser->mark;
797
798
19.4k
    return 0;
799
19.4k
}
800
801
/*
802
 * Ensure that the tokens queue contains at least one token which can be
803
 * returned to the Parser.
804
 */
805
806
YAML_DECLARE(int)
807
yaml_parser_fetch_more_tokens(yaml_parser_t *parser)
808
136M
{
809
136M
    int need_more_tokens;
810
811
    /* While we need more tokens to fetch, do it. */
812
813
231M
    while (1)
814
231M
    {
815
        /*
816
         * Check if we really need to fetch more tokens.
817
         */
818
819
231M
        need_more_tokens = 0;
820
821
231M
        if (parser->tokens.head == parser->tokens.tail)
822
69.7M
        {
823
            /* Queue is empty. */
824
825
69.7M
            need_more_tokens = 1;
826
69.7M
        }
827
161M
        else
828
161M
        {
829
161M
            yaml_simple_key_t *simple_key;
830
831
            /* Check if any potential simple key may occupy the head position. */
832
833
161M
            if (!yaml_parser_stale_simple_keys(parser))
834
269
                return 0;
835
836
161M
            for (simple_key = parser->simple_keys.start;
837
2.69G
                    simple_key != parser->simple_keys.top; simple_key++) {
838
2.55G
                if (simple_key->possible
839
51.1M
                        && simple_key->token_number == parser->tokens_parsed) {
840
25.1M
                    need_more_tokens = 1;
841
25.1M
                    break;
842
25.1M
                }
843
2.55G
            }
844
161M
        }
845
846
        /* We are finished. */
847
848
231M
        if (!need_more_tokens)
849
136M
            break;
850
851
        /* Fetch the next token. */
852
853
94.9M
        if (!yaml_parser_fetch_next_token(parser))
854
21.4k
            return 0;
855
94.9M
    }
856
857
136M
    parser->token_available = 1;
858
859
136M
    return 1;
860
136M
}
861
862
/*
863
 * The dispatcher for token fetchers.
864
 */
865
866
static int
867
yaml_parser_fetch_next_token(yaml_parser_t *parser)
868
94.9M
{
869
    /* Ensure that the buffer is initialized. */
870
871
94.9M
    if (!CACHE(parser, 1))
872
1.14k
        return 0;
873
874
    /* Check if we just started scanning.  Fetch STREAM-START then. */
875
876
94.9M
    if (!parser->stream_start_produced)
877
88.1k
        return yaml_parser_fetch_stream_start(parser);
878
879
    /* Eat whitespaces and comments until we reach the next token. */
880
881
94.8M
    if (!yaml_parser_scan_to_next_token(parser))
882
126
        return 0;
883
884
    /* Remove obsolete potential simple keys. */
885
886
94.8M
    if (!yaml_parser_stale_simple_keys(parser))
887
43
        return 0;
888
889
    /* Check the indentation level against the current column. */
890
891
94.8M
    if (!yaml_parser_unroll_indent(parser, parser->mark.column))
892
0
        return 0;
893
894
    /*
895
     * Ensure that the buffer contains at least 4 characters.  4 is the length
896
     * of the longest indicators ('--- ' and '... ').
897
     */
898
899
94.8M
    if (!CACHE(parser, 4))
900
132
        return 0;
901
902
    /* Is it the end of the stream? */
903
904
94.8M
    if (IS_Z(parser->buffer))
905
62.6k
        return yaml_parser_fetch_stream_end(parser);
906
907
    /* Is it a directive? */
908
909
94.8M
    if (parser->mark.column == 0 && CHECK(parser->buffer, '%'))
910
1.56M
        return yaml_parser_fetch_directive(parser);
911
912
    /* Is it the document start indicator? */
913
914
93.2M
    if (parser->mark.column == 0
915
15.0M
            && CHECK_AT(parser->buffer, '-', 0)
916
5.80M
            && CHECK_AT(parser->buffer, '-', 1)
917
1.68M
            && CHECK_AT(parser->buffer, '-', 2)
918
1.63M
            && IS_BLANKZ_AT(parser->buffer, 3))
919
1.62M
        return yaml_parser_fetch_document_indicator(parser,
920
1.62M
                YAML_DOCUMENT_START_TOKEN);
921
922
    /* Is it the document end indicator? */
923
924
91.6M
    if (parser->mark.column == 0
925
13.4M
            && CHECK_AT(parser->buffer, '.', 0)
926
196k
            && CHECK_AT(parser->buffer, '.', 1)
927
169k
            && CHECK_AT(parser->buffer, '.', 2)
928
166k
            && IS_BLANKZ_AT(parser->buffer, 3))
929
155k
        return yaml_parser_fetch_document_indicator(parser,
930
155k
                YAML_DOCUMENT_END_TOKEN);
931
932
    /* Is it the flow sequence start indicator? */
933
934
91.4M
    if (CHECK(parser->buffer, '['))
935
1.04M
        return yaml_parser_fetch_flow_collection_start(parser,
936
1.04M
                YAML_FLOW_SEQUENCE_START_TOKEN);
937
938
    /* Is it the flow mapping start indicator? */
939
940
90.4M
    if (CHECK(parser->buffer, '{'))
941
1.21M
        return yaml_parser_fetch_flow_collection_start(parser,
942
1.21M
                YAML_FLOW_MAPPING_START_TOKEN);
943
944
    /* Is it the flow sequence end indicator? */
945
946
89.2M
    if (CHECK(parser->buffer, ']'))
947
827k
        return yaml_parser_fetch_flow_collection_end(parser,
948
827k
                YAML_FLOW_SEQUENCE_END_TOKEN);
949
950
    /* Is it the flow mapping end indicator? */
951
952
88.3M
    if (CHECK(parser->buffer, '}'))
953
1.07M
        return yaml_parser_fetch_flow_collection_end(parser,
954
1.07M
                YAML_FLOW_MAPPING_END_TOKEN);
955
956
    /* Is it the flow entry indicator? */
957
958
87.3M
    if (CHECK(parser->buffer, ','))
959
19.4M
        return yaml_parser_fetch_flow_entry(parser);
960
961
    /* Is it the block entry indicator? */
962
963
67.8M
    if (CHECK(parser->buffer, '-') && IS_BLANKZ_AT(parser->buffer, 1))
964
5.14M
        return yaml_parser_fetch_block_entry(parser);
965
966
    /* Is it the key indicator? */
967
968
62.7M
    if (CHECK(parser->buffer, '?')
969
22.4M
            && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1)))
970
22.1M
        return yaml_parser_fetch_key(parser);
971
972
    /* Is it the value indicator? */
973
974
40.5M
    if (CHECK(parser->buffer, ':')
975
9.27M
            && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1)))
976
8.38M
        return yaml_parser_fetch_value(parser);
977
978
    /* Is it an alias? */
979
980
32.1M
    if (CHECK(parser->buffer, '*'))
981
1.15M
        return yaml_parser_fetch_anchor(parser, YAML_ALIAS_TOKEN);
982
983
    /* Is it an anchor? */
984
985
31.0M
    if (CHECK(parser->buffer, '&'))
986
103k
        return yaml_parser_fetch_anchor(parser, YAML_ANCHOR_TOKEN);
987
988
    /* Is it a tag? */
989
990
30.9M
    if (CHECK(parser->buffer, '!'))
991
6.00M
        return yaml_parser_fetch_tag(parser);
992
993
    /* Is it a literal scalar? */
994
995
24.9M
    if (CHECK(parser->buffer, '|') && !parser->flow_level)
996
85.8k
        return yaml_parser_fetch_block_scalar(parser, 1);
997
998
    /* Is it a folded scalar? */
999
1000
24.8M
    if (CHECK(parser->buffer, '>') && !parser->flow_level)
1001
723k
        return yaml_parser_fetch_block_scalar(parser, 0);
1002
1003
    /* Is it a single-quoted scalar? */
1004
1005
24.1M
    if (CHECK(parser->buffer, '\''))
1006
1.70M
        return yaml_parser_fetch_flow_scalar(parser, 1);
1007
1008
    /* Is it a double-quoted scalar? */
1009
1010
22.3M
    if (CHECK(parser->buffer, '"'))
1011
4.68M
        return yaml_parser_fetch_flow_scalar(parser, 0);
1012
1013
    /*
1014
     * Is it a plain scalar?
1015
     *
1016
     * A plain scalar may start with any non-blank characters except
1017
     *
1018
     *      '-', '?', ':', ',', '[', ']', '{', '}',
1019
     *      '#', '&', '*', '!', '|', '>', '\'', '\"',
1020
     *      '%', '@', '`'.
1021
     *
1022
     * In the block context (and, for the '-' indicator, in the flow context
1023
     * too), it may also start with the characters
1024
     *
1025
     *      '-', '?', ':'
1026
     *
1027
     * if it is followed by a non-space character.
1028
     *
1029
     * The last rule is more restrictive than the specification requires.
1030
     */
1031
1032
17.7M
    if (!(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '-')
1033
17.3M
                || CHECK(parser->buffer, '?') || CHECK(parser->buffer, ':')
1034
16.1M
                || CHECK(parser->buffer, ',') || CHECK(parser->buffer, '[')
1035
16.1M
                || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{')
1036
16.1M
                || CHECK(parser->buffer, '}') || CHECK(parser->buffer, '#')
1037
16.1M
                || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '*')
1038
16.1M
                || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '|')
1039
16.1M
                || CHECK(parser->buffer, '>') || CHECK(parser->buffer, '\'')
1040
16.1M
                || CHECK(parser->buffer, '"') || CHECK(parser->buffer, '%')
1041
16.1M
                || CHECK(parser->buffer, '@') || CHECK(parser->buffer, '`')) ||
1042
1.53M
            (CHECK(parser->buffer, '-') && !IS_BLANK_AT(parser->buffer, 1)) ||
1043
1.18M
            (!parser->flow_level &&
1044
1.18M
             (CHECK(parser->buffer, '?') || CHECK(parser->buffer, ':'))
1045
1.18M
             && !IS_BLANKZ_AT(parser->buffer, 1)))
1046
17.7M
        return yaml_parser_fetch_plain_scalar(parser);
1047
1048
    /*
1049
     * If we don't determine the token type so far, it is an error.
1050
     */
1051
1052
464
    return yaml_parser_set_scanner_error(parser,
1053
464
            "while scanning for the next token", parser->mark,
1054
464
            "found character that cannot start any token");
1055
17.7M
}
1056
1057
/*
1058
 * Check the list of potential simple keys and remove the positions that
1059
 * cannot contain simple keys anymore.
1060
 */
1061
1062
static int
1063
yaml_parser_stale_simple_keys(yaml_parser_t *parser)
1064
256M
{
1065
256M
    yaml_simple_key_t *simple_key;
1066
1067
    /* Check for a potential simple key for each flow level. */
1068
1069
256M
    for (simple_key = parser->simple_keys.start;
1070
4.67G
            simple_key != parser->simple_keys.top; simple_key ++)
1071
4.41G
    {
1072
        /*
1073
         * The specification requires that a simple key
1074
         *
1075
         *  - is limited to a single line,
1076
         *  - is shorter than 1024 characters.
1077
         */
1078
1079
4.41G
        if (simple_key->possible
1080
292M
                && (simple_key->mark.line < parser->mark.line
1081
291M
                    || simple_key->mark.index+1024 < parser->mark.index)) {
1082
1083
            /* Check if the potential simple key to be removed is required. */
1084
1085
1.55M
            if (simple_key->required) {
1086
312
                return yaml_parser_set_scanner_error(parser,
1087
312
                        "while scanning a simple key", simple_key->mark,
1088
312
                        "could not find expected ':'");
1089
312
            }
1090
1091
1.55M
            simple_key->possible = 0;
1092
1.55M
        }
1093
4.41G
    }
1094
1095
256M
    return 1;
1096
256M
}
1097
1098
/*
1099
 * Check if a simple key may start at the current position and add it if
1100
 * needed.
1101
 */
1102
1103
static int
1104
yaml_parser_save_simple_key(yaml_parser_t *parser)
1105
33.6M
{
1106
    /*
1107
     * A simple key is required at the current position if the scanner is in
1108
     * the block context and the current column coincides with the indentation
1109
     * level.
1110
     */
1111
1112
33.6M
    int required = (!parser->flow_level
1113
7.71M
            && parser->indent == (ptrdiff_t)parser->mark.column);
1114
1115
    /*
1116
     * If the current position may start a simple key, save it.
1117
     */
1118
1119
33.6M
    if (parser->simple_key_allowed)
1120
22.7M
    {
1121
22.7M
        yaml_simple_key_t simple_key;
1122
22.7M
        simple_key.possible = 1;
1123
22.7M
        simple_key.required = required;
1124
22.7M
        simple_key.token_number =
1125
22.7M
            parser->tokens_parsed + (parser->tokens.tail - parser->tokens.head);
1126
22.7M
        simple_key.mark = parser->mark;
1127
1128
22.7M
        if (!yaml_parser_remove_simple_key(parser)) return 0;
1129
1130
22.7M
        *(parser->simple_keys.top-1) = simple_key;
1131
22.7M
    }
1132
1133
33.6M
    return 1;
1134
33.6M
}
1135
1136
/*
1137
 * Remove a potential simple key at the current flow level.
1138
 */
1139
1140
static int
1141
yaml_parser_remove_simple_key(yaml_parser_t *parser)
1142
75.6M
{
1143
75.6M
    yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1144
1145
75.6M
    if (simple_key->possible)
1146
15.5M
    {
1147
        /* If the key is required, it is an error. */
1148
1149
15.5M
        if (simple_key->required) {
1150
1.15k
            return yaml_parser_set_scanner_error(parser,
1151
1.15k
                    "while scanning a simple key", simple_key->mark,
1152
1.15k
                    "could not find expected ':'");
1153
1.15k
        }
1154
15.5M
    }
1155
1156
    /* Remove the key from the stack. */
1157
1158
75.6M
    simple_key->possible = 0;
1159
1160
75.6M
    return 1;
1161
75.6M
}
1162
1163
/*
1164
 * Increase the flow level and resize the simple key list if needed.
1165
 */
1166
1167
static int
1168
yaml_parser_increase_flow_level(yaml_parser_t *parser)
1169
2.26M
{
1170
2.26M
    yaml_simple_key_t empty_simple_key = { 0, 0, 0, { 0, 0, 0 } };
1171
1172
    /* Reset the simple key on the next level. */
1173
1174
2.26M
    if (!PUSH(parser, parser->simple_keys, empty_simple_key))
1175
0
        return 0;
1176
1177
    /* Increase the flow level. */
1178
1179
2.26M
    if (parser->flow_level == INT_MAX) {
1180
0
        parser->error = YAML_MEMORY_ERROR;
1181
0
        return 0;
1182
0
    }
1183
1184
2.26M
    if (!STACK_LIMIT(parser, parser->indents, MAX_NESTING_LEVEL - parser->flow_level)) {
1185
24
        return yaml_parser_set_scanner_error(parser,
1186
24
                "while increasing flow level", parser->mark,
1187
24
                "exceeded maximum nesting depth");
1188
24
    }
1189
1190
2.26M
    parser->flow_level++;
1191
1192
2.26M
    return 1;
1193
2.26M
}
1194
1195
/*
1196
 * Decrease the flow level.
1197
 */
1198
1199
static int
1200
yaml_parser_decrease_flow_level(yaml_parser_t *parser)
1201
1.90M
{
1202
1.90M
    if (parser->flow_level) {
1203
1.86M
        parser->flow_level --;
1204
1.86M
        (void)POP(parser, parser->simple_keys);
1205
1.86M
    }
1206
1207
1.90M
    return 1;
1208
1.90M
}
1209
1210
/*
1211
 * Push the current indentation level to the stack and set the new level
1212
 * the current column is greater than the indentation level.  In this case,
1213
 * append or insert the specified token into the token queue.
1214
 *
1215
 */
1216
1217
static int
1218
yaml_parser_roll_indent(yaml_parser_t *parser, ptrdiff_t column,
1219
        ptrdiff_t number, yaml_token_type_t type, yaml_mark_t mark)
1220
31.7M
{
1221
31.7M
    yaml_token_t token;
1222
1223
    /* In the flow context, do nothing. */
1224
1225
31.7M
    if (parser->flow_level)
1226
846k
        return 1;
1227
1228
30.9M
    if (parser->indent < column)
1229
18.1M
    {
1230
        /*
1231
         * Push the current indentation level to the stack and set the new
1232
         * indentation level.
1233
         */
1234
1235
18.1M
        if (!PUSH(parser, parser->indents, parser->indent))
1236
0
            return 0;
1237
1238
18.1M
        if (!STACK_LIMIT(parser, parser->indents, MAX_NESTING_LEVEL - parser->flow_level)) {
1239
43
            return yaml_parser_set_scanner_error(parser,
1240
43
                    "while increasing block level", parser->mark,
1241
43
                    "exceeded maximum nesting depth");
1242
43
        }
1243
1244
18.1M
        if (column > INT_MAX) {
1245
0
            parser->error = YAML_MEMORY_ERROR;
1246
0
            return 0;
1247
0
        }
1248
1249
18.1M
        parser->indent = column;
1250
1251
        /* Create a token and insert it into the queue. */
1252
1253
18.1M
        TOKEN_INIT(token, type, mark, mark);
1254
1255
18.1M
        if (number == -1) {
1256
13.6M
            if (!ENQUEUE(parser, parser->tokens, token))
1257
0
                return 0;
1258
13.6M
        }
1259
4.43M
        else {
1260
4.43M
            if (!QUEUE_INSERT(parser,
1261
4.43M
                        parser->tokens, number - parser->tokens_parsed, token))
1262
0
                return 0;
1263
4.43M
        }
1264
18.1M
    }
1265
1266
30.9M
    return 1;
1267
30.9M
}
1268
1269
/*
1270
 * Pop indentation levels from the indents stack until the current level
1271
 * becomes less or equal to the column.  For each indentation level, append
1272
 * the BLOCK-END token.
1273
 */
1274
1275
1276
static int
1277
yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column)
1278
98.2M
{
1279
98.2M
    yaml_token_t token;
1280
1281
    /* In the flow context, do nothing. */
1282
1283
98.2M
    if (parser->flow_level)
1284
51.2M
        return 1;
1285
1286
    /* Loop through the indentation levels in the stack. */
1287
1288
65.0M
    while (parser->indent > column)
1289
18.0M
    {
1290
        /* Create a token and append it to the queue. */
1291
1292
18.0M
        TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark);
1293
1294
18.0M
        if (!ENQUEUE(parser, parser->tokens, token))
1295
0
            return 0;
1296
1297
        /* Pop the indentation level. */
1298
1299
18.0M
        parser->indent = POP(parser, parser->indents);
1300
18.0M
    }
1301
1302
47.0M
    return 1;
1303
47.0M
}
1304
1305
/*
1306
 * Initialize the scanner and produce the STREAM-START token.
1307
 */
1308
1309
static int
1310
yaml_parser_fetch_stream_start(yaml_parser_t *parser)
1311
88.1k
{
1312
88.1k
    yaml_simple_key_t simple_key = { 0, 0, 0, { 0, 0, 0 } };
1313
88.1k
    yaml_token_t token;
1314
1315
    /* Set the initial indentation. */
1316
1317
88.1k
    parser->indent = -1;
1318
1319
    /* Initialize the simple key stack. */
1320
1321
88.1k
    if (!PUSH(parser, parser->simple_keys, simple_key))
1322
0
        return 0;
1323
1324
    /* A simple key is allowed at the beginning of the stream. */
1325
1326
88.1k
    parser->simple_key_allowed = 1;
1327
1328
    /* We have started. */
1329
1330
88.1k
    parser->stream_start_produced = 1;
1331
1332
    /* Create the STREAM-START token and append it to the queue. */
1333
1334
88.1k
    STREAM_START_TOKEN_INIT(token, parser->encoding,
1335
88.1k
            parser->mark, parser->mark);
1336
1337
88.1k
    if (!ENQUEUE(parser, parser->tokens, token))
1338
0
        return 0;
1339
1340
88.1k
    return 1;
1341
88.1k
}
1342
1343
/*
1344
 * Produce the STREAM-END token and shut down the scanner.
1345
 */
1346
1347
static int
1348
yaml_parser_fetch_stream_end(yaml_parser_t *parser)
1349
62.6k
{
1350
62.6k
    yaml_token_t token;
1351
1352
    /* Force new line. */
1353
1354
62.6k
    if (parser->mark.column != 0) {
1355
50.3k
        parser->mark.column = 0;
1356
50.3k
        parser->mark.line ++;
1357
50.3k
    }
1358
1359
    /* Reset the indentation level. */
1360
1361
62.6k
    if (!yaml_parser_unroll_indent(parser, -1))
1362
0
        return 0;
1363
1364
    /* Reset simple keys. */
1365
1366
62.6k
    if (!yaml_parser_remove_simple_key(parser))
1367
1.11k
        return 0;
1368
1369
61.5k
    parser->simple_key_allowed = 0;
1370
1371
    /* Create the STREAM-END token and append it to the queue. */
1372
1373
61.5k
    STREAM_END_TOKEN_INIT(token, parser->mark, parser->mark);
1374
1375
61.5k
    if (!ENQUEUE(parser, parser->tokens, token))
1376
0
        return 0;
1377
1378
61.5k
    return 1;
1379
61.5k
}
1380
1381
/*
1382
 * Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
1383
 */
1384
1385
static int
1386
yaml_parser_fetch_directive(yaml_parser_t *parser)
1387
1.56M
{
1388
1.56M
    yaml_token_t token;
1389
1390
    /* Reset the indentation level. */
1391
1392
1.56M
    if (!yaml_parser_unroll_indent(parser, -1))
1393
0
        return 0;
1394
1395
    /* Reset simple keys. */
1396
1397
1.56M
    if (!yaml_parser_remove_simple_key(parser))
1398
0
        return 0;
1399
1400
1.56M
    parser->simple_key_allowed = 0;
1401
1402
    /* Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. */
1403
1404
1.56M
    if (!yaml_parser_scan_directive(parser, &token))
1405
4.14k
        return 0;
1406
1407
    /* Append the token to the queue. */
1408
1409
1.56M
    if (!ENQUEUE(parser, parser->tokens, token)) {
1410
0
        yaml_token_delete(&token);
1411
0
        return 0;
1412
0
    }
1413
1414
1.56M
    return 1;
1415
1.56M
}
1416
1417
/*
1418
 * Produce the DOCUMENT-START or DOCUMENT-END token.
1419
 */
1420
1421
static int
1422
yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
1423
        yaml_token_type_t type)
1424
1.77M
{
1425
1.77M
    yaml_mark_t start_mark, end_mark;
1426
1.77M
    yaml_token_t token;
1427
1428
    /* Reset the indentation level. */
1429
1430
1.77M
    if (!yaml_parser_unroll_indent(parser, -1))
1431
0
        return 0;
1432
1433
    /* Reset simple keys. */
1434
1435
1.77M
    if (!yaml_parser_remove_simple_key(parser))
1436
0
        return 0;
1437
1438
1.77M
    parser->simple_key_allowed = 0;
1439
1440
    /* Consume the token. */
1441
1442
1.77M
    start_mark = parser->mark;
1443
1444
1.77M
    SKIP(parser);
1445
1.77M
    SKIP(parser);
1446
1.77M
    SKIP(parser);
1447
1448
1.77M
    end_mark = parser->mark;
1449
1450
    /* Create the DOCUMENT-START or DOCUMENT-END token. */
1451
1452
1.77M
    TOKEN_INIT(token, type, start_mark, end_mark);
1453
1454
    /* Append the token to the queue. */
1455
1456
1.77M
    if (!ENQUEUE(parser, parser->tokens, token))
1457
0
        return 0;
1458
1459
1.77M
    return 1;
1460
1.77M
}
1461
1462
/*
1463
 * Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
1464
 */
1465
1466
static int
1467
yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
1468
        yaml_token_type_t type)
1469
2.26M
{
1470
2.26M
    yaml_mark_t start_mark, end_mark;
1471
2.26M
    yaml_token_t token;
1472
1473
    /* The indicators '[' and '{' may start a simple key. */
1474
1475
2.26M
    if (!yaml_parser_save_simple_key(parser))
1476
0
        return 0;
1477
1478
    /* Increase the flow level. */
1479
1480
2.26M
    if (!yaml_parser_increase_flow_level(parser))
1481
24
        return 0;
1482
1483
    /* A simple key may follow the indicators '[' and '{'. */
1484
1485
2.26M
    parser->simple_key_allowed = 1;
1486
1487
    /* Consume the token. */
1488
1489
2.26M
    start_mark = parser->mark;
1490
2.26M
    SKIP(parser);
1491
2.26M
    end_mark = parser->mark;
1492
1493
    /* Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token. */
1494
1495
2.26M
    TOKEN_INIT(token, type, start_mark, end_mark);
1496
1497
    /* Append the token to the queue. */
1498
1499
2.26M
    if (!ENQUEUE(parser, parser->tokens, token))
1500
0
        return 0;
1501
1502
2.26M
    return 1;
1503
2.26M
}
1504
1505
/*
1506
 * Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
1507
 */
1508
1509
static int
1510
yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
1511
        yaml_token_type_t type)
1512
1.90M
{
1513
1.90M
    yaml_mark_t start_mark, end_mark;
1514
1.90M
    yaml_token_t token;
1515
1516
    /* Reset any potential simple key on the current flow level. */
1517
1518
1.90M
    if (!yaml_parser_remove_simple_key(parser))
1519
11
        return 0;
1520
1521
    /* Decrease the flow level. */
1522
1523
1.90M
    if (!yaml_parser_decrease_flow_level(parser))
1524
0
        return 0;
1525
1526
    /* No simple keys after the indicators ']' and '}'. */
1527
1528
1.90M
    parser->simple_key_allowed = 0;
1529
1530
    /* Consume the token. */
1531
1532
1.90M
    start_mark = parser->mark;
1533
1.90M
    SKIP(parser);
1534
1.90M
    end_mark = parser->mark;
1535
1536
    /* Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token. */
1537
1538
1.90M
    TOKEN_INIT(token, type, start_mark, end_mark);
1539
1540
    /* Append the token to the queue. */
1541
1542
1.90M
    if (!ENQUEUE(parser, parser->tokens, token))
1543
0
        return 0;
1544
1545
1.90M
    return 1;
1546
1.90M
}
1547
1548
/*
1549
 * Produce the FLOW-ENTRY token.
1550
 */
1551
1552
static int
1553
yaml_parser_fetch_flow_entry(yaml_parser_t *parser)
1554
19.4M
{
1555
19.4M
    yaml_mark_t start_mark, end_mark;
1556
19.4M
    yaml_token_t token;
1557
1558
    /* Reset any potential simple keys on the current flow level. */
1559
1560
19.4M
    if (!yaml_parser_remove_simple_key(parser))
1561
10
        return 0;
1562
1563
    /* Simple keys are allowed after ','. */
1564
1565
19.4M
    parser->simple_key_allowed = 1;
1566
1567
    /* Consume the token. */
1568
1569
19.4M
    start_mark = parser->mark;
1570
19.4M
    SKIP(parser);
1571
19.4M
    end_mark = parser->mark;
1572
1573
    /* Create the FLOW-ENTRY token and append it to the queue. */
1574
1575
19.4M
    TOKEN_INIT(token, YAML_FLOW_ENTRY_TOKEN, start_mark, end_mark);
1576
1577
19.4M
    if (!ENQUEUE(parser, parser->tokens, token))
1578
0
        return 0;
1579
1580
19.4M
    return 1;
1581
19.4M
}
1582
1583
/*
1584
 * Produce the BLOCK-ENTRY token.
1585
 */
1586
1587
static int
1588
yaml_parser_fetch_block_entry(yaml_parser_t *parser)
1589
5.14M
{
1590
5.14M
    yaml_mark_t start_mark, end_mark;
1591
5.14M
    yaml_token_t token;
1592
1593
    /* Check if the scanner is in the block context. */
1594
1595
5.14M
    if (!parser->flow_level)
1596
5.13M
    {
1597
        /* Check if we are allowed to start a new entry. */
1598
1599
5.13M
        if (!parser->simple_key_allowed) {
1600
76
            return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1601
76
                    "block sequence entries are not allowed in this context");
1602
76
        }
1603
1604
        /* Add the BLOCK-SEQUENCE-START token if needed. */
1605
1606
5.13M
        if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1607
5.13M
                    YAML_BLOCK_SEQUENCE_START_TOKEN, parser->mark))
1608
11
            return 0;
1609
5.13M
    }
1610
8.48k
    else
1611
8.48k
    {
1612
        /*
1613
         * It is an error for the '-' indicator to occur in the flow context,
1614
         * but we let the Parser detect and report about it because the Parser
1615
         * is able to point to the context.
1616
         */
1617
8.48k
    }
1618
1619
    /* Reset any potential simple keys on the current flow level. */
1620
1621
5.14M
    if (!yaml_parser_remove_simple_key(parser))
1622
0
        return 0;
1623
1624
    /* Simple keys are allowed after '-'. */
1625
1626
5.14M
    parser->simple_key_allowed = 1;
1627
1628
    /* Consume the token. */
1629
1630
5.14M
    start_mark = parser->mark;
1631
5.14M
    SKIP(parser);
1632
5.14M
    end_mark = parser->mark;
1633
1634
    /* Create the BLOCK-ENTRY token and append it to the queue. */
1635
1636
5.14M
    TOKEN_INIT(token, YAML_BLOCK_ENTRY_TOKEN, start_mark, end_mark);
1637
1638
5.14M
    if (!ENQUEUE(parser, parser->tokens, token))
1639
0
        return 0;
1640
1641
5.14M
    return 1;
1642
5.14M
}
1643
1644
/*
1645
 * Produce the KEY token.
1646
 */
1647
1648
static int
1649
yaml_parser_fetch_key(yaml_parser_t *parser)
1650
22.1M
{
1651
22.1M
    yaml_mark_t start_mark, end_mark;
1652
22.1M
    yaml_token_t token;
1653
1654
    /* In the block context, additional checks are required. */
1655
1656
22.1M
    if (!parser->flow_level)
1657
19.9M
    {
1658
        /* Check if we are allowed to start a new key (not necessary simple). */
1659
1660
19.9M
        if (!parser->simple_key_allowed) {
1661
73
            return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1662
73
                    "mapping keys are not allowed in this context");
1663
73
        }
1664
1665
        /* Add the BLOCK-MAPPING-START token if needed. */
1666
1667
19.9M
        if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1668
19.9M
                    YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1669
12
            return 0;
1670
19.9M
    }
1671
1672
    /* Reset any potential simple keys on the current flow level. */
1673
1674
22.1M
    if (!yaml_parser_remove_simple_key(parser))
1675
0
        return 0;
1676
1677
    /* Simple keys are allowed after '?' in the block context. */
1678
1679
22.1M
    parser->simple_key_allowed = (!parser->flow_level);
1680
1681
    /* Consume the token. */
1682
1683
22.1M
    start_mark = parser->mark;
1684
22.1M
    SKIP(parser);
1685
22.1M
    end_mark = parser->mark;
1686
1687
    /* Create the KEY token and append it to the queue. */
1688
1689
22.1M
    TOKEN_INIT(token, YAML_KEY_TOKEN, start_mark, end_mark);
1690
1691
22.1M
    if (!ENQUEUE(parser, parser->tokens, token))
1692
0
        return 0;
1693
1694
22.1M
    return 1;
1695
22.1M
}
1696
1697
/*
1698
 * Produce the VALUE token.
1699
 */
1700
1701
static int
1702
yaml_parser_fetch_value(yaml_parser_t *parser)
1703
8.38M
{
1704
8.38M
    yaml_mark_t start_mark, end_mark;
1705
8.38M
    yaml_token_t token;
1706
8.38M
    yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1707
1708
    /* Have we found a simple key? */
1709
1710
8.38M
    if (simple_key->possible)
1711
5.62M
    {
1712
1713
        /* Create the KEY token and insert it into the queue. */
1714
1715
5.62M
        TOKEN_INIT(token, YAML_KEY_TOKEN, simple_key->mark, simple_key->mark);
1716
1717
5.62M
        if (!QUEUE_INSERT(parser, parser->tokens,
1718
5.62M
                    simple_key->token_number - parser->tokens_parsed, token))
1719
0
            return 0;
1720
1721
        /* In the block context, we may need to add the BLOCK-MAPPING-START token. */
1722
1723
5.62M
        if (!yaml_parser_roll_indent(parser, simple_key->mark.column,
1724
5.62M
                    simple_key->token_number,
1725
5.62M
                    YAML_BLOCK_MAPPING_START_TOKEN, simple_key->mark))
1726
10
            return 0;
1727
1728
        /* Remove the simple key. */
1729
1730
5.62M
        simple_key->possible = 0;
1731
1732
        /* A simple key cannot follow another simple key. */
1733
1734
5.62M
        parser->simple_key_allowed = 0;
1735
5.62M
    }
1736
2.76M
    else
1737
2.76M
    {
1738
        /* The ':' indicator follows a complex key. */
1739
1740
        /* In the block context, extra checks are required. */
1741
1742
2.76M
        if (!parser->flow_level)
1743
1.09M
        {
1744
            /* Check if we are allowed to start a complex value. */
1745
1746
1.09M
            if (!parser->simple_key_allowed) {
1747
240
                return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1748
240
                        "mapping values are not allowed in this context");
1749
240
            }
1750
1751
            /* Add the BLOCK-MAPPING-START token if needed. */
1752
1753
1.09M
            if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1754
1.09M
                        YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1755
10
                return 0;
1756
1.09M
        }
1757
1758
        /* Simple keys after ':' are allowed in the block context. */
1759
1760
2.76M
        parser->simple_key_allowed = (!parser->flow_level);
1761
2.76M
    }
1762
1763
    /* Consume the token. */
1764
1765
8.38M
    start_mark = parser->mark;
1766
8.38M
    SKIP(parser);
1767
8.38M
    end_mark = parser->mark;
1768
1769
    /* Create the VALUE token and append it to the queue. */
1770
1771
8.38M
    TOKEN_INIT(token, YAML_VALUE_TOKEN, start_mark, end_mark);
1772
1773
8.38M
    if (!ENQUEUE(parser, parser->tokens, token))
1774
0
        return 0;
1775
1776
8.38M
    return 1;
1777
8.38M
}
1778
1779
/*
1780
 * Produce the ALIAS or ANCHOR token.
1781
 */
1782
1783
static int
1784
yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type)
1785
1.25M
{
1786
1.25M
    yaml_token_t token;
1787
1788
    /* An anchor or an alias could be a simple key. */
1789
1790
1.25M
    if (!yaml_parser_save_simple_key(parser))
1791
0
        return 0;
1792
1793
    /* A simple key cannot follow an anchor or an alias. */
1794
1795
1.25M
    parser->simple_key_allowed = 0;
1796
1797
    /* Create the ALIAS or ANCHOR token and append it to the queue. */
1798
1799
1.25M
    if (!yaml_parser_scan_anchor(parser, &token, type))
1800
1.06k
        return 0;
1801
1802
1.25M
    if (!ENQUEUE(parser, parser->tokens, token)) {
1803
0
        yaml_token_delete(&token);
1804
0
        return 0;
1805
0
    }
1806
1.25M
    return 1;
1807
1.25M
}
1808
1809
/*
1810
 * Produce the TAG token.
1811
 */
1812
1813
static int
1814
yaml_parser_fetch_tag(yaml_parser_t *parser)
1815
6.00M
{
1816
6.00M
    yaml_token_t token;
1817
1818
    /* A tag could be a simple key. */
1819
1820
6.00M
    if (!yaml_parser_save_simple_key(parser))
1821
0
        return 0;
1822
1823
    /* A simple key cannot follow a tag. */
1824
1825
6.00M
    parser->simple_key_allowed = 0;
1826
1827
    /* Create the TAG token and append it to the queue. */
1828
1829
6.00M
    if (!yaml_parser_scan_tag(parser, &token))
1830
2.23k
        return 0;
1831
1832
6.00M
    if (!ENQUEUE(parser, parser->tokens, token)) {
1833
0
        yaml_token_delete(&token);
1834
0
        return 0;
1835
0
    }
1836
1837
6.00M
    return 1;
1838
6.00M
}
1839
1840
/*
1841
 * Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
1842
 */
1843
1844
static int
1845
yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal)
1846
809k
{
1847
809k
    yaml_token_t token;
1848
1849
    /* Remove any potential simple keys. */
1850
1851
809k
    if (!yaml_parser_remove_simple_key(parser))
1852
10
        return 0;
1853
1854
    /* A simple key may follow a block scalar. */
1855
1856
809k
    parser->simple_key_allowed = 1;
1857
1858
    /* Create the SCALAR token and append it to the queue. */
1859
1860
809k
    if (!yaml_parser_scan_block_scalar(parser, &token, literal))
1861
823
        return 0;
1862
1863
808k
    if (!ENQUEUE(parser, parser->tokens, token)) {
1864
0
        yaml_token_delete(&token);
1865
0
        return 0;
1866
0
    }
1867
1868
808k
    return 1;
1869
808k
}
1870
1871
/*
1872
 * Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
1873
 */
1874
1875
static int
1876
yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single)
1877
6.39M
{
1878
6.39M
    yaml_token_t token;
1879
1880
    /* A plain scalar could be a simple key. */
1881
1882
6.39M
    if (!yaml_parser_save_simple_key(parser))
1883
0
        return 0;
1884
1885
    /* A simple key cannot follow a flow scalar. */
1886
1887
6.39M
    parser->simple_key_allowed = 0;
1888
1889
    /* Create the SCALAR token and append it to the queue. */
1890
1891
6.39M
    if (!yaml_parser_scan_flow_scalar(parser, &token, single))
1892
9.16k
        return 0;
1893
1894
6.38M
    if (!ENQUEUE(parser, parser->tokens, token)) {
1895
0
        yaml_token_delete(&token);
1896
0
        return 0;
1897
0
    }
1898
1899
6.38M
    return 1;
1900
6.38M
}
1901
1902
/*
1903
 * Produce the SCALAR(...,plain) token.
1904
 */
1905
1906
static int
1907
yaml_parser_fetch_plain_scalar(yaml_parser_t *parser)
1908
17.7M
{
1909
17.7M
    yaml_token_t token;
1910
1911
    /* A plain scalar could be a simple key. */
1912
1913
17.7M
    if (!yaml_parser_save_simple_key(parser))
1914
0
        return 0;
1915
1916
    /* A simple key cannot follow a flow scalar. */
1917
1918
17.7M
    parser->simple_key_allowed = 0;
1919
1920
    /* Create the SCALAR token and append it to the queue. */
1921
1922
17.7M
    if (!yaml_parser_scan_plain_scalar(parser, &token))
1923
465
        return 0;
1924
1925
17.7M
    if (!ENQUEUE(parser, parser->tokens, token)) {
1926
0
        yaml_token_delete(&token);
1927
0
        return 0;
1928
0
    }
1929
1930
17.7M
    return 1;
1931
17.7M
}
1932
1933
/*
1934
 * Eat whitespaces and comments until the next token is found.
1935
 */
1936
1937
static int
1938
yaml_parser_scan_to_next_token(yaml_parser_t *parser)
1939
94.8M
{
1940
    /* Until the next token is not found. */
1941
1942
138M
    while (1)
1943
138M
    {
1944
        /* Allow the BOM mark to start a line. */
1945
1946
138M
        if (!CACHE(parser, 1)) return 0;
1947
1948
138M
        if (parser->mark.column == 0 && IS_BOM(parser->buffer))
1949
682k
            SKIP(parser);
1950
1951
        /*
1952
         * Eat whitespaces.
1953
         *
1954
         * Tabs are allowed:
1955
         *
1956
         *  - in the flow context;
1957
         *  - in the block context, but not at the beginning of the line or
1958
         *  after '-', '?', or ':' (complex value).
1959
         */
1960
1961
138M
        if (!CACHE(parser, 1)) return 0;
1962
1963
669M
        while (CHECK(parser->buffer,' ') ||
1964
138M
                ((parser->flow_level || !parser->simple_key_allowed) &&
1965
531M
                 CHECK(parser->buffer, '\t'))) {
1966
531M
            SKIP(parser);
1967
531M
            if (!CACHE(parser, 1)) return 0;
1968
531M
        }
1969
1970
        /* Eat a comment until a line break. */
1971
1972
138M
        if (CHECK(parser->buffer, '#')) {
1973
18.2M
            while (!IS_BREAKZ(parser->buffer)) {
1974
18.0M
                SKIP(parser);
1975
18.0M
                if (!CACHE(parser, 1)) return 0;
1976
18.0M
            }
1977
231k
        }
1978
1979
        /* If it is a line break, eat it. */
1980
1981
138M
        if (IS_BREAK(parser->buffer))
1982
43.4M
        {
1983
43.4M
            if (!CACHE(parser, 2)) return 0;
1984
43.4M
            SKIP_LINE(parser);
1985
1986
            /* In the block context, a new line may start a simple key. */
1987
1988
43.4M
            if (!parser->flow_level) {
1989
34.0M
                parser->simple_key_allowed = 1;
1990
34.0M
            }
1991
43.4M
        }
1992
94.8M
        else
1993
94.8M
        {
1994
            /* We have found a token. */
1995
1996
94.8M
            break;
1997
94.8M
        }
1998
138M
    }
1999
2000
94.8M
    return 1;
2001
94.8M
}
2002
2003
/*
2004
 * Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
2005
 *
2006
 * Scope:
2007
 *      %YAML    1.1    # a comment \n
2008
 *      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2009
 *      %TAG    !yaml!  tag:yaml.org,2002:  \n
2010
 *      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2011
 */
2012
2013
int
2014
yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token)
2015
1.56M
{
2016
1.56M
    yaml_mark_t start_mark, end_mark;
2017
1.56M
    yaml_char_t *name = NULL;
2018
1.56M
    int major, minor;
2019
1.56M
    yaml_char_t *handle = NULL, *prefix = NULL;
2020
2021
    /* Eat '%'. */
2022
2023
1.56M
    start_mark = parser->mark;
2024
2025
1.56M
    SKIP(parser);
2026
2027
    /* Scan the directive name. */
2028
2029
1.56M
    if (!yaml_parser_scan_directive_name(parser, start_mark, &name))
2030
792
        goto error;
2031
2032
    /* Is it a YAML directive? */
2033
2034
1.56M
    if (strcmp((char *)name, "YAML") == 0)
2035
1.26M
    {
2036
        /* Scan the VERSION directive value. */
2037
2038
1.26M
        if (!yaml_parser_scan_version_directive_value(parser, start_mark,
2039
1.26M
                    &major, &minor))
2040
667
            goto error;
2041
2042
1.26M
        end_mark = parser->mark;
2043
2044
        /* Create a VERSION-DIRECTIVE token. */
2045
2046
1.26M
        VERSION_DIRECTIVE_TOKEN_INIT(*token, major, minor,
2047
1.26M
                start_mark, end_mark);
2048
1.26M
    }
2049
2050
    /* Is it a TAG directive? */
2051
2052
298k
    else if (strcmp((char *)name, "TAG") == 0)
2053
297k
    {
2054
        /* Scan the TAG directive value. */
2055
2056
297k
        if (!yaml_parser_scan_tag_directive_value(parser, start_mark,
2057
297k
                    &handle, &prefix))
2058
1.13k
            goto error;
2059
2060
296k
        end_mark = parser->mark;
2061
2062
        /* Create a TAG-DIRECTIVE token. */
2063
2064
296k
        TAG_DIRECTIVE_TOKEN_INIT(*token, handle, prefix,
2065
296k
                start_mark, end_mark);
2066
296k
    }
2067
2068
    /* Unknown directive. */
2069
2070
1.16k
    else
2071
1.16k
    {
2072
1.16k
        yaml_parser_set_scanner_error(parser, "while scanning a directive",
2073
1.16k
                start_mark, "found unknown directive name");
2074
1.16k
        goto error;
2075
1.16k
    }
2076
2077
    /* Eat the rest of the line including any comments. */
2078
2079
1.56M
    if (!CACHE(parser, 1)) goto error;
2080
2081
8.52M
    while (IS_BLANK(parser->buffer)) {
2082
8.52M
        SKIP(parser);
2083
8.52M
        if (!CACHE(parser, 1)) goto error;
2084
8.52M
    }
2085
2086
1.56M
    if (CHECK(parser->buffer, '#')) {
2087
11.8M
        while (!IS_BREAKZ(parser->buffer)) {
2088
11.6M
            SKIP(parser);
2089
11.6M
            if (!CACHE(parser, 1)) goto error;
2090
11.6M
        }
2091
139k
    }
2092
2093
    /* Check if we are at the end of the line. */
2094
2095
1.56M
    if (!IS_BREAKZ(parser->buffer)) {
2096
331
        yaml_parser_set_scanner_error(parser, "while scanning a directive",
2097
331
                start_mark, "did not find expected comment or line break");
2098
331
        goto error;
2099
331
    }
2100
2101
    /* Eat a line break. */
2102
2103
1.56M
    if (IS_BREAK(parser->buffer)) {
2104
1.55M
        if (!CACHE(parser, 2)) goto error;
2105
1.55M
        SKIP_LINE(parser);
2106
1.55M
    }
2107
2108
1.56M
    yaml_free(name);
2109
2110
1.56M
    return 1;
2111
2112
4.14k
error:
2113
4.14k
    yaml_free(prefix);
2114
4.14k
    yaml_free(handle);
2115
4.14k
    yaml_free(name);
2116
4.14k
    return 0;
2117
1.56M
}
2118
2119
/*
2120
 * Scan the directive name.
2121
 *
2122
 * Scope:
2123
 *      %YAML   1.1     # a comment \n
2124
 *       ^^^^
2125
 *      %TAG    !yaml!  tag:yaml.org,2002:  \n
2126
 *       ^^^
2127
 */
2128
2129
static int
2130
yaml_parser_scan_directive_name(yaml_parser_t *parser,
2131
        yaml_mark_t start_mark, yaml_char_t **name)
2132
1.56M
{
2133
1.56M
    yaml_string_t string = NULL_STRING;
2134
2135
1.56M
    if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2136
2137
    /* Consume the directive name. */
2138
2139
1.56M
    if (!CACHE(parser, 1)) goto error;
2140
2141
1.56M
    while (IS_ALPHA(parser->buffer))
2142
22.1M
    {
2143
22.1M
        if (!READ(parser, string)) goto error;
2144
22.1M
        if (!CACHE(parser, 1)) goto error;
2145
22.1M
    }
2146
2147
    /* Check if the name is empty. */
2148
2149
1.56M
    if (string.start == string.pointer) {
2150
427
        yaml_parser_set_scanner_error(parser, "while scanning a directive",
2151
427
                start_mark, "could not find expected directive name");
2152
427
        goto error;
2153
427
    }
2154
2155
    /* Check for an blank character after the name. */
2156
2157
1.56M
    if (!IS_BLANKZ(parser->buffer)) {
2158
352
        yaml_parser_set_scanner_error(parser, "while scanning a directive",
2159
352
                start_mark, "found unexpected non-alphabetical character");
2160
352
        goto error;
2161
352
    }
2162
2163
1.56M
    *name = string.start;
2164
2165
1.56M
    return 1;
2166
2167
792
error:
2168
792
    STRING_DEL(parser, string);
2169
792
    return 0;
2170
1.56M
}
2171
2172
/*
2173
 * Scan the value of VERSION-DIRECTIVE.
2174
 *
2175
 * Scope:
2176
 *      %YAML   1.1     # a comment \n
2177
 *           ^^^^^^
2178
 */
2179
2180
static int
2181
yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
2182
        yaml_mark_t start_mark, int *major, int *minor)
2183
1.26M
{
2184
    /* Eat whitespaces. */
2185
2186
1.26M
    if (!CACHE(parser, 1)) return 0;
2187
2188
10.1M
    while (IS_BLANK(parser->buffer)) {
2189
10.1M
        SKIP(parser);
2190
10.1M
        if (!CACHE(parser, 1)) return 0;
2191
10.1M
    }
2192
2193
    /* Consume the major version number. */
2194
2195
1.26M
    if (!yaml_parser_scan_version_directive_number(parser, start_mark, major))
2196
381
        return 0;
2197
2198
    /* Eat '.'. */
2199
2200
1.26M
    if (!CHECK(parser->buffer, '.')) {
2201
192
        return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2202
192
                start_mark, "did not find expected digit or '.' character");
2203
192
    }
2204
2205
1.26M
    SKIP(parser);
2206
2207
    /* Consume the minor version number. */
2208
2209
1.26M
    if (!yaml_parser_scan_version_directive_number(parser, start_mark, minor))
2210
85
        return 0;
2211
2212
1.26M
    return 1;
2213
1.26M
}
2214
2215
2.54M
#define MAX_NUMBER_LENGTH   9
2216
2217
/*
2218
 * Scan the version number of VERSION-DIRECTIVE.
2219
 *
2220
 * Scope:
2221
 *      %YAML   1.1     # a comment \n
2222
 *              ^
2223
 *      %YAML   1.1     # a comment \n
2224
 *                ^
2225
 */
2226
2227
static int
2228
yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
2229
        yaml_mark_t start_mark, int *number)
2230
2.53M
{
2231
2.53M
    int value = 0;
2232
2.53M
    size_t length = 0;
2233
2234
    /* Repeat while the next character is digit. */
2235
2236
2.53M
    if (!CACHE(parser, 1)) return 0;
2237
2238
2.53M
    while (IS_DIGIT(parser->buffer))
2239
2.54M
    {
2240
        /* Check if the number is too long. */
2241
2242
2.54M
        if (++length > MAX_NUMBER_LENGTH) {
2243
23
            return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2244
23
                    start_mark, "found extremely long version number");
2245
23
        }
2246
2247
2.54M
        value = value*10 + AS_DIGIT(parser->buffer);
2248
2249
2.54M
        SKIP(parser);
2250
2251
2.54M
        if (!CACHE(parser, 1)) return 0;
2252
2.54M
    }
2253
2254
    /* Check if the number was present. */
2255
2256
2.53M
    if (!length) {
2257
424
        return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2258
424
                start_mark, "did not find expected version number");
2259
424
    }
2260
2261
2.53M
    *number = value;
2262
2263
2.53M
    return 1;
2264
2.53M
}
2265
2266
/*
2267
 * Scan the value of a TAG-DIRECTIVE token.
2268
 *
2269
 * Scope:
2270
 *      %TAG    !yaml!  tag:yaml.org,2002:  \n
2271
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2272
 */
2273
2274
static int
2275
yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
2276
        yaml_mark_t start_mark, yaml_char_t **handle, yaml_char_t **prefix)
2277
297k
{
2278
297k
    yaml_char_t *handle_value = NULL;
2279
297k
    yaml_char_t *prefix_value = NULL;
2280
2281
    /* Eat whitespaces. */
2282
2283
297k
    if (!CACHE(parser, 1)) goto error;
2284
2285
9.91M
    while (IS_BLANK(parser->buffer)) {
2286
9.91M
        SKIP(parser);
2287
9.91M
        if (!CACHE(parser, 1)) goto error;
2288
9.91M
    }
2289
2290
    /* Scan a handle. */
2291
2292
297k
    if (!yaml_parser_scan_tag_handle(parser, 1, start_mark, &handle_value))
2293
458
        goto error;
2294
2295
    /* Expect a whitespace. */
2296
2297
296k
    if (!CACHE(parser, 1)) goto error;
2298
2299
296k
    if (!IS_BLANK(parser->buffer)) {
2300
120
        yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2301
120
                start_mark, "did not find expected whitespace");
2302
120
        goto error;
2303
120
    }
2304
2305
    /* Eat whitespaces. */
2306
2307
9.46M
    while (IS_BLANK(parser->buffer)) {
2308
9.46M
        SKIP(parser);
2309
9.46M
        if (!CACHE(parser, 1)) goto error;
2310
9.46M
    }
2311
2312
    /* Scan a prefix. */
2313
2314
296k
    if (!yaml_parser_scan_tag_uri(parser, 1, 1, NULL, start_mark, &prefix_value))
2315
319
        goto error;
2316
2317
    /* Expect a whitespace or line break. */
2318
2319
296k
    if (!CACHE(parser, 1)) goto error;
2320
2321
296k
    if (!IS_BLANKZ(parser->buffer)) {
2322
209
        yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2323
209
                start_mark, "did not find expected whitespace or line break");
2324
209
        goto error;
2325
209
    }
2326
2327
296k
    *handle = handle_value;
2328
296k
    *prefix = prefix_value;
2329
2330
296k
    return 1;
2331
2332
1.13k
error:
2333
1.13k
    yaml_free(handle_value);
2334
1.13k
    yaml_free(prefix_value);
2335
1.13k
    return 0;
2336
296k
}
2337
2338
static int
2339
yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
2340
        yaml_token_type_t type)
2341
1.25M
{
2342
1.25M
    int length = 0;
2343
1.25M
    yaml_mark_t start_mark, end_mark;
2344
1.25M
    yaml_string_t string = NULL_STRING;
2345
2346
1.25M
    if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2347
2348
    /* Eat the indicator character. */
2349
2350
1.25M
    start_mark = parser->mark;
2351
2352
1.25M
    SKIP(parser);
2353
2354
    /* Consume the value. */
2355
2356
1.25M
    if (!CACHE(parser, 1)) goto error;
2357
2358
39.9M
    while (IS_ALPHA(parser->buffer)) {
2359
39.9M
        if (!READ(parser, string)) goto error;
2360
39.9M
        if (!CACHE(parser, 1)) goto error;
2361
39.9M
        length ++;
2362
39.9M
    }
2363
2364
1.25M
    end_mark = parser->mark;
2365
2366
    /*
2367
     * Check if length of the anchor is greater than 0 and it is followed by
2368
     * a whitespace character or one of the indicators:
2369
     *
2370
     *      '?', ':', ',', ']', '}', '%', '@', '`'.
2371
     */
2372
2373
1.25M
    if (!length || !(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '?')
2374
963k
                || CHECK(parser->buffer, ':') || CHECK(parser->buffer, ',')
2375
27.8k
                || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '}')
2376
305
                || CHECK(parser->buffer, '%') || CHECK(parser->buffer, '@')
2377
1.05k
                || CHECK(parser->buffer, '`'))) {
2378
1.05k
        yaml_parser_set_scanner_error(parser, type == YAML_ANCHOR_TOKEN ?
2379
662
                "while scanning an anchor" : "while scanning an alias", start_mark,
2380
1.05k
                "did not find expected alphabetic or numeric character");
2381
1.05k
        goto error;
2382
1.05k
    }
2383
2384
    /* Create a token. */
2385
2386
1.25M
    if (type == YAML_ANCHOR_TOKEN) {
2387
102k
        ANCHOR_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2388
102k
    }
2389
1.15M
    else {
2390
1.15M
        ALIAS_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2391
1.15M
    }
2392
2393
1.25M
    return 1;
2394
2395
1.06k
error:
2396
1.06k
    STRING_DEL(parser, string);
2397
1.06k
    return 0;
2398
1.25M
}
2399
2400
/*
2401
 * Scan a TAG token.
2402
 */
2403
2404
static int
2405
yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token)
2406
6.00M
{
2407
6.00M
    yaml_char_t *handle = NULL;
2408
6.00M
    yaml_char_t *suffix = NULL;
2409
6.00M
    yaml_mark_t start_mark, end_mark;
2410
2411
6.00M
    start_mark = parser->mark;
2412
2413
    /* Check if the tag is in the canonical form. */
2414
2415
6.00M
    if (!CACHE(parser, 2)) goto error;
2416
2417
6.00M
    if (CHECK_AT(parser->buffer, '<', 1))
2418
600k
    {
2419
        /* Set the handle to '' */
2420
2421
600k
        handle = YAML_MALLOC(1);
2422
600k
        if (!handle) goto error;
2423
600k
        handle[0] = '\0';
2424
2425
        /* Eat '!<' */
2426
2427
600k
        SKIP(parser);
2428
600k
        SKIP(parser);
2429
2430
        /* Consume the tag value. */
2431
2432
600k
        if (!yaml_parser_scan_tag_uri(parser, 1, 0, NULL, start_mark, &suffix))
2433
123
            goto error;
2434
2435
        /* Check for '>' and eat it. */
2436
2437
600k
        if (!CHECK(parser->buffer, '>')) {
2438
270
            yaml_parser_set_scanner_error(parser, "while scanning a tag",
2439
270
                    start_mark, "did not find the expected '>'");
2440
270
            goto error;
2441
270
        }
2442
2443
600k
        SKIP(parser);
2444
600k
    }
2445
5.40M
    else
2446
5.40M
    {
2447
        /* The tag has either the '!suffix' or the '!handle!suffix' form. */
2448
2449
        /* First, try to scan a handle. */
2450
2451
5.40M
        if (!yaml_parser_scan_tag_handle(parser, 0, start_mark, &handle))
2452
9
            goto error;
2453
2454
        /* Check if it is, indeed, handle. */
2455
2456
5.40M
        if (handle[0] == '!' && handle[1] != '\0' && handle[strlen((char *)handle)-1] == '!')
2457
3.09M
        {
2458
            /* Scan the suffix now. */
2459
2460
3.09M
            if (!yaml_parser_scan_tag_uri(parser, 0, 0, NULL, start_mark, &suffix))
2461
219
                goto error;
2462
3.09M
        }
2463
2.30M
        else
2464
2.30M
        {
2465
            /* It wasn't a handle after all.  Scan the rest of the tag. */
2466
2467
2.30M
            if (!yaml_parser_scan_tag_uri(parser, 0, 0, handle, start_mark, &suffix))
2468
1.04k
                goto error;
2469
2470
            /* Set the handle to '!'. */
2471
2472
2.30M
            yaml_free(handle);
2473
2.30M
            handle = YAML_MALLOC(2);
2474
2.30M
            if (!handle) goto error;
2475
2.30M
            handle[0] = '!';
2476
2.30M
            handle[1] = '\0';
2477
2478
            /*
2479
             * A special case: the '!' tag.  Set the handle to '' and the
2480
             * suffix to '!'.
2481
             */
2482
2483
2.30M
            if (suffix[0] == '\0') {
2484
2.02M
                yaml_char_t *tmp = handle;
2485
2.02M
                handle = suffix;
2486
2.02M
                suffix = tmp;
2487
2.02M
            }
2488
2.30M
        }
2489
5.40M
    }
2490
2491
    /* Check the character which ends the tag. */
2492
2493
6.00M
    if (!CACHE(parser, 1)) goto error;
2494
2495
6.00M
    if (!IS_BLANKZ(parser->buffer)) {
2496
1.88M
        if (!parser->flow_level || !CHECK(parser->buffer, ',') ) {
2497
560
            yaml_parser_set_scanner_error(parser, "while scanning a tag",
2498
560
                    start_mark, "did not find expected whitespace or line break");
2499
560
            goto error;
2500
560
        }
2501
1.88M
    }
2502
2503
6.00M
    end_mark = parser->mark;
2504
2505
    /* Create a token. */
2506
2507
6.00M
    TAG_TOKEN_INIT(*token, handle, suffix, start_mark, end_mark);
2508
2509
6.00M
    return 1;
2510
2511
2.23k
error:
2512
2.23k
    yaml_free(handle);
2513
2.23k
    yaml_free(suffix);
2514
2.23k
    return 0;
2515
6.00M
}
2516
2517
/*
2518
 * Scan a tag handle.
2519
 */
2520
2521
static int
2522
yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
2523
        yaml_mark_t start_mark, yaml_char_t **handle)
2524
5.69M
{
2525
5.69M
    yaml_string_t string = NULL_STRING;
2526
2527
5.69M
    if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2528
2529
    /* Check the initial '!' character. */
2530
2531
5.69M
    if (!CACHE(parser, 1)) goto error;
2532
2533
5.69M
    if (!CHECK(parser->buffer, '!')) {
2534
382
        yaml_parser_set_scanner_error(parser, directive ?
2535
382
                "while scanning a tag directive" : "while scanning a tag",
2536
382
                start_mark, "did not find expected '!'");
2537
382
        goto error;
2538
382
    }
2539
2540
    /* Copy the '!' character. */
2541
2542
5.69M
    if (!READ(parser, string)) goto error;
2543
2544
    /* Copy all subsequent alphabetical and numerical characters. */
2545
2546
5.69M
    if (!CACHE(parser, 1)) goto error;
2547
2548
5.69M
    while (IS_ALPHA(parser->buffer))
2549
127M
    {
2550
127M
        if (!READ(parser, string)) goto error;
2551
127M
        if (!CACHE(parser, 1)) goto error;
2552
127M
    }
2553
2554
    /* Check if the trailing character is '!' and copy it. */
2555
2556
5.69M
    if (CHECK(parser->buffer, '!'))
2557
3.31M
    {
2558
3.31M
        if (!READ(parser, string)) goto error;
2559
3.31M
    }
2560
2.38M
    else
2561
2.38M
    {
2562
        /*
2563
         * It's either the '!' tag or not really a tag handle.  If it's a %TAG
2564
         * directive, it's an error.  If it's a tag token, it must be a part of
2565
         * URI.
2566
         */
2567
2568
2.38M
        if (directive && !(string.start[0] == '!' && string.start[1] == '\0')) {
2569
66
            yaml_parser_set_scanner_error(parser, "while parsing a tag directive",
2570
66
                    start_mark, "did not find expected '!'");
2571
66
            goto error;
2572
66
        }
2573
2.38M
    }
2574
2575
5.69M
    *handle = string.start;
2576
2577
5.69M
    return 1;
2578
2579
467
error:
2580
467
    STRING_DEL(parser, string);
2581
467
    return 0;
2582
5.69M
}
2583
2584
/*
2585
 * Scan a tag.
2586
 */
2587
2588
static int
2589
yaml_parser_scan_tag_uri(yaml_parser_t *parser, int uri_char, int directive,
2590
        yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri)
2591
6.29M
{
2592
6.29M
    size_t length = head ? strlen((char *)head) : 0;
2593
6.29M
    yaml_string_t string = NULL_STRING;
2594
2595
6.29M
    if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2596
2597
    /* Resize the string to include the head. */
2598
2599
6.31M
    while ((size_t)(string.end - string.start) <= length) {
2600
12.6k
        if (!yaml_string_extend(&string.start, &string.pointer, &string.end)) {
2601
0
            parser->error = YAML_MEMORY_ERROR;
2602
0
            goto error;
2603
0
        }
2604
12.6k
    }
2605
2606
    /*
2607
     * Copy the head if needed.
2608
     *
2609
     * Note that we don't copy the leading '!' character.
2610
     */
2611
2612
6.29M
    if (length > 1) {
2613
153k
        memcpy(string.start, head+1, length-1);
2614
153k
        string.pointer += length-1;
2615
153k
    }
2616
2617
    /* Scan the tag. */
2618
2619
6.29M
    if (!CACHE(parser, 1)) goto error;
2620
2621
    /*
2622
     * The set of characters that may appear in URI is as follows:
2623
     *
2624
     *      '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
2625
     *      '=', '+', '$', '.', '!', '~', '*', '\'', '(', ')', '%'.
2626
     *
2627
     * If we are inside a verbatim tag <...> (parameter uri_char is true)
2628
     * then also the following flow indicators are allowed:
2629
     *      ',', '[', ']'
2630
     */
2631
2632
210M
    while (IS_ALPHA(parser->buffer) || CHECK(parser->buffer, ';')
2633
144M
            || CHECK(parser->buffer, '/') || CHECK(parser->buffer, '?')
2634
143M
            || CHECK(parser->buffer, ':') || CHECK(parser->buffer, '@')
2635
141M
            || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '=')
2636
141M
            || CHECK(parser->buffer, '+') || CHECK(parser->buffer, '$')
2637
141M
            || CHECK(parser->buffer, '.') || CHECK(parser->buffer, '%')
2638
98.0M
            || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '~')
2639
11.1M
            || CHECK(parser->buffer, '*') || CHECK(parser->buffer, '\'')
2640
9.10M
            || CHECK(parser->buffer, '(') || CHECK(parser->buffer, ')')
2641
7.86M
            || (uri_char && (
2642
2.46M
                CHECK(parser->buffer, ',')
2643
1.54M
                || CHECK(parser->buffer, '[') || CHECK(parser->buffer, ']')
2644
2.46M
                )
2645
7.86M
            ))
2646
204M
    {
2647
        /* Check if it is a URI-escape sequence. */
2648
2649
204M
        if (CHECK(parser->buffer, '%')) {
2650
42.9M
            if (!STRING_EXTEND(parser, string))
2651
0
                goto error;
2652
2653
42.9M
            if (!yaml_parser_scan_uri_escapes(parser,
2654
42.9M
                        directive, start_mark, &string)) goto error;
2655
42.9M
        }
2656
161M
        else {
2657
161M
            if (!READ(parser, string)) goto error;
2658
161M
        }
2659
2660
204M
        length ++;
2661
204M
        if (!CACHE(parser, 1)) goto error;
2662
204M
    }
2663
2664
    /* Check if the tag is non-empty. */
2665
2666
6.29M
    if (!length) {
2667
611
        if (!STRING_EXTEND(parser, string))
2668
0
            goto error;
2669
2670
611
        yaml_parser_set_scanner_error(parser, directive ?
2671
311
                "while parsing a %TAG directive" : "while parsing a tag",
2672
611
                start_mark, "did not find expected tag URI");
2673
611
        goto error;
2674
611
    }
2675
2676
6.29M
    *uri = string.start;
2677
2678
6.29M
    return 1;
2679
2680
1.70k
error:
2681
1.70k
    STRING_DEL(parser, string);
2682
1.70k
    return 0;
2683
6.29M
}
2684
2685
/*
2686
 * Decode an URI-escape sequence corresponding to a single UTF-8 character.
2687
 */
2688
2689
static int
2690
yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
2691
        yaml_mark_t start_mark, yaml_string_t *string)
2692
42.9M
{
2693
42.9M
    int width = 0;
2694
2695
    /* Decode the required number of characters. */
2696
2697
43.3M
    do {
2698
2699
43.3M
        unsigned char octet = 0;
2700
2701
        /* Check for a URI-escaped octet. */
2702
2703
43.3M
        if (!CACHE(parser, 3)) return 0;
2704
2705
43.3M
        if (!(CHECK(parser->buffer, '%')
2706
43.3M
                    && IS_HEX_AT(parser->buffer, 1)
2707
43.3M
                    && IS_HEX_AT(parser->buffer, 2))) {
2708
941
            return yaml_parser_set_scanner_error(parser, directive ?
2709
925
                    "while parsing a %TAG directive" : "while parsing a tag",
2710
941
                    start_mark, "did not find URI escaped octet");
2711
941
        }
2712
2713
        /* Get the octet. */
2714
2715
43.3M
        octet = (AS_HEX_AT(parser->buffer, 1) << 4) + AS_HEX_AT(parser->buffer, 2);
2716
2717
        /* If it is the leading octet, determine the length of the UTF-8 sequence. */
2718
2719
43.3M
        if (!width)
2720
42.9M
        {
2721
42.9M
            width = (octet & 0x80) == 0x00 ? 1 :
2722
42.9M
                    (octet & 0xE0) == 0xC0 ? 2 :
2723
283k
                    (octet & 0xF0) == 0xE0 ? 3 :
2724
146k
                    (octet & 0xF8) == 0xF0 ? 4 : 0;
2725
42.9M
            if (!width) {
2726
69
                return yaml_parser_set_scanner_error(parser, directive ?
2727
67
                        "while parsing a %TAG directive" : "while parsing a tag",
2728
69
                        start_mark, "found an incorrect leading UTF-8 octet");
2729
69
            }
2730
42.9M
        }
2731
433k
        else
2732
433k
        {
2733
            /* Check if the trailing octet is correct. */
2734
2735
433k
            if ((octet & 0xC0) != 0x80) {
2736
51
                return yaml_parser_set_scanner_error(parser, directive ?
2737
50
                        "while parsing a %TAG directive" : "while parsing a tag",
2738
51
                        start_mark, "found an incorrect trailing UTF-8 octet");
2739
51
            }
2740
433k
        }
2741
2742
        /* Copy the octet and move the pointers. */
2743
2744
43.3M
        *(string->pointer++) = octet;
2745
43.3M
        SKIP(parser);
2746
43.3M
        SKIP(parser);
2747
43.3M
        SKIP(parser);
2748
2749
43.3M
    } while (--width);
2750
2751
42.9M
    return 1;
2752
42.9M
}
2753
2754
/*
2755
 * Scan a block scalar.
2756
 */
2757
2758
static int
2759
yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
2760
        int literal)
2761
809k
{
2762
809k
    yaml_mark_t start_mark;
2763
809k
    yaml_mark_t end_mark;
2764
809k
    yaml_string_t string = NULL_STRING;
2765
809k
    yaml_string_t leading_break = NULL_STRING;
2766
809k
    yaml_string_t trailing_breaks = NULL_STRING;
2767
809k
    int chomping = 0;
2768
809k
    int increment = 0;
2769
809k
    int indent = 0;
2770
809k
    int leading_blank = 0;
2771
809k
    int trailing_blank = 0;
2772
2773
809k
    if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2774
809k
    if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
2775
809k
    if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
2776
2777
    /* Eat the indicator '|' or '>'. */
2778
2779
809k
    start_mark = parser->mark;
2780
2781
809k
    SKIP(parser);
2782
2783
    /* Scan the additional block scalar indicators. */
2784
2785
809k
    if (!CACHE(parser, 1)) goto error;
2786
2787
    /* Check for a chomping indicator. */
2788
2789
809k
    if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-'))
2790
280k
    {
2791
        /* Set the chomping method and eat the indicator. */
2792
2793
280k
        chomping = CHECK(parser->buffer, '+') ? +1 : -1;
2794
2795
280k
        SKIP(parser);
2796
2797
        /* Check for an indentation indicator. */
2798
2799
280k
        if (!CACHE(parser, 1)) goto error;
2800
2801
280k
        if (IS_DIGIT(parser->buffer))
2802
197k
        {
2803
            /* Check that the indentation is greater than 0. */
2804
2805
197k
            if (CHECK(parser->buffer, '0')) {
2806
12
                yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2807
12
                        start_mark, "found an indentation indicator equal to 0");
2808
12
                goto error;
2809
12
            }
2810
2811
            /* Get the indentation level and eat the indicator. */
2812
2813
197k
            increment = AS_DIGIT(parser->buffer);
2814
2815
197k
            SKIP(parser);
2816
197k
        }
2817
280k
    }
2818
2819
    /* Do the same as above, but in the opposite order. */
2820
2821
528k
    else if (IS_DIGIT(parser->buffer))
2822
37.6k
    {
2823
37.6k
        if (CHECK(parser->buffer, '0')) {
2824
16
            yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2825
16
                    start_mark, "found an indentation indicator equal to 0");
2826
16
            goto error;
2827
16
        }
2828
2829
37.6k
        increment = AS_DIGIT(parser->buffer);
2830
2831
37.6k
        SKIP(parser);
2832
2833
37.6k
        if (!CACHE(parser, 1)) goto error;
2834
2835
37.6k
        if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-')) {
2836
27.1k
            chomping = CHECK(parser->buffer, '+') ? +1 : -1;
2837
2838
27.1k
            SKIP(parser);
2839
27.1k
        }
2840
37.6k
    }
2841
2842
    /* Eat whitespaces and comments to the end of the line. */
2843
2844
808k
    if (!CACHE(parser, 1)) goto error;
2845
2846
17.6M
    while (IS_BLANK(parser->buffer)) {
2847
17.6M
        SKIP(parser);
2848
17.6M
        if (!CACHE(parser, 1)) goto error;
2849
17.6M
    }
2850
2851
808k
    if (CHECK(parser->buffer, '#')) {
2852
42.9M
        while (!IS_BREAKZ(parser->buffer)) {
2853
42.6M
            SKIP(parser);
2854
42.6M
            if (!CACHE(parser, 1)) goto error;
2855
42.6M
        }
2856
259k
    }
2857
2858
    /* Check if we are at the end of the line. */
2859
2860
808k
    if (!IS_BREAKZ(parser->buffer)) {
2861
632
        yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2862
632
                start_mark, "did not find expected comment or line break");
2863
632
        goto error;
2864
632
    }
2865
2866
    /* Eat a line break. */
2867
2868
808k
    if (IS_BREAK(parser->buffer)) {
2869
805k
        if (!CACHE(parser, 2)) goto error;
2870
805k
        SKIP_LINE(parser);
2871
805k
    }
2872
2873
808k
    end_mark = parser->mark;
2874
2875
    /* Set the indentation level if it was specified. */
2876
2877
808k
    if (increment) {
2878
235k
        indent = parser->indent >= 0 ? parser->indent+increment : increment;
2879
235k
    }
2880
2881
    /* Scan the leading line breaks and determine the indentation level if needed. */
2882
2883
808k
    if (!yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks,
2884
808k
                start_mark, &end_mark)) goto error;
2885
2886
    /* Scan the block scalar content. */
2887
2888
808k
    if (!CACHE(parser, 1)) goto error;
2889
2890
3.42M
    while ((int)parser->mark.column == indent && !(IS_Z(parser->buffer)))
2891
2.61M
    {
2892
        /*
2893
         * We are at the beginning of a non-empty line.
2894
         */
2895
2896
        /* Is it a trailing whitespace? */
2897
2898
2.61M
        trailing_blank = IS_BLANK(parser->buffer);
2899
2900
        /* Check if we need to fold the leading line break. */
2901
2902
2.61M
        if (!literal && (*leading_break.start == '\n')
2903
1.44M
                && !leading_blank && !trailing_blank)
2904
1.22M
        {
2905
            /* Do we need to join the lines by space? */
2906
2907
1.22M
            if (*trailing_breaks.start == '\0') {
2908
1.17M
                if (!STRING_EXTEND(parser, string)) goto error;
2909
1.17M
                *(string.pointer ++) = ' ';
2910
1.17M
            }
2911
2912
1.22M
            CLEAR(parser, leading_break);
2913
1.22M
        }
2914
1.39M
        else {
2915
1.39M
            if (!JOIN(parser, string, leading_break)) goto error;
2916
1.39M
            CLEAR(parser, leading_break);
2917
1.39M
        }
2918
2919
        /* Append the remaining line breaks. */
2920
2921
2.61M
        if (!JOIN(parser, string, trailing_breaks)) goto error;
2922
2.61M
        CLEAR(parser, trailing_breaks);
2923
2924
        /* Is it a leading whitespace? */
2925
2926
2.61M
        leading_blank = IS_BLANK(parser->buffer);
2927
2928
        /* Consume the current line. */
2929
2930
70.5M
        while (!IS_BREAKZ(parser->buffer)) {
2931
67.9M
            if (!READ(parser, string)) goto error;
2932
67.9M
            if (!CACHE(parser, 1)) goto error;
2933
67.9M
        }
2934
2935
        /* Consume the line break. */
2936
2937
2.61M
        if (!CACHE(parser, 2)) goto error;
2938
2939
2.61M
        if (!READ_LINE(parser, leading_break)) goto error;
2940
2941
        /* Eat the following indentation spaces and line breaks. */
2942
2943
2.61M
        if (!yaml_parser_scan_block_scalar_breaks(parser,
2944
2.61M
                    &indent, &trailing_breaks, start_mark, &end_mark)) goto error;
2945
2.61M
    }
2946
2947
    /* Chomp the tail. */
2948
2949
808k
    if (chomping != -1) {
2950
567k
        if (!JOIN(parser, string, leading_break)) goto error;
2951
567k
    }
2952
808k
    if (chomping == 1) {
2953
67.1k
        if (!JOIN(parser, string, trailing_breaks)) goto error;
2954
67.1k
    }
2955
2956
    /* Create a token. */
2957
2958
808k
    SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
2959
808k
            literal ? YAML_LITERAL_SCALAR_STYLE : YAML_FOLDED_SCALAR_STYLE,
2960
808k
            start_mark, end_mark);
2961
2962
808k
    STRING_DEL(parser, leading_break);
2963
808k
    STRING_DEL(parser, trailing_breaks);
2964
2965
808k
    return 1;
2966
2967
823
error:
2968
823
    STRING_DEL(parser, string);
2969
823
    STRING_DEL(parser, leading_break);
2970
823
    STRING_DEL(parser, trailing_breaks);
2971
2972
823
    return 0;
2973
808k
}
2974
2975
/*
2976
 * Scan indentation spaces and line breaks for a block scalar.  Determine the
2977
 * indentation level if needed.
2978
 */
2979
2980
static int
2981
yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
2982
        int *indent, yaml_string_t *breaks,
2983
        yaml_mark_t start_mark, yaml_mark_t *end_mark)
2984
3.42M
{
2985
3.42M
    int max_indent = 0;
2986
2987
3.42M
    *end_mark = parser->mark;
2988
2989
    /* Eat the indentation spaces and line breaks. */
2990
2991
25.7M
    while (1)
2992
25.7M
    {
2993
        /* Eat the indentation spaces. */
2994
2995
25.7M
        if (!CACHE(parser, 1)) return 0;
2996
2997
103M
        while ((!*indent || (int)parser->mark.column < *indent)
2998
100M
                && IS_SPACE(parser->buffer)) {
2999
77.5M
            SKIP(parser);
3000
77.5M
            if (!CACHE(parser, 1)) return 0;
3001
77.5M
        }
3002
3003
25.7M
        if ((int)parser->mark.column > max_indent)
3004
2.62M
            max_indent = (int)parser->mark.column;
3005
3006
        /* Check for a tab character messing the indentation. */
3007
3008
25.7M
        if ((!*indent || (int)parser->mark.column < *indent)
3009
23.2M
                && IS_TAB(parser->buffer)) {
3010
24
            return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
3011
24
                    start_mark, "found a tab character where an indentation space is expected");
3012
24
        }
3013
3014
        /* Have we found a non-empty line? */
3015
3016
25.7M
        if (!IS_BREAK(parser->buffer)) break;
3017
3018
        /* Consume the line break. */
3019
3020
22.3M
        if (!CACHE(parser, 2)) return 0;
3021
22.3M
        if (!READ_LINE(parser, *breaks)) return 0;
3022
22.3M
        *end_mark = parser->mark;
3023
22.3M
    }
3024
3025
    /* Determine the indentation level if needed. */
3026
3027
3.42M
    if (!*indent) {
3028
573k
        *indent = max_indent;
3029
573k
        if (*indent < parser->indent + 1)
3030
500k
            *indent = parser->indent + 1;
3031
573k
        if (*indent < 1)
3032
13.3k
            *indent = 1;
3033
573k
    }
3034
3035
3.42M
   return 1;
3036
3.42M
}
3037
3038
/*
3039
 * Scan a quoted scalar.
3040
 */
3041
3042
static int
3043
yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
3044
        int single)
3045
6.39M
{
3046
6.39M
    yaml_mark_t start_mark;
3047
6.39M
    yaml_mark_t end_mark;
3048
6.39M
    yaml_string_t string = NULL_STRING;
3049
6.39M
    yaml_string_t leading_break = NULL_STRING;
3050
6.39M
    yaml_string_t trailing_breaks = NULL_STRING;
3051
6.39M
    yaml_string_t whitespaces = NULL_STRING;
3052
6.39M
    int leading_blanks;
3053
3054
6.39M
    if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3055
6.39M
    if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3056
6.39M
    if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3057
6.39M
    if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3058
3059
    /* Eat the left quote. */
3060
3061
6.39M
    start_mark = parser->mark;
3062
3063
6.39M
    SKIP(parser);
3064
3065
    /* Consume the content of the quoted scalar. */
3066
3067
32.7M
    while (1)
3068
32.7M
    {
3069
        /* Check that there are no document indicators at the beginning of the line. */
3070
3071
32.7M
        if (!CACHE(parser, 4)) goto error;
3072
3073
32.7M
        if (parser->mark.column == 0 &&
3074
3.36M
            ((CHECK_AT(parser->buffer, '-', 0) &&
3075
291k
              CHECK_AT(parser->buffer, '-', 1) &&
3076
81.0k
              CHECK_AT(parser->buffer, '-', 2)) ||
3077
3.29M
             (CHECK_AT(parser->buffer, '.', 0) &&
3078
32.0k
              CHECK_AT(parser->buffer, '.', 1) &&
3079
11.3k
              CHECK_AT(parser->buffer, '.', 2))) &&
3080
82.7k
            IS_BLANKZ_AT(parser->buffer, 3))
3081
216
        {
3082
216
            yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3083
216
                    start_mark, "found unexpected document indicator");
3084
216
            goto error;
3085
216
        }
3086
3087
        /* Check for EOF. */
3088
3089
32.7M
        if (IS_Z(parser->buffer)) {
3090
7.62k
            yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3091
7.62k
                    start_mark, "found unexpected end of stream");
3092
7.62k
            goto error;
3093
7.62k
        }
3094
3095
        /* Consume non-blank characters. */
3096
3097
32.7M
        if (!CACHE(parser, 2)) goto error;
3098
3099
32.7M
        leading_blanks = 0;
3100
3101
262M
        while (!IS_BLANKZ(parser->buffer))
3102
238M
        {
3103
            /* Check for an escaped single quote. */
3104
3105
238M
            if (single && CHECK_AT(parser->buffer, '\'', 0)
3106
2.63M
                    && CHECK_AT(parser->buffer, '\'', 1))
3107
931k
            {
3108
931k
                if (!STRING_EXTEND(parser, string)) goto error;
3109
931k
                *(string.pointer++) = '\'';
3110
931k
                SKIP(parser);
3111
931k
                SKIP(parser);
3112
931k
            }
3113
3114
            /* Check for the right quote. */
3115
3116
237M
            else if (CHECK(parser->buffer, single ? '\'' : '"'))
3117
6.38M
            {
3118
6.38M
                break;
3119
6.38M
            }
3120
3121
            /* Check for an escaped line break. */
3122
3123
231M
            else if (!single && CHECK(parser->buffer, '\\')
3124
35.4M
                    && IS_BREAK_AT(parser->buffer, 1))
3125
1.97M
            {
3126
1.97M
                if (!CACHE(parser, 3)) goto error;
3127
1.97M
                SKIP(parser);
3128
1.97M
                SKIP_LINE(parser);
3129
1.97M
                leading_blanks = 1;
3130
1.97M
                break;
3131
1.97M
            }
3132
3133
            /* Check for an escape sequence. */
3134
3135
229M
            else if (!single && CHECK(parser->buffer, '\\'))
3136
33.4M
            {
3137
33.4M
                size_t code_length = 0;
3138
3139
33.4M
                if (!STRING_EXTEND(parser, string)) goto error;
3140
3141
                /* Check the escape character. */
3142
3143
33.4M
                switch (parser->buffer.pointer[1])
3144
33.4M
                {
3145
240k
                    case '0':
3146
240k
                        *(string.pointer++) = '\0';
3147
240k
                        break;
3148
3149
2.52M
                    case 'a':
3150
2.52M
                        *(string.pointer++) = '\x07';
3151
2.52M
                        break;
3152
3153
546k
                    case 'b':
3154
546k
                        *(string.pointer++) = '\x08';
3155
546k
                        break;
3156
3157
2.65M
                    case 't':
3158
2.73M
                    case '\t':
3159
2.73M
                        *(string.pointer++) = '\x09';
3160
2.73M
                        break;
3161
3162
2.21M
                    case 'n':
3163
2.21M
                        *(string.pointer++) = '\x0A';
3164
2.21M
                        break;
3165
3166
176k
                    case 'v':
3167
176k
                        *(string.pointer++) = '\x0B';
3168
176k
                        break;
3169
3170
1.47M
                    case 'f':
3171
1.47M
                        *(string.pointer++) = '\x0C';
3172
1.47M
                        break;
3173
3174
3.26M
                    case 'r':
3175
3.26M
                        *(string.pointer++) = '\x0D';
3176
3.26M
                        break;
3177
3178
1.85M
                    case 'e':
3179
1.85M
                        *(string.pointer++) = '\x1B';
3180
1.85M
                        break;
3181
3182
385k
                    case ' ':
3183
385k
                        *(string.pointer++) = '\x20';
3184
385k
                        break;
3185
3186
1.08M
                    case '"':
3187
1.08M
                        *(string.pointer++) = '"';
3188
1.08M
                        break;
3189
3190
4.15k
                    case '/':
3191
4.15k
                        *(string.pointer++) = '/';
3192
4.15k
                        break;
3193
3194
3.07M
                    case '\\':
3195
3.07M
                        *(string.pointer++) = '\\';
3196
3.07M
                        break;
3197
3198
642k
                    case 'N':   /* NEL (#x85) */
3199
642k
                        *(string.pointer++) = '\xC2';
3200
642k
                        *(string.pointer++) = '\x85';
3201
642k
                        break;
3202
3203
19.9k
                    case '_':   /* #xA0 */
3204
19.9k
                        *(string.pointer++) = '\xC2';
3205
19.9k
                        *(string.pointer++) = '\xA0';
3206
19.9k
                        break;
3207
3208
1.52M
                    case 'L':   /* LS (#x2028) */
3209
1.52M
                        *(string.pointer++) = '\xE2';
3210
1.52M
                        *(string.pointer++) = '\x80';
3211
1.52M
                        *(string.pointer++) = '\xA8';
3212
1.52M
                        break;
3213
3214
308k
                    case 'P':   /* PS (#x2029) */
3215
308k
                        *(string.pointer++) = '\xE2';
3216
308k
                        *(string.pointer++) = '\x80';
3217
308k
                        *(string.pointer++) = '\xA9';
3218
308k
                        break;
3219
3220
523k
                    case 'x':
3221
523k
                        code_length = 2;
3222
523k
                        break;
3223
3224
10.5M
                    case 'u':
3225
10.5M
                        code_length = 4;
3226
10.5M
                        break;
3227
3228
354k
                    case 'U':
3229
354k
                        code_length = 8;
3230
354k
                        break;
3231
3232
287
                    default:
3233
287
                        yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3234
287
                                start_mark, "found unknown escape character");
3235
287
                        goto error;
3236
33.4M
                }
3237
3238
33.4M
                SKIP(parser);
3239
33.4M
                SKIP(parser);
3240
3241
                /* Consume an arbitrary escape code. */
3242
3243
33.4M
                if (code_length)
3244
11.4M
                {
3245
11.4M
                    unsigned int value = 0;
3246
11.4M
                    size_t k;
3247
3248
                    /* Scan the character value. */
3249
3250
11.4M
                    if (!CACHE(parser, code_length)) goto error;
3251
3252
57.5M
                    for (k = 0; k < code_length; k ++) {
3253
46.1M
                        if (!IS_HEX_AT(parser->buffer, k)) {
3254
531
                            yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3255
531
                                    start_mark, "did not find expected hexdecimal number");
3256
531
                            goto error;
3257
531
                        }
3258
46.1M
                        value = (value << 4) + AS_HEX_AT(parser->buffer, k);
3259
46.1M
                    }
3260
3261
                    /* Check the value and write the character. */
3262
3263
11.4M
                    if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
3264
365
                        yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3265
365
                                start_mark, "found invalid Unicode character escape code");
3266
365
                        goto error;
3267
365
                    }
3268
3269
11.4M
                    if (value <= 0x7F) {
3270
516k
                        *(string.pointer++) = value;
3271
516k
                    }
3272
10.9M
                    else if (value <= 0x7FF) {
3273
281k
                        *(string.pointer++) = 0xC0 + (value >> 6);
3274
281k
                        *(string.pointer++) = 0x80 + (value & 0x3F);
3275
281k
                    }
3276
10.6M
                    else if (value <= 0xFFFF) {
3277
10.5M
                        *(string.pointer++) = 0xE0 + (value >> 12);
3278
10.5M
                        *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3279
10.5M
                        *(string.pointer++) = 0x80 + (value & 0x3F);
3280
10.5M
                    }
3281
57.7k
                    else {
3282
57.7k
                        *(string.pointer++) = 0xF0 + (value >> 18);
3283
57.7k
                        *(string.pointer++) = 0x80 + ((value >> 12) & 0x3F);
3284
57.7k
                        *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3285
57.7k
                        *(string.pointer++) = 0x80 + (value & 0x3F);
3286
57.7k
                    }
3287
3288
                    /* Advance the pointer. */
3289
3290
57.5M
                    for (k = 0; k < code_length; k ++) {
3291
46.1M
                        SKIP(parser);
3292
46.1M
                    }
3293
11.4M
                }
3294
33.4M
            }
3295
3296
195M
            else
3297
195M
            {
3298
                /* It is a non-escaped non-blank character. */
3299
3300
195M
                if (!READ(parser, string)) goto error;
3301
195M
            }
3302
3303
230M
            if (!CACHE(parser, 2)) goto error;
3304
230M
        }
3305
3306
        /* Check if we are at the end of the scalar. */
3307
3308
        /* Fix for crash uninitialized value crash
3309
         * Credit for the bug and input is to OSS Fuzz
3310
         * Credit for the fix to Alex Gaynor
3311
         */
3312
32.7M
        if (!CACHE(parser, 1)) goto error;
3313
32.7M
        if (CHECK(parser->buffer, single ? '\'' : '"'))
3314
6.38M
            break;
3315
3316
        /* Consume blank characters. */
3317
3318
26.3M
        if (!CACHE(parser, 1)) goto error;
3319
3320
787M
        while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
3321
761M
        {
3322
761M
            if (IS_BLANK(parser->buffer))
3323
738M
            {
3324
                /* Consume a space or a tab character. */
3325
3326
738M
                if (!leading_blanks) {
3327
49.5M
                    if (!READ(parser, whitespaces)) goto error;
3328
49.5M
                }
3329
689M
                else {
3330
689M
                    SKIP(parser);
3331
689M
                }
3332
738M
            }
3333
22.7M
            else
3334
22.7M
            {
3335
22.7M
                if (!CACHE(parser, 2)) goto error;
3336
3337
                /* Check if it is a first line break. */
3338
3339
22.7M
                if (!leading_blanks)
3340
9.37M
                {
3341
9.37M
                    CLEAR(parser, whitespaces);
3342
9.37M
                    if (!READ_LINE(parser, leading_break)) goto error;
3343
9.37M
                    leading_blanks = 1;
3344
9.37M
                }
3345
13.3M
                else
3346
13.3M
                {
3347
13.3M
                    if (!READ_LINE(parser, trailing_breaks)) goto error;
3348
13.3M
                }
3349
22.7M
            }
3350
761M
            if (!CACHE(parser, 1)) goto error;
3351
761M
        }
3352
3353
        /* Join the whitespaces or fold line breaks. */
3354
3355
26.3M
        if (leading_blanks)
3356
11.3M
        {
3357
            /* Do we need to fold line breaks? */
3358
3359
11.3M
            if (leading_break.start[0] == '\n') {
3360
9.29M
                if (trailing_breaks.start[0] == '\0') {
3361
9.06M
                    if (!STRING_EXTEND(parser, string)) goto error;
3362
9.06M
                    *(string.pointer++) = ' ';
3363
9.06M
                }
3364
225k
                else {
3365
225k
                    if (!JOIN(parser, string, trailing_breaks)) goto error;
3366
225k
                    CLEAR(parser, trailing_breaks);
3367
225k
                }
3368
9.29M
                CLEAR(parser, leading_break);
3369
9.29M
            }
3370
2.06M
            else {
3371
2.06M
                if (!JOIN(parser, string, leading_break)) goto error;
3372
2.06M
                if (!JOIN(parser, string, trailing_breaks)) goto error;
3373
2.06M
                CLEAR(parser, leading_break);
3374
2.06M
                CLEAR(parser, trailing_breaks);
3375
2.06M
            }
3376
11.3M
        }
3377
14.9M
        else
3378
14.9M
        {
3379
14.9M
            if (!JOIN(parser, string, whitespaces)) goto error;
3380
14.9M
            CLEAR(parser, whitespaces);
3381
14.9M
        }
3382
26.3M
    }
3383
3384
    /* Eat the right quote. */
3385
3386
6.38M
    SKIP(parser);
3387
3388
6.38M
    end_mark = parser->mark;
3389
3390
    /* Create a token. */
3391
3392
6.38M
    SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3393
6.38M
            single ? YAML_SINGLE_QUOTED_SCALAR_STYLE : YAML_DOUBLE_QUOTED_SCALAR_STYLE,
3394
6.38M
            start_mark, end_mark);
3395
3396
6.38M
    STRING_DEL(parser, leading_break);
3397
6.38M
    STRING_DEL(parser, trailing_breaks);
3398
6.38M
    STRING_DEL(parser, whitespaces);
3399
3400
6.38M
    return 1;
3401
3402
9.16k
error:
3403
9.16k
    STRING_DEL(parser, string);
3404
9.16k
    STRING_DEL(parser, leading_break);
3405
9.16k
    STRING_DEL(parser, trailing_breaks);
3406
9.16k
    STRING_DEL(parser, whitespaces);
3407
3408
9.16k
    return 0;
3409
6.39M
}
3410
3411
/*
3412
 * Scan a plain scalar.
3413
 */
3414
3415
static int
3416
yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token)
3417
17.7M
{
3418
17.7M
    yaml_mark_t start_mark;
3419
17.7M
    yaml_mark_t end_mark;
3420
17.7M
    yaml_string_t string = NULL_STRING;
3421
17.7M
    yaml_string_t leading_break = NULL_STRING;
3422
17.7M
    yaml_string_t trailing_breaks = NULL_STRING;
3423
17.7M
    yaml_string_t whitespaces = NULL_STRING;
3424
17.7M
    int leading_blanks = 0;
3425
17.7M
    int indent = parser->indent+1;
3426
3427
17.7M
    if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3428
17.7M
    if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3429
17.7M
    if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3430
17.7M
    if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3431
3432
17.7M
    start_mark = end_mark = parser->mark;
3433
3434
    /* Consume the content of the plain scalar. */
3435
3436
32.6M
    while (1)
3437
32.6M
    {
3438
        /* Check for a document indicator. */
3439
3440
32.6M
        if (!CACHE(parser, 4)) goto error;
3441
3442
32.6M
        if (parser->mark.column == 0 &&
3443
1.78M
            ((CHECK_AT(parser->buffer, '-', 0) &&
3444
478k
              CHECK_AT(parser->buffer, '-', 1) &&
3445
273k
              CHECK_AT(parser->buffer, '-', 2)) ||
3446
1.57M
             (CHECK_AT(parser->buffer, '.', 0) &&
3447
66.9k
              CHECK_AT(parser->buffer, '.', 1) &&
3448
23.0k
              CHECK_AT(parser->buffer, '.', 2))) &&
3449
219k
            IS_BLANKZ_AT(parser->buffer, 3)) break;
3450
3451
        /* Check for a comment. */
3452
3453
32.5M
        if (CHECK(parser->buffer, '#'))
3454
12.0k
            break;
3455
3456
        /* Consume non-blank characters. */
3457
3458
214M
        while (!IS_BLANKZ(parser->buffer))
3459
199M
        {
3460
            /* Check for "x:" + one of ',?[]{}' in the flow context. TODO: Fix the test "spec-08-13".
3461
             * This is not completely according to the spec
3462
             * See http://yaml.org/spec/1.1/#id907281 9.1.3. Plain
3463
             */
3464
3465
199M
            if (parser->flow_level
3466
44.6M
                    && CHECK(parser->buffer, ':')
3467
1.55M
                    && (
3468
1.55M
                        CHECK_AT(parser->buffer, ',', 1)
3469
1.55M
                        || CHECK_AT(parser->buffer, '?', 1)
3470
1.55M
                        || CHECK_AT(parser->buffer, '[', 1)
3471
1.55M
                        || CHECK_AT(parser->buffer, ']', 1)
3472
1.55M
                        || CHECK_AT(parser->buffer, '{', 1)
3473
1.55M
                        || CHECK_AT(parser->buffer, '}', 1)
3474
1.55M
                    )
3475
199M
                    ) {
3476
63
                yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3477
63
                        start_mark, "found unexpected ':'");
3478
63
                goto error;
3479
63
            }
3480
3481
            /* Check for indicators that may end a plain scalar. */
3482
3483
199M
            if ((CHECK(parser->buffer, ':') && IS_BLANKZ_AT(parser->buffer, 1))
3484
194M
                    || (parser->flow_level &&
3485
43.8M
                        (CHECK(parser->buffer, ',')
3486
31.3M
                         || CHECK(parser->buffer, '[')
3487
31.3M
                         || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{')
3488
31.2M
                         || CHECK(parser->buffer, '}'))))
3489
17.2M
                break;
3490
3491
            /* Check if we need to join whitespaces and breaks. */
3492
3493
182M
            if (leading_blanks || whitespaces.start != whitespaces.pointer)
3494
14.7M
            {
3495
14.7M
                if (leading_blanks)
3496
1.77M
                {
3497
                    /* Do we need to fold line breaks? */
3498
3499
1.77M
                    if (leading_break.start[0] == '\n') {
3500
1.59M
                        if (trailing_breaks.start[0] == '\0') {
3501
1.26M
                            if (!STRING_EXTEND(parser, string)) goto error;
3502
1.26M
                            *(string.pointer++) = ' ';
3503
1.26M
                        }
3504
326k
                        else {
3505
326k
                            if (!JOIN(parser, string, trailing_breaks)) goto error;
3506
326k
                            CLEAR(parser, trailing_breaks);
3507
326k
                        }
3508
1.59M
                        CLEAR(parser, leading_break);
3509
1.59M
                    }
3510
185k
                    else {
3511
185k
                        if (!JOIN(parser, string, leading_break)) goto error;
3512
185k
                        if (!JOIN(parser, string, trailing_breaks)) goto error;
3513
185k
                        CLEAR(parser, leading_break);
3514
185k
                        CLEAR(parser, trailing_breaks);
3515
185k
                    }
3516
3517
1.77M
                    leading_blanks = 0;
3518
1.77M
                }
3519
12.9M
                else
3520
12.9M
                {
3521
12.9M
                    if (!JOIN(parser, string, whitespaces)) goto error;
3522
12.9M
                    CLEAR(parser, whitespaces);
3523
12.9M
                }
3524
14.7M
            }
3525
3526
            /* Copy the character. */
3527
3528
182M
            if (!READ(parser, string)) goto error;
3529
3530
182M
            end_mark = parser->mark;
3531
3532
182M
            if (!CACHE(parser, 2)) goto error;
3533
182M
        }
3534
3535
        /* Is it the end? */
3536
3537
32.5M
        if (!(IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer)))
3538
17.2M
            break;
3539
3540
        /* Consume blank characters. */
3541
3542
15.3M
        if (!CACHE(parser, 1)) goto error;
3543
3544
177M
        while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
3545
161M
        {
3546
161M
            if (IS_BLANK(parser->buffer))
3547
133M
            {
3548
                /* Check for tab characters that abuse indentation. */
3549
3550
133M
                if (leading_blanks && (int)parser->mark.column < indent
3551
84.1M
                        && IS_TAB(parser->buffer)) {
3552
17
                    yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3553
17
                            start_mark, "found a tab character that violates indentation");
3554
17
                    goto error;
3555
17
                }
3556
3557
                /* Consume a space or a tab character. */
3558
3559
133M
                if (!leading_blanks) {
3560
35.6M
                    if (!READ(parser, whitespaces)) goto error;
3561
35.6M
                }
3562
98.0M
                else {
3563
98.0M
                    SKIP(parser);
3564
98.0M
                }
3565
133M
            }
3566
28.2M
            else
3567
28.2M
            {
3568
28.2M
                if (!CACHE(parser, 2)) goto error;
3569
3570
                /* Check if it is a first line break. */
3571
3572
28.2M
                if (!leading_blanks)
3573
2.31M
                {
3574
2.31M
                    CLEAR(parser, whitespaces);
3575
2.31M
                    if (!READ_LINE(parser, leading_break)) goto error;
3576
2.31M
                    leading_blanks = 1;
3577
2.31M
                }
3578
25.9M
                else
3579
25.9M
                {
3580
25.9M
                    if (!READ_LINE(parser, trailing_breaks)) goto error;
3581
25.9M
                }
3582
28.2M
            }
3583
161M
            if (!CACHE(parser, 1)) goto error;
3584
161M
        }
3585
3586
        /* Check indentation level. */
3587
3588
15.3M
        if (!parser->flow_level && (int)parser->mark.column < indent)
3589
389k
            break;
3590
15.3M
    }
3591
3592
    /* Create a token. */
3593
3594
17.7M
    SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3595
17.7M
            YAML_PLAIN_SCALAR_STYLE, start_mark, end_mark);
3596
3597
    /* Note that we change the 'simple_key_allowed' flag. */
3598
3599
17.7M
    if (leading_blanks) {
3600
538k
        parser->simple_key_allowed = 1;
3601
538k
    }
3602
3603
17.7M
    STRING_DEL(parser, leading_break);
3604
17.7M
    STRING_DEL(parser, trailing_breaks);
3605
17.7M
    STRING_DEL(parser, whitespaces);
3606
3607
17.7M
    return 1;
3608
3609
465
error:
3610
465
    STRING_DEL(parser, string);
3611
465
    STRING_DEL(parser, leading_break);
3612
465
    STRING_DEL(parser, trailing_breaks);
3613
465
    STRING_DEL(parser, whitespaces);
3614
3615
465
    return 0;
3616
17.7M
}