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