blob: a65c1bac2136d6a7b79a8764f52cbe6e775e2331 [file] [log] [blame]
Radek Krejcid91dbaf2018-09-21 15:51:39 +02001/**
2 * @file xml.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Generic XML parser implementation for libyang
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcic1c03d62018-11-27 10:52:43 +010015#include "common.h"
Radek Krejci4b74d5e2018-09-26 14:30:55 +020016
Radek Krejcib1890642018-10-03 14:05:40 +020017#include <assert.h>
Radek Krejci7a7fa902018-09-25 17:08:21 +020018#include <ctype.h>
Radek Krejcid91dbaf2018-09-21 15:51:39 +020019#include <stdbool.h>
20#include <stdint.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <stdlib.h>
Radek Krejci4b74d5e2018-09-26 14:30:55 +020022#include <string.h>
Radek Krejcid91dbaf2018-09-21 15:51:39 +020023
Radek Krejcid91dbaf2018-09-21 15:51:39 +020024#include "xml.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "printer_internal.h"
Radek Krejcid91dbaf2018-09-21 15:51:39 +020026
Radek Krejcid91dbaf2018-09-21 15:51:39 +020027/* Move input p by s characters, if EOF log with lyxml_context c */
28#define move_input(c,p,s) p += s; LY_CHECK_ERR_RET(!p[0], LOGVAL(c->ctx, LY_VLOG_LINE, &c->line, LY_VCODE_EOF), LY_EVALID)
29
Radek Krejcib1890642018-10-03 14:05:40 +020030/* Ignore whitespaces in the input string p */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020031#define ign_xmlws(c,p) while (is_xmlws(*(p))) {if (*(p) == '\n') {++c->line;} ++p;}
32
Radek Krejci4b74d5e2018-09-26 14:30:55 +020033/**
34 * @brief Ignore any characters until the delim of the size delim_len is read
35 *
36 * Detects number of read new lines.
37 * Returns the pointer to the beginning of the detected delim, or NULL in case the delim not found in
38 * NULL-terminated input string.
39 * */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020040static const char *
41ign_todelim(register const char *input, const char *delim, size_t delim_len, size_t *newlines)
42{
43 size_t i;
44 register const char *a, *b;
45
46 (*newlines) = 0;
47 for ( ; *input; ++input) {
48 if (*input != *delim) {
49 if (*input == '\n') {
50 ++(*newlines);
51 }
52 continue;
53 }
54 a = input;
55 b = delim;
56 for (i = 0; i < delim_len; ++i) {
57 if (*a++ != *b++) {
58 break;
59 }
60 }
61 if (i == delim_len) {
62 return input;
63 }
64 }
65 return NULL;
66}
67
Radek Krejci4b74d5e2018-09-26 14:30:55 +020068/**
Radek Krejci7a7fa902018-09-25 17:08:21 +020069 * Store UTF-8 character specified as 4byte integer into the dst buffer.
70 * Returns number of written bytes (4 max), expects that dst has enough space.
71 *
72 * UTF-8 mapping:
73 * 00000000 -- 0000007F: 0xxxxxxx
74 * 00000080 -- 000007FF: 110xxxxx 10xxxxxx
75 * 00000800 -- 0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
76 * 00010000 -- 001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
77 *
78 * Includes checking for valid characters (following RFC 7950, sec 9.4)
79 */
80static LY_ERR
Radek Krejci117d2082018-09-26 10:05:14 +020081lyxml_pututf8(char *dst, uint32_t value, size_t *bytes_written)
Radek Krejci7a7fa902018-09-25 17:08:21 +020082{
83 if (value < 0x80) {
84 /* one byte character */
85 if (value < 0x20 &&
86 value != 0x09 &&
87 value != 0x0a &&
88 value != 0x0d) {
89 return LY_EINVAL;
90 }
91
92 dst[0] = value;
93 (*bytes_written) = 1;
94 } else if (value < 0x800) {
95 /* two bytes character */
96 dst[0] = 0xc0 | (value >> 6);
97 dst[1] = 0x80 | (value & 0x3f);
98 (*bytes_written) = 2;
99 } else if (value < 0xfffe) {
100 /* three bytes character */
101 if (((value & 0xf800) == 0xd800) ||
102 (value >= 0xfdd0 && value <= 0xfdef)) {
103 /* exclude surrogate blocks %xD800-DFFF */
104 /* exclude noncharacters %xFDD0-FDEF */
105 return LY_EINVAL;
106 }
107
108 dst[0] = 0xe0 | (value >> 12);
109 dst[1] = 0x80 | ((value >> 6) & 0x3f);
110 dst[2] = 0x80 | (value & 0x3f);
111
112 (*bytes_written) = 3;
113 } else if (value < 0x10fffe) {
114 if ((value & 0xffe) == 0xffe) {
115 /* exclude noncharacters %xFFFE-FFFF, %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF,
116 * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF,
117 * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */
118 return LY_EINVAL;
119 }
120 /* four bytes character */
121 dst[0] = 0xf0 | (value >> 18);
122 dst[1] = 0x80 | ((value >> 12) & 0x3f);
123 dst[2] = 0x80 | ((value >> 6) & 0x3f);
124 dst[3] = 0x80 | (value & 0x3f);
125
126 (*bytes_written) = 4;
127 }
128 return LY_SUCCESS;
129}
130
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200131/**
132 * @brief Check/Get an XML qualified name from the input string.
133 *
134 * The identifier must have at least one valid character complying the name start character constraints.
135 * The identifier is terminated by the first character, which does not comply to the name character constraints.
136 *
137 * See https://www.w3.org/TR/xml-names/#NT-NCName
138 *
139 * @param[in] context XML context to track lines or store errors into libyang context.
140 * @param[in,out] input Input string to process, updated according to the processed/read data.
141 * Note that the term_char is also read, so input points after the term_char at the end.
142 * @param[out] term_char The first character in the input string which does not compy to the name constraints.
143 * @param[out] term_char_len Number of bytes used to encode UTF8 term_char. Serves to be able to go back in input string.
144 * @return LY_ERR value.
145 */
146static LY_ERR
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200147lyxml_check_qname(struct lyxml_context *context, const char **input, unsigned int *term_char, size_t *term_char_len)
148{
149 unsigned int c;
150 const char *id = (*input);
151 LY_ERR rc;
152
153 /* check NameStartChar (minus colon) */
Radek Krejcib416be62018-10-01 14:51:45 +0200154 LY_CHECK_ERR_RET(ly_getutf8(input, &c, NULL) != LY_SUCCESS,
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200155 LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID);
156 LY_CHECK_ERR_RET(!is_xmlqnamestartchar(c),
157 LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
158 "Identifier \"%s\" starts with invalid character.", id),
159 LY_EVALID);
160
161 /* check rest of the identifier */
Radek Krejcib416be62018-10-01 14:51:45 +0200162 for (rc = ly_getutf8(input, &c, term_char_len);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200163 rc == LY_SUCCESS && is_xmlqnamechar(c);
Radek Krejcib416be62018-10-01 14:51:45 +0200164 rc = ly_getutf8(input, &c, term_char_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200165 LY_CHECK_ERR_RET(rc != LY_SUCCESS, LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID);
166
167 (*term_char) = c;
168 return LY_SUCCESS;
169}
170
Radek Krejci17a78d82019-05-15 15:49:55 +0200171/**
172 * @brief Add namespace definition into XML context.
173 *
174 * Namespaces from a single element are supposed to be added sequentially together (not interleaved by a namespace from other
175 * element). This mimic namespace visibility, since the namespace defined in element E is not visible from its parents or
176 * siblings. On the other hand, namespace from a parent element can be redefined in a child element. This is also reflected
177 * by lyxml_ns_get() which returns the most recent namespace definition for the given prefix.
178 *
179 * When leaving processing of a subtree of some element (after it is removed from context->elements), caller is supposed to call
180 * lyxml_ns_rm() to remove all the namespaces defined in such an element from the context.
181 *
182 * @param[in] context XML context to work with.
183 * @param[in] prefix Pointer to the namespace prefix as taken from lyxml_get_attribute(). Can be NULL for default namespace.
184 * @param[in] prefix_len Length of the prefix string (since it is not NULL-terminated when returned from lyxml_get_attribute()).
185 * @param[in] uri Namespace URI (value) to store. Value can be obtained via lyxml_get_string() and caller is not supposed to
186 * work with the pointer when the function succeeds. In case of error the value is freed.
187 * @return LY_ERR values.
188 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200189LY_ERR
Radek Krejci17a78d82019-05-15 15:49:55 +0200190lyxml_ns_add(struct lyxml_context *context, const char *prefix, size_t prefix_len, char *uri)
191{
192 struct lyxml_ns *ns;
193
194 ns = malloc(sizeof *ns);
195 LY_CHECK_ERR_RET(!ns, LOGMEM(context->ctx), LY_EMEM);
196
197 /* we need to connect the depth of the element where the namespace is defined with the
198 * namespace record to be able to maintain (remove) the record when the parser leaves
199 * (to its sibling or back to the parent) the element where the namespace was defined */
200 ns->depth = context->elements.count;
201
202 ns->uri = uri;
203 if (prefix) {
204 ns->prefix = strndup(prefix, prefix_len);
205 LY_CHECK_ERR_RET(!ns->prefix, LOGMEM(context->ctx); free(ns->uri); free(ns), LY_EMEM);
206 } else {
207 ns->prefix = NULL;
208 }
209
210 LY_CHECK_ERR_RET(ly_set_add(&context->ns, ns, LY_SET_OPT_USEASLIST) == -1,
211 free(ns->prefix); free(ns->uri); free(ns), LY_EMEM);
212 return LY_SUCCESS;
213}
214
215/**
216 * @brief Remove all the namespaces defined in the element recently closed (removed from the context->elements).
217 *
218 * @param[in] context XML context to work with.
Radek Krejci17a78d82019-05-15 15:49:55 +0200219 */
Radek Krejci17dca992019-05-17 10:53:27 +0200220void
Radek Krejci17a78d82019-05-15 15:49:55 +0200221lyxml_ns_rm(struct lyxml_context *context)
222{
223 unsigned int u;
224
225 for (u = context->ns.count - 1; u + 1 > 0; --u) {
226 if (((struct lyxml_ns *)context->ns.objs[u])->depth != context->elements.count + 1) {
227 /* we are done, the namespaces from a single element are supposed to be together */
228 break;
229 }
230 /* remove the ns structure */
231 free(((struct lyxml_ns *)context->ns.objs[u])->prefix);
232 free(((struct lyxml_ns *)context->ns.objs[u])->uri);
233 free(context->ns.objs[u]);
234 --context->ns.count;
235 }
236
237 if (!context->ns.count) {
238 /* cleanup the context's namespaces storage */
239 ly_set_erase(&context->ns, NULL);
240 }
Radek Krejci17a78d82019-05-15 15:49:55 +0200241}
242
243const struct lyxml_ns *
244lyxml_ns_get(struct lyxml_context *context, const char *prefix, size_t prefix_len)
245{
246 unsigned int u;
247 struct lyxml_ns *ns;
248
249 for (u = context->ns.count - 1; u + 1 > 0; --u) {
250 ns = (struct lyxml_ns *)context->ns.objs[u];
Radek Krejcif2c721d2019-06-03 16:37:58 +0200251 if (prefix && prefix_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200252 if (ns->prefix && !ly_strncmp(ns->prefix, prefix, prefix_len)) {
Radek Krejci17a78d82019-05-15 15:49:55 +0200253 return ns;
254 }
255 } else if (!ns->prefix) {
256 /* default namespace */
257 return ns;
258 }
259 }
260
261 return NULL;
262}
263
Radek Krejcif2c721d2019-06-03 16:37:58 +0200264static LY_ERR
265lyxml_parse_element_start(struct lyxml_context *context, const char **input, int *closing)
266{
267 struct ly_ctx *ctx = context->ctx; /* shortcut */
268 const char *in = (*input);
269 const char *endtag;
270 const char *sectname;
271 size_t endtag_len, newlines;
272
273 while (1) {
274 ign_xmlws(context, in);
275
276 if (in[0] == '\0') {
277 /* EOF */
Radek Krejcifad79c92019-06-04 11:43:30 +0200278 if (context->elements.count) {
279 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF);
280 return LY_EVALID;
281 }
Radek Krejcif2c721d2019-06-03 16:37:58 +0200282 context->status = LYXML_END;
283 (*input) = in;
284 return LY_SUCCESS;
285 } else if (in[0] != '<') {
286 return LY_EINVAL;
287 }
288 move_input(context, in, 1);
289
290 if (in[0] == '!') {
291 move_input(context, in, 1);
292 /* sections to ignore */
293 if (!strncmp(in, "--", 2)) {
294 /* comment */
295 move_input(context, in, 2);
296 sectname = "Comment";
297 endtag = "-->";
298 endtag_len = 3;
299 } else if (!strncmp(in, "[CDATA[", 7)) {
300 /* CDATA section */
301 move_input(context, in, 7);
302 sectname = "CData";
303 endtag = "]]>";
304 endtag_len = 3;
305 } else if (!strncmp(in, "DOCTYPE", 7)) {
306 /* Document type declaration - not supported */
307 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NSUPP, "Document Type Declaration");
308 return LY_EVALID;
309 } else {
310 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Unknown XML section \"%.20s\".", &in[-2]);
311 return LY_EVALID;
312 }
313 in = ign_todelim(in, endtag, endtag_len, &newlines);
314 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, sectname), LY_EVALID);
315 context->line += newlines;
316 in += endtag_len;
317 } else if (in[0] == '?') {
318 in = ign_todelim(in, "?>", 2, &newlines);
319 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, "Declaration"), LY_EVALID);
320 context->line += newlines;
321 in += 2;
322 } else if (in[0] == '/') {
323 /* closing element tag */
324 *closing = 1;
325 ++in;
326 goto element;
327 } else {
328 /* opening element tag */
329 *closing = 0;
330element:
331 ign_xmlws(context, in);
332 LY_CHECK_ERR_RET(!in[0], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
333
334 (*input) = in;
335 return LY_SUCCESS;
336 }
337 }
338}
339
340static LY_ERR
341lyxml_parse_element_name(struct lyxml_context *context, const char **input, size_t *endtag_len, unsigned int *term_char,
342 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
343{
344 LY_ERR rc;
345 const char *in = (*input);
346 const char *id;
347 const char *endtag;
348
349 id = in;
350 rc = lyxml_check_qname(context, &in, term_char, endtag_len);
351 LY_CHECK_RET(rc);
352 if (*term_char == ':') {
353 /* we have prefixed identifier */
354 endtag = in - *endtag_len;
355
356 rc = lyxml_check_qname(context, &in, term_char, endtag_len);
357 LY_CHECK_RET(rc);
358
359 (*prefix) = id;
360 (*prefix_len) = endtag - id;
361 id = endtag + 1;
362 }
363 if (!is_xmlws(*term_char) && *term_char != '/' && *term_char != '>') {
364 (*input) = in - *endtag_len;
365 LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(*input), *input,
366 "whitespace or element tag termination ('>' or '/>'");
367 return LY_EVALID;
368 }
369 (*name) = id;
370 (*name_len) = in - *endtag_len - id;
371
372 if (is_xmlws(*term_char)) {
373 /* go to the next meaningful input */
374 ign_xmlws(context, in);
375 LY_CHECK_ERR_RET(!in[0], LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
376 *term_char = in[0];
377 ++in;
378 *endtag_len = 1;
379 }
380
381 (*input) = in;
382 return LY_SUCCESS;
383}
384
385LY_ERR
386lyxml_get_element(struct lyxml_context *context, const char **input,
387 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
388{
389 struct ly_ctx *ctx = context->ctx; /* shortcut */
390 const char *in = (*input);
391 size_t endtag_len;
392 bool loop = true;
393 int closing = 0;
394 unsigned int c;
395 LY_ERR rc;
396 struct lyxml_elem *e;
397
398 /* initialize output variables */
399 (*prefix) = (*name) = NULL;
400 (*prefix_len) = (*name_len) = 0;
401
402 while (loop) {
403 rc = lyxml_parse_element_start(context, &in, &closing);
404 if (rc) {
405 return rc;
406 } else if (context->status == LYXML_END) {
407 goto success;
408 }
409 /* we are at the begining of the element name, remember the identifier start before checking its format */
410 LY_CHECK_RET(rc = lyxml_parse_element_name(context, &in, &endtag_len, &c, prefix, prefix_len, name, name_len));
411
412 if (closing) {
413 /* match opening and closing element tags */
414 LY_CHECK_ERR_RET(
415 !context->elements.count,
Michal Vasko14654712020-02-06 08:35:21 +0100416 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
417 "Opening and closing elements tag missmatch (\"%.*s\").", *name_len, *name),
Radek Krejcif2c721d2019-06-03 16:37:58 +0200418 LY_EVALID);
419 e = (struct lyxml_elem*)context->elements.objs[context->elements.count - 1];
Michal Vasko14654712020-02-06 08:35:21 +0100420 if (e->prefix_len != *prefix_len || e->name_len != *name_len
421 || (*prefix_len && strncmp(*prefix, e->prefix, e->prefix_len)) || strncmp(*name, e->name, e->name_len)) {
422 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
423 "Opening and closing elements tag missmatch (\"%.*s\").", *name_len, *name);
424 return LY_EVALID;
425 }
Radek Krejcif2c721d2019-06-03 16:37:58 +0200426 /* opening and closing element tags matches, remove record from the opening tags list */
427 free(e);
428 --context->elements.count;
429
Michal Vasko14654712020-02-06 08:35:21 +0100430 /* remove also the namespaces connected with the element */
Radek Krejcif2c721d2019-06-03 16:37:58 +0200431 lyxml_ns_rm(context);
432
433 /* do not return element information to announce closing element being currently processed */
434 *name = *prefix = NULL;
435 *name_len = *prefix_len = 0;
436
437 if (c == '>') {
438 /* end of closing element */
439 context->status = LYXML_ELEMENT;
440 } else {
441 in -= endtag_len;
442 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Unexpected data \"%.*s\" in closing element tag.",
443 LY_VCODE_INSTREXP_len(in), in);
444 return LY_EVALID;
445 }
446 } else {
447 if (c == '>') {
448 /* end of opening element */
449 context->status = LYXML_ELEM_CONTENT;
450 } else if (c == '/' && in[0] == '>') {
451 /* empty element closing */
452 context->status = LYXML_ELEMENT;
453 ++in;
454 } else {
455 /* attribute */
456 context->status = LYXML_ATTRIBUTE;
457 in -= endtag_len;
458 }
459
460 if (context->status != LYXML_ELEMENT) {
461 /* store element opening tag information */
462 e = malloc(sizeof *e);
463 LY_CHECK_ERR_RET(!e, LOGMEM(ctx), LY_EMEM);
464 e->name = *name;
465 e->prefix = *prefix;
466 e->name_len = *name_len;
467 e->prefix_len = *prefix_len;
468 ly_set_add(&context->elements, e, LY_SET_OPT_USEASLIST);
469 }
470 }
471 loop = false;
472 }
473
474success:
Radek Krejcifad79c92019-06-04 11:43:30 +0200475 /* check for end of input */
476 if (in[0] == '\0') {
477 /* EOF */
478 if (context->elements.count) {
479 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF);
480 return LY_EVALID;
481 }
482 context->status = LYXML_END;
483 }
Radek Krejcif2c721d2019-06-03 16:37:58 +0200484 /* move caller's input */
485 (*input) = in;
486 return LY_SUCCESS;
487}
488
Radek Krejci7a7fa902018-09-25 17:08:21 +0200489LY_ERR
Michal Vasko14654712020-02-06 08:35:21 +0100490lyxml_get_string(struct lyxml_context *context, const char **input, char **buffer, size_t *buffer_size, char **output,
491 size_t *length, int *dynamic)
Radek Krejci7a7fa902018-09-25 17:08:21 +0200492{
493#define BUFSIZE 4096
494#define BUFSIZE_STEP 4096
495#define BUFSIZE_CHECK(CTX, BUF, SIZE, CURR, NEED) \
496 if (CURR+NEED >= SIZE) { \
497 BUF = ly_realloc(BUF, SIZE + BUFSIZE_STEP); \
498 LY_CHECK_ERR_RET(!BUF, LOGMEM(CTX), LY_EMEM); \
499 SIZE += BUFSIZE_STEP; \
500 }
501
502 struct ly_ctx *ctx = context->ctx; /* shortcut */
Radek Krejcid70d1072018-10-09 14:20:47 +0200503 const char *in = (*input), *start;
504 char *buf = NULL, delim;
Radek Krejci4ad42aa2019-07-23 16:55:58 +0200505 size_t offset; /* read offset in input buffer */
506 size_t len; /* length of the output string (write offset in output buffer) */
507 size_t size = 0; /* size of the output buffer */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200508 void *p;
Radek Krejci117d2082018-09-26 10:05:14 +0200509 uint32_t n;
510 size_t u, newlines;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200511 bool empty_content = false;
Radek Krejci17a78d82019-05-15 15:49:55 +0200512 LY_ERR rc = LY_SUCCESS;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200513
Radek Krejcib1890642018-10-03 14:05:40 +0200514 assert(context);
515 assert(context->status == LYXML_ELEM_CONTENT || context->status == LYXML_ATTR_CONTENT);
516
Radek Krejci7a7fa902018-09-25 17:08:21 +0200517 if (in[0] == '\'') {
518 delim = '\'';
519 ++in;
520 } else if (in[0] == '"') {
521 delim = '"';
522 ++in;
523 } else {
524 delim = '<';
525 empty_content = true;
526 }
Radek Krejcid70d1072018-10-09 14:20:47 +0200527 start = in;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200528
529 if (empty_content) {
530 /* only when processing element's content - try to ignore whitespaces used to format XML data
531 * before element's child or closing tag */
Radek Krejci117d2082018-09-26 10:05:14 +0200532 for (offset = newlines = 0; in[offset] && is_xmlws(in[offset]); ++offset) {
533 if (in[offset] == '\n') {
534 ++newlines;
535 }
536 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200537 LY_CHECK_ERR_RET(!in[offset], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
Radek Krejci117d2082018-09-26 10:05:14 +0200538 context->line += newlines;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200539 if (in[offset] == '<') {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200540 (*input) = in + offset;
Radek Krejci339e2de2019-05-17 14:28:24 +0200541
542 /* get know if it is child element (indentation) or closing element (whitespace-only content) */
Radek Krejcie553e6d2019-06-07 15:33:18 +0200543 len = offset;
544 offset = 0;
Radek Krejci339e2de2019-05-17 14:28:24 +0200545 in = *input;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200546 goto element_endtag_check;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200547 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200548 }
Radek Krejcid70d1072018-10-09 14:20:47 +0200549 /* init */
550 offset = len = 0;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200551 empty_content = false;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200552
Radek Krejcid70d1072018-10-09 14:20:47 +0200553 if (0) {
554getbuffer:
555 /* prepare output buffer */
556 if (*buffer) {
557 buf = *buffer;
558 size = *buffer_size;
559 } else {
560 buf = malloc(BUFSIZE);
561 size = BUFSIZE;
562 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
563 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200564 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200565
566 /* parse */
567 while (in[offset]) {
568 if (in[offset] == '&') {
Radek Krejciee4cab22019-07-17 17:07:47 +0200569 if (output) {
570 if (!buf) {
571 /* it is necessary to modify the input, so we will need a dynamically allocated buffer */
572 goto getbuffer;
573 }
Radek Krejcid70d1072018-10-09 14:20:47 +0200574
Radek Krejciee4cab22019-07-17 17:07:47 +0200575 if (offset) {
576 /* store what we have so far */
577 BUFSIZE_CHECK(ctx, buf, size, len, offset);
578 memcpy(&buf[len], in, offset);
579 len += offset;
580 in += offset;
581 offset = 0;
582 }
583 /* process reference */
584 /* we will need 4 bytes at most since we support only the predefined
585 * (one-char) entities and character references */
586 BUFSIZE_CHECK(ctx, buf, size, len, 4);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200587 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200588 ++offset;
589 if (in[offset] != '#') {
590 /* entity reference - only predefined references are supported */
591 if (!strncmp(&in[offset], "lt;", 3)) {
Radek Krejciee4cab22019-07-17 17:07:47 +0200592 if (output) {
593 buf[len++] = '<';
594 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200595 in += 4; /* &lt; */
596 } else if (!strncmp(&in[offset], "gt;", 3)) {
Radek Krejciee4cab22019-07-17 17:07:47 +0200597 if (output) {
598 buf[len++] = '>';
599 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200600 in += 4; /* &gt; */
601 } else if (!strncmp(&in[offset], "amp;", 4)) {
Radek Krejciee4cab22019-07-17 17:07:47 +0200602 if (output) {
603 buf[len++] = '&';
604 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200605 in += 5; /* &amp; */
606 } else if (!strncmp(&in[offset], "apos;", 5)) {
Radek Krejciee4cab22019-07-17 17:07:47 +0200607 if (output) {
608 buf[len++] = '\'';
609 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200610 in += 6; /* &apos; */
611 } else if (!strncmp(&in[offset], "quot;", 5)) {
Radek Krejciee4cab22019-07-17 17:07:47 +0200612 if (output) {
613 buf[len++] = '\"';
614 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200615 in += 6; /* &quot; */
616 } else {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200617 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
618 "Entity reference \"%.*s\" not supported, only predefined references allowed.", 10, &in[offset-1]);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200619 goto error;
620 }
621 offset = 0;
622 } else {
623 p = (void*)&in[offset - 1];
624 /* character reference */
625 ++offset;
626 if (isdigit(in[offset])) {
627 for (n = 0; isdigit(in[offset]); offset++) {
628 n = (10 * n) + (in[offset] - '0');
629 }
630 } else if (in[offset] == 'x' && isxdigit(in[offset + 1])) {
631 for (n = 0, ++offset; isxdigit(in[offset]); offset++) {
632 if (isdigit(in[offset])) {
633 u = (in[offset] - '0');
634 } else if (in[offset] > 'F') {
635 u = 10 + (in[offset] - 'a');
636 } else {
637 u = 10 + (in[offset] - 'A');
638 }
639 n = (16 * n) + u;
640 }
641 } else {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200642 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200643 goto error;
644
645 }
646 LY_CHECK_ERR_GOTO(in[offset] != ';',
647 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP,
648 LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"),
649 error);
650 ++offset;
Radek Krejciee4cab22019-07-17 17:07:47 +0200651 if (output) {
652 rc = lyxml_pututf8(&buf[len], n, &u);
653 } else {
654 char utf8[4];
655 rc = lyxml_pututf8(&utf8[0], n, &u);
656 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200657 LY_CHECK_ERR_GOTO(rc, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
Radek Krejci117d2082018-09-26 10:05:14 +0200658 "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n),
Radek Krejci7a7fa902018-09-25 17:08:21 +0200659 error);
660 len += u;
661 in += offset;
662 offset = 0;
663 }
664 } else if (in[offset] == delim) {
665 /* end of string */
Radek Krejcid70d1072018-10-09 14:20:47 +0200666 if (buf) {
667 if (len + offset >= size) {
668 buf = ly_realloc(buf, len + offset + 1);
669 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
670 size = len + offset + 1;
671 }
672 memcpy(&buf[len], in, offset);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200673 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200674 len += offset;
675 /* in case of element content, keep the leading <,
Radek Krejcib1890642018-10-03 14:05:40 +0200676 * for attribute's value move after the terminating quotation mark */
Radek Krejcie553e6d2019-06-07 15:33:18 +0200677element_endtag_check:
Radek Krejcib1890642018-10-03 14:05:40 +0200678 if (context->status == LYXML_ELEM_CONTENT) {
Radek Krejcif2c721d2019-06-03 16:37:58 +0200679 const char *name = NULL, *prefix = NULL;
680 size_t name_len = 0, prefix_len = 0;
681 int closing = 0;
682 /* use fake context to preserve real context (lines, status) since we don't want really parse the element tag here */
683 struct lyxml_context fakecontext = {.ctx = context->ctx, .line = context->line, .status = context->status};
Radek Krejci339e2de2019-05-17 14:28:24 +0200684
Radek Krejci7a7fa902018-09-25 17:08:21 +0200685 in += offset;
Radek Krejci339e2de2019-05-17 14:28:24 +0200686
687 /* get know if it is child element (mixed content) or closing element (regular content) */
Radek Krejcif2c721d2019-06-03 16:37:58 +0200688 /* We don't want actually to parse the closing element, we just need to check mixed content.
689 * The closing element tag is preserved to keep the context for the data (returned string),
690 * since it can contain data using XML prefixes defined in this element and the caller can
691 * want to work with it */
Radek Krejci339e2de2019-05-17 14:28:24 +0200692 (*input) = in;
Radek Krejcif2c721d2019-06-03 16:37:58 +0200693 rc = lyxml_parse_element_start(&fakecontext, &in, &closing);
694 if (rc) {
Radek Krejci8ced2f72019-05-20 12:33:49 +0200695 /* some parsing error */
696 goto error;
Radek Krejci339e2de2019-05-17 14:28:24 +0200697 } else {
Radek Krejcif2c721d2019-06-03 16:37:58 +0200698 size_t endtag_len;
699 unsigned int c;
700 struct lyxml_elem *e;
701
702 LY_CHECK_GOTO(lyxml_parse_element_name(&fakecontext, &in, &endtag_len, &c, &prefix, &prefix_len, &name, &name_len), error);
703
704 if (!closing) {
Radek Krejcie553e6d2019-06-07 15:33:18 +0200705 if (empty_content) {
706 /* the element here is not closing element, so we have the just indentation formatting before the child */
707 context->status = LYXML_ELEMENT;
708 return LY_EINVAL;
709 } else {
710 /* the element here is not closing element, so we have not allowed mixed content */
711 struct lyxml_elem *e = (struct lyxml_elem*)context->elements.objs[--context->elements.count];
712 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Mixed XML content is not allowed (%.*s).",
713 offset + (in - (*input)), &(*input)[-offset]);
714 free(e);
715 goto error;
716 }
Radek Krejcif2c721d2019-06-03 16:37:58 +0200717 }
718
719 /* closing element start - check the name if it matches the opening element tag */
720 LY_CHECK_ERR_GOTO(!context->elements.count,
721 LOGVAL(ctx, LY_VLOG_LINE, &fakecontext.line, LYVE_SYNTAX, "Opening and closing elements tag missmatch (\"%.*s\").",
722 name_len, name),
723 error);
724 e = (struct lyxml_elem*)context->elements.objs[context->elements.count - 1];
Michal Vasko14654712020-02-06 08:35:21 +0100725 if (e->prefix_len != prefix_len || e->name_len != name_len
726 || (prefix_len && strncmp(prefix, e->prefix, e->prefix_len)) || strncmp(name, e->name, e->name_len)) {
727 LOGVAL(ctx, LY_VLOG_LINE, &fakecontext.line, LYVE_SYNTAX,
728 "Opening and closing elements tag missmatch (\"%.*s\", expected \"%.*s\").",
729 name_len, name, e->name_len, e->name);
730 free(e);
731 --context->elements.count;
732 goto error;
733 }
Radek Krejcif2c721d2019-06-03 16:37:58 +0200734 /* opening and closing element tags matches */
735 /* return input back */
736 in = (*input);
Radek Krejci339e2de2019-05-17 14:28:24 +0200737 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200738 } else {
739 in += offset + 1;
740 }
741 goto success;
742 } else {
743 /* log lines */
744 if (in[offset] == '\n') {
745 ++context->line;
746 }
747
748 /* continue */
749 ++offset;
750 }
751 }
752 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF);
753error:
754 if (!(*buffer)) {
Radek Krejcibb9b1982019-04-08 14:24:59 +0200755 /* buffer not provided, buf is local */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200756 free(buf);
Radek Krejcibb9b1982019-04-08 14:24:59 +0200757 } else if (buf) {
758 /* buf is shared with caller via buffer, but buf could be reallocated, so update the provided buffer */
759 (*buffer) = buf;
760 (*buffer_size) = size;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200761 }
762 return LY_EVALID;
763
764success:
Radek Krejcid70d1072018-10-09 14:20:47 +0200765 if (buf) {
766 if (!(*buffer) && size != len + 1) {
767 /* not using provided buffer, so fit the allocated buffer to what we really have inside */
768 p = realloc(buf, len + 1);
769 /* ignore realloc fail because we are reducing the buffer,
770 * so just return bigger buffer than needed */
771 if (p) {
772 size = len + 1;
773 buf = p;
774 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200775 }
Radek Krejcid70d1072018-10-09 14:20:47 +0200776 /* set terminating NULL byte */
777 buf[len] = '\0';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200778 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200779
Radek Krejcib1890642018-10-03 14:05:40 +0200780 context->status -= 1;
Radek Krejcid70d1072018-10-09 14:20:47 +0200781 if (buf) {
782 (*buffer) = buf;
783 (*buffer_size) = size;
784 (*output) = buf;
785 (*dynamic) = 1;
Radek Krejciee4cab22019-07-17 17:07:47 +0200786 (*length) = len;
787 } else if (output) {
Radek Krejcid70d1072018-10-09 14:20:47 +0200788 (*output) = (char*)start;
789 (*dynamic) = 0;
Radek Krejciee4cab22019-07-17 17:07:47 +0200790 (*length) = len;
Radek Krejcid70d1072018-10-09 14:20:47 +0200791 }
Radek Krejcid70d1072018-10-09 14:20:47 +0200792
Radek Krejci28e8cb52019-03-08 11:31:31 +0100793 if (context->status == LYXML_ATTRIBUTE) {
Radek Krejcifad79c92019-06-04 11:43:30 +0200794 /* skip whitespaces after the value */
795 ign_xmlws(context, in);
796
Radek Krejci28e8cb52019-03-08 11:31:31 +0100797 if (in[0] == '>') {
798 /* element terminated by > - termination of the opening tag */
799 context->status = LYXML_ELEM_CONTENT;
800 ++in;
801 } else if (in[0] == '/' && in[1] == '>') {
802 /* element terminated by /> - termination of an empty element */
803 context->status = LYXML_ELEMENT;
804 in += 2;
805
806 /* remove the closed element record from the tags list */
807 free(context->elements.objs[context->elements.count - 1]);
808 --context->elements.count;
Radek Krejci17a78d82019-05-15 15:49:55 +0200809
810 /* remove also the namespaces conneted with the element */
Radek Krejci17dca992019-05-17 10:53:27 +0200811 lyxml_ns_rm(context);
Radek Krejcifad79c92019-06-04 11:43:30 +0200812
813 if (!context->elements.count && in[0] == '\0') {
814 /* EOF */
815 context->status = LYXML_END;
816 }
817 } /* else another attribute */
Radek Krejci28e8cb52019-03-08 11:31:31 +0100818 }
819
820 (*input) = in;
Radek Krejci17a78d82019-05-15 15:49:55 +0200821 return rc;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200822
823#undef BUFSIZE
824#undef BUFSIZE_STEP
825#undef BUFSIZE_CHECK
826}
827
Radek Krejcid972c252018-09-25 13:23:39 +0200828LY_ERR
Radek Krejci7a7fa902018-09-25 17:08:21 +0200829lyxml_get_attribute(struct lyxml_context *context, const char **input,
Radek Krejcid972c252018-09-25 13:23:39 +0200830 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
831{
832 struct ly_ctx *ctx = context->ctx; /* shortcut */
833 const char *in = (*input);
834 const char *id;
835 const char *endtag;
836 LY_ERR rc;
837 unsigned int c;
838 size_t endtag_len;
Radek Krejci17a78d82019-05-15 15:49:55 +0200839 int is_ns = 0;
840 const char *ns_prefix = NULL;
841 size_t ns_prefix_len = 0;
Radek Krejcid972c252018-09-25 13:23:39 +0200842
Radek Krejci17a78d82019-05-15 15:49:55 +0200843start:
Radek Krejcid972c252018-09-25 13:23:39 +0200844 /* initialize output variables */
845 (*prefix) = (*name) = NULL;
846 (*prefix_len) = (*name_len) = 0;
847
848 /* skip initial whitespaces */
849 ign_xmlws(context, in);
850
851 if (in[0] == '\0') {
852 /* EOF - not expected at this place */
Radek Krejcifad79c92019-06-04 11:43:30 +0200853 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF);
854 return LY_EVALID;
Radek Krejcid972c252018-09-25 13:23:39 +0200855 }
856
857 /* remember the identifier start before checking its format */
858 id = in;
859 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
860 LY_CHECK_RET(rc);
861 if (c == ':') {
862 /* we have prefixed identifier */
863 endtag = in - endtag_len;
864
865 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
866 LY_CHECK_RET(rc);
867
868 (*prefix) = id;
869 (*prefix_len) = endtag - id;
870 id = endtag + 1;
871 }
872 if (!is_xmlws(c) && c != '=') {
873 in = in - endtag_len;
874 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "whitespace or '='");
875 return LY_EVALID;
876 }
877 in = in - endtag_len;
878 (*name) = id;
879 (*name_len) = in - id;
880
881 /* eat '=' and stop at the value beginning */
882 ign_xmlws(context, in);
883 if (in[0] != '=') {
884 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "'='");
885 return LY_EVALID;
886 }
887 ++in;
888 ign_xmlws(context, in);
889 if (in[0] != '\'' && in[0] != '"') {
Radek Krejcib1890642018-10-03 14:05:40 +0200890 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP,
891 LY_VCODE_INSTREXP_len(in), in, "either single or double quotation mark");
Radek Krejcid972c252018-09-25 13:23:39 +0200892 return LY_EVALID;
893 }
Radek Krejcib1890642018-10-03 14:05:40 +0200894 context->status = LYXML_ATTR_CONTENT;
Radek Krejcid972c252018-09-25 13:23:39 +0200895
Radek Krejci17a78d82019-05-15 15:49:55 +0200896 is_ns = 0;
897 if (*prefix && *prefix_len == 5 && !strncmp(*prefix, "xmlns", 5)) {
898 is_ns = 1;
899 ns_prefix = *name;
900 ns_prefix_len = *name_len;
901 } else if (*name_len == 5 && !strncmp(*name, "xmlns", 5)) {
902 is_ns = 1;
903 }
904 if (is_ns) {
905 /* instead of attribute, we have namespace specification,
906 * so process it automatically and then move to another attribute (if any) */
907 char *value = NULL;
908 size_t value_len = 0;
909 int dynamic = 0;
910
911 LY_CHECK_RET(lyxml_get_string(context, &in, &value, &value_len, &value, &value_len, &dynamic));
912 if ((rc = lyxml_ns_add(context, ns_prefix, ns_prefix_len, dynamic ? value : strndup(value, value_len)))) {
913 if (dynamic) {
914 free(value);
915 return rc;
916 }
917 }
918 if (context->status == LYXML_ATTRIBUTE) {
919 goto start;
920 } else {
921 (*prefix) = (*name) = NULL;
922 (*prefix_len) = (*name_len) = 0;
923 }
924 }
925
Radek Krejcid972c252018-09-25 13:23:39 +0200926 /* move caller's input */
927 (*input) = in;
928 return LY_SUCCESS;
929}
930
Radek Krejcib1890642018-10-03 14:05:40 +0200931void
932lyxml_context_clear(struct lyxml_context *context)
933{
934 unsigned int u;
935
936 ly_set_erase(&context->elements, free);
937 for (u = context->ns.count - 1; u + 1 > 0; --u) {
938 /* remove the ns structure */
939 free(((struct lyxml_ns *)context->ns.objs[u])->prefix);
940 free(((struct lyxml_ns *)context->ns.objs[u])->uri);
941 free(context->ns.objs[u]);
942 }
943 ly_set_erase(&context->ns, NULL);
Radek Krejcifad79c92019-06-04 11:43:30 +0200944 context->status = 0;
Radek Krejcib1890642018-10-03 14:05:40 +0200945}
Radek Krejcie7b95092019-05-15 11:03:07 +0200946
947LY_ERR
948lyxml_dump_text(struct lyout *out, const char *text, int attribute)
949{
950 LY_ERR ret = LY_SUCCESS;
951 unsigned int u;
952
953 if (!text) {
954 return 0;
955 }
956
957 for (u = 0; text[u]; u++) {
958 switch (text[u]) {
959 case '&':
960 ret = ly_print(out, "&amp;");
961 break;
962 case '<':
963 ret = ly_print(out, "&lt;");
964 break;
965 case '>':
966 /* not needed, just for readability */
967 ret = ly_print(out, "&gt;");
968 break;
969 case '"':
970 if (attribute) {
971 ret = ly_print(out, "&quot;");
972 break;
973 }
974 /* falls through */
975 default:
976 ly_write(out, &text[u], 1);
977 }
978 }
979
980 return ret;
981}
982