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