Coverage Report

Created: 2026-12-01 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libyaml/src/emitter.c
Line
Count
Source
1
2
#include "yaml_private.h"
3
4
/*
5
 * Flush the buffer if needed.
6
 */
7
8
#define FLUSH(emitter)                                                          \
9
5.13G
    ((emitter->buffer.pointer+5 < emitter->buffer.end)                          \
10
5.13G
     || yaml_emitter_flush(emitter))
11
12
/*
13
 * Put a character to the output buffer.
14
 */
15
16
#define PUT(emitter,value)                                                      \
17
2.49G
    (FLUSH(emitter)                                                             \
18
2.49G
     && (*(emitter->buffer.pointer++) = (yaml_char_t)(value),                   \
19
2.49G
         emitter->column++,                                               \
20
2.49G
         1))
21
22
/*
23
 * Put a line break to the output buffer.
24
 */
25
26
#define PUT_BREAK(emitter)                                                      \
27
6.92M
    (FLUSH(emitter)                                                             \
28
6.92M
     && ((emitter->line_break == YAML_CR_BREAK ?                                \
29
6.92M
             (*(emitter->buffer.pointer++) = (yaml_char_t) '\r') :              \
30
6.92M
          emitter->line_break == YAML_LN_BREAK ?                                \
31
6.92M
             (*(emitter->buffer.pointer++) = (yaml_char_t) '\n') :              \
32
6.92M
          emitter->line_break == YAML_CRLN_BREAK ?                              \
33
0
             (*(emitter->buffer.pointer++) = (yaml_char_t) '\r',                \
34
0
              *(emitter->buffer.pointer++) = (yaml_char_t) '\n') : 0),          \
35
6.92M
         emitter->column = 0,                                                   \
36
6.92M
         emitter->line ++,                                                      \
37
6.92M
         1))
38
39
/*
40
 * Copy a character from a string into buffer.
41
 */
42
43
#define WRITE(emitter,string)                                                   \
44
61.0M
    (FLUSH(emitter)                                                             \
45
61.0M
     && (COPY(emitter->buffer,string),                                          \
46
61.0M
         emitter->column ++,                                                    \
47
61.0M
         1))
48
49
/*
50
 * Copy a line break character from a string into buffer.
51
 */
52
53
#define WRITE_BREAK(emitter,string)                                             \
54
4.78M
    (FLUSH(emitter)                                                             \
55
4.78M
     && (CHECK(string,'\n') ?                                                   \
56
4.78M
         (PUT_BREAK(emitter),                                                   \
57
4.51M
          string.pointer ++,                                                    \
58
4.51M
          1) :                                                                  \
59
4.78M
         (COPY(emitter->buffer,string),                                         \
60
269k
          emitter->column = 0,                                                  \
61
269k
          emitter->line ++,                                                     \
62
269k
          1)))
63
64
/*
65
 * API functions.
66
 */
67
68
YAML_DECLARE(int)
69
yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event);
70
71
/*
72
 * Utility functions.
73
 */
74
75
static int
76
yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem);
77
78
static int
79
yaml_emitter_need_more_events(yaml_emitter_t *emitter);
80
81
static int
82
yaml_emitter_append_tag_directive(yaml_emitter_t *emitter,
83
        yaml_tag_directive_t value, int allow_duplicates);
84
85
static int
86
yaml_emitter_increase_indent(yaml_emitter_t *emitter,
87
        int flow, int indentless);
88
89
/*
90
 * State functions.
91
 */
92
93
static int
94
yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event);
95
96
static int
97
yaml_emitter_emit_stream_start(yaml_emitter_t *emitter,
98
        yaml_event_t *event);
99
100
static int
101
yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
102
        yaml_event_t *event, int first);
103
104
static int
105
yaml_emitter_emit_document_content(yaml_emitter_t *emitter,
106
        yaml_event_t *event);
107
108
static int
109
yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
110
        yaml_event_t *event);
111
112
static int
113
yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
114
        yaml_event_t *event, int first);
115
116
static int
117
yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
118
        yaml_event_t *event, int first);
119
120
static int
121
yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
122
        yaml_event_t *event, int simple);
123
124
static int
125
yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
126
        yaml_event_t *event, int first);
127
128
static int
129
yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
130
        yaml_event_t *event, int first);
131
132
static int
133
yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
134
        yaml_event_t *event, int simple);
135
136
static int
137
yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
138
        int root, int sequence, int mapping, int simple_key);
139
140
static int
141
yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event);
142
143
static int
144
yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event);
145
146
static int
147
yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event);
148
149
static int
150
yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event);
151
152
/*
153
 * Checkers.
154
 */
155
156
static int
157
yaml_emitter_check_empty_document(yaml_emitter_t *emitter);
158
159
static int
160
yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter);
161
162
static int
163
yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter);
164
165
static int
166
yaml_emitter_check_simple_key(yaml_emitter_t *emitter);
167
168
static int
169
yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event);
170
171
/*
172
 * Processors.
173
 */
174
175
static int
176
yaml_emitter_process_anchor(yaml_emitter_t *emitter);
177
178
static int
179
yaml_emitter_process_tag(yaml_emitter_t *emitter);
180
181
static int
182
yaml_emitter_process_scalar(yaml_emitter_t *emitter);
183
184
/*
185
 * Analyzers.
186
 */
187
188
static int
189
yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
190
        yaml_version_directive_t version_directive);
191
192
static int
193
yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
194
        yaml_tag_directive_t tag_directive);
195
196
static int
197
yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
198
        yaml_char_t *anchor, int alias);
199
200
static int
201
yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
202
        yaml_char_t *tag);
203
204
static int
205
yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
206
        yaml_char_t *value, size_t length);
207
208
static int
209
yaml_emitter_analyze_event(yaml_emitter_t *emitter,
210
        yaml_event_t *event);
211
212
/*
213
 * Writers.
214
 */
215
216
static int
217
yaml_emitter_write_bom(yaml_emitter_t *emitter);
218
219
static int
220
yaml_emitter_write_indent(yaml_emitter_t *emitter);
221
222
static int
223
yaml_emitter_write_indicator(yaml_emitter_t *emitter,
224
        const char *indicator, int need_whitespace,
225
        int is_whitespace, int is_indention);
226
227
static int
228
yaml_emitter_write_anchor(yaml_emitter_t *emitter,
229
        yaml_char_t *value, size_t length);
230
231
static int
232
yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
233
        yaml_char_t *value, size_t length);
234
235
static int
236
yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
237
        yaml_char_t *value, size_t length, int need_whitespace);
238
239
static int
240
yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
241
        yaml_char_t *value, size_t length, int allow_breaks);
242
243
static int
244
yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
245
        yaml_char_t *value, size_t length, int allow_breaks);
246
247
static int
248
yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
249
        yaml_char_t *value, size_t length, int allow_breaks);
250
251
static int
252
yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
253
        yaml_string_t string);
254
255
static int
256
yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
257
        yaml_char_t *value, size_t length);
258
259
static int
260
yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
261
        yaml_char_t *value, size_t length);
262
263
/*
264
 * Set an emitter error and return 0.
265
 */
266
267
static int
268
yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem)
269
5
{
270
5
    emitter->error = YAML_EMITTER_ERROR;
271
5
    emitter->problem = problem;
272
273
5
    return 0;
274
5
}
275
276
/*
277
 * Emit an event.
278
 */
279
280
YAML_DECLARE(int)
281
yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event)
282
556k
{
283
556k
    if (!ENQUEUE(emitter, emitter->events, *event)) {
284
0
        yaml_event_delete(event);
285
0
        return 0;
286
0
    }
287
288
1.10M
    while (!yaml_emitter_need_more_events(emitter)) {
289
554k
        if (!yaml_emitter_analyze_event(emitter, emitter->events.head))
290
3
            return 0;
291
554k
        if (!yaml_emitter_state_machine(emitter, emitter->events.head))
292
4.11k
            return 0;
293
550k
        yaml_event_delete(&DEQUEUE(emitter, emitter->events));
294
550k
    }
295
296
551k
    return 1;
297
556k
}
298
299
/*
300
 * Check if we need to accumulate more events before emitting.
301
 *
302
 * We accumulate extra
303
 *  - 1 event for DOCUMENT-START
304
 *  - 2 events for SEQUENCE-START
305
 *  - 3 events for MAPPING-START
306
 */
307
308
static int
309
yaml_emitter_need_more_events(yaml_emitter_t *emitter)
310
1.10M
{
311
1.10M
    int level = 0;
312
1.10M
    int accumulate = 0;
313
1.10M
    yaml_event_t *event;
314
315
1.10M
    if (QUEUE_EMPTY(emitter, emitter->events))
316
303k
        return 1;
317
318
802k
    switch (emitter->events.head->type) {
319
46.7k
        case YAML_DOCUMENT_START_EVENT:
320
46.7k
            accumulate = 1;
321
46.7k
            break;
322
176k
        case YAML_SEQUENCE_START_EVENT:
323
176k
            accumulate = 2;
324
176k
            break;
325
244k
        case YAML_MAPPING_START_EVENT:
326
244k
            accumulate = 3;
327
244k
            break;
328
334k
        default:
329
334k
            return 0;
330
802k
    }
331
332
467k
    if (emitter->events.tail - emitter->events.head > accumulate)
333
217k
        return 0;
334
335
809k
    for (event = emitter->events.head; event != emitter->events.tail; event ++) {
336
561k
        switch (event->type) {
337
0
            case YAML_STREAM_START_EVENT:
338
23.4k
            case YAML_DOCUMENT_START_EVENT:
339
206k
            case YAML_SEQUENCE_START_EVENT:
340
522k
            case YAML_MAPPING_START_EVENT:
341
522k
                level += 1;
342
522k
                break;
343
0
            case YAML_STREAM_END_EVENT:
344
0
            case YAML_DOCUMENT_END_EVENT:
345
834
            case YAML_SEQUENCE_END_EVENT:
346
2.24k
            case YAML_MAPPING_END_EVENT:
347
2.24k
                level -= 1;
348
2.24k
                break;
349
37.0k
            default:
350
37.0k
                break;
351
561k
        }
352
561k
        if (!level)
353
1.70k
            return 0;
354
561k
    }
355
356
248k
    return 1;
357
249k
}
358
359
/*
360
 * Append a directive to the directives stack.
361
 */
362
363
static int
364
yaml_emitter_append_tag_directive(yaml_emitter_t *emitter,
365
        yaml_tag_directive_t value, int allow_duplicates)
366
68.3k
{
367
68.3k
    yaml_tag_directive_t *tag_directive;
368
68.3k
    yaml_tag_directive_t copy = { NULL, NULL };
369
370
68.3k
    for (tag_directive = emitter->tag_directives.start;
371
251k
            tag_directive != emitter->tag_directives.top; tag_directive ++) {
372
187k
        if (strcmp((char *)value.handle, (char *)tag_directive->handle) == 0) {
373
4.07k
            if (allow_duplicates)
374
4.07k
                return 1;
375
0
            return yaml_emitter_set_emitter_error(emitter,
376
0
                    "duplicate %TAG directive");
377
4.07k
        }
378
187k
    }
379
380
64.2k
    copy.handle = yaml_strdup(value.handle);
381
64.2k
    copy.prefix = yaml_strdup(value.prefix);
382
64.2k
    if (!copy.handle || !copy.prefix) {
383
0
        emitter->error = YAML_MEMORY_ERROR;
384
0
        goto error;
385
0
    }
386
387
64.2k
    if (!PUSH(emitter, emitter->tag_directives, copy))
388
0
        goto error;
389
390
64.2k
    return 1;
391
392
0
error:
393
0
    yaml_free(copy.handle);
394
0
    yaml_free(copy.prefix);
395
0
    return 0;
396
64.2k
}
397
398
/*
399
 * Increase the indentation level.
400
 */
401
402
static int
403
yaml_emitter_increase_indent(yaml_emitter_t *emitter,
404
        int flow, int indentless)
405
364k
{
406
364k
    if (!PUSH(emitter, emitter->indents, emitter->indent))
407
0
        return 0;
408
409
364k
    if (emitter->indent < 0) {
410
22.7k
        emitter->indent = flow ? emitter->best_indent : 0;
411
22.7k
    }
412
341k
    else if (!indentless) {
413
341k
        emitter->indent += emitter->best_indent;
414
341k
    }
415
416
364k
    return 1;
417
364k
}
418
419
/*
420
 * State dispatcher.
421
 */
422
423
static int
424
yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event)
425
554k
{
426
554k
    switch (emitter->state)
427
554k
    {
428
10.5k
        case YAML_EMIT_STREAM_START_STATE:
429
10.5k
            return yaml_emitter_emit_stream_start(emitter, event);
430
431
8.47k
        case YAML_EMIT_FIRST_DOCUMENT_START_STATE:
432
8.47k
            return yaml_emitter_emit_document_start(emitter, event, 1);
433
434
16.9k
        case YAML_EMIT_DOCUMENT_START_STATE:
435
16.9k
            return yaml_emitter_emit_document_start(emitter, event, 0);
436
437
22.9k
        case YAML_EMIT_DOCUMENT_CONTENT_STATE:
438
22.9k
            return yaml_emitter_emit_document_content(emitter, event);
439
440
21.1k
        case YAML_EMIT_DOCUMENT_END_STATE:
441
21.1k
            return yaml_emitter_emit_document_end(emitter, event);
442
443
63.2k
        case YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE:
444
63.2k
            return yaml_emitter_emit_flow_sequence_item(emitter, event, 1);
445
446
53.9k
        case YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE:
447
53.9k
            return yaml_emitter_emit_flow_sequence_item(emitter, event, 0);
448
449
78.3k
        case YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE:
450
78.3k
            return yaml_emitter_emit_flow_mapping_key(emitter, event, 1);
451
452
56.1k
        case YAML_EMIT_FLOW_MAPPING_KEY_STATE:
453
56.1k
            return yaml_emitter_emit_flow_mapping_key(emitter, event, 0);
454
455
9.81k
        case YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE:
456
9.81k
            return yaml_emitter_emit_flow_mapping_value(emitter, event, 1);
457
458
47.6k
        case YAML_EMIT_FLOW_MAPPING_VALUE_STATE:
459
47.6k
            return yaml_emitter_emit_flow_mapping_value(emitter, event, 0);
460
461
27.7k
        case YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE:
462
27.7k
            return yaml_emitter_emit_block_sequence_item(emitter, event, 1);
463
464
30.7k
        case YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE:
465
30.7k
            return yaml_emitter_emit_block_sequence_item(emitter, event, 0);
466
467
26.8k
        case YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE:
468
26.8k
            return yaml_emitter_emit_block_mapping_key(emitter, event, 1);
469
470
39.8k
        case YAML_EMIT_BLOCK_MAPPING_KEY_STATE:
471
39.8k
            return yaml_emitter_emit_block_mapping_key(emitter, event, 0);
472
473
16.5k
        case YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE:
474
16.5k
            return yaml_emitter_emit_block_mapping_value(emitter, event, 1);
475
476
23.4k
        case YAML_EMIT_BLOCK_MAPPING_VALUE_STATE:
477
23.4k
            return yaml_emitter_emit_block_mapping_value(emitter, event, 0);
478
479
0
        case YAML_EMIT_END_STATE:
480
0
            return yaml_emitter_set_emitter_error(emitter,
481
0
                    "expected nothing after STREAM-END");
482
483
0
        default:
484
0
            assert(1);      /* Invalid state. */
485
554k
    }
486
487
0
    return 0;
488
554k
}
489
490
/*
491
 * Expect STREAM-START.
492
 */
493
494
static int
495
yaml_emitter_emit_stream_start(yaml_emitter_t *emitter,
496
        yaml_event_t *event)
497
10.5k
{
498
10.5k
    emitter->open_ended = 0;
499
10.5k
    if (event->type == YAML_STREAM_START_EVENT)
500
10.5k
    {
501
10.5k
        if (!emitter->encoding) {
502
10.5k
            emitter->encoding = event->data.stream_start.encoding;
503
10.5k
        }
504
505
10.5k
        if (!emitter->encoding) {
506
0
            emitter->encoding = YAML_UTF8_ENCODING;
507
0
        }
508
509
10.5k
        if (emitter->best_indent < 2 || emitter->best_indent > 9) {
510
10.5k
            emitter->best_indent  = 2;
511
10.5k
        }
512
513
10.5k
        if (emitter->best_width >= 0
514
10.5k
                && emitter->best_width <= emitter->best_indent*2) {
515
10.5k
            emitter->best_width = 80;
516
10.5k
        }
517
518
10.5k
        if (emitter->best_width < 0) {
519
0
            emitter->best_width = INT_MAX;
520
0
        }
521
522
10.5k
        if (!emitter->line_break) {
523
10.5k
            emitter->line_break = YAML_LN_BREAK;
524
10.5k
        }
525
526
10.5k
        emitter->indent = -1;
527
528
10.5k
        emitter->line = 0;
529
10.5k
        emitter->column = 0;
530
10.5k
        emitter->whitespace = 1;
531
10.5k
        emitter->indention = 1;
532
533
10.5k
        if (emitter->encoding != YAML_UTF8_ENCODING) {
534
591
            if (!yaml_emitter_write_bom(emitter))
535
0
                return 0;
536
591
        }
537
538
10.5k
        emitter->state = YAML_EMIT_FIRST_DOCUMENT_START_STATE;
539
540
10.5k
        return 1;
541
10.5k
    }
542
543
0
    return yaml_emitter_set_emitter_error(emitter,
544
0
            "expected STREAM-START");
545
10.5k
}
546
547
/*
548
 * Expect DOCUMENT-START or STREAM-END.
549
 */
550
551
static int
552
yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
553
        yaml_event_t *event, int first)
554
25.4k
{
555
25.4k
    if (event->type == YAML_DOCUMENT_START_EVENT)
556
23.2k
    {
557
23.2k
        yaml_tag_directive_t default_tag_directives[] = {
558
23.2k
            {(yaml_char_t *)"!", (yaml_char_t *)"!"},
559
23.2k
            {(yaml_char_t *)"!!", (yaml_char_t *)"tag:yaml.org,2002:"},
560
23.2k
            {NULL, NULL}
561
23.2k
        };
562
23.2k
        yaml_tag_directive_t *tag_directive;
563
23.2k
        int implicit;
564
565
23.2k
        if (event->data.document_start.version_directive) {
566
1.57k
            if (!yaml_emitter_analyze_version_directive(emitter,
567
1.57k
                        *event->data.document_start.version_directive))
568
0
                return 0;
569
1.57k
        }
570
571
23.2k
        for (tag_directive = event->data.document_start.tag_directives.start;
572
45.0k
                tag_directive != event->data.document_start.tag_directives.end;
573
23.2k
                tag_directive ++) {
574
21.7k
            if (!yaml_emitter_analyze_tag_directive(emitter, *tag_directive))
575
2
                return 0;
576
21.7k
            if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 0))
577
0
                return 0;
578
21.7k
        }
579
580
23.2k
        for (tag_directive = default_tag_directives;
581
69.8k
                tag_directive->handle; tag_directive ++) {
582
46.5k
            if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 1))
583
0
                return 0;
584
46.5k
        }
585
586
23.2k
        implicit = event->data.document_start.implicit;
587
23.2k
        if (!first || emitter->canonical) {
588
18.5k
            implicit = 0;
589
18.5k
        }
590
591
23.2k
        if ((event->data.document_start.version_directive ||
592
21.7k
                    (event->data.document_start.tag_directives.start
593
21.7k
                     != event->data.document_start.tag_directives.end)) &&
594
6.31k
                emitter->open_ended)
595
5.76k
        {
596
5.76k
            if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
597
0
                return 0;
598
5.76k
            if (!yaml_emitter_write_indent(emitter))
599
0
                return 0;
600
5.76k
        }
601
23.2k
        emitter->open_ended = 0;
602
603
23.2k
        if (event->data.document_start.version_directive) {
604
1.57k
            implicit = 0;
605
1.57k
            if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0))
606
0
                return 0;
607
1.57k
            if (event->data.document_start.version_directive->minor == 1) {
608
1.00k
                if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0))
609
0
                    return 0;
610
1.00k
            }
611
570
            else {
612
570
                if (!yaml_emitter_write_indicator(emitter, "1.2", 1, 0, 0))
613
0
                    return 0;
614
570
            }
615
1.57k
            if (!yaml_emitter_write_indent(emitter))
616
0
                return 0;
617
1.57k
        }
618
619
23.2k
        if (event->data.document_start.tag_directives.start
620
23.2k
                != event->data.document_start.tag_directives.end) {
621
4.74k
            implicit = 0;
622
4.74k
            for (tag_directive = event->data.document_start.tag_directives.start;
623
26.5k
                    tag_directive != event->data.document_start.tag_directives.end;
624
21.7k
                    tag_directive ++) {
625
21.7k
                if (!yaml_emitter_write_indicator(emitter, "%TAG", 1, 0, 0))
626
0
                    return 0;
627
21.7k
                if (!yaml_emitter_write_tag_handle(emitter, tag_directive->handle,
628
21.7k
                            strlen((char *)tag_directive->handle)))
629
0
                    return 0;
630
21.7k
                if (!yaml_emitter_write_tag_content(emitter, tag_directive->prefix,
631
21.7k
                            strlen((char *)tag_directive->prefix), 1))
632
0
                    return 0;
633
21.7k
                if (!yaml_emitter_write_indent(emitter))
634
0
                    return 0;
635
21.7k
            }
636
4.74k
        }
637
638
23.2k
        if (yaml_emitter_check_empty_document(emitter)) {
639
0
            implicit = 0;
640
0
        }
641
642
23.2k
        if (!implicit) {
643
18.7k
            if (!yaml_emitter_write_indent(emitter))
644
0
                return 0;
645
18.7k
            if (!yaml_emitter_write_indicator(emitter, "---", 1, 0, 0))
646
0
                return 0;
647
18.7k
            if (emitter->canonical) {
648
15.4k
                if (!yaml_emitter_write_indent(emitter))
649
0
                    return 0;
650
15.4k
            }
651
18.7k
        }
652
653
23.2k
        emitter->state = YAML_EMIT_DOCUMENT_CONTENT_STATE;
654
655
23.2k
        emitter->open_ended = 0;
656
23.2k
        return 1;
657
23.2k
    }
658
659
2.15k
    else if (event->type == YAML_STREAM_END_EVENT)
660
2.15k
    {
661
662
        /**
663
         * This can happen if a block scalar with trailing empty lines
664
         * is at the end of the stream
665
         */
666
2.15k
        if (emitter->open_ended == 2)
667
58
        {
668
58
            if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
669
0
                return 0;
670
58
            emitter->open_ended = 0;
671
58
            if (!yaml_emitter_write_indent(emitter))
672
0
                return 0;
673
58
        }
674
2.15k
        if (!yaml_emitter_flush(emitter))
675
30
            return 0;
676
677
2.12k
        emitter->state = YAML_EMIT_END_STATE;
678
679
2.12k
        return 1;
680
2.15k
    }
681
682
0
    return yaml_emitter_set_emitter_error(emitter,
683
0
            "expected DOCUMENT-START or STREAM-END");
684
25.4k
}
685
686
/*
687
 * Expect the root node.
688
 */
689
690
static int
691
yaml_emitter_emit_document_content(yaml_emitter_t *emitter,
692
        yaml_event_t *event)
693
22.9k
{
694
22.9k
    if (!PUSH(emitter, emitter->states, YAML_EMIT_DOCUMENT_END_STATE))
695
0
        return 0;
696
697
22.9k
    return yaml_emitter_emit_node(emitter, event, 1, 0, 0, 0);
698
22.9k
}
699
700
/*
701
 * Expect DOCUMENT-END.
702
 */
703
704
static int
705
yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
706
        yaml_event_t *event)
707
21.1k
{
708
21.1k
    if (event->type == YAML_DOCUMENT_END_EVENT)
709
21.1k
    {
710
21.1k
        if (!yaml_emitter_write_indent(emitter))
711
0
            return 0;
712
21.1k
        if (!event->data.document_end.implicit) {
713
770
            if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
714
0
                return 0;
715
770
            emitter->open_ended = 0;
716
770
            if (!yaml_emitter_write_indent(emitter))
717
0
                return 0;
718
770
        }
719
20.4k
        else if (!emitter->open_ended)
720
19.8k
            emitter->open_ended = 1;
721
21.1k
        if (!yaml_emitter_flush(emitter))
722
4.08k
            return 0;
723
724
17.0k
        emitter->state = YAML_EMIT_DOCUMENT_START_STATE;
725
726
68.5k
        while (!STACK_EMPTY(emitter, emitter->tag_directives)) {
727
51.4k
            yaml_tag_directive_t tag_directive = POP(emitter,
728
51.4k
                    emitter->tag_directives);
729
51.4k
            yaml_free(tag_directive.handle);
730
51.4k
            yaml_free(tag_directive.prefix);
731
51.4k
        }
732
733
17.0k
        return 1;
734
21.1k
    }
735
736
0
    return yaml_emitter_set_emitter_error(emitter,
737
0
            "expected DOCUMENT-END");
738
21.1k
}
739
740
/*
741
 *
742
 * Expect a flow item node.
743
 */
744
745
static int
746
yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
747
        yaml_event_t *event, int first)
748
117k
{
749
117k
    if (first)
750
63.2k
    {
751
63.2k
        if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0))
752
0
            return 0;
753
63.2k
        if (!yaml_emitter_increase_indent(emitter, 1, 0))
754
0
            return 0;
755
63.2k
        emitter->flow_level ++;
756
63.2k
    }
757
758
117k
    if (event->type == YAML_SEQUENCE_END_EVENT)
759
43.7k
    {
760
43.7k
        emitter->flow_level --;
761
43.7k
        emitter->indent = POP(emitter, emitter->indents);
762
43.7k
        if (emitter->canonical && !first) {
763
41.3k
            if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
764
0
                return 0;
765
41.3k
            if (!yaml_emitter_write_indent(emitter))
766
0
                return 0;
767
41.3k
        }
768
43.7k
        if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0))
769
0
            return 0;
770
43.7k
        emitter->state = POP(emitter, emitter->states);
771
772
43.7k
        return 1;
773
43.7k
    }
774
775
73.4k
    if (!first) {
776
11.0k
        if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
777
0
            return 0;
778
11.0k
    }
779
780
73.4k
    if (emitter->canonical || emitter->column > emitter->best_width) {
781
65.9k
        if (!yaml_emitter_write_indent(emitter))
782
0
            return 0;
783
65.9k
    }
784
73.4k
    if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE))
785
0
        return 0;
786
787
73.4k
    return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
788
73.4k
}
789
790
/*
791
 * Expect a flow key node.
792
 */
793
794
static int
795
yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
796
        yaml_event_t *event, int first)
797
134k
{
798
134k
    if (first)
799
78.3k
    {
800
78.3k
        if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0))
801
0
            return 0;
802
78.3k
        if (!yaml_emitter_increase_indent(emitter, 1, 0))
803
0
            return 0;
804
78.3k
        emitter->flow_level ++;
805
78.3k
    }
806
807
134k
    if (event->type == YAML_MAPPING_END_EVENT)
808
38.0k
    {
809
38.0k
        emitter->flow_level --;
810
38.0k
        emitter->indent = POP(emitter, emitter->indents);
811
38.0k
        if (emitter->canonical && !first) {
812
32.5k
            if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
813
0
                return 0;
814
32.5k
            if (!yaml_emitter_write_indent(emitter))
815
0
                return 0;
816
32.5k
        }
817
38.0k
        if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0))
818
0
            return 0;
819
38.0k
        emitter->state = POP(emitter, emitter->states);
820
821
38.0k
        return 1;
822
38.0k
    }
823
824
96.4k
    if (!first) {
825
19.1k
        if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
826
0
            return 0;
827
19.1k
    }
828
96.4k
    if (emitter->canonical || emitter->column > emitter->best_width) {
829
79.8k
        if (!yaml_emitter_write_indent(emitter))
830
0
            return 0;
831
79.8k
    }
832
833
96.4k
    if (!emitter->canonical && yaml_emitter_check_simple_key(emitter))
834
9.81k
    {
835
9.81k
        if (!PUSH(emitter, emitter->states,
836
9.81k
                    YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE))
837
0
            return 0;
838
839
9.81k
        return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
840
9.81k
    }
841
86.6k
    else
842
86.6k
    {
843
86.6k
        if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0))
844
0
            return 0;
845
86.6k
        if (!PUSH(emitter, emitter->states,
846
86.6k
                    YAML_EMIT_FLOW_MAPPING_VALUE_STATE))
847
0
            return 0;
848
849
86.6k
        return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
850
86.6k
    }
851
96.4k
}
852
853
/*
854
 * Expect a flow value node.
855
 */
856
857
static int
858
yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
859
        yaml_event_t *event, int simple)
860
57.4k
{
861
57.4k
    if (simple) {
862
9.81k
        if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
863
0
            return 0;
864
9.81k
    }
865
47.6k
    else {
866
47.6k
        if (emitter->canonical || emitter->column > emitter->best_width) {
867
44.8k
            if (!yaml_emitter_write_indent(emitter))
868
0
                return 0;
869
44.8k
        }
870
47.6k
        if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0))
871
0
            return 0;
872
47.6k
    }
873
57.4k
    if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE))
874
0
        return 0;
875
57.4k
    return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
876
57.4k
}
877
878
/*
879
 * Expect a block item node.
880
 */
881
882
static int
883
yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
884
        yaml_event_t *event, int first)
885
58.4k
{
886
58.4k
    if (first)
887
27.7k
    {
888
27.7k
        if (!yaml_emitter_increase_indent(emitter, 0,
889
27.7k
                    (emitter->mapping_context && !emitter->indention)))
890
0
            return 0;
891
27.7k
    }
892
893
58.4k
    if (event->type == YAML_SEQUENCE_END_EVENT)
894
25.3k
    {
895
25.3k
        emitter->indent = POP(emitter, emitter->indents);
896
25.3k
        emitter->state = POP(emitter, emitter->states);
897
898
25.3k
        return 1;
899
25.3k
    }
900
901
33.0k
    if (!yaml_emitter_write_indent(emitter))
902
0
        return 0;
903
33.0k
    if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1))
904
0
        return 0;
905
33.0k
    if (!PUSH(emitter, emitter->states,
906
33.0k
                YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE))
907
0
        return 0;
908
909
33.0k
    return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
910
33.0k
}
911
912
/*
913
 * Expect a block key node.
914
 */
915
916
static int
917
yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
918
        yaml_event_t *event, int first)
919
66.7k
{
920
66.7k
    if (first)
921
26.8k
    {
922
26.8k
        if (!yaml_emitter_increase_indent(emitter, 0, 0))
923
0
            return 0;
924
26.8k
    }
925
926
66.7k
    if (event->type == YAML_MAPPING_END_EVENT)
927
24.0k
    {
928
24.0k
        emitter->indent = POP(emitter, emitter->indents);
929
24.0k
        emitter->state = POP(emitter, emitter->states);
930
931
24.0k
        return 1;
932
24.0k
    }
933
934
42.6k
    if (!yaml_emitter_write_indent(emitter))
935
0
        return 0;
936
937
42.6k
    if (yaml_emitter_check_simple_key(emitter))
938
16.6k
    {
939
16.6k
        if (!PUSH(emitter, emitter->states,
940
16.6k
                    YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE))
941
0
            return 0;
942
943
16.6k
        return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
944
16.6k
    }
945
25.9k
    else
946
25.9k
    {
947
25.9k
        if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1))
948
0
            return 0;
949
25.9k
        if (!PUSH(emitter, emitter->states,
950
25.9k
                    YAML_EMIT_BLOCK_MAPPING_VALUE_STATE))
951
0
            return 0;
952
953
25.9k
        return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
954
25.9k
    }
955
42.6k
}
956
957
/*
958
 * Expect a block value node.
959
 */
960
961
static int
962
yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
963
        yaml_event_t *event, int simple)
964
39.9k
{
965
39.9k
    if (simple) {
966
16.5k
        if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
967
0
            return 0;
968
16.5k
    }
969
23.4k
    else {
970
23.4k
        if (!yaml_emitter_write_indent(emitter))
971
0
            return 0;
972
23.4k
        if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1))
973
0
            return 0;
974
23.4k
    }
975
39.9k
    if (!PUSH(emitter, emitter->states,
976
39.9k
                YAML_EMIT_BLOCK_MAPPING_KEY_STATE))
977
0
        return 0;
978
979
39.9k
    return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
980
39.9k
}
981
982
/*
983
 * Expect a node.
984
 */
985
986
static int
987
yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
988
        int root, int sequence, int mapping, int simple_key)
989
365k
{
990
365k
    emitter->root_context = root;
991
365k
    emitter->sequence_context = sequence;
992
365k
    emitter->mapping_context = mapping;
993
365k
    emitter->simple_key_context = simple_key;
994
995
365k
    switch (event->type)
996
365k
    {
997
880
        case YAML_ALIAS_EVENT:
998
880
            return yaml_emitter_emit_alias(emitter, event);
999
1000
168k
        case YAML_SCALAR_EVENT:
1001
168k
            return yaml_emitter_emit_scalar(emitter, event);
1002
1003
91.1k
        case YAML_SEQUENCE_START_EVENT:
1004
91.1k
            return yaml_emitter_emit_sequence_start(emitter, event);
1005
1006
105k
        case YAML_MAPPING_START_EVENT:
1007
105k
            return yaml_emitter_emit_mapping_start(emitter, event);
1008
1009
0
        default:
1010
0
            return yaml_emitter_set_emitter_error(emitter,
1011
0
                    "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS");
1012
365k
    }
1013
1014
0
    return 0;
1015
365k
}
1016
1017
/*
1018
 * Expect ALIAS.
1019
 */
1020
1021
static int
1022
yaml_emitter_emit_alias(yaml_emitter_t *emitter, SHIM(yaml_event_t *event))
1023
880
{
1024
880
    if (!yaml_emitter_process_anchor(emitter))
1025
0
        return 0;
1026
880
    if (emitter->simple_key_context)
1027
516
        if (!PUT(emitter, ' ')) return 0;
1028
880
    emitter->state = POP(emitter, emitter->states);
1029
1030
880
    return 1;
1031
880
}
1032
1033
/*
1034
 * Expect SCALAR.
1035
 */
1036
1037
static int
1038
yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event)
1039
168k
{
1040
168k
    if (!yaml_emitter_select_scalar_style(emitter, event))
1041
0
        return 0;
1042
168k
    if (!yaml_emitter_process_anchor(emitter))
1043
0
        return 0;
1044
168k
    if (!yaml_emitter_process_tag(emitter))
1045
0
        return 0;
1046
168k
    if (!yaml_emitter_increase_indent(emitter, 1, 0))
1047
0
        return 0;
1048
168k
    if (!yaml_emitter_process_scalar(emitter))
1049
0
        return 0;
1050
168k
    emitter->indent = POP(emitter, emitter->indents);
1051
168k
    emitter->state = POP(emitter, emitter->states);
1052
1053
168k
    return 1;
1054
168k
}
1055
1056
/*
1057
 * Expect SEQUENCE-START.
1058
 */
1059
1060
static int
1061
yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event)
1062
91.1k
{
1063
91.1k
    if (!yaml_emitter_process_anchor(emitter))
1064
0
        return 0;
1065
91.1k
    if (!yaml_emitter_process_tag(emitter))
1066
0
        return 0;
1067
1068
91.1k
    if (emitter->flow_level || emitter->canonical
1069
28.2k
            || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE
1070
63.3k
            || yaml_emitter_check_empty_sequence(emitter)) {
1071
63.3k
        emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE;
1072
63.3k
    }
1073
27.7k
    else {
1074
27.7k
        emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE;
1075
27.7k
    }
1076
1077
91.1k
    return 1;
1078
91.1k
}
1079
1080
/*
1081
 * Expect MAPPING-START.
1082
 */
1083
1084
static int
1085
yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event)
1086
105k
{
1087
105k
    if (!yaml_emitter_process_anchor(emitter))
1088
0
        return 0;
1089
105k
    if (!yaml_emitter_process_tag(emitter))
1090
0
        return 0;
1091
1092
105k
    if (emitter->flow_level || emitter->canonical
1093
27.8k
            || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE
1094
78.4k
            || yaml_emitter_check_empty_mapping(emitter)) {
1095
78.4k
        emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE;
1096
78.4k
    }
1097
26.8k
    else {
1098
26.8k
        emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE;
1099
26.8k
    }
1100
1101
105k
    return 1;
1102
105k
}
1103
1104
/*
1105
 * Check if the document content is an empty scalar.
1106
 */
1107
1108
static int
1109
yaml_emitter_check_empty_document(SHIM(yaml_emitter_t *emitter))
1110
23.2k
{
1111
23.2k
    return 0;
1112
23.2k
}
1113
1114
/*
1115
 * Check if the next events represent an empty sequence.
1116
 */
1117
1118
static int
1119
yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter)
1120
34.0k
{
1121
34.0k
    if (emitter->events.tail - emitter->events.head < 2)
1122
0
        return 0;
1123
1124
34.0k
    return (emitter->events.head[0].type == YAML_SEQUENCE_START_EVENT
1125
34.0k
            && emitter->events.head[1].type == YAML_SEQUENCE_END_EVENT);
1126
34.0k
}
1127
1128
/*
1129
 * Check if the next events represent an empty mapping.
1130
 */
1131
1132
static int
1133
yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter)
1134
62.4k
{
1135
62.4k
    if (emitter->events.tail - emitter->events.head < 2)
1136
0
        return 0;
1137
1138
62.4k
    return (emitter->events.head[0].type == YAML_MAPPING_START_EVENT
1139
62.4k
            && emitter->events.head[1].type == YAML_MAPPING_END_EVENT);
1140
62.4k
}
1141
1142
/*
1143
 * Check if the next node can be expressed as a simple key.
1144
 */
1145
1146
static int
1147
yaml_emitter_check_simple_key(yaml_emitter_t *emitter)
1148
71.1k
{
1149
71.1k
    yaml_event_t *event = emitter->events.head;
1150
71.1k
    size_t length = 0;
1151
1152
71.1k
    switch (event->type)
1153
71.1k
    {
1154
517
        case YAML_ALIAS_EVENT:
1155
517
            length += emitter->anchor_data.anchor_length;
1156
517
            break;
1157
1158
28.7k
        case YAML_SCALAR_EVENT:
1159
28.7k
            if (emitter->scalar_data.multiline)
1160
2.90k
                return 0;
1161
25.8k
            length += emitter->anchor_data.anchor_length
1162
25.8k
                + emitter->tag_data.handle_length
1163
25.8k
                + emitter->tag_data.suffix_length
1164
25.8k
                + emitter->scalar_data.length;
1165
25.8k
            break;
1166
1167
6.33k
        case YAML_SEQUENCE_START_EVENT:
1168
6.33k
            if (!yaml_emitter_check_empty_sequence(emitter))
1169
5.90k
                return 0;
1170
425
            length += emitter->anchor_data.anchor_length
1171
425
                + emitter->tag_data.handle_length
1172
425
                + emitter->tag_data.suffix_length;
1173
425
            break;
1174
1175
35.6k
        case YAML_MAPPING_START_EVENT:
1176
35.6k
            if (!yaml_emitter_check_empty_mapping(emitter))
1177
34.8k
                return 0;
1178
790
            length += emitter->anchor_data.anchor_length
1179
790
                + emitter->tag_data.handle_length
1180
790
                + emitter->tag_data.suffix_length;
1181
790
            break;
1182
1183
0
        default:
1184
0
            return 0;
1185
71.1k
    }
1186
1187
27.5k
    if (length > 128)
1188
1.05k
        return 0;
1189
1190
26.4k
    return 1;
1191
27.5k
}
1192
1193
/*
1194
 * Determine an acceptable scalar style.
1195
 */
1196
1197
static int
1198
yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event)
1199
168k
{
1200
168k
    yaml_scalar_style_t style = event->data.scalar.style;
1201
168k
    int no_tag = (!emitter->tag_data.handle && !emitter->tag_data.suffix);
1202
1203
168k
    if (no_tag && !event->data.scalar.plain_implicit
1204
17.9k
            && !event->data.scalar.quoted_implicit) {
1205
0
        return yaml_emitter_set_emitter_error(emitter,
1206
0
                "neither tag nor implicit flags are specified");
1207
0
    }
1208
1209
168k
    if (style == YAML_ANY_SCALAR_STYLE)
1210
0
        style = YAML_PLAIN_SCALAR_STYLE;
1211
1212
168k
    if (emitter->canonical)
1213
72.4k
        style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1214
1215
168k
    if (emitter->simple_key_context && emitter->scalar_data.multiline)
1216
0
        style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1217
1218
168k
    if (style == YAML_PLAIN_SCALAR_STYLE)
1219
82.0k
    {
1220
82.0k
        if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed)
1221
60.6k
                || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed))
1222
27.5k
            style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1223
82.0k
        if (!emitter->scalar_data.length
1224
60.3k
                && (emitter->flow_level || emitter->simple_key_context))
1225
20.8k
            style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1226
82.0k
        if (no_tag && !event->data.scalar.plain_implicit)
1227
0
            style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1228
82.0k
    }
1229
1230
168k
    if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE)
1231
32.6k
    {
1232
32.6k
        if (!emitter->scalar_data.single_quoted_allowed)
1233
4.12k
            style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1234
32.6k
    }
1235
1236
168k
    if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE)
1237
9.42k
    {
1238
9.42k
        if (!emitter->scalar_data.block_allowed
1239
5.79k
                || emitter->flow_level || emitter->simple_key_context)
1240
3.83k
            style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1241
9.42k
    }
1242
1243
168k
    if (no_tag && !event->data.scalar.quoted_implicit
1244
144k
            && style != YAML_PLAIN_SCALAR_STYLE)
1245
93.2k
    {
1246
93.2k
        emitter->tag_data.handle = (yaml_char_t *)"!";
1247
93.2k
        emitter->tag_data.handle_length = 1;
1248
93.2k
    }
1249
1250
168k
    emitter->scalar_data.style = style;
1251
1252
168k
    return 1;
1253
168k
}
1254
1255
/*
1256
 * Write an anchor.
1257
 */
1258
1259
static int
1260
yaml_emitter_process_anchor(yaml_emitter_t *emitter)
1261
365k
{
1262
365k
    if (!emitter->anchor_data.anchor)
1263
359k
        return 1;
1264
1265
6.34k
    if (!yaml_emitter_write_indicator(emitter,
1266
6.34k
                (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0))
1267
0
        return 0;
1268
1269
6.34k
    return yaml_emitter_write_anchor(emitter,
1270
6.34k
            emitter->anchor_data.anchor, emitter->anchor_data.anchor_length);
1271
6.34k
}
1272
1273
/*
1274
 * Write a tag.
1275
 */
1276
1277
static int
1278
yaml_emitter_process_tag(yaml_emitter_t *emitter)
1279
365k
{
1280
365k
    if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
1281
262k
        return 1;
1282
1283
102k
    if (emitter->tag_data.handle)
1284
97.6k
    {
1285
97.6k
        if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle,
1286
97.6k
                    emitter->tag_data.handle_length))
1287
0
            return 0;
1288
97.6k
        if (emitter->tag_data.suffix) {
1289
4.43k
            if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1290
4.43k
                        emitter->tag_data.suffix_length, 0))
1291
0
                return 0;
1292
4.43k
        }
1293
97.6k
    }
1294
4.92k
    else
1295
4.92k
    {
1296
4.92k
        if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0))
1297
0
            return 0;
1298
4.92k
        if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1299
4.92k
                    emitter->tag_data.suffix_length, 0))
1300
0
            return 0;
1301
4.92k
        if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0))
1302
0
            return 0;
1303
4.92k
    }
1304
1305
102k
    return 1;
1306
102k
}
1307
1308
/*
1309
 * Write a scalar.
1310
 */
1311
1312
static int
1313
yaml_emitter_process_scalar(yaml_emitter_t *emitter)
1314
168k
{
1315
168k
    switch (emitter->scalar_data.style)
1316
168k
    {
1317
52.3k
        case YAML_PLAIN_SCALAR_STYLE:
1318
52.3k
            return yaml_emitter_write_plain_scalar(emitter,
1319
52.3k
                    emitter->scalar_data.value, emitter->scalar_data.length,
1320
52.3k
                    !emitter->simple_key_context);
1321
1322
28.5k
        case YAML_SINGLE_QUOTED_SCALAR_STYLE:
1323
28.5k
            return yaml_emitter_write_single_quoted_scalar(emitter,
1324
28.5k
                    emitter->scalar_data.value, emitter->scalar_data.length,
1325
28.5k
                    !emitter->simple_key_context);
1326
1327
82.1k
        case YAML_DOUBLE_QUOTED_SCALAR_STYLE:
1328
82.1k
            return yaml_emitter_write_double_quoted_scalar(emitter,
1329
82.1k
                    emitter->scalar_data.value, emitter->scalar_data.length,
1330
82.1k
                    !emitter->simple_key_context);
1331
1332
3.44k
        case YAML_LITERAL_SCALAR_STYLE:
1333
3.44k
            return yaml_emitter_write_literal_scalar(emitter,
1334
3.44k
                    emitter->scalar_data.value, emitter->scalar_data.length);
1335
1336
2.15k
        case YAML_FOLDED_SCALAR_STYLE:
1337
2.15k
            return yaml_emitter_write_folded_scalar(emitter,
1338
2.15k
                    emitter->scalar_data.value, emitter->scalar_data.length);
1339
1340
0
        default:
1341
0
            assert(1);      /* Impossible. */
1342
168k
    }
1343
1344
0
    return 0;
1345
168k
}
1346
1347
/*
1348
 * Check if a %YAML directive is valid.
1349
 */
1350
1351
static int
1352
yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
1353
        yaml_version_directive_t version_directive)
1354
1.57k
{
1355
1.57k
    if (version_directive.major != 1 || (
1356
1.57k
        version_directive.minor != 1
1357
570
        && version_directive.minor != 2
1358
1.57k
        )) {
1359
0
        return yaml_emitter_set_emitter_error(emitter,
1360
0
                "incompatible %YAML directive");
1361
0
    }
1362
1363
1.57k
    return 1;
1364
1.57k
}
1365
1366
/*
1367
 * Check if a %TAG directive is valid.
1368
 */
1369
1370
static int
1371
yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
1372
        yaml_tag_directive_t tag_directive)
1373
21.7k
{
1374
21.7k
    yaml_string_t handle;
1375
21.7k
    yaml_string_t prefix;
1376
21.7k
    size_t handle_length;
1377
21.7k
    size_t prefix_length;
1378
1379
21.7k
    handle_length = strlen((char *)tag_directive.handle);
1380
21.7k
    prefix_length = strlen((char *)tag_directive.prefix);
1381
21.7k
    STRING_ASSIGN(handle, tag_directive.handle, handle_length);
1382
21.7k
    STRING_ASSIGN(prefix, tag_directive.prefix, prefix_length);
1383
1384
21.7k
    if (handle.start == handle.end) {
1385
0
        return yaml_emitter_set_emitter_error(emitter,
1386
0
                "tag handle must not be empty");
1387
0
    }
1388
1389
21.7k
    if (handle.start[0] != '!') {
1390
0
        return yaml_emitter_set_emitter_error(emitter,
1391
0
                "tag handle must start with '!'");
1392
0
    }
1393
1394
21.7k
    if (handle.end[-1] != '!') {
1395
0
        return yaml_emitter_set_emitter_error(emitter,
1396
0
                "tag handle must end with '!'");
1397
0
    }
1398
1399
21.7k
    handle.pointer ++;
1400
1401
6.36M
    while (handle.pointer < handle.end-1) {
1402
6.34M
        if (!IS_ALPHA(handle)) {
1403
0
            return yaml_emitter_set_emitter_error(emitter,
1404
0
                    "tag handle must contain alphanumerical characters only");
1405
0
        }
1406
6.34M
        MOVE(handle);
1407
6.34M
    }
1408
1409
21.7k
    if (prefix.start == prefix.end) {
1410
2
        return yaml_emitter_set_emitter_error(emitter,
1411
2
                "tag prefix must not be empty");
1412
2
    }
1413
1414
21.7k
    return 1;
1415
21.7k
}
1416
1417
/*
1418
 * Check if an anchor is valid.
1419
 */
1420
1421
static int
1422
yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
1423
        yaml_char_t *anchor, int alias)
1424
6.34k
{
1425
6.34k
    size_t anchor_length;
1426
6.34k
    yaml_string_t string;
1427
1428
6.34k
    anchor_length = strlen((char *)anchor);
1429
6.34k
    STRING_ASSIGN(string, anchor, anchor_length);
1430
1431
6.34k
    if (string.start == string.end) {
1432
0
        return yaml_emitter_set_emitter_error(emitter, alias ?
1433
0
                "alias value must not be empty" :
1434
0
                "anchor value must not be empty");
1435
0
    }
1436
1437
7.58M
    while (string.pointer != string.end) {
1438
7.58M
        if (!IS_ALPHA(string)) {
1439
0
            return yaml_emitter_set_emitter_error(emitter, alias ?
1440
0
                    "alias value must contain alphanumerical characters only" :
1441
0
                    "anchor value must contain alphanumerical characters only");
1442
0
        }
1443
7.58M
        MOVE(string);
1444
7.58M
    }
1445
1446
6.34k
    emitter->anchor_data.anchor = string.start;
1447
6.34k
    emitter->anchor_data.anchor_length = string.end - string.start;
1448
6.34k
    emitter->anchor_data.alias = alias;
1449
1450
6.34k
    return 1;
1451
6.34k
}
1452
1453
/*
1454
 * Check if a tag is valid.
1455
 */
1456
1457
static int
1458
yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
1459
        yaml_char_t *tag)
1460
9.36k
{
1461
9.36k
    size_t tag_length;
1462
9.36k
    yaml_string_t string;
1463
9.36k
    yaml_tag_directive_t *tag_directive;
1464
1465
9.36k
    tag_length = strlen((char *)tag);
1466
9.36k
    STRING_ASSIGN(string, tag, tag_length);
1467
1468
9.36k
    if (string.start == string.end) {
1469
3
        return yaml_emitter_set_emitter_error(emitter,
1470
3
                "tag value must not be empty");
1471
3
    }
1472
1473
9.36k
    for (tag_directive = emitter->tag_directives.start;
1474
20.6k
            tag_directive != emitter->tag_directives.top; tag_directive ++) {
1475
15.7k
        size_t prefix_length = strlen((char *)tag_directive->prefix);
1476
15.7k
        if (prefix_length < (size_t)(string.end - string.start)
1477
5.97k
                && strncmp((char *)tag_directive->prefix, (char *)string.start,
1478
5.97k
                    prefix_length) == 0)
1479
4.43k
        {
1480
4.43k
            emitter->tag_data.handle = tag_directive->handle;
1481
4.43k
            emitter->tag_data.handle_length =
1482
4.43k
                strlen((char *)tag_directive->handle);
1483
4.43k
            emitter->tag_data.suffix = string.start + prefix_length;
1484
4.43k
            emitter->tag_data.suffix_length =
1485
4.43k
                (string.end - string.start) - prefix_length;
1486
4.43k
            return 1;
1487
4.43k
        }
1488
15.7k
    }
1489
1490
4.92k
    emitter->tag_data.suffix = string.start;
1491
4.92k
    emitter->tag_data.suffix_length = string.end - string.start;
1492
1493
4.92k
    return 1;
1494
9.36k
}
1495
1496
/*
1497
 * Check if a scalar is valid.
1498
 */
1499
1500
static int
1501
yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
1502
        yaml_char_t *value, size_t length)
1503
168k
{
1504
168k
    yaml_string_t string;
1505
1506
168k
    int block_indicators = 0;
1507
168k
    int flow_indicators = 0;
1508
168k
    int line_breaks = 0;
1509
168k
    int special_characters = 0;
1510
1511
168k
    int leading_space = 0;
1512
168k
    int leading_break = 0;
1513
168k
    int trailing_space = 0;
1514
168k
    int trailing_break = 0;
1515
168k
    int break_space = 0;
1516
168k
    int space_break = 0;
1517
1518
168k
    int preceded_by_whitespace = 0;
1519
168k
    int followed_by_whitespace = 0;
1520
168k
    int previous_space = 0;
1521
168k
    int previous_break = 0;
1522
1523
168k
    STRING_ASSIGN(string, value, length);
1524
1525
168k
    emitter->scalar_data.value = value;
1526
168k
    emitter->scalar_data.length = length;
1527
1528
168k
    if (string.start == string.end)
1529
121k
    {
1530
121k
        emitter->scalar_data.multiline = 0;
1531
121k
        emitter->scalar_data.flow_plain_allowed = 0;
1532
121k
        emitter->scalar_data.block_plain_allowed = 1;
1533
121k
        emitter->scalar_data.single_quoted_allowed = 1;
1534
121k
        emitter->scalar_data.block_allowed = 0;
1535
1536
121k
        return 1;
1537
121k
    }
1538
1539
46.6k
    if ((CHECK_AT(string, '-', 0)
1540
6.39k
                && CHECK_AT(string, '-', 1)
1541
3.09k
                && CHECK_AT(string, '-', 2))
1542
45.4k
            || (CHECK_AT(string, '.', 0)
1543
3.46k
                && CHECK_AT(string, '.', 1)
1544
2.25k
                && CHECK_AT(string, '.', 2))) {
1545
2.25k
        block_indicators = 1;
1546
2.25k
        flow_indicators = 1;
1547
2.25k
    }
1548
1549
46.6k
    preceded_by_whitespace = 1;
1550
46.6k
    followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
1551
1552
50.3M
    while (string.pointer != string.end)
1553
50.3M
    {
1554
50.3M
        if (string.start == string.pointer)
1555
46.6k
        {
1556
46.6k
            if (CHECK(string, '#') || CHECK(string, ',')
1557
46.0k
                    || CHECK(string, '[') || CHECK(string, ']')
1558
45.2k
                    || CHECK(string, '{') || CHECK(string, '}')
1559
44.5k
                    || CHECK(string, '&') || CHECK(string, '*')
1560
43.8k
                    || CHECK(string, '!') || CHECK(string, '|')
1561
43.1k
                    || CHECK(string, '>') || CHECK(string, '\'')
1562
42.3k
                    || CHECK(string, '"') || CHECK(string, '%')
1563
41.1k
                    || CHECK(string, '@') || CHECK(string, '`')) {
1564
5.96k
                flow_indicators = 1;
1565
5.96k
                block_indicators = 1;
1566
5.96k
            }
1567
1568
46.6k
            if (CHECK(string, '?') || CHECK(string, ':')) {
1569
4.29k
                flow_indicators = 1;
1570
4.29k
                if (followed_by_whitespace) {
1571
1.62k
                    block_indicators = 1;
1572
1.62k
                }
1573
4.29k
            }
1574
1575
46.6k
            if (CHECK(string, '-') && followed_by_whitespace) {
1576
1.87k
                flow_indicators = 1;
1577
1.87k
                block_indicators = 1;
1578
1.87k
            }
1579
46.6k
        }
1580
50.2M
        else
1581
50.2M
        {
1582
50.2M
            if (CHECK(string, ',') || CHECK(string, '?')
1583
49.1M
                    || CHECK(string, '[') || CHECK(string, ']')
1584
48.9M
                    || CHECK(string, '{') || CHECK(string, '}')) {
1585
1.56M
                flow_indicators = 1;
1586
1.56M
            }
1587
1588
50.2M
            if (CHECK(string, ':')) {
1589
57.2k
                flow_indicators = 1;
1590
57.2k
                if (followed_by_whitespace) {
1591
12.4k
                    block_indicators = 1;
1592
12.4k
                }
1593
57.2k
            }
1594
1595
50.2M
            if (CHECK(string, '#') && preceded_by_whitespace) {
1596
5.06k
                flow_indicators = 1;
1597
5.06k
                block_indicators = 1;
1598
5.06k
            }
1599
50.2M
        }
1600
1601
50.3M
        if (!IS_PRINTABLE(string)
1602
47.8M
                || (!IS_ASCII(string) && !emitter->unicode)) {
1603
11.5M
            special_characters = 1;
1604
11.5M
        }
1605
1606
50.3M
        if (IS_BREAK(string)) {
1607
6.63M
            line_breaks = 1;
1608
6.63M
        }
1609
1610
50.3M
        if (IS_SPACE(string))
1611
6.75M
        {
1612
6.75M
            if (string.start == string.pointer) {
1613
1.43k
                leading_space = 1;
1614
1.43k
            }
1615
6.75M
            if (string.pointer+WIDTH(string) == string.end) {
1616
2.03k
                trailing_space = 1;
1617
2.03k
            }
1618
6.75M
            if (previous_break) {
1619
10.4k
                break_space = 1;
1620
10.4k
            }
1621
6.75M
            previous_space = 1;
1622
6.75M
            previous_break = 0;
1623
6.75M
        }
1624
43.5M
        else if (IS_BREAK(string))
1625
6.63M
        {
1626
6.63M
            if (string.start == string.pointer) {
1627
3.29k
                leading_break = 1;
1628
3.29k
            }
1629
6.63M
            if (string.pointer+WIDTH(string) == string.end) {
1630
5.93k
                trailing_break = 1;
1631
5.93k
            }
1632
6.63M
            if (previous_space) {
1633
4.77k
                space_break = 1;
1634
4.77k
            }
1635
6.63M
            previous_space = 0;
1636
6.63M
            previous_break = 1;
1637
6.63M
        }
1638
36.9M
        else
1639
36.9M
        {
1640
36.9M
            previous_space = 0;
1641
36.9M
            previous_break = 0;
1642
36.9M
        }
1643
1644
50.3M
        preceded_by_whitespace = IS_BLANKZ(string);
1645
50.3M
        MOVE(string);
1646
50.3M
        if (string.pointer != string.end) {
1647
50.2M
            followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
1648
50.2M
        }
1649
50.3M
    }
1650
1651
46.6k
    emitter->scalar_data.multiline = line_breaks;
1652
1653
46.6k
    emitter->scalar_data.flow_plain_allowed = 1;
1654
46.6k
    emitter->scalar_data.block_plain_allowed = 1;
1655
46.6k
    emitter->scalar_data.single_quoted_allowed = 1;
1656
46.6k
    emitter->scalar_data.block_allowed = 1;
1657
1658
46.6k
    if (leading_space || leading_break || trailing_space || trailing_break) {
1659
8.55k
        emitter->scalar_data.flow_plain_allowed = 0;
1660
8.55k
        emitter->scalar_data.block_plain_allowed = 0;
1661
8.55k
    }
1662
1663
46.6k
    if (trailing_space) {
1664
2.03k
        emitter->scalar_data.block_allowed = 0;
1665
2.03k
    }
1666
1667
46.6k
    if (break_space) {
1668
725
        emitter->scalar_data.flow_plain_allowed = 0;
1669
725
        emitter->scalar_data.block_plain_allowed = 0;
1670
725
        emitter->scalar_data.single_quoted_allowed = 0;
1671
725
    }
1672
1673
46.6k
    if (space_break || special_characters) {
1674
8.74k
        emitter->scalar_data.flow_plain_allowed = 0;
1675
8.74k
        emitter->scalar_data.block_plain_allowed = 0;
1676
8.74k
        emitter->scalar_data.single_quoted_allowed = 0;
1677
8.74k
        emitter->scalar_data.block_allowed = 0;
1678
8.74k
    }
1679
1680
46.6k
    if (line_breaks) {
1681
9.30k
        emitter->scalar_data.flow_plain_allowed = 0;
1682
9.30k
        emitter->scalar_data.block_plain_allowed = 0;
1683
9.30k
    }
1684
1685
46.6k
    if (flow_indicators) {
1686
18.3k
        emitter->scalar_data.flow_plain_allowed = 0;
1687
18.3k
    }
1688
1689
46.6k
    if (block_indicators) {
1690
12.1k
        emitter->scalar_data.block_plain_allowed = 0;
1691
12.1k
    }
1692
1693
46.6k
    return 1;
1694
168k
}
1695
1696
/*
1697
 * Check if the event data is valid.
1698
 */
1699
1700
static int
1701
yaml_emitter_analyze_event(yaml_emitter_t *emitter,
1702
        yaml_event_t *event)
1703
554k
{
1704
554k
    emitter->anchor_data.anchor = NULL;
1705
554k
    emitter->anchor_data.anchor_length = 0;
1706
554k
    emitter->tag_data.handle = NULL;
1707
554k
    emitter->tag_data.handle_length = 0;
1708
554k
    emitter->tag_data.suffix = NULL;
1709
554k
    emitter->tag_data.suffix_length = 0;
1710
554k
    emitter->scalar_data.value = NULL;
1711
554k
    emitter->scalar_data.length = 0;
1712
1713
554k
    switch (event->type)
1714
554k
    {
1715
880
        case YAML_ALIAS_EVENT:
1716
880
            if (!yaml_emitter_analyze_anchor(emitter,
1717
880
                        event->data.alias.anchor, 1))
1718
0
                return 0;
1719
880
            return 1;
1720
1721
168k
        case YAML_SCALAR_EVENT:
1722
168k
            if (event->data.scalar.anchor) {
1723
2.97k
                if (!yaml_emitter_analyze_anchor(emitter,
1724
2.97k
                            event->data.scalar.anchor, 0))
1725
0
                    return 0;
1726
2.97k
            }
1727
168k
            if (event->data.scalar.tag && (emitter->canonical ||
1728
4.70k
                        (!event->data.scalar.plain_implicit
1729
6.36k
                         && !event->data.scalar.quoted_implicit))) {
1730
6.36k
                if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag))
1731
1
                    return 0;
1732
6.36k
            }
1733
168k
            if (!yaml_emitter_analyze_scalar(emitter,
1734
168k
                        event->data.scalar.value, event->data.scalar.length))
1735
0
                return 0;
1736
168k
            return 1;
1737
1738
91.1k
        case YAML_SEQUENCE_START_EVENT:
1739
91.1k
            if (event->data.sequence_start.anchor) {
1740
1.33k
                if (!yaml_emitter_analyze_anchor(emitter,
1741
1.33k
                            event->data.sequence_start.anchor, 0))
1742
0
                    return 0;
1743
1.33k
            }
1744
91.1k
            if (event->data.sequence_start.tag && (emitter->canonical ||
1745
1.65k
                        !event->data.sequence_start.implicit)) {
1746
1.65k
                if (!yaml_emitter_analyze_tag(emitter,
1747
1.65k
                            event->data.sequence_start.tag))
1748
1
                    return 0;
1749
1.65k
            }
1750
91.1k
            return 1;
1751
1752
105k
        case YAML_MAPPING_START_EVENT:
1753
105k
            if (event->data.mapping_start.anchor) {
1754
1.15k
                if (!yaml_emitter_analyze_anchor(emitter,
1755
1.15k
                            event->data.mapping_start.anchor, 0))
1756
0
                    return 0;
1757
1.15k
            }
1758
105k
            if (event->data.mapping_start.tag && (emitter->canonical ||
1759
1.34k
                        !event->data.mapping_start.implicit)) {
1760
1.34k
                if (!yaml_emitter_analyze_tag(emitter,
1761
1.34k
                            event->data.mapping_start.tag))
1762
1
                    return 0;
1763
1.34k
            }
1764
105k
            return 1;
1765
1766
188k
        default:
1767
188k
            return 1;
1768
554k
    }
1769
554k
}
1770
1771
/*
1772
 * Write the BOM character.
1773
 */
1774
1775
static int
1776
yaml_emitter_write_bom(yaml_emitter_t *emitter)
1777
591
{
1778
591
    if (!FLUSH(emitter)) return 0;
1779
1780
591
    *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF';
1781
591
    *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB';
1782
591
    *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF';
1783
1784
591
    return 1;
1785
591
}
1786
1787
static int
1788
yaml_emitter_write_indent(yaml_emitter_t *emitter)
1789
2.73M
{
1790
2.73M
    int indent = (emitter->indent >= 0) ? emitter->indent : 0;
1791
1792
2.73M
    if (!emitter->indention || emitter->column > indent
1793
2.30M
            || (emitter->column == indent && !emitter->whitespace)) {
1794
2.30M
        if (!PUT_BREAK(emitter)) return 0;
1795
2.30M
    }
1796
1797
2.33G
    while (emitter->column < indent) {
1798
2.33G
        if (!PUT(emitter, ' ')) return 0;
1799
2.33G
    }
1800
1801
2.73M
    emitter->whitespace = 1;
1802
2.73M
    emitter->indention = 1;
1803
1804
2.73M
    return 1;
1805
2.73M
}
1806
1807
static int
1808
yaml_emitter_write_indicator(yaml_emitter_t *emitter,
1809
        const char *indicator, int need_whitespace,
1810
        int is_whitespace, int is_indention)
1811
870k
{
1812
870k
    size_t indicator_length;
1813
870k
    yaml_string_t string;
1814
1815
870k
    indicator_length = strlen(indicator);
1816
870k
    STRING_ASSIGN(string, (yaml_char_t *)indicator, indicator_length);
1817
1818
870k
    if (need_whitespace && !emitter->whitespace) {
1819
202k
        if (!PUT(emitter, ' ')) return 0;
1820
202k
    }
1821
1822
1.87M
    while (string.pointer != string.end) {
1823
1.00M
        if (!WRITE(emitter, string)) return 0;
1824
1.00M
    }
1825
1826
870k
    emitter->whitespace = is_whitespace;
1827
870k
    emitter->indention = (emitter->indention && is_indention);
1828
1829
870k
    return 1;
1830
870k
}
1831
1832
static int
1833
yaml_emitter_write_anchor(yaml_emitter_t *emitter,
1834
        yaml_char_t *value, size_t length)
1835
6.34k
{
1836
6.34k
    yaml_string_t string;
1837
6.34k
    STRING_ASSIGN(string, value, length);
1838
1839
7.58M
    while (string.pointer != string.end) {
1840
7.58M
        if (!WRITE(emitter, string)) return 0;
1841
7.58M
    }
1842
1843
6.34k
    emitter->whitespace = 0;
1844
6.34k
    emitter->indention = 0;
1845
1846
6.34k
    return 1;
1847
6.34k
}
1848
1849
static int
1850
yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
1851
        yaml_char_t *value, size_t length)
1852
119k
{
1853
119k
    yaml_string_t string;
1854
119k
    STRING_ASSIGN(string, value, length);
1855
1856
119k
    if (!emitter->whitespace) {
1857
93.3k
        if (!PUT(emitter, ' ')) return 0;
1858
93.3k
    }
1859
1860
15.8M
    while (string.pointer != string.end) {
1861
15.7M
        if (!WRITE(emitter, string)) return 0;
1862
15.7M
    }
1863
1864
119k
    emitter->whitespace = 0;
1865
119k
    emitter->indention = 0;
1866
1867
119k
    return 1;
1868
119k
}
1869
1870
static int
1871
yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
1872
        yaml_char_t *value, size_t length,
1873
        int need_whitespace)
1874
31.1k
{
1875
31.1k
    yaml_string_t string;
1876
31.1k
    STRING_ASSIGN(string, value, length);
1877
1878
31.1k
    if (need_whitespace && !emitter->whitespace) {
1879
21.7k
        if (!PUT(emitter, ' ')) return 0;
1880
21.7k
    }
1881
1882
40.1M
    while (string.pointer != string.end) {
1883
40.1M
        if (IS_ALPHA(string)
1884
33.9M
                || CHECK(string, ';') || CHECK(string, '/')
1885
33.5M
                || CHECK(string, '?') || CHECK(string, ':')
1886
33.5M
                || CHECK(string, '@') || CHECK(string, '&')
1887
33.5M
                || CHECK(string, '=') || CHECK(string, '+')
1888
33.5M
                || CHECK(string, '$') || CHECK(string, ',')
1889
33.4M
                || CHECK(string, '_') || CHECK(string, '.')
1890
33.4M
                || CHECK(string, '~') || CHECK(string, '*')
1891
33.4M
                || CHECK(string, '\'') || CHECK(string, '(')
1892
33.4M
                || CHECK(string, ')') || CHECK(string, '[')
1893
32.8M
                || CHECK(string, ']')) {
1894
7.28M
            if (!WRITE(emitter, string)) return 0;
1895
7.28M
        }
1896
32.8M
        else {
1897
32.8M
            int width = WIDTH(string);
1898
32.8M
            unsigned int value;
1899
65.6M
            while (width --) {
1900
32.8M
                value = *(string.pointer++);
1901
32.8M
                if (!PUT(emitter, '%')) return 0;
1902
32.8M
                if (!PUT(emitter, (value >> 4)
1903
32.8M
                            + ((value >> 4) < 10 ? '0' : 'A' - 10)))
1904
0
                    return 0;
1905
32.8M
                if (!PUT(emitter, (value & 0x0F)
1906
32.8M
                            + ((value & 0x0F) < 10 ? '0' : 'A' - 10)))
1907
0
                    return 0;
1908
32.8M
            }
1909
32.8M
        }
1910
40.1M
    }
1911
1912
31.1k
    emitter->whitespace = 0;
1913
31.1k
    emitter->indention = 0;
1914
1915
31.1k
    return 1;
1916
31.1k
}
1917
1918
static int
1919
yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
1920
        yaml_char_t *value, size_t length, int allow_breaks)
1921
52.3k
{
1922
52.3k
    yaml_string_t string;
1923
52.3k
    int spaces = 0;
1924
52.3k
    int breaks = 0;
1925
1926
52.3k
    STRING_ASSIGN(string, value, length);
1927
1928
    /**
1929
     * Avoid trailing spaces for empty values in block mode.
1930
     * In flow mode, we still want the space to prevent ambiguous things
1931
     * like {a:}.
1932
     * Currently, the emitter forbids any plain empty scalar in flow mode
1933
     * (e.g. it outputs {a: ''} instead), so emitter->flow_level will
1934
     * never be true here.
1935
     * But if the emitter is ever changed to allow emitting empty values,
1936
     * the check for flow_level is already here.
1937
     */
1938
52.3k
    if (!emitter->whitespace && (length || emitter->flow_level)) {
1939
3.85k
        if (!PUT(emitter, ' ')) return 0;
1940
3.85k
    }
1941
1942
8.52M
    while (string.pointer != string.end)
1943
8.46M
    {
1944
8.46M
        if (IS_SPACE(string))
1945
1.28M
        {
1946
1.28M
            if (allow_breaks && !spaces
1947
408k
                    && emitter->column > emitter->best_width
1948
321k
                    && !IS_SPACE_AT(string, 1)) {
1949
137k
                if (!yaml_emitter_write_indent(emitter)) return 0;
1950
137k
                MOVE(string);
1951
137k
            }
1952
1.15M
            else {
1953
1.15M
                if (!WRITE(emitter, string)) return 0;
1954
1.15M
            }
1955
1.28M
            spaces = 1;
1956
1.28M
        }
1957
7.18M
        else if (IS_BREAK(string))
1958
0
        {
1959
0
            if (!breaks && CHECK(string, '\n')) {
1960
0
                if (!PUT_BREAK(emitter)) return 0;
1961
0
            }
1962
0
            if (!WRITE_BREAK(emitter, string)) return 0;
1963
0
            emitter->indention = 1;
1964
0
            breaks = 1;
1965
0
        }
1966
7.18M
        else
1967
7.18M
        {
1968
7.18M
            if (breaks) {
1969
0
                if (!yaml_emitter_write_indent(emitter)) return 0;
1970
0
            }
1971
7.18M
            if (!WRITE(emitter, string)) return 0;
1972
7.18M
            emitter->indention = 0;
1973
7.18M
            spaces = 0;
1974
7.18M
            breaks = 0;
1975
7.18M
        }
1976
8.46M
    }
1977
1978
52.3k
    emitter->whitespace = 0;
1979
52.3k
    emitter->indention = 0;
1980
1981
52.3k
    return 1;
1982
52.3k
}
1983
1984
static int
1985
yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
1986
        yaml_char_t *value, size_t length, int allow_breaks)
1987
28.5k
{
1988
28.5k
    yaml_string_t string;
1989
28.5k
    int spaces = 0;
1990
28.5k
    int breaks = 0;
1991
1992
28.5k
    STRING_ASSIGN(string, value, length);
1993
1994
28.5k
    if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0))
1995
0
        return 0;
1996
1997
5.95M
    while (string.pointer != string.end)
1998
5.92M
    {
1999
5.92M
        if (IS_SPACE(string))
2000
951k
        {
2001
951k
            if (allow_breaks && !spaces
2002
203k
                    && emitter->column > emitter->best_width
2003
35.7k
                    && string.pointer != string.start
2004
35.5k
                    && string.pointer != string.end - 1
2005
35.3k
                    && !IS_SPACE_AT(string, 1)) {
2006
22.1k
                if (!yaml_emitter_write_indent(emitter)) return 0;
2007
22.1k
                MOVE(string);
2008
22.1k
            }
2009
928k
            else {
2010
928k
                if (!WRITE(emitter, string)) return 0;
2011
928k
            }
2012
951k
            spaces = 1;
2013
951k
        }
2014
4.97M
        else if (IS_BREAK(string))
2015
2.31M
        {
2016
2.31M
            if (!breaks && CHECK(string, '\n')) {
2017
95.7k
                if (!PUT_BREAK(emitter)) return 0;
2018
95.7k
            }
2019
2.31M
            if (!WRITE_BREAK(emitter, string)) return 0;
2020
2.31M
            emitter->indention = 1;
2021
2.31M
            breaks = 1;
2022
2.31M
        }
2023
2.65M
        else
2024
2.65M
        {
2025
2.65M
            if (breaks) {
2026
101k
                if (!yaml_emitter_write_indent(emitter)) return 0;
2027
101k
            }
2028
2.65M
            if (CHECK(string, '\'')) {
2029
449k
                if (!PUT(emitter, '\'')) return 0;
2030
449k
            }
2031
2.65M
            if (!WRITE(emitter, string)) return 0;
2032
2.65M
            emitter->indention = 0;
2033
2.65M
            spaces = 0;
2034
2.65M
            breaks = 0;
2035
2.65M
        }
2036
5.92M
    }
2037
2038
28.5k
    if (breaks)
2039
268
        if (!yaml_emitter_write_indent(emitter)) return 0;
2040
2041
28.5k
    if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0))
2042
0
        return 0;
2043
2044
28.5k
    emitter->whitespace = 0;
2045
28.5k
    emitter->indention = 0;
2046
2047
28.5k
    return 1;
2048
28.5k
}
2049
2050
static int
2051
yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
2052
        yaml_char_t *value, size_t length, int allow_breaks)
2053
82.1k
{
2054
82.1k
    yaml_string_t string;
2055
82.1k
    int spaces = 0;
2056
2057
82.1k
    STRING_ASSIGN(string, value, length);
2058
2059
82.1k
    if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0))
2060
0
        return 0;
2061
2062
25.4M
    while (string.pointer != string.end)
2063
25.4M
    {
2064
25.4M
        if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string))
2065
25.4M
                || IS_BOM(string) || IS_BREAK(string)
2066
12.4M
                || CHECK(string, '"') || CHECK(string, '\\'))
2067
14.1M
        {
2068
14.1M
            unsigned char octet;
2069
14.1M
            unsigned int width;
2070
14.1M
            unsigned int value;
2071
14.1M
            int k;
2072
2073
14.1M
            octet = string.pointer[0];
2074
14.1M
            width = (octet & 0x80) == 0x00 ? 1 :
2075
14.1M
                    (octet & 0xE0) == 0xC0 ? 2 :
2076
9.15M
                    (octet & 0xF0) == 0xE0 ? 3 :
2077
9.09M
                    (octet & 0xF8) == 0xF0 ? 4 : 0;
2078
14.1M
            value = (octet & 0x80) == 0x00 ? octet & 0x7F :
2079
14.1M
                    (octet & 0xE0) == 0xC0 ? octet & 0x1F :
2080
9.15M
                    (octet & 0xF0) == 0xE0 ? octet & 0x0F :
2081
9.09M
                    (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
2082
32.4M
            for (k = 1; k < (int)width; k ++) {
2083
18.2M
                octet = string.pointer[k];
2084
18.2M
                value = (value << 6) + (octet & 0x3F);
2085
18.2M
            }
2086
14.1M
            string.pointer += width;
2087
2088
14.1M
            if (!PUT(emitter, '\\')) return 0;
2089
2090
14.1M
            switch (value)
2091
14.1M
            {
2092
147k
                case 0x00:
2093
147k
                    if (!PUT(emitter, '0')) return 0;
2094
147k
                    break;
2095
2096
147k
                case 0x07:
2097
16.6k
                    if (!PUT(emitter, 'a')) return 0;
2098
16.6k
                    break;
2099
2100
310k
                case 0x08:
2101
310k
                    if (!PUT(emitter, 'b')) return 0;
2102
310k
                    break;
2103
2104
1.49M
                case 0x09:
2105
1.49M
                    if (!PUT(emitter, 't')) return 0;
2106
1.49M
                    break;
2107
2108
1.49M
                case 0x0A:
2109
1.35M
                    if (!PUT(emitter, 'n')) return 0;
2110
1.35M
                    break;
2111
2112
1.35M
                case 0x0B:
2113
1.38k
                    if (!PUT(emitter, 'v')) return 0;
2114
1.38k
                    break;
2115
2116
14.1k
                case 0x0C:
2117
14.1k
                    if (!PUT(emitter, 'f')) return 0;
2118
14.1k
                    break;
2119
2120
188k
                case 0x0D:
2121
188k
                    if (!PUT(emitter, 'r')) return 0;
2122
188k
                    break;
2123
2124
237k
                case 0x1B:
2125
237k
                    if (!PUT(emitter, 'e')) return 0;
2126
237k
                    break;
2127
2128
514k
                case 0x22:
2129
514k
                    if (!PUT(emitter, '\"')) return 0;
2130
514k
                    break;
2131
2132
758k
                case 0x5C:
2133
758k
                    if (!PUT(emitter, '\\')) return 0;
2134
758k
                    break;
2135
2136
758k
                case 0x85:
2137
31.8k
                    if (!PUT(emitter, 'N')) return 0;
2138
31.8k
                    break;
2139
2140
31.8k
                case 0xA0:
2141
1.15k
                    if (!PUT(emitter, '_')) return 0;
2142
1.15k
                    break;
2143
2144
259k
                case 0x2028:
2145
259k
                    if (!PUT(emitter, 'L')) return 0;
2146
259k
                    break;
2147
2148
259k
                case 0x2029:
2149
16.5k
                    if (!PUT(emitter, 'P')) return 0;
2150
16.5k
                    break;
2151
2152
8.84M
                default:
2153
8.84M
                    if (value <= 0xFF) {
2154
16.6k
                        if (!PUT(emitter, 'x')) return 0;
2155
16.6k
                        width = 2;
2156
16.6k
                    }
2157
8.83M
                    else if (value <= 0xFFFF) {
2158
8.82M
                        if (!PUT(emitter, 'u')) return 0;
2159
8.82M
                        width = 4;
2160
8.82M
                    }
2161
2.84k
                    else {
2162
2.84k
                        if (!PUT(emitter, 'U')) return 0;
2163
2.84k
                        width = 8;
2164
2.84k
                    }
2165
44.2M
                    for (k = (width-1)*4; k >= 0; k -= 4) {
2166
35.3M
                        int digit = (value >> k) & 0x0F;
2167
35.3M
                        if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10)))
2168
0
                            return 0;
2169
35.3M
                    }
2170
14.1M
            }
2171
14.1M
            spaces = 0;
2172
14.1M
        }
2173
11.2M
        else if (IS_SPACE(string))
2174
2.85M
        {
2175
2.85M
            if (allow_breaks && !spaces
2176
1.87M
                    && emitter->column > emitter->best_width
2177
1.68M
                    && string.pointer != string.start
2178
1.68M
                    && string.pointer != string.end - 1) {
2179
1.68M
                if (!yaml_emitter_write_indent(emitter)) return 0;
2180
1.68M
                if (IS_SPACE_AT(string, 1)) {
2181
375k
                    if (!PUT(emitter, '\\')) return 0;
2182
375k
                }
2183
1.68M
                MOVE(string);
2184
1.68M
            }
2185
1.17M
            else {
2186
1.17M
                if (!WRITE(emitter, string)) return 0;
2187
1.17M
            }
2188
2.85M
            spaces = 1;
2189
2.85M
        }
2190
8.35M
        else
2191
8.35M
        {
2192
8.35M
            if (!WRITE(emitter, string)) return 0;
2193
8.35M
            spaces = 0;
2194
8.35M
        }
2195
25.4M
    }
2196
2197
82.1k
    if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0))
2198
0
        return 0;
2199
2200
82.1k
    emitter->whitespace = 0;
2201
82.1k
    emitter->indention = 0;
2202
2203
82.1k
    return 1;
2204
82.1k
}
2205
2206
static int
2207
yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
2208
        yaml_string_t string)
2209
5.59k
{
2210
5.59k
    char indent_hint[2];
2211
5.59k
    const char *chomp_hint = NULL;
2212
2213
5.59k
    if (IS_SPACE(string) || IS_BREAK(string))
2214
2.84k
    {
2215
2.84k
        indent_hint[0] = '0' + (char)emitter->best_indent;
2216
2.84k
        indent_hint[1] = '\0';
2217
2.84k
        if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0))
2218
0
            return 0;
2219
2.84k
    }
2220
2221
5.59k
    emitter->open_ended = 0;
2222
2223
5.59k
    string.pointer = string.end;
2224
5.59k
    if (string.start == string.pointer)
2225
0
    {
2226
0
        chomp_hint = "-";
2227
0
    }
2228
5.59k
    else
2229
5.59k
    {
2230
8.46k
        do {
2231
8.46k
            string.pointer --;
2232
8.46k
        } while ((*string.pointer & 0xC0) == 0x80);
2233
5.59k
        if (!IS_BREAK(string))
2234
1.26k
        {
2235
1.26k
            chomp_hint = "-";
2236
1.26k
        }
2237
4.33k
        else if (string.start == string.pointer)
2238
1.12k
        {
2239
1.12k
            chomp_hint = "+";
2240
1.12k
            emitter->open_ended = 2;
2241
1.12k
        }
2242
3.20k
        else
2243
3.20k
        {
2244
5.45k
            do {
2245
5.45k
                string.pointer --;
2246
5.45k
            } while ((*string.pointer & 0xC0) == 0x80);
2247
3.20k
            if (IS_BREAK(string))
2248
1.32k
            {
2249
1.32k
                chomp_hint = "+";
2250
1.32k
                emitter->open_ended = 2;
2251
1.32k
            }
2252
3.20k
        }
2253
5.59k
    }
2254
2255
5.59k
    if (chomp_hint)
2256
3.71k
    {
2257
3.71k
        if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0))
2258
0
            return 0;
2259
3.71k
    }
2260
2261
5.59k
    return 1;
2262
5.59k
}
2263
2264
static int
2265
yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
2266
        yaml_char_t *value, size_t length)
2267
3.44k
{
2268
3.44k
    yaml_string_t string;
2269
3.44k
    int breaks = 1;
2270
2271
3.44k
    STRING_ASSIGN(string, value, length);
2272
2273
3.44k
    if (!yaml_emitter_write_indicator(emitter, "|", 1, 0, 0))
2274
0
        return 0;
2275
3.44k
    if (!yaml_emitter_write_block_scalar_hints(emitter, string))
2276
0
        return 0;
2277
3.44k
    if (!PUT_BREAK(emitter)) return 0;
2278
3.44k
    emitter->indention = 1;
2279
3.44k
    emitter->whitespace = 1;
2280
2281
2.75M
    while (string.pointer != string.end)
2282
2.74M
    {
2283
2.74M
        if (IS_BREAK(string))
2284
969k
        {
2285
969k
            if (!WRITE_BREAK(emitter, string)) return 0;
2286
969k
            emitter->indention = 1;
2287
969k
            breaks = 1;
2288
969k
        }
2289
1.77M
        else
2290
1.77M
        {
2291
1.77M
            if (breaks) {
2292
2.63k
                if (!yaml_emitter_write_indent(emitter)) return 0;
2293
2.63k
            }
2294
1.77M
            if (!WRITE(emitter, string)) return 0;
2295
1.77M
            emitter->indention = 0;
2296
1.77M
            breaks = 0;
2297
1.77M
        }
2298
2.74M
    }
2299
2300
3.44k
    return 1;
2301
3.44k
}
2302
2303
static int
2304
yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
2305
        yaml_char_t *value, size_t length)
2306
2.15k
{
2307
2.15k
    yaml_string_t string;
2308
2.15k
    int breaks = 1;
2309
2.15k
    int leading_spaces = 1;
2310
2311
2.15k
    STRING_ASSIGN(string, value, length);
2312
2313
2.15k
    if (!yaml_emitter_write_indicator(emitter, ">", 1, 0, 0))
2314
0
        return 0;
2315
2.15k
    if (!yaml_emitter_write_block_scalar_hints(emitter, string))
2316
0
        return 0;
2317
2.15k
    if (!PUT_BREAK(emitter)) return 0;
2318
2.15k
    emitter->indention = 1;
2319
2.15k
    emitter->whitespace = 1;
2320
2321
7.79M
    while (string.pointer != string.end)
2322
7.79M
    {
2323
7.79M
        if (IS_BREAK(string))
2324
1.49M
        {
2325
1.49M
            if (!breaks && !leading_spaces && CHECK(string, '\n')) {
2326
15.6k
                int k = 0;
2327
1.13M
                while (IS_BREAK_AT(string, k)) {
2328
1.12M
                    k += WIDTH_AT(string, k);
2329
1.12M
                }
2330
15.6k
                if (!IS_BLANKZ_AT(string, k)) {
2331
10.1k
                    if (!PUT_BREAK(emitter)) return 0;
2332
10.1k
                }
2333
15.6k
            }
2334
1.49M
            if (!WRITE_BREAK(emitter, string)) return 0;
2335
1.49M
            emitter->indention = 1;
2336
1.49M
            breaks = 1;
2337
1.49M
        }
2338
6.30M
        else
2339
6.30M
        {
2340
6.30M
            if (breaks) {
2341
255k
                if (!yaml_emitter_write_indent(emitter)) return 0;
2342
255k
                leading_spaces = IS_BLANK(string);
2343
255k
            }
2344
6.30M
            if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1)
2345
561k
                    && emitter->column > emitter->best_width) {
2346
89.7k
                if (!yaml_emitter_write_indent(emitter)) return 0;
2347
89.7k
                MOVE(string);
2348
89.7k
            }
2349
6.21M
            else {
2350
6.21M
                if (!WRITE(emitter, string)) return 0;
2351
6.21M
            }
2352
6.30M
            emitter->indention = 0;
2353
6.30M
            breaks = 0;
2354
6.30M
        }
2355
7.79M
    }
2356
2357
2.15k
    return 1;
2358
2.15k
}