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