]>
Commit | Line | Data |
---|---|---|
a51447c9 | 1 | |
625fcfe9 | 2 | #include "yaml_private.h" |
a51447c9 | 3 | |
625fcfe9 KS |
4 | /* |
5 | * Get the library version. | |
6 | */ | |
95b98ba9 | 7 | |
5eff53a4 KS |
8 | YAML_DECLARE(const char *) |
9 | yaml_get_version_string(void) | |
10 | { | |
11 | return YAML_VERSION_STRING; | |
12 | } | |
13 | ||
625fcfe9 KS |
14 | /* |
15 | * Get the library version numbers. | |
16 | */ | |
17 | ||
5eff53a4 KS |
18 | YAML_DECLARE(void) |
19 | yaml_get_version(int *major, int *minor, int *patch) | |
20 | { | |
21 | *major = YAML_VERSION_MAJOR; | |
22 | *minor = YAML_VERSION_MINOR; | |
23 | *patch = YAML_VERSION_PATCH; | |
24 | } | |
25 | ||
95b98ba9 KS |
26 | /* |
27 | * Allocate a dynamic memory block. | |
28 | */ | |
29 | ||
f642fd11 | 30 | YAML_DECLARE(void *) |
95b98ba9 KS |
31 | yaml_malloc(size_t size) |
32 | { | |
33 | return malloc(size ? size : 1); | |
34 | } | |
35 | ||
36 | /* | |
37 | * Reallocate a dynamic memory block. | |
38 | */ | |
39 | ||
f642fd11 | 40 | YAML_DECLARE(void *) |
95b98ba9 KS |
41 | yaml_realloc(void *ptr, size_t size) |
42 | { | |
43 | return ptr ? realloc(ptr, size ? size : 1) : malloc(size ? size : 1); | |
44 | } | |
45 | ||
a51447c9 | 46 | /* |
95b98ba9 KS |
47 | * Free a dynamic memory block. |
48 | */ | |
49 | ||
f642fd11 | 50 | YAML_DECLARE(void) |
95b98ba9 KS |
51 | yaml_free(void *ptr) |
52 | { | |
53 | if (ptr) free(ptr); | |
54 | } | |
55 | ||
56 | /* | |
625fcfe9 | 57 | * Duplicate a string. |
a51447c9 KS |
58 | */ |
59 | ||
cf616166 KS |
60 | YAML_DECLARE(yaml_char_t *) |
61 | yaml_strdup(const yaml_char_t *str) | |
a51447c9 | 62 | { |
cf616166 KS |
63 | if (!str) |
64 | return NULL; | |
65 | ||
66 | return (yaml_char_t *)strdup((char *)str); | |
625fcfe9 | 67 | } |
a51447c9 | 68 | |
625fcfe9 KS |
69 | /* |
70 | * Extend a string. | |
71 | */ | |
6eb1ded4 | 72 | |
625fcfe9 KS |
73 | YAML_DECLARE(int) |
74 | yaml_string_extend(yaml_char_t **start, | |
75 | yaml_char_t **pointer, yaml_char_t **end) | |
76 | { | |
77 | void *new_start = yaml_realloc(*start, (*end - *start)*2); | |
f2b59d4d | 78 | |
625fcfe9 | 79 | if (!new_start) return 0; |
6eb1ded4 | 80 | |
625fcfe9 | 81 | memset(new_start + (*end - *start), 0, *end - *start); |
6eb1ded4 | 82 | |
625fcfe9 KS |
83 | *pointer = new_start + (*pointer - *start); |
84 | *end = new_start + (*end - *start)*2; | |
85 | *start = new_start; | |
f2b59d4d | 86 | |
625fcfe9 KS |
87 | return 1; |
88 | } | |
6eb1ded4 | 89 | |
625fcfe9 KS |
90 | /* |
91 | * Append a string B to a string A. | |
92 | */ | |
f2b59d4d | 93 | |
625fcfe9 KS |
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, yaml_char_t **b_end) | |
98 | { | |
99 | if (*b_start == *b_pointer) | |
100 | return 1; | |
f2b59d4d | 101 | |
625fcfe9 KS |
102 | while (*a_end - *a_pointer <= *b_pointer - *b_start) { |
103 | if (!yaml_string_extend(a_start, a_pointer, a_end)) | |
104 | return 0; | |
105 | } | |
f2b59d4d | 106 | |
625fcfe9 KS |
107 | memcpy(*a_pointer, *b_start, *b_pointer - *b_start); |
108 | *a_pointer += *b_pointer - *b_start; | |
f2b59d4d | 109 | |
625fcfe9 KS |
110 | return 1; |
111 | } | |
f2b59d4d | 112 | |
625fcfe9 KS |
113 | /* |
114 | * Extend a stack. | |
115 | */ | |
f2b59d4d | 116 | |
625fcfe9 KS |
117 | YAML_DECLARE(int) |
118 | yaml_stack_extend(void **start, void **top, void **end) | |
119 | { | |
120 | void *new_start = yaml_realloc(*start, (*end - *start)*2); | |
f2b59d4d | 121 | |
625fcfe9 | 122 | if (!new_start) return 0; |
f2b59d4d | 123 | |
625fcfe9 KS |
124 | *top = new_start + (*top - *start); |
125 | *end = new_start + (*end - *start)*2; | |
126 | *start = new_start; | |
f2b59d4d | 127 | |
625fcfe9 KS |
128 | return 1; |
129 | } | |
1eb01be7 | 130 | |
625fcfe9 KS |
131 | /* |
132 | * Extend or move a queue. | |
133 | */ | |
1eb01be7 | 134 | |
625fcfe9 KS |
135 | YAML_DECLARE(int) |
136 | yaml_queue_extend(void **start, void **head, void **tail, void **end) | |
137 | { | |
138 | /* Check if we need to resize the queue. */ | |
1eb01be7 | 139 | |
625fcfe9 KS |
140 | if (*start == *head && *tail == *end) { |
141 | void *new_start = yaml_realloc(*start, (*end - *start)*2); | |
1eb01be7 | 142 | |
625fcfe9 | 143 | if (!new_start) return 0; |
1eb01be7 | 144 | |
625fcfe9 KS |
145 | *head = new_start + (*head - *start); |
146 | *tail = new_start + (*tail - *start); | |
147 | *end = new_start + (*end - *start)*2; | |
148 | *start = new_start; | |
149 | } | |
ab01bac8 | 150 | |
625fcfe9 | 151 | /* Check if we need to move the queue at the beginning of the buffer. */ |
ab01bac8 | 152 | |
625fcfe9 KS |
153 | if (*tail == *end) { |
154 | if (*head != *tail) { | |
155 | memmove(*start, *head, *tail - *head); | |
156 | } | |
157 | *tail -= *head - *start; | |
158 | *head = *start; | |
159 | } | |
ab01bac8 | 160 | |
625fcfe9 KS |
161 | return 1; |
162 | } | |
1eb01be7 | 163 | |
1eb01be7 | 164 | |
625fcfe9 KS |
165 | /* |
166 | * Create a new parser object. | |
167 | */ | |
1eb01be7 | 168 | |
625fcfe9 KS |
169 | YAML_DECLARE(int) |
170 | yaml_parser_initialize(yaml_parser_t *parser) | |
171 | { | |
172 | assert(parser); /* Non-NULL parser object expected. */ | |
f2b59d4d | 173 | |
625fcfe9 | 174 | memset(parser, 0, sizeof(yaml_parser_t)); |
b1a54000 | 175 | if (!BUFFER_INIT(parser, parser->raw_buffer, INPUT_RAW_BUFFER_SIZE)) |
625fcfe9 | 176 | goto error; |
b1a54000 | 177 | if (!BUFFER_INIT(parser, parser->buffer, INPUT_BUFFER_SIZE)) |
625fcfe9 KS |
178 | goto error; |
179 | if (!QUEUE_INIT(parser, parser->tokens, INITIAL_QUEUE_SIZE)) | |
180 | goto error; | |
181 | if (!STACK_INIT(parser, parser->indents, INITIAL_STACK_SIZE)) | |
182 | goto error; | |
183 | if (!STACK_INIT(parser, parser->simple_keys, INITIAL_STACK_SIZE)) | |
184 | goto error; | |
185 | if (!STACK_INIT(parser, parser->states, INITIAL_STACK_SIZE)) | |
186 | goto error; | |
187 | if (!STACK_INIT(parser, parser->marks, INITIAL_STACK_SIZE)) | |
188 | goto error; | |
189 | if (!STACK_INIT(parser, parser->tag_directives, INITIAL_STACK_SIZE)) | |
190 | goto error; | |
f2b59d4d | 191 | |
625fcfe9 | 192 | return 1; |
f2b59d4d KS |
193 | |
194 | error: | |
195 | ||
625fcfe9 KS |
196 | BUFFER_DEL(parser, parser->raw_buffer); |
197 | BUFFER_DEL(parser, parser->buffer); | |
198 | QUEUE_DEL(parser, parser->tokens); | |
199 | STACK_DEL(parser, parser->indents); | |
200 | STACK_DEL(parser, parser->simple_keys); | |
201 | STACK_DEL(parser, parser->states); | |
202 | STACK_DEL(parser, parser->marks); | |
203 | STACK_DEL(parser, parser->tag_directives); | |
f2b59d4d | 204 | |
625fcfe9 | 205 | return 0; |
a51447c9 KS |
206 | } |
207 | ||
208 | /* | |
209 | * Destroy a parser object. | |
210 | */ | |
211 | ||
f642fd11 | 212 | YAML_DECLARE(void) |
a51447c9 KS |
213 | yaml_parser_delete(yaml_parser_t *parser) |
214 | { | |
95b98ba9 KS |
215 | assert(parser); /* Non-NULL parser object expected. */ |
216 | ||
625fcfe9 KS |
217 | BUFFER_DEL(parser, parser->raw_buffer); |
218 | BUFFER_DEL(parser, parser->buffer); | |
219 | while (!QUEUE_EMPTY(parser, parser->tokens)) { | |
220 | yaml_token_delete(&DEQUEUE(parser, parser->tokens)); | |
221 | } | |
222 | QUEUE_DEL(parser, parser->tokens); | |
223 | STACK_DEL(parser, parser->indents); | |
224 | STACK_DEL(parser, parser->simple_keys); | |
225 | STACK_DEL(parser, parser->states); | |
226 | STACK_DEL(parser, parser->marks); | |
227 | while (!STACK_EMPTY(parser, parser->tag_directives)) { | |
228 | yaml_tag_directive_t tag_directive = POP(parser, parser->tag_directives); | |
229 | yaml_free(tag_directive.handle); | |
230 | yaml_free(tag_directive.prefix); | |
231 | } | |
232 | STACK_DEL(parser, parser->tag_directives); | |
95b98ba9 KS |
233 | |
234 | memset(parser, 0, sizeof(yaml_parser_t)); | |
95b98ba9 KS |
235 | } |
236 | ||
237 | /* | |
6eb1ded4 | 238 | * String read handler. |
95b98ba9 KS |
239 | */ |
240 | ||
241 | static int | |
242 | yaml_string_read_handler(void *data, unsigned char *buffer, size_t size, | |
243 | size_t *size_read) | |
244 | { | |
625fcfe9 | 245 | yaml_parser_t *parser = data; |
6eb1ded4 | 246 | |
625fcfe9 | 247 | if (parser->input.string.current == parser->input.string.end) { |
6eb1ded4 KS |
248 | *size_read = 0; |
249 | return 1; | |
250 | } | |
251 | ||
625fcfe9 KS |
252 | if (size > (parser->input.string.end - parser->input.string.current)) { |
253 | size = parser->input.string.end - parser->input.string.current; | |
6eb1ded4 KS |
254 | } |
255 | ||
625fcfe9 KS |
256 | memcpy(buffer, parser->input.string.current, size); |
257 | parser->input.string.current += size; | |
6eb1ded4 | 258 | *size_read = size; |
95b98ba9 KS |
259 | return 1; |
260 | } | |
261 | ||
262 | /* | |
263 | * File read handler. | |
264 | */ | |
265 | ||
266 | static int | |
267 | yaml_file_read_handler(void *data, unsigned char *buffer, size_t size, | |
268 | size_t *size_read) | |
269 | { | |
625fcfe9 KS |
270 | yaml_parser_t *parser = data; |
271 | ||
272 | *size_read = fread(buffer, 1, size, parser->input.file); | |
273 | return !ferror(parser->input.file); | |
95b98ba9 KS |
274 | } |
275 | ||
276 | /* | |
277 | * Set a string input. | |
278 | */ | |
279 | ||
f642fd11 | 280 | YAML_DECLARE(void) |
95b98ba9 KS |
281 | yaml_parser_set_input_string(yaml_parser_t *parser, |
282 | unsigned char *input, size_t size) | |
283 | { | |
284 | assert(parser); /* Non-NULL parser object expected. */ | |
6eb1ded4 | 285 | assert(!parser->read_handler); /* You can set the source only once. */ |
95b98ba9 KS |
286 | assert(input); /* Non-NULL input string expected. */ |
287 | ||
6eb1ded4 | 288 | parser->read_handler = yaml_string_read_handler; |
625fcfe9 KS |
289 | parser->read_handler_data = parser; |
290 | ||
291 | parser->input.string.start = input; | |
292 | parser->input.string.current = input; | |
293 | parser->input.string.end = input+size; | |
95b98ba9 KS |
294 | } |
295 | ||
296 | /* | |
297 | * Set a file input. | |
298 | */ | |
299 | ||
f642fd11 | 300 | YAML_DECLARE(void) |
95b98ba9 KS |
301 | yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file) |
302 | { | |
303 | assert(parser); /* Non-NULL parser object expected. */ | |
6eb1ded4 | 304 | assert(!parser->read_handler); /* You can set the source only once. */ |
95b98ba9 KS |
305 | assert(file); /* Non-NULL file object expected. */ |
306 | ||
307 | parser->read_handler = yaml_file_read_handler; | |
625fcfe9 KS |
308 | parser->read_handler_data = parser; |
309 | ||
310 | parser->input.file = file; | |
95b98ba9 KS |
311 | } |
312 | ||
313 | /* | |
314 | * Set a generic input. | |
315 | */ | |
316 | ||
f642fd11 | 317 | YAML_DECLARE(void) |
95b98ba9 KS |
318 | yaml_parser_set_input(yaml_parser_t *parser, |
319 | yaml_read_handler_t *handler, void *data) | |
320 | { | |
321 | assert(parser); /* Non-NULL parser object expected. */ | |
6eb1ded4 | 322 | assert(!parser->read_handler); /* You can set the source only once. */ |
95b98ba9 KS |
323 | assert(handler); /* Non-NULL read handler expected. */ |
324 | ||
325 | parser->read_handler = handler; | |
6eb1ded4 | 326 | parser->read_handler_data = data; |
95b98ba9 KS |
327 | } |
328 | ||
329 | /* | |
330 | * Set the source encoding. | |
331 | */ | |
332 | ||
f642fd11 | 333 | YAML_DECLARE(void) |
95b98ba9 KS |
334 | yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding) |
335 | { | |
336 | assert(parser); /* Non-NULL parser object expected. */ | |
337 | assert(!parser->encoding); /* Encoding is already set or detected. */ | |
338 | ||
339 | parser->encoding = encoding; | |
a51447c9 KS |
340 | } |
341 | ||
b1a54000 KS |
342 | /* |
343 | * Create a new emitter object. | |
344 | */ | |
345 | ||
346 | YAML_DECLARE(int) | |
347 | yaml_emitter_initialize(yaml_emitter_t *emitter) | |
348 | { | |
349 | assert(emitter); /* Non-NULL emitter object expected. */ | |
350 | ||
351 | memset(emitter, 0, sizeof(yaml_emitter_t)); | |
352 | if (!BUFFER_INIT(emitter, emitter->buffer, OUTPUT_BUFFER_SIZE)) | |
353 | goto error; | |
354 | if (!BUFFER_INIT(emitter, emitter->raw_buffer, OUTPUT_RAW_BUFFER_SIZE)) | |
355 | goto error; | |
356 | if (!STACK_INIT(emitter, emitter->states, INITIAL_STACK_SIZE)) | |
357 | goto error; | |
358 | if (!QUEUE_INIT(emitter, emitter->events, INITIAL_QUEUE_SIZE)) | |
359 | goto error; | |
360 | if (!STACK_INIT(emitter, emitter->indents, INITIAL_STACK_SIZE)) | |
361 | goto error; | |
362 | if (!STACK_INIT(emitter, emitter->tag_directives, INITIAL_STACK_SIZE)) | |
363 | goto error; | |
364 | ||
365 | return 1; | |
366 | ||
367 | error: | |
368 | ||
369 | BUFFER_DEL(emitter, emitter->buffer); | |
370 | BUFFER_DEL(emitter, emitter->raw_buffer); | |
371 | STACK_DEL(emitter, emitter->states); | |
372 | QUEUE_DEL(emitter, emitter->events); | |
373 | STACK_DEL(emitter, emitter->indents); | |
374 | STACK_DEL(emitter, emitter->tag_directives); | |
375 | ||
376 | return 0; | |
377 | } | |
378 | ||
379 | /* | |
380 | * Destroy an emitter object. | |
381 | */ | |
382 | ||
383 | YAML_DECLARE(void) | |
384 | yaml_emitter_delete(yaml_emitter_t *emitter) | |
385 | { | |
386 | assert(emitter); /* Non-NULL emitter object expected. */ | |
387 | ||
388 | BUFFER_DEL(emitter, emitter->buffer); | |
389 | BUFFER_DEL(emitter, emitter->raw_buffer); | |
390 | STACK_DEL(emitter, emitter->states); | |
391 | while (!QUEUE_EMPTY(emitter, emitter->events)) { | |
392 | yaml_event_delete(&DEQUEUE(emitter, emitter->events)); | |
393 | } | |
394 | STACK_DEL(emitter, emitter->indents); | |
395 | while (!STACK_EMPTY(empty, emitter->tag_directives)) { | |
396 | yaml_tag_directive_t tag_directive = POP(emitter, emitter->tag_directives); | |
397 | yaml_free(tag_directive.handle); | |
398 | yaml_free(tag_directive.prefix); | |
399 | } | |
400 | STACK_DEL(emitter, emitter->tag_directives); | |
401 | ||
402 | memset(emitter, 0, sizeof(yaml_emitter_t)); | |
403 | } | |
404 | ||
405 | /* | |
406 | * String write handler. | |
407 | */ | |
408 | ||
409 | static int | |
410 | yaml_string_write_handler(void *data, unsigned char *buffer, size_t size) | |
411 | { | |
412 | yaml_emitter_t *emitter = data; | |
413 | ||
414 | if (emitter->output.string.size + *emitter->output.string.size_written | |
415 | < size) { | |
416 | memcpy(emitter->output.string.buffer | |
417 | + *emitter->output.string.size_written, | |
418 | buffer, | |
419 | emitter->output.string.size | |
420 | - *emitter->output.string.size_written); | |
421 | *emitter->output.string.size_written = emitter->output.string.size; | |
422 | return 0; | |
423 | } | |
424 | ||
425 | memcpy(emitter->output.string.buffer | |
426 | + *emitter->output.string.size_written, buffer, size); | |
427 | *emitter->output.string.size_written += size; | |
428 | return 1; | |
429 | } | |
430 | ||
431 | /* | |
432 | * File write handler. | |
433 | */ | |
434 | ||
435 | static int | |
436 | yaml_file_write_handler(void *data, unsigned char *buffer, size_t size) | |
437 | { | |
438 | yaml_emitter_t *emitter = data; | |
439 | ||
440 | return (fwrite(buffer, 1, size, emitter->output.file) == size); | |
441 | } | |
442 | /* | |
443 | * Set a string output. | |
444 | */ | |
445 | ||
446 | YAML_DECLARE(void) | |
447 | yaml_emitter_set_output_string(yaml_emitter_t *emitter, | |
448 | unsigned char *output, size_t size, size_t *size_written) | |
449 | { | |
450 | assert(emitter); /* Non-NULL emitter object expected. */ | |
451 | assert(!emitter->write_handler); /* You can set the output only once. */ | |
452 | assert(output); /* Non-NULL output string expected. */ | |
453 | ||
454 | emitter->write_handler = yaml_string_write_handler; | |
455 | emitter->write_handler_data = emitter; | |
456 | ||
457 | emitter->output.string.buffer = output; | |
458 | emitter->output.string.size = size; | |
459 | emitter->output.string.size_written = size_written; | |
460 | *size_written = 0; | |
461 | } | |
462 | ||
463 | /* | |
464 | * Set a file output. | |
465 | */ | |
466 | ||
467 | YAML_DECLARE(void) | |
468 | yaml_emitter_set_output_file(yaml_emitter_t *emitter, FILE *file) | |
469 | { | |
470 | assert(emitter); /* Non-NULL emitter object expected. */ | |
471 | assert(!emitter->write_handler); /* You can set the output only once. */ | |
472 | assert(file); /* Non-NULL file object expected. */ | |
473 | ||
474 | emitter->write_handler = yaml_file_write_handler; | |
475 | emitter->write_handler_data = emitter; | |
476 | ||
477 | emitter->output.file = file; | |
478 | } | |
479 | ||
480 | /* | |
481 | * Set a generic output handler. | |
482 | */ | |
483 | ||
484 | YAML_DECLARE(void) | |
485 | yaml_emitter_set_output(yaml_emitter_t *emitter, | |
486 | yaml_write_handler_t *handler, void *data) | |
487 | { | |
488 | assert(emitter); /* Non-NULL emitter object expected. */ | |
489 | assert(!emitter->write_handler); /* You can set the output only once. */ | |
490 | assert(handler); /* Non-NULL handler object expected. */ | |
491 | ||
492 | emitter->write_handler = handler; | |
493 | emitter->write_handler_data = data; | |
494 | } | |
495 | ||
496 | /* | |
497 | * Set the output encoding. | |
498 | */ | |
499 | ||
500 | YAML_DECLARE(void) | |
501 | yaml_emitter_set_encoding(yaml_emitter_t *emitter, yaml_encoding_t encoding) | |
502 | { | |
503 | assert(emitter); /* Non-NULL emitter object expected. */ | |
504 | assert(!emitter->encoding); /* You can set encoding only once. */ | |
505 | ||
506 | emitter->encoding = encoding; | |
507 | } | |
508 | ||
509 | /* | |
510 | * Set the canonical output style. | |
511 | */ | |
512 | ||
513 | YAML_DECLARE(void) | |
514 | yaml_emitter_set_canonical(yaml_emitter_t *emitter, int canonical) | |
515 | { | |
516 | assert(emitter); /* Non-NULL emitter object expected. */ | |
517 | ||
518 | emitter->canonical = (canonical != 0); | |
519 | } | |
520 | ||
521 | /* | |
522 | * Set the indentation increment. | |
523 | */ | |
524 | ||
525 | YAML_DECLARE(void) | |
526 | yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent) | |
527 | { | |
528 | assert(emitter); /* Non-NULL emitter object expected. */ | |
529 | ||
530 | emitter->best_indent = (1 < indent && indent < 10) ? indent : 2; | |
531 | } | |
532 | ||
533 | /* | |
534 | * Set the preferred line width. | |
535 | */ | |
536 | ||
537 | YAML_DECLARE(void) | |
538 | yaml_emitter_set_width(yaml_emitter_t *emitter, int width) | |
539 | { | |
540 | assert(emitter); /* Non-NULL emitter object expected. */ | |
541 | ||
cf616166 | 542 | emitter->best_width = (width >= 0) ? width : -1; |
b1a54000 KS |
543 | } |
544 | ||
545 | /* | |
546 | * Set if unescaped non-ASCII characters are allowed. | |
547 | */ | |
548 | ||
549 | YAML_DECLARE(void) | |
550 | yaml_emitter_set_unicode(yaml_emitter_t *emitter, int unicode) | |
551 | { | |
552 | assert(emitter); /* Non-NULL emitter object expected. */ | |
553 | ||
554 | emitter->unicode = (unicode != 0); | |
555 | } | |
556 | ||
557 | /* | |
558 | * Set the preferred line break character. | |
559 | */ | |
560 | ||
561 | YAML_DECLARE(void) | |
562 | yaml_emitter_set_break(yaml_emitter_t *emitter, yaml_break_t line_break) | |
563 | { | |
564 | assert(emitter); /* Non-NULL emitter object expected. */ | |
565 | ||
566 | emitter->line_break = line_break; | |
567 | } | |
568 | ||
f642fd11 KS |
569 | /* |
570 | * Destroy a token object. | |
571 | */ | |
572 | ||
573 | YAML_DECLARE(void) | |
574 | yaml_token_delete(yaml_token_t *token) | |
575 | { | |
576 | assert(token); /* Non-NULL token object expected. */ | |
577 | ||
578 | switch (token->type) | |
579 | { | |
580 | case YAML_TAG_DIRECTIVE_TOKEN: | |
581 | yaml_free(token->data.tag_directive.handle); | |
582 | yaml_free(token->data.tag_directive.prefix); | |
583 | break; | |
584 | ||
585 | case YAML_ALIAS_TOKEN: | |
26687d7d KS |
586 | yaml_free(token->data.alias.value); |
587 | break; | |
588 | ||
f642fd11 | 589 | case YAML_ANCHOR_TOKEN: |
26687d7d | 590 | yaml_free(token->data.anchor.value); |
f642fd11 KS |
591 | break; |
592 | ||
593 | case YAML_TAG_TOKEN: | |
594 | yaml_free(token->data.tag.handle); | |
595 | yaml_free(token->data.tag.suffix); | |
596 | break; | |
597 | ||
598 | case YAML_SCALAR_TOKEN: | |
599 | yaml_free(token->data.scalar.value); | |
600 | break; | |
54815ffd KS |
601 | |
602 | default: | |
603 | break; | |
f642fd11 KS |
604 | } |
605 | ||
606 | memset(token, 0, sizeof(yaml_token_t)); | |
26687d7d KS |
607 | } |
608 | ||
5a00d8fe KS |
609 | /* |
610 | * Check if a string is a valid UTF-8 sequence. | |
611 | * | |
612 | * Check 'reader.c' for more details on UTF-8 encoding. | |
613 | */ | |
614 | ||
615 | static int | |
616 | yaml_check_utf8(yaml_char_t *start, size_t length) | |
617 | { | |
618 | yaml_char_t *end = start+length; | |
619 | yaml_char_t *pointer = start; | |
620 | ||
621 | while (pointer < end) { | |
622 | unsigned char octet; | |
623 | unsigned int width; | |
624 | unsigned int value; | |
625 | int k; | |
626 | ||
627 | octet = pointer[0]; | |
628 | width = (octet & 0x80) == 0x00 ? 1 : | |
629 | (octet & 0xE0) == 0xC0 ? 2 : | |
630 | (octet & 0xF0) == 0xE0 ? 3 : | |
631 | (octet & 0xF8) == 0xF0 ? 4 : 0; | |
632 | value = (octet & 0x80) == 0x00 ? octet & 0x7F : | |
633 | (octet & 0xE0) == 0xC0 ? octet & 0x1F : | |
634 | (octet & 0xF0) == 0xE0 ? octet & 0x0F : | |
635 | (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; | |
636 | if (!width) return 0; | |
637 | if (pointer+width > end) return 0; | |
638 | for (k = 1; k < width; k ++) { | |
639 | octet = pointer[k]; | |
640 | if ((octet & 0xC0) != 0x80) return 0; | |
641 | value = (value << 6) + (octet & 0x3F); | |
642 | } | |
643 | if (!((width == 1) || | |
644 | (width == 2 && value >= 0x80) || | |
645 | (width == 3 && value >= 0x800) || | |
646 | (width == 4 && value >= 0x10000))) return 0; | |
647 | ||
648 | pointer += width; | |
649 | } | |
650 | ||
651 | return 1; | |
652 | } | |
653 | ||
654 | /* | |
655 | * Create STREAM-START. | |
656 | */ | |
657 | ||
658 | YAML_DECLARE(int) | |
659 | yaml_stream_start_event_initialize(yaml_event_t *event, | |
660 | yaml_encoding_t encoding) | |
661 | { | |
662 | yaml_mark_t mark = { 0, 0, 0 }; | |
663 | ||
664 | assert(event); /* Non-NULL event object is expected. */ | |
665 | ||
666 | STREAM_START_EVENT_INIT(*event, encoding, mark, mark); | |
667 | ||
668 | return 1; | |
669 | } | |
670 | ||
671 | /* | |
672 | * Create STREAM-END. | |
673 | */ | |
674 | ||
675 | YAML_DECLARE(int) | |
676 | yaml_stream_end_event_initialize(yaml_event_t *event) | |
677 | { | |
678 | yaml_mark_t mark = { 0, 0, 0 }; | |
679 | ||
680 | assert(event); /* Non-NULL event object is expected. */ | |
681 | ||
682 | STREAM_END_EVENT_INIT(*event, mark, mark); | |
683 | ||
684 | return 1; | |
685 | } | |
686 | ||
687 | /* | |
688 | * Create DOCUMENT-START. | |
689 | */ | |
690 | ||
691 | YAML_DECLARE(int) | |
692 | yaml_document_start_event_initialize(yaml_event_t *event, | |
693 | yaml_version_directive_t *version_directive, | |
694 | yaml_tag_directive_t *tag_directives_start, | |
695 | yaml_tag_directive_t *tag_directives_end, | |
696 | int implicit) | |
697 | { | |
698 | struct { | |
699 | yaml_error_type_t error; | |
700 | } context; | |
701 | yaml_mark_t mark = { 0, 0, 0 }; | |
702 | yaml_version_directive_t *version_directive_copy = NULL; | |
703 | struct { | |
704 | yaml_tag_directive_t *start; | |
705 | yaml_tag_directive_t *end; | |
706 | yaml_tag_directive_t *top; | |
707 | } tag_directives_copy = { NULL, NULL, NULL }; | |
708 | yaml_tag_directive_t value = { NULL, NULL }; | |
709 | ||
710 | assert(event); /* Non-NULL event object is expected. */ | |
711 | assert((tag_directives_start && tag_directives_end) || | |
712 | (tag_directives_start == tag_directives_end)); | |
713 | /* Valid tag directives are expected. */ | |
714 | ||
715 | if (version_directive) { | |
716 | version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t)); | |
717 | if (!version_directive_copy) goto error; | |
718 | version_directive_copy->major = version_directive->major; | |
719 | version_directive_copy->minor = version_directive->minor; | |
720 | } | |
721 | ||
722 | if (tag_directives_start != tag_directives_end) { | |
723 | yaml_tag_directive_t *tag_directive; | |
724 | if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE)) | |
725 | goto error; | |
726 | for (tag_directive = tag_directives_start; | |
727 | tag_directive != tag_directives_end; tag_directive ++) { | |
728 | assert(tag_directive->handle); | |
729 | assert(tag_directive->prefix); | |
730 | if (!yaml_check_utf8(tag_directive->handle, | |
731 | strlen((char *)tag_directive->handle))) | |
732 | goto error; | |
733 | if (!yaml_check_utf8(tag_directive->prefix, | |
734 | strlen((char *)tag_directive->prefix))) | |
735 | goto error; | |
736 | value.handle = yaml_strdup(tag_directive->handle); | |
737 | value.prefix = yaml_strdup(tag_directive->prefix); | |
738 | if (!value.handle || !value.prefix) goto error; | |
739 | if (!PUSH(&context, tag_directives_copy, value)) | |
740 | goto error; | |
741 | value.handle = NULL; | |
742 | value.prefix = NULL; | |
743 | } | |
744 | } | |
745 | ||
746 | DOCUMENT_START_EVENT_INIT(*event, version_directive_copy, | |
747 | tag_directives_copy.start, tag_directives_copy.end, | |
748 | implicit, mark, mark); | |
749 | ||
750 | return 1; | |
751 | ||
752 | error: | |
753 | yaml_free(version_directive_copy); | |
754 | while (!STACK_EMPTY(context, tag_directives_copy)) { | |
755 | yaml_tag_directive_t value = POP(context, tag_directives_copy); | |
756 | yaml_free(value.handle); | |
757 | yaml_free(value.prefix); | |
758 | } | |
759 | STACK_DEL(context, tag_directives_copy); | |
760 | yaml_free(value.handle); | |
761 | yaml_free(value.prefix); | |
762 | ||
763 | return 0; | |
764 | } | |
765 | ||
766 | /* | |
767 | * Create DOCUMENT-END. | |
768 | */ | |
769 | ||
770 | YAML_DECLARE(int) | |
771 | yaml_document_end_event_initialize(yaml_event_t *event, int implicit) | |
772 | { | |
773 | yaml_mark_t mark = { 0, 0, 0 }; | |
774 | ||
775 | assert(event); /* Non-NULL emitter object is expected. */ | |
776 | ||
777 | DOCUMENT_END_EVENT_INIT(*event, implicit, mark, mark); | |
778 | ||
779 | return 1; | |
780 | } | |
781 | ||
782 | /* | |
783 | * Create ALIAS. | |
784 | */ | |
785 | ||
786 | YAML_DECLARE(int) | |
787 | yaml_alias_event_initialize(yaml_event_t *event, yaml_char_t *anchor) | |
788 | { | |
789 | yaml_mark_t mark = { 0, 0, 0 }; | |
790 | yaml_char_t *anchor_copy = NULL; | |
791 | ||
792 | assert(event); /* Non-NULL event object is expected. */ | |
793 | assert(anchor); /* Non-NULL anchor is expected. */ | |
794 | ||
795 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) return 0; | |
796 | ||
797 | anchor_copy = yaml_strdup(anchor); | |
798 | if (!anchor_copy) | |
799 | return 0; | |
800 | ||
801 | ALIAS_EVENT_INIT(*event, anchor_copy, mark, mark); | |
802 | ||
803 | return 1; | |
804 | } | |
805 | ||
806 | /* | |
807 | * Create SCALAR. | |
808 | */ | |
809 | ||
810 | YAML_DECLARE(int) | |
811 | yaml_scalar_event_initialize(yaml_event_t *event, | |
812 | yaml_char_t *anchor, yaml_char_t *tag, | |
813 | yaml_char_t *value, size_t length, | |
814 | int plain_implicit, int quoted_implicit, | |
815 | yaml_scalar_style_t style) | |
816 | { | |
817 | yaml_mark_t mark = { 0, 0, 0 }; | |
818 | yaml_char_t *anchor_copy = NULL; | |
819 | yaml_char_t *tag_copy = NULL; | |
820 | yaml_char_t *value_copy = NULL; | |
821 | ||
822 | assert(event); /* Non-NULL event object is expected. */ | |
823 | assert(value); /* Non-NULL anchor is expected. */ | |
824 | ||
825 | ||
826 | if (anchor) { | |
827 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; | |
828 | anchor_copy = yaml_strdup(anchor); | |
829 | if (!anchor_copy) goto error; | |
830 | } | |
831 | ||
832 | if (tag) { | |
833 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; | |
834 | tag_copy = yaml_strdup(tag); | |
835 | if (!tag_copy) goto error; | |
836 | } | |
837 | ||
838 | if (!yaml_check_utf8(value, length)) goto error; | |
839 | value_copy = yaml_malloc(length+1); | |
840 | if (!value_copy) goto error; | |
841 | memcpy(value_copy, value, length); | |
842 | value_copy[length] = '\0'; | |
843 | ||
844 | SCALAR_EVENT_INIT(*event, anchor_copy, tag_copy, value_copy, length, | |
845 | plain_implicit, quoted_implicit, style, mark, mark); | |
846 | ||
847 | return 1; | |
848 | ||
849 | error: | |
850 | yaml_free(anchor_copy); | |
851 | yaml_free(tag_copy); | |
852 | yaml_free(value_copy); | |
853 | ||
854 | return 0; | |
855 | } | |
856 | ||
857 | /* | |
858 | * Create SEQUENCE-START. | |
859 | */ | |
860 | ||
861 | YAML_DECLARE(int) | |
862 | yaml_sequence_start_event_initialize(yaml_event_t *event, | |
863 | yaml_char_t *anchor, yaml_char_t *tag, int implicit, | |
864 | yaml_sequence_style_t style) | |
865 | { | |
866 | yaml_mark_t mark = { 0, 0, 0 }; | |
867 | yaml_char_t *anchor_copy = NULL; | |
868 | yaml_char_t *tag_copy = NULL; | |
869 | ||
870 | assert(event); /* Non-NULL event object is expected. */ | |
871 | ||
872 | if (anchor) { | |
873 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; | |
874 | anchor_copy = yaml_strdup(anchor); | |
875 | if (!anchor_copy) goto error; | |
876 | } | |
877 | ||
878 | if (tag) { | |
879 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; | |
880 | tag_copy = yaml_strdup(tag); | |
881 | if (!tag_copy) goto error; | |
882 | } | |
883 | ||
884 | SEQUENCE_START_EVENT_INIT(*event, anchor_copy, tag_copy, | |
885 | implicit, style, mark, mark); | |
886 | ||
887 | return 1; | |
888 | ||
889 | error: | |
890 | yaml_free(anchor_copy); | |
891 | yaml_free(tag_copy); | |
892 | ||
893 | return 0; | |
894 | } | |
895 | ||
896 | /* | |
897 | * Create SEQUENCE-END. | |
898 | */ | |
899 | ||
900 | YAML_DECLARE(int) | |
901 | yaml_sequence_end_event_initialize(yaml_event_t *event) | |
902 | { | |
903 | yaml_mark_t mark = { 0, 0, 0 }; | |
904 | ||
905 | assert(event); /* Non-NULL event object is expected. */ | |
906 | ||
907 | SEQUENCE_END_EVENT_INIT(*event, mark, mark); | |
908 | ||
909 | return 1; | |
910 | } | |
911 | ||
912 | /* | |
913 | * Create MAPPING-START. | |
914 | */ | |
915 | ||
916 | YAML_DECLARE(int) | |
917 | yaml_mapping_start_event_initialize(yaml_event_t *event, | |
918 | yaml_char_t *anchor, yaml_char_t *tag, int implicit, | |
919 | yaml_mapping_style_t style) | |
920 | { | |
921 | yaml_mark_t mark = { 0, 0, 0 }; | |
922 | yaml_char_t *anchor_copy = NULL; | |
923 | yaml_char_t *tag_copy = NULL; | |
924 | ||
925 | assert(event); /* Non-NULL event object is expected. */ | |
926 | ||
927 | if (anchor) { | |
928 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; | |
929 | anchor_copy = yaml_strdup(anchor); | |
930 | if (!anchor_copy) goto error; | |
931 | } | |
932 | ||
933 | if (tag) { | |
934 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; | |
935 | tag_copy = yaml_strdup(tag); | |
936 | if (!tag_copy) goto error; | |
937 | } | |
938 | ||
939 | MAPPING_START_EVENT_INIT(*event, anchor_copy, tag_copy, | |
940 | implicit, style, mark, mark); | |
941 | ||
942 | return 1; | |
943 | ||
944 | error: | |
945 | yaml_free(anchor_copy); | |
946 | yaml_free(tag_copy); | |
947 | ||
948 | return 0; | |
949 | } | |
950 | ||
951 | /* | |
952 | * Create MAPPING-END. | |
953 | */ | |
954 | ||
955 | YAML_DECLARE(int) | |
956 | yaml_mapping_end_event_initialize(yaml_event_t *event) | |
957 | { | |
958 | yaml_mark_t mark = { 0, 0, 0 }; | |
959 | ||
960 | assert(event); /* Non-NULL event object is expected. */ | |
961 | ||
962 | MAPPING_END_EVENT_INIT(*event, mark, mark); | |
963 | ||
964 | return 1; | |
965 | } | |
966 | ||
26687d7d KS |
967 | /* |
968 | * Destroy an event object. | |
969 | */ | |
970 | ||
971 | YAML_DECLARE(void) | |
972 | yaml_event_delete(yaml_event_t *event) | |
973 | { | |
625fcfe9 KS |
974 | yaml_tag_directive_t *tag_directive; |
975 | ||
26687d7d KS |
976 | assert(event); /* Non-NULL event object expected. */ |
977 | ||
978 | switch (event->type) | |
979 | { | |
980 | case YAML_DOCUMENT_START_EVENT: | |
625fcfe9 KS |
981 | yaml_free(event->data.document_start.version_directive); |
982 | for (tag_directive = event->data.document_start.tag_directives.start; | |
983 | tag_directive != event->data.document_start.tag_directives.end; | |
984 | tag_directive++) { | |
985 | yaml_free(tag_directive->handle); | |
986 | yaml_free(tag_directive->prefix); | |
987 | } | |
988 | yaml_free(event->data.document_start.tag_directives.start); | |
26687d7d KS |
989 | break; |
990 | ||
991 | case YAML_ALIAS_EVENT: | |
992 | yaml_free(event->data.alias.anchor); | |
993 | break; | |
994 | ||
995 | case YAML_SCALAR_EVENT: | |
996 | yaml_free(event->data.scalar.anchor); | |
997 | yaml_free(event->data.scalar.tag); | |
998 | yaml_free(event->data.scalar.value); | |
999 | break; | |
1000 | ||
1001 | case YAML_SEQUENCE_START_EVENT: | |
1002 | yaml_free(event->data.sequence_start.anchor); | |
1003 | yaml_free(event->data.sequence_start.tag); | |
1004 | break; | |
1005 | ||
1006 | case YAML_MAPPING_START_EVENT: | |
1007 | yaml_free(event->data.mapping_start.anchor); | |
1008 | yaml_free(event->data.mapping_start.tag); | |
1009 | break; | |
54815ffd KS |
1010 | |
1011 | default: | |
1012 | break; | |
26687d7d KS |
1013 | } |
1014 | ||
1015 | memset(event, 0, sizeof(yaml_event_t)); | |
26687d7d KS |
1016 | } |
1017 |