blob: d72d07f8ae66eaa30cdcfe8fb635a373d7b3aaae [file] [log] [blame]
Radek Krejcid91dbaf2018-09-21 15:51:39 +02001/**
2 * @file xml.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskob36053d2020-03-26 15:49:30 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejcid91dbaf2018-09-21 15:51:39 +02005 * @brief Generic XML parser implementation for libyang
6 *
7 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
8 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
17
18#include "xml.h"
Radek Krejci4b74d5e2018-09-26 14:30:55 +020019
Radek Krejcib1890642018-10-03 14:05:40 +020020#include <assert.h>
Radek Krejci7a7fa902018-09-25 17:08:21 +020021#include <ctype.h>
Radek Krejcid91dbaf2018-09-21 15:51:39 +020022#include <stdint.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdlib.h>
Radek Krejci4b74d5e2018-09-26 14:30:55 +020024#include <string.h>
Radek Krejcid91dbaf2018-09-21 15:51:39 +020025
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020027#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020029#include "in_internal.h"
30#include "out_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "tree.h"
32#include "tree_data.h"
Radek Krejcid91dbaf2018-09-21 15:51:39 +020033
Michal Vaskob36053d2020-03-26 15:49:30 +010034/* Move input p by s characters, if EOF log with lyxml_ctx c */
Radek Krejci2efc45b2020-12-22 16:25:44 +010035#define move_input(c, s) \
36 ly_in_skip(c->in, s); \
37 LY_CHECK_ERR_RET(!c->in->current[0], LOGVAL(c->ctx, LY_VCODE_EOF), LY_EVALID)
Radek Krejcid91dbaf2018-09-21 15:51:39 +020038
Radek Krejcib1890642018-10-03 14:05:40 +020039/* Ignore whitespaces in the input string p */
Radek Krejcid54412f2020-12-17 20:25:35 +010040#define ign_xmlws(c) while (is_xmlws(*(c)->in->current)) {ly_in_skip(c->in, 1);}
Michal Vaskob36053d2020-03-26 15:49:30 +010041
Radek Krejci857189e2020-09-01 13:26:36 +020042static LY_ERR lyxml_next_attr_content(struct lyxml_ctx *xmlctx, const char **value, size_t *value_len, ly_bool *ws_only,
43 ly_bool *dynamic);
Radek Krejcid91dbaf2018-09-21 15:51:39 +020044
Radek Krejci4b74d5e2018-09-26 14:30:55 +020045/**
46 * @brief Ignore any characters until the delim of the size delim_len is read
47 *
48 * Detects number of read new lines.
Radek Krejci857189e2020-09-01 13:26:36 +020049 * Returns Boolean value whether delim was found or not.
Michal Vasko63f3d842020-07-08 10:10:14 +020050 */
Radek Krejci857189e2020-09-01 13:26:36 +020051static ly_bool
Radek Krejcid54412f2020-12-17 20:25:35 +010052ign_todelim(register const char *input, const char *delim, size_t delim_len, size_t *parsed)
Radek Krejcid91dbaf2018-09-21 15:51:39 +020053{
54 size_t i;
55 register const char *a, *b;
56
Michal Vasko63f3d842020-07-08 10:10:14 +020057 (*parsed) = 0;
58 for ( ; *input; ++input, ++(*parsed)) {
Radek Krejcid91dbaf2018-09-21 15:51:39 +020059 if (*input != *delim) {
Radek Krejcid91dbaf2018-09-21 15:51:39 +020060 continue;
61 }
62 a = input;
63 b = delim;
64 for (i = 0; i < delim_len; ++i) {
65 if (*a++ != *b++) {
66 break;
67 }
68 }
69 if (i == delim_len) {
Michal Vasko63f3d842020-07-08 10:10:14 +020070 /* delim found */
71 return 0;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020072 }
73 }
Michal Vasko63f3d842020-07-08 10:10:14 +020074
75 /* delim not found */
Radek Krejci1deb5be2020-08-26 16:43:36 +020076 return 1;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020077}
78
Radek Krejci4b74d5e2018-09-26 14:30:55 +020079/**
Michal Vaskob36053d2020-03-26 15:49:30 +010080 * @brief Check/Get an XML identifier from the input string.
81 *
82 * The identifier must have at least one valid character complying the name start character constraints.
83 * The identifier is terminated by the first character, which does not comply to the name character constraints.
84 *
85 * See https://www.w3.org/TR/xml-names/#NT-NCName
86 *
87 * @param[in] xmlctx XML context.
88 * @param[out] start Pointer to the start of the identifier.
89 * @param[out] end Pointer ot the end of the identifier.
90 * @return LY_ERR value.
91 */
92static LY_ERR
93lyxml_parse_identifier(struct lyxml_ctx *xmlctx, const char **start, const char **end)
94{
95 const char *s, *in;
96 uint32_t c;
97 size_t parsed;
98 LY_ERR rc;
99
Michal Vasko63f3d842020-07-08 10:10:14 +0200100 in = s = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100101
102 /* check NameStartChar (minus colon) */
103 LY_CHECK_ERR_RET(ly_getutf8(&in, &c, &parsed),
Radek Krejci2efc45b2020-12-22 16:25:44 +0100104 LOGVAL(xmlctx->ctx, LY_VCODE_INCHAR, in[0]),
Michal Vasko69730152020-10-09 16:30:07 +0200105 LY_EVALID);
Michal Vaskob36053d2020-03-26 15:49:30 +0100106 LY_CHECK_ERR_RET(!is_xmlqnamestartchar(c),
Radek Krejci2efc45b2020-12-22 16:25:44 +0100107 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Identifier \"%s\" starts with an invalid character.", in - parsed),
Michal Vasko69730152020-10-09 16:30:07 +0200108 LY_EVALID);
Michal Vaskob36053d2020-03-26 15:49:30 +0100109
110 /* check rest of the identifier */
111 do {
112 /* move only successfully parsed bytes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200113 ly_in_skip(xmlctx->in, parsed);
Michal Vaskob36053d2020-03-26 15:49:30 +0100114
115 rc = ly_getutf8(&in, &c, &parsed);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100116 LY_CHECK_ERR_RET(rc, LOGVAL(xmlctx->ctx, LY_VCODE_INCHAR, in[0]), LY_EVALID);
Michal Vaskob36053d2020-03-26 15:49:30 +0100117 } while (is_xmlqnamechar(c));
118
119 *start = s;
Michal Vasko63f3d842020-07-08 10:10:14 +0200120 *end = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100121 return LY_SUCCESS;
122}
123
124/**
125 * @brief Add namespace definition into XML context.
126 *
127 * Namespaces from a single element are supposed to be added sequentially together (not interleaved by a namespace from other
128 * element). This mimic namespace visibility, since the namespace defined in element E is not visible from its parents or
129 * siblings. On the other hand, namespace from a parent element can be redefined in a child element. This is also reflected
130 * by lyxml_ns_get() which returns the most recent namespace definition for the given prefix.
131 *
132 * When leaving processing of a subtree of some element (after it is removed from xmlctx->elements), caller is supposed to call
133 * lyxml_ns_rm() to remove all the namespaces defined in such an element from the context.
134 *
135 * @param[in] xmlctx XML context to work with.
136 * @param[in] prefix Pointer to the namespace prefix. Can be NULL for default namespace.
137 * @param[in] prefix_len Length of the prefix.
138 * @param[in] uri Namespace URI (value) to store directly. Value is always spent.
139 * @return LY_ERR values.
140 */
141LY_ERR
142lyxml_ns_add(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, char *uri)
143{
Radek Krejciba03a5a2020-08-27 14:40:41 +0200144 LY_ERR ret = LY_SUCCESS;
Michal Vaskob36053d2020-03-26 15:49:30 +0100145 struct lyxml_ns *ns;
146
147 ns = malloc(sizeof *ns);
148 LY_CHECK_ERR_RET(!ns, LOGMEM(xmlctx->ctx), LY_EMEM);
149
150 /* we need to connect the depth of the element where the namespace is defined with the
151 * namespace record to be able to maintain (remove) the record when the parser leaves
152 * (to its sibling or back to the parent) the element where the namespace was defined */
153 ns->depth = xmlctx->elements.count;
154
155 ns->uri = uri;
156 if (prefix) {
157 ns->prefix = strndup(prefix, prefix_len);
158 LY_CHECK_ERR_RET(!ns->prefix, LOGMEM(xmlctx->ctx); free(ns->uri); free(ns), LY_EMEM);
159 } else {
160 ns->prefix = NULL;
161 }
162
Radek Krejci3d92e442020-10-12 12:48:13 +0200163 ret = ly_set_add(&xmlctx->ns, ns, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200164 LY_CHECK_ERR_RET(ret, free(ns->prefix); free(ns->uri); free(ns), ret);
165
Michal Vaskob36053d2020-03-26 15:49:30 +0100166 return LY_SUCCESS;
167}
168
169/**
170 * @brief Remove all the namespaces defined in the element recently closed (removed from the xmlctx->elements).
171 *
172 * @param[in] xmlctx XML context to work with.
173 */
174void
175lyxml_ns_rm(struct lyxml_ctx *xmlctx)
176{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200177 for (uint32_t u = xmlctx->ns.count - 1; u + 1 > 0; --u) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100178 if (((struct lyxml_ns *)xmlctx->ns.objs[u])->depth != xmlctx->elements.count + 1) {
179 /* we are done, the namespaces from a single element are supposed to be together */
180 break;
181 }
182 /* remove the ns structure */
183 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->prefix);
184 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->uri);
185 free(xmlctx->ns.objs[u]);
186 --xmlctx->ns.count;
187 }
188
189 if (!xmlctx->ns.count) {
190 /* cleanup the xmlctx's namespaces storage */
191 ly_set_erase(&xmlctx->ns, NULL);
192 }
193}
194
Michal Vaskob36053d2020-03-26 15:49:30 +0100195const struct lyxml_ns *
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200196lyxml_ns_get(const struct ly_set *ns_set, const char *prefix, size_t prefix_len)
Michal Vaskob36053d2020-03-26 15:49:30 +0100197{
Michal Vaskob36053d2020-03-26 15:49:30 +0100198 struct lyxml_ns *ns;
199
Radek Krejci1deb5be2020-08-26 16:43:36 +0200200 for (uint32_t u = ns_set->count - 1; u + 1 > 0; --u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200201 ns = (struct lyxml_ns *)ns_set->objs[u];
Michal Vaskob36053d2020-03-26 15:49:30 +0100202 if (prefix && prefix_len) {
203 if (ns->prefix && !ly_strncmp(ns->prefix, prefix, prefix_len)) {
204 return ns;
205 }
206 } else if (!ns->prefix) {
207 /* default namespace */
208 return ns;
209 }
210 }
211
212 return NULL;
213}
214
Michal Vasko8cef5232020-06-15 17:59:47 +0200215/**
216 * @brief Skip in the input until EOF or just after the opening tag.
217 * Handles special XML constructs (comment, cdata, doctype).
218 *
219 * @param[in] xmlctx XML context to use.
220 * @return LY_ERR value.
221 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100222static LY_ERR
223lyxml_skip_until_end_or_after_otag(struct lyxml_ctx *xmlctx)
224{
225 const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */
Michal Vasko63f3d842020-07-08 10:10:14 +0200226 const char *endtag, *sectname;
Radek Krejcid54412f2020-12-17 20:25:35 +0100227 size_t endtag_len, parsed;
Radek Krejci857189e2020-09-01 13:26:36 +0200228 ly_bool rc;
Michal Vaskob36053d2020-03-26 15:49:30 +0100229
230 while (1) {
231 ign_xmlws(xmlctx);
232
Michal Vasko63f3d842020-07-08 10:10:14 +0200233 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100234 /* EOF */
235 if (xmlctx->elements.count) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100236 LOGVAL(ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100237 return LY_EVALID;
238 }
239 return LY_SUCCESS;
Michal Vasko63f3d842020-07-08 10:10:14 +0200240 } else if (xmlctx->in->current[0] != '<') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100241 LOGVAL(ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200242 xmlctx->in->current, "element tag start ('<')");
Michal Vaskob36053d2020-03-26 15:49:30 +0100243 return LY_EVALID;
244 }
245 move_input(xmlctx, 1);
246
Michal Vasko63f3d842020-07-08 10:10:14 +0200247 if (xmlctx->in->current[0] == '!') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100248 move_input(xmlctx, 1);
249 /* sections to ignore */
Michal Vasko63f3d842020-07-08 10:10:14 +0200250 if (!strncmp(xmlctx->in->current, "--", 2)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100251 /* comment */
252 move_input(xmlctx, 2);
253 sectname = "Comment";
254 endtag = "-->";
Radek Krejcif13b87b2020-12-01 22:02:17 +0100255 endtag_len = ly_strlen_const("-->");
256 } else if (!strncmp(xmlctx->in->current, "[CDATA[", ly_strlen_const("[CDATA["))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100257 /* CDATA section */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100258 move_input(xmlctx, ly_strlen_const("[CDATA["));
Michal Vaskob36053d2020-03-26 15:49:30 +0100259 sectname = "CData";
260 endtag = "]]>";
Radek Krejcif13b87b2020-12-01 22:02:17 +0100261 endtag_len = ly_strlen_const("]]>");
262 } else if (!strncmp(xmlctx->in->current, "DOCTYPE", ly_strlen_const("DOCTYPE"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100263 /* Document type declaration - not supported */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100264 LOGVAL(ctx, LY_VCODE_NSUPP, "Document Type Declaration");
Michal Vaskob36053d2020-03-26 15:49:30 +0100265 return LY_EVALID;
266 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100267 LOGVAL(ctx, LYVE_SYNTAX, "Unknown XML section \"%.20s\".", &xmlctx->in->current[-2]);
Michal Vaskob36053d2020-03-26 15:49:30 +0100268 return LY_EVALID;
269 }
Radek Krejcid54412f2020-12-17 20:25:35 +0100270 rc = ign_todelim(xmlctx->in->current, endtag, endtag_len, &parsed);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100271 LY_CHECK_ERR_RET(rc, LOGVAL(ctx, LY_VCODE_NTERM, sectname), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +0200272 ly_in_skip(xmlctx->in, parsed + endtag_len);
273 } else if (xmlctx->in->current[0] == '?') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100274 rc = ign_todelim(xmlctx->in->current, "?>", 2, &parsed);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100275 LY_CHECK_ERR_RET(rc, LOGVAL(ctx, LY_VCODE_NTERM, "Declaration"), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +0200276 ly_in_skip(xmlctx->in, parsed + 2);
Michal Vaskob36053d2020-03-26 15:49:30 +0100277 } else {
278 /* other non-WS character */
279 break;
280 }
281 }
282
283 return LY_SUCCESS;
284}
285
Michal Vasko8cef5232020-06-15 17:59:47 +0200286/**
287 * @brief Parse QName.
288 *
289 * @param[in] xmlctx XML context to use.
290 * @param[out] prefix Parsed prefix, may be NULL.
291 * @param[out] prefix_len Length of @p prefix.
292 * @param[out] name Parsed name.
293 * @param[out] name_len Length of @p name.
294 * @return LY_ERR value.
295 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100296static LY_ERR
297lyxml_parse_qname(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
298{
299 const char *start, *end;
300
301 *prefix = NULL;
302 *prefix_len = 0;
303
304 LY_CHECK_RET(lyxml_parse_identifier(xmlctx, &start, &end));
305 if (end[0] == ':') {
306 /* we have prefixed identifier */
307 *prefix = start;
308 *prefix_len = end - start;
309
310 move_input(xmlctx, 1);
311 LY_CHECK_RET(lyxml_parse_identifier(xmlctx, &start, &end));
312 }
313
314 *name = start;
315 *name_len = end - start;
316 return LY_SUCCESS;
317}
318
319/**
Michal Vasko8cef5232020-06-15 17:59:47 +0200320 * @brief Parse XML text content (value).
321 *
322 * @param[in] xmlctx XML context to use.
323 * @param[in] endchar Expected character to mark value end.
324 * @param[out] value Parsed value.
325 * @param[out] length Length of @p value.
326 * @param[out] ws_only Whether the value is empty/white-spaces only.
327 * @param[out] dynamic Whether the value was dynamically allocated.
328 * @return LY_ERR value.
329 */
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200330static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200331lyxml_parse_value(struct lyxml_ctx *xmlctx, char endchar, char **value, size_t *length, ly_bool *ws_only, ly_bool *dynamic)
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200332{
Michal Vaskob36053d2020-03-26 15:49:30 +0100333#define BUFSIZE 24
334#define BUFSIZE_STEP 128
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200335
Michal Vaskob36053d2020-03-26 15:49:30 +0100336 const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */
Michal Vasko63f3d842020-07-08 10:10:14 +0200337 const char *in = xmlctx->in->current, *start;
Michal Vaskob36053d2020-03-26 15:49:30 +0100338 char *buf = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +0200339 size_t offset; /* read offset in input buffer */
340 size_t len; /* length of the output string (write offset in output buffer) */
341 size_t size = 0; /* size of the output buffer */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200342 void *p;
Radek Krejci117d2082018-09-26 10:05:14 +0200343 uint32_t n;
Michal Vaskob36053d2020-03-26 15:49:30 +0100344 size_t u;
Radek Krejci857189e2020-09-01 13:26:36 +0200345 ly_bool ws = 1;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200346
Michal Vaskob36053d2020-03-26 15:49:30 +0100347 assert(xmlctx);
Radek Krejcib1890642018-10-03 14:05:40 +0200348
Radek Krejcid70d1072018-10-09 14:20:47 +0200349 /* init */
Michal Vaskob36053d2020-03-26 15:49:30 +0100350 start = in;
Radek Krejcid70d1072018-10-09 14:20:47 +0200351 offset = len = 0;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200352
353 /* parse */
354 while (in[offset]) {
355 if (in[offset] == '&') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100356 /* non WS */
357 ws = 0;
Radek Krejcid70d1072018-10-09 14:20:47 +0200358
Michal Vaskob36053d2020-03-26 15:49:30 +0100359 if (!buf) {
360 /* prepare output buffer */
361 buf = malloc(BUFSIZE);
362 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
363 size = BUFSIZE;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200364 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100365
366 /* allocate enough for the offset and next character,
367 * we will need 4 bytes at most since we support only the predefined
368 * (one-char) entities and character references */
Juraj Vijtiukcb017cc2020-07-08 16:19:58 +0200369 while (len + offset + 4 >= size) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100370 buf = ly_realloc(buf, size + BUFSIZE_STEP);
371 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
372 size += BUFSIZE_STEP;
373 }
374
375 if (offset) {
376 /* store what we have so far */
377 memcpy(&buf[len], in, offset);
378 len += offset;
379 in += offset;
380 offset = 0;
381 }
382
Radek Krejci7a7fa902018-09-25 17:08:21 +0200383 ++offset;
384 if (in[offset] != '#') {
385 /* entity reference - only predefined references are supported */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100386 if (!strncmp(&in[offset], "lt;", ly_strlen_const("lt;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100387 buf[len++] = '<';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100388 in += ly_strlen_const("&lt;");
389 } else if (!strncmp(&in[offset], "gt;", ly_strlen_const("gt;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100390 buf[len++] = '>';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100391 in += ly_strlen_const("&gt;");
392 } else if (!strncmp(&in[offset], "amp;", ly_strlen_const("amp;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100393 buf[len++] = '&';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100394 in += ly_strlen_const("&amp;");
395 } else if (!strncmp(&in[offset], "apos;", ly_strlen_const("apos;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100396 buf[len++] = '\'';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100397 in += ly_strlen_const("&apos;");
398 } else if (!strncmp(&in[offset], "quot;", ly_strlen_const("quot;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100399 buf[len++] = '\"';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100400 in += ly_strlen_const("&quot;");
Radek Krejci7a7fa902018-09-25 17:08:21 +0200401 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100402 LOGVAL(ctx, LYVE_SYNTAX, "Entity reference \"%.*s\" not supported, only predefined references allowed.",
403 10, &in[offset - 1]);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200404 goto error;
405 }
406 offset = 0;
407 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100408 p = (void *)&in[offset - 1];
Radek Krejci7a7fa902018-09-25 17:08:21 +0200409 /* character reference */
410 ++offset;
411 if (isdigit(in[offset])) {
412 for (n = 0; isdigit(in[offset]); offset++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100413 n = (LY_BASE_DEC * n) + (in[offset] - '0');
Radek Krejci7a7fa902018-09-25 17:08:21 +0200414 }
Michal Vasko69730152020-10-09 16:30:07 +0200415 } else if ((in[offset] == 'x') && isxdigit(in[offset + 1])) {
Radek Krejci7a7fa902018-09-25 17:08:21 +0200416 for (n = 0, ++offset; isxdigit(in[offset]); offset++) {
417 if (isdigit(in[offset])) {
418 u = (in[offset] - '0');
419 } else if (in[offset] > 'F') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100420 u = LY_BASE_DEC + (in[offset] - 'a');
Radek Krejci7a7fa902018-09-25 17:08:21 +0200421 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100422 u = LY_BASE_DEC + (in[offset] - 'A');
Radek Krejci7a7fa902018-09-25 17:08:21 +0200423 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100424 n = (LY_BASE_HEX * n) + u;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200425 }
426 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100427 LOGVAL(ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200428 goto error;
429
430 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100431
Radek Krejci7a7fa902018-09-25 17:08:21 +0200432 LY_CHECK_ERR_GOTO(in[offset] != ';',
Radek Krejci2efc45b2020-12-22 16:25:44 +0100433 LOGVAL(ctx, LY_VCODE_INSTREXP,
Michal Vasko69730152020-10-09 16:30:07 +0200434 LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"),
435 error);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200436 ++offset;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200437 LY_CHECK_ERR_GOTO(ly_pututf8(&buf[len], n, &u),
Radek Krejci2efc45b2020-12-22 16:25:44 +0100438 LOGVAL(ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n),
Michal Vasko69730152020-10-09 16:30:07 +0200439 error);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200440 len += u;
441 in += offset;
442 offset = 0;
443 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100444 } else if (in[offset] == endchar) {
Radek Krejci7a7fa902018-09-25 17:08:21 +0200445 /* end of string */
Radek Krejcid70d1072018-10-09 14:20:47 +0200446 if (buf) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100447 /* realloc exact size string */
448 buf = ly_realloc(buf, len + offset + 1);
449 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
450 size = len + offset + 1;
Radek Krejcid70d1072018-10-09 14:20:47 +0200451 memcpy(&buf[len], in, offset);
Michal Vaskob36053d2020-03-26 15:49:30 +0100452
453 /* set terminating NULL byte */
454 buf[len + offset] = '\0';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200455 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200456 len += offset;
Michal Vaskob36053d2020-03-26 15:49:30 +0100457 in += offset;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200458 goto success;
459 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100460 if (!is_xmlws(in[offset])) {
461 /* non WS */
462 ws = 0;
463 }
464
Radek Krejci7a7fa902018-09-25 17:08:21 +0200465 /* log lines */
466 if (in[offset] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100467 LY_IN_NEW_LINE(xmlctx->in);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200468 }
469
470 /* continue */
471 ++offset;
472 }
473 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100474
475 /* EOF reached before endchar */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100476 LOGVAL(ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100477
Radek Krejci7a7fa902018-09-25 17:08:21 +0200478error:
Michal Vaskob36053d2020-03-26 15:49:30 +0100479 free(buf);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200480 return LY_EVALID;
481
482success:
Radek Krejcid70d1072018-10-09 14:20:47 +0200483 if (buf) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100484 *value = buf;
485 *dynamic = 1;
486 } else {
487 *value = (char *)start;
488 *dynamic = 0;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200489 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100490 *length = len;
491 *ws_only = ws;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200492
Radek Krejcid54412f2020-12-17 20:25:35 +0100493 xmlctx->in->current = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100494 return LY_SUCCESS;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200495
496#undef BUFSIZE
497#undef BUFSIZE_STEP
Radek Krejci7a7fa902018-09-25 17:08:21 +0200498}
499
Michal Vasko8cef5232020-06-15 17:59:47 +0200500/**
501 * @brief Parse XML closing element and match it to a stored starting element.
502 *
503 * @param[in] xmlctx XML context to use.
504 * @param[in] prefix Expected closing element prefix.
505 * @param[in] prefix_len Length of @p prefix.
506 * @param[in] name Expected closing element name.
507 * @param[in] name_len Length of @p name.
508 * @param[in] empty Whether we are parsing a special "empty" element (with joined starting and closing tag) with no value.
509 * @return LY_ERR value.
510 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100511static LY_ERR
512lyxml_close_element(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, const char *name, size_t name_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200513 ly_bool empty)
Radek Krejcid972c252018-09-25 13:23:39 +0200514{
Michal Vaskob36053d2020-03-26 15:49:30 +0100515 struct lyxml_elem *e;
Radek Krejcid972c252018-09-25 13:23:39 +0200516
Michal Vaskob36053d2020-03-26 15:49:30 +0100517 /* match opening and closing element tags */
518 if (!xmlctx->elements.count) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100519 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").",
Michal Vasko69730152020-10-09 16:30:07 +0200520 name_len, name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100521 return LY_EVALID;
522 }
Radek Krejcid972c252018-09-25 13:23:39 +0200523
Michal Vaskob36053d2020-03-26 15:49:30 +0100524 e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1];
Michal Vasko69730152020-10-09 16:30:07 +0200525 if ((e->prefix_len != prefix_len) || (e->name_len != name_len) ||
526 (prefix_len && strncmp(prefix, e->prefix, e->prefix_len)) || strncmp(name, e->name, e->name_len)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100527 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Opening (\"%.*s%s%.*s\") and closing (\"%.*s%s%.*s\") elements tag mismatch.",
Michal Vasko69730152020-10-09 16:30:07 +0200528 e->prefix_len, e->prefix ? e->prefix : "", e->prefix ? ":" : "", e->name_len, e->name,
529 prefix_len, prefix ? prefix : "", prefix ? ":" : "", name_len, name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100530 return LY_EVALID;
531 }
Radek Krejcid972c252018-09-25 13:23:39 +0200532
Michal Vaskob36053d2020-03-26 15:49:30 +0100533 /* opening and closing element tags matches, remove record from the opening tags list */
534 ly_set_rm_index(&xmlctx->elements, xmlctx->elements.count - 1, free);
Radek Krejcid972c252018-09-25 13:23:39 +0200535
Michal Vaskob36053d2020-03-26 15:49:30 +0100536 /* remove also the namespaces connected with the element */
537 lyxml_ns_rm(xmlctx);
Radek Krejcid972c252018-09-25 13:23:39 +0200538
Michal Vaskob36053d2020-03-26 15:49:30 +0100539 /* skip WS */
540 ign_xmlws(xmlctx);
Radek Krejcid972c252018-09-25 13:23:39 +0200541
Michal Vaskob36053d2020-03-26 15:49:30 +0100542 /* special "<elem/>" element */
Michal Vasko63f3d842020-07-08 10:10:14 +0200543 if (empty && (xmlctx->in->current[0] == '/')) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100544 move_input(xmlctx, 1);
545 }
Michal Vasko52927e22020-03-16 17:26:14 +0100546
Michal Vaskob36053d2020-03-26 15:49:30 +0100547 /* parse closing tag */
Michal Vasko63f3d842020-07-08 10:10:14 +0200548 if (xmlctx->in->current[0] != '>') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100549 LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200550 xmlctx->in->current, "element tag termination ('>')");
Michal Vaskob36053d2020-03-26 15:49:30 +0100551 return LY_EVALID;
552 }
Michal Vasko52927e22020-03-16 17:26:14 +0100553
Michal Vaskob36053d2020-03-26 15:49:30 +0100554 /* move after closing tag without checking for EOF */
Michal Vasko63f3d842020-07-08 10:10:14 +0200555 ly_in_skip(xmlctx->in, 1);
Michal Vasko52927e22020-03-16 17:26:14 +0100556
Radek Krejcid972c252018-09-25 13:23:39 +0200557 return LY_SUCCESS;
558}
559
Michal Vasko8cef5232020-06-15 17:59:47 +0200560/**
561 * @brief Store parsed opening element and parse any included namespaces.
562 *
563 * @param[in] xmlctx XML context to use.
564 * @param[in] prefix Parsed starting element prefix.
565 * @param[in] prefix_len Length of @p prefix.
566 * @param[in] name Parsed starting element name.
567 * @param[in] name_len Length of @p name.
568 * @return LY_ERR value.
569 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100570static LY_ERR
571lyxml_open_element(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, const char *name, size_t name_len)
Radek Krejcib1890642018-10-03 14:05:40 +0200572{
Michal Vaskob36053d2020-03-26 15:49:30 +0100573 LY_ERR ret = LY_SUCCESS;
574 struct lyxml_elem *e;
575 const char *prev_input;
576 char *value;
577 size_t parsed, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200578 ly_bool ws_only, dynamic, is_ns;
Michal Vaskob36053d2020-03-26 15:49:30 +0100579 uint32_t c;
Radek Krejcib1890642018-10-03 14:05:40 +0200580
Michal Vaskob36053d2020-03-26 15:49:30 +0100581 /* store element opening tag information */
582 e = malloc(sizeof *e);
583 LY_CHECK_ERR_RET(!e, LOGMEM(xmlctx->ctx), LY_EMEM);
584 e->name = name;
585 e->prefix = prefix;
586 e->name_len = name_len;
587 e->prefix_len = prefix_len;
Radek Krejci3d92e442020-10-12 12:48:13 +0200588 LY_CHECK_RET(ly_set_add(&xmlctx->elements, e, 1, NULL));
Michal Vaskob36053d2020-03-26 15:49:30 +0100589
590 /* skip WS */
591 ign_xmlws(xmlctx);
592
593 /* parse and store all namespaces */
Michal Vasko63f3d842020-07-08 10:10:14 +0200594 prev_input = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100595 is_ns = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200596 while ((xmlctx->in->current[0] != '\0') && !ly_getutf8(&xmlctx->in->current, &c, &parsed) && is_xmlqnamestartchar(c)) {
597 xmlctx->in->current -= parsed;
Michal Vaskob36053d2020-03-26 15:49:30 +0100598
599 /* parse attribute name */
600 LY_CHECK_GOTO(ret = lyxml_parse_qname(xmlctx, &prefix, &prefix_len, &name, &name_len), cleanup);
601
602 /* parse the value */
603 LY_CHECK_GOTO(ret = lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic), cleanup);
604
605 /* store every namespace */
606 if ((prefix && !ly_strncmp("xmlns", prefix, prefix_len)) || (!prefix && !ly_strncmp("xmlns", name, name_len))) {
607 LY_CHECK_GOTO(ret = lyxml_ns_add(xmlctx, prefix ? name : NULL, prefix ? name_len : 0,
Michal Vasko69730152020-10-09 16:30:07 +0200608 dynamic ? value : strndup(value, value_len)), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100609 dynamic = 0;
610 } else {
611 /* not a namespace */
612 is_ns = 0;
613 }
614 if (dynamic) {
615 free(value);
616 }
617
618 /* skip WS */
619 ign_xmlws(xmlctx);
620
621 if (is_ns) {
622 /* we can actually skip all the namespaces as there is no reason to parse them again */
Michal Vasko63f3d842020-07-08 10:10:14 +0200623 prev_input = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100624 }
Radek Krejcib1890642018-10-03 14:05:40 +0200625 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100626
627cleanup:
628 if (!ret) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200629 xmlctx->in->current = prev_input;
Michal Vaskob36053d2020-03-26 15:49:30 +0100630 }
631 return ret;
632}
633
Michal Vasko8cef5232020-06-15 17:59:47 +0200634/**
635 * @brief Move parser to the attribute content and parse it.
636 *
637 * @param[in] xmlctx XML context to use.
638 * @param[out] value Parsed attribute value.
639 * @param[out] value_len Length of @p value.
640 * @param[out] ws_only Whether the value is empty/white-spaces only.
641 * @param[out] dynamic Whether the value was dynamically allocated.
642 * @return LY_ERR value.
643 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100644static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200645lyxml_next_attr_content(struct lyxml_ctx *xmlctx, const char **value, size_t *value_len, ly_bool *ws_only, ly_bool *dynamic)
Michal Vaskob36053d2020-03-26 15:49:30 +0100646{
647 char quot;
648
649 /* skip WS */
650 ign_xmlws(xmlctx);
651
652 /* skip '=' */
Michal Vasko63f3d842020-07-08 10:10:14 +0200653 if (xmlctx->in->current[0] == '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100654 LOGVAL(xmlctx->ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100655 return LY_EVALID;
Michal Vasko63f3d842020-07-08 10:10:14 +0200656 } else if (xmlctx->in->current[0] != '=') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100657 LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200658 xmlctx->in->current, "'='");
Michal Vaskob36053d2020-03-26 15:49:30 +0100659 return LY_EVALID;
660 }
661 move_input(xmlctx, 1);
662
663 /* skip WS */
664 ign_xmlws(xmlctx);
665
666 /* find quotes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200667 if (xmlctx->in->current[0] == '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100668 LOGVAL(xmlctx->ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100669 return LY_EVALID;
Michal Vasko63f3d842020-07-08 10:10:14 +0200670 } else if ((xmlctx->in->current[0] != '\'') && (xmlctx->in->current[0] != '\"')) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100671 LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200672 xmlctx->in->current, "either single or double quotation mark");
Michal Vaskob36053d2020-03-26 15:49:30 +0100673 return LY_EVALID;
674 }
675
676 /* remember quote */
Michal Vasko63f3d842020-07-08 10:10:14 +0200677 quot = xmlctx->in->current[0];
Michal Vaskob36053d2020-03-26 15:49:30 +0100678 move_input(xmlctx, 1);
679
680 /* parse attribute value */
681 LY_CHECK_RET(lyxml_parse_value(xmlctx, quot, (char **)value, value_len, ws_only, dynamic));
682
683 /* move after ending quote (without checking for EOF) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200684 ly_in_skip(xmlctx->in, 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100685
686 return LY_SUCCESS;
687}
688
Michal Vasko8cef5232020-06-15 17:59:47 +0200689/**
690 * @brief Move parser to the next attribute and parse it.
691 *
692 * @param[in] xmlctx XML context to use.
693 * @param[out] prefix Parsed attribute prefix.
694 * @param[out] prefix_len Length of @p prefix.
695 * @param[out] name Parsed attribute name.
696 * @param[out] name_len Length of @p name.
697 * @return LY_ERR value.
698 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100699static LY_ERR
700lyxml_next_attribute(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
701{
702 const char *in;
703 char *value;
704 uint32_t c;
705 size_t parsed, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200706 ly_bool ws_only, dynamic;
Michal Vaskob36053d2020-03-26 15:49:30 +0100707
708 /* skip WS */
709 ign_xmlws(xmlctx);
710
711 /* parse only possible attributes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200712 while ((xmlctx->in->current[0] != '>') && (xmlctx->in->current[0] != '/')) {
713 in = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100714 if (in[0] == '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100715 LOGVAL(xmlctx->ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100716 return LY_EVALID;
717 } else if ((ly_getutf8(&in, &c, &parsed) || !is_xmlqnamestartchar(c))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100718 LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in - parsed), in - parsed,
Michal Vasko69730152020-10-09 16:30:07 +0200719 "element tag end ('>' or '/>') or an attribute");
Michal Vaskob36053d2020-03-26 15:49:30 +0100720 return LY_EVALID;
721 }
722
723 /* parse attribute name */
724 LY_CHECK_RET(lyxml_parse_qname(xmlctx, prefix, prefix_len, name, name_len));
725
726 if ((!*prefix || ly_strncmp("xmlns", *prefix, *prefix_len)) && (*prefix || ly_strncmp("xmlns", *name, *name_len))) {
727 /* standard attribute */
728 break;
729 }
730
731 /* namespace, skip it */
732 LY_CHECK_RET(lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic));
733 if (dynamic) {
734 free(value);
735 }
736
737 /* skip WS */
738 ign_xmlws(xmlctx);
739 }
740
741 return LY_SUCCESS;
742}
743
Michal Vasko8cef5232020-06-15 17:59:47 +0200744/**
745 * @brief Move parser to the next element and parse it.
746 *
747 * @param[in] xmlctx XML context to use.
748 * @param[out] prefix Parsed element prefix.
749 * @param[out] prefix_len Length of @p prefix.
750 * @param[out] name Parse element name.
751 * @param[out] name_len Length of @p name.
Radek Krejci1deb5be2020-08-26 16:43:36 +0200752 * @param[out] closing Flag if the element is closing (includes '/').
Michal Vasko8cef5232020-06-15 17:59:47 +0200753 * @return LY_ERR value.
754 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100755static LY_ERR
756lyxml_next_element(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200757 ly_bool *closing)
Michal Vaskob36053d2020-03-26 15:49:30 +0100758{
759 /* skip WS until EOF or after opening tag '<' */
760 LY_CHECK_RET(lyxml_skip_until_end_or_after_otag(xmlctx));
Michal Vasko63f3d842020-07-08 10:10:14 +0200761 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100762 /* set return values */
763 *prefix = *name = NULL;
764 *prefix_len = *name_len = 0;
765 return LY_SUCCESS;
766 }
767
Michal Vasko63f3d842020-07-08 10:10:14 +0200768 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100769 move_input(xmlctx, 1);
770 *closing = 1;
771 } else {
772 *closing = 0;
773 }
774
775 /* skip WS */
776 ign_xmlws(xmlctx);
777
778 /* parse element name */
779 LY_CHECK_RET(lyxml_parse_qname(xmlctx, prefix, prefix_len, name, name_len));
780
781 return LY_SUCCESS;
782}
783
784LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200785lyxml_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyxml_ctx **xmlctx_p)
Michal Vaskob36053d2020-03-26 15:49:30 +0100786{
787 LY_ERR ret = LY_SUCCESS;
788 struct lyxml_ctx *xmlctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200789 ly_bool closing;
Michal Vaskob36053d2020-03-26 15:49:30 +0100790
791 /* new context */
792 xmlctx = calloc(1, sizeof *xmlctx);
793 LY_CHECK_ERR_RET(!xmlctx, LOGMEM(ctx), LY_EMEM);
794 xmlctx->ctx = ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200795 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100796
Radek Krejci2efc45b2020-12-22 16:25:44 +0100797 LOG_LOCINIT(ctx, NULL, NULL, NULL, in);
798
Michal Vaskob36053d2020-03-26 15:49:30 +0100799 /* parse next element, if any */
800 LY_CHECK_GOTO(ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name,
Michal Vasko69730152020-10-09 16:30:07 +0200801 &xmlctx->name_len, &closing), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100802
Michal Vasko63f3d842020-07-08 10:10:14 +0200803 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100804 /* update status */
805 xmlctx->status = LYXML_END;
806 } else if (closing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100807 LOGVAL(ctx, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").", xmlctx->name_len, xmlctx->name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100808 ret = LY_EVALID;
809 goto cleanup;
810 } else {
811 /* open an element, also parses all enclosed namespaces */
812 LY_CHECK_GOTO(ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len), cleanup);
813
814 /* update status */
815 xmlctx->status = LYXML_ELEMENT;
816 }
817
818cleanup:
819 if (ret) {
820 lyxml_ctx_free(xmlctx);
821 } else {
822 *xmlctx_p = xmlctx;
823 }
824 return ret;
825}
826
827LY_ERR
828lyxml_ctx_next(struct lyxml_ctx *xmlctx)
829{
830 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200831 ly_bool closing;
Michal Vaskob36053d2020-03-26 15:49:30 +0100832 struct lyxml_elem *e;
833
834 /* if the value was not used, free it */
835 if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) {
836 free((char *)xmlctx->value);
837 xmlctx->value = NULL;
838 xmlctx->dynamic = 0;
839 }
840
841 switch (xmlctx->status) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100842 case LYXML_ELEM_CONTENT:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100843 /* content |</elem> */
844
Michal Vaskob36053d2020-03-26 15:49:30 +0100845 /* handle special case when empty content for "<elem/>" was returned */
Michal Vasko63f3d842020-07-08 10:10:14 +0200846 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100847 assert(xmlctx->elements.count);
848 e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1];
849
850 /* close the element (parses closing tag) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200851 ret = lyxml_close_element(xmlctx, e->prefix, e->prefix_len, e->name, e->name_len, 1);
852 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100853
854 /* update status */
855 xmlctx->status = LYXML_ELEM_CLOSE;
856 break;
857 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100858 /* fall through */
Michal Vaskob36053d2020-03-26 15:49:30 +0100859 case LYXML_ELEM_CLOSE:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100860 /* </elem>| <elem2>* */
861
Michal Vaskob36053d2020-03-26 15:49:30 +0100862 /* parse next element, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +0200863 ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len, &closing);
864 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100865
Michal Vasko63f3d842020-07-08 10:10:14 +0200866 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100867 /* update status */
868 xmlctx->status = LYXML_END;
869 } else if (closing) {
870 /* close an element (parses also closing tag) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200871 ret = lyxml_close_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len, 0);
872 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100873
874 /* update status */
875 xmlctx->status = LYXML_ELEM_CLOSE;
876 } else {
877 /* open an element, also parses all enclosed namespaces */
Michal Vasko63f3d842020-07-08 10:10:14 +0200878 ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len);
879 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100880
881 /* update status */
882 xmlctx->status = LYXML_ELEMENT;
883 }
884 break;
885
Michal Vaskob36053d2020-03-26 15:49:30 +0100886 case LYXML_ELEMENT:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100887 /* <elem| attr='val'* > content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100888 case LYXML_ATTR_CONTENT:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100889 /* attr='val'| attr='val'* > content */
890
Michal Vaskob36053d2020-03-26 15:49:30 +0100891 /* parse attribute name, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +0200892 ret = lyxml_next_attribute(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len);
893 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100894
Michal Vasko63f3d842020-07-08 10:10:14 +0200895 if (xmlctx->in->current[0] == '>') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100896 /* no attributes but a closing tag */
Michal Vasko63f3d842020-07-08 10:10:14 +0200897 ly_in_skip(xmlctx->in, 1);
898 if (!xmlctx->in->current[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100899 LOGVAL(xmlctx->ctx, LY_VCODE_EOF);
Michal Vaskof55ae202020-06-30 15:49:36 +0200900 ret = LY_EVALID;
901 goto cleanup;
902 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100903
904 /* parse element content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200905 ret = lyxml_parse_value(xmlctx, '<', (char **)&xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only,
Michal Vasko69730152020-10-09 16:30:07 +0200906 &xmlctx->dynamic);
Michal Vasko63f3d842020-07-08 10:10:14 +0200907 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100908
909 if (!xmlctx->value_len) {
910 /* use empty value, easier to work with */
911 xmlctx->value = "";
912 assert(!xmlctx->dynamic);
913 }
914
915 /* update status */
916 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vasko63f3d842020-07-08 10:10:14 +0200917 } else if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100918 /* no content but we still return it */
919 xmlctx->value = "";
920 xmlctx->value_len = 0;
921 xmlctx->ws_only = 1;
922 xmlctx->dynamic = 0;
923
924 /* update status */
925 xmlctx->status = LYXML_ELEM_CONTENT;
926 } else {
927 /* update status */
928 xmlctx->status = LYXML_ATTRIBUTE;
929 }
930 break;
931
Michal Vaskob36053d2020-03-26 15:49:30 +0100932 case LYXML_ATTRIBUTE:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100933 /* attr|='val' */
934
Michal Vaskob36053d2020-03-26 15:49:30 +0100935 /* skip formatting and parse value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200936 ret = lyxml_next_attr_content(xmlctx, &xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only, &xmlctx->dynamic);
937 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100938
939 /* update status */
940 xmlctx->status = LYXML_ATTR_CONTENT;
941 break;
942
Michal Vaskob36053d2020-03-26 15:49:30 +0100943 case LYXML_END:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100944 /* </elem> |EOF */
Michal Vaskob36053d2020-03-26 15:49:30 +0100945 /* nothing to do */
946 break;
947 }
948
949cleanup:
950 if (ret) {
951 /* invalidate context */
952 xmlctx->status = LYXML_END;
953 }
954 return ret;
955}
956
957LY_ERR
958lyxml_ctx_peek(struct lyxml_ctx *xmlctx, enum LYXML_PARSER_STATUS *next)
959{
960 LY_ERR ret = LY_SUCCESS;
961 const char *prefix, *name, *prev_input;
962 size_t prefix_len, name_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200963 ly_bool closing;
Michal Vaskob36053d2020-03-26 15:49:30 +0100964
Michal Vasko63f3d842020-07-08 10:10:14 +0200965 prev_input = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100966
967 switch (xmlctx->status) {
968 case LYXML_ELEM_CONTENT:
Michal Vasko63f3d842020-07-08 10:10:14 +0200969 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100970 *next = LYXML_ELEM_CLOSE;
971 break;
972 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100973 /* fall through */
Michal Vaskob36053d2020-03-26 15:49:30 +0100974 case LYXML_ELEM_CLOSE:
975 /* parse next element, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +0200976 ret = lyxml_next_element(xmlctx, &prefix, &prefix_len, &name, &name_len, &closing);
977 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100978
Michal Vasko63f3d842020-07-08 10:10:14 +0200979 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100980 *next = LYXML_END;
981 } else if (closing) {
982 *next = LYXML_ELEM_CLOSE;
983 } else {
984 *next = LYXML_ELEMENT;
985 }
986 break;
987 case LYXML_ELEMENT:
988 case LYXML_ATTR_CONTENT:
989 /* parse attribute name, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +0200990 ret = lyxml_next_attribute(xmlctx, &prefix, &prefix_len, &name, &name_len);
991 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100992
Michal Vasko63f3d842020-07-08 10:10:14 +0200993 if ((xmlctx->in->current[0] == '>') || (xmlctx->in->current[0] == '/')) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100994 *next = LYXML_ELEM_CONTENT;
995 } else {
996 *next = LYXML_ATTRIBUTE;
997 }
998 break;
999 case LYXML_ATTRIBUTE:
1000 *next = LYXML_ATTR_CONTENT;
1001 break;
1002 case LYXML_END:
1003 *next = LYXML_END;
1004 break;
1005 }
1006
1007cleanup:
Michal Vasko63f3d842020-07-08 10:10:14 +02001008 xmlctx->in->current = prev_input;
Michal Vaskob36053d2020-03-26 15:49:30 +01001009 return ret;
1010}
1011
1012void
1013lyxml_ctx_free(struct lyxml_ctx *xmlctx)
1014{
1015 uint32_t u;
1016
1017 if (!xmlctx) {
1018 return;
1019 }
1020
Radek Krejci2efc45b2020-12-22 16:25:44 +01001021 LOG_LOCBACK(xmlctx->ctx, 0, 0, 0, 1);
1022
Michal Vaskob36053d2020-03-26 15:49:30 +01001023 if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) {
1024 free((char *)xmlctx->value);
1025 }
1026 ly_set_erase(&xmlctx->elements, free);
1027 for (u = xmlctx->ns.count - 1; u + 1 > 0; --u) {
1028 /* remove the ns structure */
1029 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->prefix);
1030 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->uri);
1031 free(xmlctx->ns.objs[u]);
1032 }
1033 ly_set_erase(&xmlctx->ns, NULL);
1034 free(xmlctx);
Radek Krejcib1890642018-10-03 14:05:40 +02001035}
Radek Krejcie7b95092019-05-15 11:03:07 +02001036
1037LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001038lyxml_dump_text(struct ly_out *out, const char *text, ly_bool attribute)
Radek Krejcie7b95092019-05-15 11:03:07 +02001039{
Michal Vasko5233e962020-08-14 14:26:20 +02001040 LY_ERR ret;
Radek Krejcie7b95092019-05-15 11:03:07 +02001041
1042 if (!text) {
1043 return 0;
1044 }
1045
Radek Krejci1deb5be2020-08-26 16:43:36 +02001046 for (uint64_t u = 0; text[u]; u++) {
Radek Krejcie7b95092019-05-15 11:03:07 +02001047 switch (text[u]) {
1048 case '&':
Michal Vasko5233e962020-08-14 14:26:20 +02001049 ret = ly_print_(out, "&amp;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001050 break;
1051 case '<':
Michal Vasko5233e962020-08-14 14:26:20 +02001052 ret = ly_print_(out, "&lt;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001053 break;
1054 case '>':
1055 /* not needed, just for readability */
Michal Vasko5233e962020-08-14 14:26:20 +02001056 ret = ly_print_(out, "&gt;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001057 break;
1058 case '"':
1059 if (attribute) {
Michal Vasko5233e962020-08-14 14:26:20 +02001060 ret = ly_print_(out, "&quot;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001061 break;
1062 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001063 /* fall through */
Radek Krejcie7b95092019-05-15 11:03:07 +02001064 default:
Michal Vasko5233e962020-08-14 14:26:20 +02001065 ret = ly_write_(out, &text[u], 1);
1066 break;
Radek Krejcie7b95092019-05-15 11:03:07 +02001067 }
Michal Vasko5233e962020-08-14 14:26:20 +02001068 LY_CHECK_RET(ret);
Radek Krejcie7b95092019-05-15 11:03:07 +02001069 }
1070
Michal Vasko5233e962020-08-14 14:26:20 +02001071 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +02001072}
1073
Michal Vasko52927e22020-03-16 17:26:14 +01001074LY_ERR
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001075lyxml_value_compare(const struct ly_ctx *ctx, const char *value1, void *val_prefix_data1, const char *value2,
1076 void *val_prefix_data2)
Michal Vasko52927e22020-03-16 17:26:14 +01001077{
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001078 const char *ptr1, *ptr2, *end1, *end2;
1079 const struct lys_module *mod1, *mod2;
Michal Vasko52927e22020-03-16 17:26:14 +01001080
1081 if (!value1 && !value2) {
1082 return LY_SUCCESS;
1083 }
1084 if ((value1 && !value2) || (!value1 && value2)) {
1085 return LY_ENOT;
1086 }
1087
1088 ptr1 = value1;
1089 ptr2 = value2;
1090 while (ptr1[0] && ptr2[0]) {
1091 if (ptr1[0] != ptr2[0]) {
1092 /* it can be a start of prefix that maps to the same module */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001093 mod1 = mod2 = NULL;
1094 if (val_prefix_data1 && (end1 = strchr(ptr1, ':'))) {
Michal Vasko52927e22020-03-16 17:26:14 +01001095 /* find module of the first prefix, if any */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001096 mod1 = ly_resolve_prefix(ctx, ptr1, end1 - ptr1, LY_PREF_XML, val_prefix_data1);
Michal Vasko52927e22020-03-16 17:26:14 +01001097 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001098 if (val_prefix_data2 && (end2 = strchr(ptr2, ':'))) {
Michal Vasko52927e22020-03-16 17:26:14 +01001099 /* find module of the second prefix, if any */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001100 mod2 = ly_resolve_prefix(ctx, ptr2, end2 - ptr2, LY_PREF_XML, val_prefix_data2);
Michal Vasko52927e22020-03-16 17:26:14 +01001101 }
1102
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001103 if (!mod1 || !mod2 || (mod1 != mod2)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001104 /* not a prefix or maps to different namespaces */
1105 break;
1106 }
1107
1108 /* skip prefixes in both values (':' is skipped as iter) */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001109 ptr1 = end1;
1110 ptr2 = end2;
Michal Vasko52927e22020-03-16 17:26:14 +01001111 }
1112
1113 ++ptr1;
1114 ++ptr2;
1115 }
1116 if (ptr1[0] || ptr2[0]) {
1117 /* not a match or simply different lengths */
1118 return LY_ENOT;
1119 }
1120
1121 return LY_SUCCESS;
1122}