blob: 4be93dd47176cc587942f64642ca18e274d1c7f5 [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{
Michal Vasko4b3f2952022-08-26 09:24:04 +0200159 LY_ERR rc = LY_SUCCESS;
Michal Vaskob36053d2020-03-26 15:49:30 +0100160 struct lyxml_ns *ns;
Michal Vasko4b3f2952022-08-26 09:24:04 +0200161 uint32_t i;
162
163 /* check for duplicates */
164 if (xmlctx->ns.count) {
165 i = xmlctx->ns.count;
166 do {
167 --i;
168 ns = xmlctx->ns.objs[i];
169 if (ns->depth < xmlctx->elements.count) {
170 /* only namespaces of parents, no need to check further */
171 break;
172 } else if (prefix && ns->prefix && !ly_strncmp(ns->prefix, prefix, prefix_len)) {
173 if (!strcmp(ns->uri, uri)) {
174 /* exact same prefix and namespace, ignore */
175 goto cleanup;
176 }
177
178 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Duplicate XML NS prefix \"%s\" used for namespaces \"%s\" and \"%s\".",
179 ns->prefix, ns->uri, uri);
180 rc = LY_EVALID;
181 goto cleanup;
182 } else if (!prefix && !ns->prefix) {
183 if (!strcmp(ns->uri, uri)) {
184 /* exact same default namespace, ignore */
185 goto cleanup;
186 }
187
188 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Duplicate default XML namespaces \"%s\" and \"%s\".", ns->uri, uri);
189 rc = LY_EVALID;
190 goto cleanup;
191 }
192 } while (i);
193 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100194
195 ns = malloc(sizeof *ns);
Michal Vasko4b3f2952022-08-26 09:24:04 +0200196 LY_CHECK_ERR_GOTO(!ns, LOGMEM(xmlctx->ctx); rc = LY_EMEM, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100197
198 /* we need to connect the depth of the element where the namespace is defined with the
199 * namespace record to be able to maintain (remove) the record when the parser leaves
200 * (to its sibling or back to the parent) the element where the namespace was defined */
201 ns->depth = xmlctx->elements.count;
202
203 ns->uri = uri;
204 if (prefix) {
205 ns->prefix = strndup(prefix, prefix_len);
Michal Vasko4b3f2952022-08-26 09:24:04 +0200206 LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(xmlctx->ctx); free(ns); rc = LY_EMEM, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100207 } else {
208 ns->prefix = NULL;
209 }
210
Michal Vasko4b3f2952022-08-26 09:24:04 +0200211 rc = ly_set_add(&xmlctx->ns, ns, 1, NULL);
212 LY_CHECK_ERR_GOTO(rc, free(ns->prefix); free(ns), cleanup);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200213
Michal Vasko4b3f2952022-08-26 09:24:04 +0200214 /* successfully stored */
215 uri = NULL;
216
217cleanup:
218 free(uri);
219 return rc;
Michal Vaskob36053d2020-03-26 15:49:30 +0100220}
221
Michal Vaskob36053d2020-03-26 15:49:30 +0100222void
223lyxml_ns_rm(struct lyxml_ctx *xmlctx)
224{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200225 for (uint32_t u = xmlctx->ns.count - 1; u + 1 > 0; --u) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100226 if (((struct lyxml_ns *)xmlctx->ns.objs[u])->depth != xmlctx->elements.count + 1) {
227 /* we are done, the namespaces from a single element are supposed to be together */
228 break;
229 }
230 /* remove the ns structure */
231 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->prefix);
232 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->uri);
233 free(xmlctx->ns.objs[u]);
234 --xmlctx->ns.count;
235 }
236
237 if (!xmlctx->ns.count) {
238 /* cleanup the xmlctx's namespaces storage */
239 ly_set_erase(&xmlctx->ns, NULL);
240 }
241}
242
Michal Vaskob36053d2020-03-26 15:49:30 +0100243const struct lyxml_ns *
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200244lyxml_ns_get(const struct ly_set *ns_set, const char *prefix, size_t prefix_len)
Michal Vaskob36053d2020-03-26 15:49:30 +0100245{
Michal Vaskob36053d2020-03-26 15:49:30 +0100246 struct lyxml_ns *ns;
247
Radek Krejci1deb5be2020-08-26 16:43:36 +0200248 for (uint32_t u = ns_set->count - 1; u + 1 > 0; --u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200249 ns = (struct lyxml_ns *)ns_set->objs[u];
Michal Vaskob36053d2020-03-26 15:49:30 +0100250 if (prefix && prefix_len) {
251 if (ns->prefix && !ly_strncmp(ns->prefix, prefix, prefix_len)) {
252 return ns;
253 }
254 } else if (!ns->prefix) {
255 /* default namespace */
256 return ns;
257 }
258 }
259
260 return NULL;
261}
262
Michal Vasko8cef5232020-06-15 17:59:47 +0200263/**
264 * @brief Skip in the input until EOF or just after the opening tag.
265 * Handles special XML constructs (comment, cdata, doctype).
266 *
267 * @param[in] xmlctx XML context to use.
268 * @return LY_ERR value.
269 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100270static LY_ERR
271lyxml_skip_until_end_or_after_otag(struct lyxml_ctx *xmlctx)
272{
273 const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */
Michal Vasko63f3d842020-07-08 10:10:14 +0200274 const char *endtag, *sectname;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100275 size_t endtag_len;
Michal Vaskob36053d2020-03-26 15:49:30 +0100276
277 while (1) {
278 ign_xmlws(xmlctx);
279
Michal Vasko63f3d842020-07-08 10:10:14 +0200280 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100281 /* EOF */
282 if (xmlctx->elements.count) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100283 LOGVAL(ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100284 return LY_EVALID;
285 }
286 return LY_SUCCESS;
Michal Vasko63f3d842020-07-08 10:10:14 +0200287 } else if (xmlctx->in->current[0] != '<') {
Michal Vaskob597eef2022-08-26 09:24:27 +0200288 LOGVAL(ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current), xmlctx->in->current,
289 "element tag start ('<')");
Michal Vaskob36053d2020-03-26 15:49:30 +0100290 return LY_EVALID;
291 }
292 move_input(xmlctx, 1);
293
Michal Vasko63f3d842020-07-08 10:10:14 +0200294 if (xmlctx->in->current[0] == '!') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100295 move_input(xmlctx, 1);
296 /* sections to ignore */
Michal Vasko63f3d842020-07-08 10:10:14 +0200297 if (!strncmp(xmlctx->in->current, "--", 2)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100298 /* comment */
299 move_input(xmlctx, 2);
300 sectname = "Comment";
301 endtag = "-->";
Radek Krejcif13b87b2020-12-01 22:02:17 +0100302 endtag_len = ly_strlen_const("-->");
Radek Krejcif13b87b2020-12-01 22:02:17 +0100303 } else if (!strncmp(xmlctx->in->current, "DOCTYPE", ly_strlen_const("DOCTYPE"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100304 /* Document type declaration - not supported */
Michal Vaskob597eef2022-08-26 09:24:27 +0200305 LOGVAL(ctx, LY_VCODE_NSUPP, "Document Type Declaration");
Michal Vaskob36053d2020-03-26 15:49:30 +0100306 return LY_EVALID;
307 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100308 LOGVAL(ctx, LYVE_SYNTAX, "Unknown XML section \"%.20s\".", &xmlctx->in->current[-2]);
Michal Vaskob36053d2020-03-26 15:49:30 +0100309 return LY_EVALID;
310 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100311 LY_CHECK_RET(skip_section(xmlctx, endtag, endtag_len, sectname));
Michal Vasko63f3d842020-07-08 10:10:14 +0200312 } else if (xmlctx->in->current[0] == '?') {
Radek Krejcidd713ce2021-01-04 23:12:12 +0100313 LY_CHECK_RET(skip_section(xmlctx, "?>", 2, "Declaration"));
Michal Vaskob36053d2020-03-26 15:49:30 +0100314 } else {
315 /* other non-WS character */
316 break;
317 }
318 }
319
320 return LY_SUCCESS;
321}
322
Michal Vasko8cef5232020-06-15 17:59:47 +0200323/**
324 * @brief Parse QName.
325 *
326 * @param[in] xmlctx XML context to use.
327 * @param[out] prefix Parsed prefix, may be NULL.
328 * @param[out] prefix_len Length of @p prefix.
329 * @param[out] name Parsed name.
330 * @param[out] name_len Length of @p name.
331 * @return LY_ERR value.
332 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100333static LY_ERR
334lyxml_parse_qname(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
335{
336 const char *start, *end;
337
338 *prefix = NULL;
339 *prefix_len = 0;
340
341 LY_CHECK_RET(lyxml_parse_identifier(xmlctx, &start, &end));
342 if (end[0] == ':') {
343 /* we have prefixed identifier */
344 *prefix = start;
345 *prefix_len = end - start;
346
347 move_input(xmlctx, 1);
348 LY_CHECK_RET(lyxml_parse_identifier(xmlctx, &start, &end));
349 }
350
351 *name = start;
352 *name_len = end - start;
353 return LY_SUCCESS;
354}
355
356/**
Michal Vasko8cf6f722022-02-18 13:08:23 +0100357 * @brief Prepare buffer for new data.
358 *
359 * @param[in] ctx Context for logging.
360 * @param[in,out] in XML input data.
361 * @param[in,out] offset Current offset in @p in.
362 * @param[in] need_space Needed additional free space that is allocated.
363 * @param[in,out] buf Dynamic buffer.
364 * @param[in,out] len Current @p buf length (used characters).
365 * @param[in,out] size Current @p buf size (allocated characters).
366 * @return LY_ERR value.
367 */
368static LY_ERR
369lyxml_parse_value_use_buf(const struct ly_ctx *ctx, const char **in, size_t *offset, size_t need_space, char **buf,
370 size_t *len, size_t *size)
371{
372#define BUFSIZE 24
373#define BUFSIZE_STEP 128
374
375 if (!*buf) {
376 /* prepare output buffer */
377 *buf = malloc(BUFSIZE);
378 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
379 *size = BUFSIZE;
380 }
381
382 /* allocate needed space */
383 while (*len + *offset + need_space >= *size) {
384 *buf = ly_realloc(*buf, *size + BUFSIZE_STEP);
385 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
386 *size += BUFSIZE_STEP;
387 }
388
389 if (*offset) {
390 /* store what we have so far */
391 memcpy(&(*buf)[*len], *in, *offset);
392 *len += *offset;
393 *in += *offset;
394 *offset = 0;
395 }
396
397 return LY_SUCCESS;
398
399#undef BUFSIZE
400#undef BUFSIZE_STEP
401}
402
403/**
Michal Vasko8cef5232020-06-15 17:59:47 +0200404 * @brief Parse XML text content (value).
405 *
406 * @param[in] xmlctx XML context to use.
407 * @param[in] endchar Expected character to mark value end.
408 * @param[out] value Parsed value.
409 * @param[out] length Length of @p value.
410 * @param[out] ws_only Whether the value is empty/white-spaces only.
411 * @param[out] dynamic Whether the value was dynamically allocated.
412 * @return LY_ERR value.
413 */
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200414static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200415lyxml_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 +0200416{
Michal Vaskob36053d2020-03-26 15:49:30 +0100417 const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */
aPiecekb287b212021-05-04 14:24:25 +0200418 const char *in = xmlctx->in->current, *start, *in_aux;
Michal Vaskob36053d2020-03-26 15:49:30 +0100419 char *buf = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +0200420 size_t offset; /* read offset in input buffer */
421 size_t len; /* length of the output string (write offset in output buffer) */
422 size_t size = 0; /* size of the output buffer */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200423 void *p;
Radek Krejci117d2082018-09-26 10:05:14 +0200424 uint32_t n;
Michal Vaskob36053d2020-03-26 15:49:30 +0100425 size_t u;
Radek Krejci857189e2020-09-01 13:26:36 +0200426 ly_bool ws = 1;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200427
Michal Vaskob36053d2020-03-26 15:49:30 +0100428 assert(xmlctx);
Radek Krejcib1890642018-10-03 14:05:40 +0200429
Radek Krejcid70d1072018-10-09 14:20:47 +0200430 /* init */
Michal Vaskob36053d2020-03-26 15:49:30 +0100431 start = in;
Radek Krejcid70d1072018-10-09 14:20:47 +0200432 offset = len = 0;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200433
434 /* parse */
435 while (in[offset]) {
436 if (in[offset] == '&') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100437 /* non WS */
438 ws = 0;
Radek Krejcid70d1072018-10-09 14:20:47 +0200439
Michal Vasko8cf6f722022-02-18 13:08:23 +0100440 /* use buffer and allocate enough for the offset and next character,
Michal Vaskob36053d2020-03-26 15:49:30 +0100441 * we will need 4 bytes at most since we support only the predefined
442 * (one-char) entities and character references */
Michal Vasko8cf6f722022-02-18 13:08:23 +0100443 LY_CHECK_RET(lyxml_parse_value_use_buf(ctx, &in, &offset, 4, &buf, &len, &size));
Michal Vaskob36053d2020-03-26 15:49:30 +0100444
Radek Krejci7a7fa902018-09-25 17:08:21 +0200445 ++offset;
446 if (in[offset] != '#') {
447 /* entity reference - only predefined references are supported */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100448 if (!strncmp(&in[offset], "lt;", ly_strlen_const("lt;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100449 buf[len++] = '<';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100450 in += ly_strlen_const("&lt;");
451 } else if (!strncmp(&in[offset], "gt;", ly_strlen_const("gt;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100452 buf[len++] = '>';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100453 in += ly_strlen_const("&gt;");
454 } else if (!strncmp(&in[offset], "amp;", ly_strlen_const("amp;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100455 buf[len++] = '&';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100456 in += ly_strlen_const("&amp;");
457 } else if (!strncmp(&in[offset], "apos;", ly_strlen_const("apos;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100458 buf[len++] = '\'';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100459 in += ly_strlen_const("&apos;");
460 } else if (!strncmp(&in[offset], "quot;", ly_strlen_const("quot;"))) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100461 buf[len++] = '\"';
Radek Krejcif13b87b2020-12-01 22:02:17 +0100462 in += ly_strlen_const("&quot;");
Radek Krejci7a7fa902018-09-25 17:08:21 +0200463 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100464 LOGVAL(ctx, LYVE_SYNTAX, "Entity reference \"%.*s\" not supported, only predefined references allowed.",
465 10, &in[offset - 1]);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200466 goto error;
467 }
468 offset = 0;
469 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100470 p = (void *)&in[offset - 1];
Radek Krejci7a7fa902018-09-25 17:08:21 +0200471 /* character reference */
472 ++offset;
473 if (isdigit(in[offset])) {
474 for (n = 0; isdigit(in[offset]); offset++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100475 n = (LY_BASE_DEC * n) + (in[offset] - '0');
Radek Krejci7a7fa902018-09-25 17:08:21 +0200476 }
Michal Vasko69730152020-10-09 16:30:07 +0200477 } else if ((in[offset] == 'x') && isxdigit(in[offset + 1])) {
Radek Krejci7a7fa902018-09-25 17:08:21 +0200478 for (n = 0, ++offset; isxdigit(in[offset]); offset++) {
479 if (isdigit(in[offset])) {
480 u = (in[offset] - '0');
481 } else if (in[offset] > 'F') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100482 u = LY_BASE_DEC + (in[offset] - 'a');
Radek Krejci7a7fa902018-09-25 17:08:21 +0200483 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100484 u = LY_BASE_DEC + (in[offset] - 'A');
Radek Krejci7a7fa902018-09-25 17:08:21 +0200485 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100486 n = (LY_BASE_HEX * n) + u;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200487 }
488 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100489 LOGVAL(ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200490 goto error;
491
492 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100493
Michal Vasko8cf6f722022-02-18 13:08:23 +0100494 if (in[offset] != ';') {
495 LOGVAL(ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";");
496 goto error;
497 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200498 ++offset;
Michal Vasko8cf6f722022-02-18 13:08:23 +0100499 if (ly_pututf8(&buf[len], n, &u)) {
500 LOGVAL(ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n);
501 goto error;
502 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200503 len += u;
504 in += offset;
505 offset = 0;
506 }
Michal Vasko8cf6f722022-02-18 13:08:23 +0100507 } else if (!strncmp(in + offset, "<![CDATA[", ly_strlen_const("<![CDATA["))) {
508 /* CDATA, find the end */
509 in_aux = strstr(in + offset + ly_strlen_const("<![CDATA["), "]]>");
510 if (!in_aux) {
511 LOGVAL(xmlctx->ctx, LY_VCODE_NTERM, "CDATA");
512 goto error;
513 }
514 u = in_aux - (in + offset + ly_strlen_const("<![CDATA["));
515
516 /* use buffer, allocate enough for the whole CDATA */
517 LY_CHECK_RET(lyxml_parse_value_use_buf(ctx, &in, &offset, u, &buf, &len, &size));
518
519 /* skip CDATA tag */
520 in += ly_strlen_const("<![CDATA[");
521 assert(!offset);
522
523 /* analyze CDATA for non WS and newline chars */
524 for (n = 0; n < u; ++n) {
525 if (in[n] == '\n') {
526 LY_IN_NEW_LINE(xmlctx->in);
527 } else if (!is_xmlws(in[n])) {
528 ws = 0;
529 }
530 }
531
532 /* copy CDATA */
533 memcpy(buf + len, in, u);
534 len += u;
535
536 /* move input skipping the end tag */
537 in += u + ly_strlen_const("]]>");
Michal Vaskob36053d2020-03-26 15:49:30 +0100538 } else if (in[offset] == endchar) {
Radek Krejci7a7fa902018-09-25 17:08:21 +0200539 /* end of string */
Radek Krejcid70d1072018-10-09 14:20:47 +0200540 if (buf) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100541 /* realloc exact size string */
542 buf = ly_realloc(buf, len + offset + 1);
543 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
544 size = len + offset + 1;
Michal Vasko08e9b112021-06-11 15:41:17 +0200545 if (offset) {
546 memcpy(&buf[len], in, offset);
547 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100548
549 /* set terminating NULL byte */
550 buf[len + offset] = '\0';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200551 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200552 len += offset;
Michal Vaskob36053d2020-03-26 15:49:30 +0100553 in += offset;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200554 goto success;
555 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100556 if (!is_xmlws(in[offset])) {
557 /* non WS */
558 ws = 0;
559 }
560
Radek Krejci7a7fa902018-09-25 17:08:21 +0200561 /* log lines */
562 if (in[offset] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100563 LY_IN_NEW_LINE(xmlctx->in);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200564 }
565
566 /* continue */
aPiecekb287b212021-05-04 14:24:25 +0200567 in_aux = &in[offset];
568 LY_CHECK_ERR_GOTO(ly_getutf8(&in_aux, &n, &u),
569 LOGVAL(ctx, LY_VCODE_INCHAR, in[offset]), error);
570 offset += u;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200571 }
572 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100573
574 /* EOF reached before endchar */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100575 LOGVAL(ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100576
Radek Krejci7a7fa902018-09-25 17:08:21 +0200577error:
Michal Vaskob36053d2020-03-26 15:49:30 +0100578 free(buf);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200579 return LY_EVALID;
580
581success:
Radek Krejcid70d1072018-10-09 14:20:47 +0200582 if (buf) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100583 *value = buf;
584 *dynamic = 1;
585 } else {
586 *value = (char *)start;
587 *dynamic = 0;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200588 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100589 *length = len;
590 *ws_only = ws;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200591
Radek Krejcid54412f2020-12-17 20:25:35 +0100592 xmlctx->in->current = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100593 return LY_SUCCESS;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200594}
595
Michal Vasko8cef5232020-06-15 17:59:47 +0200596/**
597 * @brief Parse XML closing element and match it to a stored starting element.
598 *
599 * @param[in] xmlctx XML context to use.
600 * @param[in] prefix Expected closing element prefix.
601 * @param[in] prefix_len Length of @p prefix.
602 * @param[in] name Expected closing element name.
603 * @param[in] name_len Length of @p name.
604 * @param[in] empty Whether we are parsing a special "empty" element (with joined starting and closing tag) with no value.
605 * @return LY_ERR value.
606 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100607static LY_ERR
608lyxml_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 +0200609 ly_bool empty)
Radek Krejcid972c252018-09-25 13:23:39 +0200610{
Michal Vaskob36053d2020-03-26 15:49:30 +0100611 struct lyxml_elem *e;
Radek Krejcid972c252018-09-25 13:23:39 +0200612
Michal Vaskob36053d2020-03-26 15:49:30 +0100613 /* match opening and closing element tags */
614 if (!xmlctx->elements.count) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100615 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").",
Radek Krejci422afb12021-03-04 16:38:16 +0100616 (int)name_len, name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100617 return LY_EVALID;
618 }
Radek Krejcid972c252018-09-25 13:23:39 +0200619
Michal Vaskob36053d2020-03-26 15:49:30 +0100620 e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1];
Michal Vasko69730152020-10-09 16:30:07 +0200621 if ((e->prefix_len != prefix_len) || (e->name_len != name_len) ||
622 (prefix_len && strncmp(prefix, e->prefix, e->prefix_len)) || strncmp(name, e->name, e->name_len)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100623 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Opening (\"%.*s%s%.*s\") and closing (\"%.*s%s%.*s\") elements tag mismatch.",
Radek Krejci422afb12021-03-04 16:38:16 +0100624 (int)e->prefix_len, e->prefix ? e->prefix : "", e->prefix ? ":" : "", (int)e->name_len, e->name,
625 (int)prefix_len, prefix ? prefix : "", prefix ? ":" : "", (int)name_len, name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100626 return LY_EVALID;
627 }
Radek Krejcid972c252018-09-25 13:23:39 +0200628
Michal Vaskob36053d2020-03-26 15:49:30 +0100629 /* opening and closing element tags matches, remove record from the opening tags list */
630 ly_set_rm_index(&xmlctx->elements, xmlctx->elements.count - 1, free);
Radek Krejcid972c252018-09-25 13:23:39 +0200631
Michal Vaskob36053d2020-03-26 15:49:30 +0100632 /* remove also the namespaces connected with the element */
633 lyxml_ns_rm(xmlctx);
Radek Krejcid972c252018-09-25 13:23:39 +0200634
Michal Vaskob36053d2020-03-26 15:49:30 +0100635 /* skip WS */
636 ign_xmlws(xmlctx);
Radek Krejcid972c252018-09-25 13:23:39 +0200637
Michal Vaskob36053d2020-03-26 15:49:30 +0100638 /* special "<elem/>" element */
Michal Vasko63f3d842020-07-08 10:10:14 +0200639 if (empty && (xmlctx->in->current[0] == '/')) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100640 move_input(xmlctx, 1);
641 }
Michal Vasko52927e22020-03-16 17:26:14 +0100642
Michal Vaskob36053d2020-03-26 15:49:30 +0100643 /* parse closing tag */
Michal Vasko63f3d842020-07-08 10:10:14 +0200644 if (xmlctx->in->current[0] != '>') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100645 LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200646 xmlctx->in->current, "element tag termination ('>')");
Michal Vaskob36053d2020-03-26 15:49:30 +0100647 return LY_EVALID;
648 }
Michal Vasko52927e22020-03-16 17:26:14 +0100649
Michal Vaskob36053d2020-03-26 15:49:30 +0100650 /* move after closing tag without checking for EOF */
Michal Vasko63f3d842020-07-08 10:10:14 +0200651 ly_in_skip(xmlctx->in, 1);
Michal Vasko52927e22020-03-16 17:26:14 +0100652
Radek Krejcid972c252018-09-25 13:23:39 +0200653 return LY_SUCCESS;
654}
655
Michal Vasko8cef5232020-06-15 17:59:47 +0200656/**
657 * @brief Store parsed opening element and parse any included namespaces.
658 *
659 * @param[in] xmlctx XML context to use.
660 * @param[in] prefix Parsed starting element prefix.
661 * @param[in] prefix_len Length of @p prefix.
662 * @param[in] name Parsed starting element name.
663 * @param[in] name_len Length of @p name.
664 * @return LY_ERR value.
665 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100666static LY_ERR
667lyxml_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 +0200668{
Michal Vaskob36053d2020-03-26 15:49:30 +0100669 LY_ERR ret = LY_SUCCESS;
670 struct lyxml_elem *e;
671 const char *prev_input;
Michal Vasko4fd91922021-09-15 08:51:06 +0200672 uint64_t prev_line;
Michal Vaskob36053d2020-03-26 15:49:30 +0100673 char *value;
674 size_t parsed, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200675 ly_bool ws_only, dynamic, is_ns;
Michal Vaskob36053d2020-03-26 15:49:30 +0100676 uint32_t c;
Radek Krejcib1890642018-10-03 14:05:40 +0200677
Michal Vaskob36053d2020-03-26 15:49:30 +0100678 /* store element opening tag information */
679 e = malloc(sizeof *e);
680 LY_CHECK_ERR_RET(!e, LOGMEM(xmlctx->ctx), LY_EMEM);
681 e->name = name;
682 e->prefix = prefix;
683 e->name_len = name_len;
684 e->prefix_len = prefix_len;
aPiecek93582ed2021-05-25 14:49:06 +0200685
Radek Krejci3d92e442020-10-12 12:48:13 +0200686 LY_CHECK_RET(ly_set_add(&xmlctx->elements, e, 1, NULL));
aPiecek93582ed2021-05-25 14:49:06 +0200687 if (xmlctx->elements.count > LY_MAX_BLOCK_DEPTH) {
Michal Vasko4fd91922021-09-15 08:51:06 +0200688 LOGERR(xmlctx->ctx, LY_EINVAL, "The maximum number of open elements has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200689 ret = LY_EINVAL;
690 goto cleanup;
691 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100692
693 /* skip WS */
694 ign_xmlws(xmlctx);
695
696 /* parse and store all namespaces */
Michal Vasko63f3d842020-07-08 10:10:14 +0200697 prev_input = xmlctx->in->current;
Michal Vasko4fd91922021-09-15 08:51:06 +0200698 prev_line = xmlctx->in->line;
Michal Vaskob36053d2020-03-26 15:49:30 +0100699 is_ns = 1;
aPiecek785ad3d2021-05-10 15:51:13 +0200700 while ((xmlctx->in->current[0] != '\0') && !(ret = ly_getutf8(&xmlctx->in->current, &c, &parsed))) {
701 if (!is_xmlqnamestartchar(c)) {
702 break;
703 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200704 xmlctx->in->current -= parsed;
Michal Vaskob36053d2020-03-26 15:49:30 +0100705
706 /* parse attribute name */
707 LY_CHECK_GOTO(ret = lyxml_parse_qname(xmlctx, &prefix, &prefix_len, &name, &name_len), cleanup);
708
709 /* parse the value */
710 LY_CHECK_GOTO(ret = lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic), cleanup);
711
712 /* store every namespace */
713 if ((prefix && !ly_strncmp("xmlns", prefix, prefix_len)) || (!prefix && !ly_strncmp("xmlns", name, name_len))) {
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100714 ret = lyxml_ns_add(xmlctx, prefix ? name : NULL, prefix ? name_len : 0,
715 dynamic ? value : strndup(value, value_len));
Michal Vaskob36053d2020-03-26 15:49:30 +0100716 dynamic = 0;
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100717 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100718 } else {
719 /* not a namespace */
720 is_ns = 0;
721 }
722 if (dynamic) {
723 free(value);
724 }
725
726 /* skip WS */
727 ign_xmlws(xmlctx);
728
729 if (is_ns) {
730 /* we can actually skip all the namespaces as there is no reason to parse them again */
Michal Vasko63f3d842020-07-08 10:10:14 +0200731 prev_input = xmlctx->in->current;
Michal Vasko4fd91922021-09-15 08:51:06 +0200732 prev_line = xmlctx->in->line;
Michal Vaskob36053d2020-03-26 15:49:30 +0100733 }
Radek Krejcib1890642018-10-03 14:05:40 +0200734 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100735
736cleanup:
737 if (!ret) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200738 xmlctx->in->current = prev_input;
Michal Vasko4fd91922021-09-15 08:51:06 +0200739 xmlctx->in->line = prev_line;
Michal Vaskob36053d2020-03-26 15:49:30 +0100740 }
741 return ret;
742}
743
Michal Vasko8cef5232020-06-15 17:59:47 +0200744/**
745 * @brief Move parser to the attribute content and parse it.
746 *
747 * @param[in] xmlctx XML context to use.
748 * @param[out] value Parsed attribute value.
749 * @param[out] value_len Length of @p value.
750 * @param[out] ws_only Whether the value is empty/white-spaces only.
751 * @param[out] dynamic Whether the value was dynamically allocated.
752 * @return LY_ERR value.
753 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100754static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200755lyxml_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 +0100756{
757 char quot;
758
759 /* skip WS */
760 ign_xmlws(xmlctx);
761
762 /* skip '=' */
Michal Vasko63f3d842020-07-08 10:10:14 +0200763 if (xmlctx->in->current[0] == '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100764 LOGVAL(xmlctx->ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100765 return LY_EVALID;
Michal Vasko63f3d842020-07-08 10:10:14 +0200766 } else if (xmlctx->in->current[0] != '=') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100767 LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200768 xmlctx->in->current, "'='");
Michal Vaskob36053d2020-03-26 15:49:30 +0100769 return LY_EVALID;
770 }
771 move_input(xmlctx, 1);
772
773 /* skip WS */
774 ign_xmlws(xmlctx);
775
776 /* find quotes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200777 if (xmlctx->in->current[0] == '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100778 LOGVAL(xmlctx->ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100779 return LY_EVALID;
Michal Vasko63f3d842020-07-08 10:10:14 +0200780 } else if ((xmlctx->in->current[0] != '\'') && (xmlctx->in->current[0] != '\"')) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100781 LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200782 xmlctx->in->current, "either single or double quotation mark");
Michal Vaskob36053d2020-03-26 15:49:30 +0100783 return LY_EVALID;
784 }
785
786 /* remember quote */
Michal Vasko63f3d842020-07-08 10:10:14 +0200787 quot = xmlctx->in->current[0];
Michal Vaskob36053d2020-03-26 15:49:30 +0100788 move_input(xmlctx, 1);
789
790 /* parse attribute value */
791 LY_CHECK_RET(lyxml_parse_value(xmlctx, quot, (char **)value, value_len, ws_only, dynamic));
792
793 /* move after ending quote (without checking for EOF) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200794 ly_in_skip(xmlctx->in, 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100795
796 return LY_SUCCESS;
797}
798
Michal Vasko8cef5232020-06-15 17:59:47 +0200799/**
800 * @brief Move parser to the next attribute and parse it.
801 *
802 * @param[in] xmlctx XML context to use.
803 * @param[out] prefix Parsed attribute prefix.
804 * @param[out] prefix_len Length of @p prefix.
805 * @param[out] name Parsed attribute name.
806 * @param[out] name_len Length of @p name.
807 * @return LY_ERR value.
808 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100809static LY_ERR
810lyxml_next_attribute(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
811{
812 const char *in;
813 char *value;
814 uint32_t c;
815 size_t parsed, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200816 ly_bool ws_only, dynamic;
Michal Vaskob36053d2020-03-26 15:49:30 +0100817
818 /* skip WS */
819 ign_xmlws(xmlctx);
820
821 /* parse only possible attributes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200822 while ((xmlctx->in->current[0] != '>') && (xmlctx->in->current[0] != '/')) {
823 in = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100824 if (in[0] == '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100825 LOGVAL(xmlctx->ctx, LY_VCODE_EOF);
Michal Vaskob36053d2020-03-26 15:49:30 +0100826 return LY_EVALID;
827 } else if ((ly_getutf8(&in, &c, &parsed) || !is_xmlqnamestartchar(c))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100828 LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in - parsed), in - parsed,
Michal Vasko69730152020-10-09 16:30:07 +0200829 "element tag end ('>' or '/>') or an attribute");
Michal Vaskob36053d2020-03-26 15:49:30 +0100830 return LY_EVALID;
831 }
832
833 /* parse attribute name */
834 LY_CHECK_RET(lyxml_parse_qname(xmlctx, prefix, prefix_len, name, name_len));
835
836 if ((!*prefix || ly_strncmp("xmlns", *prefix, *prefix_len)) && (*prefix || ly_strncmp("xmlns", *name, *name_len))) {
837 /* standard attribute */
838 break;
839 }
840
841 /* namespace, skip it */
842 LY_CHECK_RET(lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic));
843 if (dynamic) {
844 free(value);
845 }
846
847 /* skip WS */
848 ign_xmlws(xmlctx);
849 }
850
851 return LY_SUCCESS;
852}
853
Michal Vasko8cef5232020-06-15 17:59:47 +0200854/**
855 * @brief Move parser to the next element and parse it.
856 *
857 * @param[in] xmlctx XML context to use.
858 * @param[out] prefix Parsed element prefix.
859 * @param[out] prefix_len Length of @p prefix.
860 * @param[out] name Parse element name.
861 * @param[out] name_len Length of @p name.
Radek Krejci1deb5be2020-08-26 16:43:36 +0200862 * @param[out] closing Flag if the element is closing (includes '/').
Michal Vasko8cef5232020-06-15 17:59:47 +0200863 * @return LY_ERR value.
864 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100865static LY_ERR
866lyxml_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 +0200867 ly_bool *closing)
Michal Vaskob36053d2020-03-26 15:49:30 +0100868{
869 /* skip WS until EOF or after opening tag '<' */
870 LY_CHECK_RET(lyxml_skip_until_end_or_after_otag(xmlctx));
Michal Vasko63f3d842020-07-08 10:10:14 +0200871 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100872 /* set return values */
873 *prefix = *name = NULL;
874 *prefix_len = *name_len = 0;
875 return LY_SUCCESS;
876 }
877
Michal Vasko63f3d842020-07-08 10:10:14 +0200878 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100879 move_input(xmlctx, 1);
880 *closing = 1;
881 } else {
882 *closing = 0;
883 }
884
885 /* skip WS */
886 ign_xmlws(xmlctx);
887
888 /* parse element name */
889 LY_CHECK_RET(lyxml_parse_qname(xmlctx, prefix, prefix_len, name, name_len));
890
891 return LY_SUCCESS;
892}
893
894LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200895lyxml_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyxml_ctx **xmlctx_p)
Michal Vaskob36053d2020-03-26 15:49:30 +0100896{
897 LY_ERR ret = LY_SUCCESS;
898 struct lyxml_ctx *xmlctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200899 ly_bool closing;
Michal Vaskob36053d2020-03-26 15:49:30 +0100900
901 /* new context */
902 xmlctx = calloc(1, sizeof *xmlctx);
903 LY_CHECK_ERR_RET(!xmlctx, LOGMEM(ctx), LY_EMEM);
904 xmlctx->ctx = ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200905 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100906
Radek Krejciddace2c2021-01-08 11:30:56 +0100907 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100908
Michal Vaskob36053d2020-03-26 15:49:30 +0100909 /* parse next element, if any */
910 LY_CHECK_GOTO(ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name,
Michal Vasko69730152020-10-09 16:30:07 +0200911 &xmlctx->name_len, &closing), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100912
Michal Vasko63f3d842020-07-08 10:10:14 +0200913 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100914 /* update status */
915 xmlctx->status = LYXML_END;
916 } else if (closing) {
Radek Krejci422afb12021-03-04 16:38:16 +0100917 LOGVAL(ctx, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").", (int)xmlctx->name_len, xmlctx->name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100918 ret = LY_EVALID;
919 goto cleanup;
920 } else {
921 /* open an element, also parses all enclosed namespaces */
922 LY_CHECK_GOTO(ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len), cleanup);
923
924 /* update status */
925 xmlctx->status = LYXML_ELEMENT;
926 }
927
928cleanup:
929 if (ret) {
930 lyxml_ctx_free(xmlctx);
931 } else {
932 *xmlctx_p = xmlctx;
933 }
934 return ret;
935}
936
937LY_ERR
938lyxml_ctx_next(struct lyxml_ctx *xmlctx)
939{
940 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200941 ly_bool closing;
Michal Vaskob36053d2020-03-26 15:49:30 +0100942 struct lyxml_elem *e;
943
944 /* if the value was not used, free it */
945 if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) {
946 free((char *)xmlctx->value);
947 xmlctx->value = NULL;
948 xmlctx->dynamic = 0;
949 }
950
951 switch (xmlctx->status) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100952 case LYXML_ELEM_CONTENT:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100953 /* content |</elem> */
954
Michal Vaskob36053d2020-03-26 15:49:30 +0100955 /* handle special case when empty content for "<elem/>" was returned */
Michal Vasko63f3d842020-07-08 10:10:14 +0200956 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100957 assert(xmlctx->elements.count);
958 e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1];
959
960 /* close the element (parses closing tag) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200961 ret = lyxml_close_element(xmlctx, e->prefix, e->prefix_len, e->name, e->name_len, 1);
962 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100963
964 /* update status */
965 xmlctx->status = LYXML_ELEM_CLOSE;
966 break;
967 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100968 /* fall through */
Michal Vaskob36053d2020-03-26 15:49:30 +0100969 case LYXML_ELEM_CLOSE:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100970 /* </elem>| <elem2>* */
971
Michal Vaskob36053d2020-03-26 15:49:30 +0100972 /* parse next element, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +0200973 ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len, &closing);
974 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100975
Michal Vasko63f3d842020-07-08 10:10:14 +0200976 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100977 /* update status */
978 xmlctx->status = LYXML_END;
979 } else if (closing) {
980 /* close an element (parses also closing tag) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200981 ret = lyxml_close_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len, 0);
982 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100983
984 /* update status */
985 xmlctx->status = LYXML_ELEM_CLOSE;
986 } else {
987 /* open an element, also parses all enclosed namespaces */
Michal Vasko63f3d842020-07-08 10:10:14 +0200988 ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len);
989 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100990
991 /* update status */
992 xmlctx->status = LYXML_ELEMENT;
993 }
994 break;
995
Michal Vaskob36053d2020-03-26 15:49:30 +0100996 case LYXML_ELEMENT:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100997 /* <elem| attr='val'* > content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100998 case LYXML_ATTR_CONTENT:
Radek Krejcif13b87b2020-12-01 22:02:17 +0100999 /* attr='val'| attr='val'* > content */
1000
Michal Vaskob36053d2020-03-26 15:49:30 +01001001 /* parse attribute name, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +02001002 ret = lyxml_next_attribute(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len);
1003 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001004
Michal Vasko63f3d842020-07-08 10:10:14 +02001005 if (xmlctx->in->current[0] == '>') {
Michal Vaskob36053d2020-03-26 15:49:30 +01001006 /* no attributes but a closing tag */
Michal Vasko63f3d842020-07-08 10:10:14 +02001007 ly_in_skip(xmlctx->in, 1);
1008 if (!xmlctx->in->current[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001009 LOGVAL(xmlctx->ctx, LY_VCODE_EOF);
Michal Vaskof55ae202020-06-30 15:49:36 +02001010 ret = LY_EVALID;
1011 goto cleanup;
1012 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001013
1014 /* parse element content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001015 ret = lyxml_parse_value(xmlctx, '<', (char **)&xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only,
Michal Vasko69730152020-10-09 16:30:07 +02001016 &xmlctx->dynamic);
Michal Vasko63f3d842020-07-08 10:10:14 +02001017 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001018
1019 if (!xmlctx->value_len) {
Radek IÅ¡a017270d2021-02-16 10:26:15 +01001020 /* empty value should by alocated staticaly, but check for in any case */
1021 if (xmlctx->dynamic) {
1022 free((char *) xmlctx->value);
1023 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001024 /* use empty value, easier to work with */
1025 xmlctx->value = "";
Radek IÅ¡a017270d2021-02-16 10:26:15 +01001026 xmlctx->dynamic = 0;
Michal Vaskob36053d2020-03-26 15:49:30 +01001027 }
1028
1029 /* update status */
1030 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vasko63f3d842020-07-08 10:10:14 +02001031 } else if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +01001032 /* no content but we still return it */
1033 xmlctx->value = "";
1034 xmlctx->value_len = 0;
1035 xmlctx->ws_only = 1;
1036 xmlctx->dynamic = 0;
1037
1038 /* update status */
1039 xmlctx->status = LYXML_ELEM_CONTENT;
1040 } else {
1041 /* update status */
1042 xmlctx->status = LYXML_ATTRIBUTE;
1043 }
1044 break;
1045
Michal Vaskob36053d2020-03-26 15:49:30 +01001046 case LYXML_ATTRIBUTE:
Radek Krejcif13b87b2020-12-01 22:02:17 +01001047 /* attr|='val' */
1048
Michal Vaskob36053d2020-03-26 15:49:30 +01001049 /* skip formatting and parse value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001050 ret = lyxml_next_attr_content(xmlctx, &xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only, &xmlctx->dynamic);
1051 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001052
1053 /* update status */
1054 xmlctx->status = LYXML_ATTR_CONTENT;
1055 break;
1056
Michal Vaskob36053d2020-03-26 15:49:30 +01001057 case LYXML_END:
Radek Krejcif13b87b2020-12-01 22:02:17 +01001058 /* </elem> |EOF */
Michal Vaskob36053d2020-03-26 15:49:30 +01001059 /* nothing to do */
1060 break;
1061 }
1062
1063cleanup:
1064 if (ret) {
1065 /* invalidate context */
1066 xmlctx->status = LYXML_END;
1067 }
1068 return ret;
1069}
1070
1071LY_ERR
1072lyxml_ctx_peek(struct lyxml_ctx *xmlctx, enum LYXML_PARSER_STATUS *next)
1073{
1074 LY_ERR ret = LY_SUCCESS;
1075 const char *prefix, *name, *prev_input;
1076 size_t prefix_len, name_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001077 ly_bool closing;
Michal Vaskob36053d2020-03-26 15:49:30 +01001078
Michal Vasko63f3d842020-07-08 10:10:14 +02001079 prev_input = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +01001080
1081 switch (xmlctx->status) {
1082 case LYXML_ELEM_CONTENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02001083 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +01001084 *next = LYXML_ELEM_CLOSE;
1085 break;
1086 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001087 /* fall through */
Michal Vaskob36053d2020-03-26 15:49:30 +01001088 case LYXML_ELEM_CLOSE:
1089 /* parse next element, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +02001090 ret = lyxml_next_element(xmlctx, &prefix, &prefix_len, &name, &name_len, &closing);
1091 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001092
Michal Vasko63f3d842020-07-08 10:10:14 +02001093 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +01001094 *next = LYXML_END;
1095 } else if (closing) {
1096 *next = LYXML_ELEM_CLOSE;
1097 } else {
1098 *next = LYXML_ELEMENT;
1099 }
1100 break;
1101 case LYXML_ELEMENT:
1102 case LYXML_ATTR_CONTENT:
1103 /* parse attribute name, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +02001104 ret = lyxml_next_attribute(xmlctx, &prefix, &prefix_len, &name, &name_len);
1105 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001106
Michal Vasko63f3d842020-07-08 10:10:14 +02001107 if ((xmlctx->in->current[0] == '>') || (xmlctx->in->current[0] == '/')) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001108 *next = LYXML_ELEM_CONTENT;
1109 } else {
1110 *next = LYXML_ATTRIBUTE;
1111 }
1112 break;
1113 case LYXML_ATTRIBUTE:
1114 *next = LYXML_ATTR_CONTENT;
1115 break;
1116 case LYXML_END:
1117 *next = LYXML_END;
1118 break;
1119 }
1120
1121cleanup:
Michal Vasko63f3d842020-07-08 10:10:14 +02001122 xmlctx->in->current = prev_input;
Michal Vaskob36053d2020-03-26 15:49:30 +01001123 return ret;
1124}
1125
Michal Vaskoda8fbbf2021-06-16 11:44:44 +02001126/**
1127 * @brief Free all namespaces in XML context.
1128 *
1129 * @param[in] xmlctx XML context to use.
1130 */
1131static void
1132lyxml_ns_rm_all(struct lyxml_ctx *xmlctx)
1133{
1134 struct lyxml_ns *ns;
1135 uint32_t i;
1136
1137 for (i = 0; i < xmlctx->ns.count; ++i) {
1138 ns = xmlctx->ns.objs[i];
1139
1140 free(ns->prefix);
1141 free(ns->uri);
1142 free(ns);
1143 }
1144 ly_set_erase(&xmlctx->ns, NULL);
1145}
1146
Michal Vaskob36053d2020-03-26 15:49:30 +01001147void
1148lyxml_ctx_free(struct lyxml_ctx *xmlctx)
1149{
Michal Vaskob36053d2020-03-26 15:49:30 +01001150 if (!xmlctx) {
1151 return;
1152 }
1153
Radek Krejciddace2c2021-01-08 11:30:56 +01001154 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001155
Michal Vaskob36053d2020-03-26 15:49:30 +01001156 if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) {
1157 free((char *)xmlctx->value);
1158 }
1159 ly_set_erase(&xmlctx->elements, free);
Michal Vaskoda8fbbf2021-06-16 11:44:44 +02001160 lyxml_ns_rm_all(xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +01001161 free(xmlctx);
Radek Krejcib1890642018-10-03 14:05:40 +02001162}
Radek Krejcie7b95092019-05-15 11:03:07 +02001163
Michal Vaskoda8fbbf2021-06-16 11:44:44 +02001164/**
1165 * @brief Duplicate an XML element.
1166 *
1167 * @param[in] elem Element to duplicate.
1168 * @return Element duplicate.
1169 * @return NULL on error.
1170 */
1171static struct lyxml_elem *
1172lyxml_elem_dup(const struct lyxml_elem *elem)
1173{
1174 struct lyxml_elem *dup;
1175
1176 dup = malloc(sizeof *dup);
1177 LY_CHECK_ERR_RET(!dup, LOGMEM(NULL), NULL);
1178
1179 memcpy(dup, elem, sizeof *dup);
1180
1181 return dup;
1182}
1183
1184/**
1185 * @brief Duplicate an XML namespace.
1186 *
1187 * @param[in] ns Namespace to duplicate.
1188 * @return Namespace duplicate.
1189 * @return NULL on error.
1190 */
1191static struct lyxml_ns *
1192lyxml_ns_dup(const struct lyxml_ns *ns)
1193{
1194 struct lyxml_ns *dup;
1195
1196 dup = malloc(sizeof *dup);
1197 LY_CHECK_ERR_RET(!dup, LOGMEM(NULL), NULL);
1198
1199 if (ns->prefix) {
1200 dup->prefix = strdup(ns->prefix);
1201 LY_CHECK_ERR_RET(!dup->prefix, LOGMEM(NULL); free(dup), NULL);
1202 } else {
1203 dup->prefix = NULL;
1204 }
1205 dup->uri = strdup(ns->uri);
1206 LY_CHECK_ERR_RET(!dup->uri, LOGMEM(NULL); free(dup->prefix); free(dup), NULL);
1207 dup->depth = ns->depth;
1208
1209 return dup;
1210}
1211
1212LY_ERR
1213lyxml_ctx_backup(struct lyxml_ctx *xmlctx, struct lyxml_ctx *backup)
1214{
1215 uint32_t i;
1216
1217 /* first make shallow copy */
1218 memcpy(backup, xmlctx, sizeof *backup);
1219
1220 if ((xmlctx->status == LYXML_ELEM_CONTENT) && xmlctx->dynamic) {
1221 /* it was backed up, do not free */
1222 xmlctx->dynamic = 0;
1223 }
1224
Michal Vasko2b979d62022-05-10 09:28:56 +02001225 /* backup in */
1226 backup->b_current = xmlctx->in->current;
1227 backup->b_line = xmlctx->in->line;
Michal Vaskoda8fbbf2021-06-16 11:44:44 +02001228
1229 /* duplicate elements */
1230 backup->elements.objs = malloc(xmlctx->elements.size * sizeof(struct lyxml_elem));
Michal Vasko0c2403d2021-09-15 08:52:18 +02001231 LY_CHECK_ERR_RET(!backup->elements.objs, LOGMEM(xmlctx->ctx), LY_EMEM);
Michal Vaskoda8fbbf2021-06-16 11:44:44 +02001232 for (i = 0; i < xmlctx->elements.count; ++i) {
1233 backup->elements.objs[i] = lyxml_elem_dup(xmlctx->elements.objs[i]);
Michal Vasko0c2403d2021-09-15 08:52:18 +02001234 LY_CHECK_RET(!backup->elements.objs[i], LY_EMEM);
Michal Vaskoda8fbbf2021-06-16 11:44:44 +02001235 }
1236
1237 /* duplicate ns */
1238 backup->ns.objs = malloc(xmlctx->ns.size * sizeof(struct lyxml_ns));
Michal Vasko0c2403d2021-09-15 08:52:18 +02001239 LY_CHECK_ERR_RET(!backup->ns.objs, LOGMEM(xmlctx->ctx), LY_EMEM);
Michal Vaskoda8fbbf2021-06-16 11:44:44 +02001240 for (i = 0; i < xmlctx->ns.count; ++i) {
1241 backup->ns.objs[i] = lyxml_ns_dup(xmlctx->ns.objs[i]);
Michal Vasko0c2403d2021-09-15 08:52:18 +02001242 LY_CHECK_RET(!backup->ns.objs[i], LY_EMEM);
Michal Vaskoda8fbbf2021-06-16 11:44:44 +02001243 }
1244
1245 return LY_SUCCESS;
1246}
1247
1248void
1249lyxml_ctx_restore(struct lyxml_ctx *xmlctx, struct lyxml_ctx *backup)
1250{
1251 if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) {
1252 /* free dynamic value */
1253 free((char *)xmlctx->value);
1254 }
1255
1256 /* free elements */
1257 ly_set_erase(&xmlctx->elements, free);
1258
1259 /* free ns */
1260 lyxml_ns_rm_all(xmlctx);
1261
Michal Vasko2b979d62022-05-10 09:28:56 +02001262 /* restore in */
1263 xmlctx->in->current = backup->b_current;
1264 xmlctx->in->line = backup->b_line;
Michal Vaskoda8fbbf2021-06-16 11:44:44 +02001265 backup->in = xmlctx->in;
1266
1267 /* restore backup */
1268 memcpy(xmlctx, backup, sizeof *xmlctx);
1269}
1270
Radek Krejcie7b95092019-05-15 11:03:07 +02001271LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001272lyxml_dump_text(struct ly_out *out, const char *text, ly_bool attribute)
Radek Krejcie7b95092019-05-15 11:03:07 +02001273{
Michal Vasko5233e962020-08-14 14:26:20 +02001274 LY_ERR ret;
Radek Krejcie7b95092019-05-15 11:03:07 +02001275
1276 if (!text) {
1277 return 0;
1278 }
1279
Radek Krejci1deb5be2020-08-26 16:43:36 +02001280 for (uint64_t u = 0; text[u]; u++) {
Radek Krejcie7b95092019-05-15 11:03:07 +02001281 switch (text[u]) {
1282 case '&':
Michal Vasko5233e962020-08-14 14:26:20 +02001283 ret = ly_print_(out, "&amp;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001284 break;
1285 case '<':
Michal Vasko5233e962020-08-14 14:26:20 +02001286 ret = ly_print_(out, "&lt;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001287 break;
1288 case '>':
1289 /* not needed, just for readability */
Michal Vasko5233e962020-08-14 14:26:20 +02001290 ret = ly_print_(out, "&gt;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001291 break;
1292 case '"':
1293 if (attribute) {
Michal Vasko5233e962020-08-14 14:26:20 +02001294 ret = ly_print_(out, "&quot;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001295 break;
1296 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001297 /* fall through */
Radek Krejcie7b95092019-05-15 11:03:07 +02001298 default:
Michal Vasko5233e962020-08-14 14:26:20 +02001299 ret = ly_write_(out, &text[u], 1);
1300 break;
Radek Krejcie7b95092019-05-15 11:03:07 +02001301 }
Michal Vasko5233e962020-08-14 14:26:20 +02001302 LY_CHECK_RET(ret);
Radek Krejcie7b95092019-05-15 11:03:07 +02001303 }
1304
Michal Vasko5233e962020-08-14 14:26:20 +02001305 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +02001306}
1307
Michal Vasko52927e22020-03-16 17:26:14 +01001308LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001309lyxml_value_compare(const struct ly_ctx *ctx1, const char *value1, void *val_prefix_data1,
1310 const struct ly_ctx *ctx2, const char *value2, void *val_prefix_data2)
Michal Vasko52927e22020-03-16 17:26:14 +01001311{
aPiecek2f63f952021-03-30 12:22:18 +02001312 const char *value1_iter, *value2_iter;
1313 const char *value1_next, *value2_next;
1314 uint32_t value1_len, value2_len;
1315 ly_bool is_prefix1, is_prefix2;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001316 const struct lys_module *mod1, *mod2;
aPiecek2f63f952021-03-30 12:22:18 +02001317 LY_ERR ret;
Michal Vasko52927e22020-03-16 17:26:14 +01001318
1319 if (!value1 && !value2) {
1320 return LY_SUCCESS;
1321 }
1322 if ((value1 && !value2) || (!value1 && value2)) {
1323 return LY_ENOT;
1324 }
1325
aPiecek2f63f952021-03-30 12:22:18 +02001326 if (!ctx2) {
1327 ctx2 = ctx1;
1328 }
Michal Vasko52927e22020-03-16 17:26:14 +01001329
aPiecek2f63f952021-03-30 12:22:18 +02001330 ret = LY_SUCCESS;
1331 for (value1_iter = value1, value2_iter = value2;
1332 value1_iter && value2_iter;
1333 value1_iter = value1_next, value2_iter = value2_next) {
aPieceke3f828d2021-05-10 15:34:41 +02001334 if ((ret = ly_value_prefix_next(value1_iter, NULL, &value1_len, &is_prefix1, &value1_next))) {
1335 break;
1336 }
1337 if ((ret = ly_value_prefix_next(value2_iter, NULL, &value2_len, &is_prefix2, &value2_next))) {
1338 break;
1339 }
aPiecek2f63f952021-03-30 12:22:18 +02001340
1341 if (is_prefix1 != is_prefix2) {
1342 ret = LY_ENOT;
1343 break;
1344 }
1345
1346 if (!is_prefix1) {
1347 if (value1_len != value2_len) {
1348 ret = LY_ENOT;
1349 break;
1350 }
1351 if (strncmp(value1_iter, value2_iter, value1_len)) {
1352 ret = LY_ENOT;
1353 break;
1354 }
1355 continue;
1356 }
1357
1358 mod1 = mod2 = NULL;
1359 if (val_prefix_data1) {
1360 /* find module of the first prefix, if any */
Radek Krejci8df109d2021-04-23 12:19:08 +02001361 mod1 = ly_resolve_prefix(ctx1, value1_iter, value1_len, LY_VALUE_XML, val_prefix_data1);
aPiecek2f63f952021-03-30 12:22:18 +02001362 }
1363 if (val_prefix_data2) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001364 mod2 = ly_resolve_prefix(ctx2, value2_iter, value2_len, LY_VALUE_XML, val_prefix_data2);
aPiecek2f63f952021-03-30 12:22:18 +02001365 }
1366 if (!mod1 || !mod2) {
1367 /* not a prefix or maps to different namespaces */
1368 ret = LY_ENOT;
1369 break;
1370 }
1371
1372 if (mod1->ctx == mod2->ctx) {
1373 /* same contexts */
1374 if ((mod1->name != mod2->name) || (mod1->revision != mod2->revision)) {
1375 ret = LY_ENOT;
1376 break;
1377 }
1378 } else {
1379 /* different contexts */
1380 if (strcmp(mod1->name, mod2->name)) {
1381 ret = LY_ENOT;
Michal Vasko52927e22020-03-16 17:26:14 +01001382 break;
1383 }
1384
aPiecek2f63f952021-03-30 12:22:18 +02001385 if (mod1->revision || mod2->revision) {
1386 if (!mod1->revision || !mod2->revision) {
1387 ret = LY_ENOT;
1388 break;
1389 }
1390 if (strcmp(mod1->revision, mod2->revision)) {
1391 ret = LY_ENOT;
1392 break;
1393 }
1394 }
Michal Vasko52927e22020-03-16 17:26:14 +01001395 }
Michal Vasko52927e22020-03-16 17:26:14 +01001396 }
1397
aPiecek2f63f952021-03-30 12:22:18 +02001398 if (value1_iter || value2_iter) {
1399 ret = LY_ENOT;
1400 }
1401
1402 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +01001403}