Line | Count | Source (jump to first uncovered line) |
1 | | |
2 | | #include "yaml_private.h" |
3 | | |
4 | | /* |
5 | | * Get the library version. |
6 | | */ |
7 | | |
8 | | YAML_DECLARE(const char *) |
9 | | yaml_get_version_string(void) |
10 | 0 | { |
11 | 0 | return YAML_VERSION_STRING; |
12 | 0 | } |
13 | | |
14 | | /* |
15 | | * Get the library version numbers. |
16 | | */ |
17 | | |
18 | | YAML_DECLARE(void) |
19 | | yaml_get_version(int *major, int *minor, int *patch) |
20 | 0 | { |
21 | 0 | *major = YAML_VERSION_MAJOR; |
22 | 0 | *minor = YAML_VERSION_MINOR; |
23 | 0 | *patch = YAML_VERSION_PATCH; |
24 | 0 | } |
25 | | |
26 | | /* |
27 | | * Allocate a dynamic memory block. |
28 | | */ |
29 | | |
30 | | YAML_DECLARE(void *) |
31 | | yaml_malloc(size_t size) |
32 | 20.6M | { |
33 | 20.6M | return malloc(size ? size : 1); |
34 | 20.6M | } |
35 | | |
36 | | /* |
37 | | * Reallocate a dynamic memory block. |
38 | | */ |
39 | | |
40 | | YAML_DECLARE(void *) |
41 | | yaml_realloc(void *ptr, size_t size) |
42 | 267k | { |
43 | 267k | return ptr ? realloc(ptr, size ? size : 1) : malloc(size ? size : 1); |
44 | 267k | } |
45 | | |
46 | | /* |
47 | | * Free a dynamic memory block. |
48 | | */ |
49 | | |
50 | | YAML_DECLARE(void) |
51 | | yaml_free(void *ptr) |
52 | 20.6M | { |
53 | 20.6M | if (ptr) free(ptr); |
54 | 20.6M | } |
55 | | |
56 | | /* |
57 | | * Duplicate a string. |
58 | | */ |
59 | | |
60 | | YAML_DECLARE(yaml_char_t *) |
61 | | yaml_strdup(const yaml_char_t *str) |
62 | 0 | { |
63 | 0 | if (!str) |
64 | 0 | return NULL; |
65 | | |
66 | 0 | return (yaml_char_t *)strdup((char *)str); |
67 | 0 | } |
68 | | |
69 | | /* |
70 | | * Extend a string. |
71 | | */ |
72 | | |
73 | | YAML_DECLARE(int) |
74 | | yaml_string_extend(yaml_char_t **start, |
75 | | yaml_char_t **pointer, yaml_char_t **end) |
76 | 262k | { |
77 | 262k | yaml_char_t *new_start = (yaml_char_t *)yaml_realloc((void*)*start, (*end - *start)*2); |
78 | | |
79 | 262k | if (!new_start) return 0; |
80 | | |
81 | 262k | memset(new_start + (*end - *start), 0, *end - *start); |
82 | | |
83 | 262k | *pointer = new_start + (*pointer - *start); |
84 | 262k | *end = new_start + (*end - *start)*2; |
85 | 262k | *start = new_start; |
86 | | |
87 | 262k | return 1; |
88 | 262k | } |
89 | | |
90 | | /* |
91 | | * Append a string B to a string A. |
92 | | */ |
93 | | |
94 | | YAML_DECLARE(int) |
95 | | yaml_string_join( |
96 | | yaml_char_t **a_start, yaml_char_t **a_pointer, yaml_char_t **a_end, |
97 | | yaml_char_t **b_start, yaml_char_t **b_pointer, SHIM(yaml_char_t **b_end)) |
98 | 2.88M | { |
99 | 2.88M | UNUSED_PARAM(b_end) |
100 | 2.88M | if (*b_start == *b_pointer) |
101 | 1.11M | return 1; |
102 | | |
103 | 1.81M | while (*a_end - *a_pointer <= *b_pointer - *b_start) { |
104 | 47.9k | if (!yaml_string_extend(a_start, a_pointer, a_end)) |
105 | 0 | return 0; |
106 | 47.9k | } |
107 | | |
108 | 1.76M | memcpy(*a_pointer, *b_start, *b_pointer - *b_start); |
109 | 1.76M | *a_pointer += *b_pointer - *b_start; |
110 | | |
111 | 1.76M | return 1; |
112 | 1.76M | } |
113 | | |
114 | | /* |
115 | | * Extend a stack. |
116 | | */ |
117 | | |
118 | | YAML_DECLARE(int) |
119 | | yaml_stack_extend(void **start, void **top, void **end) |
120 | 2.38k | { |
121 | 2.38k | void *new_start; |
122 | | |
123 | 2.38k | if ((char *)*end - (char *)*start >= INT_MAX / 2) |
124 | 0 | return 0; |
125 | | |
126 | 2.38k | new_start = yaml_realloc(*start, ((char *)*end - (char *)*start)*2); |
127 | | |
128 | 2.38k | if (!new_start) return 0; |
129 | | |
130 | 2.38k | *top = (char *)new_start + ((char *)*top - (char *)*start); |
131 | 2.38k | *end = (char *)new_start + ((char *)*end - (char *)*start)*2; |
132 | 2.38k | *start = new_start; |
133 | | |
134 | 2.38k | return 1; |
135 | 2.38k | } |
136 | | |
137 | | /* |
138 | | * Extend or move a queue. |
139 | | */ |
140 | | |
141 | | YAML_DECLARE(int) |
142 | | yaml_queue_extend(void **start, void **head, void **tail, void **end) |
143 | 655k | { |
144 | | /* Check if we need to resize the queue. */ |
145 | | |
146 | 655k | if (*start == *head && *tail == *end) { |
147 | 2.82k | void *new_start = yaml_realloc(*start, |
148 | 2.82k | ((char *)*end - (char *)*start)*2); |
149 | | |
150 | 2.82k | if (!new_start) return 0; |
151 | | |
152 | 2.82k | *head = (char *)new_start + ((char *)*head - (char *)*start); |
153 | 2.82k | *tail = (char *)new_start + ((char *)*tail - (char *)*start); |
154 | 2.82k | *end = (char *)new_start + ((char *)*end - (char *)*start)*2; |
155 | 2.82k | *start = new_start; |
156 | 2.82k | } |
157 | | |
158 | | /* Check if we need to move the queue at the beginning of the buffer. */ |
159 | | |
160 | 655k | if (*tail == *end) { |
161 | 652k | if (*head != *tail) { |
162 | 321k | memmove(*start, *head, (char *)*tail - (char *)*head); |
163 | 321k | } |
164 | 652k | *tail = (char *)*tail - (char *)*head + (char *)*start; |
165 | 652k | *head = *start; |
166 | 652k | } |
167 | | |
168 | 655k | return 1; |
169 | 655k | } |
170 | | |
171 | | |
172 | | /* |
173 | | * Create a new parser object. |
174 | | */ |
175 | | |
176 | | YAML_DECLARE(int) |
177 | | yaml_parser_initialize(yaml_parser_t *parser) |
178 | 6.90k | { |
179 | 6.90k | assert(parser); /* Non-NULL parser object expected. */ |
180 | | |
181 | 6.90k | memset(parser, 0, sizeof(yaml_parser_t)); |
182 | 6.90k | if (!BUFFER_INIT(parser, parser->raw_buffer, INPUT_RAW_BUFFER_SIZE)) |
183 | 0 | goto error; |
184 | 6.90k | if (!BUFFER_INIT(parser, parser->buffer, INPUT_BUFFER_SIZE)) |
185 | 0 | goto error; |
186 | 6.90k | if (!QUEUE_INIT(parser, parser->tokens, INITIAL_QUEUE_SIZE, yaml_token_t*)) |
187 | 0 | goto error; |
188 | 6.90k | if (!STACK_INIT(parser, parser->indents, int*)) |
189 | 0 | goto error; |
190 | 6.90k | if (!STACK_INIT(parser, parser->simple_keys, yaml_simple_key_t*)) |
191 | 0 | goto error; |
192 | 6.90k | if (!STACK_INIT(parser, parser->states, yaml_parser_state_t*)) |
193 | 0 | goto error; |
194 | 6.90k | if (!STACK_INIT(parser, parser->marks, yaml_mark_t*)) |
195 | 0 | goto error; |
196 | 6.90k | if (!STACK_INIT(parser, parser->tag_directives, yaml_tag_directive_t*)) |
197 | 0 | goto error; |
198 | | |
199 | 6.90k | return 1; |
200 | | |
201 | 0 | error: |
202 | |
|
203 | 0 | BUFFER_DEL(parser, parser->raw_buffer); |
204 | 0 | BUFFER_DEL(parser, parser->buffer); |
205 | 0 | QUEUE_DEL(parser, parser->tokens); |
206 | 0 | STACK_DEL(parser, parser->indents); |
207 | 0 | STACK_DEL(parser, parser->simple_keys); |
208 | 0 | STACK_DEL(parser, parser->states); |
209 | 0 | STACK_DEL(parser, parser->marks); |
210 | 0 | STACK_DEL(parser, parser->tag_directives); |
211 | |
|
212 | 0 | return 0; |
213 | 6.90k | } |
214 | | |
215 | | /* |
216 | | * Destroy a parser object. |
217 | | */ |
218 | | |
219 | | YAML_DECLARE(void) |
220 | | yaml_parser_delete(yaml_parser_t *parser) |
221 | 6.90k | { |
222 | 6.90k | assert(parser); /* Non-NULL parser object expected. */ |
223 | | |
224 | 6.90k | BUFFER_DEL(parser, parser->raw_buffer); |
225 | 6.90k | BUFFER_DEL(parser, parser->buffer); |
226 | 30.9k | while (!QUEUE_EMPTY(parser, parser->tokens)) { |
227 | 24.0k | yaml_token_delete(&DEQUEUE(parser, parser->tokens)); |
228 | 24.0k | } |
229 | 6.90k | QUEUE_DEL(parser, parser->tokens); |
230 | 6.90k | STACK_DEL(parser, parser->indents); |
231 | 6.90k | STACK_DEL(parser, parser->simple_keys); |
232 | 6.90k | STACK_DEL(parser, parser->states); |
233 | 6.90k | STACK_DEL(parser, parser->marks); |
234 | 6.90k | while (!STACK_EMPTY(parser, parser->tag_directives)) { |
235 | 0 | yaml_tag_directive_t tag_directive = POP(parser, parser->tag_directives); |
236 | 0 | yaml_free(tag_directive.handle); |
237 | 0 | yaml_free(tag_directive.prefix); |
238 | 0 | } |
239 | 6.90k | STACK_DEL(parser, parser->tag_directives); |
240 | | |
241 | 6.90k | memset(parser, 0, sizeof(yaml_parser_t)); |
242 | 6.90k | } |
243 | | |
244 | | /* |
245 | | * String read handler. |
246 | | */ |
247 | | |
248 | | static int |
249 | | yaml_string_read_handler(void *data, unsigned char *buffer, size_t size, |
250 | | size_t *size_read) |
251 | 20.2k | { |
252 | 20.2k | yaml_parser_t *parser = (yaml_parser_t *)data; |
253 | | |
254 | 20.2k | if (parser->input.string.current == parser->input.string.end) { |
255 | 6.47k | *size_read = 0; |
256 | 6.47k | return 1; |
257 | 6.47k | } |
258 | | |
259 | 13.7k | if (size > (size_t)(parser->input.string.end |
260 | 13.7k | - parser->input.string.current)) { |
261 | 6.79k | size = parser->input.string.end - parser->input.string.current; |
262 | 6.79k | } |
263 | | |
264 | 13.7k | memcpy(buffer, parser->input.string.current, size); |
265 | 13.7k | parser->input.string.current += size; |
266 | 13.7k | *size_read = size; |
267 | 13.7k | return 1; |
268 | 20.2k | } |
269 | | |
270 | | /* |
271 | | * File read handler. |
272 | | */ |
273 | | |
274 | | static int |
275 | | yaml_file_read_handler(void *data, unsigned char *buffer, size_t size, |
276 | | size_t *size_read) |
277 | 0 | { |
278 | 0 | yaml_parser_t *parser = (yaml_parser_t *)data; |
279 | |
|
280 | 0 | *size_read = fread(buffer, 1, size, parser->input.file); |
281 | 0 | return !ferror(parser->input.file); |
282 | 0 | } |
283 | | |
284 | | /* |
285 | | * Set a string input. |
286 | | */ |
287 | | |
288 | | YAML_DECLARE(void) |
289 | | yaml_parser_set_input_string(yaml_parser_t *parser, |
290 | | const unsigned char *input, size_t size) |
291 | 6.90k | { |
292 | 6.90k | assert(parser); /* Non-NULL parser object expected. */ |
293 | 6.90k | assert(!parser->read_handler); /* You can set the source only once. */ |
294 | 6.90k | assert(input); /* Non-NULL input string expected. */ |
295 | | |
296 | 6.90k | parser->read_handler = yaml_string_read_handler; |
297 | 6.90k | parser->read_handler_data = parser; |
298 | | |
299 | 6.90k | parser->input.string.start = input; |
300 | 6.90k | parser->input.string.current = input; |
301 | 6.90k | parser->input.string.end = input+size; |
302 | 6.90k | } |
303 | | |
304 | | /* |
305 | | * Set a file input. |
306 | | */ |
307 | | |
308 | | YAML_DECLARE(void) |
309 | | yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file) |
310 | 0 | { |
311 | 0 | assert(parser); /* Non-NULL parser object expected. */ |
312 | 0 | assert(!parser->read_handler); /* You can set the source only once. */ |
313 | 0 | assert(file); /* Non-NULL file object expected. */ |
314 | | |
315 | 0 | parser->read_handler = yaml_file_read_handler; |
316 | 0 | parser->read_handler_data = parser; |
317 | |
|
318 | 0 | parser->input.file = file; |
319 | 0 | } |
320 | | |
321 | | /* |
322 | | * Set a generic input. |
323 | | */ |
324 | | |
325 | | YAML_DECLARE(void) |
326 | | yaml_parser_set_input(yaml_parser_t *parser, |
327 | | yaml_read_handler_t *handler, void *data) |
328 | 0 | { |
329 | 0 | assert(parser); /* Non-NULL parser object expected. */ |
330 | 0 | assert(!parser->read_handler); /* You can set the source only once. */ |
331 | 0 | assert(handler); /* Non-NULL read handler expected. */ |
332 | | |
333 | 0 | parser->read_handler = handler; |
334 | 0 | parser->read_handler_data = data; |
335 | 0 | } |
336 | | |
337 | | /* |
338 | | * Set the source encoding. |
339 | | */ |
340 | | |
341 | | YAML_DECLARE(void) |
342 | | yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding) |
343 | 0 | { |
344 | 0 | assert(parser); /* Non-NULL parser object expected. */ |
345 | 0 | assert(!parser->encoding); /* Encoding is already set or detected. */ |
346 | | |
347 | 0 | parser->encoding = encoding; |
348 | 0 | } |
349 | | |
350 | | /* |
351 | | * Create a new emitter object. |
352 | | */ |
353 | | |
354 | | YAML_DECLARE(int) |
355 | | yaml_emitter_initialize(yaml_emitter_t *emitter) |
356 | 0 | { |
357 | 0 | assert(emitter); /* Non-NULL emitter object expected. */ |
358 | | |
359 | 0 | memset(emitter, 0, sizeof(yaml_emitter_t)); |
360 | 0 | if (!BUFFER_INIT(emitter, emitter->buffer, OUTPUT_BUFFER_SIZE)) |
361 | 0 | goto error; |
362 | 0 | if (!BUFFER_INIT(emitter, emitter->raw_buffer, OUTPUT_RAW_BUFFER_SIZE)) |
363 | 0 | goto error; |
364 | 0 | if (!STACK_INIT(emitter, emitter->states, yaml_emitter_state_t*)) |
365 | 0 | goto error; |
366 | 0 | if (!QUEUE_INIT(emitter, emitter->events, INITIAL_QUEUE_SIZE, yaml_event_t*)) |
367 | 0 | goto error; |
368 | 0 | if (!STACK_INIT(emitter, emitter->indents, int*)) |
369 | 0 | goto error; |
370 | 0 | if (!STACK_INIT(emitter, emitter->tag_directives, yaml_tag_directive_t*)) |
371 | 0 | goto error; |
372 | | |
373 | 0 | return 1; |
374 | | |
375 | 0 | error: |
376 | |
|
377 | 0 | BUFFER_DEL(emitter, emitter->buffer); |
378 | 0 | BUFFER_DEL(emitter, emitter->raw_buffer); |
379 | 0 | STACK_DEL(emitter, emitter->states); |
380 | 0 | QUEUE_DEL(emitter, emitter->events); |
381 | 0 | STACK_DEL(emitter, emitter->indents); |
382 | 0 | STACK_DEL(emitter, emitter->tag_directives); |
383 | |
|
384 | 0 | return 0; |
385 | 0 | } |
386 | | |
387 | | /* |
388 | | * Destroy an emitter object. |
389 | | */ |
390 | | |
391 | | YAML_DECLARE(void) |
392 | | yaml_emitter_delete(yaml_emitter_t *emitter) |
393 | 0 | { |
394 | 0 | assert(emitter); /* Non-NULL emitter object expected. */ |
395 | | |
396 | 0 | BUFFER_DEL(emitter, emitter->buffer); |
397 | 0 | BUFFER_DEL(emitter, emitter->raw_buffer); |
398 | 0 | STACK_DEL(emitter, emitter->states); |
399 | 0 | while (!QUEUE_EMPTY(emitter, emitter->events)) { |
400 | 0 | yaml_event_delete(&DEQUEUE(emitter, emitter->events)); |
401 | 0 | } |
402 | 0 | QUEUE_DEL(emitter, emitter->events); |
403 | 0 | STACK_DEL(emitter, emitter->indents); |
404 | 0 | while (!STACK_EMPTY(empty, emitter->tag_directives)) { |
405 | 0 | yaml_tag_directive_t tag_directive = POP(emitter, emitter->tag_directives); |
406 | 0 | yaml_free(tag_directive.handle); |
407 | 0 | yaml_free(tag_directive.prefix); |
408 | 0 | } |
409 | 0 | STACK_DEL(emitter, emitter->tag_directives); |
410 | 0 | yaml_free(emitter->anchors); |
411 | |
|
412 | 0 | memset(emitter, 0, sizeof(yaml_emitter_t)); |
413 | 0 | } |
414 | | |
415 | | /* |
416 | | * String write handler. |
417 | | */ |
418 | | |
419 | | static int |
420 | | yaml_string_write_handler(void *data, unsigned char *buffer, size_t size) |
421 | 0 | { |
422 | 0 | yaml_emitter_t *emitter = (yaml_emitter_t *)data; |
423 | |
|
424 | 0 | if (emitter->output.string.size - *emitter->output.string.size_written |
425 | 0 | < size) { |
426 | 0 | memcpy(emitter->output.string.buffer |
427 | 0 | + *emitter->output.string.size_written, |
428 | 0 | buffer, |
429 | 0 | emitter->output.string.size |
430 | 0 | - *emitter->output.string.size_written); |
431 | 0 | *emitter->output.string.size_written = emitter->output.string.size; |
432 | 0 | return 0; |
433 | 0 | } |
434 | | |
435 | 0 | memcpy(emitter->output.string.buffer |
436 | 0 | + *emitter->output.string.size_written, buffer, size); |
437 | 0 | *emitter->output.string.size_written += size; |
438 | 0 | return 1; |
439 | 0 | } |
440 | | |
441 | | /* |
442 | | * File write handler. |
443 | | */ |
444 | | |
445 | | static int |
446 | | yaml_file_write_handler(void *data, unsigned char *buffer, size_t size) |
447 | 0 | { |
448 | 0 | yaml_emitter_t *emitter = (yaml_emitter_t *)data; |
449 | |
|
450 | 0 | return (fwrite(buffer, 1, size, emitter->output.file) == size); |
451 | 0 | } |
452 | | /* |
453 | | * Set a string output. |
454 | | */ |
455 | | |
456 | | YAML_DECLARE(void) |
457 | | yaml_emitter_set_output_string(yaml_emitter_t *emitter, |
458 | | unsigned char *output, size_t size, size_t *size_written) |
459 | 0 | { |
460 | 0 | assert(emitter); /* Non-NULL emitter object expected. */ |
461 | 0 | assert(!emitter->write_handler); /* You can set the output only once. */ |
462 | 0 | assert(output); /* Non-NULL output string expected. */ |
463 | | |
464 | 0 | emitter->write_handler = yaml_string_write_handler; |
465 | 0 | emitter->write_handler_data = emitter; |
466 | |
|
467 | 0 | emitter->output.string.buffer = output; |
468 | 0 | emitter->output.string.size = size; |
469 | 0 | emitter->output.string.size_written = size_written; |
470 | 0 | *size_written = 0; |
471 | 0 | } |
472 | | |
473 | | /* |
474 | | * Set a file output. |
475 | | */ |
476 | | |
477 | | YAML_DECLARE(void) |
478 | | yaml_emitter_set_output_file(yaml_emitter_t *emitter, FILE *file) |
479 | 0 | { |
480 | 0 | assert(emitter); /* Non-NULL emitter object expected. */ |
481 | 0 | assert(!emitter->write_handler); /* You can set the output only once. */ |
482 | 0 | assert(file); /* Non-NULL file object expected. */ |
483 | | |
484 | 0 | emitter->write_handler = yaml_file_write_handler; |
485 | 0 | emitter->write_handler_data = emitter; |
486 | |
|
487 | 0 | emitter->output.file = file; |
488 | 0 | } |
489 | | |
490 | | /* |
491 | | * Set a generic output handler. |
492 | | */ |
493 | | |
494 | | YAML_DECLARE(void) |
495 | | yaml_emitter_set_output(yaml_emitter_t *emitter, |
496 | | yaml_write_handler_t *handler, void *data) |
497 | 0 | { |
498 | 0 | assert(emitter); /* Non-NULL emitter object expected. */ |
499 | 0 | assert(!emitter->write_handler); /* You can set the output only once. */ |
500 | 0 | assert(handler); /* Non-NULL handler object expected. */ |
501 | | |
502 | 0 | emitter->write_handler = handler; |
503 | 0 | emitter->write_handler_data = data; |
504 | 0 | } |
505 | | |
506 | | /* |
507 | | * Set the output encoding. |
508 | | */ |
509 | | |
510 | | YAML_DECLARE(void) |
511 | | yaml_emitter_set_encoding(yaml_emitter_t *emitter, yaml_encoding_t encoding) |
512 | 0 | { |
513 | 0 | assert(emitter); /* Non-NULL emitter object expected. */ |
514 | 0 | assert(!emitter->encoding); /* You can set encoding only once. */ |
515 | | |
516 | 0 | emitter->encoding = encoding; |
517 | 0 | } |
518 | | |
519 | | /* |
520 | | * Set the canonical output style. |
521 | | */ |
522 | | |
523 | | YAML_DECLARE(void) |
524 | | yaml_emitter_set_canonical(yaml_emitter_t *emitter, int canonical) |
525 | 0 | { |
526 | 0 | assert(emitter); /* Non-NULL emitter object expected. */ |
527 | | |
528 | 0 | emitter->canonical = (canonical != 0); |
529 | 0 | } |
530 | | |
531 | | /* |
532 | | * Set the indentation increment. |
533 | | */ |
534 | | |
535 | | YAML_DECLARE(void) |
536 | | yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent) |
537 | 0 | { |
538 | 0 | assert(emitter); /* Non-NULL emitter object expected. */ |
539 | | |
540 | 0 | emitter->best_indent = (1 < indent && indent < 10) ? indent : 2; |
541 | 0 | } |
542 | | |
543 | | /* |
544 | | * Set the preferred line width. |
545 | | */ |
546 | | |
547 | | YAML_DECLARE(void) |
548 | | yaml_emitter_set_width(yaml_emitter_t *emitter, int width) |
549 | 0 | { |
550 | 0 | assert(emitter); /* Non-NULL emitter object expected. */ |
551 | | |
552 | 0 | emitter->best_width = (width >= 0) ? width : -1; |
553 | 0 | } |
554 | | |
555 | | /* |
556 | | * Set if unescaped non-ASCII characters are allowed. |
557 | | */ |
558 | | |
559 | | YAML_DECLARE(void) |
560 | | yaml_emitter_set_unicode(yaml_emitter_t *emitter, int unicode) |
561 | 0 | { |
562 | 0 | assert(emitter); /* Non-NULL emitter object expected. */ |
563 | | |
564 | 0 | emitter->unicode = (unicode != 0); |
565 | 0 | } |
566 | | |
567 | | /* |
568 | | * Set the preferred line break character. |
569 | | */ |
570 | | |
571 | | YAML_DECLARE(void) |
572 | | yaml_emitter_set_break(yaml_emitter_t *emitter, yaml_break_t line_break) |
573 | 0 | { |
574 | 0 | assert(emitter); /* Non-NULL emitter object expected. */ |
575 | | |
576 | 0 | emitter->line_break = line_break; |
577 | 0 | } |
578 | | |
579 | | /* |
580 | | * Destroy a token object. |
581 | | */ |
582 | | |
583 | | YAML_DECLARE(void) |
584 | | yaml_token_delete(yaml_token_t *token) |
585 | 20.1M | { |
586 | 20.1M | assert(token); /* Non-NULL token object expected. */ |
587 | | |
588 | 20.1M | switch (token->type) |
589 | 20.1M | { |
590 | 6.76k | case YAML_TAG_DIRECTIVE_TOKEN: |
591 | 6.76k | yaml_free(token->data.tag_directive.handle); |
592 | 6.76k | yaml_free(token->data.tag_directive.prefix); |
593 | 6.76k | break; |
594 | | |
595 | 21.7k | case YAML_ALIAS_TOKEN: |
596 | 21.7k | yaml_free(token->data.alias.value); |
597 | 21.7k | break; |
598 | | |
599 | 28.4k | case YAML_ANCHOR_TOKEN: |
600 | 28.4k | yaml_free(token->data.anchor.value); |
601 | 28.4k | break; |
602 | | |
603 | 419k | case YAML_TAG_TOKEN: |
604 | 419k | yaml_free(token->data.tag.handle); |
605 | 419k | yaml_free(token->data.tag.suffix); |
606 | 419k | break; |
607 | | |
608 | 4.79M | case YAML_SCALAR_TOKEN: |
609 | 4.79M | yaml_free(token->data.scalar.value); |
610 | 4.79M | break; |
611 | | |
612 | 14.9M | default: |
613 | 14.9M | break; |
614 | 20.1M | } |
615 | | |
616 | 20.1M | memset(token, 0, sizeof(yaml_token_t)); |
617 | 20.1M | } |
618 | | |
619 | | /* |
620 | | * Check if a string is a valid UTF-8 sequence. |
621 | | * |
622 | | * Check 'reader.c' for more details on UTF-8 encoding. |
623 | | */ |
624 | | |
625 | | static int |
626 | | yaml_check_utf8(const yaml_char_t *start, size_t length) |
627 | 0 | { |
628 | 0 | const yaml_char_t *end = start+length; |
629 | 0 | const yaml_char_t *pointer = start; |
630 | |
|
631 | 0 | while (pointer < end) { |
632 | 0 | unsigned char octet; |
633 | 0 | unsigned int width; |
634 | 0 | unsigned int value; |
635 | 0 | size_t k; |
636 | |
|
637 | 0 | octet = pointer[0]; |
638 | 0 | width = (octet & 0x80) == 0x00 ? 1 : |
639 | 0 | (octet & 0xE0) == 0xC0 ? 2 : |
640 | 0 | (octet & 0xF0) == 0xE0 ? 3 : |
641 | 0 | (octet & 0xF8) == 0xF0 ? 4 : 0; |
642 | 0 | value = (octet & 0x80) == 0x00 ? octet & 0x7F : |
643 | 0 | (octet & 0xE0) == 0xC0 ? octet & 0x1F : |
644 | 0 | (octet & 0xF0) == 0xE0 ? octet & 0x0F : |
645 | 0 | (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; |
646 | 0 | if (!width) return 0; |
647 | 0 | if (pointer+width > end) return 0; |
648 | 0 | for (k = 1; k < width; k ++) { |
649 | 0 | octet = pointer[k]; |
650 | 0 | if ((octet & 0xC0) != 0x80) return 0; |
651 | 0 | value = (value << 6) + (octet & 0x3F); |
652 | 0 | } |
653 | 0 | if (!((width == 1) || |
654 | 0 | (width == 2 && value >= 0x80) || |
655 | 0 | (width == 3 && value >= 0x800) || |
656 | 0 | (width == 4 && value >= 0x10000))) return 0; |
657 | | |
658 | 0 | pointer += width; |
659 | 0 | } |
660 | | |
661 | 0 | return 1; |
662 | 0 | } |
663 | | |
664 | | /* |
665 | | * Create STREAM-START. |
666 | | */ |
667 | | |
668 | | YAML_DECLARE(int) |
669 | | yaml_stream_start_event_initialize(yaml_event_t *event, |
670 | | yaml_encoding_t encoding) |
671 | 0 | { |
672 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
673 | |
|
674 | 0 | assert(event); /* Non-NULL event object is expected. */ |
675 | | |
676 | 0 | STREAM_START_EVENT_INIT(*event, encoding, mark, mark); |
677 | |
|
678 | 0 | return 1; |
679 | 0 | } |
680 | | |
681 | | /* |
682 | | * Create STREAM-END. |
683 | | */ |
684 | | |
685 | | YAML_DECLARE(int) |
686 | | yaml_stream_end_event_initialize(yaml_event_t *event) |
687 | 0 | { |
688 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
689 | |
|
690 | 0 | assert(event); /* Non-NULL event object is expected. */ |
691 | | |
692 | 0 | STREAM_END_EVENT_INIT(*event, mark, mark); |
693 | |
|
694 | 0 | return 1; |
695 | 0 | } |
696 | | |
697 | | /* |
698 | | * Create DOCUMENT-START. |
699 | | */ |
700 | | |
701 | | YAML_DECLARE(int) |
702 | | yaml_document_start_event_initialize(yaml_event_t *event, |
703 | | yaml_version_directive_t *version_directive, |
704 | | yaml_tag_directive_t *tag_directives_start, |
705 | | yaml_tag_directive_t *tag_directives_end, |
706 | | int implicit) |
707 | 0 | { |
708 | 0 | struct { |
709 | 0 | yaml_error_type_t error; |
710 | 0 | } context; |
711 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
712 | 0 | yaml_version_directive_t *version_directive_copy = NULL; |
713 | 0 | struct { |
714 | 0 | yaml_tag_directive_t *start; |
715 | 0 | yaml_tag_directive_t *end; |
716 | 0 | yaml_tag_directive_t *top; |
717 | 0 | } tag_directives_copy = { NULL, NULL, NULL }; |
718 | 0 | yaml_tag_directive_t value = { NULL, NULL }; |
719 | |
|
720 | 0 | assert(event); /* Non-NULL event object is expected. */ |
721 | 0 | assert((tag_directives_start && tag_directives_end) || |
722 | 0 | (tag_directives_start == tag_directives_end)); |
723 | | /* Valid tag directives are expected. */ |
724 | | |
725 | 0 | if (version_directive) { |
726 | 0 | version_directive_copy = YAML_MALLOC_STATIC(yaml_version_directive_t); |
727 | 0 | if (!version_directive_copy) goto error; |
728 | 0 | version_directive_copy->major = version_directive->major; |
729 | 0 | version_directive_copy->minor = version_directive->minor; |
730 | 0 | } |
731 | | |
732 | 0 | if (tag_directives_start != tag_directives_end) { |
733 | 0 | yaml_tag_directive_t *tag_directive; |
734 | 0 | if (!STACK_INIT(&context, tag_directives_copy, yaml_tag_directive_t*)) |
735 | 0 | goto error; |
736 | 0 | for (tag_directive = tag_directives_start; |
737 | 0 | tag_directive != tag_directives_end; tag_directive ++) { |
738 | 0 | assert(tag_directive->handle); |
739 | 0 | assert(tag_directive->prefix); |
740 | 0 | if (!yaml_check_utf8(tag_directive->handle, |
741 | 0 | strlen((char *)tag_directive->handle))) |
742 | 0 | goto error; |
743 | 0 | if (!yaml_check_utf8(tag_directive->prefix, |
744 | 0 | strlen((char *)tag_directive->prefix))) |
745 | 0 | goto error; |
746 | 0 | value.handle = yaml_strdup(tag_directive->handle); |
747 | 0 | value.prefix = yaml_strdup(tag_directive->prefix); |
748 | 0 | if (!value.handle || !value.prefix) goto error; |
749 | 0 | if (!PUSH(&context, tag_directives_copy, value)) |
750 | 0 | goto error; |
751 | 0 | value.handle = NULL; |
752 | 0 | value.prefix = NULL; |
753 | 0 | } |
754 | 0 | } |
755 | | |
756 | 0 | DOCUMENT_START_EVENT_INIT(*event, version_directive_copy, |
757 | 0 | tag_directives_copy.start, tag_directives_copy.top, |
758 | 0 | implicit, mark, mark); |
759 | |
|
760 | 0 | return 1; |
761 | | |
762 | 0 | error: |
763 | 0 | yaml_free(version_directive_copy); |
764 | 0 | while (!STACK_EMPTY(context, tag_directives_copy)) { |
765 | 0 | yaml_tag_directive_t value = POP(context, tag_directives_copy); |
766 | 0 | yaml_free(value.handle); |
767 | 0 | yaml_free(value.prefix); |
768 | 0 | } |
769 | 0 | STACK_DEL(context, tag_directives_copy); |
770 | 0 | yaml_free(value.handle); |
771 | 0 | yaml_free(value.prefix); |
772 | |
|
773 | 0 | return 0; |
774 | 0 | } |
775 | | |
776 | | /* |
777 | | * Create DOCUMENT-END. |
778 | | */ |
779 | | |
780 | | YAML_DECLARE(int) |
781 | | yaml_document_end_event_initialize(yaml_event_t *event, int implicit) |
782 | 0 | { |
783 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
784 | |
|
785 | 0 | assert(event); /* Non-NULL emitter object is expected. */ |
786 | | |
787 | 0 | DOCUMENT_END_EVENT_INIT(*event, implicit, mark, mark); |
788 | |
|
789 | 0 | return 1; |
790 | 0 | } |
791 | | |
792 | | /* |
793 | | * Create ALIAS. |
794 | | */ |
795 | | |
796 | | YAML_DECLARE(int) |
797 | | yaml_alias_event_initialize(yaml_event_t *event, const yaml_char_t *anchor) |
798 | 0 | { |
799 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
800 | 0 | yaml_char_t *anchor_copy = NULL; |
801 | |
|
802 | 0 | assert(event); /* Non-NULL event object is expected. */ |
803 | 0 | assert(anchor); /* Non-NULL anchor is expected. */ |
804 | | |
805 | 0 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) return 0; |
806 | | |
807 | 0 | anchor_copy = yaml_strdup(anchor); |
808 | 0 | if (!anchor_copy) |
809 | 0 | return 0; |
810 | | |
811 | 0 | ALIAS_EVENT_INIT(*event, anchor_copy, mark, mark); |
812 | |
|
813 | 0 | return 1; |
814 | 0 | } |
815 | | |
816 | | /* |
817 | | * Create SCALAR. |
818 | | */ |
819 | | |
820 | | YAML_DECLARE(int) |
821 | | yaml_scalar_event_initialize(yaml_event_t *event, |
822 | | const yaml_char_t *anchor, const yaml_char_t *tag, |
823 | | const yaml_char_t *value, int length, |
824 | | int plain_implicit, int quoted_implicit, |
825 | | yaml_scalar_style_t style) |
826 | 0 | { |
827 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
828 | 0 | yaml_char_t *anchor_copy = NULL; |
829 | 0 | yaml_char_t *tag_copy = NULL; |
830 | 0 | yaml_char_t *value_copy = NULL; |
831 | |
|
832 | 0 | assert(event); /* Non-NULL event object is expected. */ |
833 | 0 | assert(value); /* Non-NULL anchor is expected. */ |
834 | | |
835 | 0 | if (anchor) { |
836 | 0 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; |
837 | 0 | anchor_copy = yaml_strdup(anchor); |
838 | 0 | if (!anchor_copy) goto error; |
839 | 0 | } |
840 | | |
841 | 0 | if (tag) { |
842 | 0 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
843 | 0 | tag_copy = yaml_strdup(tag); |
844 | 0 | if (!tag_copy) goto error; |
845 | 0 | } |
846 | | |
847 | 0 | if (length < 0) { |
848 | 0 | length = strlen((char *)value); |
849 | 0 | } |
850 | |
|
851 | 0 | if (!yaml_check_utf8(value, length)) goto error; |
852 | 0 | value_copy = YAML_MALLOC(length+1); |
853 | 0 | if (!value_copy) goto error; |
854 | 0 | memcpy(value_copy, value, length); |
855 | 0 | value_copy[length] = '\0'; |
856 | |
|
857 | 0 | SCALAR_EVENT_INIT(*event, anchor_copy, tag_copy, value_copy, length, |
858 | 0 | plain_implicit, quoted_implicit, style, mark, mark); |
859 | |
|
860 | 0 | return 1; |
861 | | |
862 | 0 | error: |
863 | 0 | yaml_free(anchor_copy); |
864 | 0 | yaml_free(tag_copy); |
865 | 0 | yaml_free(value_copy); |
866 | |
|
867 | 0 | return 0; |
868 | 0 | } |
869 | | |
870 | | /* |
871 | | * Create SEQUENCE-START. |
872 | | */ |
873 | | |
874 | | YAML_DECLARE(int) |
875 | | yaml_sequence_start_event_initialize(yaml_event_t *event, |
876 | | const yaml_char_t *anchor, const yaml_char_t *tag, int implicit, |
877 | | yaml_sequence_style_t style) |
878 | 0 | { |
879 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
880 | 0 | yaml_char_t *anchor_copy = NULL; |
881 | 0 | yaml_char_t *tag_copy = NULL; |
882 | |
|
883 | 0 | assert(event); /* Non-NULL event object is expected. */ |
884 | | |
885 | 0 | if (anchor) { |
886 | 0 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; |
887 | 0 | anchor_copy = yaml_strdup(anchor); |
888 | 0 | if (!anchor_copy) goto error; |
889 | 0 | } |
890 | | |
891 | 0 | if (tag) { |
892 | 0 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
893 | 0 | tag_copy = yaml_strdup(tag); |
894 | 0 | if (!tag_copy) goto error; |
895 | 0 | } |
896 | | |
897 | 0 | SEQUENCE_START_EVENT_INIT(*event, anchor_copy, tag_copy, |
898 | 0 | implicit, style, mark, mark); |
899 | |
|
900 | 0 | return 1; |
901 | | |
902 | 0 | error: |
903 | 0 | yaml_free(anchor_copy); |
904 | 0 | yaml_free(tag_copy); |
905 | |
|
906 | 0 | return 0; |
907 | 0 | } |
908 | | |
909 | | /* |
910 | | * Create SEQUENCE-END. |
911 | | */ |
912 | | |
913 | | YAML_DECLARE(int) |
914 | | yaml_sequence_end_event_initialize(yaml_event_t *event) |
915 | 0 | { |
916 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
917 | |
|
918 | 0 | assert(event); /* Non-NULL event object is expected. */ |
919 | | |
920 | 0 | SEQUENCE_END_EVENT_INIT(*event, mark, mark); |
921 | |
|
922 | 0 | return 1; |
923 | 0 | } |
924 | | |
925 | | /* |
926 | | * Create MAPPING-START. |
927 | | */ |
928 | | |
929 | | YAML_DECLARE(int) |
930 | | yaml_mapping_start_event_initialize(yaml_event_t *event, |
931 | | const yaml_char_t *anchor, const yaml_char_t *tag, int implicit, |
932 | | yaml_mapping_style_t style) |
933 | 0 | { |
934 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
935 | 0 | yaml_char_t *anchor_copy = NULL; |
936 | 0 | yaml_char_t *tag_copy = NULL; |
937 | |
|
938 | 0 | assert(event); /* Non-NULL event object is expected. */ |
939 | | |
940 | 0 | if (anchor) { |
941 | 0 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; |
942 | 0 | anchor_copy = yaml_strdup(anchor); |
943 | 0 | if (!anchor_copy) goto error; |
944 | 0 | } |
945 | | |
946 | 0 | if (tag) { |
947 | 0 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
948 | 0 | tag_copy = yaml_strdup(tag); |
949 | 0 | if (!tag_copy) goto error; |
950 | 0 | } |
951 | | |
952 | 0 | MAPPING_START_EVENT_INIT(*event, anchor_copy, tag_copy, |
953 | 0 | implicit, style, mark, mark); |
954 | |
|
955 | 0 | return 1; |
956 | | |
957 | 0 | error: |
958 | 0 | yaml_free(anchor_copy); |
959 | 0 | yaml_free(tag_copy); |
960 | |
|
961 | 0 | return 0; |
962 | 0 | } |
963 | | |
964 | | /* |
965 | | * Create MAPPING-END. |
966 | | */ |
967 | | |
968 | | YAML_DECLARE(int) |
969 | | yaml_mapping_end_event_initialize(yaml_event_t *event) |
970 | 0 | { |
971 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
972 | |
|
973 | 0 | assert(event); /* Non-NULL event object is expected. */ |
974 | | |
975 | 0 | MAPPING_END_EVENT_INIT(*event, mark, mark); |
976 | |
|
977 | 0 | return 1; |
978 | 0 | } |
979 | | |
980 | | /* |
981 | | * Destroy an event object. |
982 | | */ |
983 | | |
984 | | YAML_DECLARE(void) |
985 | | yaml_event_delete(yaml_event_t *event) |
986 | 0 | { |
987 | 0 | yaml_tag_directive_t *tag_directive; |
988 | |
|
989 | 0 | assert(event); /* Non-NULL event object expected. */ |
990 | | |
991 | 0 | switch (event->type) |
992 | 0 | { |
993 | 0 | case YAML_DOCUMENT_START_EVENT: |
994 | 0 | yaml_free(event->data.document_start.version_directive); |
995 | 0 | for (tag_directive = event->data.document_start.tag_directives.start; |
996 | 0 | tag_directive != event->data.document_start.tag_directives.end; |
997 | 0 | tag_directive++) { |
998 | 0 | yaml_free(tag_directive->handle); |
999 | 0 | yaml_free(tag_directive->prefix); |
1000 | 0 | } |
1001 | 0 | yaml_free(event->data.document_start.tag_directives.start); |
1002 | 0 | break; |
1003 | | |
1004 | 0 | case YAML_ALIAS_EVENT: |
1005 | 0 | yaml_free(event->data.alias.anchor); |
1006 | 0 | break; |
1007 | | |
1008 | 0 | case YAML_SCALAR_EVENT: |
1009 | 0 | yaml_free(event->data.scalar.anchor); |
1010 | 0 | yaml_free(event->data.scalar.tag); |
1011 | 0 | yaml_free(event->data.scalar.value); |
1012 | 0 | break; |
1013 | | |
1014 | 0 | case YAML_SEQUENCE_START_EVENT: |
1015 | 0 | yaml_free(event->data.sequence_start.anchor); |
1016 | 0 | yaml_free(event->data.sequence_start.tag); |
1017 | 0 | break; |
1018 | | |
1019 | 0 | case YAML_MAPPING_START_EVENT: |
1020 | 0 | yaml_free(event->data.mapping_start.anchor); |
1021 | 0 | yaml_free(event->data.mapping_start.tag); |
1022 | 0 | break; |
1023 | | |
1024 | 0 | default: |
1025 | 0 | break; |
1026 | 0 | } |
1027 | | |
1028 | 0 | memset(event, 0, sizeof(yaml_event_t)); |
1029 | 0 | } |
1030 | | |
1031 | | /* |
1032 | | * Create a document object. |
1033 | | */ |
1034 | | |
1035 | | YAML_DECLARE(int) |
1036 | | yaml_document_initialize(yaml_document_t *document, |
1037 | | yaml_version_directive_t *version_directive, |
1038 | | yaml_tag_directive_t *tag_directives_start, |
1039 | | yaml_tag_directive_t *tag_directives_end, |
1040 | | int start_implicit, int end_implicit) |
1041 | 0 | { |
1042 | 0 | struct { |
1043 | 0 | yaml_error_type_t error; |
1044 | 0 | } context; |
1045 | 0 | struct { |
1046 | 0 | yaml_node_t *start; |
1047 | 0 | yaml_node_t *end; |
1048 | 0 | yaml_node_t *top; |
1049 | 0 | } nodes = { NULL, NULL, NULL }; |
1050 | 0 | yaml_version_directive_t *version_directive_copy = NULL; |
1051 | 0 | struct { |
1052 | 0 | yaml_tag_directive_t *start; |
1053 | 0 | yaml_tag_directive_t *end; |
1054 | 0 | yaml_tag_directive_t *top; |
1055 | 0 | } tag_directives_copy = { NULL, NULL, NULL }; |
1056 | 0 | yaml_tag_directive_t value = { NULL, NULL }; |
1057 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
1058 | |
|
1059 | 0 | assert(document); /* Non-NULL document object is expected. */ |
1060 | 0 | assert((tag_directives_start && tag_directives_end) || |
1061 | 0 | (tag_directives_start == tag_directives_end)); |
1062 | | /* Valid tag directives are expected. */ |
1063 | | |
1064 | 0 | if (!STACK_INIT(&context, nodes, yaml_node_t*)) goto error; |
1065 | | |
1066 | 0 | if (version_directive) { |
1067 | 0 | version_directive_copy = YAML_MALLOC_STATIC(yaml_version_directive_t); |
1068 | 0 | if (!version_directive_copy) goto error; |
1069 | 0 | version_directive_copy->major = version_directive->major; |
1070 | 0 | version_directive_copy->minor = version_directive->minor; |
1071 | 0 | } |
1072 | | |
1073 | 0 | if (tag_directives_start != tag_directives_end) { |
1074 | 0 | yaml_tag_directive_t *tag_directive; |
1075 | 0 | if (!STACK_INIT(&context, tag_directives_copy, yaml_tag_directive_t*)) |
1076 | 0 | goto error; |
1077 | 0 | for (tag_directive = tag_directives_start; |
1078 | 0 | tag_directive != tag_directives_end; tag_directive ++) { |
1079 | 0 | assert(tag_directive->handle); |
1080 | 0 | assert(tag_directive->prefix); |
1081 | 0 | if (!yaml_check_utf8(tag_directive->handle, |
1082 | 0 | strlen((char *)tag_directive->handle))) |
1083 | 0 | goto error; |
1084 | 0 | if (!yaml_check_utf8(tag_directive->prefix, |
1085 | 0 | strlen((char *)tag_directive->prefix))) |
1086 | 0 | goto error; |
1087 | 0 | value.handle = yaml_strdup(tag_directive->handle); |
1088 | 0 | value.prefix = yaml_strdup(tag_directive->prefix); |
1089 | 0 | if (!value.handle || !value.prefix) goto error; |
1090 | 0 | if (!PUSH(&context, tag_directives_copy, value)) |
1091 | 0 | goto error; |
1092 | 0 | value.handle = NULL; |
1093 | 0 | value.prefix = NULL; |
1094 | 0 | } |
1095 | 0 | } |
1096 | | |
1097 | 0 | DOCUMENT_INIT(*document, nodes.start, nodes.end, version_directive_copy, |
1098 | 0 | tag_directives_copy.start, tag_directives_copy.top, |
1099 | 0 | start_implicit, end_implicit, mark, mark); |
1100 | |
|
1101 | 0 | return 1; |
1102 | | |
1103 | 0 | error: |
1104 | 0 | STACK_DEL(&context, nodes); |
1105 | 0 | yaml_free(version_directive_copy); |
1106 | 0 | while (!STACK_EMPTY(&context, tag_directives_copy)) { |
1107 | 0 | yaml_tag_directive_t value = POP(&context, tag_directives_copy); |
1108 | 0 | yaml_free(value.handle); |
1109 | 0 | yaml_free(value.prefix); |
1110 | 0 | } |
1111 | 0 | STACK_DEL(&context, tag_directives_copy); |
1112 | 0 | yaml_free(value.handle); |
1113 | 0 | yaml_free(value.prefix); |
1114 | |
|
1115 | 0 | return 0; |
1116 | 0 | } |
1117 | | |
1118 | | /* |
1119 | | * Destroy a document object. |
1120 | | */ |
1121 | | |
1122 | | YAML_DECLARE(void) |
1123 | | yaml_document_delete(yaml_document_t *document) |
1124 | 0 | { |
1125 | 0 | yaml_tag_directive_t *tag_directive; |
1126 | |
|
1127 | 0 | assert(document); /* Non-NULL document object is expected. */ |
1128 | | |
1129 | 0 | while (!STACK_EMPTY(&context, document->nodes)) { |
1130 | 0 | yaml_node_t node = POP(&context, document->nodes); |
1131 | 0 | yaml_free(node.tag); |
1132 | 0 | switch (node.type) { |
1133 | 0 | case YAML_SCALAR_NODE: |
1134 | 0 | yaml_free(node.data.scalar.value); |
1135 | 0 | break; |
1136 | 0 | case YAML_SEQUENCE_NODE: |
1137 | 0 | STACK_DEL(&context, node.data.sequence.items); |
1138 | 0 | break; |
1139 | 0 | case YAML_MAPPING_NODE: |
1140 | 0 | STACK_DEL(&context, node.data.mapping.pairs); |
1141 | 0 | break; |
1142 | 0 | default: |
1143 | 0 | assert(0); /* Should not happen. */ |
1144 | 0 | } |
1145 | 0 | } |
1146 | 0 | STACK_DEL(&context, document->nodes); |
1147 | |
|
1148 | 0 | yaml_free(document->version_directive); |
1149 | 0 | for (tag_directive = document->tag_directives.start; |
1150 | 0 | tag_directive != document->tag_directives.end; |
1151 | 0 | tag_directive++) { |
1152 | 0 | yaml_free(tag_directive->handle); |
1153 | 0 | yaml_free(tag_directive->prefix); |
1154 | 0 | } |
1155 | 0 | yaml_free(document->tag_directives.start); |
1156 | |
|
1157 | 0 | memset(document, 0, sizeof(yaml_document_t)); |
1158 | 0 | } |
1159 | | |
1160 | | /** |
1161 | | * Get a document node. |
1162 | | */ |
1163 | | |
1164 | | YAML_DECLARE(yaml_node_t *) |
1165 | | yaml_document_get_node(yaml_document_t *document, int index) |
1166 | 0 | { |
1167 | 0 | assert(document); /* Non-NULL document object is expected. */ |
1168 | | |
1169 | 0 | if (index > 0 && document->nodes.start + index <= document->nodes.top) { |
1170 | 0 | return document->nodes.start + index - 1; |
1171 | 0 | } |
1172 | 0 | return NULL; |
1173 | 0 | } |
1174 | | |
1175 | | /** |
1176 | | * Get the root object. |
1177 | | */ |
1178 | | |
1179 | | YAML_DECLARE(yaml_node_t *) |
1180 | | yaml_document_get_root_node(yaml_document_t *document) |
1181 | 0 | { |
1182 | 0 | assert(document); /* Non-NULL document object is expected. */ |
1183 | | |
1184 | 0 | if (document->nodes.top != document->nodes.start) { |
1185 | 0 | return document->nodes.start; |
1186 | 0 | } |
1187 | 0 | return NULL; |
1188 | 0 | } |
1189 | | |
1190 | | /* |
1191 | | * Add a scalar node to a document. |
1192 | | */ |
1193 | | |
1194 | | YAML_DECLARE(int) |
1195 | | yaml_document_add_scalar(yaml_document_t *document, |
1196 | | const yaml_char_t *tag, const yaml_char_t *value, int length, |
1197 | | yaml_scalar_style_t style) |
1198 | 0 | { |
1199 | 0 | struct { |
1200 | 0 | yaml_error_type_t error; |
1201 | 0 | } context; |
1202 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
1203 | 0 | yaml_char_t *tag_copy = NULL; |
1204 | 0 | yaml_char_t *value_copy = NULL; |
1205 | 0 | yaml_node_t node; |
1206 | |
|
1207 | 0 | assert(document); /* Non-NULL document object is expected. */ |
1208 | 0 | assert(value); /* Non-NULL value is expected. */ |
1209 | | |
1210 | 0 | if (!tag) { |
1211 | 0 | tag = (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG; |
1212 | 0 | } |
1213 | |
|
1214 | 0 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
1215 | 0 | tag_copy = yaml_strdup(tag); |
1216 | 0 | if (!tag_copy) goto error; |
1217 | | |
1218 | 0 | if (length < 0) { |
1219 | 0 | length = strlen((char *)value); |
1220 | 0 | } |
1221 | |
|
1222 | 0 | if (!yaml_check_utf8(value, length)) goto error; |
1223 | 0 | value_copy = YAML_MALLOC(length+1); |
1224 | 0 | if (!value_copy) goto error; |
1225 | 0 | memcpy(value_copy, value, length); |
1226 | 0 | value_copy[length] = '\0'; |
1227 | |
|
1228 | 0 | SCALAR_NODE_INIT(node, tag_copy, value_copy, length, style, mark, mark); |
1229 | 0 | if (!PUSH(&context, document->nodes, node)) goto error; |
1230 | | |
1231 | 0 | return document->nodes.top - document->nodes.start; |
1232 | | |
1233 | 0 | error: |
1234 | 0 | yaml_free(tag_copy); |
1235 | 0 | yaml_free(value_copy); |
1236 | |
|
1237 | 0 | return 0; |
1238 | 0 | } |
1239 | | |
1240 | | /* |
1241 | | * Add a sequence node to a document. |
1242 | | */ |
1243 | | |
1244 | | YAML_DECLARE(int) |
1245 | | yaml_document_add_sequence(yaml_document_t *document, |
1246 | | const yaml_char_t *tag, yaml_sequence_style_t style) |
1247 | 0 | { |
1248 | 0 | struct { |
1249 | 0 | yaml_error_type_t error; |
1250 | 0 | } context; |
1251 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
1252 | 0 | yaml_char_t *tag_copy = NULL; |
1253 | 0 | struct { |
1254 | 0 | yaml_node_item_t *start; |
1255 | 0 | yaml_node_item_t *end; |
1256 | 0 | yaml_node_item_t *top; |
1257 | 0 | } items = { NULL, NULL, NULL }; |
1258 | 0 | yaml_node_t node; |
1259 | |
|
1260 | 0 | assert(document); /* Non-NULL document object is expected. */ |
1261 | | |
1262 | 0 | if (!tag) { |
1263 | 0 | tag = (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG; |
1264 | 0 | } |
1265 | |
|
1266 | 0 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
1267 | 0 | tag_copy = yaml_strdup(tag); |
1268 | 0 | if (!tag_copy) goto error; |
1269 | | |
1270 | 0 | if (!STACK_INIT(&context, items, yaml_node_item_t*)) goto error; |
1271 | | |
1272 | 0 | SEQUENCE_NODE_INIT(node, tag_copy, items.start, items.end, |
1273 | 0 | style, mark, mark); |
1274 | 0 | if (!PUSH(&context, document->nodes, node)) goto error; |
1275 | | |
1276 | 0 | return document->nodes.top - document->nodes.start; |
1277 | | |
1278 | 0 | error: |
1279 | 0 | STACK_DEL(&context, items); |
1280 | 0 | yaml_free(tag_copy); |
1281 | |
|
1282 | 0 | return 0; |
1283 | 0 | } |
1284 | | |
1285 | | /* |
1286 | | * Add a mapping node to a document. |
1287 | | */ |
1288 | | |
1289 | | YAML_DECLARE(int) |
1290 | | yaml_document_add_mapping(yaml_document_t *document, |
1291 | | const yaml_char_t *tag, yaml_mapping_style_t style) |
1292 | 0 | { |
1293 | 0 | struct { |
1294 | 0 | yaml_error_type_t error; |
1295 | 0 | } context; |
1296 | 0 | yaml_mark_t mark = { 0, 0, 0 }; |
1297 | 0 | yaml_char_t *tag_copy = NULL; |
1298 | 0 | struct { |
1299 | 0 | yaml_node_pair_t *start; |
1300 | 0 | yaml_node_pair_t *end; |
1301 | 0 | yaml_node_pair_t *top; |
1302 | 0 | } pairs = { NULL, NULL, NULL }; |
1303 | 0 | yaml_node_t node; |
1304 | |
|
1305 | 0 | assert(document); /* Non-NULL document object is expected. */ |
1306 | | |
1307 | 0 | if (!tag) { |
1308 | 0 | tag = (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG; |
1309 | 0 | } |
1310 | |
|
1311 | 0 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
1312 | 0 | tag_copy = yaml_strdup(tag); |
1313 | 0 | if (!tag_copy) goto error; |
1314 | | |
1315 | 0 | if (!STACK_INIT(&context, pairs, yaml_node_pair_t*)) goto error; |
1316 | | |
1317 | 0 | MAPPING_NODE_INIT(node, tag_copy, pairs.start, pairs.end, |
1318 | 0 | style, mark, mark); |
1319 | 0 | if (!PUSH(&context, document->nodes, node)) goto error; |
1320 | | |
1321 | 0 | return document->nodes.top - document->nodes.start; |
1322 | | |
1323 | 0 | error: |
1324 | 0 | STACK_DEL(&context, pairs); |
1325 | 0 | yaml_free(tag_copy); |
1326 | |
|
1327 | 0 | return 0; |
1328 | 0 | } |
1329 | | |
1330 | | /* |
1331 | | * Append an item to a sequence node. |
1332 | | */ |
1333 | | |
1334 | | YAML_DECLARE(int) |
1335 | | yaml_document_append_sequence_item(yaml_document_t *document, |
1336 | | int sequence, int item) |
1337 | 0 | { |
1338 | 0 | struct { |
1339 | 0 | yaml_error_type_t error; |
1340 | 0 | } context; |
1341 | |
|
1342 | 0 | assert(document); /* Non-NULL document is required. */ |
1343 | 0 | assert(sequence > 0 |
1344 | 0 | && document->nodes.start + sequence <= document->nodes.top); |
1345 | | /* Valid sequence id is required. */ |
1346 | 0 | assert(document->nodes.start[sequence-1].type == YAML_SEQUENCE_NODE); |
1347 | | /* A sequence node is required. */ |
1348 | 0 | assert(item > 0 && document->nodes.start + item <= document->nodes.top); |
1349 | | /* Valid item id is required. */ |
1350 | | |
1351 | 0 | if (!PUSH(&context, |
1352 | 0 | document->nodes.start[sequence-1].data.sequence.items, item)) |
1353 | 0 | return 0; |
1354 | | |
1355 | 0 | return 1; |
1356 | 0 | } |
1357 | | |
1358 | | /* |
1359 | | * Append a pair of a key and a value to a mapping node. |
1360 | | */ |
1361 | | |
1362 | | YAML_DECLARE(int) |
1363 | | yaml_document_append_mapping_pair(yaml_document_t *document, |
1364 | | int mapping, int key, int value) |
1365 | 0 | { |
1366 | 0 | struct { |
1367 | 0 | yaml_error_type_t error; |
1368 | 0 | } context; |
1369 | |
|
1370 | 0 | yaml_node_pair_t pair; |
1371 | |
|
1372 | 0 | assert(document); /* Non-NULL document is required. */ |
1373 | 0 | assert(mapping > 0 |
1374 | 0 | && document->nodes.start + mapping <= document->nodes.top); |
1375 | | /* Valid mapping id is required. */ |
1376 | 0 | assert(document->nodes.start[mapping-1].type == YAML_MAPPING_NODE); |
1377 | | /* A mapping node is required. */ |
1378 | 0 | assert(key > 0 && document->nodes.start + key <= document->nodes.top); |
1379 | | /* Valid key id is required. */ |
1380 | 0 | assert(value > 0 && document->nodes.start + value <= document->nodes.top); |
1381 | | /* Valid value id is required. */ |
1382 | | |
1383 | 0 | pair.key = key; |
1384 | 0 | pair.value = value; |
1385 | |
|
1386 | 0 | if (!PUSH(&context, |
1387 | 0 | document->nodes.start[mapping-1].data.mapping.pairs, pair)) |
1388 | 0 | return 0; |
1389 | | |
1390 | 0 | return 1; |
1391 | 0 | } |
1392 | | |
1393 | | |