blob: 69766c2f4e2a485737f2e69f52f7dddee4b7e8c8 [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 *
Michal Vaskoda8fbbf2021-06-16 11:44:44 +02007 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Radek Krejcid91dbaf2018-09-21 15:51:39 +02008 *
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"
Michal Vaskoafac7822020-10-20 14:22:26 +020028#include "in_internal.h"
29#include "out_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010031#include "tree_schema_internal.h"
Radek Krejcid91dbaf2018-09-21 15:51:39 +020032
Michal Vaskob36053d2020-03-26 15:49:30 +010033/* Move input p by s characters, if EOF log with lyxml_ctx c */
Radek Krejci2efc45b2020-12-22 16:25:44 +010034#define move_input(c, s) \
35 ly_in_skip(c->in, s); \
36 LY_CHECK_ERR_RET(!c->in->current[0], LOGVAL(c->ctx, LY_VCODE_EOF), LY_EVALID)
Radek Krejcid91dbaf2018-09-21 15:51:39 +020037
Radek Krejcib1890642018-10-03 14:05:40 +020038/* Ignore whitespaces in the input string p */
Radek Krejcidd713ce2021-01-04 23:12:12 +010039#define ign_xmlws(c) \
40 while (is_xmlws(*(c)->in->current)) { \
41 if (*(c)->in->current == '\n') { \
42 LY_IN_NEW_LINE((c)->in); \
43 } \
44 ly_in_skip(c->in, 1); \
45 }
Michal Vaskob36053d2020-03-26 15:49:30 +010046
Radek Krejci857189e2020-09-01 13:26:36 +020047static LY_ERR lyxml_next_attr_content(struct lyxml_ctx *xmlctx, const char **value, size_t *value_len, ly_bool *ws_only,
48 ly_bool *dynamic);
Radek Krejcid91dbaf2018-09-21 15:51:39 +020049
Radek Krejci4b74d5e2018-09-26 14:30:55 +020050/**
Radek Krejcidd713ce2021-01-04 23:12:12 +010051 * @brief Ignore and skip any characters until the delim of the size delim_len is read, including the delim
Radek Krejci4b74d5e2018-09-26 14:30:55 +020052 *
Radek Krejcidd713ce2021-01-04 23:12:12 +010053 * @param[in] xmlctx XML parser context to provide input handler and libyang context
54 * @param[in] in input handler to read the data, it is updated only in case the section is correctly terminated.
55 * @param[in] delim Delimiter to detect end of the section.
56 * @param[in] delim_len Length of the delimiter string to use.
57 * @param[in] sectname Section name to refer in error message.
Michal Vasko63f3d842020-07-08 10:10:14 +020058 */
Radek Krejcidd713ce2021-01-04 23:12:12 +010059LY_ERR
60skip_section(struct lyxml_ctx *xmlctx, const char *delim, size_t delim_len, const char *sectname)
Radek Krejcid91dbaf2018-09-21 15:51:39 +020061{
62 size_t i;
Radek Krejcidd713ce2021-01-04 23:12:12 +010063 register const char *input, *a, *b;
64 uint64_t parsed = 0, newlines = 0;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020065
Radek Krejcidd713ce2021-01-04 23:12:12 +010066 for (input = xmlctx->in->current; *input; ++input, ++parsed) {
Radek Krejcid91dbaf2018-09-21 15:51:39 +020067 if (*input != *delim) {
Radek Krejcidd713ce2021-01-04 23:12:12 +010068 if (*input == '\n') {
69 ++newlines;
70 }
Radek Krejcid91dbaf2018-09-21 15:51:39 +020071 continue;
72 }
73 a = input;
74 b = delim;
75 for (i = 0; i < delim_len; ++i) {
76 if (*a++ != *b++) {
77 break;
78 }
79 }
80 if (i == delim_len) {
Michal Vasko63f3d842020-07-08 10:10:14 +020081 /* delim found */
Radek Krejcidd713ce2021-01-04 23:12:12 +010082 xmlctx->in->line += newlines;
83 ly_in_skip(xmlctx->in, parsed + delim_len);
84 return LY_SUCCESS;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020085 }
86 }
Michal Vasko63f3d842020-07-08 10:10:14 +020087
Radek Krejcidd713ce2021-01-04 23:12:12 +010088 /* delim not found,
89 * do not update input handler to refer to the beginning of the section in error message */
90 LOGVAL(xmlctx->ctx, LY_VCODE_NTERM, sectname);
91 return LY_EVALID;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020092}
93
Radek Krejci4b74d5e2018-09-26 14:30:55 +020094/**
Michal Vaskob36053d2020-03-26 15:49:30 +010095 * @brief Check/Get an XML identifier from the input string.
96 *
97 * The identifier must have at least one valid character complying the name start character constraints.
98 * The identifier is terminated by the first character, which does not comply to the name character constraints.
99 *
100 * See https://www.w3.org/TR/xml-names/#NT-NCName
101 *
102 * @param[in] xmlctx XML context.
103 * @param[out] start Pointer to the start of the identifier.
104 * @param[out] end Pointer ot the end of the identifier.
105 * @return LY_ERR value.
106 */
107static LY_ERR
108lyxml_parse_identifier(struct lyxml_ctx *xmlctx, const char **start, const char **end)
109{
110 const char *s, *in;
111 uint32_t c;
112 size_t parsed;
113 LY_ERR rc;
114
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 in = s = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100116
117 /* check NameStartChar (minus colon) */
118 LY_CHECK_ERR_RET(ly_getutf8(&in, &c, &parsed),
Radek Krejci2efc45b2020-12-22 16:25:44 +0100119 LOGVAL(xmlctx->ctx, LY_VCODE_INCHAR, in[0]),
Michal Vasko69730152020-10-09 16:30:07 +0200120 LY_EVALID);
Michal Vaskob36053d2020-03-26 15:49:30 +0100121 LY_CHECK_ERR_RET(!is_xmlqnamestartchar(c),
Radek Krejci2efc45b2020-12-22 16:25:44 +0100122 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Identifier \"%s\" starts with an invalid character.", in - parsed),
Michal Vasko69730152020-10-09 16:30:07 +0200123 LY_EVALID);
Michal Vaskob36053d2020-03-26 15:49:30 +0100124
125 /* check rest of the identifier */
126 do {
127 /* move only successfully parsed bytes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200128 ly_in_skip(xmlctx->in, parsed);
Michal Vaskob36053d2020-03-26 15:49:30 +0100129
130 rc = ly_getutf8(&in, &c, &parsed);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100131 LY_CHECK_ERR_RET(rc, LOGVAL(xmlctx->ctx, LY_VCODE_INCHAR, in[0]), LY_EVALID);
Michal Vaskob36053d2020-03-26 15:49:30 +0100132 } while (is_xmlqnamechar(c));
133
134 *start = s;
Michal Vasko63f3d842020-07-08 10:10:14 +0200135 *end = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100136 return LY_SUCCESS;
137}
138
139/**
140 * @brief Add namespace definition into XML context.
141 *
142 * Namespaces from a single element are supposed to be added sequentially together (not interleaved by a namespace from other
143 * element). This mimic namespace visibility, since the namespace defined in element E is not visible from its parents or
144 * siblings. On the other hand, namespace from a parent element can be redefined in a child element. This is also reflected
145 * by lyxml_ns_get() which returns the most recent namespace definition for the given prefix.
146 *
147 * When leaving processing of a subtree of some element (after it is removed from xmlctx->elements), caller is supposed to call
148 * lyxml_ns_rm() to remove all the namespaces defined in such an element from the context.
149 *
150 * @param[in] xmlctx XML context to work with.
151 * @param[in] prefix Pointer to the namespace prefix. Can be NULL for default namespace.
152 * @param[in] prefix_len Length of the prefix.
153 * @param[in] uri Namespace URI (value) to store directly. Value is always spent.
154 * @return LY_ERR values.
155 */
156LY_ERR
157lyxml_ns_add(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, char *uri)
158{
Radek Krejciba03a5a2020-08-27 14:40:41 +0200159 LY_ERR ret = LY_SUCCESS;
Michal Vaskob36053d2020-03-26 15:49:30 +0100160 struct lyxml_ns *ns;
161
162 ns = malloc(sizeof *ns);
163 LY_CHECK_ERR_RET(!ns, LOGMEM(xmlctx->ctx), LY_EMEM);
164
165 /* we need to connect the depth of the element where the namespace is defined with the
166 * namespace record to be able to maintain (remove) the record when the parser leaves
167 * (to its sibling or back to the parent) the element where the namespace was defined */
168 ns->depth = xmlctx->elements.count;
169
170 ns->uri = uri;
171 if (prefix) {
172 ns->prefix = strndup(prefix, prefix_len);
173 LY_CHECK_ERR_RET(!ns->prefix, LOGMEM(xmlctx->ctx); free(ns->uri); free(ns), LY_EMEM);
174 } else {
175 ns->prefix = NULL;
176 }
177
Radek Krejci3d92e442020-10-12 12:48:13 +0200178 ret = ly_set_add(&xmlctx->ns, ns, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200179 LY_CHECK_ERR_RET(ret, free(ns->prefix); free(ns->uri); free(ns), ret);
180
Michal Vaskob36053d2020-03-26 15:49:30 +0100181 return LY_SUCCESS;
182}
183
184/**
185 * @brief Remove all the namespaces defined in the element recently closed (removed from the xmlctx->elements).
186 *
187 * @param[in] xmlctx XML context to work with.
188 */
189void
190lyxml_ns_rm(struct lyxml_ctx *xmlctx)
191{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200192 for (uint32_t u = xmlctx->ns.count - 1; u + 1 > 0; --u) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100193 if (((struct lyxml_ns *)xmlctx->ns.objs[u])->depth != xmlctx->elements.count + 1) {
194 /* we are done, the namespaces from a single element are supposed to be together */
195 break;
196 }
197 /* remove the ns structure */
198 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->prefix);
199 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->uri);
200 free(xmlctx->ns.objs[u]);
201 --xmlctx->ns.count;
202 }
203
204 if (!xmlctx->ns.count) {
205 /* cleanup the xmlctx's namespaces storage */
206 ly_set_erase(&xmlctx->ns, NULL);
207 }
208}
209
Michal Vaskob36053d2020-03-26 15:49:30 +0100210const struct lyxml_ns *
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200211lyxml_ns_get(const struct ly_set *ns_set, const char *prefix, size_t prefix_len)
Michal Vaskob36053d2020-03-26 15:49:30 +0100212{
Michal Vaskob36053d2020-03-26 15:49:30 +0100213 struct lyxml_ns *ns;
214
Radek Krejci1deb5be2020-08-26 16:43:36 +0200215 for (uint32_t u = ns_set->count - 1; u + 1 > 0; --u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200216 ns = (struct lyxml_ns *)ns_set->objs[u];
Michal Vaskob36053d2020-03-26 15:49:30 +0100217 if (prefix && prefix_len) {
218 if (ns->prefix && !ly_strncmp(ns->prefix, prefix, prefix_len)) {
219 return ns;
220 }
221 } else if (!ns->prefix) {
222 /* default namespace */
223 return ns;
224 }
225 }
226
227 return NULL;
228}
229
Michal Vasko8cef5232020-06-15 17:59:47 +0200230/**
231 * @brief Skip in the input until EOF or just after the opening tag.
232 * Handles special XML constructs (comment, cdata, doctype).
233 *
234 * @param[in] xmlctx XML context to use.
235 * @return LY_ERR value.
236 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100237static LY_ERR
238lyxml_skip_until_end_or_after_otag(struct lyxml_ctx *xmlctx)
239{
240 const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */
Michal Vasko63f3d842020-07-08 10:10:14 +0200241 const char *endtag, *sectname;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100242 size_t endtag_len;
Michal Vaskob36053d2020-03-26 15:49:30 +0100243
244 while (1) {
245 ign_xmlws(xmlctx);
246
Michal Vasko63f3d842020-07-08 10:10:14 +0200247 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100248 /* EOF */
249 if (xmlctx->elements.count) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100250 LOGVAL(ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100251 return LY_EVALID;
252 }
253 return LY_SUCCESS;
Michal Vasko63f3d842020-07-08 10:10:14 +0200254 } else if (xmlctx->in->current[0] != '<') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100255 LOGVAL(ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200256 xmlctx->in->current, "element tag start ('<')");
Michal Vaskob36053d2020-03-26 15:49:30 +0100257 return LY_EVALID;
258 }
259 move_input(xmlctx, 1);
260
Michal Vasko63f3d842020-07-08 10:10:14 +0200261 if (xmlctx->in->current[0] == '!') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100262 move_input(xmlctx, 1);
263 /* sections to ignore */
Michal Vasko63f3d842020-07-08 10:10:14 +0200264 if (!strncmp(xmlctx->in->current, "--", 2)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100265 /* comment */
266 move_input(xmlctx, 2);
267 sectname = "Comment";
268 endtag = "-->";
Radek Krejcif13b87b2020-12-01 22:02:17 +0100269 endtag_len = ly_strlen_const("-->");
270 } else if (!strncmp(xmlctx->in->current, "[CDATA[", ly_strlen_const("[CDATA["))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100271 /* CDATA section */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100272 move_input(xmlctx, ly_strlen_const("[CDATA["));
Michal Vaskob36053d2020-03-26 15:49:30 +0100273 sectname = "CData";
274 endtag = "]]>";
Radek Krejcif13b87b2020-12-01 22:02:17 +0100275 endtag_len = ly_strlen_const("]]>");
276 } else if (!strncmp(xmlctx->in->current, "DOCTYPE", ly_strlen_const("DOCTYPE"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100277 /* Document type declaration - not supported */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100278 LOGVAL(ctx, LY_VCODE_NSUPP, "Document Type Declaration");
Michal Vaskob36053d2020-03-26 15:49:30 +0100279 return LY_EVALID;
280 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100281 LOGVAL(ctx, LYVE_SYNTAX, "Unknown XML section \"%.20s\".", &xmlctx->in->current[-2]);
Michal Vaskob36053d2020-03-26 15:49:30 +0100282 return LY_EVALID;
283 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100284 LY_CHECK_RET(skip_section(xmlctx, endtag, endtag_len, sectname));
Michal Vasko63f3d842020-07-08 10:10:14 +0200285 } else if (xmlctx->in->current[0] == '?') {
Radek Krejcidd713ce2021-01-04 23:12:12 +0100286 LY_CHECK_RET(skip_section(xmlctx, "?>", 2, "Declaration"));
Michal Vaskob36053d2020-03-26 15:49:30 +0100287 } else {
288 /* other non-WS character */
289 break;
290 }
291 }
292
293 return LY_SUCCESS;
294}
295
Michal Vasko8cef5232020-06-15 17:59:47 +0200296/**
297 * @brief Parse QName.
298 *
299 * @param[in] xmlctx XML context to use.
300 * @param[out] prefix Parsed prefix, may be NULL.
301 * @param[out] prefix_len Length of @p prefix.
302 * @param[out] name Parsed name.
303 * @param[out] name_len Length of @p name.
304 * @return LY_ERR value.
305 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100306static LY_ERR
307lyxml_parse_qname(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
308{
309 const char *start, *end;
310
311 *prefix = NULL;
312 *prefix_len = 0;
313
314 LY_CHECK_RET(lyxml_parse_identifier(xmlctx, &start, &end));
315 if (end[0] == ':') {
316 /* we have prefixed identifier */
317 *prefix = start;
318 *prefix_len = end - start;
319
320 move_input(xmlctx, 1);
321 LY_CHECK_RET(lyxml_parse_identifier(xmlctx, &start, &end));
322 }
323
324 *name = start;
325 *name_len = end - start;
326 return LY_SUCCESS;
327}
328
329/**
Michal Vasko8cef5232020-06-15 17:59:47 +0200330 * @brief Parse XML text content (value).
331 *
332 * @param[in] xmlctx XML context to use.
333 * @param[in] endchar Expected character to mark value end.
334 * @param[out] value Parsed value.
335 * @param[out] length Length of @p value.
336 * @param[out] ws_only Whether the value is empty/white-spaces only.
337 * @param[out] dynamic Whether the value was dynamically allocated.
338 * @return LY_ERR value.
339 */
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200340static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200341lyxml_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 +0200342{
Michal Vaskob36053d2020-03-26 15:49:30 +0100343#define BUFSIZE 24
344#define BUFSIZE_STEP 128
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200345
Michal Vaskob36053d2020-03-26 15:49:30 +0100346 const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */
aPiecekb287b212021-05-04 14:24:25 +0200347 const char *in = xmlctx->in->current, *start, *in_aux;
Michal Vaskob36053d2020-03-26 15:49:30 +0100348 char *buf = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +0200349 size_t offset; /* read offset in input buffer */
350 size_t len; /* length of the output string (write offset in output buffer) */
351 size_t size = 0; /* size of the output buffer */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200352 void *p;
Radek Krejci117d2082018-09-26 10:05:14 +0200353 uint32_t n;
Michal Vaskob36053d2020-03-26 15:49:30 +0100354 size_t u;
Radek Krejci857189e2020-09-01 13:26:36 +0200355 ly_bool ws = 1;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200356
Michal Vaskob36053d2020-03-26 15:49:30 +0100357 assert(xmlctx);
Radek Krejcib1890642018-10-03 14:05:40 +0200358
Radek Krejcid70d1072018-10-09 14:20:47 +0200359 /* init */
Michal Vaskob36053d2020-03-26 15:49:30 +0100360 start = in;
Radek Krejcid70d1072018-10-09 14:20:47 +0200361 offset = len = 0;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200362
363 /* parse */
364 while (in[offset]) {
365 if (in[offset] == '&') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100366 /* non WS */
367 ws = 0;
Radek Krejcid70d1072018-10-09 14:20:47 +0200368
Michal Vaskob36053d2020-03-26 15:49:30 +0100369 if (!buf) {
370 /* prepare output buffer */
371 buf = malloc(BUFSIZE);
372 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
373 size = BUFSIZE;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200374 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100375
376 /* allocate enough for the offset and next character,
377 * we will need 4 bytes at most since we support only the predefined
378 * (one-char) entities and character references */
Juraj Vijtiukcb017cc2020-07-08 16:19:58 +0200379 while (len + offset + 4 >= size) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100380 buf = ly_realloc(buf, size + BUFSIZE_STEP);
381 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
382 size += BUFSIZE_STEP;
383 }
384
385 if (offset) {
386 /* store what we have so far */
387 memcpy(&buf[len], in, offset);
388 len += offset;
389 in += offset;
390 offset = 0;
391 }
392
Radek Krejci7a7fa902018-09-25 17:08:21 +0200393 ++offset;
394 if (in[offset] != '#') {
395 /* entity reference - only predefined references are supported */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100396 if (!strncmp(&in[offset], "lt;", ly_strlen_const("lt;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100397 buf[len++] = '<';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100398 in += ly_strlen_const("&lt;");
399 } else if (!strncmp(&in[offset], "gt;", ly_strlen_const("gt;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100400 buf[len++] = '>';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100401 in += ly_strlen_const("&gt;");
402 } else if (!strncmp(&in[offset], "amp;", ly_strlen_const("amp;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100403 buf[len++] = '&';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100404 in += ly_strlen_const("&amp;");
405 } else if (!strncmp(&in[offset], "apos;", ly_strlen_const("apos;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100406 buf[len++] = '\'';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100407 in += ly_strlen_const("&apos;");
408 } else if (!strncmp(&in[offset], "quot;", ly_strlen_const("quot;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100409 buf[len++] = '\"';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100410 in += ly_strlen_const("&quot;");
Radek Krejci7a7fa902018-09-25 17:08:21 +0200411 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100412 LOGVAL(ctx, LYVE_SYNTAX, "Entity reference \"%.*s\" not supported, only predefined references allowed.",
413 10, &in[offset - 1]);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200414 goto error;
415 }
416 offset = 0;
417 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100418 p = (void *)&in[offset - 1];
Radek Krejci7a7fa902018-09-25 17:08:21 +0200419 /* character reference */
420 ++offset;
421 if (isdigit(in[offset])) {
422 for (n = 0; isdigit(in[offset]); offset++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100423 n = (LY_BASE_DEC * n) + (in[offset] - '0');
Radek Krejci7a7fa902018-09-25 17:08:21 +0200424 }
Michal Vasko69730152020-10-09 16:30:07 +0200425 } else if ((in[offset] == 'x') && isxdigit(in[offset + 1])) {
Radek Krejci7a7fa902018-09-25 17:08:21 +0200426 for (n = 0, ++offset; isxdigit(in[offset]); offset++) {
427 if (isdigit(in[offset])) {
428 u = (in[offset] - '0');
429 } else if (in[offset] > 'F') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100430 u = LY_BASE_DEC + (in[offset] - 'a');
Radek Krejci7a7fa902018-09-25 17:08:21 +0200431 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100432 u = LY_BASE_DEC + (in[offset] - 'A');
Radek Krejci7a7fa902018-09-25 17:08:21 +0200433 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100434 n = (LY_BASE_HEX * n) + u;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200435 }
436 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100437 LOGVAL(ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200438 goto error;
439
440 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100441
Radek Krejci7a7fa902018-09-25 17:08:21 +0200442 LY_CHECK_ERR_GOTO(in[offset] != ';',
Radek Krejci2efc45b2020-12-22 16:25:44 +0100443 LOGVAL(ctx, LY_VCODE_INSTREXP,
Michal Vasko69730152020-10-09 16:30:07 +0200444 LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"),
445 error);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200446 ++offset;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200447 LY_CHECK_ERR_GOTO(ly_pututf8(&buf[len], n, &u),
Radek Krejci2efc45b2020-12-22 16:25:44 +0100448 LOGVAL(ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n),
Michal Vasko69730152020-10-09 16:30:07 +0200449 error);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200450 len += u;
451 in += offset;
452 offset = 0;
453 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100454 } else if (in[offset] == endchar) {
Radek Krejci7a7fa902018-09-25 17:08:21 +0200455 /* end of string */
Radek Krejcid70d1072018-10-09 14:20:47 +0200456 if (buf) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100457 /* realloc exact size string */
458 buf = ly_realloc(buf, len + offset + 1);
459 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
460 size = len + offset + 1;
Michal Vasko08e9b112021-06-11 15:41:17 +0200461 if (offset) {
462 memcpy(&buf[len], in, offset);
463 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100464
465 /* set terminating NULL byte */
466 buf[len + offset] = '\0';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200467 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200468 len += offset;
Michal Vaskob36053d2020-03-26 15:49:30 +0100469 in += offset;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200470 goto success;
471 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100472 if (!is_xmlws(in[offset])) {
473 /* non WS */
474 ws = 0;
475 }
476
Radek Krejci7a7fa902018-09-25 17:08:21 +0200477 /* log lines */
478 if (in[offset] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100479 LY_IN_NEW_LINE(xmlctx->in);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200480 }
481
482 /* continue */
aPiecekb287b212021-05-04 14:24:25 +0200483 in_aux = &in[offset];
484 LY_CHECK_ERR_GOTO(ly_getutf8(&in_aux, &n, &u),
485 LOGVAL(ctx, LY_VCODE_INCHAR, in[offset]), error);
486 offset += u;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200487 }
488 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100489
490 /* EOF reached before endchar */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100491 LOGVAL(ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100492
Radek Krejci7a7fa902018-09-25 17:08:21 +0200493error:
Michal Vaskob36053d2020-03-26 15:49:30 +0100494 free(buf);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200495 return LY_EVALID;
496
497success:
Radek Krejcid70d1072018-10-09 14:20:47 +0200498 if (buf) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100499 *value = buf;
500 *dynamic = 1;
501 } else {
502 *value = (char *)start;
503 *dynamic = 0;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200504 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100505 *length = len;
506 *ws_only = ws;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200507
Radek Krejcid54412f2020-12-17 20:25:35 +0100508 xmlctx->in->current = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100509 return LY_SUCCESS;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200510
511#undef BUFSIZE
512#undef BUFSIZE_STEP
Radek Krejci7a7fa902018-09-25 17:08:21 +0200513}
514
Michal Vasko8cef5232020-06-15 17:59:47 +0200515/**
516 * @brief Parse XML closing element and match it to a stored starting element.
517 *
518 * @param[in] xmlctx XML context to use.
519 * @param[in] prefix Expected closing element prefix.
520 * @param[in] prefix_len Length of @p prefix.
521 * @param[in] name Expected closing element name.
522 * @param[in] name_len Length of @p name.
523 * @param[in] empty Whether we are parsing a special "empty" element (with joined starting and closing tag) with no value.
524 * @return LY_ERR value.
525 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100526static LY_ERR
527lyxml_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 +0200528 ly_bool empty)
Radek Krejcid972c252018-09-25 13:23:39 +0200529{
Michal Vaskob36053d2020-03-26 15:49:30 +0100530 struct lyxml_elem *e;
Radek Krejcid972c252018-09-25 13:23:39 +0200531
Michal Vaskob36053d2020-03-26 15:49:30 +0100532 /* match opening and closing element tags */
533 if (!xmlctx->elements.count) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100534 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").",
Radek Krejci422afb12021-03-04 16:38:16 +0100535 (int)name_len, name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100536 return LY_EVALID;
537 }
Radek Krejcid972c252018-09-25 13:23:39 +0200538
Michal Vaskob36053d2020-03-26 15:49:30 +0100539 e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1];
Michal Vasko69730152020-10-09 16:30:07 +0200540 if ((e->prefix_len != prefix_len) || (e->name_len != name_len) ||
541 (prefix_len && strncmp(prefix, e->prefix, e->prefix_len)) || strncmp(name, e->name, e->name_len)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100542 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Opening (\"%.*s%s%.*s\") and closing (\"%.*s%s%.*s\") elements tag mismatch.",
Radek Krejci422afb12021-03-04 16:38:16 +0100543 (int)e->prefix_len, e->prefix ? e->prefix : "", e->prefix ? ":" : "", (int)e->name_len, e->name,
544 (int)prefix_len, prefix ? prefix : "", prefix ? ":" : "", (int)name_len, name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100545 return LY_EVALID;
546 }
Radek Krejcid972c252018-09-25 13:23:39 +0200547
Michal Vaskob36053d2020-03-26 15:49:30 +0100548 /* opening and closing element tags matches, remove record from the opening tags list */
549 ly_set_rm_index(&xmlctx->elements, xmlctx->elements.count - 1, free);
Radek Krejcid972c252018-09-25 13:23:39 +0200550
Michal Vaskob36053d2020-03-26 15:49:30 +0100551 /* remove also the namespaces connected with the element */
552 lyxml_ns_rm(xmlctx);
Radek Krejcid972c252018-09-25 13:23:39 +0200553
Michal Vaskob36053d2020-03-26 15:49:30 +0100554 /* skip WS */
555 ign_xmlws(xmlctx);
Radek Krejcid972c252018-09-25 13:23:39 +0200556
Michal Vaskob36053d2020-03-26 15:49:30 +0100557 /* special "<elem/>" element */
Michal Vasko63f3d842020-07-08 10:10:14 +0200558 if (empty && (xmlctx->in->current[0] == '/')) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100559 move_input(xmlctx, 1);
560 }
Michal Vasko52927e22020-03-16 17:26:14 +0100561
Michal Vaskob36053d2020-03-26 15:49:30 +0100562 /* parse closing tag */
Michal Vasko63f3d842020-07-08 10:10:14 +0200563 if (xmlctx->in->current[0] != '>') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100564 LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200565 xmlctx->in->current, "element tag termination ('>')");
Michal Vaskob36053d2020-03-26 15:49:30 +0100566 return LY_EVALID;
567 }
Michal Vasko52927e22020-03-16 17:26:14 +0100568
Michal Vaskob36053d2020-03-26 15:49:30 +0100569 /* move after closing tag without checking for EOF */
Michal Vasko63f3d842020-07-08 10:10:14 +0200570 ly_in_skip(xmlctx->in, 1);
Michal Vasko52927e22020-03-16 17:26:14 +0100571
Radek Krejcid972c252018-09-25 13:23:39 +0200572 return LY_SUCCESS;
573}
574
Michal Vasko8cef5232020-06-15 17:59:47 +0200575/**
576 * @brief Store parsed opening element and parse any included namespaces.
577 *
578 * @param[in] xmlctx XML context to use.
579 * @param[in] prefix Parsed starting element prefix.
580 * @param[in] prefix_len Length of @p prefix.
581 * @param[in] name Parsed starting element name.
582 * @param[in] name_len Length of @p name.
583 * @return LY_ERR value.
584 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100585static LY_ERR
586lyxml_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 +0200587{
Michal Vaskob36053d2020-03-26 15:49:30 +0100588 LY_ERR ret = LY_SUCCESS;
589 struct lyxml_elem *e;
590 const char *prev_input;
Michal Vasko4fd91922021-09-15 08:51:06 +0200591 uint64_t prev_line;
Michal Vaskob36053d2020-03-26 15:49:30 +0100592 char *value;
593 size_t parsed, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200594 ly_bool ws_only, dynamic, is_ns;
Michal Vaskob36053d2020-03-26 15:49:30 +0100595 uint32_t c;
Radek Krejcib1890642018-10-03 14:05:40 +0200596
Michal Vaskob36053d2020-03-26 15:49:30 +0100597 /* store element opening tag information */
598 e = malloc(sizeof *e);
599 LY_CHECK_ERR_RET(!e, LOGMEM(xmlctx->ctx), LY_EMEM);
600 e->name = name;
601 e->prefix = prefix;
602 e->name_len = name_len;
603 e->prefix_len = prefix_len;
aPiecek93582ed2021-05-25 14:49:06 +0200604
Radek Krejci3d92e442020-10-12 12:48:13 +0200605 LY_CHECK_RET(ly_set_add(&xmlctx->elements, e, 1, NULL));
aPiecek93582ed2021-05-25 14:49:06 +0200606 if (xmlctx->elements.count > LY_MAX_BLOCK_DEPTH) {
Michal Vasko4fd91922021-09-15 08:51:06 +0200607 LOGERR(xmlctx->ctx, LY_EINVAL, "The maximum number of open elements has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200608 ret = LY_EINVAL;
609 goto cleanup;
610 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100611
612 /* skip WS */
613 ign_xmlws(xmlctx);
614
615 /* parse and store all namespaces */
Michal Vasko63f3d842020-07-08 10:10:14 +0200616 prev_input = xmlctx->in->current;
Michal Vasko4fd91922021-09-15 08:51:06 +0200617 prev_line = xmlctx->in->line;
Michal Vaskob36053d2020-03-26 15:49:30 +0100618 is_ns = 1;
aPiecek785ad3d2021-05-10 15:51:13 +0200619 while ((xmlctx->in->current[0] != '\0') && !(ret = ly_getutf8(&xmlctx->in->current, &c, &parsed))) {
620 if (!is_xmlqnamestartchar(c)) {
621 break;
622 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200623 xmlctx->in->current -= parsed;
Michal Vaskob36053d2020-03-26 15:49:30 +0100624
625 /* parse attribute name */
626 LY_CHECK_GOTO(ret = lyxml_parse_qname(xmlctx, &prefix, &prefix_len, &name, &name_len), cleanup);
627
628 /* parse the value */
629 LY_CHECK_GOTO(ret = lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic), cleanup);
630
631 /* store every namespace */
632 if ((prefix && !ly_strncmp("xmlns", prefix, prefix_len)) || (!prefix && !ly_strncmp("xmlns", name, name_len))) {
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100633 ret = lyxml_ns_add(xmlctx, prefix ? name : NULL, prefix ? name_len : 0,
634 dynamic ? value : strndup(value, value_len));
Michal Vaskob36053d2020-03-26 15:49:30 +0100635 dynamic = 0;
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100636 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100637 } else {
638 /* not a namespace */
639 is_ns = 0;
640 }
641 if (dynamic) {
642 free(value);
643 }
644
645 /* skip WS */
646 ign_xmlws(xmlctx);
647
648 if (is_ns) {
649 /* we can actually skip all the namespaces as there is no reason to parse them again */
Michal Vasko63f3d842020-07-08 10:10:14 +0200650 prev_input = xmlctx->in->current;
Michal Vasko4fd91922021-09-15 08:51:06 +0200651 prev_line = xmlctx->in->line;
Michal Vaskob36053d2020-03-26 15:49:30 +0100652 }
Radek Krejcib1890642018-10-03 14:05:40 +0200653 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100654
655cleanup:
656 if (!ret) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200657 xmlctx->in->current = prev_input;
Michal Vasko4fd91922021-09-15 08:51:06 +0200658 xmlctx->in->line = prev_line;
Michal Vaskob36053d2020-03-26 15:49:30 +0100659 }
660 return ret;
661}
662
Michal Vasko8cef5232020-06-15 17:59:47 +0200663/**
664 * @brief Move parser to the attribute content and parse it.
665 *
666 * @param[in] xmlctx XML context to use.
667 * @param[out] value Parsed attribute value.
668 * @param[out] value_len Length of @p value.
669 * @param[out] ws_only Whether the value is empty/white-spaces only.
670 * @param[out] dynamic Whether the value was dynamically allocated.
671 * @return LY_ERR value.
672 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100673static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200674lyxml_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 +0100675{
676 char quot;
677
678 /* skip WS */
679 ign_xmlws(xmlctx);
680
681 /* skip '=' */
Michal Vasko63f3d842020-07-08 10:10:14 +0200682 if (xmlctx->in->current[0] == '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100683 LOGVAL(xmlctx->ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100684 return LY_EVALID;
Michal Vasko63f3d842020-07-08 10:10:14 +0200685 } else if (xmlctx->in->current[0] != '=') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100686 LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200687 xmlctx->in->current, "'='");
Michal Vaskob36053d2020-03-26 15:49:30 +0100688 return LY_EVALID;
689 }
690 move_input(xmlctx, 1);
691
692 /* skip WS */
693 ign_xmlws(xmlctx);
694
695 /* find quotes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200696 if (xmlctx->in->current[0] == '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100697 LOGVAL(xmlctx->ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100698 return LY_EVALID;
Michal Vasko63f3d842020-07-08 10:10:14 +0200699 } else if ((xmlctx->in->current[0] != '\'') && (xmlctx->in->current[0] != '\"')) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100700 LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200701 xmlctx->in->current, "either single or double quotation mark");
Michal Vaskob36053d2020-03-26 15:49:30 +0100702 return LY_EVALID;
703 }
704
705 /* remember quote */
Michal Vasko63f3d842020-07-08 10:10:14 +0200706 quot = xmlctx->in->current[0];
Michal Vaskob36053d2020-03-26 15:49:30 +0100707 move_input(xmlctx, 1);
708
709 /* parse attribute value */
710 LY_CHECK_RET(lyxml_parse_value(xmlctx, quot, (char **)value, value_len, ws_only, dynamic));
711
712 /* move after ending quote (without checking for EOF) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200713 ly_in_skip(xmlctx->in, 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100714
715 return LY_SUCCESS;
716}
717
Michal Vasko8cef5232020-06-15 17:59:47 +0200718/**
719 * @brief Move parser to the next attribute and parse it.
720 *
721 * @param[in] xmlctx XML context to use.
722 * @param[out] prefix Parsed attribute prefix.
723 * @param[out] prefix_len Length of @p prefix.
724 * @param[out] name Parsed attribute name.
725 * @param[out] name_len Length of @p name.
726 * @return LY_ERR value.
727 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100728static LY_ERR
729lyxml_next_attribute(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
730{
731 const char *in;
732 char *value;
733 uint32_t c;
734 size_t parsed, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200735 ly_bool ws_only, dynamic;
Michal Vaskob36053d2020-03-26 15:49:30 +0100736
737 /* skip WS */
738 ign_xmlws(xmlctx);
739
740 /* parse only possible attributes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200741 while ((xmlctx->in->current[0] != '>') && (xmlctx->in->current[0] != '/')) {
742 in = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100743 if (in[0] == '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100744 LOGVAL(xmlctx->ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100745 return LY_EVALID;
746 } else if ((ly_getutf8(&in, &c, &parsed) || !is_xmlqnamestartchar(c))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100747 LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in - parsed), in - parsed,
Michal Vasko69730152020-10-09 16:30:07 +0200748 "element tag end ('>' or '/>') or an attribute");
Michal Vaskob36053d2020-03-26 15:49:30 +0100749 return LY_EVALID;
750 }
751
752 /* parse attribute name */
753 LY_CHECK_RET(lyxml_parse_qname(xmlctx, prefix, prefix_len, name, name_len));
754
755 if ((!*prefix || ly_strncmp("xmlns", *prefix, *prefix_len)) && (*prefix || ly_strncmp("xmlns", *name, *name_len))) {
756 /* standard attribute */
757 break;
758 }
759
760 /* namespace, skip it */
761 LY_CHECK_RET(lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic));
762 if (dynamic) {
763 free(value);
764 }
765
766 /* skip WS */
767 ign_xmlws(xmlctx);
768 }
769
770 return LY_SUCCESS;
771}
772
Michal Vasko8cef5232020-06-15 17:59:47 +0200773/**
774 * @brief Move parser to the next element and parse it.
775 *
776 * @param[in] xmlctx XML context to use.
777 * @param[out] prefix Parsed element prefix.
778 * @param[out] prefix_len Length of @p prefix.
779 * @param[out] name Parse element name.
780 * @param[out] name_len Length of @p name.
Radek Krejci1deb5be2020-08-26 16:43:36 +0200781 * @param[out] closing Flag if the element is closing (includes '/').
Michal Vasko8cef5232020-06-15 17:59:47 +0200782 * @return LY_ERR value.
783 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100784static LY_ERR
785lyxml_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 +0200786 ly_bool *closing)
Michal Vaskob36053d2020-03-26 15:49:30 +0100787{
788 /* skip WS until EOF or after opening tag '<' */
789 LY_CHECK_RET(lyxml_skip_until_end_or_after_otag(xmlctx));
Michal Vasko63f3d842020-07-08 10:10:14 +0200790 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100791 /* set return values */
792 *prefix = *name = NULL;
793 *prefix_len = *name_len = 0;
794 return LY_SUCCESS;
795 }
796
Michal Vasko63f3d842020-07-08 10:10:14 +0200797 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100798 move_input(xmlctx, 1);
799 *closing = 1;
800 } else {
801 *closing = 0;
802 }
803
804 /* skip WS */
805 ign_xmlws(xmlctx);
806
807 /* parse element name */
808 LY_CHECK_RET(lyxml_parse_qname(xmlctx, prefix, prefix_len, name, name_len));
809
810 return LY_SUCCESS;
811}
812
813LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200814lyxml_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyxml_ctx **xmlctx_p)
Michal Vaskob36053d2020-03-26 15:49:30 +0100815{
816 LY_ERR ret = LY_SUCCESS;
817 struct lyxml_ctx *xmlctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200818 ly_bool closing;
Michal Vaskob36053d2020-03-26 15:49:30 +0100819
820 /* new context */
821 xmlctx = calloc(1, sizeof *xmlctx);
822 LY_CHECK_ERR_RET(!xmlctx, LOGMEM(ctx), LY_EMEM);
823 xmlctx->ctx = ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200824 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100825
Radek Krejciddace2c2021-01-08 11:30:56 +0100826 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100827
Michal Vaskob36053d2020-03-26 15:49:30 +0100828 /* parse next element, if any */
829 LY_CHECK_GOTO(ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name,
Michal Vasko69730152020-10-09 16:30:07 +0200830 &xmlctx->name_len, &closing), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100831
Michal Vasko63f3d842020-07-08 10:10:14 +0200832 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100833 /* update status */
834 xmlctx->status = LYXML_END;
835 } else if (closing) {
Radek Krejci422afb12021-03-04 16:38:16 +0100836 LOGVAL(ctx, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").", (int)xmlctx->name_len, xmlctx->name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100837 ret = LY_EVALID;
838 goto cleanup;
839 } else {
840 /* open an element, also parses all enclosed namespaces */
841 LY_CHECK_GOTO(ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len), cleanup);
842
843 /* update status */
844 xmlctx->status = LYXML_ELEMENT;
845 }
846
847cleanup:
848 if (ret) {
849 lyxml_ctx_free(xmlctx);
850 } else {
851 *xmlctx_p = xmlctx;
852 }
853 return ret;
854}
855
856LY_ERR
857lyxml_ctx_next(struct lyxml_ctx *xmlctx)
858{
859 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200860 ly_bool closing;
Michal Vaskob36053d2020-03-26 15:49:30 +0100861 struct lyxml_elem *e;
862
863 /* if the value was not used, free it */
864 if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) {
865 free((char *)xmlctx->value);
866 xmlctx->value = NULL;
867 xmlctx->dynamic = 0;
868 }
869
870 switch (xmlctx->status) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100871 case LYXML_ELEM_CONTENT:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100872 /* content |</elem> */
873
Michal Vaskob36053d2020-03-26 15:49:30 +0100874 /* handle special case when empty content for "<elem/>" was returned */
Michal Vasko63f3d842020-07-08 10:10:14 +0200875 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100876 assert(xmlctx->elements.count);
877 e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1];
878
879 /* close the element (parses closing tag) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200880 ret = lyxml_close_element(xmlctx, e->prefix, e->prefix_len, e->name, e->name_len, 1);
881 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100882
883 /* update status */
884 xmlctx->status = LYXML_ELEM_CLOSE;
885 break;
886 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100887 /* fall through */
Michal Vaskob36053d2020-03-26 15:49:30 +0100888 case LYXML_ELEM_CLOSE:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100889 /* </elem>| <elem2>* */
890
Michal Vaskob36053d2020-03-26 15:49:30 +0100891 /* parse next element, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +0200892 ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len, &closing);
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] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100896 /* update status */
897 xmlctx->status = LYXML_END;
898 } else if (closing) {
899 /* close an element (parses also closing tag) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200900 ret = lyxml_close_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len, 0);
901 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100902
903 /* update status */
904 xmlctx->status = LYXML_ELEM_CLOSE;
905 } else {
906 /* open an element, also parses all enclosed namespaces */
Michal Vasko63f3d842020-07-08 10:10:14 +0200907 ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len);
908 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100909
910 /* update status */
911 xmlctx->status = LYXML_ELEMENT;
912 }
913 break;
914
Michal Vaskob36053d2020-03-26 15:49:30 +0100915 case LYXML_ELEMENT:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100916 /* <elem| attr='val'* > content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100917 case LYXML_ATTR_CONTENT:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100918 /* attr='val'| attr='val'* > content */
919
Michal Vaskob36053d2020-03-26 15:49:30 +0100920 /* parse attribute name, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +0200921 ret = lyxml_next_attribute(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len);
922 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100923
Michal Vasko63f3d842020-07-08 10:10:14 +0200924 if (xmlctx->in->current[0] == '>') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100925 /* no attributes but a closing tag */
Michal Vasko63f3d842020-07-08 10:10:14 +0200926 ly_in_skip(xmlctx->in, 1);
927 if (!xmlctx->in->current[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100928 LOGVAL(xmlctx->ctx, LY_VCODE_EOF);
Michal Vaskof55ae202020-06-30 15:49:36 +0200929 ret = LY_EVALID;
930 goto cleanup;
931 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100932
933 /* parse element content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200934 ret = lyxml_parse_value(xmlctx, '<', (char **)&xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only,
Michal Vasko69730152020-10-09 16:30:07 +0200935 &xmlctx->dynamic);
Michal Vasko63f3d842020-07-08 10:10:14 +0200936 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100937
938 if (!xmlctx->value_len) {
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100939 /* empty value should by alocated staticaly, but check for in any case */
940 if (xmlctx->dynamic) {
941 free((char *) xmlctx->value);
942 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100943 /* use empty value, easier to work with */
944 xmlctx->value = "";
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100945 xmlctx->dynamic = 0;
Michal Vaskob36053d2020-03-26 15:49:30 +0100946 }
947
948 /* update status */
949 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vasko63f3d842020-07-08 10:10:14 +0200950 } else if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100951 /* no content but we still return it */
952 xmlctx->value = "";
953 xmlctx->value_len = 0;
954 xmlctx->ws_only = 1;
955 xmlctx->dynamic = 0;
956
957 /* update status */
958 xmlctx->status = LYXML_ELEM_CONTENT;
959 } else {
960 /* update status */
961 xmlctx->status = LYXML_ATTRIBUTE;
962 }
963 break;
964
Michal Vaskob36053d2020-03-26 15:49:30 +0100965 case LYXML_ATTRIBUTE:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100966 /* attr|='val' */
967
Michal Vaskob36053d2020-03-26 15:49:30 +0100968 /* skip formatting and parse value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200969 ret = lyxml_next_attr_content(xmlctx, &xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only, &xmlctx->dynamic);
970 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100971
972 /* update status */
973 xmlctx->status = LYXML_ATTR_CONTENT;
974 break;
975
Michal Vaskob36053d2020-03-26 15:49:30 +0100976 case LYXML_END:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100977 /* </elem> |EOF */
Michal Vaskob36053d2020-03-26 15:49:30 +0100978 /* nothing to do */
979 break;
980 }
981
982cleanup:
983 if (ret) {
984 /* invalidate context */
985 xmlctx->status = LYXML_END;
986 }
987 return ret;
988}
989
990LY_ERR
991lyxml_ctx_peek(struct lyxml_ctx *xmlctx, enum LYXML_PARSER_STATUS *next)
992{
993 LY_ERR ret = LY_SUCCESS;
994 const char *prefix, *name, *prev_input;
995 size_t prefix_len, name_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200996 ly_bool closing;
Michal Vaskob36053d2020-03-26 15:49:30 +0100997
Michal Vasko63f3d842020-07-08 10:10:14 +0200998 prev_input = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100999
1000 switch (xmlctx->status) {
1001 case LYXML_ELEM_CONTENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02001002 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +01001003 *next = LYXML_ELEM_CLOSE;
1004 break;
1005 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001006 /* fall through */
Michal Vaskob36053d2020-03-26 15:49:30 +01001007 case LYXML_ELEM_CLOSE:
1008 /* parse next element, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +02001009 ret = lyxml_next_element(xmlctx, &prefix, &prefix_len, &name, &name_len, &closing);
1010 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001011
Michal Vasko63f3d842020-07-08 10:10:14 +02001012 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +01001013 *next = LYXML_END;
1014 } else if (closing) {
1015 *next = LYXML_ELEM_CLOSE;
1016 } else {
1017 *next = LYXML_ELEMENT;
1018 }
1019 break;
1020 case LYXML_ELEMENT:
1021 case LYXML_ATTR_CONTENT:
1022 /* parse attribute name, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +02001023 ret = lyxml_next_attribute(xmlctx, &prefix, &prefix_len, &name, &name_len);
1024 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001025
Michal Vasko63f3d842020-07-08 10:10:14 +02001026 if ((xmlctx->in->current[0] == '>') || (xmlctx->in->current[0] == '/')) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001027 *next = LYXML_ELEM_CONTENT;
1028 } else {
1029 *next = LYXML_ATTRIBUTE;
1030 }
1031 break;
1032 case LYXML_ATTRIBUTE:
1033 *next = LYXML_ATTR_CONTENT;
1034 break;
1035 case LYXML_END:
1036 *next = LYXML_END;
1037 break;
1038 }
1039
1040cleanup:
Michal Vasko63f3d842020-07-08 10:10:14 +02001041 xmlctx->in->current = prev_input;
Michal Vaskob36053d2020-03-26 15:49:30 +01001042 return ret;
1043}
1044
Michal Vaskoda8fbbf2021-06-16 11:44:44 +02001045/**
1046 * @brief Free all namespaces in XML context.
1047 *
1048 * @param[in] xmlctx XML context to use.
1049 */
1050static void
1051lyxml_ns_rm_all(struct lyxml_ctx *xmlctx)
1052{
1053 struct lyxml_ns *ns;
1054 uint32_t i;
1055
1056 for (i = 0; i < xmlctx->ns.count; ++i) {
1057 ns = xmlctx->ns.objs[i];
1058
1059 free(ns->prefix);
1060 free(ns->uri);
1061 free(ns);
1062 }
1063 ly_set_erase(&xmlctx->ns, NULL);
1064}
1065
Michal Vaskob36053d2020-03-26 15:49:30 +01001066void
1067lyxml_ctx_free(struct lyxml_ctx *xmlctx)
1068{
Michal Vaskob36053d2020-03-26 15:49:30 +01001069 if (!xmlctx) {
1070 return;
1071 }
1072
Radek Krejciddace2c2021-01-08 11:30:56 +01001073 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001074
Michal Vaskob36053d2020-03-26 15:49:30 +01001075 if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) {
1076 free((char *)xmlctx->value);
1077 }
1078 ly_set_erase(&xmlctx->elements, free);
Michal Vaskoda8fbbf2021-06-16 11:44:44 +02001079 lyxml_ns_rm_all(xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +01001080 free(xmlctx);
Radek Krejcib1890642018-10-03 14:05:40 +02001081}
Radek Krejcie7b95092019-05-15 11:03:07 +02001082
Michal Vaskoda8fbbf2021-06-16 11:44:44 +02001083/**
1084 * @brief Duplicate an XML element.
1085 *
1086 * @param[in] elem Element to duplicate.
1087 * @return Element duplicate.
1088 * @return NULL on error.
1089 */
1090static struct lyxml_elem *
1091lyxml_elem_dup(const struct lyxml_elem *elem)
1092{
1093 struct lyxml_elem *dup;
1094
1095 dup = malloc(sizeof *dup);
1096 LY_CHECK_ERR_RET(!dup, LOGMEM(NULL), NULL);
1097
1098 memcpy(dup, elem, sizeof *dup);
1099
1100 return dup;
1101}
1102
1103/**
1104 * @brief Duplicate an XML namespace.
1105 *
1106 * @param[in] ns Namespace to duplicate.
1107 * @return Namespace duplicate.
1108 * @return NULL on error.
1109 */
1110static struct lyxml_ns *
1111lyxml_ns_dup(const struct lyxml_ns *ns)
1112{
1113 struct lyxml_ns *dup;
1114
1115 dup = malloc(sizeof *dup);
1116 LY_CHECK_ERR_RET(!dup, LOGMEM(NULL), NULL);
1117
1118 if (ns->prefix) {
1119 dup->prefix = strdup(ns->prefix);
1120 LY_CHECK_ERR_RET(!dup->prefix, LOGMEM(NULL); free(dup), NULL);
1121 } else {
1122 dup->prefix = NULL;
1123 }
1124 dup->uri = strdup(ns->uri);
1125 LY_CHECK_ERR_RET(!dup->uri, LOGMEM(NULL); free(dup->prefix); free(dup), NULL);
1126 dup->depth = ns->depth;
1127
1128 return dup;
1129}
1130
1131LY_ERR
1132lyxml_ctx_backup(struct lyxml_ctx *xmlctx, struct lyxml_ctx *backup)
1133{
1134 uint32_t i;
1135
1136 /* first make shallow copy */
1137 memcpy(backup, xmlctx, sizeof *backup);
1138
1139 if ((xmlctx->status == LYXML_ELEM_CONTENT) && xmlctx->dynamic) {
1140 /* it was backed up, do not free */
1141 xmlctx->dynamic = 0;
1142 }
1143
1144 /* backup in current pointer only */
1145 backup->in = (void *)xmlctx->in->current;
1146
1147 /* duplicate elements */
1148 backup->elements.objs = malloc(xmlctx->elements.size * sizeof(struct lyxml_elem));
1149 for (i = 0; i < xmlctx->elements.count; ++i) {
1150 backup->elements.objs[i] = lyxml_elem_dup(xmlctx->elements.objs[i]);
1151 }
1152
1153 /* duplicate ns */
1154 backup->ns.objs = malloc(xmlctx->ns.size * sizeof(struct lyxml_ns));
1155 for (i = 0; i < xmlctx->ns.count; ++i) {
1156 backup->ns.objs[i] = lyxml_ns_dup(xmlctx->ns.objs[i]);
1157 }
1158
1159 return LY_SUCCESS;
1160}
1161
1162void
1163lyxml_ctx_restore(struct lyxml_ctx *xmlctx, struct lyxml_ctx *backup)
1164{
1165 if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) {
1166 /* free dynamic value */
1167 free((char *)xmlctx->value);
1168 }
1169
1170 /* free elements */
1171 ly_set_erase(&xmlctx->elements, free);
1172
1173 /* free ns */
1174 lyxml_ns_rm_all(xmlctx);
1175
1176 /* restore in current pointer */
1177 xmlctx->in->current = (void *)backup->in;
1178 backup->in = xmlctx->in;
1179
1180 /* restore backup */
1181 memcpy(xmlctx, backup, sizeof *xmlctx);
1182}
1183
Radek Krejcie7b95092019-05-15 11:03:07 +02001184LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001185lyxml_dump_text(struct ly_out *out, const char *text, ly_bool attribute)
Radek Krejcie7b95092019-05-15 11:03:07 +02001186{
Michal Vasko5233e962020-08-14 14:26:20 +02001187 LY_ERR ret;
Radek Krejcie7b95092019-05-15 11:03:07 +02001188
1189 if (!text) {
1190 return 0;
1191 }
1192
Radek Krejci1deb5be2020-08-26 16:43:36 +02001193 for (uint64_t u = 0; text[u]; u++) {
Radek Krejcie7b95092019-05-15 11:03:07 +02001194 switch (text[u]) {
1195 case '&':
Michal Vasko5233e962020-08-14 14:26:20 +02001196 ret = ly_print_(out, "&amp;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001197 break;
1198 case '<':
Michal Vasko5233e962020-08-14 14:26:20 +02001199 ret = ly_print_(out, "&lt;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001200 break;
1201 case '>':
1202 /* not needed, just for readability */
Michal Vasko5233e962020-08-14 14:26:20 +02001203 ret = ly_print_(out, "&gt;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001204 break;
1205 case '"':
1206 if (attribute) {
Michal Vasko5233e962020-08-14 14:26:20 +02001207 ret = ly_print_(out, "&quot;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001208 break;
1209 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001210 /* fall through */
Radek Krejcie7b95092019-05-15 11:03:07 +02001211 default:
Michal Vasko5233e962020-08-14 14:26:20 +02001212 ret = ly_write_(out, &text[u], 1);
1213 break;
Radek Krejcie7b95092019-05-15 11:03:07 +02001214 }
Michal Vasko5233e962020-08-14 14:26:20 +02001215 LY_CHECK_RET(ret);
Radek Krejcie7b95092019-05-15 11:03:07 +02001216 }
1217
Michal Vasko5233e962020-08-14 14:26:20 +02001218 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +02001219}
1220
Michal Vasko52927e22020-03-16 17:26:14 +01001221LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001222lyxml_value_compare(const struct ly_ctx *ctx1, const char *value1, void *val_prefix_data1,
1223 const struct ly_ctx *ctx2, const char *value2, void *val_prefix_data2)
Michal Vasko52927e22020-03-16 17:26:14 +01001224{
aPiecek2f63f952021-03-30 12:22:18 +02001225 const char *value1_iter, *value2_iter;
1226 const char *value1_next, *value2_next;
1227 uint32_t value1_len, value2_len;
1228 ly_bool is_prefix1, is_prefix2;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001229 const struct lys_module *mod1, *mod2;
aPiecek2f63f952021-03-30 12:22:18 +02001230 LY_ERR ret;
Michal Vasko52927e22020-03-16 17:26:14 +01001231
1232 if (!value1 && !value2) {
1233 return LY_SUCCESS;
1234 }
1235 if ((value1 && !value2) || (!value1 && value2)) {
1236 return LY_ENOT;
1237 }
1238
aPiecek2f63f952021-03-30 12:22:18 +02001239 if (!ctx2) {
1240 ctx2 = ctx1;
1241 }
Michal Vasko52927e22020-03-16 17:26:14 +01001242
aPiecek2f63f952021-03-30 12:22:18 +02001243 ret = LY_SUCCESS;
1244 for (value1_iter = value1, value2_iter = value2;
1245 value1_iter && value2_iter;
1246 value1_iter = value1_next, value2_iter = value2_next) {
aPieceke3f828d2021-05-10 15:34:41 +02001247 if ((ret = ly_value_prefix_next(value1_iter, NULL, &value1_len, &is_prefix1, &value1_next))) {
1248 break;
1249 }
1250 if ((ret = ly_value_prefix_next(value2_iter, NULL, &value2_len, &is_prefix2, &value2_next))) {
1251 break;
1252 }
aPiecek2f63f952021-03-30 12:22:18 +02001253
1254 if (is_prefix1 != is_prefix2) {
1255 ret = LY_ENOT;
1256 break;
1257 }
1258
1259 if (!is_prefix1) {
1260 if (value1_len != value2_len) {
1261 ret = LY_ENOT;
1262 break;
1263 }
1264 if (strncmp(value1_iter, value2_iter, value1_len)) {
1265 ret = LY_ENOT;
1266 break;
1267 }
1268 continue;
1269 }
1270
1271 mod1 = mod2 = NULL;
1272 if (val_prefix_data1) {
1273 /* find module of the first prefix, if any */
Radek Krejci8df109d2021-04-23 12:19:08 +02001274 mod1 = ly_resolve_prefix(ctx1, value1_iter, value1_len, LY_VALUE_XML, val_prefix_data1);
aPiecek2f63f952021-03-30 12:22:18 +02001275 }
1276 if (val_prefix_data2) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001277 mod2 = ly_resolve_prefix(ctx2, value2_iter, value2_len, LY_VALUE_XML, val_prefix_data2);
aPiecek2f63f952021-03-30 12:22:18 +02001278 }
1279 if (!mod1 || !mod2) {
1280 /* not a prefix or maps to different namespaces */
1281 ret = LY_ENOT;
1282 break;
1283 }
1284
1285 if (mod1->ctx == mod2->ctx) {
1286 /* same contexts */
1287 if ((mod1->name != mod2->name) || (mod1->revision != mod2->revision)) {
1288 ret = LY_ENOT;
1289 break;
1290 }
1291 } else {
1292 /* different contexts */
1293 if (strcmp(mod1->name, mod2->name)) {
1294 ret = LY_ENOT;
Michal Vasko52927e22020-03-16 17:26:14 +01001295 break;
1296 }
1297
aPiecek2f63f952021-03-30 12:22:18 +02001298 if (mod1->revision || mod2->revision) {
1299 if (!mod1->revision || !mod2->revision) {
1300 ret = LY_ENOT;
1301 break;
1302 }
1303 if (strcmp(mod1->revision, mod2->revision)) {
1304 ret = LY_ENOT;
1305 break;
1306 }
1307 }
Michal Vasko52927e22020-03-16 17:26:14 +01001308 }
Michal Vasko52927e22020-03-16 17:26:14 +01001309 }
1310
aPiecek2f63f952021-03-30 12:22:18 +02001311 if (value1_iter || value2_iter) {
1312 ret = LY_ENOT;
1313 }
1314
1315 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +01001316}