/src/libyaml_deconstructor_alt_fuzzer.c
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2020 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "yaml.h" |
16 | | #include "yaml_write_handler.h" |
17 | | #include <assert.h> |
18 | | #include <stdbool.h> |
19 | | #include <stdint.h> |
20 | | #include <stdio.h> |
21 | | #include <stdlib.h> |
22 | | #include <string.h> |
23 | | |
24 | | #ifdef NDEBUG |
25 | | #undef NDEBUG |
26 | | #endif |
27 | | |
28 | 9.37k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
29 | 9.37k | if (size < 2) |
30 | 1 | return 0; |
31 | | |
32 | 9.37k | bool done = false; |
33 | 9.37k | bool is_canonical = data[0] & 1; |
34 | 9.37k | bool is_unicode = data[1] & 1; |
35 | 9.37k | data += 2; |
36 | 9.37k | size -= 2; |
37 | | |
38 | 9.37k | yaml_parser_t parser; |
39 | 9.37k | yaml_emitter_t emitter; |
40 | 9.37k | yaml_event_t input_event; |
41 | 9.37k | yaml_document_t output_document; |
42 | | |
43 | 9.37k | int root; |
44 | | |
45 | | /* Initialize the parser and emitter objects. */ |
46 | | |
47 | 9.37k | if (!yaml_parser_initialize(&parser)) { |
48 | 0 | return 1; |
49 | 0 | } |
50 | | |
51 | 9.37k | if (!yaml_emitter_initialize(&emitter)) { |
52 | 0 | yaml_parser_delete(&parser); |
53 | 0 | return 1; |
54 | 0 | } |
55 | | |
56 | | /* Set the parser parameters. */ |
57 | | |
58 | 9.37k | yaml_parser_set_input_string(&parser, data, size); |
59 | | |
60 | | /* Set the emitter parameters. */ |
61 | 9.37k | yaml_output_buffer_t out = {/*buf=*/NULL, /*size=*/0, /*capacity=*/1000}; |
62 | 9.37k | yaml_emitter_set_output(&emitter, yaml_write_handler, &out); |
63 | | |
64 | 9.37k | yaml_emitter_set_canonical(&emitter, is_canonical); |
65 | 9.37k | yaml_emitter_set_unicode(&emitter, is_unicode); |
66 | | |
67 | | /* Create and emit the STREAM-START event. */ |
68 | | |
69 | 9.37k | if (!yaml_emitter_open(&emitter)) |
70 | 0 | goto error; |
71 | | |
72 | | /* Create a output_document object. */ |
73 | | |
74 | 9.37k | if (!yaml_document_initialize(&output_document, NULL, NULL, NULL, 0, 0)) |
75 | 0 | goto error; |
76 | | |
77 | | /* Create the root sequence. */ |
78 | | |
79 | 9.37k | root = yaml_document_add_sequence(&output_document, NULL, |
80 | 9.37k | YAML_BLOCK_SEQUENCE_STYLE); |
81 | 9.37k | if (!root) |
82 | 0 | goto error; |
83 | | |
84 | | /* Loop through the input events. */ |
85 | | |
86 | 25.4M | while (!done) { |
87 | 25.4M | int properties, key, value, map, seq; |
88 | | |
89 | | /* Get the next event. */ |
90 | | |
91 | 25.4M | if (!yaml_parser_parse(&parser, &input_event)) |
92 | 4.71k | goto error; |
93 | | |
94 | | /* Check if this is the stream end. */ |
95 | | |
96 | 25.4M | done = (input_event.type == YAML_STREAM_END_EVENT); |
97 | | |
98 | | /* Create a mapping node and attach it to the root sequence. */ |
99 | | |
100 | 25.4M | properties = yaml_document_add_mapping(&output_document, NULL, |
101 | 25.4M | YAML_BLOCK_MAPPING_STYLE); |
102 | 25.4M | if (!properties) |
103 | 0 | goto error; |
104 | 25.4M | if (!yaml_document_append_sequence_item(&output_document, root, properties)) |
105 | 0 | goto error; |
106 | | |
107 | | /* Analyze the event. */ |
108 | | |
109 | 25.4M | switch (input_event.type) { |
110 | 9.18k | case YAML_STREAM_START_EVENT: |
111 | | |
112 | | /* Add 'type': 'STREAM-START'. */ |
113 | | |
114 | 9.18k | key = yaml_document_add_scalar(&output_document, NULL, |
115 | 9.18k | (yaml_char_t *)"type", -1, |
116 | 9.18k | YAML_PLAIN_SCALAR_STYLE); |
117 | 9.18k | if (!key) |
118 | 0 | goto error; |
119 | 9.18k | value = yaml_document_add_scalar(&output_document, NULL, |
120 | 9.18k | (yaml_char_t *)"STREAM-START", -1, |
121 | 9.18k | YAML_PLAIN_SCALAR_STYLE); |
122 | 9.18k | if (!value) |
123 | 0 | goto error; |
124 | 9.18k | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
125 | 9.18k | value)) |
126 | 0 | goto error; |
127 | | |
128 | | /* Add 'encoding': <encoding>. */ |
129 | | |
130 | 9.18k | if (input_event.data.stream_start.encoding) { |
131 | 9.18k | yaml_encoding_t encoding = input_event.data.stream_start.encoding; |
132 | | |
133 | 9.18k | key = yaml_document_add_scalar(&output_document, NULL, |
134 | 9.18k | (yaml_char_t *)"encoding", -1, |
135 | 9.18k | YAML_PLAIN_SCALAR_STYLE); |
136 | 9.18k | if (!key) |
137 | 0 | goto error; |
138 | 9.18k | value = yaml_document_add_scalar( |
139 | 9.18k | &output_document, NULL, |
140 | 9.18k | (encoding == YAML_UTF8_ENCODING |
141 | 9.18k | ? (yaml_char_t *)"utf-8" |
142 | 9.18k | : encoding == YAML_UTF16LE_ENCODING |
143 | 478 | ? (yaml_char_t *)"utf-16-le" |
144 | 478 | : encoding == YAML_UTF16BE_ENCODING |
145 | 289 | ? (yaml_char_t *)"utf-16-be" |
146 | 289 | : (yaml_char_t *)"unknown"), |
147 | 9.18k | -1, YAML_PLAIN_SCALAR_STYLE); |
148 | 9.18k | if (!value) |
149 | 0 | goto error; |
150 | 9.18k | if (!yaml_document_append_mapping_pair(&output_document, properties, |
151 | 9.18k | key, value)) |
152 | 0 | goto error; |
153 | 9.18k | } |
154 | | |
155 | 9.18k | break; |
156 | | |
157 | 9.18k | case YAML_STREAM_END_EVENT: |
158 | | |
159 | | /* Add 'type': 'STREAM-END'. */ |
160 | | |
161 | 4.65k | key = yaml_document_add_scalar(&output_document, NULL, |
162 | 4.65k | (yaml_char_t *)"type", -1, |
163 | 4.65k | YAML_PLAIN_SCALAR_STYLE); |
164 | 4.65k | if (!key) |
165 | 0 | goto error; |
166 | 4.65k | value = yaml_document_add_scalar(&output_document, NULL, |
167 | 4.65k | (yaml_char_t *)"STREAM-END", -1, |
168 | 4.65k | YAML_PLAIN_SCALAR_STYLE); |
169 | 4.65k | if (!value) |
170 | 0 | goto error; |
171 | 4.65k | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
172 | 4.65k | value)) |
173 | 0 | goto error; |
174 | | |
175 | 4.65k | break; |
176 | | |
177 | 438k | case YAML_DOCUMENT_START_EVENT: |
178 | | |
179 | | /* Add 'type': 'DOCUMENT-START'. */ |
180 | | |
181 | 438k | key = yaml_document_add_scalar(&output_document, NULL, |
182 | 438k | (yaml_char_t *)"type", -1, |
183 | 438k | YAML_PLAIN_SCALAR_STYLE); |
184 | 438k | if (!key) |
185 | 0 | goto error; |
186 | 438k | value = yaml_document_add_scalar(&output_document, NULL, |
187 | 438k | (yaml_char_t *)"DOCUMENT-START", -1, |
188 | 438k | YAML_PLAIN_SCALAR_STYLE); |
189 | 438k | if (!value) |
190 | 0 | goto error; |
191 | 438k | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
192 | 438k | value)) |
193 | 0 | goto error; |
194 | | |
195 | | /* Display the output_document version numbers. */ |
196 | | |
197 | 438k | if (input_event.data.document_start.version_directive) { |
198 | 1.14k | yaml_version_directive_t *version = |
199 | 1.14k | input_event.data.document_start.version_directive; |
200 | 1.14k | char number[64]; |
201 | | |
202 | | /* Add 'version': {}. */ |
203 | | |
204 | 1.14k | key = yaml_document_add_scalar(&output_document, NULL, |
205 | 1.14k | (yaml_char_t *)"version", -1, |
206 | 1.14k | YAML_PLAIN_SCALAR_STYLE); |
207 | 1.14k | if (!key) |
208 | 0 | goto error; |
209 | 1.14k | map = yaml_document_add_mapping(&output_document, NULL, |
210 | 1.14k | YAML_FLOW_MAPPING_STYLE); |
211 | 1.14k | if (!map) |
212 | 0 | goto error; |
213 | 1.14k | if (!yaml_document_append_mapping_pair(&output_document, properties, |
214 | 1.14k | key, map)) |
215 | 0 | goto error; |
216 | | |
217 | | /* Add 'major': <number>. */ |
218 | | |
219 | 1.14k | key = yaml_document_add_scalar(&output_document, NULL, |
220 | 1.14k | (yaml_char_t *)"major", -1, |
221 | 1.14k | YAML_PLAIN_SCALAR_STYLE); |
222 | 1.14k | if (!key) |
223 | 0 | goto error; |
224 | 1.14k | sprintf(number, "%d", version->major); |
225 | 1.14k | value = yaml_document_add_scalar( |
226 | 1.14k | &output_document, (yaml_char_t *)YAML_INT_TAG, |
227 | 1.14k | (yaml_char_t *)number, -1, YAML_PLAIN_SCALAR_STYLE); |
228 | 1.14k | if (!value) |
229 | 0 | goto error; |
230 | 1.14k | if (!yaml_document_append_mapping_pair(&output_document, map, key, |
231 | 1.14k | value)) |
232 | 0 | goto error; |
233 | | |
234 | | /* Add 'minor': <number>. */ |
235 | | |
236 | 1.14k | key = yaml_document_add_scalar(&output_document, NULL, |
237 | 1.14k | (yaml_char_t *)"minor", -1, |
238 | 1.14k | YAML_PLAIN_SCALAR_STYLE); |
239 | 1.14k | if (!key) |
240 | 0 | goto error; |
241 | 1.14k | sprintf(number, "%d", version->minor); |
242 | 1.14k | value = yaml_document_add_scalar( |
243 | 1.14k | &output_document, (yaml_char_t *)YAML_INT_TAG, |
244 | 1.14k | (yaml_char_t *)number, -1, YAML_PLAIN_SCALAR_STYLE); |
245 | 1.14k | if (!value) |
246 | 0 | goto error; |
247 | 1.14k | if (!yaml_document_append_mapping_pair(&output_document, map, key, |
248 | 1.14k | value)) |
249 | 0 | goto error; |
250 | 1.14k | } |
251 | | |
252 | | /* Display the output_document tag directives. */ |
253 | | |
254 | 438k | if (input_event.data.document_start.tag_directives.start != |
255 | 438k | input_event.data.document_start.tag_directives.end) { |
256 | 19.8k | yaml_tag_directive_t *tag; |
257 | | |
258 | | /* Add 'tags': []. */ |
259 | | |
260 | 19.8k | key = yaml_document_add_scalar(&output_document, NULL, |
261 | 19.8k | (yaml_char_t *)"tags", -1, |
262 | 19.8k | YAML_PLAIN_SCALAR_STYLE); |
263 | 19.8k | if (!key) |
264 | 0 | goto error; |
265 | 19.8k | seq = yaml_document_add_sequence(&output_document, NULL, |
266 | 19.8k | YAML_BLOCK_SEQUENCE_STYLE); |
267 | 19.8k | if (!seq) |
268 | 0 | goto error; |
269 | 19.8k | if (!yaml_document_append_mapping_pair(&output_document, properties, |
270 | 19.8k | key, seq)) |
271 | 0 | goto error; |
272 | | |
273 | 19.8k | for (tag = input_event.data.document_start.tag_directives.start; |
274 | 42.6k | tag != input_event.data.document_start.tag_directives.end; tag++) { |
275 | | /* Add {}. */ |
276 | | |
277 | 22.8k | map = yaml_document_add_mapping(&output_document, NULL, |
278 | 22.8k | YAML_FLOW_MAPPING_STYLE); |
279 | 22.8k | if (!map) |
280 | 0 | goto error; |
281 | 22.8k | if (!yaml_document_append_sequence_item(&output_document, seq, map)) |
282 | 0 | goto error; |
283 | | |
284 | | /* Add 'handle': <handle>. */ |
285 | | |
286 | 22.8k | key = yaml_document_add_scalar(&output_document, NULL, |
287 | 22.8k | (yaml_char_t *)"handle", -1, |
288 | 22.8k | YAML_PLAIN_SCALAR_STYLE); |
289 | 22.8k | if (!key) |
290 | 0 | goto error; |
291 | 22.8k | value = yaml_document_add_scalar(&output_document, NULL, tag->handle, |
292 | 22.8k | -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); |
293 | 22.8k | if (!value) |
294 | 0 | goto error; |
295 | 22.8k | if (!yaml_document_append_mapping_pair(&output_document, map, key, |
296 | 22.8k | value)) |
297 | 0 | goto error; |
298 | | |
299 | | /* Add 'prefix': <prefix>. */ |
300 | | |
301 | 22.8k | key = yaml_document_add_scalar(&output_document, NULL, |
302 | 22.8k | (yaml_char_t *)"prefix", -1, |
303 | 22.8k | YAML_PLAIN_SCALAR_STYLE); |
304 | 22.8k | if (!key) |
305 | 0 | goto error; |
306 | 22.8k | value = yaml_document_add_scalar(&output_document, NULL, tag->prefix, |
307 | 22.8k | -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); |
308 | 22.8k | if (!value) |
309 | 1 | goto error; |
310 | 22.8k | if (!yaml_document_append_mapping_pair(&output_document, map, key, |
311 | 22.8k | value)) |
312 | 0 | goto error; |
313 | 22.8k | } |
314 | 19.8k | } |
315 | | |
316 | | /* Add 'implicit': <flag>. */ |
317 | | |
318 | 438k | key = yaml_document_add_scalar(&output_document, NULL, |
319 | 438k | (yaml_char_t *)"implicit", -1, |
320 | 438k | YAML_PLAIN_SCALAR_STYLE); |
321 | 438k | if (!key) |
322 | 0 | goto error; |
323 | 438k | value = yaml_document_add_scalar( |
324 | 438k | &output_document, (yaml_char_t *)YAML_BOOL_TAG, |
325 | 438k | (input_event.data.document_start.implicit ? (yaml_char_t *)"true" |
326 | 438k | : (yaml_char_t *)"false"), |
327 | 438k | -1, YAML_PLAIN_SCALAR_STYLE); |
328 | 438k | if (!value) |
329 | 0 | goto error; |
330 | 438k | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
331 | 438k | value)) |
332 | 0 | goto error; |
333 | | |
334 | 438k | break; |
335 | | |
336 | 438k | case YAML_DOCUMENT_END_EVENT: |
337 | | |
338 | | /* Add 'type': 'DOCUMENT-END'. */ |
339 | | |
340 | 436k | key = yaml_document_add_scalar(&output_document, NULL, |
341 | 436k | (yaml_char_t *)"type", -1, |
342 | 436k | YAML_PLAIN_SCALAR_STYLE); |
343 | 436k | if (!key) |
344 | 0 | goto error; |
345 | 436k | value = yaml_document_add_scalar(&output_document, NULL, |
346 | 436k | (yaml_char_t *)"DOCUMENT-END", -1, |
347 | 436k | YAML_PLAIN_SCALAR_STYLE); |
348 | 436k | if (!value) |
349 | 0 | goto error; |
350 | 436k | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
351 | 436k | value)) |
352 | 0 | goto error; |
353 | | |
354 | | /* Add 'implicit': <flag>. */ |
355 | | |
356 | 436k | key = yaml_document_add_scalar(&output_document, NULL, |
357 | 436k | (yaml_char_t *)"implicit", -1, |
358 | 436k | YAML_PLAIN_SCALAR_STYLE); |
359 | 436k | if (!key) |
360 | 0 | goto error; |
361 | 436k | value = yaml_document_add_scalar( |
362 | 436k | &output_document, (yaml_char_t *)YAML_BOOL_TAG, |
363 | 436k | (input_event.data.document_end.implicit ? (yaml_char_t *)"true" |
364 | 436k | : (yaml_char_t *)"false"), |
365 | 436k | -1, YAML_PLAIN_SCALAR_STYLE); |
366 | 436k | if (!value) |
367 | 0 | goto error; |
368 | 436k | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
369 | 436k | value)) |
370 | 0 | goto error; |
371 | | |
372 | 436k | break; |
373 | | |
374 | 436k | case YAML_ALIAS_EVENT: |
375 | | |
376 | | /* Add 'type': 'ALIAS'. */ |
377 | | |
378 | 10.3k | key = yaml_document_add_scalar(&output_document, NULL, |
379 | 10.3k | (yaml_char_t *)"type", -1, |
380 | 10.3k | YAML_PLAIN_SCALAR_STYLE); |
381 | 10.3k | if (!key) |
382 | 0 | goto error; |
383 | 10.3k | value = yaml_document_add_scalar(&output_document, NULL, |
384 | 10.3k | (yaml_char_t *)"ALIAS", -1, |
385 | 10.3k | YAML_PLAIN_SCALAR_STYLE); |
386 | 10.3k | if (!value) |
387 | 0 | goto error; |
388 | 10.3k | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
389 | 10.3k | value)) |
390 | 0 | goto error; |
391 | | |
392 | | /* Add 'anchor': <anchor>. */ |
393 | | |
394 | 10.3k | key = yaml_document_add_scalar(&output_document, NULL, |
395 | 10.3k | (yaml_char_t *)"anchor", -1, |
396 | 10.3k | YAML_PLAIN_SCALAR_STYLE); |
397 | 10.3k | if (!key) |
398 | 0 | goto error; |
399 | 10.3k | value = yaml_document_add_scalar(&output_document, NULL, |
400 | 10.3k | input_event.data.alias.anchor, -1, |
401 | 10.3k | YAML_DOUBLE_QUOTED_SCALAR_STYLE); |
402 | 10.3k | if (!value) |
403 | 0 | goto error; |
404 | 10.3k | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
405 | 10.3k | value)) |
406 | 0 | goto error; |
407 | | |
408 | 10.3k | break; |
409 | | |
410 | 15.0M | case YAML_SCALAR_EVENT: |
411 | | |
412 | | /* Add 'type': 'SCALAR'. */ |
413 | | |
414 | 15.0M | key = yaml_document_add_scalar(&output_document, NULL, |
415 | 15.0M | (yaml_char_t *)"type", -1, |
416 | 15.0M | YAML_PLAIN_SCALAR_STYLE); |
417 | 15.0M | if (!key) |
418 | 0 | goto error; |
419 | 15.0M | value = yaml_document_add_scalar(&output_document, NULL, |
420 | 15.0M | (yaml_char_t *)"SCALAR", -1, |
421 | 15.0M | YAML_PLAIN_SCALAR_STYLE); |
422 | 15.0M | if (!value) |
423 | 0 | goto error; |
424 | 15.0M | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
425 | 15.0M | value)) |
426 | 0 | goto error; |
427 | | |
428 | | /* Add 'anchor': <anchor>. */ |
429 | | |
430 | 15.0M | if (input_event.data.scalar.anchor) { |
431 | 33.8k | key = yaml_document_add_scalar(&output_document, NULL, |
432 | 33.8k | (yaml_char_t *)"anchor", -1, |
433 | 33.8k | YAML_PLAIN_SCALAR_STYLE); |
434 | 33.8k | if (!key) |
435 | 0 | goto error; |
436 | 33.8k | value = yaml_document_add_scalar(&output_document, NULL, |
437 | 33.8k | input_event.data.scalar.anchor, -1, |
438 | 33.8k | YAML_DOUBLE_QUOTED_SCALAR_STYLE); |
439 | 33.8k | if (!value) |
440 | 0 | goto error; |
441 | 33.8k | if (!yaml_document_append_mapping_pair(&output_document, properties, |
442 | 33.8k | key, value)) |
443 | 0 | goto error; |
444 | 33.8k | } |
445 | | |
446 | | /* Add 'tag': <tag>. */ |
447 | | |
448 | 15.0M | if (input_event.data.scalar.tag) { |
449 | 488k | key = yaml_document_add_scalar(&output_document, NULL, |
450 | 488k | (yaml_char_t *)"tag", -1, |
451 | 488k | YAML_PLAIN_SCALAR_STYLE); |
452 | 488k | if (!key) |
453 | 0 | goto error; |
454 | 488k | value = yaml_document_add_scalar(&output_document, NULL, |
455 | 488k | input_event.data.scalar.tag, -1, |
456 | 488k | YAML_DOUBLE_QUOTED_SCALAR_STYLE); |
457 | 488k | if (!value) |
458 | 7 | goto error; |
459 | 488k | if (!yaml_document_append_mapping_pair(&output_document, properties, |
460 | 488k | key, value)) |
461 | 0 | goto error; |
462 | 488k | } |
463 | | |
464 | | /* Add 'value': <value>. */ |
465 | | |
466 | 15.0M | key = yaml_document_add_scalar(&output_document, NULL, |
467 | 15.0M | (yaml_char_t *)"value", -1, |
468 | 15.0M | YAML_PLAIN_SCALAR_STYLE); |
469 | 15.0M | if (!key) |
470 | 0 | goto error; |
471 | 15.0M | value = yaml_document_add_scalar( |
472 | 15.0M | &output_document, NULL, input_event.data.scalar.value, |
473 | 15.0M | input_event.data.scalar.length, YAML_DOUBLE_QUOTED_SCALAR_STYLE); |
474 | 15.0M | if (!value) |
475 | 0 | goto error; |
476 | 15.0M | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
477 | 15.0M | value)) |
478 | 0 | goto error; |
479 | | |
480 | | /* Display if the scalar tag is implicit. */ |
481 | | |
482 | | /* Add 'implicit': {} */ |
483 | | |
484 | 15.0M | key = yaml_document_add_scalar(&output_document, NULL, |
485 | 15.0M | (yaml_char_t *)"version", -1, |
486 | 15.0M | YAML_PLAIN_SCALAR_STYLE); |
487 | 15.0M | if (!key) |
488 | 0 | goto error; |
489 | 15.0M | map = yaml_document_add_mapping(&output_document, NULL, |
490 | 15.0M | YAML_FLOW_MAPPING_STYLE); |
491 | 15.0M | if (!map) |
492 | 0 | goto error; |
493 | 15.0M | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
494 | 15.0M | map)) |
495 | 0 | goto error; |
496 | | |
497 | | /* Add 'plain': <flag>. */ |
498 | | |
499 | 15.0M | key = yaml_document_add_scalar(&output_document, NULL, |
500 | 15.0M | (yaml_char_t *)"plain", -1, |
501 | 15.0M | YAML_PLAIN_SCALAR_STYLE); |
502 | 15.0M | if (!key) |
503 | 0 | goto error; |
504 | 15.0M | value = yaml_document_add_scalar( |
505 | 15.0M | &output_document, (yaml_char_t *)YAML_BOOL_TAG, |
506 | 15.0M | (input_event.data.scalar.plain_implicit ? (yaml_char_t *)"true" |
507 | 15.0M | : (yaml_char_t *)"false"), |
508 | 15.0M | -1, YAML_PLAIN_SCALAR_STYLE); |
509 | 15.0M | if (!value) |
510 | 0 | goto error; |
511 | 15.0M | if (!yaml_document_append_mapping_pair(&output_document, map, key, value)) |
512 | 0 | goto error; |
513 | | |
514 | | /* Add 'quoted': <flag>. */ |
515 | | |
516 | 15.0M | key = yaml_document_add_scalar(&output_document, NULL, |
517 | 15.0M | (yaml_char_t *)"quoted", -1, |
518 | 15.0M | YAML_PLAIN_SCALAR_STYLE); |
519 | 15.0M | if (!key) |
520 | 0 | goto error; |
521 | 15.0M | value = yaml_document_add_scalar( |
522 | 15.0M | &output_document, (yaml_char_t *)YAML_BOOL_TAG, |
523 | 15.0M | (input_event.data.scalar.quoted_implicit ? (yaml_char_t *)"true" |
524 | 15.0M | : (yaml_char_t *)"false"), |
525 | 15.0M | -1, YAML_PLAIN_SCALAR_STYLE); |
526 | 15.0M | if (!value) |
527 | 0 | goto error; |
528 | 15.0M | if (!yaml_document_append_mapping_pair(&output_document, map, key, value)) |
529 | 0 | goto error; |
530 | | |
531 | | /* Display the style information. */ |
532 | | |
533 | 15.0M | if (input_event.data.scalar.style) { |
534 | 15.0M | yaml_scalar_style_t style = input_event.data.scalar.style; |
535 | | |
536 | | /* Add 'style': <style>. */ |
537 | | |
538 | 15.0M | key = yaml_document_add_scalar(&output_document, NULL, |
539 | 15.0M | (yaml_char_t *)"style", -1, |
540 | 15.0M | YAML_PLAIN_SCALAR_STYLE); |
541 | 15.0M | if (!key) |
542 | 0 | goto error; |
543 | 15.0M | value = yaml_document_add_scalar( |
544 | 15.0M | &output_document, NULL, |
545 | 15.0M | (yaml_char_t |
546 | 15.0M | *)(style == YAML_PLAIN_SCALAR_STYLE |
547 | 15.0M | ? "plain" |
548 | 15.0M | : style == YAML_SINGLE_QUOTED_SCALAR_STYLE |
549 | 31.8k | ? "single-quoted" |
550 | 31.8k | : style == YAML_DOUBLE_QUOTED_SCALAR_STYLE |
551 | 26.3k | ? "double-quoted" |
552 | 26.3k | : style == YAML_LITERAL_SCALAR_STYLE |
553 | 24.4k | ? "literal" |
554 | 24.4k | : style == YAML_FOLDED_SCALAR_STYLE |
555 | 10.1k | ? "folded" |
556 | 10.1k | : "unknown"), |
557 | 15.0M | -1, YAML_PLAIN_SCALAR_STYLE); |
558 | 15.0M | if (!value) |
559 | 0 | goto error; |
560 | 15.0M | if (!yaml_document_append_mapping_pair(&output_document, properties, |
561 | 15.0M | key, value)) |
562 | 0 | goto error; |
563 | 15.0M | } |
564 | | |
565 | 15.0M | break; |
566 | | |
567 | 15.0M | case YAML_SEQUENCE_START_EVENT: |
568 | | |
569 | | /* Add 'type': 'SEQUENCE-START'. */ |
570 | | |
571 | 152k | key = yaml_document_add_scalar(&output_document, NULL, |
572 | 152k | (yaml_char_t *)"type", -1, |
573 | 152k | YAML_PLAIN_SCALAR_STYLE); |
574 | 152k | if (!key) |
575 | 0 | goto error; |
576 | 152k | value = yaml_document_add_scalar(&output_document, NULL, |
577 | 152k | (yaml_char_t *)"SEQUENCE-START", -1, |
578 | 152k | YAML_PLAIN_SCALAR_STYLE); |
579 | 152k | if (!value) |
580 | 0 | goto error; |
581 | 152k | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
582 | 152k | value)) |
583 | 0 | goto error; |
584 | | |
585 | | /* Add 'anchor': <anchor>. */ |
586 | | |
587 | 152k | if (input_event.data.sequence_start.anchor) { |
588 | 1.18k | key = yaml_document_add_scalar(&output_document, NULL, |
589 | 1.18k | (yaml_char_t *)"anchor", -1, |
590 | 1.18k | YAML_PLAIN_SCALAR_STYLE); |
591 | 1.18k | if (!key) |
592 | 0 | goto error; |
593 | 1.18k | value = yaml_document_add_scalar(&output_document, NULL, |
594 | 1.18k | input_event.data.sequence_start.anchor, |
595 | 1.18k | -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); |
596 | 1.18k | if (!value) |
597 | 0 | goto error; |
598 | 1.18k | if (!yaml_document_append_mapping_pair(&output_document, properties, |
599 | 1.18k | key, value)) |
600 | 0 | goto error; |
601 | 1.18k | } |
602 | | |
603 | | /* Add 'tag': <tag>. */ |
604 | | |
605 | 152k | if (input_event.data.sequence_start.tag) { |
606 | 943 | key = yaml_document_add_scalar(&output_document, NULL, |
607 | 943 | (yaml_char_t *)"tag", -1, |
608 | 943 | YAML_PLAIN_SCALAR_STYLE); |
609 | 943 | if (!key) |
610 | 0 | goto error; |
611 | 943 | value = yaml_document_add_scalar(&output_document, NULL, |
612 | 943 | input_event.data.sequence_start.tag, |
613 | 943 | -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); |
614 | 943 | if (!value) |
615 | 1 | goto error; |
616 | 942 | if (!yaml_document_append_mapping_pair(&output_document, properties, |
617 | 942 | key, value)) |
618 | 0 | goto error; |
619 | 942 | } |
620 | | |
621 | | /* Add 'implicit': <flag>. */ |
622 | | |
623 | 152k | key = yaml_document_add_scalar(&output_document, NULL, |
624 | 152k | (yaml_char_t *)"implicit", -1, |
625 | 152k | YAML_PLAIN_SCALAR_STYLE); |
626 | 152k | if (!key) |
627 | 0 | goto error; |
628 | 152k | value = yaml_document_add_scalar( |
629 | 152k | &output_document, (yaml_char_t *)YAML_BOOL_TAG, |
630 | 152k | (input_event.data.sequence_start.implicit ? (yaml_char_t *)"true" |
631 | 152k | : (yaml_char_t *)"false"), |
632 | 152k | -1, YAML_PLAIN_SCALAR_STYLE); |
633 | 152k | if (!value) |
634 | 0 | goto error; |
635 | 152k | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
636 | 152k | value)) |
637 | 0 | goto error; |
638 | | |
639 | | /* Display the style information. */ |
640 | | |
641 | 152k | if (input_event.data.sequence_start.style) { |
642 | 152k | yaml_sequence_style_t style = input_event.data.sequence_start.style; |
643 | | |
644 | | /* Add 'style': <style>. */ |
645 | | |
646 | 152k | key = yaml_document_add_scalar(&output_document, NULL, |
647 | 152k | (yaml_char_t *)"style", -1, |
648 | 152k | YAML_PLAIN_SCALAR_STYLE); |
649 | 152k | if (!key) |
650 | 0 | goto error; |
651 | 152k | value = yaml_document_add_scalar( |
652 | 152k | &output_document, NULL, |
653 | 152k | (yaml_char_t *)(style == YAML_BLOCK_SEQUENCE_STYLE |
654 | 152k | ? "block" |
655 | 152k | : style == YAML_FLOW_SEQUENCE_STYLE |
656 | 29.8k | ? "flow" |
657 | 29.8k | : "unknown"), |
658 | 152k | -1, YAML_PLAIN_SCALAR_STYLE); |
659 | 152k | if (!value) |
660 | 0 | goto error; |
661 | 152k | if (!yaml_document_append_mapping_pair(&output_document, properties, |
662 | 152k | key, value)) |
663 | 0 | goto error; |
664 | 152k | } |
665 | | |
666 | 152k | break; |
667 | | |
668 | 152k | case YAML_SEQUENCE_END_EVENT: |
669 | | |
670 | | /* Add 'type': 'SEQUENCE-END'. */ |
671 | | |
672 | 122k | key = yaml_document_add_scalar(&output_document, NULL, |
673 | 122k | (yaml_char_t *)"type", -1, |
674 | 122k | YAML_PLAIN_SCALAR_STYLE); |
675 | 122k | if (!key) |
676 | 0 | goto error; |
677 | 122k | value = yaml_document_add_scalar(&output_document, NULL, |
678 | 122k | (yaml_char_t *)"SEQUENCE-END", -1, |
679 | 122k | YAML_PLAIN_SCALAR_STYLE); |
680 | 122k | if (!value) |
681 | 0 | goto error; |
682 | 122k | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
683 | 122k | value)) |
684 | 0 | goto error; |
685 | | |
686 | 122k | break; |
687 | | |
688 | 4.63M | case YAML_MAPPING_START_EVENT: |
689 | | |
690 | | /* Add 'type': 'MAPPING-START'. */ |
691 | | |
692 | 4.63M | key = yaml_document_add_scalar(&output_document, NULL, |
693 | 4.63M | (yaml_char_t *)"type", -1, |
694 | 4.63M | YAML_PLAIN_SCALAR_STYLE); |
695 | 4.63M | if (!key) |
696 | 0 | goto error; |
697 | 4.63M | value = yaml_document_add_scalar(&output_document, NULL, |
698 | 4.63M | (yaml_char_t *)"MAPPING-START", -1, |
699 | 4.63M | YAML_PLAIN_SCALAR_STYLE); |
700 | 4.63M | if (!value) |
701 | 0 | goto error; |
702 | 4.63M | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
703 | 4.63M | value)) |
704 | 0 | goto error; |
705 | | |
706 | | /* Add 'anchor': <anchor>. */ |
707 | | |
708 | 4.63M | if (input_event.data.mapping_start.anchor) { |
709 | 2.24k | key = yaml_document_add_scalar(&output_document, NULL, |
710 | 2.24k | (yaml_char_t *)"anchor", -1, |
711 | 2.24k | YAML_PLAIN_SCALAR_STYLE); |
712 | 2.24k | if (!key) |
713 | 0 | goto error; |
714 | 2.24k | value = yaml_document_add_scalar(&output_document, NULL, |
715 | 2.24k | input_event.data.mapping_start.anchor, |
716 | 2.24k | -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); |
717 | 2.24k | if (!value) |
718 | 0 | goto error; |
719 | 2.24k | if (!yaml_document_append_mapping_pair(&output_document, properties, |
720 | 2.24k | key, value)) |
721 | 0 | goto error; |
722 | 2.24k | } |
723 | | |
724 | | /* Add 'tag': <tag>. */ |
725 | | |
726 | 4.63M | if (input_event.data.mapping_start.tag) { |
727 | 13.1k | key = yaml_document_add_scalar(&output_document, NULL, |
728 | 13.1k | (yaml_char_t *)"tag", -1, |
729 | 13.1k | YAML_PLAIN_SCALAR_STYLE); |
730 | 13.1k | if (!key) |
731 | 0 | goto error; |
732 | 13.1k | value = yaml_document_add_scalar(&output_document, NULL, |
733 | 13.1k | input_event.data.mapping_start.tag, -1, |
734 | 13.1k | YAML_DOUBLE_QUOTED_SCALAR_STYLE); |
735 | 13.1k | if (!value) |
736 | 1 | goto error; |
737 | 13.1k | if (!yaml_document_append_mapping_pair(&output_document, properties, |
738 | 13.1k | key, value)) |
739 | 0 | goto error; |
740 | 13.1k | } |
741 | | |
742 | | /* Add 'implicit': <flag>. */ |
743 | | |
744 | 4.63M | key = yaml_document_add_scalar(&output_document, NULL, |
745 | 4.63M | (yaml_char_t *)"implicit", -1, |
746 | 4.63M | YAML_PLAIN_SCALAR_STYLE); |
747 | 4.63M | if (!key) |
748 | 0 | goto error; |
749 | 4.63M | value = yaml_document_add_scalar( |
750 | 4.63M | &output_document, (yaml_char_t *)YAML_BOOL_TAG, |
751 | 4.63M | (yaml_char_t *)(input_event.data.mapping_start.implicit ? "true" |
752 | 4.63M | : "false"), |
753 | 4.63M | -1, YAML_PLAIN_SCALAR_STYLE); |
754 | 4.63M | if (!value) |
755 | 0 | goto error; |
756 | 4.63M | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
757 | 4.63M | value)) |
758 | 0 | goto error; |
759 | | |
760 | | /* Display the style information. */ |
761 | | |
762 | 4.63M | if (input_event.data.sequence_start.style) { |
763 | 4.63M | yaml_sequence_style_t style = input_event.data.sequence_start.style; |
764 | | |
765 | | /* Add 'style': <style>. */ |
766 | | |
767 | 4.63M | key = yaml_document_add_scalar(&output_document, NULL, |
768 | 4.63M | (yaml_char_t *)"style", -1, |
769 | 4.63M | YAML_PLAIN_SCALAR_STYLE); |
770 | 4.63M | if (!key) |
771 | 0 | goto error; |
772 | 4.63M | value = yaml_document_add_scalar( |
773 | 4.63M | &output_document, NULL, |
774 | 4.63M | (yaml_char_t *)(style == YAML_BLOCK_MAPPING_STYLE |
775 | 4.63M | ? "block" |
776 | 4.63M | : style == YAML_FLOW_MAPPING_STYLE ? "flow" |
777 | 72.7k | : "unknown"), |
778 | 4.63M | -1, YAML_PLAIN_SCALAR_STYLE); |
779 | 4.63M | if (!value) |
780 | 0 | goto error; |
781 | 4.63M | if (!yaml_document_append_mapping_pair(&output_document, properties, |
782 | 4.63M | key, value)) |
783 | 0 | goto error; |
784 | 4.63M | } |
785 | | |
786 | 4.63M | break; |
787 | | |
788 | 4.63M | case YAML_MAPPING_END_EVENT: |
789 | | |
790 | | /* Add 'type': 'MAPPING-END'. */ |
791 | | |
792 | 4.59M | key = yaml_document_add_scalar(&output_document, NULL, |
793 | 4.59M | (yaml_char_t *)"type", -1, |
794 | 4.59M | YAML_PLAIN_SCALAR_STYLE); |
795 | 4.59M | if (!key) |
796 | 0 | goto error; |
797 | 4.59M | value = yaml_document_add_scalar(&output_document, NULL, |
798 | 4.59M | (yaml_char_t *)"MAPPING-END", -1, |
799 | 4.59M | YAML_PLAIN_SCALAR_STYLE); |
800 | 4.59M | if (!value) |
801 | 0 | goto error; |
802 | 4.59M | if (!yaml_document_append_mapping_pair(&output_document, properties, key, |
803 | 4.59M | value)) |
804 | 0 | goto error; |
805 | | |
806 | 4.59M | break; |
807 | | |
808 | 4.59M | default: |
809 | | /* It couldn't really happen. */ |
810 | 0 | break; |
811 | 25.4M | } |
812 | | |
813 | | /* Delete the event object. */ |
814 | | |
815 | 25.4M | yaml_event_delete(&input_event); |
816 | 25.4M | } |
817 | | |
818 | 4.65k | if (!yaml_emitter_dump(&emitter, &output_document)) |
819 | 2.63k | goto error; |
820 | | |
821 | 2.01k | yaml_emitter_close(&emitter); |
822 | | |
823 | 9.37k | error: |
824 | | |
825 | 9.37k | free(out.buf); |
826 | | |
827 | 9.37k | yaml_event_delete(&input_event); |
828 | 9.37k | yaml_document_delete(&output_document); |
829 | 9.37k | yaml_parser_delete(&parser); |
830 | 9.37k | yaml_emitter_delete(&emitter); |
831 | | |
832 | 9.37k | return 0; |
833 | 2.01k | } |